Switch to recommended form of GPLv3 permissions notice.
[bpt/emacs.git] / lisp / diff.el
1 ;;; diff.el --- run `diff' in compilation-mode
2
3 ;; Copyright (C) 1992, 1994, 1996, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: unix, tools
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
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
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
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
30 ;;; Code:
31
32 (defgroup diff nil
33 "Comparing files with `diff'."
34 :group 'tools)
35
36 ;;;###autoload
37 (defcustom diff-switches "-c"
38 "*A string or list of strings specifying switches to be passed to diff."
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."
45 :type 'string
46 :group 'diff)
47
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
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)
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 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))
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))))))
73
74 (defvar diff-old-file nil)
75 (defvar diff-new-file nil)
76 (defvar diff-extra-args nil)
77
78 ;;;###autoload
79 (defun diff (old new &optional switches no-async)
80 "Find and display the differences between OLD and NEW files.
81 Interactively the current buffer's file name is the default for NEW
82 and a backup file for NEW is the default for OLD.
83 If NO-ASYNC is non-nil, call diff synchronously.
84 With prefix arg, prompt for diff switches."
85 (interactive
86 (let (oldf newf)
87 (setq newf (buffer-file-name)
88 newf (if (and newf (file-exists-p newf))
89 (read-file-name
90 (concat "Diff new file (default "
91 (file-name-nondirectory newf) "): ")
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
97 (concat "Diff original file (default "
98 (file-name-nondirectory oldf) "): ")
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))))
103 (setq new (expand-file-name new)
104 old (expand-file-name old))
105 (or switches (setq switches diff-switches)) ; If not specified, use default.
106 (let* ((old-alt (file-local-copy old))
107 (new-alt (file-local-copy new))
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*"))
119 (thisdir default-directory)
120 proc)
121 (save-excursion
122 (display-buffer buf)
123 (set-buffer buf)
124 (setq buffer-read-only nil)
125 (buffer-disable-undo (current-buffer))
126 (let ((inhibit-read-only t))
127 (erase-buffer))
128 (buffer-enable-undo (current-buffer))
129 (diff-mode)
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))
134 (set (make-local-variable 'revert-buffer-function)
135 (lambda (ignore-auto noconfirm)
136 (apply 'diff diff-old-file diff-new-file diff-extra-args)))
137 (set (make-local-variable 'diff-old-temp-file) old-alt)
138 (set (make-local-variable 'diff-new-temp-file) new-alt)
139 (setq default-directory thisdir)
140 (let ((inhibit-read-only t))
141 (insert command "\n"))
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))
146 (set-process-filter proc 'diff-process-filter)
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.
152 (let ((inhibit-read-only t))
153 (diff-sentinel
154 (call-process shell-file-name nil buf nil
155 shell-command-switch command)))))
156 buf))
157
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
169 ;;;###autoload
170 (defun diff-backup (file &optional switches)
171 "Diff this file with its backup file or vice versa.
172 Uses the latest backup, if there are several numerical backups.
173 If this file is a backup, diff it with its original.
174 The backup file is the first file given to `diff'.
175 With prefix arg, prompt for diff switches."
176 (interactive (list (read-file-name "Diff (file with backup): ")
177 (diff-switches)))
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))
185 (diff bak ori switches)))
186
187 (defun diff-latest-backup-file (fn) ; actually belongs into files.el
188 "Return the latest existing backup of FILE, or nil."
189 (let ((handler (find-file-name-handler fn 'diff-latest-backup-file)))
190 (if handler
191 (funcall handler 'diff-latest-backup-file fn)
192 (file-newest-backup fn))))
193
194 (provide 'diff)
195
196 ;; arch-tag: 7de2c29b-7ea5-4b85-9b9d-72dd860de2bd
197 ;;; diff.el ends here