(Fset_match_data): Be sure to make search_regs always sane.
[bpt/emacs.git] / lisp / ediff.el
CommitLineData
0f0b0a86 1;;; ediff.el --- a comprehensive visual interface to diff & patch
b578f267 2
55d7ff38 3;; Copyright (C) 1994, 95, 96, 97, 98, 99, 2000 Free Software Foundation, Inc.
813f532d
RS
4
5;; Author: Michael Kifer <kifer@cs.sunysb.edu>
6;; Created: February 2, 1994
fcbadd58 7;; Keywords: comparing, merging, patching, version control.
813f532d 8
657f9cb8
MK
9(defconst ediff-version "2.75" "The current version of Ediff")
10(defconst ediff-date "October 29, 2000" "Date of last update")
bbe6126c 11
eaccd4d8 12
813f532d
RS
13;; This file is part of GNU Emacs.
14
15;; GNU Emacs is free software; you can redistribute it and/or modify
16;; it under the terms of the GNU General Public License as published by
17;; the Free Software Foundation; either version 2, or (at your option)
18;; any later version.
19
20;; GNU Emacs is distributed in the hope that it will be useful,
21;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23;; GNU General Public License for more details.
24
25;; You should have received a copy of the GNU General Public License
b578f267
EN
26;; along with GNU Emacs; see the file COPYING. If not, write to the
27;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28;; Boston, MA 02111-1307, USA.
813f532d 29
813f532d 30;;; Commentary:
813f532d 31
0f0b0a86 32;; Never read that diff output again!
bbe6126c 33;; Apply patch interactively!
0f0b0a86 34;; Merge with ease!
813f532d 35
fcbadd58 36;; This package provides a convenient way of simultaneous browsing through
f1a5512a 37;; the differences between a pair (or a triple) of files or buffers. The
0f0b0a86
KH
38;; files being compared, file-A, file-B, and file-C (if applicable) are
39;; shown in separate windows (side by side, one above the another, or in
40;; separate frames), and the differences are highlighted as you step
41;; through them. You can also copy difference regions from one buffer to
42;; another (and recover old differences if you change your mind).
813f532d 43
87c668b4 44;; Ediff also supports merging operations on files and buffers, including
3af0304a 45;; merging using ancestor versions. Both comparison and merging operations can
87c668b4
MK
46;; be performed on directories, i.e., by pairwise comparison of files in those
47;; directories.
48
813f532d 49;; In addition, Ediff can apply a patch to a file and then let you step
fcbadd58 50;; though both files, the patched and the original one, simultaneously,
813f532d
RS
51;; difference-by-difference. You can even apply a patch right out of a
52;; mail buffer, i.e., patches received by mail don't even have to be saved.
53;; Since Ediff lets you copy differences between buffers, you can, in
54;; effect, apply patches selectively (i.e., you can copy a difference
0f0b0a86 55;; region from file_orig to file, thereby undoing any particular patch that
813f532d
RS
56;; you don't like).
57
f1a5512a 58;; Ediff is aware of version control, which lets the user compare
3af0304a
MK
59;; files with their older versions. Ediff can also work with remote and
60;; compressed files. Details are given below.
f1a5512a 61
bbe6126c 62;; Finally, Ediff supports directory-level comparison, merging and patching.
87c668b4 63;; See the on-line manual for details.
813f532d 64
87c668b4 65;; This package builds upon the ideas borrowed from emerge.el and several
3af0304a 66;; Ediff's functions are adaptations from emerge.el. Much of the functionality
87c668b4
MK
67;; Ediff provides is also influenced by emerge.el.
68
3af0304a
MK
69;; The present version of Ediff supersedes Emerge. It provides a superior user
70;; interface and has numerous major features not found in Emerge. In
87c668b4
MK
71;; particular, it can do patching, and 2-way and 3-way file comparison,
72;; merging, and directory operations.
fcbadd58 73
4ae69eac
MK
74
75
813f532d 76;;; Bugs:
813f532d 77
3af0304a 78;; 1. The undo command doesn't restore deleted regions well. That is, if
813f532d 79;; you delete all characters in a difference region and then invoke
0f0b0a86 80;; `undo', the reinstated text will most likely be inserted outside of
b3a26225 81;; what Ediff thinks is the current difference region. (This problem
0f0b0a86 82;; doesn't seem to exist with XEmacs.)
b3a26225
RS
83;;
84;; If at any point you feel that difference regions are no longer correct,
85;; you can hit '!' to recompute the differences.
86
f1a5512a 87;; 2. On a monochrome display, the repertoire of faces with which to
3af0304a
MK
88;; highlight fine differences is limited. By default, Ediff is using
89;; underlining. However, if the region is already underlined by some other
b3a26225 90;; overlays, there is no simple way to temporarily remove that residual
3af0304a
MK
91;; underlining. This problem occurs when a buffer is highlighted with
92;; hilit19.el or font-lock.el packages. If this residual highlighting gets
93;; in the way, you can do the following. Both font-lock.el and hilit19.el
94;; provide commands for unhighlighting buffers. You can either place these
87c668b4 95;; commands in `ediff-prepare-buffer-hook' (which will unhighlight every
b3a26225
RS
96;; buffer used by Ediff) or you can execute them interactively, at any time
97;; and on any buffer.
0f0b0a86 98
4ae69eac 99
0f0b0a86
KH
100;;; Acknowledgements:
101
87c668b4
MK
102;; Ediff was inspired by Dale R. Worley's <drw@math.mit.edu> emerge.el.
103;; Ediff would not have been possible without the help and encouragement of
3af0304a
MK
104;; its many users. See Ediff on-line Info for the full list of those who
105;; helped. Improved defaults in Ediff file-name reading commands.
0f0b0a86 106
0f0b0a86
KH
107;;; Code:
108
ddc90f39 109(provide 'ediff)
eaccd4d8 110
ddc90f39 111;; Compiler pacifier
1e70790f
MK
112(defvar cvs-cookie-handle)
113(defvar ediff-last-dir-patch)
114(defvar ediff-patch-default-directory)
115
92c51e07
MK
116(and noninteractive
117 (eval-when-compile
92c51e07 118 (load-library "dired")
ddc90f39
MK
119 (load-library "info")
120 (load "pcl-cvs" 'noerror)))
121(eval-when-compile
122 (let ((load-path (cons (expand-file-name ".") load-path)))
123 (or (featurep 'ediff-init)
124 (load "ediff-init.el" nil nil 'nosuffix))
125 (or (featurep 'ediff-mult)
126 (load "ediff-mult.el" nil nil 'nosuffix))
127 (or (featurep 'ediff-ptch)
128 (load "ediff-ptch.el" nil nil 'nosuffix))
129 (or (featurep 'ediff-vers)
130 (load "ediff-vers.el" nil nil 'nosuffix))
131 ))
132;; end pacifier
133
134(require 'ediff-init)
135(require 'ediff-mult) ; required because of the registry stuff
136
137(defgroup ediff nil
138 "A comprehensive visual interface to diff & patch"
34317da2 139 :tag "Ediff"
ddc90f39
MK
140 :group 'tools)
141
142
143(defcustom ediff-use-last-dir nil
1e70790f 144 "*If t, Ediff will use previous directory as default when reading file name."
ddc90f39
MK
145 :type 'boolean
146 :group 'ediff)
bbe6126c 147
1e70790f
MK
148;; Last directory used by an Ediff command for file-A.
149(defvar ediff-last-dir-A nil)
150;; Last directory used by an Ediff command for file-B.
151(defvar ediff-last-dir-B nil)
152;; Last directory used by an Ediff command for file-C.
153(defvar ediff-last-dir-C nil)
154;; Last directory used by an Ediff command for the ancestor file.
155(defvar ediff-last-dir-ancestor nil)
156;; Last directory used by an Ediff command as the output directory for merge.
157(defvar ediff-last-merge-autostore-dir)
813f532d 158
87c668b4 159
4ae69eac 160;; Used as a startup hook to set `_orig' patch file read-only.
f1a5512a 161(defun ediff-set-read-only-in-buf-A ()
e756eb9f 162 (ediff-with-current-buffer ediff-buffer-A
f1a5512a 163 (toggle-read-only 1)))
813f532d 164
87c668b4 165;; Return a plausible default for ediff's first file:
657f9cb8
MK
166;; In dired, return the file number FILENO (or 0) in the list
167;; (all-selected-files, filename under the cursor), where directories are
168;; ignored. Otherwise, return DEFAULT file name, if non-nil. Else,
169;; if the buffer is visiting a file, return that file name.
170(defun ediff-get-default-file-name (&optional default fileno)
87c668b4 171 (cond ((eq major-mode 'dired-mode)
657f9cb8
MK
172 (let ((current (dired-get-filename nil 'no-error))
173 (marked (condition-case nil
174 (dired-get-marked-files 'no-dir)
175 (error)))
176 aux-list choices result)
177 (or (integerp fileno) (setq fileno 0))
178 (if (stringp default)
179 (setq aux-list (cons default aux-list)))
180 (if (and (stringp current) (not (file-directory-p current)))
181 (setq aux-list (cons current aux-list)))
182 (setq choices (nconc marked aux-list))
183 (setq result (elt choices fileno))
184 (or result
185 default)))
186 ((stringp default) default)
87c668b4
MK
187 ((buffer-file-name (current-buffer))
188 (file-name-nondirectory (buffer-file-name (current-buffer))))
189 ))
190
0f0b0a86 191;;; Compare files/buffers
813f532d
RS
192
193;;;###autoload
194(defun ediff-files (file-A file-B &optional startup-hooks)
0f0b0a86 195 "Run Ediff on a pair of files, FILE-A and FILE-B."
813f532d 196 (interactive
0f0b0a86
KH
197 (let ((dir-A (if ediff-use-last-dir
198 ediff-last-dir-A
199 default-directory))
200 dir-B f)
87c668b4
MK
201 (list (setq f (ediff-read-file-name
202 "File A to compare" dir-A
203 (ediff-get-default-file-name)))
813f532d 204 (ediff-read-file-name "File B to compare"
0f0b0a86
KH
205 (setq dir-B
206 (if ediff-use-last-dir
207 ediff-last-dir-B
208 (file-name-directory f)))
209 (progn
210 (setq file-name-history
87c668b4 211 (cons (ediff-abbreviate-file-name
0f0b0a86
KH
212 (expand-file-name
213 (file-name-nondirectory f)
214 dir-B))
215 file-name-history))
657f9cb8 216 (ediff-get-default-file-name f 1)))
0f0b0a86
KH
217 )))
218 (ediff-files-internal file-A
219 (if (file-directory-p file-B)
220 (expand-file-name
221 (file-name-nondirectory file-A) file-B)
222 file-B)
223 nil ; file-C
224 startup-hooks
225 'ediff-files))
226
227;;;###autoload
228(defun ediff-files3 (file-A file-B file-C &optional startup-hooks)
229 "Run Ediff on three files, FILE-A, FILE-B, and FILE-C."
230 (interactive
231 (let ((dir-A (if ediff-use-last-dir
232 ediff-last-dir-A
233 default-directory))
234 dir-B dir-C f ff)
87c668b4
MK
235 (list (setq f (ediff-read-file-name
236 "File A to compare" dir-A
237 (ediff-get-default-file-name)))
0f0b0a86
KH
238 (setq ff (ediff-read-file-name "File B to compare"
239 (setq dir-B
240 (if ediff-use-last-dir
241 ediff-last-dir-B
242 (file-name-directory f)))
243 (progn
244 (setq file-name-history
245 (cons
87c668b4 246 (ediff-abbreviate-file-name
0f0b0a86
KH
247 (expand-file-name
248 (file-name-nondirectory f)
249 dir-B))
250 file-name-history))
657f9cb8 251 (ediff-get-default-file-name f 1))))
0f0b0a86
KH
252 (ediff-read-file-name "File C to compare"
253 (setq dir-C (if ediff-use-last-dir
254 ediff-last-dir-C
255 (file-name-directory ff)))
256 (progn
257 (setq file-name-history
87c668b4 258 (cons (ediff-abbreviate-file-name
0f0b0a86
KH
259 (expand-file-name
260 (file-name-nondirectory ff)
261 dir-C))
262 file-name-history))
657f9cb8 263 (ediff-get-default-file-name ff 2)))
813f532d 264 )))
b3a26225
RS
265 (ediff-files-internal file-A
266 (if (file-directory-p file-B)
267 (expand-file-name
268 (file-name-nondirectory file-A) file-B)
269 file-B)
0f0b0a86
KH
270 (if (file-directory-p file-C)
271 (expand-file-name
272 (file-name-nondirectory file-A) file-C)
273 file-C)
274 startup-hooks
275 'ediff-files3))
813f532d 276
0f0b0a86
KH
277;;;###autoload
278(defalias 'ediff3 'ediff-files3)
813f532d 279
813f532d 280
4ae69eac
MK
281;; Visit FILE and arrange its buffer to Ediff's liking.
282;; FILE is actually a variable symbol that must contain a true file name.
283;; BUFFER-NAME is a variable symbol, which will get the buffer object into
284;; which FILE is read.
285;; LAST-DIR is the directory variable symbol where FILE's
3af0304a 286;; directory name should be returned. HOOKS-VAR is a variable symbol that will
4ae69eac
MK
287;; be assigned the hook to be executed after `ediff-startup' is finished.
288;; `ediff-find-file' arranges that the temp files it might create will be
289;; deleted.
0f0b0a86 290(defun ediff-find-file (file-var buffer-name &optional last-dir hooks-var)
0f0b0a86 291 (let* ((file (symbol-value file-var))
bd698e98 292 (file-magic (ediff-filename-magic-p file))
0f0b0a86 293 (temp-file-name-prefix (file-name-nondirectory file)))
87c668b4
MK
294 (cond ((not (file-readable-p file))
295 (error "File `%s' does not exist or is not readable" file))
296 ((file-directory-p file)
297 (error "File `%s' is a directory" file)))
0f0b0a86 298
4ae69eac 299 ;; some of the commands, below, require full file name
0f0b0a86
KH
300 (setq file (expand-file-name file))
301
302 ;; Record the directory of the file
303 (if last-dir
304 (set last-dir (expand-file-name (file-name-directory file))))
305
306 ;; Setup the buffer
307 (set buffer-name (find-file-noselect file))
308
e756eb9f 309 (ediff-with-current-buffer (symbol-value buffer-name)
0f0b0a86 310 (widen) ; Make sure the entire file is seen
ddc90f39
MK
311 (cond (file-magic ; file has a handler, such as jka-compr-handler or
312 ;;; ange-ftp-hook-function--arrange for temp file
0f0b0a86 313 (ediff-verify-file-buffer 'magic)
87c668b4
MK
314 (setq file
315 (ediff-make-temp-file
316 (current-buffer) temp-file-name-prefix))
086171bf 317 (set hooks-var (cons `(lambda () (delete-file ,file))
0f0b0a86
KH
318 (symbol-value hooks-var))))
319 ;; file processed via auto-mode-alist, a la uncompress.el
320 ((not (equal (file-truename file)
321 (file-truename (buffer-file-name))))
87c668b4
MK
322 (setq file
323 (ediff-make-temp-file
324 (current-buffer) temp-file-name-prefix))
086171bf 325 (set hooks-var (cons `(lambda () (delete-file ,file))
0f0b0a86
KH
326 (symbol-value hooks-var))))
327 (t ;; plain file---just check that the file matches the buffer
328 (ediff-verify-file-buffer))))
329 (set file-var file)))
330
328b4b70
MK
331;; MERGE-BUFFER-FILE is the file to be associated with the merge buffer
332(defun ediff-files-internal (file-A file-B file-C startup-hooks job-name
333 &optional merge-buffer-file)
0f0b0a86 334 (let (buf-A buf-B buf-C)
87c668b4
MK
335 (message "Reading file %s ... " file-A)
336 ;;(sit-for 0)
0f0b0a86 337 (ediff-find-file 'file-A 'buf-A 'ediff-last-dir-A 'startup-hooks)
87c668b4
MK
338 (message "Reading file %s ... " file-B)
339 ;;(sit-for 0)
0f0b0a86 340 (ediff-find-file 'file-B 'buf-B 'ediff-last-dir-B 'startup-hooks)
87c668b4 341 (if (stringp file-C)
0f0b0a86 342 (progn
87c668b4
MK
343 (message "Reading file %s ... " file-C)
344 ;;(sit-for 0)
0f0b0a86
KH
345 (ediff-find-file
346 'file-C 'buf-C
347 (if (eq job-name 'ediff-merge-files-with-ancestor)
348 'ediff-last-dir-ancestor 'ediff-last-dir-C)
349 'startup-hooks)))
350 (ediff-setup buf-A file-A
351 buf-B file-B
352 buf-C file-C
353 startup-hooks
328b4b70
MK
354 (list (cons 'ediff-job-name job-name))
355 merge-buffer-file)))
0f0b0a86
KH
356
357
358;;;###autoload
359(defalias 'ediff 'ediff-files)
813f532d 360
813f532d
RS
361
362;;;###autoload
0f0b0a86 363(defun ediff-buffers (buffer-A buffer-B &optional startup-hooks job-name)
813f532d 364 "Run Ediff on a pair of buffers, BUFFER-A and BUFFER-B."
fcbadd58 365 (interactive
0f0b0a86
KH
366 (let (bf)
367 (list (setq bf (read-buffer "Buffer A to compare: "
368 (ediff-other-buffer "") t))
369 (read-buffer "Buffer B to compare: "
370 (progn
371 ;; realign buffers so that two visible bufs will be
372 ;; at the top
373 (save-window-excursion (other-window 1))
374 (ediff-other-buffer bf))
375 t))))
0f0b0a86
KH
376 (or job-name (setq job-name 'ediff-buffers))
377 (ediff-buffers-internal buffer-A buffer-B nil startup-hooks job-name))
bbe6126c
MK
378
379;;;###autoload
380(defalias 'ebuffers 'ediff-buffers)
381
0f0b0a86
KH
382
383;;;###autoload
384(defun ediff-buffers3 (buffer-A buffer-B buffer-C
385 &optional startup-hooks job-name)
386 "Run Ediff on three buffers, BUFFER-A, BUFFER-B, and BUFFER-C."
387 (interactive
388 (let (bf bff)
389 (list (setq bf (read-buffer "Buffer A to compare: "
390 (ediff-other-buffer "") t))
391 (setq bff (read-buffer "Buffer B to compare: "
392 (progn
393 ;; realign buffers so that two visible
394 ;; bufs will be at the top
395 (save-window-excursion (other-window 1))
396 (ediff-other-buffer bf))
397 t))
398 (read-buffer "Buffer C to compare: "
399 (progn
400 ;; realign buffers so that three visible
401 ;; bufs will be at the top
402 (save-window-excursion (other-window 1))
403 (ediff-other-buffer (list bf bff)))
404 t)
405 )))
0f0b0a86
KH
406 (or job-name (setq job-name 'ediff-buffers3))
407 (ediff-buffers-internal buffer-A buffer-B buffer-C startup-hooks job-name))
bbe6126c
MK
408
409;;;###autoload
410(defalias 'ebuffers3 'ediff-buffers3)
0f0b0a86
KH
411
412
413
328b4b70
MK
414;; MERGE-BUFFER-FILE is the file to be associated with the merge buffer
415(defun ediff-buffers-internal (buf-A buf-B buf-C startup-hooks job-name
416 &optional merge-buffer-file)
0f0b0a86
KH
417 (let* ((buf-A-file-name (buffer-file-name (get-buffer buf-A)))
418 (buf-B-file-name (buffer-file-name (get-buffer buf-B)))
419 (buf-C-is-alive (ediff-buffer-live-p buf-C))
420 (buf-C-file-name (if buf-C-is-alive
421 (buffer-file-name (get-buffer buf-B))))
422 file-A file-B file-C)
423 (if (not (ediff-buffer-live-p buf-A))
424 (error "Buffer %S doesn't exist" buf-A))
425 (if (not (ediff-buffer-live-p buf-B))
426 (error "Buffer %S doesn't exist" buf-B))
427 (let ((ediff-job-name job-name))
428 (if (and ediff-3way-comparison-job
429 (not buf-C-is-alive))
430 (error "Buffer %S doesn't exist" buf-C)))
431 (if (stringp buf-A-file-name)
432 (setq buf-A-file-name (file-name-nondirectory buf-A-file-name)))
433 (if (stringp buf-B-file-name)
434 (setq buf-B-file-name (file-name-nondirectory buf-B-file-name)))
435 (if (stringp buf-C-file-name)
436 (setq buf-C-file-name (file-name-nondirectory buf-C-file-name)))
437
4ae69eac
MK
438 (setq file-A (ediff-make-temp-file buf-A buf-A-file-name)
439 file-B (ediff-make-temp-file buf-B buf-B-file-name))
0f0b0a86 440 (if buf-C-is-alive
87c668b4 441 (setq file-C (ediff-make-temp-file buf-C buf-C-file-name)))
0f0b0a86
KH
442
443 (ediff-setup (get-buffer buf-A) file-A
444 (get-buffer buf-B) file-B
445 (if buf-C-is-alive (get-buffer buf-C))
446 file-C
086171bf
MK
447 (cons `(lambda ()
448 (delete-file ,file-A)
449 (delete-file ,file-B)
450 (if (stringp ,file-C) (delete-file ,file-C)))
0f0b0a86
KH
451 startup-hooks)
452 (list (cons 'ediff-job-name job-name))
328b4b70 453 merge-buffer-file)))
87c668b4
MK
454
455
456;;; Directory and file group operations
457
458;; Get appropriate default name for directory:
459;; If ediff-use-last-dir, use ediff-last-dir-A.
460;; In dired mode, use the directory that is under the point (if any);
461;; otherwise, use default-directory
462(defun ediff-get-default-directory-name ()
463 (cond (ediff-use-last-dir ediff-last-dir-A)
464 ((eq major-mode 'dired-mode)
465 (let ((f (dired-get-filename nil 'noerror)))
466 (if (and (stringp f) (file-directory-p f))
467 f
468 default-directory)))
469 (t default-directory)))
470
471
472;;;###autoload
473(defun ediff-directories (dir1 dir2 regexp)
474 "Run Ediff on a pair of directories, DIR1 and DIR2, comparing files that have
3af0304a
MK
475the same name in both. The third argument, REGEXP, is a regular expression
476that can be used to filter out certain file names."
87c668b4
MK
477 (interactive
478 (let ((dir-A (ediff-get-default-directory-name))
479 f)
bbe6126c
MK
480 (list (setq f (ediff-read-file-name "Directory A to compare:" dir-A nil))
481 (ediff-read-file-name "Directory B to compare:"
87c668b4
MK
482 (if ediff-use-last-dir
483 ediff-last-dir-B
484 (ediff-strip-last-dir f))
485 nil)
486 (read-string "Filter through regular expression: "
bbe6126c 487 nil 'ediff-filtering-regexp-history)
87c668b4
MK
488 )))
489 (ediff-directories-internal
490 dir1 dir2 nil regexp 'ediff-files 'ediff-directories
491 ))
492
493;;;###autoload
494(defalias 'edirs 'ediff-directories)
495
496
497;;;###autoload
498(defun ediff-directory-revisions (dir1 regexp)
499 "Run Ediff on a directory, DIR1, comparing its files with their revisions.
500The second argument, REGEXP, is a regular expression that filters the file
3af0304a 501names. Only the files that are under revision control are taken into account."
87c668b4
MK
502 (interactive
503 (let ((dir-A (ediff-get-default-directory-name)))
504 (list (ediff-read-file-name
bbe6126c 505 "Directory to compare with revision:" dir-A nil)
87c668b4 506 (read-string "Filter through regular expression: "
bbe6126c 507 nil 'ediff-filtering-regexp-history)
87c668b4
MK
508 )))
509 (ediff-directory-revisions-internal
510 dir1 regexp 'ediff-revision 'ediff-directory-revisions
511 ))
512
513;;;###autoload
514(defalias 'edir-revisions 'ediff-directory-revisions)
515
516
517;;;###autoload
518(defun ediff-directories3 (dir1 dir2 dir3 regexp)
519 "Run Ediff on three directories, DIR1, DIR2, and DIR3, comparing files that
3af0304a 520have the same name in all three. The last argument, REGEXP, is a regular
bbe6126c 521expression that can be used to filter out certain file names."
87c668b4
MK
522 (interactive
523 (let ((dir-A (ediff-get-default-directory-name))
524 f)
bbe6126c
MK
525 (list (setq f (ediff-read-file-name "Directory A to compare:" dir-A nil))
526 (setq f (ediff-read-file-name "Directory B to compare:"
87c668b4
MK
527 (if ediff-use-last-dir
528 ediff-last-dir-B
529 (ediff-strip-last-dir f))
530 nil))
bbe6126c 531 (ediff-read-file-name "Directory C to compare:"
87c668b4
MK
532 (if ediff-use-last-dir
533 ediff-last-dir-C
534 (ediff-strip-last-dir f))
535 nil)
536 (read-string "Filter through regular expression: "
bbe6126c 537 nil 'ediff-filtering-regexp-history)
87c668b4
MK
538 )))
539 (ediff-directories-internal
540 dir1 dir2 dir3 regexp 'ediff-files3 'ediff-directories3
541 ))
542
543;;;###autoload
544(defalias 'edirs3 'ediff-directories3)
545
546;;;###autoload
3af0304a 547(defun ediff-merge-directories (dir1 dir2 regexp &optional merge-autostore-dir)
87c668b4 548 "Run Ediff on a pair of directories, DIR1 and DIR2, merging files that have
3af0304a
MK
549the same name in both. The third argument, REGEXP, is a regular expression
550that can be used to filter out certain file names."
87c668b4
MK
551 (interactive
552 (let ((dir-A (ediff-get-default-directory-name))
553 f)
bbe6126c
MK
554 (list (setq f (ediff-read-file-name "Directory A to merge:" dir-A nil))
555 (ediff-read-file-name "Directory B to merge:"
87c668b4
MK
556 (if ediff-use-last-dir
557 ediff-last-dir-B
558 (ediff-strip-last-dir f))
559 nil)
560 (read-string "Filter through regular expression: "
bbe6126c 561 nil 'ediff-filtering-regexp-history)
87c668b4
MK
562 )))
563 (ediff-directories-internal
564 dir1 dir2 nil regexp 'ediff-merge-files 'ediff-merge-directories
3af0304a 565 nil merge-autostore-dir
87c668b4
MK
566 ))
567
568;;;###autoload
569(defalias 'edirs-merge 'ediff-merge-directories)
570
571;;;###autoload
3af0304a
MK
572(defun ediff-merge-directories-with-ancestor (dir1 dir2 ancestor-dir regexp
573 &optional
574 merge-autostore-dir)
bbe6126c 575 "Merge files in directories DIR1 and DIR2 using files in ANCESTOR-DIR as ancestors.
3af0304a 576Ediff merges files that have identical names in DIR1, DIR2. If a pair of files
bbe6126c 577in DIR1 and DIR2 doesn't have an ancestor in ANCESTOR-DIR, Ediff will merge
3af0304a 578without ancestor. The fourth argument, REGEXP, is a regular expression that
bbe6126c 579can be used to filter out certain file names."
87c668b4
MK
580 (interactive
581 (let ((dir-A (ediff-get-default-directory-name))
582 f)
bbe6126c
MK
583 (list (setq f (ediff-read-file-name "Directory A to merge:" dir-A nil))
584 (setq f (ediff-read-file-name "Directory B to merge:"
87c668b4
MK
585 (if ediff-use-last-dir
586 ediff-last-dir-B
587 (ediff-strip-last-dir f))
588 nil))
bbe6126c 589 (ediff-read-file-name "Ancestor directory:"
87c668b4
MK
590 (if ediff-use-last-dir
591 ediff-last-dir-C
592 (ediff-strip-last-dir f))
593 nil)
594 (read-string "Filter through regular expression: "
bbe6126c 595 nil 'ediff-filtering-regexp-history)
87c668b4
MK
596 )))
597 (ediff-directories-internal
bbe6126c 598 dir1 dir2 ancestor-dir regexp
87c668b4 599 'ediff-merge-files-with-ancestor 'ediff-merge-directories-with-ancestor
3af0304a 600 nil merge-autostore-dir
87c668b4
MK
601 ))
602
603;;;###autoload
3af0304a
MK
604(defun ediff-merge-directory-revisions (dir1 regexp
605 &optional merge-autostore-dir)
87c668b4
MK
606 "Run Ediff on a directory, DIR1, merging its files with their revisions.
607The second argument, REGEXP, is a regular expression that filters the file
3af0304a 608names. Only the files that are under revision control are taken into account."
87c668b4 609 (interactive
bf5d92c5 610 (let ((dir-A (ediff-get-default-directory-name)))
87c668b4 611 (list (ediff-read-file-name
bbe6126c 612 "Directory to merge with revisions:" dir-A nil)
87c668b4 613 (read-string "Filter through regular expression: "
bbe6126c 614 nil 'ediff-filtering-regexp-history)
87c668b4
MK
615 )))
616 (ediff-directory-revisions-internal
617 dir1 regexp 'ediff-merge-revisions 'ediff-merge-directory-revisions
3af0304a 618 nil merge-autostore-dir
87c668b4
MK
619 ))
620
621;;;###autoload
622(defalias 'edir-merge-revisions 'ediff-merge-directory-revisions)
623
624;;;###autoload
3af0304a
MK
625(defun ediff-merge-directory-revisions-with-ancestor (dir1 regexp
626 &optional
627 merge-autostore-dir)
87c668b4
MK
628 "Run Ediff on a directory, DIR1, merging its files with their revisions and ancestors.
629The second argument, REGEXP, is a regular expression that filters the file
3af0304a 630names. Only the files that are under revision control are taken into account."
87c668b4 631 (interactive
bf5d92c5 632 (let ((dir-A (ediff-get-default-directory-name)))
87c668b4 633 (list (ediff-read-file-name
bbe6126c 634 "Directory to merge with revisions and ancestors:" dir-A nil)
87c668b4 635 (read-string "Filter through regular expression: "
bbe6126c 636 nil 'ediff-filtering-regexp-history)
87c668b4
MK
637 )))
638 (ediff-directory-revisions-internal
639 dir1 regexp 'ediff-merge-revisions-with-ancestor
640 'ediff-merge-directory-revisions-with-ancestor
3af0304a 641 nil merge-autostore-dir
87c668b4
MK
642 ))
643
644;;;###autoload
645(defalias
646 'edir-merge-revisions-with-ancestor
647 'ediff-merge-directory-revisions-with-ancestor)
648
649;;;###autoload
650(defalias 'edirs-merge-with-ancestor 'ediff-merge-directories-with-ancestor)
651
652;; Run ediff-action (ediff-files, ediff-merge, ediff-merge-with-ancestors)
653;; on a pair of directories (three directories, in case of ancestor).
bbe6126c
MK
654;; The third argument, REGEXP, is a regular expression that can be used to
655;; filter out certain file names.
87c668b4 656;; JOBNAME is the symbol indicating the meta-job to be performed.
3af0304a 657;; MERGE-AUTOSTORE-DIR is the directory in which to store merged files.
92c51e07 658(defun ediff-directories-internal (dir1 dir2 dir3 regexp action jobname
3af0304a
MK
659 &optional startup-hooks
660 merge-autostore-dir)
87c668b4 661 ;; ediff-read-file-name is set to attach a previously entered file name if
3af0304a 662 ;; the currently entered file is a directory. This code takes care of that.
87c668b4
MK
663 (setq dir1 (if (file-directory-p dir1) dir1 (file-name-directory dir1))
664 dir2 (if (file-directory-p dir2) dir2 (file-name-directory dir2)))
665
666 (if (stringp dir3)
667 (setq dir3 (if (file-directory-p dir3) dir3 (file-name-directory dir3))))
668
669 (cond ((string= dir1 dir2)
670 (error "Directories A and B are the same: %s" dir1))
671 ((and (eq jobname 'ediff-directories3)
672 (string= dir1 dir3))
673 (error "Directories A and C are the same: %s" dir1))
674 ((and (eq jobname 'ediff-directories3)
675 (string= dir2 dir3))
676 (error "Directories B and C are the same: %s" dir1)))
677
3af0304a
MK
678 (if merge-autostore-dir
679 (or (stringp merge-autostore-dir)
680 (error "%s: Directory for storing merged files must be a string"
681 jobname)))
87c668b4
MK
682 (let (diffs ; var where ediff-intersect-directories returns the diff list
683 file-list meta-buf)
3af0304a
MK
684 (if (and ediff-autostore-merges
685 (ediff-merge-metajob jobname)
686 (not merge-autostore-dir))
92c51e07 687 (setq merge-autostore-dir
3af0304a
MK
688 (read-file-name "Save merged files in directory: "
689 (if ediff-use-last-dir
92c51e07
MK
690 ediff-last-merge-autostore-dir
691 (ediff-strip-last-dir dir1))
3af0304a
MK
692 nil
693 'must-match)))
92c51e07 694 ;; verify we are not merging into an orig directory
3af0304a 695 (if merge-autostore-dir
92c51e07 696 (cond ((and (stringp dir1) (string= merge-autostore-dir dir1))
3af0304a
MK
697 (or (y-or-n-p
698 "Directory for saving merged files = Directory A. Sure? ")
92c51e07
MK
699 (error "Directory merge aborted")))
700 ((and (stringp dir2) (string= merge-autostore-dir dir2))
3af0304a
MK
701 (or (y-or-n-p
702 "Directory for saving merged files = Directory B. Sure? ")
92c51e07
MK
703 (error "Directory merge aborted")))
704 ((and (stringp dir3) (string= merge-autostore-dir dir3))
705 (or (y-or-n-p
3af0304a 706 "Directory for saving merged files = Ancestor Directory. Sure? ")
92c51e07
MK
707 (error "Directory merge aborted")))))
708
87c668b4 709 (setq file-list (ediff-intersect-directories
92c51e07
MK
710 jobname 'diffs
711 regexp dir1 dir2 dir3 merge-autostore-dir))
87c668b4
MK
712 (setq startup-hooks
713 ;; this sets various vars in the meta buffer inside
714 ;; ediff-prepare-meta-buffer
086171bf
MK
715 (cons `(lambda ()
716 ;; tell what to do if the user clicks on a session record
717 (setq ediff-session-action-function (quote ,action))
718 ;; set ediff-dir-difference-list
719 (setq ediff-dir-difference-list (quote ,diffs)))
87c668b4
MK
720 startup-hooks))
721 (setq meta-buf (ediff-prepare-meta-buffer
bbe6126c 722 'ediff-filegroup-action
87c668b4
MK
723 file-list
724 "*Ediff Session Group Panel"
725 'ediff-redraw-directory-group-buffer
726 jobname
727 startup-hooks))
728 (ediff-show-meta-buffer meta-buf)
729 ))
730
3af0304a
MK
731;; MERGE-AUTOSTORE-DIR can be given to tell ediff where to store the merged
732;; files
87c668b4 733(defun ediff-directory-revisions-internal (dir1 regexp action jobname
3af0304a
MK
734 &optional startup-hooks
735 merge-autostore-dir)
87c668b4 736 (setq dir1 (if (file-directory-p dir1) dir1 (file-name-directory dir1)))
92c51e07 737
3af0304a
MK
738 (if merge-autostore-dir
739 (or (stringp merge-autostore-dir)
740 (error "%S: Directory for storing merged files must be a string"
741 jobname)))
742 (let (file-list meta-buf)
743 (if (and ediff-autostore-merges
744 (ediff-merge-metajob jobname)
745 (not merge-autostore-dir))
92c51e07 746 (setq merge-autostore-dir
3af0304a
MK
747 (read-file-name "Save merged files in directory: "
748 (if ediff-use-last-dir
749 ediff-last-merge-autostore-dir
750 (ediff-strip-last-dir dir1))
751 nil
752 'must-match)))
92c51e07 753 ;; verify merge-autostore-dir != dir1
3af0304a 754 (if (and merge-autostore-dir
92c51e07
MK
755 (stringp dir1)
756 (string= merge-autostore-dir dir1))
757 (or (y-or-n-p
3af0304a 758 "Directory for saving merged file = directory A. Sure? ")
92c51e07
MK
759 (error "Merge of directory revisions aborted")))
760
87c668b4 761 (setq file-list
92c51e07
MK
762 (ediff-get-directory-files-under-revision
763 jobname regexp dir1 merge-autostore-dir))
87c668b4
MK
764 (setq startup-hooks
765 ;; this sets various vars in the meta buffer inside
766 ;; ediff-prepare-meta-buffer
086171bf
MK
767 (cons `(lambda ()
768 ;; tell what to do if the user clicks on a session record
769 (setq ediff-session-action-function (quote ,action)))
87c668b4
MK
770 startup-hooks))
771 (setq meta-buf (ediff-prepare-meta-buffer
bbe6126c 772 'ediff-filegroup-action
87c668b4
MK
773 file-list
774 "*Ediff Session Group Panel"
775 'ediff-redraw-directory-group-buffer
776 jobname
777 startup-hooks))
778 (ediff-show-meta-buffer meta-buf)
779 ))
0f0b0a86
KH
780
781
782;;; Compare regions and windows
783
784;;;###autoload
f1a5512a
KH
785(defun ediff-windows-wordwise (dumb-mode &optional wind-A wind-B startup-hooks)
786 "Compare WIND-A and WIND-B, which are selected by clicking, wordwise.
0f0b0a86
KH
787With prefix argument, DUMB-MODE, or on a non-windowing display, works as
788follows:
789If WIND-A is nil, use selected window.
790If WIND-B is nil, use window next to WIND-A."
0f0b0a86 791 (interactive "P")
f1a5512a
KH
792 (ediff-windows dumb-mode wind-A wind-B
793 startup-hooks 'ediff-windows-wordwise 'word-mode))
794
795;;;###autoload
796(defun ediff-windows-linewise (dumb-mode &optional wind-A wind-B startup-hooks)
797 "Compare WIND-A and WIND-B, which are selected by clicking, linewise.
798With prefix argument, DUMB-MODE, or on a non-windowing display, works as
799follows:
800If WIND-A is nil, use selected window.
801If WIND-B is nil, use window next to WIND-A."
802 (interactive "P")
803 (ediff-windows dumb-mode wind-A wind-B
804 startup-hooks 'ediff-windows-linewise nil))
0f0b0a86 805
f1a5512a
KH
806;; Compare WIND-A and WIND-B, which are selected by clicking.
807;; With prefix argument, DUMB-MODE, or on a non-windowing display,
808;; works as follows:
809;; If WIND-A is nil, use selected window.
810;; If WIND-B is nil, use window next to WIND-A.
811(defun ediff-windows (dumb-mode wind-A wind-B startup-hooks job-name word-mode)
812 (if (or dumb-mode (not (ediff-window-display-p)))
0f0b0a86
KH
813 (setq wind-A (ediff-get-next-window wind-A nil)
814 wind-B (ediff-get-next-window wind-B wind-A))
815 (setq wind-A (ediff-get-window-by-clicking wind-A nil 1)
816 wind-B (ediff-get-window-by-clicking wind-B wind-A 2)))
817
818 (let ((buffer-A (window-buffer wind-A))
819 (buffer-B (window-buffer wind-B))
820 beg-A end-A beg-B end-B)
821
822 (save-excursion
823 (save-window-excursion
a7acbbe4 824 (sit-for 0) ; sync before using window-start/end -- a precaution
0f0b0a86
KH
825 (select-window wind-A)
826 (setq beg-A (window-start)
827 end-A (window-end))
828 (select-window wind-B)
829 (setq beg-B (window-start)
830 end-B (window-end))))
831 (ediff-regions-internal
832 buffer-A beg-A end-A buffer-B beg-B end-B
ddc90f39 833 startup-hooks job-name word-mode nil)))
0f0b0a86
KH
834
835;;;###autoload
f1a5512a 836(defun ediff-regions-wordwise (buffer-A buffer-B &optional startup-hooks)
0f0b0a86
KH
837 "Run Ediff on a pair of regions in two different buffers.
838Regions \(i.e., point and mark\) are assumed to be set in advance.
839This function is effective only for relatively small regions, up to 200
3af0304a 840lines. For large regions, use `ediff-regions-linewise'."
0f0b0a86
KH
841 (interactive
842 (let (bf)
843 (list (setq bf (read-buffer "Region's A buffer: "
844 (ediff-other-buffer "") t))
845 (read-buffer "Region's B buffer: "
846 (progn
847 ;; realign buffers so that two visible bufs will be
848 ;; at the top
849 (save-window-excursion (other-window 1))
850 (ediff-other-buffer bf))
851 t))))
852 (if (not (ediff-buffer-live-p buffer-A))
853 (error "Buffer %S doesn't exist" buffer-A))
854 (if (not (ediff-buffer-live-p buffer-B))
855 (error "Buffer %S doesn't exist" buffer-B))
856
857
858 (let (reg-A-beg reg-A-end reg-B-beg reg-B-end)
859 (save-excursion
860 (set-buffer buffer-A)
861 (setq reg-A-beg (region-beginning)
862 reg-A-end (region-end))
863 (set-buffer buffer-B)
864 (setq reg-B-beg (region-beginning)
865 reg-B-end (region-end)))
866
867 (ediff-regions-internal
868 (get-buffer buffer-A) reg-A-beg reg-A-end
869 (get-buffer buffer-B) reg-B-beg reg-B-end
ddc90f39 870 startup-hooks 'ediff-regions-wordwise 'word-mode nil)))
0f0b0a86
KH
871
872;;;###autoload
f1a5512a 873(defun ediff-regions-linewise (buffer-A buffer-B &optional startup-hooks)
0f0b0a86
KH
874 "Run Ediff on a pair of regions in two different buffers.
875Regions \(i.e., point and mark\) are assumed to be set in advance.
876Each region is enlarged to contain full lines.
877This function is effective for large regions, over 100-200
3af0304a 878lines. For small regions, use `ediff-regions-wordwise'."
0f0b0a86
KH
879 (interactive
880 (let (bf)
881 (list (setq bf (read-buffer "Region A's buffer: "
882 (ediff-other-buffer "") t))
883 (read-buffer "Region B's buffer: "
884 (progn
885 ;; realign buffers so that two visible bufs will be
886 ;; at the top
887 (save-window-excursion (other-window 1))
888 (ediff-other-buffer bf))
889 t))))
fcbadd58 890 (if (not (ediff-buffer-live-p buffer-A))
0f0b0a86 891 (error "Buffer %S doesn't exist" buffer-A))
fcbadd58 892 (if (not (ediff-buffer-live-p buffer-B))
0f0b0a86
KH
893 (error "Buffer %S doesn't exist" buffer-B))
894
895 (let (reg-A-beg reg-A-end reg-B-beg reg-B-end)
896 (save-excursion
897 (set-buffer buffer-A)
898 (setq reg-A-beg (region-beginning)
899 reg-A-end (region-end))
900 ;; enlarge the region to hold full lines
901 (goto-char reg-A-beg)
902 (beginning-of-line)
903 (setq reg-A-beg (point))
904 (goto-char reg-A-end)
905 (end-of-line)
906 (or (eobp) (forward-char)) ; include the newline char
907 (setq reg-A-end (point))
fcbadd58 908
0f0b0a86
KH
909 (set-buffer buffer-B)
910 (setq reg-B-beg (region-beginning)
911 reg-B-end (region-end))
912 ;; enlarge the region to hold full lines
0f0b0a86
KH
913 (goto-char reg-B-beg)
914 (beginning-of-line)
915 (setq reg-B-beg (point))
916 (goto-char reg-B-end)
917 (end-of-line)
918 (or (eobp) (forward-char)) ; include the newline char
919 (setq reg-B-end (point))
920 ) ; save excursion
921
922 (ediff-regions-internal
923 (get-buffer buffer-A) reg-A-beg reg-A-end
924 (get-buffer buffer-B) reg-B-beg reg-B-end
1e70790f 925 startup-hooks 'ediff-regions-linewise nil nil))) ; no word mode
0f0b0a86
KH
926
927;; compare region beg-A to end-A of buffer-A
928;; to regions beg-B -- end-B in buffer-B.
929(defun ediff-regions-internal (buffer-A beg-A end-A buffer-B beg-B end-B
ddc90f39
MK
930 startup-hooks job-name word-mode
931 setup-parameters)
0f0b0a86
KH
932 (let ((tmp-buffer (get-buffer-create ediff-tmp-buffer))
933 overl-A overl-B
934 file-A file-B)
935
936 ;; in case beg/end-A/B aren't markers--make them into markers
e756eb9f 937 (ediff-with-current-buffer buffer-A
0f0b0a86
KH
938 (setq beg-A (move-marker (make-marker) beg-A)
939 end-A (move-marker (make-marker) end-A)))
e756eb9f 940 (ediff-with-current-buffer buffer-B
0f0b0a86
KH
941 (setq beg-B (move-marker (make-marker) beg-B)
942 end-B (move-marker (make-marker) end-B)))
943
944 (if (and (eq buffer-A buffer-B)
945 (or (and (< beg-A end-B) (<= beg-B beg-A)) ; b-B b-A e-B
946 (and (< beg-B end-A) (<= end-A end-B)))) ; b-B e-A e-B
947 (progn
948 (with-output-to-temp-buffer ediff-msg-buffer
949 (princ "
950You have requested to compare overlapping regions of the same buffer.
951
952In this case, Ediff's highlighting may be confusing---in the same window,
953you may see highlighted regions that belong to different regions.
954
955Continue anyway? (y/n) "))
956
957 (if (y-or-n-p "Continue anyway? ")
958 ()
959 (error "%S aborted" job-name))))
960
961 ;; make file-A
962 (if word-mode
963 (ediff-wordify beg-A end-A buffer-A tmp-buffer)
964 (ediff-copy-to-buffer beg-A end-A buffer-A tmp-buffer))
87c668b4 965 (setq file-A (ediff-make-temp-file tmp-buffer "regA"))
0f0b0a86
KH
966
967 ;; make file-B
968 (if word-mode
969 (ediff-wordify beg-B end-B buffer-B tmp-buffer)
970 (ediff-copy-to-buffer beg-B end-B buffer-B tmp-buffer))
87c668b4 971 (setq file-B (ediff-make-temp-file tmp-buffer "regB"))
0f0b0a86
KH
972
973 (setq overl-A (ediff-make-bullet-proof-overlay beg-A end-A buffer-A))
974 (setq overl-B (ediff-make-bullet-proof-overlay beg-B end-B buffer-B))
975 (ediff-setup buffer-A file-A
976 buffer-B file-B
977 nil nil ; buffer & file C
086171bf
MK
978 (cons `(lambda ()
979 (delete-file ,file-A)
980 (delete-file ,file-B))
813f532d 981 startup-hooks)
ddc90f39
MK
982 (append
983 (list (cons 'ediff-word-mode word-mode)
984 (cons 'ediff-narrow-bounds (list overl-A overl-B))
985 (cons 'ediff-job-name job-name))
328b4b70 986 setup-parameters))
0f0b0a86
KH
987 ))
988
989
990;;; Merge files and buffers
991
992;;;###autoload
993(defalias 'ediff-merge 'ediff-merge-files)
994
995(defsubst ediff-merge-on-startup ()
996 (ediff-do-merge 0)
e756eb9f 997 (ediff-with-current-buffer ediff-buffer-C
0f0b0a86
KH
998 (set-buffer-modified-p nil)))
999
1000;;;###autoload
328b4b70
MK
1001(defun ediff-merge-files (file-A file-B
1002 ;; MERGE-BUFFER-FILE is the file to be
1003 ;; associated with the merge buffer
1004 &optional startup-hooks merge-buffer-file)
0f0b0a86
KH
1005 "Merge two files without ancestor."
1006 (interactive
1007 (let ((dir-A (if ediff-use-last-dir
1008 ediff-last-dir-A
1009 default-directory))
1010 dir-B f)
87c668b4
MK
1011 (list (setq f (ediff-read-file-name
1012 "File A to merge" dir-A
1013 (ediff-get-default-file-name)))
0f0b0a86
KH
1014 (ediff-read-file-name "File B to merge"
1015 (setq dir-B
1016 (if ediff-use-last-dir
1017 ediff-last-dir-B
1018 (file-name-directory f)))
1019 (progn
1020 (setq file-name-history
87c668b4 1021 (cons (ediff-abbreviate-file-name
0f0b0a86
KH
1022 (expand-file-name
1023 (file-name-nondirectory f)
1024 dir-B))
1025 file-name-history))
657f9cb8 1026 (ediff-get-default-file-name f 1)))
0f0b0a86
KH
1027 )))
1028 (setq startup-hooks (cons 'ediff-merge-on-startup startup-hooks))
1029 (ediff-files-internal file-A
1030 (if (file-directory-p file-B)
1031 (expand-file-name
1032 (file-name-nondirectory file-A) file-B)
1033 file-B)
1034 nil ; file-C
1035 startup-hooks
328b4b70
MK
1036 'ediff-merge-files
1037 merge-buffer-file))
0f0b0a86
KH
1038
1039;;;###autoload
1040(defun ediff-merge-files-with-ancestor (file-A file-B file-ancestor
328b4b70
MK
1041 &optional
1042 startup-hooks
1043 ;; MERGE-BUFFER-FILE is the file
1044 ;; to be associated with the
1045 ;; merge buffer
1046 merge-buffer-file)
0f0b0a86
KH
1047 "Merge two files with ancestor."
1048 (interactive
1049 (let ((dir-A (if ediff-use-last-dir
1050 ediff-last-dir-A
1051 default-directory))
1052 dir-B dir-ancestor f ff)
87c668b4
MK
1053 (list (setq f (ediff-read-file-name
1054 "File A to merge" dir-A
1055 (ediff-get-default-file-name)))
0f0b0a86
KH
1056 (setq ff (ediff-read-file-name "File B to merge"
1057 (setq dir-B
1058 (if ediff-use-last-dir
1059 ediff-last-dir-B
1060 (file-name-directory f)))
1061 (progn
1062 (setq file-name-history
1063 (cons
87c668b4 1064 (ediff-abbreviate-file-name
0f0b0a86
KH
1065 (expand-file-name
1066 (file-name-nondirectory f)
1067 dir-B))
1068 file-name-history))
657f9cb8 1069 (ediff-get-default-file-name f 1))))
0f0b0a86
KH
1070 (ediff-read-file-name "Ancestor file"
1071 (setq dir-ancestor
1072 (if ediff-use-last-dir
1073 ediff-last-dir-ancestor
1074 (file-name-directory ff)))
1075 (progn
1076 (setq file-name-history
87c668b4 1077 (cons (ediff-abbreviate-file-name
0f0b0a86
KH
1078 (expand-file-name
1079 (file-name-nondirectory ff)
1080 dir-ancestor))
1081 file-name-history))
657f9cb8 1082 (ediff-get-default-file-name ff 2)))
0f0b0a86
KH
1083 )))
1084 (setq startup-hooks (cons 'ediff-merge-on-startup startup-hooks))
1085 (ediff-files-internal file-A
1086 (if (file-directory-p file-B)
1087 (expand-file-name
1088 (file-name-nondirectory file-A) file-B)
1089 file-B)
1090 file-ancestor
1091 startup-hooks
328b4b70
MK
1092 'ediff-merge-files-with-ancestor
1093 merge-buffer-file))
0f0b0a86
KH
1094
1095;;;###autoload
1096(defalias 'ediff-merge-with-ancestor 'ediff-merge-files-with-ancestor)
1097
1098;;;###autoload
328b4b70
MK
1099(defun ediff-merge-buffers (buffer-A buffer-B
1100 &optional
1101 ;; MERGE-BUFFER-FILE is the file to be
1102 ;; associated with the merge buffer
1103 startup-hooks job-name merge-buffer-file)
0f0b0a86
KH
1104 "Merge buffers without ancestor."
1105 (interactive
1106 (let (bf)
1107 (list (setq bf (read-buffer "Buffer A to merge: "
1108 (ediff-other-buffer "") t))
1109 (read-buffer "Buffer B to merge: "
1110 (progn
1111 ;; realign buffers so that two visible bufs will be
1112 ;; at the top
1113 (save-window-excursion (other-window 1))
1114 (ediff-other-buffer bf))
1115 t))))
1116
1117 (setq startup-hooks (cons 'ediff-merge-on-startup startup-hooks))
1118 (or job-name (setq job-name 'ediff-merge-buffers))
1119 (ediff-buffers-internal
328b4b70 1120 buffer-A buffer-B nil startup-hooks job-name merge-buffer-file))
0f0b0a86
KH
1121
1122;;;###autoload
328b4b70
MK
1123(defun ediff-merge-buffers-with-ancestor (buffer-A buffer-B buffer-ancestor
1124 &optional
1125 startup-hooks
1126 job-name
1127 ;; MERGE-BUFFER-FILE is the
1128 ;; file to be associated
1129 ;; with the merge buffer
1130 merge-buffer-file)
0f0b0a86
KH
1131 "Merge buffers with ancestor."
1132 (interactive
1133 (let (bf bff)
1134 (list (setq bf (read-buffer "Buffer A to merge: "
1135 (ediff-other-buffer "") t))
1136 (setq bff (read-buffer "Buffer B to merge: "
1137 (progn
1138 ;; realign buffers so that two visible
1139 ;; bufs will be at the top
1140 (save-window-excursion (other-window 1))
1141 (ediff-other-buffer bf))
1142 t))
1143 (read-buffer "Ancestor buffer: "
1144 (progn
1145 ;; realign buffers so that three visible
1146 ;; bufs will be at the top
1147 (save-window-excursion (other-window 1))
1148 (ediff-other-buffer (list bf bff)))
1149 t)
1150 )))
1151
1152 (setq startup-hooks (cons 'ediff-merge-on-startup startup-hooks))
1153 (or job-name (setq job-name 'ediff-merge-buffers-with-ancestor))
1154 (ediff-buffers-internal
328b4b70 1155 buffer-A buffer-B buffer-ancestor startup-hooks job-name merge-buffer-file))
0f0b0a86
KH
1156
1157
1158;;;###autoload
328b4b70
MK
1159(defun ediff-merge-revisions (&optional file startup-hooks merge-buffer-file)
1160 ;; MERGE-BUFFER-FILE is the file to be associated with the merge buffer
0f0b0a86 1161 "Run Ediff by merging two revisions of a file.
87c668b4
MK
1162The file is the optional FILE argument or the file visited by the current
1163buffer."
1164 (interactive)
87c668b4 1165 (if (stringp file) (find-file file))
4ae69eac 1166 (let (rev1 rev2)
87c668b4
MK
1167 (setq rev1
1168 (read-string
1169 (format
1170 "Version 1 to merge (default: %s's latest version): "
1171 (if (stringp file)
1172 (file-name-nondirectory file) "current buffer")))
1173 rev2
1174 (read-string
1175 (format
1176 "Version 2 to merge (default: %s): "
1177 (if (stringp file)
1178 (file-name-nondirectory file) "current buffer"))))
4ae69eac
MK
1179 (ediff-load-version-control)
1180 ;; ancestor-revision=nil
1181 (funcall
92c51e07 1182 (intern (format "ediff-%S-merge-internal" ediff-version-control-package))
328b4b70 1183 rev1 rev2 nil startup-hooks merge-buffer-file)))
0f0b0a86
KH
1184
1185
1186;;;###autoload
328b4b70
MK
1187(defun ediff-merge-revisions-with-ancestor (&optional
1188 file startup-hooks
1189 ;; MERGE-BUFFER-FILE is the file to
1190 ;; be associated with the merge
1191 ;; buffer
1192 merge-buffer-file)
87c668b4
MK
1193 "Run Ediff by merging two revisions of a file with a common ancestor.
1194The file is the the optional FILE argument or the file visited by the current
1195buffer."
1196 (interactive)
87c668b4 1197 (if (stringp file) (find-file file))
4ae69eac 1198 (let (rev1 rev2 ancestor-rev)
87c668b4
MK
1199 (setq rev1
1200 (read-string
1201 (format
1202 "Version 1 to merge (default: %s's latest version): "
1203 (if (stringp file)
1204 (file-name-nondirectory file) "current buffer")))
1205 rev2
1206 (read-string
1207 (format
1208 "Version 2 to merge (default: %s): "
1209 (if (stringp file)
1210 (file-name-nondirectory file) "current buffer")))
1211 ancestor-rev
1212 (read-string
1213 (format
3af0304a 1214 "Ancestor version (default: %s's base revision): "
87c668b4
MK
1215 (if (stringp file)
1216 (file-name-nondirectory file) "current buffer"))))
4ae69eac
MK
1217 (ediff-load-version-control)
1218 (funcall
92c51e07 1219 (intern (format "ediff-%S-merge-internal" ediff-version-control-package))
328b4b70 1220 rev1 rev2 ancestor-rev startup-hooks merge-buffer-file)))
4ae69eac
MK
1221
1222;;;###autoload
1223(defun run-ediff-from-cvs-buffer (pos)
1224 "Run Ediff-merge on appropriate revisions of the selected file.
3af0304a 1225First run after `M-x cvs-update'. Then place the cursor on a line describing a
4ae69eac
MK
1226file and then run `run-ediff-from-cvs-buffer'."
1227 (interactive "d")
1228 (ediff-load-version-control)
1229 (let ((tin (tin-locate cvs-cookie-handle pos)))
1230 (if tin
1231 (cvs-run-ediff-on-file-descriptor tin)
1232 (error "There is no file to merge"))))
0f0b0a86
KH
1233
1234
1235;;; Apply patch
0f0b0a86 1236
bbe6126c 1237;;;###autoload
3af0304a
MK
1238(defun ediff-patch-file (&optional arg patch-buf)
1239 "Run Ediff by patching SOURCE-FILENAME.
1240If optional PATCH-BUF is given, use the patch in that buffer
1241and don't ask the user.
1242If prefix argument, then: if even argument, assume that the patch is in a
1243buffer. If odd -- assume it is in a file."
1244 (interactive "P")
1245 (let (source-dir source-file)
bbe6126c 1246 (require 'ediff-ptch)
3af0304a
MK
1247 (setq patch-buf
1248 (ediff-get-patch-buffer
1249 (if arg (prefix-numeric-value arg)) patch-buf))
bbe6126c
MK
1250 (setq source-dir (cond (ediff-use-last-dir ediff-last-dir-patch)
1251 ((and (not ediff-patch-default-directory)
1252 (buffer-file-name patch-buf))
1253 (file-name-directory
1254 (expand-file-name
1255 (buffer-file-name patch-buf))))
1256 (t default-directory)))
1257 (setq source-file
1258 ;; the default is the directory, not the visited file name
3af0304a
MK
1259 (read-file-name
1260 "File to patch (directory, if multifile patch): "
1261 source-dir (ediff-get-default-file-name)))
bbe6126c 1262 (ediff-dispatch-file-patching-job patch-buf source-file)))
0f0b0a86 1263
bbe6126c 1264;;;###autoload
3af0304a 1265(defun ediff-patch-buffer (&optional arg patch-buf)
bbe6126c 1266 "Run Ediff by patching BUFFER-NAME."
3af0304a
MK
1267 (interactive "P")
1268 (require 'ediff-ptch)
1269 (setq patch-buf
1270 (ediff-get-patch-buffer
1271 (if arg (prefix-numeric-value arg)) patch-buf))
1272 (ediff-patch-buffer-internal
1273 patch-buf
1274 (read-buffer
1275 "Which buffer to patch? "
1276 (ediff-prompt-for-patch-buffer))))
0f0b0a86 1277
3af0304a 1278
bbe6126c
MK
1279;;;###autoload
1280(defalias 'epatch 'ediff-patch-file)
1281;;;###autoload
1282(defalias 'epatch-buffer 'ediff-patch-buffer)
0f0b0a86
KH
1283
1284
813f532d 1285
0f0b0a86 1286\f
813f532d
RS
1287;;; Versions Control functions
1288
1289;;;###autoload
87c668b4
MK
1290(defun ediff-revision (&optional file startup-hooks)
1291 "Run Ediff by comparing versions of a file.
1292The file is an optional FILE argument or the file visited by the current
3af0304a 1293buffer. Use `vc.el' or `rcs.el' depending on `ediff-version-control-package'."
87c668b4 1294 ;; if buffer is non-nil, use that buffer instead of the current buffer
f1a5512a 1295 (interactive "P")
87c668b4 1296 (if (stringp file) (find-file file))
f1a5512a 1297 (let (rev1 rev2)
87c668b4
MK
1298 (setq rev1
1299 (read-string
1300 (format "Version 1 to compare (default: %s's latest version): "
1301 (if (stringp file)
1302 (file-name-nondirectory file) "current buffer")))
1303 rev2
1304 (read-string
1305 (format "Version 2 to compare (default: %s): "
1306 (if (stringp file)
1307 (file-name-nondirectory file) "current buffer"))))
f1a5512a
KH
1308 (ediff-load-version-control)
1309 (funcall
92c51e07 1310 (intern (format "ediff-%S-internal" ediff-version-control-package))
87c668b4 1311 rev1 rev2 startup-hooks)
f1a5512a 1312 ))
1e70790f
MK
1313
1314
1315;;;###autoload
1316(defalias 'erevision 'ediff-revision)
0f0b0a86 1317
0f0b0a86
KH
1318
1319;; Test if version control package is loaded and load if not
1320;; Is SILENT is non-nil, don't report error if package is not found.
1321(defun ediff-load-version-control (&optional silent)
4ae69eac 1322 (require 'ediff-vers)
0f0b0a86
KH
1323 (or (featurep ediff-version-control-package)
1324 (if (locate-library (symbol-name ediff-version-control-package))
1325 (progn
1326 (message "") ; kill the message from `locate-library'
87c668b4 1327 (require ediff-version-control-package))
0f0b0a86 1328 (or silent
3af0304a 1329 (error "Version control package %S.el not found. Use vc.el instead"
0f0b0a86 1330 ediff-version-control-package)))))
87c668b4 1331
b3a26225 1332
138df2ce
MK
1333;;;###autoload
1334(defun ediff-version ()
1335 "Return string describing the version of Ediff.
1336When called interactively, displays the version."
1337 (interactive)
1338 (if (interactive-p)
1339 (message (ediff-version))
1340 (format "Ediff %s of %s" ediff-version ediff-date)))
1341
813f532d 1342
4ae69eac 1343;;;###autoload
92c51e07
MK
1344(defun ediff-documentation (&optional node)
1345 "Display Ediff's manual.
1346With optional NODE, goes to that node."
4ae69eac
MK
1347 (interactive)
1348 (let ((ctl-window ediff-control-window)
1349 (ctl-buf ediff-control-buffer))
1350
1351 (ediff-skip-unsuitable-frames)
1352 (condition-case nil
1353 (progn
1354 (pop-to-buffer (get-buffer-create "*info*"))
1355 (info (if ediff-xemacs-p "ediff.info" "ediff"))
92c51e07
MK
1356 (if node
1357 (Info-goto-node node)
1358 (message "Type `i' to search for a specific topic"))
1359 (raise-frame (selected-frame)))
4ae69eac 1360 (error (beep 1)
bbe6126c 1361 (with-output-to-temp-buffer ediff-msg-buffer
92c51e07 1362 (princ ediff-BAD-INFO))
4ae69eac
MK
1363 (if (window-live-p ctl-window)
1364 (progn
1365 (select-window ctl-window)
1366 (set-window-buffer ctl-window ctl-buf)))))))
1367
1368
1369
1370
1371;;; Local Variables:
1372;;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
e756eb9f
MK
1373;;; eval: (put 'ediff-with-current-buffer 'lisp-indent-hook 1)
1374;;; eval: (put 'ediff-with-current-buffer 'edebug-form-spec '(form body))
4ae69eac
MK
1375;;; End:
1376
0f0b0a86 1377(require 'ediff-util)
813f532d 1378
3af0304a
MK
1379(run-hooks 'ediff-load-hook)
1380
813f532d 1381;;; ediff.el ends here