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