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