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