org-notify.el 16 KB

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