0
|
1 ;;; diff.el --- Run `diff' in compilation-mode.
|
|
2
|
|
3 ;; Copyright (C) 1992, 1994 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Keywords: unix, tools
|
|
6
|
|
7 ;; This file is part of XEmacs.
|
|
8
|
|
9 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
10 ;; under the terms of the GNU General Public License as published by
|
|
11 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
12 ;; any later version.
|
|
13
|
|
14 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
17 ;; General Public License for more details.
|
|
18
|
|
19 ;; You should have received a copy of the GNU General Public License
|
|
20 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
2
|
21 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
22 ;; 02111-1307, USA.
|
0
|
23
|
2
|
24 ;;; Synched up with: FSF 19.34.
|
0
|
25
|
|
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
|
|
32 ;;; Code:
|
|
33
|
|
34 (require 'compile)
|
|
35
|
|
36 ;;; This is duplicated in vc.el.
|
|
37 ;;;###autoload
|
|
38 (defvar diff-switches (purecopy "-c")
|
2
|
39 "*A string or list of strings specifying switches to be passed to diff.")
|
0
|
40
|
|
41 (defvar diff-command "diff"
|
|
42 "*The command to use to run diff.")
|
|
43
|
|
44 (defvar diff-regexp-alist
|
|
45 '(
|
|
46 ;; -u format: @@ -OLDSTART,OLDEND +NEWSTART,NEWEND @@
|
|
47 ("^@@ -\\([0-9]+\\),[0-9]+ \\+\\([0-9]+\\),[0-9]+ @@$" 1 2)
|
2
|
48
|
0
|
49 ;; -c format: *** OLDSTART,OLDEND ****
|
|
50 ("^\\*\\*\\* \\([0-9]+\\),[0-9]+ \\*\\*\\*\\*$" 1 nil)
|
|
51 ;; --- NEWSTART,NEWEND ----
|
|
52 ("^--- \\([0-9]+\\),[0-9]+ ----$" nil 1)
|
|
53
|
|
54 ;; plain diff format: OLDSTART[,OLDEND]{a,d,c}NEWSTART[,NEWEND]
|
|
55 ("^\\([0-9]+\\)\\(,[0-9]+\\)?[adc]\\([0-9]+\\)\\(,[0-9]+\\)?$" 1 3)
|
|
56
|
|
57 ;; -e (ed) format: OLDSTART[,OLDEND]{a,d,c}
|
|
58 ("^\\([0-9]+\\)\\(,[0-9]+\\)?[adc]$" 1)
|
|
59
|
|
60 ;; -f format: {a,d,c}OLDSTART[ OLDEND]
|
|
61 ;; -n format: {a,d,c}OLDSTART LINES-CHANGED
|
|
62 ("^[adc]\\([0-9]+\\)\\( [0-9]+\\)?$" 1)
|
|
63 )
|
2
|
64 "Alist (REGEXP OLD-IDX NEW-IDX) of regular expressions to match difference
|
0
|
65 sections in \\[diff] output. If REGEXP matches, the OLD-IDX'th
|
|
66 subexpression gives the line number in the old file, and NEW-IDX'th
|
|
67 subexpression gives the line number in the new file. If OLD-IDX or NEW-IDX
|
|
68 is nil, REGEXP matches only half a section.")
|
|
69
|
|
70 (defvar diff-old-file nil
|
|
71 "This is the old file name in the comparison in this buffer.")
|
|
72 (defvar diff-new-file nil
|
|
73 "This is the new file name in the comparison in this buffer.")
|
|
74 (defvar diff-old-temp-file nil
|
|
75 "This is the name of a temp file to be deleted after diff finishes.")
|
|
76 (defvar diff-new-temp-file nil
|
|
77 "This is the name of a temp file to be deleted after diff finishes.")
|
|
78
|
|
79 ;; See compilation-parse-errors-function (compile.el).
|
|
80 (defun diff-parse-differences (limit-search find-at-least)
|
|
81 (setq compilation-error-list nil)
|
|
82 (message "Parsing differences...")
|
|
83
|
|
84 ;; Don't reparse diffs already seen at last parse.
|
|
85 (if compilation-parsing-end (goto-char compilation-parsing-end))
|
|
86
|
|
87 ;; Construct in REGEXP a regexp composed of all those in dired-regexp-alist.
|
|
88 (let ((regexp (mapconcat #'(lambda (elt)
|
|
89 (concat "\\(" (car elt) "\\)"))
|
|
90 diff-regexp-alist
|
|
91 "\\|"))
|
|
92 ;; (GROUP-IDX OLD-IDX NEW-IDX)
|
|
93 (groups (let ((subexpr 1))
|
|
94 (mapcar #'(lambda (elt)
|
|
95 (prog1
|
|
96 (cons subexpr
|
|
97 (mapcar #'(lambda (n)
|
|
98 (and n
|
|
99 (+ subexpr n)))
|
|
100 (cdr elt)))
|
|
101 (setq subexpr (+ subexpr 1
|
|
102 ;;#### undefined??
|
|
103 (count-regexp-groupings
|
|
104 (car elt))))))
|
|
105 diff-regexp-alist)))
|
|
106
|
|
107 (new-error
|
|
108 (function (lambda (file subexpr)
|
|
109 (setq compilation-error-list
|
|
110 (cons
|
|
111 (cons (save-excursion
|
|
112 ;; Report location of message
|
|
113 ;; at beginning of line.
|
|
114 (goto-char
|
|
115 (match-beginning subexpr))
|
|
116 (beginning-of-line)
|
|
117 (point-marker))
|
|
118 ;; Report location of corresponding text.
|
|
119 (let ((line (string-to-int
|
|
120 (buffer-substring
|
|
121 (match-beginning subexpr)
|
|
122 (match-end subexpr)))))
|
|
123 (save-excursion
|
|
124 (save-match-data
|
2
|
125 (set-buffer (find-file-noselect file)))
|
0
|
126 (save-excursion
|
|
127 (goto-line line)
|
|
128 (point-marker)))))
|
|
129 compilation-error-list)))))
|
|
130
|
|
131 (found-desired nil)
|
|
132 (num-loci-found 0)
|
|
133 g)
|
|
134
|
|
135 (while (and (not found-desired)
|
|
136 ;; We don't just pass LIMIT-SEARCH to re-search-forward
|
|
137 ;; because we want to find matches containing LIMIT-SEARCH
|
|
138 ;; but which extend past it.
|
|
139 (re-search-forward regexp nil t))
|
|
140
|
|
141 ;; Find which individual regexp matched.
|
|
142 (setq g groups)
|
|
143 (while (and g (null (match-beginning (car (car g)))))
|
|
144 (setq g (cdr g)))
|
|
145 (setq g (car g))
|
|
146
|
|
147 (if (nth 1 g) ;OLD-IDX
|
|
148 (funcall new-error diff-old-file (nth 1 g)))
|
|
149 (if (nth 2 g) ;NEW-IDX
|
|
150 (funcall new-error diff-new-file (nth 2 g)))
|
|
151
|
|
152 (setq num-loci-found (1+ num-loci-found))
|
|
153 (if (or (and find-at-least
|
|
154 (>= num-loci-found find-at-least))
|
|
155 (and limit-search (>= (point) limit-search)))
|
|
156 ;; We have found as many new loci as the user wants,
|
|
157 ;; or the user wanted a specific diff, and we're past it.
|
|
158 (setq found-desired t)))
|
|
159 (if found-desired
|
|
160 (setq compilation-parsing-end (point))
|
|
161 ;; Set to point-max, not point, so we don't perpetually
|
|
162 ;; parse the last bit of text when it isn't a diff header.
|
|
163 (setq compilation-parsing-end (point-max)))
|
|
164 (message "Parsing differences...done"))
|
|
165 (setq compilation-error-list (nreverse compilation-error-list)))
|
|
166
|
|
167 ;;;###autoload
|
|
168 (defun diff (old new &optional switches)
|
|
169 "Find and display the differences between OLD and NEW files.
|
|
170 Interactively the current buffer's file name is the default for NEW
|
|
171 and a backup file for NEW is the default for OLD.
|
|
172 With prefix arg, prompt for diff switches."
|
|
173 (interactive
|
|
174 (nconc
|
|
175 (let (oldf newf)
|
|
176 (nreverse
|
|
177 (list
|
|
178 (setq newf (buffer-file-name)
|
|
179 newf (if (and newf (file-exists-p newf))
|
|
180 (read-file-name
|
|
181 (concat "Diff new file: ("
|
|
182 (file-name-nondirectory newf) ") ")
|
|
183 nil newf t)
|
|
184 (read-file-name "Diff new file: " nil nil t)))
|
|
185 (setq oldf (file-newest-backup newf)
|
|
186 oldf (if (and oldf (file-exists-p oldf))
|
|
187 (read-file-name
|
|
188 (concat "Diff original file: ("
|
|
189 (file-name-nondirectory oldf) ") ")
|
|
190 (file-name-directory oldf) oldf t)
|
|
191 (read-file-name "Diff original file: "
|
|
192 (file-name-directory newf) nil t))))))
|
|
193 (if current-prefix-arg
|
|
194 (list (read-string "Diff switches: "
|
|
195 (if (stringp diff-switches)
|
|
196 diff-switches
|
|
197 (mapconcat 'identity diff-switches " "))))
|
|
198 nil)))
|
|
199 (setq new (expand-file-name new)
|
|
200 old (expand-file-name old))
|
|
201 ;; XEmacs addition -- allow (diff "../old/" "new-file.el") to work
|
|
202 (cond ((file-directory-p old)
|
|
203 (or (file-directory-p new)
|
|
204 (setq old (expand-file-name (file-name-nondirectory new)
|
|
205 (file-name-as-directory old)))))
|
|
206 ((file-directory-p new)
|
|
207 (setq new (expand-file-name (file-name-nondirectory old)
|
|
208 (file-name-as-directory new)))))
|
|
209 (let ((old-alt (file-local-copy old))
|
|
210 (new-alt (file-local-copy new))
|
|
211 buf)
|
|
212 (unwind-protect
|
|
213 (let ((command
|
|
214 (mapconcat 'identity
|
|
215 (append (list diff-command)
|
|
216 ;; Use explicitly specified switches
|
|
217 (if switches
|
|
218 (if (consp switches)
|
|
219 switches (list switches))
|
|
220 ;; If not specified, use default.
|
|
221 (if (consp diff-switches)
|
|
222 diff-switches
|
|
223 (list diff-switches)))
|
|
224 (if (or old-alt new-alt)
|
|
225 (list "-L" old "-L" new))
|
|
226 (list
|
|
227 (shell-quote-argument (or old-alt old)))
|
|
228 (list
|
|
229 (shell-quote-argument (or new-alt new))))
|
|
230 " ")))
|
|
231 (setq buf
|
|
232 (compile-internal command
|
|
233 "No more differences" "Diff"
|
|
234 'diff-parse-differences))
|
|
235 (pop-to-buffer buf)
|
2
|
236 ;; Avoid frightening people with "abnormally terminated"
|
|
237 ;; if diff finds differences.
|
|
238 (set (make-local-variable 'compilation-exit-message-function)
|
|
239 (lambda (status code msg)
|
|
240 (cond ((not (eq status 'exit))
|
|
241 (cons msg code))
|
|
242 ((zerop code)
|
|
243 '("finished (no differences)\n" . "no differences"))
|
|
244 ((= code 1)
|
|
245 '("finished\n" . "differences found"))
|
|
246 (t
|
|
247 (cons msg code)))))
|
0
|
248 (set (make-local-variable 'diff-old-file) old)
|
|
249 (set (make-local-variable 'diff-new-file) new)
|
|
250 (set (make-local-variable 'diff-old-temp-file) old-alt)
|
|
251 (set (make-local-variable 'diff-new-temp-file) new-alt)
|
|
252 (set (make-local-variable 'compilation-finish-function)
|
|
253 (function (lambda (buff msg)
|
|
254 (if diff-old-temp-file
|
|
255 (delete-file diff-old-temp-file))
|
|
256 (if diff-new-temp-file
|
|
257 (delete-file diff-new-temp-file)))))
|
|
258 buf))))
|
|
259
|
|
260 ;;;###autoload
|
|
261 (defun diff-backup (file &optional switches)
|
|
262 "Diff this file with its backup file or vice versa.
|
|
263 Uses the latest backup, if there are several numerical backups.
|
|
264 If this file is a backup, diff it with its original.
|
|
265 The backup file is the first file given to `diff'."
|
|
266 (interactive (list (read-file-name "Diff (file with backup): ")
|
|
267 (if current-prefix-arg
|
|
268 (read-string "Diff switches: "
|
|
269 (if (stringp diff-switches)
|
|
270 diff-switches
|
|
271 (mapconcat 'identity
|
|
272 diff-switches " ")))
|
|
273 nil)))
|
|
274 (let (bak ori)
|
|
275 (if (backup-file-name-p file)
|
|
276 (setq bak file
|
|
277 ori (file-name-sans-versions file))
|
|
278 (setq bak (or (diff-latest-backup-file file)
|
|
279 (error "No backup found for %s" file))
|
|
280 ori file))
|
|
281 (diff bak ori switches)))
|
|
282
|
|
283 (defun diff-latest-backup-file (fn) ; actually belongs into files.el
|
|
284 "Return the latest existing backup of FILE, or nil."
|
|
285 (let ((handler (find-file-name-handler fn 'diff-latest-backup-file)))
|
|
286 (if handler
|
|
287 (funcall handler 'diff-latest-backup-file fn)
|
|
288 ;; First try simple backup, then the highest numbered of the
|
|
289 ;; numbered backups.
|
|
290 ;; Ignore the value of version-control because we look for existing
|
|
291 ;; backups, which maybe were made earlier or by another user with
|
|
292 ;; a different value of version-control.
|
|
293 (setq fn (file-chase-links (expand-file-name fn)))
|
|
294 (or
|
|
295 (let ((bak (make-backup-file-name fn)))
|
|
296 (if (file-exists-p bak) bak))
|
|
297 ;; We use BACKUPNAME to cope with backups stored in a different dir.
|
|
298 (let* ((backupname (car (find-backup-file-name fn)))
|
|
299 (dir (file-name-directory backupname))
|
|
300 (base-versions (concat (file-name-sans-versions
|
|
301 (file-name-nondirectory backupname))
|
|
302 ".~"))
|
|
303 (bv-length (length base-versions)))
|
|
304 (concat dir
|
|
305 (car (sort
|
|
306 (file-name-all-completions base-versions dir)
|
|
307 ;; bv-length is a fluid var for backup-extract-version:
|
|
308 (function
|
|
309 (lambda (fn1 fn2)
|
|
310 (> (backup-extract-version fn1)
|
|
311 (backup-extract-version fn2))))))))))))
|
|
312
|
|
313 (provide 'diff)
|
|
314
|
|
315 ;;; diff.el ends here
|