org-archive.el 23 KB

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