refactor(evil): +evil--window-swap: no-op if on edge

Also error checks DIRECTION, just in case.
This commit is contained in:
Henrik Lissner 2024-07-05 20:05:38 -04:00
parent b767beaca6
commit 2bfb7821b6
No known key found for this signature in database
GPG key ID: B60957CA074D39A3

View file

@ -44,28 +44,32 @@ replacing its contents."
If there are no windows there and there is only one window, split in that If there are no windows there and there is only one window, split in that
direction and place this window there. If there are no windows and this isn't direction and place this window there. If there are no windows and this isn't
the only window, use evil-window-move-* (e.g. `evil-window-move-far-left')." the only window, use evil-window-move-* (e.g. `evil-window-move-far-left')."
(unless (memq direction '(left right up down))
(user-error "Invalid direction: %s" direction))
(when (window-dedicated-p) (when (window-dedicated-p)
(user-error "Cannot swap a dedicated window")) (user-error "Cannot swap a dedicated window"))
(let* ((this-window (selected-window)) (let* ((this-window (selected-window))
(this-buffer (current-buffer))
(that-window (window-in-direction direction nil this-window)) (that-window (window-in-direction direction nil this-window))
(that-buffer (window-buffer that-window))) (that-buffer (window-buffer that-window)))
(when (or (minibufferp that-buffer) (when (or (minibufferp that-buffer)
(window-dedicated-p this-window)) (window-dedicated-p this-window))
(setq that-buffer nil that-window nil)) (setq that-buffer nil that-window nil))
(if (not (or that-window (one-window-p t))) (if (not (or that-window (one-window-p t)))
(funcall (pcase direction (if (and (window-at-side-p
this-window (pcase direction ('up 'top) ('down 'bottom) (_ direction)))
(not (cl-loop for dir in (if (memq direction '(left right))
'(up down) '(left right))
if (window-in-direction dir nil this-window)
return t)))
(user-error "Window is already at the edge")
(call-interactively
(pcase direction
('left #'evil-window-move-far-left) ('left #'evil-window-move-far-left)
('right #'evil-window-move-far-right) ('right #'evil-window-move-far-right)
('up #'evil-window-move-very-top) ('up #'evil-window-move-very-top)
('down #'evil-window-move-very-bottom))) ('down #'evil-window-move-very-bottom))))
(unless that-window (unless that-window
(setq that-window (setq that-window (split-window this-window nil direction))
(split-window this-window nil
(pcase direction
('up 'above)
('down 'below)
(_ direction))))
(with-selected-window that-window (with-selected-window that-window
(switch-to-buffer (doom-fallback-buffer))) (switch-to-buffer (doom-fallback-buffer)))
(setq that-buffer (window-buffer that-window))) (setq that-buffer (window-buffer that-window)))