Large simplification in (vc-deduce-fileset) logic.
[emacs.git] / lisp / ediff.el
blobeaf66e87c253907192ad981bbdd590b737e3fe67
1 ;;; ediff.el --- a comprehensive visual interface to diff & patch
3 ;; Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
7 ;; Created: February 2, 1994
8 ;; Keywords: comparing, merging, patching, tools, unix
10 ;; Yoni Rabkin <yoni@rabkins.net> contacted the maintainer of this
11 ;; file on 20/3/2008, and the maintainer agreed that when a bug is
12 ;; filed in the Emacs bug reporting system against this file, a copy
13 ;; of the bug report be sent to the maintainer's email address.
15 (defconst ediff-version "2.81.2" "The current version of Ediff")
16 (defconst ediff-date "April 06, 2008" "Date of last update")
19 ;; This file is part of GNU Emacs.
21 ;; GNU Emacs is free software: you can redistribute it and/or modify
22 ;; it under the terms of the GNU General Public License as published by
23 ;; the Free Software Foundation, either version 3 of the License, or
24 ;; (at your option) any later version.
26 ;; GNU Emacs is distributed in the hope that it will be useful,
27 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
28 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 ;; GNU General Public License for more details.
31 ;; You should have received a copy of the GNU General Public License
32 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
34 ;;; Commentary:
36 ;; Never read that diff output again!
37 ;; Apply patch interactively!
38 ;; Merge with ease!
40 ;; This package provides a convenient way of simultaneous browsing through
41 ;; the differences between a pair (or a triple) of files or buffers. The
42 ;; files being compared, file-A, file-B, and file-C (if applicable) are
43 ;; shown in separate windows (side by side, one above the another, or in
44 ;; separate frames), and the differences are highlighted as you step
45 ;; through them. You can also copy difference regions from one buffer to
46 ;; another (and recover old differences if you change your mind).
48 ;; Ediff also supports merging operations on files and buffers, including
49 ;; merging using ancestor versions. Both comparison and merging operations can
50 ;; be performed on directories, i.e., by pairwise comparison of files in those
51 ;; directories.
53 ;; In addition, Ediff can apply a patch to a file and then let you step
54 ;; though both files, the patched and the original one, simultaneously,
55 ;; difference-by-difference. You can even apply a patch right out of a
56 ;; mail buffer, i.e., patches received by mail don't even have to be saved.
57 ;; Since Ediff lets you copy differences between buffers, you can, in
58 ;; effect, apply patches selectively (i.e., you can copy a difference
59 ;; region from file_orig to file, thereby undoing any particular patch that
60 ;; you don't like).
62 ;; Ediff is aware of version control, which lets the user compare
63 ;; files with their older versions. Ediff can also work with remote and
64 ;; compressed files. Details are given below.
66 ;; Finally, Ediff supports directory-level comparison, merging and patching.
67 ;; See the on-line manual for details.
69 ;; This package builds upon the ideas borrowed from emerge.el and several
70 ;; Ediff's functions are adaptations from emerge.el. Much of the functionality
71 ;; Ediff provides is also influenced by emerge.el.
73 ;; The present version of Ediff supersedes Emerge. It provides a superior user
74 ;; interface and has numerous major features not found in Emerge. In
75 ;; particular, it can do patching, and 2-way and 3-way file comparison,
76 ;; merging, and directory operations.
80 ;;; Bugs:
82 ;; 1. The undo command doesn't restore deleted regions well. That is, if
83 ;; you delete all characters in a difference region and then invoke
84 ;; `undo', the reinstated text will most likely be inserted outside of
85 ;; what Ediff thinks is the current difference region. (This problem
86 ;; doesn't seem to exist with XEmacs.)
88 ;; If at any point you feel that difference regions are no longer correct,
89 ;; you can hit '!' to recompute the differences.
91 ;; 2. On a monochrome display, the repertoire of faces with which to
92 ;; highlight fine differences is limited. By default, Ediff is using
93 ;; underlining. However, if the region is already underlined by some other
94 ;; overlays, there is no simple way to temporarily remove that residual
95 ;; underlining. This problem occurs when a buffer is highlighted with
96 ;; hilit19.el or font-lock.el packages. If this residual highlighting gets
97 ;; in the way, you can do the following. Both font-lock.el and hilit19.el
98 ;; provide commands for unhighlighting buffers. You can either place these
99 ;; commands in `ediff-prepare-buffer-hook' (which will unhighlight every
100 ;; buffer used by Ediff) or you can execute them interactively, at any time
101 ;; and on any buffer.
104 ;;; Acknowledgements:
106 ;; Ediff was inspired by Dale R. Worley's <drw@math.mit.edu> emerge.el.
107 ;; Ediff would not have been possible without the help and encouragement of
108 ;; its many users. See Ediff on-line Info for the full list of those who
109 ;; helped. Improved defaults in Ediff file-name reading commands.
111 ;;; Code:
113 (provide 'ediff)
115 ;; Compiler pacifier
116 (eval-and-compile
117 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
120 (eval-when-compile
121 (require 'dired)
122 (require 'ediff-util)
123 (require 'ediff-ptch))
124 ;; end pacifier
126 (require 'ediff-init)
127 (require 'ediff-mult) ; required because of the registry stuff
129 (defgroup ediff nil
130 "A comprehensive visual interface to diff & patch."
131 :tag "Ediff"
132 :group 'tools)
135 (defcustom ediff-use-last-dir nil
136 "*If t, Ediff will use previous directory as default when reading file name."
137 :type 'boolean
138 :group 'ediff)
140 ;; Last directory used by an Ediff command for file-A.
141 (defvar ediff-last-dir-A nil)
142 ;; Last directory used by an Ediff command for file-B.
143 (defvar ediff-last-dir-B nil)
144 ;; Last directory used by an Ediff command for file-C.
145 (defvar ediff-last-dir-C nil)
146 ;; Last directory used by an Ediff command for the ancestor file.
147 (defvar ediff-last-dir-ancestor nil)
148 ;; Last directory used by an Ediff command as the output directory for merge.
149 (defvar ediff-last-merge-autostore-dir nil)
152 ;; Used as a startup hook to set `_orig' patch file read-only.
153 (defun ediff-set-read-only-in-buf-A ()
154 (ediff-with-current-buffer ediff-buffer-A
155 (toggle-read-only 1)))
157 ;; Return a plausible default for ediff's first file:
158 ;; In dired, return the file number FILENO (or 0) in the list
159 ;; (all-selected-files, filename under the cursor), where directories are
160 ;; ignored. Otherwise, return DEFAULT file name, if non-nil. Else,
161 ;; if the buffer is visiting a file, return that file name.
162 (defun ediff-get-default-file-name (&optional default fileno)
163 (cond ((eq major-mode 'dired-mode)
164 (let ((current (dired-get-filename nil 'no-error))
165 (marked (condition-case nil
166 (dired-get-marked-files 'no-dir)
167 (error nil)))
168 aux-list choices result)
169 (or (integerp fileno) (setq fileno 0))
170 (if (stringp default)
171 (setq aux-list (cons default aux-list)))
172 (if (and (stringp current) (not (file-directory-p current)))
173 (setq aux-list (cons current aux-list)))
174 (setq choices (nconc marked aux-list))
175 (setq result (elt choices fileno))
176 (or result
177 default)))
178 ((stringp default) default)
179 ((buffer-file-name (current-buffer))
180 (file-name-nondirectory (buffer-file-name (current-buffer))))
183 ;;; Compare files/buffers
185 ;;;###autoload
186 (defun ediff-files (file-A file-B &optional startup-hooks)
187 "Run Ediff on a pair of files, FILE-A and FILE-B."
188 (interactive
189 (let ((dir-A (if ediff-use-last-dir
190 ediff-last-dir-A
191 default-directory))
192 dir-B f)
193 (list (setq f (ediff-read-file-name
194 "File A to compare"
195 dir-A
196 (ediff-get-default-file-name)
197 'no-dirs))
198 (ediff-read-file-name "File B to compare"
199 (setq dir-B
200 (if ediff-use-last-dir
201 ediff-last-dir-B
202 (file-name-directory f)))
203 (progn
204 (ediff-add-to-history
205 'file-name-history
206 (ediff-abbreviate-file-name
207 (expand-file-name
208 (file-name-nondirectory f)
209 dir-B)))
210 (ediff-get-default-file-name f 1)))
212 (ediff-files-internal file-A
213 (if (file-directory-p file-B)
214 (expand-file-name
215 (file-name-nondirectory file-A) file-B)
216 file-B)
217 nil ; file-C
218 startup-hooks
219 'ediff-files))
221 ;;;###autoload
222 (defun ediff-files3 (file-A file-B file-C &optional startup-hooks)
223 "Run Ediff on three files, FILE-A, FILE-B, and FILE-C."
224 (interactive
225 (let ((dir-A (if ediff-use-last-dir
226 ediff-last-dir-A
227 default-directory))
228 dir-B dir-C f ff)
229 (list (setq f (ediff-read-file-name
230 "File A to compare"
231 dir-A
232 (ediff-get-default-file-name)
233 'no-dirs))
234 (setq ff (ediff-read-file-name "File B to compare"
235 (setq dir-B
236 (if ediff-use-last-dir
237 ediff-last-dir-B
238 (file-name-directory f)))
239 (progn
240 (ediff-add-to-history
241 'file-name-history
242 (ediff-abbreviate-file-name
243 (expand-file-name
244 (file-name-nondirectory f)
245 dir-B)))
246 (ediff-get-default-file-name f 1))))
247 (ediff-read-file-name "File C to compare"
248 (setq dir-C (if ediff-use-last-dir
249 ediff-last-dir-C
250 (file-name-directory ff)))
251 (progn
252 (ediff-add-to-history
253 'file-name-history
254 (ediff-abbreviate-file-name
255 (expand-file-name
256 (file-name-nondirectory ff)
257 dir-C)))
258 (ediff-get-default-file-name ff 2)))
260 (ediff-files-internal file-A
261 (if (file-directory-p file-B)
262 (expand-file-name
263 (file-name-nondirectory file-A) file-B)
264 file-B)
265 (if (file-directory-p file-C)
266 (expand-file-name
267 (file-name-nondirectory file-A) file-C)
268 file-C)
269 startup-hooks
270 'ediff-files3))
272 ;;;###autoload
273 (defalias 'ediff3 'ediff-files3)
276 ;; Visit FILE and arrange its buffer to Ediff's liking.
277 ;; FILE is actually a variable symbol that must contain a true file name.
278 ;; BUFFER-NAME is a variable symbol, which will get the buffer object into
279 ;; which FILE is read.
280 ;; LAST-DIR is the directory variable symbol where FILE's
281 ;; directory name should be returned. HOOKS-VAR is a variable symbol that will
282 ;; be assigned the hook to be executed after `ediff-startup' is finished.
283 ;; `ediff-find-file' arranges that the temp files it might create will be
284 ;; deleted.
285 (defun ediff-find-file (file-var buffer-name &optional last-dir hooks-var)
286 (let* ((file (symbol-value file-var))
287 (file-magic (ediff-filename-magic-p file))
288 (temp-file-name-prefix (file-name-nondirectory file)))
289 (cond ((not (file-readable-p file))
290 (error "File `%s' does not exist or is not readable" file))
291 ((file-directory-p file)
292 (error "File `%s' is a directory" file)))
294 ;; some of the commands, below, require full file name
295 (setq file (expand-file-name file))
297 ;; Record the directory of the file
298 (if last-dir
299 (set last-dir (expand-file-name (file-name-directory file))))
301 ;; Setup the buffer
302 (set buffer-name (find-file-noselect file))
304 (ediff-with-current-buffer (symbol-value buffer-name)
305 (widen) ; Make sure the entire file is seen
306 (cond (file-magic ; file has a handler, such as jka-compr-handler or
307 ;;; ange-ftp-hook-function--arrange for temp file
308 (ediff-verify-file-buffer 'magic)
309 (setq file
310 (ediff-make-temp-file
311 (current-buffer) temp-file-name-prefix))
312 (set hooks-var (cons `(lambda () (delete-file ,file))
313 (symbol-value hooks-var))))
314 ;; file processed via auto-mode-alist, a la uncompress.el
315 ((not (equal (file-truename file)
316 (file-truename (buffer-file-name))))
317 (setq file
318 (ediff-make-temp-file
319 (current-buffer) temp-file-name-prefix))
320 (set hooks-var (cons `(lambda () (delete-file ,file))
321 (symbol-value hooks-var))))
322 (t ;; plain file---just check that the file matches the buffer
323 (ediff-verify-file-buffer))))
324 (set file-var file)))
326 ;; MERGE-BUFFER-FILE is the file to be associated with the merge buffer
327 (defun ediff-files-internal (file-A file-B file-C startup-hooks job-name
328 &optional merge-buffer-file)
329 (let (buf-A buf-B buf-C)
330 (if (string= file-A file-B)
331 (error "Files A and B are the same"))
332 (if (stringp file-C)
333 (or (and (string= file-A file-C) (error "Files A and C are the same"))
334 (and (string= file-B file-C) (error "Files B and C are the same"))))
335 (message "Reading file %s ... " file-A)
336 ;;(sit-for 0)
337 (ediff-find-file 'file-A 'buf-A 'ediff-last-dir-A 'startup-hooks)
338 (message "Reading file %s ... " file-B)
339 ;;(sit-for 0)
340 (ediff-find-file 'file-B 'buf-B 'ediff-last-dir-B 'startup-hooks)
341 (if (stringp file-C)
342 (progn
343 (message "Reading file %s ... " file-C)
344 ;;(sit-for 0)
345 (ediff-find-file
346 'file-C 'buf-C
347 (if (eq job-name 'ediff-merge-files-with-ancestor)
348 'ediff-last-dir-ancestor 'ediff-last-dir-C)
349 'startup-hooks)))
350 (ediff-setup buf-A file-A
351 buf-B file-B
352 buf-C file-C
353 startup-hooks
354 (list (cons 'ediff-job-name job-name))
355 merge-buffer-file)))
357 (declare-function diff-latest-backup-file "diff" (fn))
359 ;;;###autoload
360 (defalias 'ediff 'ediff-files)
362 ;;;###autoload
363 (defun ediff-backup (file)
364 "Run Ediff on FILE and its backup file.
365 Uses the latest backup, if there are several numerical backups.
366 If this file is a backup, `ediff' it with its original."
367 (interactive (list (read-file-name "Ediff (file with backup): ")))
368 ;; The code is taken from `diff-backup'.
369 (require 'diff)
370 (let (bak ori)
371 (if (backup-file-name-p file)
372 (setq bak file
373 ori (file-name-sans-versions file))
374 (setq bak (or (diff-latest-backup-file file)
375 (error "No backup found for %s" file))
376 ori file))
377 (ediff-files bak ori)))
379 ;;;###autoload
380 (defun ediff-buffers (buffer-A buffer-B &optional startup-hooks job-name)
381 "Run Ediff on a pair of buffers, BUFFER-A and BUFFER-B."
382 (interactive
383 (let (bf)
384 (list (setq bf (read-buffer "Buffer A to compare: "
385 (ediff-other-buffer "") t))
386 (read-buffer "Buffer B to compare: "
387 (progn
388 ;; realign buffers so that two visible bufs will be
389 ;; at the top
390 (save-window-excursion (other-window 1))
391 (ediff-other-buffer bf))
392 t))))
393 (or job-name (setq job-name 'ediff-buffers))
394 (ediff-buffers-internal buffer-A buffer-B nil startup-hooks job-name))
396 ;;;###autoload
397 (defalias 'ebuffers 'ediff-buffers)
400 ;;;###autoload
401 (defun ediff-buffers3 (buffer-A buffer-B buffer-C
402 &optional startup-hooks job-name)
403 "Run Ediff on three buffers, BUFFER-A, BUFFER-B, and BUFFER-C."
404 (interactive
405 (let (bf bff)
406 (list (setq bf (read-buffer "Buffer A to compare: "
407 (ediff-other-buffer "") t))
408 (setq bff (read-buffer "Buffer B to compare: "
409 (progn
410 ;; realign buffers so that two visible
411 ;; bufs will be at the top
412 (save-window-excursion (other-window 1))
413 (ediff-other-buffer bf))
415 (read-buffer "Buffer C to compare: "
416 (progn
417 ;; realign buffers so that three visible
418 ;; bufs will be at the top
419 (save-window-excursion (other-window 1))
420 (ediff-other-buffer (list bf bff)))
423 (or job-name (setq job-name 'ediff-buffers3))
424 (ediff-buffers-internal buffer-A buffer-B buffer-C startup-hooks job-name))
426 ;;;###autoload
427 (defalias 'ebuffers3 'ediff-buffers3)
431 ;; MERGE-BUFFER-FILE is the file to be associated with the merge buffer
432 (defun ediff-buffers-internal (buf-A buf-B buf-C startup-hooks job-name
433 &optional merge-buffer-file)
434 (let* ((buf-A-file-name (buffer-file-name (get-buffer buf-A)))
435 (buf-B-file-name (buffer-file-name (get-buffer buf-B)))
436 (buf-C-is-alive (ediff-buffer-live-p buf-C))
437 (buf-C-file-name (if buf-C-is-alive
438 (buffer-file-name (get-buffer buf-B))))
439 file-A file-B file-C)
440 (if (not (ediff-buffer-live-p buf-A))
441 (error "Buffer %S doesn't exist" buf-A))
442 (if (not (ediff-buffer-live-p buf-B))
443 (error "Buffer %S doesn't exist" buf-B))
444 (let ((ediff-job-name job-name))
445 (if (and ediff-3way-comparison-job
446 (not buf-C-is-alive))
447 (error "Buffer %S doesn't exist" buf-C)))
448 (if (stringp buf-A-file-name)
449 (setq buf-A-file-name (file-name-nondirectory buf-A-file-name)))
450 (if (stringp buf-B-file-name)
451 (setq buf-B-file-name (file-name-nondirectory buf-B-file-name)))
452 (if (stringp buf-C-file-name)
453 (setq buf-C-file-name (file-name-nondirectory buf-C-file-name)))
455 (setq file-A (ediff-make-temp-file buf-A buf-A-file-name)
456 file-B (ediff-make-temp-file buf-B buf-B-file-name))
457 (if buf-C-is-alive
458 (setq file-C (ediff-make-temp-file buf-C buf-C-file-name)))
460 (ediff-setup (get-buffer buf-A) file-A
461 (get-buffer buf-B) file-B
462 (if buf-C-is-alive (get-buffer buf-C))
463 file-C
464 (cons `(lambda ()
465 (delete-file ,file-A)
466 (delete-file ,file-B)
467 (if (stringp ,file-C) (delete-file ,file-C)))
468 startup-hooks)
469 (list (cons 'ediff-job-name job-name))
470 merge-buffer-file)))
473 ;;; Directory and file group operations
475 ;; Get appropriate default name for directory:
476 ;; If ediff-use-last-dir, use ediff-last-dir-A.
477 ;; In dired mode, use the directory that is under the point (if any);
478 ;; otherwise, use default-directory
479 (defun ediff-get-default-directory-name ()
480 (cond (ediff-use-last-dir ediff-last-dir-A)
481 ((eq major-mode 'dired-mode)
482 (let ((f (dired-get-filename nil 'noerror)))
483 (if (and (stringp f) (file-directory-p f))
485 default-directory)))
486 (t default-directory)))
489 ;;;###autoload
490 (defun ediff-directories (dir1 dir2 regexp)
491 "Run Ediff on a pair of directories, DIR1 and DIR2, comparing files that have
492 the same name in both. The third argument, REGEXP, is nil or a regular
493 expression; only file names that match the regexp are considered."
494 (interactive
495 (let ((dir-A (ediff-get-default-directory-name))
496 (default-regexp (eval ediff-default-filtering-regexp))
498 (list (setq f (read-directory-name
499 "Directory A to compare:" dir-A nil 'must-match))
500 (read-directory-name "Directory B to compare:"
501 (if ediff-use-last-dir
502 ediff-last-dir-B
503 (ediff-strip-last-dir f))
504 nil 'must-match)
505 (read-string
506 (if (stringp default-regexp)
507 (format "Filter through regular expression (default %s): "
508 default-regexp)
509 "Filter through regular expression: ")
511 'ediff-filtering-regexp-history
512 (eval ediff-default-filtering-regexp))
514 (ediff-directories-internal
515 dir1 dir2 nil regexp 'ediff-files 'ediff-directories
518 ;;;###autoload
519 (defalias 'edirs 'ediff-directories)
522 ;;;###autoload
523 (defun ediff-directory-revisions (dir1 regexp)
524 "Run Ediff on a directory, DIR1, comparing its files with their revisions.
525 The second argument, REGEXP, is a regular expression that filters the file
526 names. Only the files that are under revision control are taken into account."
527 (interactive
528 (let ((dir-A (ediff-get-default-directory-name))
529 (default-regexp (eval ediff-default-filtering-regexp))
531 (list (read-directory-name
532 "Directory to compare with revision:" dir-A nil 'must-match)
533 (read-string
534 (if (stringp default-regexp)
535 (format "Filter through regular expression (default %s): "
536 default-regexp)
537 "Filter through regular expression: ")
539 'ediff-filtering-regexp-history
540 (eval ediff-default-filtering-regexp))
542 (ediff-directory-revisions-internal
543 dir1 regexp 'ediff-revision 'ediff-directory-revisions
546 ;;;###autoload
547 (defalias 'edir-revisions 'ediff-directory-revisions)
550 ;;;###autoload
551 (defun ediff-directories3 (dir1 dir2 dir3 regexp)
552 "Run Ediff on three directories, DIR1, DIR2, and DIR3, comparing files that
553 have the same name in all three. The last argument, REGEXP, is nil or a
554 regular expression; only file names that match the regexp are considered."
556 (interactive
557 (let ((dir-A (ediff-get-default-directory-name))
558 (default-regexp (eval ediff-default-filtering-regexp))
560 (list (setq f (read-directory-name "Directory A to compare:" dir-A nil))
561 (setq f (read-directory-name "Directory B to compare:"
562 (if ediff-use-last-dir
563 ediff-last-dir-B
564 (ediff-strip-last-dir f))
565 nil 'must-match))
566 (read-directory-name "Directory C to compare:"
567 (if ediff-use-last-dir
568 ediff-last-dir-C
569 (ediff-strip-last-dir f))
570 nil 'must-match)
571 (read-string
572 (if (stringp default-regexp)
573 (format "Filter through regular expression (default %s): "
574 default-regexp)
575 "Filter through regular expression: ")
577 'ediff-filtering-regexp-history
578 (eval ediff-default-filtering-regexp))
580 (ediff-directories-internal
581 dir1 dir2 dir3 regexp 'ediff-files3 'ediff-directories3
584 ;;;###autoload
585 (defalias 'edirs3 'ediff-directories3)
587 ;;;###autoload
588 (defun ediff-merge-directories (dir1 dir2 regexp &optional merge-autostore-dir)
589 "Run Ediff on a pair of directories, DIR1 and DIR2, merging files that have
590 the same name in both. The third argument, REGEXP, is nil or a regular
591 expression; only file names that match the regexp are considered."
592 (interactive
593 (let ((dir-A (ediff-get-default-directory-name))
594 (default-regexp (eval ediff-default-filtering-regexp))
596 (list (setq f (read-directory-name "Directory A to merge:"
597 dir-A nil 'must-match))
598 (read-directory-name "Directory B to merge:"
599 (if ediff-use-last-dir
600 ediff-last-dir-B
601 (ediff-strip-last-dir f))
602 nil 'must-match)
603 (read-string
604 (if (stringp default-regexp)
605 (format "Filter through regular expression (default %s): "
606 default-regexp)
607 "Filter through regular expression: ")
609 'ediff-filtering-regexp-history
610 (eval ediff-default-filtering-regexp))
612 (ediff-directories-internal
613 dir1 dir2 nil regexp 'ediff-merge-files 'ediff-merge-directories
614 nil merge-autostore-dir
617 ;;;###autoload
618 (defalias 'edirs-merge 'ediff-merge-directories)
620 ;;;###autoload
621 (defun ediff-merge-directories-with-ancestor (dir1 dir2 ancestor-dir regexp
622 &optional
623 merge-autostore-dir)
624 "Merge files in directories DIR1 and DIR2 using files in ANCESTOR-DIR as ancestors.
625 Ediff merges files that have identical names in DIR1, DIR2. If a pair of files
626 in DIR1 and DIR2 doesn't have an ancestor in ANCESTOR-DIR, Ediff will merge
627 without ancestor. The fourth argument, REGEXP, is nil or a regular expression;
628 only file names that match the regexp are considered."
629 (interactive
630 (let ((dir-A (ediff-get-default-directory-name))
631 (default-regexp (eval ediff-default-filtering-regexp))
633 (list (setq f (read-directory-name "Directory A to merge:" dir-A nil))
634 (setq f (read-directory-name "Directory B to merge:"
635 (if ediff-use-last-dir
636 ediff-last-dir-B
637 (ediff-strip-last-dir f))
638 nil 'must-match))
639 (read-directory-name "Ancestor directory:"
640 (if ediff-use-last-dir
641 ediff-last-dir-C
642 (ediff-strip-last-dir f))
643 nil 'must-match)
644 (read-string
645 (if (stringp default-regexp)
646 (format "Filter through regular expression (default %s): "
647 default-regexp)
648 "Filter through regular expression: ")
650 'ediff-filtering-regexp-history
651 (eval ediff-default-filtering-regexp))
653 (ediff-directories-internal
654 dir1 dir2 ancestor-dir regexp
655 'ediff-merge-files-with-ancestor 'ediff-merge-directories-with-ancestor
656 nil merge-autostore-dir
659 ;;;###autoload
660 (defun ediff-merge-directory-revisions (dir1 regexp
661 &optional merge-autostore-dir)
662 "Run Ediff on a directory, DIR1, merging its files with their revisions.
663 The second argument, REGEXP, is a regular expression that filters the file
664 names. Only the files that are under revision control are taken into account."
665 (interactive
666 (let ((dir-A (ediff-get-default-directory-name))
667 (default-regexp (eval ediff-default-filtering-regexp))
669 (list (read-directory-name
670 "Directory to merge with revisions:" dir-A nil 'must-match)
671 (read-string
672 (if (stringp default-regexp)
673 (format "Filter through regular expression (default %s): "
674 default-regexp)
675 "Filter through regular expression: ")
677 'ediff-filtering-regexp-history
678 (eval ediff-default-filtering-regexp))
680 (ediff-directory-revisions-internal
681 dir1 regexp 'ediff-merge-revisions 'ediff-merge-directory-revisions
682 nil merge-autostore-dir
685 ;;;###autoload
686 (defalias 'edir-merge-revisions 'ediff-merge-directory-revisions)
688 ;;;###autoload
689 (defun ediff-merge-directory-revisions-with-ancestor (dir1 regexp
690 &optional
691 merge-autostore-dir)
692 "Run Ediff on a directory, DIR1, merging its files with their revisions and ancestors.
693 The second argument, REGEXP, is a regular expression that filters the file
694 names. Only the files that are under revision control are taken into account."
695 (interactive
696 (let ((dir-A (ediff-get-default-directory-name))
697 (default-regexp (eval ediff-default-filtering-regexp))
699 (list (read-directory-name
700 "Directory to merge with revisions and ancestors:"
701 dir-A nil 'must-match)
702 (read-string
703 (if (stringp default-regexp)
704 (format "Filter through regular expression (default %s): "
705 default-regexp)
706 "Filter through regular expression: ")
708 'ediff-filtering-regexp-history
709 (eval ediff-default-filtering-regexp))
711 (ediff-directory-revisions-internal
712 dir1 regexp 'ediff-merge-revisions-with-ancestor
713 'ediff-merge-directory-revisions-with-ancestor
714 nil merge-autostore-dir
717 ;;;###autoload
718 (defalias
719 'edir-merge-revisions-with-ancestor
720 'ediff-merge-directory-revisions-with-ancestor)
722 ;;;###autoload
723 (defalias 'edirs-merge-with-ancestor 'ediff-merge-directories-with-ancestor)
725 ;; Run ediff-action (ediff-files, ediff-merge, ediff-merge-with-ancestors)
726 ;; on a pair of directories (three directories, in case of ancestor).
727 ;; The third argument, REGEXP, is nil or a regular expression;
728 ;; only file names that match the regexp are considered.
729 ;; JOBNAME is the symbol indicating the meta-job to be performed.
730 ;; MERGE-AUTOSTORE-DIR is the directory in which to store merged files.
731 (defun ediff-directories-internal (dir1 dir2 dir3 regexp action jobname
732 &optional startup-hooks
733 merge-autostore-dir)
734 (if (stringp dir3)
735 (setq dir3 (if (file-directory-p dir3) dir3 (file-name-directory dir3))))
737 (cond ((string= dir1 dir2)
738 (error "Directories A and B are the same: %s" dir1))
739 ((and (eq jobname 'ediff-directories3)
740 (string= dir1 dir3))
741 (error "Directories A and C are the same: %s" dir1))
742 ((and (eq jobname 'ediff-directories3)
743 (string= dir2 dir3))
744 (error "Directories B and C are the same: %s" dir1)))
746 (if merge-autostore-dir
747 (or (stringp merge-autostore-dir)
748 (error "%s: Directory for storing merged files must be a string"
749 jobname)))
750 (let (;; dir-diff-struct is of the form (common-list diff-list)
751 ;; It is a structure where ediff-intersect-directories returns
752 ;; commonalities and differences among directories
753 dir-diff-struct
754 meta-buf)
755 (if (and ediff-autostore-merges
756 (ediff-merge-metajob jobname)
757 (not merge-autostore-dir))
758 (setq merge-autostore-dir
759 (read-directory-name "Save merged files in directory: "
760 (if ediff-use-last-dir
761 ediff-last-merge-autostore-dir
762 (ediff-strip-last-dir dir1))
764 'must-match)))
765 ;; verify we are not merging into an orig directory
766 (if merge-autostore-dir
767 (cond ((and (stringp dir1) (string= merge-autostore-dir dir1))
768 (or (y-or-n-p
769 "Directory for saving merged files = Directory A. Sure? ")
770 (error "Directory merge aborted")))
771 ((and (stringp dir2) (string= merge-autostore-dir dir2))
772 (or (y-or-n-p
773 "Directory for saving merged files = Directory B. Sure? ")
774 (error "Directory merge aborted")))
775 ((and (stringp dir3) (string= merge-autostore-dir dir3))
776 (or (y-or-n-p
777 "Directory for saving merged files = Ancestor Directory. Sure? ")
778 (error "Directory merge aborted")))))
780 (setq dir-diff-struct (ediff-intersect-directories
781 jobname
782 regexp dir1 dir2 dir3 merge-autostore-dir))
783 (setq startup-hooks
784 ;; this sets various vars in the meta buffer inside
785 ;; ediff-prepare-meta-buffer
786 (cons `(lambda ()
787 ;; tell what to do if the user clicks on a session record
788 (setq ediff-session-action-function (quote ,action))
789 ;; set ediff-dir-difference-list
790 (setq ediff-dir-difference-list
791 (cdr (quote ,dir-diff-struct))))
792 startup-hooks))
793 (setq meta-buf (ediff-prepare-meta-buffer
794 'ediff-filegroup-action
795 (car dir-diff-struct)
796 "*Ediff Session Group Panel"
797 'ediff-redraw-directory-group-buffer
798 jobname
799 startup-hooks))
800 (ediff-show-meta-buffer meta-buf)
803 ;; MERGE-AUTOSTORE-DIR can be given to tell ediff where to store the merged
804 ;; files
805 (defun ediff-directory-revisions-internal (dir1 regexp action jobname
806 &optional startup-hooks
807 merge-autostore-dir)
808 (setq dir1 (if (file-directory-p dir1) dir1 (file-name-directory dir1)))
810 (if merge-autostore-dir
811 (or (stringp merge-autostore-dir)
812 (error "%S: Directory for storing merged files must be a string"
813 jobname)))
814 (let (file-list meta-buf)
815 (if (and ediff-autostore-merges
816 (ediff-merge-metajob jobname)
817 (not merge-autostore-dir))
818 (setq merge-autostore-dir
819 (read-directory-name "Save merged files in directory: "
820 (if ediff-use-last-dir
821 ediff-last-merge-autostore-dir
822 (ediff-strip-last-dir dir1))
824 'must-match)))
825 ;; verify merge-autostore-dir != dir1
826 (if (and merge-autostore-dir
827 (stringp dir1)
828 (string= merge-autostore-dir dir1))
829 (or (y-or-n-p
830 "Directory for saving merged file = directory A. Sure? ")
831 (error "Merge of directory revisions aborted")))
833 (setq file-list
834 (ediff-get-directory-files-under-revision
835 jobname regexp dir1 merge-autostore-dir))
836 (setq startup-hooks
837 ;; this sets various vars in the meta buffer inside
838 ;; ediff-prepare-meta-buffer
839 (cons `(lambda ()
840 ;; tell what to do if the user clicks on a session record
841 (setq ediff-session-action-function (quote ,action)))
842 startup-hooks))
843 (setq meta-buf (ediff-prepare-meta-buffer
844 'ediff-filegroup-action
845 file-list
846 "*Ediff Session Group Panel"
847 'ediff-redraw-directory-group-buffer
848 jobname
849 startup-hooks))
850 (ediff-show-meta-buffer meta-buf)
854 ;;; Compare regions and windows
856 ;;;###autoload
857 (defun ediff-windows-wordwise (dumb-mode &optional wind-A wind-B startup-hooks)
858 "Compare WIND-A and WIND-B, which are selected by clicking, wordwise.
859 With prefix argument, DUMB-MODE, or on a non-windowing display, works as
860 follows:
861 If WIND-A is nil, use selected window.
862 If WIND-B is nil, use window next to WIND-A."
863 (interactive "P")
864 (ediff-windows dumb-mode wind-A wind-B
865 startup-hooks 'ediff-windows-wordwise 'word-mode))
867 ;;;###autoload
868 (defun ediff-windows-linewise (dumb-mode &optional wind-A wind-B startup-hooks)
869 "Compare WIND-A and WIND-B, which are selected by clicking, linewise.
870 With prefix argument, DUMB-MODE, or on a non-windowing display, works as
871 follows:
872 If WIND-A is nil, use selected window.
873 If WIND-B is nil, use window next to WIND-A."
874 (interactive "P")
875 (ediff-windows dumb-mode wind-A wind-B
876 startup-hooks 'ediff-windows-linewise nil))
878 ;; Compare WIND-A and WIND-B, which are selected by clicking.
879 ;; With prefix argument, DUMB-MODE, or on a non-windowing display,
880 ;; works as follows:
881 ;; If WIND-A is nil, use selected window.
882 ;; If WIND-B is nil, use window next to WIND-A.
883 (defun ediff-windows (dumb-mode wind-A wind-B startup-hooks job-name word-mode)
884 (if (or dumb-mode (not (ediff-window-display-p)))
885 (setq wind-A (ediff-get-next-window wind-A nil)
886 wind-B (ediff-get-next-window wind-B wind-A))
887 (setq wind-A (ediff-get-window-by-clicking wind-A nil 1)
888 wind-B (ediff-get-window-by-clicking wind-B wind-A 2)))
890 (let ((buffer-A (window-buffer wind-A))
891 (buffer-B (window-buffer wind-B))
892 beg-A end-A beg-B end-B)
894 (save-excursion
895 (save-window-excursion
896 (sit-for 0) ; sync before using window-start/end -- a precaution
897 (select-window wind-A)
898 (setq beg-A (window-start)
899 end-A (window-end))
900 (select-window wind-B)
901 (setq beg-B (window-start)
902 end-B (window-end))))
903 (setq buffer-A
904 (ediff-clone-buffer-for-window-comparison
905 buffer-A wind-A "-Window.A-")
906 buffer-B
907 (ediff-clone-buffer-for-window-comparison
908 buffer-B wind-B "-Window.B-"))
909 (ediff-regions-internal
910 buffer-A beg-A end-A buffer-B beg-B end-B
911 startup-hooks job-name word-mode nil)))
914 ;;;###autoload
915 (defun ediff-regions-wordwise (buffer-A buffer-B &optional startup-hooks)
916 "Run Ediff on a pair of regions in specified buffers.
917 Regions \(i.e., point and mark\) can be set in advance or marked interactively.
918 This function is effective only for relatively small regions, up to 200
919 lines. For large regions, use `ediff-regions-linewise'."
920 (interactive
921 (let (bf)
922 (list (setq bf (read-buffer "Region's A buffer: "
923 (ediff-other-buffer "") t))
924 (read-buffer "Region's B buffer: "
925 (progn
926 ;; realign buffers so that two visible bufs will be
927 ;; at the top
928 (save-window-excursion (other-window 1))
929 (ediff-other-buffer bf))
930 t))))
931 (if (not (ediff-buffer-live-p buffer-A))
932 (error "Buffer %S doesn't exist" buffer-A))
933 (if (not (ediff-buffer-live-p buffer-B))
934 (error "Buffer %S doesn't exist" buffer-B))
937 (let ((buffer-A
938 (ediff-clone-buffer-for-region-comparison buffer-A "-Region.A-"))
939 (buffer-B
940 (ediff-clone-buffer-for-region-comparison buffer-B "-Region.B-"))
941 reg-A-beg reg-A-end reg-B-beg reg-B-end)
942 (save-excursion
943 (set-buffer buffer-A)
944 (setq reg-A-beg (region-beginning)
945 reg-A-end (region-end))
946 (set-buffer buffer-B)
947 (setq reg-B-beg (region-beginning)
948 reg-B-end (region-end)))
950 (ediff-regions-internal
951 (get-buffer buffer-A) reg-A-beg reg-A-end
952 (get-buffer buffer-B) reg-B-beg reg-B-end
953 startup-hooks 'ediff-regions-wordwise 'word-mode nil)))
955 ;;;###autoload
956 (defun ediff-regions-linewise (buffer-A buffer-B &optional startup-hooks)
957 "Run Ediff on a pair of regions in specified buffers.
958 Regions \(i.e., point and mark\) can be set in advance or marked interactively.
959 Each region is enlarged to contain full lines.
960 This function is effective for large regions, over 100-200
961 lines. For small regions, use `ediff-regions-wordwise'."
962 (interactive
963 (let (bf)
964 (list (setq bf (read-buffer "Region A's buffer: "
965 (ediff-other-buffer "") t))
966 (read-buffer "Region B's buffer: "
967 (progn
968 ;; realign buffers so that two visible bufs will be
969 ;; at the top
970 (save-window-excursion (other-window 1))
971 (ediff-other-buffer bf))
972 t))))
973 (if (not (ediff-buffer-live-p buffer-A))
974 (error "Buffer %S doesn't exist" buffer-A))
975 (if (not (ediff-buffer-live-p buffer-B))
976 (error "Buffer %S doesn't exist" buffer-B))
978 (let ((buffer-A
979 (ediff-clone-buffer-for-region-comparison buffer-A "-Region.A-"))
980 (buffer-B
981 (ediff-clone-buffer-for-region-comparison buffer-B "-Region.B-"))
982 reg-A-beg reg-A-end reg-B-beg reg-B-end)
983 (save-excursion
984 (set-buffer buffer-A)
985 (setq reg-A-beg (region-beginning)
986 reg-A-end (region-end))
987 ;; enlarge the region to hold full lines
988 (goto-char reg-A-beg)
989 (beginning-of-line)
990 (setq reg-A-beg (point))
991 (goto-char reg-A-end)
992 (end-of-line)
993 (or (eobp) (forward-char)) ; include the newline char
994 (setq reg-A-end (point))
996 (set-buffer buffer-B)
997 (setq reg-B-beg (region-beginning)
998 reg-B-end (region-end))
999 ;; enlarge the region to hold full lines
1000 (goto-char reg-B-beg)
1001 (beginning-of-line)
1002 (setq reg-B-beg (point))
1003 (goto-char reg-B-end)
1004 (end-of-line)
1005 (or (eobp) (forward-char)) ; include the newline char
1006 (setq reg-B-end (point))
1007 ) ; save excursion
1009 (ediff-regions-internal
1010 (get-buffer buffer-A) reg-A-beg reg-A-end
1011 (get-buffer buffer-B) reg-B-beg reg-B-end
1012 startup-hooks 'ediff-regions-linewise nil nil))) ; no word mode
1014 ;; compare region beg-A to end-A of buffer-A
1015 ;; to regions beg-B -- end-B in buffer-B.
1016 (defun ediff-regions-internal (buffer-A beg-A end-A buffer-B beg-B end-B
1017 startup-hooks job-name word-mode
1018 setup-parameters)
1019 (let ((tmp-buffer (get-buffer-create ediff-tmp-buffer))
1020 overl-A overl-B
1021 file-A file-B)
1023 ;; in case beg/end-A/B aren't markers--make them into markers
1024 (ediff-with-current-buffer buffer-A
1025 (setq beg-A (move-marker (make-marker) beg-A)
1026 end-A (move-marker (make-marker) end-A)))
1027 (ediff-with-current-buffer buffer-B
1028 (setq beg-B (move-marker (make-marker) beg-B)
1029 end-B (move-marker (make-marker) end-B)))
1031 ;; make file-A
1032 (if word-mode
1033 (ediff-wordify beg-A end-A buffer-A tmp-buffer)
1034 (ediff-copy-to-buffer beg-A end-A buffer-A tmp-buffer))
1035 (setq file-A (ediff-make-temp-file tmp-buffer "regA"))
1037 ;; make file-B
1038 (if word-mode
1039 (ediff-wordify beg-B end-B buffer-B tmp-buffer)
1040 (ediff-copy-to-buffer beg-B end-B buffer-B tmp-buffer))
1041 (setq file-B (ediff-make-temp-file tmp-buffer "regB"))
1043 (setq overl-A (ediff-make-bullet-proof-overlay beg-A end-A buffer-A))
1044 (setq overl-B (ediff-make-bullet-proof-overlay beg-B end-B buffer-B))
1045 (ediff-setup buffer-A file-A
1046 buffer-B file-B
1047 nil nil ; buffer & file C
1048 (cons `(lambda ()
1049 (delete-file ,file-A)
1050 (delete-file ,file-B))
1051 startup-hooks)
1052 (append
1053 (list (cons 'ediff-word-mode word-mode)
1054 (cons 'ediff-narrow-bounds (list overl-A overl-B))
1055 (cons 'ediff-job-name job-name))
1056 setup-parameters))
1060 ;;; Merge files and buffers
1062 ;;;###autoload
1063 (defalias 'ediff-merge 'ediff-merge-files)
1065 (defsubst ediff-merge-on-startup ()
1066 (ediff-do-merge 0)
1067 ;; Can't remember why this is here, but it may cause the automatically merged
1068 ;; buffer to be lost. So, keep the buffer modified.
1069 ;;(ediff-with-current-buffer ediff-buffer-C
1070 ;; (set-buffer-modified-p nil))
1073 ;;;###autoload
1074 (defun ediff-merge-files (file-A file-B
1075 ;; MERGE-BUFFER-FILE is the file to be
1076 ;; associated with the merge buffer
1077 &optional startup-hooks merge-buffer-file)
1078 "Merge two files without ancestor."
1079 (interactive
1080 (let ((dir-A (if ediff-use-last-dir
1081 ediff-last-dir-A
1082 default-directory))
1083 dir-B f)
1084 (list (setq f (ediff-read-file-name
1085 "File A to merge"
1086 dir-A
1087 (ediff-get-default-file-name)
1088 'no-dirs))
1089 (ediff-read-file-name "File B to merge"
1090 (setq dir-B
1091 (if ediff-use-last-dir
1092 ediff-last-dir-B
1093 (file-name-directory f)))
1094 (progn
1095 (ediff-add-to-history
1096 'file-name-history
1097 (ediff-abbreviate-file-name
1098 (expand-file-name
1099 (file-name-nondirectory f)
1100 dir-B)))
1101 (ediff-get-default-file-name f 1)))
1103 (setq startup-hooks (cons 'ediff-merge-on-startup startup-hooks))
1104 (ediff-files-internal file-A
1105 (if (file-directory-p file-B)
1106 (expand-file-name
1107 (file-name-nondirectory file-A) file-B)
1108 file-B)
1109 nil ; file-C
1110 startup-hooks
1111 'ediff-merge-files
1112 merge-buffer-file))
1114 ;;;###autoload
1115 (defun ediff-merge-files-with-ancestor (file-A file-B file-ancestor
1116 &optional
1117 startup-hooks
1118 ;; MERGE-BUFFER-FILE is the file
1119 ;; to be associated with the
1120 ;; merge buffer
1121 merge-buffer-file)
1122 "Merge two files with ancestor."
1123 (interactive
1124 (let ((dir-A (if ediff-use-last-dir
1125 ediff-last-dir-A
1126 default-directory))
1127 dir-B dir-ancestor f ff)
1128 (list (setq f (ediff-read-file-name
1129 "File A to merge"
1130 dir-A
1131 (ediff-get-default-file-name)
1132 'no-dirs))
1133 (setq ff (ediff-read-file-name "File B to merge"
1134 (setq dir-B
1135 (if ediff-use-last-dir
1136 ediff-last-dir-B
1137 (file-name-directory f)))
1138 (progn
1139 (ediff-add-to-history
1140 'file-name-history
1141 (ediff-abbreviate-file-name
1142 (expand-file-name
1143 (file-name-nondirectory f)
1144 dir-B)))
1145 (ediff-get-default-file-name f 1))))
1146 (ediff-read-file-name "Ancestor file"
1147 (setq dir-ancestor
1148 (if ediff-use-last-dir
1149 ediff-last-dir-ancestor
1150 (file-name-directory ff)))
1151 (progn
1152 (ediff-add-to-history
1153 'file-name-history
1154 (ediff-abbreviate-file-name
1155 (expand-file-name
1156 (file-name-nondirectory ff)
1157 dir-ancestor)))
1158 (ediff-get-default-file-name ff 2)))
1160 (setq startup-hooks (cons 'ediff-merge-on-startup startup-hooks))
1161 (ediff-files-internal file-A
1162 (if (file-directory-p file-B)
1163 (expand-file-name
1164 (file-name-nondirectory file-A) file-B)
1165 file-B)
1166 file-ancestor
1167 startup-hooks
1168 'ediff-merge-files-with-ancestor
1169 merge-buffer-file))
1171 ;;;###autoload
1172 (defalias 'ediff-merge-with-ancestor 'ediff-merge-files-with-ancestor)
1174 ;;;###autoload
1175 (defun ediff-merge-buffers (buffer-A buffer-B
1176 &optional
1177 ;; MERGE-BUFFER-FILE is the file to be
1178 ;; associated with the merge buffer
1179 startup-hooks job-name merge-buffer-file)
1180 "Merge buffers without ancestor."
1181 (interactive
1182 (let (bf)
1183 (list (setq bf (read-buffer "Buffer A to merge: "
1184 (ediff-other-buffer "") t))
1185 (read-buffer "Buffer B to merge: "
1186 (progn
1187 ;; realign buffers so that two visible bufs will be
1188 ;; at the top
1189 (save-window-excursion (other-window 1))
1190 (ediff-other-buffer bf))
1191 t))))
1193 (setq startup-hooks (cons 'ediff-merge-on-startup startup-hooks))
1194 (or job-name (setq job-name 'ediff-merge-buffers))
1195 (ediff-buffers-internal
1196 buffer-A buffer-B nil startup-hooks job-name merge-buffer-file))
1198 ;;;###autoload
1199 (defun ediff-merge-buffers-with-ancestor (buffer-A buffer-B buffer-ancestor
1200 &optional
1201 startup-hooks
1202 job-name
1203 ;; MERGE-BUFFER-FILE is the
1204 ;; file to be associated
1205 ;; with the merge buffer
1206 merge-buffer-file)
1207 "Merge buffers with ancestor."
1208 (interactive
1209 (let (bf bff)
1210 (list (setq bf (read-buffer "Buffer A to merge: "
1211 (ediff-other-buffer "") t))
1212 (setq bff (read-buffer "Buffer B to merge: "
1213 (progn
1214 ;; realign buffers so that two visible
1215 ;; bufs will be at the top
1216 (save-window-excursion (other-window 1))
1217 (ediff-other-buffer bf))
1219 (read-buffer "Ancestor buffer: "
1220 (progn
1221 ;; realign buffers so that three visible
1222 ;; bufs will be at the top
1223 (save-window-excursion (other-window 1))
1224 (ediff-other-buffer (list bf bff)))
1228 (setq startup-hooks (cons 'ediff-merge-on-startup startup-hooks))
1229 (or job-name (setq job-name 'ediff-merge-buffers-with-ancestor))
1230 (ediff-buffers-internal
1231 buffer-A buffer-B buffer-ancestor startup-hooks job-name merge-buffer-file))
1234 ;;;###autoload
1235 (defun ediff-merge-revisions (&optional file startup-hooks merge-buffer-file)
1236 ;; MERGE-BUFFER-FILE is the file to be associated with the merge buffer
1237 "Run Ediff by merging two revisions of a file.
1238 The file is the optional FILE argument or the file visited by the current
1239 buffer."
1240 (interactive)
1241 (if (stringp file) (find-file file))
1242 (let (rev1 rev2)
1243 (setq rev1
1244 (read-string
1245 (format
1246 "Version 1 to merge (default %s's working version): "
1247 (if (stringp file)
1248 (file-name-nondirectory file) "current buffer")))
1249 rev2
1250 (read-string
1251 (format
1252 "Version 2 to merge (default %s): "
1253 (if (stringp file)
1254 (file-name-nondirectory file) "current buffer"))))
1255 (ediff-load-version-control)
1256 ;; ancestor-revision=nil
1257 (funcall
1258 (intern (format "ediff-%S-merge-internal" ediff-version-control-package))
1259 rev1 rev2 nil startup-hooks merge-buffer-file)))
1262 ;;;###autoload
1263 (defun ediff-merge-revisions-with-ancestor (&optional
1264 file startup-hooks
1265 ;; MERGE-BUFFER-FILE is the file to
1266 ;; be associated with the merge
1267 ;; buffer
1268 merge-buffer-file)
1269 "Run Ediff by merging two revisions of a file with a common ancestor.
1270 The file is the optional FILE argument or the file visited by the current
1271 buffer."
1272 (interactive)
1273 (if (stringp file) (find-file file))
1274 (let (rev1 rev2 ancestor-rev)
1275 (setq rev1
1276 (read-string
1277 (format
1278 "Version 1 to merge (default %s's working version): "
1279 (if (stringp file)
1280 (file-name-nondirectory file) "current buffer")))
1281 rev2
1282 (read-string
1283 (format
1284 "Version 2 to merge (default %s): "
1285 (if (stringp file)
1286 (file-name-nondirectory file) "current buffer")))
1287 ancestor-rev
1288 (read-string
1289 (format
1290 "Ancestor version (default %s's base revision): "
1291 (if (stringp file)
1292 (file-name-nondirectory file) "current buffer"))))
1293 (ediff-load-version-control)
1294 (funcall
1295 (intern (format "ediff-%S-merge-internal" ediff-version-control-package))
1296 rev1 rev2 ancestor-rev startup-hooks merge-buffer-file)))
1298 ;;; Apply patch
1300 ;;;###autoload
1301 (defun ediff-patch-file (&optional arg patch-buf)
1302 "Run Ediff by patching SOURCE-FILENAME.
1303 If optional PATCH-BUF is given, use the patch in that buffer
1304 and don't ask the user.
1305 If prefix argument, then: if even argument, assume that the patch is in a
1306 buffer. If odd -- assume it is in a file."
1307 (interactive "P")
1308 (let (source-dir source-file)
1309 (require 'ediff-ptch)
1310 (setq patch-buf
1311 (ediff-get-patch-buffer
1312 (if arg (prefix-numeric-value arg)) patch-buf))
1313 (setq source-dir (cond (ediff-use-last-dir ediff-last-dir-patch)
1314 ((and (not ediff-patch-default-directory)
1315 (buffer-file-name patch-buf))
1316 (file-name-directory
1317 (expand-file-name
1318 (buffer-file-name patch-buf))))
1319 (t default-directory)))
1320 (setq source-file
1321 (read-file-name
1322 "File to patch (directory, if multifile patch): "
1323 ;; use an explicit initial file
1324 source-dir nil nil (ediff-get-default-file-name)))
1325 (ediff-dispatch-file-patching-job patch-buf source-file)))
1327 ;;;###autoload
1328 (defun ediff-patch-buffer (&optional arg patch-buf)
1329 "Run Ediff by patching the buffer specified at prompt.
1330 Without the optional prefix ARG, asks if the patch is in some buffer and
1331 prompts for the buffer or a file, depending on the answer.
1332 With ARG=1, assumes the patch is in a file and prompts for the file.
1333 With ARG=2, assumes the patch is in a buffer and prompts for the buffer.
1334 PATCH-BUF is an optional argument, which specifies the buffer that contains the
1335 patch. If not given, the user is prompted according to the prefix argument."
1336 (interactive "P")
1337 (require 'ediff-ptch)
1338 (setq patch-buf
1339 (ediff-get-patch-buffer
1340 (if arg (prefix-numeric-value arg)) patch-buf))
1341 (ediff-patch-buffer-internal
1342 patch-buf
1343 (read-buffer
1344 "Which buffer to patch? "
1345 (ediff-other-buffer patch-buf))))
1348 ;;;###autoload
1349 (defalias 'epatch 'ediff-patch-file)
1350 ;;;###autoload
1351 (defalias 'epatch-buffer 'ediff-patch-buffer)
1356 ;;; Versions Control functions
1358 ;;;###autoload
1359 (defun ediff-revision (&optional file startup-hooks)
1360 "Run Ediff by comparing versions of a file.
1361 The file is an optional FILE argument or the file entered at the prompt.
1362 Default: the file visited by the current buffer.
1363 Uses `vc.el' or `rcs.el' depending on `ediff-version-control-package'."
1364 ;; if buffer is non-nil, use that buffer instead of the current buffer
1365 (interactive "P")
1366 (if (not (stringp file))
1367 (setq file
1368 (ediff-read-file-name "Compare revisions for file"
1369 (if ediff-use-last-dir
1370 ediff-last-dir-A
1371 default-directory)
1372 (ediff-get-default-file-name)
1373 'no-dirs)))
1374 (find-file file)
1375 (if (and (buffer-modified-p)
1376 (y-or-n-p (format "Buffer %s is modified. Save buffer? "
1377 (buffer-name))))
1378 (save-buffer (current-buffer)))
1379 (let (rev1 rev2)
1380 (setq rev1
1381 (read-string
1382 (format "Revision 1 to compare (default %s's latest revision): "
1383 (file-name-nondirectory file)))
1384 rev2
1385 (read-string
1386 (format "Revision 2 to compare (default %s's current state): "
1387 (file-name-nondirectory file))))
1388 (ediff-load-version-control)
1389 (funcall
1390 (intern (format "ediff-%S-internal" ediff-version-control-package))
1391 rev1 rev2 startup-hooks)
1395 ;;;###autoload
1396 (defalias 'erevision 'ediff-revision)
1399 ;; Test if version control package is loaded and load if not
1400 ;; Is SILENT is non-nil, don't report error if package is not found.
1401 (defun ediff-load-version-control (&optional silent)
1402 (require 'ediff-vers)
1403 (or (featurep ediff-version-control-package)
1404 (if (locate-library (symbol-name ediff-version-control-package))
1405 (progn
1406 (message "") ; kill the message from `locate-library'
1407 (require ediff-version-control-package))
1408 (or silent
1409 (error "Version control package %S.el not found. Use vc.el instead"
1410 ediff-version-control-package)))))
1413 ;;;###autoload
1414 (defun ediff-version ()
1415 "Return string describing the version of Ediff.
1416 When called interactively, displays the version."
1417 (interactive)
1418 (if (interactive-p)
1419 (message "%s" (ediff-version))
1420 (format "Ediff %s of %s" ediff-version ediff-date)))
1422 ;; info is run first, and will autoload info.el.
1423 (declare-function Info-goto-node "info" (nodename &optional fork))
1425 ;;;###autoload
1426 (defun ediff-documentation (&optional node)
1427 "Display Ediff's manual.
1428 With optional NODE, goes to that node."
1429 (interactive)
1430 (let ((ctl-window ediff-control-window)
1431 (ctl-buf ediff-control-buffer))
1433 (ediff-skip-unsuitable-frames)
1434 (condition-case nil
1435 (progn
1436 (pop-to-buffer (get-buffer-create "*info*"))
1437 (info (if (featurep 'xemacs) "ediff.info" "ediff"))
1438 (if node
1439 (Info-goto-node node)
1440 (message "Type `i' to search for a specific topic"))
1441 (raise-frame (selected-frame)))
1442 (error (beep 1)
1443 (with-output-to-temp-buffer ediff-msg-buffer
1444 (ediff-with-current-buffer standard-output
1445 (fundamental-mode))
1446 (princ ediff-BAD-INFO))
1447 (if (window-live-p ctl-window)
1448 (progn
1449 (select-window ctl-window)
1450 (set-window-buffer ctl-window ctl-buf)))))))
1453 (dolist (mess '("^Errors in diff output. Diff output is in "
1454 "^Hmm... I don't see an Ediff command around here...$"
1455 "^Undocumented command! Type `G' in Ediff Control Panel to drop a note to the Ediff maintainer$"
1456 ": This command runs in Ediff Control Buffer only!$"
1457 ": Invalid op in ediff-check-version$"
1458 "^ediff-shrink-window-C can be used only for merging jobs$"
1459 "^Lost difference info on these directories$"
1460 "^This command is inapplicable in the present context$"
1461 "^This session group has no parent$"
1462 "^Can't hide active session, $"
1463 "^Ediff: something wrong--no multiple diffs buffer$"
1464 "^Can't make context diff for Session $"
1465 "^The patch buffer wasn't found$"
1466 "^Aborted$"
1467 "^This Ediff session is not part of a session group$"
1468 "^No active Ediff sessions or corrupted session registry$"
1469 "^No session info in this line$"
1470 "^`.*' is not an ordinary file$"
1471 "^Patch appears to have failed$"
1472 "^Recomputation of differences cancelled$"
1473 "^No fine differences in this mode$"
1474 "^Lost connection to ancestor buffer...sorry$"
1475 "^Not merging with ancestor$"
1476 "^Don't know how to toggle read-only in buffer "
1477 "Emacs is not running as a window application$"
1478 "^This command makes sense only when merging with an ancestor$"
1479 "^At end of the difference list$"
1480 "^At beginning of the difference list$"
1481 "^Nothing saved for diff .* in buffer "
1482 "^Buffer is out of sync for file "
1483 "^Buffer out of sync for file "
1484 "^Output from `diff' not found$"
1485 "^You forgot to specify a region in buffer "
1486 "^All right. Make up your mind and come back...$"
1487 "^Current buffer is not visiting any file$"
1488 "^Failed to retrieve revision: $"
1489 "^Can't determine display width.$"
1490 "^File `.*' does not exist or is not readable$"
1491 "^File `.*' is a directory$"
1492 "^Buffer .* doesn't exist$"
1493 "^Directories . and . are the same: "
1494 "^Directory merge aborted$"
1495 "^Merge of directory revisions aborted$"
1496 "^Buffer .* doesn't exist$"
1497 "^There is no file to merge$"
1498 "^Version control package .*.el not found. Use vc.el instead$"))
1499 (add-to-list 'debug-ignored-errors mess))
1502 (require 'ediff-util)
1504 (run-hooks 'ediff-load-hook)
1507 ;;; Local Variables:
1508 ;;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
1509 ;;; eval: (put 'ediff-with-current-buffer 'lisp-indent-hook 1)
1510 ;;; eval: (put 'ediff-with-current-buffer 'edebug-form-spec '(form body))
1511 ;;; End:
1513 ;; arch-tag: 97c71396-db02-4f41-8b48-6a51c3348fcc
1514 ;;; ediff.el ends here