unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Offering the differences on exit
@ 2002-07-02 19:08 Mario Lang
  2002-07-02 20:47 ` Kim F. Storm
  0 siblings, 1 reply; 3+ messages in thread
From: Mario Lang @ 2002-07-02 19:08 UTC (permalink / raw)



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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Offering the differences on exit
  2002-07-02 20:47 ` Kim F. Storm
@ 2002-07-02 20:25   ` Mario Lang
  0 siblings, 0 replies; 3+ messages in thread
From: Mario Lang @ 2002-07-02 20:25 UTC (permalink / raw)


storm@cua.dk (Kim F. Storm) writes:

> Mario Lang <mlang@delysid.org> writes:
>
>> 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.
>
> I agree this would be extremely useful.
>
> However, I would prefer if this was built-in as a default option in
> save-some-buffers.
>
> In that case, I don't see a great need for the more general approach
> you have taken with the save-some-buffers-action-alist (although it
> might have other uses).

I figure people might want to use ediff instead of external diff.
Also, when diff is available, I would like to remove C-r, as I never
used it really before.

> The following function may be useful here (it doesn't
> use a temp file, but maybe that's not portable diff
> behaviour):

shell-command-on-region uses make-tempfile AFAICS.

> (defun diff-buffer-to-visited-file (&optional buffer)
>   (interactive "bBuffer: ")
>   (setq buffer (get-buffer (or buffer (current-buffer))))
>   (let ((file (buffer-file-name buffer)))
>     (if file
>         (with-current-buffer buffer
>           (save-restriction
>             (widen)
>             (shell-command-on-region (point-min) (point-max) 
>                (concat "diff -c " file " -"))))

If it went as a built-in into emacs I'd prefer to be able to configure
the diff options.  Either use vc-cvs-diff-switches, or
ediff-custom-diff-options or some other variable.

>       (message "No file is associated with buffer"))))

-- 
CYa,
  Mario

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Offering the differences on exit
  2002-07-02 19:08 Offering the differences on exit Mario Lang
@ 2002-07-02 20:47 ` Kim F. Storm
  2002-07-02 20:25   ` Mario Lang
  0 siblings, 1 reply; 3+ messages in thread
From: Kim F. Storm @ 2002-07-02 20:47 UTC (permalink / raw)
  Cc: emacs-devel

Mario Lang <mlang@delysid.org> writes:

> 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.

I agree this would be extremely useful.

However, I would prefer if this was built-in as a default option in
save-some-buffers.

In that case, I don't see a great need for the more general approach
you have taken with the save-some-buffers-action-alist (although it
might have other uses).

The following function may be useful here (it doesn't
use a temp file, but maybe that's not portable diff
behaviour):

(defun diff-buffer-to-visited-file (&optional buffer)
  (interactive "bBuffer: ")
  (setq buffer (get-buffer (or buffer (current-buffer))))
  (let ((file (buffer-file-name buffer)))
    (if file
        (with-current-buffer buffer
          (save-restriction
            (widen)
            (shell-command-on-region (point-min) (point-max) 
               (concat "diff -c " file " -"))))
      (message "No file is associated with buffer"))))

-- 
Kim F. Storm <storm@cua.dk> http://www.cua.dk

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2002-07-02 20:47 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-07-02 19:08 Offering the differences on exit Mario Lang
2002-07-02 20:47 ` Kim F. Storm
2002-07-02 20:25   ` Mario Lang

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).