Release 6.30d
[org-mode.git] / lisp / org-archive.el
blobeb288d54b68a8b16b7d86accbc68e59ab33da80f
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
9 ;; Version: 6.30d
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;; Commentary:
29 ;; This file contains the face definitions for Org.
31 ;;; Code:
33 (require 'org)
35 (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
37 (defcustom org-archive-sibling-heading "Archive"
38 "Name of the local archive sibling that is used to archive entries locally.
39 Locally means: in the tree, under a sibling.
40 See `org-archive-to-archive-sibling' for more information."
41 :group 'org-archive
42 :type 'string)
44 (defcustom org-archive-mark-done t
45 "Non-nil means, mark entries as DONE when they are moved to the archive file.
46 This can be a string to set the keyword to use. When t, Org-mode will
47 use the first keyword in its list that means done."
48 :group 'org-archive
49 :type '(choice
50 (const :tag "No" nil)
51 (const :tag "Yes" t)
52 (string :tag "Use this keyword")))
54 (defcustom org-archive-stamp-time t
55 "Non-nil means, add a time stamp to entries moved to an archive file.
56 This variable is obsolete and has no effect anymore, instead add or remove
57 `time' from the variable `org-archive-save-context-info'."
58 :group 'org-archive
59 :type 'boolean)
61 (defcustom org-archive-save-context-info '(time file olpath category todo itags)
62 "Parts of context info that should be stored as properties when archiving.
63 When a subtree is moved to an archive file, it loses information given by
64 context, like inherited tags, the category, and possibly also the TODO
65 state (depending on the variable `org-archive-mark-done').
66 This variable can be a list of any of the following symbols:
68 time The time of archiving.
69 file The file where the entry originates.
70 ltags The local tags, in the headline of the subtree.
71 itags The tags the subtree inherits from further up the hierarchy.
72 todo The pre-archive TODO state.
73 category The category, taken from file name or #+CATEGORY lines.
74 olpath The outline path to the item. These are all headlines above
75 the current item, separated by /, like a file path.
77 For each symbol present in the list, a property will be created in
78 the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
79 information."
80 :group 'org-archive
81 :type '(set :greedy t
82 (const :tag "Time" time)
83 (const :tag "File" file)
84 (const :tag "Category" category)
85 (const :tag "TODO state" todo)
86 (const :tag "Priority" priority)
87 (const :tag "Inherited tags" itags)
88 (const :tag "Outline path" olpath)
89 (const :tag "Local tags" ltags)))
91 (defun org-get-local-archive-location ()
92 "Get the archive location applicable at point."
93 (let ((re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
94 prop)
95 (save-excursion
96 (save-restriction
97 (widen)
98 (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
99 (cond
100 ((and prop (string-match "\\S-" prop))
101 prop)
102 ((or (re-search-backward re nil t)
103 (re-search-forward re nil t))
104 (match-string 1))
105 (t org-archive-location (match-string 1)))))))
107 (defun org-add-archive-files (files)
108 "Splice the archive files into the list of files.
109 This implies visiting all these files and finding out what the
110 archive file is."
111 (org-uniquify
112 (apply
113 'append
114 (mapcar
115 (lambda (f)
116 (if (not (file-exists-p f))
118 (with-current-buffer (org-get-agenda-file-buffer f)
119 (cons f (org-all-archive-files)))))
120 files))))
122 (defun org-all-archive-files ()
123 "Get a list of all archive files used in the current buffer."
124 (let (file files)
125 (save-excursion
126 (save-restriction
127 (goto-char (point-min))
128 (while (re-search-forward
129 "^\\(#\\+\\|[ \t]*:\\)ARCHIVE:[ \t]+\\(.*\\)"
130 nil t)
131 (setq file (org-extract-archive-file
132 (org-match-string-no-properties 2)))
133 (and file (> (length file) 0) (file-exists-p file)
134 (add-to-list 'files file)))))
135 (setq files (nreverse files))
136 (setq file (org-extract-archive-file))
137 (and file (> (length file) 0) (file-exists-p file)
138 (add-to-list 'files file))
139 files))
141 (defun org-extract-archive-file (&optional location)
142 "Extract and expand the file name from archive LOCATION.
143 if LOCATION is not given, the value of `org-archive-location' is used."
144 (setq location (or location org-archive-location))
145 (if (string-match "\\(.*\\)::\\(.*\\)" location)
146 (if (= (match-beginning 1) (match-end 1))
147 (buffer-file-name)
148 (expand-file-name
149 (format (match-string 1 location)
150 (file-name-nondirectory buffer-file-name))))))
152 (defun org-extract-archive-heading (&optional location)
153 "Extract the heading from archive LOCATION.
154 if LOCATION is not given, the value of `org-archive-location' is used."
155 (setq location (or location org-archive-location))
156 (if (string-match "\\(.*\\)::\\(.*\\)" location)
157 (format (match-string 2 location)
158 (file-name-nondirectory buffer-file-name))))
160 (defun org-archive-subtree (&optional find-done)
161 "Move the current subtree to the archive.
162 The archive can be a certain top-level heading in the current file, or in
163 a different file. The tree will be moved to that location, the subtree
164 heading be marked DONE, and the current time will be added.
166 When called with prefix argument FIND-DONE, find whole trees without any
167 open TODO items and archive them (after getting confirmation from the user).
168 If the cursor is not at a headline when this command is called, try all level
169 1 trees. If the cursor is on a headline, only try the direct children of
170 this heading."
171 (interactive "P")
172 (if find-done
173 (org-archive-all-done)
174 ;; Save all relevant TODO keyword-relatex variables
176 (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
177 (tr-org-todo-keywords-1 org-todo-keywords-1)
178 (tr-org-todo-kwd-alist org-todo-kwd-alist)
179 (tr-org-done-keywords org-done-keywords)
180 (tr-org-todo-regexp org-todo-regexp)
181 (tr-org-todo-line-regexp org-todo-line-regexp)
182 (tr-org-odd-levels-only org-odd-levels-only)
183 (this-buffer (current-buffer))
184 ;; start of variables that will be used for saving context
185 ;; The compiler complains about them - keep them anyway!
186 (file (abbreviate-file-name (buffer-file-name)))
187 (olpath (mapconcat 'identity (org-get-outline-path) "/"))
188 (time (format-time-string
189 (substring (cdr org-time-stamp-formats) 1 -1)
190 (current-time)))
191 category todo priority ltags itags
192 ;; end of variables that will be used for saving context
193 location afile heading buffer level newfile-p visiting)
195 ;; Find the local archive location
196 (setq location (org-get-local-archive-location)
197 afile (org-extract-archive-file location)
198 heading (org-extract-archive-heading location))
199 (unless afile
200 (error "Invalid `org-archive-location'"))
202 (if (> (length afile) 0)
203 (setq newfile-p (not (file-exists-p afile))
204 visiting (find-buffer-visiting afile)
205 buffer (or visiting (find-file-noselect afile)))
206 (setq buffer (current-buffer)))
207 (unless buffer
208 (error "Cannot access file \"%s\"" afile))
209 (if (and (> (length heading) 0)
210 (string-match "^\\*+" heading))
211 (setq level (match-end 0))
212 (setq heading nil level 0))
213 (save-excursion
214 (org-back-to-heading t)
215 ;; Get context information that will be lost by moving the tree
216 (org-refresh-category-properties)
217 (setq category (org-get-category)
218 todo (and (looking-at org-todo-line-regexp)
219 (match-string 2))
220 priority (org-get-priority
221 (if (match-end 3) (match-string 3) ""))
222 ltags (org-get-tags)
223 itags (org-delete-all ltags (org-get-tags-at)))
224 (setq ltags (mapconcat 'identity ltags " ")
225 itags (mapconcat 'identity itags " "))
226 ;; We first only copy, in case something goes wrong
227 ;; we need to protect `this-command', to avoid kill-region sets it,
228 ;; which would lead to duplication of subtrees
229 (let (this-command) (org-copy-subtree 1 nil t))
230 (set-buffer buffer)
231 ;; Enforce org-mode for the archive buffer
232 (if (not (org-mode-p))
233 ;; Force the mode for future visits.
234 (let ((org-insert-mode-line-in-empty-file t)
235 (org-inhibit-startup t))
236 (call-interactively 'org-mode)))
237 (when newfile-p
238 (goto-char (point-max))
239 (insert (format "\nArchived entries from file %s\n\n"
240 (buffer-file-name this-buffer))))
241 ;; Force the TODO keywords of the original buffer
242 (let ((org-todo-line-regexp tr-org-todo-line-regexp)
243 (org-todo-keywords-1 tr-org-todo-keywords-1)
244 (org-todo-kwd-alist tr-org-todo-kwd-alist)
245 (org-done-keywords tr-org-done-keywords)
246 (org-todo-regexp tr-org-todo-regexp)
247 (org-todo-line-regexp tr-org-todo-line-regexp)
248 (org-odd-levels-only
249 (if (local-variable-p 'org-odd-levels-only (current-buffer))
250 org-odd-levels-only
251 tr-org-odd-levels-only)))
252 (goto-char (point-min))
253 (show-all)
254 (if heading
255 (progn
256 (if (re-search-forward
257 (concat "^" (regexp-quote heading)
258 (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
259 nil t)
260 (goto-char (match-end 0))
261 ;; Heading not found, just insert it at the end
262 (goto-char (point-max))
263 (or (bolp) (insert "\n"))
264 (insert "\n" heading "\n")
265 (end-of-line 0))
266 ;; Make the subtree visible
267 (show-subtree)
268 (org-end-of-subtree t)
269 (skip-chars-backward " \t\r\n")
270 (and (looking-at "[ \t\r\n]*")
271 (replace-match "\n\n")))
272 ;; No specific heading, just go to end of file.
273 (goto-char (point-max)) (insert "\n"))
274 ;; Paste
275 (org-paste-subtree (org-get-valid-level level (and heading 1)))
277 ;; Mark the entry as done
278 (when (and org-archive-mark-done
279 (looking-at org-todo-line-regexp)
280 (or (not (match-end 2))
281 (not (member (match-string 2) org-done-keywords))))
282 (let (org-log-done org-todo-log-states)
283 (org-todo
284 (car (or (member org-archive-mark-done org-done-keywords)
285 org-done-keywords)))))
287 ;; Add the context info
288 (when org-archive-save-context-info
289 (let ((l org-archive-save-context-info) e n v)
290 (while (setq e (pop l))
291 (when (and (setq v (symbol-value e))
292 (stringp v) (string-match "\\S-" v))
293 (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
294 (org-entry-put (point) n v)))))
296 ;; Save and kill the buffer, if it is not the same buffer.
297 (when (not (eq this-buffer buffer))
298 (save-buffer)
299 ;; Check if it is OK to kill the buffer
300 (unless
301 (or visiting
302 (equal (marker-buffer org-clock-marker) (current-buffer)))
303 (kill-buffer buffer)))
305 ;; Here we are back in the original buffer. Everything seems to have
306 ;; worked. So now cut the tree and finish up.
307 (let (this-command) (org-cut-subtree))
308 (when (featurep 'org-inlinetask)
309 (org-inlinetask-remove-END-maybe))
310 (setq org-markers-to-move nil)
311 (message "Subtree archived %s"
312 (if (eq this-buffer buffer)
313 (concat "under heading: " heading)
314 (concat "in file: " (abbreviate-file-name afile))))))
315 (org-reveal)
316 (if (looking-at "^[ \t]*$")
317 (outline-next-visible-heading 1)))
319 (defun org-archive-to-archive-sibling ()
320 "Archive the current heading by moving it under the archive sibling.
321 The archive sibling is a sibling of the heading with the heading name
322 `org-archive-sibling-heading' and an `org-archive-tag' tag. If this
323 sibling does not exist, it will be created at the end of the subtree."
324 (interactive)
325 (save-restriction
326 (widen)
327 (let (b e pos leader level)
328 (org-back-to-heading t)
329 (looking-at outline-regexp)
330 (setq leader (match-string 0)
331 level (funcall outline-level))
332 (setq pos (point))
333 (condition-case nil
334 (outline-up-heading 1 t)
335 (error (setq e (point-max)) (goto-char (point-min))))
336 (setq b (point))
337 (unless e
338 (condition-case nil
339 (org-end-of-subtree t t)
340 (error (goto-char (point-max))))
341 (setq e (point)))
342 (goto-char b)
343 (unless (re-search-forward
344 (concat "^" (regexp-quote leader)
345 "[ \t]*"
346 org-archive-sibling-heading
347 "[ \t]*:"
348 org-archive-tag ":") e t)
349 (goto-char e)
350 (or (bolp) (newline))
351 (insert leader org-archive-sibling-heading "\n")
352 (beginning-of-line 0)
353 (org-toggle-tag org-archive-tag 'on))
354 (beginning-of-line 1)
355 (org-end-of-subtree t t)
356 (save-excursion
357 (goto-char pos)
358 (let ((this-command this-command)) (org-cut-subtree)))
359 (org-paste-subtree (org-get-valid-level level 1))
360 (org-set-property
361 "ARCHIVE_TIME"
362 (format-time-string
363 (substring (cdr org-time-stamp-formats) 1 -1)
364 (current-time)))
365 (outline-up-heading 1 t)
366 (hide-subtree)
367 (org-cycle-show-empty-lines 'folded)
368 (goto-char pos)))
369 (org-reveal)
370 (if (looking-at "^[ \t]*$")
371 (outline-next-visible-heading 1)))
373 (defun org-archive-all-done (&optional tag)
374 "Archive sublevels of the current tree without open TODO items.
375 If the cursor is not on a headline, try all level 1 trees. If
376 it is on a headline, try all direct children.
377 When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
378 (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
379 (rea (concat ".*:" org-archive-tag ":"))
380 (begm (make-marker))
381 (endm (make-marker))
382 (question (if tag "Set ARCHIVE tag (no open TODO items)? "
383 "Move subtree to archive (no open TODO items)? "))
384 beg end (cntarch 0))
385 (if (org-on-heading-p)
386 (progn
387 (setq re1 (concat "^" (regexp-quote
388 (make-string
389 (1+ (- (match-end 0) (match-beginning 0) 1))
390 ?*))
391 " "))
392 (move-marker begm (point))
393 (move-marker endm (org-end-of-subtree t)))
394 (setq re1 "^* ")
395 (move-marker begm (point-min))
396 (move-marker endm (point-max)))
397 (save-excursion
398 (goto-char begm)
399 (while (re-search-forward re1 endm t)
400 (setq beg (match-beginning 0)
401 end (save-excursion (org-end-of-subtree t) (point)))
402 (goto-char beg)
403 (if (re-search-forward re end t)
404 (goto-char end)
405 (goto-char beg)
406 (if (and (or (not tag) (not (looking-at rea)))
407 (y-or-n-p question))
408 (progn
409 (if tag
410 (org-toggle-tag org-archive-tag 'on)
411 (org-archive-subtree))
412 (setq cntarch (1+ cntarch)))
413 (goto-char end)))))
414 (message "%d trees archived" cntarch)))
416 (defun org-toggle-archive-tag (&optional find-done)
417 "Toggle the archive tag for the current headline.
418 With prefix ARG, check all children of current headline and offer tagging
419 the children that do not contain any open TODO items."
420 (interactive "P")
421 (if find-done
422 (org-archive-all-done 'tag)
423 (let (set)
424 (save-excursion
425 (org-back-to-heading t)
426 (setq set (org-toggle-tag org-archive-tag))
427 (when set (hide-subtree)))
428 (and set (beginning-of-line 1))
429 (message "Subtree %s" (if set "archived" "unarchived")))))
431 (provide 'org-archive)
433 ;; arch-tag: 0837f601-9699-43c3-8b90-631572ae6c85
435 ;;; org-archive.el ends here