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