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