<?xml version="1.0" encoding="iso-8859-1"?>

<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
<title type="text">Phil Groce</title>
<subtitle type="html"><![CDATA[
programming, computing and vanity blogging
]]></subtitle>
<id>http://philgroce.com/blog/static/growl-with-emacs-1/</id>
<link rel="alternate" type="text/html" href="http://philgroce.com/blog" />
<link rel="self" type="application/atom+xml" href="http://philgroce.com/blog/static/growl-with-emacs-1/" />


<author>
<name>Phil Groce</name>
<uri>http://philgroce.com/blog/static/growl-with-emacs-1/</uri>
<email>p.groce+blog@gmail.com</email>
</author>
<rights>Copyright 2006-2008 Phil Groce. All Rights Reserved.</rights>
<generator uri="http://pyblosxom.sourceforge.net/" version="1.4.3 01/10/2008">
PyBlosxom http://pyblosxom.sourceforge.net/ 1.4.3 01/10/2008
</generator>

<updated>2008-05-27T03:50:59Z</updated>
<!-- icon?  logo?  -->

<entry>
<title type="html">Send jabber.el Alerts Through Growl</title>
<category term="" />
<id>http://philgroce.com/blog/2008/05/26/growl-with-emacs-1</id>
<updated>2008-05-27T03:50:59Z</updated>
<published>2008-05-27T03:50:59Z</published>
<link rel="alternate" type="text/html" href="http://philgroce.com/blog/static/growl-with-emacs-1.pg" />
<content type="html">&lt;!-- STATIC PAGE START --&gt;

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

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

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

&lt;h4&gt;Growl&lt;/h4&gt;

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

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

&lt;pre&gt;
&lt;code class=&quot;codelisting&quot;&gt;
(defvar growl-program &quot;/usr/local/bin/growlnotify&quot;)

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


&lt;/code&gt;
&lt;/pre&gt;

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

&lt;p&gt;If passed an &lt;code&gt;id&lt;/code&gt; argument, &lt;code&gt;growl&lt;/code&gt; calls &lt;code&gt;growlnotify&lt;/code&gt; with &lt;code&gt;-d &amp;lt;id&amp;gt;&lt;/code&gt;. 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 &lt;code&gt;-d&lt;/code&gt;, this results in Growl notification spam taking up a quarter or more of your screen real estate.&lt;/p&gt;

&lt;h4&gt;jabber.el&lt;/h4&gt;

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

&lt;pre&gt;
&lt;code class=&quot;codelisting&quot;&gt;
;; 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)
  &quot;(jabber.el hook) Notify of new Jabber chat messages via Growl&quot;
  (when (or jabber-message-alert-same-buffer
            (not (memq (selected-window) (get-buffer-window-list buf))))
    (if (jabber-muc-sender-p from)
        (growl (format &quot;(PM) %s&quot; 
                       (jabber-jid-displayname (jabber-jid-user from))) 
               (format &quot;%s: %s&quot; (jabber-jid-resource from) text)
               (format &quot;jabber-from-%s&quot; (jabber-jid-resource from)))
      (growl (format &quot;%s&quot; (jabber-jid-displayname from)) 
             text &quot;jabber-from-unknown&quot;))))
(add-hook &apos;jabber-alert-message-hooks &apos;pg-jabber-growl-notify)

;; Same as above, for groupchats
(defun pg-jabber-muc-growl-notify (nick group buf text proposed-alert)
  &quot;(jabber.el hook) Notify of new Jabber MUC messages via Growl&quot;
  (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 &quot;%s&quot; (jabber-jid-displayname group))
                 (format &quot;%s: %s&quot; nick text)
                 (format &quot;jabber-chat-%s&quot; (jabber-jid-displayname group))))
      (growl (format &quot;%s&quot; (jabber-jid-displayname group)) 
             text &quot;jabber-chat-unknown&quot;))))
(add-hook &apos;jabber-alert-muc-hooks &apos;pg-jabber-muc-growl-notify)



&lt;/code&gt;
&lt;/pre&gt;

&lt;p&gt;Some of the niceties I&amp;#8217;ve implemented above:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Don&amp;#8217;t alert if the message is coming to the active buffer already, indicating that you&amp;#8217;re already actively participating in the chat. (Note, however, that the buffer remains &amp;#8220;active&amp;#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 &lt;code&gt;jabber-message-alert-same-buffer&lt;/code&gt;.)&lt;/li&gt;
&lt;li&gt;You won&amp;#8217;t get alerts for messages from yourself unless you set &lt;code&gt;jabber-muc-alert-self&lt;/code&gt;. This is rarely a problem, unless you&amp;#8217;re testing this setup by sending messages to yourself&amp;#8230;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;Conclusion and future work&lt;/h4&gt;

&lt;p&gt;This approach obviously has a lot of potential. It&amp;#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 &lt;code&gt;org-mode&lt;/code&gt; agenda and/or diary. We&amp;#8217;ll see how that goes.&lt;/p&gt;&lt;!-- STATIC PAGE END --&gt;
</content>
</entry>
</feed>
