Reverse evaluation order of split-height-threshold and split-width-threshold in Emacs for display-buffer

StackOverflow https://stackoverflow.com/questions/23659909

  •  22-07-2023
  •  | 
  •  

Domanda

When display-buffer has to create a new window in an existing pane, the Emacs manual states that split-height-threshold is looked at first to determine if the new window can be below the current one, then split-width-threshold is evaluated in the same way for side-by-side windows.

Is there a way to make Emacs try first to put the windows side by side first if the width is high enough? I can set split-height-threshold to nil to inhibit vertical split altogether, but that makes Emacs steal another window if the current one is not wide enough.

È stato utile?

Soluzione

You can make Emacs do this by customizing the variable split-window-preferred-function:

(defun my-split-window-sensibly (&optional window)
  (let ((window (or window (selected-window))))
    (or (and (window-splittable-p window t)
             ;; Split window horizontally.
             (with-selected-window window
               (split-window-right)))
        (and (window-splittable-p window)
             ;; Split window vertically.
             (with-selected-window window
               (split-window-below)))
        (and (eq window (frame-root-window (window-frame window)))
             (not (window-minibuffer-p window))
             ;; If WINDOW is the only window on its frame and is not the
             ;; minibuffer window, try to split it horizontally disregarding
             ;; the value of `split-width-threshold'.
             (let ((split-width-threshold 0))
               (when (window-splittable-p window t)
                 (with-selected-window window
                   (split-window-right))))))))

(setq split-window-preferred-function 'my-split-window-sensibly)

The variable split-window-preferred-function

specifies a function for splitting a window, in order to make a new window for displaying a buffer. It is used by the display-buffer-pop-up-window action function to actually split the window.

By default, it is set to split-window-sensibly. The function I am providing above is a modified version of split-window-sensibly (defined in window.el) that simply reverses the steps of the original function, causing Emacs to "prefer" side-by-side window splits over stacked ones.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top