unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#34656: 27.0.50; Directional window swap states
@ 2019-02-25 20:51 Juri Linkov
  2019-02-26  9:10 ` martin rudalics
  0 siblings, 1 reply; 3+ messages in thread
From: Juri Linkov @ 2019-02-25 20:51 UTC (permalink / raw)
  To: 34656

[-- Attachment #1: Type: text/plain, Size: 1076 bytes --]

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 :)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: windmove-swap-states.patch --]
[-- Type: text/x-diff, Size: 2631 bytes --]

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))
 
+\f
+;;; 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))
+
+\f
 (provide 'windmove)
 
 ;;; windmove.el ends here

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* bug#34656: 27.0.50; Directional window swap states
  2019-02-25 20:51 bug#34656: 27.0.50; Directional window swap states Juri Linkov
@ 2019-02-26  9:10 ` martin rudalics
  2019-02-27 21:12   ` Juri Linkov
  0 siblings, 1 reply; 3+ messages in thread
From: martin rudalics @ 2019-02-26  9:10 UTC (permalink / raw)
  To: Juri Linkov, 34656

+  (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?

martin






^ permalink raw reply	[flat|nested] 3+ messages in thread

* bug#34656: 27.0.50; Directional window swap states
  2019-02-26  9:10 ` martin rudalics
@ 2019-02-27 21:12   ` Juri Linkov
  0 siblings, 0 replies; 3+ messages in thread
From: Juri Linkov @ 2019-02-27 21:12 UTC (permalink / raw)
  To: martin rudalics; +Cc: 34656-done

> +  (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.





^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-02-27 21:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-02-25 20:51 bug#34656: 27.0.50; Directional window swap states Juri Linkov
2019-02-26  9:10 ` martin rudalics
2019-02-27 21:12   ` Juri Linkov

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).