`org-add-planning-info' need not be interactive
[org-mode/org-tableheadings.git] / lisp / org-archive.el
blob96ef021ed2b7bd9fb960cdff879373022aca069d
1 ;;; org-archive.el --- Archiving for Org-mode
3 ;; Copyright (C) 2004-2014 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 (defvar org-archive-hook nil
123 "Hook run after successfully archiving a subtree.
124 Hook functions are called with point on the subtree in the
125 original file. At this stage, the subtree has been added to the
126 archive location, but not yet deleted from the original file.")
128 (defun org-get-local-archive-location ()
129 "Get the archive location applicable at point."
130 (let ((re "^[ \t]*#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
131 prop)
132 (save-excursion
133 (save-restriction
134 (widen)
135 (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
136 (cond
137 ((and prop (string-match "\\S-" prop))
138 prop)
139 ((or (re-search-backward re nil t)
140 (re-search-forward re nil t))
141 (match-string 1))
142 (t org-archive-location))))))
144 ;;;###autoload
145 (defun org-add-archive-files (files)
146 "Splice the archive files into the list of files.
147 This implies visiting all these files and finding out what the
148 archive file is."
149 (org-uniquify
150 (apply
151 'append
152 (mapcar
153 (lambda (f)
154 (if (not (file-exists-p f))
156 (with-current-buffer (org-get-agenda-file-buffer f)
157 (cons f (org-all-archive-files)))))
158 files))))
160 (defun org-all-archive-files ()
161 "Get a list of all archive files used in the current buffer."
162 (let (file files)
163 (save-excursion
164 (save-restriction
165 (goto-char (point-min))
166 (while (re-search-forward
167 "^[ \t]*\\(#\\+\\|:\\)ARCHIVE:[ \t]+\\(.*\\)"
168 nil t)
169 (setq file (org-extract-archive-file
170 (org-match-string-no-properties 2)))
171 (and file (> (length file) 0) (file-exists-p file)
172 (add-to-list 'files file)))))
173 (setq files (nreverse files))
174 (setq file (org-extract-archive-file))
175 (and file (> (length file) 0) (file-exists-p file)
176 (add-to-list 'files file))
177 files))
179 (defun org-extract-archive-file (&optional location)
180 "Extract and expand the file name from archive LOCATION.
181 if LOCATION is not given, the value of `org-archive-location' is used."
182 (setq location (or location org-archive-location))
183 (if (string-match "\\(.*\\)::\\(.*\\)" location)
184 (if (= (match-beginning 1) (match-end 1))
185 (buffer-file-name (buffer-base-buffer))
186 (expand-file-name
187 (format (match-string 1 location)
188 (file-name-nondirectory
189 (buffer-file-name (buffer-base-buffer))))))))
191 (defun org-extract-archive-heading (&optional location)
192 "Extract the heading from archive LOCATION.
193 if LOCATION is not given, the value of `org-archive-location' is used."
194 (setq location (or location org-archive-location))
195 (if (string-match "\\(.*\\)::\\(.*\\)" location)
196 (format (match-string 2 location)
197 (file-name-nondirectory
198 (buffer-file-name (buffer-base-buffer))))))
200 ;;;###autoload
201 (defun org-archive-subtree (&optional find-done)
202 "Move the current subtree to the archive.
203 The archive can be a certain top-level heading in the current file, or in
204 a different file. The tree will be moved to that location, the subtree
205 heading be marked DONE, and the current time will be added.
207 When called with prefix argument FIND-DONE, find whole trees without any
208 open TODO items and archive them (after getting confirmation from the user).
209 If the cursor is not at a headline when this command is called, try all level
210 1 trees. If the cursor is on a headline, only try the direct children of
211 this heading."
212 (interactive "P")
213 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
214 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
215 'region-start-level 'region))
216 org-loop-over-headlines-in-active-region)
217 (org-map-entries
218 `(progn (setq org-map-continue-from (progn (org-back-to-heading) (point)))
219 (org-archive-subtree ,find-done))
220 org-loop-over-headlines-in-active-region
221 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
222 (if find-done
223 (org-archive-all-done)
224 ;; Save all relevant TODO keyword-relatex variables
225 (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
226 (tr-org-todo-keywords-1 org-todo-keywords-1)
227 (tr-org-todo-kwd-alist org-todo-kwd-alist)
228 (tr-org-done-keywords org-done-keywords)
229 (tr-org-todo-regexp org-todo-regexp)
230 (tr-org-todo-line-regexp org-todo-line-regexp)
231 (tr-org-odd-levels-only org-odd-levels-only)
232 (this-buffer (current-buffer))
233 ;; start of variables that will be used for saving context
234 ;; The compiler complains about them - keep them anyway!
235 (file (abbreviate-file-name
236 (or (buffer-file-name (buffer-base-buffer))
237 (error "No file associated to buffer"))))
238 (olpath (mapconcat 'identity (org-get-outline-path) "/"))
239 (time (format-time-string
240 (substring (cdr org-time-stamp-formats) 1 -1)
241 (current-time)))
242 category todo priority ltags itags atags
243 ;; end of variables that will be used for saving context
244 location afile heading buffer level newfile-p infile-p visiting
245 datetree-date datetree-subheading-p)
247 ;; Find the local archive location
248 (setq location (org-get-local-archive-location)
249 afile (org-extract-archive-file location)
250 heading (org-extract-archive-heading location)
251 infile-p (equal file (abbreviate-file-name (or afile ""))))
252 (unless afile
253 (error "Invalid `org-archive-location'"))
255 (if (> (length afile) 0)
256 (setq newfile-p (not (file-exists-p afile))
257 visiting (find-buffer-visiting afile)
258 buffer (or visiting (find-file-noselect afile)))
259 (setq buffer (current-buffer)))
260 (unless buffer
261 (error "Cannot access file \"%s\"" afile))
262 (when (string-match "\\`datetree/" heading)
263 ;; Replace with ***, to represent the 3 levels of headings the
264 ;; datetree has.
265 (setq heading (replace-regexp-in-string "\\`datetree/" "***" heading))
266 (setq datetree-subheading-p (> (length heading) 3))
267 (setq datetree-date (org-date-to-gregorian
268 (or (org-entry-get nil "CLOSED" t) time))))
269 (if (and (> (length heading) 0)
270 (string-match "^\\*+" heading))
271 (setq level (match-end 0))
272 (setq heading nil level 0))
273 (save-excursion
274 (org-back-to-heading t)
275 ;; Get context information that will be lost by moving the tree
276 (setq category (org-get-category nil 'force-refresh)
277 todo (and (looking-at org-todo-line-regexp)
278 (match-string 2))
279 priority (org-get-priority
280 (if (match-end 3) (match-string 3) ""))
281 ltags (org-get-tags)
282 itags (org-delete-all ltags (org-get-tags-at))
283 atags (org-get-tags-at))
284 (setq ltags (mapconcat 'identity ltags " ")
285 itags (mapconcat 'identity itags " "))
286 ;; We first only copy, in case something goes wrong
287 ;; we need to protect `this-command', to avoid kill-region sets it,
288 ;; which would lead to duplication of subtrees
289 (let (this-command) (org-copy-subtree 1 nil t))
290 (set-buffer buffer)
291 ;; Enforce org-mode for the archive buffer
292 (if (not (derived-mode-p 'org-mode))
293 ;; Force the mode for future visits.
294 (let ((org-insert-mode-line-in-empty-file t)
295 (org-inhibit-startup t))
296 (call-interactively 'org-mode)))
297 (when (and newfile-p org-archive-file-header-format)
298 (goto-char (point-max))
299 (insert (format org-archive-file-header-format
300 (buffer-file-name this-buffer))))
301 (when datetree-date
302 (require 'org-datetree)
303 (org-datetree-find-date-create datetree-date)
304 (org-narrow-to-subtree))
305 ;; Force the TODO keywords of the original buffer
306 (let ((org-todo-line-regexp tr-org-todo-line-regexp)
307 (org-todo-keywords-1 tr-org-todo-keywords-1)
308 (org-todo-kwd-alist tr-org-todo-kwd-alist)
309 (org-done-keywords tr-org-done-keywords)
310 (org-todo-regexp tr-org-todo-regexp)
311 (org-todo-line-regexp tr-org-todo-line-regexp)
312 (org-odd-levels-only
313 (if (local-variable-p 'org-odd-levels-only (current-buffer))
314 org-odd-levels-only
315 tr-org-odd-levels-only)))
316 (goto-char (point-min))
317 (show-all)
318 (if (and heading (not (and datetree-date (not datetree-subheading-p))))
319 (progn
320 (if (re-search-forward
321 (concat "^" (regexp-quote heading)
322 (org-re "[ \t]*\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\($\\|\r\\)"))
323 nil t)
324 (goto-char (match-end 0))
325 ;; Heading not found, just insert it at the end
326 (goto-char (point-max))
327 (or (bolp) (insert "\n"))
328 ;; datetrees don't need too much spacing
329 (insert (if datetree-date "" "\n") heading "\n")
330 (end-of-line 0))
331 ;; Make the subtree visible
332 (show-subtree)
333 (if org-archive-reversed-order
334 (progn
335 (org-back-to-heading t)
336 (outline-next-heading))
337 (org-end-of-subtree t))
338 (skip-chars-backward " \t\r\n")
339 (and (looking-at "[ \t\r\n]*")
340 ;; datetree archives don't need so much spacing.
341 (replace-match (if datetree-date "\n" "\n\n"))))
342 ;; No specific heading, just go to end of file.
343 (goto-char (point-max)) (unless datetree-date (insert "\n")))
344 ;; Paste
345 (org-paste-subtree (org-get-valid-level level (and heading 1)))
346 ;; Shall we append inherited tags?
347 (and itags
348 (or (and (eq org-archive-subtree-add-inherited-tags 'infile)
349 infile-p)
350 (eq org-archive-subtree-add-inherited-tags t))
351 (org-set-tags-to atags))
352 ;; Mark the entry as done
353 (when (and org-archive-mark-done
354 (looking-at org-todo-line-regexp)
355 (or (not (match-end 2))
356 (not (member (match-string 2) org-done-keywords))))
357 (let (org-log-done org-todo-log-states)
358 (org-todo
359 (car (or (member org-archive-mark-done org-done-keywords)
360 org-done-keywords)))))
362 ;; Add the context info
363 (when org-archive-save-context-info
364 (let ((l org-archive-save-context-info) e n v)
365 (while (setq e (pop l))
366 (when (and (setq v (symbol-value e))
367 (stringp v) (string-match "\\S-" v))
368 (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
369 (org-entry-put (point) n v)))))
371 (widen)
372 ;; Save and kill the buffer, if it is not the same buffer.
373 (when (not (eq this-buffer buffer))
374 (save-buffer))))
375 ;; Here we are back in the original buffer. Everything seems
376 ;; to have worked. So now run hooks, cut the tree and finish
377 ;; up.
378 (run-hooks 'org-archive-hook)
379 (let (this-command) (org-cut-subtree))
380 (when (featurep 'org-inlinetask)
381 (org-inlinetask-remove-END-maybe))
382 (setq org-markers-to-move nil)
383 (message "Subtree archived %s"
384 (if (eq this-buffer buffer)
385 (concat "under heading: " heading)
386 (concat "in file: " (abbreviate-file-name afile))))))
387 (org-reveal)
388 (if (looking-at "^[ \t]*$")
389 (outline-next-visible-heading 1))))
391 ;;;###autoload
392 (defun org-archive-to-archive-sibling ()
393 "Archive the current heading by moving it under the archive sibling.
394 The archive sibling is a sibling of the heading with the heading name
395 `org-archive-sibling-heading' and an `org-archive-tag' tag. If this
396 sibling does not exist, it will be created at the end of the subtree."
397 (interactive)
398 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
399 (let ((cl (when (eq org-loop-over-headlines-in-active-region 'start-level)
400 'region-start-level 'region))
401 org-loop-over-headlines-in-active-region)
402 (org-map-entries
403 '(progn (setq org-map-continue-from
404 (progn (org-back-to-heading)
405 (if (looking-at (concat "^.*:" org-archive-tag ":.*$"))
406 (org-end-of-subtree t)
407 (point))))
408 (when (org-at-heading-p)
409 (org-archive-to-archive-sibling)))
410 org-loop-over-headlines-in-active-region
411 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
412 (save-restriction
413 (widen)
414 (let (b e pos leader level)
415 (org-back-to-heading t)
416 (looking-at org-outline-regexp)
417 (setq leader (match-string 0)
418 level (funcall outline-level))
419 (setq pos (point))
420 (condition-case nil
421 (outline-up-heading 1 t)
422 (error (setq e (point-max)) (goto-char (point-min))))
423 (setq b (point))
424 (unless e
425 (condition-case nil
426 (org-end-of-subtree t t)
427 (error (goto-char (point-max))))
428 (setq e (point)))
429 (goto-char b)
430 (unless (re-search-forward
431 (concat "^" (regexp-quote leader)
432 "[ \t]*"
433 org-archive-sibling-heading
434 "[ \t]*:"
435 org-archive-tag ":") e t)
436 (goto-char e)
437 (or (bolp) (newline))
438 (insert leader org-archive-sibling-heading "\n")
439 (beginning-of-line 0)
440 (org-toggle-tag org-archive-tag 'on))
441 (beginning-of-line 1)
442 (if org-archive-reversed-order
443 (outline-next-heading)
444 (org-end-of-subtree t t))
445 (save-excursion
446 (goto-char pos)
447 (let ((this-command this-command)) (org-cut-subtree)))
448 (org-paste-subtree (org-get-valid-level level 1))
449 (org-set-property
450 "ARCHIVE_TIME"
451 (format-time-string
452 (substring (cdr org-time-stamp-formats) 1 -1)
453 (current-time)))
454 (outline-up-heading 1 t)
455 (hide-subtree)
456 (org-cycle-show-empty-lines 'folded)
457 (goto-char pos)))
458 (org-reveal)
459 (if (looking-at "^[ \t]*$")
460 (outline-next-visible-heading 1))))
462 (defun org-archive-all-done (&optional tag)
463 "Archive sublevels of the current tree without open TODO items.
464 If the cursor is not on a headline, try all level 1 trees. If
465 it is on a headline, try all direct children.
466 When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
467 (let ((re org-not-done-heading-regexp) re1
468 (rea (concat ".*:" org-archive-tag ":"))
469 (begm (make-marker))
470 (endm (make-marker))
471 (question (if tag "Set ARCHIVE tag (no open TODO items)? "
472 "Move subtree to archive (no open TODO items)? "))
473 beg end (cntarch 0))
474 (if (org-at-heading-p)
475 (progn
476 (setq re1 (concat "^" (regexp-quote
477 (make-string
478 (+ (- (match-end 0) (match-beginning 0) 1)
479 (if org-odd-levels-only 2 1))
480 ?*))
481 " "))
482 (move-marker begm (point))
483 (move-marker endm (org-end-of-subtree t)))
484 (setq re1 "^* ")
485 (move-marker begm (point-min))
486 (move-marker endm (point-max)))
487 (save-excursion
488 (goto-char begm)
489 (while (re-search-forward re1 endm t)
490 (setq beg (match-beginning 0)
491 end (save-excursion (org-end-of-subtree t) (point)))
492 (goto-char beg)
493 (if (re-search-forward re end t)
494 (goto-char end)
495 (goto-char beg)
496 (if (and (or (not tag) (not (looking-at rea)))
497 (y-or-n-p question))
498 (progn
499 (if tag
500 (org-toggle-tag org-archive-tag 'on)
501 (org-archive-subtree))
502 (setq cntarch (1+ cntarch)))
503 (goto-char end)))))
504 (message "%d trees archived" cntarch)))
506 ;;;###autoload
507 (defun org-toggle-archive-tag (&optional find-done)
508 "Toggle the archive tag for the current headline.
509 With prefix ARG, check all children of current headline and offer tagging
510 the children that do not contain any open TODO items."
511 (interactive "P")
512 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
513 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
514 'region-start-level 'region))
515 org-loop-over-headlines-in-active-region)
516 (org-map-entries
517 `(org-toggle-archive-tag ,find-done)
518 org-loop-over-headlines-in-active-region
519 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
520 (if find-done
521 (org-archive-all-done 'tag)
522 (let (set)
523 (save-excursion
524 (org-back-to-heading t)
525 (setq set (org-toggle-tag org-archive-tag))
526 (when set (hide-subtree)))
527 (and set (beginning-of-line 1))
528 (message "Subtree %s" (if set "archived" "unarchived"))))))
530 (defun org-archive-set-tag ()
531 "Set the ARCHIVE tag."
532 (interactive)
533 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
534 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
535 'region-start-level 'region))
536 org-loop-over-headlines-in-active-region)
537 (org-map-entries
538 'org-archive-set-tag
539 org-loop-over-headlines-in-active-region
540 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
541 (org-toggle-tag org-archive-tag 'on)))
543 ;;;###autoload
544 (defun org-archive-subtree-default ()
545 "Archive the current subtree with the default command.
546 This command is set with the variable `org-archive-default-command'."
547 (interactive)
548 (call-interactively org-archive-default-command))
550 ;;;###autoload
551 (defun org-archive-subtree-default-with-confirmation ()
552 "Archive the current subtree with the default command.
553 This command is set with the variable `org-archive-default-command'."
554 (interactive)
555 (if (y-or-n-p "Archive this subtree or entry? ")
556 (call-interactively org-archive-default-command)
557 (error "Abort")))
559 (provide 'org-archive)
561 ;; Local variables:
562 ;; generated-autoload-file: "org-loaddefs.el"
563 ;; End:
565 ;;; org-archive.el ends here