summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArtur Grabowski <art@cvs.openbsd.org>2002-01-18 09:40:08 +0000
committerArtur Grabowski <art@cvs.openbsd.org>2002-01-18 09:40:08 +0000
commit814dbc6bc8e04e6d60db0c60f4c43ca6d39da6d5 (patch)
treec6fea853483755d1e598bde24315f3139a562e7f
parent7598f41630e91a2ab9de9e4ef1f14f7f1e311e40 (diff)
When listing buffers, detect if the buffer name is too long to fit in the
designated space and if it's too long, truncate it correctly and print a '$'-sign at the end of the name. Add support for selecting a buffer with ^M in the buffer list.
-rw-r--r--usr.bin/mg/buffer.c84
1 files changed, 81 insertions, 3 deletions
diff --git a/usr.bin/mg/buffer.c b/usr.bin/mg/buffer.c
index 07c5daa6b9b..86dda192a93 100644
--- a/usr.bin/mg/buffer.c
+++ b/usr.bin/mg/buffer.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: buffer.c,v 1.18 2002/01/18 08:37:08 art Exp $ */
+/* $OpenBSD: buffer.c,v 1.19 2002/01/18 09:40:07 art Exp $ */
/*
* Buffer handling.
@@ -176,6 +176,26 @@ savebuffers(f, n)
}
/*
+ * Listing buffers.
+ */
+static int listbuf_ncol;
+
+static int listbuf_goto_buffer(int f, int n);
+
+static PF listbuf_pf[] = {
+ listbuf_goto_buffer,
+};
+
+static struct KEYMAPE (1 + IMAPEXT) listbufmap = {
+ 1,
+ 1 + IMAPEXT,
+ rescan,
+ {
+ { CCHR('M'), CCHR('M'), listbuf_pf, NULL },
+ }
+};
+
+/*
* Display the buffer list. This is done
* in two parts. The "makelist" routine figures out
* the text, and puts it in a buffer. "popbuf"
@@ -187,13 +207,24 @@ int
listbuffers(f, n)
int f, n;
{
+ static int initialized = 0;
BUFFER *bp;
MGWIN *wp;
+ if (!initialized) {
+ maps_add((KEYMAP *)&listbufmap, "listbufmap");
+ initialized = 1;
+ }
+
+
if ((bp = makelist()) == NULL || (wp = popbuf(bp)) == NULL)
return FALSE;
wp->w_dotp = bp->b_dotp;/* fix up if window already on screen */
wp->w_doto = bp->b_doto;
+ bp->b_modes[0] = name_mode("fundamental");
+ bp->b_modes[1] = name_mode("listbufmap");
+ bp->b_nmodes = 1;
+
return TRUE;
}
@@ -210,12 +241,15 @@ makelist()
BUFFER *bp, *blp;
LINE *lp;
+
if ((blp = bfind("*Buffer List*", TRUE)) == NULL)
return NULL;
if (bclear(blp) != TRUE)
return NULL;
blp->b_flag &= ~BFCHG; /* Blow away old. */
+ listbuf_ncol = ncol; /* cache ncol for listbuf_goto_buffer */
+
if (addlinef(blp, "%-*s%s", w, " MR Buffer", "Size File") == FALSE ||
addlinef(blp, "%-*s%s", w, " -- ------", "---- ----") == FALSE)
return NULL;
@@ -234,12 +268,14 @@ makelist()
nbytes--; /* no bonus newline */
}
- if (addlinef(blp, "%c%c%c %-*s%-6d %-*s",
+ if (addlinef(blp, "%c%c%c %-*.*s%c%-6d %-*s",
(bp == curbp) ? '.' : ' ', /* current buffer ? */
((bp->b_flag & BFCHG) != 0) ? '*' : ' ', /* changed ? */
' ', /* no readonly buffers yet */
- w - 4, /* four chars already written */
+ w - 5, /* four chars already written */
+ w - 5, /* four chars already written */
bp->b_bname, /* buffer name */
+ strlen(bp->b_bname) < w - 5 ? ' ' : '$', /* truncated? */
nbytes, /* buffer size */
w - 7, /* seven chars already written */
bp->b_fname) == FALSE)
@@ -251,6 +287,48 @@ makelist()
return blp; /* All done */
}
+static int
+listbuf_goto_buffer(int f, int n)
+{
+ BUFFER *bp;
+ MGWIN *wp;
+ char *line;
+ int i;
+
+ if (curwp->w_dotp->l_text[listbuf_ncol/2 - 1] == '$') {
+ ewprintf("buffer name truncated");
+ return FALSE;
+ }
+
+ if ((line = malloc(listbuf_ncol/2)) == NULL)
+ return FALSE;
+
+ memcpy(line, curwp->w_dotp->l_text + 4, listbuf_ncol/2 - 5);
+ for (i = listbuf_ncol/2 - 6; i > 0; i--) {
+ if (line[i] != ' ') {
+ line[i + 1] = '\0';
+ break;
+ }
+ }
+ if (i == 0) {
+ return FALSE;
+ }
+
+ for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
+ if (strcmp(bp->b_bname, line) == 0)
+ break;
+ }
+ if (bp == NULL) {
+ return FALSE;
+ }
+ if ((wp = popbuf(bp)) == NULL)
+ return FALSE;
+ curbp = bp;
+ curwp = wp;
+
+ return TRUE;
+}
+
/*
* The argument "text" points to a format string. Append this line to the
* buffer. Handcraft the EOL on the end. Return TRUE if it worked and