]> code.delx.au - gnu-emacs/blob - lisp/ediff-diff.el
(command-line-1): Refer to "Pure Storage" on
[gnu-emacs] / lisp / ediff-diff.el
1 ;;; ediff-diff.el --- diff-related utilities
2
3 ;; Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 ;; 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
5
6 ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29
30 ;; compiler pacifier
31 (defvar ediff-default-variant)
32 (defvar null-device)
33 (defvar longlines-mode)
34
35 (eval-when-compile
36 (let ((load-path (cons (expand-file-name ".") load-path)))
37 (or (featurep 'ediff-init)
38 (load "ediff-init.el" nil nil 'nosuffix))
39 (or (featurep 'ediff-util)
40 (load "ediff-util.el" nil nil 'nosuffix))
41 ))
42 ;; end pacifier
43
44 (require 'ediff-init)
45
46 (defgroup ediff-diff nil
47 "Diff related utilities."
48 :prefix "ediff-"
49 :group 'ediff)
50
51 ;; these two must be here to prevent ediff-test-utility from barking
52 (defcustom ediff-diff-program "diff"
53 "*Program to use for generating the differential of the two files."
54 :type 'string
55 :group 'ediff-diff)
56 (defcustom ediff-diff3-program "diff3"
57 "*Program to be used for three-way comparison.
58 Must produce output compatible with Unix's diff3 program."
59 :type 'string
60 :group 'ediff-diff)
61
62
63 ;; The following functions must precede all defcustom-defined variables.
64
65 ;; The following functions needed for setting diff/diff3 options
66 ;; test if diff supports the --binary option
67 (defsubst ediff-test-utility (diff-util option &optional files)
68 (eq 0 (apply 'call-process
69 (append (list diff-util nil nil nil option) files))))
70
71 (defun ediff-diff-mandatory-option (diff-util)
72 (let ((file (if (boundp 'null-device) null-device "/dev/null")))
73 (cond ((not (memq system-type '(ms-dos windows-nt windows-95)))
74 "")
75 ((and (string= diff-util ediff-diff-program)
76 (ediff-test-utility
77 ediff-diff-program "--binary" (list file file)))
78 "--binary")
79 ((and (string= diff-util ediff-diff3-program)
80 (ediff-test-utility
81 ediff-diff3-program "--binary" (list file file file)))
82 "--binary")
83 (t ""))))
84
85 ;; make sure that mandatory options are added even if the user changes
86 ;; ediff-diff-options or ediff-diff3-options in the customization widget
87 (defun ediff-reset-diff-options (symb val)
88 (let* ((diff-program
89 (if (eq symb 'ediff-diff-options)
90 ediff-diff-program
91 ediff-diff3-program))
92 (mandatory-option (ediff-diff-mandatory-option diff-program))
93 (spacer (if (string-equal mandatory-option "") "" " ")))
94 (set symb
95 (if (string-match mandatory-option val)
96 val
97 (concat mandatory-option spacer val)))
98 ))
99
100
101 (defcustom ediff-shell
102 (cond ((eq system-type 'emx) "cmd") ; OS/2
103 ((memq system-type '(ms-dos windows-nt windows-95))
104 shell-file-name) ; no standard name on MS-DOS
105 ((memq system-type '(vax-vms axp-vms)) "*dcl*") ; VMS
106 (t "sh")) ; UNIX
107 "*The shell used to run diff and patch.
108 If user's .profile or .cshrc files are set up correctly, any shell
109 will do. However, some people set $prompt or other things
110 incorrectly, which leads to undesirable output messages. These may
111 cause Ediff to fail. In such a case, set `ediff-shell' to a shell that
112 you are not using or, better, fix your shell's startup file."
113 :type 'string
114 :group 'ediff-diff)
115
116 (defcustom ediff-cmp-program "cmp"
117 "*Utility to use to determine if two files are identical.
118 It must return code 0, if its arguments are identical files."
119 :type 'string
120 :group 'ediff-diff)
121
122 (defcustom ediff-cmp-options nil
123 "*Options to pass to `ediff-cmp-program'.
124 If GNU diff is used as `ediff-cmp-program', then the most useful options
125 are `-I REGEXP', to ignore changes whose lines match the REGEXP."
126 :type '(repeat string)
127 :group 'ediff-diff)
128
129 (defcustom ediff-diff-options ""
130 "*Options to pass to `ediff-diff-program'.
131 If Unix diff is used as `ediff-diff-program', then a useful option is
132 `-w', to ignore space, and `-i', to ignore case of letters.
133 Options `-c' and `-i' are not allowed. Case sensitivity can be toggled
134 interactively using [ediff-toggle-ignore-case]"
135 :set 'ediff-reset-diff-options
136 :type 'string
137 :group 'ediff-diff)
138
139 (ediff-defvar-local ediff-ignore-case nil
140 "*If t, skip over difference regions that differ only in letter case.
141 This variable can be set either in .emacs or toggled interactively.
142 Use `setq-default' if setting it in .emacs")
143
144 (defcustom ediff-ignore-case-option "-i"
145 "*Option that causes the diff program to ignore case of letters."
146 :type 'string
147 :group 'ediff-diff)
148
149 (defcustom ediff-ignore-case-option3 ""
150 "*Option that causes the diff3 program to ignore case of letters.
151 GNU diff3 doesn't have such an option."
152 :type 'string
153 :group 'ediff-diff)
154
155 ;; the actual options used in comparison
156 (ediff-defvar-local ediff-actual-diff-options "" "")
157
158 (defcustom ediff-custom-diff-program ediff-diff-program
159 "*Program to use for generating custom diff output for saving it in a file.
160 This output is not used by Ediff internally."
161 :type 'string
162 :group 'ediff-diff)
163 (defcustom ediff-custom-diff-options "-c"
164 "*Options to pass to `ediff-custom-diff-program'."
165 :type 'string
166 :group 'ediff-diff)
167
168 ;;; Support for diff3
169
170 (defvar ediff-match-diff3-line "^====\\(.?\\)\C-m?$"
171 "Pattern to match lines produced by diff3 that describe differences.")
172 (defcustom ediff-diff3-options ""
173 "*Options to pass to `ediff-diff3-program'."
174 :set 'ediff-reset-diff-options
175 :type 'string
176 :group 'ediff-diff)
177
178 ;; the actual options used in comparison
179 (ediff-defvar-local ediff-actual-diff3-options "" "")
180
181 (defcustom ediff-diff3-ok-lines-regexp
182 "^\\([1-3]:\\|====\\| \\|.*Warning *:\\|.*No newline\\|.*missing newline\\|^\C-m$\\)"
183 "*Regexp that matches normal output lines from `ediff-diff3-program'.
184 Lines that do not match are assumed to be error messages."
185 :type 'regexp
186 :group 'ediff-diff)
187
188 ;; keeps the status of the current diff in 3-way jobs.
189 ;; the status can be =diff(A), =diff(B), or =diff(A+B)
190 (ediff-defvar-local ediff-diff-status "" "")
191
192
193 ;;; Fine differences
194
195 (ediff-defvar-local ediff-auto-refine (if (ediff-has-face-support-p) 'on 'nix)
196 "If `on', Ediff auto-highlights fine diffs for the current diff region.
197 If `off', auto-highlighting is not used. If `nix', no fine diffs are shown
198 at all, unless the user force-refines the region by hitting `*'.
199
200 This variable can be set either in .emacs or toggled interactively.
201 Use `setq-default' if setting it in .emacs")
202
203 (ediff-defvar-local ediff-ignore-similar-regions nil
204 "*If t, skip over difference regions that differ only in the white space and line breaks.
205 This variable can be set either in .emacs or toggled interactively.
206 Use `setq-default' if setting it in .emacs")
207
208 (ediff-defvar-local ediff-auto-refine-limit 14000
209 "*Auto-refine only the regions of this size \(in bytes\) or less.")
210
211 ;;; General
212
213 (defvar ediff-diff-ok-lines-regexp
214 (concat
215 "^\\("
216 "[0-9,]+[acd][0-9,]+\C-m?$"
217 "\\|[<>] "
218 "\\|---"
219 "\\|.*Warning *:"
220 "\\|.*No +newline"
221 "\\|.*missing +newline"
222 "\\|^\C-m?$"
223 "\\)")
224 "Regexp that matches normal output lines from `ediff-diff-program'.
225 This is mostly lifted from Emerge, except that Ediff also considers
226 warnings and `Missing newline'-type messages to be normal output.
227 Lines that do not match are assumed to be error messages.")
228
229 (defvar ediff-match-diff-line
230 (let ((x "\\([0-9]+\\)\\(\\|,\\([0-9]+\\)\\)"))
231 (concat "^" x "\\([acd]\\)" x "\C-m?$"))
232 "Pattern to match lines produced by diff that describe differences.")
233
234 (ediff-defvar-local ediff-setup-diff-regions-function nil
235 "value is a function symbol depending on the kind of job is to be done.
236 For 2-way jobs and for ediff-merge, it should be `ediff-setup-diff-regions'.
237 For jobs requiring diff3, it should be `ediff-setup-diff-regions3'.
238
239 The function should take three mandatory arguments, file-A, file-B, and
240 file-C. It may ignore file C for diff2 jobs. It should also take
241 one optional arguments, diff-number to refine.")
242
243
244 ;;; Functions
245
246 ;; Generate the difference vector and overlays for the two files
247 ;; With optional arg REG-TO-REFINE, refine this region.
248 ;; File-C argument is not used here. It is there just because
249 ;; ediff-setup-diff-regions is called via a funcall to
250 ;; ediff-setup-diff-regions-function, which can also have the value
251 ;; ediff-setup-diff-regions3, which takes 4 arguments.
252 (defun ediff-setup-diff-regions (file-A file-B file-C)
253 ;; looking for '-c', '-i', or a 'c', 'i' among clustered non-long options
254 (if (string-match "^-[ci]\\| -[ci]\\|\\(^\\| \\)-[^- ]+[ci]"
255 ediff-diff-options)
256 (error "Options `-c' and `-i' are not allowed in `ediff-diff-options'"))
257
258 ;; create, if it doesn't exist
259 (or (ediff-buffer-live-p ediff-diff-buffer)
260 (setq ediff-diff-buffer
261 (get-buffer-create (ediff-unique-buffer-name "*ediff-diff" "*"))))
262 (ediff-make-diff2-buffer ediff-diff-buffer file-A file-B)
263 (ediff-prepare-error-list ediff-diff-ok-lines-regexp ediff-diff-buffer)
264 (ediff-convert-diffs-to-overlays
265 (ediff-extract-diffs
266 ediff-diff-buffer ediff-word-mode ediff-narrow-bounds)))
267
268 ;; Run the diff program on FILE1 and FILE2 and put the output in DIFF-BUFFER
269 ;; Return the size of DIFF-BUFFER
270 ;; The return code isn't used in the program at present.
271 (defun ediff-make-diff2-buffer (diff-buffer file1 file2)
272 (let ((file1-size (ediff-file-size file1))
273 (file2-size (ediff-file-size file2)))
274 (cond ((not (numberp file1-size))
275 (message "Can't find file: %s"
276 (ediff-abbreviate-file-name file1))
277 (sit-for 2)
278 ;; 1 is an error exit code
279 1)
280 ((not (numberp file2-size))
281 (message "Can't find file: %s"
282 (ediff-abbreviate-file-name file2))
283 (sit-for 2)
284 ;; 1 is an error exit code
285 1)
286 (t (message "Computing differences between %s and %s ..."
287 (file-name-nondirectory file1)
288 (file-name-nondirectory file2))
289 ;; this erases the diff buffer automatically
290 (ediff-exec-process ediff-diff-program
291 diff-buffer
292 'synchronize
293 ediff-actual-diff-options file1 file2)
294 (message "")
295 (ediff-with-current-buffer diff-buffer
296 (buffer-size))))))
297
298
299
300 ;; If file-A/B/C is nil, do 2-way comparison with the non-nil buffers
301 ;; This function works for diff3 and diff2 jobs
302 (defun ediff-setup-fine-diff-regions (file-A file-B file-C reg-num)
303 (or (ediff-buffer-live-p ediff-fine-diff-buffer)
304 (setq ediff-fine-diff-buffer
305 (get-buffer-create
306 (ediff-unique-buffer-name "*ediff-fine-diff" "*"))))
307
308 (let (diff3-job diff-program diff-options ok-regexp diff-list)
309 (setq diff3-job ediff-3way-job
310 diff-program (if diff3-job ediff-diff3-program ediff-diff-program)
311 diff-options (if diff3-job
312 ediff-actual-diff3-options
313 ediff-actual-diff-options)
314 ok-regexp (if diff3-job
315 ediff-diff3-ok-lines-regexp
316 ediff-diff-ok-lines-regexp))
317
318 (ediff-message-if-verbose "Refining difference region %d ..." (1+ reg-num))
319 (ediff-exec-process diff-program ediff-fine-diff-buffer 'synchronize
320 diff-options
321 ;; The shuffle below is because we can compare 3-way
322 ;; or in several 2-way fashions, like fA fC, fA fB,
323 ;; or fB fC.
324 (if file-A file-A file-B)
325 (if file-B file-B file-A)
326 (if diff3-job
327 (if file-C file-C file-B))
328 ) ; exec process
329
330 (ediff-prepare-error-list ok-regexp ediff-fine-diff-buffer)
331 (ediff-message-if-verbose
332 "")
333 ;; "Refining difference region %d ... done" (1+ reg-num))
334
335 (setq diff-list
336 (if diff3-job
337 (ediff-extract-diffs3
338 ediff-fine-diff-buffer '3way-comparison 'word-mode)
339 (ediff-extract-diffs ediff-fine-diff-buffer 'word-mode)))
340 ;; fixup diff-list
341 (if diff3-job
342 (cond ((not file-A)
343 (mapcar (lambda (elt)
344 (aset elt 0 nil)
345 (aset elt 1 nil))
346 (cdr diff-list)))
347 ((not file-B)
348 (mapcar (lambda (elt)
349 (aset elt 2 nil)
350 (aset elt 3 nil))
351 (cdr diff-list)))
352 ((not file-C)
353 (mapcar (lambda (elt)
354 (aset elt 4 nil)
355 (aset elt 5 nil))
356 (cdr diff-list)))
357 ))
358
359 (ediff-convert-fine-diffs-to-overlays diff-list reg-num)
360 ))
361
362
363 (defun ediff-prepare-error-list (ok-regexp diff-buff)
364 (or (ediff-buffer-live-p ediff-error-buffer)
365 (setq ediff-error-buffer
366 (get-buffer-create (ediff-unique-buffer-name
367 "*ediff-errors" "*"))))
368 (ediff-with-current-buffer ediff-error-buffer
369 (erase-buffer)
370 (insert (ediff-with-current-buffer diff-buff (buffer-string)))
371 (goto-char (point-min))
372 (delete-matching-lines ok-regexp)
373 (if (memq system-type '(vax-vms axp-vms))
374 (delete-matching-lines "^$")))
375 ;; If diff reports errors, show them then quit.
376 (if (/= 0 (ediff-with-current-buffer ediff-error-buffer (buffer-size)))
377 (let ((ctl-buf ediff-control-buffer)
378 (error-buf ediff-error-buffer))
379 (ediff-skip-unsuitable-frames)
380 (switch-to-buffer error-buf)
381 (ediff-kill-buffer-carefully ctl-buf)
382 (error "Errors in diff output. Diff output is in %S" diff-buff))))
383
384 ;; BOUNDS specifies visibility bounds to use.
385 ;; WORD-MODE tells whether we are in the word-mode or not.
386 ;; If WORD-MODE, also construct vector of diffs using word numbers.
387 ;; Else, use point values.
388 ;; This function handles diff-2 jobs including the case of
389 ;; merging buffers and files without ancestor.
390 (defun ediff-extract-diffs (diff-buffer word-mode &optional bounds)
391 (let ((A-buffer ediff-buffer-A)
392 (B-buffer ediff-buffer-B)
393 (C-buffer ediff-buffer-C)
394 (a-prev 1) ; this is needed to set the first diff line correctly
395 (a-prev-pt nil)
396 (b-prev 1)
397 (b-prev-pt nil)
398 (c-prev 1)
399 (c-prev-pt nil)
400 diff-list shift-A shift-B
401 )
402
403 ;; diff list contains word numbers, unless changed later
404 (setq diff-list (cons (if word-mode 'words 'points)
405 diff-list))
406 ;; we don't use visibility bounds for buffer C when merging
407 (if bounds
408 (setq shift-A
409 (ediff-overlay-start
410 (ediff-get-value-according-to-buffer-type 'A bounds))
411 shift-B
412 (ediff-overlay-start
413 (ediff-get-value-according-to-buffer-type 'B bounds))))
414
415 ;; reset point in buffers A/B/C
416 (ediff-with-current-buffer A-buffer
417 (goto-char (if shift-A shift-A (point-min))))
418 (ediff-with-current-buffer B-buffer
419 (goto-char (if shift-B shift-B (point-min))))
420 (if (ediff-buffer-live-p C-buffer)
421 (ediff-with-current-buffer C-buffer
422 (goto-char (point-min))))
423
424 (ediff-with-current-buffer diff-buffer
425 (goto-char (point-min))
426 (while (re-search-forward ediff-match-diff-line nil t)
427 (let* ((a-begin (string-to-number (buffer-substring (match-beginning 1)
428 (match-end 1))))
429 (a-end (let ((b (match-beginning 3))
430 (e (match-end 3)))
431 (if b
432 (string-to-number (buffer-substring b e))
433 a-begin)))
434 (diff-type (buffer-substring (match-beginning 4) (match-end 4)))
435 (b-begin (string-to-number (buffer-substring (match-beginning 5)
436 (match-end 5))))
437 (b-end (let ((b (match-beginning 7))
438 (e (match-end 7)))
439 (if b
440 (string-to-number (buffer-substring b e))
441 b-begin)))
442 a-begin-pt a-end-pt b-begin-pt b-end-pt
443 c-begin c-end c-begin-pt c-end-pt)
444 ;; fix the beginning and end numbers, because diff is somewhat
445 ;; strange about how it numbers lines
446 (if (string-equal diff-type "a")
447 (setq b-end (1+ b-end)
448 a-begin (1+ a-begin)
449 a-end a-begin)
450 (if (string-equal diff-type "d")
451 (setq a-end (1+ a-end)
452 b-begin (1+ b-begin)
453 b-end b-begin)
454 ;; (string-equal diff-type "c")
455 (setq a-end (1+ a-end)
456 b-end (1+ b-end))))
457
458 (if (eq ediff-default-variant 'default-B)
459 (setq c-begin b-begin
460 c-end b-end)
461 (setq c-begin a-begin
462 c-end a-end))
463
464 ;; compute main diff vector
465 (if word-mode
466 ;; make diff-list contain word numbers
467 (setq diff-list
468 (nconc diff-list
469 (list
470 (if (ediff-buffer-live-p C-buffer)
471 (vector (- a-begin a-prev) (- a-end a-begin)
472 (- b-begin b-prev) (- b-end b-begin)
473 (- c-begin c-prev) (- c-end c-begin)
474 nil nil ; dummy ancestor
475 nil ; state of diff
476 nil ; state of merge
477 nil ; state of ancestor
478 )
479 (vector (- a-begin a-prev) (- a-end a-begin)
480 (- b-begin b-prev) (- b-end b-begin)
481 nil nil ; dummy buf C
482 nil nil ; dummy ancestor
483 nil ; state of diff
484 nil ; state of merge
485 nil ; state of ancestor
486 ))
487 ))
488 a-prev a-end
489 b-prev b-end
490 c-prev c-end)
491 ;; else convert lines to points
492 (ediff-with-current-buffer A-buffer
493 (let ((longlines-mode-val
494 (if (and (boundp 'longlines-mode) longlines-mode) 1 0)))
495 ;; we must disable and then restore longlines-mode
496 (if (eq longlines-mode-val 1)
497 (longlines-mode 0))
498 (goto-char (or a-prev-pt shift-A (point-min)))
499 (forward-line (- a-begin a-prev))
500 (setq a-begin-pt (point))
501 (forward-line (- a-end a-begin))
502 (setq a-end-pt (point)
503 a-prev a-end
504 a-prev-pt a-end-pt)
505 (if (eq longlines-mode-val 1)
506 (longlines-mode longlines-mode-val))
507 ))
508 (ediff-with-current-buffer B-buffer
509 (let ((longlines-mode-val
510 (if (and (boundp 'longlines-mode) longlines-mode) 1 0)))
511 (if (eq longlines-mode-val 1)
512 (longlines-mode 0))
513 (goto-char (or b-prev-pt shift-B (point-min)))
514 (forward-line (- b-begin b-prev))
515 (setq b-begin-pt (point))
516 (forward-line (- b-end b-begin))
517 (setq b-end-pt (point)
518 b-prev b-end
519 b-prev-pt b-end-pt)
520 (if (eq longlines-mode-val 1)
521 (longlines-mode longlines-mode-val))
522 ))
523 (if (ediff-buffer-live-p C-buffer)
524 (ediff-with-current-buffer C-buffer
525 (let ((longlines-mode-val
526 (if (and (boundp 'longlines-mode) longlines-mode) 1 0)))
527 (if (eq longlines-mode-val 1)
528 (longlines-mode 0))
529 (goto-char (or c-prev-pt (point-min)))
530 (forward-line (- c-begin c-prev))
531 (setq c-begin-pt (point))
532 (forward-line (- c-end c-begin))
533 (setq c-end-pt (point)
534 c-prev c-end
535 c-prev-pt c-end-pt)
536 (if (eq longlines-mode-val 1)
537 (longlines-mode longlines-mode-val))
538 )))
539 (setq diff-list
540 (nconc
541 diff-list
542 (list
543 (if (ediff-buffer-live-p C-buffer)
544 (vector
545 a-begin-pt a-end-pt b-begin-pt b-end-pt
546 c-begin-pt c-end-pt
547 nil nil ; dummy ancestor
548 ;; state of diff
549 ;; shows which buff is different from the other two
550 (if (eq ediff-default-variant 'default-B) 'A 'B)
551 ediff-default-variant ; state of merge
552 nil ; state of ancestor
553 )
554 (vector a-begin-pt a-end-pt
555 b-begin-pt b-end-pt
556 nil nil ; dummy buf C
557 nil nil ; dummy ancestor
558 nil nil ; dummy state of diff & merge
559 nil ; dummy state of ancestor
560 )))
561 )))
562
563 ))) ; end ediff-with-current-buffer
564 diff-list
565 ))
566
567
568 (defun ediff-convert-diffs-to-overlays (diff-list)
569 (ediff-set-diff-overlays-in-one-buffer 'A diff-list)
570 (ediff-set-diff-overlays-in-one-buffer 'B diff-list)
571 (if ediff-3way-job
572 (ediff-set-diff-overlays-in-one-buffer 'C diff-list))
573 (if ediff-merge-with-ancestor-job
574 (ediff-set-diff-overlays-in-one-buffer 'Ancestor diff-list))
575 ;; set up vector showing the status of merge regions
576 (if ediff-merge-job
577 (setq ediff-state-of-merge
578 (vconcat
579 (mapcar (lambda (elt)
580 (let ((state-of-merge (aref elt 9))
581 (state-of-ancestor (aref elt 10)))
582 (vector
583 ;; state of merge: prefers/default-A/B or combined
584 (if state-of-merge (format "%S" state-of-merge))
585 ;; whether the ancestor region is empty
586 state-of-ancestor)))
587 ;; the first elt designates type of list
588 (cdr diff-list))
589 )))
590 (message "Processing difference regions ... done"))
591
592
593 (defun ediff-set-diff-overlays-in-one-buffer (buf-type diff-list)
594 (let* ((current-diff -1)
595 (buff (ediff-get-buffer buf-type))
596 (ctl-buf ediff-control-buffer)
597 ;; ediff-extract-diffs puts the type of diff-list as the first elt
598 ;; of this list. The type is either 'points or 'words
599 (diff-list-type (car diff-list))
600 (shift (ediff-overlay-start
601 (ediff-get-value-according-to-buffer-type
602 buf-type ediff-narrow-bounds)))
603 (limit (ediff-overlay-end
604 (ediff-get-value-according-to-buffer-type
605 buf-type ediff-narrow-bounds)))
606 diff-overlay-list list-element total-diffs
607 begin end pt-saved overlay state-of-diff)
608
609 (setq diff-list (cdr diff-list)) ; discard diff list type
610 (setq total-diffs (length diff-list))
611
612 ;; shift, if necessary
613 (ediff-with-current-buffer buff (setq pt-saved shift))
614
615 (while diff-list
616 (setq current-diff (1+ current-diff)
617 list-element (car diff-list)
618 begin (aref list-element (cond ((eq buf-type 'A) 0)
619 ((eq buf-type 'B) 2)
620 ((eq buf-type 'C) 4)
621 (t 6))) ; Ancestor
622 end (aref list-element (cond ((eq buf-type 'A) 1)
623 ((eq buf-type 'B) 3)
624 ((eq buf-type 'C) 5)
625 (t 7))) ; Ancestor
626 state-of-diff (aref list-element 8)
627 )
628
629 (cond ((and (not (eq buf-type state-of-diff))
630 (not (eq buf-type 'Ancestor))
631 (memq state-of-diff '(A B C)))
632 (setq state-of-diff
633 (car (delq buf-type (delq state-of-diff (list 'A 'B 'C)))))
634 (setq state-of-diff (format "=diff(%S)" state-of-diff))
635 )
636 (t (setq state-of-diff nil)))
637
638 ;; Put overlays at appropriate places in buffer
639 ;; convert word numbers to points, if necessary
640 (if (eq diff-list-type 'words)
641 (progn
642 (ediff-with-current-buffer buff (goto-char pt-saved))
643 (ediff-with-current-buffer ctl-buf
644 (setq begin (ediff-goto-word (1+ begin) buff)
645 end (ediff-goto-word end buff 'end)))
646 (if (> end limit) (setq end limit))
647 (if (> begin end) (setq begin end))
648 (setq pt-saved (ediff-with-current-buffer buff (point)))))
649 (setq overlay (ediff-make-bullet-proof-overlay begin end buff))
650
651 (ediff-overlay-put overlay 'priority ediff-shadow-overlay-priority)
652 (ediff-overlay-put overlay 'ediff-diff-num current-diff)
653 (if (and (ediff-has-face-support-p)
654 ediff-use-faces ediff-highlight-all-diffs)
655 (ediff-set-overlay-face
656 overlay (ediff-background-face buf-type current-diff)))
657
658 (if (= 0 (mod current-diff 10))
659 (message "Buffer %S: Processing difference region %d of %d"
660 buf-type current-diff total-diffs))
661 ;; Record all overlays for this difference.
662 ;; The 2-d elt, nil, is a place holder for the fine diff vector.
663 ;; The 3-d elt, nil, is a place holder for no-fine-diffs flag.
664 ;; The 4-th elt says which diff region is different from the other two
665 ;; (3-way jobs only).
666 (setq diff-overlay-list
667 (nconc
668 diff-overlay-list
669 (list (vector overlay nil nil state-of-diff)))
670 diff-list
671 (cdr diff-list))
672 ) ; while
673
674 (set (ediff-get-symbol-from-alist buf-type ediff-difference-vector-alist)
675 (vconcat diff-overlay-list))
676 ))
677
678 ;; `n' is the diff region to work on. Default is ediff-current-difference.
679 ;; if `flag' is 'noforce then make fine-diffs only if this region's fine
680 ;; diffs have not been computed before.
681 ;; if `flag' is 'skip then don't compute fine diffs for this region.
682 (defun ediff-make-fine-diffs (&optional n flag)
683 (or n (setq n ediff-current-difference))
684
685 (if (< ediff-number-of-differences 1)
686 (error ediff-NO-DIFFERENCES))
687
688 (if ediff-word-mode
689 (setq flag 'skip
690 ediff-auto-refine 'nix))
691
692 (or (< n 0)
693 (>= n ediff-number-of-differences)
694 ;; n is within the range
695 (let ((tmp-buffer (get-buffer-create ediff-tmp-buffer))
696 (file-A ediff-temp-file-A)
697 (file-B ediff-temp-file-B)
698 (file-C ediff-temp-file-C)
699 (empty-A (ediff-empty-diff-region-p n 'A))
700 (empty-B (ediff-empty-diff-region-p n 'B))
701 (empty-C (ediff-empty-diff-region-p n 'C))
702 (whitespace-A (ediff-whitespace-diff-region-p n 'A))
703 (whitespace-B (ediff-whitespace-diff-region-p n 'B))
704 (whitespace-C (ediff-whitespace-diff-region-p n 'C))
705 cumulative-fine-diff-length)
706
707 (cond ;; If one of the regions is empty (or 2 in 3way comparison)
708 ;; then don't refine.
709 ;; If the region happens to be entirely whitespace or empty then
710 ;; mark as such.
711 ((> (length (delq nil (list empty-A empty-B empty-C))) 1)
712 (if (and (ediff-looks-like-combined-merge n)
713 ediff-merge-job)
714 (ediff-set-fine-overlays-in-one-buffer 'C nil n))
715 (if ediff-3way-comparison-job
716 (ediff-message-if-verbose
717 "Region %d is empty in all buffers but %S"
718 (1+ n)
719 (cond ((not empty-A) 'A)
720 ((not empty-B) 'B)
721 ((not empty-C) 'C)))
722 (ediff-message-if-verbose
723 "Region %d in buffer %S is empty"
724 (1+ n)
725 (cond (empty-A 'A)
726 (empty-B 'B)
727 (empty-C 'C)))
728 )
729 ;; if all regions happen to be whitespace
730 (if (and whitespace-A whitespace-B whitespace-C)
731 ;; mark as space only
732 (ediff-mark-diff-as-space-only n t)
733 ;; if some regions are white and others don't, then mark as
734 ;; non-white-space-only
735 (ediff-mark-diff-as-space-only n nil)))
736
737 ;; don't compute fine diffs if diff vector exists
738 ((and (eq flag 'noforce) (ediff-get-fine-diff-vector n 'A))
739 (if (ediff-no-fine-diffs-p n)
740 (message
741 "Only white-space differences in region %d %s"
742 (1+ n)
743 (cond ((eq (ediff-no-fine-diffs-p n) 'A)
744 "in buffers B & C")
745 ((eq (ediff-no-fine-diffs-p n) 'B)
746 "in buffers A & C")
747 ((eq (ediff-no-fine-diffs-p n) 'C)
748 "in buffers A & B")
749 (t "")))))
750 ;; don't compute fine diffs for this region
751 ((eq flag 'skip)
752 (or (ediff-get-fine-diff-vector n 'A)
753 (memq ediff-auto-refine '(off nix))
754 (ediff-message-if-verbose
755 "Region %d exceeds the auto-refinement limit. Type `%s' to refine"
756 (1+ n)
757 (substitute-command-keys
758 "\\[ediff-make-or-kill-fine-diffs]")
759 )))
760 (t
761 ;; recompute fine diffs
762 (ediff-wordify
763 (ediff-get-diff-posn 'A 'beg n)
764 (ediff-get-diff-posn 'A 'end n)
765 ediff-buffer-A
766 tmp-buffer
767 ediff-control-buffer)
768 (setq file-A
769 (ediff-make-temp-file tmp-buffer "fineDiffA" file-A))
770
771 (ediff-wordify
772 (ediff-get-diff-posn 'B 'beg n)
773 (ediff-get-diff-posn 'B 'end n)
774 ediff-buffer-B
775 tmp-buffer
776 ediff-control-buffer)
777 (setq file-B
778 (ediff-make-temp-file tmp-buffer "fineDiffB" file-B))
779
780 (if ediff-3way-job
781 (progn
782 (ediff-wordify
783 (ediff-get-diff-posn 'C 'beg n)
784 (ediff-get-diff-posn 'C 'end n)
785 ediff-buffer-C
786 tmp-buffer
787 ediff-control-buffer)
788 (setq file-C
789 (ediff-make-temp-file
790 tmp-buffer "fineDiffC" file-C))))
791
792 ;; save temp file names.
793 (setq ediff-temp-file-A file-A
794 ediff-temp-file-B file-B
795 ediff-temp-file-C file-C)
796
797 ;; set the new vector of fine diffs, if none exists
798 (cond ((and ediff-3way-job whitespace-A)
799 (ediff-setup-fine-diff-regions nil file-B file-C n))
800 ((and ediff-3way-job whitespace-B)
801 (ediff-setup-fine-diff-regions file-A nil file-C n))
802 ((and ediff-3way-job
803 ;; In merge-jobs, whitespace-C is t, since
804 ;; ediff-empty-diff-region-p returns t in this case
805 whitespace-C)
806 (ediff-setup-fine-diff-regions file-A file-B nil n))
807 (t
808 (ediff-setup-fine-diff-regions file-A file-B file-C n)))
809
810 (setq cumulative-fine-diff-length
811 (+ (length (ediff-get-fine-diff-vector n 'A))
812 (length (ediff-get-fine-diff-vector n 'B))
813 ;; in merge jobs, the merge buffer is never refined
814 (if (and file-C (not ediff-merge-job))
815 (length (ediff-get-fine-diff-vector n 'C))
816 0)))
817
818 (cond ((or
819 ;; all regions are white space
820 (and whitespace-A whitespace-B whitespace-C)
821 ;; none is white space and no fine diffs detected
822 (and (not whitespace-A)
823 (not whitespace-B)
824 (not (and ediff-3way-job whitespace-C))
825 (eq cumulative-fine-diff-length 0)))
826 (ediff-mark-diff-as-space-only n t)
827 (ediff-message-if-verbose
828 "Only white-space differences in region %d" (1+ n)))
829 ((eq cumulative-fine-diff-length 0)
830 (ediff-message-if-verbose
831 "Only white-space differences in region %d %s"
832 (1+ n)
833 (cond (whitespace-A (ediff-mark-diff-as-space-only n 'A)
834 "in buffers B & C")
835 (whitespace-B (ediff-mark-diff-as-space-only n 'B)
836 "in buffers A & C")
837 (whitespace-C (ediff-mark-diff-as-space-only n 'C)
838 "in buffers A & B"))))
839 (t
840 (ediff-mark-diff-as-space-only n nil)))
841 )
842 ) ; end cond
843 (ediff-set-fine-diff-properties n)
844 )))
845
846 ;; Interface to ediff-make-fine-diffs. Checks for auto-refine limit, etc.
847 (defun ediff-install-fine-diff-if-necessary (n)
848 (cond ((and (eq ediff-auto-refine 'on)
849 ediff-use-faces
850 (not (eq ediff-highlighting-style 'off))
851 (not (eq ediff-highlighting-style 'ascii)))
852 (if (and
853 (> ediff-auto-refine-limit
854 (- (ediff-get-diff-posn 'A 'end n)
855 (ediff-get-diff-posn 'A 'beg n)))
856 (> ediff-auto-refine-limit
857 (- (ediff-get-diff-posn 'B 'end n)
858 (ediff-get-diff-posn 'B 'beg n))))
859 (ediff-make-fine-diffs n 'noforce)
860 (ediff-make-fine-diffs n 'skip)))
861
862 ;; highlight iff fine diffs already exist
863 ((eq ediff-auto-refine 'off)
864 (ediff-make-fine-diffs n 'skip))))
865
866
867 ;; if fine diff vector is not set for diff N, then do nothing
868 (defun ediff-set-fine-diff-properties (n &optional default)
869 (or (not (ediff-has-face-support-p))
870 (< n 0)
871 (>= n ediff-number-of-differences)
872 ;; when faces are supported, set faces and priorities of fine overlays
873 (progn
874 (ediff-set-fine-diff-properties-in-one-buffer 'A n default)
875 (ediff-set-fine-diff-properties-in-one-buffer 'B n default)
876 (if ediff-3way-job
877 (ediff-set-fine-diff-properties-in-one-buffer 'C n default)))))
878
879 (defun ediff-set-fine-diff-properties-in-one-buffer (buf-type
880 n &optional default)
881 (let ((fine-diff-vector (ediff-get-fine-diff-vector n buf-type))
882 (face (if default
883 'default
884 (face-name
885 (ediff-get-symbol-from-alist
886 buf-type ediff-fine-diff-face-alist))))
887 (priority (if default
888 0
889 (1+ (or (ediff-overlay-get
890 (symbol-value
891 (ediff-get-symbol-from-alist
892 buf-type
893 ediff-current-diff-overlay-alist))
894 'priority)
895 0)))))
896 (mapcar (lambda (overl)
897 (ediff-set-overlay-face overl face)
898 (ediff-overlay-put overl 'priority priority))
899 fine-diff-vector)))
900
901 ;; Set overlays over the regions that denote delimiters
902 (defun ediff-set-fine-overlays-for-combined-merge (diff-list reg-num)
903 (let (overlay overlay-list)
904 (while diff-list
905 (condition-case nil
906 (setq overlay
907 (ediff-make-bullet-proof-overlay
908 (nth 0 diff-list) (nth 1 diff-list) ediff-buffer-C))
909 (error ""))
910 (setq overlay-list (cons overlay overlay-list))
911 (if (> (length diff-list) 1)
912 (setq diff-list (cdr (cdr diff-list)))
913 (error "ediff-set-fine-overlays-for-combined-merge: corrupt list of
914 delimiter regions"))
915 )
916 (setq overlay-list (reverse overlay-list))
917 (ediff-set-fine-diff-vector
918 reg-num 'C (apply 'vector overlay-list))
919 ))
920
921
922 ;; Convert diff list to overlays for a given DIFF-REGION
923 ;; in buffer of type BUF-TYPE
924 (defun ediff-set-fine-overlays-in-one-buffer (buf-type diff-list region-num)
925 (let* ((current-diff -1)
926 (reg-start (ediff-get-diff-posn buf-type 'beg region-num))
927 (buff (ediff-get-buffer buf-type))
928 (ctl-buf ediff-control-buffer)
929 combined-merge-diff-list
930 diff-overlay-list list-element
931 begin end overlay)
932
933 (ediff-clear-fine-differences-in-one-buffer region-num buf-type)
934 (setq diff-list (cdr diff-list)) ; discard list type (words or points)
935 (ediff-with-current-buffer buff (goto-char reg-start))
936
937 ;; if it is a combined merge then set overlays in buff C specially
938 (if (and ediff-merge-job (eq buf-type 'C)
939 (setq combined-merge-diff-list
940 (ediff-looks-like-combined-merge region-num)))
941 (ediff-set-fine-overlays-for-combined-merge
942 combined-merge-diff-list region-num)
943 ;; regular fine diff
944 (while diff-list
945 (setq current-diff (1+ current-diff)
946 list-element (car diff-list)
947 begin (aref list-element (cond ((eq buf-type 'A) 0)
948 ((eq buf-type 'B) 2)
949 (t 4))) ; buf C
950 end (aref list-element (cond ((eq buf-type 'A) 1)
951 ((eq buf-type 'B) 3)
952 (t 5)))) ; buf C
953 (if (not (or begin end))
954 () ; skip this diff
955 ;; Put overlays at appropriate places in buffers
956 ;; convert lines to points, if necessary
957 (ediff-with-current-buffer ctl-buf
958 (setq begin (ediff-goto-word (1+ begin) buff)
959 end (ediff-goto-word end buff 'end)))
960 (setq overlay (ediff-make-bullet-proof-overlay begin end buff))
961 ;; record all overlays for this difference region
962 (setq diff-overlay-list (nconc diff-overlay-list (list overlay))))
963
964 (setq diff-list (cdr diff-list))
965 ) ; while
966 ;; convert the list of difference information into a vector
967 ;; for fast access
968 (ediff-set-fine-diff-vector
969 region-num buf-type (vconcat diff-overlay-list))
970 )))
971
972
973 (defsubst ediff-convert-fine-diffs-to-overlays (diff-list region-num)
974 (ediff-set-fine-overlays-in-one-buffer 'A diff-list region-num)
975 (ediff-set-fine-overlays-in-one-buffer 'B diff-list region-num)
976 (if ediff-3way-job
977 (ediff-set-fine-overlays-in-one-buffer 'C diff-list region-num)
978 ))
979
980
981 ;; Stolen from emerge.el
982 (defun ediff-get-diff3-group (file)
983 ;; This save-excursion allows ediff-get-diff3-group to be called for the
984 ;; various groups of lines (1, 2, 3) in any order, and for the lines to
985 ;; appear in any order. The reason this is necessary is that Gnu diff3
986 ;; can produce the groups in the order 1, 2, 3 or 1, 3, 2.
987 (save-excursion
988 (re-search-forward
989 (concat "^" file ":\\([0-9]+\\)\\(,\\([0-9]+\\)\\)?\\([ac]\\)\C-m?$"))
990 (beginning-of-line 2)
991 ;; treatment depends on whether it is an "a" group or a "c" group
992 (if (string-equal (buffer-substring (match-beginning 4) (match-end 4)) "c")
993 ;; it is a "c" group
994 (if (match-beginning 2)
995 ;; it has two numbers
996 (list (string-to-number
997 (buffer-substring (match-beginning 1) (match-end 1)))
998 (1+ (string-to-number
999 (buffer-substring (match-beginning 3) (match-end 3)))))
1000 ;; it has one number
1001 (let ((x (string-to-number
1002 (buffer-substring (match-beginning 1) (match-end 1)))))
1003 (list x (1+ x))))
1004 ;; it is an "a" group
1005 (let ((x (1+ (string-to-number
1006 (buffer-substring (match-beginning 1) (match-end 1))))))
1007 (list x x)))))
1008
1009
1010 ;; If WORD-MODE, construct vector of diffs using word numbers.
1011 ;; Else, use point values.
1012 ;; WORD-MODE also tells if we are in the word-mode or not.
1013 ;; If THREE-WAY-COMP, then it is a 3-way comparison. Else, it is merging
1014 ;; with ancestor, in which case buffer-C contents is identical to buffer-A/B,
1015 ;; contents (unless buffer-A is narrowed) depending on ediff-default-variant's
1016 ;; value.
1017 ;; BOUNDS specifies visibility bounds to use.
1018 (defun ediff-extract-diffs3 (diff-buffer word-mode three-way-comp
1019 &optional bounds)
1020 (let ((A-buffer ediff-buffer-A)
1021 (B-buffer ediff-buffer-B)
1022 (C-buffer ediff-buffer-C)
1023 (anc-buffer ediff-ancestor-buffer)
1024 (a-prev 1) ; needed to set the first diff line correctly
1025 (a-prev-pt nil)
1026 (b-prev 1)
1027 (b-prev-pt nil)
1028 (c-prev 1)
1029 (c-prev-pt nil)
1030 (anc-prev 1)
1031 diff-list shift-A shift-B shift-C
1032 )
1033
1034 ;; diff list contains word numbers or points, depending on word-mode
1035 (setq diff-list (cons (if word-mode 'words 'points)
1036 diff-list))
1037 (if bounds
1038 (setq shift-A
1039 (ediff-overlay-start
1040 (ediff-get-value-according-to-buffer-type 'A bounds))
1041 shift-B
1042 (ediff-overlay-start
1043 (ediff-get-value-according-to-buffer-type 'B bounds))
1044 shift-C
1045 (if three-way-comp
1046 (ediff-overlay-start
1047 (ediff-get-value-according-to-buffer-type 'C bounds)))))
1048
1049 ;; reset point in buffers A, B, C
1050 (ediff-with-current-buffer A-buffer
1051 (goto-char (if shift-A shift-A (point-min))))
1052 (ediff-with-current-buffer B-buffer
1053 (goto-char (if shift-B shift-B (point-min))))
1054 (if three-way-comp
1055 (ediff-with-current-buffer C-buffer
1056 (goto-char (if shift-C shift-C (point-min)))))
1057 (if (ediff-buffer-live-p anc-buffer)
1058 (ediff-with-current-buffer anc-buffer
1059 (goto-char (point-min))))
1060
1061 (ediff-with-current-buffer diff-buffer
1062 (goto-char (point-min))
1063 (while (re-search-forward ediff-match-diff3-line nil t)
1064 ;; leave point after matched line
1065 (beginning-of-line 2)
1066 (let ((agreement (buffer-substring (match-beginning 1) (match-end 1))))
1067 ;; if the files A and B are the same and not 3way-comparison,
1068 ;; ignore the difference
1069 (if (or three-way-comp (not (string-equal agreement "3")))
1070 (let* ((a-begin (car (ediff-get-diff3-group "1")))
1071 (a-end (nth 1 (ediff-get-diff3-group "1")))
1072 (b-begin (car (ediff-get-diff3-group "2")))
1073 (b-end (nth 1 (ediff-get-diff3-group "2")))
1074 (c-or-anc-begin (car (ediff-get-diff3-group "3")))
1075 (c-or-anc-end (nth 1 (ediff-get-diff3-group "3")))
1076 (state-of-merge
1077 (cond ((string-equal agreement "1") 'prefer-A)
1078 ((string-equal agreement "2") 'prefer-B)
1079 (t ediff-default-variant)))
1080 (state-of-diff-merge
1081 (if (memq state-of-merge '(default-A prefer-A)) 'B 'A))
1082 (state-of-diff-comparison
1083 (cond ((string-equal agreement "1") 'A)
1084 ((string-equal agreement "2") 'B)
1085 ((string-equal agreement "3") 'C)))
1086 state-of-ancestor
1087 c-begin c-end
1088 a-begin-pt a-end-pt
1089 b-begin-pt b-end-pt
1090 c-begin-pt c-end-pt
1091 anc-begin-pt anc-end-pt)
1092
1093 (setq state-of-ancestor
1094 (= c-or-anc-begin c-or-anc-end))
1095
1096 (cond (three-way-comp
1097 (setq c-begin c-or-anc-begin
1098 c-end c-or-anc-end))
1099 ((eq ediff-default-variant 'default-B)
1100 (setq c-begin b-begin
1101 c-end b-end))
1102 (t
1103 (setq c-begin a-begin
1104 c-end a-end)))
1105
1106 ;; compute main diff vector
1107 (if word-mode
1108 ;; make diff-list contain word numbers
1109 (setq diff-list
1110 (nconc diff-list
1111 (list (vector
1112 (- a-begin a-prev) (- a-end a-begin)
1113 (- b-begin b-prev) (- b-end b-begin)
1114 (- c-begin c-prev) (- c-end c-begin)
1115 nil nil ; dummy ancestor
1116 nil ; state of diff
1117 nil ; state of merge
1118 nil ; state of ancestor
1119 )))
1120 a-prev a-end
1121 b-prev b-end
1122 c-prev c-end)
1123 ;; else convert lines to points
1124 (ediff-with-current-buffer A-buffer
1125 (let ((longlines-mode-val
1126 (if (and (boundp 'longlines-mode) longlines-mode) 1 0)))
1127 ;; we must disable and then restore longlines-mode
1128 (if (eq longlines-mode-val 1)
1129 (longlines-mode 0))
1130 (goto-char (or a-prev-pt shift-A (point-min)))
1131 (forward-line (- a-begin a-prev))
1132 (setq a-begin-pt (point))
1133 (forward-line (- a-end a-begin))
1134 (setq a-end-pt (point)
1135 a-prev a-end
1136 a-prev-pt a-end-pt)
1137 (if (eq longlines-mode-val 1)
1138 (longlines-mode longlines-mode-val))
1139 ))
1140 (ediff-with-current-buffer B-buffer
1141 (let ((longlines-mode-val
1142 (if (and (boundp 'longlines-mode) longlines-mode) 1 0)))
1143 (if (eq longlines-mode-val 1)
1144 (longlines-mode 0))
1145 (goto-char (or b-prev-pt shift-B (point-min)))
1146 (forward-line (- b-begin b-prev))
1147 (setq b-begin-pt (point))
1148 (forward-line (- b-end b-begin))
1149 (setq b-end-pt (point)
1150 b-prev b-end
1151 b-prev-pt b-end-pt)
1152 (if (eq longlines-mode-val 1)
1153 (longlines-mode longlines-mode-val))
1154 ))
1155 (ediff-with-current-buffer C-buffer
1156 (let ((longlines-mode-val
1157 (if (and (boundp 'longlines-mode) longlines-mode) 1 0)))
1158 (if (eq longlines-mode-val 1)
1159 (longlines-mode 0))
1160 (goto-char (or c-prev-pt shift-C (point-min)))
1161 (forward-line (- c-begin c-prev))
1162 (setq c-begin-pt (point))
1163 (forward-line (- c-end c-begin))
1164 (setq c-end-pt (point)
1165 c-prev c-end
1166 c-prev-pt c-end-pt)
1167 (if (eq longlines-mode-val 1)
1168 (longlines-mode longlines-mode-val))
1169 ))
1170 (if (ediff-buffer-live-p anc-buffer)
1171 (ediff-with-current-buffer anc-buffer
1172 (let ((longlines-mode-val
1173 (if (and (boundp 'longlines-mode) longlines-mode) 1 0)))
1174 (if (eq longlines-mode-val 1)
1175 (longlines-mode 0))
1176 (forward-line (- c-or-anc-begin anc-prev))
1177 (setq anc-begin-pt (point))
1178 (forward-line (- c-or-anc-end c-or-anc-begin))
1179 (setq anc-end-pt (point)
1180 anc-prev c-or-anc-end)
1181 (if (eq longlines-mode-val 1)
1182 (longlines-mode longlines-mode-val))
1183 )))
1184 (setq diff-list
1185 (nconc
1186 diff-list
1187 ;; if comparing with ancestor, then there also is a
1188 ;; state-of-difference marker
1189 (if three-way-comp
1190 (list (vector
1191 a-begin-pt a-end-pt
1192 b-begin-pt b-end-pt
1193 c-begin-pt c-end-pt
1194 nil nil ; ancestor begin/end
1195 state-of-diff-comparison
1196 nil ; state of merge
1197 nil ; state of ancestor
1198 ))
1199 (list (vector a-begin-pt a-end-pt
1200 b-begin-pt b-end-pt
1201 c-begin-pt c-end-pt
1202 anc-begin-pt anc-end-pt
1203 state-of-diff-merge
1204 state-of-merge
1205 state-of-ancestor
1206 )))
1207 )))
1208 ))
1209
1210 ))) ; end ediff-with-current-buffer
1211 diff-list
1212 ))
1213
1214 ;; Generate the difference vector and overlays for three files
1215 ;; File-C is either the third file to compare (in case of 3-way comparison)
1216 ;; or it is the ancestor file.
1217 (defun ediff-setup-diff-regions3 (file-A file-B file-C)
1218 ;; looking for '-i' or a 'i' among clustered non-long options
1219 (if (string-match "^-i\\| -i\\|\\(^\\| \\)-[^- ]+i" ediff-diff-options)
1220 (error "Option `-i' is not allowed in `ediff-diff3-options'"))
1221
1222 (or (ediff-buffer-live-p ediff-diff-buffer)
1223 (setq ediff-diff-buffer
1224 (get-buffer-create (ediff-unique-buffer-name "*ediff-diff" "*"))))
1225
1226 (message "Computing differences ...")
1227 (ediff-exec-process ediff-diff3-program ediff-diff-buffer 'synchronize
1228 ediff-actual-diff3-options file-A file-B file-C)
1229
1230 (ediff-prepare-error-list ediff-diff3-ok-lines-regexp ediff-diff-buffer)
1231 ;;(message "Computing differences ... done")
1232 (ediff-convert-diffs-to-overlays
1233 (ediff-extract-diffs3
1234 ediff-diff-buffer
1235 ediff-word-mode ediff-3way-comparison-job ediff-narrow-bounds)
1236 ))
1237
1238
1239 ;; Execute PROGRAM asynchronously, unless OS/2, Windows-*, or DOS, or unless
1240 ;; SYNCH is non-nil. BUFFER must be a buffer object, and must be alive. The
1241 ;; OPTIONS arg is a list of options to pass to PROGRAM. It may be a blank
1242 ;; string. All elements in FILES must be strings. We also delete nil from
1243 ;; args.
1244 (defun ediff-exec-process (program buffer synch options &rest files)
1245 (let ((data (match-data))
1246 (coding-system-for-read ediff-coding-system-for-read)
1247 args)
1248 (setq args (append (split-string options) files))
1249 (setq args (delete "" (delq nil args))) ; delete nil and "" from arguments
1250 ;; the --binary option, if present, should be used only for buffer jobs
1251 ;; or for refining the differences
1252 (or (string-match "buffer" (symbol-name ediff-job-name))
1253 (eq buffer ediff-fine-diff-buffer)
1254 (setq args (delete "--binary" args)))
1255 (unwind-protect
1256 (let ((directory default-directory)
1257 proc)
1258 (save-excursion
1259 (set-buffer buffer)
1260 (erase-buffer)
1261 (setq default-directory directory)
1262 (if (or (memq system-type '(emx ms-dos windows-nt windows-95))
1263 synch)
1264 ;; In OS/2 (emx) do it synchronously, since OS/2 doesn't let us
1265 ;; delete files used by other processes. Thus, in ediff-buffers
1266 ;; and similar functions, we can't delete temp files because
1267 ;; they might be used by the asynch process that computes
1268 ;; custom diffs. So, we have to wait till custom diff
1269 ;; subprocess is done.
1270 ;; Similarly for Windows-*
1271 ;; In DOS, must synchronize because DOS doesn't have
1272 ;; asynchronous processes.
1273 (apply 'call-process program nil buffer nil args)
1274 ;; On other systems, do it asynchronously.
1275 (setq proc (get-buffer-process buffer))
1276 (if proc (kill-process proc))
1277 (setq proc
1278 (apply 'start-process "Custom Diff" buffer program args))
1279 (setq mode-line-process '(":%s"))
1280 (set-process-sentinel proc 'ediff-process-sentinel)
1281 (set-process-filter proc 'ediff-process-filter)
1282 )))
1283 (store-match-data data))))
1284
1285 ;; This is shell-command-filter from simple.el in Emacs.
1286 ;; Copied here because XEmacs doesn't have it.
1287 (defun ediff-process-filter (proc string)
1288 ;; Do save-excursion by hand so that we can leave point numerically unchanged
1289 ;; despite an insertion immediately after it.
1290 (let* ((obuf (current-buffer))
1291 (buffer (process-buffer proc))
1292 opoint
1293 (window (get-buffer-window buffer))
1294 (pos (window-start window)))
1295 (unwind-protect
1296 (progn
1297 (set-buffer buffer)
1298 (or (= (point) (point-max))
1299 (setq opoint (point)))
1300 (goto-char (point-max))
1301 (insert-before-markers string))
1302 ;; insert-before-markers moved this marker: set it back.
1303 (set-window-start window pos)
1304 ;; Finish our save-excursion.
1305 (if opoint
1306 (goto-char opoint))
1307 (set-buffer obuf))))
1308
1309 ;; like shell-command-sentinel but doesn't print an exit status message
1310 ;; we do this because diff always exits with status 1, if diffs are found
1311 ;; so shell-command-sentinel displays a confusing message to the user
1312 (defun ediff-process-sentinel (process signal)
1313 (if (and (memq (process-status process) '(exit signal))
1314 (buffer-name (process-buffer process)))
1315 (progn
1316 (save-excursion
1317 (set-buffer (process-buffer process))
1318 (setq mode-line-process nil))
1319 (delete-process process))))
1320
1321
1322 ;;; Word functions used to refine the current diff
1323
1324 (defvar ediff-forward-word-function 'ediff-forward-word
1325 "*Function to call to move to the next word.
1326 Used for splitting difference regions into individual words.")
1327 (make-variable-buffer-local 'ediff-forward-word-function)
1328
1329 (defvar ediff-whitespace " \n\t\f"
1330 "*Characters constituting white space.
1331 These characters are ignored when differing regions are split into words.")
1332 (make-variable-buffer-local 'ediff-whitespace)
1333
1334 (defvar ediff-word-1
1335 (ediff-cond-compile-for-xemacs-or-emacs "a-zA-Z---_" "-[:word:]_")
1336 "*Characters that constitute words of type 1.
1337 More precisely, [ediff-word-1] is a regexp that matches type 1 words.
1338 See `ediff-forward-word' for more details.")
1339 (make-variable-buffer-local 'ediff-word-1)
1340
1341 (defvar ediff-word-2 "0-9.,"
1342 "*Characters that constitute words of type 2.
1343 More precisely, [ediff-word-2] is a regexp that matches type 2 words.
1344 See `ediff-forward-word' for more details.")
1345 (make-variable-buffer-local 'ediff-word-2)
1346
1347 (defvar ediff-word-3 "`'?!:;\"{}[]()"
1348 "*Characters that constitute words of type 3.
1349 More precisely, [ediff-word-3] is a regexp that matches type 3 words.
1350 See `ediff-forward-word' for more details.")
1351 (make-variable-buffer-local 'ediff-word-3)
1352
1353 (defvar ediff-word-4
1354 (concat "^" ediff-word-1 ediff-word-2 ediff-word-3 ediff-whitespace)
1355 "*Characters that constitute words of type 4.
1356 More precisely, [ediff-word-4] is a regexp that matches type 4 words.
1357 See `ediff-forward-word' for more details.")
1358 (make-variable-buffer-local 'ediff-word-4)
1359
1360 ;; Split region along word boundaries. Each word will be on its own line.
1361 ;; Output to buffer out-buffer.
1362 (defun ediff-forward-word ()
1363 "Move point one word forward.
1364 There are four types of words, each of which consists entirely of
1365 characters in `ediff-word-1', `ediff-word-2', `ediff-word-3', or
1366 `ediff-word-4'. Words are recognized by passing these one after another as
1367 arguments to `skip-chars-forward'."
1368 (or (> (+ (skip-chars-forward ediff-word-1)
1369 (skip-syntax-forward "w"))
1370 0)
1371 (> (skip-chars-forward ediff-word-2) 0)
1372 (> (skip-chars-forward ediff-word-3) 0)
1373 (> (skip-chars-forward ediff-word-4) 0)
1374 ))
1375
1376
1377 (defun ediff-wordify (beg end in-buffer out-buffer &optional control-buf)
1378 (let ((forward-word-function
1379 ;; eval in control buf to let user create local versions for
1380 ;; different invocations
1381 (if control-buf
1382 (ediff-with-current-buffer control-buf
1383 ediff-forward-word-function)
1384 ediff-forward-word-function))
1385 inbuf-syntax-tbl sv-point diff-string)
1386 (save-excursion
1387 (set-buffer in-buffer)
1388 (setq inbuf-syntax-tbl
1389 (if control-buf
1390 (ediff-with-current-buffer control-buf
1391 ediff-syntax-table)
1392 (syntax-table)))
1393 (setq diff-string (buffer-substring-no-properties beg end))
1394
1395 (set-buffer out-buffer)
1396 ;; Make sure that temp buff syntax table is the same as the original buf
1397 ;; syntax tbl, because we use ediff-forward-word in both and
1398 ;; ediff-forward-word depends on the syntax classes of characters.
1399 (set-syntax-table inbuf-syntax-tbl)
1400 (erase-buffer)
1401 (insert diff-string)
1402 (goto-char (point-min))
1403 (skip-chars-forward ediff-whitespace)
1404 (delete-region (point-min) (point))
1405
1406 (while (not (eobp))
1407 (funcall forward-word-function)
1408 (setq sv-point (point))
1409 (skip-chars-forward ediff-whitespace)
1410 (delete-region sv-point (point))
1411 (insert "\n")))))
1412
1413 ;; copy string specified as BEG END from IN-BUF to OUT-BUF
1414 (defun ediff-copy-to-buffer (beg end in-buffer out-buffer)
1415 (with-current-buffer out-buffer
1416 (erase-buffer)
1417 (insert-buffer-substring in-buffer beg end)
1418 (goto-char (point-min))))
1419
1420
1421 ;; goto word #n starting at current position in buffer `buf'
1422 ;; For ediff, a word is determined by ediff-forward-word-function
1423 ;; If `flag' is non-nil, goto the end of the n-th word.
1424 (defun ediff-goto-word (n buf &optional flag)
1425 ;; remember val ediff-forward-word-function has in ctl buf
1426 (let ((fwd-word-fun ediff-forward-word-function)
1427 (syntax-tbl ediff-syntax-table))
1428 (ediff-with-current-buffer buf
1429 (skip-chars-forward ediff-whitespace)
1430 (ediff-with-syntax-table syntax-tbl
1431 (while (> n 1)
1432 (funcall fwd-word-fun)
1433 (skip-chars-forward ediff-whitespace)
1434 (setq n (1- n)))
1435 (if (and flag (> n 0))
1436 (funcall fwd-word-fun)))
1437 (point))))
1438
1439 (defun ediff-same-file-contents (f1 f2)
1440 "Return t if files F1 and F2 have identical contents."
1441 (if (and (not (file-directory-p f1))
1442 (not (file-directory-p f2)))
1443 (let ((res
1444 (apply 'call-process ediff-cmp-program nil nil nil
1445 (append ediff-cmp-options (list f1 f2)))))
1446 (and (numberp res) (eq res 0))))
1447 )
1448
1449
1450 (defun ediff-same-contents (d1 d2 &optional filter-re)
1451 "Returns t iff D1 and D2 have the same content.
1452 D1 and D2 can either be both directories or both regular files.
1453 Symlinks and the likes are not handled.
1454 If FILTER-RE is non-nil, recursive checking in directories
1455 affects only files whose names match the expression."
1456 ;; Normalize empty filter RE to nil.
1457 (unless (> (length filter-re) 0) (setq filter-re nil))
1458 ;; Indicate progress
1459 (message "Comparing '%s' and '%s' modulo '%s'" d1 d2 filter-re)
1460 (cond
1461 ;; D1 & D2 directories => recurse
1462 ((and (file-directory-p d1)
1463 (file-directory-p d2))
1464 (if (null ediff-recurse-to-subdirectories)
1465 (if (y-or-n-p "Compare subdirectories recursively? ")
1466 (setq ediff-recurse-to-subdirectories 'yes)
1467 (setq ediff-recurse-to-subdirectories 'no)))
1468 (if (eq ediff-recurse-to-subdirectories 'yes)
1469 (let* ((all-entries-1 (directory-files d1 t filter-re))
1470 (all-entries-2 (directory-files d2 t filter-re))
1471 (entries-1 (ediff-delete-all-matches "^\\.\\.?$" all-entries-1))
1472 (entries-2 (ediff-delete-all-matches "^\\.\\.?$" all-entries-2))
1473 )
1474
1475 (ediff-same-file-contents-lists entries-1 entries-2 filter-re)
1476 ))
1477 ) ; end of the directories case
1478 ;; D1 & D2 are both files => compare directly
1479 ((and (file-regular-p d1)
1480 (file-regular-p d2))
1481 (ediff-same-file-contents d1 d2))
1482 ;; Otherwise => false: unequal contents
1483 )
1484 )
1485
1486 ;; If lists have the same length and names of files are pairwise equal
1487 ;; (removing the directories) then compare contents pairwise.
1488 ;; True if all contents are the same; false otherwise
1489 (defun ediff-same-file-contents-lists (entries-1 entries-2 filter-re)
1490 ;; First, check only the names (works quickly and ensures a
1491 ;; precondition for subsequent code)
1492 (if (and (= (length entries-1) (length entries-2))
1493 (equal (mapcar 'file-name-nondirectory entries-1)
1494 (mapcar 'file-name-nondirectory entries-2)))
1495 ;; With name equality established, compare the entries
1496 ;; through recursion.
1497 (let ((continue t))
1498 (while (and entries-1 continue)
1499 (if (ediff-same-contents
1500 (car entries-1) (car entries-2) filter-re)
1501 (setq entries-1 (cdr entries-1)
1502 entries-2 (cdr entries-2))
1503 (setq continue nil))
1504 )
1505 ;; if reached the end then lists are equal
1506 (null entries-1))
1507 )
1508 )
1509
1510
1511 ;; ARG1 is a regexp, ARG2 is a list of full-filenames
1512 ;; Delete all entries that match the regexp
1513 (defun ediff-delete-all-matches (regex file-list-list)
1514 (let (result elt)
1515 (while file-list-list
1516 (setq elt (car file-list-list))
1517 (or (string-match regex (file-name-nondirectory elt))
1518 (setq result (cons elt result)))
1519 (setq file-list-list (cdr file-list-list)))
1520 (reverse result)))
1521
1522 ;; Ignore case handling - some ideas from drew.adams@@oracle.com
1523 (defun ediff-toggle-ignore-case ()
1524 (interactive)
1525 (ediff-barf-if-not-control-buffer)
1526 (setq ediff-ignore-case (not ediff-ignore-case))
1527 (cond (ediff-ignore-case
1528 (setq ediff-actual-diff-options
1529 (concat ediff-diff-options " " ediff-ignore-case-option)
1530 ediff-actual-diff3-options
1531 (concat ediff-diff3-options " " ediff-ignore-case-option3))
1532 (message "Ignoring regions that differ only in case"))
1533 (t
1534 (setq ediff-actual-diff-options ediff-diff-options
1535 ediff-actual-diff3-options ediff-diff3-options)
1536 (message "Ignoring case differences turned OFF")))
1537 (cond (ediff-merge-job
1538 (message "Ignoring letter case is too dangerous in merge jobs"))
1539 ((and ediff-diff3-job (string= ediff-ignore-case-option3 ""))
1540 (message "Ignoring letter case is not supported by this diff3 program"))
1541 ((and (not ediff-3way-job) (string= ediff-ignore-case-option ""))
1542 (message "Ignoring letter case is not supported by this diff program"))
1543 (t
1544 (sit-for 1)
1545 (ediff-update-diffs)))
1546 )
1547
1548
1549 (provide 'ediff-diff)
1550
1551
1552 ;;; Local Variables:
1553 ;;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
1554 ;;; eval: (put 'ediff-with-current-buffer 'lisp-indent-hook 1)
1555 ;;; eval: (put 'ediff-with-current-buffer 'edebug-form-spec '(form body))
1556 ;;; End:
1557
1558 ;;; arch-tag: a86d448e-58d7-4572-a1d9-fdedfa22f648
1559 ;;; ediff-diff.el ends here