org-archive.el 23 KB

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