1 ;;; org-archive.el --- Archiving for Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
29 ;; This file contains the face definitions for Org.
35 (declare-function org-inlinetask-remove-END-maybe
"org-inlinetask" ())
37 (defcustom org-archive-default-command
'org-archive-subtree
38 "The default archiving command."
41 (const org-archive-subtree
)
42 (const org-archive-to-archive-sibling
)
43 (const org-archive-set-tag
)))
45 (defcustom org-archive-reversed-order nil
46 "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-subtree-add-inherited-tags
'infile
75 "Non-nil means append inherited tags when archiving a subtree."
78 (const :tag
"Never" nil
)
79 (const :tag
"When archiving a subtree to the same file" infile
)
80 (const :tag
"Always" t
)))
82 (defcustom org-archive-save-context-info
'(time file olpath category todo itags
)
83 "Parts of context info that should be stored as properties when archiving.
84 When a subtree is moved to an archive file, it loses information given by
85 context, like inherited tags, the category, and possibly also the TODO
86 state (depending on the variable `org-archive-mark-done').
87 This variable can be a list of any of the following symbols:
89 time The time of archiving.
90 file The file where the entry originates.
91 ltags The local tags, in the headline of the subtree.
92 itags The tags the subtree inherits from further up the hierarchy.
93 todo The pre-archive TODO state.
94 category The category, taken from file name or #+CATEGORY lines.
95 olpath The outline path to the item. These are all headlines above
96 the current item, separated by /, like a file path.
98 For each symbol present in the list, a property will be created in
99 the archived entry, with a prefix \"ARCHIVE_\", to remember this
102 :type
'(set :greedy t
103 (const :tag
"Time" time
)
104 (const :tag
"File" file
)
105 (const :tag
"Category" category
)
106 (const :tag
"TODO state" todo
)
107 (const :tag
"Priority" priority
)
108 (const :tag
"Inherited tags" itags
)
109 (const :tag
"Outline path" olpath
)
110 (const :tag
"Local tags" ltags
)))
112 (defun org-get-local-archive-location ()
113 "Get the archive location applicable at point."
114 (let ((re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
119 (setq prop
(org-entry-get nil
"ARCHIVE" 'inherit
))
121 ((and prop
(string-match "\\S-" prop
))
123 ((or (re-search-backward re nil t
)
124 (re-search-forward re nil t
))
126 (t org-archive-location
))))))
128 (defun org-add-archive-files (files)
129 "Splice the archive files into the list of files.
130 This implies visiting all these files and finding out what the
137 (if (not (file-exists-p f
))
139 (with-current-buffer (org-get-agenda-file-buffer f
)
140 (cons f
(org-all-archive-files)))))
143 (defun org-all-archive-files ()
144 "Get a list of all archive files used in the current buffer."
148 (goto-char (point-min))
149 (while (re-search-forward
150 "^\\(#\\+\\|[ \t]*:\\)ARCHIVE:[ \t]+\\(.*\\)"
152 (setq file
(org-extract-archive-file
153 (org-match-string-no-properties 2)))
154 (and file
(> (length file
) 0) (file-exists-p file
)
155 (add-to-list 'files file
)))))
156 (setq files
(nreverse files
))
157 (setq file
(org-extract-archive-file))
158 (and file
(> (length file
) 0) (file-exists-p file
)
159 (add-to-list 'files file
))
162 (defun org-extract-archive-file (&optional location
)
163 "Extract and expand the file name from archive LOCATION.
164 if LOCATION is not given, the value of `org-archive-location' is used."
165 (setq location
(or location org-archive-location
))
166 (if (string-match "\\(.*\\)::\\(.*\\)" location
)
167 (if (= (match-beginning 1) (match-end 1))
168 (buffer-file-name (buffer-base-buffer))
170 (format (match-string 1 location
)
171 (file-name-nondirectory
172 (buffer-file-name (buffer-base-buffer))))))))
174 (defun org-extract-archive-heading (&optional location
)
175 "Extract the heading from archive LOCATION.
176 if LOCATION is not given, the value of `org-archive-location' is used."
177 (setq location
(or location org-archive-location
))
178 (if (string-match "\\(.*\\)::\\(.*\\)" location
)
179 (format (match-string 2 location
)
180 (file-name-nondirectory
181 (buffer-file-name (buffer-base-buffer))))))
183 (defun org-archive-subtree (&optional find-done
)
184 "Move the current subtree to the archive.
185 The archive can be a certain top-level heading in the current file, or in
186 a different file. The tree will be moved to that location, the subtree
187 heading be marked DONE, and the current time will be added.
189 When called with prefix argument FIND-DONE, find whole trees without any
190 open TODO items and archive them (after getting confirmation from the user).
191 If the cursor is not at a headline when this command is called, try all level
192 1 trees. If the cursor is on a headline, only try the direct children of
195 (org-loop-over-siblings-in-active-region
197 (org-archive-all-done)
198 ;; Save all relevant TODO keyword-relatex variables
200 (let ((tr-org-todo-line-regexp org-todo-line-regexp
) ; keep despite compiler
201 (tr-org-todo-keywords-1 org-todo-keywords-1
)
202 (tr-org-todo-kwd-alist org-todo-kwd-alist
)
203 (tr-org-done-keywords org-done-keywords
)
204 (tr-org-todo-regexp org-todo-regexp
)
205 (tr-org-todo-line-regexp org-todo-line-regexp
)
206 (tr-org-odd-levels-only org-odd-levels-only
)
207 (this-buffer (current-buffer))
208 ;; start of variables that will be used for saving context
209 ;; The compiler complains about them - keep them anyway!
210 (file (abbreviate-file-name
211 (or (buffer-file-name (buffer-base-buffer))
212 (error "No file associated to buffer"))))
213 (olpath (mapconcat 'identity
(org-get-outline-path) "/"))
214 (time (format-time-string
215 (substring (cdr org-time-stamp-formats
) 1 -
1)
217 category todo priority ltags itags atags
218 ;; end of variables that will be used for saving context
219 location afile heading buffer level newfile-p infile-p visiting
)
221 ;; Find the local archive location
222 (setq location
(org-get-local-archive-location)
223 afile
(org-extract-archive-file location
)
224 heading
(org-extract-archive-heading location
)
225 infile-p
(equal file
(abbreviate-file-name afile
)))
227 (error "Invalid `org-archive-location'"))
229 (if (> (length afile
) 0)
230 (setq newfile-p
(not (file-exists-p afile
))
231 visiting
(find-buffer-visiting afile
)
232 buffer
(or visiting
(find-file-noselect afile
)))
233 (setq buffer
(current-buffer)))
235 (error "Cannot access file \"%s\"" afile
))
236 (if (and (> (length heading
) 0)
237 (string-match "^\\*+" heading
))
238 (setq level
(match-end 0))
239 (setq heading nil level
0))
241 (org-back-to-heading t
)
242 ;; Get context information that will be lost by moving the tree
243 (setq category
(org-get-category nil
'force-refresh
)
244 todo
(and (looking-at org-todo-line-regexp
)
246 priority
(org-get-priority
247 (if (match-end 3) (match-string 3) ""))
249 itags
(org-delete-all ltags
(org-get-tags-at))
250 atags
(org-get-tags-at))
251 (setq ltags
(mapconcat 'identity ltags
" ")
252 itags
(mapconcat 'identity itags
" "))
253 ;; We first only copy, in case something goes wrong
254 ;; we need to protect `this-command', to avoid kill-region sets it,
255 ;; which would lead to duplication of subtrees
256 (let (this-command) (org-copy-subtree 1 nil t
))
258 ;; Enforce org-mode for the archive buffer
259 (if (not (org-mode-p))
260 ;; Force the mode for future visits.
261 (let ((org-insert-mode-line-in-empty-file t
)
262 (org-inhibit-startup t
))
263 (call-interactively 'org-mode
)))
265 (goto-char (point-max))
266 (insert (format "\nArchived entries from file %s\n\n"
267 (buffer-file-name this-buffer
))))
268 ;; Force the TODO keywords of the original buffer
269 (let ((org-todo-line-regexp tr-org-todo-line-regexp
)
270 (org-todo-keywords-1 tr-org-todo-keywords-1
)
271 (org-todo-kwd-alist tr-org-todo-kwd-alist
)
272 (org-done-keywords tr-org-done-keywords
)
273 (org-todo-regexp tr-org-todo-regexp
)
274 (org-todo-line-regexp tr-org-todo-line-regexp
)
276 (if (local-variable-p 'org-odd-levels-only
(current-buffer))
278 tr-org-odd-levels-only
)))
279 (goto-char (point-min))
283 (if (re-search-forward
284 (concat "^" (regexp-quote heading
)
285 (org-re "[ \t]*\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\($\\|\r\\)"))
287 (goto-char (match-end 0))
288 ;; Heading not found, just insert it at the end
289 (goto-char (point-max))
290 (or (bolp) (insert "\n"))
291 (insert "\n" heading
"\n")
293 ;; Make the subtree visible
295 (if org-archive-reversed-order
297 (org-back-to-heading t
)
298 (outline-next-heading))
299 (org-end-of-subtree t
))
300 (skip-chars-backward " \t\r\n")
301 (and (looking-at "[ \t\r\n]*")
302 (replace-match "\n\n")))
303 ;; No specific heading, just go to end of file.
304 (goto-char (point-max)) (insert "\n"))
306 (org-paste-subtree (org-get-valid-level level
(and heading
1)))
307 ;; Shall we append inherited tags?
309 (or (and (eq org-archive-subtree-add-inherited-tags
'infile
)
311 (eq org-archive-subtree-add-inherited-tags t
))
312 (org-set-tags-to atags
))
313 ;; Mark the entry as done
314 (when (and org-archive-mark-done
315 (looking-at org-todo-line-regexp
)
316 (or (not (match-end 2))
317 (not (member (match-string 2) org-done-keywords
))))
318 (let (org-log-done org-todo-log-states
)
320 (car (or (member org-archive-mark-done org-done-keywords
)
321 org-done-keywords
)))))
323 ;; Add the context info
324 (when org-archive-save-context-info
325 (let ((l org-archive-save-context-info
) e n v
)
326 (while (setq e
(pop l
))
327 (when (and (setq v
(symbol-value e
))
328 (stringp v
) (string-match "\\S-" v
))
329 (setq n
(concat "ARCHIVE_" (upcase (symbol-name e
))))
330 (org-entry-put (point) n v
)))))
332 ;; Save and kill the buffer, if it is not the same buffer.
333 (when (not (eq this-buffer buffer
))
335 ;; Here we are back in the original buffer. Everything seems to have
336 ;; worked. So now cut the tree and finish up.
337 (let (this-command) (org-cut-subtree))
338 (when (featurep 'org-inlinetask
)
339 (org-inlinetask-remove-END-maybe))
340 (setq org-markers-to-move nil
)
341 (message "Subtree archived %s"
342 (if (eq this-buffer buffer
)
343 (concat "under heading: " heading
)
344 (concat "in file: " (abbreviate-file-name afile
))))))
346 (if (looking-at "^[ \t]*$")
347 (outline-next-visible-heading 1))))
349 (defun org-archive-to-archive-sibling ()
350 "Archive the current heading by moving it under the archive sibling.
351 The archive sibling is a sibling of the heading with the heading name
352 `org-archive-sibling-heading' and an `org-archive-tag' tag. If this
353 sibling does not exist, it will be created at the end of the subtree."
355 (org-loop-over-siblings-in-active-region
358 (let (b e pos leader level
)
359 (org-back-to-heading t
)
360 (looking-at outline-regexp
)
361 (setq leader
(match-string 0)
362 level
(funcall outline-level
))
365 (outline-up-heading 1 t
)
366 (error (setq e
(point-max)) (goto-char (point-min))))
370 (org-end-of-subtree t t
)
371 (error (goto-char (point-max))))
374 (unless (re-search-forward
375 (concat "^" (regexp-quote leader
)
377 org-archive-sibling-heading
379 org-archive-tag
":") e t
)
381 (or (bolp) (newline))
382 (insert leader org-archive-sibling-heading
"\n")
383 (beginning-of-line 0)
384 (org-toggle-tag org-archive-tag
'on
))
385 (beginning-of-line 1)
386 (if org-archive-reversed-order
387 (outline-next-heading)
388 (org-end-of-subtree t t
))
391 (let ((this-command this-command
)) (org-cut-subtree)))
392 (org-paste-subtree (org-get-valid-level level
1))
396 (substring (cdr org-time-stamp-formats
) 1 -
1)
398 (outline-up-heading 1 t
)
400 (org-cycle-show-empty-lines 'folded
)
403 (if (looking-at "^[ \t]*$")
404 (outline-next-visible-heading 1))))
406 (defun org-archive-all-done (&optional tag
)
407 "Archive sublevels of the current tree without open TODO items.
408 If the cursor is not on a headline, try all level 1 trees. If
409 it is on a headline, try all direct children.
410 When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
411 (let ((re (concat org-outline-regexp-bol
"+" org-not-done-regexp
)) re1
412 (rea (concat ".*:" org-archive-tag
":"))
415 (question (if tag
"Set ARCHIVE tag (no open TODO items)? "
416 "Move subtree to archive (no open TODO items)? "))
418 (if (org-on-heading-p)
420 (setq re1
(concat "^" (regexp-quote
422 (+ (- (match-end 0) (match-beginning 0) 1)
423 (if org-odd-levels-only
2 1))
426 (move-marker begm
(point))
427 (move-marker endm
(org-end-of-subtree t
)))
429 (move-marker begm
(point-min))
430 (move-marker endm
(point-max)))
433 (while (re-search-forward re1 endm t
)
434 (setq beg
(match-beginning 0)
435 end
(save-excursion (org-end-of-subtree t
) (point)))
437 (if (re-search-forward re end t
)
440 (if (and (or (not tag
) (not (looking-at rea
)))
444 (org-toggle-tag org-archive-tag
'on
)
445 (org-archive-subtree))
446 (setq cntarch
(1+ cntarch
)))
448 (message "%d trees archived" cntarch
)))
450 (defun org-toggle-archive-tag (&optional find-done
)
451 "Toggle the archive tag for the current headline.
452 With prefix ARG, check all children of current headline and offer tagging
453 the children that do not contain any open TODO items."
455 (org-loop-over-siblings-in-active-region
457 (org-archive-all-done 'tag
)
460 (org-back-to-heading t
)
461 (setq set
(org-toggle-tag org-archive-tag
))
462 (when set
(hide-subtree)))
463 (and set
(beginning-of-line 1))
464 (message "Subtree %s" (if set
"archived" "unarchived"))))))
466 (defun org-archive-set-tag ()
467 "Set the ARCHIVE tag."
469 (org-loop-over-siblings-in-active-region
470 (org-toggle-tag org-archive-tag
'on
)))
473 (defun org-archive-subtree-default ()
474 "Archive the current subtree with the default command.
475 This command is set with the variable `org-archive-default-command'."
477 (call-interactively org-archive-default-command
))
480 (defun org-archive-subtree-default-with-confirmation ()
481 "Archive the current subtree with the default command.
482 This command is set with the variable `org-archive-default-command'."
484 (if (y-or-n-p "Archive this subtree or entry? ")
485 (call-interactively org-archive-default-command
)
488 (provide 'org-archive
)
490 ;; arch-tag: 0837f601-9699-43c3-8b90-631572ae6c85
492 ;;; org-archive.el ends here