2009-11-19

showing pop-ups

Updated: yes, it's %s, not %d Sometimes, it's nice when emacs can warn you when something is happening or should happen. For example, when a new e-mail has arrived, or when there's a meeting in 15 minutes you should attend.

As always, there are different way to do this, but here's what I've been using for while. Various versions of this have been circulating around mailing lists, so I don't know whom to credit with the original idea – anyway, this is the (modified) version that I'm using.

(defun djcb-popup (title msg &optional icon sound)
  "Show a popup if we're on X, or echo it otherwise; TITLE is the title
of the message, MSG is the context. Optionally, you can provide an ICON and
a sound to be played"

  (interactive)
  (when sound (shell-command
                (concat "mplayer -really-quiet " sound " 2> /dev/null")))
  (if (eq window-system 'x)
    (shell-command (concat "notify-send "

                     (if icon (concat "-i " icon) "")
                     " '" title "' '" msg "'"))
    ;; text only version

    (message (concat title ": " msg))))

A couple of notes:

  • I'm using notify-send for sending notifications; this assumes you are using that system (it's part of the libnotify-bin package in Debian/Ubuntu). You can of course replace it with whatever is available on your system. Alternatives are zenity or kdialog or xmessage (for old-timers) and their equivalents (?) on Windows, MacOS.
  • I'm now using mplayer for playing sounds. This is a bit heavy, but at least plays all kinds of audio files. If you only care about .wav-files, you could replace it with e.g. aplay;
  • as always, please ignore my ego-centric function names :-)

Now, we can use this function by evaluation e.g.

(djcb-popup "Warning" "The end is near"
   "/usr/share/icons/test.png" "/usr/share/sounds/beep.ogg")

showing pop-ups from org-mode appointments

The above popup function is most useful when it's does its work based on some event. To be notified of appointments and the like, there is the emacs appt facility. Here, we set up this appt, and then hook it up with org-mode, so appt can warn us when there's something happening soon…

;; the appointment notification facility
(setq
  appt-message-warning-time 15 ;; warn 15 min in advance

  appt-display-mode-line t     ;; show in the modeline
  appt-display-format 'window) ;; use our func
(appt-activate 1)              ;; active appt (appointment notification)
(display-time)                 ;; time display is required for this...

 ;; update appt each time agenda opened

(add-hook 'org-finalize-agenda-hook 'org-agenda-to-appt)

;; our little façade-function for djcb-popup
 (defun djcb-appt-display (min-to-app new-time msg)
    (djcb-popup (format "Appointment in %s minute(s)" min-to-app) msg 
      "/usr/share/icons/gnome/32x32/status/appointment-soon.png"

      "/usr/share/sounds/ubuntu/stereo/phone-incoming-call.ogg"))
  (setq appt-disp-window-function (function djcb-appt-display))

Of course, you can freely choose a icon / sound to your liking.

showing pop-ups for new mail

Another event you might want to be warned about is new mail. There is something to be set for not letting yourself be disturbed for new mail, but if you sufficiently filter your mails before they enter your inbox, it can be a good way to periodically bring you back from your deep sl ^H^H thinking. For Wanderlust, I use something like this:

(add-hook 'wl-biff-notify-hook
    (lambda()
      (djcb-popup "Wanderlust" "You have new mail!"
        "/usr/share/icons/gnome/32x32/status/mail-unread.png"
        "/usr/share/sounds/ubuntu/stereo/phone-incoming-call.ogg")))

Exercise for the reader: adapt this for your chosen mail client.

19 comments:

matt harrison said...

this is cool. I created a tool for doing OSD (with the purpose of showing keystrokes such as emacs during presentations or screencasts) [0]. Now I'm wondering if it would be worthwhile/possible to use notify to do this within emacs itself.

0 - http://github.com/mattharrison/pykeyview

Anonymous said...

A comment for org appt:
In the latest (CVS) org mode, the appt-disp-window-function takes 3 string arguments, so:
(defun djcb-appt-display (min-to-app new-time msg)
(djcb-popup (concat "Appointment in " min-to-app " minute(s)") msg
"/usr/share/icons/gnome/32x32/status/appointment-soon.png"
"/usr/share/sounds/ubuntu/stereo/phone-incoming-call.ogg"))
(setq appt-disp-window-function (function djcb-appt-display))
will work well.

djcb said...

@kiwonum: hmmm... my djcb-apt-display function takes 3 args already; it seems the difference in your proposal is that you replaced 'format' with 'concat'.

Chris said...

I noticed the problem with the string vs integer argument as well, and simply replaced the %d in the title to %s.

I also checked the value of min-to-appt to change the text to fit a bit better.

Here's my version of the wrapper function (helpful comments are welcome, because I'm not too proficient in lisp yet)

(defun djcb-appt-display (min-to-app curr-time msg)
(setq title (format "Appt in %s mins" min-to-app))
(if (= 1 (string-to-int min-to-app))
(setq title "Appt in 1 min"))
(if (= 0 (string-to-int min-to-app))
(setq title "Appt NOW"))
(djcb-popup title msg
"/usr/share/icons/gnome/scalable/status/appointment-soon.svg"))

djcb said...

@kiwonum,@Chris: ah, I see now... I fixed the it (s/%s/%d/). I shouldn't do last minute changes...

