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, tools, unix
9 (defconst ediff-version
"2.75" "The current version of Ediff")
10 (defconst ediff-date
"October 29, 2000" "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 number FILENO (or 0) in the list
167 ;; (all-selected-files, filename under the cursor), where directories are
168 ;; ignored. Otherwise, return DEFAULT file name, if non-nil. Else,
169 ;; if the buffer is visiting a file, return that file name.
170 (defun ediff-get-default-file-name (&optional default fileno
)
171 (cond ((eq major-mode
'dired-mode
)
172 (let ((current (dired-get-filename nil
'no-error
))
173 (marked (condition-case nil
174 (dired-get-marked-files 'no-dir
)
176 aux-list choices result
)
177 (or (integerp fileno
) (setq fileno
0))
178 (if (stringp default
)
179 (setq aux-list
(cons default aux-list
)))
180 (if (and (stringp current
) (not (file-directory-p current
)))
181 (setq aux-list
(cons current aux-list
)))
182 (setq choices
(nconc marked aux-list
))
183 (setq result
(elt choices fileno
))
186 ((stringp default
) default
)
187 ((buffer-file-name (current-buffer))
188 (file-name-nondirectory (buffer-file-name (current-buffer))))
191 ;;; Compare files/buffers
194 (defun ediff-files (file-A file-B
&optional startup-hooks
)
195 "Run Ediff on a pair of files, FILE-A and FILE-B."
197 (let ((dir-A (if ediff-use-last-dir
201 (list (setq f
(ediff-read-file-name
202 "File A to compare" dir-A
203 (ediff-get-default-file-name)))
204 (ediff-read-file-name "File B to compare"
206 (if ediff-use-last-dir
208 (file-name-directory f
)))
210 (setq file-name-history
211 (cons (ediff-abbreviate-file-name
213 (file-name-nondirectory f
)
216 (ediff-get-default-file-name f
1)))
218 (ediff-files-internal file-A
219 (if (file-directory-p file-B
)
221 (file-name-nondirectory file-A
) file-B
)
228 (defun ediff-files3 (file-A file-B file-C
&optional startup-hooks
)
229 "Run Ediff on three files, FILE-A, FILE-B, and FILE-C."
231 (let ((dir-A (if ediff-use-last-dir
235 (list (setq f
(ediff-read-file-name
236 "File A to compare" dir-A
237 (ediff-get-default-file-name)))
238 (setq ff
(ediff-read-file-name "File B to compare"
240 (if ediff-use-last-dir
242 (file-name-directory f
)))
244 (setq file-name-history
246 (ediff-abbreviate-file-name
248 (file-name-nondirectory f
)
251 (ediff-get-default-file-name f
1))))
252 (ediff-read-file-name "File C to compare"
253 (setq dir-C
(if ediff-use-last-dir
255 (file-name-directory ff
)))
257 (setq file-name-history
258 (cons (ediff-abbreviate-file-name
260 (file-name-nondirectory ff
)
263 (ediff-get-default-file-name ff
2)))
265 (ediff-files-internal file-A
266 (if (file-directory-p file-B
)
268 (file-name-nondirectory file-A
) file-B
)
270 (if (file-directory-p file-C
)
272 (file-name-nondirectory file-A
) file-C
)
278 (defalias 'ediff3
'ediff-files3
)
281 ;; Visit FILE and arrange its buffer to Ediff's liking.
282 ;; FILE is actually a variable symbol that must contain a true file name.
283 ;; BUFFER-NAME is a variable symbol, which will get the buffer object into
284 ;; which FILE is read.
285 ;; LAST-DIR is the directory variable symbol where FILE's
286 ;; directory name should be returned. HOOKS-VAR is a variable symbol that will
287 ;; be assigned the hook to be executed after `ediff-startup' is finished.
288 ;; `ediff-find-file' arranges that the temp files it might create will be
290 (defun ediff-find-file (file-var buffer-name
&optional last-dir hooks-var
)
291 (let* ((file (symbol-value file-var
))
292 (file-magic (ediff-filename-magic-p file
))
293 (temp-file-name-prefix (file-name-nondirectory file
)))
294 (cond ((not (file-readable-p file
))
295 (error "File `%s' does not exist or is not readable" file
))
296 ((file-directory-p file
)
297 (error "File `%s' is a directory" file
)))
299 ;; some of the commands, below, require full file name
300 (setq file
(expand-file-name file
))
302 ;; Record the directory of the file
304 (set last-dir
(expand-file-name (file-name-directory file
))))
307 (set buffer-name
(find-file-noselect file
))
309 (ediff-with-current-buffer (symbol-value buffer-name
)
310 (widen) ; Make sure the entire file is seen
311 (cond (file-magic ; file has a handler, such as jka-compr-handler or
312 ;;; ange-ftp-hook-function--arrange for temp file
313 (ediff-verify-file-buffer 'magic
)
315 (ediff-make-temp-file
316 (current-buffer) temp-file-name-prefix
))
317 (set hooks-var
(cons `(lambda () (delete-file ,file
))
318 (symbol-value hooks-var
))))
319 ;; file processed via auto-mode-alist, a la uncompress.el
320 ((not (equal (file-truename file
)
321 (file-truename (buffer-file-name))))
323 (ediff-make-temp-file
324 (current-buffer) temp-file-name-prefix
))
325 (set hooks-var
(cons `(lambda () (delete-file ,file
))
326 (symbol-value hooks-var
))))
327 (t ;; plain file---just check that the file matches the buffer
328 (ediff-verify-file-buffer))))
329 (set file-var file
)))
331 ;; MERGE-BUFFER-FILE is the file to be associated with the merge buffer
332 (defun ediff-files-internal (file-A file-B file-C startup-hooks job-name
333 &optional merge-buffer-file
)
334 (let (buf-A buf-B buf-C
)
335 (message "Reading file %s ... " file-A
)
337 (ediff-find-file 'file-A
'buf-A
'ediff-last-dir-A
'startup-hooks
)
338 (message "Reading file %s ... " file-B
)
340 (ediff-find-file 'file-B
'buf-B
'ediff-last-dir-B
'startup-hooks
)
343 (message "Reading file %s ... " file-C
)
347 (if (eq job-name
'ediff-merge-files-with-ancestor
)
348 'ediff-last-dir-ancestor
'ediff-last-dir-C
)
350 (ediff-setup buf-A file-A
354 (list (cons 'ediff-job-name job-name
))
359 (defalias 'ediff
'ediff-files
)
363 (defun ediff-buffers (buffer-A buffer-B
&optional startup-hooks job-name
)
364 "Run Ediff on a pair of buffers, BUFFER-A and BUFFER-B."
367 (list (setq bf
(read-buffer "Buffer A to compare: "
368 (ediff-other-buffer "") t
))
369 (read-buffer "Buffer B to compare: "
371 ;; realign buffers so that two visible bufs will be
373 (save-window-excursion (other-window 1))
374 (ediff-other-buffer bf
))
376 (or job-name
(setq job-name
'ediff-buffers
))
377 (ediff-buffers-internal buffer-A buffer-B nil startup-hooks job-name
))
380 (defalias 'ebuffers
'ediff-buffers
)
384 (defun ediff-buffers3 (buffer-A buffer-B buffer-C
385 &optional startup-hooks job-name
)
386 "Run Ediff on three buffers, BUFFER-A, BUFFER-B, and BUFFER-C."
389 (list (setq bf
(read-buffer "Buffer A to compare: "
390 (ediff-other-buffer "") t
))
391 (setq bff
(read-buffer "Buffer B to compare: "
393 ;; realign buffers so that two visible
394 ;; bufs will be at the top
395 (save-window-excursion (other-window 1))
396 (ediff-other-buffer bf
))
398 (read-buffer "Buffer C to compare: "
400 ;; realign buffers so that three visible
401 ;; bufs will be at the top
402 (save-window-excursion (other-window 1))
403 (ediff-other-buffer (list bf bff
)))
406 (or job-name
(setq job-name
'ediff-buffers3
))
407 (ediff-buffers-internal buffer-A buffer-B buffer-C startup-hooks job-name
))
410 (defalias 'ebuffers3
'ediff-buffers3
)
414 ;; MERGE-BUFFER-FILE is the file to be associated with the merge buffer
415 (defun ediff-buffers-internal (buf-A buf-B buf-C startup-hooks job-name
416 &optional merge-buffer-file
)
417 (let* ((buf-A-file-name (buffer-file-name (get-buffer buf-A
)))
418 (buf-B-file-name (buffer-file-name (get-buffer buf-B
)))
419 (buf-C-is-alive (ediff-buffer-live-p buf-C
))
420 (buf-C-file-name (if buf-C-is-alive
421 (buffer-file-name (get-buffer buf-B
))))
422 file-A file-B file-C
)
423 (if (not (ediff-buffer-live-p buf-A
))
424 (error "Buffer %S doesn't exist" buf-A
))
425 (if (not (ediff-buffer-live-p buf-B
))
426 (error "Buffer %S doesn't exist" buf-B
))
427 (let ((ediff-job-name job-name
))
428 (if (and ediff-3way-comparison-job
429 (not buf-C-is-alive
))
430 (error "Buffer %S doesn't exist" buf-C
)))
431 (if (stringp buf-A-file-name
)
432 (setq buf-A-file-name
(file-name-nondirectory buf-A-file-name
)))
433 (if (stringp buf-B-file-name
)
434 (setq buf-B-file-name
(file-name-nondirectory buf-B-file-name
)))
435 (if (stringp buf-C-file-name
)
436 (setq buf-C-file-name
(file-name-nondirectory buf-C-file-name
)))
438 (setq file-A
(ediff-make-temp-file buf-A buf-A-file-name
)
439 file-B
(ediff-make-temp-file buf-B buf-B-file-name
))
441 (setq file-C
(ediff-make-temp-file buf-C buf-C-file-name
)))
443 (ediff-setup (get-buffer buf-A
) file-A
444 (get-buffer buf-B
) file-B
445 (if buf-C-is-alive
(get-buffer buf-C
))
448 (delete-file ,file-A
)
449 (delete-file ,file-B
)
450 (if (stringp ,file-C
) (delete-file ,file-C
)))
452 (list (cons 'ediff-job-name job-name
))
456 ;;; Directory and file group operations
458 ;; Get appropriate default name for directory:
459 ;; If ediff-use-last-dir, use ediff-last-dir-A.
460 ;; In dired mode, use the directory that is under the point (if any);
461 ;; otherwise, use default-directory
462 (defun ediff-get-default-directory-name ()
463 (cond (ediff-use-last-dir ediff-last-dir-A
)
464 ((eq major-mode
'dired-mode
)
465 (let ((f (dired-get-filename nil
'noerror
)))
466 (if (and (stringp f
) (file-directory-p f
))
469 (t default-directory
)))
473 (defun ediff-directories (dir1 dir2 regexp
)
474 "Run Ediff on a pair of directories, DIR1 and DIR2, comparing files that have
475 the same name in both. The third argument, REGEXP, is a regular expression
476 that can be used to filter out certain file names."
478 (let ((dir-A (ediff-get-default-directory-name))
480 (list (setq f
(ediff-read-file-name "Directory A to compare:" dir-A nil
))
481 (ediff-read-file-name "Directory B to compare:"
482 (if ediff-use-last-dir
484 (ediff-strip-last-dir f
))
486 (read-string "Filter through regular expression: "
487 nil
'ediff-filtering-regexp-history
)
489 (ediff-directories-internal
490 dir1 dir2 nil regexp
'ediff-files
'ediff-directories
494 (defalias 'edirs
'ediff-directories
)
498 (defun ediff-directory-revisions (dir1 regexp
)
499 "Run Ediff on a directory, DIR1, comparing its files with their revisions.
500 The second argument, REGEXP, is a regular expression that filters the file
501 names. Only the files that are under revision control are taken into account."
503 (let ((dir-A (ediff-get-default-directory-name)))
504 (list (ediff-read-file-name
505 "Directory to compare with revision:" dir-A nil
)
506 (read-string "Filter through regular expression: "
507 nil
'ediff-filtering-regexp-history
)
509 (ediff-directory-revisions-internal
510 dir1 regexp
'ediff-revision
'ediff-directory-revisions
514 (defalias 'edir-revisions
'ediff-directory-revisions
)
518 (defun ediff-directories3 (dir1 dir2 dir3 regexp
)
519 "Run Ediff on three directories, DIR1, DIR2, and DIR3, comparing files that
520 have the same name in all three. The last argument, REGEXP, is a regular
521 expression that can be used to filter out certain file names."
523 (let ((dir-A (ediff-get-default-directory-name))
525 (list (setq f
(ediff-read-file-name "Directory A to compare:" dir-A nil
))
526 (setq f
(ediff-read-file-name "Directory B to compare:"
527 (if ediff-use-last-dir
529 (ediff-strip-last-dir f
))
531 (ediff-read-file-name "Directory C to compare:"
532 (if ediff-use-last-dir
534 (ediff-strip-last-dir f
))
536 (read-string "Filter through regular expression: "
537 nil
'ediff-filtering-regexp-history
)
539 (ediff-directories-internal
540 dir1 dir2 dir3 regexp
'ediff-files3
'ediff-directories3
544 (defalias 'edirs3
'ediff-directories3
)
547 (defun ediff-merge-directories (dir1 dir2 regexp
&optional merge-autostore-dir
)
548 "Run Ediff on a pair of directories, DIR1 and DIR2, merging files that have
549 the same name in both. The third argument, REGEXP, is a regular expression
550 that can be used to filter out certain file names."
552 (let ((dir-A (ediff-get-default-directory-name))
554 (list (setq f
(ediff-read-file-name "Directory A to merge:" dir-A nil
))
555 (ediff-read-file-name "Directory B to merge:"
556 (if ediff-use-last-dir
558 (ediff-strip-last-dir f
))
560 (read-string "Filter through regular expression: "
561 nil
'ediff-filtering-regexp-history
)
563 (ediff-directories-internal
564 dir1 dir2 nil regexp
'ediff-merge-files
'ediff-merge-directories
565 nil merge-autostore-dir
569 (defalias 'edirs-merge
'ediff-merge-directories
)
572 (defun ediff-merge-directories-with-ancestor (dir1 dir2 ancestor-dir regexp
575 "Merge files in directories DIR1 and DIR2 using files in ANCESTOR-DIR as ancestors.
576 Ediff merges files that have identical names in DIR1, DIR2. If a pair of files
577 in DIR1 and DIR2 doesn't have an ancestor in ANCESTOR-DIR, Ediff will merge
578 without ancestor. The fourth argument, REGEXP, is a regular expression that
579 can be used to filter out certain file names."
581 (let ((dir-A (ediff-get-default-directory-name))
583 (list (setq f
(ediff-read-file-name "Directory A to merge:" dir-A nil
))
584 (setq f
(ediff-read-file-name "Directory B to merge:"
585 (if ediff-use-last-dir
587 (ediff-strip-last-dir f
))
589 (ediff-read-file-name "Ancestor directory:"
590 (if ediff-use-last-dir
592 (ediff-strip-last-dir f
))
594 (read-string "Filter through regular expression: "
595 nil
'ediff-filtering-regexp-history
)
597 (ediff-directories-internal
598 dir1 dir2 ancestor-dir regexp
599 'ediff-merge-files-with-ancestor
'ediff-merge-directories-with-ancestor
600 nil merge-autostore-dir
604 (defun ediff-merge-directory-revisions (dir1 regexp
605 &optional merge-autostore-dir
)
606 "Run Ediff on a directory, DIR1, merging its files with their revisions.
607 The second argument, REGEXP, is a regular expression that filters the file
608 names. Only the files that are under revision control are taken into account."
610 (let ((dir-A (ediff-get-default-directory-name)))
611 (list (ediff-read-file-name
612 "Directory to merge with revisions:" dir-A nil
)
613 (read-string "Filter through regular expression: "
614 nil
'ediff-filtering-regexp-history
)
616 (ediff-directory-revisions-internal
617 dir1 regexp
'ediff-merge-revisions
'ediff-merge-directory-revisions
618 nil merge-autostore-dir
622 (defalias 'edir-merge-revisions
'ediff-merge-directory-revisions
)
625 (defun ediff-merge-directory-revisions-with-ancestor (dir1 regexp
628 "Run Ediff on a directory, DIR1, merging its files with their revisions and ancestors.
629 The second argument, REGEXP, is a regular expression that filters the file
630 names. Only the files that are under revision control are taken into account."
632 (let ((dir-A (ediff-get-default-directory-name)))
633 (list (ediff-read-file-name
634 "Directory to merge with revisions and ancestors:" dir-A nil
)
635 (read-string "Filter through regular expression: "
636 nil
'ediff-filtering-regexp-history
)
638 (ediff-directory-revisions-internal
639 dir1 regexp
'ediff-merge-revisions-with-ancestor
640 'ediff-merge-directory-revisions-with-ancestor
641 nil merge-autostore-dir
646 'edir-merge-revisions-with-ancestor
647 'ediff-merge-directory-revisions-with-ancestor
)
650 (defalias 'edirs-merge-with-ancestor
'ediff-merge-directories-with-ancestor
)
652 ;; Run ediff-action (ediff-files, ediff-merge, ediff-merge-with-ancestors)
653 ;; on a pair of directories (three directories, in case of ancestor).
654 ;; The third argument, REGEXP, is a regular expression that can be used to
655 ;; filter out certain file names.
656 ;; JOBNAME is the symbol indicating the meta-job to be performed.
657 ;; MERGE-AUTOSTORE-DIR is the directory in which to store merged files.
658 (defun ediff-directories-internal (dir1 dir2 dir3 regexp action jobname
659 &optional startup-hooks
661 ;; ediff-read-file-name is set to attach a previously entered file name if
662 ;; the currently entered file is a directory. This code takes care of that.
663 (setq dir1
(if (file-directory-p dir1
) dir1
(file-name-directory dir1
))
664 dir2
(if (file-directory-p dir2
) dir2
(file-name-directory dir2
)))
667 (setq dir3
(if (file-directory-p dir3
) dir3
(file-name-directory dir3
))))
669 (cond ((string= dir1 dir2
)
670 (error "Directories A and B are the same: %s" dir1
))
671 ((and (eq jobname
'ediff-directories3
)
673 (error "Directories A and C are the same: %s" dir1
))
674 ((and (eq jobname
'ediff-directories3
)
676 (error "Directories B and C are the same: %s" dir1
)))
678 (if merge-autostore-dir
679 (or (stringp merge-autostore-dir
)
680 (error "%s: Directory for storing merged files must be a string"
682 (let (diffs ; var where ediff-intersect-directories returns the diff list
684 (if (and ediff-autostore-merges
685 (ediff-merge-metajob jobname
)
686 (not merge-autostore-dir
))
687 (setq merge-autostore-dir
688 (read-file-name "Save merged files in directory: "
689 (if ediff-use-last-dir
690 ediff-last-merge-autostore-dir
691 (ediff-strip-last-dir dir1
))
694 ;; verify we are not merging into an orig directory
695 (if merge-autostore-dir
696 (cond ((and (stringp dir1
) (string= merge-autostore-dir dir1
))
698 "Directory for saving merged files = Directory A. Sure? ")
699 (error "Directory merge aborted")))
700 ((and (stringp dir2
) (string= merge-autostore-dir dir2
))
702 "Directory for saving merged files = Directory B. Sure? ")
703 (error "Directory merge aborted")))
704 ((and (stringp dir3
) (string= merge-autostore-dir dir3
))
706 "Directory for saving merged files = Ancestor Directory. Sure? ")
707 (error "Directory merge aborted")))))
709 (setq file-list
(ediff-intersect-directories
711 regexp dir1 dir2 dir3 merge-autostore-dir
))
713 ;; this sets various vars in the meta buffer inside
714 ;; ediff-prepare-meta-buffer
716 ;; tell what to do if the user clicks on a session record
717 (setq ediff-session-action-function
(quote ,action
))
718 ;; set ediff-dir-difference-list
719 (setq ediff-dir-difference-list
(quote ,diffs
)))
721 (setq meta-buf
(ediff-prepare-meta-buffer
722 'ediff-filegroup-action
724 "*Ediff Session Group Panel"
725 'ediff-redraw-directory-group-buffer
728 (ediff-show-meta-buffer meta-buf
)
731 ;; MERGE-AUTOSTORE-DIR can be given to tell ediff where to store the merged
733 (defun ediff-directory-revisions-internal (dir1 regexp action jobname
734 &optional startup-hooks
736 (setq dir1
(if (file-directory-p dir1
) dir1
(file-name-directory dir1
)))
738 (if merge-autostore-dir
739 (or (stringp merge-autostore-dir
)
740 (error "%S: Directory for storing merged files must be a string"
742 (let (file-list meta-buf
)
743 (if (and ediff-autostore-merges
744 (ediff-merge-metajob jobname
)
745 (not merge-autostore-dir
))
746 (setq merge-autostore-dir
747 (read-file-name "Save merged files in directory: "
748 (if ediff-use-last-dir
749 ediff-last-merge-autostore-dir
750 (ediff-strip-last-dir dir1
))
753 ;; verify merge-autostore-dir != dir1
754 (if (and merge-autostore-dir
756 (string= merge-autostore-dir dir1
))
758 "Directory for saving merged file = directory A. Sure? ")
759 (error "Merge of directory revisions aborted")))
762 (ediff-get-directory-files-under-revision
763 jobname regexp dir1 merge-autostore-dir
))
765 ;; this sets various vars in the meta buffer inside
766 ;; ediff-prepare-meta-buffer
768 ;; tell what to do if the user clicks on a session record
769 (setq ediff-session-action-function
(quote ,action
)))
771 (setq meta-buf
(ediff-prepare-meta-buffer
772 'ediff-filegroup-action
774 "*Ediff Session Group Panel"
775 'ediff-redraw-directory-group-buffer
778 (ediff-show-meta-buffer meta-buf
)
782 ;;; Compare regions and windows
785 (defun ediff-windows-wordwise (dumb-mode &optional wind-A wind-B startup-hooks
)
786 "Compare WIND-A and WIND-B, which are selected by clicking, wordwise.
787 With prefix argument, DUMB-MODE, or on a non-windowing display, works as
789 If WIND-A is nil, use selected window.
790 If WIND-B is nil, use window next to WIND-A."
792 (ediff-windows dumb-mode wind-A wind-B
793 startup-hooks
'ediff-windows-wordwise
'word-mode
))
796 (defun ediff-windows-linewise (dumb-mode &optional wind-A wind-B startup-hooks
)
797 "Compare WIND-A and WIND-B, which are selected by clicking, linewise.
798 With prefix argument, DUMB-MODE, or on a non-windowing display, works as
800 If WIND-A is nil, use selected window.
801 If WIND-B is nil, use window next to WIND-A."
803 (ediff-windows dumb-mode wind-A wind-B
804 startup-hooks
'ediff-windows-linewise nil
))
806 ;; Compare WIND-A and WIND-B, which are selected by clicking.
807 ;; With prefix argument, DUMB-MODE, or on a non-windowing display,
809 ;; If WIND-A is nil, use selected window.
810 ;; If WIND-B is nil, use window next to WIND-A.
811 (defun ediff-windows (dumb-mode wind-A wind-B startup-hooks job-name word-mode
)
812 (if (or dumb-mode
(not (ediff-window-display-p)))
813 (setq wind-A
(ediff-get-next-window wind-A nil
)
814 wind-B
(ediff-get-next-window wind-B wind-A
))
815 (setq wind-A
(ediff-get-window-by-clicking wind-A nil
1)
816 wind-B
(ediff-get-window-by-clicking wind-B wind-A
2)))
818 (let ((buffer-A (window-buffer wind-A
))
819 (buffer-B (window-buffer wind-B
))
820 beg-A end-A beg-B end-B
)
823 (save-window-excursion
824 (sit-for 0) ; sync before using window-start/end -- a precaution
825 (select-window wind-A
)
826 (setq beg-A
(window-start)
828 (select-window wind-B
)
829 (setq beg-B
(window-start)
830 end-B
(window-end))))
831 (ediff-regions-internal
832 buffer-A beg-A end-A buffer-B beg-B end-B
833 startup-hooks job-name word-mode nil
)))
836 (defun ediff-regions-wordwise (buffer-A buffer-B
&optional startup-hooks
)
837 "Run Ediff on a pair of regions in two different buffers.
838 Regions \(i.e., point and mark\) are assumed to be set in advance.
839 This function is effective only for relatively small regions, up to 200
840 lines. For large regions, use `ediff-regions-linewise'."
843 (list (setq bf
(read-buffer "Region's A buffer: "
844 (ediff-other-buffer "") t
))
845 (read-buffer "Region's B buffer: "
847 ;; realign buffers so that two visible bufs will be
849 (save-window-excursion (other-window 1))
850 (ediff-other-buffer bf
))
852 (if (not (ediff-buffer-live-p buffer-A
))
853 (error "Buffer %S doesn't exist" buffer-A
))
854 (if (not (ediff-buffer-live-p buffer-B
))
855 (error "Buffer %S doesn't exist" buffer-B
))
858 (let (reg-A-beg reg-A-end reg-B-beg reg-B-end
)
860 (set-buffer buffer-A
)
861 (setq reg-A-beg
(region-beginning)
862 reg-A-end
(region-end))
863 (set-buffer buffer-B
)
864 (setq reg-B-beg
(region-beginning)
865 reg-B-end
(region-end)))
867 (ediff-regions-internal
868 (get-buffer buffer-A
) reg-A-beg reg-A-end
869 (get-buffer buffer-B
) reg-B-beg reg-B-end
870 startup-hooks
'ediff-regions-wordwise
'word-mode nil
)))
873 (defun ediff-regions-linewise (buffer-A buffer-B
&optional startup-hooks
)
874 "Run Ediff on a pair of regions in two different buffers.
875 Regions \(i.e., point and mark\) are assumed to be set in advance.
876 Each region is enlarged to contain full lines.
877 This function is effective for large regions, over 100-200
878 lines. For small regions, use `ediff-regions-wordwise'."
881 (list (setq bf
(read-buffer "Region A's buffer: "
882 (ediff-other-buffer "") t
))
883 (read-buffer "Region B's buffer: "
885 ;; realign buffers so that two visible bufs will be
887 (save-window-excursion (other-window 1))
888 (ediff-other-buffer bf
))
890 (if (not (ediff-buffer-live-p buffer-A
))
891 (error "Buffer %S doesn't exist" buffer-A
))
892 (if (not (ediff-buffer-live-p buffer-B
))
893 (error "Buffer %S doesn't exist" buffer-B
))
895 (let (reg-A-beg reg-A-end reg-B-beg reg-B-end
)
897 (set-buffer buffer-A
)
898 (setq reg-A-beg
(region-beginning)
899 reg-A-end
(region-end))
900 ;; enlarge the region to hold full lines
901 (goto-char reg-A-beg
)
903 (setq reg-A-beg
(point))
904 (goto-char reg-A-end
)
906 (or (eobp) (forward-char)) ; include the newline char
907 (setq reg-A-end
(point))
909 (set-buffer buffer-B
)
910 (setq reg-B-beg
(region-beginning)
911 reg-B-end
(region-end))
912 ;; enlarge the region to hold full lines
913 (goto-char reg-B-beg
)
915 (setq reg-B-beg
(point))
916 (goto-char reg-B-end
)
918 (or (eobp) (forward-char)) ; include the newline char
919 (setq reg-B-end
(point))
922 (ediff-regions-internal
923 (get-buffer buffer-A
) reg-A-beg reg-A-end
924 (get-buffer buffer-B
) reg-B-beg reg-B-end
925 startup-hooks
'ediff-regions-linewise nil nil
))) ; no word mode
927 ;; compare region beg-A to end-A of buffer-A
928 ;; to regions beg-B -- end-B in buffer-B.
929 (defun ediff-regions-internal (buffer-A beg-A end-A buffer-B beg-B end-B
930 startup-hooks job-name word-mode
932 (let ((tmp-buffer (get-buffer-create ediff-tmp-buffer
))
936 ;; in case beg/end-A/B aren't markers--make them into markers
937 (ediff-with-current-buffer buffer-A
938 (setq beg-A
(move-marker (make-marker) beg-A
)
939 end-A
(move-marker (make-marker) end-A
)))
940 (ediff-with-current-buffer buffer-B
941 (setq beg-B
(move-marker (make-marker) beg-B
)
942 end-B
(move-marker (make-marker) end-B
)))
944 (if (and (eq buffer-A buffer-B
)
945 (or (and (< beg-A end-B
) (<= beg-B beg-A
)) ; b-B b-A e-B
946 (and (< beg-B end-A
) (<= end-A end-B
)))) ; b-B e-A e-B
948 (with-output-to-temp-buffer ediff-msg-buffer
949 (ediff-with-current-buffer standard-output
952 You have requested to compare overlapping regions of the same buffer.
954 In this case, Ediff's highlighting may be confusing---in the same window,
955 you may see highlighted regions that belong to different regions.
957 Continue anyway? (y/n) "))
959 (if (y-or-n-p "Continue anyway? ")
961 (error "%S aborted" job-name
))))
965 (ediff-wordify beg-A end-A buffer-A tmp-buffer
)
966 (ediff-copy-to-buffer beg-A end-A buffer-A tmp-buffer
))
967 (setq file-A
(ediff-make-temp-file tmp-buffer
"regA"))
971 (ediff-wordify beg-B end-B buffer-B tmp-buffer
)
972 (ediff-copy-to-buffer beg-B end-B buffer-B tmp-buffer
))
973 (setq file-B
(ediff-make-temp-file tmp-buffer
"regB"))
975 (setq overl-A
(ediff-make-bullet-proof-overlay beg-A end-A buffer-A
))
976 (setq overl-B
(ediff-make-bullet-proof-overlay beg-B end-B buffer-B
))
977 (ediff-setup buffer-A file-A
979 nil nil
; buffer & file C
981 (delete-file ,file-A
)
982 (delete-file ,file-B
))
985 (list (cons 'ediff-word-mode word-mode
)
986 (cons 'ediff-narrow-bounds
(list overl-A overl-B
))
987 (cons 'ediff-job-name job-name
))
992 ;;; Merge files and buffers
995 (defalias 'ediff-merge
'ediff-merge-files
)
997 (defsubst ediff-merge-on-startup
()
999 (ediff-with-current-buffer ediff-buffer-C
1000 (set-buffer-modified-p nil
)))
1003 (defun ediff-merge-files (file-A file-B
1004 ;; MERGE-BUFFER-FILE is the file to be
1005 ;; associated with the merge buffer
1006 &optional startup-hooks merge-buffer-file
)
1007 "Merge two files without ancestor."
1009 (let ((dir-A (if ediff-use-last-dir
1013 (list (setq f
(ediff-read-file-name
1014 "File A to merge" dir-A
1015 (ediff-get-default-file-name)))
1016 (ediff-read-file-name "File B to merge"
1018 (if ediff-use-last-dir
1020 (file-name-directory f
)))
1022 (setq file-name-history
1023 (cons (ediff-abbreviate-file-name
1025 (file-name-nondirectory f
)
1028 (ediff-get-default-file-name f
1)))
1030 (setq startup-hooks
(cons 'ediff-merge-on-startup startup-hooks
))
1031 (ediff-files-internal file-A
1032 (if (file-directory-p file-B
)
1034 (file-name-nondirectory file-A
) file-B
)
1042 (defun ediff-merge-files-with-ancestor (file-A file-B file-ancestor
1045 ;; MERGE-BUFFER-FILE is the file
1046 ;; to be associated with the
1049 "Merge two files with ancestor."
1051 (let ((dir-A (if ediff-use-last-dir
1054 dir-B dir-ancestor f ff
)
1055 (list (setq f
(ediff-read-file-name
1056 "File A to merge" dir-A
1057 (ediff-get-default-file-name)))
1058 (setq ff
(ediff-read-file-name "File B to merge"
1060 (if ediff-use-last-dir
1062 (file-name-directory f
)))
1064 (setq file-name-history
1066 (ediff-abbreviate-file-name
1068 (file-name-nondirectory f
)
1071 (ediff-get-default-file-name f
1))))
1072 (ediff-read-file-name "Ancestor file"
1074 (if ediff-use-last-dir
1075 ediff-last-dir-ancestor
1076 (file-name-directory ff
)))
1078 (setq file-name-history
1079 (cons (ediff-abbreviate-file-name
1081 (file-name-nondirectory ff
)
1084 (ediff-get-default-file-name ff
2)))
1086 (setq startup-hooks
(cons 'ediff-merge-on-startup startup-hooks
))
1087 (ediff-files-internal file-A
1088 (if (file-directory-p file-B
)
1090 (file-name-nondirectory file-A
) file-B
)
1094 'ediff-merge-files-with-ancestor
1098 (defalias 'ediff-merge-with-ancestor
'ediff-merge-files-with-ancestor
)
1101 (defun ediff-merge-buffers (buffer-A buffer-B
1103 ;; MERGE-BUFFER-FILE is the file to be
1104 ;; associated with the merge buffer
1105 startup-hooks job-name merge-buffer-file
)
1106 "Merge buffers without ancestor."
1109 (list (setq bf
(read-buffer "Buffer A to merge: "
1110 (ediff-other-buffer "") t
))
1111 (read-buffer "Buffer B to merge: "
1113 ;; realign buffers so that two visible bufs will be
1115 (save-window-excursion (other-window 1))
1116 (ediff-other-buffer bf
))
1119 (setq startup-hooks
(cons 'ediff-merge-on-startup startup-hooks
))
1120 (or job-name
(setq job-name
'ediff-merge-buffers
))
1121 (ediff-buffers-internal
1122 buffer-A buffer-B nil startup-hooks job-name merge-buffer-file
))
1125 (defun ediff-merge-buffers-with-ancestor (buffer-A buffer-B buffer-ancestor
1129 ;; MERGE-BUFFER-FILE is the
1130 ;; file to be associated
1131 ;; with the merge buffer
1133 "Merge buffers with ancestor."
1136 (list (setq bf
(read-buffer "Buffer A to merge: "
1137 (ediff-other-buffer "") t
))
1138 (setq bff
(read-buffer "Buffer B to merge: "
1140 ;; realign buffers so that two visible
1141 ;; bufs will be at the top
1142 (save-window-excursion (other-window 1))
1143 (ediff-other-buffer bf
))
1145 (read-buffer "Ancestor buffer: "
1147 ;; realign buffers so that three visible
1148 ;; bufs will be at the top
1149 (save-window-excursion (other-window 1))
1150 (ediff-other-buffer (list bf bff
)))
1154 (setq startup-hooks
(cons 'ediff-merge-on-startup startup-hooks
))
1155 (or job-name
(setq job-name
'ediff-merge-buffers-with-ancestor
))
1156 (ediff-buffers-internal
1157 buffer-A buffer-B buffer-ancestor startup-hooks job-name merge-buffer-file
))
1161 (defun ediff-merge-revisions (&optional file startup-hooks merge-buffer-file
)
1162 ;; MERGE-BUFFER-FILE is the file to be associated with the merge buffer
1163 "Run Ediff by merging two revisions of a file.
1164 The file is the optional FILE argument or the file visited by the current
1167 (if (stringp file
) (find-file file
))
1172 "Version 1 to merge (default: %s's latest version): "
1174 (file-name-nondirectory file
) "current buffer")))
1178 "Version 2 to merge (default: %s): "
1180 (file-name-nondirectory file
) "current buffer"))))
1181 (ediff-load-version-control)
1182 ;; ancestor-revision=nil
1184 (intern (format "ediff-%S-merge-internal" ediff-version-control-package
))
1185 rev1 rev2 nil startup-hooks merge-buffer-file
)))
1189 (defun ediff-merge-revisions-with-ancestor (&optional
1191 ;; MERGE-BUFFER-FILE is the file to
1192 ;; be associated with the merge
1195 "Run Ediff by merging two revisions of a file with a common ancestor.
1196 The file is the the optional FILE argument or the file visited by the current
1199 (if (stringp file
) (find-file file
))
1200 (let (rev1 rev2 ancestor-rev
)
1204 "Version 1 to merge (default: %s's latest version): "
1206 (file-name-nondirectory file
) "current buffer")))
1210 "Version 2 to merge (default: %s): "
1212 (file-name-nondirectory file
) "current buffer")))
1216 "Ancestor version (default: %s's base revision): "
1218 (file-name-nondirectory file
) "current buffer"))))
1219 (ediff-load-version-control)
1221 (intern (format "ediff-%S-merge-internal" ediff-version-control-package
))
1222 rev1 rev2 ancestor-rev startup-hooks merge-buffer-file
)))
1225 (defun run-ediff-from-cvs-buffer (pos)
1226 "Run Ediff-merge on appropriate revisions of the selected file.
1227 First run after `M-x cvs-update'. Then place the cursor on a line describing a
1228 file and then run `run-ediff-from-cvs-buffer'."
1230 (ediff-load-version-control)
1231 (let ((tin (tin-locate cvs-cookie-handle pos
)))
1233 (cvs-run-ediff-on-file-descriptor tin
)
1234 (error "There is no file to merge"))))
1240 (defun ediff-patch-file (&optional arg patch-buf
)
1241 "Run Ediff by patching SOURCE-FILENAME.
1242 If optional PATCH-BUF is given, use the patch in that buffer
1243 and don't ask the user.
1244 If prefix argument, then: if even argument, assume that the patch is in a
1245 buffer. If odd -- assume it is in a file."
1247 (let (source-dir source-file
)
1248 (require 'ediff-ptch
)
1250 (ediff-get-patch-buffer
1251 (if arg
(prefix-numeric-value arg
)) patch-buf
))
1252 (setq source-dir
(cond (ediff-use-last-dir ediff-last-dir-patch
)
1253 ((and (not ediff-patch-default-directory
)
1254 (buffer-file-name patch-buf
))
1255 (file-name-directory
1257 (buffer-file-name patch-buf
))))
1258 (t default-directory
)))
1260 ;; the default is the directory, not the visited file name
1262 "File to patch (directory, if multifile patch): "
1263 source-dir
(ediff-get-default-file-name)))
1264 (ediff-dispatch-file-patching-job patch-buf source-file
)))
1267 (defun ediff-patch-buffer (&optional arg patch-buf
)
1268 "Run Ediff by patching BUFFER-NAME."
1270 (require 'ediff-ptch
)
1272 (ediff-get-patch-buffer
1273 (if arg
(prefix-numeric-value arg
)) patch-buf
))
1274 (ediff-patch-buffer-internal
1277 "Which buffer to patch? "
1278 (ediff-prompt-for-patch-buffer))))
1282 (defalias 'epatch
'ediff-patch-file
)
1284 (defalias 'epatch-buffer
'ediff-patch-buffer
)
1289 ;;; Versions Control functions
1292 (defun ediff-revision (&optional file startup-hooks
)
1293 "Run Ediff by comparing versions of a file.
1294 The file is an optional FILE argument or the file visited by the current
1295 buffer. Use `vc.el' or `rcs.el' depending on `ediff-version-control-package'."
1296 ;; if buffer is non-nil, use that buffer instead of the current buffer
1298 (if (stringp file
) (find-file file
))
1302 (format "Version 1 to compare (default: %s's latest version): "
1304 (file-name-nondirectory file
) "current buffer")))
1307 (format "Version 2 to compare (default: %s): "
1309 (file-name-nondirectory file
) "current buffer"))))
1310 (ediff-load-version-control)
1312 (intern (format "ediff-%S-internal" ediff-version-control-package
))
1313 rev1 rev2 startup-hooks
)
1318 (defalias 'erevision
'ediff-revision
)
1321 ;; Test if version control package is loaded and load if not
1322 ;; Is SILENT is non-nil, don't report error if package is not found.
1323 (defun ediff-load-version-control (&optional silent
)
1324 (require 'ediff-vers
)
1325 (or (featurep ediff-version-control-package
)
1326 (if (locate-library (symbol-name ediff-version-control-package
))
1328 (message "") ; kill the message from `locate-library'
1329 (require ediff-version-control-package
))
1331 (error "Version control package %S.el not found. Use vc.el instead"
1332 ediff-version-control-package
)))))
1336 (defun ediff-version ()
1337 "Return string describing the version of Ediff.
1338 When called interactively, displays the version."
1341 (message (ediff-version))
1342 (format "Ediff %s of %s" ediff-version ediff-date
)))
1346 (defun ediff-documentation (&optional node
)
1347 "Display Ediff's manual.
1348 With optional NODE, goes to that node."
1350 (let ((ctl-window ediff-control-window
)
1351 (ctl-buf ediff-control-buffer
))
1353 (ediff-skip-unsuitable-frames)
1356 (pop-to-buffer (get-buffer-create "*info*"))
1357 (info (if ediff-xemacs-p
"ediff.info" "ediff"))
1359 (Info-goto-node node
)
1360 (message "Type `i' to search for a specific topic"))
1361 (raise-frame (selected-frame)))
1363 (with-output-to-temp-buffer ediff-msg-buffer
1364 (ediff-with-current-buffer standard-output
1366 (princ ediff-BAD-INFO
))
1367 (if (window-live-p ctl-window
)
1369 (select-window ctl-window
)
1370 (set-window-buffer ctl-window ctl-buf
)))))))
1375 ;;; Local Variables:
1376 ;;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
1377 ;;; eval: (put 'ediff-with-current-buffer 'lisp-indent-hook 1)
1378 ;;; eval: (put 'ediff-with-current-buffer 'edebug-form-spec '(form body))
1381 (require 'ediff-util
)
1383 (run-hooks 'ediff-load-hook
)
1385 ;;; ediff.el ends here