org-expiry.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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-created-property-name "CREATED"
  80. "The name of the property for setting the creation date."
  81. :type 'string
  82. :group 'org-expiry)
  83. (defcustom org-expiry-expiry-property-name "EXPIRY"
  84. "The name of the property for setting the expiry date/delay."
  85. :type 'string
  86. :group 'org-expiry)
  87. (defcustom org-expiry-keyword "EXPIRED"
  88. "The default keyword for `org-expiry-add-keyword'."
  89. :type 'string
  90. :group 'org-expiry)
  91. (defcustom org-expiry-wait "+1y"
  92. "Time span between the creation date and the expiry.
  93. The default value for this variable (\"+1y\") means that entries
  94. will expire if there are at least one year old.
  95. If the expiry delay cannot be retrieved from the entry or the
  96. subtree above, the expiry process compares the expiry delay with
  97. `org-expiry-wait'. This can be either an ISO date or a relative
  98. time specification. See `org-read-date' for details."
  99. :type 'string
  100. :group 'org-expiry)
  101. (defcustom org-expiry-created-date "+0d"
  102. "The default creation date.
  103. The default value of this variable (\"+0d\") means that entries
  104. without a creation date will be handled as if they were created
  105. today.
  106. If the creation date cannot be retrieved from the entry or the
  107. subtree above, the expiry process will compare the expiry delay
  108. with this date. This can be either an ISO date or a relative
  109. time specification. See `org-read-date' for details on relative
  110. time specifications."
  111. :type 'string
  112. :group 'org-expiry)
  113. (defcustom org-expiry-handler-function 'org-toggle-archive-tag
  114. "Function to process expired entries.
  115. Possible candidates for this function are:
  116. `org-toggle-archive-tag'
  117. `org-expiry-add-keyword'
  118. `org-expiry-archive-subtree'"
  119. :type 'function
  120. :group 'org-expiry)
  121. (defcustom org-expiry-confirm-flag t
  122. "Non-nil means confirm expiration process."
  123. :type '(choice
  124. (const :tag "Always require confirmation" t)
  125. (const :tag "Do not require confirmation" nil)
  126. (const :tag "Require confirmation in interactive expiry process"
  127. interactive))
  128. :group 'org-expiry)
  129. (defcustom org-expiry-advised-functions
  130. '(org-scheduled org-deadline org-time-stamp)
  131. "A list of advised functions.
  132. `org-expiry-insinuate' will activate the expiry advice for these
  133. functions. `org-expiry-deinsinuate' will deactivate them."
  134. :type 'boolean
  135. :group 'list)
  136. ;;; Advices and insinuation:
  137. (defadvice org-schedule (after org-schedule-update-created)
  138. "Update the creation-date property when calling `org-schedule'."
  139. (org-expiry-insert-created))
  140. (defadvice org-deadline (after org-deadline-update-created)
  141. "Update the creation-date property when calling `org-deadline'."
  142. (org-expiry-insert-created))
  143. (defadvice org-time-stamp (after org-time-stamp-update-created)
  144. "Update the creation-date property when calling `org-time-stamp'."
  145. (org-expiry-insert-created))
  146. (defun org-expiry-insinuate (&optional arg)
  147. "Add hooks and activate advices for org-expiry.
  148. If ARG, also add a hook to `before-save-hook' in `org-mode' and
  149. restart `org-mode' if necessary."
  150. (interactive "P")
  151. (ad-activate 'org-schedule)
  152. (ad-activate 'org-time-stamp)
  153. (ad-activate 'org-deadline)
  154. (add-hook 'org-insert-heading-hook 'org-expiry-insert-created)
  155. (add-hook 'org-after-todo-state-change-hook 'org-expiry-insert-created)
  156. (add-hook 'org-after-tags-change-hook 'org-expiry-insert-created)
  157. (when arg
  158. (add-hook 'org-mode-hook
  159. (lambda() (add-hook 'before-save-hook
  160. 'org-expiry-process-entries t t)))
  161. ;; need this to refresh org-mode hooks
  162. (when (org-mode-p)
  163. (org-mode)
  164. (if (interactive-p)
  165. (message "Org-expiry insinuated, `org-mode' restarted.")))))
  166. (defun org-expiry-deinsinuate (&optional arg)
  167. "Remove hooks and deactivate advices for org-expiry.
  168. If ARG, also remove org-expiry hook in Org's `before-save-hook'
  169. and restart `org-mode' if necessary."
  170. (interactive "P")
  171. (ad-deactivate 'org-schedule)
  172. (ad-deactivate 'org-time-stamp)
  173. (ad-deactivate 'org-deadline)
  174. (remove-hook 'org-insert-heading-hook 'org-expiry-insert-created)
  175. (remove-hook 'org-after-todo-state-change-hook 'org-expiry-insert-created)
  176. (remove-hook 'org-after-tags-change-hook 'org-expiry-insert-created)
  177. (remove-hook 'org-mode-hook
  178. (lambda() (add-hook 'before-save-hook
  179. 'org-expiry-process-entries t t)))
  180. (when arg
  181. ;; need this to refresh org-mode hooks
  182. (when (org-mode-p)
  183. (org-mode)
  184. (if (interactive-p)
  185. (message "Org-expiry de-insinuated, `org-mode' restarted.")))))
  186. ;;; org-expiry-expired-p:
  187. (defun org-expiry-expired-p ()
  188. "Check if the entry at point is expired.
  189. Return nil if the entry is not expired. Otherwise return the
  190. amount of time between today and the expiry date.
  191. If there is no creation date, use `org-expiry-created-date'.
  192. If there is no expiry date, use `org-expiry-expiry-date'."
  193. (let* ((ex-prop org-expiry-expiry-property-name)
  194. (cr-prop org-expiry-created-property-name)
  195. (ct (current-time))
  196. (cr (org-read-date nil t (or (org-entry-get (point) cr-prop t) "+0d")))
  197. (ex-field (or (org-entry-get (point) ex-prop t) org-expiry-wait))
  198. (ex (if (string-match "^[ \t]?[+-]" ex-field)
  199. (time-add cr (time-subtract (org-read-date nil t ex-field) ct))
  200. (org-read-date nil t ex-field))))
  201. (if (time-less-p ex ct)
  202. (time-subtract ct ex))))
  203. ;;; Expire an entry or a region/buffer:
  204. (defun org-expiry-process-entry (&optional force)
  205. "Call `org-expiry-handler-function' on entry.
  206. If FORCE is non-nil, don't require confirmation from the user.
  207. Otherwise rely on `org-expiry-confirm-flag' to decide."
  208. (interactive "P")
  209. (save-excursion
  210. (when (interactive-p) (org-reveal))
  211. (when (org-expiry-expired-p)
  212. (org-back-to-heading)
  213. (looking-at org-complex-heading-regexp)
  214. (let* ((ov (make-overlay (point) (match-end 0)))
  215. (e (org-expiry-expired-p))
  216. (d (time-to-number-of-days e)))
  217. (overlay-put ov 'face 'secondary-selection)
  218. (if (or force
  219. (null org-expiry-confirm-flag)
  220. (and (eq org-expiry-confirm-flag 'interactive)
  221. (not (interactive)))
  222. (and org-expiry-confirm-flag
  223. (y-or-n-p (format "Entry expired by %d days. Process? " d))))
  224. (funcall 'org-expiry-handler-function))
  225. (delete-overlay ov)))))
  226. (defun org-expiry-process-entries (beg end)
  227. "Process all expired entries between BEG and END.
  228. The expiry process will run the function defined by
  229. `org-expiry-handler-functions'."
  230. (interactive "r")
  231. (save-excursion
  232. (let ((beg (if (org-region-active-p)
  233. (region-beginning) (point-min)))
  234. (end (if (org-region-active-p)
  235. (region-end) (point-max))))
  236. (goto-char beg)
  237. (let ((expired 0) (processed 0))
  238. (while (and (outline-next-heading) (< (point) end))
  239. (when (org-expiry-expired-p)
  240. (setq expired (1+ expired))
  241. (if (if (interactive-p)
  242. (call-interactively 'org-expiry-process-entry)
  243. (org-expiry-process-entry))
  244. (setq processed (1+ processed)))))
  245. (if (equal expired 0)
  246. (message "No expired entry")
  247. (message "Processed %d on %d expired entries"
  248. processed expired))))))
  249. ;;; Insert created/expiry property:
  250. (defun org-expiry-insert-created (&optional arg)
  251. "Insert or update a property with the creation date.
  252. If ARG, always update it. With one `C-u' prefix, silently update
  253. to today's date. With two `C-u' prefixes, prompt the user for to
  254. update the date."
  255. (interactive "P")
  256. (let* ((d (org-entry-get (point) org-expiry-created-property-name))
  257. d-time d-hour)
  258. (when (or (null d) arg)
  259. ;; update if no date or non-nil prefix argument
  260. ;; FIXME Use `org-time-string-to-time'
  261. (setq d-time (if d (apply 'encode-time (org-parse-time-string d))
  262. (current-time)))
  263. (setq d-hour (format-time-string "%H:%M" d-time))
  264. (save-excursion
  265. (org-entry-put
  266. (point) org-expiry-created-property-name
  267. ;; two C-u prefixes will call org-read-date
  268. (if (equal arg '(16))
  269. (concat "<" (org-read-date
  270. nil nil nil nil d-time d-hour) ">")
  271. (format-time-string (cdr org-time-stamp-formats))))))))
  272. (defun org-expiry-insert-expiry (&optional today)
  273. "Insert a property with the expiry date.
  274. With one `C-u' prefix, don't prompt interactively for the date
  275. and insert today's date."
  276. (interactive "P")
  277. (let* ((d (org-entry-get (point) org-expiry-expiry-property-name))
  278. d-time d-hour)
  279. (setq d-time (if d (apply 'encode-time (org-parse-time-string d))
  280. (current-time)))
  281. (setq d-hour (format-time-string "%H:%M" d-time))
  282. (save-excursion
  283. (org-entry-put
  284. (point) org-expiry-expiry-property-name
  285. (if today (format-time-string (cdr org-time-stamp-formats))
  286. (concat "<" (org-read-date
  287. nil nil nil nil d-time d-hour) ">"))))))
  288. ;;; Functions to process expired entries:
  289. (defun org-expiry-archive-subtree ()
  290. "Archive the entry at point if it is expired."
  291. (interactive)
  292. (save-excursion
  293. (if (org-expiry-expired-p)
  294. (org-archive-subtree)
  295. (if (interactive-p)
  296. (message "Entry at point is not expired.")))))
  297. (defun org-expiry-add-keyword (&optional keyword)
  298. "Add KEYWORD to the entry at point if it is expired."
  299. (interactive "sKeyword: ")
  300. (if (or (member keyword org-todo-keywords-1)
  301. (setq keyword org-expiry-keyword))
  302. (save-excursion
  303. (if (org-expiry-expired-p)
  304. (org-todo keyword)
  305. (if (interactive-p)
  306. (message "Entry at point is not expired."))))
  307. (error "\"%s\" is not a to-do keyword in this buffer" keyword)))
  308. ;; FIXME what about using org-refile ?
  309. (provide 'org-expiry)
  310. ;;; org-expiry.el ends here