org.el (org-read-date-minibuffer-local-map): Check if we are at the beginning of...
[org-mode.git] / contrib / lisp / org-expiry.el
blob363bebed87911c0d5cb8adcffc41c931db7a75b2
1 ;;; org-expiry.el --- expiry mechanism for Org entries
2 ;;
3 ;; Copyright 2007-2013 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-expiry-date'."
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) "+0d")))
226 (ex-field (or (org-entry-get (point) ex-prop t) org-expiry-wait))
227 (ex (if (string-match "^[ \t]?[+-]" ex-field)
228 (time-add cr (time-subtract (org-read-date nil t ex-field) ct))
229 (org-read-date nil t ex-field))))
230 (if (time-less-p ex ct)
231 (time-subtract ct ex))))
233 ;;; Expire an entry or a region/buffer:
235 (defun org-expiry-process-entry (&optional force)
236 "Call `org-expiry-handler-function' on entry.
237 If FORCE is non-nil, don't require confirmation from the user.
238 Otherwise rely on `org-expiry-confirm-flag' to decide."
239 (interactive "P")
240 (save-excursion
241 (when (org-called-interactively-p) (org-reveal))
242 (when (org-expiry-expired-p)
243 (org-back-to-heading)
244 (looking-at org-complex-heading-regexp)
245 (let* ((ov (make-overlay (point) (match-end 0)))
246 (e (org-expiry-expired-p))
247 (d (time-to-number-of-days e)))
248 (overlay-put ov 'face 'secondary-selection)
249 (if (or force
250 (null org-expiry-confirm-flag)
251 (and (eq org-expiry-confirm-flag 'interactive)
252 (not (interactive)))
253 (and org-expiry-confirm-flag
254 (y-or-n-p (format "Entry expired by %d days. Process? " d))))
255 (funcall 'org-expiry-handler-function))
256 (delete-overlay ov)))))
258 (defun org-expiry-process-entries (beg end)
259 "Process all expired entries between BEG and END.
260 The expiry process will run the function defined by
261 `org-expiry-handler-functions'."
262 (interactive "r")
263 (save-excursion
264 (let ((beg (if (org-region-active-p)
265 (region-beginning) (point-min)))
266 (end (if (org-region-active-p)
267 (region-end) (point-max))))
268 (goto-char beg)
269 (let ((expired 0) (processed 0))
270 (while (and (outline-next-heading) (< (point) end))
271 (when (org-expiry-expired-p)
272 (setq expired (1+ expired))
273 (if (if (org-called-interactively-p)
274 (call-interactively 'org-expiry-process-entry)
275 (org-expiry-process-entry))
276 (setq processed (1+ processed)))))
277 (if (equal expired 0)
278 (message "No expired entry")
279 (message "Processed %d on %d expired entries"
280 processed expired))))))
282 ;;; Insert created/expiry property:
284 (defun org-expiry-insert-created (&optional arg)
285 "Insert or update a property with the creation date.
286 If ARG, always update it. With one `C-u' prefix, silently update
287 to today's date. With two `C-u' prefixes, prompt the user for to
288 update the date."
289 (interactive "P")
290 (let* ((d (org-entry-get (point) org-expiry-created-property-name))
291 d-time d-hour timestr)
292 (when (or (null d) arg)
293 ;; update if no date or non-nil prefix argument
294 ;; FIXME Use `org-time-string-to-time'
295 (setq d-time (if d (org-time-string-to-time d)
296 (current-time)))
297 (setq d-hour (format-time-string "%H:%M" d-time))
298 (setq timestr
299 ;; two C-u prefixes will call org-read-date
300 (if (equal arg '(16))
301 (concat "<" (org-read-date
302 nil nil nil nil d-time d-hour) ">")
303 (format-time-string (cdr org-time-stamp-formats))))
304 ;; maybe transform to inactive timestamp
305 (if org-expiry-inactive-timestamps
306 (setq timestr (concat "[" (substring timestr 1 -1) "]")))
307 (save-excursion
308 (org-entry-put
309 (point) org-expiry-created-property-name timestr)))))
311 (defun org-expiry-insert-expiry (&optional today)
312 "Insert a property with the expiry date.
313 With one `C-u' prefix, don't prompt interactively for the date
314 and insert today's date."
315 (interactive "P")
316 (let* ((d (org-entry-get (point) org-expiry-expiry-property-name))
317 d-time d-hour)
318 (setq d-time (if d (org-time-string-to-time d)
319 (current-time)))
320 (setq d-hour (format-time-string "%H:%M" d-time))
321 (setq timestr (if today
322 (format-time-string (cdr org-time-stamp-formats))
323 (concat "<" (org-read-date
324 nil nil nil nil d-time d-hour) ">")))
325 ;; maybe transform to inactive timestamp
326 (if org-expiry-inactive-timestamps
327 (setq timestr (concat "[" (substring timestr 1 -1) "]")))
329 (save-excursion
330 (org-entry-put
331 (point) org-expiry-expiry-property-name timestr))))
333 ;;; Functions to process expired entries:
335 (defun org-expiry-archive-subtree ()
336 "Archive the entry at point if it is expired."
337 (interactive)
338 (save-excursion
339 (if (org-expiry-expired-p)
340 (org-archive-subtree)
341 (if (org-called-interactively-p)
342 (message "Entry at point is not expired.")))))
344 (defun org-expiry-add-keyword (&optional keyword)
345 "Add KEYWORD to the entry at point if it is expired."
346 (interactive "sKeyword: ")
347 (if (or (member keyword org-todo-keywords-1)
348 (setq keyword org-expiry-keyword))
349 (save-excursion
350 (if (org-expiry-expired-p)
351 (org-todo keyword)
352 (if (org-called-interactively-p)
353 (message "Entry at point is not expired."))))
354 (error "\"%s\" is not a to-do keyword in this buffer" keyword)))
356 ;; FIXME what about using org-refile ?
358 (provide 'org-expiry)
360 ;;; org-expiry.el ends here