comparison lisp/diff.el @ 474:c3bbd755b7da

*** empty log message ***
author Jim Blandy <jimb@redhat.com>
date Sat, 21 Dec 1991 09:14:03 +0000
parents feacf757c5b2
children 8a533acedb77
comparison
equal deleted inserted replaced
473:999d0b38694e 474:c3bbd755b7da
27 ;; "Function to return switches to pass to the `diff' utility, in \\[diff]. 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 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'. 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.") 30 ;; NOTE: This is not an ordinary hook; it may not be a list of functions.")
31 31
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 ;;
32 (defvar diff-switches nil 39 (defvar diff-switches nil
33 "*A list of switches to pass to the diff program.") 40 "*A list of switches to pass to the diff program.")
34 41
35 (defvar diff-search-pattern "^\\([0-9]\\|\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\)" 42 (defvar diff-search-pattern "^\\([0-9]\\|\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\)"
36 "Regular expression that delineates difference regions in diffs.") 43 "Regular expression that delineates difference regions in diffs.")
44
45 (defvar diff-rcs-extension ",v"
46 "*Extension to find RCS file, some systems do not use ,v")
37 47
38 ;; Initialize the keymap if it isn't already 48 ;; Initialize the keymap if it isn't already
39 (if (boundp 'diff-mode-map) 49 (if (boundp 'diff-mode-map)
40 nil 50 nil
41 (setq diff-mode-map (make-keymap)) 51 (setq diff-mode-map (make-keymap))
73 (read-file-name "Diff original file: " 83 (read-file-name "Diff original file: "
74 (file-name-directory newf) nil t))))))) 84 (file-name-directory newf) nil t)))))))
75 (message "Comparing files %s %s..." new old) 85 (message "Comparing files %s %s..." new old)
76 (setq new (expand-file-name new) 86 (setq new (expand-file-name new)
77 old (expand-file-name old)) 87 old (expand-file-name old))
78 (let ((buffer-read-only nil) 88 (diff-internal-diff "diff" (append diff-switches (list new old)) nil))
79 (sw diff-switches)) 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))
80 (with-output-to-temp-buffer "*Diff Output*" 141 (with-output-to-temp-buffer "*Diff Output*"
81 (buffer-disable-undo standard-output) 142 (buffer-disable-undo standard-output)
82 (save-excursion 143 (save-excursion
83 (set-buffer standard-output) 144 (set-buffer standard-output)
84 (erase-buffer) 145 (erase-buffer)
85 (apply 'call-process "diff" nil t nil 146 (apply 'call-process diff-command nil t nil sw)))
86 (append diff-switches (list old new)))))
87 (set-buffer "*Diff Output*") 147 (set-buffer "*Diff Output*")
88 (goto-char (point-min)) 148 (goto-char (point-min))
89 (while sw 149 (while sw
90 (if (string= (car sw) "-c") 150 (if (string= (car sw) "-c")
91 ;; strip leading filenames from context diffs 151 ;; strip leading filenames from context diffs
92 (progn (forward-line 2) (delete-region (point-min) (point)))) 152 (progn (forward-line 2) (delete-region (point-min) (point))))
93 (setq sw (cdr sw)))) 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)))))
94 (diff-mode) 160 (diff-mode)
95 (if (string= "0" diff-total-differences) 161 (if (string= "0" diff-total-differences)
96 (let ((buffer-read-only nil)) 162 (let ((buffer-read-only nil))
97 (insert (message "There are no differences."))) 163 (insert (message "There are no differences.")))
98 (narrow-to-region (point) (progn 164 (narrow-to-region (point) (progn
101 nil t) 167 nil t)
102 (goto-char (match-beginning 0)) 168 (goto-char (match-beginning 0))
103 (goto-char (point-max))))) 169 (goto-char (point-max)))))
104 (setq diff-current-difference "1"))) 170 (setq diff-current-difference "1")))
105 171
106 ;; Take a buffer full of Unix diff output and go into a mode to easily 172 ;; Take a buffer full of Unix diff output and go into a mode to easily
107 ;; see the next and previous difference 173 ;; see the next and previous difference
108 (defun diff-mode () 174 (defun diff-mode ()
109 "Diff Mode is used by \\[diff] for perusing the output from the diff program. 175 "Diff Mode is used by \\[diff] for perusing the output from the diff program.
110 All normal editing commands are turned off. Instead, these are available: 176 All normal editing commands are turned off. Instead, these are available:
111 \\<diff-mode-map> 177 \\<diff-mode-map>
127 (make-local-variable 'diff-current-difference) 193 (make-local-variable 'diff-current-difference)
128 (set (make-local-variable 'diff-total-differences) 194 (set (make-local-variable 'diff-total-differences)
129 (int-to-string (diff-count-differences)))) 195 (int-to-string (diff-count-differences))))
130 196
131 (defun diff-next-difference (n) 197 (defun diff-next-difference (n)
132 "In diff mode, go to the beginning of the next difference as delimited 198 "Go to the beginning of the next difference.
133 by `diff-search-pattern'." 199 Differences are delimited by `diff-search-pattern'."
134 (interactive "p") 200 (interactive "p")
135 (if (< n 0) (diff-previous-difference (- n)) 201 (if (< n 0) (diff-previous-difference (- n))
136 (if (zerop n) () 202 (if (zerop n) ()
137 (goto-char (point-min)) 203 (goto-char (point-min))
138 (forward-line 1) ; to get past the match for the start of this diff 204 (forward-line 1) ; to get past the match for the start of this diff
151 (message "No following differences.") 217 (message "No following differences.")
152 (setq diff-current-difference diff-total-differences)) 218 (setq diff-current-difference diff-total-differences))
153 (goto-char (point-min))))) 219 (goto-char (point-min)))))
154 220
155 (defun diff-previous-difference (n) 221 (defun diff-previous-difference (n)
156 "In diff mode, go the the beginning of the previous difference as delimited 222 "Go the the beginning of the previous difference.
157 by `diff-search-pattern'." 223 Differences are delimited by `diff-search-pattern'."
158 (interactive "p") 224 (interactive "p")
159 (if (< n 0) (diff-next-difference (- n)) 225 (if (< n 0) (diff-next-difference (- n))
160 (if (zerop n) () 226 (if (zerop n) ()
161 (goto-char (point-min)) 227 (goto-char (point-min))
162 (widen) 228 (widen)
170 (re-search-forward diff-search-pattern nil) 236 (re-search-forward diff-search-pattern nil)
171 (goto-char (match-beginning 0)))) 237 (goto-char (match-beginning 0))))
172 (goto-char (point-min))))) 238 (goto-char (point-min)))))
173 239
174 (defun diff-show-difference (n) 240 (defun diff-show-difference (n)
175 "Show difference number N (prefix arg)." 241 "Show difference number N (prefix argument)."
176 (interactive "p") 242 (interactive "p")
177 (let ((cur (string-to-int diff-current-difference))) 243 (let ((cur (string-to-int diff-current-difference)))
178 (cond ((or (= n cur) 244 (cond ((or (= n cur)
179 (zerop n) 245 (zerop n)
180 (not (natnump n))) ; should signal an error perhaps. 246 (not (natnump n))) ; should signal an error perhaps.