Merge changes from emacs-23 branch
[bpt/emacs.git] / lisp / vc / ediff-wind.el
CommitLineData
475f9031 1;;; ediff-wind.el --- window manipulation utilities
b578f267 2
24ac444f
GM
3;; Copyright (C) 1994, 1995, 1996, 1997, 2000, 2001, 2002, 2003, 2004,
4;; 2005, 2006, 2007, 2008, 2009, 2010
5;; Free Software Foundation, Inc.
475f9031 6
50a07e18 7;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
bd78fa1d 8;; Package: ediff
475f9031
KH
9
10;; This file is part of GNU Emacs.
11
eb3fa2cf 12;; GNU Emacs is free software: you can redistribute it and/or modify
475f9031 13;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
475f9031
KH
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
eb3fa2cf 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
475f9031 24
3afbc435
PJ
25;;; Commentary:
26
b578f267 27;;; Code:
71296446 28
475f9031 29
bbe6126c
MK
30;; Compiler pacifier
31(defvar icon-title-format)
32(defvar top-toolbar-height)
33(defvar bottom-toolbar-height)
34(defvar left-toolbar-height)
35(defvar right-toolbar-height)
36(defvar left-toolbar-width)
37(defvar right-toolbar-width)
38(defvar default-menubar)
50a07e18 39(defvar top-gutter)
bbe6126c 40(defvar frame-icon-title-format)
ddc90f39
MK
41(defvar ediff-diff-status)
42
acb93bb2
MK
43;; declare-function does not exist in XEmacs
44(eval-and-compile
45 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
46
ddc90f39 47(eval-when-compile
2d84cc27 48 (require 'ediff-util)
8480ec72 49 (require 'ediff-help))
bbe6126c
MK
50;; end pacifier
51
ddc90f39
MK
52(require 'ediff-init)
53
54;; be careful with ediff-tbar
e83d1fe8 55(if (featurep 'xemacs)
a5254f37 56 (require 'ediff-tbar)
ddc90f39
MK
57 (defun ediff-compute-toolbar-width () 0))
58
59(defgroup ediff-window nil
4491bde3 60 "Ediff window manipulation."
ddc90f39
MK
61 :prefix "ediff-"
62 :group 'ediff
63 :group 'frames)
64
475f9031 65
2d84cc27
MK
66;; Determine which window setup function to use based on current window system.
67(defun ediff-choose-window-setup-function-automatically ()
68 (if (ediff-window-display-p)
69 'ediff-setup-windows-multiframe
70 'ediff-setup-windows-plain))
71
72(defcustom ediff-window-setup-function (ediff-choose-window-setup-function-automatically)
9201cc28 73 "Function called to set up windows.
2d84cc27
MK
74Ediff provides a choice of two functions: `ediff-setup-windows-plain', for
75doing everything in one frame and `ediff-setup-windows-multiframe', which sets
76the control panel in a separate frame. By default, the appropriate function is
77chosen automatically depending on the current window system.
78However, `ediff-toggle-multiframe' can be used to toggle between the multiframe
79display and the single frame display.
80If the multiframe function detects that one of the buffers A/B is seen in some
81other frame, it will try to keep that buffer in that frame.
82
83If you don't like any of the two provided functions, write your own one.
475f9031
KH
84The basic guidelines:
85 1. It should leave the control buffer current and the control window
6628e7f8
DL
86 selected.
87 2. It should set `ediff-window-A', `ediff-window-B', `ediff-window-C',
88 and `ediff-control-window' to contain window objects that display
475f9031
KH
89 the corresponding buffers.
90 3. It should accept the following arguments:
91 buffer-A, buffer-B, buffer-C, control-buffer
92 Buffer C may not be used in jobs that compare only two buffers.
93If you plan to do something fancy, take a close look at how the two
ddc90f39 94provided functions are written."
2d84cc27 95 :type '(choice (const :tag "Multi Frame" ediff-setup-windows-multiframe)
e5184e9f
MR
96 (const :tag "Single Frame" ediff-setup-windows-plain)
97 (function :tag "Other function"))
ddc90f39 98 :group 'ediff-window)
475f9031
KH
99
100;; indicates if we are in a multiframe setup
101(ediff-defvar-local ediff-multiframe nil "")
102
103;; Share of the frame occupied by the merge window (buffer C)
104(ediff-defvar-local ediff-merge-window-share 0.45 "")
105
106;; The control window.
107(ediff-defvar-local ediff-control-window nil "")
108;; Official window for buffer A
109(ediff-defvar-local ediff-window-A nil "")
110;; Official window for buffer B
111(ediff-defvar-local ediff-window-B nil "")
112;; Official window for buffer C
113(ediff-defvar-local ediff-window-C nil "")
114;; Ediff's window configuration.
115;; Used to minimize the need to rearrange windows.
116(ediff-defvar-local ediff-window-config-saved "" "")
117
e756eb9f
MK
118;; Association between buff-type and ediff-window-*
119(defconst ediff-window-alist
120 '((A . ediff-window-A)
121 (?A . ediff-window-A)
122 (B . ediff-window-B)
123 (?B . ediff-window-B)
124 (C . ediff-window-C)
125 (?C . ediff-window-C)))
126
475f9031 127
ddc90f39 128(defcustom ediff-split-window-function 'split-window-vertically
9201cc28 129 "The function used to split the main window between buffer-A and buffer-B.
475f9031
KH
130You can set it to a horizontal split instead of the default vertical split
131by setting this variable to `split-window-horizontally'.
132You can also have your own function to do fancy splits.
133This variable has no effect when buffer-A/B are shown in different frames.
ddc90f39 134In this case, Ediff will use those frames to display these buffers."
c12b5b00
RS
135 :type '(choice
136 (const :tag "Split vertically" split-window-vertically)
137 (const :tag "Split horizontally" split-window-horizontally)
138 function)
ddc90f39 139 :group 'ediff-window)
475f9031 140
ddc90f39 141(defcustom ediff-merge-split-window-function 'split-window-horizontally
9201cc28 142 "The function used to split the main window between buffer-A and buffer-B.
475f9031
KH
143You can set it to a vertical split instead of the default horizontal split
144by setting this variable to `split-window-vertically'.
145You can also have your own function to do fancy splits.
146This variable has no effect when buffer-A/B/C are shown in different frames.
ddc90f39 147In this case, Ediff will use those frames to display these buffers."
c12b5b00
RS
148 :type '(choice
149 (const :tag "Split vertically" split-window-vertically)
150 (const :tag "Split horizontally" split-window-horizontally)
151 function)
ddc90f39 152 :group 'ediff-window)
475f9031 153
8e2075ea 154;; Definitions hidden from the compiler by compat wrappers.
acb93bb2
MK
155(declare-function ediff-display-pixel-width "ediff-init")
156(declare-function ediff-display-pixel-height "ediff-init")
8e2075ea 157
475f9031 158(defconst ediff-control-frame-parameters
6628e7f8 159 (list
4ae69eac
MK
160 '(name . "Ediff")
161 ;;'(unsplittable . t)
162 '(minibuffer . nil)
163 '(user-position . t) ; Emacs only
164 '(vertical-scroll-bars . nil) ; Emacs only
165 '(scrollbar-width . 0) ; XEmacs only
6de0156c 166 '(scrollbar-height . 0) ; XEmacs only
4ae69eac 167 '(menu-bar-lines . 0) ; Emacs only
7d027816 168 '(tool-bar-lines . 0) ; Emacs 21+ only
ec6aebe8
MK
169 '(left-fringe . 0)
170 '(right-fringe . 0)
2e955a8b 171 ;; don't lower but auto-raise
4ae69eac
MK
172 '(auto-lower . nil)
173 '(auto-raise . t)
3af0304a
MK
174 '(visibility . nil)
175 ;; make initial frame small to avoid distraction
176 '(width . 1) '(height . 1)
4ae69eac
MK
177 ;; this blocks queries from window manager as to where to put
178 ;; ediff's control frame. we put the frame outside the display,
179 ;; so the initial frame won't jump all over the screen
180 (cons 'top (if (fboundp 'ediff-display-pixel-height)
181 (1+ (ediff-display-pixel-height))
182 3000))
183 (cons 'left (if (fboundp 'ediff-display-pixel-width)
184 (1+ (ediff-display-pixel-width))
185 3000))
186 )
475f9031 187 "Frame parameters for displaying Ediff Control Panel.
3af0304a 188Used internally---not a user option.")
475f9031 189
6853a937
MK
190;; position of the mouse; used to decide whether to warp the mouse into ctl
191;; frame
192(ediff-defvar-local ediff-mouse-pixel-position nil "")
193
194;; not used for now
50893fe9 195(defvar ediff-mouse-pixel-threshold 30
6853a937
MK
196 "If the user moves mouse more than this many pixels, Ediff won't warp mouse into control window.")
197
ddc90f39 198(defcustom ediff-grab-mouse t
9201cc28 199 "If t, Ediff will always grab the mouse and put it in the control frame.
6853a937 200If 'maybe, Ediff will do it sometimes, but not after operations that require
3af0304a 201relatively long time. If nil, the mouse will be entirely user's
ddc90f39
MK
202responsibility."
203 :type 'boolean
204 :group 'ediff-window)
6853a937 205
ddc90f39 206(defcustom ediff-control-frame-position-function 'ediff-make-frame-position
475f9031
KH
207 "Function to call to determine the desired location for the control panel.
208Expects three parameters: the control buffer, the desired width and height
3af0304a 209of the control frame. It returns an association list
ddc90f39 210of the form \(\(top . <position>\) \(left . <position>\)\)"
1e70790f 211 :type 'function
ddc90f39 212 :group 'ediff-window)
475f9031 213
3af0304a 214(defcustom ediff-control-frame-upward-shift 42
9201cc28 215 "The upward shift of control frame from the top of buffer A's frame.
475f9031
KH
216Measured in pixels.
217This is used by the default control frame positioning function,
3af0304a
MK
218`ediff-make-frame-position'. This variable is provided for easy
219customization of the default control frame positioning."
ddc90f39
MK
220 :type 'integer
221 :group 'ediff-window)
475f9031 222
e83d1fe8 223(defcustom ediff-narrow-control-frame-leftward-shift (if (featurep 'xemacs) 7 3)
9201cc28 224 "The leftward shift of control frame from the right edge of buf A's frame.
475f9031
KH
225Measured in characters.
226This is used by the default control frame positioning function,
227`ediff-make-frame-position' to adjust the position of the control frame
3af0304a 228when it shows the short menu. This variable is provided for easy
ddc90f39
MK
229customization of the default."
230 :type 'integer
231 :group 'ediff-window)
475f9031 232
ddc90f39 233(defcustom ediff-wide-control-frame-rightward-shift 7
9201cc28 234 "The rightward shift of control frame from the left edge of buf A's frame.
475f9031
KH
235Measured in characters.
236This is used by the default control frame positioning function,
237`ediff-make-frame-position' to adjust the position of the control frame
3af0304a 238when it shows the full menu. This variable is provided for easy
ddc90f39
MK
239customization of the default."
240 :type 'integer
241 :group 'ediff-window)
475f9031
KH
242
243
244;; Wide frame display
245
246;; t means Ediff is using wide display
247(ediff-defvar-local ediff-wide-display-p nil "")
248;; keeps frame config for toggling wide display
6628e7f8 249(ediff-defvar-local ediff-wide-display-orig-parameters nil
475f9031
KH
250 "Frame parameters to be restored when the user wants to toggle the wide
251display off.")
252(ediff-defvar-local ediff-wide-display-frame nil
253 "Frame to be used for wide display.")
254(ediff-defvar-local ediff-make-wide-display-function 'ediff-make-wide-display
255 "The value is a function that is called to create a wide display.
3af0304a 256The function is called without arguments. It should resize the frame in
475f9031
KH
257which buffers A, B, and C are to be displayed, and it should save the old
258frame parameters in `ediff-wide-display-orig-parameters'.
259The variable `ediff-wide-display-frame' should be set to contain
260the frame used for the wide display.")
261
262;; Frame used for the control panel in a windowing system.
263(ediff-defvar-local ediff-control-frame nil "")
264
ddc90f39 265(defcustom ediff-prefer-iconified-control-frame nil
9201cc28 266 "If t, keep control panel iconified when help message is off.
475f9031 267This has effect only on a windowing system.
50893fe9 268If t, hitting `?' to toggle control panel off iconifies it.
475f9031
KH
269
270This is only useful in Emacs and only for certain kinds of window managers,
271such as TWM and its derivatives, since the window manager must permit
3af0304a 272keyboard input to go into icons. XEmacs completely ignores keyboard input
ddc90f39
MK
273into icons, regardless of the window manager."
274 :type 'boolean
275 :group 'ediff-window)
475f9031
KH
276
277;;; Functions
278
279(defun ediff-get-window-by-clicking (wind prev-wind wind-number)
280 (let (event)
281 (message
3af0304a 282 "Select windows by clicking. Please click on Window %d " wind-number)
475f9031
KH
283 (while (not (ediff-mouse-event-p (setq event (ediff-read-event))))
284 (if (sit-for 1) ; if sequence of events, wait till the final word
285 (beep 1))
286 (message "Please click on Window %d " wind-number))
287 (ediff-read-event) ; discard event
f6d49b2f
GM
288 (setq wind (if (featurep 'xemacs)
289 (event-window event)
290 (posn-window (event-start event))))))
71296446 291
475f9031 292
55d7ff38 293;; Select the lowest window on the frame.
475f9031 294(defun ediff-select-lowest-window ()
f6d49b2f 295 (if (featurep 'xemacs)
2d84cc27 296 (select-window (frame-lowest-window))
f6d49b2f
GM
297 (let* ((lowest-window (selected-window))
298 (bottom-edge (car (cdr (cdr (cdr (window-edges))))))
299 (last-window (save-excursion
300 (other-window -1) (selected-window)))
301 (window-search t))
302 (while window-search
303 (let* ((this-window (next-window))
304 (next-bottom-edge
305 (car (cdr (cdr (cdr (window-edges this-window)))))))
306 (if (< bottom-edge next-bottom-edge)
307 (setq bottom-edge next-bottom-edge
308 lowest-window this-window))
309 (select-window this-window)
310 (when (eq last-window this-window)
311 (select-window lowest-window)
312 (setq window-search nil)))))))
475f9031 313
475f9031
KH
314
315;;; Common window setup routines
316
317;; Set up the window configuration. If POS is given, set the points to
318;; the beginnings of the buffers.
319;; When 3way comparison is added, this will have to choose the appropriate
320;; setup function based on ediff-job-name
321(defun ediff-setup-windows (buffer-A buffer-B buffer-C control-buffer)
322 ;; Make sure we are not in the minibuffer window when we try to delete
323 ;; all other windows.
6853a937 324 (run-hooks 'ediff-before-setup-windows-hook)
475f9031
KH
325 (if (eq (selected-window) (minibuffer-window))
326 (other-window 1))
71296446 327
18b5607f
KH
328 ;; in case user did a no-no on a tty
329 (or (ediff-window-display-p)
330 (setq ediff-window-setup-function 'ediff-setup-windows-plain))
71296446 331
475f9031 332 (or (ediff-keep-window-config control-buffer)
6628e7f8 333 (funcall
e756eb9f 334 (ediff-with-current-buffer control-buffer ediff-window-setup-function)
475f9031 335 buffer-A buffer-B buffer-C control-buffer))
6853a937 336 (run-hooks 'ediff-after-setup-windows-hook))
475f9031
KH
337
338;; Just set up 3 windows.
339;; Usually used without windowing systems
340;; With windowing, we want to use dedicated frames.
341(defun ediff-setup-windows-plain (buffer-A buffer-B buffer-C control-buffer)
e756eb9f 342 (ediff-with-current-buffer control-buffer
475f9031
KH
343 (setq ediff-multiframe nil))
344 (if ediff-merge-job
345 (ediff-setup-windows-plain-merge
346 buffer-A buffer-B buffer-C control-buffer)
6628e7f8 347 (ediff-setup-windows-plain-compare
475f9031 348 buffer-A buffer-B buffer-C control-buffer)))
71296446 349
475f9031 350(defun ediff-setup-windows-plain-merge (buf-A buf-B buf-C control-buffer)
18b5607f
KH
351 ;; skip dedicated and unsplittable frames
352 (ediff-destroy-control-frame control-buffer)
4ae69eac 353 (let ((window-min-height 1)
6628e7f8 354 split-window-function
475f9031
KH
355 merge-window-share merge-window-lines
356 wind-A wind-B wind-C)
e756eb9f 357 (ediff-with-current-buffer control-buffer
475f9031
KH
358 (setq merge-window-share ediff-merge-window-share
359 ;; this lets us have local versions of ediff-split-window-function
360 split-window-function ediff-split-window-function))
361 (delete-other-windows)
2d84cc27 362 (set-window-dedicated-p (selected-window) nil)
475f9031
KH
363 (split-window-vertically)
364 (ediff-select-lowest-window)
365 (ediff-setup-control-buffer control-buffer)
71296446 366
475f9031 367 ;; go to the upper window and split it betw A, B, and possibly C
6628e7f8 368 (other-window 1)
475f9031
KH
369 (setq merge-window-lines
370 (max 2 (round (* (window-height) merge-window-share))))
371 (switch-to-buffer buf-A)
372 (setq wind-A (selected-window))
71296446 373
6853a937
MK
374 ;; XEmacs used to have a lot of trouble with display
375 ;; It did't set things right unless we tell it to sit still
376 ;; 19.12 seems ok.
e83d1fe8 377 ;;(if (featurep 'xemacs) (sit-for 0))
71296446 378
475f9031 379 (split-window-vertically (max 2 (- (window-height) merge-window-lines)))
6628e7f8 380 (if (eq (selected-window) wind-A)
475f9031
KH
381 (other-window 1))
382 (setq wind-C (selected-window))
383 (switch-to-buffer buf-C)
71296446 384
475f9031
KH
385 (select-window wind-A)
386 (funcall split-window-function)
71296446 387
475f9031
KH
388 (if (eq (selected-window) wind-A)
389 (other-window 1))
390 (switch-to-buffer buf-B)
391 (setq wind-B (selected-window))
71296446 392
e756eb9f 393 (ediff-with-current-buffer control-buffer
475f9031
KH
394 (setq ediff-window-A wind-A
395 ediff-window-B wind-B
396 ediff-window-C wind-C))
71296446 397
475f9031
KH
398 (ediff-select-lowest-window)
399 (ediff-setup-control-buffer control-buffer)
400 ))
401
71296446 402
475f9031
KH
403;; This function handles all comparison jobs, including 3way jobs
404(defun ediff-setup-windows-plain-compare (buf-A buf-B buf-C control-buffer)
405 ;; skip dedicated and unsplittable frames
18b5607f 406 (ediff-destroy-control-frame control-buffer)
4ae69eac 407 (let ((window-min-height 1)
475f9031
KH
408 split-window-function wind-width-or-height
409 three-way-comparison
18b5607f 410 wind-A-start wind-B-start wind-A wind-B wind-C)
e756eb9f 411 (ediff-with-current-buffer control-buffer
475f9031
KH
412 (setq wind-A-start (ediff-overlay-start
413 (ediff-get-value-according-to-buffer-type
414 'A ediff-narrow-bounds))
415 wind-B-start (ediff-overlay-start
416 (ediff-get-value-according-to-buffer-type
417 'B ediff-narrow-bounds))
475f9031
KH
418 ;; this lets us have local versions of ediff-split-window-function
419 split-window-function ediff-split-window-function
420 three-way-comparison ediff-3way-comparison-job))
42acc581
MK
421 ;; if in minibuffer go somewhere else
422 (if (save-match-data
423 (string-match "\*Minibuf-" (buffer-name (window-buffer))))
424 (select-window (next-window nil 'ignore-minibuf)))
475f9031 425 (delete-other-windows)
2d84cc27 426 (set-window-dedicated-p (selected-window) nil)
475f9031
KH
427 (split-window-vertically)
428 (ediff-select-lowest-window)
429 (ediff-setup-control-buffer control-buffer)
71296446 430
475f9031 431 ;; go to the upper window and split it betw A, B, and possibly C
6628e7f8 432 (other-window 1)
475f9031
KH
433 (switch-to-buffer buf-A)
434 (setq wind-A (selected-window))
435 (if three-way-comparison
436 (setq wind-width-or-height
437 (/ (if (eq split-window-function 'split-window-vertically)
438 (window-height wind-A)
439 (window-width wind-A))
440 3)))
71296446 441
6853a937
MK
442 ;; XEmacs used to have a lot of trouble with display
443 ;; It did't set things right unless we told it to sit still
444 ;; 19.12 seems ok.
e83d1fe8 445 ;;(if (featurep 'xemacs) (sit-for 0))
71296446 446
475f9031 447 (funcall split-window-function wind-width-or-height)
71296446 448
475f9031
KH
449 (if (eq (selected-window) wind-A)
450 (other-window 1))
451 (switch-to-buffer buf-B)
452 (setq wind-B (selected-window))
71296446 453
475f9031
KH
454 (if three-way-comparison
455 (progn
456 (funcall split-window-function) ; equally
457 (if (eq (selected-window) wind-B)
458 (other-window 1))
459 (switch-to-buffer buf-C)
460 (setq wind-C (selected-window))))
71296446 461
e756eb9f 462 (ediff-with-current-buffer control-buffer
475f9031
KH
463 (setq ediff-window-A wind-A
464 ediff-window-B wind-B
465 ediff-window-C wind-C))
71296446 466
475f9031
KH
467 ;; It is unlikely that we will want to implement 3way window comparison.
468 ;; So, only buffers A and B are used here.
18b5607f 469 (if ediff-windows-job
475f9031
KH
470 (progn
471 (set-window-start wind-A wind-A-start)
472 (set-window-start wind-B wind-B-start)))
71296446 473
475f9031
KH
474 (ediff-select-lowest-window)
475 (ediff-setup-control-buffer control-buffer)
476 ))
477
71296446 478
4ae69eac 479;; dispatch an appropriate window setup function
475f9031 480(defun ediff-setup-windows-multiframe (buf-A buf-B buf-C control-buf)
e756eb9f 481 (ediff-with-current-buffer control-buf
475f9031
KH
482 (setq ediff-multiframe t))
483 (if ediff-merge-job
484 (ediff-setup-windows-multiframe-merge buf-A buf-B buf-C control-buf)
485 (ediff-setup-windows-multiframe-compare buf-A buf-B buf-C control-buf)))
71296446 486
475f9031
KH
487(defun ediff-setup-windows-multiframe-merge (buf-A buf-B buf-C control-buf)
488;;; Algorithm:
bbe6126c
MK
489;;; 1. Never use frames that have dedicated windows in them---it is bad to
490;;; destroy dedicated windows.
491;;; 2. If A and B are in the same frame but C's frame is different--- use one
6628e7f8 492;;; frame for A and B and use a separate frame for C.
bbe6126c
MK
493;;; 3. If C's frame is non-existent, then: if the first suitable
494;;; non-dedicated frame is different from A&B's, then use it for C.
495;;; Otherwise, put A,B, and C in one frame.
496;;; 4. If buffers A, B, C are is separate frames, use them to display these
497;;; buffers.
475f9031 498
6628e7f8 499 ;; Skip dedicated or iconified frames.
18b5607f 500 ;; Unsplittable frames are taken care of later.
475f9031 501 (ediff-skip-unsuitable-frames 'ok-unsplittable)
71296446 502
4ae69eac 503 (let* ((window-min-height 1)
475f9031
KH
504 (wind-A (ediff-get-visible-buffer-window buf-A))
505 (wind-B (ediff-get-visible-buffer-window buf-B))
506 (wind-C (ediff-get-visible-buffer-window buf-C))
18b5607f
KH
507 (frame-A (if wind-A (window-frame wind-A)))
508 (frame-B (if wind-B (window-frame wind-B)))
509 (frame-C (if wind-C (window-frame wind-C)))
475f9031 510 ;; on wide display, do things in one frame
6628e7f8 511 (force-one-frame
e756eb9f 512 (ediff-with-current-buffer control-buf ediff-wide-display-p))
475f9031 513 ;; this lets us have local versions of ediff-split-window-function
6628e7f8 514 (split-window-function
e756eb9f 515 (ediff-with-current-buffer control-buf ediff-split-window-function))
475f9031 516 (orig-wind (selected-window))
18b5607f 517 (orig-frame (selected-frame))
475f9031 518 (use-same-frame (or force-one-frame
bbe6126c 519 ;; A and C must be in one frame
475f9031 520 (eq frame-A (or frame-C orig-frame))
bbe6126c 521 ;; B and C must be in one frame
475f9031 522 (eq frame-B (or frame-C orig-frame))
bbe6126c 523 ;; A or B is not visible
18b5607f
KH
524 (not (frame-live-p frame-A))
525 (not (frame-live-p frame-B))
bbe6126c
MK
526 ;; A or B is not suitable for display
527 (not (ediff-window-ok-for-display wind-A))
528 (not (ediff-window-ok-for-display wind-B))
529 ;; A and B in the same frame, and no good frame
530 ;; for C
18b5607f
KH
531 (and (eq frame-A frame-B)
532 (not (frame-live-p frame-C)))
475f9031 533 ))
bbe6126c 534 ;; use-same-frame-for-AB implies wind A and B are ok for display
475f9031
KH
535 (use-same-frame-for-AB (and (not use-same-frame)
536 (eq frame-A frame-B)))
e756eb9f 537 (merge-window-share (ediff-with-current-buffer control-buf
475f9031
KH
538 ediff-merge-window-share))
539 merge-window-lines
540 designated-minibuffer-frame
541 done-A done-B done-C)
71296446 542
475f9031
KH
543 ;; buf-A on its own
544 (if (and (window-live-p wind-A)
bbe6126c 545 (null use-same-frame) ; implies wind-A is suitable
475f9031 546 (null use-same-frame-for-AB))
bbe6126c
MK
547 (progn ; bug A on its own
548 ;; buffer buf-A is seen in live wind-A
475f9031
KH
549 (select-window wind-A)
550 (delete-other-windows)
475f9031
KH
551 (setq wind-A (selected-window))
552 (setq done-A t)))
71296446 553
475f9031 554 ;; buf-B on its own
bbe6126c
MK
555 (if (and (window-live-p wind-B)
556 (null use-same-frame) ; implies wind-B is suitable
557 (null use-same-frame-for-AB))
558 (progn ; buf B on its own
559 ;; buffer buf-B is seen in live wind-B
475f9031
KH
560 (select-window wind-B)
561 (delete-other-windows)
475f9031
KH
562 (setq wind-B (selected-window))
563 (setq done-B t)))
71296446 564
475f9031 565 ;; buf-C on its own
bbe6126c
MK
566 (if (and (window-live-p wind-C)
567 (ediff-window-ok-for-display wind-C)
568 (null use-same-frame)) ; buf C on its own
475f9031 569 (progn
bbe6126c 570 ;; buffer buf-C is seen in live wind-C
475f9031
KH
571 (select-window wind-C)
572 (delete-other-windows)
475f9031
KH
573 (setq wind-C (selected-window))
574 (setq done-C t)))
71296446 575
bbe6126c
MK
576 (if (and use-same-frame-for-AB ; implies wind A and B are suitable
577 (window-live-p wind-A))
6628e7f8 578 (progn
bbe6126c
MK
579 ;; wind-A must already be displaying buf-A
580 (select-window wind-A)
475f9031
KH
581 (delete-other-windows)
582 (setq wind-A (selected-window))
71296446 583
475f9031 584 (funcall split-window-function)
6628e7f8 585 (if (eq (selected-window) wind-A)
475f9031
KH
586 (other-window 1))
587 (switch-to-buffer buf-B)
588 (setq wind-B (selected-window))
71296446 589
475f9031
KH
590 (setq done-A t
591 done-B t)))
71296446 592
475f9031 593 (if use-same-frame
bbe6126c 594 (let ((window-min-height 1))
ddc90f39
MK
595 (if (and (eq frame-A frame-B)
596 (eq frame-B frame-C)
597 (frame-live-p frame-A))
598 (select-frame frame-A)
599 ;; avoid dedicated and non-splittable windows
600 (ediff-skip-unsuitable-frames))
475f9031
KH
601 (delete-other-windows)
602 (setq merge-window-lines
603 (max 2 (round (* (window-height) merge-window-share))))
604 (switch-to-buffer buf-A)
605 (setq wind-A (selected-window))
71296446 606
475f9031
KH
607 (split-window-vertically
608 (max 2 (- (window-height) merge-window-lines)))
6628e7f8 609 (if (eq (selected-window) wind-A)
475f9031
KH
610 (other-window 1))
611 (setq wind-C (selected-window))
612 (switch-to-buffer buf-C)
71296446 613
475f9031 614 (select-window wind-A)
71296446 615
475f9031 616 (funcall split-window-function)
6628e7f8 617 (if (eq (selected-window) wind-A)
475f9031
KH
618 (other-window 1))
619 (switch-to-buffer buf-B)
620 (setq wind-B (selected-window))
71296446 621
475f9031
KH
622 (setq done-A t
623 done-B t
624 done-C t)
625 ))
71296446 626
bbe6126c
MK
627 (or done-A ; Buf A to be set in its own frame,
628 ;;; or it was set before because use-same-frame = 1
629 (progn
630 ;; Buf-A was not set up yet as it wasn't visible,
631 ;; and use-same-frame = nil, use-same-frame-for-AB = nil
475f9031
KH
632 (select-window orig-wind)
633 (delete-other-windows)
634 (switch-to-buffer buf-A)
635 (setq wind-A (selected-window))
636 ))
bbe6126c
MK
637 (or done-B ; Buf B to be set in its own frame,
638 ;;; or it was set before because use-same-frame = 1
639 (progn
640 ;; Buf-B was not set up yet as it wasn't visible
641 ;; and use-same-frame = nil, use-same-frame-for-AB = nil
475f9031
KH
642 (select-window orig-wind)
643 (delete-other-windows)
644 (switch-to-buffer buf-B)
645 (setq wind-B (selected-window))
646 ))
71296446 647
bbe6126c
MK
648 (or done-C ; Buf C to be set in its own frame,
649 ;;; or it was set before because use-same-frame = 1
650 (progn
651 ;; Buf-C was not set up yet as it wasn't visible
652 ;; and use-same-frame = nil
475f9031
KH
653 (select-window orig-wind)
654 (delete-other-windows)
655 (switch-to-buffer buf-C)
656 (setq wind-C (selected-window))
657 ))
71296446 658
e756eb9f 659 (ediff-with-current-buffer control-buf
475f9031
KH
660 (setq ediff-window-A wind-A
661 ediff-window-B wind-B
662 ediff-window-C wind-C)
18b5607f
KH
663 (setq frame-A (window-frame ediff-window-A)
664 designated-minibuffer-frame
665 (window-frame (minibuffer-window frame-A))))
71296446 666
18b5607f 667 (ediff-setup-control-frame control-buf designated-minibuffer-frame)
475f9031
KH
668 ))
669
71296446 670
475f9031
KH
671;; Window setup for all comparison jobs, including 3way comparisons
672(defun ediff-setup-windows-multiframe-compare (buf-A buf-B buf-C control-buf)
673;;; Algorithm:
6628e7f8 674;;; If a buffer is seen in a frame, use that frame for that buffer.
475f9031
KH
675;;; If it is not seen, use the current frame.
676;;; If both buffers are not seen, they share the current frame. If one
677;;; of the buffers is not seen, it is placed in the current frame (where
3af0304a 678;;; ediff started). If that frame is displaying the other buffer, it is
475f9031
KH
679;;; shared between the two buffers.
680;;; However, if we decide to put both buffers in one frame
681;;; and the selected frame isn't splittable, we create a new frame and
682;;; put both buffers there, event if one of this buffers is visible in
683;;; another frame.
71296446 684
475f9031
KH
685 ;; Skip dedicated or iconified frames.
686 ;; Unsplittable frames are taken care of later.
687 (ediff-skip-unsuitable-frames 'ok-unsplittable)
71296446 688
4ae69eac 689 (let* ((window-min-height 1)
475f9031
KH
690 (wind-A (ediff-get-visible-buffer-window buf-A))
691 (wind-B (ediff-get-visible-buffer-window buf-B))
692 (wind-C (ediff-get-visible-buffer-window buf-C))
18b5607f
KH
693 (frame-A (if wind-A (window-frame wind-A)))
694 (frame-B (if wind-B (window-frame wind-B)))
695 (frame-C (if wind-C (window-frame wind-C)))
e756eb9f 696 (ctl-frame-exists-p (ediff-with-current-buffer control-buf
18b5607f 697 (frame-live-p ediff-control-frame)))
475f9031 698 ;; on wide display, do things in one frame
6628e7f8 699 (force-one-frame
e756eb9f 700 (ediff-with-current-buffer control-buf ediff-wide-display-p))
475f9031 701 ;; this lets us have local versions of ediff-split-window-function
6628e7f8 702 (split-window-function
e756eb9f 703 (ediff-with-current-buffer control-buf ediff-split-window-function))
475f9031 704 (three-way-comparison
e756eb9f 705 (ediff-with-current-buffer control-buf ediff-3way-comparison-job))
475f9031
KH
706 (orig-wind (selected-window))
707 (use-same-frame (or force-one-frame
708 (eq frame-A frame-B)
bbe6126c
MK
709 (not (ediff-window-ok-for-display wind-A))
710 (not (ediff-window-ok-for-display wind-B))
475f9031
KH
711 (if three-way-comparison
712 (or (eq frame-A frame-C)
713 (eq frame-B frame-C)
bbe6126c 714 (not (ediff-window-ok-for-display wind-C))
18b5607f
KH
715 (not (frame-live-p frame-A))
716 (not (frame-live-p frame-B))
717 (not (frame-live-p frame-C))))
718 (and (not (frame-live-p frame-B))
475f9031 719 (or ctl-frame-exists-p
18b5607f
KH
720 (eq frame-A (selected-frame))))
721 (and (not (frame-live-p frame-A))
475f9031 722 (or ctl-frame-exists-p
18b5607f 723 (eq frame-B (selected-frame))))))
6628e7f8 724 wind-A-start wind-B-start
475f9031
KH
725 designated-minibuffer-frame
726 done-A done-B done-C)
71296446 727
e756eb9f 728 (ediff-with-current-buffer control-buf
475f9031
KH
729 (setq wind-A-start (ediff-overlay-start
730 (ediff-get-value-according-to-buffer-type
731 'A ediff-narrow-bounds))
732 wind-B-start (ediff-overlay-start
733 (ediff-get-value-according-to-buffer-type
18b5607f 734 'B ediff-narrow-bounds))))
71296446 735
475f9031
KH
736 (if (and (window-live-p wind-A) (null use-same-frame)) ; buf-A on its own
737 (progn
bbe6126c
MK
738 ;; buffer buf-A is seen in live wind-A
739 (select-window wind-A) ; must be displaying buf-A
475f9031 740 (delete-other-windows)
475f9031
KH
741 (setq wind-A (selected-window))
742 (setq done-A t)))
71296446 743
475f9031
KH
744 (if (and (window-live-p wind-B) (null use-same-frame)) ; buf B on its own
745 (progn
bbe6126c
MK
746 ;; buffer buf-B is seen in live wind-B
747 (select-window wind-B) ; must be displaying buf-B
475f9031 748 (delete-other-windows)
475f9031
KH
749 (setq wind-B (selected-window))
750 (setq done-B t)))
71296446 751
475f9031
KH
752 (if (and (window-live-p wind-C) (null use-same-frame)) ; buf C on its own
753 (progn
bbe6126c
MK
754 ;; buffer buf-C is seen in live wind-C
755 (select-window wind-C) ; must be displaying buf-C
475f9031 756 (delete-other-windows)
475f9031
KH
757 (setq wind-C (selected-window))
758 (setq done-C t)))
71296446 759
475f9031 760 (if use-same-frame
bbe6126c 761 (let (wind-width-or-height) ; this affects 3way setups only
ddc90f39
MK
762 (if (and (eq frame-A frame-B) (frame-live-p frame-A))
763 (select-frame frame-A)
764 ;; avoid dedicated and non-splittable windows
765 (ediff-skip-unsuitable-frames))
475f9031
KH
766 (delete-other-windows)
767 (switch-to-buffer buf-A)
768 (setq wind-A (selected-window))
71296446 769
475f9031
KH
770 (if three-way-comparison
771 (setq wind-width-or-height
772 (/
773 (if (eq split-window-function 'split-window-vertically)
774 (window-height wind-A)
775 (window-width wind-A))
776 3)))
71296446 777
475f9031 778 (funcall split-window-function wind-width-or-height)
6628e7f8 779 (if (eq (selected-window) wind-A)
475f9031
KH
780 (other-window 1))
781 (switch-to-buffer buf-B)
782 (setq wind-B (selected-window))
71296446 783
475f9031
KH
784 (if three-way-comparison
785 (progn
786 (funcall split-window-function) ; equally
787 (if (memq (selected-window) (list wind-A wind-B))
788 (other-window 1))
789 (switch-to-buffer buf-C)
790 (setq wind-C (selected-window))))
791 (setq done-A t
792 done-B t
793 done-C t)
794 ))
71296446 795
475f9031 796 (or done-A ; Buf A to be set in its own frame
bbe6126c 797 ;;; or it was set before because use-same-frame = 1
6628e7f8 798 (progn
bbe6126c
MK
799 ;; Buf-A was not set up yet as it wasn't visible,
800 ;; and use-same-frame = nil
475f9031
KH
801 (select-window orig-wind)
802 (delete-other-windows)
803 (switch-to-buffer buf-A)
804 (setq wind-A (selected-window))
805 ))
806 (or done-B ; Buf B to be set in its own frame
bbe6126c 807 ;;; or it was set before because use-same-frame = 1
6628e7f8 808 (progn
bbe6126c
MK
809 ;; Buf-B was not set up yet as it wasn't visible,
810 ;; and use-same-frame = nil
475f9031
KH
811 (select-window orig-wind)
812 (delete-other-windows)
813 (switch-to-buffer buf-B)
814 (setq wind-B (selected-window))
815 ))
71296446 816
475f9031
KH
817 (if three-way-comparison
818 (or done-C ; Buf C to be set in its own frame
bbe6126c 819 ;;; or it was set before because use-same-frame = 1
6628e7f8 820 (progn
bbe6126c
MK
821 ;; Buf-C was not set up yet as it wasn't visible,
822 ;; and use-same-frame = nil
475f9031
KH
823 (select-window orig-wind)
824 (delete-other-windows)
825 (switch-to-buffer buf-C)
826 (setq wind-C (selected-window))
827 )))
71296446 828
e756eb9f 829 (ediff-with-current-buffer control-buf
475f9031
KH
830 (setq ediff-window-A wind-A
831 ediff-window-B wind-B
832 ediff-window-C wind-C)
2e955a8b 833
18b5607f
KH
834 (setq frame-A (window-frame ediff-window-A)
835 designated-minibuffer-frame
836 (window-frame (minibuffer-window frame-A))))
71296446 837
bbe6126c 838 ;; It is unlikely that we'll implement a version of ediff-windows that
3af0304a 839 ;; would compare 3 windows at once. So, we don't use buffer C here.
18b5607f 840 (if ediff-windows-job
475f9031
KH
841 (progn
842 (set-window-start wind-A wind-A-start)
843 (set-window-start wind-B wind-B-start)))
71296446 844
18b5607f 845 (ediff-setup-control-frame control-buf designated-minibuffer-frame)
475f9031
KH
846 ))
847
bbe6126c 848;; skip unsplittable frames and frames that have dedicated windows.
475f9031
KH
849;; create a new splittable frame if none is found
850(defun ediff-skip-unsuitable-frames (&optional ok-unsplittable)
18b5607f 851 (if (ediff-window-display-p)
2e955a8b
MK
852 (let ((wind-frame (window-frame (selected-window)))
853 seen-windows)
657f9cb8 854 (while (and (not (memq (selected-window) seen-windows))
475f9031 855 (or
2e955a8b
MK
856 (ediff-frame-has-dedicated-windows wind-frame)
857 (ediff-frame-iconified-p wind-frame)
3af0304a 858 ;; skip small windows
2e955a8b 859 (< (frame-height wind-frame)
3af0304a 860 (* 3 window-min-height))
475f9031
KH
861 (if ok-unsplittable
862 nil
2e955a8b
MK
863 (ediff-frame-unsplittable-p wind-frame))))
864 ;; remember history
657f9cb8 865 (setq seen-windows (cons (selected-window) seen-windows))
475f9031 866 ;; try new window
2e955a8b
MK
867 (other-window 1 t)
868 (setq wind-frame (window-frame (selected-window)))
869 )
657f9cb8 870 (if (memq (selected-window) seen-windows)
2e955a8b
MK
871 ;; fed up, no appropriate frames
872 (setq wind-frame (make-frame '((unsplittable)))))
873
874 (select-frame wind-frame)
875 )))
475f9031 876
bbe6126c 877(defun ediff-frame-has-dedicated-windows (frame)
2e955a8b 878 (let (ans)
71296446 879 (walk-windows
3af0304a
MK
880 (lambda (wind) (if (window-dedicated-p wind)
881 (setq ans t)))
bbe6126c
MK
882 'ignore-minibuffer
883 frame)
bbe6126c
MK
884 ans))
885
886;; window is ok, if it is only one window on the frame, not counting the
887;; minibuffer, or none of the frame's windows is dedicated.
888;; The idea is that it is bad to destroy dedicated windows while creating an
889;; ediff window setup
890(defun ediff-window-ok-for-display (wind)
891 (and
892 (window-live-p wind)
6628e7f8 893 (or
bbe6126c
MK
894 ;; only one window
895 (eq wind (next-window wind 'ignore-minibuffer (window-frame wind)))
2d84cc27 896 ;; none is dedicated (in multiframe setup)
bbe6126c
MK
897 (not (ediff-frame-has-dedicated-windows (window-frame wind)))
898 )))
899
475f9031 900;; Prepare or refresh control frame
18b5607f 901(defun ediff-setup-control-frame (ctl-buffer designated-minibuffer-frame)
4ae69eac 902 (let ((window-min-height 1)
475f9031 903 ctl-frame-iconified-p dont-iconify-ctl-frame deiconify-ctl-frame
6628e7f8 904 ctl-frame old-ctl-frame lines
bbe6126c 905 ;; user-grabbed-mouse
6628e7f8 906 fheight fwidth adjusted-parameters)
71296446 907
e756eb9f 908 (ediff-with-current-buffer ctl-buffer
2d84cc27
MK
909 (if (and (featurep 'xemacs) (featurep 'menubar))
910 (set-buffer-menubar nil))
6853a937
MK
911 ;;(setq user-grabbed-mouse (ediff-user-grabbed-mouse))
912 (run-hooks 'ediff-before-setup-control-frame-hook))
71296446 913
e756eb9f
MK
914 (setq old-ctl-frame (ediff-with-current-buffer ctl-buffer ediff-control-frame))
915 (ediff-with-current-buffer ctl-buffer
4ae69eac
MK
916 (setq ctl-frame (if (frame-live-p old-ctl-frame)
917 old-ctl-frame
918 (make-frame ediff-control-frame-parameters))
bbe14ff4 919 ediff-control-frame ctl-frame)
fda2ce24 920 ;; protect against undefined face-attribute
4986c2c6 921 (condition-case nil
2d84cc27
MK
922 (if (and (featurep 'emacs) (face-attribute 'mode-line :box))
923 (set-face-attribute 'mode-line ctl-frame :box nil))
f6d49b2f 924 (error)))
71296446 925
475f9031 926 (setq ctl-frame-iconified-p (ediff-frame-iconified-p ctl-frame))
18b5607f 927 (select-frame ctl-frame)
475f9031
KH
928 (if (window-dedicated-p (selected-window))
929 ()
930 (delete-other-windows)
931 (switch-to-buffer ctl-buffer))
71296446 932
475f9031 933 ;; must be before ediff-setup-control-buffer
6853a937 934 ;; just a precaution--we should be in ctl-buffer already
e756eb9f 935 (ediff-with-current-buffer ctl-buffer
6853a937
MK
936 (make-local-variable 'frame-title-format)
937 (make-local-variable 'frame-icon-title-format) ; XEmacs
938 (make-local-variable 'icon-title-format)) ; Emacs
71296446 939
475f9031
KH
940 (ediff-setup-control-buffer ctl-buffer)
941 (setq dont-iconify-ctl-frame
942 (not (string= ediff-help-message ediff-brief-help-message)))
6628e7f8 943 (setq deiconify-ctl-frame
475f9031
KH
944 (and (eq this-command 'ediff-toggle-help)
945 dont-iconify-ctl-frame))
71296446 946
475f9031 947 ;; 1 more line for the modeline
4ae69eac 948 (setq lines (1+ (count-lines (point-min) (point-max)))
475f9031 949 fheight lines
bf5d92c5
MK
950 fwidth (max (+ (ediff-help-message-line-length) 2)
951 (ediff-compute-toolbar-width))
ddc90f39
MK
952 adjusted-parameters
953 (list
954 ;; possibly change surrogate minibuffer
955 (cons 'minibuffer
956 (minibuffer-window
957 designated-minibuffer-frame))
958 (cons 'width fwidth)
59f1b058
MK
959 (cons 'height fheight)
960 (cons 'user-position t)
961 ))
2e955a8b
MK
962
963 ;; adjust autoraise
964 (setq adjusted-parameters
965 (cons (if ediff-use-long-help-message
966 '(auto-raise . nil)
967 '(auto-raise . t))
968 adjusted-parameters))
71296446 969
475f9031 970 ;; In XEmacs, buffer menubar needs to be killed before frame parameters
6628e7f8 971 ;; are changed.
2eb4bdca 972 (if (ediff-has-toolbar-support-p)
f6d49b2f
GM
973 (when (featurep 'xemacs)
974 (if (ediff-has-gutter-support-p)
975 (set-specifier top-gutter (list ctl-frame nil)))
976 (sit-for 0)
977 (set-specifier top-toolbar-height (list ctl-frame 0))
978 ;;(set-specifier bottom-toolbar-height (list ctl-frame 0))
979 (set-specifier left-toolbar-width (list ctl-frame 0))
980 (set-specifier right-toolbar-width (list ctl-frame 0))))
71296446 981
24ac444f
GM
982 ;; As a precaution, we call modify frame parameters twice, in
983 ;; order to make sure that at least once we do it for
984 ;; a non-iconified frame. (It appears that in the Windows port of
985 ;; Emacs, one can't modify frame parameters of iconified frames.)
986 (if (eq system-type 'windows-nt)
18b5607f 987 (modify-frame-parameters ctl-frame adjusted-parameters))
71296446 988
ddc90f39
MK
989 ;; make or zap toolbar (if not requested)
990 (ediff-make-bottom-toolbar ctl-frame)
71296446 991
ddc90f39
MK
992 (goto-char (point-min))
993
6853a937 994 (modify-frame-parameters ctl-frame adjusted-parameters)
bbe6126c 995 (make-frame-visible ctl-frame)
71296446 996
3af0304a 997 ;; This works around a bug in 19.25 and earlier. There, if frame gets
475f9031
KH
998 ;; iconified, the current buffer changes to that of the frame that
999 ;; becomes exposed as a result of this iconification.
1000 ;; So, we make sure the current buffer doesn't change.
18b5607f 1001 (select-frame ctl-frame)
475f9031 1002 (ediff-refresh-control-frame)
71296446 1003
6853a937
MK
1004 (cond ((and ediff-prefer-iconified-control-frame
1005 (not ctl-frame-iconified-p) (not dont-iconify-ctl-frame))
1006 (iconify-frame ctl-frame))
1007 ((or deiconify-ctl-frame (not ctl-frame-iconified-p))
1008 (raise-frame ctl-frame)))
71296446 1009
bbe6126c 1010 (set-window-dedicated-p (selected-window) t)
ddc90f39 1011
3af0304a 1012 ;; Now move the frame. We must do it separately due to an obscure bug in
ddc90f39
MK
1013 ;; XEmacs
1014 (modify-frame-parameters
1015 ctl-frame
1016 (funcall ediff-control-frame-position-function ctl-buffer fwidth fheight))
71296446 1017
bbe6126c 1018 ;; synchronize so the cursor will move to control frame
6853a937 1019 ;; per RMS suggestion
4ae69eac
MK
1020 (if (ediff-window-display-p)
1021 (let ((count 7))
1022 (sit-for .1)
1023 (while (and (not (frame-visible-p ctl-frame)) (> count 0))
1024 (setq count (1- count))
1025 (sit-for .3))))
6853a937 1026
475f9031 1027 (or (ediff-frame-iconified-p ctl-frame)
6853a937 1028 ;; don't warp the mouse, unless ediff-grab-mouse = t
4ae69eac
MK
1029 (ediff-reset-mouse ctl-frame
1030 (or (eq this-command 'ediff-quit)
1031 (not (eq ediff-grab-mouse t)))))
71296446 1032
f6d49b2f
GM
1033 (when (featurep 'xemacs)
1034 (ediff-with-current-buffer ctl-buffer
1035 (make-local-hook 'select-frame-hook)
1036 (add-hook 'select-frame-hook
1037 'ediff-xemacs-select-frame-hook nil 'local)))
71296446 1038
e756eb9f 1039 (ediff-with-current-buffer ctl-buffer
f6d49b2f 1040 (run-hooks 'ediff-after-setup-control-frame-hook))))
ddc90f39 1041
71296446 1042
18b5607f 1043(defun ediff-destroy-control-frame (ctl-buffer)
e756eb9f 1044 (ediff-with-current-buffer ctl-buffer
18b5607f
KH
1045 (if (and (ediff-window-display-p) (frame-live-p ediff-control-frame))
1046 (let ((ctl-frame ediff-control-frame))
2d84cc27
MK
1047 (if (and (featurep 'xemacs) (featurep 'menubar))
1048 (set-buffer-menubar default-menubar))
18b5607f 1049 (setq ediff-control-frame nil)
f6d49b2f 1050 (delete-frame ctl-frame))))
2d84cc27
MK
1051 (if ediff-multiframe
1052 (ediff-skip-unsuitable-frames))
6853a937
MK
1053 ;;(ediff-reset-mouse nil)
1054 )
71296446 1055
475f9031
KH
1056
1057;; finds a good place to clip control frame
1058(defun ediff-make-frame-position (ctl-buffer ctl-frame-width ctl-frame-height)
e756eb9f 1059 (ediff-with-current-buffer ctl-buffer
18b5607f
KH
1060 (let* ((frame-A (window-frame ediff-window-A))
1061 (frame-A-parameters (frame-parameters frame-A))
6853a937
MK
1062 (frame-A-top (eval (cdr (assoc 'top frame-A-parameters))))
1063 (frame-A-left (eval (cdr (assoc 'left frame-A-parameters))))
18b5607f 1064 (frame-A-width (frame-width frame-A))
475f9031
KH
1065 (ctl-frame ediff-control-frame)
1066 horizontal-adjustment upward-adjustment
6628e7f8 1067 ctl-frame-top ctl-frame-left)
71296446 1068
475f9031 1069 ;; Multiple control frames are clipped based on the value of
3af0304a 1070 ;; ediff-control-buffer-number. This is done in order not to obscure
475f9031
KH
1071 ;; other active control panels.
1072 (setq horizontal-adjustment (* 2 ediff-control-buffer-number)
1073 upward-adjustment (* -14 ediff-control-buffer-number))
1074
6853a937
MK
1075 (setq ctl-frame-top
1076 (- frame-A-top upward-adjustment ediff-control-frame-upward-shift)
1077 ctl-frame-left
1078 (+ frame-A-left
4ae69eac 1079 (if ediff-use-long-help-message
6853a937
MK
1080 (* (ediff-frame-char-width ctl-frame)
1081 (+ ediff-wide-control-frame-rightward-shift
1082 horizontal-adjustment))
1083 (- (* frame-A-width (ediff-frame-char-width frame-A))
1084 (* (ediff-frame-char-width ctl-frame)
1085 (+ ctl-frame-width
1086 ediff-narrow-control-frame-leftward-shift
1087 horizontal-adjustment))))))
6853a937
MK
1088 (setq ctl-frame-top
1089 (min ctl-frame-top
1090 (- (ediff-display-pixel-height)
1091 (* 2 ctl-frame-height
1092 (ediff-frame-char-height ctl-frame))))
1093 ctl-frame-left
1094 (min ctl-frame-left
1095 (- (ediff-display-pixel-width)
1096 (* ctl-frame-width (ediff-frame-char-width ctl-frame)))))
4ae69eac
MK
1097 ;; keep ctl frame within the visible bounds
1098 (setq ctl-frame-top (max ctl-frame-top 1)
1099 ctl-frame-left (max ctl-frame-left 1))
71296446 1100
6853a937
MK
1101 (list (cons 'top ctl-frame-top)
1102 (cons 'left ctl-frame-left))
1103 )))
71296446 1104
18b5607f 1105(defun ediff-xemacs-select-frame-hook ()
6853a937 1106 (if (and (equal (selected-frame) ediff-control-frame)
4ae69eac 1107 (not ediff-use-long-help-message))
18b5607f 1108 (raise-frame ediff-control-frame)))
71296446 1109
475f9031
KH
1110(defun ediff-make-wide-display ()
1111 "Construct an alist of parameters for the wide display.
1112Saves the old frame parameters in `ediff-wide-display-orig-parameters'.
1113The frame to be resized is kept in `ediff-wide-display-frame'.
1114This function modifies only the left margin and the width of the display.
1115It assumes that it is called from within the control buffer."
18b5607f 1116 (if (not (fboundp 'ediff-display-pixel-width))
6628e7f8 1117 (error "Can't determine display width"))
18b5607f
KH
1118 (let* ((frame-A (window-frame ediff-window-A))
1119 (frame-A-params (frame-parameters frame-A))
475f9031 1120 (cw (ediff-frame-char-width frame-A))
18b5607f 1121 (wd (- (/ (ediff-display-pixel-width) cw) 5)))
6628e7f8 1122 (setq ediff-wide-display-orig-parameters
6853a937 1123 (list (cons 'left (max 0 (eval (cdr (assoc 'left frame-A-params)))))
475f9031
KH
1124 (cons 'width (cdr (assoc 'width frame-A-params))))
1125 ediff-wide-display-frame frame-A)
59f1b058 1126 (modify-frame-parameters
23e41d15 1127 frame-A `((left . ,cw) (width . ,wd) (user-position . t)))))
71296446 1128
475f9031
KH
1129
1130;; Revise the mode line to display which difference we have selected
1131;; Also resets modelines of buffers A/B, since they may be clobbered by
1132;; anothe invocations of Ediff.
1133(defun ediff-refresh-mode-lines ()
1134 (let (buf-A-state-diff buf-B-state-diff buf-C-state-diff buf-C-state-merge)
71296446 1135
475f9031
KH
1136 (if (ediff-valid-difference-p)
1137 (setq
1138 buf-C-state-diff (ediff-get-state-of-diff ediff-current-difference 'C)
1139 buf-C-state-merge (ediff-get-state-of-merge ediff-current-difference)
1140 buf-A-state-diff (ediff-get-state-of-diff ediff-current-difference 'A)
1141 buf-B-state-diff (ediff-get-state-of-diff ediff-current-difference 'B)
1142 buf-A-state-diff (if buf-A-state-diff
1143 (format "[%s] " buf-A-state-diff)
1144 "")
1145 buf-B-state-diff (if buf-B-state-diff
1146 (format "[%s] " buf-B-state-diff)
1147 "")
1148 buf-C-state-diff (if (and (ediff-buffer-live-p ediff-buffer-C)
1149 (or buf-C-state-diff buf-C-state-merge))
6853a937 1150 (format "[%s%s%s] "
475f9031
KH
1151 (or buf-C-state-diff "")
1152 (if buf-C-state-merge
1153 (concat " " buf-C-state-merge)
6853a937 1154 "")
6628e7f8 1155 (if (ediff-get-state-of-ancestor
6853a937
MK
1156 ediff-current-difference)
1157 " AncestorEmpty"
1158 "")
1159 )
475f9031
KH
1160 ""))
1161 (setq buf-A-state-diff ""
1162 buf-B-state-diff ""
1163 buf-C-state-diff ""))
71296446 1164
475f9031
KH
1165 ;; control buffer format
1166 (setq mode-line-format
ddc90f39
MK
1167 (if (ediff-narrow-control-frame-p)
1168 (list " " mode-line-buffer-identification)
1169 (list "-- " mode-line-buffer-identification " Quick Help")))
475f9031 1170 ;; control buffer id
6628e7f8 1171 (setq mode-line-buffer-identification
475f9031
KH
1172 (if (ediff-narrow-control-frame-p)
1173 (ediff-make-narrow-control-buffer-id 'skip-name)
1174 (ediff-make-wide-control-buffer-id)))
1175 ;; Force mode-line redisplay
1176 (force-mode-line-update)
71296446 1177
18b5607f 1178 (if (and (ediff-window-display-p) (frame-live-p ediff-control-frame))
475f9031 1179 (ediff-refresh-control-frame))
71296446 1180
e756eb9f 1181 (ediff-with-current-buffer ediff-buffer-A
475f9031
KH
1182 (setq ediff-diff-status buf-A-state-diff)
1183 (ediff-strip-mode-line-format)
1184 (setq mode-line-format
1185 (list " A: " 'ediff-diff-status mode-line-format))
1186 (force-mode-line-update))
e756eb9f 1187 (ediff-with-current-buffer ediff-buffer-B
475f9031
KH
1188 (setq ediff-diff-status buf-B-state-diff)
1189 (ediff-strip-mode-line-format)
1190 (setq mode-line-format
1191 (list " B: " 'ediff-diff-status mode-line-format))
1192 (force-mode-line-update))
1193 (if ediff-3way-job
e756eb9f 1194 (ediff-with-current-buffer ediff-buffer-C
475f9031
KH
1195 (setq ediff-diff-status buf-C-state-diff)
1196 (ediff-strip-mode-line-format)
1197 (setq mode-line-format
1198 (list " C: " 'ediff-diff-status mode-line-format))
1199 (force-mode-line-update)))
6853a937 1200 (if (ediff-buffer-live-p ediff-ancestor-buffer)
e756eb9f 1201 (ediff-with-current-buffer ediff-ancestor-buffer
6853a937
MK
1202 (ediff-strip-mode-line-format)
1203 ;; we keep the second dummy string in the mode line format of the
1204 ;; ancestor, since for other buffers Ediff prepends 2 strings and
1205 ;; ediff-strip-mode-line-format expects that.
1206 (setq mode-line-format
1207 (list " Ancestor: "
1208 (cond ((not (stringp buf-C-state-merge))
1209 "")
1210 ((string-match "prefer-A" buf-C-state-merge)
1211 "[=diff(B)] ")
1212 ((string-match "prefer-B" buf-C-state-merge)
1213 "[=diff(A)] ")
1214 (t ""))
1215 mode-line-format))))
475f9031 1216 ))
71296446
JB
1217
1218
475f9031 1219(defun ediff-refresh-control-frame ()
e83d1fe8 1220 (if (featurep 'emacs)
bbe6126c 1221 ;; set frame/icon titles for Emacs
6853a937
MK
1222 (modify-frame-parameters
1223 ediff-control-frame
bbe6126c
MK
1224 (list (cons 'title (ediff-make-base-title))
1225 (cons 'icon-name (ediff-make-narrow-control-buffer-id))
1226 ))
1227 ;; set frame/icon titles for XEmacs
1228 (setq frame-title-format (ediff-make-base-title)
1229 frame-icon-title-format (ediff-make-narrow-control-buffer-id))
6853a937
MK
1230 ;; force an update of the frame title
1231 (modify-frame-parameters ediff-control-frame '(()))))
71296446
JB
1232
1233
475f9031
KH
1234(defun ediff-make-narrow-control-buffer-id (&optional skip-name)
1235 (concat
1236 (if skip-name
1237 " "
bbe6126c 1238 (ediff-make-base-title))
6628e7f8 1239 (cond ((< ediff-current-difference 0)
475f9031
KH
1240 (format " _/%d" ediff-number-of-differences))
1241 ((>= ediff-current-difference ediff-number-of-differences)
1242 (format " $/%d" ediff-number-of-differences))
1243 (t
1244 (format " %d/%d"
1245 (1+ ediff-current-difference)
1246 ediff-number-of-differences)))))
bbe6126c
MK
1247
1248(defun ediff-make-base-title ()
1249 (concat
1250 (cdr (assoc 'name ediff-control-frame-parameters))
1251 ediff-control-buffer-suffix))
71296446 1252
475f9031
KH
1253(defun ediff-make-wide-control-buffer-id ()
1254 (cond ((< ediff-current-difference 0)
1255 (list (format "%%b At start of %d diffs"
1256 ediff-number-of-differences)))
1257 ((>= ediff-current-difference ediff-number-of-differences)
1258 (list (format "%%b At end of %d diffs"
1259 ediff-number-of-differences)))
1260 (t
1261 (list (format "%%b diff %d of %d"
1262 (1+ ediff-current-difference)
1263 ediff-number-of-differences)))))
1264
1265
1266
1267;; If buff is not live, return nil
1268(defun ediff-get-visible-buffer-window (buff)
1269 (if (ediff-buffer-live-p buff)
e83d1fe8 1270 (if (featurep 'xemacs)
475f9031
KH
1271 (get-buffer-window buff t)
1272 (get-buffer-window buff 'visible))))
71296446 1273
475f9031 1274
bbe6126c 1275;;; Functions to decide when to redraw windows
71296446 1276
475f9031
KH
1277(defun ediff-keep-window-config (control-buf)
1278 (and (eq control-buf (current-buffer))
1279 (/= (buffer-size) 0)
e756eb9f 1280 (ediff-with-current-buffer control-buf
475f9031
KH
1281 (let ((ctl-wind ediff-control-window)
1282 (A-wind ediff-window-A)
1283 (B-wind ediff-window-B)
1284 (C-wind ediff-window-C))
71296446 1285
475f9031
KH
1286 (and
1287 (ediff-window-visible-p A-wind)
1288 (ediff-window-visible-p B-wind)
1289 ;; if buffer C is defined then take it into account
1290 (or (not ediff-3way-job)
1291 (ediff-window-visible-p C-wind))
1292 (eq (window-buffer A-wind) ediff-buffer-A)
1293 (eq (window-buffer B-wind) ediff-buffer-B)
1294 (or (not ediff-3way-job)
1295 (eq (window-buffer C-wind) ediff-buffer-C))
1296 (string= ediff-window-config-saved
18b5607f 1297 (format "%S%S%S%S%S%S%S"
475f9031 1298 ctl-wind A-wind B-wind C-wind
18b5607f
KH
1299 ediff-split-window-function
1300 (ediff-multiframe-setup-p)
1301 ediff-wide-display-p)))))))
475f9031
KH
1302
1303
b6178721
MK
1304(provide 'ediff-wind)
1305
1306
fa043571
SM
1307;; Local Variables:
1308;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
1309;; eval: (put 'ediff-with-current-buffer 'lisp-indent-hook 1)
1310;; eval: (put 'ediff-with-current-buffer 'edebug-form-spec '(form body))
1311;; End:
bbe6126c 1312
475f9031 1313;;; ediff-wind.el ends here