Annotation of src/usr.bin/mg/random.c, Revision 1.16
1.16 ! kjell 1: /* $OpenBSD: random.c,v 1.15 2005/11/18 20:56:53 deraadt 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.3 millert 173: int s;
1.1 deraadt 174:
1.2 millert 175: if (n < 0)
1.11 db 176: return (FALSE);
1.3 millert 177:
1.1 deraadt 178: while (n--) {
1.2 millert 179: if ((s = lnewline()) != TRUE)
1.11 db 180: return (s);
1.1 deraadt 181: }
1.11 db 182: return (TRUE);
1.1 deraadt 183: }
184:
185: /*
1.3 millert 186: * Delete blank lines around dot. What this command does depends if dot is
1.6 mickey 187: * sitting on a blank line. If dot is sitting on a blank line, this command
188: * deletes all the blank lines above and below the current line. If it is
189: * sitting on a non blank line then it deletes all of the blank lines after
190: * the line. Normally this command is bound to "C-X C-O". Any argument is
1.3 millert 191: * ignored.
1.1 deraadt 192: */
1.2 millert 193: /* ARGSUSED */
1.3 millert 194: int
1.10 cloder 195: deblank(int f, int n)
1.1 deraadt 196: {
1.15 deraadt 197: struct line *lp1, *lp2;
1.3 millert 198: RSIZE nld;
1.1 deraadt 199:
200: lp1 = curwp->w_dotp;
1.2 millert 201: while (llength(lp1) == 0 && (lp2 = lback(lp1)) != curbp->b_linep)
1.1 deraadt 202: lp1 = lp2;
203: lp2 = lp1;
1.3 millert 204: nld = (RSIZE)0;
1.2 millert 205: while ((lp2 = lforw(lp2)) != curbp->b_linep && llength(lp2) == 0)
1.1 deraadt 206: ++nld;
207: if (nld == 0)
208: return (TRUE);
209: curwp->w_dotp = lforw(lp1);
210: curwp->w_doto = 0;
1.11 db 211: return (ldelete((RSIZE)nld, KNONE));
1.1 deraadt 212: }
213:
214: /*
215: * Delete any whitespace around dot, then insert a space.
216: */
1.3 millert 217: int
1.10 cloder 218: justone(int f, int n)
1.2 millert 219: {
1.5 art 220: (void)delwhite(f, n);
1.11 db 221: return (linsert(1, ' '));
1.1 deraadt 222: }
1.3 millert 223:
1.1 deraadt 224: /*
225: * Delete any whitespace around dot.
226: */
1.2 millert 227: /* ARGSUSED */
1.3 millert 228: int
1.10 cloder 229: delwhite(int f, int n)
1.1 deraadt 230: {
1.3 millert 231: int col, c, s;
1.1 deraadt 232:
233: col = curwp->w_doto;
1.3 millert 234:
1.8 vincent 235: while (col < llength(curwp->w_dotp) &&
236: ((c = lgetc(curwp->w_dotp, col)) == ' ' || c == '\t'))
1.1 deraadt 237: ++col;
238: do {
239: if (curwp->w_doto == 0) {
240: s = FALSE;
241: break;
242: }
1.2 millert 243: if ((s = backchar(FFRAND, 1)) != TRUE)
244: break;
1.1 deraadt 245: } while ((c = lgetc(curwp->w_dotp, curwp->w_doto)) == ' ' || c == '\t');
246:
1.2 millert 247: if (s == TRUE)
1.5 art 248: (void)forwchar(FFRAND, 1);
249: (void)ldelete((RSIZE)(col - curwp->w_doto), KNONE);
1.11 db 250: return (TRUE);
1.1 deraadt 251: }
1.3 millert 252:
1.1 deraadt 253: /*
1.3 millert 254: * Insert a newline, then enough tabs and spaces to duplicate the indentation
1.6 mickey 255: * of the previous line. Assumes tabs are every eight characters. Quite
256: * simple. Figure out the indentation of the current line. Insert a newline
257: * by calling the standard routine. Insert the indentation by inserting the
258: * right number of tabs and spaces. Return TRUE if all ok. Return FALSE if
1.11 db 259: * one of the subcommands failed. Normally bound to "C-J".
1.1 deraadt 260: */
1.2 millert 261: /* ARGSUSED */
1.3 millert 262: int
1.10 cloder 263: indent(int f, int n)
1.1 deraadt 264: {
1.11 db 265: int c, i, nicol;
1.1 deraadt 266:
1.2 millert 267: if (n < 0)
268: return (FALSE);
1.3 millert 269:
1.1 deraadt 270: while (n--) {
271: nicol = 0;
1.2 millert 272: for (i = 0; i < llength(curwp->w_dotp); ++i) {
1.1 deraadt 273: c = lgetc(curwp->w_dotp, i);
1.2 millert 274: if (c != ' ' && c != '\t')
1.1 deraadt 275: break;
276: if (c == '\t')
277: nicol |= 0x07;
278: ++nicol;
279: }
280: if (lnewline() == FALSE || ((
281: #ifdef NOTAB
1.3 millert 282: curbp->b_flag & BFNOTAB) ? linsert(nicol, ' ') == FALSE : (
283: #endif /* NOTAB */
284: ((i = nicol / 8) != 0 && linsert(i, '\t') == FALSE) ||
285: ((i = nicol % 8) != 0 && linsert(i, ' ') == FALSE))))
1.11 db 286: return (FALSE);
1.1 deraadt 287: }
1.11 db 288: return (TRUE);
1.1 deraadt 289: }
290:
291: /*
1.3 millert 292: * Delete forward. This is real easy, because the basic delete routine does
1.6 mickey 293: * all of the work. Watches for negative arguments, and does the right thing.
294: * If any argument is present, it kills rather than deletes, to prevent loss
1.3 millert 295: * of text if typed with a big argument. Normally bound to "C-D".
1.1 deraadt 296: */
1.2 millert 297: /* ARGSUSED */
1.3 millert 298: int
1.10 cloder 299: forwdel(int f, int n)
1.1 deraadt 300: {
301: if (n < 0)
1.11 db 302: return (backdel(f | FFRAND, -n));
1.3 millert 303:
304: /* really a kill */
305: if (f & FFARG) {
1.2 millert 306: if ((lastflag & CFKILL) == 0)
1.1 deraadt 307: kdelete();
308: thisflag |= CFKILL;
309: }
1.3 millert 310:
1.11 db 311: return (ldelete((RSIZE) n, (f & FFARG) ? KFORW : KNONE));
1.1 deraadt 312: }
313:
314: /*
1.6 mickey 315: * Delete backwards. This is quite easy too, because it's all done with
316: * other functions. Just move the cursor back, and delete forwards. Like
1.3 millert 317: * delete forward, this actually does a kill if presented with an argument.
1.1 deraadt 318: */
1.2 millert 319: /* ARGSUSED */
1.3 millert 320: int
1.10 cloder 321: backdel(int f, int n)
1.1 deraadt 322: {
1.3 millert 323: int s;
1.1 deraadt 324:
325: if (n < 0)
1.11 db 326: return (forwdel(f | FFRAND, -n));
1.3 millert 327:
328: /* really a kill */
329: if (f & FFARG) {
1.2 millert 330: if ((lastflag & CFKILL) == 0)
1.1 deraadt 331: kdelete();
332: thisflag |= CFKILL;
333: }
1.2 millert 334: if ((s = backchar(f | FFRAND, n)) == TRUE)
1.3 millert 335: s = ldelete((RSIZE)n, (f & FFARG) ? KFORW : KNONE);
336:
1.11 db 337: return (s);
1.1 deraadt 338: }
339:
340: /*
1.6 mickey 341: * Kill line. If called without an argument, it kills from dot to the end
342: * of the line, unless it is at the end of the line, when it kills the
343: * newline. If called with an argument of 0, it kills from the start of the
344: * line to dot. If called with a positive argument, it kills from dot
345: * forward over that number of newlines. If called with a negative argument
346: * it kills any text before dot on the current line, then it kills back
1.3 millert 347: * abs(arg) lines.
1.1 deraadt 348: */
1.2 millert 349: /* ARGSUSED */
1.3 millert 350: int
1.10 cloder 351: killline(int f, int n)
1.2 millert 352: {
1.15 deraadt 353: struct line *nextp;
1.3 millert 354: RSIZE chunk;
355: int i, c;
356:
357: /* clear kill buffer if last wasn't a kill */
358: if ((lastflag & CFKILL) == 0)
359: kdelete();
1.1 deraadt 360: thisflag |= CFKILL;
361: if (!(f & FFARG)) {
362: for (i = curwp->w_doto; i < llength(curwp->w_dotp); ++i)
363: if ((c = lgetc(curwp->w_dotp, i)) != ' ' && c != '\t')
364: break;
365: if (i == llength(curwp->w_dotp))
1.2 millert 366: chunk = llength(curwp->w_dotp) - curwp->w_doto + 1;
1.1 deraadt 367: else {
1.2 millert 368: chunk = llength(curwp->w_dotp) - curwp->w_doto;
1.1 deraadt 369: if (chunk == 0)
370: chunk = 1;
371: }
372: } else if (n > 0) {
1.2 millert 373: chunk = llength(curwp->w_dotp) - curwp->w_doto + 1;
1.1 deraadt 374: nextp = lforw(curwp->w_dotp);
375: i = n;
376: while (--i) {
377: if (nextp == curbp->b_linep)
378: break;
1.2 millert 379: chunk += llength(nextp) + 1;
1.1 deraadt 380: nextp = lforw(nextp);
381: }
1.3 millert 382: } else {
383: /* n <= 0 */
1.1 deraadt 384: chunk = curwp->w_doto;
385: curwp->w_doto = 0;
386: i = n;
387: while (i++) {
388: if (lback(curwp->w_dotp) == curbp->b_linep)
389: break;
390: curwp->w_dotp = lback(curwp->w_dotp);
391: curwp->w_flag |= WFMOVE;
1.2 millert 392: chunk += llength(curwp->w_dotp) + 1;
1.1 deraadt 393: }
394: }
395: /*
1.3 millert 396: * KFORW here is a bug. Should be KBACK/KFORW, but we need to
1.1 deraadt 397: * rewrite the ldelete code (later)?
398: */
1.2 millert 399: return (ldelete(chunk, KFORW));
1.1 deraadt 400: }
401:
402: /*
1.6 mickey 403: * Yank text back from the kill buffer. This is really easy. All of the work
404: * is done by the standard insert routines. All you do is run the loop, and
1.3 millert 405: * check for errors. The blank lines are inserted with a call to "newline"
1.6 mickey 406: * instead of a call to "lnewline" so that the magic stuff that happens when
407: * you type a carriage return also happens when a carriage return is yanked
408: * back from the kill buffer. An attempt has been made to fix the cosmetic
409: * bug associated with a yank when dot is on the top line of the window
1.3 millert 410: * (nothing moves, because all of the new text landed off screen).
1.1 deraadt 411: */
1.2 millert 412: /* ARGSUSED */
1.3 millert 413: int
1.10 cloder 414: yank(int f, int n)
1.1 deraadt 415: {
1.15 deraadt 416: struct line *lp;
1.3 millert 417: int c, i, nline;
1.1 deraadt 418:
1.2 millert 419: if (n < 0)
1.11 db 420: return (FALSE);
1.3 millert 421:
422: /* newline counting */
423: nline = 0;
424:
1.13 kjell 425: undo_add_boundary();
426: undo_no_boundary(TRUE);
1.1 deraadt 427: while (n--) {
1.3 millert 428: /* mark around last yank */
429: isetmark();
1.1 deraadt 430: i = 0;
1.2 millert 431: while ((c = kremove(i)) >= 0) {
1.1 deraadt 432: if (c == '\n') {
433: if (newline(FFRAND, 1) == FALSE)
1.11 db 434: return (FALSE);
1.1 deraadt 435: ++nline;
436: } else {
437: if (linsert(1, c) == FALSE)
1.11 db 438: return (FALSE);
1.1 deraadt 439: }
440: ++i;
441: }
442: }
1.3 millert 443: /* cosmetic adjustment */
444: lp = curwp->w_linep;
445:
446: /* if offscreen insert */
447: if (curwp->w_dotp == lp) {
1.2 millert 448: while (nline-- && lback(lp) != curbp->b_linep)
1.1 deraadt 449: lp = lback(lp);
1.3 millert 450: /* adjust framing */
451: curwp->w_linep = lp;
1.1 deraadt 452: curwp->w_flag |= WFHARD;
453: }
1.13 kjell 454: undo_no_boundary(FALSE);
455: undo_add_boundary();
1.11 db 456: return (TRUE);
1.1 deraadt 457: }
458:
459: #ifdef NOTAB
1.2 millert 460: /* ARGSUSED */
1.3 millert 461: int
1.10 cloder 462: space_to_tabstop(int f, int n)
1.1 deraadt 463: {
1.2 millert 464: if (n < 0)
1.11 db 465: return (FALSE);
1.2 millert 466: if (n == 0)
1.11 db 467: return (TRUE);
468: return (linsert((n << 3) - (curwp->w_doto & 7), ' '));
1.1 deraadt 469: }
1.3 millert 470: #endif /* NOTAB */