all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: rogers-emacs@rgrjr.dyndns.org
To: 7277@debbugs.gnu.org
Subject: bug#7277: 24.0.50; Can't revert diff-buffer-with-file
Date: Sun, 24 Oct 2010 16:30:17 -0400	[thread overview]
Message-ID: <19652.38745.217242.560815@rgr.rgrjr.com> (raw)

[-- Attachment #1: message body text --]
[-- Type: text/plain, Size: 555 bytes --]

   The problem, of course, is that the temp file is deleted shortly
after "diff" exits, so the default revert-buffer function installed by
"diff" fails.  The simplest solution seems to be to install a new
revert-buffer function that redoes the whole process of checking for a
file buffer and writing the temp file.  The attached patch does a
minimal refactoring of "diff" and "diff-buffer-with-file" to do just
that, while trying to DTRT if the user renames the diff buffer or kills
the original buffer.

					-- Bob Rogers
					   http://www.rgrjr.com/


[-- Attachment #2: Type: text/plain, Size: 3338 bytes --]

diff --git a/lisp/files.el b/lisp/files.el
index d5f60b7..603c014 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -4512,24 +4512,42 @@ Before and after saving the buffer, this function runs
   "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)))
+  (diff-buffer-internal (get-buffer (or buffer (current-buffer)))
+			(get-buffer-create "*Diff*"))
+  ;; return always nil, so that save-buffers-kill-emacs will not move
+  ;; over to the next unsaved buffer when calling `d'.
+  nil)
+
+(defvar diff-buffer-buffer)	;; suppress compiler warnings.
+
+(defun diff-buffer-internal (buffer result-buffer)
+  (if (not (and buffer (buffer-name buffer)))
+      (error "Original buffer deleted."))
+  (with-current-buffer buffer
     (if (and buffer-file-name
 	     (file-exists-p buffer-file-name))
 	(let ((tempfile (make-temp-file "buffer-content-")))
 	  (unwind-protect
 	      (progn
 		(write-region nil nil tempfile nil 'nomessage)
-		(diff buffer-file-name tempfile nil t)
-		(sit-for 0))
+		;; No asynch so we don't delete the temp file prematurely.
+		(diff-into-buffer result-buffer buffer-file-name tempfile
+				  nil t)
+		(sit-for 0)
+		;; Now revise the revert-buffer-function, since the
+		;; default will not be able to find the temp file.
+		(with-current-buffer result-buffer
+		  (set (make-local-variable 'diff-buffer-buffer) buffer)
+		  (setq revert-buffer-function
+			(lambda (ignore-auto noconfirm)
+			  (diff-buffer-internal diff-buffer-buffer
+						(current-buffer))))))
 	    (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)
+      (sit-for 1))))
 
 (defvar save-some-buffers-action-alist
   `((?\C-r
diff --git a/lisp/vc/diff.el b/lisp/vc/diff.el
index e79e72c..1a835b5 100644
--- a/lisp/vc/diff.el
+++ b/lisp/vc/diff.el
@@ -108,11 +108,16 @@ specified in `diff-switches' are passed to the diff command."
 		  (read-file-name "Diff original file: "
 				  (file-name-directory newf) nil t)))
      (list oldf newf (diff-switches))))
+  (diff-into-buffer nil old new switches no-async))
+
+(defun diff-into-buffer (buf old new &optional switches no-async)
+  ;; Noninteractive helper for creating and reverting diff buffers.
   (setq new (expand-file-name new)
 	old (expand-file-name old))
   (or switches (setq switches diff-switches)) ; If not specified, use default.
+  (or buf (setq buf (get-buffer-create "*Diff*")))
   (let* ((old-alt (file-local-copy old))
-	(new-alt (file-local-copy new))
+	 (new-alt (file-local-copy new))
 	 (command
 	  (mapconcat 'identity
 		     `(,diff-command
@@ -123,7 +128,6 @@ specified in `diff-switches' are passed to the diff command."
 		       ,(shell-quote-argument (or old-alt old))
 		       ,(shell-quote-argument (or new-alt new)))
 		     " "))
-	 (buf (get-buffer-create "*Diff*"))
 	 (thisdir default-directory)
 	 proc)
     (save-excursion

             reply	other threads:[~2010-10-24 20:30 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-10-24 20:30 rogers-emacs [this message]
2010-11-22 19:22 ` bug#7277: 24.0.50; Can't revert diff-buffer-with-file Stefan Monnier
2010-11-23 19:44   ` Glenn Morris

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=19652.38745.217242.560815@rgr.rgrjr.com \
    --to=rogers-emacs@rgrjr.dyndns.org \
    --cc=7277@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.