feat: streamline window management

This commit is contained in:
Kiana Sheibani 2024-08-22 18:30:03 -04:00
parent cdff3ea38b
commit ce0301f52c
Signed by: toki
GPG key ID: 6CB106C25E86A9F7

View file

@ -1310,6 +1310,100 @@ If PARENTS is non-nil, the parents of the specified directory will also be creat
"i i" #'nerd-icons-insert) "i i" #'nerd-icons-insert)
#+end_src #+end_src
** Windows
*** Prefer Vertical Split
Emacs has a sophisticated system for controlling how windows and buffers are arranged on the screen, called ~display-buffer~. This works out of the box for almost every case I've thrown at it, except for one issue: it prefers horizontal splitting of windows to vertical splitting, despite the fact that the latter is far better in most cases.
#+begin_src emacs-lisp
(defpatch! nil
(defun split-window-sensibly) (&optional window)
"Split WINDOW in a way suitable for `display-buffer'.
WINDOW defaults to the currently selected window.
If `split-height-threshold' specifies an integer, WINDOW is at
least `split-height-threshold' lines tall and can be split
vertically, split WINDOW into two windows one above the other and
return the lower window. Otherwise, if `split-width-threshold'
specifies an integer, WINDOW is at least `split-width-threshold'
columns wide and can be split horizontally, split WINDOW into two
windows side by side and return the window on the right. If this
can't be done either and WINDOW is the only window on its frame,
try to split WINDOW vertically disregarding any value specified
by `split-height-threshold'. If that succeeds, return the lower
window. Return nil otherwise.
By default `display-buffer' routines call this function to split
the largest or least recently used window. To change the default
customize the option `split-window-preferred-function'.
You can enforce this function to not split WINDOW horizontally,
by setting (or binding) the variable `split-width-threshold' to
nil. If, in addition, you set `split-height-threshold' to zero,
chances increase that this function does split WINDOW vertically.
In order to not split WINDOW vertically, set (or bind) the
variable `split-height-threshold' to nil. Additionally, you can
set `split-width-threshold' to zero to make a horizontal split
more likely to occur.
Have a look at the function `window-splittable-p' if you want to
know how `split-window-sensibly' determines whether WINDOW can be
split."
(let ((window (or window (selected-window))))
(or (el-patch-let
(($fst (and (window-splittable-p window)
;; Split window vertically.
(with-selected-window window
(split-window-below))))
($snd (and (window-splittable-p window t)
;; Split window horizontally.
(with-selected-window window
(split-window-right)))))
(el-patch-swap $fst $snd)
(el-patch-swap $snd $fst))
(and
(let ((frame (window-frame window)))
(or
(eq window (frame-root-window frame))
(catch 'done
(walk-window-tree (lambda (w)
(unless (or (eq w window)
(window-dedicated-p w))
(throw 'done nil)))
frame)
t)))
(not (window-minibuffer-p window))
(let (((el-patch-swap split-height-threshold
split-width-threshold)
0))
(when (window-splittable-p window)
(with-selected-window window
((el-patch-swap split-window-below split-window-right)))))))))
;; Minimum window height and width
(setq split-height-threshold 10
split-width-threshold 60)
#+end_src
*** Window Split Prompt
When a window is split in Emacs, the result is two windows that show the same buffer. This is an odd state for Emacs to be in, and it's annoying to deal with afterwards, so let's fix it so that a buffer prompt is brought up for the new window.
#+begin_src emacs-lisp
;; Select new window instead of old window
(setq evil-vsplit-window-right t
evil-split-window-below t)
(defadvice! ~/evil-window-split-prompt (&rest _)
"Prompt for a buffer to show on a new split window."
:after (list #'evil-window-split #'evil-window-vsplit)
(condition-case error
(+vertico/switch-workspace-buffer t)
(quit (evil-window-delete)
(signal 'quit (cdr error)))))
#+end_src
* Aesthetics * Aesthetics
#+call: confpkg("Visual") #+call: confpkg("Visual")