org-archive.el 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. ;;; org-archive.el --- Archiving for Org-mode
  2. ;; Copyright (C) 2004-2012 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 (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  170. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  171. 'region-start-level 'region))
  172. org-loop-over-headlines-in-active-region)
  173. (org-map-entries
  174. `(progn (setq org-map-continue-from (progn (org-back-to-heading) (point)))
  175. (org-archive-subtree ,find-done))
  176. org-loop-over-headlines-in-active-region
  177. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  178. (if find-done
  179. (org-archive-all-done)
  180. ;; Save all relevant TODO keyword-relatex variables
  181. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  182. (tr-org-todo-keywords-1 org-todo-keywords-1)
  183. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  184. (tr-org-done-keywords org-done-keywords)
  185. (tr-org-todo-regexp org-todo-regexp)
  186. (tr-org-todo-line-regexp org-todo-line-regexp)
  187. (tr-org-odd-levels-only org-odd-levels-only)
  188. (this-buffer (current-buffer))
  189. ;; start of variables that will be used for saving context
  190. ;; The compiler complains about them - keep them anyway!
  191. (file (abbreviate-file-name
  192. (or (buffer-file-name (buffer-base-buffer))
  193. (error "No file associated to buffer"))))
  194. (olpath (mapconcat 'identity (org-get-outline-path) "/"))
  195. (time (format-time-string
  196. (substring (cdr org-time-stamp-formats) 1 -1)
  197. (current-time)))
  198. category todo priority ltags itags atags
  199. ;; end of variables that will be used for saving context
  200. location afile heading buffer level newfile-p infile-p visiting)
  201. ;; Find the local archive location
  202. (setq location (org-get-local-archive-location)
  203. afile (org-extract-archive-file location)
  204. heading (org-extract-archive-heading location)
  205. infile-p (equal file (abbreviate-file-name afile)))
  206. (unless afile
  207. (error "Invalid `org-archive-location'"))
  208. (if (> (length afile) 0)
  209. (setq newfile-p (not (file-exists-p afile))
  210. visiting (find-buffer-visiting afile)
  211. buffer (or visiting (find-file-noselect afile)))
  212. (setq buffer (current-buffer)))
  213. (unless buffer
  214. (error "Cannot access file \"%s\"" afile))
  215. (if (and (> (length heading) 0)
  216. (string-match "^\\*+" heading))
  217. (setq level (match-end 0))
  218. (setq heading nil level 0))
  219. (save-excursion
  220. (org-back-to-heading t)
  221. ;; Get context information that will be lost by moving the tree
  222. (setq category (org-get-category nil 'force-refresh)
  223. todo (and (looking-at org-todo-line-regexp)
  224. (match-string 2))
  225. priority (org-get-priority
  226. (if (match-end 3) (match-string 3) ""))
  227. ltags (org-get-tags)
  228. itags (org-delete-all ltags (org-get-tags-at))
  229. atags (org-get-tags-at))
  230. (setq ltags (mapconcat 'identity ltags " ")
  231. itags (mapconcat 'identity itags " "))
  232. ;; We first only copy, in case something goes wrong
  233. ;; we need to protect `this-command', to avoid kill-region sets it,
  234. ;; which would lead to duplication of subtrees
  235. (let (this-command) (org-copy-subtree 1 nil t))
  236. (set-buffer buffer)
  237. ;; Enforce org-mode for the archive buffer
  238. (if (not (eq major-mode 'org-mode))
  239. ;; Force the mode for future visits.
  240. (let ((org-insert-mode-line-in-empty-file t)
  241. (org-inhibit-startup t))
  242. (call-interactively 'org-mode)))
  243. (when newfile-p
  244. (goto-char (point-max))
  245. (insert (format "\nArchived entries from file %s\n\n"
  246. (buffer-file-name this-buffer))))
  247. ;; Force the TODO keywords of the original buffer
  248. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  249. (org-todo-keywords-1 tr-org-todo-keywords-1)
  250. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  251. (org-done-keywords tr-org-done-keywords)
  252. (org-todo-regexp tr-org-todo-regexp)
  253. (org-todo-line-regexp tr-org-todo-line-regexp)
  254. (org-odd-levels-only
  255. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  256. org-odd-levels-only
  257. tr-org-odd-levels-only)))
  258. (goto-char (point-min))
  259. (show-all)
  260. (if heading
  261. (progn
  262. (if (re-search-forward
  263. (concat "^" (regexp-quote heading)
  264. (org-re "[ \t]*\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\($\\|\r\\)"))
  265. nil t)
  266. (goto-char (match-end 0))
  267. ;; Heading not found, just insert it at the end
  268. (goto-char (point-max))
  269. (or (bolp) (insert "\n"))
  270. (insert "\n" heading "\n")
  271. (end-of-line 0))
  272. ;; Make the subtree visible
  273. (show-subtree)
  274. (if org-archive-reversed-order
  275. (progn
  276. (org-back-to-heading t)
  277. (outline-next-heading))
  278. (org-end-of-subtree t))
  279. (skip-chars-backward " \t\r\n")
  280. (and (looking-at "[ \t\r\n]*")
  281. (replace-match "\n\n")))
  282. ;; No specific heading, just go to end of file.
  283. (goto-char (point-max)) (insert "\n"))
  284. ;; Paste
  285. (org-paste-subtree (org-get-valid-level level (and heading 1)))
  286. ;; Shall we append inherited tags?
  287. (and itags
  288. (or (and (eq org-archive-subtree-add-inherited-tags 'infile)
  289. infile-p)
  290. (eq org-archive-subtree-add-inherited-tags t))
  291. (org-set-tags-to atags))
  292. ;; Mark the entry as done
  293. (when (and org-archive-mark-done
  294. (looking-at org-todo-line-regexp)
  295. (or (not (match-end 2))
  296. (not (member (match-string 2) org-done-keywords))))
  297. (let (org-log-done org-todo-log-states)
  298. (org-todo
  299. (car (or (member org-archive-mark-done org-done-keywords)
  300. org-done-keywords)))))
  301. ;; Add the context info
  302. (when org-archive-save-context-info
  303. (let ((l org-archive-save-context-info) e n v)
  304. (while (setq e (pop l))
  305. (when (and (setq v (symbol-value e))
  306. (stringp v) (string-match "\\S-" v))
  307. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  308. (org-entry-put (point) n v)))))
  309. ;; Save and kill the buffer, if it is not the same buffer.
  310. (when (not (eq this-buffer buffer))
  311. (save-buffer))))
  312. ;; Here we are back in the original buffer. Everything seems to have
  313. ;; worked. So now cut the tree and finish up.
  314. (let (this-command) (org-cut-subtree))
  315. (when (featurep 'org-inlinetask)
  316. (org-inlinetask-remove-END-maybe))
  317. (setq org-markers-to-move nil)
  318. (message "Subtree archived %s"
  319. (if (eq this-buffer buffer)
  320. (concat "under heading: " heading)
  321. (concat "in file: " (abbreviate-file-name afile))))))
  322. (org-reveal)
  323. (if (looking-at "^[ \t]*$")
  324. (outline-next-visible-heading 1))))
  325. (defun org-archive-to-archive-sibling ()
  326. "Archive the current heading by moving it under the archive sibling.
  327. The archive sibling is a sibling of the heading with the heading name
  328. `org-archive-sibling-heading' and an `org-archive-tag' tag. If this
  329. sibling does not exist, it will be created at the end of the subtree."
  330. (interactive)
  331. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  332. (let ((cl (when (eq org-loop-over-headlines-in-active-region 'start-level)
  333. 'region-start-level 'region))
  334. org-loop-over-headlines-in-active-region)
  335. (org-map-entries
  336. '(progn (setq org-map-continue-from
  337. (progn (org-back-to-heading)
  338. (if (looking-at (concat "^.*:" org-archive-tag ":.*$"))
  339. (org-end-of-subtree t)
  340. (point))))
  341. (when (org-at-heading-p)
  342. (org-archive-to-archive-sibling)))
  343. org-loop-over-headlines-in-active-region
  344. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  345. (save-restriction
  346. (widen)
  347. (let (b e pos leader level)
  348. (org-back-to-heading t)
  349. (looking-at org-outline-regexp)
  350. (setq leader (match-string 0)
  351. level (funcall outline-level))
  352. (setq pos (point))
  353. (condition-case nil
  354. (outline-up-heading 1 t)
  355. (error (setq e (point-max)) (goto-char (point-min))))
  356. (setq b (point))
  357. (unless e
  358. (condition-case nil
  359. (org-end-of-subtree t t)
  360. (error (goto-char (point-max))))
  361. (setq e (point)))
  362. (goto-char b)
  363. (unless (re-search-forward
  364. (concat "^" (regexp-quote leader)
  365. "[ \t]*"
  366. org-archive-sibling-heading
  367. "[ \t]*:"
  368. org-archive-tag ":") e t)
  369. (goto-char e)
  370. (or (bolp) (newline))
  371. (insert leader org-archive-sibling-heading "\n")
  372. (beginning-of-line 0)
  373. (org-toggle-tag org-archive-tag 'on))
  374. (beginning-of-line 1)
  375. (if org-archive-reversed-order
  376. (outline-next-heading)
  377. (org-end-of-subtree t t))
  378. (save-excursion
  379. (goto-char pos)
  380. (let ((this-command this-command)) (org-cut-subtree)))
  381. (org-paste-subtree (org-get-valid-level level 1))
  382. (org-set-property
  383. "ARCHIVE_TIME"
  384. (format-time-string
  385. (substring (cdr org-time-stamp-formats) 1 -1)
  386. (current-time)))
  387. (outline-up-heading 1 t)
  388. (hide-subtree)
  389. (org-cycle-show-empty-lines 'folded)
  390. (goto-char pos)))
  391. (org-reveal)
  392. (if (looking-at "^[ \t]*$")
  393. (outline-next-visible-heading 1))))
  394. (defun org-archive-all-done (&optional tag)
  395. "Archive sublevels of the current tree without open TODO items.
  396. If the cursor is not on a headline, try all level 1 trees. If
  397. it is on a headline, try all direct children.
  398. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  399. (let ((re org-not-done-heading-regexp) re1
  400. (rea (concat ".*:" org-archive-tag ":"))
  401. (begm (make-marker))
  402. (endm (make-marker))
  403. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  404. "Move subtree to archive (no open TODO items)? "))
  405. beg end (cntarch 0))
  406. (if (org-at-heading-p)
  407. (progn
  408. (setq re1 (concat "^" (regexp-quote
  409. (make-string
  410. (+ (- (match-end 0) (match-beginning 0) 1)
  411. (if org-odd-levels-only 2 1))
  412. ?*))
  413. " "))
  414. (move-marker begm (point))
  415. (move-marker endm (org-end-of-subtree t)))
  416. (setq re1 "^* ")
  417. (move-marker begm (point-min))
  418. (move-marker endm (point-max)))
  419. (save-excursion
  420. (goto-char begm)
  421. (while (re-search-forward re1 endm t)
  422. (setq beg (match-beginning 0)
  423. end (save-excursion (org-end-of-subtree t) (point)))
  424. (goto-char beg)
  425. (if (re-search-forward re end t)
  426. (goto-char end)
  427. (goto-char beg)
  428. (if (and (or (not tag) (not (looking-at rea)))
  429. (y-or-n-p question))
  430. (progn
  431. (if tag
  432. (org-toggle-tag org-archive-tag 'on)
  433. (org-archive-subtree))
  434. (setq cntarch (1+ cntarch)))
  435. (goto-char end)))))
  436. (message "%d trees archived" cntarch)))
  437. (defun org-toggle-archive-tag (&optional find-done)
  438. "Toggle the archive tag for the current headline.
  439. With prefix ARG, check all children of current headline and offer tagging
  440. the children that do not contain any open TODO items."
  441. (interactive "P")
  442. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  443. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  444. 'region-start-level 'region))
  445. org-loop-over-headlines-in-active-region)
  446. (org-map-entries
  447. `(org-toggle-archive-tag ,find-done)
  448. org-loop-over-headlines-in-active-region
  449. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  450. (if find-done
  451. (org-archive-all-done 'tag)
  452. (let (set)
  453. (save-excursion
  454. (org-back-to-heading t)
  455. (setq set (org-toggle-tag org-archive-tag))
  456. (when set (hide-subtree)))
  457. (and set (beginning-of-line 1))
  458. (message "Subtree %s" (if set "archived" "unarchived"))))))
  459. (defun org-archive-set-tag ()
  460. "Set the ARCHIVE tag."
  461. (interactive)
  462. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  463. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  464. 'region-start-level 'region))
  465. org-loop-over-headlines-in-active-region)
  466. (org-map-entries
  467. 'org-archive-set-tag
  468. org-loop-over-headlines-in-active-region
  469. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  470. (org-toggle-tag org-archive-tag 'on)))
  471. ;;;###autoload
  472. (defun org-archive-subtree-default ()
  473. "Archive the current subtree with the default command.
  474. This command is set with the variable `org-archive-default-command'."
  475. (interactive)
  476. (call-interactively org-archive-default-command))
  477. ;;;###autoload
  478. (defun org-archive-subtree-default-with-confirmation ()
  479. "Archive the current subtree with the default command.
  480. This command is set with the variable `org-archive-default-command'."
  481. (interactive)
  482. (if (y-or-n-p "Archive this subtree or entry? ")
  483. (call-interactively org-archive-default-command)
  484. (error "Abort")))
  485. (provide 'org-archive)
  486. ;;; org-archive.el ends here