1 ;;; diff.el --- run `diff' -*- lexical-binding: t -*-
3 ;; Copyright (C) 1992, 1994, 1996, 2001-2014 Free Software Foundation,
7 ;; (according to authors.el)
8 ;; Maintainer: emacs-devel@gnu.org
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/>.
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'.
34 (declare-function diff-setup-whitespace
"diff-mode" ())
37 "Comparing files with `diff'."
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
))
47 (defcustom diff-command
(purecopy "diff")
48 "The command to use to run 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
)
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
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))
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)")
77 (current-time-string))))))
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."
91 (let* ((newf (if (and buffer-file-name
(file-exists-p buffer-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
))
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))))
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
)
115 (file-local-copy file-or-buf
)))
117 (defvar diff-use-labels
'check
118 "Whether `diff-command' understands the \"--label\" option.
121 nil -- no, it does not
122 check -- try to probe whether it does")
124 (defun diff-no-select (old new
&optional switches no-async buf
)
125 ;; Noninteractive helper for creating and reverting diff buffers
126 (unless (bufferp new
) (setq new
(expand-file-name new
)))
127 (unless (bufferp old
) (setq old
(expand-file-name old
)))
128 (or switches
(setq switches diff-switches
)) ; If not specified, use default.
129 (unless (listp switches
) (setq switches
(list switches
)))
130 (or buf
(setq buf
(get-buffer-create "*Diff*")))
131 (when (eq 'check diff-use-labels
)
132 (setq diff-use-labels
134 (when (ignore-errors (call-process diff-command nil t nil
"--help"))
135 (if (search-backward "--label" nil t
) t
)))))
136 (let* ((old-alt (diff-file-local-copy old
))
137 (new-alt (diff-file-local-copy new
))
141 ;; Use explicitly specified switches
143 ,@(mapcar #'shell-quote-argument
145 (and (or old-alt new-alt
)
146 (eq diff-use-labels t
)
148 (if (stringp old
) old
149 (prin1-to-string old
))
151 (if (stringp new
) new
152 (prin1-to-string new
))))
153 (list (or old-alt old
)
156 (thisdir default-directory
))
157 (with-current-buffer buf
158 (setq buffer-read-only t
)
159 (buffer-disable-undo (current-buffer))
160 (let ((inhibit-read-only t
))
162 (buffer-enable-undo (current-buffer))
164 (set (make-local-variable 'revert-buffer-function
)
165 (lambda (_ignore-auto _noconfirm
)
166 (diff-no-select old new switches no-async
(current-buffer))))
167 (setq default-directory thisdir
)
168 (let ((inhibit-read-only t
))
169 (insert command
"\n"))
170 (if (and (not no-async
) (fboundp 'start-process
))
171 (let ((proc (start-process "Diff" buf shell-file-name
172 shell-command-switch command
)))
173 (set-process-filter proc
'diff-process-filter
)
174 (set-process-sentinel
175 proc
(lambda (proc _msg
)
176 (with-current-buffer (process-buffer proc
)
177 (diff-sentinel (process-exit-status proc
)
179 ;; Async processes aren't available.
180 (let ((inhibit-read-only t
))
182 (call-process shell-file-name nil buf nil
183 shell-command-switch command
)
187 (defun diff-process-filter (proc string
)
188 (with-current-buffer (process-buffer proc
)
189 (let ((moving (= (point) (process-mark proc
))))
191 ;; Insert the text, advancing the process marker.
192 (goto-char (process-mark proc
))
193 (let ((inhibit-read-only t
))
195 (set-marker (process-mark proc
) (point)))
196 (if moving
(goto-char (process-mark proc
))))))
199 (defun diff-backup (file &optional switches
)
200 "Diff this file with its backup file or vice versa.
201 Uses the latest backup, if there are several numerical backups.
202 If this file is a backup, diff it with its original.
203 The backup file is the first file given to `diff'.
204 With prefix arg, prompt for diff switches."
205 (interactive (list (read-file-name "Diff (file with backup): ")
208 (if (backup-file-name-p file
)
210 ori
(file-name-sans-versions file
))
211 (setq bak
(or (diff-latest-backup-file file
)
212 (error "No backup found for %s" file
))
214 (diff bak ori switches
)))
217 (defun diff-latest-backup-file (fn)
218 "Return the latest existing backup of FILE, or nil."
219 (let ((handler (find-file-name-handler fn
'diff-latest-backup-file
)))
221 (funcall handler
'diff-latest-backup-file fn
)
222 (file-newest-backup fn
))))
225 (defun diff-buffer-with-file (&optional buffer
)
226 "View the differences between BUFFER and its associated file.
227 This requires the external program `diff' to be in your `exec-path'."
228 (interactive "bBuffer: ")
229 (with-current-buffer (get-buffer (or buffer
(current-buffer)))
230 (diff buffer-file-name
(current-buffer) nil
'noasync
)))
234 ;;; diff.el ends here