]> code.delx.au - gnu-emacs/blob - lisp/vc/diff.el
b29f9c10b18159da02fccf98a086fd5d884400a3
[gnu-emacs] / lisp / vc / diff.el
1 ;;; diff.el --- run `diff' in compilation-mode
2
3 ;; Copyright (C) 1992, 1994, 1996, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;; 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5
6 ;; Author: Frank Bresz
7 ;; (according to authors.el)
8 ;; Maintainer: FSF
9 ;; Keywords: unix, vc, tools
10
11 ;; This file is part of GNU Emacs.
12
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.
17
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.
22
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/>.
25
26 ;;; Commentary:
27
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'.
31
32 ;;; Code:
33
34 (eval-when-compile (require 'cl))
35
36 (defgroup diff nil
37 "Comparing files with `diff'."
38 :group 'tools)
39
40 ;;;###autoload
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))
44 :group 'diff)
45
46 ;;;###autoload
47 (defcustom diff-command (purecopy "diff")
48 "The command to use to run diff."
49 :type 'string
50 :group 'diff)
51
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)
57 diff-switches
58 (mapconcat 'identity diff-switches " ")))))
59
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
63 were found.
64 If optional args OLD-TEMP-FILE and/or NEW-TEMP-FILE are non-nil,
65 delete the temporary files so named."
66 (if old-temp-file (delete-file old-temp-file))
67 (if new-temp-file (delete-file new-temp-file))
68 (save-excursion
69 (goto-char (point-max))
70 (let ((inhibit-read-only t))
71 (insert (format "\nDiff finished%s. %s\n"
72 (cond ((equal 0 code) " (no differences)")
73 ((equal 2 code) " (diff error)")
74 (t ""))
75 (current-time-string))))))
76
77 ;;;###autoload
78 (defun diff (old new &optional switches no-async)
79 "Find and display the differences between OLD and NEW files.
80 When called interactively, read OLD and NEW using the minibuffer;
81 the default for NEW is the current buffer's file name, and the
82 default for OLD is a backup file for NEW, if one exists.
83 If NO-ASYNC is non-nil, call diff synchronously.
84
85 When called interactively with a prefix argument, prompt
86 interactively for diff switches. Otherwise, the switches
87 specified in `diff-switches' are passed to the diff command."
88 (interactive
89 (let* ((newf (if (and buffer-file-name (file-exists-p buffer-file-name))
90 (read-file-name
91 (concat "Diff new file (default "
92 (file-name-nondirectory buffer-file-name) "): ")
93 nil buffer-file-name t)
94 (read-file-name "Diff new file: " nil nil t)))
95 (oldf (file-newest-backup newf)))
96 (setq oldf (if (and oldf (file-exists-p oldf))
97 (read-file-name
98 (concat "Diff original file (default "
99 (file-name-nondirectory oldf) "): ")
100 (file-name-directory oldf) oldf t)
101 (read-file-name "Diff original file: "
102 (file-name-directory newf) nil t)))
103 (list oldf newf (diff-switches))))
104 (display-buffer
105 (diff-no-select old new switches no-async)))
106
107 (defun diff-file-local-copy (file-or-buf)
108 (if (bufferp file-or-buf)
109 (with-current-buffer file-or-buf
110 (let ((tempfile (make-temp-file "buffer-content-")))
111 (write-region nil nil tempfile nil 'nomessage)
112 tempfile))
113 (file-local-copy file-or-buf)))
114
115 (defun diff-no-select (old new &optional switches no-async buf)
116 ;; Noninteractive helper for creating and reverting diff buffers
117 (unless (bufferp new) (setq new (expand-file-name new)))
118 (unless (bufferp old) (setq old (expand-file-name old)))
119 (or switches (setq switches diff-switches)) ; If not specified, use default.
120 (unless (listp switches) (setq switches (list switches)))
121 (or buf (setq buf (get-buffer-create "*Diff*")))
122 (let* ((old-alt (diff-file-local-copy old))
123 (new-alt (diff-file-local-copy new))
124 (command
125 (mapconcat 'identity
126 `(,diff-command
127 ;; Use explicitly specified switches
128 ,@switches
129 ,@(mapcar #'shell-quote-argument
130 (nconc
131 (when (or old-alt new-alt)
132 (list "-L" (if (stringp old)
133 old (prin1-to-string old))
134 "-L" (if (stringp new)
135 new (prin1-to-string new))))
136 (list (or old-alt old)
137 (or new-alt new)))))
138 " "))
139 (thisdir default-directory))
140 (with-current-buffer buf
141 (setq buffer-read-only t)
142 (buffer-disable-undo (current-buffer))
143 (let ((inhibit-read-only t))
144 (erase-buffer))
145 (buffer-enable-undo (current-buffer))
146 (diff-mode)
147 (set (make-local-variable 'revert-buffer-function)
148 (lexical-let ((old old) (new new)
149 (switches switches)
150 (no-async no-async))
151 (lambda (ignore-auto noconfirm)
152 (diff-no-select old new switches no-async (current-buffer)))))
153 (setq default-directory thisdir)
154 (let ((inhibit-read-only t))
155 (insert command "\n"))
156 (if (and (not no-async) (fboundp 'start-process))
157 (let ((proc (start-process "Diff" buf shell-file-name
158 shell-command-switch command)))
159 (set-process-filter proc 'diff-process-filter)
160 (lexical-let ((old-alt old-alt) (new-alt new-alt))
161 (set-process-sentinel
162 proc (lambda (proc msg)
163 (with-current-buffer (process-buffer proc)
164 (diff-sentinel (process-exit-status proc)
165 old-alt new-alt))))))
166 ;; Async processes aren't available.
167 (let ((inhibit-read-only t))
168 (diff-sentinel
169 (call-process shell-file-name nil buf nil
170 shell-command-switch command)
171 old-alt new-alt))))
172 buf))
173
174 (defun diff-process-filter (proc string)
175 (with-current-buffer (process-buffer proc)
176 (let ((moving (= (point) (process-mark proc))))
177 (save-excursion
178 ;; Insert the text, advancing the process marker.
179 (goto-char (process-mark proc))
180 (let ((inhibit-read-only t))
181 (insert string))
182 (set-marker (process-mark proc) (point)))
183 (if moving (goto-char (process-mark proc))))))
184
185 ;;;###autoload
186 (defun diff-backup (file &optional switches)
187 "Diff this file with its backup file or vice versa.
188 Uses the latest backup, if there are several numerical backups.
189 If this file is a backup, diff it with its original.
190 The backup file is the first file given to `diff'.
191 With prefix arg, prompt for diff switches."
192 (interactive (list (read-file-name "Diff (file with backup): ")
193 (diff-switches)))
194 (let (bak ori)
195 (if (backup-file-name-p file)
196 (setq bak file
197 ori (file-name-sans-versions file))
198 (setq bak (or (diff-latest-backup-file file)
199 (error "No backup found for %s" file))
200 ori file))
201 (diff bak ori switches)))
202
203 (defun diff-latest-backup-file (fn) ; actually belongs into files.el
204 "Return the latest existing backup of FILE, or nil."
205 (let ((handler (find-file-name-handler fn 'diff-latest-backup-file)))
206 (if handler
207 (funcall handler 'diff-latest-backup-file fn)
208 (file-newest-backup fn))))
209
210 ;;;###autoload
211 (defun diff-buffer-with-file (&optional buffer)
212 "View the differences between BUFFER and its associated file.
213 This requires the external program `diff' to be in your `exec-path'."
214 (interactive "bBuffer: ")
215 (with-current-buffer (get-buffer (or buffer (current-buffer)))
216 (diff buffer-file-name (current-buffer) nil 'noasync)))
217
218 (provide 'diff)
219
220 ;;; diff.el ends here