config

Personal configuration.
git clone git://code.dwrz.net/config
Log | Files | Refs

org-archive.el (24677B)


      1 ;;; org-archive.el --- Archiving for Org             -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2004-2024 Free Software Foundation, Inc.
      4 
      5 ;; Author: Carsten Dominik <carsten.dominik@gmail.com>
      6 ;; Keywords: outlines, hypermedia, calendar, text
      7 ;; URL: https://orgmode.org
      8 ;;
      9 ;; This file is part of GNU Emacs.
     10 ;;
     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.
     15 
     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.
     20 
     21 ;; You should have received a copy of the GNU General Public License
     22 ;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
     23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
     24 ;;
     25 ;;; Commentary:
     26 
     27 ;; This file contains the archive functionality for Org.
     28 
     29 ;;; Code:
     30 
     31 (require 'org-macs)
     32 (org-assert-version)
     33 
     34 (require 'org)
     35 (require 'cl-lib)
     36 
     37 (declare-function org-datetree-find-date-create "org-datetree" (date &optional keep-restriction))
     38 (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
     39 (declare-function org-timestamp-to-now "org" (timestamp-string &optional seconds))
     40 
     41 ;; From org-element.el
     42 (defvar org-element--cache-avoid-synchronous-headline-re-parsing)
     43 
     44 (defcustom org-archive-default-command 'org-archive-subtree
     45   "The default archiving command."
     46   :group 'org-archive
     47   :type '(choice
     48 	  (const org-archive-subtree)
     49 	  (const org-archive-to-archive-sibling)
     50 	  (const org-archive-set-tag)))
     51 
     52 (defcustom org-archive-reversed-order nil
     53   "Non-nil means make the tree first child under the archive heading, not last."
     54   :group 'org-archive
     55   :version "24.1"
     56   :type 'boolean)
     57 
     58 (defcustom org-archive-sibling-heading "Archive"
     59   "Name of the local archive sibling that is used to archive entries locally.
     60 Locally means: in the tree, under a sibling.
     61 See `org-archive-to-archive-sibling' for more information."
     62   :group 'org-archive
     63   :type 'string)
     64 
     65 (defcustom org-archive-mark-done nil
     66   "Non-nil means mark entries as DONE when they are moved to the archive file.
     67 This can be a string to set the keyword to use.  When non-nil, Org will
     68 use the first keyword in its list that means done."
     69   :group 'org-archive
     70   :type '(choice
     71 	  (const :tag "No" nil)
     72 	  (const :tag "Yes" t)
     73 	  (string :tag "Use this keyword")))
     74 
     75 (defcustom org-archive-stamp-time t
     76   "Non-nil means add a time stamp to entries moved to an archive file.
     77 This variable is obsolete and has no effect anymore, instead add or remove
     78 `time' from the variable `org-archive-save-context-info'."
     79   :group 'org-archive
     80   :type 'boolean)
     81 
     82 (defcustom org-archive-file-header-format "\nArchived entries from file %s\n\n"
     83   "The header format string for newly created archive files.
     84 When nil, no header will be inserted.
     85 When a string, a %s formatter will be replaced by the file name."
     86   :group 'org-archive
     87   :version "24.4"
     88   :package-version '(Org . "8.0")
     89   :type 'string)
     90 
     91 (defcustom org-archive-subtree-add-inherited-tags 'infile
     92   "Non-nil means append inherited tags when archiving a subtree."
     93   :group 'org-archive
     94   :version "24.1"
     95   :type '(choice
     96 	  (const :tag "Never" nil)
     97 	  (const :tag "When archiving a subtree to the same file" infile)
     98 	  (const :tag "Always" t)))
     99 
    100 (defcustom org-archive-subtree-save-file-p 'from-org
    101   "Conditionally save the archive file after archiving a subtree.
    102 This variable can be any of the following symbols:
    103 
    104 t              saves in all cases.
    105 `from-org'     prevents saving from an agenda-view.
    106 `from-agenda'  saves only when the archive is initiated from an agenda-view.
    107 nil            prevents saving in all cases.
    108 
    109 Note that, regardless of this value, the archive buffer is never
    110 saved when archiving into a location in the current buffer."
    111   :group 'org-archive
    112   :package-version '(Org . "9.4")
    113   :type '(choice
    114 	  (const :tag "Save archive buffer" t)
    115 	  (const :tag "Save when archiving from agenda" from-agenda)
    116 	  (const :tag "Save when archiving from an Org buffer" from-org)
    117 	  (const :tag "Do not save")))
    118 
    119 (defcustom org-archive-save-context-info '(time file olpath category todo itags)
    120   "Parts of context info that should be stored as properties when archiving.
    121 When a subtree is moved to an archive file, it loses information given by
    122 context, like inherited tags, the category, and possibly also the TODO
    123 state (depending on the variable `org-archive-mark-done').
    124 This variable can be a list of any of the following symbols:
    125 
    126 time       The time of archiving.
    127 file       The file where the entry originates.
    128 ltags      The local tags, in the headline of the subtree.
    129 itags      The tags the subtree inherits from further up the hierarchy.
    130 todo       The pre-archive TODO state.
    131 category   The category, taken from file name or #+CATEGORY lines.
    132 olpath     The outline path to the item.  These are all headlines above
    133            the current item, separated by /, like a file path.
    134 
    135 For each symbol present in the list, a property will be created in
    136 the archived entry, with a prefix \"ARCHIVE_\", to remember this
    137 information."
    138   :group 'org-archive
    139   :type '(set :greedy t
    140 	      (const :tag "Time" time)
    141 	      (const :tag "File" file)
    142 	      (const :tag "Category" category)
    143 	      (const :tag "TODO state" todo)
    144 	      (const :tag "Priority" priority)
    145 	      (const :tag "Inherited tags" itags)
    146 	      (const :tag "Outline path" olpath)
    147 	      (const :tag "Local tags" ltags)))
    148 
    149 (defvar org-archive-hook nil
    150   "Hook run after successfully archiving a subtree.
    151 Hook functions are called with point on the subtree in the
    152 original file.  At this stage, the subtree has been added to the
    153 archive location, but not yet deleted from the original file.")
    154 
    155 ;;;###autoload
    156 (defun org-add-archive-files (files)
    157   "Splice the archive FILES into the list of files.
    158 This implies visiting all these files and finding out what the
    159 archive file is."
    160   (seq-uniq
    161    (apply
    162     'append
    163     (mapcar
    164      (lambda (f)
    165        (if (not (file-exists-p f))
    166 	   nil
    167 	 (with-current-buffer (org-get-agenda-file-buffer f)
    168 	   (cons f (org-all-archive-files)))))
    169      files))
    170    #'file-equal-p
    171    ))
    172 
    173 (defun org-all-archive-files ()
    174   "List of all archive files used in the current buffer."
    175   (let* ((case-fold-search t)
    176 	 (files `(,(car (org-archive--compute-location org-archive-location)))))
    177     (org-with-point-at 1
    178       (while (re-search-forward "^[ \t]*:ARCHIVE:" nil t)
    179 	(when (org-at-property-p)
    180 	  (pcase (org-archive--compute-location (match-string 3))
    181 	    (`(,file . ,_)
    182 	     (when (org-string-nw-p file)
    183 	       (cl-pushnew file files :test #'file-equal-p))))))
    184       (cl-remove-if-not #'file-exists-p (nreverse files)))))
    185 
    186 (defun org-archive--compute-location (location)
    187   "Extract and expand the location from archive LOCATION.
    188 Return a pair (FILE . HEADING) where FILE is the file name and
    189 HEADING the heading of the archive location, as strings.  Raise
    190 an error if LOCATION is not a valid archive location."
    191   (unless (string-match "::" location)
    192     (error "Invalid archive location: %S" location))
    193   (let ((current-file (buffer-file-name (buffer-base-buffer)))
    194 	(file-fmt (substring location 0 (match-beginning 0)))
    195 	(heading-fmt (substring location (match-end 0))))
    196     (cons
    197      ;; File part.
    198      (if (org-string-nw-p file-fmt)
    199 	 (expand-file-name
    200 	  (format file-fmt (file-name-nondirectory current-file)))
    201        current-file)
    202      ;; Heading part.
    203      (format heading-fmt (file-name-nondirectory current-file)))))
    204 
    205 ;;;###autoload
    206 (defun org-archive-subtree (&optional find-done)
    207   "Move the current subtree to the archive.
    208 The archive can be a certain top-level heading in the current
    209 file, or in a different file.  The tree will be moved to that
    210 location, the subtree heading be marked DONE, and the current
    211 time will be added.
    212 
    213 When called with a single prefix argument FIND-DONE, find whole
    214 trees without any open TODO items and archive them (after getting
    215 confirmation from the user).  When called with a double prefix
    216 argument, find whole trees with timestamps before today and
    217 archive them (after getting confirmation from the user).  If the
    218 cursor is not at a headline when these commands are called, try
    219 all level 1 trees.  If the cursor is on a headline, only try the
    220 direct children of this heading."
    221   (interactive "P")
    222   (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
    223       (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
    224 		    'region-start-level 'region))
    225 	    org-loop-over-headlines-in-active-region)
    226 	(org-map-entries
    227 	 `(progn (setq org-map-continue-from (progn (org-back-to-heading) (point)))
    228 		 (org-archive-subtree ,find-done))
    229 	 org-loop-over-headlines-in-active-region
    230 	 cl (if (org-invisible-p) (org-end-of-subtree nil t))))
    231     (cond
    232      ((equal find-done '(4))  (org-archive-all-done))
    233      ((equal find-done '(16)) (org-archive-all-old))
    234      (t
    235       ;; Save all relevant TODO keyword-related variables.
    236       (let* ((tr-org-todo-keywords-1 org-todo-keywords-1)
    237 	     (tr-org-todo-kwd-alist org-todo-kwd-alist)
    238 	     (tr-org-done-keywords org-done-keywords)
    239 	     (tr-org-todo-regexp org-todo-regexp)
    240 	     (tr-org-todo-line-regexp org-todo-line-regexp)
    241 	     (tr-org-odd-levels-only org-odd-levels-only)
    242 	     (this-buffer (current-buffer))
    243 	     (time (format-time-string
    244                     (org-time-stamp-format 'with-time 'no-brackets)))
    245 	     (file (abbreviate-file-name
    246 		    (or (buffer-file-name (buffer-base-buffer))
    247 			(error "No file associated to buffer"))))
    248 	     (location (org-archive--compute-location
    249 			(or (org-entry-get nil "ARCHIVE" 'inherit)
    250 			    org-archive-location)))
    251 	     (afile (car location))
    252 	     (heading (cdr location))
    253 	     (infile-p (equal file (abbreviate-file-name (or afile ""))))
    254 	     (newfile-p (and (org-string-nw-p afile)
    255 			     (not (file-exists-p afile))))
    256 	     (buffer (cond ((not (org-string-nw-p afile)) this-buffer)
    257 			   ((find-file-noselect afile 'nowarn))
    258 			   (t (error "Cannot access file \"%s\"" afile))))
    259 	     (org-odd-levels-only
    260 	      (if (local-variable-p 'org-odd-levels-only (current-buffer))
    261 		  org-odd-levels-only
    262 		tr-org-odd-levels-only))
    263 	     level datetree-date datetree-subheading-p
    264              ;; Suppress on-the-fly headline updates.
    265              (org-element--cache-avoid-synchronous-headline-re-parsing t))
    266 	(when (string-match "\\`datetree/\\(\\**\\)" heading)
    267 	  ;; "datetree/" corresponds to 3 levels of headings.
    268 	  (let ((nsub (length (match-string 1 heading))))
    269 	    (setq heading (concat (make-string
    270 				   (+ (if org-odd-levels-only 5 3)
    271 				      (* (org-level-increment) nsub))
    272 				   ?*)
    273 				  (substring heading (match-end 0))))
    274 	    (setq datetree-subheading-p (> nsub 0)))
    275 	  (setq datetree-date (org-date-to-gregorian
    276 			       (or (org-entry-get nil "CLOSED" t) time))))
    277 	(if (and (> (length heading) 0)
    278 		 (string-match "^\\*+" heading))
    279 	    (setq level (match-end 0))
    280 	  (setq heading nil level 0))
    281 	(save-excursion
    282 	  (org-back-to-heading t)
    283 	  ;; Get context information that will be lost by moving the
    284 	  ;; tree.  See `org-archive-save-context-info'.
    285 	  (let* ((all-tags (org-get-tags))
    286 		 (local-tags
    287 		  (cl-remove-if (lambda (tag)
    288 				  (get-text-property 0 'inherited tag))
    289 				all-tags))
    290 		 (inherited-tags
    291 		  (cl-remove-if-not (lambda (tag)
    292 				      (get-text-property 0 'inherited tag))
    293 				    all-tags))
    294 		 (context
    295 		  `((category . ,(org-get-category nil 'force-refresh))
    296 		    (file . ,file)
    297 		    (itags . ,(mapconcat #'identity inherited-tags " "))
    298 		    (ltags . ,(mapconcat #'identity local-tags " "))
    299 		    (olpath . ,(mapconcat #'identity
    300 					  (org-get-outline-path)
    301 					  "/"))
    302 		    (time . ,time)
    303 		    (todo . ,(org-entry-get (point) "TODO")))))
    304 	    ;; We first only copy, in case something goes wrong
    305 	    ;; we need to protect `this-command', to avoid kill-region sets it,
    306 	    ;; which would lead to duplication of subtrees
    307 	    (let (this-command) (org-copy-subtree 1 nil t))
    308 	    (set-buffer buffer)
    309 	    ;; Enforce Org mode for the archive buffer
    310 	    (if (not (derived-mode-p 'org-mode))
    311 		;; Force the mode for future visits.
    312 		(let ((org-insert-mode-line-in-empty-file t)
    313 		      (org-inhibit-startup t))
    314 		  (call-interactively 'org-mode)))
    315 	    (when (and newfile-p org-archive-file-header-format)
    316 	      (goto-char (point-max))
    317 	      (insert (format org-archive-file-header-format
    318 			      (buffer-file-name this-buffer))))
    319 	    (when datetree-date
    320 	      (require 'org-datetree)
    321 	      (org-datetree-find-date-create datetree-date)
    322 	      (org-narrow-to-subtree))
    323 	    ;; Force the TODO keywords of the original buffer
    324 	    (let ((org-todo-line-regexp tr-org-todo-line-regexp)
    325 		  (org-todo-keywords-1 tr-org-todo-keywords-1)
    326 		  (org-todo-kwd-alist tr-org-todo-kwd-alist)
    327 		  (org-done-keywords tr-org-done-keywords)
    328 		  (org-todo-regexp tr-org-todo-regexp)
    329 		  (org-todo-line-regexp tr-org-todo-line-regexp))
    330 	      (goto-char (point-min))
    331 	      (org-fold-show-all '(headings blocks))
    332 	      (if (and heading (not (and datetree-date (not datetree-subheading-p))))
    333 		  (progn
    334 		    (if (re-search-forward
    335 			 (concat "^" (regexp-quote heading)
    336 				 "\\([ \t]+:\\(" org-tag-re ":\\)+\\)?[ \t]*$")
    337 			 nil t)
    338 			(goto-char (match-end 0))
    339 		      ;; Heading not found, just insert it at the end
    340 		      (goto-char (point-max))
    341 		      (or (bolp) (insert "\n"))
    342 		      ;; datetrees don't need too much spacing
    343 		      (insert (if datetree-date "" "\n") heading "\n")
    344 		      (end-of-line 0))
    345 		    ;; Make the subtree visible
    346 		    (org-fold-show-subtree)
    347 		    (if org-archive-reversed-order
    348 			(progn
    349 			  (org-back-to-heading t)
    350 			  (outline-next-heading))
    351 		      (org-end-of-subtree t))
    352 		    (skip-chars-backward " \t\r\n")
    353 		    (and (looking-at "[ \t\r\n]*")
    354 			 ;; datetree archives don't need so much spacing.
    355 			 (replace-match (if datetree-date "\n" "\n\n"))))
    356 		;; No specific heading, just go to end of file, or to the
    357 		;; beginning, depending on `org-archive-reversed-order'.
    358 		(if org-archive-reversed-order
    359 		    (progn
    360 		      (goto-char (point-min))
    361 		      (unless (org-at-heading-p) (outline-next-heading)))
    362 		  (goto-char (point-max))
    363 		  ;; Subtree narrowing can let the buffer end on
    364 		  ;; a headline.  `org-paste-subtree' then deletes it.
    365 		  ;; To prevent this, make sure visible part of buffer
    366 		  ;; always terminates on a new line, while limiting
    367 		  ;; number of blank lines in a date tree.
    368 		  (unless (and datetree-date (bolp)) (insert "\n"))))
    369 	      ;; Paste
    370 	      (org-paste-subtree (org-get-valid-level level (and heading 1)))
    371 	      ;; Shall we append inherited tags?
    372 	      (and inherited-tags
    373 		   (or (and (eq org-archive-subtree-add-inherited-tags 'infile)
    374 			    infile-p)
    375 		       (eq org-archive-subtree-add-inherited-tags t))
    376 		   (org-set-tags all-tags))
    377 	      ;; Mark the entry as done
    378 	      (when (and org-archive-mark-done
    379 			 (let ((case-fold-search nil))
    380 			   (looking-at org-todo-line-regexp))
    381 			 (or (not (match-end 2))
    382 			     (not (member (match-string 2) org-done-keywords))))
    383 		(let (org-log-done org-todo-log-states)
    384 		  (org-todo
    385 		   (car (or (member org-archive-mark-done org-done-keywords)
    386 			    org-done-keywords)))))
    387 
    388 	      ;; Add the context info.
    389 	      (dolist (item org-archive-save-context-info)
    390 		(let ((value (cdr (assq item context))))
    391 		  (when (org-string-nw-p value)
    392 		    (org-entry-put
    393 		     (point)
    394 		     (concat "ARCHIVE_" (upcase (symbol-name item)))
    395 		     value))))
    396 	      ;; Save the buffer, if it is not the same buffer and
    397 	      ;; depending on `org-archive-subtree-save-file-p'.
    398 	      (unless (eq this-buffer buffer)
    399 		(when (or (eq org-archive-subtree-save-file-p t)
    400 			  (eq org-archive-subtree-save-file-p
    401 			      (if (boundp 'org-archive-from-agenda)
    402 				  'from-agenda
    403 				'from-org)))
    404 		  (save-buffer)))
    405 	      (widen))))
    406 	;; Here we are back in the original buffer.  Everything seems
    407 	;; to have worked.  So now run hooks, cut the tree and finish
    408 	;; up.
    409 	(run-hooks 'org-archive-hook)
    410 	(let (this-command) (org-cut-subtree))
    411 	(when (featurep 'org-inlinetask)
    412 	  (org-inlinetask-remove-END-maybe))
    413 	(setq org-markers-to-move nil)
    414 	(when org-provide-todo-statistics
    415 	  (save-excursion
    416 	    ;; Go to parent, even if no children exist.
    417 	    (org-up-heading-safe)
    418 	    ;; Update cookie of parent.
    419 	    (org-update-statistics-cookies nil)))
    420 	(message "Subtree archived %s"
    421 		 (if (eq this-buffer buffer)
    422 		     (concat "under heading: " heading)
    423 		   (concat "in file: " (abbreviate-file-name afile)))))))
    424     (org-fold-reveal)
    425     (if (looking-at "^[ \t]*$")
    426 	(outline-next-visible-heading 1))))
    427 
    428 ;;;###autoload
    429 (defun org-archive-to-archive-sibling ()
    430   "Archive the current heading by moving it under the archive sibling.
    431 
    432 The archive sibling is a sibling of the heading with the heading name
    433 `org-archive-sibling-heading' and an `org-archive-tag' tag.  If this
    434 sibling does not exist, it will be created at the end of the subtree.
    435 
    436 Archiving time is retained in the ARCHIVE_TIME node property."
    437   (interactive)
    438   (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
    439       (let ((cl (when (eq org-loop-over-headlines-in-active-region 'start-level)
    440 		  'region-start-level 'region))
    441 	    org-loop-over-headlines-in-active-region)
    442 	(org-map-entries
    443 	 '(progn (setq org-map-continue-from
    444 		       (progn (org-back-to-heading)
    445 			      (if (looking-at (concat "^.*:" org-archive-tag ":.*$"))
    446 				  (org-end-of-subtree t)
    447 				(point))))
    448 		 (when (org-at-heading-p)
    449 		   (org-archive-to-archive-sibling)))
    450 	 org-loop-over-headlines-in-active-region
    451 	 cl (if (org-invisible-p) (org-end-of-subtree nil t))))
    452     (save-restriction
    453       (widen)
    454       (let (b e pos leader level)
    455 	(org-back-to-heading t)
    456 	(looking-at org-outline-regexp)
    457 	(setq leader (match-string 0)
    458 	      level (funcall outline-level))
    459 	(setq pos (point-marker))
    460         ;; Advance POS upon insertion in front of it.
    461         (set-marker-insertion-type pos t)
    462 	(condition-case nil
    463 	    (outline-up-heading 1 t)
    464 	  (error (setq e (point-max)) (goto-char (point-min))))
    465 	(setq b (point))
    466 	(unless e
    467 	  (condition-case nil
    468 	      (org-end-of-subtree t t)
    469 	    (error (goto-char (point-max))))
    470 	  (setq e (point)))
    471 	(goto-char b)
    472 	(unless (re-search-forward
    473 		 (concat "^" (regexp-quote leader)
    474 			 "[ \t]*"
    475 			 org-archive-sibling-heading
    476 			 "[ \t]*:"
    477 			 org-archive-tag ":") e t)
    478 	  (goto-char e)
    479 	  (or (bolp) (newline))
    480 	  (insert leader org-archive-sibling-heading "\n")
    481 	  (forward-line -1)
    482 	  (org-toggle-tag org-archive-tag 'on))
    483 	(forward-line 0)
    484 	(if org-archive-reversed-order
    485 	    (outline-next-heading)
    486 	  (org-end-of-subtree t t))
    487 	(save-excursion
    488 	  (goto-char pos)
    489 	  (let ((this-command this-command)) (org-cut-subtree)))
    490 	(org-paste-subtree (org-get-valid-level level 1))
    491 	(org-set-property
    492 	 "ARCHIVE_TIME"
    493 	 (format-time-string
    494           (org-time-stamp-format 'with-time 'no-brackets)))
    495 	(outline-up-heading 1 t)
    496 	(org-fold-subtree t)
    497 	(org-cycle-show-empty-lines 'folded)
    498 	(when org-provide-todo-statistics
    499 	  ;; Update TODO statistics of parent.
    500 	  (org-update-parent-todo-statistics))
    501 	(goto-char pos)))
    502     (org-fold-reveal)
    503     (if (looking-at "^[ \t]*$")
    504 	(outline-next-visible-heading 1))))
    505 
    506 (defun org-archive-all-done (&optional tag)
    507   "Archive sublevels of the current tree without open TODO items.
    508 If the cursor is not on a headline, try all level 1 trees.  If
    509 it is on a headline, try all direct children.
    510 When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
    511   (org-archive-all-matches
    512    (lambda (_beg end)
    513      (let ((case-fold-search nil))
    514        (unless (re-search-forward org-not-done-heading-regexp end t)
    515 	 "no open TODO items")))
    516    tag))
    517 
    518 (defun org-archive-all-old (&optional tag)
    519   "Archive sublevels of the current tree with timestamps prior to today.
    520 If the cursor is not on a headline, try all level 1 trees.  If
    521 it is on a headline, try all direct children.
    522 When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
    523   (org-archive-all-matches
    524    (lambda (_beg end)
    525      (let (ts)
    526        (and (re-search-forward org-ts-regexp end t)
    527 	    (setq ts (match-string 0))
    528 	    (< (org-timestamp-to-now ts) 0)
    529 	    (if (not (looking-at
    530 		    (concat "--\\(" org-ts-regexp "\\)")))
    531 		(concat "old timestamp " ts)
    532 	      (setq ts (concat "old timestamp " ts (match-string 0)))
    533 	      (and (< (org-timestamp-to-now (match-string 1)) 0)
    534 		   ts)))))
    535    tag))
    536 
    537 (defun org-archive-all-matches (predicate &optional tag)
    538   "Archive sublevels of the current tree that match PREDICATE.
    539 
    540 PREDICATE is a function of two arguments, BEG and END, which
    541 specify the beginning and end of the headline being considered.
    542 It is called with point positioned at BEG.  The headline will be
    543 archived if PREDICATE returns non-nil.  If the return value of
    544 PREDICATE is a string, it should describe the reason for
    545 archiving the heading.
    546 
    547 If the cursor is not on a headline, try all level 1 trees.  If it
    548 is on a headline, try all direct children.  When TAG is non-nil,
    549 don't move trees, but mark them with the ARCHIVE tag."
    550   (let ((rea (concat ".*:" org-archive-tag ":")) re1
    551 	(begm (make-marker))
    552 	(endm (make-marker))
    553 	(question (if tag "Set ARCHIVE tag? "
    554 		    "Move subtree to archive? "))
    555 	reason beg end (cntarch 0))
    556     (if (org-at-heading-p)
    557 	(progn
    558 	  (setq re1 (concat "^" (regexp-quote
    559 				 (make-string
    560 				  (+ (- (match-end 0) (match-beginning 0) 1)
    561 				     (if org-odd-levels-only 2 1))
    562 				  ?*))
    563 			    " "))
    564 	  (move-marker begm (point))
    565 	  (move-marker endm (org-end-of-subtree t)))
    566       (setq re1 "^* ")
    567       (move-marker begm (point-min))
    568       (move-marker endm (point-max)))
    569     (save-excursion
    570       (goto-char begm)
    571       (while (re-search-forward re1 endm t)
    572 	(setq beg (match-beginning 0)
    573 	      end (save-excursion (org-end-of-subtree t) (point)))
    574 	(goto-char beg)
    575 	(if (not (setq reason (funcall predicate beg end)))
    576 	    (goto-char end)
    577 	  (goto-char beg)
    578 	  (if (and (or (not tag) (not (looking-at rea)))
    579 		   (y-or-n-p
    580 		    (if (stringp reason)
    581 			(concat question "(" reason ")")
    582 		      question)))
    583 	      (progn
    584 		(if tag
    585 		    (org-toggle-tag org-archive-tag 'on)
    586 		  (org-archive-subtree))
    587 		(setq cntarch (1+ cntarch)))
    588 	    (goto-char end)))))
    589     (message "%d trees archived" cntarch)))
    590 
    591 ;;;###autoload
    592 (defun org-toggle-archive-tag (&optional find-done)
    593   "Toggle the archive tag for the current headline.
    594 With prefix argument FIND-DONE, check all children of current headline
    595 and offer tagging the children that do not contain any open TODO
    596 items."
    597   (interactive "P")
    598   (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
    599       (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
    600 		    'region-start-level 'region))
    601 	    org-loop-over-headlines-in-active-region)
    602 	(org-map-entries
    603 	 `(org-toggle-archive-tag ,find-done)
    604 	 org-loop-over-headlines-in-active-region
    605 	 cl (if (org-invisible-p) (org-end-of-subtree nil t))))
    606     (if find-done
    607 	(org-archive-all-done 'tag)
    608       (let (set)
    609 	(save-excursion
    610 	  (org-back-to-heading t)
    611 	  (setq set (org-toggle-tag org-archive-tag))
    612 	  (when set (org-fold-subtree t)))
    613 	(and set (forward-line 0))
    614 	(message "Subtree %s" (if set "archived" "unarchived"))))))
    615 
    616 (defun org-archive-set-tag ()
    617   "Set the ARCHIVE tag."
    618   (interactive)
    619   (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
    620       (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
    621 		    'region-start-level 'region))
    622 	    org-loop-over-headlines-in-active-region)
    623 	(org-map-entries
    624 	 'org-archive-set-tag
    625 	 org-loop-over-headlines-in-active-region
    626 	 cl (if (org-invisible-p) (org-end-of-subtree nil t))))
    627     (org-toggle-tag org-archive-tag 'on)))
    628 
    629 ;;;###autoload
    630 (defun org-archive-subtree-default ()
    631   "Archive the current subtree with the default command.
    632 This command is set with the variable `org-archive-default-command'."
    633   (interactive)
    634   (call-interactively org-archive-default-command))
    635 
    636 ;;;###autoload
    637 (defun org-archive-subtree-default-with-confirmation ()
    638   "Archive the current subtree with the default command.
    639 This command is set with the variable `org-archive-default-command'."
    640   (interactive)
    641   (if (y-or-n-p "Archive this subtree or entry? ")
    642       (call-interactively org-archive-default-command)
    643     (error "Abort")))
    644 
    645 (provide 'org-archive)
    646 
    647 ;; Local variables:
    648 ;; generated-autoload-file: "org-loaddefs.el"
    649 ;; End:
    650 
    651 ;;; org-archive.el ends here