Merge branch 'maint'
[org-mode.git] / lisp / org-archive.el
blobd5bdff16f9bbbc79b08e5c1c05950b34ce657443
1 ;;; org-archive.el --- Archiving for Org-mode
3 ;; Copyright (C) 2004-2013 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 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file contains the face definitions for Org.
29 ;;; Code:
31 (require 'org)
33 (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
34 (declare-function org-datetree-find-date-create "org-datetree" (date &optional keep-restriction))
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)))
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 :version "24.1"
48 :type 'boolean)
50 (defcustom org-archive-sibling-heading "Archive"
51 "Name of the local archive sibling that is used to archive entries locally.
52 Locally means: in the tree, under a sibling.
53 See `org-archive-to-archive-sibling' for more information."
54 :group 'org-archive
55 :type 'string)
57 (defcustom org-archive-mark-done nil
58 "Non-nil means mark entries as DONE when they are moved to the archive file.
59 This can be a string to set the keyword to use. When t, Org-mode will
60 use the first keyword in its list that means done."
61 :group 'org-archive
62 :type '(choice
63 (const :tag "No" nil)
64 (const :tag "Yes" t)
65 (string :tag "Use this keyword")))
67 (defcustom org-archive-stamp-time t
68 "Non-nil means add a time stamp to entries moved to an archive file.
69 This variable is obsolete and has no effect anymore, instead add or remove
70 `time' from the variable `org-archive-save-context-info'."
71 :group 'org-archive
72 :type 'boolean)
74 (defcustom org-archive-file-header-format "\nArchived entries from file %s\n\n"
75 "The header format string for newly created archive files.
76 When nil, no header will be inserted.
77 When a string, a %s formatter will be replaced by the file name."
78 :group 'org-archive
79 :version "24.4"
80 :package-version '(Org . "8.0")
81 :type 'string)
83 (defcustom org-archive-subtree-add-inherited-tags 'infile
84 "Non-nil means append inherited tags when archiving a subtree."
85 :group 'org-archive
86 :version "24.1"
87 :type '(choice
88 (const :tag "Never" nil)
89 (const :tag "When archiving a subtree to the same file" infile)
90 (const :tag "Always" t)))
92 (defcustom org-archive-save-context-info '(time file olpath category todo itags)
93 "Parts of context info that should be stored as properties when archiving.
94 When a subtree is moved to an archive file, it loses information given by
95 context, like inherited tags, the category, and possibly also the TODO
96 state (depending on the variable `org-archive-mark-done').
97 This variable can be a list of any of the following symbols:
99 time The time of archiving.
100 file The file where the entry originates.
101 ltags The local tags, in the headline of the subtree.
102 itags The tags the subtree inherits from further up the hierarchy.
103 todo The pre-archive TODO state.
104 category The category, taken from file name or #+CATEGORY lines.
105 olpath The outline path to the item. These are all headlines above
106 the current item, separated by /, like a file path.
108 For each symbol present in the list, a property will be created in
109 the archived entry, with a prefix \"ARCHIVE_\", to remember this
110 information."
111 :group 'org-archive
112 :type '(set :greedy t
113 (const :tag "Time" time)
114 (const :tag "File" file)
115 (const :tag "Category" category)
116 (const :tag "TODO state" todo)
117 (const :tag "Priority" priority)
118 (const :tag "Inherited tags" itags)
119 (const :tag "Outline path" olpath)
120 (const :tag "Local tags" ltags)))
122 (defun org-get-local-archive-location ()
123 "Get the archive location applicable at point."
124 (let ((re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
125 prop)
126 (save-excursion
127 (save-restriction
128 (widen)
129 (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
130 (cond
131 ((and prop (string-match "\\S-" prop))
132 prop)
133 ((or (re-search-backward re nil t)
134 (re-search-forward re nil t))
135 (match-string 1))
136 (t org-archive-location))))))
138 (defun org-add-archive-files (files)
139 "Splice the archive files into the list of files.
140 This implies visiting all these files and finding out what the
141 archive file is."
142 (org-uniquify
143 (apply
144 'append
145 (mapcar
146 (lambda (f)
147 (if (not (file-exists-p f))
149 (with-current-buffer (org-get-agenda-file-buffer f)
150 (cons f (org-all-archive-files)))))
151 files))))
153 (defun org-all-archive-files ()
154 "Get a list of all archive files used in the current buffer."
155 (let (file files)
156 (save-excursion
157 (save-restriction
158 (goto-char (point-min))
159 (while (re-search-forward
160 "^\\(#\\+\\|[ \t]*:\\)ARCHIVE:[ \t]+\\(.*\\)"
161 nil t)
162 (setq file (org-extract-archive-file
163 (org-match-string-no-properties 2)))
164 (and file (> (length file) 0) (file-exists-p file)
165 (add-to-list 'files file)))))
166 (setq files (nreverse files))
167 (setq file (org-extract-archive-file))
168 (and file (> (length file) 0) (file-exists-p file)
169 (add-to-list 'files file))
170 files))
172 (defun org-extract-archive-file (&optional location)
173 "Extract and expand the file name from archive LOCATION.
174 if LOCATION is not given, the value of `org-archive-location' is used."
175 (setq location (or location org-archive-location))
176 (if (string-match "\\(.*\\)::\\(.*\\)" location)
177 (if (= (match-beginning 1) (match-end 1))
178 (buffer-file-name (buffer-base-buffer))
179 (expand-file-name
180 (format (match-string 1 location)
181 (file-name-nondirectory
182 (buffer-file-name (buffer-base-buffer))))))))
184 (defun org-extract-archive-heading (&optional location)
185 "Extract the heading from archive LOCATION.
186 if LOCATION is not given, the value of `org-archive-location' is used."
187 (setq location (or location org-archive-location))
188 (if (string-match "\\(.*\\)::\\(.*\\)" location)
189 (format (match-string 2 location)
190 (file-name-nondirectory
191 (buffer-file-name (buffer-base-buffer))))))
193 ;;;###autoload
194 (defun org-archive-subtree (&optional find-done)
195 "Move the current subtree to the archive.
196 The archive can be a certain top-level heading in the current file, or in
197 a different file. The tree will be moved to that location, the subtree
198 heading be marked DONE, and the current time will be added.
200 When called with prefix argument FIND-DONE, find whole trees without any
201 open TODO items and archive them (after getting confirmation from the user).
202 If the cursor is not at a headline when this command is called, try all level
203 1 trees. If the cursor is on a headline, only try the direct children of
204 this heading."
205 (interactive "P")
206 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
207 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
208 'region-start-level 'region))
209 org-loop-over-headlines-in-active-region)
210 (org-map-entries
211 `(progn (setq org-map-continue-from (progn (org-back-to-heading) (point)))
212 (org-archive-subtree ,find-done))
213 org-loop-over-headlines-in-active-region
214 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
215 (if find-done
216 (org-archive-all-done)
217 ;; Save all relevant TODO keyword-relatex variables
218 (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
219 (tr-org-todo-keywords-1 org-todo-keywords-1)
220 (tr-org-todo-kwd-alist org-todo-kwd-alist)
221 (tr-org-done-keywords org-done-keywords)
222 (tr-org-todo-regexp org-todo-regexp)
223 (tr-org-todo-line-regexp org-todo-line-regexp)
224 (tr-org-odd-levels-only org-odd-levels-only)
225 (this-buffer (current-buffer))
226 ;; start of variables that will be used for saving context
227 ;; The compiler complains about them - keep them anyway!
228 (file (abbreviate-file-name
229 (or (buffer-file-name (buffer-base-buffer))
230 (error "No file associated to buffer"))))
231 (olpath (mapconcat 'identity (org-get-outline-path) "/"))
232 (time (format-time-string
233 (substring (cdr org-time-stamp-formats) 1 -1)
234 (current-time)))
235 category todo priority ltags itags atags
236 ;; end of variables that will be used for saving context
237 location afile heading buffer level newfile-p infile-p visiting
238 datetree-date datetree-subheading-p)
240 ;; Find the local archive location
241 (setq location (org-get-local-archive-location)
242 afile (org-extract-archive-file location)
243 heading (org-extract-archive-heading location)
244 infile-p (equal file (abbreviate-file-name (or afile ""))))
245 (unless afile
246 (error "Invalid `org-archive-location'"))
248 (if (> (length afile) 0)
249 (setq newfile-p (not (file-exists-p afile))
250 visiting (find-buffer-visiting afile)
251 buffer (or visiting (find-file-noselect afile)))
252 (setq buffer (current-buffer)))
253 (unless buffer
254 (error "Cannot access file \"%s\"" afile))
255 (when (string-match "\\`datetree/" heading)
256 ;; Replace with ***, to represent the 3 levels of headings the
257 ;; datetree has.
258 (setq heading (replace-regexp-in-string "\\`datetree/" "***" heading))
259 (setq datetree-subheading-p (> (length heading) 3))
260 (setq datetree-date (org-date-to-gregorian
261 (or (org-entry-get nil "CLOSED" t) time))))
262 (if (and (> (length heading) 0)
263 (string-match "^\\*+" heading))
264 (setq level (match-end 0))
265 (setq heading nil level 0))
266 (save-excursion
267 (org-back-to-heading t)
268 ;; Get context information that will be lost by moving the tree
269 (setq category (org-get-category nil 'force-refresh)
270 todo (and (looking-at org-todo-line-regexp)
271 (match-string 2))
272 priority (org-get-priority
273 (if (match-end 3) (match-string 3) ""))
274 ltags (org-get-tags)
275 itags (org-delete-all ltags (org-get-tags-at))
276 atags (org-get-tags-at))
277 (setq ltags (mapconcat 'identity ltags " ")
278 itags (mapconcat 'identity itags " "))
279 ;; We first only copy, in case something goes wrong
280 ;; we need to protect `this-command', to avoid kill-region sets it,
281 ;; which would lead to duplication of subtrees
282 (let (this-command) (org-copy-subtree 1 nil t))
283 (set-buffer buffer)
284 ;; Enforce org-mode for the archive buffer
285 (if (not (derived-mode-p 'org-mode))
286 ;; Force the mode for future visits.
287 (let ((org-insert-mode-line-in-empty-file t)
288 (org-inhibit-startup t))
289 (call-interactively 'org-mode)))
290 (when (and newfile-p org-archive-file-header-format)
291 (goto-char (point-max))
292 (insert (format org-archive-file-header-format
293 (buffer-file-name this-buffer))))
294 (when datetree-date
295 (require 'org-datetree)
296 (org-datetree-find-date-create datetree-date)
297 (org-narrow-to-subtree))
298 ;; Force the TODO keywords of the original buffer
299 (let ((org-todo-line-regexp tr-org-todo-line-regexp)
300 (org-todo-keywords-1 tr-org-todo-keywords-1)
301 (org-todo-kwd-alist tr-org-todo-kwd-alist)
302 (org-done-keywords tr-org-done-keywords)
303 (org-todo-regexp tr-org-todo-regexp)
304 (org-todo-line-regexp tr-org-todo-line-regexp)
305 (org-odd-levels-only
306 (if (local-variable-p 'org-odd-levels-only (current-buffer))
307 org-odd-levels-only
308 tr-org-odd-levels-only)))
309 (goto-char (point-min))
310 (show-all)
311 (if (and heading (not (and datetree-date (not datetree-subheading-p))))
312 (progn
313 (if (re-search-forward
314 (concat "^" (regexp-quote heading)
315 (org-re "[ \t]*\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\($\\|\r\\)"))
316 nil t)
317 (goto-char (match-end 0))
318 ;; Heading not found, just insert it at the end
319 (goto-char (point-max))
320 (or (bolp) (insert "\n"))
321 ;; datetrees don't need too much spacing
322 (insert (if datetree-date "" "\n") heading "\n")
323 (end-of-line 0))
324 ;; Make the subtree visible
325 (show-subtree)
326 (if org-archive-reversed-order
327 (progn
328 (org-back-to-heading t)
329 (outline-next-heading))
330 (org-end-of-subtree t))
331 (skip-chars-backward " \t\r\n")
332 (and (looking-at "[ \t\r\n]*")
333 ;; datetree archives don't need so much spacing.
334 (replace-match (if datetree-date "\n" "\n\n"))))
335 ;; No specific heading, just go to end of file.
336 (goto-char (point-max)) (unless datetree-date (insert "\n")))
337 ;; Paste
338 (org-paste-subtree (org-get-valid-level level (and heading 1)))
339 ;; Shall we append inherited tags?
340 (and itags
341 (or (and (eq org-archive-subtree-add-inherited-tags 'infile)
342 infile-p)
343 (eq org-archive-subtree-add-inherited-tags t))
344 (org-set-tags-to atags))
345 ;; Mark the entry as done
346 (when (and org-archive-mark-done
347 (looking-at org-todo-line-regexp)
348 (or (not (match-end 2))
349 (not (member (match-string 2) org-done-keywords))))
350 (let (org-log-done org-todo-log-states)
351 (org-todo
352 (car (or (member org-archive-mark-done org-done-keywords)
353 org-done-keywords)))))
355 ;; Add the context info
356 (when org-archive-save-context-info
357 (let ((l org-archive-save-context-info) e n v)
358 (while (setq e (pop l))
359 (when (and (setq v (symbol-value e))
360 (stringp v) (string-match "\\S-" v))
361 (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
362 (org-entry-put (point) n v)))))
364 (widen)
365 ;; Save and kill the buffer, if it is not the same buffer.
366 (when (not (eq this-buffer buffer))
367 (save-buffer))))
368 ;; Here we are back in the original buffer. Everything seems to have
369 ;; worked. So now cut the tree and finish up.
370 (let (this-command) (org-cut-subtree))
371 (when (featurep 'org-inlinetask)
372 (org-inlinetask-remove-END-maybe))
373 (setq org-markers-to-move nil)
374 (message "Subtree archived %s"
375 (if (eq this-buffer buffer)
376 (concat "under heading: " heading)
377 (concat "in file: " (abbreviate-file-name afile))))))
378 (org-reveal)
379 (if (looking-at "^[ \t]*$")
380 (outline-next-visible-heading 1))))
382 ;;;###autoload
383 (defun org-archive-to-archive-sibling ()
384 "Archive the current heading by moving it under the archive sibling.
385 The archive sibling is a sibling of the heading with the heading name
386 `org-archive-sibling-heading' and an `org-archive-tag' tag. If this
387 sibling does not exist, it will be created at the end of the subtree."
388 (interactive)
389 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
390 (let ((cl (when (eq org-loop-over-headlines-in-active-region 'start-level)
391 'region-start-level 'region))
392 org-loop-over-headlines-in-active-region)
393 (org-map-entries
394 '(progn (setq org-map-continue-from
395 (progn (org-back-to-heading)
396 (if (looking-at (concat "^.*:" org-archive-tag ":.*$"))
397 (org-end-of-subtree t)
398 (point))))
399 (when (org-at-heading-p)
400 (org-archive-to-archive-sibling)))
401 org-loop-over-headlines-in-active-region
402 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
403 (save-restriction
404 (widen)
405 (let (b e pos leader level)
406 (org-back-to-heading t)
407 (looking-at org-outline-regexp)
408 (setq leader (match-string 0)
409 level (funcall outline-level))
410 (setq pos (point))
411 (condition-case nil
412 (outline-up-heading 1 t)
413 (error (setq e (point-max)) (goto-char (point-min))))
414 (setq b (point))
415 (unless e
416 (condition-case nil
417 (org-end-of-subtree t t)
418 (error (goto-char (point-max))))
419 (setq e (point)))
420 (goto-char b)
421 (unless (re-search-forward
422 (concat "^" (regexp-quote leader)
423 "[ \t]*"
424 org-archive-sibling-heading
425 "[ \t]*:"
426 org-archive-tag ":") e t)
427 (goto-char e)
428 (or (bolp) (newline))
429 (insert leader org-archive-sibling-heading "\n")
430 (beginning-of-line 0)
431 (org-toggle-tag org-archive-tag 'on))
432 (beginning-of-line 1)
433 (if org-archive-reversed-order
434 (outline-next-heading)
435 (org-end-of-subtree t t))
436 (save-excursion
437 (goto-char pos)
438 (let ((this-command this-command)) (org-cut-subtree)))
439 (org-paste-subtree (org-get-valid-level level 1))
440 (org-set-property
441 "ARCHIVE_TIME"
442 (format-time-string
443 (substring (cdr org-time-stamp-formats) 1 -1)
444 (current-time)))
445 (outline-up-heading 1 t)
446 (hide-subtree)
447 (org-cycle-show-empty-lines 'folded)
448 (goto-char pos)))
449 (org-reveal)
450 (if (looking-at "^[ \t]*$")
451 (outline-next-visible-heading 1))))
453 (defun org-archive-all-done (&optional tag)
454 "Archive sublevels of the current tree without open TODO items.
455 If the cursor is not on a headline, try all level 1 trees. If
456 it is on a headline, try all direct children.
457 When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
458 (let ((re org-not-done-heading-regexp) re1
459 (rea (concat ".*:" org-archive-tag ":"))
460 (begm (make-marker))
461 (endm (make-marker))
462 (question (if tag "Set ARCHIVE tag (no open TODO items)? "
463 "Move subtree to archive (no open TODO items)? "))
464 beg end (cntarch 0))
465 (if (org-at-heading-p)
466 (progn
467 (setq re1 (concat "^" (regexp-quote
468 (make-string
469 (+ (- (match-end 0) (match-beginning 0) 1)
470 (if org-odd-levels-only 2 1))
471 ?*))
472 " "))
473 (move-marker begm (point))
474 (move-marker endm (org-end-of-subtree t)))
475 (setq re1 "^* ")
476 (move-marker begm (point-min))
477 (move-marker endm (point-max)))
478 (save-excursion
479 (goto-char begm)
480 (while (re-search-forward re1 endm t)
481 (setq beg (match-beginning 0)
482 end (save-excursion (org-end-of-subtree t) (point)))
483 (goto-char beg)
484 (if (re-search-forward re end t)
485 (goto-char end)
486 (goto-char beg)
487 (if (and (or (not tag) (not (looking-at rea)))
488 (y-or-n-p question))
489 (progn
490 (if tag
491 (org-toggle-tag org-archive-tag 'on)
492 (org-archive-subtree))
493 (setq cntarch (1+ cntarch)))
494 (goto-char end)))))
495 (message "%d trees archived" cntarch)))
497 ;;;###autoload
498 (defun org-toggle-archive-tag (&optional find-done)
499 "Toggle the archive tag for the current headline.
500 With prefix ARG, check all children of current headline and offer tagging
501 the children that do not contain any open TODO items."
502 (interactive "P")
503 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
504 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
505 'region-start-level 'region))
506 org-loop-over-headlines-in-active-region)
507 (org-map-entries
508 `(org-toggle-archive-tag ,find-done)
509 org-loop-over-headlines-in-active-region
510 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
511 (if find-done
512 (org-archive-all-done 'tag)
513 (let (set)
514 (save-excursion
515 (org-back-to-heading t)
516 (setq set (org-toggle-tag org-archive-tag))
517 (when set (hide-subtree)))
518 (and set (beginning-of-line 1))
519 (message "Subtree %s" (if set "archived" "unarchived"))))))
521 (defun org-archive-set-tag ()
522 "Set the ARCHIVE tag."
523 (interactive)
524 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
525 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
526 'region-start-level 'region))
527 org-loop-over-headlines-in-active-region)
528 (org-map-entries
529 'org-archive-set-tag
530 org-loop-over-headlines-in-active-region
531 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
532 (org-toggle-tag org-archive-tag 'on)))
534 ;;;###autoload
535 (defun org-archive-subtree-default ()
536 "Archive the current subtree with the default command.
537 This command is set with the variable `org-archive-default-command'."
538 (interactive)
539 (call-interactively org-archive-default-command))
541 ;;;###autoload
542 (defun org-archive-subtree-default-with-confirmation ()
543 "Archive the current subtree with the default command.
544 This command is set with the variable `org-archive-default-command'."
545 (interactive)
546 (if (y-or-n-p "Archive this subtree or entry? ")
547 (call-interactively org-archive-default-command)
548 (error "Abort")))
550 (provide 'org-archive)
552 ;; Local variables:
553 ;; generated-autoload-file: "org-loaddefs.el"
554 ;; End:
556 ;;; org-archive.el ends here