1 ;;; org-archive.el --- Archiving for Org-mode
3 ;; Copyright (C) 2004-2011 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" ())
35 (defcustom org-archive-default-command
'org-archive-subtree
36 "The default archiving command."
39 (const org-archive-subtree
)
40 (const org-archive-to-archive-sibling
)
41 (const org-archive-set-tag
)))
43 (defcustom org-archive-reversed-order nil
44 "Non-nil means make the tree first child under the archive heading, not last."
48 (defcustom org-archive-sibling-heading
"Archive"
49 "Name of the local archive sibling that is used to archive entries locally.
50 Locally means: in the tree, under a sibling.
51 See `org-archive-to-archive-sibling' for more information."
55 (defcustom org-archive-mark-done nil
56 "Non-nil means mark entries as DONE when they are moved to the archive file.
57 This can be a string to set the keyword to use. When t, Org-mode will
58 use the first keyword in its list that means done."
63 (string :tag
"Use this keyword")))
65 (defcustom org-archive-stamp-time t
66 "Non-nil means add a time stamp to entries moved to an archive file.
67 This variable is obsolete and has no effect anymore, instead add or remove
68 `time' from the variable `org-archive-save-context-info'."
72 (defcustom org-archive-subtree-add-inherited-tags
'infile
73 "Non-nil means append inherited tags when archiving a subtree."
76 (const :tag
"Never" nil
)
77 (const :tag
"When archiving a subtree to the same file" infile
)
78 (const :tag
"Always" t
)))
80 (defcustom org-archive-save-context-info
'(time file olpath category todo itags
)
81 "Parts of context info that should be stored as properties when archiving.
82 When a subtree is moved to an archive file, it loses information given by
83 context, like inherited tags, the category, and possibly also the TODO
84 state (depending on the variable `org-archive-mark-done').
85 This variable can be a list of any of the following symbols:
87 time The time of archiving.
88 file The file where the entry originates.
89 ltags The local tags, in the headline of the subtree.
90 itags The tags the subtree inherits from further up the hierarchy.
91 todo The pre-archive TODO state.
92 category The category, taken from file name or #+CATEGORY lines.
93 olpath The outline path to the item. These are all headlines above
94 the current item, separated by /, like a file path.
96 For each symbol present in the list, a property will be created in
97 the archived entry, with a prefix \"ARCHIVE_\", to remember this
100 :type
'(set :greedy t
101 (const :tag
"Time" time
)
102 (const :tag
"File" file
)
103 (const :tag
"Category" category
)
104 (const :tag
"TODO state" todo
)
105 (const :tag
"Priority" priority
)
106 (const :tag
"Inherited tags" itags
)
107 (const :tag
"Outline path" olpath
)
108 (const :tag
"Local tags" ltags
)))
110 (defun org-get-local-archive-location ()
111 "Get the archive location applicable at point."
112 (let ((re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
117 (setq prop
(org-entry-get nil
"ARCHIVE" 'inherit
))
119 ((and prop
(string-match "\\S-" prop
))
121 ((or (re-search-backward re nil t
)
122 (re-search-forward re nil t
))
124 (t org-archive-location
))))))
126 (defun org-add-archive-files (files)
127 "Splice the archive files into the list of files.
128 This implies visiting all these files and finding out what the
135 (if (not (file-exists-p f
))
137 (with-current-buffer (org-get-agenda-file-buffer f
)
138 (cons f
(org-all-archive-files)))))
141 (defun org-all-archive-files ()
142 "Get a list of all archive files used in the current buffer."
146 (goto-char (point-min))
147 (while (re-search-forward
148 "^\\(#\\+\\|[ \t]*:\\)ARCHIVE:[ \t]+\\(.*\\)"
150 (setq file
(org-extract-archive-file
151 (org-match-string-no-properties 2)))
152 (and file
(> (length file
) 0) (file-exists-p file
)
153 (add-to-list 'files file
)))))
154 (setq files
(nreverse files
))
155 (setq file
(org-extract-archive-file))
156 (and file
(> (length file
) 0) (file-exists-p file
)
157 (add-to-list 'files file
))
160 (defun org-extract-archive-file (&optional location
)
161 "Extract and expand the file name from archive LOCATION.
162 if LOCATION is not given, the value of `org-archive-location' is used."
163 (setq location
(or location org-archive-location
))
164 (if (string-match "\\(.*\\)::\\(.*\\)" location
)
165 (if (= (match-beginning 1) (match-end 1))
166 (buffer-file-name (buffer-base-buffer))
168 (format (match-string 1 location
)
169 (file-name-nondirectory
170 (buffer-file-name (buffer-base-buffer))))))))
172 (defun org-extract-archive-heading (&optional location
)
173 "Extract the heading from archive LOCATION.
174 if LOCATION is not given, the value of `org-archive-location' is used."
175 (setq location
(or location org-archive-location
))
176 (if (string-match "\\(.*\\)::\\(.*\\)" location
)
177 (format (match-string 2 location
)
178 (file-name-nondirectory
179 (buffer-file-name (buffer-base-buffer))))))
181 (defun org-archive-subtree (&optional find-done
)
182 "Move the current subtree to the archive.
183 The archive can be a certain top-level heading in the current file, or in
184 a different file. The tree will be moved to that location, the subtree
185 heading be marked DONE, and the current time will be added.
187 When called with prefix argument FIND-DONE, find whole trees without any
188 open TODO items and archive them (after getting confirmation from the user).
189 If the cursor is not at a headline when this command is called, try all level
190 1 trees. If the cursor is on a headline, only try the direct children of
194 (org-archive-all-done)
195 ;; Save all relevant TODO keyword-relatex variables
197 (let ((tr-org-todo-line-regexp org-todo-line-regexp
) ; keep despite compiler
198 (tr-org-todo-keywords-1 org-todo-keywords-1
)
199 (tr-org-todo-kwd-alist org-todo-kwd-alist
)
200 (tr-org-done-keywords org-done-keywords
)
201 (tr-org-todo-regexp org-todo-regexp
)
202 (tr-org-todo-line-regexp org-todo-line-regexp
)
203 (tr-org-odd-levels-only org-odd-levels-only
)
204 (this-buffer (current-buffer))
205 ;; start of variables that will be used for saving context
206 ;; The compiler complains about them - keep them anyway!
207 (file (abbreviate-file-name
208 (or (buffer-file-name (buffer-base-buffer))
209 (error "No file associated to buffer"))))
210 (olpath (mapconcat 'identity
(org-get-outline-path) "/"))
211 (time (format-time-string
212 (substring (cdr org-time-stamp-formats
) 1 -
1)
214 category todo priority ltags itags atags
215 ;; end of variables that will be used for saving context
216 location afile heading buffer level newfile-p infile-p visiting
)
218 ;; Find the local archive location
219 (setq location
(org-get-local-archive-location)
220 afile
(org-extract-archive-file location
)
221 heading
(org-extract-archive-heading location
)
222 infile-p
(equal file
(abbreviate-file-name afile
)))
224 (error "Invalid `org-archive-location'"))
226 (if (> (length afile
) 0)
227 (setq newfile-p
(not (file-exists-p afile
))
228 visiting
(find-buffer-visiting afile
)
229 buffer
(or visiting
(find-file-noselect afile
)))
230 (setq buffer
(current-buffer)))
232 (error "Cannot access file \"%s\"" afile
))
233 (if (and (> (length heading
) 0)
234 (string-match "^\\*+" heading
))
235 (setq level
(match-end 0))
236 (setq heading nil level
0))
238 (org-back-to-heading t
)
239 ;; Get context information that will be lost by moving the tree
240 (setq category
(org-get-category nil
'force-refresh
)
241 todo
(and (looking-at org-todo-line-regexp
)
243 priority
(org-get-priority
244 (if (match-end 3) (match-string 3) ""))
246 itags
(org-delete-all ltags
(org-get-tags-at))
247 atags
(org-get-tags-at))
248 (setq ltags
(mapconcat 'identity ltags
" ")
249 itags
(mapconcat 'identity itags
" "))
250 ;; We first only copy, in case something goes wrong
251 ;; we need to protect `this-command', to avoid kill-region sets it,
252 ;; which would lead to duplication of subtrees
253 (let (this-command) (org-copy-subtree 1 nil t
))
255 ;; Enforce org-mode for the archive buffer
256 (if (not (eq major-mode
'org-mode
))
257 ;; Force the mode for future visits.
258 (let ((org-insert-mode-line-in-empty-file t
)
259 (org-inhibit-startup t
))
260 (call-interactively 'org-mode
)))
262 (goto-char (point-max))
263 (insert (format "\nArchived entries from file %s\n\n"
264 (buffer-file-name this-buffer
))))
265 ;; Force the TODO keywords of the original buffer
266 (let ((org-todo-line-regexp tr-org-todo-line-regexp
)
267 (org-todo-keywords-1 tr-org-todo-keywords-1
)
268 (org-todo-kwd-alist tr-org-todo-kwd-alist
)
269 (org-done-keywords tr-org-done-keywords
)
270 (org-todo-regexp tr-org-todo-regexp
)
271 (org-todo-line-regexp tr-org-todo-line-regexp
)
273 (if (local-variable-p 'org-odd-levels-only
(current-buffer))
275 tr-org-odd-levels-only
)))
276 (goto-char (point-min))
280 (if (re-search-forward
281 (concat "^" (regexp-quote heading
)
282 (org-re "[ \t]*\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\($\\|\r\\)"))
284 (goto-char (match-end 0))
285 ;; Heading not found, just insert it at the end
286 (goto-char (point-max))
287 (or (bolp) (insert "\n"))
288 (insert "\n" heading
"\n")
290 ;; Make the subtree visible
292 (if org-archive-reversed-order
294 (org-back-to-heading t
)
295 (outline-next-heading))
296 (org-end-of-subtree t
))
297 (skip-chars-backward " \t\r\n")
298 (and (looking-at "[ \t\r\n]*")
299 (replace-match "\n\n")))
300 ;; No specific heading, just go to end of file.
301 (goto-char (point-max)) (insert "\n"))
303 (org-paste-subtree (org-get-valid-level level
(and heading
1)))
304 ;; Shall we append inherited tags?
306 (or (and (eq org-archive-subtree-add-inherited-tags
'infile
)
308 (eq org-archive-subtree-add-inherited-tags t
))
309 (org-set-tags-to atags
))
310 ;; Mark the entry as done
311 (when (and org-archive-mark-done
312 (looking-at org-todo-line-regexp
)
313 (or (not (match-end 2))
314 (not (member (match-string 2) org-done-keywords
))))
315 (let (org-log-done org-todo-log-states
)
317 (car (or (member org-archive-mark-done org-done-keywords
)
318 org-done-keywords
)))))
320 ;; Add the context info
321 (when org-archive-save-context-info
322 (let ((l org-archive-save-context-info
) e n v
)
323 (while (setq e
(pop l
))
324 (when (and (setq v
(symbol-value e
))
325 (stringp v
) (string-match "\\S-" v
))
326 (setq n
(concat "ARCHIVE_" (upcase (symbol-name e
))))
327 (org-entry-put (point) n v
)))))
329 ;; Save and kill the buffer, if it is not the same buffer.
330 (when (not (eq this-buffer buffer
))
332 ;; Here we are back in the original buffer. Everything seems to have
333 ;; worked. So now cut the tree and finish up.
334 (let (this-command) (org-cut-subtree))
335 (when (featurep 'org-inlinetask
)
336 (org-inlinetask-remove-END-maybe))
337 (setq org-markers-to-move nil
)
338 (message "Subtree archived %s"
339 (if (eq this-buffer buffer
)
340 (concat "under heading: " heading
)
341 (concat "in file: " (abbreviate-file-name afile
))))))
343 (if (looking-at "^[ \t]*$")
344 (outline-next-visible-heading 1)))
346 (defun org-archive-to-archive-sibling ()
347 "Archive the current heading by moving it under the archive sibling.
348 The archive sibling is a sibling of the heading with the heading name
349 `org-archive-sibling-heading' and an `org-archive-tag' tag. If this
350 sibling does not exist, it will be created at the end of the subtree."
354 (let (b e pos leader level
)
355 (org-back-to-heading t
)
356 (looking-at org-outline-regexp
)
357 (setq leader
(match-string 0)
358 level
(funcall outline-level
))
361 (outline-up-heading 1 t
)
362 (error (setq e
(point-max)) (goto-char (point-min))))
366 (org-end-of-subtree t t
)
367 (error (goto-char (point-max))))
370 (unless (re-search-forward
371 (concat "^" (regexp-quote leader
)
373 org-archive-sibling-heading
375 org-archive-tag
":") e t
)
377 (or (bolp) (newline))
378 (insert leader org-archive-sibling-heading
"\n")
379 (beginning-of-line 0)
380 (org-toggle-tag org-archive-tag
'on
))
381 (beginning-of-line 1)
382 (if org-archive-reversed-order
383 (outline-next-heading)
384 (org-end-of-subtree t t
))
387 (let ((this-command this-command
)) (org-cut-subtree)))
388 (org-paste-subtree (org-get-valid-level level
1))
392 (substring (cdr org-time-stamp-formats
) 1 -
1)
394 (outline-up-heading 1 t
)
396 (org-cycle-show-empty-lines 'folded
)
399 (if (looking-at "^[ \t]*$")
400 (outline-next-visible-heading 1)))
402 (defun org-archive-all-done (&optional tag
)
403 "Archive sublevels of the current tree without open TODO items.
404 If the cursor is not on a headline, try all level 1 trees. If
405 it is on a headline, try all direct children.
406 When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
407 (let ((re org-not-done-heading-regexp
) re1
408 (rea (concat ".*:" org-archive-tag
":"))
411 (question (if tag
"Set ARCHIVE tag (no open TODO items)? "
412 "Move subtree to archive (no open TODO items)? "))
414 (if (org-on-heading-p)
416 (setq re1
(concat "^" (regexp-quote
418 (+ (- (match-end 0) (match-beginning 0) 1)
419 (if org-odd-levels-only
2 1))
422 (move-marker begm
(point))
423 (move-marker endm
(org-end-of-subtree t
)))
425 (move-marker begm
(point-min))
426 (move-marker endm
(point-max)))
429 (while (re-search-forward re1 endm t
)
430 (setq beg
(match-beginning 0)
431 end
(save-excursion (org-end-of-subtree t
) (point)))
433 (if (re-search-forward re end t
)
436 (if (and (or (not tag
) (not (looking-at rea
)))
440 (org-toggle-tag org-archive-tag
'on
)
441 (org-archive-subtree))
442 (setq cntarch
(1+ cntarch
)))
444 (message "%d trees archived" cntarch
)))
446 (defun org-toggle-archive-tag (&optional find-done
)
447 "Toggle the archive tag for the current headline.
448 With prefix ARG, check all children of current headline and offer tagging
449 the children that do not contain any open TODO items."
452 (org-archive-all-done 'tag
)
455 (org-back-to-heading t
)
456 (setq set
(org-toggle-tag org-archive-tag
))
457 (when set
(hide-subtree)))
458 (and set
(beginning-of-line 1))
459 (message "Subtree %s" (if set
"archived" "unarchived")))))
461 (defun org-archive-set-tag ()
462 "Set the ARCHIVE tag."
464 (org-toggle-tag org-archive-tag
'on
))
467 (defun org-archive-subtree-default ()
468 "Archive the current subtree with the default command.
469 This command is set with the variable `org-archive-default-command'."
471 (call-interactively org-archive-default-command
))
474 (defun org-archive-subtree-default-with-confirmation ()
475 "Archive the current subtree with the default command.
476 This command is set with the variable `org-archive-default-command'."
478 (if (y-or-n-p "Archive this subtree or entry? ")
479 (call-interactively org-archive-default-command
)
482 (provide 'org-archive
)
484 ;;; org-archive.el ends here