org-archive.el 17 KB

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