org-archive.el 24 KB

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