24
|
1 ;; -*-Emacs-Lisp-*-
|
|
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
3 ;;
|
|
4 ;; File: diff.el
|
32
|
5 ;; Version: $Revision: 1.4 $
|
24
|
6 ;; Author: This file is based on diff.el by
|
|
7 ;; sunpitt!wpmstr!fbresz@Sun.COM 1/27/89.
|
|
8 ;; It has been completely rewritten in July 1994 by
|
|
9 ;; Sandy Rutherford <sandy@ibm550.sissa.it>
|
|
10 ;; RCS:
|
|
11 ;; Description: diff-mode for handling output from unix diff utility.
|
|
12 ;; Modified: Wed Jul 17 10:26:57 1996 (Andy Norman) ange@hplb.hpl.hp.com
|
|
13 ;;
|
|
14 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
0
|
15
|
24
|
16 ;;; Copyright (C) 1990 Free Software Foundation, Inc.
|
|
17 ;;; Copyright (C) 1994 Sandy Rutherford
|
0
|
18
|
24
|
19 ;;; This file is based on diff.el by sunpitt!wpmstr!fbresz@Sun.COM 1/27/89.
|
|
20 ;;; It has been completely rewritten in July 1994 by
|
|
21 ;;; Sandy Rutherford <sandy@ibm550.sissa.it>
|
0
|
22
|
24
|
23 ;;; This program is free software; you can redistribute it and/or modify
|
|
24 ;;; it under the terms of the GNU General Public License as published by
|
|
25 ;;; the Free Software Foundation; either version 1, or (at your option)
|
|
26 ;;; any later version.
|
|
27 ;;;
|
|
28 ;;; This program is distributed in the hope that it will be useful,
|
|
29 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
30 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
31 ;;; GNU General Public License for more details.
|
|
32 ;;;
|
|
33 ;;; A copy of the GNU General Public License can be obtained from this
|
|
34 ;;; program's author (send electronic mail to sandy@ibm550.sissa.it) or
|
|
35 ;;; from the Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
|
|
36 ;;; MA 02139, USA.
|
0
|
37
|
24
|
38 (provide 'diff)
|
0
|
39
|
24
|
40 ;;; User Variables
|
0
|
41
|
24
|
42 ;; should be in to loaddefs.el now.
|
26
|
43 ;;;###autoload
|
32
|
44 (defvar diff-switches "-c"
|
24
|
45 "*A list of switches (strings) to pass to the diff program.")
|
0
|
46
|
24
|
47 (defvar diff-do-narrow nil
|
|
48 "*If non-nil diff buffers are initialized narrowed to each difference.")
|
0
|
49
|
24
|
50 (defvar diff-load-hooks nil
|
|
51 "Hooks to run after loading diff.el")
|
|
52
|
|
53 ;;; Internal variables
|
2
|
54
|
24
|
55 (defconst diff-emacs-19-p
|
|
56 (let ((ver (string-to-int (substring emacs-version 0 2))))
|
|
57 (>= ver 19)))
|
0
|
58
|
24
|
59 (or diff-emacs-19-p (require 'emacs-19))
|
0
|
60
|
24
|
61 (defvar diff-old-file nil)
|
|
62 ;; A list whose car is the name of the old file, and whose cdr indicates
|
|
63 ;; whether we should delete the buffer on quit.
|
|
64 (defvar diff-new-file nil)
|
|
65 ;; Same as diff-old-file, except for the new file.
|
|
66 (defvar diff-total-differences "0")
|
|
67 ;; Total number of difference hunks as a string.
|
|
68 (defvar diff-current-difference "0")
|
|
69 ;; Current difference hunk as a string.
|
|
70 (defvar diff-current-hunk 0)
|
|
71 ;; Current difference hunk as an integer.
|
|
72 (defvar diff-total-hunks 0)
|
|
73 ;; Total number of difference hunks as an integer.
|
|
74 (defvar diff-hunk-vector (vector 0))
|
|
75 ;; Vector storing the starting positions of the difference hunks.
|
|
76 (defvar diff-old-file-pattern nil)
|
|
77 (defvar diff-new-file-pattern nil)
|
|
78 (defvar diff-hunk-pattern nil)
|
|
79 ;; Regexps to use when parsing file lines in difference hunks.
|
0
|
80
|
24
|
81
|
|
82 (defvar diff-search-pattern-alist
|
|
83 (list
|
|
84 (list ?e "^[0-9]\\(,[0-9]+\\)?[acd]$" "^\\([0-9]+\\)" nil)
|
|
85 (list ?c "^\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\n"
|
|
86 "^\\*+ \\([0-9]+\\)" "^-+ \\([0-9]+\\)")
|
|
87 (list ?u "^@@ " "^@@ -\\([0-9]+\\)" "^@@ -[0-9,]+ \\+\\([0-9]+\\)")
|
|
88 (list nil "^[0-9]+" "^\\([0-9]+\\)" "^[0-9,]+[acd]\\([0-9]+\\)")))
|
0
|
89
|
24
|
90 (defvar diff-keymap-grokked nil)
|
|
91
|
|
92 (defvar diff-temp-template "/tmp/diff")
|
0
|
93
|
24
|
94 ;; Initialize the keymap if it isn't already
|
|
95
|
|
96 (defvar diff-mode-map nil
|
|
97 "Keymap used in diff mode.")
|
0
|
98
|
24
|
99 (if diff-mode-map
|
|
100 nil
|
|
101 (setq diff-mode-map (make-keymap))
|
|
102 (suppress-keymap diff-mode-map)
|
|
103 (define-key diff-mode-map "?" 'describe-mode)
|
|
104 (define-key diff-mode-map "." 'diff-display-file)
|
|
105 (define-key diff-mode-map "N" 'diff-narrow)
|
|
106 (define-key diff-mode-map "W" 'widen)
|
|
107 (define-key diff-mode-map "f" 'diff-find-file)
|
|
108 (define-key diff-mode-map "h" 'diff-show-header)
|
|
109 (define-key diff-mode-map "j" 'diff-show-difference)
|
|
110 (define-key diff-mode-map "n" 'diff-next-difference)
|
|
111 (define-key diff-mode-map "o" 'diff-find-file-other-window)
|
|
112 (define-key diff-mode-map "p" 'diff-previous-difference)
|
|
113 (define-key diff-mode-map "q" 'diff-quit)
|
|
114 (define-key diff-mode-map "w" 'diff-find-file-other-frame)
|
|
115 (define-key diff-mode-map "\C-c\C-c" 'diff-find-file-other-window)
|
|
116 (define-key diff-mode-map " " 'diff-advertised-scroll-up)
|
|
117 (define-key diff-mode-map "\177" 'diff-advertised-scroll-down)
|
|
118 (define-key diff-mode-map "\C-n" 'diff-next-line)
|
|
119 (define-key diff-mode-map "\C-p" 'diff-previous-line)
|
|
120 (define-key diff-mode-map "\M->" 'diff-end-of-buffer)
|
|
121 (define-key diff-mode-map "\M-<" 'diff-beginning-of-buffer))
|
|
122
|
|
123 ;;; Internal functions
|
0
|
124
|
24
|
125 (defun diff-grok-keys (to-command from-command)
|
|
126 ;; Assigns to TO-COMMAND the keys for the global binding of FROM-COMMAND.
|
|
127 ;; Does not clobber anything in the local keymap.
|
|
128 (let ((keys (where-is-internal from-command)))
|
|
129 (while keys
|
|
130 (condition-case nil
|
|
131 (if (eq (global-key-binding (car keys)) (key-binding (car keys)))
|
|
132 (local-set-key (car keys) to-command))
|
|
133 (error nil))
|
|
134 (setq keys (cdr keys)))))
|
|
135
|
|
136 (defun diff-grok-keymap ()
|
|
137 (if diff-keymap-grokked
|
|
138 ()
|
|
139 (if (and term-setup-hook (boundp 'command-line-args-left))
|
|
140 (progn
|
|
141 (if diff-emacs-19-p
|
|
142 (run-hooks term-setup-hook)
|
|
143 (funcall term-setup-hook))
|
|
144 (setq term-setup-hook nil)))
|
|
145 (setq diff-keymap-grokked t)
|
|
146 (diff-grok-keys 'diff-next-line 'next-line)
|
|
147 (diff-grok-keys 'diff-previous-line 'previous-line)
|
|
148 (diff-grok-keys 'diff-forward-char 'forward-char)
|
|
149 (diff-grok-keys 'diff-backward-char 'backward-char)
|
|
150 (diff-grok-keys 'diff-scroll-up 'scroll-up)
|
|
151 (diff-grok-keys 'diff-scroll-down 'scroll-down)
|
|
152 (diff-grok-keys 'diff-beginning-of-buffer 'beginning-of-buffer)
|
|
153 (diff-grok-keys 'diff-end-of-buffer 'end-of-buffer)))
|
0
|
154
|
24
|
155 (defun diff-buffer-narrowed-p ()
|
|
156 ;; Returns t if the current buffer is narrowed
|
|
157 (save-restriction
|
|
158 (let ((min (point-min))
|
|
159 (max (point-max)))
|
|
160 (widen)
|
|
161 (not (and (= min (point-min)) (= max (point-max)))))))
|
0
|
162
|
24
|
163 (defun diff-current-hunk ()
|
|
164 ;; Returns the current diff hunk.
|
|
165 (let ((pt (point))
|
|
166 (start 0)
|
|
167 (end (1+ diff-total-hunks))
|
|
168 m)
|
|
169 (while (> (- end start) 1)
|
|
170 (setq m (/ (+ start end) 2))
|
|
171 (if (>= pt (aref diff-hunk-vector m))
|
|
172 (setq start m)
|
|
173 (setq end m)))
|
|
174 (if (>= pt (aref diff-hunk-vector end))
|
|
175 (setq m end)
|
|
176 (setq m start))
|
|
177 ;; Don't treat end of buffer as start of next hunk
|
|
178 (if (eobp) (1- m) m)))
|
0
|
179
|
24
|
180 (defun diff-hunk-min (n)
|
|
181 ;; Returns the start of the current diff hunk.
|
|
182 (aref diff-hunk-vector n))
|
|
183
|
|
184 (defun diff-hunk-max (n)
|
|
185 ;; Returns the end of the current diff hunk.
|
|
186 (aref diff-hunk-vector (1+ n)))
|
0
|
187
|
24
|
188 (defun diff-parse-hunks ()
|
|
189 ;; Parses a buffer of diff output.
|
|
190 (save-excursion
|
|
191 (save-restriction
|
|
192 (message "Parsing differences...")
|
|
193 (widen)
|
|
194 (goto-char (point-min))
|
|
195 (let ((hunks (list 1)))
|
|
196 (while (re-search-forward diff-hunk-pattern nil t)
|
|
197 (setq hunks (cons (match-beginning 0) hunks)))
|
|
198 (setq diff-total-hunks (1- (length hunks))
|
|
199 diff-hunk-vector (apply 'vector
|
|
200 (nreverse (cons (point-max) hunks)))))))
|
|
201 (message "Parsing differences...done"))
|
0
|
202
|
24
|
203 (defun diff-update-modeline ()
|
|
204 ;; Updates the mode line to show current diff hunk.
|
|
205 (if (or (< (point) (diff-hunk-min diff-current-hunk))
|
|
206 (>= (point) (diff-hunk-max diff-current-hunk)))
|
|
207 (progn
|
|
208 (setq diff-current-hunk (diff-current-hunk)
|
|
209 diff-current-difference (int-to-string diff-current-hunk))
|
|
210 (set-buffer-modified-p (buffer-modified-p)))))
|
|
211
|
|
212 (defun diff-read-args (oldprompt newprompt switchprompt
|
|
213 &optional file-for-backup)
|
|
214 ;; Grab the args for diff. OLDPROMPT and NEWPROMPT are the prompts
|
|
215 ;; for the old & new filenames, SWITCHPROMPT for the list of
|
|
216 ;; switches. If FILE_FOR_BACKUP is provided (it must be a string if
|
|
217 ;; so), then it will be used to try & work out a file & backup to
|
|
218 ;; diff, & in this case the prompting order is backwards. %s in a
|
|
219 ;; prompt has a guess substituted into it. This is nasty.
|
|
220 (let (oldf newf)
|
|
221 (if file-for-backup
|
|
222 (setq newf file-for-backup
|
0
|
223 newf (if (and newf (file-exists-p newf))
|
|
224 (read-file-name
|
24
|
225 (format newprompt (file-name-nondirectory newf))
|
0
|
226 nil newf t)
|
24
|
227 (read-file-name (format newprompt "") nil nil t))
|
|
228 oldf (file-newest-backup newf)
|
0
|
229 oldf (if (and oldf (file-exists-p oldf))
|
|
230 (read-file-name
|
24
|
231 (format oldprompt (file-name-nondirectory oldf))
|
|
232 nil oldf t)
|
|
233 (read-file-name (format oldprompt "")
|
|
234 (file-name-directory newf) nil t)))
|
|
235 ;; Else we aren't trying to be bright...
|
|
236 (setq oldf (read-file-name (format oldprompt "") nil nil t)
|
|
237 newf (read-file-name
|
|
238 (format newprompt (file-name-nondirectory oldf))
|
|
239 nil (file-name-directory oldf) t)))
|
|
240 (list oldf newf (diff-read-switches switchprompt))))
|
|
241
|
|
242 (defun diff-read-switches (switchprompt)
|
|
243 ;; Read and return a list of switches
|
|
244 (if current-prefix-arg
|
|
245 (let ((default (if (listp diff-switches)
|
|
246 (mapconcat 'identity diff-switches " ")
|
|
247 diff-switches)))
|
|
248 (diff-fix-switches
|
|
249 (read-string (format switchprompt default) default)))))
|
|
250
|
|
251 (defun diff-fix-switches (switch-spec)
|
|
252 ;; Parse a string into a list of switches or leave it be if it's
|
|
253 ;; not a string
|
|
254 (if (stringp switch-spec)
|
|
255 (let (result (start 0))
|
|
256 (while (string-match "\\(\\S-+\\)" switch-spec start)
|
|
257 (setq result (cons (substring switch-spec (match-beginning 1)
|
|
258 (match-end 1))
|
|
259 result)
|
|
260 start (match-end 0)))
|
|
261 (nreverse result))
|
|
262 switch-spec))
|
0
|
263
|
24
|
264 (defun diff-get-file-buffer (file)
|
|
265 ;; Returns \(BUFFER . DEL-P\), where DEL-P is t if diff is expected
|
|
266 ;; to delete the buffer, and nil otherwise.
|
|
267 (let* ((buff (get-file-buffer file))
|
|
268 (del-p (null buff)))
|
|
269 (if (and buff (buffer-modified-p buff))
|
|
270 (progn
|
|
271 (message
|
|
272 "Buffer %s is modified. Diffing against buffer contents."
|
|
273 (buffer-name buff))
|
|
274 (sit-for 1)))
|
|
275 ;; Call find-file-noselect even if we already have the buffer,
|
|
276 ;; as it will run verify-buffer-file-modtime.
|
|
277 (cons (find-file-noselect file) del-p)))
|
0
|
278
|
24
|
279 (defun diff-cleanup-buffers ()
|
|
280 ;; Cleans up diff buffers by deleting buffers that we don't expect
|
|
281 ;; the user to care about.
|
|
282 (let ((files (list diff-old-file diff-new-file)))
|
|
283 (while files
|
|
284 (let ((ent (car files))
|
|
285 buff)
|
|
286 (and (cdr ent)
|
|
287 (setq buff (get-file-buffer (car ent)))
|
|
288 (not (buffer-modified-p buff))
|
|
289 (kill-buffer buff)))
|
|
290 (setq files (cdr files)))
|
|
291 (if (get-buffer "*Diff Header*")
|
|
292 (kill-buffer "*Diff Header*"))))
|
|
293
|
|
294 (defun diff-latest-backup-file (file)
|
0
|
295 "Return the latest existing backup of FILE, or nil."
|
24
|
296 ;; First try simple backup, then the highest numbered of the
|
|
297 ;; numbered backups.
|
|
298 ;; Ignore the value of version-control because we look for existing
|
|
299 ;; backups, which maybe were made earlier or by another user with
|
|
300 ;; a different value of version-control.
|
|
301 (let* ((file (expand-file-name file))
|
|
302 (handler (find-file-name-handler file 'diff-latest-backup-file)))
|
0
|
303 (if handler
|
24
|
304 (funcall handler 'diff-latest-backup-file file)
|
0
|
305 (or
|
24
|
306 (let ((bak (make-backup-file-name file)))
|
0
|
307 (if (file-exists-p bak) bak))
|
24
|
308 (let* ((dir (file-name-directory file))
|
|
309 (base-versions (concat (file-name-nondirectory file) ".~"))
|
0
|
310 (bv-length (length base-versions)))
|
|
311 (concat dir
|
|
312 (car (sort
|
|
313 (file-name-all-completions base-versions dir)
|
|
314 ;; bv-length is a fluid var for backup-extract-version:
|
|
315 (function
|
|
316 (lambda (fn1 fn2)
|
|
317 (> (backup-extract-version fn1)
|
|
318 (backup-extract-version fn2))))))))))))
|
|
319
|
24
|
320 (defun diff-file-line (&optional old-file-p)
|
|
321 "Return line number of current hunk in `diff-new-file'.
|
|
322 With optional argument OLD-FILE-P, use `diff-old-file' instead."
|
|
323 (save-excursion
|
|
324 (let ((min (diff-hunk-min diff-current-hunk))
|
|
325 (max (diff-hunk-max diff-current-hunk))
|
|
326 (regexp (if old-file-p diff-old-file-pattern diff-new-file-pattern)))
|
|
327 (goto-char min)
|
|
328 (or (and regexp (re-search-forward regexp max t))
|
|
329 (error "Unable to locate a file line for %s file."
|
|
330 (if old-file-p "old" "new")))
|
|
331 (string-to-int (buffer-substring (match-beginning 1) (match-end 1))))))
|
|
332
|
|
333 (defun diff-run-diff (switches old old-temp new new-temp)
|
|
334 ;; Actually run the diff process with SWITCHES on OLD and NEW.
|
|
335 ;; OLD-TEMP and NEW-TEMP are names of temp files that can be used
|
|
336 ;; to dump the data out to.
|
|
337 (insert "diff " (mapconcat 'identity switches " ") " " old
|
|
338 " " new "\n")
|
|
339 (apply 'call-process "diff" nil t nil
|
|
340 (append switches (list old-temp new-temp))))
|
|
341
|
|
342
|
|
343 (defun diff-fix-file-names (old old-temp new new-temp pattern)
|
|
344 ;; Replaces any temp file names with the real names of files.
|
|
345 (save-excursion
|
|
346 (save-restriction
|
|
347 (let ((files (list old new))
|
|
348 (temps (list old-temp new-temp))
|
|
349 buffer-read-only case-fold-search)
|
|
350 (goto-char (point-min))
|
|
351 (if (re-search-forward pattern nil t)
|
|
352 (narrow-to-region (point-min) (match-beginning 0)))
|
|
353 (while files
|
|
354 (let ((regexp (concat "[ \t\n]\\("
|
|
355 (regexp-quote (car temps))
|
|
356 "\\)[ \t\n]")))
|
|
357 (goto-char (point-min))
|
|
358 (forward-line 1)
|
|
359 (while (re-search-forward regexp nil t)
|
|
360 (goto-char (match-beginning 1))
|
|
361 (delete-region (point) (match-end 1))
|
|
362 (insert (car files))))
|
|
363 (setq files (cdr files)
|
|
364 temps (cdr temps)))))))
|
|
365
|
|
366 ;;;; User commands
|
|
367
|
|
368 (defun diff-mode ()
|
|
369 "Diff Mode is used by \\[diff] for perusing the output from the diff program.
|
|
370 All normal editing commands are turned off. Instead, these are available:
|
|
371 \\<diff-mode-map>
|
|
372 \\[diff-advertised-scroll-up] Scroll to next screen of this difference.
|
|
373 \\[diff-advertised-scroll-down] Scroll to previous screen of this difference.
|
|
374 \\[diff-next-difference] Move to Next Difference.
|
|
375 \\[diff-previous-difference] Move to Previous Difference.
|
|
376 \\[diff-show-difference] Jump to difference specified by numeric position.
|
|
377 \\[diff-find-file] Find current diff in file
|
|
378 \\[diff-find-file-other-window] Find current diff in file in other window
|
|
379 \\[diff-display-file] Display file in other window
|
|
380 \\[diff-narrow] Narrow diff buffer to current difference
|
|
381 \\[widen] Widen diff buffer
|
|
382 \\[diff-show-header] Show diff header describing file name etc.
|
|
383 \\[diff-quit] Quit diff
|
|
384 "
|
|
385 (interactive)
|
|
386 (use-local-map diff-mode-map)
|
|
387 (diff-grok-keymap)
|
|
388 (setq buffer-read-only t
|
|
389 major-mode 'diff-mode
|
|
390 mode-name "Diff"
|
|
391 mode-line-modified "--- "
|
|
392 mode-line-process
|
|
393 '(" " diff-current-difference "/" diff-total-differences))
|
|
394 (diff-parse-hunks)
|
|
395 (setq diff-total-differences (int-to-string diff-total-hunks)))
|
|
396
|
|
397 ;;; Motion commands
|
|
398
|
|
399 (defun diff-next-difference (n)
|
|
400 "In diff-mode go the the beginning of the next difference hunk."
|
|
401 (interactive "p")
|
|
402 (if (zerop n)
|
|
403 (goto-char (diff-hunk-min diff-current-hunk))
|
|
404 (let ((narrow (diff-buffer-narrowed-p))
|
|
405 (max (point-max))
|
|
406 (min (point-min)))
|
|
407 (unwind-protect
|
|
408 (progn
|
|
409 (widen)
|
|
410 (setq diff-current-hunk (+ n diff-current-hunk))
|
|
411 (cond ((> diff-current-hunk diff-total-hunks)
|
|
412 (setq diff-current-hunk diff-total-hunks)
|
|
413 (message "No following difference hunks."))
|
|
414 ((< diff-current-hunk 0)
|
|
415 (setq diff-current-hunk 0)
|
|
416 (message "No preceding difference hunks.")))
|
|
417 (setq diff-current-difference (int-to-string diff-current-hunk)
|
|
418 min (goto-char (diff-hunk-min diff-current-hunk))
|
|
419 max (diff-hunk-max diff-current-hunk)))
|
|
420 (if narrow (narrow-to-region min max))))
|
|
421 (set-buffer-modified-p (buffer-modified-p))))
|
|
422
|
|
423 (defun diff-previous-difference (n)
|
|
424 "In diff-mode go the the beginning of the previous difference hunk."
|
|
425 (interactive "p")
|
|
426 (diff-next-difference (- n)))
|
|
427
|
|
428 (defun diff-next-line (n)
|
|
429 "In diff-mode go to the next line."
|
|
430 (interactive "p")
|
|
431 (condition-case nil
|
|
432 (next-line n)
|
|
433 (error (if (> n 0) (message "End of difference hunk"))))
|
|
434 (diff-update-modeline))
|
|
435
|
|
436 (defun diff-previous-line (n)
|
|
437 "In diff-mode go to the previous line."
|
|
438 (interactive "p")
|
|
439 (diff-next-line (- n)))
|
|
440
|
|
441 (defun diff-forward-char (n)
|
|
442 "In diff-mode move the point forward."
|
|
443 (interactive "p")
|
|
444 (forward-char n)
|
|
445 (diff-update-modeline))
|
|
446
|
|
447 (defun diff-backward-char (n)
|
|
448 "In diff-mode move the point backward."
|
|
449 (interactive "p")
|
|
450 (backward-char n)
|
|
451 (diff-update-modeline))
|
|
452
|
|
453 (defun diff-scroll-up (n)
|
|
454 "In diff-mode scroll the buffer up."
|
|
455 (interactive "P")
|
|
456 (scroll-up n)
|
|
457 (diff-update-modeline))
|
|
458
|
|
459 (fset 'diff-advertised-scroll-up 'diff-scroll-up)
|
|
460
|
|
461 (defun diff-scroll-down (n)
|
|
462 "In diff-mode scroll the buffer down."
|
|
463 (interactive "P")
|
|
464 (scroll-down n)
|
|
465 (diff-update-modeline))
|
|
466
|
|
467 (fset 'diff-advertised-scroll-down 'diff-scroll-down)
|
|
468
|
|
469 (defun diff-beginning-of-buffer (n)
|
|
470 "In diff-mode go to the beginning of the buffer."
|
|
471 (interactive "P")
|
|
472 (beginning-of-buffer n)
|
|
473 (diff-update-modeline))
|
|
474
|
|
475 (defun diff-end-of-buffer (n)
|
|
476 "In diff-mode go to the end of the buffer."
|
|
477 (interactive "P")
|
|
478 (end-of-buffer n)
|
|
479 (diff-update-modeline))
|
|
480
|
|
481 ;;; The main command.
|
0
|
482
|
26
|
483 ;;;###autoload
|
24
|
484 (defun diff (old new &optional switches)
|
|
485 "Find and display the differences between OLD and NEW files.
|
|
486 Interactively you are prompted with the current buffer's file name for NEW
|
|
487 and what appears to be its backup for OLD."
|
|
488 ;; Support for diffing directories is rather limited. It needs work.
|
|
489 (interactive (diff-read-args "Diff original file (%s) "
|
|
490 "Diff new file (%s) "
|
|
491 "Switches for diff (%s) "
|
|
492 (buffer-file-name)))
|
|
493 (setq switches (diff-fix-switches (or switches diff-switches))
|
|
494 old (expand-file-name old)
|
|
495 new (expand-file-name new))
|
|
496 (let ((curr-buff (current-buffer))
|
|
497 doing-dirs old-temp new-temp old-buffer new-buffer flag)
|
|
498 (let ((fdp-old (file-directory-p old))
|
|
499 (fdp-new (file-directory-p new)))
|
|
500 (cond
|
|
501 ((null (or fdp-new fdp-old)))
|
|
502 ((null fdp-new)
|
|
503 (setq old (expand-file-name (file-name-nondirectory new) old)))
|
|
504 ((null fdp-old)
|
|
505 (setq new (expand-file-name (file-name-nondirectory old) new)))
|
|
506 (t (setq doing-dirs t))))
|
|
507 ;; (message "diff %s %s %s..."
|
|
508 ;; (mapconcat (function identity) switches " ") new old)
|
|
509 (message "diff %s %s %s..."
|
|
510 (mapconcat (function identity) switches " ") old new)
|
|
511 (if doing-dirs
|
|
512 (setq diff-old-file nil
|
|
513 diff-new-file nil)
|
|
514 (setq old-temp (make-temp-name (concat diff-temp-template "1"))
|
|
515 new-temp (make-temp-name (concat diff-temp-template "2"))
|
|
516 old-buffer (diff-get-file-buffer old)
|
|
517 new-buffer (diff-get-file-buffer new)
|
|
518 diff-old-file (cons old (cdr old-buffer))
|
|
519 diff-new-file (cons new (cdr new-buffer))))
|
|
520 (let (case-fold-search)
|
|
521 (mapcar (function
|
|
522 (lambda (x)
|
|
523 (if (string-match "[ecu]" x)
|
|
524 (setq flag (aref x (match-beginning 0))))))
|
|
525 switches))
|
|
526 (unwind-protect
|
|
527 (let ((patterns (assq flag diff-search-pattern-alist)))
|
|
528 (set-buffer (get-buffer-create "*Diff Output*"))
|
|
529 (setq default-directory (file-name-directory new)
|
|
530 diff-old-file-pattern (nth 2 patterns)
|
|
531 diff-new-file-pattern (nth 3 patterns)
|
|
532 diff-hunk-pattern (nth 1 patterns))
|
|
533 (let (buffer-read-only)
|
|
534 (if (fboundp 'buffer-disable-undo)
|
|
535 (buffer-disable-undo (current-buffer))
|
|
536 ;; old style (Emacs 18.55 and earlier)
|
|
537 (buffer-disable-undo (current-buffer)))
|
|
538 (widen)
|
|
539 (erase-buffer)
|
|
540 (if doing-dirs
|
|
541 (progn
|
|
542 (diff-run-diff switches old old new new)
|
|
543 (setq diff-hunk-pattern (concat diff-hunk-pattern
|
|
544 "\\|^Only in ")))
|
|
545 (save-excursion
|
|
546 (set-buffer (car old-buffer))
|
|
547 (write-region (point-min) (point-max) old-temp nil 'quiet)
|
|
548 (set-buffer (car new-buffer))
|
|
549 (write-region (point-min) (point-max) new-temp nil 'quiet))
|
|
550 (diff-run-diff switches old old-temp new new-temp))
|
|
551 ;; Need to replace file names
|
|
552 (if (and (not doing-dirs) (memq flag '(?c ?u)))
|
|
553 (diff-fix-file-names old old-temp new new-temp
|
|
554 diff-hunk-pattern))
|
|
555 (diff-mode)
|
|
556 (goto-char (point-min))
|
|
557 (setq diff-current-difference "0"
|
|
558 diff-current-hunk 0)
|
|
559 (if (zerop diff-total-hunks)
|
|
560 (progn
|
|
561 (diff-cleanup-buffers)
|
|
562 (message "No differences"))
|
|
563 (if diff-do-narrow (narrow-to-region (point) (diff-hunk-max 0)))
|
|
564 (display-buffer (current-buffer))
|
|
565 (message "%s difference hunk%s" diff-total-differences
|
|
566 (if (= diff-total-hunks 1) "" "s")))))
|
|
567 (condition-case nil
|
|
568 (delete-file old-temp)
|
|
569 (error nil))
|
|
570 (condition-case nil
|
|
571 (delete-file new-temp)
|
|
572 (error nil))
|
|
573 (set-buffer curr-buff))))
|
|
574
|
26
|
575 ;;;###autoload
|
24
|
576 (defun diff-backup (file &optional switches)
|
|
577 "Diff this file with its backup file or vice versa.
|
|
578 Uses the latest backup, if there are several numerical backups.
|
|
579 If this file is a backup, diff it with its original.
|
|
580 The backup file is the first file given to `diff'."
|
|
581 (interactive (list (read-file-name "Diff (file with backup): ")
|
|
582 (and current-prefix-arg
|
|
583 (diff-read-switches "Diff switches: "))))
|
|
584 (let (bak ori)
|
|
585 (if (backup-file-name-p file)
|
|
586 (setq bak file
|
|
587 ori (file-name-sans-versions file))
|
|
588 (setq bak (or (diff-latest-backup-file file)
|
|
589 (error "No backup found for %s" file))
|
|
590 ori file))
|
|
591 (diff bak ori switches)))
|
|
592
|
|
593 (defun diff-show-difference (n)
|
|
594 "Show difference number N (prefix argument)."
|
|
595 (interactive "p")
|
|
596 (let ((narrowedp (diff-buffer-narrowed-p))
|
|
597 (min (diff-hunk-min diff-current-hunk))
|
|
598 (max (diff-hunk-max diff-current-hunk)))
|
|
599 (unwind-protect
|
|
600 (progn
|
|
601 (widen)
|
|
602 (cond
|
|
603 ((< n 0)
|
|
604 (message "No negative hunks.")
|
|
605 (setq n 0))
|
|
606 ((> n diff-total-hunks)
|
|
607 (message "No hunk %d." n)
|
|
608 (setq n diff-total-hunks)))
|
|
609 (setq diff-current-hunk n
|
|
610 diff-current-difference (int-to-string diff-current-hunk)
|
|
611 min (diff-hunk-min n)
|
|
612 max (diff-hunk-max n))
|
|
613 (goto-char min))
|
|
614 (if narrowedp (narrow-to-region min max))
|
|
615 (set-buffer-modified-p (buffer-modified-p)))))
|
|
616
|
|
617 (defun diff-show-header ()
|
|
618 "Show `diff-header'."
|
|
619 (interactive)
|
|
620 (with-output-to-temp-buffer "*Diff Header*"
|
|
621 (princ (save-restriction
|
|
622 (widen)
|
|
623 (buffer-substring (diff-hunk-min 0) (diff-hunk-max 0))))))
|
|
624
|
|
625
|
|
626 (defun diff-find-file (old-file-p)
|
|
627 "Visit diffed file, at the point corresponding to the current hunk.
|
|
628 Default is to visit the new file; prefix means visit old file instead."
|
|
629 (interactive "P")
|
|
630 (let ((line (diff-file-line old-file-p)))
|
|
631 (find-file
|
|
632 (if old-file-p
|
|
633 (car diff-old-file)
|
|
634 (car diff-new-file)))
|
|
635 (goto-line line)
|
|
636 (recenter 0)))
|
|
637
|
|
638 (defun diff-find-file-other-window (old-file-p)
|
|
639 "Visit the diffed file in other window, with the point at the current hunk.
|
|
640 Default is to visit the new file; prefix means visit the old file instead."
|
|
641 (interactive "P")
|
|
642 (let ((line (diff-file-line old-file-p)))
|
|
643 (find-file-other-window
|
|
644 (if old-file-p
|
|
645 (car diff-old-file)
|
|
646 (car diff-new-file)))
|
|
647 (goto-line line)
|
|
648 (recenter 0)))
|
|
649
|
|
650 (defun diff-find-file-other-frame (old-file-p)
|
|
651 "Visit the diffed file in other frame, with point at the current hunk.
|
|
652 Default is to visit the new file; prefix means visit the old file instead."
|
|
653 (interactive "P")
|
|
654 (let ((line (diff-file-line old-file-p)))
|
|
655 (find-file-other-frame
|
|
656 (if old-file-p
|
|
657 (car diff-old-file)
|
|
658 (car diff-new-file)))
|
|
659 (goto-line line)
|
|
660 (recenter 0)))
|
|
661
|
|
662 (defun diff-display-file (old-file-p)
|
|
663 "Display the diffed file in other window, with point at the current hunk.
|
|
664 Default is to visit the new file; prefix means visit the old file instead."
|
|
665 (interactive "P")
|
|
666 (let ((line (diff-file-line old-file-p))
|
|
667 (wind (display-buffer (find-file-noselect (if old-file-p
|
|
668 (car diff-old-file)
|
|
669 (car diff-new-file)))))
|
|
670 (curr-wind (selected-window)))
|
|
671 (unwind-protect
|
|
672 (progn
|
|
673 (select-window wind)
|
|
674 (goto-line line)
|
|
675 (recenter 0))
|
|
676 (select-window curr-wind))))
|
|
677
|
|
678 (defun diff-quit ()
|
|
679 "Quit diff by killing the diff buffer."
|
|
680 (interactive)
|
|
681 (kill-buffer "*Diff Output*")
|
|
682 (diff-cleanup-buffers))
|
|
683
|
|
684 (defun diff-narrow ()
|
|
685 "Narrow diff buffer to current difference hunk."
|
|
686 (interactive)
|
|
687 (narrow-to-region (diff-hunk-min diff-current-hunk)
|
|
688 (diff-hunk-max diff-current-hunk)))
|
|
689
|
|
690 ;;; Run any load hooks
|
|
691 (run-hooks 'diff-load-hook)
|
|
692
|
|
693 ;;; end of diff.el
|