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, vc, 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 (if diff-old-temp-file
(delete-file diff-old-temp-file
))
68 (if diff-new-temp-file
(delete-file diff-new-temp-file
))
70 (goto-char (point-max))
71 (let ((inhibit-read-only t
))
72 (insert (format "\nDiff finished%s. %s\n"
73 (cond ((equal 0 code
) " (no differences)")
74 ((equal 2 code
) " (diff error)")
76 (current-time-string))))))
78 (defvar diff-old-file nil
)
79 (defvar diff-new-file nil
)
80 (defvar diff-extra-args nil
)
83 (defun diff (old new
&optional switches no-async
)
84 "Find and display the differences between OLD and NEW files.
85 When called interactively, read OLD and NEW using the minibuffer;
86 the default for NEW is the current buffer's file name, and the
87 default for OLD is a backup file for NEW, if one exists.
88 If NO-ASYNC is non-nil, call diff synchronously.
90 When called interactively with a prefix argument, prompt
91 interactively for diff switches. Otherwise, the switches
92 specified in `diff-switches' are passed to the diff command."
95 (setq newf
(buffer-file-name)
96 newf
(if (and newf
(file-exists-p newf
))
98 (concat "Diff new file (default "
99 (file-name-nondirectory newf
) "): ")
101 (read-file-name "Diff new file: " nil nil t
)))
102 (setq oldf
(file-newest-backup newf
)
103 oldf
(if (and oldf
(file-exists-p oldf
))
105 (concat "Diff original file (default "
106 (file-name-nondirectory oldf
) "): ")
107 (file-name-directory oldf
) oldf t
)
108 (read-file-name "Diff original file: "
109 (file-name-directory newf
) nil t
)))
110 (list oldf newf
(diff-switches))))
111 (diff-into-buffer nil old new switches no-async
))
113 (defun diff-into-buffer (buf old new
&optional switches no-async
)
114 ;; Noninteractive helper for creating and reverting diff buffers.
115 (setq new
(expand-file-name new
)
116 old
(expand-file-name old
))
117 (or switches
(setq switches diff-switches
)) ; If not specified, use default.
118 (or buf
(setq buf
(get-buffer-create "*Diff*")))
119 (let* ((old-alt (file-local-copy old
))
120 (new-alt (file-local-copy new
))
124 ;; Use explicitly specified switches
125 ,@(if (listp switches
) switches
(list switches
))
126 ,@(if (or old-alt new-alt
)
127 (list "-L" old
"-L" new
))
128 ,(shell-quote-argument (or old-alt old
))
129 ,(shell-quote-argument (or new-alt new
)))
131 (thisdir default-directory
)
136 (setq buffer-read-only nil
)
137 (buffer-disable-undo (current-buffer))
138 (let ((inhibit-read-only t
))
140 (buffer-enable-undo (current-buffer))
142 ;; Use below 2 vars for backward-compatibility.
143 (set (make-local-variable 'diff-old-file
) old
)
144 (set (make-local-variable 'diff-new-file
) new
)
145 (set (make-local-variable 'diff-extra-args
) (list switches no-async
))
146 (set (make-local-variable 'revert-buffer-function
)
147 (lambda (ignore-auto noconfirm
)
148 (apply 'diff diff-old-file diff-new-file diff-extra-args
)))
149 (set (make-local-variable 'diff-old-temp-file
) old-alt
)
150 (set (make-local-variable 'diff-new-temp-file
) new-alt
)
151 (setq default-directory thisdir
)
152 (let ((inhibit-read-only t
))
153 (insert command
"\n"))
154 (if (and (not no-async
) (fboundp 'start-process
))
156 (setq proc
(start-process "Diff" buf shell-file-name
157 shell-command-switch command
))
158 (set-process-filter proc
'diff-process-filter
)
159 (set-process-sentinel
160 proc
(lambda (proc msg
)
161 (with-current-buffer (process-buffer proc
)
162 (diff-sentinel (process-exit-status proc
))))))
163 ;; Async processes aren't available.
164 (let ((inhibit-read-only t
))
166 (call-process shell-file-name nil buf nil
167 shell-command-switch command
)))))
170 (defun diff-process-filter (proc string
)
171 (with-current-buffer (process-buffer proc
)
172 (let ((moving (= (point) (process-mark proc
))))
174 ;; Insert the text, advancing the process marker.
175 (goto-char (process-mark proc
))
176 (let ((inhibit-read-only t
))
178 (set-marker (process-mark proc
) (point)))
179 (if moving
(goto-char (process-mark proc
))))))
182 (defun diff-backup (file &optional switches
)
183 "Diff this file with its backup file or vice versa.
184 Uses the latest backup, if there are several numerical backups.
185 If this file is a backup, diff it with its original.
186 The backup file is the first file given to `diff'.
187 With prefix arg, prompt for diff switches."
188 (interactive (list (read-file-name "Diff (file with backup): ")
191 (if (backup-file-name-p file
)
193 ori
(file-name-sans-versions file
))
194 (setq bak
(or (diff-latest-backup-file file
)
195 (error "No backup found for %s" file
))
197 (diff bak ori switches
)))
199 (defun diff-latest-backup-file (fn) ; actually belongs into files.el
200 "Return the latest existing backup of FILE, or nil."
201 (let ((handler (find-file-name-handler fn
'diff-latest-backup-file
)))
203 (funcall handler
'diff-latest-backup-file fn
)
204 (file-newest-backup fn
))))
208 ;; arch-tag: 7de2c29b-7ea5-4b85-9b9d-72dd860de2bd
209 ;;; diff.el ends here