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