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