1 /* $OpenBSD: util.c,v 1.51 2024/07/08 14:33:29 op Exp $ */
2
3 /* This file is in the public domain. */
4
5 /*
6 * Assorted commands.
7 * This file contains the command processors for a large assortment of
8 * unrelated commands. The only thing they have in common is that they
9 * are all command processors.
10 */
11
12 #include <sys/queue.h>
13 #include <ctype.h>
14 #include <signal.h>
15 #include <stdio.h>
16
17 #include "def.h"
18
19 int doindent(int);
20
21 /*
22 * Compute next tab stop, with `col' being the a column number and
23 * `tabw' the tab width.
24 */
25 int
ntabstop(int col,int tabw)26 ntabstop(int col, int tabw)
27 {
28 return (((col + tabw) / tabw) * tabw);
29 }
30
31 /*
32 * Display a bunch of useful information about the current location of dot.
33 * The character under the cursor (in octal), the current line, row, and
34 * column, and approximate position of the cursor in the file (as a
35 * percentage) is displayed.
36 * Also included at the moment are some values in parenthesis for debugging
37 * explicit newline inclusion into the buffer.
38 * The column position assumes an infinite
39 * position display; it does not truncate just because the screen does.
40 * This is normally bound to "C-x =".
41 */
42 int
showcpos(int f,int n)43 showcpos(int f, int n)
44 {
45 struct line *clp;
46 char *msg;
47 long nchar, cchar;
48 int nline, row;
49 int cline, cbyte; /* Current line/char/byte */
50 int ratio;
51
52 /* collect the data */
53 clp = bfirstlp(curbp);
54 msg = "Char:";
55 cchar = 0;
56 cline = 0;
57 cbyte = 0;
58 nchar = 0;
59 nline = 0;
60 for (;;) {
61 /* count lines and display total as (raw) 'lines' and
62 compare with b_lines */
63 ++nline;
64 if (clp == curwp->w_dotp) {
65 /* obtain (raw) dot line # and compare with w_dotline */
66 cline = nline;
67 cchar = nchar + curwp->w_doto;
68 if (curwp->w_doto == llength(clp))
69 /* fake a \n at end of line */
70 cbyte = *curbp->b_nlchr;
71 else
72 cbyte = lgetc(clp, curwp->w_doto);
73 }
74 /* include # of chars in this line for point-thru-buff ratio */
75 nchar += llength(clp);
76 clp = lforw(clp);
77 if (clp == curbp->b_headp) {
78 if (cbyte == *curbp->b_nlchr &&
79 cline == curbp->b_lines) {
80 /* swap faked \n for EOB msg */
81 cbyte = EOF;
82 msg = "(EOB)";
83 }
84 break;
85 }
86 /* count the implied newline */
87 nchar++;
88 }
89 /* determine row # within current window */
90 row = curwp->w_toprow + 1;
91 clp = curwp->w_linep;
92 while (clp != curbp->b_headp && clp != curwp->w_dotp) {
93 ++row;
94 clp = lforw(clp);
95 }
96 ratio = nchar ? (100L * cchar) / nchar : 100;
97 ewprintf("%s %c (0%o) point=%ld(%d%%) line=%d row=%d col=%d" \
98 " (blines=%d rlines=%d l_size=%d)", msg,
99 cbyte, cbyte, cchar, ratio, cline, row, getcolpos(curwp),
100 curbp->b_lines, nline, clp->l_size);
101 return (TRUE);
102 }
103
104 int
getcolpos(struct mgwin * wp)105 getcolpos(struct mgwin *wp)
106 {
107 int col, i, c;
108 char tmp[5];
109
110 /* determine column */
111 col = 0;
112
113 for (i = 0; i < wp->w_doto; ++i) {
114 c = lgetc(wp->w_dotp, i);
115 if (c == '\t') {
116 col = ntabstop(col, wp->w_bufp->b_tabw);
117 } else if (ISCTRL(c) != FALSE)
118 col += 2;
119 else if (isprint(c)) {
120 col++;
121 } else {
122 col += snprintf(tmp, sizeof(tmp), "\\%o", c);
123 }
124
125 }
126 return (col);
127 }
128
129 /*
130 * Twiddle the two characters in front of and under dot, then move forward
131 * one character. Treat new-line characters the same as any other.
132 * Normally bound to "C-t". This always works within a line, so "WFEDIT"
133 * is good enough.
134 */
135 int
twiddle(int f,int n)136 twiddle(int f, int n)
137 {
138 struct line *dotp;
139 int doto, cr;
140
141 if (n == 0)
142 return (TRUE);
143
144 dotp = curwp->w_dotp;
145 doto = curwp->w_doto;
146
147 /* Don't twiddle if the dot is on the first char of buffer */
148 if (doto == 0 && lback(dotp) == curbp->b_headp) {
149 dobeep();
150 ewprintf("Beginning of buffer");
151 return(FALSE);
152 }
153 /* Don't twiddle if the dot is on the last char of buffer */
154 if (doto == llength(dotp) && lforw(dotp) == curbp->b_headp) {
155 dobeep();
156 return(FALSE);
157 }
158 undo_boundary_enable(FFRAND, 0);
159 if (doto == 0 && doto == llength(dotp)) { /* only '\n' on this line */
160 (void)forwline(FFRAND, 1);
161 curwp->w_doto = 0;
162 } else {
163 if (doto == 0) { /* 1st twiddle is on 1st character of a line */
164 cr = lgetc(dotp, doto);
165 (void)backdel(FFRAND, 1);
166 (void)forwchar(FFRAND, 1);
167 lnewline();
168 linsert(1, cr);
169 (void)backdel(FFRAND, 1);
170 } else { /* twiddle is elsewhere in line */
171 cr = lgetc(dotp, doto - 1);
172 (void)backdel(FFRAND, 1);
173 (void)forwchar(FFRAND, 1);
174 linsert(1, cr);
175 }
176 }
177 undo_boundary_enable(FFRAND, 1);
178 lchange(WFEDIT);
179 return (TRUE);
180 }
181
182 /*
183 * Open up some blank space. The basic plan is to insert a bunch of
184 * newlines, and then back up over them. Everything is done by the
185 * subcommand processors. They even handle the looping. Normally this
186 * is bound to "C-o".
187 */
188 int
openline(int f,int n)189 openline(int f, int n)
190 {
191 int i, s;
192
193 if (n < 0)
194 return (FALSE);
195 if (n == 0)
196 return (TRUE);
197
198 /* insert newlines */
199 undo_boundary_enable(FFRAND, 0);
200 i = n;
201 do {
202 s = lnewline();
203 } while (s == TRUE && --i);
204
205 /* then go back up overtop of them all */
206 if (s == TRUE)
207 s = backchar(f | FFRAND, n);
208 undo_boundary_enable(FFRAND, 1);
209 return (s);
210 }
211
212 /*
213 * Insert a newline.
214 */
215 int
enewline(int f,int n)216 enewline(int f, int n)
217 {
218 int s;
219
220 if (n < 0)
221 return (FALSE);
222
223 while (n--) {
224 if ((s = lnewline()) != TRUE)
225 return (s);
226 }
227 return (TRUE);
228 }
229
230 /*
231 * Delete blank lines around dot. What this command does depends if dot is
232 * sitting on a blank line. If dot is sitting on a blank line, this command
233 * deletes all the blank lines above and below the current line. If it is
234 * sitting on a non blank line then it deletes all of the blank lines after
235 * the line. Normally this command is bound to "C-x C-o". Any argument is
236 * ignored.
237 */
238 int
deblank(int f,int n)239 deblank(int f, int n)
240 {
241 struct line *lp1, *lp2;
242 RSIZE nld;
243
244 lp1 = curwp->w_dotp;
245 while (llength(lp1) == 0 && (lp2 = lback(lp1)) != curbp->b_headp)
246 lp1 = lp2;
247 lp2 = lp1;
248 nld = (RSIZE)0;
249 while ((lp2 = lforw(lp2)) != curbp->b_headp && llength(lp2) == 0)
250 ++nld;
251 if (nld == 0)
252 return (TRUE);
253 curwp->w_dotp = lforw(lp1);
254 curwp->w_doto = 0;
255 return (ldelete((RSIZE)nld, KNONE));
256 }
257
258 /*
259 * Delete any whitespace around dot, then insert a space.
260 */
261 int
justone(int f,int n)262 justone(int f, int n)
263 {
264 undo_boundary_enable(FFRAND, 0);
265 (void)delwhite(f, n);
266 linsert(1, ' ');
267 undo_boundary_enable(FFRAND, 1);
268 return (TRUE);
269 }
270
271 /*
272 * Delete any whitespace around dot.
273 */
274 int
delwhite(int f,int n)275 delwhite(int f, int n)
276 {
277 int col, s;
278
279 col = curwp->w_doto;
280
281 while (col < llength(curwp->w_dotp) &&
282 (isspace(lgetc(curwp->w_dotp, col))))
283 ++col;
284 do {
285 if (curwp->w_doto == 0) {
286 s = FALSE;
287 break;
288 }
289 if ((s = backchar(FFRAND, 1)) != TRUE)
290 break;
291 } while (isspace(lgetc(curwp->w_dotp, curwp->w_doto)));
292
293 if (s == TRUE)
294 (void)forwchar(FFRAND, 1);
295 (void)ldelete((RSIZE)(col - curwp->w_doto), KNONE);
296 return (TRUE);
297 }
298
299 /*
300 * Delete any leading whitespace on the current line
301 */
302 int
delleadwhite(int f,int n)303 delleadwhite(int f, int n)
304 {
305 int soff, ls;
306 struct line *slp;
307
308 /* Save current position */
309 slp = curwp->w_dotp;
310 soff = curwp->w_doto;
311
312 for (ls = 0; ls < llength(slp); ls++)
313 if (!isspace(lgetc(slp, ls)))
314 break;
315 gotobol(FFRAND, 1);
316 forwdel(FFRAND, ls);
317 soff -= ls;
318 if (soff < 0)
319 soff = 0;
320 forwchar(FFRAND, soff);
321
322 return (TRUE);
323 }
324
325 /*
326 * Delete any trailing whitespace on the current line
327 */
328 int
deltrailwhite(int f,int n)329 deltrailwhite(int f, int n)
330 {
331 int soff;
332
333 /* Save current position */
334 soff = curwp->w_doto;
335
336 gotoeol(FFRAND, 1);
337 delwhite(FFRAND, 1);
338
339 /* restore original position, if possible */
340 if (soff < curwp->w_doto)
341 curwp->w_doto = soff;
342
343 return (TRUE);
344 }
345
346 /*
347 * Raw indent routine. Use spaces and tabs to fill the given number of
348 * cols, but respect no-tab-mode.
349 */
350 int
doindent(int cols)351 doindent(int cols)
352 {
353 int n;
354
355 if (curbp->b_flag & BFNOTAB)
356 return (linsert(cols, ' '));
357 if ((n = cols / curbp->b_tabw) != 0 && linsert(n, '\t') == FALSE)
358 return (FALSE);
359 if ((n = cols % curbp->b_tabw) != 0 && linsert(n, ' ') == FALSE)
360 return (FALSE);
361 return (TRUE);
362 }
363
364 /*
365 * Insert a newline, then enough tabs and spaces to duplicate the indentation
366 * of the previous line, respecting no-tab-mode and the buffer tab width.
367 * Figure out the indentation of the current line. Insert a newline by
368 * calling the standard routine. Insert the indentation by inserting the
369 * right number of tabs and spaces. Return TRUE if all ok. Return FALSE if
370 * one of the subcommands failed. Normally bound to "C-m".
371 */
372 int
lfindent(int f,int n)373 lfindent(int f, int n)
374 {
375 int c, i, nicol;
376 int s = TRUE;
377
378 if (n < 0)
379 return (FALSE);
380
381 undo_boundary_enable(FFRAND, 0);
382 while (n--) {
383 nicol = 0;
384 for (i = 0; i < llength(curwp->w_dotp); ++i) {
385 c = lgetc(curwp->w_dotp, i);
386 if (c != ' ' && c != '\t')
387 break;
388 if (c == '\t')
389 nicol = ntabstop(nicol, curwp->w_bufp->b_tabw);
390 else
391 ++nicol;
392 }
393 (void)delwhite(FFRAND, 1);
394
395 if (lnewline() == FALSE || doindent(nicol) == FALSE) {
396 s = FALSE;
397 break;
398 }
399 }
400 undo_boundary_enable(FFRAND, 1);
401 return (s);
402 }
403
404 /*
405 * Indent the current line. Delete existing leading whitespace,
406 * and use tabs/spaces to achieve correct indentation. Try
407 * to leave dot where it started.
408 */
409 int
indent(int f,int n)410 indent(int f, int n)
411 {
412 int soff;
413
414 if (n < 0)
415 return (FALSE);
416
417 delleadwhite(FFRAND, 1);
418
419 /* If not invoked with a numerical argument, done */
420 if (!(f & FFARG))
421 return (TRUE);
422
423 /* insert appropriate whitespace */
424 soff = curwp->w_doto;
425 (void)gotobol(FFRAND, 1);
426 if (doindent(n) == FALSE)
427 return (FALSE);
428
429 forwchar(FFRAND, soff);
430
431 return (TRUE);
432 }
433
434
435 /*
436 * Delete forward. This is real easy, because the basic delete routine does
437 * all of the work. Watches for negative arguments, and does the right thing.
438 * If any argument is present, it kills rather than deletes, to prevent loss
439 * of text if typed with a big argument. Normally bound to "C-d".
440 */
441 int
forwdel(int f,int n)442 forwdel(int f, int n)
443 {
444 if (n < 0)
445 return (backdel(f | FFRAND, -n));
446
447 /* really a kill */
448 if (f & FFARG) {
449 if ((lastflag & CFKILL) == 0)
450 kdelete();
451 thisflag |= CFKILL;
452 }
453
454 return (ldelete((RSIZE) n, (f & FFARG) ? KFORW : KNONE));
455 }
456
457 /*
458 * Delete backwards. This is quite easy too, because it's all done with
459 * other functions. Just move the cursor back, and delete forwards. Like
460 * delete forward, this actually does a kill if presented with an argument.
461 */
462 int
backdel(int f,int n)463 backdel(int f, int n)
464 {
465 int s;
466
467 if (n < 0)
468 return (forwdel(f | FFRAND, -n));
469
470 /* really a kill */
471 if (f & FFARG) {
472 if ((lastflag & CFKILL) == 0)
473 kdelete();
474 thisflag |= CFKILL;
475 }
476 if ((s = backchar(f | FFRAND, n)) == TRUE)
477 s = ldelete((RSIZE)n, (f & FFARG) ? KFORW : KNONE);
478
479 return (s);
480 }
481
482 int
space_to_tabstop(int f,int n)483 space_to_tabstop(int f, int n)
484 {
485 int col, target;
486
487 if (n < 0)
488 return (FALSE);
489 if (n == 0)
490 return (TRUE);
491
492 col = target = getcolpos(curwp);
493 while (n-- > 0)
494 target = ntabstop(target, curbp->b_tabw);
495 return (linsert(target - col, ' '));
496 }
497
498 /*
499 * Move the dot to the first non-whitespace character of the current line.
500 */
501 int
backtoindent(int f,int n)502 backtoindent(int f, int n)
503 {
504 gotobol(FFRAND, 1);
505 while (curwp->w_doto < llength(curwp->w_dotp) &&
506 (isspace(lgetc(curwp->w_dotp, curwp->w_doto))))
507 ++curwp->w_doto;
508 return (TRUE);
509 }
510
511 /*
512 * Join the current line to the previous, or with arg, the next line
513 * to the current one. If the former line is not empty, leave exactly
514 * one space at the joint. Otherwise, leave no whitespace.
515 */
516 int
joinline(int f,int n)517 joinline(int f, int n)
518 {
519 int doto;
520
521 undo_boundary_enable(FFRAND, 0);
522 if (f & FFARG) {
523 gotoeol(FFRAND, 1);
524 forwdel(FFRAND, 1);
525 } else {
526 gotobol(FFRAND, 1);
527 backdel(FFRAND, 1);
528 }
529
530 delwhite(FFRAND, 1);
531
532 if ((doto = curwp->w_doto) > 0) {
533 linsert(1, ' ');
534 curwp->w_doto = doto;
535 }
536 undo_boundary_enable(FFRAND, 1);
537
538 return (TRUE);
539 }
540