1 ;;; diff.el --- run `diff' in compilation-mode
3 ;; Copyright (C) 1992, 1994, 1996, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;; 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
7 ;; (according to authors.el)
9 ;; Keywords: unix, tools
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; This package helps you explore differences between files, using the
29 ;; UNIX command diff(1). The commands are `diff' and `diff-backup'.
30 ;; You can specify options with `diff-switches'.
35 "Comparing files with `diff'."
39 (defcustom diff-switches
(purecopy "-c")
40 "A string or list of strings specifying switches to be passed to diff."
41 :type
'(choice string
(repeat string
))
45 (defcustom diff-command
(purecopy "diff")
46 "The command to use to run diff."
50 (defvar diff-old-temp-file nil
51 "This is the name of a temp file to be deleted after diff finishes.")
52 (defvar diff-new-temp-file nil
53 "This is the name of a temp file to be deleted after diff finishes.")
55 ;; prompt if prefix arg present
56 (defun diff-switches ()
57 (if current-prefix-arg
58 (read-string "Diff switches: "
59 (if (stringp diff-switches
)
61 (mapconcat 'identity diff-switches
" ")))))
63 (defun diff-sentinel (code)
64 "Code run when the diff process exits.
65 CODE is the exit code of the process. It should be 0 only if no diffs
67 (let (delete-by-moving-to-trash)
68 (if diff-old-temp-file
(delete-file diff-old-temp-file
))
69 (if diff-new-temp-file
(delete-file diff-new-temp-file
)))
71 (goto-char (point-max))
72 (let ((inhibit-read-only t
))
73 (insert (format "\nDiff finished%s. %s\n"
74 (cond ((equal 0 code
) " (no differences)")
75 ((equal 2 code
) " (diff error)")
77 (current-time-string))))))
79 (defvar diff-old-file nil
)
80 (defvar diff-new-file nil
)
81 (defvar diff-extra-args nil
)
84 (defun diff (old new
&optional switches no-async
)
85 "Find and display the differences between OLD and NEW files.
86 When called interactively, read OLD and NEW using the minibuffer;
87 the default for NEW is the current buffer's file name, and the
88 default for OLD is a backup file for NEW, if one exists.
89 If NO-ASYNC is non-nil, call diff synchronously.
91 When called interactively with a prefix argument, prompt
92 interactively for diff switches. Otherwise, the switches
93 specified in `diff-switches' are passed to the diff command."
96 (setq newf
(buffer-file-name)
97 newf
(if (and newf
(file-exists-p newf
))
99 (concat "Diff new file (default "
100 (file-name-nondirectory newf
) "): ")
102 (read-file-name "Diff new file: " nil nil t
)))
103 (setq oldf
(file-newest-backup newf
)
104 oldf
(if (and oldf
(file-exists-p oldf
))
106 (concat "Diff original file (default "
107 (file-name-nondirectory oldf
) "): ")
108 (file-name-directory oldf
) oldf t
)
109 (read-file-name "Diff original file: "
110 (file-name-directory newf
) nil t
)))
111 (list oldf newf
(diff-switches))))
112 (setq new
(expand-file-name new
)
113 old
(expand-file-name old
))
114 (or switches
(setq switches diff-switches
)) ; If not specified, use default.
115 (let* ((old-alt (file-local-copy old
))
116 (new-alt (file-local-copy new
))
120 ;; Use explicitly specified switches
121 ,@(if (listp switches
) switches
(list switches
))
122 ,@(if (or old-alt new-alt
)
123 (list "-L" old
"-L" new
))
124 ,(shell-quote-argument (or old-alt old
))
125 ,(shell-quote-argument (or new-alt new
)))
127 (buf (get-buffer-create "*Diff*"))
128 (thisdir default-directory
)
133 (setq buffer-read-only nil
)
134 (buffer-disable-undo (current-buffer))
135 (let ((inhibit-read-only t
))
137 (buffer-enable-undo (current-buffer))
139 ;; Use below 2 vars for backward-compatibility.
140 (set (make-local-variable 'diff-old-file
) old
)
141 (set (make-local-variable 'diff-new-file
) new
)
142 (set (make-local-variable 'diff-extra-args
) (list switches no-async
))
143 (set (make-local-variable 'revert-buffer-function
)
144 (lambda (ignore-auto noconfirm
)
145 (apply 'diff diff-old-file diff-new-file diff-extra-args
)))
146 (set (make-local-variable 'diff-old-temp-file
) old-alt
)
147 (set (make-local-variable 'diff-new-temp-file
) new-alt
)
148 (setq default-directory thisdir
)
149 (let ((inhibit-read-only t
))
150 (insert command
"\n"))
151 (if (and (not no-async
) (fboundp 'start-process
))
153 (setq proc
(start-process "Diff" buf shell-file-name
154 shell-command-switch command
))
155 (set-process-filter proc
'diff-process-filter
)
156 (set-process-sentinel
157 proc
(lambda (proc msg
)
158 (with-current-buffer (process-buffer proc
)
159 (diff-sentinel (process-exit-status proc
))))))
160 ;; Async processes aren't available.
161 (let ((inhibit-read-only t
))
163 (call-process shell-file-name nil buf nil
164 shell-command-switch command
)))))
167 (defun diff-process-filter (proc string
)
168 (with-current-buffer (process-buffer proc
)
169 (let ((moving (= (point) (process-mark proc
))))
171 ;; Insert the text, advancing the process marker.
172 (goto-char (process-mark proc
))
173 (let ((inhibit-read-only t
))
175 (set-marker (process-mark proc
) (point)))
176 (if moving
(goto-char (process-mark proc
))))))
179 (defun diff-backup (file &optional switches
)
180 "Diff this file with its backup file or vice versa.
181 Uses the latest backup, if there are several numerical backups.
182 If this file is a backup, diff it with its original.
183 The backup file is the first file given to `diff'.
184 With prefix arg, prompt for diff switches."
185 (interactive (list (read-file-name "Diff (file with backup): ")
188 (if (backup-file-name-p file
)
190 ori
(file-name-sans-versions file
))
191 (setq bak
(or (diff-latest-backup-file file
)
192 (error "No backup found for %s" file
))
194 (diff bak ori switches
)))
196 (defun diff-latest-backup-file (fn) ; actually belongs into files.el
197 "Return the latest existing backup of FILE, or nil."
198 (let ((handler (find-file-name-handler fn
'diff-latest-backup-file
)))
200 (funcall handler
'diff-latest-backup-file fn
)
201 (file-newest-backup fn
))))
205 ;; arch-tag: 7de2c29b-7ea5-4b85-9b9d-72dd860de2bd
206 ;;; diff.el ends here