/
opers.c
471 lines (412 loc) · 8.89 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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
* This file contains the command processing functions for the commands
* that take motion operators.
* written for vile. Copyright (c) 1990, 1995-2003 by Paul Fox
*
* $Header: /usr/build/vile/vile/RCS/opers.c,v 1.97 2008/08/17 16:25:49 tom Exp $
*
*/
#include "estruct.h"
#include "edef.h"
#include "nefunc.h"
/* For the "operator" commands -- the following command is a motion, or
* the operator itself is repeated. All operate on regions.
*/
int
vile_op(int f, int n, OpsFunc fn, const char *str)
{
int c = 0;
int thiskey;
int status;
const CMDFUNC *cfp; /* function to execute */
const CMDFUNC *save_cmd_motion = cmd_motion;
BUFFER *ourbp;
#if OPT_MOUSE
WINDOW *wp0 = curwp;
#endif
TRACE((T_CALLED "vile_op(%s)\n", str));
doingopcmd = TRUE;
pre_op_dot = DOT;
ourbp = curbp;
if (havemotion != NULL) {
cfp = havemotion;
havemotion = NULL;
} else {
TBUFF *tok = 0;
mlwrite("%s operation pending...", str);
(void) update(FALSE);
/* get the next command from the keyboard */
/* or a command line, as approp. */
if (clexec) {
char *value = mac_unquotedarg(&tok); /* get the next token */
if (value != 0 && strcmp(value, "lines"))
cfp = engl2fnc(value);
else
cfp = &f_godotplus;
} else {
thiskey = lastkey;
c = kbd_seq();
#if OPT_MOUSE
if (curwp != wp0) {
unkeystroke(c);
doingopcmd = FALSE;
returnCode(FALSE);
}
#endif
/* allow second chance for entering counts */
do_repeats(&c, &f, &n);
if (thiskey == lastkey)
cfp = &f_godotplus;
else
cfp = DefaultKeyBinding(c);
}
if (cfp != 0) {
mlerase();
} else {
if (!clexec) {
char temp[NSTRING];
lsprintf(temp, "(%d)", c);
tb_scopy(&tok, temp);
}
(void) no_such_function(tb_values(tok));
}
tb_free(&tok);
}
if (!cfp) {
status = FALSE;
} else if ((cfp->c_flags & MOTION) == 0) {
kbd_alarm();
status = ABORT;
} else {
/* motion is interpreted as affecting full lines */
if (regionshape == rgn_EXACT) {
if (cfp->c_flags & FL)
regionshape = rgn_FULLLINE;
if (cfp->c_flags & VL_RECT)
regionshape = rgn_RECTANGLE;
}
/* and execute the motion */
if ((status = execute(cfp, f, n)) == TRUE) {
post_op_dot = DOT;
} else {
mlforce("[Motion failed]");
status = FALSE;
}
}
if (status == TRUE) {
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 if (fn == user_operator) {
swapmark();
cmd_motion = cfp;
status = dobuf(find_b_name(str), 1, f ? n : 1);
} else {
status = (fn) ();
}
if (ourbp == curbp) /* in case the func switched buffers on us */
swapmark();
if (regionshape == rgn_FULLLINE)
(void) firstnonwhite(FALSE, 1);
}
regionshape = rgn_EXACT;
doingopcmd = FALSE;
haveregion = FALSE;
cmd_motion = save_cmd_motion;
returnCode(status);
}
int
operdel(int f, int n)
{
int status;
opcmd = OPDEL;
lines_deleted = 0;
status = vile_op(f, n, killregion,
((regionshape == rgn_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 = rgn_FULLLINE;
return operdel(f, n);
}
static int
chgreg(void)
{
if (regionshape == rgn_RECTANGLE) {
return stringrect();
} else {
killregion();
if (regionshape == rgn_FULLLINE && !is_empty_buf(curbp)) {
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 = vile_op(f, n, chgreg, "Change");
if (s == TRUE)
swapmark();
return s;
}
int
operlinechg(int f, int n)
{
int s;
regionshape = rgn_FULLLINE;
opcmd = OPOTHER;
s = vile_op(f, n, chgreg, "Change of full lines");
if (s == TRUE)
swapmark();
return s;
}
int
operjoin(int f, int n)
{
opcmd = OPOTHER;
return vile_op(f, n, joinregion, "Join");
}
int
operjoin_x(int f, int n)
{
opcmd = OPOTHER;
return vile_op(f, n, joinregion_x, "Join Exact");
}
int
operyank(int f, int n)
{
MARK savedot;
REGION region;
int s;
savedot = DOT;
opcmd = OPDEL;
wantregion = ®ion;
s = vile_op(f, n, yankregion, "Yank");
wantregion = 0;
/*
* If the associated motion was to the left, or up, we want to set DOT to
* the beginning of the region, to match vi's behavior. Otherwise leave
* DOT where it is.
*/
if (s == TRUE && b_val(curbp, MDYANKMOTION)) {
if (line_no(curbp, post_op_dot.l) != 0
&& line_no(curbp, post_op_dot.l) < line_no(curbp, savedot.l)) {
savedot = post_op_dot;
} else if (!samepoint(region.r_orig, region.r_end)) {
if (sameline(region.r_orig, savedot)
&& sameline(region.r_end, savedot)
&& region.r_orig.o < savedot.o) {
savedot = region.r_orig;
}
}
}
DOT = savedot;
return s;
}
int
operlineyank(int f, int n)
{
MARK savedot;
int s;
savedot = DOT;
regionshape = rgn_FULLLINE;
opcmd = OPOTHER;
s = vile_op(f, n, yankregion, "Yank of full lines");
DOT = savedot;
return s;
}
int
operflip(int f, int n)
{
opcmd = OPOTHER;
return vile_op(f, n, flipregion, "Flip case");
}
int
operupper(int f, int n)
{
opcmd = OPOTHER;
return vile_op(f, n, upperregion, "Upper case");
}
int
operlower(int f, int n)
{
opcmd = OPOTHER;
return vile_op(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, const char *msg)
{
register int status = FALSE;
regionshape = rgn_FULLLINE;
opcmd = OPOTHER;
if (havemotion != NULL) {
const CMDFUNC *cfp = havemotion;
while (n-- > 0) {
havemotion = cfp;
if ((status = vile_op(FALSE, 1, func, msg)) != TRUE)
break;
}
} else
status = vile_op(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 vile_op(f, n, writeregion, "File write");
}
}
#if OPT_FORMAT
int
operformat(int f, int n)
{
regionshape = rgn_FULLLINE;
opcmd = OPOTHER;
return vile_op(f, n, formatregion, "Format");
}
#endif
#if OPT_SHELL
int
operfilter(int f, int n)
{
regionshape = rgn_FULLLINE;
opcmd = OPOTHER;
return vile_op(f, n, filterregion, "Filter");
}
#endif
int
operprint(int f, int n)
{
regionshape = rgn_FULLLINE;
opcmd = OPOTHER;
return vile_op(f, n, plineregion, "Line print");
}
int
operpprint(int f, int n)
{
regionshape = rgn_FULLLINE;
opcmd = OPOTHER;
return vile_op(f, n, pplineregion, "Line-number print");
}
int
operlist(int f, int n)
{
regionshape = rgn_FULLLINE;
opcmd = OPOTHER;
return vile_op(f, n, llineregion, "Line list");
}
int
opernumber(int f, int n)
{
regionshape = rgn_FULLLINE;
opcmd = OPOTHER;
return vile_op(f, n, nlineregion, "Numbered list");
}
int
opersubst(int f, int n)
{
regionshape = rgn_FULLLINE;
opcmd = OPOTHER;
return vile_op(f, n, substregion, "Substitute");
}
int
opersubstall(int f, int n)
{
regionshape = rgn_FULLLINE;
opcmd = OPOTHER;
return vile_op(f, n, subst_all_region, "Substitute-all");
}
int
opersubstagain(int f, int n)
{
regionshape = rgn_FULLLINE;
opcmd = OPOTHER;
return vile_op(f, n, subst_again_region, "Substitute-again");
}
int
operentab(int f, int n)
{
regionshape = rgn_FULLLINE;
opcmd = OPOTHER;
return vile_op(f, n, entab_region, "Spaces-->Tabs");
}
int
oper_l_entab(int f, int n)
{
regionshape = rgn_FULLLINE;
opcmd = OPOTHER;
return vile_op(f, n, l_entab_region, "Spaces-->Tabs");
}
int
operdetab(int f, int n)
{
regionshape = rgn_FULLLINE;
opcmd = OPOTHER;
return vile_op(f, n, detab_region, "Tabs-->Spaces");
}
int
oper_l_detab(int f, int n)
{
regionshape = rgn_FULLLINE;
opcmd = OPOTHER;
return vile_op(f, n, l_detab_region, "Tabs-->Spaces");
}
int
opertrim(int f, int n)
{
regionshape = rgn_FULLLINE;
opcmd = OPOTHER;
return vile_op(f, n, trim_region, "Trim whitespace");
}
int
operblank(int f, int n)
{
opcmd = OPOTHER;
return vile_op(f, n, blank_region, "Blanking");
}
int
operopenrect(int f, int n)
{
opcmd = OPOTHER;
regionshape = rgn_RECTANGLE;
return vile_op(f, n, openregion, "Opening");
}