org-archive.el 24 KB

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