emacs-devel
[Top][All Lists]
Advanced

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

Offering the differences on exit


From: Mario Lang
Subject: Offering the differences on exit
Date: Tue, 02 Jul 2002 21:08:28 +0200
User-agent: Gnus/5.090007 (Oort Gnus v0.07) Emacs/21.2 (i386-debian-linux-gnu)

One feature I miss since I tried out Emacs the first time
is the ability to see a diff when you are asked if you want to
save changed you've made.

Now, there are actually two places where this could happen, when you explicitly
kill a buffer (I dont really need it there), and when you exit
Emacs (there I usually already forgot what I really edited, and sometimes would
like to have a look at the actual differencies).

The second case is handled in save-some-buffers.  Now it was quite
hard at first to understand whats going on there :), but I finally came up
with a little patch to allow custom additions (like the diff option).

It created a new variable, save-some-buffers-action-alist which
holds the required values for C-r as default.  This variable
can then be used to insert more options there, like 'd' for diff.

OK, here is the patch.

Index: lisp/files.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/files.el,v
retrieving revision 1.587
diff -u -r1.587 files.el
--- lisp/files.el       30 Jun 2002 14:41:43 -0000      1.587
+++ lisp/files.el       2 Jul 2002 19:00:43 -0000
@@ -2913,6 +2913,19 @@
                      buffer-file-name nil t buffer-file-truename)))
     setmodes))
 
+(defvar save-some-buffers-action-alist
+  '((?\C-r
+     (lambda (buf)
+       (view-buffer buf
+                   (function
+                    (lambda (ignore)
+                      (exit-recursive-edit))))
+       (recursive-edit)
+       ;; Return nil to ask about BUF again.
+       nil)
+     "display the current buffer"))
+  "ACTION-ALIST argument used in call to `map-y-or-n-p'.")
+
 (defun save-some-buffers (&optional arg pred)
   "Save some modified file-visiting buffers.  Asks user about each one.
 Optional argument (the prefix) non-nil means save all with no questions.
@@ -2952,15 +2965,7 @@
                (save-buffer)))
             (buffer-list)
             '("buffer" "buffers" "save")
-            (list (list ?\C-r (lambda (buf)
-                                (view-buffer buf
-                                             (function
-                                              (lambda (ignore)
-                                                (exit-recursive-edit))))
-                                (recursive-edit)
-                                ;; Return nil to ask about BUF again.
-                                nil)
-                        "display the current buffer"))))
+            save-some-buffers-action-alist))
           (abbrevs-done
            (and save-abbrevs abbrevs-changed
                 (progn

---<snip>---

Now, I use the following code to get my diff option:

(add-to-list 'save-some-buffers-action-alist
             (list ?d #'diff-buffer-with-file
                   "Show difference to last saved version"))


;; Code borrowed from ibuffer-diff-with-file
(defun diff-buffer-with-file (buffer)
  "View the differences between BUFFER and its associated file.
This requires the external program \"diff\" to be in your `exec-path'."
  (interactive "b")
  (let ((buf-filename (with-current-buffer buffer buffer-file-name)))
    (unless buf-filename
      (error "Buffer %s has no associated file" buffer))
    (let ((diff-buf (get-buffer-create "*Buffer-diff*")))
      (with-current-buffer diff-buf
        (setq buffer-read-only nil)
        (erase-buffer))
      (let ((tempfile (make-temp-file "buffer-to-file-diff-")))
        (unwind-protect
            (progn
              (with-current-buffer buffer
                (write-region (point-min) (point-max) tempfile nil 'nomessage))
              (if (zerop
                   (apply #'call-process "diff" nil diff-buf nil
                          (append
                           (when (and (boundp 'ediff-custom-diff-options)
                                      (stringp ediff-custom-diff-options))
                             (list ediff-custom-diff-options))
                           (list buf-filename tempfile))))
                  (message "No differences found")
                (progn
                  (with-current-buffer diff-buf
                    (goto-char (point-min))
                    (if (fboundp 'diff-mode)
                        (diff-mode)
                      (fundamental-mode)))
                  (display-buffer diff-buf))))
          (when (file-exists-p tempfile)
            (delete-file tempfile)))))
      nil))



Question: 

Could the patch make it into Standard Emacs?

-- 
CYa,
  Mario




reply via email to

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