Annotation of src/usr.bin/mg/buffer.c, Revision 1.50
1.50 ! kjell 1: /* $OpenBSD: buffer.c,v 1.49 2005/10/14 06:44:49 kjell Exp $ */
1.43 kjell 2:
3: /* This file is in the public domain. */
1.5 niklas 4:
1.1 deraadt 5: /*
6: * Buffer handling.
7: */
1.4 millert 8:
9: #include "def.h"
10: #include "kbd.h" /* needed for modes */
1.8 art 11: #include <stdarg.h>
1.1 deraadt 12:
1.22 millert 13: static BUFFER *makelist(void);
1.1 deraadt 14:
1.50 ! kjell 15: /* ARGSUSED */
1.27 vincent 16: int
1.31 vincent 17: togglereadonly(int f, int n)
1.27 vincent 18: {
1.47 kjell 19: if (!(curbp->b_flag & BFREADONLY))
1.27 vincent 20: curbp->b_flag |= BFREADONLY;
1.47 kjell 21: else {
1.27 vincent 22: curbp->b_flag &=~ BFREADONLY;
23: if (curbp->b_flag & BFCHG)
24: ewprintf("Warning: Buffer was modified");
25: }
26: curwp->w_flag |= WFMODE;
27:
1.47 kjell 28: return (TRUE);
1.27 vincent 29: }
30:
1.1 deraadt 31: /*
32: * Attach a buffer to a window. The values of dot and mark come
33: * from the buffer if the use count is 0. Otherwise, they come
34: * from some other window. *scratch* is the default alternate
35: * buffer.
36: */
1.3 millert 37: /* ARGSUSED */
38: int
1.26 vincent 39: usebuffer(int f, int n)
1.1 deraadt 40: {
1.3 millert 41: BUFFER *bp;
1.34 vincent 42: char bufn[NBUFN], *bufp;
1.1 deraadt 43:
44: /* Get buffer to use from user */
1.21 deraadt 45: if ((curbp->b_altb == NULL) &&
46: ((curbp->b_altb = bfind("*scratch*", TRUE)) == NULL))
1.34 vincent 47: bufp = eread("Switch to buffer: ", bufn, NBUFN, EFNEW | EFBUF);
1.1 deraadt 48: else
1.34 vincent 49: bufp = eread("Switch to buffer: (default %s) ", bufn, NBUFN,
1.40 kjell 50: EFNUL | EFNEW | EFBUF, curbp->b_altb->b_bname);
1.1 deraadt 51:
1.34 vincent 52: if (bufp == NULL)
1.37 db 53: return (ABORT);
1.44 kjell 54: if (bufp[0] == '\0' && curbp->b_altb != NULL)
1.3 millert 55: bp = curbp->b_altb;
56: else if ((bp = bfind(bufn, TRUE)) == NULL)
1.37 db 57: return (FALSE);
1.1 deraadt 58:
59: /* and put it in current window */
60: curbp = bp;
1.37 db 61: return (showbuffer(bp, curwp, WFFORCE | WFHARD));
1.1 deraadt 62: }
63:
64: /*
65: * pop to buffer asked for by the user.
66: */
1.3 millert 67: /* ARGSUSED */
68: int
1.26 vincent 69: poptobuffer(int f, int n)
1.1 deraadt 70: {
1.3 millert 71: BUFFER *bp;
72: MGWIN *wp;
1.34 vincent 73: char bufn[NBUFN], *bufp;
1.1 deraadt 74:
75: /* Get buffer to use from user */
1.21 deraadt 76: if ((curbp->b_altb == NULL) &&
77: ((curbp->b_altb = bfind("*scratch*", TRUE)) == NULL))
1.34 vincent 78: bufp = eread("Switch to buffer in other window: ", bufn, NBUFN,
1.3 millert 79: EFNEW | EFBUF);
1.1 deraadt 80: else
1.34 vincent 81: bufp = eread("Switch to buffer in other window: (default %s) ",
1.40 kjell 82: bufn, NBUFN, EFNUL | EFNEW | EFBUF, curbp->b_altb->b_bname);
1.34 vincent 83: if (bufp == NULL)
1.37 db 84: return (ABORT);
1.44 kjell 85: if (bufp[0] == '\0' && curbp->b_altb != NULL)
1.3 millert 86: bp = curbp->b_altb;
87: else if ((bp = bfind(bufn, TRUE)) == NULL)
1.37 db 88: return (FALSE);
1.1 deraadt 89:
90: /* and put it in a new window */
1.3 millert 91: if ((wp = popbuf(bp)) == NULL)
1.37 db 92: return (FALSE);
1.1 deraadt 93: curbp = bp;
94: curwp = wp;
1.37 db 95: return (TRUE);
1.1 deraadt 96: }
97:
98: /*
99: * Dispose of a buffer, by name.
100: * Ask for the name. Look it up (don't get too
101: * upset if it isn't there at all!). Clear the buffer (ask
102: * if the buffer has been changed). Then free the header
103: * line and the buffer header. Bound to "C-X K".
104: */
1.3 millert 105: /* ARGSUSED */
106: int
1.35 jfb 107: killbuffer_cmd(int f, int n)
1.1 deraadt 108: {
1.3 millert 109: BUFFER *bp;
1.34 vincent 110: char bufn[NBUFN], *bufp;
1.3 millert 111:
1.40 kjell 112: if ((bufp = eread("Kill buffer: (default %s) ", bufn, NBUFN,
113: EFNUL | EFNEW | EFBUF, curbp->b_bname)) == NULL)
1.37 db 114: return (ABORT);
1.44 kjell 115: else if (bufp[0] == '\0')
1.3 millert 116: bp = curbp;
117: else if ((bp = bfind(bufn, FALSE)) == NULL)
1.37 db 118: return (FALSE);
119: return (killbuffer(bp));
1.35 jfb 120: }
121:
122: int
123: killbuffer(BUFFER *bp)
124: {
125: BUFFER *bp1;
126: BUFFER *bp2;
127: MGWIN *wp;
1.41 kjell 128: int s;
1.48 deraadt 129: struct undo_rec *rec, *next;
1.3 millert 130:
131: /*
1.37 db 132: * Find some other buffer to display. Try the alternate buffer,
1.3 millert 133: * then the first different buffer in the buffer list. If there's
134: * only one buffer, create buffer *scratch* and make it the alternate
135: * buffer. Return if *scratch* is only buffer...
1.1 deraadt 136: */
137: if ((bp1 = bp->b_altb) == NULL) {
138: bp1 = (bp == bheadp) ? bp->b_bufp : bheadp;
139: if (bp1 == NULL) {
140: /* only one buffer. see if it's *scratch* */
1.3 millert 141: if (bp == bfind("*scratch*", FALSE))
1.49 kjell 142: return (TRUE);
1.1 deraadt 143: /* create *scratch* for alternate buffer */
1.3 millert 144: if ((bp1 = bfind("*scratch*", TRUE)) == NULL)
1.37 db 145: return (FALSE);
1.1 deraadt 146: }
147: }
1.41 kjell 148: if ((s = bclear(bp)) != TRUE)
149: return (s);
1.1 deraadt 150: for (wp = wheadp; bp->b_nwnd > 0; wp = wp->w_wndp) {
1.3 millert 151: if (wp->w_bufp == bp) {
152: bp2 = bp1->b_altb; /* save alternate buffer */
153: if (showbuffer(bp1, wp, WFMODE | WFFORCE | WFHARD))
154: bp1->b_altb = bp2;
155: else
156: bp1 = bp2;
157: }
158: }
159: if (bp == curbp)
160: curbp = bp1;
161: free(bp->b_linep); /* Release header line. */
162: bp2 = NULL; /* Find the header. */
1.1 deraadt 163: bp1 = bheadp;
164: while (bp1 != bp) {
165: if (bp1->b_altb == bp)
166: bp1->b_altb = (bp->b_altb == bp1) ? NULL : bp->b_altb;
167: bp2 = bp1;
168: bp1 = bp1->b_bufp;
169: }
1.3 millert 170: bp1 = bp1->b_bufp; /* Next one in chain. */
171: if (bp2 == NULL) /* Unlink it. */
1.1 deraadt 172: bheadp = bp1;
173: else
174: bp2->b_bufp = bp1;
1.3 millert 175: while (bp1 != NULL) { /* Finish with altb's */
1.1 deraadt 176: if (bp1->b_altb == bp)
177: bp1->b_altb = (bp->b_altb == bp1) ? NULL : bp->b_altb;
178: bp1 = bp1->b_bufp;
179: }
1.46 kjell 180: rec = LIST_FIRST(&bp->b_undo);
181: while (rec != NULL) {
182: next = LIST_NEXT(rec, next);
183: free_undo_record(rec);
184: rec = next;
185: }
186:
1.26 vincent 187: free((char *)bp->b_bname); /* Release name block */
1.3 millert 188: free(bp); /* Release buffer block */
1.37 db 189: return (TRUE);
1.1 deraadt 190: }
191:
192: /*
193: * Save some buffers - just call anycb with the arg flag.
194: */
1.3 millert 195: /* ARGSUSED */
196: int
1.26 vincent 197: savebuffers(int f, int n)
1.1 deraadt 198: {
1.3 millert 199: if (anycb(f) == ABORT)
1.37 db 200: return (ABORT);
201: return (TRUE);
1.1 deraadt 202: }
203:
204: /*
1.19 art 205: * Listing buffers.
206: */
207: static int listbuf_ncol;
208:
209: static int listbuf_goto_buffer(int f, int n);
1.39 jason 210: static int listbuf_goto_buffer_one(int f, int n);
211: static int listbuf_goto_buffer_helper(int f, int n, int only);
1.19 art 212:
213: static PF listbuf_pf[] = {
1.37 db 214: listbuf_goto_buffer
1.19 art 215: };
1.39 jason 216: static PF listbuf_one[] = {
217: listbuf_goto_buffer_one
218: };
219:
1.19 art 220:
1.39 jason 221: static struct KEYMAPE (2 + IMAPEXT) listbufmap = {
222: 2,
223: 2 + IMAPEXT,
1.19 art 224: rescan,
225: {
1.39 jason 226: {
1.45 deraadt 227: CCHR('M'), CCHR('M'), listbuf_pf, NULL
1.39 jason 228: },
229: {
1.45 deraadt 230: '1', '1', listbuf_one, NULL
1.39 jason 231: }
1.19 art 232: }
233: };
234:
235: /*
1.1 deraadt 236: * Display the buffer list. This is done
237: * in two parts. The "makelist" routine figures out
238: * the text, and puts it in a buffer. "popbuf"
239: * then pops the data onto the screen. Bound to
240: * "C-X C-B".
241: */
1.3 millert 242: /* ARGSUSED */
243: int
1.26 vincent 244: listbuffers(int f, int n)
1.1 deraadt 245: {
1.37 db 246: static int initialized = 0;
247: BUFFER *bp;
248: MGWIN *wp;
1.1 deraadt 249:
1.19 art 250: if (!initialized) {
251: maps_add((KEYMAP *)&listbufmap, "listbufmap");
252: initialized = 1;
253: }
254:
1.3 millert 255: if ((bp = makelist()) == NULL || (wp = popbuf(bp)) == NULL)
1.37 db 256: return (FALSE);
257: wp->w_dotp = bp->b_dotp; /* fix up if window already on screen */
1.1 deraadt 258: wp->w_doto = bp->b_doto;
1.19 art 259: bp->b_modes[0] = name_mode("fundamental");
260: bp->b_modes[1] = name_mode("listbufmap");
261: bp->b_nmodes = 1;
262:
1.37 db 263: return (TRUE);
1.1 deraadt 264: }
265:
266: /*
267: * This routine rebuilds the text for the
1.37 db 268: * list buffers command. Return pointer
269: * to new list if everything works.
270: * Return NULL if there is an error (if
271: * there is no memory).
1.1 deraadt 272: */
1.3 millert 273: static BUFFER *
1.26 vincent 274: makelist(void)
1.3 millert 275: {
1.11 art 276: int w = ncol / 2;
277: BUFFER *bp, *blp;
1.3 millert 278: LINE *lp;
279:
1.19 art 280:
1.3 millert 281: if ((blp = bfind("*Buffer List*", TRUE)) == NULL)
1.37 db 282: return (NULL);
1.3 millert 283: if (bclear(blp) != TRUE)
1.37 db 284: return (NULL);
1.3 millert 285: blp->b_flag &= ~BFCHG; /* Blow away old. */
1.28 vincent 286: blp->b_flag |= BFREADONLY;
1.1 deraadt 287:
1.19 art 288: listbuf_ncol = ncol; /* cache ncol for listbuf_goto_buffer */
289:
1.11 art 290: if (addlinef(blp, "%-*s%s", w, " MR Buffer", "Size File") == FALSE ||
291: addlinef(blp, "%-*s%s", w, " -- ------", "---- ----") == FALSE)
1.37 db 292: return (NULL);
1.11 art 293:
294: for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
295: RSIZE nbytes;
296:
1.3 millert 297: nbytes = 0; /* Count bytes in buf. */
1.1 deraadt 298: if (bp != blp) {
299: lp = lforw(bp->b_linep);
300: while (lp != bp->b_linep) {
1.3 millert 301: nbytes += llength(lp) + 1;
1.1 deraadt 302: lp = lforw(lp);
303: }
1.3 millert 304: if (nbytes)
305: nbytes--; /* no bonus newline */
1.1 deraadt 306: }
1.11 art 307:
1.19 art 308: if (addlinef(blp, "%c%c%c %-*.*s%c%-6d %-*s",
1.11 art 309: (bp == curbp) ? '.' : ' ', /* current buffer ? */
310: ((bp->b_flag & BFCHG) != 0) ? '*' : ' ', /* changed ? */
1.27 vincent 311: ((bp->b_flag & BFREADONLY) != 0) ? ' ' : '*',
1.19 art 312: w - 5, /* four chars already written */
313: w - 5, /* four chars already written */
1.11 art 314: bp->b_bname, /* buffer name */
1.19 art 315: strlen(bp->b_bname) < w - 5 ? ' ' : '$', /* truncated? */
1.11 art 316: nbytes, /* buffer size */
317: w - 7, /* seven chars already written */
318: bp->b_fname) == FALSE)
1.37 db 319: return (NULL);
1.1 deraadt 320: }
1.3 millert 321: blp->b_dotp = lforw(blp->b_linep); /* put dot at beginning of
322: * buffer */
1.1 deraadt 323: blp->b_doto = 0;
1.37 db 324: return (blp); /* All done */
1.19 art 325: }
326:
327: static int
328: listbuf_goto_buffer(int f, int n)
329: {
1.39 jason 330: return (listbuf_goto_buffer_helper(f, n, 0));
331: }
332:
333: static int
334: listbuf_goto_buffer_one(int f, int n)
335: {
336: return (listbuf_goto_buffer_helper(f, n, 1));
337: }
338:
339: static int
340: listbuf_goto_buffer_helper(int f, int n, int only)
341: {
1.37 db 342: BUFFER *bp;
343: MGWIN *wp;
1.42 cloder 344: char *line = NULL;
345: int i, ret = FALSE;
1.19 art 346:
347: if (curwp->w_dotp->l_text[listbuf_ncol/2 - 1] == '$') {
348: ewprintf("buffer name truncated");
1.37 db 349: return (FALSE);
1.19 art 350: }
351:
352: if ((line = malloc(listbuf_ncol/2)) == NULL)
1.37 db 353: return (FALSE);
1.19 art 354:
355: memcpy(line, curwp->w_dotp->l_text + 4, listbuf_ncol/2 - 5);
356: for (i = listbuf_ncol/2 - 6; i > 0; i--) {
357: if (line[i] != ' ') {
358: line[i + 1] = '\0';
359: break;
360: }
361: }
1.37 db 362: if (i == 0)
1.42 cloder 363: goto cleanup;
1.19 art 364:
365: for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
366: if (strcmp(bp->b_bname, line) == 0)
367: break;
368: }
1.37 db 369: if (bp == NULL)
1.42 cloder 370: goto cleanup;
1.37 db 371:
1.19 art 372: if ((wp = popbuf(bp)) == NULL)
1.42 cloder 373: goto cleanup;
1.19 art 374: curbp = bp;
375: curwp = wp;
1.39 jason 376:
377: if (only)
1.42 cloder 378: ret = (onlywind(f, n));
379: else
380: ret = TRUE;
381:
382: cleanup:
383: free(line);
1.19 art 384:
1.42 cloder 385: return (ret);
1.1 deraadt 386: }
387:
388: /*
1.47 kjell 389: * The argument "fmt" points to a format string. Append this line to the
1.3 millert 390: * buffer. Handcraft the EOL on the end. Return TRUE if it worked and
1.1 deraadt 391: * FALSE if you ran out of room.
392: */
1.3 millert 393: int
1.8 art 394: addlinef(BUFFER *bp, char *fmt, ...)
1.3 millert 395: {
1.37 db 396: va_list ap;
397: LINE *lp;
1.1 deraadt 398:
1.30 vincent 399: if ((lp = lalloc(0)) == NULL)
400: return (FALSE);
1.8 art 401: va_start(ap, fmt);
1.30 vincent 402: if (vasprintf(&lp->l_text, fmt, ap) == -1) {
403: lfree(lp);
1.16 deraadt 404: va_end(ap);
1.30 vincent 405: return (FALSE);
1.16 deraadt 406: }
1.30 vincent 407: lp->l_used = strlen(lp->l_text);
1.8 art 408: va_end(ap);
409:
1.3 millert 410: bp->b_linep->l_bp->l_fp = lp; /* Hook onto the end */
1.1 deraadt 411: lp->l_bp = bp->b_linep->l_bp;
412: bp->b_linep->l_bp = lp;
413: lp->l_fp = bp->b_linep;
1.8 art 414:
1.37 db 415: return (TRUE);
1.1 deraadt 416: }
417:
418: /*
1.3 millert 419: * Look through the list of buffers, giving the user a chance to save them.
420: * Return TRUE if there are any changed buffers afterwards. Buffers that
421: * don't have an associated file don't count. Return FALSE if there are
422: * no changed buffers.
423: */
424: int
1.26 vincent 425: anycb(int f)
1.3 millert 426: {
427: BUFFER *bp;
1.37 db 428: int s = FALSE, save = FALSE;
429: char prompt[NFILEN + 11];
1.1 deraadt 430:
431: for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
1.21 deraadt 432: if (bp->b_fname != NULL && *(bp->b_fname) != '\0' &&
433: (bp->b_flag & BFCHG) != 0) {
1.37 db 434: snprintf(prompt, sizeof(prompt), "Save file %s",
1.17 deraadt 435: bp->b_fname);
1.21 deraadt 436: if ((f == TRUE || (save = eyorn(prompt)) == TRUE) &&
437: buffsave(bp) == TRUE) {
1.1 deraadt 438: bp->b_flag &= ~BFCHG;
439: upmodes(bp);
1.3 millert 440: } else
441: s = TRUE;
442: if (save == ABORT)
443: return (save);
1.1 deraadt 444: save = TRUE;
445: }
446: }
447: if (save == FALSE /* && kbdmop == NULL */ ) /* experimental */
448: ewprintf("(No files need saving)");
1.37 db 449: return (s);
1.1 deraadt 450: }
451:
452: /*
453: * Search for a buffer, by name.
454: * If not found, and the "cflag" is TRUE,
455: * create a buffer and put it in the list of
456: * all buffers. Return pointer to the BUFFER
457: * block for the buffer.
458: */
1.3 millert 459: BUFFER *
1.26 vincent 460: bfind(const char *bname, int cflag)
1.3 millert 461: {
1.4 millert 462: BUFFER *bp;
463: LINE *lp;
1.15 mickey 464: int i;
1.1 deraadt 465:
466: bp = bheadp;
467: while (bp != NULL) {
1.7 art 468: if (strcmp(bname, bp->b_bname) == 0)
1.37 db 469: return (bp);
1.1 deraadt 470: bp = bp->b_bufp;
471: }
1.3 millert 472: if (cflag != TRUE)
1.37 db 473: return (NULL);
1.29 vincent 474:
1.32 vincent 475: bp = calloc(1, sizeof(BUFFER));
476: if (bp == NULL) {
1.1 deraadt 477: ewprintf("Can't get %d bytes", sizeof(BUFFER));
1.37 db 478: return (NULL);
1.1 deraadt 479: }
1.20 vincent 480: if ((bp->b_bname = strdup(bname)) == NULL) {
1.3 millert 481: ewprintf("Can't get %d bytes", strlen(bname) + 1);
1.29 vincent 482: free(bp);
1.37 db 483: return (NULL);
1.1 deraadt 484: }
485: if ((lp = lalloc(0)) == NULL) {
1.26 vincent 486: free((char *) bp->b_bname);
1.29 vincent 487: free(bp);
1.37 db 488: return (NULL);
1.1 deraadt 489: }
1.3 millert 490: bp->b_altb = bp->b_bufp = NULL;
491: bp->b_dotp = lp;
492: bp->b_doto = 0;
1.1 deraadt 493: bp->b_markp = NULL;
494: bp->b_marko = 0;
1.3 millert 495: bp->b_flag = defb_flag;
496: bp->b_nwnd = 0;
1.1 deraadt 497: bp->b_linep = lp;
498: bp->b_nmodes = defb_nmodes;
1.46 kjell 499: LIST_INIT(&bp->b_undo);
500: bp->b_undoptr = NULL;
501: memset(&bp->b_undopos, 0, sizeof(bp->b_undopos));
1.1 deraadt 502: i = 0;
503: do {
1.3 millert 504: bp->b_modes[i] = defb_modes[i];
505: } while (i++ < defb_nmodes);
1.1 deraadt 506: bp->b_fname[0] = '\0';
507: bzero(&bp->b_fi, sizeof(bp->b_fi));
508: lp->l_fp = lp;
509: lp->l_bp = lp;
510: bp->b_bufp = bheadp;
511: bheadp = bp;
1.37 db 512: return (bp);
1.1 deraadt 513: }
514:
515: /*
516: * This routine blows away all of the text
517: * in a buffer. If the buffer is marked as changed
518: * then we ask if it is ok to blow it away; this is
519: * to save the user the grief of losing text. The
520: * window chain is nearly always wrong if this gets
521: * called; the caller must arrange for the updates
522: * that are required. Return TRUE if everything
523: * looks good.
524: */
1.3 millert 525: int
1.26 vincent 526: bclear(BUFFER *bp)
1.3 millert 527: {
528: LINE *lp;
529: int s;
1.1 deraadt 530:
1.37 db 531: if ((bp->b_flag & BFCHG) != 0 && /* Changed. */
1.21 deraadt 532: (s = eyesno("Buffer modified; kill anyway")) != TRUE)
1.1 deraadt 533: return (s);
1.3 millert 534: bp->b_flag &= ~BFCHG; /* Not changed */
535: while ((lp = lforw(bp->b_linep)) != bp->b_linep)
1.1 deraadt 536: lfree(lp);
1.32 vincent 537: bp->b_dotp = bp->b_linep; /* Fix dot */
1.3 millert 538: bp->b_doto = 0;
539: bp->b_markp = NULL; /* Invalidate "mark" */
1.1 deraadt 540: bp->b_marko = 0;
1.37 db 541: return (TRUE);
1.1 deraadt 542: }
543:
544: /*
545: * Display the given buffer in the given window. Flags indicated
546: * action on redisplay.
547: */
1.3 millert 548: int
1.26 vincent 549: showbuffer(BUFFER *bp, MGWIN *wp, int flags)
1.3 millert 550: {
551: BUFFER *obp;
552: MGWIN *owp;
1.1 deraadt 553:
1.32 vincent 554: if (wp->w_bufp == bp) { /* Easy case! */
1.1 deraadt 555: wp->w_flag |= flags;
1.32 vincent 556: wp->w_dotp = bp->b_dotp;
557: wp->w_doto = bp->b_doto;
1.37 db 558: return (TRUE);
1.1 deraadt 559: }
1.37 db 560: /* First, detach the old buffer from the window */
1.1 deraadt 561: if ((bp->b_altb = obp = wp->w_bufp) != NULL) {
562: if (--obp->b_nwnd == 0) {
1.3 millert 563: obp->b_dotp = wp->w_dotp;
564: obp->b_doto = wp->w_doto;
1.1 deraadt 565: obp->b_markp = wp->w_markp;
566: obp->b_marko = wp->w_marko;
567: }
568: }
569: /* Now, attach the new buffer to the window */
570: wp->w_bufp = bp;
571:
1.3 millert 572: if (bp->b_nwnd++ == 0) { /* First use. */
573: wp->w_dotp = bp->b_dotp;
574: wp->w_doto = bp->b_doto;
1.1 deraadt 575: wp->w_markp = bp->b_markp;
576: wp->w_marko = bp->b_marko;
577: } else
1.3 millert 578: /* already on screen, steal values from other window */
1.1 deraadt 579: for (owp = wheadp; owp != NULL; owp = wp->w_wndp)
580: if (wp->w_bufp == bp && owp != wp) {
1.3 millert 581: wp->w_dotp = owp->w_dotp;
582: wp->w_doto = owp->w_doto;
1.1 deraadt 583: wp->w_markp = owp->w_markp;
584: wp->w_marko = owp->w_marko;
585: break;
586: }
1.3 millert 587: wp->w_flag |= WFMODE | flags;
1.37 db 588: return (TRUE);
1.1 deraadt 589: }
590:
591: /*
592: * Pop the buffer we got passed onto the screen.
593: * Returns a status.
594: */
1.2 millert 595: MGWIN *
1.26 vincent 596: popbuf(BUFFER *bp)
1.3 millert 597: {
598: MGWIN *wp;
1.1 deraadt 599:
1.3 millert 600: if (bp->b_nwnd == 0) { /* Not on screen yet. */
601: if ((wp = wpopup()) == NULL)
1.37 db 602: return (NULL);
1.1 deraadt 603: } else
604: for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
605: if (wp->w_bufp == bp) {
1.3 millert 606: wp->w_flag |= WFHARD | WFFORCE;
1.37 db 607: return (wp);
1.1 deraadt 608: }
1.3 millert 609: if (showbuffer(bp, wp, WFHARD) != TRUE)
1.37 db 610: return (NULL);
611: return (wp);
1.1 deraadt 612: }
613:
614: /*
615: * Insert another buffer at dot. Very useful.
616: */
1.3 millert 617: /* ARGSUSED */
618: int
1.26 vincent 619: bufferinsert(int f, int n)
1.1 deraadt 620: {
1.3 millert 621: BUFFER *bp;
622: LINE *clp;
1.36 deraadt 623: int clo, nline;
624: char bufn[NBUFN], *bufp;
1.1 deraadt 625:
626: /* Get buffer to use from user */
627: if (curbp->b_altb != NULL)
1.34 vincent 628: bufp = eread("Insert buffer: (default %s) ", bufn, NBUFN,
1.40 kjell 629: EFNUL | EFNEW | EFBUF, curbp->b_altb->b_bname);
1.1 deraadt 630: else
1.38 cloder 631: bufp = eread("Insert buffer: ", bufn, NBUFN, EFNEW | EFBUF);
1.34 vincent 632: if (bufp == NULL)
1.37 db 633: return (ABORT);
1.34 vincent 634: if (bufp[0] == '\0' && curbp->b_altb != NULL)
1.3 millert 635: bp = curbp->b_altb;
636: else if ((bp = bfind(bufn, FALSE)) == NULL)
1.37 db 637: return (FALSE);
1.1 deraadt 638:
1.3 millert 639: if (bp == curbp) {
1.1 deraadt 640: ewprintf("Cannot insert buffer into self");
1.37 db 641: return (FALSE);
1.1 deraadt 642: }
643: /* insert the buffer */
644: nline = 0;
645: clp = lforw(bp->b_linep);
1.3 millert 646: for (;;) {
1.1 deraadt 647: for (clo = 0; clo < llength(clp); clo++)
648: if (linsert(1, lgetc(clp, clo)) == FALSE)
1.37 db 649: return (FALSE);
1.3 millert 650: if ((clp = lforw(clp)) == bp->b_linep)
651: break;
652: if (newline(FFRAND, 1) == FALSE) /* fake newline */
1.37 db 653: return (FALSE);
1.1 deraadt 654: nline++;
655: }
1.3 millert 656: if (nline == 1)
657: ewprintf("[Inserted 1 line]");
658: else
659: ewprintf("[Inserted %d lines]", nline);
1.1 deraadt 660:
1.37 db 661: clp = curwp->w_linep; /* cosmetic adjustment */
1.3 millert 662: if (curwp->w_dotp == clp) { /* for offscreen insert */
663: while (nline-- && lback(clp) != curbp->b_linep)
1.1 deraadt 664: clp = lback(clp);
1.37 db 665: curwp->w_linep = clp; /* adjust framing. */
1.1 deraadt 666: curwp->w_flag |= WFHARD;
667: }
668: return (TRUE);
669: }
670:
671: /*
672: * Turn off the dirty bit on this buffer.
673: */
1.3 millert 674: /* ARGSUSED */
675: int
1.26 vincent 676: notmodified(int f, int n)
1.1 deraadt 677: {
1.3 millert 678: MGWIN *wp;
1.1 deraadt 679:
680: curbp->b_flag &= ~BFCHG;
1.3 millert 681: wp = wheadp; /* Update mode lines. */
1.1 deraadt 682: while (wp != NULL) {
683: if (wp->w_bufp == curbp)
684: wp->w_flag |= WFMODE;
685: wp = wp->w_wndp;
686: }
687: ewprintf("Modification-flag cleared");
1.37 db 688: return (TRUE);
1.1 deraadt 689: }
690:
691: #ifndef NO_HELP
692: /*
693: * Popbuf and set all windows to top of buffer. Currently only used by
694: * help functions.
695: */
1.3 millert 696: int
1.26 vincent 697: popbuftop(BUFFER *bp)
1.1 deraadt 698: {
1.3 millert 699: MGWIN *wp;
1.1 deraadt 700:
1.3 millert 701: bp->b_dotp = lforw(bp->b_linep);
702: bp->b_doto = 0;
703: if (bp->b_nwnd != 0) {
704: for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
705: if (wp->w_bufp == bp) {
706: wp->w_dotp = bp->b_dotp;
707: wp->w_doto = 0;
708: wp->w_flag |= WFHARD;
709: }
710: }
1.37 db 711: return (popbuf(bp) != NULL);
1.1 deraadt 712: }
713: #endif