16

I'm writing an extension that talks to an outside process, so it is a given to lower the amount of 'hey whats up' requests when my buffer isn't focused.

So, what is the best way to recognize:

  1. When my buffer is visible and focused
  2. When my buffer is visible but not focused
  3. When my buffer is neither visible nor focused
  • Clarifying question: do you mean "how does the outside process recognize" these characteristics? – Dan Nov 1 '14 at 13:55
  • @Dan I think he has a function being run as an idle timer, and needs to know from this function. – Malabarba Nov 1 '14 at 14:53
24
  • window-buffer returns the buffer currently displayed by a given window.

  • get-buffer-window, on the contrary, returns a window currently displaying the given buffer (or nil if there is no such window; play with the optional 2nd argument to tell it how to behave in cases where you have multiple frames).

With these two ingredients, you should be able to discriminate all your cases:

;; my-buffer is supposed to be the buffer you are looking for
(cond ((eq my-buffer (window-buffer (selected-window)))
       (message "Visible and focused"))
      ((get-buffer-window my-buffer)
       (message "Visible and unfocused")) 
      (t
       (message "Not visible")))

Your Answer

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

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