aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/diff.el
diff options
context:
space:
mode:
authorStefan Monnier2004-02-11 06:06:24 +0000
committerStefan Monnier2004-02-11 06:06:24 +0000
commit540d066630c55ab957dc08aa7abbca7775c9bd7b (patch)
tree62fcf560450dce98ad9e83e0eb159da7f1cbb431 /lisp/diff.el
parent17c91d79003357f4fe180d39a0c328eff9862500 (diff)
downloademacs-540d066630c55ab957dc08aa7abbca7775c9bd7b.tar.gz
emacs-540d066630c55ab957dc08aa7abbca7775c9bd7b.zip
Don't use compile any more, use diff-mode instead.
(diff-regexp-alist, diff-old-file, diff-new-file) (diff-parse-differences, diff-process-setup): Remove. (diff-sentinel): New fun. (diff): Use it. Run the process ourselves. Use diff-mode for the rest of the processing.
Diffstat (limited to 'lisp/diff.el')
-rw-r--r--lisp/diff.el219
1 files changed, 49 insertions, 170 deletions
diff --git a/lisp/diff.el b/lisp/diff.el
index 01b9e590a07..231130db212 100644
--- a/lisp/diff.el
+++ b/lisp/diff.el
@@ -30,8 +30,6 @@
30 30
31;;; Code: 31;;; Code:
32 32
33(require 'compile)
34
35(defgroup diff nil 33(defgroup diff nil
36 "Comparing files with `diff'." 34 "Comparing files with `diff'."
37 :group 'tools) 35 :group 'tools)
@@ -48,143 +46,11 @@
48 :type 'string 46 :type 'string
49 :group 'diff) 47 :group 'diff)
50 48
51(defvar diff-regexp-alist
52 '(
53 ;; -u format: @@ -OLDSTART,OLDEND +NEWSTART,NEWEND @@
54 ("^@@ -\\([0-9]+\\),[0-9]+ \\+\\([0-9]+\\),[0-9]+ @@$" 1 2)
55
56 ;; -c format: *** OLDSTART,OLDEND ****
57 ("^\\*\\*\\* \\([0-9]+\\),[0-9]+ \\*\\*\\*\\*$" 1 nil)
58 ;; --- NEWSTART,NEWEND ----
59 ("^--- \\([0-9]+\\),[0-9]+ ----$" nil 1)
60
61 ;; plain diff format: OLDSTART[,OLDEND]{a,d,c}NEWSTART[,NEWEND]
62 ("^\\([0-9]+\\)\\(,[0-9]+\\)?[adc]\\([0-9]+\\)\\(,[0-9]+\\)?$" 1 3)
63
64 ;; -e (ed) format: OLDSTART[,OLDEND]{a,d,c}
65 ("^\\([0-9]+\\)\\(,[0-9]+\\)?[adc]$" 1)
66
67 ;; -f format: {a,d,c}OLDSTART[ OLDEND]
68 ;; -n format: {a,d,c}OLDSTART LINES-CHANGED
69 ("^[adc]\\([0-9]+\\)\\( [0-9]+\\)?$" 1)
70 )
71 "Alist of regular expressions to match difference sections in \\[diff] output.
72Each element has the form (REGEXP OLD-IDX NEW-IDX).
73Any text that REGEXP matches identifies one difference hunk
74or the header of a hunk.
75
76The OLD-IDX'th subexpression of REGEXP gives the line number
77in the old file, and NEW-IDX'th subexpression gives the line number
78in the new file. If OLD-IDX or NEW-IDX
79is nil, REGEXP matches only half a hunk.")
80
81(defvar diff-old-file nil
82 "This is the old file name in the comparison in this buffer.")
83(defvar diff-new-file nil
84 "This is the new file name in the comparison in this buffer.")
85(defvar diff-old-temp-file nil 49(defvar diff-old-temp-file nil
86 "This is the name of a temp file to be deleted after diff finishes.") 50 "This is the name of a temp file to be deleted after diff finishes.")
87(defvar diff-new-temp-file nil 51(defvar diff-new-temp-file nil
88 "This is the name of a temp file to be deleted after diff finishes.") 52 "This is the name of a temp file to be deleted after diff finishes.")
89 53
90;; See compilation-parse-errors-function (compile.el).
91(defun diff-parse-differences (limit-search find-at-least)
92 (setq compilation-error-list nil)
93 (message "Parsing differences...")
94
95 ;; Don't reparse diffs already seen at last parse.
96 (if compilation-parsing-end (goto-char compilation-parsing-end))
97
98 ;; Construct in REGEXP a regexp composed of all those in dired-regexp-alist.
99 (let ((regexp (mapconcat (lambda (elt)
100 (concat "\\(" (car elt) "\\)"))
101 diff-regexp-alist
102 "\\|"))
103 ;; (GROUP-IDX OLD-IDX NEW-IDX)
104 (groups (let ((subexpr 1))
105 (mapcar (lambda (elt)
106 (prog1
107 (cons subexpr
108 (mapcar (lambda (n)
109 (and n
110 (+ subexpr n)))
111 (cdr elt)))
112 (setq subexpr (+ subexpr 1
113 (count-regexp-groupings
114 (car elt))))))
115 diff-regexp-alist)))
116
117 (new-error
118 (function (lambda (file subexpr)
119 (setq compilation-error-list
120 (cons
121 (list (save-excursion
122 ;; Report location of message
123 ;; at beginning of line.
124 (goto-char
125 (match-beginning subexpr))
126 (beginning-of-line)
127 (point-marker))
128 ;; Report location of corresponding text.
129 (list file nil)
130 (string-to-int
131 (buffer-substring
132 (match-beginning subexpr)
133 (match-end subexpr)))
134 nil)
135 compilation-error-list)))))
136
137 (found-desired nil)
138 (num-loci-found 0)
139 g)
140
141 (while (and (not found-desired)
142 ;; We don't just pass LIMIT-SEARCH to re-search-forward
143 ;; because we want to find matches containing LIMIT-SEARCH
144 ;; but which extend past it.
145 (re-search-forward regexp nil t))
146
147 ;; Find which individual regexp matched.
148 (setq g groups)
149 (while (and g (null (match-beginning (car (car g)))))
150 (setq g (cdr g)))
151 (setq g (car g))
152
153 (if (nth 1 g) ;OLD-IDX
154 (funcall new-error diff-old-file (nth 1 g)))
155 (if (nth 2 g) ;NEW-IDX
156 (funcall new-error diff-new-file (nth 2 g)))
157
158 (setq num-loci-found (1+ num-loci-found))
159 (if (or (and find-at-least
160 (>= num-loci-found find-at-least))
161 (and limit-search (>= (point) limit-search)))
162 ;; We have found as many new loci as the user wants,
163 ;; or the user wanted a specific diff, and we're past it.
164 (setq found-desired t)))
165 (set-marker compilation-parsing-end
166 (if found-desired (point)
167 ;; Set to point-max, not point, so we don't perpetually
168 ;; parse the last bit of text when it isn't a diff header.
169 (point-max)))
170 (message "Parsing differences...done"))
171 (setq compilation-error-list (nreverse compilation-error-list)))
172
173(defun diff-process-setup ()
174 "Set up \`compilation-exit-message-function' for \`diff'."
175 ;; Avoid frightening people with "abnormally terminated"
176 ;; if diff finds differences.
177 (set (make-local-variable 'compilation-exit-message-function)
178 (lambda (status code msg)
179 (cond ((not (eq status 'exit))
180 (cons msg code))
181 ((zerop code)
182 '("finished (no differences)\n" . "no differences"))
183 ((= code 1)
184 '("finished\n" . "differences found"))
185 (t
186 (cons msg code))))))
187
188;; prompt if prefix arg present 54;; prompt if prefix arg present
189(defun diff-switches () 55(defun diff-switches ()
190 (if current-prefix-arg 56 (if current-prefix-arg
@@ -193,6 +59,17 @@ is nil, REGEXP matches only half a hunk.")
193 diff-switches 59 diff-switches
194 (mapconcat 'identity diff-switches " "))))) 60 (mapconcat 'identity diff-switches " ")))))
195 61
62(defun diff-sentinel (code)
63 "Code run when the diff process exits.
64CODE is the exit code of the process. It should be 0 iff no diffs were found."
65 (if diff-old-temp-file (delete-file diff-old-temp-file))
66 (if diff-new-temp-file (delete-file diff-new-temp-file))
67 (save-excursion
68 (goto-char (point-max))
69 (insert (format "\nDiff finished%s. %s\n"
70 (if (equal 0 code) " (no differences)" "")
71 (current-time-string)))))
72
196;;;###autoload 73;;;###autoload
197(defun diff (old new &optional switches no-async) 74(defun diff (old new &optional switches no-async)
198 "Find and display the differences between OLD and NEW files. 75 "Find and display the differences between OLD and NEW files.
@@ -223,43 +100,45 @@ With prefix arg, prompt for diff switches."
223 (or switches (setq switches diff-switches)) ; If not specified, use default. 100 (or switches (setq switches diff-switches)) ; If not specified, use default.
224 (let* ((old-alt (file-local-copy old)) 101 (let* ((old-alt (file-local-copy old))
225 (new-alt (file-local-copy new)) 102 (new-alt (file-local-copy new))
226 buf) 103 (command
104 (mapconcat 'identity
105 `(,diff-command
106 ;; Use explicitly specified switches
107 ,@(if (listp switches) switches (list switches))
108 ,@(if (or old-alt new-alt)
109 (list "-L" old "-L" new))
110 ,(shell-quote-argument (or old-alt old))
111 ,(shell-quote-argument (or new-alt new)))
112 " "))
113 (buf (get-buffer-create "*Diff*"))
114 proc)
227 (save-excursion 115 (save-excursion
228 (let ((compilation-process-setup-function 'diff-process-setup) 116 (display-buffer buf)
229 (command 117 (set-buffer buf)
230 (mapconcat 'identity 118 (setq buffer-read-only nil)
231 `(,diff-command 119 (buffer-disable-undo (current-buffer))
232 ;; Use explicitly specified switches 120 (erase-buffer)
233 ,@(if (listp switches) switches (list switches)) 121 (buffer-enable-undo (current-buffer))
234 ,@(if (or old-alt new-alt) 122 (diff-mode)
235 (list "-L" old "-L" new)) 123 (set (make-local-variable 'revert-buffer-function)
236 ,(shell-quote-argument (or old-alt old)) 124 `(lambda (ignore-auto noconfirm)
237 ,(shell-quote-argument (or new-alt new))) 125 (diff ',old ',new ',switches ',no-async)))
238 " "))) 126 (set (make-local-variable 'diff-old-temp-file) old-alt)
239 (setq buf 127 (set (make-local-variable 'diff-new-temp-file) new-alt)
240 (compile-internal command 128 (insert command "\n")
241 "No more differences" "Diff" 129 (if (and (not no-async) (fboundp 'start-process))
242 'diff-parse-differences 130 (progn
243 nil nil nil nil nil nil no-async)) 131 (setq proc (start-process "Diff" buf shell-file-name
244 (set-buffer buf) 132 shell-command-switch command))
245 (set (make-local-variable 'revert-buffer-function) 133 (set-process-sentinel
246 `(lambda (ignore-auto noconfirm) 134 proc (lambda (proc msg)
247 (diff ',old ',new ',switches ',no-async))) 135 (with-current-buffer (process-buffer proc)
248 (set (make-local-variable 'diff-old-file) old) 136 (diff-sentinel (process-exit-status proc))))))
249 (set (make-local-variable 'diff-new-file) new) 137 ;; Async processes aren't available.
250 (set (make-local-variable 'diff-old-temp-file) old-alt) 138 (diff-sentinel
251 (set (make-local-variable 'diff-new-temp-file) new-alt) 139 (call-process shell-file-name nil buf nil
252 (set (make-local-variable 'compilation-finish-function) 140 shell-command-switch command))))
253 (function (lambda (buff msg) 141 buf))
254 (if diff-old-temp-file
255 (delete-file diff-old-temp-file))
256 (if diff-new-temp-file
257 (delete-file diff-new-temp-file)))))
258 ;; When async processes aren't available, the compilation finish
259 ;; function doesn't get chance to run. Invoke it by hand.
260 (or (fboundp 'start-process)
261 (funcall compilation-finish-function nil nil))
262 buf))))
263 142
264;;;###autoload 143;;;###autoload
265(defun diff-backup (file &optional switches) 144(defun diff-backup (file &optional switches)