I want to know which frame on a given X11 display was most recently selected. In fact, I want to know which frame that is mapped on the current desktop was most recently selected.

I can tell what display a frame is on with the display frame parameter, and I can enumerate all the frames on the same display as a given frame with next-frame. I can tell whether a frame is mapped with the visibility frame parameter. I can also call visible-frame-alist to list visible frames. But none of this helps me know which of these was most recently selected.

If the most recently selected frame is on the display I'm interested in, the answer is selected-frame. But what if it isn't?

How can I list all the frames (or all the frames on a given display) in the order in which they were last selected?

I want a solution that works in GNU Emacs ≥23. Ideally this should work for all supported display types, not just X11.


1 Answer 1


You might try this:

Whenever a frame is selected (e.g. advise relevant functions), you update a frame parameter on that frame with a timestamp value.

It should then be easy (dunno about performance) to check the frame list for a given display (or whatever), to either look for the most recent frame or to sort the frames.

If this is unclear, let me know and I'll come up with some code. But I think you get the idea: you add your own timestamps, as a frame parameter.

  • 1
    I was thinking of a variant of that: update a MRU frame list on focus-in-hook. I guess updating a time stamp would be faster on focus change, good idea. I was hoping that I'd missed something built in. Jun 1, 2015 at 6:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.