org-expiry.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. ;;; org-expiry.el --- expiry mechanism for Org entries
  2. ;;
  3. ;; Copyright 2007-2011 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: bzg AT gnu DOT org
  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 this program; if not, write to the Free Software
  21. ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. ;;
  23. ;;; Commentary:
  24. ;;
  25. ;; This gives you a chance to get rid of old entries in your Org files
  26. ;; by expiring them.
  27. ;;
  28. ;; By default, entries that have no EXPIRY property are considered to be
  29. ;; new (i.e. 0 day old) and only entries older than one year go to the
  30. ;; expiry process, which consist in adding the ARCHIVE tag. None of
  31. ;; your tasks will be deleted with the default settings.
  32. ;;
  33. ;; When does an entry expires?
  34. ;;
  35. ;; Consider this entry:
  36. ;;
  37. ;; * Stop watching TV
  38. ;; :PROPERTIES:
  39. ;; :CREATED: <2008-01-07 lun 08:01>
  40. ;; :EXPIRY: <2008-01-09 08:01>
  41. ;; :END:
  42. ;;
  43. ;; This entry will expire on the 9th, january 2008.
  44. ;; * Stop watching TV
  45. ;; :PROPERTIES:
  46. ;; :CREATED: <2008-01-07 lun 08:01>
  47. ;; :EXPIRY: +1w
  48. ;; :END:
  49. ;;
  50. ;; This entry will expire on the 14th, january 2008, one week after its
  51. ;; creation date.
  52. ;;
  53. ;; What happen when an entry is expired? Nothing until you explicitely
  54. ;; M-x org-expiry-process-entries When doing this, org-expiry will check
  55. ;; for expired entries and request permission to process them.
  56. ;;
  57. ;; Processing an expired entries means calling the function associated
  58. ;; with `org-expiry-handler-function'; the default is to add the tag
  59. ;; :ARCHIVE:, but you can also add a EXPIRED keyword or even archive
  60. ;; the subtree.
  61. ;;
  62. ;; Is this useful? Well, when you're in a brainstorming session, it
  63. ;; might be useful to know about the creation date of an entry, and be
  64. ;; able to archive those entries that are more than xxx days/weeks old.
  65. ;;
  66. ;; When you're in such a session, you can insinuate org-expiry like
  67. ;; this: M-x org-expiry-insinuate
  68. ;;
  69. ;; Then, each time you're pressing M-RET to insert an item, the CREATION
  70. ;; property will be automatically added. Same when you're scheduling or
  71. ;; deadlining items. You can deinsinuate: M-x org-expiry-deinsinuate
  72. ;;; Code:
  73. ;;; User variables:
  74. (defgroup org-expiry nil
  75. "Org expiry process."
  76. :tag "Org Expiry"
  77. :group 'org)
  78. (defcustom org-expiry-inactive-timestamps nil
  79. "Insert inactive timestamps for the created and expired time properties"
  80. :type 'boolean
  81. :group 'org-expiry)
  82. (defcustom org-expiry-created-property-name "CREATED"
  83. "The name of the property for setting the creation date."
  84. :type 'string
  85. :group 'org-expiry)
  86. (defcustom org-expiry-expiry-property-name "EXPIRY"
  87. "The name of the property for setting the expiry date/delay."
  88. :type 'string
  89. :group 'org-expiry)
  90. (defcustom org-expiry-keyword "EXPIRED"
  91. "The default keyword for `org-expiry-add-keyword'."
  92. :type 'string
  93. :group 'org-expiry)
  94. (defcustom org-expiry-wait "+1y"
  95. "Time span between the creation date and the expiry.
  96. The default value for this variable (\"+1y\") means that entries
  97. will expire if there are at least one year old.
  98. If the expiry delay cannot be retrieved from the entry or the
  99. subtree above, the expiry process compares the expiry delay with
  100. `org-expiry-wait'. This can be either an ISO date or a relative
  101. time specification. See `org-read-date' for details."
  102. :type 'string
  103. :group 'org-expiry)
  104. (defcustom org-expiry-created-date "+0d"
  105. "The default creation date.
  106. The default value of this variable (\"+0d\") means that entries
  107. without a creation date will be handled as if they were created
  108. today.
  109. If the creation date cannot be retrieved from the entry or the
  110. subtree above, the expiry process will compare the expiry delay
  111. with this date. This can be either an ISO date or a relative
  112. time specification. See `org-read-date' for details on relative
  113. time specifications."
  114. :type 'string
  115. :group 'org-expiry)
  116. (defcustom org-expiry-handler-function 'org-toggle-archive-tag
  117. "Function to process expired entries.
  118. Possible candidates for this function are:
  119. `org-toggle-archive-tag'
  120. `org-expiry-add-keyword'
  121. `org-expiry-archive-subtree'"
  122. :type 'function
  123. :group 'org-expiry)
  124. (defcustom org-expiry-confirm-flag t
  125. "Non-nil means confirm expiration process."
  126. :type '(choice
  127. (const :tag "Always require confirmation" t)
  128. (const :tag "Do not require confirmation" nil)
  129. (const :tag "Require confirmation in interactive expiry process"
  130. interactive))
  131. :group 'org-expiry)
  132. (defcustom org-expiry-advised-functions
  133. '(org-scheduled org-deadline org-time-stamp)
  134. "A list of advised functions.
  135. `org-expiry-insinuate' will activate the expiry advice for these
  136. functions. `org-expiry-deinsinuate' will deactivate them."
  137. :type 'boolean
  138. :group 'list)
  139. ;;; Advices and insinuation:
  140. (defadvice org-schedule (after org-schedule-update-created)
  141. "Update the creation-date property when calling `org-schedule'."
  142. (org-expiry-insert-created))
  143. (defadvice org-deadline (after org-deadline-update-created)
  144. "Update the creation-date property when calling `org-deadline'."
  145. (org-expiry-insert-created))
  146. (defadvice org-time-stamp (after org-time-stamp-update-created)
  147. "Update the creation-date property when calling `org-time-stamp'."
  148. (org-expiry-insert-created))
  149. (defun org-expiry-insinuate (&optional arg)
  150. "Add hooks and activate advices for org-expiry.
  151. If ARG, also add a hook to `before-save-hook' in `org-mode' and
  152. restart `org-mode' if necessary."
  153. (interactive "P")
  154. (ad-activate 'org-schedule)
  155. (ad-activate 'org-time-stamp)
  156. (ad-activate 'org-deadline)
  157. (add-hook 'org-insert-heading-hook 'org-expiry-insert-created)
  158. (add-hook 'org-after-todo-state-change-hook 'org-expiry-insert-created)
  159. (add-hook 'org-after-tags-change-hook 'org-expiry-insert-created)
  160. (when arg
  161. (add-hook 'org-mode-hook
  162. (lambda() (add-hook 'before-save-hook
  163. 'org-expiry-process-entries t t)))
  164. ;; need this to refresh org-mode hooks
  165. (when (eq major-mode 'org-mode)
  166. (org-mode)
  167. (if (org-called-interactively-p)
  168. (message "Org-expiry insinuated, `org-mode' restarted.")))))
  169. (defun org-expiry-deinsinuate (&optional arg)
  170. "Remove hooks and deactivate advices for org-expiry.
  171. If ARG, also remove org-expiry hook in Org's `before-save-hook'
  172. and restart `org-mode' if necessary."
  173. (interactive "P")
  174. (ad-deactivate 'org-schedule)
  175. (ad-deactivate 'org-time-stamp)
  176. (ad-deactivate 'org-deadline)
  177. (remove-hook 'org-insert-heading-hook 'org-expiry-insert-created)
  178. (remove-hook 'org-after-todo-state-change-hook 'org-expiry-insert-created)
  179. (remove-hook 'org-after-tags-change-hook 'org-expiry-insert-created)
  180. (remove-hook 'org-mode-hook
  181. (lambda() (add-hook 'before-save-hook
  182. 'org-expiry-process-entries t t)))
  183. (when arg
  184. ;; need this to refresh org-mode hooks
  185. (when (eq major-mode 'org-mode)
  186. (org-mode)
  187. (if (org-called-interactively-p)
  188. (message "Org-expiry de-insinuated, `org-mode' restarted.")))))
  189. ;;; org-expiry-expired-p:
  190. (defun org-expiry-expired-p ()
  191. "Check if the entry at point is expired.
  192. Return nil if the entry is not expired. Otherwise return the
  193. amount of time between today and the expiry date.
  194. If there is no creation date, use `org-expiry-created-date'.
  195. If there is no expiry date, use `org-expiry-expiry-date'."
  196. (let* ((ex-prop org-expiry-expiry-property-name)
  197. (cr-prop org-expiry-created-property-name)
  198. (ct (current-time))
  199. (cr (org-read-date nil t (or (org-entry-get (point) cr-prop t) "+0d")))
  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 (org-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 (org-called-interactively-p)
  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 (org-called-interactively-p)
  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 (org-called-interactively-p)
  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