1 ;;; org-archive.el --- Archiving for Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
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 (defcustom org-archive-sibling-heading
"Archive"
36 "Name of the local archive sibling that is used to archive entries locally.
37 Locally means: in the tree, under a sibling.
38 See `org-archive-to-archive-sibling' for more information."
42 (defcustom org-archive-mark-done t
43 "Non-nil means, mark entries as DONE when they are moved to the archive file.
44 This can be a string to set the keyword to use. When t, Org-mode will
45 use the first keyword in its list that means done."
50 (string :tag
"Use this keyword")))
52 (defcustom org-archive-stamp-time t
53 "Non-nil means, add a time stamp to entries moved to an archive file.
54 This variable is obsolete and has no effect anymore, instead add or remove
55 `time' from the variable `org-archive-save-context-info'."
59 (defcustom org-archive-save-context-info
'(time file olpath category todo itags
)
60 "Parts of context info that should be stored as properties when archiving.
61 When a subtree is moved to an archive file, it loses information given by
62 context, like inherited tags, the category, and possibly also the TODO
63 state (depending on the variable `org-archive-mark-done').
64 This variable can be a list of any of the following symbols:
66 time The time of archiving.
67 file The file where the entry originates.
68 ltags The local tags, in the headline of the subtree.
69 itags The tags the subtree inherits from further up the hierarchy.
70 todo The pre-archive TODO state.
71 category The category, taken from file name or #+CATEGORY lines.
72 olpath The outline path to the item. These are all headlines above
73 the current item, separated by /, like a file path.
75 For each symbol present in the list, a property will be created in
76 the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
80 (const :tag
"Time" time
)
81 (const :tag
"File" file
)
82 (const :tag
"Category" category
)
83 (const :tag
"TODO state" todo
)
84 (const :tag
"Priority" priority
)
85 (const :tag
"Inherited tags" itags
)
86 (const :tag
"Outline path" olpath
)
87 (const :tag
"Local tags" ltags
)))
89 (defun org-get-local-archive-location ()
90 "Get the archive location applicable at point."
91 (let ((re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
96 (setq prop
(org-entry-get nil
"ARCHIVE" 'inherit
))
98 ((and prop
(string-match "\\S-" prop
))
100 ((or (re-search-backward re nil t
)
101 (re-search-forward re nil t
))
103 (t org-archive-location
(match-string 1)))))))
105 (defun org-add-archive-files (files)
106 "Splice the archive files into the list of files.
107 This implies visiting all these files and finding out what the
114 (if (not (file-exists-p f
))
116 (with-current-buffer (org-get-agenda-file-buffer f
)
117 (cons f
(org-all-archive-files)))))
120 (defun org-all-archive-files ()
121 "Get a list of all archive files used in the current buffer."
125 (goto-char (point-min))
126 (while (re-search-forward
127 "^\\(#\\+\\|[ \t]*:\\)ARCHIVE:[ \t]+\\(.*\\)"
129 (setq file
(org-extract-archive-file
130 (org-match-string-no-properties 2)))
131 (and file
(> (length file
) 0) (file-exists-p file
)
132 (add-to-list 'files file
)))))
133 (setq files
(nreverse files
))
134 (setq file
(org-extract-archive-file))
135 (and file
(> (length file
) 0) (file-exists-p file
)
136 (add-to-list 'files file
))
139 (defun org-extract-archive-file (&optional location
)
140 "Extract and expand the file name from archive LOCATION.
141 if LOCATION is not given, the value of `org-archive-location' is used."
142 (setq location
(or location org-archive-location
))
143 (if (string-match "\\(.*\\)::\\(.*\\)" location
)
144 (if (= (match-beginning 1) (match-end 1))
147 (format (match-string 1 location
)
148 (file-name-nondirectory buffer-file-name
))))))
150 (defun org-extract-archive-heading (&optional location
)
151 "Extract the heading from archive LOCATION.
152 if LOCATION is not given, the value of `org-archive-location' is used."
153 (setq location
(or location org-archive-location
))
154 (if (string-match "\\(.*\\)::\\(.*\\)" location
)
155 (format (match-string 2 location
)
156 (file-name-nondirectory buffer-file-name
))))
158 (defun org-archive-subtree (&optional find-done
)
159 "Move the current subtree to the archive.
160 The archive can be a certain top-level heading in the current file, or in
161 a different file. The tree will be moved to that location, the subtree
162 heading be marked DONE, and the current time will be added.
164 When called with prefix argument FIND-DONE, find whole trees without any
165 open TODO items and archive them (after getting confirmation from the user).
166 If the cursor is not at a headline when this command is called, try all level
167 1 trees. If the cursor is on a headline, only try the direct children of
171 (org-archive-all-done)
172 ;; Save all relevant TODO keyword-relatex variables
174 (let ((tr-org-todo-line-regexp org-todo-line-regexp
) ; keep despite compiler
175 (tr-org-todo-keywords-1 org-todo-keywords-1
)
176 (tr-org-todo-kwd-alist org-todo-kwd-alist
)
177 (tr-org-done-keywords org-done-keywords
)
178 (tr-org-todo-regexp org-todo-regexp
)
179 (tr-org-todo-line-regexp org-todo-line-regexp
)
180 (tr-org-odd-levels-only org-odd-levels-only
)
181 (this-buffer (current-buffer))
182 ;; start of variables that will be used for saving context
183 ;; The compiler complains about them - keep them anyway!
184 (file (abbreviate-file-name (buffer-file-name)))
185 (olpath (mapconcat 'identity
(org-get-outline-path) "/"))
186 (time (format-time-string
187 (substring (cdr org-time-stamp-formats
) 1 -
1)
189 category todo priority ltags itags
190 ;; end of variables that will be used for saving context
191 location afile heading buffer level newfile-p visiting
)
193 ;; Find the local archive location
194 (setq location
(org-get-local-archive-location)
195 afile
(org-extract-archive-file location
)
196 heading
(org-extract-archive-heading location
))
198 (error "Invalid `org-archive-location'"))
200 (if (> (length afile
) 0)
201 (setq newfile-p
(not (file-exists-p afile
))
202 visiting
(find-buffer-visiting afile
)
203 buffer
(or visiting
(find-file-noselect afile
)))
204 (setq buffer
(current-buffer)))
206 (error "Cannot access file \"%s\"" afile
))
207 (if (and (> (length heading
) 0)
208 (string-match "^\\*+" heading
))
209 (setq level
(match-end 0))
210 (setq heading nil level
0))
212 (org-back-to-heading t
)
213 ;; Get context information that will be lost by moving the tree
214 (org-refresh-category-properties)
215 (setq category
(org-get-category)
216 todo
(and (looking-at org-todo-line-regexp
)
218 priority
(org-get-priority
219 (if (match-end 3) (match-string 3) ""))
221 itags
(org-delete-all ltags
(org-get-tags-at)))
222 (setq ltags
(mapconcat 'identity ltags
" ")
223 itags
(mapconcat 'identity itags
" "))
224 ;; We first only copy, in case something goes wrong
225 ;; we need to protect `this-command', to avoid kill-region sets it,
226 ;; which would lead to duplication of subtrees
227 (let (this-command) (org-copy-subtree 1 nil t
))
229 ;; Enforce org-mode for the archive buffer
230 (if (not (org-mode-p))
231 ;; Force the mode for future visits.
232 (let ((org-insert-mode-line-in-empty-file t
)
233 (org-inhibit-startup t
))
234 (call-interactively 'org-mode
)))
236 (goto-char (point-max))
237 (insert (format "\nArchived entries from file %s\n\n"
238 (buffer-file-name this-buffer
))))
239 ;; Force the TODO keywords of the original buffer
240 (let ((org-todo-line-regexp tr-org-todo-line-regexp
)
241 (org-todo-keywords-1 tr-org-todo-keywords-1
)
242 (org-todo-kwd-alist tr-org-todo-kwd-alist
)
243 (org-done-keywords tr-org-done-keywords
)
244 (org-todo-regexp tr-org-todo-regexp
)
245 (org-todo-line-regexp tr-org-todo-line-regexp
)
247 (if (local-variable-p 'org-odd-levels-only
(current-buffer))
249 tr-org-odd-levels-only
)))
250 (goto-char (point-min))
254 (if (re-search-forward
255 (concat "^" (regexp-quote heading
)
256 (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
258 (goto-char (match-end 0))
259 ;; Heading not found, just insert it at the end
260 (goto-char (point-max))
261 (or (bolp) (insert "\n"))
262 (insert "\n" heading
"\n")
264 ;; Make the subtree visible
266 (org-end-of-subtree t
)
267 (skip-chars-backward " \t\r\n")
268 (and (looking-at "[ \t\r\n]*")
269 (replace-match "\n\n")))
270 ;; No specific heading, just go to end of file.
271 (goto-char (point-max)) (insert "\n"))
273 (org-paste-subtree (org-get-valid-level level
(and heading
1)))
275 ;; Mark the entry as done
276 (when (and org-archive-mark-done
277 (looking-at org-todo-line-regexp
)
278 (or (not (match-end 2))
279 (not (member (match-string 2) org-done-keywords
))))
280 (let (org-log-done org-todo-log-states
)
282 (car (or (member org-archive-mark-done org-done-keywords
)
283 org-done-keywords
)))))
285 ;; Add the context info
286 (when org-archive-save-context-info
287 (let ((l org-archive-save-context-info
) e n v
)
288 (while (setq e
(pop l
))
289 (when (and (setq v
(symbol-value e
))
290 (stringp v
) (string-match "\\S-" v
))
291 (setq n
(concat "ARCHIVE_" (upcase (symbol-name e
))))
292 (org-entry-put (point) n v
)))))
294 ;; Save and kill the buffer, if it is not the same buffer.
295 (when (not (eq this-buffer buffer
))
297 ;; Check if it is OK to kill the buffer
300 (equal (marker-buffer org-clock-marker
) (current-buffer)))
301 (kill-buffer buffer
)))
303 ;; Here we are back in the original buffer. Everything seems to have
304 ;; worked. So now cut the tree and finish up.
305 (let (this-command) (org-cut-subtree))
306 (setq org-markers-to-move nil
)
307 (message "Subtree archived %s"
308 (if (eq this-buffer buffer
)
309 (concat "under heading: " heading
)
310 (concat "in file: " (abbreviate-file-name afile
))))))
313 (defun org-archive-to-archive-sibling ()
314 "Archive the current heading by moving it under the archive sibling.
315 The archive sibling is a sibling of the heading with the heading name
316 `org-archive-sibling-heading' and an `org-archive-tag' tag. If this
317 sibling does not exist, it will be created at the end of the subtree."
321 (let (b e pos leader level
)
322 (org-back-to-heading t
)
323 (looking-at outline-regexp
)
324 (setq leader
(match-string 0)
325 level
(funcall outline-level
))
328 (outline-up-heading 1 t
)
329 (error (setq e
(point-max)) (goto-char (point-min))))
333 (org-end-of-subtree t t
)
334 (error (goto-char (point-max))))
337 (unless (re-search-forward
338 (concat "^" (regexp-quote leader
)
340 org-archive-sibling-heading
342 org-archive-tag
":") e t
)
344 (or (bolp) (newline))
345 (insert leader org-archive-sibling-heading
"\n")
346 (beginning-of-line 0)
347 (org-toggle-tag org-archive-tag
'on
))
348 (beginning-of-line 1)
349 (org-end-of-subtree t t
)
352 (let ((this-command this-command
)) (org-cut-subtree)))
353 (org-paste-subtree (org-get-valid-level level
1))
357 (substring (cdr org-time-stamp-formats
) 1 -
1)
359 (outline-up-heading 1 t
)
361 (org-cycle-show-empty-lines 'folded
)
365 (defun org-archive-all-done (&optional tag
)
366 "Archive sublevels of the current tree without open TODO items.
367 If the cursor is not on a headline, try all level 1 trees. If
368 it is on a headline, try all direct children.
369 When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
370 (let ((re (concat "^\\*+ +" org-not-done-regexp
)) re1
371 (rea (concat ".*:" org-archive-tag
":"))
374 (question (if tag
"Set ARCHIVE tag (no open TODO items)? "
375 "Move subtree to archive (no open TODO items)? "))
377 (if (org-on-heading-p)
379 (setq re1
(concat "^" (regexp-quote
381 (1+ (- (match-end 0) (match-beginning 0) 1))
384 (move-marker begm
(point))
385 (move-marker endm
(org-end-of-subtree t
)))
387 (move-marker begm
(point-min))
388 (move-marker endm
(point-max)))
391 (while (re-search-forward re1 endm t
)
392 (setq beg
(match-beginning 0)
393 end
(save-excursion (org-end-of-subtree t
) (point)))
395 (if (re-search-forward re end t
)
398 (if (and (or (not tag
) (not (looking-at rea
)))
402 (org-toggle-tag org-archive-tag
'on
)
403 (org-archive-subtree))
404 (setq cntarch
(1+ cntarch
)))
406 (message "%d trees archived" cntarch
)))
408 (defun org-toggle-archive-tag (&optional find-done
)
409 "Toggle the archive tag for the current headline.
410 With prefix ARG, check all children of current headline and offer tagging
411 the children that do not contain any open TODO items."
414 (org-archive-all-done 'tag
)
417 (org-back-to-heading t
)
418 (setq set
(org-toggle-tag org-archive-tag
))
419 (when set
(hide-subtree)))
420 (and set
(beginning-of-line 1))
421 (message "Subtree %s" (if set
"archived" "unarchived")))))
423 (provide 'org-archive
)
425 ;; arch-tag: 0837f601-9699-43c3-8b90-631572ae6c85
427 ;;; org-archive.el ends here