* lisp/vc/vc-svn.el (vc-svn-diff): Use `diff-command' instead of the
[emacs.git] / lisp / vc / diff.el
blob4bc3466f13fbace40c7cae051db483b833104de6
1 ;;; diff.el --- run `diff' in compilation-mode
3 ;; Copyright (C) 1992, 1994, 1996, 2001-2011 Free Software Foundation, Inc.
5 ;; Author: Frank Bresz
6 ;; (according to authors.el)
7 ;; Maintainer: FSF
8 ;; Keywords: unix, vc, tools
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This package helps you explore differences between files, using the
28 ;; UNIX command diff(1). The commands are `diff' and `diff-backup'.
29 ;; You can specify options with `diff-switches'.
31 ;;; Code:
33 (eval-when-compile (require 'cl))
35 (defgroup diff nil
36 "Comparing files with `diff'."
37 :group 'tools)
39 ;;;###autoload
40 (defcustom diff-switches (purecopy "-c")
41 "A string or list of strings specifying switches to be passed to diff."
42 :type '(choice string (repeat string))
43 :group 'diff)
45 ;;;###autoload
46 (defcustom diff-command (purecopy "diff")
47 "The command to use to run diff."
48 :type 'string
49 :group 'diff)
51 ;; prompt if prefix arg present
52 (defun diff-switches ()
53 (if current-prefix-arg
54 (read-string "Diff switches: "
55 (if (stringp diff-switches)
56 diff-switches
57 (mapconcat 'identity diff-switches " ")))))
59 (defun diff-sentinel (code &optional old-temp-file new-temp-file)
60 "Code run when the diff process exits.
61 CODE is the exit code of the process. It should be 0 only if no diffs
62 were found.
63 If optional args OLD-TEMP-FILE and/or NEW-TEMP-FILE are non-nil,
64 delete the temporary files so named."
65 (if old-temp-file (delete-file old-temp-file))
66 (if new-temp-file (delete-file new-temp-file))
67 (save-excursion
68 (goto-char (point-max))
69 (let ((inhibit-read-only t))
70 (insert (format "\nDiff finished%s. %s\n"
71 (cond ((equal 0 code) " (no differences)")
72 ((equal 2 code) " (diff error)")
73 (t ""))
74 (current-time-string))))))
76 ;;;###autoload
77 (defun diff (old new &optional switches no-async)
78 "Find and display the differences between OLD and NEW files.
79 When called interactively, read OLD and NEW using the minibuffer;
80 the default for NEW is the current buffer's file name, and the
81 default for OLD is a backup file for NEW, if one exists.
82 If NO-ASYNC is non-nil, call diff synchronously.
84 When called interactively with a prefix argument, prompt
85 interactively for diff switches. Otherwise, the switches
86 specified in `diff-switches' are passed to the diff command."
87 (interactive
88 (let* ((newf (if (and buffer-file-name (file-exists-p buffer-file-name))
89 (read-file-name
90 (concat "Diff new file (default "
91 (file-name-nondirectory buffer-file-name) "): ")
92 nil buffer-file-name t)
93 (read-file-name "Diff new file: " nil nil t)))
94 (oldf (file-newest-backup newf)))
95 (setq oldf (if (and oldf (file-exists-p oldf))
96 (read-file-name
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))))
103 (display-buffer
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)
111 tempfile))
112 (file-local-copy file-or-buf)))
114 (defun diff-no-select (old new &optional switches no-async buf)
115 ;; Noninteractive helper for creating and reverting diff buffers
116 (unless (bufferp new) (setq new (expand-file-name new)))
117 (unless (bufferp old) (setq old (expand-file-name old)))
118 (or switches (setq switches diff-switches)) ; If not specified, use default.
119 (unless (listp switches) (setq switches (list switches)))
120 (or buf (setq buf (get-buffer-create "*Diff*")))
121 (let* ((old-alt (diff-file-local-copy old))
122 (new-alt (diff-file-local-copy new))
123 (command
124 (mapconcat 'identity
125 `(,diff-command
126 ;; Use explicitly specified switches
127 ,@switches
128 ,@(mapcar #'shell-quote-argument
129 (nconc
130 (when (or old-alt new-alt)
131 (list "-L" (if (stringp old)
132 old (prin1-to-string old))
133 "-L" (if (stringp new)
134 new (prin1-to-string new))))
135 (list (or old-alt old)
136 (or new-alt new)))))
137 " "))
138 (thisdir default-directory))
139 (with-current-buffer buf
140 (setq buffer-read-only t)
141 (buffer-disable-undo (current-buffer))
142 (let ((inhibit-read-only t))
143 (erase-buffer))
144 (buffer-enable-undo (current-buffer))
145 (diff-mode)
146 (set (make-local-variable 'revert-buffer-function)
147 (lexical-let ((old old) (new new)
148 (switches switches)
149 (no-async no-async))
150 (lambda (ignore-auto noconfirm)
151 (diff-no-select old new switches no-async (current-buffer)))))
152 (setq default-directory thisdir)
153 (let ((inhibit-read-only t))
154 (insert command "\n"))
155 (if (and (not no-async) (fboundp 'start-process))
156 (let ((proc (start-process "Diff" buf shell-file-name
157 shell-command-switch command)))
158 (set-process-filter proc 'diff-process-filter)
159 (lexical-let ((old-alt old-alt) (new-alt new-alt))
160 (set-process-sentinel
161 proc (lambda (proc msg)
162 (with-current-buffer (process-buffer proc)
163 (diff-sentinel (process-exit-status proc)
164 old-alt new-alt))))))
165 ;; Async processes aren't available.
166 (let ((inhibit-read-only t))
167 (diff-sentinel
168 (call-process shell-file-name nil buf nil
169 shell-command-switch command)
170 old-alt new-alt))))
171 buf))
173 (defun diff-process-filter (proc string)
174 (with-current-buffer (process-buffer proc)
175 (let ((moving (= (point) (process-mark proc))))
176 (save-excursion
177 ;; Insert the text, advancing the process marker.
178 (goto-char (process-mark proc))
179 (let ((inhibit-read-only t))
180 (insert string))
181 (set-marker (process-mark proc) (point)))
182 (if moving (goto-char (process-mark proc))))))
184 ;;;###autoload
185 (defun diff-backup (file &optional switches)
186 "Diff this file with its backup file or vice versa.
187 Uses the latest backup, if there are several numerical backups.
188 If this file is a backup, diff it with its original.
189 The backup file is the first file given to `diff'.
190 With prefix arg, prompt for diff switches."
191 (interactive (list (read-file-name "Diff (file with backup): ")
192 (diff-switches)))
193 (let (bak ori)
194 (if (backup-file-name-p file)
195 (setq bak file
196 ori (file-name-sans-versions file))
197 (setq bak (or (diff-latest-backup-file file)
198 (error "No backup found for %s" file))
199 ori file))
200 (diff bak ori switches)))
202 (defun diff-latest-backup-file (fn) ; actually belongs into files.el
203 "Return the latest existing backup of FILE, or nil."
204 (let ((handler (find-file-name-handler fn 'diff-latest-backup-file)))
205 (if handler
206 (funcall handler 'diff-latest-backup-file fn)
207 (file-newest-backup fn))))
209 ;;;###autoload
210 (defun diff-buffer-with-file (&optional buffer)
211 "View the differences between BUFFER and its associated file.
212 This requires the external program `diff' to be in your `exec-path'."
213 (interactive "bBuffer: ")
214 (with-current-buffer (get-buffer (or buffer (current-buffer)))
215 (diff buffer-file-name (current-buffer) nil 'noasync)))
217 (provide 'diff)
219 ;;; diff.el ends here