Mercurial > emacs
annotate lisp/diff.el @ 891:f7de428cb8bf
*** empty log message ***
author | Roland McGrath <roland@gnu.org> |
---|---|
date | Tue, 28 Jul 1992 23:26:57 +0000 |
parents | 945558e05127 |
children | 41507a5a8fd7 |
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 |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
811
diff
changeset
|
3 ;; Copyright (C) 1990 Free Software Foundation, Inc. |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
811
diff
changeset
|
4 |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
669
diff
changeset
|
5 ;; Author: Frank P. Bresz <fpb@ittc.wec.com> |
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
669
diff
changeset
|
6 ;; Maintainer: FSF |
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
669
diff
changeset
|
7 ;; Created: 27 Jan 1989 |
811
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
807
diff
changeset
|
8 ;; Keyword: unix, tools |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
669
diff
changeset
|
9 |
349 | 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 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
669
diff
changeset
|
14 ;; the Free Software Foundation; either version 2, or (at your option) |
349 | 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 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
669
diff
changeset
|
26 ;;; Commentary: |
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
669
diff
changeset
|
27 |
349 | 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 | |
474 | 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 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
669
diff
changeset
|
45 ;;; Code: |
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
669
diff
changeset
|
46 |
349 | 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 | |
474 | 53 (defvar diff-rcs-extension ",v" |
54 "*Extension to find RCS file, some systems do not use ,v") | |
55 | |
349 | 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. | |
669
4c64c671426f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
662
diff
changeset
|
72 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
|
73 and a backup file for NEW is the default for OLD." |
349 | 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)) | |
474 | 96 (diff-internal-diff "diff" (append diff-switches (list new old)) nil)) |
97 | |
881
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
98 (defun diff-backup (file) |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
99 "Diff this file with its backup file or vice versa. |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
100 Uses the latest backup, if there are several numerical backups. |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
101 If this file is a backup, diff it with its original. |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
102 The backup file is the first file given to `diff'." |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
103 (interactive "fDiff (file with backup): ") |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
104 (let (bak ori) |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
105 (if (backup-file-name-p file) |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
106 (setq bak file |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
107 ori (file-name-sans-versions file)) |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
108 (setq bak (or (diff-latest-backup-file file) |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
109 (error "No backup found for %s" file)) |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
110 ori file)) |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
111 (diff bak ori))) |
474 | 112 |
881
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
113 (defun diff-latest-backup-file (fn) ; actually belongs into files.el |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
114 "Return the latest existing backup of FILE, or nil." |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
115 ;; First try simple backup, then the highest numbered of the |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
116 ;; numbered backups. |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
117 ;; Ignore the value of version-control because we look for existing |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
118 ;; backups, which maybe were made earlier or by another user with |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
119 ;; a different value of version-control. |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
120 (setq fn (expand-file-name fn)) |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
121 (or |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
122 (let ((bak (make-backup-file-name fn))) |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
123 (if (file-exists-p bak) bak)) |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
124 (let* ((dir (file-name-directory fn)) |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
125 (base-versions (concat (file-name-nondirectory fn) ".~")) |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
126 (bv-length (length base-versions))) |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
127 (concat dir |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
128 (car (sort |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
129 (file-name-all-completions base-versions dir) |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
130 ;; bv-length is a fluid var for backup-extract-version: |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
131 (function |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
132 (lambda (fn1 fn2) |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
133 (> (backup-extract-version fn1) |
945558e05127
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
134 (backup-extract-version fn2)))))))))) |
474 | 135 |
136 (defun diff-internal-diff (diff-command sw strip) | |
137 (let ((buffer-read-only nil)) | |
349 | 138 (with-output-to-temp-buffer "*Diff Output*" |
139 (buffer-disable-undo standard-output) | |
140 (save-excursion | |
141 (set-buffer standard-output) | |
142 (erase-buffer) | |
474 | 143 (apply 'call-process diff-command nil t nil sw))) |
349 | 144 (set-buffer "*Diff Output*") |
145 (goto-char (point-min)) | |
146 (while sw | |
147 (if (string= (car sw) "-c") | |
148 ;; strip leading filenames from context diffs | |
149 (progn (forward-line 2) (delete-region (point-min) (point)))) | |
474 | 150 (if (and (string= (car sw) "-C") (string= "sccs" diff-command)) |
151 ;; strip stuff from SCCS context diffs | |
152 (progn (forward-line 2) (delete-region (point-min) (point)))) | |
153 (setq sw (cdr sw))) | |
154 (if strip | |
155 ;; strip stuff from SCCS context diffs | |
156 (progn (forward-line strip) (delete-region (point-min) (point))))) | |
349 | 157 (diff-mode) |
158 (if (string= "0" diff-total-differences) | |
159 (let ((buffer-read-only nil)) | |
160 (insert (message "There are no differences."))) | |
161 (narrow-to-region (point) (progn | |
162 (forward-line 1) | |
163 (if (re-search-forward diff-search-pattern | |
164 nil t) | |
165 (goto-char (match-beginning 0)) | |
166 (goto-char (point-max))))) | |
167 (setq diff-current-difference "1"))) | |
168 | |
474 | 169 ;; Take a buffer full of Unix diff output and go into a mode to easily |
349 | 170 ;; see the next and previous difference |
171 (defun diff-mode () | |
172 "Diff Mode is used by \\[diff] for perusing the output from the diff program. | |
173 All normal editing commands are turned off. Instead, these are available: | |
174 \\<diff-mode-map> | |
175 \\[diff-beginning-of-diff] Move point to start of this difference. | |
176 \\[scroll-up] Scroll to next screen of this difference. | |
177 \\[scroll-down] Scroll to previous screen of this difference. | |
178 \\[diff-next-difference] Move to Next Difference. | |
179 \\[diff-previous-difference] Move to Previous Difference. | |
180 \\[diff-show-difference] Jump to difference specified by numeric position. | |
181 " | |
182 (interactive) | |
183 (use-local-map diff-mode-map) | |
184 (setq buffer-read-only t | |
185 major-mode 'diff-mode | |
186 mode-name "Diff" | |
187 mode-line-modified "--- " | |
188 mode-line-process | |
189 '(" " diff-current-difference "/" diff-total-differences)) | |
190 (make-local-variable 'diff-current-difference) | |
191 (set (make-local-variable 'diff-total-differences) | |
192 (int-to-string (diff-count-differences)))) | |
193 | |
194 (defun diff-next-difference (n) | |
474 | 195 "Go to the beginning of the next difference. |
196 Differences are delimited by `diff-search-pattern'." | |
349 | 197 (interactive "p") |
198 (if (< n 0) (diff-previous-difference (- n)) | |
199 (if (zerop n) () | |
200 (goto-char (point-min)) | |
201 (forward-line 1) ; to get past the match for the start of this diff | |
202 (widen) | |
203 (if (re-search-forward diff-search-pattern nil 'move n) | |
204 (let ((start (goto-char (match-beginning 0)))) | |
205 (forward-line 1) | |
206 (if (re-search-forward diff-search-pattern nil 'move) | |
207 (goto-char (match-beginning 0))) | |
208 (narrow-to-region start (point)) | |
209 (setq diff-current-difference | |
210 (int-to-string (+ n (string-to-int | |
211 diff-current-difference))))) | |
212 (re-search-backward diff-search-pattern nil) | |
213 (narrow-to-region (point) (point-max)) | |
214 (message "No following differences.") | |
215 (setq diff-current-difference diff-total-differences)) | |
216 (goto-char (point-min))))) | |
217 | |
218 (defun diff-previous-difference (n) | |
474 | 219 "Go the the beginning of the previous difference. |
220 Differences are delimited by `diff-search-pattern'." | |
349 | 221 (interactive "p") |
222 (if (< n 0) (diff-next-difference (- n)) | |
223 (if (zerop n) () | |
224 (goto-char (point-min)) | |
225 (widen) | |
226 (if (re-search-backward diff-search-pattern nil 'move n) | |
227 (setq diff-current-difference | |
228 (int-to-string (- (string-to-int diff-current-difference) n))) | |
229 (message "No previous differences.") | |
230 (setq diff-current-difference "1")) | |
231 (narrow-to-region (point) (progn | |
232 (forward-line 1) | |
233 (re-search-forward diff-search-pattern nil) | |
234 (goto-char (match-beginning 0)))) | |
235 (goto-char (point-min))))) | |
236 | |
237 (defun diff-show-difference (n) | |
474 | 238 "Show difference number N (prefix argument)." |
349 | 239 (interactive "p") |
240 (let ((cur (string-to-int diff-current-difference))) | |
241 (cond ((or (= n cur) | |
242 (zerop n) | |
243 (not (natnump n))) ; should signal an error perhaps. | |
244 ;; just redisplay. | |
245 (goto-char (point-min))) | |
246 ((< n cur) | |
247 (diff-previous-difference (- cur n))) | |
248 ((> n cur) | |
249 (diff-next-difference (- n cur)))))) | |
250 | |
251 (defun diff-beginning-of-diff () | |
252 "Go to beginning of current difference." | |
253 (interactive) | |
254 (goto-char (point-min))) | |
255 | |
256 ;; This function counts up the number of differences in the buffer. | |
257 (defun diff-count-differences () | |
258 "Count number of differences in the current buffer." | |
259 (message "Counting differences...") | |
260 (save-excursion | |
261 (save-restriction | |
262 (widen) | |
263 (goto-char (point-min)) | |
264 (let ((cnt 0)) | |
265 (while (re-search-forward diff-search-pattern nil t) | |
266 (setq cnt (1+ cnt))) | |
267 (message "Counting differences...done (%d)" cnt) | |
268 cnt)))) | |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
474
diff
changeset
|
269 |
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
474
diff
changeset
|
270 ;;; diff.el ends here |