Replace xref-match-bounds with xref-match-length
[emacs.git] / lisp / org / org-archive.el
blob2637623abba7eb115277fd73d9be367cbaee4ed0
1 ;;; org-archive.el --- Archiving for Org-mode
3 ;; Copyright (C) 2004-2015 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 ;;;###autoload
139 (defun org-add-archive-files (files)
140 "Splice the archive files into the list of files.
141 This implies visiting all these files and finding out what the
142 archive file is."
143 (org-uniquify
144 (apply
145 'append
146 (mapcar
147 (lambda (f)
148 (if (not (file-exists-p f))
150 (with-current-buffer (org-get-agenda-file-buffer f)
151 (cons f (org-all-archive-files)))))
152 files))))
154 (defun org-all-archive-files ()
155 "Get a list of all archive files used in the current buffer."
156 (let (file files)
157 (save-excursion
158 (save-restriction
159 (goto-char (point-min))
160 (while (re-search-forward
161 "^\\(#\\+\\|[ \t]*:\\)ARCHIVE:[ \t]+\\(.*\\)"
162 nil t)
163 (setq file (org-extract-archive-file
164 (org-match-string-no-properties 2)))
165 (and file (> (length file) 0) (file-exists-p file)
166 (add-to-list 'files file)))))
167 (setq files (nreverse files))
168 (setq file (org-extract-archive-file))
169 (and file (> (length file) 0) (file-exists-p file)
170 (add-to-list 'files file))
171 files))
173 (defun org-extract-archive-file (&optional location)
174 "Extract and expand the file name from archive LOCATION.
175 if LOCATION is not given, the value of `org-archive-location' is used."
176 (setq location (or location org-archive-location))
177 (if (string-match "\\(.*\\)::\\(.*\\)" location)
178 (if (= (match-beginning 1) (match-end 1))
179 (buffer-file-name (buffer-base-buffer))
180 (expand-file-name
181 (format (match-string 1 location)
182 (file-name-nondirectory
183 (buffer-file-name (buffer-base-buffer))))))))
185 (defun org-extract-archive-heading (&optional location)
186 "Extract the heading from archive LOCATION.
187 if LOCATION is not given, the value of `org-archive-location' is used."
188 (setq location (or location org-archive-location))
189 (if (string-match "\\(.*\\)::\\(.*\\)" location)
190 (format (match-string 2 location)
191 (file-name-nondirectory
192 (buffer-file-name (buffer-base-buffer))))))
194 ;;;###autoload
195 (defun org-archive-subtree (&optional find-done)
196 "Move the current subtree to the archive.
197 The archive can be a certain top-level heading in the current file, or in
198 a different file. The tree will be moved to that location, the subtree
199 heading be marked DONE, and the current time will be added.
201 When called with prefix argument FIND-DONE, find whole trees without any
202 open TODO items and archive them (after getting confirmation from the user).
203 If the cursor is not at a headline when this command is called, try all level
204 1 trees. If the cursor is on a headline, only try the direct children of
205 this heading."
206 (interactive "P")
207 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
208 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
209 'region-start-level 'region))
210 org-loop-over-headlines-in-active-region)
211 (org-map-entries
212 `(progn (setq org-map-continue-from (progn (org-back-to-heading) (point)))
213 (org-archive-subtree ,find-done))
214 org-loop-over-headlines-in-active-region
215 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
216 (if find-done
217 (org-archive-all-done)
218 ;; Save all relevant TODO keyword-relatex variables
219 (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
220 (tr-org-todo-keywords-1 org-todo-keywords-1)
221 (tr-org-todo-kwd-alist org-todo-kwd-alist)
222 (tr-org-done-keywords org-done-keywords)
223 (tr-org-todo-regexp org-todo-regexp)
224 (tr-org-todo-line-regexp org-todo-line-regexp)
225 (tr-org-odd-levels-only org-odd-levels-only)
226 (this-buffer (current-buffer))
227 ;; start of variables that will be used for saving context
228 ;; The compiler complains about them - keep them anyway!
229 (file (abbreviate-file-name
230 (or (buffer-file-name (buffer-base-buffer))
231 (error "No file associated to buffer"))))
232 (olpath (mapconcat 'identity (org-get-outline-path) "/"))
233 (time (format-time-string
234 (substring (cdr org-time-stamp-formats) 1 -1)))
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 (outline-up-heading 1 t)
445 (hide-subtree)
446 (org-cycle-show-empty-lines 'folded)
447 (goto-char pos)))
448 (org-reveal)
449 (if (looking-at "^[ \t]*$")
450 (outline-next-visible-heading 1))))
452 (defun org-archive-all-done (&optional tag)
453 "Archive sublevels of the current tree without open TODO items.
454 If the cursor is not on a headline, try all level 1 trees. If
455 it is on a headline, try all direct children.
456 When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
457 (let ((re org-not-done-heading-regexp) re1
458 (rea (concat ".*:" org-archive-tag ":"))
459 (begm (make-marker))
460 (endm (make-marker))
461 (question (if tag "Set ARCHIVE tag (no open TODO items)? "
462 "Move subtree to archive (no open TODO items)? "))
463 beg end (cntarch 0))
464 (if (org-at-heading-p)
465 (progn
466 (setq re1 (concat "^" (regexp-quote
467 (make-string
468 (+ (- (match-end 0) (match-beginning 0) 1)
469 (if org-odd-levels-only 2 1))
470 ?*))
471 " "))
472 (move-marker begm (point))
473 (move-marker endm (org-end-of-subtree t)))
474 (setq re1 "^* ")
475 (move-marker begm (point-min))
476 (move-marker endm (point-max)))
477 (save-excursion
478 (goto-char begm)
479 (while (re-search-forward re1 endm t)
480 (setq beg (match-beginning 0)
481 end (save-excursion (org-end-of-subtree t) (point)))
482 (goto-char beg)
483 (if (re-search-forward re end t)
484 (goto-char end)
485 (goto-char beg)
486 (if (and (or (not tag) (not (looking-at rea)))
487 (y-or-n-p question))
488 (progn
489 (if tag
490 (org-toggle-tag org-archive-tag 'on)
491 (org-archive-subtree))
492 (setq cntarch (1+ cntarch)))
493 (goto-char end)))))
494 (message "%d trees archived" cntarch)))
496 ;;;###autoload
497 (defun org-toggle-archive-tag (&optional find-done)
498 "Toggle the archive tag for the current headline.
499 With prefix ARG, check all children of current headline and offer tagging
500 the children that do not contain any open TODO items."
501 (interactive "P")
502 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
503 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
504 'region-start-level 'region))
505 org-loop-over-headlines-in-active-region)
506 (org-map-entries
507 `(org-toggle-archive-tag ,find-done)
508 org-loop-over-headlines-in-active-region
509 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
510 (if find-done
511 (org-archive-all-done 'tag)
512 (let (set)
513 (save-excursion
514 (org-back-to-heading t)
515 (setq set (org-toggle-tag org-archive-tag))
516 (when set (hide-subtree)))
517 (and set (beginning-of-line 1))
518 (message "Subtree %s" (if set "archived" "unarchived"))))))
520 (defun org-archive-set-tag ()
521 "Set the ARCHIVE tag."
522 (interactive)
523 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
524 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
525 'region-start-level 'region))
526 org-loop-over-headlines-in-active-region)
527 (org-map-entries
528 'org-archive-set-tag
529 org-loop-over-headlines-in-active-region
530 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
531 (org-toggle-tag org-archive-tag 'on)))
533 ;;;###autoload
534 (defun org-archive-subtree-default ()
535 "Archive the current subtree with the default command.
536 This command is set with the variable `org-archive-default-command'."
537 (interactive)
538 (call-interactively org-archive-default-command))
540 ;;;###autoload
541 (defun org-archive-subtree-default-with-confirmation ()
542 "Archive the current subtree with the default command.
543 This command is set with the variable `org-archive-default-command'."
544 (interactive)
545 (if (y-or-n-p "Archive this subtree or entry? ")
546 (call-interactively org-archive-default-command)
547 (error "Abort")))
549 (provide 'org-archive)
551 ;; Local variables:
552 ;; generated-autoload-file: "org-loaddefs.el"
553 ;; End:
555 ;;; org-archive.el ends here