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