1 ;;; window-xemacs.el --- SXEmacs window commands aside from those written in C.
3 ;; Copyright (C) 1985, 1989, 1993-94, 1997 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995, 1996 Ben Wing.
6 ;; Maintainer: SXEmacs Development Team
7 ;; Keywords: frames, extensions, dumped
9 ;; This file is part of SXEmacs.
11 ;; SXEmacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; SXEmacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Synched up with: Not synched.
28 ;; This file is dumped with SXEmacs.
31 ;; Split apart from window.el in order to keep that file better in synch
37 "Windows within a frame."
40 (defun recenter (&optional n window)
41 "Center point in WINDOW and redisplay frame. With N, put point on line N.
42 The desired position of point is always relative to the window.
43 Just C-u as prefix means put point in the center of the window.
44 No N (i.e., it is nil) erases the entire frame and then
45 redraws with point in the center of the window.
46 If WINDOW is nil, the selected window is used."
48 (center-to-window-line (if (consp n) nil n) window)
50 (redraw-frame (window-frame window) t)))
52 (defun backward-other-window (count &optional which-frames which-devices)
53 "Select the COUNT'th different window on this frame, going backwards.
54 This is just like calling `other-window' with COUNT negated."
56 (other-window (- count) which-frames which-devices))
58 (defalias 'windows-of-buffer 'get-buffer-window-list)
60 (defun buffer-in-multiple-windows-p (&optional buffer)
61 "Return t if BUFFER is in multiple windows.
62 If BUFFER is not specified, the current buffer will be used."
63 (setq buffer (or buffer
65 (get-file-buffer buffer)
67 (> (length (windows-of-buffer buffer)) 1))
69 (defun window-list (&optional frame minibuf window)
70 "Return a list of windows on FRAME, beginning with WINDOW.
71 FRAME and WINDOW default to the selected ones.
72 Optional second arg MINIBUF t means count the minibuffer window
73 even if not active. If MINIBUF is neither t nor nil it means
74 not to count the minibuffer even if it is active."
75 (setq window (or window (selected-window))
76 frame (or frame (selected-frame)))
77 (if (not (eq (window-frame window) frame))
78 (error "Window must be on frame."))
79 (let ((current-frame (selected-frame))
82 (save-window-excursion
85 (function (lambda (cur-window)
86 (if (not (eq window cur-window))
87 (setq list (cons cur-window list)))))
89 (setq list (cons window list)))
90 (select-frame current-frame))))
92 ;; We used to have set-window-dedicated-p as an obsolete version
93 ;; of set-window-buffer-dedicated, but it really makes more sense
96 (make-obsolete 'set-window-buffer-dedicated 'set-window-dedicated-p)
97 (defun set-window-buffer-dedicated (window buffer)
98 "Make WINDOW display BUFFER and be dedicated to that buffer.
99 Then Emacs will not automatically change which buffer appears in WINDOW.
100 If BUFFER is nil, make WINDOW not be dedicated (but don't change which
101 buffer appears in it currently)."
103 (set-window-buffer window (get-buffer-create buffer)))
104 (set-window-dedicated-p window (not (null buffer))))
107 ;; The window-config stack is stored as a list in frame property
108 ;; 'window-config-stack, with the most recent element at the front.
109 ;; When you pop off an element, the popped off element gets put at the
110 ;; front of frame property 'window-config-unpop-stack, so you can
111 ;; retrieve it using unpop-window-configuration.
113 (defcustom window-config-stack-max 16
114 "*Maximum size of window configuration stack.
115 Start discarding off end if it gets this big."
119 (defun window-config-stack (&optional frame)
120 (or frame (setq frame (selected-frame)))
121 (let ((stack (frame-property frame 'window-config-stack)))
123 (set-undoable-stack-max stack window-config-stack-max)
125 (setq stack (make-undoable-stack window-config-stack-max))
126 (set-frame-property frame 'window-config-stack stack)))
129 (defun push-window-configuration (&optional config)
130 "Push the current window configuration onto the window-config stack.
131 If CONFIG is specified, push it instead of the current window configuration.
132 Each frame has its own window-config stack."
134 (let ((wc (or config (current-window-configuration)))
135 (stack (window-config-stack)))
136 (if (or (= 0 (undoable-stack-a-length stack))
137 (not (equal (undoable-stack-a-top stack) wc)))
138 (undoable-stack-push stack wc))))
140 (defun pop-window-configuration ()
141 "Pop the top window configuration off the window-config stack and set it.
142 Before setting the new window configuration, the current window configuration
143 is pushed onto the \"unpop\" stack.
144 `unpop-window-configuration' undoes what this function does.
145 Each frame has its own window-config and \"unpop\" stack."
147 (let ((stack (window-config-stack))
148 (wc (current-window-configuration))
152 (setq popped (undoable-stack-pop stack))
153 (while (equal popped wc)
154 (setq popped (undoable-stack-pop stack)))
155 (undoable-stack-push stack wc)
156 (undoable-stack-undo stack)
157 (set-window-configuration popped)
160 (error "Bottom of window config stack")))))
162 (defun unpop-window-configuration ()
163 "Undo the effect of the most recent `pop-window-configuration'.
164 This does exactly the inverse of what `pop-window-configuration' does:
165 i.e. it pops a window configuration off of the \"unpop\" stack and
166 pushes the current window configuration onto the window-config stack.
167 Each frame has its own window-config and \"unpop\" stack."
169 (let ((stack (window-config-stack))
170 (wc (current-window-configuration))
176 (undoable-stack-redo stack)
177 (undoable-stack-pop stack)))
178 (while (equal popped wc)
181 (undoable-stack-redo stack)
182 (undoable-stack-pop stack))))
183 (undoable-stack-push stack wc)
184 (set-window-configuration popped)
187 (error "Top of window config stack")))))
190 ;;;;;;;;;;;;; display-buffer, moved here from C. Hallelujah.
192 (make-variable-buffer-local '__buffer-dedicated-frame)
194 (defun buffer-dedicated-frame (&optional buffer)
195 "Return the frame dedicated to this BUFFER, or nil if there is none.
196 No argument or nil as argument means use current buffer as BUFFER."
197 (let ((buffer (decode-buffer buffer)))
198 (let ((frame (symbol-value-in-buffer '__buffer-dedicated-frame buffer)))
199 ;; XEmacs addition: if the frame is dead, silently make it go away.
200 (when (and (framep frame) (not (frame-live-p frame)))
201 (with-current-buffer buffer
202 (setq __buffer-dedicated-frame nil))
206 (defun set-buffer-dedicated-frame (buffer frame)
207 "For this BUFFER, set the FRAME dedicated to it.
208 FRAME must be a frame or nil."
209 (let ((buffer (decode-buffer buffer)))
211 (check-argument-type #'frame-live-p frame))
212 (with-current-buffer buffer
213 (setq __buffer-dedicated-frame frame))))
215 (defvar display-buffer-function nil
216 "If non-nil, function to call to handle `display-buffer'.
217 It will receive four args: the same as those to `display-buffer'.")
219 (defvar pre-display-buffer-function nil
220 "If non-nil, function that will be called from `display-buffer'
221 as the first action. It will receive four args: the same as those
223 This function may be used to select an appropriate frame for the buffer,
224 for example. See also the variable `display-buffer-function', which may
225 be used to completely replace the `display-buffer' function.
226 If the return value of this function is non-nil, it should be a frame,
227 and that frame will be used to display the buffer.")
229 (defcustom pop-up-frames nil
230 "*Non-nil means `display-buffer' should make a separate frame."
234 (defvar pop-up-frame-function nil
235 "Function to call to handle automatic new frame creation.
236 It is called with no arguments and should return a newly created frame.
238 A typical value might be `(lambda () (new-frame pop-up-frame-alist))'
239 where `pop-up-frame-alist' would hold the default frame parameters.")
241 (defcustom special-display-buffer-names nil
242 "*List of buffer names that should have their own special frames.
243 Displaying a buffer whose name is in this list makes a special frame for it
244 using `special-display-function'.
246 An element of the list can be a cons cell instead of just a string.
247 Then the car should be a buffer name, and the cdr specifies frame
248 parameters for creating the frame for that buffer.
249 More precisely, the cdr is passed as the second argument to
250 the function found in `special-display-function', when making that frame.
251 See also `special-display-regexps'."
252 :type '(repeat (choice :value ""
254 (cons :menu-tag "Properties"
257 (repeat :tag "Properties"
259 (symbol :tag "Property")
260 (sexp :tag "Value"))))))
263 (defcustom special-display-regexps nil
264 "*List of regexps saying which buffers should have their own special frames.
265 If a buffer name matches one of these regexps, it gets its own frame.
266 Displaying a buffer whose name is in this list makes a special frame for it
267 using `special-display-function'.
269 An element of the list can be a cons cell instead of just a string.
270 Then the car should be the regexp, and the cdr specifies frame
271 parameters for creating the frame for buffers that match.
272 More precisely, the cdr is passed as the second argument to
273 the function found in `special-display-function', when making that frame.
274 See also `special-display-buffer-names'."
275 :type '(repeat (choice :value ""
277 (cons :menu-tag "Properties"
280 (repeat :tag "Properties"
282 (symbol :tag "Property")
283 (sexp :tag "Value"))))))
286 (defvar special-display-function nil
287 "Function to call to make a new frame for a special buffer.
288 It is called with two arguments, the buffer and optional buffer specific
289 data, and should return a window displaying that buffer.
290 The default value makes a separate frame for the buffer,
291 using `special-display-frame-alist' to specify the frame parameters.
293 A buffer is special if its is listed in `special-display-buffer-names'
294 or matches a regexp in `special-display-regexps'.")
296 (defcustom same-window-buffer-names nil
297 "*List of buffer names that should appear in the selected window.
298 Displaying one of these buffers using `display-buffer' or `pop-to-buffer'
299 switches to it in the selected window, rather than making it appear
300 in some other window.
302 An element of the list can be a cons cell instead of just a string.
303 Then the car must be a string, which specifies the buffer name.
304 This is for compatibility with `special-display-buffer-names';
305 the cdr of the cons cell is ignored.
307 See also `same-window-regexps'."
308 :type '(repeat (string :tag "Name"))
311 (defcustom same-window-regexps nil
312 "*List of regexps saying which buffers should appear in the selected window.
313 If a buffer name matches one of these regexps, then displaying it
314 using `display-buffer' or `pop-to-buffer' switches to it
315 in the selected window, rather than making it appear in some other window.
317 An element of the list can be a cons cell instead of just a string.
318 Then the car must be a string, which specifies the buffer name.
319 This is for compatibility with `special-display-buffer-names';
320 the cdr of the cons cell is ignored.
322 See also `same-window-buffer-names'."
323 :type '(repeat regexp)
326 (defcustom pop-up-windows t
327 "*Non-nil means display-buffer should make new windows."
331 (defcustom split-height-threshold 500
332 "*display-buffer would prefer to split the largest window if this large.
333 If there is only one window, it is split regardless of this value."
337 (defcustom split-width-threshold 500
338 "*display-buffer would prefer to split the largest window if this large.
339 If there is only one window, it is split regardless of this value."
343 ;; Deiconify the frame containing the window WINDOW, then return WINDOW.
345 (defun display-buffer-1 (window)
346 (if (frame-iconified-p (window-frame window))
347 (make-frame-visible (window-frame window)))
350 ;; Can you believe that all of this crap was formerly in C?
351 ;; Praise Jesus that it's not there any more.
353 (defun display-buffer (buffer &optional not-this-window-p override-frame
355 "Make BUFFER appear in some window on the current frame, but don't select it.
356 BUFFER can be a buffer or a buffer name.
357 If BUFFER is shown already in some window in the current frame,
358 just uses that one, unless the window is the selected window and
359 NOT-THIS-WINDOW-P is non-nil (interactively, with prefix arg).
361 If BUFFER has a dedicated frame, display on that frame instead of
362 the current frame, unless OVERRIDE-FRAME is non-nil.
364 If OVERRIDE-FRAME is non-nil, display on that frame instead of
365 the current frame (or the dedicated frame).
367 If SHRINK-TO-FIT is non-nil and splitting the window is appropriate, give
368 the new buffer less than half the space if it is small enough to fit.
370 If `pop-up-windows' is non-nil, always use the
371 current frame and create a new window regardless of whether the
372 buffer has a dedicated frame, and regardless of whether
373 OVERRIDE-FRAME was specified.
375 If `pop-up-frames' is non-nil, make a new frame if no window shows BUFFER.
377 If the buffer name is a member of the `same-window-buffer-names' list,
378 or matches one of the `same-window-regexps' expressions, display the
379 buffer in the currently selected window.
381 Returns the window displaying BUFFER."
382 (interactive "BDisplay buffer:\nP")
384 (let ((wconfig (current-window-configuration))
386 ;; We just simulate a `return' in C. This function is way ugly
387 ;; and does `returns' all over the place and there's no sense
388 ;; in trying to rewrite it to be more Lispy.
390 (let (window old-frame target-frame explicit-frame shrink-it)
391 (setq old-frame (or (last-nonminibuf-frame) (selected-frame)))
392 (setq buffer (get-buffer buffer))
393 (check-argument-type 'bufferp buffer)
396 (if pre-display-buffer-function
397 (funcall pre-display-buffer-function buffer
402 ;; Give the user the ability to completely reimplement
403 ;; this function via the `display-buffer-function'.
404 (if display-buffer-function
406 (funcall display-buffer-function buffer
411 ;; If the buffer has a dedicated frame, that takes
412 ;; precedence over the current frame, and over what the
413 ;; pre-display-buffer-function did.
414 (let ((dedi (buffer-dedicated-frame buffer)))
415 (if (frame-live-p dedi) (setq explicit-frame dedi)))
417 ;; if override-frame is supplied, that takes precedence over
418 ;; everything. This is gonna look bad if the
419 ;; pre-display-buffer-function raised some other frame
423 (check-argument-type 'frame-live-p override-frame)
424 (setq explicit-frame override-frame)))
428 (last-nonminibuf-frame)
431 ;; If we have switched frames, then set not-this-window-p
432 ;; to false. Switching frames means that selected-window
433 ;; is no longer the same as it was on entry -- it's the
434 ;; selected-window of target_frame instead of old_frame,
435 ;; so it's a fine candidate for display.
436 (if (not (eq old-frame target-frame))
437 (setq not-this-window-p nil))
439 ;; if it's in the selected window, and that's ok, then we're done.
440 (if (and (not not-this-window-p)
441 (eq buffer (window-buffer (selected-window))))
442 (throw 'done (display-buffer-1 (selected-window))))
444 ;; See if the user has specified this buffer should appear
445 ;; in the selected window.
447 (if not-this-window-p
450 (if (or (member (buffer-name buffer) same-window-buffer-names)
451 (assoc (buffer-name buffer) same-window-buffer-names))
453 (switch-to-buffer buffer)
454 (throw 'done (display-buffer-1 (selected-window)))))
456 (let ((tem same-window-regexps))
458 (let ((car (car tem)))
461 (string-match car (buffer-name buffer)))
462 (and (consp car) (stringp (car car))
463 (string-match (car car) (buffer-name buffer))))
465 (switch-to-buffer buffer)
466 (throw 'done (display-buffer-1
467 (selected-window))))))
468 (setq tem (cdr tem)))))
470 ;; If pop-up-frames, look for a window showing BUFFER on
471 ;; any visible or iconified frame. Otherwise search only
472 ;; the current frame.
473 (if (and (not explicit-frame)
474 (or pop-up-frames (not (last-nonminibuf-frame))))
475 (setq target-frame 0))
477 ;; Otherwise, find some window that it's already in, and
478 ;; return that, unless that window is the selected window
479 ;; and that isn't ok. What a contorted mess!
480 (setq window (or (if (not explicit-frame)
481 ;; search the selected frame
482 ;; first if the user didn't
483 ;; specify an explicit frame.
484 (get-buffer-window buffer nil))
485 (get-buffer-window buffer target-frame)))
487 (or (not not-this-window-p)
488 (not (eq window (selected-window)))))
489 (throw 'done (display-buffer-1 window)))
491 ;; Certain buffer names get special handling.
492 (if special-display-function
494 (if (member (buffer-name buffer)
495 special-display-buffer-names)
496 (throw 'done (funcall special-display-function buffer)))
498 (let ((tem (assoc (buffer-name buffer)
499 special-display-buffer-names)))
501 (throw 'done (funcall special-display-function
504 (let ((tem special-display-regexps))
506 (let ((car (car tem)))
507 (if (and (stringp car)
508 (string-match car (buffer-name buffer)))
510 (funcall special-display-function buffer)))
513 (string-match (car car)
514 (buffer-name buffer)))
515 (throw 'done (funcall
516 special-display-function buffer
518 (setq tem (cdr tem))))))
520 ;; If there are no frames open that have more than a minibuffer,
521 ;; we need to create a new frame.
522 (if (or pop-up-frames
523 (null (last-nonminibuf-frame)))
525 (setq window (frame-selected-window
526 (funcall pop-up-frame-function)))
527 (set-window-buffer window buffer)
528 (throw 'done (display-buffer-1 window))))
530 ;; Otherwise, make it be in some window, splitting if
531 ;; appropriate/possible. Do not split a window if we are
532 ;; displaying the buffer in a different frame than that which
533 ;; was current when we were called. (It is already in a
534 ;; different window by virtue of being in another frame.)
535 (if (or (and pop-up-windows (eq target-frame old-frame))
536 (eq 'only (frame-property (selected-frame) 'minibuffer))
537 ;; If the current frame is a special display frame,
538 ;; don't try to reuse its windows.
539 (window-dedicated-p (frame-root-window (selected-frame))))
541 (if (eq 'only (frame-property (selected-frame) 'minibuffer))
542 (setq target-frame (last-nonminibuf-frame)))
544 ;; Don't try to create a window if would get an error with
546 (if (< split-height-threshold (* 2 window-min-height))
547 (setq split-height-threshold (* 2 window-min-height)))
550 (if (< split-width-threshold (* 2 window-min-width))
551 (setq split-width-threshold (* 2 window-min-width)))
553 ;; If the frame we would try to split cannot be split,
555 (if (frame-property (if (null target-frame)
557 (last-nonminibuf-frame))
560 ;; Try visible frames first.
561 (or (get-largest-window 'visible)
562 ;; If that didn't work, try iconified frames.
563 (get-largest-window 0)
564 (get-largest-window t)))
565 (setq window (get-largest-window target-frame)))
567 ;; If we got a tall enough full-width window that
568 ;; can be split, split it.
570 (not (frame-property (window-frame window)
572 (>= (window-height window) split-height-threshold)
573 (or (>= (window-width window)
574 split-width-threshold)
575 (and (window-leftmost-p window)
576 (window-rightmost-p window))))
577 (setq window (split-window window))
579 (setq window (get-lru-window target-frame))
580 ;; If the LRU window is selected, and big enough,
581 ;; and can be split, split it.
583 (not (frame-property (window-frame window)
585 (or (eq window (selected-window))
586 (not (window-parent window)))
587 (>= (window-height window)
588 (* 2 window-min-height)))
589 (setq window (split-window window)))
590 ;; If get-lru-window returned nil, try other approaches.
591 ;; Try visible frames first.
593 (setq window (or (get-largest-window 'visible)
594 ;; If that didn't work, try
596 (get-largest-window 0)
597 ;; Try invisible frames.
598 (get-largest-window t)
599 ;; As a last resort, make
601 (frame-selected-window
603 pop-up-frame-function)))))
604 ;; If window appears above or below another,
605 ;; even out their heights.
606 (if (window-previous-child window)
607 (setq other (window-previous-child window)
609 (if (window-next-child window)
610 (setq other (window-next-child window)
612 ;; Check that OTHER and WINDOW are vertically arrayed.
614 (not (= (nth 1 (window-pixel-edges other))
615 (nth 1 (window-pixel-edges window))))
616 (> (window-pixel-height other)
617 (window-pixel-height window)))
618 (enlarge-window (- (/ (+ (window-height other)
619 (window-height window))
621 (window-height upper))
623 ;; Klaus Berndl <klaus.berndl@sdm.de>: Only in
624 ;; this situation we shrink-to-fit but we can do
625 ;; this first after we have displayed buffer in
626 ;; window (s.b. (set-window-buffer window buffer))
627 (setq shrink-it shrink-to-fit))))
629 (setq window (get-lru-window target-frame)))
631 ;; Bring the window's previous buffer to the top of the MRU chain.
632 (if (window-buffer window)
634 (save-selected-window
635 (select-window window)
636 (record-buffer (window-buffer window)))))
638 (set-window-buffer window buffer)
640 ;; Now window's previous buffer has been brought to the top
641 ;; of the MRU chain and window displays buffer - now we can
642 ;; shrink-to-fit if necessary
644 (shrink-window-if-larger-than-buffer window))
646 (display-buffer-1 window)))))
647 (or (equal wconfig (current-window-configuration))
648 (push-window-configuration wconfig))
651 ;;; window-xemacs.el ends here