1 ;;; org-archive.el --- Archiving for Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;; This file contains the face definitons for Org.
34 (defcustom org-archive-sibling-heading
"Archive"
35 "Name of the local archive sibling that is used to archive entries locally.
36 Locally means: in the tree, under a sibling.
37 See `org-archive-to-archive-sibling' for more information."
41 (defcustom org-archive-mark-done t
42 "Non-nil means, mark entries as DONE when they are moved to the archive file.
43 This can be a string to set the keyword to use. When t, Org-mode will
44 use the first keyword in its list that means done."
49 (string :tag
"Use this keyword")))
51 (defcustom org-archive-stamp-time t
52 "Non-nil means, add a time stamp to entries moved to an archive file.
53 This variable is obsolete and has no effect anymore, instead add or remove
54 `time' from the variable `org-archive-save-context-info'."
58 (defcustom org-archive-save-context-info
'(time file olpath category todo itags
)
59 "Parts of context info that should be stored as properties when archiving.
60 When a subtree is moved to an archive file, it loses information given by
61 context, like inherited tags, the category, and possibly also the TODO
62 state (depending on the variable `org-archive-mark-done').
63 This variable can be a list of any of the following symbols:
65 time The time of archiving.
66 file The file where the entry originates.
67 ltags The local tags, in the headline of the subtree.
68 itags The tags the subtree inherits from further up the hierarchy.
69 todo The pre-archive TODO state.
70 category The category, taken from file name or #+CATEGORY lines.
71 olpath The outline path to the item. These are all headlines above
72 the current item, separated by /, like a file path.
74 For each symbol present in the list, a property will be created in
75 the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
79 (const :tag
"Time" time
)
80 (const :tag
"File" file
)
81 (const :tag
"Category" category
)
82 (const :tag
"TODO state" todo
)
83 (const :tag
"Priority" priority
)
84 (const :tag
"Inherited tags" itags
)
85 (const :tag
"Outline path" olpath
)
86 (const :tag
"Local tags" ltags
)))
88 (defun org-get-local-archive-location ()
89 "Get the archive location applicable at point."
90 (let ((re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
95 (setq prop
(org-entry-get nil
"ARCHIVE" 'inherit
))
97 ((and prop
(string-match "\\S-" prop
))
99 ((or (re-search-backward re nil t
)
100 (re-search-forward re nil t
))
102 (t org-archive-location
(match-string 1)))))))
104 (defun org-add-archive-files (files)
105 "Splice the archive files into the list f files.
106 This implies visiting all these files and finding out what the
112 (if (not (file-exists-p f
))
114 (with-current-buffer (org-get-agenda-file-buffer f
)
115 (cons f
(org-all-archive-files)))))
118 (defun org-all-archive-files ()
119 "Get a list of all archive files used in the current buffer."
123 (goto-char (point-min))
124 (while (re-search-forward
125 "^\\(#\\+\\|[ \t]*:\\)ARCHIVE:[ \t]+\\(.*\\)"
127 (setq file
(org-extract-archive-file
128 (org-match-string-no-properties 2)))
129 (and file
(> (length file
) 0) (file-exists-p file
)
130 (add-to-list 'files file
)))))
131 (setq files
(nreverse files
))
132 (setq file
(org-extract-archive-file))
133 (and file
(> (length file
) 0) (file-exists-p file
)
134 (add-to-list 'files file
))
137 (defun org-extract-archive-file (&optional location
)
138 "Extract and expand the file name from archive LOCATION.
139 if LOCATION is not given, the value of `org-archive-location' is used."
140 (setq location
(or location org-archive-location
))
141 (if (string-match "\\(.*\\)::\\(.*\\)" location
)
142 (if (= (match-beginning 1) (match-end 1))
145 (format (match-string 1 location
)
146 (file-name-nondirectory buffer-file-name
))))))
148 (defun org-extract-archive-heading (&optional location
)
149 "Extract the heading from archive LOCATION.
150 if LOCATION is not given, the value of `org-archive-location' is used."
151 (setq location
(or location org-archive-location
))
152 (if (string-match "\\(.*\\)::\\(.*\\)" location
)
153 (match-string 2 location
)))
155 (defun org-archive-subtree (&optional find-done
)
156 "Move the current subtree to the archive.
157 The archive can be a certain top-level heading in the current file, or in
158 a different file. The tree will be moved to that location, the subtree
159 heading be marked DONE, and the current time will be added.
161 When called with prefix argument FIND-DONE, find whole trees without any
162 open TODO items and archive them (after getting confirmation from the user).
163 If the cursor is not at a headline when this comand is called, try all level
164 1 trees. If the cursor is on a headline, only try the direct children of
168 (org-archive-all-done)
169 ;; Save all relevant TODO keyword-relatex variables
171 (let ((tr-org-todo-line-regexp org-todo-line-regexp
) ; keep despite compiler
172 (tr-org-todo-keywords-1 org-todo-keywords-1
)
173 (tr-org-todo-kwd-alist org-todo-kwd-alist
)
174 (tr-org-done-keywords org-done-keywords
)
175 (tr-org-todo-regexp org-todo-regexp
)
176 (tr-org-todo-line-regexp org-todo-line-regexp
)
177 (tr-org-odd-levels-only org-odd-levels-only
)
178 (this-buffer (current-buffer))
179 ;; start of variables that will be used for saving context
180 ;; The compiler complains about them - keep them anyway!
181 (file (abbreviate-file-name (buffer-file-name)))
182 (olpath (mapconcat 'identity
(org-get-outline-path) "/"))
183 (time (format-time-string
184 (substring (cdr org-time-stamp-formats
) 1 -
1)
186 category todo priority ltags itags
187 ;; end of variables that will be used for saving context
188 location afile heading buffer level newfile-p visiting
)
190 ;; Find the local archive location
191 (setq location
(org-get-local-archive-location)
192 afile
(org-extract-archive-file location
)
193 heading
(org-extract-archive-heading location
))
195 (error "Invalid `org-archive-location'"))
197 (if (> (length afile
) 0)
198 (setq newfile-p
(not (file-exists-p afile
))
199 visiting
(find-buffer-visiting afile
)
200 buffer
(or visiting
(find-file-noselect afile
)))
201 (setq buffer
(current-buffer)))
203 (error "Cannot access file \"%s\"" afile
))
204 (if (and (> (length heading
) 0)
205 (string-match "^\\*+" heading
))
206 (setq level
(match-end 0))
207 (setq heading nil level
0))
209 (org-back-to-heading t
)
210 ;; Get context information that will be lost by moving the tree
211 (org-refresh-category-properties)
212 (setq category
(org-get-category)
213 todo
(and (looking-at org-todo-line-regexp
)
215 priority
(org-get-priority
216 (if (match-end 3) (match-string 3) ""))
218 itags
(org-delete-all ltags
(org-get-tags-at)))
219 (setq ltags
(mapconcat 'identity ltags
" ")
220 itags
(mapconcat 'identity itags
" "))
221 ;; We first only copy, in case something goes wrong
222 ;; we need to protect `this-command', to avoid kill-region sets it,
223 ;; which would lead to duplication of subtrees
224 (let (this-command) (org-copy-subtree 1 nil t
))
226 ;; Enforce org-mode for the archive buffer
227 (if (not (org-mode-p))
228 ;; Force the mode for future visits.
229 (let ((org-insert-mode-line-in-empty-file t
)
230 (org-inhibit-startup t
))
231 (call-interactively 'org-mode
)))
233 (goto-char (point-max))
234 (insert (format "\nArchived entries from file %s\n\n"
235 (buffer-file-name this-buffer
))))
236 ;; Force the TODO keywords of the original buffer
237 (let ((org-todo-line-regexp tr-org-todo-line-regexp
)
238 (org-todo-keywords-1 tr-org-todo-keywords-1
)
239 (org-todo-kwd-alist tr-org-todo-kwd-alist
)
240 (org-done-keywords tr-org-done-keywords
)
241 (org-todo-regexp tr-org-todo-regexp
)
242 (org-todo-line-regexp tr-org-todo-line-regexp
)
244 (if (local-variable-p 'org-odd-levels-only
(current-buffer))
246 tr-org-odd-levels-only
)))
247 (goto-char (point-min))
251 (if (re-search-forward
252 (concat "^" (regexp-quote heading
)
253 (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
255 (goto-char (match-end 0))
256 ;; Heading not found, just insert it at the end
257 (goto-char (point-max))
258 (or (bolp) (insert "\n"))
259 (insert "\n" heading
"\n")
261 ;; Make the subtree visible
263 (org-end-of-subtree t
)
264 (skip-chars-backward " \t\r\n")
265 (and (looking-at "[ \t\r\n]*")
266 (replace-match "\n\n")))
267 ;; No specific heading, just go to end of file.
268 (goto-char (point-max)) (insert "\n"))
270 (org-paste-subtree (org-get-valid-level level
1))
272 ;; Mark the entry as done
273 (when (and org-archive-mark-done
274 (looking-at org-todo-line-regexp
)
275 (or (not (match-end 2))
276 (not (member (match-string 2) org-done-keywords
))))
277 (let (org-log-done org-todo-log-states
)
279 (car (or (member org-archive-mark-done org-done-keywords
)
280 org-done-keywords
)))))
282 ;; Add the context info
283 (when org-archive-save-context-info
284 (let ((l org-archive-save-context-info
) e n v
)
285 (while (setq e
(pop l
))
286 (when (and (setq v
(symbol-value e
))
287 (stringp v
) (string-match "\\S-" v
))
288 (setq n
(concat "ARCHIVE_" (upcase (symbol-name e
))))
289 (org-entry-put (point) n v
)))))
291 ;; Save and kill the buffer, if it is not the same buffer.
292 (when (not (eq this-buffer buffer
))
294 ;; Check if it is OK to kill the buffer
297 (equal (marker-buffer org-clock-marker
) (current-buffer)))
298 (kill-buffer buffer
)))
300 ;; Here we are back in the original buffer. Everything seems to have
301 ;; worked. So now cut the tree and finish up.
302 (let (this-command) (org-cut-subtree))
303 (setq org-markers-to-move nil
)
304 (message "Subtree archived %s"
305 (if (eq this-buffer buffer
)
306 (concat "under heading: " heading
)
307 (concat "in file: " (abbreviate-file-name afile
)))))))
309 (defun org-archive-to-archive-sibling ()
310 "Archive the current heading by moving it under the archive sibling.
311 The archive sibling is a sibling of the heading with the heading name
312 `org-archive-sibling-heading' and an `org-archive-tag' tag. If this
313 sibling does not exist, it will be created at the end of the subtree."
317 (let (b e pos leader level
)
318 (org-back-to-heading t
)
319 (looking-at outline-regexp
)
320 (setq leader
(match-string 0)
321 level
(funcall outline-level
))
324 (outline-up-heading 1 t
)
325 (error (setq e
(point-max)) (goto-char (point-min))))
329 (org-end-of-subtree t t
)
330 (error (goto-char (point-max))))
333 (unless (re-search-forward
334 (concat "^" (regexp-quote leader
)
336 org-archive-sibling-heading
338 org-archive-tag
":") e t
)
340 (or (bolp) (newline))
341 (insert leader org-archive-sibling-heading
"\n")
342 (beginning-of-line 0)
343 (org-toggle-tag org-archive-tag
'on
))
344 (beginning-of-line 1)
345 (org-end-of-subtree t t
)
349 (org-paste-subtree (org-get-valid-level level
1))
353 (substring (cdr org-time-stamp-formats
) 1 -
1)
355 (outline-up-heading 1 t
)
357 (org-cycle-show-empty-lines 'folded
)
360 (defun org-archive-all-done (&optional tag
)
361 "Archive sublevels of the current tree without open TODO items.
362 If the cursor is not on a headline, try all level 1 trees. If
363 it is on a headline, try all direct children.
364 When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
365 (let ((re (concat "^\\*+ +" org-not-done-regexp
)) re1
366 (rea (concat ".*:" org-archive-tag
":"))
369 (question (if tag
"Set ARCHIVE tag (no open TODO items)? "
370 "Move subtree to archive (no open TODO items)? "))
372 (if (org-on-heading-p)
374 (setq re1
(concat "^" (regexp-quote
376 (1+ (- (match-end 0) (match-beginning 0) 1))
379 (move-marker begm
(point))
380 (move-marker endm
(org-end-of-subtree t
)))
382 (move-marker begm
(point-min))
383 (move-marker endm
(point-max)))
386 (while (re-search-forward re1 endm t
)
387 (setq beg
(match-beginning 0)
388 end
(save-excursion (org-end-of-subtree t
) (point)))
390 (if (re-search-forward re end t
)
393 (if (and (or (not tag
) (not (looking-at rea
)))
397 (org-toggle-tag org-archive-tag
'on
)
398 (org-archive-subtree))
399 (setq cntarch
(1+ cntarch
)))
401 (message "%d trees archived" cntarch
)))
403 (defun org-toggle-archive-tag (&optional find-done
)
404 "Toggle the archive tag for the current headline.
405 With prefix ARG, check all children of current headline and offer tagging
406 the children that do not contain any open TODO items."
409 (org-archive-all-done 'tag
)
412 (org-back-to-heading t
)
413 (setq set
(org-toggle-tag org-archive-tag
))
414 (when set
(hide-subtree)))
415 (and set
(beginning-of-line 1))
416 (message "Subtree %s" (if set
"archived" "unarchived")))))
418 (provide 'org-archive
)
420 ;; arch-tag: 0837f601-9699-43c3-8b90-631572ae6c85
422 ;;; org-archive.el ends here