org-archive.el 23 KB

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