I am trying to use the code from this answer: Is there a generic toggle-previous-window function?

(defun switch-to-last-window ()
  (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_w1 and f2_w2. When I switch from f1_w1 to f2_w1 there is no problem, but when on the next cycle after f2_w1 -> f1_w1 I switch from f1_w1 to 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?

  • What's the question?
    – Drew
    Sep 30 '18 at 1:49
  • In answer to your revised question "Is this behavior unavoidable when using multiple frames?", "No, it is not unavoidable." I have been using that snippet verbatim for several months. I use 4 monitors, two of which each have a separate frame, each with at least two windows at any given time. I don't doubt that you're experiencing problems. However, I have not have any of the problems you describe. Hopefully you can see where this is going... Have you tried replicating the problem with emacs -Q? What are the minimal steps required to reproduce it? What version of Emacs are you using? Jan 25 '19 at 17:51
  • This post describes how to debug a problem like this: emacs.stackexchange.com/questions/28429/… Jan 25 '19 at 17:54
  • In the end I just went with ace-window, it tracks the windows well and it has go to the last window functionality. Works pretty well for me. Jan 28 '19 at 8:18
  • For those wondering, the function from the ace-window package which accomplishes this is called aw-flip-window (description: Switch to the window you were previously in.). The funcionality of this function is similar to the function tab-bar-switch-to-recent-tab defined in tab-bar.el but for windows instead of tabs.
    – doltes
    Sep 24 '20 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.