Annotation of src/usr.bin/mg/buffer.c, Revision 1.59
1.59 ! kjell 1: /* $OpenBSD: buffer.c,v 1.58 2006/05/28 23:30:16 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.57 kjell 16: static struct buffer *bnew(void);
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;
165: free(bp->b_linep); /* Release header line. */
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) {
302: lp = lforw(bp->b_linep);
303: while (lp != bp->b_linep) {
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.3 millert 324: blp->b_dotp = lforw(blp->b_linep); /* put dot at beginning of
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.3 millert 413: bp->b_linep->l_bp->l_fp = lp; /* Hook onto the end */
1.1 deraadt 414: lp->l_bp = bp->b_linep->l_bp;
415: bp->b_linep->l_bp = lp;
416: lp->l_fp = bp->b_linep;
1.8 art 417:
1.37 db 418: return (TRUE);
1.1 deraadt 419: }
420:
421: /*
1.3 millert 422: * Look through the list of buffers, giving the user a chance to save them.
1.51 kjell 423: * Return TRUE if there are any changed buffers afterwards. Buffers that don't
424: * have an associated file don't count. Return FALSE if there are no changed
425: * buffers. Return ABORT if an error occurs or if the user presses c-g.
1.3 millert 426: */
427: int
1.26 vincent 428: anycb(int f)
1.3 millert 429: {
1.52 deraadt 430: struct buffer *bp;
431: int s = FALSE, save = FALSE, ret;
1.53 kjell 432: char pbuf[NFILEN + 11];
1.1 deraadt 433:
434: for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
1.21 deraadt 435: if (bp->b_fname != NULL && *(bp->b_fname) != '\0' &&
436: (bp->b_flag & BFCHG) != 0) {
1.53 kjell 437: ret = snprintf(pbuf, sizeof(pbuf), "Save file %s",
1.17 deraadt 438: bp->b_fname);
1.53 kjell 439: if (ret < 0 || ret >= sizeof(pbuf)) {
1.51 kjell 440: ewprintf("Error: filename too long!");
441: return (ABORT);
442: }
1.53 kjell 443: if ((f == TRUE || (save = eyorn(pbuf)) == TRUE) &&
1.21 deraadt 444: buffsave(bp) == TRUE) {
1.1 deraadt 445: bp->b_flag &= ~BFCHG;
446: upmodes(bp);
1.3 millert 447: } else
448: s = TRUE;
449: if (save == ABORT)
450: return (save);
1.1 deraadt 451: save = TRUE;
452: }
453: }
454: if (save == FALSE /* && kbdmop == NULL */ ) /* experimental */
455: ewprintf("(No files need saving)");
1.37 db 456: return (s);
1.1 deraadt 457: }
458:
459: /*
460: * Search for a buffer, by name.
461: * If not found, and the "cflag" is TRUE,
1.57 kjell 462: * create a new buffer. Return pointer to the found
463: * (or new) buffer.
1.1 deraadt 464: */
1.52 deraadt 465: struct buffer *
1.26 vincent 466: bfind(const char *bname, int cflag)
1.3 millert 467: {
1.52 deraadt 468: struct buffer *bp;
1.1 deraadt 469:
470: bp = bheadp;
471: while (bp != NULL) {
1.7 art 472: if (strcmp(bname, bp->b_bname) == 0)
1.37 db 473: return (bp);
1.1 deraadt 474: bp = bp->b_bufp;
475: }
1.3 millert 476: if (cflag != TRUE)
1.37 db 477: return (NULL);
1.29 vincent 478:
1.57 kjell 479: bp = bnew();
480:
481: if ((bp->b_bname = strdup(bname)) == NULL) {
482: ewprintf("Can't get %d bytes", strlen(bname) + 1);
483: free(bp);
484: return (NULL);
485: }
486:
487: return (bp);
488: }
489:
490: /*
491: * Create a new buffer and put it in the list of
492: * all buffers.
493: */
494: static struct buffer *
495: bnew()
496: {
497: struct buffer *bp;
498: struct line *lp;
499: int i;
500:
1.52 deraadt 501: bp = calloc(1, sizeof(struct buffer));
1.32 vincent 502: if (bp == NULL) {
1.52 deraadt 503: ewprintf("Can't get %d bytes", sizeof(struct buffer));
1.37 db 504: return (NULL);
1.1 deraadt 505: }
506: if ((lp = lalloc(0)) == NULL) {
1.29 vincent 507: free(bp);
1.37 db 508: return (NULL);
1.1 deraadt 509: }
1.3 millert 510: bp->b_altb = bp->b_bufp = NULL;
511: bp->b_dotp = lp;
512: bp->b_doto = 0;
1.1 deraadt 513: bp->b_markp = NULL;
514: bp->b_marko = 0;
1.3 millert 515: bp->b_flag = defb_flag;
516: bp->b_nwnd = 0;
1.1 deraadt 517: bp->b_linep = lp;
518: bp->b_nmodes = defb_nmodes;
1.46 kjell 519: LIST_INIT(&bp->b_undo);
520: bp->b_undoptr = NULL;
521: memset(&bp->b_undopos, 0, sizeof(bp->b_undopos));
1.1 deraadt 522: i = 0;
523: do {
1.3 millert 524: bp->b_modes[i] = defb_modes[i];
525: } while (i++ < defb_nmodes);
1.1 deraadt 526: bp->b_fname[0] = '\0';
1.57 kjell 527: bp->b_cwd[0] = '\0';
1.1 deraadt 528: bzero(&bp->b_fi, sizeof(bp->b_fi));
529: lp->l_fp = lp;
530: lp->l_bp = lp;
531: bp->b_bufp = bheadp;
532: bheadp = bp;
1.57 kjell 533:
1.37 db 534: return (bp);
1.1 deraadt 535: }
536:
537: /*
538: * This routine blows away all of the text
539: * in a buffer. If the buffer is marked as changed
540: * then we ask if it is ok to blow it away; this is
541: * to save the user the grief of losing text. The
542: * window chain is nearly always wrong if this gets
543: * called; the caller must arrange for the updates
544: * that are required. Return TRUE if everything
545: * looks good.
546: */
1.3 millert 547: int
1.52 deraadt 548: bclear(struct buffer *bp)
1.3 millert 549: {
1.52 deraadt 550: struct line *lp;
551: int s;
1.1 deraadt 552:
1.37 db 553: if ((bp->b_flag & BFCHG) != 0 && /* Changed. */
1.21 deraadt 554: (s = eyesno("Buffer modified; kill anyway")) != TRUE)
1.1 deraadt 555: return (s);
1.3 millert 556: bp->b_flag &= ~BFCHG; /* Not changed */
557: while ((lp = lforw(bp->b_linep)) != bp->b_linep)
1.1 deraadt 558: lfree(lp);
1.32 vincent 559: bp->b_dotp = bp->b_linep; /* Fix dot */
1.3 millert 560: bp->b_doto = 0;
561: bp->b_markp = NULL; /* Invalidate "mark" */
1.1 deraadt 562: bp->b_marko = 0;
1.37 db 563: return (TRUE);
1.1 deraadt 564: }
565:
566: /*
567: * Display the given buffer in the given window. Flags indicated
568: * action on redisplay.
569: */
1.3 millert 570: int
1.52 deraadt 571: showbuffer(struct buffer *bp, struct mgwin *wp, int flags)
1.3 millert 572: {
1.52 deraadt 573: struct buffer *obp;
574: struct mgwin *owp;
1.1 deraadt 575:
1.32 vincent 576: if (wp->w_bufp == bp) { /* Easy case! */
1.1 deraadt 577: wp->w_flag |= flags;
1.32 vincent 578: wp->w_dotp = bp->b_dotp;
579: wp->w_doto = bp->b_doto;
1.37 db 580: return (TRUE);
1.1 deraadt 581: }
1.37 db 582: /* First, detach the old buffer from the window */
1.1 deraadt 583: if ((bp->b_altb = obp = wp->w_bufp) != NULL) {
584: if (--obp->b_nwnd == 0) {
1.3 millert 585: obp->b_dotp = wp->w_dotp;
586: obp->b_doto = wp->w_doto;
1.1 deraadt 587: obp->b_markp = wp->w_markp;
588: obp->b_marko = wp->w_marko;
589: }
590: }
591: /* Now, attach the new buffer to the window */
592: wp->w_bufp = bp;
593:
1.3 millert 594: if (bp->b_nwnd++ == 0) { /* First use. */
595: wp->w_dotp = bp->b_dotp;
596: wp->w_doto = bp->b_doto;
1.1 deraadt 597: wp->w_markp = bp->b_markp;
598: wp->w_marko = bp->b_marko;
599: } else
1.3 millert 600: /* already on screen, steal values from other window */
1.1 deraadt 601: for (owp = wheadp; owp != NULL; owp = wp->w_wndp)
602: if (wp->w_bufp == bp && owp != wp) {
1.3 millert 603: wp->w_dotp = owp->w_dotp;
604: wp->w_doto = owp->w_doto;
1.1 deraadt 605: wp->w_markp = owp->w_markp;
606: wp->w_marko = owp->w_marko;
607: break;
608: }
1.3 millert 609: wp->w_flag |= WFMODE | flags;
1.56 kjell 610: return (TRUE);
611: }
612:
613: /*
614: * Augment a buffer name with a number, if necessary
615: *
616: * If more than one file of the same basename() is open,
617: * the additional buffers are named "file<2>", "file<3>", and
618: * so forth. This function adjusts a buffer name to
619: * include the number, if necessary.
620: */
621: int
1.57 kjell 622: augbname(char *bn, const char *fn, size_t bs)
1.56 kjell 623: {
624: int count;
625: size_t remain, len;
626:
627: len = strlcpy(bn, basename(fn), bs);
628: if (len >= bs)
629: return (FALSE);
630:
631: remain = bs - len;
632: for (count = 2; bfind(bn, FALSE) != NULL; count++)
633: snprintf(bn + len, remain, "<%d>", count);
634:
1.37 db 635: return (TRUE);
1.1 deraadt 636: }
637:
638: /*
639: * Pop the buffer we got passed onto the screen.
640: * Returns a status.
641: */
1.52 deraadt 642: struct mgwin *
643: popbuf(struct buffer *bp)
1.3 millert 644: {
1.52 deraadt 645: struct mgwin *wp;
1.1 deraadt 646:
1.3 millert 647: if (bp->b_nwnd == 0) { /* Not on screen yet. */
648: if ((wp = wpopup()) == NULL)
1.37 db 649: return (NULL);
1.1 deraadt 650: } else
651: for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
652: if (wp->w_bufp == bp) {
1.58 kjell 653: wp->w_flag |= WFFULL | WFFRAME;
1.37 db 654: return (wp);
1.1 deraadt 655: }
1.58 kjell 656: if (showbuffer(bp, wp, WFFULL) != TRUE)
1.37 db 657: return (NULL);
658: return (wp);
1.1 deraadt 659: }
660:
661: /*
662: * Insert another buffer at dot. Very useful.
663: */
1.3 millert 664: /* ARGSUSED */
665: int
1.26 vincent 666: bufferinsert(int f, int n)
1.1 deraadt 667: {
1.52 deraadt 668: struct buffer *bp;
669: struct line *clp;
1.36 deraadt 670: int clo, nline;
671: char bufn[NBUFN], *bufp;
1.1 deraadt 672:
673: /* Get buffer to use from user */
674: if (curbp->b_altb != NULL)
1.34 vincent 675: bufp = eread("Insert buffer: (default %s) ", bufn, NBUFN,
1.40 kjell 676: EFNUL | EFNEW | EFBUF, curbp->b_altb->b_bname);
1.1 deraadt 677: else
1.38 cloder 678: bufp = eread("Insert buffer: ", bufn, NBUFN, EFNEW | EFBUF);
1.34 vincent 679: if (bufp == NULL)
1.37 db 680: return (ABORT);
1.34 vincent 681: if (bufp[0] == '\0' && curbp->b_altb != NULL)
1.3 millert 682: bp = curbp->b_altb;
683: else if ((bp = bfind(bufn, FALSE)) == NULL)
1.37 db 684: return (FALSE);
1.1 deraadt 685:
1.3 millert 686: if (bp == curbp) {
1.1 deraadt 687: ewprintf("Cannot insert buffer into self");
1.37 db 688: return (FALSE);
1.1 deraadt 689: }
690: /* insert the buffer */
691: nline = 0;
692: clp = lforw(bp->b_linep);
1.3 millert 693: for (;;) {
1.1 deraadt 694: for (clo = 0; clo < llength(clp); clo++)
695: if (linsert(1, lgetc(clp, clo)) == FALSE)
1.37 db 696: return (FALSE);
1.3 millert 697: if ((clp = lforw(clp)) == bp->b_linep)
698: break;
699: if (newline(FFRAND, 1) == FALSE) /* fake newline */
1.37 db 700: return (FALSE);
1.1 deraadt 701: nline++;
702: }
1.3 millert 703: if (nline == 1)
704: ewprintf("[Inserted 1 line]");
705: else
706: ewprintf("[Inserted %d lines]", nline);
1.1 deraadt 707:
1.37 db 708: clp = curwp->w_linep; /* cosmetic adjustment */
1.3 millert 709: if (curwp->w_dotp == clp) { /* for offscreen insert */
710: while (nline-- && lback(clp) != curbp->b_linep)
1.1 deraadt 711: clp = lback(clp);
1.37 db 712: curwp->w_linep = clp; /* adjust framing. */
1.58 kjell 713: curwp->w_flag |= WFFULL;
1.1 deraadt 714: }
715: return (TRUE);
716: }
717:
718: /*
719: * Turn off the dirty bit on this buffer.
720: */
1.3 millert 721: /* ARGSUSED */
722: int
1.26 vincent 723: notmodified(int f, int n)
1.1 deraadt 724: {
1.52 deraadt 725: struct mgwin *wp;
1.1 deraadt 726:
727: curbp->b_flag &= ~BFCHG;
1.3 millert 728: wp = wheadp; /* Update mode lines. */
1.1 deraadt 729: while (wp != NULL) {
730: if (wp->w_bufp == curbp)
731: wp->w_flag |= WFMODE;
732: wp = wp->w_wndp;
733: }
734: ewprintf("Modification-flag cleared");
1.37 db 735: return (TRUE);
1.1 deraadt 736: }
737:
738: #ifndef NO_HELP
739: /*
740: * Popbuf and set all windows to top of buffer. Currently only used by
741: * help functions.
742: */
1.3 millert 743: int
1.52 deraadt 744: popbuftop(struct buffer *bp)
1.1 deraadt 745: {
1.52 deraadt 746: struct mgwin *wp;
1.1 deraadt 747:
1.3 millert 748: bp->b_dotp = lforw(bp->b_linep);
749: bp->b_doto = 0;
750: if (bp->b_nwnd != 0) {
751: for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
752: if (wp->w_bufp == bp) {
753: wp->w_dotp = bp->b_dotp;
754: wp->w_doto = 0;
1.58 kjell 755: wp->w_flag |= WFFULL;
1.3 millert 756: }
757: }
1.37 db 758: return (popbuf(bp) != NULL);
1.1 deraadt 759: }
760: #endif
1.57 kjell 761:
762: /*
763: * Return the working directory for the current buffer, terminated
764: * with a '/'. First, try to extract it from the current buffer's
765: * filename. If that fails, use global cwd.
766: */
767: int
768: getbufcwd(char *path, size_t plen)
769: {
770: char cwd[NFILEN];
771:
772: if (plen == 0)
773: return (FALSE);
774:
775: if (curbp->b_cwd[0] != '\0') {
776: (void)strlcpy(path, curbp->b_cwd, plen);
777: } else {
778: if (getcwdir(cwd, sizeof(cwd)) == FALSE)
779: goto error;
780: (void)strlcpy(path, cwd, plen);
781: }
782: return (TRUE);
783: error:
784: path[0] = '\0';
785: return (FALSE);
786: }
787: