Merge from emacs-24; up to 2013-01-03T02:31:36Z!rgm@gnu.org
[emacs.git] / lisp / org / org-archive.el
blob2fcfc8634fe68b8c48dd769081e6a7555f20d570
1 ;;; org-archive.el --- Archiving for Org-mode
3 ;; Copyright (C) 2004-2013 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 ;;;###autoload
185 (defun org-archive-subtree (&optional find-done)
186 "Move the current subtree to the archive.
187 The archive can be a certain top-level heading in the current file, or in
188 a different file. The tree will be moved to that location, the subtree
189 heading be marked DONE, and the current time will be added.
191 When called with prefix argument FIND-DONE, find whole trees without any
192 open TODO items and archive them (after getting confirmation from the user).
193 If the cursor is not at a headline when this command is called, try all level
194 1 trees. If the cursor is on a headline, only try the direct children of
195 this heading."
196 (interactive "P")
197 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
198 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
199 'region-start-level 'region))
200 org-loop-over-headlines-in-active-region)
201 (org-map-entries
202 `(progn (setq org-map-continue-from (progn (org-back-to-heading) (point)))
203 (org-archive-subtree ,find-done))
204 org-loop-over-headlines-in-active-region
205 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
206 (if find-done
207 (org-archive-all-done)
208 ;; Save all relevant TODO keyword-relatex variables
209 (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
210 (tr-org-todo-keywords-1 org-todo-keywords-1)
211 (tr-org-todo-kwd-alist org-todo-kwd-alist)
212 (tr-org-done-keywords org-done-keywords)
213 (tr-org-todo-regexp org-todo-regexp)
214 (tr-org-todo-line-regexp org-todo-line-regexp)
215 (tr-org-odd-levels-only org-odd-levels-only)
216 (this-buffer (current-buffer))
217 ;; start of variables that will be used for saving context
218 ;; The compiler complains about them - keep them anyway!
219 (file (abbreviate-file-name
220 (or (buffer-file-name (buffer-base-buffer))
221 (error "No file associated to buffer"))))
222 (olpath (mapconcat 'identity (org-get-outline-path) "/"))
223 (time (format-time-string
224 (substring (cdr org-time-stamp-formats) 1 -1)
225 (current-time)))
226 category todo priority ltags itags atags
227 ;; end of variables that will be used for saving context
228 location afile heading buffer level newfile-p infile-p visiting
229 datetree-date datetree-subheading-p)
231 ;; Find the local archive location
232 (setq location (org-get-local-archive-location)
233 afile (org-extract-archive-file location)
234 heading (org-extract-archive-heading location)
235 infile-p (equal file (abbreviate-file-name (or afile ""))))
236 (unless afile
237 (error "Invalid `org-archive-location'"))
239 (if (> (length afile) 0)
240 (setq newfile-p (not (file-exists-p afile))
241 visiting (find-buffer-visiting afile)
242 buffer (or visiting (find-file-noselect afile)))
243 (setq buffer (current-buffer)))
244 (unless buffer
245 (error "Cannot access file \"%s\"" afile))
246 (when (string-match "\\`datetree/" heading)
247 ;; Replace with ***, to represent the 3 levels of headings the
248 ;; datetree has.
249 (setq heading (replace-regexp-in-string "\\`datetree/" "***" heading))
250 (setq datetree-subheading-p (> (length heading) 3))
251 (setq datetree-date (org-date-to-gregorian
252 (or (org-entry-get nil "CLOSED" t) time))))
253 (if (and (> (length heading) 0)
254 (string-match "^\\*+" heading))
255 (setq level (match-end 0))
256 (setq heading nil level 0))
257 (save-excursion
258 (org-back-to-heading t)
259 ;; Get context information that will be lost by moving the tree
260 (setq category (org-get-category nil 'force-refresh)
261 todo (and (looking-at org-todo-line-regexp)
262 (match-string 2))
263 priority (org-get-priority
264 (if (match-end 3) (match-string 3) ""))
265 ltags (org-get-tags)
266 itags (org-delete-all ltags (org-get-tags-at))
267 atags (org-get-tags-at))
268 (setq ltags (mapconcat 'identity ltags " ")
269 itags (mapconcat 'identity itags " "))
270 ;; We first only copy, in case something goes wrong
271 ;; we need to protect `this-command', to avoid kill-region sets it,
272 ;; which would lead to duplication of subtrees
273 (let (this-command) (org-copy-subtree 1 nil t))
274 (set-buffer buffer)
275 ;; Enforce org-mode for the archive buffer
276 (if (not (derived-mode-p 'org-mode))
277 ;; Force the mode for future visits.
278 (let ((org-insert-mode-line-in-empty-file t)
279 (org-inhibit-startup t))
280 (call-interactively 'org-mode)))
281 (when newfile-p
282 (goto-char (point-max))
283 (insert (format "\nArchived entries from file %s\n\n"
284 (buffer-file-name this-buffer))))
285 (when datetree-date
286 (require 'org-datetree)
287 (org-datetree-find-date-create datetree-date)
288 (org-narrow-to-subtree))
289 ;; Force the TODO keywords of the original buffer
290 (let ((org-todo-line-regexp tr-org-todo-line-regexp)
291 (org-todo-keywords-1 tr-org-todo-keywords-1)
292 (org-todo-kwd-alist tr-org-todo-kwd-alist)
293 (org-done-keywords tr-org-done-keywords)
294 (org-todo-regexp tr-org-todo-regexp)
295 (org-todo-line-regexp tr-org-todo-line-regexp)
296 (org-odd-levels-only
297 (if (local-variable-p 'org-odd-levels-only (current-buffer))
298 org-odd-levels-only
299 tr-org-odd-levels-only)))
300 (goto-char (point-min))
301 (show-all)
302 (if (and heading (not (and datetree-date (not datetree-subheading-p))))
303 (progn
304 (if (re-search-forward
305 (concat "^" (regexp-quote heading)
306 (org-re "[ \t]*\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\($\\|\r\\)"))
307 nil t)
308 (goto-char (match-end 0))
309 ;; Heading not found, just insert it at the end
310 (goto-char (point-max))
311 (or (bolp) (insert "\n"))
312 ;; datetrees don't need too much spacing
313 (insert (if datetree-date "" "\n") heading "\n")
314 (end-of-line 0))
315 ;; Make the subtree visible
316 (show-subtree)
317 (if org-archive-reversed-order
318 (progn
319 (org-back-to-heading t)
320 (outline-next-heading))
321 (org-end-of-subtree t))
322 (skip-chars-backward " \t\r\n")
323 (and (looking-at "[ \t\r\n]*")
324 ;; datetree archives don't need so much spacing.
325 (replace-match (if datetree-date "\n" "\n\n"))))
326 ;; No specific heading, just go to end of file.
327 (goto-char (point-max)) (unless datetree-date (insert "\n")))
328 ;; Paste
329 (org-paste-subtree (org-get-valid-level level (and heading 1)))
330 ;; Shall we append inherited tags?
331 (and itags
332 (or (and (eq org-archive-subtree-add-inherited-tags 'infile)
333 infile-p)
334 (eq org-archive-subtree-add-inherited-tags t))
335 (org-set-tags-to atags))
336 ;; Mark the entry as done
337 (when (and org-archive-mark-done
338 (looking-at org-todo-line-regexp)
339 (or (not (match-end 2))
340 (not (member (match-string 2) org-done-keywords))))
341 (let (org-log-done org-todo-log-states)
342 (org-todo
343 (car (or (member org-archive-mark-done org-done-keywords)
344 org-done-keywords)))))
346 ;; Add the context info
347 (when org-archive-save-context-info
348 (let ((l org-archive-save-context-info) e n v)
349 (while (setq e (pop l))
350 (when (and (setq v (symbol-value e))
351 (stringp v) (string-match "\\S-" v))
352 (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
353 (org-entry-put (point) n v)))))
355 (widen)
356 ;; Save and kill the buffer, if it is not the same buffer.
357 (when (not (eq this-buffer buffer))
358 (save-buffer))))
359 ;; Here we are back in the original buffer. Everything seems to have
360 ;; worked. So now cut the tree and finish up.
361 (let (this-command) (org-cut-subtree))
362 (when (featurep 'org-inlinetask)
363 (org-inlinetask-remove-END-maybe))
364 (setq org-markers-to-move nil)
365 (message "Subtree archived %s"
366 (if (eq this-buffer buffer)
367 (concat "under heading: " heading)
368 (concat "in file: " (abbreviate-file-name afile))))))
369 (org-reveal)
370 (if (looking-at "^[ \t]*$")
371 (outline-next-visible-heading 1))))
373 ;;;###autoload
374 (defun org-archive-to-archive-sibling ()
375 "Archive the current heading by moving it under the archive sibling.
376 The archive sibling is a sibling of the heading with the heading name
377 `org-archive-sibling-heading' and an `org-archive-tag' tag. If this
378 sibling does not exist, it will be created at the end of the subtree."
379 (interactive)
380 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
381 (let ((cl (when (eq org-loop-over-headlines-in-active-region 'start-level)
382 'region-start-level 'region))
383 org-loop-over-headlines-in-active-region)
384 (org-map-entries
385 '(progn (setq org-map-continue-from
386 (progn (org-back-to-heading)
387 (if (looking-at (concat "^.*:" org-archive-tag ":.*$"))
388 (org-end-of-subtree t)
389 (point))))
390 (when (org-at-heading-p)
391 (org-archive-to-archive-sibling)))
392 org-loop-over-headlines-in-active-region
393 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
394 (save-restriction
395 (widen)
396 (let (b e pos leader level)
397 (org-back-to-heading t)
398 (looking-at org-outline-regexp)
399 (setq leader (match-string 0)
400 level (funcall outline-level))
401 (setq pos (point))
402 (condition-case nil
403 (outline-up-heading 1 t)
404 (error (setq e (point-max)) (goto-char (point-min))))
405 (setq b (point))
406 (unless e
407 (condition-case nil
408 (org-end-of-subtree t t)
409 (error (goto-char (point-max))))
410 (setq e (point)))
411 (goto-char b)
412 (unless (re-search-forward
413 (concat "^" (regexp-quote leader)
414 "[ \t]*"
415 org-archive-sibling-heading
416 "[ \t]*:"
417 org-archive-tag ":") e t)
418 (goto-char e)
419 (or (bolp) (newline))
420 (insert leader org-archive-sibling-heading "\n")
421 (beginning-of-line 0)
422 (org-toggle-tag org-archive-tag 'on))
423 (beginning-of-line 1)
424 (if org-archive-reversed-order
425 (outline-next-heading)
426 (org-end-of-subtree t t))
427 (save-excursion
428 (goto-char pos)
429 (let ((this-command this-command)) (org-cut-subtree)))
430 (org-paste-subtree (org-get-valid-level level 1))
431 (org-set-property
432 "ARCHIVE_TIME"
433 (format-time-string
434 (substring (cdr org-time-stamp-formats) 1 -1)
435 (current-time)))
436 (outline-up-heading 1 t)
437 (hide-subtree)
438 (org-cycle-show-empty-lines 'folded)
439 (goto-char pos)))
440 (org-reveal)
441 (if (looking-at "^[ \t]*$")
442 (outline-next-visible-heading 1))))
444 (defun org-archive-all-done (&optional tag)
445 "Archive sublevels of the current tree without open TODO items.
446 If the cursor is not on a headline, try all level 1 trees. If
447 it is on a headline, try all direct children.
448 When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
449 (let ((re org-not-done-heading-regexp) re1
450 (rea (concat ".*:" org-archive-tag ":"))
451 (begm (make-marker))
452 (endm (make-marker))
453 (question (if tag "Set ARCHIVE tag (no open TODO items)? "
454 "Move subtree to archive (no open TODO items)? "))
455 beg end (cntarch 0))
456 (if (org-at-heading-p)
457 (progn
458 (setq re1 (concat "^" (regexp-quote
459 (make-string
460 (+ (- (match-end 0) (match-beginning 0) 1)
461 (if org-odd-levels-only 2 1))
462 ?*))
463 " "))
464 (move-marker begm (point))
465 (move-marker endm (org-end-of-subtree t)))
466 (setq re1 "^* ")
467 (move-marker begm (point-min))
468 (move-marker endm (point-max)))
469 (save-excursion
470 (goto-char begm)
471 (while (re-search-forward re1 endm t)
472 (setq beg (match-beginning 0)
473 end (save-excursion (org-end-of-subtree t) (point)))
474 (goto-char beg)
475 (if (re-search-forward re end t)
476 (goto-char end)
477 (goto-char beg)
478 (if (and (or (not tag) (not (looking-at rea)))
479 (y-or-n-p question))
480 (progn
481 (if tag
482 (org-toggle-tag org-archive-tag 'on)
483 (org-archive-subtree))
484 (setq cntarch (1+ cntarch)))
485 (goto-char end)))))
486 (message "%d trees archived" cntarch)))
488 ;;;###autoload
489 (defun org-toggle-archive-tag (&optional find-done)
490 "Toggle the archive tag for the current headline.
491 With prefix ARG, check all children of current headline and offer tagging
492 the children that do not contain any open TODO items."
493 (interactive "P")
494 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
495 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
496 'region-start-level 'region))
497 org-loop-over-headlines-in-active-region)
498 (org-map-entries
499 `(org-toggle-archive-tag ,find-done)
500 org-loop-over-headlines-in-active-region
501 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
502 (if find-done
503 (org-archive-all-done 'tag)
504 (let (set)
505 (save-excursion
506 (org-back-to-heading t)
507 (setq set (org-toggle-tag org-archive-tag))
508 (when set (hide-subtree)))
509 (and set (beginning-of-line 1))
510 (message "Subtree %s" (if set "archived" "unarchived"))))))
512 (defun org-archive-set-tag ()
513 "Set the ARCHIVE tag."
514 (interactive)
515 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
516 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
517 'region-start-level 'region))
518 org-loop-over-headlines-in-active-region)
519 (org-map-entries
520 'org-archive-set-tag
521 org-loop-over-headlines-in-active-region
522 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
523 (org-toggle-tag org-archive-tag 'on)))
525 ;;;###autoload
526 (defun org-archive-subtree-default ()
527 "Archive the current subtree with the default command.
528 This command is set with the variable `org-archive-default-command'."
529 (interactive)
530 (call-interactively org-archive-default-command))
532 ;;;###autoload
533 (defun org-archive-subtree-default-with-confirmation ()
534 "Archive the current subtree with the default command.
535 This command is set with the variable `org-archive-default-command'."
536 (interactive)
537 (if (y-or-n-p "Archive this subtree or entry? ")
538 (call-interactively org-archive-default-command)
539 (error "Abort")))
541 (provide 'org-archive)
543 ;; Local variables:
544 ;; generated-autoload-file: "org-loaddefs.el"
545 ;; End:
547 ;;; org-archive.el ends here