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