No more right side window

This commit is contained in:
Joe Frikker
2026-09-04 14:36:52 -04:00
parent 3682c1ad10
commit 9e9d2bbfcc
+213 -200
View File
@@ -1054,229 +1054,242 @@ windows. I place windows in the bottom or on the right mostly depending on how w
content to be. content to be.
#+BEGIN_SRC elisp #+BEGIN_SRC elisp
(defun jf/window-children (window) (defun jf/window-children (window)
(let ((result nil) (let ((result nil)
(child (window-child window))) (child (window-child window)))
(while child (while child
(push child result) (push child result)
(setq child (window-next-sibling child))) (setq child (window-next-sibling child)))
(reverse result))) (reverse result)))
(defun jf/enforce-right-side-heights (win) (defun jf/window-main-root (&optional frame)
(let ((slot-heights '((-1 . 20))) "Return FRAME's main (non-side) window or window subtree."
(parent (window-parent win))) (let* ((root (frame-root-window (or frame (selected-frame))))
(when parent (children (jf/window-children root)))
(let ((children (jf/window-children parent))) (if (seq-some (lambda (w) (window-parameter w 'window-side)) children)
(when (> (length children) 1) (seq-find (lambda (w) (not (window-parameter w 'window-side))) children)
(cl-loop for child in children root)))
for slot = (window-parameter child 'window-slot)
for height = (alist-get slot slot-heights)
when height do (progn (window-resize child (- height (window-height child)))
(window-preserve-size child nil t))))))))
(defun jf/enforce-right-side-width (win) (defun jf/window-leaves (window)
"Resize side window WIN so its frame keeps 120 columns for the rest of the frame." "Return the live leaf windows in WINDOW's subtree."
(let ((width (max 1 (- (frame-width (window-frame win)) 120)))) (if (window-live-p window)
(window-resize win (- width (window-width win)) t))) (list window)
(mapcan #'jf/window-leaves (jf/window-children window))))
(defun jf/enforce-right-side-widths (frame) (defun jf/topright-window (windows)
"Re-apply `jf/enforce-right-side-width' to all of FRAME's right-side windows." "Return whichever of WINDOWS sits furthest up, then furthest right."
(dolist (win (window-list frame 'nomini)) (car (sort (copy-sequence windows)
(when (eq (window-parameter win 'window-side) 'right) (lambda (a b)
(jf/enforce-right-side-width win)))) (let ((top-a (nth 1 (window-edges a)))
(top-b (nth 1 (window-edges b))))
(if (= top-a top-b)
(> (nth 0 (window-edges a)) (nth 0 (window-edges b)))
(< top-a top-b)))))))
(add-hook 'window-size-change-functions #'jf/enforce-right-side-widths) (defun jf/display-buffer-right (buffer alist)
"Display BUFFER on the right of the main editing area.
;; (require 'perspective) Applies three rules, based on the shape of the frame's non-side windows:
;; (setopt persp-mode-prefix-key (kbd "C-x t")) 1. One window -> split it and show BUFFER in the new right window.
;; (setopt persp-show-modestring nil) 2. Two windows side-by-side -> show BUFFER in the right one. If the
;; (persp-mode) right window is selected, swap the two windows first (keeping
focus on the buffer that was active) so BUFFER doesn't evict it.
3. Any other layout -> show BUFFER in the upper-rightmost window."
(let ((main (jf/window-main-root)))
(cond
((window-live-p main)
(let* ((new (split-window main nil 'right))
(width (max 1 (- (frame-width) 120)))
(delta (window-resizable new (- width (window-width new)) t)))
(window-resize new delta t)
(window--display-buffer buffer new 'window alist)))
((and (window-left-child main)
(= (length (jf/window-children main)) 2)
(seq-every-p #'window-live-p (jf/window-children main)))
(let* ((left (window-left-child main))
(right (window-next-sibling left)))
(when (eq (selected-window) right)
(window-swap-states left right)
(select-window left))
(window--display-buffer buffer right 'reuse alist)))
(t
(window--display-buffer buffer (jf/topright-window (jf/window-leaves main)) 'reuse alist)))))
;; (defun jf/popper-group-by-owning-perspective () ;; (require 'perspective)
;; "Return the name of the perspective that owns the current buffer. ;; (setopt persp-mode-prefix-key (kbd "C-x t"))
;; (setopt persp-show-modestring nil)
;; (persp-mode)
;; Unlike `popper-group-by-perspective', which just returns whichever ;; (defun jf/popper-group-by-owning-perspective ()
;; perspective happens to be active on the frame right now, this looks ;; "Return the name of the perspective that owns the current buffer.
;; up which perspective's buffer list the popup buffer actually belongs
;; to. This matters because popper re-groups popups on
;; `window-configuration-change-hook', which perspective.el triggers
;; *after* switching the frame's active perspective -- so
;; `popper-group-by-perspective' ends up filing a popup under the
;; perspective you just switched *to* rather than the one it came from."
;; (let ((buf (current-buffer)))
;; (or (cl-loop for persp being the hash-values of (perspectives-hash (selected-frame))
;; if (memq buf (persp-buffers persp))
;; return (persp-name persp))
;; (persp-current-name))))
(setopt popper-reference-buffers ;; Unlike `popper-group-by-perspective', which just returns whichever
'("\\*Flymake diagnostics .*" ;; perspective happens to be active on the frame right now, this looks
"\\*Embark Export: .*" ;; up which perspective's buffer list the popup buffer actually belongs
"\\*Embark Collect .*" ;; to. This matters because popper re-groups popups on
"\\*xref\\*" ;; `window-configuration-change-hook', which perspective.el triggers
"\\*compilation\\*" ;; *after* switching the frame's active perspective -- so
"\\*pnpm:.*" ;; `popper-group-by-perspective' ends up filing a popup under the
"\\*sly-mrepl *" ;; perspective you just switched *to* rather than the one it came from."
"\\*lispy-message\\*" ;; (let ((buf (current-buffer)))
"\\*occur\\*" ;; (or (cl-loop for persp being the hash-values of (perspectives-hash (selected-frame))
".*eshell\\*" ;; if (memq buf (persp-buffers persp))
"\\*SQL: .*" ;; return (persp-name persp))
"magit-revision: .*" ;; (persp-current-name))))
"\\*Help\\*"
"\\*rustfmt\\*"
"\\*slack: .*"
"\\*forge: .*"
"new-pullreq"
"\\*claude:.*"
"\\*Man .*"
"\\*List .*"
"\\*eldoc\\*"))
(setopt popper-display-control nil)
;; (setopt popper-group-function 'jf/popper-group-by-owning-perspective)
(popper-mode) (setopt popper-reference-buffers
(popper-echo-mode) '("\\*Flymake diagnostics .*"
"\\*Embark Export: .*"
"\\*Embark Collect .*"
"\\*xref\\*"
"\\*compilation\\*"
"\\*pnpm:.*"
"\\*sly-mrepl *"
"\\*lispy-message\\*"
"\\*occur\\*"
".*eshell\\*"
"\\*SQL: .*"
"magit-revision: .*"
"\\*Help\\*"
"\\*rustfmt\\*"
"\\*slack: .*"
"\\*forge: .*"
"new-pullreq"
"\\*claude:.*"
"\\*Man .*"
"\\*List .*"
"\\*eldoc\\*"))
(setopt popper-display-control nil)
;; (setopt popper-group-function 'jf/popper-group-by-owning-perspective)
(bind-keys ("C-q" . popper-toggle) (popper-mode)
("C-;" . popper-cycle)) (popper-echo-mode)
(defvar jf/fullscreen-popup--param 'jf/fullscreen-saved-configuration (bind-keys ("C-q" . popper-toggle)
"Key used to stash the window configuration a fullscreen popup replaced. ("C-;" . popper-cycle))
Stored per-tab when `tab-bar-mode' is active, otherwise as a plain frame
parameter; see `jf/tab-or-frame-parameter'.")
(defvar-local jf/fullscreen-popup-p nil (defvar jf/fullscreen-popup--param 'jf/fullscreen-saved-configuration
"Non-nil in a buffer currently being displayed as a fullscreen popup.") "Key used to stash the window configuration a fullscreen popup replaced.
Stored per-tab when `tab-bar-mode' is active, otherwise as a plain frame
parameter; see `jf/tab-or-frame-parameter'.")
(defun jf/current-tab-cell (&optional frame) (defvar-local jf/fullscreen-popup-p nil
"Return FRAME's current tab's raw data cell, or nil without `tab-bar-mode'." "Non-nil in a buffer currently being displayed as a fullscreen popup.")
(and (bound-and-true-p tab-bar-mode)
(assq 'current-tab (tab-bar-tabs frame))))
(defun jf/tab-or-frame-parameter (parameter &optional frame) (defun jf/current-tab-cell (&optional frame)
"Like `frame-parameter', but scoped to FRAME's selected tab when tabs are in use. "Return FRAME's current tab's raw data cell, or nil without `tab-bar-mode'."
(and (bound-and-true-p tab-bar-mode)
(assq 'current-tab (tab-bar-tabs frame))))
A plain frame parameter is shared by every tab on that frame, which is wrong (defun jf/tab-or-frame-parameter (parameter &optional frame)
for state like `jf/fullscreen-popup--param': switching tabs swaps in an "Like `frame-parameter', but scoped to FRAME's selected tab when tabs are in use.
entirely different window layout, so \"the layout to restore\" has to travel
with the tab, not the frame."
(if-let ((tab (jf/current-tab-cell frame)))
(alist-get parameter tab)
(frame-parameter frame parameter)))
(defun jf/set-tab-or-frame-parameter (parameter value &optional frame) A plain frame parameter is shared by every tab on that frame, which is wrong
"Set PARAMETER to VALUE, scoped like `jf/tab-or-frame-parameter'." for state like `jf/fullscreen-popup--param': switching tabs swaps in an
(if-let ((tab (jf/current-tab-cell frame))) entirely different window layout, so \"the layout to restore\" has to travel
(let ((cell (assq parameter tab))) with the tab, not the frame."
(if cell (if-let ((tab (jf/current-tab-cell frame)))
(setcdr cell value) (alist-get parameter tab)
(setcdr tab (cons (cons parameter value) (cdr tab))))) (frame-parameter frame parameter)))
(set-frame-parameter frame parameter value)))
(defun jf/display-buffer-fullscreen (buffer alist) (defun jf/set-tab-or-frame-parameter (parameter value &optional frame)
"Display BUFFER, deleting other windows, saving the prior layout. "Set PARAMETER to VALUE, scoped like `jf/tab-or-frame-parameter'."
(if-let ((tab (jf/current-tab-cell frame)))
(let ((cell (assq parameter tab)))
(if cell
(setcdr cell value)
(setcdr tab (cons (cons parameter value) (cdr tab)))))
(set-frame-parameter frame parameter value)))
Intended for use in `display-buffer-alist' for popups (e.g. Magit (defun jf/display-buffer-fullscreen (buffer alist)
status/log) that should take over the frame while displayed, but "Display BUFFER, deleting other windows, saving the prior layout.
restore the previous window configuration once closed via either
`q' or `popper-toggle' -- both end up calling `quit-window', which
is where the restore actually happens; see
`jf/quit-window-restore-fullscreen'."
(unless (jf/tab-or-frame-parameter jf/fullscreen-popup--param)
(jf/set-tab-or-frame-parameter jf/fullscreen-popup--param
(current-window-configuration)))
(when-let ((window (or (display-buffer-reuse-window buffer alist)
(display-buffer-same-window buffer alist)
(display-buffer-pop-up-window buffer alist)
(display-buffer-use-some-window buffer alist))))
(let ((ignore-window-parameters t))
(delete-other-windows window))
(with-current-buffer buffer
(setq jf/fullscreen-popup-p t))
window))
(defun jf/quit-window-restore-fullscreen (orig-fn &optional kill window) Intended for use in `display-buffer-alist' for popups (e.g. Magit
"Restore the saved window configuration after quitting a fullscreen popup. status/log) that should take over the frame while displayed, but
restore the previous window configuration once closed via either
`q' or `popper-toggle' -- both end up calling `quit-window', which
is where the restore actually happens; see
`jf/quit-window-restore-fullscreen'."
(unless (jf/tab-or-frame-parameter jf/fullscreen-popup--param)
(jf/set-tab-or-frame-parameter jf/fullscreen-popup--param
(current-window-configuration)))
(when-let ((window (or (display-buffer-reuse-window buffer alist)
(display-buffer-same-window buffer alist)
(display-buffer-pop-up-window buffer alist)
(display-buffer-use-some-window buffer alist))))
(let ((ignore-window-parameters t))
(delete-other-windows window))
(with-current-buffer buffer
(setq jf/fullscreen-popup-p t))
window))
Advice around `quit-window' so both `q' (which Magit and other (defun jf/quit-window-restore-fullscreen (orig-fn &optional kill window)
special-mode buffers bind to buffer-quitting commands that call "Restore the saved window configuration after quitting a fullscreen popup.
`quit-window') and Popper's close/toggle commands (which also call
`quit-window' directly) trigger the restore. Only restores once the
last fullscreen popup on the tab (or frame, without `tab-bar-mode')
has closed, so opening one fullscreen popup from within another
falls back to the outer one first."
(let* ((window (or window (selected-window)))
(was-fullscreen (buffer-local-value 'jf/fullscreen-popup-p
(window-buffer window)))
(frame (window-frame window)))
(funcall orig-fn kill window)
(when (and was-fullscreen
(not (and (window-live-p window)
(buffer-local-value 'jf/fullscreen-popup-p
(window-buffer window))))
(jf/tab-or-frame-parameter jf/fullscreen-popup--param frame))
(let ((conf (jf/tab-or-frame-parameter jf/fullscreen-popup--param frame)))
(jf/set-tab-or-frame-parameter jf/fullscreen-popup--param nil frame)
(set-window-configuration conf)))))
(advice-add 'quit-window :around #'jf/quit-window-restore-fullscreen) Advice around `quit-window' so both `q' (which Magit and other
special-mode buffers bind to buffer-quitting commands that call
`quit-window') and Popper's close/toggle commands (which also call
`quit-window' directly) trigger the restore. Only restores once the
last fullscreen popup on the tab (or frame, without `tab-bar-mode')
has closed, so opening one fullscreen popup from within another
falls back to the outer one first."
(let* ((window (or window (selected-window)))
(was-fullscreen (buffer-local-value 'jf/fullscreen-popup-p
(window-buffer window)))
(frame (window-frame window)))
(funcall orig-fn kill window)
(when (and was-fullscreen
(not (and (window-live-p window)
(buffer-local-value 'jf/fullscreen-popup-p
(window-buffer window))))
(jf/tab-or-frame-parameter jf/fullscreen-popup--param frame))
(let ((conf (jf/tab-or-frame-parameter jf/fullscreen-popup--param frame)))
(jf/set-tab-or-frame-parameter jf/fullscreen-popup--param nil frame)
(set-window-configuration conf)))))
(setq display-buffer-alist (advice-add 'quit-window :around #'jf/quit-window-restore-fullscreen)
'(((or "magit: *"
"magit-log*"
"\\*Messages\\*"
"\\*trace-output\\*"
"\\*EGLOT connections\\*")
(jf/display-buffer-fullscreen))
((or "\\*Flymake diagnostics .*"
"\\*Embark Export: .*"
"\\*Embark Collect .*"
"\\*xref\\*"
"\\*compilation\\*"
"\\*pnpm:.*"
"\\*sly-mrepl *"
"\\*lispy-message\\*"
"\\*occur\\*"
".*eshell\\*"
"\\*SQL: .*")
(display-buffer-in-side-window)
(side . bottom)
(slot . 0)
(window-height . 0.33)
(window-parameters (no-delete-other-windows . t)
(no-other-window . t)))
((or "magit-revision: .*"
"\\*Help\\*"
"\\*rustfmt\\*"
"\\*slack: .*"
"\\*forge: .*"
"new-pullreq"
"\\*claude:.*"
"\\*Man .*"
"\\*List .*")
(display-buffer-in-side-window)
(side . right)
(slot . 0)
(window-width . jf/enforce-right-side-width)
(window-height . jf/enforce-right-side-heights)
(preserve-size . (nil . nil))
(window-parameters (no-delete-other-windows . t)
(no-other-window . t)))
((or "\\*Embark Actions\\*"
"\\*eldoc\\*")
(display-buffer-in-side-window)
(side . right)
(slot . -1)
(window-width . jf/enforce-right-side-width)
(window-height . jf/enforce-right-side-heights)
(preserve-size . (nil . t))
(window-parameters (no-delete-other-windows . t)
(no-other-window . t)))))
(setopt window-persistent-parameters (setq display-buffer-alist
(cl-list* '(no-delete-other-windows . writable) '(((or "magit: *"
'(no-other-window . writable) "magit-log*"
window-persistent-parameters)) "\\*Messages\\*"
"\\*trace-output\\*"
"\\*EGLOT connections\\*")
(jf/display-buffer-fullscreen))
((or "\\*Flymake diagnostics .*"
"\\*Embark Export: .*"
"\\*Embark Collect .*"
"\\*xref\\*"
"\\*compilation\\*"
"\\*pnpm:.*"
"\\*sly-mrepl *"
"\\*lispy-message\\*"
"\\*occur\\*"
".*eshell\\*"
"\\*SQL: .*")
(display-buffer-in-side-window)
(side . bottom)
(slot . 0)
(window-height . 0.33)
(window-parameters (no-delete-other-windows . t)
(no-other-window . t)))
((or "magit-revision: .*"
"\\*Help\\*"
"\\*rustfmt\\*"
"\\*slack: .*"
"\\*forge: .*"
"new-pullreq"
"\\*claude:.*"
"\\*Man .*"
"\\*List .*"
"\\*Embark Actions\\*"
"\\*eldoc\\*")
(jf/display-buffer-right))))
(setopt window-persistent-parameters
(cl-list* '(no-delete-other-windows . writable)
'(no-other-window . writable)
window-persistent-parameters))
#+END_SRC #+END_SRC