org-archive.el 17 KB

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