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