Release 6.03.
[org-mode/org-tableheadings.git] / lisp / org-archive.el
blob3b1105db626056978fd52db6373b533c8d3c1794
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
8 ;; Version: 6.03
9 ;;
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;; Commentary:
28 ;; This file contains the face definitons for Org.
30 ;;; Code:
32 (require '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."
38 :group 'org-archive
39 :type 'string)
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."
45 :group 'org-archive
46 :type '(choice
47 (const :tag "No" nil)
48 (const :tag "Yes" t)
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'."
55 :group 'org-archive
56 :type 'boolean)
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 itags The local tags, in the headline of the subtree.
68 ltags 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
76 information."
77 :group 'org-archive
78 :type '(set :greedy t
79 (const :tag "Time" time)
80 (const :tag "File" file)
81 (const :tag "Category" category)
82 (const :tag "TODO state" todo)
83 (const :tag "TODO state" 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]*$")
91 prop)
92 (save-excursion
93 (save-restriction
94 (widen)
95 (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
96 (cond
97 ((and prop (string-match "\\S-" prop))
98 prop)
99 ((or (re-search-backward re nil t)
100 (re-search-forward re nil t))
101 (match-string 1))
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
107 archive file is."
108 (apply
109 'append
110 (mapcar
111 (lambda (f)
112 (if (not (file-exists-p f))
114 (with-current-buffer (org-get-agenda-file-buffer f)
115 (cons f (org-all-archive-files)))))
116 files)))
118 (defun org-all-archive-files ()
119 "Get a list of all archive files used in the current buffer."
120 (let (file files)
121 (save-excursion
122 (save-restriction
123 (goto-char (point-min))
124 (while (re-search-forward
125 "^\\(#\\+\\|[ \t]*:\\)ARCHIVE:[ \t]+\\(.*\\)"
126 nil 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))
135 files))
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))
143 (buffer-file-name)
144 (expand-file-name
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
165 this heading."
166 (interactive "P")
167 (if find-done
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)
185 (current-time)))
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))
194 (unless afile
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)))
202 (unless 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))
208 (save-excursion
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)
214 (match-string 2))
215 priority (org-get-priority
216 (if (match-end 3) (match-string 3) ""))
217 ltags (org-get-tags)
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))
225 (set-buffer buffer)
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)))
232 (when newfile-p
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)
243 (org-odd-levels-only
244 (if (local-variable-p 'org-odd-levels-only (current-buffer))
245 org-odd-levels-only
246 tr-org-odd-levels-only)))
247 (goto-char (point-min))
248 (show-all)
249 (if heading
250 (progn
251 (if (re-search-forward
252 (concat "^" (regexp-quote heading)
253 (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
254 nil t)
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")
260 (end-of-line 0))
261 ;; Make the subtree visible
262 (show-subtree)
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"))
269 ;; Paste
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)
278 (org-todo
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))
293 (save-buffer)
294 ;; Check if it is OK to kill the buffer
295 (unless
296 (or visiting
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."
314 (interactive)
315 (save-restriction
316 (widen)
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))
322 (setq pos (point))
323 (condition-case nil
324 (outline-up-heading 1 t)
325 (error (goto-char (point-min))))
326 (setq b (point))
327 (condition-case nil
328 (org-end-of-subtree t t)
329 (error (goto-char (point-max))))
330 (setq e (point))
331 (goto-char b)
332 (unless (re-search-forward
333 (concat "^" (regexp-quote leader)
334 "[ \t]*"
335 org-archive-sibling-heading
336 "[ \t]*:"
337 org-archive-tag ":") e t)
338 (goto-char e)
339 (or (bolp) (newline))
340 (insert leader org-archive-sibling-heading "\n")
341 (beginning-of-line 0)
342 (org-toggle-tag org-archive-tag 'on))
343 (beginning-of-line 1)
344 (org-end-of-subtree t t)
345 (save-excursion
346 (goto-char pos)
347 (org-cut-subtree))
348 (org-paste-subtree (org-get-valid-level level 1))
349 (org-set-property
350 "ARCHIVE_TIME"
351 (format-time-string
352 (substring (cdr org-time-stamp-formats) 1 -1)
353 (current-time)))
354 (outline-up-heading 1 t)
355 (hide-subtree)
356 (goto-char pos))))
358 (defun org-archive-all-done (&optional tag)
359 "Archive sublevels of the current tree without open TODO items.
360 If the cursor is not on a headline, try all level 1 trees. If
361 it is on a headline, try all direct children.
362 When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
363 (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
364 (rea (concat ".*:" org-archive-tag ":"))
365 (begm (make-marker))
366 (endm (make-marker))
367 (question (if tag "Set ARCHIVE tag (no open TODO items)? "
368 "Move subtree to archive (no open TODO items)? "))
369 beg end (cntarch 0))
370 (if (org-on-heading-p)
371 (progn
372 (setq re1 (concat "^" (regexp-quote
373 (make-string
374 (1+ (- (match-end 0) (match-beginning 0) 1))
375 ?*))
376 " "))
377 (move-marker begm (point))
378 (move-marker endm (org-end-of-subtree t)))
379 (setq re1 "^* ")
380 (move-marker begm (point-min))
381 (move-marker endm (point-max)))
382 (save-excursion
383 (goto-char begm)
384 (while (re-search-forward re1 endm t)
385 (setq beg (match-beginning 0)
386 end (save-excursion (org-end-of-subtree t) (point)))
387 (goto-char beg)
388 (if (re-search-forward re end t)
389 (goto-char end)
390 (goto-char beg)
391 (if (and (or (not tag) (not (looking-at rea)))
392 (y-or-n-p question))
393 (progn
394 (if tag
395 (org-toggle-tag org-archive-tag 'on)
396 (org-archive-subtree))
397 (setq cntarch (1+ cntarch)))
398 (goto-char end)))))
399 (message "%d trees archived" cntarch)))
401 (defun org-toggle-archive-tag (&optional find-done)
402 "Toggle the archive tag for the current headline.
403 With prefix ARG, check all children of current headline and offer tagging
404 the children that do not contain any open TODO items."
405 (interactive "P")
406 (if find-done
407 (org-archive-all-done 'tag)
408 (let (set)
409 (save-excursion
410 (org-back-to-heading t)
411 (setq set (org-toggle-tag org-archive-tag))
412 (when set (hide-subtree)))
413 (and set (beginning-of-line 1))
414 (message "Subtree %s" (if set "archived" "unarchived")))))
416 (provide 'org-archive)
418 ;; arch-tag: 0837f601-9699-43c3-8b90-631572ae6c85
420 ;;; org-archive.el ends here