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