-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathex_voperate.c
369 lines (350 loc) · 5.89 KB
/
ex_voperate.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
#include "ex.h"
#ifdef VISUAL
#include "ex_tty.h"
#include "ex_vis.h"
/*
* Ex - a text editor
* Bill Joy UCB September 1977
*/
#define blank() white(wcursor[0])
static int find(int);
static void word(void (*)(), int);
static void eend(void (*)(), int);
static int edge(void);
static int margin(void);
void
operate(int c, int cnt)
{
register int i;
void (*moveop)(), (*deleteop)();
void (*op)();
char subop;
static char lastFKND, lastFCHR;
moveop = vmove;
deleteop = vdelete;
wcursor = cursor;
dir = 1;
subop = 0;
switch (c) {
case '@':
case CTRL('x'):
wcursor = linebuf;
vdelete('@');
return;
case 'd':
moveop = vdelete;
deleteop = beep;
break;
case 's':
ungetkey(' ');
subop++;
case 'c':
if (c == 'c' && workcmd[0] == 'C')
subop++;
moveop = vchange;
deleteop = beep;
break;
case 'y':
moveop = vyankit;
deleteop = beep;
break;
case 'g':
moveop = vgrabit;
deleteop = beep;
break;
case 'r':
if (cnt > (ssize_t)strlen(cursor))
goto errlab;
if (*cursor == '\t' || Putchar == &listchar)
vgotoCL(column(cursor - 1));
c = getesc();
if (c == 0) {
vfixcurs();
return;
}
ungetkey(c);
strcpy(vutmp, linebuf);
vundkind = VCHNG;
wcursor = cursor + cnt;
setLAST();
CP(cursor, wcursor);
vappend('r', cnt, 0);
*lastcp++ = c;
setLAST();
return;
default:
goto nocount;
}
if (digit(peekkey()) && peekkey() != '0') {
cnt *= vgetcnt();
Xcnt = cnt;
if (cnt < 0) {
beep();
return;
}
}
c = getesc();
if (c == 0)
return;
if (!subop)
*lastcp++ = c;
if (NDSPACE && NDSPACE[0] == c && NDSPACE[1] == 0)
goto space;
nocount:
switch (c) {
case CTRL('w'):
c = 'B';
case 'b':
case 'B':
dir = -1;
case 'W':
case 'w':
wdkind = c & ' ';
op = moveop;
if (edge())
goto errlab;
while (cnt > 0 && !edge()) {
word(op, cnt);
cnt--;
}
break;
#ifdef UNIMP
case 'E':
dir = -1;
case 'e':
wdkind = 1;
op = moveop;
if (edge())
goto errlab;
while (cnt > 1 && !edge()) {
word(op, cnt);
cnt--;
}
eend(op, cnt);
break;
#endif
case '0':
wcursor = linebuf;
op = moveop;
break;
case ';':
if (lastFKND == 0) {
beep();
return;
}
c = lastFKND;
ungetkey(lastFCHR);
subop++;
goto nocount;
case 'F': /* inverted find */
case 'T':
dir = -1;
case 'f': /* find */
case 't':
i = getesc();
if (i == 0)
return;
if (!subop)
*lastcp++ = i;
lastFKND = c;
lastFCHR = i;
while (cnt > 0) {
if (find(i) == 0)
goto errlab;
cnt--;
}
switch (c) {
case 'T':
wcursor++;
break;
case 't':
wcursor--;
case 'f':
fixup:
if (moveop != vmove)
wcursor++;
break;
}
op = moveop;
break;
case '|':
if (Xhadcnt) {
if (Pline == numbline)
cnt += 8;
vmovcol = cnt;
}
vmoving = 1;
wcursor = vfindcol(cnt);
op = moveop;
break;
case '^':
wcursor = vskipwh(linebuf);
op = moveop;
break;
case '$':
wcursor = strend(linebuf) - 1;
goto fixup;
case 'h':
case CTRL('h'):
dir = -1;
case ' ':
space:
case 'l':
op = moveop;
if (margin() || (op == &vmove && edge()))
goto errlab;
while (cnt > 0 && !margin()) {
wcursor += dir;
cnt--;
}
if ((margin() && op == &vmove) || wcursor < linebuf)
wcursor -= dir;
break;
case 'D':
cnt = INF;
goto deleteit;
case 'X': /* inverted delete */
case '#': /* "oex" delete */
dir = -1;
deleteit:
case 'x': /* delete */
if (margin())
goto errlab;
while (cnt > 0 && !margin()) {
wcursor += dir;
cnt--;
}
op = deleteop;
break;
default:
errlab:
beep();
return;
}
(*op)(c);
}
static int
find(int c)
{
for(;;) {
if (edge())
return (0);
wcursor += dir;
if (*wcursor == c)
return (1);
}
}
static void
word(void (*op)(), int cnt)
{
register int which;
register char *iwc;
if (dir == 1) {
iwc = wcursor;
/*
* Word going forward.
* Determine whether the character under
* the cursor is a "word" character.
* If it is, skip through such
* else through nonesuch.
*/
which = wordch(wcursor);
while (!margin() && wordof(which, wcursor))
wcursor++;
/*
* Unless this the last segment of a change
* we want to skip blanks.
*/
if (op != vchange || cnt > 1)
while (!margin() && blank())
wcursor++;
else
if (wcursor == iwc && *iwc)
wcursor++;
/*
* Can't move off end
*/
if (op == vmove && margin())
wcursor--;
} else {
/*
* Word going backwards.
* First skip through blanks, then through word
* or non-word characters.
*/
wcursor--;
while (!margin() && blank())
wcursor--;
if (!margin()) {
which = wordch(wcursor);
while (!margin() && wordof(which, wcursor))
wcursor--;
}
which = wordch(wcursor); /* reported by OI compiler */
if (margin() || !wordof(which, wcursor))
wcursor++;
}
}
#ifdef UNIMP
static void
eend(void (*op)(), int cnt)
{
register int which;
if (dir == 1) {
if (!margin())
wcursor++;
while (!margin() && blank())
wcursor++;
which = wordch(wcursor);
while (!margin() && wordof(which, wcursor))
wcursor++;
if (cnt == 1 && op != &vchange && op != &vdelete)
wcursor--;
} else {
if (!blank()) {
which = wordch(wcursor);
while (!margin() && wordof(which, wcursor))
wcursor--;
}
while (!margin() && blank())
wcursor--;
if (margin())
wcursor++;
}
}
#endif
int
wordof(char which, char *wcursor)
{
if (white(*wcursor))
return (0);
return (!wdkind || wordch(wcursor) == which);
}
int
wordch(char *wcursor)
{
register int c;
c = wcursor[0];
return (letter(c) || digit(c) || c == '_');
}
static int
edge(void)
{
if (linebuf[0] == 0)
return (1);
if (dir == 1)
return (wcursor[1] == 0);
else
return (wcursor == linebuf);
}
static int
margin(void)
{
return (wcursor < linebuf || wcursor[0] == 0);
}
void
beep(void)
{
vputc('\07');
}
#endif