branch: master
commit a3d48170a40a7dbd94ae144c7683f50509d8193b
Author: Oleh Krehel <[email protected]>
Commit: Oleh Krehel <[email protected]>
Allow switching to the previous window
* ace-window.el (aw--flip-keys): New defvar.
(aw-flip-keys): New defcustom. If one of the keys on this list is
pressed, when `aw-keys' are expected, instead of giving an error, select
the previously visited window.
(aw-select): Update.
(aw--window-ring): New defvar. Store the last 10 selected windows.
(aw--push-window): Remember window into `aw--window-ring'.
(aw--pop-window): Pop window from `aw--window-ring'.
(aw-switch-to-window): Insert the window into `aw--window-ring'.
(aw-swap-window): Insert the window into `aw--window-ring'.
(aw-flip-window): New defun, do the flipping action without having to go
though the `ace-select-window' dispatch.
Fixes #23.
---
ace-window.el | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++-----
1 files changed, 50 insertions(+), 5 deletions(-)
diff --git a/ace-window.el b/ace-window.el
index 86f910c..b688a0e 100644
--- a/ace-window.el
+++ b/ace-window.el
@@ -205,6 +205,16 @@ LEAF is (PT . WND)."
ol))
wnd-list))))
+(defvar aw--flip-keys nil
+ "Pre-processed `aw-flip-keys'.")
+
+(defcustom aw-flip-keys '("n")
+ "Keys which should select the last window."
+ :set (lambda (sym val)
+ (set sym val)
+ (setq aw--flip-keys
+ (mapcar (lambda (x) (aref (kbd x) 0)) val))))
+
(defun aw-select (mode-line)
"Return a selected other window.
Amend MODE-LINE to the mode line for the duration of the selection."
@@ -239,10 +249,16 @@ Amend MODE-LINE to the mode line for the duration of the
selection."
(force-mode-line-update)
;; turn off helm transient map
(remove-hook 'post-command-hook 'helm--maybe-update-keymap)
- (unwind-protect (or (cdr (avy-read (avy-tree candidate-list aw-keys)
- #'aw--lead-overlay
- #'aw--remove-leading-chars))
- start-window)
+ (unwind-protect
+ (condition-case err
+ (or (cdr (avy-read (avy-tree candidate-list aw-keys)
+ #'aw--lead-overlay
+ #'aw--remove-leading-chars))
+ start-window)
+ (error
+ (if (memq (caddr err) aw--flip-keys)
+ (aw--pop-window)
+ (signal (car err) (cdr err)))))
(aw--done)))))))
;;* Interactive
@@ -318,6 +334,27 @@ Windows are numbered top down, left to right."
((< (cadr e1) (cadr e2))
t))))
+(defvar aw--window-ring (make-ring 10)
+ "Hold the window switching history.")
+
+(defun aw--push-window (window)
+ "Store WINDOW to `aw--window-ring'."
+ (when (or (zerop (ring-length aw--window-ring))
+ (not (equal
+ (ring-ref aw--window-ring 0)
+ window)))
+ (ring-insert aw--window-ring (selected-window))))
+
+(defun aw--pop-window ()
+ "Return the removed top of `aw--window-ring'."
+ (let (res)
+ (condition-case nil
+ (while (not (window-live-p
+ (setq res (ring-remove aw--window-ring 0)))))
+ (error
+ (error "No previous windows stored")))
+ res))
+
(defun aw-switch-to-window (window)
"Switch to the window WINDOW."
(let ((frame (window-frame window)))
@@ -325,9 +362,16 @@ Windows are numbered top down, left to right."
(not (eq frame (selected-frame))))
(select-frame-set-input-focus frame))
(if (window-live-p window)
- (select-window window)
+ (progn
+ (aw--push-window (selected-window))
+ (select-window window))
(error "Got a dead window %S" window))))
+(defun aw-flip-window ()
+ "Switch to the window you were previously in."
+ (interactive)
+ (aw-switch-to-window (aw--pop-window)))
+
(defun aw-delete-window (window)
"Delete window WINDOW."
(let ((frame (window-frame window)))
@@ -356,6 +400,7 @@ Windows are numbered top down, left to right."
(select-frame-set-input-focus (window-frame window)))
(when (and (window-live-p window)
(not (eq window this-window)))
+ (aw--push-window this-window)
(swap-windows this-window window)))))
(defun aw-offset (window)