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