org-archive.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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.02b
  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 visiting)
  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. visiting (find-buffer-visiting afile)
  174. buffer (or visiting (find-file-noselect afile)))
  175. (setq buffer (current-buffer)))
  176. (unless buffer
  177. (error "Cannot access file \"%s\"" afile))
  178. (if (and (> (length heading) 0)
  179. (string-match "^\\*+" heading))
  180. (setq level (match-end 0))
  181. (setq heading nil level 0))
  182. (save-excursion
  183. (org-back-to-heading t)
  184. ;; Get context information that will be lost by moving the tree
  185. (org-refresh-category-properties)
  186. (setq category (org-get-category)
  187. todo (and (looking-at org-todo-line-regexp)
  188. (match-string 2))
  189. priority (org-get-priority
  190. (if (match-end 3) (match-string 3) ""))
  191. ltags (org-get-tags)
  192. itags (org-delete-all ltags (org-get-tags-at)))
  193. (setq ltags (mapconcat 'identity ltags " ")
  194. itags (mapconcat 'identity itags " "))
  195. ;; We first only copy, in case something goes wrong
  196. ;; we need to protect `this-command', to avoid kill-region sets it,
  197. ;; which would lead to duplication of subtrees
  198. (let ((org-markers-to-move 'force)
  199. this-command)
  200. (org-copy-subtree))
  201. (set-buffer buffer)
  202. ;; Enforce org-mode for the archive buffer
  203. (if (not (org-mode-p))
  204. ;; Force the mode for future visits.
  205. (let ((org-insert-mode-line-in-empty-file t)
  206. (org-inhibit-startup t))
  207. (call-interactively 'org-mode)))
  208. (when newfile-p
  209. (goto-char (point-max))
  210. (insert (format "\nArchived entries from file %s\n\n"
  211. (buffer-file-name this-buffer))))
  212. ;; Force the TODO keywords of the original buffer
  213. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  214. (org-todo-keywords-1 tr-org-todo-keywords-1)
  215. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  216. (org-done-keywords tr-org-done-keywords)
  217. (org-todo-regexp tr-org-todo-regexp)
  218. (org-todo-line-regexp tr-org-todo-line-regexp)
  219. (org-odd-levels-only
  220. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  221. org-odd-levels-only
  222. tr-org-odd-levels-only)))
  223. (goto-char (point-min))
  224. (show-all)
  225. (if heading
  226. (progn
  227. (if (re-search-forward
  228. (concat "^" (regexp-quote heading)
  229. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  230. nil t)
  231. (goto-char (match-end 0))
  232. ;; Heading not found, just insert it at the end
  233. (goto-char (point-max))
  234. (or (bolp) (insert "\n"))
  235. (insert "\n" heading "\n")
  236. (end-of-line 0))
  237. ;; Make the subtree visible
  238. (show-subtree)
  239. (org-end-of-subtree t)
  240. (skip-chars-backward " \t\r\n")
  241. (and (looking-at "[ \t\r\n]*")
  242. (replace-match "\n\n")))
  243. ;; No specific heading, just go to end of file.
  244. (goto-char (point-max)) (insert "\n"))
  245. ;; Paste
  246. (org-paste-subtree (org-get-valid-level level 1))
  247. ;; Mark the entry as done
  248. (when (and org-archive-mark-done
  249. (looking-at org-todo-line-regexp)
  250. (or (not (match-end 2))
  251. (not (member (match-string 2) org-done-keywords))))
  252. (let (org-log-done org-todo-log-states)
  253. (org-todo
  254. (car (or (member org-archive-mark-done org-done-keywords)
  255. org-done-keywords)))))
  256. ;; Add the context info
  257. (when org-archive-save-context-info
  258. (let ((l org-archive-save-context-info) e n v)
  259. (while (setq e (pop l))
  260. (when (and (setq v (symbol-value e))
  261. (stringp v) (string-match "\\S-" v))
  262. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  263. (org-entry-put (point) n v)))))
  264. ;; Save and kill the buffer, if it is not the same buffer.
  265. (when (not (eq this-buffer buffer))
  266. (save-buffer)
  267. ;; Check if it is OK to kill the buffer
  268. (unless
  269. (or visiting
  270. (equal (marker-buffer org-clock-marker) (current-buffer)))
  271. (kill-buffer buffer)))
  272. ))
  273. ;; Here we are back in the original buffer. Everything seems to have
  274. ;; worked. So now cut the tree and finish up.
  275. (let (this-command) (org-cut-subtree))
  276. (message "Subtree archived %s"
  277. (if (eq this-buffer buffer)
  278. (concat "under heading: " heading)
  279. (concat "in file: " (abbreviate-file-name afile)))))))
  280. (defun org-archive-to-archive-sibling ()
  281. "Archive the current heading by moving it under the archive sibling.
  282. The archive sibling is a sibling of the heading with the heading name
  283. `org-archive-sibling-heading' and an `org-archive-tag' tag. If this
  284. sibling does not exist, it will be created at the end of the subtree."
  285. (interactive)
  286. (save-restriction
  287. (widen)
  288. (let (b e pos leader level)
  289. (org-back-to-heading t)
  290. (looking-at outline-regexp)
  291. (setq leader (match-string 0)
  292. level (funcall outline-level))
  293. (setq pos (point))
  294. (condition-case nil
  295. (outline-up-heading 1 t)
  296. (error (goto-char (point-min))))
  297. (setq b (point))
  298. (condition-case nil
  299. (org-end-of-subtree t t)
  300. (error (goto-char (point-max))))
  301. (setq e (point))
  302. (goto-char b)
  303. (unless (re-search-forward
  304. (concat "^" (regexp-quote leader)
  305. "[ \t]*"
  306. org-archive-sibling-heading
  307. "[ \t]*:"
  308. org-archive-tag ":") e t)
  309. (goto-char e)
  310. (or (bolp) (newline))
  311. (insert leader org-archive-sibling-heading "\n")
  312. (beginning-of-line 0)
  313. (org-toggle-tag org-archive-tag 'on))
  314. (beginning-of-line 1)
  315. (org-end-of-subtree t t)
  316. (save-excursion
  317. (goto-char pos)
  318. (org-cut-subtree))
  319. (org-paste-subtree (org-get-valid-level level 1))
  320. (org-set-property
  321. "ARCHIVE_TIME"
  322. (format-time-string
  323. (substring (cdr org-time-stamp-formats) 1 -1)
  324. (current-time)))
  325. (outline-up-heading 1 t)
  326. (hide-subtree)
  327. (goto-char pos))))
  328. (defun org-archive-all-done (&optional tag)
  329. "Archive sublevels of the current tree without open TODO items.
  330. If the cursor is not on a headline, try all level 1 trees. If
  331. it is on a headline, try all direct children.
  332. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  333. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  334. (rea (concat ".*:" org-archive-tag ":"))
  335. (begm (make-marker))
  336. (endm (make-marker))
  337. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  338. "Move subtree to archive (no open TODO items)? "))
  339. beg end (cntarch 0))
  340. (if (org-on-heading-p)
  341. (progn
  342. (setq re1 (concat "^" (regexp-quote
  343. (make-string
  344. (1+ (- (match-end 0) (match-beginning 0) 1))
  345. ?*))
  346. " "))
  347. (move-marker begm (point))
  348. (move-marker endm (org-end-of-subtree t)))
  349. (setq re1 "^* ")
  350. (move-marker begm (point-min))
  351. (move-marker endm (point-max)))
  352. (save-excursion
  353. (goto-char begm)
  354. (while (re-search-forward re1 endm t)
  355. (setq beg (match-beginning 0)
  356. end (save-excursion (org-end-of-subtree t) (point)))
  357. (goto-char beg)
  358. (if (re-search-forward re end t)
  359. (goto-char end)
  360. (goto-char beg)
  361. (if (and (or (not tag) (not (looking-at rea)))
  362. (y-or-n-p question))
  363. (progn
  364. (if tag
  365. (org-toggle-tag org-archive-tag 'on)
  366. (org-archive-subtree))
  367. (setq cntarch (1+ cntarch)))
  368. (goto-char end)))))
  369. (message "%d trees archived" cntarch)))
  370. (defun org-toggle-archive-tag (&optional find-done)
  371. "Toggle the archive tag for the current headline.
  372. With prefix ARG, check all children of current headline and offer tagging
  373. the children that do not contain any open TODO items."
  374. (interactive "P")
  375. (if find-done
  376. (org-archive-all-done 'tag)
  377. (let (set)
  378. (save-excursion
  379. (org-back-to-heading t)
  380. (setq set (org-toggle-tag org-archive-tag))
  381. (when set (hide-subtree)))
  382. (and set (beginning-of-line 1))
  383. (message "Subtree %s" (if set "archived" "unarchived")))))
  384. (provide 'org-archive)
  385. ;; arch-tag: 0837f601-9699-43c3-8b90-631572ae6c85
  386. ;;; org-archive.el ends here