org-archive.el 17 KB

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