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