Fix aa86e4bc again
[org-mode.git] / contrib / lisp / org-expiry.el
blob1506c3bc07e3e9fddf1a8709a17d0c08d55fa02b
1 ;;; org-expiry.el --- expiry mechanism for Org entries
2 ;;
3 ;; Copyright 2007-2014 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Bastien Guerry
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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This gives you a chance to get rid of old entries in your Org files
27 ;; by expiring them.
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:
38 ;; * Stop watching TV
39 ;; :PROPERTIES:
40 ;; :CREATED: <2008-01-07 lun 08:01>
41 ;; :EXPIRY: <2008-01-09 08:01>
42 ;; :END:
44 ;; This entry will expire on the 9th, january 2008.
46 ;; * Stop watching TV
47 ;; :PROPERTIES:
48 ;; :CREATED: <2008-01-07 lun 08:01>
49 ;; :EXPIRY: +1w
50 ;; :END:
52 ;; This entry will expire on the 14th, january 2008, one week after its
53 ;; creation date.
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
62 ;; the subtree.
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
75 ;;; Code:
77 ;;; User variables:
79 (defgroup org-expiry nil
80 "Org expiry process."
81 :tag "Org Expiry"
82 :group 'org)
84 (defcustom org-expiry-inactive-timestamps nil
85 "Insert inactive timestamps for created/expired properties."
86 :type 'boolean
87 :group 'org-expiry)
89 (defcustom org-expiry-created-property-name "CREATED"
90 "The name of the property for setting the creation date."
91 :type 'string
92 :group 'org-expiry)
94 (defcustom org-expiry-expiry-property-name "EXPIRY"
95 "The name of the property for setting the expiry date/delay."
96 :type 'string
97 :group 'org-expiry)
99 (defcustom org-expiry-keyword "EXPIRED"
100 "The default keyword for `org-expiry-add-keyword'."
101 :type 'string
102 :group 'org-expiry)
104 (defcustom org-expiry-wait "+1y"
105 "Time span between the creation date and the expiry.
106 The default value for this variable (\"+1y\") means that entries
107 will expire if there are at least one year old.
109 If the expiry delay cannot be retrieved from the entry or the
110 subtree above, the expiry process compares the expiry delay with
111 `org-expiry-wait'. This can be either an ISO date or a relative
112 time specification. See `org-read-date' for details."
113 :type 'string
114 :group 'org-expiry)
116 (defcustom org-expiry-created-date "+0d"
117 "The default creation date.
118 The default value of this variable (\"+0d\") means that entries
119 without a creation date will be handled as if they were created
120 today.
122 If the creation date cannot be retrieved from the entry or the
123 subtree above, the expiry process will compare the expiry delay
124 with this date. This can be either an ISO date or a relative
125 time specification. See `org-read-date' for details on relative
126 time specifications."
127 :type 'string
128 :group 'org-expiry)
130 (defcustom org-expiry-handler-function 'org-toggle-archive-tag
131 "Function to process expired entries.
132 Possible candidates for this function are:
134 `org-toggle-archive-tag'
135 `org-expiry-add-keyword'
136 `org-expiry-archive-subtree'"
137 :type 'function
138 :group 'org-expiry)
140 (defcustom org-expiry-confirm-flag t
141 "Non-nil means confirm expiration process."
142 :type '(choice
143 (const :tag "Always require confirmation" t)
144 (const :tag "Do not require confirmation" nil)
145 (const :tag "Require confirmation in interactive expiry process"
146 interactive))
147 :group 'org-expiry)
149 (defcustom org-expiry-advised-functions
150 '(org-scheduled org-deadline org-time-stamp)
151 "A list of advised functions.
152 `org-expiry-insinuate' will activate the expiry advice for these
153 functions. `org-expiry-deinsinuate' will deactivate them."
154 :type 'boolean
155 :group 'list)
157 ;;; Advices and insinuation:
159 (defadvice org-schedule (after org-schedule-update-created)
160 "Update the creation-date property when calling `org-schedule'."
161 (org-expiry-insert-created))
163 (defadvice org-deadline (after org-deadline-update-created)
164 "Update the creation-date property when calling `org-deadline'."
165 (org-expiry-insert-created))
167 (defadvice org-time-stamp (after org-time-stamp-update-created)
168 "Update the creation-date property when calling `org-time-stamp'."
169 (org-expiry-insert-created))
171 (defun org-expiry-insinuate (&optional arg)
172 "Add hooks and activate advices for org-expiry.
173 If ARG, also add a hook to `before-save-hook' in `org-mode' and
174 restart `org-mode' if necessary."
175 (interactive "P")
176 (ad-activate 'org-schedule)
177 (ad-activate 'org-time-stamp)
178 (ad-activate 'org-deadline)
179 (add-hook 'org-insert-heading-hook 'org-expiry-insert-created)
180 (add-hook 'org-after-todo-state-change-hook 'org-expiry-insert-created)
181 (add-hook 'org-after-tags-change-hook 'org-expiry-insert-created)
182 (when arg
183 (add-hook 'org-mode-hook
184 (lambda() (add-hook 'before-save-hook
185 'org-expiry-process-entries t t)))
186 ;; need this to refresh org-mode hooks
187 (when (eq major-mode 'org-mode)
188 (org-mode)
189 (if (org-called-interactively-p)
190 (message "Org-expiry insinuated, `org-mode' restarted.")))))
192 (defun org-expiry-deinsinuate (&optional arg)
193 "Remove hooks and deactivate advices for org-expiry.
194 If ARG, also remove org-expiry hook in Org's `before-save-hook'
195 and restart `org-mode' if necessary."
196 (interactive "P")
197 (ad-deactivate 'org-schedule)
198 (ad-deactivate 'org-time-stamp)
199 (ad-deactivate 'org-deadline)
200 (remove-hook 'org-insert-heading-hook 'org-expiry-insert-created)
201 (remove-hook 'org-after-todo-state-change-hook 'org-expiry-insert-created)
202 (remove-hook 'org-after-tags-change-hook 'org-expiry-insert-created)
203 (remove-hook 'org-mode-hook
204 (lambda() (add-hook 'before-save-hook
205 'org-expiry-process-entries t t)))
206 (when arg
207 ;; need this to refresh org-mode hooks
208 (when (eq major-mode 'org-mode)
209 (org-mode)
210 (if (org-called-interactively-p)
211 (message "Org-expiry de-insinuated, `org-mode' restarted.")))))
213 ;;; org-expiry-expired-p:
215 (defun org-expiry-expired-p ()
216 "Check if the entry at point is expired.
217 Return nil if the entry is not expired. Otherwise return the
218 amount of time between today and the expiry date.
220 If there is no creation date, use `org-expiry-created-date'.
221 If there is no expiry date, use `org-expiry-wait'."
222 (let* ((ex-prop org-expiry-expiry-property-name)
223 (cr-prop org-expiry-created-property-name)
224 (ct (current-time))
225 (cr (org-read-date nil t (or (org-entry-get (point) cr-prop t)
226 org-expiry-created-date)))
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