Annotation of src/usr.bin/mg/basic.c, Revision 1.54
1.54 ! op 1: /* $OpenBSD: basic.c,v 1.53 2023/04/17 09:49:04 op Exp $ */
1.19 kjell 2:
3: /* This file is in the public domain */
1.4 niklas 4:
1.1 deraadt 5: /*
6: * Basic cursor motion commands.
7: *
8: * The routines in this file are the basic
9: * command functions for moving the cursor around on
10: * the screen, setting mark, and swapping dot with
11: * mark. Only moves between lines, which might make the
12: * current buffer framing bad, are hard.
13: */
14:
1.44 bcallah 15: #include <sys/queue.h>
1.14 vincent 16: #include <ctype.h>
1.43 guenther 17: #include <limits.h>
1.44 bcallah 18: #include <signal.h>
19: #include <stdio.h>
20: #include <stdlib.h>
21:
22: #include "def.h"
1.1 deraadt 23:
1.48 lum 24: #define percint(n1, n2) ((n1 * (int) n2) * 0.1)
25:
1.1 deraadt 26: /*
27: * Go to beginning of line.
28: */
1.3 millert 29: int
1.12 vincent 30: gotobol(int f, int n)
1.1 deraadt 31: {
1.47 lum 32: if (n == 0)
33: return (TRUE);
34:
1.3 millert 35: curwp->w_doto = 0;
1.1 deraadt 36: return (TRUE);
37: }
38:
39: /*
40: * Move cursor backwards. Do the
41: * right thing if the count is less than
42: * 0. Error if you try to move back from
43: * the beginning of the buffer.
44: */
1.3 millert 45: int
1.12 vincent 46: backchar(int f, int n)
1.1 deraadt 47: {
1.21 deraadt 48: struct line *lp;
1.1 deraadt 49:
1.3 millert 50: if (n < 0)
1.18 db 51: return (forwchar(f, -n));
1.1 deraadt 52: while (n--) {
53: if (curwp->w_doto == 0) {
1.25 kjell 54: if ((lp = lback(curwp->w_dotp)) == curbp->b_headp) {
1.50 lum 55: if (!(f & FFRAND))
56: (void)dobeep_msg("Beginning "
57: "of buffer");
1.1 deraadt 58: return (FALSE);
59: }
1.3 millert 60: curwp->w_dotp = lp;
61: curwp->w_doto = llength(lp);
1.30 kjell 62: curwp->w_rflag |= WFMOVE;
1.23 kjell 63: curwp->w_dotline--;
1.1 deraadt 64: } else
65: curwp->w_doto--;
66: }
1.18 db 67: return (TRUE);
1.1 deraadt 68: }
69:
70: /*
71: * Go to end of line.
72: */
1.3 millert 73: int
1.12 vincent 74: gotoeol(int f, int n)
1.1 deraadt 75: {
1.47 lum 76: if (n == 0)
77: return (TRUE);
78:
1.3 millert 79: curwp->w_doto = llength(curwp->w_dotp);
1.1 deraadt 80: return (TRUE);
81: }
82:
83: /*
84: * Move cursor forwards. Do the
85: * right thing if the count is less than
86: * 0. Error if you try to move forward
87: * from the end of the buffer.
88: */
1.3 millert 89: int
1.12 vincent 90: forwchar(int f, int n)
1.1 deraadt 91: {
1.3 millert 92: if (n < 0)
1.18 db 93: return (backchar(f, -n));
1.1 deraadt 94: while (n--) {
95: if (curwp->w_doto == llength(curwp->w_dotp)) {
1.3 millert 96: curwp->w_dotp = lforw(curwp->w_dotp);
1.25 kjell 97: if (curwp->w_dotp == curbp->b_headp) {
1.1 deraadt 98: curwp->w_dotp = lback(curwp->w_dotp);
1.50 lum 99: if (!(f & FFRAND))
100: (void)dobeep_msg("End of buffer");
1.18 db 101: return (FALSE);
1.1 deraadt 102: }
1.3 millert 103: curwp->w_doto = 0;
1.23 kjell 104: curwp->w_dotline++;
1.30 kjell 105: curwp->w_rflag |= WFMOVE;
1.1 deraadt 106: } else
107: curwp->w_doto++;
108: }
1.18 db 109: return (TRUE);
1.1 deraadt 110: }
111:
112: /*
1.48 lum 113: * Go to the beginning of the buffer. Setting WFFULL is conservative,
114: * but almost always the case. A universal argument of higher than 9
115: * puts the cursor back to the end of buffer.
1.1 deraadt 116: */
1.3 millert 117: int
1.12 vincent 118: gotobob(int f, int n)
1.1 deraadt 119: {
1.49 lum 120: if (!curwp->w_markp)
121: (void) setmark(f, n);
1.26 kjell 122: curwp->w_dotp = bfirstlp(curbp);
1.3 millert 123: curwp->w_doto = 0;
1.30 kjell 124: curwp->w_rflag |= WFFULL;
1.23 kjell 125: curwp->w_dotline = 1;
1.48 lum 126: if (f & FFOTHARG && n > 0) {
127: if (n > 9)
128: gotoeob(0, 0);
129: else
130: forwline(f, percint(curwp->w_bufp->b_lines, n) - 1);
131: }
1.18 db 132: return (TRUE);
1.1 deraadt 133: }
134:
135: /*
1.37 lum 136: * Go to the end of the buffer. Leave dot 3 lines from the bottom of the
137: * window if buffer length is longer than window length; same as emacs.
1.48 lum 138: * Setting WFFULL is conservative, but almost always the case. A universal
139: * argument of higher than 9 puts the cursor back to the start of buffer.
1.1 deraadt 140: */
1.3 millert 141: int
1.12 vincent 142: gotoeob(int f, int n)
1.1 deraadt 143: {
1.48 lum 144: int ln;
1.37 lum 145: struct line *lp;
1.46 jasper 146:
1.49 lum 147: if (!curwp->w_markp)
148: (void) setmark(f, n);
1.26 kjell 149: curwp->w_dotp = blastlp(curbp);
1.3 millert 150: curwp->w_doto = llength(curwp->w_dotp);
1.23 kjell 151: curwp->w_dotline = curwp->w_bufp->b_lines;
1.37 lum 152:
153: lp = curwp->w_dotp;
1.48 lum 154: ln = curwp->w_ntrows - 3;
1.37 lum 155:
1.48 lum 156: if (ln < curwp->w_bufp->b_lines && ln >= 3) {
157: while (ln--)
1.37 lum 158: curwp->w_dotp = lback(curwp->w_dotp);
159:
160: curwp->w_linep = curwp->w_dotp;
161: curwp->w_dotp = lp;
162: }
1.48 lum 163: if (f & FFOTHARG && n > 0) {
164: if (n > 9)
165: gotobob(0, 0);
166: else
167: backline(f, percint(curwp->w_bufp->b_lines, n));
168: }
169:
1.30 kjell 170: curwp->w_rflag |= WFFULL;
1.18 db 171: return (TRUE);
1.1 deraadt 172: }
173:
174: /*
175: * Move forward by full lines.
176: * If the number of lines to move is less
177: * than zero, call the backward line function to
178: * actually do it. The last command controls how
179: * the goal column is set.
180: */
1.3 millert 181: int
1.12 vincent 182: forwline(int f, int n)
1.1 deraadt 183: {
1.21 deraadt 184: struct line *dlp;
1.1 deraadt 185:
186: if (n < 0)
1.18 db 187: return (backline(f | FFRAND, -n));
1.41 florian 188: if ((dlp = curwp->w_dotp) == curbp->b_headp) {
1.50 lum 189: if (!(f & FFRAND))
190: (void)dobeep_msg("End of buffer");
1.23 kjell 191: return(TRUE);
1.41 florian 192: }
1.14 vincent 193: if ((lastflag & CFCPCN) == 0) /* Fix goal. */
1.1 deraadt 194: setgoal();
195: thisflag |= CFCPCN;
1.3 millert 196: if (n == 0)
1.18 db 197: return (TRUE);
1.23 kjell 198: while (n--) {
1.1 deraadt 199: dlp = lforw(dlp);
1.25 kjell 200: if (dlp == curbp->b_headp) {
1.23 kjell 201: curwp->w_dotp = lback(dlp);
202: curwp->w_doto = llength(curwp->w_dotp);
1.30 kjell 203: curwp->w_rflag |= WFMOVE;
1.50 lum 204: if (!(f & FFRAND))
205: (void)dobeep_msg("End of buffer");
1.23 kjell 206: return (TRUE);
1.1 deraadt 207: }
1.23 kjell 208: curwp->w_dotline++;
1.1 deraadt 209: }
1.30 kjell 210: curwp->w_rflag |= WFMOVE;
1.23 kjell 211: curwp->w_dotp = dlp;
212: curwp->w_doto = getgoal(dlp);
213:
1.18 db 214: return (TRUE);
1.1 deraadt 215: }
216:
217: /*
218: * This function is like "forwline", but
219: * goes backwards. The scheme is exactly the same.
220: * Check for arguments that are less than zero and
221: * call your alternate. Figure out the new line and
222: * call "movedot" to perform the motion.
223: */
1.3 millert 224: int
1.12 vincent 225: backline(int f, int n)
1.1 deraadt 226: {
1.21 deraadt 227: struct line *dlp;
1.1 deraadt 228:
1.3 millert 229: if (n < 0)
1.18 db 230: return (forwline(f | FFRAND, -n));
1.14 vincent 231: if ((lastflag & CFCPCN) == 0) /* Fix goal. */
1.1 deraadt 232: setgoal();
233: thisflag |= CFCPCN;
234: dlp = curwp->w_dotp;
1.41 florian 235: if (lback(dlp) == curbp->b_headp) {
1.50 lum 236: if (!(f & FFRAND))
237: (void)dobeep_msg("Beginning of buffer");
1.41 florian 238: return(TRUE);
239: }
1.25 kjell 240: while (n-- && lback(dlp) != curbp->b_headp) {
1.1 deraadt 241: dlp = lback(dlp);
1.23 kjell 242: curwp->w_dotline--;
1.41 florian 243: }
1.50 lum 244: if (n > 0 && !(f & FFRAND))
245: (void)dobeep_msg("Beginning of buffer");
1.3 millert 246: curwp->w_dotp = dlp;
247: curwp->w_doto = getgoal(dlp);
1.30 kjell 248: curwp->w_rflag |= WFMOVE;
1.18 db 249: return (TRUE);
1.1 deraadt 250: }
251:
252: /*
1.3 millert 253: * Set the current goal column, which is saved in the external variable
254: * "curgoal", to the current cursor column. The column is never off
255: * the edge of the screen; it's more like display then show position.
1.1 deraadt 256: */
1.6 art 257: void
1.12 vincent 258: setgoal(void)
1.3 millert 259: {
1.39 florian 260: curgoal = getcolpos(curwp); /* Get the position. */
1.3 millert 261: /* we can now display past end of display, don't chop! */
1.1 deraadt 262: }
263:
264: /*
265: * This routine looks at a line (pointed
266: * to by the LINE pointer "dlp") and the current
267: * vertical motion goal column (set by the "setgoal"
268: * routine above) and returns the best offset to use
269: * when a vertical motion is made into the line.
270: */
1.3 millert 271: int
1.21 deraadt 272: getgoal(struct line *dlp)
1.3 millert 273: {
1.14 vincent 274: int c, i, col = 0;
1.24 kjell 275: char tmp[5];
276:
1.14 vincent 277: for (i = 0; i < llength(dlp); i++) {
278: c = lgetc(dlp, i);
1.53 op 279: if (c == '\t') {
1.54 ! op 280: col = ntabstop(col, curbp->b_tabw);
1.14 vincent 281: } else if (ISCTRL(c) != FALSE) {
282: col += 2;
283: } else if (isprint(c))
284: col++;
285: else {
1.24 kjell 286: col += snprintf(tmp, sizeof(tmp), "\\%o", c);
1.14 vincent 287: }
288: if (col > curgoal)
1.1 deraadt 289: break;
290: }
1.14 vincent 291: return (i);
1.1 deraadt 292: }
293:
294: /*
295: * Scroll forward by a specified number
296: * of lines, or by a full page if no argument.
297: * The "2" is the window overlap (this is the default
298: * value from ITS EMACS). Because the top line in
299: * the window is zapped, we have to do a hard
300: * update and get it back.
301: */
1.3 millert 302: int
1.12 vincent 303: forwpage(int f, int n)
1.1 deraadt 304: {
1.21 deraadt 305: struct line *lp;
1.1 deraadt 306:
307: if (!(f & FFARG)) {
1.3 millert 308: n = curwp->w_ntrows - 2; /* Default scroll. */
309: if (n <= 0) /* Forget the overlap */
310: n = 1; /* if tiny window. */
1.1 deraadt 311: } else if (n < 0)
1.18 db 312: return (backpage(f | FFRAND, -n));
1.31 lum 313:
1.1 deraadt 314: lp = curwp->w_linep;
1.31 lum 315: while (n--)
1.34 lum 316: if ((lp = lforw(lp)) == curbp->b_headp) {
1.50 lum 317: (void)dobeep_msg("End of buffer");
1.31 lum 318: return(TRUE);
1.34 lum 319: }
1.1 deraadt 320: curwp->w_linep = lp;
1.30 kjell 321: curwp->w_rflag |= WFFULL;
1.31 lum 322:
1.1 deraadt 323: /* if in current window, don't move dot */
1.32 lum 324: for (n = curwp->w_ntrows; n-- && lp != curbp->b_headp; lp = lforw(lp))
1.3 millert 325: if (lp == curwp->w_dotp)
1.18 db 326: return (TRUE);
1.31 lum 327:
1.27 kjell 328: /* Advance the dot the slow way, for line nos */
329: while (curwp->w_dotp != curwp->w_linep) {
330: curwp->w_dotp = lforw(curwp->w_dotp);
331: curwp->w_dotline++;
332: }
1.3 millert 333: curwp->w_doto = 0;
1.18 db 334: return (TRUE);
1.1 deraadt 335: }
336:
337: /*
338: * This command is like "forwpage",
339: * but it goes backwards. The "2", like above,
340: * is the overlap between the two windows. The
341: * value is from the ITS EMACS manual. The
342: * hard update is done because the top line in
343: * the window is zapped.
344: */
1.3 millert 345: int
1.12 vincent 346: backpage(int f, int n)
1.1 deraadt 347: {
1.33 lum 348: struct line *lp, *lp2;
1.1 deraadt 349:
350: if (!(f & FFARG)) {
1.3 millert 351: n = curwp->w_ntrows - 2; /* Default scroll. */
352: if (n <= 0) /* Don't blow up if the */
1.35 lum 353: return (backline(f, 1));/* window is tiny. */
1.1 deraadt 354: } else if (n < 0)
1.18 db 355: return (forwpage(f | FFRAND, -n));
1.33 lum 356:
357: lp = lp2 = curwp->w_linep;
358:
1.25 kjell 359: while (n-- && lback(lp) != curbp->b_headp) {
1.1 deraadt 360: lp = lback(lp);
1.34 lum 361: }
1.50 lum 362: if (lp == curwp->w_linep)
363: (void)dobeep_msg("Beginning of buffer");
364:
1.1 deraadt 365: curwp->w_linep = lp;
1.30 kjell 366: curwp->w_rflag |= WFFULL;
1.33 lum 367:
1.1 deraadt 368: /* if in current window, don't move dot */
1.25 kjell 369: for (n = curwp->w_ntrows; n-- && lp != curbp->b_headp; lp = lforw(lp))
1.3 millert 370: if (lp == curwp->w_dotp)
1.18 db 371: return (TRUE);
1.33 lum 372:
373: lp2 = lforw(lp2);
374:
1.27 kjell 375: /* Move the dot the slow way, for line nos */
1.33 lum 376: while (curwp->w_dotp != lp2) {
1.36 lum 377: if (curwp->w_dotline <= curwp->w_ntrows)
1.45 florian 378: goto out;
1.27 kjell 379: curwp->w_dotp = lback(curwp->w_dotp);
380: curwp->w_dotline--;
381: }
1.45 florian 382: out:
1.1 deraadt 383: curwp->w_doto = 0;
1.18 db 384: return (TRUE);
1.1 deraadt 385: }
386:
1.3 millert 387: /*
388: * These functions are provided for compatibility with Gosling's Emacs. They
389: * are used to scroll the display up (or down) one line at a time.
1.1 deraadt 390: */
1.7 deraadt 391: int
1.12 vincent 392: forw1page(int f, int n)
1.1 deraadt 393: {
1.3 millert 394: if (!(f & FFARG)) {
395: n = 1;
1.1 deraadt 396: f = FFUNIV;
397: }
1.3 millert 398: forwpage(f | FFRAND, n);
1.18 db 399: return (TRUE);
1.1 deraadt 400: }
401:
1.7 deraadt 402: int
1.12 vincent 403: back1page(int f, int n)
1.1 deraadt 404: {
405: if (!(f & FFARG)) {
1.3 millert 406: n = 1;
1.1 deraadt 407: f = FFUNIV;
408: }
1.3 millert 409: backpage(f | FFRAND, n);
1.18 db 410: return (TRUE);
1.1 deraadt 411: }
412:
413: /*
414: * Page the other window. Check to make sure it exists, then
415: * nextwind, forwpage and restore window pointers.
416: */
1.3 millert 417: int
1.12 vincent 418: pagenext(int f, int n)
1.1 deraadt 419: {
1.21 deraadt 420: struct mgwin *wp;
1.1 deraadt 421:
1.50 lum 422: if (wheadp->w_wndp == NULL)
423: return(dobeep_msg("No other window"));
424:
1.1 deraadt 425: wp = curwp;
1.6 art 426: (void) nextwind(f, n);
427: (void) forwpage(f, n);
1.1 deraadt 428: curwp = wp;
429: curbp = wp->w_bufp;
1.18 db 430: return (TRUE);
1.1 deraadt 431: }
432:
433: /*
434: * Internal set mark routine, used by other functions (daveb).
435: */
1.6 art 436: void
1.12 vincent 437: isetmark(void)
1.1 deraadt 438: {
439: curwp->w_markp = curwp->w_dotp;
440: curwp->w_marko = curwp->w_doto;
1.23 kjell 441: curwp->w_markline = curwp->w_dotline;
1.1 deraadt 442: }
443:
444: /*
445: * Set the mark in the current window
446: * to the value of dot. A message is written to
447: * the echo line. (ewprintf knows about macros)
448: */
1.3 millert 449: int
1.12 vincent 450: setmark(int f, int n)
1.1 deraadt 451: {
452: isetmark();
453: ewprintf("Mark set");
1.29 kjell 454: return (TRUE);
455: }
456:
457: /* Clear the mark, if set. */
458: int
459: clearmark(int f, int n)
460: {
461: if (!curwp->w_markp)
462: return (FALSE);
463:
464: curwp->w_markp = NULL;
465: curwp->w_marko = 0;
466: curwp->w_markline = 0;
467:
1.18 db 468: return (TRUE);
1.1 deraadt 469: }
470:
471: /*
472: * Swap the values of "dot" and "mark" in
473: * the current window. This is pretty easy, because
474: * all of the hard work gets done by the standard routine
475: * that moves the mark about. The only possible
476: * error is "no mark".
477: */
1.3 millert 478: int
1.12 vincent 479: swapmark(int f, int n)
1.1 deraadt 480: {
1.21 deraadt 481: struct line *odotp;
1.23 kjell 482: int odoto, odotline;
1.1 deraadt 483:
1.50 lum 484: if (curwp->w_markp == NULL)
485: return(dobeep_msg("No mark in this window"));
486:
1.1 deraadt 487: odotp = curwp->w_dotp;
488: odoto = curwp->w_doto;
1.23 kjell 489: odotline = curwp->w_dotline;
1.3 millert 490: curwp->w_dotp = curwp->w_markp;
491: curwp->w_doto = curwp->w_marko;
1.23 kjell 492: curwp->w_dotline = curwp->w_markline;
1.1 deraadt 493: curwp->w_markp = odotp;
494: curwp->w_marko = odoto;
1.23 kjell 495: curwp->w_markline = odotline;
1.30 kjell 496: curwp->w_rflag |= WFMOVE;
1.18 db 497: return (TRUE);
1.1 deraadt 498: }
499:
500: /*
501: * Go to a specific line, mostly for
502: * looking up errors in C programs, which give the
503: * error a line number. If an argument is present, then
504: * it is the line number, else prompt for a line number
505: * to use.
506: */
1.3 millert 507: int
1.12 vincent 508: gotoline(int f, int n)
1.1 deraadt 509: {
1.23 kjell 510: char buf[32], *bufp;
511: const char *err;
1.1 deraadt 512:
513: if (!(f & FFARG)) {
1.20 kjell 514: if ((bufp = eread("Goto line: ", buf, sizeof(buf),
515: EFNUL | EFNEW | EFCR)) == NULL)
1.24 kjell 516: return (ABORT);
517: if (bufp[0] == '\0')
1.18 db 518: return (ABORT);
1.23 kjell 519: n = (int)strtonum(buf, INT_MIN, INT_MAX, &err);
1.50 lum 520: if (err)
1.51 op 521: return(dobeep_msgs("Line number", err));
1.1 deraadt 522: }
1.38 jasper 523: return(setlineno(n));
524: }
525:
526: /*
527: * Set the line number and switch to it.
528: */
529: int
530: setlineno(int n)
531: {
532: struct line *clp;
533:
1.5 millert 534: if (n >= 0) {
1.23 kjell 535: if (n == 0)
536: n++;
537: curwp->w_dotline = n;
1.25 kjell 538: clp = lforw(curbp->b_headp); /* "clp" is first line */
1.1 deraadt 539: while (--n > 0) {
1.25 kjell 540: if (lforw(clp) == curbp->b_headp) {
1.23 kjell 541: curwp->w_dotline = curwp->w_bufp->b_lines;
1.3 millert 542: break;
1.23 kjell 543: }
1.1 deraadt 544: clp = lforw(clp);
545: }
546: } else {
1.23 kjell 547: curwp->w_dotline = curwp->w_bufp->b_lines + n;
1.25 kjell 548: clp = lback(curbp->b_headp); /* "clp" is last line */
1.1 deraadt 549: while (n < 0) {
1.25 kjell 550: if (lback(clp) == curbp->b_headp) {
1.23 kjell 551: curwp->w_dotline = 1;
1.3 millert 552: break;
1.23 kjell 553: }
1.1 deraadt 554: clp = lback(clp);
555: n++;
556: }
557: }
558: curwp->w_dotp = clp;
559: curwp->w_doto = 0;
1.30 kjell 560: curwp->w_rflag |= WFMOVE;
1.18 db 561: return (TRUE);
1.1 deraadt 562: }