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 (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-sibling-heading
"Archive"
46 "Name of the local archive sibling that is used to archive entries locally.
47 Locally means: in the tree, under a sibling.
48 See `org-archive-to-archive-sibling' for more information."
52 (defcustom org-archive-mark-done nil
53 "Non-nil means, mark entries as DONE when they are moved to the archive file.
54 This can be a string to set the keyword to use. When t, Org-mode will
55 use the first keyword in its list that means done."
60 (string :tag
"Use this keyword")))
62 (defcustom org-archive-stamp-time t
63 "Non-nil means, add a time stamp to entries moved to an archive file.
64 This variable is obsolete and has no effect anymore, instead add or remove
65 `time' from the variable `org-archive-save-context-info'."
69 (defcustom org-archive-save-context-info
'(time file olpath category todo itags
)
70 "Parts of context info that should be stored as properties when archiving.
71 When a subtree is moved to an archive file, it loses information given by
72 context, like inherited tags, the category, and possibly also the TODO
73 state (depending on the variable `org-archive-mark-done').
74 This variable can be a list of any of the following symbols:
76 time The time of archiving.
77 file The file where the entry originates.
78 ltags The local tags, in the headline of the subtree.
79 itags The tags the subtree inherits from further up the hierarchy.
80 todo The pre-archive TODO state.
81 category The category, taken from file name or #+CATEGORY lines.
82 olpath The outline path to the item. These are all headlines above
83 the current item, separated by /, like a file path.
85 For each symbol present in the list, a property will be created in
86 the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
90 (const :tag
"Time" time
)
91 (const :tag
"File" file
)
92 (const :tag
"Category" category
)
93 (const :tag
"TODO state" todo
)
94 (const :tag
"Priority" priority
)
95 (const :tag
"Inherited tags" itags
)
96 (const :tag
"Outline path" olpath
)
97 (const :tag
"Local tags" ltags
)))
99 (defun org-get-local-archive-location ()
100 "Get the archive location applicable at point."
101 (let ((re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
106 (setq prop
(org-entry-get nil
"ARCHIVE" 'inherit
))
108 ((and prop
(string-match "\\S-" prop
))
110 ((or (re-search-backward re nil t
)
111 (re-search-forward re nil t
))
113 (t org-archive-location
(match-string 1)))))))
115 (defun org-add-archive-files (files)
116 "Splice the archive files into the list of files.
117 This implies visiting all these files and finding out what the
124 (if (not (file-exists-p f
))
126 (with-current-buffer (org-get-agenda-file-buffer f
)
127 (cons f
(org-all-archive-files)))))
130 (defun org-all-archive-files ()
131 "Get a list of all archive files used in the current buffer."
135 (goto-char (point-min))
136 (while (re-search-forward
137 "^\\(#\\+\\|[ \t]*:\\)ARCHIVE:[ \t]+\\(.*\\)"
139 (setq file
(org-extract-archive-file
140 (org-match-string-no-properties 2)))
141 (and file
(> (length file
) 0) (file-exists-p file
)
142 (add-to-list 'files file
)))))
143 (setq files
(nreverse files
))
144 (setq file
(org-extract-archive-file))
145 (and file
(> (length file
) 0) (file-exists-p file
)
146 (add-to-list 'files file
))
149 (defun org-extract-archive-file (&optional location
)
150 "Extract and expand the file name from archive LOCATION.
151 if LOCATION is not given, the value of `org-archive-location' is used."
152 (setq location
(or location org-archive-location
))
153 (if (string-match "\\(.*\\)::\\(.*\\)" location
)
154 (if (= (match-beginning 1) (match-end 1))
157 (format (match-string 1 location
)
158 (file-name-nondirectory buffer-file-name
))))))
160 (defun org-extract-archive-heading (&optional location
)
161 "Extract the heading 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 (format (match-string 2 location
)
166 (file-name-nondirectory buffer-file-name
))))
168 (defun org-archive-subtree (&optional find-done
)
169 "Move the current subtree to the archive.
170 The archive can be a certain top-level heading in the current file, or in
171 a different file. The tree will be moved to that location, the subtree
172 heading be marked DONE, and the current time will be added.
174 When called with prefix argument FIND-DONE, find whole trees without any
175 open TODO items and archive them (after getting confirmation from the user).
176 If the cursor is not at a headline when this command is called, try all level
177 1 trees. If the cursor is on a headline, only try the direct children of
181 (org-archive-all-done)
182 ;; Save all relevant TODO keyword-relatex variables
184 (let ((tr-org-todo-line-regexp org-todo-line-regexp
) ; keep despite compiler
185 (tr-org-todo-keywords-1 org-todo-keywords-1
)
186 (tr-org-todo-kwd-alist org-todo-kwd-alist
)
187 (tr-org-done-keywords org-done-keywords
)
188 (tr-org-todo-regexp org-todo-regexp
)
189 (tr-org-todo-line-regexp org-todo-line-regexp
)
190 (tr-org-odd-levels-only org-odd-levels-only
)
191 (this-buffer (current-buffer))
192 ;; start of variables that will be used for saving context
193 ;; The compiler complains about them - keep them anyway!
194 (file (abbreviate-file-name (buffer-file-name)))
195 (olpath (mapconcat 'identity
(org-get-outline-path) "/"))
196 (time (format-time-string
197 (substring (cdr org-time-stamp-formats
) 1 -
1)
199 category todo priority ltags itags
200 ;; end of variables that will be used for saving context
201 location afile heading buffer level newfile-p visiting
)
203 ;; Find the local archive location
204 (setq location
(org-get-local-archive-location)
205 afile
(org-extract-archive-file location
)
206 heading
(org-extract-archive-heading location
))
208 (error "Invalid `org-archive-location'"))
210 (if (> (length afile
) 0)
211 (setq newfile-p
(not (file-exists-p afile
))
212 visiting
(find-buffer-visiting afile
)
213 buffer
(or visiting
(find-file-noselect afile
)))
214 (setq buffer
(current-buffer)))
216 (error "Cannot access file \"%s\"" afile
))
217 (if (and (> (length heading
) 0)
218 (string-match "^\\*+" heading
))
219 (setq level
(match-end 0))
220 (setq heading nil level
0))
222 (org-back-to-heading t
)
223 ;; Get context information that will be lost by moving the tree
224 (org-refresh-category-properties)
225 (setq category
(org-get-category)
226 todo
(and (looking-at org-todo-line-regexp
)
228 priority
(org-get-priority
229 (if (match-end 3) (match-string 3) ""))
231 itags
(org-delete-all ltags
(org-get-tags-at)))
232 (setq ltags
(mapconcat 'identity ltags
" ")
233 itags
(mapconcat 'identity itags
" "))
234 ;; We first only copy, in case something goes wrong
235 ;; we need to protect `this-command', to avoid kill-region sets it,
236 ;; which would lead to duplication of subtrees
237 (let (this-command) (org-copy-subtree 1 nil t
))
239 ;; Enforce org-mode for the archive buffer
240 (if (not (org-mode-p))
241 ;; Force the mode for future visits.
242 (let ((org-insert-mode-line-in-empty-file t
)
243 (org-inhibit-startup t
))
244 (call-interactively 'org-mode
)))
246 (goto-char (point-max))
247 (insert (format "\nArchived entries from file %s\n\n"
248 (buffer-file-name this-buffer
))))
249 ;; Force the TODO keywords of the original buffer
250 (let ((org-todo-line-regexp tr-org-todo-line-regexp
)
251 (org-todo-keywords-1 tr-org-todo-keywords-1
)
252 (org-todo-kwd-alist tr-org-todo-kwd-alist
)
253 (org-done-keywords tr-org-done-keywords
)
254 (org-todo-regexp tr-org-todo-regexp
)
255 (org-todo-line-regexp tr-org-todo-line-regexp
)
257 (if (local-variable-p 'org-odd-levels-only
(current-buffer))
259 tr-org-odd-levels-only
)))
260 (goto-char (point-min))
264 (if (re-search-forward
265 (concat "^" (regexp-quote heading
)
266 (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
268 (goto-char (match-end 0))
269 ;; Heading not found, just insert it at the end
270 (goto-char (point-max))
271 (or (bolp) (insert "\n"))
272 (insert "\n" heading
"\n")
274 ;; Make the subtree visible
276 (org-end-of-subtree t
)
277 (skip-chars-backward " \t\r\n")
278 (and (looking-at "[ \t\r\n]*")
279 (replace-match "\n\n")))
280 ;; No specific heading, just go to end of file.
281 (goto-char (point-max)) (insert "\n"))
283 (org-paste-subtree (org-get-valid-level level
(and heading
1)))
285 ;; Mark the entry as done
286 (when (and org-archive-mark-done
287 (looking-at org-todo-line-regexp
)
288 (or (not (match-end 2))
289 (not (member (match-string 2) org-done-keywords
))))
290 (let (org-log-done org-todo-log-states
)
292 (car (or (member org-archive-mark-done org-done-keywords
)
293 org-done-keywords
)))))
295 ;; Add the context info
296 (when org-archive-save-context-info
297 (let ((l org-archive-save-context-info
) e n v
)
298 (while (setq e
(pop l
))
299 (when (and (setq v
(symbol-value e
))
300 (stringp v
) (string-match "\\S-" v
))
301 (setq n
(concat "ARCHIVE_" (upcase (symbol-name e
))))
302 (org-entry-put (point) n v
)))))
304 ;; Save and kill the buffer, if it is not the same buffer.
305 (when (not (eq this-buffer buffer
))
308 ;; Here we are back in the original buffer. Everything seems to have
309 ;; worked. So now cut the tree and finish up.
310 (let (this-command) (org-cut-subtree))
311 (when (featurep 'org-inlinetask
)
312 (org-inlinetask-remove-END-maybe))
313 (setq org-markers-to-move nil
)
314 (message "Subtree archived %s"
315 (if (eq this-buffer buffer
)
316 (concat "under heading: " heading
)
317 (concat "in file: " (abbreviate-file-name afile
))))))
319 (if (looking-at "^[ \t]*$")
320 (outline-next-visible-heading 1)))
322 (defun org-archive-to-archive-sibling ()
323 "Archive the current heading by moving it under the archive sibling.
324 The archive sibling is a sibling of the heading with the heading name
325 `org-archive-sibling-heading' and an `org-archive-tag' tag. If this
326 sibling does not exist, it will be created at the end of the subtree."
330 (let (b e pos leader level
)
331 (org-back-to-heading t
)
332 (looking-at outline-regexp
)
333 (setq leader
(match-string 0)
334 level
(funcall outline-level
))
337 (outline-up-heading 1 t
)
338 (error (setq e
(point-max)) (goto-char (point-min))))
342 (org-end-of-subtree t t
)
343 (error (goto-char (point-max))))
346 (unless (re-search-forward
347 (concat "^" (regexp-quote leader
)
349 org-archive-sibling-heading
351 org-archive-tag
":") e t
)
353 (or (bolp) (newline))
354 (insert leader org-archive-sibling-heading
"\n")
355 (beginning-of-line 0)
356 (org-toggle-tag org-archive-tag
'on
))
357 (beginning-of-line 1)
358 (org-end-of-subtree t t
)
361 (let ((this-command this-command
)) (org-cut-subtree)))
362 (org-paste-subtree (org-get-valid-level level
1))
366 (substring (cdr org-time-stamp-formats
) 1 -
1)
368 (outline-up-heading 1 t
)
370 (org-cycle-show-empty-lines 'folded
)
373 (if (looking-at "^[ \t]*$")
374 (outline-next-visible-heading 1)))
376 (defun org-archive-all-done (&optional tag
)
377 "Archive sublevels of the current tree without open TODO items.
378 If the cursor is not on a headline, try all level 1 trees. If
379 it is on a headline, try all direct children.
380 When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
381 (let ((re (concat "^\\*+ +" org-not-done-regexp
)) re1
382 (rea (concat ".*:" org-archive-tag
":"))
385 (question (if tag
"Set ARCHIVE tag (no open TODO items)? "
386 "Move subtree to archive (no open TODO items)? "))
388 (if (org-on-heading-p)
390 (setq re1
(concat "^" (regexp-quote
392 (+ (- (match-end 0) (match-beginning 0) 1)
393 (if org-odd-levels-only
2 1))
396 (move-marker begm
(point))
397 (move-marker endm
(org-end-of-subtree t
)))
399 (move-marker begm
(point-min))
400 (move-marker endm
(point-max)))
403 (while (re-search-forward re1 endm t
)
404 (setq beg
(match-beginning 0)
405 end
(save-excursion (org-end-of-subtree t
) (point)))
407 (if (re-search-forward re end t
)
410 (if (and (or (not tag
) (not (looking-at rea
)))
414 (org-toggle-tag org-archive-tag
'on
)
415 (org-archive-subtree))
416 (setq cntarch
(1+ cntarch
)))
418 (message "%d trees archived" cntarch
)))
420 (defun org-toggle-archive-tag (&optional find-done
)
421 "Toggle the archive tag for the current headline.
422 With prefix ARG, check all children of current headline and offer tagging
423 the children that do not contain any open TODO items."
426 (org-archive-all-done 'tag
)
429 (org-back-to-heading t
)
430 (setq set
(org-toggle-tag org-archive-tag
))
431 (when set
(hide-subtree)))
432 (and set
(beginning-of-line 1))
433 (message "Subtree %s" (if set
"archived" "unarchived")))))
435 (defun org-archive-set-tag ()
436 "Set the ARCHIVE tag."
438 (org-toggle-tag org-archive-tag
'on
))
441 (defun org-archive-subtree-default ()
442 "Archive the current subtree with the default command.
443 This command is set with the variable `org-archive-default-command'."
445 (call-interactively org-archive-default-command
))
448 (defun org-archive-subtree-default-with-confirmation ()
449 "Archive the current subtree with the default command.
450 This command is set with the variable `org-archive-default-command'."
452 (if (y-or-n-p "Archive this subtree or entry? ")
453 (call-interactively org-archive-default-command
)
456 (provide 'org-archive
)
458 ;; arch-tag: 0837f601-9699-43c3-8b90-631572ae6c85
460 ;;; org-archive.el ends here