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, 2011 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 &optional 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 (if (and buffer-file-name
(file-exists-p buffer-file-name
))
89 (concat "Diff new file (default "
90 (file-name-nondirectory buffer-file-name
) "): ")
91 nil buffer-file-name t
)
92 (read-file-name "Diff new file: " nil nil t
)))
93 (oldf (file-newest-backup newf
)))
94 (setq oldf
(if (and oldf
(file-exists-p oldf
))
96 (concat "Diff original file (default "
97 (file-name-nondirectory oldf
) "): ")
98 (file-name-directory oldf
) oldf t
)
99 (read-file-name "Diff original file: "
100 (file-name-directory newf
) nil t
)))
101 (list oldf newf
(diff-switches))))
103 (diff-no-select old new switches no-async
)))
105 (defun diff-file-local-copy (file-or-buf)
106 (if (bufferp file-or-buf
)
107 (with-current-buffer file-or-buf
108 (let ((tempfile (make-temp-file "buffer-content-")))
109 (write-region nil nil tempfile nil
'nomessage
)
111 (file-local-copy file-or-buf
)))
113 (defun diff-no-select (old new
&optional switches no-async buf
)
114 ;; Noninteractive helper for creating and reverting diff buffers
115 (unless (bufferp new
) (setq new
(expand-file-name new
)))
116 (unless (bufferp old
) (setq old
(expand-file-name old
)))
117 (or switches
(setq switches diff-switches
)) ; If not specified, use default.
118 (unless (listp switches
) (setq switches
(list switches
)))
119 (or buf
(setq buf
(get-buffer-create "*Diff*")))
120 (let* ((old-alt (diff-file-local-copy old
))
121 (new-alt (diff-file-local-copy new
))
125 ;; Use explicitly specified switches
127 ,@(mapcar #'shell-quote-argument
129 (when (or old-alt new-alt
)
130 (list "-L" (if (stringp old
)
131 old
(prin1-to-string old
))
132 "-L" (if (stringp new
)
133 new
(prin1-to-string new
))))
134 (list (or old-alt old
)
137 (thisdir default-directory
))
138 (with-current-buffer buf
139 (setq buffer-read-only t
)
140 (buffer-disable-undo (current-buffer))
141 (let ((inhibit-read-only t
))
143 (buffer-enable-undo (current-buffer))
145 (set (make-local-variable 'revert-buffer-function
)
146 (lexical-let ((old old
) (new new
)
149 (lambda (ignore-auto noconfirm
)
150 (diff-no-select old new switches no-async
(current-buffer)))))
151 (setq default-directory thisdir
)
152 (let ((inhibit-read-only t
))
153 (insert command
"\n"))
154 (if (and (not no-async
) (fboundp 'start-process
))
155 (let ((proc (start-process "Diff" buf shell-file-name
156 shell-command-switch command
)))
157 (set-process-filter proc
'diff-process-filter
)
158 (lexical-let ((old-alt old-alt
) (new-alt new-alt
))
159 (set-process-sentinel
160 proc
(lambda (proc msg
)
161 (with-current-buffer (process-buffer proc
)
162 (diff-sentinel (process-exit-status proc
)
163 old-alt new-alt
))))))
164 ;; Async processes aren't available.
165 (let ((inhibit-read-only t
))
167 (call-process shell-file-name nil buf nil
168 shell-command-switch command
)
172 (defun diff-process-filter (proc string
)
173 (with-current-buffer (process-buffer proc
)
174 (let ((moving (= (point) (process-mark proc
))))
176 ;; Insert the text, advancing the process marker.
177 (goto-char (process-mark proc
))
178 (let ((inhibit-read-only t
))
180 (set-marker (process-mark proc
) (point)))
181 (if moving
(goto-char (process-mark proc
))))))
184 (defun diff-backup (file &optional switches
)
185 "Diff this file with its backup file or vice versa.
186 Uses the latest backup, if there are several numerical backups.
187 If this file is a backup, diff it with its original.
188 The backup file is the first file given to `diff'.
189 With prefix arg, prompt for diff switches."
190 (interactive (list (read-file-name "Diff (file with backup): ")
193 (if (backup-file-name-p file
)
195 ori
(file-name-sans-versions file
))
196 (setq bak
(or (diff-latest-backup-file file
)
197 (error "No backup found for %s" file
))
199 (diff bak ori switches
)))
201 (defun diff-latest-backup-file (fn) ; actually belongs into files.el
202 "Return the latest existing backup of FILE, or nil."
203 (let ((handler (find-file-name-handler fn
'diff-latest-backup-file
)))
205 (funcall handler
'diff-latest-backup-file fn
)
206 (file-newest-backup fn
))))
209 (defun diff-buffer-with-file (&optional buffer
)
210 "View the differences between BUFFER and its associated file.
211 This requires the external program `diff' to be in your `exec-path'."
212 (interactive "bBuffer: ")
213 (with-current-buffer (get-buffer (or buffer
(current-buffer)))
214 (diff buffer-file-name
(current-buffer) nil
'noasync
)))
218 ;;; diff.el ends here