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'.
34 (eval-when-compile (require 'cl
))
37 "Comparing files with `diff'."
41 (defcustom diff-switches
(purecopy "-c")
42 "A string or list of strings specifying switches to be passed to diff."
43 :type
'(choice string
(repeat string
))
47 (defcustom diff-command
(purecopy "diff")
48 "The command to use to run diff."
52 ;; prompt if prefix arg present
53 (defun diff-switches ()
54 (if current-prefix-arg
55 (read-string "Diff switches: "
56 (if (stringp diff-switches
)
58 (mapconcat 'identity diff-switches
" ")))))
60 (defun diff-sentinel (code old-temp-file new-temp-file
)
61 "Code run when the diff process exits.
62 CODE is the exit code of the process. It should be 0 only if no diffs
64 (if old-temp-file
(delete-file old-temp-file
))
65 (if new-temp-file
(delete-file new-temp-file
))
67 (goto-char (point-max))
68 (let ((inhibit-read-only t
))
69 (insert (format "\nDiff finished%s. %s\n"
70 (cond ((equal 0 code
) " (no differences)")
71 ((equal 2 code
) " (diff error)")
73 (current-time-string))))))
76 (defun diff (old new
&optional switches no-async
)
77 "Find and display the differences between OLD and NEW files.
78 When called interactively, read OLD and NEW using the minibuffer;
79 the default for NEW is the current buffer's file name, and the
80 default for OLD is a backup file for NEW, if one exists.
81 If NO-ASYNC is non-nil, call diff synchronously.
83 When called interactively with a prefix argument, prompt
84 interactively for diff switches. Otherwise, the switches
85 specified in `diff-switches' are passed to the diff command."
87 (let* ((newf (buffer-file-name))
88 (oldf (file-newest-backup newf
)))
89 (setq newf
(if (and newf
(file-exists-p newf
))
91 (concat "Diff new file (default "
92 (file-name-nondirectory newf
) "): ")
94 (read-file-name "Diff new file: " nil nil t
)))
95 (setq oldf
(if (and oldf
(file-exists-p oldf
))
97 (concat "Diff original file (default "
98 (file-name-nondirectory oldf
) "): ")
99 (file-name-directory oldf
) oldf t
)
100 (read-file-name "Diff original file: "
101 (file-name-directory newf
) nil t
)))
102 (list oldf newf
(diff-switches))))
104 (diff-no-select old new switches no-async
)))
106 (defun diff-file-local-copy (file-or-buf)
107 (if (bufferp file-or-buf
)
108 (with-current-buffer file-or-buf
109 (let ((tempfile (make-temp-file "buffer-content-")))
110 (write-region nil nil tempfile nil
'nomessage
)
112 (file-local-copy file-or-buf
)))
114 (defun diff-better-file-name (file)
115 (if (bufferp file
) file
116 (let ((rel (file-relative-name file
))
117 (abbr (abbreviate-file-name (expand-file-name file
))))
118 (if (< (length abbr
) (length rel
))
122 (defun diff-no-select (old new
&optional switches no-async buf
)
123 ;; Noninteractive helper for creating and reverting diff buffers
124 (setq new
(diff-better-file-name new
)
125 old
(diff-better-file-name old
))
126 (or switches
(setq switches diff-switches
)) ; If not specified, use default.
127 (unless (listp switches
) (setq switches
(list switches
)))
128 (or buf
(setq buf
(get-buffer-create "*Diff*")))
129 (let* ((old-alt (diff-file-local-copy old
))
130 (new-alt (diff-file-local-copy new
))
134 ;; Use explicitly specified switches
136 ,@(mapcar #'shell-quote-argument
138 (when (or old-alt new-alt
)
139 (list "-L" (if (stringp old
)
140 old
(prin1-to-string old
))
141 "-L" (if (stringp new
)
142 new
(prin1-to-string new
))))
143 (list (or old-alt old
)
146 (thisdir default-directory
))
147 (with-current-buffer buf
148 (setq buffer-read-only t
)
149 (buffer-disable-undo (current-buffer))
150 (let ((inhibit-read-only t
))
152 (buffer-enable-undo (current-buffer))
154 (set (make-local-variable 'revert-buffer-function
)
155 (lexical-let ((old old
) (new new
)
158 (lambda (ignore-auto noconfirm
)
159 (diff-no-select old new switches no-async
(current-buffer)))))
160 (setq default-directory thisdir
)
161 (let ((inhibit-read-only t
))
162 (insert command
"\n"))
163 (if (and (not no-async
) (fboundp 'start-process
))
164 (let ((proc (start-process "Diff" buf shell-file-name
165 shell-command-switch command
)))
166 (set-process-filter proc
'diff-process-filter
)
167 (lexical-let ((old-alt old-alt
) (new-alt new-alt
))
168 (set-process-sentinel
169 proc
(lambda (proc msg
)
170 (with-current-buffer (process-buffer proc
)
171 (diff-sentinel (process-exit-status proc
)
172 old-alt new-alt
))))))
173 ;; Async processes aren't available.
174 (let ((inhibit-read-only t
))
176 (call-process shell-file-name nil buf nil
177 shell-command-switch command
)
181 (defun diff-process-filter (proc string
)
182 (with-current-buffer (process-buffer proc
)
183 (let ((moving (= (point) (process-mark proc
))))
185 ;; Insert the text, advancing the process marker.
186 (goto-char (process-mark proc
))
187 (let ((inhibit-read-only t
))
189 (set-marker (process-mark proc
) (point)))
190 (if moving
(goto-char (process-mark proc
))))))
193 (defun diff-backup (file &optional switches
)
194 "Diff this file with its backup file or vice versa.
195 Uses the latest backup, if there are several numerical backups.
196 If this file is a backup, diff it with its original.
197 The backup file is the first file given to `diff'.
198 With prefix arg, prompt for diff switches."
199 (interactive (list (read-file-name "Diff (file with backup): ")
202 (if (backup-file-name-p file
)
204 ori
(file-name-sans-versions file
))
205 (setq bak
(or (diff-latest-backup-file file
)
206 (error "No backup found for %s" file
))
208 (diff bak ori switches
)))
210 (defun diff-latest-backup-file (fn) ; actually belongs into files.el
211 "Return the latest existing backup of FILE, or nil."
212 (let ((handler (find-file-name-handler fn
'diff-latest-backup-file
)))
214 (funcall handler
'diff-latest-backup-file fn
)
215 (file-newest-backup fn
))))
218 (defun diff-buffer-with-file (&optional buffer
)
219 "View the differences between BUFFER and its associated file.
220 This requires the external program `diff' to be in your `exec-path'."
221 (interactive "bBuffer: ")
222 (with-current-buffer (get-buffer (or buffer
(current-buffer)))
223 (diff buffer-file-name
(current-buffer) nil
'noasync
)))
227 ;; arch-tag: 7de2c29b-7ea5-4b85-9b9d-72dd860de2bd
228 ;;; diff.el ends here