org-archive.el 15 KB

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