<?xml version="1.0" encoding="iso-8859-1" ?>
<rss version="2.0" 
   xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" 
   xmlns:html="http://www.w3.org/1999/html" 
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
   xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
<channel>
   <title>Phil Groce</title>
   <link>http://philgroce.com/blog</link>
   <description>programming, computing and vanity blogging</description>
   <language>en</language>
   <copyright>Copyright 2006-2008 Phil Groce. All Rights Reserved.</copyright>
   <ttl>60</ttl>
   <pubDate>Tue, 27 May 2008 03:50 GMT</pubDate>
   <managingEditor>p.groce+blog@gmail.com</managingEditor>
   <generator>PyBlosxom http://pyblosxom.sourceforge.net/ 1.4.3 01/10/2008</generator>
<item>
   <title>Send jabber.el Alerts Through Growl</title>
   <guid isPermaLink="false">static/growl-with-emacs-1</guid>
   <link>http://philgroce.com/blog/static/growl-with-emacs-1.pg</link>
   <description><![CDATA[
<!-- STATIC PAGE START -->

<p>Let&#8217;s say you&#8217;re using Emacs as your <a href="http://emacs-jabber.sourceforge.net">Jabber client</a>. (Stop looking at me like that.) You like it well enough, but you&#8217;d like it to be a little more aggressive about telling you when you have a new message.</p>

<p>Oh, and you&#8217;re using a Mac. Because Macs have <a href="http://growl.info">Growl</a>. (You might be able to make this work with <a href="http://gnotify.sourceforge.net/">GNotify</a> or <a href="http://lukeplant.me.uk/articles.php?id=3">KNotify</a> on Linux/*BSD/etc. or <a href="http://www.fullphat.net/">Snarl</a> on Windows.)</p>

<p>Making Emacs send notifications of all kinds through Growl (including those from <code>jabber.el</code>) turns out to be surprisingly easy. Here&#8217;s how I did it.</p>

<h4>Growl</h4>

<p>First you need a way for Emacs to talk to Growl. This is easy, thanks to <a href="http://growl.info/documentation/growlnotify.php">growlnotify</a>. In fact, <a href="http://edward.oconnor.cx/elisp/growl.el">it&#8217;s already been written</a> at least <a href="http://www.emacswiki.org/cgi-bin/wiki/growl.el">twice</a>.</p>

<p>I&#8217;m kind of partial to that last one, but I&#8217;ve tweaked it a bit, thusly:</p>

<pre>
<code class="codelisting">
(defvar growl-program "/usr/local/bin/growlnotify")

(defun growl (title message &amp;optional id)
  (if (eq id nil)
      (start-process "growl" " growl"
                     growl-program title "-w")
    (start-process "growl" " growl"
                   growl-program title "-w" "-d" id))
  (process-send-string " growl" message)
  (process-send-string " growl" "\n")
  (process-send-eof " growl"))


</code>
</pre>

<p>All this does is call <code>growl &#8220;title&#8221; -w</code> and echo the message into it. (<code>growlnotify</code> seems to prefer taking its messages from <span class="caps">STDIN</span>.) The <code>-w</code> keeps the subprocess around until the notification is dismissed&#8212;at least long enough to pipe the message into it. I found that if I didn&#8217;t, the process would go away before I could pipe in the message.</p>

<p>If passed an <code>id</code> argument, <code>growl</code> calls <code>growlnotify</code> with <code>-d &lt;id&gt;</code>. This keeps the desktop clean; if a notification balloon is already active with that id, Growl will reuse it instead of making another one. This is a particular problem with group chats; the server keeps a log of recent messages and sends them to you when you join. Without <code>-d</code>, this results in Growl notification spam taking up a quarter or more of your screen real estate.</p>

<h4>jabber.el</h4>

<p>To tell <code>jabber.el</code> to notify us via Growl when people talk to us, add a hook to <code>jabber-alert-message-hooks</code> (for one-on-one conversations) and another to <code>jabber-alert-muc-hooks</code> (for groupchats or chatrooms, a.k.a multi-user chats, or MUCs).</p>

<pre>
<code class="codelisting">
;; Make jabber.el notify through growl when I get a new message
(setq jabber-message-alert-same-buffer nil)
(defun pg-jabber-growl-notify (from buf text proposed-alert)
  "(jabber.el hook) Notify of new Jabber chat messages via Growl"
  (when (or jabber-message-alert-same-buffer
            (not (memq (selected-window) (get-buffer-window-list buf))))
    (if (jabber-muc-sender-p from)
        (growl (format "(PM) %s" 
                       (jabber-jid-displayname (jabber-jid-user from))) 
               (format "%s: %s" (jabber-jid-resource from) text)
               (format "jabber-from-%s" (jabber-jid-resource from)))
      (growl (format "%s" (jabber-jid-displayname from)) 
             text "jabber-from-unknown"))))
(add-hook 'jabber-alert-message-hooks 'pg-jabber-growl-notify)

;; Same as above, for groupchats
(defun pg-jabber-muc-growl-notify (nick group buf text proposed-alert)
  "(jabber.el hook) Notify of new Jabber MUC messages via Growl"
  (when (or jabber-message-alert-same-buffer
	    (not (memq (selected-window) (get-buffer-window-list buf))))
    (if nick
        (when (or jabber-muc-alert-self
                  (not (string= 
                        nick (cdr (assoc group *jabber-active-groupchats*)))))
          (growl (format "%s" (jabber-jid-displayname group))
                 (format "%s: %s" nick text)
                 (format "jabber-chat-%s" (jabber-jid-displayname group))))
      (growl (format "%s" (jabber-jid-displayname group)) 
             text "jabber-chat-unknown"))))
(add-hook 'jabber-alert-muc-hooks 'pg-jabber-muc-growl-notify)



</code>
</pre>

<p>Some of the niceties I&#8217;ve implemented above:</p>

<ul>
<li>Don&#8217;t alert if the message is coming to the active buffer already, indicating that you&#8217;re already actively participating in the chat. (Note, however, that the buffer remains &#8220;active&#8221; on the Mac if you select another application, so either switch away from any chat buffers before selecting an app other than Emacs, or set <code>jabber-message-alert-same-buffer</code>.)</li>
<li>You won&#8217;t get alerts for messages from yourself unless you set <code>jabber-muc-alert-self</code>. This is rarely a problem, unless you&#8217;re testing this setup by sending messages to yourself&#8230;.</li>
</ul>

<h4>Conclusion and future work</h4>

<p>This approach obviously has a lot of potential. It&#8217;s easy enough to configure new mail notification this way, but I tend not to like that sort of thing; IM on work-related topics is all the distraction I need. I do, however, plan to make this work with upcoming appointments in my <code>org-mode</code> agenda and/or diary. We&#8217;ll see how that goes.</p><!-- STATIC PAGE END -->

]]></description>
   <category domain="http://philgroce.com/blog"></category>
   <pubDate>Tue, 27 May 2008 03:50 GMT</pubDate>
</item>
</channel>
</rss>
