updated diff:
- make error message more clear when access(DIFFTOOL, X_OK) fails
- name output buffer *Diff* to make it more emacs-like
- make message more emacs-like when there is no diff
input gsoares@, Sunil Nimmagadda
diff --git buffer.c buffer.c
index 0b16af1..424f3a7 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,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", 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 def.h def.h
index 3c7702c..6fce102 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.