Mercurial > emacs
annotate lisp/diff.el @ 812:485e82a8acb5
*** empty log message ***
author | Eric S. Raymond <esr@snark.thyrsus.com> |
---|---|
date | Fri, 17 Jul 1992 18:53:44 +0000 |
parents | e694e0879463 |
children | 20674ae6bf52 |
rev | line source |
---|---|
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
474
diff
changeset
|
1 ;;; diff.el --- "DIFF" mode for handling output from unix diff utility. |
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
474
diff
changeset
|
2 |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
669
diff
changeset
|
3 ;; Author: Frank P. Bresz <fpb@ittc.wec.com> |
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
669
diff
changeset
|
4 ;; Maintainer: FSF |
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
669
diff
changeset
|
5 ;; Created: 27 Jan 1989 |
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
669
diff
changeset
|
6 ;; Last-Modified: 21 Dec 1992 |
811
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
807
diff
changeset
|
7 ;; Keyword: unix, tools |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
669
diff
changeset
|
8 |
349 | 9 ;; Copyright (C) 1990 Free Software Foundation, Inc. |
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 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
669
diff
changeset
|
15 ;; the Free Software Foundation; either version 2, or (at your option) |
349 | 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 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
669
diff
changeset
|
27 ;;; Commentary: |
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
669
diff
changeset
|
28 |
349 | 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 | |
474 | 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 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
669
diff
changeset
|
46 ;;; Code: |
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
669
diff
changeset
|
47 |
349 | 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 | |
474 | 54 (defvar diff-rcs-extension ",v" |
55 "*Extension to find RCS file, some systems do not use ,v") | |
56 | |
349 | 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. | |
669
4c64c671426f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
662
diff
changeset
|
73 Interactively the current buffer's file name is the default for for NEW |
4c64c671426f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
662
diff
changeset
|
74 and a backup file for NEW is the default for OLD." |
349 | 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)) | |
474 | 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)) | |
349 | 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) | |
474 | 155 (apply 'call-process diff-command nil t nil sw))) |
349 | 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)))) | |
474 | 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))))) | |
349 | 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 | |
474 | 181 ;; Take a buffer full of Unix diff output and go into a mode to easily |
349 | 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. | |
185 All 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) | |
474 | 207 "Go to the beginning of the next difference. |
208 Differences are delimited by `diff-search-pattern'." | |
349 | 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) | |
474 | 231 "Go the the beginning of the previous difference. |
232 Differences are delimited by `diff-search-pattern'." | |
349 | 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) | |
474 | 250 "Show difference number N (prefix argument)." |
349 | 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)))) | |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
474
diff
changeset
|
281 |
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
474
diff
changeset
|
282 ;;; diff.el ends here |