(woman-parse-colon-path): Return a list even if there's only one directory
[emacs.git] / lisp / diff.el
blob1af34dbf493685063a9702a24e0f7d68e0b9807e
1 ;;; diff.el --- run `diff' in compilation-mode
3 ;; Copyright (C) 1992, 1994, 1996, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;; 2007, 2008, 2009 Free Software Foundation, Inc.
6 ;; Author: Frank Bresz
7 ;; (according to authors.el)
8 ;; Maintainer: FSF
9 ;; Keywords: unix, 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/>.
26 ;;; Commentary:
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'.
32 ;;; Code:
34 (defgroup diff nil
35 "Comparing files with `diff'."
36 :group 'tools)
38 ;;;###autoload
39 (defcustom diff-switches "-c"
40 "A string or list of strings specifying switches to be passed to diff."
41 :type '(choice string (repeat string))
42 :group 'diff)
44 ;;;###autoload
45 (defcustom diff-command "diff"
46 "The command to use to run diff."
47 :type 'string
48 :group 'diff)
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.")
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 " ")))))
63 (defun diff-sentinel (code)
64 "Code run when the diff process exits.
65 CODE is the exit code of the process. It should be 0 only if no diffs
66 were found."
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))
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 (defvar diff-old-file nil)
79 (defvar diff-new-file nil)
80 (defvar diff-extra-args nil)
82 ;;;###autoload
83 (defun diff (old new &optional switches no-async)
84 "Find and display the differences between OLD and NEW files.
85 When called interactively, read OLD and NEW using the minibuffer;
86 the default for NEW is the current buffer's file name, and the
87 default for OLD is a backup file for NEW, if one exists.
88 If NO-ASYNC is non-nil, call diff synchronously.
90 When called interactively with a prefix argument, prompt
91 interactively for diff switches. Otherwise, the switches
92 specified in `diff-switches' are passed to the diff command."
93 (interactive
94 (let (oldf newf)
95 (setq newf (buffer-file-name)
96 newf (if (and newf (file-exists-p newf))
97 (read-file-name
98 (concat "Diff new file (default "
99 (file-name-nondirectory newf) "): ")
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
105 (concat "Diff original file (default "
106 (file-name-nondirectory oldf) "): ")
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))))
111 (setq new (expand-file-name new)
112 old (expand-file-name old))
113 (or switches (setq switches diff-switches)) ; If not specified, use default.
114 (let* ((old-alt (file-local-copy old))
115 (new-alt (file-local-copy new))
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*"))
127 (thisdir default-directory)
128 proc)
129 (save-excursion
130 (display-buffer buf)
131 (set-buffer buf)
132 (setq buffer-read-only nil)
133 (buffer-disable-undo (current-buffer))
134 (let ((inhibit-read-only t))
135 (erase-buffer))
136 (buffer-enable-undo (current-buffer))
137 (diff-mode)
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))
142 (set (make-local-variable 'revert-buffer-function)
143 (lambda (ignore-auto noconfirm)
144 (apply 'diff diff-old-file diff-new-file diff-extra-args)))
145 (set (make-local-variable 'diff-old-temp-file) old-alt)
146 (set (make-local-variable 'diff-new-temp-file) new-alt)
147 (setq default-directory thisdir)
148 (let ((inhibit-read-only t))
149 (insert command "\n"))
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))
154 (set-process-filter proc 'diff-process-filter)
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.
160 (let ((inhibit-read-only t))
161 (diff-sentinel
162 (call-process shell-file-name nil buf nil
163 shell-command-switch command)))))
164 buf))
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))))))
177 ;;;###autoload
178 (defun diff-backup (file &optional switches)
179 "Diff this file with its backup file or vice versa.
180 Uses the latest backup, if there are several numerical backups.
181 If this file is a backup, diff it with its original.
182 The backup file is the first file given to `diff'.
183 With prefix arg, prompt for diff switches."
184 (interactive (list (read-file-name "Diff (file with backup): ")
185 (diff-switches)))
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))
193 (diff bak ori switches)))
195 (defun diff-latest-backup-file (fn) ; actually belongs into files.el
196 "Return the latest existing backup of FILE, or nil."
197 (let ((handler (find-file-name-handler fn 'diff-latest-backup-file)))
198 (if handler
199 (funcall handler 'diff-latest-backup-file fn)
200 (file-newest-backup fn))))
202 (provide 'diff)
204 ;; arch-tag: 7de2c29b-7ea5-4b85-9b9d-72dd860de2bd
205 ;;; diff.el ends here