]> code.delx.au - gnu-emacs/blob - lisp/ediff-wind.el
Initial revision
[gnu-emacs] / lisp / ediff-wind.el
1 ;;; ediff-wind.el --- window manipulation utilities
2 ;;; Copyright (C) 1994 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 (not window-system)
27 'ediff-setup-windows-plain
28 'ediff-setup-windows-multiframe)
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 window-system
86 (list
87 '(name . "Ediff")
88 ;;'(unsplittable . t)
89 '(minibuffer . nil)
90 '(vertical-scroll-bars . nil) ; FSF only
91 '(scrollbar-width . 0) ; XEmacs only
92 '(menu-bar-lines . 0) ; FSF 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 'x-display-pixel-height)
100 (1+ (x-display-pixel-height))
101 3000))
102 (cons 'left (if (fboundp 'x-display-pixel-width)
103 (1+ (x-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 (let* ((lowest-window (selected-window))
190 (bottom-edge (car (cdr (cdr (cdr (window-edges))))))
191 ;;(last-window (previous-window))
192 (last-window (save-excursion
193 (other-window -1) (selected-window)))
194 (window-search t))
195 (while window-search
196 (let* ((this-window (next-window))
197 (next-bottom-edge (car (cdr (cdr (cdr
198 (window-edges this-window)))))))
199 (if (< bottom-edge next-bottom-edge)
200 (progn
201 (setq bottom-edge next-bottom-edge)
202 (setq lowest-window this-window)))
203
204 (select-window this-window)
205 (if (eq last-window this-window)
206 (progn
207 (select-window lowest-window)
208 (setq window-search nil)))))))
209
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 ;; when support for buf C is added, the actual window setup function will
226 ;; depend on ediff-job-name
227 (or (ediff-keep-window-config control-buffer)
228 (funcall
229 (ediff-eval-in-buffer control-buffer ediff-window-setup-function)
230 buffer-A buffer-B buffer-C control-buffer))
231 (run-hooks 'ediff-after-setup-windows-hooks))
232
233 ;; Just set up 3 windows.
234 ;; Usually used without windowing systems
235 ;; With windowing, we want to use dedicated frames.
236 (defun ediff-setup-windows-plain (buffer-A buffer-B buffer-C control-buffer)
237 (ediff-eval-in-buffer control-buffer
238 (setq ediff-multiframe nil))
239 (if ediff-merge-job
240 (ediff-setup-windows-plain-merge
241 buffer-A buffer-B buffer-C control-buffer)
242 (ediff-setup-windows-plain-compare
243 buffer-A buffer-B buffer-C control-buffer)))
244
245 (defun ediff-setup-windows-plain-merge (buf-A buf-B buf-C control-buffer)
246 ;; skip dedicated and unsplittable frames
247 (ediff-eval-in-buffer control-buffer
248 (if (and window-system (ediff-frame-live-p ediff-control-frame))
249 (progn
250 (redraw-display)
251 (ediff-delete-frame ediff-control-frame)
252 (setq ediff-control-frame nil))))
253 (ediff-skip-unsuitable-frames)
254 (let ((window-min-height 1)
255 split-window-function
256 merge-window-share merge-window-lines
257 wind-A wind-B wind-C)
258 (ediff-eval-in-buffer control-buffer
259 (setq merge-window-share ediff-merge-window-share
260 ;; this lets us have local versions of ediff-split-window-function
261 split-window-function ediff-split-window-function))
262 (delete-other-windows)
263 (split-window-vertically)
264 (ediff-select-lowest-window)
265 (ediff-setup-control-buffer control-buffer)
266
267 ;; go to the upper window and split it betw A, B, and possibly C
268 (other-window 1)
269 (setq merge-window-lines
270 (max 2 (round (* (window-height) merge-window-share))))
271 (switch-to-buffer buf-A)
272 (setq wind-A (selected-window))
273
274 ;; XEmacs seems to have a lot of trouble with display
275 ;; It won't set things right unless we tell it to sit still
276 (if ediff-xemacs-p (sit-for 0))
277
278 (split-window-vertically (max 2 (- (window-height) merge-window-lines)))
279 (if (eq (selected-window) wind-A)
280 (other-window 1))
281 (setq wind-C (selected-window))
282 (switch-to-buffer buf-C)
283
284 (select-window wind-A)
285 (funcall split-window-function)
286
287 (if (eq (selected-window) wind-A)
288 (other-window 1))
289 (switch-to-buffer buf-B)
290 (setq wind-B (selected-window))
291
292 (ediff-eval-in-buffer control-buffer
293 (setq ediff-window-A wind-A
294 ediff-window-B wind-B
295 ediff-window-C wind-C))
296
297 (ediff-select-lowest-window)
298 (ediff-setup-control-buffer control-buffer)
299 ))
300
301
302 ;; This function handles all comparison jobs, including 3way jobs
303 (defun ediff-setup-windows-plain-compare (buf-A buf-B buf-C control-buffer)
304 ;; skip dedicated and unsplittable frames
305 (ediff-eval-in-buffer control-buffer
306 (if (and window-system (ediff-frame-live-p ediff-control-frame))
307 (progn
308 (redraw-display)
309 (ediff-delete-frame ediff-control-frame)
310 (setq ediff-control-frame nil))))
311 (ediff-skip-unsuitable-frames)
312 (let ((window-min-height 1)
313 split-window-function wind-width-or-height
314 three-way-comparison
315 job wind-A-start wind-B-start wind-A wind-B wind-C)
316 (ediff-eval-in-buffer control-buffer
317 (setq wind-A-start (ediff-overlay-start
318 (ediff-get-value-according-to-buffer-type
319 'A ediff-narrow-bounds))
320 wind-B-start (ediff-overlay-start
321 (ediff-get-value-according-to-buffer-type
322 'B ediff-narrow-bounds))
323 job ediff-job-name
324 ;; this lets us have local versions of ediff-split-window-function
325 split-window-function ediff-split-window-function
326 three-way-comparison ediff-3way-comparison-job))
327 (delete-other-windows)
328 (split-window-vertically)
329 (ediff-select-lowest-window)
330 (ediff-setup-control-buffer control-buffer)
331
332 ;; go to the upper window and split it betw A, B, and possibly C
333 (other-window 1)
334 (switch-to-buffer buf-A)
335 (setq wind-A (selected-window))
336 (if three-way-comparison
337 (setq wind-width-or-height
338 (/ (if (eq split-window-function 'split-window-vertically)
339 (window-height wind-A)
340 (window-width wind-A))
341 3)))
342
343 ;; XEmacs seems to have a lot of trouble with display
344 ;; It won't set things right unless we tell it to sit still
345 (if ediff-xemacs-p (sit-for 0))
346
347 (funcall split-window-function wind-width-or-height)
348
349 (if (eq (selected-window) wind-A)
350 (other-window 1))
351 (switch-to-buffer buf-B)
352 (setq wind-B (selected-window))
353
354 (if three-way-comparison
355 (progn
356 (funcall split-window-function) ; equally
357 (if (eq (selected-window) wind-B)
358 (other-window 1))
359 (switch-to-buffer buf-C)
360 (setq wind-C (selected-window))))
361
362 (ediff-eval-in-buffer control-buffer
363 (setq ediff-window-A wind-A
364 ediff-window-B wind-B
365 ediff-window-C wind-C))
366
367 ;; It is unlikely that we will want to implement 3way window comparison.
368 ;; So, only buffers A and B are used here.
369 (if (eq job 'ediff-windows)
370 (progn
371 (set-window-start wind-A wind-A-start)
372 (set-window-start wind-B wind-B-start)))
373
374 (ediff-select-lowest-window)
375 (ediff-setup-control-buffer control-buffer)
376 ))
377
378
379 ;; dispatch the appropriate window setup function
380 (defun ediff-setup-windows-multiframe (buf-A buf-B buf-C control-buf)
381 (ediff-eval-in-buffer control-buf
382 (setq ediff-multiframe t))
383 (if ediff-merge-job
384 (ediff-setup-windows-multiframe-merge buf-A buf-B buf-C control-buf)
385 (ediff-setup-windows-multiframe-compare buf-A buf-B buf-C control-buf)))
386
387 (defun ediff-setup-windows-multiframe-merge (buf-A buf-B buf-C control-buf)
388 ;;; Algorithm:
389 ;;; If A and B are in the same frame but C's frame is different--- use one
390 ;;; frame for A and B and use a separate frame for C.
391 ;;; If C's frame is non-existent, then: if the first suitable
392 ;;; non-dedicated frame is different from A&B's, then use it for C.
393 ;;; Otherwise, put A,B, and C in one frame.
394 ;;; If buffers A, B, C are is separate frames, use them to display these
395 ;;; buffers.
396
397 ;; Skip dedicated or iconified frames. Unsplittable frames are taken
398 ;; care of later.
399 (ediff-skip-unsuitable-frames 'ok-unsplittable)
400
401 (let* ((window-min-height 1)
402 (wind-A (ediff-get-visible-buffer-window buf-A))
403 (wind-B (ediff-get-visible-buffer-window buf-B))
404 (wind-C (ediff-get-visible-buffer-window buf-C))
405 (frame-A (if wind-A (ediff-window-frame wind-A)))
406 (frame-B (if wind-B (ediff-window-frame wind-B)))
407 (frame-C (if wind-C (ediff-window-frame wind-C)))
408 ;; on wide display, do things in one frame
409 (force-one-frame
410 (ediff-eval-in-buffer control-buf ediff-wide-display-p))
411 ;; this lets us have local versions of ediff-split-window-function
412 (split-window-function
413 (ediff-eval-in-buffer control-buf ediff-split-window-function))
414 (orig-wind (selected-window))
415 (orig-frame (ediff-selected-frame))
416 (use-same-frame (or force-one-frame
417 (eq frame-A (or frame-C orig-frame))
418 (eq frame-B (or frame-C orig-frame))
419 (not (ediff-frame-live-p frame-A))
420 (not (ediff-frame-live-p frame-B))
421 ))
422 (use-same-frame-for-AB (and (not use-same-frame)
423 (eq frame-A frame-B)))
424 (merge-window-share (ediff-eval-in-buffer control-buf
425 ediff-merge-window-share))
426 merge-window-lines
427 designated-minibuffer-frame
428 done-A done-B done-C)
429
430 ;; buf-A on its own
431 (if (and (window-live-p wind-A)
432 (null use-same-frame)
433 (null use-same-frame-for-AB))
434 (progn
435 (select-window wind-A)
436 (delete-other-windows)
437 (switch-to-buffer buf-A)
438 (setq wind-A (selected-window))
439 (setq done-A t)))
440
441 ;; buf-B on its own
442 (if (and (window-live-p wind-B) (null use-same-frame)) ; buf B on its own
443 (progn
444 (select-window wind-B)
445 (delete-other-windows)
446 (switch-to-buffer buf-B)
447 (setq wind-B (selected-window))
448 (setq done-B t)))
449
450 ;; buf-C on its own
451 (if (and (window-live-p wind-C) (null use-same-frame)) ; buf C on its own
452 (progn
453 (select-window wind-C)
454 (delete-other-windows)
455 (switch-to-buffer buf-C)
456 (setq wind-C (selected-window))
457 (setq done-C t)))
458
459 (if use-same-frame-for-AB
460 (progn
461 (ediff-select-frame frame-A)
462 (switch-to-buffer buf-A)
463 (delete-other-windows)
464 (setq wind-A (selected-window))
465
466 (funcall split-window-function)
467 (if (eq (selected-window) wind-A)
468 (other-window 1))
469 (switch-to-buffer buf-B)
470 (setq wind-B (selected-window))
471
472 (setq done-A t
473 done-B t)))
474
475 (if use-same-frame
476 (let ((curr-frame (ediff-selected-frame))
477 (window-min-height 1))
478 ;; avoid dedicated and non-splittable windows
479 (ediff-skip-unsuitable-frames)
480 (or (eq curr-frame (ediff-selected-frame))
481 (setq wind-A nil
482 wind-B nil
483 wind-C nil
484 orig-wind (selected-window)))
485
486 ;; set the right frame
487 (cond (wind-A (select-window wind-A))
488 (wind-B (select-window wind-B))
489 (wind-C (select-window wind-C))
490 (t (select-window orig-wind)))
491 (delete-other-windows)
492 (setq merge-window-lines
493 (max 2 (round (* (window-height) merge-window-share))))
494 (switch-to-buffer buf-A)
495 (setq wind-A (selected-window))
496
497 ;; XEmacs seems to have a lot of trouble with display
498 ;; It won't set things right unless we tell it to catch breath
499 (if ediff-xemacs-p (sit-for 0))
500
501 (split-window-vertically
502 (max 2 (- (window-height) merge-window-lines)))
503 (if (eq (selected-window) wind-A)
504 (other-window 1))
505 (setq wind-C (selected-window))
506 (switch-to-buffer buf-C)
507
508 (select-window wind-A)
509
510 (funcall split-window-function)
511 (if (eq (selected-window) wind-A)
512 (other-window 1))
513 (switch-to-buffer buf-B)
514 (setq wind-B (selected-window))
515
516 (setq done-A t
517 done-B t
518 done-C t)
519 ))
520
521 (or done-A ; Buf A to be set in its own frame
522 (progn ; It was not set up yet as it wasn't visible
523 (select-window orig-wind)
524 (delete-other-windows)
525 (switch-to-buffer buf-A)
526 (setq wind-A (selected-window))
527 ))
528 (or done-B ; Buf B to be set in its own frame
529 (progn ; It was not set up yet as it wasn't visible
530 (select-window orig-wind)
531 (delete-other-windows)
532 (switch-to-buffer buf-B)
533 (setq wind-B (selected-window))
534 ))
535
536 (or done-C ; Buf C to be set in its own frame.
537 (progn ; It was not set up yet as it wasn't visible
538 (select-window orig-wind)
539 (delete-other-windows)
540 (switch-to-buffer buf-C)
541 (setq wind-C (selected-window))
542 ))
543
544 (ediff-eval-in-buffer control-buf
545 (setq ediff-window-A wind-A
546 ediff-window-B wind-B
547 ediff-window-C wind-C)
548
549 (setq frame-A (ediff-window-frame ediff-window-A)
550 designated-minibuffer-frame (ediff-window-frame
551 (minibuffer-window frame-A)))
552 (if ediff-xemacs-p
553 ()
554 (if (not (eq designated-minibuffer-frame default-minibuffer-frame))
555 (progn
556 (setq default-minibuffer-frame designated-minibuffer-frame)
557 (if (ediff-frame-live-p ediff-control-frame)
558 (ediff-delete-frame ediff-control-frame)))
559 ))
560 )
561
562 (ediff-setup-control-frame control-buf)
563 ))
564
565
566 ;; Window setup for all comparison jobs, including 3way comparisons
567 (defun ediff-setup-windows-multiframe-compare (buf-A buf-B buf-C control-buf)
568 ;;; Algorithm:
569 ;;; If a buffer is seen in a frame, use that frame for that buffer.
570 ;;; If it is not seen, use the current frame.
571 ;;; If both buffers are not seen, they share the current frame. If one
572 ;;; of the buffers is not seen, it is placed in the current frame (where
573 ;;; ediff started). If that frame is displaying the other buffer, it is
574 ;;; shared between the two buffers.
575 ;;; However, if we decide to put both buffers in one frame
576 ;;; and the selected frame isn't splittable, we create a new frame and
577 ;;; put both buffers there, event if one of this buffers is visible in
578 ;;; another frame.
579
580 ;; Skip dedicated or iconified frames.
581 ;; Unsplittable frames are taken care of later.
582 (ediff-skip-unsuitable-frames 'ok-unsplittable)
583
584 (let* ((window-min-height 1)
585 (wind-A (ediff-get-visible-buffer-window buf-A))
586 (wind-B (ediff-get-visible-buffer-window buf-B))
587 (wind-C (ediff-get-visible-buffer-window buf-C))
588 (frame-A (if wind-A (ediff-window-frame wind-A)))
589 (frame-B (if wind-B (ediff-window-frame wind-B)))
590 (frame-C (if wind-C (ediff-window-frame wind-C)))
591 (ctl-frame-exists-p (ediff-eval-in-buffer control-buf
592 (ediff-frame-live-p ediff-control-frame)))
593 ;; on wide display, do things in one frame
594 (force-one-frame
595 (ediff-eval-in-buffer control-buf ediff-wide-display-p))
596 ;; this lets us have local versions of ediff-split-window-function
597 (split-window-function
598 (ediff-eval-in-buffer control-buf ediff-split-window-function))
599 (three-way-comparison
600 (ediff-eval-in-buffer control-buf ediff-3way-comparison-job))
601 (orig-wind (selected-window))
602 (use-same-frame (or force-one-frame
603 (eq frame-A frame-B)
604 (if three-way-comparison
605 (or (eq frame-A frame-C)
606 (eq frame-B frame-C)
607 (not (ediff-frame-live-p frame-A))
608 (not (ediff-frame-live-p frame-B))
609 (not (ediff-frame-live-p frame-C))))
610 (and (not (ediff-frame-live-p frame-B))
611 (or ctl-frame-exists-p
612 (eq frame-A (ediff-selected-frame))))
613 (and (not (ediff-frame-live-p frame-A))
614 (or ctl-frame-exists-p
615 (eq frame-B (ediff-selected-frame))))))
616 wind-A-start wind-B-start job-name
617 designated-minibuffer-frame
618 done-A done-B done-C)
619
620 (ediff-eval-in-buffer control-buf
621 (setq wind-A-start (ediff-overlay-start
622 (ediff-get-value-according-to-buffer-type
623 'A ediff-narrow-bounds))
624 wind-B-start (ediff-overlay-start
625 (ediff-get-value-according-to-buffer-type
626 'B ediff-narrow-bounds))
627 job-name ediff-job-name))
628
629 (if (and (window-live-p wind-A) (null use-same-frame)) ; buf-A on its own
630 (progn
631 (select-window wind-A)
632 (delete-other-windows)
633 (switch-to-buffer buf-A)
634 (setq wind-A (selected-window))
635 (setq done-A t)))
636
637 (if (and (window-live-p wind-B) (null use-same-frame)) ; buf B on its own
638 (progn
639 (select-window wind-B)
640 (delete-other-windows)
641 (switch-to-buffer buf-B)
642 (setq wind-B (selected-window))
643 (setq done-B t)))
644
645 (if (and (window-live-p wind-C) (null use-same-frame)) ; buf C on its own
646 (progn
647 (select-window wind-C)
648 (delete-other-windows)
649 (switch-to-buffer buf-C)
650 (setq wind-C (selected-window))
651 (setq done-C t)))
652
653 (if use-same-frame
654 (let ((curr-frame (ediff-selected-frame))
655 ;; this affects 3way setups only
656 wind-width-or-height)
657 ;; avoid dedicated and non-splittable windows
658 (ediff-skip-unsuitable-frames)
659 (or (eq curr-frame (ediff-selected-frame))
660 (setq wind-A nil
661 wind-B nil
662 wind-C nil
663 orig-wind (selected-window)))
664
665 ;; set the right frame
666 (cond (wind-A (select-window wind-A))
667 (wind-B (select-window wind-B))
668 (wind-C (select-window wind-C))
669 (t (select-window orig-wind)))
670 (delete-other-windows)
671 (switch-to-buffer buf-A)
672 (setq wind-A (selected-window))
673
674 ;; XEmacs seems to have a lot of trouble with display
675 ;; It won't set things right unless we tell it to catch breath
676 (if ediff-xemacs-p (sit-for 0))
677
678 (if three-way-comparison
679 (setq wind-width-or-height
680 (/
681 (if (eq split-window-function 'split-window-vertically)
682 (window-height wind-A)
683 (window-width wind-A))
684 3)))
685
686 (funcall split-window-function wind-width-or-height)
687 (if (eq (selected-window) wind-A)
688 (other-window 1))
689 (switch-to-buffer buf-B)
690 (setq wind-B (selected-window))
691
692 (if three-way-comparison
693 (progn
694 (funcall split-window-function) ; equally
695 (if (memq (selected-window) (list wind-A wind-B))
696 (other-window 1))
697 (switch-to-buffer buf-C)
698 (setq wind-C (selected-window))))
699 (setq done-A t
700 done-B t
701 done-C t)
702 ))
703
704 (or done-A ; Buf A to be set in its own frame
705 (progn ; It was not set up yet as it wasn't visible
706 (select-window orig-wind)
707 (delete-other-windows)
708 (switch-to-buffer buf-A)
709 (setq wind-A (selected-window))
710 ))
711 (or done-B ; Buf B to be set in its own frame
712 (progn ; It was not set up yet as it wasn't visible
713 (select-window orig-wind)
714 (delete-other-windows)
715 (switch-to-buffer buf-B)
716 (setq wind-B (selected-window))
717 ))
718
719 (if three-way-comparison
720 (or done-C ; Buf C to be set in its own frame
721 (progn ; It was not set up yet as it wasn't visible
722 (select-window orig-wind)
723 (delete-other-windows)
724 (switch-to-buffer buf-C)
725 (setq wind-C (selected-window))
726 )))
727
728 (ediff-eval-in-buffer control-buf
729 (setq ediff-window-A wind-A
730 ediff-window-B wind-B
731 ediff-window-C wind-C)
732
733 (setq frame-A (ediff-window-frame ediff-window-A)
734 designated-minibuffer-frame (ediff-window-frame
735 (minibuffer-window frame-A)))
736 (if ediff-xemacs-p
737 ()
738 (if (not (eq designated-minibuffer-frame default-minibuffer-frame))
739 (progn
740 (setq default-minibuffer-frame designated-minibuffer-frame)
741 (if (ediff-frame-live-p ediff-control-frame)
742 (ediff-delete-frame ediff-control-frame)))
743 ))
744 )
745
746 ;; It is unlikely that we'll implement ediff-windows that would compare
747 ;; 3 windows at once. So, we don't use buffer C here.
748 (if (eq job-name 'ediff-windows)
749 (progn
750 (set-window-start wind-A wind-A-start)
751 (set-window-start wind-B wind-B-start)))
752
753 (ediff-setup-control-frame control-buf)
754 ))
755
756 ;; skip unsplittable and dedicated windows
757 ;; create a new splittable frame if none is found
758 (defun ediff-skip-unsuitable-frames (&optional ok-unsplittable)
759 (if window-system
760 (let (last-window)
761 (while (and (not (eq (selected-window) last-window))
762 (or
763 (window-dedicated-p (selected-window))
764 (ediff-frame-iconified-p (ediff-selected-frame))
765 (if ok-unsplittable
766 nil
767 (ediff-frame-unsplittable-p (ediff-selected-frame)))))
768 ;; remember where started
769 (or last-window (setq last-window (selected-window)))
770 ;; try new window
771 (other-window 1 t))
772 (if (eq (selected-window) last-window)
773 ;; fed up, no appropriate frame
774 (progn
775 (redraw-display)
776 (ediff-select-frame (ediff-make-frame '((unsplittable)))))))))
777
778 ;; Prepare or refresh control frame
779 (defun ediff-setup-control-frame (ctl-buffer)
780 (let ((window-min-height 1)
781 ctl-frame-iconified-p dont-iconify-ctl-frame deiconify-ctl-frame
782 ctl-frame old-ctl-frame lines
783 fheight fwidth adjusted-parameters)
784
785 (ediff-eval-in-buffer ctl-buffer
786 (run-hooks 'ediff-before-setup-control-frame-hooks))
787
788 (setq old-ctl-frame (ediff-eval-in-buffer ctl-buffer ediff-control-frame))
789 (if (ediff-frame-live-p old-ctl-frame)
790 (setq ctl-frame old-ctl-frame)
791 (redraw-display)
792 ;; frame should be made while ctl-buff is current, so that
793 ;; the local default-minibuffer-frame will be consulted and
794 ;; that ediff-control-frame-parameters will have the right value.
795 (ediff-eval-in-buffer ctl-buffer
796 (setq ctl-frame (ediff-make-frame
797 ediff-control-frame-parameters)))
798 (ediff-eval-in-buffer ctl-buffer (setq ediff-control-frame ctl-frame)))
799
800 (setq ctl-frame-iconified-p (ediff-frame-iconified-p ctl-frame))
801 (ediff-select-frame ctl-frame)
802 (if (window-dedicated-p (selected-window))
803 ()
804 (delete-other-windows)
805 (switch-to-buffer ctl-buffer))
806
807 ;; must be before ediff-setup-control-buffer
808 (if ediff-xemacs-p
809 ;; just a precaution--we should be in ctl-buffer already
810 (ediff-eval-in-buffer ctl-buffer
811 (make-local-variable 'screen-title-format)
812 (make-local-variable 'screen-icon-title-format)))
813
814 (ediff-setup-control-buffer ctl-buffer)
815 (setq dont-iconify-ctl-frame
816 (not (string= ediff-help-message ediff-brief-help-message)))
817 (setq deiconify-ctl-frame
818 (and (eq this-command 'ediff-toggle-help)
819 dont-iconify-ctl-frame))
820
821 ;; 1 more line for the modeline
822 (setq lines (if ediff-xemacs-p
823 (+ 2 (count-lines (point-min) (point-max)))
824 (1+ (count-lines (point-min) (point-max))))
825 fheight lines
826 fwidth (+ (ediff-help-message-line-length) 2)
827 adjusted-parameters (append (list
828 (cons 'width fwidth)
829 (cons 'height fheight))
830 (funcall
831 ediff-control-frame-position-function
832 ctl-buffer fwidth fheight)))
833
834 ;; In XEmacs, buffer menubar needs to be killed before frame parameters
835 ;; are changed XEmacs needs to redisplay, as it has trouble setting
836 ;; height correctly otherwise.
837 (if (and ediff-xemacs-p (ediff-frame-has-menubar))
838 (progn (set-buffer-menubar nil)(sit-for 0)))
839
840 ;; Under OS/2 (emx) we have to call modify frame parameters twice, in
841 ;; order to make sure that at least once we do it for non-iconified
842 ;; frame. If appears that in the OS/2 port of Emacs, one can't modify
843 ;; frame parameters of iconified frames.
844 (if (eq system-type 'emx)
845 (ediff-modify-frame-parameters ctl-frame adjusted-parameters))
846
847 (goto-char (point-min))
848
849 (cond ((and ediff-prefer-iconified-control-frame
850 (not ctl-frame-iconified-p)
851 (not dont-iconify-ctl-frame))
852 (ediff-iconify-frame ctl-frame))
853 ((or deiconify-ctl-frame
854 (not ctl-frame-iconified-p))
855 (ediff-raise-frame ctl-frame)))
856
857 ;; This works around a bug in 19.25 and earlier. There, if frame gets
858 ;; iconified, the current buffer changes to that of the frame that
859 ;; becomes exposed as a result of this iconification.
860 ;; So, we make sure the current buffer doesn't change.
861 (ediff-select-frame ctl-frame)
862 (ediff-refresh-control-frame)
863
864 (ediff-modify-frame-parameters ctl-frame adjusted-parameters)
865
866 (if ediff-xemacs-p
867 (set-window-buffer-dedicated (selected-window) ctl-buffer)
868 (set-window-dedicated-p (selected-window) t))
869
870 (or ediff-xemacs-p (sit-for 0 200)) ; emacs has trouble here, needs time
871 (or (ediff-frame-iconified-p ctl-frame)
872 (set-mouse-position ctl-frame 1 0))
873 (if (ediff-check-version '< 19 11 'xemacs) (sit-for 0))
874 (or ediff-xemacs-p (unfocus-frame))
875
876 (if ediff-xemacs-p
877 (ediff-eval-in-buffer ctl-buffer
878 (make-local-variable 'select-screen-hook)
879 (add-hook 'select-screen-hook 'ediff-xemacs-select-screen-hook)
880 ))
881
882 (ediff-eval-in-buffer ctl-buffer
883 (run-hooks 'ediff-after-setup-control-frame-hooks))
884
885 ))
886
887
888 ;; finds a good place to clip control frame
889 (defun ediff-make-frame-position (ctl-buffer ctl-frame-width ctl-frame-height)
890 (ediff-eval-in-buffer ctl-buffer
891 (let* ((frame-A (ediff-window-frame ediff-window-A))
892 (frame-A-parameters (ediff-frame-parameters frame-A))
893 (frame-A-top (cdr (assoc 'top frame-A-parameters)))
894 (frame-A-left (cdr (assoc 'left frame-A-parameters)))
895 (frame-A-width (ediff-frame-width frame-A))
896 (ctl-frame ediff-control-frame)
897 horizontal-adjustment upward-adjustment
898 ctl-frame-top)
899
900 ;; Multiple control frames are clipped based on the value of
901 ;; ediff-control-buffer-number. This is done in order not to obscure
902 ;; other active control panels.
903 (setq horizontal-adjustment (* 2 ediff-control-buffer-number)
904 upward-adjustment (* -14 ediff-control-buffer-number))
905
906 (setq ctl-frame-top (- frame-A-top
907 upward-adjustment
908 ediff-control-frame-upward-shift))
909 (list
910 (cons 'top (if (> ctl-frame-top 0) ctl-frame-top 1))
911 (cons 'left (+ frame-A-left
912 (if ediff-prefer-long-help-message
913 (* (ediff-frame-char-width ctl-frame)
914 (+ ediff-wide-control-frame-rightward-shift
915 horizontal-adjustment))
916 (- (* frame-A-width
917 (ediff-frame-char-width frame-A))
918 (* (ediff-frame-char-width ctl-frame)
919 (+ ctl-frame-width
920 ediff-narrow-control-frame-leftward-shift
921 horizontal-adjustment))))))))))
922
923 (defun ediff-xemacs-select-screen-hook ()
924 (if (equal (ediff-selected-frame) ediff-control-frame)
925 (ediff-raise-frame ediff-control-frame)))
926
927 (defun ediff-make-wide-display ()
928 "Construct an alist of parameters for the wide display.
929 Saves the old frame parameters in `ediff-wide-display-orig-parameters'.
930 The frame to be resized is kept in `ediff-wide-display-frame'.
931 This function modifies only the left margin and the width of the display.
932 It assumes that it is called from within the control buffer."
933 (if (not (fboundp 'x-display-pixel-width))
934 (error
935 "Can't determine display width. Please upgrade your version of Emacs"))
936 (let* ((frame-A (ediff-window-frame ediff-window-A))
937 (frame-A-params (ediff-frame-parameters frame-A))
938 (cw (ediff-frame-char-width frame-A))
939 (wd (- (/ (x-display-pixel-width) cw) 5)))
940 (setq ediff-wide-display-orig-parameters
941 (list (cons 'left (max 0 (cdr (assoc 'left frame-A-params))))
942 (cons 'width (cdr (assoc 'width frame-A-params))))
943 ediff-wide-display-frame frame-A)
944 (ediff-modify-frame-parameters frame-A (list (cons 'left cw)
945 (cons 'width wd)))))
946
947
948
949 ;; Revise the mode line to display which difference we have selected
950 ;; Also resets modelines of buffers A/B, since they may be clobbered by
951 ;; anothe invocations of Ediff.
952 (defun ediff-refresh-mode-lines ()
953 (let (buf-A-state-diff buf-B-state-diff buf-C-state-diff buf-C-state-merge)
954
955 (if (ediff-valid-difference-p)
956 (setq
957 buf-C-state-diff (ediff-get-state-of-diff ediff-current-difference 'C)
958 buf-C-state-merge (ediff-get-state-of-merge ediff-current-difference)
959 buf-A-state-diff (ediff-get-state-of-diff ediff-current-difference 'A)
960 buf-B-state-diff (ediff-get-state-of-diff ediff-current-difference 'B)
961 buf-A-state-diff (if buf-A-state-diff
962 (format "[%s] " buf-A-state-diff)
963 "")
964 buf-B-state-diff (if buf-B-state-diff
965 (format "[%s] " buf-B-state-diff)
966 "")
967 buf-C-state-diff (if (and (ediff-buffer-live-p ediff-buffer-C)
968 (or buf-C-state-diff buf-C-state-merge))
969 (format "[%s%s] "
970 (or buf-C-state-diff "")
971 (if buf-C-state-merge
972 (concat " " buf-C-state-merge)
973 ""))
974 ""))
975 (setq buf-A-state-diff ""
976 buf-B-state-diff ""
977 buf-C-state-diff ""))
978
979 ;; control buffer format
980 (setq mode-line-format
981 (list (if (ediff-narrow-control-frame-p) " " "-- ")
982 mode-line-buffer-identification
983 " Howdy!"))
984 ;; control buffer id
985 (setq mode-line-buffer-identification
986 (if (ediff-narrow-control-frame-p)
987 (ediff-make-narrow-control-buffer-id 'skip-name)
988 (ediff-make-wide-control-buffer-id)))
989 ;; Force mode-line redisplay
990 (force-mode-line-update)
991
992 (if (and window-system (ediff-frame-live-p ediff-control-frame))
993 (ediff-refresh-control-frame))
994
995 (ediff-eval-in-buffer ediff-buffer-A
996 (setq ediff-diff-status buf-A-state-diff)
997 (ediff-strip-mode-line-format)
998 (setq mode-line-format
999 (list " A: " 'ediff-diff-status mode-line-format))
1000 (force-mode-line-update))
1001 (ediff-eval-in-buffer ediff-buffer-B
1002 (setq ediff-diff-status buf-B-state-diff)
1003 (ediff-strip-mode-line-format)
1004 (setq mode-line-format
1005 (list " B: " 'ediff-diff-status mode-line-format))
1006 (force-mode-line-update))
1007 (if ediff-3way-job
1008 (ediff-eval-in-buffer ediff-buffer-C
1009 (setq ediff-diff-status buf-C-state-diff)
1010 (ediff-strip-mode-line-format)
1011 (setq mode-line-format
1012 (list " C: " 'ediff-diff-status mode-line-format))
1013 (force-mode-line-update)))
1014 ))
1015
1016
1017 (defun ediff-refresh-control-frame ()
1018 (if ediff-xemacs-p
1019 (progn
1020 (setq screen-title-format (ediff-make-narrow-control-buffer-id)
1021 screen-icon-title-format (ediff-make-narrow-control-buffer-id))
1022 ;; this forces update of the screen title
1023 (ediff-modify-frame-parameters ediff-control-frame '(())))
1024 (ediff-modify-frame-parameters
1025 ediff-control-frame
1026 (list (cons 'name (ediff-make-narrow-control-buffer-id))))
1027 ))
1028
1029
1030 (defun ediff-make-narrow-control-buffer-id (&optional skip-name)
1031 (concat
1032 (if skip-name
1033 " "
1034 (concat
1035 (cdr (assoc 'name ediff-control-frame-parameters))
1036 ediff-control-buffer-suffix))
1037 (cond ((< ediff-current-difference 0)
1038 (format " _/%d" ediff-number-of-differences))
1039 ((>= ediff-current-difference ediff-number-of-differences)
1040 (format " $/%d" ediff-number-of-differences))
1041 (t
1042 (format " %d/%d"
1043 (1+ ediff-current-difference)
1044 ediff-number-of-differences)))))
1045
1046 (defun ediff-make-wide-control-buffer-id ()
1047 (cond ((< ediff-current-difference 0)
1048 (list (format "%%b At start of %d diffs"
1049 ediff-number-of-differences)))
1050 ((>= ediff-current-difference ediff-number-of-differences)
1051 (list (format "%%b At end of %d diffs"
1052 ediff-number-of-differences)))
1053 (t
1054 (list (format "%%b diff %d of %d"
1055 (1+ ediff-current-difference)
1056 ediff-number-of-differences)))))
1057
1058
1059
1060 ;; If buff is not live, return nil
1061 (defun ediff-get-visible-buffer-window (buff)
1062 (if (ediff-buffer-live-p buff)
1063 (if ediff-xemacs-p
1064 (get-buffer-window buff t)
1065 (get-buffer-window buff 'visible))))
1066
1067 ;;; Functions to decide when to redraw windows
1068
1069
1070 (defun ediff-keep-window-config (control-buf)
1071 (and (eq control-buf (current-buffer))
1072 (/= (buffer-size) 0)
1073 (ediff-eval-in-buffer control-buf
1074 (let ((ctl-wind ediff-control-window)
1075 (A-wind ediff-window-A)
1076 (B-wind ediff-window-B)
1077 (C-wind ediff-window-C))
1078
1079 (and
1080 (ediff-window-visible-p A-wind)
1081 (ediff-window-visible-p B-wind)
1082 ;; if buffer C is defined then take it into account
1083 (or (not ediff-3way-job)
1084 (ediff-window-visible-p C-wind))
1085 (eq (window-buffer A-wind) ediff-buffer-A)
1086 (eq (window-buffer B-wind) ediff-buffer-B)
1087 (or (not ediff-3way-job)
1088 (eq (window-buffer C-wind) ediff-buffer-C))
1089 (string= ediff-window-config-saved
1090 (format "%S%S%S%S%S"
1091 ctl-wind A-wind B-wind C-wind
1092 ediff-split-window-function)))))))
1093
1094
1095 (provide 'ediff-wind)
1096
1097
1098 ;;; ediff-wind.el ends here