diff options
-rw-r--r-- | usr.bin/mg/buffer.c | 77 | ||||
-rw-r--r-- | usr.bin/mg/def.h | 3 | ||||
-rw-r--r-- | usr.bin/mg/funmap.c | 3 | ||||
-rw-r--r-- | usr.bin/mg/mg.1 | 6 |
4 files changed, 84 insertions, 5 deletions
diff --git a/usr.bin/mg/buffer.c b/usr.bin/mg/buffer.c index 0b16af1f737..0b590ebbfd8 100644 --- a/usr.bin/mg/buffer.c +++ b/usr.bin/mg/buffer.c @@ -1,4 +1,4 @@ -/* $OpenBSD: buffer.c,v 1.87 2012/11/06 18:04:10 florian Exp $ */ +/* $OpenBSD: buffer.c,v 1.88 2012/12/27 18:51:52 florian Exp $ */ /* This file is in the public domain. */ @@ -12,6 +12,10 @@ #include <libgen.h> #include <stdarg.h> +#ifndef DIFFTOOL +#define DIFFTOOL "/usr/bin/diff" +#endif /* !DIFFTOOL */ + static struct buffer *makelist(void); static struct buffer *bnew(const char *); @@ -920,3 +924,74 @@ dorevert(void) return(setlineno(lineno)); return (FALSE); } + +/* + * Diff the current buffer to what is on disk. + */ +/*ARGSUSED */ +int +diffbuffer(int f, int n) +{ + struct buffer *bp; + struct line *lp, *lpend; + size_t len; + int ret; + char *text, *ttext; + char * const argv[] = + {DIFFTOOL, "-u", "-p", curbp->b_fname, "-", (char *)NULL}; + + len = 0; + + /* C-u is not supported */ + if (n > 1) + return (ABORT); + + if (access(DIFFTOOL, X_OK) != 0) { + ewprintf("%s not found or not executable.", DIFFTOOL); + return (FALSE); + } + + if (curbp->b_fname[0] == 0) { + ewprintf("Cannot diff buffer not associated with any files."); + return (FALSE); + } + + lpend = curbp->b_headp; + for (lp = lforw(lpend); lp != lpend; lp = lforw(lp)) { + len+=llength(lp); + if (lforw(lp) != lpend) /* no implied \n on last line */ + len++; + } + if ((text = calloc(len + 1, sizeof(char))) == NULL) { + ewprintf("Cannot allocate memory."); + return (FALSE); + } + ttext = text; + + for (lp = lforw(lpend); lp != lpend; lp = lforw(lp)) { + if (llength(lp) != 0) { + memcpy(ttext, ltext(lp), llength(lp)); + ttext += llength(lp); + } + if (lforw(lp) != lpend) /* no implied \n on last line */ + *ttext++ = '\n'; + } + + bp = bfind("*Diff*", TRUE); + bp->b_flag |= BFREADONLY; + if (bclear(bp) != TRUE) { + free(text); + return (FALSE); + } + + ret = pipeio(DIFFTOOL, argv, text, len, bp); + + if (ret == TRUE) { + eerase(); + if (lforw(bp->b_headp) == bp->b_headp) + addline(bp, "Diff finished (no differences)."); + } + + free(text); + return (ret); +} diff --git a/usr.bin/mg/def.h b/usr.bin/mg/def.h index 9a6e82ce7d9..c3c8f6194bb 100644 --- a/usr.bin/mg/def.h +++ b/usr.bin/mg/def.h @@ -1,4 +1,4 @@ -/* $OpenBSD: def.h,v 1.133 2012/12/27 18:49:59 florian Exp $ */ +/* $OpenBSD: def.h,v 1.134 2012/12/27 18:51:52 florian Exp $ */ /* This file is in the public domain. */ @@ -417,6 +417,7 @@ int getbufcwd(char *, size_t); int checkdirty(struct buffer *); int revertbuffer(int, int); int dorevert(void); +int diffbuffer(int, int); /* display.c */ int vtresize(int, int, int); diff --git a/usr.bin/mg/funmap.c b/usr.bin/mg/funmap.c index 608ba5922b8..a675b6ec1fc 100644 --- a/usr.bin/mg/funmap.c +++ b/usr.bin/mg/funmap.c @@ -1,4 +1,4 @@ -/* $OpenBSD: funmap.c,v 1.44 2012/12/04 10:54:20 florian Exp $ */ +/* $OpenBSD: funmap.c,v 1.45 2012/12/27 18:51:52 florian Exp $ */ /* This file is in the public domain */ @@ -74,6 +74,7 @@ static struct funmap functnames[] = { {delwind, "delete-window",}, {wallchart, "describe-bindings",}, {desckey, "describe-key-briefly",}, + {diffbuffer, "diff-buffer-with-file",}, {digit_argument, "digit-argument",}, {lowerregion, "downcase-region",}, {lowerword, "downcase-word",}, diff --git a/usr.bin/mg/mg.1 b/usr.bin/mg/mg.1 index 618cc3936ba..fffd26ed620 100644 --- a/usr.bin/mg/mg.1 +++ b/usr.bin/mg/mg.1 @@ -1,7 +1,7 @@ -.\" $OpenBSD: mg.1,v 1.73 2012/12/04 10:54:20 florian Exp $ +.\" $OpenBSD: mg.1,v 1.74 2012/12/27 18:51:52 florian Exp $ .\" This file is in the public domain. .\" -.Dd $Mdocdate: December 4 2012 $ +.Dd $Mdocdate: December 27 2012 $ .Dt MG 1 .Os .Sh NAME @@ -495,6 +495,8 @@ the *help* buffer. .It describe-key-briefly Read a key from the keyboard, and look it up in the keymap. Display the name of the function currently bound to the key. +.It diff-buffer-with-file +View the differences between buffer and its associated file. .It digit-argument Process a numerical argument for keyboard-invoked functions. .It downcase-region |