1 ;; "DIFF" mode for handling output from unix diff utility.
2 ;; Copyright (C) 1990 Free Software Foundation, Inc.
3 ;; Written fpb@ittc.wec.com 1/27/89
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 1, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to
19 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21 ;; todo: diff-switches flexibility:
22 ;; (defconst diff-switches-function
24 ;; (if (string-match "\\.el$" file)
27 ;; "Function to return switches to pass to the `diff' utility, in \\[diff].
28 ;; This function is called with one arg, a file name, and returns a string
29 ;; containing 0 or more arguments which are passed on to `diff'.
30 ;; NOTE: This is not an ordinary hook; it may not be a list of functions.")
32 ;; - fpb@ittc.wec.com - Sep 25, 1990
33 ;; Added code to support sccs diffing.
34 ;; also fixed one minor glitch in the
35 ;; search for the pattern. If you only 1 addition you won't find the end
36 ;; of the pattern (minor)
39 (defvar diff-switches nil
40 "*A list of switches to pass to the diff program.")
42 (defvar diff-search-pattern
"^\\([0-9]\\|\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\)"
43 "Regular expression that delineates difference regions in diffs.")
45 (defvar diff-rcs-extension
",v"
46 "*Extension to find RCS file, some systems do not use ,v")
48 ;; Initialize the keymap if it isn't already
49 (if (boundp 'diff-mode-map
)
51 (setq diff-mode-map
(make-keymap))
52 (suppress-keymap diff-mode-map
)
53 (define-key diff-mode-map
"?" 'describe-mode
)
54 (define-key diff-mode-map
"." 'diff-beginning-of-diff
)
55 (define-key diff-mode-map
" " 'scroll-up
)
56 (define-key diff-mode-map
"\177" 'scroll-down
)
57 (define-key diff-mode-map
"n" 'diff-next-difference
)
58 (define-key diff-mode-map
"p" 'diff-previous-difference
)
59 (define-key diff-mode-map
"j" 'diff-show-difference
))
63 "Find and display the differences between OLD and NEW files.
64 Interactively you are prompted with the current buffer's file name for NEW
65 and what appears to be it's backup for OLD."
70 (setq newf
(buffer-file-name)
71 newf
(if (and newf
(file-exists-p newf
))
73 (concat "Diff new file: ("
74 (file-name-nondirectory newf
) ") ")
76 (read-file-name "Diff new file: " nil nil t
)))
77 (setq oldf
(file-newest-backup newf
)
78 oldf
(if (and oldf
(file-exists-p oldf
))
80 (concat "Diff original file: ("
81 (file-name-nondirectory oldf
) ") ")
82 (file-name-directory oldf
) oldf t
)
83 (read-file-name "Diff original file: "
84 (file-name-directory newf
) nil t
)))))))
85 (message "Comparing files %s %s..." new old
)
86 (setq new
(expand-file-name new
)
87 old
(expand-file-name old
))
88 (diff-internal-diff "diff" (append diff-switches
(list new old
)) nil
))
90 (defun diff-sccs (new)
91 "Find and display the differences between OLD and SCCS files."
95 (setq newf
(buffer-file-name)
96 newf
(if (and newf
(file-exists-p newf
))
98 (concat "Diff new file: ("
99 (file-name-nondirectory newf
) ") ")
101 (read-file-name "Diff new file: " nil nil t
))))))
103 (message "Comparing SCCS file %s..." new
)
104 (setq new
(expand-file-name new
))
105 (if (file-exists-p (concat
106 (file-name-directory new
)
108 (file-name-nondirectory new
)))
109 (diff-internal-diff "sccs"
110 (append '("diffs") diff-switches
(list new
))
112 (error "%s does not exist"
113 (concat (file-name-directory new
) "SCCS/s."
114 (file-name-nondirectory new
)))))
116 (defun diff-rcs (new)
117 "Find and display the differences between OLD and RCS files."
121 (setq newf
(buffer-file-name)
122 newf
(if (and newf
(file-exists-p newf
))
124 (concat "Diff new file: ("
125 (file-name-nondirectory newf
) ") ")
127 (read-file-name "Diff new file: " nil nil t
))))))
129 (message "Comparing RCS file %s..." new
)
130 (let* ((fullname (expand-file-name new
))
131 (rcsfile (concat (file-name-directory fullname
)
133 (file-name-nondirectory fullname
)
134 diff-rcs-extension
)))
135 (if (file-exists-p rcsfile
)
136 (diff-internal-diff "rcsdiff" (append diff-switches
(list fullname
)) 4)
137 (error "%s does not exist" rcsfile
))))
139 (defun diff-internal-diff (diff-command sw strip
)
140 (let ((buffer-read-only nil
))
141 (with-output-to-temp-buffer "*Diff Output*"
142 (buffer-disable-undo standard-output
)
144 (set-buffer standard-output
)
146 (apply 'call-process diff-command nil t nil sw
)))
147 (set-buffer "*Diff Output*")
148 (goto-char (point-min))
150 (if (string= (car sw
) "-c")
151 ;; strip leading filenames from context diffs
152 (progn (forward-line 2) (delete-region (point-min) (point))))
153 (if (and (string= (car sw
) "-C") (string= "sccs" diff-command
))
154 ;; strip stuff from SCCS context diffs
155 (progn (forward-line 2) (delete-region (point-min) (point))))
158 ;; strip stuff from SCCS context diffs
159 (progn (forward-line strip
) (delete-region (point-min) (point)))))
161 (if (string= "0" diff-total-differences
)
162 (let ((buffer-read-only nil
))
163 (insert (message "There are no differences.")))
164 (narrow-to-region (point) (progn
166 (if (re-search-forward diff-search-pattern
168 (goto-char (match-beginning 0))
169 (goto-char (point-max)))))
170 (setq diff-current-difference
"1")))
172 ;; Take a buffer full of Unix diff output and go into a mode to easily
173 ;; see the next and previous difference
175 "Diff Mode is used by \\[diff] for perusing the output from the diff program.
176 All normal editing commands are turned off. Instead, these are available:
178 \\[diff-beginning-of-diff] Move point to start of this difference.
179 \\[scroll-up] Scroll to next screen of this difference.
180 \\[scroll-down] Scroll to previous screen of this difference.
181 \\[diff-next-difference] Move to Next Difference.
182 \\[diff-previous-difference] Move to Previous Difference.
183 \\[diff-show-difference] Jump to difference specified by numeric position.
186 (use-local-map diff-mode-map
)
187 (setq buffer-read-only t
188 major-mode
'diff-mode
190 mode-line-modified
"--- "
192 '(" " diff-current-difference
"/" diff-total-differences
))
193 (make-local-variable 'diff-current-difference
)
194 (set (make-local-variable 'diff-total-differences
)
195 (int-to-string (diff-count-differences))))
197 (defun diff-next-difference (n)
198 "Go to the beginning of the next difference.
199 Differences are delimited by `diff-search-pattern'."
201 (if (< n
0) (diff-previous-difference (- n
))
203 (goto-char (point-min))
204 (forward-line 1) ; to get past the match for the start of this diff
206 (if (re-search-forward diff-search-pattern nil
'move n
)
207 (let ((start (goto-char (match-beginning 0))))
209 (if (re-search-forward diff-search-pattern nil
'move
)
210 (goto-char (match-beginning 0)))
211 (narrow-to-region start
(point))
212 (setq diff-current-difference
213 (int-to-string (+ n
(string-to-int
214 diff-current-difference
)))))
215 (re-search-backward diff-search-pattern nil
)
216 (narrow-to-region (point) (point-max))
217 (message "No following differences.")
218 (setq diff-current-difference diff-total-differences
))
219 (goto-char (point-min)))))
221 (defun diff-previous-difference (n)
222 "Go the the beginning of the previous difference.
223 Differences are delimited by `diff-search-pattern'."
225 (if (< n
0) (diff-next-difference (- n
))
227 (goto-char (point-min))
229 (if (re-search-backward diff-search-pattern nil
'move n
)
230 (setq diff-current-difference
231 (int-to-string (- (string-to-int diff-current-difference
) n
)))
232 (message "No previous differences.")
233 (setq diff-current-difference
"1"))
234 (narrow-to-region (point) (progn
236 (re-search-forward diff-search-pattern nil
)
237 (goto-char (match-beginning 0))))
238 (goto-char (point-min)))))
240 (defun diff-show-difference (n)
241 "Show difference number N (prefix argument)."
243 (let ((cur (string-to-int diff-current-difference
)))
246 (not (natnump n
))) ; should signal an error perhaps.
248 (goto-char (point-min)))
250 (diff-previous-difference (- cur n
)))
252 (diff-next-difference (- n cur
))))))
254 (defun diff-beginning-of-diff ()
255 "Go to beginning of current difference."
257 (goto-char (point-min)))
259 ;; This function counts up the number of differences in the buffer.
260 (defun diff-count-differences ()
261 "Count number of differences in the current buffer."
262 (message "Counting differences...")
266 (goto-char (point-min))
268 (while (re-search-forward diff-search-pattern nil t
)
270 (message "Counting differences...done (%d)" cnt
)