Add current-line in simple.el
[emacs.git] / lisp / org / org-archive.el
blob39a6581046ae0fbed02a14442da3c61eda087773
1 ;;; org-archive.el --- Archiving for Org-mode
3 ;; Copyright (C) 2004-2017 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)
32 (eval-when-compile (require 'cl))
34 (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
35 (declare-function org-datetree-find-date-create "org-datetree" (date &optional keep-restriction))
37 (defcustom org-archive-default-command 'org-archive-subtree
38 "The default archiving command."
39 :group 'org-archive
40 :type '(choice
41 (const org-archive-subtree)
42 (const org-archive-to-archive-sibling)
43 (const org-archive-set-tag)))
45 (defcustom org-archive-reversed-order nil
46 "Non-nil means make the tree first child under the archive heading, not last."
47 :group 'org-archive
48 :version "24.1"
49 :type 'boolean)
51 (defcustom org-archive-sibling-heading "Archive"
52 "Name of the local archive sibling that is used to archive entries locally.
53 Locally means: in the tree, under a sibling.
54 See `org-archive-to-archive-sibling' for more information."
55 :group 'org-archive
56 :type 'string)
58 (defcustom org-archive-mark-done nil
59 "Non-nil means mark entries as DONE when they are moved to the archive file.
60 This can be a string to set the keyword to use. When t, Org-mode will
61 use the first keyword in its list that means done."
62 :group 'org-archive
63 :type '(choice
64 (const :tag "No" nil)
65 (const :tag "Yes" t)
66 (string :tag "Use this keyword")))
68 (defcustom org-archive-stamp-time t
69 "Non-nil means add a time stamp to entries moved to an archive file.
70 This variable is obsolete and has no effect anymore, instead add or remove
71 `time' from the variable `org-archive-save-context-info'."
72 :group 'org-archive
73 :type 'boolean)
75 (defcustom org-archive-file-header-format "\nArchived entries from file %s\n\n"
76 "The header format string for newly created archive files.
77 When nil, no header will be inserted.
78 When a string, a %s formatter will be replaced by the file name."
79 :group 'org-archive
80 :version "24.4"
81 :package-version '(Org . "8.0")
82 :type 'string)
84 (defcustom org-archive-subtree-add-inherited-tags 'infile
85 "Non-nil means append inherited tags when archiving a subtree."
86 :group 'org-archive
87 :version "24.1"
88 :type '(choice
89 (const :tag "Never" nil)
90 (const :tag "When archiving a subtree to the same file" infile)
91 (const :tag "Always" t)))
93 (defcustom org-archive-save-context-info '(time file olpath category todo itags)
94 "Parts of context info that should be stored as properties when archiving.
95 When a subtree is moved to an archive file, it loses information given by
96 context, like inherited tags, the category, and possibly also the TODO
97 state (depending on the variable `org-archive-mark-done').
98 This variable can be a list of any of the following symbols:
100 time The time of archiving.
101 file The file where the entry originates.
102 ltags The local tags, in the headline of the subtree.
103 itags The tags the subtree inherits from further up the hierarchy.
104 todo The pre-archive TODO state.
105 category The category, taken from file name or #+CATEGORY lines.
106 olpath The outline path to the item. These are all headlines above
107 the current item, separated by /, like a file path.
109 For each symbol present in the list, a property will be created in
110 the archived entry, with a prefix \"ARCHIVE_\", to remember this
111 information."
112 :group 'org-archive
113 :type '(set :greedy t
114 (const :tag "Time" time)
115 (const :tag "File" file)
116 (const :tag "Category" category)
117 (const :tag "TODO state" todo)
118 (const :tag "Priority" priority)
119 (const :tag "Inherited tags" itags)
120 (const :tag "Outline path" olpath)
121 (const :tag "Local tags" ltags)))
123 (defun org-get-local-archive-location ()
124 "Get the archive location applicable at point."
125 (let ((re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
126 prop)
127 (save-excursion
128 (save-restriction
129 (widen)
130 (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
131 (cond
132 ((and prop (string-match "\\S-" prop))
133 prop)
134 ((or (re-search-backward re nil t)
135 (re-search-forward re nil t))
136 (match-string 1))
137 (t org-archive-location))))))
139 ;;;###autoload
140 (defun org-add-archive-files (files)
141 "Splice the archive files into the list of files.
142 This implies visiting all these files and finding out what the
143 archive file is."
144 (org-uniquify
145 (apply
146 'append
147 (mapcar
148 (lambda (f)
149 (if (not (file-exists-p f))
151 (with-current-buffer (org-get-agenda-file-buffer f)
152 (cons f (org-all-archive-files)))))
153 files))))
155 (defun org-all-archive-files ()
156 "Get a list of all archive files used in the current buffer."
157 (let (file files)
158 (save-excursion
159 (save-restriction
160 (goto-char (point-min))
161 (while (re-search-forward
162 "^\\(#\\+\\|[ \t]*:\\)ARCHIVE:[ \t]+\\(.*\\)"
163 nil t)
164 (setq file (org-extract-archive-file
165 (org-match-string-no-properties 2)))
166 (and file (> (length file) 0) (file-exists-p file)
167 (pushnew file files :test #'equal)))))
168 (setq files (nreverse files))
169 (setq file (org-extract-archive-file))
170 (and file (> (length file) 0) (file-exists-p file)
171 (pushnew file files :test #'equal))
172 files))
174 (defun org-extract-archive-file (&optional location)
175 "Extract and expand the file name 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 (if (= (match-beginning 1) (match-end 1))
180 (buffer-file-name (buffer-base-buffer))
181 (expand-file-name
182 (format (match-string 1 location)
183 (file-name-nondirectory
184 (buffer-file-name (buffer-base-buffer))))))))
186 (defun org-extract-archive-heading (&optional location)
187 "Extract the heading from archive LOCATION.
188 if LOCATION is not given, the value of `org-archive-location' is used."
189 (setq location (or location org-archive-location))
190 (if (string-match "\\(.*\\)::\\(.*\\)" location)
191 (format (match-string 2 location)
192 (file-name-nondirectory
193 (buffer-file-name (buffer-base-buffer))))))
195 ;;;###autoload
196 (defun org-archive-subtree (&optional find-done)
197 "Move the current subtree to the archive.
198 The archive can be a certain top-level heading in the current file, or in
199 a different file. The tree will be moved to that location, the subtree
200 heading be marked DONE, and the current time will be added.
202 When called with prefix argument FIND-DONE, find whole trees without any
203 open TODO items and archive them (after getting confirmation from the user).
204 If the cursor is not at a headline when this command is called, try all level
205 1 trees. If the cursor is on a headline, only try the direct children of
206 this heading."
207 (interactive "P")
208 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
209 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
210 'region-start-level 'region))
211 org-loop-over-headlines-in-active-region)
212 (org-map-entries
213 `(progn (setq org-map-continue-from (progn (org-back-to-heading) (point)))
214 (org-archive-subtree ,find-done))
215 org-loop-over-headlines-in-active-region
216 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
217 (if find-done
218 (org-archive-all-done)
219 ;; Save all relevant TODO keyword-relatex variables
220 (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
221 (tr-org-todo-keywords-1 org-todo-keywords-1)
222 (tr-org-todo-kwd-alist org-todo-kwd-alist)
223 (tr-org-done-keywords org-done-keywords)
224 (tr-org-todo-regexp org-todo-regexp)
225 (tr-org-todo-line-regexp org-todo-line-regexp)
226 (tr-org-odd-levels-only org-odd-levels-only)
227 (this-buffer (current-buffer))
228 ;; start of variables that will be used for saving context
229 ;; The compiler complains about them - keep them anyway!
230 (file (abbreviate-file-name
231 (or (buffer-file-name (buffer-base-buffer))
232 (error "No file associated to buffer"))))
233 (olpath (mapconcat 'identity (org-get-outline-path) "/"))
234 (time (format-time-string
235 (substring (cdr org-time-stamp-formats) 1 -1)))
236 category todo priority ltags itags atags
237 ;; end of variables that will be used for saving context
238 location afile heading buffer level newfile-p infile-p visiting
239 datetree-date datetree-subheading-p)
241 ;; Find the local archive location
242 (setq location (org-get-local-archive-location)
243 afile (org-extract-archive-file location)
244 heading (org-extract-archive-heading location)
245 infile-p (equal file (abbreviate-file-name (or afile ""))))
246 (unless afile
247 (error "Invalid `org-archive-location'"))
249 (if (> (length afile) 0)
250 (setq newfile-p (not (file-exists-p afile))
251 visiting (find-buffer-visiting afile)
252 buffer (or visiting (find-file-noselect afile)))
253 (setq buffer (current-buffer)))
254 (unless buffer
255 (error "Cannot access file \"%s\"" afile))
256 (when (string-match "\\`datetree/" heading)
257 ;; Replace with ***, to represent the 3 levels of headings the
258 ;; datetree has.
259 (setq heading (replace-regexp-in-string "\\`datetree/" "***" heading))
260 (setq datetree-subheading-p (> (length heading) 3))
261 (setq datetree-date (org-date-to-gregorian
262 (or (org-entry-get nil "CLOSED" t) time))))
263 (if (and (> (length heading) 0)
264 (string-match "^\\*+" heading))
265 (setq level (match-end 0))
266 (setq heading nil level 0))
267 (save-excursion
268 (org-back-to-heading t)
269 ;; Get context information that will be lost by moving the tree
270 (setq category (org-get-category nil 'force-refresh)
271 todo (and (looking-at org-todo-line-regexp)
272 (match-string 2))
273 priority (org-get-priority
274 (if (match-end 3) (match-string 3) ""))
275 ltags (org-get-tags)
276 itags (org-delete-all ltags (org-get-tags-at))
277 atags (org-get-tags-at))
278 (setq ltags (mapconcat 'identity ltags " ")
279 itags (mapconcat 'identity itags " "))
280 ;; We first only copy, in case something goes wrong
281 ;; we need to protect `this-command', to avoid kill-region sets it,
282 ;; which would lead to duplication of subtrees
283 (let (this-command) (org-copy-subtree 1 nil t))
284 (set-buffer buffer)
285 ;; Enforce org-mode for the archive buffer
286 (if (not (derived-mode-p 'org-mode))
287 ;; Force the mode for future visits.
288 (let ((org-insert-mode-line-in-empty-file t)
289 (org-inhibit-startup t))
290 (call-interactively 'org-mode)))
291 (when (and newfile-p org-archive-file-header-format)
292 (goto-char (point-max))
293 (insert (format org-archive-file-header-format
294 (buffer-file-name this-buffer))))
295 (when datetree-date
296 (require 'org-datetree)
297 (org-datetree-find-date-create datetree-date)
298 (org-narrow-to-subtree))
299 ;; Force the TODO keywords of the original buffer
300 (let ((org-todo-line-regexp tr-org-todo-line-regexp)
301 (org-todo-keywords-1 tr-org-todo-keywords-1)
302 (org-todo-kwd-alist tr-org-todo-kwd-alist)
303 (org-done-keywords tr-org-done-keywords)
304 (org-todo-regexp tr-org-todo-regexp)
305 (org-todo-line-regexp tr-org-todo-line-regexp)
306 (org-odd-levels-only
307 (if (local-variable-p 'org-odd-levels-only (current-buffer))
308 org-odd-levels-only
309 tr-org-odd-levels-only)))
310 (goto-char (point-min))
311 (show-all)
312 (if (and heading (not (and datetree-date (not datetree-subheading-p))))
313 (progn
314 (if (re-search-forward
315 (concat "^" (regexp-quote heading)
316 (org-re "[ \t]*\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\($\\|\r\\)"))
317 nil t)
318 (goto-char (match-end 0))
319 ;; Heading not found, just insert it at the end
320 (goto-char (point-max))
321 (or (bolp) (insert "\n"))
322 ;; datetrees don't need too much spacing
323 (insert (if datetree-date "" "\n") heading "\n")
324 (end-of-line 0))
325 ;; Make the subtree visible
326 (show-subtree)
327 (if org-archive-reversed-order
328 (progn
329 (org-back-to-heading t)
330 (outline-next-heading))
331 (org-end-of-subtree t))
332 (skip-chars-backward " \t\r\n")
333 (and (looking-at "[ \t\r\n]*")
334 ;; datetree archives don't need so much spacing.
335 (replace-match (if datetree-date "\n" "\n\n"))))
336 ;; No specific heading, just go to end of file.
337 (goto-char (point-max)) (unless datetree-date (insert "\n")))
338 ;; Paste
339 (org-paste-subtree (org-get-valid-level level (and heading 1)))
340 ;; Shall we append inherited tags?
341 (and itags
342 (or (and (eq org-archive-subtree-add-inherited-tags 'infile)
343 infile-p)
344 (eq org-archive-subtree-add-inherited-tags t))
345 (org-set-tags-to atags))
346 ;; Mark the entry as done
347 (when (and org-archive-mark-done
348 (looking-at org-todo-line-regexp)
349 (or (not (match-end 2))
350 (not (member (match-string 2) org-done-keywords))))
351 (let (org-log-done org-todo-log-states)
352 (org-todo
353 (car (or (member org-archive-mark-done org-done-keywords)
354 org-done-keywords)))))
356 ;; Add the context info
357 (when org-archive-save-context-info
358 (let ((l org-archive-save-context-info) e n v)
359 (while (setq e (pop l))
360 (when (and (setq v (symbol-value e))
361 (stringp v) (string-match "\\S-" v))
362 (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
363 (org-entry-put (point) n v)))))
365 (widen)
366 ;; Save and kill the buffer, if it is not the same buffer.
367 (when (not (eq this-buffer buffer))
368 (save-buffer))))
369 ;; Here we are back in the original buffer. Everything seems to have
370 ;; worked. So now cut the tree and finish up.
371 (let (this-command) (org-cut-subtree))
372 (when (featurep 'org-inlinetask)
373 (org-inlinetask-remove-END-maybe))
374 (setq org-markers-to-move nil)
375 (message "Subtree archived %s"
376 (if (eq this-buffer buffer)
377 (concat "under heading: " heading)
378 (concat "in file: " (abbreviate-file-name afile))))))
379 (org-reveal)
380 (if (looking-at "^[ \t]*$")
381 (outline-next-visible-heading 1))))
383 ;;;###autoload
384 (defun org-archive-to-archive-sibling ()
385 "Archive the current heading by moving it under the archive sibling.
386 The archive sibling is a sibling of the heading with the heading name
387 `org-archive-sibling-heading' and an `org-archive-tag' tag. If this
388 sibling does not exist, it will be created at the end of the subtree."
389 (interactive)
390 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
391 (let ((cl (when (eq org-loop-over-headlines-in-active-region 'start-level)
392 'region-start-level 'region))
393 org-loop-over-headlines-in-active-region)
394 (org-map-entries
395 '(progn (setq org-map-continue-from
396 (progn (org-back-to-heading)
397 (if (looking-at (concat "^.*:" org-archive-tag ":.*$"))
398 (org-end-of-subtree t)
399 (point))))
400 (when (org-at-heading-p)
401 (org-archive-to-archive-sibling)))
402 org-loop-over-headlines-in-active-region
403 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
404 (save-restriction
405 (widen)
406 (let (b e pos leader level)
407 (org-back-to-heading t)
408 (looking-at org-outline-regexp)
409 (setq leader (match-string 0)
410 level (funcall outline-level))
411 (setq pos (point))
412 (condition-case nil
413 (outline-up-heading 1 t)
414 (error (setq e (point-max)) (goto-char (point-min))))
415 (setq b (point))
416 (unless e
417 (condition-case nil
418 (org-end-of-subtree t t)
419 (error (goto-char (point-max))))
420 (setq e (point)))
421 (goto-char b)
422 (unless (re-search-forward
423 (concat "^" (regexp-quote leader)
424 "[ \t]*"
425 org-archive-sibling-heading
426 "[ \t]*:"
427 org-archive-tag ":") e t)
428 (goto-char e)
429 (or (bolp) (newline))
430 (insert leader org-archive-sibling-heading "\n")
431 (beginning-of-line 0)
432 (org-toggle-tag org-archive-tag 'on))
433 (beginning-of-line 1)
434 (if org-archive-reversed-order
435 (outline-next-heading)
436 (org-end-of-subtree t t))
437 (save-excursion
438 (goto-char pos)
439 (let ((this-command this-command)) (org-cut-subtree)))
440 (org-paste-subtree (org-get-valid-level level 1))
441 (org-set-property
442 "ARCHIVE_TIME"
443 (format-time-string
444 (substring (cdr org-time-stamp-formats) 1 -1)))
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