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