1 ;;; ediff-diff.el --- diff-related utilities
3 ;; Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
5 ;; Author: Michael Kifer <kifer@cs.sunysb.edu>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
30 (cond ((eq system-type 'emx) "cmd") ; OS/2
31 ((memq system-type '(ms-dos windows-nt windows-95))
32 shell-file-name) ; no standard name on MS-DOS
33 ((memq system-type '(vax-vms axp-vms)) "*dcl*") ; VMS
35 "*The shell used to run diff and patch. If user's .profile or
36 .cshrc files are set up correctly, any shell will do. However, some people
37 set $prompt or other things incorrectly, which leads to undesirable output
38 messages. These may cause Ediff to fail. In such a case, set ediff-shell
39 to a shell that you are not using or, better, fix your shell's startup file.")
42 (defvar ediff-diff-program "diff"
43 "*Program to use for generating the differential of the two files.")
44 (defvar ediff-diff-options ""
45 "*Options to pass to `ediff-diff-program'.
46 If diff\(1\) is used as `ediff-diff-program', then the most useful options are
47 `-w', to ignore space, and `-i', to ignore case of letters.
48 At present, the option `-c' is ignored, since Ediff doesn't understand this
51 (defvar ediff-custom-diff-program ediff-diff-program
52 "*Program to use for generating custom diff output for saving it in a file.
53 This output is not used by Ediff internally.")
54 (defvar ediff-custom-diff-options "-c"
55 "*Options to pass to `ediff-custom-diff-program'.")
59 (defvar ediff-match-diff3-line "^====\\(.?\\)$"
60 "Pattern to match lines produced by diff3 that describe differences.")
61 (defvar ediff-diff3-program "diff3"
62 "*Program to be used for three-way comparison.
63 Must produce output compatible with Unix's diff3 program.")
64 (defvar ediff-diff3-options ""
65 "*Options to pass to `ediff-diff3-program'.")
66 (defvar ediff-diff3-ok-lines-regexp
67 "^\\([1-3]:\\|====\\| \\|.*Warning *:\\|.*No newline\\|.*missing newline\\|^\C-m$\\)"
68 "*Regexp that matches normal output lines from `ediff-diff3-program'.
69 Lines that do not match are assumed to be error messages.")
71 ;; keeps the status of the current diff in 3-way jobs.
72 ;; the status can be =diff(A), =diff(B), or =diff(A+B)
73 (ediff-defvar-local ediff-diff-status "" "")
78 (ediff-defvar-local ediff-auto-refine (if (ediff-has-face-support-p) 'on 'nix)
79 "If `on', Ediff auto-highlights fine diffs for the current diff region.
80 If `off', auto-highlighting is not used. If `nix', no fine diffs are shown
81 at all, unless the user force-refines the region by hitting `*'.
83 This variable can be set either in .emacs or toggled interactively.
84 Use `setq-default' if setting it in .emacs")
86 (ediff-defvar-local ediff-ignore-similar-regions nil
87 "*If t, skip over difference regions that differ only in the white space and line breaks.
88 This variable can be set either in .emacs or toggled interactively.
89 Use `setq-default' if setting it in .emacs")
91 (ediff-defvar-local ediff-auto-refine-limit 1400
92 "*Auto-refine only the regions of this size \(in bytes\) or less.")
96 (defvar ediff-diff-ok-lines-regexp
97 "^\\([0-9,]+[acd][0-9,]+$\\|[<>] \\|---\\|.*Warning *:\\|.*No +newline\\|.*missing +newline\\|^\C-m$\\)"
98 "Regexp that matches normal output lines from `ediff-diff-program'.
99 This is mostly lifted from Emerge, except that Ediff also considers
100 warnings and `Missing newline'-type messages to be normal output.
101 Lines that do not match are assumed to be error messages.")
103 (defvar ediff-match-diff-line (let ((x "\\([0-9]+\\)\\(\\|,\\([0-9]+\\)\\)"))
104 (concat "^" x "\\([acd]\\)" x "$"))
105 "Pattern to match lines produced by diff that describe differences.")
107 (ediff-defvar-local ediff-setup-diff-regions-function nil
108 "value is a function symbol depending on the kind of job is to be done.
109 For 2-way jobs and for ediff-merge, it should be `ediff-setup-diff-regions'.
110 For jobs requiring diff3, it should be `ediff-setup-diff-regions3'.
112 The function should take three mandatory arguments, file-A, file-B, and
113 file-C. It may ignore file C for diff2 jobs. It should also take
114 one optional arguments, diff-number to refine.")
119 ;; Generate the difference vector and overlays for the two files
120 ;; With optional arg REG-TO-REFINE, refine this region.
121 ;; File-C argument is not used here. It is there just because
122 ;; ediff-setup-diff-regions is called via a funcall to
123 ;; ediff-setup-diff-regions-function, which can also have the value
124 ;; ediff-setup-diff-regions3, which takes 4 arguments.
125 (defun ediff-setup-diff-regions (file-A file-B file-C)
126 ;;; ;; Force all minibuffers to display ediff's messages.
127 ;;; ;; When xemacs implements minibufferless frames, this won't be necessary
128 ;;; (if ediff-xemacs-p (setq synchronize-minibuffers t))
130 ;; create, if it doesn't exist
131 (or (ediff-buffer-live-p ediff-diff-buffer)
132 (setq ediff-diff-buffer
133 (get-buffer-create (ediff-unique-buffer-name "*ediff-diff" "*"))))
134 (ediff-make-diff2-buffer ediff-diff-buffer file-A file-B)
135 (ediff-prepare-error-list ediff-diff-ok-lines-regexp ediff-diff-buffer)
136 (ediff-convert-diffs-to-overlays
138 ediff-diff-buffer ediff-word-mode ediff-narrow-bounds)))
140 ;; Run the diff program on FILE1 and FILE2 and put the output in DIFF-BUFFER
141 ;; Return the size of DIFF-BUFFER
142 (defun ediff-make-diff2-buffer (diff-buffer file1 file2)
143 (let ((file1-size (ediff-file-size file1))
144 (file2-size (ediff-file-size file2)))
145 (cond ((not (numberp file1-size))
146 (message "Can't find file: %s"
147 (ediff-abbreviate-file-name file1))
149 ;; 1 is an error exit code
151 ((not (numberp file2-size))
152 (message "Can't find file: %s"
153 (ediff-abbreviate-file-name file2))
155 ;; 1 is an error exit code
158 (message "Can't diff remote files: %s"
159 (ediff-abbreviate-file-name file1))
161 ;; 1 is an error exit code
164 (message "Can't diff remote file: %s"
165 (ediff-abbreviate-file-name file2))
168 ;; 1 is an error exit code
170 (t (message "Computing differences between %s and %s ..."
171 (file-name-nondirectory file1)
172 (file-name-nondirectory file2))
173 ;; this erases the diff buffer automatically
174 (ediff-exec-process ediff-diff-program
177 ediff-diff-options file1 file2)
178 ;;(message "Computing differences ... done")
180 (ediff-eval-in-buffer diff-buffer
185 ;; If file-A/B/C is nil, do 2-way comparison with the non-nil buffers
186 ;; This function works for diff3 and diff2 jobs
187 (defun ediff-setup-fine-diff-regions (file-A file-B file-C reg-num)
188 (or (ediff-buffer-live-p ediff-fine-diff-buffer)
189 (setq ediff-fine-diff-buffer
191 (ediff-unique-buffer-name "*ediff-fine-diff" "*"))))
193 (let (diff3-job diff-program diff-options ok-regexp diff-list)
194 (setq diff3-job ediff-3way-job
195 diff-program (if diff3-job ediff-diff3-program ediff-diff-program)
196 diff-options (if diff3-job ediff-diff3-options ediff-diff-options)
197 ok-regexp (if diff3-job
198 ediff-diff3-ok-lines-regexp
199 ediff-diff-ok-lines-regexp))
201 (ediff-message-if-verbose "Refining difference region %d ..." (1+ reg-num))
202 (ediff-exec-process diff-program ediff-fine-diff-buffer 'synchronize
204 ;; The shuffle below is because we can compare 3-way
205 ;; or in several 2-way fashions, like fA fC, fA fB,
207 (if file-A file-A file-B)
208 (if file-B file-B file-A)
210 (if file-C file-C file-B))
213 (ediff-prepare-error-list ok-regexp ediff-fine-diff-buffer)
214 (ediff-message-if-verbose
216 ;; "Refining difference region %d ... done" (1+ reg-num))
220 (ediff-extract-diffs3
221 ediff-fine-diff-buffer '3way-comparison 'word-mode)
222 (ediff-extract-diffs ediff-fine-diff-buffer 'word-mode)))
226 (mapcar (function (lambda (elt)
231 (mapcar (function (lambda (elt)
236 (mapcar (function (lambda (elt)
242 (ediff-convert-fine-diffs-to-overlays diff-list reg-num)
246 (defun ediff-prepare-error-list (ok-regexp diff-buff)
247 (or (ediff-buffer-live-p ediff-error-buffer)
248 (setq ediff-error-buffer
249 (get-buffer-create (ediff-unique-buffer-name
250 "*ediff-errors" "*"))))
251 (ediff-eval-in-buffer ediff-error-buffer
253 (insert (ediff-eval-in-buffer diff-buff (buffer-string)))
254 (goto-char (point-min))
255 (delete-matching-lines ok-regexp)
256 (if (memq system-type '(vax-vms axp-vms))
257 (delete-matching-lines "^$")))
258 ;; If diff reports errors, show them then quit.
259 (if (/= 0 (ediff-eval-in-buffer ediff-error-buffer (buffer-size)))
260 (let ((ctl-buf ediff-control-buffer)
261 (error-buf ediff-error-buffer))
262 (ediff-skip-unsuitable-frames)
263 (switch-to-buffer error-buf)
264 (ediff-kill-buffer-carefully ctl-buf)
265 (error "Errors in diff output. Diff output is in %S" diff-buff))))
267 ;; BOUNDS specifies visibility bounds to use.
268 ;; WORD-MODE tells whether we are in the word-mode or not.
269 ;; If WORD-MODE, also construct vector of diffs using word numbers.
270 ;; Else, use point values.
271 ;; This function handles diff-2 jobs including the case of
272 ;; merging buffers and files without ancestor.
273 (defun ediff-extract-diffs (diff-buffer word-mode &optional bounds)
274 (let ((A-buffer ediff-buffer-A)
275 (B-buffer ediff-buffer-B)
276 (C-buffer ediff-buffer-C)
277 (a-prev 1) ; this is needed to set the first diff line correctly
280 diff-list shift-A shift-B
283 ;; diff list contains word numbers, unless changed later
284 (setq diff-list (cons (if word-mode 'words 'points)
286 ;; we don't use visibility bounds for buffer C when merging
290 (ediff-get-value-according-to-buffer-type 'A bounds))
293 (ediff-get-value-according-to-buffer-type 'B bounds))))
295 ;; reset point in buffers A/B/C
296 (ediff-eval-in-buffer A-buffer
297 (goto-char (if shift-A shift-A (point-min))))
298 (ediff-eval-in-buffer B-buffer
299 (goto-char (if shift-B shift-B (point-min))))
300 (if (ediff-buffer-live-p C-buffer)
301 (ediff-eval-in-buffer C-buffer
302 (goto-char (point-min))))
304 (ediff-eval-in-buffer diff-buffer
305 (goto-char (point-min))
306 (while (re-search-forward ediff-match-diff-line nil t)
307 (let* ((a-begin (string-to-int (buffer-substring (match-beginning 1)
309 (a-end (let ((b (match-beginning 3))
312 (string-to-int (buffer-substring b e))
314 (diff-type (buffer-substring (match-beginning 4) (match-end 4)))
315 (b-begin (string-to-int (buffer-substring (match-beginning 5)
317 (b-end (let ((b (match-beginning 7))
320 (string-to-int (buffer-substring b e))
322 a-begin-pt a-end-pt b-begin-pt b-end-pt
323 c-begin c-end c-begin-pt c-end-pt)
324 ;; fix the beginning and end numbers, because diff is somewhat
325 ;; strange about how it numbers lines
326 (if (string-equal diff-type "a")
327 (setq b-end (1+ b-end)
330 (if (string-equal diff-type "d")
331 (setq a-end (1+ a-end)
334 ;; (string-equal diff-type "c")
335 (setq a-end (1+ a-end)
338 (if (eq ediff-default-variant 'default-B)
339 (setq c-begin b-begin
341 (setq c-begin a-begin
344 ;; compute main diff vector
346 ;; make diff-list contain word numbers
350 (if (ediff-buffer-live-p C-buffer)
351 (vector (- a-begin a-prev) (- a-end a-begin)
352 (- b-begin b-prev) (- b-end b-begin)
353 (- c-begin c-prev) (- c-end c-begin)
354 nil nil ; dummy ancestor
357 nil ; state of ancestor
359 (vector (- a-begin a-prev) (- a-end a-begin)
360 (- b-begin b-prev) (- b-end b-begin)
361 nil nil ; dummy buf C
362 nil nil ; dummy ancestor
365 nil ; state of ancestor
371 ;; else convert lines to points
372 (ediff-eval-in-buffer A-buffer
373 (forward-line (- a-begin a-prev))
374 (setq a-begin-pt (point))
375 (forward-line (- a-end a-begin))
376 (setq a-end-pt (point)
378 (ediff-eval-in-buffer B-buffer
379 (forward-line (- b-begin b-prev))
380 (setq b-begin-pt (point))
381 (forward-line (- b-end b-begin))
382 (setq b-end-pt (point)
384 (if (ediff-buffer-live-p C-buffer)
385 (ediff-eval-in-buffer C-buffer
386 (forward-line (- c-begin c-prev))
387 (setq c-begin-pt (point))
388 (forward-line (- c-end c-begin))
389 (setq c-end-pt (point)
395 (if (ediff-buffer-live-p C-buffer)
397 a-begin-pt a-end-pt b-begin-pt b-end-pt
399 nil nil ; dummy ancestor
401 ;; shows which buff is different from the other two
402 (if (eq ediff-default-variant 'default-B) 'A 'B)
403 ediff-default-variant ; state of merge
404 nil ; state of ancestor
406 (vector a-begin-pt a-end-pt
408 nil nil ; dummy buf C
409 nil nil ; dummy ancestor
410 nil nil ; dummy state of diff & merge
411 nil ; dummy state of ancestor
415 ))) ; end ediff-eval-in-buffer
420 (defun ediff-convert-diffs-to-overlays (diff-list)
421 (ediff-set-diff-overlays-in-one-buffer 'A diff-list)
422 (ediff-set-diff-overlays-in-one-buffer 'B diff-list)
424 (ediff-set-diff-overlays-in-one-buffer 'C diff-list))
425 (if ediff-merge-with-ancestor-job
426 (ediff-set-diff-overlays-in-one-buffer 'Ancestor diff-list))
427 ;; set up vector showing the status of merge regions
429 (setq ediff-state-of-merge
433 (let ((state-of-merge (aref elt 9))
434 (state-of-ancestor (aref elt 10)))
436 (if state-of-merge (format "%S" state-of-merge))
437 state-of-ancestor))))
438 ;; the first elt designates type of list
441 (message "Processing difference regions ... done"))
444 (defun ediff-set-diff-overlays-in-one-buffer (buf-type diff-list)
445 (let* ((current-diff -1)
446 (buff (ediff-get-buffer buf-type))
447 ;; ediff-extract-diffs puts the type of diff-list as the first elt
448 ;; of this list. The type is either 'points or 'words
449 (diff-list-type (car diff-list))
450 (shift (ediff-overlay-start
451 (ediff-get-value-according-to-buffer-type
452 buf-type ediff-narrow-bounds)))
453 (limit (ediff-overlay-end
454 (ediff-get-value-according-to-buffer-type
455 buf-type ediff-narrow-bounds)))
456 diff-overlay-list list-element total-diffs
457 begin end pt-saved overlay state-of-diff)
459 (setq diff-list (cdr diff-list)) ; discard diff list type
460 (setq total-diffs (length diff-list))
462 ;; shift, if necessary
463 (ediff-eval-in-buffer buff (setq pt-saved shift))
466 (setq current-diff (1+ current-diff)
467 list-element (car diff-list)
468 begin (aref list-element (cond ((eq buf-type 'A) 0)
472 end (aref list-element (cond ((eq buf-type 'A) 1)
476 state-of-diff (aref list-element 8)
479 (cond ((and (not (eq buf-type state-of-diff))
480 (not (eq buf-type 'Ancestor))
481 (memq state-of-diff '(A B C)))
483 (car (delq buf-type (delq state-of-diff (list 'A 'B 'C)))))
484 (setq state-of-diff (format "=diff(%S)" state-of-diff))
486 (t (setq state-of-diff nil)))
488 ;; Put overlays at appropriate places in buffer
489 ;; convert word numbers to points, if necessary
490 (if (eq diff-list-type 'words)
492 (ediff-eval-in-buffer buff (goto-char pt-saved))
493 (setq begin (ediff-goto-word (1+ begin) buff)
494 end (ediff-goto-word end buff 'end))
495 (if (> end limit) (setq end limit))
496 (if (> begin end) (setq begin end))
497 (setq pt-saved (ediff-eval-in-buffer buff (point)))))
498 (setq overlay (ediff-make-bullet-proof-overlay begin end buff))
500 (ediff-overlay-put overlay 'priority ediff-shadow-overlay-priority)
501 (ediff-overlay-put overlay 'ediff-diff-num current-diff)
502 (if (and (ediff-has-face-support-p)
503 ediff-use-faces ediff-highlight-all-diffs)
504 (ediff-set-overlay-face
505 overlay (ediff-background-face buf-type current-diff)))
507 (if (= 0 (mod current-diff 10))
508 (message "Buffer %S: Processing difference region %d of %d"
509 buf-type current-diff total-diffs))
510 ;; record all overlays for this difference
511 ;; the second elt, nil, is a place holder for the fine diff vector.
512 ;; the third elt, nil, is a place holder for no-fine-diffs flag.
513 (setq diff-overlay-list
516 (list (vector overlay nil nil state-of-diff)))
521 (set (intern (format "ediff-difference-vector-%S" buf-type))
522 (vconcat diff-overlay-list))
525 ;; `n' is the diff region to work on. Default is ediff-current-difference.
526 ;; if `flag' is 'noforce then make fine-diffs only if this region's fine
527 ;; diffs have not been computed before.
528 ;; if `flag' is 'skip then don't compute fine diffs for this region.
529 (defun ediff-make-fine-diffs (&optional n flag)
530 (or n (setq n ediff-current-difference))
532 (if (< ediff-number-of-differences 1)
533 (error ediff-NO-DIFFERENCES))
537 ediff-auto-refine 'nix))
540 (>= n ediff-number-of-differences)
541 ;; n is within the range
542 (let ((tmp-buffer (get-buffer-create ediff-tmp-buffer))
543 (file-A ediff-temp-file-A)
544 (file-B ediff-temp-file-B)
545 (file-C ediff-temp-file-C)
546 (empty-A (ediff-empty-diff-region-p n 'A))
547 (empty-B (ediff-empty-diff-region-p n 'B))
548 (empty-C (ediff-empty-diff-region-p n 'C))
549 (whitespace-A (ediff-whitespace-diff-region-p n 'A))
550 (whitespace-B (ediff-whitespace-diff-region-p n 'B))
551 (whitespace-C (ediff-whitespace-diff-region-p n 'C))
552 cumulative-fine-diff-length)
554 (cond ((and (eq flag 'noforce) (ediff-get-fine-diff-vector n 'A))
555 ;; don't compute fine diffs if diff vector exists
556 (if (ediff-no-fine-diffs-p n)
557 ;;(ediff-message-if-verbose
559 "Only white-space differences in region %d" (1+ n))))
560 ;; If one of the regions is empty (or 2 in 3way comparison)
561 ;; then don't refine.
562 ;; If the region happens to be entirely whitespace or empty then
564 ((> (length (delq nil (list empty-A empty-B empty-C))) 1)
565 (if (and (ediff-looks-like-combined-merge n)
567 (ediff-set-fine-overlays-in-one-buffer 'C nil n))
568 (if ediff-3way-comparison-job
569 (ediff-message-if-verbose
570 "Region %d is empty in all buffers but %S"
572 (cond ((not empty-A) 'A)
575 (ediff-message-if-verbose
576 "Region %d in buffer %S is empty"
582 ;; if all regions happen to be whitespace
583 (if (and whitespace-A whitespace-B whitespace-C)
584 ;; mark as space only
585 (ediff-mark-diff-as-space-only n t)
586 ;; if some regions are white and others don't, then mark as
587 ;; non-white-space-only
588 (ediff-mark-diff-as-space-only n nil)))
589 ;; don't compute fine diffs for this region
591 (or (ediff-get-fine-diff-vector n 'A)
592 (memq ediff-auto-refine '(off nix))
593 (ediff-message-if-verbose
594 "Region %d exceeds auto-refine limit. Type `%s' to refine"
596 (substitute-command-keys
597 "\\[ediff-make-or-kill-fine-diffs]")
600 ;; recompute fine diffs
602 (ediff-get-diff-posn 'A 'beg n)
603 (ediff-get-diff-posn 'A 'end n)
606 ediff-control-buffer)
608 (ediff-make-temp-file tmp-buffer "fineDiffA" file-A))
611 (ediff-get-diff-posn 'B 'beg n)
612 (ediff-get-diff-posn 'B 'end n)
615 ediff-control-buffer)
617 (ediff-make-temp-file tmp-buffer "fineDiffB" file-B))
622 (ediff-get-diff-posn 'C 'beg n)
623 (ediff-get-diff-posn 'C 'end n)
626 ediff-control-buffer)
628 (ediff-make-temp-file
629 tmp-buffer "fineDiffC" file-C))))
631 ;; save temp file names.
632 (setq ediff-temp-file-A file-A
633 ediff-temp-file-B file-B
634 ediff-temp-file-C file-C)
636 ;; set the new vector of fine diffs, if none exists
637 (cond ((and ediff-3way-job whitespace-A)
638 (ediff-setup-fine-diff-regions nil file-B file-C n))
639 ((and ediff-3way-job whitespace-B)
640 (ediff-setup-fine-diff-regions file-A nil file-C n))
642 ;; In merge-jobs, whitespace-C is t, since
643 ;; ediff-empty-diff-region-p returns t in this case
645 (ediff-setup-fine-diff-regions file-A file-B nil n))
647 (ediff-setup-fine-diff-regions file-A file-B file-C n)))
649 (setq cumulative-fine-diff-length
650 (+ (length (ediff-get-fine-diff-vector n 'A))
651 (length (ediff-get-fine-diff-vector n 'B))
652 ;; in merge jobs, the merge buffer is never refined
653 (if (and file-C (not ediff-merge-job))
654 (length (ediff-get-fine-diff-vector n 'C))
658 ;; all regions are white space
659 (and whitespace-A whitespace-B whitespace-C)
660 ;; none is white space and no fine diffs detected
661 (and (not whitespace-A)
663 (not (and ediff-3way-job whitespace-C))
664 (eq cumulative-fine-diff-length 0)))
665 (ediff-mark-diff-as-space-only n t)
666 (ediff-message-if-verbose
667 "Only white-space differences in region %d" (1+ n)))
668 ((eq cumulative-fine-diff-length 0)
669 (ediff-mark-diff-as-space-only n t)
670 (ediff-message-if-verbose
671 "Only white-space differences in region %d %s"
673 (cond (whitespace-A "in buffers B & C")
674 (whitespace-B "in buffers A & C")
675 (whitespace-C "in buffers A & B"))))
677 (ediff-mark-diff-as-space-only n nil)))
680 (ediff-set-fine-diff-properties n)
683 ;; Interface to ediff-make-fine-diffs. Checks for auto-refine limit, etc.
684 (defun ediff-install-fine-diff-if-necessary (n)
685 (cond ((eq ediff-auto-refine 'on)
687 (> ediff-auto-refine-limit
688 (- (ediff-get-diff-posn 'A 'end n)
689 (ediff-get-diff-posn 'A 'beg n)))
690 (> ediff-auto-refine-limit
691 (- (ediff-get-diff-posn 'B 'end n)
692 (ediff-get-diff-posn 'B 'beg n))))
693 (ediff-make-fine-diffs n 'noforce)
694 (ediff-make-fine-diffs n 'skip)))
696 ;; highlight iff fine diffs already exist
697 ((eq ediff-auto-refine 'off)
698 (ediff-make-fine-diffs n 'skip))))
701 ;; if fine diff vector is not set for diff N, then do nothing
702 (defun ediff-set-fine-diff-properties (n &optional default)
703 (or (not (ediff-has-face-support-p))
705 (>= n ediff-number-of-differences)
706 ;; when faces are supported, set faces and priorities of fine overlays
708 (ediff-set-fine-diff-properties-in-one-buffer 'A n default)
709 (ediff-set-fine-diff-properties-in-one-buffer 'B n default)
711 (ediff-set-fine-diff-properties-in-one-buffer 'C n default)))))
713 (defun ediff-set-fine-diff-properties-in-one-buffer (buf-type
715 (let ((fine-diff-vector (ediff-get-fine-diff-vector n buf-type))
719 (intern (format "ediff-fine-diff-face-%S" buf-type)))))
720 (priority (if default
722 (1+ (or (ediff-overlay-get
726 "ediff-current-diff-overlay-%S" buf-type)))
730 (function (lambda (overl)
731 (ediff-set-overlay-face overl face)
732 (ediff-overlay-put overl 'priority priority)))
735 ;; This assumes buffer C and that the region looks like a combination of
736 ;; regions in buffer A and C.
737 (defun ediff-set-fine-overlays-for-combined-merge (diff-list reg-num)
738 (let (overlay1 overlay2 overlay3)
739 (setq overlay1 (ediff-make-bullet-proof-overlay (nth 0 diff-list)
742 overlay2 (ediff-make-bullet-proof-overlay (nth 2 diff-list)
745 overlay3 (ediff-make-bullet-proof-overlay (nth 4 diff-list)
748 (ediff-set-fine-diff-vector reg-num 'C (vector overlay1 overlay2 overlay3))
752 ;; Convert diff list to overlays for a given DIFF-REGION
753 ;; in buffer of type BUF-TYPE
754 (defun ediff-set-fine-overlays-in-one-buffer (buf-type diff-list region-num)
755 (let* ((current-diff -1)
756 (reg-start (ediff-get-diff-posn buf-type 'beg region-num))
757 (buff (ediff-get-buffer buf-type))
758 combined-merge-diff-list
759 diff-overlay-list list-element
762 (ediff-clear-fine-differences-in-one-buffer region-num buf-type)
763 (setq diff-list (cdr diff-list)) ; discard list type (words or points)
764 (ediff-eval-in-buffer buff (goto-char reg-start))
766 ;; if it is a combined merge then set overlays in buff C specially
767 (if (and ediff-merge-job (eq buf-type 'C)
768 (setq combined-merge-diff-list
769 (ediff-looks-like-combined-merge region-num)))
770 (ediff-set-fine-overlays-for-combined-merge
771 combined-merge-diff-list region-num)
774 (setq current-diff (1+ current-diff)
775 list-element (car diff-list)
776 begin (aref list-element (cond ((eq buf-type 'A) 0)
779 end (aref list-element (cond ((eq buf-type 'A) 1)
782 (if (not (or begin end))
784 ;; Put overlays at appropriate places in buffers
785 ;; convert lines to points, if necessary
786 (setq begin (ediff-goto-word (1+ begin) buff)
787 end (ediff-goto-word end buff 'end))
788 (setq overlay (ediff-make-bullet-proof-overlay begin end buff))
789 ;; record all overlays for this difference region
790 (setq diff-overlay-list (nconc diff-overlay-list (list overlay))))
792 (setq diff-list (cdr diff-list))
794 ;; convert the list of difference information into a vector
796 (ediff-set-fine-diff-vector
797 region-num buf-type (vconcat diff-overlay-list))
801 ;; Stolen from emerge.el
802 (defun ediff-get-diff3-group (file)
803 ;; This save-excursion allows ediff-get-diff3-group to be called for the
804 ;; various groups of lines (1, 2, 3) in any order, and for the lines to
805 ;; appear in any order. The reason this is necessary is that Gnu diff3
806 ;; can produce the groups in the order 1, 2, 3 or 1, 3, 2.
809 (concat "^" file ":\\([0-9]+\\)\\(,\\([0-9]+\\)\\)?\\([ac]\\)$"))
810 (beginning-of-line 2)
811 ;; treatment depends on whether it is an "a" group or a "c" group
812 (if (string-equal (buffer-substring (match-beginning 4) (match-end 4)) "c")
814 (if (match-beginning 2)
815 ;; it has two numbers
817 (buffer-substring (match-beginning 1) (match-end 1)))
819 (buffer-substring (match-beginning 3) (match-end 3)))))
821 (let ((x (string-to-int
822 (buffer-substring (match-beginning 1) (match-end 1)))))
824 ;; it is an "a" group
825 (let ((x (1+ (string-to-int
826 (buffer-substring (match-beginning 1) (match-end 1))))))
830 ;; If WORD-MODE, construct vector of diffs using word numbers.
831 ;; Else, use point values.
832 ;; WORD-MODE also tells if we are in the word-mode or not.
833 ;; If THREE-WAY-COMP, then it is a 3-way comparison. Else, it is merging
834 ;; with ancestor, in which case buffer-C contents is identical to buffer-A/B,
835 ;; contents (unless buffer-A is narrowed) depending on ediff-default-variant's
837 ;; BOUNDS specifies visibility bounds to use.
838 (defun ediff-extract-diffs3 (diff-buffer word-mode three-way-comp
840 (let ((A-buffer ediff-buffer-A)
841 (B-buffer ediff-buffer-B)
842 (C-buffer ediff-buffer-C)
843 (anc-buffer ediff-ancestor-buffer)
844 (a-prev 1) ; needed to set the first diff line correctly
848 diff-list shift-A shift-B shift-C
851 ;; diff list contains word numbers or points, depending on word-mode
852 (setq diff-list (cons (if word-mode 'words 'points)
857 (ediff-get-value-according-to-buffer-type 'A bounds))
860 (ediff-get-value-according-to-buffer-type 'B bounds))
864 (ediff-get-value-according-to-buffer-type 'C bounds)))))
866 ;; reset point in buffers A, B, C
867 (ediff-eval-in-buffer A-buffer
868 (goto-char (if shift-A shift-A (point-min))))
869 (ediff-eval-in-buffer B-buffer
870 (goto-char (if shift-B shift-B (point-min))))
872 (ediff-eval-in-buffer C-buffer
873 (goto-char (if shift-C shift-C (point-min)))))
874 (if (ediff-buffer-live-p anc-buffer)
875 (ediff-eval-in-buffer anc-buffer
876 (goto-char (point-min))))
878 (ediff-eval-in-buffer diff-buffer
879 (goto-char (point-min))
880 (while (re-search-forward ediff-match-diff3-line nil t)
881 ;; leave point after matched line
882 (beginning-of-line 2)
883 (let ((agreement (buffer-substring (match-beginning 1) (match-end 1))))
884 ;; if the files A and B are the same and not 3way-comparison,
885 ;; ignore the difference
886 (if (or three-way-comp (not (string-equal agreement "3")))
887 (let* ((a-begin (car (ediff-get-diff3-group "1")))
888 (a-end (nth 1 (ediff-get-diff3-group "1")))
889 (b-begin (car (ediff-get-diff3-group "2")))
890 (b-end (nth 1 (ediff-get-diff3-group "2")))
891 (c-or-anc-begin (car (ediff-get-diff3-group "3")))
892 (c-or-anc-end (nth 1 (ediff-get-diff3-group "3")))
894 (cond ((string-equal agreement "1") 'prefer-A)
895 ((string-equal agreement "2") 'prefer-B)
896 (t ediff-default-variant)))
898 (if (memq state-of-merge '(default-A prefer-A)) 'B 'A))
899 (state-of-diff-comparison
900 (cond ((string-equal agreement "1") 'A)
901 ((string-equal agreement "2") 'B)
902 ((string-equal agreement "3") 'C)))
908 anc-begin-pt anc-end-pt)
910 (setq state-of-ancestor
911 (= c-or-anc-begin c-or-anc-end))
913 (cond (three-way-comp
914 (setq c-begin c-or-anc-begin
916 ((eq ediff-default-variant 'default-B)
917 (setq c-begin b-begin
920 (setq c-begin a-begin
923 ;; compute main diff vector
925 ;; make diff-list contain word numbers
929 (- a-begin a-prev) (- a-end a-begin)
930 (- b-begin b-prev) (- b-end b-begin)
931 (- c-begin c-prev) (- c-end c-begin)
932 nil nil ; dummy ancestor
935 nil ; state of ancestor
940 ;; else convert lines to points
941 (ediff-eval-in-buffer A-buffer
942 (forward-line (- a-begin a-prev))
943 (setq a-begin-pt (point))
944 (forward-line (- a-end a-begin))
945 (setq a-end-pt (point)
947 (ediff-eval-in-buffer B-buffer
948 (forward-line (- b-begin b-prev))
949 (setq b-begin-pt (point))
950 (forward-line (- b-end b-begin))
951 (setq b-end-pt (point)
953 (ediff-eval-in-buffer C-buffer
954 (forward-line (- c-begin c-prev))
955 (setq c-begin-pt (point))
956 (forward-line (- c-end c-begin))
957 (setq c-end-pt (point)
959 (if (ediff-buffer-live-p anc-buffer)
960 (ediff-eval-in-buffer anc-buffer
961 (forward-line (- c-or-anc-begin anc-prev))
962 (setq anc-begin-pt (point))
963 (forward-line (- c-or-anc-end c-or-anc-begin))
964 (setq anc-end-pt (point)
965 anc-prev c-or-anc-end)))
969 ;; if comparing with ancestor, then there also is a
970 ;; state-of-difference marker
976 nil nil ; ancestor begin/end
977 state-of-diff-comparison
979 nil ; state of ancestor
981 (list (vector a-begin-pt a-end-pt
984 anc-begin-pt anc-end-pt
992 ))) ; end ediff-eval-in-buffer
996 ;; Generate the difference vector and overlays for three files
997 ;; File-C is either the third file to compare (in case of 3-way comparison)
998 ;; or it is the ancestor file.
999 (defun ediff-setup-diff-regions3 (file-A file-B file-C)
1001 ;;; ;; force all minibuffers to display ediff's messages.
1002 ;;; ;; when xemacs implements minibufferless frames, this won't be necessary
1003 ;;; (if ediff-xemacs-p (setq synchronize-minibuffers t))
1005 (or (ediff-buffer-live-p ediff-diff-buffer)
1006 (setq ediff-diff-buffer
1007 (get-buffer-create (ediff-unique-buffer-name "*ediff-diff" "*"))))
1009 (message "Computing differences ...")
1010 (ediff-exec-process ediff-diff3-program ediff-diff-buffer 'synchronize
1011 ediff-diff3-options file-A file-B file-C)
1013 (ediff-prepare-error-list ediff-diff3-ok-lines-regexp ediff-diff-buffer)
1014 ;;(message "Computing differences ... done")
1015 (ediff-convert-diffs-to-overlays
1016 (ediff-extract-diffs3
1018 ediff-word-mode ediff-3way-comparison-job ediff-narrow-bounds)
1022 ;; Execute PROGRAM asynchronously, unless OS/2, Windows-*, or DOS, or unless
1023 ;; SYNCH is non-nil. BUFFER must be a buffer object, and must be alive. All
1024 ;; arguments in ARGS must be strings. The first arg may be a blank string, in
1025 ;; which case we delete it from ARGS list. We also delete nil from args.
1026 (defun ediff-exec-process (program buffer synch &rest args)
1027 (let ((data (match-data)))
1028 (if (string-match "^[ \t]*$" (car args)) ; delete blank string
1029 (setq args (cdr args)))
1030 (setq args (delq nil args)) ; delete nil from arguments
1031 (setq args (ediff-split-string (mapconcat 'identity args " ")))
1033 (let ((directory default-directory)
1038 (setq default-directory directory)
1039 (if (or (memq system-type '(emx ms-dos windows-nt windows-95))
1041 ;; In OS/2 (emx) do it synchronously, since OS/2 doesn't let us
1042 ;; delete files used by other processes. Thus, in ediff-buffers
1043 ;; and similar functions, we can't delete temp files because
1044 ;; they might be used by the asynch process that computes
1045 ;; custom diffs. So, we have to wait till custom diff
1046 ;; subprocess is done.
1047 ;; Similarly for Windows-*
1048 ;; In DOS, must synchronize because DOS doesn't have
1049 ;; asynchronous processes.
1050 (apply 'call-process program nil buffer nil args)
1051 ;; On other systems, do it asynchronously.
1052 (setq proc (get-buffer-process buffer))
1053 (if proc (kill-process proc))
1055 (apply 'start-process "Custom Diff" buffer program args))
1056 (setq mode-line-process '(":%s"))
1057 (set-process-sentinel proc 'ediff-process-sentinel)
1058 (set-process-filter proc 'ediff-process-filter)
1060 (store-match-data data))))
1062 ;; This is shell-command-filter from simple.el in FSF Emacs.
1063 ;; Copied here because XEmacs doesn't have it.
1064 (defun ediff-process-filter (proc string)
1065 ;; Do save-excursion by hand so that we can leave point numerically unchanged
1066 ;; despite an insertion immediately after it.
1067 (let* ((obuf (current-buffer))
1068 (buffer (process-buffer proc))
1070 (window (get-buffer-window buffer))
1071 (pos (window-start window)))
1075 (or (= (point) (point-max))
1076 (setq opoint (point)))
1077 (goto-char (point-max))
1078 (insert-before-markers string))
1079 ;; insert-before-markers moved this marker: set it back.
1080 (set-window-start window pos)
1081 ;; Finish our save-excursion.
1084 (set-buffer obuf))))
1086 ;; like shell-command-sentinel but doesn't print an exit status message
1087 ;; we do this because diff always exits with status 1, if diffs are found
1088 ;; so shell-command-sentinel displays a confusing message to the user
1089 (defun ediff-process-sentinel (process signal)
1090 (if (and (memq (process-status process) '(exit signal))
1091 (buffer-name (process-buffer process)))
1094 (set-buffer (process-buffer process))
1095 (setq mode-line-process nil))
1096 (delete-process process))))
1099 ;;; Word functions used to refine the current diff
1101 (defvar ediff-forward-word-function 'ediff-forward-word
1102 "*Function to call to move to the next word.
1103 Used for splitting difference regions into individual words.")
1105 (defvar ediff-whitespace " \n\t\f"
1106 "*Characters constituting white space.
1107 These characters are ignored when differing regions are split into words.")
1109 ;;(defvar ediff-word-1 "a-zA-Z---_`'.?!:"
1110 (defvar ediff-word-1 "a-zA-Z---_"
1111 "*Characters that constitute words of type 1.
1112 More precisely, [ediff-word-1] is a regexp that matches type 1 words.
1113 See `ediff-forward-word' for more details.")
1115 (defvar ediff-word-2 "0-9.,"
1116 "*Characters that constitute words of type 2.
1117 More precisely, [ediff-word-2] is a regexp that matches type 2 words.
1118 See `ediff-forward-word' for more details.")
1120 (defvar ediff-word-3 "`'?!:;\"{}[]()"
1121 "*Characters that constitute words of type 3.
1122 More precisely, [ediff-word-3] is a regexp that matches type 3 words.
1123 See `ediff-forward-word' for more details.")
1125 (defvar ediff-word-4
1126 (concat "^" ediff-word-1 ediff-word-2 ediff-word-3 ediff-whitespace)
1127 "*Characters that constitute words of type 4.
1128 More precisely, [ediff-word-4] is a regexp that matches type 4 words.
1129 See `ediff-forward-word' for more details.")
1131 ;; Split region along word boundaries. Each word will be on its own line.
1132 ;; Output to buffer out-buffer.
1133 (defun ediff-forward-word ()
1134 "Move point one word forward.
1135 There are four types of words, each of which consists entirely of
1136 characters in `ediff-word-1', `ediff-word-2', `ediff-word-3', or
1137 `ediff-word-4'. Words are recognized by passing these in turn as the
1138 argument to `skip-chars-forward'."
1139 (or (> (skip-chars-forward ediff-word-1) 0)
1140 (> (skip-chars-forward ediff-word-2) 0)
1141 (> (skip-chars-forward ediff-word-3) 0)
1142 (> (skip-chars-forward ediff-word-4) 0)
1145 (defun ediff-wordify (beg end in-buffer out-buffer &optional control-buf)
1146 (let (sv-point string)
1148 (set-buffer in-buffer)
1149 (setq string (buffer-substring beg end))
1151 (set-buffer out-buffer)
1154 (goto-char (point-min))
1155 (skip-chars-forward ediff-whitespace)
1156 (delete-region (point-min) (point))
1159 ;; eval incontrol buf to let user create local versions for
1160 ;; different invocations
1163 (ediff-eval-in-buffer control-buf ediff-forward-word-function))
1164 (funcall ediff-forward-word-function))
1165 (setq sv-point (point))
1166 (skip-chars-forward ediff-whitespace)
1167 (delete-region sv-point (point))
1170 ;; copy string from BEG END from IN-BUF to OUT-BUF
1171 (defun ediff-copy-to-buffer (beg end in-buffer out-buffer)
1174 (set-buffer in-buffer)
1175 (setq string (buffer-substring beg end))
1177 (set-buffer out-buffer)
1180 (goto-char (point-min)))))
1183 ;; goto word #n starting at current position in buffer `buf'
1184 ;; For ediff, a word is either a string of a-z,A-Z, incl `-' and `_';
1185 ;; or a string of other non-blanks. A blank is a \n\t\f
1186 ;; If `flag' is non-nil, goto the end of the n-th word.
1187 (defun ediff-goto-word (n buf &optional flag)
1188 ;; remember val ediff-forward-word-function has in ctl buf
1189 (let ((fwd-word-fun ediff-forward-word-function))
1190 (ediff-eval-in-buffer buf
1191 (skip-chars-forward ediff-whitespace)
1193 (funcall fwd-word-fun)
1194 (skip-chars-forward ediff-whitespace)
1196 (if (and flag (> n 0))
1197 (funcall fwd-word-fun))
1201 ;;; Local Variables:
1202 ;;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
1203 ;;; eval: (put 'ediff-eval-in-buffer 'lisp-indent-hook 1)
1204 ;;; eval: (put 'ediff-eval-in-buffer 'edebug-form-spec '(form body))
1207 (provide 'ediff-diff)
1210 ;; ediff-diff.el ends here