Updated diff, on pressing the wrong key the prompt should be Please answer y, n or r. pointed out by lum@, thanks!
diff --git buffer.c buffer.c index 60076bb..6bce870 100644 --- buffer.c +++ buffer.c @@ -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 def.h def.h index c24c850..2098521 100644 --- def.h +++ def.h @@ -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 echo.c echo.c index beff2f1..21ef865 100644 --- echo.c +++ echo.c @@ -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. %s? (y, n or r) ", sp); + } + /* NOTREACHED */ +} + +/* * Like eyorn, but for more important questions. User must type all of * "yes" or "no" and the trailing newline. */ -- I'm not entirely sure you are real.