*** empty log message ***
[emacs.git] / lisp / diff.el
blob7a0dd95387c21e3f95ef7cc0d01014615e1ccd34
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)
11 ;; any later version.
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
24 ;; '(lambda (file)
25 ;; (if (string-match "\\.el$" file)
26 ;; "-c -F\"^(\""
27 ;; "-p"))
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)
51 nil
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))
62 ;;;###autoload
63 (defun diff (old new)
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."
67 (interactive
68 (let (oldf newf)
69 (reverse
70 (list
71 (setq newf (buffer-file-name)
72 newf (if (and newf (file-exists-p newf))
73 (read-file-name
74 (concat "Diff new file: ("
75 (file-name-nondirectory newf) ") ")
76 nil newf t)
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))
80 (read-file-name
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."
93 (interactive
94 (let (newf)
95 (list
96 (setq newf (buffer-file-name)
97 newf (if (and newf (file-exists-p newf))
98 (read-file-name
99 (concat "Diff new file: ("
100 (file-name-nondirectory newf) ") ")
101 nil newf t)
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)
108 "SCCS/s."
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."
119 (interactive
120 (let (newf)
121 (list
122 (setq newf (buffer-file-name)
123 newf (if (and newf (file-exists-p newf))
124 (read-file-name
125 (concat "Diff new file: ("
126 (file-name-nondirectory newf) ") ")
127 nil newf t)
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)
133 "RCS/"
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)
144 (save-excursion
145 (set-buffer standard-output)
146 (erase-buffer)
147 (apply 'call-process diff-command nil t nil sw)))
148 (set-buffer "*Diff Output*")
149 (goto-char (point-min))
150 (while sw
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))))
157 (setq sw (cdr sw)))
158 (if strip
159 ;; strip stuff from SCCS context diffs
160 (progn (forward-line strip) (delete-region (point-min) (point)))))
161 (diff-mode)
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
166 (forward-line 1)
167 (if (re-search-forward diff-search-pattern
168 nil t)
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
175 (defun diff-mode ()
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:
178 \\<diff-mode-map>
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.
186 (interactive)
187 (use-local-map diff-mode-map)
188 (setq buffer-read-only t
189 major-mode 'diff-mode
190 mode-name "Diff"
191 mode-line-modified "--- "
192 mode-line-process
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'."
201 (interactive "p")
202 (if (< n 0) (diff-previous-difference (- n))
203 (if (zerop n) ()
204 (goto-char (point-min))
205 (forward-line 1) ; to get past the match for the start of this diff
206 (widen)
207 (if (re-search-forward diff-search-pattern nil 'move n)
208 (let ((start (goto-char (match-beginning 0))))
209 (forward-line 1)
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'."
225 (interactive "p")
226 (if (< n 0) (diff-next-difference (- n))
227 (if (zerop n) ()
228 (goto-char (point-min))
229 (widen)
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
236 (forward-line 1)
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)."
243 (interactive "p")
244 (let ((cur (string-to-int diff-current-difference)))
245 (cond ((or (= n cur)
246 (zerop n)
247 (not (natnump n))) ; should signal an error perhaps.
248 ;; just redisplay.
249 (goto-char (point-min)))
250 ((< n cur)
251 (diff-previous-difference (- cur n)))
252 ((> n cur)
253 (diff-next-difference (- n cur))))))
255 (defun diff-beginning-of-diff ()
256 "Go to beginning of current difference."
257 (interactive)
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...")
264 (save-excursion
265 (save-restriction
266 (widen)
267 (goto-char (point-min))
268 (let ((cnt 0))
269 (while (re-search-forward diff-search-pattern nil t)
270 (setq cnt (1+ cnt)))
271 (message "Counting differences...done (%d)" cnt)
272 cnt))))
274 ;;; diff.el ends here