summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Obser <florian@cvs.openbsd.org>2012-10-22 08:31:43 +0000
committerFlorian Obser <florian@cvs.openbsd.org>2012-10-22 08:31:43 +0000
commit894d1e27e363f283d4f4345d495c69662628068a (patch)
treea8ab51a73667b6a7eae41a29c8d58bdb5b063748
parentd801589fb7c8f9f2a6cb773ecfe2409799fc90db (diff)
Add (r)evert prompt to "File changed on disk; really edit the buffer"
prompt. input, ok lum@, man bit ok jmc@, ok benno@
-rw-r--r--usr.bin/mg/buffer.c63
-rw-r--r--usr.bin/mg/def.h5
-rw-r--r--usr.bin/mg/echo.c32
-rw-r--r--usr.bin/mg/mg.111
4 files changed, 83 insertions, 28 deletions
diff --git a/usr.bin/mg/buffer.c b/usr.bin/mg/buffer.c
index 5f3330c3ee1..c81c50dfce8 100644
--- a/usr.bin/mg/buffer.c
+++ b/usr.bin/mg/buffer.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: buffer.c,v 1.83 2012/10/22 08:22:04 florian Exp $ */
+/* $OpenBSD: buffer.c,v 1.84 2012/10/22 08:31:42 florian Exp $ */
/* This file is in the public domain. */
@@ -856,11 +856,18 @@ checkdirty(struct buffer *bp)
bp->b_flag |= BFDIRTY;
if ((bp->b_flag & (BFDIRTY | BFIGNDIRTY)) == BFDIRTY) {
- if ((s = eyorn("File changed on disk; really edit the buffer"))
- != TRUE)
+ s = eynorr("File changed on disk; really edit the buffer");
+ switch (s) {
+ case TRUE:
+ bp->b_flag &= ~BFDIRTY;
+ bp->b_flag |= BFIGNDIRTY;
+ return (TRUE);
+ case REVERT:
+ dorevert();
+ return (FALSE);
+ default:
return (s);
- bp->b_flag &= ~BFDIRTY;
- bp->b_flag |= BFIGNDIRTY;
+ }
}
return (TRUE);
@@ -873,10 +880,8 @@ checkdirty(struct buffer *bp)
int
revertbuffer(int f, int n)
{
- struct mgwin *wp = wheadp;
- struct buffer *bp = wp->w_bufp;
+ struct buffer *bp = wheadp->w_bufp;
char fbuf[NFILEN + 32];
- int lineno;
if (bp->b_fname[0] == 0) {
ewprintf("Cannot revert buffer not associated with any files.");
@@ -885,26 +890,36 @@ revertbuffer(int f, int n)
snprintf(fbuf, sizeof(fbuf), "Revert buffer from file %s", bp->b_fname);
- if (eyorn(fbuf)) {
- if (access(bp->b_fname, F_OK|R_OK) != 0) {
- if (errno == ENOENT)
- ewprintf("File %s no longer exists!",
- bp->b_fname);
- else
- ewprintf("File %s is no longer readable!",
- bp->b_fname);
- return (FALSE);
- }
+ if (eyorn(fbuf))
+ return dorevert();
- /* Save our current line, so we can go back after reloading. */
- lineno = wp->w_dotline;
+ return (FALSE);
+}
- /* Prevent readin from asking if we want to kill the buffer. */
- curbp->b_flag &= ~BFCHG;
+int
+dorevert()
+{
+ struct mgwin *wp = wheadp;
+ struct buffer *bp = wp->w_bufp;
+ int lineno;
- if (readin(bp->b_fname))
- return(setlineno(lineno));
+ if (access(bp->b_fname, F_OK|R_OK) != 0) {
+ if (errno == ENOENT)
+ ewprintf("File %s no longer exists!",
+ bp->b_fname);
+ else
+ ewprintf("File %s is no longer readable!",
+ bp->b_fname);
+ return (FALSE);
}
+ /* Save our current line, so we can go back after reloading. */
+ lineno = wp->w_dotline;
+
+ /* Prevent readin from asking if we want to kill the buffer. */
+ curbp->b_flag &= ~BFCHG;
+
+ if (readin(bp->b_fname))
+ return(setlineno(lineno));
return (FALSE);
}
diff --git a/usr.bin/mg/def.h b/usr.bin/mg/def.h
index c24c8505a73..2efe5cc6f36 100644
--- a/usr.bin/mg/def.h
+++ b/usr.bin/mg/def.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: def.h,v 1.126 2012/10/12 21:13:46 jasper Exp $ */
+/* $OpenBSD: def.h,v 1.127 2012/10/22 08:31:42 florian Exp $ */
/* This file is in the public domain. */
@@ -37,6 +37,7 @@ typedef int (*PF)(int, int); /* generally useful type */
#define TRUE 1 /* True, yes, good, etc. */
#define ABORT 2 /* Death, ^G, abort, etc. */
#define UERROR 3 /* User Error. */
+#define REVERT 4 /* Revert the buffer */
#define KCLEAR 2 /* clear echo area */
@@ -415,6 +416,7 @@ int popbuftop(struct buffer *, int);
int getbufcwd(char *, size_t);
int checkdirty(struct buffer *);
int revertbuffer(int, int);
+int dorevert();
/* display.c */
int vtresize(int, int, int);
@@ -426,6 +428,7 @@ int linenotoggle(int, int);
/* echo.c X */
void eerase(void);
int eyorn(const char *);
+int eynorr(const char *);
int eyesno(const char *);
void ewprintf(const char *fmt, ...);
char *ereply(const char *, char *, size_t, ...);
diff --git a/usr.bin/mg/echo.c b/usr.bin/mg/echo.c
index beff2f1420b..1e500b99ec2 100644
--- a/usr.bin/mg/echo.c
+++ b/usr.bin/mg/echo.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: echo.c,v 1.51 2012/09/25 19:16:52 lum Exp $ */
+/* $OpenBSD: echo.c,v 1.52 2012/10/22 08:31:42 florian Exp $ */
/* This file is in the public domain. */
@@ -72,6 +72,36 @@ eyorn(const char *sp)
}
/*
+ * Ask a "yes", "no" or "revert" question. Return ABORT if the user answers
+ * the question with the abort ("^G") character. Return FALSE for "no",
+ * TRUE for "yes" and REVERT for "revert". No formatting services are
+ * available. No newline required.
+ */
+int
+eynorr(const char *sp)
+{
+ int s;
+
+ if (inmacro)
+ return (TRUE);
+
+ ewprintf("%s? (y, n or r) ", sp);
+ for (;;) {
+ s = getkey(FALSE);
+ if (s == 'y' || s == 'Y' || s == ' ')
+ return (TRUE);
+ if (s == 'n' || s == 'N' || s == CCHR('M'))
+ return (FALSE);
+ if (s == 'r' || s == 'R')
+ return (REVERT);
+ if (s == CCHR('G'))
+ return (ctrlg(FFRAND, 1));
+ ewprintf("Please answer y, n or r.");
+ }
+ /* NOTREACHED */
+}
+
+/*
* Like eyorn, but for more important questions. User must type all of
* "yes" or "no" and the trailing newline.
*/
diff --git a/usr.bin/mg/mg.1 b/usr.bin/mg/mg.1
index baab1863ce1..e47474365ee 100644
--- a/usr.bin/mg/mg.1
+++ b/usr.bin/mg/mg.1
@@ -1,7 +1,7 @@
-.\" $OpenBSD: mg.1,v 1.69 2012/10/12 21:13:47 jasper Exp $
+.\" $OpenBSD: mg.1,v 1.70 2012/10/22 08:31:42 florian Exp $
.\" This file is in the public domain.
.\"
-.Dd $Mdocdate: October 12 2012 $
+.Dd $Mdocdate: October 22 2012 $
.Dt MG 1
.Os
.Sh NAME
@@ -51,6 +51,13 @@ At present, windows may only be split horizontally, so each window is
delineated by a modeline at the bottom.
If changes are made to a buffer, it will be reflected in all open windows.
.Pp
+If a file is changed outside
+.Nm
+and its buffer is about to be changed,
+.Nm
+prompts if the change should go ahead (y), not go ahead (n) or if the buffer
+should be reverted (r) to the latest file on disk.
+.Pp
If a buffer name begins and ends with an asterisk, the buffer is considered
throwaway; i.e. the user will not be prompted to save changes when
the buffer is killed.