org-archive.el 24 KB

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