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