org-archive.el 19 KB

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