Skip to content

Commit

Permalink
[prot] Customizable window splitting direction
Browse files Browse the repository at this point in the history
  • Loading branch information
pkryger committed Jan 21, 2025
1 parent c39ef79 commit 7bebd02
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 2 deletions.
21 changes: 19 additions & 2 deletions modules/init-helm.el
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,24 @@
("g" . #'helm-google-suggest))
:custom
(helm-split-window-default-side 'other)
(helm-split-window-other-side-when-one-window 'right)

(helm-split-width-threshold
(when (eq exordium-split-window-preffered-direction
'longest)
split-width-threshold))

(helm-split-window-state
(if (memq exordium-split-window-preffered-direction
'(horizontal longest-horizontal))
'horizontal
'vertical))

(helm-split-window-other-side-when-one-window
(if (memq exordium-split-window-preffered-direction
'(horizontal longest-horizontal))
'right
'below))

(helm-buffer-details-flag nil)
(helm-completion-style (cond
((and exordium-helm-fuzzy-match
Expand Down Expand Up @@ -196,7 +213,7 @@
helm-swoop--edit-cancel
helm-swoop--edit-delete-all-lines)
:custom
(helm-swoop-split-direction 'split-window-horizontally)
(helm-swoop-split-direction 'split-window-sensibly)
:bind
(("C-S-s" . #'helm-swoop)
;; Use similar bindings to `helm-ag-edit'
Expand Down
33 changes: 33 additions & 0 deletions modules/init-prefs.el
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,39 @@ evaluating `font-family-list'."
:group 'exordium
:type 'integer)

(defcustom exordium-split-window-preffered-direction 'vertical
"The first direction tried when Emacs needs to split a window.
This variable controls in which order `split-window-sensibly'
will try to split the window. That order specially matters when
both dimensions of the frame are long enough to be split
according to `split-width-threshold' and
`split-height-threshold'. If this is set to `vertical' (the
default), `split-window-sensibly' tries to split vertically first
and then horizontally. If set to `horizontal' it does the
opposite. If set to `longest' the first direction tried depends
on the frame shape: in landscape orientation it will be like
`horizontal', but in portrait it will be like `vertical'.
Basically, the longest of the two dimension is split first.
When set `longest-vertical' or `longest-horizontal' then Helm
will use vertical/below or horizontal/right respectively for
window splitting.
If both `split-width-threshold' and `split-height-threshold'
cannot be satisfied, it will fallback to split vertically."
:group 'exordium
:type '(radio
(const :tag "Try to split vertically first"
vertical)
(const :tag "Try to split horizontally first"
horizontal)
(const :tag "Try to split along the longest edge"
longest)
(const :tag "Try to split along the longest edge or vertically in Helm"
longest-vertical)
(const :tag "Try to split along the longest edge or horizontally in Helm"
longest-horizontal)))

(defcustom exordium-line-mode t
"Whether the current line is highlighted or not."
:group 'exordium
Expand Down
65 changes: 65 additions & 0 deletions modules/init-window-manager.el
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,71 @@


;;; Code:
(eval-when-compile
(unless (featurep 'init-require)
(load (file-name-concat (locate-user-emacs-file "modules") "init-require"))))
(exordium-require 'init-prefs)

(use-package window
:ensure nil
:custom
(split-window-preferred-direction
(if (memq exordium-split-window-preffered-direction
'(longest longest-vertical longest-horizontal))
'longest
exordium-split-window-preffered-direction))
:functions (exordium--window-try-vertical-split
exordium--window-try-horizontal-split
exordium-split-window-sensibly)
:init
(unless (boundp 'split-window-preferred-direction) ;; Until Emacs-29
(defun exordium--window-try-vertical-split (window)
"Try split WINDOW vertically."
(when (window-splittable-p window)
(with-selected-window window
(split-window-below))))

(defun exordium--window-try-horizontal-split (window)
"Try split WINDOW horizontally."
(when (window-splittable-p window t)
(with-selected-window window
(split-window-right))))

(defun exordium-split-window-sensibly (&optional window)
"Split WINDOW in a way suitable for `display-buffer'."
(let ((window (or window (selected-window))))
(or (if (or
(eq exordium-split-window-preffered-direction 'horizontal)
(and (memq exordium-split-window-preffered-direction
'(longest longest-horizontal longest-vertical))
(> (frame-width) (frame-height))))
(or (exordium--window-try-horizontal-split window)
(exordium--window-try-vertical-split window))
(or (exordium--window-try-vertical-split window)
(exordium--window-try-horizontal-split window)))
(and
;; If WINDOW is the only usable window on its frame (it is
;; the only one or, not being the only one, all the other
;; ones are dedicated) and is not the minibuffer window, try
;; to split it vertically disregarding the value of
;; `split-height-threshold'.
(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 nil 'nomini)
t)))
(not (window-minibuffer-p window))
(let ((split-height-threshold 0))
(exordium--window-try-vertical-split window))))))

;; Helm directly calls `split-window-sensibly'
(advice-add 'split-window-sensibly :override
#'exordium-split-window-sensibly)))

(use-package windmove
:ensure nil
Expand Down

0 comments on commit 7bebd02

Please sign in to comment.