Merge from emacs-24; up to 2012-12-10T20:27:33Z!eggert@cs.ucla.edu
[emacs.git] / lisp / vc / diff.el
blob8b4ff7929692c99c3e40b178729d3ac883dfba7e
1 ;;; diff.el --- run `diff' -*- lexical-binding: t -*-
3 ;; Copyright (C) 1992, 1994, 1996, 2001-2013 Free Software Foundation,
4 ;; 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 (declare-function diff-setup-whitespace "diff-mode" ())
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 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 (diff-setup-whitespace)
69 (goto-char (point-min))
70 (save-excursion
71 (goto-char (point-max))
72 (let ((inhibit-read-only t))
73 (insert (format "\nDiff finished%s. %s\n"
74 (cond ((equal 0 code) " (no differences)")
75 ((equal 2 code) " (diff error)")
76 (t ""))
77 (current-time-string))))))
79 ;;;###autoload
80 (defun diff (old new &optional switches no-async)
81 "Find and display the differences between OLD and NEW files.
82 When called interactively, read NEW, then OLD, using the
83 minibuffer. The default for NEW is the current buffer's file
84 name, and the default for OLD is a backup file for NEW, if one
85 exists. If NO-ASYNC is non-nil, call diff synchronously.
87 When called interactively with a prefix argument, prompt
88 interactively for diff switches. Otherwise, the switches
89 specified in the variable `diff-switches' are passed to the diff command."
90 (interactive
91 (let* ((newf (if (and buffer-file-name (file-exists-p buffer-file-name))
92 (read-file-name
93 (concat "Diff new file (default "
94 (file-name-nondirectory buffer-file-name) "): ")
95 nil buffer-file-name t)
96 (read-file-name "Diff new file: " nil nil t)))
97 (oldf (file-newest-backup newf)))
98 (setq oldf (if (and oldf (file-exists-p oldf))
99 (read-file-name
100 (concat "Diff original file (default "
101 (file-name-nondirectory oldf) "): ")
102 (file-name-directory oldf) oldf t)
103 (read-file-name "Diff original file: "
104 (file-name-directory newf) nil t)))
105 (list oldf newf (diff-switches))))
106 (display-buffer
107 (diff-no-select old new switches no-async)))
109 (defun diff-file-local-copy (file-or-buf)
110 (if (bufferp file-or-buf)
111 (with-current-buffer file-or-buf
112 (let ((tempfile (make-temp-file "buffer-content-")))
113 (write-region nil nil tempfile nil 'nomessage)
114 tempfile))
115 (file-local-copy file-or-buf)))
117 (defun diff-no-select (old new &optional switches no-async buf)
118 ;; Noninteractive helper for creating and reverting diff buffers
119 (unless (bufferp new) (setq new (expand-file-name new)))
120 (unless (bufferp old) (setq old (expand-file-name old)))
121 (or switches (setq switches diff-switches)) ; If not specified, use default.
122 (unless (listp switches) (setq switches (list switches)))
123 (or buf (setq buf (get-buffer-create "*Diff*")))
124 (let* ((old-alt (diff-file-local-copy old))
125 (new-alt (diff-file-local-copy new))
126 (command
127 (mapconcat 'identity
128 `(,diff-command
129 ;; Use explicitly specified switches
130 ,@switches
131 ,@(mapcar #'shell-quote-argument
132 (nconc
133 (when (or old-alt new-alt)
134 (list "-L" (if (stringp old)
135 old (prin1-to-string old))
136 "-L" (if (stringp new)
137 new (prin1-to-string new))))
138 (list (or old-alt old)
139 (or new-alt new)))))
140 " "))
141 (thisdir default-directory))
142 (with-current-buffer buf
143 (setq buffer-read-only t)
144 (buffer-disable-undo (current-buffer))
145 (let ((inhibit-read-only t))
146 (erase-buffer))
147 (buffer-enable-undo (current-buffer))
148 (diff-mode)
149 (set (make-local-variable 'revert-buffer-function)
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 (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 ;;;###autoload
202 (defun diff-latest-backup-file (fn)
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