4

I use jabber.el to communicate with people at work, and I have sound notifications setup so that I get notified even when I’m not using Emacs. The notification is setup in the following way.

(add-hook 'jabber-alert-message-hooks #'my-jabber-sound-notification)

The problem is: when I’m actually writing in Emacs I already have access to mode-line notifications, and the sound notifications are just a useless distraction.

Is there a way to disable sound notifications while Emacs has focus?

I guess I could write something with an idle timer. But if there’s a way to check if Emacs has focus in the window manager, that would be much nicer.

Using Emacs 24.4

  • 1
    AFAIK, unless you are using Emacs 24.4, the answers will be highly dependent upon the window manager. So it would be helpful to give details about your window system. – Vamsi Sep 30 '14 at 20:00
  • Somewhat related question regarding emacs focus – Vamsi Sep 30 '14 at 20:04
  • @Vamsi I'm in 24.4, fortunately. :-) – Malabarba Sep 30 '14 at 20:19
7

In Emacs 24.4 you should be able to use

(add-hook 'focus-out-hook #'turn-on-my-jabber-sound)
(add-hook 'focus-in-hook #'turn-off-my-jabber-sound)

(defun turn-on-my-jabber-sound ()
  ;; t for `on'
  (setq my-jabber-sound 't))

(defun turn-off-my-jabber-sound ()
  ;; nil for `off'
  (setq my-jabber-sound 'nil))

Then test if my-jabber-sound is t or nil when executing my-jabber-sound-notification (pseudo-lisp below):

(...
  (when my-jabber-sound
    (<play sound>)))
2

I have custom notification code that runs, and like you I often do not want them if I am obviously editing in Emacs at the time the message arrives.

Often I am looking away from Emacs, away from my desk, or something else and emacs still has focus, so messages can be missed easily if only mode line notifications are used or if they are notifications are canceled based on focus. I use lasting OS specific notifications that always run if the corresponding chat buffer is not visible when the message comes in, that way even if you miss the mode line blip, you will always know when messages came through when you return to your desk etc. You have access to which buffer the message belongs to in the custom notification hook, so basically, in your notification hook do...

(unless (get-buffer-window chat-buffer) (do-notification))

Sometimes I may get important chat notifications while I am away from Emacs for a long time but am still connected, in those cases I use current-idle-time to choose whether or not to elevate the notification to an email.

(when (> (time-to-seconds (or (current-idle-time) (seconds-to-time 0)))
         (or min-number-of-idle-seconds-before-email 300))
    (do-custom-email-notification))

In the above code I only perform an email when emacs has been idle for a given number of seconds or 300 seconds (5 minutes) if my custom variable is nil.

With these hooks, I ensure that I won't be bothered with excessive noise while editting, won't miss any messages if I am looking away or gone for a bit, and will also not miss any messages that come even if I am away for hours by getting them by email.

I am hesitant to link this as my jabber configuration is without a doubt, the messiest and least organized file in my .emacs.d, but here is what I use currently in total: .emacs.d/jorbi/jorbi-jabber.el

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.