before:
me: *edit* *edit* C-x C-s
mg: File has changed on disk since last save. Save anyway?
me: uh oh

now:
me: *edit* *edit* C-x C-s
mg: File has changed on disk since last save. Save anyway?
me: no; M-x diff-buffer-with-file

Comments, ok?

diff --git buffer.c buffer.c
index 0b16af1..a2e692f 100644
--- buffer.c
+++ buffer.c
@@ -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,60 @@ dorevert(void)
                return(setlineno(lineno));
        return (FALSE);
 }
+
+/*
+ * Diff the current buffer to what is on disk.
+ */
+/*ARGSUSED */
+int
+diffbuffer(int f, int n)
+{
+       struct line     *lp, *lpend;
+       size_t           len;
+       int              ret;
+       char            *text, *ttext;
+       char            * const argv[] =
+           {DIFFTOOL, "-u", 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.", 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';
+       }
+
+       ret = pipeio(DIFFTOOL, argv, text, len);
+       free(text);
+       eerase();
+       return (ret);
+}
diff --git def.h def.h
index 5cc6525..766d62d 100644
--- def.h
+++ def.h
@@ -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 funmap.c funmap.c
index 608ba59..8b34ccc 100644
--- funmap.c
+++ funmap.c
@@ -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 mg.1 mg.1
index 618cc39..3cd3ced 100644
--- mg.1
+++ mg.1
@@ -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

-- 
I'm not entirely sure you are real.

Reply via email to