Cleanup intervals.
[emacs.git] / lisp / org / org-archive.el
blobdb3b8250bc01eae18da2acd9c3a78f03ba4d08ed
1 ;;; org-archive.el --- Archiving for Org-mode
3 ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs 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 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs 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/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file contains the face definitions for Org.
29 ;;; Code:
31 (require 'org)
33 (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
35 (defcustom org-archive-default-command 'org-archive-subtree
36 "The default archiving command."
37 :group 'org-archive
38 :type '(choice
39 (const org-archive-subtree)
40 (const org-archive-to-archive-sibling)
41 (const org-archive-set-tag)))
43 (defcustom org-archive-reversed-order nil
44 "Non-nil means make the tree first child under the archive heading, not last."
45 :group 'org-archive
46 :version "24.1"
47 :type 'boolean)
49 (defcustom org-archive-sibling-heading "Archive"
50 "Name of the local archive sibling that is used to archive entries locally.
51 Locally means: in the tree, under a sibling.
52 See `org-archive-to-archive-sibling' for more information."
53 :group 'org-archive
54 :type 'string)
56 (defcustom org-archive-mark-done nil
57 "Non-nil means mark entries as DONE when they are moved to the archive file.
58 This can be a string to set the keyword to use. When t, Org-mode will
59 use the first keyword in its list that means done."
60 :group 'org-archive
61 :type '(choice
62 (const :tag "No" nil)
63 (const :tag "Yes" t)
64 (string :tag "Use this keyword")))
66 (defcustom org-archive-stamp-time t
67 "Non-nil means add a time stamp to entries moved to an archive file.
68 This variable is obsolete and has no effect anymore, instead add or remove
69 `time' from the variable `org-archive-save-context-info'."
70 :group 'org-archive
71 :type 'boolean)
73 (defcustom org-archive-subtree-add-inherited-tags 'infile
74 "Non-nil means append inherited tags when archiving a subtree."
75 :group 'org-archive
76 :version "24.1"
77 :type '(choice
78 (const :tag "Never" nil)
79 (const :tag "When archiving a subtree to the same file" infile)
80 (const :tag "Always" t)))
82 (defcustom org-archive-save-context-info '(time file olpath category todo itags)
83 "Parts of context info that should be stored as properties when archiving.
84 When a subtree is moved to an archive file, it loses information given by
85 context, like inherited tags, the category, and possibly also the TODO
86 state (depending on the variable `org-archive-mark-done').
87 This variable can be a list of any of the following symbols:
89 time The time of archiving.
90 file The file where the entry originates.
91 ltags The local tags, in the headline of the subtree.
92 itags The tags the subtree inherits from further up the hierarchy.
93 todo The pre-archive TODO state.
94 category The category, taken from file name or #+CATEGORY lines.
95 olpath The outline path to the item. These are all headlines above
96 the current item, separated by /, like a file path.
98 For each symbol present in the list, a property will be created in
99 the archived entry, with a prefix \"ARCHIVE_\", to remember this
100 information."
101 :group 'org-archive
102 :type '(set :greedy t
103 (const :tag "Time" time)
104 (const :tag "File" file)
105 (const :tag "Category" category)
106 (const :tag "TODO state" todo)
107 (const :tag "Priority" priority)
108 (const :tag "Inherited tags" itags)
109 (const :tag "Outline path" olpath)
110 (const :tag "Local tags" ltags)))
112 (defun org-get-local-archive-location ()
113 "Get the archive location applicable at point."
114 (let ((re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
115 prop)
116 (save-excursion
117 (save-restriction
118 (widen)
119 (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
120 (cond
121 ((and prop (string-match "\\S-" prop))
122 prop)
123 ((or (re-search-backward re nil t)
124 (re-search-forward re nil t))
125 (match-string 1))
126 (t org-archive-location))))))
128 (defun org-add-archive-files (files)
129 "Splice the archive files into the list of files.
130 This implies visiting all these files and finding out what the
131 archive file is."
132 (org-uniquify
133 (apply
134 'append
135 (mapcar
136 (lambda (f)
137 (if (not (file-exists-p f))
139 (with-current-buffer (org-get-agenda-file-buffer f)
140 (cons f (org-all-archive-files)))))
141 files))))
143 (defun org-all-archive-files ()
144 "Get a list of all archive files used in the current buffer."
145 (let (file files)
146 (save-excursion
147 (save-restriction
148 (goto-char (point-min))
149 (while (re-search-forward
150 "^\\(#\\+\\|[ \t]*:\\)ARCHIVE:[ \t]+\\(.*\\)"
151 nil t)
152 (setq file (org-extract-archive-file
153 (org-match-string-no-properties 2)))
154 (and file (> (length file) 0) (file-exists-p file)
155 (add-to-list 'files file)))))
156 (setq files (nreverse files))
157 (setq file (org-extract-archive-file))
158 (and file (> (length file) 0) (file-exists-p file)
159 (add-to-list 'files file))
160 files))
162 (defun org-extract-archive-file (&optional location)
163 "Extract and expand the file name from archive LOCATION.
164 if LOCATION is not given, the value of `org-archive-location' is used."
165 (setq location (or location org-archive-location))
166 (if (string-match "\\(.*\\)::\\(.*\\)" location)
167 (if (= (match-beginning 1) (match-end 1))
168 (buffer-file-name (buffer-base-buffer))
169 (expand-file-name
170 (format (match-string 1 location)
171 (file-name-nondirectory
172 (buffer-file-name (buffer-base-buffer))))))))
174 (defun org-extract-archive-heading (&optional location)
175 "Extract the heading from archive LOCATION.
176 if LOCATION is not given, the value of `org-archive-location' is used."
177 (setq location (or location org-archive-location))
178 (if (string-match "\\(.*\\)::\\(.*\\)" location)
179 (format (match-string 2 location)
180 (file-name-nondirectory
181 (buffer-file-name (buffer-base-buffer))))))
183 (defun org-archive-subtree (&optional find-done)
184 "Move the current subtree to the archive.
185 The archive can be a certain top-level heading in the current file, or in
186 a different file. The tree will be moved to that location, the subtree
187 heading be marked DONE, and the current time will be added.
189 When called with prefix argument FIND-DONE, find whole trees without any
190 open TODO items and archive them (after getting confirmation from the user).
191 If the cursor is not at a headline when this command is called, try all level
192 1 trees. If the cursor is on a headline, only try the direct children of
193 this heading."
194 (interactive "P")
195 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
196 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
197 'region-start-level 'region))
198 org-loop-over-headlines-in-active-region)
199 (org-map-entries
200 `(progn (setq org-map-continue-from (progn (org-back-to-heading) (point)))
201 (org-archive-subtree ,find-done))
202 org-loop-over-headlines-in-active-region
203 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
204 (if find-done
205 (org-archive-all-done)
206 ;; Save all relevant TODO keyword-relatex variables
207 (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
208 (tr-org-todo-keywords-1 org-todo-keywords-1)
209 (tr-org-todo-kwd-alist org-todo-kwd-alist)
210 (tr-org-done-keywords org-done-keywords)
211 (tr-org-todo-regexp org-todo-regexp)
212 (tr-org-todo-line-regexp org-todo-line-regexp)
213 (tr-org-odd-levels-only org-odd-levels-only)
214 (this-buffer (current-buffer))
215 ;; start of variables that will be used for saving context
216 ;; The compiler complains about them - keep them anyway!
217 (file (abbreviate-file-name
218 (or (buffer-file-name (buffer-base-buffer))
219 (error "No file associated to buffer"))))
220 (olpath (mapconcat 'identity (org-get-outline-path) "/"))
221 (time (format-time-string
222 (substring (cdr org-time-stamp-formats) 1 -1)
223 (current-time)))
224 category todo priority ltags itags atags
225 ;; end of variables that will be used for saving context
226 location afile heading buffer level newfile-p infile-p visiting)
228 ;; Find the local archive location
229 (setq location (org-get-local-archive-location)
230 afile (org-extract-archive-file location)
231 heading (org-extract-archive-heading location)
232 infile-p (equal file (abbreviate-file-name afile)))
233 (unless afile
234 (error "Invalid `org-archive-location'"))
236 (if (> (length afile) 0)
237 (setq newfile-p (not (file-exists-p afile))
238 visiting (find-buffer-visiting afile)
239 buffer (or visiting (find-file-noselect afile)))
240 (setq buffer (current-buffer)))
241 (unless buffer
242 (error "Cannot access file \"%s\"" afile))
243 (if (and (> (length heading) 0)
244 (string-match "^\\*+" heading))
245 (setq level (match-end 0))
246 (setq heading nil level 0))
247 (save-excursion
248 (org-back-to-heading t)
249 ;; Get context information that will be lost by moving the tree
250 (setq category (org-get-category nil 'force-refresh)
251 todo (and (looking-at org-todo-line-regexp)
252 (match-string 2))
253 priority (org-get-priority
254 (if (match-end 3) (match-string 3) ""))
255 ltags (org-get-tags)
256 itags (org-delete-all ltags (org-get-tags-at))
257 atags (org-get-tags-at))
258 (setq ltags (mapconcat 'identity ltags " ")
259 itags (mapconcat 'identity itags " "))
260 ;; We first only copy, in case something goes wrong
261 ;; we need to protect `this-command', to avoid kill-region sets it,
262 ;; which would lead to duplication of subtrees
263 (let (this-command) (org-copy-subtree 1 nil t))
264 (set-buffer buffer)
265 ;; Enforce org-mode for the archive buffer
266 (if (not (eq major-mode 'org-mode))
267 ;; Force the mode for future visits.
268 (let ((org-insert-mode-line-in-empty-file t)
269 (org-inhibit-startup t))
270 (call-interactively 'org-mode)))
271 (when newfile-p
272 (goto-char (point-max))
273 (insert (format "\nArchived entries from file %s\n\n"
274 (buffer-file-name this-buffer))))
275 ;; Force the TODO keywords of the original buffer
276 (let ((org-todo-line-regexp tr-org-todo-line-regexp)
277 (org-todo-keywords-1 tr-org-todo-keywords-1)
278 (org-todo-kwd-alist tr-org-todo-kwd-alist)
279 (org-done-keywords tr-org-done-keywords)
280 (org-todo-regexp tr-org-todo-regexp)
281 (org-todo-line-regexp tr-org-todo-line-regexp)
282 (org-odd-levels-only
283 (if (local-variable-p 'org-odd-levels-only (current-buffer))
284 org-odd-levels-only
285 tr-org-odd-levels-only)))
286 (goto-char (point-min))
287 (show-all)
288 (if heading
289 (progn
290 (if (re-search-forward
291 (concat "^" (regexp-quote heading)
292 (org-re "[ \t]*\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\($\\|\r\\)"))
293 nil t)
294 (goto-char (match-end 0))
295 ;; Heading not found, just insert it at the end
296 (goto-char (point-max))
297 (or (bolp) (insert "\n"))
298 (insert "\n" heading "\n")
299 (end-of-line 0))
300 ;; Make the subtree visible
301 (show-subtree)
302 (if org-archive-reversed-order
303 (progn
304 (org-back-to-heading t)
305 (outline-next-heading))
306 (org-end-of-subtree t))
307 (skip-chars-backward " \t\r\n")
308 (and (looking-at "[ \t\r\n]*")
309 (replace-match "\n\n")))
310 ;; No specific heading, just go to end of file.
311 (goto-char (point-max)) (insert "\n"))
312 ;; Paste
313 (org-paste-subtree (org-get-valid-level level (and heading 1)))
314 ;; Shall we append inherited tags?
315 (and itags
316 (or (and (eq org-archive-subtree-add-inherited-tags 'infile)
317 infile-p)
318 (eq org-archive-subtree-add-inherited-tags t))
319 (org-set-tags-to atags))
320 ;; Mark the entry as done
321 (when (and org-archive-mark-done
322 (looking-at org-todo-line-regexp)
323 (or (not (match-end 2))
324 (not (member (match-string 2) org-done-keywords))))
325 (let (org-log-done org-todo-log-states)
326 (org-todo
327 (car (or (member org-archive-mark-done org-done-keywords)
328 org-done-keywords)))))
330 ;; Add the context info
331 (when org-archive-save-context-info
332 (let ((l org-archive-save-context-info) e n v)
333 (while (setq e (pop l))
334 (when (and (setq v (symbol-value e))
335 (stringp v) (string-match "\\S-" v))
336 (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
337 (org-entry-put (point) n v)))))
339 ;; Save and kill the buffer, if it is not the same buffer.
340 (when (not (eq this-buffer buffer))
341 (save-buffer))))
342 ;; Here we are back in the original buffer. Everything seems to have
343 ;; worked. So now cut the tree and finish up.
344 (let (this-command) (org-cut-subtree))
345 (when (featurep 'org-inlinetask)
346 (org-inlinetask-remove-END-maybe))
347 (setq org-markers-to-move nil)
348 (message "Subtree archived %s"
349 (if (eq this-buffer buffer)
350 (concat "under heading: " heading)
351 (concat "in file: " (abbreviate-file-name afile))))))
352 (org-reveal)
353 (if (looking-at "^[ \t]*$")
354 (outline-next-visible-heading 1))))
356 (defun org-archive-to-archive-sibling ()
357 "Archive the current heading by moving it under the archive sibling.
358 The archive sibling is a sibling of the heading with the heading name
359 `org-archive-sibling-heading' and an `org-archive-tag' tag. If this
360 sibling does not exist, it will be created at the end of the subtree."
361 (interactive)
362 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
363 (let ((cl (when (eq org-loop-over-headlines-in-active-region 'start-level)
364 'region-start-level 'region))
365 org-loop-over-headlines-in-active-region)
366 (org-map-entries
367 '(progn (setq org-map-continue-from
368 (progn (org-back-to-heading)
369 (if (looking-at (concat "^.*:" org-archive-tag ":.*$"))
370 (org-end-of-subtree t)
371 (point))))
372 (when (org-at-heading-p)
373 (org-archive-to-archive-sibling)))
374 org-loop-over-headlines-in-active-region
375 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
376 (save-restriction
377 (widen)
378 (let (b e pos leader level)
379 (org-back-to-heading t)
380 (looking-at org-outline-regexp)
381 (setq leader (match-string 0)
382 level (funcall outline-level))
383 (setq pos (point))
384 (condition-case nil
385 (outline-up-heading 1 t)
386 (error (setq e (point-max)) (goto-char (point-min))))
387 (setq b (point))
388 (unless e
389 (condition-case nil
390 (org-end-of-subtree t t)
391 (error (goto-char (point-max))))
392 (setq e (point)))
393 (goto-char b)
394 (unless (re-search-forward
395 (concat "^" (regexp-quote leader)
396 "[ \t]*"
397 org-archive-sibling-heading
398 "[ \t]*:"
399 org-archive-tag ":") e t)
400 (goto-char e)
401 (or (bolp) (newline))
402 (insert leader org-archive-sibling-heading "\n")
403 (beginning-of-line 0)
404 (org-toggle-tag org-archive-tag 'on))
405 (beginning-of-line 1)
406 (if org-archive-reversed-order
407 (outline-next-heading)
408 (org-end-of-subtree t t))
409 (save-excursion
410 (goto-char pos)
411 (let ((this-command this-command)) (org-cut-subtree)))
412 (org-paste-subtree (org-get-valid-level level 1))
413 (org-set-property
414 "ARCHIVE_TIME"
415 (format-time-string
416 (substring (cdr org-time-stamp-formats) 1 -1)
417 (current-time)))
418 (outline-up-heading 1 t)
419 (hide-subtree)
420 (org-cycle-show-empty-lines 'folded)
421 (goto-char pos)))
422 (org-reveal)
423 (if (looking-at "^[ \t]*$")
424 (outline-next-visible-heading 1))))
426 (defun org-archive-all-done (&optional tag)
427 "Archive sublevels of the current tree without open TODO items.
428 If the cursor is not on a headline, try all level 1 trees. If
429 it is on a headline, try all direct children.
430 When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
431 (let ((re org-not-done-heading-regexp) re1
432 (rea (concat ".*:" org-archive-tag ":"))
433 (begm (make-marker))
434 (endm (make-marker))
435 (question (if tag "Set ARCHIVE tag (no open TODO items)? "
436 "Move subtree to archive (no open TODO items)? "))
437 beg end (cntarch 0))
438 (if (org-at-heading-p)
439 (progn
440 (setq re1 (concat "^" (regexp-quote
441 (make-string
442 (+ (- (match-end 0) (match-beginning 0) 1)
443 (if org-odd-levels-only 2 1))
444 ?*))
445 " "))
446 (move-marker begm (point))
447 (move-marker endm (org-end-of-subtree t)))
448 (setq re1 "^* ")
449 (move-marker begm (point-min))
450 (move-marker endm (point-max)))
451 (save-excursion
452 (goto-char begm)
453 (while (re-search-forward re1 endm t)
454 (setq beg (match-beginning 0)
455 end (save-excursion (org-end-of-subtree t) (point)))
456 (goto-char beg)
457 (if (re-search-forward re end t)
458 (goto-char end)
459 (goto-char beg)
460 (if (and (or (not tag) (not (looking-at rea)))
461 (y-or-n-p question))
462 (progn
463 (if tag
464 (org-toggle-tag org-archive-tag 'on)
465 (org-archive-subtree))
466 (setq cntarch (1+ cntarch)))
467 (goto-char end)))))
468 (message "%d trees archived" cntarch)))
470 (defun org-toggle-archive-tag (&optional find-done)
471 "Toggle the archive tag for the current headline.
472 With prefix ARG, check all children of current headline and offer tagging
473 the children that do not contain any open TODO items."
474 (interactive "P")
475 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
476 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
477 'region-start-level 'region))
478 org-loop-over-headlines-in-active-region)
479 (org-map-entries
480 `(org-toggle-archive-tag ,find-done)
481 org-loop-over-headlines-in-active-region
482 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
483 (if find-done
484 (org-archive-all-done 'tag)
485 (let (set)
486 (save-excursion
487 (org-back-to-heading t)
488 (setq set (org-toggle-tag org-archive-tag))
489 (when set (hide-subtree)))
490 (and set (beginning-of-line 1))
491 (message "Subtree %s" (if set "archived" "unarchived"))))))
493 (defun org-archive-set-tag ()
494 "Set the ARCHIVE tag."
495 (interactive)
496 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
497 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
498 'region-start-level 'region))
499 org-loop-over-headlines-in-active-region)
500 (org-map-entries
501 'org-archive-set-tag
502 org-loop-over-headlines-in-active-region
503 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
504 (org-toggle-tag org-archive-tag 'on)))
506 ;;;###autoload
507 (defun org-archive-subtree-default ()
508 "Archive the current subtree with the default command.
509 This command is set with the variable `org-archive-default-command'."
510 (interactive)
511 (call-interactively org-archive-default-command))
513 ;;;###autoload
514 (defun org-archive-subtree-default-with-confirmation ()
515 "Archive the current subtree with the default command.
516 This command is set with the variable `org-archive-default-command'."
517 (interactive)
518 (if (y-or-n-p "Archive this subtree or entry? ")
519 (call-interactively org-archive-default-command)
520 (error "Abort")))
522 (provide 'org-archive)
524 ;;; org-archive.el ends here