(xcar, xcdr): Print with /x.
[emacs.git] / lisp / ediff-mult.el
blobc6fa5694612d0caadeb2ea81b58ed69de53a64de
1 ;;; ediff-mult.el --- support for multi-file/multi-buffer processing in Ediff
3 ;; Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
5 ;; Author: Michael Kifer <kifer@cs.sunysb.edu>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
24 ;;; Commentary:
26 ;; Users are encouraged to add functionality to this file.
27 ;; The present file contains all the infrastructure needed for that.
29 ;; Generally, to to implement a new multisession capability within Ediff,
30 ;; you need to tell it
32 ;; 1. How to display the session group buffer.
33 ;; This function must indicate which Ediff sessions are active (+) and
34 ;; which are finished (-).
35 ;; See ediff-redraw-directory-group-buffer for an example.
36 ;; In all likelihood, ediff-redraw-directory-group-buffer can be used
37 ;; directly or after a small modification.
38 ;; 2. What action to take when the user clicks button 2 or types v,e, or
39 ;; RET. See ediff-filegroup-action.
40 ;; 3. Provide a list of pairs or triples of file names (or buffers,
41 ;; depending on the particular Ediff operation you want to invoke)
42 ;; in the following format:
43 ;; (descriptor (obj1 obj2 obj3) (...) ...)
44 ;; Actually, the format of this list is pretty much up to the
45 ;; developer. The only thing is that it must be a list of lists,
46 ;; and the first list must describe the meta session, and subsequent
47 ;; elements must describe individual sessions.
48 ;; This descriptor must be a list of two, three, or four elements (nil
49 ;; or string). The function ediff-redraw-registry-buffer displays the
50 ;; second through last of these in the registry buffer.
51 ;; Also, keep in mind that the function ediff-prepare-meta-buffer
52 ;; (which see) prepends the session group buffer to the descriptor and
53 ;; nil in front of each subsequent list (i.e., the above list
54 ;; will become
55 ;; ((meta-buf descriptor) (nil obj1 obj2 obj3) (nil ...) ...)
56 ;; Ediff expects that your function (in 2 above) will arrange to
57 ;; replace this prepended nil (via setcar) with the actual ediff
58 ;; control buffer associated with an appropriate Ediff session.
59 ;; This is arranged through internal startup hooks that can be passed
60 ;; to any of Ediff major entries (such as ediff-files, epatch, etc.).
61 ;; See how this is done in ediff-filegroup-action.
63 ;; Session descriptions are of the form (obj1 obj2 obj3), which
64 ;; describe objects relevant to the session. Usually they are names of
65 ;; files, but sometimes they may be other things. For instance, obj3 is
66 ;; nil for jobs that involve only two files. For patch jobs, obj2 and
67 ;; obj3 are markers that specify the patch corresponding to the file
68 ;; (whose name is obj1).
69 ;; 4. Write a function that makes a call to ediff-prepare-meta-buffer
70 ;; passing all this info.
71 ;; You may be able to use ediff-directories-internal as a template.
72 ;; 5. If you intend to add several related pieces of functionality,
73 ;; you may want to keep the function in 4 as an internal version
74 ;; and then write several top-level interactive functions that call it
75 ;; with different parameters.
76 ;; See how ediff-directories, ediff-merge-directories, and
77 ;; ediff-merge-directories-with-ancestor all use
78 ;; ediff-directories-internal.
80 ;; A useful addition here could be session groups selected by patterns
81 ;; (which are different in each directory). For instance, one may want to
82 ;; compare files of the form abc{something}.c to files old{something}.d
83 ;; which may be in the same or different directories. Or, one may want to
84 ;; compare all files of the form {something} to files of the form {something}~.
86 ;; Implementing this requires writing an collating function, which should pair
87 ;; up appropriate files. It will also require a generalization of the functions
88 ;; that do the layout of the meta- and differences buffers and of
89 ;; ediff-filegroup-action.
91 ;;; Code:
93 (provide 'ediff-mult)
95 (defgroup ediff-mult nil
96 "Multi-file and multi-buffer processing in ediff"
97 :prefix "ediff-"
98 :group 'ediff)
101 ;; compiler pacifier
102 (eval-when-compile
103 (let ((load-path (cons (expand-file-name ".") load-path)))
104 (or (featurep 'ediff-init)
105 (load "ediff-init.el" nil nil 'nosuffix))
106 (or (featurep 'ediff-util)
107 (load "ediff-util.el" nil nil 'nosuffix))
109 ;; end pacifier
111 (require 'ediff-init)
112 (require 'ediff-util)
114 ;; meta-buffer
115 (ediff-defvar-local ediff-meta-buffer nil "")
116 (ediff-defvar-local ediff-parent-meta-buffer nil "")
117 ;; the registry buffer
118 (defvar ediff-registry-buffer nil)
120 (defconst ediff-meta-buffer-message "This is an Ediff Session Group Panel: %s
122 Useful commands:
123 button2, `v', RET over a session line: start that Ediff session
124 `M':\tin any session invoked from here, brings back this group panel
125 `R':\tdisplay the registry of active Ediff sessions
126 `h':\tmark session for hiding (toggle)
127 `x':\thide marked sessions; with prefix arg--unhide hidden sessions
128 `m':\tmark session for a non-hiding operation (toggle)
129 SPC:\tnext session
130 DEL:\tprevious session
131 `E':\tbrowse Ediff on-line manual
132 `q':\tquit this session group
135 (ediff-defvar-local ediff-meta-buffer-map nil
136 "The keymap for the meta buffer.")
137 (defvar ediff-dir-diffs-buffer-map (make-sparse-keymap)
138 "The keymap to be installed in the buffer showing differences between
139 directories.")
141 ;; Variable specifying the action to take when the use invokes ediff in the
142 ;; meta buffer. This is usually ediff-registry-action or ediff-filegroup-action
143 (ediff-defvar-local ediff-meta-action-function nil "")
144 ;; Tells ediff-update-meta-buffer how to redraw it
145 (ediff-defvar-local ediff-meta-redraw-function nil "")
146 ;; Tells ediff-filegroup-action and similar procedures how to invoke Ediff for
147 ;; the sessions in a given session group
148 (ediff-defvar-local ediff-session-action-function nil "")
150 (ediff-defvar-local ediff-metajob-name nil "")
152 ;; buffer used to collect custom diffs from individual sessions in the group
153 (ediff-defvar-local ediff-meta-diff-buffer nil "")
155 ;; history var to use for filtering groups
156 (defvar ediff-filtering-regexp-history nil "")
158 ;; This has the form ((ctl-buf file1 file2) (stl-buf file1 file2) ...)
159 ;; If ctl-buf is nil, the file-pair wasn't processed yet. If it is
160 ;; killed-buffer object, the file pair has been processed. If it is a live
161 ;; buffer, this means ediff is still working on the pair
162 (ediff-defvar-local ediff-meta-list nil "")
165 ;; the difference list between directories in a directory session group
166 (ediff-defvar-local ediff-dir-difference-list nil "")
167 (ediff-defvar-local ediff-dir-diffs-buffer nil "")
169 ;; The registry of Ediff sessions. A list of control buffers.
170 (defvar ediff-session-registry nil)
172 (defcustom ediff-registry-setup-hook nil
173 "*Hooks run just after the registry control panel is set up."
174 :type 'hook
175 :group 'ediff-mult)
176 (defcustom ediff-session-group-setup-hook nil
177 "*Hooks run just after a meta-buffer controlling a session group, such as
178 ediff-directories, is run."
179 :type 'hook
180 :group 'ediff-mult)
181 (defcustom ediff-quit-session-group-hook nil
182 "*Hooks run just before exiting a session group."
183 :type 'hook
184 :group 'ediff-mult)
185 (defcustom ediff-show-registry-hook nil
186 "*Hooks run just after the registry buffer is shown."
187 :type 'hook
188 :group 'ediff-mult)
189 (defcustom ediff-show-session-group-hook nil
190 "*Hooks run just after a session group buffer is shown."
191 :type 'hook
192 :group 'ediff-mult)
193 (defcustom ediff-meta-buffer-keymap-setup-hook nil
194 "*Hooks run just after setting up the ediff-meta-buffer-map.
195 This keymap controls key bindings in the meta buffer and is a local variable.
196 This means that you can set different bindings for different kinds of meta
197 buffers."
198 :type 'hook
199 :group 'ediff-mult)
201 ;; buffer holding the multi-file patch. local to the meta buffer
202 (ediff-defvar-local ediff-meta-patchbufer nil "")
204 ;;; API for ediff-meta-list
206 ;; group buffer/regexp
207 (defun ediff-get-group-buffer (meta-list)
208 (nth 0 (car meta-list)))
210 (defun ediff-get-group-regexp (meta-list)
211 (nth 1 (car meta-list)))
212 ;; group objects
213 (defun ediff-get-group-objA (meta-list)
214 (nth 2 (car meta-list)))
215 (defun ediff-get-group-objB (meta-list)
216 (nth 3 (car meta-list)))
217 (defun ediff-get-group-objC (meta-list)
218 (nth 4 (car meta-list)))
219 (defun ediff-get-group-merge-autostore-dir (meta-list)
220 (nth 5 (car meta-list)))
222 ;; session buffer
223 (defun ediff-get-session-buffer (elt)
224 (nth 0 elt))
225 (defun ediff-get-session-status (elt)
226 (nth 1 elt))
227 (defun ediff-set-session-status (session-info new-status)
228 (setcar (cdr session-info) new-status))
229 ;; session objects
230 (defun ediff-get-session-objA (elt)
231 (nth 2 elt))
232 (defun ediff-get-session-objB (elt)
233 (nth 3 elt))
234 (defun ediff-get-session-objC (elt)
235 (nth 4 elt))
236 (defun ediff-get-session-objA-name (elt)
237 (car (nth 2 elt)))
238 (defun ediff-get-session-objB-name (elt)
239 (car (nth 3 elt)))
240 (defun ediff-get-session-objC-name (elt)
241 (car (nth 4 elt)))
242 ;; equality indicators
243 (defsubst ediff-get-file-eqstatus (elt)
244 (nth 1 elt))
245 (defsubst ediff-set-file-eqstatus (elt value)
246 (setcar (cdr elt) value))
248 ;; set up the keymap in the meta buffer
249 (defun ediff-setup-meta-map()
250 (setq ediff-meta-buffer-map (make-sparse-keymap))
251 (suppress-keymap ediff-meta-buffer-map)
252 (define-key ediff-meta-buffer-map "q" 'ediff-quit-meta-buffer)
253 (define-key ediff-meta-buffer-map "R" 'ediff-show-registry)
254 (define-key ediff-meta-buffer-map "E" 'ediff-documentation)
255 (define-key ediff-meta-buffer-map "v" ediff-meta-action-function)
256 (define-key ediff-meta-buffer-map "\C-m" ediff-meta-action-function)
257 (define-key ediff-meta-buffer-map " " 'ediff-next-meta-item)
258 (define-key ediff-meta-buffer-map "\C-?" 'ediff-previous-meta-item)
259 (define-key ediff-meta-buffer-map [delete] 'ediff-previous-meta-item)
260 (define-key ediff-meta-buffer-map [backspace] 'ediff-previous-meta-item)
261 (or (ediff-one-filegroup-metajob)
262 (define-key ediff-meta-buffer-map "=" 'ediff-meta-mark-equal-files))
263 (if ediff-no-emacs-help-in-control-buffer
264 (define-key ediff-meta-buffer-map "\C-h" 'ediff-previous-meta-item))
265 (if ediff-emacs-p
266 (define-key ediff-meta-buffer-map [mouse-2] ediff-meta-action-function)
267 (define-key ediff-meta-buffer-map [button2] ediff-meta-action-function))
269 (use-local-map ediff-meta-buffer-map)
270 ;; modify ediff-meta-buffer-map here
271 (run-hooks 'ediff-meta-buffer-keymap-setup-hook))
273 (defun ediff-meta-mode ()
274 "This mode controls all operations on Ediff session groups.
275 It is entered through one of the following commands:
276 `ediff-directories'
277 `edirs'
278 `ediff-directories3'
279 `edirs3'
280 `ediff-merge-directories'
281 `edirs-merge'
282 `ediff-merge-directories-with-ancestor'
283 `edirs-merge-with-ancestor'
284 `ediff-directory-revisions'
285 `edir-revisions'
286 `ediff-merge-directory-revisions'
287 `edir-merge-revisions'
288 `ediff-merge-directory-revisions-with-ancestor'
289 `edir-merge-revisions-with-ancestor'
291 Commands:
292 \\{ediff-meta-buffer-map}"
293 (kill-all-local-variables)
294 (setq major-mode 'ediff-meta-mode)
295 (setq mode-name "MetaEdiff"))
298 ;; the keymap for the buffer showing directory differences
299 (suppress-keymap ediff-dir-diffs-buffer-map)
300 (define-key ediff-dir-diffs-buffer-map "q" 'ediff-bury-dir-diffs-buffer)
301 (define-key ediff-dir-diffs-buffer-map " " 'next-line)
302 (define-key ediff-dir-diffs-buffer-map "\C-?" 'previous-line)
303 (define-key ediff-dir-diffs-buffer-map [delete] 'previous-line)
304 (define-key ediff-dir-diffs-buffer-map [backspace] 'previous-line)
306 (defun ediff-next-meta-item (count)
307 "Move to the next item in Ediff registry or session group buffer.
308 Moves in circular fashion. With numeric prefix arg, skip this many items."
309 (interactive "p")
310 (or count (setq count 1))
311 (while (< 0 count)
312 (setq count (1- count))
313 (ediff-next-meta-item1)))
315 ;; Move to the next meta item
316 (defun ediff-next-meta-item1 ()
317 (let (pos)
318 (setq pos (ediff-next-meta-overlay-start (point)))
319 ;;; ;; skip deleted
320 ;;; (while (memq (ediff-get-session-status
321 ;;; (ediff-get-meta-info (current-buffer) pos 'noerror))
322 ;;; '(?H ?I))
323 ;;; (setq pos (ediff-next-meta-overlay-start pos)))
325 (if pos (goto-char pos))
326 (if (eq ediff-metajob-name 'ediff-registry)
327 (if (and (ediff-get-meta-info (current-buffer) pos 'noerror)
328 (search-forward "*Ediff" nil t))
329 (skip-chars-backward "a-zA-Z*"))
330 (if (> (skip-chars-forward "-+?H* \t0-9") 0)
331 (backward-char 1)))))
334 (defun ediff-previous-meta-item (count)
335 "Move to the previous item in Ediff registry or session group buffer.
336 Moves in circular fashion. With numeric prefix arg, skip this many items."
337 (interactive "p")
338 (or count (setq count 1))
339 (while (< 0 count)
340 (setq count (1- count))
341 (ediff-previous-meta-item1)))
343 (defun ediff-previous-meta-item1 ()
344 (let (pos)
345 (setq pos (ediff-previous-meta-overlay-start (point)))
346 ;;; ;; skip deleted
347 ;;; (while (ediff-get-session-status
348 ;;; (ediff-get-meta-info (current-buffer) pos 'noerror))
349 ;;; (setq pos (ediff-previous-meta-overlay-start pos)))
351 (if pos (goto-char pos))
352 (if (eq ediff-metajob-name 'ediff-registry)
353 (if (and (ediff-get-meta-info (current-buffer) pos 'noerror)
354 (search-forward "*Ediff" nil t))
355 (skip-chars-backward "a-zA-Z*"))
356 (if (> (skip-chars-forward "-+?H* \t0-9") 0)
357 (backward-char 1)))
360 (defsubst ediff-add-slash-if-directory (dir file)
361 (if (file-directory-p (concat dir file))
362 (file-name-as-directory file)
363 file))
366 ;; DIR1, DIR2, DIR3 are directories. DIR3 can be nil.
367 ;; OUTPUT-DIR is a directory for auto-storing the results of merge jobs.
368 ;; Can be nil.
369 ;; REGEXP is a regexp used to filter out files in the directories.
370 ;; If a file is a directory in dir1 but not dir2 (or vice versa), it is not
371 ;; included in the intersection. However, a regular file that is a dir in dir3
372 ;; is included, since dir3 files are supposed to be ancestors for merging.
373 ;; Returns a list of the form:
374 ;; ((dir1 dir2 dir3) (f1 f2 f3) (f1 f2 f3) ...)
375 ;; dir3, f3 can be nil if intersecting only 2 directories.
376 ;; If COMPARISON-FUNC is given, use it. Otherwise, use string=
377 ;; DIFF-VAR contains the name of the variable in which to return the
378 ;; difference list (which represents the differences among the contents of
379 ;; directories). The diff list is of the form:
380 ;; ((dir1 dir2 dir3) (file . num) (file . num)...)
381 ;; where num encodes the set of dirs where the file is found:
382 ;; 2 - only dir1; 3 - only dir2; 5 - only dir3; 6 - dir1&2; 10 - dir1&3; etc.
383 (defun ediff-intersect-directories (jobname
384 diff-var regexp dir1 dir2
385 &optional
386 dir3 merge-autostore-dir comparison-func)
387 (setq comparison-func (or comparison-func 'string=))
388 (let (lis1 lis2 lis3 common auxdir1 auxdir2 auxdir3 difflist)
390 (setq auxdir1 (file-name-as-directory dir1)
391 lis1 (directory-files auxdir1 nil regexp)
392 lis1 (delete "." lis1)
393 lis1 (delete ".." lis1)
394 lis1 (mapcar
395 (function
396 (lambda (elt)
397 (ediff-add-slash-if-directory auxdir1 elt)))
398 lis1)
399 auxdir2 (file-name-as-directory dir2)
400 lis2 (mapcar
401 (function
402 (lambda (elt)
403 (ediff-add-slash-if-directory auxdir2 elt)))
404 (directory-files auxdir2 nil regexp)))
406 (if (stringp dir3)
407 (setq auxdir3 (file-name-as-directory dir3)
408 lis3 (mapcar
409 (function
410 (lambda (elt)
411 (ediff-add-slash-if-directory auxdir3 elt)))
412 (directory-files auxdir3 nil regexp))))
414 (if (stringp merge-autostore-dir)
415 (setq merge-autostore-dir
416 (file-name-as-directory merge-autostore-dir)))
417 (setq common (ediff-intersection lis1 lis2 comparison-func))
419 ;; In merge with ancestor jobs, we don't intersect with lis3.
420 ;; If there is no ancestor, we'll offer to merge without the ancestor.
421 ;; So, we intersect with lis3 only when we are doing 3-way file comparison
422 (if (and lis3 (ediff-comparison-metajob3 jobname))
423 (setq common (ediff-intersection common lis3 comparison-func)))
425 ;; copying is needed because sort sorts via side effects
426 (setq common (sort (ediff-copy-list common) 'string-lessp))
428 ;; compute difference list
429 (setq difflist (ediff-set-difference
430 (ediff-union (ediff-union lis1 lis2 comparison-func)
431 lis3
432 comparison-func)
433 common
434 comparison-func)
435 difflist (delete "." difflist)
436 ;; copying is needed because sort sorts via side effects
437 difflist (sort (ediff-copy-list (delete ".." difflist))
438 'string-lessp))
440 (setq difflist (mapcar (function (lambda (elt) (cons elt 1))) difflist))
442 ;; check for files belonging to lis1/2/3
443 (mapcar (function (lambda (elt)
444 (if (member (car elt) lis1)
445 (setcdr elt (* (cdr elt) 2)))
446 (if (member (car elt) lis2)
447 (setcdr elt (* (cdr elt) 3)))
448 (if (member (car elt) lis3)
449 (setcdr elt (* (cdr elt) 5)))
451 difflist)
452 (setq difflist (cons (list regexp auxdir1 auxdir2 auxdir3) difflist))
454 ;; return the difference list back to the calling function
455 (set diff-var difflist)
457 ;; return result
458 (cons (list regexp auxdir1 auxdir2 auxdir3 merge-autostore-dir)
459 (mapcar
460 (function
461 (lambda (elt)
462 (list (concat auxdir1 elt)
463 (concat auxdir2 elt)
464 (if lis3
465 (progn
466 ;; The following is done because:
467 ;; In merging with ancestor, we don't intersect
468 ;; with lis3. So, it is possible that elt is a
469 ;; file in auxdir1/2 but a directory in auxdir3
470 ;; Or elt may not exist in auxdir3 at all.
471 ;; In the first case, we add a slash at the end.
472 ;; In the second case, we insert nil.
473 (setq elt (ediff-add-slash-if-directory auxdir3 elt))
474 (if (file-exists-p (concat auxdir3 elt))
475 (concat auxdir3 elt)))))))
476 common))
479 ;; find directory files that are under revision.
480 ;; Include subdirectories, since we may visit them recursively.
481 ;; DIR1 is the directory to inspect.
482 ;; OUTPUT-DIR is the directory where to auto-store the results of merges. Can
483 ;; be nil.
484 (defun ediff-get-directory-files-under-revision (jobname
485 regexp dir1
486 &optional merge-autostore-dir)
487 (let (lis1 elt common auxdir1)
488 (setq auxdir1 (file-name-as-directory dir1)
489 lis1 (directory-files auxdir1 nil regexp))
491 (if (stringp merge-autostore-dir)
492 (setq merge-autostore-dir
493 (file-name-as-directory merge-autostore-dir)))
495 (while lis1
496 (setq elt (car lis1)
497 lis1 (cdr lis1))
498 ;; take files under revision control
499 (cond ((file-directory-p (concat auxdir1 elt))
500 (setq common (cons elt common)))
501 ((file-exists-p (concat auxdir1 elt ",v"))
502 (setq common (cons elt common)))
503 ((file-exists-p (concat auxdir1 "RCS/" elt ",v"))
504 (setq common (cons elt common)))
505 ) ; cond
506 ) ; while
508 (setq common (delete "." common)
509 common (delete ".." common)
510 common (delete "RCS" common))
512 ;; copying is needed because sort sorts via side effects
513 (setq common (sort (ediff-copy-list common) 'string-lessp))
515 ;; return result
516 (cons (list regexp auxdir1 nil nil merge-autostore-dir)
517 (mapcar (function (lambda (elt)
518 (list (concat auxdir1 elt)
519 nil nil)))
520 common))
524 ;; If file groups selected by patterns will ever be implemented, this
525 ;; comparison function might become useful.
526 ;;;; uses external variables PAT1 PAT2 to compare str1/2
527 ;;;; patterns must be of the form ???*???? where ??? are strings of chars
528 ;;;; containing no *.
529 ;;(defun ediff-pattern= (str1 str2)
530 ;; (let (pos11 pos12 pos21 pos22 len1 len2)
531 ;; (setq pos11 0
532 ;; len (length epat1)
533 ;; pos12 len)
534 ;; (while (and (< pos11 len) (not (= (aref epat1 pos11) ?*)))
535 ;; (setq pos11 (1+ pos11)))
536 ;; (while (and (> pos12 0) (not (= (aref epat1 (1- pos12)) ?*)))
537 ;; (setq pos12 (1- pos12)))
539 ;; (setq pos21 0
540 ;; len (length epat2)
541 ;; pos22 len)
542 ;; (while (and (< pos21 len) (not (= (aref epat2 pos21) ?*)))
543 ;; (setq pos21 (1+ pos21)))
544 ;; (while (and (> pos22 0) (not (= (aref epat2 (1- pos22)) ?*)))
545 ;; (setq pos22 (1- pos22)))
547 ;; (if (and (> (length str1) pos12) (>= pos12 pos11) (> pos11 -1)
548 ;; (> (length str2) pos22) (>= pos22 pos21) (> pos21 -1))
549 ;; (string= (substring str1 pos11 pos12)
550 ;; (substring str2 pos21 pos22)))
551 ;; ))
554 ;; Prepare meta-buffer in accordance with the argument-function and
555 ;; redraw-function. Must return the created meta-buffer.
556 (defun ediff-prepare-meta-buffer (action-func meta-list
557 meta-buffer-name redraw-function
558 jobname &optional startup-hooks)
559 (let* ((meta-buffer-name
560 (ediff-unique-buffer-name meta-buffer-name "*"))
561 (meta-buffer (get-buffer-create meta-buffer-name)))
562 (ediff-eval-in-buffer meta-buffer
564 ;; comes first
565 (ediff-meta-mode)
567 (setq ediff-meta-action-function action-func
568 ediff-meta-redraw-function redraw-function
569 ediff-metajob-name jobname
570 ediff-meta-buffer meta-buffer)
572 ;; comes after ediff-meta-action-function is set
573 (ediff-setup-meta-map)
575 (if (eq ediff-metajob-name 'ediff-registry)
576 (progn
577 (setq ediff-registry-buffer meta-buffer
578 ediff-meta-list meta-list)
579 ;; this func is used only from registry buffer, not from other
580 ;; meta-buffs.
581 (define-key
582 ediff-meta-buffer-map "M" 'ediff-show-meta-buff-from-registry))
583 ;; Initialize the meta list -- don't do this for registry.
585 ;; We prepend '(nil nil) to all elts of meta-list, except the first.
586 ;; The first nil will later be replaced by the session buffer. The
587 ;; second is reserved for session status.
589 ;; (car ediff-meta-list) gets cons'ed with the session group buffer.
590 ;; Also, session objects A/B/C are turned into lists of the form
591 ;; (obj eq-indicator). Eq-indicator is either nil or =. Initialized to
592 ;; nil. If later it is discovered that this file is = to some other
593 ;; file in the same session, eq-indicator is changed to `='.
594 ;; For now, the eq-indicator is used only for 2 and 3-file jobs.
595 (setq ediff-meta-list
596 (cons (cons meta-buffer (car meta-list))
597 (mapcar
598 (function
599 (lambda (elt)
600 (cons nil
601 (cons nil
602 ;; convert each obj to (obj nil),
603 ;; where nil is the initial value
604 ;; for eq-indicator -- see above
605 (mapcar
606 (function (lambda (obj) (list obj nil)))
607 elt)))))
608 (cdr meta-list)))))
610 (or (eq meta-buffer ediff-registry-buffer)
611 (setq ediff-session-registry
612 (cons meta-buffer ediff-session-registry)))
614 ;; redraw-function uses ediff-meta-list
615 (funcall redraw-function ediff-meta-list)
617 ;; set read-only/non-modified
618 (setq buffer-read-only t)
619 (set-buffer-modified-p nil)
621 (run-hooks 'startup-hooks)
623 ;; Arrange to show directory contents differences
624 ;; Must be after run startup-hooks, since ediff-dir-difference-list is
625 ;; set inside these hooks
626 (if (eq action-func 'ediff-filegroup-action)
627 (progn
628 ;; put meta buffer in (car ediff-dir-difference-list)
629 (setq ediff-dir-difference-list
630 (cons (cons meta-buffer (car ediff-dir-difference-list))
631 (cdr ediff-dir-difference-list)))
633 (or (ediff-one-filegroup-metajob jobname)
634 (ediff-draw-dir-diffs ediff-dir-difference-list))
635 (define-key ediff-meta-buffer-map "h" 'ediff-mark-for-hiding)
636 (define-key
637 ediff-meta-buffer-map "x" 'ediff-hide-marked-sessions)
638 (define-key ediff-meta-buffer-map "m" 'ediff-mark-for-operation)
639 (cond ((ediff-collect-diffs-metajob jobname)
640 (define-key
641 ediff-meta-buffer-map "P" 'ediff-collect-custom-diffs))
642 ((ediff-patch-metajob jobname)
643 (define-key
644 ediff-meta-buffer-map "P" 'ediff-meta-show-patch)))
645 (define-key ediff-meta-buffer-map "u" 'ediff-up-meta-hierarchy)
646 (define-key ediff-meta-buffer-map "D" 'ediff-show-dir-diffs)))
648 (if (eq ediff-metajob-name 'ediff-registry)
649 (run-hooks 'ediff-registry-setup-hook)
650 (run-hooks 'ediff-session-group-setup-hook))
651 ) ; eval in meta-buffer
652 meta-buffer))
655 ;; this is a setup function for ediff-directories
656 ;; must return meta-buffer
657 (defun ediff-redraw-directory-group-buffer (meta-list)
658 ;; extract directories
659 (let ((meta-buf (ediff-get-group-buffer meta-list))
660 (empty t)
661 (sessionNum 0)
662 regexp elt session-buf f1 f2 f3 pt
663 merge-autostore-dir
664 point tmp-list buffer-read-only)
665 (ediff-eval-in-buffer meta-buf
666 (setq point (point))
667 (erase-buffer)
668 (insert (format ediff-meta-buffer-message
669 (ediff-abbrev-jobname ediff-metajob-name)))
671 (setq regexp (ediff-get-group-regexp meta-list)
672 merge-autostore-dir (ediff-get-group-merge-autostore-dir meta-list))
674 (cond ((ediff-collect-diffs-metajob)
675 (insert
676 " `P':\tcollect custom diffs of all marked sessions\n"))
677 ((ediff-patch-metajob)
678 (insert
679 " `P':\tshow patch appropriately for the context (session or group)\n")))
680 (insert
681 " `u':\tshow parent session group\n")
682 (or (ediff-one-filegroup-metajob)
683 (insert
684 " `D':\tshow differences among directories\n"
685 " `=':\tmark identical files in each session\n\n"))
687 (if (and (stringp regexp) (> (length regexp) 0))
688 (insert
689 (format "\n*** Filter-through regular expression: %s\n" regexp)))
690 (if (and ediff-autostore-merges (ediff-merge-metajob)
691 (stringp merge-autostore-dir))
692 (insert (format
693 "\nMerges are automatically stored in directory: %s\n"
694 merge-autostore-dir)))
695 (insert "\n
696 Size Last modified Name
697 -----------------------------------------------------------------------
701 ;; discard info on directories and regexp
702 (setq meta-list (cdr meta-list)
703 tmp-list meta-list)
704 (while (and tmp-list empty)
705 (if (and (car tmp-list)
706 (not (eq (ediff-get-session-status (car tmp-list)) ?I)))
707 (setq empty nil))
708 (setq tmp-list (cdr tmp-list)))
710 (if empty
711 (insert
712 " ****** ****** This session group has no members\n"))
714 ;; now organize file names like this:
715 ;; use-mark sizeA dateA sizeB dateB filename
716 ;; make sure directories are displayed with a trailing slash.
717 (while meta-list
718 (setq elt (car meta-list)
719 meta-list (cdr meta-list)
720 sessionNum (1+ sessionNum))
721 (if (eq (ediff-get-session-status elt) ?I)
723 (setq session-buf (ediff-get-session-buffer elt)
724 f1 (ediff-get-session-objA elt)
725 f2 (ediff-get-session-objB elt)
726 f3 (ediff-get-session-objC elt))
727 (setq pt (point))
728 ;; insert markers
729 (insert (cond ((null session-buf) " ") ; virgin session
730 ((ediff-buffer-live-p session-buf) "+") ;active session
731 (t "-"))) ; finished session
732 (insert (cond ((ediff-get-session-status elt)) ; session has status,
733 ;;; e.g., ?H, ?I
734 (t " "))) ; normal session
735 (insert " Session " (int-to-string sessionNum) ":\n")
736 (ediff-meta-insert-file-info f1)
737 (ediff-meta-insert-file-info f2)
738 (ediff-meta-insert-file-info f3)
739 (ediff-set-meta-overlay pt (point) elt)))
740 (set-buffer-modified-p nil)
741 (goto-char point)
742 meta-buf)))
744 ;; Check if this is a problematic session.
745 ;; Return nil if not. Otherwise, return symbol representing the problem
746 ;; At present, problematic sessions occur only in -with-ancestor comparisons
747 ;; when the ancestor is a directory rather than a file, or when there is no
748 ;; suitable ancestor file in the ancestor directory
749 (defun ediff-problematic-session-p (session)
750 (let ((f1 (ediff-get-session-objA-name session))
751 (f2 (ediff-get-session-objB-name session))
752 (f3 (ediff-get-session-objC-name session)))
753 (cond ((and (stringp f1) (not (file-directory-p f1))
754 (stringp f2) (not (file-directory-p f2))
755 ;; either invalid file name or a directory
756 (or (not (stringp f3)) (file-directory-p f3))
757 (ediff-ancestor-metajob))
758 ;; more may be added later
759 'ancestor-is-dir)
760 (t nil))))
762 (defun ediff-meta-insert-file-info (fileinfo)
763 (let ((fname (car fileinfo))
764 (feq (ediff-get-file-eqstatus fileinfo))
765 file-modtime file-size)
767 (cond ((not (stringp fname)) (setq file-size -2)) ; file doesn't exits
768 ((not (ediff-file-remote-p fname))
769 (if (file-exists-p fname)
770 ;; set real size and modtime
771 (setq file-size (ediff-file-size fname)
772 file-modtime (ediff-file-modtime fname))
773 (setq file-size -2))) ; file doesn't exist
774 ( t (setq file-size -1))) ; remote file
775 (if (stringp fname)
776 (insert
777 (format
778 "%s %s %-20s %s\n"
779 (if feq "=" " ") ; equality indicator
780 (format "%10s" (cond ((= file-size -1) "--")
781 ((< file-size -1) "--")
782 (t file-size)))
783 (cond ((= file-size -1) "*remote file*")
784 ((< file-size -1) "*file doesn't exist*")
785 (t (ediff-format-date (decode-time file-modtime))))
787 ;; dir names in meta lists have training slashes, so we just
788 ;; abbreviate the file name, if file exists
789 (if (and (not (stringp fname)) (< file-size -1))
790 "-------" ; file doesn't exist
791 (ediff-abbreviate-file-name fname)))))))
793 (defconst ediff-months '((1 . "Jan") (2 . "Feb") (3 . "Mar") (4 . "Apr")
794 (5 . "May") (6 . "Jun") (7 . "Jul") (8 . "Aug")
795 (9 . "Sep") (10 . "Oct") (11 . "Nov") (12 . "Dec"))
796 "Months' associative array.")
798 ;; returns 2char string
799 (defsubst ediff-fill-leading-zero (num)
800 (if (< num 10)
801 (format "0%d" num)
802 (number-to-string num)))
804 ;; TIME is like the output of decode-time
805 (defun ediff-format-date (time)
806 (format "%s %2d %4d %s:%s:%s"
807 (cdr (assoc (nth 4 time) ediff-months)) ; month
808 (nth 3 time) ; day
809 (nth 5 time) ; year
810 (ediff-fill-leading-zero (nth 2 time)) ; hour
811 (ediff-fill-leading-zero (nth 1 time)) ; min
812 (ediff-fill-leading-zero (nth 0 time)) ; sec
815 (defun ediff-draw-dir-diffs (diff-list)
816 (if (null diff-list) (error "Lost difference info on these directories"))
817 (let* ((buf-name (ediff-unique-buffer-name
818 "*Ediff File Group Differences" "*"))
819 (regexp (ediff-get-group-regexp diff-list))
820 (dir1 (ediff-abbreviate-file-name (ediff-get-group-objA diff-list)))
821 (dir2 (ediff-abbreviate-file-name (ediff-get-group-objB diff-list)))
822 (dir3 (ediff-get-group-objC diff-list))
823 (dir3 (if (stringp dir3) (ediff-abbreviate-file-name dir3)))
824 (meta-buf (ediff-get-group-buffer diff-list))
825 (underline (make-string 26 ?-))
826 file code
827 buffer-read-only)
828 ;; skip the directory part
829 (setq diff-list (cdr diff-list))
830 (setq ediff-dir-diffs-buffer (get-buffer-create buf-name))
831 (ediff-eval-in-buffer ediff-dir-diffs-buffer
832 (use-local-map ediff-dir-diffs-buffer-map)
833 (erase-buffer)
834 (setq ediff-meta-buffer meta-buf)
835 (insert "\t\t*** Directory Differences ***\n")
836 (insert "
837 Useful commands:
838 `q': hide this buffer
839 SPC: next line
840 DEL: previous line\n\n")
842 (if (and (stringp regexp) (> (length regexp) 0))
843 (insert
844 (format "\n*** Filter-through regular expression: %s\n" regexp)))
845 (insert "\n")
846 (insert (format "\n%-27s%-26s"
847 (ediff-truncate-string-left
848 (ediff-abbreviate-file-name
849 (file-name-as-directory dir1))
851 (ediff-truncate-string-left
852 (ediff-abbreviate-file-name
853 (file-name-as-directory dir2))
854 25)))
855 (if dir3
856 (insert (format " %-25s\n"
857 (ediff-truncate-string-left
858 (ediff-abbreviate-file-name
859 (file-name-as-directory dir3))
860 25)))
861 (insert "\n"))
862 (insert (format "%s%s" underline underline))
863 (if (stringp dir3)
864 (insert (format "%s\n\n" underline))
865 (insert "\n\n"))
867 (if (null diff-list)
868 (insert "\n\t*** No differences ***\n"))
870 (while diff-list
871 (setq file (car (car diff-list))
872 code (cdr (car diff-list))
873 diff-list (cdr diff-list))
874 (if (= (mod code 2) 0) ; dir1
875 (insert (format "%-27s"
876 (ediff-truncate-string-left
877 (ediff-abbreviate-file-name
878 (if (file-directory-p (concat dir1 file))
879 (file-name-as-directory file)
880 file))
881 24)))
882 (insert (format "%-27s" "---")))
883 (if (= (mod code 3) 0) ; dir2
884 (insert (format "%-26s"
885 (ediff-truncate-string-left
886 (ediff-abbreviate-file-name
887 (if (file-directory-p (concat dir2 file))
888 (file-name-as-directory file)
889 file))
890 24)))
891 (insert (format "%-26s" "---")))
892 (if (stringp dir3)
893 (if (= (mod code 5) 0) ; dir3
894 (insert (format " %-25s"
895 (ediff-truncate-string-left
896 (ediff-abbreviate-file-name
897 (if (file-directory-p (concat dir3 file))
898 (file-name-as-directory file)
899 file))
900 24)))
901 (insert (format " %-25s" "---"))))
902 (insert "\n"))
903 (setq buffer-read-only t)
904 (set-buffer-modified-p nil)) ; eval in diff buffer
907 (defun ediff-bury-dir-diffs-buffer ()
908 "Bury the directory difference buffer. Display the meta buffer instead."
909 (interactive)
910 (let ((buf ediff-meta-buffer)
911 wind)
912 (bury-buffer)
913 (if (setq wind (ediff-get-visible-buffer-window buf))
914 (select-window wind)
915 (set-window-buffer (selected-window) buf))))
917 ;; executes in dir session group buffer
918 ;; show buffer differences
919 (defun ediff-show-dir-diffs ()
920 "Display differences among the directories involved in session group."
921 (interactive)
922 (if (ediff-one-filegroup-metajob)
923 (error "This command is inapplicable in the present context"))
924 (or (ediff-buffer-live-p ediff-dir-diffs-buffer)
925 (ediff-draw-dir-diffs ediff-dir-difference-list))
926 (let ((buf ediff-dir-diffs-buffer))
927 (other-window 1)
928 (set-window-buffer (selected-window) buf)
929 (goto-char (point-min))))
931 (defun ediff-up-meta-hierarchy ()
932 "Go to the parent session group buffer."
933 (interactive)
934 (if (ediff-buffer-live-p ediff-parent-meta-buffer)
935 (ediff-show-meta-buffer ediff-parent-meta-buffer)
936 (error "This session group has no parent")))
939 ;; argument is ignored
940 (defun ediff-redraw-registry-buffer (&optional ignore)
941 (ediff-eval-in-buffer ediff-registry-buffer
942 (let ((point (point))
943 elt bufAname bufBname bufCname cur-diff total-diffs pt
944 job-name meta-list registry-list buffer-read-only)
945 (erase-buffer)
946 (insert "This is a registry of all active Ediff sessions.
948 Useful commands:
949 button2, `v', RET over a session record: switch to that session
950 `M' over a session record: display the associated session group
951 `R' in any Ediff session: display session registry
952 SPC:\tnext session
953 DEL:\tprevious session
954 `E':\tbrowse Ediff on-line manual
955 `q':\tbury registry
958 \t\tActive Ediff Sessions:
959 \t\t----------------------
962 ;; purge registry list from dead buffers
963 (mapcar (function (lambda (elt)
964 (if (not (ediff-buffer-live-p elt))
965 (setq ediff-session-registry
966 (delq elt ediff-session-registry)))))
967 ediff-session-registry)
969 (if (null ediff-session-registry)
970 (insert " ******* No active Ediff sessions *******\n"))
972 (setq registry-list ediff-session-registry)
973 (while registry-list
974 (setq elt (car registry-list)
975 registry-list (cdr registry-list))
977 (if (ediff-buffer-live-p elt)
978 (if (ediff-eval-in-buffer elt
979 (setq job-name ediff-metajob-name
980 meta-list ediff-meta-list)
981 (and ediff-metajob-name
982 (not (eq ediff-metajob-name 'ediff-registry))))
983 (progn
984 (setq pt (point))
985 (insert (format " *group*\t%s: %s\n"
986 (buffer-name elt)
987 (ediff-abbrev-jobname job-name)))
988 (insert (format "\t\t %s %s %s\n"
989 (ediff-abbreviate-file-name
990 (ediff-get-group-objA meta-list))
991 (ediff-abbreviate-file-name
992 (if (stringp
993 (ediff-get-group-objB meta-list))
994 (ediff-get-group-objB meta-list)
995 ""))
996 (ediff-abbreviate-file-name
997 (if (stringp
998 (ediff-get-group-objC meta-list))
999 (ediff-get-group-objC meta-list)
1000 ""))))
1001 (ediff-set-meta-overlay pt (point) elt))
1002 (progn
1003 (ediff-eval-in-buffer elt
1004 (setq bufAname (if (ediff-buffer-live-p ediff-buffer-A)
1005 (buffer-name ediff-buffer-A)
1006 "!!!killed buffer!!!")
1007 bufBname (if (ediff-buffer-live-p ediff-buffer-B)
1008 (buffer-name ediff-buffer-B)
1009 "!!!killed buffer!!!")
1010 bufCname (cond ((not (ediff-3way-job))
1012 ((ediff-buffer-live-p ediff-buffer-C)
1013 (buffer-name ediff-buffer-C))
1014 (t "!!!killed buffer!!!")))
1015 (setq total-diffs (format "%-4d" ediff-number-of-differences)
1016 cur-diff
1017 (cond ((= ediff-current-difference -1) " _")
1018 ((= ediff-current-difference
1019 ediff-number-of-differences)
1020 " $")
1021 (t (format
1022 "%4d" (1+ ediff-current-difference))))
1023 job-name ediff-job-name))
1024 ;; back in the meta buf
1025 (setq pt (point))
1026 (insert cur-diff "/" total-diffs "\t"
1027 (buffer-name elt)
1028 (format ": %s" (ediff-abbrev-jobname job-name)))
1029 (insert
1030 "\n\t\t " bufAname " " bufBname " " bufCname "\n")
1031 (ediff-set-meta-overlay pt (point) elt))))
1032 ) ; while
1033 (set-buffer-modified-p nil)
1034 (goto-char point)
1037 ;; sets overlay around a meta record with 'ediff-meta-info property PROP
1038 (defun ediff-set-meta-overlay (b e prop)
1039 (let (overl)
1040 (setq overl (ediff-make-overlay b e))
1041 (if ediff-emacs-p
1042 (ediff-overlay-put overl 'mouse-face 'highlight)
1043 (ediff-overlay-put overl 'highlight t))
1044 (ediff-overlay-put overl 'ediff-meta-info prop)))
1046 (defun ediff-mark-for-hiding (unmark)
1047 "Mark session for hiding. With prefix arg, unmark."
1048 (interactive "P")
1049 (let* ((pos (ediff-event-point last-command-event))
1050 (meta-buf (ediff-event-buffer last-command-event))
1051 ;; ediff-get-meta-info gives error if meta-buf or pos are invalid
1052 (info (ediff-get-meta-info meta-buf pos))
1053 (session-buf (ediff-get-session-buffer info)))
1055 (if (eq (ediff-get-session-status info) ?H)
1056 (setq unmark t))
1057 (if unmark
1058 (ediff-set-session-status info nil)
1059 (if (ediff-buffer-live-p session-buf)
1060 (error "Can't hide active session, %s" (buffer-name session-buf)))
1061 (ediff-set-session-status info ?H))
1062 (or unmark
1063 (ediff-next-meta-item 1))
1064 (ediff-update-meta-buffer meta-buf)
1067 (defun ediff-mark-for-operation (unmark)
1068 "Mark session for a group operation. With prefix arg, unmark."
1069 (interactive "P")
1070 (let* ((pos (ediff-event-point last-command-event))
1071 (meta-buf (ediff-event-buffer last-command-event))
1072 ;; ediff-get-meta-info gives error if meta-buf or pos are invalid
1073 (info (ediff-get-meta-info meta-buf pos)))
1075 (if (eq (ediff-get-session-status info) ?*)
1076 (setq unmark t))
1077 (if unmark
1078 (ediff-set-session-status info nil)
1079 (ediff-set-session-status info ?*))
1080 (or unmark
1081 (ediff-next-meta-item 1))
1082 (ediff-update-meta-buffer meta-buf)
1085 (defun ediff-hide-marked-sessions (unhide)
1086 "Hide marked sessions. With prefix arg, unhide."
1087 (interactive "P")
1088 (let ((grp-buf (ediff-get-group-buffer ediff-meta-list))
1089 (meta-list (cdr ediff-meta-list))
1090 (from (if unhide ?I ?H))
1091 (to (if unhide ?H ?I))
1092 (numMarked 0)
1093 active-sessions-exist session-buf elt)
1094 (while meta-list
1095 (setq elt (car meta-list)
1096 meta-list (cdr meta-list)
1097 session-buf (ediff-get-session-buffer elt))
1099 (if (eq (ediff-get-session-status elt) from)
1100 (progn
1101 (setq numMarked (1+ numMarked))
1102 (if (and (eq to ?I) (buffer-live-p session-buf))
1103 ;; shouldn't hide active sessions
1104 (setq active-sessions-exist t)
1105 (ediff-set-session-status elt to)))))
1106 (if (> numMarked 0)
1107 (ediff-update-meta-buffer grp-buf)
1108 (beep)
1109 (if unhide
1110 (message "Nothing to reveal...")
1111 (message "Nothing to hide...")))
1112 (if active-sessions-exist
1113 (message "Note: didn't hide active sessions!"))
1116 ;; Apply OPERATION to marked sessions. Operation expects one argument of type
1117 ;; meta-list member (not the first one), i.e., a regular session description.
1118 ;; Returns number of marked sessions on which operation was performed
1119 (defun ediff-operate-on-marked-sessions (operation)
1120 (let ((grp-buf (ediff-get-group-buffer ediff-meta-list))
1121 (meta-list (cdr ediff-meta-list))
1122 (marksym ?*)
1123 (numMarked 0)
1124 (sessionNum 0)
1125 elt)
1126 (while meta-list
1127 (setq elt (car meta-list)
1128 meta-list (cdr meta-list)
1129 sessionNum (1+ sessionNum))
1130 (if (eq (ediff-get-session-status elt) marksym)
1131 (save-excursion
1132 (setq numMarked (1+ numMarked))
1133 (funcall operation elt sessionNum))))
1134 (ediff-update-meta-buffer grp-buf) ; just in case
1135 numMarked
1138 (defun ediff-append-custom-diff (session sessionNum)
1139 (or (ediff-collect-diffs-metajob)
1140 (error "Sorry, I don't do this for everyone..."))
1141 (let ((session-buf (ediff-get-session-buffer session))
1142 (meta-diff-buff ediff-meta-diff-buffer)
1143 (metajob ediff-metajob-name)
1144 tmp-buf custom-diff-buf)
1145 (if (ediff-buffer-live-p session-buf)
1146 (ediff-eval-in-buffer session-buf
1147 (if (eq ediff-control-buffer session-buf) ; individual session
1148 (progn
1149 (ediff-compute-custom-diffs-maybe)
1150 (setq custom-diff-buf ediff-custom-diff-buffer)))))
1152 (or (ediff-buffer-live-p meta-diff-buff)
1153 (error "Ediff: something wrong--no multiple diffs buffer"))
1155 (cond ((ediff-buffer-live-p custom-diff-buf)
1156 (save-excursion
1157 (set-buffer meta-diff-buff)
1158 (goto-char (point-max))
1159 (insert-buffer custom-diff-buf)
1160 (insert "\n")))
1161 ((memq metajob '(ediff-directories
1162 ediff-merge-directories
1163 ediff-merge-directories-with-ancestor))
1164 ;; get diffs by calling shell command on ediff-custom-diff-program
1165 (save-excursion
1166 (set-buffer (setq tmp-buf (get-buffer-create ediff-tmp-buffer)))
1167 (erase-buffer)
1168 (shell-command
1169 (format "%s %s %s %s"
1170 ediff-custom-diff-program ediff-custom-diff-options
1171 (ediff-get-session-objA-name session)
1172 (ediff-get-session-objB-name session))
1174 (save-excursion
1175 (set-buffer meta-diff-buff)
1176 (goto-char (point-max))
1177 (insert-buffer tmp-buf)
1178 (insert "\n")))
1180 (error "Can't make context diff for Session %d" sessionNum )))
1183 (defun ediff-collect-custom-diffs ()
1184 "Collect custom diffs of marked sessions in buffer `*Ediff Multifile Diffs*'.
1185 This operation is defined only for `ediff-directories' and
1186 `ediff-directory-revisions', since its intent is to produce
1187 multifile patches. For `ediff-directory-revisions', we insist that
1188 all marked sessions must be active."
1189 (interactive)
1190 (or (ediff-buffer-live-p ediff-meta-diff-buffer)
1191 (setq ediff-meta-diff-buffer
1192 (get-buffer-create
1193 (ediff-unique-buffer-name "*Ediff Multifile Diffs" "*"))))
1194 (ediff-eval-in-buffer ediff-meta-diff-buffer
1195 (erase-buffer))
1196 (if (> (ediff-operate-on-marked-sessions 'ediff-append-custom-diff) 0)
1197 ;; did something
1198 (display-buffer ediff-meta-diff-buffer 'not-this-window)
1199 (beep)
1200 (message "No marked sessions found")))
1202 (defun ediff-meta-show-patch ()
1203 "Show the multi-file patch associated with this group session."
1204 (interactive)
1205 (let* ((pos (ediff-event-point last-command-event))
1206 (meta-buf (ediff-event-buffer last-command-event))
1207 (info (ediff-get-meta-info meta-buf pos 'noerror))
1208 (patchbuffer ediff-meta-patchbufer))
1209 (if (ediff-buffer-live-p patchbuffer)
1210 (ediff-eval-in-buffer patchbuffer
1211 (save-restriction
1212 (if (not info)
1213 (widen)
1214 (narrow-to-region
1215 (ediff-get-session-objB-name info)
1216 (ediff-get-session-objC-name info)))
1217 (set-buffer (get-buffer-create ediff-tmp-buffer))
1218 (erase-buffer)
1219 (insert-buffer patchbuffer)
1220 (display-buffer ediff-tmp-buffer 'not-this-window)
1222 (error "The patch buffer wasn't found"))))
1225 ;; This function executes in meta buffer. It knows where event happened.
1226 (defun ediff-filegroup-action ()
1227 "Execute appropriate action for the selected session."
1228 (interactive)
1229 (let* ((pos (ediff-event-point last-command-event))
1230 (meta-buf (ediff-event-buffer last-command-event))
1231 ;; ediff-get-meta-info gives error if meta-buf or pos are invalid
1232 (info (ediff-get-meta-info meta-buf pos))
1233 merge-autostore-dir
1234 session-buf file1 file2 file3 regexp)
1236 (setq session-buf (ediff-get-session-buffer info)
1237 file1 (ediff-get-session-objA-name info)
1238 file2 (ediff-get-session-objB-name info)
1239 file3 (ediff-get-session-objC-name info))
1241 ;; make sure we don't start on hidden sessions
1242 ;; ?H means marked for hiding. ?I means invalid (hidden).
1243 (if (memq (ediff-get-session-status info) '(?I))
1244 (progn
1245 (beep)
1246 (if (y-or-n-p "This session is marked as hidden, unmark? ")
1247 (progn
1248 (ediff-set-session-status info nil)
1249 (ediff-update-meta-buffer meta-buf))
1250 (error "Aborted"))))
1252 (ediff-eval-in-buffer meta-buf
1253 (setq merge-autostore-dir
1254 (ediff-get-group-merge-autostore-dir ediff-meta-list))
1255 (goto-char pos) ; if the user clicked on session--move point there
1256 ;; First handle sessions involving directories (which are themselves
1257 ;; session groups)
1258 ;; After that handle individual sessions
1259 (cond ((and (file-directory-p file1)
1260 (stringp file2) (file-directory-p file2)
1261 (if (stringp file3) (file-directory-p file1) t))
1262 ;; do ediff/ediff-merge on subdirectories
1263 (if (ediff-buffer-live-p session-buf)
1264 (ediff-show-meta-buffer session-buf)
1265 (setq regexp (read-string "Filter through regular expression: "
1266 nil 'ediff-filtering-regexp-history))
1267 (ediff-directories-internal
1268 file1 file2 file3 regexp
1269 ediff-session-action-function
1270 ediff-metajob-name
1271 ;; make it update car info after startup
1272 (` (list (lambda ()
1273 ;; child session group should know its parent
1274 (setq ediff-parent-meta-buffer
1275 (quote (, ediff-meta-buffer)))
1276 ;; and parent will know its child
1277 (setcar (quote (, info)) ediff-meta-buffer)))))))
1279 ;; Do ediff-revision on a subdirectory
1280 ((and (ediff-one-filegroup-metajob)
1281 (ediff-revision-metajob)
1282 (file-directory-p file1))
1283 (if (ediff-buffer-live-p session-buf)
1284 (ediff-show-meta-buffer session-buf)
1285 (setq regexp (read-string "Filter through regular expression: "
1286 nil 'ediff-filtering-regexp-history))
1287 (ediff-directory-revisions-internal
1288 file1 regexp
1289 ediff-session-action-function ediff-metajob-name
1290 ;; make it update car info after startup
1291 (` (list (lambda ()
1292 ;; child session group should know its parent
1293 (setq ediff-parent-meta-buffer
1294 (quote (, ediff-meta-buffer)))
1295 ;; and parent will know its child
1296 (setcar (quote (, info)) ediff-meta-buffer)))))))
1298 ;; From here on---only individual session handlers
1300 ;; handle an individual session with a live control buffer
1301 ((ediff-buffer-live-p session-buf)
1302 (ediff-eval-in-buffer session-buf
1303 (setq ediff-mouse-pixel-position (mouse-pixel-position))
1304 (ediff-recenter 'no-rehighlight)))
1306 ((ediff-problematic-session-p info)
1307 (beep)
1308 (if (y-or-n-p
1309 "This session has no ancestor. Merge without the ancestor? ")
1310 (ediff-merge-files
1311 file1 file2
1312 ;; provide startup hooks
1313 (` (list (lambda ()
1314 (setq ediff-meta-buffer (, (current-buffer)))
1315 (setq ediff-merge-store-file
1316 (, (concat
1317 merge-autostore-dir
1318 "mrg_"
1319 (file-name-nondirectory file1))))
1320 ;; make ediff-startup pass
1321 ;; ediff-control-buffer back to the meta
1322 ;; level; see below
1323 (setcar
1324 (quote (, info)) ediff-control-buffer)))))
1325 (error "Aborted")))
1326 ((ediff-one-filegroup-metajob) ; needs 1 file arg
1327 (funcall ediff-session-action-function
1328 file1
1329 ;; provide startup hooks
1330 (` (list (lambda ()
1331 (setq ediff-meta-buffer (, (current-buffer)))
1332 (setq ediff-merge-store-file
1333 (, (concat
1334 merge-autostore-dir
1335 "mrg_"
1336 (file-name-nondirectory file1))))
1337 ;; make ediff-startup pass
1338 ;; ediff-control-buffer back to the meta
1339 ;; level; see below
1340 (setcar
1341 (quote (, info)) ediff-control-buffer))))))
1342 ((not (ediff-metajob3)) ; need 2 file args
1343 (funcall ediff-session-action-function
1344 file1 file2
1345 ;; provide startup hooks
1346 (` (list (lambda ()
1347 (setq ediff-meta-buffer (, (current-buffer)))
1348 (setq ediff-merge-store-file
1349 (, (concat
1350 merge-autostore-dir
1351 "mrg_"
1352 (file-name-nondirectory file1))))
1353 ;; make ediff-startup pass
1354 ;; ediff-control-buffer back to the meta
1355 ;; level; see below
1356 (setcar
1357 (quote (, info)) ediff-control-buffer))))))
1358 ((ediff-metajob3) ; need 3 file args
1359 (funcall ediff-session-action-function
1360 file1 file2 file3
1361 ;; arrange startup hooks
1362 (` (list (lambda ()
1363 (setq ediff-merge-store-file
1364 (, (concat
1365 merge-autostore-dir
1366 "mrg_"
1367 (file-name-nondirectory file1))))
1368 (setq ediff-meta-buffer (, (current-buffer)))
1369 ;; this arranges that ediff-startup will pass
1370 ;; the value of ediff-control-buffer back to
1371 ;; the meta level, to the record in the meta
1372 ;; list containing the information about the
1373 ;; session associated with that
1374 ;; ediff-control-buffer
1375 (setcar
1376 (quote (, info)) ediff-control-buffer))))))
1377 ) ; cond
1378 ) ; eval in meta-buf
1381 (defun ediff-registry-action ()
1382 "Switch to a selected session."
1383 (interactive)
1384 (let* ((pos (ediff-event-point last-command-event))
1385 (buf (ediff-event-buffer last-command-event))
1386 (ctl-buf (ediff-get-meta-info buf pos)))
1388 (if (ediff-buffer-live-p ctl-buf)
1389 ;; check if this is ediff-control-buffer or ediff-meta-buffer
1390 (if (ediff-eval-in-buffer ctl-buf
1391 (eq (key-binding "q") 'ediff-quit-meta-buffer))
1392 ;; it's a meta-buffer -- last action should just display it
1393 (ediff-show-meta-buffer ctl-buf)
1394 ;; it's a session buffer -- invoke go back to session
1395 (ediff-eval-in-buffer ctl-buf
1396 (setq ediff-mouse-pixel-position (mouse-pixel-position))
1397 (ediff-recenter 'no-rehighlight)))
1398 (beep)
1399 (message "You've selected a stale session --- try again")
1400 (ediff-update-registry))
1401 (ediff-eval-in-buffer buf
1402 (goto-char pos))
1406 (defun ediff-show-meta-buffer (&optional meta-buf)
1407 "Show the session group buffer."
1408 (interactive)
1409 (let (wind frame silent)
1410 (if meta-buf (setq silent t))
1412 (setq meta-buf (or meta-buf ediff-meta-buffer))
1413 (cond ((not (bufferp meta-buf))
1414 (error "This Ediff session is not part of a session group"))
1415 ((not (ediff-buffer-live-p meta-buf))
1416 (error
1417 "Can't find this session's group panel -- session itself is ok")))
1419 (ediff-cleanup-meta-buffer meta-buf)
1420 (ediff-eval-in-buffer meta-buf
1421 (save-excursion
1422 (cond ((setq wind (ediff-get-visible-buffer-window meta-buf))
1423 (or silent
1424 (message
1425 "Already showing the group panel for this session"))
1426 (set-window-buffer wind meta-buf)
1427 (select-window wind))
1428 ((window-live-p (setq wind ediff-window-C)) ;in merge--merge buf
1429 (set-window-buffer ediff-window-C meta-buf)
1430 (select-window wind))
1431 ((window-live-p (setq wind ediff-window-A))
1432 (set-window-buffer ediff-window-A meta-buf)
1433 (select-window wind))
1434 ((window-live-p (setq wind ediff-window-B))
1435 (set-window-buffer ediff-window-B meta-buf)
1436 (select-window wind))
1437 ((and
1438 (setq wind
1439 (ediff-get-visible-buffer-window ediff-registry-buffer))
1440 (ediff-window-display-p))
1441 (select-window wind)
1442 (other-window 1)
1443 (set-window-buffer (selected-window) meta-buf))
1444 (t (ediff-skip-unsuitable-frames 'ok-unsplittable)
1445 (set-window-buffer (selected-window) meta-buf)))
1447 (if (and (ediff-window-display-p)
1448 (window-live-p
1449 (setq wind (ediff-get-visible-buffer-window meta-buf))))
1450 (progn
1451 (setq frame (window-frame wind))
1452 (raise-frame frame)
1453 (ediff-reset-mouse frame)))
1454 (run-hooks 'ediff-show-session-group-hook)
1457 (defun ediff-show-meta-buff-from-registry ()
1458 "Display the session group buffer for a selected session group."
1459 (interactive)
1460 (let* ((pos (ediff-event-point last-command-event))
1461 (meta-buf (ediff-event-buffer last-command-event))
1462 (info (ediff-get-meta-info meta-buf pos))
1463 (meta-or-session-buf info))
1464 (ediff-eval-in-buffer meta-or-session-buf
1465 (ediff-show-meta-buffer))))
1467 ;;;###autoload
1468 (defun ediff-show-registry ()
1469 "Display Ediff's registry."
1470 (interactive)
1471 (ediff-update-registry)
1472 (if (not (ediff-buffer-live-p ediff-registry-buffer))
1473 (error "No active Ediff sessions or corrupted session registry"))
1474 (let (wind frame)
1475 ;; for some reason, point moves in ediff-registry-buffer, so we preserve it
1476 ;; explicitly
1477 (ediff-eval-in-buffer ediff-registry-buffer
1478 (save-excursion
1479 (cond ((setq wind
1480 (ediff-get-visible-buffer-window ediff-registry-buffer))
1481 (message "Already showing the registry")
1482 (set-window-buffer wind ediff-registry-buffer)
1483 (select-window wind))
1484 ((window-live-p ediff-window-C)
1485 (set-window-buffer ediff-window-C ediff-registry-buffer)
1486 (select-window ediff-window-C))
1487 ((window-live-p ediff-window-A)
1488 (set-window-buffer ediff-window-A ediff-registry-buffer)
1489 (select-window ediff-window-A))
1490 ((window-live-p ediff-window-B)
1491 (set-window-buffer ediff-window-B ediff-registry-buffer)
1492 (select-window ediff-window-B))
1493 ((and (setq wind
1494 (ediff-get-visible-buffer-window ediff-meta-buffer))
1495 (ediff-window-display-p))
1496 (select-window wind)
1497 (other-window 1)
1498 (set-window-buffer (selected-window) ediff-registry-buffer))
1499 (t (ediff-skip-unsuitable-frames 'ok-unsplittable)
1500 (set-window-buffer (selected-window) ediff-registry-buffer)))
1502 (if (ediff-window-display-p)
1503 (progn
1504 (setq frame
1505 (window-frame
1506 (ediff-get-visible-buffer-window ediff-registry-buffer)))
1507 (raise-frame frame)
1508 (ediff-reset-mouse frame)))
1509 (run-hooks 'ediff-show-registry-hook)
1512 ;;;###autoload
1513 (defalias 'eregistry 'ediff-show-registry)
1515 ;; If meta-buf doesn't exist, it is created. In that case, id doesn't have a
1516 ;; parent meta-buf
1517 ;; Check if META-BUF exists before calling this function
1518 (defun ediff-update-meta-buffer (meta-buf)
1519 (ediff-eval-in-buffer (current-buffer)
1520 (if (ediff-buffer-live-p meta-buf)
1521 (ediff-eval-in-buffer meta-buf
1522 (funcall ediff-meta-redraw-function ediff-meta-list))
1525 (defun ediff-update-registry ()
1526 (ediff-eval-in-buffer (current-buffer)
1527 (if (ediff-buffer-live-p ediff-registry-buffer)
1528 (ediff-redraw-registry-buffer)
1529 (ediff-prepare-meta-buffer
1530 'ediff-registry-action
1531 ediff-session-registry
1532 "*Ediff Registry"
1533 'ediff-redraw-registry-buffer
1534 'ediff-registry))
1537 ;; If meta-buf exists, it is redrawn along with parent.
1538 ;; Otherwise, nothing happens.
1539 (defun ediff-cleanup-meta-buffer (meta-buffer)
1540 (if (ediff-buffer-live-p meta-buffer)
1541 (ediff-eval-in-buffer meta-buffer
1542 (ediff-update-meta-buffer meta-buffer)
1543 (if (ediff-buffer-live-p ediff-parent-meta-buffer)
1544 (ediff-update-meta-buffer ediff-parent-meta-buffer)))))
1546 ;; t if no session in progress
1547 (defun ediff-safe-to-quit (meta-buffer)
1548 (if (ediff-buffer-live-p meta-buffer)
1549 (let ((lis ediff-meta-list)
1550 (cont t)
1551 buffer-read-only)
1552 (ediff-update-meta-buffer meta-buffer)
1553 (ediff-eval-in-buffer meta-buffer
1554 (setq lis (cdr lis)) ; discard the description part of meta-list
1555 (while (and cont lis)
1556 (if (ediff-buffer-live-p
1557 (ediff-get-group-buffer lis)) ; in progress
1558 (setq cont nil))
1559 (setq lis (cdr lis)))
1560 cont))))
1562 (defun ediff-quit-meta-buffer ()
1563 "If the group has no active session, delete the meta buffer.
1564 If no session is in progress, ask to confirm before deleting meta buffer.
1565 Otherwise, bury the meta buffer.
1566 If this is a session registry buffer then just bury it."
1567 (interactive)
1568 (let* ((buf (current-buffer))
1569 (dir-diffs-buffer ediff-dir-diffs-buffer)
1570 (meta-diff-buffer ediff-meta-diff-buffer)
1571 (parent-buf ediff-parent-meta-buffer)
1572 (dont-show-registry (eq buf ediff-registry-buffer)))
1573 (if dont-show-registry
1574 (bury-buffer)
1575 (ediff-cleanup-meta-buffer buf)
1576 (cond ((and (ediff-safe-to-quit buf)
1577 (y-or-n-p "Quit this session group? "))
1578 (run-hooks 'ediff-quit-session-group-hook)
1579 (message "")
1580 (ediff-dispose-of-meta-buffer buf))
1581 ((ediff-safe-to-quit buf)
1582 (bury-buffer))
1584 (error
1585 "This session group has active sessions---cannot exit")))
1586 (ediff-cleanup-meta-buffer parent-buf)
1587 (ediff-kill-buffer-carefully dir-diffs-buffer)
1588 (ediff-kill-buffer-carefully meta-diff-buffer)
1589 (if (ediff-buffer-live-p parent-buf)
1590 (progn
1591 (setq dont-show-registry t)
1592 (ediff-show-meta-buffer parent-buf)))
1594 (or dont-show-registry
1595 (ediff-show-registry))))
1597 (defun ediff-dispose-of-meta-buffer (buf)
1598 (setq ediff-session-registry (delq buf ediff-session-registry))
1599 (ediff-eval-in-buffer buf
1600 (if (ediff-buffer-live-p ediff-dir-diffs-buffer)
1601 (kill-buffer ediff-dir-diffs-buffer)))
1602 (kill-buffer buf))
1605 ;; Obtain information on a meta record where the user clicked or typed
1606 ;; BUF is the buffer where this happened and POINT is the position
1607 ;; If optional NOERROR arg is given, don't report error and return nil if no
1608 ;; meta info is found on line.
1609 (defun ediff-get-meta-info (buf point &optional noerror)
1610 (let (result olist tmp)
1611 (if (and point (ediff-buffer-live-p buf))
1612 (ediff-eval-in-buffer buf
1613 (if ediff-xemacs-p
1614 (setq result
1615 (if (setq tmp (extent-at point buf 'ediff-meta-info))
1616 (ediff-overlay-get tmp 'ediff-meta-info)))
1617 (setq olist (overlays-at point))
1618 (setq olist
1619 (mapcar (function (lambda (elt)
1620 (overlay-get elt 'ediff-meta-info)))
1621 olist))
1622 (while (and olist (null (car olist))
1623 (overlay-get (car olist) 'invisible))
1624 (setq olist (cdr olist)))
1625 (setq result (car olist)))))
1626 (if result
1627 result
1628 (if noerror
1630 (ediff-update-registry)
1631 (error "No session info in this line")))))
1633 ;; return location of the next meta overlay after point
1634 (defun ediff-next-meta-overlay-start (point)
1635 (if (eobp)
1636 (goto-char (point-min))
1637 (let (overl)
1638 (if ediff-xemacs-p
1639 (progn
1640 (setq overl (extent-at point (current-buffer) 'ediff-meta-info))
1641 (if overl
1642 (setq overl (next-extent overl))
1643 (setq overl (next-extent (current-buffer))))
1644 (if overl
1645 (extent-start-position overl)
1646 (point-max)))
1647 (setq overl (car (overlays-at point)))
1648 (if (and overl (overlay-get overl 'ediff-meta-info))
1649 ;; note: end of current overlay is the beginning of the next one
1650 (overlay-end overl)
1651 (next-overlay-change point))))
1654 (defun ediff-previous-meta-overlay-start (point)
1655 (if (bobp)
1656 (goto-char (point-max))
1657 (let (overl)
1658 (if ediff-xemacs-p
1659 (progn
1660 (setq overl (extent-at point (current-buffer) 'ediff-meta-info))
1661 (if overl
1662 (setq overl (previous-extent overl))
1663 (setq overl (previous-extent (current-buffer))))
1664 (if overl
1665 (extent-start-position overl)
1666 (point-min)))
1667 (setq overl (car (overlays-at point)))
1668 (if (and overl (overlay-get overl 'ediff-meta-info))
1669 (setq point (overlay-start overl)))
1670 ;; to get to the beginning of prev overlay
1671 (if (not (bobp))
1672 ;; trickery to overcome an emacs bug--doesn't always find previous
1673 ;; overlay change correctly
1674 (setq point (1- point)))
1675 (setq point (previous-overlay-change point))
1676 ;; If we are not over an overlay after subtracting 1, it means we are
1677 ;; in the description area preceding session records. In this case,
1678 ;; goto the top of the registry buffer.
1679 (or (car (overlays-at point))
1680 (setq point (point-min)))
1681 point
1682 ))))
1684 ;; this is the action invoked when the user selects a patch from the meta
1685 ;; buffer.
1686 (defun ediff-patch-file-form-meta (file &optional startup-hooks)
1687 (let* ((pos (ediff-event-point last-command-event))
1688 (meta-buf (ediff-event-buffer last-command-event))
1689 ;; ediff-get-meta-info gives error if meta-buf or pos are invalid
1690 (info (ediff-get-meta-info meta-buf pos))
1691 (meta-patchbuf ediff-meta-patchbufer)
1692 session-buf beg-marker end-marker)
1694 (if (or (file-directory-p file) (string-match "/dev/null" file))
1695 (error "`%s' is not an ordinary file" (file-name-as-directory file)))
1696 (setq session-buf (ediff-get-session-buffer info)
1697 beg-marker (ediff-get-session-objB-name info)
1698 end-marker (ediff-get-session-objC-name info))
1700 (or (ediff-buffer-live-p session-buf) ; either an active patch session
1701 (null session-buf) ; or it is a virgin session
1702 (error
1703 "Patch has been already applied to this file--cannot be repeated!"))
1705 (ediff-eval-in-buffer meta-patchbuf
1706 (save-restriction
1707 (widen)
1708 (narrow-to-region beg-marker end-marker)
1709 (ediff-patch-file-internal meta-patchbuf file startup-hooks)))))
1712 (defun ediff-meta-mark-equal-files ()
1713 "Run though the session list and mark identical files.
1714 This is used only for sessions that involve 2 or 3 files at the same time."
1715 (interactive)
1716 (let ((list (cdr ediff-meta-list))
1717 fileinfo1 fileinfo2 fileinfo3 elt)
1718 (while (setq elt (car list))
1719 (setq fileinfo1 (ediff-get-session-objA elt)
1720 fileinfo2 (ediff-get-session-objB elt)
1721 fileinfo3 (ediff-get-session-objC elt))
1722 (ediff-set-file-eqstatus fileinfo1 nil)
1723 (ediff-set-file-eqstatus fileinfo2 nil)
1724 (ediff-set-file-eqstatus fileinfo3 nil)
1726 (ediff-mark-if-equal fileinfo1 fileinfo2)
1727 (if (ediff-metajob3)
1728 (progn
1729 (ediff-mark-if-equal fileinfo1 fileinfo3)
1730 (ediff-mark-if-equal fileinfo2 fileinfo3)))
1731 (setq list (cdr list))))
1732 (ediff-update-meta-buffer (current-buffer)))
1734 ;; mark files 1 and 2 as equal, if they are.
1735 (defun ediff-mark-if-equal (fileinfo1 fileinfo2)
1736 (get-buffer-create ediff-tmp-buffer)
1737 (or (file-directory-p (car fileinfo1))
1738 (file-directory-p (car fileinfo2))
1739 (if (= (ediff-make-diff2-buffer
1740 ediff-tmp-buffer (car fileinfo1) (car fileinfo2))
1742 (progn
1743 (ediff-set-file-eqstatus fileinfo1 t)
1744 (ediff-set-file-eqstatus fileinfo2 t)))))
1748 ;;; Local Variables:
1749 ;;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
1750 ;;; eval: (put 'ediff-eval-in-buffer 'lisp-indent-hook 1)
1751 ;;; eval: (put 'ediff-eval-in-buffer 'edebug-form-spec '(form body))
1752 ;;; End:
1754 ;;; ediff-mult.el ends here