org-archive.el 14 KB

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