Annotation of src/usr.bin/mg/random.c, Revision 1.15
1.15 ! deraadt 1: /* $OpenBSD: random.c,v 1.14 2005/11/18 19:04:09 kjell 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:
12: #include "def.h"
1.9 vincent 13: #include <ctype.h>
1.1 deraadt 14:
15: /*
1.6 mickey 16: * Display a bunch of useful information about the current location of dot.
17: * The character under the cursor (in octal), the current line, row, and
18: * column, and approximate position of the cursor in the file (as a
19: * percentage) is displayed. The column position assumes an infinite
1.3 millert 20: * position display; it does not truncate just because the screen does.
1.1 deraadt 21: * This is normally bound to "C-X =".
22: */
1.2 millert 23: /* ARGSUSED */
1.3 millert 24: int
1.10 cloder 25: showcpos(int f, int n)
1.1 deraadt 26: {
1.15 ! deraadt 27: struct line *clp;
1.11 db 28: long nchar, cchar;
1.3 millert 29: int nline, row;
30: int cline, cbyte; /* Current line/char/byte */
31: int ratio;
32:
33: /* collect the data */
34: clp = lforw(curbp->b_linep);
35: cchar = 0;
36: cline = 0;
37: cbyte = 0;
1.1 deraadt 38: nchar = 0;
39: nline = 0;
1.6 mickey 40: for (;;) {
1.3 millert 41: /* count this line */
42: ++nline;
1.1 deraadt 43: if (clp == curwp->w_dotp) {
1.3 millert 44: /* mark line */
45: cline = nline;
1.1 deraadt 46: cchar = nchar + curwp->w_doto;
47: if (curwp->w_doto == llength(clp))
48: cbyte = '\n';
49: else
50: cbyte = lgetc(clp, curwp->w_doto);
51: }
1.3 millert 52: /* now count the chars */
53: nchar += llength(clp);
1.1 deraadt 54: clp = lforw(clp);
1.2 millert 55: if (clp == curbp->b_linep)
56: break;
1.3 millert 57: /* count the newline */
58: nchar++;
1.1 deraadt 59: }
1.3 millert 60: /* determine row */
61: row = curwp->w_toprow + 1;
1.1 deraadt 62: clp = curwp->w_linep;
1.2 millert 63: while (clp != curbp->b_linep && clp != curwp->w_dotp) {
1.1 deraadt 64: ++row;
65: clp = lforw(clp);
66: }
1.2 millert 67: /* NOSTRICT */
68: ratio = nchar ? (100L * cchar) / nchar : 100;
1.1 deraadt 69: ewprintf("Char: %c (0%o) point=%ld(%d%%) line=%d row=%d col=%d",
1.3 millert 70: cbyte, cbyte, cchar, ratio, cline, row, getcolpos());
1.11 db 71: return (TRUE);
1.1 deraadt 72: }
73:
1.3 millert 74: int
1.9 vincent 75: getcolpos(void)
1.2 millert 76: {
1.3 millert 77: int col, i, c;
78:
79: /* determine column */
1.9 vincent 80: col = 0;
1.1 deraadt 81:
1.2 millert 82: for (i = 0; i < curwp->w_doto; ++i) {
1.1 deraadt 83: c = lgetc(curwp->w_dotp, i);
84: if (c == '\t'
1.3 millert 85: #ifdef NOTAB
1.2 millert 86: && !(curbp->b_flag & BFNOTAB)
1.3 millert 87: #endif /* NOTAB */
1.1 deraadt 88: ) {
1.2 millert 89: col |= 0x07;
1.9 vincent 90: col++;
1.1 deraadt 91: } else if (ISCTRL(c) != FALSE)
1.9 vincent 92: col += 2;
93: else if (isprint(c))
94: col++;
95: else {
96: char tmp[5];
1.11 db 97: snprintf(tmp, sizeof(tmp), "\\%o", c);
1.9 vincent 98: col += strlen(tmp);
99: }
100:
1.1 deraadt 101: }
1.11 db 102: return (col);
1.1 deraadt 103: }
1.3 millert 104:
1.1 deraadt 105: /*
1.6 mickey 106: * Twiddle the two characters on either side of dot. If dot is at the end
107: * of the line twiddle the two characters before it. Return with an error
108: * if dot is at the beginning of line; it seems to be a bit pointless to
109: * make this work. This fixes up a very common typo with a single stroke.
110: * Normally bound to "C-T". This always works within a line, so "WFEDIT"
1.3 millert 111: * is good enough.
1.1 deraadt 112: */
1.2 millert 113: /* ARGSUSED */
1.3 millert 114: int
1.10 cloder 115: twiddle(int f, int n)
1.1 deraadt 116: {
1.15 ! deraadt 117: struct line *dotp;
1.3 millert 118: int doto, cr;
1.1 deraadt 119:
120: dotp = curwp->w_dotp;
121: doto = curwp->w_doto;
1.2 millert 122: if (doto == llength(dotp)) {
123: if (--doto <= 0)
1.11 db 124: return (FALSE);
1.1 deraadt 125: } else {
1.2 millert 126: if (doto == 0)
1.11 db 127: return (FALSE);
1.1 deraadt 128: ++curwp->w_doto;
129: }
130: cr = lgetc(dotp, doto--);
1.2 millert 131: lputc(dotp, doto + 1, lgetc(dotp, doto));
1.1 deraadt 132: lputc(dotp, doto, cr);
133: lchange(WFEDIT);
1.11 db 134: return (TRUE);
1.1 deraadt 135: }
136:
137: /*
1.6 mickey 138: * Open up some blank space. The basic plan is to insert a bunch of
139: * newlines, and then back up over them. Everything is done by the
1.11 db 140: * subcommand processors. They even handle the looping. Normally this
1.3 millert 141: * is bound to "C-O".
1.1 deraadt 142: */
1.2 millert 143: /* ARGSUSED */
1.3 millert 144: int
1.10 cloder 145: openline(int f, int n)
1.1 deraadt 146: {
1.11 db 147: int i, s;
1.1 deraadt 148:
149: if (n < 0)
1.11 db 150: return (FALSE);
1.1 deraadt 151: if (n == 0)
1.11 db 152: return (TRUE);
1.3 millert 153:
154: /* insert newlines */
155: i = n;
1.1 deraadt 156: do {
157: s = lnewline();
1.2 millert 158: } while (s == TRUE && --i);
1.3 millert 159:
160: /* then go back up overtop of them all */
161: if (s == TRUE)
162: s = backchar(f | FFRAND, n);
1.11 db 163: return (s);
1.1 deraadt 164: }
165:
166: /*
1.14 kjell 167: * Insert a newline.
1.1 deraadt 168: */
1.2 millert 169: /* ARGSUSED */
1.3 millert 170: int
1.10 cloder 171: newline(int f, int n)
1.1 deraadt 172: {
1.15 ! deraadt 173: struct line *lp;
1.3 millert 174: int s;
1.1 deraadt 175:
1.2 millert 176: if (n < 0)
1.11 db 177: return (FALSE);
1.3 millert 178:
1.1 deraadt 179: while (n--) {
180: lp = curwp->w_dotp;
1.2 millert 181: if ((s = lnewline()) != TRUE)
1.11 db 182: return (s);
1.1 deraadt 183: }
1.11 db 184: return (TRUE);
1.1 deraadt 185: }
186:
187: /*
1.3 millert 188: * Delete blank lines around dot. What this command does depends if dot is
1.6 mickey 189: * sitting on a blank line. If dot is sitting on a blank line, this command
190: * deletes all the blank lines above and below the current line. If it is
191: * sitting on a non blank line then it deletes all of the blank lines after
192: * the line. Normally this command is bound to "C-X C-O". Any argument is
1.3 millert 193: * ignored.
1.1 deraadt 194: */
1.2 millert 195: /* ARGSUSED */
1.3 millert 196: int
1.10 cloder 197: deblank(int f, int n)
1.1 deraadt 198: {
1.15 ! deraadt 199: struct line *lp1, *lp2;
1.3 millert 200: RSIZE nld;
1.1 deraadt 201:
202: lp1 = curwp->w_dotp;
1.2 millert 203: while (llength(lp1) == 0 && (lp2 = lback(lp1)) != curbp->b_linep)
1.1 deraadt 204: lp1 = lp2;
205: lp2 = lp1;
1.3 millert 206: nld = (RSIZE)0;
1.2 millert 207: while ((lp2 = lforw(lp2)) != curbp->b_linep && llength(lp2) == 0)
1.1 deraadt 208: ++nld;
209: if (nld == 0)
210: return (TRUE);
211: curwp->w_dotp = lforw(lp1);
212: curwp->w_doto = 0;
1.11 db 213: return (ldelete((RSIZE)nld, KNONE));
1.1 deraadt 214: }
215:
216: /*
217: * Delete any whitespace around dot, then insert a space.
218: */
1.3 millert 219: int
1.10 cloder 220: justone(int f, int n)
1.2 millert 221: {
1.5 art 222: (void)delwhite(f, n);
1.11 db 223: return (linsert(1, ' '));
1.1 deraadt 224: }
1.3 millert 225:
1.1 deraadt 226: /*
227: * Delete any whitespace around dot.
228: */
1.2 millert 229: /* ARGSUSED */
1.3 millert 230: int
1.10 cloder 231: delwhite(int f, int n)
1.1 deraadt 232: {
1.3 millert 233: int col, c, s;
1.1 deraadt 234:
235: col = curwp->w_doto;
1.3 millert 236:
1.8 vincent 237: while (col < llength(curwp->w_dotp) &&
238: ((c = lgetc(curwp->w_dotp, col)) == ' ' || c == '\t'))
1.1 deraadt 239: ++col;
240: do {
241: if (curwp->w_doto == 0) {
242: s = FALSE;
243: break;
244: }
1.2 millert 245: if ((s = backchar(FFRAND, 1)) != TRUE)
246: break;
1.1 deraadt 247: } while ((c = lgetc(curwp->w_dotp, curwp->w_doto)) == ' ' || c == '\t');
248:
1.2 millert 249: if (s == TRUE)
1.5 art 250: (void)forwchar(FFRAND, 1);
251: (void)ldelete((RSIZE)(col - curwp->w_doto), KNONE);
1.11 db 252: return (TRUE);
1.1 deraadt 253: }
1.3 millert 254:
1.1 deraadt 255: /*
1.3 millert 256: * Insert a newline, then enough tabs and spaces to duplicate the indentation
1.6 mickey 257: * of the previous line. Assumes tabs are every eight characters. Quite
258: * simple. Figure out the indentation of the current line. Insert a newline
259: * by calling the standard routine. Insert the indentation by inserting the
260: * right number of tabs and spaces. Return TRUE if all ok. Return FALSE if
1.11 db 261: * one of the subcommands failed. Normally bound to "C-J".
1.1 deraadt 262: */
1.2 millert 263: /* ARGSUSED */
1.3 millert 264: int
1.10 cloder 265: indent(int f, int n)
1.1 deraadt 266: {
1.11 db 267: int c, i, nicol;
1.1 deraadt 268:
1.2 millert 269: if (n < 0)
270: return (FALSE);
1.3 millert 271:
1.1 deraadt 272: while (n--) {
273: nicol = 0;
1.2 millert 274: for (i = 0; i < llength(curwp->w_dotp); ++i) {
1.1 deraadt 275: c = lgetc(curwp->w_dotp, i);
1.2 millert 276: if (c != ' ' && c != '\t')
1.1 deraadt 277: break;
278: if (c == '\t')
279: nicol |= 0x07;
280: ++nicol;
281: }
282: if (lnewline() == FALSE || ((
283: #ifdef NOTAB
1.3 millert 284: curbp->b_flag & BFNOTAB) ? linsert(nicol, ' ') == FALSE : (
285: #endif /* NOTAB */
286: ((i = nicol / 8) != 0 && linsert(i, '\t') == FALSE) ||
287: ((i = nicol % 8) != 0 && linsert(i, ' ') == FALSE))))
1.11 db 288: return (FALSE);
1.1 deraadt 289: }
1.11 db 290: return (TRUE);
1.1 deraadt 291: }
292:
293: /*
1.3 millert 294: * Delete forward. This is real easy, because the basic delete routine does
1.6 mickey 295: * all of the work. Watches for negative arguments, and does the right thing.
296: * If any argument is present, it kills rather than deletes, to prevent loss
1.3 millert 297: * of text if typed with a big argument. Normally bound to "C-D".
1.1 deraadt 298: */
1.2 millert 299: /* ARGSUSED */
1.3 millert 300: int
1.10 cloder 301: forwdel(int f, int n)
1.1 deraadt 302: {
303: if (n < 0)
1.11 db 304: return (backdel(f | FFRAND, -n));
1.3 millert 305:
306: /* really a kill */
307: if (f & FFARG) {
1.2 millert 308: if ((lastflag & CFKILL) == 0)
1.1 deraadt 309: kdelete();
310: thisflag |= CFKILL;
311: }
1.3 millert 312:
1.11 db 313: return (ldelete((RSIZE) n, (f & FFARG) ? KFORW : KNONE));
1.1 deraadt 314: }
315:
316: /*
1.6 mickey 317: * Delete backwards. This is quite easy too, because it's all done with
318: * other functions. Just move the cursor back, and delete forwards. Like
1.3 millert 319: * delete forward, this actually does a kill if presented with an argument.
1.1 deraadt 320: */
1.2 millert 321: /* ARGSUSED */
1.3 millert 322: int
1.10 cloder 323: backdel(int f, int n)
1.1 deraadt 324: {
1.3 millert 325: int s;
1.1 deraadt 326:
327: if (n < 0)
1.11 db 328: return (forwdel(f | FFRAND, -n));
1.3 millert 329:
330: /* really a kill */
331: if (f & FFARG) {
1.2 millert 332: if ((lastflag & CFKILL) == 0)
1.1 deraadt 333: kdelete();
334: thisflag |= CFKILL;
335: }
1.2 millert 336: if ((s = backchar(f | FFRAND, n)) == TRUE)
1.3 millert 337: s = ldelete((RSIZE)n, (f & FFARG) ? KFORW : KNONE);
338:
1.11 db 339: return (s);
1.1 deraadt 340: }
341:
342: /*
1.6 mickey 343: * Kill line. If called without an argument, it kills from dot to the end
344: * of the line, unless it is at the end of the line, when it kills the
345: * newline. If called with an argument of 0, it kills from the start of the
346: * line to dot. If called with a positive argument, it kills from dot
347: * forward over that number of newlines. If called with a negative argument
348: * it kills any text before dot on the current line, then it kills back
1.3 millert 349: * abs(arg) lines.
1.1 deraadt 350: */
1.2 millert 351: /* ARGSUSED */
1.3 millert 352: int
1.10 cloder 353: killline(int f, int n)
1.2 millert 354: {
1.15 ! deraadt 355: struct line *nextp;
1.3 millert 356: RSIZE chunk;
357: int i, c;
358:
359: /* clear kill buffer if last wasn't a kill */
360: if ((lastflag & CFKILL) == 0)
361: kdelete();
1.1 deraadt 362: thisflag |= CFKILL;
363: if (!(f & FFARG)) {
364: for (i = curwp->w_doto; i < llength(curwp->w_dotp); ++i)
365: if ((c = lgetc(curwp->w_dotp, i)) != ' ' && c != '\t')
366: break;
367: if (i == llength(curwp->w_dotp))
1.2 millert 368: chunk = llength(curwp->w_dotp) - curwp->w_doto + 1;
1.1 deraadt 369: else {
1.2 millert 370: chunk = llength(curwp->w_dotp) - curwp->w_doto;
1.1 deraadt 371: if (chunk == 0)
372: chunk = 1;
373: }
374: } else if (n > 0) {
1.2 millert 375: chunk = llength(curwp->w_dotp) - curwp->w_doto + 1;
1.1 deraadt 376: nextp = lforw(curwp->w_dotp);
377: i = n;
378: while (--i) {
379: if (nextp == curbp->b_linep)
380: break;
1.2 millert 381: chunk += llength(nextp) + 1;
1.1 deraadt 382: nextp = lforw(nextp);
383: }
1.3 millert 384: } else {
385: /* n <= 0 */
1.1 deraadt 386: chunk = curwp->w_doto;
387: curwp->w_doto = 0;
388: i = n;
389: while (i++) {
390: if (lback(curwp->w_dotp) == curbp->b_linep)
391: break;
392: curwp->w_dotp = lback(curwp->w_dotp);
393: curwp->w_flag |= WFMOVE;
1.2 millert 394: chunk += llength(curwp->w_dotp) + 1;
1.1 deraadt 395: }
396: }
397: /*
1.3 millert 398: * KFORW here is a bug. Should be KBACK/KFORW, but we need to
1.1 deraadt 399: * rewrite the ldelete code (later)?
400: */
1.2 millert 401: return (ldelete(chunk, KFORW));
1.1 deraadt 402: }
403:
404: /*
1.6 mickey 405: * Yank text back from the kill buffer. This is really easy. All of the work
406: * is done by the standard insert routines. All you do is run the loop, and
1.3 millert 407: * check for errors. The blank lines are inserted with a call to "newline"
1.6 mickey 408: * instead of a call to "lnewline" so that the magic stuff that happens when
409: * you type a carriage return also happens when a carriage return is yanked
410: * back from the kill buffer. An attempt has been made to fix the cosmetic
411: * bug associated with a yank when dot is on the top line of the window
1.3 millert 412: * (nothing moves, because all of the new text landed off screen).
1.1 deraadt 413: */
1.2 millert 414: /* ARGSUSED */
1.3 millert 415: int
1.10 cloder 416: yank(int f, int n)
1.1 deraadt 417: {
1.15 ! deraadt 418: struct line *lp;
1.3 millert 419: int c, i, nline;
1.1 deraadt 420:
1.2 millert 421: if (n < 0)
1.11 db 422: return (FALSE);
1.3 millert 423:
424: /* newline counting */
425: nline = 0;
426:
1.13 kjell 427: undo_add_boundary();
428: undo_no_boundary(TRUE);
1.1 deraadt 429: while (n--) {
1.3 millert 430: /* mark around last yank */
431: isetmark();
1.1 deraadt 432: i = 0;
1.2 millert 433: while ((c = kremove(i)) >= 0) {
1.1 deraadt 434: if (c == '\n') {
435: if (newline(FFRAND, 1) == FALSE)
1.11 db 436: return (FALSE);
1.1 deraadt 437: ++nline;
438: } else {
439: if (linsert(1, c) == FALSE)
1.11 db 440: return (FALSE);
1.1 deraadt 441: }
442: ++i;
443: }
444: }
1.3 millert 445: /* cosmetic adjustment */
446: lp = curwp->w_linep;
447:
448: /* if offscreen insert */
449: if (curwp->w_dotp == lp) {
1.2 millert 450: while (nline-- && lback(lp) != curbp->b_linep)
1.1 deraadt 451: lp = lback(lp);
1.3 millert 452: /* adjust framing */
453: curwp->w_linep = lp;
1.1 deraadt 454: curwp->w_flag |= WFHARD;
455: }
1.13 kjell 456: undo_no_boundary(FALSE);
457: undo_add_boundary();
1.11 db 458: return (TRUE);
1.1 deraadt 459: }
460:
461: #ifdef NOTAB
1.2 millert 462: /* ARGSUSED */
1.3 millert 463: int
1.10 cloder 464: space_to_tabstop(int f, int n)
1.1 deraadt 465: {
1.2 millert 466: if (n < 0)
1.11 db 467: return (FALSE);
1.2 millert 468: if (n == 0)
1.11 db 469: return (TRUE);
470: return (linsert((n << 3) - (curwp->w_doto & 7), ' '));
1.1 deraadt 471: }
1.3 millert 472: #endif /* NOTAB */