abo-abo / ace-window

Quickly switch windows in Emacs
977 stars 87 forks source link

Add "Enter" to go back to last window #125

Open JohnC32 opened 6 years ago

JohnC32 commented 6 years ago

Currently if you hit the enter key when using ace-window it cancels the switch window operation. Instead of canceling, please make it jump back to where I was.

Consider a multi-window layout where your often switching between two windows (e.g. the "gdb" and "source" window when using M-x gdb with many windows). I've bound C-x o ace-window which works very nicely except in this case. I'd like to have enter jump back to where I was. This way I can do C-x o NUM to go to a specific window or C-x o RET to jump back to the window I was last in.

abo-abo commented 6 years ago

You can customize it like this:

(add-to-list 'aw-dispatch-alist '(minibuffer-keyboard-quit))
JohnC32 commented 6 years ago

I briefly looked at the code. I think I see one could add to aw-dispatch-alist an action for RET. However, to do this one would need to track the previous window that was active and also improve the window numbering to indicate where RET takes you.

Here's what I think would be generally useful: Let's say you have three windows and you've bound 'C-x o' ace-window. Suppose you are in window 1 and you were last in window 3. Typing C-x o would should the red "1", "2", and "3/RET" in the upper right corner. If I hit 3 or simply RET (Enter) I'd jump to 3. Then if I'm in window 3, when I type C-x o, I would see a red "1/RET", "2", and "3" in each window and if I hit RET, I'd jump back to window 1.

I'm finding ace-window really useful and it seems to me adding the RET behavior would make it generally more useful. It handles the case where your often toggling between two windows. It would also handle the case where you working in one primary window, then jumping to others and back.

abo-abo commented 6 years ago

OK, I see your point now. I thought you wanted to stay in the current window, but you want to select the previous one.

I don't think Emacs keeps track of the previous window. It's not too difficult to do if you only use ace-window for switching. But windows can be switched using the mouse and with package functions. window-configuration-change-hook looks promising. I'll look at this later.

abo-abo commented 6 years ago

I forgot that this feature already exists: n will call aw-flip-window. Let me know if that's what you were looking for.

luciferasm commented 6 years ago

It seems like aw-flip-window only considers windows that were selected with ace-window and, as you wrote, not windows that were selected by other means (e.g. mouse or other package functions). In an attempt to find the previous selected window regardless of how it was selected, I have always used this code snippet:

(defun sg-previous-window ()
  "Toggle between the last two selected windows."
  (interactive)
  (let ((win (get-mru-window t t t)))
    (unless win (error "Last window not found."))
    (let ((frame (window-frame win)))
      (raise-frame frame)
      (select-frame frame)
      (select-window win))))

There might be some edge case that isn't covered, but it has served me well for a long time. This functionality should be built-in into vanilla Emacs, since toggling between two windows when more than two windows are visible is a common action. Feel free to use it if you think it's useful.

JohnC32 commented 6 years ago

Thanks luciferasm - this works nicely. I couldn't find any case that it didn't work for me. I modified my local version of ace-window to contain

(?\r aw-previous-window)

in aw-dispatch-list and also added function:

(defun aw-previous-window ()
  "Toggle between the last two selected windows."
  (interactive)
  (let ((win (get-mru-window t t t)))
    (unless win (error "Last window not found."))
    (aw-switch-to-window win)))

Could this be added to ace-window, maybe replacing the default 'n' binding, i.e. remove 'n'?

Note, I'm using RET instead of n as the switch to previous window key because it seems more natural to type/remember. If you don't want to retire 'n', perhaps, a customization could be defined to the prior window and then I could use customize to use RET. The only other thing that would be really nice is to have the "red" indicators that label windows when switching to have a /RET for the one that hitting RET would switch to.

abo-abo commented 6 years ago

Could this be added to ace-window, maybe replacing the default 'n' binding, i.e. remove 'n'?

Since it works better, I'm fine with it. @luciferasm Could you please open a PR with this change?