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