Switch to recommended form of GPLv3 permissions notice.
[bpt/emacs.git] / lisp / diff.el
CommitLineData
60370d40 1;;; diff.el --- run `diff' in compilation-mode
c0274f38 2
0d30b337 3;; Copyright (C) 1992, 1994, 1996, 2001, 2002, 2003, 2004,
409cc4a3 4;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
3a801d0c 5
6228c05b 6;; Maintainer: FSF
e9571d2a 7;; Keywords: unix, tools
e5167999 8
b10a4379
JB
9;; This file is part of GNU Emacs.
10
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
b10a4379 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
b10a4379
JB
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
b10a4379 23
e41b2db1
ER
24;;; Commentary:
25
26;; This package helps you explore differences between files, using the
27;; UNIX command diff(1). The commands are `diff' and `diff-backup'.
28;; You can specify options with `diff-switches'.
29
e5167999
ER
30;;; Code:
31
85b6275f
RS
32(defgroup diff nil
33 "Comparing files with `diff'."
34 :group 'tools)
35
36;;;###autoload
37(defcustom diff-switches "-c"
76f024d9 38 "*A string or list of strings specifying switches to be passed to diff."
85b6275f
RS
39 :type '(choice string (repeat string))
40 :group 'diff)
41
42;;;###autoload
43(defcustom diff-command "diff"
44 "*The command to use to run diff."
660d4800 45 :type 'string
85b6275f 46 :group 'diff)
d009603c 47
5d68c2c2
RS
48(defvar diff-old-temp-file nil
49 "This is the name of a temp file to be deleted after diff finishes.")
50(defvar diff-new-temp-file nil
51 "This is the name of a temp file to be deleted after diff finishes.")
52
4c11f6a8
SM
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
540d0666
SM
61(defun diff-sentinel (code)
62 "Code run when the diff process exits.
4837b516
GM
63CODE is the exit code of the process. It should be 0 only if no diffs
64were found."
540d0666
SM
65 (if diff-old-temp-file (delete-file diff-old-temp-file))
66 (if diff-new-temp-file (delete-file diff-new-temp-file))
67 (save-excursion
68 (goto-char (point-max))
53dd481c
CY
69 (let ((inhibit-read-only t))
70 (insert (format "\nDiff finished%s. %s\n"
71 (if (equal 0 code) " (no differences)" "")
72 (current-time-string))))))
540d0666 73
3fe5c37a
DN
74(defvar diff-old-file nil)
75(defvar diff-new-file nil)
76(defvar diff-extra-args nil)
77
b10a4379 78;;;###autoload
0f7b0f88 79(defun diff (old new &optional switches no-async)
b10a4379 80 "Find and display the differences between OLD and NEW files.
4b00edc8 81Interactively the current buffer's file name is the default for NEW
646bd331 82and a backup file for NEW is the default for OLD.
4c11f6a8
SM
83If NO-ASYNC is non-nil, call diff synchronously.
84With prefix arg, prompt for diff switches."
b10a4379 85 (interactive
4c11f6a8
SM
86 (let (oldf newf)
87 (setq newf (buffer-file-name)
88 newf (if (and newf (file-exists-p newf))
89 (read-file-name
5b76833f
RF
90 (concat "Diff new file (default "
91 (file-name-nondirectory newf) "): ")
4c11f6a8
SM
92 nil newf t)
93 (read-file-name "Diff new file: " nil nil t)))
94 (setq oldf (file-newest-backup newf)
95 oldf (if (and oldf (file-exists-p oldf))
96 (read-file-name
5b76833f
RF
97 (concat "Diff original file (default "
98 (file-name-nondirectory oldf) "): ")
4c11f6a8
SM
99 (file-name-directory oldf) oldf t)
100 (read-file-name "Diff original file: "
101 (file-name-directory newf) nil t)))
102 (list oldf newf (diff-switches))))
b10a4379
JB
103 (setq new (expand-file-name new)
104 old (expand-file-name old))
17c91d79
SM
105 (or switches (setq switches diff-switches)) ; If not specified, use default.
106 (let* ((old-alt (file-local-copy old))
5d68c2c2 107 (new-alt (file-local-copy new))
540d0666
SM
108 (command
109 (mapconcat 'identity
110 `(,diff-command
111 ;; Use explicitly specified switches
112 ,@(if (listp switches) switches (list switches))
113 ,@(if (or old-alt new-alt)
114 (list "-L" old "-L" new))
115 ,(shell-quote-argument (or old-alt old))
116 ,(shell-quote-argument (or new-alt new)))
117 " "))
118 (buf (get-buffer-create "*Diff*"))
de9dc5e5 119 (thisdir default-directory)
540d0666 120 proc)
9f748a35 121 (save-excursion
540d0666
SM
122 (display-buffer buf)
123 (set-buffer buf)
124 (setq buffer-read-only nil)
125 (buffer-disable-undo (current-buffer))
53dd481c
CY
126 (let ((inhibit-read-only t))
127 (erase-buffer))
540d0666
SM
128 (buffer-enable-undo (current-buffer))
129 (diff-mode)
d4871b4f
SM
130 ;; Use below 2 vars for backward-compatibility.
131 (set (make-local-variable 'diff-old-file) old)
132 (set (make-local-variable 'diff-new-file) new)
133 (set (make-local-variable 'diff-extra-args) (list switches no-async))
540d0666 134 (set (make-local-variable 'revert-buffer-function)
d4871b4f
SM
135 (lambda (ignore-auto noconfirm)
136 (apply 'diff diff-old-file diff-new-file diff-extra-args)))
540d0666
SM
137 (set (make-local-variable 'diff-old-temp-file) old-alt)
138 (set (make-local-variable 'diff-new-temp-file) new-alt)
de9dc5e5 139 (setq default-directory thisdir)
53dd481c
CY
140 (let ((inhibit-read-only t))
141 (insert command "\n"))
540d0666
SM
142 (if (and (not no-async) (fboundp 'start-process))
143 (progn
144 (setq proc (start-process "Diff" buf shell-file-name
145 shell-command-switch command))
53dd481c 146 (set-process-filter proc 'diff-process-filter)
540d0666
SM
147 (set-process-sentinel
148 proc (lambda (proc msg)
149 (with-current-buffer (process-buffer proc)
150 (diff-sentinel (process-exit-status proc))))))
151 ;; Async processes aren't available.
53dd481c
CY
152 (let ((inhibit-read-only t))
153 (diff-sentinel
154 (call-process shell-file-name nil buf nil
155 shell-command-switch command)))))
540d0666 156 buf))
aa228418 157
53dd481c
CY
158(defun diff-process-filter (proc string)
159 (with-current-buffer (process-buffer proc)
160 (let ((moving (= (point) (process-mark proc))))
161 (save-excursion
162 ;; Insert the text, advancing the process marker.
163 (goto-char (process-mark proc))
164 (let ((inhibit-read-only t))
165 (insert string))
166 (set-marker (process-mark proc) (point)))
167 (if moving (goto-char (process-mark proc))))))
168
646bd331
RM
169;;;###autoload
170(defun diff-backup (file &optional switches)
37c51f00
RS
171 "Diff this file with its backup file or vice versa.
172Uses the latest backup, if there are several numerical backups.
173If this file is a backup, diff it with its original.
4c11f6a8
SM
174The backup file is the first file given to `diff'.
175With prefix arg, prompt for diff switches."
646bd331 176 (interactive (list (read-file-name "Diff (file with backup): ")
4c11f6a8 177 (diff-switches)))
37c51f00
RS
178 (let (bak ori)
179 (if (backup-file-name-p file)
180 (setq bak file
181 ori (file-name-sans-versions file))
182 (setq bak (or (diff-latest-backup-file file)
183 (error "No backup found for %s" file))
184 ori file))
646bd331 185 (diff bak ori switches)))
37c51f00
RS
186
187(defun diff-latest-backup-file (fn) ; actually belongs into files.el
188 "Return the latest existing backup of FILE, or nil."
a617e913 189 (let ((handler (find-file-name-handler fn 'diff-latest-backup-file)))
34c46d87 190 (if handler
6d0d0e8d 191 (funcall handler 'diff-latest-backup-file fn)
a8090e38 192 (file-newest-backup fn))))
aa228418 193
a4ef6584
ER
194(provide 'diff)
195
d4871b4f 196;; arch-tag: 7de2c29b-7ea5-4b85-9b9d-72dd860de2bd
c0274f38 197;;; diff.el ends here