org-archive.el 22 KB

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