Don't mess with *temp*.
[emacs.git] / lisp / vc / diff.el
blob0a853c5d9024eea9128d3943888a0db354285bd8
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.
6 ;; Author: Frank Bresz
7 ;; (according to authors.el)
8 ;; Maintainer: FSF
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/>.
26 ;;; Commentary:
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'.
32 ;;; Code:
34 (eval-when-compile (require 'cl))
36 (defgroup diff nil
37 "Comparing files with `diff'."
38 :group 'tools)
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)
46 ;;;###autoload
47 (defcustom diff-command (purecopy "diff")
48 "The command to use to run diff."
49 :type 'string
50 :group '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)
57 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
63 were found."
64 (if old-temp-file (delete-file old-temp-file))
65 (if new-temp-file (delete-file new-temp-file))
66 (save-excursion
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)")
72 (t ""))
73 (current-time-string))))))
75 ;;;###autoload
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."
86 (interactive
87 (let* ((newf (if (and buffer-file-name (file-exists-p buffer-file-name))
88 (read-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))
95 (read-file-name
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))))
102 (display-buffer
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)
110 tempfile))
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))
122 (command
123 (mapconcat 'identity
124 `(,diff-command
125 ;; Use explicitly specified switches
126 ,@switches
127 ,@(mapcar #'shell-quote-argument
128 (nconc
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)
135 (or new-alt new)))))
136 " "))
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))
142 (erase-buffer))
143 (buffer-enable-undo (current-buffer))
144 (diff-mode)
145 (set (make-local-variable 'revert-buffer-function)
146 (lexical-let ((old old) (new new)
147 (switches switches)
148 (no-async no-async))
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))
166 (diff-sentinel
167 (call-process shell-file-name nil buf nil
168 shell-command-switch command)
169 old-alt new-alt))))
170 buf))
172 (defun diff-process-filter (proc string)
173 (with-current-buffer (process-buffer proc)
174 (let ((moving (= (point) (process-mark proc))))
175 (save-excursion
176 ;; Insert the text, advancing the process marker.
177 (goto-char (process-mark proc))
178 (let ((inhibit-read-only t))
179 (insert string))
180 (set-marker (process-mark proc) (point)))
181 (if moving (goto-char (process-mark proc))))))
183 ;;;###autoload
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): ")
191 (diff-switches)))
192 (let (bak ori)
193 (if (backup-file-name-p file)
194 (setq bak 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))
198 ori 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)))
204 (if handler
205 (funcall handler 'diff-latest-backup-file fn)
206 (file-newest-backup fn))))
208 ;;;###autoload
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)))
216 (provide 'diff)
218 ;;; diff.el ends here