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