help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: diffs between a buffer and the underlying file


From: John Paul Wallington
Subject: Re: diffs between a buffer and the underlying file
Date: Wed, 06 Sep 2006 01:18:10 +0100
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (windows-nt)

Urs Thuermann <urs@isnogud.escape.de> writes:

> I sometimes get into the following situation:  I open a file, make
> some changes, then I want to see the changes before saving the file.

The development version of Emacs has M-x diff-buffer-with-file that
does what you want.  If you are using a release version of Emacs then
you could try putting this in your .emacs file (untested):

(defun diff-buffer-with-file (&optional buffer)
  "View the differences between BUFFER and its associated file.
This requires the external program `diff' to be in your `exec-path'."
  (interactive "bBuffer: ")
  (with-current-buffer (get-buffer (or buffer (current-buffer)))
    (if (and buffer-file-name
             (file-exists-p buffer-file-name))
        (let ((tempfile (make-temp-file "buffer-content-")))
          (unwind-protect
              (save-restriction
                (widen)
                (write-region (point-min) (point-max) tempfile nil 'nomessage)
                (diff buffer-file-name tempfile nil t)
                (sit-for 0))
            (when (file-exists-p tempfile)
              (delete-file tempfile))))
      (message "Buffer %s has no associated file on disc" (buffer-name))
      ;; Display that message for 1 second so that user can read it
      ;; in the minibuffer.
      (sit-for 1)))
  ;; return always nil, so that save-buffers-kill-emacs will not move
  ;; over to the next unsaved buffer when calling `d'.
  nil)

Also, the Ibuffer library, an advanced replacement for the normal
buffer menu, has an `ibuffer-diff-with-file' command that you can get
by pressing =.  Ibuffer is included in the development version of
Emacs.  Alternatively, a stable version of Ibuffer is available from
http://www.shootybangbang.com/software/ibuffer.el


reply via email to

[Prev in Thread] Current Thread [Next in Thread]