org-archive.el 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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. "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. level datetree-date datetree-subheading-p)
  226. (when (string-match "\\`datetree/" heading)
  227. ;; Replace with ***, to represent the 3 levels of headings the
  228. ;; datetree has.
  229. (setq heading (replace-regexp-in-string "\\`datetree/" "***" heading))
  230. (setq datetree-subheading-p (> (length heading) 3))
  231. (setq datetree-date (org-date-to-gregorian
  232. (or (org-entry-get nil "CLOSED" t) time))))
  233. (if (and (> (length heading) 0)
  234. (string-match "^\\*+" heading))
  235. (setq level (match-end 0))
  236. (setq heading nil level 0))
  237. (save-excursion
  238. (org-back-to-heading t)
  239. ;; Get context information that will be lost by moving the
  240. ;; tree. See `org-archive-save-context-info'.
  241. (let* ((all-tags (org-get-tags))
  242. (local-tags
  243. (cl-remove-if (lambda (tag)
  244. (get-text-property 0 'inherited tag))
  245. all-tags))
  246. (inherited-tags
  247. (cl-remove-if-not (lambda (tag)
  248. (get-text-property 0 'inherited tag))
  249. all-tags))
  250. (context
  251. `((category . ,(org-get-category nil 'force-refresh))
  252. (file . ,file)
  253. (itags . ,(mapconcat #'identity inherited-tags " "))
  254. (ltags . ,(mapconcat #'identity local-tags " "))
  255. (olpath . ,(mapconcat #'identity
  256. (org-get-outline-path)
  257. "/"))
  258. (time . ,time)
  259. (todo . ,(org-entry-get (point) "TODO")))))
  260. ;; We first only copy, in case something goes wrong
  261. ;; we need to protect `this-command', to avoid kill-region sets it,
  262. ;; which would lead to duplication of subtrees
  263. (let (this-command) (org-copy-subtree 1 nil t))
  264. (set-buffer buffer)
  265. ;; Enforce Org mode for the archive buffer
  266. (if (not (derived-mode-p 'org-mode))
  267. ;; Force the mode for future visits.
  268. (let ((org-insert-mode-line-in-empty-file t)
  269. (org-inhibit-startup t))
  270. (call-interactively 'org-mode)))
  271. (when (and newfile-p org-archive-file-header-format)
  272. (goto-char (point-max))
  273. (insert (format org-archive-file-header-format
  274. (buffer-file-name this-buffer))))
  275. (when datetree-date
  276. (require 'org-datetree)
  277. (org-datetree-find-date-create datetree-date)
  278. (org-narrow-to-subtree))
  279. ;; Force the TODO keywords of the original buffer
  280. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  281. (org-todo-keywords-1 tr-org-todo-keywords-1)
  282. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  283. (org-done-keywords tr-org-done-keywords)
  284. (org-todo-regexp tr-org-todo-regexp)
  285. (org-todo-line-regexp tr-org-todo-line-regexp)
  286. (org-odd-levels-only
  287. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  288. org-odd-levels-only
  289. tr-org-odd-levels-only)))
  290. (goto-char (point-min))
  291. (org-show-all '(headings blocks))
  292. (if (and heading (not (and datetree-date (not datetree-subheading-p))))
  293. (progn
  294. (if (re-search-forward
  295. (concat "^" (regexp-quote heading)
  296. "\\([ \t]+:\\(" org-tag-re ":\\)+\\)?[ \t]*$")
  297. nil t)
  298. (goto-char (match-end 0))
  299. ;; Heading not found, just insert it at the end
  300. (goto-char (point-max))
  301. (or (bolp) (insert "\n"))
  302. ;; datetrees don't need too much spacing
  303. (insert (if datetree-date "" "\n") heading "\n")
  304. (end-of-line 0))
  305. ;; Make the subtree visible
  306. (outline-show-subtree)
  307. (if org-archive-reversed-order
  308. (progn
  309. (org-back-to-heading t)
  310. (outline-next-heading))
  311. (org-end-of-subtree t))
  312. (skip-chars-backward " \t\r\n")
  313. (and (looking-at "[ \t\r\n]*")
  314. ;; datetree archives don't need so much spacing.
  315. (replace-match (if datetree-date "\n" "\n\n"))))
  316. ;; No specific heading, just go to end of file, or to the
  317. ;; beginning, depending on `org-archive-reversed-order'.
  318. (if org-archive-reversed-order
  319. (progn
  320. (goto-char (point-min))
  321. (unless (org-at-heading-p) (outline-next-heading)))
  322. (goto-char (point-max))
  323. ;; Subtree narrowing can let the buffer end on
  324. ;; a headline. `org-paste-subtree' then deletes it.
  325. ;; To prevent this, make sure visible part of buffer
  326. ;; always terminates on a new line, while limiting
  327. ;; number of blank lines in a date tree.
  328. (unless (and datetree-date (bolp)) (insert "\n"))))
  329. ;; Paste
  330. (org-paste-subtree (org-get-valid-level level (and heading 1)))
  331. ;; Shall we append inherited tags?
  332. (and inherited-tags
  333. (or (and (eq org-archive-subtree-add-inherited-tags 'infile)
  334. infile-p)
  335. (eq org-archive-subtree-add-inherited-tags t))
  336. (org-set-tags all-tags))
  337. ;; Mark the entry as done
  338. (when (and org-archive-mark-done
  339. (let ((case-fold-search nil))
  340. (looking-at org-todo-line-regexp))
  341. (or (not (match-end 2))
  342. (not (member (match-string 2) org-done-keywords))))
  343. (let (org-log-done org-todo-log-states)
  344. (org-todo
  345. (car (or (member org-archive-mark-done org-done-keywords)
  346. org-done-keywords)))))
  347. ;; Add the context info.
  348. (dolist (item org-archive-save-context-info)
  349. (let ((value (cdr (assq item context))))
  350. (when (org-string-nw-p value)
  351. (org-entry-put
  352. (point)
  353. (concat "ARCHIVE_" (upcase (symbol-name item)))
  354. value))))
  355. ;; Save the buffer, if it is not the same buffer and
  356. ;; depending on `org-archive-subtree-save-file-p'.
  357. (unless (eq this-buffer buffer)
  358. (when (or (eq org-archive-subtree-save-file-p t)
  359. (eq org-archive-subtree-save-file-p
  360. (if (boundp 'org-archive-from-agenda)
  361. 'from-agenda
  362. 'from-org)))
  363. (save-buffer)))
  364. (widen))))
  365. ;; Here we are back in the original buffer. Everything seems
  366. ;; to have worked. So now run hooks, cut the tree and finish
  367. ;; up.
  368. (run-hooks 'org-archive-hook)
  369. (let (this-command) (org-cut-subtree))
  370. (when (featurep 'org-inlinetask)
  371. (org-inlinetask-remove-END-maybe))
  372. (setq org-markers-to-move nil)
  373. (when org-provide-todo-statistics
  374. (save-excursion
  375. ;; Go to parent, even if no children exist.
  376. (org-up-heading-safe)
  377. ;; Update cookie of parent.
  378. (org-update-statistics-cookies nil)))
  379. (message "Subtree archived %s"
  380. (if (eq this-buffer buffer)
  381. (concat "under heading: " heading)
  382. (concat "in file: " (abbreviate-file-name afile)))))))
  383. (org-reveal)
  384. (if (looking-at "^[ \t]*$")
  385. (outline-next-visible-heading 1))))
  386. ;;;###autoload
  387. (defun org-archive-to-archive-sibling ()
  388. "Archive the current heading by moving it under the archive sibling.
  389. The archive sibling is a sibling of the heading with the heading name
  390. `org-archive-sibling-heading' and an `org-archive-tag' tag. If this
  391. sibling does not exist, it will be created at the end of the subtree.
  392. Archiving time is retained in the ARCHIVE_TIME node property."
  393. (interactive)
  394. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  395. (let ((cl (when (eq org-loop-over-headlines-in-active-region 'start-level)
  396. 'region-start-level 'region))
  397. org-loop-over-headlines-in-active-region)
  398. (org-map-entries
  399. '(progn (setq org-map-continue-from
  400. (progn (org-back-to-heading)
  401. (if (looking-at (concat "^.*:" org-archive-tag ":.*$"))
  402. (org-end-of-subtree t)
  403. (point))))
  404. (when (org-at-heading-p)
  405. (org-archive-to-archive-sibling)))
  406. org-loop-over-headlines-in-active-region
  407. cl (if (org-invisible-p) (org-end-of-subtree nil t))))
  408. (save-restriction
  409. (widen)
  410. (let (b e pos leader level)
  411. (org-back-to-heading t)
  412. (looking-at org-outline-regexp)
  413. (setq leader (match-string 0)
  414. level (funcall outline-level))
  415. (setq pos (point-marker))
  416. (condition-case nil
  417. (outline-up-heading 1 t)
  418. (error (setq e (point-max)) (goto-char (point-min))))
  419. (setq b (point))
  420. (unless e
  421. (condition-case nil
  422. (org-end-of-subtree t t)
  423. (error (goto-char (point-max))))
  424. (setq e (point)))
  425. (goto-char b)
  426. (unless (re-search-forward
  427. (concat "^" (regexp-quote leader)
  428. "[ \t]*"
  429. org-archive-sibling-heading
  430. "[ \t]*:"
  431. org-archive-tag ":") e t)
  432. (goto-char e)
  433. (or (bolp) (newline))
  434. (insert leader org-archive-sibling-heading "\n")
  435. (beginning-of-line 0)
  436. (org-toggle-tag org-archive-tag 'on))
  437. (beginning-of-line 1)
  438. (if org-archive-reversed-order
  439. (outline-next-heading)
  440. (org-end-of-subtree t t))
  441. (save-excursion
  442. (goto-char pos)
  443. (let ((this-command this-command)) (org-cut-subtree)))
  444. (org-paste-subtree (org-get-valid-level level 1))
  445. (org-set-property
  446. "ARCHIVE_TIME"
  447. (format-time-string
  448. (substring (cdr org-time-stamp-formats) 1 -1)))
  449. (outline-up-heading 1 t)
  450. (org-flag-subtree t)
  451. (org-cycle-show-empty-lines 'folded)
  452. (when org-provide-todo-statistics
  453. ;; Update TODO statistics of parent.
  454. (org-update-parent-todo-statistics))
  455. (goto-char pos)))
  456. (org-reveal)
  457. (if (looking-at "^[ \t]*$")
  458. (outline-next-visible-heading 1))))
  459. (defun org-archive-all-done (&optional tag)
  460. "Archive sublevels of the current tree without open TODO items.
  461. If the cursor is not on a headline, try all level 1 trees. If
  462. it is on a headline, try all direct children.
  463. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  464. (org-archive-all-matches
  465. (lambda (_beg end)
  466. (let ((case-fold-search nil))
  467. (unless (re-search-forward org-not-done-heading-regexp end t)
  468. "no open TODO items")))
  469. tag))
  470. (defun org-archive-all-old (&optional tag)
  471. "Archive sublevels of the current tree with timestamps prior to today.
  472. If the cursor is not on a headline, try all level 1 trees. If
  473. it is on a headline, try all direct children.
  474. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  475. (org-archive-all-matches
  476. (lambda (_beg end)
  477. (let (ts)
  478. (and (re-search-forward org-ts-regexp end t)
  479. (setq ts (match-string 0))
  480. (< (org-time-stamp-to-now ts) 0)
  481. (if (not (looking-at
  482. (concat "--\\(" org-ts-regexp "\\)")))
  483. (concat "old timestamp " ts)
  484. (setq ts (concat "old timestamp " ts (match-string 0)))
  485. (and (< (org-time-stamp-to-now (match-string 1)) 0)
  486. ts)))))
  487. tag))
  488. (defun org-archive-all-matches (predicate &optional tag)
  489. "Archive sublevels of the current tree that match PREDICATE.
  490. PREDICATE is a function of two arguments, BEG and END, which
  491. specify the beginning and end of the headline being considered.
  492. It is called with point positioned at BEG. The headline will be
  493. archived if PREDICATE returns non-nil. If the return value of
  494. PREDICATE is a string, it should describe the reason for
  495. archiving the heading.
  496. If the cursor is not on a headline, try all level 1 trees. If it
  497. is on a headline, try all direct children. When TAG is non-nil,
  498. don't move trees, but mark them with the ARCHIVE tag."
  499. (let ((rea (concat ".*:" org-archive-tag ":")) re1
  500. (begm (make-marker))
  501. (endm (make-marker))
  502. (question (if tag "Set ARCHIVE tag? "
  503. "Move subtree to archive? "))
  504. reason beg end (cntarch 0))
  505. (if (org-at-heading-p)
  506. (progn
  507. (setq re1 (concat "^" (regexp-quote
  508. (make-string
  509. (+ (- (match-end 0) (match-beginning 0) 1)
  510. (if org-odd-levels-only 2 1))
  511. ?*))
  512. " "))
  513. (move-marker begm (point))
  514. (move-marker endm (org-end-of-subtree t)))
  515. (setq re1 "^* ")
  516. (move-marker begm (point-min))
  517. (move-marker endm (point-max)))
  518. (save-excursion
  519. (goto-char begm)
  520. (while (re-search-forward re1 endm t)
  521. (setq beg (match-beginning 0)
  522. end (save-excursion (org-end-of-subtree t) (point)))
  523. (goto-char beg)
  524. (if (not (setq reason (funcall predicate beg end)))
  525. (goto-char end)
  526. (goto-char beg)
  527. (if (and (or (not tag) (not (looking-at rea)))
  528. (y-or-n-p
  529. (if (stringp reason)
  530. (concat question "(" reason ")")
  531. question)))
  532. (progn
  533. (if tag
  534. (org-toggle-tag org-archive-tag 'on)
  535. (org-archive-subtree))
  536. (setq cntarch (1+ cntarch)))
  537. (goto-char end)))))
  538. (message "%d trees archived" cntarch)))
  539. ;;;###autoload
  540. (defun org-toggle-archive-tag (&optional find-done)
  541. "Toggle the archive tag for the current headline.
  542. With prefix ARG, check all children of current headline and offer tagging
  543. the children that do not contain any open TODO items."
  544. (interactive "P")
  545. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  546. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  547. 'region-start-level 'region))
  548. org-loop-over-headlines-in-active-region)
  549. (org-map-entries
  550. `(org-toggle-archive-tag ,find-done)
  551. org-loop-over-headlines-in-active-region
  552. cl (if (org-invisible-p) (org-end-of-subtree nil t))))
  553. (if find-done
  554. (org-archive-all-done 'tag)
  555. (let (set)
  556. (save-excursion
  557. (org-back-to-heading t)
  558. (setq set (org-toggle-tag org-archive-tag))
  559. (when set (org-flag-subtree t)))
  560. (and set (beginning-of-line 1))
  561. (message "Subtree %s" (if set "archived" "unarchived"))))))
  562. (defun org-archive-set-tag ()
  563. "Set the ARCHIVE tag."
  564. (interactive)
  565. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  566. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  567. 'region-start-level 'region))
  568. org-loop-over-headlines-in-active-region)
  569. (org-map-entries
  570. 'org-archive-set-tag
  571. org-loop-over-headlines-in-active-region
  572. cl (if (org-invisible-p) (org-end-of-subtree nil t))))
  573. (org-toggle-tag org-archive-tag 'on)))
  574. ;;;###autoload
  575. (defun org-archive-subtree-default ()
  576. "Archive the current subtree with the default command.
  577. This command is set with the variable `org-archive-default-command'."
  578. (interactive)
  579. (call-interactively org-archive-default-command))
  580. ;;;###autoload
  581. (defun org-archive-subtree-default-with-confirmation ()
  582. "Archive the current subtree with the default command.
  583. This command is set with the variable `org-archive-default-command'."
  584. (interactive)
  585. (if (y-or-n-p "Archive this subtree or entry? ")
  586. (call-interactively org-archive-default-command)
  587. (error "Abort")))
  588. (provide 'org-archive)
  589. ;; Local variables:
  590. ;; generated-autoload-file: "org-loaddefs.el"
  591. ;; End:
  592. ;;; org-archive.el ends here