diff options
| -rw-r--r-- | bard-elisp/bard-window.el | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/bard-elisp/bard-window.el b/bard-elisp/bard-window.el index de84cb5..f3ac558 100644 --- a/bard-elisp/bard-window.el +++ b/bard-elisp/bard-window.el @@ -173,5 +173,46 @@ use in `display-buffer-alist'." for window in windows do (set-window-buffer window buffer))))) +;; The ultimate DWIM other window command. +(defvar pmx--direction -1) +(defvar pmx--last-win nil) +(defun pmx-other-window (frame) + "Switch window, with DWIM behavior. +Prefix argument FRAME will unconditionally switch frames. +When called without any windows to switch to, split and select. +If called not in repeat, reverse directions and switch back to +usually the most recent window (though not `get-mru-window'). +Finally, when called in repeat, continue in the same direction so +that we can usually get to the right window faster than an `avy' +call unless there's a ton of windows for some reason." + (interactive "P") + (cond (frame (other-frame 1)) ; unconditional with prefix arg + ((equal 1 (length (window-list + (selected-frame)))) + ;; If there is no window or even minibuffer open, split window. Change + ;; the direction so that we go back to the source window on repeat or + ;; next call. + (let ((source (selected-window)) + (tall (> (frame-pixel-height) (frame-pixel-width)))) + (select-window (split-window-right)) + (if (eq source (next-window)) + (setq pmx--direction 1) + (setq pmx--direction -1) + (when (not (eq source (previous-window))) + (warn "Split window sucessor inconsistent"))))) + ((not (eq last-command 'pmx-other-window)) + ;; If we are not repeating an other-window command, reverse the + ;; direction and select in that direction. + (if (eq pmx--last-win (selected-window)) + (setq pmx--directionapmx--direction (- pmx--direction)) + ;; we changed windows out of band. Reverse directions. + (setq pmx--direction -1)) + (other-window pmx--direction)) + (t + ;; We are repeating. Continue going in the established direction. + (other-window pmx--direction))) + (setq pmx--last-win (selected-window))) + +(keymap-global-set "C-x o" #'pmx-other-window) (provide 'bard-window) |
