org-expiry.el 13 KB

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