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
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 prefix argument FIND-DONE, find whole trees without any
208 open TODO items and archive them (after getting confirmation from the user).
209 If the cursor is not at a headline when this command is called, try all level
210 1 trees. If the cursor is on a headline, only try the direct children of
213 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region
)
214 (let ((cl (if (eq org-loop-over-headlines-in-active-region
'start-level
)
215 'region-start-level
'region
))
216 org-loop-over-headlines-in-active-region
)
218 `(progn (setq org-map-continue-from
(progn (org-back-to-heading) (point)))
219 (org-archive-subtree ,find-done
))
220 org-loop-over-headlines-in-active-region
221 cl
(if (outline-invisible-p) (org-end-of-subtree nil t
))))
223 (org-archive-all-done)
224 ;; Save all relevant TODO keyword-relatex variables
225 (let ((tr-org-todo-line-regexp org-todo-line-regexp
) ; keep despite compiler
226 (tr-org-todo-keywords-1 org-todo-keywords-1
)
227 (tr-org-todo-kwd-alist org-todo-kwd-alist
)
228 (tr-org-done-keywords org-done-keywords
)
229 (tr-org-todo-regexp org-todo-regexp
)
230 (tr-org-todo-line-regexp org-todo-line-regexp
)
231 (tr-org-odd-levels-only org-odd-levels-only
)
232 (this-buffer (current-buffer))
233 ;; start of variables that will be used for saving context
234 ;; The compiler complains about them - keep them anyway!
235 (file (abbreviate-file-name
236 (or (buffer-file-name (buffer-base-buffer))
237 (error "No file associated to buffer"))))
238 (olpath (mapconcat 'identity
(org-get-outline-path) "/"))
239 (time (format-time-string
240 (substring (cdr org-time-stamp-formats
) 1 -
1)
242 category todo priority ltags itags atags
243 ;; end of variables that will be used for saving context
244 location afile heading buffer level newfile-p infile-p visiting
245 datetree-date datetree-subheading-p
)
247 ;; Find the local archive location
248 (setq location
(org-get-local-archive-location)
249 afile
(org-extract-archive-file location
)
250 heading
(org-extract-archive-heading location
)
251 infile-p
(equal file
(abbreviate-file-name (or afile
""))))
253 (error "Invalid `org-archive-location'"))
255 (if (> (length afile
) 0)
256 (setq newfile-p
(not (file-exists-p afile
))
257 visiting
(find-buffer-visiting afile
)
258 buffer
(or visiting
(find-file-noselect afile
)))
259 (setq buffer
(current-buffer)))
261 (error "Cannot access file \"%s\"" afile
))
262 (when (string-match "\\`datetree/" heading
)
263 ;; Replace with ***, to represent the 3 levels of headings the
265 (setq heading
(replace-regexp-in-string "\\`datetree/" "***" heading
))
266 (setq datetree-subheading-p
(> (length heading
) 3))
267 (setq datetree-date
(org-date-to-gregorian
268 (or (org-entry-get nil
"CLOSED" t
) time
))))
269 (if (and (> (length heading
) 0)
270 (string-match "^\\*+" heading
))
271 (setq level
(match-end 0))
272 (setq heading nil level
0))
274 (org-back-to-heading t
)
275 ;; Get context information that will be lost by moving the tree
276 (setq category
(org-get-category nil
'force-refresh
)
277 todo
(and (looking-at org-todo-line-regexp
)
279 priority
(org-get-priority
280 (if (match-end 3) (match-string 3) ""))
282 itags
(org-delete-all ltags
(org-get-tags-at))
283 atags
(org-get-tags-at))
284 (setq ltags
(mapconcat 'identity ltags
" ")
285 itags
(mapconcat 'identity itags
" "))
286 ;; We first only copy, in case something goes wrong
287 ;; we need to protect `this-command', to avoid kill-region sets it,
288 ;; which would lead to duplication of subtrees
289 (let (this-command) (org-copy-subtree 1 nil t
))
291 ;; Enforce org-mode for the archive buffer
292 (if (not (derived-mode-p 'org-mode
))
293 ;; Force the mode for future visits.
294 (let ((org-insert-mode-line-in-empty-file t
)
295 (org-inhibit-startup t
))
296 (call-interactively 'org-mode
)))
297 (when (and newfile-p org-archive-file-header-format
)
298 (goto-char (point-max))
299 (insert (format org-archive-file-header-format
300 (buffer-file-name this-buffer
))))
302 (require 'org-datetree
)
303 (org-datetree-find-date-create datetree-date
)
304 (org-narrow-to-subtree))
305 ;; Force the TODO keywords of the original buffer
306 (let ((org-todo-line-regexp tr-org-todo-line-regexp
)
307 (org-todo-keywords-1 tr-org-todo-keywords-1
)
308 (org-todo-kwd-alist tr-org-todo-kwd-alist
)
309 (org-done-keywords tr-org-done-keywords
)
310 (org-todo-regexp tr-org-todo-regexp
)
311 (org-todo-line-regexp tr-org-todo-line-regexp
)
313 (if (local-variable-p 'org-odd-levels-only
(current-buffer))
315 tr-org-odd-levels-only
)))
316 (goto-char (point-min))
318 (if (and heading
(not (and datetree-date
(not datetree-subheading-p
))))
320 (if (re-search-forward
321 (concat "^" (regexp-quote heading
)
322 (org-re "[ \t]*\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\($\\|\r\\)"))
324 (goto-char (match-end 0))
325 ;; Heading not found, just insert it at the end
326 (goto-char (point-max))
327 (or (bolp) (insert "\n"))
328 ;; datetrees don't need too much spacing
329 (insert (if datetree-date
"" "\n") heading
"\n")
331 ;; Make the subtree visible
333 (if org-archive-reversed-order
335 (org-back-to-heading t
)
336 (outline-next-heading))
337 (org-end-of-subtree t
))
338 (skip-chars-backward " \t\r\n")
339 (and (looking-at "[ \t\r\n]*")
340 ;; datetree archives don't need so much spacing.
341 (replace-match (if datetree-date
"\n" "\n\n"))))
342 ;; No specific heading, just go to end of file.
343 (goto-char (point-max)) (unless datetree-date
(insert "\n")))
345 (org-paste-subtree (org-get-valid-level level
(and heading
1)))
346 ;; Shall we append inherited tags?
348 (or (and (eq org-archive-subtree-add-inherited-tags
'infile
)
350 (eq org-archive-subtree-add-inherited-tags t
))
351 (org-set-tags-to atags
))
352 ;; Mark the entry as done
353 (when (and org-archive-mark-done
354 (looking-at org-todo-line-regexp
)
355 (or (not (match-end 2))
356 (not (member (match-string 2) org-done-keywords
))))
357 (let (org-log-done org-todo-log-states
)
359 (car (or (member org-archive-mark-done org-done-keywords
)
360 org-done-keywords
)))))
362 ;; Add the context info
363 (when org-archive-save-context-info
364 (let ((l org-archive-save-context-info
) e n v
)
365 (while (setq e
(pop l
))
366 (when (and (setq v
(symbol-value e
))
367 (stringp v
) (string-match "\\S-" v
))
368 (setq n
(concat "ARCHIVE_" (upcase (symbol-name e
))))
369 (org-entry-put (point) n v
)))))
372 ;; Save and kill the buffer, if it is not the same buffer.
373 (when (not (eq this-buffer buffer
))
375 ;; Here we are back in the original buffer. Everything seems
376 ;; to have worked. So now run hooks, cut the tree and finish
378 (run-hooks 'org-archive-hook
)
379 (let (this-command) (org-cut-subtree))
380 (when (featurep 'org-inlinetask
)
381 (org-inlinetask-remove-END-maybe))
382 (setq org-markers-to-move nil
)
383 (message "Subtree archived %s"
384 (if (eq this-buffer buffer
)
385 (concat "under heading: " heading
)
386 (concat "in file: " (abbreviate-file-name afile
))))))
388 (if (looking-at "^[ \t]*$")
389 (outline-next-visible-heading 1))))
392 (defun org-archive-to-archive-sibling ()
393 "Archive the current heading by moving it under the archive sibling.
394 The archive sibling is a sibling of the heading with the heading name
395 `org-archive-sibling-heading' and an `org-archive-tag' tag. If this
396 sibling does not exist, it will be created at the end of the subtree."
398 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region
)
399 (let ((cl (when (eq org-loop-over-headlines-in-active-region
'start-level
)
400 'region-start-level
'region
))
401 org-loop-over-headlines-in-active-region
)
403 '(progn (setq org-map-continue-from
404 (progn (org-back-to-heading)
405 (if (looking-at (concat "^.*:" org-archive-tag
":.*$"))
406 (org-end-of-subtree t
)
408 (when (org-at-heading-p)
409 (org-archive-to-archive-sibling)))
410 org-loop-over-headlines-in-active-region
411 cl
(if (outline-invisible-p) (org-end-of-subtree nil t
))))
414 (let (b e pos leader level
)
415 (org-back-to-heading t
)
416 (looking-at org-outline-regexp
)
417 (setq leader
(match-string 0)
418 level
(funcall outline-level
))
421 (outline-up-heading 1 t
)
422 (error (setq e
(point-max)) (goto-char (point-min))))
426 (org-end-of-subtree t t
)
427 (error (goto-char (point-max))))
430 (unless (re-search-forward
431 (concat "^" (regexp-quote leader
)
433 org-archive-sibling-heading
435 org-archive-tag
":") e t
)
437 (or (bolp) (newline))
438 (insert leader org-archive-sibling-heading
"\n")
439 (beginning-of-line 0)
440 (org-toggle-tag org-archive-tag
'on
))
441 (beginning-of-line 1)
442 (if org-archive-reversed-order
443 (outline-next-heading)
444 (org-end-of-subtree t t
))
447 (let ((this-command this-command
)) (org-cut-subtree)))
448 (org-paste-subtree (org-get-valid-level level
1))
452 (substring (cdr org-time-stamp-formats
) 1 -
1)
454 (outline-up-heading 1 t
)
456 (org-cycle-show-empty-lines 'folded
)
459 (if (looking-at "^[ \t]*$")
460 (outline-next-visible-heading 1))))
462 (defun org-archive-all-done (&optional tag
)
463 "Archive sublevels of the current tree without open TODO items.
464 If the cursor is not on a headline, try all level 1 trees. If
465 it is on a headline, try all direct children.
466 When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
467 (let ((re org-not-done-heading-regexp
) re1
468 (rea (concat ".*:" org-archive-tag
":"))
471 (question (if tag
"Set ARCHIVE tag (no open TODO items)? "
472 "Move subtree to archive (no open TODO items)? "))
474 (if (org-at-heading-p)
476 (setq re1
(concat "^" (regexp-quote
478 (+ (- (match-end 0) (match-beginning 0) 1)
479 (if org-odd-levels-only
2 1))
482 (move-marker begm
(point))
483 (move-marker endm
(org-end-of-subtree t
)))
485 (move-marker begm
(point-min))
486 (move-marker endm
(point-max)))
489 (while (re-search-forward re1 endm t
)
490 (setq beg
(match-beginning 0)
491 end
(save-excursion (org-end-of-subtree t
) (point)))
493 (if (re-search-forward re end t
)
496 (if (and (or (not tag
) (not (looking-at rea
)))
500 (org-toggle-tag org-archive-tag
'on
)
501 (org-archive-subtree))
502 (setq cntarch
(1+ cntarch
)))
504 (message "%d trees archived" cntarch
)))
507 (defun org-toggle-archive-tag (&optional find-done
)
508 "Toggle the archive tag for the current headline.
509 With prefix ARG, check all children of current headline and offer tagging
510 the children that do not contain any open TODO items."
512 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region
)
513 (let ((cl (if (eq org-loop-over-headlines-in-active-region
'start-level
)
514 'region-start-level
'region
))
515 org-loop-over-headlines-in-active-region
)
517 `(org-toggle-archive-tag ,find-done
)
518 org-loop-over-headlines-in-active-region
519 cl
(if (outline-invisible-p) (org-end-of-subtree nil t
))))
521 (org-archive-all-done 'tag
)
524 (org-back-to-heading t
)
525 (setq set
(org-toggle-tag org-archive-tag
))
526 (when set
(hide-subtree)))
527 (and set
(beginning-of-line 1))
528 (message "Subtree %s" (if set
"archived" "unarchived"))))))
530 (defun org-archive-set-tag ()
531 "Set the ARCHIVE tag."
533 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region
)
534 (let ((cl (if (eq org-loop-over-headlines-in-active-region
'start-level
)
535 'region-start-level
'region
))
536 org-loop-over-headlines-in-active-region
)
539 org-loop-over-headlines-in-active-region
540 cl
(if (outline-invisible-p) (org-end-of-subtree nil t
))))
541 (org-toggle-tag org-archive-tag
'on
)))
544 (defun org-archive-subtree-default ()
545 "Archive the current subtree with the default command.
546 This command is set with the variable `org-archive-default-command'."
548 (call-interactively org-archive-default-command
))
551 (defun org-archive-subtree-default-with-confirmation ()
552 "Archive the current subtree with the default command.
553 This command is set with the variable `org-archive-default-command'."
555 (if (y-or-n-p "Archive this subtree or entry? ")
556 (call-interactively org-archive-default-command
)
559 (provide 'org-archive
)
562 ;; generated-autoload-file: "org-loaddefs.el"
565 ;;; org-archive.el ends here