org-archive.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. ;;; org-archive.el --- Archiving for Org-mode
  2. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 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. ;; Version: 6.01c
  7. ;;
  8. ;; This file is part of GNU Emacs.
  9. ;;
  10. ;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 3, or (at your option)
  13. ;; any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;; This file contains the face definitons for Org.
  26. ;;; Code:
  27. (require 'org)
  28. (defcustom org-archive-location "%s_archive::"
  29. "The location where subtrees should be archived.
  30. Otherwise, the value of this variable is a string, consisting of two
  31. parts, separated by a double-colon.
  32. The first part is a file name - when omitted, archiving happens in the same
  33. file. %s will be replaced by the current file name (without directory part).
  34. Archiving to a different file is useful to keep archived entries from
  35. contributing to the Org-mode Agenda.
  36. The part after the double colon is a headline. The archived entries will be
  37. filed under that headline. When omitted, the subtrees are simply filed away
  38. at the end of the file, as top-level entries.
  39. Here are a few examples:
  40. \"%s_archive::\"
  41. If the current file is Projects.org, archive in file
  42. Projects.org_archive, as top-level trees. This is the default.
  43. \"::* Archived Tasks\"
  44. Archive in the current file, under the top-level headline
  45. \"* Archived Tasks\".
  46. \"~/org/archive.org::\"
  47. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  48. \"basement::** Finished Tasks\"
  49. Archive in file ./basement (relative path), as level 3 trees
  50. below the level 2 heading \"** Finished Tasks\".
  51. You may set this option on a per-file basis by adding to the buffer a
  52. line like
  53. #+ARCHIVE: basement::** Finished Tasks
  54. You may also define it locally for a subtree by setting an ARCHIVE property
  55. in the entry. If such a property is found in an entry, or anywhere up
  56. the hierarchy, it will be used."
  57. :group 'org-archive
  58. :type 'string)
  59. (defcustom org-archive-sibling-heading "Archive"
  60. "Name of the local archive sibling that is used to archive entries locally.
  61. Locally means: in the tree, under a sibling.
  62. See `org-archive-to-archive-sibling' for more information."
  63. :group 'org-archive
  64. :type 'string)
  65. (defcustom org-archive-mark-done t
  66. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  67. This can be a string to set the keyword to use. When t, Org-mode will
  68. use the first keyword in its list that means done."
  69. :group 'org-archive
  70. :type '(choice
  71. (const :tag "No" nil)
  72. (const :tag "Yes" t)
  73. (string :tag "Use this keyword")))
  74. (defcustom org-archive-stamp-time t
  75. "Non-nil means, add a time stamp to entries moved to an archive file.
  76. This variable is obsolete and has no effect anymore, instead add ot remove
  77. `time' from the variablle `org-archive-save-context-info'."
  78. :group 'org-archive
  79. :type 'boolean)
  80. (defcustom org-archive-save-context-info '(time file olpath category todo itags)
  81. "Parts of context info that should be stored as properties when archiving.
  82. When a subtree is moved to an archive file, it looses information given by
  83. context, like inherited tags, the category, and possibly also the TODO
  84. state (depending on the variable `org-archive-mark-done').
  85. This variable can be a list of any of the following symbols:
  86. time The time of archiving.
  87. file The file where the entry originates.
  88. itags The local tags, in the headline of the subtree.
  89. ltags The tags the subtree inherits from further up the hierarchy.
  90. todo The pre-archive TODO state.
  91. category The category, taken from file name or #+CATEGORY lines.
  92. olpath The outline path to the item. These are all headlines above
  93. the current item, separated by /, like a file path.
  94. For each symbol present in the list, a property will be created in
  95. the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
  96. information."
  97. :group 'org-archive
  98. :type '(set :greedy t
  99. (const :tag "Time" time)
  100. (const :tag "File" file)
  101. (const :tag "Category" category)
  102. (const :tag "TODO state" todo)
  103. (const :tag "TODO state" priority)
  104. (const :tag "Inherited tags" itags)
  105. (const :tag "Outline path" olpath)
  106. (const :tag "Local tags" ltags)))
  107. (defun org-archive-subtree (&optional find-done)
  108. "Move the current subtree to the archive.
  109. The archive can be a certain top-level heading in the current file, or in
  110. a different file. The tree will be moved to that location, the subtree
  111. heading be marked DONE, and the current time will be added.
  112. When called with prefix argument FIND-DONE, find whole trees without any
  113. open TODO items and archive them (after getting confirmation from the user).
  114. If the cursor is not at a headline when this comand is called, try all level
  115. 1 trees. If the cursor is on a headline, only try the direct children of
  116. this heading."
  117. (interactive "P")
  118. (if find-done
  119. (org-archive-all-done)
  120. ;; Save all relevant TODO keyword-relatex variables
  121. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  122. (tr-org-todo-keywords-1 org-todo-keywords-1)
  123. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  124. (tr-org-done-keywords org-done-keywords)
  125. (tr-org-todo-regexp org-todo-regexp)
  126. (tr-org-todo-line-regexp org-todo-line-regexp)
  127. (tr-org-odd-levels-only org-odd-levels-only)
  128. (this-buffer (current-buffer))
  129. (org-archive-location org-archive-location)
  130. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  131. ;; start of variables that will be used for saving context
  132. ;; The compiler complains about them - keep them anyway!
  133. (file (abbreviate-file-name (buffer-file-name)))
  134. (olpath (mapconcat 'identity (org-get-outline-path) "/"))
  135. (time (format-time-string
  136. (substring (cdr org-time-stamp-formats) 1 -1)
  137. (current-time)))
  138. afile heading buffer level newfile-p
  139. category todo priority
  140. ;; start of variables that will be used for savind context
  141. ltags itags prop)
  142. ;; Try to find a local archive location
  143. (save-excursion
  144. (save-restriction
  145. (widen)
  146. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  147. (if (and prop (string-match "\\S-" prop))
  148. (setq org-archive-location prop)
  149. (if (or (re-search-backward re nil t)
  150. (re-search-forward re nil t))
  151. (setq org-archive-location (match-string 1))))))
  152. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  153. (progn
  154. (setq afile (format (match-string 1 org-archive-location)
  155. (file-name-nondirectory buffer-file-name))
  156. heading (match-string 2 org-archive-location)))
  157. (error "Invalid `org-archive-location'"))
  158. (if (> (length afile) 0)
  159. (setq newfile-p (not (file-exists-p afile))
  160. buffer (find-file-noselect afile))
  161. (setq buffer (current-buffer)))
  162. (unless buffer
  163. (error "Cannot access file \"%s\"" afile))
  164. (if (and (> (length heading) 0)
  165. (string-match "^\\*+" heading))
  166. (setq level (match-end 0))
  167. (setq heading nil level 0))
  168. (save-excursion
  169. (org-back-to-heading t)
  170. ;; Get context information that will be lost by moving the tree
  171. (org-refresh-category-properties)
  172. (setq category (org-get-category)
  173. todo (and (looking-at org-todo-line-regexp)
  174. (match-string 2))
  175. priority (org-get-priority
  176. (if (match-end 3) (match-string 3) ""))
  177. ltags (org-get-tags)
  178. itags (org-delete-all ltags (org-get-tags-at)))
  179. (setq ltags (mapconcat 'identity ltags " ")
  180. itags (mapconcat 'identity itags " "))
  181. ;; We first only copy, in case something goes wrong
  182. ;; we need to protect this-command, to avoid kill-region sets it,
  183. ;; which would lead to duplication of subtrees
  184. (let (this-command) (org-copy-subtree))
  185. (set-buffer buffer)
  186. ;; Enforce org-mode for the archive buffer
  187. (if (not (org-mode-p))
  188. ;; Force the mode for future visits.
  189. (let ((org-insert-mode-line-in-empty-file t)
  190. (org-inhibit-startup t))
  191. (call-interactively 'org-mode)))
  192. (when newfile-p
  193. (goto-char (point-max))
  194. (insert (format "\nArchived entries from file %s\n\n"
  195. (buffer-file-name this-buffer))))
  196. ;; Force the TODO keywords of the original buffer
  197. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  198. (org-todo-keywords-1 tr-org-todo-keywords-1)
  199. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  200. (org-done-keywords tr-org-done-keywords)
  201. (org-todo-regexp tr-org-todo-regexp)
  202. (org-todo-line-regexp tr-org-todo-line-regexp)
  203. (org-odd-levels-only
  204. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  205. org-odd-levels-only
  206. tr-org-odd-levels-only)))
  207. (goto-char (point-min))
  208. (show-all)
  209. (if heading
  210. (progn
  211. (if (re-search-forward
  212. (concat "^" (regexp-quote heading)
  213. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  214. nil t)
  215. (goto-char (match-end 0))
  216. ;; Heading not found, just insert it at the end
  217. (goto-char (point-max))
  218. (or (bolp) (insert "\n"))
  219. (insert "\n" heading "\n")
  220. (end-of-line 0))
  221. ;; Make the subtree visible
  222. (show-subtree)
  223. (org-end-of-subtree t)
  224. (skip-chars-backward " \t\r\n")
  225. (and (looking-at "[ \t\r\n]*")
  226. (replace-match "\n\n")))
  227. ;; No specific heading, just go to end of file.
  228. (goto-char (point-max)) (insert "\n"))
  229. ;; Paste
  230. (org-paste-subtree (org-get-valid-level level 1))
  231. ;; Mark the entry as done
  232. (when (and org-archive-mark-done
  233. (looking-at org-todo-line-regexp)
  234. (or (not (match-end 2))
  235. (not (member (match-string 2) org-done-keywords))))
  236. (let (org-log-done org-todo-log-states)
  237. (org-todo
  238. (car (or (member org-archive-mark-done org-done-keywords)
  239. org-done-keywords)))))
  240. ;; Add the context info
  241. (when org-archive-save-context-info
  242. (let ((l org-archive-save-context-info) e n v)
  243. (while (setq e (pop l))
  244. (when (and (setq v (symbol-value e))
  245. (stringp v) (string-match "\\S-" v))
  246. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  247. (org-entry-put (point) n v)))))
  248. ;; Save and kill the buffer, if it is not the same buffer.
  249. (if (not (eq this-buffer buffer))
  250. (progn (save-buffer) (kill-buffer buffer)))))
  251. ;; Here we are back in the original buffer. Everything seems to have
  252. ;; worked. So now cut the tree and finish up.
  253. (let (this-command) (org-cut-subtree))
  254. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  255. (message "Subtree archived %s"
  256. (if (eq this-buffer buffer)
  257. (concat "under heading: " heading)
  258. (concat "in file: " (abbreviate-file-name afile)))))))
  259. (defun org-archive-to-archive-sibling ()
  260. "Archive the current heading by moving it under the archive sibling.
  261. The archive sibling is a sibling of the heading with the heading name
  262. `org-archive-sibling-heading' and an `org-archive-tag' tag. If this
  263. sibling does not exist, it will be created at the end of the subtree."
  264. (interactive)
  265. (save-restriction
  266. (widen)
  267. (let (b e pos leader level)
  268. (org-back-to-heading t)
  269. (looking-at outline-regexp)
  270. (setq leader (match-string 0)
  271. level (funcall outline-level))
  272. (setq pos (point))
  273. (condition-case nil
  274. (outline-up-heading 1 t)
  275. (error (goto-char (point-min))))
  276. (setq b (point))
  277. (condition-case nil
  278. (org-end-of-subtree t t)
  279. (error (goto-char (point-max))))
  280. (setq e (point))
  281. (goto-char b)
  282. (unless (re-search-forward
  283. (concat "^" (regexp-quote leader)
  284. "[ \t]*"
  285. org-archive-sibling-heading
  286. "[ \t]*:"
  287. org-archive-tag ":") e t)
  288. (goto-char e)
  289. (or (bolp) (newline))
  290. (insert leader org-archive-sibling-heading "\n")
  291. (beginning-of-line 0)
  292. (org-toggle-tag org-archive-tag 'on))
  293. (beginning-of-line 1)
  294. (org-end-of-subtree t t)
  295. (save-excursion
  296. (goto-char pos)
  297. (org-cut-subtree))
  298. (org-paste-subtree (org-get-valid-level level 1))
  299. (org-set-property
  300. "ARCHIVE_TIME"
  301. (format-time-string
  302. (substring (cdr org-time-stamp-formats) 1 -1)
  303. (current-time)))
  304. (outline-up-heading 1 t)
  305. (hide-subtree)
  306. (goto-char pos))))
  307. (defun org-archive-all-done (&optional tag)
  308. "Archive sublevels of the current tree without open TODO items.
  309. If the cursor is not on a headline, try all level 1 trees. If
  310. it is on a headline, try all direct children.
  311. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  312. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  313. (rea (concat ".*:" org-archive-tag ":"))
  314. (begm (make-marker))
  315. (endm (make-marker))
  316. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  317. "Move subtree to archive (no open TODO items)? "))
  318. beg end (cntarch 0))
  319. (if (org-on-heading-p)
  320. (progn
  321. (setq re1 (concat "^" (regexp-quote
  322. (make-string
  323. (1+ (- (match-end 0) (match-beginning 0) 1))
  324. ?*))
  325. " "))
  326. (move-marker begm (point))
  327. (move-marker endm (org-end-of-subtree t)))
  328. (setq re1 "^* ")
  329. (move-marker begm (point-min))
  330. (move-marker endm (point-max)))
  331. (save-excursion
  332. (goto-char begm)
  333. (while (re-search-forward re1 endm t)
  334. (setq beg (match-beginning 0)
  335. end (save-excursion (org-end-of-subtree t) (point)))
  336. (goto-char beg)
  337. (if (re-search-forward re end t)
  338. (goto-char end)
  339. (goto-char beg)
  340. (if (and (or (not tag) (not (looking-at rea)))
  341. (y-or-n-p question))
  342. (progn
  343. (if tag
  344. (org-toggle-tag org-archive-tag 'on)
  345. (org-archive-subtree))
  346. (setq cntarch (1+ cntarch)))
  347. (goto-char end)))))
  348. (message "%d trees archived" cntarch)))
  349. (defun org-toggle-archive-tag (&optional find-done)
  350. "Toggle the archive tag for the current headline.
  351. With prefix ARG, check all children of current headline and offer tagging
  352. the children that do not contain any open TODO items."
  353. (interactive "P")
  354. (if find-done
  355. (org-archive-all-done 'tag)
  356. (let (set)
  357. (save-excursion
  358. (org-back-to-heading t)
  359. (setq set (org-toggle-tag org-archive-tag))
  360. (when set (hide-subtree)))
  361. (and set (beginning-of-line 1))
  362. (message "Subtree %s" (if set "archived" "unarchived")))))
  363. (provide 'org-archive)
  364. ;;; org-archive.el ends here