1 ;;; org.el --- Outline-based notes management and organizer
2 ;; Carstens outline-mode for keeping track of everything.
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
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
29 ;; project planning with a fast and effective plain-text system.
31 ;; Org-mode develops organizational tasks around NOTES files that contain
32 ;; information about projects as plain text. Org-mode is implemented on
33 ;; top of outline-mode, which makes it possible to keep the content of
34 ;; large files well structured. Visibility cycling and structure editing
35 ;; help to work with the tree. Tables are easily created with a built-in
36 ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
37 ;; and scheduling. It dynamically compiles entries into an agenda that
38 ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
39 ;; Plain text URL-like links connect to websites, emails, Usenet
40 ;; messages, BBDB entries, and any files related to the projects. For
41 ;; printing and sharing of notes, an Org-mode file can be exported as a
42 ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
43 ;; iCalendar file. It can also serve as a publishing tool for a set of
46 ;; Installation and Activation
47 ;; ---------------------------
48 ;; See the corresponding sections in the manual at
50 ;; http://orgmode.org/org.html#Installation
54 ;; The documentation of Org-mode can be found in the TeXInfo file. The
55 ;; distribution also contains a PDF version of it. At the homepage of
56 ;; Org-mode, you can read the same text online as HTML. There is also an
57 ;; excellent reference card made by Philip Rooke. This card can be found
58 ;; in the etc/ directory of Emacs 22.
60 ;; A list of recent changes can be found at
61 ;; http://orgmode.org/Changes.html
65 (defvar org-inhibit-highlight-removal nil
) ; dynamically scoped param
66 (defvar org-table-formula-constants-local nil
67 "Local version of `org-table-formula-constants'.")
68 (make-variable-buffer-local 'org-table-formula-constants-local
)
70 ;;;; Require other packages
76 ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
77 ;; the file noutline.el being loaded.
78 (if (featurep 'xemacs
) (condition-case nil
(require 'noutline
)))
79 ;; We require noutline, which might be provided in outline.el
80 (require 'outline
) (require 'noutline
)
81 ;; Other stuff we need.
83 (unless (fboundp 'time-subtract
) (defalias 'time-subtract
'subtract-time
))
90 ;;;; Customization variables
94 (defconst org-version
"6.03pre01"
95 "The version number of the file org.el.")
97 (defun org-version (&optional here
)
98 "Show the org-mode version in the echo area.
99 With prefix arg HERE, insert it at point."
101 (let ((version (format "Org-mode version %s" org-version
)))
106 ;;; Compatibility constants
108 ;;; The custom variables
111 "Outline-based notes management and organizer."
117 (defcustom org-load-hook nil
118 "Hook that is run after org.el has been loaded."
122 (defvar org-modules
) ; defined below
123 (defvar org-modules-loaded nil
124 "Have the modules been loaded already?")
126 (defun org-load-modules-maybe (&optional force
)
127 "Load all extensions listed in `org-default-extensions'."
128 (when (or force
(not org-modules-loaded
))
130 (condition-case nil
(require ext
)
131 (error (message "Problems while trying to load feature `%s'" ext
))))
133 (setq org-modules-loaded t
)))
135 (defun org-set-modules (var value
)
136 "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
138 (when (featurep 'org
)
139 (org-load-modules-maybe 'force
)))
141 (when (org-bound-and-true-p org-modules
)
142 (let ((a (member 'org-infojs org-modules
)))
143 (and a
(setcar a
'org-jsinfo
))))
145 (defcustom org-modules
'(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-wl
)
146 "Modules that should always be loaded together with org.el.
147 If a description starts with <C>, the file is not part of Emacs
148 and loading it will require that you have downloaded and properly installed
149 the org-mode distribution.
151 You can also use this system to load external packages (i.e. neither Org
152 core modules, not modules from the CONTRIB directory). Just add symbols
153 to the end of the list. If the package is called org-xyz.el, then you need
154 to add the symbol `xyz', and the package must have a call to
158 :set
'org-set-modules
161 (const :tag
" bbdb: Links to BBDB entries" org-bbdb
)
162 (const :tag
" bibtex: Links to BibTeX entries" org-bibtex
)
163 (const :tag
" gnus: Links to GNUS folders/messages" org-gnus
)
164 (const :tag
" info: Links to Info nodes" org-info
)
165 (const :tag
" jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo
)
166 (const :tag
" irc: Links to IRC/ERC chat sessions" org-irc
)
167 (const :tag
" mac-message: Links to messages in Apple Mail" org-mac-message
)
168 (const :tag
" mew Links to Mew folders/messages" org-mew
)
169 (const :tag
" mhe: Links to MHE folders/messages" org-mhe
)
170 (const :tag
" rmail: Links to RMAIL folders/messages" org-rmail
)
171 (const :tag
" vm: Links to VM folders/messages" org-vm
)
172 (const :tag
" wl: Links to Wanderlust folders/messages" org-wl
)
173 (const :tag
" mouse: Additional mouse support" org-mouse
)
175 (const :tag
"C annotate-file: Annotate a file with org syntax" org-annotate-file
)
176 (const :tag
"C bookmark: Org links to bookmarks" org-bookmark
)
177 (const :tag
"C depend: TODO dependencies for Org-mode" org-depend
)
178 (const :tag
"C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol
)
179 (const :tag
"C eval: Include command output as text" org-eval
)
180 (const :tag
"C expiry: Expiry mechanism for Org entries" org-expiry
)
181 (const :tag
"C id: Global id's for identifying entries" org-id
)
182 (const :tag
"C interactive-query: Interactive modification of tags query" org-interactive-query
)
183 (const :tag
"C mairix: Hook mairix search into Org for different MUAs" org-mairix
)
184 (const :tag
"C man: Support for links to manpages in Org-mode" org-man
)
185 (const :tag
"C mtags: Support for muse-like tags" org-mtags
)
186 (const :tag
"C panel: Simple routines for us with bad memory" org-panel
)
187 (const :tag
"C registry: A registry for Org links" org-registry
)
188 (const :tag
"C org2rem: Convert org appointments into reminders" org2rem
)
189 (const :tag
"C screen: Visit screen sessions through Org-mode links" org-screen
)
190 (const :tag
"C toc: Table of contents for Org-mode buffer" org-toc
)
191 (const :tag
"C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert
)
192 (repeat :tag
"External packages" :inline t
(symbol :tag
"Package"))))
195 (defgroup org-startup nil
196 "Options concerning startup of Org-mode."
200 (defcustom org-startup-folded t
201 "Non-nil means, entering Org-mode will switch to OVERVIEW.
202 This can also be configured on a per-file basis by adding one of
203 the following lines anywhere in the buffer:
210 (const :tag
"nofold: show all" nil
)
211 (const :tag
"fold: overview" t
)
212 (const :tag
"content: all headlines" content
)))
214 (defcustom org-startup-truncated t
215 "Non-nil means, entering Org-mode will set `truncate-lines'.
216 This is useful since some lines containing links can be very long and
217 uninteresting. Also tables look terrible when wrapped."
221 (defcustom org-startup-align-all-tables nil
222 "Non-nil means, align all tables when visiting a file.
223 This is useful when the column width in tables is forced with <N> cookies
224 in table fields. Such tables will look correct only after the first re-align.
225 This can also be configured on a per-file basis by adding one of
226 the following lines anywhere in the buffer:
232 (defcustom org-insert-mode-line-in-empty-file nil
233 "Non-nil means insert the first line setting Org-mode in empty files.
234 When the function `org-mode' is called interactively in an empty file, this
235 normally means that the file name does not automatically trigger Org-mode.
236 To ensure that the file will always be in Org-mode in the future, a
237 line enforcing Org-mode will be inserted into the buffer, if this option
242 (defcustom org-replace-disputed-keys nil
243 "Non-nil means use alternative key bindings for some keys.
244 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
245 These keys are also used by other packages like `CUA-mode' or `windmove.el'.
246 If you want to use Org-mode together with one of these other modes,
247 or more generally if you would like to move some Org-mode commands to
248 other keys, set this variable and configure the keys with the variable
251 This option is only relevant at load-time of Org-mode, and must be set
252 *before* org.el is loaded. Changing it requires a restart of Emacs to
257 (if (fboundp 'defvaralias
)
258 (defvaralias 'org-CUA-compatible
'org-replace-disputed-keys
))
260 (defcustom org-disputed-keys
261 '(([(shift up
)] .
[(meta p
)])
262 ([(shift down
)] .
[(meta n
)])
263 ([(shift left
)] .
[(meta -
)])
264 ([(shift right
)] .
[(meta +)])
265 ([(control shift right
)] .
[(meta shift
+)])
266 ([(control shift left
)] .
[(meta shift -
)]))
267 "Keys for which Org-mode and other modes compete.
268 This is an alist, cars are the default keys, second element specifies
269 the alternative to use when `org-replace-disputed-keys' is t.
271 Keys can be specified in any syntax supported by `define-key'.
272 The value of this option takes effect only at Org-mode's startup,
273 therefore you'll have to restart Emacs to apply it after changing."
278 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
279 Or return the original if not disputed."
280 (if org-replace-disputed-keys
281 (let* ((nkey (key-description key
))
282 (x (org-find-if (lambda (x)
283 (equal (key-description (car x
)) nkey
))
288 (defun org-find-if (predicate seq
)
291 (if (funcall predicate
(car seq
))
292 (throw 'exit
(car seq
))
295 (defun org-defkey (keymap key def
)
296 "Define a key, possibly translated, as returned by `org-key'."
297 (define-key keymap
(org-key key
) def
))
299 (defcustom org-ellipsis nil
300 "The ellipsis to use in the Org-mode outline.
301 When nil, just use the standard three dots. When a string, use that instead,
302 When a face, use the standart 3 dots, but with the specified face.
303 The change affects only Org-mode (which will then use its own display table).
304 Changing this requires executing `M-x org-mode' in a buffer to become
307 :type
'(choice (const :tag
"Default" nil
)
308 (face :tag
"Face" :value org-warning
)
309 (string :tag
"String" :value
"...#")))
311 (defvar org-display-table nil
312 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
314 (defgroup org-keywords nil
315 "Keywords in Org-mode."
319 (defcustom org-deadline-string
"DEADLINE:"
320 "String to mark deadline entries.
321 A deadline is this string, followed by a time stamp. Should be a word,
322 terminated by a colon. You can insert a schedule keyword and
323 a timestamp with \\[org-deadline].
324 Changes become only effective after restarting Emacs."
328 (defcustom org-scheduled-string
"SCHEDULED:"
329 "String to mark scheduled TODO entries.
330 A schedule is this string, followed by a time stamp. Should be a word,
331 terminated by a colon. You can insert a schedule keyword and
332 a timestamp with \\[org-schedule].
333 Changes become only effective after restarting Emacs."
337 (defcustom org-closed-string
"CLOSED:"
338 "String used as the prefix for timestamps logging closing a TODO entry."
342 (defcustom org-clock-string
"CLOCK:"
343 "String used as prefix for timestamps clocking work hours on an item."
347 (defcustom org-comment-string
"COMMENT"
348 "Entries starting with this keyword will never be exported.
349 An entry can be toggled between COMMENT and normal with
350 \\[org-toggle-comment].
351 Changes become only effective after restarting Emacs."
355 (defcustom org-quote-string
"QUOTE"
356 "Entries starting with this keyword will be exported in fixed-width font.
357 Quoting applies only to the text in the entry following the headline, and does
358 not extend beyond the next headline, even if that is lower level.
359 An entry can be toggled between QUOTE and normal with
360 \\[org-toggle-fixed-width-section]."
364 (defconst org-repeat-re
365 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
366 "Regular expression for specifying repeated events.
367 After a match, group 1 contains the repeat expression.")
369 (defgroup org-structure nil
370 "Options concerning the general structure of Org-mode files."
374 (defgroup org-reveal-location nil
375 "Options about how to make context of a location visible."
376 :tag
"Org Reveal Location"
377 :group
'org-structure
)
379 (defconst org-context-choice
381 (const :tag
"Always" t
)
382 (const :tag
"Never" nil
)
383 (repeat :greedy t
:tag
"Individual contexts"
385 (choice :tag
"Context"
392 (const bookmark-jump
)
396 "Contexts for the reveal options.")
398 (defcustom org-show-hierarchy-above
'((default . t
))
399 "Non-nil means, show full hierarchy when revealing a location.
400 Org-mode often shows locations in an org-mode file which might have
401 been invisible before. When this is set, the hierarchy of headings
402 above the exposed location is shown.
403 Turning this off for example for sparse trees makes them very compact.
404 Instead of t, this can also be an alist specifying this option for different
405 contexts. Valid contexts are
406 agenda when exposing an entry from the agenda
407 org-goto when using the command `org-goto' on key C-c C-j
408 occur-tree when using the command `org-occur' on key C-c /
409 tags-tree when constructing a sparse tree based on tags matches
410 link-search when exposing search matches associated with a link
411 mark-goto when exposing the jump goal of a mark
412 bookmark-jump when exposing a bookmark location
413 isearch when exiting from an incremental search
414 default default for all contexts not set explicitly"
415 :group
'org-reveal-location
416 :type org-context-choice
)
418 (defcustom org-show-following-heading
'((default . nil
))
419 "Non-nil means, show following heading when revealing a location.
420 Org-mode often shows locations in an org-mode file which might have
421 been invisible before. When this is set, the heading following the
423 Turning this off for example for sparse trees makes them very compact,
424 but makes it harder to edit the location of the match. In such a case,
425 use the command \\[org-reveal] to show more context.
426 Instead of t, this can also be an alist specifying this option for different
427 contexts. See `org-show-hierarchy-above' for valid contexts."
428 :group
'org-reveal-location
429 :type org-context-choice
)
431 (defcustom org-show-siblings
'((default . nil
) (isearch t
))
432 "Non-nil means, show all sibling heading when revealing a location.
433 Org-mode often shows locations in an org-mode file which might have
434 been invisible before. When this is set, the sibling of the current entry
435 heading are all made visible. If `org-show-hierarchy-above' is t,
436 the same happens on each level of the hierarchy above the current entry.
438 By default this is on for the isearch context, off for all other contexts.
439 Turning this off for example for sparse trees makes them very compact,
440 but makes it harder to edit the location of the match. In such a case,
441 use the command \\[org-reveal] to show more context.
442 Instead of t, this can also be an alist specifying this option for different
443 contexts. See `org-show-hierarchy-above' for valid contexts."
444 :group
'org-reveal-location
445 :type org-context-choice
)
447 (defcustom org-show-entry-below
'((default . nil
))
448 "Non-nil means, show the entry below a headline when revealing a location.
449 Org-mode often shows locations in an org-mode file which might have
450 been invisible before. When this is set, the text below the headline that is
451 exposed is also shown.
453 By default this is off for all contexts.
454 Instead of t, this can also be an alist specifying this option for different
455 contexts. See `org-show-hierarchy-above' for valid contexts."
456 :group
'org-reveal-location
457 :type org-context-choice
)
459 (defcustom org-indirect-buffer-display
'other-window
460 "How should indirect tree buffers be displayed?
461 This applies to indirect buffers created with the commands
462 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
464 current-window Display in the current window
465 other-window Just display in another window.
466 dedicated-frame Create one new frame, and re-use it each time.
467 new-frame Make a new frame each time. Note that in this case
468 previously-made indirect buffers are kept, and you need to
469 kill these buffers yourself."
470 :group
'org-structure
471 :group
'org-agenda-windows
473 (const :tag
"In current window" current-window
)
474 (const :tag
"In current frame, other window" other-window
)
475 (const :tag
"Each time a new frame" new-frame
)
476 (const :tag
"One dedicated frame" dedicated-frame
)))
478 (defgroup org-cycle nil
479 "Options concerning visibility cycling in Org-mode."
481 :group
'org-structure
)
483 (defcustom org-drawers
'("PROPERTIES" "CLOCK")
484 "Names of drawers. Drawers are not opened by cycling on the headline above.
485 Drawers only open with a TAB on the drawer line itself. A drawer looks like
490 The drawer \"PROPERTIES\" is special for capturing properties through
493 Drawers can be defined on the per-file basis with a line like:
495 #+DRAWERS: HIDDEN STATE PROPERTIES"
496 :group
'org-structure
497 :type
'(repeat (string :tag
"Drawer Name")))
499 (defcustom org-cycle-global-at-bob nil
500 "Cycle globally if cursor is at beginning of buffer and not at a headline.
501 This makes it possible to do global cycling without having to use S-TAB or
502 C-u TAB. For this special case to work, the first line of the buffer
503 must not be a headline - it may be empty ot some other text. When used in
504 this way, `org-cycle-hook' is disables temporarily, to make sure the
505 cursor stays at the beginning of the buffer.
506 When this option is nil, don't do anything special at the beginning
511 (defcustom org-cycle-emulate-tab t
512 "Where should `org-cycle' emulate TAB.
514 white Only in completely white lines
515 whitestart Only at the beginning of lines, before the first non-white char
516 t Everywhere except in headlines
517 exc-hl-bol Everywhere except at the start of a headline
518 If TAB is used in a place where it does not emulate TAB, the current subtree
519 visibility is cycled."
521 :type
'(choice (const :tag
"Never" nil
)
522 (const :tag
"Only in completely white lines" white
)
523 (const :tag
"Before first char in a line" whitestart
)
524 (const :tag
"Everywhere except in headlines" t
)
525 (const :tag
"Everywhere except at bol in headlines" exc-hl-bol
)
528 (defcustom org-cycle-separator-lines
2
529 "Number of empty lines needed to keep an empty line between collapsed trees.
530 If you leave an empty line between the end of a subtree and the following
531 headline, this empty line is hidden when the subtree is folded.
532 Org-mode will leave (exactly) one empty line visible if the number of
533 empty lines is equal or larger to the number given in this variable.
534 So the default 2 means, at least 2 empty lines after the end of a subtree
535 are needed to produce free space between a collapsed subtree and the
538 Special case: when 0, never leave empty lines in collapsed view."
542 (defcustom org-cycle-hook
'(org-cycle-hide-archived-subtrees
543 org-cycle-hide-drawers
544 org-cycle-show-empty-lines
545 org-optimize-window-after-visibility-change
)
546 "Hook that is run after `org-cycle' has changed the buffer visibility.
547 The function(s) in this hook must accept a single argument which indicates
548 the new state that was set by the most recent `org-cycle' command. The
549 argument is a symbol. After a global state change, it can have the values
550 `overview', `content', or `all'. After a local state change, it can have
551 the values `folded', `children', or `subtree'."
555 (defgroup org-edit-structure nil
556 "Options concerning structure editing in Org-mode."
557 :tag
"Org Edit Structure"
558 :group
'org-structure
)
560 (defcustom org-odd-levels-only nil
561 "Non-nil means, skip even levels and only use odd levels for the outline.
562 This has the effect that two stars are being added/taken away in
563 promotion/demotion commands. It also influences how levels are
564 handled by the exporters.
565 Changing it requires restart of `font-lock-mode' to become effective
566 for fontification also in regions already fontified.
567 You may also set this on a per-file basis by adding one of the following
572 :group
'org-edit-structure
573 :group
'org-font-lock
576 (defcustom org-adapt-indentation t
577 "Non-nil means, adapt indentation when promoting and demoting.
578 When this is set and the *entire* text in an entry is indented, the
579 indentation is increased by one space in a demotion command, and
580 decreased by one in a promotion command. If any line in the entry
581 body starts at column 0, indentation is not changed at all."
582 :group
'org-edit-structure
585 (defcustom org-special-ctrl-a
/e nil
586 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
587 When t, `C-a' will bring back the cursor to the beginning of the
588 headline text, i.e. after the stars and after a possible TODO keyword.
589 In an item, this will be the position after the bullet.
590 When the cursor is already at that position, another `C-a' will bring
591 it to the beginning of the line.
592 `C-e' will jump to the end of the headline, ignoring the presence of tags
593 in the headline. A second `C-e' will then jump to the true end of the
594 line, after any tags.
595 When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
596 and only a directly following, identical keypress will bring the cursor
597 to the special positions."
598 :group
'org-edit-structure
600 (const :tag
"off" nil
)
601 (const :tag
"after bullet first" t
)
602 (const :tag
"border first" reversed
)))
604 (if (fboundp 'defvaralias
)
605 (defvaralias 'org-special-ctrl-a
'org-special-ctrl-a
/e
))
607 (defcustom org-special-ctrl-k nil
608 "Non-nil means `C-k' will behave specially in headlines.
609 When nil, `C-k' will call the default `kill-line' command.
610 When t, the following will happen while the cursor is in the headline:
612 - When the cursor is at the beginning of a headline, kill the entire
613 line and possible the folded subtree below the line.
614 - When in the middle of the headline text, kill the headline up to the tags.
615 - When after the headline text, kill the tags."
616 :group
'org-edit-structure
619 (defcustom org-M-RET-may-split-line
'((default . t
))
620 "Non-nil means, M-RET will split the line at the cursor position.
621 When nil, it will go to the end of the line before making a
623 You may also set this option in a different way for different
624 contexts. Valid contexts are:
626 headline when creating a new headline
627 item when creating a new item
628 table in a table field
629 default the value to be used for all contexts not explicitly
631 :group
'org-structure
634 (const :tag
"Always" t
)
635 (const :tag
"Never" nil
)
636 (repeat :greedy t
:tag
"Individual contexts"
638 (choice :tag
"Context"
646 (defcustom org-blank-before-new-entry
'((heading . nil
)
647 (plain-list-item . nil
))
648 "Should `org-insert-heading' leave a blank line before new heading/item?
649 The value is an alist, with `heading' and `plain-list-item' as car,
650 and a boolean flag as cdr."
651 :group
'org-edit-structure
653 (cons (const heading
) (boolean))
654 (cons (const plain-list-item
) (boolean))))
656 (defcustom org-insert-heading-hook nil
657 "Hook being run after inserting a new heading."
658 :group
'org-edit-structure
661 (defcustom org-enable-fixed-width-editor t
662 "Non-nil means, lines starting with \":\" are treated as fixed-width.
663 This currently only means, they are never auto-wrapped.
664 When nil, such lines will be treated like ordinary lines.
665 See also the QUOTE keyword."
666 :group
'org-edit-structure
669 (defcustom org-goto-auto-isearch t
670 "Non-nil means, typing characters in org-goto starts incremental search."
671 :group
'org-edit-structure
674 (defgroup org-sparse-trees nil
675 "Options concerning sparse trees in Org-mode."
676 :tag
"Org Sparse Trees"
677 :group
'org-structure
)
679 (defcustom org-highlight-sparse-tree-matches t
680 "Non-nil means, highlight all matches that define a sparse tree.
681 The highlights will automatically disappear the next time the buffer is
682 changed by an edit command."
683 :group
'org-sparse-trees
686 (defcustom org-remove-highlights-with-change t
687 "Non-nil means, any change to the buffer will remove temporary highlights.
688 Such highlights are created by `org-occur' and `org-clock-display'.
689 When nil, `C-c C-c needs to be used to get rid of the highlights.
690 The highlights created by `org-preview-latex-fragment' always need
691 `C-c C-c' to be removed."
692 :group
'org-sparse-trees
697 (defcustom org-occur-hook
'(org-first-headline-recenter)
698 "Hook that is run after `org-occur' has constructed a sparse tree.
699 This can be used to recenter the window to show as much of the structure
701 :group
'org-sparse-trees
704 (defgroup org-plain-lists nil
705 "Options concerning plain lists in Org-mode."
706 :tag
"Org Plain lists"
707 :group
'org-structure
)
709 (defcustom org-cycle-include-plain-lists nil
710 "Non-nil means, include plain lists into visibility cycling.
711 This means that during cycling, plain list items will *temporarily* be
712 interpreted as outline headlines with a level given by 1000+i where i is the
713 indentation of the bullet. In all other operations, plain list items are
714 not seen as headlines. For example, you cannot assign a TODO keyword to
716 :group
'org-plain-lists
719 (defcustom org-plain-list-ordered-item-terminator t
720 "The character that makes a line with leading number an ordered list item.
721 Valid values are ?. and ?\). To get both terminators, use t. While
722 ?. may look nicer, it creates the danger that a line with leading
723 number may be incorrectly interpreted as an item. ?\) therefore is
725 :group
'org-plain-lists
726 :type
'(choice (const :tag
"dot like in \"2.\"" ?.
)
727 (const :tag
"paren like in \"2)\"" ?\
))
728 (const :tab
"both" t
)))
730 (defcustom org-empty-line-terminates-plain-lists nil
731 "Non-nil means, an empty line ends all plain list levels.
732 When nil, empty lines are part of the preceeding item."
733 :group
'org-plain-lists
736 (defcustom org-auto-renumber-ordered-lists t
737 "Non-nil means, automatically renumber ordered plain lists.
738 Renumbering happens when the sequence have been changed with
739 \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
740 use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
741 :group
'org-plain-lists
744 (defcustom org-provide-checkbox-statistics t
745 "Non-nil means, update checkbox statistics after insert and toggle.
746 When this is set, checkbox statistics is updated each time you either insert
747 a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
748 with \\[org-ctrl-c-ctrl-c\\]."
749 :group
'org-plain-lists
752 (defcustom org-description-max-indent
20
753 "Maximum indentation for the second line of a description list.
754 When the indentation would be larger than this, it will become
755 5 characters instead."
756 :group
'org-plain-lists
759 (defgroup org-imenu-and-speedbar nil
760 "Options concerning imenu and speedbar in Org-mode."
761 :tag
"Org Imenu and Speedbar"
762 :group
'org-structure
)
764 (defcustom org-imenu-depth
2
765 "The maximum level for Imenu access to Org-mode headlines.
766 This also applied for speedbar access."
767 :group
'org-imenu-and-speedbar
770 (defgroup org-table nil
771 "Options concerning tables in Org-mode."
775 (defcustom org-enable-table-editor
'optimized
776 "Non-nil means, lines starting with \"|\" are handled by the table editor.
777 When nil, such lines will be treated like ordinary lines.
779 When equal to the symbol `optimized', the table editor will be optimized to
781 - Automatic overwrite mode in front of whitespace in table fields.
782 This makes the structure of the table stay in tact as long as the edited
783 field does not exceed the column width.
784 - Minimize the number of realigns. Normally, the table is aligned each time
785 TAB or RET are pressed to move to another field. With optimization this
786 happens only if changes to a field might have changed the column width.
787 Optimization requires replacing the functions `self-insert-command',
788 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
789 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
790 very good at guessing when a re-align will be necessary, but you can always
791 force one with \\[org-ctrl-c-ctrl-c].
793 If you would like to use the optimized version in Org-mode, but the
794 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
796 This variable can be used to turn on and off the table editor during a session,
797 but in order to toggle optimization, a restart is required.
799 See also the variable `org-table-auto-blank-field'."
802 (const :tag
"off" nil
)
804 (const :tag
"on, optimized" optimized
)))
806 (defcustom org-table-tab-recognizes-table.el t
807 "Non-nil means, TAB will automatically notice a table.el table.
808 When it sees such a table, it moves point into it and - if necessary -
809 calls `table-recognize-table'."
810 :group
'org-table-editing
813 (defgroup org-link nil
814 "Options concerning links in Org-mode."
818 (defvar org-link-abbrev-alist-local nil
819 "Buffer-local version of `org-link-abbrev-alist', which see.
820 The value of this is taken from the #+LINK lines.")
821 (make-variable-buffer-local 'org-link-abbrev-alist-local
)
823 (defcustom org-link-abbrev-alist nil
824 "Alist of link abbreviations.
825 The car of each element is a string, to be replaced at the start of a link.
826 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
827 links in Org-mode buffers can have an optional tag after a double colon, e.g.
829 [[linkkey:tag][description]]
831 If REPLACE is a string, the tag will simply be appended to create the link.
832 If the string contains \"%s\", the tag will be inserted there.
834 REPLACE may also be a function that will be called with the tag as the
835 only argument to create the link, which should be returned as a string.
837 See the manual for examples."
841 (defcustom org-descriptive-links t
842 "Non-nil means, hide link part and only show description of bracket links.
843 Bracket links are like [[link][descritpion]]. This variable sets the initial
844 state in new org-mode buffers. The setting can then be toggled on a
845 per-buffer basis from the Org->Hyperlinks menu."
849 (defcustom org-link-file-path-type
'adaptive
850 "How the path name in file links should be stored.
853 relative Relative to the current directory, i.e. the directory of the file
854 into which the link is being inserted.
855 absolute Absolute path, if possible with ~ for home directory.
856 noabbrev Absolute path, no abbreviation of home directory.
857 adaptive Use relative path for files in the current directory and sub-
858 directories of it. For other files, use an absolute path."
866 (defcustom org-activate-links
'(bracket angle plain radio tag date
)
867 "Types of links that should be activated in Org-mode files.
868 This is a list of symbols, each leading to the activation of a certain link
869 type. In principle, it does not hurt to turn on most link types - there may
870 be a small gain when turning off unused link types. The types are:
872 bracket The recommended [[link][description]] or [[link]] links with hiding.
873 angular Links in angular brackes that may contain whitespace like
874 <bbdb:Carsten Dominik>.
875 plain Plain links in normal text, no whitespace, like http://google.com.
876 radio Text that is matched by a radio target, see manual for details.
877 tag Tag settings in a headline (link to tag search).
878 date Time stamps (link to calendar).
880 Changing this variable requires a restart of Emacs to become effective."
882 :type
'(set (const :tag
"Double bracket links (new style)" bracket
)
883 (const :tag
"Angular bracket links (old style)" angular
)
884 (const :tag
"Plain text links" plain
)
885 (const :tag
"Radio target matches" radio
)
886 (const :tag
"Tags" tag
)
887 (const :tag
"Timestamps" date
)))
889 (defcustom org-make-link-description-function nil
890 "Function to use to generate link descriptions from links. If
891 nil the link location will be used. This function must take two
892 parameters; the first is the link and the second the description
893 org-insert-link has generated, and should return the description
898 (defgroup org-link-store nil
899 "Options concerning storing links in Org-mode."
900 :tag
"Org Store Link"
903 (defcustom org-email-link-description-format
"Email %c: %.30s"
904 "Format of the description part of a link to an email or usenet message.
905 The following %-excapes will be replaced by corresponding information:
907 %F full \"From\" field
908 %f name, taken from \"From\" field, address if no name
910 %t first name in \"To\" field, address if no name
911 %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
912 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
916 You may use normal field width specification between the % and the letter.
917 This is for example useful to limit the length of the subject.
919 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
920 :group
'org-link-store
923 (defcustom org-from-is-user-regexp
925 (when (and user-mail-address
(not (string= user-mail-address
"")))
926 (setq r1
(concat "\\<" (regexp-quote user-mail-address
) "\\>")))
927 (when (and user-full-name
(not (string= user-full-name
"")))
928 (setq r2
(concat "\\<" (regexp-quote user-full-name
) "\\>")))
929 (if (and r1 r2
) (concat r1
"\\|" r2
) (or r1 r2
)))
930 "Regexp mached against the \"From:\" header of an email or usenet message.
931 It should match if the message is from the user him/herself."
932 :group
'org-link-store
935 (defcustom org-context-in-file-links t
936 "Non-nil means, file links from `org-store-link' contain context.
937 A search string will be added to the file name with :: as separator and
938 used to find the context when the link is activated by the command
940 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
941 negates this setting for the duration of the command."
942 :group
'org-link-store
945 (defcustom org-keep-stored-link-after-insertion nil
946 "Non-nil means, keep link in list for entire session.
948 The command `org-store-link' adds a link pointing to the current
949 location to an internal list. These links accumulate during a session.
950 The command `org-insert-link' can be used to insert links into any
951 Org-mode file (offering completion for all stored links). When this
952 option is nil, every link which has been inserted once using \\[org-insert-link]
953 will be removed from the list, to make completing the unused links
955 :group
'org-link-store
958 (defgroup org-link-follow nil
959 "Options concerning following links in Org-mode."
960 :tag
"Org Follow Link"
963 (defcustom org-follow-link-hook nil
964 "Hook that is run after a link has been followed."
965 :group
'org-link-follow
968 (defcustom org-tab-follows-link nil
969 "Non-nil means, on links TAB will follow the link.
970 Needs to be set before org.el is loaded."
971 :group
'org-link-follow
974 (defcustom org-return-follows-link nil
975 "Non-nil means, on links RET will follow the link.
976 Needs to be set before org.el is loaded."
977 :group
'org-link-follow
980 (defcustom org-mouse-1-follows-link
981 (if (boundp 'mouse-1-click-follows-link
) mouse-1-click-follows-link t
)
982 "Non-nil means, mouse-1 on a link will follow the link.
983 A longer mouse click will still set point. Does not work on XEmacs.
984 Needs to be set before org.el is loaded."
985 :group
'org-link-follow
988 (defcustom org-mark-ring-length
4
989 "Number of different positions to be recorded in the ring
990 Changing this requires a restart of Emacs to work correctly."
991 :group
'org-link-follow
994 (defcustom org-link-frame-setup
995 '((vm . vm-visit-folder-other-frame
)
996 (gnus . gnus-other-frame
)
997 (file . find-file-other-window
))
998 "Setup the frame configuration for following links.
999 When following a link with Emacs, it may often be useful to display
1000 this link in another window or frame. This variable can be used to
1001 set this up for the different types of links.
1004 `vm-visit-folder-other-frame'
1005 For Gnus, use any of
1008 For FILE, use any of
1010 `find-file-other-window'
1011 `find-file-other-frame'
1012 For the calendar, use the variable `calendar-setup'.
1013 For BBDB, it is currently only possible to display the matches in
1015 :group
'org-link-follow
1019 (const vm-visit-folder
)
1020 (const vm-visit-folder-other-window
)
1021 (const vm-visit-folder-other-frame
)))
1025 (const gnus-other-frame
)))
1029 (const find-file-other-window
)
1030 (const find-file-other-frame
)))))
1032 (defcustom org-display-internal-link-with-indirect-buffer nil
1033 "Non-nil means, use indirect buffer to display infile links.
1034 Activating internal links (from one location in a file to another location
1035 in the same file) normally just jumps to the location. When the link is
1036 activated with a C-u prefix (or with mouse-3), the link is displayed in
1037 another window. When this option is set, the other window actually displays
1038 an indirect buffer clone of the current buffer, to avoid any visibility
1039 changes to the current buffer."
1040 :group
'org-link-follow
1043 (defcustom org-open-non-existing-files nil
1044 "Non-nil means, `org-open-file' will open non-existing files.
1045 When nil, an error will be generated."
1046 :group
'org-link-follow
1049 (defcustom org-link-mailto-program
'(browse-url "mailto:%a?subject=%s")
1050 "Function and arguments to call for following mailto links.
1051 This is a list with the first element being a lisp function, and the
1052 remaining elements being arguments to the function. In string arguments,
1053 %a will be replaced by the address, and %s will be replaced by the subject
1054 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1055 :group
'org-link-follow
1057 (const :tag
"browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1058 (const :tag
"compose-mail" (compose-mail "%a" "%s"))
1059 (const :tag
"message-mail" (message-mail "%a" "%s"))
1060 (cons :tag
"other" (function) (repeat :tag
"argument" sexp
))))
1062 (defcustom org-confirm-shell-link-function
'yes-or-no-p
1063 "Non-nil means, ask for confirmation before executing shell links.
1064 Shell links can be dangerous: just think about a link
1066 [[shell:rm -rf ~/*][Google Search]]
1068 This link would show up in your Org-mode document as \"Google Search\",
1069 but really it would remove your entire home directory.
1070 Therefore we advise against setting this variable to nil.
1071 Just change it to `y-or-n-p' of you want to confirm with a
1072 single keystroke rather than having to type \"yes\"."
1073 :group
'org-link-follow
1075 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1076 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1077 (const :tag
"no confirmation (dangerous)" nil
)))
1079 (defcustom org-confirm-elisp-link-function
'yes-or-no-p
1080 "Non-nil means, ask for confirmation before executing Emacs Lisp links.
1081 Elisp links can be dangerous: just think about a link
1083 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1085 This link would show up in your Org-mode document as \"Google Search\",
1086 but really it would remove your entire home directory.
1087 Therefore we advise against setting this variable to nil.
1088 Just change it to `y-or-n-p' of you want to confirm with a
1089 single keystroke rather than having to type \"yes\"."
1090 :group
'org-link-follow
1092 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1093 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1094 (const :tag
"no confirmation (dangerous)" nil
)))
1096 (defconst org-file-apps-defaults-gnu
1099 "Default file applications on a UNIX or GNU/Linux system.
1100 See `org-file-apps'.")
1102 (defconst org-file-apps-defaults-macosx
1108 ("eps.gz" .
"gv %s")
1110 ("fig" .
"xfig %s"))
1111 "Default file applications on a MacOS X system.
1112 The system \"open\" is known as a default, but we use X11 applications
1113 for some files for which the OS does not have a good default.
1114 See `org-file-apps'.")
1116 (defconst org-file-apps-defaults-windowsnt
1120 (list (if (featurep 'xemacs
)
1121 'mswindows-shell-execute
1124 "Default file applications on a Windows NT system.
1125 The system \"open\" is used for most files.
1126 See `org-file-apps'.")
1128 (defcustom org-file-apps
1137 "External applications for opening `file:path' items in a document.
1138 Org-mode uses system defaults for different file types, but
1139 you can use this variable to set the application for a given file
1140 extension. The entries in this list are cons cells where the car identifies
1141 files and the cdr the corresponding command. Possible values for the
1143 \"ext\" A string identifying an extension
1144 `directory' Matches a directory
1145 `remote' Matches a remote file, accessible through tramp or efs.
1146 Remote files most likely should be visited through Emacs
1147 because external applications cannot handle such paths.
1148 t Default for all remaining files
1150 Possible values for the command are:
1151 `emacs' The file will be visited by the current Emacs process.
1152 `default' Use the default application for this file type.
1153 string A command to be executed by a shell; %s will be replaced
1154 by the path to the file.
1155 sexp A Lisp form which will be evaluated. The file path will
1156 be available in the Lisp variable `file'.
1157 For more examples, see the system specific constants
1158 `org-file-apps-defaults-macosx'
1159 `org-file-apps-defaults-windowsnt'
1160 `org-file-apps-defaults-gnu'."
1161 :group
'org-link-follow
1163 (cons (choice :value
""
1164 (string :tag
"Extension")
1165 (const :tag
"Default for unrecognized files" t
)
1166 (const :tag
"Remote file" remote
)
1167 (const :tag
"Links to a directory" directory
))
1169 (const :tag
"Visit with Emacs" emacs
)
1170 (const :tag
"Use system default" default
)
1171 (string :tag
"Command")
1172 (sexp :tag
"Lisp form")))))
1174 (defgroup org-refile nil
1175 "Options concerning refiling entries in Org-mode."
1179 (defcustom org-directory
"~/org"
1180 "Directory with org files.
1181 This directory will be used as default to prompt for org files.
1182 Used by the hooks for remember.el."
1184 :group
'org-remember
1187 (defcustom org-default-notes-file
"~/.notes"
1188 "Default target for storing notes.
1189 Used by the hooks for remember.el. This can be a string, or nil to mean
1190 the value of `remember-data-file'.
1191 You can set this on a per-template basis with the variable
1192 `org-remember-templates'."
1194 :group
'org-remember
1196 (const :tag
"Default from remember-data-file" nil
)
1199 (defcustom org-goto-interface
'outline
1200 "The default interface to be used for `org-goto'.
1202 outline The interface shows an outline of the relevant file
1203 and the correct heading is found by moving through
1204 the outline or by searching with incremental search.
1205 outline-path-completion Headlines in the current buffer are offered via
1209 (const :tag
"Outline" outline
)
1210 (const :tag
"Outline-path-completion" outline-path-completion
)))
1212 (defcustom org-reverse-note-order nil
1213 "Non-nil means, store new notes at the beginning of a file or entry.
1214 When nil, new notes will be filed to the end of a file or entry.
1215 This can also be a list with cons cells of regular expressions that
1216 are matched against file names, and values."
1217 :group
'org-remember
1219 (const :tag
"Reverse always" t
)
1220 (const :tag
"Reverse never" nil
)
1221 (repeat :tag
"By file name regexp"
1222 (cons regexp boolean
))))
1224 (defcustom org-refile-targets nil
1225 "Targets for refiling entries with \\[org-refile].
1226 This is list of cons cells. Each cell contains:
1227 - a specification of the files to be considered, either a list of files,
1228 or a symbol whose function or variable value will be used to retrieve
1229 a file name or a list of file names. Nil means, refile to a different
1230 heading in the current buffer.
1231 - A specification of how to find candidate refile targets. This may be
1233 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
1234 This tag has to be present in all target headlines, inheritance will
1236 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
1238 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
1239 headlines that are refiling targets.
1240 - a cons cell (:level . N). Any headline of level N is considered a target.
1241 - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
1242 :group
'org-remember
1245 (choice :value org-agenda-files
1246 (const :tag
"All agenda files" org-agenda-files
)
1247 (const :tag
"Current buffer" nil
)
1248 (function) (variable) (file))
1249 (choice :tag
"Identify target headline by"
1250 (cons :tag
"Specific tag" (const :tag
) (string))
1251 (cons :tag
"TODO keyword" (const :todo
) (string))
1252 (cons :tag
"Regular expression" (const :regexp
) (regexp))
1253 (cons :tag
"Level number" (const :level
) (integer))
1254 (cons :tag
"Max Level number" (const :maxlevel
) (integer))))))
1256 (defcustom org-refile-use-outline-path nil
1257 "Non-nil means, provide refile targets as paths.
1258 So a level 3 headline will be available as level1/level2/level3.
1259 When the value is `file', also include the file name (without directory)
1260 into the path. When `full-file-path', include the full file path."
1261 :group
'org-remember
1263 (const :tag
"Not" nil
)
1264 (const :tag
"Yes" t
)
1265 (const :tag
"Start with file name" file
)
1266 (const :tag
"Start with full file path" full-file-path
)))
1268 (defgroup org-todo nil
1269 "Options concerning TODO items in Org-mode."
1273 (defgroup org-progress nil
1274 "Options concerning Progress logging in Org-mode."
1278 (defcustom org-todo-keywords
'((sequence "TODO" "DONE"))
1279 "List of TODO entry keyword sequences and their interpretation.
1280 \\<org-mode-map>This is a list of sequences.
1282 Each sequence starts with a symbol, either `sequence' or `type',
1283 indicating if the keywords should be interpreted as a sequence of
1284 action steps, or as different types of TODO items. The first
1285 keywords are states requiring action - these states will select a headline
1286 for inclusion into the global TODO list Org-mode produces. If one of
1287 the \"keywords\" is the vertical bat \"|\" the remaining keywords
1288 signify that no further action is necessary. If \"|\" is not found,
1289 the last keyword is treated as the only DONE state of the sequence.
1291 The command \\[org-todo] cycles an entry through these states, and one
1292 additional state where no keyword is present. For details about this
1293 cycling, see the manual.
1295 TODO keywords and interpretation can also be set on a per-file basis with
1296 the special #+SEQ_TODO and #+TYP_TODO lines.
1298 Each keyword can optionally specify a character for fast state selection
1299 \(in combination with the variable `org-use-fast-todo-selection')
1300 and specifiers for state change logging, using the same syntax
1301 that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
1302 that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
1303 indicates to record a time stamp each time this state is selected.
1305 Each keyword may also specify if a timestamp or a note should be
1306 recorded when entering or leaving the state, by adding additional
1307 characters in the parenthesis after the keyword. This looks like this:
1308 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
1309 record only the time of the state change. With X and Y being either
1310 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
1311 Y when leaving the state if and only if the *target* state does not
1312 define X. You may omit any of the fast-selection key or X or /Y,
1313 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
1315 For backward compatibility, this variable may also be just a list
1316 of keywords - in this case the interptetation (sequence or type) will be
1317 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
1319 :group
'org-keywords
1321 (repeat :tag
"Old syntax, just keywords"
1322 (string :tag
"Keyword"))
1323 (repeat :tag
"New syntax"
1326 :tag
"Interpretation"
1327 (const :tag
"Sequence (cycling hits every state)" sequence
)
1328 (const :tag
"Type (cycling directly to DONE)" type
))
1330 (string :tag
"Keyword"))))))
1332 (defvar org-todo-keywords-1 nil
1333 "All TODO and DONE keywords active in a buffer.")
1334 (make-variable-buffer-local 'org-todo-keywords-1
)
1335 (defvar org-todo-keywords-for-agenda nil
)
1336 (defvar org-done-keywords-for-agenda nil
)
1337 (defvar org-agenda-contributing-files nil
)
1338 (defvar org-not-done-keywords nil
)
1339 (make-variable-buffer-local 'org-not-done-keywords
)
1340 (defvar org-done-keywords nil
)
1341 (make-variable-buffer-local 'org-done-keywords
)
1342 (defvar org-todo-heads nil
)
1343 (make-variable-buffer-local 'org-todo-heads
)
1344 (defvar org-todo-sets nil
)
1345 (make-variable-buffer-local 'org-todo-sets
)
1346 (defvar org-todo-log-states nil
)
1347 (make-variable-buffer-local 'org-todo-log-states
)
1348 (defvar org-todo-kwd-alist nil
)
1349 (make-variable-buffer-local 'org-todo-kwd-alist
)
1350 (defvar org-todo-key-alist nil
)
1351 (make-variable-buffer-local 'org-todo-key-alist
)
1352 (defvar org-todo-key-trigger nil
)
1353 (make-variable-buffer-local 'org-todo-key-trigger
)
1355 (defcustom org-todo-interpretation
'sequence
1356 "Controls how TODO keywords are interpreted.
1357 This variable is in principle obsolete and is only used for
1358 backward compatibility, if the interpretation of todo keywords is
1359 not given already in `org-todo-keywords'. See that variable for
1362 :group
'org-keywords
1363 :type
'(choice (const sequence
)
1366 (defcustom org-use-fast-todo-selection
'prefix
1367 "Non-nil means, use the fast todo selection scheme with C-c C-t.
1368 This variable describes if and under what circumstances the cycling
1369 mechanism for TODO keywords will be replaced by a single-key, direct
1372 When nil, fast selection is never used.
1374 When the symbol `prefix', it will be used when `org-todo' is called with
1375 a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
1376 in an agenda buffer.
1378 When t, fast selection is used by default. In this case, the prefix
1379 argument forces cycling instead.
1381 In all cases, the special interface is only used if access keys have actually
1382 been assigned by the user, i.e. if keywords in the configuration are followed
1383 by a letter in parenthesis, like TODO(t)."
1386 (const :tag
"Never" nil
)
1387 (const :tag
"By default" t
)
1388 (const :tag
"Only with C-u C-c C-t" prefix
)))
1390 (defcustom org-after-todo-state-change-hook nil
1391 "Hook which is run after the state of a TODO item was changed.
1392 The new state (a string with a TODO keyword, or nil) is available in the
1393 Lisp variable `state'."
1397 (defcustom org-log-done nil
1398 "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
1399 When equal to the list (done), also prompt for a closing note.
1400 This can also be configured on a per-file basis by adding one of
1401 the following lines anywhere in the buffer:
1404 #+STARTUP: lognotedone
1405 #+STARTUP: nologdone"
1407 :group
'org-progress
1409 (const :tag
"No logging" nil
)
1410 (const :tag
"Record CLOSED timestamp" time
)
1411 (const :tag
"Record CLOSED timestamp with closing note." note
)))
1413 ;; Normalize old uses of org-log-done.
1415 ((eq org-log-done t
) (setq org-log-done
'time
))
1416 ((and (listp org-log-done
) (memq 'done org-log-done
))
1417 (setq org-log-done
'note
)))
1419 (defcustom org-log-note-clock-out nil
1420 "Non-nil means, recored a note when clocking out of an item.
1421 This can also be configured on a per-file basis by adding one of
1422 the following lines anywhere in the buffer:
1424 #+STARTUP: lognoteclock-out
1425 #+STARTUP: nolognoteclock-out"
1427 :group
'org-progress
1430 (defcustom org-log-done-with-time t
1431 "Non-nil means, the CLOSED time stamp will contain date and time.
1432 When nil, only the date will be recorded."
1433 :group
'org-progress
1436 (defcustom org-log-note-headings
1437 '((done .
"CLOSING NOTE %t")
1438 (state .
"State %-12s %t")
1439 (note .
"Note taken on %t")
1441 "Headings for notes added to entries.
1442 The value is an alist, with the car being a symbol indicating the note
1443 context, and the cdr is the heading to be used. The heading may also be the
1445 %t in the heading will be replaced by a time stamp.
1446 %s will be replaced by the new TODO state, in double quotes.
1447 %u will be replaced by the user name.
1448 %U will be replaced by the full user name."
1450 :group
'org-progress
1451 :type
'(list :greedy t
1452 (cons (const :tag
"Heading when closing an item" done
) string
)
1454 "Heading when changing todo state (todo sequence only)"
1456 (cons (const :tag
"Heading when just taking a note" note
) string
)
1457 (cons (const :tag
"Heading when clocking out" clock-out
) string
)))
1459 (unless (assq 'note org-log-note-headings
)
1460 (push '(note .
"%t") org-log-note-headings
))
1462 (defcustom org-log-states-order-reversed t
1463 "Non-nil means, the latest state change note will be directly after heading.
1464 When nil, the notes will be orderer according to time."
1466 :group
'org-progress
1469 (defcustom org-log-repeat
'time
1470 "Non-nil means, record moving through the DONE state when triggering repeat.
1471 An auto-repeating tasks is immediately switched back to TODO when marked
1472 done. If you are not logging state changes (by adding \"@\" or \"!\" to
1473 the TODO keyword definition, or recording a closing note by setting
1474 `org-log-done', there will be no record of the task moving through DONE.
1475 This variable forces taking a note anyway. Possible values are:
1477 nil Don't force a record
1478 time Record a time stamp
1481 This option can also be set with on a per-file-basis with
1483 #+STARTUP: logrepeat
1484 #+STARTUP: lognoterepeat
1485 #+STARTUP: nologrepeat
1487 You can have local logging settings for a subtree by setting the LOGGING
1488 property to one or more of these keywords."
1490 :group
'org-progress
1492 (const :tag
"Don't force a record" nil
)
1493 (const :tag
"Force recording the DONE state" time
)
1494 (const :tag
"Force recording a note with the DONE state" note
)))
1497 (defgroup org-priorities nil
1498 "Priorities in Org-mode."
1499 :tag
"Org Priorities"
1502 (defcustom org-highest-priority ?A
1503 "The highest priority of TODO items. A character like ?A, ?B etc.
1504 Must have a smaller ASCII number than `org-lowest-priority'."
1505 :group
'org-priorities
1508 (defcustom org-lowest-priority ?C
1509 "The lowest priority of TODO items. A character like ?A, ?B etc.
1510 Must have a larger ASCII number than `org-highest-priority'."
1511 :group
'org-priorities
1514 (defcustom org-default-priority ?B
1515 "The default priority of TODO items.
1516 This is the priority an item get if no explicit priority is given."
1517 :group
'org-priorities
1520 (defcustom org-priority-start-cycle-with-default t
1521 "Non-nil means, start with default priority when starting to cycle.
1522 When this is nil, the first step in the cycle will be (depending on the
1523 command used) one higher or lower that the default priority."
1524 :group
'org-priorities
1527 (defgroup org-time nil
1528 "Options concerning time stamps and deadlines in Org-mode."
1532 (defcustom org-insert-labeled-timestamps-at-point nil
1533 "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
1534 When nil, these labeled time stamps are forces into the second line of an
1535 entry, just after the headline. When scheduling from the global TODO list,
1536 the time stamp will always be forced into the second line."
1540 (defconst org-time-stamp-formats
'("<%Y-%m-%d %a>" .
"<%Y-%m-%d %a %H:%M>")
1541 "Formats for `format-time-string' which are used for time stamps.
1542 It is not recommended to change this constant.")
1544 (defcustom org-time-stamp-rounding-minutes
'(0 5)
1545 "Number of minutes to round time stamps to.
1546 These are two values, the first applies when first creating a time stamp.
1547 The second applies when changing it with the commands `S-up' and `S-down'.
1548 When changing the time stamp, this means that it will change in steps
1549 of N minutes, as given by the second value.
1551 When a setting is 0 or 1, insert the time unmodified. Useful rounding
1552 numbers should be factors of 60, so for example 5, 10, 15.
1554 When this is larger than 1, you can still force an exact time-stamp by using
1555 a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
1556 and by using a prefix arg to `S-up/down' to specify the exact number
1557 of minutes to shift."
1559 :get
'(lambda (var) ; Make sure all entries have 5 elements
1560 (if (integerp (default-value var
))
1561 (list (default-value var
) 5)
1562 (default-value var
)))
1564 (integer :tag
"when inserting times")
1565 (integer :tag
"when modifying times")))
1567 ;; Normalize old customizations of this variable.
1568 (when (integerp org-time-stamp-rounding-minutes
)
1569 (setq org-time-stamp-rounding-minutes
1570 (list org-time-stamp-rounding-minutes
1571 org-time-stamp-rounding-minutes
)))
1573 (defcustom org-display-custom-times nil
1574 "Non-nil means, overlay custom formats over all time stamps.
1575 The formats are defined through the variable `org-time-stamp-custom-formats'.
1576 To turn this on on a per-file basis, insert anywhere in the file:
1577 #+STARTUP: customtime"
1581 (make-variable-buffer-local 'org-display-custom-times
)
1583 (defcustom org-time-stamp-custom-formats
1584 '("<%m/%d/%y %a>" .
"<%m/%d/%y %a %H:%M>") ; american
1585 "Custom formats for time stamps. See `format-time-string' for the syntax.
1586 These are overlayed over the default ISO format if the variable
1587 `org-display-custom-times' is set. Time like %H:%M should be at the
1588 end of the second format."
1592 (defun org-time-stamp-format (&optional long inactive
)
1593 "Get the right format for a time string."
1594 (let ((f (if long
(cdr org-time-stamp-formats
)
1595 (car org-time-stamp-formats
))))
1597 (concat "[" (substring f
1 -
1) "]")
1600 (defcustom org-deadline-warning-days
14
1601 "No. of days before expiration during which a deadline becomes active.
1602 This variable governs the display in sparse trees and in the agenda.
1603 When 0 or negative, it means use this number (the absolute value of it)
1604 even if a deadline has a different individual lead time specified."
1606 :group
'org-agenda-daily
/weekly
1609 (defcustom org-read-date-prefer-future t
1610 "Non-nil means, assume future for incomplete date input from user.
1611 This affects the following situations:
1612 1. The user gives a day, but no month.
1613 For example, if today is the 15th, and you enter \"3\", Org-mode will
1614 read this as the third of *next* month. However, if you enter \"17\",
1615 it will be considered as *this* month.
1616 2. The user gives a month but not a year.
1617 For example, if it is april and you enter \"feb 2\", this will be read
1618 as feb 2, *next* year. \"May 5\", however, will be this year.
1620 Currently this does not work for ISO week specifications.
1622 When this option is nil, the current month and year will always be used
1627 (defcustom org-read-date-display-live t
1628 "Non-nil means, display current interpretation of date prompt live.
1629 This display will be in an overlay, in the minibuffer."
1633 (defcustom org-read-date-popup-calendar t
1634 "Non-nil means, pop up a calendar when prompting for a date.
1635 In the calendar, the date can be selected with mouse-1. However, the
1636 minibuffer will also be active, and you can simply enter the date as well.
1637 When nil, only the minibuffer will be available."
1640 (if (fboundp 'defvaralias
)
1641 (defvaralias 'org-popup-calendar-for-date-prompt
1642 'org-read-date-popup-calendar
))
1644 (defcustom org-extend-today-until
0
1645 "The hour when your day really ends.
1646 This has influence for the following applications:
1647 - When switching the agenda to \"today\". It it is still earlier than
1648 the time given here, the day recognized as TODAY is actually yesterday.
1649 - When a date is read from the user and it is still before the time given
1650 here, the current date and time will be assumed to be yesterday, 23:59.
1653 IMPORTANT: This is still a very experimental feature, it may disappear
1654 again or it may be extended to mean more things."
1658 (defcustom org-edit-timestamp-down-means-later nil
1659 "Non-nil means, S-down will increase the time in a time stamp.
1660 When nil, S-up will increase."
1664 (defcustom org-calendar-follow-timestamp-change t
1665 "Non-nil means, make the calendar window follow timestamp changes.
1666 When a timestamp is modified and the calendar window is visible, it will be
1667 moved to the new date."
1671 (defgroup org-tags nil
1672 "Options concerning tags in Org-mode."
1676 (defcustom org-tag-alist nil
1677 "List of tags allowed in Org-mode files.
1678 When this list is nil, Org-mode will base TAG input on what is already in the
1680 The value of this variable is an alist, the car of each entry must be a
1681 keyword as a string, the cdr may be a character that is used to select
1682 that tag through the fast-tag-selection interface.
1683 See the manual for details."
1687 (cons (string :tag
"Tag name")
1688 (character :tag
"Access char"))
1689 (const :tag
"Start radio group" (:startgroup
))
1690 (const :tag
"End radio group" (:endgroup
)))))
1692 (defcustom org-use-fast-tag-selection
'auto
1693 "Non-nil means, use fast tag selection scheme.
1694 This is a special interface to select and deselect tags with single keys.
1695 When nil, fast selection is never used.
1696 When the symbol `auto', fast selection is used if and only if selection
1697 characters for tags have been configured, either through the variable
1698 `org-tag-alist' or through a #+TAGS line in the buffer.
1699 When t, fast selection is always used and selection keys are assigned
1700 automatically if necessary."
1703 (const :tag
"Always" t
)
1704 (const :tag
"Never" nil
)
1705 (const :tag
"When selection characters are configured" 'auto
)))
1707 (defcustom org-fast-tag-selection-single-key nil
1708 "Non-nil means, fast tag selection exits after first change.
1709 When nil, you have to press RET to exit it.
1710 During fast tag selection, you can toggle this flag with `C-c'.
1711 This variable can also have the value `expert'. In this case, the window
1712 displaying the tags menu is not even shown, until you press C-c again."
1715 (const :tag
"No" nil
)
1716 (const :tag
"Yes" t
)
1717 (const :tag
"Expert" expert
)))
1719 (defvar org-fast-tag-selection-include-todo nil
1720 "Non-nil means, fast tags selection interface will also offer TODO states.
1721 This is an undocumented feature, you should not rely on it.")
1723 (defcustom org-tags-column
(if (featurep 'xemacs
) -
79 -
80)
1724 "The column to which tags should be indented in a headline.
1725 If this number is positive, it specifies the column. If it is negative,
1726 it means that the tags should be flushright to that column. For example,
1727 -80 works well for a normal 80 character screen."
1731 (defcustom org-auto-align-tags t
1732 "Non-nil means, realign tags after pro/demotion of TODO state change.
1733 These operations change the length of a headline and therefore shift
1734 the tags around. With this options turned on, after each such operation
1735 the tags are again aligned to `org-tags-column'."
1739 (defcustom org-use-tag-inheritance t
1740 "Non-nil means, tags in levels apply also for sublevels.
1741 When nil, only the tags directly given in a specific line apply there.
1742 If you turn off this option, you very likely want to turn on the
1743 companion option `org-tags-match-list-sublevels'.
1745 This may also be a list of tags that should be inherited, or a regexp that
1746 matches tags that should be inherited."
1749 (const :tag
"Not" nil
)
1750 (const :tag
"Always" t
)
1751 (repeat :tag
"Specific tags" (string :tag
"Tag"))
1752 (regexp :tag
"Tags matched by regexp")))
1754 (defun org-tag-inherit-p (tag)
1755 "Check if TAG is one that should be inherited."
1757 ((eq org-use-tag-inheritance t
) t
)
1758 ((not org-use-tag-inheritance
) nil
)
1759 ((stringp org-use-tag-inheritance
)
1760 (string-match org-use-tag-inheritance tag
))
1761 ((listp org-use-tag-inheritance
)
1762 (member tag org-use-tag-inheritance
))
1763 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
1765 (defcustom org-tags-match-list-sublevels nil
1766 "Non-nil means list also sublevels of headlines matching tag search.
1767 Because of tag inheritance (see variable `org-use-tag-inheritance'),
1768 the sublevels of a headline matching a tag search often also match
1769 the same search. Listing all of them can create very long lists.
1770 Setting this variable to nil causes subtrees of a match to be skipped.
1771 This option is off by default, because inheritance in on. If you turn
1772 inheritance off, you very likely want to turn this option on.
1774 As a special case, if the tag search is restricted to TODO items, the
1775 value of this variable is ignored and sublevels are always checked, to
1776 make sure all corresponding TODO items find their way into the list."
1780 (defvar org-tags-history nil
1781 "History of minibuffer reads for tags.")
1782 (defvar org-last-tags-completion-table nil
1783 "The last used completion table for tags.")
1784 (defvar org-after-tags-change-hook nil
1785 "Hook that is run after the tags in a line have changed.")
1787 (defgroup org-properties nil
1788 "Options concerning properties in Org-mode."
1789 :tag
"Org Properties"
1792 (defcustom org-property-format
"%-10s %s"
1793 "How property key/value pairs should be formatted by `indent-line'.
1794 When `indent-line' hits a property definition, it will format the line
1795 according to this format, mainly to make sure that the values are
1796 lined-up with respect to each other."
1797 :group
'org-properties
1800 (defcustom org-use-property-inheritance nil
1801 "Non-nil means, properties apply also for sublevels.
1803 This setting is chiefly used during property searches. Turning it on can
1804 cause significant overhead when doing a search, which is why it is not
1807 When nil, only the properties directly given in the current entry count.
1808 When t, every property is inherited. The value may also be a list of
1809 properties that should have inheritance, or a regular expression matching
1810 properties that should be inherited.
1812 However, note that some special properties use inheritance under special
1813 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
1814 and the properties ending in \"_ALL\" when they are used as descriptor
1815 for valid values of a property.
1817 Note for programmers:
1818 When querying an entry with `org-entry-get', you can control if inheritance
1819 should be used. By default, `org-entry-get' looks only at the local
1820 properties. You can request inheritance by setting the inherit argument
1821 to t (to force inheritance) or to `selective' (to respect the setting
1823 :group
'org-properties
1825 (const :tag
"Not" nil
)
1826 (const :tag
"Always" t
)
1827 (repeat :tag
"Specific properties" (string :tag
"Property"))
1828 (regexp :tag
"Properties matched by regexp")))
1830 (defun org-property-inherit-p (property)
1831 "Check if PROPERTY is one that should be inherited."
1833 ((eq org-use-property-inheritance t
) t
)
1834 ((not org-use-property-inheritance
) nil
)
1835 ((stringp org-use-property-inheritance
)
1836 (string-match org-use-property-inheritance property
))
1837 ((listp org-use-property-inheritance
)
1838 (member property org-use-property-inheritance
))
1839 (t (error "Invalid setting of `org-use-property-inheritance'"))))
1841 (defcustom org-columns-default-format
"%25ITEM %TODO %3PRIORITY %TAGS"
1842 "The default column format, if no other format has been defined.
1843 This variable can be set on the per-file basis by inserting a line
1845 #+COLUMNS: %25ITEM ....."
1846 :group
'org-properties
1849 (defcustom org-effort-property
"Effort"
1850 "The property that is being used to keep track of effort estimates.
1851 Effort estimates given in this property need to have the format H:MM."
1852 :group
'org-properties
1853 :group
'org-progress
1854 :type
'(string :tag
"Property"))
1856 (defconst org-global-properties-fixed
1857 '(("VISIBILITY_ALL" .
"folded children content all"))
1858 "List of property/value pairs that can be inherited by any entry.
1859 These are fixed values, for the preset properties.")
1862 (defcustom org-global-properties nil
1863 "List of property/value pairs that can be inherited by any entry.
1864 You can set buffer-local values for this by adding lines like
1866 #+PROPERTY: NAME VALUE"
1867 :group
'org-properties
1869 (cons (string :tag
"Property")
1870 (string :tag
"Value"))))
1872 (defvar org-local-properties nil
1873 "List of property/value pairs that can be inherited by any entry.
1874 Valid for the current buffer.
1875 This variable is populated from #+PROPERTY lines.")
1877 (defgroup org-agenda nil
1878 "Options concerning agenda views in Org-mode."
1882 (defvar org-category nil
1883 "Variable used by org files to set a category for agenda display.
1884 Such files should use a file variable to set it, for example
1886 # -*- mode: org; org-category: \"ELisp\"
1888 or contain a special line
1892 If the file does not specify a category, then file's base name
1894 (make-variable-buffer-local 'org-category
)
1896 (defcustom org-agenda-files nil
1897 "The files to be used for agenda display.
1898 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
1899 \\[org-remove-file]. You can also use customize to edit the list.
1901 If an entry is a directory, all files in that directory that are matched by
1902 `org-agenda-file-regexp' will be part of the file list.
1904 If the value of the variable is not a list but a single file name, then
1905 the list of agenda files is actually stored and maintained in that file, one
1906 agenda file per line."
1909 (repeat :tag
"List of files and directories" file
)
1910 (file :tag
"Store list in a file\n" :value
"~/.agenda_files")))
1912 (defcustom org-agenda-file-regexp
"\\`[^.].*\\.org\\'"
1913 "Regular expression to match files for `org-agenda-files'.
1914 If any element in the list in that variable contains a directory instead
1915 of a normal file, all files in that directory that are matched by this
1916 regular expression will be included."
1920 (defcustom org-agenda-text-search-extra-files nil
1921 "List of extra files to be searched by text search commands.
1922 These files will be search in addition to the agenda files by the
1923 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
1924 Note that these files will only be searched for text search commands,
1925 not for the other agenda views like todo lists, tag searches or the weekly
1926 agenda. This variable is intended to list notes and possibly archive files
1927 that should also be searched by these two commands.
1928 In fact, if the first element in the list is the symbol `agenda-archives',
1929 than all archive files of all agenda files will be added to the search
1932 :type
'(set :greedy t
1933 (const :tag
"Agenda Archives" agenda-archives
)
1934 (repeat :inline t
(file))))
1936 (if (fboundp 'defvaralias
)
1937 (defvaralias 'org-agenda-multi-occur-extra-files
1938 'org-agenda-text-search-extra-files
))
1940 (defcustom org-agenda-skip-unavailable-files nil
1941 "t means to just skip non-reachable files in `org-agenda-files'.
1942 Nil means to remove them, after a query, from the list."
1946 (defcustom org-calendar-to-agenda-key
[?c
]
1947 "The key to be installed in `calendar-mode-map' for switching to the agenda.
1948 The command `org-calendar-goto-agenda' will be bound to this key. The
1949 default is the character `c' because then `c' can be used to switch back and
1950 forth between agenda and calendar."
1954 (eval-after-load "calendar"
1955 '(org-defkey calendar-mode-map org-calendar-to-agenda-key
1956 'org-calendar-goto-agenda
))
1958 (defgroup org-latex nil
1959 "Options for embedding LaTeX code into Org-mode."
1963 (defcustom org-format-latex-options
1964 '(:foreground default
:background default
:scale
1.0
1965 :html-foreground
"Black" :html-background
"Transparent" :html-scale
1.0
1966 :matchers
("begin" "$" "$$" "\\(" "\\["))
1967 "Options for creating images from LaTeX fragments.
1968 This is a property list with the following properties:
1969 :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
1970 `default' means use the foreground of the default face.
1971 :background the background color, or \"Transparent\".
1972 `default' means use the background of the default face.
1973 :scale a scaling factor for the size of the images.
1974 :html-foreground, :html-background, :html-scale
1975 the same numbers for HTML export.
1976 :matchers a list indicating which matchers should be used to
1977 find LaTeX fragments. Valid members of this list are:
1978 \"begin\" find environments
1979 \"$\" find math expressions surrounded by $...$
1980 \"$$\" find math expressions surrounded by $$....$$
1981 \"\\(\" find math expressions surrounded by \\(...\\)
1982 \"\\ [\" find math expressions surrounded by \\ [...\\]"
1986 (defcustom org-format-latex-header
"\\documentclass{article}
1987 \\usepackage{fullpage} % do not remove
1988 \\usepackage{amssymb}
1989 \\usepackage[usenames]{color}
1990 \\usepackage{amsmath}
1991 \\usepackage{latexsym}
1992 \\usepackage[mathscr]{eucal}
1993 \\pagestyle{empty} % do not remove"
1994 "The document header used for processing LaTeX fragments."
1999 (defgroup org-font-lock nil
2000 "Font-lock settings for highlighting in Org-mode."
2001 :tag
"Org Font Lock"
2004 (defcustom org-level-color-stars-only nil
2005 "Non-nil means fontify only the stars in each headline.
2006 When nil, the entire headline is fontified.
2007 Changing it requires restart of `font-lock-mode' to become effective
2008 also in regions already fontified."
2009 :group
'org-font-lock
2012 (defcustom org-hide-leading-stars nil
2013 "Non-nil means, hide the first N-1 stars in a headline.
2014 This works by using the face `org-hide' for these stars. This
2015 face is white for a light background, and black for a dark
2016 background. You may have to customize the face `org-hide' to
2018 Changing it requires restart of `font-lock-mode' to become effective
2019 also in regions already fontified.
2020 You may also set this on a per-file basis by adding one of the following
2021 lines to the buffer:
2023 #+STARTUP: hidestars
2024 #+STARTUP: showstars"
2025 :group
'org-font-lock
2028 (defcustom org-fontify-done-headline nil
2029 "Non-nil means, change the face of a headline if it is marked DONE.
2030 Normally, only the TODO/DONE keyword indicates the state of a headline.
2031 When this is non-nil, the headline after the keyword is set to the
2032 `org-headline-done' as an additional indication."
2033 :group
'org-font-lock
2036 (defcustom org-fontify-emphasized-text t
2037 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
2038 Changing this variable requires a restart of Emacs to take effect."
2039 :group
'org-font-lock
2042 (defcustom org-highlight-latex-fragments-and-specials nil
2043 "Non-nil means, fontify what is treated specially by the exporters."
2044 :group
'org-font-lock
2047 (defcustom org-hide-emphasis-markers nil
2048 "Non-nil mean font-lock should hide the emphasis marker characters."
2049 :group
'org-font-lock
2052 (defvar org-emph-re nil
2053 "Regular expression for matching emphasis.")
2054 (defvar org-verbatim-re nil
2055 "Regular expression for matching verbatim text.")
2056 (defvar org-emphasis-regexp-components
) ; defined just below
2057 (defvar org-emphasis-alist
) ; defined just below
2058 (defun org-set-emph-re (var val
)
2059 "Set variable and compute the emphasis regular expression."
2061 (when (and (boundp 'org-emphasis-alist
)
2062 (boundp 'org-emphasis-regexp-components
)
2063 org-emphasis-alist org-emphasis-regexp-components
)
2064 (let* ((e org-emphasis-regexp-components
)
2070 (stacked (and nil
(nth 5 e
))) ; stacked is no longer allowed, forced to nil
2071 (body1 (concat body
"*?"))
2072 (markers (mapconcat 'car org-emphasis-alist
""))
2073 (vmarkers (mapconcat
2074 (lambda (x) (if (eq (nth 4 x
) 'verbatim
) (car x
) ""))
2075 org-emphasis-alist
"")))
2076 ;; make sure special characters appear at the right position in the class
2077 (if (string-match "\\^" markers
)
2078 (setq markers
(concat (replace-match "" t t markers
) "^")))
2079 (if (string-match "-" markers
)
2080 (setq markers
(concat (replace-match "" t t markers
) "-")))
2081 (if (string-match "\\^" vmarkers
)
2082 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "^")))
2083 (if (string-match "-" vmarkers
)
2084 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "-")))
2086 (setq body1
(concat body1
"\\(?:\n" body
"*?\\)\\{0,"
2087 (int-to-string nl
) "\\}")))
2090 (concat "\\([" pre
(if (and nil stacked
) markers
) "]\\|^\\)"
2092 "\\([" markers
"]\\)"
2095 "[^" border
(if (and nil stacked
) markers
) "]"
2097 "[^" border
(if (and nil stacked
) markers
) "]"
2100 "\\([" post
(if (and nil stacked
) markers
) "]\\|$\\)"))
2101 (setq org-verbatim-re
2102 (concat "\\([" pre
"]\\|^\\)"
2104 "\\([" vmarkers
"]\\)"
2112 "\\([" post
"]\\|$\\)")))))
2114 (defcustom org-emphasis-regexp-components
2115 '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
2116 "Components used to build the regular expression for emphasis.
2117 This is a list with 6 entries. Terminology: In an emphasis string
2118 like \" *strong word* \", we call the initial space PREMATCH, the final
2119 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
2120 and \"trong wor\" is the body. The different components in this variable
2121 specify what is allowed/forbidden in each part:
2123 pre Chars allowed as prematch. Beginning of line will be allowed too.
2124 post Chars allowed as postmatch. End of line will be allowed too.
2125 border The chars *forbidden* as border characters.
2126 body-regexp A regexp like \".\" to match a body character. Don't use
2127 non-shy groups here, and don't allow newline here.
2128 newline The maximum number of newlines allowed in an emphasis exp.
2130 Use customize to modify this, or restart Emacs after changing it."
2131 :group
'org-font-lock
2132 :set
'org-set-emph-re
2134 (sexp :tag
"Allowed chars in pre ")
2135 (sexp :tag
"Allowed chars in post ")
2136 (sexp :tag
"Forbidden chars in border ")
2137 (sexp :tag
"Regexp for body ")
2138 (integer :tag
"number of newlines allowed")
2139 (option (boolean :tag
"Stacking (DISABLED) "))))
2141 (defcustom org-emphasis-alist
2142 `(("*" bold
"<b>" "</b>")
2143 ("/" italic
"<i>" "</i>")
2144 ("_" underline
"<u>" "</u>")
2145 ("=" org-code
"<code>" "</code>" verbatim
)
2146 ("~" org-verbatim
"" "" verbatim
)
2147 ("+" ,(if (featurep 'xemacs
) 'org-table
'(:strike-through t
))
2150 "Special syntax for emphasized text.
2151 Text starting and ending with a special character will be emphasized, for
2152 example *bold*, _underlined_ and /italic/. This variable sets the marker
2153 characters, the face to be used by font-lock for highlighting in Org-mode
2154 Emacs buffers, and the HTML tags to be used for this.
2155 Use customize to modify this, or restart Emacs after changing it."
2156 :group
'org-font-lock
2157 :set
'org-set-emph-re
2160 (string :tag
"Marker character")
2162 (face :tag
"Font-lock-face")
2163 (plist :tag
"Face property list"))
2164 (string :tag
"HTML start tag")
2165 (string :tag
"HTML end tag")
2166 (option (const verbatim
)))))
2168 ;;; Miscellaneous options
2170 (defgroup org-completion nil
2171 "Completion in Org-mode."
2172 :tag
"Org Completion"
2175 (defcustom org-completion-fallback-command
'hippie-expand
2176 "The expansion command called by \\[org-complete] in normal context.
2177 Normal means, no org-mode-specific context."
2178 :group
'org-completion
2181 ;;; Functions and variables from ther packages
2182 ;; Declared here to avoid compiler warnings
2185 (defvar outline-mode-menu-heading
)
2186 (defvar outline-mode-menu-show
)
2187 (defvar outline-mode-menu-hide
)
2188 (defvar zmacs-regions
) ; XEmacs regions
2191 (defvar mark-active
)
2194 (declare-function calendar-absolute-from-iso
"cal-iso" (date))
2195 (declare-function calendar-forward-day
"cal-move" (arg))
2196 (declare-function calendar-goto-date
"cal-move" (date))
2197 (declare-function calendar-goto-today
"cal-move" ())
2198 (declare-function calendar-iso-from-absolute
"cal-iso" (date))
2199 (defvar calc-embedded-close-formula
)
2200 (defvar calc-embedded-open-formula
)
2201 (declare-function cdlatex-tab
"ext:cdlatex" ())
2202 (declare-function dired-get-filename
"dired" (&optional localp no-error-if-not-filep
))
2203 (defvar font-lock-unfontify-region-function
)
2204 (declare-function iswitchb-mode
"iswitchb" (&optional arg
))
2205 (declare-function iswitchb-read-buffer
(prompt &optional default require-match start matches-set
))
2206 (defvar iswitchb-temp-buflist
)
2207 (declare-function org-gnus-follow-link
"org-gnus" (&optional group article
))
2208 (declare-function org-agenda-skip
"org-agenda" ())
2209 (declare-function org-format-agenda-item
"org-agenda"
2210 (extra txt
&optional category tags dotime noprefix remove-re
))
2211 (declare-function org-agenda-new-marker
"org-agenda" (&optional pos
))
2212 (declare-function org-agenda-change-all-lines
"org-agenda"
2213 (newhead hdmarker
&optional fixface
))
2214 (declare-function org-agenda-set-restriction-lock
"org-agenda" (&optional type
))
2215 (declare-function org-agenda-maybe-redo
"org-agenda" ())
2216 (declare-function org-agenda-save-markers-for-cut-and-paste
"org-agenda"
2218 (declare-function parse-time-string
"parse-time" (string))
2219 (declare-function remember
"remember" (&optional initial
))
2220 (declare-function remember-buffer-desc
"remember" ())
2221 (declare-function remember-finalize
"remember" ())
2222 (defvar remember-save-after-remembering
)
2223 (defvar remember-data-file
)
2224 (defvar remember-register
)
2225 (defvar remember-buffer
)
2226 (defvar remember-handler-functions
)
2227 (defvar remember-annotation-functions
)
2228 (defvar texmathp-why
)
2229 (declare-function speedbar-line-directory
"speedbar" (&optional depth
))
2230 (declare-function table--at-cell-p
"table" (position &optional object at-column
))
2232 (defvar w3m-current-url
)
2233 (defvar w3m-current-title
)
2235 (defvar org-latex-regexps
)
2237 ;;; Autoload and prepare some org modules
2239 ;; Some table stuff that needs to be defined here, because it is used
2240 ;; by the functions setting up org-mode or checking for table context.
2242 (defconst org-table-any-line-regexp
"^[ \t]*\\(|\\|\\+-[-+]\\)"
2243 "Detects an org-type or table-type table.")
2244 (defconst org-table-line-regexp
"^[ \t]*|"
2245 "Detects an org-type table line.")
2246 (defconst org-table-dataline-regexp
"^[ \t]*|[^-]"
2247 "Detects an org-type table line.")
2248 (defconst org-table-hline-regexp
"^[ \t]*|-"
2249 "Detects an org-type table hline.")
2250 (defconst org-table1-hline-regexp
"^[ \t]*\\+-[-+]"
2251 "Detects a table-type table hline.")
2252 (defconst org-table-any-border-regexp
"^[ \t]*[^|+ \t]"
2253 "Searching from within a table (any type) this finds the first line
2254 outside the table.")
2256 ;; Autoload the functions in org-table.el that are needed by functions here.
2259 (org-autoload "org-table"
2260 '(org-table-align org-table-begin org-table-blank-field
2261 org-table-convert org-table-convert-region org-table-copy-down
2262 org-table-copy-region org-table-create
2263 org-table-create-or-convert-from-region
2264 org-table-create-with-table.el org-table-current-dline
2265 org-table-cut-region org-table-delete-column org-table-edit-field
2266 org-table-edit-formulas org-table-end org-table-eval-formula
2267 org-table-export org-table-field-info
2268 org-table-get-stored-formulas org-table-goto-column
2269 org-table-hline-and-move org-table-import org-table-insert-column
2270 org-table-insert-hline org-table-insert-row org-table-iterate
2271 org-table-justify-field-maybe org-table-kill-row
2272 org-table-maybe-eval-formula org-table-maybe-recalculate-line
2273 org-table-move-column org-table-move-column-left
2274 org-table-move-column-right org-table-move-row
2275 org-table-move-row-down org-table-move-row-up
2276 org-table-next-field org-table-next-row org-table-paste-rectangle
2277 org-table-previous-field org-table-recalculate
2278 org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
2279 org-table-toggle-coordinate-overlays
2280 org-table-toggle-formula-debugger org-table-wrap-region
2281 orgtbl-mode turn-on-orgtbl
)))
2283 (defun org-at-table-p (&optional table-type
)
2284 "Return t if the cursor is inside an org-type table.
2285 If TABLE-TYPE is non-nil, also check for table.el-type tables."
2286 (if org-enable-table-editor
2288 (beginning-of-line 1)
2289 (looking-at (if table-type org-table-any-line-regexp
2290 org-table-line-regexp
)))
2292 (defsubst org-table-p
() (org-at-table-p))
2294 (defun org-at-table.el-p
()
2295 "Return t if and only if we are at a table.el table."
2296 (and (org-at-table-p 'any
)
2298 (goto-char (org-table-begin 'any
))
2299 (looking-at org-table1-hline-regexp
))))
2300 (defun org-table-recognize-table.el
()
2301 "If there is a table.el table nearby, recognize it and move into it."
2302 (if org-table-tab-recognizes-table.el
2303 (if (org-at-table.el-p
)
2305 (beginning-of-line 1)
2306 (if (looking-at org-table-dataline-regexp
)
2308 (if (looking-at org-table1-hline-regexp
)
2310 (beginning-of-line 2)
2311 (if (looking-at org-table-any-border-regexp
)
2312 (beginning-of-line -
1)))))
2313 (if (re-search-forward "|" (org-table-end t
) t
)
2316 (if (table--at-cell-p (point))
2318 (message "recognizing table.el table...")
2319 (table-recognize-table)
2320 (message "recognizing table.el table...done")))
2321 (error "This should not happen..."))
2326 (defun org-at-table-hline-p ()
2327 "Return t if the cursor is inside a hline in a table."
2328 (if org-enable-table-editor
2330 (beginning-of-line 1)
2331 (looking-at org-table-hline-regexp
))
2334 (defvar org-table-clean-did-remove-column nil
)
2336 (defun org-table-map-tables (function)
2337 "Apply FUNCTION to the start of all tables in the buffer."
2341 (goto-char (point-min))
2342 (while (re-search-forward org-table-any-line-regexp nil t
)
2343 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
2344 (beginning-of-line 1)
2345 (if (looking-at org-table-line-regexp
)
2346 (save-excursion (funcall function
)))
2347 (re-search-forward org-table-any-border-regexp nil
1))))
2348 (message "Mapping tables: done"))
2350 ;; Declare and autoload functions from org-exp.el
2352 (declare-function org-default-export-plist
"org-exp")
2353 (declare-function org-infile-export-plist
"org-exp")
2354 (declare-function org-get-current-options
"org-exp")
2356 (org-autoload "org-exp"
2357 '(org-export org-export-as-ascii org-export-visible
2358 org-insert-export-options-template org-export-as-html-and-open
2359 org-export-as-html-batch org-export-as-html-to-buffer
2360 org-replace-region-by-html org-export-region-as-html
2361 org-export-as-html org-export-icalendar-this-file
2362 org-export-icalendar-all-agenda-files
2363 org-export-icalendar-combine-agenda-files org-export-as-xoxo
)))
2365 ;; Declare and autoload functions from org-exp.el
2368 (org-autoload "org-exp"
2369 '(org-agenda org-agenda-list org-search-view
2370 org-todo-list org-tags-view org-agenda-list-stuck-projects
2371 org-diary org-agenda-to-appt
)))
2373 ;; Autoload org-remember
2376 (org-autoload "org-remember"
2377 '(org-remember-insinuate org-remember-annotation
2378 org-remember-apply-template org-remember org-remember-handler
)))
2380 ;; Autoload org-clock.el
2383 (declare-function org-clock-save-markers-for-cut-and-paste
"org-clock"
2385 (defvar org-clock-marker
(make-marker)
2386 "Marker recording the last clock-in.")
2391 '(org-clock-in org-clock-out org-clock-cancel
2392 org-clock-goto org-clock-sum org-clock-display
2393 org-remove-clock-overlays org-clock-report
2394 org-clocktable-shift org-dblock-write
:clocktable
2395 org-get-clocktable
)))
2397 (defun org-clock-update-time-maybe ()
2398 "If this is a CLOCK line, update it and return t.
2399 Otherwise, return nil."
2402 (beginning-of-line 1)
2403 (skip-chars-forward " \t")
2404 (when (looking-at org-clock-string
)
2405 (let ((re (concat "[ \t]*" org-clock-string
2406 " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
2407 "\\([ \t]*=>.*\\)?"))
2409 (if (not (looking-at re
))
2411 (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
2413 (setq ts
(match-string 1)
2414 te
(match-string 2))
2415 (setq s
(- (time-to-seconds
2416 (apply 'encode-time
(org-parse-time-string te
)))
2418 (apply 'encode-time
(org-parse-time-string ts
))))
2419 h
(floor (/ s
3600))
2423 (insert " => " (format "%2d:%02d" h m
))
2426 (defun org-check-running-clock ()
2427 "Check if the current buffer contains the running clock.
2428 If yes, offer to stop it and to save the buffer with the changes."
2429 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
2430 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
2433 (when (y-or-n-p "Save changed buffer?")
2436 (defun org-clocktable-try-shift (dir n
)
2437 "Check if this line starts a clock table, if yes, shift the time block."
2438 (when (org-match-line "#\\+BEGIN: clocktable\\>")
2439 (org-clocktable-shift dir n
)))
2441 ;; Autoload archiving code
2442 ;; The stuff that is needed for cycling and tags has to be defined here.
2444 (defgroup org-archive nil
2445 "Options concerning archiving in Org-mode."
2447 :group
'org-structure
)
2449 (defcustom org-archive-location
"%s_archive::"
2450 "The location where subtrees should be archived.
2452 Otherwise, the value of this variable is a string, consisting of two
2453 parts, separated by a double-colon.
2455 The first part is a file name - when omitted, archiving happens in the same
2456 file. %s will be replaced by the current file name (without directory part).
2457 Archiving to a different file is useful to keep archived entries from
2458 contributing to the Org-mode Agenda.
2460 The part after the double colon is a headline. The archived entries will be
2461 filed under that headline. When omitted, the subtrees are simply filed away
2462 at the end of the file, as top-level entries.
2464 Here are a few examples:
2466 If the current file is Projects.org, archive in file
2467 Projects.org_archive, as top-level trees. This is the default.
2469 \"::* Archived Tasks\"
2470 Archive in the current file, under the top-level headline
2471 \"* Archived Tasks\".
2473 \"~/org/archive.org::\"
2474 Archive in file ~/org/archive.org (absolute path), as top-level trees.
2476 \"basement::** Finished Tasks\"
2477 Archive in file ./basement (relative path), as level 3 trees
2478 below the level 2 heading \"** Finished Tasks\".
2480 You may set this option on a per-file basis by adding to the buffer a
2483 #+ARCHIVE: basement::** Finished Tasks
2485 You may also define it locally for a subtree by setting an ARCHIVE property
2486 in the entry. If such a property is found in an entry, or anywhere up
2487 the hierarchy, it will be used."
2491 (defcustom org-archive-tag
"ARCHIVE"
2492 "The tag that marks a subtree as archived.
2493 An archived subtree does not open during visibility cycling, and does
2494 not contribute to the agenda listings.
2495 After changing this, font-lock must be restarted in the relevant buffers to
2496 get the proper fontification."
2498 :group
'org-keywords
2501 (defcustom org-agenda-skip-archived-trees t
2502 "Non-nil means, the agenda will skip any items located in archived trees.
2503 An archived tree is a tree marked with the tag ARCHIVE."
2505 :group
'org-agenda-skip
2508 (defcustom org-cycle-open-archived-trees nil
2509 "Non-nil means, `org-cycle' will open archived trees.
2510 An archived tree is a tree marked with the tag ARCHIVE.
2511 When nil, archived trees will stay folded. You can still open them with
2512 normal outline commands like `show-all', but not with the cycling commands."
2517 (defcustom org-sparse-tree-open-archived-trees nil
2518 "Non-nil means sparse tree construction shows matches in archived trees.
2519 When nil, matches in these trees are highlighted, but the trees are kept in
2522 :group
'org-sparse-trees
2525 (defun org-cycle-hide-archived-subtrees (state)
2526 "Re-hide all archived subtrees after a visibility state change."
2527 (when (and (not org-cycle-open-archived-trees
)
2528 (not (memq state
'(overview folded
))))
2530 (let* ((globalp (memq state
'(contents all
)))
2531 (beg (if globalp
(point-min) (point)))
2532 (end (if globalp
(point-max) (org-end-of-subtree t
))))
2533 (org-hide-archived-subtrees beg end
)
2535 (if (looking-at (concat ".*:" org-archive-tag
":"))
2536 (message "%s" (substitute-command-keys
2537 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
2539 (defun org-force-cycle-archived ()
2540 "Cycle subtree even if it is archived."
2542 (setq this-command
'org-cycle
)
2543 (let ((org-cycle-open-archived-trees t
))
2544 (call-interactively 'org-cycle
)))
2546 (defun org-hide-archived-subtrees (beg end
)
2547 "Re-hide all archived subtrees after a visibility state change."
2549 (let* ((re (concat ":" org-archive-tag
":")))
2551 (while (re-search-forward re end t
)
2552 (and (org-on-heading-p) (hide-subtree))
2553 (org-end-of-subtree t
)))))
2555 (defalias 'org-advertized-archive-subtree
'org-archive-subtree
)
2558 (org-autoload "org-archive"
2559 '(org-add-archive-files org-archive-subtree
2560 org-archive-to-archive-sibling org-toggle-archive-tag
)))
2562 ;; Autoload Column View Code
2564 (declare-function org-columns-number-to-string
"org-colview")
2565 (declare-function org-columns-get-format-and-top-level
"org-colview")
2566 (declare-function org-columns-compute
"org-colview")
2568 (org-autoload (if (featurep 'xemacs
) "org-colview-xemacs" "org-colview")
2569 '(org-columns-number-to-string org-columns-get-format-and-top-level
2570 org-columns-compute org-agenda-columns org-columns-remove-overlays
2571 org-columns org-insert-columns-dblock
))
2573 ;;; Variables for pre-computed regular expressions, all buffer local
2575 (defvar org-drawer-regexp nil
2576 "Matches first line of a hidden block.")
2577 (make-variable-buffer-local 'org-drawer-regexp
)
2578 (defvar org-todo-regexp nil
2579 "Matches any of the TODO state keywords.")
2580 (make-variable-buffer-local 'org-todo-regexp
)
2581 (defvar org-not-done-regexp nil
2582 "Matches any of the TODO state keywords except the last one.")
2583 (make-variable-buffer-local 'org-not-done-regexp
)
2584 (defvar org-todo-line-regexp nil
2585 "Matches a headline and puts TODO state into group 2 if present.")
2586 (make-variable-buffer-local 'org-todo-line-regexp
)
2587 (defvar org-complex-heading-regexp nil
2588 "Matches a headline and puts everything into groups:
2590 group 2: The todo keyword, maybe
2591 group 3: Priority cookie
2592 group 4: True headline
2594 (make-variable-buffer-local 'org-complex-heading-regexp
)
2595 (defvar org-todo-line-tags-regexp nil
2596 "Matches a headline and puts TODO state into group 2 if present.
2597 Also put tags into group 4 if tags are present.")
2598 (make-variable-buffer-local 'org-todo-line-tags-regexp
)
2599 (defvar org-nl-done-regexp nil
2600 "Matches newline followed by a headline with the DONE keyword.")
2601 (make-variable-buffer-local 'org-nl-done-regexp
)
2602 (defvar org-looking-at-done-regexp nil
2603 "Matches the DONE keyword a point.")
2604 (make-variable-buffer-local 'org-looking-at-done-regexp
)
2605 (defvar org-ds-keyword-length
12
2606 "Maximum length of the Deadline and SCHEDULED keywords.")
2607 (make-variable-buffer-local 'org-ds-keyword-length
)
2608 (defvar org-deadline-regexp nil
2609 "Matches the DEADLINE keyword.")
2610 (make-variable-buffer-local 'org-deadline-regexp
)
2611 (defvar org-deadline-time-regexp nil
2612 "Matches the DEADLINE keyword together with a time stamp.")
2613 (make-variable-buffer-local 'org-deadline-time-regexp
)
2614 (defvar org-deadline-line-regexp nil
2615 "Matches the DEADLINE keyword and the rest of the line.")
2616 (make-variable-buffer-local 'org-deadline-line-regexp
)
2617 (defvar org-scheduled-regexp nil
2618 "Matches the SCHEDULED keyword.")
2619 (make-variable-buffer-local 'org-scheduled-regexp
)
2620 (defvar org-scheduled-time-regexp nil
2621 "Matches the SCHEDULED keyword together with a time stamp.")
2622 (make-variable-buffer-local 'org-scheduled-time-regexp
)
2623 (defvar org-closed-time-regexp nil
2624 "Matches the CLOSED keyword together with a time stamp.")
2625 (make-variable-buffer-local 'org-closed-time-regexp
)
2627 (defvar org-keyword-time-regexp nil
2628 "Matches any of the 4 keywords, together with the time stamp.")
2629 (make-variable-buffer-local 'org-keyword-time-regexp
)
2630 (defvar org-keyword-time-not-clock-regexp nil
2631 "Matches any of the 3 keywords, together with the time stamp.")
2632 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp
)
2633 (defvar org-maybe-keyword-time-regexp nil
2634 "Matches a timestamp, possibly preceeded by a keyword.")
2635 (make-variable-buffer-local 'org-maybe-keyword-time-regexp
)
2636 (defvar org-planning-or-clock-line-re nil
2637 "Matches a line with planning or clock info.")
2638 (make-variable-buffer-local 'org-planning-or-clock-line-re
)
2640 (defconst org-plain-time-of-day-regexp
2643 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
2646 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
2648 "Regular expression to match a plain time or time range.
2649 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
2650 groups carry important information:
2652 1 the first time, range or not
2653 8 the second time, if it is a range.")
2655 (defconst org-plain-time-extension-regexp
2658 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
2659 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
2660 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
2661 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
2662 groups carry important information:
2665 9 minutes of duration")
2667 (defconst org-stamp-time-of-day-regexp
2669 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
2670 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
2672 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
2673 "Regular expression to match a timestamp time or time range.
2674 After a match, the following groups carry important information:
2676 1 date plus weekday, for backreferencing to make sure both times on same day
2677 2 the first time, range or not
2678 4 the second time, if it is a range.")
2680 (defconst org-startup-options
2681 '(("fold" org-startup-folded t
)
2682 ("overview" org-startup-folded t
)
2683 ("nofold" org-startup-folded nil
)
2684 ("showall" org-startup-folded nil
)
2685 ("content" org-startup-folded content
)
2686 ("hidestars" org-hide-leading-stars t
)
2687 ("showstars" org-hide-leading-stars nil
)
2688 ("odd" org-odd-levels-only t
)
2689 ("oddeven" org-odd-levels-only nil
)
2690 ("align" org-startup-align-all-tables t
)
2691 ("noalign" org-startup-align-all-tables nil
)
2692 ("customtime" org-display-custom-times t
)
2693 ("logdone" org-log-done time
)
2694 ("lognotedone" org-log-done note
)
2695 ("nologdone" org-log-done nil
)
2696 ("lognoteclock-out" org-log-note-clock-out t
)
2697 ("nolognoteclock-out" org-log-note-clock-out nil
)
2698 ("logrepeat" org-log-repeat state
)
2699 ("lognoterepeat" org-log-repeat note
)
2700 ("nologrepeat" org-log-repeat nil
)
2701 ("constcgs" constants-unit-system cgs
)
2702 ("constSI" constants-unit-system SI
))
2703 "Variable associated with STARTUP options for org-mode.
2704 Each element is a list of three items: The startup options as written
2705 in the #+STARTUP line, the corresponding variable, and the value to
2706 set this variable to if the option is found. An optional forth element PUSH
2707 means to push this value onto the list in the variable.")
2709 (defun org-set-regexps-and-options ()
2710 "Precompute regular expressions for current buffer."
2712 (org-set-local 'org-todo-kwd-alist nil
)
2713 (org-set-local 'org-todo-key-alist nil
)
2714 (org-set-local 'org-todo-key-trigger nil
)
2715 (org-set-local 'org-todo-keywords-1 nil
)
2716 (org-set-local 'org-done-keywords nil
)
2717 (org-set-local 'org-todo-heads nil
)
2718 (org-set-local 'org-todo-sets nil
)
2719 (org-set-local 'org-todo-log-states nil
)
2720 (let ((re (org-make-options-regexp
2721 '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
2722 "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
2723 "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
2725 kwds kws0 kwsa key log value cat arch tags const links hw dws
2726 tail sep kws1 prio props drawers
2727 ext-setup-or-nil setup-contents
(start 0))
2731 (goto-char (point-min))
2732 (while (or (and ext-setup-or-nil
2733 (string-match re ext-setup-or-nil start
)
2734 (setq start
(match-end 0)))
2735 (and (setq ext-setup-or-nil nil start
0)
2736 (re-search-forward re nil t
)))
2737 (setq key
(upcase (match-string 1 ext-setup-or-nil
))
2738 value
(org-match-string-no-properties 2 ext-setup-or-nil
))
2740 ((equal key
"CATEGORY")
2741 (if (string-match "[ \t]+$" value
)
2742 (setq value
(replace-match "" t t value
)))
2744 ((member key
'("SEQ_TODO" "TODO"))
2745 (push (cons 'sequence
(org-split-string value splitre
)) kwds
))
2746 ((equal key
"TYP_TODO")
2747 (push (cons 'type
(org-split-string value splitre
)) kwds
))
2749 (setq tags
(append tags
(org-split-string value splitre
))))
2750 ((equal key
"COLUMNS")
2751 (org-set-local 'org-columns-default-format value
))
2753 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value
)
2754 (push (cons (match-string 1 value
)
2755 (org-trim (match-string 2 value
)))
2757 ((equal key
"PRIORITIES")
2758 (setq prio
(org-split-string value
" +")))
2759 ((equal key
"PROPERTY")
2760 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
2761 (push (cons (match-string 1 value
) (match-string 2 value
))
2763 ((equal key
"DRAWERS")
2764 (setq drawers
(org-split-string value splitre
)))
2765 ((equal key
"CONSTANTS")
2766 (setq const
(append const
(org-split-string value splitre
))))
2767 ((equal key
"STARTUP")
2768 (let ((opts (org-split-string value splitre
))
2770 (while (setq l
(pop opts
))
2771 (when (setq l
(assoc l org-startup-options
))
2772 (setq var
(nth 1 l
) val
(nth 2 l
))
2774 (set (make-local-variable var
) val
)
2775 (if (not (listp (symbol-value var
)))
2776 (set (make-local-variable var
) nil
))
2777 (set (make-local-variable var
) (symbol-value var
))
2778 (add-to-list var val
))))))
2779 ((equal key
"ARCHIVE")
2780 (string-match " *$" value
)
2781 (setq arch
(replace-match "" t t value
))
2782 (remove-text-properties 0 (length arch
)
2783 '(face t fontified t
) arch
))
2784 ((equal key
"SETUPFILE")
2785 (setq setup-contents
(org-file-contents
2787 (org-remove-double-quotes value
))
2789 (if (not ext-setup-or-nil
)
2790 (setq ext-setup-or-nil setup-contents start
0)
2791 (setq ext-setup-or-nil
2792 (concat (substring ext-setup-or-nil
0 start
)
2793 "\n" setup-contents
"\n"
2794 (substring ext-setup-or-nil start
)))))
2797 (org-set-local 'org-category
(intern cat
))
2798 (push (cons "CATEGORY" cat
) props
))
2800 (if (< (length prio
) 3) (setq prio
'("A" "C" "B")))
2801 (setq prio
(mapcar 'string-to-char prio
))
2802 (org-set-local 'org-highest-priority
(nth 0 prio
))
2803 (org-set-local 'org-lowest-priority
(nth 1 prio
))
2804 (org-set-local 'org-default-priority
(nth 2 prio
)))
2805 (and props
(org-set-local 'org-local-properties
(nreverse props
)))
2806 (and drawers
(org-set-local 'org-drawers drawers
))
2807 (and arch
(org-set-local 'org-archive-location arch
))
2808 (and links
(setq org-link-abbrev-alist-local
(nreverse links
)))
2809 ;; Process the TODO keywords
2811 ;; Use the global values as if they had been given locally.
2812 (setq kwds
(default-value 'org-todo-keywords
))
2813 (if (stringp (car kwds
))
2814 (setq kwds
(list (cons org-todo-interpretation
2815 (default-value 'org-todo-keywords
)))))
2816 (setq kwds
(reverse kwds
)))
2817 (setq kwds
(nreverse kwds
))
2819 (while (setq kws
(pop kwds
))
2820 (setq inter
(pop kws
) sep
(member "|" kws
)
2821 kws0
(delete "|" (copy-sequence kws
))
2826 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x
)
2828 (setq kw
(match-string 1 x
)
2829 key
(and (match-end 2) (match-string 2 x
))
2830 log
(org-extract-log-state-settings x
))
2831 (push (cons kw
(and key
(string-to-char key
))) kwsa
)
2832 (and log
(push log org-todo-log-states
))
2834 (error "Invalid TODO keyword %s" x
)))
2836 kwsa
(if kwsa
(append '((:startgroup
))
2840 dws
(if sep
(org-remove-keyword-keys (cdr sep
)) (last kws1
))
2841 tail
(list inter hw
(car dws
) (org-last dws
)))
2842 (add-to-list 'org-todo-heads hw
'append
)
2843 (push kws1 org-todo-sets
)
2844 (setq org-done-keywords
(append org-done-keywords dws nil
))
2845 (setq org-todo-key-alist
(append org-todo-key-alist kwsa
))
2846 (mapc (lambda (x) (push (cons x tail
) org-todo-kwd-alist
)) kws1
)
2847 (setq org-todo-keywords-1
(append org-todo-keywords-1 kws1 nil
)))
2848 (setq org-todo-sets
(nreverse org-todo-sets
)
2849 org-todo-kwd-alist
(nreverse org-todo-kwd-alist
)
2850 org-todo-key-trigger
(delq nil
(mapcar 'cdr org-todo-key-alist
))
2851 org-todo-key-alist
(org-assign-fast-keys org-todo-key-alist
)))
2852 ;; Process the constants
2855 (while (setq e
(pop const
))
2856 (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e
)
2857 (push (cons (match-string 1 e
) (match-string 2 e
)) cst
)))
2858 (setq org-table-formula-constants-local cst
)))
2860 ;; Process the tags.
2863 (while (setq e
(pop tags
))
2865 ((equal e
"{") (push '(:startgroup
) tgs
))
2866 ((equal e
"}") (push '(:endgroup
) tgs
))
2867 ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e
)
2868 (push (cons (match-string 1 e
)
2869 (string-to-char (match-string 2 e
)))
2871 (t (push (list e
) tgs
))))
2872 (org-set-local 'org-tag-alist nil
)
2873 (while (setq e
(pop tgs
))
2874 (or (and (stringp (car e
))
2875 (assoc (car e
) org-tag-alist
))
2876 (push e org-tag-alist
))))))
2878 ;; Compute the regular expressions and other local variables
2879 (if (not org-done-keywords
)
2880 (setq org-done-keywords
(list (org-last org-todo-keywords-1
))))
2881 (setq org-ds-keyword-length
(+ 2 (max (length org-deadline-string
)
2882 (length org-scheduled-string
)
2883 (length org-clock-string
)
2884 (length org-closed-string
)))
2886 (concat "^[ \t]*:\\("
2887 (mapconcat 'regexp-quote org-drawers
"\\|")
2889 org-not-done-keywords
2890 (org-delete-all org-done-keywords
(copy-sequence org-todo-keywords-1
))
2892 (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
2896 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
2898 org-todo-line-regexp
2899 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
2900 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
2901 "\\)\\>\\)?[ \t]*\\(.*\\)")
2902 org-complex-heading-regexp
2903 (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
2904 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
2905 "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
2906 "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
2908 (concat "\n\\*+[ \t]+"
2909 "\\(?:" (mapconcat 'regexp-quote org-done-keywords
"\\|")
2911 org-todo-line-tags-regexp
2912 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
2913 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
2915 "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
2916 org-looking-at-done-regexp
2918 (mapconcat 'regexp-quote org-done-keywords
"\\|") "\\)"
2920 org-deadline-regexp
(concat "\\<" org-deadline-string
)
2921 org-deadline-time-regexp
2922 (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>")
2923 org-deadline-line-regexp
2924 (concat "\\<\\(" org-deadline-string
"\\).*")
2925 org-scheduled-regexp
2926 (concat "\\<" org-scheduled-string
)
2927 org-scheduled-time-regexp
2928 (concat "\\<" org-scheduled-string
" *<\\([^>]+\\)>")
2929 org-closed-time-regexp
2930 (concat "\\<" org-closed-string
" *\\[\\([^]]+\\)\\]")
2931 org-keyword-time-regexp
2932 (concat "\\<\\(" org-scheduled-string
2933 "\\|" org-deadline-string
2934 "\\|" org-closed-string
2935 "\\|" org-clock-string
"\\)"
2936 " *[[<]\\([^]>]+\\)[]>]")
2937 org-keyword-time-not-clock-regexp
2938 (concat "\\<\\(" org-scheduled-string
2939 "\\|" org-deadline-string
2940 "\\|" org-closed-string
2942 " *[[<]\\([^]>]+\\)[]>]")
2943 org-maybe-keyword-time-regexp
2944 (concat "\\(\\<\\(" org-scheduled-string
2945 "\\|" org-deadline-string
2946 "\\|" org-closed-string
2947 "\\|" org-clock-string
"\\)\\)?"
2948 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
2949 org-planning-or-clock-line-re
2950 (concat "\\(?:^[ \t]*\\(" org-scheduled-string
2951 "\\|" org-deadline-string
2952 "\\|" org-closed-string
"\\|" org-clock-string
2955 (org-compute-latex-and-specials-regexp)
2956 (org-set-font-lock-defaults)))
2958 (defun org-file-contents (file &optional noerror
)
2959 "Return the contents of FILE, as a string."
2961 (not (file-readable-p file
)))
2964 (message "Cannot read file %s" file
)
2967 (error "Cannot read file %s" file
))
2969 (insert-file-contents file
)
2972 (defun org-extract-log-state-settings (x)
2973 "Extract the log state setting from a TODO keyword string.
2974 This will extract info from a string like \"WAIT(w@/!)\"."
2975 (let (kw key log1 log2
)
2976 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x
)
2977 (setq kw
(match-string 1 x
)
2978 key
(and (match-end 2) (match-string 2 x
))
2979 log1
(and (match-end 3) (match-string 3 x
))
2980 log2
(and (match-end 4) (match-string 4 x
)))
2983 (and log1
(if (equal log1
"!") 'time
'note
))
2984 (and log2
(if (equal log2
"!") 'time
'note
)))))))
2986 (defun org-remove-keyword-keys (list)
2987 "Remove a pair of parenthesis at the end of each string in LIST."
2989 (if (string-match "(.*)$" x
)
2990 (substring x
0 (match-beginning 0))
2994 ;; FIXME: this could be done much better, using second characters etc.
2995 (defun org-assign-fast-keys (alist)
2996 "Assign fast keys to a keyword-key alist.
2997 Respect keys that are already there."
2998 (let (new e k c c1 c2
(char ?a
))
2999 (while (setq e
(pop alist
))
3001 ((equal e
'(:startgroup
)) (push e new
))
3002 ((equal e
'(:endgroup
)) (push e new
))
3004 (setq k
(car e
) c2 nil
)
3007 ;; automatically assign a character.
3008 (setq c1
(string-to-char
3009 (downcase (substring
3010 k
(if (= (string-to-char k
) ?
@) 1 0)))))
3011 (if (or (rassoc c1 new
) (rassoc c1 alist
))
3012 (while (or (rassoc char new
) (rassoc char alist
))
3013 (setq char
(1+ char
)))
3015 (setq c
(or c2 char
)))
3016 (push (cons k c
) new
))))
3019 ;;; Some variables used in various places
3021 (defvar org-window-configuration nil
3022 "Used in various places to store a window configuration.")
3023 (defvar org-finish-function nil
3024 "Function to be called when `C-c C-c' is used.
3025 This is for getting out of special buffers like remember.")
3028 ;; FIXME: Occasionally check by commenting these, to make sure
3029 ;; no other functions uses these, forgetting to let-bind them.
3034 (defvar description
)
3036 ;; Defined somewhere in this file, but used before definition.
3037 (defvar org-html-entities
)
3038 (defvar org-struct-menu
)
3039 (defvar org-org-menu
)
3040 (defvar org-tbl-menu
)
3041 (defvar org-agenda-keymap
)
3043 ;;;; Define the Org-mode
3045 (if (and (not (keymapp outline-mode-map
)) (featurep 'allout
))
3046 (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or ugrade to newer allout, for example by switching to Emacs 22."))
3049 ;; We use a before-change function to check if a table might need
3051 (defvar org-table-may-need-update t
3052 "Indicates that a table might need an update.
3053 This variable is set by `org-before-change-function'.
3054 `org-table-align' sets it back to nil.")
3055 (defun org-before-change-function (beg end
)
3056 "Every change indicates that a table might need an update."
3057 (setq org-table-may-need-update t
))
3058 (defvar org-mode-map
)
3059 (defvar org-mode-hook nil
)
3060 (defvar org-inhibit-startup nil
) ; Dynamically-scoped param.
3061 (defvar org-agenda-keep-modes nil
) ; Dynamically-scoped param.
3062 (defvar org-table-buffer-is-an nil
)
3063 (defconst org-outline-regexp
"\\*+ ")
3066 (define-derived-mode org-mode outline-mode
"Org"
3067 "Outline-based notes management and organizer, alias
3068 \"Carsten's outline-mode for keeping track of everything.\"
3070 Org-mode develops organizational tasks around a NOTES file which
3071 contains information about projects as plain text. Org-mode is
3072 implemented on top of outline-mode, which is ideal to keep the content
3073 of large files well structured. It supports ToDo items, deadlines and
3074 time stamps, which magically appear in the diary listing of the Emacs
3075 calendar. Tables are easily created with a built-in table editor.
3076 Plain text URL-like links connect to websites, emails (VM), Usenet
3077 messages (Gnus), BBDB entries, and any files related to the project.
3078 For printing and sharing of notes, an Org-mode file (or a part of it)
3079 can be exported as a structured ASCII or HTML file.
3081 The following commands are available:
3085 ;; Get rid of Outline menus, they are not needed
3086 ;; Need to do this here because define-derived-mode sets up
3087 ;; the keymap so late. Still, it is a waste to call this each time
3088 ;; we switch another buffer into org-mode.
3089 (if (featurep 'xemacs
)
3090 (when (boundp 'outline-mode-menu-heading
)
3091 ;; Assume this is Greg's port, it used easymenu
3092 (easy-menu-remove outline-mode-menu-heading
)
3093 (easy-menu-remove outline-mode-menu-show
)
3094 (easy-menu-remove outline-mode-menu-hide
))
3095 (define-key org-mode-map
[menu-bar headings
] 'undefined
)
3096 (define-key org-mode-map
[menu-bar hide
] 'undefined
)
3097 (define-key org-mode-map
[menu-bar show
] 'undefined
))
3099 (org-load-modules-maybe)
3100 (easy-menu-add org-org-menu
)
3101 (easy-menu-add org-tbl-menu
)
3102 (org-install-agenda-files-menu)
3103 (if org-descriptive-links
(org-add-to-invisibility-spec '(org-link)))
3104 (org-add-to-invisibility-spec '(org-cwidth))
3105 (when (featurep 'xemacs
)
3106 (org-set-local 'line-move-ignore-invisible t
))
3107 (org-set-local 'outline-regexp org-outline-regexp
)
3108 (org-set-local 'outline-level
'org-outline-level
)
3109 (when (and org-ellipsis
3110 (fboundp 'set-display-table-slot
) (boundp 'buffer-display-table
)
3111 (fboundp 'make-glyph-code
))
3112 (unless org-display-table
3113 (setq org-display-table
(make-display-table)))
3114 (set-display-table-slot
3117 (lambda (c) (make-glyph-code c
(and (not (stringp org-ellipsis
))
3119 (if (stringp org-ellipsis
) org-ellipsis
"..."))))
3120 (setq buffer-display-table org-display-table
))
3121 (org-set-regexps-and-options)
3123 (org-set-local 'calc-embedded-open-mode
"# ")
3124 (modify-syntax-entry ?
# "<")
3125 (modify-syntax-entry ?
@ "w")
3126 (if org-startup-truncated
(setq truncate-lines t
))
3127 (org-set-local 'font-lock-unfontify-region-function
3128 'org-unfontify-region
)
3129 ;; Activate before-change-function
3130 (org-set-local 'org-table-may-need-update t
)
3131 (org-add-hook 'before-change-functions
'org-before-change-function nil
3133 ;; Check for running clock before killing a buffer
3134 (org-add-hook 'kill-buffer-hook
'org-check-running-clock nil
'local
)
3135 ;; Paragraphs and auto-filling
3136 (org-set-autofill-regexps)
3137 (setq indent-line-function
'org-indent-line-function
)
3138 (org-update-radio-target-regexp)
3140 ;; Comment characters
3141 ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
3142 (org-set-local 'comment-padding
" ")
3144 ;; Align options lines
3146 'align-mode-rules-list
3147 '((org-in-buffer-settings
3148 (regexp .
"^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
3149 (modes .
'(org-mode)))))
3152 (org-set-local 'imenu-create-index-function
3153 'org-imenu-get-tree
)
3155 ;; Make isearch reveal context
3156 (if (or (featurep 'xemacs
)
3157 (not (boundp 'outline-isearch-open-invisible-function
)))
3158 ;; Emacs 21 and XEmacs make use of the hook
3159 (org-add-hook 'isearch-mode-end-hook
'org-isearch-end
'append
'local
)
3160 ;; Emacs 22 deals with this through a special variable
3161 (org-set-local 'outline-isearch-open-invisible-function
3162 (lambda (&rest ignore
) (org-show-context 'isearch
))))
3164 ;; If empty file that did not turn on org-mode automatically, make it to.
3165 (if (and org-insert-mode-line-in-empty-file
3167 (= (point-min) (point-max)))
3168 (insert "# -*- mode: org -*-\n\n"))
3170 (unless org-inhibit-startup
3171 (when org-startup-align-all-tables
3172 (let ((bmp (buffer-modified-p)))
3173 (org-table-map-tables 'org-table-align
)
3174 (set-buffer-modified-p bmp
)))
3175 (org-set-startup-visibility)))
3177 (put 'org-mode
'flyspell-mode-predicate
'org-mode-flyspell-verify
)
3179 (defun org-current-time ()
3180 "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
3181 (if (> (car org-time-stamp-rounding-minutes
) 1)
3182 (let ((r (car org-time-stamp-rounding-minutes
))
3183 (time (decode-time)))
3185 (append (list 0 (* r
(floor (+ .5 (/ (float (nth 1 time
)) r
)))))
3189 ;;;; Font-Lock stuff, including the activators
3191 (defvar org-mouse-map
(make-sparse-keymap))
3192 (org-defkey org-mouse-map
3193 (if (featurep 'xemacs
) [button2] [mouse-2]) 'org-open-at-mouse)
3194 (org-defkey org-mouse-map
3195 (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
3196 (when org-mouse-1-follows-link
3197 (org-defkey org-mouse-map [follow-link] 'mouse-face))
3198 (when org-tab-follows-link
3199 (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
3200 (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
3201 (when org-return-follows-link
3202 (org-defkey org-mouse-map [(return)] 'org-open-at-point)
3203 (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
3205 (require 'font-lock)
3207 (defconst org-non-link-chars "]\t\n\r<>")
3208 (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
3210 (defvar org-link-types-re nil
3211 "Matches a link that has a url-like prefix like \"http:\"")
3212 (defvar org-link-re-with-space nil
3213 "Matches a link with spaces, optional angular brackets around it.")
3214 (defvar org-link-re-with-space2 nil
3215 "Matches a link with spaces, optional angular brackets around it.")
3216 (defvar org-angle-link-re nil
3217 "Matches link with angular brackets, spaces are allowed.")
3218 (defvar org-plain-link-re nil
3219 "Matches plain link, without spaces.")
3220 (defvar org-bracket-link-regexp nil
3221 "Matches a link in double brackets.")
3222 (defvar org-bracket-link-analytic-regexp nil
3223 "Regular expression used to analyze links.
3224 Here is what the match groups contain after a match:
3230 (defvar org-any-link-re nil
3231 "Regular expression matching any link.")
3233 (defun org-make-link-regexps ()
3234 "Update the link regular expressions.
3235 This should be called after the variable `org-link-types' has changed."
3236 (setq org-link-types-re
3238 "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
3239 org-link-re-with-space
3241 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3242 "\\([^" org-non-link-chars " ]"
3243 "[^" org-non-link-chars "]*"
3244 "[^" org-non-link-chars " ]\\)>?")
3245 org-link-re-with-space2
3247 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3248 "\\([^" org-non-link-chars " ]"
3250 "[^" org-non-link-chars " ]\\)>?")
3253 "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3254 "\\([^" org-non-link-chars " ]"
3255 "[^" org-non-link-chars "]*"
3259 "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3260 "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
3261 org-bracket-link-regexp
3262 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
3263 org-bracket-link-analytic-regexp
3266 "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
3269 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
3272 (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
3273 org-angle-link-re "\\)\\|\\("
3274 org-plain-link-re "\\)")))
3276 (org-make-link-regexps)
3278 (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
3279 "Regular expression for fast time stamp matching.")
3280 (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
3281 "Regular expression for fast time stamp matching.")
3282 (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
3283 "Regular expression matching time strings for analysis.
3284 This one does not require the space after the date, so it can be used
3285 on a string that terminates immediately after the date.")
3286 (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
3287 "Regular expression matching time strings for analysis.")
3288 (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
3289 "Regular expression matching time stamps, with groups.")
3290 (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
3291 "Regular expression matching time stamps (also [..]), with groups.")
3292 (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
3293 "Regular expression matching a time stamp range.")
3294 (defconst org-tr-regexp-both
3295 (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
3296 "Regular expression matching a time stamp range.")
3297 (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
3298 org-ts-regexp "\\)?")
3299 "Regular expression matching a time stamp or time stamp range.")
3300 (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
3301 org-ts-regexp-both "\\)?")
3302 "Regular expression matching a time stamp or time stamp range.
3303 The time stamps may be either active or inactive.")
3305 (defvar org-emph-face nil)
3307 (defun org-do-emphasis-faces (limit)
3308 "Run through the buffer and add overlays to links."
3310 (while (and (not rtn) (re-search-forward org-emph-re limit t))
3311 (if (not (= (char-after (match-beginning 3))
3312 (char-after (match-beginning 4))))
3315 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
3317 (nth 1 (assoc (match-string 3)
3318 org-emphasis-alist)))
3319 (add-text-properties (match-beginning 2) (match-end 2)
3320 '(font-lock-multiline t))
3321 (when org-hide-emphasis-markers
3322 (add-text-properties (match-end 4) (match-beginning 5)
3323 '(invisible org-link))
3324 (add-text-properties (match-beginning 3) (match-end 3)
3325 '(invisible org-link)))))
3329 (defun org-emphasize (&optional char)
3330 "Insert or change an emphasis, i.e. a font like bold or italic.
3331 If there is an active region, change that region to a new emphasis.
3332 If there is no region, just insert the marker characters and position
3333 the cursor between them.
3334 CHAR should be either the marker character, or the first character of the
3335 HTML tag associated with that emphasis. If CHAR is a space, the means
3336 to remove the emphasis of the selected region.
3337 If char is not given (for example in an interactive call) it
3338 will be prompted for."
3340 (let ((eal org-emphasis-alist) e det
3341 (erc org-emphasis-regexp-components)
3343 (string "") beg end move tag c s)
3344 (if (org-region-active-p)
3345 (setq beg (region-beginning) end (region-end)
3346 string (buffer-substring beg end))
3349 (while (setq e (pop eal))
3350 (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
3352 (push (cons c (string-to-char (car e))) det)
3353 (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
3354 (substring tag 1)))))
3356 (message "%s" (concat "Emphasis marker or tag:" prompt))
3357 (setq char (read-char-exclusive)))
3358 (setq char (or (cdr (assoc char det)) char))
3359 (if (equal char ?\ )
3360 (setq s "" move nil)
3361 (unless (assoc (char-to-string char) org-emphasis-alist)
3362 (error "No such emphasis marker: \"%c\"" char))
3363 (setq s (char-to-string char)))
3364 (while (and (> (length string) 1)
3365 (equal (substring string 0 1) (substring string -1))
3366 (assoc (substring string 0 1) org-emphasis-alist))
3367 (setq string (substring string 1 -1)))
3368 (setq string (concat s string s))
3369 (if beg (delete-region beg end))
3371 (string-match (concat "[" (nth 0 erc) "\n]")
3372 (char-to-string (char-before (point)))))
3374 (unless (string-match (concat "[" (nth 1 erc) "\n]")
3375 (char-to-string (char-after (point))))
3376 (insert " ") (backward-char 1))
3378 (and move (backward-char 1))))
3380 (defconst org-nonsticky-props
3381 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
3384 (defun org-activate-plain-links (limit)
3385 "Run through the buffer and add overlays to links."
3388 (while (re-search-forward org-plain-link-re limit t)
3389 (setq f (get-text-property (match-beginning 0) 'face))
3390 (if (or (eq f 'org-tag)
3391 (and (listp f) (memq 'org-tag f)))
3393 (add-text-properties (match-beginning 0) (match-end 0)
3394 (list 'mouse-face 'highlight
3395 'rear-nonsticky org-nonsticky-props
3396 'keymap org-mouse-map
3398 (throw 'exit t))))))
3400 (defun org-activate-code (limit)
3401 (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
3402 (unless (get-text-property (match-beginning 1) 'face)
3403 (remove-text-properties (match-beginning 0) (match-end 0)
3404 '(display t invisible t intangible t))
3407 (defun org-activate-angle-links (limit)
3408 "Run through the buffer and add overlays to links."
3409 (if (re-search-forward org-angle-link-re limit t)
3411 (add-text-properties (match-beginning 0) (match-end 0)
3412 (list 'mouse-face 'highlight
3413 'rear-nonsticky org-nonsticky-props
3414 'keymap org-mouse-map
3418 (defun org-activate-bracket-links (limit)
3419 "Run through the buffer and add overlays to bracketed links."
3420 (if (re-search-forward org-bracket-link-regexp limit t)
3421 (let* ((help (concat "LINK: "
3422 (org-match-string-no-properties 1)))
3423 ;; FIXME: above we should remove the escapes.
3424 ;; but that requires another match, protecting match data,
3425 ;; a lot of overhead for font-lock.
3426 (ip (org-maybe-intangible
3427 (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
3428 'keymap org-mouse-map 'mouse-face 'highlight
3429 'font-lock-multiline t 'help-echo help)))
3430 (vp (list 'rear-nonsticky org-nonsticky-props
3431 'keymap org-mouse-map 'mouse-face 'highlight
3432 ' font-lock-multiline t 'help-echo help)))
3433 ;; We need to remove the invisible property here. Table narrowing
3434 ;; may have made some of this invisible.
3435 (remove-text-properties (match-beginning 0) (match-end 0)
3439 (add-text-properties (match-beginning 0) (match-beginning 3) ip)
3440 (add-text-properties (match-beginning 3) (match-end 3) vp)
3441 (add-text-properties (match-end 3) (match-end 0) ip))
3442 (add-text-properties (match-beginning 0) (match-beginning 1) ip)
3443 (add-text-properties (match-beginning 1) (match-end 1) vp)
3444 (add-text-properties (match-end 1) (match-end 0) ip))
3447 (defun org-activate-dates (limit)
3448 "Run through the buffer and add overlays to dates."
3449 (if (re-search-forward org-tsr-regexp-both limit t)
3451 (add-text-properties (match-beginning 0) (match-end 0)
3452 (list 'mouse-face 'highlight
3453 'rear-nonsticky org-nonsticky-props
3454 'keymap org-mouse-map))
3455 (when org-display-custom-times
3457 (org-display-custom-time (match-beginning 3) (match-end 3)))
3458 (org-display-custom-time (match-beginning 1) (match-end 1)))
3461 (defvar org-target-link-regexp nil
3462 "Regular expression matching radio targets in plain text.")
3463 (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
3464 "Regular expression matching a link target.")
3465 (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
3466 "Regular expression matching a radio target.")
3467 (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
3468 "Regular expression matching any target.")
3470 (defun org-activate-target-links (limit)
3471 "Run through the buffer and add overlays to target matches."
3472 (when org-target-link-regexp
3473 (let ((case-fold-search t))
3474 (if (re-search-forward org-target-link-regexp limit t)
3476 (add-text-properties (match-beginning 0) (match-end 0)
3477 (list 'mouse-face 'highlight
3478 'rear-nonsticky org-nonsticky-props
3479 'keymap org-mouse-map
3480 'help-echo "Radio target link"
3481 'org-linked-text t))
3484 (defun org-update-radio-target-regexp ()
3485 "Find all radio targets in this file and update the regular expression."
3487 (when (memq 'radio org-activate-links)
3488 (setq org-target-link-regexp
3489 (org-make-target-link-regexp (org-all-targets 'radio)))
3490 (org-restart-font-lock)))
3492 (defun org-hide-wide-columns (limit)
3494 (setq s (text-property-any (point) (or limit (point-max))
3497 (setq e (next-single-property-change s 'org-cwidth))
3498 (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
3502 (defvar org-latex-and-specials-regexp nil
3503 "Regular expression for highlighting export special stuff.")
3504 (defvar org-match-substring-regexp)
3505 (defvar org-match-substring-with-braces-regexp)
3506 (defvar org-export-html-special-string-regexps)
3508 (defun org-compute-latex-and-specials-regexp ()
3509 "Compute regular expression for stuff treated specially by exporters."
3510 (if (not org-highlight-latex-fragments-and-specials)
3511 (org-set-local 'org-latex-and-specials-regexp nil)
3514 ((matchers (plist-get org-format-latex-options :matchers))
3515 (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
3516 org-latex-regexps)))
3517 (options (org-combine-plists (org-default-export-plist)
3518 (org-infile-export-plist)))
3519 (org-export-with-sub-superscripts (plist-get options :sub-superscript))
3520 (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
3521 (org-export-with-TeX-macros (plist-get options :TeX-macros))
3522 (org-export-html-expand (plist-get options :expand-quoted-html))
3523 (org-export-with-special-strings (plist-get options :special-strings))
3526 ((equal org-export-with-sub-superscripts '{})
3527 (list org-match-substring-with-braces-regexp))
3528 (org-export-with-sub-superscripts
3529 (list org-match-substring-regexp))
3532 (if org-export-with-LaTeX-fragments
3533 (mapcar (lambda (x) (nth 1 x)) latexs)))
3535 (if org-export-with-TeX-macros
3536 (list (concat "\\\\"
3538 (append (mapcar 'car org-html-entities)
3539 (if (boundp 'org-latex-entities)
3540 org-latex-entities nil))
3543 ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
3544 (re-special (if org-export-with-special-strings
3545 (mapcar (lambda (x) (car x))
3546 org-export-html-special-string-regexps)))
3550 (if org-export-html-expand "@<[^>\n]+>")
3553 'org-latex-and-specials-regexp
3554 (mapconcat 'identity (append re-latex re-sub re-macros re-special
3557 (defun org-do-latex-and-special-faces (limit)
3558 "Run through the buffer and add overlays to links."
3559 (when org-latex-and-specials-regexp
3561 (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
3563 (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
3565 '(org-code org-verbatim underline)))
3568 d (cond ((member (char-after (1+ (match-beginning 0)))
3571 (font-lock-prepend-text-property
3572 (+ d (match-beginning 0)) (match-end 0)
3573 'face 'org-latex-and-export-specials)
3574 (add-text-properties (+ d (match-beginning 0)) (match-end 0)
3575 '(font-lock-multiline t)))))
3578 (defun org-restart-font-lock ()
3579 "Restart font-lock-mode, to force refontification."
3580 (when (and (boundp 'font-lock-mode) font-lock-mode)
3582 (font-lock-mode 1)))
3584 (defun org-all-targets (&optional radio)
3585 "Return a list of all targets in this file.
3586 With optional argument RADIO, only find radio targets."
3587 (let ((re (if radio org-radio-target-regexp org-target-regexp))
3590 (goto-char (point-min))
3591 (while (re-search-forward re nil t)
3592 (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
3595 (defun org-make-target-link-regexp (targets)
3596 "Make regular expression matching all strings in TARGETS.
3597 The regular expression finds the targets also if there is a line break
3604 (while (string-match " +" x)
3605 (setq x (replace-match "\\s-+" t t x)))
3611 (defun org-activate-tags (limit)
3612 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
3614 (add-text-properties (match-beginning 1) (match-end 1)
3615 (list 'mouse-face 'highlight
3616 'rear-nonsticky org-nonsticky-props
3617 'keymap org-mouse-map))
3620 (defun org-outline-level ()
3622 (looking-at outline-regexp)
3623 (if (match-beginning 1)
3624 (+ (org-get-string-indentation (match-string 1)) 1000)
3625 (1- (- (match-end 0) (match-beginning 0))))))
3627 (defvar org-font-lock-keywords nil)
3629 (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
3630 "Regular expression matching a property line.")
3632 (defvar org-font-lock-hook nil
3633 "Functions to be called for special font lock stuff.")
3635 (defun org-font-lock-hook (limit)
3636 (run-hook-with-args 'org-font-lock-hook limit))
3638 (defun org-set-font-lock-defaults ()
3639 (let* ((em org-fontify-emphasized-text)
3640 (lk org-activate-links)
3641 (org-font-lock-extra-keywords
3644 '(org-font-lock-hook)
3646 '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
3647 (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
3649 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
3652 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
3653 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
3654 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
3656 (list org-drawer-regexp '(0 'org-special-keyword t))
3657 (list "^[ \t]*:END:" '(0 'org-special-keyword t))
3659 (list org-property-re
3660 '(1 'org-special-keyword t)
3661 '(3 'org-property-value t))
3662 (if org-format-transports-properties-p
3663 '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
3665 (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
3666 (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
3667 (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
3668 (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
3669 (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
3670 (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
3671 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
3672 '(org-hide-wide-columns (0 nil append))
3674 (list (concat "^\\*+[ \t]+" org-todo-regexp)
3675 '(1 (org-get-todo-face 1) t))
3677 (if org-fontify-done-headline
3678 (list (concat "^[*]+ +\\<\\("
3679 (mapconcat 'regexp-quote org-done-keywords "\\|")
3681 '(2 'org-headline-done t))
3684 (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
3686 (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
3687 (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
3688 (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
3689 (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
3692 (if (featurep 'xemacs)
3693 '(org-do-emphasis-faces (0 nil append))
3694 '(org-do-emphasis-faces)))
3696 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
3698 (if org-provide-checkbox-statistics
3699 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
3700 (0 (org-get-checkbox-statistics-face) t)))
3701 ;; Description list items
3702 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
3704 (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
3705 '(1 'org-archived prepend))
3707 '(org-do-latex-and-special-faces)
3709 '(org-activate-code (1 'org-code t))
3711 (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
3712 "\\|" org-quote-string "\\)\\>")
3713 '(1 'org-special-keyword t))
3714 '("^#.*" (0 'font-lock-comment-face t))
3716 (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
3717 ;; Now set the full font-lock-keywords
3718 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
3719 (org-set-local 'font-lock-defaults
3720 '(org-font-lock-keywords t nil nil backward-paragraph))
3721 (kill-local-variable 'font-lock-keywords) nil))
3726 (defun org-get-level-face (n)
3727 "Get the right face for match N in font-lock matching of healdines."
3728 (setq org-l (- (match-end 2) (match-beginning 1) 1))
3729 (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
3730 (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
3732 ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
3734 (t (if org-level-color-stars-only nil org-f))))
3736 (defun org-get-todo-face (kwd)
3737 "Get the right face for a TODO keyword KWD.
3738 If KWD is a number, get the corresponding match group."
3739 (if (numberp kwd) (setq kwd (match-string kwd)))
3740 (or (cdr (assoc kwd org-todo-keyword-faces))
3741 (and (member kwd org-done-keywords) 'org-done)
3744 (defun org-unfontify-region (beg end &optional maybe_loudly)
3745 "Remove fontification and activation overlays from links."
3746 (font-lock-default-unfontify-region beg end)
3747 (let* ((buffer-undo-list t)
3748 (inhibit-read-only t) (inhibit-point-motion-hooks t)
3749 (inhibit-modification-hooks t)
3750 deactivate-mark buffer-file-name buffer-file-truename)
3751 (remove-text-properties beg end
3752 '(mouse-face t keymap t org-linked-text t
3753 invisible t intangible t))))
3755 ;;;; Visibility cycling, including org-goto and indirect buffer
3759 (defvar org-cycle-global-status nil)
3760 (make-variable-buffer-local 'org-cycle-global-status)
3761 (defvar org-cycle-subtree-status nil)
3762 (make-variable-buffer-local 'org-cycle-subtree-status)
3765 (defun org-cycle (&optional arg)
3766 "Visibility cycling for Org-mode.
3768 - When this function is called with a prefix argument, rotate the entire
3769 buffer through 3 states (global cycling)
3770 1. OVERVIEW: Show only top-level headlines.
3771 2. CONTENTS: Show all headlines of all levels, but no body text.
3772 3. SHOW ALL: Show everything.
3773 When called with two C-c C-u prefixes, switch to the startup visibility,
3774 determined by the variable `org-startup-folded', and by any VISIBILITY
3775 properties in the buffer.
3777 - When point is at the beginning of a headline, rotate the subtree started
3778 by this line through 3 different states (local cycling)
3779 1. FOLDED: Only the main headline is shown.
3780 2. CHILDREN: The main headline and the direct children are shown.
3781 From this state, you can move to one of the children
3782 and zoom in further.
3783 3. SUBTREE: Show the entire subtree, including body text.
3785 - When there is a numeric prefix, go up to a heading with level ARG, do
3786 a `show-subtree' and return to the previous cursor position. If ARG
3787 is negative, go up that many levels.
3789 - When point is not at the beginning of a headline, execute the global
3790 binding for TAB, which is re-indenting the line. See the option
3791 `org-cycle-emulate-tab' for details.
3793 - Special case: if point is at the beginning of the buffer and there is
3794 no headline in line 1, this function will act as if called with prefix arg.
3795 But only if also the variable `org-cycle-global-at-bob' is t."
3797 (org-load-modules-maybe)
3798 (let* ((outline-regexp
3799 (if (and (org-mode-p) org-cycle-include-plain-lists)
3800 "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
3802 (bob-special (and org-cycle-global-at-bob (bobp)
3803 (not (looking-at outline-regexp))))
3806 (delq 'org-optimize-window-after-visibility-change
3807 (copy-sequence org-cycle-hook))
3811 (if (or bob-special (equal arg '(4)))
3812 ;; special case: use global cycling
3818 (org-set-startup-visibility)
3819 (message "Startup visibility, plus VISIBILITY properties."))
3821 ((org-at-table-p 'any)
3822 ;; Enter the table or move to the next field in the table
3823 (or (org-table-recognize-table.el)
3825 (if arg (org-table-edit-field t)
3826 (org-table-justify-field-maybe)
3827 (call-interactively 'org-table-next-field)))))
3829 ((eq arg t) ;; Global cycling
3832 ((and (eq last-command this-command)
3833 (eq org-cycle-global-status 'overview))
3834 ;; We just created the overview - now do table of contents
3835 ;; This can be slow in very large buffers, so indicate action
3836 (message "CONTENTS...")
3838 (message "CONTENTS...done")
3839 (setq org-cycle-global-status 'contents)
3840 (run-hook-with-args 'org-cycle-hook 'contents))
3842 ((and (eq last-command this-command)
3843 (eq org-cycle-global-status 'contents))
3844 ;; We just showed the table of contents - now show everything
3846 (message "SHOW ALL")
3847 (setq org-cycle-global-status 'all)
3848 (run-hook-with-args 'org-cycle-hook 'all))
3851 ;; Default action: go to overview
3853 (message "OVERVIEW")
3854 (setq org-cycle-global-status 'overview)
3855 (run-hook-with-args 'org-cycle-hook 'overview))))
3857 ((and org-drawers org-drawer-regexp
3859 (beginning-of-line 1)
3860 (looking-at org-drawer-regexp)))
3861 ;; Toggle block visibility
3863 (not (get-char-property (match-end 0) 'invisible))))
3866 ;; Show-subtree, ARG levels up from here.
3868 (org-back-to-heading)
3869 (outline-up-heading (if (< arg 0) (- arg)
3870 (- (funcall outline-level) arg)))
3871 (org-show-subtree)))
3873 ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
3874 (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
3875 ;; At a heading: rotate between three different views
3876 (org-back-to-heading)
3877 (let ((goal-column 0) eoh eol eos)
3878 ;; First, some boundaries
3880 (org-back-to-heading)
3882 (beginning-of-line 2)
3883 (while (and (not (eobp)) ;; this is like `next-line'
3884 (get-char-property (1- (point)) 'invisible))
3885 (beginning-of-line 2)) (setq eol (point)))
3886 (outline-end-of-heading) (setq eoh (point))
3887 (org-end-of-subtree t)
3889 (skip-chars-forward " \t\n")
3890 (beginning-of-line 1) ; in case this is an item
3892 (setq eos (1- (point))))
3893 ;; Find out what to do next and set `this-command'
3896 ;; Nothing is hidden behind this heading
3897 (message "EMPTY ENTRY")
3898 (setq org-cycle-subtree-status nil)
3901 (outline-next-heading)
3902 (if (org-invisible-p) (org-flag-heading nil))))
3904 (not (string-match "\\S-" (buffer-substring eol eos))))
3905 ;; Entire subtree is hidden in one line: open it
3908 (message "CHILDREN")
3911 (outline-next-heading)
3912 (if (org-invisible-p) (org-flag-heading nil)))
3913 (setq org-cycle-subtree-status 'children)
3914 (run-hook-with-args 'org-cycle-hook 'children))
3915 ((and (eq last-command this-command)
3916 (eq org-cycle-subtree-status 'children))
3917 ;; We just showed the children, now show everything.
3920 (setq org-cycle-subtree-status 'subtree)
3921 (run-hook-with-args 'org-cycle-hook 'subtree))
3923 ;; Default action: hide the subtree.
3926 (setq org-cycle-subtree-status 'folded)
3927 (run-hook-with-args 'org-cycle-hook 'folded)))))
3930 (buffer-read-only (org-back-to-heading))
3932 ((org-try-cdlatex-tab))
3934 ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
3936 (not (looking-at outline-regexp))))
3937 (call-interactively (global-key-binding "\t")))
3939 ((if (and (memq org-cycle-emulate-tab '(white whitestart))
3940 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
3941 (or (and (eq org-cycle-emulate-tab 'white)
3942 (= (match-end 0) (point-at-eol)))
3943 (and (eq org-cycle-emulate-tab 'whitestart)
3944 (>= (match-end 0) pos))))
3946 (eq org-cycle-emulate-tab t))
3947 (call-interactively (global-key-binding "\t")))
3950 (org-back-to-heading)
3954 (defun org-global-cycle (&optional arg)
3955 "Cycle the global visibility. For details see `org-cycle'.
3956 With C-u prefix arg, switch to startup visibility.
3957 With a numeric prefix, show all headlines up to that level."
3959 (let ((org-cycle-include-plain-lists
3960 (if (org-mode-p) org-cycle-include-plain-lists nil)))
3964 (hide-sublevels arg)
3965 (setq org-cycle-global-status 'contents))
3967 (org-set-startup-visibility)
3968 (message "Startup visibility, plus VISIBILITY properties."))
3970 (org-cycle '(4))))))
3972 (defun org-set-startup-visibility ()
3973 "Set the visibility required by startup options and properties."
3975 ((eq org-startup-folded t)
3977 ((eq org-startup-folded 'content)
3978 (let ((this-command 'org-cycle) (last-command 'org-cycle))
3979 (org-cycle '(4)) (org-cycle '(4)))))
3980 (org-set-visibility-according-to-property 'no-cleanup)
3981 (org-cycle-hide-archived-subtrees 'all)
3982 (org-cycle-hide-drawers 'all)
3983 (org-cycle-show-empty-lines 'all))
3985 (defun org-set-visibility-according-to-property (&optional no-cleanup)
3986 "Switch subtree visibilities according to :VISIBILITY: property."
3990 (goto-char (point-min))
3991 (while (re-search-forward
3992 "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
3994 (setq state (match-string 1))
3996 (org-back-to-heading t)
4000 ((equal state '("fold" "folded"))
4002 ((equal state "children")
4003 (org-show-hidden-entry)
4005 ((equal state "content")
4008 (org-narrow-to-subtree)
4010 ((member state '("all" "showall"))
4013 (org-cycle-hide-archived-subtrees 'all)
4014 (org-cycle-hide-drawers 'all)
4015 (org-cycle-show-empty-lines 'all)))))
4017 (defun org-overview ()
4018 "Switch to overview mode, shoing only top-level headlines.
4019 Really, this shows all headlines with level equal or greater than the level
4020 of the first headline in the buffer. This is important, because if the
4021 first headline is not level one, then (hide-sublevels 1) gives confusing
4024 (let ((level (save-excursion
4025 (goto-char (point-min))
4026 (if (re-search-forward (concat "^" outline-regexp) nil t)
4028 (goto-char (match-beginning 0))
4029 (funcall outline-level))))))
4030 (and level (hide-sublevels level))))
4032 (defun org-content (&optional arg)
4033 "Show all headlines in the buffer, like a table of contents.
4034 With numerical argument N, show content up to level N."
4037 ;; Visit all headings and show their offspring
4038 (and (integerp arg) (org-overview))
4039 (goto-char (point-max))
4041 (while (and (progn (condition-case nil
4042 (outline-previous-visible-heading 1)
4043 (error (goto-char (point-min))))
4045 (looking-at outline-regexp))
4047 (show-children (1- arg))
4049 (if (bobp) (throw 'exit nil))))))
4052 (defun org-optimize-window-after-visibility-change (state)
4053 "Adjust the window after a change in outline visibility.
4054 This function is the default value of the hook `org-cycle-hook'."
4055 (when (get-buffer-window (current-buffer))
4057 ; ((eq state 'overview) (org-first-headline-recenter 1))
4058 ; ((eq state 'overview) (org-beginning-of-line))
4059 ((eq state 'content) nil)
4060 ((eq state 'all) nil)
4061 ((eq state 'folded) nil)
4062 ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
4063 ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
4065 (defun org-compact-display-after-subtree-move ()
4068 (if (org-up-heading-safe)
4073 (org-cycle-show-empty-lines 'children)
4074 (org-cycle-hide-drawers 'children))
4077 (defun org-cycle-show-empty-lines (state)
4078 "Show empty lines above all visible headlines.
4079 The region to be covered depends on STATE when called through
4080 `org-cycle-hook'. Lisp program can use t for STATE to get the
4081 entire buffer covered. Note that an empty line is only shown if there
4082 are at least `org-cycle-separator-lines' empty lines before the headeline."
4083 (when (> org-cycle-separator-lines 0)
4085 (let* ((n org-cycle-separator-lines)
4087 ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
4088 ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
4089 (t (let ((ns (number-to-string (- n 2))))
4090 (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
4091 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
4094 ((memq state '(overview contents t))
4095 (setq beg (point-min) end (point-max)))
4096 ((memq state '(children folded))
4097 (setq beg (point) end (progn (org-end-of-subtree t t)
4098 (beginning-of-line 2)
4102 (while (re-search-forward re end t)
4103 (if (not (get-char-property (match-end 1) 'invisible))
4104 (outline-flag-region
4105 (match-beginning 1) (match-end 1) nil)))))))
4106 ;; Never hide empty lines at the end of the file.
4108 (goto-char (point-max))
4109 (outline-previous-heading)
4110 (outline-end-of-heading)
4111 (if (and (looking-at "[ \t\n]+")
4112 (= (match-end 0) (point-max)))
4113 (outline-flag-region (point) (match-end 0) nil))))
4115 (defun org-cycle-hide-drawers (state)
4116 "Re-hide all drawers after a visibility state change."
4117 (when (and (org-mode-p)
4118 (not (memq state '(overview folded))))
4120 (let* ((globalp (memq state '(contents all)))
4121 (beg (if globalp (point-min) (point)))
4122 (end (if globalp (point-max) (org-end-of-subtree t))))
4124 (while (re-search-forward org-drawer-regexp end t)
4125 (org-flag-drawer t))))))
4127 (defun org-flag-drawer (flag)
4129 (beginning-of-line 1)
4130 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
4131 (let ((b (match-end 0))
4132 (outline-regexp org-outline-regexp))
4133 (if (re-search-forward
4135 (save-excursion (outline-next-heading) (point)) t)
4136 (outline-flag-region b (point-at-eol) flag)
4137 (error ":END: line missing"))))))
4139 (defun org-subtree-end-visible-p ()
4140 "Is the end of the current subtree visible?"
4141 (pos-visible-in-window-p
4142 (save-excursion (org-end-of-subtree t) (point))))
4144 (defun org-first-headline-recenter (&optional N)
4145 "Move cursor to the first headline and recenter the headline.
4146 Optional argument N means, put the headline into the Nth line of the window."
4147 (goto-char (point-min))
4148 (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
4150 (recenter (prefix-numeric-value N))))
4154 (defvar org-goto-window-configuration nil)
4155 (defvar org-goto-marker nil)
4156 (defvar org-goto-map
4157 (let ((map (make-sparse-keymap)))
4158 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
4159 (while (setq cmd (pop cmds))
4160 (substitute-key-definition cmd cmd map global-map)))
4161 (suppress-keymap map)
4162 (org-defkey map "\C-m" 'org-goto-ret)
4163 (org-defkey map [(return)] 'org-goto-ret)
4164 (org-defkey map [(left)] 'org-goto-left)
4165 (org-defkey map [(right)] 'org-goto-right)
4166 (org-defkey map [(control ?g)] 'org-goto-quit)
4167 (org-defkey map "\C-i" 'org-cycle)
4168 (org-defkey map [(tab)] 'org-cycle)
4169 (org-defkey map [(down)] 'outline-next-visible-heading)
4170 (org-defkey map [(up)] 'outline-previous-visible-heading)
4171 (if org-goto-auto-isearch
4172 (if (fboundp 'define-key-after)
4173 (define-key-after map [t] 'org-goto-local-auto-isearch)
4175 (org-defkey map "q" 'org-goto-quit)
4176 (org-defkey map "n" 'outline-next-visible-heading)
4177 (org-defkey map "p" 'outline-previous-visible-heading)
4178 (org-defkey map "f" 'outline-forward-same-level)
4179 (org-defkey map "b" 'outline-backward-same-level)
4180 (org-defkey map "u" 'outline-up-heading))
4181 (org-defkey map "/" 'org-occur)
4182 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
4183 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
4184 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
4185 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
4186 (org-defkey map "\C-c\C-u" 'outline-up-heading)
4189 (defconst org-goto-help
4190 "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
4191 RET=jump to location [Q]uit and return to previous location
4192 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
4194 (defvar org-goto-start-pos) ; dynamically scoped parameter
4196 (defun org-goto (&optional alternative-interface)
4197 "Look up a different location in the current file, keeping current visibility.
4199 When you want look-up or go to a different location in a document, the
4200 fastest way is often to fold the entire buffer and then dive into the tree.
4201 This method has the disadvantage, that the previous location will be folded,
4202 which may not be what you want.
4204 This command works around this by showing a copy of the current buffer
4205 in an indirect buffer, in overview mode. You can dive into the tree in
4206 that copy, use org-occur and incremental search to find a location.
4207 When pressing RET or `Q', the command returns to the original buffer in
4208 which the visibility is still unchanged. After RET is will also jump to
4209 the location selected in the indirect buffer and expose the
4210 the headline hierarchy above."
4212 (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
4213 (org-refile-use-outline-path t)
4215 (if (not alternative-interface)
4217 (if (eq org-goto-interface 'outline)
4218 'outline-path-completion
4220 (org-goto-start-pos (point))
4222 (if (eq interface 'outline)
4223 (car (org-get-location (current-buffer) org-goto-help))
4224 (nth 3 (org-refile-get-location "Goto: ")))))
4227 (org-mark-ring-push org-goto-start-pos)
4228 (goto-char selected-point)
4229 (if (or (org-invisible-p) (org-invisible-p2))
4230 (org-show-context 'org-goto)))
4233 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
4234 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
4235 (defvar org-goto-local-auto-isearch-map) ; defined below
4237 (defun org-get-location (buf help)
4238 "Let the user select a location in the Org-mode buffer BUF.
4239 This function uses a recursive edit. It returns the selected position
4241 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
4242 (isearch-hide-immediately nil)
4243 (isearch-search-fun-function
4244 (lambda () 'org-goto-local-search-forward-headings))
4245 (org-goto-selected-point org-goto-exit-command))
4247 (save-window-excursion
4248 (delete-other-windows)
4249 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
4252 (make-indirect-buffer (current-buffer) "*org-goto*")
4253 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
4254 (with-output-to-temp-buffer "*Help*"
4256 (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
4257 (setq buffer-read-only nil)
4258 (let ((org-startup-truncated t)
4259 (org-startup-folded nil)
4260 (org-startup-align-all-tables nil))
4263 (setq buffer-read-only t)
4264 (if (and (boundp 'org-goto-start-pos)
4265 (integer-or-marker-p org-goto-start-pos))
4266 (let ((org-show-hierarchy-above t)
4267 (org-show-siblings t)
4268 (org-show-following-heading t))
4269 (goto-char org-goto-start-pos)
4270 (and (org-invisible-p) (org-show-context)))
4271 (goto-char (point-min)))
4272 (org-beginning-of-line)
4273 (message "Select location and press RET")
4274 (use-local-map org-goto-map)
4277 (kill-buffer "*org-goto*")
4278 (cons org-goto-selected-point org-goto-exit-command)))
4280 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
4281 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
4282 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
4283 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
4285 (defun org-goto-local-search-forward-headings (string bound noerror)
4286 "Search and make sure that anu matches are in headlines."
4288 (while (search-forward string bound noerror)
4289 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
4290 (and (member :headline context)
4291 (not (member :tags context))))
4292 (throw 'return (point))))))
4294 (defun org-goto-local-auto-isearch ()
4297 (goto-char (point-min))
4298 (let ((keys (this-command-keys)))
4299 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
4301 (isearch-process-search-char (string-to-char keys)))))
4303 (defun org-goto-ret (&optional arg)
4304 "Finish `org-goto' by going to the new location."
4306 (setq org-goto-selected-point (point)
4307 org-goto-exit-command 'return)
4310 (defun org-goto-left ()
4311 "Finish `org-goto' by going to the new location."
4313 (if (org-on-heading-p)
4315 (beginning-of-line 1)
4316 (setq org-goto-selected-point (point)
4317 org-goto-exit-command 'left)
4319 (error "Not on a heading")))
4321 (defun org-goto-right ()
4322 "Finish `org-goto' by going to the new location."
4324 (if (org-on-heading-p)
4326 (setq org-goto-selected-point (point)
4327 org-goto-exit-command 'right)
4329 (error "Not on a heading")))
4331 (defun org-goto-quit ()
4332 "Finish `org-goto' without cursor motion."
4334 (setq org-goto-selected-point nil)
4335 (setq org-goto-exit-command 'quit)
4338 ;;; Indirect buffer display of subtrees
4340 (defvar org-indirect-dedicated-frame nil
4341 "This is the frame being used for indirect tree display.")
4342 (defvar org-last-indirect-buffer nil)
4344 (defun org-tree-to-indirect-buffer (&optional arg)
4345 "Create indirect buffer and narrow it to current subtree.
4346 With numerical prefix ARG, go up to this level and then take that tree.
4347 If ARG is negative, go up that many levels.
4348 If `org-indirect-buffer-display' is not `new-frame', the command removes the
4349 indirect buffer previously made with this command, to avoid proliferation of
4350 indirect buffers. However, when you call the command with a `C-u' prefix, or
4351 when `org-indirect-buffer-display' is `new-frame', the last buffer
4352 is kept so that you can work with several indirect buffers at the same time.
4353 If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
4354 requests that a new frame be made for the new buffer, so that the dedicated
4355 frame is not changed."
4357 (let ((cbuf (current-buffer))
4358 (cwin (selected-window))
4360 beg end level heading ibuf)
4362 (org-back-to-heading t)
4364 (setq level (org-outline-level))
4365 (if (< arg 0) (setq arg (+ level arg)))
4366 (while (> (setq level (org-outline-level)) arg)
4367 (outline-up-heading 1 t)))
4369 heading (org-get-heading))
4370 (org-end-of-subtree t) (setq end (point)))
4371 (if (and (buffer-live-p org-last-indirect-buffer)
4372 (not (eq org-indirect-buffer-display 'new-frame))
4374 (kill-buffer org-last-indirect-buffer))
4375 (setq ibuf (org-get-indirect-buffer cbuf)
4376 org-last-indirect-buffer ibuf)
4378 ((or (eq org-indirect-buffer-display 'new-frame)
4379 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
4380 (select-frame (make-frame))
4381 (delete-other-windows)
4382 (switch-to-buffer ibuf)
4383 (org-set-frame-title heading))
4384 ((eq org-indirect-buffer-display 'dedicated-frame)
4386 (select-frame (or (and org-indirect-dedicated-frame
4387 (frame-live-p org-indirect-dedicated-frame)
4388 org-indirect-dedicated-frame)
4389 (setq org-indirect-dedicated-frame (make-frame)))))
4390 (delete-other-windows)
4391 (switch-to-buffer ibuf)
4392 (org-set-frame-title (concat "Indirect: " heading)))
4393 ((eq org-indirect-buffer-display 'current-window)
4394 (switch-to-buffer ibuf))
4395 ((eq org-indirect-buffer-display 'other-window)
4396 (pop-to-buffer ibuf))
4397 (t (error "Invalid value.")))
4398 (if (featurep 'xemacs)
4399 (save-excursion (org-mode) (turn-on-font-lock)))
4400 (narrow-to-region beg end)
4403 (and (window-live-p cwin) (select-window cwin))))
4405 (defun org-get-indirect-buffer (&optional buffer)
4406 (setq buffer (or buffer (current-buffer)))
4407 (let ((n 1) (base (buffer-name buffer)) bname)
4408 (while (buffer-live-p
4409 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
4412 (make-indirect-buffer buffer bname 'clone)
4413 (error (make-indirect-buffer buffer bname)))))
4415 (defun org-set-frame-title (title)
4416 "Set the title of the current frame to the string TITLE."
4417 ;; FIXME: how to name a single frame in XEmacs???
4418 (unless (featurep 'xemacs)
4419 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
4421 ;;;; Structure editing
4423 ;;; Inserting headlines
4425 (defun org-insert-heading (&optional force-heading)
4426 "Insert a new heading or item with same depth at point.
4427 If point is in a plain list and FORCE-HEADING is nil, create a new list item.
4428 If point is at the beginning of a headline, insert a sibling before the
4429 current headline. If point is not at the beginning, do not split the line,
4430 but create the new hedline after the current line."
4432 (if (= (buffer-size) 0)
4434 (when (or force-heading (not (org-insert-item)))
4435 (let* ((head (save-excursion
4438 (org-back-to-heading)
4441 (blank (cdr (assq 'heading org-blank-before-new-entry)))
4444 ((and (org-on-heading-p) (bolp)
4446 (save-excursion (backward-char 1) (not (org-invisible-p)))))
4447 ;; insert before the current line
4448 (open-line (if blank 2 1)))
4452 (backward-char 1) (not (org-invisible-p)))))
4453 ;; insert right here
4456 ;; in the middle of the line
4459 (org-get-alist-option org-M-RET-may-split-line 'headline))
4461 (if (org-on-heading-p)
4463 (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
4464 (setq tags (and (match-end 2) (match-string 2)))
4466 (delete-region (match-beginning 1) (match-end 1)))
4467 (setq pos (point-at-bol))
4468 (or split (end-of-line 1))
4469 (delete-horizontal-space)
4470 (newline (if blank 2 1))
4476 (org-set-tags nil 'align))))
4477 (or split (end-of-line 1))
4478 (newline (if blank 2 1))))))
4479 (insert head) (just-one-space)
4482 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
4483 (run-hooks 'org-insert-heading-hook)))))
4485 (defun org-get-heading (&optional no-tags)
4486 "Return the heading of the current entry, without the stars."
4488 (org-back-to-heading t)
4491 (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
4492 "\\*+[ \t]+\\([^\r\n]*\\)"))
4493 (match-string 1) "")))
4495 (defun org-insert-heading-after-current ()
4496 "Insert a new heading with same level as current, after current subtree."
4498 (org-back-to-heading)
4499 (org-insert-heading)
4500 (org-move-subtree-down)
4503 (defun org-insert-todo-heading (arg)
4504 "Insert a new heading with the same level and TODO state as current heading.
4505 If the heading has no TODO state, or if the state is DONE, use the first
4506 state (TODO by default). Also with prefix arg, force first state."
4508 (when (not (org-insert-item 'checkbox))
4509 (org-insert-heading)
4511 (org-back-to-heading)
4512 (outline-previous-heading)
4513 (looking-at org-todo-line-regexp))
4515 (not (match-beginning 2))
4516 (member (match-string 2) org-done-keywords))
4517 (insert (car org-todo-keywords-1) " ")
4518 (insert (match-string 2) " "))))
4520 (defun org-insert-subheading (arg)
4521 "Insert a new subheading and demote it.
4522 Works for outline headings and for plain lists alike."
4524 (org-insert-heading arg)
4526 ((org-on-heading-p) (org-do-demote))
4527 ((org-at-item-p) (org-indent-item 1))))
4529 (defun org-insert-todo-subheading (arg)
4530 "Insert a new subheading with TODO keyword or checkbox and demote it.
4531 Works for outline headings and for plain lists alike."
4533 (org-insert-todo-heading arg)
4535 ((org-on-heading-p) (org-do-demote))
4536 ((org-at-item-p) (org-indent-item 1))))
4538 ;;; Promotion and Demotion
4540 (defun org-promote-subtree ()
4541 "Promote the entire subtree.
4542 See also `org-promote'."
4545 (org-map-tree 'org-promote))
4546 (org-fix-position-after-promote))
4548 (defun org-demote-subtree ()
4549 "Demote the entire subtree. See `org-demote'.
4550 See also `org-promote'."
4553 (org-map-tree 'org-demote))
4554 (org-fix-position-after-promote))
4557 (defun org-do-promote ()
4558 "Promote the current heading higher up the tree.
4559 If the region is active in `transient-mark-mode', promote all headings
4563 (if (org-region-active-p)
4564 (org-map-region 'org-promote (region-beginning) (region-end))
4566 (org-fix-position-after-promote))
4568 (defun org-do-demote ()
4569 "Demote the current heading lower down the tree.
4570 If the region is active in `transient-mark-mode', demote all headings
4574 (if (org-region-active-p)
4575 (org-map-region 'org-demote (region-beginning) (region-end))
4577 (org-fix-position-after-promote))
4579 (defun org-fix-position-after-promote ()
4580 "Make sure that after pro/demotion cursor position is right."
4581 (let ((pos (point)))
4582 (when (save-excursion
4583 (beginning-of-line 1)
4584 (looking-at org-todo-line-regexp)
4585 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
4586 (cond ((eobp) (insert " "))
4587 ((eolp) (insert " "))
4588 ((equal (char-after) ?\ ) (forward-char 1))))))
4590 (defun org-reduced-level (l)
4591 (if org-odd-levels-only (1+ (floor (/ l 2))) l))
4593 (defun org-get-valid-level (level &optional change)
4594 "Rectify a level change under the influence of `org-odd-levels-only'
4595 LEVEL is a current level, CHANGE is by how much the level should be
4596 modified. Even if CHANGE is nil, LEVEL may be returned modified because
4597 even level numbers will become the next higher odd number."
4598 (if org-odd-levels-only
4599 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
4600 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
4601 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
4602 (max 1 (+ level change))))
4604 (if (boundp 'define-obsolete-function-alias)
4605 (if (or (featurep 'xemacs) (< emacs-major-version 23))
4606 (define-obsolete-function-alias 'org-get-legal-level
4607 'org-get-valid-level)
4608 (define-obsolete-function-alias 'org-get-legal-level
4609 'org-get-valid-level "23.1")))
4611 (defun org-promote ()
4612 "Promote the current heading higher up the tree.
4613 If the region is active in `transient-mark-mode', promote all headings
4615 (org-back-to-heading t)
4616 (let* ((level (save-match-data (funcall outline-level)))
4617 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
4618 (diff (abs (- level (length up-head) -1))))
4619 (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
4620 (replace-match up-head nil t)
4621 ;; Fixup tag positioning
4622 (and org-auto-align-tags (org-set-tags nil t))
4623 (if org-adapt-indentation (org-fixup-indentation (- diff)))))
4625 (defun org-demote ()
4626 "Demote the current heading lower down the tree.
4627 If the region is active in `transient-mark-mode', demote all headings
4629 (org-back-to-heading t)
4630 (let* ((level (save-match-data (funcall outline-level)))
4631 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
4632 (diff (abs (- level (length down-head) -1))))
4633 (replace-match down-head nil t)
4634 ;; Fixup tag positioning
4635 (and org-auto-align-tags (org-set-tags nil t))
4636 (if org-adapt-indentation (org-fixup-indentation diff))))
4638 (defun org-map-tree (fun)
4639 "Call FUN for every heading underneath the current one."
4640 (org-back-to-heading)
4641 (let ((level (funcall outline-level)))
4645 (outline-next-heading)
4646 (> (funcall outline-level) level))
4650 (defun org-map-region (fun beg end)
4651 "Call FUN for every heading between BEG and END."
4652 (let ((org-ignore-region t))
4654 (setq end (copy-marker end))
4656 (if (and (re-search-forward (concat "^" outline-regexp) nil t)
4660 (outline-next-heading)
4665 (defun org-fixup-indentation (diff)
4666 "Change the indentation in the current entry by DIFF
4667 However, if any line in the current entry has no indentation, or if it
4668 would end up with no indentation after the change, nothing at all is done."
4670 (let ((end (save-excursion (outline-next-heading)
4672 (prohibit (if (> diff 0)
4674 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
4676 (unless (save-excursion (end-of-line 1)
4677 (re-search-forward prohibit end t))
4678 (while (and (< (point) end)
4679 (re-search-forward "^[ \t]+" end t))
4680 (goto-char (match-end 0))
4681 (setq col (current-column))
4682 (if (< diff 0) (replace-match ""))
4683 (indent-to (+ diff col))))
4684 (move-marker end nil))))
4686 (defun org-convert-to-odd-levels ()
4687 "Convert an org-mode file with all levels allowed to one with odd levels.
4688 This will leave level 1 alone, convert level 2 to level 3, level 3 to
4691 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
4692 (let ((org-odd-levels-only nil) n)
4694 (goto-char (point-min))
4695 (while (re-search-forward "^\\*\\*+ " nil t)
4696 (setq n (- (length (match-string 0)) 2))
4697 (while (>= (setq n (1- n)) 0)
4699 (end-of-line 1))))))
4702 (defun org-convert-to-oddeven-levels ()
4703 "Convert an org-mode file with only odd levels to one with odd and even levels.
4704 This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
4705 section with an even level, conversion would destroy the structure of the file. An error
4706 is signaled in this case."
4708 (goto-char (point-min))
4709 ;; First check if there are no even levels
4710 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
4711 (org-show-context t)
4712 (error "Not all levels are odd in this file. Conversion not possible."))
4713 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
4714 (let ((org-odd-levels-only nil) n)
4716 (goto-char (point-min))
4717 (while (re-search-forward "^\\*\\*+ " nil t)
4718 (setq n (/ (1- (length (match-string 0))) 2))
4719 (while (>= (setq n (1- n)) 0)
4721 (end-of-line 1))))))
4723 (defun org-tr-level (n)
4724 "Make N odd if required."
4725 (if org-odd-levels-only (1+ (/ n 2)) n))
4727 ;;; Vertical tree motion, cutting and pasting of subtrees
4729 (defun org-move-subtree-up (&optional arg)
4730 "Move the current subtree up past ARG headlines of the same level."
4732 (org-move-subtree-down (- (prefix-numeric-value arg))))
4734 (defun org-move-subtree-down (&optional arg)
4735 "Move the current subtree down past ARG headlines of the same level."
4737 (setq arg (prefix-numeric-value arg))
4738 (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
4739 'outline-get-last-sibling))
4740 (ins-point (make-marker))
4742 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
4744 (org-back-to-heading)
4747 (setq ne-beg (org-back-over-empty-lines))
4750 (save-excursion (outline-end-of-heading)
4751 (setq folded (org-invisible-p)))
4752 (outline-end-of-subtree))
4753 (outline-next-heading)
4754 (setq ne-end (org-back-over-empty-lines))
4757 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
4758 ;; include less whitespace
4761 (forward-line (- ne-beg ne-end))
4762 (setq beg (point))))
4763 ;; Find insertion point, with error handling
4765 (or (and (funcall movfunc) (looking-at outline-regexp))
4766 (progn (goto-char beg0)
4767 (error "Cannot move past superior level or buffer limit")))
4768 (setq cnt (1- cnt)))
4770 ;; Moving forward - still need to move over subtree
4771 (progn (org-end-of-subtree t t)
4773 (org-back-over-empty-lines)
4774 (or (bolp) (newline)))))
4775 (setq ne-ins (org-back-over-empty-lines))
4776 (move-marker ins-point (point))
4777 (setq txt (buffer-substring beg end))
4778 (org-save-markers-in-region beg end)
4779 (delete-region beg end)
4780 (outline-flag-region (1- beg) beg nil)
4781 (outline-flag-region (1- (point)) (point) nil)
4782 (let ((bbb (point)))
4783 (insert-before-markers txt)
4784 (org-reinstall-markers-in-region bbb)
4785 (move-marker ins-point bbb))
4786 (or (bolp) (insert "\n"))
4787 (setq ins-end (point))
4788 (goto-char ins-point)
4789 (org-skip-whitespace)
4790 (when (and (< arg 0)
4791 (org-first-sibling-p)
4793 ;; Move whitespace back to beginning
4796 (let ((kill-whole-line t))
4797 (kill-line (- ne-ins ne-beg)) (point)))
4798 (insert (make-string (- ne-ins ne-beg) ?\n)))
4799 (move-marker ins-point nil)
4800 (org-compact-display-after-subtree-move)
4804 (org-cycle-hide-drawers 'children))))
4806 (defvar org-subtree-clip ""
4807 "Clipboard for cut and paste of subtrees.
4808 This is actually only a copy of the kill, because we use the normal kill
4809 ring. We need it to check if the kill was created by `org-copy-subtree'.")
4811 (defvar org-subtree-clip-folded nil
4812 "Was the last copied subtree folded?
4813 This is used to fold the tree back after pasting.")
4815 (defun org-cut-subtree (&optional n)
4816 "Cut the current subtree into the clipboard.
4817 With prefix arg N, cut this many sequential subtrees.
4818 This is a short-hand for marking the subtree and then cutting it."
4820 (org-copy-subtree n 'cut))
4822 (defun org-copy-subtree (&optional n cut force-store-markers)
4823 "Cut the current subtree into the clipboard.
4824 With prefix arg N, cut this many sequential subtrees.
4825 This is a short-hand for marking the subtree and then copying it.
4826 If CUT is non-nil, actually cut the subtree.
4827 If FORCE-STORE-MARKERS is non-nil, store the relative locations
4828 of some markers in the region, even if CUT is non-nil. This is
4829 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
4831 (let (beg end folded (beg0 (point)))
4833 (org-back-to-heading nil) ; take what looks like a subtree
4834 (org-back-to-heading t)) ; take what is really there
4835 (org-back-over-empty-lines)
4837 (skip-chars-forward " \t\r\n")
4839 (save-excursion (outline-end-of-heading)
4840 (setq folded (org-invisible-p)))
4842 (outline-forward-same-level (1- n))
4844 (org-end-of-subtree t t))
4845 (org-back-over-empty-lines)
4849 (setq org-subtree-clip-folded folded)
4850 (when (or cut force-store-markers)
4851 (org-save-markers-in-region beg end))
4852 (if cut (kill-region beg end) (copy-region-as-kill beg end))
4853 (setq org-subtree-clip (current-kill 0))
4854 (message "%s: Subtree(s) with %d characters"
4855 (if cut "Cut" "Copied")
4856 (length org-subtree-clip)))))
4858 (defun org-paste-subtree (&optional level tree)
4859 "Paste the clipboard as a subtree, with modification of headline level.
4860 The entire subtree is promoted or demoted in order to match a new headline
4861 level. By default, the new level is derived from the visible headings
4862 before and after the insertion point, and taken to be the inferior headline
4863 level of the two. So if the previous visible heading is level 3 and the
4864 next is level 4 (or vice versa), level 4 will be used for insertion.
4865 This makes sure that the subtree remains an independent subtree and does
4866 not swallow low level entries.
4868 You can also force a different level, either by using a numeric prefix
4869 argument, or by inserting the heading marker by hand. For example, if the
4870 cursor is after \"*****\", then the tree will be shifted to level 5.
4872 If you want to insert the tree as is, just use \\[yank].
4874 If optional TREE is given, use this text instead of the kill ring."
4876 (unless (org-kill-is-subtree-p tree)
4878 (substitute-command-keys
4879 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
4880 (let* ((txt (or tree (and kill-ring (current-kill 0))))
4881 (^re (concat "^\\(" outline-regexp "\\)"))
4882 (re (concat "\\(" outline-regexp "\\)"))
4883 (^re_ (concat "\\(\\*+\\)[ \t]*"))
4885 (old-level (if (string-match ^re txt)
4886 (- (match-end 0) (match-beginning 0) 1)
4888 (force-level (cond (level (prefix-numeric-value level))
4890 ^re_ (buffer-substring (point-at-bol) (point)))
4891 (- (match-end 1) (match-beginning 1)))
4893 (previous-level (save-excursion
4896 (outline-previous-visible-heading 1)
4898 (- (match-end 0) (match-beginning 0) 1)
4901 (next-level (save-excursion
4904 (or (looking-at outline-regexp)
4905 (outline-next-visible-heading 1))
4907 (- (match-end 0) (match-beginning 0) 1)
4910 (new-level (or force-level (max previous-level next-level)))
4911 (shift (if (or (= old-level -1)
4913 (= old-level new-level))
4915 (- new-level old-level)))
4916 (delta (if (> shift 0) -1 1))
4917 (func (if (> shift 0) 'org-demote 'org-promote))
4918 (org-odd-levels-only nil)
4920 ;; Remove the forced level indicator
4922 (delete-region (point-at-bol) (point)))
4924 (beginning-of-line 1)
4925 (org-back-over-empty-lines)
4927 (insert-before-markers txt)
4928 (unless (string-match "\n\\'" txt) (insert "\n"))
4929 (org-reinstall-markers-in-region beg)
4932 (skip-chars-forward " \t\n\r")
4934 ;; Shift if necessary
4937 (narrow-to-region beg end)
4938 (while (not (= shift 0))
4939 (org-map-region func (point-min) (point-max))
4940 (setq shift (+ delta shift)))
4941 (goto-char (point-min))))
4942 (when (interactive-p)
4943 (message "Clipboard pasted as level %d subtree" new-level))
4945 (eq org-subtree-clip (current-kill 0))
4946 org-subtree-clip-folded)
4947 ;; The tree was folded before it was killed/copied
4950 (defun org-kill-is-subtree-p (&optional txt)
4951 "Check if the current kill is an outline subtree, or a set of trees.
4952 Returns nil if kill does not start with a headline, or if the first
4953 headline level is not the largest headline level in the tree.
4954 So this will actually accept several entries of equal levels as well,
4955 which is OK for `org-paste-subtree'.
4956 If optional TXT is given, check this string instead of the current kill."
4957 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
4958 (start-level (and kill
4959 (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
4960 org-outline-regexp "\\)")
4962 (- (match-end 2) (match-beginning 2) 1)))
4963 (re (concat "^" org-outline-regexp))
4964 (start (1+ (match-beginning 2))))
4965 (if (not start-level)
4967 nil) ;; does not even start with a heading
4969 (while (setq start (string-match re kill (1+ start)))
4970 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
4974 (defvar org-markers-to-move nil
4975 "Markers that should be moved with a cut-and-paste operation.
4976 Those markers are stored together with their positions relative to
4977 the start of the region.")
4979 (defun org-save-markers-in-region (beg end)
4980 "Check markers in region.
4981 If these markers are between BEG and END, record their position relative
4982 to BEG, so that after moving the block of text, we can put the markers back
4984 This function gets called just before an entry or tree gets cut from the
4985 buffer. After re-insertion, `org-reinstall-markers-in-region' must be
4986 called immediately, to move the markers with the entries."
4987 (setq org-markers-to-move nil)
4988 (when (featurep 'org-clock)
4989 (org-clock-save-markers-for-cut-and-paste beg end))
4990 (when (featurep 'org-agenda)
4991 (org-agenda-save-markers-for-cut-and-paste beg end)))
4993 (defun org-check-and-save-marker (marker beg end)
4994 "Check if MARKER is between BEG and END.
4995 If yes, remember the marker and the distance to BEG."
4996 (when (and (marker-buffer marker)
4997 (equal (marker-buffer marker) (current-buffer)))
4998 (if (and (>= marker beg) (< marker end))
4999 (push (cons marker (- marker beg)) org-markers-to-move))))
5001 (defun org-reinstall-markers-in-region (beg)
5002 "Move all remembered markers to their position relative to BEG."
5004 (move-marker (car x) (+ beg (cdr x))))
5005 org-markers-to-move)
5006 (setq org-markers-to-move nil))
5008 (defun org-narrow-to-subtree ()
5009 "Narrow buffer to the current subtree."
5014 (progn (org-back-to-heading) (point))
5015 (progn (org-end-of-subtree t t) (point))))))
5020 (defun org-sort (with-case)
5021 "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
5022 Optional argument WITH-CASE means sort case-sensitively."
5024 (if (org-at-table-p)
5025 (org-call-with-arg 'org-table-sort-lines with-case)
5026 (org-call-with-arg 'org-sort-entries-or-items with-case)))
5028 (defun org-sort-remove-invisible (s)
5029 (remove-text-properties 0 (length s) org-rm-props s)
5030 (while (string-match org-bracket-link-regexp s)
5031 (setq s (replace-match (if (match-end 2)
5033 (match-string 1 s)) t t s)))
5036 (defvar org-priority-regexp) ; defined later in the file
5038 (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
5039 "Sort entries on a certain level of an outline tree.
5040 If there is an active region, the entries in the region are sorted.
5041 Else, if the cursor is before the first entry, sort the top-level items.
5042 Else, the children of the entry at point are sorted.
5044 Sorting can be alphabetically, numerically, and by date/time as given by
5045 the first time stamp in the entry. The command prompts for the sorting
5046 type unless it has been given to the function through the SORTING-TYPE
5047 argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
5048 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
5049 called with point at the beginning of the record. It must return either
5050 a string or a number that should serve as the sorting key for that record.
5052 Comparing entries ignores case by default. However, with an optional argument
5053 WITH-CASE, the sorting considers case as well."
5055 (let ((case-func (if with-case 'identity 'downcase))
5056 start beg end stars re re2
5057 txt what tmp plain-list-p)
5058 ;; Find beginning and end of region to sort
5060 ((org-region-active-p)
5061 ;; we will sort the region
5062 (setq end (region-end)
5064 (goto-char (region-beginning))
5065 (if (not (org-on-heading-p)) (outline-next-heading))
5066 (setq start (point)))
5068 ;; we will sort this plain list
5069 (org-beginning-of-item-list) (setq start (point))
5070 (org-end-of-item-list) (setq end (point))
5072 (setq plain-list-p t
5074 ((or (org-on-heading-p)
5075 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
5076 ;; we will sort the children of the current headline
5077 (org-back-to-heading)
5079 end (progn (org-end-of-subtree t t)
5080 (org-back-over-empty-lines)
5085 (outline-next-heading))
5087 ;; we will sort the top-level entries in this file
5088 (goto-char (point-min))
5089 (or (org-on-heading-p) (outline-next-heading))
5090 (setq start (point) end (point-max) what "top-level")
5095 (if (>= beg end) (error "Nothing to sort"))
5097 (unless plain-list-p
5098 (looking-at "\\(\\*+\\)")
5099 (setq stars (match-string 1)
5100 re (concat "^" (regexp-quote stars) " +")
5101 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
5102 txt (buffer-substring beg end))
5103 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
5104 (if (and (not (equal stars "*")) (string-match re2 txt))
5105 (error "Region to sort contains a level above the first entry")))
5107 (unless sorting-type
5110 "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
5111 "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty todo[o]rder [f]unc A/N/T/P/O/F means reversed:")
5113 (setq sorting-type (read-char-exclusive))
5115 (and (= (downcase sorting-type) ?f)
5117 (completing-read "Sort using function: "
5118 obarray 'fboundp t nil nil))
5119 (setq getkey-func (intern getkey-func)))
5121 (and (= (downcase sorting-type) ?r)
5123 (completing-read "Property: "
5124 (mapcar 'list (org-buffer-property-keys t))
5127 (message "Sorting entries...")
5130 (narrow-to-region start end)
5132 (let ((dcst (downcase sorting-type))
5133 (now (current-time)))
5135 (/= dcst sorting-type)
5136 ;; This function moves to the beginning character of the "record" to
5140 (if (org-at-item-p) t (goto-char (point-max))))
5142 (if (re-search-forward re nil t)
5143 (goto-char (match-beginning 0))
5144 (goto-char (point-max)))))
5145 ;; This function moves to the last character of the "record" being
5152 (outline-forward-same-level 1)
5154 (goto-char (point-max)))))))
5156 ;; This function returns the value that gets sorted against.
5159 (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
5162 (string-to-number (buffer-substring (match-end 0)
5165 (buffer-substring (match-end 0) (point-at-eol)))
5167 (if (re-search-forward org-ts-regexp
5169 (org-time-string-to-time (match-string 0))
5174 (setq tmp (funcall getkey-func))
5175 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
5177 (error "Invalid key function `%s'" getkey-func)))
5178 (t (error "Invalid sorting type `%c'" sorting-type)))))
5182 (if (looking-at outline-regexp)
5183 (string-to-number (buffer-substring (match-end 0)
5187 (funcall case-func (buffer-substring (point-at-bol)
5190 (if (re-search-forward org-ts-regexp
5194 (org-time-string-to-time (match-string 0))
5197 (if (re-search-forward org-priority-regexp (point-at-eol) t)
5198 (string-to-char (match-string 2))
5199 org-default-priority))
5201 (or (org-entry-get nil property) ""))
5203 (if (looking-at org-complex-heading-regexp)
5204 (- 9999 (length (member (match-string 2)
5205 org-todo-keywords-1)))))
5209 (setq tmp (funcall getkey-func))
5210 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
5212 (error "Invalid key function `%s'" getkey-func)))
5213 (t (error "Invalid sorting type `%c'" sorting-type)))))
5216 ((= dcst ?a) 'string<)
5217 ((= dcst ?t) 'time-less-p)
5219 (message "Sorting entries...done")))
5221 (defun org-do-sort (table what &optional with-case sorting-type)
5222 "Sort TABLE of WHAT according to SORTING-TYPE.
5223 The user will be prompted for the SORTING-TYPE if the call to this
5224 function does not specify it. WHAT is only for the prompt, to indicate
5225 what is being sorted. The sorting key will be extracted from
5226 the car of the elements of the table.
5227 If WITH-CASE is non-nil, the sorting will be case-sensitive."
5228 (unless sorting-type
5230 "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
5232 (setq sorting-type (read-char-exclusive)))
5233 (let ((dcst (downcase sorting-type))
5234 extractfun comparefun)
5235 ;; Define the appropriate functions
5238 (setq extractfun 'string-to-number
5239 comparefun (if (= dcst sorting-type) '< '>)))
5241 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
5242 (lambda(x) (downcase (org-sort-remove-invisible x))))
5243 comparefun (if (= dcst sorting-type)
5245 (lambda (a b) (and (not (string< a b))
5246 (not (string= a b)))))))
5250 (if (string-match org-ts-regexp x)
5252 (org-time-string-to-time (match-string 0 x)))
5254 comparefun (if (= dcst sorting-type) '< '>)))
5255 (t (error "Invalid sorting type `%c'" sorting-type)))
5257 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
5259 (lambda (a b) (funcall comparefun (car a) (car b))))))
5261 ;;;; Plain list items, including checkboxes
5263 ;;; Plain list items
5265 (defun org-at-item-p ()
5266 "Is point in a line starting a hand-formatted item?"
5267 (let ((llt org-plain-list-ordered-item-terminator))
5269 (goto-char (point-at-bol))
5272 ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
5273 ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
5274 ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
5275 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
5277 (defun org-in-item-p ()
5278 "It the cursor inside a plain list item.
5279 Does not have to be the first line."
5283 (org-beginning-of-item)
5288 (defun org-insert-item (&optional checkbox)
5289 "Insert a new item at the current level.
5290 Return t when things worked, nil when we are not in an item."
5291 (when (save-excursion
5294 (org-beginning-of-item)
5296 (if (org-invisible-p) (error "Invisible item"))
5299 (let* ((bul (match-string 0))
5300 (descp (save-excursion (goto-char (match-beginning 0))
5301 (beginning-of-line 1)
5303 (looking-at "[ \t]*.*? ::"))))
5304 (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
5306 (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
5308 (if descp (setq checkbox nil))
5310 ((and (org-at-item-p) (<= (point) eow))
5311 ;; before the bullet
5312 (beginning-of-line 1)
5313 (open-line (if blank 2 1)))
5315 (beginning-of-line 1))
5317 (unless (org-get-alist-option org-M-RET-may-split-line 'item)
5319 (delete-horizontal-space))
5320 (newline (if blank 2 1))))
5322 (if checkbox "[ ]" "")
5323 (if descp (concat (if checkbox " " "")
5324 (read-string "Term: ") " :: ") ""))
5328 (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
5329 (org-maybe-renumber-ordered-list)
5330 (and checkbox (org-update-checkbox-count-maybe))
5335 (defun org-at-item-checkbox-p ()
5336 "Is point at a line starting a plain-list item with a checklet?"
5337 (and (org-at-item-p)
5339 (goto-char (match-end 0))
5340 (skip-chars-forward " \t")
5341 (looking-at "\\[[- X]\\]"))))
5343 (defun org-toggle-checkbox (&optional arg)
5344 "Toggle the checkbox in the current line."
5347 (let (beg end status (firstnew 'unknown))
5349 ((org-region-active-p)
5350 (setq beg (region-beginning) end (region-end)))
5352 (setq beg (point) end (save-excursion (outline-next-heading) (point))))
5353 ((org-at-item-checkbox-p)
5354 (let ((pos (point)))
5357 ((member (match-string 0) '("[ ]" "[-]")) "[X]")
5362 (t (error "Not at a checkbox or heading, and no active region")))
5365 (while (< (point) end)
5366 (when (org-at-item-checkbox-p)
5367 (setq status (equal (match-string 0) "[X]"))
5368 (when (eq firstnew 'unknown)
5369 (setq firstnew (not status)))
5371 (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
5372 (beginning-of-line 2)))))
5373 (org-update-checkbox-count-maybe))
5375 (defun org-update-checkbox-count-maybe ()
5376 "Update checkbox statistics unless turned off by user."
5377 (when org-provide-checkbox-statistics
5378 (org-update-checkbox-count)))
5380 (defun org-update-checkbox-count (&optional all)
5381 "Update the checkbox statistics in the current section.
5382 This will find all statistic cookies like [57%] and [6/12] and update them
5383 with the current numbers. With optional prefix argument ALL, do this for
5387 (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
5388 (beg (condition-case nil
5389 (progn (outline-back-to-heading) (point))
5390 (error (point-min))))
5391 (end (move-marker (make-marker)
5392 (progn (outline-next-heading) (point))))
5393 (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
5394 (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
5395 (re-find (concat re "\\|" re-box))
5396 beg-cookie end-cookie is-percent c-on c-off lim
5397 eline curr-ind next-ind continue-from startsearch
5401 (goto-char (point-min))
5402 (outline-next-heading)
5403 (setq beg (point) end (point-max)))
5405 ;; find each statistic cookie
5406 (while (re-search-backward re-find beg t)
5407 (setq beg-cookie (match-beginning 1)
5408 end-cookie (match-end 1)
5409 cstat (+ cstat (if end-cookie 1 0))
5410 startsearch (point-at-eol)
5411 continue-from (point-at-bol)
5412 is-percent (match-beginning 2)
5414 ((org-on-heading-p) (outline-next-heading) (point))
5415 ((org-at-item-p) (org-end-of-item) (point))
5420 ;; find first checkbox for this cookie and gather
5421 ;; statistics from all that are at this indentation level
5422 (goto-char startsearch)
5423 (if (re-search-forward re-box lim t)
5425 (org-beginning-of-item)
5426 (setq curr-ind (org-get-indentation))
5427 (setq next-ind curr-ind)
5428 (while (and (bolp) (org-at-item-p) (= curr-ind next-ind))
5429 (save-excursion (end-of-line) (setq eline (point)))
5430 (if (re-search-forward re-box eline t)
5431 (if (member (match-string 2) '("[ ]" "[-]"))
5432 (setq c-off (1+ c-off))
5433 (setq c-on (1+ c-on))
5437 (setq next-ind (org-get-indentation))
5439 (goto-char continue-from)
5442 (delete-region beg-cookie end-cookie)
5443 (goto-char beg-cookie)
5446 (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
5447 (format "[%d/%d]" c-on (+ c-on c-off)))))
5448 ;; update items checkbox if it has one
5449 (when (org-at-item-p)
5450 (org-beginning-of-item)
5451 (when (and (> (+ c-on c-off) 0)
5452 (re-search-forward re-box (point-at-eol) t))
5453 (setq beg-cookie (match-beginning 2)
5454 end-cookie (match-end 2))
5455 (delete-region beg-cookie end-cookie)
5456 (goto-char beg-cookie)
5457 (cond ((= c-off 0) (insert "[X]"))
5458 ((= c-on 0) (insert "[ ]"))
5461 (goto-char continue-from))
5462 (when (interactive-p)
5463 (message "Checkbox satistics updated %s (%d places)"
5464 (if all "in entire file" "in current outline entry") cstat)))))
5466 (defun org-get-checkbox-statistics-face ()
5467 "Select the face for checkbox statistics.
5468 The face will be `org-done' when all relevant boxes are checked. Otherwise
5469 it will be `org-todo'."
5471 (if (equal (match-string 1) "100%") 'org-done 'org-todo)
5472 (if (and (> (match-end 2) (match-beginning 2))
5473 (equal (match-string 2) (match-string 3)))
5477 (defun org-get-indentation (&optional line)
5478 "Get the indentation of the current line, interpreting tabs.
5479 When LINE is given, assume it represents a line and compute its indentation."
5481 (if (string-match "^ *" (org-remove-tabs line))
5484 (beginning-of-line 1)
5485 (skip-chars-forward " \t")
5488 (defun org-remove-tabs (s &optional width)
5489 "Replace tabulators in S with spaces.
5490 Assumes that s is a single line, starting in column 0."
5491 (setq width (or width tab-width))
5492 (while (string-match "\t" s)
5493 (setq s (replace-match
5495 (- (* width (/ (+ (match-beginning 0) width) width))
5496 (match-beginning 0)) ?\ )
5500 (defun org-fix-indentation (line ind)
5501 "Fix indentation in LINE.
5502 IND is a cons cell with target and minimum indentation.
5503 If the current indenation in LINE is smaller than the minimum,
5504 leave it alone. If it is larger than ind, set it to the target."
5505 (let* ((l (org-remove-tabs line))
5506 (i (org-get-indentation l))
5507 (i1 (car ind)) (i2 (cdr ind)))
5508 (if (>= i i2) (setq l (substring line i2)))
5510 (concat (make-string i1 ?\ ) l)
5513 (defun org-beginning-of-item ()
5514 "Go to the beginning of the current hand-formatted item.
5515 If the cursor is not in an item, throw an error."
5518 (limit (save-excursion
5521 (org-back-to-heading)
5522 (beginning-of-line 2) (point))
5523 (error (point-min)))))
5524 (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
5527 (beginning-of-line 1)
5528 (beginning-of-line 1)
5529 (skip-chars-forward " \t")
5530 (setq ind (current-column))
5533 (beginning-of-line 0)
5534 (if (or (bobp) (< (point) limit)) (throw 'exit nil))
5536 (if (looking-at "[ \t]*$")
5537 (setq ind1 ind-empty)
5538 (skip-chars-forward " \t")
5539 (setq ind1 (current-column)))
5541 (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
5544 (error "Not in an item")))))
5546 (defun org-end-of-item ()
5547 "Go to the end of the current hand-formatted item.
5548 If the cursor is not in an item, throw an error."
5550 (let* ((pos (point))
5552 (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
5553 (limit (save-excursion (outline-next-heading) (point)))
5554 (ind (save-excursion
5555 (org-beginning-of-item)
5556 (skip-chars-forward " \t")
5560 (beginning-of-line 2)
5561 (if (eobp) (throw 'exit (point)))
5562 (if (>= (point) limit) (throw 'exit (point-at-bol)))
5563 (if (looking-at "[ \t]*$")
5564 (setq ind1 ind-empty)
5565 (skip-chars-forward " \t")
5566 (setq ind1 (current-column)))
5568 (throw 'exit (point-at-bol)))))))
5572 (error "Not in an item"))))
5574 (defun org-next-item ()
5575 "Move to the beginning of the next item in the current plain list.
5576 Error if not at a plain list, or if this is the last item in the list."
5578 (let (ind ind1 (pos (point)))
5579 (org-beginning-of-item)
5580 (setq ind (org-get-indentation))
5582 (setq ind1 (org-get-indentation))
5583 (unless (and (org-at-item-p) (= ind ind1))
5585 (error "On last item"))))
5587 (defun org-previous-item ()
5588 "Move to the beginning of the previous item in the current plain list.
5589 Error if not at a plain list, or if this is the first item in the list."
5591 (let (beg ind ind1 (pos (point)))
5592 (org-beginning-of-item)
5594 (setq ind (org-get-indentation))
5598 (beginning-of-line 0)
5599 (if (looking-at "[ \t]*$")
5601 (if (<= (setq ind1 (org-get-indentation)) ind)
5604 (if (or (not (org-at-item-p))
5607 (org-beginning-of-item))
5608 (error (goto-char pos)
5609 (error "On first item")))))
5611 (defun org-first-list-item-p ()
5612 "Is this heading the item in a plain list?"
5613 (unless (org-at-item-p)
5614 (error "Not at a plain list item"))
5615 (org-beginning-of-item)
5616 (= (point) (save-excursion (org-beginning-of-item-list))))
5618 (defun org-move-item-down ()
5619 "Move the plain list item at point down, i.e. swap with following item.
5620 Subitems (items with larger indentation) are considered part of the item,
5621 so this really moves item trees."
5623 (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
5624 (org-beginning-of-item)
5627 (setq ne-beg (org-back-over-empty-lines))
5630 (setq ind (org-get-indentation))
5633 (setq ind1 (org-get-indentation))
5634 (setq ne-end (org-back-over-empty-lines))
5637 (when (and (org-first-list-item-p) (< ne-end ne-beg))
5638 ;; include less whitespace
5641 (forward-line (- ne-beg ne-end))
5642 (setq beg (point))))
5644 (if (and (org-at-item-p) (= ind ind1))
5647 (org-back-over-empty-lines)
5648 (setq txt (buffer-substring beg end))
5650 (delete-region beg end))
5653 (goto-char pos) (org-skip-whitespace)
5654 (org-maybe-renumber-ordered-list))
5656 (error "Cannot move this item further down"))))
5658 (defun org-move-item-up (arg)
5659 "Move the plain list item at point up, i.e. swap with previous item.
5660 Subitems (items with larger indentation) are considered part of the item,
5661 so this really moves item trees."
5663 (let (beg beg0 end ind ind1 (pos (point)) txt
5664 ne-beg ne-ins ins-end)
5665 (org-beginning-of-item)
5667 (setq ind (org-get-indentation))
5669 (setq ne-beg (org-back-over-empty-lines))
5677 (beginning-of-line 0)
5678 (if (looking-at "[ \t]*$")
5679 (if org-empty-line-terminates-plain-lists
5682 (error "Cannot move this item further up"))
5684 (if (<= (setq ind1 (org-get-indentation)) ind)
5687 (org-beginning-of-item)
5688 (error (goto-char beg)
5689 (error "Cannot move this item further up")))
5690 (setq ind1 (org-get-indentation))
5691 (if (and (org-at-item-p) (= ind ind1))
5693 (setq ne-ins (org-back-over-empty-lines))
5694 (setq txt (buffer-substring beg end))
5696 (delete-region beg end))
5699 (setq ins-end (point))
5700 (goto-char pos) (org-skip-whitespace)
5702 (when (and (org-first-list-item-p) (> ne-ins ne-beg))
5703 ;; Move whitespace back to beginning
5706 (let ((kill-whole-line t))
5707 (kill-line (- ne-ins ne-beg)) (point)))
5708 (insert (make-string (- ne-ins ne-beg) ?\n)))
5710 (org-maybe-renumber-ordered-list))
5712 (error "Cannot move this item further up"))))
5714 (defun org-maybe-renumber-ordered-list ()
5715 "Renumber the ordered list at point if setup allows it.
5716 This tests the user option `org-auto-renumber-ordered-lists' before
5717 doing the renumbering."
5719 (when (and org-auto-renumber-ordered-lists
5721 (if (match-beginning 3)
5722 (org-renumber-ordered-list 1)
5723 (org-fix-bullet-type))))
5725 (defun org-maybe-renumber-ordered-list-safe ()
5728 (org-maybe-renumber-ordered-list))
5731 (defun org-cycle-list-bullet (&optional which)
5732 "Cycle through the different itemize/enumerate bullets.
5733 This cycle the entire list level through the sequence:
5735 `-' -> `+' -> `*' -> `1.' -> `1)'
5737 If WHICH is a string, use that as the new bullet. If WHICH is an integer,
5738 0 meand `-', 1 means `+' etc."
5741 (org-beginning-of-item-list)
5743 (beginning-of-line 1)
5744 (let ((current (match-string 0))
5745 (prevp (eq which 'previous))
5748 ((and (numberp which)
5749 (nth (1- which) '("-" "+" "*" "1." "1)"))))
5750 ((string-match "-" current) (if prevp "1)" "+"))
5751 ((string-match "\\+" current)
5752 (if prevp "-" (if (looking-at "\\S-") "1." "*")))
5753 ((string-match "\\*" current) (if prevp "+" "1."))
5754 ((string-match "\\." current) (if prevp "*" "1)"))
5755 ((string-match ")" current) (if prevp "1." "-"))
5756 (t (error "This should not happen"))))
5757 (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
5758 (org-fix-bullet-type)
5759 (org-maybe-renumber-ordered-list))))
5761 (defun org-get-string-indentation (s)
5762 "What indentation has S due to SPACE and TAB at the beginning of the string?"
5763 (let ((n -1) (i 0) (w tab-width) c)
5765 (while (< (setq n (1+ n)) (length s))
5767 (cond ((= c ?\ ) (setq i (1+ i)))
5768 ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
5769 (t (throw 'exit t)))))
5772 (defun org-renumber-ordered-list (arg)
5773 "Renumber an ordered plain list.
5774 Cursor needs to be in the first line of an item, the line that starts
5775 with something like \"1.\" or \"2)\"."
5777 (unless (and (org-at-item-p)
5778 (match-beginning 3))
5779 (error "This is not an ordered list"))
5780 (let ((line (org-current-line))
5781 (col (current-column))
5782 (ind (org-get-string-indentation
5783 (buffer-substring (point-at-bol) (match-beginning 3))))
5784 ;; (term (substring (match-string 3) -1))
5787 ;; find where this list begins
5788 (org-beginning-of-item-list)
5789 (looking-at "[ \t]*[0-9]+\\([.)]\\)")
5790 (setq fmt (concat "%d" (match-string 1)))
5791 (beginning-of-line 0)
5792 ;; walk forward and replace these numbers
5796 (beginning-of-line 2)
5797 (if (eobp) (throw 'exit nil))
5798 (if (looking-at "[ \t]*$") (throw 'next nil))
5799 (skip-chars-forward " \t") (setq ind1 (current-column))
5800 (if (> ind1 ind) (throw 'next t))
5801 (if (< ind1 ind) (throw 'exit t))
5802 (if (not (org-at-item-p)) (throw 'exit nil))
5803 (delete-region (match-beginning 2) (match-end 2))
5804 (goto-char (match-beginning 2))
5805 (insert (format fmt (setq n (1+ n)))))))
5807 (org-move-to-column col)))
5809 (defun org-fix-bullet-type ()
5810 "Make sure all items in this list have the same bullet as the firsst item."
5812 (unless (org-at-item-p) (error "This is not a list"))
5813 (let ((line (org-current-line))
5814 (col (current-column))
5815 (ind (current-indentation))
5817 ;; find where this list begins
5818 (org-beginning-of-item-list)
5819 (beginning-of-line 1)
5820 ;; find out what the bullet type is
5821 (looking-at "[ \t]*\\(\\S-+\\)")
5822 (setq bullet (match-string 1))
5823 ;; walk forward and replace these numbers
5824 (beginning-of-line 0)
5828 (beginning-of-line 2)
5829 (if (eobp) (throw 'exit nil))
5830 (if (looking-at "[ \t]*$") (throw 'next nil))
5831 (skip-chars-forward " \t") (setq ind1 (current-column))
5832 (if (> ind1 ind) (throw 'next t))
5833 (if (< ind1 ind) (throw 'exit t))
5834 (if (not (org-at-item-p)) (throw 'exit nil))
5835 (skip-chars-forward " \t")
5836 (looking-at "\\S-+")
5837 (replace-match bullet))))
5839 (org-move-to-column col)
5840 (if (string-match "[0-9]" bullet)
5841 (org-renumber-ordered-list 1))))
5843 (defun org-beginning-of-item-list ()
5844 "Go to the beginning of the current item list.
5845 I.e. to the first item in this list."
5847 (org-beginning-of-item)
5848 (let ((pos (point-at-bol))
5849 (ind (org-get-indentation))
5851 ;; find where this list begins
5855 (beginning-of-line 0)
5856 (if (looking-at "[ \t]*$")
5857 (throw (if (bobp) 'exit 'next) t))
5858 (skip-chars-forward " \t") (setq ind1 (current-column))
5859 (if (or (< ind1 ind)
5861 (not (org-at-item-p)))
5864 (when (org-at-item-p) (setq pos (point-at-bol)))))))
5868 (defun org-end-of-item-list ()
5869 "Go to the end of the current item list.
5870 I.e. to the text after the last item."
5872 (org-beginning-of-item)
5873 (let ((pos (point-at-bol))
5874 (ind (org-get-indentation))
5876 ;; find where this list begins
5880 (beginning-of-line 2)
5881 (if (looking-at "[ \t]*$")
5882 (throw (if (eobp) 'exit 'next) t))
5883 (skip-chars-forward " \t") (setq ind1 (current-column))
5884 (if (or (< ind1 ind)
5886 (not (org-at-item-p)))
5889 (setq pos (point-at-bol))
5890 (throw 'exit t))))))
5894 (defvar org-last-indent-begin-marker (make-marker))
5895 (defvar org-last-indent-end-marker (make-marker))
5897 (defun org-outdent-item (arg)
5898 "Outdent a local list item."
5900 (org-indent-item (- arg)))
5902 (defun org-indent-item (arg)
5903 "Indent a local list item."
5905 (unless (org-at-item-p)
5906 (error "Not on an item"))
5908 (let (beg end ind ind1 tmp delta ind-down ind-up)
5909 (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
5910 (setq beg org-last-indent-begin-marker
5911 end org-last-indent-end-marker)
5912 (org-beginning-of-item)
5913 (setq beg (move-marker org-last-indent-begin-marker (point)))
5915 (setq end (move-marker org-last-indent-end-marker (point))))
5917 (setq tmp (org-item-indent-positions)
5919 ind-down (nth 2 tmp)
5922 (if ind-down (- ind-down ind) 2)
5923 (if ind-up (- ind-up ind) -2)))
5924 (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
5925 (while (< (point) end)
5926 (beginning-of-line 1)
5927 (skip-chars-forward " \t") (setq ind1 (current-column))
5928 (delete-region (point-at-bol) (point))
5929 (or (eolp) (org-indent-to-column (+ ind1 delta)))
5930 (beginning-of-line 2))))
5931 (org-fix-bullet-type)
5932 (org-maybe-renumber-ordered-list-safe)
5934 (beginning-of-line 0)
5935 (condition-case nil (org-beginning-of-item) (error nil))
5936 (org-maybe-renumber-ordered-list-safe)))
5938 (defun org-item-indent-positions ()
5939 "Return indentation for plain list items.
5940 This returns a list with three values: The current indentation, the
5941 parent indentation and the indentation a child should habe.
5942 Assumes cursor in item line."
5943 (let* ((bolpos (point-at-bol))
5944 (ind (org-get-indentation))
5945 ind-down ind-up pos)
5947 (org-beginning-of-item-list)
5948 (skip-chars-backward "\n\r \t")
5949 (when (org-in-item-p)
5950 (org-beginning-of-item)
5951 (setq ind-up (org-get-indentation))))
5955 ((and (condition-case nil (progn (org-previous-item) t)
5957 (or (forward-char 1) t)
5958 (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
5959 (setq ind-down (org-get-indentation)))
5960 ((and (goto-char pos)
5962 (goto-char (match-end 0))
5963 (skip-chars-forward " \t")
5964 (setq ind-down (current-column)))))
5965 (list ind ind-up ind-down)))
5967 ;;; The orgstruct minor mode
5969 ;; Define a minor mode which can be used in other modes in order to
5970 ;; integrate the org-mode structure editing commands.
5972 ;; This is really a hack, because the org-mode structure commands use
5973 ;; keys which normally belong to the major mode. Here is how it
5974 ;; works: The minor mode defines all the keys necessary to operate the
5975 ;; structure commands, but wraps the commands into a function which
5976 ;; tests if the cursor is currently at a headline or a plain list
5977 ;; item. If that is the case, the structure command is used,
5978 ;; temporarily setting many Org-mode variables like regular
5979 ;; expressions for filling etc. However, when any of those keys is
5980 ;; used at a different location, function uses `key-binding' to look
5981 ;; up if the key has an associated command in another currently active
5982 ;; keymap (minor modes, major mode, global), and executes that
5983 ;; command. There might be problems if any of the keys is otherwise
5984 ;; used as a prefix key.
5986 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
5987 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
5988 ;; addresses this by checking explicitly for both bindings.
5990 (defvar orgstruct-mode-map (make-sparse-keymap)
5991 "Keymap for the minor `orgstruct-mode'.")
5993 (defvar org-local-vars nil
5994 "List of local variables, for use by `orgstruct-mode'")
5997 (define-minor-mode orgstruct-mode
5998 "Toggle the minor more `orgstruct-mode'.
5999 This mode is for using Org-mode structure commands in other modes.
6000 The following key behave as if Org-mode was active, if the cursor
6001 is on a headline, or on a plain list item (both in the definition
6004 M-up Move entry/item up
6005 M-down Move entry/item down
6008 M-S-up Move entry/item up
6009 M-S-down Move entry/item down
6010 M-S-left Promote subtree
6011 M-S-right Demote subtree
6012 M-q Fill paragraph and items like in Org-mode
6014 C-c - Cycle list bullet
6015 TAB Cycle item visibility
6016 M-RET Insert new heading/item
6017 S-M-RET Insert new TODO heading / Chekbox item
6018 C-c C-c Set tags / toggle checkbox"
6019 nil " OrgStruct" nil
6020 (org-load-modules-maybe)
6021 (and (orgstruct-setup) (defun orgstruct-setup () nil)))
6024 (defun turn-on-orgstruct ()
6025 "Unconditionally turn on `orgstruct-mode'."
6029 (defun turn-on-orgstruct++ ()
6030 "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
6031 In addition to setting orgstruct-mode, this also exports all indentation and
6032 autofilling variables from org-mode into the buffer. Note that turning
6033 off orgstruct-mode will *not* remove these additional settings."
6039 "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
6040 (symbol-name (car x)))
6041 (setq var (car x) val (nth 1 x))
6042 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
6045 (defun orgstruct-error ()
6046 "Error when there is no default binding for a structure key."
6048 (error "This key has no function outside structure elements"))
6050 (defun orgstruct-setup ()
6051 "Setup orgstruct keymaps."
6055 '([(meta up)] org-metaup)
6056 '([(meta down)] org-metadown)
6057 '([(meta left)] org-metaleft)
6058 '([(meta right)] org-metaright)
6059 '([(meta shift up)] org-shiftmetaup)
6060 '([(meta shift down)] org-shiftmetadown)
6061 '([(meta shift left)] org-shiftmetaleft)
6062 '([(meta shift right)] org-shiftmetaright)
6063 '([(shift up)] org-shiftup)
6064 '([(shift down)] org-shiftdown)
6065 '("\C-c\C-c" org-ctrl-c-ctrl-c)
6066 '("\M-q" fill-paragraph)
6068 '("\C-c-" org-cycle-list-bullet)))
6070 (while (setq elt (pop bindings))
6071 (setq nfunc (1+ nfunc))
6072 (setq key (org-key (car elt))
6074 cmd (orgstruct-make-binding fun nfunc key))
6075 (org-defkey orgstruct-mode-map key cmd))
6077 ;; Special treatment needed for TAB and RET
6078 (org-defkey orgstruct-mode-map [(tab)]
6079 (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
6080 (org-defkey orgstruct-mode-map "\C-i"
6081 (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
6083 (org-defkey orgstruct-mode-map "\M-\C-m"
6084 (orgstruct-make-binding 'org-insert-heading 105
6085 "\M-\C-m" [(meta return)]))
6086 (org-defkey orgstruct-mode-map [(meta return)]
6087 (orgstruct-make-binding 'org-insert-heading 106
6088 [(meta return)] "\M-\C-m"))
6090 (org-defkey orgstruct-mode-map [(shift meta return)]
6091 (orgstruct-make-binding 'org-insert-todo-heading 107
6092 [(meta return)] "\M-\C-m"))
6094 (unless org-local-vars
6095 (setq org-local-vars (org-get-local-variables)))
6099 (defun orgstruct-make-binding (fun n &rest keys)
6100 "Create a function for binding in the structure minor mode.
6101 FUN is the command to call inside a table. N is used to create a unique
6102 command name. KEYS are keys that should be checked in for a command
6103 to execute outside of tables."
6106 (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
6108 (concat "In Structure, run `" (symbol-name fun) "'.\n"
6109 "Outside of structure, run the binding of `"
6110 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
6114 '(org-context-p 'headline 'item)
6115 (list 'org-run-like-in-org-mode (list 'quote fun))
6116 (list 'let '(orgstruct-mode)
6117 (list 'call-interactively
6120 (list 'key-binding k))
6122 '('orgstruct-error))))))))
6124 (defun org-context-p (&rest contexts)
6125 "Check if local context is and of CONTEXTS.
6126 Possible values in the list of contexts are `table', `headline', and `item'."
6127 (let ((pos (point)))
6128 (goto-char (point-at-bol))
6129 (prog1 (or (and (memq 'table contexts)
6130 (looking-at "[ \t]*|"))
6131 (and (memq 'headline contexts)
6132 (looking-at "\\*+"))
6133 (and (memq 'item contexts)
6134 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
6137 (defun org-get-local-variables ()
6138 "Return a list of all local variables in an org-mode buffer."
6140 (with-current-buffer (get-buffer-create "*Org tmp*")
6143 (setq varlist (buffer-local-variables)))
6144 (kill-buffer "*Org tmp*")
6151 (list (car x) (list 'quote (cdr x)))))
6153 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
6154 (symbol-name (car x)))
6159 (defun org-run-like-in-org-mode (cmd)
6160 (org-load-modules-maybe)
6161 (unless org-local-vars
6162 (setq org-local-vars (org-get-local-variables)))
6163 (eval (list 'let org-local-vars
6164 (list 'call-interactively (list 'quote cmd)))))
6168 (defun org-get-category (&optional pos)
6169 "Get the category applying to position POS."
6170 (get-text-property (or pos (point)) 'org-category))
6172 (defun org-refresh-category-properties ()
6173 "Refresh category text properties in the buffer."
6174 (let ((def-cat (cond
6175 ((null org-category)
6176 (if buffer-file-name
6177 (file-name-sans-extension
6178 (file-name-nondirectory buffer-file-name))
6180 ((symbolp org-category) (symbol-name org-category))
6182 beg end cat pos optionp)
6187 (goto-char (point-min))
6188 (put-text-property (point) (point-max) 'org-category def-cat)
6189 (while (re-search-forward
6190 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
6191 (setq pos (match-end 0)
6192 optionp (equal (char-after (match-beginning 0)) ?#)
6193 cat (org-trim (match-string 2)))
6195 (setq beg (point-at-bol) end (point-max))
6196 (org-back-to-heading t)
6197 (setq beg (point) end (org-end-of-subtree t t)))
6198 (put-text-property beg end 'org-category cat)
6199 (goto-char pos)))))))
6204 ;;; Link abbreviations
6206 (defun org-link-expand-abbrev (link)
6207 "Apply replacements as defined in `org-link-abbrev-alist."
6208 (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
6209 (let* ((key (match-string 1 link))
6210 (as (or (assoc key org-link-abbrev-alist-local)
6211 (assoc key org-link-abbrev-alist)))
6212 (tag (and (match-end 2) (match-string 3 link)))
6218 ((symbolp rpl) (funcall rpl tag))
6219 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
6220 (t (concat rpl tag)))))
6223 ;;; Storing and inserting links
6225 (defvar org-insert-link-history nil
6226 "Minibuffer history for links inserted with `org-insert-link'.")
6228 (defvar org-stored-links nil
6229 "Contains the links stored with `org-store-link'.")
6231 (defvar org-store-link-plist nil
6232 "Plist with info about the most recently link created with `org-store-link'.")
6234 (defvar org-link-protocols nil
6235 "Link protocols added to Org-mode using `org-add-link-type'.")
6237 (defvar org-store-link-functions nil
6238 "List of functions that are called to create and store a link.
6239 Each function will be called in turn until one returns a non-nil
6240 value. Each function should check if it is responsible for creating
6241 this link (for example by looking at the major mode).
6242 If not, it must exit and return nil.
6243 If yes, it should return a non-nil value after a calling
6244 `org-store-link-props' with a list of properties and values.
6245 Special properties are:
6247 :type The link prefix. like \"http\". This must be given.
6248 :link The link, like \"http://www.astro.uva.nl/~dominik\".
6249 This is obligatory as well.
6250 :description Optional default description for the second pair
6251 of brackets in an Org-mode link. The user can still change
6252 this when inserting this link into an Org-mode buffer.
6254 In addition to these, any additional properties can be specified
6255 and then used in remember templates.")
6257 (defun org-add-link-type (type &optional follow export)
6258 "Add TYPE to the list of `org-link-types'.
6259 Re-compute all regular expressions depending on `org-link-types'
6261 FOLLOW and EXPORT are two functions.
6263 FOLLOW should take the link path as the single argument and do whatever
6264 is necessary to follow the link, for example find a file or display
6267 EXPORT should format the link path for export to one of the export formats.
6268 It should be a function accepting three arguments:
6270 path the path of the link, the text after the prefix (like \"http:\")
6271 desc the description of the link, if any, nil if there was no descripton
6272 format the export format, a symbol like `html' or `latex'.
6274 The function may use the FORMAT information to return different values
6275 depending on the format. The return value will be put literally into
6277 Org-mode has a built-in default for exporting links. If you are happy with
6278 this default, there is no need to define an export function for the link
6279 type. For a simple example of an export function, see `org-bbdb.el'."
6280 (add-to-list 'org-link-types type t)
6281 (org-make-link-regexps)
6282 (if (assoc type org-link-protocols)
6283 (setcdr (assoc type org-link-protocols) (list follow export))
6284 (push (list type follow export) org-link-protocols)))
6288 (defun org-store-link (arg)
6289 "\\<org-mode-map>Store an org-link to the current location.
6290 This link is added to `org-stored-links' and can later be inserted
6291 into an org-buffer with \\[org-insert-link].
6293 For some link types, a prefix arg is interpreted:
6294 For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
6295 For file links, arg negates `org-context-in-file-links'."
6297 (org-load-modules-maybe)
6298 (setq org-store-link-plist nil) ; reset
6299 (let (link cpltxt desc description search txt)
6302 ((run-hook-with-args-until-success 'org-store-link-functions)
6303 (setq link (plist-get org-store-link-plist :link)
6304 desc (or (plist-get org-store-link-plist :description) link)))
6306 ((eq major-mode 'calendar-mode)
6307 (let ((cd (calendar-cursor-to-date)))
6310 (car org-time-stamp-formats)
6312 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
6314 (org-store-link-props :type "calendar" :date cd)))
6316 ((eq major-mode 'w3-mode)
6317 (setq cpltxt (url-view-url t)
6318 link (org-make-link cpltxt))
6319 (org-store-link-props :type "w3" :url (url-view-url t)))
6321 ((eq major-mode 'w3m-mode)
6322 (setq cpltxt (or w3m-current-title w3m-current-url)
6323 link (org-make-link w3m-current-url))
6324 (org-store-link-props :type "w3m" :url (url-view-url t)))
6326 ((setq search (run-hook-with-args-until-success
6327 'org-create-file-search-functions))
6328 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
6330 (setq cpltxt (or description link)))
6332 ((eq major-mode 'image-mode)
6333 (setq cpltxt (concat "file:"
6334 (abbreviate-file-name buffer-file-name))
6335 link (org-make-link cpltxt))
6336 (org-store-link-props :type "image" :file buffer-file-name))
6338 ((eq major-mode 'dired-mode)
6339 ;; link to the file in the current line
6340 (setq cpltxt (concat "file:"
6341 (abbreviate-file-name
6343 (dired-get-filename nil t))))
6344 link (org-make-link cpltxt)))
6346 ((and buffer-file-name (org-mode-p))
6347 ;; Just link to current headline
6348 (setq cpltxt (concat "file:"
6349 (abbreviate-file-name buffer-file-name)))
6350 ;; Add a context search string
6351 (when (org-xor org-context-in-file-links arg)
6352 ;; Check if we are on a target
6353 (if (org-in-regexp "<<\\(.*?\\)>>")
6354 (setq cpltxt (concat cpltxt "::" (match-string 1)))
6356 ((org-on-heading-p) nil)
6357 ((org-region-active-p)
6358 (buffer-substring (region-beginning) (region-end)))
6360 (when (or (null txt) (string-match "\\S-" txt))
6364 (org-make-org-heading-search-string txt)
6367 (if (string-match "::\\'" cpltxt)
6368 (setq cpltxt (substring cpltxt 0 -2)))
6369 (setq link (org-make-link cpltxt)))
6371 ((buffer-file-name (buffer-base-buffer))
6372 ;; Just link to this file here.
6373 (setq cpltxt (concat "file:"
6374 (abbreviate-file-name
6375 (buffer-file-name (buffer-base-buffer)))))
6376 ;; Add a context string
6377 (when (org-xor org-context-in-file-links arg)
6378 (setq txt (if (org-region-active-p)
6379 (buffer-substring (region-beginning) (region-end))
6380 (buffer-substring (point-at-bol) (point-at-eol))))
6381 ;; Only use search option if there is some text.
6382 (when (string-match "\\S-" txt)
6384 (concat cpltxt "::" (org-make-org-heading-search-string txt))
6386 (setq link (org-make-link cpltxt)))
6389 (error "Cannot link to a buffer which is not visiting a file"))
6391 (t (setq link nil)))
6393 (if (consp link) (setq cpltxt (car link) link (cdr link)))
6394 (setq link (or link cpltxt)
6395 desc (or desc cpltxt))
6396 (if (equal desc "NONE") (setq desc nil))
6398 (if (and (interactive-p) link)
6400 (setq org-stored-links
6401 (cons (list link desc) org-stored-links))
6402 (message "Stored: %s" (or desc link)))
6403 (and link (org-make-link-string link desc)))))
6405 (defun org-store-link-props (&rest plist)
6406 "Store link properties, extract names and addresses."
6408 (when (setq x (plist-get plist :from))
6409 (setq adr (mail-extract-address-components x))
6410 (plist-put plist :fromname (car adr))
6411 (plist-put plist :fromaddress (nth 1 adr)))
6412 (when (setq x (plist-get plist :to))
6413 (setq adr (mail-extract-address-components x))
6414 (plist-put plist :toname (car adr))
6415 (plist-put plist :toaddress (nth 1 adr))))
6416 (let ((from (plist-get plist :from))
6417 (to (plist-get plist :to)))
6418 (when (and from to org-from-is-user-regexp)
6419 (plist-put plist :fromto
6420 (if (string-match org-from-is-user-regexp from)
6422 (concat "from %f")))))
6423 (setq org-store-link-plist plist))
6425 (defun org-add-link-props (&rest plist)
6426 "Add these properties to the link property list."
6429 (setq key (pop plist) value (pop plist))
6430 (setq org-store-link-plist
6431 (plist-put org-store-link-plist key value)))))
6433 (defun org-email-link-description (&optional fmt)
6434 "Return the description part of an email link.
6435 This takes information from `org-store-link-plist' and formats it
6436 according to FMT (default from `org-email-link-description-format')."
6437 (setq fmt (or fmt org-email-link-description-format))
6438 (let* ((p org-store-link-plist)
6439 (to (plist-get p :toaddress))
6440 (from (plist-get p :fromaddress))
6443 (cons "%c" (plist-get p :fromto))
6444 (cons "%F" (plist-get p :from))
6445 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
6446 (cons "%T" (plist-get p :to))
6447 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
6448 (cons "%s" (plist-get p :subject))
6449 (cons "%m" (plist-get p :message-id)))))
6450 (when (string-match "%c" fmt)
6451 ;; Check if the user wrote this message
6452 (if (and org-from-is-user-regexp from to
6453 (save-match-data (string-match org-from-is-user-regexp from)))
6454 (setq fmt (replace-match "to %t" t t fmt))
6455 (setq fmt (replace-match "from %f" t t fmt))))
6456 (org-replace-escapes fmt table)))
6458 (defun org-make-org-heading-search-string (&optional string heading)
6459 "Make search string for STRING or current headline."
6461 (let ((s (or string (org-get-heading))))
6462 (unless (and string (not heading))
6463 ;; We are using a headline, clean up garbage in there.
6464 (if (string-match org-todo-regexp s)
6465 (setq s (replace-match "" t t s)))
6466 (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
6467 (setq s (replace-match "" t t s)))
6468 (setq s (org-trim s))
6469 (if (string-match (concat "^\\(" org-quote-string "\\|"
6470 org-comment-string "\\)") s)
6471 (setq s (replace-match "" t t s)))
6472 (while (string-match org-ts-regexp s)
6473 (setq s (replace-match "" t t s))))
6474 (while (string-match "[^a-zA-Z_0-9 \t]+" s)
6475 (setq s (replace-match " " t t s)))
6476 (or string (setq s (concat "*" s))) ; Add * for headlines
6477 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
6479 (defun org-make-link (&rest strings)
6480 "Concatenate STRINGS."
6481 (apply 'concat strings))
6483 (defun org-make-link-string (link &optional description)
6484 "Make a link with brackets, consisting of LINK and DESCRIPTION."
6485 (unless (string-match "\\S-" link)
6486 (error "Empty link"))
6487 (when (stringp description)
6488 ;; Remove brackets from the description, they are fatal.
6489 (while (string-match "\\[" description)
6490 (setq description (replace-match "{" t t description)))
6491 (while (string-match "\\]" description)
6492 (setq description (replace-match "}" t t description))))
6493 (when (equal (org-link-escape link) description)
6494 ;; No description needed, it is identical
6495 (setq description nil))
6496 (when (and (not description)
6497 (not (equal link (org-link-escape link))))
6498 (setq description link))
6499 (concat "[[" (org-link-escape link) "]"
6500 (if description (concat "[" description "]") "")
6503 (defconst org-link-escape-chars
6507 (?\340 . "%E0") ; `a
6508 (?\342 . "%E2") ; ^a
6509 (?\347 . "%E7") ; ,c
6510 (?\350 . "%E8") ; `e
6511 (?\351 . "%E9") ; 'e
6512 (?\352 . "%EA") ; ^e
6513 (?\356 . "%EE") ; ^i
6514 (?\364 . "%F4") ; ^o
6515 (?\371 . "%F9") ; `u
6516 (?\373 . "%FB") ; ^u
6522 "Association list of escapes for some characters problematic in links.
6523 This is the list that is used for internal purposes.")
6525 (defconst org-link-escape-chars-browser
6526 '((?\ . "%20")) ; 32 for the SPC char
6527 "Association list of escapes for some characters problematic in links.
6528 This is the list that is used before handing over to the browser.")
6530 (defun org-link-escape (text &optional table)
6531 "Escape charaters in TEXT that are problematic for links."
6532 (setq table (or table org-link-escape-chars))
6534 (let ((re (mapconcat (lambda (x) (regexp-quote
6535 (char-to-string (car x))))
6537 (while (string-match re text)
6540 (cdr (assoc (string-to-char (match-string 0 text))
6545 (defun org-link-unescape (text &optional table)
6546 "Reverse the action of `org-link-escape'."
6547 (setq table (or table org-link-escape-chars))
6549 (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
6551 (while (string-match re text)
6554 (char-to-string (car (rassoc (match-string 0 text) table)))
6558 (defun org-xor (a b)
6562 (defun org-get-header (header)
6563 "Find a header field in the current buffer."
6565 (goto-char (point-min))
6566 (let ((case-fold-search t) s)
6569 (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
6570 (setq s (match-string 1)))
6571 (while (string-match "\"" s)
6572 (setq s (replace-match "" t t s)))
6573 (if (string-match "[<(].*" s)
6574 (setq s (replace-match "" t t s))))
6575 ((eq header 'message-id)
6576 (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
6577 (setq s (match-string 1))))
6578 ((eq header 'subject)
6579 (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
6580 (setq s (match-string 1)))))
6581 (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
6582 (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
6586 (defun org-fixup-message-id-for-http (s)
6587 "Replace special characters in a message id, so it can be used in an http query."
6588 (while (string-match "<" s)
6589 (setq s (replace-match "%3C" t t s)))
6590 (while (string-match ">" s)
6591 (setq s (replace-match "%3E" t t s)))
6592 (while (string-match "@" s)
6593 (setq s (replace-match "%40" t t s)))
6597 (defun org-insert-link-global ()
6598 "Insert a link like Org-mode does.
6599 This command can be called in any mode to insert a link in Org-mode syntax."
6601 (org-load-modules-maybe)
6602 (org-run-like-in-org-mode 'org-insert-link))
6604 (defun org-insert-link (&optional complete-file link-location)
6605 "Insert a link. At the prompt, enter the link.
6607 Completion can be used to select a link previously stored with
6608 `org-store-link'. When the empty string is entered (i.e. if you just
6609 press RET at the prompt), the link defaults to the most recently
6610 stored link. As SPC triggers completion in the minibuffer, you need to
6611 use M-SPC or C-q SPC to force the insertion of a space character.
6613 You will also be prompted for a description, and if one is given, it will
6614 be displayed in the buffer instead of the link.
6616 If there is already a link at point, this command will allow you to edit link
6617 and description parts.
6619 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
6620 be selected using completion. The path to the file will be relative to the
6621 current directory if the file is in the current directory or a subdirectory.
6622 Otherwise, the link will be the absolute path as completed in the minibuffer
6623 \(i.e. normally ~/path/to/file).
6625 With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
6626 the current directory or below. With three \\[universal-argument] prefixes, negate the meaning
6627 of `org-keep-stored-link-after-insertion'.
6629 If `org-make-link-description-function' is non-nil, this function will be
6630 called with the link target, and the result will be the default
6633 If the LINK-LOCATION parameter is non-nil, this value will be
6634 used as the link location instead of reading one interactively."
6636 (let* ((wcf (current-window-configuration))
6637 (region (if (org-region-active-p)
6638 (buffer-substring (region-beginning) (region-end))))
6639 (remove (and region (list (region-beginning) (region-end))))
6641 tmphist ; byte-compile incorrectly complains about this
6642 (link link-location)
6645 (link-location) ; specified by arg, just use it.
6646 ((org-in-regexp org-bracket-link-regexp 1)
6647 ;; We do have a link at point, and we are going to edit it.
6648 (setq remove (list (match-beginning 0) (match-end 0)))
6649 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
6650 (setq link (read-string "Link: "
6652 (org-match-string-no-properties 1)))))
6653 ((or (org-in-regexp org-angle-link-re)
6654 (org-in-regexp org-plain-link-re))
6655 ;; Convert to bracket link
6656 (setq remove (list (match-beginning 0) (match-end 0))
6657 link (read-string "Link: "
6658 (org-remove-angle-brackets (match-string 0)))))
6659 ((equal complete-file '(4))
6660 ;; Completing read for file names.
6661 (setq file (read-file-name "File: "))
6662 (let ((pwd (file-name-as-directory (expand-file-name ".")))
6663 (pwd1 (file-name-as-directory (abbreviate-file-name
6664 (expand-file-name ".")))))
6666 ((equal complete-file '(16))
6667 (setq link (org-make-link
6669 (abbreviate-file-name (expand-file-name file)))))
6670 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
6671 (setq link (org-make-link "file:" (match-string 1 file))))
6672 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
6673 (expand-file-name file))
6674 (setq link (org-make-link
6675 "file:" (match-string 1 (expand-file-name file)))))
6676 (t (setq link (org-make-link "file:" file))))))
6678 ;; Read link, with completion for stored links.
6679 (with-output-to-temp-buffer "*Org Links*"
6680 (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
6681 (when org-stored-links
6682 (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
6685 (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
6686 (reverse org-stored-links) "\n"))))
6687 (let ((cw (selected-window)))
6688 (select-window (get-buffer-window "*Org Links*"))
6689 (shrink-window-if-larger-than-buffer)
6690 (setq truncate-lines t)
6692 ;; Fake a link history, containing the stored links.
6693 (setq tmphist (append (mapcar 'car org-stored-links)
6694 org-insert-link-history))
6696 (setq link (org-completing-read
6699 (mapcar (lambda (x) (list (concat (car x) ":")))
6700 (append org-link-abbrev-alist-local org-link-abbrev-alist))
6701 (mapcar (lambda (x) (list (concat x ":")))
6705 (or (car (car org-stored-links)))))
6706 (set-window-configuration wcf)
6707 (kill-buffer "*Org Links*"))
6708 (setq entry (assoc link org-stored-links))
6709 (or entry (push link org-insert-link-history))
6710 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
6711 (not org-keep-stored-link-after-insertion))
6712 (setq org-stored-links (delq (assoc link org-stored-links)
6714 (setq desc (or desc (nth 1 entry)))))
6716 (if (string-match org-plain-link-re link)
6717 ;; URL-like link, normalize the use of angular brackets.
6718 (setq link (org-make-link (org-remove-angle-brackets link))))
6720 ;; Check if we are linking to the current file with a search option
6721 ;; If yes, simplify the link by using only the search option.
6722 (when (and buffer-file-name
6723 (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
6724 (let* ((path (match-string 1 link))
6725 (case-fold-search nil)
6726 (search (match-string 2 link)))
6728 (if (equal (file-truename buffer-file-name) (file-truename path))
6729 ;; We are linking to this same file, with a search option
6730 (setq link search)))))
6732 ;; Check if we can/should use a relative path. If yes, simplify the link
6733 (when (string-match "\\<file:\\(.*\\)" link)
6734 (let* ((path (match-string 1 link))
6736 (case-fold-search nil))
6738 ((eq org-link-file-path-type 'absolute)
6739 (setq path (abbreviate-file-name (expand-file-name path))))
6740 ((eq org-link-file-path-type 'noabbrev)
6741 (setq path (expand-file-name path)))
6742 ((eq org-link-file-path-type 'relative)
6743 (setq path (file-relative-name path)))
6746 (if (string-match (concat "^" (regexp-quote
6747 (file-name-as-directory
6748 (expand-file-name "."))))
6749 (expand-file-name path))
6750 ;; We are linking a file with relative path name.
6751 (setq path (substring (expand-file-name path)
6753 (setq link (concat "file:" path))
6754 (if (equal desc origpath)
6757 (if org-make-link-description-function
6758 (setq desc (funcall org-make-link-description-function link desc)))
6760 (setq desc (read-string "Description: " desc))
6761 (unless (string-match "\\S-" desc) (setq desc nil))
6762 (if remove (apply 'delete-region remove))
6763 (insert (org-make-link-string link desc))))
6765 (defun org-completing-read (&rest args)
6766 (let ((minibuffer-local-completion-map
6767 (copy-keymap minibuffer-local-completion-map)))
6768 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
6769 (apply 'completing-read args)))
6771 ;;; Opening/following a link
6773 (defvar org-link-search-failed nil)
6775 (defun org-next-link ()
6776 "Move forward to the next link.
6777 If the link is in hidden text, expose it."
6779 (when (and org-link-search-failed (eq this-command last-command))
6780 (goto-char (point-min))
6781 (message "Link search wrapped back to beginning of buffer"))
6782 (setq org-link-search-failed nil)
6783 (let* ((pos (point))
6785 (a (assoc :link ct)))
6786 (if a (goto-char (nth 2 a)))
6787 (if (re-search-forward org-any-link-re nil t)
6789 (goto-char (match-beginning 0))
6790 (if (org-invisible-p) (org-show-context)))
6792 (setq org-link-search-failed t)
6793 (error "No further link found"))))
6795 (defun org-previous-link ()
6796 "Move backward to the previous link.
6797 If the link is in hidden text, expose it."
6799 (when (and org-link-search-failed (eq this-command last-command))
6800 (goto-char (point-max))
6801 (message "Link search wrapped back to end of buffer"))
6802 (setq org-link-search-failed nil)
6803 (let* ((pos (point))
6805 (a (assoc :link ct)))
6806 (if a (goto-char (nth 1 a)))
6807 (if (re-search-backward org-any-link-re nil t)
6809 (goto-char (match-beginning 0))
6810 (if (org-invisible-p) (org-show-context)))
6812 (setq org-link-search-failed t)
6813 (error "No further link found"))))
6815 (defun org-find-file-at-mouse (ev)
6816 "Open file link or URL at mouse."
6818 (mouse-set-point ev)
6819 (org-open-at-point 'in-emacs))
6821 (defun org-open-at-mouse (ev)
6822 "Open file link or URL at mouse."
6824 (mouse-set-point ev)
6825 (org-open-at-point))
6827 (defvar org-window-config-before-follow-link nil
6828 "The window configuration before following a link.
6829 This is saved in case the need arises to restore it.")
6831 (defvar org-open-link-marker (make-marker)
6832 "Marker pointing to the location where `org-open-at-point; was called.")
6835 (defun org-open-at-point-global ()
6836 "Follow a link like Org-mode does.
6837 This command can be called in any mode to follow a link that has
6840 (org-run-like-in-org-mode 'org-open-at-point))
6843 (defun org-open-link-from-string (s &optional arg)
6844 "Open a link in the string S, as if it was in Org-mode."
6845 (interactive "sLink: \nP")
6847 (let ((org-inhibit-startup t))
6850 (goto-char (point-min))
6851 (org-open-at-point arg))))
6853 (defun org-open-at-point (&optional in-emacs)
6854 "Open link at or after point.
6855 If there is no link at point, this function will search forward up to
6856 the end of the current subtree.
6857 Normally, files will be opened by an appropriate application. If the
6858 optional argument IN-EMACS is non-nil, Emacs will visit the file."
6860 (org-load-modules-maybe)
6861 (move-marker org-open-link-marker (point))
6862 (setq org-window-config-before-follow-link (current-window-configuration))
6863 (org-remove-occur-highlights nil nil t)
6864 (if (org-at-timestamp-p t)
6865 (org-follow-timestamp-link)
6866 (let (type path link line search (pos (point)))
6869 (skip-chars-forward "^]\n\r")
6870 (when (org-in-regexp org-bracket-link-regexp)
6871 (setq link (org-link-unescape (org-match-string-no-properties 1)))
6872 (while (string-match " *\n *" link)
6873 (setq link (replace-match " " t t link)))
6874 (setq link (org-link-expand-abbrev link))
6875 (if (string-match org-link-re-with-space2 link)
6876 (setq type (match-string 1 link) path (match-string 2 link))
6877 (setq type "thisfile" path link))
6880 (when (get-text-property (point) 'org-linked-text)
6881 (setq type "thisfile"
6882 pos (if (get-text-property (1+ (point)) 'org-linked-text)
6883 (1+ (point)) (point))
6884 path (buffer-substring
6885 (previous-single-property-change pos 'org-linked-text)
6886 (next-single-property-change pos 'org-linked-text)))
6890 (when (or (org-in-regexp org-angle-link-re)
6891 (org-in-regexp org-plain-link-re))
6892 (setq type (match-string 1) path (match-string 2))
6894 (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
6895 (setq type "tree-match"
6896 path (match-string 1))
6899 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
6901 path (match-string 1))
6902 (while (string-match ":" path)
6903 (setq path (replace-match "+" t t path)))
6906 (error "No link found"))
6907 ;; Remove any trailing spaces in path
6908 (if (string-match " +\\'" path)
6909 (setq path (replace-match "" t t path)))
6913 ((assoc type org-link-protocols)
6914 (funcall (nth 1 (assoc type org-link-protocols)) path))
6916 ((equal type "mailto")
6917 (let ((cmd (car org-link-mailto-program))
6918 (args (cdr org-link-mailto-program)) args1
6919 (address path) (subject "") a)
6920 (if (string-match "\\(.*\\)::\\(.*\\)" path)
6921 (setq address (match-string 1 path)
6922 subject (org-link-escape (match-string 2 path))))
6925 ((not (stringp (car args))) (push (pop args) args1))
6926 (t (setq a (pop args))
6927 (if (string-match "%a" a)
6928 (setq a (replace-match address t t a)))
6929 (if (string-match "%s" a)
6930 (setq a (replace-match subject t t a)))
6932 (apply cmd (nreverse args1))))
6934 ((member type '("http" "https" "ftp" "news"))
6935 (browse-url (concat type ":" (org-link-escape
6936 path org-link-escape-chars-browser))))
6938 ((member type '("message"))
6939 (browse-url (concat type ":" path)))
6941 ((string= type "tags")
6942 (org-tags-view in-emacs path))
6943 ((string= type "thisfile")
6945 (switch-to-buffer-other-window
6946 (org-get-buffer-for-internal-link (current-buffer)))
6947 (org-mark-ring-push))
6948 (let ((cmd `(org-link-search
6950 ,(cond ((equal in-emacs '(4)) 'occur)
6951 ((equal in-emacs '(16)) 'org-occur)
6954 (condition-case nil (eval cmd)
6955 (error (progn (widen) (eval cmd))))))
6957 ((string= type "tree-match")
6958 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
6960 ((string= type "file")
6961 (if (string-match "::\\([0-9]+\\)\\'" path)
6962 (setq line (string-to-number (match-string 1 path))
6963 path (substring path 0 (match-beginning 0)))
6964 (if (string-match "::\\(.+\\)\\'" path)
6965 (setq search (match-string 1 path)
6966 path (substring path 0 (match-beginning 0)))))
6967 (if (string-match "[*?{]" (file-name-nondirectory path))
6969 (org-open-file path in-emacs line search)))
6971 ((string= type "news")
6973 (org-gnus-follow-link path))
6975 ((string= type "shell")
6977 (if (or (not org-confirm-shell-link-function)
6978 (funcall org-confirm-shell-link-function
6979 (format "Execute \"%s\" in shell? "
6980 (org-add-props cmd nil
6981 'face 'org-warning))))
6983 (message "Executing %s" cmd)
6984 (shell-command cmd))
6987 ((string= type "elisp")
6989 (if (or (not org-confirm-elisp-link-function)
6990 (funcall org-confirm-elisp-link-function
6991 (format "Execute \"%s\" as elisp? "
6992 (org-add-props cmd nil
6993 'face 'org-warning))))
6994 (message "%s => %s" cmd (eval (read cmd)))
6998 (browse-url-at-point)))))
6999 (move-marker org-open-link-marker nil)
7000 (run-hook-with-args 'org-follow-link-hook))
7004 (defun org-get-effort (&optional pom)
7005 "Get the effort estimate for the current entry."
7006 (org-entry-get pom org-effort-property))
7010 (defvar org-create-file-search-functions nil
7011 "List of functions to construct the right search string for a file link.
7012 These functions are called in turn with point at the location to
7013 which the link should point.
7015 A function in the hook should first test if it would like to
7016 handle this file type, for example by checking the major-mode or
7017 the file extension. If it decides not to handle this file, it
7018 should just return nil to give other functions a chance. If it
7019 does handle the file, it must return the search string to be used
7020 when following the link. The search string will be part of the
7021 file link, given after a double colon, and `org-open-at-point'
7022 will automatically search for it. If special measures must be
7023 taken to make the search successful, another function should be
7024 added to the companion hook `org-execute-file-search-functions',
7027 A function in this hook may also use `setq' to set the variable
7028 `description' to provide a suggestion for the descriptive text to
7029 be used for this link when it gets inserted into an Org-mode
7030 buffer with \\[org-insert-link].")
7032 (defvar org-execute-file-search-functions nil
7033 "List of functions to execute a file search triggered by a link.
7035 Functions added to this hook must accept a single argument, the
7036 search string that was part of the file link, the part after the
7037 double colon. The function must first check if it would like to
7038 handle this search, for example by checking the major-mode or the
7039 file extension. If it decides not to handle this search, it
7040 should just return nil to give other functions a chance. If it
7041 does handle the search, it must return a non-nil value to keep
7042 other functions from trying.
7044 Each function can access the current prefix argument through the
7045 variable `current-prefix-argument'. Note that a single prefix is
7046 used to force opening a link in Emacs, so it may be good to only
7047 use a numeric or double prefix to guide the search function.
7049 In case this is needed, a function in this hook can also restore
7050 the window configuration before `org-open-at-point' was called using:
7052 (set-window-configuration org-window-config-before-follow-link)")
7054 (defun org-link-search (s &optional type avoid-pos)
7055 "Search for a link search option.
7056 If S is surrounded by forward slashes, it is interpreted as a
7057 regular expression. In org-mode files, this will create an `org-occur'
7058 sparse tree. In ordinary files, `occur' will be used to list matches.
7059 If the current buffer is in `dired-mode', grep will be used to search
7060 in all files. If AVOID-POS is given, ignore matches near that position."
7061 (let ((case-fold-search t)
7062 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
7063 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
7064 (append '(("") (" ") ("\t") ("\n"))
7068 (pre nil) (post nil)
7069 words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
7071 ;; First check if there are any special
7072 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
7073 ;; Now try the builtin stuff
7075 (goto-char (point-min))
7078 (concat "<<" (regexp-quote s0) ">>") nil t)
7079 (setq type 'dedicated
7080 pos (match-beginning 0))))
7081 ;; There is an exact target for this
7083 ((string-match "^/\\(.*\\)/$" s)
7084 ;; A regular expression
7087 (org-occur (match-string 1 s)))
7088 ;;((eq major-mode 'dired-mode)
7089 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
7090 (t (org-do-occur (match-string 1 s)))))
7092 ;; A normal search strings
7093 (when (equal (string-to-char s) ?*)
7094 ;; Anchor on headlines, post may include tags.
7095 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
7096 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
7098 (remove-text-properties
7100 '(face nil mouse-face nil keymap nil fontified nil) s)
7101 ;; Make a series of regular expressions to find a match
7102 (setq words (org-split-string s "[ \n\r\t]+")
7104 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
7105 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
7107 re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
7108 re2a (concat "[ \t\r\n]" re2a_)
7109 re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
7110 re4 (concat "[^a-zA-Z_]" re4_)
7112 re1 (concat pre re2 post)
7113 re3 (concat pre (if pre re4_ re4) post)
7114 re5 (concat pre ".*" re4)
7115 re2 (concat pre re2)
7116 re2a (concat pre (if pre re2a_ re2a))
7117 re4 (concat pre (if pre re4_ re4))
7118 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
7119 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
7123 ((eq type 'org-occur) (org-occur reall))
7124 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
7125 (t (goto-char (point-min))
7127 (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
7128 (org-search-not-self 1 re1 nil t)
7129 (org-search-not-self 1 re2 nil t)
7130 (org-search-not-self 1 re2a nil t)
7131 (org-search-not-self 1 re3 nil t)
7132 (org-search-not-self 1 re4 nil t)
7133 (org-search-not-self 1 re5 nil t)
7135 (goto-char (match-beginning 1))
7137 (error "No match")))))
7139 ;; Normal string-search
7140 (goto-char (point-min))
7141 (if (search-forward s nil t)
7142 (goto-char (match-beginning 0))
7143 (error "No match"))))
7144 (and (org-mode-p) (org-show-context 'link-search))
7147 (defun org-search-not-self (group &rest args)
7148 "Execute `re-search-forward', but only accept matches that do not
7149 enclose the position of `org-open-link-marker'."
7150 (let ((m org-open-link-marker))
7152 (while (apply 're-search-forward args)
7153 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
7154 (goto-char (match-end group))
7155 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
7156 (> (match-beginning 0) (marker-position m))
7157 (< (match-end 0) (marker-position m)))
7159 (or (not (org-in-regexp
7160 org-bracket-link-analytic-regexp 1))
7161 (not (match-end 4)) ; no description
7162 (and (<= (match-beginning 4) (point))
7163 (>= (match-end 4) (point))))))
7164 (throw 'exit (point))))))))
7166 (defun org-get-buffer-for-internal-link (buffer)
7167 "Return a buffer to be used for displaying the link target of internal links."
7169 ((not org-display-internal-link-with-indirect-buffer)
7171 ((string-match "(Clone)$" (buffer-name buffer))
7172 (message "Buffer is already a clone, not making another one")
7173 ;; we also do not modify visibility in this case
7175 (t ; make a new indirect buffer for displaying the link
7176 (let* ((bn (buffer-name buffer))
7177 (ibn (concat bn "(Clone)"))
7178 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
7179 (with-current-buffer ib (org-overview))
7182 (defun org-do-occur (regexp &optional cleanup)
7183 "Call the Emacs command `occur'.
7184 If CLEANUP is non-nil, remove the printout of the regular expression
7185 in the *Occur* buffer. This is useful if the regex is long and not useful
7189 (let ((cwin (selected-window)) win beg end)
7190 (when (setq win (get-buffer-window "*Occur*"))
7191 (select-window win))
7192 (goto-char (point-min))
7193 (when (re-search-forward "match[a-z]+" nil t)
7194 (setq beg (match-end 0))
7195 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
7196 (setq end (1- (match-beginning 0)))))
7197 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
7198 (goto-char (point-min))
7199 (select-window cwin))))
7201 ;;; The mark ring for links jumps
7203 (defvar org-mark-ring nil
7204 "Mark ring for positions before jumps in Org-mode.")
7205 (defvar org-mark-ring-last-goto nil
7206 "Last position in the mark ring used to go back.")
7207 ;; Fill and close the ring
7208 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
7209 (loop for i from 1 to org-mark-ring-length do
7210 (push (make-marker) org-mark-ring))
7211 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
7214 (defun org-mark-ring-push (&optional pos buffer)
7215 "Put the current position or POS into the mark ring and rotate it."
7217 (setq pos (or pos (point)))
7218 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
7219 (move-marker (car org-mark-ring)
7221 (or buffer (current-buffer)))
7223 (substitute-command-keys
7224 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
7226 (defun org-mark-ring-goto (&optional n)
7227 "Jump to the previous position in the mark ring.
7228 With prefix arg N, jump back that many stored positions. When
7229 called several times in succession, walk through the entire ring.
7230 Org-mode commands jumping to a different position in the current file,
7231 or to another Org-mode file, automatically push the old position
7235 (if (eq last-command this-command)
7236 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
7237 (setq p org-mark-ring))
7238 (setq org-mark-ring-last-goto p)
7240 (switch-to-buffer (marker-buffer m))
7242 (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
7244 (defun org-remove-angle-brackets (s)
7245 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
7246 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
7248 (defun org-add-angle-brackets (s)
7249 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
7250 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
7252 (defun org-remove-double-quotes (s)
7253 (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
7254 (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
7257 ;;; Following specific links
7259 (defun org-follow-timestamp-link ()
7261 ((org-at-date-range-p t)
7262 (let ((org-agenda-start-on-weekday)
7263 (t1 (match-string 1))
7264 (t2 (match-string 2)))
7265 (setq t1 (time-to-days (org-time-string-to-time t1))
7266 t2 (time-to-days (org-time-string-to-time t2)))
7267 (org-agenda-list nil t1 (1+ (- t2 t1)))))
7268 ((org-at-timestamp-p t)
7269 (org-agenda-list nil (time-to-days (org-time-string-to-time
7270 (substring (match-string 1) 0 10)))
7272 (t (error "This should not happen"))))
7275 ;;; Following file links
7276 (defvar org-wait nil)
7277 (defun org-open-file (path &optional in-emacs line search)
7278 "Open the file at PATH.
7279 First, this expands any special file name abbreviations. Then the
7280 configuration variable `org-file-apps' is checked if it contains an
7281 entry for this file type, and if yes, the corresponding command is launched.
7282 If no application is found, Emacs simply visits the file.
7283 With optional argument IN-EMACS, Emacs will visit the file.
7284 Optional LINE specifies a line to go to, optional SEARCH a string to
7285 search for. If LINE or SEARCH is given, the file will always be
7287 If the file does not exist, an error is thrown."
7288 (setq in-emacs (or in-emacs line search))
7289 (let* ((file (if (equal path "")
7291 (substitute-in-file-name (expand-file-name path))))
7292 (apps (append org-file-apps (org-default-apps)))
7293 (remp (and (assq 'remote apps) (org-file-remote-p file)))
7294 (dirp (if remp nil (file-directory-p file)))
7295 (dfile (downcase file))
7296 (old-buffer (current-buffer))
7298 (old-mode major-mode)
7300 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
7301 (setq ext (match-string 1 dfile))
7302 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
7303 (setq ext (match-string 1 dfile))))
7306 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
7307 (and dirp (cdr (assoc 'directory apps)))
7308 (cdr (assoc ext apps))
7309 (cdr (assoc t apps)))))
7310 (when (eq cmd 'mailcap)
7312 (mailcap-parse-mailcaps)
7313 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
7314 (command (mailcap-mime-info mime-type)))
7315 (if (stringp command)
7317 (setq cmd 'emacs))))
7318 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
7319 (not (file-exists-p file))
7320 (not org-open-non-existing-files))
7321 (error "No such file: %s" file))
7323 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
7324 ;; Remove quotes around the file name - we'll use shell-quote-argument.
7325 (while (string-match "['\"]%s['\"]" cmd)
7326 (setq cmd (replace-match "%s" t t cmd)))
7327 (while (string-match "%s" cmd)
7328 (setq cmd (replace-match
7329 (save-match-data (shell-quote-argument file))
7331 (save-window-excursion
7332 (start-process-shell-command cmd nil cmd)
7333 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
7337 (funcall (cdr (assq 'file org-link-frame-setup)) file)
7339 (if line (goto-line line)
7340 (if search (org-link-search search))))
7343 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
7344 (and (org-mode-p) (eq old-mode 'org-mode)
7345 (or (not (equal old-buffer (current-buffer)))
7346 (not (equal old-pos (point))))
7347 (org-mark-ring-push old-pos old-buffer))))
7349 (defun org-default-apps ()
7350 "Return the default applications for this operating system."
7352 ((eq system-type 'darwin)
7353 org-file-apps-defaults-macosx)
7354 ((eq system-type 'windows-nt)
7355 org-file-apps-defaults-windowsnt)
7356 (t org-file-apps-defaults-gnu)))
7358 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
7359 (defun org-file-remote-p (file)
7360 "Test whether FILE specifies a location on a remote system.
7361 Return non-nil if the location is indeed remote.
7363 For example, the filename \"/user@host:/foo\" specifies a location
7364 on the system \"/user@host:\"."
7365 (cond ((fboundp 'file-remote-p)
7366 (file-remote-p file))
7367 ((fboundp 'tramp-handle-file-remote-p)
7368 (tramp-handle-file-remote-p file))
7369 ((and (boundp 'ange-ftp-name-format)
7370 (string-match (car ange-ftp-name-format) file))
7377 (defun org-get-org-file ()
7378 "Read a filename, with default directory `org-directory'."
7379 (let ((default (or org-default-notes-file remember-data-file)))
7380 (read-file-name (format "File name [%s]: " default)
7381 (file-name-as-directory org-directory)
7384 (defun org-notes-order-reversed-p ()
7385 "Check if the current file should receive notes in reversed order."
7387 ((not org-reverse-note-order) nil)
7388 ((eq t org-reverse-note-order) t)
7389 ((not (listp org-reverse-note-order)) nil)
7391 (let ((all org-reverse-note-order)
7393 (while (setq entry (pop all))
7394 (if (string-match (car entry) buffer-file-name)
7395 (throw 'exit (cdr entry))))
7398 (defvar org-refile-target-table nil
7399 "The list of refile targets, created by `org-refile'.")
7401 (defvar org-agenda-new-buffers nil
7402 "Buffers created to visit agenda files.")
7404 (defun org-get-refile-targets (&optional default-buffer)
7405 "Produce a table with refile targets."
7406 (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
7407 targets txt re files f desc descre)
7408 (with-current-buffer (or default-buffer (current-buffer))
7409 (while (setq entry (pop entries))
7410 (setq files (car entry) desc (cdr entry))
7412 ((null files) (setq files (list (current-buffer))))
7413 ((eq files 'org-agenda-files)
7414 (setq files (org-agenda-files 'unrestricted)))
7415 ((and (symbolp files) (fboundp files))
7416 (setq files (funcall files)))
7417 ((and (symbolp files) (boundp files))
7418 (setq files (symbol-value files))))
7419 (if (stringp files) (setq files (list files)))
7421 ((eq (car desc) :tag)
7422 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
7423 ((eq (car desc) :todo)
7424 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
7425 ((eq (car desc) :regexp)
7426 (setq descre (cdr desc)))
7427 ((eq (car desc) :level)
7428 (setq descre (concat "^\\*\\{" (number-to-string
7429 (if org-odd-levels-only
7430 (1- (* 2 (cdr desc)))
7433 ((eq (car desc) :maxlevel)
7434 (setq descre (concat "^\\*\\{1," (number-to-string
7435 (if org-odd-levels-only
7436 (1- (* 2 (cdr desc)))
7439 (t (error "Bad refiling target description %s" desc)))
7440 (while (setq f (pop files))
7442 (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
7443 (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
7447 (goto-char (point-min))
7448 (while (re-search-forward descre nil t)
7449 (goto-char (point-at-bol))
7450 (when (looking-at org-complex-heading-regexp)
7451 (setq txt (match-string 4)
7452 re (concat "^" (regexp-quote
7453 (buffer-substring (match-beginning 1)
7455 (if (match-end 5) (setq re (concat re "[ \t]+"
7457 (match-string 5)))))
7458 (setq re (concat re "[ \t]*$"))
7459 (when org-refile-use-outline-path
7460 (setq txt (mapconcat 'identity
7462 (if (eq org-refile-use-outline-path 'file)
7463 (list (file-name-nondirectory
7464 (buffer-file-name (buffer-base-buffer))))
7465 (if (eq org-refile-use-outline-path 'full-file-path)
7466 (list (buffer-file-name (buffer-base-buffer)))))
7467 (org-get-outline-path)
7470 (push (list txt f re (point)) targets))
7471 (goto-char (point-at-eol))))))))
7472 (nreverse targets))))
7474 (defun org-get-outline-path ()
7475 "Return the outline path to the current entry, as a list."
7478 (while (org-up-heading-safe)
7479 (when (looking-at org-complex-heading-regexp)
7480 (push (org-match-string-no-properties 4) rtn)))
7483 (defvar org-refile-history nil
7484 "History for refiling operations.")
7486 (defun org-refile (&optional goto default-buffer)
7487 "Move the entry at point to another heading.
7488 The list of target headings is compiled using the information in
7489 `org-refile-targets', which see. This list is created before each use
7490 and will therefore always be up-to-date.
7492 At the target location, the entry is filed as a subitem of the target heading.
7493 Depending on `org-reverse-note-order', the new subitem will either be the
7494 first of the last subitem.
7496 With prefix arg GOTO, the command will only visit the target location,
7497 not actually move anything.
7498 With a double prefix `C-c C-c', go to the location where the last refiling
7499 operation has put the subtree."
7501 (let* ((cbuf (current-buffer))
7502 (filename (buffer-file-name (buffer-base-buffer cbuf)))
7503 pos it nbuf file re level reversed)
7504 (if (equal goto '(16))
7505 (org-refile-goto-last-stored)
7506 (when (setq it (org-refile-get-location
7507 (if goto "Goto: " "Refile to: ") default-buffer))
7508 (setq file (nth 1 it)
7511 (setq nbuf (or (find-buffer-visiting file)
7512 (find-file-noselect file)))
7515 (switch-to-buffer nbuf)
7517 (org-show-context 'org-goto))
7518 (org-copy-subtree 1 nil t)
7520 (set-buffer (setq nbuf (or (find-buffer-visiting file)
7521 (find-file-noselect file))))
7522 (setq reversed (org-notes-order-reversed-p))
7527 (looking-at outline-regexp)
7528 (setq level (org-get-valid-level (funcall outline-level) 1))
7531 (outline-next-heading)
7532 (or (save-excursion (outline-get-next-sibling))
7533 (org-end-of-subtree t t)
7535 (bookmark-set "org-refile-last-stored")
7536 (org-paste-subtree level))))
7538 (setq org-markers-to-move nil)
7539 (message "Entry refiled to \"%s\"" (car it)))))))
7541 (defun org-refile-goto-last-stored ()
7542 "Go to the location where the last refile was stored."
7544 (bookmark-jump "org-refile-last-stored")
7545 (message "This is the location of the last refile"))
7547 (defun org-refile-get-location (&optional prompt default-buffer)
7548 "Prompt the user for a refile location, using PROMPT."
7549 (let ((org-refile-targets org-refile-targets)
7550 (org-refile-use-outline-path org-refile-use-outline-path))
7551 (setq org-refile-target-table (org-get-refile-targets default-buffer)))
7552 (unless org-refile-target-table
7553 (error "No refile targets"))
7554 (let* ((cbuf (current-buffer))
7555 (filename (buffer-file-name (buffer-base-buffer cbuf)))
7556 (fname (and filename (file-truename filename)))
7559 (if (not (equal fname (file-truename (nth 1 x))))
7560 (cons (concat (car x) " (" (file-name-nondirectory
7564 org-refile-target-table))
7565 (completion-ignore-case t))
7566 (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
7571 (defun org-find-dblock (name)
7572 "Find the first dynamic block with name NAME in the buffer.
7573 If not found, stay at current position and return nil."
7576 (goto-char (point-min))
7577 (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
7579 (match-beginning 0))))
7580 (if pos (goto-char pos))
7583 (defconst org-dblock-start-re
7584 "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
7585 "Matches the startline of a dynamic block, with parameters.")
7587 (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
7588 "Matches the end of a dyhamic block.")
7590 (defun org-create-dblock (plist)
7591 "Create a dynamic block section, with parameters taken from PLIST.
7592 PLIST must containe a :name entry which is used as name of the block."
7593 (unless (bolp) (newline))
7594 (let ((name (plist-get plist :name)))
7595 (insert "#+BEGIN: " name)
7597 (if (eq (car plist) :name)
7598 (setq plist (cddr plist))
7599 (insert " " (prin1-to-string (pop plist)))))
7600 (insert "\n\n#+END:\n")
7601 (beginning-of-line -2)))
7603 (defun org-prepare-dblock ()
7604 "Prepare dynamic block for refresh.
7605 This empties the block, puts the cursor at the insert position and returns
7606 the property list including an extra property :name with the block name."
7607 (unless (looking-at org-dblock-start-re)
7608 (error "Not at a dynamic block"))
7609 (let* ((begdel (1+ (match-end 0)))
7610 (name (org-no-properties (match-string 1)))
7611 (params (append (list :name name)
7612 (read (concat "(" (match-string 3) ")")))))
7613 (unless (re-search-forward org-dblock-end-re nil t)
7614 (error "Dynamic block not terminated"))
7617 (list :content (buffer-substring
7618 begdel (match-beginning 0)))))
7619 (delete-region begdel (match-beginning 0))
7624 (defun org-map-dblocks (&optional command)
7625 "Apply COMMAND to all dynamic blocks in the current buffer.
7626 If COMMAND is not given, use `org-update-dblock'."
7627 (let ((cmd (or command 'org-update-dblock))
7630 (goto-char (point-min))
7631 (while (re-search-forward org-dblock-start-re nil t)
7632 (goto-char (setq pos (match-beginning 0)))
7635 (error (message "Error during update of dynamic block")))
7637 (unless (re-search-forward org-dblock-end-re nil t)
7638 (error "Dynamic block not terminated"))))))
7640 (defun org-dblock-update (&optional arg)
7641 "User command for updating dynamic blocks.
7642 Update the dynamic block at point. With prefix ARG, update all dynamic
7643 blocks in the buffer."
7646 (org-update-all-dblocks)
7647 (or (looking-at org-dblock-start-re)
7648 (org-beginning-of-dblock))
7649 (org-update-dblock)))
7651 (defun org-update-dblock ()
7652 "Update the dynamic block at point
7653 This means to empty the block, parse for parameters and then call
7654 the correct writing function."
7655 (save-window-excursion
7656 (let* ((pos (point))
7657 (line (org-current-line))
7658 (params (org-prepare-dblock))
7659 (name (plist-get params :name))
7660 (cmd (intern (concat "org-dblock-write:" name))))
7661 (message "Updating dynamic block `%s' at line %d..." name line)
7662 (funcall cmd params)
7663 (message "Updating dynamic block `%s' at line %d...done" name line)
7666 (defun org-beginning-of-dblock ()
7667 "Find the beginning of the dynamic block at point.
7668 Error if there is no scuh block at point."
7672 (if (and (re-search-backward org-dblock-start-re nil t)
7673 (setq beg (match-beginning 0))
7674 (re-search-forward org-dblock-end-re nil t)
7675 (> (match-end 0) pos))
7678 (error "Not in a dynamic block"))))
7680 (defun org-update-all-dblocks ()
7681 "Update all dynamic blocks in the buffer.
7682 This function can be used in a hook."
7684 (org-map-dblocks 'org-update-dblock)))
7689 (defconst org-additional-option-like-keywords
7690 '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
7691 "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
7692 "BEGIN_EXAMPLE" "END_EXAMPLE"))
7694 (defcustom org-structure-template-alist
7696 ("s" "#+begin_src ?\n\n#+end_src")
7697 ("e" "#+begin_example\n?\n#+end_example")
7698 ("q" "#+begin_quote\n?\n#+end_quote")
7699 ("v" "#+begin_verse\n?\n#+end_verse")
7700 ("l" "#+begin_latex\n?\n#+end_latex")
7702 ("h" "#+begin_html\n?\n#+end_html")
7704 ("a" "#+begin_ascii\n?\n#+end_ascii")
7706 ("i" "#+include: %file ?")
7708 "Structure completion elements.
7709 This is a list of abbreviation keys and values. The value gets inserted
7710 it you type @samp{.} followed by the key and then the completion key,
7711 usually `M-TAB'. %file will be replaced by a file name after prompting
7712 for the file uning completion.
7713 This is an experimental feature, it is undecided if it is going to stay in."
7714 :group 'org-completion
7716 (string :tag "Key") (string :tag "Template")))
7718 (defun org-complete-expand-structure-template (start cell)
7719 "Expand a structure template."
7720 (let ((rpl (nth 1 cell)))
7721 (delete-region start (point))
7722 (when (string-match "\\`#\\+" rpl)
7725 ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
7726 (delete-region (point-at-bol) (point)))
7728 (setq start (point))
7729 (if (string-match "%file" rpl)
7730 (setq rpl (replace-match
7734 (abbreviate-file-name (read-file-name "Include file: ")))
7738 (if (re-search-backward "\\?" start t) (delete-char 1))))
7741 (defun org-complete (&optional arg)
7742 "Perform completion on word at point.
7743 At the beginning of a headline, this completes TODO keywords as given in
7744 `org-todo-keywords'.
7745 If the current word is preceded by a backslash, completes the TeX symbols
7746 that are supported for HTML support.
7747 If the current word is preceded by \"#+\", completes special words for
7748 setting file options.
7749 In the line after \"#+STARTUP:, complete valid keywords.\"
7750 At all other locations, this simply calls the value of
7751 `org-completion-fallback-command'."
7753 (org-without-partial-completion
7757 (beg1 (save-excursion
7758 (skip-chars-backward (org-re "[:alnum:]_@"))
7760 (beg (save-excursion
7761 (skip-chars-backward "a-zA-Z0-9_:$")
7763 (confirm (lambda (x) (stringp (car x))))
7764 (searchhead (equal (char-before beg) ?*))
7766 (when (and (equal (char-before beg1) ?.)
7767 (setq a (assoc (buffer-substring beg1 (point))
7768 org-structure-template-alist)))
7769 (org-complete-expand-structure-template (1- beg1) a)
7771 (tag (and (equal (char-before beg1) ?:)
7772 (equal (char-after (point-at-bol)) ?*)))
7773 (prop (and (equal (char-before beg1) ?:)
7774 (not (equal (char-after (point-at-bol)) ?*))))
7775 (texp (equal (char-before beg) ?\\))
7776 (link (equal (char-before beg) ?\[))
7777 (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
7780 (startup (string-match "^#\\+STARTUP:.*"
7781 (buffer-substring (point-at-bol) (point))))
7782 (completion-ignore-case opt)
7792 (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
7793 (cons (match-string 2 x) (match-string 1 x)))
7794 (org-split-string (org-get-current-options) "\n"))
7795 (mapcar 'list org-additional-option-like-keywords)))
7797 (setq type :startup)
7798 org-startup-options)
7799 (link (append org-link-abbrev-alist-local
7800 org-link-abbrev-alist))
7804 ((string-match "\\`\\*+[ \t]+\\'"
7805 (buffer-substring (point-at-bol) beg))
7807 (mapcar 'list org-todo-keywords-1))
7809 (setq type :searchhead)
7811 (goto-char (point-min))
7812 (while (re-search-forward org-todo-line-regexp nil t)
7814 (org-make-org-heading-search-string
7815 (match-string 3) t))
7818 (tag (setq type :tag beg beg1)
7819 (or org-tag-alist (org-get-buffer-tags)))
7820 (prop (setq type :prop beg beg1)
7821 (mapcar 'list (org-buffer-property-keys nil t t)))
7823 (call-interactively org-completion-fallback-command)
7824 (throw 'exit nil)))))
7825 (pattern (buffer-substring-no-properties beg end))
7826 (completion (try-completion pattern table confirm)))
7827 (cond ((eq completion t)
7828 (if (not (assoc (upcase pattern) table))
7829 (message "Already complete")
7830 (if (and (equal type :opt)
7831 (not (member (car (assoc (upcase pattern) table))
7832 org-additional-option-like-keywords)))
7833 (insert (substring (cdr (assoc (upcase pattern) table))
7835 (if (memq type '(:tag :prop)) (insert ":")))))
7837 (message "Can't find completion for \"%s\"" pattern)
7839 ((not (string= pattern completion))
7840 (delete-region beg end)
7841 (if (string-match " +$" completion)
7842 (setq completion (replace-match "" t t completion)))
7844 (if (get-buffer-window "*Completions*")
7845 (delete-window (get-buffer-window "*Completions*")))
7846 (if (assoc completion table)
7847 (if (eq type :todo) (insert " ")
7848 (if (memq type '(:tag :prop)) (insert ":"))))
7849 (if (and (equal type :opt) (assoc completion table))
7850 (message "%s" (substitute-command-keys
7851 "Press \\[org-complete] again to insert example settings"))))
7853 (message "Making completion list...")
7854 (let ((list (sort (all-completions pattern table confirm)
7856 (with-output-to-temp-buffer "*Completions*"
7858 ;; Protection needed for XEmacs and emacs 21
7859 (display-completion-list list pattern)
7860 (error (display-completion-list list)))))
7861 (message "Making completion list...%s" "done")))))))
7863 ;;;; TODO, DEADLINE, Comments
7865 (defun org-toggle-comment ()
7866 "Change the COMMENT state of an entry."
7869 (org-back-to-heading)
7870 (let (case-fold-search)
7871 (if (looking-at (concat outline-regexp
7872 "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
7873 (replace-match "" t t nil 1)
7874 (if (looking-at outline-regexp)
7876 (goto-char (match-end 0))
7877 (insert org-comment-string " ")))))))
7879 (defvar org-last-todo-state-is-todo nil
7880 "This is non-nil when the last TODO state change led to a TODO state.
7881 If the last change removed the TODO tag or switched to DONE, then
7884 (defvar org-setting-tags nil) ; dynamically skiped
7886 (defun org-parse-local-options (string var)
7887 "Parse STRING for startup setting relevant for variable VAR."
7888 (let ((rtn (symbol-value var))
7891 (if (or (not string) (not (string-match "\\S-" string)))
7893 (setq opts (delq nil (mapcar (lambda (x)
7894 (setq e (assoc x org-startup-options))
7895 (if (eq (nth 1 e) var) e nil))
7896 (org-split-string string "[ \t]+"))))
7900 (while (setq e (pop opts))
7902 (setq rtn (nth 2 e))
7903 (if (not (listp rtn)) (setq rtn nil))
7904 (push (nth 2 e) rtn)))
7907 (defvar org-blocker-hook nil
7908 "Hook for functions that are allowed to block a state change.
7910 Each function gets as its single argument a property list, see
7911 `org-trigger-hook' for more information about this list.
7913 If any of the functions in this hook returns nil, the state change
7916 (defvar org-trigger-hook nil
7917 "Hook for functions that are triggered by a state change.
7919 Each function gets as its single argument a property list with at least
7920 the following elements:
7922 (:type type-of-change :position pos-at-entry-start
7923 :from old-state :to new-state)
7925 Depending on the type, more properties may be present.
7927 This mechanism is currently implemented for:
7931 :type todo-state-change
7932 :from previous state (keyword as a string), or nil
7933 :to new state (keyword as a string), or nil")
7936 (defun org-todo (&optional arg)
7937 "Change the TODO state of an item.
7938 The state of an item is given by a keyword at the start of the heading,
7940 *** TODO Write paper
7943 The different keywords are specified in the variable `org-todo-keywords'.
7944 By default the available states are \"TODO\" and \"DONE\".
7945 So for this example: when the item starts with TODO, it is changed to DONE.
7946 When it starts with DONE, the DONE is removed. And when neither TODO nor
7947 DONE are present, add TODO at the beginning of the heading.
7949 With C-u prefix arg, use completion to determine the new state.
7950 With numeric prefix arg, switch to that state.
7952 For calling through lisp, arg is also interpreted in the following way:
7953 'none -> empty state
7954 \"\"(empty string) -> switch to empty state
7955 'done -> switch to DONE
7956 'nextset -> switch to the next set of keywords
7957 'previousset -> switch to the previous set of keywords
7958 \"WAITING\" -> switch to the specified keyword, but only if it
7959 really is a member of `org-todo-keywords'."
7963 (org-back-to-heading)
7964 (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
7965 (or (looking-at (concat " +" org-todo-regexp " *"))
7967 (let* ((match-data (match-data))
7968 (startpos (point-at-bol))
7969 (logging (save-match-data (org-entry-get nil "LOGGING" t)))
7970 (org-log-done org-log-done)
7971 (org-log-repeat org-log-repeat)
7972 (org-todo-log-states org-todo-log-states)
7973 (this (match-string 1))
7974 (hl-pos (match-beginning 0))
7975 (head (org-get-todo-sequence-head this))
7976 (ass (assoc head org-todo-kwd-alist))
7977 (interpret (nth 1 ass))
7978 (done-word (nth 3 ass))
7979 (final-done-word (nth 4 ass))
7980 (last-state (or this ""))
7981 (completion-ignore-case t)
7982 (member (member this org-todo-keywords-1))
7985 ((and org-todo-key-trigger
7986 (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
7987 (and (not arg) org-use-fast-todo-selection
7988 (not (eq org-use-fast-todo-selection 'prefix)))))
7989 ;; Use fast selection
7990 (org-fast-todo-selection))
7991 ((and (equal arg '(4))
7992 (or (not org-use-fast-todo-selection)
7993 (not org-todo-key-trigger)))
7994 ;; Read a state with completion
7995 (completing-read "State: " (mapcar (lambda(x) (list x))
7996 org-todo-keywords-1)
8000 (if tail (car tail) nil)
8001 (car org-todo-keywords-1)))
8003 (if (equal member org-todo-keywords-1)
8006 (nth (- (length org-todo-keywords-1) (length tail) 2)
8007 org-todo-keywords-1)
8008 (org-last org-todo-keywords-1))))
8009 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
8010 (setq arg nil))) ; hack to fall back to cycling
8012 ;; user or caller requests a specific state
8014 ((equal arg "") nil)
8015 ((eq arg 'none) nil)
8016 ((eq arg 'done) (or done-word (car org-done-keywords)))
8018 (or (car (cdr (member head org-todo-heads)))
8019 (car org-todo-heads)))
8020 ((eq arg 'previousset)
8021 (let ((org-todo-heads (reverse org-todo-heads)))
8022 (or (car (cdr (member head org-todo-heads)))
8023 (car org-todo-heads))))
8024 ((car (member arg org-todo-keywords-1)))
8025 ((nth (1- (prefix-numeric-value arg))
8026 org-todo-keywords-1))))
8027 ((null member) (or head (car org-todo-keywords-1)))
8028 ((equal this final-done-word) nil) ;; -> make empty
8029 ((null tail) nil) ;; -> first entry
8030 ((eq interpret 'sequence)
8032 ((memq interpret '(type priority))
8033 (if (eq this-command last-command)
8035 (if (> (length tail) 0)
8036 (or done-word (car org-done-keywords))
8039 (next (if state (concat " " state " ") " "))
8040 (change-plist (list :type 'todo-state-change :from this :to state
8041 :position startpos))
8043 (when org-blocker-hook
8044 (unless (save-excursion
8046 (run-hook-with-args-until-failure
8047 'org-blocker-hook change-plist)))
8049 (error "TODO state change from %s to %s blocked" this state)
8051 (message "TODO state change from %s to %s blocked" this state)
8052 (throw 'exit nil))))
8053 (store-match-data match-data)
8054 (replace-match next t t)
8055 (unless (pos-visible-in-window-p hl-pos)
8056 (message "TODO state changed to %s" (org-trim next)))
8058 (setq head (org-get-todo-sequence-head state)
8059 ass (assoc head org-todo-kwd-alist)
8060 interpret (nth 1 ass)
8061 done-word (nth 3 ass)
8062 final-done-word (nth 4 ass)))
8063 (when (memq arg '(nextset previousset))
8064 (message "Keyword-Set %d/%d: %s"
8065 (- (length org-todo-sets) -1
8066 (length (memq (assoc state org-todo-sets) org-todo-sets)))
8067 (length org-todo-sets)
8068 (mapconcat 'identity (assoc state org-todo-sets) " ")))
8069 (setq org-last-todo-state-is-todo
8070 (not (member state org-done-keywords)))
8071 (setq now-done-p (and (member state org-done-keywords)
8072 (not (member this org-done-keywords))))
8073 (and logging (org-local-logging logging))
8074 (when (and (or org-todo-log-states org-log-done)
8075 (not (memq arg '(nextset previousset))))
8076 ;; we need to look at recording a time and note
8077 (setq dolog (or (nth 1 (assoc state org-todo-log-states))
8078 (nth 2 (assoc this org-todo-log-states))))
8080 (member state org-not-done-keywords)
8081 (not (member this org-not-done-keywords)))
8082 ;; This is now a todo state and was not one before
8083 ;; If there was a CLOSED time stamp, get rid of it.
8084 (org-add-planning-info nil nil 'closed))
8085 (when (and now-done-p org-log-done)
8086 ;; It is now done, and it was not done before
8087 (org-add-planning-info 'closed (org-current-time))
8088 (if (and (not dolog) (eq 'note org-log-done))
8089 (org-add-log-setup 'done state 'findpos 'note)))
8090 (when (and state dolog)
8091 ;; This is a non-nil state, and we need to log it
8092 (org-add-log-setup 'state state 'findpos dolog)))
8093 ;; Fixup tag positioning
8094 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
8095 (run-hooks 'org-after-todo-state-change-hook)
8096 (if (and arg (not (member state org-done-keywords)))
8097 (setq head (org-get-todo-sequence-head state)))
8098 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
8099 ;; Do we need to trigger a repeat?
8100 (when now-done-p (org-auto-repeat-maybe state))
8101 ;; Fixup cursor location if close to the keyword
8102 (if (and (outline-on-heading-p)
8104 (save-excursion (beginning-of-line 1)
8105 (looking-at org-todo-line-regexp))
8106 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
8108 (goto-char (or (match-end 2) (match-end 1)))
8110 (when org-trigger-hook
8112 (run-hook-with-args 'org-trigger-hook change-plist)))))))
8114 (defun org-local-logging (value)
8115 "Get logging settings from a property VALUE."
8117 ;; directly set the variables, they are already local.
8118 (setq org-log-done nil
8120 org-todo-log-states nil)
8121 (setq words (org-split-string value))
8122 (while (setq w (pop words))
8124 ((setq a (assoc w org-startup-options))
8125 (and (member (nth 1 a) '(org-log-done org-log-repeat))
8126 (set (nth 1 a) (nth 2 a))))
8127 ((setq a (org-extract-log-state-settings w))
8128 (and (member (car a) org-todo-keywords-1)
8129 (push a org-todo-log-states)))))))
8131 (defun org-get-todo-sequence-head (kwd)
8132 "Return the head of the TODO sequence to which KWD belongs.
8133 If KWD is not set, check if there is a text property remembering the
8138 (or (get-text-property (point-at-bol) 'org-todo-head)
8140 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
8141 nil (point-at-eol)))
8142 (get-text-property p 'org-todo-head))))
8143 ((not (member kwd org-todo-keywords-1))
8144 (car org-todo-keywords-1))
8145 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
8147 (defun org-fast-todo-selection ()
8148 "Fast TODO keyword selection with single keys.
8149 Returns the new TODO keyword, or nil if no state change should occur."
8150 (let* ((fulltable org-todo-key-alist)
8151 (done-keywords org-done-keywords) ;; needed for the faces.
8152 (maxlen (apply 'max (mapcar
8154 (if (stringp (car x)) (string-width (car x)) 0))
8157 (fwidth (+ maxlen 3 1 3))
8158 (ncol (/ (- (window-width) 4) fwidth))
8161 (save-window-excursion
8163 (set-buffer (get-buffer-create " *Org todo*"))
8164 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
8166 (org-set-local 'org-done-keywords done-keywords)
8167 (setq tbl fulltable cnt 0)
8168 (while (setq e (pop tbl))
8170 ((equal e '(:startgroup))
8171 (push '() groups) (setq ingroup t)
8172 (when (not (= cnt 0))
8176 ((equal e '(:endgroup))
8177 (setq ingroup nil cnt 0)
8180 (setq tg (car e) c (cdr e))
8181 (if ingroup (push tg (car groups)))
8182 (setq tg (org-add-props tg nil 'face
8183 (org-get-todo-face tg)))
8184 (if (and (= cnt 0) (not ingroup)) (insert " "))
8185 (insert "[" c "] " tg (make-string
8186 (- fwidth 4 (length tg)) ?\ ))
8187 (when (= (setq cnt (1+ cnt)) ncol)
8189 (if ingroup (insert " "))
8192 (goto-char (point-min))
8193 (if (and (not expert) (fboundp 'fit-window-to-buffer))
8194 (fit-window-to-buffer))
8195 (message "[a-z..]:Set [SPC]:clear")
8196 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
8199 (and (= c ?q) (not (rassoc c fulltable))))
8202 ((setq e (rassoc c fulltable) tg (car e))
8204 (t (setq quit-flag t))))))
8206 (defun org-entry-is-todo-p ()
8207 (member (org-get-todo-state) org-not-done-keywords))
8209 (defun org-entry-is-done-p ()
8210 (member (org-get-todo-state) org-done-keywords))
8212 (defun org-get-todo-state ()
8214 (org-back-to-heading t)
8215 (and (looking-at org-todo-line-regexp)
8219 (defun org-at-date-range-p (&optional inactive-ok)
8220 "Is the cursor inside a date range?"
8224 (let ((pos (point)))
8225 (skip-chars-backward "^[<\r\n")
8226 (skip-chars-backward "<[")
8227 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
8228 (>= (match-end 0) pos)
8230 (skip-chars-backward "^<[\r\n")
8231 (skip-chars-backward "<[")
8232 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
8233 (>= (match-end 0) pos)
8237 (defun org-get-repeat ()
8238 "Check if tere is a deadline/schedule with repeater in this entry."
8241 (org-back-to-heading t)
8242 (if (re-search-forward
8243 org-repeat-re (save-excursion (outline-next-heading) (point)) t)
8244 (match-string 1)))))
8246 (defvar org-last-changed-timestamp)
8247 (defvar org-log-post-message)
8248 (defvar org-log-note-purpose)
8249 (defvar org-log-note-how)
8250 (defun org-auto-repeat-maybe (done-word)
8251 "Check if the current headline contains a repeated deadline/schedule.
8252 If yes, set TODO state back to what it was and change the base date
8253 of repeating deadline/scheduled time stamps to new date.
8254 This function is run automatically after each state change to a DONE state."
8255 ;; last-state is dynamically scoped into this function
8256 (let* ((repeat (org-get-repeat))
8257 (aa (assoc last-state org-todo-kwd-alist))
8258 (interpret (nth 1 aa))
8260 (whata '(("d" . day) ("m" . month) ("y" . year)))
8261 (msg "Entry repeats: ")
8263 (org-todo-log-states nil)
8264 (nshiftmax 10) (nshift 0)
8265 re type n what ts mb0 time)
8267 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
8268 (org-todo (if (eq interpret 'type) last-state head))
8269 (when org-log-repeat
8270 (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
8271 (memq 'org-add-log-note post-command-hook))
8272 ;; OK, we are already setup for some record
8273 (if (eq org-log-repeat 'note)
8274 ;; make sure we take a note, not only a time stamp
8275 (setq org-log-note-how 'note))
8276 ;; Set up for taking a record
8277 (org-add-log-setup 'state (or done-word (car org-done-keywords))
8278 'findpos org-log-repeat)))
8279 (org-back-to-heading t)
8280 (org-add-planning-info nil nil 'closed)
8281 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
8282 org-deadline-time-regexp "\\)\\|\\("
8283 org-ts-regexp "\\)"))
8284 (while (re-search-forward
8285 re (save-excursion (outline-next-heading) (point)) t)
8286 (setq type (if (match-end 1) org-scheduled-string
8287 (if (match-end 3) org-deadline-string "Plain:"))
8288 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
8289 mb0 (match-beginning 0))
8290 (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
8291 (setq n (string-to-number (match-string 2 ts))
8292 what (match-string 3 ts))
8293 (if (equal what "w") (setq n (* n 7) what "d"))
8294 ;; Preparation, see if we need to modify the start date for the change
8296 (setq time (save-match-data (org-time-string-to-time ts)))
8298 ((equal (match-string 1 ts) ".")
8299 ;; Shift starting date to today
8300 (org-timestamp-change
8301 (- (time-to-days (current-time)) (time-to-days time))
8303 ((equal (match-string 1 ts) "+")
8304 (while (or (= nshift 0)
8305 (<= (time-to-days time) (time-to-days (current-time))))
8306 (when (= (incf nshift) nshiftmax)
8307 (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
8309 (org-timestamp-change n (cdr (assoc what whata)))
8310 (org-at-timestamp-p t)
8311 (setq ts (match-string 1))
8312 (setq time (save-match-data (org-time-string-to-time ts))))
8313 (org-timestamp-change (- n) (cdr (assoc what whata)))
8314 ;; rematch, so that we have everything in place for the real shift
8315 (org-at-timestamp-p t)
8316 (setq ts (match-string 1))
8317 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
8318 (org-timestamp-change n (cdr (assoc what whata)))
8319 (setq msg (concat msg type org-last-changed-timestamp " "))))
8320 (setq org-log-post-message msg)
8321 (message "%s" msg))))
8323 (defun org-show-todo-tree (arg)
8324 "Make a compact tree which shows all headlines marked with TODO.
8325 The tree will show the lines where the regexp matches, and all higher
8326 headlines above the match.
8327 With a \\[universal-argument] prefix, also show the DONE entries.
8328 With a numeric prefix N, construct a sparse tree for the Nth element
8329 of `org-todo-keywords-1'."
8331 (let ((case-fold-search nil)
8333 (cond ((null arg) org-not-done-regexp)
8335 (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
8336 (mapcar 'list org-todo-keywords-1))))
8338 (mapconcat 'identity (org-split-string kwd "|") "\\|")
8340 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
8341 (regexp-quote (nth (1- (prefix-numeric-value arg))
8342 org-todo-keywords-1)))
8343 (t (error "Invalid prefix argument: %s" arg)))))
8344 (message "%d TODO entries found"
8345 (org-occur (concat "^" outline-regexp " *" kwd-re )))))
8347 (defun org-deadline (&optional remove)
8348 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
8349 With argument REMOVE, remove any deadline from the item."
8353 (org-remove-timestamp-with-keyword org-deadline-string)
8354 (message "Item no longer has a deadline."))
8355 (org-add-planning-info 'deadline nil 'closed)))
8357 (defun org-schedule (&optional remove)
8358 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
8359 With argument REMOVE, remove any scheduling date from the item."
8363 (org-remove-timestamp-with-keyword org-scheduled-string)
8364 (message "Item is no longer scheduled."))
8365 (org-add-planning-info 'scheduled nil 'closed)))
8367 (defun org-remove-timestamp-with-keyword (keyword)
8368 "Remove all time stamps with KEYWORD in the current entry."
8369 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
8372 (org-back-to-heading t)
8374 (org-end-of-subtree t t)
8375 (while (re-search-backward re beg t)
8377 (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
8378 (equal (char-before) ?\ ))
8379 (backward-delete-char 1)
8380 (if (string-match "^[ \t]*$" (buffer-substring
8381 (point-at-bol) (point-at-eol)))
8382 (delete-region (point-at-bol)
8383 (min (point-max) (1+ (point-at-eol))))))))))
8385 (defun org-add-planning-info (what &optional time &rest remove)
8386 "Insert new timestamp with keyword in the line directly after the headline.
8387 WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
8388 If non is given, the user is prompted for a date.
8389 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
8392 (let (org-time-was-given org-end-time-was-given ts
8393 end default-time default-input)
8395 (when (and (not time) (memq what '(scheduled deadline)))
8396 ;; Try to get a default date/time from existing timestamp
8398 (org-back-to-heading t)
8399 (setq end (save-excursion (outline-next-heading) (point)))
8400 (when (re-search-forward (if (eq what 'scheduled)
8401 org-scheduled-time-regexp
8402 org-deadline-time-regexp)
8404 (setq ts (match-string 1)
8406 (apply 'encode-time (org-parse-time-string ts))
8407 default-input (and ts (org-get-compact-tod ts))))))
8409 ;; If necessary, get the time from the user
8410 (setq time (or time (org-read-date nil 'to-time nil nil
8411 default-time default-input))))
8413 (when (and org-insert-labeled-timestamps-at-point
8414 (member what '(scheduled deadline)))
8416 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
8417 (org-insert-time-stamp time org-time-was-given
8418 nil nil nil (list org-end-time-was-given))
8422 (let (col list elt ts buffer-invisibility-spec)
8423 (org-back-to-heading t)
8424 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
8425 (goto-char (match-end 1))
8426 (setq col (current-column))
8427 (goto-char (match-end 0))
8428 (if (eobp) (insert "\n") (forward-char 1))
8429 (if (and (not (looking-at outline-regexp))
8430 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
8432 (not (equal (match-string 1) org-clock-string)))
8433 (narrow-to-region (match-beginning 0) (match-end 0))
8434 (insert-before-markers "\n")
8436 (narrow-to-region (point) (point))
8437 (org-indent-to-column col))
8438 ;; Check if we have to remove something.
8439 (setq list (cons what remove))
8441 (setq elt (pop list))
8442 (goto-char (point-min))
8443 (when (or (and (eq elt 'scheduled)
8444 (re-search-forward org-scheduled-time-regexp nil t))
8445 (and (eq elt 'deadline)
8446 (re-search-forward org-deadline-time-regexp nil t))
8447 (and (eq elt 'closed)
8448 (re-search-forward org-closed-time-regexp nil t)))
8450 (if (looking-at "--+<[^>]+>") (replace-match ""))
8451 (if (looking-at " +") (replace-match ""))))
8452 (goto-char (point-max))
8455 (if (not (equal (char-before) ?\ )) " " "")
8456 (cond ((eq what 'scheduled) org-scheduled-string)
8457 ((eq what 'deadline) org-deadline-string)
8458 ((eq what 'closed) org-closed-string))
8460 (setq ts (org-insert-time-stamp
8462 (or org-time-was-given
8463 (and (eq what 'closed) org-log-done-with-time))
8465 nil nil (list org-end-time-was-given)))
8467 (goto-char (point-min))
8469 (if (and (looking-at "[ \t]+\n")
8470 (equal (char-before) ?\n))
8471 (delete-region (1- (point)) (point-at-eol)))
8474 (defvar org-log-note-marker (make-marker))
8475 (defvar org-log-note-purpose nil)
8476 (defvar org-log-note-state nil)
8477 (defvar org-log-note-how nil)
8478 (defvar org-log-note-window-configuration nil)
8479 (defvar org-log-note-return-to (make-marker))
8480 (defvar org-log-post-message nil
8481 "Message to be displayed after a log note has been stored.
8482 The auto-repeater uses this.")
8484 (defun org-add-note ()
8485 "Add a note to the current entry.
8486 This is done in the same way as adding a state change note."
8488 (org-add-log-setup 'note nil t nil))
8490 (defun org-add-log-setup (&optional purpose state findpos how)
8491 "Set up the post command hook to take a note.
8492 If this is about to TODO state change, the new state is expected in STATE.
8493 When FINDPOS is non-nil, find the correct position for the note in
8494 the current entry. If not, assume that it can be inserted at point."
8497 (org-back-to-heading t)
8498 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
8499 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
8501 (goto-char (match-end 0))
8502 (unless org-log-states-order-reversed
8503 (and (= (char-after) ?\n) (forward-char 1))
8504 (org-skip-over-state-notes)
8505 (skip-chars-backward " \t\n\r")))
8506 (move-marker org-log-note-marker (point))
8507 (setq org-log-note-purpose purpose
8508 org-log-note-state state
8509 org-log-note-how how)
8510 (add-hook 'post-command-hook 'org-add-log-note 'append)))
8512 (defun org-skip-over-state-notes ()
8513 "Skip past the list of State notes in an entry."
8514 (if (looking-at "\n[ \t]*- State") (forward-char 1))
8515 (while (looking-at "[ \t]*- State")
8518 (error (org-end-of-item)))))
8520 (defun org-add-log-note (&optional purpose)
8521 "Pop up a window for taking a note, and add this note later at point."
8522 (remove-hook 'post-command-hook 'org-add-log-note)
8523 (setq org-log-note-window-configuration (current-window-configuration))
8524 (delete-other-windows)
8525 (move-marker org-log-note-return-to (point))
8526 (switch-to-buffer (marker-buffer org-log-note-marker))
8527 (goto-char org-log-note-marker)
8528 (org-switch-to-buffer-other-window "*Org Note*")
8530 (if (memq org-log-note-how '(time state))
8531 (org-store-log-note)
8532 (let ((org-inhibit-startup t)) (org-mode))
8533 (insert (format "# Insert note for %s.
8534 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
8536 ((eq org-log-note-purpose 'clock-out) "stopped clock")
8537 ((eq org-log-note-purpose 'done) "closed todo item")
8538 ((eq org-log-note-purpose 'state)
8539 (format "state change to \"%s\"" org-log-note-state))
8540 ((eq org-log-note-purpose 'note)
8542 (t (error "This should not happen")))))
8543 (org-set-local 'org-finish-function 'org-store-log-note)))
8545 (defvar org-note-abort nil) ; dynamically scoped
8546 (defun org-store-log-note ()
8547 "Finish taking a log note, and insert it to where it belongs."
8548 (let ((txt (buffer-string))
8549 (note (cdr (assq org-log-note-purpose org-log-note-headings)))
8551 (kill-buffer (current-buffer))
8552 (while (string-match "\\`#.*\n[ \t\n]*" txt)
8553 (setq txt (replace-match "" t t txt)))
8554 (if (string-match "\\s-+\\'" txt)
8555 (setq txt (replace-match "" t t txt)))
8556 (setq lines (org-split-string txt "\n"))
8557 (when (and note (string-match "\\S-" note))
8559 (org-replace-escapes
8561 (list (cons "%u" (user-login-name))
8562 (cons "%U" user-full-name)
8563 (cons "%t" (format-time-string
8564 (org-time-stamp-format 'long 'inactive)
8566 (cons "%s" (if org-log-note-state
8567 (concat "\"" org-log-note-state "\"")
8569 (if lines (setq note (concat note " \\\\")))
8571 (when (or current-prefix-arg org-note-abort) (setq lines nil))
8574 (set-buffer (marker-buffer org-log-note-marker))
8576 (goto-char org-log-note-marker)
8577 (move-marker org-log-note-marker nil)
8579 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
8580 (indent-relative nil)
8581 (insert "- " (pop lines))
8582 (org-indent-line-function)
8583 (beginning-of-line 1)
8584 (looking-at "[ \t]*")
8585 (setq ind (concat (match-string 0) " "))
8587 (while lines (insert "\n" ind (pop lines)))))))
8588 (set-window-configuration org-log-note-window-configuration)
8589 (with-current-buffer (marker-buffer org-log-note-return-to)
8590 (goto-char org-log-note-return-to))
8591 (move-marker org-log-note-return-to nil)
8592 (and org-log-post-message (message "%s" org-log-post-message)))
8594 (defun org-sparse-tree (&optional arg)
8595 "Create a sparse tree, prompt for the details.
8596 This command can create sparse trees. You first need to select the type
8597 of match used to create the tree:
8599 t Show entries with a specific TODO keyword.
8600 T Show entries selected by a tags match.
8601 p Enter a property name and its value (both with completion on existing
8602 names/values) and show entries with that property.
8603 r Show entries matching a regular expression
8604 d Show deadlines due within `org-deadline-warning-days'."
8606 (let (ans kwd value)
8607 (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
8608 (setq ans (read-char-exclusive))
8611 (call-interactively 'org-check-deadlines))
8613 (call-interactively 'org-check-before-date))
8615 (org-show-todo-tree '(4)))
8617 (call-interactively 'org-tags-sparse-tree))
8618 ((member ans '(?p ?P))
8619 (setq kwd (completing-read "Property: "
8620 (mapcar 'list (org-buffer-property-keys))))
8621 (setq value (completing-read "Value: "
8622 (mapcar 'list (org-property-values kwd))))
8623 (unless (string-match "\\`{.*}\\'" value)
8624 (setq value (concat "\"" value "\"")))
8625 (org-tags-sparse-tree arg (concat kwd "=" value)))
8626 ((member ans '(?r ?R ?/))
8627 (call-interactively 'org-occur))
8628 (t (error "No such sparse tree command \"%c\"" ans)))))
8630 (defvar org-occur-highlights nil
8631 "List of overlays used for occur matches.")
8632 (make-variable-buffer-local 'org-occur-highlights)
8633 (defvar org-occur-parameters nil
8634 "Parameters of the active org-occur calls.
8635 This is a list, each call to org-occur pushes as cons cell,
8636 containing the regular expression and the callback, onto the list.
8637 The list can contain several entries if `org-occur' has been called
8638 several time with the KEEP-PREVIOUS argument. Otherwise, this list
8639 will only contain one set of parameters. When the highlights are
8640 removed (for example with `C-c C-c', or with the next edit (depending
8641 on `org-remove-highlights-with-change'), this variable is emptied
8643 (make-variable-buffer-local 'org-occur-parameters)
8645 (defun org-occur (regexp &optional keep-previous callback)
8646 "Make a compact tree which shows all matches of REGEXP.
8647 The tree will show the lines where the regexp matches, and all higher
8648 headlines above the match. It will also show the heading after the match,
8649 to make sure editing the matching entry is easy.
8650 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
8651 call to `org-occur' will be kept, to allow stacking of calls to this
8653 If CALLBACK is non-nil, it is a function which is called to confirm
8654 that the match should indeed be shown."
8655 (interactive "sRegexp: \nP")
8656 (unless keep-previous
8657 (org-remove-occur-highlights nil nil t))
8658 (push (cons regexp callback) org-occur-parameters)
8661 (goto-char (point-min))
8662 (if (or (not keep-previous) ; do not want to keep
8663 (not org-occur-highlights)) ; no previous matches
8666 (while (re-search-forward regexp nil t)
8667 (when (or (not callback)
8668 (save-match-data (funcall callback)))
8670 (when org-highlight-sparse-tree-matches
8671 (org-highlight-new-match (match-beginning 0) (match-end 0)))
8672 (org-show-context 'occur-tree))))
8673 (when org-remove-highlights-with-change
8674 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
8676 (unless org-sparse-tree-open-archived-trees
8677 (org-hide-archived-subtrees (point-min) (point-max)))
8678 (run-hooks 'org-occur-hook)
8680 (message "%d match(es) for regexp %s" cnt regexp))
8683 (defun org-show-context (&optional key)
8684 "Make sure point and context and visible.
8685 How much context is shown depends upon the variables
8686 `org-show-hierarchy-above', `org-show-following-heading'. and
8687 `org-show-siblings'."
8688 (let ((heading-p (org-on-heading-p t))
8689 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
8690 (following-p (org-get-alist-option org-show-following-heading key))
8691 (entry-p (org-get-alist-option org-show-entry-below key))
8692 (siblings-p (org-get-alist-option org-show-siblings key)))
8694 ;; Show heading or entry text
8695 (if (and heading-p (not entry-p))
8696 (org-flag-heading nil) ; only show the heading
8697 (and (or entry-p (org-invisible-p) (org-invisible-p2))
8698 (org-show-hidden-entry))) ; show entire entry
8700 ;; Show next sibling, or heading below text
8702 (and (if heading-p (org-goto-sibling) (outline-next-heading))
8703 (org-flag-heading nil))))
8704 (when siblings-p (org-show-siblings))
8706 ;; show all higher headings, possibly with siblings
8708 (while (and (condition-case nil
8709 (progn (org-up-heading-all 1) t)
8712 (org-flag-heading nil)
8713 (when siblings-p (org-show-siblings))))))))
8715 (defun org-reveal (&optional siblings)
8716 "Show current entry, hierarchy above it, and the following headline.
8717 This can be used to show a consistent set of context around locations
8718 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
8719 not t for the search context.
8721 With optional argument SIBLINGS, on each level of the hierarchy all
8722 siblings are shown. This repairs the tree structure to what it would
8723 look like when opened with hierarchical calls to `org-cycle'."
8725 (let ((org-show-hierarchy-above t)
8726 (org-show-following-heading t)
8727 (org-show-siblings (if siblings t org-show-siblings)))
8728 (org-show-context nil)))
8730 (defun org-highlight-new-match (beg end)
8731 "Highlight from BEG to END and mark the highlight is an occur headline."
8732 (let ((ov (org-make-overlay beg end)))
8733 (org-overlay-put ov 'face 'secondary-selection)
8734 (push ov org-occur-highlights)))
8736 (defun org-remove-occur-highlights (&optional beg end noremove)
8737 "Remove the occur highlights from the buffer.
8738 BEG and END are ignored. If NOREMOVE is nil, remove this function
8739 from the `before-change-functions' in the current buffer."
8741 (unless org-inhibit-highlight-removal
8742 (mapc 'org-delete-overlay org-occur-highlights)
8743 (setq org-occur-highlights nil)
8744 (setq org-occur-parameters nil)
8746 (remove-hook 'before-change-functions
8747 'org-remove-occur-highlights 'local))))
8751 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
8752 "Regular expression matching the priority indicator.")
8754 (defvar org-remove-priority-next-time nil)
8756 (defun org-priority-up ()
8757 "Increase the priority of the current item."
8761 (defun org-priority-down ()
8762 "Decrease the priority of the current item."
8764 (org-priority 'down))
8766 (defun org-priority (&optional action)
8767 "Change the priority of an item by ARG.
8768 ACTION can be `set', `up', `down', or a character."
8770 (setq action (or action 'set))
8771 (let (current new news have remove)
8773 (org-back-to-heading)
8774 (if (looking-at org-priority-regexp)
8775 (setq current (string-to-char (match-string 2))
8777 (setq current org-default-priority))
8779 ((or (eq action 'set)
8780 (if (featurep 'xemacs) (characterp action) (integerp action)))
8781 (if (not (eq action 'set))
8783 (message "Priority %c-%c, SPC to remove: "
8784 org-highest-priority org-lowest-priority)
8785 (setq new (read-char-exclusive)))
8786 (if (and (= (upcase org-highest-priority) org-highest-priority)
8787 (= (upcase org-lowest-priority) org-lowest-priority))
8788 (setq new (upcase new)))
8789 (cond ((equal new ?\ ) (setq remove t))
8790 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
8791 (error "Priority must be between `%c' and `%c'"
8792 org-highest-priority org-lowest-priority))))
8794 (if (and (not have) (eq last-command this-command))
8795 (setq new org-lowest-priority)
8796 (setq new (if (and org-priority-start-cycle-with-default (not have))
8797 org-default-priority (1- current)))))
8799 (if (and (not have) (eq last-command this-command))
8800 (setq new org-highest-priority)
8801 (setq new (if (and org-priority-start-cycle-with-default (not have))
8802 org-default-priority (1+ current)))))
8803 (t (error "Invalid action")))
8804 (if (or (< (upcase new) org-highest-priority)
8805 (> (upcase new) org-lowest-priority))
8807 (setq news (format "%c" new))
8810 (replace-match "" t t nil 1)
8811 (replace-match news t t nil 2))
8813 (error "No priority cookie found in line")
8814 (looking-at org-todo-line-regexp)
8817 (goto-char (match-end 2))
8818 (insert " [#" news "]"))
8819 (goto-char (match-beginning 3))
8820 (insert "[#" news "] ")))))
8821 (org-preserve-lc (org-set-tags nil 'align))
8823 (message "Priority removed")
8824 (message "Priority of current item set to %s" news))))
8827 (defun org-get-priority (s)
8828 "Find priority cookie and return priority."
8830 (if (not (string-match org-priority-regexp s))
8831 (* 1000 (- org-lowest-priority org-default-priority))
8832 (* 1000 (- org-lowest-priority
8833 (string-to-char (match-string 2 s)))))))
8837 (defun org-scan-tags (action matcher &optional todo-only)
8838 "Scan headline tags with inheritance and produce output ACTION.
8839 ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
8840 evaluated, testing if a given set of tags qualifies a headline for
8841 inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
8842 are included in the output."
8843 (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
8844 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
8846 "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
8847 (props (list 'face nil
8848 'done-face 'org-done
8850 'mouse-face 'highlight
8851 'org-not-done-regexp org-not-done-regexp
8852 'org-todo-regexp org-todo-regexp
8853 'keymap org-agenda-keymap
8855 (format "mouse-2 or RET jump to org file %s"
8856 (abbreviate-file-name
8857 (or (buffer-file-name (buffer-base-buffer))
8858 (buffer-name (buffer-base-buffer)))))))
8859 (case-fold-search nil)
8861 tags tags-list tags-alist (llast 0) rtn level category i txt
8862 todo marker entry priority)
8864 (goto-char (point-min))
8865 (when (eq action 'sparse-tree)
8867 (org-remove-occur-highlights))
8868 (while (re-search-forward re nil t)
8870 (setq todo (if (match-end 1) (match-string 2))
8871 tags (if (match-end 4) (match-string 4)))
8872 (goto-char (setq lspos (1+ (match-beginning 0))))
8873 (setq level (org-reduced-level (funcall outline-level))
8874 category (org-get-category))
8875 (setq i llast llast level)
8876 ;; remove tag lists from same and sublevels
8878 (when (setq entry (assoc i tags-alist))
8879 (setq tags-alist (delete entry tags-alist)))
8881 ;; add the next tags
8883 (setq tags (mapcar 'downcase (org-split-string tags ":"))
8885 (cons (cons level tags) tags-alist)))
8886 ;; compile tags for current headline
8888 (if org-use-tag-inheritance
8889 (apply 'append (mapcar 'cdr tags-alist))
8891 (when (and tags org-use-tag-inheritance
8892 (not (eq t org-use-tag-inheritance)))
8893 ;; selective inheritance, remove uninherited ones
8894 (setcdr (car tags-alist)
8895 (org-remove-uniherited-tags (cdar tags-alist))))
8896 (when (and (or (not todo-only) (member todo org-not-done-keywords))
8898 (or (not org-agenda-skip-archived-trees)
8899 (not (member org-archive-tag tags-list))))
8900 (and (eq action 'agenda) (org-agenda-skip))
8901 ;; list this headline
8903 (if (eq action 'sparse-tree)
8905 (and org-highlight-sparse-tree-matches
8906 (org-get-heading) (match-end 0)
8907 (org-highlight-new-match
8908 (match-beginning 0) (match-beginning 1)))
8909 (org-show-context 'tags-tree))
8910 (setq txt (org-format-agenda-item
8913 (if org-tags-match-list-sublevels
8914 (make-string (1- level) ?.) "")
8917 priority (org-get-priority txt))
8919 (setq marker (org-agenda-new-marker))
8920 (org-add-props txt props
8921 'org-marker marker 'org-hd-marker marker 'org-category category
8922 'priority priority 'type "tagsmatch")
8924 ;; if we are to skip sublevels, jump to end of subtree
8925 (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
8926 (when (and (eq action 'sparse-tree)
8927 (not org-sparse-tree-open-archived-trees))
8928 (org-hide-archived-subtrees (point-min) (point-max)))
8931 (defun org-remove-uniherited-tags (tags)
8932 "Remove all tags that are not inherited from the list TAGS."
8934 ((eq org-use-tag-inheritance t) tags)
8935 ((not org-use-tag-inheritance) nil)
8936 ((stringp org-use-tag-inheritance)
8938 (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
8940 ((listp org-use-tag-inheritance)
8941 (org-delete-all org-use-tag-inheritance tags))))
8943 (defvar todo-only) ;; dynamically scoped
8945 (defun org-tags-sparse-tree (&optional todo-only match)
8946 "Create a sparse tree according to tags string MATCH.
8947 MATCH can contain positive and negative selection of tags, like
8948 \"+WORK+URGENT-WITHBOSS\".
8949 If optional argument TODO_ONLY is non-nil, only select lines that are
8952 (org-prepare-agenda-buffers (list (current-buffer)))
8953 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
8955 (defvar org-cached-props nil)
8956 (defun org-cached-entry-get (pom property)
8957 (if (or (eq t org-use-property-inheritance)
8958 (and (stringp org-use-property-inheritance)
8959 (string-match org-use-property-inheritance property))
8960 (and (listp org-use-property-inheritance)
8961 (member property org-use-property-inheritance)))
8962 ;; Caching is not possible, check it directly
8963 (org-entry-get pom property 'inherit)
8964 ;; Get all properties, so that we can do complicated checks easily
8965 (cdr (assoc property (or org-cached-props
8966 (setq org-cached-props
8967 (org-entry-properties pom)))))))
8969 (defun org-global-tags-completion-table (&optional files)
8970 "Return the list of all tags in all agenda buffer/files."
8977 (set-buffer (find-file-noselect file))
8978 (append (org-get-buffer-tags)
8979 (mapcar (lambda (x) (if (stringp (car-safe x))
8980 (list (car-safe x)) nil))
8982 (if (and files (car files))
8984 (org-agenda-files))))))))
8986 (defun org-make-tags-matcher (match)
8987 "Create the TAGS//TODO matcher form for the selection string MATCH."
8988 ;; todo-only is scoped dynamically into this function, and the function
8989 ;; may change it it the matcher asksk for it.
8991 ;; Get a new match request, with completion
8992 (let ((org-last-tags-completion-table
8993 (org-global-tags-completion-table)))
8994 (setq match (completing-read
8995 "Match: " 'org-tags-completion-function nil nil nil
8996 'org-tags-history))))
8998 ;; Parse the string and create a lisp form
8999 (let ((match0 match)
9000 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
9002 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
9003 orterms term orlist re-p str-p level-p level-op
9004 prop-p pn pv po cat-p gv)
9005 (if (string-match "/+" match)
9006 ;; match contains also a todo-matching request
9008 (setq tagsmatch (substring match 0 (match-beginning 0))
9009 todomatch (substring match (match-end 0)))
9010 (if (string-match "^!" todomatch)
9011 (setq todo-only t todomatch (substring todomatch 1)))
9012 (if (string-match "^\\s-*$" todomatch)
9013 (setq todomatch nil)))
9014 ;; only matching tags
9015 (setq tagsmatch match todomatch nil))
9017 ;; Make the tags matcher
9018 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
9019 (setq tagsmatcher t)
9020 (setq orterms (org-split-string tagsmatch "|") orlist nil)
9021 (while (setq term (pop orterms))
9022 (while (and (equal (substring term -1) "\\") orterms)
9023 (setq term (concat term "|" (pop orterms)))) ; repair bad split
9024 (while (string-match re term)
9025 (setq minus (and (match-end 1)
9026 (equal (match-string 1 term) "-"))
9027 tag (match-string 2 term)
9028 re-p (equal (string-to-char tag) ?{)
9029 level-p (match-end 4)
9030 prop-p (match-end 5)
9032 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
9034 (setq level-op (org-op-to-function (match-string 3 term)))
9035 `(,level-op level ,(string-to-number
9036 (match-string 4 term))))
9038 (setq pn (match-string 5 term)
9039 po (match-string 6 term)
9040 pv (match-string 7 term)
9041 cat-p (equal pn "CATEGORY")
9042 re-p (equal (string-to-char pv) ?{)
9043 str-p (equal (string-to-char pv) ?\")
9044 pv (if (or re-p str-p) (substring pv 1 -1) pv))
9045 (setq po (org-op-to-function po str-p))
9046 (if (equal pn "CATEGORY")
9047 (setq gv '(get-text-property (point) 'org-category))
9048 (setq gv `(org-cached-entry-get nil ,pn)))
9051 `(not (string-match ,pv (or ,gv "")))
9052 `(string-match ,pv (or ,gv "")))
9054 `(,po (or ,gv "") ,pv)
9055 `(,po (string-to-number (or ,gv ""))
9056 ,(string-to-number pv) ))))
9057 (t `(member ,(downcase tag) tags-list)))
9058 mm (if minus (list 'not mm) mm)
9059 term (substring term (match-end 0)))
9060 (push mm tagsmatcher))
9061 (push (if (> (length tagsmatcher) 1)
9062 (cons 'and tagsmatcher)
9065 (setq tagsmatcher nil))
9066 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
9068 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
9069 ;; Make the todo matcher
9070 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
9071 (setq todomatcher t)
9072 (setq orterms (org-split-string todomatch "|") orlist nil)
9073 (while (setq term (pop orterms))
9074 (while (string-match re term)
9075 (setq minus (and (match-end 1)
9076 (equal (match-string 1 term) "-"))
9077 kwd (match-string 2 term)
9078 re-p (equal (string-to-char kwd) ?{)
9079 term (substring term (match-end 0))
9081 `(string-match ,(substring kwd 1 -1) todo)
9082 (list 'equal 'todo kwd))
9083 mm (if minus (list 'not mm) mm))
9084 (push mm todomatcher))
9085 (push (if (> (length todomatcher) 1)
9086 (cons 'and todomatcher)
9089 (setq todomatcher nil))
9090 (setq todomatcher (if (> (length orlist) 1)
9091 (cons 'or orlist) (car orlist))))
9093 ;; Return the string and lisp forms of the matcher
9094 (setq matcher (if todomatcher
9095 (list 'and tagsmatcher todomatcher)
9097 (cons match0 matcher)))
9099 (defun org-op-to-function (op &optional stringp)
9102 ((equal op "<" ) '(< string< ))
9103 ((equal op ">" ) '(> org-string> ))
9104 ((member op '("<=" "=<")) '(<= org-string<= ))
9105 ((member op '(">=" "=>")) '(>= org-string>= ))
9106 ((member op '("=" "==")) '(= string= ))
9107 ((member op '("<>" "!=")) '(org<> org-string<> ))))
9108 (nth (if stringp 1 0) op))
9110 (defun org<> (a b) (not (= a b)))
9111 (defun org-string<= (a b) (or (string= a b) (string< a b)))
9112 (defun org-string>= (a b) (not (string< a b)))
9113 (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
9114 (defun org-string<> (a b) (not (string= a b)))
9116 (defun org-match-any-p (re list)
9117 "Does re match any element of list?"
9118 (setq list (mapcar (lambda (x) (string-match re x)) list))
9121 (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
9122 (defvar org-tags-overlay (org-make-overlay 1 1))
9123 (org-detach-overlay org-tags-overlay)
9125 (defun org-get-tags-at (&optional pos)
9126 "Get a list of all headline tags applicable at POS.
9127 POS defaults to point. If tags are inherited, the list contains
9128 the targets in the same sequence as the headlines appear, i.e.
9129 sthe tags of the current headline come last."
9131 (let (tags ltags lastpos parent)
9135 (goto-char (or pos (point)))
9139 (org-back-to-heading t)
9140 (while (not (equal lastpos (point)))
9141 (setq lastpos (point))
9142 (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
9143 (setq ltags (org-split-string
9144 (org-match-string-no-properties 1) ":"))
9145 (setq tags (append (org-remove-uniherited-tags ltags)
9147 (or org-use-tag-inheritance (error ""))
9148 (org-up-heading-all 1)
9153 (defun org-toggle-tag (tag &optional onoff)
9154 "Toggle the tag TAG for the current line.
9155 If ONOFF is `on' or `off', don't toggle but set to this state."
9156 (unless (org-on-heading-p t) (error "Not on headling"))
9160 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
9163 (setq current (match-string 1))
9166 (setq current (nreverse (org-split-string current ":")))
9170 (or (member tag current) (push tag current)))
9172 (or (not (member tag current)) (setq current (delete tag current))))
9173 (t (if (member tag current)
9174 (setq current (delete tag current))
9176 (push tag current))))
9180 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
9181 (org-set-tags nil t))
9182 (delete-horizontal-space))
9183 (run-hooks 'org-after-tags-change-hook))
9186 (defun org-align-tags-here (to-col)
9187 ;; Assumes that this is a headline
9188 (let ((pos (point)) (col (current-column)) ncol tags-l p)
9189 (beginning-of-line 1)
9190 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
9191 (< pos (match-beginning 2)))
9193 (setq tags-l (- (match-end 2) (match-beginning 2)))
9194 (goto-char (match-beginning 1))
9196 (delete-region (point) (1+ (match-beginning 2)))
9197 (setq ncol (max (1+ (current-column))
9201 (- (abs to-col) tags-l))))
9203 (insert (make-string (- ncol (current-column)) ?\ ))
9204 (setq ncol (current-column))
9205 (tabify p (point-at-eol))
9206 (org-move-to-column (min ncol col) t))
9209 (defun org-set-tags (&optional arg just-align)
9210 "Set the tags for the current headline.
9211 With prefix ARG, realign all tags in headings in the current buffer."
9213 (let* ((re (concat "^" outline-regexp))
9214 (current (org-get-tags-string))
9215 (col (current-column))
9216 (org-setting-tags t)
9217 table current-tags inherited-tags ; computed below when needed
9221 (goto-char (point-min))
9222 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
9223 (while (re-search-forward re nil t)
9224 (org-set-tags nil t)
9226 (message "All tags realigned to column %d" org-tags-column))
9229 ;; Get a new set of tags from the user
9231 (setq table (or org-tag-alist (org-get-buffer-tags))
9232 org-last-tags-completion-table table
9233 current-tags (org-split-string current ":")
9234 inherited-tags (nreverse
9235 (nthcdr (length current-tags)
9236 (nreverse (org-get-tags-at))))
9238 (if (or (eq t org-use-fast-tag-selection)
9239 (and org-use-fast-tag-selection
9240 (delq nil (mapcar 'cdr table))))
9241 (org-fast-tag-selection
9242 current-tags inherited-tags table
9243 (if org-fast-tag-selection-include-todo org-todo-key-alist))
9244 (let ((org-add-colon-after-tag-completion t))
9246 (org-without-partial-completion
9247 (completing-read "Tags: " 'org-tags-completion-function
9248 nil nil current 'org-tags-history)))))))
9249 (while (string-match "[-+&]+" tags)
9250 ;; No boolean logic, just a list
9251 (setq tags (replace-match ":" t t tags))))
9253 (if (string-match "\\`[\t ]*\\'" tags)
9255 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
9256 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
9258 ;; Insert new tags at the correct column
9259 (beginning-of-line 1)
9261 ((and (equal current "") (equal tags "")))
9263 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
9267 (goto-char (match-beginning 0))
9268 (setq c0 (current-column) p0 (point)
9269 c1 (max (1+ c0) (if (> org-tags-column 0)
9271 (- (- org-tags-column) (length tags))))
9272 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
9273 (replace-match rpl t t)
9274 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
9276 (t (error "Tags alignment failed")))
9277 (org-move-to-column col)
9279 (run-hooks 'org-after-tags-change-hook)))))
9281 (defun org-change-tag-in-region (beg end tag off)
9282 "Add or remove TAG for each entry in the region.
9283 This works in the agenda, and also in an org-mode buffer."
9285 (list (region-beginning) (region-end)
9286 (let ((org-last-tags-completion-table
9288 (org-get-buffer-tags)
9289 (org-global-tags-completion-table))))
9291 "Tag: " 'org-tags-completion-function nil nil nil
9294 (message "[s]et or [r]emove? ")
9295 (equal (read-char-exclusive) ?r))))
9296 (if (fboundp 'deactivate-mark) (deactivate-mark))
9297 (let ((agendap (equal major-mode 'org-agenda-mode))
9298 l1 l2 m buf pos newhead (cnt 0))
9300 (setq l2 (1- (org-current-line)))
9302 (setq l1 (org-current-line))
9303 (loop for l from l1 to l2 do
9305 (setq m (get-text-property (point) 'org-hd-marker))
9306 (when (or (and (org-mode-p) (org-on-heading-p))
9308 (setq buf (if agendap (marker-buffer m) (current-buffer))
9309 pos (if agendap m (point)))
9310 (with-current-buffer buf
9315 (org-toggle-tag tag (if off 'off 'on))
9316 (setq newhead (org-get-heading)))))
9317 (and agendap (org-agenda-change-all-lines newhead m))))
9318 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
9320 (defun org-tags-completion-function (string predicate &optional flag)
9321 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
9322 (confirm (lambda (x) (stringp (car x)))))
9323 (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
9324 (setq s1 (match-string 1 string)
9325 s2 (match-string 2 string))
9326 (setq s1 "" s2 string))
9330 (setq rtn (try-completion s2 ctable confirm))
9333 (concat s1 s2 (substring rtn (length s2))
9334 (if (and org-add-colon-after-tag-completion
9340 (all-completions s2 ctable confirm)
9347 (defun org-fast-tag-insert (kwd tags face &optional end)
9348 "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
9349 (insert (format "%-12s" (concat kwd ":"))
9350 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
9353 (defun org-fast-tag-show-exit (flag)
9356 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
9360 (org-move-to-column (- (window-width) 19) t)
9361 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
9363 (defun org-set-current-tags-overlay (current prefix)
9364 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
9365 (if (featurep 'xemacs)
9366 (org-overlay-display org-tags-overlay (concat prefix s)
9367 'secondary-selection)
9368 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
9369 (org-overlay-display org-tags-overlay (concat prefix s)))))
9371 (defun org-fast-tag-selection (current inherited table &optional todo-table)
9372 "Fast tag selection with single keys.
9373 CURRENT is the current list of tags in the headline, INHERITED is the
9374 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
9375 possibly with grouping information. TODO-TABLE is a similar table with
9376 TODO keywords, should these have keys assigned to them.
9377 If the keys are nil, a-z are automatically assigned.
9378 Returns the new tags string, or nil to not change the current settings."
9379 (let* ((fulltable (append table todo-table))
9380 (maxlen (apply 'max (mapcar
9382 (if (stringp (car x)) (string-width (car x)) 0))
9384 (buf (current-buffer))
9385 (expert (eq org-fast-tag-selection-single-key 'expert))
9387 (fwidth (+ maxlen 3 1 3))
9388 (ncol (/ (- (window-width) 4) fwidth))
9391 tg cnt e c char c1 c2 ntable tbl rtn
9392 ov-start ov-end ov-prefix
9393 (exit-after-next org-fast-tag-selection-single-key)
9394 (done-keywords org-done-keywords)
9397 (beginning-of-line 1)
9399 (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
9400 (setq ov-start (match-beginning 1)
9401 ov-end (match-end 1)
9403 (setq ov-start (1- (point-at-eol))
9404 ov-end (1+ ov-start))
9405 (skip-chars-forward "^\n\r")
9408 (buffer-substring (1- (point)) (point))
9409 (if (> (current-column) org-tags-column)
9411 (make-string (- org-tags-column (current-column)) ?\ ))))))
9412 (org-move-overlay org-tags-overlay ov-start ov-end)
9413 (save-window-excursion
9415 (set-buffer (get-buffer-create " *Org tags*"))
9416 (delete-other-windows)
9417 (split-window-vertically)
9418 (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
9420 (org-set-local 'org-done-keywords done-keywords)
9421 (org-fast-tag-insert "Inherited" inherited i-face "\n")
9422 (org-fast-tag-insert "Current" current c-face "\n\n")
9423 (org-fast-tag-show-exit exit-after-next)
9424 (org-set-current-tags-overlay current ov-prefix)
9425 (setq tbl fulltable char ?a cnt 0)
9426 (while (setq e (pop tbl))
9428 ((equal e '(:startgroup))
9429 (push '() groups) (setq ingroup t)
9430 (when (not (= cnt 0))
9434 ((equal e '(:endgroup))
9435 (setq ingroup nil cnt 0)
9438 (setq tg (car e) c2 nil)
9441 ;; automatically assign a character.
9442 (setq c1 (string-to-char
9443 (downcase (substring
9444 tg (if (= (string-to-char tg) ?@) 1 0)))))
9445 (if (or (rassoc c1 ntable) (rassoc c1 table))
9446 (while (or (rassoc char ntable) (rassoc char table))
9447 (setq char (1+ char)))
9449 (setq c (or c2 char)))
9450 (if ingroup (push tg (car groups)))
9451 (setq tg (org-add-props tg nil 'face
9453 ((not (assoc tg table))
9454 (org-get-todo-face tg))
9455 ((member tg current) c-face)
9456 ((member tg inherited) i-face)
9458 (if (and (= cnt 0) (not ingroup)) (insert " "))
9459 (insert "[" c "] " tg (make-string
9460 (- fwidth 4 (length tg)) ?\ ))
9461 (push (cons tg c) ntable)
9462 (when (= (setq cnt (1+ cnt)) ncol)
9464 (if ingroup (insert " "))
9466 (setq ntable (nreverse ntable))
9468 (goto-char (point-min))
9469 (if (and (not expert) (fboundp 'fit-window-to-buffer))
9470 (fit-window-to-buffer))
9474 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
9475 (if groups " [!] no groups" " [!]groups")
9476 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
9477 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
9479 ((= c ?\r) (throw 'exit t))
9481 (setq groups (not groups))
9482 (goto-char (point-min))
9483 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
9486 (org-fast-tag-show-exit
9487 (setq exit-after-next (not exit-after-next)))
9489 (delete-other-windows)
9490 (split-window-vertically)
9491 (org-switch-to-buffer-other-window " *Org tags*")
9492 (and (fboundp 'fit-window-to-buffer)
9493 (fit-window-to-buffer))))
9495 (and (= c ?q) (not (rassoc c ntable))))
9496 (org-detach-overlay org-tags-overlay)
9500 (if exit-after-next (setq exit-after-next 'now)))
9503 (setq tg (completing-read
9506 (with-current-buffer buf
9507 (org-get-buffer-tags)))))
9508 (quit (setq tg "")))
9509 (when (string-match "\\S-" tg)
9510 (add-to-list 'buffer-tags (list tg))
9511 (if (member tg current)
9512 (setq current (delete tg current))
9514 (if exit-after-next (setq exit-after-next 'now)))
9515 ((setq e (rassoc c todo-table) tg (car e))
9516 (with-current-buffer buf
9517 (save-excursion (org-todo tg)))
9518 (if exit-after-next (setq exit-after-next 'now)))
9519 ((setq e (rassoc c ntable) tg (car e))
9520 (if (member tg current)
9521 (setq current (delete tg current))
9522 (loop for g in groups do
9525 (setq current (delete x current)))
9528 (if exit-after-next (setq exit-after-next 'now))))
9530 ;; Create a sorted list
9534 (assoc b (cdr (memq (assoc a ntable) ntable))))))
9535 (if (eq exit-after-next 'now) (throw 'exit t))
9536 (goto-char (point-min))
9537 (beginning-of-line 2)
9538 (delete-region (point) (point-at-eol))
9539 (org-fast-tag-insert "Current" current c-face)
9540 (org-set-current-tags-overlay current ov-prefix)
9541 (while (re-search-forward
9542 (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
9543 (setq tg (match-string 1))
9544 (add-text-properties
9545 (match-beginning 1) (match-end 1)
9548 ((member tg current) c-face)
9549 ((member tg inherited) i-face)
9550 (t (get-text-property (match-beginning 1) 'face))))))
9551 (goto-char (point-min)))))
9552 (org-detach-overlay org-tags-overlay)
9554 (mapconcat 'identity current ":")
9557 (defun org-get-tags-string ()
9558 "Get the TAGS string in the current headline."
9559 (unless (org-on-heading-p t)
9560 (error "Not on a heading"))
9562 (beginning-of-line 1)
9563 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
9564 (org-match-string-no-properties 1)
9567 (defun org-get-tags ()
9568 "Get the list of tags specified in the current headline."
9569 (org-split-string (org-get-tags-string) ":"))
9571 (defun org-get-buffer-tags ()
9572 "Get a table of all tags used in the buffer, for completion."
9575 (goto-char (point-min))
9576 (while (re-search-forward
9577 (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
9578 (when (equal (char-after (point-at-bol 0)) ?*)
9579 (mapc (lambda (x) (add-to-list 'tags x))
9580 (org-split-string (org-match-string-no-properties 1) ":")))))
9581 (mapcar 'list tags)))
9586 ;;; Setting and retrieving properties
9588 (defconst org-special-properties
9589 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
9590 "TIMESTAMP" "TIMESTAMP_IA")
9591 "The special properties valid in Org-mode.
9593 These are properties that are not defined in the property drawer,
9594 but in some other way.")
9596 (defconst org-default-properties
9597 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
9598 "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
9599 "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE")
9600 "Some properties that are used by Org-mode for various purposes.
9601 Being in this list makes sure that they are offered for completion.")
9603 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
9604 "Regular expression matching the first line of a property drawer.")
9606 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
9607 "Regular expression matching the first line of a property drawer.")
9609 (defun org-property-action ()
9610 "Do an action on properties."
9614 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
9615 (setq c (read-char-exclusive))
9618 (call-interactively 'org-set-property))
9620 (call-interactively 'org-delete-property))
9622 (call-interactively 'org-delete-property-globally))
9624 (call-interactively 'org-compute-property-at-point))
9625 (t (error "No such property action %c" c)))))
9627 (defun org-at-property-p ()
9628 "Is the cursor in a property line?"
9629 ;; FIXME: Does not check if we are actually in the drawer.
9630 ;; FIXME: also returns true on any drawers.....
9631 ;; This is used by C-c C-c for property action.
9633 (beginning-of-line 1)
9634 (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
9636 (defun org-get-property-block (&optional beg end force)
9637 "Return the (beg . end) range of the body of the property drawer.
9638 BEG and END can be beginning and end of subtree, if not given
9640 If the drawer does not exist and FORCE is non-nil, create the drawer."
9643 (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
9644 (end (or end (progn (outline-next-heading) (point)))))
9646 (if (re-search-forward org-property-start-re end t)
9647 (setq beg (1+ (match-end 0)))
9650 (org-insert-property-drawer)
9651 (setq end (progn (outline-next-heading) (point))))
9654 (if (re-search-forward org-property-start-re end t)
9655 (setq beg (1+ (match-end 0)))))
9656 (if (re-search-forward org-property-end-re end t)
9657 (setq end (match-beginning 0))
9658 (or force (throw 'exit nil))
9661 (org-indent-line-function)
9665 (defun org-entry-properties (&optional pom which)
9666 "Get all properties of the entry at point-or-marker POM.
9667 This includes the TODO keyword, the tags, time strings for deadline,
9668 scheduled, and clocking, and any additional properties defined in the
9669 entry. The return value is an alist, keys may occur multiple times
9670 if the property key was used several times.
9671 POM may also be nil, in which case the current entry is used.
9672 If WHICH is nil or `all', get all properties. If WHICH is
9673 `special' or `standard', only get that subclass."
9674 (setq which (or which 'all))
9675 (org-with-point-at pom
9676 (let ((clockstr (substring org-clock-string 0 -1))
9677 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
9678 beg end range props sum-props key value string clocksum)
9680 (when (condition-case nil (org-back-to-heading t) (error nil))
9682 (setq sum-props (get-text-property (point) 'org-summaries))
9683 (setq clocksum (get-text-property (point) :org-clock-minutes))
9684 (outline-next-heading)
9686 (when (memq which '(all special))
9687 ;; Get the special properties, like TODO and tags
9689 (when (and (looking-at org-todo-line-regexp) (match-end 2))
9690 (push (cons "TODO" (org-match-string-no-properties 2)) props))
9691 (when (looking-at org-priority-regexp)
9692 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
9693 (when (and (setq value (org-get-tags-string))
9694 (string-match "\\S-" value))
9695 (push (cons "TAGS" value) props))
9696 (when (setq value (org-get-tags-at))
9697 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
9699 (while (re-search-forward org-maybe-keyword-time-regexp end t)
9700 (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
9701 string (if (equal key clockstr)
9705 (match-beginning 3) (goto-char (point-at-eol)))))
9706 (substring (org-match-string-no-properties 3) 1 -1)))
9708 (if (= (char-after (match-beginning 3)) ?\[)
9709 (setq key "TIMESTAMP_IA")
9710 (setq key "TIMESTAMP")))
9711 (when (or (equal key clockstr) (not (assoc key props)))
9712 (push (cons key string) props)))
9716 (when (memq which '(all standard))
9717 ;; Get the standard properties, like :PORP: ...
9718 (setq range (org-get-property-block beg end))
9720 (goto-char (car range))
9721 (while (re-search-forward
9722 (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
9724 (setq key (org-match-string-no-properties 1)
9725 value (org-trim (or (org-match-string-no-properties 2) "")))
9726 (unless (member key excluded)
9727 (push (cons key (or value "")) props)))))
9729 (push (cons "CLOCKSUM"
9730 (org-columns-number-to-string (/ (float clocksum) 60.)
9733 (append sum-props (nreverse props)))))))
9735 (defun org-entry-get (pom property &optional inherit)
9736 "Get value of PROPERTY for entry at point-or-marker POM.
9737 If INHERIT is non-nil and the entry does not have the property,
9738 then also check higher levels of the hierarchy.
9739 If INHERIT is the symbol `selective', use inheritance only if the setting
9740 in `org-use-property-inheritance' selects PROPERTY for inheritance.
9741 If the property is present but empty, the return value is the empty string.
9742 If the property is not present at all, nil is returned."
9743 (org-with-point-at pom
9744 (if (and inherit (if (eq inherit 'selective)
9745 (org-property-inherit-p property)
9747 (org-entry-get-with-inheritance property)
9748 (if (member property org-special-properties)
9749 ;; We need a special property. Use brute force, get all properties.
9750 (cdr (assoc property (org-entry-properties nil 'special)))
9751 (let ((range (org-get-property-block)))
9753 (goto-char (car range))
9755 (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
9757 ;; Found the property, return it.
9759 (org-match-string-no-properties 1)
9762 (defun org-property-or-variable-value (var &optional inherit)
9763 "Check if there is a property fixing the value of VAR.
9764 If yes, return this value. If not, return the current value of the variable."
9765 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
9766 (if (and prop (stringp prop) (string-match "\\S-" prop))
9768 (symbol-value var))))
9770 (defun org-entry-delete (pom property)
9771 "Delete the property PROPERTY from entry at point-or-marker POM."
9772 (org-with-point-at pom
9773 (if (member property org-special-properties)
9774 nil ; cannot delete these properties.
9775 (let ((range (org-get-property-block)))
9777 (goto-char (car range))
9779 (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
9782 (delete-region (match-beginning 0) (1+ (point-at-eol)))
9786 ;; Multi-values properties are properties that contain multiple values
9787 ;; These values are assumed to be single words, separated by whitespace.
9788 (defun org-entry-add-to-multivalued-property (pom property value)
9789 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
9790 (let* ((old (org-entry-get pom property))
9791 (values (and old (org-split-string old "[ \t]"))))
9792 (unless (member value values)
9793 (setq values (cons value values))
9794 (org-entry-put pom property
9795 (mapconcat 'identity values " ")))))
9797 (defun org-entry-remove-from-multivalued-property (pom property value)
9798 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
9799 (let* ((old (org-entry-get pom property))
9800 (values (and old (org-split-string old "[ \t]"))))
9801 (when (member value values)
9802 (setq values (delete value values))
9803 (org-entry-put pom property
9804 (mapconcat 'identity values " ")))))
9806 (defun org-entry-member-in-multivalued-property (pom property value)
9807 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
9808 (let* ((old (org-entry-get pom property))
9809 (values (and old (org-split-string old "[ \t]"))))
9810 (member value values)))
9812 (defvar org-entry-property-inherited-from (make-marker))
9814 (defun org-entry-get-with-inheritance (property)
9815 "Get entry property, and search higher levels if not present."
9822 (when (setq tmp (org-entry-get nil property))
9823 (org-back-to-heading t)
9824 (move-marker org-entry-property-inherited-from (point))
9826 (or (org-up-heading-safe) (throw 'ex nil)))))
9828 (cdr (assoc property org-local-properties))
9829 (cdr (assoc property org-global-properties))
9830 (cdr (assoc property org-global-properties-fixed))))))
9832 (defun org-entry-put (pom property value)
9833 "Set PROPERTY to VALUE for entry at point-or-marker POM."
9834 (org-with-point-at pom
9835 (org-back-to-heading t)
9836 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
9839 ((equal property "TODO")
9840 (when (and (stringp value) (string-match "\\S-" value)
9841 (not (member value org-todo-keywords-1)))
9842 (error "\"%s\" is not a valid TODO state" value))
9844 (not (string-match "\\S-" value)))
9847 (org-set-tags nil 'align))
9848 ((equal property "PRIORITY")
9849 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
9850 (string-to-char value) ?\ ))
9851 (org-set-tags nil 'align))
9852 ((equal property "SCHEDULED")
9853 (if (re-search-forward org-scheduled-time-regexp end t)
9855 ((eq value 'earlier) (org-timestamp-change -1 'day))
9856 ((eq value 'later) (org-timestamp-change 1 'day))
9857 (t (call-interactively 'org-schedule)))
9858 (call-interactively 'org-schedule)))
9859 ((equal property "DEADLINE")
9860 (if (re-search-forward org-deadline-time-regexp end t)
9862 ((eq value 'earlier) (org-timestamp-change -1 'day))
9863 ((eq value 'later) (org-timestamp-change 1 'day))
9864 (t (call-interactively 'org-deadline)))
9865 (call-interactively 'org-deadline)))
9866 ((member property org-special-properties)
9867 (error "The %s property can not yet be set with `org-entry-put'"
9869 (t ; a non-special property
9870 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
9871 (setq range (org-get-property-block beg end 'force))
9872 (goto-char (car range))
9873 (if (re-search-forward
9874 (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
9876 (delete-region (match-beginning 1) (match-end 1))
9877 (goto-char (match-beginning 1)))
9878 (goto-char (cdr range))
9881 (org-indent-line-function)
9882 (insert ":" property ":"))
9883 (and value (insert " " value))
9884 (org-indent-line-function)))))))
9886 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
9887 "Get all property keys in the current buffer.
9888 With INCLUDE-SPECIALS, also list the special properties that relect things
9889 like tags and TODO state.
9890 With INCLUDE-DEFAULTS, also include properties that has special meaning
9891 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
9892 With INCLUDE-COLUMNS, also include property names given in COLUMN
9893 formats in the current buffer."
9894 (let (rtn range cfmt cols s p)
9898 (goto-char (point-min))
9899 (while (re-search-forward org-property-start-re nil t)
9900 (setq range (org-get-property-block))
9901 (goto-char (car range))
9902 (while (re-search-forward
9903 (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
9905 (add-to-list 'rtn (org-match-string-no-properties 1)))
9906 (outline-next-heading))))
9908 (when include-specials
9909 (setq rtn (append org-special-properties rtn)))
9911 (when include-defaults
9912 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
9914 (when include-columns
9918 (goto-char (point-min))
9919 (while (re-search-forward
9920 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
9922 (setq cfmt (match-string 2) s 0)
9923 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
9925 (setq s (match-end 0)
9926 p (match-string 1 cfmt))
9927 (unless (or (equal p "ITEM")
9928 (member p org-special-properties))
9929 (add-to-list 'rtn (match-string 1 cfmt))))))))
9931 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
9933 (defun org-property-values (key)
9934 "Return a list of all values of property KEY."
9938 (goto-char (point-min))
9939 (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
9941 (while (re-search-forward re nil t)
9942 (add-to-list 'values (org-trim (match-string 1))))
9943 (delete "" values)))))
9945 (defun org-insert-property-drawer ()
9946 "Insert a property drawer into the current entry."
9948 (org-back-to-heading t)
9949 (looking-at outline-regexp)
9950 (let ((indent (- (match-end 0)(match-beginning 0)))
9952 (re (concat "^[ \t]*" org-keyword-time-regexp))
9954 (outline-next-heading)
9957 (while (re-search-forward re end t))
9958 (setq hiddenp (org-invisible-p))
9960 (and (equal (char-after) ?\n) (forward-char 1))
9961 (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
9962 (beginning-of-line 2))
9963 (org-skip-over-state-notes)
9964 (skip-chars-backward " \t\n\r")
9965 (if (eq (char-before) ?*) (forward-char 1))
9966 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
9967 (beginning-of-line 0)
9968 (org-indent-to-column indent)
9969 (beginning-of-line 2)
9970 (org-indent-to-column indent)
9971 (beginning-of-line 0)
9974 (org-back-to-heading t)
9976 (org-flag-drawer t))))
9978 (defun org-set-property (property value)
9979 "In the current entry, set PROPERTY to VALUE.
9980 When called interactively, this will prompt for a property name, offering
9981 completion on existing and default properties. And then it will prompt
9982 for a value, offering competion either on allowed values (via an inherited
9983 xxx_ALL property) or on existing values in other instances of this property
9984 in the current file."
9986 (let* ((completion-ignore-case t)
9987 (keys (org-buffer-property-keys nil t t))
9988 (prop0 (completing-read "Property: " (mapcar 'list keys)))
9989 (prop (if (member prop0 keys)
9991 (or (cdr (assoc (downcase prop0)
9992 (mapcar (lambda (x) (cons (downcase x) x))
9995 (cur (org-entry-get nil prop))
9996 (allowed (org-property-get-allowed-values nil prop 'table))
9997 (existing (mapcar 'list (org-property-values prop)))
9999 (org-completing-read "Value: " allowed nil 'req-match)
10000 (org-completing-read
10001 (concat "Value" (if (and cur (string-match "\\S-" cur))
10002 (concat "[" cur "]") "")
10004 existing nil nil "" nil cur))))
10005 (list prop (if (equal val "") cur val))))
10006 (unless (equal (org-entry-get nil property) value)
10007 (org-entry-put nil property value)))
10009 (defun org-delete-property (property)
10010 "In the current entry, delete PROPERTY."
10012 (let* ((completion-ignore-case t)
10013 (prop (completing-read
10014 "Property: " (org-entry-properties nil 'standard))))
10016 (message "Property %s %s" property
10017 (if (org-entry-delete nil property)
10019 "was not present in the entry")))
10021 (defun org-delete-property-globally (property)
10022 "Remove PROPERTY globally, from all entries."
10024 (let* ((completion-ignore-case t)
10025 (prop (completing-read
10026 "Globally remove property: "
10027 (mapcar 'list (org-buffer-property-keys)))))
10032 (goto-char (point-min))
10034 (while (re-search-forward
10035 (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
10037 (setq cnt (1+ cnt))
10038 (replace-match ""))
10039 (message "Property \"%s\" removed from %d entries" property cnt)))))
10041 (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
10043 (defun org-compute-property-at-point ()
10044 "Compute the property at point.
10045 This looks for an enclosing column format, extracts the operator and
10046 then applies it to the proerty in the column format's scope."
10048 (unless (org-at-property-p)
10049 (error "Not at a property"))
10050 (let ((prop (org-match-string-no-properties 2)))
10051 (org-columns-get-format-and-top-level)
10052 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
10053 (error "No operator defined for property %s" prop))
10054 (org-columns-compute prop)))
10056 (defun org-property-get-allowed-values (pom property &optional table)
10057 "Get allowed values for the property PROPERTY.
10058 When TABLE is non-nil, return an alist that can directly be used for
10062 ((equal property "TODO")
10063 (setq vals (org-with-point-at pom
10064 (append org-todo-keywords-1 '("")))))
10065 ((equal property "PRIORITY")
10066 (let ((n org-lowest-priority))
10067 (while (>= n org-highest-priority)
10068 (push (char-to-string n) vals)
10070 ((member property org-special-properties))
10072 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
10074 (when (and vals (string-match "\\S-" vals))
10075 (setq vals (car (read-from-string (concat "(" vals ")"))))
10076 (setq vals (mapcar (lambda (x)
10077 (cond ((stringp x) x)
10078 ((numberp x) (number-to-string x))
10079 ((symbolp x) (symbol-name x))
10082 (if table (mapcar 'list vals) vals)))
10084 (defun org-property-previous-allowed-value (&optional previous)
10085 "Switch to the next allowed value for this property."
10087 (org-property-next-allowed-value t))
10089 (defun org-property-next-allowed-value (&optional previous)
10090 "Switch to the next allowed value for this property."
10092 (unless (org-at-property-p)
10093 (error "Not at a property"))
10094 (let* ((key (match-string 2))
10095 (value (match-string 3))
10096 (allowed (or (org-property-get-allowed-values (point) key)
10097 (and (member value '("[ ]" "[-]" "[X]"))
10101 (error "Allowed values for this property have not been defined"))
10102 (if previous (setq allowed (reverse allowed)))
10103 (if (member value allowed)
10104 (setq nval (car (cdr (member value allowed)))))
10105 (setq nval (or nval (car allowed)))
10106 (if (equal nval value)
10107 (error "Only one allowed value for this property"))
10108 (org-at-property-p)
10109 (replace-match (concat " :" key ": " nval) t t)
10110 (org-indent-line-function)
10111 (beginning-of-line 1)
10112 (skip-chars-forward " \t")))
10114 (defun org-find-entry-with-id (ident)
10115 "Locate the entry that contains the ID property with exact value IDENT.
10116 IDENT can be a string, a symbol or a number, this function will search for
10117 the string representation of it.
10118 Return the position where this entry starts, or nil if there is no such entry."
10120 ((stringp ident) ident)
10121 ((symbol-name ident) (symbol-name ident))
10122 ((numberp ident) (number-to-string ident))
10123 (t (error "IDENT %s must be a string, symbol or number" ident))))
10124 (case-fold-search nil))
10128 (goto-char (point-min))
10129 (when (re-search-forward
10130 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
10132 (org-back-to-heading)
10137 (defvar org-last-changed-timestamp nil)
10138 (defvar org-time-was-given) ; dynamically scoped parameter
10139 (defvar org-end-time-was-given) ; dynamically scoped parameter
10140 (defvar org-ts-what) ; dynamically scoped parameter
10142 (defun org-time-stamp (arg)
10143 "Prompt for a date/time and insert a time stamp.
10144 If the user specifies a time like HH:MM, or if this command is called
10145 with a prefix argument, the time stamp will contain date and time.
10146 Otherwise, only the date will be included. All parts of a date not
10147 specified by the user will be filled in from the current date/time.
10148 So if you press just return without typing anything, the time stamp
10149 will represent the current date/time. If there is already a timestamp
10150 at the cursor, it will be modified."
10154 ;; Default time is either today, or, when entering a range,
10155 ;; the range start.
10156 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
10158 (re-search-backward
10159 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
10160 (- (point) 20) t)))
10161 (apply 'encode-time (org-parse-time-string (match-string 1)))
10163 (default-input (and ts (org-get-compact-tod ts)))
10164 org-time-was-given org-end-time-was-given time)
10166 ((and (org-at-timestamp-p)
10167 (eq last-command 'org-time-stamp)
10168 (eq this-command 'org-time-stamp))
10170 (setq time (let ((this-command this-command))
10171 (org-read-date arg 'totime nil nil default-time default-input)))
10172 (org-insert-time-stamp time (or org-time-was-given arg)))
10173 ((org-at-timestamp-p)
10174 (setq time (let ((this-command this-command))
10175 (org-read-date arg 'totime nil nil default-time default-input)))
10176 (when (org-at-timestamp-p) ; just to get the match data
10178 (setq org-last-changed-timestamp
10179 (org-insert-time-stamp
10180 time (or org-time-was-given arg)
10181 nil nil nil (list org-end-time-was-given))))
10182 (message "Timestamp updated"))
10184 (setq time (let ((this-command this-command))
10185 (org-read-date arg 'totime nil nil default-time default-input)))
10186 (org-insert-time-stamp time (or org-time-was-given arg)
10187 nil nil nil (list org-end-time-was-given))))))
10189 ;; FIXME: can we use this for something else, like computing time differences?
10190 (defun org-get-compact-tod (s)
10191 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
10192 (let* ((t1 (match-string 1 s))
10193 (h1 (string-to-number (match-string 2 s)))
10194 (m1 (string-to-number (match-string 3 s)))
10195 (t2 (and (match-end 4) (match-string 5 s)))
10196 (h2 (and t2 (string-to-number (match-string 6 s))))
10197 (m2 (and t2 (string-to-number (match-string 7 s))))
10201 (setq dh (- h2 h1) dm (- m2 m1))
10202 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
10203 (concat t1 "+" (number-to-string dh)
10204 (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
10206 (defun org-time-stamp-inactive (&optional arg)
10207 "Insert an inactive time stamp.
10208 An inactive time stamp is enclosed in square brackets instead of angle
10209 brackets. It is inactive in the sense that it does not trigger agenda entries,
10210 does not link to the calendar and cannot be changed with the S-cursor keys.
10211 So these are more for recording a certain time/date."
10213 (let (org-time-was-given org-end-time-was-given time)
10214 (setq time (org-read-date arg 'totime))
10215 (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
10216 nil nil (list org-end-time-was-given))))
10218 (defvar org-date-ovl (org-make-overlay 1 1))
10219 (org-overlay-put org-date-ovl 'face 'org-warning)
10220 (org-detach-overlay org-date-ovl)
10222 (defvar org-ans1) ; dynamically scoped parameter
10223 (defvar org-ans2) ; dynamically scoped parameter
10225 (defvar org-plain-time-of-day-regexp) ; defined below
10227 (defvar org-read-date-overlay nil)
10228 (defvar org-dcst nil) ; dynamically scoped
10230 (defun org-read-date (&optional with-time to-time from-string prompt
10231 default-time default-input)
10232 "Read a date, possibly a time, and make things smooth for the user.
10233 The prompt will suggest to enter an ISO date, but you can also enter anything
10234 which will at least partially be understood by `parse-time-string'.
10235 Unrecognized parts of the date will default to the current day, month, year,
10236 hour and minute. If this command is called to replace a timestamp at point,
10237 of to enter the second timestamp of a range, the default time is taken from the
10238 existing stamp. For example,
10239 3-2-5 --> 2003-02-05
10240 feb 15 --> currentyear-02-15
10241 sep 12 9 --> 2009-09-12
10242 12:45 --> today 12:45
10243 22 sept 0:34 --> currentyear-09-22 0:34
10244 12 --> currentyear-currentmonth-12
10245 Fri --> nearest Friday (today or later)
10248 Furthermore you can specify a relative date by giving, as the *first* thing
10249 in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
10250 change in days weeks, months, years.
10251 With a single plus or minus, the date is relative to today. With a double
10252 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
10253 +4d --> four days from today
10254 +4 --> same as above
10255 +2w --> two weeks from today
10256 ++5 --> five days from default date
10258 The function understands only English month and weekday abbreviations,
10259 but this can be configured with the variables `parse-time-months' and
10260 `parse-time-weekdays'.
10262 While prompting, a calendar is popped up - you can also select the
10263 date with the mouse (button 1). The calendar shows a period of three
10264 months. To scroll it to other months, use the keys `>' and `<'.
10265 If you don't like the calendar, turn it off with
10266 \(setq org-read-date-popup-calendar nil)
10268 With optional argument TO-TIME, the date will immediately be converted
10269 to an internal time.
10270 With an optional argument WITH-TIME, the prompt will suggest to also
10271 insert a time. Note that when WITH-TIME is not set, you can still
10272 enter a time, and this function will inform the calling routine about
10273 this change. The calling routine may then choose to change the format
10274 used to insert the time stamp into the buffer to include the time.
10275 With optional argument FROM-STRING, read from this string instead from
10276 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
10277 the time/date that is used for everything that is not specified by the
10279 (require 'parse-time)
10280 (let* ((org-time-stamp-rounding-minutes
10281 (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
10282 (org-dcst org-display-custom-times)
10283 (ct (org-current-time))
10284 (def (or default-time ct))
10285 (defdecode (decode-time def))
10287 (when (< (nth 2 defdecode) org-extend-today-until)
10288 (setcar (nthcdr 2 defdecode) -1)
10289 (setcar (nthcdr 1 defdecode) 59)
10290 (setq def (apply 'encode-time defdecode)
10291 defdecode (decode-time def)))))
10292 (calendar-move-hook nil)
10293 (calendar-view-diary-initially-flag nil)
10294 (view-diary-entries-initially nil)
10295 (calendar-view-holidays-initially-flag nil)
10296 (view-calendar-holidays-initially nil)
10297 (timestr (format-time-string
10298 (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
10299 (prompt (concat (if prompt (concat prompt " ") "")
10300 (format "Date+time [%s]: " timestr)))
10301 ans (org-ans0 "") org-ans1 org-ans2 final)
10304 (from-string (setq ans from-string))
10305 (org-read-date-popup-calendar
10307 (save-window-excursion
10309 (calendar-forward-day (- (time-to-days def)
10310 (calendar-absolute-from-gregorian
10311 (calendar-current-date))))
10312 (org-eval-in-calendar nil t)
10313 (let* ((old-map (current-local-map))
10314 (map (copy-keymap calendar-mode-map))
10315 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
10316 (org-defkey map (kbd "RET") 'org-calendar-select)
10317 (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
10318 'org-calendar-select-mouse)
10319 (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2
])
10320 'org-calendar-select-mouse
)
10321 (org-defkey minibuffer-local-map
[(meta shift left
)]
10322 (lambda () (interactive)
10323 (org-eval-in-calendar '(calendar-backward-month 1))))
10324 (org-defkey minibuffer-local-map
[(meta shift right
)]
10325 (lambda () (interactive)
10326 (org-eval-in-calendar '(calendar-forward-month 1))))
10327 (org-defkey minibuffer-local-map
[(meta shift up
)]
10328 (lambda () (interactive)
10329 (org-eval-in-calendar '(calendar-backward-year 1))))
10330 (org-defkey minibuffer-local-map
[(meta shift down
)]
10331 (lambda () (interactive)
10332 (org-eval-in-calendar '(calendar-forward-year 1))))
10333 (org-defkey minibuffer-local-map
[(shift up
)]
10334 (lambda () (interactive)
10335 (org-eval-in-calendar '(calendar-backward-week 1))))
10336 (org-defkey minibuffer-local-map
[(shift down
)]
10337 (lambda () (interactive)
10338 (org-eval-in-calendar '(calendar-forward-week 1))))
10339 (org-defkey minibuffer-local-map
[(shift left
)]
10340 (lambda () (interactive)
10341 (org-eval-in-calendar '(calendar-backward-day 1))))
10342 (org-defkey minibuffer-local-map
[(shift right
)]
10343 (lambda () (interactive)
10344 (org-eval-in-calendar '(calendar-forward-day 1))))
10345 (org-defkey minibuffer-local-map
">"
10346 (lambda () (interactive)
10347 (org-eval-in-calendar '(scroll-calendar-left 1))))
10348 (org-defkey minibuffer-local-map
"<"
10349 (lambda () (interactive)
10350 (org-eval-in-calendar '(scroll-calendar-right 1))))
10353 (use-local-map map
)
10354 (add-hook 'post-command-hook
'org-read-date-display
)
10355 (setq org-ans0
(read-string prompt default-input nil nil
))
10356 ;; org-ans0: from prompt
10357 ;; org-ans1: from mouse click
10358 ;; org-ans2: from calendar motion
10359 (setq ans
(concat org-ans0
" " (or org-ans1 org-ans2
))))
10360 (remove-hook 'post-command-hook
'org-read-date-display
)
10361 (use-local-map old-map
)
10362 (when org-read-date-overlay
10363 (org-delete-overlay org-read-date-overlay
)
10364 (setq org-read-date-overlay nil
)))))))
10366 (t ; Naked prompt only
10368 (setq ans
(read-string prompt default-input nil timestr
))
10369 (when org-read-date-overlay
10370 (org-delete-overlay org-read-date-overlay
)
10371 (setq org-read-date-overlay nil
)))))
10373 (setq final
(org-read-date-analyze ans def defdecode
))
10376 (apply 'encode-time final
)
10377 (if (and (boundp 'org-time-was-given
) org-time-was-given
)
10378 (format "%04d-%02d-%02d %02d:%02d"
10379 (nth 5 final
) (nth 4 final
) (nth 3 final
)
10380 (nth 2 final
) (nth 1 final
))
10381 (format "%04d-%02d-%02d" (nth 5 final
) (nth 4 final
) (nth 3 final
))))))
10385 (defun org-read-date-display ()
10386 "Display the currrent date prompt interpretation in the minibuffer."
10387 (when org-read-date-display-live
10388 (when org-read-date-overlay
10389 (org-delete-overlay org-read-date-overlay
))
10392 (while (not (equal (buffer-substring
10393 (max (point-min) (- (point) 4)) (point))
10397 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
10398 " " (or org-ans1 org-ans2
)))
10399 (org-end-time-was-given nil
)
10400 (f (org-read-date-analyze ans def defdecode
))
10402 org-time-stamp-custom-formats
10403 org-time-stamp-formats
))
10404 (fmt (if (or with-time
10405 (and (boundp 'org-time-was-given
) org-time-was-given
))
10408 (txt (concat "=> " (format-time-string fmt
(apply 'encode-time f
)))))
10409 (when (and org-end-time-was-given
10410 (string-match org-plain-time-of-day-regexp txt
))
10411 (setq txt
(concat (substring txt
0 (match-end 0)) "-"
10412 org-end-time-was-given
10413 (substring txt
(match-end 0)))))
10414 (setq org-read-date-overlay
10415 (make-overlay (1- (point-at-eol)) (point-at-eol)))
10416 (org-overlay-display org-read-date-overlay txt
'secondary-selection
))))
10418 (defun org-read-date-analyze (ans def defdecode
)
10419 "Analyze the combined answer of the date prompt."
10420 ;; FIXME: cleanup and comment
10421 (let (delta deltan deltaw deltadef year month day
10422 hour minute second wday pm h2 m2 tl wday1
10423 iso-year iso-weekday iso-week iso-year iso-date
)
10425 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans
)
10428 (when (setq delta
(org-read-date-get-relative ans
(current-time) def
))
10429 (setq ans
(replace-match "" t t ans
)
10431 deltaw
(nth 1 delta
)
10432 deltadef
(nth 2 delta
)))
10434 ;; Check if there is an iso week date in there
10435 ;; If yes, sore the info and ostpone interpreting it until the rest
10436 ;; of the parsing is done
10437 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans
)
10438 (setq iso-year
(if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans
))))
10439 iso-weekday
(if (match-end 3) (string-to-number (match-string 3 ans
)))
10440 iso-week
(string-to-number (match-string 2 ans
)))
10441 (setq ans
(replace-match "" t t ans
)))
10443 ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
10444 (when (string-match
10445 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans
)
10446 (setq year
(if (match-end 2)
10447 (string-to-number (match-string 2 ans
))
10448 (string-to-number (format-time-string "%Y")))
10449 month
(string-to-number (match-string 3 ans
))
10450 day
(string-to-number (match-string 4 ans
)))
10451 (if (< year
100) (setq year
(+ 2000 year
)))
10452 (setq ans
(replace-match (format "%04d-%02d-%02d\\5" year month day
)
10454 ;; Help matching am/pm times, because `parse-time-string' does not do that.
10455 ;; If there is a time with am/pm, and *no* time without it, we convert
10456 ;; so that matching will be successful.
10457 (loop for i from
1 to
2 do
; twice, for end time as well
10458 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans
))
10459 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans
))
10460 (setq hour
(string-to-number (match-string 1 ans
))
10461 minute
(if (match-end 3)
10462 (string-to-number (match-string 3 ans
))
10465 (string-to-char (downcase (match-string 4 ans
)))))
10466 (if (and (= hour
12) (not pm
))
10468 (if (and pm
(< hour
12)) (setq hour
(+ 12 hour
))))
10469 (setq ans
(replace-match (format "%02d:%02d" hour minute
)
10472 ;; Check if a time range is given as a duration
10473 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans
)
10474 (setq hour
(string-to-number (match-string 1 ans
))
10475 h2
(+ hour
(string-to-number (match-string 3 ans
)))
10476 minute
(string-to-number (match-string 2 ans
))
10477 m2
(+ minute
(if (match-end 5) (string-to-number
10478 (match-string 5 ans
))0)))
10479 (if (>= m2
60) (setq h2
(1+ h2
) m2
(- m2
60)))
10480 (setq ans
(replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2
)
10483 ;; Check if there is a time range
10484 (when (boundp 'org-end-time-was-given
)
10485 (setq org-time-was-given nil
)
10486 (when (and (string-match org-plain-time-of-day-regexp ans
)
10488 (setq org-end-time-was-given
(match-string 8 ans
))
10489 (setq ans
(concat (substring ans
0 (match-beginning 7))
10490 (substring ans
(match-end 7))))))
10492 (setq tl
(parse-time-string ans
)
10493 day
(or (nth 3 tl
) (nth 3 defdecode
))
10494 month
(or (nth 4 tl
)
10495 (if (and org-read-date-prefer-future
10496 (nth 3 tl
) (< (nth 3 tl
) (nth 3 defdecode
)))
10497 (1+ (nth 4 defdecode
))
10498 (nth 4 defdecode
)))
10499 year
(or (nth 5 tl
)
10500 (if (and org-read-date-prefer-future
10501 (nth 4 tl
) (< (nth 4 tl
) (nth 4 defdecode
)))
10502 (1+ (nth 5 defdecode
))
10503 (nth 5 defdecode
)))
10504 hour
(or (nth 2 tl
) (nth 2 defdecode
))
10505 minute
(or (nth 1 tl
) (nth 1 defdecode
))
10506 second
(or (nth 0 tl
) 0)
10509 ;; Special date definitions below
10512 ;; There was an iso week
10513 (setq year
(or iso-year year
)
10514 day
(or iso-weekday wday
1)
10515 wday nil
; to make sure that the trigger below does not match
10516 iso-date
(calendar-gregorian-from-absolute
10517 (calendar-absolute-from-iso
10518 (list iso-week day year
))))
10519 ; FIXME: Should we also push ISO weeks into the future?
10520 ; (when (and org-read-date-prefer-future
10522 ; (< (calendar-absolute-from-gregorian iso-date)
10523 ; (time-to-days (current-time))))
10524 ; (setq year (1+ year)
10525 ; iso-date (calendar-gregorian-from-absolute
10526 ; (calendar-absolute-from-iso
10527 ; (list iso-week day year)))))
10528 (setq month
(car iso-date
)
10529 year
(nth 2 iso-date
)
10530 day
(nth 1 iso-date
)))
10533 (let ((now (decode-time (current-time))))
10534 (setq day
(nth 3 now
) month
(nth 4 now
) year
(nth 5 now
))))
10535 (cond ((member deltaw
'("d" "")) (setq day
(+ day deltan
)))
10536 ((equal deltaw
"w") (setq day
(+ day
(* 7 deltan
))))
10537 ((equal deltaw
"m") (setq month
(+ month deltan
)))
10538 ((equal deltaw
"y") (setq year
(+ year deltan
)))))
10539 ((and wday
(not (nth 3 tl
)))
10540 ;; Weekday was given, but no day, so pick that day in the week
10541 ;; on or after the derived date.
10542 (setq wday1
(nth 6 (decode-time (encode-time 0 0 0 day month year
))))
10543 (unless (equal wday wday1
)
10544 (setq day
(+ day
(%
(- wday wday1 -
7) 7))))))
10545 (if (and (boundp 'org-time-was-given
)
10547 (setq org-time-was-given t
))
10548 (if (< year
100) (setq year
(+ 2000 year
)))
10549 (if (< year
1970) (setq year
(nth 5 defdecode
))) ; not representable
10550 (list second minute hour day month year
)))
10552 (defvar parse-time-weekdays
)
10554 (defun org-read-date-get-relative (s today default
)
10555 "Check string S for special relative date string.
10556 TODAY and DEFAULT are internal times, for today and for a default.
10557 Return shift list (N what def-flag)
10558 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
10559 N is the number of WHATs to shift.
10560 DEF-FLAG is t when a double ++ or -- indicates shift relative to
10561 the DEFAULT date rather than TODAY."
10565 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
10567 "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays
"\\|") "\\)\\)?"
10568 "\\([ \t]\\|$\\)") s
)
10569 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
10570 (let* ((dir (if (> (match-end 1) (match-beginning 1))
10571 (string-to-char (substring (match-string 1 s
) -
1))
10573 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
10574 (n (if (match-end 2) (string-to-number (match-string 2 s
)) 1))
10575 (what (if (match-end 3) (match-string 3 s
) "d"))
10576 (wday1 (cdr (assoc (downcase what
) parse-time-weekdays
)))
10577 (date (if rel default today
))
10578 (wday (nth 6 (decode-time date
)))
10582 (setq delta
(mod (+ 7 (- wday1 wday
)) 7))
10583 (if (= dir ?-
) (setq delta
(- delta
7)))
10584 (if (> n
1) (setq delta
(+ delta
(* (1- n
) (if (= dir ?-
) -
7 7)))))
10585 (list delta
"d" rel
))
10586 (list (* n
(if (= dir ?-
) -
1 1)) what rel
)))))
10588 (defun org-eval-in-calendar (form &optional keepdate
)
10589 "Eval FORM in the calendar window and return to current window.
10590 Also, store the cursor date in variable org-ans2."
10591 (let ((sw (selected-window)))
10592 (select-window (get-buffer-window "*Calendar*"))
10594 (when (and (not keepdate
) (calendar-cursor-to-date))
10595 (let* ((date (calendar-cursor-to-date))
10596 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
10597 (setq org-ans2
(format-time-string "%Y-%m-%d" time
))))
10598 (org-move-overlay org-date-ovl
(1- (point)) (1+ (point)) (current-buffer))
10599 (select-window sw
)))
10601 ; ;; Update the prompt to show new default date
10603 ; (goto-char (point-min))
10604 ; (when (and org-ans2
10605 ; (re-search-forward "\\[[-0-9]+\\]" nil t)
10606 ; (get-text-property (match-end 0) 'field))
10607 ; (let ((inhibit-read-only t))
10608 ; (replace-match (concat "[" org-ans2 "]") t t)
10609 ; (add-text-properties (point-min) (1+ (match-end 0))
10610 ; (text-properties-at (1+ (point-min)))))))))
10612 (defun org-calendar-select ()
10613 "Return to `org-read-date' with the date currently selected.
10614 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
10616 (when (calendar-cursor-to-date)
10617 (let* ((date (calendar-cursor-to-date))
10618 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
10619 (setq org-ans1
(format-time-string "%Y-%m-%d" time
)))
10620 (if (active-minibuffer-window) (exit-minibuffer))))
10622 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra
)
10623 "Insert a date stamp for the date given by the internal TIME.
10624 WITH-HM means, use the stamp format that includes the time of the day.
10625 INACTIVE means use square brackets instead of angular ones, so that the
10626 stamp will not contribute to the agenda.
10627 PRE and POST are optional strings to be inserted before and after the
10629 The command returns the inserted time stamp."
10630 (let ((fmt (funcall (if with-hm
'cdr
'car
) org-time-stamp-formats
))
10632 (if inactive
(setq fmt
(concat "[" (substring fmt
1 -
1) "]")))
10633 (insert-before-markers (or pre
""))
10634 (insert-before-markers (setq stamp
(format-time-string fmt time
)))
10635 (when (listp extra
)
10636 (setq extra
(car extra
))
10637 (if (and (stringp extra
)
10638 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra
))
10639 (setq extra
(format "-%02d:%02d"
10640 (string-to-number (match-string 1 extra
))
10641 (string-to-number (match-string 2 extra
))))
10645 (insert-before-markers extra
)
10647 (insert-before-markers (or post
""))
10650 (defun org-toggle-time-stamp-overlays ()
10651 "Toggle the use of custom time stamp formats."
10653 (setq org-display-custom-times
(not org-display-custom-times
))
10654 (unless org-display-custom-times
10655 (let ((p (point-min)) (bmp (buffer-modified-p)))
10656 (while (setq p
(next-single-property-change p
'display
))
10657 (if (and (get-text-property p
'display
)
10658 (eq (get-text-property p
'face
) 'org-date
))
10659 (remove-text-properties
10660 p
(setq p
(next-single-property-change p
'display
))
10662 (set-buffer-modified-p bmp
)))
10663 (if (featurep 'xemacs
)
10664 (remove-text-properties (point-min) (point-max) '(end-glyph t
)))
10665 (org-restart-font-lock)
10666 (setq org-table-may-need-update t
)
10667 (if org-display-custom-times
10668 (message "Time stamps are overlayed with custom format")
10669 (message "Time stamp overlays removed")))
10671 (defun org-display-custom-time (beg end
)
10672 "Overlay modified time stamp format over timestamp between BEG and END."
10673 (let* ((ts (buffer-substring beg end
))
10674 t1 w1 with-hm tf time str w2
(off 0))
10676 (setq t1
(org-parse-time-string ts t
))
10677 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts
)
10678 (setq off
(- (match-end 0) (match-beginning 0)))))
10679 (setq end
(- end off
))
10680 (setq w1
(- end beg
)
10681 with-hm
(and (nth 1 t1
) (nth 2 t1
))
10682 tf
(funcall (if with-hm
'cdr
'car
) org-time-stamp-custom-formats
)
10683 time
(org-fix-decoded-time t1
)
10685 (format-time-string
10686 (substring tf
1 -
1) (apply 'encode-time time
))
10687 nil
'mouse-face
'highlight
)
10689 (if (not (= w2 w1
))
10690 (add-text-properties (1+ beg
) (+ 2 beg
)
10691 (list 'org-dwidth t
'org-dwidth-n
(- w1 w2
))))
10692 (if (featurep 'xemacs
)
10694 (put-text-property beg end
'invisible t
)
10695 (put-text-property beg end
'end-glyph
(make-glyph str
)))
10696 (put-text-property beg end
'display str
))))
10698 (defun org-translate-time (string)
10699 "Translate all timestamps in STRING to custom format.
10700 But do this only if the variable `org-display-custom-times' is set."
10701 (when org-display-custom-times
10704 (re org-ts-regexp-both
)
10705 t1 with-hm inactive tf time str beg end
)
10706 (while (setq start
(string-match re string start
))
10707 (setq beg
(match-beginning 0)
10709 t1
(save-match-data
10710 (org-parse-time-string (substring string beg end
) t
))
10711 with-hm
(and (nth 1 t1
) (nth 2 t1
))
10712 inactive
(equal (substring string beg
(1+ beg
)) "[")
10713 tf
(funcall (if with-hm
'cdr
'car
)
10714 org-time-stamp-custom-formats
)
10715 time
(org-fix-decoded-time t1
)
10716 str
(format-time-string
10718 (if inactive
"[" "<") (substring tf
1 -
1)
10719 (if inactive
"]" ">"))
10720 (apply 'encode-time time
))
10721 string
(replace-match str t t string
)
10722 start
(+ start
(length str
)))))))
10725 (defun org-fix-decoded-time (time)
10726 "Set 0 instead of nil for the first 6 elements of time.
10727 Don't touch the rest."
10729 (mapcar (lambda (x) (if (< (setq n
(1+ n
)) 7) (or x
0) x
)) time
)))
10731 (defun org-days-to-time (timestamp-string)
10732 "Difference between TIMESTAMP-STRING and now in days."
10733 (- (time-to-days (org-time-string-to-time timestamp-string
))
10734 (time-to-days (current-time))))
10736 (defun org-deadline-close (timestamp-string &optional ndays
)
10737 "Is the time in TIMESTAMP-STRING close to the current date?"
10738 (setq ndays
(or ndays
(org-get-wdays timestamp-string
)))
10739 (and (< (org-days-to-time timestamp-string
) ndays
)
10740 (not (org-entry-is-done-p))))
10742 (defun org-get-wdays (ts)
10743 "Get the deadline lead time appropriate for timestring TS."
10745 ((<= org-deadline-warning-days
0)
10746 ;; 0 or negative, enforce this value no matter what
10747 (- org-deadline-warning-days
))
10748 ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts
)
10749 ;; lead time is specified.
10750 (floor (* (string-to-number (match-string 1 ts
))
10751 (cdr (assoc (match-string 2 ts
)
10752 '(("d" .
1) ("w" .
7)
10753 ("m" .
30.4) ("y" .
365.25)))))))
10754 ;; go for the default.
10755 (t org-deadline-warning-days
)))
10757 (defun org-calendar-select-mouse (ev)
10758 "Return to `org-read-date' with the date currently selected.
10759 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
10761 (mouse-set-point ev
)
10762 (when (calendar-cursor-to-date)
10763 (let* ((date (calendar-cursor-to-date))
10764 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
10765 (setq org-ans1
(format-time-string "%Y-%m-%d" time
)))
10766 (if (active-minibuffer-window) (exit-minibuffer))))
10768 (defun org-check-deadlines (ndays)
10769 "Check if there are any deadlines due or past due.
10770 A deadline is considered due if it happens within `org-deadline-warning-days'
10771 days from today's date. If the deadline appears in an entry marked DONE,
10772 it is not shown. The prefix arg NDAYS can be used to test that many
10773 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
10775 (let* ((org-warn-days
10777 ((equal ndays
'(4)) 100000)
10778 (ndays (prefix-numeric-value ndays
))
10779 (t (abs org-deadline-warning-days
))))
10780 (case-fold-search nil
)
10781 (regexp (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>"))
10783 (lambda () (org-deadline-close (match-string 1) org-warn-days
))))
10785 (message "%d deadlines past-due or due within %d days"
10786 (org-occur regexp nil callback
)
10789 (defun org-check-before-date (date)
10790 "Check if there are deadlines or scheduled entries before DATE."
10791 (interactive (list (org-read-date)))
10792 (let ((case-fold-search nil
)
10793 (regexp (concat "\\<\\(" org-deadline-string
10794 "\\|" org-scheduled-string
10795 "\\) *<\\([^>]+\\)>"))
10797 (lambda () (time-less-p
10798 (org-time-string-to-time (match-string 2))
10799 (org-time-string-to-time date
)))))
10800 (message "%d entries before %s"
10801 (org-occur regexp nil callback
) date
)))
10803 (defun org-evaluate-time-range (&optional to-buffer
)
10804 "Evaluate a time range by computing the difference between start and end.
10805 Normally the result is just printed in the echo area, but with prefix arg
10806 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
10807 If the time range is actually in a table, the result is inserted into the
10809 For time difference computation, a year is assumed to be exactly 365
10810 days in order to avoid rounding problems."
10813 (org-clock-update-time-maybe)
10815 (unless (org-at-date-range-p t
)
10816 (goto-char (point-at-bol))
10817 (re-search-forward org-tr-regexp-both
(point-at-eol) t
))
10818 (if (not (org-at-date-range-p t
))
10819 (error "Not at a time-stamp range, and none found in current line")))
10820 (let* ((ts1 (match-string 1))
10821 (ts2 (match-string 2))
10822 (havetime (or (> (length ts1
) 15) (> (length ts2
) 15)))
10823 (match-end (match-end 0))
10824 (time1 (org-time-string-to-time ts1
))
10825 (time2 (org-time-string-to-time ts2
))
10826 (t1 (time-to-seconds time1
))
10827 (t2 (time-to-seconds time2
))
10828 (diff (abs (- t2 t1
)))
10829 (negative (< (- t2 t1
) 0))
10830 ;; (ys (floor (* 365 24 60 60)))
10833 (fy "%dy %dd %02d:%02d")
10835 (fd "%dd %02d:%02d")
10840 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
10842 d
(floor (/ diff ds
)) diff
(mod diff ds
)
10843 h
(floor (/ diff hs
)) diff
(mod diff hs
)
10844 m
(floor (/ diff
60)))
10845 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
10847 d
(floor (+ (/ diff ds
) 0.5))
10849 (if (not to-buffer
)
10850 (message "%s" (org-make-tdiff-string y d h m
))
10851 (if (org-at-table-p)
10853 (goto-char match-end
)
10855 (and (looking-at " *|") (goto-char (match-end 0))))
10856 (goto-char match-end
))
10858 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
10859 (replace-match ""))
10860 (if negative
(insert " -"))
10861 (if (> y
0) (insert " " (format (if havetime fy fy1
) y d h m
))
10862 (if (> d
0) (insert " " (format (if havetime fd fd1
) d h m
))
10863 (insert " " (format fh h m
))))
10864 (if align
(org-table-align))
10865 (message "Time difference inserted")))))
10867 (defun org-make-tdiff-string (y d h m
)
10870 (if (> y
0) (setq fmt
(concat fmt
"%d year" (if (> y
1) "s" "") " ")
10872 (if (> d
0) (setq fmt
(concat fmt
"%d day" (if (> d
1) "s" "") " ")
10874 (if (> h
0) (setq fmt
(concat fmt
"%d hour" (if (> h
1) "s" "") " ")
10876 (if (> m
0) (setq fmt
(concat fmt
"%d minute" (if (> m
1) "s" "") " ")
10878 (apply 'format fmt
(nreverse l
))))
10880 (defun org-time-string-to-time (s)
10881 (apply 'encode-time
(org-parse-time-string s
)))
10883 (defun org-time-string-to-absolute (s &optional daynr prefer show-all
)
10884 "Convert a time stamp to an absolute day number.
10885 If there is a specifyer for a cyclic time stamp, get the closest date to
10887 PREFER and SHOW_ALL are passed through to `org-closest-date'."
10889 ((and daynr
(string-match "\\`%%\\((.*)\\)" s
))
10890 (if (org-diary-sexp-entry (match-string 1 s
) "" date
)
10893 ((and daynr
(string-match "\\+[0-9]+[dwmy]" s
))
10894 (org-closest-date s
(if (and (boundp 'daynr
) (integerp daynr
)) daynr
10895 (time-to-days (current-time))) (match-string 0 s
)
10897 (t (time-to-days (apply 'encode-time
(org-parse-time-string s
))))))
10899 (defun org-days-to-iso-week (days)
10900 "Return the iso week number."
10902 (car (calendar-iso-from-absolute days
)))
10904 (defun org-small-year-to-year (year)
10905 "Convert 2-digit years into 4-digit years.
10906 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
10907 The year 2000 cannot be abbreviated. Any year lager than 99
10908 is retrned unchanged."
10910 (setq year
(+ 2000 year
))
10912 (setq year
(+ 1900 year
))))
10915 (defun org-time-from-absolute (d)
10916 "Return the time corresponding to date D.
10917 D may be an absolute day number, or a calendar-type list (month day year)."
10918 (if (numberp d
) (setq d
(calendar-gregorian-from-absolute d
)))
10919 (encode-time 0 0 0 (nth 1 d
) (car d
) (nth 2 d
)))
10921 (defun org-calendar-holiday ()
10922 "List of holidays, for Diary display in Org-mode."
10923 (require 'holidays
)
10925 (if (fboundp 'calendar-check-holidays
)
10926 'calendar-check-holidays
'check-calendar-holidays
) date
)))
10927 (if hl
(mapconcat 'identity hl
"; "))))
10929 (defun org-diary-sexp-entry (sexp entry date
)
10930 "Process a SEXP diary ENTRY for DATE."
10931 (require 'diary-lib
)
10932 (let ((result (if calendar-debug-sexp
10933 (let ((stack-trace-on-error t
))
10934 (eval (car (read-from-string sexp
))))
10935 (condition-case nil
10936 (eval (car (read-from-string sexp
)))
10939 (message "Bad sexp at line %d in %s: %s"
10941 (buffer-file-name) sexp
)
10943 (cond ((stringp result
) result
)
10944 ((and (consp result
)
10945 (stringp (cdr result
))) (cdr result
))
10949 (defun org-diary-to-ical-string (frombuf)
10950 "Get iCalendar entries from diary entries in buffer FROMBUF.
10951 This uses the icalendar.el library."
10952 (let* ((tmpdir (if (featurep 'xemacs
)
10954 temporary-file-directory
))
10955 (tmpfile (make-temp-name
10956 (expand-file-name "orgics" tmpdir
)))
10959 (set-buffer frombuf
)
10960 (icalendar-export-region (point-min) (point-max) tmpfile
)
10961 (setq buf
(find-buffer-visiting tmpfile
))
10963 (goto-char (point-min))
10964 (if (re-search-forward "^BEGIN:VEVENT" nil t
)
10965 (setq b
(match-beginning 0)))
10966 (goto-char (point-max))
10967 (if (re-search-backward "^END:VEVENT" nil t
)
10968 (setq e
(match-end 0)))
10969 (setq rtn
(if (and b e
) (concat (buffer-substring b e
) "\n") "")))
10971 (kill-buffer frombuf
)
10972 (delete-file tmpfile
)
10975 (defun org-closest-date (start current change prefer show-all
)
10976 "Find the date closest to CURRENT that is consistent with START and CHANGE.
10977 When PREFER is `past' return a date that is either CURRENT or past.
10978 When PREFER is `future', return a date that is either CURRENT or future.
10979 When SHOW-ALL is nil, only return the current occurence of a time stamp."
10980 ;; Make the proper lists from the dates
10982 (let ((a1 '(("d" . day
) ("w" . week
) ("m" . month
) ("y" . year
)))
10983 dn dw sday cday n1 n2
10984 d m y y1 y2 date1 date2 nmonths nm ny m2
)
10986 (setq start
(org-date-to-gregorian start
)
10987 current
(org-date-to-gregorian
10990 (time-to-days (current-time))))
10991 sday
(calendar-absolute-from-gregorian start
)
10992 cday
(calendar-absolute-from-gregorian current
))
10994 (if (<= cday sday
) (throw 'exit sday
))
10996 (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change
)
10997 (setq dn
(string-to-number (match-string 1 change
))
10998 dw
(cdr (assoc (match-string 2 change
) a1
)))
10999 (error "Invalid change specifyer: %s" change
))
11000 (if (eq dw
'week
) (setq dw
'day dn
(* 7 dn
)))
11003 (setq n1
(+ sday
(* dn
(floor (/ (- cday sday
) dn
))))
11006 (setq d
(nth 1 start
) m
(car start
) y1
(nth 2 start
) y2
(nth 2 current
))
11007 (setq y1
(+ (* (floor (/ (- y2 y1
) dn
)) dn
) y1
))
11008 (setq date1
(list m d y1
)
11009 n1
(calendar-absolute-from-gregorian date1
)
11010 date2
(list m d
(+ y1
(* (if (< n1 cday
) 1 -
1) dn
)))
11011 n2
(calendar-absolute-from-gregorian date2
)))
11013 ;; approx number of month between the tow dates
11014 (setq nmonths
(floor (/ (- cday sday
) 30.436875)))
11015 ;; How often does dn fit in there?
11016 (setq d
(nth 1 start
) m
(car start
) y
(nth 2 start
)
11017 nm
(* dn
(max 0 (1- (floor (/ nmonths dn
)))))
11019 ny
(floor (/ m
12))
11022 (while (> m
12) (setq m
(- m
12) y
(1+ y
)))
11023 (setq n1
(calendar-absolute-from-gregorian (list m d y
)))
11024 (setq m2
(+ m dn
) y2 y
)
11025 (if (> m2
12) (setq y2
(1+ y2
) m2
(- m2
12)))
11026 (setq n2
(calendar-absolute-from-gregorian (list m2 d y2
)))
11028 (setq n1 n2 m m2 y y2
)
11029 (setq m2
(+ m dn
) y2 y
)
11030 (if (> m2
12) (setq y2
(1+ y2
) m2
(- m2
12)))
11031 (setq n2
(calendar-absolute-from-gregorian (list m2 d y2
))))))
11035 ((eq prefer
'past
) n1
)
11036 ((eq prefer
'future
) (if (= cday n1
) n1 n2
))
11037 (t (if (> (abs (- cday n1
)) (abs (- cday n2
))) n2 n1
)))
11039 ((eq prefer
'past
) n1
)
11040 ((eq prefer
'future
) (if (= cday n1
) n1 n2
))
11041 (t (if (= cday n1
) n1 n2
)))))))
11043 (defun org-date-to-gregorian (date)
11044 "Turn any specification of DATE into a gregorian date for the calendar."
11045 (cond ((integerp date
) (calendar-gregorian-from-absolute date
))
11046 ((and (listp date
) (= (length date
) 3)) date
)
11048 (setq date
(org-parse-time-string date
))
11049 (list (nth 4 date
) (nth 3 date
) (nth 5 date
)))
11051 (list (nth 4 date
) (nth 3 date
) (nth 5 date
)))))
11053 (defun org-parse-time-string (s &optional nodefault
)
11054 "Parse the standard Org-mode time string.
11055 This should be a lot faster than the normal `parse-time-string'.
11056 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
11057 hour and minute fields will be nil if not given."
11058 (if (string-match org-ts-regexp0 s
)
11060 (if (or (match-beginning 8) (not nodefault
))
11061 (string-to-number (or (match-string 8 s
) "0")))
11062 (if (or (match-beginning 7) (not nodefault
))
11063 (string-to-number (or (match-string 7 s
) "0")))
11064 (string-to-number (match-string 4 s
))
11065 (string-to-number (match-string 3 s
))
11066 (string-to-number (match-string 2 s
))
11070 (defun org-timestamp-up (&optional arg
)
11071 "Increase the date item at the cursor by one.
11072 If the cursor is on the year, change the year. If it is on the month or
11073 the day, change that.
11074 With prefix ARG, change by that many units."
11076 (org-timestamp-change (prefix-numeric-value arg
)))
11078 (defun org-timestamp-down (&optional arg
)
11079 "Decrease the date item at the cursor by one.
11080 If the cursor is on the year, change the year. If it is on the month or
11081 the day, change that.
11082 With prefix ARG, change by that many units."
11084 (org-timestamp-change (- (prefix-numeric-value arg
))))
11086 (defun org-timestamp-up-day (&optional arg
)
11087 "Increase the date in the time stamp by one day.
11088 With prefix ARG, change that many days."
11090 (if (and (not (org-at-timestamp-p t
))
11091 (org-on-heading-p))
11093 (org-timestamp-change (prefix-numeric-value arg
) 'day
)))
11095 (defun org-timestamp-down-day (&optional arg
)
11096 "Decrease the date in the time stamp by one day.
11097 With prefix ARG, change that many days."
11099 (if (and (not (org-at-timestamp-p t
))
11100 (org-on-heading-p))
11102 (org-timestamp-change (- (prefix-numeric-value arg
)) 'day
)))
11104 (defun org-at-timestamp-p (&optional inactive-ok
)
11105 "Determine if the cursor is in or at a timestamp."
11107 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2
))
11109 (ans (or (looking-at tsr
)
11111 (skip-chars-backward "^[<\n\r\t")
11112 (if (> (point) (point-min)) (backward-char 1))
11113 (and (looking-at tsr
)
11114 (> (- (match-end 0) pos
) -
1))))))
11116 (boundp 'org-ts-what
)
11119 ((= pos
(match-beginning 0)) 'bracket
)
11120 ((= pos
(1- (match-end 0))) 'bracket
)
11121 ((org-pos-in-match-range pos
2) 'year
)
11122 ((org-pos-in-match-range pos
3) 'month
)
11123 ((org-pos-in-match-range pos
7) 'hour
)
11124 ((org-pos-in-match-range pos
8) 'minute
)
11125 ((or (org-pos-in-match-range pos
4)
11126 (org-pos-in-match-range pos
5)) 'day
)
11127 ((and (> pos
(or (match-end 8) (match-end 5)))
11128 (< pos
(match-end 0)))
11129 (- pos
(or (match-end 8) (match-end 5))))
11133 (defun org-toggle-timestamp-type ()
11134 "Toggle the type (<active> or [inactive]) of a time stamp."
11136 (when (org-at-timestamp-p t
)
11138 (goto-char (match-beginning 0))
11139 (insert (if (equal (char-after) ?
<) "[" "<")) (delete-char 1)
11140 (goto-char (1- (match-end 0)))
11141 (insert (if (equal (char-after) ?
>) "]" ">")) (delete-char 1))
11142 (message "Timestamp is now %sactive"
11143 (if (equal (char-before) ?
>) "in" ""))))
11145 (defun org-timestamp-change (n &optional what
)
11146 "Change the date in the time stamp at point.
11147 The date will be changed by N times WHAT. WHAT can be `day', `month',
11148 `year', `minute', `second'. If WHAT is not given, the cursor position
11149 in the timestamp determines what will be changed."
11150 (let ((pos (point))
11152 (dm (max (nth 1 org-time-stamp-rounding-minutes
) 1))
11156 (if (not (org-at-timestamp-p t
))
11157 (error "Not at a timestamp"))
11158 (if (and (not what
) (eq org-ts-what
'bracket
))
11159 (org-toggle-timestamp-type)
11160 (if (and (not what
) (not (eq org-ts-what
'day
))
11161 org-display-custom-times
11162 (get-text-property (point) 'display
)
11163 (not (get-text-property (1- (point)) 'display
)))
11164 (setq org-ts-what
'day
))
11165 (setq org-ts-what
(or what org-ts-what
)
11166 inactive
(= (char-after (match-beginning 0)) ?\
[)
11167 ts
(match-string 0))
11170 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
11172 (setq extra
(match-string 1 ts
)))
11173 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts
)
11175 (setq time0
(org-parse-time-string ts
))
11176 (when (and (eq org-ts-what
'minute
)
11177 (eq current-prefix-arg nil
))
11178 (setq n
(* dm
(cond ((> n
0) 1) ((< n
0) -
1) (t 0))))
11179 (when (not (= 0 (setq rem
(%
(nth 1 time0
) dm
))))
11180 (setcar (cdr time0
) (+ (nth 1 time0
)
11181 (if (> n
0) (- rem
) (- dm rem
))))))
11183 (encode-time (or (car time0
) 0)
11184 (+ (if (eq org-ts-what
'minute
) n
0) (nth 1 time0
))
11185 (+ (if (eq org-ts-what
'hour
) n
0) (nth 2 time0
))
11186 (+ (if (eq org-ts-what
'day
) n
0) (nth 3 time0
))
11187 (+ (if (eq org-ts-what
'month
) n
0) (nth 4 time0
))
11188 (+ (if (eq org-ts-what
'year
) n
0) (nth 5 time0
))
11190 (when (integerp org-ts-what
)
11191 (setq extra
(org-modify-ts-extra extra org-ts-what n dm
)))
11192 (if (eq what
'calendar
)
11193 (let ((cal-date (org-get-date-from-calendar)))
11194 (setcar (nthcdr 4 time0
) (nth 0 cal-date
)) ; month
11195 (setcar (nthcdr 3 time0
) (nth 1 cal-date
)) ; day
11196 (setcar (nthcdr 5 time0
) (nth 2 cal-date
)) ; year
11197 (setcar time0
(or (car time0
) 0))
11198 (setcar (nthcdr 1 time0
) (or (nth 1 time0
) 0))
11199 (setcar (nthcdr 2 time0
) (or (nth 2 time0
) 0))
11200 (setq time
(apply 'encode-time time0
))))
11201 (setq org-last-changed-timestamp
11202 (org-insert-time-stamp time with-hm inactive nil nil extra
))
11203 (org-clock-update-time-maybe)
11205 ;; Try to recenter the calendar window, if any
11206 (if (and org-calendar-follow-timestamp-change
11207 (get-buffer-window "*Calendar*" t
)
11208 (memq org-ts-what
'(day month year
)))
11209 (org-recenter-calendar (time-to-days time
))))))
11211 (defun org-modify-ts-extra (s pos n dm
)
11212 "Change the different parts of the lead-time and repeat fields in timestamp."
11213 (let ((idx '(("d" .
0) ("w" .
1) ("m" .
2) ("y" .
3) ("d" . -
1) ("y" .
4)))
11215 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s
)
11217 ((or (org-pos-in-match-range pos
2)
11218 (org-pos-in-match-range pos
3))
11219 (setq m
(string-to-number (match-string 3 s
))
11220 h
(string-to-number (match-string 2 s
)))
11221 (if (org-pos-in-match-range pos
2)
11223 (setq n
(* dm
(org-no-warnings (signum n
))))
11224 (when (not (= 0 (setq rem
(% m dm
))))
11225 (setq m
(+ m
(if (> n
0) (- rem
) (- dm rem
)))))
11227 (if (< m
0) (setq m
(+ m
60) h
(1- h
)))
11228 (if (> m
59) (setq m
(- m
60) h
(1+ h
)))
11229 (setq h
(min 24 (max 0 h
)))
11230 (setq ng
1 new
(format "-%02d:%02d" h m
)))
11231 ((org-pos-in-match-range pos
6)
11232 (setq ng
6 new
(car (rassoc (+ n
(cdr (assoc (match-string 6 s
) idx
))) idx
))))
11233 ((org-pos-in-match-range pos
5)
11234 (setq ng
5 new
(format "%d" (max 1 (+ n
(string-to-number (match-string 5 s
)))))))
11236 ((org-pos-in-match-range pos
9)
11237 (setq ng
9 new
(car (rassoc (+ n
(cdr (assoc (match-string 9 s
) idx
))) idx
))))
11238 ((org-pos-in-match-range pos
8)
11239 (setq ng
8 new
(format "%d" (max 0 (+ n
(string-to-number (match-string 8 s
))))))))
11243 (substring s
0 (match-beginning ng
))
11245 (substring s
(match-end ng
))))))
11248 (defun org-recenter-calendar (date)
11249 "If the calendar is visible, recenter it to DATE."
11250 (let* ((win (selected-window))
11251 (cwin (get-buffer-window "*Calendar*" t
))
11252 (calendar-move-hook nil
))
11254 (select-window cwin
)
11255 (calendar-goto-date (if (listp date
) date
11256 (calendar-gregorian-from-absolute date
)))
11257 (select-window win
))))
11259 (defun org-goto-calendar (&optional arg
)
11260 "Go to the Emacs calendar at the current date.
11261 If there is a time stamp in the current line, go to that date.
11262 A prefix ARG can be used to force the current date."
11264 (let ((tsr org-ts-regexp
) diff
11265 (calendar-move-hook nil
)
11266 (calendar-view-holidays-initially-flag nil
)
11267 (view-calendar-holidays-initially nil
)
11268 (calendar-view-diary-initially-flag nil
)
11269 (view-diary-entries-initially nil
))
11270 (if (or (org-at-timestamp-p)
11272 (beginning-of-line 1)
11273 (looking-at (concat ".*" tsr
))))
11274 (let ((d1 (time-to-days (current-time)))
11276 (org-time-string-to-time (match-string 1)))))
11277 (setq diff
(- d2 d1
))))
11279 (calendar-goto-today)
11280 (if (and diff
(not arg
)) (calendar-forward-day diff
))))
11282 (defun org-get-date-from-calendar ()
11283 "Return a list (month day year) of date at point in calendar."
11284 (with-current-buffer "*Calendar*"
11286 (calendar-cursor-to-date))))
11288 (defun org-date-from-calendar ()
11289 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
11290 If there is already a time stamp at the cursor position, update it."
11292 (if (org-at-timestamp-p t
)
11293 (org-timestamp-change 0 'calendar
)
11294 (let ((cal-date (org-get-date-from-calendar)))
11295 (org-insert-time-stamp
11296 (encode-time 0 0 0 (nth 1 cal-date
) (car cal-date
) (nth 2 cal-date
))))))
11298 (defun org-minutes-to-hh:mm-string
(m)
11299 "Compute H:MM from a number of minutes."
11300 (let ((h (/ m
60)))
11301 (setq m
(- m
(* 60 h
)))
11302 (format "%d:%02d" h m
)))
11304 (defun org-hh:mm-string-to-minutes
(s)
11305 "Convert a string H:MM to a number of minutes."
11306 (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s
)
11307 (+ (* (string-to-number (match-string 1 s
)) 60)
11308 (string-to-number (match-string 2 s
)))
11314 (defun org-iswitchb (&optional arg
)
11315 "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
11316 With a prefix argument, restrict available to files.
11317 With two prefix arguments, restrict available buffers to agenda files.
11319 Due to some yet unresolved reason, global function
11320 `iswitchb-mode' needs to be active for this function to work."
11322 (require 'iswitchb
)
11323 (let ((enabled iswitchb-mode
) blist
)
11324 (or enabled
(iswitchb-mode 1))
11325 (setq blist
(cond ((equal arg
'(4)) (org-buffer-list 'files
))
11326 ((equal arg
'(16)) (org-buffer-list 'agenda
))
11327 (t (org-buffer-list))))
11329 (let ((iswitchb-make-buflist-hook
11331 (setq iswitchb-temp-buflist
11332 (mapcar 'buffer-name blist
)))))
11334 (iswitchb-read-buffer
11335 "Switch-to: " nil t
))
11336 (or enabled
(iswitchb-mode -
1))))))
11338 (defun org-buffer-list (&optional predicate tmp
)
11339 "Return a list of Org buffers.
11340 PREDICATE can be either 'export, 'files or 'agenda.
11342 'export restrict the list to Export buffers.
11343 'files restrict the list to buffers visiting Org files.
11344 'agenda restrict the list to buffers visiting agenda files.
11346 If TMP is non-nil, don't include temporary buffers."
11347 (let (filter blist
)
11349 (cond ((eq predicate
'files
) "\.org$")
11350 ((eq predicate
'export
) "\*Org .*Export")
11351 (t "\*Org \\|\.org$")))
11355 (let ((bname (buffer-name b
))
11356 (bfile (buffer-file-name b
)))
11357 (if (and (string-match filter bname
)
11358 (if (eq predicate
'agenda
)
11360 (mapcar (lambda(f) (file-truename f
))
11361 org-agenda-files
)) t
)
11362 (if tmp
(not (string-match "tmp" bname
)) t
)) b
)))
11364 (delete nil blist
)))
11366 (defun org-agenda-files (&optional unrestricted ext
)
11367 "Get the list of agenda files.
11368 Optional UNRESTRICTED means return the full list even if a restriction
11369 is currently in place.
11370 When EXT is non-nil, try to add all files that are created by adding EXT
11371 to the file nemes. Basically, this is a way to add the archive files
11372 to the list, by setting EXT to \"_archive\" If EXT is non-nil, but not
11373 a string, \"_archive\" will be used."
11376 ((and (not unrestricted
) (get 'org-agenda-files
'org-restrict
)))
11377 ((stringp org-agenda-files
) (org-read-agenda-file-list))
11378 ((listp org-agenda-files
) org-agenda-files
)
11379 (t (error "Invalid value of `org-agenda-files'")))))
11380 (setq files
(apply 'append
11381 (mapcar (lambda (f)
11382 (if (file-directory-p f
)
11384 f t org-agenda-file-regexp
)
11387 (when org-agenda-skip-unavailable-files
11388 (setq files
(delq nil
11391 (and (file-readable-p file
) file
)))
11394 (setq ext
(if (and (stringp ext
) (string-match "\\S-" ext
))
11396 (setq files
(apply 'append
11399 (if (file-exists-p (concat f ext
))
11400 (list f
(concat f ext
))
11405 (defun org-edit-agenda-file-list ()
11406 "Edit the list of agenda files.
11407 Depending on setup, this either uses customize to edit the variable
11408 `org-agenda-files', or it visits the file that is holding the list. In the
11409 latter case, the buffer is set up in a way that saving it automatically kills
11410 the buffer and restores the previous window configuration."
11412 (if (stringp org-agenda-files
)
11413 (let ((cw (current-window-configuration)))
11414 (find-file org-agenda-files
)
11415 (org-set-local 'org-window-configuration cw
)
11416 (org-add-hook 'after-save-hook
11418 (set-window-configuration
11419 (prog1 org-window-configuration
11420 (kill-buffer (current-buffer))))
11421 (org-install-agenda-files-menu)
11422 (message "New agenda file list installed"))
11424 (message "%s" (substitute-command-keys
11425 "Edit list and finish with \\[save-buffer]")))
11426 (customize-variable 'org-agenda-files
)))
11428 (defun org-store-new-agenda-file-list (list)
11429 "Set new value for the agenda file list and save it correcly."
11430 (if (stringp org-agenda-files
)
11431 (let ((f org-agenda-files
) b
)
11432 (while (setq b
(find-buffer-visiting f
)) (kill-buffer b
))
11434 (insert (mapconcat 'identity list
"\n") "\n")))
11435 (let ((org-mode-hook nil
) (default-major-mode 'fundamental-mode
))
11436 (setq org-agenda-files list
)
11437 (customize-save-variable 'org-agenda-files org-agenda-files
))))
11439 (defun org-read-agenda-file-list ()
11440 "Read the list of agenda files from a file."
11441 (when (file-directory-p org-agenda-files
)
11442 (error "`org-agenda-files' cannot be a single directory"))
11443 (when (stringp org-agenda-files
)
11445 (insert-file-contents org-agenda-files
)
11446 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
11450 (defun org-cycle-agenda-files ()
11451 "Cycle through the files in `org-agenda-files'.
11452 If the current buffer visits an agenda file, find the next one in the list.
11453 If the current buffer does not, find the first agenda file."
11455 (let* ((fs (org-agenda-files t
))
11456 (files (append fs
(list (car fs
))))
11457 (tcf (if buffer-file-name
(file-truename buffer-file-name
)))
11459 (unless files
(error "No agenda files"))
11461 (while (setq file
(pop files
))
11462 (if (equal (file-truename file
) tcf
)
11464 (find-file (car files
))
11466 (find-file (car fs
)))
11467 (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
11469 (defun org-agenda-file-to-front (&optional to-end
)
11470 "Move/add the current file to the top of the agenda file list.
11471 If the file is not present in the list, it is added to the front. If it is
11472 present, it is moved there. With optional argument TO-END, add/move to the
11475 (let ((org-agenda-skip-unavailable-files nil
)
11476 (file-alist (mapcar (lambda (x)
11477 (cons (file-truename x
) x
))
11478 (org-agenda-files t
)))
11479 (ctf (file-truename buffer-file-name
))
11481 (setq x
(assoc ctf file-alist
) had x
)
11483 (if (not x
) (setq x
(cons ctf
(abbreviate-file-name buffer-file-name
))))
11485 (setq file-alist
(append (delq x file-alist
) (list x
)))
11486 (setq file-alist
(cons x
(delq x file-alist
))))
11487 (org-store-new-agenda-file-list (mapcar 'cdr file-alist
))
11488 (org-install-agenda-files-menu)
11489 (message "File %s to %s of agenda file list"
11490 (if had
"moved" "added") (if to-end
"end" "front"))))
11492 (defun org-remove-file (&optional file
)
11493 "Remove current file from the list of files in variable `org-agenda-files'.
11494 These are the files which are being checked for agenda entries.
11495 Optional argument FILE means, use this file instead of the current."
11497 (let* ((org-agenda-skip-unavailable-files nil
)
11498 (file (or file buffer-file-name
))
11499 (true-file (file-truename file
))
11500 (afile (abbreviate-file-name file
))
11501 (files (delq nil
(mapcar
11503 (if (equal true-file
11506 (org-agenda-files t
)))))
11507 (if (not (= (length files
) (length (org-agenda-files t
))))
11509 (org-store-new-agenda-file-list files
)
11510 (org-install-agenda-files-menu)
11511 (message "Removed file: %s" afile
))
11512 (message "File was not in list: %s (not removed)" afile
))))
11514 (defun org-file-menu-entry (file)
11515 (vector file
(list 'find-file file
) t
))
11517 (defun org-check-agenda-file (file)
11518 "Make sure FILE exists. If not, ask user what to do."
11519 (when (not (file-exists-p file
))
11520 (message "non-existent file %s. [R]emove from list or [A]bort?"
11521 (abbreviate-file-name file
))
11522 (let ((r (downcase (read-char-exclusive))))
11525 (org-remove-file file
)
11526 (throw 'nextfile t
))
11527 (t (error "Abort"))))))
11529 (defun org-get-agenda-file-buffer (file)
11530 "Get a buffer visiting FILE. If the buffer needs to be created, add
11531 it to the list of buffers which might be released later."
11532 (let ((buf (org-find-base-buffer-visiting file
)))
11534 buf
; just return it
11535 ;; Make a new buffer and remember it
11536 (setq buf
(find-file-noselect file
))
11537 (if buf
(push buf org-agenda-new-buffers
))
11540 (defun org-release-buffers (blist)
11541 "Release all buffers in list, asking the user for confirmation when needed.
11542 When a buffer is unmodified, it is just killed. When modified, it is saved
11543 \(if the user agrees) and then killed."
11545 (while (setq buf
(pop blist
))
11546 (setq file
(buffer-file-name buf
))
11547 (when (and (buffer-modified-p buf
)
11549 (y-or-n-p (format "Save file %s? " file
)))
11550 (with-current-buffer buf
(save-buffer)))
11551 (kill-buffer buf
))))
11553 (defun org-prepare-agenda-buffers (files)
11554 "Create buffers for all agenda files, protect archived trees and comments."
11556 (let ((pa '(:org-archived t
))
11557 (pc '(:org-comment t
))
11558 (pall '(:org-archived t
:org-comment t
))
11559 (inhibit-read-only t
)
11560 (rea (concat ":" org-archive-tag
":"))
11564 (while (setq file
(pop files
))
11567 (org-check-agenda-file file
)
11568 (set-buffer (org-get-agenda-file-buffer file
)))
11570 (setq bmp
(buffer-modified-p))
11571 (org-refresh-category-properties)
11572 (setq org-todo-keywords-for-agenda
11573 (append org-todo-keywords-for-agenda org-todo-keywords-1
))
11574 (setq org-done-keywords-for-agenda
11575 (append org-done-keywords-for-agenda org-done-keywords
))
11577 (remove-text-properties (point-min) (point-max) pall
)
11578 (when org-agenda-skip-archived-trees
11579 (goto-char (point-min))
11580 (while (re-search-forward rea nil t
)
11581 (if (org-on-heading-p t
)
11582 (add-text-properties (point-at-bol) (org-end-of-subtree t
) pa
))))
11583 (goto-char (point-min))
11584 (setq re
(concat "^\\*+ +" org-comment-string
"\\>"))
11585 (while (re-search-forward re nil t
)
11586 (add-text-properties
11587 (match-beginning 0) (org-end-of-subtree t
) pc
)))
11588 (set-buffer-modified-p bmp
))))))
11590 ;;;; Embedded LaTeX
11592 (defvar org-cdlatex-mode-map
(make-sparse-keymap)
11593 "Keymap for the minor `org-cdlatex-mode'.")
11595 (org-defkey org-cdlatex-mode-map
"_" 'org-cdlatex-underscore-caret
)
11596 (org-defkey org-cdlatex-mode-map
"^" 'org-cdlatex-underscore-caret
)
11597 (org-defkey org-cdlatex-mode-map
"`" 'cdlatex-math-symbol
)
11598 (org-defkey org-cdlatex-mode-map
"'" 'org-cdlatex-math-modify
)
11599 (org-defkey org-cdlatex-mode-map
"\C-c{" 'cdlatex-environment
)
11601 (defvar org-cdlatex-texmathp-advice-is-done nil
11602 "Flag remembering if we have applied the advice to texmathp already.")
11604 (define-minor-mode org-cdlatex-mode
11605 "Toggle the minor `org-cdlatex-mode'.
11606 This mode supports entering LaTeX environment and math in LaTeX fragments
11608 \\{org-cdlatex-mode-map}"
11610 (when org-cdlatex-mode
(require 'cdlatex
))
11611 (unless org-cdlatex-texmathp-advice-is-done
11612 (setq org-cdlatex-texmathp-advice-is-done t
)
11613 (defadvice texmathp
(around org-math-always-on activate
)
11614 "Always return t in org-mode buffers.
11615 This is because we want to insert math symbols without dollars even outside
11616 the LaTeX math segments. If Orgmode thinks that point is actually inside
11617 en embedded LaTeX fragement, let texmathp do its job.
11618 \\[org-cdlatex-mode-map]"
11622 ((not (org-mode-p)) ad-do-it
)
11623 ((eq this-command
'cdlatex-math-symbol
)
11624 (setq ad-return-value t
11625 texmathp-why
'("cdlatex-math-symbol in org-mode" .
0)))
11627 (let ((p (org-inside-LaTeX-fragment-p)))
11628 (if (and p
(member (car p
) (plist-get org-format-latex-options
:matchers
)))
11629 (setq ad-return-value t
11630 texmathp-why
'("Org-mode embedded math" .
0))
11631 (if p ad-do-it
)))))))))
11633 (defun turn-on-org-cdlatex ()
11634 "Unconditionally turn on `org-cdlatex-mode'."
11635 (org-cdlatex-mode 1))
11637 (defun org-inside-LaTeX-fragment-p ()
11638 "Test if point is inside a LaTeX fragment.
11639 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
11640 sequence appearing also before point.
11641 Even though the matchers for math are configurable, this function assumes
11642 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
11643 delimiters are skipped when they have been removed by customization.
11644 The return value is nil, or a cons cell with the delimiter and
11645 and the position of this delimiter.
11647 This function does a reasonably good job, but can locally be fooled by
11648 for example currency specifications. For example it will assume being in
11649 inline math after \"$22.34\". The LaTeX fragment formatter will only format
11650 fragments that are properly closed, but during editing, we have to live
11651 with the uncertainty caused by missing closing delimiters. This function
11652 looks only before point, not after."
11654 (let ((pos (point))
11655 (dodollar (member "$" (plist-get org-format-latex-options
:matchers
)))
11657 (re-search-backward (concat "^\\(" paragraph-start
"\\)") nil t
)
11659 dd-on str
(start 0) m re
)
11662 (setq str
(concat (buffer-substring lim
(point)) "\000 X$.")
11663 re
(nth 1 (assoc "$" org-latex-regexps
)))
11664 (while (string-match re str start
)
11666 ((= (match-end 0) (length str
))
11667 (throw 'exit
(cons "$" (+ lim
(match-beginning 0) 1))))
11668 ((= (match-end 0) (- (length str
) 5))
11670 (t (setq start
(match-end 0))))))
11671 (when (setq m
(re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t
))
11673 (and (match-beginning 1) (throw 'exit
(cons (match-string 1) m
)))
11674 (and (match-beginning 2) (throw 'exit nil
))
11676 (while (re-search-backward "\\$\\$" lim t
)
11677 (setq dd-on
(not dd-on
)))
11679 (if dd-on
(cons "$$" m
))))))
11682 (defun org-try-cdlatex-tab ()
11683 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
11684 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
11685 - inside a LaTeX fragment, or
11686 - after the first word in a line, where an abbreviation expansion could
11687 insert a LaTeX environment."
11688 (when org-cdlatex-mode
11691 (skip-chars-backward "a-zA-Z0-9*")
11692 (skip-chars-backward " \t")
11695 ((org-inside-LaTeX-fragment-p)
11699 (defun org-cdlatex-underscore-caret (&optional arg
)
11700 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
11701 Revert to the normal definition outside of these fragments."
11703 (if (org-inside-LaTeX-fragment-p)
11704 (call-interactively 'cdlatex-sub-superscript
)
11705 (let (org-cdlatex-mode)
11706 (call-interactively (key-binding (vector last-input-event
))))))
11708 (defun org-cdlatex-math-modify (&optional arg
)
11709 "Execute `cdlatex-math-modify' in LaTeX fragments.
11710 Revert to the normal definition outside of these fragments."
11712 (if (org-inside-LaTeX-fragment-p)
11713 (call-interactively 'cdlatex-math-modify
)
11714 (let (org-cdlatex-mode)
11715 (call-interactively (key-binding (vector last-input-event
))))))
11717 (defvar org-latex-fragment-image-overlays nil
11718 "List of overlays carrying the images of latex fragments.")
11719 (make-variable-buffer-local 'org-latex-fragment-image-overlays
)
11721 (defun org-remove-latex-fragment-image-overlays ()
11722 "Remove all overlays with LaTeX fragment images in current buffer."
11723 (mapc 'org-delete-overlay org-latex-fragment-image-overlays
)
11724 (setq org-latex-fragment-image-overlays nil
))
11726 (defun org-preview-latex-fragment (&optional subtree
)
11727 "Preview the LaTeX fragment at point, or all locally or globally.
11728 If the cursor is in a LaTeX fragment, create the image and overlay
11729 it over the source code. If there is no fragment at point, display
11730 all fragments in the current text, from one headline to the next. With
11731 prefix SUBTREE, display all fragments in the current subtree. With a
11732 double prefix `C-u C-u', or when the cursor is before the first headline,
11733 display all fragments in the buffer.
11734 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
11736 (org-remove-latex-fragment-image-overlays)
11739 (let (beg end at msg
)
11741 ((or (equal subtree
'(16))
11742 (not (save-excursion
11743 (re-search-backward (concat "^" outline-regexp
) nil t
))))
11744 (setq beg
(point-min) end
(point-max)
11745 msg
"Creating images for buffer...%s"))
11746 ((equal subtree
'(4))
11747 (org-back-to-heading)
11748 (setq beg
(point) end
(org-end-of-subtree t
)
11749 msg
"Creating images for subtree...%s"))
11751 (if (setq at
(org-inside-LaTeX-fragment-p))
11752 (goto-char (max (point-min) (- (cdr at
) 2)))
11753 (org-back-to-heading))
11754 (setq beg
(point) end
(progn (outline-next-heading) (point))
11755 msg
(if at
"Creating image...%s"
11756 "Creating images for entry...%s"))))
11758 (narrow-to-region beg end
)
11761 (concat "ltxpng/" (file-name-sans-extension
11762 (file-name-nondirectory
11763 buffer-file-name
)))
11764 default-directory
'overlays msg at
'forbuffer
)
11765 (message msg
"done. Use `C-c C-c' to remove images.")))))
11767 (defvar org-latex-regexps
11768 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t
)
11769 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
11770 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
11771 ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil
)
11772 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil
)
11773 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t
)
11774 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t
))
11775 "Regular expressions for matching embedded LaTeX.")
11777 (defun org-format-latex (prefix &optional dir overlays msg at forbuffer
)
11778 "Replace LaTeX fragments with links to an image, and produce images."
11779 (if (and overlays
(fboundp 'clear-image-cache
)) (clear-image-cache))
11780 (let* ((prefixnodir (file-name-nondirectory prefix
))
11781 (absprefix (expand-file-name prefix dir
))
11782 (todir (file-name-directory absprefix
))
11783 (opt org-format-latex-options
)
11784 (matchers (plist-get opt
:matchers
))
11785 (re-list org-latex-regexps
)
11786 (cnt 0) txt link beg end re e checkdir
11787 m n block linkfile movefile ov
)
11788 ;; Check if there are old images files with this prefix, and remove them
11789 (when (file-directory-p todir
)
11793 (concat (regexp-quote prefixnodir
) "_[0-9]+\\.png$"))))
11794 ;; Check the different regular expressions
11795 (while (setq e
(pop re-list
))
11796 (setq m
(car e
) re
(nth 1 e
) n
(nth 2 e
)
11797 block
(if (nth 3 e
) "\n\n" ""))
11798 (when (member m matchers
)
11799 (goto-char (point-min))
11800 (while (re-search-forward re nil t
)
11801 (when (or (not at
) (equal (cdr at
) (match-beginning n
)))
11802 (setq txt
(match-string n
)
11803 beg
(match-beginning n
) end
(match-end n
)
11805 linkfile
(format "%s_%04d.png" prefix cnt
)
11806 movefile
(format "%s_%04d.png" absprefix cnt
)
11807 link
(concat block
"[[file:" linkfile
"]]" block
))
11808 (if msg
(message msg cnt
))
11810 (unless checkdir
; make sure the directory exists
11812 (or (file-directory-p todir
) (make-directory todir
)))
11813 (org-create-formula-image
11814 txt movefile opt forbuffer
)
11817 (setq ov
(org-make-overlay beg end
))
11818 (if (featurep 'xemacs
)
11820 (org-overlay-put ov
'invisible t
)
11823 (make-glyph (vector 'png
:file movefile
))))
11826 (list 'image
:type
'png
:file movefile
:ascent
'center
)))
11827 (push ov org-latex-fragment-image-overlays
)
11829 (delete-region beg end
)
11830 (insert link
))))))))
11832 ;; This function borrows from Ganesh Swami's latex2png.el
11833 (defun org-create-formula-image (string tofile options buffer
)
11834 (let* ((tmpdir (if (featurep 'xemacs
)
11836 temporary-file-directory
))
11837 (texfilebase (make-temp-name
11838 (expand-file-name "orgtex" tmpdir
)))
11839 (texfile (concat texfilebase
".tex"))
11840 (dvifile (concat texfilebase
".dvi"))
11841 (pngfile (concat texfilebase
".png"))
11842 (fnh (if (featurep 'xemacs
)
11843 (font-height (get-face-font 'default
))
11844 (face-attribute 'default
:height nil
)))
11845 (scale (or (plist-get options
(if buffer
:scale
:html-scale
)) 1.0))
11846 (dpi (number-to-string (* scale
(floor (* 0.9 (if buffer fnh
140.
))))))
11847 (fg (or (plist-get options
(if buffer
:foreground
:html-foreground
))
11849 (bg (or (plist-get options
(if buffer
:background
:html-background
))
11851 (if (eq fg
'default
) (setq fg
(org-dvipng-color :foreground
)))
11852 (if (eq bg
'default
) (setq bg
(org-dvipng-color :background
)))
11853 (with-temp-file texfile
11854 (insert org-format-latex-header
11855 "\n\\begin{document}\n" string
"\n\\end{document}\n"))
11856 (let ((dir default-directory
))
11857 (condition-case nil
11860 (call-process "latex" nil nil nil texfile
))
11863 (if (not (file-exists-p dvifile
))
11864 (progn (message "Failed to create dvi file from %s" texfile
) nil
)
11865 (call-process "dvipng" nil nil nil
11866 "-E" "-fg" fg
"-bg" bg
11868 ;;"-x" scale "-y" scale
11872 (if (not (file-exists-p pngfile
))
11873 (progn (message "Failed to create png file from %s" texfile
) nil
)
11874 ;; Use the requested file name and clean up
11875 (copy-file pngfile tofile
'replace
)
11876 (loop for e in
'(".dvi" ".tex" ".aux" ".log" ".png") do
11877 (delete-file (concat texfilebase e
)))
11880 (defun org-dvipng-color (attr)
11881 "Return an rgb color specification for dvipng."
11882 (apply 'format
"rgb %s %s %s"
11883 (mapcar 'org-normalize-color
11884 (color-values (face-attribute 'default attr nil
)))))
11886 (defun org-normalize-color (value)
11887 "Return string to be used as color value for an RGB component."
11888 (format "%g" (/ value
65535.0)))
11893 ;; Make `C-c C-x' a prefix key
11894 (org-defkey org-mode-map
"\C-c\C-x" (make-sparse-keymap))
11896 ;; TAB key with modifiers
11897 (org-defkey org-mode-map
"\C-i" 'org-cycle
)
11898 (org-defkey org-mode-map
[(tab)] 'org-cycle
)
11899 (org-defkey org-mode-map
[(control tab
)] 'org-force-cycle-archived
)
11900 (org-defkey org-mode-map
[(meta tab
)] 'org-complete
)
11901 (org-defkey org-mode-map
"\M-\t" 'org-complete
)
11902 (org-defkey org-mode-map
"\M-\C-i" 'org-complete
)
11903 ;; The following line is necessary under Suse GNU/Linux
11904 (unless (featurep 'xemacs
)
11905 (org-defkey org-mode-map
[S-iso-lefttab
] 'org-shifttab
))
11906 (org-defkey org-mode-map
[(shift tab
)] 'org-shifttab
)
11907 (define-key org-mode-map
[backtab] 'org-shifttab)
11909 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
11910 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
11911 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
11913 ;; Cursor keys with modifiers
11914 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
11915 (org-defkey org-mode-map [(meta right)] 'org-metaright)
11916 (org-defkey org-mode-map [(meta up)] 'org-metaup)
11917 (org-defkey org-mode-map [(meta down)] 'org-metadown)
11919 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
11920 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
11921 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
11922 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
11924 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
11925 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
11926 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
11927 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
11929 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
11930 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
11932 ;;; Extra keys for tty access.
11933 ;; We only set them when really needed because otherwise the
11934 ;; menus don't show the simple keys
11936 (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
11937 (not window-system))
11938 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
11939 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
11940 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
11941 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
11942 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
11943 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
11944 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
11945 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
11946 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
11947 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
11948 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
11949 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
11950 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
11951 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
11952 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
11953 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
11954 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
11955 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
11956 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
11957 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
11958 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
11959 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
11961 ;; All the other keys
11963 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
11964 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
11965 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
11966 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
11967 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
11968 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
11969 (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
11970 (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
11971 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
11972 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
11973 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
11974 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
11975 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
11976 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
11977 (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
11978 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
11979 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
11980 (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
11981 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
11982 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
11983 (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
11984 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
11985 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
11986 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
11987 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
11988 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
11989 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
11990 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
11991 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
11992 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
11993 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
11994 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
11995 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
11996 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
11997 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
11998 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
11999 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
12000 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
12001 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
12002 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
12003 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
12004 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
12005 (org-defkey org-mode-map "\C-c^" 'org-sort)
12006 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
12007 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
12008 (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
12009 (org-defkey org-mode-map "\C-m" 'org-return)
12010 (org-defkey org-mode-map "\C-j" 'org-return-indent)
12011 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
12012 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
12013 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
12014 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
12015 (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
12016 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
12017 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
12018 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
12019 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
12020 (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
12021 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
12022 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
12023 (org-defkey org-mode-map "\C-c\C-e" 'org-export)
12024 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
12025 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
12027 (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
12028 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
12029 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
12030 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
12032 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
12033 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
12034 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
12035 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
12036 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
12037 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
12038 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
12039 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
12040 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
12041 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
12042 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
12043 (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
12045 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
12047 (when (featurep 'xemacs)
12048 (org-defkey org-mode-map 'button3 'popup-mode-menu))
12050 (defvar org-table-auto-blank-field) ; defined in org-table.el
12051 (defun org-self-insert-command (N)
12052 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
12053 If the cursor is in a table looking at whitespace, the whitespace is
12054 overwritten, and the table is not marked as requiring realignment."
12056 (if (and (org-table-p)
12058 ;; check if we blank the field, and if that triggers align
12059 (and (featurep 'org-table) org-table-auto-blank-field
12060 (member last-command
12061 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
12062 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
12063 ;; got extra space, this field does not determine column width
12064 (let (org-table-may-need-update) (org-table-blank-field))
12065 ;; no extra space, this field may determine column width
12066 (org-table-blank-field)))
12069 (looking-at "[^|\n]* |"))
12070 (let (org-table-may-need-update)
12071 (goto-char (1- (match-end 0)))
12072 (delete-backward-char 1)
12073 (goto-char (match-beginning 0))
12074 (self-insert-command N))
12075 (setq org-table-may-need-update t)
12076 (self-insert-command N)
12077 (org-fix-tags-on-the-fly)))
12079 (defun org-fix-tags-on-the-fly ()
12080 (when (and (equal (char-after (point-at-bol)) ?*)
12081 (org-on-heading-p))
12082 (org-align-tags-here org-tags-column)))
12084 (defun org-delete-backward-char (N)
12085 "Like `delete-backward-char', insert whitespace at field end in tables.
12086 When deleting backwards, in tables this function will insert whitespace in
12087 front of the next \"|\" separator, to keep the table aligned. The table will
12088 still be marked for re-alignment if the field did fill the entire column,
12089 because, in this case the deletion might narrow the column."
12091 (if (and (org-table-p)
12093 (string-match "|" (buffer-substring (point-at-bol) (point)))
12094 (looking-at ".*?|"))
12095 (let ((pos (point))
12096 (noalign (looking-at "[^|\n\r]* |"))
12097 (c org-table-may-need-update))
12098 (backward-delete-char N)
12099 (skip-chars-forward "^|")
12101 (goto-char (1- pos))
12102 ;; noalign: if there were two spaces at the end, this field
12103 ;; does not determine the width of the column.
12104 (if noalign (setq org-table-may-need-update c)))
12105 (backward-delete-char N)
12106 (org-fix-tags-on-the-fly)))
12108 (defun org-delete-char (N)
12109 "Like `delete-char', but insert whitespace at field end in tables.
12110 When deleting characters, in tables this function will insert whitespace in
12111 front of the next \"|\" separator, to keep the table aligned. The table will
12112 still be marked for re-alignment if the field did fill the entire column,
12113 because, in this case the deletion might narrow the column."
12115 (if (and (org-table-p)
12117 (not (= (char-after) ?|))
12119 (if (looking-at ".*?|")
12120 (let ((pos (point))
12121 (noalign (looking-at "[^|\n\r]* |"))
12122 (c org-table-may-need-update))
12123 (replace-match (concat
12124 (substring (match-string 0) 1 -1)
12127 ;; noalign: if there were two spaces at the end, this field
12128 ;; does not determine the width of the column.
12129 (if noalign (setq org-table-may-need-update c)))
12132 (org-fix-tags-on-the-fly)))
12134 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
12135 (put 'org-self-insert-command 'delete-selection t)
12136 (put 'orgtbl-self-insert-command 'delete-selection t)
12137 (put 'org-delete-char 'delete-selection 'supersede)
12138 (put 'org-delete-backward-char 'delete-selection 'supersede)
12140 ;; Make `flyspell-mode' delay after some commands
12141 (put 'org-self-insert-command 'flyspell-delayed t)
12142 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
12143 (put 'org-delete-char 'flyspell-delayed t)
12144 (put 'org-delete-backward-char 'flyspell-delayed t)
12146 ;; Make pabbrev-mode expand after org-mode commands
12147 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
12148 (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
12150 ;; How to do this: Measure non-white length of current string
12151 ;; If equal to column width, we should realign.
12153 (defun org-remap (map &rest commands)
12154 "In MAP, remap the functions given in COMMANDS.
12155 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
12158 (setq old (pop commands) new (pop commands))
12159 (if (fboundp 'command-remapping)
12160 (org-defkey map (vector 'remap old) new)
12161 (substitute-key-definition old new map global-map)))))
12163 (when (eq org-enable-table-editor 'optimized)
12164 ;; If the user wants maximum table support, we need to hijack
12165 ;; some standard editing functions
12166 (org-remap org-mode-map
12167 'self-insert-command 'org-self-insert-command
12168 'delete-char 'org-delete-char
12169 'delete-backward-char 'org-delete-backward-char)
12170 (org-defkey org-mode-map "|" 'org-force-self-insert))
12172 (defun org-shiftcursor-error ()
12173 "Throw an error because Shift-Cursor command was applied in wrong context."
12174 (error "This command is active in special context like tables, headlines or timestamps"))
12176 (defun org-shifttab (&optional arg)
12177 "Global visibility cycling or move to previous table field.
12178 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
12180 See the individual commands for more information."
12183 ((org-at-table-p) (call-interactively 'org-table-previous-field))
12185 (message "Content view to level: %d" arg)
12186 (org-content (prefix-numeric-value arg))
12187 (setq org-cycle-global-status 'overview))
12188 (t (call-interactively 'org-global-cycle))))
12190 (defun org-shiftmetaleft ()
12191 "Promote subtree or delete table column.
12192 Calls `org-promote-subtree', `org-outdent-item',
12193 or `org-table-delete-column', depending on context.
12194 See the individual commands for more information."
12197 ((org-at-table-p) (call-interactively 'org-table-delete-column))
12198 ((org-on-heading-p) (call-interactively 'org-promote-subtree))
12199 ((org-at-item-p) (call-interactively 'org-outdent-item))
12200 (t (org-shiftcursor-error))))
12202 (defun org-shiftmetaright ()
12203 "Demote subtree or insert table column.
12204 Calls `org-demote-subtree', `org-indent-item',
12205 or `org-table-insert-column', depending on context.
12206 See the individual commands for more information."
12209 ((org-at-table-p) (call-interactively 'org-table-insert-column))
12210 ((org-on-heading-p) (call-interactively 'org-demote-subtree))
12211 ((org-at-item-p) (call-interactively 'org-indent-item))
12212 (t (org-shiftcursor-error))))
12214 (defun org-shiftmetaup (&optional arg)
12215 "Move subtree up or kill table row.
12216 Calls `org-move-subtree-up' or `org-table-kill-row' or
12217 `org-move-item-up' depending on context. See the individual commands
12218 for more information."
12221 ((org-at-table-p) (call-interactively 'org-table-kill-row))
12222 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
12223 ((org-at-item-p) (call-interactively 'org-move-item-up))
12224 (t (org-shiftcursor-error))))
12225 (defun org-shiftmetadown (&optional arg)
12226 "Move subtree down or insert table row.
12227 Calls `org-move-subtree-down' or `org-table-insert-row' or
12228 `org-move-item-down', depending on context. See the individual
12229 commands for more information."
12232 ((org-at-table-p) (call-interactively 'org-table-insert-row))
12233 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
12234 ((org-at-item-p) (call-interactively 'org-move-item-down))
12235 (t (org-shiftcursor-error))))
12237 (defun org-metaleft (&optional arg)
12238 "Promote heading or move table column to left.
12239 Calls `org-do-promote' or `org-table-move-column', depending on context.
12240 With no specific context, calls the Emacs default `backward-word'.
12241 See the individual commands for more information."
12244 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
12245 ((or (org-on-heading-p) (org-region-active-p))
12246 (call-interactively 'org-do-promote))
12247 ((org-at-item-p) (call-interactively 'org-outdent-item))
12248 (t (call-interactively 'backward-word))))
12250 (defun org-metaright (&optional arg)
12251 "Demote subtree or move table column to right.
12252 Calls `org-do-demote' or `org-table-move-column', depending on context.
12253 With no specific context, calls the Emacs default `forward-word'.
12254 See the individual commands for more information."
12257 ((org-at-table-p) (call-interactively 'org-table-move-column))
12258 ((or (org-on-heading-p) (org-region-active-p))
12259 (call-interactively 'org-do-demote))
12260 ((org-at-item-p) (call-interactively 'org-indent-item))
12261 (t (call-interactively 'forward-word))))
12263 (defun org-metaup (&optional arg)
12264 "Move subtree up or move table row up.
12265 Calls `org-move-subtree-up' or `org-table-move-row' or
12266 `org-move-item-up', depending on context. See the individual commands
12267 for more information."
12270 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
12271 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
12272 ((org-at-item-p) (call-interactively 'org-move-item-up))
12273 (t (transpose-lines 1) (beginning-of-line -1))))
12275 (defun org-metadown (&optional arg)
12276 "Move subtree down or move table row down.
12277 Calls `org-move-subtree-down' or `org-table-move-row' or
12278 `org-move-item-down', depending on context. See the individual
12279 commands for more information."
12282 ((org-at-table-p) (call-interactively 'org-table-move-row))
12283 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
12284 ((org-at-item-p) (call-interactively 'org-move-item-down))
12285 (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
12287 (defun org-shiftup (&optional arg)
12288 "Increase item in timestamp or increase priority of current headline.
12289 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
12290 depending on context. See the individual commands for more information."
12293 ((org-at-timestamp-p t)
12294 (call-interactively (if org-edit-timestamp-down-means-later
12295 'org-timestamp-down 'org-timestamp-up)))
12296 ((org-on-heading-p) (call-interactively 'org-priority-up))
12297 ((org-at-item-p) (call-interactively 'org-previous-item))
12298 ((org-clocktable-try-shift 'up arg))
12299 (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
12301 (defun org-shiftdown (&optional arg)
12302 "Decrease item in timestamp or decrease priority of current headline.
12303 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
12304 depending on context. See the individual commands for more information."
12307 ((org-at-timestamp-p t)
12308 (call-interactively (if org-edit-timestamp-down-means-later
12309 'org-timestamp-up 'org-timestamp-down)))
12310 ((org-on-heading-p) (call-interactively 'org-priority-down))
12311 ((org-clocktable-try-shift 'down arg))
12312 (t (call-interactively 'org-next-item))))
12314 (defun org-shiftright (&optional arg)
12315 "Next TODO keyword or timestamp one day later, depending on context."
12318 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
12319 ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
12320 ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
12321 ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
12322 ((org-clocktable-try-shift 'right arg))
12323 (t (org-shiftcursor-error))))
12325 (defun org-shiftleft (&optional arg)
12326 "Previous TODO keyword or timestamp one day earlier, depending on context."
12329 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
12330 ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
12331 ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
12332 ((org-at-property-p)
12333 (call-interactively 'org-property-previous-allowed-value))
12334 ((org-clocktable-try-shift 'left arg))
12335 (t (org-shiftcursor-error))))
12337 (defun org-shiftcontrolright ()
12338 "Switch to next TODO set."
12341 ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
12342 (t (org-shiftcursor-error))))
12344 (defun org-shiftcontrolleft ()
12345 "Switch to previous TODO set."
12348 ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
12349 (t (org-shiftcursor-error))))
12351 (defun org-ctrl-c-ret ()
12352 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
12355 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
12356 (t (call-interactively 'org-insert-heading))))
12358 (defun org-copy-special ()
12359 "Copy region in table or copy current subtree.
12360 Calls `org-table-copy' or `org-copy-subtree', depending on context.
12361 See the individual commands for more information."
12363 (call-interactively
12364 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
12366 (defun org-cut-special ()
12367 "Cut region in table or cut current subtree.
12368 Calls `org-table-copy' or `org-cut-subtree', depending on context.
12369 See the individual commands for more information."
12371 (call-interactively
12372 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
12374 (defun org-paste-special (arg)
12375 "Paste rectangular region into table, or past subtree relative to level.
12376 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
12377 See the individual commands for more information."
12379 (if (org-at-table-p)
12380 (org-table-paste-rectangle)
12381 (org-paste-subtree arg)))
12383 (defun org-ctrl-c-ctrl-c (&optional arg)
12384 "Set tags in headline, or update according to changed information at point.
12386 This command does many different things, depending on context:
12388 - If the cursor is in a headline, prompt for tags and insert them
12389 into the current line, aligned to `org-tags-column'. When called
12390 with prefix arg, realign all tags in the current buffer.
12392 - If the cursor is in one of the special #+KEYWORD lines, this
12393 triggers scanning the buffer for these lines and updating the
12396 - If the cursor is inside a table, realign the table. This command
12397 works even if the automatic table editor has been turned off.
12399 - If the cursor is on a #+TBLFM line, re-apply the formulas to
12402 - If the cursor is a the beginning of a dynamic block, update it.
12404 - If the cursor is inside a table created by the table.el package,
12405 activate that table.
12407 - If the current buffer is a remember buffer, close note and file it.
12408 with a prefix argument, file it without further interaction to the default
12411 - If the cursor is on a <<<target>>>, update radio targets and corresponding
12412 links in this buffer.
12414 - If the cursor is on a numbered item in a plain list, renumber the
12417 - If the cursor is on a checkbox, toggle it."
12419 (let ((org-enable-table-editor t))
12421 ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
12422 org-occur-highlights
12423 org-latex-fragment-image-overlays)
12424 (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
12425 (org-remove-occur-highlights)
12426 (org-remove-latex-fragment-image-overlays)
12427 (message "Temporary highlights/overlays removed from current buffer"))
12428 ((and (local-variable-p 'org-finish-function (current-buffer))
12429 (fboundp org-finish-function))
12430 (funcall org-finish-function))
12431 ((org-at-property-p)
12432 (call-interactively 'org-property-action))
12433 ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
12434 ((org-on-heading-p) (call-interactively 'org-set-tags))
12435 ((org-at-table.el-p)
12437 (beginning-of-line 1)
12438 (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
12439 (call-interactively 'table-recognize-table))
12441 (org-table-maybe-eval-formula)
12443 (call-interactively 'org-table-recalculate)
12444 (org-table-maybe-recalculate-line))
12445 (call-interactively 'org-table-align))
12446 ((org-at-item-checkbox-p)
12447 (call-interactively 'org-toggle-checkbox))
12449 (call-interactively 'org-maybe-renumber-ordered-list))
12450 ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
12452 (beginning-of-line 1)
12453 (org-update-dblock))
12454 ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
12456 ((equal (match-string 1) "TBLFM")
12457 ;; Recalculate the table before this line
12459 (beginning-of-line 1)
12460 (skip-chars-backward " \r\n\t")
12461 (if (org-at-table-p)
12462 (org-call-with-arg 'org-table-recalculate t))))
12464 ; (org-set-regexps-and-options)
12465 ; (org-restart-font-lock)
12466 (let ((org-inhibit-startup t)) (org-mode-restart))
12467 (message "Local setup has been refreshed"))))
12468 (t (error "C-c C-c can do nothing useful at this location.")))))
12470 (defun org-mode-restart ()
12471 "Restart Org-mode, to scan again for special lines.
12472 Also updates the keyword regular expressions."
12475 (message "Org-mode restarted"))
12477 (defun org-kill-note-or-show-branches ()
12478 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
12480 (if (not org-finish-function)
12481 (call-interactively 'show-branches)
12482 (let ((org-note-abort t))
12483 (funcall org-finish-function))))
12485 (defun org-return (&optional indent)
12486 "Goto next table row or insert a newline.
12487 Calls `org-table-next-row' or `newline', depending on context.
12488 See the individual commands for more information."
12491 ((bobp) (if indent (newline-and-indent) (newline)))
12492 ((and (org-at-heading-p)
12494 (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
12499 (org-table-justify-field-maybe)
12500 (call-interactively 'org-table-next-row))
12501 (t (if indent (newline-and-indent) (newline)))))
12503 (defun org-return-indent ()
12504 "Goto next table row or insert a newline and indent.
12505 Calls `org-table-next-row' or `newline-and-indent', depending on
12506 context. See the individual commands for more information."
12510 (defun org-ctrl-c-star ()
12511 "Compute table, or change heading status of lines.
12512 Calls `org-table-recalculate' or `org-toggle-region-headlines',
12513 depending on context. This will also turn a plain list item or a normal
12514 line into a subheading."
12518 (call-interactively 'org-table-recalculate))
12519 ((org-region-active-p)
12520 ;; Convert all lines in region to list items
12521 (call-interactively 'org-toggle-region-headings))
12522 ((org-on-heading-p)
12523 (org-toggle-region-headings (point-at-bol)
12524 (min (1+ (point-at-eol)) (point-max))))
12526 ;; Convert to heading
12527 (let ((level (save-match-data
12529 (condition-case nil
12531 (org-back-to-heading t)
12532 (funcall outline-level))
12535 (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
12536 (t (org-toggle-region-headings (point-at-bol)
12537 (min (1+ (point-at-eol)) (point-max))))))
12539 (defun org-ctrl-c-minus ()
12540 "Insert separator line in table or modify bullet status of line.
12541 Also turns a plain line or a region of lines into list items.
12542 Calls `org-table-insert-hline', `org-toggle-region-items', or
12543 `org-cycle-list-bullet', depending on context."
12547 (call-interactively 'org-table-insert-hline))
12548 ((org-on-heading-p)
12551 (beginning-of-line 1)
12552 (if (looking-at "\\*+ ")
12553 (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
12554 ((org-region-active-p)
12555 ;; Convert all lines in region to list items
12556 (call-interactively 'org-toggle-region-items))
12558 (call-interactively 'org-cycle-list-bullet))
12559 (t (org-toggle-region-items (point-at-bol)
12560 (min (1+ (point-at-eol)) (point-max))))))
12562 (defun org-toggle-region-items (beg end)
12563 "Convert all lines in region to list items.
12564 If the first line is already an item, convert all list items in the region
12570 (setq l2 (org-current-line))
12572 (beginning-of-line 1)
12573 (setq l (1- (org-current-line)))
12574 (if (org-at-item-p)
12575 ;; We already have items, de-itemize
12576 (while (< (setq l (1+ l)) l2)
12577 (when (org-at-item-p)
12578 (goto-char (match-beginning 2))
12579 (delete-region (match-beginning 2) (match-end 2))
12580 (and (looking-at "[ \t]+") (replace-match "")))
12581 (beginning-of-line 2))
12582 (while (< (setq l (1+ l)) l2)
12583 (unless (org-at-item-p)
12584 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
12585 (replace-match "\\1- \\2")))
12586 (beginning-of-line 2))))))
12588 (defun org-toggle-region-headings (beg end)
12589 "Convert all lines in region to list items.
12590 If the first line is already an item, convert all list items in the region
12596 (setq l2 (org-current-line))
12598 (beginning-of-line 1)
12599 (setq l (1- (org-current-line)))
12600 (if (org-on-heading-p)
12601 ;; We already have headlines, de-star them
12602 (while (< (setq l (1+ l)) l2)
12603 (when (org-on-heading-p t)
12604 (and (looking-at outline-regexp) (replace-match "")))
12605 (beginning-of-line 2))
12606 (let* ((stars (save-excursion
12607 (re-search-backward org-complex-heading-regexp nil t)
12608 (or (match-string 1) "*")))
12609 (add-stars (if org-odd-levels-only "**" "*"))
12610 (rpl (concat stars add-stars " \\2")))
12611 (while (< (setq l (1+ l)) l2)
12612 (unless (org-on-heading-p)
12613 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
12614 (replace-match rpl)))
12615 (beginning-of-line 2)))))))
12617 (defun org-meta-return (&optional arg)
12618 "Insert a new heading or wrap a region in a table.
12619 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
12620 See the individual commands for more information."
12624 (call-interactively 'org-table-wrap-region))
12625 (t (call-interactively 'org-insert-heading))))
12629 ;; Define the Org-mode menus
12630 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
12632 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
12633 ["Next Field" org-cycle (org-at-table-p)]
12634 ["Previous Field" org-shifttab (org-at-table-p)]
12635 ["Next Row" org-return (org-at-table-p)]
12637 ["Blank Field" org-table-blank-field (org-at-table-p)]
12638 ["Edit Field" org-table-edit-field (org-at-table-p)]
12639 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
12642 ["Move Column Left" org-metaleft (org-at-table-p)]
12643 ["Move Column Right" org-metaright (org-at-table-p)]
12644 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
12645 ["Insert Column" org-shiftmetaright (org-at-table-p)])
12647 ["Move Row Up" org-metaup (org-at-table-p)]
12648 ["Move Row Down" org-metadown (org-at-table-p)]
12649 ["Delete Row" org-shiftmetaup (org-at-table-p)]
12650 ["Insert Row" org-shiftmetadown (org-at-table-p)]
12651 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
12653 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
12655 ["Copy Rectangle" org-copy-special (org-at-table-p)]
12656 ["Cut Rectangle" org-cut-special (org-at-table-p)]
12657 ["Paste Rectangle" org-paste-special (org-at-table-p)]
12658 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
12661 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
12662 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
12663 ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
12665 ["Recalculate line" org-table-recalculate (org-at-table-p)]
12666 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
12667 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
12669 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
12671 ["Sum Column/Rectangle" org-table-sum
12672 (or (org-at-table-p) (org-region-active-p))]
12673 ["Which Column?" org-table-current-column (org-at-table-p)])
12675 org-table-toggle-formula-debugger
12676 :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
12677 ["Show Col/Row Numbers"
12678 org-table-toggle-coordinate-overlays
12680 :selected (org-bound-and-true-p org-table-overlay-coordinates)]
12682 ["Create" org-table-create (and (not (org-at-table-p))
12683 org-enable-table-editor)]
12684 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
12685 ["Import from File" org-table-import (not (org-at-table-p))]
12686 ["Export to File" org-table-export (org-at-table-p)]
12688 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
12690 (easy-menu-define org-org-menu org-mode-map "Org menu"
12693 ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
12694 ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
12695 ["Sparse Tree..." org-sparse-tree t]
12696 ["Reveal Context" org-reveal t]
12697 ["Show All" show-all t]
12699 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
12701 ["New Heading" org-insert-heading t]
12702 ("Navigate Headings"
12703 ["Up" outline-up-heading t]
12704 ["Next" outline-next-visible-heading t]
12705 ["Previous" outline-previous-visible-heading t]
12706 ["Next Same Level" outline-forward-same-level t]
12707 ["Previous Same Level" outline-backward-same-level t]
12709 ["Jump" org-goto t])
12711 ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
12712 ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
12714 ["Copy Subtree" org-copy-special (not (org-at-table-p))]
12715 ["Cut Subtree" org-cut-special (not (org-at-table-p))]
12716 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
12718 ["Promote Heading" org-metaleft (not (org-at-table-p))]
12719 ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
12720 ["Demote Heading" org-metaright (not (org-at-table-p))]
12721 ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
12723 ["Sort Region/Children" org-sort (not (org-at-table-p))]
12725 ["Convert to odd levels" org-convert-to-odd-levels t]
12726 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
12728 ["Emphasis..." org-emphasize t])
12730 ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
12731 ; ["Check and Tag Children" (org-toggle-archive-tag (4))
12732 ; :active t :keys "C-u C-c C-x C-a"]
12733 ["Sparse trees open ARCHIVE trees"
12734 (setq org-sparse-tree-open-archived-trees
12735 (not org-sparse-tree-open-archived-trees))
12736 :style toggle :selected org-sparse-tree-open-archived-trees]
12737 ["Cycling opens ARCHIVE trees"
12738 (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
12739 :style toggle :selected org-cycle-open-archived-trees]
12740 ["Agenda includes ARCHIVE trees"
12741 (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
12742 :style toggle :selected (not org-agenda-skip-archived-trees)]
12744 ["Move Subtree to Archive" org-advertized-archive-subtree t]
12745 ; ["Check and Move Children" (org-archive-subtree '(4))
12746 ; :active t :keys "C-u C-c C-x C-s"]
12750 ["TODO/DONE/-" org-todo t]
12752 ["Next keyword" org-shiftright (org-on-heading-p)]
12753 ["Previous keyword" org-shiftleft (org-on-heading-p)]
12754 ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
12755 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
12756 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
12757 ["Show TODO Tree" org-show-todo-tree t]
12758 ["Global TODO list" org-todo-list t]
12760 ["Set Priority" org-priority t]
12761 ["Priority Up" org-shiftup t]
12762 ["Priority Down" org-shiftdown t])
12763 ("TAGS and Properties"
12764 ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
12765 ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
12767 ["Set property" 'org-set-property t]
12768 ["Column view of properties" org-columns t]
12769 ["Insert Column View DBlock" org-insert-columns-dblock t])
12770 ("Dates and Scheduling"
12771 ["Timestamp" org-time-stamp t]
12772 ["Timestamp (inactive)" org-time-stamp-inactive t]
12774 ["1 Day Later" org-shiftright t]
12775 ["1 Day Earlier" org-shiftleft t]
12776 ["1 ... Later" org-shiftup t]
12777 ["1 ... Earlier" org-shiftdown t])
12778 ["Compute Time Range" org-evaluate-time-range t]
12779 ["Schedule Item" org-schedule t]
12780 ["Deadline" org-deadline t]
12782 ["Custom time format" org-toggle-time-stamp-overlays
12783 :style radio :selected org-display-custom-times]
12785 ["Goto Calendar" org-goto-calendar t]
12786 ["Date from Calendar" org-date-from-calendar t])
12788 ["Clock in" org-clock-in t]
12789 ["Clock out" org-clock-out t]
12790 ["Clock cancel" org-clock-cancel t]
12791 ["Goto running clock" org-clock-goto t]
12792 ["Display times" org-clock-display t]
12793 ["Create clock table" org-clock-report t]
12795 ["Record DONE time"
12796 (progn (setq org-log-done (not org-log-done))
12797 (message "Switching to %s will %s record a timestamp"
12798 (car org-done-keywords)
12799 (if org-log-done "automatically" "not")))
12800 :style toggle :selected org-log-done])
12802 ["Agenda Command..." org-agenda t]
12803 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
12804 ("File List for Agenda")
12805 ("Special views current file"
12806 ["TODO Tree" org-show-todo-tree t]
12807 ["Check Deadlines" org-check-deadlines t]
12808 ["Timeline" org-timeline t]
12809 ["Tags Tree" org-tags-sparse-tree t])
12812 ["Store Link (Global)" org-store-link t]
12813 ["Insert Link" org-insert-link t]
12814 ["Follow Link" org-open-at-point t]
12816 ["Next link" org-next-link t]
12817 ["Previous link" org-previous-link t]
12819 ["Descriptive Links"
12820 (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
12822 :selected (member '(org-link) buffer-invisibility-spec)]
12825 (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
12827 :selected (not (member '(org-link) buffer-invisibility-spec))])
12829 ["Export/Publish..." org-export t]
12831 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
12832 :selected org-cdlatex-mode]
12833 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
12834 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
12835 ["Modify math symbol" org-cdlatex-math-modify
12836 (org-inside-LaTeX-fragment-p)]
12837 ["Export LaTeX fragments as images"
12838 (if (featurep 'org-exp)
12839 (setq org-export-with-LaTeX-fragments
12840 (not org-export-with-LaTeX-fragments))
12841 (require 'org-exp))
12842 :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
12843 org-export-with-LaTeX-fragments)])
12846 ["Show Version" org-version t]
12847 ["Info Documentation" org-info t])
12849 ["Browse Org Group" org-customize t]
12851 ["Expand This Menu" org-create-customize-menu
12852 (fboundp 'customize-menu-create)])
12854 ["Refresh setup" org-mode-restart t]
12857 (defun org-info (&optional node)
12858 "Read documentation for Org-mode in the info system.
12859 With optional NODE, go directly to that node."
12861 (info (format "(org)%s" (or node ""))))
12863 (defun org-install-agenda-files-menu ()
12864 (let ((bl (buffer-list)))
12867 (set-buffer (pop bl))
12868 (if (org-mode-p) (setq bl nil)))
12871 '("Org") "File List for Agenda"
12874 ["Edit File List" (org-edit-agenda-file-list) t]
12875 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
12876 ["Remove Current File from List" org-remove-file t]
12877 ["Cycle through agenda files" org-cycle-agenda-files t]
12878 ["Occur in all agenda files" org-occur-in-agenda-files t]
12880 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
12884 (defun org-require-autoloaded-modules ()
12887 '(org-agenda org-archive org-clock org-colview
12888 org-exp org-export-latex org-publish
12889 org-remember org-table)))
12891 (defun org-customize ()
12892 "Call the customize function with org as argument."
12894 (org-load-modules-maybe)
12895 (org-require-autoloaded-modules)
12896 (customize-browse 'org))
12898 (defun org-create-customize-menu ()
12899 "Create a full customization menu for Org-mode, insert it into the menu."
12901 (org-load-modules-maybe)
12902 (org-require-autoloaded-modules)
12903 (if (fboundp 'customize-menu-create)
12906 '("Org") "Customize"
12907 `(["Browse Org group" org-customize t]
12909 ,(customize-menu-create 'org)
12910 ["Set" Custom-set t]
12911 ["Save" Custom-save t]
12912 ["Reset to Current" Custom-reset-current t]
12913 ["Reset to Saved" Custom-reset-saved t]
12914 ["Reset to Standard Settings" Custom-reset-standard t]))
12915 (message "\"Org\"-menu now contains full customization menu"))
12916 (error "Cannot expand menu (outdated version of cus-edit.el)")))
12918 ;;;; Miscellaneous stuff
12920 ;;; Generally useful functions
12922 (defun org-display-warning (message) ;; Copied from Emacs-Muse
12923 "Display the given MESSAGE as a warning."
12924 (if (fboundp 'display-warning)
12925 (display-warning 'org message
12926 (if (featurep 'xemacs)
12929 (let ((buf (get-buffer-create "*Org warnings*")))
12930 (with-current-buffer buf
12931 (goto-char (point-max))
12932 (insert "Warning (Org): " message)
12935 (display-buffer buf)
12938 (defun org-quote-csv-field (s)
12939 "Quote field for inclusion in CSV material."
12940 (if (string-match "[\",]" s)
12941 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
12944 (defun org-plist-delete (plist property)
12945 "Delete PROPERTY from PLIST.
12946 This is in contrast to merely setting it to 0."
12949 (if (not (eq property (car plist)))
12950 (setq p (plist-put p (car plist) (nth 1 plist))))
12951 (setq plist (cddr plist)))
12954 (defun org-force-self-insert (N)
12955 "Needed to enforce self-insert under remapping."
12957 (self-insert-command N))
12959 (defun org-string-width (s)
12960 "Compute width of string, ignoring invisible characters.
12961 This ignores character with invisibility property `org-link', and also
12962 characters with property `org-cwidth', because these will become invisible
12963 upon the next fontification round."
12965 (when (or (eq t buffer-invisibility-spec)
12966 (assq 'org-link buffer-invisibility-spec))
12967 (while (setq b (text-property-any 0 (length s)
12968 'invisible 'org-link s))
12969 (setq s (concat (substring s 0 b)
12970 (substring s (or (next-single-property-change
12971 b 'invisible s) (length s)))))))
12972 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
12973 (setq s (concat (substring s 0 b)
12974 (substring s (or (next-single-property-change
12975 b 'org-cwidth s) (length s))))))
12976 (setq l (string-width s) b -1)
12977 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
12978 (setq l (- l (get-text-property b 'org-dwidth-n s))))
12981 (defun org-base-buffer (buffer)
12982 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
12985 (or (buffer-base-buffer buffer)
12988 (defun org-trim (s)
12989 "Remove whitespace at beginning and end of string."
12990 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
12991 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
12994 (defun org-wrap (string &optional width lines)
12995 "Wrap string to either a number of lines, or a width in characters.
12996 If WIDTH is non-nil, the string is wrapped to that width, however many lines
12997 that costs. If there is a word longer than WIDTH, the text is actually
12998 wrapped to the length of that word.
12999 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
13000 many lines, whatever width that takes.
13001 The return value is a list of lines, without newlines at the end."
13002 (let* ((words (org-split-string string "[ \t\n]+"))
13003 (maxword (apply 'max (mapcar 'org-string-width words)))
13006 (org-do-wrap words (max maxword width)))
13009 (setq ll (org-do-wrap words maxword))
13010 (if (<= (length ll) lines)
13013 (while (> (length ll) lines)
13015 (setq ll (org-do-wrap words w)))
13017 (t (error "Cannot wrap this")))))
13019 (defun org-do-wrap (words width)
13020 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
13023 (setq line (pop words))
13024 (while (and words (< (+ (length line) (length (car words))) width))
13025 (setq line (concat line " " (pop words))))
13026 (setq lines (push line lines)))
13029 (defun org-split-string (string &optional separators)
13030 "Splits STRING into substrings at SEPARATORS.
13031 No empty strings are returned if there are matches at the beginning
13032 and end of string."
13033 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
13037 (while (and (string-match rexp string
13039 (= start (match-beginning 0))
13040 (< start (length string)))
13042 (< (match-beginning 0) (length string)))
13044 (or (eq (match-beginning 0) 0)
13045 (and (eq (match-beginning 0) (match-end 0))
13046 (eq (match-beginning 0) start))
13048 (cons (substring string start (match-beginning 0))
13050 (setq start (match-end 0)))
13051 (or (eq start (length string))
13053 (cons (substring string start)
13057 (defun org-context ()
13058 "Return a list of contexts of the current cursor position.
13059 If several contexts apply, all are returned.
13060 Each context entry is a list with a symbol naming the context, and
13061 two positions indicating start and end of the context. Possible
13064 :headline anywhere in a headline
13065 :headline-stars on the leading stars in a headline
13066 :todo-keyword on a TODO keyword (including DONE) in a headline
13067 :tags on the TAGS in a headline
13068 :priority on the priority cookie in a headline
13069 :item on the first line of a plain list item
13070 :item-bullet on the bullet/number of a plain list item
13071 :checkbox on the checkbox in a plain list item
13072 :table in an org-mode table
13073 :table-special on a special filed in a table
13074 :table-table in a table.el table
13075 :link on a hyperlink
13076 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
13077 :target on a <<target>>
13078 :radio-target on a <<<radio-target>>>
13079 :latex-fragment on a LaTeX fragment
13080 :latex-preview on a LaTeX fragment with overlayed preview image
13082 This function expects the position to be visible because it uses font-lock
13083 faces as a help to recognize the following contexts: :table-special, :link,
13085 (let* ((f (get-text-property (point) 'face))
13086 (faces (if (listp f) f (list f)))
13087 (p (point)) clist o)
13088 ;; First the large context
13090 ((org-on-heading-p t)
13091 (push (list :headline (point-at-bol) (point-at-eol)) clist)
13093 (beginning-of-line 1)
13094 (looking-at org-todo-line-tags-regexp))
13095 (push (org-point-in-group p 1 :headline-stars) clist)
13096 (push (org-point-in-group p 2 :todo-keyword) clist)
13097 (push (org-point-in-group p 4 :tags) clist))
13099 (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
13100 (if (looking-at "\\[#[A-Z0-9]\\]")
13101 (push (org-point-in-group p 0 :priority) clist)))
13104 (push (org-point-in-group p 2 :item-bullet) clist)
13105 (push (list :item (point-at-bol)
13106 (save-excursion (org-end-of-item) (point)))
13108 (and (org-at-item-checkbox-p)
13109 (push (org-point-in-group p 0 :checkbox) clist)))
13112 (push (list :table (org-table-begin) (org-table-end)) clist)
13113 (if (memq 'org-formula faces)
13114 (push (list :table-special
13115 (previous-single-property-change p 'face)
13116 (next-single-property-change p 'face)) clist)))
13117 ((org-at-table-p 'any)
13118 (push (list :table-table) clist)))
13121 ;; Now the small context
13123 ((org-at-timestamp-p)
13124 (push (org-point-in-group p 0 :timestamp) clist))
13125 ((memq 'org-link faces)
13127 (previous-single-property-change p 'face)
13128 (next-single-property-change p 'face)) clist))
13129 ((memq 'org-special-keyword faces)
13130 (push (list :keyword
13131 (previous-single-property-change p 'face)
13132 (next-single-property-change p 'face)) clist))
13134 (push (org-point-in-group p 0 :target) clist)
13135 (goto-char (1- (match-beginning 0)))
13136 (if (looking-at org-radio-target-regexp)
13137 (push (org-point-in-group p 0 :radio-target) clist))
13139 ((setq o (car (delq nil
13142 (if (memq x org-latex-fragment-image-overlays) x))
13143 (org-overlays-at (point))))))
13144 (push (list :latex-fragment
13145 (org-overlay-start o) (org-overlay-end o)) clist)
13146 (push (list :latex-preview
13147 (org-overlay-start o) (org-overlay-end o)) clist))
13148 ((org-inside-LaTeX-fragment-p)
13149 ;; FIXME: positions wrong.
13150 (push (list :latex-fragment (point) (point)) clist)))
13152 (setq clist (nreverse (delq nil clist)))
13155 ;; FIXME: Compare with at-regexp-p Do we need both?
13156 (defun org-in-regexp (re &optional nlines visually)
13157 "Check if point is inside a match of regexp.
13158 Normally only the current line is checked, but you can include NLINES extra
13159 lines both before and after point into the search.
13160 If VISUALLY is set, require that the cursor is not after the match but
13161 really on, so that the block visually is on the match."
13163 (let ((pos (point))
13164 (eol (point-at-eol (+ 1 (or nlines 0))))
13165 (inc (if visually 1 0)))
13167 (beginning-of-line (- 1 (or nlines 0)))
13168 (while (re-search-forward re eol t)
13169 (if (and (<= (match-beginning 0) pos)
13170 (>= (+ inc (match-end 0)) pos))
13171 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
13173 (defun org-at-regexp-p (regexp)
13174 "Is point inside a match of REGEXP in the current line?"
13177 (let ((pos (point)) (end (point-at-eol)))
13178 (beginning-of-line 1)
13179 (while (re-search-forward regexp end t)
13180 (if (and (<= (match-beginning 0) pos)
13181 (>= (match-end 0) pos))
13185 (defun org-occur-in-agenda-files (regexp &optional nlines)
13186 "Call `multi-occur' with buffers for all agenda files."
13187 (interactive "sOrg-files matching: \np")
13188 (let* ((files (org-agenda-files))
13189 (tnames (mapcar 'file-truename files))
13190 (extra org-agenda-text-search-extra-files)
13192 (when (eq (car extra) 'agenda-archives)
13193 (setq extra (cdr extra))
13194 (setq files (org-add-archive-files files)))
13195 (while (setq f (pop extra))
13196 (unless (member (file-truename f) tnames)
13197 (add-to-list 'files f 'append)
13198 (add-to-list 'tnames (file-truename f) 'append)))
13200 (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
13203 (if (boundp 'occur-mode-find-occurrence-hook)
13205 (add-hook 'occur-mode-find-occurrence-hook
13210 (defadvice occur-mode-goto-occurrence
13211 (after org-occur-reveal activate)
13212 (and (org-mode-p) (org-reveal)))
13213 (defadvice occur-mode-goto-occurrence-other-window
13214 (after org-occur-reveal activate)
13215 (and (org-mode-p) (org-reveal)))
13216 (defadvice occur-mode-display-occurrence
13217 (after org-occur-reveal activate)
13219 (let ((pos (occur-mode-find-occurrence)))
13220 (with-current-buffer (marker-buffer pos)
13225 (defun org-uniquify (list)
13226 "Remove duplicate elements from LIST."
13228 (mapc (lambda (x) (add-to-list 'res x 'append)) list)
13231 (defun org-delete-all (elts list)
13232 "Remove all elements in ELTS from LIST."
13234 (setq list (delete (pop elts) list)))
13237 (defun org-back-over-empty-lines ()
13238 "Move backwards over witespace, to the beginning of the first empty line.
13239 Returns the number of empty lines passed."
13240 (let ((pos (point)))
13241 (skip-chars-backward " \t\n\r")
13242 (beginning-of-line 2)
13243 (goto-char (min (point) pos))
13244 (count-lines (point) pos)))
13246 (defun org-skip-whitespace ()
13247 (skip-chars-forward " \t\n\r"))
13249 (defun org-point-in-group (point group &optional context)
13250 "Check if POINT is in match-group GROUP.
13251 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
13252 match. If the match group does ot exist or point is not inside it,
13254 (and (match-beginning group)
13255 (>= point (match-beginning group))
13256 (<= point (match-end group))
13258 (list context (match-beginning group) (match-end group))
13261 (defun org-switch-to-buffer-other-window (&rest args)
13262 "Switch to buffer in a second window on the current frame.
13263 In particular, do not allow pop-up frames."
13264 (let (pop-up-frames special-display-buffer-names special-display-regexps
13265 special-display-function)
13266 (apply 'switch-to-buffer-other-window args)))
13268 (defun org-combine-plists (&rest plists)
13269 "Create a single property list from all plists in PLISTS.
13270 The process starts by copying the first list, and then setting properties
13271 from the other lists. Settings in the last list are the most significant
13272 ones and overrule settings in the other lists."
13273 (let ((rtn (copy-sequence (pop plists)))
13276 (setq ls (pop plists))
13278 (setq p (pop ls) v (pop ls))
13279 (setq rtn (plist-put rtn p v))))
13282 (defun org-move-line-down (arg)
13283 "Move the current line down. With prefix argument, move it past ARG lines."
13285 (let ((col (current-column))
13287 (beginning-of-line 1) (setq beg (point))
13288 (beginning-of-line 2) (setq end (point))
13289 (beginning-of-line (+ 1 arg))
13290 (setq pos (move-marker (make-marker) (point)))
13291 (insert (delete-and-extract-region beg end))
13293 (org-move-to-column col)))
13295 (defun org-move-line-up (arg)
13296 "Move the current line up. With prefix argument, move it past ARG lines."
13298 (let ((col (current-column))
13300 (beginning-of-line 1) (setq beg (point))
13301 (beginning-of-line 2) (setq end (point))
13302 (beginning-of-line (- arg))
13303 (setq pos (move-marker (make-marker) (point)))
13304 (insert (delete-and-extract-region beg end))
13306 (org-move-to-column col)))
13308 (defun org-replace-escapes (string table)
13309 "Replace %-escapes in STRING with values in TABLE.
13310 TABLE is an association list with keys like \"%a\" and string values.
13311 The sequences in STRING may contain normal field width and padding information,
13312 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
13313 so values can contain further %-escapes if they are define later in TABLE."
13314 (let ((case-fold-search nil)
13316 (while (setq e (pop table))
13317 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
13318 (while (string-match re string)
13319 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
13321 (setq string (replace-match rpl t t string))))
13325 (defun org-sublist (list start end)
13326 "Return a section of LIST, from START to END.
13327 Counting starts at 1."
13328 (let (rtn (c start))
13329 (setq list (nthcdr (1- start) list))
13330 (while (and list (<= c end))
13331 (push (pop list) rtn)
13335 (defun org-find-base-buffer-visiting (file)
13336 "Like `find-buffer-visiting' but alway return the base buffer and
13337 not an indirect buffer."
13338 (let ((buf (find-buffer-visiting file)))
13340 (or (buffer-base-buffer buf) buf)
13343 (defun org-image-file-name-regexp ()
13344 "Return regexp matching the file names of images."
13345 (if (fboundp 'image-file-name-regexp)
13346 (image-file-name-regexp)
13347 (let ((image-file-name-extensions
13348 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
13349 "xbm" "xpm" "pbm" "pgm" "ppm")))
13351 (regexp-opt (nconc (mapcar 'upcase
13352 image-file-name-extensions)
13353 image-file-name-extensions)
13357 (defun org-file-image-p (file)
13358 "Return non-nil if FILE is an image."
13360 (string-match (org-image-file-name-regexp) file)))
13362 ;;; Paragraph filling stuff.
13363 ;; We want this to be just right, so use the full arsenal.
13365 (defun org-indent-line-function ()
13366 "Indent line like previous, but further if previous was headline or item."
13368 (let* ((pos (point))
13369 (itemp (org-at-item-p))
13370 column bpos bcol tpos tcol bullet btype bullet-type)
13371 ;; Find the previous relevant line
13372 (beginning-of-line 1)
13374 ((looking-at "#") (setq column 0))
13375 ((looking-at "\\*+ ") (setq column 0))
13377 (beginning-of-line 0)
13378 (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
13379 (beginning-of-line 0))
13381 ((looking-at "\\*+[ \t]+")
13382 (goto-char (match-end 0))
13383 (setq column (current-column)))
13385 (org-beginning-of-item)
13386 ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
13387 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
13388 (setq bpos (match-beginning 1) tpos (match-end 0)
13389 bcol (progn (goto-char bpos) (current-column))
13390 tcol (progn (goto-char tpos) (current-column))
13391 bullet (match-string 1)
13392 bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
13393 (if (> tcol (+ bcol org-description-max-indent))
13394 (setq tcol (+ bcol 5)))
13398 (beginning-of-line 1)
13399 (if (looking-at "\\S-")
13401 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
13402 (setq bullet (match-string 1)
13403 btype (if (string-match "[0-9]" bullet) "n" bullet))
13404 (setq column (if (equal btype bullet-type) bcol tcol)))
13405 (setq column (org-get-indentation)))))
13406 (t (setq column (org-get-indentation))))))
13408 (if (<= (current-column) (current-indentation))
13409 (org-indent-line-to column)
13410 (save-excursion (org-indent-line-to column)))
13411 (setq column (current-column))
13412 (beginning-of-line 1)
13414 "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
13415 (replace-match (concat "\\1" (format org-property-format
13416 (match-string 2) (match-string 3)))
13418 (org-move-to-column column)))
13420 (defun org-set-autofill-regexps ()
13422 ;; In the paragraph separator we include headlines, because filling
13423 ;; text in a line directly attached to a headline would otherwise
13424 ;; fill the headline as well.
13425 (org-set-local 'comment-start-skip "^#+[ \t]*")
13426 (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
13427 ;; The paragraph starter includes hand-formatted lists.
13428 (org-set-local 'paragraph-start
13429 "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
13430 ;; Inhibit auto-fill for headers, tables and fixed-width lines.
13431 ;; But only if the user has not turned off tables or fixed-width regions
13433 'auto-fill-inhibit-regexp
13434 (concat "\\*+ \\|#\\+"
13435 "\\|[ \t]*" org-keyword-time-regexp
13436 (if (or org-enable-table-editor org-enable-fixed-width-editor)
13439 (if org-enable-table-editor "|" "")
13440 (if org-enable-fixed-width-editor ":" "")
13442 ;; We use our own fill-paragraph function, to make sure that tables
13443 ;; and fixed-width regions are not wrapped. That function will pass
13444 ;; through to `fill-paragraph' when appropriate.
13445 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
13446 ; Adaptive filling: To get full control, first make sure that
13447 ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
13448 (org-set-local 'adaptive-fill-regexp "\000")
13449 (org-set-local 'adaptive-fill-function
13450 'org-adaptive-fill-function)
13452 'align-mode-rules-list
13453 '((org-in-buffer-settings
13454 (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
13455 (modes . '(org-mode))))))
13457 (defun org-fill-paragraph (&optional justify)
13458 "Re-align a table, pass through to fill-paragraph if no table."
13459 (let ((table-p (org-at-table-p))
13460 (table.el-p (org-at-table.el-p)))
13461 (cond ((and (equal (char-after (point-at-bol)) ?*)
13462 (save-excursion (goto-char (point-at-bol))
13463 (looking-at outline-regexp)))
13464 t) ; skip headlines
13465 (table.el-p t) ; skip table.el tables
13466 (table-p (org-table-align) t) ; align org-mode tables
13467 (t nil)))) ; call paragraph-fill
13469 ;; For reference, this is the default value of adaptive-fill-regexp
13470 ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
13472 (defun org-adaptive-fill-function ()
13473 "Return a fill prefix for org-mode files.
13474 In particular, this makes sure hanging paragraphs for hand-formatted lists
13476 (cond ((looking-at "#[ \t]+")
13478 ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
13480 (if (> (match-end 1) (+ (match-beginning 1)
13481 org-description-max-indent))
13482 (goto-char (+ (match-beginning 1) 5))
13483 (goto-char (match-end 0)))
13484 (make-string (current-column) ?\ )))
13485 ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
13487 (goto-char (match-end 0))
13488 (make-string (current-column) ?\ )))
13493 (defun org-toggle-fixed-width-section (arg)
13494 "Toggle the fixed-width export.
13495 If there is no active region, the QUOTE keyword at the current headline is
13496 inserted or removed. When present, it causes the text between this headline
13497 and the next to be exported as fixed-width text, and unmodified.
13498 If there is an active region, this command adds or removes a colon as the
13499 first character of this line. If the first character of a line is a colon,
13500 this line is also exported in fixed-width font."
13503 (regionp (org-region-active-p))
13504 (beg (if regionp (region-beginning) (point)))
13505 (end (if regionp (region-end)))
13506 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
13507 (case-fold-search nil)
13508 (re "[ \t]*\\(:\\)")
13513 (setq cc (current-column))
13514 (beginning-of-line 1)
13515 (setq off (looking-at re))
13516 (while (> nlines 0)
13517 (setq nlines (1- nlines))
13518 (beginning-of-line 1)
13521 (org-move-to-column cc t)
13524 ((and off (looking-at re))
13525 (replace-match "" t t nil 1))
13526 ((not off) (org-move-to-column cc t) (insert ":")))
13529 (org-back-to-heading)
13530 (if (looking-at (concat outline-regexp
13531 "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
13532 (replace-match "" t t nil 1)
13533 (if (looking-at outline-regexp)
13535 (goto-char (match-end 0))
13536 (insert org-quote-string " "))))))))
13538 ;;;; Functions extending outline functionality
13540 (defun org-beginning-of-line (&optional arg)
13541 "Go to the beginning of the current line. If that is invisible, continue
13542 to a visible line beginning. This makes the function of C-a more intuitive.
13543 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
13544 first attempt, and only move to after the tags when the cursor is already
13545 beyond the end of the headline."
13547 (let ((pos (point)))
13548 (beginning-of-line 1)
13552 (if (org-invisible-p)
13553 (while (and (not (bobp)) (org-invisible-p))
13555 (beginning-of-line 1))
13557 (when org-special-ctrl-a/e
13559 ((and (looking-at org-todo-line-regexp)
13560 (= (char-after (match-end 1)) ?\ ))
13562 (if (eq org-special-ctrl-a/e t)
13563 (cond ((> pos (match-beginning 3)) (match-beginning 3))
13564 ((= pos (point)) (match-beginning 3))
13566 (cond ((> pos (point)) (point))
13567 ((not (eq last-command this-command)) (point))
13568 (t (match-beginning 3))))))
13571 (if (eq org-special-ctrl-a/e t)
13572 (cond ((> pos (match-end 4)) (match-end 4))
13573 ((= pos (point)) (match-end 4))
13575 (cond ((> pos (point)) (point))
13576 ((not (eq last-command this-command)) (point))
13577 (t (match-end 4))))))))))
13579 (defun org-end-of-line (&optional arg)
13580 "Go to the end of the line.
13581 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
13582 first attempt, and only move to after the tags when the cursor is already
13583 beyond the end of the headline."
13585 (if (or (not org-special-ctrl-a/e)
13586 (not (org-on-heading-p)))
13588 (let ((pos (point)))
13589 (beginning-of-line 1)
13590 (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
13591 (if (eq org-special-ctrl-a/e t)
13592 (if (or (< pos (match-beginning 1))
13593 (= pos (match-end 0)))
13594 (goto-char (match-beginning 1))
13595 (goto-char (match-end 0)))
13596 (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
13597 (goto-char (match-end 0))
13598 (goto-char (match-beginning 1))))
13599 (end-of-line arg)))))
13601 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
13602 (define-key org-mode-map "\C-e" 'org-end-of-line)
13604 (defun org-kill-line (&optional arg)
13605 "Kill line, to tags or end of line."
13608 ((or (not org-special-ctrl-k)
13610 (not (org-on-heading-p)))
13611 (call-interactively 'kill-line))
13612 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
13613 (kill-region (point) (match-beginning 1))
13614 (org-set-tags nil t))
13615 (t (kill-region (point) (point-at-eol)))))
13617 (define-key org-mode-map "\C-k" 'org-kill-line)
13619 (defun org-invisible-p ()
13620 "Check if point is at a character currently not visible."
13621 ;; Early versions of noutline don't have `outline-invisible-p'.
13622 (if (fboundp 'outline-invisible-p)
13623 (outline-invisible-p)
13624 (get-char-property (point) 'invisible)))
13626 (defun org-invisible-p2 ()
13627 "Check if point is at a character currently not visible."
13629 (if (and (eolp) (not (bobp))) (backward-char 1))
13630 ;; Early versions of noutline don't have `outline-invisible-p'.
13631 (if (fboundp 'outline-invisible-p)
13632 (outline-invisible-p)
13633 (get-char-property (point) 'invisible))))
13635 (defalias 'org-back-to-heading 'outline-back-to-heading)
13636 (defalias 'org-on-heading-p 'outline-on-heading-p)
13637 (defalias 'org-at-heading-p 'outline-on-heading-p)
13638 (defun org-at-heading-or-item-p ()
13639 (or (org-on-heading-p) (org-at-item-p)))
13641 (defun org-on-target-p ()
13642 (or (org-in-regexp org-radio-target-regexp)
13643 (org-in-regexp org-target-regexp)))
13645 (defun org-up-heading-all (arg)
13646 "Move to the heading line of which the present line is a subheading.
13647 This function considers both visible and invisible heading lines.
13648 With argument, move up ARG levels."
13649 (if (fboundp 'outline-up-heading-all)
13650 (outline-up-heading-all arg) ; emacs 21 version of outline.el
13651 (outline-up-heading arg t))) ; emacs 22 version of outline.el
13653 (defun org-up-heading-safe ()
13654 "Move to the heading line of which the present line is a subheading.
13655 This version will not throw an error. It will return the level of the
13656 headline found, or nil if no higher level is found."
13657 (let ((pos (point)) start-level level
13658 (re (concat "^" outline-regexp)))
13660 (outline-back-to-heading t)
13661 (setq start-level (funcall outline-level))
13662 (if (equal start-level 1) (throw 'exit nil))
13663 (while (re-search-backward re nil t)
13664 (setq level (funcall outline-level))
13665 (if (< level start-level) (throw 'exit level)))
13668 (defun org-first-sibling-p ()
13669 "Is this heading the first child of its parents?"
13671 (let ((re (concat "^" outline-regexp))
13673 (unless (org-at-heading-p t)
13674 (error "Not at a heading"))
13675 (setq level (funcall outline-level))
13677 (if (not (re-search-backward re nil t))
13679 (setq l (funcall outline-level))
13682 (defun org-goto-sibling (&optional previous)
13683 "Goto the next sibling, even if it is invisible.
13684 When PREVIOUS is set, go to the previous sibling instead. Returns t
13685 when a sibling was found. When none is found, return nil and don't
13687 (let ((fun (if previous 're-search-backward 're-search-forward))
13689 (re (concat "^" outline-regexp))
13691 (when (condition-case nil (org-back-to-heading t) (error nil))
13692 (setq level (funcall outline-level))
13694 (or previous (forward-char 1))
13695 (while (funcall fun re nil t)
13696 (setq l (funcall outline-level))
13697 (when (< l level) (goto-char pos) (throw 'exit nil))
13698 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
13702 (defun org-show-siblings ()
13703 "Show all siblings of the current headline."
13705 (while (org-goto-sibling) (org-flag-heading nil)))
13707 (while (org-goto-sibling 'previous)
13708 (org-flag-heading nil))))
13710 (defun org-show-hidden-entry ()
13711 "Show an entry where even the heading is hidden."
13715 (defun org-flag-heading (flag &optional entry)
13716 "Flag the current heading. FLAG non-nil means make invisible.
13717 When ENTRY is non-nil, show the entire entry."
13719 (org-back-to-heading t)
13720 ;; Check if we should show the entire entry
13725 (and (outline-next-heading)
13726 (org-flag-heading nil))))
13727 (outline-flag-region (max (point-min) (1- (point)))
13728 (save-excursion (outline-end-of-heading) (point))
13731 (defun org-end-of-subtree (&optional invisible-OK to-heading)
13732 ;; This is an exact copy of the original function, but it uses
13733 ;; `org-back-to-heading', to make it work also in invisible
13734 ;; trees. And is uses an invisible-OK argument.
13735 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
13736 (org-back-to-heading invisible-OK)
13738 (level (funcall outline-level)))
13739 (while (and (not (eobp))
13740 (or first (> (funcall outline-level) level)))
13742 (outline-next-heading))
13744 (if (memq (preceding-char) '(?\n ?\^M))
13746 ;; Go to end of line before heading
13748 (if (memq (preceding-char) '(?\n ?\^M))
13749 ;; leave blank line before heading
13750 (forward-char -1))))))
13753 (defun org-show-subtree ()
13754 "Show everything after this heading at deeper levels."
13755 (outline-flag-region
13758 (outline-end-of-subtree) (outline-next-heading) (point))
13761 (defun org-show-entry ()
13762 "Show the body directly following this heading.
13763 Show the heading too, if it is currently invisible."
13766 (condition-case nil
13768 (org-back-to-heading t)
13769 (outline-flag-region
13770 (max (point-min) (1- (point)))
13773 (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
13774 (or (match-beginning 1) (point-max)))
13778 (defun org-make-options-regexp (kwds)
13779 "Make a regular expression for keyword lines."
13783 (mapconcat 'regexp-quote kwds "\\|")
13787 ;; Make isearch reveal the necessary context
13788 (defun org-isearch-end ()
13789 "Reveal context after isearch exits."
13790 (when isearch-success ; only if search was successful
13791 (if (featurep 'xemacs)
13792 ;; Under XEmacs, the hook is run in the correct place,
13793 ;; we directly show the context.
13794 (org-show-context 'isearch)
13795 ;; In Emacs the hook runs *before* restoring the overlays.
13796 ;; So we have to use a one-time post-command-hook to do this.
13797 ;; (Emacs 22 has a special variable, see function `org-mode')
13798 (unless (and (boundp 'isearch-mode-end-hook-quit)
13799 isearch-mode-end-hook-quit)
13800 ;; Only when the isearch was not quitted.
13801 (org-add-hook 'post-command-hook 'org-isearch-post-command
13802 'append 'local)))))
13804 (defun org-isearch-post-command ()
13805 "Remove self from hook, and show context."
13806 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
13807 (org-show-context 'isearch))
13810 ;;;; Integration with and fixes for other packages
13814 (defvar org-imenu-markers nil
13815 "All markers currently used by Imenu.")
13816 (make-variable-buffer-local 'org-imenu-markers)
13818 (defun org-imenu-new-marker (&optional pos)
13819 "Return a new marker for use by Imenu, and remember the marker."
13820 (let ((m (make-marker)))
13821 (move-marker m (or pos (point)))
13822 (push m org-imenu-markers)
13825 (defun org-imenu-get-tree ()
13826 "Produce the index for Imenu."
13827 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
13828 (setq org-imenu-markers nil)
13829 (let* ((n org-imenu-depth)
13830 (re (concat "^" outline-regexp))
13831 (subs (make-vector (1+ n) nil))
13837 (goto-char (point-max))
13838 (while (re-search-backward re nil t)
13839 (setq level (org-reduced-level (funcall outline-level)))
13841 (looking-at org-complex-heading-regexp)
13842 (setq head (org-match-string-no-properties 4)
13843 m (org-imenu-new-marker))
13844 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
13845 (if (>= level last-level)
13846 (push (cons head m) (aref subs level))
13847 (push (cons head (aref subs (1+ level))) (aref subs level))
13848 (loop for i from (1+ level) to n do (aset subs i nil)))
13849 (setq last-level level)))))
13852 (eval-after-load "imenu"
13854 (add-hook 'imenu-after-jump-hook
13855 (lambda () (org-show-context 'org-goto)))))
13857 ;; Speedbar support
13859 (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
13860 "Overlay marking the agenda restriction line in speedbar.")
13861 (org-overlay-put org-speedbar-restriction-lock-overlay
13862 'face 'org-agenda-restriction-lock)
13863 (org-overlay-put org-speedbar-restriction-lock-overlay
13864 'help-echo "Agendas are currently limited to this item.")
13865 (org-detach-overlay org-speedbar-restriction-lock-overlay)
13867 (defun org-speedbar-set-agenda-restriction ()
13868 "Restrict future agenda commands to the location at point in speedbar.
13869 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
13871 (require 'org-agenda)
13872 (let (p m tp np dir txt w)
13874 ((setq p (text-property-any (point-at-bol) (point-at-eol)
13876 (setq m (get-text-property p 'org-imenu-marker))
13879 (set-buffer (marker-buffer m))
13881 (org-agenda-set-restriction-lock 'subtree))))
13882 ((setq p (text-property-any (point-at-bol) (point-at-eol)
13883 'speedbar-function 'speedbar-find-file))
13884 (setq tp (previous-single-property-change
13885 (1+ p) 'speedbar-function)
13886 np (next-single-property-change
13887 tp 'speedbar-function)
13888 dir (speedbar-line-directory)
13889 txt (buffer-substring-no-properties (or tp (point-min))
13890 (or np (point-max))))
13893 (set-buffer (find-file-noselect
13894 (let ((default-directory dir))
13895 (expand-file-name txt))))
13896 (unless (org-mode-p)
13897 (error "Cannot restrict to non-Org-mode file"))
13898 (org-agenda-set-restriction-lock 'file))))
13899 (t (error "Don't know how to restrict Org-mode's agenda")))
13900 (org-move-overlay org-speedbar-restriction-lock-overlay
13901 (point-at-bol) (point-at-eol))
13902 (setq current-prefix-arg nil)
13903 (org-agenda-maybe-redo)))
13905 (eval-after-load "speedbar"
13907 (speedbar-add-supported-extension ".org")
13908 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
13909 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
13910 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
13911 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
13912 (add-hook 'speedbar-visiting-tag-hook
13913 (lambda () (org-show-context 'org-goto)))))
13916 ;;; Fixes and Hacks for problems with other packages
13918 ;; Make flyspell not check words in links, to not mess up our keymap
13919 (defun org-mode-flyspell-verify ()
13920 "Don't let flyspell put overlays at active buttons."
13921 (not (get-text-property (point) 'keymap)))
13923 ;; Make `bookmark-jump' show the jump location if it was hidden.
13924 (eval-after-load "bookmark"
13925 '(if (boundp 'bookmark-after-jump-hook)
13926 ;; We can use the hook
13927 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
13928 ;; Hook not available, use advice
13929 (defadvice bookmark-jump (after org-make-visible activate)
13930 "Make the position visible."
13931 (org-bookmark-jump-unhide))))
13933 (defun org-bookmark-jump-unhide ()
13934 "Unhide the current position, to show the bookmark location."
13936 (or (org-invisible-p)
13937 (save-excursion (goto-char (max (point-min) (1- (point))))
13938 (org-invisible-p)))
13939 (org-show-context 'bookmark-jump)))
13941 ;; Make session.el ignore our circular variable
13942 (eval-after-load "session"
13943 '(add-to-list 'session-globals-exclude 'org-mark-ring))
13945 ;;;; Experimental code
13947 (defun org-closed-in-range ()
13948 "Sparse tree of items closed in a certain time range.
13949 Still experimental, may disappear in the future."
13951 ;; Get the time interval from the user.
13952 (let* ((time1 (time-to-seconds
13953 (org-read-date nil 'to-time nil "Starting date: ")))
13954 (time2 (time-to-seconds
13955 (org-read-date nil 'to-time nil "End date:")))
13956 ;; callback function
13957 (callback (lambda ()
13960 (apply 'encode-time
13961 (org-parse-time-string
13962 (match-string 1))))))
13963 ;; check if time in interval
13964 (and (>= time time1) (<= time time2))))))
13965 ;; make tree, check each match with the callback
13966 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
13973 (run-hooks 'org-load-hook)
13975 ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
13977 ;;; org.el ends here