;;; window.el --- GNU Emacs window commands aside from those written in C
;; Copyright (C) 1985, 1989, 1992, 1993, 1994, 2000, 2001, 2002,
-;; 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
+;; 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
;; Maintainer: FSF
;; Keywords: internal
BUFFER in any way it likes. All this is done by the function
found in `special-display-function'.
-If the specified frame parameters include (same-buffer . t), the
-buffer is displayed in the currently selected window. Otherwise, if
-they include (same-frame . t), the buffer is displayed in a new window
-in the currently selected frame.
+If the specified frame parameters include (same-window . t), the
+buffer is displayed in the currently selected window. Otherwise,
+if they include (same-frame . t), the buffer is displayed in a
+new window in the currently selected frame.
If this variable appears \"not to work\", because you add a name to it
but that buffer still appears in the selected window, look at the
the buffer in any way it likes. All this is done by the function
found in `special-display-function'.
-If the specified frame parameters include (same-buffer . t), the
+If the specified frame parameters include (same-window . t), the
buffer is displayed in the currently selected window. Otherwise,
if they include (same-frame . t), the buffer is displayed in a
new window in the currently selected frame.
using `special-display-frame-alist' to specify the frame
parameters.
-But if the buffer specific data includes (same-buffer . t) then
-the buffer is displayed in the current selected window.
-Otherwise if it includes (same-frame . t) then the buffer is
+But if the buffer specific data includes (same-window . t) then
+the buffer is displayed in the currently selected window.
+Otherwise, if it includes (same-frame . t) then the buffer is
displayed in a new window in the currently selected frame.
A buffer is special if it is listed in
(not (last-nonminibuffer-frame)))
0)
(last-nonminibuffer-frame))))
- (and (setq window-to-use (get-buffer-window buffer frames))
- (or can-use-selected-window
- (not (eq (selected-window) window-to-use)))))
- ;; If the buffer is already displayed in some window use that.
+ (setq window-to-use
+ (catch 'found
+ ;; Search frames for a window displaying BUFFER. Return
+ ;; the selected window only if we are allowed to do so.
+ (dolist (window (get-buffer-window-list buffer 'nomini frames))
+ (when (or can-use-selected-window
+ (not (eq (selected-window) window)))
+ (throw 'found window))))))
+ ;; The buffer is already displayed in some window; use that.
(window--display-buffer-1 window-to-use))
((and special-display-function
;; `special-display-p' returns either t or a list of frame
(window--try-to-split-window
(get-lru-window frame-to-use t))))
(window--display-buffer-2 buffer window-to-use)))
- ((setq window-to-use
- ;; Reuse an existing window.
- (or (get-lru-window frame-to-use)
- (get-buffer-window buffer 'visible)
- (get-largest-window 'visible nil)
- (get-buffer-window buffer 0)
- (get-largest-window 0 nil)
- (frame-selected-window (funcall pop-up-frame-function))))
+ ((let ((window-to-undedicate
+ ;; When NOT-THIS-WINDOW is non-nil, temporarily dedicate
+ ;; the selected window to its buffer, to avoid that some of
+ ;; the `get-' routines below choose it. (Bug#1415)
+ (and not-this-window (not (window-dedicated-p))
+ (set-window-dedicated-p (selected-window) t)
+ (selected-window))))
+ (unwind-protect
+ (setq window-to-use
+ ;; Reuse an existing window.
+ (or (get-lru-window frame-to-use)
+ (let ((window (get-buffer-window buffer 'visible)))
+ (unless (and not-this-window
+ (eq window (selected-window)))
+ window))
+ (get-largest-window 'visible)
+ (let ((window (get-buffer-window buffer 0)))
+ (unless (and not-this-window
+ (eq window (selected-window)))
+ window))
+ (get-largest-window 0)
+ (frame-selected-window (funcall pop-up-frame-function))))
+ (when (window-live-p window-to-undedicate)
+ ;; Restore dedicated status of selected window.
+ (set-window-dedicated-p window-to-undedicate nil))))
(window--even-window-heights window-to-use)
(window--display-buffer-2 buffer window-to-use)))))
\f
(defun set-window-text-height (window height)
- "Sets the height in lines of the text display area of WINDOW to HEIGHT.
+ "Set the height in lines of the text display area of WINDOW to HEIGHT.
HEIGHT doesn't include the mode line or header line, if any, or
any partial-height lines in the text display area.
"Adjust height of WINDOW to display its buffer's contents exactly.
WINDOW defaults to the selected window.
Optional argument MAX-HEIGHT specifies the maximum height of the
-window and defaults to the height of WINDOW's frame.
+window and defaults to the maximum permissible height of a window
+on WINDOW's frame.
Optional argument MIN-HEIGHT specifies the minimum height of the
window and defaults to `window-min-height'.
Both, MAX-HEIGHT and MIN-HEIGHT are specified in lines and
include the mode line and header line, if any.
-Always return nil."
- (interactive)
- (when (null window)
- (setq window (selected-window)))
- (when (null max-height)
- (setq max-height (frame-height (window-frame window))))
-
- (let* ((buf
- ;; Buffer that is displayed in WINDOW
- (window-buffer window))
- (window-height
- ;; The current height of WINDOW
- (window-height window))
- (desired-height
- ;; The height necessary to show the buffer displayed by WINDOW
- ;; (`count-screen-lines' always works on the current buffer).
- (with-current-buffer buf
- (+ (count-screen-lines)
- ;; If the buffer is empty, (count-screen-lines) is
- ;; zero. But, even in that case, we need one text line
- ;; for cursor.
- (if (= (point-min) (point-max))
- 1 0)
- ;; For non-minibuffers, count the mode-line, if any
- (if (and (not (window-minibuffer-p window))
- mode-line-format)
- 1 0)
- ;; Count the header-line, if any
- (if header-line-format 1 0))))
- (delta
- ;; Calculate how much the window height has to change to show
- ;; desired-height lines, constrained by MIN-HEIGHT and MAX-HEIGHT.
- (- (max (min desired-height max-height)
- (or min-height window-min-height))
- window-height)))
-
- ;; Don't try to redisplay with the cursor at the end
- ;; on its own line--that would force a scroll and spoil things.
- (when (with-current-buffer buf
- (and (eobp) (bolp) (not (bobp))))
- (set-window-point window (1- (window-point window))))
-
- (save-selected-window
- (select-window window 'norecord)
-
- ;; Adjust WINDOW to the nominally correct size (which may actually
- ;; be slightly off because of variable height text, etc).
- (unless (zerop delta)
- (enlarge-window delta))
-
- ;; Check if the last line is surely fully visible. If not,
- ;; enlarge the window.
- (let ((end (with-current-buffer buf
- (save-excursion
- (goto-char (point-max))
- (when (and (bolp) (not (bobp)))
- ;; Don't include final newline
- (backward-char 1))
- (when truncate-lines
- ;; If line-wrapping is turned off, test the
- ;; beginning of the last line for visibility
- ;; instead of the end, as the end of the line
- ;; could be invisible by virtue of extending past
- ;; the edge of the window.
- (forward-line 0))
- (point)))))
- (set-window-vscroll window 0)
- (while (and (< desired-height max-height)
- (= desired-height (window-height window))
- (not (pos-visible-in-window-p end window)))
- (enlarge-window 1)
- (setq desired-height (1+ desired-height)))))))
+Return non-nil if height was orderly adjusted, nil otherwise.
+
+Caution: This function can delete WINDOW and/or other windows
+when their height shrinks to less than MIN-HEIGHT."
+ (interactive)
+ ;; Do all the work in WINDOW and its buffer and restore the selected
+ ;; window and the current buffer when we're done.
+ (let ((old-buffer (current-buffer))
+ value)
+ (with-selected-window (or window (setq window (selected-window)))
+ (set-buffer (window-buffer))
+ ;; Use `condition-case' to handle any fixed-size windows and other
+ ;; pitfalls nearby.
+ (condition-case nil
+ (let* (;; MIN-HEIGHT must not be less than 1 and defaults to
+ ;; `window-min-height'.
+ (min-height (max (or min-height window-min-height) 1))
+ (max-window-height
+ ;; Maximum height of any window on this frame.
+ (min (window-height (frame-root-window)) (frame-height)))
+ ;; MAX-HEIGHT must not be larger than max-window-height and
+ ;; defaults to max-window-height.
+ (max-height
+ (min (or max-height max-window-height) max-window-height))
+ (desired-height
+ ;; The height necessary to show all of WINDOW's buffer,
+ ;; constrained by MIN-HEIGHT and MAX-HEIGHT.
+ (max
+ (min
+ ;; For an empty buffer `count-screen-lines' returns zero.
+ ;; Even in that case we need one line for the cursor.
+ (+ (max (count-screen-lines) 1)
+ ;; For non-minibuffers count the mode line, if any.
+ (if (and (not (window-minibuffer-p)) mode-line-format)
+ 1 0)
+ ;; Count the header line, if any.
+ (if header-line-format 1 0))
+ max-height)
+ min-height))
+ (delta
+ ;; How much the window height has to change.
+ (if (= (window-height) (window-height (frame-root-window)))
+ ;; Don't try to resize a full-height window.
+ 0
+ (- desired-height (window-height))))
+ ;; Do something reasonable so `enlarge-window' can make
+ ;; windows as small as MIN-HEIGHT.
+ (window-min-height (min min-height window-min-height)))
+ ;; Don't try to redisplay with the cursor at the end on its
+ ;; own line--that would force a scroll and spoil things.
+ (when (and (eobp) (bolp) (not (bobp)))
+ (set-window-point window (1- (window-point))))
+ ;; Adjust WINDOW's height to the nominally correct one
+ ;; (which may actually be slightly off because of variable
+ ;; height text, etc).
+ (unless (zerop delta)
+ (enlarge-window delta))
+ ;; `enlarge-window' might have deleted WINDOW, so make sure
+ ;; WINDOW's still alive for the remainder of this.
+ ;; Note: Deleting WINDOW is clearly counter-intuitive in
+ ;; this context, but we can't do much about it given the
+ ;; current semantics of `enlarge-window'.
+ (when (window-live-p window)
+ ;; Check if the last line is surely fully visible. If
+ ;; not, enlarge the window.
+ (let ((end (save-excursion
+ (goto-char (point-max))
+ (when (and (bolp) (not (bobp)))
+ ;; Don't include final newline.
+ (backward-char 1))
+ (when truncate-lines
+ ;; If line-wrapping is turned off, test the
+ ;; beginning of the last line for
+ ;; visibility instead of the end, as the
+ ;; end of the line could be invisible by
+ ;; virtue of extending past the edge of the
+ ;; window.
+ (forward-line 0))
+ (point))))
+ (set-window-vscroll window 0)
+ (while (and (< desired-height max-height)
+ (= desired-height (window-height))
+ (not (pos-visible-in-window-p end)))
+ (enlarge-window 1)
+ (setq desired-height (1+ desired-height))))
+ ;; Return non-nil only if nothing "bad" happened.
+ (setq value t)))
+ (error nil)))
+ (when (buffer-live-p old-buffer)
+ (set-buffer old-buffer))
+ value))
(defun window-safely-shrinkable-p (&optional window)
"Return t if WINDOW can be shrunk without shrinking other windows.