org-notify.el 16 KB

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