org-notify.el 16 KB

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