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