1 ;;; org-expiry.el --- expiry mechanism for Org entries
3 ;; Copyright 2007 2008 Bastien Guerry
5 ;; Author: bzg AT altern DOT org
7 ;; Keywords: org expiry
8 ;; URL: http://www.cognition.ens.fr/~guerry/u/org-expiry.el
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)
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.
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.
26 ;; This gives you a chance to get rid of old entries in your Org files
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.
34 ;; When does an entry expires?
36 ;; Consider this entry:
40 ;; :CREATED: <2008-01-07 lun 08:01>
41 ;; :EXPIRY: <2008-01-09 08:01>
44 ;; This entry will expire on the 9th, january 2008.
48 ;; :CREATED: <2008-01-07 lun 08:01>
52 ;; This entry will expire on the 14th, january 2008, one week after its
55 ;; What happen when an entry is expired? Nothing until you explicitely
56 ;; M-x org-expiry-process-entries When doing this, org-expiry will check
57 ;; for expired entries and request permission to process them.
59 ;; Processing an expired entries means calling the function associated
60 ;; with `org-expiry-handler-function'; the default is to add the tag
61 ;; :ARCHIVE:, but you can also add a EXPIRED keyword or even archive
64 ;; Is this useful? Well, when you're in a brainstorming session, it
65 ;; might be useful to know about the creation date of an entry, and be
66 ;; able to archive those entries that are more than xxx days/weeks old.
68 ;; When you're in such a session, you can insinuate org-expiry like
69 ;; this: M-x org-expiry-insinuate
71 ;; Then, each time you're pressing M-RET to insert an item, the CREATION
72 ;; property will be automatically added. Same when you're scheduling or
73 ;; deadlining items. You can deinsinuate: M-x org-expiry-deinsinuate
79 (defgroup org-expiry nil
84 (defcustom org-expiry-created-property-name
"CREATED"
85 "The name of the property for setting the creation date."
89 (defcustom org-expiry-expiry-property-name
"EXPIRY"
90 "The name of the property for setting the expiry date/delay."
94 (defcustom org-expiry-keyword
"EXPIRED"
95 "The default keyword for `org-expiry-add-keyword'."
99 (defcustom org-expiry-wait
"+1y"
100 "Time span between the creation date and the expiry.
101 The default value for this variable (\"+1y\") means that entries
102 will expire if there are at least one year old.
104 If the expiry delay cannot be retrieved from the entry or the
105 subtree above, the expiry process compares the expiry delay with
106 `org-expiry-wait'. This can be either an ISO date or a relative
107 time specification. See `org-read-date' for details."
111 (defcustom org-expiry-created-date
"+0d"
112 "The default creation date.
113 The default value of this variable (\"+0d\") means that entries
114 without a creation date will be handled as if they were created
117 If the creation date cannot be retrieved from the entry or the
118 subtree above, the expiry process will compare the expiry delay
119 with this date. This can be either an ISO date or a relative
120 time specification. See `org-read-date' for details on relative
121 time specifications."
125 (defcustom org-expiry-handler-function
'org-toggle-archive-tag
126 "Function to process expired entries.
127 Possible candidates for this function are:
129 `org-toggle-archive-tag'
130 `org-expiry-add-keyword'
131 `org-expiry-archive-subtree'"
135 (defcustom org-expiry-confirm-flag t
136 "Non-nil means confirm expiration process."
138 (const :tag
"Always require confirmation" t
)
139 (const :tag
"Do not require confirmation" nil
)
140 (const :tag
"Require confirmation in interactive expiry process"
144 (defcustom org-expiry-advised-functions
145 '(org-scheduled org-deadline org-time-stamp
)
146 "A list of advised functions.
147 `org-expiry-insinuate' will activate the expiry advice for these
148 functions. `org-expiry-deinsinuate' will deactivate them."
152 ;;; Advices and insinuation:
154 (defadvice org-schedule
(after org-schedule-update-created
)
155 "Update the creation-date property when calling `org-schedule'."
156 (org-expiry-insert-created))
158 (defadvice org-deadline
(after org-deadline-update-created
)
159 "Update the creation-date property when calling `org-deadline'."
160 (org-expiry-insert-created))
162 (defadvice org-time-stamp
(after org-time-stamp-update-created
)
163 "Update the creation-date property when calling `org-time-stamp'."
164 (org-expiry-insert-created))
166 (defun org-expiry-insinuate (&optional arg
)
167 "Add hooks and activate advices for org-expiry.
168 If ARG, also add a hook to `before-save-hook' in `org-mode' and
169 restart `org-mode' if necessary."
171 (ad-activate 'org-schedule
)
172 (ad-activate 'org-time-stamp
)
173 (ad-activate 'org-deadline
)
174 (add-hook 'org-insert-heading-hook
'org-expiry-insert-created
)
175 (add-hook 'org-after-todo-state-change-hook
'org-expiry-insert-created
)
176 (add-hook 'org-after-tags-change-hook
'org-expiry-insert-created
)
178 (add-hook 'org-mode-hook
179 (lambda() (add-hook 'before-save-hook
180 'org-expiry-process-entries t t
)))
181 ;; need this to refresh org-mode hooks
185 (message "Org-expiry insinuated, `org-mode' restarted.")))))
187 (defun org-expiry-deinsinuate (&optional arg
)
188 "Remove hooks and deactivate advices for org-expiry.
189 If ARG, also remove org-expiry hook in Org's `before-save-hook'
190 and restart `org-mode' if necessary."
192 (ad-deactivate 'org-schedule
)
193 (ad-deactivate 'org-time-stamp
)
194 (ad-deactivate 'org-deadline
)
195 (remove-hook 'org-insert-heading-hook
'org-expiry-insert-created
)
196 (remove-hook 'org-after-todo-state-change-hook
'org-expiry-insert-created
)
197 (remove-hook 'org-after-tags-change-hook
'org-expiry-insert-created
)
198 (remove-hook 'org-mode-hook
199 (lambda() (add-hook 'before-save-hook
200 'org-expiry-process-entries t t
)))
202 ;; need this to refresh org-mode hooks
206 (message "Org-expiry de-insinuated, `org-mode' restarted.")))))
208 ;;; org-expiry-expired-p:
210 (defun org-expiry-expired-p ()
211 "Check if the entry at point is expired.
212 Return nil if the entry is not expired. Otherwise return the
213 amount of time between today and the expiry date.
215 If there is no creation date, use `org-expiry-created-date'.
216 If there is no expiry date, use `org-expiry-expiry-date'."
217 (let* ((ex-prop org-expiry-expiry-property-name
)
218 (cr-prop org-expiry-created-property-name
)
220 (cr (org-read-date nil t
(or (org-entry-get (point) cr-prop t
) "+0d")))
221 (ex-field (or (org-entry-get (point) ex-prop t
) org-expiry-wait
))
222 (ex (if (string-match "^[ \t]?[+-]" ex-field
)
223 (time-add cr
(time-subtract (org-read-date nil t ex-field
) ct
))
224 (org-read-date nil t ex-field
))))
225 (if (time-less-p ex ct
)
226 (time-subtract ct ex
))))
228 ;;; Expire an entry or a region/buffer:
230 (defun org-expiry-process-entry (&optional force
)
231 "Call `org-expiry-handler-function' on entry.
232 If FORCE is non-nil, don't require confirmation from the user.
233 Otherwise rely on `org-expiry-confirm-flag' to decide."
236 (when (interactive-p) (org-reveal))
237 (when (org-expiry-expired-p)
238 (org-back-to-heading)
239 (looking-at org-complex-heading-regexp
)
240 (let* ((ov (org-make-overlay (point) (match-end 0)))
241 (e (org-expiry-expired-p))
242 (d (time-to-number-of-days e
)))
243 (org-overlay-put ov
'face
'secondary-selection
)
245 (null org-expiry-confirm-flag
)
246 (and (eq org-expiry-confirm-flag
'interactive
)
248 (and org-expiry-confirm-flag
249 (y-or-n-p (format "Entry expired by %d days. Process? " d
))))
250 (funcall 'org-expiry-handler-function
))
251 (org-delete-overlay ov
)))))
253 (defun org-expiry-process-entries (beg end
)
254 "Process all expired entries between BEG and END.
255 The expiry process will run the function defined by
256 `org-expiry-handler-functions'."
259 (let ((beg (if (org-region-active-p)
260 (region-beginning) (point-min)))
261 (end (if (org-region-active-p)
262 (region-end) (point-max))))
264 (let ((expired 0) (processed 0))
265 (while (and (outline-next-heading) (< (point) end
))
266 (when (org-expiry-expired-p)
267 (setq expired
(1+ expired
))
268 (if (if (interactive-p)
269 (call-interactively 'org-expiry-process-entry
)
270 (org-expiry-process-entry))
271 (setq processed
(1+ processed
)))))
272 (if (equal expired
0)
273 (message "No expired entry")
274 (message "Processed %d on %d expired entries"
275 processed expired
))))))
277 ;;; Insert created/expiry property:
279 (defun org-expiry-insert-created (&optional arg
)
280 "Insert or update a property with the creation date.
281 If ARG, always update it. With one `C-u' prefix, silently update
282 to today's date. With two `C-u' prefixes, prompt the user for to
285 (let* ((d (org-entry-get (point) org-expiry-created-property-name
))
287 (when (or (null d
) arg
)
288 ;; update if no date or non-nil prefix argument
289 ;; FIXME Use `org-time-string-to-time'
290 (setq d-time
(if d
(apply 'encode-time
(org-parse-time-string d
))
292 (setq d-hour
(format-time-string "%H:%M" d-time
))
295 (point) org-expiry-created-property-name
296 ;; two C-u prefixes will call org-read-date
297 (if (equal arg
'(16))
298 (concat "<" (org-read-date
299 nil nil nil nil d-time d-hour
) ">")
300 (format-time-string (cdr org-time-stamp-formats
))))))))
302 (defun org-expiry-insert-expiry (&optional today
)
303 "Insert a property with the expiry date.
304 With one `C-u' prefix, don't prompt interactively for the date
305 and insert today's date."
307 (let* ((d (org-entry-get (point) org-expiry-expiry-property-name
))
309 (setq d-time
(if d
(apply 'encode-time
(org-parse-time-string d
))
311 (setq d-hour
(format-time-string "%H:%M" d-time
))
314 (point) org-expiry-expiry-property-name
315 (if today
(format-time-string (cdr org-time-stamp-formats
))
316 (concat "<" (org-read-date
317 nil nil nil nil d-time d-hour
) ">"))))))
319 ;;; Functions to process expired entries:
321 (defun org-expiry-archive-subtree ()
322 "Archive the entry at point if it is expired."
325 (if (org-expiry-expired-p)
326 (org-archive-subtree)
328 (message "Entry at point is not expired.")))))
330 (defun org-expiry-add-keyword (&optional keyword
)
331 "Add KEYWORD to the entry at point if it is expired."
332 (interactive "sKeyword: ")
333 (if (or (member keyword org-todo-keywords-1
)
334 (setq keyword org-expiry-keyword
))
336 (if (org-expiry-expired-p)
339 (message "Entry at point is not expired."))))
340 (error "\"%s\" is not a to-do keyword in this buffer" keyword
)))
342 ;; FIXME what about using org-refile ?
344 (provide 'org-expiry
)
346 ;;; org-expiry.el ends here