Trivial ert.texi update for cl-lib namespace
[emacs.git] / lisp / vc / diff.el
blobb1c334ddcfcef6164186fa0f1d5f23d130e1d1fe
1 ;;; diff.el --- run `diff' -*- lexical-binding: t -*-
3 ;; Copyright (C) 1992, 1994, 1996, 2001-2012 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 (declare-function diff-setup-whitespace "diff-mode" ())
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 (diff-setup-whitespace)
68 (goto-char (point-min))
69 (save-excursion
70 (goto-char (point-max))
71 (let ((inhibit-read-only t))
72 (insert (format "\nDiff finished%s. %s\n"
73 (cond ((equal 0 code) " (no differences)")
74 ((equal 2 code) " (diff error)")
75 (t ""))
76 (current-time-string))))))
78 ;;;###autoload
79 (defun diff (old new &optional switches no-async)
80 "Find and display the differences between OLD and NEW files.
81 When called interactively, read NEW, then OLD, using the
82 minibuffer. The default for NEW is the current buffer's file
83 name, and the default for OLD is a backup file for NEW, if one
84 exists. If NO-ASYNC is non-nil, call diff synchronously.
86 When called interactively with a prefix argument, prompt
87 interactively for diff switches. Otherwise, the switches
88 specified in `diff-switches' are passed to the diff command."
89 (interactive
90 (let* ((newf (if (and buffer-file-name (file-exists-p buffer-file-name))
91 (read-file-name
92 (concat "Diff new file (default "
93 (file-name-nondirectory buffer-file-name) "): ")
94 nil buffer-file-name t)
95 (read-file-name "Diff new file: " nil nil t)))
96 (oldf (file-newest-backup newf)))
97 (setq oldf (if (and oldf (file-exists-p oldf))
98 (read-file-name
99 (concat "Diff original file (default "
100 (file-name-nondirectory oldf) "): ")
101 (file-name-directory oldf) oldf t)
102 (read-file-name "Diff original file: "
103 (file-name-directory newf) nil t)))
104 (list oldf newf (diff-switches))))
105 (display-buffer
106 (diff-no-select old new switches no-async)))
108 (defun diff-file-local-copy (file-or-buf)
109 (if (bufferp file-or-buf)
110 (with-current-buffer file-or-buf
111 (let ((tempfile (make-temp-file "buffer-content-")))
112 (write-region nil nil tempfile nil 'nomessage)
113 tempfile))
114 (file-local-copy file-or-buf)))
116 (defun diff-no-select (old new &optional switches no-async buf)
117 ;; Noninteractive helper for creating and reverting diff buffers
118 (unless (bufferp new) (setq new (expand-file-name new)))
119 (unless (bufferp old) (setq old (expand-file-name old)))
120 (or switches (setq switches diff-switches)) ; If not specified, use default.
121 (unless (listp switches) (setq switches (list switches)))
122 (or buf (setq buf (get-buffer-create "*Diff*")))
123 (let* ((old-alt (diff-file-local-copy old))
124 (new-alt (diff-file-local-copy new))
125 (command
126 (mapconcat 'identity
127 `(,diff-command
128 ;; Use explicitly specified switches
129 ,@switches
130 ,@(mapcar #'shell-quote-argument
131 (nconc
132 (when (or old-alt new-alt)
133 (list "-L" (if (stringp old)
134 old (prin1-to-string old))
135 "-L" (if (stringp new)
136 new (prin1-to-string new))))
137 (list (or old-alt old)
138 (or new-alt new)))))
139 " "))
140 (thisdir default-directory))
141 (with-current-buffer buf
142 (setq buffer-read-only t)
143 (buffer-disable-undo (current-buffer))
144 (let ((inhibit-read-only t))
145 (erase-buffer))
146 (buffer-enable-undo (current-buffer))
147 (diff-mode)
148 (set (make-local-variable 'revert-buffer-function)
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 (set-process-sentinel
159 proc (lambda (proc _msg)
160 (with-current-buffer (process-buffer proc)
161 (diff-sentinel (process-exit-status proc)
162 old-alt new-alt)))))
163 ;; Async processes aren't available.
164 (let ((inhibit-read-only t))
165 (diff-sentinel
166 (call-process shell-file-name nil buf nil
167 shell-command-switch command)
168 old-alt new-alt))))
169 buf))
171 (defun diff-process-filter (proc string)
172 (with-current-buffer (process-buffer proc)
173 (let ((moving (= (point) (process-mark proc))))
174 (save-excursion
175 ;; Insert the text, advancing the process marker.
176 (goto-char (process-mark proc))
177 (let ((inhibit-read-only t))
178 (insert string))
179 (set-marker (process-mark proc) (point)))
180 (if moving (goto-char (process-mark proc))))))
182 ;;;###autoload
183 (defun diff-backup (file &optional switches)
184 "Diff this file with its backup file or vice versa.
185 Uses the latest backup, if there are several numerical backups.
186 If this file is a backup, diff it with its original.
187 The backup file is the first file given to `diff'.
188 With prefix arg, prompt for diff switches."
189 (interactive (list (read-file-name "Diff (file with backup): ")
190 (diff-switches)))
191 (let (bak ori)
192 (if (backup-file-name-p file)
193 (setq bak file
194 ori (file-name-sans-versions file))
195 (setq bak (or (diff-latest-backup-file file)
196 (error "No backup found for %s" file))
197 ori file))
198 (diff bak ori switches)))
200 ;;;###autoload
201 (defun diff-latest-backup-file (fn)
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