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