1 ;;; org-archive.el --- Archiving for Org -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2004-2019 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: https://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 <https://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;; This file contains the face definitions for Org.
34 (declare-function org-element-type
"org-element" (element))
35 (declare-function org-datetree-find-date-create
"org-datetree" (date &optional keep-restriction
))
36 (declare-function org-inlinetask-remove-END-maybe
"org-inlinetask" ())
38 (defcustom org-archive-default-command
'org-archive-subtree
39 "The default archiving command."
42 (const org-archive-subtree
)
43 (const org-archive-to-archive-sibling
)
44 (const org-archive-set-tag
)))
46 (defcustom org-archive-reversed-order nil
47 "Non-nil means make the tree first child under the archive heading, not last."
52 (defcustom org-archive-sibling-heading
"Archive"
53 "Name of the local archive sibling that is used to archive entries locally.
54 Locally means: in the tree, under a sibling.
55 See `org-archive-to-archive-sibling' for more information."
59 (defcustom org-archive-mark-done nil
60 "Non-nil means mark entries as DONE when they are moved to the archive file.
61 This can be a string to set the keyword to use. When non-nil, Org will
62 use the first keyword in its list that means done."
67 (string :tag
"Use this keyword")))
69 (defcustom org-archive-stamp-time t
70 "Non-nil means add a time stamp to entries moved to an archive file.
71 This variable is obsolete and has no effect anymore, instead add or remove
72 `time' from the variable `org-archive-save-context-info'."
76 (defcustom org-archive-file-header-format
"\nArchived entries from file %s\n\n"
77 "The header format string for newly created archive files.
78 When nil, no header will be inserted.
79 When a string, a %s formatter will be replaced by the file name."
82 :package-version
'(Org .
"8.0")
85 (defcustom org-archive-subtree-add-inherited-tags
'infile
86 "Non-nil means append inherited tags when archiving a subtree."
90 (const :tag
"Never" nil
)
91 (const :tag
"When archiving a subtree to the same file" infile
)
92 (const :tag
"Always" t
)))
94 (defcustom org-archive-save-context-info
'(time file olpath category todo itags
)
95 "Parts of context info that should be stored as properties when archiving.
96 When a subtree is moved to an archive file, it loses information given by
97 context, like inherited tags, the category, and possibly also the TODO
98 state (depending on the variable `org-archive-mark-done').
99 This variable can be a list of any of the following symbols:
101 time The time of archiving.
102 file The file where the entry originates.
103 ltags The local tags, in the headline of the subtree.
104 itags The tags the subtree inherits from further up the hierarchy.
105 todo The pre-archive TODO state.
106 category The category, taken from file name or #+CATEGORY lines.
107 olpath The outline path to the item. These are all headlines above
108 the current item, separated by /, like a file path.
110 For each symbol present in the list, a property will be created in
111 the archived entry, with a prefix \"ARCHIVE_\", to remember this
114 :type
'(set :greedy t
115 (const :tag
"Time" time
)
116 (const :tag
"File" file
)
117 (const :tag
"Category" category
)
118 (const :tag
"TODO state" todo
)
119 (const :tag
"Priority" priority
)
120 (const :tag
"Inherited tags" itags
)
121 (const :tag
"Outline path" olpath
)
122 (const :tag
"Local tags" ltags
)))
124 (defvar org-archive-hook nil
125 "Hook run after successfully archiving a subtree.
126 Hook functions are called with point on the subtree in the
127 original file. At this stage, the subtree has been added to the
128 archive location, but not yet deleted from the original file.")
131 (defun org-add-archive-files (files)
132 "Splice the archive files into the list of files.
133 This implies visiting all these files and finding out what the
140 (if (not (file-exists-p f
))
142 (with-current-buffer (org-get-agenda-file-buffer f
)
143 (cons f
(org-all-archive-files)))))
146 (defun org-all-archive-files ()
147 "List of all archive files used in the current buffer."
148 (let* ((case-fold-search t
)
149 (files `(,(car (org-archive--compute-location org-archive-location
)))))
151 (while (re-search-forward "^[ \t]*:ARCHIVE:" nil t
)
152 (when (org-at-property-p)
153 (pcase (org-archive--compute-location (match-string 3))
155 (when (org-string-nw-p file
)
156 (cl-pushnew file files
:test
#'file-equal-p
))))))
157 (cl-remove-if-not #'file-exists-p
(nreverse files
)))))
159 (defun org-archive--compute-location (location)
160 "Extract and expand the location from archive LOCATION.
161 Return a pair (FILE . HEADING) where FILE is the file name and
162 HEADING the heading of the archive location, as strings. Raise
163 an error if LOCATION is not a valid archive location."
164 (unless (string-match "::" location
)
165 (error "Invalid archive location: %S" location
))
166 (let ((current-file (buffer-file-name (buffer-base-buffer)))
167 (file-fmt (substring location
0 (match-beginning 0)))
168 (heading-fmt (substring location
(match-end 0))))
171 (if (org-string-nw-p file-fmt
)
173 (format file-fmt
(file-name-nondirectory current-file
)))
176 (format heading-fmt
(file-name-nondirectory current-file
)))))
179 (defun org-archive-subtree (&optional find-done
)
180 "Move the current subtree to the archive.
181 The archive can be a certain top-level heading in the current
182 file, or in a different file. The tree will be moved to that
183 location, the subtree heading be marked DONE, and the current
186 When called with a single prefix argument FIND-DONE, find whole
187 trees without any open TODO items and archive them (after getting
188 confirmation from the user). When called with a double prefix
189 argument, find whole trees with timestamps before today and
190 archive them (after getting confirmation from the user). If the
191 cursor is not at a headline when these commands are called, try
192 all level 1 trees. If the cursor is on a headline, only try the
193 direct children of this heading."
195 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region
)
196 (let ((cl (if (eq org-loop-over-headlines-in-active-region
'start-level
)
197 'region-start-level
'region
))
198 org-loop-over-headlines-in-active-region
)
200 `(progn (setq org-map-continue-from
(progn (org-back-to-heading) (point)))
201 (org-archive-subtree ,find-done
))
202 org-loop-over-headlines-in-active-region
203 cl
(if (org-invisible-p) (org-end-of-subtree nil t
))))
205 ((equal find-done
'(4)) (org-archive-all-done))
206 ((equal find-done
'(16)) (org-archive-all-old))
208 ;; Save all relevant TODO keyword-related variables.
209 (let* ((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 (time (format-time-string
217 (substring (cdr org-time-stamp-formats
) 1 -
1)))
218 (file (abbreviate-file-name
219 (or (buffer-file-name (buffer-base-buffer))
220 (error "No file associated to buffer"))))
221 (location (org-archive--compute-location
222 (or (org-entry-get nil
"ARCHIVE" 'inherit
)
223 org-archive-location
)))
224 (afile (car location
))
225 (heading (cdr location
))
226 (infile-p (equal file
(abbreviate-file-name (or afile
""))))
227 (newfile-p (and (org-string-nw-p afile
)
228 (not (file-exists-p afile
))))
229 (buffer (cond ((not (org-string-nw-p afile
)) this-buffer
)
230 ((find-buffer-visiting afile
))
231 ((find-file-noselect afile
))
232 (t (error "Cannot access file \"%s\"" afile
))))
233 level datetree-date datetree-subheading-p
)
234 (when (string-match "\\`datetree/" heading
)
235 ;; Replace with ***, to represent the 3 levels of headings the
237 (setq heading
(replace-regexp-in-string "\\`datetree/" "***" heading
))
238 (setq datetree-subheading-p
(> (length heading
) 3))
239 (setq datetree-date
(org-date-to-gregorian
240 (or (org-entry-get nil
"CLOSED" t
) time
))))
241 (if (and (> (length heading
) 0)
242 (string-match "^\\*+" heading
))
243 (setq level
(match-end 0))
244 (setq heading nil level
0))
246 (org-back-to-heading t
)
247 ;; Get context information that will be lost by moving the
248 ;; tree. See `org-archive-save-context-info'.
249 (let* ((all-tags (org-get-tags))
251 (cl-remove-if (lambda (tag)
252 (get-text-property 0 'inherited tag
))
255 (cl-remove-if-not (lambda (tag)
256 (get-text-property 0 'inherited tag
))
259 `((category .
,(org-get-category nil
'force-refresh
))
261 (itags .
,(mapconcat #'identity inherited-tags
" "))
262 (ltags .
,(mapconcat #'identity local-tags
" "))
263 (olpath .
,(mapconcat #'identity
264 (org-get-outline-path)
267 (todo .
,(org-entry-get (point) "TODO")))))
268 ;; We first only copy, in case something goes wrong
269 ;; we need to protect `this-command', to avoid kill-region sets it,
270 ;; which would lead to duplication of subtrees
271 (let (this-command) (org-copy-subtree 1 nil t
))
273 ;; Enforce Org mode for the archive buffer
274 (if (not (derived-mode-p 'org-mode
))
275 ;; Force the mode for future visits.
276 (let ((org-insert-mode-line-in-empty-file t
)
277 (org-inhibit-startup t
))
278 (call-interactively 'org-mode
)))
279 (when (and newfile-p org-archive-file-header-format
)
280 (goto-char (point-max))
281 (insert (format org-archive-file-header-format
282 (buffer-file-name this-buffer
))))
284 (require 'org-datetree
)
285 (org-datetree-find-date-create datetree-date
)
286 (org-narrow-to-subtree))
287 ;; Force the TODO keywords of the original buffer
288 (let ((org-todo-line-regexp tr-org-todo-line-regexp
)
289 (org-todo-keywords-1 tr-org-todo-keywords-1
)
290 (org-todo-kwd-alist tr-org-todo-kwd-alist
)
291 (org-done-keywords tr-org-done-keywords
)
292 (org-todo-regexp tr-org-todo-regexp
)
293 (org-todo-line-regexp tr-org-todo-line-regexp
)
295 (if (local-variable-p 'org-odd-levels-only
(current-buffer))
297 tr-org-odd-levels-only
)))
298 (goto-char (point-min))
299 (org-show-all '(headings blocks
))
300 (if (and heading
(not (and datetree-date
(not datetree-subheading-p
))))
302 (if (re-search-forward
303 (concat "^" (regexp-quote heading
)
304 "\\([ \t]+:\\(" org-tag-re
":\\)+\\)?[ \t]*$")
306 (goto-char (match-end 0))
307 ;; Heading not found, just insert it at the end
308 (goto-char (point-max))
309 (or (bolp) (insert "\n"))
310 ;; datetrees don't need too much spacing
311 (insert (if datetree-date
"" "\n") heading
"\n")
313 ;; Make the subtree visible
314 (outline-show-subtree)
315 (if org-archive-reversed-order
317 (org-back-to-heading t
)
318 (outline-next-heading))
319 (org-end-of-subtree t
))
320 (skip-chars-backward " \t\r\n")
321 (and (looking-at "[ \t\r\n]*")
322 ;; datetree archives don't need so much spacing.
323 (replace-match (if datetree-date
"\n" "\n\n"))))
324 ;; No specific heading, just go to end of file, or to the
325 ;; beginning, depending on `org-archive-reversed-order'.
326 (if org-archive-reversed-order
328 (goto-char (point-min))
329 (unless (org-at-heading-p) (outline-next-heading)))
330 (goto-char (point-max))
331 ;; Subtree narrowing can let the buffer end on
332 ;; a headline. `org-paste-subtree' then deletes it.
333 ;; To prevent this, make sure visible part of buffer
334 ;; always terminates on a new line, while limiting
335 ;; number of blank lines in a date tree.
336 (unless (and datetree-date
(bolp)) (insert "\n"))))
338 (org-paste-subtree (org-get-valid-level level
(and heading
1)))
339 ;; Shall we append inherited tags?
341 (or (and (eq org-archive-subtree-add-inherited-tags
'infile
)
343 (eq org-archive-subtree-add-inherited-tags t
))
344 (org-set-tags all-tags
))
345 ;; Mark the entry as done
346 (when (and org-archive-mark-done
347 (let ((case-fold-search nil
))
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
)
353 (car (or (member org-archive-mark-done org-done-keywords
)
354 org-done-keywords
)))))
356 ;; Add the context info.
357 (dolist (item org-archive-save-context-info
)
358 (let ((value (cdr (assq item context
))))
359 (when (org-string-nw-p value
)
362 (concat "ARCHIVE_" (upcase (symbol-name item
)))
365 ;; Here we are back in the original buffer. Everything seems
366 ;; to have worked. So now run hooks, cut the tree and finish
368 (run-hooks 'org-archive-hook
)
369 (let (this-command) (org-cut-subtree))
370 (when (featurep 'org-inlinetask
)
371 (org-inlinetask-remove-END-maybe))
372 (setq org-markers-to-move nil
)
373 (when org-provide-todo-statistics
375 ;; Go to parent, even if no children exist.
376 (org-up-heading-safe)
377 ;; Update cookie of parent.
378 (org-update-statistics-cookies nil
)))
379 (message "Subtree archived %s"
380 (if (eq this-buffer buffer
)
381 (concat "under heading: " heading
)
382 (concat "in file: " (abbreviate-file-name afile
)))))))
384 (if (looking-at "^[ \t]*$")
385 (outline-next-visible-heading 1))))
388 (defun org-archive-to-archive-sibling ()
389 "Archive the current heading by moving it under the archive sibling.
391 The archive sibling is a sibling of the heading with the heading name
392 `org-archive-sibling-heading' and an `org-archive-tag' tag. If this
393 sibling does not exist, it will be created at the end of the subtree.
395 Archiving time is retained in the ARCHIVE_TIME node property."
397 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region
)
398 (let ((cl (when (eq org-loop-over-headlines-in-active-region
'start-level
)
399 'region-start-level
'region
))
400 org-loop-over-headlines-in-active-region
)
402 '(progn (setq org-map-continue-from
403 (progn (org-back-to-heading)
404 (if (looking-at (concat "^.*:" org-archive-tag
":.*$"))
405 (org-end-of-subtree t
)
407 (when (org-at-heading-p)
408 (org-archive-to-archive-sibling)))
409 org-loop-over-headlines-in-active-region
410 cl
(if (org-invisible-p) (org-end-of-subtree nil t
))))
413 (let (b e pos leader level
)
414 (org-back-to-heading t
)
415 (looking-at org-outline-regexp
)
416 (setq leader
(match-string 0)
417 level
(funcall outline-level
))
418 (setq pos
(point-marker))
420 (outline-up-heading 1 t
)
421 (error (setq e
(point-max)) (goto-char (point-min))))
425 (org-end-of-subtree t t
)
426 (error (goto-char (point-max))))
429 (unless (re-search-forward
430 (concat "^" (regexp-quote leader
)
432 org-archive-sibling-heading
434 org-archive-tag
":") e t
)
436 (or (bolp) (newline))
437 (insert leader org-archive-sibling-heading
"\n")
438 (beginning-of-line 0)
439 (org-toggle-tag org-archive-tag
'on
))
440 (beginning-of-line 1)
441 (if org-archive-reversed-order
442 (outline-next-heading)
443 (org-end-of-subtree t t
))
446 (let ((this-command this-command
)) (org-cut-subtree)))
447 (org-paste-subtree (org-get-valid-level level
1))
451 (substring (cdr org-time-stamp-formats
) 1 -
1)))
452 (outline-up-heading 1 t
)
454 (org-cycle-show-empty-lines 'folded
)
455 (when org-provide-todo-statistics
456 ;; Update TODO statistics of parent.
457 (org-update-parent-todo-statistics))
460 (if (looking-at "^[ \t]*$")
461 (outline-next-visible-heading 1))))
463 (defun org-archive-all-done (&optional tag
)
464 "Archive sublevels of the current tree without open TODO items.
465 If the cursor is not on a headline, try all level 1 trees. If
466 it is on a headline, try all direct children.
467 When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
468 (org-archive-all-matches
470 (let ((case-fold-search nil
))
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 (org-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
(org-flag-subtree t
)))
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 (org-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