org-archive.el 17 KB

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