org-archive.el 15 KB

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