(command-line): Extract arg value properly for -u.
[bpt/emacs.git] / lisp / ediff-wind.el
1 ;;; ediff-wind.el --- window manipulation utilities
2 ;;; Copyright (C) 1994, 1995 Free Software Foundation, Inc.
3
4 ;; Author: Michael Kifer <kifer@cs.sunysb.edu>
5
6 ;; This file is part of GNU Emacs.
7
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
12
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to
20 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21
22
23 (require 'ediff-init)
24
25
26 (defvar ediff-window-setup-function (if (ediff-window-display-p)
27 'ediff-setup-windows-multiframe
28 'ediff-setup-windows-plain)
29 "*Function called to set up windows.
30 Ediff provides a choice of two functions: ediff-setup-windows-plain, for
31 doing everything in one frame, and ediff-setup-windows-multiframe,
32 which sets the control panel in a separate frame. Also, if the latter
33 function detects that one of the buffers A/B is seen in some other frame,
34 it will try to keep that buffer in that frame.
35
36 If you don't like the two functions provided---write your own one.
37 The basic guidelines:
38 1. It should leave the control buffer current and the control window
39 selected.
40 2. It should set ediff-window-A, ediff-window-B, ediff-window-C,
41 and ediff-control-window to contain window objects that display
42 the corresponding buffers.
43 3. It should accept the following arguments:
44 buffer-A, buffer-B, buffer-C, control-buffer
45 Buffer C may not be used in jobs that compare only two buffers.
46 If you plan to do something fancy, take a close look at how the two
47 provided functions are written.")
48
49 ;; indicates if we are in a multiframe setup
50 (ediff-defvar-local ediff-multiframe nil "")
51
52 ;; Share of the frame occupied by the merge window (buffer C)
53 (ediff-defvar-local ediff-merge-window-share 0.45 "")
54
55 ;; The control window.
56 (ediff-defvar-local ediff-control-window nil "")
57 ;; Official window for buffer A
58 (ediff-defvar-local ediff-window-A nil "")
59 ;; Official window for buffer B
60 (ediff-defvar-local ediff-window-B nil "")
61 ;; Official window for buffer C
62 (ediff-defvar-local ediff-window-C nil "")
63 ;; Ediff's window configuration.
64 ;; Used to minimize the need to rearrange windows.
65 (ediff-defvar-local ediff-window-config-saved "" "")
66
67
68 (defvar ediff-split-window-function 'split-window-vertically
69 "*The function used to split the main window between buffer-A and buffer-B.
70 You can set it to a horizontal split instead of the default vertical split
71 by setting this variable to `split-window-horizontally'.
72 You can also have your own function to do fancy splits.
73 This variable has no effect when buffer-A/B are shown in different frames.
74 In this case, Ediff will use those frames to display these buffers.")
75
76 (defvar ediff-merge-split-window-function 'split-window-horizontally
77 "*The function used to split the main window between buffer-A and buffer-B.
78 You can set it to a vertical split instead of the default horizontal split
79 by setting this variable to `split-window-vertically'.
80 You can also have your own function to do fancy splits.
81 This variable has no effect when buffer-A/B/C are shown in different frames.
82 In this case, Ediff will use those frames to display these buffers.")
83
84 (defconst ediff-control-frame-parameters
85 (if (ediff-window-display-p)
86 (list
87 '(name . "Ediff")
88 ;;'(unsplittable . t)
89 '(minibuffer . nil)
90 '(vertical-scroll-bars . nil) ; Emacs only
91 '(scrollbar-width . 0) ; XEmacs only
92 '(menu-bar-lines . 0) ; Emacs only
93 ;; don't lower and auto-raise
94 '(auto-lower . nil)
95 '(auto-raise . t)
96 ;; this blocks queries from window manager as to where to put
97 ;; ediff's control frame. we put the frame outside the display,
98 ;; so the initial frame won't jump all over the screen
99 (cons 'top (if (fboundp 'ediff-display-pixel-height)
100 (1+ (ediff-display-pixel-height))
101 3000))
102 (cons 'left (if (fboundp 'ediff-display-pixel-width)
103 (1+ (ediff-display-pixel-width))
104 3000))
105 ))
106 "Frame parameters for displaying Ediff Control Panel.
107 Do not specify width and height here. These are computed automatically.")
108
109 (defvar ediff-control-frame-position-function 'ediff-make-frame-position
110 "Function to call to determine the desired location for the control panel.
111 Expects three parameters: the control buffer, the desired width and height
112 of the control frame. It returns an association list
113 of the form \(\(top . <position>\) \(left . <position>\)\)")
114
115 (defvar ediff-control-frame-upward-shift (if ediff-xemacs-p 36 4)
116 "*The upward shift of control frame from the top of buffer A's frame.
117 Measured in pixels.
118 This is used by the default control frame positioning function,
119 `ediff-make-frame-position'. This variable is provided for easy
120 customization of the default.")
121
122 (defvar ediff-narrow-control-frame-leftward-shift (if ediff-xemacs-p 7 3)
123 "*The leftward shift of control frame from the right edge of buf A's frame.
124 Measured in characters.
125 This is used by the default control frame positioning function,
126 `ediff-make-frame-position' to adjust the position of the control frame
127 when it shows the short menu. This variable is provided for easy
128 customization of the default.")
129
130 (defvar ediff-wide-control-frame-rightward-shift 7
131 "*The rightward shift of control frame from the left edge of buf A's frame.
132 Measured in characters.
133 This is used by the default control frame positioning function,
134 `ediff-make-frame-position' to adjust the position of the control frame
135 when it shows the full menu. This variable is provided for easy
136 customization of the default.")
137
138
139 ;; Wide frame display
140
141 ;; t means Ediff is using wide display
142 (ediff-defvar-local ediff-wide-display-p nil "")
143 ;; keeps frame config for toggling wide display
144 (ediff-defvar-local ediff-wide-display-orig-parameters nil
145 "Frame parameters to be restored when the user wants to toggle the wide
146 display off.")
147 (ediff-defvar-local ediff-wide-display-frame nil
148 "Frame to be used for wide display.")
149 (ediff-defvar-local ediff-make-wide-display-function 'ediff-make-wide-display
150 "The value is a function that is called to create a wide display.
151 The function is called without arguments. It should resize the frame in
152 which buffers A, B, and C are to be displayed, and it should save the old
153 frame parameters in `ediff-wide-display-orig-parameters'.
154 The variable `ediff-wide-display-frame' should be set to contain
155 the frame used for the wide display.")
156
157 ;; Frame used for the control panel in a windowing system.
158 (ediff-defvar-local ediff-control-frame nil "")
159
160 (defvar ediff-prefer-iconified-control-frame nil
161 "*If t, keep control panel iconified when help message is off.
162 This has effect only on a windowing system.
163 If t, hiting `?' to toggle control panel off iconifies it.
164
165 This is only useful in Emacs and only for certain kinds of window managers,
166 such as TWM and its derivatives, since the window manager must permit
167 keyboard input to go into icons. XEmacs completely ignores keyboard input
168 into icons, regardless of the window manager.")
169
170 ;;; Functions
171
172 (defun ediff-get-window-by-clicking (wind prev-wind wind-number)
173 (let (event)
174 (message
175 "Select windows by clicking. Please click on Window %d " wind-number)
176 (while (not (ediff-mouse-event-p (setq event (ediff-read-event))))
177 (if (sit-for 1) ; if sequence of events, wait till the final word
178 (beep 1))
179 (message "Please click on Window %d " wind-number))
180 (ediff-read-event) ; discard event
181 (setq wind (if ediff-xemacs-p
182 (event-window event)
183 (posn-window (event-start event))))
184 ))
185
186
187 ;; Select the lowest window on the frame.
188 (defun ediff-select-lowest-window ()
189 (if ediff-xemacs-p
190 (select-window (frame-lowest-window))
191 (let* ((lowest-window (selected-window))
192 (bottom-edge (car (cdr (cdr (cdr (window-edges))))))
193 (last-window (save-excursion
194 (other-window -1) (selected-window)))
195 (window-search t))
196 (while window-search
197 (let* ((this-window (next-window))
198 (next-bottom-edge
199 (car (cdr (cdr (cdr (window-edges this-window)))))))
200 (if (< bottom-edge next-bottom-edge)
201 (progn
202 (setq bottom-edge next-bottom-edge)
203 (setq lowest-window this-window)))
204
205 (select-window this-window)
206 (if (eq last-window this-window)
207 (progn
208 (select-window lowest-window)
209 (setq window-search nil))))))))
210
211
212 ;;; Common window setup routines
213
214 ;; Set up the window configuration. If POS is given, set the points to
215 ;; the beginnings of the buffers.
216 ;; When 3way comparison is added, this will have to choose the appropriate
217 ;; setup function based on ediff-job-name
218 (defun ediff-setup-windows (buffer-A buffer-B buffer-C control-buffer)
219 ;; Make sure we are not in the minibuffer window when we try to delete
220 ;; all other windows.
221 (run-hooks 'ediff-before-setup-windows-hooks)
222 (if (eq (selected-window) (minibuffer-window))
223 (other-window 1))
224
225 ;; in case user did a no-no on a tty
226 (or (ediff-window-display-p)
227 (setq ediff-window-setup-function 'ediff-setup-windows-plain))
228
229 (or (ediff-keep-window-config control-buffer)
230 (funcall
231 (ediff-eval-in-buffer control-buffer ediff-window-setup-function)
232 buffer-A buffer-B buffer-C control-buffer))
233 (run-hooks 'ediff-after-setup-windows-hooks))
234
235 ;; Just set up 3 windows.
236 ;; Usually used without windowing systems
237 ;; With windowing, we want to use dedicated frames.
238 (defun ediff-setup-windows-plain (buffer-A buffer-B buffer-C control-buffer)
239 (ediff-eval-in-buffer control-buffer
240 (setq ediff-multiframe nil))
241 (if ediff-merge-job
242 (ediff-setup-windows-plain-merge
243 buffer-A buffer-B buffer-C control-buffer)
244 (ediff-setup-windows-plain-compare
245 buffer-A buffer-B buffer-C control-buffer)))
246
247 (defun ediff-setup-windows-plain-merge (buf-A buf-B buf-C control-buffer)
248 ;; skip dedicated and unsplittable frames
249 (ediff-destroy-control-frame control-buffer)
250 (let ((window-min-height 2)
251 split-window-function
252 merge-window-share merge-window-lines
253 wind-A wind-B wind-C)
254 (ediff-eval-in-buffer control-buffer
255 (setq merge-window-share ediff-merge-window-share
256 ;; this lets us have local versions of ediff-split-window-function
257 split-window-function ediff-split-window-function))
258 (delete-other-windows)
259 (split-window-vertically)
260 (ediff-select-lowest-window)
261 (ediff-setup-control-buffer control-buffer)
262
263 ;; go to the upper window and split it betw A, B, and possibly C
264 (other-window 1)
265 (setq merge-window-lines
266 (max 2 (round (* (window-height) merge-window-share))))
267 (switch-to-buffer buf-A)
268 (setq wind-A (selected-window))
269
270 ;; XEmacs seems to have a lot of trouble with display
271 ;; It won't set things right unless we tell it to sit still
272 (if ediff-xemacs-p (sit-for 0))
273
274 (split-window-vertically (max 2 (- (window-height) merge-window-lines)))
275 (if (eq (selected-window) wind-A)
276 (other-window 1))
277 (setq wind-C (selected-window))
278 (switch-to-buffer buf-C)
279
280 (select-window wind-A)
281 (funcall split-window-function)
282
283 (if (eq (selected-window) wind-A)
284 (other-window 1))
285 (switch-to-buffer buf-B)
286 (setq wind-B (selected-window))
287
288 (ediff-eval-in-buffer control-buffer
289 (setq ediff-window-A wind-A
290 ediff-window-B wind-B
291 ediff-window-C wind-C))
292
293 (ediff-select-lowest-window)
294 (ediff-setup-control-buffer control-buffer)
295 ))
296
297
298 ;; This function handles all comparison jobs, including 3way jobs
299 (defun ediff-setup-windows-plain-compare (buf-A buf-B buf-C control-buffer)
300 ;; skip dedicated and unsplittable frames
301 (ediff-destroy-control-frame control-buffer)
302 (let ((window-min-height 2)
303 split-window-function wind-width-or-height
304 three-way-comparison
305 wind-A-start wind-B-start wind-A wind-B wind-C)
306 (ediff-eval-in-buffer control-buffer
307 (setq wind-A-start (ediff-overlay-start
308 (ediff-get-value-according-to-buffer-type
309 'A ediff-narrow-bounds))
310 wind-B-start (ediff-overlay-start
311 (ediff-get-value-according-to-buffer-type
312 'B ediff-narrow-bounds))
313 ;; this lets us have local versions of ediff-split-window-function
314 split-window-function ediff-split-window-function
315 three-way-comparison ediff-3way-comparison-job))
316 (delete-other-windows)
317 (split-window-vertically)
318 (ediff-select-lowest-window)
319 (ediff-setup-control-buffer control-buffer)
320
321 ;; go to the upper window and split it betw A, B, and possibly C
322 (other-window 1)
323 (switch-to-buffer buf-A)
324 (setq wind-A (selected-window))
325 (if three-way-comparison
326 (setq wind-width-or-height
327 (/ (if (eq split-window-function 'split-window-vertically)
328 (window-height wind-A)
329 (window-width wind-A))
330 3)))
331
332 ;; XEmacs seems to have a lot of trouble with display
333 ;; It won't set things right unless we tell it to sit still
334 (if ediff-xemacs-p (sit-for 0))
335
336 (funcall split-window-function wind-width-or-height)
337
338 (if (eq (selected-window) wind-A)
339 (other-window 1))
340 (switch-to-buffer buf-B)
341 (setq wind-B (selected-window))
342
343 (if three-way-comparison
344 (progn
345 (funcall split-window-function) ; equally
346 (if (eq (selected-window) wind-B)
347 (other-window 1))
348 (switch-to-buffer buf-C)
349 (setq wind-C (selected-window))))
350
351 (ediff-eval-in-buffer control-buffer
352 (setq ediff-window-A wind-A
353 ediff-window-B wind-B
354 ediff-window-C wind-C))
355
356 ;; It is unlikely that we will want to implement 3way window comparison.
357 ;; So, only buffers A and B are used here.
358 (if ediff-windows-job
359 (progn
360 (set-window-start wind-A wind-A-start)
361 (set-window-start wind-B wind-B-start)))
362
363 (ediff-select-lowest-window)
364 (ediff-setup-control-buffer control-buffer)
365 ))
366
367
368 ;; dispatch the appropriate window setup function
369 (defun ediff-setup-windows-multiframe (buf-A buf-B buf-C control-buf)
370 (ediff-eval-in-buffer control-buf
371 (setq ediff-multiframe t))
372 (if ediff-merge-job
373 (ediff-setup-windows-multiframe-merge buf-A buf-B buf-C control-buf)
374 (ediff-setup-windows-multiframe-compare buf-A buf-B buf-C control-buf)))
375
376 (defun ediff-setup-windows-multiframe-merge (buf-A buf-B buf-C control-buf)
377 ;;; Algorithm:
378 ;;; If A and B are in the same frame but C's frame is different--- use one
379 ;;; frame for A and B and use a separate frame for C.
380 ;;; If C's frame is non-existent, then: if the first suitable
381 ;;; non-dedicated frame is different from A&B's, then use it for C.
382 ;;; Otherwise, put A,B, and C in one frame.
383 ;;; If buffers A, B, C are is separate frames, use them to display these
384 ;;; buffers.
385
386 ;; Skip dedicated or iconified frames.
387 ;; Unsplittable frames are taken care of later.
388 (ediff-skip-unsuitable-frames 'ok-unsplittable)
389
390 (let* ((window-min-height 2)
391 (wind-A (ediff-get-visible-buffer-window buf-A))
392 (wind-B (ediff-get-visible-buffer-window buf-B))
393 (wind-C (ediff-get-visible-buffer-window buf-C))
394 (frame-A (if wind-A (window-frame wind-A)))
395 (frame-B (if wind-B (window-frame wind-B)))
396 (frame-C (if wind-C (window-frame wind-C)))
397 ;; on wide display, do things in one frame
398 (force-one-frame
399 (ediff-eval-in-buffer control-buf ediff-wide-display-p))
400 ;; this lets us have local versions of ediff-split-window-function
401 (split-window-function
402 (ediff-eval-in-buffer control-buf ediff-split-window-function))
403 (orig-wind (selected-window))
404 (orig-frame (selected-frame))
405 (use-same-frame (or force-one-frame
406 (eq frame-A (or frame-C orig-frame))
407 (eq frame-B (or frame-C orig-frame))
408 (not (frame-live-p frame-A))
409 (not (frame-live-p frame-B))
410 (and (eq frame-A frame-B)
411 (not (frame-live-p frame-C)))
412 ))
413 (use-same-frame-for-AB (and (not use-same-frame)
414 (eq frame-A frame-B)))
415 (merge-window-share (ediff-eval-in-buffer control-buf
416 ediff-merge-window-share))
417 merge-window-lines
418 designated-minibuffer-frame
419 done-A done-B done-C)
420
421 ;; buf-A on its own
422 (if (and (window-live-p wind-A)
423 (null use-same-frame)
424 (null use-same-frame-for-AB))
425 (progn
426 (select-window wind-A)
427 (delete-other-windows)
428 (switch-to-buffer buf-A)
429 (setq wind-A (selected-window))
430 (setq done-A t)))
431
432 ;; buf-B on its own
433 (if (and (window-live-p wind-B) (null use-same-frame)) ; buf B on its own
434 (progn
435 (select-window wind-B)
436 (delete-other-windows)
437 (switch-to-buffer buf-B)
438 (setq wind-B (selected-window))
439 (setq done-B t)))
440
441 ;; buf-C on its own
442 (if (and (window-live-p wind-C) (null use-same-frame)) ; buf C on its own
443 (progn
444 (select-window wind-C)
445 (delete-other-windows)
446 (switch-to-buffer buf-C)
447 (setq wind-C (selected-window))
448 (setq done-C t)))
449
450 (if use-same-frame-for-AB
451 (progn
452 (select-frame frame-A)
453 (switch-to-buffer buf-A)
454 (delete-other-windows)
455 (setq wind-A (selected-window))
456
457 (funcall split-window-function)
458 (if (eq (selected-window) wind-A)
459 (other-window 1))
460 (switch-to-buffer buf-B)
461 (setq wind-B (selected-window))
462
463 (setq done-A t
464 done-B t)))
465
466 (if use-same-frame
467 (let ((curr-frame (selected-frame))
468 (window-min-height 2))
469 ;; avoid dedicated and non-splittable windows
470 (ediff-skip-unsuitable-frames)
471 (or (eq curr-frame (selected-frame))
472 (setq wind-A nil
473 wind-B nil
474 wind-C nil
475 orig-wind (selected-window)))
476
477 ;; set the right frame
478 (cond (wind-A (select-window wind-A))
479 (wind-B (select-window wind-B))
480 (wind-C (select-window wind-C))
481 (t (select-window orig-wind)))
482 (delete-other-windows)
483 (setq merge-window-lines
484 (max 2 (round (* (window-height) merge-window-share))))
485 (switch-to-buffer buf-A)
486 (setq wind-A (selected-window))
487
488 ;; XEmacs seems to have a lot of trouble with display
489 ;; It won't set things right unless we tell it to catch breath
490 (if ediff-xemacs-p (sit-for 0))
491
492 (split-window-vertically
493 (max 2 (- (window-height) merge-window-lines)))
494 (if (eq (selected-window) wind-A)
495 (other-window 1))
496 (setq wind-C (selected-window))
497 (switch-to-buffer buf-C)
498
499 (select-window wind-A)
500
501 (funcall split-window-function)
502 (if (eq (selected-window) wind-A)
503 (other-window 1))
504 (switch-to-buffer buf-B)
505 (setq wind-B (selected-window))
506
507 (setq done-A t
508 done-B t
509 done-C t)
510 ))
511
512 (or done-A ; Buf A to be set in its own frame
513 (progn ; It was not set up yet as it wasn't visible
514 (select-window orig-wind)
515 (delete-other-windows)
516 (switch-to-buffer buf-A)
517 (setq wind-A (selected-window))
518 ))
519 (or done-B ; Buf B to be set in its own frame
520 (progn ; It was not set up yet as it wasn't visible
521 (select-window orig-wind)
522 (delete-other-windows)
523 (switch-to-buffer buf-B)
524 (setq wind-B (selected-window))
525 ))
526
527 (or done-C ; Buf C to be set in its own frame.
528 (progn ; It was not set up yet as it wasn't visible
529 (select-window orig-wind)
530 (delete-other-windows)
531 (switch-to-buffer buf-C)
532 (setq wind-C (selected-window))
533 ))
534
535 (ediff-eval-in-buffer control-buf
536 (setq ediff-window-A wind-A
537 ediff-window-B wind-B
538 ediff-window-C wind-C)
539 (setq frame-A (window-frame ediff-window-A)
540 designated-minibuffer-frame
541 (window-frame (minibuffer-window frame-A))))
542
543 (ediff-setup-control-frame control-buf designated-minibuffer-frame)
544 ))
545
546
547 ;; Window setup for all comparison jobs, including 3way comparisons
548 (defun ediff-setup-windows-multiframe-compare (buf-A buf-B buf-C control-buf)
549 ;;; Algorithm:
550 ;;; If a buffer is seen in a frame, use that frame for that buffer.
551 ;;; If it is not seen, use the current frame.
552 ;;; If both buffers are not seen, they share the current frame. If one
553 ;;; of the buffers is not seen, it is placed in the current frame (where
554 ;;; ediff started). If that frame is displaying the other buffer, it is
555 ;;; shared between the two buffers.
556 ;;; However, if we decide to put both buffers in one frame
557 ;;; and the selected frame isn't splittable, we create a new frame and
558 ;;; put both buffers there, event if one of this buffers is visible in
559 ;;; another frame.
560
561 ;; Skip dedicated or iconified frames.
562 ;; Unsplittable frames are taken care of later.
563 (ediff-skip-unsuitable-frames 'ok-unsplittable)
564
565 (let* ((window-min-height 2)
566 (wind-A (ediff-get-visible-buffer-window buf-A))
567 (wind-B (ediff-get-visible-buffer-window buf-B))
568 (wind-C (ediff-get-visible-buffer-window buf-C))
569 (frame-A (if wind-A (window-frame wind-A)))
570 (frame-B (if wind-B (window-frame wind-B)))
571 (frame-C (if wind-C (window-frame wind-C)))
572 (ctl-frame-exists-p (ediff-eval-in-buffer control-buf
573 (frame-live-p ediff-control-frame)))
574 ;; on wide display, do things in one frame
575 (force-one-frame
576 (ediff-eval-in-buffer control-buf ediff-wide-display-p))
577 ;; this lets us have local versions of ediff-split-window-function
578 (split-window-function
579 (ediff-eval-in-buffer control-buf ediff-split-window-function))
580 (three-way-comparison
581 (ediff-eval-in-buffer control-buf ediff-3way-comparison-job))
582 (orig-wind (selected-window))
583 (use-same-frame (or force-one-frame
584 (eq frame-A frame-B)
585 (if three-way-comparison
586 (or (eq frame-A frame-C)
587 (eq frame-B frame-C)
588 (not (frame-live-p frame-A))
589 (not (frame-live-p frame-B))
590 (not (frame-live-p frame-C))))
591 (and (not (frame-live-p frame-B))
592 (or ctl-frame-exists-p
593 (eq frame-A (selected-frame))))
594 (and (not (frame-live-p frame-A))
595 (or ctl-frame-exists-p
596 (eq frame-B (selected-frame))))))
597 wind-A-start wind-B-start
598 designated-minibuffer-frame
599 done-A done-B done-C)
600
601 (ediff-eval-in-buffer control-buf
602 (setq wind-A-start (ediff-overlay-start
603 (ediff-get-value-according-to-buffer-type
604 'A ediff-narrow-bounds))
605 wind-B-start (ediff-overlay-start
606 (ediff-get-value-according-to-buffer-type
607 'B ediff-narrow-bounds))))
608
609 (if (and (window-live-p wind-A) (null use-same-frame)) ; buf-A on its own
610 (progn
611 (select-window wind-A)
612 (delete-other-windows)
613 (switch-to-buffer buf-A)
614 (setq wind-A (selected-window))
615 (setq done-A t)))
616
617 (if (and (window-live-p wind-B) (null use-same-frame)) ; buf B on its own
618 (progn
619 (select-window wind-B)
620 (delete-other-windows)
621 (switch-to-buffer buf-B)
622 (setq wind-B (selected-window))
623 (setq done-B t)))
624
625 (if (and (window-live-p wind-C) (null use-same-frame)) ; buf C on its own
626 (progn
627 (select-window wind-C)
628 (delete-other-windows)
629 (switch-to-buffer buf-C)
630 (setq wind-C (selected-window))
631 (setq done-C t)))
632
633 (if use-same-frame
634 (let ((curr-frame (selected-frame))
635 ;; this affects 3way setups only
636 wind-width-or-height)
637 ;; avoid dedicated and non-splittable windows
638 (ediff-skip-unsuitable-frames)
639 (or (eq curr-frame (selected-frame))
640 (setq wind-A nil
641 wind-B nil
642 wind-C nil
643 orig-wind (selected-window)))
644
645 ;; set the right frame
646 (cond (wind-A (select-window wind-A))
647 (wind-B (select-window wind-B))
648 (wind-C (select-window wind-C))
649 (t (select-window orig-wind)))
650 (delete-other-windows)
651 (switch-to-buffer buf-A)
652 (setq wind-A (selected-window))
653
654 ;; XEmacs seems to have a lot of trouble with display
655 ;; It won't set things right unless we tell it to catch breath
656 (if ediff-xemacs-p (sit-for 0))
657
658 (if three-way-comparison
659 (setq wind-width-or-height
660 (/
661 (if (eq split-window-function 'split-window-vertically)
662 (window-height wind-A)
663 (window-width wind-A))
664 3)))
665
666 (funcall split-window-function wind-width-or-height)
667 (if (eq (selected-window) wind-A)
668 (other-window 1))
669 (switch-to-buffer buf-B)
670 (setq wind-B (selected-window))
671
672 (if three-way-comparison
673 (progn
674 (funcall split-window-function) ; equally
675 (if (memq (selected-window) (list wind-A wind-B))
676 (other-window 1))
677 (switch-to-buffer buf-C)
678 (setq wind-C (selected-window))))
679 (setq done-A t
680 done-B t
681 done-C t)
682 ))
683
684 (or done-A ; Buf A to be set in its own frame
685 (progn ; It was not set up yet as it wasn't visible
686 (select-window orig-wind)
687 (delete-other-windows)
688 (switch-to-buffer buf-A)
689 (setq wind-A (selected-window))
690 ))
691 (or done-B ; Buf B to be set in its own frame
692 (progn ; It was not set up yet as it wasn't visible
693 (select-window orig-wind)
694 (delete-other-windows)
695 (switch-to-buffer buf-B)
696 (setq wind-B (selected-window))
697 ))
698
699 (if three-way-comparison
700 (or done-C ; Buf C to be set in its own frame
701 (progn ; It was not set up yet as it wasn't visible
702 (select-window orig-wind)
703 (delete-other-windows)
704 (switch-to-buffer buf-C)
705 (setq wind-C (selected-window))
706 )))
707
708 (ediff-eval-in-buffer control-buf
709 (setq ediff-window-A wind-A
710 ediff-window-B wind-B
711 ediff-window-C wind-C)
712
713 (setq frame-A (window-frame ediff-window-A)
714 designated-minibuffer-frame
715 (window-frame (minibuffer-window frame-A))))
716
717 ;; It is unlikely that we'll implement ediff-windows that would compare
718 ;; 3 windows at once. So, we don't use buffer C here.
719 (if ediff-windows-job
720 (progn
721 (set-window-start wind-A wind-A-start)
722 (set-window-start wind-B wind-B-start)))
723
724 (ediff-setup-control-frame control-buf designated-minibuffer-frame)
725 ))
726
727 ;; skip unsplittable and dedicated windows
728 ;; create a new splittable frame if none is found
729 (defun ediff-skip-unsuitable-frames (&optional ok-unsplittable)
730 (if (ediff-window-display-p)
731 (let (last-window)
732 (while (and (not (eq (selected-window) last-window))
733 (or
734 (window-dedicated-p (selected-window))
735 (ediff-frame-iconified-p (selected-frame))
736 (if ok-unsplittable
737 nil
738 (ediff-frame-unsplittable-p (selected-frame)))))
739 ;; remember where started
740 (or last-window (setq last-window (selected-window)))
741 ;; try new window
742 (other-window 1 t))
743 (if (eq (selected-window) last-window)
744 ;; fed up, no appropriate frame
745 (progn
746 ;;(redraw-display)
747 (select-frame (ediff-make-frame '((unsplittable)))))))))
748
749 ;; Prepare or refresh control frame
750 (defun ediff-setup-control-frame (ctl-buffer designated-minibuffer-frame)
751 (let ((window-min-height 2)
752 ctl-frame-iconified-p dont-iconify-ctl-frame deiconify-ctl-frame
753 ctl-frame old-ctl-frame lines
754 fheight fwidth adjusted-parameters)
755
756 (ediff-eval-in-buffer ctl-buffer
757 (if ediff-xemacs-p (set-buffer-menubar nil))
758 (run-hooks 'ediff-before-setup-control-frame-hooks))
759
760 (setq old-ctl-frame (ediff-eval-in-buffer ctl-buffer ediff-control-frame))
761 ;; Delete the old ctl frame and get a new ctl frame.
762 ;; The old ctl frame is deleted to let emacs reset default minibuffer
763 ;; frame or when the ctl frame needs to be moved.
764 ;; The old frame isn't reused, since ediff-setup-control-frame is called
765 ;; very rarely, so the overhead is minimal.
766 (if (frame-live-p old-ctl-frame) (delete-frame old-ctl-frame))
767 ;;(redraw-display)
768 ;; new ctl frame should be created while ctl-buff is current, so that
769 ;; the local default-minibuffer-frame will be consulted and
770 ;; that ediff-control-frame-parameters will have the right value.
771 (ediff-eval-in-buffer ctl-buffer
772 (let ((default-minibuffer-frame designated-minibuffer-frame))
773 (setq ctl-frame (make-frame ediff-control-frame-parameters)
774 ediff-control-frame ctl-frame)))
775
776 (setq ctl-frame-iconified-p (ediff-frame-iconified-p ctl-frame))
777 (select-frame ctl-frame)
778 (if (window-dedicated-p (selected-window))
779 ()
780 (delete-other-windows)
781 (switch-to-buffer ctl-buffer))
782
783 ;; must be before ediff-setup-control-buffer
784 (if ediff-xemacs-p
785 ;; just a precaution--we should be in ctl-buffer already
786 (ediff-eval-in-buffer ctl-buffer
787 (make-local-variable 'frame-title-format)
788 (make-local-variable 'frame-icon-title-format)))
789
790 (ediff-setup-control-buffer ctl-buffer)
791 (setq dont-iconify-ctl-frame
792 (not (string= ediff-help-message ediff-brief-help-message)))
793 (setq deiconify-ctl-frame
794 (and (eq this-command 'ediff-toggle-help)
795 dont-iconify-ctl-frame))
796
797 ;; 1 more line for the modeline
798 (setq lines (if ediff-xemacs-p
799 (+ 2 (count-lines (point-min) (point-max)))
800 (1+ (count-lines (point-min) (point-max))))
801 fheight lines
802 fwidth (+ (ediff-help-message-line-length) 2)
803 adjusted-parameters (append (list
804 (cons 'width fwidth)
805 (cons 'height fheight))
806 (funcall
807 ediff-control-frame-position-function
808 ctl-buffer fwidth fheight)))
809
810 ;; In XEmacs, buffer menubar needs to be killed before frame parameters
811 ;; are changed. XEmacs needs to redisplay, as it has trouble setting
812 ;; height correctly otherwise.
813 (if ediff-xemacs-p
814 (progn
815 (set-specifier top-toolbar-height (list ctl-frame 0))
816 (set-specifier bottom-toolbar-height (list ctl-frame 0))
817 (set-specifier left-toolbar-width (list ctl-frame 0))
818 (set-specifier right-toolbar-width (list ctl-frame 0))
819 (sit-for 0)))
820
821 ;; Under OS/2 (emx) we have to call modify frame parameters twice, in
822 ;; order to make sure that at least once we do it for non-iconified
823 ;; frame. If appears that in the OS/2 port of Emacs, one can't modify
824 ;; frame parameters of iconified frames.
825 (if (eq system-type 'emx)
826 (modify-frame-parameters ctl-frame adjusted-parameters))
827
828 (goto-char (point-min))
829
830 (cond ((and ediff-prefer-iconified-control-frame
831 (not ctl-frame-iconified-p)
832 (not dont-iconify-ctl-frame))
833 (iconify-frame ctl-frame))
834 ((or deiconify-ctl-frame
835 (not ctl-frame-iconified-p))
836 (raise-frame ctl-frame)))
837
838 ;; This works around a bug in 19.25 and earlier. There, if frame gets
839 ;; iconified, the current buffer changes to that of the frame that
840 ;; becomes exposed as a result of this iconification.
841 ;; So, we make sure the current buffer doesn't change.
842 (select-frame ctl-frame)
843 (ediff-refresh-control-frame)
844
845 (modify-frame-parameters ctl-frame adjusted-parameters)
846
847 (if ediff-xemacs-p
848 (set-window-buffer-dedicated (selected-window) ctl-buffer)
849 (set-window-dedicated-p (selected-window) t))
850
851 (or ediff-xemacs-p (sit-for 0 200)) ; emacs has trouble here, needs time
852 (or (ediff-frame-iconified-p ctl-frame)
853 (ediff-reset-mouse ctl-frame))
854 (or ediff-xemacs-p (unfocus-frame))
855
856 (if ediff-xemacs-p
857 (ediff-eval-in-buffer ctl-buffer
858 (make-local-variable 'select-frame-hook)
859 (add-hook 'select-frame-hook 'ediff-xemacs-select-frame-hook)
860 ))
861
862 (ediff-eval-in-buffer ctl-buffer
863 (run-hooks 'ediff-after-setup-control-frame-hooks))
864 ))
865
866 (defun ediff-destroy-control-frame (ctl-buffer)
867 (ediff-eval-in-buffer ctl-buffer
868 (if (and (ediff-window-display-p) (frame-live-p ediff-control-frame))
869 (let ((ctl-frame ediff-control-frame))
870 (if ediff-xemacs-p
871 (set-buffer-menubar default-menubar))
872 ;;(redraw-display)
873 (setq ediff-control-frame nil)
874 (delete-frame ctl-frame)
875 )))
876 (ediff-skip-unsuitable-frames)
877 (ediff-reset-mouse))
878
879
880 ;; finds a good place to clip control frame
881 (defun ediff-make-frame-position (ctl-buffer ctl-frame-width ctl-frame-height)
882 (ediff-eval-in-buffer ctl-buffer
883 (let* ((frame-A (window-frame ediff-window-A))
884 (frame-A-parameters (frame-parameters frame-A))
885 (frame-A-top (cdr (assoc 'top frame-A-parameters)))
886 (frame-A-left (cdr (assoc 'left frame-A-parameters)))
887 (frame-A-width (frame-width frame-A))
888 (ctl-frame ediff-control-frame)
889 horizontal-adjustment upward-adjustment
890 ctl-frame-top)
891
892 ;; Multiple control frames are clipped based on the value of
893 ;; ediff-control-buffer-number. This is done in order not to obscure
894 ;; other active control panels.
895 (setq horizontal-adjustment (* 2 ediff-control-buffer-number)
896 upward-adjustment (* -14 ediff-control-buffer-number))
897
898 (setq ctl-frame-top (- frame-A-top
899 upward-adjustment
900 ediff-control-frame-upward-shift))
901 (list
902 (cons 'top (if (> ctl-frame-top 0) ctl-frame-top 1))
903 (cons 'left (+ frame-A-left
904 (if ediff-prefer-long-help-message
905 (* (ediff-frame-char-width ctl-frame)
906 (+ ediff-wide-control-frame-rightward-shift
907 horizontal-adjustment))
908 (- (* frame-A-width
909 (ediff-frame-char-width frame-A))
910 (* (ediff-frame-char-width ctl-frame)
911 (+ ctl-frame-width
912 ediff-narrow-control-frame-leftward-shift
913 horizontal-adjustment))))))))))
914
915 (defun ediff-xemacs-select-frame-hook ()
916 (if (equal (selected-frame) ediff-control-frame)
917 (raise-frame ediff-control-frame)))
918
919 (defun ediff-make-wide-display ()
920 "Construct an alist of parameters for the wide display.
921 Saves the old frame parameters in `ediff-wide-display-orig-parameters'.
922 The frame to be resized is kept in `ediff-wide-display-frame'.
923 This function modifies only the left margin and the width of the display.
924 It assumes that it is called from within the control buffer."
925 (if (not (fboundp 'ediff-display-pixel-width))
926 (error "Can't determine display width."))
927 (let* ((frame-A (window-frame ediff-window-A))
928 (frame-A-params (frame-parameters frame-A))
929 (cw (ediff-frame-char-width frame-A))
930 (wd (- (/ (ediff-display-pixel-width) cw) 5)))
931 (setq ediff-wide-display-orig-parameters
932 (list (cons 'left (max 0 (cdr (assoc 'left frame-A-params))))
933 (cons 'width (cdr (assoc 'width frame-A-params))))
934 ediff-wide-display-frame frame-A)
935 (modify-frame-parameters frame-A (list (cons 'left cw)
936 (cons 'width wd)))))
937
938
939
940 ;; Revise the mode line to display which difference we have selected
941 ;; Also resets modelines of buffers A/B, since they may be clobbered by
942 ;; anothe invocations of Ediff.
943 (defun ediff-refresh-mode-lines ()
944 (let (buf-A-state-diff buf-B-state-diff buf-C-state-diff buf-C-state-merge)
945
946 (if (ediff-valid-difference-p)
947 (setq
948 buf-C-state-diff (ediff-get-state-of-diff ediff-current-difference 'C)
949 buf-C-state-merge (ediff-get-state-of-merge ediff-current-difference)
950 buf-A-state-diff (ediff-get-state-of-diff ediff-current-difference 'A)
951 buf-B-state-diff (ediff-get-state-of-diff ediff-current-difference 'B)
952 buf-A-state-diff (if buf-A-state-diff
953 (format "[%s] " buf-A-state-diff)
954 "")
955 buf-B-state-diff (if buf-B-state-diff
956 (format "[%s] " buf-B-state-diff)
957 "")
958 buf-C-state-diff (if (and (ediff-buffer-live-p ediff-buffer-C)
959 (or buf-C-state-diff buf-C-state-merge))
960 (format "[%s%s] "
961 (or buf-C-state-diff "")
962 (if buf-C-state-merge
963 (concat " " buf-C-state-merge)
964 ""))
965 ""))
966 (setq buf-A-state-diff ""
967 buf-B-state-diff ""
968 buf-C-state-diff ""))
969
970 ;; control buffer format
971 (setq mode-line-format
972 (list (if (ediff-narrow-control-frame-p) " " "-- ")
973 mode-line-buffer-identification
974 " Howdy!"))
975 ;; control buffer id
976 (setq mode-line-buffer-identification
977 (if (ediff-narrow-control-frame-p)
978 (ediff-make-narrow-control-buffer-id 'skip-name)
979 (ediff-make-wide-control-buffer-id)))
980 ;; Force mode-line redisplay
981 (force-mode-line-update)
982
983 (if (and (ediff-window-display-p) (frame-live-p ediff-control-frame))
984 (ediff-refresh-control-frame))
985
986 (ediff-eval-in-buffer ediff-buffer-A
987 (setq ediff-diff-status buf-A-state-diff)
988 (ediff-strip-mode-line-format)
989 (setq mode-line-format
990 (list " A: " 'ediff-diff-status mode-line-format))
991 (force-mode-line-update))
992 (ediff-eval-in-buffer ediff-buffer-B
993 (setq ediff-diff-status buf-B-state-diff)
994 (ediff-strip-mode-line-format)
995 (setq mode-line-format
996 (list " B: " 'ediff-diff-status mode-line-format))
997 (force-mode-line-update))
998 (if ediff-3way-job
999 (ediff-eval-in-buffer ediff-buffer-C
1000 (setq ediff-diff-status buf-C-state-diff)
1001 (ediff-strip-mode-line-format)
1002 (setq mode-line-format
1003 (list " C: " 'ediff-diff-status mode-line-format))
1004 (force-mode-line-update)))
1005 ))
1006
1007
1008 (defun ediff-refresh-control-frame ()
1009 (if ediff-xemacs-p
1010 (progn
1011 (setq frame-title-format (ediff-make-narrow-control-buffer-id)
1012 frame-icon-title-format (ediff-make-narrow-control-buffer-id))
1013 ;; this forces update of the frame title
1014 (modify-frame-parameters ediff-control-frame '(())))
1015 (modify-frame-parameters
1016 ediff-control-frame
1017 (list (cons 'name (ediff-make-narrow-control-buffer-id))))
1018 ))
1019
1020
1021 (defun ediff-make-narrow-control-buffer-id (&optional skip-name)
1022 (concat
1023 (if skip-name
1024 " "
1025 (concat
1026 (cdr (assoc 'name ediff-control-frame-parameters))
1027 ediff-control-buffer-suffix))
1028 (cond ((< ediff-current-difference 0)
1029 (format " _/%d" ediff-number-of-differences))
1030 ((>= ediff-current-difference ediff-number-of-differences)
1031 (format " $/%d" ediff-number-of-differences))
1032 (t
1033 (format " %d/%d"
1034 (1+ ediff-current-difference)
1035 ediff-number-of-differences)))))
1036
1037 (defun ediff-make-wide-control-buffer-id ()
1038 (cond ((< ediff-current-difference 0)
1039 (list (format "%%b At start of %d diffs"
1040 ediff-number-of-differences)))
1041 ((>= ediff-current-difference ediff-number-of-differences)
1042 (list (format "%%b At end of %d diffs"
1043 ediff-number-of-differences)))
1044 (t
1045 (list (format "%%b diff %d of %d"
1046 (1+ ediff-current-difference)
1047 ediff-number-of-differences)))))
1048
1049
1050
1051 ;; If buff is not live, return nil
1052 (defun ediff-get-visible-buffer-window (buff)
1053 (if (ediff-buffer-live-p buff)
1054 (if ediff-xemacs-p
1055 (get-buffer-window buff t)
1056 (get-buffer-window buff 'visible))))
1057
1058 ;;; Functions to decide when to redraw windows
1059
1060
1061 (defun ediff-keep-window-config (control-buf)
1062 (and (eq control-buf (current-buffer))
1063 (/= (buffer-size) 0)
1064 (ediff-eval-in-buffer control-buf
1065 (let ((ctl-wind ediff-control-window)
1066 (A-wind ediff-window-A)
1067 (B-wind ediff-window-B)
1068 (C-wind ediff-window-C))
1069
1070 (and
1071 (ediff-window-visible-p A-wind)
1072 (ediff-window-visible-p B-wind)
1073 ;; if buffer C is defined then take it into account
1074 (or (not ediff-3way-job)
1075 (ediff-window-visible-p C-wind))
1076 (eq (window-buffer A-wind) ediff-buffer-A)
1077 (eq (window-buffer B-wind) ediff-buffer-B)
1078 (or (not ediff-3way-job)
1079 (eq (window-buffer C-wind) ediff-buffer-C))
1080 (string= ediff-window-config-saved
1081 (format "%S%S%S%S%S%S%S"
1082 ctl-wind A-wind B-wind C-wind
1083 ediff-split-window-function
1084 (ediff-multiframe-setup-p)
1085 ediff-wide-display-p)))))))
1086
1087
1088 (provide 'ediff-wind)
1089
1090
1091 ;;; ediff-wind.el ends here