--- Begin Message ---
Subject: |
27.0.50; Directional window swap states |
Date: |
Mon, 25 Feb 2019 22:51:06 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (x86_64-pc-linux-gnu) |
Tags: patch
Severity: wishlist
Now we have a useful command ‘window-swap-states’ whose only drawback is that
it operates on a random window, i.e. a next window in some mystic cyclic
ordering
whose logic I've never been able to understand.
To simplify its logic, windmove could be extended to support directional
swapping
where the user can point at the window whose state should be swapped
with the selected window.
As a funny effect, when windows are arranged as 4x4 square tiles, e.g.
(window-state-put
'(()
hc
(vc
(leaf (buffer "1"))
(leaf (buffer "5"))
(leaf (buffer "9"))
(leaf (buffer "13")))
(vc
(leaf (buffer "2"))
(leaf (buffer "6"))
(leaf (buffer "10"))
(leaf (buffer "14")))
(vc
(leaf (buffer "3"))
(leaf (buffer "7"))
(leaf (buffer "11"))
(leaf (buffer "15")))
(vc
(leaf (buffer "4"))
(leaf (buffer "8"))
(leaf (buffer "12"))
(leaf (buffer " "))))
(frame-root-window))
sliding them has the same feeling like solving the 15 puzzle :)
diff --git a/lisp/windmove.el b/lisp/windmove.el
index 65270d9bbe..e2422a7df4 100644
--- a/lisp/windmove.el
+++ b/lisp/windmove.el
@@ -750,6 +752,60 @@ windmove-delete-default-keybindings
(global-set-key (vector prefix (append modifiers '(up)))
'windmove-delete-up)
(global-set-key (vector prefix (append modifiers '(down)))
'windmove-delete-down))
+
+;;; Directional window swap states
+
+(defun windmove-swap-states-in-direction (dir)
+ "Swap the states of the selected window and the window at direction DIR.
+When `windmove-wrap-around' is non-nil, takes the window
+from the opposite side of the frame."
+ (let ((other-window (window-in-direction dir nil nil nil
+ windmove-wrap-around t)))
+ (cond ((null other-window)
+ (user-error "No window %s from selected window" dir))
+ (t
+ (window-swap-states nil other-window)))))
+
+;;;###autoload
+(defun windmove-swap-states-left ()
+ "Swap the states with the window on the left from the current one."
+ (interactive)
+ (windmove-swap-states-in-direction 'left))
+
+;;;###autoload
+(defun windmove-swap-states-up ()
+ "Swap the states with the window above from the current one."
+ (interactive)
+ (windmove-swap-states-in-direction 'up))
+
+;;;###autoload
+(defun windmove-swap-states-down ()
+ "Swap the states with the window below from the current one."
+ (interactive)
+ (windmove-swap-states-in-direction 'down))
+
+;;;###autoload
+(defun windmove-swap-states-right ()
+ "Swap the states with the window on the right from the current one."
+ (interactive)
+ (windmove-swap-states-in-direction 'right))
+
+;;;###autoload
+(defun windmove-swap-states-default-keybindings (&optional modifiers)
+ "Set up keybindings for directional window swap states.
+Keys are bound to commands that swap the states of the selected window
+with the window in the specified direction. Keybindings are of the form
+MODIFIERS-{left,right,up,down}, where MODIFIERS is either a list of modifiers
+or a single modifier. Default value of MODIFIERS is `shift-super'."
+ (interactive)
+ (unless modifiers (setq modifiers '(shift super)))
+ (unless (listp modifiers) (setq modifiers (list modifiers)))
+ (global-set-key (vector (append modifiers '(left)))
'windmove-swap-states-left)
+ (global-set-key (vector (append modifiers '(right)))
'windmove-swap-states-right)
+ (global-set-key (vector (append modifiers '(up)))
'windmove-swap-states-up)
+ (global-set-key (vector (append modifiers '(down)))
'windmove-swap-states-down))
+
+
(provide 'windmove)
;;; windmove.el ends here
--- End Message ---
--- Begin Message ---
Subject: |
Re: bug#34656: 27.0.50; Directional window swap states |
Date: |
Wed, 27 Feb 2019 23:12:16 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (x86_64-pc-linux-gnu) |
> + (let ((other-window (window-in-direction dir nil nil nil
> + windmove-wrap-around t)))
>
> If 'windmove-wrap-around' is nil, this may swap the selected window's
> state with that of the minibuffer window. Or am I misinterpreting?
Thanks for the review. Now I fixed the minibuffer case and closed this request.
Before this fix, trying to swap states with the minibuffer failed with the
error:
(error "Window #<window 4 on *Minibuf-0*> too small to accommodate state")
Now after the fix, it doesn't consider the minibuffer as a candidate to swap
states.
BTW, before proposing the implementation in windmove, the first idea was
to improve the logic of finding the second window in window-swap-states like:
diff --git a/lisp/window.el b/lisp/window.el
index 80828bb35c..3961ec3975 100644
--- a/lisp/window.el
+++ b/lisp/window.el
@@ -6018,7 +6018,10 @@ window-swap-states
(if window-2
(unless (window-live-p window-2)
(error "%s is not a live window" window-2))
- (setq window-2 (next-window window-1 'nomini 'visible)))
+ (setq window-2 (or (get-mru-window 'visible t t)
+ (get-mru-window 0 t t)
+ (get-mru-window t t t)
+ (next-window window-1 'nomini 'visible))))
(unless (eq window-1 window-2)
(let* ((height (memq size '(t height)))
(width (memq size '(t width)))
This logic is like finding the second window in
`compare-windows-get-recent-window'.
But since directional swapping in windmove is more convenient,
it seems the above change is not needed.
--- End Message ---