org-archive.el 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. ;;; org-archive.el --- Archiving for Org-mode
  2. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
  3. ;; Free Software Foundation, Inc.
  4. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;; Homepage: http://orgmode.org
  7. ;; Version: 7.6
  8. ;;
  9. ;; This file is part of GNU Emacs.
  10. ;;
  11. ;; GNU Emacs is free software: you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation, either version 3 of the License, or
  14. ;; (at your option) any later version.
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  21. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  22. ;;
  23. ;;; Commentary:
  24. ;; This file contains the face definitions for Org.
  25. ;;; Code:
  26. (require 'org)
  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. :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-subtree-add-inherited-tags 'infile
  61. "Non-nil means append inherited tags when archiving a subtree."
  62. :group 'org-archive
  63. :type '(choice
  64. (const :tag "Never" nil)
  65. (const :tag "When archiving a subtree to the same file" infile)
  66. (const :tag "Always" t)))
  67. (defcustom org-archive-save-context-info '(time file olpath category todo itags)
  68. "Parts of context info that should be stored as properties when archiving.
  69. When a subtree is moved to an archive file, it loses information given by
  70. context, like inherited tags, the category, and possibly also the TODO
  71. state (depending on the variable `org-archive-mark-done').
  72. This variable can be a list of any of the following symbols:
  73. time The time of archiving.
  74. file The file where the entry originates.
  75. ltags The local tags, in the headline of the subtree.
  76. itags The tags the subtree inherits from further up the hierarchy.
  77. todo The pre-archive TODO state.
  78. category The category, taken from file name or #+CATEGORY lines.
  79. olpath The outline path to the item. These are all headlines above
  80. the current item, separated by /, like a file path.
  81. For each symbol present in the list, a property will be created in
  82. the archived entry, with a prefix \"ARCHIVE_\", to remember this
  83. information."
  84. :group 'org-archive
  85. :type '(set :greedy t
  86. (const :tag "Time" time)
  87. (const :tag "File" file)
  88. (const :tag "Category" category)
  89. (const :tag "TODO state" todo)
  90. (const :tag "Priority" priority)
  91. (const :tag "Inherited tags" itags)
  92. (const :tag "Outline path" olpath)
  93. (const :tag "Local tags" ltags)))
  94. (defun org-get-local-archive-location ()
  95. "Get the archive location applicable at point."
  96. (let ((re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  97. prop)
  98. (save-excursion
  99. (save-restriction
  100. (widen)
  101. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  102. (cond
  103. ((and prop (string-match "\\S-" prop))
  104. prop)
  105. ((or (re-search-backward re nil t)
  106. (re-search-forward re nil t))
  107. (match-string 1))
  108. (t org-archive-location))))))
  109. (defun org-add-archive-files (files)
  110. "Splice the archive files into the list of files.
  111. This implies visiting all these files and finding out what the
  112. archive file is."
  113. (org-uniquify
  114. (apply
  115. 'append
  116. (mapcar
  117. (lambda (f)
  118. (if (not (file-exists-p f))
  119. nil
  120. (with-current-buffer (org-get-agenda-file-buffer f)
  121. (cons f (org-all-archive-files)))))
  122. files))))
  123. (defun org-all-archive-files ()
  124. "Get a list of all archive files used in the current buffer."
  125. (let (file files)
  126. (save-excursion
  127. (save-restriction
  128. (goto-char (point-min))
  129. (while (re-search-forward
  130. "^\\(#\\+\\|[ \t]*:\\)ARCHIVE:[ \t]+\\(.*\\)"
  131. nil t)
  132. (setq file (org-extract-archive-file
  133. (org-match-string-no-properties 2)))
  134. (and file (> (length file) 0) (file-exists-p file)
  135. (add-to-list 'files file)))))
  136. (setq files (nreverse files))
  137. (setq file (org-extract-archive-file))
  138. (and file (> (length file) 0) (file-exists-p file)
  139. (add-to-list 'files file))
  140. files))
  141. (defun org-extract-archive-file (&optional location)
  142. "Extract and expand the file name from archive LOCATION.
  143. if LOCATION is not given, the value of `org-archive-location' is used."
  144. (setq location (or location org-archive-location))
  145. (if (string-match "\\(.*\\)::\\(.*\\)" location)
  146. (if (= (match-beginning 1) (match-end 1))
  147. (buffer-file-name (buffer-base-buffer))
  148. (expand-file-name
  149. (format (match-string 1 location)
  150. (file-name-nondirectory
  151. (buffer-file-name (buffer-base-buffer))))))))
  152. (defun org-extract-archive-heading (&optional location)
  153. "Extract the heading from archive LOCATION.
  154. if LOCATION is not given, the value of `org-archive-location' is used."
  155. (setq location (or location org-archive-location))
  156. (if (string-match "\\(.*\\)::\\(.*\\)" location)
  157. (format (match-string 2 location)
  158. (file-name-nondirectory
  159. (buffer-file-name (buffer-base-buffer))))))
  160. (defun org-archive-subtree (&optional find-done)
  161. "Move the current subtree to the archive.
  162. The archive can be a certain top-level heading in the current file, or in
  163. a different file. The tree will be moved to that location, the subtree
  164. heading be marked DONE, and the current time will be added.
  165. When called with prefix argument FIND-DONE, find whole trees without any
  166. open TODO items and archive them (after getting confirmation from the user).
  167. If the cursor is not at a headline when this command is called, try all level
  168. 1 trees. If the cursor is on a headline, only try the direct children of
  169. this heading."
  170. (interactive "P")
  171. (org-loop-over-siblings-in-active-region
  172. (if find-done
  173. (org-archive-all-done)
  174. ;; Save all relevant TODO keyword-relatex variables
  175. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  176. (tr-org-todo-keywords-1 org-todo-keywords-1)
  177. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  178. (tr-org-done-keywords org-done-keywords)
  179. (tr-org-todo-regexp org-todo-regexp)
  180. (tr-org-todo-line-regexp org-todo-line-regexp)
  181. (tr-org-odd-levels-only org-odd-levels-only)
  182. (this-buffer (current-buffer))
  183. ;; start of variables that will be used for saving context
  184. ;; The compiler complains about them - keep them anyway!
  185. (file (abbreviate-file-name
  186. (or (buffer-file-name (buffer-base-buffer))
  187. (error "No file associated to buffer"))))
  188. (olpath (mapconcat 'identity (org-get-outline-path) "/"))
  189. (time (format-time-string
  190. (substring (cdr org-time-stamp-formats) 1 -1)
  191. (current-time)))
  192. category todo priority ltags itags atags
  193. ;; end of variables that will be used for saving context
  194. location afile heading buffer level newfile-p infile-p visiting)
  195. ;; Find the local archive location
  196. (setq location (org-get-local-archive-location)
  197. afile (org-extract-archive-file location)
  198. heading (org-extract-archive-heading location)
  199. infile-p (equal file (abbreviate-file-name afile)))
  200. (unless afile
  201. (error "Invalid `org-archive-location'"))
  202. (if (> (length afile) 0)
  203. (setq newfile-p (not (file-exists-p afile))
  204. visiting (find-buffer-visiting afile)
  205. buffer (or visiting (find-file-noselect afile)))
  206. (setq buffer (current-buffer)))
  207. (unless buffer
  208. (error "Cannot access file \"%s\"" afile))
  209. (if (and (> (length heading) 0)
  210. (string-match "^\\*+" heading))
  211. (setq level (match-end 0))
  212. (setq heading nil level 0))
  213. (save-excursion
  214. (org-back-to-heading t)
  215. ;; Get context information that will be lost by moving the tree
  216. (setq category (org-get-category nil 'force-refresh)
  217. todo (and (looking-at org-todo-line-regexp)
  218. (match-string 2))
  219. priority (org-get-priority
  220. (if (match-end 3) (match-string 3) ""))
  221. ltags (org-get-tags)
  222. itags (org-delete-all ltags (org-get-tags-at))
  223. atags (org-get-tags-at))
  224. (setq ltags (mapconcat 'identity ltags " ")
  225. itags (mapconcat 'identity itags " "))
  226. ;; We first only copy, in case something goes wrong
  227. ;; we need to protect `this-command', to avoid kill-region sets it,
  228. ;; which would lead to duplication of subtrees
  229. (let (this-command) (org-copy-subtree 1 nil t))
  230. (set-buffer buffer)
  231. ;; Enforce org-mode for the archive buffer
  232. (if (not (org-mode-p))
  233. ;; Force the mode for future visits.
  234. (let ((org-insert-mode-line-in-empty-file t)
  235. (org-inhibit-startup t))
  236. (call-interactively 'org-mode)))
  237. (when newfile-p
  238. (goto-char (point-max))
  239. (insert (format "\nArchived entries from file %s\n\n"
  240. (buffer-file-name this-buffer))))
  241. ;; Force the TODO keywords of the original buffer
  242. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  243. (org-todo-keywords-1 tr-org-todo-keywords-1)
  244. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  245. (org-done-keywords tr-org-done-keywords)
  246. (org-todo-regexp tr-org-todo-regexp)
  247. (org-todo-line-regexp tr-org-todo-line-regexp)
  248. (org-odd-levels-only
  249. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  250. org-odd-levels-only
  251. tr-org-odd-levels-only)))
  252. (goto-char (point-min))
  253. (show-all)
  254. (if heading
  255. (progn
  256. (if (re-search-forward
  257. (concat "^" (regexp-quote heading)
  258. (org-re "[ \t]*\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\($\\|\r\\)"))
  259. nil t)
  260. (goto-char (match-end 0))
  261. ;; Heading not found, just insert it at the end
  262. (goto-char (point-max))
  263. (or (bolp) (insert "\n"))
  264. (insert "\n" heading "\n")
  265. (end-of-line 0))
  266. ;; Make the subtree visible
  267. (show-subtree)
  268. (if org-archive-reversed-order
  269. (progn
  270. (org-back-to-heading t)
  271. (outline-next-heading))
  272. (org-end-of-subtree t))
  273. (skip-chars-backward " \t\r\n")
  274. (and (looking-at "[ \t\r\n]*")
  275. (replace-match "\n\n")))
  276. ;; No specific heading, just go to end of file.
  277. (goto-char (point-max)) (insert "\n"))
  278. ;; Paste
  279. (org-paste-subtree (org-get-valid-level level (and heading 1)))
  280. ;; Shall we append inherited tags?
  281. (and itags
  282. (or (and (eq org-archive-subtree-add-inherited-tags 'infile)
  283. infile-p)
  284. (eq org-archive-subtree-add-inherited-tags t))
  285. (org-set-tags-to atags))
  286. ;; Mark the entry as done
  287. (when (and org-archive-mark-done
  288. (looking-at org-todo-line-regexp)
  289. (or (not (match-end 2))
  290. (not (member (match-string 2) org-done-keywords))))
  291. (let (org-log-done org-todo-log-states)
  292. (org-todo
  293. (car (or (member org-archive-mark-done org-done-keywords)
  294. org-done-keywords)))))
  295. ;; Add the context info
  296. (when org-archive-save-context-info
  297. (let ((l org-archive-save-context-info) e n v)
  298. (while (setq e (pop l))
  299. (when (and (setq v (symbol-value e))
  300. (stringp v) (string-match "\\S-" v))
  301. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  302. (org-entry-put (point) n v)))))
  303. ;; Save and kill the buffer, if it is not the same buffer.
  304. (when (not (eq this-buffer buffer))
  305. (save-buffer))))
  306. ;; Here we are back in the original buffer. Everything seems to have
  307. ;; worked. So now cut the tree and finish up.
  308. (let (this-command) (org-cut-subtree))
  309. (when (featurep 'org-inlinetask)
  310. (org-inlinetask-remove-END-maybe))
  311. (setq org-markers-to-move nil)
  312. (message "Subtree archived %s"
  313. (if (eq this-buffer buffer)
  314. (concat "under heading: " heading)
  315. (concat "in file: " (abbreviate-file-name afile))))))
  316. (org-reveal)
  317. (if (looking-at "^[ \t]*$")
  318. (outline-next-visible-heading 1))))
  319. (defun org-archive-to-archive-sibling ()
  320. "Archive the current heading by moving it under the archive sibling.
  321. The archive sibling is a sibling of the heading with the heading name
  322. `org-archive-sibling-heading' and an `org-archive-tag' tag. If this
  323. sibling does not exist, it will be created at the end of the subtree."
  324. (interactive)
  325. (org-loop-over-siblings-in-active-region
  326. (save-restriction
  327. (widen)
  328. (let (b e pos leader level)
  329. (org-back-to-heading t)
  330. (looking-at outline-regexp)
  331. (setq leader (match-string 0)
  332. level (funcall outline-level))
  333. (setq pos (point))
  334. (condition-case nil
  335. (outline-up-heading 1 t)
  336. (error (setq e (point-max)) (goto-char (point-min))))
  337. (setq b (point))
  338. (unless e
  339. (condition-case nil
  340. (org-end-of-subtree t t)
  341. (error (goto-char (point-max))))
  342. (setq e (point)))
  343. (goto-char b)
  344. (unless (re-search-forward
  345. (concat "^" (regexp-quote leader)
  346. "[ \t]*"
  347. org-archive-sibling-heading
  348. "[ \t]*:"
  349. org-archive-tag ":") e t)
  350. (goto-char e)
  351. (or (bolp) (newline))
  352. (insert leader org-archive-sibling-heading "\n")
  353. (beginning-of-line 0)
  354. (org-toggle-tag org-archive-tag 'on))
  355. (beginning-of-line 1)
  356. (if org-archive-reversed-order
  357. (outline-next-heading)
  358. (org-end-of-subtree t t))
  359. (save-excursion
  360. (goto-char pos)
  361. (let ((this-command this-command)) (org-cut-subtree)))
  362. (org-paste-subtree (org-get-valid-level level 1))
  363. (org-set-property
  364. "ARCHIVE_TIME"
  365. (format-time-string
  366. (substring (cdr org-time-stamp-formats) 1 -1)
  367. (current-time)))
  368. (outline-up-heading 1 t)
  369. (hide-subtree)
  370. (org-cycle-show-empty-lines 'folded)
  371. (goto-char pos)))
  372. (org-reveal)
  373. (if (looking-at "^[ \t]*$")
  374. (outline-next-visible-heading 1))))
  375. (defun org-archive-all-done (&optional tag)
  376. "Archive sublevels of the current tree without open TODO items.
  377. If the cursor is not on a headline, try all level 1 trees. If
  378. it is on a headline, try all direct children.
  379. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  380. (let ((re (concat org-outline-regexp-bol "+" org-not-done-regexp)) re1
  381. (rea (concat ".*:" org-archive-tag ":"))
  382. (begm (make-marker))
  383. (endm (make-marker))
  384. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  385. "Move subtree to archive (no open TODO items)? "))
  386. beg end (cntarch 0))
  387. (if (org-on-heading-p)
  388. (progn
  389. (setq re1 (concat "^" (regexp-quote
  390. (make-string
  391. (+ (- (match-end 0) (match-beginning 0) 1)
  392. (if org-odd-levels-only 2 1))
  393. ?*))
  394. " "))
  395. (move-marker begm (point))
  396. (move-marker endm (org-end-of-subtree t)))
  397. (setq re1 "^* ")
  398. (move-marker begm (point-min))
  399. (move-marker endm (point-max)))
  400. (save-excursion
  401. (goto-char begm)
  402. (while (re-search-forward re1 endm t)
  403. (setq beg (match-beginning 0)
  404. end (save-excursion (org-end-of-subtree t) (point)))
  405. (goto-char beg)
  406. (if (re-search-forward re end t)
  407. (goto-char end)
  408. (goto-char beg)
  409. (if (and (or (not tag) (not (looking-at rea)))
  410. (y-or-n-p question))
  411. (progn
  412. (if tag
  413. (org-toggle-tag org-archive-tag 'on)
  414. (org-archive-subtree))
  415. (setq cntarch (1+ cntarch)))
  416. (goto-char end)))))
  417. (message "%d trees archived" cntarch)))
  418. (defun org-toggle-archive-tag (&optional find-done)
  419. "Toggle the archive tag for the current headline.
  420. With prefix ARG, check all children of current headline and offer tagging
  421. the children that do not contain any open TODO items."
  422. (interactive "P")
  423. (org-loop-over-siblings-in-active-region
  424. (if find-done
  425. (org-archive-all-done 'tag)
  426. (let (set)
  427. (save-excursion
  428. (org-back-to-heading t)
  429. (setq set (org-toggle-tag org-archive-tag))
  430. (when set (hide-subtree)))
  431. (and set (beginning-of-line 1))
  432. (message "Subtree %s" (if set "archived" "unarchived"))))))
  433. (defun org-archive-set-tag ()
  434. "Set the ARCHIVE tag."
  435. (interactive)
  436. (org-loop-over-siblings-in-active-region
  437. (org-toggle-tag org-archive-tag 'on)))
  438. ;;;###autoload
  439. (defun org-archive-subtree-default ()
  440. "Archive the current subtree with the default command.
  441. This command is set with the variable `org-archive-default-command'."
  442. (interactive)
  443. (call-interactively org-archive-default-command))
  444. ;;;###autoload
  445. (defun org-archive-subtree-default-with-confirmation ()
  446. "Archive the current subtree with the default command.
  447. This command is set with the variable `org-archive-default-command'."
  448. (interactive)
  449. (if (y-or-n-p "Archive this subtree or entry? ")
  450. (call-interactively org-archive-default-command)
  451. (error "Abort")))
  452. (provide 'org-archive)
  453. ;; arch-tag: 0837f601-9699-43c3-8b90-631572ae6c85
  454. ;;; org-archive.el ends here