1 ;;; ediff.el --- a comprehensive visual interface to diff & patch
3 ;; Copyright (C) 1994, 95, 96, 97, 98, 99, 2000 Free Software Foundation, Inc.
5 ;; Author: Michael Kifer <kifer@cs.sunysb.edu>
6 ;; Created: February 2, 1994
7 ;; Keywords: comparing, merging, patching, version control.
9 (defconst ediff-version
"2.74" "The current version of Ediff")
10 (defconst ediff-date
"October 31, 1999" "Date of last update")
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 ;; Boston, MA 02111-1307, USA.
32 ;; Never read that diff output again!
33 ;; Apply patch interactively!
36 ;; This package provides a convenient way of simultaneous browsing through
37 ;; the differences between a pair (or a triple) of files or buffers. The
38 ;; files being compared, file-A, file-B, and file-C (if applicable) are
39 ;; shown in separate windows (side by side, one above the another, or in
40 ;; separate frames), and the differences are highlighted as you step
41 ;; through them. You can also copy difference regions from one buffer to
42 ;; another (and recover old differences if you change your mind).
44 ;; Ediff also supports merging operations on files and buffers, including
45 ;; merging using ancestor versions. Both comparison and merging operations can
46 ;; be performed on directories, i.e., by pairwise comparison of files in those
49 ;; In addition, Ediff can apply a patch to a file and then let you step
50 ;; though both files, the patched and the original one, simultaneously,
51 ;; difference-by-difference. You can even apply a patch right out of a
52 ;; mail buffer, i.e., patches received by mail don't even have to be saved.
53 ;; Since Ediff lets you copy differences between buffers, you can, in
54 ;; effect, apply patches selectively (i.e., you can copy a difference
55 ;; region from file_orig to file, thereby undoing any particular patch that
58 ;; Ediff is aware of version control, which lets the user compare
59 ;; files with their older versions. Ediff can also work with remote and
60 ;; compressed files. Details are given below.
62 ;; Finally, Ediff supports directory-level comparison, merging and patching.
63 ;; See the on-line manual for details.
65 ;; This package builds upon the ideas borrowed from emerge.el and several
66 ;; Ediff's functions are adaptations from emerge.el. Much of the functionality
67 ;; Ediff provides is also influenced by emerge.el.
69 ;; The present version of Ediff supersedes Emerge. It provides a superior user
70 ;; interface and has numerous major features not found in Emerge. In
71 ;; particular, it can do patching, and 2-way and 3-way file comparison,
72 ;; merging, and directory operations.
78 ;; 1. The undo command doesn't restore deleted regions well. That is, if
79 ;; you delete all characters in a difference region and then invoke
80 ;; `undo', the reinstated text will most likely be inserted outside of
81 ;; what Ediff thinks is the current difference region. (This problem
82 ;; doesn't seem to exist with XEmacs.)
84 ;; If at any point you feel that difference regions are no longer correct,
85 ;; you can hit '!' to recompute the differences.
87 ;; 2. On a monochrome display, the repertoire of faces with which to
88 ;; highlight fine differences is limited. By default, Ediff is using
89 ;; underlining. However, if the region is already underlined by some other
90 ;; overlays, there is no simple way to temporarily remove that residual
91 ;; underlining. This problem occurs when a buffer is highlighted with
92 ;; hilit19.el or font-lock.el packages. If this residual highlighting gets
93 ;; in the way, you can do the following. Both font-lock.el and hilit19.el
94 ;; provide commands for unhighlighting buffers. You can either place these
95 ;; commands in `ediff-prepare-buffer-hook' (which will unhighlight every
96 ;; buffer used by Ediff) or you can execute them interactively, at any time
100 ;;; Acknowledgements:
102 ;; Ediff was inspired by Dale R. Worley's <drw@math.mit.edu> emerge.el.
103 ;; Ediff would not have been possible without the help and encouragement of
104 ;; its many users. See Ediff on-line Info for the full list of those who
105 ;; helped. Improved defaults in Ediff file-name reading commands.
112 (defvar cvs-cookie-handle
)
113 (defvar ediff-last-dir-patch
)
114 (defvar ediff-patch-default-directory
)
118 (load-library "dired")
119 (load-library "info")
120 (load "pcl-cvs" 'noerror
)))
122 (let ((load-path (cons (expand-file-name ".") load-path
)))
123 (or (featurep 'ediff-init
)
124 (load "ediff-init.el" nil nil
'nosuffix
))
125 (or (featurep 'ediff-mult
)
126 (load "ediff-mult.el" nil nil
'nosuffix
))
127 (or (featurep 'ediff-ptch
)
128 (load "ediff-ptch.el" nil nil
'nosuffix
))
129 (or (featurep 'ediff-vers
)
130 (load "ediff-vers.el" nil nil
'nosuffix
))
134 (require 'ediff-init
)
135 (require 'ediff-mult
) ; required because of the registry stuff
138 "A comprehensive visual interface to diff & patch"
143 (defcustom ediff-use-last-dir nil
144 "*If t, Ediff will use previous directory as default when reading file name."
148 ;; Last directory used by an Ediff command for file-A.
149 (defvar ediff-last-dir-A nil
)
150 ;; Last directory used by an Ediff command for file-B.
151 (defvar ediff-last-dir-B nil
)
152 ;; Last directory used by an Ediff command for file-C.
153 (defvar ediff-last-dir-C nil
)
154 ;; Last directory used by an Ediff command for the ancestor file.
155 (defvar ediff-last-dir-ancestor nil
)
156 ;; Last directory used by an Ediff command as the output directory for merge.
157 (defvar ediff-last-merge-autostore-dir
)
160 ;; Used as a startup hook to set `_orig' patch file read-only.
161 (defun ediff-set-read-only-in-buf-A ()
162 (ediff-with-current-buffer ediff-buffer-A
163 (toggle-read-only 1)))
165 ;; Return a plausible default for ediff's first file:
166 ;; In dired, return the file name under the point, unless it is a directory
167 ;; If the buffer has a file name, return that file name.
168 (defun ediff-get-default-file-name ()
169 (cond ((eq major-mode
'dired-mode
)
170 (let ((f (dired-get-filename nil
'no-error
)))
171 (if (and (stringp f
) (not (file-directory-p f
)))
173 ((buffer-file-name (current-buffer))
174 (file-name-nondirectory (buffer-file-name (current-buffer))))
177 ;;; Compare files/buffers
180 (defun ediff-files (file-A file-B
&optional startup-hooks
)
181 "Run Ediff on a pair of files, FILE-A and FILE-B."
183 (let ((dir-A (if ediff-use-last-dir
187 (list (setq f
(ediff-read-file-name
188 "File A to compare" dir-A
189 (ediff-get-default-file-name)))
190 (ediff-read-file-name "File B to compare"
192 (if ediff-use-last-dir
194 (file-name-directory f
)))
196 (setq file-name-history
197 (cons (ediff-abbreviate-file-name
199 (file-name-nondirectory f
)
204 (ediff-files-internal file-A
205 (if (file-directory-p file-B
)
207 (file-name-nondirectory file-A
) file-B
)
214 (defun ediff-files3 (file-A file-B file-C
&optional startup-hooks
)
215 "Run Ediff on three files, FILE-A, FILE-B, and FILE-C."
217 (let ((dir-A (if ediff-use-last-dir
221 (list (setq f
(ediff-read-file-name
222 "File A to compare" dir-A
223 (ediff-get-default-file-name)))
224 (setq ff
(ediff-read-file-name "File B to compare"
226 (if ediff-use-last-dir
228 (file-name-directory f
)))
230 (setq file-name-history
232 (ediff-abbreviate-file-name
234 (file-name-nondirectory f
)
238 (ediff-read-file-name "File C to compare"
239 (setq dir-C
(if ediff-use-last-dir
241 (file-name-directory ff
)))
243 (setq file-name-history
244 (cons (ediff-abbreviate-file-name
246 (file-name-nondirectory ff
)
251 (ediff-files-internal file-A
252 (if (file-directory-p file-B
)
254 (file-name-nondirectory file-A
) file-B
)
256 (if (file-directory-p file-C
)
258 (file-name-nondirectory file-A
) file-C
)
264 (defalias 'ediff3
'ediff-files3
)
267 ;; Visit FILE and arrange its buffer to Ediff's liking.
268 ;; FILE is actually a variable symbol that must contain a true file name.
269 ;; BUFFER-NAME is a variable symbol, which will get the buffer object into
270 ;; which FILE is read.
271 ;; LAST-DIR is the directory variable symbol where FILE's
272 ;; directory name should be returned. HOOKS-VAR is a variable symbol that will
273 ;; be assigned the hook to be executed after `ediff-startup' is finished.
274 ;; `ediff-find-file' arranges that the temp files it might create will be
276 (defun ediff-find-file (file-var buffer-name
&optional last-dir hooks-var
)
277 (let* ((file (symbol-value file-var
))
278 (file-magic (ediff-filename-magic-p file
))
279 (temp-file-name-prefix (file-name-nondirectory file
)))
280 (cond ((not (file-readable-p file
))
281 (error "File `%s' does not exist or is not readable" file
))
282 ((file-directory-p file
)
283 (error "File `%s' is a directory" file
)))
285 ;; some of the commands, below, require full file name
286 (setq file
(expand-file-name file
))
288 ;; Record the directory of the file
290 (set last-dir
(expand-file-name (file-name-directory file
))))
293 (set buffer-name
(find-file-noselect file
))
295 (ediff-with-current-buffer (symbol-value buffer-name
)
296 (widen) ; Make sure the entire file is seen
297 (cond (file-magic ; file has a handler, such as jka-compr-handler or
298 ;;; ange-ftp-hook-function--arrange for temp file
299 (ediff-verify-file-buffer 'magic
)
301 (ediff-make-temp-file
302 (current-buffer) temp-file-name-prefix
))
303 (set hooks-var
(cons `(lambda () (delete-file ,file
))
304 (symbol-value hooks-var
))))
305 ;; file processed via auto-mode-alist, a la uncompress.el
306 ((not (equal (file-truename file
)
307 (file-truename (buffer-file-name))))
309 (ediff-make-temp-file
310 (current-buffer) temp-file-name-prefix
))
311 (set hooks-var
(cons `(lambda () (delete-file ,file
))
312 (symbol-value hooks-var
))))
313 (t ;; plain file---just check that the file matches the buffer
314 (ediff-verify-file-buffer))))
315 (set file-var file
)))
317 ;; MERGE-BUFFER-FILE is the file to be associated with the merge buffer
318 (defun ediff-files-internal (file-A file-B file-C startup-hooks job-name
319 &optional merge-buffer-file
)
320 (let (buf-A buf-B buf-C
)
321 (message "Reading file %s ... " file-A
)
323 (ediff-find-file 'file-A
'buf-A
'ediff-last-dir-A
'startup-hooks
)
324 (message "Reading file %s ... " file-B
)
326 (ediff-find-file 'file-B
'buf-B
'ediff-last-dir-B
'startup-hooks
)
329 (message "Reading file %s ... " file-C
)
333 (if (eq job-name
'ediff-merge-files-with-ancestor
)
334 'ediff-last-dir-ancestor
'ediff-last-dir-C
)
336 (ediff-setup buf-A file-A
340 (list (cons 'ediff-job-name job-name
))
345 (defalias 'ediff
'ediff-files
)
349 (defun ediff-buffers (buffer-A buffer-B
&optional startup-hooks job-name
)
350 "Run Ediff on a pair of buffers, BUFFER-A and BUFFER-B."
353 (list (setq bf
(read-buffer "Buffer A to compare: "
354 (ediff-other-buffer "") t
))
355 (read-buffer "Buffer B to compare: "
357 ;; realign buffers so that two visible bufs will be
359 (save-window-excursion (other-window 1))
360 (ediff-other-buffer bf
))
362 (or job-name
(setq job-name
'ediff-buffers
))
363 (ediff-buffers-internal buffer-A buffer-B nil startup-hooks job-name
))
366 (defalias 'ebuffers
'ediff-buffers
)
370 (defun ediff-buffers3 (buffer-A buffer-B buffer-C
371 &optional startup-hooks job-name
)
372 "Run Ediff on three buffers, BUFFER-A, BUFFER-B, and BUFFER-C."
375 (list (setq bf
(read-buffer "Buffer A to compare: "
376 (ediff-other-buffer "") t
))
377 (setq bff
(read-buffer "Buffer B to compare: "
379 ;; realign buffers so that two visible
380 ;; bufs will be at the top
381 (save-window-excursion (other-window 1))
382 (ediff-other-buffer bf
))
384 (read-buffer "Buffer C to compare: "
386 ;; realign buffers so that three visible
387 ;; bufs will be at the top
388 (save-window-excursion (other-window 1))
389 (ediff-other-buffer (list bf bff
)))
392 (or job-name
(setq job-name
'ediff-buffers3
))
393 (ediff-buffers-internal buffer-A buffer-B buffer-C startup-hooks job-name
))
396 (defalias 'ebuffers3
'ediff-buffers3
)
400 ;; MERGE-BUFFER-FILE is the file to be associated with the merge buffer
401 (defun ediff-buffers-internal (buf-A buf-B buf-C startup-hooks job-name
402 &optional merge-buffer-file
)
403 (let* ((buf-A-file-name (buffer-file-name (get-buffer buf-A
)))
404 (buf-B-file-name (buffer-file-name (get-buffer buf-B
)))
405 (buf-C-is-alive (ediff-buffer-live-p buf-C
))
406 (buf-C-file-name (if buf-C-is-alive
407 (buffer-file-name (get-buffer buf-B
))))
408 file-A file-B file-C
)
409 (if (not (ediff-buffer-live-p buf-A
))
410 (error "Buffer %S doesn't exist" buf-A
))
411 (if (not (ediff-buffer-live-p buf-B
))
412 (error "Buffer %S doesn't exist" buf-B
))
413 (let ((ediff-job-name job-name
))
414 (if (and ediff-3way-comparison-job
415 (not buf-C-is-alive
))
416 (error "Buffer %S doesn't exist" buf-C
)))
417 (if (stringp buf-A-file-name
)
418 (setq buf-A-file-name
(file-name-nondirectory buf-A-file-name
)))
419 (if (stringp buf-B-file-name
)
420 (setq buf-B-file-name
(file-name-nondirectory buf-B-file-name
)))
421 (if (stringp buf-C-file-name
)
422 (setq buf-C-file-name
(file-name-nondirectory buf-C-file-name
)))
424 (setq file-A
(ediff-make-temp-file buf-A buf-A-file-name
)
425 file-B
(ediff-make-temp-file buf-B buf-B-file-name
))
427 (setq file-C
(ediff-make-temp-file buf-C buf-C-file-name
)))
429 (ediff-setup (get-buffer buf-A
) file-A
430 (get-buffer buf-B
) file-B
431 (if buf-C-is-alive
(get-buffer buf-C
))
434 (delete-file ,file-A
)
435 (delete-file ,file-B
)
436 (if (stringp ,file-C
) (delete-file ,file-C
)))
438 (list (cons 'ediff-job-name job-name
))
442 ;;; Directory and file group operations
444 ;; Get appropriate default name for directory:
445 ;; If ediff-use-last-dir, use ediff-last-dir-A.
446 ;; In dired mode, use the directory that is under the point (if any);
447 ;; otherwise, use default-directory
448 (defun ediff-get-default-directory-name ()
449 (cond (ediff-use-last-dir ediff-last-dir-A
)
450 ((eq major-mode
'dired-mode
)
451 (let ((f (dired-get-filename nil
'noerror
)))
452 (if (and (stringp f
) (file-directory-p f
))
455 (t default-directory
)))
459 (defun ediff-directories (dir1 dir2 regexp
)
460 "Run Ediff on a pair of directories, DIR1 and DIR2, comparing files that have
461 the same name in both. The third argument, REGEXP, is a regular expression
462 that can be used to filter out certain file names."
464 (let ((dir-A (ediff-get-default-directory-name))
466 (list (setq f
(ediff-read-file-name "Directory A to compare:" dir-A nil
))
467 (ediff-read-file-name "Directory B to compare:"
468 (if ediff-use-last-dir
470 (ediff-strip-last-dir f
))
472 (read-string "Filter through regular expression: "
473 nil
'ediff-filtering-regexp-history
)
475 (ediff-directories-internal
476 dir1 dir2 nil regexp
'ediff-files
'ediff-directories
480 (defalias 'edirs
'ediff-directories
)
484 (defun ediff-directory-revisions (dir1 regexp
)
485 "Run Ediff on a directory, DIR1, comparing its files with their revisions.
486 The second argument, REGEXP, is a regular expression that filters the file
487 names. Only the files that are under revision control are taken into account."
489 (let ((dir-A (ediff-get-default-directory-name)))
490 (list (ediff-read-file-name
491 "Directory to compare with revision:" dir-A nil
)
492 (read-string "Filter through regular expression: "
493 nil
'ediff-filtering-regexp-history
)
495 (ediff-directory-revisions-internal
496 dir1 regexp
'ediff-revision
'ediff-directory-revisions
500 (defalias 'edir-revisions
'ediff-directory-revisions
)
504 (defun ediff-directories3 (dir1 dir2 dir3 regexp
)
505 "Run Ediff on three directories, DIR1, DIR2, and DIR3, comparing files that
506 have the same name in all three. The last argument, REGEXP, is a regular
507 expression that can be used to filter out certain file names."
509 (let ((dir-A (ediff-get-default-directory-name))
511 (list (setq f
(ediff-read-file-name "Directory A to compare:" dir-A nil
))
512 (setq f
(ediff-read-file-name "Directory B to compare:"
513 (if ediff-use-last-dir
515 (ediff-strip-last-dir f
))
517 (ediff-read-file-name "Directory C to compare:"
518 (if ediff-use-last-dir
520 (ediff-strip-last-dir f
))
522 (read-string "Filter through regular expression: "
523 nil
'ediff-filtering-regexp-history
)
525 (ediff-directories-internal
526 dir1 dir2 dir3 regexp
'ediff-files3
'ediff-directories3
530 (defalias 'edirs3
'ediff-directories3
)
533 (defun ediff-merge-directories (dir1 dir2 regexp
&optional merge-autostore-dir
)
534 "Run Ediff on a pair of directories, DIR1 and DIR2, merging files that have
535 the same name in both. The third argument, REGEXP, is a regular expression
536 that can be used to filter out certain file names."
538 (let ((dir-A (ediff-get-default-directory-name))
540 (list (setq f
(ediff-read-file-name "Directory A to merge:" dir-A nil
))
541 (ediff-read-file-name "Directory B to merge:"
542 (if ediff-use-last-dir
544 (ediff-strip-last-dir f
))
546 (read-string "Filter through regular expression: "
547 nil
'ediff-filtering-regexp-history
)
549 (ediff-directories-internal
550 dir1 dir2 nil regexp
'ediff-merge-files
'ediff-merge-directories
551 nil merge-autostore-dir
555 (defalias 'edirs-merge
'ediff-merge-directories
)
558 (defun ediff-merge-directories-with-ancestor (dir1 dir2 ancestor-dir regexp
561 "Merge files in directories DIR1 and DIR2 using files in ANCESTOR-DIR as ancestors.
562 Ediff merges files that have identical names in DIR1, DIR2. If a pair of files
563 in DIR1 and DIR2 doesn't have an ancestor in ANCESTOR-DIR, Ediff will merge
564 without ancestor. The fourth argument, REGEXP, is a regular expression that
565 can be used to filter out certain file names."
567 (let ((dir-A (ediff-get-default-directory-name))
569 (list (setq f
(ediff-read-file-name "Directory A to merge:" dir-A nil
))
570 (setq f
(ediff-read-file-name "Directory B to merge:"
571 (if ediff-use-last-dir
573 (ediff-strip-last-dir f
))
575 (ediff-read-file-name "Ancestor directory:"
576 (if ediff-use-last-dir
578 (ediff-strip-last-dir f
))
580 (read-string "Filter through regular expression: "
581 nil
'ediff-filtering-regexp-history
)
583 (ediff-directories-internal
584 dir1 dir2 ancestor-dir regexp
585 'ediff-merge-files-with-ancestor
'ediff-merge-directories-with-ancestor
586 nil merge-autostore-dir
590 (defun ediff-merge-directory-revisions (dir1 regexp
591 &optional merge-autostore-dir
)
592 "Run Ediff on a directory, DIR1, merging its files with their revisions.
593 The second argument, REGEXP, is a regular expression that filters the file
594 names. Only the files that are under revision control are taken into account."
596 (let ((dir-A (ediff-get-default-directory-name)))
597 (list (ediff-read-file-name
598 "Directory to merge with revisions:" dir-A nil
)
599 (read-string "Filter through regular expression: "
600 nil
'ediff-filtering-regexp-history
)
602 (ediff-directory-revisions-internal
603 dir1 regexp
'ediff-merge-revisions
'ediff-merge-directory-revisions
604 nil merge-autostore-dir
608 (defalias 'edir-merge-revisions
'ediff-merge-directory-revisions
)
611 (defun ediff-merge-directory-revisions-with-ancestor (dir1 regexp
614 "Run Ediff on a directory, DIR1, merging its files with their revisions and ancestors.
615 The second argument, REGEXP, is a regular expression that filters the file
616 names. Only the files that are under revision control are taken into account."
618 (let ((dir-A (ediff-get-default-directory-name)))
619 (list (ediff-read-file-name
620 "Directory to merge with revisions and ancestors:" dir-A nil
)
621 (read-string "Filter through regular expression: "
622 nil
'ediff-filtering-regexp-history
)
624 (ediff-directory-revisions-internal
625 dir1 regexp
'ediff-merge-revisions-with-ancestor
626 'ediff-merge-directory-revisions-with-ancestor
627 nil merge-autostore-dir
632 'edir-merge-revisions-with-ancestor
633 'ediff-merge-directory-revisions-with-ancestor
)
636 (defalias 'edirs-merge-with-ancestor
'ediff-merge-directories-with-ancestor
)
638 ;; Run ediff-action (ediff-files, ediff-merge, ediff-merge-with-ancestors)
639 ;; on a pair of directories (three directories, in case of ancestor).
640 ;; The third argument, REGEXP, is a regular expression that can be used to
641 ;; filter out certain file names.
642 ;; JOBNAME is the symbol indicating the meta-job to be performed.
643 ;; MERGE-AUTOSTORE-DIR is the directory in which to store merged files.
644 (defun ediff-directories-internal (dir1 dir2 dir3 regexp action jobname
645 &optional startup-hooks
647 ;; ediff-read-file-name is set to attach a previously entered file name if
648 ;; the currently entered file is a directory. This code takes care of that.
649 (setq dir1
(if (file-directory-p dir1
) dir1
(file-name-directory dir1
))
650 dir2
(if (file-directory-p dir2
) dir2
(file-name-directory dir2
)))
653 (setq dir3
(if (file-directory-p dir3
) dir3
(file-name-directory dir3
))))
655 (cond ((string= dir1 dir2
)
656 (error "Directories A and B are the same: %s" dir1
))
657 ((and (eq jobname
'ediff-directories3
)
659 (error "Directories A and C are the same: %s" dir1
))
660 ((and (eq jobname
'ediff-directories3
)
662 (error "Directories B and C are the same: %s" dir1
)))
664 (if merge-autostore-dir
665 (or (stringp merge-autostore-dir
)
666 (error "%s: Directory for storing merged files must be a string"
668 (let (diffs ; var where ediff-intersect-directories returns the diff list
670 (if (and ediff-autostore-merges
671 (ediff-merge-metajob jobname
)
672 (not merge-autostore-dir
))
673 (setq merge-autostore-dir
674 (read-file-name "Save merged files in directory: "
675 (if ediff-use-last-dir
676 ediff-last-merge-autostore-dir
677 (ediff-strip-last-dir dir1
))
680 ;; verify we are not merging into an orig directory
681 (if merge-autostore-dir
682 (cond ((and (stringp dir1
) (string= merge-autostore-dir dir1
))
684 "Directory for saving merged files = Directory A. Sure? ")
685 (error "Directory merge aborted")))
686 ((and (stringp dir2
) (string= merge-autostore-dir dir2
))
688 "Directory for saving merged files = Directory B. Sure? ")
689 (error "Directory merge aborted")))
690 ((and (stringp dir3
) (string= merge-autostore-dir dir3
))
692 "Directory for saving merged files = Ancestor Directory. Sure? ")
693 (error "Directory merge aborted")))))
695 (setq file-list
(ediff-intersect-directories
697 regexp dir1 dir2 dir3 merge-autostore-dir
))
699 ;; this sets various vars in the meta buffer inside
700 ;; ediff-prepare-meta-buffer
702 ;; tell what to do if the user clicks on a session record
703 (setq ediff-session-action-function
(quote ,action
))
704 ;; set ediff-dir-difference-list
705 (setq ediff-dir-difference-list
(quote ,diffs
)))
707 (setq meta-buf
(ediff-prepare-meta-buffer
708 'ediff-filegroup-action
710 "*Ediff Session Group Panel"
711 'ediff-redraw-directory-group-buffer
714 (ediff-show-meta-buffer meta-buf
)
717 ;; MERGE-AUTOSTORE-DIR can be given to tell ediff where to store the merged
719 (defun ediff-directory-revisions-internal (dir1 regexp action jobname
720 &optional startup-hooks
722 (setq dir1
(if (file-directory-p dir1
) dir1
(file-name-directory dir1
)))
724 (if merge-autostore-dir
725 (or (stringp merge-autostore-dir
)
726 (error "%S: Directory for storing merged files must be a string"
728 (let (file-list meta-buf
)
729 (if (and ediff-autostore-merges
730 (ediff-merge-metajob jobname
)
731 (not merge-autostore-dir
))
732 (setq merge-autostore-dir
733 (read-file-name "Save merged files in directory: "
734 (if ediff-use-last-dir
735 ediff-last-merge-autostore-dir
736 (ediff-strip-last-dir dir1
))
739 ;; verify merge-autostore-dir != dir1
740 (if (and merge-autostore-dir
742 (string= merge-autostore-dir dir1
))
744 "Directory for saving merged file = directory A. Sure? ")
745 (error "Merge of directory revisions aborted")))
748 (ediff-get-directory-files-under-revision
749 jobname regexp dir1 merge-autostore-dir
))
751 ;; this sets various vars in the meta buffer inside
752 ;; ediff-prepare-meta-buffer
754 ;; tell what to do if the user clicks on a session record
755 (setq ediff-session-action-function
(quote ,action
)))
757 (setq meta-buf
(ediff-prepare-meta-buffer
758 'ediff-filegroup-action
760 "*Ediff Session Group Panel"
761 'ediff-redraw-directory-group-buffer
764 (ediff-show-meta-buffer meta-buf
)
768 ;;; Compare regions and windows
771 (defun ediff-windows-wordwise (dumb-mode &optional wind-A wind-B startup-hooks
)
772 "Compare WIND-A and WIND-B, which are selected by clicking, wordwise.
773 With prefix argument, DUMB-MODE, or on a non-windowing display, works as
775 If WIND-A is nil, use selected window.
776 If WIND-B is nil, use window next to WIND-A."
778 (ediff-windows dumb-mode wind-A wind-B
779 startup-hooks
'ediff-windows-wordwise
'word-mode
))
782 (defun ediff-windows-linewise (dumb-mode &optional wind-A wind-B startup-hooks
)
783 "Compare WIND-A and WIND-B, which are selected by clicking, linewise.
784 With prefix argument, DUMB-MODE, or on a non-windowing display, works as
786 If WIND-A is nil, use selected window.
787 If WIND-B is nil, use window next to WIND-A."
789 (ediff-windows dumb-mode wind-A wind-B
790 startup-hooks
'ediff-windows-linewise nil
))
792 ;; Compare WIND-A and WIND-B, which are selected by clicking.
793 ;; With prefix argument, DUMB-MODE, or on a non-windowing display,
795 ;; If WIND-A is nil, use selected window.
796 ;; If WIND-B is nil, use window next to WIND-A.
797 (defun ediff-windows (dumb-mode wind-A wind-B startup-hooks job-name word-mode
)
798 (if (or dumb-mode
(not (ediff-window-display-p)))
799 (setq wind-A
(ediff-get-next-window wind-A nil
)
800 wind-B
(ediff-get-next-window wind-B wind-A
))
801 (setq wind-A
(ediff-get-window-by-clicking wind-A nil
1)
802 wind-B
(ediff-get-window-by-clicking wind-B wind-A
2)))
804 (let ((buffer-A (window-buffer wind-A
))
805 (buffer-B (window-buffer wind-B
))
806 beg-A end-A beg-B end-B
)
809 (save-window-excursion
810 (sit-for 0) ; sync before using window-start/end -- a precaution
811 (select-window wind-A
)
812 (setq beg-A
(window-start)
814 (select-window wind-B
)
815 (setq beg-B
(window-start)
816 end-B
(window-end))))
817 (ediff-regions-internal
818 buffer-A beg-A end-A buffer-B beg-B end-B
819 startup-hooks job-name word-mode nil
)))
822 (defun ediff-regions-wordwise (buffer-A buffer-B
&optional startup-hooks
)
823 "Run Ediff on a pair of regions in two different buffers.
824 Regions \(i.e., point and mark\) are assumed to be set in advance.
825 This function is effective only for relatively small regions, up to 200
826 lines. For large regions, use `ediff-regions-linewise'."
829 (list (setq bf
(read-buffer "Region's A buffer: "
830 (ediff-other-buffer "") t
))
831 (read-buffer "Region's B buffer: "
833 ;; realign buffers so that two visible bufs will be
835 (save-window-excursion (other-window 1))
836 (ediff-other-buffer bf
))
838 (if (not (ediff-buffer-live-p buffer-A
))
839 (error "Buffer %S doesn't exist" buffer-A
))
840 (if (not (ediff-buffer-live-p buffer-B
))
841 (error "Buffer %S doesn't exist" buffer-B
))
844 (let (reg-A-beg reg-A-end reg-B-beg reg-B-end
)
846 (set-buffer buffer-A
)
847 (setq reg-A-beg
(region-beginning)
848 reg-A-end
(region-end))
849 (set-buffer buffer-B
)
850 (setq reg-B-beg
(region-beginning)
851 reg-B-end
(region-end)))
853 (ediff-regions-internal
854 (get-buffer buffer-A
) reg-A-beg reg-A-end
855 (get-buffer buffer-B
) reg-B-beg reg-B-end
856 startup-hooks
'ediff-regions-wordwise
'word-mode nil
)))
859 (defun ediff-regions-linewise (buffer-A buffer-B
&optional startup-hooks
)
860 "Run Ediff on a pair of regions in two different buffers.
861 Regions \(i.e., point and mark\) are assumed to be set in advance.
862 Each region is enlarged to contain full lines.
863 This function is effective for large regions, over 100-200
864 lines. For small regions, use `ediff-regions-wordwise'."
867 (list (setq bf
(read-buffer "Region A's buffer: "
868 (ediff-other-buffer "") t
))
869 (read-buffer "Region B's buffer: "
871 ;; realign buffers so that two visible bufs will be
873 (save-window-excursion (other-window 1))
874 (ediff-other-buffer bf
))
876 (if (not (ediff-buffer-live-p buffer-A
))
877 (error "Buffer %S doesn't exist" buffer-A
))
878 (if (not (ediff-buffer-live-p buffer-B
))
879 (error "Buffer %S doesn't exist" buffer-B
))
881 (let (reg-A-beg reg-A-end reg-B-beg reg-B-end
)
883 (set-buffer buffer-A
)
884 (setq reg-A-beg
(region-beginning)
885 reg-A-end
(region-end))
886 ;; enlarge the region to hold full lines
887 (goto-char reg-A-beg
)
889 (setq reg-A-beg
(point))
890 (goto-char reg-A-end
)
892 (or (eobp) (forward-char)) ; include the newline char
893 (setq reg-A-end
(point))
895 (set-buffer buffer-B
)
896 (setq reg-B-beg
(region-beginning)
897 reg-B-end
(region-end))
898 ;; enlarge the region to hold full lines
899 (goto-char reg-B-beg
)
901 (setq reg-B-beg
(point))
902 (goto-char reg-B-end
)
904 (or (eobp) (forward-char)) ; include the newline char
905 (setq reg-B-end
(point))
908 (ediff-regions-internal
909 (get-buffer buffer-A
) reg-A-beg reg-A-end
910 (get-buffer buffer-B
) reg-B-beg reg-B-end
911 startup-hooks
'ediff-regions-linewise nil nil
))) ; no word mode
913 ;; compare region beg-A to end-A of buffer-A
914 ;; to regions beg-B -- end-B in buffer-B.
915 (defun ediff-regions-internal (buffer-A beg-A end-A buffer-B beg-B end-B
916 startup-hooks job-name word-mode
918 (let ((tmp-buffer (get-buffer-create ediff-tmp-buffer
))
922 ;; in case beg/end-A/B aren't markers--make them into markers
923 (ediff-with-current-buffer buffer-A
924 (setq beg-A
(move-marker (make-marker) beg-A
)
925 end-A
(move-marker (make-marker) end-A
)))
926 (ediff-with-current-buffer buffer-B
927 (setq beg-B
(move-marker (make-marker) beg-B
)
928 end-B
(move-marker (make-marker) end-B
)))
930 (if (and (eq buffer-A buffer-B
)
931 (or (and (< beg-A end-B
) (<= beg-B beg-A
)) ; b-B b-A e-B
932 (and (< beg-B end-A
) (<= end-A end-B
)))) ; b-B e-A e-B
934 (with-output-to-temp-buffer ediff-msg-buffer
936 You have requested to compare overlapping regions of the same buffer.
938 In this case, Ediff's highlighting may be confusing---in the same window,
939 you may see highlighted regions that belong to different regions.
941 Continue anyway? (y/n) "))
943 (if (y-or-n-p "Continue anyway? ")
945 (error "%S aborted" job-name
))))
949 (ediff-wordify beg-A end-A buffer-A tmp-buffer
)
950 (ediff-copy-to-buffer beg-A end-A buffer-A tmp-buffer
))
951 (setq file-A
(ediff-make-temp-file tmp-buffer
"regA"))
955 (ediff-wordify beg-B end-B buffer-B tmp-buffer
)
956 (ediff-copy-to-buffer beg-B end-B buffer-B tmp-buffer
))
957 (setq file-B
(ediff-make-temp-file tmp-buffer
"regB"))
959 (setq overl-A
(ediff-make-bullet-proof-overlay beg-A end-A buffer-A
))
960 (setq overl-B
(ediff-make-bullet-proof-overlay beg-B end-B buffer-B
))
961 (ediff-setup buffer-A file-A
963 nil nil
; buffer & file C
965 (delete-file ,file-A
)
966 (delete-file ,file-B
))
969 (list (cons 'ediff-word-mode word-mode
)
970 (cons 'ediff-narrow-bounds
(list overl-A overl-B
))
971 (cons 'ediff-job-name job-name
))
976 ;;; Merge files and buffers
979 (defalias 'ediff-merge
'ediff-merge-files
)
981 (defsubst ediff-merge-on-startup
()
983 (ediff-with-current-buffer ediff-buffer-C
984 (set-buffer-modified-p nil
)))
987 (defun ediff-merge-files (file-A file-B
988 ;; MERGE-BUFFER-FILE is the file to be
989 ;; associated with the merge buffer
990 &optional startup-hooks merge-buffer-file
)
991 "Merge two files without ancestor."
993 (let ((dir-A (if ediff-use-last-dir
997 (list (setq f
(ediff-read-file-name
998 "File A to merge" dir-A
999 (ediff-get-default-file-name)))
1000 (ediff-read-file-name "File B to merge"
1002 (if ediff-use-last-dir
1004 (file-name-directory f
)))
1006 (setq file-name-history
1007 (cons (ediff-abbreviate-file-name
1009 (file-name-nondirectory f
)
1014 (setq startup-hooks
(cons 'ediff-merge-on-startup startup-hooks
))
1015 (ediff-files-internal file-A
1016 (if (file-directory-p file-B
)
1018 (file-name-nondirectory file-A
) file-B
)
1026 (defun ediff-merge-files-with-ancestor (file-A file-B file-ancestor
1029 ;; MERGE-BUFFER-FILE is the file
1030 ;; to be associated with the
1033 "Merge two files with ancestor."
1035 (let ((dir-A (if ediff-use-last-dir
1038 dir-B dir-ancestor f ff
)
1039 (list (setq f
(ediff-read-file-name
1040 "File A to merge" dir-A
1041 (ediff-get-default-file-name)))
1042 (setq ff
(ediff-read-file-name "File B to merge"
1044 (if ediff-use-last-dir
1046 (file-name-directory f
)))
1048 (setq file-name-history
1050 (ediff-abbreviate-file-name
1052 (file-name-nondirectory f
)
1056 (ediff-read-file-name "Ancestor file"
1058 (if ediff-use-last-dir
1059 ediff-last-dir-ancestor
1060 (file-name-directory ff
)))
1062 (setq file-name-history
1063 (cons (ediff-abbreviate-file-name
1065 (file-name-nondirectory ff
)
1070 (setq startup-hooks
(cons 'ediff-merge-on-startup startup-hooks
))
1071 (ediff-files-internal file-A
1072 (if (file-directory-p file-B
)
1074 (file-name-nondirectory file-A
) file-B
)
1078 'ediff-merge-files-with-ancestor
1082 (defalias 'ediff-merge-with-ancestor
'ediff-merge-files-with-ancestor
)
1085 (defun ediff-merge-buffers (buffer-A buffer-B
1087 ;; MERGE-BUFFER-FILE is the file to be
1088 ;; associated with the merge buffer
1089 startup-hooks job-name merge-buffer-file
)
1090 "Merge buffers without ancestor."
1093 (list (setq bf
(read-buffer "Buffer A to merge: "
1094 (ediff-other-buffer "") t
))
1095 (read-buffer "Buffer B to merge: "
1097 ;; realign buffers so that two visible bufs will be
1099 (save-window-excursion (other-window 1))
1100 (ediff-other-buffer bf
))
1103 (setq startup-hooks
(cons 'ediff-merge-on-startup startup-hooks
))
1104 (or job-name
(setq job-name
'ediff-merge-buffers
))
1105 (ediff-buffers-internal
1106 buffer-A buffer-B nil startup-hooks job-name merge-buffer-file
))
1109 (defun ediff-merge-buffers-with-ancestor (buffer-A buffer-B buffer-ancestor
1113 ;; MERGE-BUFFER-FILE is the
1114 ;; file to be associated
1115 ;; with the merge buffer
1117 "Merge buffers with ancestor."
1120 (list (setq bf
(read-buffer "Buffer A to merge: "
1121 (ediff-other-buffer "") t
))
1122 (setq bff
(read-buffer "Buffer B to merge: "
1124 ;; realign buffers so that two visible
1125 ;; bufs will be at the top
1126 (save-window-excursion (other-window 1))
1127 (ediff-other-buffer bf
))
1129 (read-buffer "Ancestor buffer: "
1131 ;; realign buffers so that three visible
1132 ;; bufs will be at the top
1133 (save-window-excursion (other-window 1))
1134 (ediff-other-buffer (list bf bff
)))
1138 (setq startup-hooks
(cons 'ediff-merge-on-startup startup-hooks
))
1139 (or job-name
(setq job-name
'ediff-merge-buffers-with-ancestor
))
1140 (ediff-buffers-internal
1141 buffer-A buffer-B buffer-ancestor startup-hooks job-name merge-buffer-file
))
1145 (defun ediff-merge-revisions (&optional file startup-hooks merge-buffer-file
)
1146 ;; MERGE-BUFFER-FILE is the file to be associated with the merge buffer
1147 "Run Ediff by merging two revisions of a file.
1148 The file is the optional FILE argument or the file visited by the current
1151 (if (stringp file
) (find-file file
))
1156 "Version 1 to merge (default: %s's latest version): "
1158 (file-name-nondirectory file
) "current buffer")))
1162 "Version 2 to merge (default: %s): "
1164 (file-name-nondirectory file
) "current buffer"))))
1165 (ediff-load-version-control)
1166 ;; ancestor-revision=nil
1168 (intern (format "ediff-%S-merge-internal" ediff-version-control-package
))
1169 rev1 rev2 nil startup-hooks merge-buffer-file
)))
1173 (defun ediff-merge-revisions-with-ancestor (&optional
1175 ;; MERGE-BUFFER-FILE is the file to
1176 ;; be associated with the merge
1179 "Run Ediff by merging two revisions of a file with a common ancestor.
1180 The file is the the optional FILE argument or the file visited by the current
1183 (if (stringp file
) (find-file file
))
1184 (let (rev1 rev2 ancestor-rev
)
1188 "Version 1 to merge (default: %s's latest version): "
1190 (file-name-nondirectory file
) "current buffer")))
1194 "Version 2 to merge (default: %s): "
1196 (file-name-nondirectory file
) "current buffer")))
1200 "Ancestor version (default: %s's base revision): "
1202 (file-name-nondirectory file
) "current buffer"))))
1203 (ediff-load-version-control)
1205 (intern (format "ediff-%S-merge-internal" ediff-version-control-package
))
1206 rev1 rev2 ancestor-rev startup-hooks merge-buffer-file
)))
1209 (defun run-ediff-from-cvs-buffer (pos)
1210 "Run Ediff-merge on appropriate revisions of the selected file.
1211 First run after `M-x cvs-update'. Then place the cursor on a line describing a
1212 file and then run `run-ediff-from-cvs-buffer'."
1214 (ediff-load-version-control)
1215 (let ((tin (tin-locate cvs-cookie-handle pos
)))
1217 (cvs-run-ediff-on-file-descriptor tin
)
1218 (error "There is no file to merge"))))
1224 (defun ediff-patch-file (&optional arg patch-buf
)
1225 "Run Ediff by patching SOURCE-FILENAME.
1226 If optional PATCH-BUF is given, use the patch in that buffer
1227 and don't ask the user.
1228 If prefix argument, then: if even argument, assume that the patch is in a
1229 buffer. If odd -- assume it is in a file."
1231 (let (source-dir source-file
)
1232 (require 'ediff-ptch
)
1234 (ediff-get-patch-buffer
1235 (if arg
(prefix-numeric-value arg
)) patch-buf
))
1236 (setq source-dir
(cond (ediff-use-last-dir ediff-last-dir-patch
)
1237 ((and (not ediff-patch-default-directory
)
1238 (buffer-file-name patch-buf
))
1239 (file-name-directory
1241 (buffer-file-name patch-buf
))))
1242 (t default-directory
)))
1244 ;; the default is the directory, not the visited file name
1246 "File to patch (directory, if multifile patch): "
1247 source-dir
(ediff-get-default-file-name)))
1248 (ediff-dispatch-file-patching-job patch-buf source-file
)))
1251 (defun ediff-patch-buffer (&optional arg patch-buf
)
1252 "Run Ediff by patching BUFFER-NAME."
1254 (require 'ediff-ptch
)
1256 (ediff-get-patch-buffer
1257 (if arg
(prefix-numeric-value arg
)) patch-buf
))
1258 (ediff-patch-buffer-internal
1261 "Which buffer to patch? "
1262 (ediff-prompt-for-patch-buffer))))
1266 (defalias 'epatch
'ediff-patch-file
)
1268 (defalias 'epatch-buffer
'ediff-patch-buffer
)
1273 ;;; Versions Control functions
1276 (defun ediff-revision (&optional file startup-hooks
)
1277 "Run Ediff by comparing versions of a file.
1278 The file is an optional FILE argument or the file visited by the current
1279 buffer. Use `vc.el' or `rcs.el' depending on `ediff-version-control-package'."
1280 ;; if buffer is non-nil, use that buffer instead of the current buffer
1282 (if (stringp file
) (find-file file
))
1286 (format "Version 1 to compare (default: %s's latest version): "
1288 (file-name-nondirectory file
) "current buffer")))
1291 (format "Version 2 to compare (default: %s): "
1293 (file-name-nondirectory file
) "current buffer"))))
1294 (ediff-load-version-control)
1296 (intern (format "ediff-%S-internal" ediff-version-control-package
))
1297 rev1 rev2 startup-hooks
)
1302 (defalias 'erevision
'ediff-revision
)
1305 ;; Test if version control package is loaded and load if not
1306 ;; Is SILENT is non-nil, don't report error if package is not found.
1307 (defun ediff-load-version-control (&optional silent
)
1308 (require 'ediff-vers
)
1309 (or (featurep ediff-version-control-package
)
1310 (if (locate-library (symbol-name ediff-version-control-package
))
1312 (message "") ; kill the message from `locate-library'
1313 (require ediff-version-control-package
))
1315 (error "Version control package %S.el not found. Use vc.el instead"
1316 ediff-version-control-package
)))))
1320 (defun ediff-version ()
1321 "Return string describing the version of Ediff.
1322 When called interactively, displays the version."
1325 (message (ediff-version))
1326 (format "Ediff %s of %s" ediff-version ediff-date
)))
1330 (defun ediff-documentation (&optional node
)
1331 "Display Ediff's manual.
1332 With optional NODE, goes to that node."
1334 (let ((ctl-window ediff-control-window
)
1335 (ctl-buf ediff-control-buffer
))
1337 (ediff-skip-unsuitable-frames)
1340 (pop-to-buffer (get-buffer-create "*info*"))
1341 (info (if ediff-xemacs-p
"ediff.info" "ediff"))
1343 (Info-goto-node node
)
1344 (message "Type `i' to search for a specific topic"))
1345 (raise-frame (selected-frame)))
1347 (with-output-to-temp-buffer ediff-msg-buffer
1348 (princ ediff-BAD-INFO
))
1349 (if (window-live-p ctl-window
)
1351 (select-window ctl-window
)
1352 (set-window-buffer ctl-window ctl-buf
)))))))
1357 ;;; Local Variables:
1358 ;;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
1359 ;;; eval: (put 'ediff-with-current-buffer 'lisp-indent-hook 1)
1360 ;;; eval: (put 'ediff-with-current-buffer 'edebug-form-spec '(form body))
1363 (require 'ediff-util
)
1365 (run-hooks 'ediff-load-hook
)
1367 ;;; ediff.el ends here