comparison lisp/prim/window-xemacs.el @ 155:43dd3413c7c7 r20-3b4

Import from CVS: tag r20-3b4
author cvs
date Mon, 13 Aug 2007 09:39:39 +0200
parents
children 0132846995bd
comparison
equal deleted inserted replaced
154:94141801dd7e 155:43dd3413c7c7
1 ;;; window-xemacs.el --- XEmacs window commands aside from those written in C.
2
3 ;; Copyright (C) 1985, 1989, 1993-94, 1997 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995, 1996 Ben Wing.
5
6 ;; Maintainer: XEmacs Development Team
7 ;; Keywords: extensions
8
9 ;; This file is part of XEmacs.
10
11 ;; XEmacs is free software; you can redistribute it and/or modify it
12 ;; under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; XEmacs is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with XEmacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Synched up with: Not synched.
27
28 ;;; Commentary:
29
30 ;; slb - 5/29/97
31 ;; Split apart from window.el in order to keep that file better in synch
32 ;; with Emacs.
33
34 ;;; Code:
35
36 (defun backward-other-window (arg &optional all-frames device)
37 "Select the ARG'th different window on this frame, going backwards.
38 This is just like calling `other-window' with the arg negated."
39 (interactive "p")
40 (other-window (- arg) all-frames device))
41
42 (defun windows-of-buffer (&optional buffer)
43 "Returns a list of windows that have BUFFER in them.
44 If BUFFER is not specified, the current buffer will be used."
45 (or (bufferp buffer)
46 (if (stringp buffer)
47 (setq buffer (or (get-buffer buffer)
48 (get-file-buffer buffer)))
49 (setq buffer (current-buffer))))
50 (let* ((firstwin (next-window nil nil t))
51 (wind firstwin)
52 (done nil)
53 window-list)
54 (while (not done)
55 (if (eq (window-buffer wind) buffer)
56 (setq window-list (append window-list (list wind))))
57 (setq wind (next-window wind nil t))
58 (setq done (eq wind firstwin)))
59 window-list))
60
61 (defun buffer-in-multiple-windows-p (&optional buffer)
62 "Return t if BUFFER is in multiple windows.
63 If BUFFER is not specified, the current buffer will be used."
64 (setq buffer (or buffer
65 (get-buffer buffer)
66 (get-file-buffer buffer)
67 (current-buffer)))
68 (> (length (windows-of-buffer buffer)) 1))
69
70 (defun window-list (&optional frame minibuf window)
71 "Return a list of windows on FRAME, beginning with WINDOW.
72 FRAME and WINDOW default to the selected ones.
73 Optional second arg MINIBUF t means count the minibuffer window
74 even if not active. If MINIBUF is neither t nor nil it means
75 not to count the minibuffer even if it is active."
76 (setq window (or window (selected-window))
77 frame (or frame (selected-frame)))
78 (if (not (eq (window-frame window) frame))
79 (error "Window must be on frame."))
80 (let ((current-frame (selected-frame))
81 list)
82 (unwind-protect
83 (save-window-excursion
84 (select-frame frame)
85 (walk-windows
86 (function (lambda (cur-window)
87 (if (not (eq window cur-window))
88 (setq list (cons cur-window list)))))
89 minibuf)
90 (setq list (cons window list)))
91 (select-frame current-frame))))
92
93 ;; We used to have set-window-dedicated-p as an obsolete version
94 ;; of set-window-buffer-dedicated, but it really makes more sense
95 ;; this way.
96
97 (make-obsolete 'set-window-buffer-dedicated 'set-window-dedicated-p)
98 (defun set-window-buffer-dedicated (window buffer)
99 "Make WINDOW display BUFFER and be dedicated to that buffer.
100 Then Emacs will not automatically change which buffer appears in WINDOW.
101 If BUFFER is nil, make WINDOW not be dedicated (but don't change which
102 buffer appears in it currently)."
103 (if (bufferp buffer)
104 (set-window-buffer window (get-buffer-create buffer)))
105 (set-window-dedicated-p window (not (null buffer))))
106
107
108 ;; The window-config stack is stored as a list in frame property
109 ;; 'window-config-stack, with the most recent element at the front.
110 ;; When you pop off an element, the popped off element gets put at the
111 ;; front of frame property 'window-config-unpop-stack, so you can
112 ;; retrieve it using unpop-window-configuration.
113
114 (defvar window-config-stack-max 16
115 "*Maximum size of window configuration stack.
116 Start discarding off end if it gets this big.")
117
118 (defun window-config-stack (&optional frame)
119 (or frame (setq frame (selected-frame)))
120 (let ((stack (frame-property frame 'window-config-stack)))
121 (if stack
122 (set-undoable-stack-max stack window-config-stack-max)
123 (progn
124 (setq stack (make-undoable-stack window-config-stack-max))
125 (set-frame-property frame 'window-config-stack stack)))
126 stack))
127
128 (defun push-window-configuration (&optional config)
129 "Push the current window configuration onto the window-config stack.
130 If CONFIG is specified, push it instead of the current window configuration.
131 Each frame has its own window-config stack."
132 (interactive)
133 (let ((wc (or config (current-window-configuration)))
134 (stack (window-config-stack)))
135 (if (or (= 0 (undoable-stack-a-length stack))
136 (not (equal (undoable-stack-a-top stack) wc)))
137 (progn
138 (undoable-stack-push stack wc)
139 ;; kludge.
140 (if (featurep 'toolbar)
141 (set-specifier-dirty-flag default-toolbar))))))
142
143 (defun pop-window-configuration ()
144 "Pop the top window configuration off the window-config stack and set it.
145 Before setting the new window configuration, the current window configuration
146 is pushed onto the \"unpop\" stack.
147 `unpop-window-configuration' undoes what this function does.
148 Each frame has its own window-config and \"unpop\" stack."
149 (interactive)
150 (let ((stack (window-config-stack))
151 (wc (current-window-configuration))
152 popped)
153 (condition-case nil
154 (progn
155 (setq popped (undoable-stack-pop stack))
156 (while (equal popped wc)
157 (setq popped (undoable-stack-pop stack)))
158 (undoable-stack-push stack wc)
159 (undoable-stack-undo stack)
160 (set-window-configuration popped)
161 ;; probably not necessary:
162 (if (featurep 'toolbar)
163 (set-specifier-dirty-flag default-toolbar))
164 popped)
165 (trunc-stack-bottom
166 (error "Bottom of window config stack")))))
167
168 (defun unpop-window-configuration ()
169 "Undo the effect of the most recent `pop-window-configuration'.
170 This does exactly the inverse of what `pop-window-configuration' does:
171 i.e. it pops a window configuration off of the \"unpop\" stack and
172 pushes the current window configuration onto the window-config stack.
173 Each frame has its own window-config and \"unpop\" stack."
174 (interactive)
175 (let ((stack (window-config-stack))
176 (wc (current-window-configuration))
177 popped)
178 (condition-case nil
179 (progn
180 (setq popped
181 (progn
182 (undoable-stack-redo stack)
183 (undoable-stack-pop stack)))
184 (while (equal popped wc)
185 (setq popped
186 (progn
187 (undoable-stack-redo stack)
188 (undoable-stack-pop stack))))
189 (undoable-stack-push stack wc)
190 (set-window-configuration popped)
191 ;; probably not necessary:
192 (if (featurep 'toolbar)
193 (set-specifier-dirty-flag default-toolbar))
194 popped)
195 (trunc-stack-bottom
196 (error "Top of window config stack")))))
197
198
199 ;;;;;;;;;;;;; display-buffer, moved here from C. Hallelujah.
200
201 (defvar display-buffer-function nil
202 "If non-nil, function to call to handle `display-buffer'.
203 It will receive three args: the same as those to `display-buffer'.")
204
205 (defvar pre-display-buffer-function nil
206 "If non-nil, function that will be called from `display-buffer'
207 as the first action. It will receive three args: the same as those
208 to `display-buffer'.
209 This function may be used to select an appropriate frame for the buffer,
210 for example. See also the variable `display-buffer-function', which may
211 be used to completely replace the `display-buffer' function.
212 If the return value of this function is non-nil, it should be a frame,
213 and that frame will be used to display the buffer.")
214
215 (defvar pop-up-frames nil
216 "*Non-nil means `display-buffer' should make a separate frame.")
217
218 (defvar pop-up-frame-function nil
219 "Function to call to handle automatic new frame creation.
220 It is called with no arguments and should return a newly created frame.
221
222 A typical value might be `(lambda () (new-frame pop-up-frame-alist))'
223 where `pop-up-frame-alist' would hold the default frame parameters.")
224
225 (defvar special-display-buffer-names nil
226 "*List of buffer names that should have their own special frames.
227 Displaying a buffer whose name is in this list makes a special frame for it
228 using `special-display-function'.
229
230 An element of the list can be a cons cell instead of just a string.
231 Then the car should be a buffer name, and the cdr specifies frame
232 parameters for creating the frame for that buffer.
233 More precisely, the cdr is passed as the second argument to
234 the function found in `special-display-function', when making that frame.
235 See also `special-display-regexps'.")
236
237 (defvar special-display-regexps nil
238 "*List of regexps saying which buffers should have their own special frames.
239 If a buffer name matches one of these regexps, it gets its own frame.
240 Displaying a buffer whose name is in this list makes a special frame for it
241 using `special-display-function'.
242
243 An element of the list can be a cons cell instead of just a string.
244 Then the car should be the regexp, and the cdr specifies frame
245 parameters for creating the frame for buffers that match.
246 More precisely, the cdr is passed as the second argument to
247 the function found in `special-display-function', when making that frame.
248 See also `special-display-buffer-names'.")
249
250 (defvar special-display-function nil
251 "Function to call to make a new frame for a special buffer.
252 It is called with two arguments, the buffer and optional buffer specific
253 data, and should return a window displaying that buffer.
254 The default value makes a separate frame for the buffer,
255 using `special-display-frame-alist' to specify the frame parameters.
256
257 A buffer is special if its is listed in `special-display-buffer-names'
258 or matches a regexp in `special-display-regexps'.")
259
260 (defvar same-window-buffer-names nil
261 "*List of buffer names that should appear in the selected window.
262 Displaying one of these buffers using `display-buffer' or `pop-to-buffer'
263 switches to it in the selected window, rather than making it appear
264 in some other window.
265
266 An element of the list can be a cons cell instead of just a string.
267 Then the car must be a string, which specifies the buffer name.
268 This is for compatibility with `special-display-buffer-names';
269 the cdr of the cons cell is ignored.
270
271 See also `same-window-regexps'.")
272
273 (defvar same-window-regexps nil
274 "*List of regexps saying which buffers should appear in the selected window.
275 If a buffer name matches one of these regexps, then displaying it
276 using `display-buffer' or `pop-to-buffer' switches to it
277 in the selected window, rather than making it appear in some other window.
278
279 An element of the list can be a cons cell instead of just a string.
280 Then the car must be a string, which specifies the buffer name.
281 This is for compatibility with `special-display-buffer-names';
282 the cdr of the cons cell is ignored.
283
284 See also `same-window-buffer-names'.")
285
286 (defvar pop-up-windows t
287 "*Non-nil means display-buffer should make new windows.")
288
289 (defvar split-height-threshold 500
290 "*display-buffer would prefer to split the largest window if this large.
291 If there is only one window, it is split regardless of this value.")
292
293 (defvar split-width-threshold 500
294 "*display-buffer would prefer to split the largest window if this large.
295 If there is only one window, it is split regardless of this value.")
296
297 ;; Deiconify the frame containing the window WINDOW, then return WINDOW.
298
299 (defun display-buffer-1 (window)
300 (if (frame-iconified-p (window-frame window))
301 (make-frame-visible (window-frame window)))
302 window)
303
304 ;; Can you believe that all of this crap was formerly in C?
305 ;; Praise Jesus that it's not there any more.
306
307 (defun display-buffer (buffer &optional not-this-window-p override-frame)
308 "Make BUFFER appear in some window on the current frame, but don't select it.
309 BUFFER can be a buffer or a buffer name.
310 If BUFFER is shown already in some window in the current frame,
311 just uses that one, unless the window is the selected window and
312 NOT-THIS-WINDOW-P is non-nil (interactively, with prefix arg).
313
314 If BUFFER has a dedicated frame, display on that frame instead of
315 the current frame, unless OVERRIDE-FRAME is non-nil.
316
317 If OVERRIDE-FRAME is non-nil, display on that frame instead of
318 the current frame (or the dedicated frame).
319
320 If `pop-up-windows' is non-nil, always use the
321 current frame and create a new window regardless of whether the
322 buffer has a dedicated frame, and regardless of whether
323 OVERRIDE-FRAME was specified.
324
325 If `pop-up-frames' is non-nil, make a new frame if no window shows BUFFER.
326
327 Returns the window displaying BUFFER."
328 (interactive "BDisplay buffer:\nP")
329
330 (let ((wconfig (current-window-configuration))
331 (result
332 ;; We just simulate a `return' in C. This function is way ugly
333 ;; and does `returns' all over the place and there's no sense
334 ;; in trying to rewrite it to be more Lispy.
335 (catch 'done
336 (let (window old-frame target-frame explicit-frame)
337 (setq old-frame (or (last-nonminibuf-frame) (selected-frame)))
338 (setq buffer (get-buffer buffer))
339 (check-argument-type 'bufferp buffer)
340
341 (setq explicit-frame
342 (if pre-display-buffer-function
343 (funcall pre-display-buffer-function buffer
344 not-this-window-p
345 override-frame)))
346
347 ;; Give the user the ability to completely reimplement
348 ;; this function via the `display-buffer-function'.
349 (if display-buffer-function
350 (throw 'done
351 (funcall display-buffer-function buffer
352 not-this-window-p
353 override-frame)))
354
355 ;; If the buffer has a dedicated frame, that takes
356 ;; precedence over the current frame, and over what the
357 ;; pre-display-buffer-function did.
358 (let ((dedi (buffer-dedicated-frame buffer)))
359 (if (frame-live-p dedi) (setq explicit-frame dedi)))
360
361 ;; if override-frame is supplied, that takes precedence over
362 ;; everything. This is gonna look bad if the
363 ;; pre-display-buffer-function raised some other frame
364 ;; already.
365 (if override-frame
366 (progn
367 (check-argument-type 'frame-live-p override-frame)
368 (setq explicit-frame override-frame)))
369
370 (setq target-frame
371 (or explicit-frame
372 (last-nonminibuf-frame)
373 (selected-frame)))
374
375 ;; If we have switched frames, then set not-this-window-p
376 ;; to false. Switching frames means that selected-window
377 ;; is no longer the same as it was on entry -- it's the
378 ;; selected-window of target_frame instead of old_frame,
379 ;; so it's a fine candidate for display.
380 (if (not (eq old-frame target-frame))
381 (setq not-this-window-p nil))
382
383 ;; if it's in the selected window, and that's ok, then we're done.
384 (if (and (not not-this-window-p)
385 (eq buffer (window-buffer (selected-window))))
386 (throw 'done (display-buffer-1 (selected-window))))
387
388 ;; See if the user has specified this buffer should appear
389 ;; in the selected window.
390
391 (if not-this-window-p
392 nil
393
394 (if (or (member (buffer-name buffer) same-window-buffer-names)
395 (assoc (buffer-name buffer) same-window-buffer-names))
396 (progn
397 (switch-to-buffer buffer)
398 (throw 'done (display-buffer-1 (selected-window)))))
399
400 (let ((tem same-window-regexps))
401 (while tem
402 (let ((car (car tem)))
403 (if (or
404 (and (stringp car)
405 (string-match car (buffer-name buffer)))
406 (and (consp car) (stringp (car car))
407 (string-match (car car) (buffer-name buffer))))
408 (progn
409 (switch-to-buffer buffer)
410 (throw 'done (display-buffer-1
411 (selected-window))))))
412 (setq tem (cdr tem)))))
413
414 ;; If pop-up-frames, look for a window showing BUFFER on
415 ;; any visible or iconified frame. Otherwise search only
416 ;; the current frame.
417 (if (and (not explicit-frame)
418 (or pop-up-frames (not (last-nonminibuf-frame))))
419 (setq target-frame 0))
420
421 ;; Otherwise, find some window that it's already in, and
422 ;; return that, unless that window is the selected window
423 ;; and that isn't ok. What a contorted mess!
424 (setq window (get-buffer-window buffer target-frame))
425 (if (and window
426 (or (not not-this-window-p)
427 (not (eq window (selected-window)))))
428 (throw 'done (display-buffer-1 window)))
429
430 ;; Certain buffer names get special handling.
431 (if special-display-function
432 (progn
433 (if (member (buffer-name buffer)
434 special-display-buffer-names)
435 (throw 'done (funcall special-display-function buffer)))
436
437 (let ((tem (assoc (buffer-name buffer)
438 special-display-buffer-names)))
439 (if tem
440 (throw 'done (funcall special-display-function
441 buffer (cdr tem)))))
442
443 (let ((tem special-display-regexps))
444 (while tem
445 (let ((car (car tem)))
446 (if (and (stringp car)
447 (string-match car (buffer-name buffer)))
448 (throw 'done
449 (funcall special-display-function buffer)))
450 (if (and (consp car)
451 (stringp (car car))
452 (string-match (car car)
453 (buffer-name buffer)))
454 (throw 'done (funcall
455 special-display-function buffer
456 (cdr car)))))
457 (setq tem (cdr tem))))))
458
459 ;; If there are no frames open that have more than a minibuffer,
460 ;; we need to create a new frame.
461 (if (or pop-up-frames
462 (null (last-nonminibuf-frame)))
463 (progn
464 (setq window (frame-selected-window
465 (funcall pop-up-frame-function)))
466 (set-window-buffer window buffer)
467 (throw 'done (display-buffer-1 window))))
468
469 ;; Otherwise, make it be in some window, splitting if
470 ;; appropriate/possible. Do not split a window if we are
471 ;; displaying the buffer in a different frame than that which
472 ;; was current when we were called. (It is already in a
473 ;; different window by virtue of being in another frame.)
474 (if (or (and pop-up-windows (eq target-frame old-frame))
475 (eq 'only (frame-property (selected-frame) 'minibuffer))
476 ;; If the current frame is a special display frame,
477 ;; don't try to reuse its windows.
478 (window-dedicated-p (frame-root-window (selected-frame))))
479 (progn
480 (if (eq 'only (frame-property (selected-frame) 'minibuffer))
481 (setq target-frame (last-nonminibuf-frame)))
482
483 ;; Don't try to create a window if would get an error with
484 ;; height.
485 (if (< split-height-threshold (* 2 window-min-height))
486 (setq split-height-threshold (* 2 window-min-height)))
487
488 ;; Same with width.
489 (if (< split-width-threshold (* 2 window-min-width))
490 (setq split-width-threshold (* 2 window-min-width)))
491
492 ;; If the frame we would try to split cannot be split,
493 ;; try other frames.
494 (if (frame-property (if (null target-frame)
495 (selected-frame)
496 (last-nonminibuf-frame))
497 'unsplittable)
498 (setq window
499 ;; Try visible frames first.
500 (or (get-largest-window 'visible)
501 ;; If that didn't work, try iconified frames.
502 (get-largest-window 0)
503 (get-largest-window t)))
504 (setq window (get-largest-window target-frame)))
505
506 ;; If we got a tall enough full-width window that
507 ;; can be split, split it.
508 (if (and window
509 (not (frame-property (window-frame window)
510 'unsplittable))
511 (>= (window-height window) split-height-threshold)
512 (or (>= (window-width window)
513 split-width-threshold)
514 (and (window-leftmost-p window)
515 (window-rightmost-p window))))
516 (setq window (split-window window))
517 (let (upper
518 ;; lower
519 other)
520 (setq window (get-lru-window target-frame))
521 ;; If the LRU window is selected, and big enough,
522 ;; and can be split, split it.
523 (if (and window
524 (not (frame-property (window-frame window)
525 'unsplittable))
526 (or (eq window (selected-window))
527 (not (window-parent window)))
528 (>= (window-height window)
529 (* 2 window-min-height)))
530 (setq window (split-window window)))
531 ;; If get-lru-window returned nil, try other approaches.
532 ;; Try visible frames first.
533 (or window
534 (setq window (or (get-largest-window 'visible)
535 ;; If that didn't work, try
536 ;; iconified frames.
537 (get-largest-window 0)
538 ;; Try invisible frames.
539 (get-largest-window t)
540 ;; As a last resort, make
541 ;; a new frame.
542 (frame-selected-window
543 (funcall
544 pop-up-frame-function)))))
545 ;; If window appears above or below another,
546 ;; even out their heights.
547 (if (window-previous-child window)
548 (setq other (window-previous-child window)
549 ;; lower window
550 upper other))
551 (if (window-next-child window)
552 (setq other (window-next-child window)
553 ;; lower other
554 upper window))
555 ;; Check that OTHER and WINDOW are vertically arrayed.
556 (if (and other
557 (not (= (nth 1 (window-pixel-edges other))
558 (nth 1 (window-pixel-edges window))))
559 (> (window-pixel-height other)
560 (window-pixel-height window)))
561 (enlarge-window (- (/ (+ (window-height other)
562 (window-height window))
563 2)
564 (window-height upper))
565 nil upper)))))
566
567 (setq window (get-lru-window target-frame)))
568
569 ;; Bring the window's previous buffer to the top of the MRU chain.
570 (if (window-buffer window)
571 (save-excursion
572 (save-selected-window
573 (select-window window)
574 (record-buffer (window-buffer window)))))
575
576 (set-window-buffer window buffer)
577
578 (display-buffer-1 window)))))
579 (or (equal wconfig (current-window-configuration))
580 (push-window-configuration wconfig))
581 result))
582
583 ;;; window-xemacs.el ends here