Skip to main content

28.5 Preserving Window Sizes

A window can get resized explicitly by using one of the functions from the preceding section or implicitly, for example, when resizing an adjacent window, when splitting or deleting a window (see Splitting Windows, see Deleting Windows) or when resizing the window’s frame (see Frame Size).

It is possible to avoid implicit resizing of a specific window when there are one or more other resizable windows on the same frame. For this purpose, Emacs must be advised to preserve the size of that window. There are two basic ways to do that.

variable window-size-fixed

If this buffer-local variable is non-nil, the size of any window displaying the buffer cannot normally be changed. Deleting a window or changing the frame’s size may still change the window’s size, if there is no choice.

If the value is height, then only the window’s height is fixed; if the value is width, then only the window’s width is fixed. Any other non-nil value fixes both the width and the height.

If this variable is nil, this does not necessarily mean that any window showing the buffer can be resized in the desired direction. To determine that, use the function window-resizable. See Resizing Windows.

Often window-size-fixed is overly aggressive because it inhibits any attempt to explicitly resize or split an affected window as well. This may even happen after the window has been resized implicitly, for example, when deleting an adjacent window or resizing the window’s frame. The following function tries hard to never disallow resizing such a window explicitly:

function window-preserve-size \&optional window horizontal preserve

This function (un-)marks the height of window window as preserved for future resize operations. window must be a live window and defaults to the selected one. If the optional argument horizontal is non-nil, it (un-)marks the width of window as preserved.

If the optional argument preserve is t, this means to preserve the current height/width of window’s body. The height/width of window will change only if Emacs has no better choice. Resizing a window whose height/width is preserved by this function never throws an error.

If preserve is nil, this means to stop preserving the height/width of window, lifting any respective restraint induced by a previous call of this function for window. Calling enlarge-window, shrink-window or fit-window-to-buffer with window as argument may also remove the respective restraint.

window-preserve-size is currently invoked by the following functions:

fit-window-to-buffer

If the optional argument preserve-size of that function (see Resizing Windows) is non-nil, the size established by that function is preserved.

display-buffer

If the alist argument of that function (see Choosing Window) contains a preserve-size entry, the size of the window produced by that function is preserved.

window-preserve-size installs a window parameter (see Window Parameters) called window-preserved-size which is consulted by the window resizing functions. This parameter will not prevent resizing the window when the window shows another buffer than the one when window-preserve-size was invoked or if its size has changed since then.

The following function can be used to check whether the height of a particular window is preserved:

function window-preserved-size \&optional window horizontal

This function returns the preserved height of window window in pixels. window must be a live window and defaults to the selected one. If the optional argument horizontal is non-nil, it returns the preserved width of window. It returns nil if the size of window is not preserved.