org-archive.el 16 KB

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