Skip to content

Instantly share code, notes, and snippets.

@simonmichael
Last active March 16, 2023 09:30
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonmichael/1f3d6067f437821ea41ccc35e2a69673 to your computer and use it in GitHub Desktop.
Save simonmichael/1f3d6067f437821ea41ccc35e2a69673 to your computer and use it in GitHub Desktop.
emacs: simple window splitting policy
;; Try to ensure sensible window splitting, independent of frame and font size.
;; Things I don't want:
;; - excessive horizontal splitting (generally I prefer no more than 2 windows across)
;; - horizontal splitting when frame is tall and narrow (causing eg useless narrow man page formatting)
;; So, aim for a simple policy: popup windows shall be half width in a wide frame,
;; half height in a tall frame.
;;
;; Emacs' window splitting behaviour is complex and must be read carefully.
;; In particular, note: it checks split-window-height first, and if that permits it to
;; split vertically, it does that and split-window-horizontally is ignored.
;;
;; Approach 1: set split-window-height to the full screen (frame-height) and
;; split-window-width to half the full screen (frame-width). Problem: these
;; numbers change as the font is resized.
;;
;; Approach 2: update the split thresholds dynamically whenever window
;; configuration changes. Inspect the current frame aspect ratio. If it is
;; wide, set split-window-height to frame height and split-window-width to
;; half frame width. If it is tall, set split-window-height to frame height
;; and split-window-width to full frame width.
;;
(defun adjust-window-split-thresholds nil
"Adjust split thresholds so that popup windows always split vertically in a tall frame, horizontally in a wide frame, with a maximum of two columns"
(interactive)
(if (>= (frame-pixel-width) (frame-pixel-height))
; wide frame
(progn
(setq split-height-threshold (frame-height))
(setq split-width-threshold (/ (frame-width) 2))
)
; tall frame
(progn
(setq split-height-threshold (frame-height))
(setq split-width-threshold (frame-width))
))
)
(add-hook 'window-configuration-change-hook 'adjust-window-split-thresholds)
;; (remove-hook 'window-configuration-change-hook 'adjust-window-split-thresholds)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment