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