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