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