Commit | Line | Data |
---|---|---|
20908596 CD |
1 | ;;; org-archive.el --- Archiving for Org-mode |
2 | ||
73b0cd50 | 3 | ;; Copyright (C) 2004-2011 |
1e4f816a | 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 | |
acedf35c | 9 | ;; Version: 7.4 |
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. | |
52 | Locally means: in the tree, under a sibling. | |
53 | See `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 |
59 | This can be a string to set the keyword to use. When t, Org-mode will |
60 | use 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 |
69 | This 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 | ||
74 | (defcustom org-archive-save-context-info '(time file olpath category todo itags) | |
75 | "Parts of context info that should be stored as properties when archiving. | |
423a66f9 | 76 | When a subtree is moved to an archive file, it loses information given by |
20908596 CD |
77 | context, like inherited tags, the category, and possibly also the TODO |
78 | state (depending on the variable `org-archive-mark-done'). | |
79 | This variable can be a list of any of the following symbols: | |
80 | ||
81 | time The time of archiving. | |
82 | file The file where the entry originates. | |
b349f79f CD |
83 | ltags The local tags, in the headline of the subtree. |
84 | itags The tags the subtree inherits from further up the hierarchy. | |
20908596 CD |
85 | todo The pre-archive TODO state. |
86 | category The category, taken from file name or #+CATEGORY lines. | |
87 | olpath The outline path to the item. These are all headlines above | |
88 | the current item, separated by /, like a file path. | |
89 | ||
90 | For each symbol present in the list, a property will be created in | |
91 | the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this | |
92 | information." | |
93 | :group 'org-archive | |
94 | :type '(set :greedy t | |
95 | (const :tag "Time" time) | |
96 | (const :tag "File" file) | |
97 | (const :tag "Category" category) | |
98 | (const :tag "TODO state" todo) | |
b349f79f | 99 | (const :tag "Priority" priority) |
20908596 CD |
100 | (const :tag "Inherited tags" itags) |
101 | (const :tag "Outline path" olpath) | |
102 | (const :tag "Local tags" ltags))) | |
103 | ||
104 | (defun org-get-local-archive-location () | |
105 | "Get the archive location applicable at point." | |
106 | (let ((re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$") | |
107 | prop) | |
108 | (save-excursion | |
109 | (save-restriction | |
110 | (widen) | |
111 | (setq prop (org-entry-get nil "ARCHIVE" 'inherit)) | |
112 | (cond | |
113 | ((and prop (string-match "\\S-" prop)) | |
114 | prop) | |
115 | ((or (re-search-backward re nil t) | |
116 | (re-search-forward re nil t)) | |
117 | (match-string 1)) | |
afe98dfa | 118 | (t org-archive-location)))))) |
20908596 CD |
119 | |
120 | (defun org-add-archive-files (files) | |
0bd48b37 | 121 | "Splice the archive files into the list of files. |
20908596 CD |
122 | This implies visiting all these files and finding out what the |
123 | archive file is." | |
0bd48b37 CD |
124 | (org-uniquify |
125 | (apply | |
126 | 'append | |
127 | (mapcar | |
128 | (lambda (f) | |
129 | (if (not (file-exists-p f)) | |
130 | nil | |
131 | (with-current-buffer (org-get-agenda-file-buffer f) | |
132 | (cons f (org-all-archive-files))))) | |
133 | files)))) | |
20908596 CD |
134 | |
135 | (defun org-all-archive-files () | |
136 | "Get a list of all archive files used in the current buffer." | |
137 | (let (file files) | |
138 | (save-excursion | |
139 | (save-restriction | |
140 | (goto-char (point-min)) | |
141 | (while (re-search-forward | |
142 | "^\\(#\\+\\|[ \t]*:\\)ARCHIVE:[ \t]+\\(.*\\)" | |
143 | nil t) | |
144 | (setq file (org-extract-archive-file | |
145 | (org-match-string-no-properties 2))) | |
146 | (and file (> (length file) 0) (file-exists-p file) | |
147 | (add-to-list 'files file))))) | |
148 | (setq files (nreverse files)) | |
149 | (setq file (org-extract-archive-file)) | |
150 | (and file (> (length file) 0) (file-exists-p file) | |
151 | (add-to-list 'files file)) | |
152 | files)) | |
153 | ||
154 | (defun org-extract-archive-file (&optional location) | |
b349f79f CD |
155 | "Extract and expand the file name from archive LOCATION. |
156 | if LOCATION is not given, the value of `org-archive-location' is used." | |
20908596 CD |
157 | (setq location (or location org-archive-location)) |
158 | (if (string-match "\\(.*\\)::\\(.*\\)" location) | |
159 | (if (= (match-beginning 1) (match-end 1)) | |
160 | (buffer-file-name) | |
161 | (expand-file-name | |
b349f79f CD |
162 | (format (match-string 1 location) |
163 | (file-name-nondirectory buffer-file-name)))))) | |
20908596 CD |
164 | |
165 | (defun org-extract-archive-heading (&optional location) | |
b349f79f CD |
166 | "Extract the heading from archive LOCATION. |
167 | if LOCATION is not given, the value of `org-archive-location' is used." | |
20908596 CD |
168 | (setq location (or location org-archive-location)) |
169 | (if (string-match "\\(.*\\)::\\(.*\\)" location) | |
0bd48b37 CD |
170 | (format (match-string 2 location) |
171 | (file-name-nondirectory buffer-file-name)))) | |
20908596 CD |
172 | |
173 | (defun org-archive-subtree (&optional find-done) | |
174 | "Move the current subtree to the archive. | |
175 | The archive can be a certain top-level heading in the current file, or in | |
176 | a different file. The tree will be moved to that location, the subtree | |
177 | heading be marked DONE, and the current time will be added. | |
178 | ||
179 | When called with prefix argument FIND-DONE, find whole trees without any | |
180 | open TODO items and archive them (after getting confirmation from the user). | |
33306645 | 181 | If the cursor is not at a headline when this command is called, try all level |
20908596 CD |
182 | 1 trees. If the cursor is on a headline, only try the direct children of |
183 | this heading." | |
184 | (interactive "P") | |
185 | (if find-done | |
186 | (org-archive-all-done) | |
187 | ;; Save all relevant TODO keyword-relatex variables | |
188 | ||
189 | (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler | |
190 | (tr-org-todo-keywords-1 org-todo-keywords-1) | |
191 | (tr-org-todo-kwd-alist org-todo-kwd-alist) | |
192 | (tr-org-done-keywords org-done-keywords) | |
193 | (tr-org-todo-regexp org-todo-regexp) | |
194 | (tr-org-todo-line-regexp org-todo-line-regexp) | |
195 | (tr-org-odd-levels-only org-odd-levels-only) | |
196 | (this-buffer (current-buffer)) | |
197 | ;; start of variables that will be used for saving context | |
198 | ;; The compiler complains about them - keep them anyway! | |
199 | (file (abbreviate-file-name (buffer-file-name))) | |
200 | (olpath (mapconcat 'identity (org-get-outline-path) "/")) | |
201 | (time (format-time-string | |
202 | (substring (cdr org-time-stamp-formats) 1 -1) | |
203 | (current-time))) | |
204 | category todo priority ltags itags | |
205 | ;; end of variables that will be used for saving context | |
b349f79f | 206 | location afile heading buffer level newfile-p visiting) |
20908596 CD |
207 | |
208 | ;; Find the local archive location | |
209 | (setq location (org-get-local-archive-location) | |
210 | afile (org-extract-archive-file location) | |
211 | heading (org-extract-archive-heading location)) | |
212 | (unless afile | |
213 | (error "Invalid `org-archive-location'")) | |
214 | ||
215 | (if (> (length afile) 0) | |
216 | (setq newfile-p (not (file-exists-p afile)) | |
b349f79f CD |
217 | visiting (find-buffer-visiting afile) |
218 | buffer (or visiting (find-file-noselect afile))) | |
20908596 CD |
219 | (setq buffer (current-buffer))) |
220 | (unless buffer | |
221 | (error "Cannot access file \"%s\"" afile)) | |
222 | (if (and (> (length heading) 0) | |
223 | (string-match "^\\*+" heading)) | |
224 | (setq level (match-end 0)) | |
225 | (setq heading nil level 0)) | |
226 | (save-excursion | |
227 | (org-back-to-heading t) | |
228 | ;; Get context information that will be lost by moving the tree | |
229 | (org-refresh-category-properties) | |
230 | (setq category (org-get-category) | |
231 | todo (and (looking-at org-todo-line-regexp) | |
232 | (match-string 2)) | |
233 | priority (org-get-priority | |
234 | (if (match-end 3) (match-string 3) "")) | |
235 | ltags (org-get-tags) | |
236 | itags (org-delete-all ltags (org-get-tags-at))) | |
237 | (setq ltags (mapconcat 'identity ltags " ") | |
238 | itags (mapconcat 'identity itags " ")) | |
239 | ;; We first only copy, in case something goes wrong | |
b349f79f | 240 | ;; we need to protect `this-command', to avoid kill-region sets it, |
20908596 | 241 | ;; which would lead to duplication of subtrees |
b349f79f | 242 | (let (this-command) (org-copy-subtree 1 nil t)) |
20908596 CD |
243 | (set-buffer buffer) |
244 | ;; Enforce org-mode for the archive buffer | |
245 | (if (not (org-mode-p)) | |
246 | ;; Force the mode for future visits. | |
247 | (let ((org-insert-mode-line-in-empty-file t) | |
248 | (org-inhibit-startup t)) | |
249 | (call-interactively 'org-mode))) | |
250 | (when newfile-p | |
251 | (goto-char (point-max)) | |
252 | (insert (format "\nArchived entries from file %s\n\n" | |
253 | (buffer-file-name this-buffer)))) | |
254 | ;; Force the TODO keywords of the original buffer | |
255 | (let ((org-todo-line-regexp tr-org-todo-line-regexp) | |
256 | (org-todo-keywords-1 tr-org-todo-keywords-1) | |
257 | (org-todo-kwd-alist tr-org-todo-kwd-alist) | |
258 | (org-done-keywords tr-org-done-keywords) | |
259 | (org-todo-regexp tr-org-todo-regexp) | |
260 | (org-todo-line-regexp tr-org-todo-line-regexp) | |
261 | (org-odd-levels-only | |
262 | (if (local-variable-p 'org-odd-levels-only (current-buffer)) | |
263 | org-odd-levels-only | |
264 | tr-org-odd-levels-only))) | |
265 | (goto-char (point-min)) | |
266 | (show-all) | |
267 | (if heading | |
268 | (progn | |
269 | (if (re-search-forward | |
270 | (concat "^" (regexp-quote heading) | |
afe98dfa | 271 | (org-re "[ \t]*\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\($\\|\r\\)")) |
20908596 CD |
272 | nil t) |
273 | (goto-char (match-end 0)) | |
274 | ;; Heading not found, just insert it at the end | |
275 | (goto-char (point-max)) | |
276 | (or (bolp) (insert "\n")) | |
277 | (insert "\n" heading "\n") | |
278 | (end-of-line 0)) | |
279 | ;; Make the subtree visible | |
280 | (show-subtree) | |
ed21c5c8 CD |
281 | (if org-archive-reversed-order |
282 | (progn | |
283 | (org-back-to-heading t) | |
284 | (outline-next-heading)) | |
285 | (org-end-of-subtree t)) | |
20908596 CD |
286 | (skip-chars-backward " \t\r\n") |
287 | (and (looking-at "[ \t\r\n]*") | |
288 | (replace-match "\n\n"))) | |
289 | ;; No specific heading, just go to end of file. | |
290 | (goto-char (point-max)) (insert "\n")) | |
291 | ;; Paste | |
c8d0cf5c | 292 | (org-paste-subtree (org-get-valid-level level (and heading 1))) |
20908596 CD |
293 | |
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 | ||
304 | ;; Add the context info | |
305 | (when org-archive-save-context-info | |
306 | (let ((l org-archive-save-context-info) e n v) | |
307 | (while (setq e (pop l)) | |
308 | (when (and (setq v (symbol-value e)) | |
309 | (stringp v) (string-match "\\S-" v)) | |
310 | (setq n (concat "ARCHIVE_" (upcase (symbol-name e)))) | |
311 | (org-entry-put (point) n v))))) | |
312 | ||
313 | ;; Save and kill the buffer, if it is not the same buffer. | |
b349f79f | 314 | (when (not (eq this-buffer buffer)) |
8bfe682a | 315 | (save-buffer)) |
b349f79f | 316 | )) |
20908596 CD |
317 | ;; Here we are back in the original buffer. Everything seems to have |
318 | ;; worked. So now cut the tree and finish up. | |
319 | (let (this-command) (org-cut-subtree)) | |
c8d0cf5c CD |
320 | (when (featurep 'org-inlinetask) |
321 | (org-inlinetask-remove-END-maybe)) | |
b349f79f | 322 | (setq org-markers-to-move nil) |
20908596 CD |
323 | (message "Subtree archived %s" |
324 | (if (eq this-buffer buffer) | |
325 | (concat "under heading: " heading) | |
0bd48b37 | 326 | (concat "in file: " (abbreviate-file-name afile)))))) |
c8d0cf5c CD |
327 | (org-reveal) |
328 | (if (looking-at "^[ \t]*$") | |
329 | (outline-next-visible-heading 1))) | |
20908596 CD |
330 | |
331 | (defun org-archive-to-archive-sibling () | |
332 | "Archive the current heading by moving it under the archive sibling. | |
333 | The archive sibling is a sibling of the heading with the heading name | |
334 | `org-archive-sibling-heading' and an `org-archive-tag' tag. If this | |
335 | sibling does not exist, it will be created at the end of the subtree." | |
336 | (interactive) | |
337 | (save-restriction | |
338 | (widen) | |
339 | (let (b e pos leader level) | |
340 | (org-back-to-heading t) | |
341 | (looking-at outline-regexp) | |
342 | (setq leader (match-string 0) | |
343 | level (funcall outline-level)) | |
344 | (setq pos (point)) | |
345 | (condition-case nil | |
346 | (outline-up-heading 1 t) | |
71d35b24 | 347 | (error (setq e (point-max)) (goto-char (point-min)))) |
20908596 | 348 | (setq b (point)) |
71d35b24 CD |
349 | (unless e |
350 | (condition-case nil | |
351 | (org-end-of-subtree t t) | |
352 | (error (goto-char (point-max)))) | |
353 | (setq e (point))) | |
20908596 CD |
354 | (goto-char b) |
355 | (unless (re-search-forward | |
356 | (concat "^" (regexp-quote leader) | |
357 | "[ \t]*" | |
358 | org-archive-sibling-heading | |
359 | "[ \t]*:" | |
360 | org-archive-tag ":") e t) | |
361 | (goto-char e) | |
362 | (or (bolp) (newline)) | |
363 | (insert leader org-archive-sibling-heading "\n") | |
364 | (beginning-of-line 0) | |
365 | (org-toggle-tag org-archive-tag 'on)) | |
366 | (beginning-of-line 1) | |
ed21c5c8 CD |
367 | (if org-archive-reversed-order |
368 | (outline-next-heading) | |
369 | (org-end-of-subtree t t)) | |
20908596 CD |
370 | (save-excursion |
371 | (goto-char pos) | |
ff4be292 | 372 | (let ((this-command this-command)) (org-cut-subtree))) |
20908596 CD |
373 | (org-paste-subtree (org-get-valid-level level 1)) |
374 | (org-set-property | |
375 | "ARCHIVE_TIME" | |
376 | (format-time-string | |
377 | (substring (cdr org-time-stamp-formats) 1 -1) | |
378 | (current-time))) | |
379 | (outline-up-heading 1 t) | |
380 | (hide-subtree) | |
621f83e4 | 381 | (org-cycle-show-empty-lines 'folded) |
0bd48b37 | 382 | (goto-char pos))) |
c8d0cf5c CD |
383 | (org-reveal) |
384 | (if (looking-at "^[ \t]*$") | |
385 | (outline-next-visible-heading 1))) | |
20908596 CD |
386 | |
387 | (defun org-archive-all-done (&optional tag) | |
388 | "Archive sublevels of the current tree without open TODO items. | |
389 | If the cursor is not on a headline, try all level 1 trees. If | |
390 | it is on a headline, try all direct children. | |
391 | When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag." | |
392 | (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1 | |
393 | (rea (concat ".*:" org-archive-tag ":")) | |
394 | (begm (make-marker)) | |
395 | (endm (make-marker)) | |
396 | (question (if tag "Set ARCHIVE tag (no open TODO items)? " | |
397 | "Move subtree to archive (no open TODO items)? ")) | |
398 | beg end (cntarch 0)) | |
399 | (if (org-on-heading-p) | |
400 | (progn | |
401 | (setq re1 (concat "^" (regexp-quote | |
402 | (make-string | |
ed21c5c8 CD |
403 | (+ (- (match-end 0) (match-beginning 0) 1) |
404 | (if org-odd-levels-only 2 1)) | |
20908596 CD |
405 | ?*)) |
406 | " ")) | |
407 | (move-marker begm (point)) | |
408 | (move-marker endm (org-end-of-subtree t))) | |
409 | (setq re1 "^* ") | |
410 | (move-marker begm (point-min)) | |
411 | (move-marker endm (point-max))) | |
412 | (save-excursion | |
413 | (goto-char begm) | |
414 | (while (re-search-forward re1 endm t) | |
415 | (setq beg (match-beginning 0) | |
416 | end (save-excursion (org-end-of-subtree t) (point))) | |
417 | (goto-char beg) | |
418 | (if (re-search-forward re end t) | |
419 | (goto-char end) | |
420 | (goto-char beg) | |
421 | (if (and (or (not tag) (not (looking-at rea))) | |
422 | (y-or-n-p question)) | |
423 | (progn | |
424 | (if tag | |
425 | (org-toggle-tag org-archive-tag 'on) | |
426 | (org-archive-subtree)) | |
427 | (setq cntarch (1+ cntarch))) | |
428 | (goto-char end))))) | |
429 | (message "%d trees archived" cntarch))) | |
430 | ||
431 | (defun org-toggle-archive-tag (&optional find-done) | |
432 | "Toggle the archive tag for the current headline. | |
433 | With prefix ARG, check all children of current headline and offer tagging | |
434 | the children that do not contain any open TODO items." | |
435 | (interactive "P") | |
436 | (if find-done | |
437 | (org-archive-all-done 'tag) | |
438 | (let (set) | |
439 | (save-excursion | |
440 | (org-back-to-heading t) | |
441 | (setq set (org-toggle-tag org-archive-tag)) | |
442 | (when set (hide-subtree))) | |
443 | (and set (beginning-of-line 1)) | |
444 | (message "Subtree %s" (if set "archived" "unarchived"))))) | |
445 | ||
8d642074 CD |
446 | (defun org-archive-set-tag () |
447 | "Set the ARCHIVE tag." | |
448 | (interactive) | |
449 | (org-toggle-tag org-archive-tag 'on)) | |
450 | ||
451 | ;;;###autoload | |
452 | (defun org-archive-subtree-default () | |
453 | "Archive the current subtree with the default command. | |
454 | This command is set with the variable `org-archive-default-command'." | |
455 | (interactive) | |
8bfe682a CD |
456 | (call-interactively org-archive-default-command)) |
457 | ||
5dec9555 | 458 | ;;;###autoload |
8bfe682a CD |
459 | (defun org-archive-subtree-default-with-confirmation () |
460 | "Archive the current subtree with the default command. | |
461 | This command is set with the variable `org-archive-default-command'." | |
462 | (interactive) | |
463 | (if (y-or-n-p "Archive this subtree or entry? ") | |
464 | (call-interactively org-archive-default-command) | |
465 | (error "Abort"))) | |
8d642074 | 466 | |
20908596 CD |
467 | (provide 'org-archive) |
468 | ||
b349f79f | 469 | |
20908596 | 470 | ;;; org-archive.el ends here |