org-archive.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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.30trans
  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. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  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 or remove
  46. `time' from the variable `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 loses 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. ltags The local tags, in the headline of the subtree.
  58. itags 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 "Priority" 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 of files.
  93. This implies visiting all these files and finding out what the
  94. archive file is."
  95. (org-uniquify
  96. (apply
  97. 'append
  98. (mapcar
  99. (lambda (f)
  100. (if (not (file-exists-p f))
  101. nil
  102. (with-current-buffer (org-get-agenda-file-buffer f)
  103. (cons f (org-all-archive-files)))))
  104. files))))
  105. (defun org-all-archive-files ()
  106. "Get a list of all archive files used in the current buffer."
  107. (let (file files)
  108. (save-excursion
  109. (save-restriction
  110. (goto-char (point-min))
  111. (while (re-search-forward
  112. "^\\(#\\+\\|[ \t]*:\\)ARCHIVE:[ \t]+\\(.*\\)"
  113. nil t)
  114. (setq file (org-extract-archive-file
  115. (org-match-string-no-properties 2)))
  116. (and file (> (length file) 0) (file-exists-p file)
  117. (add-to-list 'files file)))))
  118. (setq files (nreverse files))
  119. (setq file (org-extract-archive-file))
  120. (and file (> (length file) 0) (file-exists-p file)
  121. (add-to-list 'files file))
  122. files))
  123. (defun org-extract-archive-file (&optional location)
  124. "Extract and expand the file name from archive LOCATION.
  125. if LOCATION is not given, the value of `org-archive-location' is used."
  126. (setq location (or location org-archive-location))
  127. (if (string-match "\\(.*\\)::\\(.*\\)" location)
  128. (if (= (match-beginning 1) (match-end 1))
  129. (buffer-file-name)
  130. (expand-file-name
  131. (format (match-string 1 location)
  132. (file-name-nondirectory buffer-file-name))))))
  133. (defun org-extract-archive-heading (&optional location)
  134. "Extract the heading from archive LOCATION.
  135. if LOCATION is not given, the value of `org-archive-location' is used."
  136. (setq location (or location org-archive-location))
  137. (if (string-match "\\(.*\\)::\\(.*\\)" location)
  138. (format (match-string 2 location)
  139. (file-name-nondirectory buffer-file-name))))
  140. (defun org-archive-subtree (&optional find-done)
  141. "Move the current subtree to the archive.
  142. The archive can be a certain top-level heading in the current file, or in
  143. a different file. The tree will be moved to that location, the subtree
  144. heading be marked DONE, and the current time will be added.
  145. When called with prefix argument FIND-DONE, find whole trees without any
  146. open TODO items and archive them (after getting confirmation from the user).
  147. If the cursor is not at a headline when this command is called, try all level
  148. 1 trees. If the cursor is on a headline, only try the direct children of
  149. this heading."
  150. (interactive "P")
  151. (if find-done
  152. (org-archive-all-done)
  153. ;; Save all relevant TODO keyword-relatex variables
  154. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  155. (tr-org-todo-keywords-1 org-todo-keywords-1)
  156. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  157. (tr-org-done-keywords org-done-keywords)
  158. (tr-org-todo-regexp org-todo-regexp)
  159. (tr-org-todo-line-regexp org-todo-line-regexp)
  160. (tr-org-odd-levels-only org-odd-levels-only)
  161. (this-buffer (current-buffer))
  162. ;; start of variables that will be used for saving context
  163. ;; The compiler complains about them - keep them anyway!
  164. (file (abbreviate-file-name (buffer-file-name)))
  165. (olpath (mapconcat 'identity (org-get-outline-path) "/"))
  166. (time (format-time-string
  167. (substring (cdr org-time-stamp-formats) 1 -1)
  168. (current-time)))
  169. category todo priority ltags itags
  170. ;; end of variables that will be used for saving context
  171. location afile heading buffer level newfile-p visiting)
  172. ;; Find the local archive location
  173. (setq location (org-get-local-archive-location)
  174. afile (org-extract-archive-file location)
  175. heading (org-extract-archive-heading location))
  176. (unless afile
  177. (error "Invalid `org-archive-location'"))
  178. (if (> (length afile) 0)
  179. (setq newfile-p (not (file-exists-p afile))
  180. visiting (find-buffer-visiting afile)
  181. buffer (or visiting (find-file-noselect afile)))
  182. (setq buffer (current-buffer)))
  183. (unless buffer
  184. (error "Cannot access file \"%s\"" afile))
  185. (if (and (> (length heading) 0)
  186. (string-match "^\\*+" heading))
  187. (setq level (match-end 0))
  188. (setq heading nil level 0))
  189. (save-excursion
  190. (org-back-to-heading t)
  191. ;; Get context information that will be lost by moving the tree
  192. (org-refresh-category-properties)
  193. (setq category (org-get-category)
  194. todo (and (looking-at org-todo-line-regexp)
  195. (match-string 2))
  196. priority (org-get-priority
  197. (if (match-end 3) (match-string 3) ""))
  198. ltags (org-get-tags)
  199. itags (org-delete-all ltags (org-get-tags-at)))
  200. (setq ltags (mapconcat 'identity ltags " ")
  201. itags (mapconcat 'identity itags " "))
  202. ;; We first only copy, in case something goes wrong
  203. ;; we need to protect `this-command', to avoid kill-region sets it,
  204. ;; which would lead to duplication of subtrees
  205. (let (this-command) (org-copy-subtree 1 nil t))
  206. (set-buffer buffer)
  207. ;; Enforce org-mode for the archive buffer
  208. (if (not (org-mode-p))
  209. ;; Force the mode for future visits.
  210. (let ((org-insert-mode-line-in-empty-file t)
  211. (org-inhibit-startup t))
  212. (call-interactively 'org-mode)))
  213. (when newfile-p
  214. (goto-char (point-max))
  215. (insert (format "\nArchived entries from file %s\n\n"
  216. (buffer-file-name this-buffer))))
  217. ;; Force the TODO keywords of the original buffer
  218. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  219. (org-todo-keywords-1 tr-org-todo-keywords-1)
  220. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  221. (org-done-keywords tr-org-done-keywords)
  222. (org-todo-regexp tr-org-todo-regexp)
  223. (org-todo-line-regexp tr-org-todo-line-regexp)
  224. (org-odd-levels-only
  225. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  226. org-odd-levels-only
  227. tr-org-odd-levels-only)))
  228. (goto-char (point-min))
  229. (show-all)
  230. (if heading
  231. (progn
  232. (if (re-search-forward
  233. (concat "^" (regexp-quote heading)
  234. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  235. nil t)
  236. (goto-char (match-end 0))
  237. ;; Heading not found, just insert it at the end
  238. (goto-char (point-max))
  239. (or (bolp) (insert "\n"))
  240. (insert "\n" heading "\n")
  241. (end-of-line 0))
  242. ;; Make the subtree visible
  243. (show-subtree)
  244. (org-end-of-subtree t)
  245. (skip-chars-backward " \t\r\n")
  246. (and (looking-at "[ \t\r\n]*")
  247. (replace-match "\n\n")))
  248. ;; No specific heading, just go to end of file.
  249. (goto-char (point-max)) (insert "\n"))
  250. ;; Paste
  251. (org-paste-subtree (org-get-valid-level level (and heading 1)))
  252. ;; Mark the entry as done
  253. (when (and org-archive-mark-done
  254. (looking-at org-todo-line-regexp)
  255. (or (not (match-end 2))
  256. (not (member (match-string 2) org-done-keywords))))
  257. (let (org-log-done org-todo-log-states)
  258. (org-todo
  259. (car (or (member org-archive-mark-done org-done-keywords)
  260. org-done-keywords)))))
  261. ;; Add the context info
  262. (when org-archive-save-context-info
  263. (let ((l org-archive-save-context-info) e n v)
  264. (while (setq e (pop l))
  265. (when (and (setq v (symbol-value e))
  266. (stringp v) (string-match "\\S-" v))
  267. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  268. (org-entry-put (point) n v)))))
  269. ;; Save and kill the buffer, if it is not the same buffer.
  270. (when (not (eq this-buffer buffer))
  271. (save-buffer)
  272. ;; Check if it is OK to kill the buffer
  273. (unless
  274. (or visiting
  275. (equal (marker-buffer org-clock-marker) (current-buffer)))
  276. (kill-buffer buffer)))
  277. ))
  278. ;; Here we are back in the original buffer. Everything seems to have
  279. ;; worked. So now cut the tree and finish up.
  280. (let (this-command) (org-cut-subtree))
  281. (when (featurep 'org-inlinetask)
  282. (org-inlinetask-remove-END-maybe))
  283. (setq org-markers-to-move nil)
  284. (message "Subtree archived %s"
  285. (if (eq this-buffer buffer)
  286. (concat "under heading: " heading)
  287. (concat "in file: " (abbreviate-file-name afile))))))
  288. (org-reveal)
  289. (if (looking-at "^[ \t]*$")
  290. (outline-next-visible-heading 1)))
  291. (defun org-archive-to-archive-sibling ()
  292. "Archive the current heading by moving it under the archive sibling.
  293. The archive sibling is a sibling of the heading with the heading name
  294. `org-archive-sibling-heading' and an `org-archive-tag' tag. If this
  295. sibling does not exist, it will be created at the end of the subtree."
  296. (interactive)
  297. (save-restriction
  298. (widen)
  299. (let (b e pos leader level)
  300. (org-back-to-heading t)
  301. (looking-at outline-regexp)
  302. (setq leader (match-string 0)
  303. level (funcall outline-level))
  304. (setq pos (point))
  305. (condition-case nil
  306. (outline-up-heading 1 t)
  307. (error (setq e (point-max)) (goto-char (point-min))))
  308. (setq b (point))
  309. (unless e
  310. (condition-case nil
  311. (org-end-of-subtree t t)
  312. (error (goto-char (point-max))))
  313. (setq e (point)))
  314. (goto-char b)
  315. (unless (re-search-forward
  316. (concat "^" (regexp-quote leader)
  317. "[ \t]*"
  318. org-archive-sibling-heading
  319. "[ \t]*:"
  320. org-archive-tag ":") e t)
  321. (goto-char e)
  322. (or (bolp) (newline))
  323. (insert leader org-archive-sibling-heading "\n")
  324. (beginning-of-line 0)
  325. (org-toggle-tag org-archive-tag 'on))
  326. (beginning-of-line 1)
  327. (org-end-of-subtree t t)
  328. (save-excursion
  329. (goto-char pos)
  330. (let ((this-command this-command)) (org-cut-subtree)))
  331. (org-paste-subtree (org-get-valid-level level 1))
  332. (org-set-property
  333. "ARCHIVE_TIME"
  334. (format-time-string
  335. (substring (cdr org-time-stamp-formats) 1 -1)
  336. (current-time)))
  337. (outline-up-heading 1 t)
  338. (hide-subtree)
  339. (org-cycle-show-empty-lines 'folded)
  340. (goto-char pos)))
  341. (org-reveal)
  342. (if (looking-at "^[ \t]*$")
  343. (outline-next-visible-heading 1)))
  344. (defun org-archive-all-done (&optional tag)
  345. "Archive sublevels of the current tree without open TODO items.
  346. If the cursor is not on a headline, try all level 1 trees. If
  347. it is on a headline, try all direct children.
  348. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  349. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  350. (rea (concat ".*:" org-archive-tag ":"))
  351. (begm (make-marker))
  352. (endm (make-marker))
  353. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  354. "Move subtree to archive (no open TODO items)? "))
  355. beg end (cntarch 0))
  356. (if (org-on-heading-p)
  357. (progn
  358. (setq re1 (concat "^" (regexp-quote
  359. (make-string
  360. (1+ (- (match-end 0) (match-beginning 0) 1))
  361. ?*))
  362. " "))
  363. (move-marker begm (point))
  364. (move-marker endm (org-end-of-subtree t)))
  365. (setq re1 "^* ")
  366. (move-marker begm (point-min))
  367. (move-marker endm (point-max)))
  368. (save-excursion
  369. (goto-char begm)
  370. (while (re-search-forward re1 endm t)
  371. (setq beg (match-beginning 0)
  372. end (save-excursion (org-end-of-subtree t) (point)))
  373. (goto-char beg)
  374. (if (re-search-forward re end t)
  375. (goto-char end)
  376. (goto-char beg)
  377. (if (and (or (not tag) (not (looking-at rea)))
  378. (y-or-n-p question))
  379. (progn
  380. (if tag
  381. (org-toggle-tag org-archive-tag 'on)
  382. (org-archive-subtree))
  383. (setq cntarch (1+ cntarch)))
  384. (goto-char end)))))
  385. (message "%d trees archived" cntarch)))
  386. (defun org-toggle-archive-tag (&optional find-done)
  387. "Toggle the archive tag for the current headline.
  388. With prefix ARG, check all children of current headline and offer tagging
  389. the children that do not contain any open TODO items."
  390. (interactive "P")
  391. (if find-done
  392. (org-archive-all-done 'tag)
  393. (let (set)
  394. (save-excursion
  395. (org-back-to-heading t)
  396. (setq set (org-toggle-tag org-archive-tag))
  397. (when set (hide-subtree)))
  398. (and set (beginning-of-line 1))
  399. (message "Subtree %s" (if set "archived" "unarchived")))))
  400. (provide 'org-archive)
  401. ;; arch-tag: 0837f601-9699-43c3-8b90-631572ae6c85
  402. ;;; org-archive.el ends here