]> git.eshelyaron.com Git - emacs.git/commitdiff
New command 'diff-buffers'
authorPhil Sainty <psainty@orcon.net.nz>
Sat, 14 Dec 2019 07:49:41 +0000 (20:49 +1300)
committerPhil Sainty <psainty@orcon.net.nz>
Sun, 15 Dec 2019 21:55:24 +0000 (10:55 +1300)
* lisp/vc/diff.el (diff-buffers): New command.
(diff, diff-no-select, diff-file-local-copy): Improve docstrings.
* doc/emacs/files.texi:
* etc/NEWS: Document new command, and the previously-undocumented
ability for 'diff' to compare buffers.

doc/emacs/files.texi
etc/NEWS
lisp/vc/diff.el

index c3ede1833b51811fbac7377b2fbf36577e6b7101..7221edcc1b2083a050a7567b84369b6cca0c9d44 100644 (file)
@@ -1394,6 +1394,10 @@ backup of.  In all other respects, this behaves like @kbd{M-x diff}.
 buffer with its corresponding file.  This shows you what changes you
 would make to the file if you save the buffer.
 
+@findex diff-buffers
+  The command @kbd{M-x diff-buffers} compares the contents of two
+specified buffers.
+
 @findex compare-windows
   The command @kbd{M-x compare-windows} compares the text in the
 current window with that in the window that was the selected window
index a7f3c3d2fe8543807cf9497b122cce1df55c7e94..18ed8de736e509b8563976ab5aa01992ac2c7e04 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1038,6 +1038,14 @@ is shown.)
 Set the new user option 'diff-font-lock-prettify' to t for that, see
 below under "Diff mode".
 
+---
+*** The 'diff' function arguments OLD and NEW may each be a buffer
+rather than a file, in non-interactive calls.  This change was made in
+Emacs 24.1, but wasn't documented until now.
+
++++
+*** New command 'diff-buffers' interactively diffs two buffers.
+
 ** Diff mode
 +++
 *** Hunks are now automatically refined by font-lock.
index 9ece8bc1fb473a859324ecd2357f13ff043a2431..5b055a1620fbe022aa31a46c528b03c235cf0774 100644 (file)
@@ -91,7 +91,10 @@ exists.  If NO-ASYNC is non-nil, call diff synchronously.
 
 When called interactively with a prefix argument, prompt
 interactively for diff switches.  Otherwise, the switches
-specified in the variable `diff-switches' are passed to the diff command."
+specified in the variable `diff-switches' are passed to the
+diff command.
+
+Non-interactively, OLD and NEW may each be a file or a buffer."
   (interactive
    (let* ((newf (if (and buffer-file-name (file-exists-p buffer-file-name))
                    (read-file-name
@@ -112,6 +115,9 @@ specified in the variable `diff-switches' are passed to the diff command."
    (diff-no-select old new switches no-async)))
 
 (defun diff-file-local-copy (file-or-buf)
+  "Like `file-local-copy' but also supports a buffer as the argument.
+When FILE-OR-BUF is a buffer, return the filename of a local
+temporary file with the buffer's contents."
   (if (bufferp file-or-buf)
       (with-current-buffer file-or-buf
         (let ((tempfile (make-temp-file "buffer-content-")))
@@ -139,6 +145,9 @@ Possible values are:
 
 (defun diff-no-select (old new &optional switches no-async buf)
   ;; Noninteractive helper for creating and reverting diff buffers
+  "Compare the OLD and NEW file/buffer, and return a diff buffer.
+
+See `diff' for the meaning of the arguments."
   (unless (bufferp new) (setq new (expand-file-name new)))
   (unless (bufferp old) (setq old (expand-file-name old)))
   (or switches (setq switches diff-switches)) ; If not specified, use default.
@@ -243,6 +252,28 @@ This requires the external program `diff' to be in your `exec-path'."
     (with-current-buffer (or (buffer-base-buffer buf) buf)
       (diff buffer-file-name (current-buffer) nil 'noasync))))
 
+;;;###autoload
+(defun diff-buffers (old new &optional switches no-async)
+  "Find and display the differences between OLD and NEW buffers.
+
+When called interactively, read NEW, then OLD, using the
+minibuffer.  The default for NEW is the current buffer, and the
+default for OLD is the most recently selected other buffer.
+If NO-ASYNC is non-nil, call diff synchronously.
+
+When called interactively with a prefix argument, prompt
+interactively for diff switches.  Otherwise, the switches
+specified in the variable `diff-switches' are passed to the
+diff command.
+
+OLD and NEW may each be a buffer or a buffer name."
+  (interactive
+   (let ((newb (read-buffer "Diff new buffer" (current-buffer) t))
+         (oldb (read-buffer "Diff original buffer"
+                            (other-buffer (current-buffer) t) t)))
+     (list oldb newb (diff-switches))))
+  (diff (get-buffer old) (get-buffer new) switches no-async))
+
 (provide 'diff)
 
 ;;; diff.el ends here