]> code.delx.au - gnu-emacs/blob - lisp/vc/diff.el
Merge from emacs-24; up to 2012-04-24T08:35:02Z!lekktu@gmail.com
[gnu-emacs] / lisp / vc / diff.el
1 ;;; diff.el --- run `diff' -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 1992, 1994, 1996, 2001-2012 Free Software Foundation, Inc.
4
5 ;; Author: Frank Bresz
6 ;; (according to authors.el)
7 ;; Maintainer: FSF
8 ;; Keywords: unix, vc, tools
9
10 ;; This file is part of GNU Emacs.
11
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.
16
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.
21
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/>.
24
25 ;;; Commentary:
26
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'.
30
31 ;;; Code:
32
33 (declare-function diff-setup-whitespace "diff-mode" ())
34
35 (eval-when-compile (require 'cl))
36
37 (defgroup diff nil
38 "Comparing files with `diff'."
39 :group 'tools)
40
41 ;;;###autoload
42 (defcustom diff-switches (purecopy "-c")
43 "A string or list of strings specifying switches to be passed to diff."
44 :type '(choice string (repeat string))
45 :group 'diff)
46
47 ;;;###autoload
48 (defcustom diff-command (purecopy "diff")
49 "The command to use to run diff."
50 :type 'string
51 :group 'diff)
52
53 ;; prompt if prefix arg present
54 (defun diff-switches ()
55 (if current-prefix-arg
56 (read-string "Diff switches: "
57 (if (stringp diff-switches)
58 diff-switches
59 (mapconcat 'identity diff-switches " ")))))
60
61 (defun diff-sentinel (code &optional old-temp-file new-temp-file)
62 "Code run when the diff process exits.
63 CODE is the exit code of the process. It should be 0 only if no diffs
64 were found.
65 If optional args OLD-TEMP-FILE and/or NEW-TEMP-FILE are non-nil,
66 delete the temporary files so named."
67 (if old-temp-file (delete-file old-temp-file))
68 (if new-temp-file (delete-file new-temp-file))
69 (diff-setup-whitespace)
70 (goto-char (point-min))
71 (save-excursion
72 (goto-char (point-max))
73 (let ((inhibit-read-only t))
74 (insert (format "\nDiff finished%s. %s\n"
75 (cond ((equal 0 code) " (no differences)")
76 ((equal 2 code) " (diff error)")
77 (t ""))
78 (current-time-string))))))
79
80 ;;;###autoload
81 (defun diff (old new &optional switches no-async)
82 "Find and display the differences between OLD and NEW files.
83 When called interactively, read NEW, then OLD, using the
84 minibuffer. The default for NEW is the current buffer's file
85 name, and the default for OLD is a backup file for NEW, if one
86 exists. If NO-ASYNC is non-nil, call diff synchronously.
87
88 When called interactively with a prefix argument, prompt
89 interactively for diff switches. Otherwise, the switches
90 specified in `diff-switches' are passed to the diff command."
91 (interactive
92 (let* ((newf (if (and buffer-file-name (file-exists-p buffer-file-name))
93 (read-file-name
94 (concat "Diff new file (default "
95 (file-name-nondirectory buffer-file-name) "): ")
96 nil buffer-file-name t)
97 (read-file-name "Diff new file: " nil nil t)))
98 (oldf (file-newest-backup newf)))
99 (setq oldf (if (and oldf (file-exists-p oldf))
100 (read-file-name
101 (concat "Diff original file (default "
102 (file-name-nondirectory oldf) "): ")
103 (file-name-directory oldf) oldf t)
104 (read-file-name "Diff original file: "
105 (file-name-directory newf) nil t)))
106 (list oldf newf (diff-switches))))
107 (display-buffer
108 (diff-no-select old new switches no-async)))
109
110 (defun diff-file-local-copy (file-or-buf)
111 (if (bufferp file-or-buf)
112 (with-current-buffer file-or-buf
113 (let ((tempfile (make-temp-file "buffer-content-")))
114 (write-region nil nil tempfile nil 'nomessage)
115 tempfile))
116 (file-local-copy file-or-buf)))
117
118 (defun diff-no-select (old new &optional switches no-async buf)
119 ;; Noninteractive helper for creating and reverting diff buffers
120 (unless (bufferp new) (setq new (expand-file-name new)))
121 (unless (bufferp old) (setq old (expand-file-name old)))
122 (or switches (setq switches diff-switches)) ; If not specified, use default.
123 (unless (listp switches) (setq switches (list switches)))
124 (or buf (setq buf (get-buffer-create "*Diff*")))
125 (let* ((old-alt (diff-file-local-copy old))
126 (new-alt (diff-file-local-copy new))
127 (command
128 (mapconcat 'identity
129 `(,diff-command
130 ;; Use explicitly specified switches
131 ,@switches
132 ,@(mapcar #'shell-quote-argument
133 (nconc
134 (when (or old-alt new-alt)
135 (list "-L" (if (stringp old)
136 old (prin1-to-string old))
137 "-L" (if (stringp new)
138 new (prin1-to-string new))))
139 (list (or old-alt old)
140 (or new-alt new)))))
141 " "))
142 (thisdir default-directory))
143 (with-current-buffer buf
144 (setq buffer-read-only t)
145 (buffer-disable-undo (current-buffer))
146 (let ((inhibit-read-only t))
147 (erase-buffer))
148 (buffer-enable-undo (current-buffer))
149 (diff-mode)
150 (set (make-local-variable 'revert-buffer-function)
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 (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))
172
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))))))
183
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)))
201
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))))
208
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)))
216
217 (provide 'diff)
218
219 ;;; diff.el ends here