Document reserved keys
[emacs.git] / lisp / vc / ediff.el
blobcd2b2c4e62888c76982000960127a2710ae929ce
1 ;;; ediff.el --- a comprehensive visual interface to diff & patch
3 ;; Copyright (C) 1994-2018 Free Software Foundation, Inc.
5 ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
6 ;; Created: February 2, 1994
7 ;; Keywords: comparing, merging, patching, vc, tools, unix
8 ;; Version: 2.81.4
10 ;; Yoni Rabkin <yoni@rabkins.net> contacted the maintainer of this
11 ;; file on 20/3/2008, and the maintainer agreed that when a bug is
12 ;; filed in the Emacs bug reporting system against this file, a copy
13 ;; of the bug report be sent to the maintainer's email address.
15 (defconst ediff-version "2.81.5" "The current version of Ediff")
16 (defconst ediff-date "July 4, 2013" "Date of last update")
19 ;; This file is part of GNU Emacs.
21 ;; GNU Emacs is free software: you can redistribute it and/or modify
22 ;; it under the terms of the GNU General Public License as published by
23 ;; the Free Software Foundation, either version 3 of the License, or
24 ;; (at your option) any later version.
26 ;; GNU Emacs is distributed in the hope that it will be useful,
27 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
28 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 ;; GNU General Public License for more details.
31 ;; You should have received a copy of the GNU General Public License
32 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
34 ;;; Commentary:
36 ;; Never read that diff output again!
37 ;; Apply patch interactively!
38 ;; Merge with ease!
40 ;; This package provides a convenient way of simultaneous browsing through
41 ;; the differences between a pair (or a triple) of files or buffers. The
42 ;; files being compared, file-A, file-B, and file-C (if applicable) are
43 ;; shown in separate windows (side by side, one above the another, or in
44 ;; separate frames), and the differences are highlighted as you step
45 ;; through them. You can also copy difference regions from one buffer to
46 ;; another (and recover old differences if you change your mind).
48 ;; Ediff also supports merging operations on files and buffers, including
49 ;; merging using ancestor versions. Both comparison and merging operations can
50 ;; be performed on directories, i.e., by pairwise comparison of files in those
51 ;; directories.
53 ;; In addition, Ediff can apply a patch to a file and then let you step
54 ;; though both files, the patched and the original one, simultaneously,
55 ;; difference-by-difference. You can even apply a patch right out of a
56 ;; mail buffer, i.e., patches received by mail don't even have to be saved.
57 ;; Since Ediff lets you copy differences between buffers, you can, in
58 ;; effect, apply patches selectively (i.e., you can copy a difference
59 ;; region from file_orig to file, thereby undoing any particular patch that
60 ;; you don't like).
62 ;; Ediff is aware of version control, which lets the user compare
63 ;; files with their older versions. Ediff can also work with remote and
64 ;; compressed files. Details are given below.
66 ;; Finally, Ediff supports directory-level comparison, merging and patching.
67 ;; See the Ediff manual for details.
69 ;; This package builds upon the ideas borrowed from emerge.el and several
70 ;; Ediff's functions are adaptations from emerge.el. Much of the functionality
71 ;; Ediff provides is also influenced by emerge.el.
73 ;; The present version of Ediff supersedes Emerge. It provides a superior user
74 ;; interface and has numerous major features not found in Emerge. In
75 ;; particular, it can do patching, and 2-way and 3-way file comparison,
76 ;; merging, and directory operations.
80 ;;; Bugs:
82 ;; 1. The undo command doesn't restore deleted regions well. That is, if
83 ;; you delete all characters in a difference region and then invoke
84 ;; `undo', the reinstated text will most likely be inserted outside of
85 ;; what Ediff thinks is the current difference region. (This problem
86 ;; doesn't seem to exist with XEmacs.)
88 ;; If at any point you feel that difference regions are no longer correct,
89 ;; you can hit '!' to recompute the differences.
91 ;; 2. On a monochrome display, the repertoire of faces with which to
92 ;; highlight fine differences is limited. By default, Ediff is using
93 ;; underlining. However, if the region is already underlined by some other
94 ;; overlays, there is no simple way to temporarily remove that residual
95 ;; underlining. This problem occurs when a buffer is highlighted with
96 ;; hilit19.el or font-lock.el packages. If this residual highlighting gets
97 ;; in the way, you can do the following. Both font-lock.el and hilit19.el
98 ;; provide commands for unhighlighting buffers. You can either place these
99 ;; commands in `ediff-prepare-buffer-hook' (which will unhighlight every
100 ;; buffer used by Ediff) or you can execute them interactively, at any time
101 ;; and on any buffer.
104 ;;; Acknowledgments:
106 ;; Ediff was inspired by Dale R. Worley's <drw@math.mit.edu> emerge.el.
107 ;; Ediff would not have been possible without the help and encouragement of
108 ;; its many users. See Ediff on-line Info for the full list of those who
109 ;; helped. Improved defaults in Ediff file-name reading commands.
111 ;;; Code:
113 (provide 'ediff)
115 ;; Compiler pacifier
116 (eval-and-compile
117 (unless (fboundp 'declare-function) (defmacro declare-function (&rest _r))))
119 (require 'ediff-util)
120 ;; end pacifier
122 (require 'ediff-init)
123 (require 'ediff-mult) ; required because of the registry stuff
125 (defgroup ediff nil
126 "Comprehensive visual interface to `diff' and `patch'."
127 :tag "Ediff"
128 :group 'tools)
131 (defcustom ediff-use-last-dir nil
132 "If t, Ediff will use previous directory as default when reading file name."
133 :type 'boolean
134 :group 'ediff)
136 ;; Last directory used by an Ediff command for file-A.
137 (defvar ediff-last-dir-A nil)
138 ;; Last directory used by an Ediff command for file-B.
139 (defvar ediff-last-dir-B nil)
140 ;; Last directory used by an Ediff command for file-C.
141 (defvar ediff-last-dir-C nil)
142 ;; Last directory used by an Ediff command for the ancestor file.
143 (defvar ediff-last-dir-ancestor nil)
144 ;; Last directory used by an Ediff command as the output directory for merge.
145 (defvar ediff-last-merge-autostore-dir nil)
148 ;; Used as a startup hook to set `_orig' patch file read-only.
149 (defun ediff-set-read-only-in-buf-A ()
150 (ediff-with-current-buffer ediff-buffer-A
151 (setq buffer-read-only t)))
153 (declare-function dired-get-filename "dired"
154 (&optional localp no-error-if-not-filep))
155 (declare-function dired-get-marked-files "dired"
156 (&optional localp arg filter distinguish-one-marked))
158 ;; Return a plausible default for ediff's first file:
159 ;; In dired, return the file number FILENO (or 0) in the list
160 ;; (all-selected-files, filename under the cursor), where directories are
161 ;; ignored. Otherwise, return DEFAULT file name, if non-nil. Else,
162 ;; if the buffer is visiting a file, return that file name.
163 (defun ediff-get-default-file-name (&optional default fileno)
164 (cond ((eq major-mode 'dired-mode)
165 (let ((current (dired-get-filename nil 'no-error))
166 (marked (condition-case nil
167 (dired-get-marked-files 'no-dir)
168 (error nil)))
169 aux-list choices result)
170 (or (integerp fileno) (setq fileno 0))
171 (if (stringp default)
172 (setq aux-list (cons default aux-list)))
173 (if (and (stringp current) (not (file-directory-p current)))
174 (setq aux-list (cons current aux-list)))
175 (setq choices (nconc marked aux-list))
176 (setq result (elt choices fileno))
177 (or result
178 default)))
179 ((stringp default) default)
180 ((buffer-file-name (current-buffer))
181 (file-name-nondirectory (buffer-file-name (current-buffer))))
184 ;;; Compare files/buffers
186 ;;;###autoload
187 (defun ediff-files (file-A file-B &optional startup-hooks)
188 "Run Ediff on a pair of files, FILE-A and FILE-B.
189 STARTUP-HOOKS is a list of functions that Emacs calls without
190 arguments after setting up the Ediff buffers."
191 (interactive
192 (let ((dir-A (if ediff-use-last-dir
193 ediff-last-dir-A
194 default-directory))
195 dir-B f)
196 (list (setq f (ediff-read-file-name
197 "File A to compare"
198 dir-A
199 (ediff-get-default-file-name)
200 'no-dirs))
201 (ediff-read-file-name "File B to compare"
202 (setq dir-B
203 (if ediff-use-last-dir
204 ediff-last-dir-B
205 (file-name-directory f)))
206 (progn
207 (ediff-add-to-history
208 'file-name-history
209 (ediff-abbreviate-file-name
210 (expand-file-name
211 (file-name-nondirectory f)
212 dir-B)))
213 (ediff-get-default-file-name f 1)))
215 (ediff-files-internal file-A
216 (if (file-directory-p file-B)
217 (expand-file-name
218 (file-name-nondirectory file-A) file-B)
219 file-B)
220 nil ; file-C
221 startup-hooks
222 'ediff-files))
224 ;;;###autoload
225 (defun ediff-files3 (file-A file-B file-C &optional startup-hooks)
226 "Run Ediff on three files, FILE-A, FILE-B, and FILE-C.
227 STARTUP-HOOKS is a list of functions that Emacs calls without
228 arguments after setting up the Ediff buffers."
229 (interactive
230 (let ((dir-A (if ediff-use-last-dir
231 ediff-last-dir-A
232 default-directory))
233 dir-B dir-C f ff)
234 (list (setq f (ediff-read-file-name
235 "File A to compare"
236 dir-A
237 (ediff-get-default-file-name)
238 'no-dirs))
239 (setq ff (ediff-read-file-name "File B to compare"
240 (setq dir-B
241 (if ediff-use-last-dir
242 ediff-last-dir-B
243 (file-name-directory f)))
244 (progn
245 (ediff-add-to-history
246 'file-name-history
247 (ediff-abbreviate-file-name
248 (expand-file-name
249 (file-name-nondirectory f)
250 dir-B)))
251 (ediff-get-default-file-name f 1))))
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 (ediff-add-to-history
258 'file-name-history
259 (ediff-abbreviate-file-name
260 (expand-file-name
261 (file-name-nondirectory ff)
262 dir-C)))
263 (ediff-get-default-file-name ff 2)))
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)
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))
277 ;;;###autoload
278 (defalias 'ediff3 'ediff-files3)
281 (defun ediff-find-file (file-var buffer-name &optional last-dir hooks-var)
282 "Visit FILE and arrange its buffer to Ediff's liking.
283 FILE-VAR is actually a variable symbol whose value must contain a true
284 file name.
285 BUFFER-NAME is a variable symbol, which will get the buffer object into
286 which FILE is read.
287 LAST-DIR is the directory variable symbol where FILE's
288 directory name should be returned. HOOKS-VAR is a variable symbol that will
289 be assigned the hook to be executed after `ediff-startup' is finished.
290 `ediff-find-file' arranges that the temp files it might create will be
291 deleted."
292 (let* ((file (symbol-value file-var))
293 (file-magic (ediff-filename-magic-p file))
294 (temp-file-name-prefix (file-name-nondirectory file)))
295 (cond ((not (file-readable-p file))
296 (user-error "File `%s' does not exist or is not readable" file))
297 ((file-directory-p file)
298 (user-error "File `%s' is a directory" file)))
300 ;; some of the commands, below, require full file name
301 (setq file (expand-file-name file))
303 ;; Record the directory of the file
304 (if last-dir
305 (set last-dir (expand-file-name (file-name-directory file))))
307 ;; Setup the buffer
308 (set buffer-name (find-file-noselect file))
310 (ediff-with-current-buffer (symbol-value buffer-name)
311 (widen) ; Make sure the entire file is seen
312 (cond (file-magic ; file has a handler, such as jka-compr-handler or
313 ;;; ange-ftp-hook-function--arrange for temp file
314 (ediff-verify-file-buffer 'magic)
315 (setq file
316 (ediff-make-temp-file
317 (current-buffer) temp-file-name-prefix))
318 (set hooks-var (cons `(lambda () (delete-file ,file))
319 (symbol-value hooks-var))))
320 ;; file processed via auto-mode-alist, a la uncompress.el
321 ((not (equal (file-truename file)
322 (file-truename (buffer-file-name))))
323 (setq file
324 (ediff-make-temp-file
325 (current-buffer) temp-file-name-prefix))
326 (set hooks-var (cons `(lambda () (delete-file ,file))
327 (symbol-value hooks-var))))
328 (t ;; plain file---just check that the file matches the buffer
329 (ediff-verify-file-buffer))))
330 (set file-var file)))
332 ;; MERGE-BUFFER-FILE is the file to be associated with the merge buffer
333 (defun ediff-files-internal (file-A file-B file-C startup-hooks job-name
334 &optional merge-buffer-file)
335 (let (buf-A buf-B buf-C)
336 (if (string= file-A file-B)
337 (error "Files A and B are the same"))
338 (if (stringp file-C)
339 (or (and (string= file-A file-C) (error "Files A and C are the same"))
340 (and (string= file-B file-C) (error "Files B and C are the same"))))
341 (message "Reading file %s ... " file-A)
342 ;;(sit-for 0)
343 (ediff-find-file 'file-A 'buf-A 'ediff-last-dir-A 'startup-hooks)
344 (message "Reading file %s ... " file-B)
345 ;;(sit-for 0)
346 (ediff-find-file 'file-B 'buf-B 'ediff-last-dir-B 'startup-hooks)
347 (if (stringp file-C)
348 (progn
349 (message "Reading file %s ... " file-C)
350 ;;(sit-for 0)
351 (ediff-find-file
352 'file-C 'buf-C
353 (if (eq job-name 'ediff-merge-files-with-ancestor)
354 'ediff-last-dir-ancestor 'ediff-last-dir-C)
355 'startup-hooks)))
356 (ediff-setup buf-A file-A
357 buf-B file-B
358 buf-C file-C
359 startup-hooks
360 (list (cons 'ediff-job-name job-name))
361 merge-buffer-file)))
363 (declare-function diff-latest-backup-file "diff" (fn))
365 ;;;###autoload
366 (defalias 'ediff 'ediff-files)
368 ;;;###autoload
369 (defun ediff-current-file ()
370 "Start ediff between current buffer and its file on disk.
371 This command can be used instead of `revert-buffer'. If there is
372 nothing to revert then this command fails."
373 (interactive)
374 ;; This duplicates code from menu-bar.el.
375 (unless (or (not (eq revert-buffer-function 'revert-buffer--default))
376 (not (eq revert-buffer-insert-file-contents-function
377 'revert-buffer-insert-file-contents--default-function))
378 (and buffer-file-number
379 (or (buffer-modified-p)
380 (not (verify-visited-file-modtime
381 (current-buffer))))))
382 (error "Nothing to revert"))
383 (let* ((auto-save-p (and (recent-auto-save-p)
384 buffer-auto-save-file-name
385 (file-readable-p buffer-auto-save-file-name)
386 (y-or-n-p
387 "Buffer has been auto-saved recently. Compare with auto-save file? ")))
388 (file-name (if auto-save-p
389 buffer-auto-save-file-name
390 buffer-file-name))
391 (revert-buf-name (concat "FILE=" file-name))
392 (revert-buf (get-buffer revert-buf-name))
393 (current-major major-mode))
394 (unless file-name
395 (error "Buffer does not seem to be associated with any file"))
396 (when revert-buf
397 (kill-buffer revert-buf)
398 (setq revert-buf nil))
399 (setq revert-buf (get-buffer-create revert-buf-name))
400 (with-current-buffer revert-buf
401 (insert-file-contents file-name)
402 ;; Assume same modes:
403 (funcall current-major))
404 (ediff-buffers revert-buf (current-buffer))))
407 ;;;###autoload
408 (defun ediff-backup (file)
409 "Run Ediff on FILE and its backup file.
410 Uses the latest backup, if there are several numerical backups.
411 If this file is a backup, `ediff' it with its original."
412 (interactive (list (read-file-name "Ediff (file with backup): ")))
413 ;; The code is taken from `diff-backup'.
414 (require 'diff)
415 (let (bak ori)
416 (if (backup-file-name-p file)
417 (setq bak file
418 ori (file-name-sans-versions file))
419 (setq bak (or (diff-latest-backup-file file)
420 (error "No backup found for %s" file))
421 ori file))
422 (ediff-files bak ori)))
424 ;;;###autoload
425 (defun ediff-buffers (buffer-A buffer-B &optional startup-hooks job-name)
426 "Run Ediff on a pair of buffers, BUFFER-A and BUFFER-B.
427 STARTUP-HOOKS is a list of functions that Emacs calls without
428 arguments after setting up the Ediff buffers. JOB-NAME is a
429 symbol describing the Ediff job type; it defaults to
430 `ediff-buffers', but can also be one of
431 `ediff-merge-files-with-ancestor', `ediff-last-dir-ancestor',
432 `ediff-last-dir-C', `ediff-buffers3', `ediff-merge-buffers', or
433 `ediff-merge-buffers-with-ancestor'."
434 (interactive
435 (let (bf)
436 (list (setq bf (read-buffer "Buffer A to compare: "
437 (ediff-other-buffer "") t))
438 (read-buffer "Buffer B to compare: "
439 (progn
440 ;; realign buffers so that two visible bufs will be
441 ;; at the top
442 (save-window-excursion (other-window 1))
443 (ediff-other-buffer bf))
444 t))))
445 (or job-name (setq job-name 'ediff-buffers))
446 (ediff-buffers-internal buffer-A buffer-B nil startup-hooks job-name))
448 ;;;###autoload
449 (defalias 'ebuffers 'ediff-buffers)
452 ;;;###autoload
453 (defun ediff-buffers3 (buffer-A buffer-B buffer-C
454 &optional startup-hooks job-name)
455 "Run Ediff on three buffers, BUFFER-A, BUFFER-B, and BUFFER-C.
456 STARTUP-HOOKS is a list of functions that Emacs calls without
457 arguments after setting up the Ediff buffers. JOB-NAME is a
458 symbol describing the Ediff job type; it defaults to
459 `ediff-buffers3', but can also be one of
460 `ediff-merge-files-with-ancestor', `ediff-last-dir-ancestor',
461 `ediff-last-dir-C', `ediff-buffers', `ediff-merge-buffers', or
462 `ediff-merge-buffers-with-ancestor'."
463 (interactive
464 (let (bf bff)
465 (list (setq bf (read-buffer "Buffer A to compare: "
466 (ediff-other-buffer "") t))
467 (setq bff (read-buffer "Buffer B to compare: "
468 (progn
469 ;; realign buffers so that two visible
470 ;; bufs will be at the top
471 (save-window-excursion (other-window 1))
472 (ediff-other-buffer bf))
474 (read-buffer "Buffer C to compare: "
475 (progn
476 ;; realign buffers so that three visible
477 ;; bufs will be at the top
478 (save-window-excursion (other-window 1))
479 (ediff-other-buffer (list bf bff)))
482 (or job-name (setq job-name 'ediff-buffers3))
483 (ediff-buffers-internal buffer-A buffer-B buffer-C startup-hooks job-name))
485 ;;;###autoload
486 (defalias 'ebuffers3 'ediff-buffers3)
490 ;; MERGE-BUFFER-FILE is the file to be associated with the merge buffer
491 (defun ediff-buffers-internal (buf-A buf-B buf-C startup-hooks job-name
492 &optional merge-buffer-file)
493 (let* ((buf-A-file-name (buffer-file-name (get-buffer buf-A)))
494 (buf-B-file-name (buffer-file-name (get-buffer buf-B)))
495 (buf-C-is-alive (ediff-buffer-live-p buf-C))
496 (buf-C-file-name (if buf-C-is-alive
497 (buffer-file-name (get-buffer buf-B))))
498 file-A file-B file-C)
499 (unwind-protect
500 (progn
501 (if (not (ediff-buffer-live-p buf-A))
502 (user-error "Buffer %S doesn't exist" buf-A))
503 (if (not (ediff-buffer-live-p buf-B))
504 (user-error "Buffer %S doesn't exist" buf-B))
505 (let ((ediff-job-name job-name))
506 (if (and ediff-3way-comparison-job
507 (not buf-C-is-alive))
508 (user-error "Buffer %S doesn't exist" buf-C)))
509 (if (stringp buf-A-file-name)
510 (setq buf-A-file-name (file-name-nondirectory buf-A-file-name)))
511 (if (stringp buf-B-file-name)
512 (setq buf-B-file-name (file-name-nondirectory buf-B-file-name)))
513 (if (stringp buf-C-file-name)
514 (setq buf-C-file-name (file-name-nondirectory buf-C-file-name)))
516 (setq file-A (ediff-make-temp-file buf-A buf-A-file-name)
517 file-B (ediff-make-temp-file buf-B buf-B-file-name))
518 (if buf-C-is-alive
519 (setq file-C (ediff-make-temp-file buf-C buf-C-file-name)))
521 (ediff-setup (get-buffer buf-A) file-A
522 (get-buffer buf-B) file-B
523 (if buf-C-is-alive (get-buffer buf-C))
524 file-C
525 (cons `(lambda ()
526 (delete-file ,file-A)
527 (delete-file ,file-B)
528 (if (stringp ,file-C) (delete-file ,file-C)))
529 startup-hooks)
530 (list (cons 'ediff-job-name job-name))
531 merge-buffer-file))
532 (if (and (stringp file-A) (file-exists-p file-A))
533 (delete-file file-A))
534 (if (and (stringp file-B) (file-exists-p file-B))
535 (delete-file file-B))
536 (if (and (stringp file-C) (file-exists-p file-C))
537 (delete-file file-C)))))
540 ;;; Directory and file group operations
542 ;; Get appropriate default name for directory:
543 ;; If ediff-use-last-dir, use ediff-last-dir-A.
544 ;; In dired mode, use the directory that is under the point (if any);
545 ;; otherwise, use default-directory
546 (defun ediff-get-default-directory-name ()
547 (cond (ediff-use-last-dir ediff-last-dir-A)
548 ((eq major-mode 'dired-mode)
549 (let ((f (dired-get-filename nil 'noerror)))
550 (if (and (stringp f) (file-directory-p f))
552 default-directory)))
553 (t default-directory)))
556 ;;;###autoload
557 (defun ediff-directories (dir1 dir2 regexp)
558 "Run Ediff on a pair of directories, DIR1 and DIR2, comparing files that have
559 the same name in both. The third argument, REGEXP, is nil or a regular
560 expression; only file names that match the regexp are considered."
561 (interactive
562 (let ((dir-A (ediff-get-default-directory-name))
563 (default-regexp (eval ediff-default-filtering-regexp))
565 (list (setq f (read-directory-name
566 "Directory A to compare: " dir-A nil 'must-match))
567 (read-directory-name "Directory B to compare: "
568 (if ediff-use-last-dir
569 ediff-last-dir-B
570 (ediff-strip-last-dir f))
571 nil 'must-match)
572 (read-string
573 (if (stringp default-regexp)
574 (format "Filter filenames through regular expression (default %s): "
575 default-regexp)
576 "Filter filenames through regular expression: ")
578 'ediff-filtering-regexp-history
579 (eval ediff-default-filtering-regexp))
581 (ediff-directories-internal
582 dir1 dir2 nil regexp 'ediff-files 'ediff-directories
585 ;;;###autoload
586 (defalias 'edirs 'ediff-directories)
589 ;;;###autoload
590 (defun ediff-directory-revisions (dir1 regexp)
591 "Run Ediff on a directory, DIR1, comparing its files with their revisions.
592 The second argument, REGEXP, is a regular expression that filters the file
593 names. Only the files that are under revision control are taken into account."
594 (interactive
595 (let ((dir-A (ediff-get-default-directory-name))
596 (default-regexp (eval ediff-default-filtering-regexp))
598 (list (read-directory-name
599 "Directory to compare with revision:" dir-A nil 'must-match)
600 (read-string
601 (if (stringp default-regexp)
602 (format "Filter filenames through regular expression (default %s): "
603 default-regexp)
604 "Filter filenames through regular expression: ")
606 'ediff-filtering-regexp-history
607 (eval ediff-default-filtering-regexp))
609 (ediff-directory-revisions-internal
610 dir1 regexp 'ediff-revision 'ediff-directory-revisions
613 ;;;###autoload
614 (defalias 'edir-revisions 'ediff-directory-revisions)
617 ;;;###autoload
618 (defun ediff-directories3 (dir1 dir2 dir3 regexp)
619 "Run Ediff on three directories, DIR1, DIR2, and DIR3, comparing files that
620 have the same name in all three. The last argument, REGEXP, is nil or a
621 regular expression; only file names that match the regexp are considered."
623 (interactive
624 (let ((dir-A (ediff-get-default-directory-name))
625 (default-regexp (eval ediff-default-filtering-regexp))
627 (list (setq f (read-directory-name "Directory A to compare:" dir-A nil))
628 (setq f (read-directory-name "Directory B to compare:"
629 (if ediff-use-last-dir
630 ediff-last-dir-B
631 (ediff-strip-last-dir f))
632 nil 'must-match))
633 (read-directory-name "Directory C to compare:"
634 (if ediff-use-last-dir
635 ediff-last-dir-C
636 (ediff-strip-last-dir f))
637 nil 'must-match)
638 (read-string
639 (if (stringp default-regexp)
640 (format "Filter filenames through regular expression (default %s): "
641 default-regexp)
642 "Filter filenames through regular expression: ")
644 'ediff-filtering-regexp-history
645 (eval ediff-default-filtering-regexp))
647 (ediff-directories-internal
648 dir1 dir2 dir3 regexp 'ediff-files3 'ediff-directories3
651 ;;;###autoload
652 (defalias 'edirs3 'ediff-directories3)
654 ;;;###autoload
655 (defun ediff-merge-directories (dir1 dir2 regexp &optional merge-autostore-dir)
656 "Run Ediff on a pair of directories, DIR1 and DIR2, merging files that have
657 the same name in both. The third argument, REGEXP, is nil or a regular
658 expression; only file names that match the regexp are considered.
659 MERGE-AUTOSTORE-DIR is the directory in which to store merged files."
660 (interactive
661 (let ((dir-A (ediff-get-default-directory-name))
662 (default-regexp (eval ediff-default-filtering-regexp))
664 (list (setq f (read-directory-name "Directory A to merge:"
665 dir-A nil 'must-match))
666 (read-directory-name "Directory B to merge:"
667 (if ediff-use-last-dir
668 ediff-last-dir-B
669 (ediff-strip-last-dir f))
670 nil 'must-match)
671 (read-string
672 (if (stringp default-regexp)
673 (format "Filter filenames through regular expression (default %s): "
674 default-regexp)
675 "Filter filenames through regular expression: ")
677 'ediff-filtering-regexp-history
678 (eval ediff-default-filtering-regexp))
680 (ediff-directories-internal
681 dir1 dir2 nil regexp 'ediff-merge-files 'ediff-merge-directories
682 nil merge-autostore-dir
685 ;;;###autoload
686 (defalias 'edirs-merge 'ediff-merge-directories)
688 ;;;###autoload
689 (defun ediff-merge-directories-with-ancestor (dir1 dir2 ancestor-dir regexp
690 &optional
691 merge-autostore-dir)
692 "Merge files in directories DIR1 and DIR2 using files in ANCESTOR-DIR as ancestors.
693 Ediff merges files that have identical names in DIR1, DIR2. If a pair of files
694 in DIR1 and DIR2 doesn't have an ancestor in ANCESTOR-DIR, Ediff will merge
695 without ancestor. The fourth argument, REGEXP, is nil or a regular expression;
696 only file names that match the regexp are considered.
697 MERGE-AUTOSTORE-DIR is the directory in which to store merged files."
698 (interactive
699 (let ((dir-A (ediff-get-default-directory-name))
700 (default-regexp (eval ediff-default-filtering-regexp))
702 (list (setq f (read-directory-name "Directory A to merge:" dir-A nil))
703 (setq f (read-directory-name "Directory B to merge:"
704 (if ediff-use-last-dir
705 ediff-last-dir-B
706 (ediff-strip-last-dir f))
707 nil 'must-match))
708 (read-directory-name "Ancestor directory:"
709 (if ediff-use-last-dir
710 ediff-last-dir-C
711 (ediff-strip-last-dir f))
712 nil 'must-match)
713 (read-string
714 (if (stringp default-regexp)
715 (format "Filter filenames through regular expression (default %s): "
716 default-regexp)
717 "Filter filenames through regular expression: ")
719 'ediff-filtering-regexp-history
720 (eval ediff-default-filtering-regexp))
722 (ediff-directories-internal
723 dir1 dir2 ancestor-dir regexp
724 'ediff-merge-files-with-ancestor 'ediff-merge-directories-with-ancestor
725 nil merge-autostore-dir
728 ;;;###autoload
729 (defun ediff-merge-directory-revisions (dir1 regexp
730 &optional merge-autostore-dir)
731 "Run Ediff on a directory, DIR1, merging its files with their revisions.
732 The second argument, REGEXP, is a regular expression that filters the file
733 names. Only the files that are under revision control are taken into account.
734 MERGE-AUTOSTORE-DIR is the directory in which to store merged files."
735 (interactive
736 (let ((dir-A (ediff-get-default-directory-name))
737 (default-regexp (eval ediff-default-filtering-regexp))
739 (list (read-directory-name
740 "Directory to merge with revisions:" dir-A nil 'must-match)
741 (read-string
742 (if (stringp default-regexp)
743 (format "Filter filenames through regular expression (default %s): "
744 default-regexp)
745 "Filter filenames through regular expression: ")
747 'ediff-filtering-regexp-history
748 (eval ediff-default-filtering-regexp))
750 (ediff-directory-revisions-internal
751 dir1 regexp 'ediff-merge-revisions 'ediff-merge-directory-revisions
752 nil merge-autostore-dir
755 ;;;###autoload
756 (defalias 'edir-merge-revisions 'ediff-merge-directory-revisions)
758 ;;;###autoload
759 (defun ediff-merge-directory-revisions-with-ancestor (dir1 regexp
760 &optional
761 merge-autostore-dir)
762 "Run Ediff on a directory, DIR1, merging its files with their revisions and ancestors.
763 The second argument, REGEXP, is a regular expression that filters the file
764 names. Only the files that are under revision control are taken into account.
765 MERGE-AUTOSTORE-DIR is the directory in which to store merged files."
766 (interactive
767 (let ((dir-A (ediff-get-default-directory-name))
768 (default-regexp (eval ediff-default-filtering-regexp))
770 (list (read-directory-name
771 "Directory to merge with revisions and ancestors:"
772 dir-A nil 'must-match)
773 (read-string
774 (if (stringp default-regexp)
775 (format "Filter filenames through regular expression (default %s): "
776 default-regexp)
777 "Filter filenames through regular expression: ")
779 'ediff-filtering-regexp-history
780 (eval ediff-default-filtering-regexp))
782 (ediff-directory-revisions-internal
783 dir1 regexp 'ediff-merge-revisions-with-ancestor
784 'ediff-merge-directory-revisions-with-ancestor
785 nil merge-autostore-dir
788 ;;;###autoload
789 (defalias
790 'edir-merge-revisions-with-ancestor
791 'ediff-merge-directory-revisions-with-ancestor)
793 ;;;###autoload
794 (defalias 'edirs-merge-with-ancestor 'ediff-merge-directories-with-ancestor)
796 ;; Run ediff-action (ediff-files, ediff-merge, ediff-merge-with-ancestors)
797 ;; on a pair of directories (three directories, in case of ancestor).
798 ;; The third argument, REGEXP, is nil or a regular expression;
799 ;; only file names that match the regexp are considered.
800 ;; JOBNAME is the symbol indicating the meta-job to be performed.
801 ;; MERGE-AUTOSTORE-DIR is the directory in which to store merged files.
802 (defun ediff-directories-internal (dir1 dir2 dir3 regexp action jobname
803 &optional startup-hooks
804 merge-autostore-dir)
805 (if (stringp dir3)
806 (setq dir3 (if (file-directory-p dir3) dir3 (file-name-directory dir3))))
808 (cond ((string= dir1 dir2)
809 (user-error "Directories A and B are the same: %s" dir1))
810 ((and (eq jobname 'ediff-directories3)
811 (string= dir1 dir3))
812 (user-error "Directories A and C are the same: %s" dir1))
813 ((and (eq jobname 'ediff-directories3)
814 (string= dir2 dir3))
815 (user-error "Directories B and C are the same: %s" dir1)))
817 (if merge-autostore-dir
818 (or (stringp merge-autostore-dir)
819 (error "%s: Directory for storing merged files must be a string"
820 jobname)))
821 (let (;; dir-diff-struct is of the form (common-list diff-list)
822 ;; It is a structure where ediff-intersect-directories returns
823 ;; commonalities and differences among directories
824 dir-diff-struct
825 meta-buf)
826 (if (and ediff-autostore-merges
827 (ediff-merge-metajob jobname)
828 (not merge-autostore-dir))
829 (setq merge-autostore-dir
830 (read-directory-name "Save merged files in directory: "
831 (if ediff-use-last-dir
832 ediff-last-merge-autostore-dir
833 (ediff-strip-last-dir dir1))
835 'must-match)))
836 ;; verify we are not merging into an orig directory
837 (if merge-autostore-dir
838 (cond ((and (stringp dir1) (string= merge-autostore-dir dir1))
839 (or (y-or-n-p
840 "Directory for saving merged files = Directory A. Sure? ")
841 (user-error "Directory merge aborted")))
842 ((and (stringp dir2) (string= merge-autostore-dir dir2))
843 (or (y-or-n-p
844 "Directory for saving merged files = Directory B. Sure? ")
845 (user-error "Directory merge aborted")))
846 ((and (stringp dir3) (string= merge-autostore-dir dir3))
847 (or (y-or-n-p
848 "Directory for saving merged files = Ancestor Directory. Sure? ")
849 (user-error "Directory merge aborted")))))
851 (setq dir-diff-struct (ediff-intersect-directories
852 jobname
853 regexp dir1 dir2 dir3 merge-autostore-dir))
854 (setq startup-hooks
855 ;; this sets various vars in the meta buffer inside
856 ;; ediff-prepare-meta-buffer
857 (cons `(lambda ()
858 ;; tell what to do if the user clicks on a session record
859 (setq ediff-session-action-function (quote ,action))
860 ;; set ediff-dir-difference-list
861 (setq ediff-dir-difference-list
862 (cdr (quote ,dir-diff-struct))))
863 startup-hooks))
864 (setq meta-buf (ediff-prepare-meta-buffer
865 'ediff-filegroup-action
866 (car dir-diff-struct)
867 "*Ediff Session Group Panel"
868 'ediff-redraw-directory-group-buffer
869 jobname
870 startup-hooks))
871 (ediff-show-meta-buffer meta-buf)
874 ;; MERGE-AUTOSTORE-DIR can be given to tell ediff where to store the merged
875 ;; files
876 (defun ediff-directory-revisions-internal (dir1 regexp action jobname
877 &optional startup-hooks
878 merge-autostore-dir)
879 (setq dir1 (if (file-directory-p dir1) dir1 (file-name-directory dir1)))
881 (if merge-autostore-dir
882 (or (stringp merge-autostore-dir)
883 (error "%S: Directory for storing merged files must be a string"
884 jobname)))
885 (let (file-list meta-buf)
886 (if (and ediff-autostore-merges
887 (ediff-merge-metajob jobname)
888 (not merge-autostore-dir))
889 (setq merge-autostore-dir
890 (read-directory-name "Save merged files in directory: "
891 (if ediff-use-last-dir
892 ediff-last-merge-autostore-dir
893 (ediff-strip-last-dir dir1))
895 'must-match)))
896 ;; verify merge-autostore-dir != dir1
897 (if (and merge-autostore-dir
898 (stringp dir1)
899 (string= merge-autostore-dir dir1))
900 (or (y-or-n-p
901 "Directory for saving merged file = directory A. Sure? ")
902 (user-error "Merge of directory revisions aborted")))
904 (setq file-list
905 (ediff-get-directory-files-under-revision
906 jobname regexp dir1 merge-autostore-dir))
907 (setq startup-hooks
908 ;; this sets various vars in the meta buffer inside
909 ;; ediff-prepare-meta-buffer
910 (cons `(lambda ()
911 ;; tell what to do if the user clicks on a session record
912 (setq ediff-session-action-function (quote ,action)))
913 startup-hooks))
914 (setq meta-buf (ediff-prepare-meta-buffer
915 'ediff-filegroup-action
916 file-list
917 "*Ediff Session Group Panel"
918 'ediff-redraw-directory-group-buffer
919 jobname
920 startup-hooks))
921 (ediff-show-meta-buffer meta-buf)
925 ;;; Compare regions and windows
927 ;;;###autoload
928 (defun ediff-windows-wordwise (dumb-mode &optional wind-A wind-B startup-hooks)
929 "Compare WIND-A and WIND-B, which are selected by clicking, wordwise.
930 With prefix argument, DUMB-MODE, or on a non-windowing display, works as
931 follows:
932 If WIND-A is nil, use selected window.
933 If WIND-B is nil, use window next to WIND-A.
934 STARTUP-HOOKS is a list of functions that Emacs calls without
935 arguments after setting up the Ediff buffers."
936 (interactive "P")
937 (ediff-windows dumb-mode wind-A wind-B
938 startup-hooks 'ediff-windows-wordwise 'word-mode))
940 ;;;###autoload
941 (defun ediff-windows-linewise (dumb-mode &optional wind-A wind-B startup-hooks)
942 "Compare WIND-A and WIND-B, which are selected by clicking, linewise.
943 With prefix argument, DUMB-MODE, or on a non-windowing display, works as
944 follows:
945 If WIND-A is nil, use selected window.
946 If WIND-B is nil, use window next to WIND-A.
947 STARTUP-HOOKS is a list of functions that Emacs calls without
948 arguments after setting up the Ediff buffers."
949 (interactive "P")
950 (ediff-windows dumb-mode wind-A wind-B
951 startup-hooks 'ediff-windows-linewise nil))
953 ;; Compare WIND-A and WIND-B, which are selected by clicking.
954 ;; With prefix argument, DUMB-MODE, or on a non-windowing display,
955 ;; works as follows:
956 ;; If WIND-A is nil, use selected window.
957 ;; If WIND-B is nil, use window next to WIND-A.
958 (defun ediff-windows (dumb-mode wind-A wind-B startup-hooks job-name word-mode)
959 (if (or dumb-mode (not (ediff-window-display-p)))
960 (setq wind-A (ediff-get-next-window wind-A nil)
961 wind-B (ediff-get-next-window wind-B wind-A))
962 (setq wind-A (ediff-get-window-by-clicking wind-A nil 1)
963 wind-B (ediff-get-window-by-clicking wind-B wind-A 2)))
965 (let ((buffer-A (window-buffer wind-A))
966 (buffer-B (window-buffer wind-B))
967 beg-A end-A beg-B end-B)
969 (save-excursion
970 (save-window-excursion
971 (sit-for 0) ; sync before using window-start/end -- a precaution
972 (select-window wind-A)
973 (setq beg-A (window-start)
974 end-A (window-end))
975 (select-window wind-B)
976 (setq beg-B (window-start)
977 end-B (window-end))))
978 (setq buffer-A
979 (ediff-clone-buffer-for-window-comparison
980 buffer-A wind-A "-Window.A-")
981 buffer-B
982 (ediff-clone-buffer-for-window-comparison
983 buffer-B wind-B "-Window.B-"))
984 (ediff-regions-internal
985 buffer-A beg-A end-A buffer-B beg-B end-B
986 startup-hooks job-name word-mode nil)))
989 ;;;###autoload
990 (defun ediff-regions-wordwise (buffer-A buffer-B &optional startup-hooks)
991 "Run Ediff on a pair of regions in specified buffers.
992 BUFFER-A and BUFFER-B are the buffers to be compared.
993 Regions (i.e., point and mark) can be set in advance or marked interactively.
994 This function is effective only for relatively small regions, up to 200
995 lines. For large regions, use `ediff-regions-linewise'.
996 STARTUP-HOOKS is a list of functions that Emacs calls without
997 arguments after setting up the Ediff buffers."
998 (interactive
999 (let (bf)
1000 (list (setq bf (read-buffer "Region's A buffer: "
1001 (ediff-other-buffer "") t))
1002 (read-buffer "Region's B buffer: "
1003 (progn
1004 ;; realign buffers so that two visible bufs will be
1005 ;; at the top
1006 (save-window-excursion (other-window 1))
1007 (ediff-other-buffer bf))
1008 t))))
1009 (if (not (ediff-buffer-live-p buffer-A))
1010 (user-error "Buffer %S doesn't exist" buffer-A))
1011 (if (not (ediff-buffer-live-p buffer-B))
1012 (user-error "Buffer %S doesn't exist" buffer-B))
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 (set-buffer buffer-B)
1024 (setq reg-B-beg (region-beginning)
1025 reg-B-end (region-end)))
1027 (ediff-regions-internal
1028 (get-buffer buffer-A) reg-A-beg reg-A-end
1029 (get-buffer buffer-B) reg-B-beg reg-B-end
1030 startup-hooks 'ediff-regions-wordwise 'word-mode nil)))
1032 ;;;###autoload
1033 (defun ediff-regions-linewise (buffer-A buffer-B &optional startup-hooks)
1034 "Run Ediff on a pair of regions in specified buffers.
1035 BUFFER-A and BUFFER-B are the buffers to be compared.
1036 Regions (i.e., point and mark) can be set in advance or marked interactively.
1037 Each region is enlarged to contain full lines.
1038 This function is effective for large regions, over 100-200
1039 lines. For small regions, use `ediff-regions-wordwise'.
1040 STARTUP-HOOKS is a list of functions that Emacs calls without
1041 arguments after setting up the Ediff buffers."
1042 (interactive
1043 (let (bf)
1044 (list (setq bf (read-buffer "Region A's buffer: "
1045 (ediff-other-buffer "") t))
1046 (read-buffer "Region B's buffer: "
1047 (progn
1048 ;; realign buffers so that two visible bufs will be
1049 ;; at the top
1050 (save-window-excursion (other-window 1))
1051 (ediff-other-buffer bf))
1052 t))))
1053 (if (not (ediff-buffer-live-p buffer-A))
1054 (user-error "Buffer %S doesn't exist" buffer-A))
1055 (if (not (ediff-buffer-live-p buffer-B))
1056 (user-error "Buffer %S doesn't exist" buffer-B))
1058 (let ((buffer-A
1059 (ediff-clone-buffer-for-region-comparison buffer-A "-Region.A-"))
1060 (buffer-B
1061 (ediff-clone-buffer-for-region-comparison buffer-B "-Region.B-"))
1062 reg-A-beg reg-A-end reg-B-beg reg-B-end)
1063 (with-current-buffer buffer-A
1064 (setq reg-A-beg (region-beginning)
1065 reg-A-end (region-end))
1066 ;; enlarge the region to hold full lines
1067 (goto-char reg-A-beg)
1068 (beginning-of-line)
1069 (setq reg-A-beg (point))
1070 (goto-char reg-A-end)
1071 (end-of-line)
1072 (or (eobp) (forward-char)) ; include the newline char
1073 (setq reg-A-end (point))
1075 (set-buffer buffer-B)
1076 (setq reg-B-beg (region-beginning)
1077 reg-B-end (region-end))
1078 ;; enlarge the region to hold full lines
1079 (goto-char reg-B-beg)
1080 (beginning-of-line)
1081 (setq reg-B-beg (point))
1082 (goto-char reg-B-end)
1083 (end-of-line)
1084 (or (eobp) (forward-char)) ; include the newline char
1085 (setq reg-B-end (point))
1086 ) ; save excursion
1088 (ediff-regions-internal
1089 (get-buffer buffer-A) reg-A-beg reg-A-end
1090 (get-buffer buffer-B) reg-B-beg reg-B-end
1091 startup-hooks 'ediff-regions-linewise nil nil))) ; no word mode
1093 ;; compare region beg-A to end-A of buffer-A
1094 ;; to regions beg-B -- end-B in buffer-B.
1095 (defun ediff-regions-internal (buffer-A beg-A end-A buffer-B beg-B end-B
1096 startup-hooks job-name word-mode
1097 setup-parameters)
1098 (let ((tmp-buffer (get-buffer-create ediff-tmp-buffer))
1099 overl-A overl-B
1100 file-A file-B)
1101 (unwind-protect
1102 (progn
1103 ;; in case beg/end-A/B aren't markers--make them into markers
1104 (ediff-with-current-buffer buffer-A
1105 (setq beg-A (move-marker (make-marker) beg-A)
1106 end-A (move-marker (make-marker) end-A)))
1107 (ediff-with-current-buffer buffer-B
1108 (setq beg-B (move-marker (make-marker) beg-B)
1109 end-B (move-marker (make-marker) end-B)))
1111 ;; make file-A
1112 (if word-mode
1113 (ediff-wordify beg-A end-A buffer-A tmp-buffer)
1114 (ediff-copy-to-buffer beg-A end-A buffer-A tmp-buffer))
1115 (setq file-A (ediff-make-temp-file tmp-buffer "regA"))
1117 ;; make file-B
1118 (if word-mode
1119 (ediff-wordify beg-B end-B buffer-B tmp-buffer)
1120 (ediff-copy-to-buffer beg-B end-B buffer-B tmp-buffer))
1121 (setq file-B (ediff-make-temp-file tmp-buffer "regB"))
1123 (setq overl-A (ediff-make-bullet-proof-overlay beg-A end-A buffer-A))
1124 (setq overl-B (ediff-make-bullet-proof-overlay beg-B end-B buffer-B))
1125 (ediff-setup buffer-A file-A
1126 buffer-B file-B
1127 nil nil ; buffer & file C
1128 (cons `(lambda ()
1129 (delete-file ,file-A)
1130 (delete-file ,file-B))
1131 startup-hooks)
1132 (append
1133 (list (cons 'ediff-word-mode word-mode)
1134 (cons 'ediff-narrow-bounds (list overl-A overl-B))
1135 (cons 'ediff-job-name job-name))
1136 setup-parameters)))
1137 (if (and (stringp file-A) (file-exists-p file-A))
1138 (delete-file file-A))
1139 (if (and (stringp file-B) (file-exists-p file-B))
1140 (delete-file file-B)))
1144 ;;; Merge files and buffers
1146 ;;;###autoload
1147 (defalias 'ediff-merge 'ediff-merge-files)
1149 (defsubst ediff-merge-on-startup ()
1150 (ediff-do-merge 0)
1151 ;; Can't remember why this is here, but it may cause the automatically merged
1152 ;; buffer to be lost. So, keep the buffer modified.
1153 ;;(ediff-with-current-buffer ediff-buffer-C
1154 ;; (set-buffer-modified-p nil))
1157 ;;;###autoload
1158 (defun ediff-merge-files (file-A file-B
1159 ;; MERGE-BUFFER-FILE is the file to be
1160 ;; associated with the merge buffer
1161 &optional startup-hooks merge-buffer-file)
1162 "Merge two files without ancestor.
1163 FILE-A and FILE-B are the names of the files to be merged.
1164 STARTUP-HOOKS is a list of functions that Emacs calls without
1165 arguments after setting up the Ediff buffers. MERGE-BUFFER-FILE
1166 is the name of the file to be associated with the merge buffer.."
1167 (interactive
1168 (let ((dir-A (if ediff-use-last-dir
1169 ediff-last-dir-A
1170 default-directory))
1171 dir-B f)
1172 (list (setq f (ediff-read-file-name
1173 "File A to merge"
1174 dir-A
1175 (ediff-get-default-file-name)
1176 'no-dirs))
1177 (ediff-read-file-name "File B to merge"
1178 (setq dir-B
1179 (if ediff-use-last-dir
1180 ediff-last-dir-B
1181 (file-name-directory f)))
1182 (progn
1183 (ediff-add-to-history
1184 'file-name-history
1185 (ediff-abbreviate-file-name
1186 (expand-file-name
1187 (file-name-nondirectory f)
1188 dir-B)))
1189 (ediff-get-default-file-name f 1)))
1191 (setq startup-hooks (cons 'ediff-merge-on-startup startup-hooks))
1192 (ediff-files-internal file-A
1193 (if (file-directory-p file-B)
1194 (expand-file-name
1195 (file-name-nondirectory file-A) file-B)
1196 file-B)
1197 nil ; file-C
1198 startup-hooks
1199 'ediff-merge-files
1200 merge-buffer-file))
1202 ;;;###autoload
1203 (defun ediff-merge-files-with-ancestor (file-A file-B file-ancestor
1204 &optional
1205 startup-hooks
1206 ;; MERGE-BUFFER-FILE is the file
1207 ;; to be associated with the
1208 ;; merge buffer
1209 merge-buffer-file)
1210 "Merge two files with ancestor.
1211 FILE-A and FILE-B are the names of the files to be merged, and
1212 FILE-ANCESTOR is the name of the ancestor file. STARTUP-HOOKS is
1213 a list of functions that Emacs calls without arguments after
1214 setting up the Ediff buffers. MERGE-BUFFER-FILE is the name of
1215 the file to be associated with the merge buffer."
1216 (interactive
1217 (let ((dir-A (if ediff-use-last-dir
1218 ediff-last-dir-A
1219 default-directory))
1220 dir-B dir-ancestor f ff)
1221 (list (setq f (ediff-read-file-name
1222 "File A to merge"
1223 dir-A
1224 (ediff-get-default-file-name)
1225 'no-dirs))
1226 (setq ff (ediff-read-file-name "File B to merge"
1227 (setq dir-B
1228 (if ediff-use-last-dir
1229 ediff-last-dir-B
1230 (file-name-directory f)))
1231 (progn
1232 (ediff-add-to-history
1233 'file-name-history
1234 (ediff-abbreviate-file-name
1235 (expand-file-name
1236 (file-name-nondirectory f)
1237 dir-B)))
1238 (ediff-get-default-file-name f 1))))
1239 (ediff-read-file-name "Ancestor file"
1240 (setq dir-ancestor
1241 (if ediff-use-last-dir
1242 ediff-last-dir-ancestor
1243 (file-name-directory ff)))
1244 (progn
1245 (ediff-add-to-history
1246 'file-name-history
1247 (ediff-abbreviate-file-name
1248 (expand-file-name
1249 (file-name-nondirectory ff)
1250 dir-ancestor)))
1251 (ediff-get-default-file-name ff 2)))
1253 (setq startup-hooks (cons 'ediff-merge-on-startup startup-hooks))
1254 (ediff-files-internal file-A
1255 (if (file-directory-p file-B)
1256 (expand-file-name
1257 (file-name-nondirectory file-A) file-B)
1258 file-B)
1259 file-ancestor
1260 startup-hooks
1261 'ediff-merge-files-with-ancestor
1262 merge-buffer-file))
1264 ;;;###autoload
1265 (defalias 'ediff-merge-with-ancestor 'ediff-merge-files-with-ancestor)
1267 ;;;###autoload
1268 (defun ediff-merge-buffers (buffer-A buffer-B
1269 &optional
1270 ;; MERGE-BUFFER-FILE is the file to be
1271 ;; associated with the merge buffer
1272 startup-hooks job-name merge-buffer-file)
1273 "Merge buffers without ancestor.
1274 BUFFER-A and BUFFER-B are the buffers to be merged.
1275 STARTUP-HOOKS is a list of functions that Emacs calls without
1276 arguments after setting up the Ediff buffers. JOB-NAME is a
1277 symbol describing the Ediff job type; it defaults to
1278 `ediff-merge-buffers', but can also be one of
1279 `ediff-merge-files-with-ancestor', `ediff-last-dir-ancestor',
1280 `ediff-last-dir-C', `ediff-buffers', `ediff-buffers3', or
1281 `ediff-merge-buffers-with-ancestor'. MERGE-BUFFER-FILE is the
1282 name of the file to be associated with the merge buffer."
1283 (interactive
1284 (let (bf)
1285 (list (setq bf (read-buffer "Buffer A to merge: "
1286 (ediff-other-buffer "") t))
1287 (read-buffer "Buffer B to merge: "
1288 (progn
1289 ;; realign buffers so that two visible bufs will be
1290 ;; at the top
1291 (save-window-excursion (other-window 1))
1292 (ediff-other-buffer bf))
1293 t))))
1295 (setq startup-hooks (cons 'ediff-merge-on-startup startup-hooks))
1296 (or job-name (setq job-name 'ediff-merge-buffers))
1297 (ediff-buffers-internal
1298 buffer-A buffer-B nil startup-hooks job-name merge-buffer-file))
1300 ;;;###autoload
1301 (defun ediff-merge-buffers-with-ancestor (buffer-A buffer-B buffer-ancestor
1302 &optional
1303 startup-hooks
1304 job-name
1305 ;; MERGE-BUFFER-FILE is the
1306 ;; file to be associated
1307 ;; with the merge buffer
1308 merge-buffer-file)
1309 "Merge buffers with ancestor.
1310 BUFFER-A and BUFFER-B are the buffers to be merged, and
1311 BUFFER-ANCESTOR is their ancestor. STARTUP-HOOKS is a list of
1312 functions that Emacs calls without arguments after setting up the
1313 Ediff buffers. JOB-NAME is a symbol describing the Ediff job
1314 type; it defaults to `ediff-merge-buffers-with-ancestor', but can
1315 also be one of `ediff-merge-files-with-ancestor',
1316 `ediff-last-dir-ancestor', `ediff-last-dir-C', `ediff-buffers',
1317 `ediff-buffers3', or `ediff-merge-buffers'. MERGE-BUFFER-FILE is
1318 the name of the file to be associated with the merge buffer."
1319 (interactive
1320 (let (bf bff)
1321 (list (setq bf (read-buffer "Buffer A to merge: "
1322 (ediff-other-buffer "") t))
1323 (setq bff (read-buffer "Buffer B to merge: "
1324 (progn
1325 ;; realign buffers so that two visible
1326 ;; bufs will be at the top
1327 (save-window-excursion (other-window 1))
1328 (ediff-other-buffer bf))
1330 (read-buffer "Ancestor buffer: "
1331 (progn
1332 ;; realign buffers so that three visible
1333 ;; bufs will be at the top
1334 (save-window-excursion (other-window 1))
1335 (ediff-other-buffer (list bf bff)))
1339 (setq startup-hooks (cons 'ediff-merge-on-startup startup-hooks))
1340 (or job-name (setq job-name 'ediff-merge-buffers-with-ancestor))
1341 (ediff-buffers-internal
1342 buffer-A buffer-B buffer-ancestor startup-hooks job-name merge-buffer-file))
1345 ;;;###autoload
1346 (defun ediff-merge-revisions (&optional file startup-hooks merge-buffer-file)
1347 ;; MERGE-BUFFER-FILE is the file to be associated with the merge buffer
1348 "Run Ediff by merging two revisions of a file.
1349 The file is the optional FILE argument or the file visited by the
1350 current buffer. STARTUP-HOOKS is a list of functions that Emacs
1351 calls without arguments after setting up the Ediff buffers.
1352 MERGE-BUFFER-FILE is the name of the file to be associated with
1353 the merge buffer."
1354 (interactive)
1355 (if (stringp file) (find-file file))
1356 (let (rev1 rev2)
1357 (setq rev1
1358 (read-string
1359 (format-message
1360 "Version 1 to merge (default %s's working version): "
1361 (if (stringp file)
1362 (file-name-nondirectory file) "current buffer")))
1363 rev2
1364 (read-string
1365 (format
1366 "Version 2 to merge (default %s): "
1367 (if (stringp file)
1368 (file-name-nondirectory file) "current buffer"))))
1369 (ediff-load-version-control)
1370 ;; ancestor-revision=nil
1371 (funcall
1372 (intern (format "ediff-%S-merge-internal" ediff-version-control-package))
1373 rev1 rev2 nil startup-hooks merge-buffer-file)))
1376 ;;;###autoload
1377 (defun ediff-merge-revisions-with-ancestor (&optional
1378 file startup-hooks
1379 ;; MERGE-BUFFER-FILE is the file to
1380 ;; be associated with the merge
1381 ;; buffer
1382 merge-buffer-file)
1383 "Run Ediff by merging two revisions of a file with a common ancestor.
1384 The file is the optional FILE argument or the file visited by the
1385 current buffer. STARTUP-HOOKS is a list of functions that Emacs
1386 calls without arguments after setting up the Ediff buffers.
1387 MERGE-BUFFER-FILE is the name of the file to be associated with
1388 the merge buffer."
1389 (interactive)
1390 (if (stringp file) (find-file file))
1391 (let (rev1 rev2 ancestor-rev)
1392 (setq rev1
1393 (read-string
1394 (format-message
1395 "Version 1 to merge (default %s's working version): "
1396 (if (stringp file)
1397 (file-name-nondirectory file) "current buffer")))
1398 rev2
1399 (read-string
1400 (format
1401 "Version 2 to merge (default %s): "
1402 (if (stringp file)
1403 (file-name-nondirectory file) "current buffer")))
1404 ancestor-rev
1405 (read-string
1406 (format-message
1407 "Ancestor version (default %s's base revision): "
1408 (if (stringp file)
1409 (file-name-nondirectory file) "current buffer"))))
1410 (ediff-load-version-control)
1411 (funcall
1412 (intern (format "ediff-%S-merge-internal" ediff-version-control-package))
1413 rev1 rev2 ancestor-rev startup-hooks merge-buffer-file)))
1415 ;;; Apply patch
1416 (defvar ediff-last-dir-patch)
1417 (defvar ediff-patch-default-directory)
1418 (declare-function ediff-get-patch-buffer "ediff-ptch"
1419 (&optional arg patch-buf))
1420 (declare-function ediff-dispatch-file-patching-job "ediff-ptch"
1421 (patch-buf filename &optional startup-hooks))
1423 ;;;###autoload
1424 (defun ediff-patch-file (&optional arg patch-buf)
1425 "Query for a file name, and then run Ediff by patching that file.
1426 If optional PATCH-BUF is given, use the patch in that buffer
1427 and don't ask the user.
1428 If prefix argument ARG, then: if even argument, assume that the
1429 patch is in a buffer. If odd -- assume it is in a file."
1430 (interactive "P")
1431 (let (source-dir source-file)
1432 (require 'ediff-ptch)
1433 (setq patch-buf
1434 (ediff-get-patch-buffer
1435 (and arg (prefix-numeric-value arg))
1436 (and patch-buf (get-buffer patch-buf))))
1437 (setq source-dir (cond (ediff-use-last-dir ediff-last-dir-patch)
1438 ((and (not ediff-patch-default-directory)
1439 (buffer-file-name patch-buf))
1440 (file-name-directory
1441 (expand-file-name
1442 (buffer-file-name patch-buf))))
1443 (t default-directory)))
1444 (setq source-file
1445 (read-file-name
1446 "File to patch (directory, if multifile patch): "
1447 ;; use an explicit initial file
1448 source-dir nil nil (ediff-get-default-file-name)))
1449 (ediff-dispatch-file-patching-job patch-buf source-file)))
1451 (declare-function ediff-patch-buffer-internal "ediff-ptch"
1452 (patch-buf buf-to-patch-name &optional startup-hooks))
1454 ;;;###autoload
1455 (defun ediff-patch-buffer (&optional arg patch-buf)
1456 "Run Ediff by patching the buffer specified at prompt.
1457 Without the optional prefix ARG, asks if the patch is in some buffer and
1458 prompts for the buffer or a file, depending on the answer.
1459 With ARG=1, assumes the patch is in a file and prompts for the file.
1460 With ARG=2, assumes the patch is in a buffer and prompts for the buffer.
1461 PATCH-BUF is an optional argument, which specifies the buffer that contains the
1462 patch. If not given, the user is prompted according to the prefix argument."
1463 (interactive "P")
1464 (require 'ediff-ptch)
1465 (setq patch-buf
1466 (ediff-get-patch-buffer
1467 (if arg (prefix-numeric-value arg)) patch-buf))
1468 (ediff-patch-buffer-internal
1469 patch-buf
1470 (read-buffer "Which buffer to patch? " (ediff-other-buffer patch-buf)
1471 'require-match)))
1474 ;;;###autoload
1475 (defalias 'epatch 'ediff-patch-file)
1476 ;;;###autoload
1477 (defalias 'epatch-buffer 'ediff-patch-buffer)
1482 ;;; Versions Control functions
1484 ;;;###autoload
1485 (defun ediff-revision (&optional file startup-hooks)
1486 "Run Ediff by comparing versions of a file.
1487 The file is an optional FILE argument or the file entered at the prompt.
1488 Default: the file visited by the current buffer.
1489 Uses `vc.el' or `rcs.el' depending on `ediff-version-control-package'.
1490 STARTUP-HOOKS is a list of functions that Emacs calls without
1491 arguments after setting up the Ediff buffers."
1492 ;; if buffer is non-nil, use that buffer instead of the current buffer
1493 (interactive "P")
1494 (if (not (stringp file))
1495 (setq file
1496 (ediff-read-file-name "Compare revisions for file"
1497 (if ediff-use-last-dir
1498 ediff-last-dir-A
1499 default-directory)
1500 (ediff-get-default-file-name)
1501 'no-dirs)))
1502 (find-file file)
1503 (if (and (buffer-modified-p)
1504 (y-or-n-p (format "Buffer %s is modified. Save buffer? "
1505 (buffer-name))))
1506 (save-buffer (current-buffer)))
1507 (let (rev1 rev2)
1508 (setq rev1
1509 (read-string
1510 (format "Revision 1 to compare (default %s's latest revision): "
1511 (file-name-nondirectory file)))
1512 rev2
1513 (read-string
1514 (format "Revision 2 to compare (default %s's current state): "
1515 (file-name-nondirectory file))))
1516 (ediff-load-version-control)
1517 (funcall
1518 (intern (format "ediff-%S-internal" ediff-version-control-package))
1519 rev1 rev2 startup-hooks)
1523 ;;;###autoload
1524 (defalias 'erevision 'ediff-revision)
1527 ;; Test if version control package is loaded and load if not
1528 ;; Is SILENT is non-nil, don't report error if package is not found.
1529 (defun ediff-load-version-control (&optional silent)
1530 (require 'ediff-vers)
1531 (or (featurep ediff-version-control-package)
1532 (if (locate-library (symbol-name ediff-version-control-package))
1533 (progn
1534 (message "") ; kill the message from `locate-library'
1535 (require ediff-version-control-package))
1536 (or silent
1537 (user-error "Version control package %S.el not found. Use vc.el instead"
1538 ediff-version-control-package)))))
1541 ;;;###autoload
1542 (defun ediff-version ()
1543 "Return string describing the version of Ediff.
1544 When called interactively, displays the version."
1545 (interactive)
1546 (if (if (featurep 'xemacs)
1547 (interactive-p)
1548 (called-interactively-p 'interactive))
1549 (message "%s" (ediff-version))
1550 (format "Ediff %s of %s" ediff-version ediff-date)))
1552 ;; info is run first, and will autoload info.el.
1553 (declare-function Info-goto-node "info" (nodename &optional fork strict-case))
1555 ;;;###autoload
1556 (defun ediff-documentation (&optional node)
1557 "Display Ediff's manual.
1558 With optional NODE, goes to that node."
1559 (interactive)
1560 (let ((ctl-window ediff-control-window)
1561 (ctl-buf ediff-control-buffer))
1563 (ediff-skip-unsuitable-frames)
1564 (condition-case nil
1565 (progn
1566 (pop-to-buffer (get-buffer-create "*info*"))
1567 (info (if (featurep 'xemacs) "ediff.info" "ediff"))
1568 (if node
1569 (Info-goto-node node)
1570 (message "Type `i' to search for a specific topic"))
1571 (raise-frame))
1572 (error (beep 1)
1573 (with-output-to-temp-buffer ediff-msg-buffer
1574 (ediff-with-current-buffer standard-output
1575 (fundamental-mode))
1576 (princ ediff-BAD-INFO))
1577 (if (window-live-p ctl-window)
1578 (progn
1579 (select-window ctl-window)
1580 (set-window-buffer ctl-window ctl-buf)))))))
1583 ;;; Command line interface
1585 ;;;###autoload
1586 (defun ediff-files-command ()
1587 "Call `ediff-files' with the next two command line arguments."
1588 (let ((file-a (nth 0 command-line-args-left))
1589 (file-b (nth 1 command-line-args-left)))
1590 (setq command-line-args-left (nthcdr 2 command-line-args-left))
1591 (ediff file-a file-b)))
1593 ;;;###autoload
1594 (defun ediff3-files-command ()
1595 "Call `ediff3-files' with the next three command line arguments."
1596 (let ((file-a (nth 0 command-line-args-left))
1597 (file-b (nth 1 command-line-args-left))
1598 (file-c (nth 2 command-line-args-left)))
1599 (setq command-line-args-left (nthcdr 3 command-line-args-left))
1600 (ediff3 file-a file-b file-c)))
1602 ;;;###autoload
1603 (defun ediff-merge-command ()
1604 "Call `ediff-merge-files' with the next two command line arguments."
1605 (let ((file-a (nth 0 command-line-args-left))
1606 (file-b (nth 1 command-line-args-left)))
1607 (setq command-line-args-left (nthcdr 2 command-line-args-left))
1608 (ediff-merge-files file-a file-b)))
1610 ;;;###autoload
1611 (defun ediff-merge-with-ancestor-command ()
1612 "Call `ediff-merge-files-with-ancestor' with the next three command line arguments."
1613 (let ((file-a (nth 0 command-line-args-left))
1614 (file-b (nth 1 command-line-args-left))
1615 (ancestor (nth 2 command-line-args-left)))
1616 (setq command-line-args-left (nthcdr 3 command-line-args-left))
1617 (ediff-merge-files-with-ancestor file-a file-b ancestor)))
1619 ;;;###autoload
1620 (defun ediff-directories-command ()
1621 "Call `ediff-directories' with the next three command line arguments."
1622 (let ((file-a (nth 0 command-line-args-left))
1623 (file-b (nth 1 command-line-args-left))
1624 (regexp (nth 2 command-line-args-left)))
1625 (setq command-line-args-left (nthcdr 3 command-line-args-left))
1626 (ediff-directories file-a file-b regexp)))
1628 ;;;###autoload
1629 (defun ediff-directories3-command ()
1630 "Call `ediff-directories3' with the next four command line arguments."
1631 (let ((file-a (nth 0 command-line-args-left))
1632 (file-b (nth 1 command-line-args-left))
1633 (file-c (nth 2 command-line-args-left))
1634 (regexp (nth 3 command-line-args-left)))
1635 (setq command-line-args-left (nthcdr 4 command-line-args-left))
1636 (ediff-directories3 file-a file-b file-c regexp)))
1638 ;;;###autoload
1639 (defun ediff-merge-directories-command ()
1640 "Call `ediff-merge-directories' with the next three command line arguments."
1641 (let ((file-a (nth 0 command-line-args-left))
1642 (file-b (nth 1 command-line-args-left))
1643 (regexp (nth 2 command-line-args-left)))
1644 (setq command-line-args-left (nthcdr 3 command-line-args-left))
1645 (ediff-merge-directories file-a file-b regexp)))
1647 ;;;###autoload
1648 (defun ediff-merge-directories-with-ancestor-command ()
1649 "Call `ediff-merge-directories-with-ancestor' with the next four command line arguments."
1650 (let ((file-a (nth 0 command-line-args-left))
1651 (file-b (nth 1 command-line-args-left))
1652 (ancestor (nth 2 command-line-args-left))
1653 (regexp (nth 3 command-line-args-left)))
1654 (setq command-line-args-left (nthcdr 4 command-line-args-left))
1655 (ediff-merge-directories-with-ancestor file-a file-b ancestor regexp)))
1659 (require 'ediff-util)
1661 (run-hooks 'ediff-load-hook)
1664 ;; Local Variables:
1665 ;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
1666 ;; eval: (put 'ediff-with-current-buffer 'lisp-indent-hook 1)
1667 ;; eval: (put 'ediff-with-current-buffer 'edebug-form-spec '(form body))
1668 ;; End:
1670 ;;; ediff.el ends here