/
opers.c
400 lines (343 loc) · 6.62 KB
/
opers.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
/*
* This file contains the command processing functions for the commands
* that take motion operators.
* written for vile: Copyright (c) 1990, 1995 by Paul Fox
*
* $Header: /usr/build/VCS/pgf-vile/RCS/opers.c,v 1.55 1996/02/26 04:24:35 pgf Exp $
*
*/
#include "estruct.h"
#include "edef.h"
#include "nefunc.h"
extern REGION *haveregion;
/* For the "operator" commands -- the following command is a motion, or
* the operator itself is repeated. All operate on regions.
*/
int
operator(int f, int n, OpsFunc fn, const char *str)
{
int c;
int thiskey;
int status;
const CMDFUNC *cfp; /* function to execute */
char tok[NSTRING]; /* command incoming */
BUFFER *ourbp;
#if OPT_MOUSE
WINDOW *wp0 = curwp;
#endif
doingopcmd = TRUE;
pre_op_dot = DOT;
ourbp = curbp;
if (havemotion != NULL) {
cfp = havemotion;
havemotion = NULL;
} else {
mlwrite("%s operation pending...",str);
(void)update(FALSE);
/* get the next command from the keyboard */
/* or a command line, as approp. */
if (clexec) {
macarg(tok); /* get the next token */
if (!strcmp(tok,"lines"))
cfp = &f_godotplus;
else
cfp = engl2fnc(tok);
} else {
thiskey = lastkey;
c = kbd_seq();
#if OPT_MOUSE
if (curwp != wp0) {
unkeystroke(c);
doingopcmd = FALSE;
return FALSE;
}
#endif
/* allow second chance for entering counts */
do_repeats(&c,&f,&n);
if (thiskey == lastkey)
cfp = &f_godotplus;
else
cfp = kcod2fnc(c);
}
if (cfp)
mlerase();
else
mlforce("[No such function]");
}
if (!cfp) {
doingopcmd = FALSE;
return FALSE;
}
if ((cfp->c_flags & MOTION) == 0) {
kbd_alarm();
doingopcmd = FALSE;
return(ABORT);
}
/* motion is interpreted as affecting full lines */
if (regionshape == EXACT) {
if (cfp->c_flags & FL)
regionshape = FULLLINE;
if (cfp->c_flags & RECT)
regionshape = RECTANGLE;
}
/* and execute the motion */
status = execute(cfp, f,n);
if (status != TRUE) {
doingopcmd = FALSE;
regionshape = EXACT;
mlforce("[Motion failed]");
return FALSE;
}
opcmd = 0;
MK = pre_op_dot;
/* we've successfully set up a region */
if (!fn) { /* be defensive */
mlforce("BUG -- null func pointer in operator");
status = FALSE;
} else {
status = (fn)();
}
if (ourbp == curbp) /* in case the func switched buffers on us */
swapmark();
if (regionshape == FULLLINE)
(void)firstnonwhite(FALSE,1);
regionshape = EXACT;
doingopcmd = FALSE;
haveregion = FALSE;
return status;
}
int
operdel(int f, int n)
{
int status;
opcmd = OPDEL;
lines_deleted = 0;
status = operator(f, n, killregion,
regionshape == FULLLINE
? "Delete of full lines"
: "Delete");
if (do_report(lines_deleted))
mlforce("[%d lines deleted]", lines_deleted);
return status;
}
int
operlinedel(int f, int n)
{
regionshape = FULLLINE;
return operdel(f,n);
}
static int
chgreg(void)
{
if (regionshape == RECTANGLE) {
return stringrect();
} else {
killregion();
if (regionshape == FULLLINE) {
if (backline(FALSE,1) == TRUE)
/* backline returns FALSE at top of buf */
return opendown(TRUE,1);
else
return openup(TRUE,1);
}
return ins();
}
}
int
operchg(int f, int n)
{
int s;
opcmd = OPOTHER;
s = operator(f,n,chgreg,"Change");
if (s == TRUE) swapmark();
return s;
}
int
operlinechg(int f, int n)
{
int s;
regionshape = FULLLINE;
opcmd = OPOTHER;
s = operator(f,n,chgreg,"Change of full lines");
if (s == TRUE) swapmark();
return s;
}
int
operjoin(int f, int n)
{
opcmd = OPOTHER;
return operator(f,n,joinregion,"Join");
}
int
operyank(int f, int n)
{
MARK savedot;
int s;
savedot = DOT;
opcmd = OPDEL;
s = operator(f,n,yankregion,"Yank");
DOT = savedot;
return s;
}
int
operlineyank(int f, int n)
{
MARK savedot;
int s;
savedot = DOT;
regionshape = FULLLINE;
opcmd = OPOTHER;
s = operator(f,n,yankregion,"Yank of full lines");
DOT = savedot;
return s;
}
int
operflip(int f, int n)
{
opcmd = OPOTHER;
return operator(f,n,flipregion,"Flip case");
}
int
operupper(int f, int n)
{
opcmd = OPOTHER;
return operator(f,n,upperregion,"Upper case");
}
int
operlower(int f, int n)
{
opcmd = OPOTHER;
return operator(f,n,lowerregion,"Lower case");
}
/*
* The shift commands are special, because vi allows an implicit repeat-count
* to be specified by repeating the '<' or '>' operators.
*/
static int
shift_n_times(int f, int n, OpsFunc func, char *msg)
{
register int status = FALSE;
regionshape = FULLLINE;
opcmd = OPOTHER;
if (havemotion != NULL) {
const CMDFUNC *cfp = havemotion;
while (n-- > 0) {
havemotion = cfp;
if ((status = operator(FALSE,1, func, msg)) != TRUE)
break;
}
} else
status = operator(f,n, func, msg);
return status;
}
int
operlshift(int f, int n)
{
return shift_n_times(f,n,shiftlregion,"Left shift");
}
int
operrshift(int f, int n)
{
return shift_n_times(f,n,shiftrregion,"Right shift");
}
int
operwrite(int f, int n)
{
register int s;
char fname[NFILEN];
if (ukb != 0) {
if ((s=mlreply_file("Write to file: ", (TBUFF **)0,
FILEC_WRITE|FILEC_PROMPT, fname)) != TRUE)
return s;
return kwrite(fname,TRUE);
} else {
opcmd = OPOTHER;
return operator(f,n,writeregion,"File write");
}
}
#if OPT_FORMAT
int
operformat(int f, int n)
{
regionshape = FULLLINE;
opcmd = OPOTHER;
return operator(f,n,formatregion,"Format");
}
#endif
int
operfilter(int f, int n)
{
regionshape = FULLLINE;
opcmd = OPOTHER;
return operator(f,n,filterregion,"Filter");
}
int
operprint(int f, int n)
{
regionshape = FULLLINE;
opcmd = OPOTHER;
return operator(f,n,plineregion,"Line print");
}
int
operlist(int f, int n)
{
regionshape = FULLLINE;
opcmd = OPOTHER;
return operator(f,n,llineregion,"Line list");
}
int
opersubst(int f, int n)
{
regionshape = FULLLINE;
opcmd = OPOTHER;
return operator(f,n,substregion,"Substitute");
}
int
opersubstagain(int f, int n)
{
regionshape = FULLLINE;
opcmd = OPOTHER;
return operator(f,n,subst_again_region,"Substitute-again");
}
#if OPT_AEDIT
int
operentab(int f, int n)
{
regionshape = FULLLINE;
opcmd = OPOTHER;
return operator(f,n,entab_region,"Spaces-->Tabs");
}
#endif
#if OPT_AEDIT
int
operdetab(int f, int n)
{
regionshape = FULLLINE;
opcmd = OPOTHER;
return operator(f,n,detab_region,"Tabs-->Spaces");
}
#endif
#if OPT_AEDIT
int
opertrim(int f, int n)
{
regionshape = FULLLINE;
opcmd = OPOTHER;
return operator(f,n,trim_region,"Trim whitespace");
}
#endif
#if OPT_AEDIT
int
operblank(int f, int n)
{
opcmd = OPOTHER;
return operator(f,n,blank_region,"Blanking");
}
#endif
int
operopenrect(int f, int n)
{
opcmd = OPOTHER;
regionshape = RECTANGLE;
return operator(f,n,openregion,"Opening");
}