I am trying to use the code from this answer: Is there a generic toggle-previous-window function?
(defun switch-to-last-window () (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))))
I had to add:
(select-frame-set-input-focus frame) to make it work. But with this code or just switching windows using the mouse I have the following problem. Imagine I have two frames, one with one window
f1_w1 and the other with two windows
f2_w2. When I switch from
f2_w1 there is no problem, but when on the next cycle after
f1_w1 I switch from
f2_w2 I can see the cursor first appear on
f2_w1 and then quickly jump to
f2_w2 making my last visited window
f2_w1 and not
f1_w1 as it should be.
EDIT: as per Drew's comment below that asks for the question. I just find it surprising that Emacs can't properly track the last selected window (unless something is messed up in my configuration). It seems that
get-mru-window works as expected but switching to a window on another frame seems to be a two step operation under the bonnet, where Emacs selects the previously active window on the target frame (not the one I am switching to) and then quickly jumps to the one I am selecting. Is this behaviour unavoidable when using multiple frames?