1 ;;; org-notify.el --- Notifications for Org-mode
3 ;; Copyright (C) 2012 Free Software Foundation, Inc.
5 ;; Author: Peter Münster <pmrb@free.fr>
6 ;; Keywords: notification, todo-list, alarm, reminder, pop-up
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
23 ;; Get notifications, when there is something to do.
24 ;; Sometimes, you need a reminder a few days before a deadline, e.g. to buy a
25 ;; present for a birthday, and then another notification one hour before to
26 ;; have enough time to choose the right clothes.
27 ;; For other events, e.g. rolling the dustbin to the roadside once per week,
28 ;; you probably need another kind of notification strategy.
29 ;; This package tries to satisfy the various needs.
31 ;; In order to activate this package, you must add the following code
34 ;; (require 'org-notify)
39 ;; (org-notify-add 'appt
40 ;; '(:time "-1s" :period "20s" :duration 10
41 ;; :actions (-message -ding))
42 ;; '(:time "15m" :period "2m" :duration 100
44 ;; '(:time "2h" :period "5m" :actions -message)
45 ;; '(:time "3d" :actions -email))
47 ;; This means for todo-items with `notify' property set to `appt': 3 days
48 ;; before deadline, send a reminder-email, 2 hours before deadline, start to
49 ;; send messages every 5 minutes, then 15 minutes before deadline, start to
50 ;; pop up notification windows every 2 minutes. The timeout of the window is
51 ;; set to 100 seconds. Finally, when deadline is overdue, send messages and
54 ;; Take also a look at the function `org-notify-add'.
58 (eval-when-compile (require 'cl
))
59 (require 'org-element
)
61 (declare-function appt-delete-window
"appt" ())
62 (declare-function notifications-notify
"notifications" (&rest prms
))
63 (declare-function article-lapsed-string
"gnus-art" (t &optional ms
))
65 (defgroup org-notify nil
66 "Options for Org-mode notifications."
70 (defcustom org-notify-audible t
71 "Non-nil means beep to indicate notification."
75 (defconst org-notify-actions
76 '("show" "show" "done" "done" "hour" "one hour later" "day" "one day later"
77 "week" "one week later")
78 "Possible actions for call-back functions.")
80 (defconst org-notify-window-buffer-name
"*org-notify-%s*"
81 "Buffer-name for the `org-notify-action-window' function.")
83 (defvar org-notify-map nil
84 "Mapping between names and parameter lists.")
86 (defvar org-notify-timer nil
87 "Timer of the notification daemon.")
89 (defvar org-notify-parse-file nil
90 "Index of current file, that `org-element-parse-buffer' is parsing.")
92 (defvar org-notify-on-action-map nil
93 "Mapping between on-action identifiers and parameter lists.")
95 (defun org-notify-string->seconds
(str)
96 "Convert time string STR to number of seconds."
98 (let* ((conv `(("s" .
1) ("m" .
60) ("h" .
,(* 60 60))
99 ("d" .
,(* 24 60 60)) ("w" .
,(* 7 24 60 60))
100 ("M" .
,(* 30 24 60 60))))
102 (mapcar (lambda (x) (string-to-char (car x
))) conv
)))
103 (case-fold-search nil
))
104 (string-match (concat "\\(-?\\)\\([0-9]+\\)\\([" letters
"]\\)") str
)
105 (* (string-to-number (match-string 2 str
))
106 (cdr (assoc (match-string 3 str
) conv
))
107 (if (= (length (match-string 1 str
)) 1) -
1 1)))))
109 (defun org-notify-make-todo (heading &rest ignored
)
110 "Create one todo item."
111 (macrolet ((get (k) `(plist-get list
,k
))
112 (pr (k v
) `(setq result
(plist-put result
,k
,v
))))
113 (let* ((list (nth 1 heading
)) (notify (or (get :notify
) "default"))
114 (deadline (get :deadline
)) (heading (get :raw-value
))
116 (when (and (eq (get :todo-type
) 'todo
) heading deadline
)
117 (pr :heading heading
) (pr :notify
(intern notify
))
118 (pr :begin
(get :begin
))
119 (pr :file
(nth org-notify-parse-file
(org-agenda-files 'unrestricted
)))
120 (pr :timestamp deadline
) (pr :uid
(md5 (concat heading deadline
)))
121 (pr :deadline
(- (org-time-string-to-seconds deadline
)
125 (defun org-notify-todo-list ()
126 "Create the todo-list for one org-agenda file."
127 (let* ((files (org-agenda-files 'unrestricted
))
128 (max (1- (length files
))))
129 (setq org-notify-parse-file
130 (if (or (not org-notify-parse-file
) (>= org-notify-parse-file max
))
132 (1+ org-notify-parse-file
)))
134 (with-current-buffer (find-file-noselect
135 (nth org-notify-parse-file files
))
136 (org-element-map (org-element-parse-buffer 'headline
)
137 'headline
'org-notify-make-todo
)))))
139 (defun org-notify-maybe-too-late (diff period heading
)
140 "Print waring message, when notified significantly later than defined by
142 (if (> (/ diff period
) 1.5)
143 (message "Warning: notification for \"%s\" behind schedule!" heading
))
146 (defun org-notify-process ()
147 "Process the todo-list, and possibly notify user about upcoming or
149 (macrolet ((prm (k) `(plist-get prms
,k
)) (td (k) `(plist-get todo
,k
)))
150 (dolist (todo (org-notify-todo-list))
151 (let* ((deadline (td :deadline
)) (heading (td :heading
))
152 (uid (td :uid
)) (last-run-sym
153 (intern (concat ":last-run-" uid
))))
154 (dolist (prms (plist-get org-notify-map
(td :notify
)))
155 (when (< deadline
(org-notify-string->seconds
(prm :time
)))
156 (let ((period (org-notify-string->seconds
(prm :period
)))
157 (last-run (prm last-run-sym
)) (now (org-float-time))
158 (actions (prm :actions
)) diff plist
)
159 (when (or (not last-run
)
160 (and period
(< period
(setq diff
(- now last-run
)))
161 (org-notify-maybe-too-late diff period heading
)))
162 (setq prms
(plist-put prms last-run-sym now
)
163 plist
(append todo prms
))
164 (if (if (plist-member prms
:audible
)
168 (unless (listp actions
)
169 (setq actions
(list actions
)))
170 (dolist (action actions
)
171 (funcall (if (fboundp action
) action
172 (intern (concat "org-notify-action"
173 (symbol-name action
))))
177 (defun org-notify-add (name &rest params
)
178 "Add a new notification type.
179 The NAME can be used in Org-mode property `notify'. If NAME is
180 `default', the notification type applies for todo items without
181 the `notify' property. This file predefines such a default
184 Each element of PARAMS is a list with parameters for a given time
185 distance to the deadline. This distance must increase from one
188 List of possible parameters:
190 :time Time distance to deadline, when this type of notification shall
191 start. It's a string: an integral value (positive or negative)
192 followed by a unit (s, m, h, d, w, M).
193 :actions A function or a list of functions to be called to notify the
194 user. Instead of a function name, you can also supply a suffix
195 of one of the various predefined `org-notify-action-xxx'
197 :period Optional: can be used to repeat the actions periodically.
198 Same format as :time.
199 :duration Some actions use this parameter to specify the duration of the
200 notification. It's an integral number in seconds.
201 :audible Overwrite the value of `org-notify-audible' for this action.
203 For the actions, you can use your own functions or some of the predefined
204 ones, whose names are prefixed with `org-notify-action-'."
205 (setq org-notify-map
(plist-put org-notify-map name params
)))
207 (defun org-notify-start (&optional secs
)
208 "Start the notification daemon.
209 If SECS is positive, it's the period in seconds for processing
210 the notifications of one org-agenda file, and if negative,
211 notifications will be checked only when emacs is idle for -SECS
212 seconds. The default value for SECS is 20."
216 (setq secs
(or secs
20)
217 org-notify-timer
(if (< secs
0)
218 (run-with-idle-timer (* -
1 secs
) t
220 (run-with-timer secs secs
'org-notify-process
))))
222 (defun org-notify-stop ()
223 "Stop the notification daemon."
224 (when org-notify-timer
225 (cancel-timer org-notify-timer
)
226 (setq org-notify-timer nil
)))
228 (defun org-notify-on-action (plist key
)
229 "User wants to see action."
230 (let ((file (plist-get plist
:file
))
231 (begin (plist-get plist
:begin
)))
232 (if (string-equal key
"show")
234 (switch-to-buffer (find-file-noselect file
))
235 (org-with-wide-buffer
239 (search-forward "DEADLINE: <")
240 (if (display-graphic-p)
241 (x-focus-frame nil
)))
243 (with-current-buffer (find-file-noselect file
)
244 (org-with-wide-buffer
246 (search-forward "DEADLINE: <")
248 ((string-equal key
"done") (org-todo))
249 ((string-equal key
"hour") (org-timestamp-change 60 'minute
))
250 ((string-equal key
"day") (org-timestamp-up-day))
251 ((string-equal key
"week") (org-timestamp-change 7 'day
)))))))))
253 (defun org-notify-on-action-notify (id key
)
254 "User wants to see action after mouse-click in notify window."
255 (org-notify-on-action (plist-get org-notify-on-action-map id
) key
)
256 (org-notify-on-close id nil
))
258 (defun org-notify-on-action-button (button)
259 "User wants to see action after button activation."
260 (macrolet ((get (k) `(button-get button
,k
)))
261 (org-notify-on-action (get 'plist
) (get 'key
))
262 (org-notify-delete-window (get 'buffer
))
263 (cancel-timer (get 'timer
))))
265 (defun org-notify-delete-window (buffer)
266 "Delete the notification window."
268 (let ((appt-buffer-name buffer
)
270 (appt-delete-window)))
272 (defun org-notify-on-close (id reason
)
273 "Notification window has been closed."
274 (setq org-notify-on-action-map
(plist-put org-notify-on-action-map id nil
)))
276 (defun org-notify-action-message (plist)
278 (message "TODO: \"%s\" at %s!" (plist-get plist
:heading
)
279 (plist-get plist
:timestamp
)))
281 (defun org-notify-action-ding (plist)
283 (let ((timer (run-with-timer 0 1 'ding
)))
284 (run-with-timer (or (plist-get plist
:duration
) 3) nil
285 'cancel-timer timer
)))
287 (defun org-notify-body-text (plist)
288 "Make human readable string for remaining time to deadline."
291 (replace-regexp-in-string
293 (article-lapsed-string
294 (time-add (current-time)
295 (seconds-to-time (plist-get plist
:deadline
))) 2))
296 (plist-get plist
:timestamp
)))
298 (defun org-notify-action-email (plist)
299 "Send email to user."
300 (compose-mail user-mail-address
(concat "TODO: " (plist-get plist
:heading
)))
301 (insert (org-notify-body-text plist
))
302 (funcall send-mail-function
)
303 (flet ((yes-or-no-p (prompt) t
))
306 (defun org-notify-select-highest-window ()
307 "Select the highest window on the frame, that is not is not an
308 org-notify window. Mostly copied from `appt-select-lowest-window'."
309 (let ((highest-window (selected-window))
310 (bottom-edge (nth 3 (window-edges)))
312 (walk-windows (lambda (w)
314 (not (string-match "^\\*org-notify-.*\\*$"
317 (> bottom-edge
(setq next-bottom-edge
318 (nth 3 (window-edges w
)))))
319 (setq bottom-edge next-bottom-edge
320 highest-window w
))) 'nomini
)
321 (select-window highest-window
)))
323 (defun org-notify-action-window (plist)
324 "Pop up a window, mostly copied from `appt-disp-window'."
326 (macrolet ((get (k) `(plist-get plist
,k
)))
327 (let ((this-window (selected-window))
328 (buf (get-buffer-create
329 (format org-notify-window-buffer-name
(get :uid
)))))
332 (and (minibufferp) (display-multi-frame-p) (other-frame 1)))
333 (if (cdr (assq 'unsplittable
(frame-parameters)))
334 (progn (set-buffer buf
) (display-buffer buf
))
335 (unless (or (special-display-p (buffer-name buf
))
336 (same-window-p (buffer-name buf
)))
337 (org-notify-select-highest-window)
338 (when (>= (window-height) (* 2 window-min-height
))
339 (select-window (split-window nil nil
'above
))))
340 (switch-to-buffer buf
))
341 (setq buffer-read-only nil buffer-undo-list t
)
343 (insert (format "TODO: %s, %s.\n" (get :heading
)
344 (org-notify-body-text plist
)))
345 (let ((timer (run-with-timer (or (get :duration
) 10) nil
346 'org-notify-delete-window buf
)))
347 (dotimes (i (/ (length org-notify-actions
) 2))
348 (let ((key (nth (* i
2) org-notify-actions
))
349 (text (nth (1+ (* i
2)) org-notify-actions
)))
350 (insert-button text
'action
'org-notify-on-action-button
351 'key key
'buffer buf
'plist plist
'timer timer
)
353 (shrink-window-if-larger-than-buffer (get-buffer-window buf t
))
354 (set-buffer-modified-p nil
) (setq buffer-read-only t
)
355 (raise-frame (selected-frame)) (select-window this-window
)))))
357 (defun org-notify-action-notify (plist)
358 "Pop up a notification window."
359 (require 'notifications
)
360 (let* ((duration (plist-get plist
:duration
))
361 (id (notifications-notify
362 :title
(plist-get plist
:heading
)
363 :body
(org-notify-body-text plist
)
364 :timeout
(if duration
(* duration
1000))
365 :actions org-notify-actions
366 :on-action
'org-notify-on-action-notify
)))
367 (setq org-notify-on-action-map
368 (plist-put org-notify-on-action-map id plist
))))
370 (defun org-notify-action-notify/window
(plist)
371 "For a graphics display, pop up a notification window, for a text
372 terminal an emacs window."
373 (if (display-graphic-p)
374 (org-notify-action-notify plist
)
375 (org-notify-action-window plist
)))
377 ;;; Provide a minimal default setup.
378 (org-notify-add 'default
'(:time
"1h" :actions -notify
/window
379 :period
"2m" :duration
60))
381 (provide 'org-notify
)
383 ;;; org-notify.el ends here