1 /* $OpenBSD: buffer.c,v 1.78 2012/03/14 13:56:35 lum Exp $ */
3 /* This file is in the public domain. */
10 #include "kbd.h" /* needed for modes */
15 static struct buffer *makelist(void);
16 static struct buffer *bnew(const char *);
18 static int usebufname(const char *);
20 /* Flag for global working dir */
25 togglereadonly(int f, int n)
29 if ((s = checkdirty(curbp)) != TRUE)
31 if (!(curbp->b_flag & BFREADONLY))
32 curbp->b_flag |= BFREADONLY;
34 curbp->b_flag &= ~BFREADONLY;
35 if (curbp->b_flag & BFCHG)
36 ewprintf("Warning: Buffer was modified");
38 curwp->w_rflag |= WFMODE;
43 /* Switch to the named buffer.
44 * If no name supplied, switch to the default (alternate) buffer.
47 usebufname(const char *bufp)
53 if (bufp[0] == '\0' && curbp->b_altb != NULL)
55 else if ((bp = bfind(bufp, TRUE)) == NULL)
58 /* and put it in current window */
60 return (showbuffer(bp, curwp, WFFRAME | WFFULL));
64 * Attach a buffer to a window. The values of dot and mark come
65 * from the buffer if the use count is 0. Otherwise, they come
66 * from some other window. *scratch* is the default alternate
71 usebuffer(int f, int n)
73 char bufn[NBUFN], *bufp;
75 /* Get buffer to use from user */
76 if ((curbp->b_altb == NULL) &&
77 ((curbp->b_altb = bfind("*scratch*", TRUE)) == NULL))
78 bufp = eread("Switch to buffer: ", bufn, NBUFN, EFNEW | EFBUF);
80 bufp = eread("Switch to buffer: (default %s) ", bufn, NBUFN,
81 EFNUL | EFNEW | EFBUF, curbp->b_altb->b_bname);
83 return (usebufname(bufp));
87 * pop to buffer asked for by the user.
91 poptobuffer(int f, int n)
95 char bufn[NBUFN], *bufp;
97 /* Get buffer to use from user */
98 if ((curbp->b_altb == NULL) &&
99 ((curbp->b_altb = bfind("*scratch*", TRUE)) == NULL))
100 bufp = eread("Switch to buffer in other window: ", bufn, NBUFN,
103 bufp = eread("Switch to buffer in other window: (default %s) ",
104 bufn, NBUFN, EFNUL | EFNEW | EFBUF, curbp->b_altb->b_bname);
107 if (bufp[0] == '\0' && curbp->b_altb != NULL)
109 else if ((bp = bfind(bufn, TRUE)) == NULL)
112 return (splitwind(f, n));
113 /* and put it in a new, non-ephemeral window */
114 if ((wp = popbuf(bp, WNONE)) == NULL)
122 * Dispose of a buffer, by name.
123 * Ask for the name. Look it up (don't get too
124 * upset if it isn't there at all!). Clear the buffer (ask
125 * if the buffer has been changed). Then free the header
126 * line and the buffer header. Bound to "C-X k".
130 killbuffer_cmd(int f, int n)
133 char bufn[NBUFN], *bufp;
135 if ((bufp = eread("Kill buffer: (default %s) ", bufn, NBUFN,
136 EFNUL | EFNEW | EFBUF, curbp->b_bname)) == NULL)
138 else if (bufp[0] == '\0')
140 else if ((bp = bfind(bufn, FALSE)) == NULL)
142 return (killbuffer(bp));
146 killbuffer(struct buffer *bp)
152 struct undo_rec *rec, *next;
155 * Find some other buffer to display. Try the alternate buffer,
156 * then the first different buffer in the buffer list. If there's
157 * only one buffer, create buffer *scratch* and make it the alternate
158 * buffer. Return if *scratch* is only buffer...
160 if ((bp1 = bp->b_altb) == NULL) {
161 bp1 = (bp == bheadp) ? bp->b_bufp : bheadp;
163 /* only one buffer. see if it's *scratch* */
164 if (bp == bfind("*scratch*", FALSE))
166 /* create *scratch* for alternate buffer */
167 if ((bp1 = bfind("*scratch*", TRUE)) == NULL)
171 if ((s = bclear(bp)) != TRUE)
173 for (wp = wheadp; bp->b_nwnd > 0; wp = wp->w_wndp) {
174 if (wp->w_bufp == bp) {
175 bp2 = bp1->b_altb; /* save alternate buffer */
176 if (showbuffer(bp1, wp, WFMODE | WFFRAME | WFFULL))
184 free(bp->b_headp); /* Release header line. */
185 bp2 = NULL; /* Find the header. */
188 if (bp1->b_altb == bp)
189 bp1->b_altb = (bp->b_altb == bp1) ? NULL : bp->b_altb;
193 bp1 = bp1->b_bufp; /* Next one in chain. */
194 if (bp2 == NULL) /* Unlink it. */
198 while (bp1 != NULL) { /* Finish with altb's */
199 if (bp1->b_altb == bp)
200 bp1->b_altb = (bp->b_altb == bp1) ? NULL : bp->b_altb;
203 rec = TAILQ_FIRST(&bp->b_undo);
205 while (rec != NULL) {
206 next = TAILQ_NEXT(rec, next);
207 free_undo_record(rec);
211 free(bp->b_bname); /* Release name block */
212 free(bp); /* Release buffer block */
217 * Save some buffers - just call anycb with the arg flag.
221 savebuffers(int f, int n)
223 if (anycb(f) == ABORT)
231 static int listbuf_ncol;
233 static int listbuf_goto_buffer(int f, int n);
234 static int listbuf_goto_buffer_one(int f, int n);
235 static int listbuf_goto_buffer_helper(int f, int n, int only);
237 static PF listbuf_pf[] = {
240 static PF listbuf_one[] = {
241 listbuf_goto_buffer_one
245 static struct KEYMAPE (2 + IMAPEXT) listbufmap = {
251 CCHR('M'), CCHR('M'), listbuf_pf, NULL
254 '1', '1', listbuf_one, NULL
260 * Display the buffer list. This is done
261 * in two parts. The "makelist" routine figures out
262 * the text, and puts it in a buffer. "popbuf"
263 * then pops the data onto the screen. Bound to
268 listbuffers(int f, int n)
270 static int initialized = 0;
275 maps_add((KEYMAP *)&listbufmap, "listbufmap");
279 if ((bp = makelist()) == NULL || (wp = popbuf(bp, WNONE)) == NULL)
281 wp->w_dotp = bp->b_dotp; /* fix up if window already on screen */
282 wp->w_doto = bp->b_doto;
283 bp->b_modes[0] = name_mode("fundamental");
284 bp->b_modes[1] = name_mode("listbufmap");
291 * This routine rebuilds the text for the
292 * list buffers command. Return pointer
293 * to new list if everything works.
294 * Return NULL if there is an error (if
295 * there is no memory).
297 static struct buffer *
301 struct buffer *bp, *blp;
304 if ((blp = bfind("*Buffer List*", TRUE)) == NULL)
306 if (bclear(blp) != TRUE)
308 blp->b_flag &= ~BFCHG; /* Blow away old. */
309 blp->b_flag |= BFREADONLY;
311 listbuf_ncol = ncol; /* cache ncol for listbuf_goto_buffer */
313 if (addlinef(blp, "%-*s%s", w, " MR Buffer", "Size File") == FALSE ||
314 addlinef(blp, "%-*s%s", w, " -- ------", "---- ----") == FALSE)
317 for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
320 nbytes = 0; /* Count bytes in buf. */
323 while (lp != bp->b_headp) {
324 nbytes += llength(lp) + 1;
328 nbytes--; /* no bonus newline */
331 if (addlinef(blp, "%c%c%c %-*.*s%c%-6d %-*s",
332 (bp == curbp) ? '.' : ' ', /* current buffer ? */
333 ((bp->b_flag & BFCHG) != 0) ? '*' : ' ', /* changed ? */
334 ((bp->b_flag & BFREADONLY) != 0) ? ' ' : '*',
335 w - 5, /* four chars already written */
336 w - 5, /* four chars already written */
337 bp->b_bname, /* buffer name */
338 strlen(bp->b_bname) < w - 5 ? ' ' : '$', /* truncated? */
339 nbytes, /* buffer size */
340 w - 7, /* seven chars already written */
341 bp->b_fname) == FALSE)
344 blp->b_dotp = bfirstlp(blp); /* put dot at beginning of
347 return (blp); /* All done */
351 listbuf_goto_buffer(int f, int n)
353 return (listbuf_goto_buffer_helper(f, n, 0));
357 listbuf_goto_buffer_one(int f, int n)
359 return (listbuf_goto_buffer_helper(f, n, 1));
363 listbuf_goto_buffer_helper(int f, int n, int only)
370 if (curwp->w_dotp->l_text[listbuf_ncol/2 - 1] == '$') {
371 ewprintf("buffer name truncated");
375 if ((line = malloc(listbuf_ncol/2)) == NULL)
378 memcpy(line, curwp->w_dotp->l_text + 4, listbuf_ncol/2 - 5);
379 for (i = listbuf_ncol/2 - 6; i > 0; i--) {
380 if (line[i] != ' ') {
388 for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
389 if (strcmp(bp->b_bname, line) == 0)
395 if ((wp = popbuf(bp, WNONE)) == NULL)
401 ret = (onlywind(f, n));
412 * The argument "fmt" points to a format string. Append this line to the
413 * buffer. Handcraft the EOL on the end. Return TRUE if it worked and
414 * FALSE if you ran out of room.
417 addlinef(struct buffer *bp, char *fmt, ...)
422 if ((lp = lalloc(0)) == NULL)
425 if (vasprintf(&lp->l_text, fmt, ap) == -1) {
430 lp->l_used = strlen(lp->l_text);
433 bp->b_headp->l_bp->l_fp = lp; /* Hook onto the end */
434 lp->l_bp = bp->b_headp->l_bp;
435 bp->b_headp->l_bp = lp;
436 lp->l_fp = bp->b_headp;
443 * Look through the list of buffers, giving the user a chance to save them.
444 * Return TRUE if there are any changed buffers afterwards. Buffers that don't
445 * have an associated file don't count. Return FALSE if there are no changed
446 * buffers. Return ABORT if an error occurs or if the user presses c-g.
452 int s = FALSE, save = FALSE, ret;
453 char pbuf[NFILEN + 11];
455 for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
456 if (bp->b_fname != NULL && *(bp->b_fname) != '\0' &&
457 (bp->b_flag & BFCHG) != 0) {
458 ret = snprintf(pbuf, sizeof(pbuf), "Save file %s",
460 if (ret < 0 || ret >= sizeof(pbuf)) {
461 ewprintf("Error: filename too long!");
464 if ((f == TRUE || (save = eyorn(pbuf)) == TRUE) &&
465 buffsave(bp) == TRUE) {
466 bp->b_flag &= ~BFCHG;
475 if (save == FALSE /* && kbdmop == NULL */ ) /* experimental */
476 ewprintf("(No files need saving)");
481 * Search for a buffer, by name.
482 * If not found, and the "cflag" is TRUE,
483 * create a new buffer. Return pointer to the found
487 bfind(const char *bname, int cflag)
493 if (strcmp(bname, bp->b_bname) == 0)
506 * Create a new buffer and put it in the list of
509 static struct buffer *
510 bnew(const char *bname)
517 bp = calloc(1, sizeof(struct buffer));
519 ewprintf("Can't get %d bytes", sizeof(struct buffer));
522 if ((lp = lalloc(0)) == NULL) {
526 bp->b_altb = bp->b_bufp = NULL;
531 bp->b_flag = defb_flag;
532 /* if buffer name starts and ends with '*', we ignore changes */
535 if (bname[0] == '*' && bname[len - 1] == '*')
536 bp->b_flag |= BFIGNDIRTY;
540 bp->b_nmodes = defb_nmodes;
541 TAILQ_INIT(&bp->b_undo);
542 bp->b_undoptr = NULL;
543 memset(&bp->b_undopos, 0, sizeof(bp->b_undopos));
546 bp->b_modes[i] = defb_modes[i];
547 } while (i++ < defb_nmodes);
548 bp->b_fname[0] = '\0';
550 bzero(&bp->b_fi, sizeof(bp->b_fi));
555 bp->b_dotline = bp->b_markline = 1;
557 if ((bp->b_bname = strdup(bname)) == NULL) {
558 ewprintf("Can't get %d bytes", strlen(bname) + 1);
566 * This routine blows away all of the text
567 * in a buffer. If the buffer is marked as changed
568 * then we ask if it is ok to blow it away; this is
569 * to save the user the grief of losing text. The
570 * window chain is nearly always wrong if this gets
571 * called; the caller must arrange for the updates
572 * that are required. Return TRUE if everything
576 bclear(struct buffer *bp)
581 /* Has buffer changed, and do we care? */
582 if (!(bp->b_flag & BFIGNDIRTY) && (bp->b_flag & BFCHG) != 0 &&
583 (s = eyesno("Buffer modified; kill anyway")) != TRUE)
585 bp->b_flag &= ~BFCHG; /* Not changed */
586 while ((lp = lforw(bp->b_headp)) != bp->b_headp)
588 bp->b_dotp = bp->b_headp; /* Fix dot */
590 bp->b_markp = NULL; /* Invalidate "mark" */
592 bp->b_dotline = bp->b_markline = 1;
599 * Display the given buffer in the given window. Flags indicated
600 * action on redisplay. Update modified flag so insert loop can check it.
603 showbuffer(struct buffer *bp, struct mgwin *wp, int flags)
608 /* Ensure file has not been modified elsewhere */
609 if (fchecktime(bp) != TRUE)
610 bp->b_flag |= BFDIRTY;
612 if (wp->w_bufp == bp) { /* Easy case! */
613 wp->w_rflag |= flags;
614 wp->w_dotp = bp->b_dotp;
615 wp->w_doto = bp->b_doto;
618 /* First, detach the old buffer from the window */
619 if ((bp->b_altb = obp = wp->w_bufp) != NULL) {
620 if (--obp->b_nwnd == 0) {
621 obp->b_dotp = wp->w_dotp;
622 obp->b_doto = wp->w_doto;
623 obp->b_markp = wp->w_markp;
624 obp->b_marko = wp->w_marko;
625 obp->b_dotline = wp->w_dotline;
626 obp->b_markline = wp->w_markline;
629 /* Now, attach the new buffer to the window */
632 if (bp->b_nwnd++ == 0) { /* First use. */
633 wp->w_dotp = bp->b_dotp;
634 wp->w_doto = bp->b_doto;
635 wp->w_markp = bp->b_markp;
636 wp->w_marko = bp->b_marko;
637 wp->w_dotline = bp->b_dotline;
638 wp->w_markline = bp->b_markline;
640 /* already on screen, steal values from other window */
641 for (owp = wheadp; owp != NULL; owp = wp->w_wndp)
642 if (wp->w_bufp == bp && owp != wp) {
643 wp->w_dotp = owp->w_dotp;
644 wp->w_doto = owp->w_doto;
645 wp->w_markp = owp->w_markp;
646 wp->w_marko = owp->w_marko;
647 wp->w_dotline = owp->w_dotline;
648 wp->w_markline = owp->w_markline;
651 wp->w_rflag |= WFMODE | flags;
656 * Augment a buffer name with a number, if necessary
658 * If more than one file of the same basename() is open,
659 * the additional buffers are named "file<2>", "file<3>", and
660 * so forth. This function adjusts a buffer name to
661 * include the number, if necessary.
664 augbname(char *bn, const char *fn, size_t bs)
669 if ((len = xbasename(bn, fn, bs)) >= bs)
673 for (count = 2; bfind(bn, FALSE) != NULL; count++)
674 snprintf(bn + len, remain, "<%d>", count);
680 * Pop the buffer we got passed onto the screen.
684 popbuf(struct buffer *bp, int flags)
688 if (bp->b_nwnd == 0) { /* Not on screen yet. */
690 * Pick a window for a pop-up.
691 * If only one window, split the screen.
692 * Flag the new window as ephemeral
694 if (wheadp->w_wndp == NULL &&
695 splitwind(FFOTHARG, flags) == FALSE)
699 * Pick the uppermost window that isn't
700 * the current window. An LRU algorithm
701 * might be better. Return a pointer, or NULL on error.
705 while (wp != NULL && wp == curwp)
708 for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
709 if (wp->w_bufp == bp) {
710 wp->w_rflag |= WFFULL | WFFRAME;
713 if (showbuffer(bp, wp, WFFULL) != TRUE)
719 * Insert another buffer at dot. Very useful.
723 bufferinsert(int f, int n)
728 char bufn[NBUFN], *bufp;
730 /* Get buffer to use from user */
731 if (curbp->b_altb != NULL)
732 bufp = eread("Insert buffer: (default %s) ", bufn, NBUFN,
733 EFNUL | EFNEW | EFBUF, curbp->b_altb->b_bname);
735 bufp = eread("Insert buffer: ", bufn, NBUFN, EFNEW | EFBUF);
738 if (bufp[0] == '\0' && curbp->b_altb != NULL)
740 else if ((bp = bfind(bufn, FALSE)) == NULL)
744 ewprintf("Cannot insert buffer into self");
747 /* insert the buffer */
751 for (clo = 0; clo < llength(clp); clo++)
752 if (linsert(1, lgetc(clp, clo)) == FALSE)
754 if ((clp = lforw(clp)) == bp->b_headp)
756 if (newline(FFRAND, 1) == FALSE) /* fake newline */
761 ewprintf("[Inserted 1 line]");
763 ewprintf("[Inserted %d lines]", nline);
765 clp = curwp->w_linep; /* cosmetic adjustment */
766 if (curwp->w_dotp == clp) { /* for offscreen insert */
767 while (nline-- && lback(clp) != curbp->b_headp)
769 curwp->w_linep = clp; /* adjust framing. */
770 curwp->w_rflag |= WFFULL;
776 * Turn off the dirty bit on this buffer.
780 notmodified(int f, int n)
784 curbp->b_flag &= ~BFCHG;
785 wp = wheadp; /* Update mode lines. */
787 if (wp->w_bufp == curbp)
788 wp->w_rflag |= WFMODE;
791 ewprintf("Modification-flag cleared");
796 * Popbuf and set all windows to top of buffer.
799 popbuftop(struct buffer *bp, int flags)
803 bp->b_dotp = bfirstlp(bp);
805 if (bp->b_nwnd != 0) {
806 for (wp = wheadp; wp != NULL; wp = wp->w_wndp)
807 if (wp->w_bufp == bp) {
808 wp->w_dotp = bp->b_dotp;
810 wp->w_rflag |= WFFULL;
813 return (popbuf(bp, flags) != NULL);
817 * Return the working directory for the current buffer, terminated
818 * with a '/'. First, try to extract it from the current buffer's
819 * filename. If that fails, use global cwd.
822 getbufcwd(char *path, size_t plen)
829 if (globalwd == FALSE && curbp->b_cwd[0] != '\0') {
830 (void)strlcpy(path, curbp->b_cwd, plen);
832 if (getcwdir(cwd, sizeof(cwd)) == FALSE)
834 (void)strlcpy(path, cwd, plen);
843 * Ensures a buffer has not been modified elsewhere; e.g. on disk.
844 * Prompt the user if it has.
845 * Returns TRUE if it has NOT (i.e. buffer is ok to edit).
846 * FALSE or ABORT otherwise
849 checkdirty(struct buffer *bp)
853 if ((bp->b_flag & (BFDIRTY | BFIGNDIRTY)) == BFDIRTY) {
854 if ((s = eyorn("File changed on disk; really edit the buffer"))
857 bp->b_flag &= ~BFDIRTY;
858 bp->b_flag |= BFIGNDIRTY;