*** empty log message ***
[bpt/emacs.git] / lisp / diff.el
CommitLineData
c0274f38
ER
1;;; diff.el --- "DIFF" mode for handling output from unix diff utility.
2
e5167999
ER
3;; Author: Frank P. Bresz <fpb@ittc.wec.com>
4;; Maintainer: FSF
5;; Created: 27 Jan 1989
6;; Last-Modified: 21 Dec 1992
fd7fa35a 7;; Keyword: unix, tools
e5167999 8
b10a4379 9;; Copyright (C) 1990 Free Software Foundation, Inc.
b10a4379
JB
10
11;; This file is part of GNU Emacs.
12
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
e5167999 15;; the Free Software Foundation; either version 2, or (at your option)
b10a4379
JB
16;; any later version.
17
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.
22
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.
26
e5167999
ER
27;;; Commentary:
28
b10a4379
JB
29;; todo: diff-switches flexibility:
30;; (defconst diff-switches-function
31;; '(lambda (file)
32;; (if (string-match "\\.el$" file)
33;; "-c -F\"^(\""
34;; "-p"))
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.")
39
aa228418
JB
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)
45
e5167999
ER
46;;; Code:
47
b10a4379
JB
48(defvar diff-switches nil
49 "*A list of switches to pass to the diff program.")
50
51(defvar diff-search-pattern "^\\([0-9]\\|\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\)"
52 "Regular expression that delineates difference regions in diffs.")
53
aa228418
JB
54(defvar diff-rcs-extension ",v"
55 "*Extension to find RCS file, some systems do not use ,v")
56
b10a4379
JB
57;; Initialize the keymap if it isn't already
58(if (boundp 'diff-mode-map)
59 nil
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))
69
70;;;###autoload
71(defun diff (old new)
72 "Find and display the differences between OLD and NEW files.
9c50f912
RS
73Interactively the current buffer's file name is the default for for NEW
74and a backup file for NEW is the default for OLD."
b10a4379
JB
75 (interactive
76 (let (oldf newf)
77 (reverse
78 (list
79 (setq newf (buffer-file-name)
80 newf (if (and newf (file-exists-p newf))
81 (read-file-name
82 (concat "Diff new file: ("
83 (file-name-nondirectory newf) ") ")
84 nil newf t)
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))
88 (read-file-name
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))
aa228418
JB
97 (diff-internal-diff "diff" (append diff-switches (list new old)) nil))
98
99(defun diff-sccs (new)
100 "Find and display the differences between OLD and SCCS files."
101 (interactive
102 (let (newf)
103 (list
104 (setq newf (buffer-file-name)
105 newf (if (and newf (file-exists-p newf))
106 (read-file-name
107 (concat "Diff new file: ("
108 (file-name-nondirectory newf) ") ")
109 nil newf t)
110 (read-file-name "Diff new file: " nil nil t))))))
111
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)
116 "SCCS/s."
117 (file-name-nondirectory new)))
118 (diff-internal-diff "sccs"
119 (append '("diffs") diff-switches (list new))
120 2)
121 (error "%s does not exist"
122 (concat (file-name-directory new) "SCCS/s."
123 (file-name-nondirectory new)))))
124
125(defun diff-rcs (new)
126 "Find and display the differences between OLD and RCS files."
127 (interactive
128 (let (newf)
129 (list
130 (setq newf (buffer-file-name)
131 newf (if (and newf (file-exists-p newf))
132 (read-file-name
133 (concat "Diff new file: ("
134 (file-name-nondirectory newf) ") ")
135 nil newf t)
136 (read-file-name "Diff new file: " nil nil t))))))
137
138 (message "Comparing RCS file %s..." new)
139 (let* ((fullname (expand-file-name new))
140 (rcsfile (concat (file-name-directory fullname)
141 "RCS/"
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))))
147
148(defun diff-internal-diff (diff-command sw strip)
149 (let ((buffer-read-only nil))
b10a4379
JB
150 (with-output-to-temp-buffer "*Diff Output*"
151 (buffer-disable-undo standard-output)
152 (save-excursion
153 (set-buffer standard-output)
154 (erase-buffer)
aa228418 155 (apply 'call-process diff-command nil t nil sw)))
b10a4379
JB
156 (set-buffer "*Diff Output*")
157 (goto-char (point-min))
158 (while sw
159 (if (string= (car sw) "-c")
160 ;; strip leading filenames from context diffs
161 (progn (forward-line 2) (delete-region (point-min) (point))))
aa228418
JB
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))))
165 (setq sw (cdr sw)))
166 (if strip
167 ;; strip stuff from SCCS context diffs
168 (progn (forward-line strip) (delete-region (point-min) (point)))))
b10a4379
JB
169 (diff-mode)
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
174 (forward-line 1)
175 (if (re-search-forward diff-search-pattern
176 nil t)
177 (goto-char (match-beginning 0))
178 (goto-char (point-max)))))
179 (setq diff-current-difference "1")))
180
aa228418 181;; Take a buffer full of Unix diff output and go into a mode to easily
b10a4379
JB
182;; see the next and previous difference
183(defun diff-mode ()
184 "Diff Mode is used by \\[diff] for perusing the output from the diff program.
185All normal editing commands are turned off. Instead, these are available:
186\\<diff-mode-map>
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.
193"
194 (interactive)
195 (use-local-map diff-mode-map)
196 (setq buffer-read-only t
197 major-mode 'diff-mode
198 mode-name "Diff"
199 mode-line-modified "--- "
200 mode-line-process
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))))
205
206(defun diff-next-difference (n)
aa228418
JB
207 "Go to the beginning of the next difference.
208Differences are delimited by `diff-search-pattern'."
b10a4379
JB
209 (interactive "p")
210 (if (< n 0) (diff-previous-difference (- n))
211 (if (zerop n) ()
212 (goto-char (point-min))
213 (forward-line 1) ; to get past the match for the start of this diff
214 (widen)
215 (if (re-search-forward diff-search-pattern nil 'move n)
216 (let ((start (goto-char (match-beginning 0))))
217 (forward-line 1)
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)))))
229
230(defun diff-previous-difference (n)
aa228418
JB
231 "Go the the beginning of the previous difference.
232Differences are delimited by `diff-search-pattern'."
b10a4379
JB
233 (interactive "p")
234 (if (< n 0) (diff-next-difference (- n))
235 (if (zerop n) ()
236 (goto-char (point-min))
237 (widen)
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
244 (forward-line 1)
245 (re-search-forward diff-search-pattern nil)
246 (goto-char (match-beginning 0))))
247 (goto-char (point-min)))))
248
249(defun diff-show-difference (n)
aa228418 250 "Show difference number N (prefix argument)."
b10a4379
JB
251 (interactive "p")
252 (let ((cur (string-to-int diff-current-difference)))
253 (cond ((or (= n cur)
254 (zerop n)
255 (not (natnump n))) ; should signal an error perhaps.
256 ;; just redisplay.
257 (goto-char (point-min)))
258 ((< n cur)
259 (diff-previous-difference (- cur n)))
260 ((> n cur)
261 (diff-next-difference (- n cur))))))
262
263(defun diff-beginning-of-diff ()
264 "Go to beginning of current difference."
265 (interactive)
266 (goto-char (point-min)))
267
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...")
272 (save-excursion
273 (save-restriction
274 (widen)
275 (goto-char (point-min))
276 (let ((cnt 0))
277 (while (re-search-forward diff-search-pattern nil t)
278 (setq cnt (1+ cnt)))
279 (message "Counting differences...done (%d)" cnt)
280 cnt))))
c0274f38
ER
281
282;;; diff.el ends here