1 ;;; org-archive.el --- Archiving for Org-mode
3 ;; Copyright (C) 2004-2015 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;; This file contains the face definitions for 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."
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."
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."
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."
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'."
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."
80 :package-version
'(Org .
"8.0")
83 (defcustom org-archive-subtree-add-inherited-tags
'infile
84 "Non-nil means append inherited tags when archiving a subtree."
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
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 (defvar org-archive-hook nil
123 "Hook run after successfully archiving a subtree.
124 Hook functions are called with point on the subtree in the
125 original file. At this stage, the subtree has been added to the
126 archive location, but not yet deleted from the original file.")
128 (defun org-get-local-archive-location ()
129 "Get the archive location applicable at point."
130 (let ((re "^[ \t]*#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
135 (setq prop
(org-entry-get nil
"ARCHIVE" 'inherit
))
137 ((and prop
(string-match "\\S-" prop
))
139 ((or (re-search-backward re nil t
)
140 (re-search-forward re nil t
))
142 (t org-archive-location
))))))
145 (defun org-add-archive-files (files)
146 "Splice the archive files into the list of files.
147 This implies visiting all these files and finding out what the
154 (if (not (file-exists-p f
))
156 (with-current-buffer (org-get-agenda-file-buffer f
)
157 (cons f
(org-all-archive-files)))))
160 (defun org-all-archive-files ()
161 "Get a list of all archive files used in the current buffer."
165 (goto-char (point-min))
166 (while (re-search-forward
167 "^[ \t]*\\(#\\+\\|:\\)ARCHIVE:[ \t]+\\(.*\\)"
169 (setq file
(org-extract-archive-file
170 (org-match-string-no-properties 2)))
171 (and file
(> (length file
) 0) (file-exists-p file
)
172 (add-to-list 'files file
)))))
173 (setq files
(nreverse files
))
174 (setq file
(org-extract-archive-file))
175 (and file
(> (length file
) 0) (file-exists-p file
)
176 (add-to-list 'files file
))
179 (defun org-extract-archive-file (&optional location
)
180 "Extract and expand the file name from archive LOCATION.
181 if LOCATION is not given, the value of `org-archive-location' is used."
182 (setq location
(or location org-archive-location
))
183 (if (string-match "\\(.*\\)::\\(.*\\)" location
)
184 (if (= (match-beginning 1) (match-end 1))
185 (buffer-file-name (buffer-base-buffer))
187 (format (match-string 1 location
)
188 (file-name-nondirectory
189 (buffer-file-name (buffer-base-buffer))))))))
191 (defun org-extract-archive-heading (&optional location
)
192 "Extract the heading from archive LOCATION.
193 if LOCATION is not given, the value of `org-archive-location' is used."
194 (setq location
(or location org-archive-location
))
195 (if (string-match "\\(.*\\)::\\(.*\\)" location
)
196 (format (match-string 2 location
)
197 (file-name-nondirectory
198 (buffer-file-name (buffer-base-buffer))))))
201 (defun org-archive-subtree (&optional find-done
)
202 "Move the current subtree to the archive.
203 The archive can be a certain top-level heading in the current file, or in
204 a different file. The tree will be moved to that location, the subtree
205 heading be marked DONE, and the current time will be added.
207 When called with a single prefix argument FIND-DONE, find whole trees without any
208 open TODO items and archive them (after getting confirmation from the user).
209 When called with a double prefix argument, find whole trees with timestamps before
210 today and archive them (after getting confirmation from the user).
211 If the cursor is not at a headline when these commands are called, try all level
212 1 trees. If the cursor is on a headline, only try the direct children of
215 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region
)
216 (let ((cl (if (eq org-loop-over-headlines-in-active-region
'start-level
)
217 'region-start-level
'region
))
218 org-loop-over-headlines-in-active-region
)
220 `(progn (setq org-map-continue-from
(progn (org-back-to-heading) (point)))
221 (org-archive-subtree ,find-done
))
222 org-loop-over-headlines-in-active-region
223 cl
(if (outline-invisible-p) (org-end-of-subtree nil t
))))
225 ((equal find-done
'(4)) (org-archive-all-done))
226 ((equal find-done
'(16)) (org-archive-all-old))
228 ;; Save all relevant TODO keyword-relatex variables
229 (let ((tr-org-todo-line-regexp org-todo-line-regexp
) ; keep despite compiler
230 (tr-org-todo-keywords-1 org-todo-keywords-1
)
231 (tr-org-todo-kwd-alist org-todo-kwd-alist
)
232 (tr-org-done-keywords org-done-keywords
)
233 (tr-org-todo-regexp org-todo-regexp
)
234 (tr-org-todo-line-regexp org-todo-line-regexp
)
235 (tr-org-odd-levels-only org-odd-levels-only
)
236 (this-buffer (current-buffer))
237 ;; start of variables that will be used for saving context
238 ;; The compiler complains about them - keep them anyway!
239 (file (abbreviate-file-name
240 (or (buffer-file-name (buffer-base-buffer))
241 (error "No file associated to buffer"))))
242 (olpath (mapconcat 'identity
(org-get-outline-path) "/"))
243 (time (format-time-string
244 (substring (cdr org-time-stamp-formats
) 1 -
1)))
245 category todo priority ltags itags atags
246 ;; end of variables that will be used for saving context
247 location afile heading buffer level newfile-p infile-p visiting
248 datetree-date datetree-subheading-p
)
250 ;; Find the local archive location
251 (setq location
(org-get-local-archive-location)
252 afile
(org-extract-archive-file location
)
253 heading
(org-extract-archive-heading location
)
254 infile-p
(equal file
(abbreviate-file-name (or afile
""))))
256 (error "Invalid `org-archive-location'"))
258 (if (> (length afile
) 0)
259 (setq newfile-p
(not (file-exists-p afile
))
260 visiting
(find-buffer-visiting afile
)
261 buffer
(or visiting
(find-file-noselect afile
)))
262 (setq buffer
(current-buffer)))
264 (error "Cannot access file \"%s\"" afile
))
265 (when (string-match "\\`datetree/" heading
)
266 ;; Replace with ***, to represent the 3 levels of headings the
268 (setq heading
(replace-regexp-in-string "\\`datetree/" "***" heading
))
269 (setq datetree-subheading-p
(> (length heading
) 3))
270 (setq datetree-date
(org-date-to-gregorian
271 (or (org-entry-get nil
"CLOSED" t
) time
))))
272 (if (and (> (length heading
) 0)
273 (string-match "^\\*+" heading
))
274 (setq level
(match-end 0))
275 (setq heading nil level
0))
277 (org-back-to-heading t
)
278 ;; Get context information that will be lost by moving the tree
279 (setq category
(org-get-category nil
'force-refresh
)
280 todo
(and (looking-at org-todo-line-regexp
)
282 priority
(org-get-priority
283 (if (match-end 3) (match-string 3) ""))
285 itags
(org-delete-all ltags
(org-get-tags-at))
286 atags
(org-get-tags-at))
287 (setq ltags
(mapconcat 'identity ltags
" ")
288 itags
(mapconcat 'identity itags
" "))
289 ;; We first only copy, in case something goes wrong
290 ;; we need to protect `this-command', to avoid kill-region sets it,
291 ;; which would lead to duplication of subtrees
292 (let (this-command) (org-copy-subtree 1 nil t
))
294 ;; Enforce org-mode for the archive buffer
295 (if (not (derived-mode-p 'org-mode
))
296 ;; Force the mode for future visits.
297 (let ((org-insert-mode-line-in-empty-file t
)
298 (org-inhibit-startup t
))
299 (call-interactively 'org-mode
)))
300 (when (and newfile-p org-archive-file-header-format
)
301 (goto-char (point-max))
302 (insert (format org-archive-file-header-format
303 (buffer-file-name this-buffer
))))
305 (require 'org-datetree
)
306 (org-datetree-find-date-create datetree-date
)
307 (org-narrow-to-subtree))
308 ;; Force the TODO keywords of the original buffer
309 (let ((org-todo-line-regexp tr-org-todo-line-regexp
)
310 (org-todo-keywords-1 tr-org-todo-keywords-1
)
311 (org-todo-kwd-alist tr-org-todo-kwd-alist
)
312 (org-done-keywords tr-org-done-keywords
)
313 (org-todo-regexp tr-org-todo-regexp
)
314 (org-todo-line-regexp tr-org-todo-line-regexp
)
316 (if (local-variable-p 'org-odd-levels-only
(current-buffer))
318 tr-org-odd-levels-only
)))
319 (goto-char (point-min))
321 (if (and heading
(not (and datetree-date
(not datetree-subheading-p
))))
323 (if (re-search-forward
324 (concat "^" (regexp-quote heading
)
325 (org-re "[ \t]*\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\($\\|\r\\)"))
327 (goto-char (match-end 0))
328 ;; Heading not found, just insert it at the end
329 (goto-char (point-max))
330 (or (bolp) (insert "\n"))
331 ;; datetrees don't need too much spacing
332 (insert (if datetree-date
"" "\n") heading
"\n")
334 ;; Make the subtree visible
336 (if org-archive-reversed-order
338 (org-back-to-heading t
)
339 (outline-next-heading))
340 (org-end-of-subtree t
))
341 (skip-chars-backward " \t\r\n")
342 (and (looking-at "[ \t\r\n]*")
343 ;; datetree archives don't need so much spacing.
344 (replace-match (if datetree-date
"\n" "\n\n"))))
345 ;; No specific heading, just go to end of file.
346 (goto-char (point-max)) (unless datetree-date
(insert "\n")))
348 (org-paste-subtree (org-get-valid-level level
(and heading
1)))
349 ;; Shall we append inherited tags?
351 (or (and (eq org-archive-subtree-add-inherited-tags
'infile
)
353 (eq org-archive-subtree-add-inherited-tags t
))
354 (org-set-tags-to atags
))
355 ;; Mark the entry as done
356 (when (and org-archive-mark-done
357 (looking-at org-todo-line-regexp
)
358 (or (not (match-end 2))
359 (not (member (match-string 2) org-done-keywords
))))
360 (let (org-log-done org-todo-log-states
)
362 (car (or (member org-archive-mark-done org-done-keywords
)
363 org-done-keywords
)))))
365 ;; Add the context info
366 (when org-archive-save-context-info
367 (let ((l org-archive-save-context-info
) e n v
)
368 (while (setq e
(pop l
))
369 (when (and (setq v
(symbol-value e
))
370 (stringp v
) (string-match "\\S-" v
))
371 (setq n
(concat "ARCHIVE_" (upcase (symbol-name e
))))
372 (org-entry-put (point) n v
)))))
375 ;; Save and kill the buffer, if it is not the same buffer.
376 (when (not (eq this-buffer buffer
))
378 ;; Here we are back in the original buffer. Everything seems
379 ;; to have worked. So now run hooks, cut the tree and finish
381 (run-hooks 'org-archive-hook
)
382 (let (this-command) (org-cut-subtree))
383 (when (featurep 'org-inlinetask
)
384 (org-inlinetask-remove-END-maybe))
385 (setq org-markers-to-move nil
)
386 (message "Subtree archived %s"
387 (if (eq this-buffer buffer
)
388 (concat "under heading: " heading
)
389 (concat "in file: " (abbreviate-file-name afile
)))))))
391 (if (looking-at "^[ \t]*$")
392 (outline-next-visible-heading 1))))
395 (defun org-archive-to-archive-sibling ()
396 "Archive the current heading by moving it under the archive sibling.
397 The archive sibling is a sibling of the heading with the heading name
398 `org-archive-sibling-heading' and an `org-archive-tag' tag. If this
399 sibling does not exist, it will be created at the end of the subtree."
401 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region
)
402 (let ((cl (when (eq org-loop-over-headlines-in-active-region
'start-level
)
403 'region-start-level
'region
))
404 org-loop-over-headlines-in-active-region
)
406 '(progn (setq org-map-continue-from
407 (progn (org-back-to-heading)
408 (if (looking-at (concat "^.*:" org-archive-tag
":.*$"))
409 (org-end-of-subtree t
)
411 (when (org-at-heading-p)
412 (org-archive-to-archive-sibling)))
413 org-loop-over-headlines-in-active-region
414 cl
(if (outline-invisible-p) (org-end-of-subtree nil t
))))
417 (let (b e pos leader level
)
418 (org-back-to-heading t
)
419 (looking-at org-outline-regexp
)
420 (setq leader
(match-string 0)
421 level
(funcall outline-level
))
424 (outline-up-heading 1 t
)
425 (error (setq e
(point-max)) (goto-char (point-min))))
429 (org-end-of-subtree t t
)
430 (error (goto-char (point-max))))
433 (unless (re-search-forward
434 (concat "^" (regexp-quote leader
)
436 org-archive-sibling-heading
438 org-archive-tag
":") e t
)
440 (or (bolp) (newline))
441 (insert leader org-archive-sibling-heading
"\n")
442 (beginning-of-line 0)
443 (org-toggle-tag org-archive-tag
'on
))
444 (beginning-of-line 1)
445 (if org-archive-reversed-order
446 (outline-next-heading)
447 (org-end-of-subtree t t
))
450 (let ((this-command this-command
)) (org-cut-subtree)))
451 (org-paste-subtree (org-get-valid-level level
1))
455 (substring (cdr org-time-stamp-formats
) 1 -
1)))
456 (outline-up-heading 1 t
)
458 (org-cycle-show-empty-lines 'folded
)
461 (if (looking-at "^[ \t]*$")
462 (outline-next-visible-heading 1))))
464 (defun org-archive-all-done (&optional tag
)
465 "Archive sublevels of the current tree without open TODO items.
466 If the cursor is not on a headline, try all level 1 trees. If
467 it is on a headline, try all direct children.
468 When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
469 (org-archive-all-matches
471 (unless (re-search-forward org-not-done-heading-regexp end t
)
472 "no open TODO items"))
475 (defun org-archive-all-old (&optional tag
)
476 "Archive sublevels of the current tree with timestamps prior to today.
477 If the cursor is not on a headline, try all level 1 trees. If
478 it is on a headline, try all direct children.
479 When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
480 (org-archive-all-matches
483 (and (re-search-forward org-ts-regexp end t
)
484 (setq ts
(match-string 0))
485 (< (org-time-stamp-to-now ts
) 0)
487 (concat "--\\(" org-ts-regexp
"\\)")))
488 (concat "old timestamp " ts
)
489 (setq ts
(concat "old timestamp " ts
(match-string 0)))
490 (and (< (org-time-stamp-to-now (match-string 1)) 0)
494 (defun org-archive-all-matches (predicate &optional tag
)
495 "Archive sublevels of the current tree that match PREDICATE.
497 PREDICATE is a function of two arguments, BEG and END, which
498 specify the beginning and end of the headline being considered.
499 It is called with point positioned at BEG. The headline will be
500 archived if PREDICATE returns non-nil. If the return value of
501 PREDICATE is a string, it should describe the reason for
502 archiving the heading.
504 If the cursor is not on a headline, try all level 1 trees. If it
505 is on a headline, try all direct children. When TAG is non-nil,
506 don't move trees, but mark them with the ARCHIVE tag."
507 (let ((rea (concat ".*:" org-archive-tag
":")) re1
510 (question (if tag
"Set ARCHIVE tag? "
511 "Move subtree to archive? "))
512 reason beg end
(cntarch 0))
513 (if (org-at-heading-p)
515 (setq re1
(concat "^" (regexp-quote
517 (+ (- (match-end 0) (match-beginning 0) 1)
518 (if org-odd-levels-only
2 1))
521 (move-marker begm
(point))
522 (move-marker endm
(org-end-of-subtree t
)))
524 (move-marker begm
(point-min))
525 (move-marker endm
(point-max)))
528 (while (re-search-forward re1 endm t
)
529 (setq beg
(match-beginning 0)
530 end
(save-excursion (org-end-of-subtree t
) (point)))
532 (if (not (setq reason
(funcall predicate beg end
)))
535 (if (and (or (not tag
) (not (looking-at rea
)))
538 (concat question
"(" reason
")")
542 (org-toggle-tag org-archive-tag
'on
)
543 (org-archive-subtree))
544 (setq cntarch
(1+ cntarch
)))
546 (message "%d trees archived" cntarch
)))
549 (defun org-toggle-archive-tag (&optional find-done
)
550 "Toggle the archive tag for the current headline.
551 With prefix ARG, check all children of current headline and offer tagging
552 the children that do not contain any open TODO items."
554 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region
)
555 (let ((cl (if (eq org-loop-over-headlines-in-active-region
'start-level
)
556 'region-start-level
'region
))
557 org-loop-over-headlines-in-active-region
)
559 `(org-toggle-archive-tag ,find-done
)
560 org-loop-over-headlines-in-active-region
561 cl
(if (outline-invisible-p) (org-end-of-subtree nil t
))))
563 (org-archive-all-done 'tag
)
566 (org-back-to-heading t
)
567 (setq set
(org-toggle-tag org-archive-tag
))
568 (when set
(hide-subtree)))
569 (and set
(beginning-of-line 1))
570 (message "Subtree %s" (if set
"archived" "unarchived"))))))
572 (defun org-archive-set-tag ()
573 "Set the ARCHIVE tag."
575 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region
)
576 (let ((cl (if (eq org-loop-over-headlines-in-active-region
'start-level
)
577 'region-start-level
'region
))
578 org-loop-over-headlines-in-active-region
)
581 org-loop-over-headlines-in-active-region
582 cl
(if (outline-invisible-p) (org-end-of-subtree nil t
))))
583 (org-toggle-tag org-archive-tag
'on
)))
586 (defun org-archive-subtree-default ()
587 "Archive the current subtree with the default command.
588 This command is set with the variable `org-archive-default-command'."
590 (call-interactively org-archive-default-command
))
593 (defun org-archive-subtree-default-with-confirmation ()
594 "Archive the current subtree with the default command.
595 This command is set with the variable `org-archive-default-command'."
597 (if (y-or-n-p "Archive this subtree or entry? ")
598 (call-interactively org-archive-default-command
)
601 (provide 'org-archive
)
604 ;; generated-autoload-file: "org-loaddefs.el"
607 ;;; org-archive.el ends here