org-expiry.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. ;;; org-expiry.el --- expiry mechanism for Org entries
  2. ;;
  3. ;; Copyright 2007-2017 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Bastien Guerry
  6. ;; Version: 0.2
  7. ;; Keywords: org expiry
  8. ;; This file is not part of GNU Emacs.
  9. ;; This program is free software; you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 3, or (at your option)
  12. ;; any later version.
  13. ;;
  14. ;; This program is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;;
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  21. ;;
  22. ;;; Commentary:
  23. ;;
  24. ;; This gives you a chance to get rid of old entries in your Org files
  25. ;; by expiring them.
  26. ;;
  27. ;; By default, entries that have no EXPIRY property are considered to be
  28. ;; new (i.e. 0 day old) and only entries older than one year go to the
  29. ;; expiry process, which consist in adding the ARCHIVE tag. None of
  30. ;; your tasks will be deleted with the default settings.
  31. ;;
  32. ;; When does an entry expires?
  33. ;;
  34. ;; Consider this entry:
  35. ;;
  36. ;; * Stop watching TV
  37. ;; :PROPERTIES:
  38. ;; :CREATED: <2008-01-07 lun 08:01>
  39. ;; :EXPIRY: <2008-01-09 08:01>
  40. ;; :END:
  41. ;;
  42. ;; This entry will expire on the 9th, january 2008.
  43. ;; * Stop watching TV
  44. ;; :PROPERTIES:
  45. ;; :CREATED: <2008-01-07 lun 08:01>
  46. ;; :EXPIRY: +1w
  47. ;; :END:
  48. ;;
  49. ;; This entry will expire on the 14th, january 2008, one week after its
  50. ;; creation date.
  51. ;;
  52. ;; What happen when an entry is expired? Nothing until you explicitely
  53. ;; M-x org-expiry-process-entries When doing this, org-expiry will check
  54. ;; for expired entries and request permission to process them.
  55. ;;
  56. ;; Processing an expired entries means calling the function associated
  57. ;; with `org-expiry-handler-function'; the default is to add the tag
  58. ;; :ARCHIVE:, but you can also add a EXPIRED keyword or even archive
  59. ;; the subtree.
  60. ;;
  61. ;; Is this useful? Well, when you're in a brainstorming session, it
  62. ;; might be useful to know about the creation date of an entry, and be
  63. ;; able to archive those entries that are more than xxx days/weeks old.
  64. ;;
  65. ;; When you're in such a session, you can insinuate org-expiry like
  66. ;; this: M-x org-expiry-insinuate
  67. ;;
  68. ;; Then, each time you're pressing M-RET to insert an item, the CREATION
  69. ;; property will be automatically added. Same when you're scheduling or
  70. ;; deadlining items. You can deinsinuate: M-x org-expiry-deinsinuate
  71. ;;; Code:
  72. ;;; User variables:
  73. (defgroup org-expiry nil
  74. "Org expiry process."
  75. :tag "Org Expiry"
  76. :group 'org)
  77. (defcustom org-expiry-inactive-timestamps nil
  78. "Insert inactive timestamps for created/expired properties."
  79. :type 'boolean
  80. :group 'org-expiry)
  81. (defcustom org-expiry-created-property-name "CREATED"
  82. "The name of the property for setting the creation date."
  83. :type 'string
  84. :group 'org-expiry)
  85. (defcustom org-expiry-expiry-property-name "EXPIRY"
  86. "The name of the property for setting the expiry date/delay."
  87. :type 'string
  88. :group 'org-expiry)
  89. (defcustom org-expiry-keyword "EXPIRED"
  90. "The default keyword for `org-expiry-add-keyword'."
  91. :type 'string
  92. :group 'org-expiry)
  93. (defcustom org-expiry-wait "+1y"
  94. "Time span between the creation date and the expiry.
  95. The default value for this variable (\"+1y\") means that entries
  96. will expire if there are at least one year old.
  97. If the expiry delay cannot be retrieved from the entry or the
  98. subtree above, the expiry process compares the expiry delay with
  99. `org-expiry-wait'. This can be either an ISO date or a relative
  100. time specification. See `org-read-date' for details."
  101. :type 'string
  102. :group 'org-expiry)
  103. (defcustom org-expiry-created-date "+0d"
  104. "The default creation date.
  105. The default value of this variable (\"+0d\") means that entries
  106. without a creation date will be handled as if they were created
  107. today.
  108. If the creation date cannot be retrieved from the entry or the
  109. subtree above, the expiry process will compare the expiry delay
  110. with this date. This can be either an ISO date or a relative
  111. time specification. See `org-read-date' for details on relative
  112. time specifications."
  113. :type 'string
  114. :group 'org-expiry)
  115. (defcustom org-expiry-handler-function 'org-toggle-archive-tag
  116. "Function to process expired entries.
  117. Possible candidates for this function are:
  118. `org-toggle-archive-tag'
  119. `org-expiry-add-keyword'
  120. `org-expiry-archive-subtree'"
  121. :type 'function
  122. :group 'org-expiry)
  123. (defcustom org-expiry-confirm-flag t
  124. "Non-nil means confirm expiration process."
  125. :type '(choice
  126. (const :tag "Always require confirmation" t)
  127. (const :tag "Do not require confirmation" nil)
  128. (const :tag "Require confirmation in interactive expiry process"
  129. interactive))
  130. :group 'org-expiry)
  131. (defcustom org-expiry-advised-functions
  132. '(org-scheduled org-deadline org-time-stamp)
  133. "A list of advised functions.
  134. `org-expiry-insinuate' will activate the expiry advice for these
  135. functions. `org-expiry-deinsinuate' will deactivate them."
  136. :type 'boolean
  137. :group 'list)
  138. ;;; Advices and insinuation:
  139. (defadvice org-schedule (after org-schedule-update-created)
  140. "Update the creation-date property when calling `org-schedule'."
  141. (org-expiry-insert-created))
  142. (defadvice org-deadline (after org-deadline-update-created)
  143. "Update the creation-date property when calling `org-deadline'."
  144. (org-expiry-insert-created))
  145. (defadvice org-time-stamp (after org-time-stamp-update-created)
  146. "Update the creation-date property when calling `org-time-stamp'."
  147. (org-expiry-insert-created))
  148. (defun org-expiry-insinuate (&optional arg)
  149. "Add hooks and activate advices for org-expiry.
  150. If ARG, also add a hook to `before-save-hook' in `org-mode' and
  151. restart `org-mode' if necessary."
  152. (interactive "P")
  153. (ad-activate 'org-schedule)
  154. (ad-activate 'org-time-stamp)
  155. (ad-activate 'org-deadline)
  156. (add-hook 'org-insert-heading-hook 'org-expiry-insert-created)
  157. (add-hook 'org-after-todo-state-change-hook 'org-expiry-insert-created)
  158. (add-hook 'org-after-tags-change-hook 'org-expiry-insert-created)
  159. (when arg
  160. (add-hook 'org-mode-hook
  161. (lambda() (add-hook 'before-save-hook
  162. 'org-expiry-process-entries t t)))
  163. ;; need this to refresh org-mode hooks
  164. (when (eq major-mode 'org-mode)
  165. (org-mode)
  166. (if (called-interactively-p 'any)
  167. (message "Org-expiry insinuated, `org-mode' restarted.")))))
  168. (defun org-expiry-deinsinuate (&optional arg)
  169. "Remove hooks and deactivate advices for org-expiry.
  170. If ARG, also remove org-expiry hook in Org's `before-save-hook'
  171. and restart `org-mode' if necessary."
  172. (interactive "P")
  173. (ad-deactivate 'org-schedule)
  174. (ad-deactivate 'org-time-stamp)
  175. (ad-deactivate 'org-deadline)
  176. (remove-hook 'org-insert-heading-hook 'org-expiry-insert-created)
  177. (remove-hook 'org-after-todo-state-change-hook 'org-expiry-insert-created)
  178. (remove-hook 'org-after-tags-change-hook 'org-expiry-insert-created)
  179. (remove-hook 'org-mode-hook
  180. (lambda() (add-hook 'before-save-hook
  181. 'org-expiry-process-entries t t)))
  182. (when arg
  183. ;; need this to refresh org-mode hooks
  184. (when (eq major-mode 'org-mode)
  185. (org-mode)
  186. (if (called-interactively-p 'any)
  187. (message "Org-expiry de-insinuated, `org-mode' restarted.")))))
  188. ;;; org-expiry-expired-p:
  189. (defun org-expiry-expired-p ()
  190. "Check if the entry at point is expired.
  191. Return nil if the entry is not expired. Otherwise return the
  192. amount of time between today and the expiry date.
  193. If there is no creation date, use `org-expiry-created-date'.
  194. If there is no expiry date, use `org-expiry-wait'."
  195. (let* ((ex-prop org-expiry-expiry-property-name)
  196. (cr-prop org-expiry-created-property-name)
  197. (ct (current-time))
  198. (cr (org-read-date nil t (or (org-entry-get (point) cr-prop t)
  199. org-expiry-created-date)))
  200. (ex-field (or (org-entry-get (point) ex-prop t) org-expiry-wait))
  201. (ex (if (string-match "^[ \t]?[+-]" ex-field)
  202. (time-add cr (time-subtract (org-read-date nil t ex-field) ct))
  203. (org-read-date nil t ex-field))))
  204. (if (time-less-p ex ct)
  205. (time-subtract ct ex))))
  206. ;;; Expire an entry or a region/buffer:
  207. (defun org-expiry-process-entry (&optional force)
  208. "Call `org-expiry-handler-function' on entry.
  209. If FORCE is non-nil, don't require confirmation from the user.
  210. Otherwise rely on `org-expiry-confirm-flag' to decide."
  211. (interactive "P")
  212. (save-excursion
  213. (when (called-interactively-p) (org-reveal))
  214. (when (org-expiry-expired-p)
  215. (org-back-to-heading)
  216. (looking-at org-complex-heading-regexp)
  217. (let* ((ov (make-overlay (point) (match-end 0)))
  218. (e (org-expiry-expired-p))
  219. (d (time-to-number-of-days e)))
  220. (overlay-put ov 'face 'secondary-selection)
  221. (if (or force
  222. (null org-expiry-confirm-flag)
  223. (and (eq org-expiry-confirm-flag 'interactive)
  224. (not (interactive)))
  225. (and org-expiry-confirm-flag
  226. (y-or-n-p (format "Entry expired by %d days. Process? " d))))
  227. (funcall org-expiry-handler-function))
  228. (delete-overlay ov)))))
  229. (defun org-expiry-process-entries (beg end)
  230. "Process all expired entries between BEG and END.
  231. The expiry process will run the function defined by
  232. `org-expiry-handler-functions'."
  233. (interactive "r")
  234. (save-excursion
  235. (let ((beg (if (org-region-active-p)
  236. (region-beginning) (point-min)))
  237. (end (if (org-region-active-p)
  238. (region-end) (point-max))))
  239. (goto-char beg)
  240. (let ((expired 0) (processed 0))
  241. (while (and (outline-next-heading) (< (point) end))
  242. (when (org-expiry-expired-p)
  243. (setq expired (1+ expired))
  244. (if (if (called-interactively-p 'any)
  245. (call-interactively 'org-expiry-process-entry)
  246. (org-expiry-process-entry))
  247. (setq processed (1+ processed)))))
  248. (if (equal expired 0)
  249. (message "No expired entry")
  250. (message "Processed %d on %d expired entries"
  251. processed expired))))))
  252. ;;; Insert created/expiry property:
  253. (defun org-expiry-insert-created (&optional arg)
  254. "Insert or update a property with the creation date.
  255. If ARG, always update it. With one `C-u' prefix, silently update
  256. to today's date. With two `C-u' prefixes, prompt the user for to
  257. update the date."
  258. (interactive "P")
  259. (let* ((d (org-entry-get (point) org-expiry-created-property-name))
  260. d-time d-hour timestr)
  261. (when (or (null d) arg)
  262. ;; update if no date or non-nil prefix argument
  263. ;; FIXME Use `org-time-string-to-time'
  264. (setq d-time (if d (org-time-string-to-time d)
  265. (current-time)))
  266. (setq d-hour (format-time-string "%H:%M" d-time))
  267. (setq timestr
  268. ;; two C-u prefixes will call org-read-date
  269. (if (equal arg '(16))
  270. (concat "<" (org-read-date
  271. nil nil nil nil d-time d-hour) ">")
  272. (format-time-string (cdr org-time-stamp-formats))))
  273. ;; maybe transform to inactive timestamp
  274. (if org-expiry-inactive-timestamps
  275. (setq timestr (concat "[" (substring timestr 1 -1) "]")))
  276. (save-excursion
  277. (org-entry-put
  278. (point) org-expiry-created-property-name timestr)))))
  279. (defun org-expiry-insert-expiry (&optional today)
  280. "Insert a property with the expiry date.
  281. With one `C-u' prefix, don't prompt interactively for the date
  282. and insert today's date."
  283. (interactive "P")
  284. (let* ((d (org-entry-get (point) org-expiry-expiry-property-name))
  285. d-time d-hour)
  286. (setq d-time (if d (org-time-string-to-time d)
  287. (current-time)))
  288. (setq d-hour (format-time-string "%H:%M" d-time))
  289. (setq timestr (if today
  290. (format-time-string (cdr org-time-stamp-formats))
  291. (concat "<" (org-read-date
  292. nil nil nil nil d-time d-hour) ">")))
  293. ;; maybe transform to inactive timestamp
  294. (if org-expiry-inactive-timestamps
  295. (setq timestr (concat "[" (substring timestr 1 -1) "]")))
  296. (save-excursion
  297. (org-entry-put
  298. (point) org-expiry-expiry-property-name timestr))))
  299. ;;; Functions to process expired entries:
  300. (defun org-expiry-archive-subtree ()
  301. "Archive the entry at point if it is expired."
  302. (interactive)
  303. (save-excursion
  304. (if (org-expiry-expired-p)
  305. (org-archive-subtree)
  306. (if (called-interactively-p 'any)
  307. (message "Entry at point is not expired.")))))
  308. (defun org-expiry-add-keyword (&optional keyword)
  309. "Add KEYWORD to the entry at point if it is expired."
  310. (interactive "sKeyword: ")
  311. (if (or (member keyword org-todo-keywords-1)
  312. (setq keyword org-expiry-keyword))
  313. (save-excursion
  314. (if (org-expiry-expired-p)
  315. (org-todo keyword)
  316. (if (called-interactively-p 'any)
  317. (message "Entry at point is not expired."))))
  318. (error "\"%s\" is not a to-do keyword in this buffer" keyword)))
  319. ;; FIXME what about using org-refile ?
  320. (provide 'org-expiry)
  321. ;;; org-expiry.el ends here