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