1

I have a function that gets called when idle, but I would like it to run only when a frame has input focus.

My current work around is to use focus-in-hook and focus-out-hook to change the behavior when idle, but I'd like to simplify it by just having one function that would check whether or not it had focus. Here's the code I am working with:

(use-package zone
  :ensure shut-up
  :commands (zone-when-idle)
  :bind ("C-c z" . zone)
  :init
  (setq zone-timeout 30)
  (defun zone-when-idle-and-focused ()
    "Use with `focus-in-hook' to only zone when our focus is back in Emacs."
    (zone-when-idle (or zone-timeout 30)))
  (defun zone-nodoze ()
    "Use with `focus-out-hook' so we don't zone when our focus is elsewhere."
    (let ((inhibit-message t)) (zone-leave-me-alone)))
  (add-hook 'focus-in-hook #'zone-when-idle-and-focused)
  (add-hook 'focus-out-hook #'zone-nodoze)
  (zone-when-idle zone-timeout))
  • Does the function selected-frame help any? – lawlist Jul 14 '16 at 5:38
  • selected-frame doesn't seem to help; it reports the same frame whether or not the frame has input focus when the idle-timer function runs. – aculich Jul 14 '16 at 5:52
  • The following Emacs discussion on the mailing list (initiated by Marcin Borkowski) deals with this issue. Perhaps there is something in there that will answer your question -- How to determine (from Elisp) whether the Emacs frame has focus?: lists.gnu.org/archive/html/help-gnu-emacs/2015-11/msg00160.html – lawlist Jul 14 '16 at 6:22
  • What Marcin asks in the thread sounds like exactly what I am looking for, but I couldn't find an answer that worked and, it seems, neither did he in that discussion. – aculich Jul 14 '16 at 14:17
  • 1
    Note that you can't know whether a terminal frame has input focus. – Gilles Jul 14 '16 at 18:41

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.