-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathword.c
422 lines (383 loc) · 9.19 KB
/
word.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*_ word.c Sun Jun 26 1988 */
/*
* The routines in this file implement commands that work word at a time.
* There are all sorts of word mode commands. If I do any sentence and/or
* paragraph mode commands, they are likely to be put in this file.
*/
#include <stdio.h>
#include <ctype.h>
#include "ed.h"
/* Word wrap. Back-over whatever precedes the point on the current
* line and stop on the first word-break or the beginning of the line. If we
* reach the beginning of the line, jump back to the end of the word and start
* a new line. Otherwise, break the line at the word-break, eat it, and jump
* back to the end of the word.
* NOTE: This function may leaving trailing blanks.
* Returns TRUE on success, FALSE on errors.
*/
word_wrap()
{
register int cnt;
register struct LINE *oldp;
oldp = curwp->w_dotp;
cnt = -1;
do {
cnt++;
if (! backchar(0, 1))
goto err;
} while (! inword());
if (! word_back(FALSE, 1))
goto err;
/* If still on same line (but not at the beginning) */
if (oldp == curwp->w_dotp && curwp->w_doto)
{ int i;
if (!random_backdel(0, 1))
goto err;
if (!random_newline(FALSE, 1))
goto err;
oldp = lback(curwp->w_dotp);
i = 0;
while (1)
{ char c;
c = lgetc(oldp,i);
if (c != ' ' && c != '\t')
break;
line_insert(1,c);
i++;
}
}
while (inword() == TRUE)
if (forwchar(FALSE, 1) == FALSE)
goto err;
return forwchar(FALSE, cnt);
err:
return FALSE;
}
/****************************
* Word wrap the current line.
*/
int word_wrap_line(f, n)
{
int i;
int j;
int col;
int c;
int inword;
int lasti;
LINE* oldp;
LINE* dotpsave;
int dotosave;
if (n < 0)
goto err;
if (window_marking(curwp))
{ REGION region;
int s;
if ((s = getregion(®ion)) != TRUE)
return s;
dotpsave = curwp->w_dotp;
dotosave = curwp->w_doto;
curwp->w_dotp = region.r_linep;
curwp->w_doto = region.r_offset;
n = region.r_nlines;
}
while (n-- > 0)
{
L1:
col = 0;
lasti = 0;
inword = 0;
for (i = 0; i < llength(curwp->w_dotp); i++)
{
c = lgetc(curwp->w_dotp, i);
if (c == ' ' || c == '\t')
{
if (inword)
lasti = i;
inword = 0;
}
else
{
inword = 1;
}
col = getcol(curwp->w_dotp, i);
if (col >= term.t_ncol && lasti)
{
if (!forwchar(0, lasti - curwp->w_doto))
goto err;
if (!random_newline(0,1))
goto err;
/* Remove leading whitespace from new line */
while (1)
{
if (!llength(curwp->w_dotp))
break;
c = lgetc(curwp->w_dotp, 0);
if (c == ' ' || c == '\t')
{
if (!random_forwdel(0, 1))
goto err;
}
else
break;
}
/* Match indenting of original line (oldp) */
oldp = lback(curwp->w_dotp);
for (j = 0; j < llength(oldp); j++)
{
c = lgetc(oldp, j);
if (c == ' ' || c == '\t')
{
if (!line_insert(1, c))
goto err;
}
else
break;
}
goto L1;
}
}
if (!forwline(0, 1))
goto err;
}
if (window_marking(curwp))
{
if (dotosave > llength(dotpsave))
dotosave = llength(dotpsave);
curwp->w_dotp = dotpsave;
curwp->w_doto = dotosave;
}
return TRUE;
err:
return FALSE;
}
/*************************
* Select word that the cursor is on.
*/
word_select(f,n)
{
int inw;
int s;
inw = inword();
do
s = backchar(FALSE, 1);
while (s && inword() == inw);
return s &&
forwchar(FALSE,1) &&
basic_setmark(FALSE,1) &&
word_forw(f,n);
}
/******************************
* Select line that the cursor is on.
*/
word_lineselect(f,n)
{
return (curwp->w_doto == 0 || gotobol(FALSE,1)) &&
basic_setmark(FALSE,1) &&
forwline(f,n);
}
/*
* Move the cursor backward by "n" words. All of the details of motion are
* performed by the "backchar" and "forwchar" routines. Error if you try to
* move beyond the buffers.
*/
word_back(f, n)
{
if (n < 0)
return (word_forw(f, -n));
if (backchar(FALSE, 1) == FALSE)
return (FALSE);
while (n--) {
#if 0
while (inword() == FALSE) {
if (backchar(FALSE, 1) == FALSE)
return (FALSE);
}
while (inword()) {
if (backchar(FALSE, 1) == FALSE)
return (FALSE);
}
#else
int inw;
inw = inword();
do
if (backchar(FALSE, 1) == FALSE)
return (FALSE);
while (inword() == inw);
#endif
}
return (forwchar(FALSE, 1));
}
/*
* Move the cursor forward by the specified number of words. All of the motion
* is done by "forwchar". Error if you try and move beyond the buffer's end.
*/
word_forw(f, n)
{
if (n < 0)
return (word_back(f, -n));
while (n--) {
#if 0
while( inword() == TRUE )
if( forwchar(FALSE, 1) == FALSE)
return( FALSE );
while (inword() == FALSE) {
if (forwchar(FALSE, 1) == FALSE)
return (FALSE);
}
#else
int inw;
inw = inword();
do
if (forwchar(FALSE, 1) == FALSE)
return (FALSE);
while (inword() == inw);
#endif
}
return (TRUE);
}
/*
* Move the cursor forward by the specified number of words. As you move,
* convert any characters to upper case. Error if you try and move beyond the
* end of the buffer. Bound to "M-U".
*/
word_upper(f, n)
{
return word_setcase(f,n,0);
}
/*
* Move the cursor forward by the specified number of words. As you move
* convert characters to lower case. Error if you try and move over the end of
* the buffer. Bound to "M-L".
*/
word_lower(f, n)
{
return word_setcase(f,n,1);
}
/*************************
* Move the cursor forward by the specified number of words. As you move
* convert the first character of the word to upper case, and subsequent
* characters to lower case. Error if you try and move past the end of the
* buffer. Bound to "M-C".
*/
capword(f, n)
{
return word_setcase(f,n,2);
}
int word_setcase(f,n,flag)
{
register int c;
if (n < 0)
return (FALSE);
while (n--) {
while (inword() == FALSE) {
if (forwchar(FALSE, 1) == FALSE)
return (FALSE);
}
if (flag == 2 && inword() != FALSE) {
c = lgetc(curwp->w_dotp, curwp->w_doto);
if (islower(c))
{ c -= 'a'-'A';
lputc(curwp->w_dotp, curwp->w_doto, c);
line_change(WFHARD);
}
if (forwchar(FALSE, 1) == FALSE)
return (FALSE);
}
while (inword() != FALSE) {
c = lgetc(curwp->w_dotp, curwp->w_doto);
switch (flag)
{ case 0:
if (islower(c)) {
c -= 'a'-'A';
goto L1;
}
break;
case 1:
case 2:
if (isupper(c)) {
c += 'a'-'A';
L1: lputc(curwp->w_dotp, curwp->w_doto, c);
line_change(WFHARD);
}
break;
}
if (forwchar(FALSE, 1) == FALSE)
return (FALSE);
}
}
return (TRUE);
}
/*
* Kill forward by "n" words. Remember the location of dot. Move forward by
* the right number of words. Put dot back where it was and issue the kill
* command for the right number of characters. Bound to "M-D".
*/
delfword(f, n)
{
register int size;
register LINE *dotp;
register int doto;
if (n < 0)
return (FALSE);
dotp = curwp->w_dotp;
doto = curwp->w_doto;
size = 0;
while (n--) {
while (inword() == FALSE) {
if (forwchar(FALSE, 1) == FALSE)
return (FALSE);
++size;
}
while (inword() != FALSE) {
if (forwchar(FALSE, 1) == FALSE)
return (FALSE);
++size;
}
}
curwp->w_dotp = dotp;
curwp->w_doto = doto;
return (line_delete(size, TRUE));
}
/*
* Kill backwards by "n" words. Move backwards by the desired number of words,
* counting the characters. When dot is finally moved to its resting place,
* fire off the kill command. Bound to "M-Rubout" and to "M-Backspace".
*/
delbword(f, n)
{
register int size;
if (n < 0)
return (FALSE);
if (backchar(FALSE, 1) == FALSE)
return (FALSE);
size = 0;
while (n--) {
while (inword() == FALSE) {
if (backchar(FALSE, 1) == FALSE)
return (FALSE);
++size;
}
while (inword() != FALSE) {
if (backchar(FALSE, 1) == FALSE)
return (FALSE);
++size;
}
}
if (forwchar(FALSE, 1) == FALSE)
return (FALSE);
return (line_delete(size, TRUE));
}
/*
* Return TRUE if the character at dot is a character that is considered to be
* part of a word. The word character list is hard coded. Should be setable.
* This routine MUST return only a 1 or a 0.
*/
inword()
{
register int c;
if (curwp->w_doto == llength(curwp->w_dotp))
return (FALSE);
c = lgetc(curwp->w_dotp, curwp->w_doto);
return (isalnum(c) ||
c=='$' || c=='_'); /* For identifiers */
}