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