org-babel: further cleaning of parameter-passing to language files
[rgr-org-mode.git] / lisp / org-archive.el
blobffc6c5aecebccd6fa8baacacaf7599df2efa0e5d
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.32trans
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 :group 'org-archive
40 :type '(choice
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."
49 :group 'org-archive
50 :type 'string)
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."
56 :group 'org-archive
57 :type '(choice
58 (const :tag "No" nil)
59 (const :tag "Yes" t)
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'."
66 :group 'org-archive
67 :type 'boolean)
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
87 information."
88 :group 'org-archive
89 :type '(set :greedy t
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]*$")
102 prop)
103 (save-excursion
104 (save-restriction
105 (widen)
106 (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
107 (cond
108 ((and prop (string-match "\\S-" prop))
109 prop)
110 ((or (re-search-backward re nil t)
111 (re-search-forward re nil t))
112 (match-string 1))
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
118 archive file is."
119 (org-uniquify
120 (apply
121 'append
122 (mapcar
123 (lambda (f)
124 (if (not (file-exists-p f))
126 (with-current-buffer (org-get-agenda-file-buffer f)
127 (cons f (org-all-archive-files)))))
128 files))))
130 (defun org-all-archive-files ()
131 "Get a list of all archive files used in the current buffer."
132 (let (file files)
133 (save-excursion
134 (save-restriction
135 (goto-char (point-min))
136 (while (re-search-forward
137 "^\\(#\\+\\|[ \t]*:\\)ARCHIVE:[ \t]+\\(.*\\)"
138 nil 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))
147 files))
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))
155 (buffer-file-name)
156 (expand-file-name
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
178 this heading."
179 (interactive "P")
180 (if find-done
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)
198 (current-time)))
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))
207 (unless afile
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)))
215 (unless 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))
221 (save-excursion
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)
227 (match-string 2))
228 priority (org-get-priority
229 (if (match-end 3) (match-string 3) ""))
230 ltags (org-get-tags)
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))
238 (set-buffer buffer)
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)))
245 (when newfile-p
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)
256 (org-odd-levels-only
257 (if (local-variable-p 'org-odd-levels-only (current-buffer))
258 org-odd-levels-only
259 tr-org-odd-levels-only)))
260 (goto-char (point-min))
261 (show-all)
262 (if heading
263 (progn
264 (if (re-search-forward
265 (concat "^" (regexp-quote heading)
266 (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
267 nil t)
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")
273 (end-of-line 0))
274 ;; Make the subtree visible
275 (show-subtree)
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"))
282 ;; Paste
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)
291 (org-todo
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))
306 (save-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))))))
318 (org-reveal)
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."
327 (interactive)
328 (save-restriction
329 (widen)
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))
335 (setq pos (point))
336 (condition-case nil
337 (outline-up-heading 1 t)
338 (error (setq e (point-max)) (goto-char (point-min))))
339 (setq b (point))
340 (unless e
341 (condition-case nil
342 (org-end-of-subtree t t)
343 (error (goto-char (point-max))))
344 (setq e (point)))
345 (goto-char b)
346 (unless (re-search-forward
347 (concat "^" (regexp-quote leader)
348 "[ \t]*"
349 org-archive-sibling-heading
350 "[ \t]*:"
351 org-archive-tag ":") e t)
352 (goto-char e)
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)
359 (save-excursion
360 (goto-char pos)
361 (let ((this-command this-command)) (org-cut-subtree)))
362 (org-paste-subtree (org-get-valid-level level 1))
363 (org-set-property
364 "ARCHIVE_TIME"
365 (format-time-string
366 (substring (cdr org-time-stamp-formats) 1 -1)
367 (current-time)))
368 (outline-up-heading 1 t)
369 (hide-subtree)
370 (org-cycle-show-empty-lines 'folded)
371 (goto-char pos)))
372 (org-reveal)
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 ":"))
383 (begm (make-marker))
384 (endm (make-marker))
385 (question (if tag "Set ARCHIVE tag (no open TODO items)? "
386 "Move subtree to archive (no open TODO items)? "))
387 beg end (cntarch 0))
388 (if (org-on-heading-p)
389 (progn
390 (setq re1 (concat "^" (regexp-quote
391 (make-string
392 (1+ (- (match-end 0) (match-beginning 0) 1))
393 ?*))
394 " "))
395 (move-marker begm (point))
396 (move-marker endm (org-end-of-subtree t)))
397 (setq re1 "^* ")
398 (move-marker begm (point-min))
399 (move-marker endm (point-max)))
400 (save-excursion
401 (goto-char begm)
402 (while (re-search-forward re1 endm t)
403 (setq beg (match-beginning 0)
404 end (save-excursion (org-end-of-subtree t) (point)))
405 (goto-char beg)
406 (if (re-search-forward re end t)
407 (goto-char end)
408 (goto-char beg)
409 (if (and (or (not tag) (not (looking-at rea)))
410 (y-or-n-p question))
411 (progn
412 (if tag
413 (org-toggle-tag org-archive-tag 'on)
414 (org-archive-subtree))
415 (setq cntarch (1+ cntarch)))
416 (goto-char end)))))
417 (message "%d trees archived" cntarch)))
419 (defun org-toggle-archive-tag (&optional find-done)
420 "Toggle the archive tag for the current headline.
421 With prefix ARG, check all children of current headline and offer tagging
422 the children that do not contain any open TODO items."
423 (interactive "P")
424 (if find-done
425 (org-archive-all-done 'tag)
426 (let (set)
427 (save-excursion
428 (org-back-to-heading t)
429 (setq set (org-toggle-tag org-archive-tag))
430 (when set (hide-subtree)))
431 (and set (beginning-of-line 1))
432 (message "Subtree %s" (if set "archived" "unarchived")))))
434 (defun org-archive-set-tag ()
435 "Set the ARCHIVE tag."
436 (interactive)
437 (org-toggle-tag org-archive-tag 'on))
439 ;;;###autoload
440 (defun org-archive-subtree-default ()
441 "Archive the current subtree with the default command.
442 This command is set with the variable `org-archive-default-command'."
443 (interactive)
444 (call-interactively org-archive-default-command))
446 (defun org-archive-subtree-default-with-confirmation ()
447 "Archive the current subtree with the default command.
448 This command is set with the variable `org-archive-default-command'."
449 (interactive)
450 (if (y-or-n-p "Archive this subtree or entry? ")
451 (call-interactively org-archive-default-command)
452 (error "Abort")))
454 (provide 'org-archive)
456 ;; arch-tag: 0837f601-9699-43c3-8b90-631572ae6c85
458 ;;; org-archive.el ends here