Annotation of src/usr.bin/mg/buffer.c, Revision 1.93
1.93 ! lum 1: /* $OpenBSD: buffer.c,v 1.92 2013/12/23 14:58:16 lum 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.76 kjell 9: #include "def.h"
10: #include "kbd.h" /* needed for modes */
11:
1.56 kjell 12: #include <libgen.h>
13: #include <stdarg.h>
1.1 deraadt 14:
1.88 florian 15: #ifndef DIFFTOOL
16: #define DIFFTOOL "/usr/bin/diff"
17: #endif /* !DIFFTOOL */
18:
1.52 deraadt 19: static struct buffer *makelist(void);
1.65 kjell 20: static struct buffer *bnew(const char *);
1.75 lum 21:
22: static int usebufname(const char *);
1.1 deraadt 23:
1.67 kjell 24: /* Flag for global working dir */
25: extern int globalwd;
26:
1.50 kjell 27: /* ARGSUSED */
1.27 vincent 28: int
1.31 vincent 29: togglereadonly(int f, int n)
1.27 vincent 30: {
1.68 kjell 31: int s;
32:
33: if ((s = checkdirty(curbp)) != TRUE)
34: return (s);
1.47 kjell 35: if (!(curbp->b_flag & BFREADONLY))
1.27 vincent 36: curbp->b_flag |= BFREADONLY;
1.47 kjell 37: else {
1.68 kjell 38: curbp->b_flag &= ~BFREADONLY;
1.27 vincent 39: if (curbp->b_flag & BFCHG)
40: ewprintf("Warning: Buffer was modified");
41: }
1.70 kjell 42: curwp->w_rflag |= WFMODE;
1.27 vincent 43:
1.47 kjell 44: return (TRUE);
1.27 vincent 45: }
46:
1.73 deraadt 47: /* Switch to the named buffer.
48: * If no name supplied, switch to the default (alternate) buffer.
49: */
50: int
51: usebufname(const char *bufp)
52: {
53: struct buffer *bp;
54:
55: if (bufp == NULL)
56: return (ABORT);
57: if (bufp[0] == '\0' && curbp->b_altb != NULL)
58: bp = curbp->b_altb;
59: else if ((bp = bfind(bufp, TRUE)) == NULL)
60: return (FALSE);
61:
62: /* and put it in current window */
63: curbp = bp;
64: return (showbuffer(bp, curwp, WFFRAME | WFFULL));
65: }
66:
1.1 deraadt 67: /*
68: * Attach a buffer to a window. The values of dot and mark come
69: * from the buffer if the use count is 0. Otherwise, they come
70: * from some other window. *scratch* is the default alternate
71: * buffer.
72: */
1.3 millert 73: /* ARGSUSED */
74: int
1.26 vincent 75: usebuffer(int f, int n)
1.1 deraadt 76: {
1.34 vincent 77: char bufn[NBUFN], *bufp;
1.1 deraadt 78:
79: /* Get buffer to use from user */
1.21 deraadt 80: if ((curbp->b_altb == NULL) &&
81: ((curbp->b_altb = bfind("*scratch*", TRUE)) == NULL))
1.34 vincent 82: bufp = eread("Switch to buffer: ", bufn, NBUFN, EFNEW | EFBUF);
1.1 deraadt 83: else
1.34 vincent 84: bufp = eread("Switch to buffer: (default %s) ", bufn, NBUFN,
1.55 deraadt 85: EFNUL | EFNEW | EFBUF, curbp->b_altb->b_bname);
1.1 deraadt 86:
1.72 kjell 87: return (usebufname(bufp));
1.1 deraadt 88: }
89:
90: /*
91: * pop to buffer asked for by the user.
92: */
1.3 millert 93: /* ARGSUSED */
94: int
1.26 vincent 95: poptobuffer(int f, int n)
1.1 deraadt 96: {
1.52 deraadt 97: struct buffer *bp;
98: struct mgwin *wp;
1.34 vincent 99: char bufn[NBUFN], *bufp;
1.1 deraadt 100:
101: /* Get buffer to use from user */
1.21 deraadt 102: if ((curbp->b_altb == NULL) &&
103: ((curbp->b_altb = bfind("*scratch*", TRUE)) == NULL))
1.34 vincent 104: bufp = eread("Switch to buffer in other window: ", bufn, NBUFN,
1.55 deraadt 105: EFNEW | EFBUF);
1.1 deraadt 106: else
1.34 vincent 107: bufp = eread("Switch to buffer in other window: (default %s) ",
1.55 deraadt 108: bufn, NBUFN, EFNUL | EFNEW | EFBUF, curbp->b_altb->b_bname);
1.34 vincent 109: if (bufp == NULL)
1.37 db 110: return (ABORT);
1.44 kjell 111: if (bufp[0] == '\0' && curbp->b_altb != NULL)
1.3 millert 112: bp = curbp->b_altb;
113: else if ((bp = bfind(bufn, TRUE)) == NULL)
1.37 db 114: return (FALSE);
1.59 kjell 115: if (bp == curbp)
116: return (splitwind(f, n));
1.71 kjell 117: /* and put it in a new, non-ephemeral window */
118: if ((wp = popbuf(bp, WNONE)) == NULL)
1.37 db 119: return (FALSE);
1.1 deraadt 120: curbp = bp;
121: curwp = wp;
1.37 db 122: return (TRUE);
1.1 deraadt 123: }
124:
125: /*
126: * Dispose of a buffer, by name.
1.91 lum 127: * Ask for the name (unless called by dired mode). Look it up (don't
128: * get too upset if it isn't there at all!). Clear the buffer (ask
1.1 deraadt 129: * if the buffer has been changed). Then free the header
1.91 lum 130: * line and the buffer header. Bound to "C-x k".
1.1 deraadt 131: */
1.3 millert 132: /* ARGSUSED */
133: int
1.35 jfb 134: killbuffer_cmd(int f, int n)
1.1 deraadt 135: {
1.52 deraadt 136: struct buffer *bp;
1.34 vincent 137: char bufn[NBUFN], *bufp;
1.3 millert 138:
1.91 lum 139: if (f & FFRAND) /* dired mode 'q' */
140: bp = curbp;
141: else if ((bufp = eread("Kill buffer: (default %s) ", bufn, NBUFN,
1.40 kjell 142: EFNUL | EFNEW | EFBUF, curbp->b_bname)) == NULL)
1.37 db 143: return (ABORT);
1.44 kjell 144: else if (bufp[0] == '\0')
1.3 millert 145: bp = curbp;
146: else if ((bp = bfind(bufn, FALSE)) == NULL)
1.37 db 147: return (FALSE);
148: return (killbuffer(bp));
1.35 jfb 149: }
150:
151: int
1.52 deraadt 152: killbuffer(struct buffer *bp)
1.35 jfb 153: {
1.52 deraadt 154: struct buffer *bp1;
155: struct buffer *bp2;
156: struct mgwin *wp;
1.41 kjell 157: int s;
1.90 florian 158: struct undo_rec *rec;
1.3 millert 159:
160: /*
1.37 db 161: * Find some other buffer to display. Try the alternate buffer,
1.3 millert 162: * then the first different buffer in the buffer list. If there's
163: * only one buffer, create buffer *scratch* and make it the alternate
164: * buffer. Return if *scratch* is only buffer...
1.1 deraadt 165: */
166: if ((bp1 = bp->b_altb) == NULL) {
167: bp1 = (bp == bheadp) ? bp->b_bufp : bheadp;
168: if (bp1 == NULL) {
169: /* only one buffer. see if it's *scratch* */
1.3 millert 170: if (bp == bfind("*scratch*", FALSE))
1.49 kjell 171: return (TRUE);
1.1 deraadt 172: /* create *scratch* for alternate buffer */
1.3 millert 173: if ((bp1 = bfind("*scratch*", TRUE)) == NULL)
1.37 db 174: return (FALSE);
1.1 deraadt 175: }
176: }
1.41 kjell 177: if ((s = bclear(bp)) != TRUE)
178: return (s);
1.1 deraadt 179: for (wp = wheadp; bp->b_nwnd > 0; wp = wp->w_wndp) {
1.3 millert 180: if (wp->w_bufp == bp) {
181: bp2 = bp1->b_altb; /* save alternate buffer */
1.58 kjell 182: if (showbuffer(bp1, wp, WFMODE | WFFRAME | WFFULL))
1.3 millert 183: bp1->b_altb = bp2;
184: else
185: bp1 = bp2;
186: }
187: }
188: if (bp == curbp)
189: curbp = bp1;
1.62 kjell 190: free(bp->b_headp); /* Release header line. */
1.3 millert 191: bp2 = NULL; /* Find the header. */
1.1 deraadt 192: bp1 = bheadp;
193: while (bp1 != bp) {
194: if (bp1->b_altb == bp)
195: bp1->b_altb = (bp->b_altb == bp1) ? NULL : bp->b_altb;
196: bp2 = bp1;
197: bp1 = bp1->b_bufp;
198: }
1.3 millert 199: bp1 = bp1->b_bufp; /* Next one in chain. */
200: if (bp2 == NULL) /* Unlink it. */
1.1 deraadt 201: bheadp = bp1;
202: else
203: bp2->b_bufp = bp1;
1.3 millert 204: while (bp1 != NULL) { /* Finish with altb's */
1.1 deraadt 205: if (bp1->b_altb == bp)
206: bp1->b_altb = (bp->b_altb == bp1) ? NULL : bp->b_altb;
207: bp1 = bp1->b_bufp;
208: }
1.74 oga 209:
1.90 florian 210: while ((rec = TAILQ_FIRST(&bp->b_undo))) {
211: TAILQ_REMOVE(&bp->b_undo, rec, next);
1.46 kjell 212: free_undo_record(rec);
213: }
214:
1.54 kjell 215: free(bp->b_bname); /* Release name block */
1.3 millert 216: free(bp); /* Release buffer block */
1.37 db 217: return (TRUE);
1.1 deraadt 218: }
219:
220: /*
221: * Save some buffers - just call anycb with the arg flag.
222: */
1.3 millert 223: /* ARGSUSED */
224: int
1.26 vincent 225: savebuffers(int f, int n)
1.1 deraadt 226: {
1.3 millert 227: if (anycb(f) == ABORT)
1.37 db 228: return (ABORT);
229: return (TRUE);
1.1 deraadt 230: }
231:
232: /*
1.19 art 233: * Listing buffers.
234: */
235: static int listbuf_ncol;
236:
237: static int listbuf_goto_buffer(int f, int n);
1.39 jason 238: static int listbuf_goto_buffer_one(int f, int n);
239: static int listbuf_goto_buffer_helper(int f, int n, int only);
1.19 art 240:
241: static PF listbuf_pf[] = {
1.37 db 242: listbuf_goto_buffer
1.19 art 243: };
1.39 jason 244: static PF listbuf_one[] = {
245: listbuf_goto_buffer_one
246: };
247:
1.19 art 248:
1.39 jason 249: static struct KEYMAPE (2 + IMAPEXT) listbufmap = {
250: 2,
251: 2 + IMAPEXT,
1.19 art 252: rescan,
253: {
1.39 jason 254: {
1.45 deraadt 255: CCHR('M'), CCHR('M'), listbuf_pf, NULL
1.39 jason 256: },
257: {
1.45 deraadt 258: '1', '1', listbuf_one, NULL
1.39 jason 259: }
1.19 art 260: }
261: };
262:
263: /*
1.1 deraadt 264: * Display the buffer list. This is done
265: * in two parts. The "makelist" routine figures out
266: * the text, and puts it in a buffer. "popbuf"
267: * then pops the data onto the screen. Bound to
268: * "C-X C-B".
269: */
1.3 millert 270: /* ARGSUSED */
271: int
1.26 vincent 272: listbuffers(int f, int n)
1.1 deraadt 273: {
1.52 deraadt 274: static int initialized = 0;
275: struct buffer *bp;
276: struct mgwin *wp;
1.1 deraadt 277:
1.19 art 278: if (!initialized) {
279: maps_add((KEYMAP *)&listbufmap, "listbufmap");
280: initialized = 1;
281: }
282:
1.71 kjell 283: if ((bp = makelist()) == NULL || (wp = popbuf(bp, WNONE)) == NULL)
1.37 db 284: return (FALSE);
285: wp->w_dotp = bp->b_dotp; /* fix up if window already on screen */
1.1 deraadt 286: wp->w_doto = bp->b_doto;
1.19 art 287: bp->b_modes[0] = name_mode("fundamental");
288: bp->b_modes[1] = name_mode("listbufmap");
289: bp->b_nmodes = 1;
290:
1.37 db 291: return (TRUE);
1.1 deraadt 292: }
293:
294: /*
295: * This routine rebuilds the text for the
1.37 db 296: * list buffers command. Return pointer
297: * to new list if everything works.
298: * Return NULL if there is an error (if
299: * there is no memory).
1.1 deraadt 300: */
1.52 deraadt 301: static struct buffer *
1.26 vincent 302: makelist(void)
1.3 millert 303: {
1.52 deraadt 304: int w = ncol / 2;
305: struct buffer *bp, *blp;
306: struct line *lp;
1.19 art 307:
1.3 millert 308: if ((blp = bfind("*Buffer List*", TRUE)) == NULL)
1.37 db 309: return (NULL);
1.3 millert 310: if (bclear(blp) != TRUE)
1.37 db 311: return (NULL);
1.3 millert 312: blp->b_flag &= ~BFCHG; /* Blow away old. */
1.28 vincent 313: blp->b_flag |= BFREADONLY;
1.1 deraadt 314:
1.19 art 315: listbuf_ncol = ncol; /* cache ncol for listbuf_goto_buffer */
316:
1.11 art 317: if (addlinef(blp, "%-*s%s", w, " MR Buffer", "Size File") == FALSE ||
318: addlinef(blp, "%-*s%s", w, " -- ------", "---- ----") == FALSE)
1.37 db 319: return (NULL);
1.11 art 320:
321: for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
322: RSIZE nbytes;
323:
1.3 millert 324: nbytes = 0; /* Count bytes in buf. */
1.1 deraadt 325: if (bp != blp) {
1.63 kjell 326: lp = bfirstlp(bp);
1.62 kjell 327: while (lp != bp->b_headp) {
1.3 millert 328: nbytes += llength(lp) + 1;
1.1 deraadt 329: lp = lforw(lp);
330: }
1.3 millert 331: if (nbytes)
332: nbytes--; /* no bonus newline */
1.1 deraadt 333: }
1.11 art 334:
1.19 art 335: if (addlinef(blp, "%c%c%c %-*.*s%c%-6d %-*s",
1.11 art 336: (bp == curbp) ? '.' : ' ', /* current buffer ? */
337: ((bp->b_flag & BFCHG) != 0) ? '*' : ' ', /* changed ? */
1.27 vincent 338: ((bp->b_flag & BFREADONLY) != 0) ? ' ' : '*',
1.19 art 339: w - 5, /* four chars already written */
340: w - 5, /* four chars already written */
1.11 art 341: bp->b_bname, /* buffer name */
1.19 art 342: strlen(bp->b_bname) < w - 5 ? ' ' : '$', /* truncated? */
1.11 art 343: nbytes, /* buffer size */
344: w - 7, /* seven chars already written */
345: bp->b_fname) == FALSE)
1.37 db 346: return (NULL);
1.1 deraadt 347: }
1.63 kjell 348: blp->b_dotp = bfirstlp(blp); /* put dot at beginning of
1.3 millert 349: * buffer */
1.1 deraadt 350: blp->b_doto = 0;
1.37 db 351: return (blp); /* All done */
1.19 art 352: }
353:
354: static int
355: listbuf_goto_buffer(int f, int n)
356: {
1.39 jason 357: return (listbuf_goto_buffer_helper(f, n, 0));
358: }
359:
360: static int
361: listbuf_goto_buffer_one(int f, int n)
362: {
363: return (listbuf_goto_buffer_helper(f, n, 1));
364: }
365:
366: static int
367: listbuf_goto_buffer_helper(int f, int n, int only)
368: {
1.52 deraadt 369: struct buffer *bp;
370: struct mgwin *wp;
371: char *line = NULL;
372: int i, ret = FALSE;
1.19 art 373:
374: if (curwp->w_dotp->l_text[listbuf_ncol/2 - 1] == '$') {
1.93 ! lum 375: dobeep();
1.19 art 376: ewprintf("buffer name truncated");
1.37 db 377: return (FALSE);
1.19 art 378: }
379:
380: if ((line = malloc(listbuf_ncol/2)) == NULL)
1.37 db 381: return (FALSE);
1.19 art 382:
383: memcpy(line, curwp->w_dotp->l_text + 4, listbuf_ncol/2 - 5);
384: for (i = listbuf_ncol/2 - 6; i > 0; i--) {
385: if (line[i] != ' ') {
386: line[i + 1] = '\0';
387: break;
388: }
389: }
1.37 db 390: if (i == 0)
1.42 cloder 391: goto cleanup;
1.19 art 392:
393: for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
394: if (strcmp(bp->b_bname, line) == 0)
395: break;
396: }
1.37 db 397: if (bp == NULL)
1.42 cloder 398: goto cleanup;
1.37 db 399:
1.71 kjell 400: if ((wp = popbuf(bp, WNONE)) == NULL)
1.42 cloder 401: goto cleanup;
1.19 art 402: curbp = bp;
403: curwp = wp;
1.39 jason 404:
405: if (only)
1.42 cloder 406: ret = (onlywind(f, n));
407: else
408: ret = TRUE;
409:
410: cleanup:
411: free(line);
1.19 art 412:
1.42 cloder 413: return (ret);
1.1 deraadt 414: }
415:
416: /*
1.47 kjell 417: * The argument "fmt" points to a format string. Append this line to the
1.3 millert 418: * buffer. Handcraft the EOL on the end. Return TRUE if it worked and
1.1 deraadt 419: * FALSE if you ran out of room.
420: */
1.3 millert 421: int
1.52 deraadt 422: addlinef(struct buffer *bp, char *fmt, ...)
1.3 millert 423: {
1.52 deraadt 424: va_list ap;
425: struct line *lp;
1.1 deraadt 426:
1.30 vincent 427: if ((lp = lalloc(0)) == NULL)
428: return (FALSE);
1.8 art 429: va_start(ap, fmt);
1.30 vincent 430: if (vasprintf(&lp->l_text, fmt, ap) == -1) {
431: lfree(lp);
1.16 deraadt 432: va_end(ap);
1.30 vincent 433: return (FALSE);
1.16 deraadt 434: }
1.30 vincent 435: lp->l_used = strlen(lp->l_text);
1.8 art 436: va_end(ap);
437:
1.62 kjell 438: bp->b_headp->l_bp->l_fp = lp; /* Hook onto the end */
439: lp->l_bp = bp->b_headp->l_bp;
440: bp->b_headp->l_bp = lp;
441: lp->l_fp = bp->b_headp;
1.60 kjell 442: bp->b_lines++;
1.8 art 443:
1.37 db 444: return (TRUE);
1.1 deraadt 445: }
446:
447: /*
1.3 millert 448: * Look through the list of buffers, giving the user a chance to save them.
1.51 kjell 449: * Return TRUE if there are any changed buffers afterwards. Buffers that don't
450: * have an associated file don't count. Return FALSE if there are no changed
451: * buffers. Return ABORT if an error occurs or if the user presses c-g.
1.3 millert 452: */
453: int
1.26 vincent 454: anycb(int f)
1.3 millert 455: {
1.52 deraadt 456: struct buffer *bp;
1.79 lum 457: int s = FALSE, save = FALSE, save2 = FALSE, ret;
1.53 kjell 458: char pbuf[NFILEN + 11];
1.1 deraadt 459:
460: for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
1.21 deraadt 461: if (bp->b_fname != NULL && *(bp->b_fname) != '\0' &&
462: (bp->b_flag & BFCHG) != 0) {
1.53 kjell 463: ret = snprintf(pbuf, sizeof(pbuf), "Save file %s",
1.17 deraadt 464: bp->b_fname);
1.53 kjell 465: if (ret < 0 || ret >= sizeof(pbuf)) {
1.93 ! lum 466: dobeep();
1.51 kjell 467: ewprintf("Error: filename too long!");
1.81 lum 468: return (UERROR);
1.51 kjell 469: }
1.53 kjell 470: if ((f == TRUE || (save = eyorn(pbuf)) == TRUE) &&
1.79 lum 471: (save2 = buffsave(bp)) == TRUE) {
1.1 deraadt 472: bp->b_flag &= ~BFCHG;
473: upmodes(bp);
1.79 lum 474: } else {
475: if (save2 == FIOERR)
476: return (save2);
1.3 millert 477: s = TRUE;
1.79 lum 478: }
1.3 millert 479: if (save == ABORT)
480: return (save);
1.1 deraadt 481: save = TRUE;
482: }
483: }
484: if (save == FALSE /* && kbdmop == NULL */ ) /* experimental */
485: ewprintf("(No files need saving)");
1.37 db 486: return (s);
1.1 deraadt 487: }
488:
489: /*
490: * Search for a buffer, by name.
491: * If not found, and the "cflag" is TRUE,
1.57 kjell 492: * create a new buffer. Return pointer to the found
493: * (or new) buffer.
1.1 deraadt 494: */
1.52 deraadt 495: struct buffer *
1.26 vincent 496: bfind(const char *bname, int cflag)
1.3 millert 497: {
1.52 deraadt 498: struct buffer *bp;
1.1 deraadt 499:
500: bp = bheadp;
501: while (bp != NULL) {
1.7 art 502: if (strcmp(bname, bp->b_bname) == 0)
1.37 db 503: return (bp);
1.1 deraadt 504: bp = bp->b_bufp;
505: }
1.3 millert 506: if (cflag != TRUE)
1.37 db 507: return (NULL);
1.29 vincent 508:
1.65 kjell 509: bp = bnew(bname);
1.57 kjell 510:
511: return (bp);
512: }
513:
514: /*
515: * Create a new buffer and put it in the list of
1.66 deraadt 516: * all buffers.
1.57 kjell 517: */
518: static struct buffer *
1.65 kjell 519: bnew(const char *bname)
1.57 kjell 520: {
1.66 deraadt 521: struct buffer *bp;
1.57 kjell 522: struct line *lp;
523: int i;
1.72 kjell 524: size_t len;
1.57 kjell 525:
1.52 deraadt 526: bp = calloc(1, sizeof(struct buffer));
1.32 vincent 527: if (bp == NULL) {
1.93 ! lum 528: dobeep();
1.52 deraadt 529: ewprintf("Can't get %d bytes", sizeof(struct buffer));
1.37 db 530: return (NULL);
1.1 deraadt 531: }
532: if ((lp = lalloc(0)) == NULL) {
1.29 vincent 533: free(bp);
1.37 db 534: return (NULL);
1.1 deraadt 535: }
1.3 millert 536: bp->b_altb = bp->b_bufp = NULL;
537: bp->b_dotp = lp;
538: bp->b_doto = 0;
1.1 deraadt 539: bp->b_markp = NULL;
540: bp->b_marko = 0;
1.3 millert 541: bp->b_flag = defb_flag;
1.72 kjell 542: /* if buffer name starts and ends with '*', we ignore changes */
543: len = strlen(bname);
544: if (len) {
545: if (bname[0] == '*' && bname[len - 1] == '*')
546: bp->b_flag |= BFIGNDIRTY;
547: }
1.3 millert 548: bp->b_nwnd = 0;
1.62 kjell 549: bp->b_headp = lp;
1.1 deraadt 550: bp->b_nmodes = defb_nmodes;
1.74 oga 551: TAILQ_INIT(&bp->b_undo);
1.46 kjell 552: bp->b_undoptr = NULL;
1.1 deraadt 553: i = 0;
554: do {
1.3 millert 555: bp->b_modes[i] = defb_modes[i];
556: } while (i++ < defb_nmodes);
1.1 deraadt 557: bp->b_fname[0] = '\0';
1.57 kjell 558: bp->b_cwd[0] = '\0';
1.1 deraadt 559: bzero(&bp->b_fi, sizeof(bp->b_fi));
560: lp->l_fp = lp;
561: lp->l_bp = lp;
562: bp->b_bufp = bheadp;
563: bheadp = bp;
1.60 kjell 564: bp->b_dotline = bp->b_markline = 1;
1.61 kjell 565: bp->b_lines = 1;
1.65 kjell 566: if ((bp->b_bname = strdup(bname)) == NULL) {
1.93 ! lum 567: dobeep();
1.65 kjell 568: ewprintf("Can't get %d bytes", strlen(bname) + 1);
569: return (NULL);
570: }
1.57 kjell 571:
1.37 db 572: return (bp);
1.1 deraadt 573: }
574:
575: /*
576: * This routine blows away all of the text
577: * in a buffer. If the buffer is marked as changed
578: * then we ask if it is ok to blow it away; this is
579: * to save the user the grief of losing text. The
580: * window chain is nearly always wrong if this gets
581: * called; the caller must arrange for the updates
582: * that are required. Return TRUE if everything
583: * looks good.
584: */
1.3 millert 585: int
1.52 deraadt 586: bclear(struct buffer *bp)
1.3 millert 587: {
1.52 deraadt 588: struct line *lp;
589: int s;
1.1 deraadt 590:
1.72 kjell 591: /* Has buffer changed, and do we care? */
592: if (!(bp->b_flag & BFIGNDIRTY) && (bp->b_flag & BFCHG) != 0 &&
1.21 deraadt 593: (s = eyesno("Buffer modified; kill anyway")) != TRUE)
1.1 deraadt 594: return (s);
1.3 millert 595: bp->b_flag &= ~BFCHG; /* Not changed */
1.62 kjell 596: while ((lp = lforw(bp->b_headp)) != bp->b_headp)
1.1 deraadt 597: lfree(lp);
1.62 kjell 598: bp->b_dotp = bp->b_headp; /* Fix dot */
1.3 millert 599: bp->b_doto = 0;
600: bp->b_markp = NULL; /* Invalidate "mark" */
1.1 deraadt 601: bp->b_marko = 0;
1.60 kjell 602: bp->b_dotline = bp->b_markline = 1;
1.61 kjell 603: bp->b_lines = 1;
1.60 kjell 604:
1.37 db 605: return (TRUE);
1.1 deraadt 606: }
607:
608: /*
609: * Display the given buffer in the given window. Flags indicated
1.68 kjell 610: * action on redisplay. Update modified flag so insert loop can check it.
1.1 deraadt 611: */
1.3 millert 612: int
1.52 deraadt 613: showbuffer(struct buffer *bp, struct mgwin *wp, int flags)
1.3 millert 614: {
1.52 deraadt 615: struct buffer *obp;
616: struct mgwin *owp;
1.1 deraadt 617:
1.68 kjell 618: /* Ensure file has not been modified elsewhere */
619: if (fchecktime(bp) != TRUE)
620: bp->b_flag |= BFDIRTY;
621:
1.32 vincent 622: if (wp->w_bufp == bp) { /* Easy case! */
1.70 kjell 623: wp->w_rflag |= flags;
1.37 db 624: return (TRUE);
1.1 deraadt 625: }
1.37 db 626: /* First, detach the old buffer from the window */
1.1 deraadt 627: if ((bp->b_altb = obp = wp->w_bufp) != NULL) {
628: if (--obp->b_nwnd == 0) {
1.3 millert 629: obp->b_dotp = wp->w_dotp;
630: obp->b_doto = wp->w_doto;
1.1 deraadt 631: obp->b_markp = wp->w_markp;
632: obp->b_marko = wp->w_marko;
1.60 kjell 633: obp->b_dotline = wp->w_dotline;
634: obp->b_markline = wp->w_markline;
1.1 deraadt 635: }
636: }
637: /* Now, attach the new buffer to the window */
638: wp->w_bufp = bp;
639:
1.3 millert 640: if (bp->b_nwnd++ == 0) { /* First use. */
641: wp->w_dotp = bp->b_dotp;
642: wp->w_doto = bp->b_doto;
1.1 deraadt 643: wp->w_markp = bp->b_markp;
644: wp->w_marko = bp->b_marko;
1.60 kjell 645: wp->w_dotline = bp->b_dotline;
646: wp->w_markline = bp->b_markline;
1.1 deraadt 647: } else
1.3 millert 648: /* already on screen, steal values from other window */
1.1 deraadt 649: for (owp = wheadp; owp != NULL; owp = wp->w_wndp)
650: if (wp->w_bufp == bp && owp != wp) {
1.3 millert 651: wp->w_dotp = owp->w_dotp;
652: wp->w_doto = owp->w_doto;
1.1 deraadt 653: wp->w_markp = owp->w_markp;
654: wp->w_marko = owp->w_marko;
1.60 kjell 655: wp->w_dotline = owp->w_dotline;
1.64 kjell 656: wp->w_markline = owp->w_markline;
1.1 deraadt 657: break;
658: }
1.70 kjell 659: wp->w_rflag |= WFMODE | flags;
1.56 kjell 660: return (TRUE);
661: }
662:
663: /*
664: * Augment a buffer name with a number, if necessary
665: *
666: * If more than one file of the same basename() is open,
667: * the additional buffers are named "file<2>", "file<3>", and
668: * so forth. This function adjusts a buffer name to
669: * include the number, if necessary.
670: */
671: int
1.57 kjell 672: augbname(char *bn, const char *fn, size_t bs)
1.56 kjell 673: {
1.66 deraadt 674: int count;
1.56 kjell 675: size_t remain, len;
676:
1.77 kjell 677: if ((len = xbasename(bn, fn, bs)) >= bs)
1.56 kjell 678: return (FALSE);
679:
680: remain = bs - len;
681: for (count = 2; bfind(bn, FALSE) != NULL; count++)
682: snprintf(bn + len, remain, "<%d>", count);
683:
1.37 db 684: return (TRUE);
1.1 deraadt 685: }
686:
687: /*
688: * Pop the buffer we got passed onto the screen.
689: * Returns a status.
690: */
1.52 deraadt 691: struct mgwin *
1.71 kjell 692: popbuf(struct buffer *bp, int flags)
1.3 millert 693: {
1.52 deraadt 694: struct mgwin *wp;
1.1 deraadt 695:
1.3 millert 696: if (bp->b_nwnd == 0) { /* Not on screen yet. */
1.71 kjell 697: /*
698: * Pick a window for a pop-up.
699: * If only one window, split the screen.
700: * Flag the new window as ephemeral
701: */
702: if (wheadp->w_wndp == NULL &&
703: splitwind(FFOTHARG, flags) == FALSE)
704: return (NULL);
705:
706: /*
707: * Pick the uppermost window that isn't
708: * the current window. An LRU algorithm
709: * might be better. Return a pointer, or NULL on error.
710: */
711: wp = wheadp;
712:
713: while (wp != NULL && wp == curwp)
714: wp = wp->w_wndp;
1.1 deraadt 715: } else
716: for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
717: if (wp->w_bufp == bp) {
1.70 kjell 718: wp->w_rflag |= WFFULL | WFFRAME;
1.37 db 719: return (wp);
1.1 deraadt 720: }
1.58 kjell 721: if (showbuffer(bp, wp, WFFULL) != TRUE)
1.37 db 722: return (NULL);
723: return (wp);
1.1 deraadt 724: }
725:
726: /*
727: * Insert another buffer at dot. Very useful.
728: */
1.3 millert 729: /* ARGSUSED */
730: int
1.26 vincent 731: bufferinsert(int f, int n)
1.1 deraadt 732: {
1.52 deraadt 733: struct buffer *bp;
734: struct line *clp;
1.36 deraadt 735: int clo, nline;
736: char bufn[NBUFN], *bufp;
1.1 deraadt 737:
738: /* Get buffer to use from user */
739: if (curbp->b_altb != NULL)
1.34 vincent 740: bufp = eread("Insert buffer: (default %s) ", bufn, NBUFN,
1.40 kjell 741: EFNUL | EFNEW | EFBUF, curbp->b_altb->b_bname);
1.1 deraadt 742: else
1.38 cloder 743: bufp = eread("Insert buffer: ", bufn, NBUFN, EFNEW | EFBUF);
1.34 vincent 744: if (bufp == NULL)
1.37 db 745: return (ABORT);
1.34 vincent 746: if (bufp[0] == '\0' && curbp->b_altb != NULL)
1.3 millert 747: bp = curbp->b_altb;
748: else if ((bp = bfind(bufn, FALSE)) == NULL)
1.37 db 749: return (FALSE);
1.1 deraadt 750:
1.3 millert 751: if (bp == curbp) {
1.93 ! lum 752: dobeep();
1.1 deraadt 753: ewprintf("Cannot insert buffer into self");
1.37 db 754: return (FALSE);
1.1 deraadt 755: }
756: /* insert the buffer */
757: nline = 0;
1.63 kjell 758: clp = bfirstlp(bp);
1.3 millert 759: for (;;) {
1.1 deraadt 760: for (clo = 0; clo < llength(clp); clo++)
761: if (linsert(1, lgetc(clp, clo)) == FALSE)
1.37 db 762: return (FALSE);
1.62 kjell 763: if ((clp = lforw(clp)) == bp->b_headp)
1.3 millert 764: break;
765: if (newline(FFRAND, 1) == FALSE) /* fake newline */
1.37 db 766: return (FALSE);
1.1 deraadt 767: nline++;
768: }
1.3 millert 769: if (nline == 1)
770: ewprintf("[Inserted 1 line]");
771: else
772: ewprintf("[Inserted %d lines]", nline);
1.1 deraadt 773:
1.37 db 774: clp = curwp->w_linep; /* cosmetic adjustment */
1.3 millert 775: if (curwp->w_dotp == clp) { /* for offscreen insert */
1.62 kjell 776: while (nline-- && lback(clp) != curbp->b_headp)
1.1 deraadt 777: clp = lback(clp);
1.37 db 778: curwp->w_linep = clp; /* adjust framing. */
1.70 kjell 779: curwp->w_rflag |= WFFULL;
1.1 deraadt 780: }
781: return (TRUE);
782: }
783:
784: /*
785: * Turn off the dirty bit on this buffer.
786: */
1.3 millert 787: /* ARGSUSED */
788: int
1.26 vincent 789: notmodified(int f, int n)
1.1 deraadt 790: {
1.52 deraadt 791: struct mgwin *wp;
1.1 deraadt 792:
793: curbp->b_flag &= ~BFCHG;
1.3 millert 794: wp = wheadp; /* Update mode lines. */
1.1 deraadt 795: while (wp != NULL) {
796: if (wp->w_bufp == curbp)
1.70 kjell 797: wp->w_rflag |= WFMODE;
1.1 deraadt 798: wp = wp->w_wndp;
799: }
800: ewprintf("Modification-flag cleared");
1.37 db 801: return (TRUE);
1.1 deraadt 802: }
803:
804: /*
1.78 lum 805: * Popbuf and set all windows to top of buffer.
1.1 deraadt 806: */
1.3 millert 807: int
1.71 kjell 808: popbuftop(struct buffer *bp, int flags)
1.1 deraadt 809: {
1.52 deraadt 810: struct mgwin *wp;
1.1 deraadt 811:
1.63 kjell 812: bp->b_dotp = bfirstlp(bp);
1.3 millert 813: bp->b_doto = 0;
814: if (bp->b_nwnd != 0) {
815: for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
816: if (wp->w_bufp == bp) {
817: wp->w_dotp = bp->b_dotp;
818: wp->w_doto = 0;
1.70 kjell 819: wp->w_rflag |= WFFULL;
1.3 millert 820: }
821: }
1.71 kjell 822: return (popbuf(bp, flags) != NULL);
1.1 deraadt 823: }
1.57 kjell 824:
825: /*
826: * Return the working directory for the current buffer, terminated
827: * with a '/'. First, try to extract it from the current buffer's
828: * filename. If that fails, use global cwd.
829: */
830: int
831: getbufcwd(char *path, size_t plen)
832: {
833: char cwd[NFILEN];
834:
835: if (plen == 0)
836: return (FALSE);
837:
1.67 kjell 838: if (globalwd == FALSE && curbp->b_cwd[0] != '\0') {
1.57 kjell 839: (void)strlcpy(path, curbp->b_cwd, plen);
840: } else {
841: if (getcwdir(cwd, sizeof(cwd)) == FALSE)
842: goto error;
843: (void)strlcpy(path, cwd, plen);
844: }
845: return (TRUE);
846: error:
847: path[0] = '\0';
848: return (FALSE);
849: }
850:
1.68 kjell 851: /*
1.69 kjell 852: * Ensures a buffer has not been modified elsewhere; e.g. on disk.
853: * Prompt the user if it has.
854: * Returns TRUE if it has NOT (i.e. buffer is ok to edit).
855: * FALSE or ABORT otherwise
1.68 kjell 856: */
857: int
858: checkdirty(struct buffer *bp)
859: {
860: int s;
1.83 florian 861:
862: if ((bp->b_flag & (BFCHG | BFDIRTY)) == 0)
863: if (fchecktime(bp) != TRUE)
864: bp->b_flag |= BFDIRTY;
865:
1.68 kjell 866: if ((bp->b_flag & (BFDIRTY | BFIGNDIRTY)) == BFDIRTY) {
1.84 florian 867: s = eynorr("File changed on disk; really edit the buffer");
868: switch (s) {
869: case TRUE:
870: bp->b_flag &= ~BFDIRTY;
871: bp->b_flag |= BFIGNDIRTY;
872: return (TRUE);
873: case REVERT:
874: dorevert();
875: return (FALSE);
876: default:
1.68 kjell 877: return (s);
1.84 florian 878: }
1.68 kjell 879: }
880:
881: return (TRUE);
882: }
1.82 jasper 883:
884: /*
885: * Revert the current buffer to whatever is on disk.
886: */
887: /* ARGSUSED */
888: int
889: revertbuffer(int f, int n)
890: {
891: char fbuf[NFILEN + 32];
892:
1.85 florian 893: if (curbp->b_fname[0] == 0) {
1.93 ! lum 894: dobeep();
1.82 jasper 895: ewprintf("Cannot revert buffer not associated with any files.");
896: return (FALSE);
897: }
898:
1.85 florian 899: snprintf(fbuf, sizeof(fbuf), "Revert buffer from file %s",
900: curbp->b_fname);
1.82 jasper 901:
1.85 florian 902: if (eyorn(fbuf) == TRUE)
1.84 florian 903: return dorevert();
1.82 jasper 904:
1.84 florian 905: return (FALSE);
906: }
1.82 jasper 907:
1.84 florian 908: int
1.86 haesbaer 909: dorevert(void)
1.84 florian 910: {
911: int lineno;
1.89 florian 912: struct undo_rec *rec;
1.82 jasper 913:
1.85 florian 914: if (access(curbp->b_fname, F_OK|R_OK) != 0) {
1.93 ! lum 915: dobeep();
1.84 florian 916: if (errno == ENOENT)
917: ewprintf("File %s no longer exists!",
1.85 florian 918: curbp->b_fname);
1.84 florian 919: else
920: ewprintf("File %s is no longer readable!",
1.85 florian 921: curbp->b_fname);
1.84 florian 922: return (FALSE);
1.82 jasper 923: }
924:
1.84 florian 925: /* Save our current line, so we can go back after reloading. */
1.85 florian 926: lineno = curwp->w_dotline;
1.84 florian 927:
928: /* Prevent readin from asking if we want to kill the buffer. */
929: curbp->b_flag &= ~BFCHG;
1.89 florian 930:
931: /* Clean up undo memory */
932: while ((rec = TAILQ_FIRST(&curbp->b_undo))) {
933: TAILQ_REMOVE(&curbp->b_undo, rec, next);
934: free_undo_record(rec);
935: }
1.84 florian 936:
1.85 florian 937: if (readin(curbp->b_fname))
1.84 florian 938: return(setlineno(lineno));
1.82 jasper 939: return (FALSE);
1.88 florian 940: }
941:
942: /*
943: * Diff the current buffer to what is on disk.
944: */
945: /*ARGSUSED */
946: int
947: diffbuffer(int f, int n)
948: {
949: struct buffer *bp;
950: struct line *lp, *lpend;
951: size_t len;
952: int ret;
953: char *text, *ttext;
954: char * const argv[] =
955: {DIFFTOOL, "-u", "-p", curbp->b_fname, "-", (char *)NULL};
956:
957: len = 0;
958:
959: /* C-u is not supported */
960: if (n > 1)
961: return (ABORT);
962:
963: if (access(DIFFTOOL, X_OK) != 0) {
1.93 ! lum 964: dobeep();
1.88 florian 965: ewprintf("%s not found or not executable.", DIFFTOOL);
966: return (FALSE);
967: }
968:
969: if (curbp->b_fname[0] == 0) {
1.93 ! lum 970: dobeep();
1.88 florian 971: ewprintf("Cannot diff buffer not associated with any files.");
972: return (FALSE);
973: }
974:
975: lpend = curbp->b_headp;
976: for (lp = lforw(lpend); lp != lpend; lp = lforw(lp)) {
977: len+=llength(lp);
978: if (lforw(lp) != lpend) /* no implied \n on last line */
979: len++;
980: }
981: if ((text = calloc(len + 1, sizeof(char))) == NULL) {
1.93 ! lum 982: dobeep();
1.88 florian 983: ewprintf("Cannot allocate memory.");
984: return (FALSE);
985: }
986: ttext = text;
987:
988: for (lp = lforw(lpend); lp != lpend; lp = lforw(lp)) {
989: if (llength(lp) != 0) {
990: memcpy(ttext, ltext(lp), llength(lp));
991: ttext += llength(lp);
992: }
993: if (lforw(lp) != lpend) /* no implied \n on last line */
994: *ttext++ = '\n';
995: }
996:
997: bp = bfind("*Diff*", TRUE);
998: bp->b_flag |= BFREADONLY;
999: if (bclear(bp) != TRUE) {
1000: free(text);
1001: return (FALSE);
1002: }
1003:
1004: ret = pipeio(DIFFTOOL, argv, text, len, bp);
1005:
1006: if (ret == TRUE) {
1007: eerase();
1008: if (lforw(bp->b_headp) == bp->b_headp)
1009: addline(bp, "Diff finished (no differences).");
1010: }
1011:
1012: free(text);
1013: return (ret);
1.92 lum 1014: }
1015:
1016: /*
1017: * Given a file name, either find the buffer it uses, or create a new
1018: * empty buffer to put it in.
1019: */
1020: struct buffer *
1021: findbuffer(char *fn)
1022: {
1023: struct buffer *bp;
1024: char bname[NBUFN], fname[NBUFN];
1025:
1026: if (strlcpy(fname, fn, sizeof(fname)) >= sizeof(fname)) {
1.93 ! lum 1027: dobeep();
1.92 lum 1028: ewprintf("filename too long");
1029: return (NULL);
1030: }
1031:
1032: for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
1033: if (strcmp(bp->b_fname, fname) == 0)
1034: return (bp);
1035: }
1036: /* Not found. Create a new one, adjusting name first */
1037: if (augbname(bname, fname, sizeof(bname)) == FALSE)
1038: return (NULL);
1039:
1040: bp = bfind(bname, TRUE);
1041: return (bp);
1.82 jasper 1042: }