@Chris: it's a nice way to experiment with Elisp, You could use 'cond' instead of the 'if's. Any maybe increasingly scary sounds as the deadline draws near... good luck!

wilane said...

Mac users can also use growlnotify. growl.el comes in handy.

John Tells All said...

just for testing, this bit of code works on Ubuntu:

(djcb-popup "Warning" "The end is near"
"/usr/share/icons/Human/scalable/status/user-busy.svg"
"/usr/share/sounds/gnome/default/alerts/glass.ogg")

I'd imagine notify-send has a icon searchpath based on your Gnome theme. For me, finding icons works like this: "find /usr/share/icons/gnome/scalable/"

Examples:

notify-send -i face-cool "free beer"
notify-send -i stock_dialog-warning "it's warm!"

djcb said...

@John M: of course my icon/sound paths are very specific to my machine... but anyway thanks for the great tip about icon-search path in notify-send, did not know that one...

Folgert Karsdorp said...

This sounds really cool. However, it doesn't seem to work for me. I have libnotify installed (ubuntu 9.10) and the latest emacs-snap-shot version. I do manage to see te notification via the elisp interpreter, but not when new email arrives... Do you perhaps have an idea what may be wrong?

Many thanks!

djcb said...

@Folgert Karsdorps: hmmm, do you have things set up on your e-mail program's
side? I.e., if you use Wanderlust, you should have something like:

(setq
wl-biff-check-interval 30 ;; check every 30 seconds
wl-biff-use-idle-timer t) ;; in the background


And may have a look at the other 'wl-biff-' variables. If you're using another
e-mail program... you need to do something else.

HelloWorld said...

Hey this is really awesome.

So if I'm getting this right, in order to work, you have to run the agenda command? Is there a way to add the hook so that it runs after you insert the date (either after C-c . or inserting a deadline)? I don't always run the agenda command.

The other thing is that when I tried this it kept bugging me with popups until I marked the task complete. Is there a way to make it popup once?

HelloWorld said...

I think I may have answered my own question. While there doesn't seem to be another good hook to run org-agenda-to-appt, I did find "run-at-time". The following runs org-agenda-to-appt every hour:

(run-at-time "08:01" 3600 'org-agenda-to-appt)

As for the other question, I think "appt-display-interval" controls the number of popups before the deadline.

I'm not always at my computer, so if I'm not physically present at the computer before the deadline, I'll miss the popups altogether. Is there a way for emacs to keep sending you popups until you mark the task as done? Notifications stop coming up after the deadline time even if you still haven't marked them done.

HelloWorld said...

Unfortunately, after playing around with this all day, it seems that emacs' appt system is limited. If you have two appointments at the same time, you get a notification only for one of them. Also, suppose you have two appointments, say at 12pm and 12:10pm. If the appt warning time is 15 minutes, you get warnings only for the 12pm appointment.

The bottom line is that appt is still useful but you'll still need to check your agenda. Don't rely on the appt warnings.

Anonymous said...

As @HelloWorld said, some emacs users have reported that appt only works for one element when there are many schedules at the same time. So I post my org-appt hack which merges the schedules at same time together:

(defun kiwon/merge-appt-time-msg-list (time-msg-list)
"Merge time-msg-list's elements if they have the same time."
(let* ((merged-time-msg-list (list)))
(while time-msg-list
(if (eq (car (caar time-msg-list)) (car (caar (cdr time-msg-list))))
(setq time-msg-list
(cons
(append
(list (car (car time-msg-list)) ; time
(concat (car (cdr (car time-msg-list))) " / "(car (cdr (car (cdr time-msg-list)))))) ; combined msg
(cdr (cdr (car time-msg-list)))) ; rest information
(nthcdr 2 time-msg-list)))
(progn (add-to-list 'merged-time-msg-list (car time-msg-list) t)
(setq time-msg-list (cdr time-msg-list)))))
merged-time-msg-list))

And you can use this function in org-configuration as follows:

(defun kiwon/org-agenda-to-appt ()
(prog2
(setq appt-time-msg-list nil)
(org-agenda-to-appt)
(setq appt-time-msg-list (kiwon/merge-appt-time-msg-list appt-time-msg-list))))

(add-hook 'org-finalize-agenda-hook (function kiwon/org-agenda-to-appt))

Be careful! If you manage some appt elements not only from org but also others, please keep in mind this hack firstly delete all pre-existing appt-time-msg-list elements!

Good luck!

HelloWorld said...

Thanks for the fix, @Kiwon Um.

It seems to be working with a few tests I did.

One question, the trailing slash at the end of each task is intentional, right?

My "combined" tasks look like this in the popup:

task1 :tag: \
task2 :tag:

with a trailing slash after the first

wik said...

awesome, tnx!

kipuamutay said...

Hi djbc.

just a little modification when calling mplayer. If you are using pulseaudio add the '-ao pulse' parameters.

It ends "mplayer -ao pulse -really-quiet " sound " 2> /dev/null"

When mplayer was being used(just with alsa) and a notification happens at the same time the notification sound was lost

Simon said...

I've been trying this and it works, but it blocks the emacs process for some seconds which is annoying. Has anybody managed to get it work in an async manner?

sping said...

It'd be better to replace the "(shell-command (concat..." with "(start-process ...)".

As it is it's wide open to command injection if the incoming messages can come from uncontrolled sources.