(org-attach-store-link-p): Use `t' instead of 'origin for backward compatibility.
[org-mode/org-mode-NeilSmithlineMods.git] / contrib / lisp / org-expiry.el
blob4a49399417387547400e6bf4b8bfeb31648f5b6e
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.
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.
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?
35 ;;
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:
43 ;;
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.
58 ;;
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.
67 ;;
68 ;; When you're in such a session, you can insinuate org-expiry like
69 ;; this: M-x org-expiry-insinuate
70 ;;
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-created-property-name "CREATED"
85 "The name of the property for setting the creation date."
86 :type 'string
87 :group 'org-expiry)
89 (defcustom org-expiry-expiry-property-name "EXPIRY"
90 "The name of the property for setting the expiry date/delay."
91 :type 'string
92 :group 'org-expiry)
94 (defcustom org-expiry-keyword "EXPIRED"
95 "The default keyword for `org-expiry-add-keyword'."
96 :type 'string
97 :group 'org-expiry)
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."
108 :type 'string
109 :group 'org-expiry)
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
115 today.
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."
122 :type 'string
123 :group 'org-expiry)
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'"
132 :type 'function
133 :group 'org-expiry)
135 (defcustom org-expiry-confirm-flag t
136 "Non-nil means confirm expiration process."
137 :type '(choice
138 (const :tag "Always require confirmation" t)
139 (const :tag "Do not require confirmation" nil)
140 (const :tag "Require confirmation in interactive expiry process"
141 interactive))
142 :group 'org-expiry)
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."
149 :type 'boolean
150 :group 'list)
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."
170 (interactive "P")
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)
177 (when arg
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
182 (when (org-mode-p)
183 (org-mode)
184 (if (interactive-p)
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."
191 (interactive "P")
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)))
201 (when arg
202 ;; need this to refresh org-mode hooks
203 (when (org-mode-p)
204 (org-mode)
205 (if (interactive-p)
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)
219 (ct (current-time))
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."
234 (interactive "P")
235 (save-excursion
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 (make-overlay (point) (match-end 0)))
241 (e (org-expiry-expired-p))
242 (d (time-to-number-of-days e)))
243 (overlay-put ov 'face 'secondary-selection)
244 (if (or force
245 (null org-expiry-confirm-flag)
246 (and (eq org-expiry-confirm-flag 'interactive)
247 (not (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 (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'."
257 (interactive "r")
258 (save-excursion
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))))
263 (goto-char beg)
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
283 update the date."
284 (interactive "P")
285 (let* ((d (org-entry-get (point) org-expiry-created-property-name))
286 d-time d-hour)
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))
291 (current-time)))
292 (setq d-hour (format-time-string "%H:%M" d-time))
293 (save-excursion
294 (org-entry-put
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."
306 (interactive "P")
307 (let* ((d (org-entry-get (point) org-expiry-expiry-property-name))
308 d-time d-hour)
309 (setq d-time (if d (apply 'encode-time (org-parse-time-string d))
310 (current-time)))
311 (setq d-hour (format-time-string "%H:%M" d-time))
312 (save-excursion
313 (org-entry-put
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."
323 (interactive)
324 (save-excursion
325 (if (org-expiry-expired-p)
326 (org-archive-subtree)
327 (if (interactive-p)
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))
335 (save-excursion
336 (if (org-expiry-expired-p)
337 (org-todo keyword)
338 (if (interactive-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