(url-file-handler): Save match data.
[emacs.git] / lisp / ediff.el
blob1b76db41449ed3e89f5cbc48b5a27d07115f0c78
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, 2009 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 "November 22, 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 (defun ediff-find-file (file-var buffer-name &optional last-dir hooks-var)
277 "Visit FILE and arrange its buffer to Ediff's liking.
278 FILE-VAR is actually a variable symbol whose value must contain a true
279 file name.
280 BUFFER-NAME is a variable symbol, which will get the buffer object into
281 which FILE is read.
282 LAST-DIR is the directory variable symbol where FILE's
283 directory name should be returned. HOOKS-VAR is a variable symbol that will
284 be assigned the hook to be executed after `ediff-startup' is finished.
285 `ediff-find-file' arranges that the temp files it might create will be
286 deleted."
287 (let* ((file (symbol-value file-var))
288 (file-magic (ediff-filename-magic-p file))
289 (temp-file-name-prefix (file-name-nondirectory file)))
290 (cond ((not (file-readable-p file))
291 (error "File `%s' does not exist or is not readable" file))
292 ((file-directory-p file)
293 (error "File `%s' is a directory" file)))
295 ;; some of the commands, below, require full file name
296 (setq file (expand-file-name file))
298 ;; Record the directory of the file
299 (if last-dir
300 (set last-dir (expand-file-name (file-name-directory file))))
302 ;; Setup the buffer
303 (set buffer-name (find-file-noselect file))
305 (ediff-with-current-buffer (symbol-value buffer-name)
306 (widen) ; Make sure the entire file is seen
307 (cond (file-magic ; file has a handler, such as jka-compr-handler or
308 ;;; ange-ftp-hook-function--arrange for temp file
309 (ediff-verify-file-buffer 'magic)
310 (setq file
311 (ediff-make-temp-file
312 (current-buffer) temp-file-name-prefix))
313 (set hooks-var (cons `(lambda () (delete-file ,file))
314 (symbol-value hooks-var))))
315 ;; file processed via auto-mode-alist, a la uncompress.el
316 ((not (equal (file-truename file)
317 (file-truename (buffer-file-name))))
318 (setq file
319 (ediff-make-temp-file
320 (current-buffer) temp-file-name-prefix))
321 (set hooks-var (cons `(lambda () (delete-file ,file))
322 (symbol-value hooks-var))))
323 (t ;; plain file---just check that the file matches the buffer
324 (ediff-verify-file-buffer))))
325 (set file-var file)))
327 ;; MERGE-BUFFER-FILE is the file to be associated with the merge buffer
328 (defun ediff-files-internal (file-A file-B file-C startup-hooks job-name
329 &optional merge-buffer-file)
330 (let (buf-A buf-B buf-C)
331 (if (string= file-A file-B)
332 (error "Files A and B are the same"))
333 (if (stringp file-C)
334 (or (and (string= file-A file-C) (error "Files A and C are the same"))
335 (and (string= file-B file-C) (error "Files B and C are the same"))))
336 (message "Reading file %s ... " file-A)
337 ;;(sit-for 0)
338 (ediff-find-file 'file-A 'buf-A 'ediff-last-dir-A 'startup-hooks)
339 (message "Reading file %s ... " file-B)
340 ;;(sit-for 0)
341 (ediff-find-file 'file-B 'buf-B 'ediff-last-dir-B 'startup-hooks)
342 (if (stringp file-C)
343 (progn
344 (message "Reading file %s ... " file-C)
345 ;;(sit-for 0)
346 (ediff-find-file
347 'file-C 'buf-C
348 (if (eq job-name 'ediff-merge-files-with-ancestor)
349 'ediff-last-dir-ancestor 'ediff-last-dir-C)
350 'startup-hooks)))
351 (ediff-setup buf-A file-A
352 buf-B file-B
353 buf-C file-C
354 startup-hooks
355 (list (cons 'ediff-job-name job-name))
356 merge-buffer-file)))
358 (declare-function diff-latest-backup-file "diff" (fn))
360 ;;;###autoload
361 (defalias 'ediff 'ediff-files)
363 ;;;###autoload
364 (defun ediff-backup (file)
365 "Run Ediff on FILE and its backup file.
366 Uses the latest backup, if there are several numerical backups.
367 If this file is a backup, `ediff' it with its original."
368 (interactive (list (read-file-name "Ediff (file with backup): ")))
369 ;; The code is taken from `diff-backup'.
370 (require 'diff)
371 (let (bak ori)
372 (if (backup-file-name-p file)
373 (setq bak file
374 ori (file-name-sans-versions file))
375 (setq bak (or (diff-latest-backup-file file)
376 (error "No backup found for %s" file))
377 ori file))
378 (ediff-files bak ori)))
380 ;;;###autoload
381 (defun ediff-buffers (buffer-A buffer-B &optional startup-hooks job-name)
382 "Run Ediff on a pair of buffers, BUFFER-A and BUFFER-B."
383 (interactive
384 (let (bf)
385 (list (setq bf (read-buffer "Buffer A to compare: "
386 (ediff-other-buffer "") t))
387 (read-buffer "Buffer B to compare: "
388 (progn
389 ;; realign buffers so that two visible bufs will be
390 ;; at the top
391 (save-window-excursion (other-window 1))
392 (ediff-other-buffer bf))
393 t))))
394 (or job-name (setq job-name 'ediff-buffers))
395 (ediff-buffers-internal buffer-A buffer-B nil startup-hooks job-name))
397 ;;;###autoload
398 (defalias 'ebuffers 'ediff-buffers)
401 ;;;###autoload
402 (defun ediff-buffers3 (buffer-A buffer-B buffer-C
403 &optional startup-hooks job-name)
404 "Run Ediff on three buffers, BUFFER-A, BUFFER-B, and BUFFER-C."
405 (interactive
406 (let (bf bff)
407 (list (setq bf (read-buffer "Buffer A to compare: "
408 (ediff-other-buffer "") t))
409 (setq bff (read-buffer "Buffer B to compare: "
410 (progn
411 ;; realign buffers so that two visible
412 ;; bufs will be at the top
413 (save-window-excursion (other-window 1))
414 (ediff-other-buffer bf))
416 (read-buffer "Buffer C to compare: "
417 (progn
418 ;; realign buffers so that three visible
419 ;; bufs will be at the top
420 (save-window-excursion (other-window 1))
421 (ediff-other-buffer (list bf bff)))
424 (or job-name (setq job-name 'ediff-buffers3))
425 (ediff-buffers-internal buffer-A buffer-B buffer-C startup-hooks job-name))
427 ;;;###autoload
428 (defalias 'ebuffers3 'ediff-buffers3)
432 ;; MERGE-BUFFER-FILE is the file to be associated with the merge buffer
433 (defun ediff-buffers-internal (buf-A buf-B buf-C startup-hooks job-name
434 &optional merge-buffer-file)
435 (let* ((buf-A-file-name (buffer-file-name (get-buffer buf-A)))
436 (buf-B-file-name (buffer-file-name (get-buffer buf-B)))
437 (buf-C-is-alive (ediff-buffer-live-p buf-C))
438 (buf-C-file-name (if buf-C-is-alive
439 (buffer-file-name (get-buffer buf-B))))
440 file-A file-B file-C)
441 (if (not (ediff-buffer-live-p buf-A))
442 (error "Buffer %S doesn't exist" buf-A))
443 (if (not (ediff-buffer-live-p buf-B))
444 (error "Buffer %S doesn't exist" buf-B))
445 (let ((ediff-job-name job-name))
446 (if (and ediff-3way-comparison-job
447 (not buf-C-is-alive))
448 (error "Buffer %S doesn't exist" buf-C)))
449 (if (stringp buf-A-file-name)
450 (setq buf-A-file-name (file-name-nondirectory buf-A-file-name)))
451 (if (stringp buf-B-file-name)
452 (setq buf-B-file-name (file-name-nondirectory buf-B-file-name)))
453 (if (stringp buf-C-file-name)
454 (setq buf-C-file-name (file-name-nondirectory buf-C-file-name)))
456 (setq file-A (ediff-make-temp-file buf-A buf-A-file-name)
457 file-B (ediff-make-temp-file buf-B buf-B-file-name))
458 (if buf-C-is-alive
459 (setq file-C (ediff-make-temp-file buf-C buf-C-file-name)))
461 (ediff-setup (get-buffer buf-A) file-A
462 (get-buffer buf-B) file-B
463 (if buf-C-is-alive (get-buffer buf-C))
464 file-C
465 (cons `(lambda ()
466 (delete-file ,file-A)
467 (delete-file ,file-B)
468 (if (stringp ,file-C) (delete-file ,file-C)))
469 startup-hooks)
470 (list (cons 'ediff-job-name job-name))
471 merge-buffer-file)))
474 ;;; Directory and file group operations
476 ;; Get appropriate default name for directory:
477 ;; If ediff-use-last-dir, use ediff-last-dir-A.
478 ;; In dired mode, use the directory that is under the point (if any);
479 ;; otherwise, use default-directory
480 (defun ediff-get-default-directory-name ()
481 (cond (ediff-use-last-dir ediff-last-dir-A)
482 ((eq major-mode 'dired-mode)
483 (let ((f (dired-get-filename nil 'noerror)))
484 (if (and (stringp f) (file-directory-p f))
486 default-directory)))
487 (t default-directory)))
490 ;;;###autoload
491 (defun ediff-directories (dir1 dir2 regexp)
492 "Run Ediff on a pair of directories, DIR1 and DIR2, comparing files that have
493 the same name in both. The third argument, REGEXP, is nil or a regular
494 expression; only file names that match the regexp are considered."
495 (interactive
496 (let ((dir-A (ediff-get-default-directory-name))
497 (default-regexp (eval ediff-default-filtering-regexp))
499 (list (setq f (read-directory-name
500 "Directory A to compare:" dir-A nil 'must-match))
501 (read-directory-name "Directory B to compare:"
502 (if ediff-use-last-dir
503 ediff-last-dir-B
504 (ediff-strip-last-dir f))
505 nil 'must-match)
506 (read-string
507 (if (stringp default-regexp)
508 (format "Filter through regular expression (default %s): "
509 default-regexp)
510 "Filter through regular expression: ")
512 'ediff-filtering-regexp-history
513 (eval ediff-default-filtering-regexp))
515 (ediff-directories-internal
516 dir1 dir2 nil regexp 'ediff-files 'ediff-directories
519 ;;;###autoload
520 (defalias 'edirs 'ediff-directories)
523 ;;;###autoload
524 (defun ediff-directory-revisions (dir1 regexp)
525 "Run Ediff on a directory, DIR1, comparing its files with their revisions.
526 The second argument, REGEXP, is a regular expression that filters the file
527 names. Only the files that are under revision control are taken into account."
528 (interactive
529 (let ((dir-A (ediff-get-default-directory-name))
530 (default-regexp (eval ediff-default-filtering-regexp))
532 (list (read-directory-name
533 "Directory to compare with revision:" dir-A nil 'must-match)
534 (read-string
535 (if (stringp default-regexp)
536 (format "Filter through regular expression (default %s): "
537 default-regexp)
538 "Filter through regular expression: ")
540 'ediff-filtering-regexp-history
541 (eval ediff-default-filtering-regexp))
543 (ediff-directory-revisions-internal
544 dir1 regexp 'ediff-revision 'ediff-directory-revisions
547 ;;;###autoload
548 (defalias 'edir-revisions 'ediff-directory-revisions)
551 ;;;###autoload
552 (defun ediff-directories3 (dir1 dir2 dir3 regexp)
553 "Run Ediff on three directories, DIR1, DIR2, and DIR3, comparing files that
554 have the same name in all three. The last argument, REGEXP, is nil or a
555 regular expression; only file names that match the regexp are considered."
557 (interactive
558 (let ((dir-A (ediff-get-default-directory-name))
559 (default-regexp (eval ediff-default-filtering-regexp))
561 (list (setq f (read-directory-name "Directory A to compare:" dir-A nil))
562 (setq f (read-directory-name "Directory B to compare:"
563 (if ediff-use-last-dir
564 ediff-last-dir-B
565 (ediff-strip-last-dir f))
566 nil 'must-match))
567 (read-directory-name "Directory C to compare:"
568 (if ediff-use-last-dir
569 ediff-last-dir-C
570 (ediff-strip-last-dir f))
571 nil 'must-match)
572 (read-string
573 (if (stringp default-regexp)
574 (format "Filter through regular expression (default %s): "
575 default-regexp)
576 "Filter through regular expression: ")
578 'ediff-filtering-regexp-history
579 (eval ediff-default-filtering-regexp))
581 (ediff-directories-internal
582 dir1 dir2 dir3 regexp 'ediff-files3 'ediff-directories3
585 ;;;###autoload
586 (defalias 'edirs3 'ediff-directories3)
588 ;;;###autoload
589 (defun ediff-merge-directories (dir1 dir2 regexp &optional merge-autostore-dir)
590 "Run Ediff on a pair of directories, DIR1 and DIR2, merging files that have
591 the same name in both. The third argument, REGEXP, is nil or a regular
592 expression; only file names that match the regexp are considered."
593 (interactive
594 (let ((dir-A (ediff-get-default-directory-name))
595 (default-regexp (eval ediff-default-filtering-regexp))
597 (list (setq f (read-directory-name "Directory A to merge:"
598 dir-A nil 'must-match))
599 (read-directory-name "Directory B to merge:"
600 (if ediff-use-last-dir
601 ediff-last-dir-B
602 (ediff-strip-last-dir f))
603 nil 'must-match)
604 (read-string
605 (if (stringp default-regexp)
606 (format "Filter through regular expression (default %s): "
607 default-regexp)
608 "Filter through regular expression: ")
610 'ediff-filtering-regexp-history
611 (eval ediff-default-filtering-regexp))
613 (ediff-directories-internal
614 dir1 dir2 nil regexp 'ediff-merge-files 'ediff-merge-directories
615 nil merge-autostore-dir
618 ;;;###autoload
619 (defalias 'edirs-merge 'ediff-merge-directories)
621 ;;;###autoload
622 (defun ediff-merge-directories-with-ancestor (dir1 dir2 ancestor-dir regexp
623 &optional
624 merge-autostore-dir)
625 "Merge files in directories DIR1 and DIR2 using files in ANCESTOR-DIR as ancestors.
626 Ediff merges files that have identical names in DIR1, DIR2. If a pair of files
627 in DIR1 and DIR2 doesn't have an ancestor in ANCESTOR-DIR, Ediff will merge
628 without ancestor. The fourth argument, REGEXP, is nil or a regular expression;
629 only file names that match the regexp are considered."
630 (interactive
631 (let ((dir-A (ediff-get-default-directory-name))
632 (default-regexp (eval ediff-default-filtering-regexp))
634 (list (setq f (read-directory-name "Directory A to merge:" dir-A nil))
635 (setq f (read-directory-name "Directory B to merge:"
636 (if ediff-use-last-dir
637 ediff-last-dir-B
638 (ediff-strip-last-dir f))
639 nil 'must-match))
640 (read-directory-name "Ancestor directory:"
641 (if ediff-use-last-dir
642 ediff-last-dir-C
643 (ediff-strip-last-dir f))
644 nil 'must-match)
645 (read-string
646 (if (stringp default-regexp)
647 (format "Filter through regular expression (default %s): "
648 default-regexp)
649 "Filter through regular expression: ")
651 'ediff-filtering-regexp-history
652 (eval ediff-default-filtering-regexp))
654 (ediff-directories-internal
655 dir1 dir2 ancestor-dir regexp
656 'ediff-merge-files-with-ancestor 'ediff-merge-directories-with-ancestor
657 nil merge-autostore-dir
660 ;;;###autoload
661 (defun ediff-merge-directory-revisions (dir1 regexp
662 &optional merge-autostore-dir)
663 "Run Ediff on a directory, DIR1, merging its files with their revisions.
664 The second argument, REGEXP, is a regular expression that filters the file
665 names. Only the files that are under revision control are taken into account."
666 (interactive
667 (let ((dir-A (ediff-get-default-directory-name))
668 (default-regexp (eval ediff-default-filtering-regexp))
670 (list (read-directory-name
671 "Directory to merge with revisions:" dir-A nil 'must-match)
672 (read-string
673 (if (stringp default-regexp)
674 (format "Filter through regular expression (default %s): "
675 default-regexp)
676 "Filter through regular expression: ")
678 'ediff-filtering-regexp-history
679 (eval ediff-default-filtering-regexp))
681 (ediff-directory-revisions-internal
682 dir1 regexp 'ediff-merge-revisions 'ediff-merge-directory-revisions
683 nil merge-autostore-dir
686 ;;;###autoload
687 (defalias 'edir-merge-revisions 'ediff-merge-directory-revisions)
689 ;;;###autoload
690 (defun ediff-merge-directory-revisions-with-ancestor (dir1 regexp
691 &optional
692 merge-autostore-dir)
693 "Run Ediff on a directory, DIR1, merging its files with their revisions and ancestors.
694 The second argument, REGEXP, is a regular expression that filters the file
695 names. Only the files that are under revision control are taken into account."
696 (interactive
697 (let ((dir-A (ediff-get-default-directory-name))
698 (default-regexp (eval ediff-default-filtering-regexp))
700 (list (read-directory-name
701 "Directory to merge with revisions and ancestors:"
702 dir-A nil 'must-match)
703 (read-string
704 (if (stringp default-regexp)
705 (format "Filter through regular expression (default %s): "
706 default-regexp)
707 "Filter through regular expression: ")
709 'ediff-filtering-regexp-history
710 (eval ediff-default-filtering-regexp))
712 (ediff-directory-revisions-internal
713 dir1 regexp 'ediff-merge-revisions-with-ancestor
714 'ediff-merge-directory-revisions-with-ancestor
715 nil merge-autostore-dir
718 ;;;###autoload
719 (defalias
720 'edir-merge-revisions-with-ancestor
721 'ediff-merge-directory-revisions-with-ancestor)
723 ;;;###autoload
724 (defalias 'edirs-merge-with-ancestor 'ediff-merge-directories-with-ancestor)
726 ;; Run ediff-action (ediff-files, ediff-merge, ediff-merge-with-ancestors)
727 ;; on a pair of directories (three directories, in case of ancestor).
728 ;; The third argument, REGEXP, is nil or a regular expression;
729 ;; only file names that match the regexp are considered.
730 ;; JOBNAME is the symbol indicating the meta-job to be performed.
731 ;; MERGE-AUTOSTORE-DIR is the directory in which to store merged files.
732 (defun ediff-directories-internal (dir1 dir2 dir3 regexp action jobname
733 &optional startup-hooks
734 merge-autostore-dir)
735 (if (stringp dir3)
736 (setq dir3 (if (file-directory-p dir3) dir3 (file-name-directory dir3))))
738 (cond ((string= dir1 dir2)
739 (error "Directories A and B are the same: %s" dir1))
740 ((and (eq jobname 'ediff-directories3)
741 (string= dir1 dir3))
742 (error "Directories A and C are the same: %s" dir1))
743 ((and (eq jobname 'ediff-directories3)
744 (string= dir2 dir3))
745 (error "Directories B and C are the same: %s" dir1)))
747 (if merge-autostore-dir
748 (or (stringp merge-autostore-dir)
749 (error "%s: Directory for storing merged files must be a string"
750 jobname)))
751 (let (;; dir-diff-struct is of the form (common-list diff-list)
752 ;; It is a structure where ediff-intersect-directories returns
753 ;; commonalities and differences among directories
754 dir-diff-struct
755 meta-buf)
756 (if (and ediff-autostore-merges
757 (ediff-merge-metajob jobname)
758 (not merge-autostore-dir))
759 (setq merge-autostore-dir
760 (read-directory-name "Save merged files in directory: "
761 (if ediff-use-last-dir
762 ediff-last-merge-autostore-dir
763 (ediff-strip-last-dir dir1))
765 'must-match)))
766 ;; verify we are not merging into an orig directory
767 (if merge-autostore-dir
768 (cond ((and (stringp dir1) (string= merge-autostore-dir dir1))
769 (or (y-or-n-p
770 "Directory for saving merged files = Directory A. Sure? ")
771 (error "Directory merge aborted")))
772 ((and (stringp dir2) (string= merge-autostore-dir dir2))
773 (or (y-or-n-p
774 "Directory for saving merged files = Directory B. Sure? ")
775 (error "Directory merge aborted")))
776 ((and (stringp dir3) (string= merge-autostore-dir dir3))
777 (or (y-or-n-p
778 "Directory for saving merged files = Ancestor Directory. Sure? ")
779 (error "Directory merge aborted")))))
781 (setq dir-diff-struct (ediff-intersect-directories
782 jobname
783 regexp dir1 dir2 dir3 merge-autostore-dir))
784 (setq startup-hooks
785 ;; this sets various vars in the meta buffer inside
786 ;; ediff-prepare-meta-buffer
787 (cons `(lambda ()
788 ;; tell what to do if the user clicks on a session record
789 (setq ediff-session-action-function (quote ,action))
790 ;; set ediff-dir-difference-list
791 (setq ediff-dir-difference-list
792 (cdr (quote ,dir-diff-struct))))
793 startup-hooks))
794 (setq meta-buf (ediff-prepare-meta-buffer
795 'ediff-filegroup-action
796 (car dir-diff-struct)
797 "*Ediff Session Group Panel"
798 'ediff-redraw-directory-group-buffer
799 jobname
800 startup-hooks))
801 (ediff-show-meta-buffer meta-buf)
804 ;; MERGE-AUTOSTORE-DIR can be given to tell ediff where to store the merged
805 ;; files
806 (defun ediff-directory-revisions-internal (dir1 regexp action jobname
807 &optional startup-hooks
808 merge-autostore-dir)
809 (setq dir1 (if (file-directory-p dir1) dir1 (file-name-directory dir1)))
811 (if merge-autostore-dir
812 (or (stringp merge-autostore-dir)
813 (error "%S: Directory for storing merged files must be a string"
814 jobname)))
815 (let (file-list meta-buf)
816 (if (and ediff-autostore-merges
817 (ediff-merge-metajob jobname)
818 (not merge-autostore-dir))
819 (setq merge-autostore-dir
820 (read-directory-name "Save merged files in directory: "
821 (if ediff-use-last-dir
822 ediff-last-merge-autostore-dir
823 (ediff-strip-last-dir dir1))
825 'must-match)))
826 ;; verify merge-autostore-dir != dir1
827 (if (and merge-autostore-dir
828 (stringp dir1)
829 (string= merge-autostore-dir dir1))
830 (or (y-or-n-p
831 "Directory for saving merged file = directory A. Sure? ")
832 (error "Merge of directory revisions aborted")))
834 (setq file-list
835 (ediff-get-directory-files-under-revision
836 jobname regexp dir1 merge-autostore-dir))
837 (setq startup-hooks
838 ;; this sets various vars in the meta buffer inside
839 ;; ediff-prepare-meta-buffer
840 (cons `(lambda ()
841 ;; tell what to do if the user clicks on a session record
842 (setq ediff-session-action-function (quote ,action)))
843 startup-hooks))
844 (setq meta-buf (ediff-prepare-meta-buffer
845 'ediff-filegroup-action
846 file-list
847 "*Ediff Session Group Panel"
848 'ediff-redraw-directory-group-buffer
849 jobname
850 startup-hooks))
851 (ediff-show-meta-buffer meta-buf)
855 ;;; Compare regions and windows
857 ;;;###autoload
858 (defun ediff-windows-wordwise (dumb-mode &optional wind-A wind-B startup-hooks)
859 "Compare WIND-A and WIND-B, which are selected by clicking, wordwise.
860 With prefix argument, DUMB-MODE, or on a non-windowing display, works as
861 follows:
862 If WIND-A is nil, use selected window.
863 If WIND-B is nil, use window next to WIND-A."
864 (interactive "P")
865 (ediff-windows dumb-mode wind-A wind-B
866 startup-hooks 'ediff-windows-wordwise 'word-mode))
868 ;;;###autoload
869 (defun ediff-windows-linewise (dumb-mode &optional wind-A wind-B startup-hooks)
870 "Compare WIND-A and WIND-B, which are selected by clicking, linewise.
871 With prefix argument, DUMB-MODE, or on a non-windowing display, works as
872 follows:
873 If WIND-A is nil, use selected window.
874 If WIND-B is nil, use window next to WIND-A."
875 (interactive "P")
876 (ediff-windows dumb-mode wind-A wind-B
877 startup-hooks 'ediff-windows-linewise nil))
879 ;; Compare WIND-A and WIND-B, which are selected by clicking.
880 ;; With prefix argument, DUMB-MODE, or on a non-windowing display,
881 ;; works as follows:
882 ;; If WIND-A is nil, use selected window.
883 ;; If WIND-B is nil, use window next to WIND-A.
884 (defun ediff-windows (dumb-mode wind-A wind-B startup-hooks job-name word-mode)
885 (if (or dumb-mode (not (ediff-window-display-p)))
886 (setq wind-A (ediff-get-next-window wind-A nil)
887 wind-B (ediff-get-next-window wind-B wind-A))
888 (setq wind-A (ediff-get-window-by-clicking wind-A nil 1)
889 wind-B (ediff-get-window-by-clicking wind-B wind-A 2)))
891 (let ((buffer-A (window-buffer wind-A))
892 (buffer-B (window-buffer wind-B))
893 beg-A end-A beg-B end-B)
895 (save-excursion
896 (save-window-excursion
897 (sit-for 0) ; sync before using window-start/end -- a precaution
898 (select-window wind-A)
899 (setq beg-A (window-start)
900 end-A (window-end))
901 (select-window wind-B)
902 (setq beg-B (window-start)
903 end-B (window-end))))
904 (setq buffer-A
905 (ediff-clone-buffer-for-window-comparison
906 buffer-A wind-A "-Window.A-")
907 buffer-B
908 (ediff-clone-buffer-for-window-comparison
909 buffer-B wind-B "-Window.B-"))
910 (ediff-regions-internal
911 buffer-A beg-A end-A buffer-B beg-B end-B
912 startup-hooks job-name word-mode nil)))
915 ;;;###autoload
916 (defun ediff-regions-wordwise (buffer-A buffer-B &optional startup-hooks)
917 "Run Ediff on a pair of regions in specified buffers.
918 Regions \(i.e., point and mark\) can be set in advance or marked interactively.
919 This function is effective only for relatively small regions, up to 200
920 lines. For large regions, use `ediff-regions-linewise'."
921 (interactive
922 (let (bf)
923 (list (setq bf (read-buffer "Region's A buffer: "
924 (ediff-other-buffer "") t))
925 (read-buffer "Region's B buffer: "
926 (progn
927 ;; realign buffers so that two visible bufs will be
928 ;; at the top
929 (save-window-excursion (other-window 1))
930 (ediff-other-buffer bf))
931 t))))
932 (if (not (ediff-buffer-live-p buffer-A))
933 (error "Buffer %S doesn't exist" buffer-A))
934 (if (not (ediff-buffer-live-p buffer-B))
935 (error "Buffer %S doesn't exist" buffer-B))
938 (let ((buffer-A
939 (ediff-clone-buffer-for-region-comparison buffer-A "-Region.A-"))
940 (buffer-B
941 (ediff-clone-buffer-for-region-comparison buffer-B "-Region.B-"))
942 reg-A-beg reg-A-end reg-B-beg reg-B-end)
943 (save-excursion
944 (set-buffer buffer-A)
945 (setq reg-A-beg (region-beginning)
946 reg-A-end (region-end))
947 (set-buffer buffer-B)
948 (setq reg-B-beg (region-beginning)
949 reg-B-end (region-end)))
951 (ediff-regions-internal
952 (get-buffer buffer-A) reg-A-beg reg-A-end
953 (get-buffer buffer-B) reg-B-beg reg-B-end
954 startup-hooks 'ediff-regions-wordwise 'word-mode nil)))
956 ;;;###autoload
957 (defun ediff-regions-linewise (buffer-A buffer-B &optional startup-hooks)
958 "Run Ediff on a pair of regions in specified buffers.
959 Regions \(i.e., point and mark\) can be set in advance or marked interactively.
960 Each region is enlarged to contain full lines.
961 This function is effective for large regions, over 100-200
962 lines. For small regions, use `ediff-regions-wordwise'."
963 (interactive
964 (let (bf)
965 (list (setq bf (read-buffer "Region A's buffer: "
966 (ediff-other-buffer "") t))
967 (read-buffer "Region B's buffer: "
968 (progn
969 ;; realign buffers so that two visible bufs will be
970 ;; at the top
971 (save-window-excursion (other-window 1))
972 (ediff-other-buffer bf))
973 t))))
974 (if (not (ediff-buffer-live-p buffer-A))
975 (error "Buffer %S doesn't exist" buffer-A))
976 (if (not (ediff-buffer-live-p buffer-B))
977 (error "Buffer %S doesn't exist" buffer-B))
979 (let ((buffer-A
980 (ediff-clone-buffer-for-region-comparison buffer-A "-Region.A-"))
981 (buffer-B
982 (ediff-clone-buffer-for-region-comparison buffer-B "-Region.B-"))
983 reg-A-beg reg-A-end reg-B-beg reg-B-end)
984 (save-excursion
985 (set-buffer buffer-A)
986 (setq reg-A-beg (region-beginning)
987 reg-A-end (region-end))
988 ;; enlarge the region to hold full lines
989 (goto-char reg-A-beg)
990 (beginning-of-line)
991 (setq reg-A-beg (point))
992 (goto-char reg-A-end)
993 (end-of-line)
994 (or (eobp) (forward-char)) ; include the newline char
995 (setq reg-A-end (point))
997 (set-buffer buffer-B)
998 (setq reg-B-beg (region-beginning)
999 reg-B-end (region-end))
1000 ;; enlarge the region to hold full lines
1001 (goto-char reg-B-beg)
1002 (beginning-of-line)
1003 (setq reg-B-beg (point))
1004 (goto-char reg-B-end)
1005 (end-of-line)
1006 (or (eobp) (forward-char)) ; include the newline char
1007 (setq reg-B-end (point))
1008 ) ; save excursion
1010 (ediff-regions-internal
1011 (get-buffer buffer-A) reg-A-beg reg-A-end
1012 (get-buffer buffer-B) reg-B-beg reg-B-end
1013 startup-hooks 'ediff-regions-linewise nil nil))) ; no word mode
1015 ;; compare region beg-A to end-A of buffer-A
1016 ;; to regions beg-B -- end-B in buffer-B.
1017 (defun ediff-regions-internal (buffer-A beg-A end-A buffer-B beg-B end-B
1018 startup-hooks job-name word-mode
1019 setup-parameters)
1020 (let ((tmp-buffer (get-buffer-create ediff-tmp-buffer))
1021 overl-A overl-B
1022 file-A file-B)
1024 ;; in case beg/end-A/B aren't markers--make them into markers
1025 (ediff-with-current-buffer buffer-A
1026 (setq beg-A (move-marker (make-marker) beg-A)
1027 end-A (move-marker (make-marker) end-A)))
1028 (ediff-with-current-buffer buffer-B
1029 (setq beg-B (move-marker (make-marker) beg-B)
1030 end-B (move-marker (make-marker) end-B)))
1032 ;; make file-A
1033 (if word-mode
1034 (ediff-wordify beg-A end-A buffer-A tmp-buffer)
1035 (ediff-copy-to-buffer beg-A end-A buffer-A tmp-buffer))
1036 (setq file-A (ediff-make-temp-file tmp-buffer "regA"))
1038 ;; make file-B
1039 (if word-mode
1040 (ediff-wordify beg-B end-B buffer-B tmp-buffer)
1041 (ediff-copy-to-buffer beg-B end-B buffer-B tmp-buffer))
1042 (setq file-B (ediff-make-temp-file tmp-buffer "regB"))
1044 (setq overl-A (ediff-make-bullet-proof-overlay beg-A end-A buffer-A))
1045 (setq overl-B (ediff-make-bullet-proof-overlay beg-B end-B buffer-B))
1046 (ediff-setup buffer-A file-A
1047 buffer-B file-B
1048 nil nil ; buffer & file C
1049 (cons `(lambda ()
1050 (delete-file ,file-A)
1051 (delete-file ,file-B))
1052 startup-hooks)
1053 (append
1054 (list (cons 'ediff-word-mode word-mode)
1055 (cons 'ediff-narrow-bounds (list overl-A overl-B))
1056 (cons 'ediff-job-name job-name))
1057 setup-parameters))
1061 ;;; Merge files and buffers
1063 ;;;###autoload
1064 (defalias 'ediff-merge 'ediff-merge-files)
1066 (defsubst ediff-merge-on-startup ()
1067 (ediff-do-merge 0)
1068 ;; Can't remember why this is here, but it may cause the automatically merged
1069 ;; buffer to be lost. So, keep the buffer modified.
1070 ;;(ediff-with-current-buffer ediff-buffer-C
1071 ;; (set-buffer-modified-p nil))
1074 ;;;###autoload
1075 (defun ediff-merge-files (file-A file-B
1076 ;; MERGE-BUFFER-FILE is the file to be
1077 ;; associated with the merge buffer
1078 &optional startup-hooks merge-buffer-file)
1079 "Merge two files without ancestor."
1080 (interactive
1081 (let ((dir-A (if ediff-use-last-dir
1082 ediff-last-dir-A
1083 default-directory))
1084 dir-B f)
1085 (list (setq f (ediff-read-file-name
1086 "File A to merge"
1087 dir-A
1088 (ediff-get-default-file-name)
1089 'no-dirs))
1090 (ediff-read-file-name "File B to merge"
1091 (setq dir-B
1092 (if ediff-use-last-dir
1093 ediff-last-dir-B
1094 (file-name-directory f)))
1095 (progn
1096 (ediff-add-to-history
1097 'file-name-history
1098 (ediff-abbreviate-file-name
1099 (expand-file-name
1100 (file-name-nondirectory f)
1101 dir-B)))
1102 (ediff-get-default-file-name f 1)))
1104 (setq startup-hooks (cons 'ediff-merge-on-startup startup-hooks))
1105 (ediff-files-internal file-A
1106 (if (file-directory-p file-B)
1107 (expand-file-name
1108 (file-name-nondirectory file-A) file-B)
1109 file-B)
1110 nil ; file-C
1111 startup-hooks
1112 'ediff-merge-files
1113 merge-buffer-file))
1115 ;;;###autoload
1116 (defun ediff-merge-files-with-ancestor (file-A file-B file-ancestor
1117 &optional
1118 startup-hooks
1119 ;; MERGE-BUFFER-FILE is the file
1120 ;; to be associated with the
1121 ;; merge buffer
1122 merge-buffer-file)
1123 "Merge two files with ancestor."
1124 (interactive
1125 (let ((dir-A (if ediff-use-last-dir
1126 ediff-last-dir-A
1127 default-directory))
1128 dir-B dir-ancestor f ff)
1129 (list (setq f (ediff-read-file-name
1130 "File A to merge"
1131 dir-A
1132 (ediff-get-default-file-name)
1133 'no-dirs))
1134 (setq ff (ediff-read-file-name "File B to merge"
1135 (setq dir-B
1136 (if ediff-use-last-dir
1137 ediff-last-dir-B
1138 (file-name-directory f)))
1139 (progn
1140 (ediff-add-to-history
1141 'file-name-history
1142 (ediff-abbreviate-file-name
1143 (expand-file-name
1144 (file-name-nondirectory f)
1145 dir-B)))
1146 (ediff-get-default-file-name f 1))))
1147 (ediff-read-file-name "Ancestor file"
1148 (setq dir-ancestor
1149 (if ediff-use-last-dir
1150 ediff-last-dir-ancestor
1151 (file-name-directory ff)))
1152 (progn
1153 (ediff-add-to-history
1154 'file-name-history
1155 (ediff-abbreviate-file-name
1156 (expand-file-name
1157 (file-name-nondirectory ff)
1158 dir-ancestor)))
1159 (ediff-get-default-file-name ff 2)))
1161 (setq startup-hooks (cons 'ediff-merge-on-startup startup-hooks))
1162 (ediff-files-internal file-A
1163 (if (file-directory-p file-B)
1164 (expand-file-name
1165 (file-name-nondirectory file-A) file-B)
1166 file-B)
1167 file-ancestor
1168 startup-hooks
1169 'ediff-merge-files-with-ancestor
1170 merge-buffer-file))
1172 ;;;###autoload
1173 (defalias 'ediff-merge-with-ancestor 'ediff-merge-files-with-ancestor)
1175 ;;;###autoload
1176 (defun ediff-merge-buffers (buffer-A buffer-B
1177 &optional
1178 ;; MERGE-BUFFER-FILE is the file to be
1179 ;; associated with the merge buffer
1180 startup-hooks job-name merge-buffer-file)
1181 "Merge buffers without ancestor."
1182 (interactive
1183 (let (bf)
1184 (list (setq bf (read-buffer "Buffer A to merge: "
1185 (ediff-other-buffer "") t))
1186 (read-buffer "Buffer B to merge: "
1187 (progn
1188 ;; realign buffers so that two visible bufs will be
1189 ;; at the top
1190 (save-window-excursion (other-window 1))
1191 (ediff-other-buffer bf))
1192 t))))
1194 (setq startup-hooks (cons 'ediff-merge-on-startup startup-hooks))
1195 (or job-name (setq job-name 'ediff-merge-buffers))
1196 (ediff-buffers-internal
1197 buffer-A buffer-B nil startup-hooks job-name merge-buffer-file))
1199 ;;;###autoload
1200 (defun ediff-merge-buffers-with-ancestor (buffer-A buffer-B buffer-ancestor
1201 &optional
1202 startup-hooks
1203 job-name
1204 ;; MERGE-BUFFER-FILE is the
1205 ;; file to be associated
1206 ;; with the merge buffer
1207 merge-buffer-file)
1208 "Merge buffers with ancestor."
1209 (interactive
1210 (let (bf bff)
1211 (list (setq bf (read-buffer "Buffer A to merge: "
1212 (ediff-other-buffer "") t))
1213 (setq bff (read-buffer "Buffer B to merge: "
1214 (progn
1215 ;; realign buffers so that two visible
1216 ;; bufs will be at the top
1217 (save-window-excursion (other-window 1))
1218 (ediff-other-buffer bf))
1220 (read-buffer "Ancestor buffer: "
1221 (progn
1222 ;; realign buffers so that three visible
1223 ;; bufs will be at the top
1224 (save-window-excursion (other-window 1))
1225 (ediff-other-buffer (list bf bff)))
1229 (setq startup-hooks (cons 'ediff-merge-on-startup startup-hooks))
1230 (or job-name (setq job-name 'ediff-merge-buffers-with-ancestor))
1231 (ediff-buffers-internal
1232 buffer-A buffer-B buffer-ancestor startup-hooks job-name merge-buffer-file))
1235 ;;;###autoload
1236 (defun ediff-merge-revisions (&optional file startup-hooks merge-buffer-file)
1237 ;; MERGE-BUFFER-FILE is the file to be associated with the merge buffer
1238 "Run Ediff by merging two revisions of a file.
1239 The file is the optional FILE argument or the file visited by the current
1240 buffer."
1241 (interactive)
1242 (if (stringp file) (find-file file))
1243 (let (rev1 rev2)
1244 (setq rev1
1245 (read-string
1246 (format
1247 "Version 1 to merge (default %s's working version): "
1248 (if (stringp file)
1249 (file-name-nondirectory file) "current buffer")))
1250 rev2
1251 (read-string
1252 (format
1253 "Version 2 to merge (default %s): "
1254 (if (stringp file)
1255 (file-name-nondirectory file) "current buffer"))))
1256 (ediff-load-version-control)
1257 ;; ancestor-revision=nil
1258 (funcall
1259 (intern (format "ediff-%S-merge-internal" ediff-version-control-package))
1260 rev1 rev2 nil startup-hooks merge-buffer-file)))
1263 ;;;###autoload
1264 (defun ediff-merge-revisions-with-ancestor (&optional
1265 file startup-hooks
1266 ;; MERGE-BUFFER-FILE is the file to
1267 ;; be associated with the merge
1268 ;; buffer
1269 merge-buffer-file)
1270 "Run Ediff by merging two revisions of a file with a common ancestor.
1271 The file is the optional FILE argument or the file visited by the current
1272 buffer."
1273 (interactive)
1274 (if (stringp file) (find-file file))
1275 (let (rev1 rev2 ancestor-rev)
1276 (setq rev1
1277 (read-string
1278 (format
1279 "Version 1 to merge (default %s's working version): "
1280 (if (stringp file)
1281 (file-name-nondirectory file) "current buffer")))
1282 rev2
1283 (read-string
1284 (format
1285 "Version 2 to merge (default %s): "
1286 (if (stringp file)
1287 (file-name-nondirectory file) "current buffer")))
1288 ancestor-rev
1289 (read-string
1290 (format
1291 "Ancestor version (default %s's base revision): "
1292 (if (stringp file)
1293 (file-name-nondirectory file) "current buffer"))))
1294 (ediff-load-version-control)
1295 (funcall
1296 (intern (format "ediff-%S-merge-internal" ediff-version-control-package))
1297 rev1 rev2 ancestor-rev startup-hooks merge-buffer-file)))
1299 ;;; Apply patch
1301 ;;;###autoload
1302 (defun ediff-patch-file (&optional arg patch-buf)
1303 "Run Ediff by patching SOURCE-FILENAME.
1304 If optional PATCH-BUF is given, use the patch in that buffer
1305 and don't ask the user.
1306 If prefix argument, then: if even argument, assume that the patch is in a
1307 buffer. If odd -- assume it is in a file."
1308 (interactive "P")
1309 (let (source-dir source-file)
1310 (require 'ediff-ptch)
1311 (setq patch-buf
1312 (ediff-get-patch-buffer
1313 (if arg (prefix-numeric-value arg)) patch-buf))
1314 (setq source-dir (cond (ediff-use-last-dir ediff-last-dir-patch)
1315 ((and (not ediff-patch-default-directory)
1316 (buffer-file-name patch-buf))
1317 (file-name-directory
1318 (expand-file-name
1319 (buffer-file-name patch-buf))))
1320 (t default-directory)))
1321 (setq source-file
1322 (read-file-name
1323 "File to patch (directory, if multifile patch): "
1324 ;; use an explicit initial file
1325 source-dir nil nil (ediff-get-default-file-name)))
1326 (ediff-dispatch-file-patching-job patch-buf source-file)))
1328 ;;;###autoload
1329 (defun ediff-patch-buffer (&optional arg patch-buf)
1330 "Run Ediff by patching the buffer specified at prompt.
1331 Without the optional prefix ARG, asks if the patch is in some buffer and
1332 prompts for the buffer or a file, depending on the answer.
1333 With ARG=1, assumes the patch is in a file and prompts for the file.
1334 With ARG=2, assumes the patch is in a buffer and prompts for the buffer.
1335 PATCH-BUF is an optional argument, which specifies the buffer that contains the
1336 patch. If not given, the user is prompted according to the prefix argument."
1337 (interactive "P")
1338 (require 'ediff-ptch)
1339 (setq patch-buf
1340 (ediff-get-patch-buffer
1341 (if arg (prefix-numeric-value arg)) patch-buf))
1342 (ediff-patch-buffer-internal
1343 patch-buf
1344 (read-buffer
1345 "Which buffer to patch? "
1346 (ediff-other-buffer patch-buf))))
1349 ;;;###autoload
1350 (defalias 'epatch 'ediff-patch-file)
1351 ;;;###autoload
1352 (defalias 'epatch-buffer 'ediff-patch-buffer)
1357 ;;; Versions Control functions
1359 ;;;###autoload
1360 (defun ediff-revision (&optional file startup-hooks)
1361 "Run Ediff by comparing versions of a file.
1362 The file is an optional FILE argument or the file entered at the prompt.
1363 Default: the file visited by the current buffer.
1364 Uses `vc.el' or `rcs.el' depending on `ediff-version-control-package'."
1365 ;; if buffer is non-nil, use that buffer instead of the current buffer
1366 (interactive "P")
1367 (if (not (stringp file))
1368 (setq file
1369 (ediff-read-file-name "Compare revisions for file"
1370 (if ediff-use-last-dir
1371 ediff-last-dir-A
1372 default-directory)
1373 (ediff-get-default-file-name)
1374 'no-dirs)))
1375 (find-file file)
1376 (if (and (buffer-modified-p)
1377 (y-or-n-p (format "Buffer %s is modified. Save buffer? "
1378 (buffer-name))))
1379 (save-buffer (current-buffer)))
1380 (let (rev1 rev2)
1381 (setq rev1
1382 (read-string
1383 (format "Revision 1 to compare (default %s's latest revision): "
1384 (file-name-nondirectory file)))
1385 rev2
1386 (read-string
1387 (format "Revision 2 to compare (default %s's current state): "
1388 (file-name-nondirectory file))))
1389 (ediff-load-version-control)
1390 (funcall
1391 (intern (format "ediff-%S-internal" ediff-version-control-package))
1392 rev1 rev2 startup-hooks)
1396 ;;;###autoload
1397 (defalias 'erevision 'ediff-revision)
1400 ;; Test if version control package is loaded and load if not
1401 ;; Is SILENT is non-nil, don't report error if package is not found.
1402 (defun ediff-load-version-control (&optional silent)
1403 (require 'ediff-vers)
1404 (or (featurep ediff-version-control-package)
1405 (if (locate-library (symbol-name ediff-version-control-package))
1406 (progn
1407 (message "") ; kill the message from `locate-library'
1408 (require ediff-version-control-package))
1409 (or silent
1410 (error "Version control package %S.el not found. Use vc.el instead"
1411 ediff-version-control-package)))))
1414 ;;;###autoload
1415 (defun ediff-version ()
1416 "Return string describing the version of Ediff.
1417 When called interactively, displays the version."
1418 (interactive)
1419 (if (interactive-p)
1420 (message "%s" (ediff-version))
1421 (format "Ediff %s of %s" ediff-version ediff-date)))
1423 ;; info is run first, and will autoload info.el.
1424 (declare-function Info-goto-node "info" (nodename &optional fork))
1426 ;;;###autoload
1427 (defun ediff-documentation (&optional node)
1428 "Display Ediff's manual.
1429 With optional NODE, goes to that node."
1430 (interactive)
1431 (let ((ctl-window ediff-control-window)
1432 (ctl-buf ediff-control-buffer))
1434 (ediff-skip-unsuitable-frames)
1435 (condition-case nil
1436 (progn
1437 (pop-to-buffer (get-buffer-create "*info*"))
1438 (info (if (featurep 'xemacs) "ediff.info" "ediff"))
1439 (if node
1440 (Info-goto-node node)
1441 (message "Type `i' to search for a specific topic"))
1442 (raise-frame (selected-frame)))
1443 (error (beep 1)
1444 (with-output-to-temp-buffer ediff-msg-buffer
1445 (ediff-with-current-buffer standard-output
1446 (fundamental-mode))
1447 (princ ediff-BAD-INFO))
1448 (if (window-live-p ctl-window)
1449 (progn
1450 (select-window ctl-window)
1451 (set-window-buffer ctl-window ctl-buf)))))))
1454 (dolist (mess '("^Errors in diff output. Diff output is in "
1455 "^Hmm... I don't see an Ediff command around here...$"
1456 "^Undocumented command! Type `G' in Ediff Control Panel to drop a note to the Ediff maintainer$"
1457 ": This command runs in Ediff Control Buffer only!$"
1458 ": Invalid op in ediff-check-version$"
1459 "^ediff-shrink-window-C can be used only for merging jobs$"
1460 "^Lost difference info on these directories$"
1461 "^This command is inapplicable in the present context$"
1462 "^This session group has no parent$"
1463 "^Can't hide active session, $"
1464 "^Ediff: something wrong--no multiple diffs buffer$"
1465 "^Can't make context diff for Session $"
1466 "^The patch buffer wasn't found$"
1467 "^Aborted$"
1468 "^This Ediff session is not part of a session group$"
1469 "^No active Ediff sessions or corrupted session registry$"
1470 "^No session info in this line$"
1471 "^`.*' is not an ordinary file$"
1472 "^Patch appears to have failed$"
1473 "^Recomputation of differences cancelled$"
1474 "^No fine differences in this mode$"
1475 "^Lost connection to ancestor buffer...sorry$"
1476 "^Not merging with ancestor$"
1477 "^Don't know how to toggle read-only in buffer "
1478 "Emacs is not running as a window application$"
1479 "^This command makes sense only when merging with an ancestor$"
1480 "^At end of the difference list$"
1481 "^At beginning of the difference list$"
1482 "^Nothing saved for diff .* in buffer "
1483 "^Buffer is out of sync for file "
1484 "^Buffer out of sync for file "
1485 "^Output from `diff' not found$"
1486 "^You forgot to specify a region in buffer "
1487 "^All right. Make up your mind and come back...$"
1488 "^Current buffer is not visiting any file$"
1489 "^Failed to retrieve revision: $"
1490 "^Can't determine display width.$"
1491 "^File `.*' does not exist or is not readable$"
1492 "^File `.*' is a directory$"
1493 "^Buffer .* doesn't exist$"
1494 "^Directories . and . are the same: "
1495 "^Directory merge aborted$"
1496 "^Merge of directory revisions aborted$"
1497 "^Buffer .* doesn't exist$"
1498 "^There is no file to merge$"
1499 "^Version control package .*.el not found. Use vc.el instead$"))
1500 (add-to-list 'debug-ignored-errors mess))
1503 (require 'ediff-util)
1505 (run-hooks 'ediff-load-hook)
1508 ;; Local Variables:
1509 ;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
1510 ;; eval: (put 'ediff-with-current-buffer 'lisp-indent-hook 1)
1511 ;; eval: (put 'ediff-with-current-buffer 'edebug-form-spec '(form body))
1512 ;; End:
1514 ;; arch-tag: 97c71396-db02-4f41-8b48-6a51c3348fcc
1515 ;;; ediff.el ends here