org-archive.el 22 KB

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