19

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
itsjeyd
  • 14,586
  • 3
  • 58
  • 87
Łukasz Gruner
  • 1,008
  • 8
  • 16
  • Clarifying question: do you mean "how does the *outside process* recognize" these characteristics? – Dan Nov 01 '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 01 '14 at 14:53

1 Answers1

28
  • 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")))
François Févotte
  • 5,917
  • 1
  • 24
  • 37