1 ;;; diff.el --- "DIFF" mode for handling output from unix diff utility.
3 ;; Author: Frank P. Bresz <fpb@ittc.wec.com>
5 ;; Created: 27 Jan 1989
6 ;; Last-Modified: 21 Dec 1992
7 ;; Keyword: unix, tools
9 ;; Copyright (C) 1990 Free Software Foundation, Inc.
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to
25 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
29 ;; todo: diff-switches flexibility:
30 ;; (defconst diff-switches-function
32 ;; (if (string-match "\\.el$" file)
35 ;; "Function to return switches to pass to the `diff' utility, in \\[diff].
36 ;; This function is called with one arg, a file name, and returns a string
37 ;; containing 0 or more arguments which are passed on to `diff'.
38 ;; NOTE: This is not an ordinary hook; it may not be a list of functions.")
40 ;; - fpb@ittc.wec.com - Sep 25, 1990
41 ;; Added code to support sccs diffing.
42 ;; also fixed one minor glitch in the
43 ;; search for the pattern. If you only 1 addition you won't find the end
44 ;; of the pattern (minor)
48 (defvar diff-switches nil
49 "*A list of switches to pass to the diff program.")
51 (defvar diff-search-pattern
"^\\([0-9]\\|\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\)"
52 "Regular expression that delineates difference regions in diffs.")
54 (defvar diff-rcs-extension
",v"
55 "*Extension to find RCS file, some systems do not use ,v")
57 ;; Initialize the keymap if it isn't already
58 (if (boundp 'diff-mode-map
)
60 (setq diff-mode-map
(make-keymap))
61 (suppress-keymap diff-mode-map
)
62 (define-key diff-mode-map
"?" 'describe-mode
)
63 (define-key diff-mode-map
"." 'diff-beginning-of-diff
)
64 (define-key diff-mode-map
" " 'scroll-up
)
65 (define-key diff-mode-map
"\177" 'scroll-down
)
66 (define-key diff-mode-map
"n" 'diff-next-difference
)
67 (define-key diff-mode-map
"p" 'diff-previous-difference
)
68 (define-key diff-mode-map
"j" 'diff-show-difference
))
72 "Find and display the differences between OLD and NEW files.
73 Interactively the current buffer's file name is the default for for NEW
74 and a backup file for NEW is the default for OLD."
79 (setq newf
(buffer-file-name)
80 newf
(if (and newf
(file-exists-p newf
))
82 (concat "Diff new file: ("
83 (file-name-nondirectory newf
) ") ")
85 (read-file-name "Diff new file: " nil nil t
)))
86 (setq oldf
(file-newest-backup newf
)
87 oldf
(if (and oldf
(file-exists-p oldf
))
89 (concat "Diff original file: ("
90 (file-name-nondirectory oldf
) ") ")
91 (file-name-directory oldf
) oldf t
)
92 (read-file-name "Diff original file: "
93 (file-name-directory newf
) nil t
)))))))
94 (message "Comparing files %s %s..." new old
)
95 (setq new
(expand-file-name new
)
96 old
(expand-file-name old
))
97 (diff-internal-diff "diff" (append diff-switches
(list new old
)) nil
))
99 (defun diff-sccs (new)
100 "Find and display the differences between OLD and SCCS files."
104 (setq newf
(buffer-file-name)
105 newf
(if (and newf
(file-exists-p newf
))
107 (concat "Diff new file: ("
108 (file-name-nondirectory newf
) ") ")
110 (read-file-name "Diff new file: " nil nil t
))))))
112 (message "Comparing SCCS file %s..." new
)
113 (setq new
(expand-file-name new
))
114 (if (file-exists-p (concat
115 (file-name-directory new
)
117 (file-name-nondirectory new
)))
118 (diff-internal-diff "sccs"
119 (append '("diffs") diff-switches
(list new
))
121 (error "%s does not exist"
122 (concat (file-name-directory new
) "SCCS/s."
123 (file-name-nondirectory new
)))))
125 (defun diff-rcs (new)
126 "Find and display the differences between OLD and RCS files."
130 (setq newf
(buffer-file-name)
131 newf
(if (and newf
(file-exists-p newf
))
133 (concat "Diff new file: ("
134 (file-name-nondirectory newf
) ") ")
136 (read-file-name "Diff new file: " nil nil t
))))))
138 (message "Comparing RCS file %s..." new
)
139 (let* ((fullname (expand-file-name new
))
140 (rcsfile (concat (file-name-directory fullname
)
142 (file-name-nondirectory fullname
)
143 diff-rcs-extension
)))
144 (if (file-exists-p rcsfile
)
145 (diff-internal-diff "rcsdiff" (append diff-switches
(list fullname
)) 4)
146 (error "%s does not exist" rcsfile
))))
148 (defun diff-internal-diff (diff-command sw strip
)
149 (let ((buffer-read-only nil
))
150 (with-output-to-temp-buffer "*Diff Output*"
151 (buffer-disable-undo standard-output
)
153 (set-buffer standard-output
)
155 (apply 'call-process diff-command nil t nil sw
)))
156 (set-buffer "*Diff Output*")
157 (goto-char (point-min))
159 (if (string= (car sw
) "-c")
160 ;; strip leading filenames from context diffs
161 (progn (forward-line 2) (delete-region (point-min) (point))))
162 (if (and (string= (car sw
) "-C") (string= "sccs" diff-command
))
163 ;; strip stuff from SCCS context diffs
164 (progn (forward-line 2) (delete-region (point-min) (point))))
167 ;; strip stuff from SCCS context diffs
168 (progn (forward-line strip
) (delete-region (point-min) (point)))))
170 (if (string= "0" diff-total-differences
)
171 (let ((buffer-read-only nil
))
172 (insert (message "There are no differences.")))
173 (narrow-to-region (point) (progn
175 (if (re-search-forward diff-search-pattern
177 (goto-char (match-beginning 0))
178 (goto-char (point-max)))))
179 (setq diff-current-difference
"1")))
181 ;; Take a buffer full of Unix diff output and go into a mode to easily
182 ;; see the next and previous difference
184 "Diff Mode is used by \\[diff] for perusing the output from the diff program.
185 All normal editing commands are turned off. Instead, these are available:
187 \\[diff-beginning-of-diff] Move point to start of this difference.
188 \\[scroll-up] Scroll to next screen of this difference.
189 \\[scroll-down] Scroll to previous screen of this difference.
190 \\[diff-next-difference] Move to Next Difference.
191 \\[diff-previous-difference] Move to Previous Difference.
192 \\[diff-show-difference] Jump to difference specified by numeric position.
195 (use-local-map diff-mode-map
)
196 (setq buffer-read-only t
197 major-mode
'diff-mode
199 mode-line-modified
"--- "
201 '(" " diff-current-difference
"/" diff-total-differences
))
202 (make-local-variable 'diff-current-difference
)
203 (set (make-local-variable 'diff-total-differences
)
204 (int-to-string (diff-count-differences))))
206 (defun diff-next-difference (n)
207 "Go to the beginning of the next difference.
208 Differences are delimited by `diff-search-pattern'."
210 (if (< n
0) (diff-previous-difference (- n
))
212 (goto-char (point-min))
213 (forward-line 1) ; to get past the match for the start of this diff
215 (if (re-search-forward diff-search-pattern nil
'move n
)
216 (let ((start (goto-char (match-beginning 0))))
218 (if (re-search-forward diff-search-pattern nil
'move
)
219 (goto-char (match-beginning 0)))
220 (narrow-to-region start
(point))
221 (setq diff-current-difference
222 (int-to-string (+ n
(string-to-int
223 diff-current-difference
)))))
224 (re-search-backward diff-search-pattern nil
)
225 (narrow-to-region (point) (point-max))
226 (message "No following differences.")
227 (setq diff-current-difference diff-total-differences
))
228 (goto-char (point-min)))))
230 (defun diff-previous-difference (n)
231 "Go the the beginning of the previous difference.
232 Differences are delimited by `diff-search-pattern'."
234 (if (< n
0) (diff-next-difference (- n
))
236 (goto-char (point-min))
238 (if (re-search-backward diff-search-pattern nil
'move n
)
239 (setq diff-current-difference
240 (int-to-string (- (string-to-int diff-current-difference
) n
)))
241 (message "No previous differences.")
242 (setq diff-current-difference
"1"))
243 (narrow-to-region (point) (progn
245 (re-search-forward diff-search-pattern nil
)
246 (goto-char (match-beginning 0))))
247 (goto-char (point-min)))))
249 (defun diff-show-difference (n)
250 "Show difference number N (prefix argument)."
252 (let ((cur (string-to-int diff-current-difference
)))
255 (not (natnump n
))) ; should signal an error perhaps.
257 (goto-char (point-min)))
259 (diff-previous-difference (- cur n
)))
261 (diff-next-difference (- n cur
))))))
263 (defun diff-beginning-of-diff ()
264 "Go to beginning of current difference."
266 (goto-char (point-min)))
268 ;; This function counts up the number of differences in the buffer.
269 (defun diff-count-differences ()
270 "Count number of differences in the current buffer."
271 (message "Counting differences...")
275 (goto-char (point-min))
277 (while (re-search-forward diff-search-pattern nil t
)
279 (message "Counting differences...done (%d)" cnt
)
282 ;;; diff.el ends here