1 ;;; diff.el --- "DIFF" mode for handling output from unix diff utility.
3 ;; Copyright (C) 1990 Free Software Foundation, Inc.
4 ;; Written fpb@ittc.wec.com 1/27/89
6 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 1, or (at your option)
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to
20 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 ;; todo: diff-switches flexibility:
23 ;; (defconst diff-switches-function
25 ;; (if (string-match "\\.el$" file)
28 ;; "Function to return switches to pass to the `diff' utility, in \\[diff].
29 ;; This function is called with one arg, a file name, and returns a string
30 ;; containing 0 or more arguments which are passed on to `diff'.
31 ;; NOTE: This is not an ordinary hook; it may not be a list of functions.")
33 ;; - fpb@ittc.wec.com - Sep 25, 1990
34 ;; Added code to support sccs diffing.
35 ;; also fixed one minor glitch in the
36 ;; search for the pattern. If you only 1 addition you won't find the end
37 ;; of the pattern (minor)
40 (defvar diff-switches nil
41 "*A list of switches to pass to the diff program.")
43 (defvar diff-search-pattern
"^\\([0-9]\\|\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\)"
44 "Regular expression that delineates difference regions in diffs.")
46 (defvar diff-rcs-extension
",v"
47 "*Extension to find RCS file, some systems do not use ,v")
49 ;; Initialize the keymap if it isn't already
50 (if (boundp 'diff-mode-map
)
52 (setq diff-mode-map
(make-keymap))
53 (suppress-keymap diff-mode-map
)
54 (define-key diff-mode-map
"?" 'describe-mode
)
55 (define-key diff-mode-map
"." 'diff-beginning-of-diff
)
56 (define-key diff-mode-map
" " 'scroll-up
)
57 (define-key diff-mode-map
"\177" 'scroll-down
)
58 (define-key diff-mode-map
"n" 'diff-next-difference
)
59 (define-key diff-mode-map
"p" 'diff-previous-difference
)
60 (define-key diff-mode-map
"j" 'diff-show-difference
))
64 "Find and display the differences between OLD and NEW files.
65 Interactively the current buffer's file name is the default for for NEW
66 and a backup file for NEW is the default for OLD."
71 (setq newf
(buffer-file-name)
72 newf
(if (and newf
(file-exists-p newf
))
74 (concat "Diff new file: ("
75 (file-name-nondirectory newf
) ") ")
77 (read-file-name "Diff new file: " nil nil t
)))
78 (setq oldf
(file-newest-backup newf
)
79 oldf
(if (and oldf
(file-exists-p oldf
))
81 (concat "Diff original file: ("
82 (file-name-nondirectory oldf
) ") ")
83 (file-name-directory oldf
) oldf t
)
84 (read-file-name "Diff original file: "
85 (file-name-directory newf
) nil t
)))))))
86 (message "Comparing files %s %s..." new old
)
87 (setq new
(expand-file-name new
)
88 old
(expand-file-name old
))
89 (diff-internal-diff "diff" (append diff-switches
(list new old
)) nil
))
91 (defun diff-sccs (new)
92 "Find and display the differences between OLD and SCCS files."
96 (setq newf
(buffer-file-name)
97 newf
(if (and newf
(file-exists-p newf
))
99 (concat "Diff new file: ("
100 (file-name-nondirectory newf
) ") ")
102 (read-file-name "Diff new file: " nil nil t
))))))
104 (message "Comparing SCCS file %s..." new
)
105 (setq new
(expand-file-name new
))
106 (if (file-exists-p (concat
107 (file-name-directory new
)
109 (file-name-nondirectory new
)))
110 (diff-internal-diff "sccs"
111 (append '("diffs") diff-switches
(list new
))
113 (error "%s does not exist"
114 (concat (file-name-directory new
) "SCCS/s."
115 (file-name-nondirectory new
)))))
117 (defun diff-rcs (new)
118 "Find and display the differences between OLD and RCS files."
122 (setq newf
(buffer-file-name)
123 newf
(if (and newf
(file-exists-p newf
))
125 (concat "Diff new file: ("
126 (file-name-nondirectory newf
) ") ")
128 (read-file-name "Diff new file: " nil nil t
))))))
130 (message "Comparing RCS file %s..." new
)
131 (let* ((fullname (expand-file-name new
))
132 (rcsfile (concat (file-name-directory fullname
)
134 (file-name-nondirectory fullname
)
135 diff-rcs-extension
)))
136 (if (file-exists-p rcsfile
)
137 (diff-internal-diff "rcsdiff" (append diff-switches
(list fullname
)) 4)
138 (error "%s does not exist" rcsfile
))))
140 (defun diff-internal-diff (diff-command sw strip
)
141 (let ((buffer-read-only nil
))
142 (with-output-to-temp-buffer "*Diff Output*"
143 (buffer-disable-undo standard-output
)
145 (set-buffer standard-output
)
147 (apply 'call-process diff-command nil t nil sw
)))
148 (set-buffer "*Diff Output*")
149 (goto-char (point-min))
151 (if (string= (car sw
) "-c")
152 ;; strip leading filenames from context diffs
153 (progn (forward-line 2) (delete-region (point-min) (point))))
154 (if (and (string= (car sw
) "-C") (string= "sccs" diff-command
))
155 ;; strip stuff from SCCS context diffs
156 (progn (forward-line 2) (delete-region (point-min) (point))))
159 ;; strip stuff from SCCS context diffs
160 (progn (forward-line strip
) (delete-region (point-min) (point)))))
162 (if (string= "0" diff-total-differences
)
163 (let ((buffer-read-only nil
))
164 (insert (message "There are no differences.")))
165 (narrow-to-region (point) (progn
167 (if (re-search-forward diff-search-pattern
169 (goto-char (match-beginning 0))
170 (goto-char (point-max)))))
171 (setq diff-current-difference
"1")))
173 ;; Take a buffer full of Unix diff output and go into a mode to easily
174 ;; see the next and previous difference
176 "Diff Mode is used by \\[diff] for perusing the output from the diff program.
177 All normal editing commands are turned off. Instead, these are available:
179 \\[diff-beginning-of-diff] Move point to start of this difference.
180 \\[scroll-up] Scroll to next screen of this difference.
181 \\[scroll-down] Scroll to previous screen of this difference.
182 \\[diff-next-difference] Move to Next Difference.
183 \\[diff-previous-difference] Move to Previous Difference.
184 \\[diff-show-difference] Jump to difference specified by numeric position.
187 (use-local-map diff-mode-map
)
188 (setq buffer-read-only t
189 major-mode
'diff-mode
191 mode-line-modified
"--- "
193 '(" " diff-current-difference
"/" diff-total-differences
))
194 (make-local-variable 'diff-current-difference
)
195 (set (make-local-variable 'diff-total-differences
)
196 (int-to-string (diff-count-differences))))
198 (defun diff-next-difference (n)
199 "Go to the beginning of the next difference.
200 Differences are delimited by `diff-search-pattern'."
202 (if (< n
0) (diff-previous-difference (- n
))
204 (goto-char (point-min))
205 (forward-line 1) ; to get past the match for the start of this diff
207 (if (re-search-forward diff-search-pattern nil
'move n
)
208 (let ((start (goto-char (match-beginning 0))))
210 (if (re-search-forward diff-search-pattern nil
'move
)
211 (goto-char (match-beginning 0)))
212 (narrow-to-region start
(point))
213 (setq diff-current-difference
214 (int-to-string (+ n
(string-to-int
215 diff-current-difference
)))))
216 (re-search-backward diff-search-pattern nil
)
217 (narrow-to-region (point) (point-max))
218 (message "No following differences.")
219 (setq diff-current-difference diff-total-differences
))
220 (goto-char (point-min)))))
222 (defun diff-previous-difference (n)
223 "Go the the beginning of the previous difference.
224 Differences are delimited by `diff-search-pattern'."
226 (if (< n
0) (diff-next-difference (- n
))
228 (goto-char (point-min))
230 (if (re-search-backward diff-search-pattern nil
'move n
)
231 (setq diff-current-difference
232 (int-to-string (- (string-to-int diff-current-difference
) n
)))
233 (message "No previous differences.")
234 (setq diff-current-difference
"1"))
235 (narrow-to-region (point) (progn
237 (re-search-forward diff-search-pattern nil
)
238 (goto-char (match-beginning 0))))
239 (goto-char (point-min)))))
241 (defun diff-show-difference (n)
242 "Show difference number N (prefix argument)."
244 (let ((cur (string-to-int diff-current-difference
)))
247 (not (natnump n
))) ; should signal an error perhaps.
249 (goto-char (point-min)))
251 (diff-previous-difference (- cur n
)))
253 (diff-next-difference (- n cur
))))))
255 (defun diff-beginning-of-diff ()
256 "Go to beginning of current difference."
258 (goto-char (point-min)))
260 ;; This function counts up the number of differences in the buffer.
261 (defun diff-count-differences ()
262 "Count number of differences in the current buffer."
263 (message "Counting differences...")
267 (goto-char (point-min))
269 (while (re-search-forward diff-search-pattern nil t
)
271 (message "Counting differences...done (%d)" cnt
)
274 ;;; diff.el ends here