(undigestify-rmail-message): Better error messages.
[bpt/emacs.git] / lisp / ediff-diff.el
1 ;;; ediff-diff.el --- diff-related utilities
2 ;;; Copyright (C) 1994, 1995 Free Software Foundation, Inc.
3
4 ;; Author: Michael Kifer <kifer@cs.sunysb.edu>
5
6 ;; This file is part of GNU Emacs.
7
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
12
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to
20 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 (require 'ediff-init)
23
24
25 (defvar ediff-shell
26 (cond ((eq system-type 'emx) "cmd") ; OS/2
27 ((memq system-type '(vax-vms axp-vms)) "*dcl*") ; VMS
28 (t "sh")) ; UNIX
29 "*The shell used to run diff and patch. If user's .profile or
30 .cshrc files are set up correctly, any shell will do. However, some people
31 set $prompt or other things incorrectly, which leads to undesirable output
32 messages. These may cause Ediff to fail. In such a case, set ediff-shell
33 to a shell that you are not using or, better, fix your shell's startup file.")
34
35
36 (defvar ediff-diff-program "diff"
37 "*Program to use for generating the differential of the two files.")
38 (defvar ediff-diff-options ""
39 "*Options to pass to `ediff-diff-program'.
40 If diff\(1\) is used as `ediff-diff-program', then the most useful options are
41 `-w', to ignore space, and `-i', to ignore case of letters.
42 At present, the option `-c' is ignored, since Ediff doesn't understand this
43 type of output.")
44
45 (defvar ediff-custom-diff-program ediff-diff-program
46 "*Program to use for generating custom diff output for saving it in a file.
47 This output is not used by Ediff internally.")
48 (defvar ediff-custom-diff-options "-c"
49 "*Options to pass to `ediff-custom-diff-program'.")
50
51 ;;; Support for diff3
52
53 (defvar ediff-match-diff3-line "^====\\(.?\\)$"
54 "Pattern to match lines produced by diff3 that describe differences.")
55 (defvar ediff-diff3-program "diff3"
56 "*Program to be used for three-way comparison.
57 Must produce output compatible with Unix's diff3 program.")
58 (defvar ediff-diff3-options ""
59 "*Options to pass to `ediff-diff3-program'.")
60 (defvar ediff-diff3-ok-lines-regexp
61 "^\\([1-3]:\\|====\\| \\|.*Warning *:\\|.*No newline\\|.*missing newline\\)"
62 "*Regexp that matches normal output lines from `ediff-diff3-program'.
63 Lines that do not match are assumed to be error messages.")
64
65 ;; keeps the status of the current diff in 3-way jobs.
66 ;; the status can be =diff(A), =diff(B), or =diff(A+B)
67 (ediff-defvar-local ediff-diff-status "" "")
68
69 ;; Support for patch
70
71 (defvar ediff-patch-program "patch"
72 "*Name of the program that applies patches.")
73 (defvar ediff-patch-options ""
74 "*Options to pass to ediff-patch-program.")
75
76 ;; The buffer of the patch file.
77 (defvar ediff-patch-buf nil)
78 ;; The buffer where patch would display its diagnostics.
79 (defvar ediff-patch-diagnostics nil)
80
81
82 ;;; Fine differences
83
84 (ediff-defvar-local ediff-auto-refine (if (ediff-window-display-p) 'on 'nix)
85 "If `on', Ediff auto-highlights fine diffs for the current diff region.
86 If `off', auto-highlighting is not used. If `nix', no fine diffs are shown
87 at all, unless the user force-refines the region by hitting `*'.
88
89 This variable can be set either in .emacs or toggled interactively.
90 Use `setq-default' if setting it in .emacs")
91
92 (ediff-defvar-local ediff-ignore-similar-regions nil
93 "*If t, skip over difference regions that differ only in the white space and line breaks.
94 This variable can be set either in .emacs or toggled interactively.
95 Use `setq-default' if setting it in .emacs")
96
97 (ediff-defvar-local ediff-auto-refine-limit 700
98 "Auto-refine only those regions that are smaller than this number of bytes.")
99
100 ;;; General
101
102 (defvar ediff-diff-ok-lines-regexp
103 "^\\([0-9,]+[acd][0-9,]+$\\|[<>] \\|---\\|.*Warning *:\\|.*No newline\\|.*missing newline\\)"
104 "Regexp that matches normal output lines from `ediff-diff-program'.
105 This is mostly lifted from Emerge, except that Ediff also considers
106 warnings and `Missing newline'-type messages to be normal output.
107 Lines that do not match are assumed to be error messages.")
108
109 (defvar ediff-match-diff-line (let ((x "\\([0-9]+\\)\\(\\|,\\([0-9]+\\)\\)"))
110 (concat "^" x "\\([acd]\\)" x "$"))
111 "Pattern to match lines produced by diff that describe differences.")
112
113 (ediff-defvar-local ediff-setup-diff-regions-function nil
114 "value is a function symbol depending on the kind of job is to be done.
115 For 2-way jobs and for ediff-merge, it should be `ediff-setup-diff-regions'.
116 For jobs requiring diff3, it should be `ediff-setup-diff-regions3'.
117
118 The function should take three mandatory arguments, file-A, file-B, and
119 file-C. It may ignore file C for diff2 jobs. It should also take
120 one optional arguments, diff-number to refine.")
121
122
123
124 ;;; Functions
125
126 ;; Generate the difference vector and overlays for the two files
127 ;; With optional arg REG-TO-REFINE, refine this region.
128 ;; File-C argument is not used here. It is there just because
129 ;; ediff-setup-diff-regions is called via a funcall to
130 ;; ediff-setup-diff-regions-function, which can also have the value
131 ;; ediff-setup-diff-regions3, which takes 4 arguments.
132 (defun ediff-setup-diff-regions (file-A file-B file-C)
133 ;; force all minibuffers to display ediff's messages.
134 ;; when xemacs implements minibufferless frames, this won't be necessary
135 (if ediff-xemacs-p (setq synchronize-minibuffers t))
136
137 (or (ediff-buffer-live-p ediff-diff-buffer)
138 (setq ediff-diff-buffer
139 (get-buffer-create (ediff-unique-buffer-name "*ediff-diff" "*"))))
140 (or (ediff-buffer-live-p ediff-custom-diff-buffer)
141 (setq ediff-custom-diff-buffer
142 (get-buffer-create
143 (ediff-unique-buffer-name "*ediff-custom-diff" "*"))))
144
145 (ediff-eval-in-buffer ediff-diff-buffer
146 (erase-buffer)
147 ;; shell-command tends to display old shell command buffers even when it
148 ;; puts output in another buffer---probably an Emacs bug.
149 (ediff-kill-buffer-carefully "*Shell Command Output*")
150 (let ((shell-file-name ediff-shell))
151 (message "Computing differences ...") (sit-for 0)
152 (shell-command
153 (format "%s %s %s %s"
154 ediff-diff-program ediff-diff-options
155 (ediff-protect-metachars file-A)
156 (ediff-protect-metachars file-B))
157 t)
158 ))
159
160 ;; compute custom diffs
161 (let ((shell-file-name ediff-shell)
162 (recomp-diff-key
163 (string-to-char
164 (substitute-command-keys "\\[ediff-update-diffs]"))))
165 (if (or (= (buffer-size) 0) ; was the first time
166 (eq last-command-char recomp-diff-key)) ; was a recompute cmd
167 ;; do asynchronously & insert output in ediff-custom-diff-buffer
168 (ediff-shell-command
169 (format "%s %s %s %s"
170 ediff-custom-diff-program ediff-custom-diff-options
171 (ediff-protect-metachars file-A)
172 (ediff-protect-metachars file-B))
173 ediff-custom-diff-buffer)))
174
175 (ediff-prepare-error-list ediff-diff-ok-lines-regexp ediff-diff-buffer)
176 (message "Computing differences ... done")(sit-for 0)
177 (ediff-convert-diffs-to-overlays
178 (ediff-extract-diffs
179 ediff-diff-buffer ediff-word-mode ediff-narrow-bounds))
180 )
181
182 ;; If file-A/B/C is nil, do 2-way comparison with the non-nil buffers
183 ;; This function works for diff3 and diff2 jobs
184 (defun ediff-setup-fine-diff-regions (file-A file-B file-C reg-num)
185 (or (ediff-buffer-live-p ediff-fine-diff-buffer)
186 (setq ediff-fine-diff-buffer
187 (get-buffer-create
188 (ediff-unique-buffer-name "*ediff-fine-diff" "*"))))
189
190 (let (diff3-job diff-program diff-options ok-regexp diff-list)
191 (setq diff3-job ediff-3way-job
192 diff-program (if diff3-job ediff-diff3-program ediff-diff-program)
193 diff-options ""
194 ok-regexp (if diff3-job
195 ediff-diff3-ok-lines-regexp
196 ediff-diff-ok-lines-regexp))
197
198 (ediff-eval-in-buffer ediff-fine-diff-buffer
199 (erase-buffer)
200 ;; shell-command tends to display old shell command buffers even when
201 ;; it puts output in another buffer---probably an Emacs bug.
202 (ediff-kill-buffer-carefully "*Shell Command Output*")
203 (let ((shell-file-name ediff-shell))
204 (message "Refining difference region %d ..." (1+ reg-num))
205 (shell-command
206 (format "%s %s %s %s %s"
207 diff-program diff-options
208 (if file-A
209 (ediff-protect-metachars file-A)
210 (ediff-protect-metachars file-B))
211 (if file-B
212 (ediff-protect-metachars file-B)
213 (ediff-protect-metachars file-A))
214 (if diff3-job
215 (if file-C
216 (ediff-protect-metachars file-C)
217 (ediff-protect-metachars file-B))
218 "")
219 )
220 t)
221 ))
222
223 (ediff-prepare-error-list ok-regexp ediff-fine-diff-buffer)
224 (message "Refining difference region %d ... done" (1+ reg-num))
225
226 (setq diff-list
227 (if diff3-job
228 (ediff-extract-diffs3
229 ediff-fine-diff-buffer '3way-comparison 'word-mode)
230 (ediff-extract-diffs ediff-fine-diff-buffer 'word-mode)))
231 ;; fixup diff-list
232 (if diff3-job
233 (cond ((not file-A)
234 (mapcar (function (lambda (elt)
235 (aset elt 0 nil)
236 (aset elt 1 nil)))
237 (cdr diff-list)))
238 ((not file-B)
239 (mapcar (function (lambda (elt)
240 (aset elt 2 nil)
241 (aset elt 3 nil)))
242 (cdr diff-list)))
243 ((not file-C)
244 (mapcar (function (lambda (elt)
245 (aset elt 4 nil)
246 (aset elt 5 nil)))
247 (cdr diff-list)))
248 ))
249
250 (ediff-convert-fine-diffs-to-overlays diff-list reg-num)
251 ))
252
253
254 (defun ediff-prepare-error-list (ok-regexp diff-buff)
255 (ediff-eval-in-buffer ediff-error-buffer
256 (erase-buffer)
257 (insert-buffer diff-buff)
258 (delete-matching-lines ok-regexp)
259 (if (memq system-type '(vax-vms axp-vms))
260 (delete-matching-lines "^$")))
261 ;; If diff reports errors, show them then quit.
262 (if (/= 0 (ediff-eval-in-buffer ediff-error-buffer (buffer-size)))
263 (let ((ctl-buf ediff-control-buffer)
264 (error-buf ediff-error-buffer))
265 (ediff-skip-unsuitable-frames)
266 (switch-to-buffer error-buf)
267 (ediff-kill-buffer-carefully ctl-buf)
268 (error "Errors in diff output. Diff output is in %S" diff-buff))))
269
270 ;; BOUNDS specifies visibility bounds to use.
271 ;; WORD-MODE tells whether we are in the word-mode or not.
272 ;; If WORD-MODE, also construct vector of diffs using word numbers.
273 ;; Else, use point values.
274 ;; This function handles diff-2 jobs including the case of
275 ;; merging buffers and files without ancestor.
276 (defun ediff-extract-diffs (diff-buffer word-mode &optional bounds)
277 (let ((A-buffer ediff-buffer-A)
278 (B-buffer ediff-buffer-B)
279 (C-buffer ediff-buffer-C)
280 (a-prev 1) ; this is needed to set the first diff line correctly
281 (b-prev 1)
282 (c-prev 1)
283 diff-list shift-A shift-B
284 )
285
286 ;; diff list contains word numbers, unless changed later
287 (setq diff-list (cons (if word-mode 'words 'points)
288 diff-list))
289 ;; we don't use visibility bounds for buffer C when merging
290 (if bounds
291 (setq shift-A
292 (ediff-overlay-start
293 (ediff-get-value-according-to-buffer-type 'A bounds))
294 shift-B
295 (ediff-overlay-start
296 (ediff-get-value-according-to-buffer-type 'B bounds))))
297
298 ;; reset point in buffers A/B/C
299 (ediff-eval-in-buffer A-buffer
300 (goto-char (if shift-A shift-A (point-min))))
301 (ediff-eval-in-buffer B-buffer
302 (goto-char (if shift-B shift-B (point-min))))
303 (if (ediff-buffer-live-p C-buffer)
304 (ediff-eval-in-buffer C-buffer
305 (goto-char (point-min))))
306
307 (ediff-eval-in-buffer diff-buffer
308 (goto-char (point-min))
309 (while (re-search-forward ediff-match-diff-line nil t)
310 (let* ((a-begin (string-to-int (buffer-substring (match-beginning 1)
311 (match-end 1))))
312 (a-end (let ((b (match-beginning 3))
313 (e (match-end 3)))
314 (if b
315 (string-to-int (buffer-substring b e))
316 a-begin)))
317 (diff-type (buffer-substring (match-beginning 4) (match-end 4)))
318 (b-begin (string-to-int (buffer-substring (match-beginning 5)
319 (match-end 5))))
320 (b-end (let ((b (match-beginning 7))
321 (e (match-end 7)))
322 (if b
323 (string-to-int (buffer-substring b e))
324 b-begin)))
325 a-begin-pt a-end-pt b-begin-pt b-end-pt
326 c-begin c-end c-begin-pt c-end-pt)
327 ;; fix the beginning and end numbers, because diff is somewhat
328 ;; strange about how it numbers lines
329 (if (string-equal diff-type "a")
330 (setq b-end (1+ b-end)
331 a-begin (1+ a-begin)
332 a-end a-begin)
333 (if (string-equal diff-type "d")
334 (setq a-end (1+ a-end)
335 b-begin (1+ b-begin)
336 b-end b-begin)
337 ;; (string-equal diff-type "c")
338 (setq a-end (1+ a-end)
339 b-end (1+ b-end))))
340
341 (if (eq ediff-default-variant 'default-B)
342 (setq c-begin b-begin
343 c-end b-end)
344 (setq c-begin a-begin
345 c-end a-end))
346
347 ;; compute main diff vector
348 (if word-mode
349 ;; make diff-list contain word numbers
350 (setq diff-list
351 (nconc diff-list
352 (list
353 (if (ediff-buffer-live-p C-buffer)
354 (vector (- a-begin a-prev) (- a-end a-begin)
355 (- b-begin b-prev) (- b-end b-begin)
356 (- c-begin c-prev) (- c-end c-begin)
357 nil ; state of diff
358 nil ; state of merge
359 )
360 (vector (- a-begin a-prev) (- a-end a-begin)
361 (- b-begin b-prev) (- b-end b-begin)
362 nil nil ; dummy buf C
363 nil ; state of diff
364 nil ; state of merge
365 ))
366 ))
367 a-prev a-end
368 b-prev b-end
369 c-prev c-end)
370 ;; else convert lines to points
371 (ediff-eval-in-buffer A-buffer
372 (forward-line (- a-begin a-prev))
373 (setq a-begin-pt (point))
374 (forward-line (- a-end a-begin))
375 (setq a-end-pt (point)
376 a-prev a-end))
377 (ediff-eval-in-buffer B-buffer
378 (forward-line (- b-begin b-prev))
379 (setq b-begin-pt (point))
380 (forward-line (- b-end b-begin))
381 (setq b-end-pt (point)
382 b-prev b-end))
383 (if (ediff-buffer-live-p C-buffer)
384 (ediff-eval-in-buffer C-buffer
385 (forward-line (- c-begin c-prev))
386 (setq c-begin-pt (point))
387 (forward-line (- c-end c-begin))
388 (setq c-end-pt (point)
389 c-prev c-end)))
390 (setq diff-list
391 (nconc
392 diff-list
393 (list
394 (if (ediff-buffer-live-p C-buffer)
395 (vector
396 a-begin-pt a-end-pt b-begin-pt b-end-pt
397 c-begin-pt c-end-pt
398 ;; state of diff
399 ;; shows which buff is different from the other two
400 (if (eq ediff-default-variant 'default-B) 'A 'B)
401 ;; state of merge
402 ediff-default-variant
403 )
404 (vector a-begin-pt a-end-pt b-begin-pt b-end-pt
405 nil nil ; dummy buf C
406 nil nil ; dummy state of diff & merge
407 )))
408 )))
409
410 ))) ; end ediff-eval-in-buffer
411 diff-list
412 ))
413
414
415 (defun ediff-set-diff-overlays-in-one-buffer (buf-type diff-list)
416 (let* ((current-diff -1)
417 (buff (ediff-get-buffer buf-type))
418 ;; ediff-extract-diffs puts the type of diff-list as the first elt
419 ;; of this list. The type is either 'points or 'words
420 (diff-list-type (car diff-list))
421 (shift (ediff-overlay-start
422 (ediff-get-value-according-to-buffer-type
423 buf-type ediff-narrow-bounds)))
424 (limit (ediff-overlay-end
425 (ediff-get-value-according-to-buffer-type
426 buf-type ediff-narrow-bounds)))
427 diff-overlay-list list-element total-diffs
428 begin end pt-saved overlay state-of-diff state-of-merge)
429
430 (setq diff-list (cdr diff-list)) ; discard diff list type
431 (setq total-diffs (length diff-list))
432
433 ;; shift, if necessary
434 (ediff-eval-in-buffer buff (setq pt-saved shift))
435
436 (while diff-list
437 (setq current-diff (1+ current-diff)
438 list-element (car diff-list)
439 begin (aref list-element (cond ((eq buf-type 'A) 0)
440 ((eq buf-type 'B) 2)
441 (t 4))) ; buff C
442 end (aref list-element (cond ((eq buf-type 'A) 1)
443 ((eq buf-type 'B) 3)
444 (t 5))) ; buff C
445 state-of-diff (aref list-element 6)
446 state-of-merge (aref list-element 7))
447
448 (cond ((and (not (eq buf-type state-of-diff))
449 (memq state-of-diff '(A B C)))
450 (setq state-of-diff
451 (car (delq buf-type (delq state-of-diff (list 'A 'B 'C)))))
452 (setq state-of-diff (format "=diff(%S)" state-of-diff))
453 (if (and ediff-merge-job state-of-merge (eq buf-type 'C))
454 (setq state-of-merge (format "%S" state-of-merge)))
455 )
456 (t (setq state-of-diff nil
457 state-of-merge nil)))
458
459 ;; Put overlays at appropriate places in buffer
460 ;; convert word numbers to points, if necessary
461 (if (eq diff-list-type 'words)
462 (progn
463 (ediff-eval-in-buffer buff (goto-char pt-saved))
464 (setq begin (ediff-goto-word (1+ begin) buff)
465 end (ediff-goto-word end buff 'end))
466 (if (> end limit) (setq end limit))
467 (if (> begin end) (setq begin end))
468 (setq pt-saved (ediff-eval-in-buffer buff (point)))))
469 (setq overlay (ediff-make-bullet-proof-overlay begin end buff))
470
471 ;; Priorities of overlays should be equal in all ediff control
472 ;; panel buffers. Otherwise it won't work due to Emacs
473 ;; bug, as insert-in-front-hooks will be called
474 ;; only on behalf of the buffer with higher priority.
475 (ediff-overlay-put overlay 'priority ediff-shadow-overlay-priority)
476 (ediff-overlay-put overlay 'ediff-diff-num current-diff)
477 (ediff-overlay-put
478 overlay 'insert-in-front-hooks '(ediff-insert-in-front))
479 (if (ediff-window-display-p)
480 (ediff-overlay-put
481 overlay 'face (if (ediff-odd-p current-diff)
482 (intern
483 (format "ediff-odd-diff-face-%S-var" buf-type))
484 (intern
485 (format "ediff-even-diff-face-%S-var" buf-type)))))
486
487 (if (= 0 (mod current-diff 10))
488 (message "Buffer %S: Processing difference region %d of %d"
489 buf-type current-diff total-diffs))
490 ;; record all overlays for this difference
491 ;; the second elt, nill, is a place holder for the fine diff vector.
492 ;; the last nil is a place holder for no-fine-diffs flag.
493 (setq diff-overlay-list
494 (nconc
495 diff-overlay-list
496 (list (vector overlay nil nil state-of-diff state-of-merge)))
497 diff-list
498 (cdr diff-list))
499 ) ; while
500
501 (set (intern (format "ediff-difference-vector-%S" buf-type))
502 (apply 'vector diff-overlay-list))
503 ))
504
505 ;; `n' is the diff region to work on. Default is ediff-current-difference.
506 ;; if `flag' is 'noforce then make fine-diffs only if this region's fine
507 ;; diffs have not been computed before.
508 ;; if `flag' is 'skip then don't compute fine diffs for this region.
509 ;;
510 (defun ediff-make-fine-diffs (&optional n flag)
511 (or n (setq n ediff-current-difference))
512
513 (if (< ediff-number-of-differences 1)
514 (error "No differences found"))
515
516 (if ediff-word-mode
517 (setq flag 'skip
518 ediff-auto-refine 'nix))
519
520 (or (< n 0)
521 (>= n ediff-number-of-differences)
522 ;; n is within the range
523 (let ((tmp-buffer (get-buffer-create ediff-tmp-buffer))
524 (file-A ediff-temp-file-A)
525 (file-B ediff-temp-file-B)
526 (file-C ediff-temp-file-C)
527 (empty-A (ediff-empty-diff-region-p n 'A))
528 (empty-B (ediff-empty-diff-region-p n 'B))
529 (empty-C (ediff-empty-diff-region-p n 'C))
530 (whitespace-A (ediff-whitespace-diff-region-p n 'A))
531 (whitespace-B (ediff-whitespace-diff-region-p n 'B))
532 (whitespace-C (ediff-whitespace-diff-region-p n 'C))
533 cumulative-fine-diff-length)
534
535 (cond ((and (eq flag 'noforce) (ediff-get-fine-diff-vector n 'A))
536 ;; don't compute fine diffs if diff vector exists
537 (if (and (ediff-no-fine-diffs-p n) ediff-verbose-p)
538 (message "Only white-space differences in region %d"
539 (1+ n))))
540 ;; if one of the regions is empty (or 2 in 3way comparison
541 ;; then don't refine
542 ((> (length (delq nil (list empty-A empty-B empty-C))) 1)
543 (if (and (ediff-looks-like-combined-merge n)
544 ediff-merge-job)
545 (ediff-set-fine-overlays-in-one-buffer 'C nil n))
546 (if ediff-verbose-p
547 (if ediff-3way-comparison-job
548 (message
549 "Region %d is empty in all buffers but %S"
550 (1+ n)
551 (cond ((not empty-A) 'A)
552 ((not empty-B) 'B)
553 ((not empty-C) 'C)))
554 (message
555 "Region %d in buffer %S is empty"
556 (1+ n)
557 (cond (empty-A 'A)
558 (empty-B 'B)
559 (empty-C 'C)))
560 ))
561 ;; if all regions happen to be empty, say, as a result of
562 ;; deletions from non-empty regions, then mark as space only
563 (if (and empty-A empty-B empty-C)
564 (ediff-mark-diff-as-space-only n t)
565 (ediff-mark-diff-as-space-only n nil)))
566 ;; don't compute fine diffs for this region
567 ((eq flag 'skip)
568 (or (ediff-get-fine-diff-vector n 'A)
569 (memq ediff-auto-refine '(off nix))
570 (not ediff-verbose-p)
571 (message
572 "Region %d exceeds auto-refine limit. `%s' force-refines"
573 (1+ n)
574 (substitute-command-keys "\\[ediff-make-or-kill-fine-diffs]")
575 )))
576 (t
577 ;; delete old fine diffs
578 ;;(ediff-clear-fine-differences n)
579 ;; recompute fine diffs
580 (ediff-wordify
581 (ediff-get-diff-posn 'A 'beg n)
582 (ediff-get-diff-posn 'A 'end n)
583 ediff-buffer-A
584 tmp-buffer
585 ediff-control-buffer)
586 (ediff-eval-in-buffer tmp-buffer
587 (setq file-A (ediff-make-temp-file "fineDiffA" file-A)))
588
589 (ediff-wordify
590 (ediff-get-diff-posn 'B 'beg n)
591 (ediff-get-diff-posn 'B 'end n)
592 ediff-buffer-B
593 tmp-buffer
594 ediff-control-buffer)
595 (ediff-eval-in-buffer tmp-buffer
596 (setq file-B (ediff-make-temp-file "fineDiffB" file-B)))
597
598 (if ediff-3way-job
599 (progn
600 (ediff-wordify
601 (ediff-get-diff-posn 'C 'beg n)
602 (ediff-get-diff-posn 'C 'end n)
603 ediff-buffer-C
604 tmp-buffer
605 ediff-control-buffer)
606 (ediff-eval-in-buffer tmp-buffer
607 (setq file-C
608 (ediff-make-temp-file "fineDiffC" file-C)))))
609
610 ;; save temp file names.
611 (setq ediff-temp-file-A file-A
612 ediff-temp-file-B file-B
613 ediff-temp-file-C file-C)
614
615 ;; set the new vector of fine diffs, if none exists
616 (cond ((and ediff-3way-job whitespace-A)
617 (ediff-setup-fine-diff-regions nil file-B file-C n))
618 ((and ediff-3way-job whitespace-B)
619 (ediff-setup-fine-diff-regions file-A nil file-C n))
620 ((and ediff-3way-job
621 (or whitespace-C
622 (and ediff-merge-job
623 (ediff-looks-like-combined-merge n))))
624 (ediff-setup-fine-diff-regions file-A file-B nil n))
625 (t
626 (ediff-setup-fine-diff-regions file-A file-B file-C n)))
627
628 (setq cumulative-fine-diff-length
629 (+ (length (ediff-get-fine-diff-vector n 'A))
630 (length (ediff-get-fine-diff-vector n 'B))
631 (if file-C
632 (length
633 (ediff-get-fine-diff-vector n 'C))
634 0)))
635
636 (cond ((or
637 ;; all regions are white space
638 (and whitespace-A whitespace-B whitespace-C)
639 ;; none is white space and no fine diffs detected
640 (and (not whitespace-A)
641 (not whitespace-B)
642 (not (and ediff-3way-job whitespace-C))
643 (eq cumulative-fine-diff-length 0)))
644 (ediff-mark-diff-as-space-only n t)
645 (message
646 "Only white-space differences in region %d" (1+ n)))
647 ((eq cumulative-fine-diff-length 0)
648 (ediff-mark-diff-as-space-only n nil)
649 (message
650 "Only white-space differences in region %d %s"
651 (1+ n)
652 (cond (whitespace-A "in buffers B & C")
653 (whitespace-B "in buffers A & C")
654 (whitespace-C "in buffers A & B"))))
655 (t
656 (ediff-mark-diff-as-space-only n nil)))
657 )
658 ) ; end cond
659 (ediff-set-fine-diff-properties n)
660 )))
661
662 ;; Interface to ediff-make-fine-diffs. Checks for auto-refine limit, etc.
663 (defun ediff-install-fine-diff-if-necessary (n)
664 (cond ((eq ediff-auto-refine 'on)
665 (if (and
666 (> ediff-auto-refine-limit
667 (- (ediff-get-diff-posn 'A 'end n)
668 (ediff-get-diff-posn 'A 'beg n)))
669 (> ediff-auto-refine-limit
670 (- (ediff-get-diff-posn 'B 'end n)
671 (ediff-get-diff-posn 'B 'beg n))))
672 (ediff-make-fine-diffs n 'noforce)
673 (ediff-make-fine-diffs n 'skip)))
674
675 ;; highlight iff fine diffs already exist
676 ((eq ediff-auto-refine 'off)
677 (ediff-make-fine-diffs n 'skip))))
678
679
680 ;; if fine diff vector is not set for diff N, then do nothing
681 (defun ediff-set-fine-diff-properties (n &optional default)
682 (or (not (ediff-window-display-p))
683 (< n 0)
684 (>= n ediff-number-of-differences)
685 ;; in a window system, set faces and priorities of fine overlays
686 (progn
687 (ediff-set-fine-diff-properties-in-one-buffer 'A n default)
688 (ediff-set-fine-diff-properties-in-one-buffer 'B n default)
689 (if ediff-3way-job
690 (ediff-set-fine-diff-properties-in-one-buffer 'C n default)))))
691
692 (defun ediff-set-fine-diff-properties-in-one-buffer (buf-type
693 n &optional default)
694 (let ((fine-diff-vector (ediff-get-fine-diff-vector n buf-type))
695 (face (if default
696 'default
697 (face-name
698 (intern (format "ediff-fine-diff-face-%S" buf-type)))))
699 ;; (if (and (eq buf-type 'C)
700 ;; (ediff-looks-like-combined-merge n))
701 ;; 'ediff-combined-diff-face
702 ;; (face-name
703 ;; (intern (format "ediff-fine-diff-face-%S" buf-type))))))
704 (priority (if default
705 0
706 (1+ (or (ediff-overlay-get
707 (symbol-value
708 (intern
709 (format
710 "ediff-current-diff-overlay-%S" buf-type)))
711 'priority)
712 0)))))
713 (mapcar
714 (function (lambda (overl)
715 (ediff-overlay-put overl 'face face)
716 (ediff-overlay-put overl 'priority priority)))
717 fine-diff-vector)))
718
719 ;; This assumes buffer C and that the region looks like a combination of
720 ;; regions in buffer A and C.
721 (defun ediff-set-fine-overlays-for-combined-merge (diff-list reg-num)
722 (let (overlay1 overlay2 overlay3)
723 (setq overlay1 (ediff-make-bullet-proof-overlay (nth 0 diff-list)
724 (nth 1 diff-list)
725 ediff-buffer-C)
726 overlay2 (ediff-make-bullet-proof-overlay (nth 2 diff-list)
727 (nth 3 diff-list)
728 ediff-buffer-C)
729 overlay3 (ediff-make-bullet-proof-overlay (nth 4 diff-list)
730 (nth 5 diff-list)
731 ediff-buffer-C))
732 (ediff-set-fine-diff-vector reg-num 'C (vector overlay1 overlay2 overlay3))
733 ))
734
735
736 ;; Convert diff list to overlays for a given DIFF-REGION
737 ;; in buffer of type BUF-TYPE
738 (defun ediff-set-fine-overlays-in-one-buffer (buf-type diff-list region-num)
739 (let* ((current-diff -1)
740 (reg-start (ediff-get-diff-posn buf-type 'beg region-num))
741 (buff (ediff-get-buffer buf-type))
742 combined-merge-diff-list
743 diff-overlay-list list-element
744 begin end overlay)
745
746 (ediff-clear-fine-differences-in-one-buffer region-num buf-type)
747 (setq diff-list (cdr diff-list)) ; discard list type (words or points)
748 (ediff-eval-in-buffer buff (goto-char reg-start))
749
750 ;; if it is a combined merge then set overlays in buff C specially
751 (if (and ediff-merge-job (eq buf-type 'C)
752 (setq combined-merge-diff-list
753 (ediff-looks-like-combined-merge region-num)))
754 (ediff-set-fine-overlays-for-combined-merge
755 combined-merge-diff-list region-num)
756 ;; regular fine diff
757 (while diff-list
758 (setq current-diff (1+ current-diff)
759 list-element (car diff-list)
760 begin (aref list-element (cond ((eq buf-type 'A) 0)
761 ((eq buf-type 'B) 2)
762 (t 4))) ; buf C
763 end (aref list-element (cond ((eq buf-type 'A) 1)
764 ((eq buf-type 'B) 3)
765 (t 5)))) ; buf C
766 (if (not (or begin end))
767 () ; skip this diff
768 ;; Put overlays at appropriate places in buffers
769 ;; convert lines to points, if necessary
770 (setq begin (ediff-goto-word (1+ begin) buff)
771 end (ediff-goto-word end buff 'end))
772 (setq overlay (ediff-make-bullet-proof-overlay begin end buff))
773 ;; record all overlays for this difference region
774 (setq diff-overlay-list (nconc diff-overlay-list (list overlay))))
775
776 (setq diff-list (cdr diff-list))
777 ) ; while
778 ;; convert the list of difference information into a vector
779 ;; for fast access
780 (ediff-set-fine-diff-vector
781 region-num buf-type (apply 'vector diff-overlay-list))
782 )))
783
784
785 ;; Stolen from emerge.el
786 (defun ediff-get-diff3-group (file)
787 ;; This save-excursion allows ediff-get-diff3-group to be called for the
788 ;; various groups of lines (1, 2, 3) in any order, and for the lines to
789 ;; appear in any order. The reason this is necessary is that Gnu diff3
790 ;; can produce the groups in the order 1, 2, 3 or 1, 3, 2.
791 (save-excursion
792 (re-search-forward
793 (concat "^" file ":\\([0-9]+\\)\\(,\\([0-9]+\\)\\)?\\([ac]\\)$"))
794 (beginning-of-line 2)
795 ;; treatment depends on whether it is an "a" group or a "c" group
796 (if (string-equal (buffer-substring (match-beginning 4) (match-end 4)) "c")
797 ;; it is a "c" group
798 (if (match-beginning 2)
799 ;; it has two numbers
800 (list (string-to-int
801 (buffer-substring (match-beginning 1) (match-end 1)))
802 (1+ (string-to-int
803 (buffer-substring (match-beginning 3) (match-end 3)))))
804 ;; it has one number
805 (let ((x (string-to-int
806 (buffer-substring (match-beginning 1) (match-end 1)))))
807 (list x (1+ x))))
808 ;; it is an "a" group
809 (let ((x (1+ (string-to-int
810 (buffer-substring (match-beginning 1) (match-end 1))))))
811 (list x x)))))
812
813
814 ;; If WORD-MODE, construct vector of diffs using word numbers.
815 ;; Else, use point values.
816 ;; WORD-MODE also tells if we are in the word-mode or not.
817 ;; If THREE-WAY-COMP, then it is a 3-way comparison. Else, it is merging
818 ;; with ancestor, in which case buffer-C contents is identical to buffer-A
819 ;; contents (unless buffer-A is narrowed.
820 ;; BOUNDS specifies visibility bounds to use.
821 (defun ediff-extract-diffs3 (diff-buffer word-mode three-way-comp
822 &optional bounds)
823 (let ((A-buffer ediff-buffer-A)
824 (B-buffer ediff-buffer-B)
825 (C-buffer ediff-buffer-C)
826 (a-prev 1) ; needed to set the first diff line correctly
827 (b-prev 1)
828 (c-prev 1)
829 diff-list shift-A shift-B shift-C
830 )
831
832 ;; diff list contains word numbers or points, depending on word-mode
833 (setq diff-list (cons (if word-mode 'words 'points)
834 diff-list))
835 (if bounds
836 (setq shift-A
837 (ediff-overlay-start
838 (ediff-get-value-according-to-buffer-type 'A bounds))
839 shift-B
840 (ediff-overlay-start
841 (ediff-get-value-according-to-buffer-type 'B bounds))
842 shift-C
843 (if three-way-comp
844 (ediff-overlay-start
845 (ediff-get-value-according-to-buffer-type 'C bounds)))))
846
847 ;; reset point in buffers A, B, C
848 (ediff-eval-in-buffer A-buffer
849 (goto-char (if shift-A shift-A (point-min))))
850 (ediff-eval-in-buffer B-buffer
851 (goto-char (if shift-B shift-B (point-min))))
852 (ediff-eval-in-buffer C-buffer
853 (goto-char (if shift-C shift-C (point-min))))
854
855 (ediff-eval-in-buffer diff-buffer
856 (goto-char (point-min))
857 (while (re-search-forward ediff-match-diff3-line nil t)
858 ;; leave point after matched line
859 (beginning-of-line 2)
860 (let ((agreement (buffer-substring (match-beginning 1) (match-end 1))))
861 ;; if the A and B files are the same and not 3way-comparison,
862 ;; ignore the difference
863 (if (or three-way-comp (not (string-equal agreement "3")))
864 (let* ((a-begin (car (ediff-get-diff3-group "1")))
865 (a-end (nth 1 (ediff-get-diff3-group "1")))
866 (b-begin (car (ediff-get-diff3-group "2")))
867 (b-end (nth 1 (ediff-get-diff3-group "2")))
868 (state-of-merge
869 (cond ((string-equal agreement "1") 'prefer-A)
870 ((string-equal agreement "2") 'prefer-B)
871 (t ediff-default-variant)))
872 (state-of-diff-merge
873 (if (memq state-of-merge '(default-A prefer-A)) 'B 'A))
874 (state-of-diff-comparison
875 (cond ((string-equal agreement "1") 'A)
876 ((string-equal agreement "2") 'B)
877 ((string-equal agreement "3") 'C)))
878 c-begin c-end
879 a-begin-pt a-end-pt
880 b-begin-pt b-end-pt c-begin-pt c-end-pt)
881
882 (if three-way-comp
883 (setq c-begin (car (ediff-get-diff3-group "3"))
884 c-end (nth 1 (ediff-get-diff3-group "3")))
885 (if (eq ediff-default-variant 'default-B)
886 (setq c-begin b-begin
887 c-end b-end)
888 (setq c-begin a-begin
889 c-end a-end)))
890
891 ;; compute main diff vector
892 (if word-mode
893 ;; make diff-list contain word numbers
894 (setq diff-list
895 (nconc diff-list
896 (list (vector
897 (- a-begin a-prev) (- a-end a-begin)
898 (- b-begin b-prev) (- b-end b-begin)
899 (- c-begin c-prev) (- c-end c-begin)
900 nil nil))) ; state of diff & merge
901 a-prev a-end
902 b-prev b-end
903 c-prev c-end)
904 ;; else convert lines to points
905 (ediff-eval-in-buffer A-buffer
906 (forward-line (- a-begin a-prev))
907 (setq a-begin-pt (point))
908 (forward-line (- a-end a-begin))
909 (setq a-end-pt (point)
910 a-prev a-end))
911 (ediff-eval-in-buffer B-buffer
912 (forward-line (- b-begin b-prev))
913 (setq b-begin-pt (point))
914 (forward-line (- b-end b-begin))
915 (setq b-end-pt (point)
916 b-prev b-end))
917 (ediff-eval-in-buffer C-buffer
918 (forward-line (- c-begin c-prev))
919 (setq c-begin-pt (point))
920 (forward-line (- c-end c-begin))
921 (setq c-end-pt (point)
922 c-prev c-end))
923 (setq diff-list
924 (nconc
925 diff-list
926 ;; if comparing with ancestor, then there also is a
927 ;; state-of-difference marker
928 (if three-way-comp
929 (list (vector
930 a-begin-pt a-end-pt
931 b-begin-pt b-end-pt
932 c-begin-pt c-end-pt
933 state-of-diff-comparison
934 nil ; state of merge
935 ))
936 (list (vector a-begin-pt a-end-pt
937 b-begin-pt b-end-pt
938 c-begin-pt c-end-pt
939 state-of-diff-merge
940 state-of-merge
941 )))
942 )))
943 ))
944
945 ))) ; end ediff-eval-in-buffer
946 diff-list
947 ))
948
949 ;; Generate the difference vector and overlays for three files
950 ;; File-C is either the third file to compare (in case of 3-way comparison)
951 ;; or it is the ancestor file.
952 (defun ediff-setup-diff-regions3 (file-A file-B file-C)
953
954 ;; force all minibuffers to display ediff's messages.
955 ;; when xemacs implements minibufferless frames, this won't be necessary
956 (if ediff-xemacs-p (setq synchronize-minibuffers t))
957
958 (or (ediff-buffer-live-p ediff-diff-buffer)
959 (setq ediff-diff-buffer
960 (get-buffer-create (ediff-unique-buffer-name "*ediff-diff" "*"))))
961
962 (ediff-eval-in-buffer ediff-diff-buffer
963 (erase-buffer)
964 ;; shell-command tends to display old shell command buffers even when it
965 ;; puts output in another buffer---probably an Emacs bug.
966 (ediff-kill-buffer-carefully "*Shell Command Output*")
967 (let ((shell-file-name ediff-shell))
968 (message "Computing differences ...")(sit-for 0)
969 (shell-command
970 (format "%s %s %s %s %s"
971 ediff-diff3-program ediff-diff3-options
972 (ediff-protect-metachars file-A)
973 (ediff-protect-metachars file-B)
974 (ediff-protect-metachars file-C))
975 t)
976 ))
977
978 (ediff-prepare-error-list ediff-diff3-ok-lines-regexp ediff-diff-buffer)
979 (message "Computing differences ... done")(sit-for 0)
980 (ediff-convert-diffs-to-overlays
981 (ediff-extract-diffs3
982 ediff-diff-buffer
983 ediff-word-mode ediff-3way-comparison-job ediff-narrow-bounds)
984 ))
985
986
987 ;; Execute shell COMMAND asynchronously and insert output in BUFFER.
988 ;; BUFFER must be a buffer object, and must be alive.
989 ;; There should be NO ampersand at the end of the command.
990 ;; We introduce this command because Emacs shell-command doesn't let us
991 ;; insert output in current buffer asynchronously.
992 ;; Most of this command was stolen from Emacs shell-command."
993 (defun ediff-shell-command (command buffer)
994 (let ((data (match-data)))
995 (unwind-protect
996 (let ((directory default-directory)
997 proc)
998 ;; If will kill a process, query first.
999 (setq proc (get-buffer-process buffer))
1000 (if proc (kill-process proc))
1001 (save-excursion
1002 (set-buffer buffer)
1003 (erase-buffer)
1004 (setq default-directory directory)
1005 (setq proc (start-process "Shell" buffer
1006 shell-file-name "-c" command))
1007 (setq mode-line-process '(":%s"))
1008 (set-process-sentinel proc 'ediff-shell-command-sentinel)
1009 (set-process-filter proc 'ediff-shell-command-filter)
1010 ))
1011 (store-match-data data))))
1012
1013 ;; This is shell-command-filter from simple.el in FSF Emacs.
1014 ;; Copied here because XEmacs doesn't have it.
1015 (defun ediff-shell-command-filter (proc string)
1016 ;; Do save-excursion by hand so that we can leave point numerically unchanged
1017 ;; despite an insertion immediately after it.
1018 (let* ((obuf (current-buffer))
1019 (buffer (process-buffer proc))
1020 opoint
1021 (window (get-buffer-window buffer))
1022 (pos (window-start window)))
1023 (unwind-protect
1024 (progn
1025 (set-buffer buffer)
1026 (or (= (point) (point-max))
1027 (setq opoint (point)))
1028 (goto-char (point-max))
1029 (insert-before-markers string))
1030 ;; insert-before-markers moved this marker: set it back.
1031 (set-window-start window pos)
1032 ;; Finish our save-excursion.
1033 (if opoint
1034 (goto-char opoint))
1035 (set-buffer obuf))))
1036
1037 ;; like shell-command-sentinel but doesn't print an exit status message
1038 ;; we do this because diff always exits with status 1, if diffs are found
1039 ;; so shell-command-sentinel displays a confusing message to the user
1040 (defun ediff-shell-command-sentinel (process signal)
1041 (if (and (memq (process-status process) '(exit signal))
1042 (buffer-name (process-buffer process)))
1043 (progn
1044 (save-excursion
1045 (set-buffer (process-buffer process))
1046 (setq mode-line-process nil))
1047 (delete-process process))))
1048
1049
1050 ;;; Word functions used to refine the current diff
1051
1052 (defvar ediff-forward-word-function 'ediff-forward-word
1053 "*Function to call to move to the next word.
1054 Used for splitting difference regions into individual words.")
1055
1056 (defvar ediff-whitespace " \n\t\f"
1057 "*Characters constituting white space.
1058 These characters are ignored when differing regions are split into words.")
1059
1060 ;;(defvar ediff-word-1 "a-zA-Z---_`'.?!:"
1061 (defvar ediff-word-1 "a-zA-Z---_"
1062 "*Characters that constitute words of type 1.
1063 More precisely, [ediff-word-1] is a regexp that matches type 1 words.
1064 See `ediff-forward-word' for more details.")
1065
1066 (defvar ediff-word-2 "0-9.,"
1067 "*Characters that constitute words of type 2.
1068 More precisely, [ediff-word-2] is a regexp that matches type 2 words.
1069 See `ediff-forward-word' for more details.")
1070
1071 (defvar ediff-word-3 "`'?!:;\"{}[]()"
1072 "*Characters that constitute words of type 3.
1073 More precisely, [ediff-word-3] is a regexp that matches type 3 words.
1074 See `ediff-forward-word' for more details.")
1075
1076 (defvar ediff-word-4
1077 (concat "^" ediff-word-1 ediff-word-2 ediff-word-3 ediff-whitespace)
1078 "*Characters that constitute words of type 4.
1079 More precisely, [ediff-word-4] is a regexp that matches type 4 words.
1080 See `ediff-forward-word' for more details.")
1081
1082 ;; Split region along word boundaries. Each word will be on its own line.
1083 ;; Output to buffer out-buffer.
1084 (defun ediff-forward-word ()
1085 "Move point one word forward.
1086 There are four types of words, each of which consists entirely of
1087 characters in `ediff-word-1', `ediff-word-2', `ediff-word-3', or
1088 `ediff-word-4'. Words are recognized by passing these in turn as the
1089 argument to `skip-chars-forward'."
1090 (or (> (skip-chars-forward ediff-word-1) 0)
1091 (> (skip-chars-forward ediff-word-2) 0)
1092 (> (skip-chars-forward ediff-word-3) 0)
1093 (> (skip-chars-forward ediff-word-4) 0)
1094 ))
1095
1096 (defun ediff-wordify (beg end in-buffer out-buffer &optional control-buf)
1097 (let (sv-point string)
1098 (save-excursion
1099 (set-buffer in-buffer)
1100 (setq string (buffer-substring beg end))
1101
1102 (set-buffer out-buffer)
1103 (erase-buffer)
1104 (insert string)
1105 (goto-char (point-min))
1106 (skip-chars-forward ediff-whitespace)
1107 (delete-region (point-min) (point))
1108
1109 (while (not (eobp))
1110 ;; eval incontrol buf to let user create local versions for
1111 ;; different invocations
1112 (if control-buf
1113 (funcall
1114 (ediff-eval-in-buffer control-buf ediff-forward-word-function))
1115 (funcall ediff-forward-word-function))
1116 (setq sv-point (point))
1117 (skip-chars-forward ediff-whitespace)
1118 (delete-region sv-point (point))
1119 (insert "\n")))))
1120
1121 ;; copy string from BEG END from IN-BUF to OUT-BUF
1122 (defun ediff-copy-to-buffer (beg end in-buffer out-buffer)
1123 (let (string)
1124 (save-excursion
1125 (set-buffer in-buffer)
1126 (setq string (buffer-substring beg end))
1127
1128 (set-buffer out-buffer)
1129 (erase-buffer)
1130 (insert string)
1131 (goto-char (point-min)))))
1132
1133
1134 ;; goto word #n starting at current position in buffer `buf'
1135 ;; For ediff, a word is either a string of a-z,A-Z, incl `-' and `_';
1136 ;; or a string of other non-blanks. A blank is a \n\t\f
1137 ;; If `flag' is non-nil, goto the end of the n-th word.
1138 (defun ediff-goto-word (n buf &optional flag)
1139 ;; remember val ediff-forward-word-function has in ctl buf
1140 (let ((fwd-word-fun ediff-forward-word-function))
1141 (ediff-eval-in-buffer buf
1142 (skip-chars-forward ediff-whitespace)
1143 (while (> n 1)
1144 (funcall fwd-word-fun)
1145 (skip-chars-forward ediff-whitespace)
1146 (setq n (1- n)))
1147 (if (and flag (> n 0))
1148 (funcall fwd-word-fun))
1149 (point))))
1150
1151
1152 (provide 'ediff-diff)
1153
1154
1155 ;; ediff-diff.el ends here