Doc and comment fixes re "online" help
[emacs.git] / lisp / vc / ediff.el
blob5418039143fededb6b2316503dc5a2c4a2706eec
1 ;;; ediff.el --- a comprehensive visual interface to diff & patch
3 ;; Copyright (C) 1994-2014 Free Software Foundation, Inc.
5 ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
6 ;; Created: February 2, 1994
7 ;; Keywords: comparing, merging, patching, vc, tools, unix
8 ;; Version: 2.81.4
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.5" "The current version of Ediff")
16 (defconst ediff-date "July 4, 2013" "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 Ediff 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 ;;; Acknowledgments:
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))))
119 (require 'ediff-util)
120 ;; end pacifier
122 (require 'ediff-init)
123 (require 'ediff-mult) ; required because of the registry stuff
125 (defgroup ediff nil
126 "Comprehensive visual interface to `diff' and `patch'."
127 :tag "Ediff"
128 :group 'tools)
131 (defcustom ediff-use-last-dir nil
132 "If t, Ediff will use previous directory as default when reading file name."
133 :type 'boolean
134 :group 'ediff)
136 ;; Last directory used by an Ediff command for file-A.
137 (defvar ediff-last-dir-A nil)
138 ;; Last directory used by an Ediff command for file-B.
139 (defvar ediff-last-dir-B nil)
140 ;; Last directory used by an Ediff command for file-C.
141 (defvar ediff-last-dir-C nil)
142 ;; Last directory used by an Ediff command for the ancestor file.
143 (defvar ediff-last-dir-ancestor nil)
144 ;; Last directory used by an Ediff command as the output directory for merge.
145 (defvar ediff-last-merge-autostore-dir nil)
148 ;; Used as a startup hook to set `_orig' patch file read-only.
149 (defun ediff-set-read-only-in-buf-A ()
150 (ediff-with-current-buffer ediff-buffer-A
151 (setq buffer-read-only t)))
153 (declare-function dired-get-filename "dired"
154 (&optional localp no-error-if-not-filep))
155 (declare-function dired-get-marked-files "dired"
156 (&optional localp arg filter distinguish-one-marked))
158 ;; Return a plausible default for ediff's first file:
159 ;; In dired, return the file number FILENO (or 0) in the list
160 ;; (all-selected-files, filename under the cursor), where directories are
161 ;; ignored. Otherwise, return DEFAULT file name, if non-nil. Else,
162 ;; if the buffer is visiting a file, return that file name.
163 (defun ediff-get-default-file-name (&optional default fileno)
164 (cond ((eq major-mode 'dired-mode)
165 (let ((current (dired-get-filename nil 'no-error))
166 (marked (condition-case nil
167 (dired-get-marked-files 'no-dir)
168 (error nil)))
169 aux-list choices result)
170 (or (integerp fileno) (setq fileno 0))
171 (if (stringp default)
172 (setq aux-list (cons default aux-list)))
173 (if (and (stringp current) (not (file-directory-p current)))
174 (setq aux-list (cons current aux-list)))
175 (setq choices (nconc marked aux-list))
176 (setq result (elt choices fileno))
177 (or result
178 default)))
179 ((stringp default) default)
180 ((buffer-file-name (current-buffer))
181 (file-name-nondirectory (buffer-file-name (current-buffer))))
184 ;;; Compare files/buffers
186 ;;;###autoload
187 (defun ediff-files (file-A file-B &optional startup-hooks)
188 "Run Ediff on a pair of files, FILE-A and FILE-B."
189 (interactive
190 (let ((dir-A (if ediff-use-last-dir
191 ediff-last-dir-A
192 default-directory))
193 dir-B f)
194 (list (setq f (ediff-read-file-name
195 "File A to compare"
196 dir-A
197 (ediff-get-default-file-name)
198 'no-dirs))
199 (ediff-read-file-name "File B to compare"
200 (setq dir-B
201 (if ediff-use-last-dir
202 ediff-last-dir-B
203 (file-name-directory f)))
204 (progn
205 (ediff-add-to-history
206 'file-name-history
207 (ediff-abbreviate-file-name
208 (expand-file-name
209 (file-name-nondirectory f)
210 dir-B)))
211 (ediff-get-default-file-name f 1)))
213 (ediff-files-internal file-A
214 (if (file-directory-p file-B)
215 (expand-file-name
216 (file-name-nondirectory file-A) file-B)
217 file-B)
218 nil ; file-C
219 startup-hooks
220 'ediff-files))
222 ;;;###autoload
223 (defun ediff-files3 (file-A file-B file-C &optional startup-hooks)
224 "Run Ediff on three files, FILE-A, FILE-B, and FILE-C."
225 (interactive
226 (let ((dir-A (if ediff-use-last-dir
227 ediff-last-dir-A
228 default-directory))
229 dir-B dir-C f ff)
230 (list (setq f (ediff-read-file-name
231 "File A to compare"
232 dir-A
233 (ediff-get-default-file-name)
234 'no-dirs))
235 (setq ff (ediff-read-file-name "File B to compare"
236 (setq dir-B
237 (if ediff-use-last-dir
238 ediff-last-dir-B
239 (file-name-directory f)))
240 (progn
241 (ediff-add-to-history
242 'file-name-history
243 (ediff-abbreviate-file-name
244 (expand-file-name
245 (file-name-nondirectory f)
246 dir-B)))
247 (ediff-get-default-file-name f 1))))
248 (ediff-read-file-name "File C to compare"
249 (setq dir-C (if ediff-use-last-dir
250 ediff-last-dir-C
251 (file-name-directory ff)))
252 (progn
253 (ediff-add-to-history
254 'file-name-history
255 (ediff-abbreviate-file-name
256 (expand-file-name
257 (file-name-nondirectory ff)
258 dir-C)))
259 (ediff-get-default-file-name ff 2)))
261 (ediff-files-internal file-A
262 (if (file-directory-p file-B)
263 (expand-file-name
264 (file-name-nondirectory file-A) file-B)
265 file-B)
266 (if (file-directory-p file-C)
267 (expand-file-name
268 (file-name-nondirectory file-A) file-C)
269 file-C)
270 startup-hooks
271 'ediff-files3))
273 ;;;###autoload
274 (defalias 'ediff3 'ediff-files3)
277 (defun ediff-find-file (file-var buffer-name &optional last-dir hooks-var)
278 "Visit FILE and arrange its buffer to Ediff's liking.
279 FILE-VAR is actually a variable symbol whose value must contain a true
280 file name.
281 BUFFER-NAME is a variable symbol, which will get the buffer object into
282 which FILE is read.
283 LAST-DIR is the directory variable symbol where FILE's
284 directory name should be returned. HOOKS-VAR is a variable symbol that will
285 be assigned the hook to be executed after `ediff-startup' is finished.
286 `ediff-find-file' arranges that the temp files it might create will be
287 deleted."
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-current-file ()
366 "Start ediff between current buffer and its file on disk.
367 This command can be used instead of `revert-buffer'. If there is
368 nothing to revert then this command fails."
369 (interactive)
370 ;; This duplicates code from menu-bar.el.
371 (unless (or (not (eq revert-buffer-function 'revert-buffer--default))
372 (not (eq revert-buffer-insert-file-contents-function
373 'revert-buffer-insert-file-contents--default-function))
374 (and buffer-file-number
375 (or (buffer-modified-p)
376 (not (verify-visited-file-modtime
377 (current-buffer))))))
378 (error "Nothing to revert"))
379 (let* ((auto-save-p (and (recent-auto-save-p)
380 buffer-auto-save-file-name
381 (file-readable-p buffer-auto-save-file-name)
382 (y-or-n-p
383 "Buffer has been auto-saved recently. Compare with auto-save file? ")))
384 (file-name (if auto-save-p
385 buffer-auto-save-file-name
386 buffer-file-name))
387 (revert-buf-name (concat "FILE=" file-name))
388 (revert-buf (get-buffer revert-buf-name))
389 (current-major major-mode))
390 (unless file-name
391 (error "Buffer does not seem to be associated with any file"))
392 (when revert-buf
393 (kill-buffer revert-buf)
394 (setq revert-buf nil))
395 (setq revert-buf (get-buffer-create revert-buf-name))
396 (with-current-buffer revert-buf
397 (insert-file-contents file-name)
398 ;; Assume same modes:
399 (funcall current-major))
400 (ediff-buffers revert-buf (current-buffer))))
403 ;;;###autoload
404 (defun ediff-backup (file)
405 "Run Ediff on FILE and its backup file.
406 Uses the latest backup, if there are several numerical backups.
407 If this file is a backup, `ediff' it with its original."
408 (interactive (list (read-file-name "Ediff (file with backup): ")))
409 ;; The code is taken from `diff-backup'.
410 (require 'diff)
411 (let (bak ori)
412 (if (backup-file-name-p file)
413 (setq bak file
414 ori (file-name-sans-versions file))
415 (setq bak (or (diff-latest-backup-file file)
416 (error "No backup found for %s" file))
417 ori file))
418 (ediff-files bak ori)))
420 ;;;###autoload
421 (defun ediff-buffers (buffer-A buffer-B &optional startup-hooks job-name)
422 "Run Ediff on a pair of buffers, BUFFER-A and BUFFER-B."
423 (interactive
424 (let (bf)
425 (list (setq bf (read-buffer "Buffer A to compare: "
426 (ediff-other-buffer "") t))
427 (read-buffer "Buffer B to compare: "
428 (progn
429 ;; realign buffers so that two visible bufs will be
430 ;; at the top
431 (save-window-excursion (other-window 1))
432 (ediff-other-buffer bf))
433 t))))
434 (or job-name (setq job-name 'ediff-buffers))
435 (ediff-buffers-internal buffer-A buffer-B nil startup-hooks job-name))
437 ;;;###autoload
438 (defalias 'ebuffers 'ediff-buffers)
441 ;;;###autoload
442 (defun ediff-buffers3 (buffer-A buffer-B buffer-C
443 &optional startup-hooks job-name)
444 "Run Ediff on three buffers, BUFFER-A, BUFFER-B, and BUFFER-C."
445 (interactive
446 (let (bf bff)
447 (list (setq bf (read-buffer "Buffer A to compare: "
448 (ediff-other-buffer "") t))
449 (setq bff (read-buffer "Buffer B to compare: "
450 (progn
451 ;; realign buffers so that two visible
452 ;; bufs will be at the top
453 (save-window-excursion (other-window 1))
454 (ediff-other-buffer bf))
456 (read-buffer "Buffer C to compare: "
457 (progn
458 ;; realign buffers so that three visible
459 ;; bufs will be at the top
460 (save-window-excursion (other-window 1))
461 (ediff-other-buffer (list bf bff)))
464 (or job-name (setq job-name 'ediff-buffers3))
465 (ediff-buffers-internal buffer-A buffer-B buffer-C startup-hooks job-name))
467 ;;;###autoload
468 (defalias 'ebuffers3 'ediff-buffers3)
472 ;; MERGE-BUFFER-FILE is the file to be associated with the merge buffer
473 (defun ediff-buffers-internal (buf-A buf-B buf-C startup-hooks job-name
474 &optional merge-buffer-file)
475 (let* ((buf-A-file-name (buffer-file-name (get-buffer buf-A)))
476 (buf-B-file-name (buffer-file-name (get-buffer buf-B)))
477 (buf-C-is-alive (ediff-buffer-live-p buf-C))
478 (buf-C-file-name (if buf-C-is-alive
479 (buffer-file-name (get-buffer buf-B))))
480 file-A file-B file-C)
481 (unwind-protect
482 (progn
483 (if (not (ediff-buffer-live-p buf-A))
484 (error "Buffer %S doesn't exist" buf-A))
485 (if (not (ediff-buffer-live-p buf-B))
486 (error "Buffer %S doesn't exist" buf-B))
487 (let ((ediff-job-name job-name))
488 (if (and ediff-3way-comparison-job
489 (not buf-C-is-alive))
490 (error "Buffer %S doesn't exist" buf-C)))
491 (if (stringp buf-A-file-name)
492 (setq buf-A-file-name (file-name-nondirectory buf-A-file-name)))
493 (if (stringp buf-B-file-name)
494 (setq buf-B-file-name (file-name-nondirectory buf-B-file-name)))
495 (if (stringp buf-C-file-name)
496 (setq buf-C-file-name (file-name-nondirectory buf-C-file-name)))
498 (setq file-A (ediff-make-temp-file buf-A buf-A-file-name)
499 file-B (ediff-make-temp-file buf-B buf-B-file-name))
500 (if buf-C-is-alive
501 (setq file-C (ediff-make-temp-file buf-C buf-C-file-name)))
503 (ediff-setup (get-buffer buf-A) file-A
504 (get-buffer buf-B) file-B
505 (if buf-C-is-alive (get-buffer buf-C))
506 file-C
507 (cons `(lambda ()
508 (delete-file ,file-A)
509 (delete-file ,file-B)
510 (if (stringp ,file-C) (delete-file ,file-C)))
511 startup-hooks)
512 (list (cons 'ediff-job-name job-name))
513 merge-buffer-file))
514 (if (and (stringp file-A) (file-exists-p file-A))
515 (delete-file file-A))
516 (if (and (stringp file-B) (file-exists-p file-B))
517 (delete-file file-B))
518 (if (and (stringp file-C) (file-exists-p file-C))
519 (delete-file file-C)))))
522 ;;; Directory and file group operations
524 ;; Get appropriate default name for directory:
525 ;; If ediff-use-last-dir, use ediff-last-dir-A.
526 ;; In dired mode, use the directory that is under the point (if any);
527 ;; otherwise, use default-directory
528 (defun ediff-get-default-directory-name ()
529 (cond (ediff-use-last-dir ediff-last-dir-A)
530 ((eq major-mode 'dired-mode)
531 (let ((f (dired-get-filename nil 'noerror)))
532 (if (and (stringp f) (file-directory-p f))
534 default-directory)))
535 (t default-directory)))
538 ;;;###autoload
539 (defun ediff-directories (dir1 dir2 regexp)
540 "Run Ediff on a pair of directories, DIR1 and DIR2, comparing files that have
541 the same name in both. The third argument, REGEXP, is nil or a regular
542 expression; only file names that match the regexp are considered."
543 (interactive
544 (let ((dir-A (ediff-get-default-directory-name))
545 (default-regexp (eval ediff-default-filtering-regexp))
547 (list (setq f (read-directory-name
548 "Directory A to compare: " dir-A nil 'must-match))
549 (read-directory-name "Directory B to compare: "
550 (if ediff-use-last-dir
551 ediff-last-dir-B
552 (ediff-strip-last-dir f))
553 nil 'must-match)
554 (read-string
555 (if (stringp default-regexp)
556 (format "Filter through regular expression (default %s): "
557 default-regexp)
558 "Filter through regular expression: ")
560 'ediff-filtering-regexp-history
561 (eval ediff-default-filtering-regexp))
563 (ediff-directories-internal
564 dir1 dir2 nil regexp 'ediff-files 'ediff-directories
567 ;;;###autoload
568 (defalias 'edirs 'ediff-directories)
571 ;;;###autoload
572 (defun ediff-directory-revisions (dir1 regexp)
573 "Run Ediff on a directory, DIR1, comparing its files with their revisions.
574 The second argument, REGEXP, is a regular expression that filters the file
575 names. Only the files that are under revision control are taken into account."
576 (interactive
577 (let ((dir-A (ediff-get-default-directory-name))
578 (default-regexp (eval ediff-default-filtering-regexp))
580 (list (read-directory-name
581 "Directory to compare with revision:" dir-A nil 'must-match)
582 (read-string
583 (if (stringp default-regexp)
584 (format "Filter through regular expression (default %s): "
585 default-regexp)
586 "Filter through regular expression: ")
588 'ediff-filtering-regexp-history
589 (eval ediff-default-filtering-regexp))
591 (ediff-directory-revisions-internal
592 dir1 regexp 'ediff-revision 'ediff-directory-revisions
595 ;;;###autoload
596 (defalias 'edir-revisions 'ediff-directory-revisions)
599 ;;;###autoload
600 (defun ediff-directories3 (dir1 dir2 dir3 regexp)
601 "Run Ediff on three directories, DIR1, DIR2, and DIR3, comparing files that
602 have the same name in all three. The last argument, REGEXP, is nil or a
603 regular expression; only file names that match the regexp are considered."
605 (interactive
606 (let ((dir-A (ediff-get-default-directory-name))
607 (default-regexp (eval ediff-default-filtering-regexp))
609 (list (setq f (read-directory-name "Directory A to compare:" dir-A nil))
610 (setq f (read-directory-name "Directory B to compare:"
611 (if ediff-use-last-dir
612 ediff-last-dir-B
613 (ediff-strip-last-dir f))
614 nil 'must-match))
615 (read-directory-name "Directory C to compare:"
616 (if ediff-use-last-dir
617 ediff-last-dir-C
618 (ediff-strip-last-dir f))
619 nil 'must-match)
620 (read-string
621 (if (stringp default-regexp)
622 (format "Filter through regular expression (default %s): "
623 default-regexp)
624 "Filter through regular expression: ")
626 'ediff-filtering-regexp-history
627 (eval ediff-default-filtering-regexp))
629 (ediff-directories-internal
630 dir1 dir2 dir3 regexp 'ediff-files3 'ediff-directories3
633 ;;;###autoload
634 (defalias 'edirs3 'ediff-directories3)
636 ;;;###autoload
637 (defun ediff-merge-directories (dir1 dir2 regexp &optional merge-autostore-dir)
638 "Run Ediff on a pair of directories, DIR1 and DIR2, merging files that have
639 the same name in both. The third argument, REGEXP, is nil or a regular
640 expression; only file names that match the regexp are considered."
641 (interactive
642 (let ((dir-A (ediff-get-default-directory-name))
643 (default-regexp (eval ediff-default-filtering-regexp))
645 (list (setq f (read-directory-name "Directory A to merge:"
646 dir-A nil 'must-match))
647 (read-directory-name "Directory B to merge:"
648 (if ediff-use-last-dir
649 ediff-last-dir-B
650 (ediff-strip-last-dir f))
651 nil 'must-match)
652 (read-string
653 (if (stringp default-regexp)
654 (format "Filter through regular expression (default %s): "
655 default-regexp)
656 "Filter through regular expression: ")
658 'ediff-filtering-regexp-history
659 (eval ediff-default-filtering-regexp))
661 (ediff-directories-internal
662 dir1 dir2 nil regexp 'ediff-merge-files 'ediff-merge-directories
663 nil merge-autostore-dir
666 ;;;###autoload
667 (defalias 'edirs-merge 'ediff-merge-directories)
669 ;;;###autoload
670 (defun ediff-merge-directories-with-ancestor (dir1 dir2 ancestor-dir regexp
671 &optional
672 merge-autostore-dir)
673 "Merge files in directories DIR1 and DIR2 using files in ANCESTOR-DIR as ancestors.
674 Ediff merges files that have identical names in DIR1, DIR2. If a pair of files
675 in DIR1 and DIR2 doesn't have an ancestor in ANCESTOR-DIR, Ediff will merge
676 without ancestor. The fourth argument, REGEXP, is nil or a regular expression;
677 only file names that match the regexp are considered."
678 (interactive
679 (let ((dir-A (ediff-get-default-directory-name))
680 (default-regexp (eval ediff-default-filtering-regexp))
682 (list (setq f (read-directory-name "Directory A to merge:" dir-A nil))
683 (setq f (read-directory-name "Directory B to merge:"
684 (if ediff-use-last-dir
685 ediff-last-dir-B
686 (ediff-strip-last-dir f))
687 nil 'must-match))
688 (read-directory-name "Ancestor directory:"
689 (if ediff-use-last-dir
690 ediff-last-dir-C
691 (ediff-strip-last-dir f))
692 nil 'must-match)
693 (read-string
694 (if (stringp default-regexp)
695 (format "Filter through regular expression (default %s): "
696 default-regexp)
697 "Filter through regular expression: ")
699 'ediff-filtering-regexp-history
700 (eval ediff-default-filtering-regexp))
702 (ediff-directories-internal
703 dir1 dir2 ancestor-dir regexp
704 'ediff-merge-files-with-ancestor 'ediff-merge-directories-with-ancestor
705 nil merge-autostore-dir
708 ;;;###autoload
709 (defun ediff-merge-directory-revisions (dir1 regexp
710 &optional merge-autostore-dir)
711 "Run Ediff on a directory, DIR1, merging its files with their revisions.
712 The second argument, REGEXP, is a regular expression that filters the file
713 names. Only the files that are under revision control are taken into account."
714 (interactive
715 (let ((dir-A (ediff-get-default-directory-name))
716 (default-regexp (eval ediff-default-filtering-regexp))
718 (list (read-directory-name
719 "Directory to merge with revisions:" dir-A nil 'must-match)
720 (read-string
721 (if (stringp default-regexp)
722 (format "Filter through regular expression (default %s): "
723 default-regexp)
724 "Filter through regular expression: ")
726 'ediff-filtering-regexp-history
727 (eval ediff-default-filtering-regexp))
729 (ediff-directory-revisions-internal
730 dir1 regexp 'ediff-merge-revisions 'ediff-merge-directory-revisions
731 nil merge-autostore-dir
734 ;;;###autoload
735 (defalias 'edir-merge-revisions 'ediff-merge-directory-revisions)
737 ;;;###autoload
738 (defun ediff-merge-directory-revisions-with-ancestor (dir1 regexp
739 &optional
740 merge-autostore-dir)
741 "Run Ediff on a directory, DIR1, merging its files with their revisions and ancestors.
742 The second argument, REGEXP, is a regular expression that filters the file
743 names. Only the files that are under revision control are taken into account."
744 (interactive
745 (let ((dir-A (ediff-get-default-directory-name))
746 (default-regexp (eval ediff-default-filtering-regexp))
748 (list (read-directory-name
749 "Directory to merge with revisions and ancestors:"
750 dir-A nil 'must-match)
751 (read-string
752 (if (stringp default-regexp)
753 (format "Filter through regular expression (default %s): "
754 default-regexp)
755 "Filter through regular expression: ")
757 'ediff-filtering-regexp-history
758 (eval ediff-default-filtering-regexp))
760 (ediff-directory-revisions-internal
761 dir1 regexp 'ediff-merge-revisions-with-ancestor
762 'ediff-merge-directory-revisions-with-ancestor
763 nil merge-autostore-dir
766 ;;;###autoload
767 (defalias
768 'edir-merge-revisions-with-ancestor
769 'ediff-merge-directory-revisions-with-ancestor)
771 ;;;###autoload
772 (defalias 'edirs-merge-with-ancestor 'ediff-merge-directories-with-ancestor)
774 ;; Run ediff-action (ediff-files, ediff-merge, ediff-merge-with-ancestors)
775 ;; on a pair of directories (three directories, in case of ancestor).
776 ;; The third argument, REGEXP, is nil or a regular expression;
777 ;; only file names that match the regexp are considered.
778 ;; JOBNAME is the symbol indicating the meta-job to be performed.
779 ;; MERGE-AUTOSTORE-DIR is the directory in which to store merged files.
780 (defun ediff-directories-internal (dir1 dir2 dir3 regexp action jobname
781 &optional startup-hooks
782 merge-autostore-dir)
783 (if (stringp dir3)
784 (setq dir3 (if (file-directory-p dir3) dir3 (file-name-directory dir3))))
786 (cond ((string= dir1 dir2)
787 (error "Directories A and B are the same: %s" dir1))
788 ((and (eq jobname 'ediff-directories3)
789 (string= dir1 dir3))
790 (error "Directories A and C are the same: %s" dir1))
791 ((and (eq jobname 'ediff-directories3)
792 (string= dir2 dir3))
793 (error "Directories B and C are the same: %s" dir1)))
795 (if merge-autostore-dir
796 (or (stringp merge-autostore-dir)
797 (error "%s: Directory for storing merged files must be a string"
798 jobname)))
799 (let (;; dir-diff-struct is of the form (common-list diff-list)
800 ;; It is a structure where ediff-intersect-directories returns
801 ;; commonalities and differences among directories
802 dir-diff-struct
803 meta-buf)
804 (if (and ediff-autostore-merges
805 (ediff-merge-metajob jobname)
806 (not merge-autostore-dir))
807 (setq merge-autostore-dir
808 (read-directory-name "Save merged files in directory: "
809 (if ediff-use-last-dir
810 ediff-last-merge-autostore-dir
811 (ediff-strip-last-dir dir1))
813 'must-match)))
814 ;; verify we are not merging into an orig directory
815 (if merge-autostore-dir
816 (cond ((and (stringp dir1) (string= merge-autostore-dir dir1))
817 (or (y-or-n-p
818 "Directory for saving merged files = Directory A. Sure? ")
819 (error "Directory merge aborted")))
820 ((and (stringp dir2) (string= merge-autostore-dir dir2))
821 (or (y-or-n-p
822 "Directory for saving merged files = Directory B. Sure? ")
823 (error "Directory merge aborted")))
824 ((and (stringp dir3) (string= merge-autostore-dir dir3))
825 (or (y-or-n-p
826 "Directory for saving merged files = Ancestor Directory. Sure? ")
827 (error "Directory merge aborted")))))
829 (setq dir-diff-struct (ediff-intersect-directories
830 jobname
831 regexp dir1 dir2 dir3 merge-autostore-dir))
832 (setq startup-hooks
833 ;; this sets various vars in the meta buffer inside
834 ;; ediff-prepare-meta-buffer
835 (cons `(lambda ()
836 ;; tell what to do if the user clicks on a session record
837 (setq ediff-session-action-function (quote ,action))
838 ;; set ediff-dir-difference-list
839 (setq ediff-dir-difference-list
840 (cdr (quote ,dir-diff-struct))))
841 startup-hooks))
842 (setq meta-buf (ediff-prepare-meta-buffer
843 'ediff-filegroup-action
844 (car dir-diff-struct)
845 "*Ediff Session Group Panel"
846 'ediff-redraw-directory-group-buffer
847 jobname
848 startup-hooks))
849 (ediff-show-meta-buffer meta-buf)
852 ;; MERGE-AUTOSTORE-DIR can be given to tell ediff where to store the merged
853 ;; files
854 (defun ediff-directory-revisions-internal (dir1 regexp action jobname
855 &optional startup-hooks
856 merge-autostore-dir)
857 (setq dir1 (if (file-directory-p dir1) dir1 (file-name-directory dir1)))
859 (if merge-autostore-dir
860 (or (stringp merge-autostore-dir)
861 (error "%S: Directory for storing merged files must be a string"
862 jobname)))
863 (let (file-list meta-buf)
864 (if (and ediff-autostore-merges
865 (ediff-merge-metajob jobname)
866 (not merge-autostore-dir))
867 (setq merge-autostore-dir
868 (read-directory-name "Save merged files in directory: "
869 (if ediff-use-last-dir
870 ediff-last-merge-autostore-dir
871 (ediff-strip-last-dir dir1))
873 'must-match)))
874 ;; verify merge-autostore-dir != dir1
875 (if (and merge-autostore-dir
876 (stringp dir1)
877 (string= merge-autostore-dir dir1))
878 (or (y-or-n-p
879 "Directory for saving merged file = directory A. Sure? ")
880 (error "Merge of directory revisions aborted")))
882 (setq file-list
883 (ediff-get-directory-files-under-revision
884 jobname regexp dir1 merge-autostore-dir))
885 (setq startup-hooks
886 ;; this sets various vars in the meta buffer inside
887 ;; ediff-prepare-meta-buffer
888 (cons `(lambda ()
889 ;; tell what to do if the user clicks on a session record
890 (setq ediff-session-action-function (quote ,action)))
891 startup-hooks))
892 (setq meta-buf (ediff-prepare-meta-buffer
893 'ediff-filegroup-action
894 file-list
895 "*Ediff Session Group Panel"
896 'ediff-redraw-directory-group-buffer
897 jobname
898 startup-hooks))
899 (ediff-show-meta-buffer meta-buf)
903 ;;; Compare regions and windows
905 ;;;###autoload
906 (defun ediff-windows-wordwise (dumb-mode &optional wind-A wind-B startup-hooks)
907 "Compare WIND-A and WIND-B, which are selected by clicking, wordwise.
908 With prefix argument, DUMB-MODE, or on a non-windowing display, works as
909 follows:
910 If WIND-A is nil, use selected window.
911 If WIND-B is nil, use window next to WIND-A."
912 (interactive "P")
913 (ediff-windows dumb-mode wind-A wind-B
914 startup-hooks 'ediff-windows-wordwise 'word-mode))
916 ;;;###autoload
917 (defun ediff-windows-linewise (dumb-mode &optional wind-A wind-B startup-hooks)
918 "Compare WIND-A and WIND-B, which are selected by clicking, linewise.
919 With prefix argument, DUMB-MODE, or on a non-windowing display, works as
920 follows:
921 If WIND-A is nil, use selected window.
922 If WIND-B is nil, use window next to WIND-A."
923 (interactive "P")
924 (ediff-windows dumb-mode wind-A wind-B
925 startup-hooks 'ediff-windows-linewise nil))
927 ;; Compare WIND-A and WIND-B, which are selected by clicking.
928 ;; With prefix argument, DUMB-MODE, or on a non-windowing display,
929 ;; works as follows:
930 ;; If WIND-A is nil, use selected window.
931 ;; If WIND-B is nil, use window next to WIND-A.
932 (defun ediff-windows (dumb-mode wind-A wind-B startup-hooks job-name word-mode)
933 (if (or dumb-mode (not (ediff-window-display-p)))
934 (setq wind-A (ediff-get-next-window wind-A nil)
935 wind-B (ediff-get-next-window wind-B wind-A))
936 (setq wind-A (ediff-get-window-by-clicking wind-A nil 1)
937 wind-B (ediff-get-window-by-clicking wind-B wind-A 2)))
939 (let ((buffer-A (window-buffer wind-A))
940 (buffer-B (window-buffer wind-B))
941 beg-A end-A beg-B end-B)
943 (save-excursion
944 (save-window-excursion
945 (sit-for 0) ; sync before using window-start/end -- a precaution
946 (select-window wind-A)
947 (setq beg-A (window-start)
948 end-A (window-end))
949 (select-window wind-B)
950 (setq beg-B (window-start)
951 end-B (window-end))))
952 (setq buffer-A
953 (ediff-clone-buffer-for-window-comparison
954 buffer-A wind-A "-Window.A-")
955 buffer-B
956 (ediff-clone-buffer-for-window-comparison
957 buffer-B wind-B "-Window.B-"))
958 (ediff-regions-internal
959 buffer-A beg-A end-A buffer-B beg-B end-B
960 startup-hooks job-name word-mode nil)))
963 ;;;###autoload
964 (defun ediff-regions-wordwise (buffer-A buffer-B &optional startup-hooks)
965 "Run Ediff on a pair of regions in specified buffers.
966 Regions \(i.e., point and mark\) can be set in advance or marked interactively.
967 This function is effective only for relatively small regions, up to 200
968 lines. For large regions, use `ediff-regions-linewise'."
969 (interactive
970 (let (bf)
971 (list (setq bf (read-buffer "Region's A buffer: "
972 (ediff-other-buffer "") t))
973 (read-buffer "Region's B buffer: "
974 (progn
975 ;; realign buffers so that two visible bufs will be
976 ;; at the top
977 (save-window-excursion (other-window 1))
978 (ediff-other-buffer bf))
979 t))))
980 (if (not (ediff-buffer-live-p buffer-A))
981 (error "Buffer %S doesn't exist" buffer-A))
982 (if (not (ediff-buffer-live-p buffer-B))
983 (error "Buffer %S doesn't exist" buffer-B))
986 (let ((buffer-A
987 (ediff-clone-buffer-for-region-comparison buffer-A "-Region.A-"))
988 (buffer-B
989 (ediff-clone-buffer-for-region-comparison buffer-B "-Region.B-"))
990 reg-A-beg reg-A-end reg-B-beg reg-B-end)
991 (with-current-buffer buffer-A
992 (setq reg-A-beg (region-beginning)
993 reg-A-end (region-end))
994 (set-buffer buffer-B)
995 (setq reg-B-beg (region-beginning)
996 reg-B-end (region-end)))
998 (ediff-regions-internal
999 (get-buffer buffer-A) reg-A-beg reg-A-end
1000 (get-buffer buffer-B) reg-B-beg reg-B-end
1001 startup-hooks 'ediff-regions-wordwise 'word-mode nil)))
1003 ;;;###autoload
1004 (defun ediff-regions-linewise (buffer-A buffer-B &optional startup-hooks)
1005 "Run Ediff on a pair of regions in specified buffers.
1006 Regions \(i.e., point and mark\) can be set in advance or marked interactively.
1007 Each region is enlarged to contain full lines.
1008 This function is effective for large regions, over 100-200
1009 lines. For small regions, use `ediff-regions-wordwise'."
1010 (interactive
1011 (let (bf)
1012 (list (setq bf (read-buffer "Region A's buffer: "
1013 (ediff-other-buffer "") t))
1014 (read-buffer "Region B's buffer: "
1015 (progn
1016 ;; realign buffers so that two visible bufs will be
1017 ;; at the top
1018 (save-window-excursion (other-window 1))
1019 (ediff-other-buffer bf))
1020 t))))
1021 (if (not (ediff-buffer-live-p buffer-A))
1022 (error "Buffer %S doesn't exist" buffer-A))
1023 (if (not (ediff-buffer-live-p buffer-B))
1024 (error "Buffer %S doesn't exist" buffer-B))
1026 (let ((buffer-A
1027 (ediff-clone-buffer-for-region-comparison buffer-A "-Region.A-"))
1028 (buffer-B
1029 (ediff-clone-buffer-for-region-comparison buffer-B "-Region.B-"))
1030 reg-A-beg reg-A-end reg-B-beg reg-B-end)
1031 (with-current-buffer buffer-A
1032 (setq reg-A-beg (region-beginning)
1033 reg-A-end (region-end))
1034 ;; enlarge the region to hold full lines
1035 (goto-char reg-A-beg)
1036 (beginning-of-line)
1037 (setq reg-A-beg (point))
1038 (goto-char reg-A-end)
1039 (end-of-line)
1040 (or (eobp) (forward-char)) ; include the newline char
1041 (setq reg-A-end (point))
1043 (set-buffer buffer-B)
1044 (setq reg-B-beg (region-beginning)
1045 reg-B-end (region-end))
1046 ;; enlarge the region to hold full lines
1047 (goto-char reg-B-beg)
1048 (beginning-of-line)
1049 (setq reg-B-beg (point))
1050 (goto-char reg-B-end)
1051 (end-of-line)
1052 (or (eobp) (forward-char)) ; include the newline char
1053 (setq reg-B-end (point))
1054 ) ; save excursion
1056 (ediff-regions-internal
1057 (get-buffer buffer-A) reg-A-beg reg-A-end
1058 (get-buffer buffer-B) reg-B-beg reg-B-end
1059 startup-hooks 'ediff-regions-linewise nil nil))) ; no word mode
1061 ;; compare region beg-A to end-A of buffer-A
1062 ;; to regions beg-B -- end-B in buffer-B.
1063 (defun ediff-regions-internal (buffer-A beg-A end-A buffer-B beg-B end-B
1064 startup-hooks job-name word-mode
1065 setup-parameters)
1066 (let ((tmp-buffer (get-buffer-create ediff-tmp-buffer))
1067 overl-A overl-B
1068 file-A file-B)
1069 (unwind-protect
1070 (progn
1071 ;; in case beg/end-A/B aren't markers--make them into markers
1072 (ediff-with-current-buffer buffer-A
1073 (setq beg-A (move-marker (make-marker) beg-A)
1074 end-A (move-marker (make-marker) end-A)))
1075 (ediff-with-current-buffer buffer-B
1076 (setq beg-B (move-marker (make-marker) beg-B)
1077 end-B (move-marker (make-marker) end-B)))
1079 ;; make file-A
1080 (if word-mode
1081 (ediff-wordify beg-A end-A buffer-A tmp-buffer)
1082 (ediff-copy-to-buffer beg-A end-A buffer-A tmp-buffer))
1083 (setq file-A (ediff-make-temp-file tmp-buffer "regA"))
1085 ;; make file-B
1086 (if word-mode
1087 (ediff-wordify beg-B end-B buffer-B tmp-buffer)
1088 (ediff-copy-to-buffer beg-B end-B buffer-B tmp-buffer))
1089 (setq file-B (ediff-make-temp-file tmp-buffer "regB"))
1091 (setq overl-A (ediff-make-bullet-proof-overlay beg-A end-A buffer-A))
1092 (setq overl-B (ediff-make-bullet-proof-overlay beg-B end-B buffer-B))
1093 (ediff-setup buffer-A file-A
1094 buffer-B file-B
1095 nil nil ; buffer & file C
1096 (cons `(lambda ()
1097 (delete-file ,file-A)
1098 (delete-file ,file-B))
1099 startup-hooks)
1100 (append
1101 (list (cons 'ediff-word-mode word-mode)
1102 (cons 'ediff-narrow-bounds (list overl-A overl-B))
1103 (cons 'ediff-job-name job-name))
1104 setup-parameters)))
1105 (if (and (stringp file-A) (file-exists-p file-A))
1106 (delete-file file-A))
1107 (if (and (stringp file-B) (file-exists-p file-B))
1108 (delete-file file-B)))
1112 ;;; Merge files and buffers
1114 ;;;###autoload
1115 (defalias 'ediff-merge 'ediff-merge-files)
1117 (defsubst ediff-merge-on-startup ()
1118 (ediff-do-merge 0)
1119 ;; Can't remember why this is here, but it may cause the automatically merged
1120 ;; buffer to be lost. So, keep the buffer modified.
1121 ;;(ediff-with-current-buffer ediff-buffer-C
1122 ;; (set-buffer-modified-p nil))
1125 ;;;###autoload
1126 (defun ediff-merge-files (file-A file-B
1127 ;; MERGE-BUFFER-FILE is the file to be
1128 ;; associated with the merge buffer
1129 &optional startup-hooks merge-buffer-file)
1130 "Merge two files without ancestor."
1131 (interactive
1132 (let ((dir-A (if ediff-use-last-dir
1133 ediff-last-dir-A
1134 default-directory))
1135 dir-B f)
1136 (list (setq f (ediff-read-file-name
1137 "File A to merge"
1138 dir-A
1139 (ediff-get-default-file-name)
1140 'no-dirs))
1141 (ediff-read-file-name "File B to merge"
1142 (setq dir-B
1143 (if ediff-use-last-dir
1144 ediff-last-dir-B
1145 (file-name-directory f)))
1146 (progn
1147 (ediff-add-to-history
1148 'file-name-history
1149 (ediff-abbreviate-file-name
1150 (expand-file-name
1151 (file-name-nondirectory f)
1152 dir-B)))
1153 (ediff-get-default-file-name f 1)))
1155 (setq startup-hooks (cons 'ediff-merge-on-startup startup-hooks))
1156 (ediff-files-internal file-A
1157 (if (file-directory-p file-B)
1158 (expand-file-name
1159 (file-name-nondirectory file-A) file-B)
1160 file-B)
1161 nil ; file-C
1162 startup-hooks
1163 'ediff-merge-files
1164 merge-buffer-file))
1166 ;;;###autoload
1167 (defun ediff-merge-files-with-ancestor (file-A file-B file-ancestor
1168 &optional
1169 startup-hooks
1170 ;; MERGE-BUFFER-FILE is the file
1171 ;; to be associated with the
1172 ;; merge buffer
1173 merge-buffer-file)
1174 "Merge two files with ancestor."
1175 (interactive
1176 (let ((dir-A (if ediff-use-last-dir
1177 ediff-last-dir-A
1178 default-directory))
1179 dir-B dir-ancestor f ff)
1180 (list (setq f (ediff-read-file-name
1181 "File A to merge"
1182 dir-A
1183 (ediff-get-default-file-name)
1184 'no-dirs))
1185 (setq ff (ediff-read-file-name "File B to merge"
1186 (setq dir-B
1187 (if ediff-use-last-dir
1188 ediff-last-dir-B
1189 (file-name-directory f)))
1190 (progn
1191 (ediff-add-to-history
1192 'file-name-history
1193 (ediff-abbreviate-file-name
1194 (expand-file-name
1195 (file-name-nondirectory f)
1196 dir-B)))
1197 (ediff-get-default-file-name f 1))))
1198 (ediff-read-file-name "Ancestor file"
1199 (setq dir-ancestor
1200 (if ediff-use-last-dir
1201 ediff-last-dir-ancestor
1202 (file-name-directory ff)))
1203 (progn
1204 (ediff-add-to-history
1205 'file-name-history
1206 (ediff-abbreviate-file-name
1207 (expand-file-name
1208 (file-name-nondirectory ff)
1209 dir-ancestor)))
1210 (ediff-get-default-file-name ff 2)))
1212 (setq startup-hooks (cons 'ediff-merge-on-startup startup-hooks))
1213 (ediff-files-internal file-A
1214 (if (file-directory-p file-B)
1215 (expand-file-name
1216 (file-name-nondirectory file-A) file-B)
1217 file-B)
1218 file-ancestor
1219 startup-hooks
1220 'ediff-merge-files-with-ancestor
1221 merge-buffer-file))
1223 ;;;###autoload
1224 (defalias 'ediff-merge-with-ancestor 'ediff-merge-files-with-ancestor)
1226 ;;;###autoload
1227 (defun ediff-merge-buffers (buffer-A buffer-B
1228 &optional
1229 ;; MERGE-BUFFER-FILE is the file to be
1230 ;; associated with the merge buffer
1231 startup-hooks job-name merge-buffer-file)
1232 "Merge buffers without ancestor."
1233 (interactive
1234 (let (bf)
1235 (list (setq bf (read-buffer "Buffer A to merge: "
1236 (ediff-other-buffer "") t))
1237 (read-buffer "Buffer B to merge: "
1238 (progn
1239 ;; realign buffers so that two visible bufs will be
1240 ;; at the top
1241 (save-window-excursion (other-window 1))
1242 (ediff-other-buffer bf))
1243 t))))
1245 (setq startup-hooks (cons 'ediff-merge-on-startup startup-hooks))
1246 (or job-name (setq job-name 'ediff-merge-buffers))
1247 (ediff-buffers-internal
1248 buffer-A buffer-B nil startup-hooks job-name merge-buffer-file))
1250 ;;;###autoload
1251 (defun ediff-merge-buffers-with-ancestor (buffer-A buffer-B buffer-ancestor
1252 &optional
1253 startup-hooks
1254 job-name
1255 ;; MERGE-BUFFER-FILE is the
1256 ;; file to be associated
1257 ;; with the merge buffer
1258 merge-buffer-file)
1259 "Merge buffers with ancestor."
1260 (interactive
1261 (let (bf bff)
1262 (list (setq bf (read-buffer "Buffer A to merge: "
1263 (ediff-other-buffer "") t))
1264 (setq bff (read-buffer "Buffer B to merge: "
1265 (progn
1266 ;; realign buffers so that two visible
1267 ;; bufs will be at the top
1268 (save-window-excursion (other-window 1))
1269 (ediff-other-buffer bf))
1271 (read-buffer "Ancestor buffer: "
1272 (progn
1273 ;; realign buffers so that three visible
1274 ;; bufs will be at the top
1275 (save-window-excursion (other-window 1))
1276 (ediff-other-buffer (list bf bff)))
1280 (setq startup-hooks (cons 'ediff-merge-on-startup startup-hooks))
1281 (or job-name (setq job-name 'ediff-merge-buffers-with-ancestor))
1282 (ediff-buffers-internal
1283 buffer-A buffer-B buffer-ancestor startup-hooks job-name merge-buffer-file))
1286 ;;;###autoload
1287 (defun ediff-merge-revisions (&optional file startup-hooks merge-buffer-file)
1288 ;; MERGE-BUFFER-FILE is the file to be associated with the merge buffer
1289 "Run Ediff by merging two revisions of a file.
1290 The file is the optional FILE argument or the file visited by the current
1291 buffer."
1292 (interactive)
1293 (if (stringp file) (find-file file))
1294 (let (rev1 rev2)
1295 (setq rev1
1296 (read-string
1297 (format
1298 "Version 1 to merge (default %s's working version): "
1299 (if (stringp file)
1300 (file-name-nondirectory file) "current buffer")))
1301 rev2
1302 (read-string
1303 (format
1304 "Version 2 to merge (default %s): "
1305 (if (stringp file)
1306 (file-name-nondirectory file) "current buffer"))))
1307 (ediff-load-version-control)
1308 ;; ancestor-revision=nil
1309 (funcall
1310 (intern (format "ediff-%S-merge-internal" ediff-version-control-package))
1311 rev1 rev2 nil startup-hooks merge-buffer-file)))
1314 ;;;###autoload
1315 (defun ediff-merge-revisions-with-ancestor (&optional
1316 file startup-hooks
1317 ;; MERGE-BUFFER-FILE is the file to
1318 ;; be associated with the merge
1319 ;; buffer
1320 merge-buffer-file)
1321 "Run Ediff by merging two revisions of a file with a common ancestor.
1322 The file is the optional FILE argument or the file visited by the current
1323 buffer."
1324 (interactive)
1325 (if (stringp file) (find-file file))
1326 (let (rev1 rev2 ancestor-rev)
1327 (setq rev1
1328 (read-string
1329 (format
1330 "Version 1 to merge (default %s's working version): "
1331 (if (stringp file)
1332 (file-name-nondirectory file) "current buffer")))
1333 rev2
1334 (read-string
1335 (format
1336 "Version 2 to merge (default %s): "
1337 (if (stringp file)
1338 (file-name-nondirectory file) "current buffer")))
1339 ancestor-rev
1340 (read-string
1341 (format
1342 "Ancestor version (default %s's base revision): "
1343 (if (stringp file)
1344 (file-name-nondirectory file) "current buffer"))))
1345 (ediff-load-version-control)
1346 (funcall
1347 (intern (format "ediff-%S-merge-internal" ediff-version-control-package))
1348 rev1 rev2 ancestor-rev startup-hooks merge-buffer-file)))
1350 ;;; Apply patch
1351 (defvar ediff-last-dir-patch)
1352 (defvar ediff-patch-default-directory)
1353 (declare-function ediff-get-patch-buffer "ediff-ptch"
1354 (&optional arg patch-buf))
1355 (declare-function ediff-dispatch-file-patching-job "ediff-ptch"
1356 (patch-buf filename &optional startup-hooks))
1358 ;;;###autoload
1359 (defun ediff-patch-file (&optional arg patch-buf)
1360 "Query for a file name, and then run Ediff by patching that file.
1361 If optional PATCH-BUF is given, use the patch in that buffer
1362 and don't ask the user.
1363 If prefix argument, then: if even argument, assume that the patch is in a
1364 buffer. If odd -- assume it is in a file."
1365 (interactive "P")
1366 (let (source-dir source-file)
1367 (require 'ediff-ptch)
1368 (setq patch-buf
1369 (ediff-get-patch-buffer
1370 (if arg (prefix-numeric-value arg)) patch-buf))
1371 (setq source-dir (cond (ediff-use-last-dir ediff-last-dir-patch)
1372 ((and (not ediff-patch-default-directory)
1373 (buffer-file-name patch-buf))
1374 (file-name-directory
1375 (expand-file-name
1376 (buffer-file-name patch-buf))))
1377 (t default-directory)))
1378 (setq source-file
1379 (read-file-name
1380 "File to patch (directory, if multifile patch): "
1381 ;; use an explicit initial file
1382 source-dir nil nil (ediff-get-default-file-name)))
1383 (ediff-dispatch-file-patching-job patch-buf source-file)))
1385 (declare-function ediff-patch-buffer-internal "ediff-ptch"
1386 (patch-buf buf-to-patch-name &optional startup-hooks))
1388 ;;;###autoload
1389 (defun ediff-patch-buffer (&optional arg patch-buf)
1390 "Run Ediff by patching the buffer specified at prompt.
1391 Without the optional prefix ARG, asks if the patch is in some buffer and
1392 prompts for the buffer or a file, depending on the answer.
1393 With ARG=1, assumes the patch is in a file and prompts for the file.
1394 With ARG=2, assumes the patch is in a buffer and prompts for the buffer.
1395 PATCH-BUF is an optional argument, which specifies the buffer that contains the
1396 patch. If not given, the user is prompted according to the prefix argument."
1397 (interactive "P")
1398 (require 'ediff-ptch)
1399 (setq patch-buf
1400 (ediff-get-patch-buffer
1401 (if arg (prefix-numeric-value arg)) patch-buf))
1402 (ediff-patch-buffer-internal
1403 patch-buf
1404 (read-buffer
1405 "Which buffer to patch? "
1406 (ediff-other-buffer patch-buf))))
1409 ;;;###autoload
1410 (defalias 'epatch 'ediff-patch-file)
1411 ;;;###autoload
1412 (defalias 'epatch-buffer 'ediff-patch-buffer)
1417 ;;; Versions Control functions
1419 ;;;###autoload
1420 (defun ediff-revision (&optional file startup-hooks)
1421 "Run Ediff by comparing versions of a file.
1422 The file is an optional FILE argument or the file entered at the prompt.
1423 Default: the file visited by the current buffer.
1424 Uses `vc.el' or `rcs.el' depending on `ediff-version-control-package'."
1425 ;; if buffer is non-nil, use that buffer instead of the current buffer
1426 (interactive "P")
1427 (if (not (stringp file))
1428 (setq file
1429 (ediff-read-file-name "Compare revisions for file"
1430 (if ediff-use-last-dir
1431 ediff-last-dir-A
1432 default-directory)
1433 (ediff-get-default-file-name)
1434 'no-dirs)))
1435 (find-file file)
1436 (if (and (buffer-modified-p)
1437 (y-or-n-p (format "Buffer %s is modified. Save buffer? "
1438 (buffer-name))))
1439 (save-buffer (current-buffer)))
1440 (let (rev1 rev2)
1441 (setq rev1
1442 (read-string
1443 (format "Revision 1 to compare (default %s's latest revision): "
1444 (file-name-nondirectory file)))
1445 rev2
1446 (read-string
1447 (format "Revision 2 to compare (default %s's current state): "
1448 (file-name-nondirectory file))))
1449 (ediff-load-version-control)
1450 (funcall
1451 (intern (format "ediff-%S-internal" ediff-version-control-package))
1452 rev1 rev2 startup-hooks)
1456 ;;;###autoload
1457 (defalias 'erevision 'ediff-revision)
1460 ;; Test if version control package is loaded and load if not
1461 ;; Is SILENT is non-nil, don't report error if package is not found.
1462 (defun ediff-load-version-control (&optional silent)
1463 (require 'ediff-vers)
1464 (or (featurep ediff-version-control-package)
1465 (if (locate-library (symbol-name ediff-version-control-package))
1466 (progn
1467 (message "") ; kill the message from `locate-library'
1468 (require ediff-version-control-package))
1469 (or silent
1470 (error "Version control package %S.el not found. Use vc.el instead"
1471 ediff-version-control-package)))))
1474 ;;;###autoload
1475 (defun ediff-version ()
1476 "Return string describing the version of Ediff.
1477 When called interactively, displays the version."
1478 (interactive)
1479 (if (if (featurep 'xemacs)
1480 (interactive-p)
1481 (called-interactively-p 'interactive))
1482 (message "%s" (ediff-version))
1483 (format "Ediff %s of %s" ediff-version ediff-date)))
1485 ;; info is run first, and will autoload info.el.
1486 (declare-function Info-goto-node "info" (nodename &optional fork strict-case))
1488 ;;;###autoload
1489 (defun ediff-documentation (&optional node)
1490 "Display Ediff's manual.
1491 With optional NODE, goes to that node."
1492 (interactive)
1493 (let ((ctl-window ediff-control-window)
1494 (ctl-buf ediff-control-buffer))
1496 (ediff-skip-unsuitable-frames)
1497 (condition-case nil
1498 (progn
1499 (pop-to-buffer (get-buffer-create "*info*"))
1500 (info (if (featurep 'xemacs) "ediff.info" "ediff"))
1501 (if node
1502 (Info-goto-node node)
1503 (message "Type `i' to search for a specific topic"))
1504 (raise-frame))
1505 (error (beep 1)
1506 (with-output-to-temp-buffer ediff-msg-buffer
1507 (ediff-with-current-buffer standard-output
1508 (fundamental-mode))
1509 (princ ediff-BAD-INFO))
1510 (if (window-live-p ctl-window)
1511 (progn
1512 (select-window ctl-window)
1513 (set-window-buffer ctl-window ctl-buf)))))))
1516 (dolist (mess '("^Errors in diff output. Diff output is in "
1517 "^Hmm... I don't see an Ediff command around here...$"
1518 "^Undocumented command! Type `G' in Ediff Control Panel to drop a note to the Ediff maintainer$"
1519 ": This command runs in Ediff Control Buffer only!$"
1520 ": Invalid op in ediff-check-version$"
1521 "^ediff-shrink-window-C can be used only for merging jobs$"
1522 "^Lost difference info on these directories$"
1523 "^This command is inapplicable in the present context$"
1524 "^This session group has no parent$"
1525 "^Can't hide active session, $"
1526 "^Ediff: something wrong--no multiple diffs buffer$"
1527 "^Can't make context diff for Session $"
1528 "^The patch buffer wasn't found$"
1529 "^Aborted$"
1530 "^This Ediff session is not part of a session group$"
1531 "^No active Ediff sessions or corrupted session registry$"
1532 "^No session info in this line$"
1533 "^`.*' is not an ordinary file$"
1534 "^Patch appears to have failed$"
1535 "^Recomputation of differences cancelled$"
1536 "^No fine differences in this mode$"
1537 "^Lost connection to ancestor buffer...sorry$"
1538 "^Not merging with ancestor$"
1539 "^Don't know how to toggle read-only in buffer "
1540 "Emacs is not running as a window application$"
1541 "^This command makes sense only when merging with an ancestor$"
1542 "^At end of the difference list$"
1543 "^At beginning of the difference list$"
1544 "^Nothing saved for diff .* in buffer "
1545 "^Buffer is out of sync for file "
1546 "^Buffer out of sync for file "
1547 "^Output from `diff' not found$"
1548 "^You forgot to specify a region in buffer "
1549 "^All right. Make up your mind and come back...$"
1550 "^Current buffer is not visiting any file$"
1551 "^Failed to retrieve revision: $"
1552 "^Can't determine display width.$"
1553 "^File `.*' does not exist or is not readable$"
1554 "^File `.*' is a directory$"
1555 "^Buffer .* doesn't exist$"
1556 "^Directories . and . are the same: "
1557 "^Directory merge aborted$"
1558 "^Merge of directory revisions aborted$"
1559 "^Buffer .* doesn't exist$"
1560 "^There is no file to merge$"
1561 "^Version control package .*.el not found. Use vc.el instead$"))
1562 (add-to-list 'debug-ignored-errors mess))
1566 ;;; Command line interface
1568 ;;;###autoload
1569 (defun ediff-files-command ()
1570 (let ((file-a (nth 0 command-line-args-left))
1571 (file-b (nth 1 command-line-args-left)))
1572 (setq command-line-args-left (nthcdr 2 command-line-args-left))
1573 (ediff file-a file-b)))
1575 ;;;###autoload
1576 (defun ediff3-files-command ()
1577 (let ((file-a (nth 0 command-line-args-left))
1578 (file-b (nth 1 command-line-args-left))
1579 (file-c (nth 2 command-line-args-left)))
1580 (setq command-line-args-left (nthcdr 3 command-line-args-left))
1581 (ediff3 file-a file-b file-c)))
1583 ;;;###autoload
1584 (defun ediff-merge-command ()
1585 (let ((file-a (nth 0 command-line-args-left))
1586 (file-b (nth 1 command-line-args-left)))
1587 (setq command-line-args-left (nthcdr 2 command-line-args-left))
1588 (ediff-merge-files file-a file-b)))
1590 ;;;###autoload
1591 (defun ediff-merge-with-ancestor-command ()
1592 (let ((file-a (nth 0 command-line-args-left))
1593 (file-b (nth 1 command-line-args-left))
1594 (ancestor (nth 2 command-line-args-left)))
1595 (setq command-line-args-left (nthcdr 3 command-line-args-left))
1596 (ediff-merge-files-with-ancestor file-a file-b ancestor)))
1598 ;;;###autoload
1599 (defun ediff-directories-command ()
1600 (let ((file-a (nth 0 command-line-args-left))
1601 (file-b (nth 1 command-line-args-left))
1602 (regexp (nth 2 command-line-args-left)))
1603 (setq command-line-args-left (nthcdr 3 command-line-args-left))
1604 (ediff-directories file-a file-b regexp)))
1606 ;;;###autoload
1607 (defun ediff-directories3-command ()
1608 (let ((file-a (nth 0 command-line-args-left))
1609 (file-b (nth 1 command-line-args-left))
1610 (file-c (nth 2 command-line-args-left))
1611 (regexp (nth 3 command-line-args-left)))
1612 (setq command-line-args-left (nthcdr 4 command-line-args-left))
1613 (ediff-directories3 file-a file-b file-c regexp)))
1615 ;;;###autoload
1616 (defun ediff-merge-directories-command ()
1617 (let ((file-a (nth 0 command-line-args-left))
1618 (file-b (nth 1 command-line-args-left))
1619 (regexp (nth 2 command-line-args-left)))
1620 (setq command-line-args-left (nthcdr 3 command-line-args-left))
1621 (ediff-merge-directories file-a file-b regexp)))
1623 ;;;###autoload
1624 (defun ediff-merge-directories-with-ancestor-command ()
1625 (let ((file-a (nth 0 command-line-args-left))
1626 (file-b (nth 1 command-line-args-left))
1627 (ancestor (nth 2 command-line-args-left))
1628 (regexp (nth 3 command-line-args-left)))
1629 (setq command-line-args-left (nthcdr 4 command-line-args-left))
1630 (ediff-merge-directories-with-ancestor file-a file-b ancestor regexp)))
1634 (require 'ediff-util)
1636 (run-hooks 'ediff-load-hook)
1639 ;; Local Variables:
1640 ;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
1641 ;; eval: (put 'ediff-with-current-buffer 'lisp-indent-hook 1)
1642 ;; eval: (put 'ediff-with-current-buffer 'edebug-form-spec '(form body))
1643 ;; End:
1645 ;;; ediff.el ends here