]> code.delx.au - gnu-emacs/blob - lisp/diff.el
* net/tramp.el (tramp-do-copy-or-rename-file-directly)
[gnu-emacs] / 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, 2009 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 (cond ((equal 0 code) " (no differences)")
72 ((equal 2 code) " (diff error)")
73 (t ""))
74 (current-time-string))))))
75
76 (defvar diff-old-file nil)
77 (defvar diff-new-file nil)
78 (defvar diff-extra-args nil)
79
80 ;;;###autoload
81 (defun diff (old new &optional switches no-async)
82 "Find and display the differences between OLD and NEW files.
83 When called interactively, read OLD and NEW using the minibuffer;
84 the default for NEW is the current buffer's file name, and the
85 default for OLD is a backup file for NEW, if one exists.
86 If NO-ASYNC is non-nil, call diff synchronously.
87
88 When called interactively with a prefix argument, prompt
89 interactively for diff switches. Otherwise, the switches
90 specified in `diff-switches' are passed to the diff command."
91 (interactive
92 (let (oldf newf)
93 (setq newf (buffer-file-name)
94 newf (if (and newf (file-exists-p newf))
95 (read-file-name
96 (concat "Diff new file (default "
97 (file-name-nondirectory newf) "): ")
98 nil newf t)
99 (read-file-name "Diff new file: " nil nil t)))
100 (setq oldf (file-newest-backup newf)
101 oldf (if (and oldf (file-exists-p oldf))
102 (read-file-name
103 (concat "Diff original file (default "
104 (file-name-nondirectory oldf) "): ")
105 (file-name-directory oldf) oldf t)
106 (read-file-name "Diff original file: "
107 (file-name-directory newf) nil t)))
108 (list oldf newf (diff-switches))))
109 (setq new (expand-file-name new)
110 old (expand-file-name old))
111 (or switches (setq switches diff-switches)) ; If not specified, use default.
112 (let* ((old-alt (file-local-copy old))
113 (new-alt (file-local-copy new))
114 (command
115 (mapconcat 'identity
116 `(,diff-command
117 ;; Use explicitly specified switches
118 ,@(if (listp switches) switches (list switches))
119 ,@(if (or old-alt new-alt)
120 (list "-L" old "-L" new))
121 ,(shell-quote-argument (or old-alt old))
122 ,(shell-quote-argument (or new-alt new)))
123 " "))
124 (buf (get-buffer-create "*Diff*"))
125 (thisdir default-directory)
126 proc)
127 (save-excursion
128 (display-buffer buf)
129 (set-buffer buf)
130 (setq buffer-read-only nil)
131 (buffer-disable-undo (current-buffer))
132 (let ((inhibit-read-only t))
133 (erase-buffer))
134 (buffer-enable-undo (current-buffer))
135 (diff-mode)
136 ;; Use below 2 vars for backward-compatibility.
137 (set (make-local-variable 'diff-old-file) old)
138 (set (make-local-variable 'diff-new-file) new)
139 (set (make-local-variable 'diff-extra-args) (list switches no-async))
140 (set (make-local-variable 'revert-buffer-function)
141 (lambda (ignore-auto noconfirm)
142 (apply 'diff diff-old-file diff-new-file diff-extra-args)))
143 (set (make-local-variable 'diff-old-temp-file) old-alt)
144 (set (make-local-variable 'diff-new-temp-file) new-alt)
145 (setq default-directory thisdir)
146 (let ((inhibit-read-only t))
147 (insert command "\n"))
148 (if (and (not no-async) (fboundp 'start-process))
149 (progn
150 (setq proc (start-process "Diff" buf shell-file-name
151 shell-command-switch command))
152 (set-process-filter proc 'diff-process-filter)
153 (set-process-sentinel
154 proc (lambda (proc msg)
155 (with-current-buffer (process-buffer proc)
156 (diff-sentinel (process-exit-status proc))))))
157 ;; Async processes aren't available.
158 (let ((inhibit-read-only t))
159 (diff-sentinel
160 (call-process shell-file-name nil buf nil
161 shell-command-switch command)))))
162 buf))
163
164 (defun diff-process-filter (proc string)
165 (with-current-buffer (process-buffer proc)
166 (let ((moving (= (point) (process-mark proc))))
167 (save-excursion
168 ;; Insert the text, advancing the process marker.
169 (goto-char (process-mark proc))
170 (let ((inhibit-read-only t))
171 (insert string))
172 (set-marker (process-mark proc) (point)))
173 (if moving (goto-char (process-mark proc))))))
174
175 ;;;###autoload
176 (defun diff-backup (file &optional switches)
177 "Diff this file with its backup file or vice versa.
178 Uses the latest backup, if there are several numerical backups.
179 If this file is a backup, diff it with its original.
180 The backup file is the first file given to `diff'.
181 With prefix arg, prompt for diff switches."
182 (interactive (list (read-file-name "Diff (file with backup): ")
183 (diff-switches)))
184 (let (bak ori)
185 (if (backup-file-name-p file)
186 (setq bak file
187 ori (file-name-sans-versions file))
188 (setq bak (or (diff-latest-backup-file file)
189 (error "No backup found for %s" file))
190 ori file))
191 (diff bak ori switches)))
192
193 (defun diff-latest-backup-file (fn) ; actually belongs into files.el
194 "Return the latest existing backup of FILE, or nil."
195 (let ((handler (find-file-name-handler fn 'diff-latest-backup-file)))
196 (if handler
197 (funcall handler 'diff-latest-backup-file fn)
198 (file-newest-backup fn))))
199
200 (provide 'diff)
201
202 ;; arch-tag: 7de2c29b-7ea5-4b85-9b9d-72dd860de2bd
203 ;;; diff.el ends here