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