7

frame-list returns a list of references for live Emacs frames. How does one get a reference for the current Emacs frame? I have been unable to find an appropriate function. Current could be either (1) the Emacs frame that has focus (one or zero references) or (2) the Emacs frame that last had focus.

I'm presently looking to use (1) under w32, but for didactic purposes I'm interested in both and whether there are any caveats for focus tracking in Emacs.

ebpa
  • 7,319
  • 26
  • 53
  • 2
    How about `selected-frame`? – glucas Jan 10 '16 at 21:36
  • Yes; that's the synonym I was looking for! My apropos-command searches were coming up empty :-\ – ebpa Jan 10 '16 at 21:41
  • 2
    Use `M-x apropos`, not `apropos-command` (frame functions are generally not commands). – Drew Jan 10 '16 at 23:20
  • 1
    Interesting. That explains why I usually don't get useful information from `C-h a` (`apropos-command`). I primarily rely on `describe-function` and `describe-variable` narrowed with helm. Now that I know of `apropos` I'm going to bind that to `C-h a`! Thanks for pointing that out, @Drew. – ebpa Jan 10 '16 at 23:31
  • 2
    @ebpa: If you're using Helm, you may also be interested in `helm-apropos`, which jams sources for all of the different symbol types (variable, function, etc.) into one Helm command. That's what I use on `C-h a`. – Aaron Harris Jan 11 '16 at 22:50
  • Thanks for the `apropos` vs `apropos-command` tip. I was suffering exactly the same problem, searching for `(selected-frame)`. – Kevin Jun 19 '16 at 01:32

1 Answers1

11

You can use selected-frame and selected-window to get the current frame and window. Also see the focus-in-hook an focus-out-hook hooks (new in Emacs 24.4) if you want to take some action when a frame gains or loses focus.

glucas
  • 20,175
  • 1
  • 51
  • 83
  • I gather that since you mention `focus-in-hook` there isn't any tracking of frame focus? The command description for `select-frame` has a "NORECORD" parameter which only mentions recently selected windows and buffers which supports that. – ebpa Jan 10 '16 at 21:51
  • 1
    The buffer list order is updated when you switch buffers. I believe `select-frame` also calls `select-window`, which changes the current buffer. NORECORD is used to temporarily switch to another frame/window/buffer without changing the buffer list order. I don't think a separate ordered history is maintained for frames or windows. See also `get-mru-window`, which finds the most recently selected window by looking at the `window-use-time`, rather than by querying some ordered list. – glucas Jan 10 '16 at 22:10
  • 1
    See also `select-frame-set-input-focus`. The `selected-frame` does not necessarily have the input focus. – Drew Jan 10 '16 at 23:21