GNU Emacs
Emacs
Dashboard

20.6.1 How display-buffer works

The display-buffer command (as well as commands that call it internally) chooses a window to display by following the steps given below. See Choosing a Window for Displaying a
Buffer
in The Emacs Lisp Reference Manual, for details about how to alter this sequence of steps.

  • If the buffer should be displayed in the selected window regardless of other considerations, reuse the selected window. By default, this step is skipped, but you can tell Emacs not to skip it by adding a regular expression matching the buffer’s name together with a reference to the display-buffer-same-window action function (see Action Functions for Buffer
    Display
    in The Emacs Lisp Reference Manual) to the option display-buffer-alist (see Choosing a Window
    for Displaying a Buffer
    in The Emacs Lisp Reference Manual). For example, to display the buffer *scratch* preferably in the selected window write:
(customize-set-variable
'display-buffer-alist
'(("\\*scratch\\*" (display-buffer-same-window))))

By default, display-buffer-alist is nil.

  • Otherwise, if the buffer is already displayed in an existing window, reuse that window. Normally, only windows on the selected frame are considered, but windows on other frames are also reusable if you use the corresponding reusable-frames action alist entry (see Action Alists for Buffer
    Display
    in The Emacs Lisp Reference Manual). See the next step for an example of how to do that.

  • Otherwise, optionally create a new frame and display the buffer there. By default, this step is skipped. To enable it, change the value of the option display-buffer-base-action (see Choosing a Window for Displaying a Buffer in The Emacs Lisp Reference Manual) as follows:

(customize-set-variable
'display-buffer-base-action
'((display-buffer-reuse-window display-buffer-pop-up-frame)
     (reusable-frames . 0)))

This customization will also try to make the preceding step search for a reusable window on all visible or iconified frames.

  • Otherwise, try to create a new window by splitting a window on the selected frame, and display the buffer in that new window.

The split can be either vertical or horizontal, depending on the variables split-height-threshold and split-width-threshold. These variables should have integer values. If split-height-threshold is smaller than the chosen window’s height, the split puts the new window below. Otherwise, if split-width-threshold is smaller than the window’s width, the split puts the new window on the right. If neither condition holds, Emacs tries to split so that the new window is below—but only if the window was not split before (to avoid excessive splitting).

  • Otherwise, display the buffer in a window previously showing it. Normally, only windows on the selected frame are considered, but with a suitable reusable-frames action alist entry (see above) the window may be also on another frame.

  • Otherwise, display the buffer in an existing window on the selected frame.

  • If all the above methods fail for whatever reason, create a new frame and display the buffer there.