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
))
91 ;;;; Customization variables
95 (defconst org-version
"6.15trans"
96 "The version number of the file org.el.")
98 (defun org-version (&optional here
)
99 "Show the org-mode version in the echo area.
100 With prefix arg HERE, insert it at point."
102 (let ((version (format "Org-mode version %s" org-version
)))
107 ;;; Compatibility constants
109 ;;; The custom variables
112 "Outline-based notes management and organizer."
118 (defcustom org-load-hook nil
119 "Hook that is run after org.el has been loaded."
123 (defvar org-modules
) ; defined below
124 (defvar org-modules-loaded nil
125 "Have the modules been loaded already?")
127 (defun org-load-modules-maybe (&optional force
)
128 "Load all extensions listed in `org-modules'."
129 (when (or force
(not org-modules-loaded
))
131 (condition-case nil
(require ext
)
132 (error (message "Problems while trying to load feature `%s'" ext
))))
134 (setq org-modules-loaded t
)))
136 (defun org-set-modules (var value
)
137 "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
139 (when (featurep 'org
)
140 (org-load-modules-maybe 'force
)))
142 (when (org-bound-and-true-p org-modules
)
143 (let ((a (member 'org-infojs org-modules
)))
144 (and a
(setcar a
'org-jsinfo
))))
146 (defcustom org-modules
'(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl
)
147 "Modules that should always be loaded together with org.el.
148 If a description starts with <C>, the file is not part of Emacs
149 and loading it will require that you have downloaded and properly installed
150 the org-mode distribution.
152 You can also use this system to load external packages (i.e. neither Org
153 core modules, not modules from the CONTRIB directory). Just add symbols
154 to the end of the list. If the package is called org-xyz.el, then you need
155 to add the symbol `xyz', and the package must have a call to
159 :set
'org-set-modules
162 (const :tag
" bbdb: Links to BBDB entries" org-bbdb
)
163 (const :tag
" bibtex: Links to BibTeX entries" org-bibtex
)
164 (const :tag
" gnus: Links to GNUS folders/messages" org-gnus
)
165 (const :tag
" id: Global IDs for identifying entries" org-id
)
166 (const :tag
" info: Links to Info nodes" org-info
)
167 (const :tag
" jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo
)
168 (const :tag
" irc: Links to IRC/ERC chat sessions" org-irc
)
169 (const :tag
" mac-message: Links to messages in Apple Mail" org-mac-message
)
170 (const :tag
" mew Links to Mew folders/messages" org-mew
)
171 (const :tag
" mhe: Links to MHE folders/messages" org-mhe
)
172 (const :tag
" rmail: Links to RMAIL folders/messages" org-rmail
)
173 (const :tag
" vm: Links to VM folders/messages" org-vm
)
174 (const :tag
" wl: Links to Wanderlust folders/messages" org-wl
)
175 (const :tag
" w3m: Special cut/past from w3m to Org." org-w3m
)
176 (const :tag
" mouse: Additional mouse support" org-mouse
)
178 (const :tag
"C annotate-file: Annotate a file with org syntax" org-annotate-file
)
179 (const :tag
"C annotation-helper: Call Remember directly from Browser" org-annotation-helper
)
180 (const :tag
"C bookmark: Org links to bookmarks" org-bookmark
)
181 (const :tag
"C browser-url: Store link, directly from Browser" org-browser-url
)
182 (const :tag
"C depend: TODO dependencies for Org-mode" org-depend
)
183 (const :tag
"C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol
)
184 (const :tag
"C eval: Include command output as text" org-eval
)
185 (const :tag
"C eval-light: Evaluate inbuffer-code on demand" org-eval-light
)
186 (const :tag
"C expiry: Expiry mechanism for Org entries" org-expiry
)
187 (const :tag
"C exp-blocks: Pre-process blocks for export" org-exp-blocks
)
188 (const :tag
"C interactive-query: Interactive modification of tags query" org-interactive-query
)
189 (const :tag
"C mairix: Hook mairix search into Org for different MUAs" org-mairix
)
190 (const :tag
"C man: Support for links to manpages in Org-mode" org-man
)
191 (const :tag
"C mtags: Support for muse-like tags" org-mtags
)
192 (const :tag
"C panel: Simple routines for us with bad memory" org-panel
)
193 (const :tag
"C registry: A registry for Org links" org-registry
)
194 (const :tag
"C org2rem: Convert org appointments into reminders" org2rem
)
195 (const :tag
"C screen: Visit screen sessions through Org-mode links" org-screen
)
196 (const :tag
"C toc: Table of contents for Org-mode buffer" org-toc
)
197 (const :tag
"C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert
)
198 (repeat :tag
"External packages" :inline t
(symbol :tag
"Package"))))
201 (defgroup org-startup nil
202 "Options concerning startup of Org-mode."
206 (defcustom org-startup-folded t
207 "Non-nil means, entering Org-mode will switch to OVERVIEW.
208 This can also be configured on a per-file basis by adding one of
209 the following lines anywhere in the buffer:
216 (const :tag
"nofold: show all" nil
)
217 (const :tag
"fold: overview" t
)
218 (const :tag
"content: all headlines" content
)))
220 (defcustom org-startup-truncated t
221 "Non-nil means, entering Org-mode will set `truncate-lines'.
222 This is useful since some lines containing links can be very long and
223 uninteresting. Also tables look terrible when wrapped."
227 (defcustom org-startup-align-all-tables nil
228 "Non-nil means, align all tables when visiting a file.
229 This is useful when the column width in tables is forced with <N> cookies
230 in table fields. Such tables will look correct only after the first re-align.
231 This can also be configured on a per-file basis by adding one of
232 the following lines anywhere in the buffer:
238 (defcustom org-insert-mode-line-in-empty-file nil
239 "Non-nil means insert the first line setting Org-mode in empty files.
240 When the function `org-mode' is called interactively in an empty file, this
241 normally means that the file name does not automatically trigger Org-mode.
242 To ensure that the file will always be in Org-mode in the future, a
243 line enforcing Org-mode will be inserted into the buffer, if this option
248 (defcustom org-replace-disputed-keys nil
249 "Non-nil means use alternative key bindings for some keys.
250 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
251 These keys are also used by other packages like `CUA-mode' or `windmove.el'.
252 If you want to use Org-mode together with one of these other modes,
253 or more generally if you would like to move some Org-mode commands to
254 other keys, set this variable and configure the keys with the variable
257 This option is only relevant at load-time of Org-mode, and must be set
258 *before* org.el is loaded. Changing it requires a restart of Emacs to
263 (defcustom org-use-extra-keys nil
264 "Non-nil means use extra key sequence definitions for certain
265 commands. This happens automatically if you run XEmacs or if
266 window-system is nil. This variable lets you do the same
267 manually. You must set it before loading org.
269 Example: on Carbon Emacs 22 running graphically, with an external
270 keyboard on a Powerbook, the default way of setting M-left might
271 not work for either Alt or ESC. Setting this variable will make
276 (if (fboundp 'defvaralias
)
277 (defvaralias 'org-CUA-compatible
'org-replace-disputed-keys
))
279 (defcustom org-disputed-keys
280 '(([(shift up
)] .
[(meta p
)])
281 ([(shift down
)] .
[(meta n
)])
282 ([(shift left
)] .
[(meta -
)])
283 ([(shift right
)] .
[(meta +)])
284 ([(control shift right
)] .
[(meta shift
+)])
285 ([(control shift left
)] .
[(meta shift -
)]))
286 "Keys for which Org-mode and other modes compete.
287 This is an alist, cars are the default keys, second element specifies
288 the alternative to use when `org-replace-disputed-keys' is t.
290 Keys can be specified in any syntax supported by `define-key'.
291 The value of this option takes effect only at Org-mode's startup,
292 therefore you'll have to restart Emacs to apply it after changing."
297 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
298 Or return the original if not disputed."
299 (if org-replace-disputed-keys
300 (let* ((nkey (key-description key
))
301 (x (org-find-if (lambda (x)
302 (equal (key-description (car x
)) nkey
))
307 (defun org-find-if (predicate seq
)
310 (if (funcall predicate
(car seq
))
311 (throw 'exit
(car seq
))
314 (defun org-defkey (keymap key def
)
315 "Define a key, possibly translated, as returned by `org-key'."
316 (define-key keymap
(org-key key
) def
))
318 (defcustom org-ellipsis nil
319 "The ellipsis to use in the Org-mode outline.
320 When nil, just use the standard three dots. When a string, use that instead,
321 When a face, use the standard 3 dots, but with the specified face.
322 The change affects only Org-mode (which will then use its own display table).
323 Changing this requires executing `M-x org-mode' in a buffer to become
326 :type
'(choice (const :tag
"Default" nil
)
327 (face :tag
"Face" :value org-warning
)
328 (string :tag
"String" :value
"...#")))
330 (defvar org-display-table nil
331 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
333 (defgroup org-keywords nil
334 "Keywords in Org-mode."
338 (defcustom org-deadline-string
"DEADLINE:"
339 "String to mark deadline entries.
340 A deadline is this string, followed by a time stamp. Should be a word,
341 terminated by a colon. You can insert a schedule keyword and
342 a timestamp with \\[org-deadline].
343 Changes become only effective after restarting Emacs."
347 (defcustom org-scheduled-string
"SCHEDULED:"
348 "String to mark scheduled TODO entries.
349 A schedule is this string, followed by a time stamp. Should be a word,
350 terminated by a colon. You can insert a schedule keyword and
351 a timestamp with \\[org-schedule].
352 Changes become only effective after restarting Emacs."
356 (defcustom org-closed-string
"CLOSED:"
357 "String used as the prefix for timestamps logging closing a TODO entry."
361 (defcustom org-clock-string
"CLOCK:"
362 "String used as prefix for timestamps clocking work hours on an item."
366 (defcustom org-comment-string
"COMMENT"
367 "Entries starting with this keyword will never be exported.
368 An entry can be toggled between COMMENT and normal with
369 \\[org-toggle-comment].
370 Changes become only effective after restarting Emacs."
374 (defcustom org-quote-string
"QUOTE"
375 "Entries starting with this keyword will be exported in fixed-width font.
376 Quoting applies only to the text in the entry following the headline, and does
377 not extend beyond the next headline, even if that is lower level.
378 An entry can be toggled between QUOTE and normal with
379 \\[org-toggle-fixed-width-section]."
383 (defconst org-repeat-re
384 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
385 "Regular expression for specifying repeated events.
386 After a match, group 1 contains the repeat expression.")
388 (defgroup org-structure nil
389 "Options concerning the general structure of Org-mode files."
393 (defgroup org-reveal-location nil
394 "Options about how to make context of a location visible."
395 :tag
"Org Reveal Location"
396 :group
'org-structure
)
398 (defconst org-context-choice
400 (const :tag
"Always" t
)
401 (const :tag
"Never" nil
)
402 (repeat :greedy t
:tag
"Individual contexts"
404 (choice :tag
"Context"
411 (const bookmark-jump
)
415 "Contexts for the reveal options.")
417 (defcustom org-show-hierarchy-above
'((default . t
))
418 "Non-nil means, show full hierarchy when revealing a location.
419 Org-mode often shows locations in an org-mode file which might have
420 been invisible before. When this is set, the hierarchy of headings
421 above the exposed location is shown.
422 Turning this off for example for sparse trees makes them very compact.
423 Instead of t, this can also be an alist specifying this option for different
424 contexts. Valid contexts are
425 agenda when exposing an entry from the agenda
426 org-goto when using the command `org-goto' on key C-c C-j
427 occur-tree when using the command `org-occur' on key C-c /
428 tags-tree when constructing a sparse tree based on tags matches
429 link-search when exposing search matches associated with a link
430 mark-goto when exposing the jump goal of a mark
431 bookmark-jump when exposing a bookmark location
432 isearch when exiting from an incremental search
433 default default for all contexts not set explicitly"
434 :group
'org-reveal-location
435 :type org-context-choice
)
437 (defcustom org-show-following-heading
'((default . nil
))
438 "Non-nil means, show following heading when revealing a location.
439 Org-mode often shows locations in an org-mode file which might have
440 been invisible before. When this is set, the heading following the
442 Turning this off for example for sparse trees makes them very compact,
443 but makes it harder to edit the location of the match. In such a case,
444 use the command \\[org-reveal] to show more context.
445 Instead of t, this can also be an alist specifying this option for different
446 contexts. See `org-show-hierarchy-above' for valid contexts."
447 :group
'org-reveal-location
448 :type org-context-choice
)
450 (defcustom org-show-siblings
'((default . nil
) (isearch t
))
451 "Non-nil means, show all sibling heading when revealing a location.
452 Org-mode often shows locations in an org-mode file which might have
453 been invisible before. When this is set, the sibling of the current entry
454 heading are all made visible. If `org-show-hierarchy-above' is t,
455 the same happens on each level of the hierarchy above the current entry.
457 By default this is on for the isearch context, off for all other contexts.
458 Turning this off for example for sparse trees makes them very compact,
459 but makes it harder to edit the location of the match. In such a case,
460 use the command \\[org-reveal] to show more context.
461 Instead of t, this can also be an alist specifying this option for different
462 contexts. See `org-show-hierarchy-above' for valid contexts."
463 :group
'org-reveal-location
464 :type org-context-choice
)
466 (defcustom org-show-entry-below
'((default . nil
))
467 "Non-nil means, show the entry below a headline when revealing a location.
468 Org-mode often shows locations in an org-mode file which might have
469 been invisible before. When this is set, the text below the headline that is
470 exposed is also shown.
472 By default this is off for all contexts.
473 Instead of t, this can also be an alist specifying this option for different
474 contexts. See `org-show-hierarchy-above' for valid contexts."
475 :group
'org-reveal-location
476 :type org-context-choice
)
478 (defcustom org-indirect-buffer-display
'other-window
479 "How should indirect tree buffers be displayed?
480 This applies to indirect buffers created with the commands
481 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
483 current-window Display in the current window
484 other-window Just display in another window.
485 dedicated-frame Create one new frame, and re-use it each time.
486 new-frame Make a new frame each time. Note that in this case
487 previously-made indirect buffers are kept, and you need to
488 kill these buffers yourself."
489 :group
'org-structure
490 :group
'org-agenda-windows
492 (const :tag
"In current window" current-window
)
493 (const :tag
"In current frame, other window" other-window
)
494 (const :tag
"Each time a new frame" new-frame
)
495 (const :tag
"One dedicated frame" dedicated-frame
)))
497 (defgroup org-cycle nil
498 "Options concerning visibility cycling in Org-mode."
500 :group
'org-structure
)
502 (defcustom org-drawers
'("PROPERTIES" "CLOCK")
503 "Names of drawers. Drawers are not opened by cycling on the headline above.
504 Drawers only open with a TAB on the drawer line itself. A drawer looks like
509 The drawer \"PROPERTIES\" is special for capturing properties through
512 Drawers can be defined on the per-file basis with a line like:
514 #+DRAWERS: HIDDEN STATE PROPERTIES"
515 :group
'org-structure
516 :type
'(repeat (string :tag
"Drawer Name")))
518 (defcustom org-cycle-global-at-bob nil
519 "Cycle globally if cursor is at beginning of buffer and not at a headline.
520 This makes it possible to do global cycling without having to use S-TAB or
521 C-u TAB. For this special case to work, the first line of the buffer
522 must not be a headline - it may be empty ot some other text. When used in
523 this way, `org-cycle-hook' is disables temporarily, to make sure the
524 cursor stays at the beginning of the buffer.
525 When this option is nil, don't do anything special at the beginning
530 (defcustom org-cycle-emulate-tab t
531 "Where should `org-cycle' emulate TAB.
533 white Only in completely white lines
534 whitestart Only at the beginning of lines, before the first non-white char
535 t Everywhere except in headlines
536 exc-hl-bol Everywhere except at the start of a headline
537 If TAB is used in a place where it does not emulate TAB, the current subtree
538 visibility is cycled."
540 :type
'(choice (const :tag
"Never" nil
)
541 (const :tag
"Only in completely white lines" white
)
542 (const :tag
"Before first char in a line" whitestart
)
543 (const :tag
"Everywhere except in headlines" t
)
544 (const :tag
"Everywhere except at bol in headlines" exc-hl-bol
)
547 (defcustom org-cycle-separator-lines
2
548 "Number of empty lines needed to keep an empty line between collapsed trees.
549 If you leave an empty line between the end of a subtree and the following
550 headline, this empty line is hidden when the subtree is folded.
551 Org-mode will leave (exactly) one empty line visible if the number of
552 empty lines is equal or larger to the number given in this variable.
553 So the default 2 means, at least 2 empty lines after the end of a subtree
554 are needed to produce free space between a collapsed subtree and the
557 Special case: when 0, never leave empty lines in collapsed view."
560 (put 'org-cycle-separator-lines
'safe-local-variable
'integerp
)
562 (defcustom org-cycle-hook
'(org-cycle-hide-archived-subtrees
563 org-cycle-hide-drawers
564 org-cycle-show-empty-lines
565 org-optimize-window-after-visibility-change
)
566 "Hook that is run after `org-cycle' has changed the buffer visibility.
567 The function(s) in this hook must accept a single argument which indicates
568 the new state that was set by the most recent `org-cycle' command. The
569 argument is a symbol. After a global state change, it can have the values
570 `overview', `content', or `all'. After a local state change, it can have
571 the values `folded', `children', or `subtree'."
575 (defgroup org-edit-structure nil
576 "Options concerning structure editing in Org-mode."
577 :tag
"Org Edit Structure"
578 :group
'org-structure
)
580 (defcustom org-odd-levels-only nil
581 "Non-nil means, skip even levels and only use odd levels for the outline.
582 This has the effect that two stars are being added/taken away in
583 promotion/demotion commands. It also influences how levels are
584 handled by the exporters.
585 Changing it requires restart of `font-lock-mode' to become effective
586 for fontification also in regions already fontified.
587 You may also set this on a per-file basis by adding one of the following
592 :group
'org-edit-structure
593 :group
'org-font-lock
596 (defcustom org-adapt-indentation t
597 "Non-nil means, adapt indentation when promoting and demoting.
598 When this is set and the *entire* text in an entry is indented, the
599 indentation is increased by one space in a demotion command, and
600 decreased by one in a promotion command. If any line in the entry
601 body starts at column 0, indentation is not changed at all."
602 :group
'org-edit-structure
605 (defcustom org-special-ctrl-a
/e nil
606 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
607 When t, `C-a' will bring back the cursor to the beginning of the
608 headline text, i.e. after the stars and after a possible TODO keyword.
609 In an item, this will be the position after the bullet.
610 When the cursor is already at that position, another `C-a' will bring
611 it to the beginning of the line.
612 `C-e' will jump to the end of the headline, ignoring the presence of tags
613 in the headline. A second `C-e' will then jump to the true end of the
614 line, after any tags.
615 When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
616 and only a directly following, identical keypress will bring the cursor
617 to the special positions."
618 :group
'org-edit-structure
620 (const :tag
"off" nil
)
621 (const :tag
"after bullet first" t
)
622 (const :tag
"border first" reversed
)))
624 (if (fboundp 'defvaralias
)
625 (defvaralias 'org-special-ctrl-a
'org-special-ctrl-a
/e
))
627 (defcustom org-special-ctrl-k nil
628 "Non-nil means `C-k' will behave specially in headlines.
629 When nil, `C-k' will call the default `kill-line' command.
630 When t, the following will happen while the cursor is in the headline:
632 - When the cursor is at the beginning of a headline, kill the entire
633 line and possible the folded subtree below the line.
634 - When in the middle of the headline text, kill the headline up to the tags.
635 - When after the headline text, kill the tags."
636 :group
'org-edit-structure
639 (defcustom org-yank-folded-subtrees t
640 "Non-nil means, when yanking subtrees, fold them.
641 If the kill is a single subtree, or a sequence of subtrees, i.e. if
642 it starts with a heading and all other headings in it are either children
643 or siblings, then fold all the subtrees. However, do this only if no
644 text after the yank would be swallowed into a folded tree by this action."
645 :group
'org-edit-structure
648 (defcustom org-yank-adjusted-subtrees t
649 "Non-nil means, when yanking subtrees, adjust the level.
650 With this setting, `org-paste-subtree' is used to insert the subtree, see
651 this function for details."
652 :group
'org-edit-structure
655 (defcustom org-M-RET-may-split-line
'((default . t
))
656 "Non-nil means, M-RET will split the line at the cursor position.
657 When nil, it will go to the end of the line before making a
659 You may also set this option in a different way for different
660 contexts. Valid contexts are:
662 headline when creating a new headline
663 item when creating a new item
664 table in a table field
665 default the value to be used for all contexts not explicitly
667 :group
'org-structure
670 (const :tag
"Always" t
)
671 (const :tag
"Never" nil
)
672 (repeat :greedy t
:tag
"Individual contexts"
674 (choice :tag
"Context"
682 (defcustom org-insert-heading-respect-content nil
683 "Non-nil means, insert new headings after the current subtree.
684 When nil, the new heading is created directly after the current line.
685 The commands \\[org-insert-heading-respect-content] and
686 \\[org-insert-todo-heading-respect-content] turn this variable on
687 for the duration of the command."
688 :group
'org-structure
691 (defcustom org-blank-before-new-entry
'((heading . nil
)
692 (plain-list-item . nil
))
693 "Should `org-insert-heading' leave a blank line before new heading/item?
694 The value is an alist, with `heading' and `plain-list-item' as car,
695 and a boolean flag as cdr."
696 :group
'org-edit-structure
698 (cons (const heading
) (boolean))
699 (cons (const plain-list-item
) (boolean))))
701 (defcustom org-insert-heading-hook nil
702 "Hook being run after inserting a new heading."
703 :group
'org-edit-structure
706 (defcustom org-enable-fixed-width-editor t
707 "Non-nil means, lines starting with \":\" are treated as fixed-width.
708 This currently only means, they are never auto-wrapped.
709 When nil, such lines will be treated like ordinary lines.
710 See also the QUOTE keyword."
711 :group
'org-edit-structure
714 (defcustom org-edit-src-region-extra nil
715 "Additional regexps to identify regions for editing with `org-edit-src-code'.
716 For examples see the function `org-edit-src-find-region-and-lang'.
717 The regular expression identifying the begin marker should end with a newline,
718 and the regexp marking the end line should start with a newline, to make sure
719 there are kept outside the narrowed region."
720 :group
'org-edit-structure
723 (regexp :tag
"begin regexp")
724 (regexp :tag
"end regexp")
725 (choice :tag
"language"
726 (string :tag
"specify")
727 (integer :tag
"from match group")
728 (const :tag
"from `lang' element")
729 (const :tag
"from `style' element")))))
731 (defcustom org-edit-fixed-width-region-mode
'artist-mode
732 "The mode that should be used to edit fixed-width regions.
733 These are the regions where each line starts with a colon."
734 :group
'org-edit-structure
738 (const fundamental-mode
)
739 (function :tag
"Other (specify)")))
741 (defcustom org-goto-auto-isearch t
742 "Non-nil means, typing characters in org-goto starts incremental search."
743 :group
'org-edit-structure
746 (defgroup org-sparse-trees nil
747 "Options concerning sparse trees in Org-mode."
748 :tag
"Org Sparse Trees"
749 :group
'org-structure
)
751 (defcustom org-highlight-sparse-tree-matches t
752 "Non-nil means, highlight all matches that define a sparse tree.
753 The highlights will automatically disappear the next time the buffer is
754 changed by an edit command."
755 :group
'org-sparse-trees
758 (defcustom org-remove-highlights-with-change t
759 "Non-nil means, any change to the buffer will remove temporary highlights.
760 Such highlights are created by `org-occur' and `org-clock-display'.
761 When nil, `C-c C-c needs to be used to get rid of the highlights.
762 The highlights created by `org-preview-latex-fragment' always need
763 `C-c C-c' to be removed."
764 :group
'org-sparse-trees
769 (defcustom org-occur-hook
'(org-first-headline-recenter)
770 "Hook that is run after `org-occur' has constructed a sparse tree.
771 This can be used to recenter the window to show as much of the structure
773 :group
'org-sparse-trees
776 (defgroup org-imenu-and-speedbar nil
777 "Options concerning imenu and speedbar in Org-mode."
778 :tag
"Org Imenu and Speedbar"
779 :group
'org-structure
)
781 (defcustom org-imenu-depth
2
782 "The maximum level for Imenu access to Org-mode headlines.
783 This also applied for speedbar access."
784 :group
'org-imenu-and-speedbar
787 (defgroup org-table nil
788 "Options concerning tables in Org-mode."
792 (defcustom org-enable-table-editor
'optimized
793 "Non-nil means, lines starting with \"|\" are handled by the table editor.
794 When nil, such lines will be treated like ordinary lines.
796 When equal to the symbol `optimized', the table editor will be optimized to
798 - Automatic overwrite mode in front of whitespace in table fields.
799 This makes the structure of the table stay in tact as long as the edited
800 field does not exceed the column width.
801 - Minimize the number of realigns. Normally, the table is aligned each time
802 TAB or RET are pressed to move to another field. With optimization this
803 happens only if changes to a field might have changed the column width.
804 Optimization requires replacing the functions `self-insert-command',
805 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
806 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
807 very good at guessing when a re-align will be necessary, but you can always
808 force one with \\[org-ctrl-c-ctrl-c].
810 If you would like to use the optimized version in Org-mode, but the
811 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
813 This variable can be used to turn on and off the table editor during a session,
814 but in order to toggle optimization, a restart is required.
816 See also the variable `org-table-auto-blank-field'."
819 (const :tag
"off" nil
)
821 (const :tag
"on, optimized" optimized
)))
823 (defcustom org-table-tab-recognizes-table.el t
824 "Non-nil means, TAB will automatically notice a table.el table.
825 When it sees such a table, it moves point into it and - if necessary -
826 calls `table-recognize-table'."
827 :group
'org-table-editing
830 (defgroup org-link nil
831 "Options concerning links in Org-mode."
835 (defvar org-link-abbrev-alist-local nil
836 "Buffer-local version of `org-link-abbrev-alist', which see.
837 The value of this is taken from the #+LINK lines.")
838 (make-variable-buffer-local 'org-link-abbrev-alist-local
)
840 (defcustom org-link-abbrev-alist nil
841 "Alist of link abbreviations.
842 The car of each element is a string, to be replaced at the start of a link.
843 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
844 links in Org-mode buffers can have an optional tag after a double colon, e.g.
846 [[linkkey:tag][description]]
848 If REPLACE is a string, the tag will simply be appended to create the link.
849 If the string contains \"%s\", the tag will be inserted there. Alternatively,
850 the placeholder \"%h\" will cause a url-encoded version of the tag to
851 be inserted at that point (see the function `url-hexify-string').
853 REPLACE may also be a function that will be called with the tag as the
854 only argument to create the link, which should be returned as a string.
856 See the manual for examples."
860 (string :tag
"Protocol")
862 (string :tag
"Format")
865 (defcustom org-descriptive-links t
866 "Non-nil means, hide link part and only show description of bracket links.
867 Bracket links are like [[link][description]]. This variable sets the initial
868 state in new org-mode buffers. The setting can then be toggled on a
869 per-buffer basis from the Org->Hyperlinks menu."
873 (defcustom org-link-file-path-type
'adaptive
874 "How the path name in file links should be stored.
877 relative Relative to the current directory, i.e. the directory of the file
878 into which the link is being inserted.
879 absolute Absolute path, if possible with ~ for home directory.
880 noabbrev Absolute path, no abbreviation of home directory.
881 adaptive Use relative path for files in the current directory and sub-
882 directories of it. For other files, use an absolute path."
890 (defcustom org-activate-links
'(bracket angle plain radio tag date
)
891 "Types of links that should be activated in Org-mode files.
892 This is a list of symbols, each leading to the activation of a certain link
893 type. In principle, it does not hurt to turn on most link types - there may
894 be a small gain when turning off unused link types. The types are:
896 bracket The recommended [[link][description]] or [[link]] links with hiding.
897 angular Links in angular brackets that may contain whitespace like
898 <bbdb:Carsten Dominik>.
899 plain Plain links in normal text, no whitespace, like http://google.com.
900 radio Text that is matched by a radio target, see manual for details.
901 tag Tag settings in a headline (link to tag search).
902 date Time stamps (link to calendar).
904 Changing this variable requires a restart of Emacs to become effective."
906 :type
'(set (const :tag
"Double bracket links (new style)" bracket
)
907 (const :tag
"Angular bracket links (old style)" angular
)
908 (const :tag
"Plain text links" plain
)
909 (const :tag
"Radio target matches" radio
)
910 (const :tag
"Tags" tag
)
911 (const :tag
"Timestamps" date
)))
913 (defcustom org-make-link-description-function nil
914 "Function to use to generate link descriptions from links. If
915 nil the link location will be used. This function must take two
916 parameters; the first is the link and the second the description
917 org-insert-link has generated, and should return the description
922 (defgroup org-link-store nil
923 "Options concerning storing links in Org-mode."
924 :tag
"Org Store Link"
927 (defcustom org-email-link-description-format
"Email %c: %.30s"
928 "Format of the description part of a link to an email or usenet message.
929 The following %-escapes will be replaced by corresponding information:
931 %F full \"From\" field
932 %f name, taken from \"From\" field, address if no name
934 %t first name in \"To\" field, address if no name
935 %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
936 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
940 You may use normal field width specification between the % and the letter.
941 This is for example useful to limit the length of the subject.
943 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
944 :group
'org-link-store
947 (defcustom org-from-is-user-regexp
949 (when (and user-mail-address
(not (string= user-mail-address
"")))
950 (setq r1
(concat "\\<" (regexp-quote user-mail-address
) "\\>")))
951 (when (and user-full-name
(not (string= user-full-name
"")))
952 (setq r2
(concat "\\<" (regexp-quote user-full-name
) "\\>")))
953 (if (and r1 r2
) (concat r1
"\\|" r2
) (or r1 r2
)))
954 "Regexp matched against the \"From:\" header of an email or usenet message.
955 It should match if the message is from the user him/herself."
956 :group
'org-link-store
959 (defcustom org-link-to-org-use-id
'create-if-interactive
960 "Non-nil means, storing a link to an Org file will use entry IDs.
962 Note that before this variable is even considered, org-id must be loaded,
963 to please customize `org-modules' and turn it on.
965 The variable can have the following values:
967 t Create an ID if needed to make a link to the current entry.
969 create-if-interactive
970 If `org-store-link' is called directly (interactively, as a user
971 command), do create an ID to support the link. But when doing the
972 job for remember, only use the ID if it already exists. The
973 purpose of this setting is to avoid proliferation of unwanted
974 IDs, just because you happen to be in an Org file when you
975 call `org-remember' that automatically and preemptively
976 creates a link. If you do want to get an ID link in a remember
977 template to an entry not having an ID, create it first by
978 explicitly creating a link to it, using `C-c C-l' first.
981 Use existing ID, do not create one.
983 nil Never use an ID to make a link, instead link using a text search for
985 :group
'org-link-store
987 (const :tag
"Create ID to make link" t
)
988 (const :tag
"Create if string link interactively"
989 'create-if-interactive
)
990 (const :tag
"Only use existing" 'use-existing
)
991 (const :tag
"Do not use ID to create link" nil
)))
993 (defcustom org-context-in-file-links t
994 "Non-nil means, file links from `org-store-link' contain context.
995 A search string will be added to the file name with :: as separator and
996 used to find the context when the link is activated by the command
998 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
999 negates this setting for the duration of the command."
1000 :group
'org-link-store
1003 (defcustom org-keep-stored-link-after-insertion nil
1004 "Non-nil means, keep link in list for entire session.
1006 The command `org-store-link' adds a link pointing to the current
1007 location to an internal list. These links accumulate during a session.
1008 The command `org-insert-link' can be used to insert links into any
1009 Org-mode file (offering completion for all stored links). When this
1010 option is nil, every link which has been inserted once using \\[org-insert-link]
1011 will be removed from the list, to make completing the unused links
1013 :group
'org-link-store
1016 (defgroup org-link-follow nil
1017 "Options concerning following links in Org-mode."
1018 :tag
"Org Follow Link"
1021 (defcustom org-link-translation-function nil
1022 "Function to translate links with different syntax to Org syntax.
1023 This can be used to translate links created for example by the Planner
1024 or emacs-wiki packages to Org syntax.
1025 The function must accept two parameters, a TYPE containing the link
1026 protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
1027 which is everything after the link protocol. It should return a cons
1028 with possibly modified values of type and path.
1029 Org contains a function for this, so if you set this variable to
1030 `org-translate-link-from-planner', you should be able follow many
1031 links created by planner."
1032 :group
'org-link-follow
1035 (defcustom org-follow-link-hook nil
1036 "Hook that is run after a link has been followed."
1037 :group
'org-link-follow
1040 (defcustom org-tab-follows-link nil
1041 "Non-nil means, on links TAB will follow the link.
1042 Needs to be set before org.el is loaded."
1043 :group
'org-link-follow
1046 (defcustom org-return-follows-link nil
1047 "Non-nil means, on links RET will follow the link.
1048 Needs to be set before org.el is loaded."
1049 :group
'org-link-follow
1052 (defcustom org-mouse-1-follows-link
1053 (if (boundp 'mouse-1-click-follows-link
) mouse-1-click-follows-link t
)
1054 "Non-nil means, mouse-1 on a link will follow the link.
1055 A longer mouse click will still set point. Does not work on XEmacs.
1056 Needs to be set before org.el is loaded."
1057 :group
'org-link-follow
1060 (defcustom org-mark-ring-length
4
1061 "Number of different positions to be recorded in the ring
1062 Changing this requires a restart of Emacs to work correctly."
1063 :group
'org-link-follow
1066 (defcustom org-link-frame-setup
1067 '((vm . vm-visit-folder-other-frame
)
1068 (gnus . gnus-other-frame
)
1069 (file . find-file-other-window
))
1070 "Setup the frame configuration for following links.
1071 When following a link with Emacs, it may often be useful to display
1072 this link in another window or frame. This variable can be used to
1073 set this up for the different types of links.
1076 `vm-visit-folder-other-frame'
1077 For Gnus, use any of
1080 `org-gnus-no-new-news'
1081 For FILE, use any of
1083 `find-file-other-window'
1084 `find-file-other-frame'
1085 For the calendar, use the variable `calendar-setup'.
1086 For BBDB, it is currently only possible to display the matches in
1088 :group
'org-link-follow
1092 (const vm-visit-folder
)
1093 (const vm-visit-folder-other-window
)
1094 (const vm-visit-folder-other-frame
)))
1098 (const gnus-other-frame
)
1099 (const org-gnus-no-new-news
)))
1103 (const find-file-other-window
)
1104 (const find-file-other-frame
)))))
1106 (defcustom org-display-internal-link-with-indirect-buffer nil
1107 "Non-nil means, use indirect buffer to display infile links.
1108 Activating internal links (from one location in a file to another location
1109 in the same file) normally just jumps to the location. When the link is
1110 activated with a C-u prefix (or with mouse-3), the link is displayed in
1111 another window. When this option is set, the other window actually displays
1112 an indirect buffer clone of the current buffer, to avoid any visibility
1113 changes to the current buffer."
1114 :group
'org-link-follow
1117 (defcustom org-open-non-existing-files nil
1118 "Non-nil means, `org-open-file' will open non-existing files.
1119 When nil, an error will be generated."
1120 :group
'org-link-follow
1123 (defcustom org-open-directory-means-index-dot-org nil
1124 "Non-nil means, a link to a directory really means to index.org.
1125 When nil, following a directory link will run dired or open a finder/explorer
1126 window on that directory."
1127 :group
'org-link-follow
1130 (defcustom org-link-mailto-program
'(browse-url "mailto:%a?subject=%s")
1131 "Function and arguments to call for following mailto links.
1132 This is a list with the first element being a lisp function, and the
1133 remaining elements being arguments to the function. In string arguments,
1134 %a will be replaced by the address, and %s will be replaced by the subject
1135 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1136 :group
'org-link-follow
1138 (const :tag
"browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1139 (const :tag
"compose-mail" (compose-mail "%a" "%s"))
1140 (const :tag
"message-mail" (message-mail "%a" "%s"))
1141 (cons :tag
"other" (function) (repeat :tag
"argument" sexp
))))
1143 (defcustom org-confirm-shell-link-function
'yes-or-no-p
1144 "Non-nil means, ask for confirmation before executing shell links.
1145 Shell links can be dangerous: just think about a link
1147 [[shell:rm -rf ~/*][Google Search]]
1149 This link would show up in your Org-mode document as \"Google Search\",
1150 but really it would remove your entire home directory.
1151 Therefore we advise against setting this variable to nil.
1152 Just change it to `y-or-n-p' of you want to confirm with a
1153 single keystroke rather than having to type \"yes\"."
1154 :group
'org-link-follow
1156 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1157 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1158 (const :tag
"no confirmation (dangerous)" nil
)))
1160 (defcustom org-confirm-elisp-link-function
'yes-or-no-p
1161 "Non-nil means, ask for confirmation before executing Emacs Lisp links.
1162 Elisp links can be dangerous: just think about a link
1164 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1166 This link would show up in your Org-mode document as \"Google Search\",
1167 but really it would remove your entire home directory.
1168 Therefore we advise against setting this variable to nil.
1169 Just change it to `y-or-n-p' of you want to confirm with a
1170 single keystroke rather than having to type \"yes\"."
1171 :group
'org-link-follow
1173 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1174 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1175 (const :tag
"no confirmation (dangerous)" nil
)))
1177 (defconst org-file-apps-defaults-gnu
1181 "Default file applications on a UNIX or GNU/Linux system.
1182 See `org-file-apps'.")
1184 (defconst org-file-apps-defaults-macosx
1187 (system .
"open %s")
1189 ("eps.gz" .
"gv %s")
1191 ("fig" .
"xfig %s"))
1192 "Default file applications on a MacOS X system.
1193 The system \"open\" is known as a default, but we use X11 applications
1194 for some files for which the OS does not have a good default.
1195 See `org-file-apps'.")
1197 (defconst org-file-apps-defaults-windowsnt
1201 (list (if (featurep 'xemacs
)
1202 'mswindows-shell-execute
1206 (list (if (featurep 'xemacs
)
1207 'mswindows-shell-execute
1210 "Default file applications on a Windows NT system.
1211 The system \"open\" is used for most files.
1212 See `org-file-apps'.")
1214 (defcustom org-file-apps
1217 ("\\.x?html?\\'" . default
)
1218 ("\\.pdf\\'" . default
)
1220 "External applications for opening `file:path' items in a document.
1221 Org-mode uses system defaults for different file types, but
1222 you can use this variable to set the application for a given file
1223 extension. The entries in this list are cons cells where the car identifies
1224 files and the cdr the corresponding command. Possible values for the
1226 \"regex\" Regular expression matched against the file name. For backward
1227 compatibility, this can also be a string with only alphanumeric
1228 characters, which is then interpreted as an extension.
1229 `directory' Matches a directory
1230 `remote' Matches a remote file, accessible through tramp or efs.
1231 Remote files most likely should be visited through Emacs
1232 because external applications cannot handle such paths.
1233 `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
1234 so all files Emacs knows how to handle. Using this with
1235 command `emacs' will open most files in Emacs. Beware that this
1236 will also open html files inside Emacs, unless you add
1237 (\"html\" . default) to the list as well.
1238 t Default for files not matched by any of the other options.
1239 `system' The system command to open files, like `open' on Windows
1240 and Mac OS X, and mailcap under GNU/Linux. This is the command
1241 that will be selected if you call `C-c C-o' with a double
1244 Possible values for the command are:
1245 `emacs' The file will be visited by the current Emacs process.
1246 `default' Use the default application for this file type, which is the
1247 association for t in the list, most likely in the system-specific
1249 This can be used to overrule an unwanted setting in the
1250 system-specific variable.
1251 `system' Use the system command for opening files, like \"open\".
1252 This command is specified by the entry whose car is `system'.
1253 Most likely, the system-specific version of this variable
1254 does define this command, but you can overrule/replace it
1256 string A command to be executed by a shell; %s will be replaced
1257 by the path to the file.
1258 sexp A Lisp form which will be evaluated. The file path will
1259 be available in the Lisp variable `file'.
1260 For more examples, see the system specific constants
1261 `org-file-apps-defaults-macosx'
1262 `org-file-apps-defaults-windowsnt'
1263 `org-file-apps-defaults-gnu'."
1264 :group
'org-link-follow
1266 (cons (choice :value
""
1267 (string :tag
"Extension")
1268 (const :tag
"System command to open files" system
)
1269 (const :tag
"Default for unrecognized files" t
)
1270 (const :tag
"Remote file" remote
)
1271 (const :tag
"Links to a directory" directory
)
1272 (const :tag
"Any files that have Emacs modes"
1275 (const :tag
"Visit with Emacs" emacs
)
1276 (const :tag
"Use default" default
)
1277 (const :tag
"Use the system command" system
)
1278 (string :tag
"Command")
1279 (sexp :tag
"Lisp form")))))
1281 (defgroup org-refile nil
1282 "Options concerning refiling entries in Org-mode."
1286 (defcustom org-directory
"~/org"
1287 "Directory with org files.
1288 This directory will be used as default to prompt for org files.
1289 Used by the hooks for remember.el."
1291 :group
'org-remember
1294 (defcustom org-default-notes-file
(convert-standard-filename "~/.notes")
1295 "Default target for storing notes.
1296 Used by the hooks for remember.el. This can be a string, or nil to mean
1297 the value of `remember-data-file'.
1298 You can set this on a per-template basis with the variable
1299 `org-remember-templates'."
1301 :group
'org-remember
1303 (const :tag
"Default from remember-data-file" nil
)
1306 (defcustom org-goto-interface
'outline
1307 "The default interface to be used for `org-goto'.
1309 outline The interface shows an outline of the relevant file
1310 and the correct heading is found by moving through
1311 the outline or by searching with incremental search.
1312 outline-path-completion Headlines in the current buffer are offered via
1313 completion. This is the interface also used by
1314 the refile command."
1317 (const :tag
"Outline" outline
)
1318 (const :tag
"Outline-path-completion" outline-path-completion
)))
1320 (defcustom org-goto-max-level
5
1321 "Maximum level to be considered when running org-goto with refile interface."
1325 (defcustom org-reverse-note-order nil
1326 "Non-nil means, store new notes at the beginning of a file or entry.
1327 When nil, new notes will be filed to the end of a file or entry.
1328 This can also be a list with cons cells of regular expressions that
1329 are matched against file names, and values."
1330 :group
'org-remember
1333 (const :tag
"Reverse always" t
)
1334 (const :tag
"Reverse never" nil
)
1335 (repeat :tag
"By file name regexp"
1336 (cons regexp boolean
))))
1338 (defcustom org-refile-targets nil
1339 "Targets for refiling entries with \\[org-refile].
1340 This is list of cons cells. Each cell contains:
1341 - a specification of the files to be considered, either a list of files,
1342 or a symbol whose function or variable value will be used to retrieve
1343 a file name or a list of file names. If you use `org-agenda-files' for
1344 that, all agenda files will be scanned for targets. Nil means, consider
1345 headings in the current buffer.
1346 - A specification of how to select find candidate refile targets. This
1348 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
1349 This tag has to be present in all target headlines, inheritance will
1351 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
1353 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
1354 headlines that are refiling targets.
1355 - a cons cell (:level . N). Any headline of level N is considered a target.
1356 - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
1358 When this variable is nil, all top-level headlines in the current buffer
1359 are used, equivalent to the value `((nil . (:level . 1))'."
1363 (choice :value org-agenda-files
1364 (const :tag
"All agenda files" org-agenda-files
)
1365 (const :tag
"Current buffer" nil
)
1366 (function) (variable) (file))
1367 (choice :tag
"Identify target headline by"
1368 (cons :tag
"Specific tag" (const :value
:tag
) (string))
1369 (cons :tag
"TODO keyword" (const :value
:todo
) (string))
1370 (cons :tag
"Regular expression" (const :value
:regexp
) (regexp))
1371 (cons :tag
"Level number" (const :value
:level
) (integer))
1372 (cons :tag
"Max Level number" (const :value
:maxlevel
) (integer))))))
1374 (defcustom org-refile-use-outline-path nil
1375 "Non-nil means, provide refile targets as paths.
1376 So a level 3 headline will be available as level1/level2/level3.
1377 When the value is `file', also include the file name (without directory)
1378 into the path. When `full-file-path', include the full file path."
1381 (const :tag
"Not" nil
)
1382 (const :tag
"Yes" t
)
1383 (const :tag
"Start with file name" file
)
1384 (const :tag
"Start with full file path" full-file-path
)))
1386 (defcustom org-outline-path-complete-in-steps t
1387 "Non-nil means, complete the outline path in hierarchical steps.
1388 When Org-mode uses the refile interface to select an outline path
1389 \(see variable `org-refile-use-outline-path'), the completion of
1390 the path can be done is a single go, or if can be done in steps down
1391 the headline hierarchy. Going in steps is probably the best if you
1392 do not use a special completion package like `ido' or `icicles'.
1393 However, when using these packages, going in one step can be very
1394 fast, while still showing the whole path to the entry."
1398 (defgroup org-todo nil
1399 "Options concerning TODO items in Org-mode."
1403 (defgroup org-progress nil
1404 "Options concerning Progress logging in Org-mode."
1408 (defcustom org-todo-keywords
'((sequence "TODO" "DONE"))
1409 "List of TODO entry keyword sequences and their interpretation.
1410 \\<org-mode-map>This is a list of sequences.
1412 Each sequence starts with a symbol, either `sequence' or `type',
1413 indicating if the keywords should be interpreted as a sequence of
1414 action steps, or as different types of TODO items. The first
1415 keywords are states requiring action - these states will select a headline
1416 for inclusion into the global TODO list Org-mode produces. If one of
1417 the \"keywords\" is the vertical bat \"|\" the remaining keywords
1418 signify that no further action is necessary. If \"|\" is not found,
1419 the last keyword is treated as the only DONE state of the sequence.
1421 The command \\[org-todo] cycles an entry through these states, and one
1422 additional state where no keyword is present. For details about this
1423 cycling, see the manual.
1425 TODO keywords and interpretation can also be set on a per-file basis with
1426 the special #+SEQ_TODO and #+TYP_TODO lines.
1428 Each keyword can optionally specify a character for fast state selection
1429 \(in combination with the variable `org-use-fast-todo-selection')
1430 and specifiers for state change logging, using the same syntax
1431 that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
1432 that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
1433 indicates to record a time stamp each time this state is selected.
1435 Each keyword may also specify if a timestamp or a note should be
1436 recorded when entering or leaving the state, by adding additional
1437 characters in the parenthesis after the keyword. This looks like this:
1438 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
1439 record only the time of the state change. With X and Y being either
1440 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
1441 Y when leaving the state if and only if the *target* state does not
1442 define X. You may omit any of the fast-selection key or X or /Y,
1443 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
1445 For backward compatibility, this variable may also be just a list
1446 of keywords - in this case the interpretation (sequence or type) will be
1447 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
1449 :group
'org-keywords
1451 (repeat :tag
"Old syntax, just keywords"
1452 (string :tag
"Keyword"))
1453 (repeat :tag
"New syntax"
1456 :tag
"Interpretation"
1457 (const :tag
"Sequence (cycling hits every state)" sequence
)
1458 (const :tag
"Type (cycling directly to DONE)" type
))
1460 (string :tag
"Keyword"))))))
1462 (defvar org-todo-keywords-1 nil
1463 "All TODO and DONE keywords active in a buffer.")
1464 (make-variable-buffer-local 'org-todo-keywords-1
)
1465 (defvar org-todo-keywords-for-agenda nil
)
1466 (defvar org-done-keywords-for-agenda nil
)
1467 (defvar org-todo-keyword-alist-for-agenda nil
)
1468 (defvar org-tag-alist-for-agenda nil
)
1469 (defvar org-agenda-contributing-files nil
)
1470 (defvar org-not-done-keywords nil
)
1471 (make-variable-buffer-local 'org-not-done-keywords
)
1472 (defvar org-done-keywords nil
)
1473 (make-variable-buffer-local 'org-done-keywords
)
1474 (defvar org-todo-heads nil
)
1475 (make-variable-buffer-local 'org-todo-heads
)
1476 (defvar org-todo-sets nil
)
1477 (make-variable-buffer-local 'org-todo-sets
)
1478 (defvar org-todo-log-states nil
)
1479 (make-variable-buffer-local 'org-todo-log-states
)
1480 (defvar org-todo-kwd-alist nil
)
1481 (make-variable-buffer-local 'org-todo-kwd-alist
)
1482 (defvar org-todo-key-alist nil
)
1483 (make-variable-buffer-local 'org-todo-key-alist
)
1484 (defvar org-todo-key-trigger nil
)
1485 (make-variable-buffer-local 'org-todo-key-trigger
)
1487 (defcustom org-todo-interpretation
'sequence
1488 "Controls how TODO keywords are interpreted.
1489 This variable is in principle obsolete and is only used for
1490 backward compatibility, if the interpretation of todo keywords is
1491 not given already in `org-todo-keywords'. See that variable for
1494 :group
'org-keywords
1495 :type
'(choice (const sequence
)
1498 (defcustom org-use-fast-todo-selection
'prefix
1499 "Non-nil means, use the fast todo selection scheme with C-c C-t.
1500 This variable describes if and under what circumstances the cycling
1501 mechanism for TODO keywords will be replaced by a single-key, direct
1504 When nil, fast selection is never used.
1506 When the symbol `prefix', it will be used when `org-todo' is called with
1507 a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
1508 in an agenda buffer.
1510 When t, fast selection is used by default. In this case, the prefix
1511 argument forces cycling instead.
1513 In all cases, the special interface is only used if access keys have actually
1514 been assigned by the user, i.e. if keywords in the configuration are followed
1515 by a letter in parenthesis, like TODO(t)."
1518 (const :tag
"Never" nil
)
1519 (const :tag
"By default" t
)
1520 (const :tag
"Only with C-u C-c C-t" prefix
)))
1522 (defcustom org-provide-todo-statistics t
1523 "Non-nil means, update todo statistics after insert and toggle.
1524 When this is set, todo statistics is updated in the parent of the current
1525 entry each time a todo state is changed."
1529 (defcustom org-after-todo-state-change-hook nil
1530 "Hook which is run after the state of a TODO item was changed.
1531 The new state (a string with a TODO keyword, or nil) is available in the
1532 Lisp variable `state'."
1536 (defcustom org-todo-state-tags-triggers nil
1537 "Tag changes that should be triggered by TODO state changes.
1538 This is a list. Each entry is
1540 (state-change (tag . flag) .......)
1542 State-change can be a string with a state, and empty string to indicate the
1543 state that has no TODO keyword, or it can be one of the symbols `todo'
1544 or `done', meaning any not-done or done state, respectively."
1548 (cons (choice :tag
"When changing to"
1549 (const :tag
"Not-done state" todo
)
1550 (const :tag
"Done state" done
)
1551 (string :tag
"State"))
1553 (cons :tag
"Tag action"
1555 (choice (const :tag
"Add" t
) (const :tag
"Remove" nil
)))))))
1557 (defcustom org-log-done nil
1558 "Information to record when a task moves to the DONE state.
1560 Possible values are:
1562 nil Don't add anything, just change the keyword
1563 time Add a time stamp to the task
1564 note Prompt a closing note and add it with template `org-log-note-headings'
1566 This option can also be set with on a per-file-basis with
1568 #+STARTUP: nologdone
1570 #+STARTUP: lognotedone
1572 You can have local logging settings for a subtree by setting the LOGGING
1573 property to one or more of these keywords."
1575 :group
'org-progress
1577 (const :tag
"No logging" nil
)
1578 (const :tag
"Record CLOSED timestamp" time
)
1579 (const :tag
"Record CLOSED timestamp with closing note." note
)))
1581 ;; Normalize old uses of org-log-done.
1583 ((eq org-log-done t
) (setq org-log-done
'time
))
1584 ((and (listp org-log-done
) (memq 'done org-log-done
))
1585 (setq org-log-done
'note
)))
1587 (defcustom org-log-note-clock-out nil
1588 "Non-nil means, record a note when clocking out of an item.
1589 This can also be configured on a per-file basis by adding one of
1590 the following lines anywhere in the buffer:
1592 #+STARTUP: lognoteclock-out
1593 #+STARTUP: nolognoteclock-out"
1595 :group
'org-progress
1598 (defcustom org-log-done-with-time t
1599 "Non-nil means, the CLOSED time stamp will contain date and time.
1600 When nil, only the date will be recorded."
1601 :group
'org-progress
1604 (defcustom org-log-note-headings
1605 '((done .
"CLOSING NOTE %t")
1606 (state .
"State %-12s %t")
1607 (note .
"Note taken on %t")
1609 "Headings for notes added to entries.
1610 The value is an alist, with the car being a symbol indicating the note
1611 context, and the cdr is the heading to be used. The heading may also be the
1613 %t in the heading will be replaced by a time stamp.
1614 %s will be replaced by the new TODO state, in double quotes.
1615 %u will be replaced by the user name.
1616 %U will be replaced by the full user name."
1618 :group
'org-progress
1619 :type
'(list :greedy t
1620 (cons (const :tag
"Heading when closing an item" done
) string
)
1622 "Heading when changing todo state (todo sequence only)"
1624 (cons (const :tag
"Heading when just taking a note" note
) string
)
1625 (cons (const :tag
"Heading when clocking out" clock-out
) string
)))
1627 (unless (assq 'note org-log-note-headings
)
1628 (push '(note .
"%t") org-log-note-headings
))
1630 (defcustom org-log-state-notes-insert-after-drawers nil
1631 "Non-nil means, insert state change notes after any drawers in entry.
1632 Only the drawers that *immediately* follow the headline and the
1633 deadline/scheduled line are skipped.
1634 When nil, insert notes right after the heading and perhaps the line
1635 with deadline/scheduling if present."
1637 :group
'org-progress
1640 (defcustom org-log-states-order-reversed t
1641 "Non-nil means, the latest state change note will be directly after heading.
1642 When nil, the notes will be orderer according to time."
1644 :group
'org-progress
1647 (defcustom org-log-repeat
'time
1648 "Non-nil means, record moving through the DONE state when triggering repeat.
1649 An auto-repeating tasks is immediately switched back to TODO when marked
1650 done. If you are not logging state changes (by adding \"@\" or \"!\" to
1651 the TODO keyword definition, or recording a closing note by setting
1652 `org-log-done', there will be no record of the task moving through DONE.
1653 This variable forces taking a note anyway. Possible values are:
1655 nil Don't force a record
1656 time Record a time stamp
1659 This option can also be set with on a per-file-basis with
1661 #+STARTUP: logrepeat
1662 #+STARTUP: lognoterepeat
1663 #+STARTUP: nologrepeat
1665 You can have local logging settings for a subtree by setting the LOGGING
1666 property to one or more of these keywords."
1668 :group
'org-progress
1670 (const :tag
"Don't force a record" nil
)
1671 (const :tag
"Force recording the DONE state" time
)
1672 (const :tag
"Force recording a note with the DONE state" note
)))
1675 (defgroup org-priorities nil
1676 "Priorities in Org-mode."
1677 :tag
"Org Priorities"
1680 (defcustom org-highest-priority ?A
1681 "The highest priority of TODO items. A character like ?A, ?B etc.
1682 Must have a smaller ASCII number than `org-lowest-priority'."
1683 :group
'org-priorities
1686 (defcustom org-lowest-priority ?C
1687 "The lowest priority of TODO items. A character like ?A, ?B etc.
1688 Must have a larger ASCII number than `org-highest-priority'."
1689 :group
'org-priorities
1692 (defcustom org-default-priority ?B
1693 "The default priority of TODO items.
1694 This is the priority an item get if no explicit priority is given."
1695 :group
'org-priorities
1698 (defcustom org-priority-start-cycle-with-default t
1699 "Non-nil means, start with default priority when starting to cycle.
1700 When this is nil, the first step in the cycle will be (depending on the
1701 command used) one higher or lower that the default priority."
1702 :group
'org-priorities
1705 (defgroup org-time nil
1706 "Options concerning time stamps and deadlines in Org-mode."
1710 (defcustom org-insert-labeled-timestamps-at-point nil
1711 "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
1712 When nil, these labeled time stamps are forces into the second line of an
1713 entry, just after the headline. When scheduling from the global TODO list,
1714 the time stamp will always be forced into the second line."
1718 (defconst org-time-stamp-formats
'("<%Y-%m-%d %a>" .
"<%Y-%m-%d %a %H:%M>")
1719 "Formats for `format-time-string' which are used for time stamps.
1720 It is not recommended to change this constant.")
1722 (defcustom org-time-stamp-rounding-minutes
'(0 5)
1723 "Number of minutes to round time stamps to.
1724 These are two values, the first applies when first creating a time stamp.
1725 The second applies when changing it with the commands `S-up' and `S-down'.
1726 When changing the time stamp, this means that it will change in steps
1727 of N minutes, as given by the second value.
1729 When a setting is 0 or 1, insert the time unmodified. Useful rounding
1730 numbers should be factors of 60, so for example 5, 10, 15.
1732 When this is larger than 1, you can still force an exact time-stamp by using
1733 a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
1734 and by using a prefix arg to `S-up/down' to specify the exact number
1735 of minutes to shift."
1737 :get
'(lambda (var) ; Make sure all entries have 5 elements
1738 (if (integerp (default-value var
))
1739 (list (default-value var
) 5)
1740 (default-value var
)))
1742 (integer :tag
"when inserting times")
1743 (integer :tag
"when modifying times")))
1745 ;; Normalize old customizations of this variable.
1746 (when (integerp org-time-stamp-rounding-minutes
)
1747 (setq org-time-stamp-rounding-minutes
1748 (list org-time-stamp-rounding-minutes
1749 org-time-stamp-rounding-minutes
)))
1751 (defcustom org-display-custom-times nil
1752 "Non-nil means, overlay custom formats over all time stamps.
1753 The formats are defined through the variable `org-time-stamp-custom-formats'.
1754 To turn this on on a per-file basis, insert anywhere in the file:
1755 #+STARTUP: customtime"
1759 (make-variable-buffer-local 'org-display-custom-times
)
1761 (defcustom org-time-stamp-custom-formats
1762 '("<%m/%d/%y %a>" .
"<%m/%d/%y %a %H:%M>") ; american
1763 "Custom formats for time stamps. See `format-time-string' for the syntax.
1764 These are overlayed over the default ISO format if the variable
1765 `org-display-custom-times' is set. Time like %H:%M should be at the
1766 end of the second format."
1770 (defun org-time-stamp-format (&optional long inactive
)
1771 "Get the right format for a time string."
1772 (let ((f (if long
(cdr org-time-stamp-formats
)
1773 (car org-time-stamp-formats
))))
1775 (concat "[" (substring f
1 -
1) "]")
1778 (defcustom org-time-clocksum-format
"%d:%02d"
1779 "The format string used when creating CLOCKSUM lines, or when
1780 org-mode generates a time duration."
1784 (defcustom org-deadline-warning-days
14
1785 "No. of days before expiration during which a deadline becomes active.
1786 This variable governs the display in sparse trees and in the agenda.
1787 When 0 or negative, it means use this number (the absolute value of it)
1788 even if a deadline has a different individual lead time specified."
1790 :group
'org-agenda-daily
/weekly
1793 (defcustom org-read-date-prefer-future t
1794 "Non-nil means, assume future for incomplete date input from user.
1795 This affects the following situations:
1796 1. The user gives a day, but no month.
1797 For example, if today is the 15th, and you enter \"3\", Org-mode will
1798 read this as the third of *next* month. However, if you enter \"17\",
1799 it will be considered as *this* month.
1800 2. The user gives a month but not a year.
1801 For example, if it is april and you enter \"feb 2\", this will be read
1802 as feb 2, *next* year. \"May 5\", however, will be this year.
1804 Currently this does not work for ISO week specifications.
1806 When this option is nil, the current month and year will always be used
1811 (defcustom org-read-date-display-live t
1812 "Non-nil means, display current interpretation of date prompt live.
1813 This display will be in an overlay, in the minibuffer."
1817 (defcustom org-read-date-popup-calendar t
1818 "Non-nil means, pop up a calendar when prompting for a date.
1819 In the calendar, the date can be selected with mouse-1. However, the
1820 minibuffer will also be active, and you can simply enter the date as well.
1821 When nil, only the minibuffer will be available."
1824 (if (fboundp 'defvaralias
)
1825 (defvaralias 'org-popup-calendar-for-date-prompt
1826 'org-read-date-popup-calendar
))
1828 (defcustom org-extend-today-until
0
1829 "The hour when your day really ends. Must be an integer.
1830 This has influence for the following applications:
1831 - When switching the agenda to \"today\". It it is still earlier than
1832 the time given here, the day recognized as TODAY is actually yesterday.
1833 - When a date is read from the user and it is still before the time given
1834 here, the current date and time will be assumed to be yesterday, 23:59.
1835 Also, timestamps inserted in remember templates follow this rule.
1837 IMPORTANT: This is a feature whose implementation is and likely will
1838 remain incomplete. Really, it is only here because past midnight seems to
1839 be the favorite working time of John Wiegley :-)"
1843 (defcustom org-edit-timestamp-down-means-later nil
1844 "Non-nil means, S-down will increase the time in a time stamp.
1845 When nil, S-up will increase."
1849 (defcustom org-calendar-follow-timestamp-change t
1850 "Non-nil means, make the calendar window follow timestamp changes.
1851 When a timestamp is modified and the calendar window is visible, it will be
1852 moved to the new date."
1856 (defgroup org-tags nil
1857 "Options concerning tags in Org-mode."
1861 (defcustom org-tag-alist nil
1862 "List of tags allowed in Org-mode files.
1863 When this list is nil, Org-mode will base TAG input on what is already in the
1865 The value of this variable is an alist, the car of each entry must be a
1866 keyword as a string, the cdr may be a character that is used to select
1867 that tag through the fast-tag-selection interface.
1868 See the manual for details."
1872 (cons (string :tag
"Tag name")
1873 (character :tag
"Access char"))
1874 (const :tag
"Start radio group" (:startgroup
))
1875 (const :tag
"End radio group" (:endgroup
)))))
1877 (defvar org-file-tags nil
1878 "List of tags that can be inherited by all entries in the file.
1879 The tags will be inherited if the variable `org-use-tag-inheritance'
1880 says they should be.
1881 This variable is populated from #+TAG lines.")
1883 (defcustom org-use-fast-tag-selection
'auto
1884 "Non-nil means, use fast tag selection scheme.
1885 This is a special interface to select and deselect tags with single keys.
1886 When nil, fast selection is never used.
1887 When the symbol `auto', fast selection is used if and only if selection
1888 characters for tags have been configured, either through the variable
1889 `org-tag-alist' or through a #+TAGS line in the buffer.
1890 When t, fast selection is always used and selection keys are assigned
1891 automatically if necessary."
1894 (const :tag
"Always" t
)
1895 (const :tag
"Never" nil
)
1896 (const :tag
"When selection characters are configured" 'auto
)))
1898 (defcustom org-fast-tag-selection-single-key nil
1899 "Non-nil means, fast tag selection exits after first change.
1900 When nil, you have to press RET to exit it.
1901 During fast tag selection, you can toggle this flag with `C-c'.
1902 This variable can also have the value `expert'. In this case, the window
1903 displaying the tags menu is not even shown, until you press C-c again."
1906 (const :tag
"No" nil
)
1907 (const :tag
"Yes" t
)
1908 (const :tag
"Expert" expert
)))
1910 (defvar org-fast-tag-selection-include-todo nil
1911 "Non-nil means, fast tags selection interface will also offer TODO states.
1912 This is an undocumented feature, you should not rely on it.")
1914 (defcustom org-tags-column
(if (featurep 'xemacs
) -
79 -
80)
1915 "The column to which tags should be indented in a headline.
1916 If this number is positive, it specifies the column. If it is negative,
1917 it means that the tags should be flushright to that column. For example,
1918 -80 works well for a normal 80 character screen."
1922 (defcustom org-auto-align-tags t
1923 "Non-nil means, realign tags after pro/demotion of TODO state change.
1924 These operations change the length of a headline and therefore shift
1925 the tags around. With this options turned on, after each such operation
1926 the tags are again aligned to `org-tags-column'."
1930 (defcustom org-use-tag-inheritance t
1931 "Non-nil means, tags in levels apply also for sublevels.
1932 When nil, only the tags directly given in a specific line apply there.
1933 This may also be a list of tags that should be inherited, or a regexp that
1934 matches tags that should be inherited. Additional control is possible
1935 with the variable `org-tags-exclude-from-inheritance' which gives an
1936 explicit list of tags to be excluded from inheritance., even if the value of
1937 `org-use-tag-inheritance' would select it for inheritance.
1939 If this option is t, a match early-on in a tree can lead to a large
1940 number of matches in the subtree when constructing the agenda or creating
1941 a sparse tree. If you only want to see the first match in a tree during
1942 a search, check out the variable `org-tags-match-list-sublevels'."
1945 (const :tag
"Not" nil
)
1946 (const :tag
"Always" t
)
1947 (repeat :tag
"Specific tags" (string :tag
"Tag"))
1948 (regexp :tag
"Tags matched by regexp")))
1950 (defcustom org-tags-exclude-from-inheritance nil
1951 "List of tags that should never be inherited.
1952 This is a way to exclude a few tags from inheritance. For way to do
1953 the opposite, to actively allow inheritance for selected tags,
1954 see the variable `org-use-tag-inheritance'."
1956 :type
'(repeat (string :tag
"Tag")))
1958 (defun org-tag-inherit-p (tag)
1959 "Check if TAG is one that should be inherited."
1961 ((member tag org-tags-exclude-from-inheritance
) nil
)
1962 ((eq org-use-tag-inheritance t
) t
)
1963 ((not org-use-tag-inheritance
) nil
)
1964 ((stringp org-use-tag-inheritance
)
1965 (string-match org-use-tag-inheritance tag
))
1966 ((listp org-use-tag-inheritance
)
1967 (member tag org-use-tag-inheritance
))
1968 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
1970 (defcustom org-tags-match-list-sublevels t
1971 "Non-nil means list also sublevels of headlines matching tag search.
1972 Because of tag inheritance (see variable `org-use-tag-inheritance'),
1973 the sublevels of a headline matching a tag search often also match
1974 the same search. Listing all of them can create very long lists.
1975 Setting this variable to nil causes subtrees of a match to be skipped.
1976 This option is off by default, because inheritance in on. If you turn
1977 inheritance off, you very likely want to turn this option on.
1979 As a special case, if the tag search is restricted to TODO items, the
1980 value of this variable is ignored and sublevels are always checked, to
1981 make sure all corresponding TODO items find their way into the list.
1983 This variable is semi-obsolete and probably should always be true. It
1984 is better to limit inheritance to certain tags using the variables
1985 `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
1989 (defvar org-tags-history nil
1990 "History of minibuffer reads for tags.")
1991 (defvar org-last-tags-completion-table nil
1992 "The last used completion table for tags.")
1993 (defvar org-after-tags-change-hook nil
1994 "Hook that is run after the tags in a line have changed.")
1996 (defgroup org-properties nil
1997 "Options concerning properties in Org-mode."
1998 :tag
"Org Properties"
2001 (defcustom org-property-format
"%-10s %s"
2002 "How property key/value pairs should be formatted by `indent-line'.
2003 When `indent-line' hits a property definition, it will format the line
2004 according to this format, mainly to make sure that the values are
2005 lined-up with respect to each other."
2006 :group
'org-properties
2009 (defcustom org-use-property-inheritance nil
2010 "Non-nil means, properties apply also for sublevels.
2012 This setting is chiefly used during property searches. Turning it on can
2013 cause significant overhead when doing a search, which is why it is not
2016 When nil, only the properties directly given in the current entry count.
2017 When t, every property is inherited. The value may also be a list of
2018 properties that should have inheritance, or a regular expression matching
2019 properties that should be inherited.
2021 However, note that some special properties use inheritance under special
2022 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
2023 and the properties ending in \"_ALL\" when they are used as descriptor
2024 for valid values of a property.
2026 Note for programmers:
2027 When querying an entry with `org-entry-get', you can control if inheritance
2028 should be used. By default, `org-entry-get' looks only at the local
2029 properties. You can request inheritance by setting the inherit argument
2030 to t (to force inheritance) or to `selective' (to respect the setting
2032 :group
'org-properties
2034 (const :tag
"Not" nil
)
2035 (const :tag
"Always" t
)
2036 (repeat :tag
"Specific properties" (string :tag
"Property"))
2037 (regexp :tag
"Properties matched by regexp")))
2039 (defun org-property-inherit-p (property)
2040 "Check if PROPERTY is one that should be inherited."
2042 ((eq org-use-property-inheritance t
) t
)
2043 ((not org-use-property-inheritance
) nil
)
2044 ((stringp org-use-property-inheritance
)
2045 (string-match org-use-property-inheritance property
))
2046 ((listp org-use-property-inheritance
)
2047 (member property org-use-property-inheritance
))
2048 (t (error "Invalid setting of `org-use-property-inheritance'"))))
2050 (defcustom org-columns-default-format
"%25ITEM %TODO %3PRIORITY %TAGS"
2051 "The default column format, if no other format has been defined.
2052 This variable can be set on the per-file basis by inserting a line
2054 #+COLUMNS: %25ITEM ....."
2055 :group
'org-properties
2058 (defcustom org-columns-ellipses
".."
2059 "The ellipses to be used when a field in column view is truncated.
2060 When this is the empty string, as many characters as possible are shown,
2061 but then there will be no visual indication that the field has been truncated.
2062 When this is a string of length N, the last N characters of a truncated
2063 field are replaced by this string. If the column is narrower than the
2064 ellipses string, only part of the ellipses string will be shown."
2065 :group
'org-properties
2068 (defcustom org-columns-modify-value-for-display-function nil
2069 "Function that modifies values for display in column view.
2070 For example, it can be used to cut out a certain part from a time stamp.
2071 The function must take 2 arguments:
2073 column-title The title of the column (*not* the property name)
2074 value The value that should be modified.
2076 The function should return the value that should be displayed,
2077 or nil if the normal value should be used."
2078 :group
'org-properties
2081 (defcustom org-effort-property
"Effort"
2082 "The property that is being used to keep track of effort estimates.
2083 Effort estimates given in this property need to have the format H:MM."
2084 :group
'org-properties
2085 :group
'org-progress
2086 :type
'(string :tag
"Property"))
2088 (defconst org-global-properties-fixed
2089 '(("VISIBILITY_ALL" .
"folded children content all"))
2090 "List of property/value pairs that can be inherited by any entry.
2091 These are fixed values, for the preset properties.")
2094 (defcustom org-global-properties nil
2095 "List of property/value pairs that can be inherited by any entry.
2096 You can set buffer-local values for the same purpose in the variable
2097 `org-file-properties' this by adding lines like
2099 #+PROPERTY: NAME VALUE"
2100 :group
'org-properties
2102 (cons (string :tag
"Property")
2103 (string :tag
"Value"))))
2105 (defvar org-file-properties nil
2106 "List of property/value pairs that can be inherited by any entry.
2107 Valid for the current buffer.
2108 This variable is populated from #+PROPERTY lines.")
2109 (make-variable-buffer-local 'org-file-properties
)
2111 (defgroup org-agenda nil
2112 "Options concerning agenda views in Org-mode."
2116 (defvar org-category nil
2117 "Variable used by org files to set a category for agenda display.
2118 Such files should use a file variable to set it, for example
2120 # -*- mode: org; org-category: \"ELisp\"
2122 or contain a special line
2126 If the file does not specify a category, then file's base name
2128 (make-variable-buffer-local 'org-category
)
2129 (put 'org-category
'safe-local-variable
'(lambda (x) (or (symbolp x
) (stringp x
))))
2131 (defcustom org-agenda-files nil
2132 "The files to be used for agenda display.
2133 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
2134 \\[org-remove-file]. You can also use customize to edit the list.
2136 If an entry is a directory, all files in that directory that are matched by
2137 `org-agenda-file-regexp' will be part of the file list.
2139 If the value of the variable is not a list but a single file name, then
2140 the list of agenda files is actually stored and maintained in that file, one
2141 agenda file per line."
2144 (repeat :tag
"List of files and directories" file
)
2145 (file :tag
"Store list in a file\n" :value
"~/.agenda_files")))
2147 (defcustom org-agenda-file-regexp
"\\`[^.].*\\.org\\'"
2148 "Regular expression to match files for `org-agenda-files'.
2149 If any element in the list in that variable contains a directory instead
2150 of a normal file, all files in that directory that are matched by this
2151 regular expression will be included."
2155 (defcustom org-agenda-text-search-extra-files nil
2156 "List of extra files to be searched by text search commands.
2157 These files will be search in addition to the agenda files by the
2158 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
2159 Note that these files will only be searched for text search commands,
2160 not for the other agenda views like todo lists, tag searches or the weekly
2161 agenda. This variable is intended to list notes and possibly archive files
2162 that should also be searched by these two commands.
2163 In fact, if the first element in the list is the symbol `agenda-archives',
2164 than all archive files of all agenda files will be added to the search
2167 :type
'(set :greedy t
2168 (const :tag
"Agenda Archives" agenda-archives
)
2169 (repeat :inline t
(file))))
2171 (if (fboundp 'defvaralias
)
2172 (defvaralias 'org-agenda-multi-occur-extra-files
2173 'org-agenda-text-search-extra-files
))
2175 (defcustom org-agenda-skip-unavailable-files nil
2176 "Non-nil means to just skip non-reachable files in `org-agenda-files'.
2177 A nil value means to remove them, after a query, from the list."
2181 (defcustom org-calendar-to-agenda-key
[?c
]
2182 "The key to be installed in `calendar-mode-map' for switching to the agenda.
2183 The command `org-calendar-goto-agenda' will be bound to this key. The
2184 default is the character `c' because then `c' can be used to switch back and
2185 forth between agenda and calendar."
2189 (defcustom org-calendar-agenda-action-key
[?k
]
2190 "The key to be installed in `calendar-mode-map' for agenda-action.
2191 The command `org-agenda-action' will be bound to this key. The
2192 default is the character `k' because we use the same key in the agenda."
2196 (eval-after-load "calendar"
2198 (org-defkey calendar-mode-map org-calendar-to-agenda-key
2199 'org-calendar-goto-agenda
)
2200 (org-defkey calendar-mode-map org-calendar-agenda-action-key
2201 'org-agenda-action
)))
2203 (defgroup org-latex nil
2204 "Options for embedding LaTeX code into Org-mode."
2208 (defcustom org-format-latex-options
2209 '(:foreground default
:background default
:scale
1.0
2210 :html-foreground
"Black" :html-background
"Transparent" :html-scale
1.0
2211 :matchers
("begin" "$" "$$" "\\(" "\\["))
2212 "Options for creating images from LaTeX fragments.
2213 This is a property list with the following properties:
2214 :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
2215 `default' means use the foreground of the default face.
2216 :background the background color, or \"Transparent\".
2217 `default' means use the background of the default face.
2218 :scale a scaling factor for the size of the images.
2219 :html-foreground, :html-background, :html-scale
2220 the same numbers for HTML export.
2221 :matchers a list indicating which matchers should be used to
2222 find LaTeX fragments. Valid members of this list are:
2223 \"begin\" find environments
2224 \"$\" find math expressions surrounded by $...$
2225 \"$$\" find math expressions surrounded by $$....$$
2226 \"\\(\" find math expressions surrounded by \\(...\\)
2227 \"\\ [\" find math expressions surrounded by \\ [...\\]"
2231 (defcustom org-format-latex-header
"\\documentclass{article}
2232 \\usepackage{fullpage} % do not remove
2233 \\usepackage{amssymb}
2234 \\usepackage[usenames]{color}
2235 \\usepackage{amsmath}
2236 \\usepackage{latexsym}
2237 \\usepackage[mathscr]{eucal}
2238 \\pagestyle{empty} % do not remove"
2239 "The document header used for processing LaTeX fragments."
2244 (defgroup org-font-lock nil
2245 "Font-lock settings for highlighting in Org-mode."
2246 :tag
"Org Font Lock"
2249 (defcustom org-level-color-stars-only nil
2250 "Non-nil means fontify only the stars in each headline.
2251 When nil, the entire headline is fontified.
2252 Changing it requires restart of `font-lock-mode' to become effective
2253 also in regions already fontified."
2254 :group
'org-font-lock
2257 (defcustom org-hide-leading-stars nil
2258 "Non-nil means, hide the first N-1 stars in a headline.
2259 This works by using the face `org-hide' for these stars. This
2260 face is white for a light background, and black for a dark
2261 background. You may have to customize the face `org-hide' to
2263 Changing it requires restart of `font-lock-mode' to become effective
2264 also in regions already fontified.
2265 You may also set this on a per-file basis by adding one of the following
2266 lines to the buffer:
2268 #+STARTUP: hidestars
2269 #+STARTUP: showstars"
2270 :group
'org-font-lock
2273 (defcustom org-fontify-done-headline nil
2274 "Non-nil means, change the face of a headline if it is marked DONE.
2275 Normally, only the TODO/DONE keyword indicates the state of a headline.
2276 When this is non-nil, the headline after the keyword is set to the
2277 `org-headline-done' as an additional indication."
2278 :group
'org-font-lock
2281 (defcustom org-fontify-emphasized-text t
2282 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
2283 Changing this variable requires a restart of Emacs to take effect."
2284 :group
'org-font-lock
2287 (defcustom org-highlight-latex-fragments-and-specials nil
2288 "Non-nil means, fontify what is treated specially by the exporters."
2289 :group
'org-font-lock
2292 (defcustom org-hide-emphasis-markers nil
2293 "Non-nil mean font-lock should hide the emphasis marker characters."
2294 :group
'org-font-lock
2297 (defvar org-emph-re nil
2298 "Regular expression for matching emphasis.")
2299 (defvar org-verbatim-re nil
2300 "Regular expression for matching verbatim text.")
2301 (defvar org-emphasis-regexp-components
) ; defined just below
2302 (defvar org-emphasis-alist
) ; defined just below
2303 (defun org-set-emph-re (var val
)
2304 "Set variable and compute the emphasis regular expression."
2306 (when (and (boundp 'org-emphasis-alist
)
2307 (boundp 'org-emphasis-regexp-components
)
2308 org-emphasis-alist org-emphasis-regexp-components
)
2309 (let* ((e org-emphasis-regexp-components
)
2315 (stacked (and nil
(nth 5 e
))) ; stacked is no longer allowed, forced to nil
2316 (body1 (concat body
"*?"))
2317 (markers (mapconcat 'car org-emphasis-alist
""))
2318 (vmarkers (mapconcat
2319 (lambda (x) (if (eq (nth 4 x
) 'verbatim
) (car x
) ""))
2320 org-emphasis-alist
"")))
2321 ;; make sure special characters appear at the right position in the class
2322 (if (string-match "\\^" markers
)
2323 (setq markers
(concat (replace-match "" t t markers
) "^")))
2324 (if (string-match "-" markers
)
2325 (setq markers
(concat (replace-match "" t t markers
) "-")))
2326 (if (string-match "\\^" vmarkers
)
2327 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "^")))
2328 (if (string-match "-" vmarkers
)
2329 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "-")))
2331 (setq body1
(concat body1
"\\(?:\n" body
"*?\\)\\{0,"
2332 (int-to-string nl
) "\\}")))
2335 (concat "\\([" pre
(if (and nil stacked
) markers
) "]\\|^\\)"
2337 "\\([" markers
"]\\)"
2340 "[^" border
(if (and nil stacked
) markers
) "]"
2342 "[^" border
(if (and nil stacked
) markers
) "]"
2345 "\\([" post
(if (and nil stacked
) markers
) "]\\|$\\)"))
2346 (setq org-verbatim-re
2347 (concat "\\([" pre
"]\\|^\\)"
2349 "\\([" vmarkers
"]\\)"
2357 "\\([" post
"]\\|$\\)")))))
2359 (defcustom org-emphasis-regexp-components
2360 '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
2361 "Components used to build the regular expression for emphasis.
2362 This is a list with 6 entries. Terminology: In an emphasis string
2363 like \" *strong word* \", we call the initial space PREMATCH, the final
2364 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
2365 and \"trong wor\" is the body. The different components in this variable
2366 specify what is allowed/forbidden in each part:
2368 pre Chars allowed as prematch. Beginning of line will be allowed too.
2369 post Chars allowed as postmatch. End of line will be allowed too.
2370 border The chars *forbidden* as border characters.
2371 body-regexp A regexp like \".\" to match a body character. Don't use
2372 non-shy groups here, and don't allow newline here.
2373 newline The maximum number of newlines allowed in an emphasis exp.
2375 Use customize to modify this, or restart Emacs after changing it."
2376 :group
'org-font-lock
2377 :set
'org-set-emph-re
2379 (sexp :tag
"Allowed chars in pre ")
2380 (sexp :tag
"Allowed chars in post ")
2381 (sexp :tag
"Forbidden chars in border ")
2382 (sexp :tag
"Regexp for body ")
2383 (integer :tag
"number of newlines allowed")
2384 (option (boolean :tag
"Please ignore this button"))))
2386 (defcustom org-emphasis-alist
2387 `(("*" bold
"<b>" "</b>")
2388 ("/" italic
"<i>" "</i>")
2389 ("_" underline
"<span style=\"text-decoration:underline;\">" "</span>")
2390 ("=" org-code
"<code>" "</code>" verbatim
)
2391 ("~" org-verbatim
"<code>" "</code>" verbatim
)
2392 ("+" ,(if (featurep 'xemacs
) 'org-table
'(:strike-through t
))
2395 "Special syntax for emphasized text.
2396 Text starting and ending with a special character will be emphasized, for
2397 example *bold*, _underlined_ and /italic/. This variable sets the marker
2398 characters, the face to be used by font-lock for highlighting in Org-mode
2399 Emacs buffers, and the HTML tags to be used for this.
2400 Use customize to modify this, or restart Emacs after changing it."
2401 :group
'org-font-lock
2402 :set
'org-set-emph-re
2405 (string :tag
"Marker character")
2407 (face :tag
"Font-lock-face")
2408 (plist :tag
"Face property list"))
2409 (string :tag
"HTML start tag")
2410 (string :tag
"HTML end tag")
2411 (option (const verbatim
)))))
2413 ;;; Miscellaneous options
2415 (defgroup org-completion nil
2416 "Completion in Org-mode."
2417 :tag
"Org Completion"
2420 (defcustom org-completion-use-ido nil
2421 "Non-nil means, use ido completion wherever possible."
2422 :group
'org-completion
2425 (defcustom org-completion-fallback-command
'hippie-expand
2426 "The expansion command called by \\[org-complete] in normal context.
2427 Normal means, no org-mode-specific context."
2428 :group
'org-completion
2431 ;;; Functions and variables from ther packages
2432 ;; Declared here to avoid compiler warnings
2435 (defvar outline-mode-menu-heading
)
2436 (defvar outline-mode-menu-show
)
2437 (defvar outline-mode-menu-hide
)
2438 (defvar zmacs-regions
) ; XEmacs regions
2441 (defvar mark-active
)
2444 (declare-function calendar-absolute-from-iso
"cal-iso" (date))
2445 (declare-function calendar-forward-day
"cal-move" (arg))
2446 (declare-function calendar-goto-date
"cal-move" (date))
2447 (declare-function calendar-goto-today
"cal-move" ())
2448 (declare-function calendar-iso-from-absolute
"cal-iso" (date))
2449 (defvar calc-embedded-close-formula
)
2450 (defvar calc-embedded-open-formula
)
2451 (declare-function cdlatex-tab
"ext:cdlatex" ())
2452 (declare-function dired-get-filename
"dired" (&optional localp no-error-if-not-filep
))
2453 (defvar font-lock-unfontify-region-function
)
2454 (declare-function iswitchb-mode
"iswitchb" (&optional arg
))
2455 (declare-function iswitchb-read-buffer
(prompt &optional default require-match start matches-set
))
2456 (defvar iswitchb-temp-buflist
)
2457 (declare-function org-gnus-follow-link
"org-gnus" (&optional group article
))
2458 (declare-function org-agenda-skip
"org-agenda" ())
2459 (declare-function org-format-agenda-item
"org-agenda"
2460 (extra txt
&optional category tags dotime noprefix remove-re
))
2461 (declare-function org-agenda-new-marker
"org-agenda" (&optional pos
))
2462 (declare-function org-agenda-change-all-lines
"org-agenda"
2463 (newhead hdmarker
&optional fixface just-this
))
2464 (declare-function org-agenda-set-restriction-lock
"org-agenda" (&optional type
))
2465 (declare-function org-agenda-maybe-redo
"org-agenda" ())
2466 (declare-function org-agenda-save-markers-for-cut-and-paste
"org-agenda"
2468 (declare-function org-agenda-copy-local-variable
"org-agenda" (var))
2469 (declare-function parse-time-string
"parse-time" (string))
2470 (declare-function remember
"remember" (&optional initial
))
2471 (declare-function remember-buffer-desc
"remember" ())
2472 (declare-function remember-finalize
"remember" ())
2473 (defvar remember-save-after-remembering
)
2474 (defvar remember-data-file
)
2475 (defvar remember-register
)
2476 (defvar remember-buffer
)
2477 (defvar remember-handler-functions
)
2478 (defvar remember-annotation-functions
)
2479 (defvar texmathp-why
)
2480 (declare-function speedbar-line-directory
"speedbar" (&optional depth
))
2481 (declare-function table--at-cell-p
"table" (position &optional object at-column
))
2483 (defvar w3m-current-url
)
2484 (defvar w3m-current-title
)
2486 (defvar org-latex-regexps
)
2488 ;;; Autoload and prepare some org modules
2490 ;; Some table stuff that needs to be defined here, because it is used
2491 ;; by the functions setting up org-mode or checking for table context.
2493 (defconst org-table-any-line-regexp
"^[ \t]*\\(|\\|\\+-[-+]\\)"
2494 "Detects an org-type or table-type table.")
2495 (defconst org-table-line-regexp
"^[ \t]*|"
2496 "Detects an org-type table line.")
2497 (defconst org-table-dataline-regexp
"^[ \t]*|[^-]"
2498 "Detects an org-type table line.")
2499 (defconst org-table-hline-regexp
"^[ \t]*|-"
2500 "Detects an org-type table hline.")
2501 (defconst org-table1-hline-regexp
"^[ \t]*\\+-[-+]"
2502 "Detects a table-type table hline.")
2503 (defconst org-table-any-border-regexp
"^[ \t]*[^|+ \t]"
2504 "Searching from within a table (any type) this finds the first line
2505 outside the table.")
2507 ;; Autoload the functions in org-table.el that are needed by functions here.
2510 (org-autoload "org-table"
2511 '(org-table-align org-table-begin org-table-blank-field
2512 org-table-convert org-table-convert-region org-table-copy-down
2513 org-table-copy-region org-table-create
2514 org-table-create-or-convert-from-region
2515 org-table-create-with-table.el org-table-current-dline
2516 org-table-cut-region org-table-delete-column org-table-edit-field
2517 org-table-edit-formulas org-table-end org-table-eval-formula
2518 org-table-export org-table-field-info
2519 org-table-get-stored-formulas org-table-goto-column
2520 org-table-hline-and-move org-table-import org-table-insert-column
2521 org-table-insert-hline org-table-insert-row org-table-iterate
2522 org-table-justify-field-maybe org-table-kill-row
2523 org-table-maybe-eval-formula org-table-maybe-recalculate-line
2524 org-table-move-column org-table-move-column-left
2525 org-table-move-column-right org-table-move-row
2526 org-table-move-row-down org-table-move-row-up
2527 org-table-next-field org-table-next-row org-table-paste-rectangle
2528 org-table-previous-field org-table-recalculate
2529 org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
2530 org-table-toggle-coordinate-overlays
2531 org-table-toggle-formula-debugger org-table-wrap-region
2532 orgtbl-mode turn-on-orgtbl org-table-to-lisp
)))
2534 (defun org-at-table-p (&optional table-type
)
2535 "Return t if the cursor is inside an org-type table.
2536 If TABLE-TYPE is non-nil, also check for table.el-type tables."
2537 (if org-enable-table-editor
2539 (beginning-of-line 1)
2540 (looking-at (if table-type org-table-any-line-regexp
2541 org-table-line-regexp
)))
2543 (defsubst org-table-p
() (org-at-table-p))
2545 (defun org-at-table.el-p
()
2546 "Return t if and only if we are at a table.el table."
2547 (and (org-at-table-p 'any
)
2549 (goto-char (org-table-begin 'any
))
2550 (looking-at org-table1-hline-regexp
))))
2551 (defun org-table-recognize-table.el
()
2552 "If there is a table.el table nearby, recognize it and move into it."
2553 (if org-table-tab-recognizes-table.el
2554 (if (org-at-table.el-p
)
2556 (beginning-of-line 1)
2557 (if (looking-at org-table-dataline-regexp
)
2559 (if (looking-at org-table1-hline-regexp
)
2561 (beginning-of-line 2)
2562 (if (looking-at org-table-any-border-regexp
)
2563 (beginning-of-line -
1)))))
2564 (if (re-search-forward "|" (org-table-end t
) t
)
2567 (if (table--at-cell-p (point))
2569 (message "recognizing table.el table...")
2570 (table-recognize-table)
2571 (message "recognizing table.el table...done")))
2572 (error "This should not happen..."))
2577 (defun org-at-table-hline-p ()
2578 "Return t if the cursor is inside a hline in a table."
2579 (if org-enable-table-editor
2581 (beginning-of-line 1)
2582 (looking-at org-table-hline-regexp
))
2585 (defvar org-table-clean-did-remove-column nil
)
2587 (defun org-table-map-tables (function)
2588 "Apply FUNCTION to the start of all tables in the buffer."
2592 (goto-char (point-min))
2593 (while (re-search-forward org-table-any-line-regexp nil t
)
2594 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
2595 (beginning-of-line 1)
2596 (if (looking-at org-table-line-regexp
)
2597 (save-excursion (funcall function
)))
2598 (re-search-forward org-table-any-border-regexp nil
1))))
2599 (message "Mapping tables: done"))
2601 ;; Declare and autoload functions from org-exp.el
2603 (declare-function org-default-export-plist
"org-exp")
2604 (declare-function org-infile-export-plist
"org-exp")
2605 (declare-function org-get-current-options
"org-exp")
2607 (org-autoload "org-exp"
2608 '(org-export org-export-as-ascii org-export-visible
2609 org-insert-export-options-template org-export-as-html-and-open
2610 org-export-as-html-batch org-export-as-html-to-buffer
2611 org-replace-region-by-html org-export-region-as-html
2612 org-export-as-html org-export-icalendar-this-file
2613 org-export-icalendar-all-agenda-files
2614 org-table-clean-before-export
2615 org-export-icalendar-combine-agenda-files org-export-as-xoxo
)))
2617 ;; Declare and autoload functions from org-agenda.el
2620 (org-autoload "org-agenda"
2621 '(org-agenda org-agenda-list org-search-view
2622 org-todo-list org-tags-view org-agenda-list-stuck-projects
2623 org-diary org-agenda-to-appt
)))
2625 ;; Autoload org-remember
2628 (org-autoload "org-remember"
2629 '(org-remember-insinuate org-remember-annotation
2630 org-remember-apply-template org-remember org-remember-handler
)))
2632 ;; Autoload org-clock.el
2635 (declare-function org-clock-save-markers-for-cut-and-paste
"org-clock"
2637 (declare-function org-update-mode-line
"org-clock" ())
2638 (defvar org-clock-start-time
)
2639 (defvar org-clock-marker
(make-marker)
2640 "Marker recording the last clock-in.")
2645 '(org-clock-in org-clock-out org-clock-cancel
2646 org-clock-goto org-clock-sum org-clock-display
2647 org-remove-clock-overlays org-clock-report
2648 org-clocktable-shift org-dblock-write
:clocktable
2649 org-get-clocktable
)))
2651 (defun org-clock-update-time-maybe ()
2652 "If this is a CLOCK line, update it and return t.
2653 Otherwise, return nil."
2656 (beginning-of-line 1)
2657 (skip-chars-forward " \t")
2658 (when (looking-at org-clock-string
)
2659 (let ((re (concat "[ \t]*" org-clock-string
2660 " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
2661 "\\([ \t]*=>.*\\)?\\)?"))
2664 ((not (looking-at re
))
2666 ((not (match-end 2))
2667 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
2668 (> org-clock-marker
(point))
2669 (<= org-clock-marker
(point-at-eol)))
2670 ;; The clock is running here
2671 (setq org-clock-start-time
2673 (org-parse-time-string (match-string 1))))
2674 (org-update-mode-line)))
2676 (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
2678 (setq ts
(match-string 1)
2679 te
(match-string 3))
2680 (setq s
(- (time-to-seconds
2681 (apply 'encode-time
(org-parse-time-string te
)))
2683 (apply 'encode-time
(org-parse-time-string ts
))))
2686 h
(floor (/ s
3600))
2690 (insert " => " (format (if neg
"-%d:%02d" "%2d:%02d") h m
))
2693 (defun org-check-running-clock ()
2694 "Check if the current buffer contains the running clock.
2695 If yes, offer to stop it and to save the buffer with the changes."
2696 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
2697 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
2700 (when (y-or-n-p "Save changed buffer?")
2703 (defun org-clocktable-try-shift (dir n
)
2704 "Check if this line starts a clock table, if yes, shift the time block."
2705 (when (org-match-line "#\\+BEGIN: clocktable\\>")
2706 (org-clocktable-shift dir n
)))
2708 ;; Autoload org-timer.el
2710 ;(declare-function org-timer "org-timer")
2715 '(org-timer-start org-timer org-timer-item
2716 org-timer-change-times-in-region
)))
2719 ;; Autoload archiving code
2720 ;; The stuff that is needed for cycling and tags has to be defined here.
2722 (defgroup org-archive nil
2723 "Options concerning archiving in Org-mode."
2725 :group
'org-structure
)
2727 (defcustom org-archive-location
"%s_archive::"
2728 "The location where subtrees should be archived.
2730 The value of this variable is a string, consisting of two parts,
2731 separated by a double-colon. The first part is a filename and
2732 the second part is a headline.
2734 When the filename is omitted, archiving happens in the same file.
2735 %s in the filename will be replaced by the current file
2736 name (without the directory part). Archiving to a different file
2737 is useful to keep archived entries from contributing to the
2740 The archived entries will be filed as subtrees of the specified
2741 headline. When the headline is omitted, the subtrees are simply
2742 filed away at the end of the file, as top-level entries.
2744 Here are a few examples:
2746 If the current file is Projects.org, archive in file
2747 Projects.org_archive, as top-level trees. This is the default.
2749 \"::* Archived Tasks\"
2750 Archive in the current file, under the top-level headline
2751 \"* Archived Tasks\".
2753 \"~/org/archive.org::\"
2754 Archive in file ~/org/archive.org (absolute path), as top-level trees.
2756 \"basement::** Finished Tasks\"
2757 Archive in file ./basement (relative path), as level 3 trees
2758 below the level 2 heading \"** Finished Tasks\".
2760 You may set this option on a per-file basis by adding to the buffer a
2763 #+ARCHIVE: basement::** Finished Tasks
2765 You may also define it locally for a subtree by setting an ARCHIVE property
2766 in the entry. If such a property is found in an entry, or anywhere up
2767 the hierarchy, it will be used."
2771 (defcustom org-archive-tag
"ARCHIVE"
2772 "The tag that marks a subtree as archived.
2773 An archived subtree does not open during visibility cycling, and does
2774 not contribute to the agenda listings.
2775 After changing this, font-lock must be restarted in the relevant buffers to
2776 get the proper fontification."
2778 :group
'org-keywords
2781 (defcustom org-agenda-skip-archived-trees t
2782 "Non-nil means, the agenda will skip any items located in archived trees.
2783 An archived tree is a tree marked with the tag ARCHIVE. The use of this
2784 variable is no longer recommended, you should leave it at the value t.
2785 Instead, use the key `v' to cycle the archives-mode in the agenda."
2787 :group
'org-agenda-skip
2790 (defcustom org-cycle-open-archived-trees nil
2791 "Non-nil means, `org-cycle' will open archived trees.
2792 An archived tree is a tree marked with the tag ARCHIVE.
2793 When nil, archived trees will stay folded. You can still open them with
2794 normal outline commands like `show-all', but not with the cycling commands."
2799 (defcustom org-sparse-tree-open-archived-trees nil
2800 "Non-nil means sparse tree construction shows matches in archived trees.
2801 When nil, matches in these trees are highlighted, but the trees are kept in
2804 :group
'org-sparse-trees
2807 (defun org-cycle-hide-archived-subtrees (state)
2808 "Re-hide all archived subtrees after a visibility state change."
2809 (when (and (not org-cycle-open-archived-trees
)
2810 (not (memq state
'(overview folded
))))
2812 (let* ((globalp (memq state
'(contents all
)))
2813 (beg (if globalp
(point-min) (point)))
2814 (end (if globalp
(point-max) (org-end-of-subtree t
))))
2815 (org-hide-archived-subtrees beg end
)
2817 (if (looking-at (concat ".*:" org-archive-tag
":"))
2818 (message "%s" (substitute-command-keys
2819 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
2821 (defun org-force-cycle-archived ()
2822 "Cycle subtree even if it is archived."
2824 (setq this-command
'org-cycle
)
2825 (let ((org-cycle-open-archived-trees t
))
2826 (call-interactively 'org-cycle
)))
2828 (defun org-hide-archived-subtrees (beg end
)
2829 "Re-hide all archived subtrees after a visibility state change."
2831 (let* ((re (concat ":" org-archive-tag
":")))
2833 (while (re-search-forward re end t
)
2834 (and (org-on-heading-p) (hide-subtree))
2835 (org-end-of-subtree t
)))))
2837 (defalias 'org-advertized-archive-subtree
'org-archive-subtree
)
2840 (org-autoload "org-archive"
2841 '(org-add-archive-files org-archive-subtree
2842 org-archive-to-archive-sibling org-toggle-archive-tag
)))
2844 ;; Autoload Column View Code
2846 (declare-function org-columns-number-to-string
"org-colview")
2847 (declare-function org-columns-get-format-and-top-level
"org-colview")
2848 (declare-function org-columns-compute
"org-colview")
2850 (org-autoload (if (featurep 'xemacs
) "org-colview-xemacs" "org-colview")
2851 '(org-columns-number-to-string org-columns-get-format-and-top-level
2852 org-columns-compute org-agenda-columns org-columns-remove-overlays
2853 org-columns org-insert-columns-dblock org-dblock-write
:columnview
))
2857 (declare-function org-id-store-link
"org-id")
2858 (org-autoload "org-id"
2859 '(org-id-get-create org-id-new org-id-copy org-id-get
2860 org-id-get-with-outline-path-completion
2861 org-id-get-with-outline-drilling
2862 org-id-goto org-id-find org-id-store-link
))
2864 ;;; Variables for pre-computed regular expressions, all buffer local
2866 (defvar org-drawer-regexp nil
2867 "Matches first line of a hidden block.")
2868 (make-variable-buffer-local 'org-drawer-regexp
)
2869 (defvar org-todo-regexp nil
2870 "Matches any of the TODO state keywords.")
2871 (make-variable-buffer-local 'org-todo-regexp
)
2872 (defvar org-not-done-regexp nil
2873 "Matches any of the TODO state keywords except the last one.")
2874 (make-variable-buffer-local 'org-not-done-regexp
)
2875 (defvar org-todo-line-regexp nil
2876 "Matches a headline and puts TODO state into group 2 if present.")
2877 (make-variable-buffer-local 'org-todo-line-regexp
)
2878 (defvar org-complex-heading-regexp nil
2879 "Matches a headline and puts everything into groups:
2881 group 2: The todo keyword, maybe
2882 group 3: Priority cookie
2883 group 4: True headline
2885 (make-variable-buffer-local 'org-complex-heading-regexp
)
2886 (defvar org-todo-line-tags-regexp nil
2887 "Matches a headline and puts TODO state into group 2 if present.
2888 Also put tags into group 4 if tags are present.")
2889 (make-variable-buffer-local 'org-todo-line-tags-regexp
)
2890 (defvar org-nl-done-regexp nil
2891 "Matches newline followed by a headline with the DONE keyword.")
2892 (make-variable-buffer-local 'org-nl-done-regexp
)
2893 (defvar org-looking-at-done-regexp nil
2894 "Matches the DONE keyword a point.")
2895 (make-variable-buffer-local 'org-looking-at-done-regexp
)
2896 (defvar org-ds-keyword-length
12
2897 "Maximum length of the Deadline and SCHEDULED keywords.")
2898 (make-variable-buffer-local 'org-ds-keyword-length
)
2899 (defvar org-deadline-regexp nil
2900 "Matches the DEADLINE keyword.")
2901 (make-variable-buffer-local 'org-deadline-regexp
)
2902 (defvar org-deadline-time-regexp nil
2903 "Matches the DEADLINE keyword together with a time stamp.")
2904 (make-variable-buffer-local 'org-deadline-time-regexp
)
2905 (defvar org-deadline-line-regexp nil
2906 "Matches the DEADLINE keyword and the rest of the line.")
2907 (make-variable-buffer-local 'org-deadline-line-regexp
)
2908 (defvar org-scheduled-regexp nil
2909 "Matches the SCHEDULED keyword.")
2910 (make-variable-buffer-local 'org-scheduled-regexp
)
2911 (defvar org-scheduled-time-regexp nil
2912 "Matches the SCHEDULED keyword together with a time stamp.")
2913 (make-variable-buffer-local 'org-scheduled-time-regexp
)
2914 (defvar org-closed-time-regexp nil
2915 "Matches the CLOSED keyword together with a time stamp.")
2916 (make-variable-buffer-local 'org-closed-time-regexp
)
2918 (defvar org-keyword-time-regexp nil
2919 "Matches any of the 4 keywords, together with the time stamp.")
2920 (make-variable-buffer-local 'org-keyword-time-regexp
)
2921 (defvar org-keyword-time-not-clock-regexp nil
2922 "Matches any of the 3 keywords, together with the time stamp.")
2923 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp
)
2924 (defvar org-maybe-keyword-time-regexp nil
2925 "Matches a timestamp, possibly preceeded by a keyword.")
2926 (make-variable-buffer-local 'org-maybe-keyword-time-regexp
)
2927 (defvar org-planning-or-clock-line-re nil
2928 "Matches a line with planning or clock info.")
2929 (make-variable-buffer-local 'org-planning-or-clock-line-re
)
2931 (defconst org-plain-time-of-day-regexp
2934 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
2937 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
2939 "Regular expression to match a plain time or time range.
2940 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
2941 groups carry important information:
2943 1 the first time, range or not
2944 8 the second time, if it is a range.")
2946 (defconst org-plain-time-extension-regexp
2949 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
2950 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
2951 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
2952 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
2953 groups carry important information:
2956 9 minutes of duration")
2958 (defconst org-stamp-time-of-day-regexp
2960 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
2961 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
2963 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
2964 "Regular expression to match a timestamp time or time range.
2965 After a match, the following groups carry important information:
2967 1 date plus weekday, for backreferencing to make sure both times on same day
2968 2 the first time, range or not
2969 4 the second time, if it is a range.")
2971 (defconst org-startup-options
2972 '(("fold" org-startup-folded t
)
2973 ("overview" org-startup-folded t
)
2974 ("nofold" org-startup-folded nil
)
2975 ("showall" org-startup-folded nil
)
2976 ("content" org-startup-folded content
)
2977 ("hidestars" org-hide-leading-stars t
)
2978 ("showstars" org-hide-leading-stars nil
)
2979 ("odd" org-odd-levels-only t
)
2980 ("oddeven" org-odd-levels-only nil
)
2981 ("align" org-startup-align-all-tables t
)
2982 ("noalign" org-startup-align-all-tables nil
)
2983 ("customtime" org-display-custom-times t
)
2984 ("logdone" org-log-done time
)
2985 ("lognotedone" org-log-done note
)
2986 ("nologdone" org-log-done nil
)
2987 ("lognoteclock-out" org-log-note-clock-out t
)
2988 ("nolognoteclock-out" org-log-note-clock-out nil
)
2989 ("logrepeat" org-log-repeat state
)
2990 ("lognoterepeat" org-log-repeat note
)
2991 ("nologrepeat" org-log-repeat nil
)
2992 ("constcgs" constants-unit-system cgs
)
2993 ("constSI" constants-unit-system SI
))
2994 "Variable associated with STARTUP options for org-mode.
2995 Each element is a list of three items: The startup options as written
2996 in the #+STARTUP line, the corresponding variable, and the value to
2997 set this variable to if the option is found. An optional forth element PUSH
2998 means to push this value onto the list in the variable.")
3000 (defun org-set-regexps-and-options ()
3001 "Precompute regular expressions for current buffer."
3003 (org-set-local 'org-todo-kwd-alist nil
)
3004 (org-set-local 'org-todo-key-alist nil
)
3005 (org-set-local 'org-todo-key-trigger nil
)
3006 (org-set-local 'org-todo-keywords-1 nil
)
3007 (org-set-local 'org-done-keywords nil
)
3008 (org-set-local 'org-todo-heads nil
)
3009 (org-set-local 'org-todo-sets nil
)
3010 (org-set-local 'org-todo-log-states nil
)
3011 (org-set-local 'org-file-properties nil
)
3012 (org-set-local 'org-file-tags nil
)
3013 (let ((re (org-make-options-regexp
3014 '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
3015 "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
3016 "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
3018 kwds kws0 kwsa key log value cat arch tags const links hw dws
3019 tail sep kws1 prio props ftags drawers
3020 ext-setup-or-nil setup-contents
(start 0))
3024 (goto-char (point-min))
3025 (while (or (and ext-setup-or-nil
3026 (string-match re ext-setup-or-nil start
)
3027 (setq start
(match-end 0)))
3028 (and (setq ext-setup-or-nil nil start
0)
3029 (re-search-forward re nil t
)))
3030 (setq key
(upcase (match-string 1 ext-setup-or-nil
))
3031 value
(org-match-string-no-properties 2 ext-setup-or-nil
))
3033 ((equal key
"CATEGORY")
3034 (if (string-match "[ \t]+$" value
)
3035 (setq value
(replace-match "" t t value
)))
3037 ((member key
'("SEQ_TODO" "TODO"))
3038 (push (cons 'sequence
(org-split-string value splitre
)) kwds
))
3039 ((equal key
"TYP_TODO")
3040 (push (cons 'type
(org-split-string value splitre
)) kwds
))
3042 (setq tags
(append tags
(org-split-string value splitre
))))
3043 ((equal key
"COLUMNS")
3044 (org-set-local 'org-columns-default-format value
))
3046 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value
)
3047 (push (cons (match-string 1 value
)
3048 (org-trim (match-string 2 value
)))
3050 ((equal key
"PRIORITIES")
3051 (setq prio
(org-split-string value
" +")))
3052 ((equal key
"PROPERTY")
3053 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
3054 (push (cons (match-string 1 value
) (match-string 2 value
))
3056 ((equal key
"FILETAGS")
3057 (when (string-match "\\S-" value
)
3062 (mapcar (lambda (x) (org-split-string x
":"))
3063 (org-split-string value
)))))))
3064 ((equal key
"DRAWERS")
3065 (setq drawers
(org-split-string value splitre
)))
3066 ((equal key
"CONSTANTS")
3067 (setq const
(append const
(org-split-string value splitre
))))
3068 ((equal key
"STARTUP")
3069 (let ((opts (org-split-string value splitre
))
3071 (while (setq l
(pop opts
))
3072 (when (setq l
(assoc l org-startup-options
))
3073 (setq var
(nth 1 l
) val
(nth 2 l
))
3075 (set (make-local-variable var
) val
)
3076 (if (not (listp (symbol-value var
)))
3077 (set (make-local-variable var
) nil
))
3078 (set (make-local-variable var
) (symbol-value var
))
3079 (add-to-list var val
))))))
3080 ((equal key
"ARCHIVE")
3081 (string-match " *$" value
)
3082 (setq arch
(replace-match "" t t value
))
3083 (remove-text-properties 0 (length arch
)
3084 '(face t fontified t
) arch
))
3085 ((equal key
"SETUPFILE")
3086 (setq setup-contents
(org-file-contents
3088 (org-remove-double-quotes value
))
3090 (if (not ext-setup-or-nil
)
3091 (setq ext-setup-or-nil setup-contents start
0)
3092 (setq ext-setup-or-nil
3093 (concat (substring ext-setup-or-nil
0 start
)
3094 "\n" setup-contents
"\n"
3095 (substring ext-setup-or-nil start
)))))
3098 (org-set-local 'org-category
(intern cat
))
3099 (push (cons "CATEGORY" cat
) props
))
3101 (if (< (length prio
) 3) (setq prio
'("A" "C" "B")))
3102 (setq prio
(mapcar 'string-to-char prio
))
3103 (org-set-local 'org-highest-priority
(nth 0 prio
))
3104 (org-set-local 'org-lowest-priority
(nth 1 prio
))
3105 (org-set-local 'org-default-priority
(nth 2 prio
)))
3106 (and props
(org-set-local 'org-file-properties
(nreverse props
)))
3107 (and ftags
(org-set-local 'org-file-tags ftags
))
3108 (and drawers
(org-set-local 'org-drawers drawers
))
3109 (and arch
(org-set-local 'org-archive-location arch
))
3110 (and links
(setq org-link-abbrev-alist-local
(nreverse links
)))
3111 ;; Process the TODO keywords
3113 ;; Use the global values as if they had been given locally.
3114 (setq kwds
(default-value 'org-todo-keywords
))
3115 (if (stringp (car kwds
))
3116 (setq kwds
(list (cons org-todo-interpretation
3117 (default-value 'org-todo-keywords
)))))
3118 (setq kwds
(reverse kwds
)))
3119 (setq kwds
(nreverse kwds
))
3121 (while (setq kws
(pop kwds
))
3122 (setq inter
(pop kws
) sep
(member "|" kws
)
3123 kws0
(delete "|" (copy-sequence kws
))
3128 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x
)
3130 (setq kw
(match-string 1 x
)
3131 key
(and (match-end 2) (match-string 2 x
))
3132 log
(org-extract-log-state-settings x
))
3133 (push (cons kw
(and key
(string-to-char key
))) kwsa
)
3134 (and log
(push log org-todo-log-states
))
3136 (error "Invalid TODO keyword %s" x
)))
3138 kwsa
(if kwsa
(append '((:startgroup
))
3142 dws
(if sep
(org-remove-keyword-keys (cdr sep
)) (last kws1
))
3143 tail
(list inter hw
(car dws
) (org-last dws
)))
3144 (add-to-list 'org-todo-heads hw
'append
)
3145 (push kws1 org-todo-sets
)
3146 (setq org-done-keywords
(append org-done-keywords dws nil
))
3147 (setq org-todo-key-alist
(append org-todo-key-alist kwsa
))
3148 (mapc (lambda (x) (push (cons x tail
) org-todo-kwd-alist
)) kws1
)
3149 (setq org-todo-keywords-1
(append org-todo-keywords-1 kws1 nil
)))
3150 (setq org-todo-sets
(nreverse org-todo-sets
)
3151 org-todo-kwd-alist
(nreverse org-todo-kwd-alist
)
3152 org-todo-key-trigger
(delq nil
(mapcar 'cdr org-todo-key-alist
))
3153 org-todo-key-alist
(org-assign-fast-keys org-todo-key-alist
)))
3154 ;; Process the constants
3157 (while (setq e
(pop const
))
3158 (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e
)
3159 (push (cons (match-string 1 e
) (match-string 2 e
)) cst
)))
3160 (setq org-table-formula-constants-local cst
)))
3162 ;; Process the tags.
3165 (while (setq e
(pop tags
))
3167 ((equal e
"{") (push '(:startgroup
) tgs
))
3168 ((equal e
"}") (push '(:endgroup
) tgs
))
3169 ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e
)
3170 (push (cons (match-string 1 e
)
3171 (string-to-char (match-string 2 e
)))
3173 (t (push (list e
) tgs
))))
3174 (org-set-local 'org-tag-alist nil
)
3175 (while (setq e
(pop tgs
))
3176 (or (and (stringp (car e
))
3177 (assoc (car e
) org-tag-alist
))
3178 (push e org-tag-alist
)))))
3180 ;; Compute the regular expressions and other local variables
3181 (if (not org-done-keywords
)
3182 (setq org-done-keywords
(list (org-last org-todo-keywords-1
))))
3183 (setq org-ds-keyword-length
(+ 2 (max (length org-deadline-string
)
3184 (length org-scheduled-string
)
3185 (length org-clock-string
)
3186 (length org-closed-string
)))
3188 (concat "^[ \t]*:\\("
3189 (mapconcat 'regexp-quote org-drawers
"\\|")
3191 org-not-done-keywords
3192 (org-delete-all org-done-keywords
(copy-sequence org-todo-keywords-1
))
3194 (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
3198 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
3200 org-todo-line-regexp
3201 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3202 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
3203 "\\)\\>\\)?[ \t]*\\(.*\\)")
3204 org-complex-heading-regexp
3205 (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
3206 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
3207 "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
3208 "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
3210 (concat "\n\\*+[ \t]+"
3211 "\\(?:" (mapconcat 'regexp-quote org-done-keywords
"\\|")
3213 org-todo-line-tags-regexp
3214 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3215 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
3217 "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
3218 org-looking-at-done-regexp
3220 (mapconcat 'regexp-quote org-done-keywords
"\\|") "\\)"
3222 org-deadline-regexp
(concat "\\<" org-deadline-string
)
3223 org-deadline-time-regexp
3224 (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>")
3225 org-deadline-line-regexp
3226 (concat "\\<\\(" org-deadline-string
"\\).*")
3227 org-scheduled-regexp
3228 (concat "\\<" org-scheduled-string
)
3229 org-scheduled-time-regexp
3230 (concat "\\<" org-scheduled-string
" *<\\([^>]+\\)>")
3231 org-closed-time-regexp
3232 (concat "\\<" org-closed-string
" *\\[\\([^]]+\\)\\]")
3233 org-keyword-time-regexp
3234 (concat "\\<\\(" org-scheduled-string
3235 "\\|" org-deadline-string
3236 "\\|" org-closed-string
3237 "\\|" org-clock-string
"\\)"
3238 " *[[<]\\([^]>]+\\)[]>]")
3239 org-keyword-time-not-clock-regexp
3240 (concat "\\<\\(" org-scheduled-string
3241 "\\|" org-deadline-string
3242 "\\|" org-closed-string
3244 " *[[<]\\([^]>]+\\)[]>]")
3245 org-maybe-keyword-time-regexp
3246 (concat "\\(\\<\\(" org-scheduled-string
3247 "\\|" org-deadline-string
3248 "\\|" org-closed-string
3249 "\\|" org-clock-string
"\\)\\)?"
3250 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
3251 org-planning-or-clock-line-re
3252 (concat "\\(?:^[ \t]*\\(" org-scheduled-string
3253 "\\|" org-deadline-string
3254 "\\|" org-closed-string
"\\|" org-clock-string
3257 (org-compute-latex-and-specials-regexp)
3258 (org-set-font-lock-defaults))))
3260 (defun org-file-contents (file &optional noerror
)
3261 "Return the contents of FILE, as a string."
3263 (not (file-readable-p file
)))
3266 (message "Cannot read file %s" file
)
3269 (error "Cannot read file %s" file
))
3271 (insert-file-contents file
)
3274 (defun org-extract-log-state-settings (x)
3275 "Extract the log state setting from a TODO keyword string.
3276 This will extract info from a string like \"WAIT(w@/!)\"."
3277 (let (kw key log1 log2
)
3278 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x
)
3279 (setq kw
(match-string 1 x
)
3280 key
(and (match-end 2) (match-string 2 x
))
3281 log1
(and (match-end 3) (match-string 3 x
))
3282 log2
(and (match-end 4) (match-string 4 x
)))
3285 (and log1
(if (equal log1
"!") 'time
'note
))
3286 (and log2
(if (equal log2
"!") 'time
'note
)))))))
3288 (defun org-remove-keyword-keys (list)
3289 "Remove a pair of parenthesis at the end of each string in LIST."
3291 (if (string-match "(.*)$" x
)
3292 (substring x
0 (match-beginning 0))
3296 ;; FIXME: this could be done much better, using second characters etc.
3297 (defun org-assign-fast-keys (alist)
3298 "Assign fast keys to a keyword-key alist.
3299 Respect keys that are already there."
3300 (let (new e k c c1 c2
(char ?a
))
3301 (while (setq e
(pop alist
))
3303 ((equal e
'(:startgroup
)) (push e new
))
3304 ((equal e
'(:endgroup
)) (push e new
))
3306 (setq k
(car e
) c2 nil
)
3309 ;; automatically assign a character.
3310 (setq c1
(string-to-char
3311 (downcase (substring
3312 k
(if (= (string-to-char k
) ?
@) 1 0)))))
3313 (if (or (rassoc c1 new
) (rassoc c1 alist
))
3314 (while (or (rassoc char new
) (rassoc char alist
))
3315 (setq char
(1+ char
)))
3317 (setq c
(or c2 char
)))
3318 (push (cons k c
) new
))))
3321 ;;; Some variables used in various places
3323 (defvar org-window-configuration nil
3324 "Used in various places to store a window configuration.")
3325 (defvar org-finish-function nil
3326 "Function to be called when `C-c C-c' is used.
3327 This is for getting out of special buffers like remember.")
3330 ;; FIXME: Occasionally check by commenting these, to make sure
3331 ;; no other functions uses these, forgetting to let-bind them.
3336 (defvar description
)
3338 ;; Defined somewhere in this file, but used before definition.
3339 (defvar org-html-entities
)
3340 (defvar org-struct-menu
)
3341 (defvar org-org-menu
)
3342 (defvar org-tbl-menu
)
3343 (defvar org-agenda-keymap
)
3345 ;;;; Define the Org-mode
3347 (if (and (not (keymapp outline-mode-map
)) (featurep 'allout
))
3348 (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or upgrade to newer allout, for example by switching to Emacs 22."))
3351 ;; We use a before-change function to check if a table might need
3353 (defvar org-table-may-need-update t
3354 "Indicates that a table might need an update.
3355 This variable is set by `org-before-change-function'.
3356 `org-table-align' sets it back to nil.")
3357 (defun org-before-change-function (beg end
)
3358 "Every change indicates that a table might need an update."
3359 (setq org-table-may-need-update t
))
3360 (defvar org-mode-map
)
3361 (defvar org-mode-hook nil
)
3362 (defvar org-inhibit-startup nil
) ; Dynamically-scoped param.
3363 (defvar org-agenda-keep-modes nil
) ; Dynamically-scoped param.
3364 (defvar org-table-buffer-is-an nil
)
3365 (defconst org-outline-regexp
"\\*+ ")
3368 (define-derived-mode org-mode outline-mode
"Org"
3369 "Outline-based notes management and organizer, alias
3370 \"Carsten's outline-mode for keeping track of everything.\"
3372 Org-mode develops organizational tasks around a NOTES file which
3373 contains information about projects as plain text. Org-mode is
3374 implemented on top of outline-mode, which is ideal to keep the content
3375 of large files well structured. It supports ToDo items, deadlines and
3376 time stamps, which magically appear in the diary listing of the Emacs
3377 calendar. Tables are easily created with a built-in table editor.
3378 Plain text URL-like links connect to websites, emails (VM), Usenet
3379 messages (Gnus), BBDB entries, and any files related to the project.
3380 For printing and sharing of notes, an Org-mode file (or a part of it)
3381 can be exported as a structured ASCII or HTML file.
3383 The following commands are available:
3387 ;; Get rid of Outline menus, they are not needed
3388 ;; Need to do this here because define-derived-mode sets up
3389 ;; the keymap so late. Still, it is a waste to call this each time
3390 ;; we switch another buffer into org-mode.
3391 (if (featurep 'xemacs
)
3392 (when (boundp 'outline-mode-menu-heading
)
3393 ;; Assume this is Greg's port, it used easymenu
3394 (easy-menu-remove outline-mode-menu-heading
)
3395 (easy-menu-remove outline-mode-menu-show
)
3396 (easy-menu-remove outline-mode-menu-hide
))
3397 (define-key org-mode-map
[menu-bar headings
] 'undefined
)
3398 (define-key org-mode-map
[menu-bar hide
] 'undefined
)
3399 (define-key org-mode-map
[menu-bar show
] 'undefined
))
3401 (org-load-modules-maybe)
3402 (easy-menu-add org-org-menu
)
3403 (easy-menu-add org-tbl-menu
)
3404 (org-install-agenda-files-menu)
3405 (if org-descriptive-links
(org-add-to-invisibility-spec '(org-link)))
3406 (org-add-to-invisibility-spec '(org-cwidth))
3407 (when (featurep 'xemacs
)
3408 (org-set-local 'line-move-ignore-invisible t
))
3409 (org-set-local 'outline-regexp org-outline-regexp
)
3410 (org-set-local 'outline-level
'org-outline-level
)
3411 (when (and org-ellipsis
3412 (fboundp 'set-display-table-slot
) (boundp 'buffer-display-table
)
3413 (fboundp 'make-glyph-code
))
3414 (unless org-display-table
3415 (setq org-display-table
(make-display-table)))
3416 (set-display-table-slot
3419 (lambda (c) (make-glyph-code c
(and (not (stringp org-ellipsis
))
3421 (if (stringp org-ellipsis
) org-ellipsis
"..."))))
3422 (setq buffer-display-table org-display-table
))
3423 (org-set-regexps-and-options)
3424 (when (and org-tag-faces
(not org-tags-special-faces-re
))
3425 ;; tag faces set outside customize.... force initialization.
3426 (org-set-tag-faces 'org-tag-faces org-tag-faces
))
3428 (org-set-local 'calc-embedded-open-mode
"# ")
3429 (modify-syntax-entry ?
# "<")
3430 (modify-syntax-entry ?
@ "w")
3431 (if org-startup-truncated
(setq truncate-lines t
))
3432 (org-set-local 'font-lock-unfontify-region-function
3433 'org-unfontify-region
)
3434 ;; Activate before-change-function
3435 (org-set-local 'org-table-may-need-update t
)
3436 (org-add-hook 'before-change-functions
'org-before-change-function nil
3438 ;; Check for running clock before killing a buffer
3439 (org-add-hook 'kill-buffer-hook
'org-check-running-clock nil
'local
)
3440 ;; Paragraphs and auto-filling
3441 (org-set-autofill-regexps)
3442 (setq indent-line-function
'org-indent-line-function
)
3443 (org-update-radio-target-regexp)
3445 ;; Comment characters
3446 ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
3447 (org-set-local 'comment-padding
" ")
3449 ;; Align options lines
3451 'align-mode-rules-list
3452 '((org-in-buffer-settings
3453 (regexp .
"^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
3454 (modes .
'(org-mode)))))
3457 (org-set-local 'imenu-create-index-function
3458 'org-imenu-get-tree
)
3460 ;; Make isearch reveal context
3461 (if (or (featurep 'xemacs
)
3462 (not (boundp 'outline-isearch-open-invisible-function
)))
3463 ;; Emacs 21 and XEmacs make use of the hook
3464 (org-add-hook 'isearch-mode-end-hook
'org-isearch-end
'append
'local
)
3465 ;; Emacs 22 deals with this through a special variable
3466 (org-set-local 'outline-isearch-open-invisible-function
3467 (lambda (&rest ignore
) (org-show-context 'isearch
))))
3469 ;; If empty file that did not turn on org-mode automatically, make it to.
3470 (if (and org-insert-mode-line-in-empty-file
3472 (= (point-min) (point-max)))
3473 (insert "# -*- mode: org -*-\n\n"))
3475 (unless org-inhibit-startup
3476 (when org-startup-align-all-tables
3477 (let ((bmp (buffer-modified-p)))
3478 (org-table-map-tables 'org-table-align
)
3479 (set-buffer-modified-p bmp
)))
3480 (org-set-startup-visibility)))
3482 (put 'org-mode
'flyspell-mode-predicate
'org-mode-flyspell-verify
)
3484 (defun org-current-time ()
3485 "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
3486 (if (> (car org-time-stamp-rounding-minutes
) 1)
3487 (let ((r (car org-time-stamp-rounding-minutes
))
3488 (time (decode-time)))
3490 (append (list 0 (* r
(floor (+ .5 (/ (float (nth 1 time
)) r
)))))
3494 ;;;; Font-Lock stuff, including the activators
3496 (defvar org-mouse-map
(make-sparse-keymap))
3497 (org-defkey org-mouse-map
3498 (if (featurep 'xemacs
) [button2] [mouse-2]) 'org-open-at-mouse)
3499 (org-defkey org-mouse-map
3500 (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
3501 (when org-mouse-1-follows-link
3502 (org-defkey org-mouse-map [follow-link] 'mouse-face))
3503 (when org-tab-follows-link
3504 (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
3505 (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
3506 (when org-return-follows-link
3507 (org-defkey org-mouse-map [(return)] 'org-open-at-point)
3508 (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
3510 (require 'font-lock)
3512 (defconst org-non-link-chars "]\t\n\r<>")
3513 (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
3515 (defvar org-link-types-re nil
3516 "Matches a link that has a url-like prefix like \"http:\"")
3517 (defvar org-link-re-with-space nil
3518 "Matches a link with spaces, optional angular brackets around it.")
3519 (defvar org-link-re-with-space2 nil
3520 "Matches a link with spaces, optional angular brackets around it.")
3521 (defvar org-link-re-with-space3 nil
3522 "Matches a link with spaces, only for internal part in bracket links.")
3523 (defvar org-angle-link-re nil
3524 "Matches link with angular brackets, spaces are allowed.")
3525 (defvar org-plain-link-re nil
3526 "Matches plain link, without spaces.")
3527 (defvar org-bracket-link-regexp nil
3528 "Matches a link in double brackets.")
3529 (defvar org-bracket-link-analytic-regexp nil
3530 "Regular expression used to analyze links.
3531 Here is what the match groups contain after a match:
3537 (defvar org-any-link-re nil
3538 "Regular expression matching any link.")
3540 (defun org-make-link-regexps ()
3541 "Update the link regular expressions.
3542 This should be called after the variable `org-link-types' has changed."
3543 (setq org-link-types-re
3545 "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
3546 org-link-re-with-space
3548 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3549 "\\([^" org-non-link-chars " ]"
3550 "[^" org-non-link-chars "]*"
3551 "[^" org-non-link-chars " ]\\)>?")
3552 org-link-re-with-space2
3554 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3555 "\\([^" org-non-link-chars " ]"
3557 "[^" org-non-link-chars " ]\\)>?")
3558 org-link-re-with-space3
3560 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3561 "\\([^" org-non-link-chars " ]"
3565 "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3566 "\\([^" org-non-link-chars " ]"
3567 "[^" org-non-link-chars "]*"
3571 "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3572 "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
3573 org-bracket-link-regexp
3574 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
3575 org-bracket-link-analytic-regexp
3578 "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
3581 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
3584 (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
3585 org-angle-link-re "\\)\\|\\("
3586 org-plain-link-re "\\)")))
3588 (org-make-link-regexps)
3590 (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
3591 "Regular expression for fast time stamp matching.")
3592 (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
3593 "Regular expression for fast time stamp matching.")
3594 (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
3595 "Regular expression matching time strings for analysis.
3596 This one does not require the space after the date, so it can be used
3597 on a string that terminates immediately after the date.")
3598 (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
3599 "Regular expression matching time strings for analysis.")
3600 (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
3601 "Regular expression matching time stamps, with groups.")
3602 (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
3603 "Regular expression matching time stamps (also [..]), with groups.")
3604 (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
3605 "Regular expression matching a time stamp range.")
3606 (defconst org-tr-regexp-both
3607 (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
3608 "Regular expression matching a time stamp range.")
3609 (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
3610 org-ts-regexp "\\)?")
3611 "Regular expression matching a time stamp or time stamp range.")
3612 (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
3613 org-ts-regexp-both "\\)?")
3614 "Regular expression matching a time stamp or time stamp range.
3615 The time stamps may be either active or inactive.")
3617 (defvar org-emph-face nil)
3619 (defun org-do-emphasis-faces (limit)
3620 "Run through the buffer and add overlays to links."
3622 (while (and (not rtn) (re-search-forward org-emph-re limit t))
3623 (if (not (= (char-after (match-beginning 3))
3624 (char-after (match-beginning 4))))
3627 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
3629 (nth 1 (assoc (match-string 3)
3630 org-emphasis-alist)))
3631 (add-text-properties (match-beginning 2) (match-end 2)
3632 '(font-lock-multiline t))
3633 (when org-hide-emphasis-markers
3634 (add-text-properties (match-end 4) (match-beginning 5)
3635 '(invisible org-link))
3636 (add-text-properties (match-beginning 3) (match-end 3)
3637 '(invisible org-link)))))
3641 (defun org-emphasize (&optional char)
3642 "Insert or change an emphasis, i.e. a font like bold or italic.
3643 If there is an active region, change that region to a new emphasis.
3644 If there is no region, just insert the marker characters and position
3645 the cursor between them.
3646 CHAR should be either the marker character, or the first character of the
3647 HTML tag associated with that emphasis. If CHAR is a space, the means
3648 to remove the emphasis of the selected region.
3649 If char is not given (for example in an interactive call) it
3650 will be prompted for."
3652 (let ((eal org-emphasis-alist) e det
3653 (erc org-emphasis-regexp-components)
3655 (string "") beg end move tag c s)
3656 (if (org-region-active-p)
3657 (setq beg (region-beginning) end (region-end)
3658 string (buffer-substring beg end))
3661 (while (setq e (pop eal))
3662 (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
3664 (push (cons c (string-to-char (car e))) det)
3665 (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
3666 (substring tag 1)))))
3667 (setq det (nreverse det))
3669 (message "%s" (concat "Emphasis marker or tag:" prompt))
3670 (setq char (read-char-exclusive)))
3671 (setq char (or (cdr (assoc char det)) char))
3672 (if (equal char ?\ )
3673 (setq s "" move nil)
3674 (unless (assoc (char-to-string char) org-emphasis-alist)
3675 (error "No such emphasis marker: \"%c\"" char))
3676 (setq s (char-to-string char)))
3677 (while (and (> (length string) 1)
3678 (equal (substring string 0 1) (substring string -1))
3679 (assoc (substring string 0 1) org-emphasis-alist))
3680 (setq string (substring string 1 -1)))
3681 (setq string (concat s string s))
3682 (if beg (delete-region beg end))
3684 (string-match (concat "[" (nth 0 erc) "\n]")
3685 (char-to-string (char-before (point)))))
3687 (unless (string-match (concat "[" (nth 1 erc) "\n]")
3688 (char-to-string (char-after (point))))
3689 (insert " ") (backward-char 1))
3691 (and move (backward-char 1))))
3693 (defconst org-nonsticky-props
3694 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
3697 (defun org-activate-plain-links (limit)
3698 "Run through the buffer and add overlays to links."
3701 (while (re-search-forward org-plain-link-re limit t)
3702 (setq f (get-text-property (match-beginning 0) 'face))
3703 (if (or (eq f 'org-tag)
3704 (and (listp f) (memq 'org-tag f)))
3706 (add-text-properties (match-beginning 0) (match-end 0)
3707 (list 'mouse-face 'highlight
3708 'rear-nonsticky org-nonsticky-props
3709 'keymap org-mouse-map
3711 (throw 'exit t))))))
3713 (defun org-activate-code (limit)
3714 (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
3716 (remove-text-properties (match-beginning 0) (match-end 0)
3717 '(display t invisible t intangible t))
3720 (defun org-activate-angle-links (limit)
3721 "Run through the buffer and add overlays to links."
3722 (if (re-search-forward org-angle-link-re limit t)
3724 (add-text-properties (match-beginning 0) (match-end 0)
3725 (list 'mouse-face 'highlight
3726 'rear-nonsticky org-nonsticky-props
3727 'keymap org-mouse-map
3731 (defun org-activate-bracket-links (limit)
3732 "Run through the buffer and add overlays to bracketed links."
3733 (if (re-search-forward org-bracket-link-regexp limit t)
3734 (let* ((help (concat "LINK: "
3735 (org-match-string-no-properties 1)))
3736 ;; FIXME: above we should remove the escapes.
3737 ;; but that requires another match, protecting match data,
3738 ;; a lot of overhead for font-lock.
3739 (ip (org-maybe-intangible
3740 (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
3741 'keymap org-mouse-map 'mouse-face 'highlight
3742 'font-lock-multiline t 'help-echo help)))
3743 (vp (list 'rear-nonsticky org-nonsticky-props
3744 'keymap org-mouse-map 'mouse-face 'highlight
3745 ' font-lock-multiline t 'help-echo help)))
3746 ;; We need to remove the invisible property here. Table narrowing
3747 ;; may have made some of this invisible.
3748 (remove-text-properties (match-beginning 0) (match-end 0)
3752 (add-text-properties (match-beginning 0) (match-beginning 3) ip)
3753 (add-text-properties (match-beginning 3) (match-end 3) vp)
3754 (add-text-properties (match-end 3) (match-end 0) ip))
3755 (add-text-properties (match-beginning 0) (match-beginning 1) ip)
3756 (add-text-properties (match-beginning 1) (match-end 1) vp)
3757 (add-text-properties (match-end 1) (match-end 0) ip))
3760 (defun org-activate-dates (limit)
3761 "Run through the buffer and add overlays to dates."
3762 (if (re-search-forward org-tsr-regexp-both limit t)
3764 (add-text-properties (match-beginning 0) (match-end 0)
3765 (list 'mouse-face 'highlight
3766 'rear-nonsticky org-nonsticky-props
3767 'keymap org-mouse-map))
3768 (when org-display-custom-times
3770 (org-display-custom-time (match-beginning 3) (match-end 3)))
3771 (org-display-custom-time (match-beginning 1) (match-end 1)))
3774 (defvar org-target-link-regexp nil
3775 "Regular expression matching radio targets in plain text.")
3776 (make-variable-buffer-local 'org-target-link-regexp)
3777 (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
3778 "Regular expression matching a link target.")
3779 (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
3780 "Regular expression matching a radio target.")
3781 (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
3782 "Regular expression matching any target.")
3784 (defun org-activate-target-links (limit)
3785 "Run through the buffer and add overlays to target matches."
3786 (when org-target-link-regexp
3787 (let ((case-fold-search t))
3788 (if (re-search-forward org-target-link-regexp limit t)
3790 (add-text-properties (match-beginning 0) (match-end 0)
3791 (list 'mouse-face 'highlight
3792 'rear-nonsticky org-nonsticky-props
3793 'keymap org-mouse-map
3794 'help-echo "Radio target link"
3795 'org-linked-text t))
3798 (defun org-update-radio-target-regexp ()
3799 "Find all radio targets in this file and update the regular expression."
3801 (when (memq 'radio org-activate-links)
3802 (setq org-target-link-regexp
3803 (org-make-target-link-regexp (org-all-targets 'radio)))
3804 (org-restart-font-lock)))
3806 (defun org-hide-wide-columns (limit)
3808 (setq s (text-property-any (point) (or limit (point-max))
3811 (setq e (next-single-property-change s 'org-cwidth))
3812 (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
3816 (defvar org-latex-and-specials-regexp nil
3817 "Regular expression for highlighting export special stuff.")
3818 (defvar org-match-substring-regexp)
3819 (defvar org-match-substring-with-braces-regexp)
3820 (defvar org-export-html-special-string-regexps)
3822 (defun org-compute-latex-and-specials-regexp ()
3823 "Compute regular expression for stuff treated specially by exporters."
3824 (if (not org-highlight-latex-fragments-and-specials)
3825 (org-set-local 'org-latex-and-specials-regexp nil)
3828 ((matchers (plist-get org-format-latex-options :matchers))
3829 (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
3830 org-latex-regexps)))
3831 (options (org-combine-plists (org-default-export-plist)
3832 (org-infile-export-plist)))
3833 (org-export-with-sub-superscripts (plist-get options :sub-superscript))
3834 (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
3835 (org-export-with-TeX-macros (plist-get options :TeX-macros))
3836 (org-export-html-expand (plist-get options :expand-quoted-html))
3837 (org-export-with-special-strings (plist-get options :special-strings))
3840 ((equal org-export-with-sub-superscripts '{})
3841 (list org-match-substring-with-braces-regexp))
3842 (org-export-with-sub-superscripts
3843 (list org-match-substring-regexp))
3846 (if org-export-with-LaTeX-fragments
3847 (mapcar (lambda (x) (nth 1 x)) latexs)))
3849 (if org-export-with-TeX-macros
3850 (list (concat "\\\\"
3852 (append (mapcar 'car org-html-entities)
3853 (if (boundp 'org-latex-entities)
3854 org-latex-entities nil))
3857 ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
3858 (re-special (if org-export-with-special-strings
3859 (mapcar (lambda (x) (car x))
3860 org-export-html-special-string-regexps)))
3864 (if org-export-html-expand "@<[^>\n]+>")
3867 'org-latex-and-specials-regexp
3868 (mapconcat 'identity (append re-latex re-sub re-macros re-special
3871 (defun org-do-latex-and-special-faces (limit)
3872 "Run through the buffer and add overlays to links."
3873 (when org-latex-and-specials-regexp
3875 (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
3877 (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
3879 '(org-code org-verbatim underline)))
3882 d (cond ((member (char-after (1+ (match-beginning 0)))
3885 (font-lock-prepend-text-property
3886 (+ d (match-beginning 0)) (match-end 0)
3887 'face 'org-latex-and-export-specials)
3888 (add-text-properties (+ d (match-beginning 0)) (match-end 0)
3889 '(font-lock-multiline t)))))
3892 (defun org-restart-font-lock ()
3893 "Restart font-lock-mode, to force refontification."
3894 (when (and (boundp 'font-lock-mode) font-lock-mode)
3896 (font-lock-mode 1)))
3898 (defun org-all-targets (&optional radio)
3899 "Return a list of all targets in this file.
3900 With optional argument RADIO, only find radio targets."
3901 (let ((re (if radio org-radio-target-regexp org-target-regexp))
3904 (goto-char (point-min))
3905 (while (re-search-forward re nil t)
3906 (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
3909 (defun org-make-target-link-regexp (targets)
3910 "Make regular expression matching all strings in TARGETS.
3911 The regular expression finds the targets also if there is a line break
3918 (while (string-match " +" x)
3919 (setq x (replace-match "\\s-+" t t x)))
3925 (defun org-activate-tags (limit)
3926 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
3928 (add-text-properties (match-beginning 1) (match-end 1)
3929 (list 'mouse-face 'highlight
3930 'rear-nonsticky org-nonsticky-props
3931 'keymap org-mouse-map))
3934 (defun org-outline-level ()
3936 (looking-at outline-regexp)
3937 (if (match-beginning 1)
3938 (+ (org-get-string-indentation (match-string 1)) 1000)
3939 (1- (- (match-end 0) (match-beginning 0))))))
3941 (defvar org-font-lock-keywords nil)
3943 (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
3944 "Regular expression matching a property line.")
3946 (defvar org-font-lock-hook nil
3947 "Functions to be called for special font lock stuff.")
3949 (defun org-font-lock-hook (limit)
3950 (run-hook-with-args 'org-font-lock-hook limit))
3952 (defun org-set-font-lock-defaults ()
3953 (let* ((em org-fontify-emphasized-text)
3954 (lk org-activate-links)
3955 (org-font-lock-extra-keywords
3958 '(org-font-lock-hook)
3960 '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
3961 (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
3963 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
3966 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
3967 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
3968 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
3970 (list org-drawer-regexp '(0 'org-special-keyword t))
3971 (list "^[ \t]*:END:" '(0 'org-special-keyword t))
3973 (list org-property-re
3974 '(1 'org-special-keyword t)
3975 '(3 'org-property-value t))
3976 (if org-format-transports-properties-p
3977 '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
3979 (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
3980 (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
3981 (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
3982 (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
3983 (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
3984 (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
3985 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
3986 '(org-hide-wide-columns (0 nil append))
3988 (list (concat "^\\*+[ \t]+" org-todo-regexp)
3989 '(1 (org-get-todo-face 1) t))
3991 (if org-fontify-done-headline
3992 (list (concat "^[*]+ +\\<\\("
3993 (mapconcat 'regexp-quote org-done-keywords "\\|")
3995 '(2 'org-headline-done t))
3998 (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
4000 '(org-font-lock-add-tag-faces)
4002 (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
4003 (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
4004 (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
4005 (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
4008 (if (featurep 'xemacs)
4009 '(org-do-emphasis-faces (0 nil append))
4010 '(org-do-emphasis-faces)))
4012 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
4014 (if org-provide-checkbox-statistics
4015 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
4016 (0 (org-get-checkbox-statistics-face) t)))
4017 ;; Description list items
4018 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
4020 (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
4021 '(1 'org-archived prepend))
4023 '(org-do-latex-and-special-faces)
4025 '(org-activate-code (1 'org-code t))
4027 (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
4028 "\\|" org-quote-string "\\)\\>")
4029 '(1 'org-special-keyword t))
4030 '("^#.*" (0 'font-lock-comment-face t))
4032 (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
4033 ;; Now set the full font-lock-keywords
4034 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
4035 (org-set-local 'font-lock-defaults
4036 '(org-font-lock-keywords t nil nil backward-paragraph))
4037 (kill-local-variable 'font-lock-keywords) nil))
4042 (defun org-get-level-face (n)
4043 "Get the right face for match N in font-lock matching of headlines."
4044 (setq org-l (- (match-end 2) (match-beginning 1) 1))
4045 (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
4046 (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
4048 ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
4050 (t (if org-level-color-stars-only nil org-f))))
4052 (defun org-get-todo-face (kwd)
4053 "Get the right face for a TODO keyword KWD.
4054 If KWD is a number, get the corresponding match group."
4055 (if (numberp kwd) (setq kwd (match-string kwd)))
4056 (or (cdr (assoc kwd org-todo-keyword-faces))
4057 (and (member kwd org-done-keywords) 'org-done)
4060 (defun org-font-lock-add-tag-faces (limit)
4061 "Add the special tag faces."
4062 (when (and org-tag-faces org-tags-special-faces-re)
4063 (while (re-search-forward org-tags-special-faces-re limit t)
4064 (add-text-properties (match-beginning 1) (match-end 1)
4065 (list 'face (org-get-tag-face 1)
4066 'font-lock-fontified t))
4067 (backward-char 1))))
4069 (defun org-get-tag-face (kwd)
4070 "Get the right face for a TODO keyword KWD.
4071 If KWD is a number, get the corresponding match group."
4072 (if (numberp kwd) (setq kwd (match-string kwd)))
4073 (or (cdr (assoc kwd org-tag-faces))
4076 (defun org-unfontify-region (beg end &optional maybe_loudly)
4077 "Remove fontification and activation overlays from links."
4078 (font-lock-default-unfontify-region beg end)
4079 (let* ((buffer-undo-list t)
4080 (inhibit-read-only t) (inhibit-point-motion-hooks t)
4081 (inhibit-modification-hooks t)
4082 deactivate-mark buffer-file-name buffer-file-truename)
4083 (remove-text-properties beg end
4084 '(mouse-face t keymap t org-linked-text t
4085 invisible t intangible t))))
4087 ;;;; Visibility cycling, including org-goto and indirect buffer
4091 (defvar org-cycle-global-status nil)
4092 (make-variable-buffer-local 'org-cycle-global-status)
4093 (defvar org-cycle-subtree-status nil)
4094 (make-variable-buffer-local 'org-cycle-subtree-status)
4097 (defun org-cycle (&optional arg)
4098 "Visibility cycling for Org-mode.
4100 - When this function is called with a prefix argument, rotate the entire
4101 buffer through 3 states (global cycling)
4102 1. OVERVIEW: Show only top-level headlines.
4103 2. CONTENTS: Show all headlines of all levels, but no body text.
4104 3. SHOW ALL: Show everything.
4105 When called with two C-u C-u prefixes, switch to the startup visibility,
4106 determined by the variable `org-startup-folded', and by any VISIBILITY
4107 properties in the buffer.
4108 When called with three C-u C-u C-u prefixed, show the entire buffer,
4111 - When point is at the beginning of a headline, rotate the subtree started
4112 by this line through 3 different states (local cycling)
4113 1. FOLDED: Only the main headline is shown.
4114 2. CHILDREN: The main headline and the direct children are shown.
4115 From this state, you can move to one of the children
4116 and zoom in further.
4117 3. SUBTREE: Show the entire subtree, including body text.
4119 - When there is a numeric prefix, go up to a heading with level ARG, do
4120 a `show-subtree' and return to the previous cursor position. If ARG
4121 is negative, go up that many levels.
4123 - When point is not at the beginning of a headline, execute the global
4124 binding for TAB, which is re-indenting the line. See the option
4125 `org-cycle-emulate-tab' for details.
4127 - Special case: if point is at the beginning of the buffer and there is
4128 no headline in line 1, this function will act as if called with prefix arg.
4129 But only if also the variable `org-cycle-global-at-bob' is t."
4131 (org-load-modules-maybe)
4132 (let* ((outline-regexp
4133 (if (and (org-mode-p) org-cycle-include-plain-lists)
4134 "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
4136 (bob-special (and org-cycle-global-at-bob (bobp)
4137 (not (looking-at outline-regexp))))
4140 (delq 'org-optimize-window-after-visibility-change
4141 (copy-sequence org-cycle-hook))
4145 (if (or bob-special (equal arg '(4)))
4146 ;; special case: use global cycling
4152 (org-set-startup-visibility)
4153 (message "Startup visibility, plus VISIBILITY properties"))
4157 (message "Entire buffer visible, including drawers"))
4159 ((org-at-table-p 'any)
4160 ;; Enter the table or move to the next field in the table
4161 (or (org-table-recognize-table.el)
4163 (if arg (org-table-edit-field t)
4164 (org-table-justify-field-maybe)
4165 (call-interactively 'org-table-next-field)))))
4167 ((eq arg t) ;; Global cycling
4170 ((and (eq last-command this-command)
4171 (eq org-cycle-global-status 'overview))
4172 ;; We just created the overview - now do table of contents
4173 ;; This can be slow in very large buffers, so indicate action
4174 (message "CONTENTS...")
4176 (message "CONTENTS...done")
4177 (setq org-cycle-global-status 'contents)
4178 (run-hook-with-args 'org-cycle-hook 'contents))
4180 ((and (eq last-command this-command)
4181 (eq org-cycle-global-status 'contents))
4182 ;; We just showed the table of contents - now show everything
4184 (message "SHOW ALL")
4185 (setq org-cycle-global-status 'all)
4186 (run-hook-with-args 'org-cycle-hook 'all))
4189 ;; Default action: go to overview
4191 (message "OVERVIEW")
4192 (setq org-cycle-global-status 'overview)
4193 (run-hook-with-args 'org-cycle-hook 'overview))))
4195 ((and org-drawers org-drawer-regexp
4197 (beginning-of-line 1)
4198 (looking-at org-drawer-regexp)))
4199 ;; Toggle block visibility
4201 (not (get-char-property (match-end 0) 'invisible))))
4204 ;; Show-subtree, ARG levels up from here.
4206 (org-back-to-heading)
4207 (outline-up-heading (if (< arg 0) (- arg)
4208 (- (funcall outline-level) arg)))
4209 (org-show-subtree)))
4211 ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
4212 (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
4213 ;; At a heading: rotate between three different views
4214 (org-back-to-heading)
4215 (let ((goal-column 0) eoh eol eos)
4216 ;; First, some boundaries
4218 (org-back-to-heading)
4220 (beginning-of-line 2)
4221 (while (and (not (eobp)) ;; this is like `next-line'
4222 (get-char-property (1- (point)) 'invisible))
4223 (beginning-of-line 2)) (setq eol (point)))
4224 (outline-end-of-heading) (setq eoh (point))
4225 (org-end-of-subtree t)
4227 (skip-chars-forward " \t\n")
4228 (beginning-of-line 1) ; in case this is an item
4230 (setq eos (1- (point))))
4231 ;; Find out what to do next and set `this-command'
4234 ;; Nothing is hidden behind this heading
4235 (message "EMPTY ENTRY")
4236 (setq org-cycle-subtree-status nil)
4239 (outline-next-heading)
4240 (if (org-invisible-p) (org-flag-heading nil))))
4242 (not (string-match "\\S-" (buffer-substring eol eos))))
4243 ;; Entire subtree is hidden in one line: open it
4246 (message "CHILDREN")
4249 (outline-next-heading)
4250 (if (org-invisible-p) (org-flag-heading nil)))
4251 (setq org-cycle-subtree-status 'children)
4252 (run-hook-with-args 'org-cycle-hook 'children))
4253 ((and (eq last-command this-command)
4254 (eq org-cycle-subtree-status 'children))
4255 ;; We just showed the children, now show everything.
4258 (setq org-cycle-subtree-status 'subtree)
4259 (run-hook-with-args 'org-cycle-hook 'subtree))
4261 ;; Default action: hide the subtree.
4264 (setq org-cycle-subtree-status 'folded)
4265 (run-hook-with-args 'org-cycle-hook 'folded)))))
4267 ;; TAB emulation and template completion
4268 (buffer-read-only (org-back-to-heading))
4270 ((org-try-structure-completion))
4272 ((org-try-cdlatex-tab))
4274 ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
4276 (not (looking-at outline-regexp))))
4277 (call-interactively (global-key-binding "\t")))
4279 ((if (and (memq org-cycle-emulate-tab '(white whitestart))
4280 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
4281 (or (and (eq org-cycle-emulate-tab 'white)
4282 (= (match-end 0) (point-at-eol)))
4283 (and (eq org-cycle-emulate-tab 'whitestart)
4284 (>= (match-end 0) pos))))
4286 (eq org-cycle-emulate-tab t))
4287 (call-interactively (global-key-binding "\t")))
4290 (org-back-to-heading)
4294 (defun org-global-cycle (&optional arg)
4295 "Cycle the global visibility. For details see `org-cycle'.
4296 With C-u prefix arg, switch to startup visibility.
4297 With a numeric prefix, show all headlines up to that level."
4299 (let ((org-cycle-include-plain-lists
4300 (if (org-mode-p) org-cycle-include-plain-lists nil)))
4304 (hide-sublevels arg)
4305 (setq org-cycle-global-status 'contents))
4307 (org-set-startup-visibility)
4308 (message "Startup visibility, plus VISIBILITY properties."))
4310 (org-cycle '(4))))))
4312 (defun org-set-startup-visibility ()
4313 "Set the visibility required by startup options and properties."
4315 ((eq org-startup-folded t)
4317 ((eq org-startup-folded 'content)
4318 (let ((this-command 'org-cycle) (last-command 'org-cycle))
4319 (org-cycle '(4)) (org-cycle '(4)))))
4320 (org-set-visibility-according-to-property 'no-cleanup)
4321 (org-cycle-hide-archived-subtrees 'all)
4322 (org-cycle-hide-drawers 'all)
4323 (org-cycle-show-empty-lines 'all))
4325 (defun org-set-visibility-according-to-property (&optional no-cleanup)
4326 "Switch subtree visibilities according to :VISIBILITY: property."
4330 (goto-char (point-min))
4331 (while (re-search-forward
4332 "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
4334 (setq state (match-string 1))
4336 (org-back-to-heading t)
4340 ((equal state '("fold" "folded"))
4342 ((equal state "children")
4343 (org-show-hidden-entry)
4345 ((equal state "content")
4348 (org-narrow-to-subtree)
4350 ((member state '("all" "showall"))
4353 (org-cycle-hide-archived-subtrees 'all)
4354 (org-cycle-hide-drawers 'all)
4355 (org-cycle-show-empty-lines 'all)))))
4357 (defun org-overview ()
4358 "Switch to overview mode, showing only top-level headlines.
4359 Really, this shows all headlines with level equal or greater than the level
4360 of the first headline in the buffer. This is important, because if the
4361 first headline is not level one, then (hide-sublevels 1) gives confusing
4364 (let ((level (save-excursion
4365 (goto-char (point-min))
4366 (if (re-search-forward (concat "^" outline-regexp) nil t)
4368 (goto-char (match-beginning 0))
4369 (funcall outline-level))))))
4370 (and level (hide-sublevels level))))
4372 (defun org-content (&optional arg)
4373 "Show all headlines in the buffer, like a table of contents.
4374 With numerical argument N, show content up to level N."
4377 ;; Visit all headings and show their offspring
4378 (and (integerp arg) (org-overview))
4379 (goto-char (point-max))
4381 (while (and (progn (condition-case nil
4382 (outline-previous-visible-heading 1)
4383 (error (goto-char (point-min))))
4385 (looking-at outline-regexp))
4387 (show-children (1- arg))
4389 (if (bobp) (throw 'exit nil))))))
4392 (defun org-optimize-window-after-visibility-change (state)
4393 "Adjust the window after a change in outline visibility.
4394 This function is the default value of the hook `org-cycle-hook'."
4395 (when (get-buffer-window (current-buffer))
4397 ; ((eq state 'overview) (org-first-headline-recenter 1))
4398 ; ((eq state 'overview) (org-beginning-of-line))
4399 ((eq state 'content) nil)
4400 ((eq state 'all) nil)
4401 ((eq state 'folded) nil)
4402 ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
4403 ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
4405 (defun org-compact-display-after-subtree-move ()
4408 (if (org-up-heading-safe)
4413 (org-cycle-show-empty-lines 'children)
4414 (org-cycle-hide-drawers 'children))
4417 (defun org-cycle-show-empty-lines (state)
4418 "Show empty lines above all visible headlines.
4419 The region to be covered depends on STATE when called through
4420 `org-cycle-hook'. Lisp program can use t for STATE to get the
4421 entire buffer covered. Note that an empty line is only shown if there
4422 are at least `org-cycle-separator-lines' empty lines before the headline."
4423 (when (> org-cycle-separator-lines 0)
4425 (let* ((n org-cycle-separator-lines)
4427 ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
4428 ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
4429 (t (let ((ns (number-to-string (- n 2))))
4430 (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
4431 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
4434 ((memq state '(overview contents t))
4435 (setq beg (point-min) end (point-max)))
4436 ((memq state '(children folded))
4437 (setq beg (point) end (progn (org-end-of-subtree t t)
4438 (beginning-of-line 2)
4442 (while (re-search-forward re end t)
4443 (if (not (get-char-property (match-end 1) 'invisible))
4444 (outline-flag-region
4445 (match-beginning 1) (match-end 1) nil)))))))
4446 ;; Never hide empty lines at the end of the file.
4448 (goto-char (point-max))
4449 (outline-previous-heading)
4450 (outline-end-of-heading)
4451 (if (and (looking-at "[ \t\n]+")
4452 (= (match-end 0) (point-max)))
4453 (outline-flag-region (point) (match-end 0) nil))))
4455 (defun org-show-empty-lines-in-parent ()
4456 "Move to the parent and re-show empty lines before visible headlines."
4458 (let ((context (if (org-up-heading-safe) 'children 'overview)))
4459 (org-cycle-show-empty-lines context))))
4461 (defun org-cycle-hide-drawers (state)
4462 "Re-hide all drawers after a visibility state change."
4463 (when (and (org-mode-p)
4464 (not (memq state '(overview folded))))
4466 (let* ((globalp (memq state '(contents all)))
4467 (beg (if globalp (point-min) (point)))
4468 (end (if globalp (point-max) (org-end-of-subtree t))))
4470 (while (re-search-forward org-drawer-regexp end t)
4471 (org-flag-drawer t))))))
4473 (defun org-flag-drawer (flag)
4475 (beginning-of-line 1)
4476 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
4477 (let ((b (match-end 0))
4478 (outline-regexp org-outline-regexp))
4479 (if (re-search-forward
4481 (save-excursion (outline-next-heading) (point)) t)
4482 (outline-flag-region b (point-at-eol) flag)
4483 (error ":END: line missing"))))))
4485 (defun org-subtree-end-visible-p ()
4486 "Is the end of the current subtree visible?"
4487 (pos-visible-in-window-p
4488 (save-excursion (org-end-of-subtree t) (point))))
4490 (defun org-first-headline-recenter (&optional N)
4491 "Move cursor to the first headline and recenter the headline.
4492 Optional argument N means, put the headline into the Nth line of the window."
4493 (goto-char (point-min))
4494 (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
4496 (recenter (prefix-numeric-value N))))
4500 (defvar org-goto-window-configuration nil)
4501 (defvar org-goto-marker nil)
4502 (defvar org-goto-map
4503 (let ((map (make-sparse-keymap)))
4504 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
4505 (while (setq cmd (pop cmds))
4506 (substitute-key-definition cmd cmd map global-map)))
4507 (suppress-keymap map)
4508 (org-defkey map "\C-m" 'org-goto-ret)
4509 (org-defkey map [(return)] 'org-goto-ret)
4510 (org-defkey map [(left)] 'org-goto-left)
4511 (org-defkey map [(right)] 'org-goto-right)
4512 (org-defkey map [(control ?g)] 'org-goto-quit)
4513 (org-defkey map "\C-i" 'org-cycle)
4514 (org-defkey map [(tab)] 'org-cycle)
4515 (org-defkey map [(down)] 'outline-next-visible-heading)
4516 (org-defkey map [(up)] 'outline-previous-visible-heading)
4517 (if org-goto-auto-isearch
4518 (if (fboundp 'define-key-after)
4519 (define-key-after map [t] 'org-goto-local-auto-isearch)
4521 (org-defkey map "q" 'org-goto-quit)
4522 (org-defkey map "n" 'outline-next-visible-heading)
4523 (org-defkey map "p" 'outline-previous-visible-heading)
4524 (org-defkey map "f" 'outline-forward-same-level)
4525 (org-defkey map "b" 'outline-backward-same-level)
4526 (org-defkey map "u" 'outline-up-heading))
4527 (org-defkey map "/" 'org-occur)
4528 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
4529 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
4530 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
4531 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
4532 (org-defkey map "\C-c\C-u" 'outline-up-heading)
4535 (defconst org-goto-help
4536 "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
4537 RET=jump to location [Q]uit and return to previous location
4538 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
4540 (defvar org-goto-start-pos) ; dynamically scoped parameter
4542 ;; FIXME: Docstring doe not mention both interfaces
4543 (defun org-goto (&optional alternative-interface)
4544 "Look up a different location in the current file, keeping current visibility.
4546 When you want look-up or go to a different location in a document, the
4547 fastest way is often to fold the entire buffer and then dive into the tree.
4548 This method has the disadvantage, that the previous location will be folded,
4549 which may not be what you want.
4551 This command works around this by showing a copy of the current buffer
4552 in an indirect buffer, in overview mode. You can dive into the tree in
4553 that copy, use org-occur and incremental search to find a location.
4554 When pressing RET or `Q', the command returns to the original buffer in
4555 which the visibility is still unchanged. After RET is will also jump to
4556 the location selected in the indirect buffer and expose the
4557 the headline hierarchy above."
4559 (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
4560 (org-refile-use-outline-path t)
4562 (if (not alternative-interface)
4564 (if (eq org-goto-interface 'outline)
4565 'outline-path-completion
4567 (org-goto-start-pos (point))
4569 (if (eq interface 'outline)
4570 (car (org-get-location (current-buffer) org-goto-help))
4571 (nth 3 (org-refile-get-location "Goto: ")))))
4574 (org-mark-ring-push org-goto-start-pos)
4575 (goto-char selected-point)
4576 (if (or (org-invisible-p) (org-invisible-p2))
4577 (org-show-context 'org-goto)))
4580 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
4581 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
4582 (defvar org-goto-local-auto-isearch-map) ; defined below
4584 (defun org-get-location (buf help)
4585 "Let the user select a location in the Org-mode buffer BUF.
4586 This function uses a recursive edit. It returns the selected position
4588 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
4589 (isearch-hide-immediately nil)
4590 (isearch-search-fun-function
4591 (lambda () 'org-goto-local-search-headings))
4592 (org-goto-selected-point org-goto-exit-command))
4594 (save-window-excursion
4595 (delete-other-windows)
4596 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
4599 (make-indirect-buffer (current-buffer) "*org-goto*")
4600 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
4601 (with-output-to-temp-buffer "*Help*"
4603 (org-fit-window-to-buffer (get-buffer-window "*Help*"))
4604 (setq buffer-read-only nil)
4605 (let ((org-startup-truncated t)
4606 (org-startup-folded nil)
4607 (org-startup-align-all-tables nil))
4610 (setq buffer-read-only t)
4611 (if (and (boundp 'org-goto-start-pos)
4612 (integer-or-marker-p org-goto-start-pos))
4613 (let ((org-show-hierarchy-above t)
4614 (org-show-siblings t)
4615 (org-show-following-heading t))
4616 (goto-char org-goto-start-pos)
4617 (and (org-invisible-p) (org-show-context)))
4618 (goto-char (point-min)))
4619 (org-beginning-of-line)
4620 (message "Select location and press RET")
4621 (use-local-map org-goto-map)
4624 (kill-buffer "*org-goto*")
4625 (cons org-goto-selected-point org-goto-exit-command)))
4627 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
4628 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
4629 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
4630 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
4632 (defun org-goto-local-search-headings (string bound noerror)
4633 "Search and make sure that any matches are in headlines."
4635 (while (if isearch-forward
4636 (search-forward string bound noerror)
4637 (search-backward string bound noerror))
4638 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
4639 (and (member :headline context)
4640 (not (member :tags context))))
4641 (throw 'return (point))))))
4643 (defun org-goto-local-auto-isearch ()
4646 (goto-char (point-min))
4647 (let ((keys (this-command-keys)))
4648 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
4650 (isearch-process-search-char (string-to-char keys)))))
4652 (defun org-goto-ret (&optional arg)
4653 "Finish `org-goto' by going to the new location."
4655 (setq org-goto-selected-point (point)
4656 org-goto-exit-command 'return)
4659 (defun org-goto-left ()
4660 "Finish `org-goto' by going to the new location."
4662 (if (org-on-heading-p)
4664 (beginning-of-line 1)
4665 (setq org-goto-selected-point (point)
4666 org-goto-exit-command 'left)
4668 (error "Not on a heading")))
4670 (defun org-goto-right ()
4671 "Finish `org-goto' by going to the new location."
4673 (if (org-on-heading-p)
4675 (setq org-goto-selected-point (point)
4676 org-goto-exit-command 'right)
4678 (error "Not on a heading")))
4680 (defun org-goto-quit ()
4681 "Finish `org-goto' without cursor motion."
4683 (setq org-goto-selected-point nil)
4684 (setq org-goto-exit-command 'quit)
4687 ;;; Indirect buffer display of subtrees
4689 (defvar org-indirect-dedicated-frame nil
4690 "This is the frame being used for indirect tree display.")
4691 (defvar org-last-indirect-buffer nil)
4693 (defun org-tree-to-indirect-buffer (&optional arg)
4694 "Create indirect buffer and narrow it to current subtree.
4695 With numerical prefix ARG, go up to this level and then take that tree.
4696 If ARG is negative, go up that many levels.
4697 If `org-indirect-buffer-display' is not `new-frame', the command removes the
4698 indirect buffer previously made with this command, to avoid proliferation of
4699 indirect buffers. However, when you call the command with a `C-u' prefix, or
4700 when `org-indirect-buffer-display' is `new-frame', the last buffer
4701 is kept so that you can work with several indirect buffers at the same time.
4702 If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
4703 requests that a new frame be made for the new buffer, so that the dedicated
4704 frame is not changed."
4706 (let ((cbuf (current-buffer))
4707 (cwin (selected-window))
4709 beg end level heading ibuf)
4711 (org-back-to-heading t)
4713 (setq level (org-outline-level))
4714 (if (< arg 0) (setq arg (+ level arg)))
4715 (while (> (setq level (org-outline-level)) arg)
4716 (outline-up-heading 1 t)))
4718 heading (org-get-heading))
4719 (org-end-of-subtree t) (setq end (point)))
4720 (if (and (buffer-live-p org-last-indirect-buffer)
4721 (not (eq org-indirect-buffer-display 'new-frame))
4723 (kill-buffer org-last-indirect-buffer))
4724 (setq ibuf (org-get-indirect-buffer cbuf)
4725 org-last-indirect-buffer ibuf)
4727 ((or (eq org-indirect-buffer-display 'new-frame)
4728 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
4729 (select-frame (make-frame))
4730 (delete-other-windows)
4731 (switch-to-buffer ibuf)
4732 (org-set-frame-title heading))
4733 ((eq org-indirect-buffer-display 'dedicated-frame)
4735 (select-frame (or (and org-indirect-dedicated-frame
4736 (frame-live-p org-indirect-dedicated-frame)
4737 org-indirect-dedicated-frame)
4738 (setq org-indirect-dedicated-frame (make-frame)))))
4739 (delete-other-windows)
4740 (switch-to-buffer ibuf)
4741 (org-set-frame-title (concat "Indirect: " heading)))
4742 ((eq org-indirect-buffer-display 'current-window)
4743 (switch-to-buffer ibuf))
4744 ((eq org-indirect-buffer-display 'other-window)
4745 (pop-to-buffer ibuf))
4746 (t (error "Invalid value.")))
4747 (if (featurep 'xemacs)
4748 (save-excursion (org-mode) (turn-on-font-lock)))
4749 (narrow-to-region beg end)
4752 (and (window-live-p cwin) (select-window cwin))))
4754 (defun org-get-indirect-buffer (&optional buffer)
4755 (setq buffer (or buffer (current-buffer)))
4756 (let ((n 1) (base (buffer-name buffer)) bname)
4757 (while (buffer-live-p
4758 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
4761 (make-indirect-buffer buffer bname 'clone)
4762 (error (make-indirect-buffer buffer bname)))))
4764 (defun org-set-frame-title (title)
4765 "Set the title of the current frame to the string TITLE."
4766 ;; FIXME: how to name a single frame in XEmacs???
4767 (unless (featurep 'xemacs)
4768 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
4770 ;;;; Structure editing
4772 ;;; Inserting headlines
4774 (defun org-insert-heading (&optional force-heading)
4775 "Insert a new heading or item with same depth at point.
4776 If point is in a plain list and FORCE-HEADING is nil, create a new list item.
4777 If point is at the beginning of a headline, insert a sibling before the
4778 current headline. If point is not at the beginning, do not split the line,
4779 but create the new headline after the current line."
4781 (if (= (buffer-size) 0)
4783 (when (or force-heading (not (org-insert-item)))
4784 (let* ((head (save-excursion
4787 (org-back-to-heading)
4790 (blank (cdr (assq 'heading org-blank-before-new-entry)))
4791 pos hide-previous previous-pos)
4793 ((and (org-on-heading-p) (bolp)
4795 (save-excursion (backward-char 1) (not (org-invisible-p)))))
4796 ;; insert before the current line
4797 (open-line (if blank 2 1)))
4801 (backward-char 1) (not (org-invisible-p)))))
4802 ;; insert right here
4805 ;; somewhere in the line
4807 (setq previous-pos (point-at-bol))
4809 (setq hide-previous (org-invisible-p)))
4810 (and org-insert-heading-respect-content (org-show-subtree))
4812 (and (org-get-alist-option org-M-RET-may-split-line 'headline)
4815 (goto-char (point-at-bol))
4816 (and (looking-at org-complex-heading-regexp)
4817 (> p (match-beginning 4)))))))
4820 (org-insert-heading-respect-content
4821 (org-end-of-subtree nil t)
4822 (or (bolp) (newline))
4828 (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
4829 (setq tags (and (match-end 2) (match-string 2)))
4831 (delete-region (match-beginning 1) (match-end 1)))
4832 (setq pos (point-at-bol))
4833 (or split (end-of-line 1))
4834 (delete-horizontal-space)
4835 (newline (if blank 2 1))
4841 (org-set-tags nil 'align))))
4843 (or split (end-of-line 1))
4844 (newline (if blank 2 1)))))))
4845 (insert head) (just-one-space)
4848 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
4849 (when (and org-insert-heading-respect-content hide-previous)
4851 (goto-char previous-pos)
4853 (run-hooks 'org-insert-heading-hook)))))
4855 (defun org-get-heading (&optional no-tags)
4856 "Return the heading of the current entry, without the stars."
4858 (org-back-to-heading t)
4861 (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
4862 "\\*+[ \t]+\\([^\r\n]*\\)"))
4863 (match-string 1) "")))
4865 (defun org-insert-heading-after-current ()
4866 "Insert a new heading with same level as current, after current subtree."
4868 (org-back-to-heading)
4869 (org-insert-heading)
4870 (org-move-subtree-down)
4873 (defun org-insert-heading-respect-content ()
4875 (let ((org-insert-heading-respect-content t))
4876 (org-insert-heading t)))
4878 (defun org-insert-todo-heading-respect-content (&optional force-state)
4880 (let ((org-insert-heading-respect-content t))
4881 (org-insert-todo-heading force-state t)))
4883 (defun org-insert-todo-heading (arg &optional force-heading)
4884 "Insert a new heading with the same level and TODO state as current heading.
4885 If the heading has no TODO state, or if the state is DONE, use the first
4886 state (TODO by default). Also with prefix arg, force first state."
4888 (when (or force-heading (not (org-insert-item 'checkbox)))
4889 (org-insert-heading force-heading)
4891 (org-back-to-heading)
4892 (outline-previous-heading)
4893 (looking-at org-todo-line-regexp))
4895 (not (match-beginning 2))
4896 (member (match-string 2) org-done-keywords))
4897 (insert (car org-todo-keywords-1) " ")
4898 (insert (match-string 2) " "))
4899 (when org-provide-todo-statistics
4900 (org-update-parent-todo-statistics))))
4902 (defun org-insert-subheading (arg)
4903 "Insert a new subheading and demote it.
4904 Works for outline headings and for plain lists alike."
4906 (org-insert-heading arg)
4908 ((org-on-heading-p) (org-do-demote))
4909 ((org-at-item-p) (org-indent-item 1))))
4911 (defun org-insert-todo-subheading (arg)
4912 "Insert a new subheading with TODO keyword or checkbox and demote it.
4913 Works for outline headings and for plain lists alike."
4915 (org-insert-todo-heading arg)
4917 ((org-on-heading-p) (org-do-demote))
4918 ((org-at-item-p) (org-indent-item 1))))
4920 ;;; Promotion and Demotion
4922 (defun org-promote-subtree ()
4923 "Promote the entire subtree.
4924 See also `org-promote'."
4927 (org-map-tree 'org-promote))
4928 (org-fix-position-after-promote))
4930 (defun org-demote-subtree ()
4931 "Demote the entire subtree. See `org-demote'.
4932 See also `org-promote'."
4935 (org-map-tree 'org-demote))
4936 (org-fix-position-after-promote))
4939 (defun org-do-promote ()
4940 "Promote the current heading higher up the tree.
4941 If the region is active in `transient-mark-mode', promote all headings
4945 (if (org-region-active-p)
4946 (org-map-region 'org-promote (region-beginning) (region-end))
4948 (org-fix-position-after-promote))
4950 (defun org-do-demote ()
4951 "Demote the current heading lower down the tree.
4952 If the region is active in `transient-mark-mode', demote all headings
4956 (if (org-region-active-p)
4957 (org-map-region 'org-demote (region-beginning) (region-end))
4959 (org-fix-position-after-promote))
4961 (defun org-fix-position-after-promote ()
4962 "Make sure that after pro/demotion cursor position is right."
4963 (let ((pos (point)))
4964 (when (save-excursion
4965 (beginning-of-line 1)
4966 (looking-at org-todo-line-regexp)
4967 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
4968 (cond ((eobp) (insert " "))
4969 ((eolp) (insert " "))
4970 ((equal (char-after) ?\ ) (forward-char 1))))))
4972 (defun org-reduced-level (l)
4973 (if org-odd-levels-only (1+ (floor (/ l 2))) l))
4975 (defun org-get-valid-level (level &optional change)
4976 "Rectify a level change under the influence of `org-odd-levels-only'
4977 LEVEL is a current level, CHANGE is by how much the level should be
4978 modified. Even if CHANGE is nil, LEVEL may be returned modified because
4979 even level numbers will become the next higher odd number."
4980 (if org-odd-levels-only
4981 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
4982 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
4983 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
4984 (max 1 (+ level change))))
4986 (if (boundp 'define-obsolete-function-alias)
4987 (if (or (featurep 'xemacs) (< emacs-major-version 23))
4988 (define-obsolete-function-alias 'org-get-legal-level
4989 'org-get-valid-level)
4990 (define-obsolete-function-alias 'org-get-legal-level
4991 'org-get-valid-level "23.1")))
4993 (defun org-promote ()
4994 "Promote the current heading higher up the tree.
4995 If the region is active in `transient-mark-mode', promote all headings
4997 (org-back-to-heading t)
4998 (let* ((level (save-match-data (funcall outline-level)))
4999 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
5000 (diff (abs (- level (length up-head) -1))))
5001 (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
5002 (replace-match up-head nil t)
5003 ;; Fixup tag positioning
5004 (and org-auto-align-tags (org-set-tags nil t))
5005 (if org-adapt-indentation (org-fixup-indentation (- diff)))))
5007 (defun org-demote ()
5008 "Demote the current heading lower down the tree.
5009 If the region is active in `transient-mark-mode', demote all headings
5011 (org-back-to-heading t)
5012 (let* ((level (save-match-data (funcall outline-level)))
5013 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
5014 (diff (abs (- level (length down-head) -1))))
5015 (replace-match down-head nil t)
5016 ;; Fixup tag positioning
5017 (and org-auto-align-tags (org-set-tags nil t))
5018 (if org-adapt-indentation (org-fixup-indentation diff))))
5020 (defun org-map-tree (fun)
5021 "Call FUN for every heading underneath the current one."
5022 (org-back-to-heading)
5023 (let ((level (funcall outline-level)))
5027 (outline-next-heading)
5028 (> (funcall outline-level) level))
5032 (defun org-map-region (fun beg end)
5033 "Call FUN for every heading between BEG and END."
5034 (let ((org-ignore-region t))
5036 (setq end (copy-marker end))
5038 (if (and (re-search-forward (concat "^" outline-regexp) nil t)
5042 (outline-next-heading)
5047 (defun org-fixup-indentation (diff)
5048 "Change the indentation in the current entry by DIFF
5049 However, if any line in the current entry has no indentation, or if it
5050 would end up with no indentation after the change, nothing at all is done."
5052 (let ((end (save-excursion (outline-next-heading)
5054 (prohibit (if (> diff 0)
5056 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
5058 (unless (save-excursion (end-of-line 1)
5059 (re-search-forward prohibit end t))
5060 (while (and (< (point) end)
5061 (re-search-forward "^[ \t]+" end t))
5062 (goto-char (match-end 0))
5063 (setq col (current-column))
5064 (if (< diff 0) (replace-match ""))
5065 (org-indent-to-column (+ diff col))))
5066 (move-marker end nil))))
5068 (defun org-convert-to-odd-levels ()
5069 "Convert an org-mode file with all levels allowed to one with odd levels.
5070 This will leave level 1 alone, convert level 2 to level 3, level 3 to
5073 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
5074 (let ((org-odd-levels-only nil) n)
5076 (goto-char (point-min))
5077 (while (re-search-forward "^\\*\\*+ " nil t)
5078 (setq n (- (length (match-string 0)) 2))
5079 (while (>= (setq n (1- n)) 0)
5081 (end-of-line 1))))))
5084 (defun org-convert-to-oddeven-levels ()
5085 "Convert an org-mode file with only odd levels to one with odd and even levels.
5086 This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
5087 section with an even level, conversion would destroy the structure of the file. An error
5088 is signaled in this case."
5090 (goto-char (point-min))
5091 ;; First check if there are no even levels
5092 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
5093 (org-show-context t)
5094 (error "Not all levels are odd in this file. Conversion not possible."))
5095 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
5096 (let ((org-odd-levels-only nil) n)
5098 (goto-char (point-min))
5099 (while (re-search-forward "^\\*\\*+ " nil t)
5100 (setq n (/ (1- (length (match-string 0))) 2))
5101 (while (>= (setq n (1- n)) 0)
5103 (end-of-line 1))))))
5105 (defun org-tr-level (n)
5106 "Make N odd if required."
5107 (if org-odd-levels-only (1+ (/ n 2)) n))
5109 ;;; Vertical tree motion, cutting and pasting of subtrees
5111 (defun org-move-subtree-up (&optional arg)
5112 "Move the current subtree up past ARG headlines of the same level."
5114 (org-move-subtree-down (- (prefix-numeric-value arg))))
5116 (defun org-move-subtree-down (&optional arg)
5117 "Move the current subtree down past ARG headlines of the same level."
5119 (setq arg (prefix-numeric-value arg))
5120 (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
5121 'outline-get-last-sibling))
5122 (ins-point (make-marker))
5124 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
5126 (org-back-to-heading)
5129 (setq ne-beg (org-back-over-empty-lines))
5132 (save-excursion (outline-end-of-heading)
5133 (setq folded (org-invisible-p)))
5134 (outline-end-of-subtree))
5135 (outline-next-heading)
5136 (setq ne-end (org-back-over-empty-lines))
5139 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
5140 ;; include less whitespace
5143 (forward-line (- ne-beg ne-end))
5144 (setq beg (point))))
5145 ;; Find insertion point, with error handling
5147 (or (and (funcall movfunc) (looking-at outline-regexp))
5148 (progn (goto-char beg0)
5149 (error "Cannot move past superior level or buffer limit")))
5150 (setq cnt (1- cnt)))
5152 ;; Moving forward - still need to move over subtree
5153 (progn (org-end-of-subtree t t)
5155 (org-back-over-empty-lines)
5156 (or (bolp) (newline)))))
5157 (setq ne-ins (org-back-over-empty-lines))
5158 (move-marker ins-point (point))
5159 (setq txt (buffer-substring beg end))
5160 (org-save-markers-in-region beg end)
5161 (delete-region beg end)
5162 (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
5163 (or (bobp) (outline-flag-region (1- (point)) (point) nil))
5164 (let ((bbb (point)))
5165 (insert-before-markers txt)
5166 (org-reinstall-markers-in-region bbb)
5167 (move-marker ins-point bbb))
5168 (or (bolp) (insert "\n"))
5169 (setq ins-end (point))
5170 (goto-char ins-point)
5171 (org-skip-whitespace)
5172 (when (and (< arg 0)
5173 (org-first-sibling-p)
5175 ;; Move whitespace back to beginning
5178 (let ((kill-whole-line t))
5179 (kill-line (- ne-ins ne-beg)) (point)))
5180 (insert (make-string (- ne-ins ne-beg) ?\n)))
5181 (move-marker ins-point nil)
5182 (org-compact-display-after-subtree-move)
5183 (org-show-empty-lines-in-parent)
5187 (org-cycle-hide-drawers 'children))))
5189 (defvar org-subtree-clip ""
5190 "Clipboard for cut and paste of subtrees.
5191 This is actually only a copy of the kill, because we use the normal kill
5192 ring. We need it to check if the kill was created by `org-copy-subtree'.")
5194 (defvar org-subtree-clip-folded nil
5195 "Was the last copied subtree folded?
5196 This is used to fold the tree back after pasting.")
5198 (defun org-cut-subtree (&optional n)
5199 "Cut the current subtree into the clipboard.
5200 With prefix arg N, cut this many sequential subtrees.
5201 This is a short-hand for marking the subtree and then cutting it."
5203 (org-copy-subtree n 'cut))
5205 (defun org-copy-subtree (&optional n cut force-store-markers)
5206 "Cut the current subtree into the clipboard.
5207 With prefix arg N, cut this many sequential subtrees.
5208 This is a short-hand for marking the subtree and then copying it.
5209 If CUT is non-nil, actually cut the subtree.
5210 If FORCE-STORE-MARKERS is non-nil, store the relative locations
5211 of some markers in the region, even if CUT is non-nil. This is
5212 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
5214 (let (beg end folded (beg0 (point)))
5216 (org-back-to-heading nil) ; take what looks like a subtree
5217 (org-back-to-heading t)) ; take what is really there
5218 (org-back-over-empty-lines)
5220 (skip-chars-forward " \t\r\n")
5222 (save-excursion (outline-end-of-heading)
5223 (setq folded (org-invisible-p)))
5225 (outline-forward-same-level (1- n))
5227 (org-end-of-subtree t t))
5228 (org-back-over-empty-lines)
5232 (setq org-subtree-clip-folded folded)
5233 (when (or cut force-store-markers)
5234 (org-save-markers-in-region beg end))
5235 (if cut (kill-region beg end) (copy-region-as-kill beg end))
5236 (setq org-subtree-clip (current-kill 0))
5237 (message "%s: Subtree(s) with %d characters"
5238 (if cut "Cut" "Copied")
5239 (length org-subtree-clip)))))
5241 (defun org-paste-subtree (&optional level tree for-yank)
5242 "Paste the clipboard as a subtree, with modification of headline level.
5243 The entire subtree is promoted or demoted in order to match a new headline
5246 If the cursor is at the beginning of a headline, the same level as
5247 that headline is used to paste the tree
5249 If not, the new level is derived from the *visible* headings
5250 before and after the insertion point, and taken to be the inferior headline
5251 level of the two. So if the previous visible heading is level 3 and the
5252 next is level 4 (or vice versa), level 4 will be used for insertion.
5253 This makes sure that the subtree remains an independent subtree and does
5254 not swallow low level entries.
5256 You can also force a different level, either by using a numeric prefix
5257 argument, or by inserting the heading marker by hand. For example, if the
5258 cursor is after \"*****\", then the tree will be shifted to level 5.
5260 If optional TREE is given, use this text instead of the kill ring.
5262 When FOR-YANK is set, this is called by `org-yank'. In this case, do not
5263 move back over whitespace before inserting, and move point to the end of
5264 the inserted text when done."
5266 (unless (org-kill-is-subtree-p tree)
5268 (substitute-command-keys
5269 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
5270 (let* ((visp (not (org-invisible-p)))
5271 (txt (or tree (and kill-ring (current-kill 0))))
5272 (^re (concat "^\\(" outline-regexp "\\)"))
5273 (re (concat "\\(" outline-regexp "\\)"))
5274 (^re_ (concat "\\(\\*+\\)[ \t]*"))
5276 (old-level (if (string-match ^re txt)
5277 (- (match-end 0) (match-beginning 0) 1)
5279 (force-level (cond (level (prefix-numeric-value level))
5280 ((and (looking-at "[ \t]*$")
5282 ^re_ (buffer-substring
5283 (point-at-bol) (point))))
5284 (- (match-end 1) (match-beginning 1)))
5286 (looking-at org-outline-regexp))
5287 (- (match-end 0) (point) 1))
5289 (previous-level (save-excursion
5292 (outline-previous-visible-heading 1)
5294 (- (match-end 0) (match-beginning 0) 1)
5297 (next-level (save-excursion
5300 (or (looking-at outline-regexp)
5301 (outline-next-visible-heading 1))
5303 (- (match-end 0) (match-beginning 0) 1)
5306 (new-level (or force-level (max previous-level next-level)))
5307 (shift (if (or (= old-level -1)
5309 (= old-level new-level))
5311 (- new-level old-level)))
5312 (delta (if (> shift 0) -1 1))
5313 (func (if (> shift 0) 'org-demote 'org-promote))
5314 (org-odd-levels-only nil)
5316 ;; Remove the forced level indicator
5318 (delete-region (point-at-bol) (point)))
5320 (beginning-of-line 1)
5321 (unless for-yank (org-back-over-empty-lines))
5323 (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
5324 (insert-before-markers txt)
5325 (unless (string-match "\n\\'" txt) (insert "\n"))
5326 (setq newend (point))
5327 (org-reinstall-markers-in-region beg)
5330 (skip-chars-forward " \t\n\r")
5332 (if (and (org-invisible-p) visp)
5333 (save-excursion (outline-show-heading)))
5334 ;; Shift if necessary
5337 (narrow-to-region beg end)
5338 (while (not (= shift 0))
5339 (org-map-region func (point-min) (point-max))
5340 (setq shift (+ delta shift)))
5341 (goto-char (point-min))
5342 (setq newend (point-max))))
5343 (when (or (interactive-p) for-yank)
5344 (message "Clipboard pasted as level %d subtree" new-level))
5345 (if (and (not for-yank) ; in this case, org-yank will decide about folding
5347 (eq org-subtree-clip (current-kill 0))
5348 org-subtree-clip-folded)
5349 ;; The tree was folded before it was killed/copied
5351 (and for-yank (goto-char newend))))
5353 (defun org-kill-is-subtree-p (&optional txt)
5354 "Check if the current kill is an outline subtree, or a set of trees.
5355 Returns nil if kill does not start with a headline, or if the first
5356 headline level is not the largest headline level in the tree.
5357 So this will actually accept several entries of equal levels as well,
5358 which is OK for `org-paste-subtree'.
5359 If optional TXT is given, check this string instead of the current kill."
5360 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
5361 (start-level (and kill
5362 (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
5363 org-outline-regexp "\\)")
5365 (- (match-end 2) (match-beginning 2) 1)))
5366 (re (concat "^" org-outline-regexp))
5367 (start (1+ (or (match-beginning 2) -1))))
5368 (if (not start-level)
5370 nil) ;; does not even start with a heading
5372 (while (setq start (string-match re kill (1+ start)))
5373 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
5377 (defvar org-markers-to-move nil
5378 "Markers that should be moved with a cut-and-paste operation.
5379 Those markers are stored together with their positions relative to
5380 the start of the region.")
5382 (defun org-save-markers-in-region (beg end)
5383 "Check markers in region.
5384 If these markers are between BEG and END, record their position relative
5385 to BEG, so that after moving the block of text, we can put the markers back
5387 This function gets called just before an entry or tree gets cut from the
5388 buffer. After re-insertion, `org-reinstall-markers-in-region' must be
5389 called immediately, to move the markers with the entries."
5390 (setq org-markers-to-move nil)
5391 (when (featurep 'org-clock)
5392 (org-clock-save-markers-for-cut-and-paste beg end))
5393 (when (featurep 'org-agenda)
5394 (org-agenda-save-markers-for-cut-and-paste beg end)))
5396 (defun org-check-and-save-marker (marker beg end)
5397 "Check if MARKER is between BEG and END.
5398 If yes, remember the marker and the distance to BEG."
5399 (when (and (marker-buffer marker)
5400 (equal (marker-buffer marker) (current-buffer)))
5401 (if (and (>= marker beg) (< marker end))
5402 (push (cons marker (- marker beg)) org-markers-to-move))))
5404 (defun org-reinstall-markers-in-region (beg)
5405 "Move all remembered markers to their position relative to BEG."
5407 (move-marker (car x) (+ beg (cdr x))))
5408 org-markers-to-move)
5409 (setq org-markers-to-move nil))
5411 (defun org-narrow-to-subtree ()
5412 "Narrow buffer to the current subtree."
5417 (progn (org-back-to-heading) (point))
5418 (progn (org-end-of-subtree t) (point))))))
5423 (defun org-sort (with-case)
5424 "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
5425 Optional argument WITH-CASE means sort case-sensitively."
5427 (if (org-at-table-p)
5428 (org-call-with-arg 'org-table-sort-lines with-case)
5429 (org-call-with-arg 'org-sort-entries-or-items with-case)))
5431 (defun org-sort-remove-invisible (s)
5432 (remove-text-properties 0 (length s) org-rm-props s)
5433 (while (string-match org-bracket-link-regexp s)
5434 (setq s (replace-match (if (match-end 2)
5436 (match-string 1 s)) t t s)))
5439 (defvar org-priority-regexp) ; defined later in the file
5441 (defun org-sort-entries-or-items
5442 (&optional with-case sorting-type getkey-func compare-func property)
5443 "Sort entries on a certain level of an outline tree.
5444 If there is an active region, the entries in the region are sorted.
5445 Else, if the cursor is before the first entry, sort the top-level items.
5446 Else, the children of the entry at point are sorted.
5448 Sorting can be alphabetically, numerically, and by date/time as given by
5449 the first time stamp in the entry. The command prompts for the sorting
5450 type unless it has been given to the function through the SORTING-TYPE
5451 argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
5452 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
5453 called with point at the beginning of the record. It must return either
5454 a string or a number that should serve as the sorting key for that record.
5456 Comparing entries ignores case by default. However, with an optional argument
5457 WITH-CASE, the sorting considers case as well."
5459 (let ((case-func (if with-case 'identity 'downcase))
5460 start beg end stars re re2
5461 txt what tmp plain-list-p)
5462 ;; Find beginning and end of region to sort
5464 ((org-region-active-p)
5465 ;; we will sort the region
5466 (setq end (region-end)
5468 (goto-char (region-beginning))
5469 (if (not (org-on-heading-p)) (outline-next-heading))
5470 (setq start (point)))
5472 ;; we will sort this plain list
5473 (org-beginning-of-item-list) (setq start (point))
5474 (org-end-of-item-list) (setq end (point))
5476 (setq plain-list-p t
5478 ((or (org-on-heading-p)
5479 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
5480 ;; we will sort the children of the current headline
5481 (org-back-to-heading)
5483 end (progn (org-end-of-subtree t t)
5484 (org-back-over-empty-lines)
5489 (outline-next-heading))
5491 ;; we will sort the top-level entries in this file
5492 (goto-char (point-min))
5493 (or (org-on-heading-p) (outline-next-heading))
5494 (setq start (point) end (point-max) what "top-level")
5499 (if (>= beg end) (error "Nothing to sort"))
5501 (unless plain-list-p
5502 (looking-at "\\(\\*+\\)")
5503 (setq stars (match-string 1)
5504 re (concat "^" (regexp-quote stars) " +")
5505 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
5506 txt (buffer-substring beg end))
5507 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
5508 (if (and (not (equal stars "*")) (string-match re2 txt))
5509 (error "Region to sort contains a level above the first entry")))
5511 (unless sorting-type
5514 "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
5515 "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:")
5517 (setq sorting-type (read-char-exclusive))
5519 (and (= (downcase sorting-type) ?f)
5521 (org-ido-completing-read "Sort using function: "
5522 obarray 'fboundp t nil nil))
5523 (setq getkey-func (intern getkey-func)))
5525 (and (= (downcase sorting-type) ?r)
5527 (org-ido-completing-read "Property: "
5528 (mapcar 'list (org-buffer-property-keys t))
5531 (message "Sorting entries...")
5534 (narrow-to-region start end)
5536 (let ((dcst (downcase sorting-type))
5537 (now (current-time)))
5539 (/= dcst sorting-type)
5540 ;; This function moves to the beginning character of the "record" to
5544 (if (org-at-item-p) t (goto-char (point-max))))
5546 (if (re-search-forward re nil t)
5547 (goto-char (match-beginning 0))
5548 (goto-char (point-max)))))
5549 ;; This function moves to the last character of the "record" being
5556 (outline-forward-same-level 1)
5558 (goto-char (point-max)))))))
5560 ;; This function returns the value that gets sorted against.
5563 (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
5566 (string-to-number (buffer-substring (match-end 0)
5569 (buffer-substring (match-end 0) (point-at-eol)))
5571 (if (re-search-forward org-ts-regexp
5573 (org-time-string-to-time (match-string 0))
5578 (setq tmp (funcall getkey-func))
5579 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
5581 (error "Invalid key function `%s'" getkey-func)))
5582 (t (error "Invalid sorting type `%c'" sorting-type)))))
5586 (if (looking-at org-complex-heading-regexp)
5587 (string-to-number (match-string 4))
5590 (if (looking-at org-complex-heading-regexp)
5591 (funcall case-func (match-string 4))
5594 (if (re-search-forward org-ts-regexp
5598 (org-time-string-to-time (match-string 0))
5601 (if (re-search-forward org-priority-regexp (point-at-eol) t)
5602 (string-to-char (match-string 2))
5603 org-default-priority))
5605 (or (org-entry-get nil property) ""))
5607 (if (looking-at org-complex-heading-regexp)
5608 (- 9999 (length (member (match-string 2)
5609 org-todo-keywords-1)))))
5613 (setq tmp (funcall getkey-func))
5614 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
5616 (error "Invalid key function `%s'" getkey-func)))
5617 (t (error "Invalid sorting type `%c'" sorting-type)))))
5620 ((= dcst ?a) 'string<)
5621 ((= dcst ?t) 'time-less-p)
5622 ((= dcst ?f) compare-func)
5624 (message "Sorting entries...done")))
5626 (defun org-do-sort (table what &optional with-case sorting-type)
5627 "Sort TABLE of WHAT according to SORTING-TYPE.
5628 The user will be prompted for the SORTING-TYPE if the call to this
5629 function does not specify it. WHAT is only for the prompt, to indicate
5630 what is being sorted. The sorting key will be extracted from
5631 the car of the elements of the table.
5632 If WITH-CASE is non-nil, the sorting will be case-sensitive."
5633 (unless sorting-type
5635 "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
5637 (setq sorting-type (read-char-exclusive)))
5638 (let ((dcst (downcase sorting-type))
5639 extractfun comparefun)
5640 ;; Define the appropriate functions
5643 (setq extractfun 'string-to-number
5644 comparefun (if (= dcst sorting-type) '< '>)))
5646 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
5647 (lambda(x) (downcase (org-sort-remove-invisible x))))
5648 comparefun (if (= dcst sorting-type)
5650 (lambda (a b) (and (not (string< a b))
5651 (not (string= a b)))))))
5655 (if (string-match org-ts-regexp x)
5657 (org-time-string-to-time (match-string 0 x)))
5659 comparefun (if (= dcst sorting-type) '< '>)))
5660 (t (error "Invalid sorting type `%c'" sorting-type)))
5662 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
5664 (lambda (a b) (funcall comparefun (car a) (car b))))))
5666 ;;; Editing source examples
5668 (defvar org-exit-edit-mode-map (make-sparse-keymap))
5669 (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
5670 (defvar org-edit-src-force-single-line nil)
5671 (defvar org-edit-src-from-org-mode nil)
5672 (defvar org-edit-src-picture nil)
5674 (define-minor-mode org-exit-edit-mode
5675 "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
5677 (defun org-edit-src-code ()
5678 "Edit the source code example at point.
5679 An indirect buffer is created, and that buffer is then narrowed to the
5680 example at point and switched to the correct language mode. When done,
5681 exit by killing the buffer with \\[org-edit-src-exit]."
5683 (let ((line (org-current-line))
5684 (case-fold-search t)
5685 (msg (substitute-command-keys
5686 "Edit, then exit with C-c ' (C-c and single quote)"))
5687 (info (org-edit-src-find-region-and-lang))
5688 (org-mode-p (eq major-mode 'org-mode))
5689 beg end lang lang-f single)
5692 (setq beg (nth 0 info)
5696 lang-f (intern (concat lang "-mode")))
5697 (unless (functionp lang-f)
5698 (error "No such language mode: %s" lang-f))
5700 (if (get-buffer "*Org Edit Src Example*")
5701 (kill-buffer "*Org Edit Src Example*"))
5702 (switch-to-buffer (make-indirect-buffer (current-buffer)
5703 "*Org Edit Src Example*"))
5704 (narrow-to-region beg end)
5705 (remove-text-properties beg end '(display nil invisible nil
5707 (let ((org-inhibit-startup t))
5709 (set (make-local-variable 'org-edit-src-force-single-line) single)
5710 (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
5712 (goto-char (point-min))
5713 (while (re-search-forward "^," nil t)
5714 (replace-match "")))
5716 (org-exit-edit-mode)
5717 (org-set-local 'header-line-format msg)
5721 (defun org-edit-fixed-width-region ()
5722 "Edit the fixed-width ascii drawing at point.
5723 This must be a region where each line starts with a colon followed by
5725 An indirect buffer is created, and that buffer is then narrowed to the
5726 example at point and switched to artist-mode. When done,
5727 exit by killing the buffer with \\[org-edit-src-exit]."
5729 (let ((line (org-current-line))
5730 (case-fold-search t)
5731 (msg (substitute-command-keys
5732 "Edit, then exit with C-c ' (C-c and single quote)"))
5733 (org-mode-p (eq major-mode 'org-mode))
5734 beg end lang lang-f)
5735 (beginning-of-line 1)
5736 (if (looking-at "[ \t]*[^:\n \t]")
5738 (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
5739 (setq beg (point) end beg)
5741 (if (re-search-backward "^[ \t]*[^:]" nil 'move)
5742 (setq beg (point-at-bol 2))
5743 (setq beg (point))))
5745 (if (re-search-forward "^[ \t]*[^:]" nil 'move)
5746 (setq end (1- (match-beginning 0)))
5747 (setq end (point))))
5749 (if (get-buffer "*Org Edit Picture*")
5750 (kill-buffer "*Org Edit Picture*"))
5751 (switch-to-buffer (make-indirect-buffer (current-buffer)
5752 "*Org Edit Picture*"))
5753 (narrow-to-region beg end)
5754 (remove-text-properties beg end '(display nil invisible nil
5756 (when (fboundp 'font-lock-unfontify-region)
5757 (font-lock-unfontify-region (point-min) (point-max)))
5759 ((eq org-edit-fixed-width-region-mode 'artist-mode)
5762 (t (funcall org-edit-fixed-width-region-mode)))
5763 (set (make-local-variable 'org-edit-src-force-single-line) nil)
5764 (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
5765 (set (make-local-variable 'org-edit-src-picture) t)
5766 (goto-char (point-min))
5767 (while (re-search-forward "^[ \t]*: ?" nil t)
5770 (org-exit-edit-mode)
5771 (org-set-local 'header-line-format msg)
5776 (defun org-edit-src-find-region-and-lang ()
5777 "Find the region and language for a local edit.
5778 Return a list with beginning and end of the region, a string representing
5779 the language, a switch telling of the content should be in a single line."
5782 org-edit-src-region-extra
5784 ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
5785 ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
5786 ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
5787 ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
5788 ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
5789 ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
5790 ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
5791 ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
5792 ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
5793 ("^#\\+html:" "\n" "html" single-line)
5794 ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
5795 ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
5796 ("^#\\+latex:" "\n" "latex" single-line)
5797 ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
5798 ("^#\\+ascii:" "\n" "ascii" single-line)
5801 re re1 re2 single beg end lang)
5803 (while (setq entry (pop re-list))
5804 (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
5805 single (nth 3 entry))
5807 (if (or (looking-at re1)
5808 (re-search-backward re1 nil t))
5810 (setq beg (match-end 0) lang (org-edit-src-get-lang lang))
5811 (if (and (re-search-forward re2 nil t)
5812 (>= (match-end 0) pos))
5813 (throw 'exit (list beg (match-beginning 0) lang single))))
5814 (if (or (looking-at re2)
5815 (re-search-forward re2 nil t))
5817 (setq end (match-beginning 0))
5818 (if (and (re-search-backward re1 nil t)
5819 (<= (match-beginning 0) pos))
5821 (list (match-end 0) end
5822 (org-edit-src-get-lang lang) single)))))))))))
5824 (defun org-edit-src-get-lang (lang)
5825 "Extract the src language."
5826 (let ((m (match-string 0)))
5828 ((stringp lang) lang)
5829 ((integerp lang) (match-string lang))
5830 ((and (eq lang 'lang)
5831 (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
5833 ((and (eq lang 'style)
5834 (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
5836 (t "fundamental"))))
5838 (defun org-edit-src-exit ()
5839 "Exit special edit and protect problematic lines."
5841 (unless (buffer-base-buffer (current-buffer))
5842 (error "This is not an indirect buffer, something is wrong..."))
5843 (unless (> (point-min) 1)
5844 (error "This buffer is not narrowed, something is wrong..."))
5845 (goto-char (point-min))
5846 (if (looking-at "[ \t\n]*\n") (replace-match ""))
5847 (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
5848 (when (org-bound-and-true-p org-edit-src-force-single-line)
5849 (goto-char (point-min))
5850 (while (re-search-forward "\n" nil t)
5851 (replace-match " "))
5852 (goto-char (point-min))
5853 (if (looking-at "\\s-*") (replace-match " "))
5854 (if (re-search-forward "\\s-+\\'" nil t)
5855 (replace-match "")))
5856 (when (org-bound-and-true-p org-edit-src-from-org-mode)
5857 (goto-char (point-min))
5858 (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
5859 (replace-match ",\\1"))
5860 (when font-lock-mode
5861 (font-lock-unfontify-region (point-min) (point-max)))
5862 (put-text-property (point-min) (point-max) 'font-lock-fontified t))
5863 (when (org-bound-and-true-p org-edit-src-picture)
5864 (untabify (point-min) (point-max))
5865 (goto-char (point-min))
5866 (while (re-search-forward "^" nil t)
5867 (replace-match ": "))
5868 (when font-lock-mode
5869 (font-lock-unfontify-region (point-min) (point-max)))
5870 (put-text-property (point-min) (point-max) 'font-lock-fontified t))
5871 (kill-buffer (current-buffer))
5872 (and (org-mode-p) (org-restart-font-lock)))
5875 ;;; The orgstruct minor mode
5877 ;; Define a minor mode which can be used in other modes in order to
5878 ;; integrate the org-mode structure editing commands.
5880 ;; This is really a hack, because the org-mode structure commands use
5881 ;; keys which normally belong to the major mode. Here is how it
5882 ;; works: The minor mode defines all the keys necessary to operate the
5883 ;; structure commands, but wraps the commands into a function which
5884 ;; tests if the cursor is currently at a headline or a plain list
5885 ;; item. If that is the case, the structure command is used,
5886 ;; temporarily setting many Org-mode variables like regular
5887 ;; expressions for filling etc. However, when any of those keys is
5888 ;; used at a different location, function uses `key-binding' to look
5889 ;; up if the key has an associated command in another currently active
5890 ;; keymap (minor modes, major mode, global), and executes that
5891 ;; command. There might be problems if any of the keys is otherwise
5892 ;; used as a prefix key.
5894 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
5895 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
5896 ;; addresses this by checking explicitly for both bindings.
5898 (defvar orgstruct-mode-map (make-sparse-keymap)
5899 "Keymap for the minor `orgstruct-mode'.")
5901 (defvar org-local-vars nil
5902 "List of local variables, for use by `orgstruct-mode'")
5905 (define-minor-mode orgstruct-mode
5906 "Toggle the minor more `orgstruct-mode'.
5907 This mode is for using Org-mode structure commands in other modes.
5908 The following key behave as if Org-mode was active, if the cursor
5909 is on a headline, or on a plain list item (both in the definition
5912 M-up Move entry/item up
5913 M-down Move entry/item down
5916 M-S-up Move entry/item up
5917 M-S-down Move entry/item down
5918 M-S-left Promote subtree
5919 M-S-right Demote subtree
5920 M-q Fill paragraph and items like in Org-mode
5922 C-c - Cycle list bullet
5923 TAB Cycle item visibility
5924 M-RET Insert new heading/item
5925 S-M-RET Insert new TODO heading / Checkbox item
5926 C-c C-c Set tags / toggle checkbox"
5927 nil " OrgStruct" nil
5928 (org-load-modules-maybe)
5929 (and (orgstruct-setup) (defun orgstruct-setup () nil)))
5932 (defun turn-on-orgstruct ()
5933 "Unconditionally turn on `orgstruct-mode'."
5937 (defun turn-on-orgstruct++ ()
5938 "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
5939 In addition to setting orgstruct-mode, this also exports all indentation and
5940 autofilling variables from org-mode into the buffer. Note that turning
5941 off orgstruct-mode will *not* remove these additional settings."
5947 "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
5948 (symbol-name (car x)))
5949 (setq var (car x) val (nth 1 x))
5950 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
5953 (defun orgstruct-error ()
5954 "Error when there is no default binding for a structure key."
5956 (error "This key has no function outside structure elements"))
5958 (defun orgstruct-setup ()
5959 "Setup orgstruct keymaps."
5963 '([(meta up)] org-metaup)
5964 '([(meta down)] org-metadown)
5965 '([(meta left)] org-metaleft)
5966 '([(meta right)] org-metaright)
5967 '([(meta shift up)] org-shiftmetaup)
5968 '([(meta shift down)] org-shiftmetadown)
5969 '([(meta shift left)] org-shiftmetaleft)
5970 '([(meta shift right)] org-shiftmetaright)
5971 '([(shift up)] org-shiftup)
5972 '([(shift down)] org-shiftdown)
5973 '([(shift left)] org-shiftleft)
5974 '([(shift right)] org-shiftright)
5975 '("\C-c\C-c" org-ctrl-c-ctrl-c)
5976 '("\M-q" fill-paragraph)
5978 '("\C-c-" org-cycle-list-bullet)))
5980 (while (setq elt (pop bindings))
5981 (setq nfunc (1+ nfunc))
5982 (setq key (org-key (car elt))
5984 cmd (orgstruct-make-binding fun nfunc key))
5985 (org-defkey orgstruct-mode-map key cmd))
5987 ;; Special treatment needed for TAB and RET
5988 (org-defkey orgstruct-mode-map [(tab)]
5989 (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
5990 (org-defkey orgstruct-mode-map "\C-i"
5991 (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
5993 (org-defkey orgstruct-mode-map "\M-\C-m"
5994 (orgstruct-make-binding 'org-insert-heading 105
5995 "\M-\C-m" [(meta return)]))
5996 (org-defkey orgstruct-mode-map [(meta return)]
5997 (orgstruct-make-binding 'org-insert-heading 106
5998 [(meta return)] "\M-\C-m"))
6000 (org-defkey orgstruct-mode-map [(shift meta return)]
6001 (orgstruct-make-binding 'org-insert-todo-heading 107
6002 [(meta return)] "\M-\C-m"))
6004 (unless org-local-vars
6005 (setq org-local-vars (org-get-local-variables)))
6009 (defun orgstruct-make-binding (fun n &rest keys)
6010 "Create a function for binding in the structure minor mode.
6011 FUN is the command to call inside a table. N is used to create a unique
6012 command name. KEYS are keys that should be checked in for a command
6013 to execute outside of tables."
6016 (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
6018 (concat "In Structure, run `" (symbol-name fun) "'.\n"
6019 "Outside of structure, run the binding of `"
6020 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
6024 '(org-context-p 'headline 'item)
6025 (list 'org-run-like-in-org-mode (list 'quote fun))
6026 (list 'let '(orgstruct-mode)
6027 (list 'call-interactively
6030 (list 'key-binding k))
6032 '('orgstruct-error))))))))
6034 (defun org-context-p (&rest contexts)
6035 "Check if local context is any of CONTEXTS.
6036 Possible values in the list of contexts are `table', `headline', and `item'."
6037 (let ((pos (point)))
6038 (goto-char (point-at-bol))
6039 (prog1 (or (and (memq 'table contexts)
6040 (looking-at "[ \t]*|"))
6041 (and (memq 'headline contexts)
6042 ;;????????? (looking-at "\\*+"))
6043 (looking-at outline-regexp))
6044 (and (memq 'item contexts)
6045 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
6048 (defun org-get-local-variables ()
6049 "Return a list of all local variables in an org-mode buffer."
6051 (with-current-buffer (get-buffer-create "*Org tmp*")
6054 (setq varlist (buffer-local-variables)))
6055 (kill-buffer "*Org tmp*")
6062 (list (car x) (list 'quote (cdr x)))))
6064 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
6065 (symbol-name (car x)))
6070 (defun org-run-like-in-org-mode (cmd)
6071 (org-load-modules-maybe)
6072 (unless org-local-vars
6073 (setq org-local-vars (org-get-local-variables)))
6074 (eval (list 'let org-local-vars
6075 (list 'call-interactively (list 'quote cmd)))))
6079 (defun org-get-category (&optional pos)
6080 "Get the category applying to position POS."
6081 (get-text-property (or pos (point)) 'org-category))
6083 (defun org-refresh-category-properties ()
6084 "Refresh category text properties in the buffer."
6085 (let ((def-cat (cond
6086 ((null org-category)
6087 (if buffer-file-name
6088 (file-name-sans-extension
6089 (file-name-nondirectory buffer-file-name))
6091 ((symbolp org-category) (symbol-name org-category))
6093 beg end cat pos optionp)
6098 (goto-char (point-min))
6099 (put-text-property (point) (point-max) 'org-category def-cat)
6100 (while (re-search-forward
6101 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
6102 (setq pos (match-end 0)
6103 optionp (equal (char-after (match-beginning 0)) ?#)
6104 cat (org-trim (match-string 2)))
6106 (setq beg (point-at-bol) end (point-max))
6107 (org-back-to-heading t)
6108 (setq beg (point) end (org-end-of-subtree t t)))
6109 (put-text-property beg end 'org-category cat)
6110 (goto-char pos)))))))
6115 ;;; Link abbreviations
6117 (defun org-link-expand-abbrev (link)
6118 "Apply replacements as defined in `org-link-abbrev-alist."
6119 (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
6120 (let* ((key (match-string 1 link))
6121 (as (or (assoc key org-link-abbrev-alist-local)
6122 (assoc key org-link-abbrev-alist)))
6123 (tag (and (match-end 2) (match-string 3 link)))
6129 ((symbolp rpl) (funcall rpl tag))
6130 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
6131 ((string-match "%h" rpl)
6132 (replace-match (url-hexify-string (or tag "")) t t rpl))
6133 (t (concat rpl tag)))))
6136 ;;; Storing and inserting links
6138 (defvar org-insert-link-history nil
6139 "Minibuffer history for links inserted with `org-insert-link'.")
6141 (defvar org-stored-links nil
6142 "Contains the links stored with `org-store-link'.")
6144 (defvar org-store-link-plist nil
6145 "Plist with info about the most recently link created with `org-store-link'.")
6147 (defvar org-link-protocols nil
6148 "Link protocols added to Org-mode using `org-add-link-type'.")
6150 (defvar org-store-link-functions nil
6151 "List of functions that are called to create and store a link.
6152 Each function will be called in turn until one returns a non-nil
6153 value. Each function should check if it is responsible for creating
6154 this link (for example by looking at the major mode).
6155 If not, it must exit and return nil.
6156 If yes, it should return a non-nil value after a calling
6157 `org-store-link-props' with a list of properties and values.
6158 Special properties are:
6160 :type The link prefix. like \"http\". This must be given.
6161 :link The link, like \"http://www.astro.uva.nl/~dominik\".
6162 This is obligatory as well.
6163 :description Optional default description for the second pair
6164 of brackets in an Org-mode link. The user can still change
6165 this when inserting this link into an Org-mode buffer.
6167 In addition to these, any additional properties can be specified
6168 and then used in remember templates.")
6170 (defun org-add-link-type (type &optional follow export)
6171 "Add TYPE to the list of `org-link-types'.
6172 Re-compute all regular expressions depending on `org-link-types'
6174 FOLLOW and EXPORT are two functions.
6176 FOLLOW should take the link path as the single argument and do whatever
6177 is necessary to follow the link, for example find a file or display
6180 EXPORT should format the link path for export to one of the export formats.
6181 It should be a function accepting three arguments:
6183 path the path of the link, the text after the prefix (like \"http:\")
6184 desc the description of the link, if any, nil if there was no description
6185 format the export format, a symbol like `html' or `latex'.
6187 The function may use the FORMAT information to return different values
6188 depending on the format. The return value will be put literally into
6190 Org-mode has a built-in default for exporting links. If you are happy with
6191 this default, there is no need to define an export function for the link
6192 type. For a simple example of an export function, see `org-bbdb.el'."
6193 (add-to-list 'org-link-types type t)
6194 (org-make-link-regexps)
6195 (if (assoc type org-link-protocols)
6196 (setcdr (assoc type org-link-protocols) (list follow export))
6197 (push (list type follow export) org-link-protocols)))
6200 (defun org-store-link (arg)
6201 "\\<org-mode-map>Store an org-link to the current location.
6202 This link is added to `org-stored-links' and can later be inserted
6203 into an org-buffer with \\[org-insert-link].
6205 For some link types, a prefix arg is interpreted:
6206 For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
6207 For file links, arg negates `org-context-in-file-links'."
6209 (org-load-modules-maybe)
6210 (setq org-store-link-plist nil) ; reset
6211 (let (link cpltxt desc description search txt)
6214 ((run-hook-with-args-until-success 'org-store-link-functions)
6215 (setq link (plist-get org-store-link-plist :link)
6216 desc (or (plist-get org-store-link-plist :description) link)))
6218 ((eq major-mode 'calendar-mode)
6219 (let ((cd (calendar-cursor-to-date)))
6222 (car org-time-stamp-formats)
6224 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
6226 (org-store-link-props :type "calendar" :date cd)))
6228 ((eq major-mode 'w3-mode)
6229 (setq cpltxt (url-view-url t)
6230 link (org-make-link cpltxt))
6231 (org-store-link-props :type "w3" :url (url-view-url t)))
6233 ((eq major-mode 'w3m-mode)
6234 (setq cpltxt (or w3m-current-title w3m-current-url)
6235 link (org-make-link w3m-current-url))
6236 (org-store-link-props :type "w3m" :url (url-view-url t)))
6238 ((setq search (run-hook-with-args-until-success
6239 'org-create-file-search-functions))
6240 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
6242 (setq cpltxt (or description link)))
6244 ((eq major-mode 'image-mode)
6245 (setq cpltxt (concat "file:"
6246 (abbreviate-file-name buffer-file-name))
6247 link (org-make-link cpltxt))
6248 (org-store-link-props :type "image" :file buffer-file-name))
6250 ((eq major-mode 'dired-mode)
6251 ;; link to the file in the current line
6252 (setq cpltxt (concat "file:"
6253 (abbreviate-file-name
6255 (dired-get-filename nil t))))
6256 link (org-make-link cpltxt)))
6258 ((and buffer-file-name (org-mode-p))
6260 ((org-in-regexp "<<\\(.*?\\)>>")
6263 (abbreviate-file-name buffer-file-name)
6264 "::" (match-string 1))
6265 link (org-make-link cpltxt)))
6266 ((and (featurep 'org-id)
6267 (or (eq org-link-to-org-use-id t)
6268 (and (eq org-link-to-org-use-id 'create-if-interactive)
6270 (and org-link-to-org-use-id
6272 (org-entry-get nil "ID")
6274 ;; We can make a link using the ID.
6275 (setq link (condition-case nil
6276 (prog1 (org-id-store-link)
6277 (setq desc (plist-get org-store-link-plist
6280 ;; probably before first headline, link to file only
6282 (abbreviate-file-name buffer-file-name))))))
6284 ;; Just link to current headline
6285 (setq cpltxt (concat "file:"
6286 (abbreviate-file-name buffer-file-name)))
6287 ;; Add a context search string
6288 (when (org-xor org-context-in-file-links arg)
6290 ((org-on-heading-p) nil)
6291 ((org-region-active-p)
6292 (buffer-substring (region-beginning) (region-end)))
6294 (when (or (null txt) (string-match "\\S-" txt))
6298 (org-make-org-heading-search-string txt)
6301 (if (string-match "::\\'" cpltxt)
6302 (setq cpltxt (substring cpltxt 0 -2)))
6303 (setq link (org-make-link cpltxt)))))
6305 ((buffer-file-name (buffer-base-buffer))
6306 ;; Just link to this file here.
6307 (setq cpltxt (concat "file:"
6308 (abbreviate-file-name
6309 (buffer-file-name (buffer-base-buffer)))))
6310 ;; Add a context string
6311 (when (org-xor org-context-in-file-links arg)
6312 (setq txt (if (org-region-active-p)
6313 (buffer-substring (region-beginning) (region-end))
6314 (buffer-substring (point-at-bol) (point-at-eol))))
6315 ;; Only use search option if there is some text.
6316 (when (string-match "\\S-" txt)
6318 (concat cpltxt "::" (org-make-org-heading-search-string txt))
6320 (setq link (org-make-link cpltxt)))
6323 (error "Cannot link to a buffer which is not visiting a file"))
6325 (t (setq link nil)))
6327 (if (consp link) (setq cpltxt (car link) link (cdr link)))
6328 (setq link (or link cpltxt)
6329 desc (or desc cpltxt))
6330 (if (equal desc "NONE") (setq desc nil))
6332 (if (and (interactive-p) link)
6334 (setq org-stored-links
6335 (cons (list link desc) org-stored-links))
6336 (message "Stored: %s" (or desc link)))
6337 (and link (org-make-link-string link desc)))))
6339 (defun org-store-link-props (&rest plist)
6340 "Store link properties, extract names and addresses."
6342 (when (setq x (plist-get plist :from))
6343 (setq adr (mail-extract-address-components x))
6344 (setq plist (plist-put plist :fromname (car adr)))
6345 (setq plist (plist-put plist :fromaddress (nth 1 adr))))
6346 (when (setq x (plist-get plist :to))
6347 (setq adr (mail-extract-address-components x))
6348 (setq plist (plist-put plist :toname (car adr)))
6349 (setq plist (plist-put plist :toaddress (nth 1 adr)))))
6350 (let ((from (plist-get plist :from))
6351 (to (plist-get plist :to)))
6352 (when (and from to org-from-is-user-regexp)
6354 (plist-put plist :fromto
6355 (if (string-match org-from-is-user-regexp from)
6357 (concat "from %f"))))))
6358 (setq org-store-link-plist plist))
6360 (defun org-add-link-props (&rest plist)
6361 "Add these properties to the link property list."
6364 (setq key (pop plist) value (pop plist))
6365 (setq org-store-link-plist
6366 (plist-put org-store-link-plist key value)))))
6368 (defun org-email-link-description (&optional fmt)
6369 "Return the description part of an email link.
6370 This takes information from `org-store-link-plist' and formats it
6371 according to FMT (default from `org-email-link-description-format')."
6372 (setq fmt (or fmt org-email-link-description-format))
6373 (let* ((p org-store-link-plist)
6374 (to (plist-get p :toaddress))
6375 (from (plist-get p :fromaddress))
6378 (cons "%c" (plist-get p :fromto))
6379 (cons "%F" (plist-get p :from))
6380 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
6381 (cons "%T" (plist-get p :to))
6382 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
6383 (cons "%s" (plist-get p :subject))
6384 (cons "%m" (plist-get p :message-id)))))
6385 (when (string-match "%c" fmt)
6386 ;; Check if the user wrote this message
6387 (if (and org-from-is-user-regexp from to
6388 (save-match-data (string-match org-from-is-user-regexp from)))
6389 (setq fmt (replace-match "to %t" t t fmt))
6390 (setq fmt (replace-match "from %f" t t fmt))))
6391 (org-replace-escapes fmt table)))
6393 (defun org-make-org-heading-search-string (&optional string heading)
6394 "Make search string for STRING or current headline."
6396 (let ((s (or string (org-get-heading))))
6397 (unless (and string (not heading))
6398 ;; We are using a headline, clean up garbage in there.
6399 (if (string-match org-todo-regexp s)
6400 (setq s (replace-match "" t t s)))
6401 (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
6402 (setq s (replace-match "" t t s)))
6403 (setq s (org-trim s))
6404 (if (string-match (concat "^\\(" org-quote-string "\\|"
6405 org-comment-string "\\)") s)
6406 (setq s (replace-match "" t t s)))
6407 (while (string-match org-ts-regexp s)
6408 (setq s (replace-match "" t t s))))
6409 (while (string-match "[^a-zA-Z_0-9 \t]+" s)
6410 (setq s (replace-match " " t t s)))
6411 (or string (setq s (concat "*" s))) ; Add * for headlines
6412 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
6414 (defun org-make-link (&rest strings)
6415 "Concatenate STRINGS."
6416 (apply 'concat strings))
6418 (defun org-make-link-string (link &optional description)
6419 "Make a link with brackets, consisting of LINK and DESCRIPTION."
6420 (unless (string-match "\\S-" link)
6421 (error "Empty link"))
6422 (when (stringp description)
6423 ;; Remove brackets from the description, they are fatal.
6424 (while (string-match "\\[" description)
6425 (setq description (replace-match "{" t t description)))
6426 (while (string-match "\\]" description)
6427 (setq description (replace-match "}" t t description))))
6428 (when (equal (org-link-escape link) description)
6429 ;; No description needed, it is identical
6430 (setq description nil))
6431 (when (and (not description)
6432 (not (equal link (org-link-escape link))))
6433 (setq description (org-extract-attributes link)))
6434 (concat "[[" (org-link-escape link) "]"
6435 (if description (concat "[" description "]") "")
6438 (defconst org-link-escape-chars
6442 (?\340 . "%E0") ; `a
6443 (?\342 . "%E2") ; ^a
6444 (?\347 . "%E7") ; ,c
6445 (?\350 . "%E8") ; `e
6446 (?\351 . "%E9") ; 'e
6447 (?\352 . "%EA") ; ^e
6448 (?\356 . "%EE") ; ^i
6449 (?\364 . "%F4") ; ^o
6450 (?\371 . "%F9") ; `u
6451 (?\373 . "%FB") ; ^u
6457 "Association list of escapes for some characters problematic in links.
6458 This is the list that is used for internal purposes.")
6460 (defconst org-link-escape-chars-browser
6461 '((?\ . "%20")) ; 32 for the SPC char
6462 "Association list of escapes for some characters problematic in links.
6463 This is the list that is used before handing over to the browser.")
6465 (defun org-link-escape (text &optional table)
6466 "Escape characters in TEXT that are problematic for links."
6467 (setq table (or table org-link-escape-chars))
6469 (let ((re (mapconcat (lambda (x) (regexp-quote
6470 (char-to-string (car x))))
6472 (while (string-match re text)
6475 (cdr (assoc (string-to-char (match-string 0 text))
6480 (defun org-link-unescape (text &optional table)
6481 "Reverse the action of `org-link-escape'."
6482 (setq table (or table org-link-escape-chars))
6484 (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
6486 (while (string-match re text)
6489 (char-to-string (car (rassoc (match-string 0 text) table)))
6493 (defun org-xor (a b)
6497 (defun org-get-header (header)
6498 "Find a header field in the current buffer."
6500 (goto-char (point-min))
6501 (let ((case-fold-search t) s)
6504 (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
6505 (setq s (match-string 1)))
6506 (while (string-match "\"" s)
6507 (setq s (replace-match "" t t s)))
6508 (if (string-match "[<(].*" s)
6509 (setq s (replace-match "" t t s))))
6510 ((eq header 'message-id)
6511 (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
6512 (setq s (match-string 1))))
6513 ((eq header 'subject)
6514 (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
6515 (setq s (match-string 1)))))
6516 (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
6517 (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
6521 (defun org-fixup-message-id-for-http (s)
6522 "Replace special characters in a message id, so it can be used in an http query."
6523 (while (string-match "<" s)
6524 (setq s (replace-match "%3C" t t s)))
6525 (while (string-match ">" s)
6526 (setq s (replace-match "%3E" t t s)))
6527 (while (string-match "@" s)
6528 (setq s (replace-match "%40" t t s)))
6532 (defun org-insert-link-global ()
6533 "Insert a link like Org-mode does.
6534 This command can be called in any mode to insert a link in Org-mode syntax."
6536 (org-load-modules-maybe)
6537 (org-run-like-in-org-mode 'org-insert-link))
6539 (defun org-insert-link (&optional complete-file link-location)
6540 "Insert a link. At the prompt, enter the link.
6542 Completion can be used to insert any of the link protocol prefixes like
6545 The history can be used to select a link previously stored with
6546 `org-store-link'. When the empty string is entered (i.e. if you just
6547 press RET at the prompt), the link defaults to the most recently
6548 stored link. As SPC triggers completion in the minibuffer, you need to
6549 use M-SPC or C-q SPC to force the insertion of a space character.
6551 You will also be prompted for a description, and if one is given, it will
6552 be displayed in the buffer instead of the link.
6554 If there is already a link at point, this command will allow you to edit link
6555 and description parts.
6557 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
6558 be selected using completion. The path to the file will be relative to the
6559 current directory if the file is in the current directory or a subdirectory.
6560 Otherwise, the link will be the absolute path as completed in the minibuffer
6561 \(i.e. normally ~/path/to/file). You can configure this behavior using the
6562 option `org-link-file-path-type'.
6564 With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
6565 the current directory or below.
6567 With three \\[universal-argument] prefixes, negate the meaning of
6568 `org-keep-stored-link-after-insertion'.
6570 If `org-make-link-description-function' is non-nil, this function will be
6571 called with the link target, and the result will be the default
6574 If the LINK-LOCATION parameter is non-nil, this value will be
6575 used as the link location instead of reading one interactively."
6577 (let* ((wcf (current-window-configuration))
6578 (region (if (org-region-active-p)
6579 (buffer-substring (region-beginning) (region-end))))
6580 (remove (and region (list (region-beginning) (region-end))))
6582 tmphist ; byte-compile incorrectly complains about this
6583 (link link-location)
6586 (link-location) ; specified by arg, just use it.
6587 ((org-in-regexp org-bracket-link-regexp 1)
6588 ;; We do have a link at point, and we are going to edit it.
6589 (setq remove (list (match-beginning 0) (match-end 0)))
6590 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
6591 (setq link (read-string "Link: "
6593 (org-match-string-no-properties 1)))))
6594 ((or (org-in-regexp org-angle-link-re)
6595 (org-in-regexp org-plain-link-re))
6596 ;; Convert to bracket link
6597 (setq remove (list (match-beginning 0) (match-end 0))
6598 link (read-string "Link: "
6599 (org-remove-angle-brackets (match-string 0)))))
6600 ((member complete-file '((4) (16)))
6601 ;; Completing read for file names.
6602 (setq file (read-file-name "File: "))
6603 (let ((pwd (file-name-as-directory (expand-file-name ".")))
6604 (pwd1 (file-name-as-directory (abbreviate-file-name
6605 (expand-file-name ".")))))
6607 ((equal complete-file '(16))
6608 (setq link (org-make-link
6610 (abbreviate-file-name (expand-file-name file)))))
6611 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
6612 (setq link (org-make-link "file:" (match-string 1 file))))
6613 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
6614 (expand-file-name file))
6615 (setq link (org-make-link
6616 "file:" (match-string 1 (expand-file-name file)))))
6617 (t (setq link (org-make-link "file:" file))))))
6619 ;; Read link, with completion for stored links.
6620 (with-output-to-temp-buffer "*Org Links*"
6621 (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
6622 (when org-stored-links
6623 (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
6626 (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
6627 (reverse org-stored-links) "\n"))))
6628 (let ((cw (selected-window)))
6629 (select-window (get-buffer-window "*Org Links*"))
6630 (org-fit-window-to-buffer)
6631 (setq truncate-lines t)
6633 ;; Fake a link history, containing the stored links.
6634 (setq tmphist (append (mapcar 'car org-stored-links)
6635 org-insert-link-history))
6637 (setq link (org-completing-read
6640 (mapcar (lambda (x) (list (concat (car x) ":")))
6641 (append org-link-abbrev-alist-local org-link-abbrev-alist))
6642 (mapcar (lambda (x) (list (concat x ":")))
6646 (or (car (car org-stored-links)))))
6647 (set-window-configuration wcf)
6648 (kill-buffer "*Org Links*"))
6649 (setq entry (assoc link org-stored-links))
6650 (or entry (push link org-insert-link-history))
6651 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
6652 (not org-keep-stored-link-after-insertion))
6653 (setq org-stored-links (delq (assoc link org-stored-links)
6655 (setq desc (or desc (nth 1 entry)))))
6657 (if (string-match org-plain-link-re link)
6658 ;; URL-like link, normalize the use of angular brackets.
6659 (setq link (org-make-link (org-remove-angle-brackets link))))
6661 ;; Check if we are linking to the current file with a search option
6662 ;; If yes, simplify the link by using only the search option.
6663 (when (and buffer-file-name
6664 (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
6665 (let* ((path (match-string 1 link))
6666 (case-fold-search nil)
6667 (search (match-string 2 link)))
6669 (if (equal (file-truename buffer-file-name) (file-truename path))
6670 ;; We are linking to this same file, with a search option
6671 (setq link search)))))
6673 ;; Check if we can/should use a relative path. If yes, simplify the link
6674 (when (string-match "^file:\\(.*\\)" link)
6675 (let* ((path (match-string 1 link))
6677 (case-fold-search nil))
6679 ((or (eq org-link-file-path-type 'absolute)
6680 (equal complete-file '(16)))
6681 (setq path (abbreviate-file-name (expand-file-name path))))
6682 ((eq org-link-file-path-type 'noabbrev)
6683 (setq path (expand-file-name path)))
6684 ((eq org-link-file-path-type 'relative)
6685 (setq path (file-relative-name path)))
6688 (if (string-match (concat "^" (regexp-quote
6689 (file-name-as-directory
6690 (expand-file-name "."))))
6691 (expand-file-name path))
6692 ;; We are linking a file with relative path name.
6693 (setq path (substring (expand-file-name path)
6695 (setq path (abbreviate-file-name (expand-file-name path)))))))
6696 (setq link (concat "file:" path))
6697 (if (equal desc origpath)
6700 (if org-make-link-description-function
6701 (setq desc (funcall org-make-link-description-function link desc)))
6703 (setq desc (read-string "Description: " desc))
6704 (unless (string-match "\\S-" desc) (setq desc nil))
6705 (if remove (apply 'delete-region remove))
6706 (insert (org-make-link-string link desc))))
6708 (defun org-completing-read (&rest args)
6709 "Completing-read with SPACE being a normal character."
6710 (let ((minibuffer-local-completion-map
6711 (copy-keymap minibuffer-local-completion-map)))
6712 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
6713 (apply 'org-ido-completing-read args)))
6715 (defun org-ido-completing-read (&rest args)
6716 "Completing-read using `ido-mode' speedups if available"
6717 (if (and org-completion-use-ido
6718 (fboundp 'ido-completing-read)
6719 (boundp 'ido-mode) ido-mode
6720 (listp (second args)))
6721 (apply 'ido-completing-read (concat (car args)) (cdr args))
6722 (apply 'completing-read args)))
6724 (defun org-extract-attributes (s)
6725 "Extract the attributes cookie from a string and set as text property."
6726 (let (a attr (start 0) key value)
6728 (when (string-match "{{\\([^}]+\\)}}$" s)
6729 (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
6730 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
6731 (setq key (match-string 1 a) value (match-string 2 a)
6733 attr (plist-put attr (intern key) value))))
6734 (org-add-props s nil 'org-attr attr))
6737 (defun org-attributes-to-string (plist)
6738 "Format a property list into an HTML attribute list."
6739 (let ((s "") key value)
6741 (setq key (pop plist) value (pop plist))
6743 (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
6746 ;;; Opening/following a link
6748 (defvar org-link-search-failed nil)
6750 (defun org-next-link ()
6751 "Move forward to the next link.
6752 If the link is in hidden text, expose it."
6754 (when (and org-link-search-failed (eq this-command last-command))
6755 (goto-char (point-min))
6756 (message "Link search wrapped back to beginning of buffer"))
6757 (setq org-link-search-failed nil)
6758 (let* ((pos (point))
6760 (a (assoc :link ct)))
6761 (if a (goto-char (nth 2 a)))
6762 (if (re-search-forward org-any-link-re nil t)
6764 (goto-char (match-beginning 0))
6765 (if (org-invisible-p) (org-show-context)))
6767 (setq org-link-search-failed t)
6768 (error "No further link found"))))
6770 (defun org-previous-link ()
6771 "Move backward to the previous link.
6772 If the link is in hidden text, expose it."
6774 (when (and org-link-search-failed (eq this-command last-command))
6775 (goto-char (point-max))
6776 (message "Link search wrapped back to end of buffer"))
6777 (setq org-link-search-failed nil)
6778 (let* ((pos (point))
6780 (a (assoc :link ct)))
6781 (if a (goto-char (nth 1 a)))
6782 (if (re-search-backward org-any-link-re nil t)
6784 (goto-char (match-beginning 0))
6785 (if (org-invisible-p) (org-show-context)))
6787 (setq org-link-search-failed t)
6788 (error "No further link found"))))
6790 (defun org-translate-link (s)
6791 "Translate a link string if a translation function has been defined."
6792 (if (and org-link-translation-function
6793 (fboundp org-link-translation-function)
6794 (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
6796 (setq s (funcall org-link-translation-function
6797 (match-string 1) (match-string 2)))
6798 (concat (car s) ":" (cdr s)))
6801 (defun org-translate-link-from-planner (type path)
6802 "Translate a link from Emacs Planner syntax so that Org can follow it.
6803 This is still an experimental function, your mileage may vary."
6805 ((member type '("http" "https" "news" "ftp"))
6806 ;; standard Internet links are the same.
6808 ((and (equal type "irc") (string-match "^//" path))
6809 ;; Planner has two / at the beginning of an irc link, we have 1.
6810 ;; We should have zero, actually....
6811 (setq path (substring path 1)))
6812 ((and (equal type "lisp") (string-match "^/" path))
6813 ;; Planner has a slash, we do not.
6814 (setq type "elisp" path (substring path 1)))
6815 ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
6816 ;; A typical message link. Planner has the id after the fina slash,
6817 ;; we separate it with a hash mark
6818 (setq path (concat (match-string 1 path) "#"
6819 (org-remove-angle-brackets (match-string 2 path)))))
6823 (defun org-find-file-at-mouse (ev)
6824 "Open file link or URL at mouse."
6826 (mouse-set-point ev)
6827 (org-open-at-point 'in-emacs))
6829 (defun org-open-at-mouse (ev)
6830 "Open file link or URL at mouse."
6832 (mouse-set-point ev)
6833 (if (eq major-mode 'org-agenda-mode)
6834 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
6835 (org-open-at-point))
6837 (defvar org-window-config-before-follow-link nil
6838 "The window configuration before following a link.
6839 This is saved in case the need arises to restore it.")
6841 (defvar org-open-link-marker (make-marker)
6842 "Marker pointing to the location where `org-open-at-point; was called.")
6845 (defun org-open-at-point-global ()
6846 "Follow a link like Org-mode does.
6847 This command can be called in any mode to follow a link that has
6850 (org-run-like-in-org-mode 'org-open-at-point))
6853 (defun org-open-link-from-string (s &optional arg)
6854 "Open a link in the string S, as if it was in Org-mode."
6855 (interactive "sLink: \nP")
6857 (let ((org-inhibit-startup t))
6860 (goto-char (point-min))
6861 (org-open-at-point arg))))
6863 (defun org-open-at-point (&optional in-emacs)
6864 "Open link at or after point.
6865 If there is no link at point, this function will search forward up to
6866 the end of the current subtree.
6867 Normally, files will be opened by an appropriate application. If the
6868 optional argument IN-EMACS is non-nil, Emacs will visit the file.
6869 With a double prefix argument, try to open outside of Emacs, in the
6870 application the system uses for this file type."
6872 (org-load-modules-maybe)
6873 (move-marker org-open-link-marker (point))
6874 (setq org-window-config-before-follow-link (current-window-configuration))
6875 (org-remove-occur-highlights nil nil t)
6876 (if (org-at-timestamp-p t)
6877 (org-follow-timestamp-link)
6878 (let (type path link line search (pos (point)))
6881 (skip-chars-forward "^]\n\r")
6882 (when (org-in-regexp org-bracket-link-regexp)
6883 (setq link (org-extract-attributes
6884 (org-link-unescape (org-match-string-no-properties 1))))
6885 (while (string-match " *\n *" link)
6886 (setq link (replace-match " " t t link)))
6887 (setq link (org-link-expand-abbrev link))
6889 ((or (file-name-absolute-p link)
6890 (string-match "^\\.\\.?/" link))
6891 (setq type "file" path link))
6892 ((string-match org-link-re-with-space3 link)
6893 (setq type (match-string 1 link) path (match-string 2 link)))
6894 (t (setq type "thisfile" path link)))
6897 (when (get-text-property (point) 'org-linked-text)
6898 (setq type "thisfile"
6899 pos (if (get-text-property (1+ (point)) 'org-linked-text)
6900 (1+ (point)) (point))
6901 path (buffer-substring
6902 (previous-single-property-change pos 'org-linked-text)
6903 (next-single-property-change pos 'org-linked-text)))
6907 (when (or (org-in-regexp org-angle-link-re)
6908 (org-in-regexp org-plain-link-re))
6909 (setq type (match-string 1) path (match-string 2))
6911 (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
6912 (setq type "tree-match"
6913 path (match-string 1))
6916 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
6918 path (match-string 1))
6919 (while (string-match ":" path)
6920 (setq path (replace-match "+" t t path)))
6923 (error "No link found"))
6924 ;; Remove any trailing spaces in path
6925 (if (string-match " +\\'" path)
6926 (setq path (replace-match "" t t path)))
6927 (if (and org-link-translation-function
6928 (fboundp org-link-translation-function))
6929 ;; Check if we need to translate the link
6930 (let ((tmp (funcall org-link-translation-function type path)))
6931 (setq type (car tmp) path (cdr tmp))))
6935 ((assoc type org-link-protocols)
6936 (funcall (nth 1 (assoc type org-link-protocols)) path))
6938 ((equal type "mailto")
6939 (let ((cmd (car org-link-mailto-program))
6940 (args (cdr org-link-mailto-program)) args1
6941 (address path) (subject "") a)
6942 (if (string-match "\\(.*\\)::\\(.*\\)" path)
6943 (setq address (match-string 1 path)
6944 subject (org-link-escape (match-string 2 path))))
6947 ((not (stringp (car args))) (push (pop args) args1))
6948 (t (setq a (pop args))
6949 (if (string-match "%a" a)
6950 (setq a (replace-match address t t a)))
6951 (if (string-match "%s" a)
6952 (setq a (replace-match subject t t a)))
6954 (apply cmd (nreverse args1))))
6956 ((member type '("http" "https" "ftp" "news"))
6957 (browse-url (concat type ":" (org-link-escape
6958 path org-link-escape-chars-browser))))
6960 ((member type '("message"))
6961 (browse-url (concat type ":" path)))
6963 ((string= type "tags")
6964 (org-tags-view in-emacs path))
6965 ((string= type "thisfile")
6967 (switch-to-buffer-other-window
6968 (org-get-buffer-for-internal-link (current-buffer)))
6969 (org-mark-ring-push))
6970 (let ((cmd `(org-link-search
6972 ,(cond ((equal in-emacs '(4)) 'occur)
6973 ((equal in-emacs '(16)) 'org-occur)
6976 (condition-case nil (eval cmd)
6977 (error (progn (widen) (eval cmd))))))
6979 ((string= type "tree-match")
6980 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
6982 ((string= type "file")
6983 (if (string-match "::\\([0-9]+\\)\\'" path)
6984 (setq line (string-to-number (match-string 1 path))
6985 path (substring path 0 (match-beginning 0)))
6986 (if (string-match "::\\(.+\\)\\'" path)
6987 (setq search (match-string 1 path)
6988 path (substring path 0 (match-beginning 0)))))
6989 (if (string-match "[*?{]" (file-name-nondirectory path))
6991 (org-open-file path in-emacs line search)))
6993 ((string= type "news")
6995 (org-gnus-follow-link path))
6997 ((string= type "shell")
6999 (if (or (not org-confirm-shell-link-function)
7000 (funcall org-confirm-shell-link-function
7001 (format "Execute \"%s\" in shell? "
7002 (org-add-props cmd nil
7003 'face 'org-warning))))
7005 (message "Executing %s" cmd)
7006 (shell-command cmd))
7009 ((string= type "elisp")
7011 (if (or (not org-confirm-elisp-link-function)
7012 (funcall org-confirm-elisp-link-function
7013 (format "Execute \"%s\" as elisp? "
7014 (org-add-props cmd nil
7015 'face 'org-warning))))
7016 (message "%s => %s" cmd
7017 (if (equal (string-to-char cmd) ?\()
7019 (call-interactively (read cmd))))
7023 (browse-url-at-point)))))
7024 (move-marker org-open-link-marker nil)
7025 (run-hook-with-args 'org-follow-link-hook))
7029 (defun org-get-effort (&optional pom)
7030 "Get the effort estimate for the current entry."
7031 (org-entry-get pom org-effort-property))
7035 (defvar org-create-file-search-functions nil
7036 "List of functions to construct the right search string for a file link.
7037 These functions are called in turn with point at the location to
7038 which the link should point.
7040 A function in the hook should first test if it would like to
7041 handle this file type, for example by checking the major-mode or
7042 the file extension. If it decides not to handle this file, it
7043 should just return nil to give other functions a chance. If it
7044 does handle the file, it must return the search string to be used
7045 when following the link. The search string will be part of the
7046 file link, given after a double colon, and `org-open-at-point'
7047 will automatically search for it. If special measures must be
7048 taken to make the search successful, another function should be
7049 added to the companion hook `org-execute-file-search-functions',
7052 A function in this hook may also use `setq' to set the variable
7053 `description' to provide a suggestion for the descriptive text to
7054 be used for this link when it gets inserted into an Org-mode
7055 buffer with \\[org-insert-link].")
7057 (defvar org-execute-file-search-functions nil
7058 "List of functions to execute a file search triggered by a link.
7060 Functions added to this hook must accept a single argument, the
7061 search string that was part of the file link, the part after the
7062 double colon. The function must first check if it would like to
7063 handle this search, for example by checking the major-mode or the
7064 file extension. If it decides not to handle this search, it
7065 should just return nil to give other functions a chance. If it
7066 does handle the search, it must return a non-nil value to keep
7067 other functions from trying.
7069 Each function can access the current prefix argument through the
7070 variable `current-prefix-argument'. Note that a single prefix is
7071 used to force opening a link in Emacs, so it may be good to only
7072 use a numeric or double prefix to guide the search function.
7074 In case this is needed, a function in this hook can also restore
7075 the window configuration before `org-open-at-point' was called using:
7077 (set-window-configuration org-window-config-before-follow-link)")
7079 (defun org-link-search (s &optional type avoid-pos)
7080 "Search for a link search option.
7081 If S is surrounded by forward slashes, it is interpreted as a
7082 regular expression. In org-mode files, this will create an `org-occur'
7083 sparse tree. In ordinary files, `occur' will be used to list matches.
7084 If the current buffer is in `dired-mode', grep will be used to search
7085 in all files. If AVOID-POS is given, ignore matches near that position."
7086 (let ((case-fold-search t)
7087 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
7088 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
7089 (append '(("") (" ") ("\t") ("\n"))
7093 (pre nil) (post nil)
7094 words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
7096 ;; First check if there are any special
7097 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
7098 ;; Now try the builtin stuff
7100 (goto-char (point-min))
7103 (concat "<<" (regexp-quote s0) ">>") nil t)
7104 (setq type 'dedicated
7105 pos (match-beginning 0))))
7106 ;; There is an exact target for this
7108 ((string-match "^/\\(.*\\)/$" s)
7109 ;; A regular expression
7112 (org-occur (match-string 1 s)))
7113 ;;((eq major-mode 'dired-mode)
7114 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
7115 (t (org-do-occur (match-string 1 s)))))
7117 ;; A normal search strings
7118 (when (equal (string-to-char s) ?*)
7119 ;; Anchor on headlines, post may include tags.
7120 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
7121 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
7123 (remove-text-properties
7125 '(face nil mouse-face nil keymap nil fontified nil) s)
7126 ;; Make a series of regular expressions to find a match
7127 (setq words (org-split-string s "[ \n\r\t]+")
7129 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
7130 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
7132 re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
7133 re2a (concat "[ \t\r\n]" re2a_)
7134 re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
7135 re4 (concat "[^a-zA-Z_]" re4_)
7137 re1 (concat pre re2 post)
7138 re3 (concat pre (if pre re4_ re4) post)
7139 re5 (concat pre ".*" re4)
7140 re2 (concat pre re2)
7141 re2a (concat pre (if pre re2a_ re2a))
7142 re4 (concat pre (if pre re4_ re4))
7143 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
7144 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
7148 ((eq type 'org-occur) (org-occur reall))
7149 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
7150 (t (goto-char (point-min))
7152 (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
7153 (org-search-not-self 1 re1 nil t)
7154 (org-search-not-self 1 re2 nil t)
7155 (org-search-not-self 1 re2a nil t)
7156 (org-search-not-self 1 re3 nil t)
7157 (org-search-not-self 1 re4 nil t)
7158 (org-search-not-self 1 re5 nil t)
7160 (goto-char (match-beginning 1))
7162 (error "No match")))))
7164 ;; Normal string-search
7165 (goto-char (point-min))
7166 (if (search-forward s nil t)
7167 (goto-char (match-beginning 0))
7168 (error "No match"))))
7169 (and (org-mode-p) (org-show-context 'link-search))
7172 (defun org-search-not-self (group &rest args)
7173 "Execute `re-search-forward', but only accept matches that do not
7174 enclose the position of `org-open-link-marker'."
7175 (let ((m org-open-link-marker))
7177 (while (apply 're-search-forward args)
7178 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
7179 (goto-char (match-end group))
7180 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
7181 (> (match-beginning 0) (marker-position m))
7182 (< (match-end 0) (marker-position m)))
7184 (or (not (org-in-regexp
7185 org-bracket-link-analytic-regexp 1))
7186 (not (match-end 4)) ; no description
7187 (and (<= (match-beginning 4) (point))
7188 (>= (match-end 4) (point))))))
7189 (throw 'exit (point))))))))
7191 (defun org-get-buffer-for-internal-link (buffer)
7192 "Return a buffer to be used for displaying the link target of internal links."
7194 ((not org-display-internal-link-with-indirect-buffer)
7196 ((string-match "(Clone)$" (buffer-name buffer))
7197 (message "Buffer is already a clone, not making another one")
7198 ;; we also do not modify visibility in this case
7200 (t ; make a new indirect buffer for displaying the link
7201 (let* ((bn (buffer-name buffer))
7202 (ibn (concat bn "(Clone)"))
7203 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
7204 (with-current-buffer ib (org-overview))
7207 (defun org-do-occur (regexp &optional cleanup)
7208 "Call the Emacs command `occur'.
7209 If CLEANUP is non-nil, remove the printout of the regular expression
7210 in the *Occur* buffer. This is useful if the regex is long and not useful
7214 (let ((cwin (selected-window)) win beg end)
7215 (when (setq win (get-buffer-window "*Occur*"))
7216 (select-window win))
7217 (goto-char (point-min))
7218 (when (re-search-forward "match[a-z]+" nil t)
7219 (setq beg (match-end 0))
7220 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
7221 (setq end (1- (match-beginning 0)))))
7222 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
7223 (goto-char (point-min))
7224 (select-window cwin))))
7226 ;;; The mark ring for links jumps
7228 (defvar org-mark-ring nil
7229 "Mark ring for positions before jumps in Org-mode.")
7230 (defvar org-mark-ring-last-goto nil
7231 "Last position in the mark ring used to go back.")
7232 ;; Fill and close the ring
7233 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
7234 (loop for i from 1 to org-mark-ring-length do
7235 (push (make-marker) org-mark-ring))
7236 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
7239 (defun org-mark-ring-push (&optional pos buffer)
7240 "Put the current position or POS into the mark ring and rotate it."
7242 (setq pos (or pos (point)))
7243 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
7244 (move-marker (car org-mark-ring)
7246 (or buffer (current-buffer)))
7248 (substitute-command-keys
7249 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
7251 (defun org-mark-ring-goto (&optional n)
7252 "Jump to the previous position in the mark ring.
7253 With prefix arg N, jump back that many stored positions. When
7254 called several times in succession, walk through the entire ring.
7255 Org-mode commands jumping to a different position in the current file,
7256 or to another Org-mode file, automatically push the old position
7260 (if (eq last-command this-command)
7261 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
7262 (setq p org-mark-ring))
7263 (setq org-mark-ring-last-goto p)
7265 (switch-to-buffer (marker-buffer m))
7267 (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
7269 (defun org-remove-angle-brackets (s)
7270 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
7271 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
7273 (defun org-add-angle-brackets (s)
7274 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
7275 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
7277 (defun org-remove-double-quotes (s)
7278 (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
7279 (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
7282 ;;; Following specific links
7284 (defun org-follow-timestamp-link ()
7286 ((org-at-date-range-p t)
7287 (let ((org-agenda-start-on-weekday)
7288 (t1 (match-string 1))
7289 (t2 (match-string 2)))
7290 (setq t1 (time-to-days (org-time-string-to-time t1))
7291 t2 (time-to-days (org-time-string-to-time t2)))
7292 (org-agenda-list nil t1 (1+ (- t2 t1)))))
7293 ((org-at-timestamp-p t)
7294 (org-agenda-list nil (time-to-days (org-time-string-to-time
7295 (substring (match-string 1) 0 10)))
7297 (t (error "This should not happen"))))
7300 ;;; Following file links
7301 (defvar org-wait nil)
7302 (defun org-open-file (path &optional in-emacs line search)
7303 "Open the file at PATH.
7304 First, this expands any special file name abbreviations. Then the
7305 configuration variable `org-file-apps' is checked if it contains an
7306 entry for this file type, and if yes, the corresponding command is launched.
7308 If no application is found, Emacs simply visits the file.
7310 With optional prefix argument IN-EMACS, Emacs will visit the file.
7311 With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
7312 and o use an external application to visit the file.
7314 Optional LINE specifies a line to go to, optional SEARCH a string to
7315 search for. If LINE or SEARCH is given, the file will always be
7317 If the file does not exist, an error is thrown."
7318 (setq in-emacs (or in-emacs line search))
7319 (let* ((file (if (equal path "")
7321 (substitute-in-file-name (expand-file-name path))))
7322 (apps (append org-file-apps (org-default-apps)))
7323 (remp (and (assq 'remote apps) (org-file-remote-p file)))
7324 (dirp (if remp nil (file-directory-p file)))
7325 (file (if (and dirp org-open-directory-means-index-dot-org)
7326 (concat (file-name-as-directory file) "index.org")
7328 (a-m-a-p (assq 'auto-mode apps))
7329 (dfile (downcase file))
7330 (old-buffer (current-buffer))
7332 (old-mode major-mode)
7334 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
7335 (setq ext (match-string 1 dfile))
7336 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
7337 (setq ext (match-string 1 dfile))))
7339 ((equal in-emacs '(16))
7340 (setq cmd (cdr (assoc 'system apps))))
7341 (in-emacs (setq cmd 'emacs))
7343 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
7344 (and dirp (cdr (assoc 'directory apps)))
7345 (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
7347 (cdr (assoc ext apps))
7348 (cdr (assoc t apps))))))
7349 (when (eq cmd 'system)
7350 (setq cmd (cdr (assoc 'system apps))))
7351 (when (eq cmd 'default)
7352 (setq cmd (cdr (assoc t apps))))
7353 (when (eq cmd 'mailcap)
7355 (mailcap-parse-mailcaps)
7356 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
7357 (command (mailcap-mime-info mime-type)))
7358 (if (stringp command)
7360 (setq cmd 'emacs))))
7361 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
7362 (not (file-exists-p file))
7363 (not org-open-non-existing-files))
7364 (error "No such file: %s" file))
7366 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
7367 ;; Remove quotes around the file name - we'll use shell-quote-argument.
7368 (while (string-match "['\"]%s['\"]" cmd)
7369 (setq cmd (replace-match "%s" t t cmd)))
7370 (while (string-match "%s" cmd)
7371 (setq cmd (replace-match
7373 (shell-quote-argument
7374 (convert-standard-filename file)))
7376 (save-window-excursion
7377 (start-process-shell-command cmd nil cmd)
7378 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
7382 (funcall (cdr (assq 'file org-link-frame-setup)) file)
7384 (if line (goto-line line)
7385 (if search (org-link-search search))))
7387 (let ((file (convert-standard-filename file)))
7389 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
7390 (and (org-mode-p) (eq old-mode 'org-mode)
7391 (or (not (equal old-buffer (current-buffer)))
7392 (not (equal old-pos (point))))
7393 (org-mark-ring-push old-pos old-buffer))))
7395 (defun org-default-apps ()
7396 "Return the default applications for this operating system."
7398 ((eq system-type 'darwin)
7399 org-file-apps-defaults-macosx)
7400 ((eq system-type 'windows-nt)
7401 org-file-apps-defaults-windowsnt)
7402 (t org-file-apps-defaults-gnu)))
7404 (defun org-apps-regexp-alist (list &optional add-auto-mode)
7405 "Convert extensions to regular expressions in the cars of LIST.
7406 Also, weed out any non-string entries, because the return value is used
7407 only for regexp matching.
7408 When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
7409 point to the symbol `emacs', indicating that the file should
7410 be opened in Emacs."
7414 (if (not (stringp (car x)))
7416 (if (string-match "\\W" (car x))
7418 (cons (concat "\\." (car x) "\\'") (cdr x)))))
7421 (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
7423 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
7424 (defun org-file-remote-p (file)
7425 "Test whether FILE specifies a location on a remote system.
7426 Return non-nil if the location is indeed remote.
7428 For example, the filename \"/user@host:/foo\" specifies a location
7429 on the system \"/user@host:\"."
7430 (cond ((fboundp 'file-remote-p)
7431 (file-remote-p file))
7432 ((fboundp 'tramp-handle-file-remote-p)
7433 (tramp-handle-file-remote-p file))
7434 ((and (boundp 'ange-ftp-name-format)
7435 (string-match (car ange-ftp-name-format) file))
7442 (defun org-get-org-file ()
7443 "Read a filename, with default directory `org-directory'."
7444 (let ((default (or org-default-notes-file remember-data-file)))
7445 (read-file-name (format "File name [%s]: " default)
7446 (file-name-as-directory org-directory)
7449 (defun org-notes-order-reversed-p ()
7450 "Check if the current file should receive notes in reversed order."
7452 ((not org-reverse-note-order) nil)
7453 ((eq t org-reverse-note-order) t)
7454 ((not (listp org-reverse-note-order)) nil)
7456 (let ((all org-reverse-note-order)
7458 (while (setq entry (pop all))
7459 (if (string-match (car entry) buffer-file-name)
7460 (throw 'exit (cdr entry))))
7463 (defvar org-refile-target-table nil
7464 "The list of refile targets, created by `org-refile'.")
7466 (defvar org-agenda-new-buffers nil
7467 "Buffers created to visit agenda files.")
7469 (defun org-get-refile-targets (&optional default-buffer)
7470 "Produce a table with refile targets."
7471 (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
7472 targets txt re files f desc descre fast-path-p level)
7473 (message "Getting targets...")
7474 (with-current-buffer (or default-buffer (current-buffer))
7475 (while (setq entry (pop entries))
7476 (setq files (car entry) desc (cdr entry))
7477 (setq fast-path-p nil)
7479 ((null files) (setq files (list (current-buffer))))
7480 ((eq files 'org-agenda-files)
7481 (setq files (org-agenda-files 'unrestricted)))
7482 ((and (symbolp files) (fboundp files))
7483 (setq files (funcall files)))
7484 ((and (symbolp files) (boundp files))
7485 (setq files (symbol-value files))))
7486 (if (stringp files) (setq files (list files)))
7488 ((eq (car desc) :tag)
7489 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
7490 ((eq (car desc) :todo)
7491 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
7492 ((eq (car desc) :regexp)
7493 (setq descre (cdr desc)))
7494 ((eq (car desc) :level)
7495 (setq descre (concat "^\\*\\{" (number-to-string
7496 (if org-odd-levels-only
7497 (1- (* 2 (cdr desc)))
7500 ((eq (car desc) :maxlevel)
7501 (setq fast-path-p t)
7502 (setq descre (concat "^\\*\\{1," (number-to-string
7503 (if org-odd-levels-only
7504 (1- (* 2 (cdr desc)))
7507 (t (error "Bad refiling target description %s" desc)))
7508 (while (setq f (pop files))
7510 (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
7511 (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
7515 (goto-char (point-min))
7516 (while (re-search-forward descre nil t)
7517 (goto-char (point-at-bol))
7518 (when (looking-at org-complex-heading-regexp)
7519 (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
7520 txt (org-link-display-format (match-string 4))
7521 re (concat "^" (regexp-quote
7522 (buffer-substring (match-beginning 1)
7524 (if (match-end 5) (setq re (concat re "[ \t]+"
7526 (match-string 5)))))
7527 (setq re (concat re "[ \t]*$"))
7528 (when org-refile-use-outline-path
7529 (setq txt (mapconcat 'org-protect-slash
7531 (if (eq org-refile-use-outline-path 'file)
7532 (list (file-name-nondirectory
7533 (buffer-file-name (buffer-base-buffer))))
7534 (if (eq org-refile-use-outline-path 'full-file-path)
7535 (list (buffer-file-name (buffer-base-buffer)))))
7536 (org-get-outline-path fast-path-p level txt)
7539 (push (list txt f re (point)) targets))
7540 (goto-char (point-at-eol))))))))
7541 (message "Getting targets...done")
7542 (nreverse targets))))
7544 (defun org-protect-slash (s)
7545 (while (string-match "/" s)
7546 (setq s (replace-match "\\" t t s)))
7549 (defvar org-olpa (make-vector 20 nil))
7551 (defun org-get-outline-path (&optional fastp level heading)
7552 "Return the outline path to the current entry, as a list."
7556 (error "Outline path failure, more than 19 levels."))
7557 (loop for i from level upto 19 do
7558 (aset org-olpa i nil))
7560 (delq nil (append org-olpa nil))
7561 (aset org-olpa level heading)))
7564 (while (org-up-heading-safe)
7565 (when (looking-at org-complex-heading-regexp)
7566 (push (org-match-string-no-properties 4) rtn)))
7569 (defvar org-refile-history nil
7570 "History for refiling operations.")
7572 (defun org-refile (&optional goto default-buffer)
7573 "Move the entry at point to another heading.
7574 The list of target headings is compiled using the information in
7575 `org-refile-targets', which see. This list is created before each use
7576 and will therefore always be up-to-date.
7578 At the target location, the entry is filed as a subitem of the target heading.
7579 Depending on `org-reverse-note-order', the new subitem will either be the
7580 first or the last subitem.
7582 If there is an active region, all entries in that region will be moved.
7583 However, the region must fulfil the requirement that the first heading
7584 is the first one sets the top-level of the moved text - at most siblings
7585 below it are allowed.
7587 With prefix arg GOTO, the command will only visit the target location,
7588 not actually move anything.
7589 With a double prefix `C-u C-u', go to the location where the last refiling
7590 operation has put the subtree."
7592 (let* ((cbuf (current-buffer))
7593 (regionp (org-region-active-p))
7594 (region-start (and regionp (region-beginning)))
7595 (region-end (and regionp (region-end)))
7596 (region-length (and regionp (- region-end region-start)))
7597 (filename (buffer-file-name (buffer-base-buffer cbuf)))
7598 pos it nbuf file re level reversed)
7599 (when regionp (goto-char region-start)
7600 (unless (org-kill-is-subtree-p
7601 (buffer-substring region-start region-end))
7602 (error "The region is not a (sequence of) subtree(s)")))
7603 (if (equal goto '(16))
7604 (org-refile-goto-last-stored)
7605 (when (setq it (org-refile-get-location
7606 (if goto "Goto: " "Refile to: ") default-buffer))
7607 (setq file (nth 1 it)
7610 (if (and (equal (buffer-file-name) file)
7612 (and (>= pos region-start)
7613 (<= pos region-end))
7614 (and (>= pos (point))
7615 (< pos (save-excursion
7616 (org-end-of-subtree t t))))))
7617 (error "Cannot refile to position inside the tree or region"))
7619 (setq nbuf (or (find-buffer-visiting file)
7620 (find-file-noselect file)))
7623 (switch-to-buffer nbuf)
7625 (org-show-context 'org-goto))
7628 (kill-new (buffer-substring region-start region-end))
7629 (org-save-markers-in-region region-start region-end))
7630 (org-copy-subtree 1 nil t))
7632 (set-buffer (setq nbuf (or (find-buffer-visiting file)
7633 (find-file-noselect file))))
7634 (setq reversed (org-notes-order-reversed-p))
7639 (looking-at outline-regexp)
7640 (setq level (org-get-valid-level (funcall outline-level) 1))
7643 (or (outline-next-heading) (point-max))
7644 (or (save-excursion (outline-get-next-sibling))
7645 (org-end-of-subtree t t)
7647 (if (not (bolp)) (newline))
7648 (bookmark-set "org-refile-last-stored")
7649 (org-paste-subtree level))))
7651 (delete-region (point) (+ (point) region-length))
7653 (setq org-markers-to-move nil)
7654 (message "Refiled to \"%s\"" (car it)))))))
7656 (defun org-refile-goto-last-stored ()
7657 "Go to the location where the last refile was stored."
7659 (bookmark-jump "org-refile-last-stored")
7660 (message "This is the location of the last refile"))
7662 (defun org-refile-get-location (&optional prompt default-buffer)
7663 "Prompt the user for a refile location, using PROMPT."
7664 (let ((org-refile-targets org-refile-targets)
7665 (org-refile-use-outline-path org-refile-use-outline-path))
7666 (setq org-refile-target-table (org-get-refile-targets default-buffer)))
7667 (unless org-refile-target-table
7668 (error "No refile targets"))
7669 (let* ((cbuf (current-buffer))
7670 (cfunc (if (and org-refile-use-outline-path
7671 org-outline-path-complete-in-steps)
7672 'org-olpath-completing-read
7673 'org-ido-completing-read))
7674 (extra (if org-refile-use-outline-path "/" ""))
7675 (filename (buffer-file-name (buffer-base-buffer cbuf)))
7678 (if (not (equal filename (nth 1 x)))
7679 (cons (concat (car x) extra " ("
7680 (file-name-nondirectory (nth 1 x)) ")")
7682 (cons (concat (car x) extra) (cdr x))))
7683 org-refile-target-table))
7684 (completion-ignore-case t))
7685 (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
7688 (defun org-olpath-completing-read (prompt collection &rest args)
7689 "Read an outline path like a file name."
7690 (let ((thetable collection))
7692 'org-ido-completing-read prompt
7693 (lambda (string predicate &optional flag)
7694 (let (rtn r s f (l (length string)))
7698 (try-completion string thetable))
7701 (setq rtn (all-completions string thetable predicate))
7704 (setq r (substring x l))
7705 (if (string-match " ([^)]*)$" x)
7706 (setq f (match-string 0 x))
7708 (if (string-match "/" r)
7709 (concat string (substring r 0 (match-end 0)) f)
7714 (assoc string thetable)))
7720 (defun org-find-dblock (name)
7721 "Find the first dynamic block with name NAME in the buffer.
7722 If not found, stay at current position and return nil."
7725 (goto-char (point-min))
7726 (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
7728 (match-beginning 0))))
7729 (if pos (goto-char pos))
7732 (defconst org-dblock-start-re
7733 "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
7734 "Matches the startline of a dynamic block, with parameters.")
7736 (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
7737 "Matches the end of a dynamic block.")
7739 (defun org-create-dblock (plist)
7740 "Create a dynamic block section, with parameters taken from PLIST.
7741 PLIST must contain a :name entry which is used as name of the block."
7742 (unless (bolp) (newline))
7743 (let ((name (plist-get plist :name)))
7744 (insert "#+BEGIN: " name)
7746 (if (eq (car plist) :name)
7747 (setq plist (cddr plist))
7748 (insert " " (prin1-to-string (pop plist)))))
7749 (insert "\n\n#+END:\n")
7750 (beginning-of-line -2)))
7752 (defun org-prepare-dblock ()
7753 "Prepare dynamic block for refresh.
7754 This empties the block, puts the cursor at the insert position and returns
7755 the property list including an extra property :name with the block name."
7756 (unless (looking-at org-dblock-start-re)
7757 (error "Not at a dynamic block"))
7758 (let* ((begdel (1+ (match-end 0)))
7759 (name (org-no-properties (match-string 1)))
7760 (params (append (list :name name)
7761 (read (concat "(" (match-string 3) ")")))))
7762 (unless (re-search-forward org-dblock-end-re nil t)
7763 (error "Dynamic block not terminated"))
7766 (list :content (buffer-substring
7767 begdel (match-beginning 0)))))
7768 (delete-region begdel (match-beginning 0))
7773 (defun org-map-dblocks (&optional command)
7774 "Apply COMMAND to all dynamic blocks in the current buffer.
7775 If COMMAND is not given, use `org-update-dblock'."
7776 (let ((cmd (or command 'org-update-dblock))
7779 (goto-char (point-min))
7780 (while (re-search-forward org-dblock-start-re nil t)
7781 (goto-char (setq pos (match-beginning 0)))
7784 (error (message "Error during update of dynamic block")))
7786 (unless (re-search-forward org-dblock-end-re nil t)
7787 (error "Dynamic block not terminated"))))))
7789 (defun org-dblock-update (&optional arg)
7790 "User command for updating dynamic blocks.
7791 Update the dynamic block at point. With prefix ARG, update all dynamic
7792 blocks in the buffer."
7795 (org-update-all-dblocks)
7796 (or (looking-at org-dblock-start-re)
7797 (org-beginning-of-dblock))
7798 (org-update-dblock)))
7800 (defun org-update-dblock ()
7801 "Update the dynamic block at point
7802 This means to empty the block, parse for parameters and then call
7803 the correct writing function."
7804 (save-window-excursion
7805 (let* ((pos (point))
7806 (line (org-current-line))
7807 (params (org-prepare-dblock))
7808 (name (plist-get params :name))
7809 (cmd (intern (concat "org-dblock-write:" name))))
7810 (message "Updating dynamic block `%s' at line %d..." name line)
7811 (funcall cmd params)
7812 (message "Updating dynamic block `%s' at line %d...done" name line)
7815 (defun org-beginning-of-dblock ()
7816 "Find the beginning of the dynamic block at point.
7817 Error if there is no such block at point."
7821 (if (and (re-search-backward org-dblock-start-re nil t)
7822 (setq beg (match-beginning 0))
7823 (re-search-forward org-dblock-end-re nil t)
7824 (> (match-end 0) pos))
7827 (error "Not in a dynamic block"))))
7829 (defun org-update-all-dblocks ()
7830 "Update all dynamic blocks in the buffer.
7831 This function can be used in a hook."
7833 (org-map-dblocks 'org-update-dblock)))
7838 (defconst org-additional-option-like-keywords
7839 '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
7840 "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
7841 "BEGIN_EXAMPLE" "END_EXAMPLE"
7842 "BEGIN_QUOTE" "END_QUOTE"
7843 "BEGIN_VERSE" "END_VERSE"
7844 "BEGIN_SRC" "END_SRC"
7845 "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
7847 (defcustom org-structure-template-alist
7849 ("s" "#+begin_src ?\n\n#+end_src"
7850 "<src lang=\"?\">\n\n</src>")
7851 ("e" "#+begin_example\n?\n#+end_example"
7852 "<example>\n?\n</example>")
7853 ("q" "#+begin_quote\n?\n#+end_quote"
7854 "<quote>\n?\n</quote>")
7855 ("v" "#+begin_verse\n?\n#+end_verse"
7856 "<verse>\n?\n/verse>")
7857 ("l" "#+begin_latex\n?\n#+end_latex"
7858 "<literal style=\"latex\">\n?\n</literal>")
7860 "<literal style=\"latex\">?</literal>")
7861 ("h" "#+begin_html\n?\n#+end_html"
7862 "<literal style=\"html\">\n?\n</literal>")
7864 "<literal style=\"html\">?</literal>")
7865 ("a" "#+begin_ascii\n?\n#+end_ascii")
7867 ("i" "#+include %file ?"
7868 "<include file=%file markup=\"?\">")
7870 "Structure completion elements.
7871 This is a list of abbreviation keys and values. The value gets inserted
7872 it you type @samp{.} followed by the key and then the completion key,
7873 usually `M-TAB'. %file will be replaced by a file name after prompting
7874 for the file using completion.
7875 There are two templates for each key, the first uses the original Org syntax,
7876 the second uses Emacs Muse-like syntax tags. These Muse-like tags become
7877 the default when the /org-mtags.el/ module has been loaded. See also the
7878 variable `org-mtags-prefer-muse-templates'.
7879 This is an experimental feature, it is undecided if it is going to stay in."
7880 :group 'org-completion
7883 (string :tag "Template")
7884 (string :tag "Muse Template")))
7886 (defun org-try-structure-completion ()
7887 "Try to complete a structure template before point.
7888 This looks for strings like \"<e\" on an otherwise empty line and
7890 (let ((l (buffer-substring (point-at-bol) (point)))
7892 (when (and (looking-at "[ \t]*$")
7893 (string-match "^[ \t]*<\\([a-z]+\\)$"l)
7894 (setq a (assoc (match-string 1 l) org-structure-template-alist)))
7895 (org-complete-expand-structure-template (+ -1 (point-at-bol)
7896 (match-beginning 1)) a)
7899 (defun org-complete-expand-structure-template (start cell)
7900 "Expand a structure template."
7901 (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
7902 (rpl (nth (if musep 2 1) cell)))
7903 (delete-region start (point))
7904 (when (string-match "\\`#\\+" rpl)
7907 ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
7908 (delete-region (point-at-bol) (point)))
7910 (setq start (point))
7911 (if (string-match "%file" rpl)
7912 (setq rpl (replace-match
7916 (abbreviate-file-name (read-file-name "Include file: ")))
7920 (if (re-search-backward "\\?" start t) (delete-char 1))))
7923 (defun org-complete (&optional arg)
7924 "Perform completion on word at point.
7925 At the beginning of a headline, this completes TODO keywords as given in
7926 `org-todo-keywords'.
7927 If the current word is preceded by a backslash, completes the TeX symbols
7928 that are supported for HTML support.
7929 If the current word is preceded by \"#+\", completes special words for
7930 setting file options.
7931 In the line after \"#+STARTUP:, complete valid keywords.\"
7932 At all other locations, this simply calls the value of
7933 `org-completion-fallback-command'."
7935 (org-without-partial-completion
7939 (beg1 (save-excursion
7940 (skip-chars-backward (org-re "[:alnum:]_@"))
7942 (beg (save-excursion
7943 (skip-chars-backward "a-zA-Z0-9_:$")
7945 (confirm (lambda (x) (stringp (car x))))
7946 (searchhead (equal (char-before beg) ?*))
7948 (when (and (member (char-before beg1) '(?. ?<))
7949 (setq a (assoc (buffer-substring beg1 (point))
7950 org-structure-template-alist)))
7951 (org-complete-expand-structure-template (1- beg1) a)
7953 (tag (and (equal (char-before beg1) ?:)
7954 (equal (char-after (point-at-bol)) ?*)))
7955 (prop (and (equal (char-before beg1) ?:)
7956 (not (equal (char-after (point-at-bol)) ?*))))
7957 (texp (equal (char-before beg) ?\\))
7958 (link (equal (char-before beg) ?\[))
7959 (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
7962 (startup (string-match "^#\\+STARTUP:.*"
7963 (buffer-substring (point-at-bol) (point))))
7964 (completion-ignore-case opt)
7974 (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
7975 (cons (match-string 2 x) (match-string 1 x)))
7976 (org-split-string (org-get-current-options) "\n"))
7977 (mapcar 'list org-additional-option-like-keywords)))
7979 (setq type :startup)
7980 org-startup-options)
7981 (link (append org-link-abbrev-alist-local
7982 org-link-abbrev-alist))
7986 ((string-match "\\`\\*+[ \t]+\\'"
7987 (buffer-substring (point-at-bol) beg))
7989 (mapcar 'list org-todo-keywords-1))
7991 (setq type :searchhead)
7993 (goto-char (point-min))
7994 (while (re-search-forward org-todo-line-regexp nil t)
7996 (org-make-org-heading-search-string
7997 (match-string 3) t))
8000 (tag (setq type :tag beg beg1)
8001 (or org-tag-alist (org-get-buffer-tags)))
8002 (prop (setq type :prop beg beg1)
8003 (mapcar 'list (org-buffer-property-keys nil t t)))
8005 (call-interactively org-completion-fallback-command)
8006 (throw 'exit nil)))))
8007 (pattern (buffer-substring-no-properties beg end))
8008 (completion (try-completion pattern table confirm)))
8009 (cond ((eq completion t)
8010 (if (not (assoc (upcase pattern) table))
8011 (message "Already complete")
8012 (if (and (equal type :opt)
8013 (not (member (car (assoc (upcase pattern) table))
8014 org-additional-option-like-keywords)))
8015 (insert (substring (cdr (assoc (upcase pattern) table))
8017 (if (memq type '(:tag :prop)) (insert ":")))))
8019 (message "Can't find completion for \"%s\"" pattern)
8021 ((not (string= pattern completion))
8022 (delete-region beg end)
8023 (if (string-match " +$" completion)
8024 (setq completion (replace-match "" t t completion)))
8026 (if (get-buffer-window "*Completions*")
8027 (delete-window (get-buffer-window "*Completions*")))
8028 (if (assoc completion table)
8029 (if (eq type :todo) (insert " ")
8030 (if (memq type '(:tag :prop)) (insert ":"))))
8031 (if (and (equal type :opt) (assoc completion table))
8032 (message "%s" (substitute-command-keys
8033 "Press \\[org-complete] again to insert example settings"))))
8035 (message "Making completion list...")
8036 (let ((list (sort (all-completions pattern table confirm)
8038 (with-output-to-temp-buffer "*Completions*"
8040 ;; Protection needed for XEmacs and emacs 21
8041 (display-completion-list list pattern)
8042 (error (display-completion-list list)))))
8043 (message "Making completion list...%s" "done")))))))
8045 ;;;; TODO, DEADLINE, Comments
8047 (defun org-toggle-comment ()
8048 "Change the COMMENT state of an entry."
8051 (org-back-to-heading)
8052 (let (case-fold-search)
8053 (if (looking-at (concat outline-regexp
8054 "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
8055 (replace-match "" t t nil 1)
8056 (if (looking-at outline-regexp)
8058 (goto-char (match-end 0))
8059 (insert org-comment-string " ")))))))
8061 (defvar org-last-todo-state-is-todo nil
8062 "This is non-nil when the last TODO state change led to a TODO state.
8063 If the last change removed the TODO tag or switched to DONE, then
8066 (defvar org-setting-tags nil) ; dynamically skipped
8068 (defun org-parse-local-options (string var)
8069 "Parse STRING for startup setting relevant for variable VAR."
8070 (let ((rtn (symbol-value var))
8073 (if (or (not string) (not (string-match "\\S-" string)))
8075 (setq opts (delq nil (mapcar (lambda (x)
8076 (setq e (assoc x org-startup-options))
8077 (if (eq (nth 1 e) var) e nil))
8078 (org-split-string string "[ \t]+"))))
8082 (while (setq e (pop opts))
8084 (setq rtn (nth 2 e))
8085 (if (not (listp rtn)) (setq rtn nil))
8086 (push (nth 2 e) rtn)))
8089 (defvar org-blocker-hook nil
8090 "Hook for functions that are allowed to block a state change.
8092 Each function gets as its single argument a property list, see
8093 `org-trigger-hook' for more information about this list.
8095 If any of the functions in this hook returns nil, the state change
8098 (defvar org-trigger-hook nil
8099 "Hook for functions that are triggered by a state change.
8101 Each function gets as its single argument a property list with at least
8102 the following elements:
8104 (:type type-of-change :position pos-at-entry-start
8105 :from old-state :to new-state)
8107 Depending on the type, more properties may be present.
8109 This mechanism is currently implemented for:
8113 :type todo-state-change
8114 :from previous state (keyword as a string), or nil
8115 :to new state (keyword as a string), or nil")
8117 (defvar org-agenda-headline-snapshot-before-repeat)
8118 (defun org-todo (&optional arg)
8119 "Change the TODO state of an item.
8120 The state of an item is given by a keyword at the start of the heading,
8122 *** TODO Write paper
8125 The different keywords are specified in the variable `org-todo-keywords'.
8126 By default the available states are \"TODO\" and \"DONE\".
8127 So for this example: when the item starts with TODO, it is changed to DONE.
8128 When it starts with DONE, the DONE is removed. And when neither TODO nor
8129 DONE are present, add TODO at the beginning of the heading.
8131 With C-u prefix arg, use completion to determine the new state.
8132 With numeric prefix arg, switch to that state.
8134 For calling through lisp, arg is also interpreted in the following way:
8135 'none -> empty state
8136 \"\"(empty string) -> switch to empty state
8137 'done -> switch to DONE
8138 'nextset -> switch to the next set of keywords
8139 'previousset -> switch to the previous set of keywords
8140 \"WAITING\" -> switch to the specified keyword, but only if it
8141 really is a member of `org-todo-keywords'."
8145 (org-back-to-heading)
8146 (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
8147 (or (looking-at (concat " +" org-todo-regexp " *"))
8149 (let* ((match-data (match-data))
8150 (startpos (point-at-bol))
8151 (logging (save-match-data (org-entry-get nil "LOGGING" t)))
8152 (org-log-done org-log-done)
8153 (org-log-repeat org-log-repeat)
8154 (org-todo-log-states org-todo-log-states)
8155 (this (match-string 1))
8156 (hl-pos (match-beginning 0))
8157 (head (org-get-todo-sequence-head this))
8158 (ass (assoc head org-todo-kwd-alist))
8159 (interpret (nth 1 ass))
8160 (done-word (nth 3 ass))
8161 (final-done-word (nth 4 ass))
8162 (last-state (or this ""))
8163 (completion-ignore-case t)
8164 (member (member this org-todo-keywords-1))
8167 ((and org-todo-key-trigger
8168 (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
8169 (and (not arg) org-use-fast-todo-selection
8170 (not (eq org-use-fast-todo-selection 'prefix)))))
8171 ;; Use fast selection
8172 (org-fast-todo-selection))
8173 ((and (equal arg '(4))
8174 (or (not org-use-fast-todo-selection)
8175 (not org-todo-key-trigger)))
8176 ;; Read a state with completion
8177 (org-ido-completing-read "State: " (mapcar (lambda(x) (list x))
8178 org-todo-keywords-1)
8182 (if tail (car tail) nil)
8183 (car org-todo-keywords-1)))
8185 (if (equal member org-todo-keywords-1)
8188 (nth (- (length org-todo-keywords-1) (length tail) 2)
8189 org-todo-keywords-1)
8190 (org-last org-todo-keywords-1))))
8191 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
8192 (setq arg nil))) ; hack to fall back to cycling
8194 ;; user or caller requests a specific state
8196 ((equal arg "") nil)
8197 ((eq arg 'none) nil)
8198 ((eq arg 'done) (or done-word (car org-done-keywords)))
8200 (or (car (cdr (member head org-todo-heads)))
8201 (car org-todo-heads)))
8202 ((eq arg 'previousset)
8203 (let ((org-todo-heads (reverse org-todo-heads)))
8204 (or (car (cdr (member head org-todo-heads)))
8205 (car org-todo-heads))))
8206 ((car (member arg org-todo-keywords-1)))
8207 ((nth (1- (prefix-numeric-value arg))
8208 org-todo-keywords-1))))
8209 ((null member) (or head (car org-todo-keywords-1)))
8210 ((equal this final-done-word) nil) ;; -> make empty
8211 ((null tail) nil) ;; -> first entry
8212 ((eq interpret 'sequence)
8214 ((memq interpret '(type priority))
8215 (if (eq this-command last-command)
8217 (if (> (length tail) 0)
8218 (or done-word (car org-done-keywords))
8221 (next (if state (concat " " state " ") " "))
8222 (change-plist (list :type 'todo-state-change :from this :to state
8223 :position startpos))
8225 (when org-blocker-hook
8226 (unless (save-excursion
8228 (run-hook-with-args-until-failure
8229 'org-blocker-hook change-plist)))
8231 (error "TODO state change from %s to %s blocked" this state)
8233 (message "TODO state change from %s to %s blocked" this state)
8234 (throw 'exit nil))))
8235 (store-match-data match-data)
8236 (replace-match next t t)
8237 (unless (pos-visible-in-window-p hl-pos)
8238 (message "TODO state changed to %s" (org-trim next)))
8240 (setq head (org-get-todo-sequence-head state)
8241 ass (assoc head org-todo-kwd-alist)
8242 interpret (nth 1 ass)
8243 done-word (nth 3 ass)
8244 final-done-word (nth 4 ass)))
8245 (when (memq arg '(nextset previousset))
8246 (message "Keyword-Set %d/%d: %s"
8247 (- (length org-todo-sets) -1
8248 (length (memq (assoc state org-todo-sets) org-todo-sets)))
8249 (length org-todo-sets)
8250 (mapconcat 'identity (assoc state org-todo-sets) " ")))
8251 (setq org-last-todo-state-is-todo
8252 (not (member state org-done-keywords)))
8253 (setq now-done-p (and (member state org-done-keywords)
8254 (not (member this org-done-keywords))))
8255 (and logging (org-local-logging logging))
8256 (when (and (or org-todo-log-states org-log-done)
8257 (not (memq arg '(nextset previousset))))
8258 ;; we need to look at recording a time and note
8259 (setq dolog (or (nth 1 (assoc state org-todo-log-states))
8260 (nth 2 (assoc this org-todo-log-states))))
8262 (member state org-not-done-keywords)
8263 (not (member this org-not-done-keywords)))
8264 ;; This is now a todo state and was not one before
8265 ;; If there was a CLOSED time stamp, get rid of it.
8266 (org-add-planning-info nil nil 'closed))
8267 (when (and now-done-p org-log-done)
8268 ;; It is now done, and it was not done before
8269 (org-add-planning-info 'closed (org-current-time))
8270 (if (and (not dolog) (eq 'note org-log-done))
8271 (org-add-log-setup 'done state 'findpos 'note)))
8272 (when (and state dolog)
8273 ;; This is a non-nil state, and we need to log it
8274 (org-add-log-setup 'state state 'findpos dolog)))
8275 ;; Fixup tag positioning
8276 (org-todo-trigger-tag-changes state)
8277 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
8278 (when org-provide-todo-statistics
8279 (org-update-parent-todo-statistics))
8280 (run-hooks 'org-after-todo-state-change-hook)
8281 (if (and arg (not (member state org-done-keywords)))
8282 (setq head (org-get-todo-sequence-head state)))
8283 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
8284 ;; Do we need to trigger a repeat?
8286 (when (boundp 'org-agenda-headline-snapshot-before-repeat)
8287 ;; This is for the agenda, take a snapshot of the headline.
8289 (setq org-agenda-headline-snapshot-before-repeat
8290 (org-get-heading))))
8291 (org-auto-repeat-maybe state))
8292 ;; Fixup cursor location if close to the keyword
8293 (if (and (outline-on-heading-p)
8295 (save-excursion (beginning-of-line 1)
8296 (looking-at org-todo-line-regexp))
8297 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
8299 (goto-char (or (match-end 2) (match-end 1)))
8301 (when org-trigger-hook
8303 (run-hook-with-args 'org-trigger-hook change-plist)))))))
8305 (defun org-update-parent-todo-statistics ()
8306 "Update any statistics cookie in the parent of the current headline."
8308 (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
8309 level (cnt-all 0) (cnt-done 0) is-percent kwd)
8312 (setq level (org-up-heading-safe))
8314 (re-search-forward box-re (point-at-eol) t))
8316 (setq is-percent (match-end 2))
8318 (unless (outline-next-heading) (throw 'exit nil))
8319 (while (looking-at org-todo-line-regexp)
8320 (setq kwd (match-string 2))
8321 (and kwd (setq cnt-all (1+ cnt-all)))
8322 (and (member kwd org-done-keywords)
8323 (setq cnt-done (1+ cnt-done)))
8325 (org-forward-same-level 1)
8326 (error (end-of-line 1)))))
8329 (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
8330 (format "[%d/%d]" cnt-done cnt-all)))
8331 (run-hook-with-args 'org-after-todo-statistics-hook
8332 cnt-done (- cnt-all cnt-done))))))
8334 (defvar org-after-todo-statistics-hook nil
8335 "Hook that is called after a TODO statistics cookie has been updated.
8336 Each function is called with two arguments: the number of not-done entries
8337 and the number of done entries.
8339 For example, the following function, when added to this hook, will switch
8340 an entry to DONE when all children are done, and back to TODO when new
8341 entries are set to a TODO status. Note that this hook is only called
8342 when there is a statistics cookie in the headline!
8344 (defun org-summary-todo (n-done n-not-done)
8345 \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
8346 (let (org-log-done org-log-states) ; turn off logging
8347 (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
8350 (defun org-todo-trigger-tag-changes (state)
8351 "Apply the changes defined in `org-todo-state-tags-triggers'."
8352 (let ((l org-todo-state-tags-triggers)
8354 (when (or (not state) (equal state ""))
8355 (setq changes (append changes (cdr (assoc "" l)))))
8356 (when (and (stringp state) (> (length state) 0))
8357 (setq changes (append changes (cdr (assoc state l)))))
8358 (when (member state org-not-done-keywords)
8359 (setq changes (append changes (cdr (assoc 'todo l)))))
8360 (when (member state org-done-keywords)
8361 (setq changes (append changes (cdr (assoc 'done l)))))
8363 (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
8365 (defun org-local-logging (value)
8366 "Get logging settings from a property VALUE."
8368 ;; directly set the variables, they are already local.
8369 (setq org-log-done nil
8371 org-todo-log-states nil)
8372 (setq words (org-split-string value))
8373 (while (setq w (pop words))
8375 ((setq a (assoc w org-startup-options))
8376 (and (member (nth 1 a) '(org-log-done org-log-repeat))
8377 (set (nth 1 a) (nth 2 a))))
8378 ((setq a (org-extract-log-state-settings w))
8379 (and (member (car a) org-todo-keywords-1)
8380 (push a org-todo-log-states)))))))
8382 (defun org-get-todo-sequence-head (kwd)
8383 "Return the head of the TODO sequence to which KWD belongs.
8384 If KWD is not set, check if there is a text property remembering the
8389 (or (get-text-property (point-at-bol) 'org-todo-head)
8391 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
8392 nil (point-at-eol)))
8393 (get-text-property p 'org-todo-head))))
8394 ((not (member kwd org-todo-keywords-1))
8395 (car org-todo-keywords-1))
8396 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
8398 (defun org-fast-todo-selection ()
8399 "Fast TODO keyword selection with single keys.
8400 Returns the new TODO keyword, or nil if no state change should occur."
8401 (let* ((fulltable org-todo-key-alist)
8402 (done-keywords org-done-keywords) ;; needed for the faces.
8403 (maxlen (apply 'max (mapcar
8405 (if (stringp (car x)) (string-width (car x)) 0))
8408 (fwidth (+ maxlen 3 1 3))
8409 (ncol (/ (- (window-width) 4) fwidth))
8412 (save-window-excursion
8414 (set-buffer (get-buffer-create " *Org todo*"))
8415 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
8417 (org-set-local 'org-done-keywords done-keywords)
8418 (setq tbl fulltable cnt 0)
8419 (while (setq e (pop tbl))
8421 ((equal e '(:startgroup))
8422 (push '() groups) (setq ingroup t)
8423 (when (not (= cnt 0))
8427 ((equal e '(:endgroup))
8428 (setq ingroup nil cnt 0)
8431 (setq tg (car e) c (cdr e))
8432 (if ingroup (push tg (car groups)))
8433 (setq tg (org-add-props tg nil 'face
8434 (org-get-todo-face tg)))
8435 (if (and (= cnt 0) (not ingroup)) (insert " "))
8436 (insert "[" c "] " tg (make-string
8437 (- fwidth 4 (length tg)) ?\ ))
8438 (when (= (setq cnt (1+ cnt)) ncol)
8440 (if ingroup (insert " "))
8443 (goto-char (point-min))
8444 (if (not expert) (org-fit-window-to-buffer))
8445 (message "[a-z..]:Set [SPC]:clear")
8446 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
8449 (and (= c ?q) (not (rassoc c fulltable))))
8452 ((setq e (rassoc c fulltable) tg (car e))
8454 (t (setq quit-flag t))))))
8456 (defun org-entry-is-todo-p ()
8457 (member (org-get-todo-state) org-not-done-keywords))
8459 (defun org-entry-is-done-p ()
8460 (member (org-get-todo-state) org-done-keywords))
8462 (defun org-get-todo-state ()
8464 (org-back-to-heading t)
8465 (and (looking-at org-todo-line-regexp)
8469 (defun org-at-date-range-p (&optional inactive-ok)
8470 "Is the cursor inside a date range?"
8474 (let ((pos (point)))
8475 (skip-chars-backward "^[<\r\n")
8476 (skip-chars-backward "<[")
8477 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
8478 (>= (match-end 0) pos)
8480 (skip-chars-backward "^<[\r\n")
8481 (skip-chars-backward "<[")
8482 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
8483 (>= (match-end 0) pos)
8487 (defun org-get-repeat ()
8488 "Check if there is a deadline/schedule with repeater in this entry."
8491 (org-back-to-heading t)
8492 (if (re-search-forward
8493 org-repeat-re (save-excursion (outline-next-heading) (point)) t)
8494 (match-string 1)))))
8496 (defvar org-last-changed-timestamp)
8497 (defvar org-last-inserted-timestamp)
8498 (defvar org-log-post-message)
8499 (defvar org-log-note-purpose)
8500 (defvar org-log-note-how)
8501 (defvar org-log-note-extra)
8502 (defun org-auto-repeat-maybe (done-word)
8503 "Check if the current headline contains a repeated deadline/schedule.
8504 If yes, set TODO state back to what it was and change the base date
8505 of repeating deadline/scheduled time stamps to new date.
8506 This function is run automatically after each state change to a DONE state."
8507 ;; last-state is dynamically scoped into this function
8508 (let* ((repeat (org-get-repeat))
8509 (aa (assoc last-state org-todo-kwd-alist))
8510 (interpret (nth 1 aa))
8512 (whata '(("d" . day) ("m" . month) ("y" . year)))
8513 (msg "Entry repeats: ")
8515 (org-todo-log-states nil)
8516 (nshiftmax 10) (nshift 0)
8517 re type n what ts mb0 time)
8519 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
8520 (org-todo (if (eq interpret 'type) last-state head))
8521 (when org-log-repeat
8522 (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
8523 (memq 'org-add-log-note post-command-hook))
8524 ;; OK, we are already setup for some record
8525 (if (eq org-log-repeat 'note)
8526 ;; make sure we take a note, not only a time stamp
8527 (setq org-log-note-how 'note))
8528 ;; Set up for taking a record
8529 (org-add-log-setup 'state (or done-word (car org-done-keywords))
8530 'findpos org-log-repeat)))
8531 (org-back-to-heading t)
8532 (org-add-planning-info nil nil 'closed)
8533 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
8534 org-deadline-time-regexp "\\)\\|\\("
8535 org-ts-regexp "\\)"))
8536 (while (re-search-forward
8537 re (save-excursion (outline-next-heading) (point)) t)
8538 (setq type (if (match-end 1) org-scheduled-string
8539 (if (match-end 3) org-deadline-string "Plain:"))
8540 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
8541 mb0 (match-beginning 0))
8542 (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
8543 (setq n (string-to-number (match-string 2 ts))
8544 what (match-string 3 ts))
8545 (if (equal what "w") (setq n (* n 7) what "d"))
8546 ;; Preparation, see if we need to modify the start date for the change
8548 (setq time (save-match-data (org-time-string-to-time ts)))
8550 ((equal (match-string 1 ts) ".")
8551 ;; Shift starting date to today
8552 (org-timestamp-change
8553 (- (time-to-days (current-time)) (time-to-days time))
8555 ((equal (match-string 1 ts) "+")
8556 (while (or (= nshift 0)
8557 (<= (time-to-days time) (time-to-days (current-time))))
8558 (when (= (incf nshift) nshiftmax)
8559 (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
8561 (org-timestamp-change n (cdr (assoc what whata)))
8562 (org-at-timestamp-p t)
8563 (setq ts (match-string 1))
8564 (setq time (save-match-data (org-time-string-to-time ts))))
8565 (org-timestamp-change (- n) (cdr (assoc what whata)))
8566 ;; rematch, so that we have everything in place for the real shift
8567 (org-at-timestamp-p t)
8568 (setq ts (match-string 1))
8569 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
8570 (org-timestamp-change n (cdr (assoc what whata)))
8571 (setq msg (concat msg type " " org-last-changed-timestamp " "))))
8572 (setq org-log-post-message msg)
8573 (message "%s" msg))))
8575 (defun org-show-todo-tree (arg)
8576 "Make a compact tree which shows all headlines marked with TODO.
8577 The tree will show the lines where the regexp matches, and all higher
8578 headlines above the match.
8579 With a \\[universal-argument] prefix, also show the DONE entries.
8580 With a numeric prefix N, construct a sparse tree for the Nth element
8581 of `org-todo-keywords-1'."
8583 (let ((case-fold-search nil)
8585 (cond ((null arg) org-not-done-regexp)
8587 (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
8588 (mapcar 'list org-todo-keywords-1))))
8590 (mapconcat 'identity (org-split-string kwd "|") "\\|")
8592 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
8593 (regexp-quote (nth (1- (prefix-numeric-value arg))
8594 org-todo-keywords-1)))
8595 (t (error "Invalid prefix argument: %s" arg)))))
8596 (message "%d TODO entries found"
8597 (org-occur (concat "^" outline-regexp " *" kwd-re )))))
8599 (defun org-deadline (&optional remove time)
8600 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
8601 With argument REMOVE, remove any deadline from the item.
8602 When TIME is set, it should be an internal time specification, and the
8603 scheduling will use the corresponding date."
8607 (org-remove-timestamp-with-keyword org-deadline-string)
8608 (message "Item no longer has a deadline."))
8609 (if (org-get-repeat)
8610 (error "Cannot change deadline on task with repeater, please do that by hand")
8611 (org-add-planning-info 'deadline time 'closed)
8612 (message "Deadline on %s" org-last-inserted-timestamp))))
8614 (defun org-schedule (&optional remove time)
8615 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
8616 With argument REMOVE, remove any scheduling date from the item.
8617 When TIME is set, it should be an internal time specification, and the
8618 scheduling will use the corresponding date."
8622 (org-remove-timestamp-with-keyword org-scheduled-string)
8623 (message "Item is no longer scheduled."))
8624 (if (org-get-repeat)
8625 (error "Cannot reschedule task with repeater, please do that by hand")
8626 (org-add-planning-info 'scheduled time 'closed)
8627 (message "Scheduled to %s" org-last-inserted-timestamp))))
8629 (defun org-remove-timestamp-with-keyword (keyword)
8630 "Remove all time stamps with KEYWORD in the current entry."
8631 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
8634 (org-back-to-heading t)
8636 (org-end-of-subtree t t)
8637 (while (re-search-backward re beg t)
8639 (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
8640 (equal (char-before) ?\ ))
8641 (backward-delete-char 1)
8642 (if (string-match "^[ \t]*$" (buffer-substring
8643 (point-at-bol) (point-at-eol)))
8644 (delete-region (point-at-bol)
8645 (min (point-max) (1+ (point-at-eol))))))))))
8647 (defun org-add-planning-info (what &optional time &rest remove)
8648 "Insert new timestamp with keyword in the line directly after the headline.
8649 WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
8650 If non is given, the user is prompted for a date.
8651 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
8654 (let (org-time-was-given org-end-time-was-given ts
8655 end default-time default-input)
8657 (when (and (not time) (memq what '(scheduled deadline)))
8658 ;; Try to get a default date/time from existing timestamp
8660 (org-back-to-heading t)
8661 (setq end (save-excursion (outline-next-heading) (point)))
8662 (when (re-search-forward (if (eq what 'scheduled)
8663 org-scheduled-time-regexp
8664 org-deadline-time-regexp)
8666 (setq ts (match-string 1)
8668 (apply 'encode-time (org-parse-time-string ts))
8669 default-input (and ts (org-get-compact-tod ts))))))
8671 ;; If necessary, get the time from the user
8672 (setq time (or time (org-read-date nil 'to-time nil nil
8673 default-time default-input))))
8675 (when (and org-insert-labeled-timestamps-at-point
8676 (member what '(scheduled deadline)))
8678 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
8679 (org-insert-time-stamp time org-time-was-given
8680 nil nil nil (list org-end-time-was-given))
8684 (let (col list elt ts buffer-invisibility-spec)
8685 (org-back-to-heading t)
8686 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
8687 (goto-char (match-end 1))
8688 (setq col (current-column))
8689 (goto-char (match-end 0))
8690 (if (eobp) (insert "\n") (forward-char 1))
8691 (if (and (not (looking-at outline-regexp))
8692 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
8694 (not (equal (match-string 1) org-clock-string)))
8695 (narrow-to-region (match-beginning 0) (match-end 0))
8696 (insert-before-markers "\n")
8698 (narrow-to-region (point) (point))
8699 (and org-adapt-indentation (org-indent-to-column col)))
8700 ;; Check if we have to remove something.
8701 (setq list (cons what remove))
8703 (setq elt (pop list))
8704 (goto-char (point-min))
8705 (when (or (and (eq elt 'scheduled)
8706 (re-search-forward org-scheduled-time-regexp nil t))
8707 (and (eq elt 'deadline)
8708 (re-search-forward org-deadline-time-regexp nil t))
8709 (and (eq elt 'closed)
8710 (re-search-forward org-closed-time-regexp nil t)))
8712 (if (looking-at "--+<[^>]+>") (replace-match ""))
8713 (if (looking-at " +") (replace-match ""))))
8714 (goto-char (point-max))
8717 (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
8718 (cond ((eq what 'scheduled) org-scheduled-string)
8719 ((eq what 'deadline) org-deadline-string)
8720 ((eq what 'closed) org-closed-string))
8722 (setq ts (org-insert-time-stamp
8724 (or org-time-was-given
8725 (and (eq what 'closed) org-log-done-with-time))
8727 nil nil (list org-end-time-was-given)))
8729 (goto-char (point-min))
8731 (if (and (looking-at "[ \t]+\n")
8732 (equal (char-before) ?\n))
8733 (delete-region (1- (point)) (point-at-eol)))
8736 (defvar org-log-note-marker (make-marker))
8737 (defvar org-log-note-purpose nil)
8738 (defvar org-log-note-state nil)
8739 (defvar org-log-note-how nil)
8740 (defvar org-log-note-extra nil)
8741 (defvar org-log-note-window-configuration nil)
8742 (defvar org-log-note-return-to (make-marker))
8743 (defvar org-log-post-message nil
8744 "Message to be displayed after a log note has been stored.
8745 The auto-repeater uses this.")
8747 (defun org-add-note ()
8748 "Add a note to the current entry.
8749 This is done in the same way as adding a state change note."
8751 (org-add-log-setup 'note nil 'findpos nil))
8753 (defvar org-property-end-re)
8754 (defun org-add-log-setup (&optional purpose state findpos how &optional extra)
8755 "Set up the post command hook to take a note.
8756 If this is about to TODO state change, the new state is expected in STATE.
8757 When FINDPOS is non-nil, find the correct position for the note in
8758 the current entry. If not, assume that it can be inserted at point.
8759 HOW is an indicator what kind of note should be created.
8760 EXTRA is additional text that will be inserted into the notes buffer."
8764 (org-back-to-heading t)
8765 (narrow-to-region (point) (save-excursion
8766 (outline-next-heading) (point)))
8767 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
8768 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
8770 (goto-char (match-end 0))
8771 (when (and org-log-state-notes-insert-after-drawers
8773 (forward-line) (looking-at org-drawer-regexp)))
8774 (progn (forward-line)
8775 (while (looking-at org-drawer-regexp)
8776 (goto-char (match-end 0))
8777 (re-search-forward org-property-end-re (point-max) t)
8780 (unless org-log-states-order-reversed
8781 (and (= (char-after) ?\n) (forward-char 1))
8782 (org-skip-over-state-notes)
8783 (skip-chars-backward " \t\n\r")))
8784 (move-marker org-log-note-marker (point))
8785 (setq org-log-note-purpose purpose
8786 org-log-note-state state
8787 org-log-note-how how
8788 org-log-note-extra extra)
8789 (add-hook 'post-command-hook 'org-add-log-note 'append))))
8791 (defun org-skip-over-state-notes ()
8792 "Skip past the list of State notes in an entry."
8793 (if (looking-at "\n[ \t]*- State") (forward-char 1))
8794 (while (looking-at "[ \t]*- State")
8797 (error (org-end-of-item)))))
8799 (defun org-add-log-note (&optional purpose)
8800 "Pop up a window for taking a note, and add this note later at point."
8801 (remove-hook 'post-command-hook 'org-add-log-note)
8802 (setq org-log-note-window-configuration (current-window-configuration))
8803 (delete-other-windows)
8804 (move-marker org-log-note-return-to (point))
8805 (switch-to-buffer (marker-buffer org-log-note-marker))
8806 (goto-char org-log-note-marker)
8807 (org-switch-to-buffer-other-window "*Org Note*")
8809 (if (memq org-log-note-how '(time state))
8810 (let (current-prefix-arg) (org-store-log-note))
8811 (let ((org-inhibit-startup t)) (org-mode))
8812 (insert (format "# Insert note for %s.
8813 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
8815 ((eq org-log-note-purpose 'clock-out) "stopped clock")
8816 ((eq org-log-note-purpose 'done) "closed todo item")
8817 ((eq org-log-note-purpose 'state)
8818 (format "state change to \"%s\"" org-log-note-state))
8819 ((eq org-log-note-purpose 'note)
8821 (t (error "This should not happen")))))
8822 (if org-log-note-extra (insert org-log-note-extra))
8823 (org-set-local 'org-finish-function 'org-store-log-note)))
8825 (defvar org-note-abort nil) ; dynamically scoped
8826 (defun org-store-log-note ()
8827 "Finish taking a log note, and insert it to where it belongs."
8828 (let ((txt (buffer-string))
8829 (note (cdr (assq org-log-note-purpose org-log-note-headings)))
8831 (kill-buffer (current-buffer))
8832 (while (string-match "\\`#.*\n[ \t\n]*" txt)
8833 (setq txt (replace-match "" t t txt)))
8834 (if (string-match "\\s-+\\'" txt)
8835 (setq txt (replace-match "" t t txt)))
8836 (setq lines (org-split-string txt "\n"))
8837 (when (and note (string-match "\\S-" note))
8839 (org-replace-escapes
8841 (list (cons "%u" (user-login-name))
8842 (cons "%U" user-full-name)
8843 (cons "%t" (format-time-string
8844 (org-time-stamp-format 'long 'inactive)
8846 (cons "%s" (if org-log-note-state
8847 (concat "\"" org-log-note-state "\"")
8849 (if lines (setq note (concat note " \\\\")))
8851 (when (or current-prefix-arg org-note-abort) (setq lines nil))
8854 (set-buffer (marker-buffer org-log-note-marker))
8856 (goto-char org-log-note-marker)
8857 (move-marker org-log-note-marker nil)
8859 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
8860 (indent-relative nil)
8861 (insert "- " (pop lines))
8862 (org-indent-line-function)
8863 (beginning-of-line 1)
8864 (looking-at "[ \t]*")
8865 (setq ind (concat (match-string 0) " "))
8867 (while lines (insert "\n" ind (pop lines)))))))
8868 (set-window-configuration org-log-note-window-configuration)
8869 (with-current-buffer (marker-buffer org-log-note-return-to)
8870 (goto-char org-log-note-return-to))
8871 (move-marker org-log-note-return-to nil)
8872 (and org-log-post-message (message "%s" org-log-post-message)))
8874 (defun org-sparse-tree (&optional arg)
8875 "Create a sparse tree, prompt for the details.
8876 This command can create sparse trees. You first need to select the type
8877 of match used to create the tree:
8879 t Show entries with a specific TODO keyword.
8880 T Show entries selected by a tags match.
8881 p Enter a property name and its value (both with completion on existing
8882 names/values) and show entries with that property.
8883 r Show entries matching a regular expression
8884 d Show deadlines due within `org-deadline-warning-days'."
8886 (let (ans kwd value)
8887 (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
8888 (setq ans (read-char-exclusive))
8891 (call-interactively 'org-check-deadlines))
8893 (call-interactively 'org-check-before-date))
8895 (org-show-todo-tree '(4)))
8897 (call-interactively 'org-tags-sparse-tree))
8898 ((member ans '(?p ?P))
8899 (setq kwd (org-ido-completing-read "Property: "
8900 (mapcar 'list (org-buffer-property-keys))))
8901 (setq value (org-ido-completing-read "Value: "
8902 (mapcar 'list (org-property-values kwd))))
8903 (unless (string-match "\\`{.*}\\'" value)
8904 (setq value (concat "\"" value "\"")))
8905 (org-tags-sparse-tree arg (concat kwd "=" value)))
8906 ((member ans '(?r ?R ?/))
8907 (call-interactively 'org-occur))
8908 (t (error "No such sparse tree command \"%c\"" ans)))))
8910 (defvar org-occur-highlights nil
8911 "List of overlays used for occur matches.")
8912 (make-variable-buffer-local 'org-occur-highlights)
8913 (defvar org-occur-parameters nil
8914 "Parameters of the active org-occur calls.
8915 This is a list, each call to org-occur pushes as cons cell,
8916 containing the regular expression and the callback, onto the list.
8917 The list can contain several entries if `org-occur' has been called
8918 several time with the KEEP-PREVIOUS argument. Otherwise, this list
8919 will only contain one set of parameters. When the highlights are
8920 removed (for example with `C-c C-c', or with the next edit (depending
8921 on `org-remove-highlights-with-change'), this variable is emptied
8923 (make-variable-buffer-local 'org-occur-parameters)
8925 (defun org-occur (regexp &optional keep-previous callback)
8926 "Make a compact tree which shows all matches of REGEXP.
8927 The tree will show the lines where the regexp matches, and all higher
8928 headlines above the match. It will also show the heading after the match,
8929 to make sure editing the matching entry is easy.
8930 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
8931 call to `org-occur' will be kept, to allow stacking of calls to this
8933 If CALLBACK is non-nil, it is a function which is called to confirm
8934 that the match should indeed be shown."
8935 (interactive "sRegexp: \nP")
8936 (unless keep-previous
8937 (org-remove-occur-highlights nil nil t))
8938 (push (cons regexp callback) org-occur-parameters)
8941 (goto-char (point-min))
8942 (if (or (not keep-previous) ; do not want to keep
8943 (not org-occur-highlights)) ; no previous matches
8946 (while (re-search-forward regexp nil t)
8947 (when (or (not callback)
8948 (save-match-data (funcall callback)))
8950 (when org-highlight-sparse-tree-matches
8951 (org-highlight-new-match (match-beginning 0) (match-end 0)))
8952 (org-show-context 'occur-tree))))
8953 (when org-remove-highlights-with-change
8954 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
8956 (unless org-sparse-tree-open-archived-trees
8957 (org-hide-archived-subtrees (point-min) (point-max)))
8958 (run-hooks 'org-occur-hook)
8960 (message "%d match(es) for regexp %s" cnt regexp))
8963 (defun org-show-context (&optional key)
8964 "Make sure point and context and visible.
8965 How much context is shown depends upon the variables
8966 `org-show-hierarchy-above', `org-show-following-heading'. and
8967 `org-show-siblings'."
8968 (let ((heading-p (org-on-heading-p t))
8969 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
8970 (following-p (org-get-alist-option org-show-following-heading key))
8971 (entry-p (org-get-alist-option org-show-entry-below key))
8972 (siblings-p (org-get-alist-option org-show-siblings key)))
8974 ;; Show heading or entry text
8975 (if (and heading-p (not entry-p))
8976 (org-flag-heading nil) ; only show the heading
8977 (and (or entry-p (org-invisible-p) (org-invisible-p2))
8978 (org-show-hidden-entry))) ; show entire entry
8980 ;; Show next sibling, or heading below text
8982 (and (if heading-p (org-goto-sibling) (outline-next-heading))
8983 (org-flag-heading nil))))
8984 (when siblings-p (org-show-siblings))
8986 ;; show all higher headings, possibly with siblings
8988 (while (and (condition-case nil
8989 (progn (org-up-heading-all 1) t)
8992 (org-flag-heading nil)
8993 (when siblings-p (org-show-siblings))))))))
8995 (defun org-reveal (&optional siblings)
8996 "Show current entry, hierarchy above it, and the following headline.
8997 This can be used to show a consistent set of context around locations
8998 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
8999 not t for the search context.
9001 With optional argument SIBLINGS, on each level of the hierarchy all
9002 siblings are shown. This repairs the tree structure to what it would
9003 look like when opened with hierarchical calls to `org-cycle'."
9005 (let ((org-show-hierarchy-above t)
9006 (org-show-following-heading t)
9007 (org-show-siblings (if siblings t org-show-siblings)))
9008 (org-show-context nil)))
9010 (defun org-highlight-new-match (beg end)
9011 "Highlight from BEG to END and mark the highlight is an occur headline."
9012 (let ((ov (org-make-overlay beg end)))
9013 (org-overlay-put ov 'face 'secondary-selection)
9014 (push ov org-occur-highlights)))
9016 (defun org-remove-occur-highlights (&optional beg end noremove)
9017 "Remove the occur highlights from the buffer.
9018 BEG and END are ignored. If NOREMOVE is nil, remove this function
9019 from the `before-change-functions' in the current buffer."
9021 (unless org-inhibit-highlight-removal
9022 (mapc 'org-delete-overlay org-occur-highlights)
9023 (setq org-occur-highlights nil)
9024 (setq org-occur-parameters nil)
9026 (remove-hook 'before-change-functions
9027 'org-remove-occur-highlights 'local))))
9031 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
9032 "Regular expression matching the priority indicator.")
9034 (defvar org-remove-priority-next-time nil)
9036 (defun org-priority-up ()
9037 "Increase the priority of the current item."
9041 (defun org-priority-down ()
9042 "Decrease the priority of the current item."
9044 (org-priority 'down))
9046 (defun org-priority (&optional action)
9047 "Change the priority of an item by ARG.
9048 ACTION can be `set', `up', `down', or a character."
9050 (setq action (or action 'set))
9051 (let (current new news have remove)
9053 (org-back-to-heading)
9054 (if (looking-at org-priority-regexp)
9055 (setq current (string-to-char (match-string 2))
9057 (setq current org-default-priority))
9059 ((or (eq action 'set)
9060 (if (featurep 'xemacs) (characterp action) (integerp action)))
9061 (if (not (eq action 'set))
9063 (message "Priority %c-%c, SPC to remove: "
9064 org-highest-priority org-lowest-priority)
9065 (setq new (read-char-exclusive)))
9066 (if (and (= (upcase org-highest-priority) org-highest-priority)
9067 (= (upcase org-lowest-priority) org-lowest-priority))
9068 (setq new (upcase new)))
9069 (cond ((equal new ?\ ) (setq remove t))
9070 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
9071 (error "Priority must be between `%c' and `%c'"
9072 org-highest-priority org-lowest-priority))))
9074 (if (and (not have) (eq last-command this-command))
9075 (setq new org-lowest-priority)
9076 (setq new (if (and org-priority-start-cycle-with-default (not have))
9077 org-default-priority (1- current)))))
9079 (if (and (not have) (eq last-command this-command))
9080 (setq new org-highest-priority)
9081 (setq new (if (and org-priority-start-cycle-with-default (not have))
9082 org-default-priority (1+ current)))))
9083 (t (error "Invalid action")))
9084 (if (or (< (upcase new) org-highest-priority)
9085 (> (upcase new) org-lowest-priority))
9087 (setq news (format "%c" new))
9090 (replace-match "" t t nil 1)
9091 (replace-match news t t nil 2))
9093 (error "No priority cookie found in line")
9094 (looking-at org-todo-line-regexp)
9097 (goto-char (match-end 2))
9098 (insert " [#" news "]"))
9099 (goto-char (match-beginning 3))
9100 (insert "[#" news "] ")))))
9101 (org-preserve-lc (org-set-tags nil 'align))
9103 (message "Priority removed")
9104 (message "Priority of current item set to %s" news))))
9107 (defun org-get-priority (s)
9108 "Find priority cookie and return priority."
9110 (if (not (string-match org-priority-regexp s))
9111 (* 1000 (- org-lowest-priority org-default-priority))
9112 (* 1000 (- org-lowest-priority
9113 (string-to-char (match-string 2 s)))))))
9117 (defvar org-agenda-archives-mode)
9118 (defun org-scan-tags (action matcher &optional todo-only)
9119 "Scan headline tags with inheritance and produce output ACTION.
9121 ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
9122 or `agenda' to produce an entry list for an agenda view. It can also be
9123 a Lisp form or a function that should be called at each matched headline, in
9124 this case the return value is a list of all return values from these calls.
9126 MATCHER is a Lisp form to be evaluated, testing if a given set of tags
9127 qualifies a headline for inclusion. When TODO-ONLY is non-nil,
9128 only lines with a TODO keyword are included in the output."
9129 (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
9130 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
9132 "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
9133 (props (list 'face 'default
9134 'done-face 'org-done
9135 'undone-face 'default
9136 'mouse-face 'highlight
9137 'org-not-done-regexp org-not-done-regexp
9138 'org-todo-regexp org-todo-regexp
9139 'keymap org-agenda-keymap
9141 (format "mouse-2 or RET jump to org file %s"
9142 (abbreviate-file-name
9143 (or (buffer-file-name (buffer-base-buffer))
9144 (buffer-name (buffer-base-buffer)))))))
9145 (case-fold-search nil)
9146 lspos tags tags-list
9147 (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
9148 (llast 0) rtn rtn1 level category i txt
9149 todo marker entry priority)
9150 (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
9151 (setq action (list 'lambda nil action)))
9153 (goto-char (point-min))
9154 (when (eq action 'sparse-tree)
9156 (org-remove-occur-highlights))
9157 (while (re-search-forward re nil t)
9159 (setq todo (if (match-end 1) (match-string 2))
9160 tags (if (match-end 4) (match-string 4)))
9161 (goto-char (setq lspos (1+ (match-beginning 0))))
9162 (setq level (org-reduced-level (funcall outline-level))
9163 category (org-get-category))
9164 (setq i llast llast level)
9165 ;; remove tag lists from same and sublevels
9167 (when (setq entry (assoc i tags-alist))
9168 (setq tags-alist (delete entry tags-alist)))
9170 ;; add the next tags
9172 (setq tags (mapcar 'downcase (org-split-string tags ":"))
9174 (cons (cons level tags) tags-alist)))
9175 ;; compile tags for current headline
9177 (if org-use-tag-inheritance
9178 (apply 'append (mapcar 'cdr (reverse tags-alist)))
9180 (when org-use-tag-inheritance
9181 (setcdr (car tags-alist)
9183 (setq x (copy-sequence x))
9184 (org-add-prop-inherited x))
9185 (cdar tags-alist))))
9186 (when (and tags org-use-tag-inheritance
9187 (not (eq t org-use-tag-inheritance)))
9188 ;; selective inheritance, remove uninherited ones
9189 (setcdr (car tags-alist)
9190 (org-remove-uniherited-tags (cdar tags-alist))))
9191 (when (and (or (not todo-only) (member todo org-not-done-keywords))
9192 (let ((case-fold-search t)) (eval matcher))
9194 (not (member org-archive-tag tags-list))
9195 ;; we have an archive tag, should we use this anyway?
9196 (or (not org-agenda-skip-archived-trees)
9197 (and (eq action 'agenda) org-agenda-archives-mode))))
9198 (unless (eq action 'sparse-tree) (org-agenda-skip))
9200 ;; select this headline
9203 ((eq action 'sparse-tree)
9204 (and org-highlight-sparse-tree-matches
9205 (org-get-heading) (match-end 0)
9206 (org-highlight-new-match
9207 (match-beginning 0) (match-beginning 1)))
9208 (org-show-context 'tags-tree))
9209 ((eq action 'agenda)
9210 (setq txt (org-format-agenda-item
9213 (if org-tags-match-list-sublevels
9214 (make-string (1- level) ?.) "")
9216 category (org-get-tags-at))
9217 priority (org-get-priority txt))
9219 (setq marker (org-agenda-new-marker))
9220 (org-add-props txt props
9221 'org-marker marker 'org-hd-marker marker 'org-category category
9222 'priority priority 'type "tagsmatch")
9226 (setq rtn1 (funcall action))
9228 (goto-char (point-at-eol)))
9229 (t (error "Invalid action")))
9231 ;; if we are to skip sublevels, jump to end of subtree
9232 (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
9233 (when (and (eq action 'sparse-tree)
9234 (not org-sparse-tree-open-archived-trees))
9235 (org-hide-archived-subtrees (point-min) (point-max)))
9238 (defun org-remove-uniherited-tags (tags)
9239 "Remove all tags that are not inherited from the list TAGS."
9241 ((eq org-use-tag-inheritance t)
9242 (if org-tags-exclude-from-inheritance
9243 (org-delete-all org-tags-exclude-from-inheritance tags)
9245 ((not org-use-tag-inheritance) nil)
9246 ((stringp org-use-tag-inheritance)
9249 (if (and (string-match org-use-tag-inheritance x)
9250 (not (member x org-tags-exclude-from-inheritance)))
9253 ((listp org-use-tag-inheritance)
9256 (if (member x org-use-tag-inheritance) x nil))
9259 (defvar todo-only) ;; dynamically scoped
9261 (defun org-tags-sparse-tree (&optional todo-only match)
9262 "Create a sparse tree according to tags string MATCH.
9263 MATCH can contain positive and negative selection of tags, like
9264 \"+WORK+URGENT-WITHBOSS\".
9265 If optional argument TODO-ONLY is non-nil, only select lines that are
9268 (org-prepare-agenda-buffers (list (current-buffer)))
9269 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
9271 (defvar org-cached-props nil)
9272 (defun org-cached-entry-get (pom property)
9273 (if (or (eq t org-use-property-inheritance)
9274 (and (stringp org-use-property-inheritance)
9275 (string-match org-use-property-inheritance property))
9276 (and (listp org-use-property-inheritance)
9277 (member property org-use-property-inheritance)))
9278 ;; Caching is not possible, check it directly
9279 (org-entry-get pom property 'inherit)
9280 ;; Get all properties, so that we can do complicated checks easily
9281 (cdr (assoc property (or org-cached-props
9282 (setq org-cached-props
9283 (org-entry-properties pom)))))))
9285 (defun org-global-tags-completion-table (&optional files)
9286 "Return the list of all tags in all agenda buffer/files."
9293 (set-buffer (find-file-noselect file))
9294 (append (org-get-buffer-tags)
9295 (mapcar (lambda (x) (if (stringp (car-safe x))
9296 (list (car-safe x)) nil))
9298 (if (and files (car files))
9300 (org-agenda-files))))))))
9302 (defun org-make-tags-matcher (match)
9303 "Create the TAGS//TODO matcher form for the selection string MATCH."
9304 ;; todo-only is scoped dynamically into this function, and the function
9305 ;; may change it if the matcher asks for it.
9307 ;; Get a new match request, with completion
9308 (let ((org-last-tags-completion-table
9309 (org-global-tags-completion-table)))
9310 (setq match (org-ido-completing-read
9311 "Match: " 'org-tags-completion-function nil nil nil
9312 'org-tags-history))))
9314 ;; Parse the string and create a lisp form
9315 (let ((match0 match)
9316 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
9318 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
9319 orterms term orlist re-p str-p level-p level-op time-p
9320 prop-p pn pv po cat-p gv rest)
9321 (if (string-match "/+" match)
9322 ;; match contains also a todo-matching request
9324 (setq tagsmatch (substring match 0 (match-beginning 0))
9325 todomatch (substring match (match-end 0)))
9326 (if (string-match "^!" todomatch)
9327 (setq todo-only t todomatch (substring todomatch 1)))
9328 (if (string-match "^\\s-*$" todomatch)
9329 (setq todomatch nil)))
9330 ;; only matching tags
9331 (setq tagsmatch match todomatch nil))
9333 ;; Make the tags matcher
9334 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
9335 (setq tagsmatcher t)
9336 (setq orterms (org-split-string tagsmatch "|") orlist nil)
9337 (while (setq term (pop orterms))
9338 (while (and (equal (substring term -1) "\\") orterms)
9339 (setq term (concat term "|" (pop orterms)))) ; repair bad split
9340 (while (string-match re term)
9341 (setq rest (substring term (match-end 0))
9342 minus (and (match-end 1)
9343 (equal (match-string 1 term) "-"))
9344 tag (match-string 2 term)
9345 re-p (equal (string-to-char tag) ?{)
9346 level-p (match-end 4)
9347 prop-p (match-end 5)
9349 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
9351 (setq level-op (org-op-to-function (match-string 3 term)))
9352 `(,level-op level ,(string-to-number
9353 (match-string 4 term))))
9355 (setq pn (match-string 5 term)
9356 po (match-string 6 term)
9357 pv (match-string 7 term)
9358 cat-p (equal pn "CATEGORY")
9359 re-p (equal (string-to-char pv) ?{)
9360 str-p (equal (string-to-char pv) ?\")
9361 time-p (save-match-data
9362 (string-match "^\"[[<].*[]>]\"$" pv))
9363 pv (if (or re-p str-p) (substring pv 1 -1) pv))
9364 (if time-p (setq pv (org-matcher-time pv)))
9365 (setq po (org-op-to-function po (if time-p 'time str-p)))
9367 ((equal pn "CATEGORY")
9368 (setq gv '(get-text-property (point) 'org-category)))
9372 (setq gv `(org-cached-entry-get nil ,pn))))
9375 `(not (string-match ,pv (or ,gv "")))
9376 `(string-match ,pv (or ,gv "")))
9378 `(,po (or ,gv "") ,pv)
9379 `(,po (string-to-number (or ,gv ""))
9380 ,(string-to-number pv) ))))
9381 (t `(member ,(downcase tag) tags-list)))
9382 mm (if minus (list 'not mm) mm)
9384 (push mm tagsmatcher))
9385 (push (if (> (length tagsmatcher) 1)
9386 (cons 'and tagsmatcher)
9389 (setq tagsmatcher nil))
9390 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
9392 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
9393 ;; Make the todo matcher
9394 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
9395 (setq todomatcher t)
9396 (setq orterms (org-split-string todomatch "|") orlist nil)
9397 (while (setq term (pop orterms))
9398 (while (string-match re term)
9399 (setq minus (and (match-end 1)
9400 (equal (match-string 1 term) "-"))
9401 kwd (match-string 2 term)
9402 re-p (equal (string-to-char kwd) ?{)
9403 term (substring term (match-end 0))
9405 `(string-match ,(substring kwd 1 -1) todo)
9406 (list 'equal 'todo kwd))
9407 mm (if minus (list 'not mm) mm))
9408 (push mm todomatcher))
9409 (push (if (> (length todomatcher) 1)
9410 (cons 'and todomatcher)
9413 (setq todomatcher nil))
9414 (setq todomatcher (if (> (length orlist) 1)
9415 (cons 'or orlist) (car orlist))))
9417 ;; Return the string and lisp forms of the matcher
9418 (setq matcher (if todomatcher
9419 (list 'and tagsmatcher todomatcher)
9421 (cons match0 matcher)))
9423 (defun org-op-to-function (op &optional stringp)
9424 "Turn an operator into the appropriate function."
9427 ((equal op "<" ) '(< string< org-time<))
9428 ((equal op ">" ) '(> org-string> org-time>))
9429 ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
9430 ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
9431 ((member op '("=" "==")) '(= string= org-time=))
9432 ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
9433 (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
9435 (defun org<> (a b) (not (= a b)))
9436 (defun org-string<= (a b) (or (string= a b) (string< a b)))
9437 (defun org-string>= (a b) (not (string< a b)))
9438 (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
9439 (defun org-string<> (a b) (not (string= a b)))
9440 (defun org-time= (a b) (= (org-2ft a) (org-2ft b)))
9441 (defun org-time< (a b) (< (org-2ft a) (org-2ft b)))
9442 (defun org-time<= (a b) (<= (org-2ft a) (org-2ft b)))
9443 (defun org-time> (a b) (> (org-2ft a) (org-2ft b)))
9444 (defun org-time>= (a b) (>= (org-2ft a) (org-2ft b)))
9445 (defun org-time<> (a b) (org<> (org-2ft a) (org-2ft b)))
9447 "Convert S to a floating point time.
9448 If S is already a number, just return it. If it is a string, parse
9449 it as a time string and apply `float-time' to it. f S is nil, just return 0."
9454 (float-time (apply 'encode-time (org-parse-time-string s)))
9458 (defun org-time-today ()
9459 "Time in seconds today at 0:00.
9460 Returns the float number of seconds since the beginning of the
9461 epoch to the beginning of today (00:00)."
9462 (float-time (apply 'encode-time
9463 (append '(0 0 0) (nthcdr 3 (decode-time))))))
9465 (defun org-matcher-time (s)
9466 "Interpret a time comparison value."
9469 ((string= s "<now>") (float-time))
9470 ((string= s "<today>") (org-time-today))
9471 ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
9472 ((string= s "<yesterday>") (- (org-time-today) 86400.0))
9473 ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
9475 (* (string-to-number (match-string 1 s))
9476 (cdr (assoc (match-string 2 s)
9477 '(("d" . 86400.0) ("w" . 604800.0)
9478 ("m" . 2678400.0) ("y" . 31557600.0)))))))
9481 (defun org-match-any-p (re list)
9482 "Does re match any element of list?"
9483 (setq list (mapcar (lambda (x) (string-match re x)) list))
9486 (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
9487 (defvar org-tags-overlay (org-make-overlay 1 1))
9488 (org-detach-overlay org-tags-overlay)
9490 (defun org-get-local-tags-at (&optional pos)
9491 "Get a list of tags defined in the current headline."
9492 (org-get-tags-at pos 'local))
9494 (defun org-get-local-tags ()
9495 "Get a list of tags defined in the current headline."
9496 (org-get-tags-at nil 'local))
9498 (defun org-get-tags-at (&optional pos local)
9499 "Get a list of all headline tags applicable at POS.
9500 POS defaults to point. If tags are inherited, the list contains
9501 the targets in the same sequence as the headlines appear, i.e.
9502 the tags of the current headline come last.
9503 When LOCAL is non-nil, only return tags from the current headline,
9504 ignore inherited ones."
9506 (let (tags ltags lastpos parent)
9510 (goto-char (or pos (point)))
9515 (org-back-to-heading t)
9516 (while (not (equal lastpos (point)))
9517 (setq lastpos (point))
9518 (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
9519 (setq ltags (org-split-string
9520 (org-match-string-no-properties 1) ":"))
9522 (setq ltags (mapcar 'org-add-prop-inherited ltags)))
9525 (org-remove-uniherited-tags ltags)
9528 (or org-use-tag-inheritance (throw 'done t))
9529 (if local (throw 'done t))
9530 (org-up-heading-all 1)
9533 (append (org-remove-uniherited-tags org-file-tags) tags))))
9535 (defun org-add-prop-inherited (s)
9536 (add-text-properties 0 (length s) '(inherited t) s)
9539 (defun org-toggle-tag (tag &optional onoff)
9540 "Toggle the tag TAG for the current line.
9541 If ONOFF is `on' or `off', don't toggle but set to this state."
9544 (org-back-to-heading t)
9545 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
9548 (setq current (match-string 1))
9551 (setq current (nreverse (org-split-string current ":")))
9555 (or (member tag current) (push tag current)))
9557 (or (not (member tag current)) (setq current (delete tag current))))
9558 (t (if (member tag current)
9559 (setq current (delete tag current))
9561 (push tag current))))
9565 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
9566 (org-set-tags nil t))
9567 (delete-horizontal-space))
9568 (run-hooks 'org-after-tags-change-hook))
9571 (defun org-align-tags-here (to-col)
9572 ;; Assumes that this is a headline
9573 (let ((pos (point)) (col (current-column)) ncol tags-l p)
9574 (beginning-of-line 1)
9575 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
9576 (< pos (match-beginning 2)))
9578 (setq tags-l (- (match-end 2) (match-beginning 2)))
9579 (goto-char (match-beginning 1))
9581 (delete-region (point) (1+ (match-beginning 2)))
9582 (setq ncol (max (1+ (current-column))
9586 (- (abs to-col) tags-l))))
9588 (insert (make-string (- ncol (current-column)) ?\ ))
9589 (setq ncol (current-column))
9590 (when indent-tabs-mode (tabify p (point-at-eol)))
9591 (org-move-to-column (min ncol col) t))
9594 (defun org-set-tags-command (&optional arg just-align)
9595 "Call the set-tags command for the current entry."
9597 (if (org-on-heading-p)
9598 (org-set-tags arg just-align)
9600 (org-back-to-heading t)
9601 (org-set-tags arg just-align))))
9603 (defun org-set-tags (&optional arg just-align)
9604 "Set the tags for the current headline.
9605 With prefix ARG, realign all tags in headings in the current buffer."
9607 (let* ((re (concat "^" outline-regexp))
9608 (current (org-get-tags-string))
9609 (col (current-column))
9610 (org-setting-tags t)
9611 table current-tags inherited-tags ; computed below when needed
9615 (goto-char (point-min))
9616 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
9617 (while (re-search-forward re nil t)
9618 (org-set-tags nil t)
9620 (message "All tags realigned to column %d" org-tags-column))
9623 ;; Get a new set of tags from the user
9625 (setq table (or org-tag-alist (org-get-buffer-tags))
9626 org-last-tags-completion-table table
9627 current-tags (org-split-string current ":")
9628 inherited-tags (nreverse
9629 (nthcdr (length current-tags)
9630 (nreverse (org-get-tags-at))))
9632 (if (or (eq t org-use-fast-tag-selection)
9633 (and org-use-fast-tag-selection
9634 (delq nil (mapcar 'cdr table))))
9635 (org-fast-tag-selection
9636 current-tags inherited-tags table
9637 (if org-fast-tag-selection-include-todo org-todo-key-alist))
9638 (let ((org-add-colon-after-tag-completion t))
9640 (org-without-partial-completion
9641 (org-ido-completing-read "Tags: " 'org-tags-completion-function
9642 nil nil current 'org-tags-history)))))))
9643 (while (string-match "[-+&]+" tags)
9644 ;; No boolean logic, just a list
9645 (setq tags (replace-match ":" t t tags))))
9647 (if (string-match "\\`[\t ]*\\'" tags)
9649 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
9650 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
9652 ;; Insert new tags at the correct column
9653 (beginning-of-line 1)
9655 ((and (equal current "") (equal tags "")))
9657 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
9661 (goto-char (match-beginning 0))
9662 (setq c0 (current-column) p0 (point)
9663 c1 (max (1+ c0) (if (> org-tags-column 0)
9665 (- (- org-tags-column) (length tags))))
9666 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
9667 (replace-match rpl t t)
9668 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
9670 (t (error "Tags alignment failed")))
9671 (org-move-to-column col)
9673 (run-hooks 'org-after-tags-change-hook)))))
9675 (defun org-change-tag-in-region (beg end tag off)
9676 "Add or remove TAG for each entry in the region.
9677 This works in the agenda, and also in an org-mode buffer."
9679 (list (region-beginning) (region-end)
9680 (let ((org-last-tags-completion-table
9682 (org-get-buffer-tags)
9683 (org-global-tags-completion-table))))
9684 (org-ido-completing-read
9685 "Tag: " 'org-tags-completion-function nil nil nil
9688 (message "[s]et or [r]emove? ")
9689 (equal (read-char-exclusive) ?r))))
9690 (if (fboundp 'deactivate-mark) (deactivate-mark))
9691 (let ((agendap (equal major-mode 'org-agenda-mode))
9692 l1 l2 m buf pos newhead (cnt 0))
9694 (setq l2 (1- (org-current-line)))
9696 (setq l1 (org-current-line))
9697 (loop for l from l1 to l2 do
9699 (setq m (get-text-property (point) 'org-hd-marker))
9700 (when (or (and (org-mode-p) (org-on-heading-p))
9702 (setq buf (if agendap (marker-buffer m) (current-buffer))
9703 pos (if agendap m (point)))
9704 (with-current-buffer buf
9709 (org-toggle-tag tag (if off 'off 'on))
9710 (setq newhead (org-get-heading)))))
9711 (and agendap (org-agenda-change-all-lines newhead m))))
9712 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
9714 (defun org-tags-completion-function (string predicate &optional flag)
9715 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
9716 (confirm (lambda (x) (stringp (car x)))))
9717 (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
9718 (setq s1 (match-string 1 string)
9719 s2 (match-string 2 string))
9720 (setq s1 "" s2 string))
9724 (setq rtn (try-completion s2 ctable confirm))
9727 (concat s1 s2 (substring rtn (length s2))
9728 (if (and org-add-colon-after-tag-completion
9734 (all-completions s2 ctable confirm)
9741 (defun org-fast-tag-insert (kwd tags face &optional end)
9742 "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
9743 (insert (format "%-12s" (concat kwd ":"))
9744 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
9747 (defun org-fast-tag-show-exit (flag)
9750 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
9754 (org-move-to-column (- (window-width) 19) t)
9755 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
9757 (defun org-set-current-tags-overlay (current prefix)
9758 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
9759 (if (featurep 'xemacs)
9760 (org-overlay-display org-tags-overlay (concat prefix s)
9761 'secondary-selection)
9762 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
9763 (org-overlay-display org-tags-overlay (concat prefix s)))))
9765 (defun org-fast-tag-selection (current inherited table &optional todo-table)
9766 "Fast tag selection with single keys.
9767 CURRENT is the current list of tags in the headline, INHERITED is the
9768 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
9769 possibly with grouping information. TODO-TABLE is a similar table with
9770 TODO keywords, should these have keys assigned to them.
9771 If the keys are nil, a-z are automatically assigned.
9772 Returns the new tags string, or nil to not change the current settings."
9773 (let* ((fulltable (append table todo-table))
9774 (maxlen (apply 'max (mapcar
9776 (if (stringp (car x)) (string-width (car x)) 0))
9778 (buf (current-buffer))
9779 (expert (eq org-fast-tag-selection-single-key 'expert))
9781 (fwidth (+ maxlen 3 1 3))
9782 (ncol (/ (- (window-width) 4) fwidth))
9785 tg cnt e c char c1 c2 ntable tbl rtn
9786 ov-start ov-end ov-prefix
9787 (exit-after-next org-fast-tag-selection-single-key)
9788 (done-keywords org-done-keywords)
9791 (beginning-of-line 1)
9793 (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
9794 (setq ov-start (match-beginning 1)
9795 ov-end (match-end 1)
9797 (setq ov-start (1- (point-at-eol))
9798 ov-end (1+ ov-start))
9799 (skip-chars-forward "^\n\r")
9802 (buffer-substring (1- (point)) (point))
9803 (if (> (current-column) org-tags-column)
9805 (make-string (- org-tags-column (current-column)) ?\ ))))))
9806 (org-move-overlay org-tags-overlay ov-start ov-end)
9807 (save-window-excursion
9809 (set-buffer (get-buffer-create " *Org tags*"))
9810 (delete-other-windows)
9811 (split-window-vertically)
9812 (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
9814 (org-set-local 'org-done-keywords done-keywords)
9815 (org-fast-tag-insert "Inherited" inherited i-face "\n")
9816 (org-fast-tag-insert "Current" current c-face "\n\n")
9817 (org-fast-tag-show-exit exit-after-next)
9818 (org-set-current-tags-overlay current ov-prefix)
9819 (setq tbl fulltable char ?a cnt 0)
9820 (while (setq e (pop tbl))
9822 ((equal e '(:startgroup))
9823 (push '() groups) (setq ingroup t)
9824 (when (not (= cnt 0))
9828 ((equal e '(:endgroup))
9829 (setq ingroup nil cnt 0)
9832 (setq tg (car e) c2 nil)
9835 ;; automatically assign a character.
9836 (setq c1 (string-to-char
9837 (downcase (substring
9838 tg (if (= (string-to-char tg) ?@) 1 0)))))
9839 (if (or (rassoc c1 ntable) (rassoc c1 table))
9840 (while (or (rassoc char ntable) (rassoc char table))
9841 (setq char (1+ char)))
9843 (setq c (or c2 char)))
9844 (if ingroup (push tg (car groups)))
9845 (setq tg (org-add-props tg nil 'face
9847 ((not (assoc tg table))
9848 (org-get-todo-face tg))
9849 ((member tg current) c-face)
9850 ((member tg inherited) i-face)
9852 (if (and (= cnt 0) (not ingroup)) (insert " "))
9853 (insert "[" c "] " tg (make-string
9854 (- fwidth 4 (length tg)) ?\ ))
9855 (push (cons tg c) ntable)
9856 (when (= (setq cnt (1+ cnt)) ncol)
9858 (if ingroup (insert " "))
9860 (setq ntable (nreverse ntable))
9862 (goto-char (point-min))
9863 (if (not expert) (org-fit-window-to-buffer))
9867 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
9868 (if groups " [!] no groups" " [!]groups")
9869 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
9870 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
9872 ((= c ?\r) (throw 'exit t))
9874 (setq groups (not groups))
9875 (goto-char (point-min))
9876 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
9879 (org-fast-tag-show-exit
9880 (setq exit-after-next (not exit-after-next)))
9882 (delete-other-windows)
9883 (split-window-vertically)
9884 (org-switch-to-buffer-other-window " *Org tags*")
9885 (org-fit-window-to-buffer)))
9887 (and (= c ?q) (not (rassoc c ntable))))
9888 (org-detach-overlay org-tags-overlay)
9892 (if exit-after-next (setq exit-after-next 'now)))
9895 (setq tg (org-ido-completing-read
9898 (with-current-buffer buf
9899 (org-get-buffer-tags)))))
9900 (quit (setq tg "")))
9901 (when (string-match "\\S-" tg)
9902 (add-to-list 'buffer-tags (list tg))
9903 (if (member tg current)
9904 (setq current (delete tg current))
9906 (if exit-after-next (setq exit-after-next 'now)))
9907 ((setq e (rassoc c todo-table) tg (car e))
9908 (with-current-buffer buf
9909 (save-excursion (org-todo tg)))
9910 (if exit-after-next (setq exit-after-next 'now)))
9911 ((setq e (rassoc c ntable) tg (car e))
9912 (if (member tg current)
9913 (setq current (delete tg current))
9914 (loop for g in groups do
9917 (setq current (delete x current)))
9920 (if exit-after-next (setq exit-after-next 'now))))
9922 ;; Create a sorted list
9926 (assoc b (cdr (memq (assoc a ntable) ntable))))))
9927 (if (eq exit-after-next 'now) (throw 'exit t))
9928 (goto-char (point-min))
9929 (beginning-of-line 2)
9930 (delete-region (point) (point-at-eol))
9931 (org-fast-tag-insert "Current" current c-face)
9932 (org-set-current-tags-overlay current ov-prefix)
9933 (while (re-search-forward
9934 (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
9935 (setq tg (match-string 1))
9936 (add-text-properties
9937 (match-beginning 1) (match-end 1)
9940 ((member tg current) c-face)
9941 ((member tg inherited) i-face)
9942 (t (get-text-property (match-beginning 1) 'face))))))
9943 (goto-char (point-min)))))
9944 (org-detach-overlay org-tags-overlay)
9946 (mapconcat 'identity current ":")
9949 (defun org-get-tags-string ()
9950 "Get the TAGS string in the current headline."
9951 (unless (org-on-heading-p t)
9952 (error "Not on a heading"))
9954 (beginning-of-line 1)
9955 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
9956 (org-match-string-no-properties 1)
9959 (defun org-get-tags ()
9960 "Get the list of tags specified in the current headline."
9961 (org-split-string (org-get-tags-string) ":"))
9963 (defun org-get-buffer-tags ()
9964 "Get a table of all tags used in the buffer, for completion."
9967 (goto-char (point-min))
9968 (while (re-search-forward
9969 (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
9970 (when (equal (char-after (point-at-bol 0)) ?*)
9971 (mapc (lambda (x) (add-to-list 'tags x))
9972 (org-split-string (org-match-string-no-properties 1) ":")))))
9973 (mapcar 'list tags)))
9975 ;;;; The mapping API
9978 (defun org-map-entries (func &optional match scope &rest skip)
9979 "Call FUNC at each headline selected by MATCH in SCOPE.
9981 FUNC is a function or a lisp form. The function will be called without
9982 arguments, with the cursor positioned at the beginning of the headline.
9983 The return values of all calls to the function will be collected and
9986 MATCH is a tags/property/todo match as it is used in the agenda tags view.
9987 Only headlines that are matched by this query will be considered during
9988 the iteration. When MATCH is nil or t, all headlines will be
9989 visited by the iteration.
9991 SCOPE determines the scope of this command. It can be any of:
9993 nil The current buffer, respecting the restriction if any
9994 tree The subtree started with the entry at point
9995 file The current buffer, without restriction
9997 The current buffer, and any archives associated with it
9998 agenda All agenda files
9999 agenda-with-archives
10000 All agenda files with any archive files associated with them
10002 If this is a list, all files in the list will be scanned
10004 The remaining args are treated as settings for the skipping facilities of
10005 the scanner. The following items can be given here:
10007 archive skip trees with the archive tag.
10008 comment skip trees with the COMMENT keyword
10009 function or Emacs Lisp form:
10010 will be used as value for `org-agenda-skip-function', so whenever
10011 the the function returns t, FUNC will not be called for that
10012 entry and search will continue from the point where the
10013 function leaves it."
10014 (let* ((org-agenda-archives-mode nil) ; just to make sure
10015 (org-agenda-skip-archived-trees (memq 'archive skip))
10016 (org-agenda-skip-comment-trees (memq 'comment skip))
10017 (org-agenda-skip-function
10018 (car (org-delete-all '(comment archive) skip)))
10019 (org-tags-match-list-sublevels t)
10020 matcher pos file res
10021 org-todo-keywords-for-agenda
10022 org-done-keywords-for-agenda
10023 org-todo-keyword-alist-for-agenda
10024 org-tag-alist-for-agenda)
10027 ((eq match t) (setq matcher t))
10028 ((eq match nil) (setq matcher t))
10029 (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
10031 (when (eq scope 'tree)
10032 (org-back-to-heading t)
10033 (org-narrow-to-subtree)
10038 (org-prepare-agenda-buffers
10039 (list (buffer-file-name (current-buffer))))
10040 (org-scan-tags func matcher))
10041 ;; Get the right scope
10044 ((and scope (listp scope) (symbolp (car scope)))
10045 (setq scope (eval scope)))
10046 ((eq scope 'agenda)
10047 (setq scope (org-agenda-files t)))
10048 ((eq scope 'agenda-with-archives)
10049 (setq scope (org-agenda-files t))
10050 (setq scope (org-add-archive-files scope)))
10052 (setq scope (list (buffer-file-name))))
10053 ((eq scope 'file-with-archives)
10054 (setq scope (org-add-archive-files (list (buffer-file-name))))))
10055 (org-prepare-agenda-buffers scope)
10056 (while (setq file (pop scope))
10057 (with-current-buffer (org-find-base-buffer-visiting file)
10061 (goto-char (point-min))
10062 (setq res (append res (org-scan-tags func matcher)))))))
10067 ;;; Setting and retrieving properties
10069 (defconst org-special-properties
10070 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
10071 "TIMESTAMP" "TIMESTAMP_IA")
10072 "The special properties valid in Org-mode.
10074 These are properties that are not defined in the property drawer,
10075 but in some other way.")
10077 (defconst org-default-properties
10078 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
10079 "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
10080 "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
10081 "EXPORT_FILE_NAME" "EXPORT_TITLE")
10082 "Some properties that are used by Org-mode for various purposes.
10083 Being in this list makes sure that they are offered for completion.")
10085 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
10086 "Regular expression matching the first line of a property drawer.")
10088 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
10089 "Regular expression matching the first line of a property drawer.")
10091 (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
10092 "Regular expression matching the first line of a property drawer.")
10094 (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
10095 "Regular expression matching the first line of a property drawer.")
10097 (defconst org-property-drawer-re
10098 (concat "\\(" org-property-start-re "\\)[^\000]*\\("
10099 org-property-end-re "\\)\n?")
10100 "Matches an entire property drawer.")
10102 (defconst org-clock-drawer-re
10103 (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
10104 org-property-end-re "\\)\n?")
10105 "Matches an entire clock drawer.")
10107 (defun org-property-action ()
10108 "Do an action on properties."
10111 (org-at-property-p)
10112 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
10113 (setq c (read-char-exclusive))
10116 (call-interactively 'org-set-property))
10118 (call-interactively 'org-delete-property))
10120 (call-interactively 'org-delete-property-globally))
10122 (call-interactively 'org-compute-property-at-point))
10123 (t (error "No such property action %c" c)))))
10125 (defun org-at-property-p ()
10126 "Is the cursor in a property line?"
10127 ;; FIXME: Does not check if we are actually in the drawer.
10128 ;; FIXME: also returns true on any drawers.....
10129 ;; This is used by C-c C-c for property action.
10131 (beginning-of-line 1)
10132 (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
10134 (defun org-get-property-block (&optional beg end force)
10135 "Return the (beg . end) range of the body of the property drawer.
10136 BEG and END can be beginning and end of subtree, if not given
10137 they will be found.
10138 If the drawer does not exist and FORCE is non-nil, create the drawer."
10141 (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
10142 (end (or end (progn (outline-next-heading) (point)))))
10144 (if (re-search-forward org-property-start-re end t)
10145 (setq beg (1+ (match-end 0)))
10148 (org-insert-property-drawer)
10149 (setq end (progn (outline-next-heading) (point))))
10152 (if (re-search-forward org-property-start-re end t)
10153 (setq beg (1+ (match-end 0)))))
10154 (if (re-search-forward org-property-end-re end t)
10155 (setq end (match-beginning 0))
10156 (or force (throw 'exit nil))
10159 (org-indent-line-function)
10160 (insert ":END:\n"))
10163 (defun org-entry-properties (&optional pom which)
10164 "Get all properties of the entry at point-or-marker POM.
10165 This includes the TODO keyword, the tags, time strings for deadline,
10166 scheduled, and clocking, and any additional properties defined in the
10167 entry. The return value is an alist, keys may occur multiple times
10168 if the property key was used several times.
10169 POM may also be nil, in which case the current entry is used.
10170 If WHICH is nil or `all', get all properties. If WHICH is
10171 `special' or `standard', only get that subclass."
10172 (setq which (or which 'all))
10173 (org-with-point-at pom
10174 (let ((clockstr (substring org-clock-string 0 -1))
10175 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
10176 beg end range props sum-props key value string clocksum)
10178 (when (condition-case nil (org-back-to-heading t) (error nil))
10180 (setq sum-props (get-text-property (point) 'org-summaries))
10181 (setq clocksum (get-text-property (point) :org-clock-minutes))
10182 (outline-next-heading)
10184 (when (memq which '(all special))
10185 ;; Get the special properties, like TODO and tags
10187 (when (and (looking-at org-todo-line-regexp) (match-end 2))
10188 (push (cons "TODO" (org-match-string-no-properties 2)) props))
10189 (when (looking-at org-priority-regexp)
10190 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
10191 (when (and (setq value (org-get-tags-string))
10192 (string-match "\\S-" value))
10193 (push (cons "TAGS" value) props))
10194 (when (setq value (org-get-tags-at))
10195 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
10197 (while (re-search-forward org-maybe-keyword-time-regexp end t)
10198 (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
10199 string (if (equal key clockstr)
10203 (match-beginning 3) (goto-char (point-at-eol)))))
10204 (substring (org-match-string-no-properties 3) 1 -1)))
10206 (if (= (char-after (match-beginning 3)) ?\[)
10207 (setq key "TIMESTAMP_IA")
10208 (setq key "TIMESTAMP")))
10209 (when (or (equal key clockstr) (not (assoc key props)))
10210 (push (cons key string) props)))
10214 (when (memq which '(all standard))
10215 ;; Get the standard properties, like :PORP: ...
10216 (setq range (org-get-property-block beg end))
10218 (goto-char (car range))
10219 (while (re-search-forward
10220 (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
10222 (setq key (org-match-string-no-properties 1)
10223 value (org-trim (or (org-match-string-no-properties 2) "")))
10224 (unless (member key excluded)
10225 (push (cons key (or value "")) props)))))
10227 (push (cons "CLOCKSUM"
10228 (org-columns-number-to-string (/ (float clocksum) 60.)
10231 (unless (assoc "CATEGORY" props)
10232 (setq value (or (org-get-category)
10233 (progn (org-refresh-category-properties)
10234 (org-get-category))))
10235 (push (cons "CATEGORY" value) props))
10236 (append sum-props (nreverse props)))))))
10238 (defun org-entry-get (pom property &optional inherit)
10239 "Get value of PROPERTY for entry at point-or-marker POM.
10240 If INHERIT is non-nil and the entry does not have the property,
10241 then also check higher levels of the hierarchy.
10242 If INHERIT is the symbol `selective', use inheritance only if the setting
10243 in `org-use-property-inheritance' selects PROPERTY for inheritance.
10244 If the property is present but empty, the return value is the empty string.
10245 If the property is not present at all, nil is returned."
10246 (org-with-point-at pom
10247 (if (and inherit (if (eq inherit 'selective)
10248 (org-property-inherit-p property)
10250 (org-entry-get-with-inheritance property)
10251 (if (member property org-special-properties)
10252 ;; We need a special property. Use brute force, get all properties.
10253 (cdr (assoc property (org-entry-properties nil 'special)))
10254 (let ((range (org-get-property-block)))
10256 (goto-char (car range))
10258 (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
10260 ;; Found the property, return it.
10262 (org-match-string-no-properties 1)
10265 (defun org-property-or-variable-value (var &optional inherit)
10266 "Check if there is a property fixing the value of VAR.
10267 If yes, return this value. If not, return the current value of the variable."
10268 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
10269 (if (and prop (stringp prop) (string-match "\\S-" prop))
10271 (symbol-value var))))
10273 (defun org-entry-delete (pom property)
10274 "Delete the property PROPERTY from entry at point-or-marker POM."
10275 (org-with-point-at pom
10276 (if (member property org-special-properties)
10277 nil ; cannot delete these properties.
10278 (let ((range (org-get-property-block)))
10280 (goto-char (car range))
10282 (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
10285 (delete-region (match-beginning 0) (1+ (point-at-eol)))
10289 ;; Multi-values properties are properties that contain multiple values
10290 ;; These values are assumed to be single words, separated by whitespace.
10291 (defun org-entry-add-to-multivalued-property (pom property value)
10292 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
10293 (let* ((old (org-entry-get pom property))
10294 (values (and old (org-split-string old "[ \t]"))))
10295 (setq value (org-entry-protect-space value))
10296 (unless (member value values)
10297 (setq values (cons value values))
10298 (org-entry-put pom property
10299 (mapconcat 'identity values " ")))))
10301 (defun org-entry-remove-from-multivalued-property (pom property value)
10302 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
10303 (let* ((old (org-entry-get pom property))
10304 (values (and old (org-split-string old "[ \t]"))))
10305 (setq value (org-entry-protect-space value))
10306 (when (member value values)
10307 (setq values (delete value values))
10308 (org-entry-put pom property
10309 (mapconcat 'identity values " ")))))
10311 (defun org-entry-member-in-multivalued-property (pom property value)
10312 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
10313 (let* ((old (org-entry-get pom property))
10314 (values (and old (org-split-string old "[ \t]"))))
10315 (setq value (org-entry-protect-space value))
10316 (member value values)))
10318 (defun org-entry-get-multivalued-property (pom property)
10319 "Return a list of values in a multivalued property."
10320 (let* ((value (org-entry-get pom property))
10321 (values (and value (org-split-string value "[ \t]"))))
10322 (mapcar 'org-entry-restore-space values)))
10324 (defun org-entry-put-multivalued-property (pom property &rest values)
10325 "Set multivalued PROPERTY at point-or-marker POM to VALUES.
10326 VALUES should be a list of strings. Spaces will be protected."
10327 (org-entry-put pom property
10328 (mapconcat 'org-entry-protect-space values " "))
10329 (let* ((value (org-entry-get pom property))
10330 (values (and value (org-split-string value "[ \t]"))))
10331 (mapcar 'org-entry-restore-space values)))
10333 (defun org-entry-protect-space (s)
10334 "Protect spaces and newline in string S."
10335 (while (string-match " " s)
10336 (setq s (replace-match "%20" t t s)))
10337 (while (string-match "\n" s)
10338 (setq s (replace-match "%0A" t t s)))
10341 (defun org-entry-restore-space (s)
10342 "Restore spaces and newline in string S."
10343 (while (string-match "%20" s)
10344 (setq s (replace-match " " t t s)))
10345 (while (string-match "%0A" s)
10346 (setq s (replace-match "\n" t t s)))
10349 (defvar org-entry-property-inherited-from (make-marker)
10350 "Marker pointing to the entry from where a property was inherited.
10351 Each call to `org-entry-get-with-inheritance' will set this marker to the
10352 location of the entry where the inheritance search matched. If there was
10353 no match, the marker will point nowhere.
10354 Note that also `org-entry-get' calls this function, if the INHERIT flag
10357 (defun org-entry-get-with-inheritance (property)
10358 "Get entry property, and search higher levels if not present."
10359 (move-marker org-entry-property-inherited-from nil)
10366 (when (setq tmp (org-entry-get nil property))
10367 (org-back-to-heading t)
10368 (move-marker org-entry-property-inherited-from (point))
10370 (or (org-up-heading-safe) (throw 'ex nil)))))
10372 (cdr (assoc property org-file-properties))
10373 (cdr (assoc property org-global-properties))
10374 (cdr (assoc property org-global-properties-fixed))))))
10376 (defun org-entry-put (pom property value)
10377 "Set PROPERTY to VALUE for entry at point-or-marker POM."
10378 (org-with-point-at pom
10379 (org-back-to-heading t)
10380 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
10383 ((equal property "TODO")
10384 (when (and (stringp value) (string-match "\\S-" value)
10385 (not (member value org-todo-keywords-1)))
10386 (error "\"%s\" is not a valid TODO state" value))
10387 (if (or (not value)
10388 (not (string-match "\\S-" value)))
10389 (setq value 'none))
10391 (org-set-tags nil 'align))
10392 ((equal property "PRIORITY")
10393 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
10394 (string-to-char value) ?\ ))
10395 (org-set-tags nil 'align))
10396 ((equal property "SCHEDULED")
10397 (if (re-search-forward org-scheduled-time-regexp end t)
10399 ((eq value 'earlier) (org-timestamp-change -1 'day))
10400 ((eq value 'later) (org-timestamp-change 1 'day))
10401 (t (call-interactively 'org-schedule)))
10402 (call-interactively 'org-schedule)))
10403 ((equal property "DEADLINE")
10404 (if (re-search-forward org-deadline-time-regexp end t)
10406 ((eq value 'earlier) (org-timestamp-change -1 'day))
10407 ((eq value 'later) (org-timestamp-change 1 'day))
10408 (t (call-interactively 'org-deadline)))
10409 (call-interactively 'org-deadline)))
10410 ((member property org-special-properties)
10411 (error "The %s property can not yet be set with `org-entry-put'"
10413 (t ; a non-special property
10414 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
10415 (setq range (org-get-property-block beg end 'force))
10416 (goto-char (car range))
10417 (if (re-search-forward
10418 (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
10420 (delete-region (match-beginning 1) (match-end 1))
10421 (goto-char (match-beginning 1)))
10422 (goto-char (cdr range))
10425 (org-indent-line-function)
10426 (insert ":" property ":"))
10427 (and value (insert " " value))
10428 (org-indent-line-function)))))))
10430 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
10431 "Get all property keys in the current buffer.
10432 With INCLUDE-SPECIALS, also list the special properties that reflect things
10433 like tags and TODO state.
10434 With INCLUDE-DEFAULTS, also include properties that has special meaning
10435 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
10436 With INCLUDE-COLUMNS, also include property names given in COLUMN
10437 formats in the current buffer."
10438 (let (rtn range cfmt cols s p)
10442 (goto-char (point-min))
10443 (while (re-search-forward org-property-start-re nil t)
10444 (setq range (org-get-property-block))
10445 (goto-char (car range))
10446 (while (re-search-forward
10447 (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
10449 (add-to-list 'rtn (org-match-string-no-properties 1)))
10450 (outline-next-heading))))
10452 (when include-specials
10453 (setq rtn (append org-special-properties rtn)))
10455 (when include-defaults
10456 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
10458 (when include-columns
10462 (goto-char (point-min))
10463 (while (re-search-forward
10464 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
10466 (setq cfmt (match-string 2) s 0)
10467 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
10469 (setq s (match-end 0)
10470 p (match-string 1 cfmt))
10471 (unless (or (equal p "ITEM")
10472 (member p org-special-properties))
10473 (add-to-list 'rtn (match-string 1 cfmt))))))))
10475 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
10477 (defun org-property-values (key)
10478 "Return a list of all values of property KEY."
10482 (goto-char (point-min))
10483 (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
10485 (while (re-search-forward re nil t)
10486 (add-to-list 'values (org-trim (match-string 1))))
10487 (delete "" values)))))
10489 (defun org-insert-property-drawer ()
10490 "Insert a property drawer into the current entry."
10492 (org-back-to-heading t)
10493 (looking-at outline-regexp)
10494 (let ((indent (- (match-end 0)(match-beginning 0)))
10496 (re (concat "^[ \t]*" org-keyword-time-regexp))
10498 (outline-next-heading)
10501 (while (re-search-forward re end t))
10502 (setq hiddenp (org-invisible-p))
10504 (and (equal (char-after) ?\n) (forward-char 1))
10505 (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
10506 (beginning-of-line 2))
10507 (org-skip-over-state-notes)
10508 (skip-chars-backward " \t\n\r")
10509 (if (eq (char-before) ?*) (forward-char 1))
10510 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
10511 (beginning-of-line 0)
10512 (org-indent-to-column indent)
10513 (beginning-of-line 2)
10514 (org-indent-to-column indent)
10515 (beginning-of-line 0)
10518 (org-back-to-heading t)
10520 (org-flag-drawer t))))
10522 (defun org-set-property (property value)
10523 "In the current entry, set PROPERTY to VALUE.
10524 When called interactively, this will prompt for a property name, offering
10525 completion on existing and default properties. And then it will prompt
10526 for a value, offering completion either on allowed values (via an inherited
10527 xxx_ALL property) or on existing values in other instances of this property
10528 in the current file."
10530 (let* ((completion-ignore-case t)
10531 (keys (org-buffer-property-keys nil t t))
10532 (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
10533 (prop (if (member prop0 keys)
10535 (or (cdr (assoc (downcase prop0)
10536 (mapcar (lambda (x) (cons (downcase x) x))
10539 (cur (org-entry-get nil prop))
10540 (allowed (org-property-get-allowed-values nil prop 'table))
10541 (existing (mapcar 'list (org-property-values prop)))
10543 (org-completing-read "Value: " allowed nil 'req-match)
10544 (org-completing-read
10545 (concat "Value" (if (and cur (string-match "\\S-" cur))
10546 (concat "[" cur "]") "")
10548 existing nil nil "" nil cur))))
10549 (list prop (if (equal val "") cur val))))
10550 (unless (equal (org-entry-get nil property) value)
10551 (org-entry-put nil property value)))
10553 (defun org-delete-property (property)
10554 "In the current entry, delete PROPERTY."
10556 (let* ((completion-ignore-case t)
10557 (prop (org-ido-completing-read
10558 "Property: " (org-entry-properties nil 'standard))))
10560 (message "Property %s %s" property
10561 (if (org-entry-delete nil property)
10563 "was not present in the entry")))
10565 (defun org-delete-property-globally (property)
10566 "Remove PROPERTY globally, from all entries."
10568 (let* ((completion-ignore-case t)
10569 (prop (org-ido-completing-read
10570 "Globally remove property: "
10571 (mapcar 'list (org-buffer-property-keys)))))
10576 (goto-char (point-min))
10578 (while (re-search-forward
10579 (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
10581 (setq cnt (1+ cnt))
10582 (replace-match ""))
10583 (message "Property \"%s\" removed from %d entries" property cnt)))))
10585 (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
10587 (defun org-compute-property-at-point ()
10588 "Compute the property at point.
10589 This looks for an enclosing column format, extracts the operator and
10590 then applies it to the property in the column format's scope."
10592 (unless (org-at-property-p)
10593 (error "Not at a property"))
10594 (let ((prop (org-match-string-no-properties 2)))
10595 (org-columns-get-format-and-top-level)
10596 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
10597 (error "No operator defined for property %s" prop))
10598 (org-columns-compute prop)))
10600 (defun org-property-get-allowed-values (pom property &optional table)
10601 "Get allowed values for the property PROPERTY.
10602 When TABLE is non-nil, return an alist that can directly be used for
10606 ((equal property "TODO")
10607 (setq vals (org-with-point-at pom
10608 (append org-todo-keywords-1 '("")))))
10609 ((equal property "PRIORITY")
10610 (let ((n org-lowest-priority))
10611 (while (>= n org-highest-priority)
10612 (push (char-to-string n) vals)
10614 ((member property org-special-properties))
10616 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
10618 (when (and vals (string-match "\\S-" vals))
10619 (setq vals (car (read-from-string (concat "(" vals ")"))))
10620 (setq vals (mapcar (lambda (x)
10621 (cond ((stringp x) x)
10622 ((numberp x) (number-to-string x))
10623 ((symbolp x) (symbol-name x))
10626 (if table (mapcar 'list vals) vals)))
10628 (defun org-property-previous-allowed-value (&optional previous)
10629 "Switch to the next allowed value for this property."
10631 (org-property-next-allowed-value t))
10633 (defun org-property-next-allowed-value (&optional previous)
10634 "Switch to the next allowed value for this property."
10636 (unless (org-at-property-p)
10637 (error "Not at a property"))
10638 (let* ((key (match-string 2))
10639 (value (match-string 3))
10640 (allowed (or (org-property-get-allowed-values (point) key)
10641 (and (member value '("[ ]" "[-]" "[X]"))
10645 (error "Allowed values for this property have not been defined"))
10646 (if previous (setq allowed (reverse allowed)))
10647 (if (member value allowed)
10648 (setq nval (car (cdr (member value allowed)))))
10649 (setq nval (or nval (car allowed)))
10650 (if (equal nval value)
10651 (error "Only one allowed value for this property"))
10652 (org-at-property-p)
10653 (replace-match (concat " :" key ": " nval) t t)
10654 (org-indent-line-function)
10655 (beginning-of-line 1)
10656 (skip-chars-forward " \t")))
10658 (defun org-find-entry-with-id (ident)
10659 "Locate the entry that contains the ID property with exact value IDENT.
10660 IDENT can be a string, a symbol or a number, this function will search for
10661 the string representation of it.
10662 Return the position where this entry starts, or nil if there is no such entry."
10663 (interactive "sID: ")
10665 ((stringp ident) ident)
10666 ((symbol-name ident) (symbol-name ident))
10667 ((numberp ident) (number-to-string ident))
10668 (t (error "IDENT %s must be a string, symbol or number" ident))))
10669 (case-fold-search nil))
10673 (goto-char (point-min))
10674 (when (re-search-forward
10675 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
10677 (org-back-to-heading)
10682 (defvar org-last-changed-timestamp nil)
10683 (defvar org-last-inserted-timestamp nil
10684 "The last time stamp inserted with `org-insert-time-stamp'.")
10685 (defvar org-time-was-given) ; dynamically scoped parameter
10686 (defvar org-end-time-was-given) ; dynamically scoped parameter
10687 (defvar org-ts-what) ; dynamically scoped parameter
10689 (defun org-time-stamp (arg &optional inactive)
10690 "Prompt for a date/time and insert a time stamp.
10691 If the user specifies a time like HH:MM, or if this command is called
10692 with a prefix argument, the time stamp will contain date and time.
10693 Otherwise, only the date will be included. All parts of a date not
10694 specified by the user will be filled in from the current date/time.
10695 So if you press just return without typing anything, the time stamp
10696 will represent the current date/time. If there is already a timestamp
10697 at the cursor, it will be modified."
10701 ;; Default time is either today, or, when entering a range,
10702 ;; the range start.
10703 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
10705 (re-search-backward
10706 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
10707 (- (point) 20) t)))
10708 (apply 'encode-time (org-parse-time-string (match-string 1)))
10710 (default-input (and ts (org-get-compact-tod ts)))
10711 org-time-was-given org-end-time-was-given time)
10713 ((and (org-at-timestamp-p t)
10714 (memq last-command '(org-time-stamp org-time-stamp-inactive))
10715 (memq this-command '(org-time-stamp org-time-stamp-inactive)))
10717 (setq time (let ((this-command this-command))
10718 (org-read-date arg 'totime nil nil
10719 default-time default-input)))
10720 (org-insert-time-stamp time (or org-time-was-given arg) inactive))
10721 ((org-at-timestamp-p t)
10722 (setq time (let ((this-command this-command))
10723 (org-read-date arg 'totime nil nil default-time default-input)))
10724 (when (org-at-timestamp-p t) ; just to get the match data
10725 ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
10727 (setq org-last-changed-timestamp
10728 (org-insert-time-stamp
10729 time (or org-time-was-given arg)
10730 inactive nil nil (list org-end-time-was-given))))
10731 (message "Timestamp updated"))
10733 (setq time (let ((this-command this-command))
10734 (org-read-date arg 'totime nil nil default-time default-input)))
10735 (org-insert-time-stamp time (or org-time-was-given arg) inactive
10736 nil nil (list org-end-time-was-given))))))
10738 ;; FIXME: can we use this for something else, like computing time differences?
10739 (defun org-get-compact-tod (s)
10740 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
10741 (let* ((t1 (match-string 1 s))
10742 (h1 (string-to-number (match-string 2 s)))
10743 (m1 (string-to-number (match-string 3 s)))
10744 (t2 (and (match-end 4) (match-string 5 s)))
10745 (h2 (and t2 (string-to-number (match-string 6 s))))
10746 (m2 (and t2 (string-to-number (match-string 7 s))))
10750 (setq dh (- h2 h1) dm (- m2 m1))
10751 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
10752 (concat t1 "+" (number-to-string dh)
10753 (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
10755 (defun org-time-stamp-inactive (&optional arg)
10756 "Insert an inactive time stamp.
10757 An inactive time stamp is enclosed in square brackets instead of angle
10758 brackets. It is inactive in the sense that it does not trigger agenda entries,
10759 does not link to the calendar and cannot be changed with the S-cursor keys.
10760 So these are more for recording a certain time/date."
10762 (org-time-stamp arg 'inactive))
10764 (defvar org-date-ovl (org-make-overlay 1 1))
10765 (org-overlay-put org-date-ovl 'face 'org-warning)
10766 (org-detach-overlay org-date-ovl)
10768 (defvar org-ans1) ; dynamically scoped parameter
10769 (defvar org-ans2) ; dynamically scoped parameter
10771 (defvar org-plain-time-of-day-regexp) ; defined below
10773 (defvar org-overriding-default-time nil) ; dynamically scoped
10774 (defvar org-read-date-overlay nil)
10775 (defvar org-dcst nil) ; dynamically scoped
10777 (defun org-read-date (&optional with-time to-time from-string prompt
10778 default-time default-input)
10779 "Read a date, possibly a time, and make things smooth for the user.
10780 The prompt will suggest to enter an ISO date, but you can also enter anything
10781 which will at least partially be understood by `parse-time-string'.
10782 Unrecognized parts of the date will default to the current day, month, year,
10783 hour and minute. If this command is called to replace a timestamp at point,
10784 of to enter the second timestamp of a range, the default time is taken from the
10785 existing stamp. For example,
10786 3-2-5 --> 2003-02-05
10787 feb 15 --> currentyear-02-15
10788 sep 12 9 --> 2009-09-12
10789 12:45 --> today 12:45
10790 22 sept 0:34 --> currentyear-09-22 0:34
10791 12 --> currentyear-currentmonth-12
10792 Fri --> nearest Friday (today or later)
10795 Furthermore you can specify a relative date by giving, as the *first* thing
10796 in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
10797 change in days weeks, months, years.
10798 With a single plus or minus, the date is relative to today. With a double
10799 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
10800 +4d --> four days from today
10801 +4 --> same as above
10802 +2w --> two weeks from today
10803 ++5 --> five days from default date
10805 The function understands only English month and weekday abbreviations,
10806 but this can be configured with the variables `parse-time-months' and
10807 `parse-time-weekdays'.
10809 While prompting, a calendar is popped up - you can also select the
10810 date with the mouse (button 1). The calendar shows a period of three
10811 months. To scroll it to other months, use the keys `>' and `<'.
10812 If you don't like the calendar, turn it off with
10813 \(setq org-read-date-popup-calendar nil)
10815 With optional argument TO-TIME, the date will immediately be converted
10816 to an internal time.
10817 With an optional argument WITH-TIME, the prompt will suggest to also
10818 insert a time. Note that when WITH-TIME is not set, you can still
10819 enter a time, and this function will inform the calling routine about
10820 this change. The calling routine may then choose to change the format
10821 used to insert the time stamp into the buffer to include the time.
10822 With optional argument FROM-STRING, read from this string instead from
10823 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
10824 the time/date that is used for everything that is not specified by the
10826 (require 'parse-time)
10827 (let* ((org-time-stamp-rounding-minutes
10828 (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
10829 (org-dcst org-display-custom-times)
10830 (ct (org-current-time))
10831 (def (or org-overriding-default-time default-time ct))
10832 (defdecode (decode-time def))
10834 (when (< (nth 2 defdecode) org-extend-today-until)
10835 (setcar (nthcdr 2 defdecode) -1)
10836 (setcar (nthcdr 1 defdecode) 59)
10837 (setq def (apply 'encode-time defdecode)
10838 defdecode (decode-time def)))))
10839 (calendar-move-hook nil)
10840 (calendar-view-diary-initially-flag nil)
10841 (view-diary-entries-initially nil)
10842 (calendar-view-holidays-initially-flag nil)
10843 (view-calendar-holidays-initially nil)
10844 (timestr (format-time-string
10845 (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
10846 (prompt (concat (if prompt (concat prompt " ") "")
10847 (format "Date+time [%s]: " timestr)))
10848 ans (org-ans0 "") org-ans1 org-ans2 final)
10851 (from-string (setq ans from-string))
10852 (org-read-date-popup-calendar
10854 (save-window-excursion
10856 (calendar-forward-day (- (time-to-days def)
10857 (calendar-absolute-from-gregorian
10858 (calendar-current-date))))
10859 (org-eval-in-calendar nil t)
10860 (let* ((old-map (current-local-map))
10861 (map (copy-keymap calendar-mode-map))
10862 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
10863 (org-defkey map (kbd "RET") 'org-calendar-select)
10864 (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
10865 'org-calendar-select-mouse)
10866 (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2
])
10867 'org-calendar-select-mouse
)
10868 (org-defkey minibuffer-local-map
[(meta shift left
)]
10869 (lambda () (interactive)
10870 (org-eval-in-calendar '(calendar-backward-month 1))))
10871 (org-defkey minibuffer-local-map
[(meta shift right
)]
10872 (lambda () (interactive)
10873 (org-eval-in-calendar '(calendar-forward-month 1))))
10874 (org-defkey minibuffer-local-map
[(meta shift up
)]
10875 (lambda () (interactive)
10876 (org-eval-in-calendar '(calendar-backward-year 1))))
10877 (org-defkey minibuffer-local-map
[(meta shift down
)]
10878 (lambda () (interactive)
10879 (org-eval-in-calendar '(calendar-forward-year 1))))
10880 (org-defkey minibuffer-local-map
[(shift up
)]
10881 (lambda () (interactive)
10882 (org-eval-in-calendar '(calendar-backward-week 1))))
10883 (org-defkey minibuffer-local-map
[(shift down
)]
10884 (lambda () (interactive)
10885 (org-eval-in-calendar '(calendar-forward-week 1))))
10886 (org-defkey minibuffer-local-map
[(shift left
)]
10887 (lambda () (interactive)
10888 (org-eval-in-calendar '(calendar-backward-day 1))))
10889 (org-defkey minibuffer-local-map
[(shift right
)]
10890 (lambda () (interactive)
10891 (org-eval-in-calendar '(calendar-forward-day 1))))
10892 (org-defkey minibuffer-local-map
">"
10893 (lambda () (interactive)
10894 (org-eval-in-calendar '(scroll-calendar-left 1))))
10895 (org-defkey minibuffer-local-map
"<"
10896 (lambda () (interactive)
10897 (org-eval-in-calendar '(scroll-calendar-right 1))))
10900 (use-local-map map
)
10901 (add-hook 'post-command-hook
'org-read-date-display
)
10902 (setq org-ans0
(read-string prompt default-input nil nil
))
10903 ;; org-ans0: from prompt
10904 ;; org-ans1: from mouse click
10905 ;; org-ans2: from calendar motion
10906 (setq ans
(concat org-ans0
" " (or org-ans1 org-ans2
))))
10907 (remove-hook 'post-command-hook
'org-read-date-display
)
10908 (use-local-map old-map
)
10909 (when org-read-date-overlay
10910 (org-delete-overlay org-read-date-overlay
)
10911 (setq org-read-date-overlay nil
)))))))
10913 (t ; Naked prompt only
10915 (setq ans
(read-string prompt default-input nil timestr
))
10916 (when org-read-date-overlay
10917 (org-delete-overlay org-read-date-overlay
)
10918 (setq org-read-date-overlay nil
)))))
10920 (setq final
(org-read-date-analyze ans def defdecode
))
10923 (apply 'encode-time final
)
10924 (if (and (boundp 'org-time-was-given
) org-time-was-given
)
10925 (format "%04d-%02d-%02d %02d:%02d"
10926 (nth 5 final
) (nth 4 final
) (nth 3 final
)
10927 (nth 2 final
) (nth 1 final
))
10928 (format "%04d-%02d-%02d" (nth 5 final
) (nth 4 final
) (nth 3 final
))))))
10932 (defun org-read-date-display ()
10933 "Display the current date prompt interpretation in the minibuffer."
10934 (when org-read-date-display-live
10935 (when org-read-date-overlay
10936 (org-delete-overlay org-read-date-overlay
))
10939 (while (not (equal (buffer-substring
10940 (max (point-min) (- (point) 4)) (point))
10944 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
10945 " " (or org-ans1 org-ans2
)))
10946 (org-end-time-was-given nil
)
10947 (f (org-read-date-analyze ans def defdecode
))
10949 org-time-stamp-custom-formats
10950 org-time-stamp-formats
))
10951 (fmt (if (or with-time
10952 (and (boundp 'org-time-was-given
) org-time-was-given
))
10955 (txt (concat "=> " (format-time-string fmt
(apply 'encode-time f
)))))
10956 (when (and org-end-time-was-given
10957 (string-match org-plain-time-of-day-regexp txt
))
10958 (setq txt
(concat (substring txt
0 (match-end 0)) "-"
10959 org-end-time-was-given
10960 (substring txt
(match-end 0)))))
10961 (setq org-read-date-overlay
10962 (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
10963 (org-overlay-display org-read-date-overlay txt
'secondary-selection
))))
10965 (defun org-read-date-analyze (ans def defdecode
)
10966 "Analyse the combined answer of the date prompt."
10967 ;; FIXME: cleanup and comment
10968 (let (delta deltan deltaw deltadef year month day
10969 hour minute second wday pm h2 m2 tl wday1
10970 iso-year iso-weekday iso-week iso-year iso-date
)
10972 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans
)
10975 (when (setq delta
(org-read-date-get-relative ans
(current-time) def
))
10976 (setq ans
(replace-match "" t t ans
)
10978 deltaw
(nth 1 delta
)
10979 deltadef
(nth 2 delta
)))
10981 ;; Check if there is an iso week date in there
10982 ;; If yes, sore the info and postpone interpreting it until the rest
10983 ;; of the parsing is done
10984 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans
)
10985 (setq iso-year
(if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans
))))
10986 iso-weekday
(if (match-end 3) (string-to-number (match-string 3 ans
)))
10987 iso-week
(string-to-number (match-string 2 ans
)))
10988 (setq ans
(replace-match "" t t ans
)))
10990 ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
10991 (when (string-match
10992 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans
)
10993 (setq year
(if (match-end 2)
10994 (string-to-number (match-string 2 ans
))
10995 (string-to-number (format-time-string "%Y")))
10996 month
(string-to-number (match-string 3 ans
))
10997 day
(string-to-number (match-string 4 ans
)))
10998 (if (< year
100) (setq year
(+ 2000 year
)))
10999 (setq ans
(replace-match (format "%04d-%02d-%02d\\5" year month day
)
11001 ;; Help matching am/pm times, because `parse-time-string' does not do that.
11002 ;; If there is a time with am/pm, and *no* time without it, we convert
11003 ;; so that matching will be successful.
11004 (loop for i from
1 to
2 do
; twice, for end time as well
11005 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans
))
11006 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans
))
11007 (setq hour
(string-to-number (match-string 1 ans
))
11008 minute
(if (match-end 3)
11009 (string-to-number (match-string 3 ans
))
11012 (string-to-char (downcase (match-string 4 ans
)))))
11013 (if (and (= hour
12) (not pm
))
11015 (if (and pm
(< hour
12)) (setq hour
(+ 12 hour
))))
11016 (setq ans
(replace-match (format "%02d:%02d" hour minute
)
11019 ;; Check if a time range is given as a duration
11020 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans
)
11021 (setq hour
(string-to-number (match-string 1 ans
))
11022 h2
(+ hour
(string-to-number (match-string 3 ans
)))
11023 minute
(string-to-number (match-string 2 ans
))
11024 m2
(+ minute
(if (match-end 5) (string-to-number
11025 (match-string 5 ans
))0)))
11026 (if (>= m2
60) (setq h2
(1+ h2
) m2
(- m2
60)))
11027 (setq ans
(replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2
)
11030 ;; Check if there is a time range
11031 (when (boundp 'org-end-time-was-given
)
11032 (setq org-time-was-given nil
)
11033 (when (and (string-match org-plain-time-of-day-regexp ans
)
11035 (setq org-end-time-was-given
(match-string 8 ans
))
11036 (setq ans
(concat (substring ans
0 (match-beginning 7))
11037 (substring ans
(match-end 7))))))
11039 (setq tl
(parse-time-string ans
)
11040 day
(or (nth 3 tl
) (nth 3 defdecode
))
11041 month
(or (nth 4 tl
)
11042 (if (and org-read-date-prefer-future
11043 (nth 3 tl
) (< (nth 3 tl
) (nth 3 defdecode
)))
11044 (1+ (nth 4 defdecode
))
11045 (nth 4 defdecode
)))
11046 year
(or (nth 5 tl
)
11047 (if (and org-read-date-prefer-future
11048 (nth 4 tl
) (< (nth 4 tl
) (nth 4 defdecode
)))
11049 (1+ (nth 5 defdecode
))
11050 (nth 5 defdecode
)))
11051 hour
(or (nth 2 tl
) (nth 2 defdecode
))
11052 minute
(or (nth 1 tl
) (nth 1 defdecode
))
11053 second
(or (nth 0 tl
) 0)
11056 ;; Special date definitions below
11059 ;; There was an iso week
11060 (setq year
(or iso-year year
)
11061 day
(or iso-weekday wday
1)
11062 wday nil
; to make sure that the trigger below does not match
11063 iso-date
(calendar-gregorian-from-absolute
11064 (calendar-absolute-from-iso
11065 (list iso-week day year
))))
11066 ; FIXME: Should we also push ISO weeks into the future?
11067 ; (when (and org-read-date-prefer-future
11069 ; (< (calendar-absolute-from-gregorian iso-date)
11070 ; (time-to-days (current-time))))
11071 ; (setq year (1+ year)
11072 ; iso-date (calendar-gregorian-from-absolute
11073 ; (calendar-absolute-from-iso
11074 ; (list iso-week day year)))))
11075 (setq month
(car iso-date
)
11076 year
(nth 2 iso-date
)
11077 day
(nth 1 iso-date
)))
11080 (let ((now (decode-time (current-time))))
11081 (setq day
(nth 3 now
) month
(nth 4 now
) year
(nth 5 now
))))
11082 (cond ((member deltaw
'("d" "")) (setq day
(+ day deltan
)))
11083 ((equal deltaw
"w") (setq day
(+ day
(* 7 deltan
))))
11084 ((equal deltaw
"m") (setq month
(+ month deltan
)))
11085 ((equal deltaw
"y") (setq year
(+ year deltan
)))))
11086 ((and wday
(not (nth 3 tl
)))
11087 ;; Weekday was given, but no day, so pick that day in the week
11088 ;; on or after the derived date.
11089 (setq wday1
(nth 6 (decode-time (encode-time 0 0 0 day month year
))))
11090 (unless (equal wday wday1
)
11091 (setq day
(+ day
(%
(- wday wday1 -
7) 7))))))
11092 (if (and (boundp 'org-time-was-given
)
11094 (setq org-time-was-given t
))
11095 (if (< year
100) (setq year
(+ 2000 year
)))
11096 (if (< year
1970) (setq year
(nth 5 defdecode
))) ; not representable
11097 (list second minute hour day month year
)))
11099 (defvar parse-time-weekdays
)
11101 (defun org-read-date-get-relative (s today default
)
11102 "Check string S for special relative date string.
11103 TODAY and DEFAULT are internal times, for today and for a default.
11104 Return shift list (N what def-flag)
11105 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
11106 N is the number of WHATs to shift.
11107 DEF-FLAG is t when a double ++ or -- indicates shift relative to
11108 the DEFAULT date rather than TODAY."
11112 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
11114 "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays
"\\|") "\\)\\)?"
11115 "\\([ \t]\\|$\\)") s
)
11116 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
11117 (let* ((dir (if (> (match-end 1) (match-beginning 1))
11118 (string-to-char (substring (match-string 1 s
) -
1))
11120 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
11121 (n (if (match-end 2) (string-to-number (match-string 2 s
)) 1))
11122 (what (if (match-end 3) (match-string 3 s
) "d"))
11123 (wday1 (cdr (assoc (downcase what
) parse-time-weekdays
)))
11124 (date (if rel default today
))
11125 (wday (nth 6 (decode-time date
)))
11129 (setq delta
(mod (+ 7 (- wday1 wday
)) 7))
11130 (if (= dir ?-
) (setq delta
(- delta
7)))
11131 (if (> n
1) (setq delta
(+ delta
(* (1- n
) (if (= dir ?-
) -
7 7)))))
11132 (list delta
"d" rel
))
11133 (list (* n
(if (= dir ?-
) -
1 1)) what rel
)))))
11135 (defun org-eval-in-calendar (form &optional keepdate
)
11136 "Eval FORM in the calendar window and return to current window.
11137 Also, store the cursor date in variable org-ans2."
11138 (let ((sw (selected-window)))
11139 (select-window (get-buffer-window "*Calendar*"))
11141 (when (and (not keepdate
) (calendar-cursor-to-date))
11142 (let* ((date (calendar-cursor-to-date))
11143 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
11144 (setq org-ans2
(format-time-string "%Y-%m-%d" time
))))
11145 (org-move-overlay org-date-ovl
(1- (point)) (1+ (point)) (current-buffer))
11146 (select-window sw
)))
11148 (defun org-calendar-select ()
11149 "Return to `org-read-date' with the date currently selected.
11150 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
11152 (when (calendar-cursor-to-date)
11153 (let* ((date (calendar-cursor-to-date))
11154 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
11155 (setq org-ans1
(format-time-string "%Y-%m-%d" time
)))
11156 (if (active-minibuffer-window) (exit-minibuffer))))
11158 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra
)
11159 "Insert a date stamp for the date given by the internal TIME.
11160 WITH-HM means, use the stamp format that includes the time of the day.
11161 INACTIVE means use square brackets instead of angular ones, so that the
11162 stamp will not contribute to the agenda.
11163 PRE and POST are optional strings to be inserted before and after the
11165 The command returns the inserted time stamp."
11166 (let ((fmt (funcall (if with-hm
'cdr
'car
) org-time-stamp-formats
))
11168 (if inactive
(setq fmt
(concat "[" (substring fmt
1 -
1) "]")))
11169 (insert-before-markers (or pre
""))
11170 (insert-before-markers (setq stamp
(format-time-string fmt time
)))
11171 (when (listp extra
)
11172 (setq extra
(car extra
))
11173 (if (and (stringp extra
)
11174 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra
))
11175 (setq extra
(format "-%02d:%02d"
11176 (string-to-number (match-string 1 extra
))
11177 (string-to-number (match-string 2 extra
))))
11181 (insert-before-markers extra
)
11183 (insert-before-markers (or post
""))
11184 (setq org-last-inserted-timestamp stamp
)))
11186 (defun org-toggle-time-stamp-overlays ()
11187 "Toggle the use of custom time stamp formats."
11189 (setq org-display-custom-times
(not org-display-custom-times
))
11190 (unless org-display-custom-times
11191 (let ((p (point-min)) (bmp (buffer-modified-p)))
11192 (while (setq p
(next-single-property-change p
'display
))
11193 (if (and (get-text-property p
'display
)
11194 (eq (get-text-property p
'face
) 'org-date
))
11195 (remove-text-properties
11196 p
(setq p
(next-single-property-change p
'display
))
11198 (set-buffer-modified-p bmp
)))
11199 (if (featurep 'xemacs
)
11200 (remove-text-properties (point-min) (point-max) '(end-glyph t
)))
11201 (org-restart-font-lock)
11202 (setq org-table-may-need-update t
)
11203 (if org-display-custom-times
11204 (message "Time stamps are overlayed with custom format")
11205 (message "Time stamp overlays removed")))
11207 (defun org-display-custom-time (beg end
)
11208 "Overlay modified time stamp format over timestamp between BEG and END."
11209 (let* ((ts (buffer-substring beg end
))
11210 t1 w1 with-hm tf time str w2
(off 0))
11212 (setq t1
(org-parse-time-string ts t
))
11213 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts
)
11214 (setq off
(- (match-end 0) (match-beginning 0)))))
11215 (setq end
(- end off
))
11216 (setq w1
(- end beg
)
11217 with-hm
(and (nth 1 t1
) (nth 2 t1
))
11218 tf
(funcall (if with-hm
'cdr
'car
) org-time-stamp-custom-formats
)
11219 time
(org-fix-decoded-time t1
)
11221 (format-time-string
11222 (substring tf
1 -
1) (apply 'encode-time time
))
11223 nil
'mouse-face
'highlight
)
11225 (if (not (= w2 w1
))
11226 (add-text-properties (1+ beg
) (+ 2 beg
)
11227 (list 'org-dwidth t
'org-dwidth-n
(- w1 w2
))))
11228 (if (featurep 'xemacs
)
11230 (put-text-property beg end
'invisible t
)
11231 (put-text-property beg end
'end-glyph
(make-glyph str
)))
11232 (put-text-property beg end
'display str
))))
11234 (defun org-translate-time (string)
11235 "Translate all timestamps in STRING to custom format.
11236 But do this only if the variable `org-display-custom-times' is set."
11237 (when org-display-custom-times
11240 (re org-ts-regexp-both
)
11241 t1 with-hm inactive tf time str beg end
)
11242 (while (setq start
(string-match re string start
))
11243 (setq beg
(match-beginning 0)
11245 t1
(save-match-data
11246 (org-parse-time-string (substring string beg end
) t
))
11247 with-hm
(and (nth 1 t1
) (nth 2 t1
))
11248 inactive
(equal (substring string beg
(1+ beg
)) "[")
11249 tf
(funcall (if with-hm
'cdr
'car
)
11250 org-time-stamp-custom-formats
)
11251 time
(org-fix-decoded-time t1
)
11252 str
(format-time-string
11254 (if inactive
"[" "<") (substring tf
1 -
1)
11255 (if inactive
"]" ">"))
11256 (apply 'encode-time time
))
11257 string
(replace-match str t t string
)
11258 start
(+ start
(length str
)))))))
11261 (defun org-fix-decoded-time (time)
11262 "Set 0 instead of nil for the first 6 elements of time.
11263 Don't touch the rest."
11265 (mapcar (lambda (x) (if (< (setq n
(1+ n
)) 7) (or x
0) x
)) time
)))
11267 (defun org-days-to-time (timestamp-string)
11268 "Difference between TIMESTAMP-STRING and now in days."
11269 (- (time-to-days (org-time-string-to-time timestamp-string
))
11270 (time-to-days (current-time))))
11272 (defun org-deadline-close (timestamp-string &optional ndays
)
11273 "Is the time in TIMESTAMP-STRING close to the current date?"
11274 (setq ndays
(or ndays
(org-get-wdays timestamp-string
)))
11275 (and (< (org-days-to-time timestamp-string
) ndays
)
11276 (not (org-entry-is-done-p))))
11278 (defun org-get-wdays (ts)
11279 "Get the deadline lead time appropriate for timestring TS."
11281 ((<= org-deadline-warning-days
0)
11282 ;; 0 or negative, enforce this value no matter what
11283 (- org-deadline-warning-days
))
11284 ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts
)
11285 ;; lead time is specified.
11286 (floor (* (string-to-number (match-string 1 ts
))
11287 (cdr (assoc (match-string 2 ts
)
11288 '(("d" .
1) ("w" .
7)
11289 ("m" .
30.4) ("y" .
365.25)))))))
11290 ;; go for the default.
11291 (t org-deadline-warning-days
)))
11293 (defun org-calendar-select-mouse (ev)
11294 "Return to `org-read-date' with the date currently selected.
11295 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
11297 (mouse-set-point ev
)
11298 (when (calendar-cursor-to-date)
11299 (let* ((date (calendar-cursor-to-date))
11300 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
11301 (setq org-ans1
(format-time-string "%Y-%m-%d" time
)))
11302 (if (active-minibuffer-window) (exit-minibuffer))))
11304 (defun org-check-deadlines (ndays)
11305 "Check if there are any deadlines due or past due.
11306 A deadline is considered due if it happens within `org-deadline-warning-days'
11307 days from today's date. If the deadline appears in an entry marked DONE,
11308 it is not shown. The prefix arg NDAYS can be used to test that many
11309 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
11311 (let* ((org-warn-days
11313 ((equal ndays
'(4)) 100000)
11314 (ndays (prefix-numeric-value ndays
))
11315 (t (abs org-deadline-warning-days
))))
11316 (case-fold-search nil
)
11317 (regexp (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>"))
11319 (lambda () (org-deadline-close (match-string 1) org-warn-days
))))
11321 (message "%d deadlines past-due or due within %d days"
11322 (org-occur regexp nil callback
)
11325 (defun org-check-before-date (date)
11326 "Check if there are deadlines or scheduled entries before DATE."
11327 (interactive (list (org-read-date)))
11328 (let ((case-fold-search nil
)
11329 (regexp (concat "\\<\\(" org-deadline-string
11330 "\\|" org-scheduled-string
11331 "\\) *<\\([^>]+\\)>"))
11333 (lambda () (time-less-p
11334 (org-time-string-to-time (match-string 2))
11335 (org-time-string-to-time date
)))))
11336 (message "%d entries before %s"
11337 (org-occur regexp nil callback
) date
)))
11339 (defun org-evaluate-time-range (&optional to-buffer
)
11340 "Evaluate a time range by computing the difference between start and end.
11341 Normally the result is just printed in the echo area, but with prefix arg
11342 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
11343 If the time range is actually in a table, the result is inserted into the
11345 For time difference computation, a year is assumed to be exactly 365
11346 days in order to avoid rounding problems."
11349 (org-clock-update-time-maybe)
11351 (unless (org-at-date-range-p t
)
11352 (goto-char (point-at-bol))
11353 (re-search-forward org-tr-regexp-both
(point-at-eol) t
))
11354 (if (not (org-at-date-range-p t
))
11355 (error "Not at a time-stamp range, and none found in current line")))
11356 (let* ((ts1 (match-string 1))
11357 (ts2 (match-string 2))
11358 (havetime (or (> (length ts1
) 15) (> (length ts2
) 15)))
11359 (match-end (match-end 0))
11360 (time1 (org-time-string-to-time ts1
))
11361 (time2 (org-time-string-to-time ts2
))
11362 (t1 (time-to-seconds time1
))
11363 (t2 (time-to-seconds time2
))
11364 (diff (abs (- t2 t1
)))
11365 (negative (< (- t2 t1
) 0))
11366 ;; (ys (floor (* 365 24 60 60)))
11369 (fy "%dy %dd %02d:%02d")
11371 (fd "%dd %02d:%02d")
11376 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
11378 d
(floor (/ diff ds
)) diff
(mod diff ds
)
11379 h
(floor (/ diff hs
)) diff
(mod diff hs
)
11380 m
(floor (/ diff
60)))
11381 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
11383 d
(floor (+ (/ diff ds
) 0.5))
11385 (if (not to-buffer
)
11386 (message "%s" (org-make-tdiff-string y d h m
))
11387 (if (org-at-table-p)
11389 (goto-char match-end
)
11391 (and (looking-at " *|") (goto-char (match-end 0))))
11392 (goto-char match-end
))
11394 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
11395 (replace-match ""))
11396 (if negative
(insert " -"))
11397 (if (> y
0) (insert " " (format (if havetime fy fy1
) y d h m
))
11398 (if (> d
0) (insert " " (format (if havetime fd fd1
) d h m
))
11399 (insert " " (format fh h m
))))
11400 (if align
(org-table-align))
11401 (message "Time difference inserted")))))
11403 (defun org-make-tdiff-string (y d h m
)
11406 (if (> y
0) (setq fmt
(concat fmt
"%d year" (if (> y
1) "s" "") " ")
11408 (if (> d
0) (setq fmt
(concat fmt
"%d day" (if (> d
1) "s" "") " ")
11410 (if (> h
0) (setq fmt
(concat fmt
"%d hour" (if (> h
1) "s" "") " ")
11412 (if (> m
0) (setq fmt
(concat fmt
"%d minute" (if (> m
1) "s" "") " ")
11414 (apply 'format fmt
(nreverse l
))))
11416 (defun org-time-string-to-time (s)
11417 (apply 'encode-time
(org-parse-time-string s
)))
11419 (defun org-time-string-to-absolute (s &optional daynr prefer show-all
)
11420 "Convert a time stamp to an absolute day number.
11421 If there is a specifyer for a cyclic time stamp, get the closest date to
11423 PREFER and SHOW-ALL are passed through to `org-closest-date'."
11425 ((and daynr
(string-match "\\`%%\\((.*)\\)" s
))
11426 (if (org-diary-sexp-entry (match-string 1 s
) "" date
)
11429 ((and daynr
(string-match "\\+[0-9]+[dwmy]" s
))
11430 (org-closest-date s
(if (and (boundp 'daynr
) (integerp daynr
)) daynr
11431 (time-to-days (current-time))) (match-string 0 s
)
11433 (t (time-to-days (apply 'encode-time
(org-parse-time-string s
))))))
11435 (defun org-days-to-iso-week (days)
11436 "Return the iso week number."
11438 (car (calendar-iso-from-absolute days
)))
11440 (defun org-small-year-to-year (year)
11441 "Convert 2-digit years into 4-digit years.
11442 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
11443 The year 2000 cannot be abbreviated. Any year larger than 99
11444 is returned unchanged."
11446 (setq year
(+ 2000 year
))
11448 (setq year
(+ 1900 year
))))
11451 (defun org-time-from-absolute (d)
11452 "Return the time corresponding to date D.
11453 D may be an absolute day number, or a calendar-type list (month day year)."
11454 (if (numberp d
) (setq d
(calendar-gregorian-from-absolute d
)))
11455 (encode-time 0 0 0 (nth 1 d
) (car d
) (nth 2 d
)))
11457 (defun org-calendar-holiday ()
11458 "List of holidays, for Diary display in Org-mode."
11459 (require 'holidays
)
11461 (if (fboundp 'calendar-check-holidays
)
11462 'calendar-check-holidays
'check-calendar-holidays
) date
)))
11463 (if hl
(mapconcat 'identity hl
"; "))))
11465 (defun org-diary-sexp-entry (sexp entry date
)
11466 "Process a SEXP diary ENTRY for DATE."
11467 (require 'diary-lib
)
11468 (let ((result (if calendar-debug-sexp
11469 (let ((stack-trace-on-error t
))
11470 (eval (car (read-from-string sexp
))))
11471 (condition-case nil
11472 (eval (car (read-from-string sexp
)))
11475 (message "Bad sexp at line %d in %s: %s"
11477 (buffer-file-name) sexp
)
11479 (cond ((stringp result
) result
)
11480 ((and (consp result
)
11481 (stringp (cdr result
))) (cdr result
))
11485 (defun org-diary-to-ical-string (frombuf)
11486 "Get iCalendar entries from diary entries in buffer FROMBUF.
11487 This uses the icalendar.el library."
11488 (let* ((tmpdir (if (featurep 'xemacs
)
11490 temporary-file-directory
))
11491 (tmpfile (make-temp-name
11492 (expand-file-name "orgics" tmpdir
)))
11495 (set-buffer frombuf
)
11496 (icalendar-export-region (point-min) (point-max) tmpfile
)
11497 (setq buf
(find-buffer-visiting tmpfile
))
11499 (goto-char (point-min))
11500 (if (re-search-forward "^BEGIN:VEVENT" nil t
)
11501 (setq b
(match-beginning 0)))
11502 (goto-char (point-max))
11503 (if (re-search-backward "^END:VEVENT" nil t
)
11504 (setq e
(match-end 0)))
11505 (setq rtn
(if (and b e
) (concat (buffer-substring b e
) "\n") "")))
11507 (delete-file tmpfile
)
11510 (defun org-closest-date (start current change prefer show-all
)
11511 "Find the date closest to CURRENT that is consistent with START and CHANGE.
11512 When PREFER is `past' return a date that is either CURRENT or past.
11513 When PREFER is `future', return a date that is either CURRENT or future.
11514 When SHOW-ALL is nil, only return the current occurrence of a time stamp."
11515 ;; Make the proper lists from the dates
11517 (let ((a1 '(("d" . day
) ("w" . week
) ("m" . month
) ("y" . year
)))
11518 dn dw sday cday n1 n2
11519 d m y y1 y2 date1 date2 nmonths nm ny m2
)
11521 (setq start
(org-date-to-gregorian start
)
11522 current
(org-date-to-gregorian
11525 (time-to-days (current-time))))
11526 sday
(calendar-absolute-from-gregorian start
)
11527 cday
(calendar-absolute-from-gregorian current
))
11529 (if (<= cday sday
) (throw 'exit sday
))
11531 (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change
)
11532 (setq dn
(string-to-number (match-string 1 change
))
11533 dw
(cdr (assoc (match-string 2 change
) a1
)))
11534 (error "Invalid change specifyer: %s" change
))
11535 (if (eq dw
'week
) (setq dw
'day dn
(* 7 dn
)))
11538 (setq n1
(+ sday
(* dn
(floor (/ (- cday sday
) dn
))))
11541 (setq d
(nth 1 start
) m
(car start
) y1
(nth 2 start
) y2
(nth 2 current
))
11542 (setq y1
(+ (* (floor (/ (- y2 y1
) dn
)) dn
) y1
))
11543 (setq date1
(list m d y1
)
11544 n1
(calendar-absolute-from-gregorian date1
)
11545 date2
(list m d
(+ y1
(* (if (< n1 cday
) 1 -
1) dn
)))
11546 n2
(calendar-absolute-from-gregorian date2
)))
11548 ;; approx number of month between the two dates
11549 (setq nmonths
(floor (/ (- cday sday
) 30.436875)))
11550 ;; How often does dn fit in there?
11551 (setq d
(nth 1 start
) m
(car start
) y
(nth 2 start
)
11552 nm
(* dn
(max 0 (1- (floor (/ nmonths dn
)))))
11554 ny
(floor (/ m
12))
11557 (while (> m
12) (setq m
(- m
12) y
(1+ y
)))
11558 (setq n1
(calendar-absolute-from-gregorian (list m d y
)))
11559 (setq m2
(+ m dn
) y2 y
)
11560 (if (> m2
12) (setq y2
(1+ y2
) m2
(- m2
12)))
11561 (setq n2
(calendar-absolute-from-gregorian (list m2 d y2
)))
11562 (while (<= n2 cday
)
11563 (setq n1 n2 m m2 y y2
)
11564 (setq m2
(+ m dn
) y2 y
)
11565 (if (> m2
12) (setq y2
(1+ y2
) m2
(- m2
12)))
11566 (setq n2
(calendar-absolute-from-gregorian (list m2 d y2
))))))
11569 ((eq prefer
'past
) n1
)
11570 ((eq prefer
'future
) (if (= cday n1
) n1 n2
))
11571 (t (if (> (abs (- cday n1
)) (abs (- cday n2
))) n2 n1
)))
11573 ((eq prefer
'past
) n1
)
11574 ((eq prefer
'future
) (if (= cday n1
) n1 n2
))
11575 (t (if (= cday n1
) n1 n2
)))))))
11577 (defun org-date-to-gregorian (date)
11578 "Turn any specification of DATE into a gregorian date for the calendar."
11579 (cond ((integerp date
) (calendar-gregorian-from-absolute date
))
11580 ((and (listp date
) (= (length date
) 3)) date
)
11582 (setq date
(org-parse-time-string date
))
11583 (list (nth 4 date
) (nth 3 date
) (nth 5 date
)))
11585 (list (nth 4 date
) (nth 3 date
) (nth 5 date
)))))
11587 (defun org-parse-time-string (s &optional nodefault
)
11588 "Parse the standard Org-mode time string.
11589 This should be a lot faster than the normal `parse-time-string'.
11590 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
11591 hour and minute fields will be nil if not given."
11592 (if (string-match org-ts-regexp0 s
)
11594 (if (or (match-beginning 8) (not nodefault
))
11595 (string-to-number (or (match-string 8 s
) "0")))
11596 (if (or (match-beginning 7) (not nodefault
))
11597 (string-to-number (or (match-string 7 s
) "0")))
11598 (string-to-number (match-string 4 s
))
11599 (string-to-number (match-string 3 s
))
11600 (string-to-number (match-string 2 s
))
11604 (defun org-timestamp-up (&optional arg
)
11605 "Increase the date item at the cursor by one.
11606 If the cursor is on the year, change the year. If it is on the month or
11607 the day, change that.
11608 With prefix ARG, change by that many units."
11610 (org-timestamp-change (prefix-numeric-value arg
)))
11612 (defun org-timestamp-down (&optional arg
)
11613 "Decrease the date item at the cursor by one.
11614 If the cursor is on the year, change the year. If it is on the month or
11615 the day, change that.
11616 With prefix ARG, change by that many units."
11618 (org-timestamp-change (- (prefix-numeric-value arg
))))
11620 (defun org-timestamp-up-day (&optional arg
)
11621 "Increase the date in the time stamp by one day.
11622 With prefix ARG, change that many days."
11624 (if (and (not (org-at-timestamp-p t
))
11625 (org-on-heading-p))
11627 (org-timestamp-change (prefix-numeric-value arg
) 'day
)))
11629 (defun org-timestamp-down-day (&optional arg
)
11630 "Decrease the date in the time stamp by one day.
11631 With prefix ARG, change that many days."
11633 (if (and (not (org-at-timestamp-p t
))
11634 (org-on-heading-p))
11636 (org-timestamp-change (- (prefix-numeric-value arg
)) 'day
)))
11638 (defun org-at-timestamp-p (&optional inactive-ok
)
11639 "Determine if the cursor is in or at a timestamp."
11641 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2
))
11643 (ans (or (looking-at tsr
)
11645 (skip-chars-backward "^[<\n\r\t")
11646 (if (> (point) (point-min)) (backward-char 1))
11647 (and (looking-at tsr
)
11648 (> (- (match-end 0) pos
) -
1))))))
11650 (boundp 'org-ts-what
)
11653 ((= pos
(match-beginning 0)) 'bracket
)
11654 ((= pos
(1- (match-end 0))) 'bracket
)
11655 ((org-pos-in-match-range pos
2) 'year
)
11656 ((org-pos-in-match-range pos
3) 'month
)
11657 ((org-pos-in-match-range pos
7) 'hour
)
11658 ((org-pos-in-match-range pos
8) 'minute
)
11659 ((or (org-pos-in-match-range pos
4)
11660 (org-pos-in-match-range pos
5)) 'day
)
11661 ((and (> pos
(or (match-end 8) (match-end 5)))
11662 (< pos
(match-end 0)))
11663 (- pos
(or (match-end 8) (match-end 5))))
11667 (defun org-toggle-timestamp-type ()
11668 "Toggle the type (<active> or [inactive]) of a time stamp."
11670 (when (org-at-timestamp-p t
)
11671 (let ((beg (match-beginning 0)) (end (match-end 0))
11672 (map '((?\
[ .
"<") (?\
] .
">") (?
< .
"[") (?
> .
"]"))))
11675 (while (re-search-forward "[][<>]" end t
)
11676 (replace-match (cdr (assoc (char-after (match-beginning 0)) map
))
11678 (message "Timestamp is now %sactive"
11679 (if (equal (char-after beg
) ?
<) "" "in")))))
11681 (defun org-timestamp-change (n &optional what
)
11682 "Change the date in the time stamp at point.
11683 The date will be changed by N times WHAT. WHAT can be `day', `month',
11684 `year', `minute', `second'. If WHAT is not given, the cursor position
11685 in the timestamp determines what will be changed."
11686 (let ((pos (point))
11688 (dm (max (nth 1 org-time-stamp-rounding-minutes
) 1))
11692 (if (not (org-at-timestamp-p t
))
11693 (error "Not at a timestamp"))
11694 (if (and (not what
) (eq org-ts-what
'bracket
))
11695 (org-toggle-timestamp-type)
11696 (if (and (not what
) (not (eq org-ts-what
'day
))
11697 org-display-custom-times
11698 (get-text-property (point) 'display
)
11699 (not (get-text-property (1- (point)) 'display
)))
11700 (setq org-ts-what
'day
))
11701 (setq org-ts-what
(or what org-ts-what
)
11702 inactive
(= (char-after (match-beginning 0)) ?\
[)
11703 ts
(match-string 0))
11706 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
11708 (setq extra
(match-string 1 ts
)))
11709 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts
)
11711 (setq time0
(org-parse-time-string ts
))
11712 (when (and (eq org-ts-what
'minute
)
11713 (eq current-prefix-arg nil
))
11714 (setq n
(* dm
(cond ((> n
0) 1) ((< n
0) -
1) (t 0))))
11715 (when (not (= 0 (setq rem
(%
(nth 1 time0
) dm
))))
11716 (setcar (cdr time0
) (+ (nth 1 time0
)
11717 (if (> n
0) (- rem
) (- dm rem
))))))
11719 (encode-time (or (car time0
) 0)
11720 (+ (if (eq org-ts-what
'minute
) n
0) (nth 1 time0
))
11721 (+ (if (eq org-ts-what
'hour
) n
0) (nth 2 time0
))
11722 (+ (if (eq org-ts-what
'day
) n
0) (nth 3 time0
))
11723 (+ (if (eq org-ts-what
'month
) n
0) (nth 4 time0
))
11724 (+ (if (eq org-ts-what
'year
) n
0) (nth 5 time0
))
11726 (when (integerp org-ts-what
)
11727 (setq extra
(org-modify-ts-extra extra org-ts-what n dm
)))
11728 (if (eq what
'calendar
)
11729 (let ((cal-date (org-get-date-from-calendar)))
11730 (setcar (nthcdr 4 time0
) (nth 0 cal-date
)) ; month
11731 (setcar (nthcdr 3 time0
) (nth 1 cal-date
)) ; day
11732 (setcar (nthcdr 5 time0
) (nth 2 cal-date
)) ; year
11733 (setcar time0
(or (car time0
) 0))
11734 (setcar (nthcdr 1 time0
) (or (nth 1 time0
) 0))
11735 (setcar (nthcdr 2 time0
) (or (nth 2 time0
) 0))
11736 (setq time
(apply 'encode-time time0
))))
11737 (setq org-last-changed-timestamp
11738 (org-insert-time-stamp time with-hm inactive nil nil extra
))
11739 (org-clock-update-time-maybe)
11741 ;; Try to recenter the calendar window, if any
11742 (if (and org-calendar-follow-timestamp-change
11743 (get-buffer-window "*Calendar*" t
)
11744 (memq org-ts-what
'(day month year
)))
11745 (org-recenter-calendar (time-to-days time
))))))
11747 (defun org-modify-ts-extra (s pos n dm
)
11748 "Change the different parts of the lead-time and repeat fields in timestamp."
11749 (let ((idx '(("d" .
0) ("w" .
1) ("m" .
2) ("y" .
3) ("d" . -
1) ("y" .
4)))
11751 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s
)
11753 ((or (org-pos-in-match-range pos
2)
11754 (org-pos-in-match-range pos
3))
11755 (setq m
(string-to-number (match-string 3 s
))
11756 h
(string-to-number (match-string 2 s
)))
11757 (if (org-pos-in-match-range pos
2)
11759 (setq n
(* dm
(org-no-warnings (signum n
))))
11760 (when (not (= 0 (setq rem
(% m dm
))))
11761 (setq m
(+ m
(if (> n
0) (- rem
) (- dm rem
)))))
11763 (if (< m
0) (setq m
(+ m
60) h
(1- h
)))
11764 (if (> m
59) (setq m
(- m
60) h
(1+ h
)))
11765 (setq h
(min 24 (max 0 h
)))
11766 (setq ng
1 new
(format "-%02d:%02d" h m
)))
11767 ((org-pos-in-match-range pos
6)
11768 (setq ng
6 new
(car (rassoc (+ n
(cdr (assoc (match-string 6 s
) idx
))) idx
))))
11769 ((org-pos-in-match-range pos
5)
11770 (setq ng
5 new
(format "%d" (max 1 (+ n
(string-to-number (match-string 5 s
)))))))
11772 ((org-pos-in-match-range pos
9)
11773 (setq ng
9 new
(car (rassoc (+ n
(cdr (assoc (match-string 9 s
) idx
))) idx
))))
11774 ((org-pos-in-match-range pos
8)
11775 (setq ng
8 new
(format "%d" (max 0 (+ n
(string-to-number (match-string 8 s
))))))))
11779 (substring s
0 (match-beginning ng
))
11781 (substring s
(match-end ng
))))))
11784 (defun org-recenter-calendar (date)
11785 "If the calendar is visible, recenter it to DATE."
11786 (let* ((win (selected-window))
11787 (cwin (get-buffer-window "*Calendar*" t
))
11788 (calendar-move-hook nil
))
11790 (select-window cwin
)
11791 (calendar-goto-date (if (listp date
) date
11792 (calendar-gregorian-from-absolute date
)))
11793 (select-window win
))))
11795 (defun org-goto-calendar (&optional arg
)
11796 "Go to the Emacs calendar at the current date.
11797 If there is a time stamp in the current line, go to that date.
11798 A prefix ARG can be used to force the current date."
11800 (let ((tsr org-ts-regexp
) diff
11801 (calendar-move-hook nil
)
11802 (calendar-view-holidays-initially-flag nil
)
11803 (view-calendar-holidays-initially nil
)
11804 (calendar-view-diary-initially-flag nil
)
11805 (view-diary-entries-initially nil
))
11806 (if (or (org-at-timestamp-p)
11808 (beginning-of-line 1)
11809 (looking-at (concat ".*" tsr
))))
11810 (let ((d1 (time-to-days (current-time)))
11812 (org-time-string-to-time (match-string 1)))))
11813 (setq diff
(- d2 d1
))))
11815 (calendar-goto-today)
11816 (if (and diff
(not arg
)) (calendar-forward-day diff
))))
11818 (defun org-get-date-from-calendar ()
11819 "Return a list (month day year) of date at point in calendar."
11820 (with-current-buffer "*Calendar*"
11822 (calendar-cursor-to-date))))
11824 (defun org-date-from-calendar ()
11825 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
11826 If there is already a time stamp at the cursor position, update it."
11828 (if (org-at-timestamp-p t
)
11829 (org-timestamp-change 0 'calendar
)
11830 (let ((cal-date (org-get-date-from-calendar)))
11831 (org-insert-time-stamp
11832 (encode-time 0 0 0 (nth 1 cal-date
) (car cal-date
) (nth 2 cal-date
))))))
11834 (defun org-minutes-to-hh:mm-string
(m)
11835 "Compute H:MM from a number of minutes."
11836 (let ((h (/ m
60)))
11837 (setq m
(- m
(* 60 h
)))
11838 (format org-time-clocksum-format h m
)))
11840 (defun org-hh:mm-string-to-minutes
(s)
11841 "Convert a string H:MM to a number of minutes."
11842 (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s
)
11843 (+ (* (string-to-number (match-string 1 s
)) 60)
11844 (string-to-number (match-string 2 s
)))
11850 (defun org-iswitchb (&optional arg
)
11851 "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
11852 With a prefix argument, restrict available to files.
11853 With two prefix arguments, restrict available buffers to agenda files.
11855 Due to some yet unresolved reason, the global function
11856 `iswitchb-mode' needs to be active for this function to work."
11858 (require 'iswitchb
)
11859 (let ((enabled iswitchb-mode
) blist
)
11860 (or enabled
(iswitchb-mode 1))
11861 (setq blist
(cond ((equal arg
'(4)) (org-buffer-list 'files
))
11862 ((equal arg
'(16)) (org-buffer-list 'agenda
))
11863 (t (org-buffer-list))))
11865 (let ((iswitchb-make-buflist-hook
11867 (setq iswitchb-temp-buflist
11868 (mapcar 'buffer-name blist
)))))
11870 (iswitchb-read-buffer
11871 "Switch-to: " nil t
))
11872 (or enabled
(iswitchb-mode -
1))))))
11875 (defun org-ido-switchb (&optional arg
)
11876 "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
11877 With a prefix argument, restrict available to files.
11878 With two prefix arguments, restrict available buffers to agenda files."
11880 (let ((blist (cond ((equal arg
'(4)) (org-buffer-list 'files
))
11881 ((equal arg
'(16)) (org-buffer-list 'agenda
))
11882 (t (org-buffer-list)))))
11884 (org-ido-completing-read "Org buffer: "
11885 (mapcar 'buffer-name blist
)
11888 (defun org-buffer-list (&optional predicate exclude-tmp
)
11889 "Return a list of Org buffers.
11890 PREDICATE can be `export', `files' or `agenda'.
11892 export restrict the list to Export buffers.
11893 files restrict the list to buffers visiting Org files.
11894 agenda restrict the list to buffers visiting agenda files.
11896 If EXCLUDE-TMP is non-nil, ignore temporary buffers."
11898 (agenda-files (and (eq predicate
'agenda
)
11899 (mapcar 'file-truename
(org-agenda-files t
))))
11902 ((eq predicate
'files
)
11903 (lambda (b) (with-current-buffer b
(eq major-mode
'org-mode
))))
11904 ((eq predicate
'export
)
11905 (lambda (b) (string-match "\*Org .*Export" (buffer-name b
))))
11906 ((eq predicate
'agenda
)
11908 (with-current-buffer b
11909 (and (eq major-mode
'org-mode
)
11910 (setq bfn
(buffer-file-name b
))
11911 (member (file-truename bfn
) agenda-files
)))))
11912 (t (lambda (b) (with-current-buffer b
11913 (or (eq major-mode
'org-mode
)
11914 (string-match "\*Org .*Export"
11915 (buffer-name b
)))))))))
11919 (if (and (funcall filter b
)
11920 (or (not exclude-tmp
)
11921 (not (string-match "tmp" (buffer-name b
)))))
11926 (defun org-agenda-files (&optional unrestricted archives
)
11927 "Get the list of agenda files.
11928 Optional UNRESTRICTED means return the full list even if a restriction
11929 is currently in place.
11930 When ARCHIVES is t, include all archive files hat are really being
11931 used by the agenda files. If ARCHIVE is `ifmode', do this only if
11932 `org-agenda-archives-mode' is t."
11935 ((and (not unrestricted
) (get 'org-agenda-files
'org-restrict
)))
11936 ((stringp org-agenda-files
) (org-read-agenda-file-list))
11937 ((listp org-agenda-files
) org-agenda-files
)
11938 (t (error "Invalid value of `org-agenda-files'")))))
11939 (setq files
(apply 'append
11940 (mapcar (lambda (f)
11941 (if (file-directory-p f
)
11943 f t org-agenda-file-regexp
)
11946 (when org-agenda-skip-unavailable-files
11947 (setq files
(delq nil
11950 (and (file-readable-p file
) file
)))
11952 (when (or (eq archives t
)
11953 (and (eq archives
'ifmode
) (eq org-agenda-archives-mode t
)))
11954 (setq files
(org-add-archive-files files
)))
11957 (defun org-edit-agenda-file-list ()
11958 "Edit the list of agenda files.
11959 Depending on setup, this either uses customize to edit the variable
11960 `org-agenda-files', or it visits the file that is holding the list. In the
11961 latter case, the buffer is set up in a way that saving it automatically kills
11962 the buffer and restores the previous window configuration."
11964 (if (stringp org-agenda-files
)
11965 (let ((cw (current-window-configuration)))
11966 (find-file org-agenda-files
)
11967 (org-set-local 'org-window-configuration cw
)
11968 (org-add-hook 'after-save-hook
11970 (set-window-configuration
11971 (prog1 org-window-configuration
11972 (kill-buffer (current-buffer))))
11973 (org-install-agenda-files-menu)
11974 (message "New agenda file list installed"))
11976 (message "%s" (substitute-command-keys
11977 "Edit list and finish with \\[save-buffer]")))
11978 (customize-variable 'org-agenda-files
)))
11980 (defun org-store-new-agenda-file-list (list)
11981 "Set new value for the agenda file list and save it correctly."
11982 (if (stringp org-agenda-files
)
11983 (let ((f org-agenda-files
) b
)
11984 (while (setq b
(find-buffer-visiting f
)) (kill-buffer b
))
11986 (insert (mapconcat 'identity list
"\n") "\n")))
11987 (let ((org-mode-hook nil
) (default-major-mode 'fundamental-mode
))
11988 (setq org-agenda-files list
)
11989 (customize-save-variable 'org-agenda-files org-agenda-files
))))
11991 (defun org-read-agenda-file-list ()
11992 "Read the list of agenda files from a file."
11993 (when (file-directory-p org-agenda-files
)
11994 (error "`org-agenda-files' cannot be a single directory"))
11995 (when (stringp org-agenda-files
)
11997 (insert-file-contents org-agenda-files
)
11998 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
12002 (defun org-cycle-agenda-files ()
12003 "Cycle through the files in `org-agenda-files'.
12004 If the current buffer visits an agenda file, find the next one in the list.
12005 If the current buffer does not, find the first agenda file."
12007 (let* ((fs (org-agenda-files t
))
12008 (files (append fs
(list (car fs
))))
12009 (tcf (if buffer-file-name
(file-truename buffer-file-name
)))
12011 (unless files
(error "No agenda files"))
12013 (while (setq file
(pop files
))
12014 (if (equal (file-truename file
) tcf
)
12016 (find-file (car files
))
12018 (find-file (car fs
)))
12019 (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
12021 (defun org-agenda-file-to-front (&optional to-end
)
12022 "Move/add the current file to the top of the agenda file list.
12023 If the file is not present in the list, it is added to the front. If it is
12024 present, it is moved there. With optional argument TO-END, add/move to the
12027 (let ((org-agenda-skip-unavailable-files nil
)
12028 (file-alist (mapcar (lambda (x)
12029 (cons (file-truename x
) x
))
12030 (org-agenda-files t
)))
12031 (ctf (file-truename buffer-file-name
))
12033 (setq x
(assoc ctf file-alist
) had x
)
12035 (if (not x
) (setq x
(cons ctf
(abbreviate-file-name buffer-file-name
))))
12037 (setq file-alist
(append (delq x file-alist
) (list x
)))
12038 (setq file-alist
(cons x
(delq x file-alist
))))
12039 (org-store-new-agenda-file-list (mapcar 'cdr file-alist
))
12040 (org-install-agenda-files-menu)
12041 (message "File %s to %s of agenda file list"
12042 (if had
"moved" "added") (if to-end
"end" "front"))))
12044 (defun org-remove-file (&optional file
)
12045 "Remove current file from the list of files in variable `org-agenda-files'.
12046 These are the files which are being checked for agenda entries.
12047 Optional argument FILE means, use this file instead of the current."
12049 (let* ((org-agenda-skip-unavailable-files nil
)
12050 (file (or file buffer-file-name
))
12051 (true-file (file-truename file
))
12052 (afile (abbreviate-file-name file
))
12053 (files (delq nil
(mapcar
12055 (if (equal true-file
12058 (org-agenda-files t
)))))
12059 (if (not (= (length files
) (length (org-agenda-files t
))))
12061 (org-store-new-agenda-file-list files
)
12062 (org-install-agenda-files-menu)
12063 (message "Removed file: %s" afile
))
12064 (message "File was not in list: %s (not removed)" afile
))))
12066 (defun org-file-menu-entry (file)
12067 (vector file
(list 'find-file file
) t
))
12069 (defun org-check-agenda-file (file)
12070 "Make sure FILE exists. If not, ask user what to do."
12071 (when (not (file-exists-p file
))
12072 (message "non-existent file %s. [R]emove from list or [A]bort?"
12073 (abbreviate-file-name file
))
12074 (let ((r (downcase (read-char-exclusive))))
12077 (org-remove-file file
)
12078 (throw 'nextfile t
))
12079 (t (error "Abort"))))))
12081 (defun org-get-agenda-file-buffer (file)
12082 "Get a buffer visiting FILE. If the buffer needs to be created, add
12083 it to the list of buffers which might be released later."
12084 (let ((buf (org-find-base-buffer-visiting file
)))
12086 buf
; just return it
12087 ;; Make a new buffer and remember it
12088 (setq buf
(find-file-noselect file
))
12089 (if buf
(push buf org-agenda-new-buffers
))
12092 (defun org-release-buffers (blist)
12093 "Release all buffers in list, asking the user for confirmation when needed.
12094 When a buffer is unmodified, it is just killed. When modified, it is saved
12095 \(if the user agrees) and then killed."
12097 (while (setq buf
(pop blist
))
12098 (setq file
(buffer-file-name buf
))
12099 (when (and (buffer-modified-p buf
)
12101 (y-or-n-p (format "Save file %s? " file
)))
12102 (with-current-buffer buf
(save-buffer)))
12103 (kill-buffer buf
))))
12105 (defun org-prepare-agenda-buffers (files)
12106 "Create buffers for all agenda files, protect archived trees and comments."
12108 (let ((pa '(:org-archived t
))
12109 (pc '(:org-comment t
))
12110 (pall '(:org-archived t
:org-comment t
))
12111 (inhibit-read-only t
)
12112 (rea (concat ":" org-archive-tag
":"))
12116 (while (setq file
(pop files
))
12119 (org-check-agenda-file file
)
12120 (set-buffer (org-get-agenda-file-buffer file
)))
12122 (setq bmp
(buffer-modified-p))
12123 (org-refresh-category-properties)
12124 (setq org-todo-keywords-for-agenda
12125 (append org-todo-keywords-for-agenda org-todo-keywords-1
))
12126 (setq org-done-keywords-for-agenda
12127 (append org-done-keywords-for-agenda org-done-keywords
))
12128 (setq org-todo-keyword-alist-for-agenda
12129 (append org-todo-keyword-alist-for-agenda org-todo-key-alist
))
12130 (setq org-tag-alist-for-agenda
12131 (append org-tag-alist-for-agenda org-tag-alist
))
12134 (remove-text-properties (point-min) (point-max) pall
)
12135 (when org-agenda-skip-archived-trees
12136 (goto-char (point-min))
12137 (while (re-search-forward rea nil t
)
12138 (if (org-on-heading-p t
)
12139 (add-text-properties (point-at-bol) (org-end-of-subtree t
) pa
))))
12140 (goto-char (point-min))
12141 (setq re
(concat "^\\*+ +" org-comment-string
"\\>"))
12142 (while (re-search-forward re nil t
)
12143 (add-text-properties
12144 (match-beginning 0) (org-end-of-subtree t
) pc
)))
12145 (set-buffer-modified-p bmp
))))
12146 (setq org-todo-keyword-alist-for-agenda
12147 (org-uniquify org-todo-keyword-alist-for-agenda
)
12148 org-tag-alist-for-agenda
(org-uniquify org-tag-alist-for-agenda
))))
12150 ;;;; Embedded LaTeX
12152 (defvar org-cdlatex-mode-map
(make-sparse-keymap)
12153 "Keymap for the minor `org-cdlatex-mode'.")
12155 (org-defkey org-cdlatex-mode-map
"_" 'org-cdlatex-underscore-caret
)
12156 (org-defkey org-cdlatex-mode-map
"^" 'org-cdlatex-underscore-caret
)
12157 (org-defkey org-cdlatex-mode-map
"`" 'cdlatex-math-symbol
)
12158 (org-defkey org-cdlatex-mode-map
"'" 'org-cdlatex-math-modify
)
12159 (org-defkey org-cdlatex-mode-map
"\C-c{" 'cdlatex-environment
)
12161 (defvar org-cdlatex-texmathp-advice-is-done nil
12162 "Flag remembering if we have applied the advice to texmathp already.")
12164 (define-minor-mode org-cdlatex-mode
12165 "Toggle the minor `org-cdlatex-mode'.
12166 This mode supports entering LaTeX environment and math in LaTeX fragments
12168 \\{org-cdlatex-mode-map}"
12170 (when org-cdlatex-mode
(require 'cdlatex
))
12171 (unless org-cdlatex-texmathp-advice-is-done
12172 (setq org-cdlatex-texmathp-advice-is-done t
)
12173 (defadvice texmathp
(around org-math-always-on activate
)
12174 "Always return t in org-mode buffers.
12175 This is because we want to insert math symbols without dollars even outside
12176 the LaTeX math segments. If Orgmode thinks that point is actually inside
12177 an embedded LaTeX fragment, let texmathp do its job.
12178 \\[org-cdlatex-mode-map]"
12182 ((not (org-mode-p)) ad-do-it
)
12183 ((eq this-command
'cdlatex-math-symbol
)
12184 (setq ad-return-value t
12185 texmathp-why
'("cdlatex-math-symbol in org-mode" .
0)))
12187 (let ((p (org-inside-LaTeX-fragment-p)))
12188 (if (and p
(member (car p
) (plist-get org-format-latex-options
:matchers
)))
12189 (setq ad-return-value t
12190 texmathp-why
'("Org-mode embedded math" .
0))
12191 (if p ad-do-it
)))))))))
12193 (defun turn-on-org-cdlatex ()
12194 "Unconditionally turn on `org-cdlatex-mode'."
12195 (org-cdlatex-mode 1))
12197 (defun org-inside-LaTeX-fragment-p ()
12198 "Test if point is inside a LaTeX fragment.
12199 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
12200 sequence appearing also before point.
12201 Even though the matchers for math are configurable, this function assumes
12202 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
12203 delimiters are skipped when they have been removed by customization.
12204 The return value is nil, or a cons cell with the delimiter and
12205 and the position of this delimiter.
12207 This function does a reasonably good job, but can locally be fooled by
12208 for example currency specifications. For example it will assume being in
12209 inline math after \"$22.34\". The LaTeX fragment formatter will only format
12210 fragments that are properly closed, but during editing, we have to live
12211 with the uncertainty caused by missing closing delimiters. This function
12212 looks only before point, not after."
12214 (let ((pos (point))
12215 (dodollar (member "$" (plist-get org-format-latex-options
:matchers
)))
12217 (re-search-backward (concat "^\\(" paragraph-start
"\\)") nil t
)
12219 dd-on str
(start 0) m re
)
12222 (setq str
(concat (buffer-substring lim
(point)) "\000 X$.")
12223 re
(nth 1 (assoc "$" org-latex-regexps
)))
12224 (while (string-match re str start
)
12226 ((= (match-end 0) (length str
))
12227 (throw 'exit
(cons "$" (+ lim
(match-beginning 0) 1))))
12228 ((= (match-end 0) (- (length str
) 5))
12230 (t (setq start
(match-end 0))))))
12231 (when (setq m
(re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t
))
12233 (and (match-beginning 1) (throw 'exit
(cons (match-string 1) m
)))
12234 (and (match-beginning 2) (throw 'exit nil
))
12236 (while (re-search-backward "\\$\\$" lim t
)
12237 (setq dd-on
(not dd-on
)))
12239 (if dd-on
(cons "$$" m
))))))
12242 (defun org-try-cdlatex-tab ()
12243 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
12244 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
12245 - inside a LaTeX fragment, or
12246 - after the first word in a line, where an abbreviation expansion could
12247 insert a LaTeX environment."
12248 (when org-cdlatex-mode
12251 (skip-chars-backward "a-zA-Z0-9*")
12252 (skip-chars-backward " \t")
12255 ((org-inside-LaTeX-fragment-p)
12259 (defun org-cdlatex-underscore-caret (&optional arg
)
12260 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
12261 Revert to the normal definition outside of these fragments."
12263 (if (org-inside-LaTeX-fragment-p)
12264 (call-interactively 'cdlatex-sub-superscript
)
12265 (let (org-cdlatex-mode)
12266 (call-interactively (key-binding (vector last-input-event
))))))
12268 (defun org-cdlatex-math-modify (&optional arg
)
12269 "Execute `cdlatex-math-modify' in LaTeX fragments.
12270 Revert to the normal definition outside of these fragments."
12272 (if (org-inside-LaTeX-fragment-p)
12273 (call-interactively 'cdlatex-math-modify
)
12274 (let (org-cdlatex-mode)
12275 (call-interactively (key-binding (vector last-input-event
))))))
12277 (defvar org-latex-fragment-image-overlays nil
12278 "List of overlays carrying the images of latex fragments.")
12279 (make-variable-buffer-local 'org-latex-fragment-image-overlays
)
12281 (defun org-remove-latex-fragment-image-overlays ()
12282 "Remove all overlays with LaTeX fragment images in current buffer."
12283 (mapc 'org-delete-overlay org-latex-fragment-image-overlays
)
12284 (setq org-latex-fragment-image-overlays nil
))
12286 (defun org-preview-latex-fragment (&optional subtree
)
12287 "Preview the LaTeX fragment at point, or all locally or globally.
12288 If the cursor is in a LaTeX fragment, create the image and overlay
12289 it over the source code. If there is no fragment at point, display
12290 all fragments in the current text, from one headline to the next. With
12291 prefix SUBTREE, display all fragments in the current subtree. With a
12292 double prefix `C-u C-u', or when the cursor is before the first headline,
12293 display all fragments in the buffer.
12294 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
12296 (org-remove-latex-fragment-image-overlays)
12299 (let (beg end at msg
)
12301 ((or (equal subtree
'(16))
12302 (not (save-excursion
12303 (re-search-backward (concat "^" outline-regexp
) nil t
))))
12304 (setq beg
(point-min) end
(point-max)
12305 msg
"Creating images for buffer...%s"))
12306 ((equal subtree
'(4))
12307 (org-back-to-heading)
12308 (setq beg
(point) end
(org-end-of-subtree t
)
12309 msg
"Creating images for subtree...%s"))
12311 (if (setq at
(org-inside-LaTeX-fragment-p))
12312 (goto-char (max (point-min) (- (cdr at
) 2)))
12313 (org-back-to-heading))
12314 (setq beg
(point) end
(progn (outline-next-heading) (point))
12315 msg
(if at
"Creating image...%s"
12316 "Creating images for entry...%s"))))
12318 (narrow-to-region beg end
)
12321 (concat "ltxpng/" (file-name-sans-extension
12322 (file-name-nondirectory
12323 buffer-file-name
)))
12324 default-directory
'overlays msg at
'forbuffer
)
12325 (message msg
"done. Use `C-c C-c' to remove images.")))))
12327 (defvar org-latex-regexps
12328 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t
)
12329 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
12330 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
12331 ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil
)
12332 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil
)
12333 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t
)
12334 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t
))
12335 "Regular expressions for matching embedded LaTeX.")
12337 (defun org-format-latex (prefix &optional dir overlays msg at forbuffer
)
12338 "Replace LaTeX fragments with links to an image, and produce images."
12339 (if (and overlays
(fboundp 'clear-image-cache
)) (clear-image-cache))
12340 (let* ((prefixnodir (file-name-nondirectory prefix
))
12341 (absprefix (expand-file-name prefix dir
))
12342 (todir (file-name-directory absprefix
))
12343 (opt org-format-latex-options
)
12344 (matchers (plist-get opt
:matchers
))
12345 (re-list org-latex-regexps
)
12346 (cnt 0) txt link beg end re e checkdir
12347 m n block linkfile movefile ov
)
12348 ;; Check if there are old images files with this prefix, and remove them
12349 (when (file-directory-p todir
)
12353 (concat (regexp-quote prefixnodir
) "_[0-9]+\\.png$"))))
12354 ;; Check the different regular expressions
12355 (while (setq e
(pop re-list
))
12356 (setq m
(car e
) re
(nth 1 e
) n
(nth 2 e
)
12357 block
(if (nth 3 e
) "\n\n" ""))
12358 (when (member m matchers
)
12359 (goto-char (point-min))
12360 (while (re-search-forward re nil t
)
12361 (when (or (not at
) (equal (cdr at
) (match-beginning n
)))
12362 (setq txt
(match-string n
)
12363 beg
(match-beginning n
) end
(match-end n
)
12365 linkfile
(format "%s_%04d.png" prefix cnt
)
12366 movefile
(format "%s_%04d.png" absprefix cnt
)
12367 link
(concat block
"[[file:" linkfile
"]]" block
))
12368 (if msg
(message msg cnt
))
12370 (unless checkdir
; make sure the directory exists
12372 (or (file-directory-p todir
) (make-directory todir
)))
12373 (org-create-formula-image
12374 txt movefile opt forbuffer
)
12377 (setq ov
(org-make-overlay beg end
))
12378 (if (featurep 'xemacs
)
12380 (org-overlay-put ov
'invisible t
)
12383 (make-glyph (vector 'png
:file movefile
))))
12386 (list 'image
:type
'png
:file movefile
:ascent
'center
)))
12387 (push ov org-latex-fragment-image-overlays
)
12389 (delete-region beg end
)
12390 (insert link
))))))))
12392 ;; This function borrows from Ganesh Swami's latex2png.el
12393 (defun org-create-formula-image (string tofile options buffer
)
12394 (let* ((tmpdir (if (featurep 'xemacs
)
12396 temporary-file-directory
))
12397 (texfilebase (make-temp-name
12398 (expand-file-name "orgtex" tmpdir
)))
12399 (texfile (concat texfilebase
".tex"))
12400 (dvifile (concat texfilebase
".dvi"))
12401 (pngfile (concat texfilebase
".png"))
12402 (fnh (if (featurep 'xemacs
)
12403 (font-height (get-face-font 'default
))
12404 (face-attribute 'default
:height nil
)))
12405 (scale (or (plist-get options
(if buffer
:scale
:html-scale
)) 1.0))
12406 (dpi (number-to-string (* scale
(floor (* 0.9 (if buffer fnh
140.
))))))
12407 (fg (or (plist-get options
(if buffer
:foreground
:html-foreground
))
12409 (bg (or (plist-get options
(if buffer
:background
:html-background
))
12411 (if (eq fg
'default
) (setq fg
(org-dvipng-color :foreground
)))
12412 (if (eq bg
'default
) (setq bg
(org-dvipng-color :background
)))
12413 (with-temp-file texfile
12414 (insert org-format-latex-header
12415 "\n\\begin{document}\n" string
"\n\\end{document}\n"))
12416 (let ((dir default-directory
))
12417 (condition-case nil
12420 (call-process "latex" nil nil nil texfile
))
12423 (if (not (file-exists-p dvifile
))
12424 (progn (message "Failed to create dvi file from %s" texfile
) nil
)
12425 (condition-case nil
12426 (call-process "dvipng" nil nil nil
12427 "-E" "-fg" fg
"-bg" bg
12429 ;;"-x" scale "-y" scale
12434 (if (not (file-exists-p pngfile
))
12435 (progn (message "Failed to create png file from %s" texfile
) nil
)
12436 ;; Use the requested file name and clean up
12437 (copy-file pngfile tofile
'replace
)
12438 (loop for e in
'(".dvi" ".tex" ".aux" ".log" ".png") do
12439 (delete-file (concat texfilebase e
)))
12442 (defun org-dvipng-color (attr)
12443 "Return an rgb color specification for dvipng."
12444 (apply 'format
"rgb %s %s %s"
12445 (mapcar 'org-normalize-color
12446 (color-values (face-attribute 'default attr nil
)))))
12448 (defun org-normalize-color (value)
12449 "Return string to be used as color value for an RGB component."
12450 (format "%g" (/ value
65535.0)))
12455 ;; Make `C-c C-x' a prefix key
12456 (org-defkey org-mode-map
"\C-c\C-x" (make-sparse-keymap))
12458 ;; TAB key with modifiers
12459 (org-defkey org-mode-map
"\C-i" 'org-cycle
)
12460 (org-defkey org-mode-map
[(tab)] 'org-cycle
)
12461 (org-defkey org-mode-map
[(control tab
)] 'org-force-cycle-archived
)
12462 (org-defkey org-mode-map
[(meta tab
)] 'org-complete
)
12463 (org-defkey org-mode-map
"\M-\t" 'org-complete
)
12464 (org-defkey org-mode-map
"\M-\C-i" 'org-complete
)
12465 ;; The following line is necessary under Suse GNU/Linux
12466 (unless (featurep 'xemacs
)
12467 (org-defkey org-mode-map
[S-iso-lefttab
] 'org-shifttab
))
12468 (org-defkey org-mode-map
[(shift tab
)] 'org-shifttab
)
12469 (define-key org-mode-map
[backtab] 'org-shifttab)
12471 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
12472 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
12473 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
12475 ;; Cursor keys with modifiers
12476 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
12477 (org-defkey org-mode-map [(meta right)] 'org-metaright)
12478 (org-defkey org-mode-map [(meta up)] 'org-metaup)
12479 (org-defkey org-mode-map [(meta down)] 'org-metadown)
12481 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
12482 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
12483 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
12484 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
12486 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
12487 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
12488 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
12489 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
12491 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
12492 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
12494 ;;; Extra keys for tty access.
12495 ;; We only set them when really needed because otherwise the
12496 ;; menus don't show the simple keys
12498 (when (or org-use-extra-keys
12499 (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
12500 (not window-system))
12501 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
12502 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
12503 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
12504 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
12505 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
12506 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
12507 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
12508 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
12509 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
12510 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
12511 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
12512 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
12513 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
12514 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
12515 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
12516 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
12517 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
12518 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
12519 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
12520 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
12521 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
12522 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
12524 ;; All the other keys
12526 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
12527 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
12528 (if (boundp 'narrow-map)
12529 (org-defkey narrow-map "s" 'org-narrow-to-subtree)
12530 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
12531 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
12532 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
12533 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
12534 (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
12535 (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
12536 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
12537 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
12538 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
12539 (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
12540 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
12541 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
12542 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
12543 (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
12544 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
12545 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
12546 (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
12547 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
12548 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
12549 (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
12550 (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
12551 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
12552 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
12553 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
12554 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
12555 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
12556 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
12557 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
12558 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
12559 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
12560 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
12561 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
12562 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
12563 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
12564 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
12565 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
12566 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
12567 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
12568 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
12569 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
12570 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
12571 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
12572 (org-defkey org-mode-map "\C-c^" 'org-sort)
12573 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
12574 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
12575 (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
12576 (org-defkey org-mode-map "\C-m" 'org-return)
12577 (org-defkey org-mode-map "\C-j" 'org-return-indent)
12578 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
12579 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
12580 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
12581 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
12582 (org-defkey org-mode-map "\C-c'" 'org-edit-special)
12583 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
12584 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
12585 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
12586 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
12587 (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
12588 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
12589 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
12590 (org-defkey org-mode-map "\C-c\C-e" 'org-export)
12591 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
12592 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
12594 (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
12595 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
12596 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
12597 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
12599 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
12600 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
12601 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
12602 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
12603 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
12604 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
12605 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
12606 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
12607 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
12608 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
12609 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
12610 (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
12612 (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
12613 (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
12614 (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
12616 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
12618 (when (featurep 'xemacs)
12619 (org-defkey org-mode-map 'button3 'popup-mode-menu))
12621 (defvar org-table-auto-blank-field) ; defined in org-table.el
12622 (defun org-self-insert-command (N)
12623 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
12624 If the cursor is in a table looking at whitespace, the whitespace is
12625 overwritten, and the table is not marked as requiring realignment."
12627 (if (and (org-table-p)
12629 ;; check if we blank the field, and if that triggers align
12630 (and (featurep 'org-table) org-table-auto-blank-field
12631 (member last-command
12632 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
12633 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
12634 ;; got extra space, this field does not determine column width
12635 (let (org-table-may-need-update) (org-table-blank-field))
12636 ;; no extra space, this field may determine column width
12637 (org-table-blank-field)))
12640 (looking-at "[^|\n]* |"))
12641 (let (org-table-may-need-update)
12642 (goto-char (1- (match-end 0)))
12643 (delete-backward-char 1)
12644 (goto-char (match-beginning 0))
12645 (self-insert-command N))
12646 (setq org-table-may-need-update t)
12647 (self-insert-command N)
12648 (org-fix-tags-on-the-fly)))
12650 (defun org-fix-tags-on-the-fly ()
12651 (when (and (equal (char-after (point-at-bol)) ?*)
12652 (org-on-heading-p))
12653 (org-align-tags-here org-tags-column)))
12655 (defun org-delete-backward-char (N)
12656 "Like `delete-backward-char', insert whitespace at field end in tables.
12657 When deleting backwards, in tables this function will insert whitespace in
12658 front of the next \"|\" separator, to keep the table aligned. The table will
12659 still be marked for re-alignment if the field did fill the entire column,
12660 because, in this case the deletion might narrow the column."
12662 (if (and (org-table-p)
12664 (string-match "|" (buffer-substring (point-at-bol) (point)))
12665 (looking-at ".*?|"))
12666 (let ((pos (point))
12667 (noalign (looking-at "[^|\n\r]* |"))
12668 (c org-table-may-need-update))
12669 (backward-delete-char N)
12670 (skip-chars-forward "^|")
12672 (goto-char (1- pos))
12673 ;; noalign: if there were two spaces at the end, this field
12674 ;; does not determine the width of the column.
12675 (if noalign (setq org-table-may-need-update c)))
12676 (backward-delete-char N)
12677 (org-fix-tags-on-the-fly)))
12679 (defun org-delete-char (N)
12680 "Like `delete-char', but insert whitespace at field end in tables.
12681 When deleting characters, in tables this function will insert whitespace in
12682 front of the next \"|\" separator, to keep the table aligned. The table will
12683 still be marked for re-alignment if the field did fill the entire column,
12684 because, in this case the deletion might narrow the column."
12686 (if (and (org-table-p)
12688 (not (= (char-after) ?|))
12690 (if (looking-at ".*?|")
12691 (let ((pos (point))
12692 (noalign (looking-at "[^|\n\r]* |"))
12693 (c org-table-may-need-update))
12694 (replace-match (concat
12695 (substring (match-string 0) 1 -1)
12698 ;; noalign: if there were two spaces at the end, this field
12699 ;; does not determine the width of the column.
12700 (if noalign (setq org-table-may-need-update c)))
12703 (org-fix-tags-on-the-fly)))
12705 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
12706 (put 'org-self-insert-command 'delete-selection t)
12707 (put 'orgtbl-self-insert-command 'delete-selection t)
12708 (put 'org-delete-char 'delete-selection 'supersede)
12709 (put 'org-delete-backward-char 'delete-selection 'supersede)
12711 ;; Make `flyspell-mode' delay after some commands
12712 (put 'org-self-insert-command 'flyspell-delayed t)
12713 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
12714 (put 'org-delete-char 'flyspell-delayed t)
12715 (put 'org-delete-backward-char 'flyspell-delayed t)
12717 ;; Make pabbrev-mode expand after org-mode commands
12718 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
12719 (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
12721 ;; How to do this: Measure non-white length of current string
12722 ;; If equal to column width, we should realign.
12724 (defun org-remap (map &rest commands)
12725 "In MAP, remap the functions given in COMMANDS.
12726 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
12729 (setq old (pop commands) new (pop commands))
12730 (if (fboundp 'command-remapping)
12731 (org-defkey map (vector 'remap old) new)
12732 (substitute-key-definition old new map global-map)))))
12734 (when (eq org-enable-table-editor 'optimized)
12735 ;; If the user wants maximum table support, we need to hijack
12736 ;; some standard editing functions
12737 (org-remap org-mode-map
12738 'self-insert-command 'org-self-insert-command
12739 'delete-char 'org-delete-char
12740 'delete-backward-char 'org-delete-backward-char)
12741 (org-defkey org-mode-map "|" 'org-force-self-insert))
12743 (defun org-shiftcursor-error ()
12744 "Throw an error because Shift-Cursor command was applied in wrong context."
12745 (error "This command is active in special context like tables, headlines or timestamps"))
12747 (defun org-shifttab (&optional arg)
12748 "Global visibility cycling or move to previous table field.
12749 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
12751 See the individual commands for more information."
12754 ((org-at-table-p) (call-interactively 'org-table-previous-field))
12756 (message "Content view to level: %d" arg)
12757 (org-content (prefix-numeric-value arg))
12758 (setq org-cycle-global-status 'overview))
12759 (t (call-interactively 'org-global-cycle))))
12761 (defun org-shiftmetaleft ()
12762 "Promote subtree or delete table column.
12763 Calls `org-promote-subtree', `org-outdent-item',
12764 or `org-table-delete-column', depending on context.
12765 See the individual commands for more information."
12768 ((org-at-table-p) (call-interactively 'org-table-delete-column))
12769 ((org-on-heading-p) (call-interactively 'org-promote-subtree))
12770 ((org-at-item-p) (call-interactively 'org-outdent-item))
12771 (t (org-shiftcursor-error))))
12773 (defun org-shiftmetaright ()
12774 "Demote subtree or insert table column.
12775 Calls `org-demote-subtree', `org-indent-item',
12776 or `org-table-insert-column', depending on context.
12777 See the individual commands for more information."
12780 ((org-at-table-p) (call-interactively 'org-table-insert-column))
12781 ((org-on-heading-p) (call-interactively 'org-demote-subtree))
12782 ((org-at-item-p) (call-interactively 'org-indent-item))
12783 (t (org-shiftcursor-error))))
12785 (defun org-shiftmetaup (&optional arg)
12786 "Move subtree up or kill table row.
12787 Calls `org-move-subtree-up' or `org-table-kill-row' or
12788 `org-move-item-up' depending on context. See the individual commands
12789 for more information."
12792 ((org-at-table-p) (call-interactively 'org-table-kill-row))
12793 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
12794 ((org-at-item-p) (call-interactively 'org-move-item-up))
12795 (t (org-shiftcursor-error))))
12796 (defun org-shiftmetadown (&optional arg)
12797 "Move subtree down or insert table row.
12798 Calls `org-move-subtree-down' or `org-table-insert-row' or
12799 `org-move-item-down', depending on context. See the individual
12800 commands for more information."
12803 ((org-at-table-p) (call-interactively 'org-table-insert-row))
12804 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
12805 ((org-at-item-p) (call-interactively 'org-move-item-down))
12806 (t (org-shiftcursor-error))))
12808 (defun org-metaleft (&optional arg)
12809 "Promote heading or move table column to left.
12810 Calls `org-do-promote' or `org-table-move-column', depending on context.
12811 With no specific context, calls the Emacs default `backward-word'.
12812 See the individual commands for more information."
12815 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
12816 ((or (org-on-heading-p) (org-region-active-p))
12817 (call-interactively 'org-do-promote))
12818 ((org-at-item-p) (call-interactively 'org-outdent-item))
12819 (t (call-interactively 'backward-word))))
12821 (defun org-metaright (&optional arg)
12822 "Demote subtree or move table column to right.
12823 Calls `org-do-demote' or `org-table-move-column', depending on context.
12824 With no specific context, calls the Emacs default `forward-word'.
12825 See the individual commands for more information."
12828 ((org-at-table-p) (call-interactively 'org-table-move-column))
12829 ((or (org-on-heading-p) (org-region-active-p))
12830 (call-interactively 'org-do-demote))
12831 ((org-at-item-p) (call-interactively 'org-indent-item))
12832 (t (call-interactively 'forward-word))))
12834 (defun org-metaup (&optional arg)
12835 "Move subtree up or move table row up.
12836 Calls `org-move-subtree-up' or `org-table-move-row' or
12837 `org-move-item-up', depending on context. See the individual commands
12838 for more information."
12841 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
12842 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
12843 ((org-at-item-p) (call-interactively 'org-move-item-up))
12844 (t (transpose-lines 1) (beginning-of-line -1))))
12846 (defun org-metadown (&optional arg)
12847 "Move subtree down or move table row down.
12848 Calls `org-move-subtree-down' or `org-table-move-row' or
12849 `org-move-item-down', depending on context. See the individual
12850 commands for more information."
12853 ((org-at-table-p) (call-interactively 'org-table-move-row))
12854 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
12855 ((org-at-item-p) (call-interactively 'org-move-item-down))
12856 (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
12858 (defun org-shiftup (&optional arg)
12859 "Increase item in timestamp or increase priority of current headline.
12860 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
12861 depending on context. See the individual commands for more information."
12864 ((org-at-timestamp-p t)
12865 (call-interactively (if org-edit-timestamp-down-means-later
12866 'org-timestamp-down 'org-timestamp-up)))
12867 ((org-on-heading-p) (call-interactively 'org-priority-up))
12868 ((org-at-item-p) (call-interactively 'org-previous-item))
12869 ((org-clocktable-try-shift 'up arg))
12870 (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
12872 (defun org-shiftdown (&optional arg)
12873 "Decrease item in timestamp or decrease priority of current headline.
12874 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
12875 depending on context. See the individual commands for more information."
12878 ((org-at-timestamp-p t)
12879 (call-interactively (if org-edit-timestamp-down-means-later
12880 'org-timestamp-up 'org-timestamp-down)))
12881 ((org-on-heading-p) (call-interactively 'org-priority-down))
12882 ((org-clocktable-try-shift 'down arg))
12883 (t (call-interactively 'org-next-item))))
12885 (defun org-shiftright (&optional arg)
12886 "Cycle the thing at point or in the current line, depending on context.
12887 Depending on context, this does one of the following:
12889 - switch a timestamp at point one day into the future
12890 - on a headline, switch to the next TODO keyword.
12891 - on an item, switch entire list to the next bullet type
12892 - on a property line, switch to the next allowed value
12893 - on a clocktable definition line, move time block into the future"
12896 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
12897 ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
12898 ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
12899 ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
12900 ((org-clocktable-try-shift 'right arg))
12901 (t (org-shiftcursor-error))))
12903 (defun org-shiftleft (&optional arg)
12904 "Cycle the thing at point or in the current line, depending on context.
12905 Depending on context, this does one of the following:
12907 - switch a timestamp at point one day into the past
12908 - on a headline, switch to the previous TODO keyword.
12909 - on an item, switch entire list to the previous bullet type
12910 - on a property line, switch to the previous allowed value
12911 - on a clocktable definition line, move time block into the past"
12914 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
12915 ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
12916 ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
12917 ((org-at-property-p)
12918 (call-interactively 'org-property-previous-allowed-value))
12919 ((org-clocktable-try-shift 'left arg))
12920 (t (org-shiftcursor-error))))
12922 (defun org-shiftcontrolright ()
12923 "Switch to next TODO set."
12926 ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
12927 (t (org-shiftcursor-error))))
12929 (defun org-shiftcontrolleft ()
12930 "Switch to previous TODO set."
12933 ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
12934 (t (org-shiftcursor-error))))
12936 (defun org-ctrl-c-ret ()
12937 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
12940 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
12941 (t (call-interactively 'org-insert-heading))))
12943 (defun org-copy-special ()
12944 "Copy region in table or copy current subtree.
12945 Calls `org-table-copy' or `org-copy-subtree', depending on context.
12946 See the individual commands for more information."
12948 (call-interactively
12949 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
12951 (defun org-cut-special ()
12952 "Cut region in table or cut current subtree.
12953 Calls `org-table-copy' or `org-cut-subtree', depending on context.
12954 See the individual commands for more information."
12956 (call-interactively
12957 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
12959 (defun org-paste-special (arg)
12960 "Paste rectangular region into table, or past subtree relative to level.
12961 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
12962 See the individual commands for more information."
12964 (if (org-at-table-p)
12965 (org-table-paste-rectangle)
12966 (org-paste-subtree arg)))
12968 (defun org-edit-special ()
12969 "Call a special editor for the stuff at point.
12970 When at a table, call the formula editor with `org-table-edit-formulas'.
12971 When at the first line of an src example, call `org-edit-src-code'.
12972 When in an #+include line, visit the include file. Otherwise call
12973 `ffap' to visit the file at point."
12977 (call-interactively 'org-table-edit-formulas))
12979 (beginning-of-line 1)
12980 (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
12981 (find-file (org-trim (match-string 1))))
12982 ((org-edit-src-code))
12983 ((org-edit-fixed-width-region))
12984 (t (call-interactively 'ffap))))
12986 (defun org-ctrl-c-ctrl-c (&optional arg)
12987 "Set tags in headline, or update according to changed information at point.
12989 This command does many different things, depending on context:
12991 - If the cursor is in a headline, prompt for tags and insert them
12992 into the current line, aligned to `org-tags-column'. When called
12993 with prefix arg, realign all tags in the current buffer.
12995 - If the cursor is in one of the special #+KEYWORD lines, this
12996 triggers scanning the buffer for these lines and updating the
12999 - If the cursor is inside a table, realign the table. This command
13000 works even if the automatic table editor has been turned off.
13002 - If the cursor is on a #+TBLFM line, re-apply the formulas to
13005 - If the cursor is a the beginning of a dynamic block, update it.
13007 - If the cursor is inside a table created by the table.el package,
13008 activate that table.
13010 - If the current buffer is a remember buffer, close note and file
13011 it. A prefix argument of 1 files to the default location
13012 without further interaction. A prefix argument of 2 files to
13013 the currently clocking task.
13015 - If the cursor is on a <<<target>>>, update radio targets and corresponding
13016 links in this buffer.
13018 - If the cursor is on a numbered item in a plain list, renumber the
13021 - If the cursor is on a checkbox, toggle it."
13023 (let ((org-enable-table-editor t))
13025 ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
13026 org-occur-highlights
13027 org-latex-fragment-image-overlays)
13028 (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
13029 (org-remove-occur-highlights)
13030 (org-remove-latex-fragment-image-overlays)
13031 (message "Temporary highlights/overlays removed from current buffer"))
13032 ((and (local-variable-p 'org-finish-function (current-buffer))
13033 (fboundp org-finish-function))
13034 (funcall org-finish-function))
13035 ((org-at-property-p)
13036 (call-interactively 'org-property-action))
13037 ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
13038 ((org-on-heading-p) (call-interactively 'org-set-tags))
13039 ((org-at-table.el-p)
13041 (beginning-of-line 1)
13042 (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
13043 (call-interactively 'table-recognize-table))
13045 (org-table-maybe-eval-formula)
13047 (call-interactively 'org-table-recalculate)
13048 (org-table-maybe-recalculate-line))
13049 (call-interactively 'org-table-align))
13050 ((org-at-item-checkbox-p)
13051 (call-interactively 'org-toggle-checkbox))
13053 (call-interactively 'org-maybe-renumber-ordered-list))
13054 ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
13056 (beginning-of-line 1)
13057 (save-excursion (org-update-dblock)))
13058 ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
13060 ((equal (match-string 1) "TBLFM")
13061 ;; Recalculate the table before this line
13063 (beginning-of-line 1)
13064 (skip-chars-backward " \r\n\t")
13065 (if (org-at-table-p)
13066 (org-call-with-arg 'org-table-recalculate t))))
13068 ; (org-set-regexps-and-options)
13069 ; (org-restart-font-lock)
13070 (let ((org-inhibit-startup t)) (org-mode-restart))
13071 (message "Local setup has been refreshed"))))
13072 (t (error "C-c C-c can do nothing useful at this location.")))))
13074 (defun org-mode-restart ()
13075 "Restart Org-mode, to scan again for special lines.
13076 Also updates the keyword regular expressions."
13079 (message "Org-mode restarted"))
13081 (defun org-kill-note-or-show-branches ()
13082 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
13084 (if (not org-finish-function)
13085 (call-interactively 'show-branches)
13086 (let ((org-note-abort t))
13087 (funcall org-finish-function))))
13089 (defun org-return (&optional indent)
13090 "Goto next table row or insert a newline.
13091 Calls `org-table-next-row' or `newline', depending on context.
13092 See the individual commands for more information."
13095 ((bobp) (if indent (newline-and-indent) (newline)))
13096 ((and (org-at-heading-p)
13098 (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
13103 (org-table-justify-field-maybe)
13104 (call-interactively 'org-table-next-row))
13105 (t (if indent (newline-and-indent) (newline)))))
13107 (defun org-return-indent ()
13108 "Goto next table row or insert a newline and indent.
13109 Calls `org-table-next-row' or `newline-and-indent', depending on
13110 context. See the individual commands for more information."
13114 (defun org-ctrl-c-star ()
13115 "Compute table, or change heading status of lines.
13116 Calls `org-table-recalculate' or `org-toggle-region-headings',
13117 depending on context. This will also turn a plain list item or a normal
13118 line into a subheading."
13122 (call-interactively 'org-table-recalculate))
13123 ((org-region-active-p)
13124 ;; Convert all lines in region to list items
13125 (call-interactively 'org-toggle-region-headings))
13126 ((org-on-heading-p)
13127 (org-toggle-region-headings (point-at-bol)
13128 (min (1+ (point-at-eol)) (point-max))))
13130 ;; Convert to heading
13131 (let ((level (save-match-data
13133 (condition-case nil
13135 (org-back-to-heading t)
13136 (funcall outline-level))
13139 (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
13140 (t (org-toggle-region-headings (point-at-bol)
13141 (min (1+ (point-at-eol)) (point-max))))))
13143 (defun org-ctrl-c-minus ()
13144 "Insert separator line in table or modify bullet status of line.
13145 Also turns a plain line or a region of lines into list items.
13146 Calls `org-table-insert-hline', `org-toggle-region-items', or
13147 `org-cycle-list-bullet', depending on context."
13151 (call-interactively 'org-table-insert-hline))
13152 ((org-on-heading-p)
13155 (beginning-of-line 1)
13156 (if (looking-at "\\*+ ")
13157 (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
13158 ((org-region-active-p)
13159 ;; Convert all lines in region to list items
13160 (call-interactively 'org-toggle-region-items))
13162 (call-interactively 'org-cycle-list-bullet))
13163 (t (org-toggle-region-items (point-at-bol)
13164 (min (1+ (point-at-eol)) (point-max))))))
13166 (defun org-toggle-region-items (beg end)
13167 "Convert all lines in region to list items.
13168 If the first line is already an item, convert all list items in the region
13174 (setq l2 (org-current-line))
13176 (beginning-of-line 1)
13177 (setq l (1- (org-current-line)))
13178 (if (org-at-item-p)
13179 ;; We already have items, de-itemize
13180 (while (< (setq l (1+ l)) l2)
13181 (when (org-at-item-p)
13182 (goto-char (match-beginning 2))
13183 (delete-region (match-beginning 2) (match-end 2))
13184 (and (looking-at "[ \t]+") (replace-match "")))
13185 (beginning-of-line 2))
13186 (while (< (setq l (1+ l)) l2)
13187 (unless (org-at-item-p)
13188 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
13189 (replace-match "\\1- \\2")))
13190 (beginning-of-line 2))))))
13192 (defun org-toggle-region-headings (beg end)
13193 "Convert all lines in region to list items.
13194 If the first line is already an item, convert all list items in the region
13200 (setq l2 (org-current-line))
13202 (beginning-of-line 1)
13203 (setq l (1- (org-current-line)))
13204 (if (org-on-heading-p)
13205 ;; We already have headlines, de-star them
13206 (while (< (setq l (1+ l)) l2)
13207 (when (org-on-heading-p t)
13208 (and (looking-at outline-regexp) (replace-match "")))
13209 (beginning-of-line 2))
13210 (let* ((stars (save-excursion
13211 (re-search-backward org-complex-heading-regexp nil t)
13212 (or (match-string 1) "*")))
13213 (add-stars (if org-odd-levels-only "**" "*"))
13214 (rpl (concat stars add-stars " \\2")))
13215 (while (< (setq l (1+ l)) l2)
13216 (unless (org-on-heading-p)
13217 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
13218 (replace-match rpl)))
13219 (beginning-of-line 2)))))))
13221 (defun org-meta-return (&optional arg)
13222 "Insert a new heading or wrap a region in a table.
13223 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
13224 See the individual commands for more information."
13228 (call-interactively 'org-table-wrap-region))
13229 (t (call-interactively 'org-insert-heading))))
13233 ;; Define the Org-mode menus
13234 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
13236 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
13237 ["Next Field" org-cycle (org-at-table-p)]
13238 ["Previous Field" org-shifttab (org-at-table-p)]
13239 ["Next Row" org-return (org-at-table-p)]
13241 ["Blank Field" org-table-blank-field (org-at-table-p)]
13242 ["Edit Field" org-table-edit-field (org-at-table-p)]
13243 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
13246 ["Move Column Left" org-metaleft (org-at-table-p)]
13247 ["Move Column Right" org-metaright (org-at-table-p)]
13248 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
13249 ["Insert Column" org-shiftmetaright (org-at-table-p)])
13251 ["Move Row Up" org-metaup (org-at-table-p)]
13252 ["Move Row Down" org-metadown (org-at-table-p)]
13253 ["Delete Row" org-shiftmetaup (org-at-table-p)]
13254 ["Insert Row" org-shiftmetadown (org-at-table-p)]
13255 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
13257 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
13259 ["Copy Rectangle" org-copy-special (org-at-table-p)]
13260 ["Cut Rectangle" org-cut-special (org-at-table-p)]
13261 ["Paste Rectangle" org-paste-special (org-at-table-p)]
13262 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
13265 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
13266 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
13267 ["Edit Formulas" org-edit-special (org-at-table-p)]
13269 ["Recalculate line" org-table-recalculate (org-at-table-p)]
13270 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
13271 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
13273 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
13275 ["Sum Column/Rectangle" org-table-sum
13276 (or (org-at-table-p) (org-region-active-p))]
13277 ["Which Column?" org-table-current-column (org-at-table-p)])
13279 org-table-toggle-formula-debugger
13280 :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
13281 ["Show Col/Row Numbers"
13282 org-table-toggle-coordinate-overlays
13284 :selected (org-bound-and-true-p org-table-overlay-coordinates)]
13286 ["Create" org-table-create (and (not (org-at-table-p))
13287 org-enable-table-editor)]
13288 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
13289 ["Import from File" org-table-import (not (org-at-table-p))]
13290 ["Export to File" org-table-export (org-at-table-p)]
13292 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
13294 (easy-menu-define org-org-menu org-mode-map "Org menu"
13297 ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
13298 ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
13299 ["Sparse Tree..." org-sparse-tree t]
13300 ["Reveal Context" org-reveal t]
13301 ["Show All" show-all t]
13303 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
13305 ["New Heading" org-insert-heading t]
13306 ("Navigate Headings"
13307 ["Up" outline-up-heading t]
13308 ["Next" outline-next-visible-heading t]
13309 ["Previous" outline-previous-visible-heading t]
13310 ["Next Same Level" outline-forward-same-level t]
13311 ["Previous Same Level" outline-backward-same-level t]
13313 ["Jump" org-goto t])
13315 ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
13316 ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
13318 ["Copy Subtree" org-copy-special (not (org-at-table-p))]
13319 ["Cut Subtree" org-cut-special (not (org-at-table-p))]
13320 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
13322 ["Promote Heading" org-metaleft (not (org-at-table-p))]
13323 ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
13324 ["Demote Heading" org-metaright (not (org-at-table-p))]
13325 ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
13327 ["Sort Region/Children" org-sort (not (org-at-table-p))]
13329 ["Convert to odd levels" org-convert-to-odd-levels t]
13330 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
13332 ["Emphasis..." org-emphasize t]
13333 ["Edit Source Example" org-edit-special t])
13335 ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
13336 ; ["Check and Tag Children" (org-toggle-archive-tag (4))
13337 ; :active t :keys "C-u C-c C-x C-a"]
13338 ["Sparse trees open ARCHIVE trees"
13339 (setq org-sparse-tree-open-archived-trees
13340 (not org-sparse-tree-open-archived-trees))
13341 :style toggle :selected org-sparse-tree-open-archived-trees]
13342 ["Cycling opens ARCHIVE trees"
13343 (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
13344 :style toggle :selected org-cycle-open-archived-trees]
13346 ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
13347 ["Move Subtree to Archive" org-advertized-archive-subtree t]
13348 ; ["Check and Move Children" (org-archive-subtree '(4))
13349 ; :active t :keys "C-u C-c C-x C-s"]
13353 ["TODO/DONE/-" org-todo t]
13355 ["Next keyword" org-shiftright (org-on-heading-p)]
13356 ["Previous keyword" org-shiftleft (org-on-heading-p)]
13357 ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
13358 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
13359 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
13360 ["Show TODO Tree" org-show-todo-tree t]
13361 ["Global TODO list" org-todo-list t]
13363 ["Set Priority" org-priority t]
13364 ["Priority Up" org-shiftup t]
13365 ["Priority Down" org-shiftdown t])
13366 ("TAGS and Properties"
13367 ["Set Tags" org-set-tags-command t]
13368 ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
13370 ["Set property" 'org-set-property t]
13371 ["Column view of properties" org-columns t]
13372 ["Insert Column View DBlock" org-insert-columns-dblock t])
13373 ("Dates and Scheduling"
13374 ["Timestamp" org-time-stamp t]
13375 ["Timestamp (inactive)" org-time-stamp-inactive t]
13377 ["1 Day Later" org-shiftright t]
13378 ["1 Day Earlier" org-shiftleft t]
13379 ["1 ... Later" org-shiftup t]
13380 ["1 ... Earlier" org-shiftdown t])
13381 ["Compute Time Range" org-evaluate-time-range t]
13382 ["Schedule Item" org-schedule t]
13383 ["Deadline" org-deadline t]
13385 ["Custom time format" org-toggle-time-stamp-overlays
13386 :style radio :selected org-display-custom-times]
13388 ["Goto Calendar" org-goto-calendar t]
13389 ["Date from Calendar" org-date-from-calendar t]
13391 ["Start/restart timer" org-timer-start t]
13392 ["Insert timer string" org-timer t]
13393 ["Insert timer item" org-timer-item t])
13395 ["Clock in" org-clock-in t]
13396 ["Clock out" org-clock-out t]
13397 ["Clock cancel" org-clock-cancel t]
13398 ["Goto running clock" org-clock-goto t]
13399 ["Display times" org-clock-display t]
13400 ["Create clock table" org-clock-report t]
13402 ["Record DONE time"
13403 (progn (setq org-log-done (not org-log-done))
13404 (message "Switching to %s will %s record a timestamp"
13405 (car org-done-keywords)
13406 (if org-log-done "automatically" "not")))
13407 :style toggle :selected org-log-done])
13409 ["Agenda Command..." org-agenda t]
13410 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
13411 ("File List for Agenda")
13412 ("Special views current file"
13413 ["TODO Tree" org-show-todo-tree t]
13414 ["Check Deadlines" org-check-deadlines t]
13415 ["Timeline" org-timeline t]
13416 ["Tags Tree" org-tags-sparse-tree t])
13419 ["Store Link (Global)" org-store-link t]
13420 ["Insert Link" org-insert-link t]
13421 ["Follow Link" org-open-at-point t]
13423 ["Next link" org-next-link t]
13424 ["Previous link" org-previous-link t]
13426 ["Descriptive Links"
13427 (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
13429 :selected (member '(org-link) buffer-invisibility-spec)]
13432 (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
13434 :selected (not (member '(org-link) buffer-invisibility-spec))])
13436 ["Export/Publish..." org-export t]
13438 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
13439 :selected org-cdlatex-mode]
13440 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
13441 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
13442 ["Modify math symbol" org-cdlatex-math-modify
13443 (org-inside-LaTeX-fragment-p)]
13444 ["Export LaTeX fragments as images"
13445 (if (featurep 'org-exp)
13446 (setq org-export-with-LaTeX-fragments
13447 (not org-export-with-LaTeX-fragments))
13448 (require 'org-exp))
13449 :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
13450 org-export-with-LaTeX-fragments)])
13453 ["Show Version" org-version t]
13454 ["Info Documentation" org-info t])
13456 ["Browse Org Group" org-customize t]
13458 ["Expand This Menu" org-create-customize-menu
13459 (fboundp 'customize-menu-create)])
13461 ["Refresh setup" org-mode-restart t]
13464 (defun org-info (&optional node)
13465 "Read documentation for Org-mode in the info system.
13466 With optional NODE, go directly to that node."
13468 (info (format "(org)%s" (or node ""))))
13470 (defun org-install-agenda-files-menu ()
13471 (let ((bl (buffer-list)))
13474 (set-buffer (pop bl))
13475 (if (org-mode-p) (setq bl nil)))
13478 '("Org") "File List for Agenda"
13481 ["Edit File List" (org-edit-agenda-file-list) t]
13482 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
13483 ["Remove Current File from List" org-remove-file t]
13484 ["Cycle through agenda files" org-cycle-agenda-files t]
13485 ["Occur in all agenda files" org-occur-in-agenda-files t]
13487 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
13492 (defun org-require-autoloaded-modules ()
13495 '(org-agenda org-archive org-clock org-colview
13496 org-exp org-id org-export-latex org-publish
13497 org-remember org-table)))
13500 (defun org-customize ()
13501 "Call the customize function with org as argument."
13503 (org-load-modules-maybe)
13504 (org-require-autoloaded-modules)
13505 (customize-browse 'org))
13507 (defun org-create-customize-menu ()
13508 "Create a full customization menu for Org-mode, insert it into the menu."
13510 (org-load-modules-maybe)
13511 (org-require-autoloaded-modules)
13512 (if (fboundp 'customize-menu-create)
13515 '("Org") "Customize"
13516 `(["Browse Org group" org-customize t]
13518 ,(customize-menu-create 'org)
13519 ["Set" Custom-set t]
13520 ["Save" Custom-save t]
13521 ["Reset to Current" Custom-reset-current t]
13522 ["Reset to Saved" Custom-reset-saved t]
13523 ["Reset to Standard Settings" Custom-reset-standard t]))
13524 (message "\"Org\"-menu now contains full customization menu"))
13525 (error "Cannot expand menu (outdated version of cus-edit.el)")))
13527 ;;;; Miscellaneous stuff
13529 ;;; Generally useful functions
13531 (defun org-find-text-property-in-string (prop s)
13532 "Return the first non-nil value of property PROP in string S."
13533 (or (get-text-property 0 prop s)
13534 (get-text-property (or (next-single-property-change 0 prop s) 0)
13537 (defun org-display-warning (message) ;; Copied from Emacs-Muse
13538 "Display the given MESSAGE as a warning."
13539 (if (fboundp 'display-warning)
13540 (display-warning 'org message
13541 (if (featurep 'xemacs)
13544 (let ((buf (get-buffer-create "*Org warnings*")))
13545 (with-current-buffer buf
13546 (goto-char (point-max))
13547 (insert "Warning (Org): " message)
13550 (display-buffer buf)
13553 (defun org-goto-marker-or-bmk (marker &optional bookmark)
13554 "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
13555 (if (and marker (marker-buffer marker)
13556 (buffer-live-p (marker-buffer marker)))
13558 (switch-to-buffer (marker-buffer marker))
13559 (if (or (> marker (point-max)) (< marker (point-min)))
13561 (goto-char marker))
13563 (bookmark-jump bookmark)
13564 (error "Cannot find location"))))
13566 (defun org-quote-csv-field (s)
13567 "Quote field for inclusion in CSV material."
13568 (if (string-match "[\",]" s)
13569 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
13572 (defun org-plist-delete (plist property)
13573 "Delete PROPERTY from PLIST.
13574 This is in contrast to merely setting it to 0."
13577 (if (not (eq property (car plist)))
13578 (setq p (plist-put p (car plist) (nth 1 plist))))
13579 (setq plist (cddr plist)))
13582 (defun org-force-self-insert (N)
13583 "Needed to enforce self-insert under remapping."
13585 (self-insert-command N))
13587 (defun org-string-width (s)
13588 "Compute width of string, ignoring invisible characters.
13589 This ignores character with invisibility property `org-link', and also
13590 characters with property `org-cwidth', because these will become invisible
13591 upon the next fontification round."
13593 (when (or (eq t buffer-invisibility-spec)
13594 (assq 'org-link buffer-invisibility-spec))
13595 (while (setq b (text-property-any 0 (length s)
13596 'invisible 'org-link s))
13597 (setq s (concat (substring s 0 b)
13598 (substring s (or (next-single-property-change
13599 b 'invisible s) (length s)))))))
13600 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
13601 (setq s (concat (substring s 0 b)
13602 (substring s (or (next-single-property-change
13603 b 'org-cwidth s) (length s))))))
13604 (setq l (string-width s) b -1)
13605 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
13606 (setq l (- l (get-text-property b 'org-dwidth-n s))))
13609 (defun org-get-indentation (&optional line)
13610 "Get the indentation of the current line, interpreting tabs.
13611 When LINE is given, assume it represents a line and compute its indentation."
13613 (if (string-match "^ *" (org-remove-tabs line))
13616 (beginning-of-line 1)
13617 (skip-chars-forward " \t")
13618 (current-column))))
13620 (defun org-remove-tabs (s &optional width)
13621 "Replace tabulators in S with spaces.
13622 Assumes that s is a single line, starting in column 0."
13623 (setq width (or width tab-width))
13624 (while (string-match "\t" s)
13625 (setq s (replace-match
13627 (- (* width (/ (+ (match-beginning 0) width) width))
13628 (match-beginning 0)) ?\ )
13632 (defun org-fix-indentation (line ind)
13633 "Fix indentation in LINE.
13634 IND is a cons cell with target and minimum indentation.
13635 If the current indentation in LINE is smaller than the minimum,
13636 leave it alone. If it is larger than ind, set it to the target."
13637 (let* ((l (org-remove-tabs line))
13638 (i (org-get-indentation l))
13639 (i1 (car ind)) (i2 (cdr ind)))
13640 (if (>= i i2) (setq l (substring line i2)))
13642 (concat (make-string i1 ?\ ) l)
13645 (defun org-base-buffer (buffer)
13646 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
13649 (or (buffer-base-buffer buffer)
13652 (defun org-trim (s)
13653 "Remove whitespace at beginning and end of string."
13654 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
13655 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
13658 (defun org-wrap (string &optional width lines)
13659 "Wrap string to either a number of lines, or a width in characters.
13660 If WIDTH is non-nil, the string is wrapped to that width, however many lines
13661 that costs. If there is a word longer than WIDTH, the text is actually
13662 wrapped to the length of that word.
13663 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
13664 many lines, whatever width that takes.
13665 The return value is a list of lines, without newlines at the end."
13666 (let* ((words (org-split-string string "[ \t\n]+"))
13667 (maxword (apply 'max (mapcar 'org-string-width words)))
13670 (org-do-wrap words (max maxword width)))
13673 (setq ll (org-do-wrap words maxword))
13674 (if (<= (length ll) lines)
13677 (while (> (length ll) lines)
13679 (setq ll (org-do-wrap words w)))
13681 (t (error "Cannot wrap this")))))
13683 (defun org-do-wrap (words width)
13684 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
13687 (setq line (pop words))
13688 (while (and words (< (+ (length line) (length (car words))) width))
13689 (setq line (concat line " " (pop words))))
13690 (setq lines (push line lines)))
13693 (defun org-split-string (string &optional separators)
13694 "Splits STRING into substrings at SEPARATORS.
13695 No empty strings are returned if there are matches at the beginning
13696 and end of string."
13697 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
13701 (while (and (string-match rexp string
13703 (= start (match-beginning 0))
13704 (< start (length string)))
13706 (< (match-beginning 0) (length string)))
13708 (or (eq (match-beginning 0) 0)
13709 (and (eq (match-beginning 0) (match-end 0))
13710 (eq (match-beginning 0) start))
13712 (cons (substring string start (match-beginning 0))
13714 (setq start (match-end 0)))
13715 (or (eq start (length string))
13717 (cons (substring string start)
13721 (defun org-context ()
13722 "Return a list of contexts of the current cursor position.
13723 If several contexts apply, all are returned.
13724 Each context entry is a list with a symbol naming the context, and
13725 two positions indicating start and end of the context. Possible
13728 :headline anywhere in a headline
13729 :headline-stars on the leading stars in a headline
13730 :todo-keyword on a TODO keyword (including DONE) in a headline
13731 :tags on the TAGS in a headline
13732 :priority on the priority cookie in a headline
13733 :item on the first line of a plain list item
13734 :item-bullet on the bullet/number of a plain list item
13735 :checkbox on the checkbox in a plain list item
13736 :table in an org-mode table
13737 :table-special on a special filed in a table
13738 :table-table in a table.el table
13739 :link on a hyperlink
13740 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
13741 :target on a <<target>>
13742 :radio-target on a <<<radio-target>>>
13743 :latex-fragment on a LaTeX fragment
13744 :latex-preview on a LaTeX fragment with overlayed preview image
13746 This function expects the position to be visible because it uses font-lock
13747 faces as a help to recognize the following contexts: :table-special, :link,
13749 (let* ((f (get-text-property (point) 'face))
13750 (faces (if (listp f) f (list f)))
13751 (p (point)) clist o)
13752 ;; First the large context
13754 ((org-on-heading-p t)
13755 (push (list :headline (point-at-bol) (point-at-eol)) clist)
13757 (beginning-of-line 1)
13758 (looking-at org-todo-line-tags-regexp))
13759 (push (org-point-in-group p 1 :headline-stars) clist)
13760 (push (org-point-in-group p 2 :todo-keyword) clist)
13761 (push (org-point-in-group p 4 :tags) clist))
13763 (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
13764 (if (looking-at "\\[#[A-Z0-9]\\]")
13765 (push (org-point-in-group p 0 :priority) clist)))
13768 (push (org-point-in-group p 2 :item-bullet) clist)
13769 (push (list :item (point-at-bol)
13770 (save-excursion (org-end-of-item) (point)))
13772 (and (org-at-item-checkbox-p)
13773 (push (org-point-in-group p 0 :checkbox) clist)))
13776 (push (list :table (org-table-begin) (org-table-end)) clist)
13777 (if (memq 'org-formula faces)
13778 (push (list :table-special
13779 (previous-single-property-change p 'face)
13780 (next-single-property-change p 'face)) clist)))
13781 ((org-at-table-p 'any)
13782 (push (list :table-table) clist)))
13785 ;; Now the small context
13787 ((org-at-timestamp-p)
13788 (push (org-point-in-group p 0 :timestamp) clist))
13789 ((memq 'org-link faces)
13791 (previous-single-property-change p 'face)
13792 (next-single-property-change p 'face)) clist))
13793 ((memq 'org-special-keyword faces)
13794 (push (list :keyword
13795 (previous-single-property-change p 'face)
13796 (next-single-property-change p 'face)) clist))
13798 (push (org-point-in-group p 0 :target) clist)
13799 (goto-char (1- (match-beginning 0)))
13800 (if (looking-at org-radio-target-regexp)
13801 (push (org-point-in-group p 0 :radio-target) clist))
13803 ((setq o (car (delq nil
13806 (if (memq x org-latex-fragment-image-overlays) x))
13807 (org-overlays-at (point))))))
13808 (push (list :latex-fragment
13809 (org-overlay-start o) (org-overlay-end o)) clist)
13810 (push (list :latex-preview
13811 (org-overlay-start o) (org-overlay-end o)) clist))
13812 ((org-inside-LaTeX-fragment-p)
13813 ;; FIXME: positions wrong.
13814 (push (list :latex-fragment (point) (point)) clist)))
13816 (setq clist (nreverse (delq nil clist)))
13819 ;; FIXME: Compare with at-regexp-p Do we need both?
13820 (defun org-in-regexp (re &optional nlines visually)
13821 "Check if point is inside a match of regexp.
13822 Normally only the current line is checked, but you can include NLINES extra
13823 lines both before and after point into the search.
13824 If VISUALLY is set, require that the cursor is not after the match but
13825 really on, so that the block visually is on the match."
13827 (let ((pos (point))
13828 (eol (point-at-eol (+ 1 (or nlines 0))))
13829 (inc (if visually 1 0)))
13831 (beginning-of-line (- 1 (or nlines 0)))
13832 (while (re-search-forward re eol t)
13833 (if (and (<= (match-beginning 0) pos)
13834 (>= (+ inc (match-end 0)) pos))
13835 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
13837 (defun org-at-regexp-p (regexp)
13838 "Is point inside a match of REGEXP in the current line?"
13841 (let ((pos (point)) (end (point-at-eol)))
13842 (beginning-of-line 1)
13843 (while (re-search-forward regexp end t)
13844 (if (and (<= (match-beginning 0) pos)
13845 (>= (match-end 0) pos))
13849 (defun org-occur-in-agenda-files (regexp &optional nlines)
13850 "Call `multi-occur' with buffers for all agenda files."
13851 (interactive "sOrg-files matching: \np")
13852 (let* ((files (org-agenda-files))
13853 (tnames (mapcar 'file-truename files))
13854 (extra org-agenda-text-search-extra-files)
13856 (when (eq (car extra) 'agenda-archives)
13857 (setq extra (cdr extra))
13858 (setq files (org-add-archive-files files)))
13859 (while (setq f (pop extra))
13860 (unless (member (file-truename f) tnames)
13861 (add-to-list 'files f 'append)
13862 (add-to-list 'tnames (file-truename f) 'append)))
13864 (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
13867 (if (boundp 'occur-mode-find-occurrence-hook)
13869 (add-hook 'occur-mode-find-occurrence-hook
13874 (defadvice occur-mode-goto-occurrence
13875 (after org-occur-reveal activate)
13876 (and (org-mode-p) (org-reveal)))
13877 (defadvice occur-mode-goto-occurrence-other-window
13878 (after org-occur-reveal activate)
13879 (and (org-mode-p) (org-reveal)))
13880 (defadvice occur-mode-display-occurrence
13881 (after org-occur-reveal activate)
13883 (let ((pos (occur-mode-find-occurrence)))
13884 (with-current-buffer (marker-buffer pos)
13889 (defun org-uniquify (list)
13890 "Remove duplicate elements from LIST."
13892 (mapc (lambda (x) (add-to-list 'res x 'append)) list)
13895 (defun org-delete-all (elts list)
13896 "Remove all elements in ELTS from LIST."
13898 (setq list (delete (pop elts) list)))
13901 (defun org-back-over-empty-lines ()
13902 "Move backwards over whitespace, to the beginning of the first empty line.
13903 Returns the number of empty lines passed."
13904 (let ((pos (point)))
13905 (skip-chars-backward " \t\n\r")
13906 (beginning-of-line 2)
13907 (goto-char (min (point) pos))
13908 (count-lines (point) pos)))
13910 (defun org-skip-whitespace ()
13911 (skip-chars-forward " \t\n\r"))
13913 (defun org-point-in-group (point group &optional context)
13914 "Check if POINT is in match-group GROUP.
13915 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
13916 match. If the match group does ot exist or point is not inside it,
13918 (and (match-beginning group)
13919 (>= point (match-beginning group))
13920 (<= point (match-end group))
13922 (list context (match-beginning group) (match-end group))
13925 (defun org-switch-to-buffer-other-window (&rest args)
13926 "Switch to buffer in a second window on the current frame.
13927 In particular, do not allow pop-up frames."
13928 (let (pop-up-frames special-display-buffer-names special-display-regexps
13929 special-display-function)
13930 (apply 'switch-to-buffer-other-window args)))
13932 (defun org-combine-plists (&rest plists)
13933 "Create a single property list from all plists in PLISTS.
13934 The process starts by copying the first list, and then setting properties
13935 from the other lists. Settings in the last list are the most significant
13936 ones and overrule settings in the other lists."
13937 (let ((rtn (copy-sequence (pop plists)))
13940 (setq ls (pop plists))
13942 (setq p (pop ls) v (pop ls))
13943 (setq rtn (plist-put rtn p v))))
13946 (defun org-move-line-down (arg)
13947 "Move the current line down. With prefix argument, move it past ARG lines."
13949 (let ((col (current-column))
13951 (beginning-of-line 1) (setq beg (point))
13952 (beginning-of-line 2) (setq end (point))
13953 (beginning-of-line (+ 1 arg))
13954 (setq pos (move-marker (make-marker) (point)))
13955 (insert (delete-and-extract-region beg end))
13957 (org-move-to-column col)))
13959 (defun org-move-line-up (arg)
13960 "Move the current line up. With prefix argument, move it past ARG lines."
13962 (let ((col (current-column))
13964 (beginning-of-line 1) (setq beg (point))
13965 (beginning-of-line 2) (setq end (point))
13966 (beginning-of-line (- arg))
13967 (setq pos (move-marker (make-marker) (point)))
13968 (insert (delete-and-extract-region beg end))
13970 (org-move-to-column col)))
13972 (defun org-replace-escapes (string table)
13973 "Replace %-escapes in STRING with values in TABLE.
13974 TABLE is an association list with keys like \"%a\" and string values.
13975 The sequences in STRING may contain normal field width and padding information,
13976 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
13977 so values can contain further %-escapes if they are define later in TABLE."
13978 (let ((case-fold-search nil)
13980 (while (setq e (pop table))
13981 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
13982 (while (string-match re string)
13983 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
13985 (setq string (replace-match rpl t t string))))
13989 (defun org-sublist (list start end)
13990 "Return a section of LIST, from START to END.
13991 Counting starts at 1."
13992 (let (rtn (c start))
13993 (setq list (nthcdr (1- start) list))
13994 (while (and list (<= c end))
13995 (push (pop list) rtn)
13999 (defun org-find-base-buffer-visiting (file)
14000 "Like `find-buffer-visiting' but alway return the base buffer and
14001 not an indirect buffer."
14002 (let ((buf (find-buffer-visiting file)))
14004 (or (buffer-base-buffer buf) buf)
14007 (defun org-image-file-name-regexp ()
14008 "Return regexp matching the file names of images."
14009 (if (fboundp 'image-file-name-regexp)
14010 (image-file-name-regexp)
14011 (let ((image-file-name-extensions
14012 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
14013 "xbm" "xpm" "pbm" "pgm" "ppm")))
14015 (regexp-opt (nconc (mapcar 'upcase
14016 image-file-name-extensions)
14017 image-file-name-extensions)
14021 (defun org-file-image-p (file)
14022 "Return non-nil if FILE is an image."
14024 (string-match (org-image-file-name-regexp) file)))
14026 (defun org-get-cursor-date ()
14027 "Return the date at cursor in as a time.
14028 This works in the calendar and in the agenda, anywhere else it just
14029 returns the current time."
14030 (let (date day defd)
14032 ((eq major-mode 'calendar-mode)
14033 (setq date (calendar-cursor-to-date)
14034 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
14035 ((eq major-mode 'org-agenda-mode)
14036 (setq day (get-text-property (point) 'day))
14038 (setq date (calendar-gregorian-from-absolute day)
14039 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
14041 (or defd (current-time))))
14043 (defvar org-agenda-action-marker (make-marker)
14044 "Marker pointing to the entry for the next agenda action.")
14046 (defun org-mark-entry-for-agenda-action ()
14047 "Mark the current entry as target of an agenda action.
14048 Agenda actions are actions executed from the agenda with the key `k',
14049 which make use of the date at the cursor."
14051 (move-marker org-agenda-action-marker
14052 (save-excursion (org-back-to-heading t) (point))
14055 "Entry marked for action; press `k' at desired date in agenda or calendar"))
14057 ;;; Paragraph filling stuff.
14058 ;; We want this to be just right, so use the full arsenal.
14060 (defun org-indent-line-function ()
14061 "Indent line like previous, but further if previous was headline or item."
14063 (let* ((pos (point))
14064 (itemp (org-at-item-p))
14065 column bpos bcol tpos tcol bullet btype bullet-type)
14066 ;; Find the previous relevant line
14067 (beginning-of-line 1)
14069 ((looking-at "#") (setq column 0))
14070 ((looking-at "\\*+ ") (setq column 0))
14072 (beginning-of-line 0)
14073 (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
14074 (beginning-of-line 0))
14076 ((looking-at "\\*+[ \t]+")
14077 (if (not org-adapt-indentation)
14079 (goto-char (match-end 0))
14080 (setq column (current-column))))
14082 (org-beginning-of-item)
14083 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
14084 (setq bpos (match-beginning 1) tpos (match-end 0)
14085 bcol (progn (goto-char bpos) (current-column))
14086 tcol (progn (goto-char tpos) (current-column))
14087 bullet (match-string 1)
14088 bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
14089 (if (> tcol (+ bcol org-description-max-indent))
14090 (setq tcol (+ bcol 5)))
14094 (beginning-of-line 1)
14095 (if (looking-at "\\S-")
14097 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
14098 (setq bullet (match-string 1)
14099 btype (if (string-match "[0-9]" bullet) "n" bullet))
14100 (setq column (if (equal btype bullet-type) bcol tcol)))
14101 (setq column (org-get-indentation)))))
14102 (t (setq column (org-get-indentation))))))
14104 (if (<= (current-column) (current-indentation))
14105 (org-indent-line-to column)
14106 (save-excursion (org-indent-line-to column)))
14107 (setq column (current-column))
14108 (beginning-of-line 1)
14110 "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
14111 (replace-match (concat "\\1" (format org-property-format
14112 (match-string 2) (match-string 3)))
14114 (org-move-to-column column)))
14116 (defun org-set-autofill-regexps ()
14118 ;; In the paragraph separator we include headlines, because filling
14119 ;; text in a line directly attached to a headline would otherwise
14120 ;; fill the headline as well.
14121 (org-set-local 'comment-start-skip "^#+[ \t]*")
14122 (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
14123 ;; The paragraph starter includes hand-formatted lists.
14124 (org-set-local 'paragraph-start
14125 "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
14126 ;; Inhibit auto-fill for headers, tables and fixed-width lines.
14127 ;; But only if the user has not turned off tables or fixed-width regions
14129 'auto-fill-inhibit-regexp
14130 (concat "\\*+ \\|#\\+"
14131 "\\|[ \t]*" org-keyword-time-regexp
14132 (if (or org-enable-table-editor org-enable-fixed-width-editor)
14135 (if org-enable-table-editor "|" "")
14136 (if org-enable-fixed-width-editor ":" "")
14138 ;; We use our own fill-paragraph function, to make sure that tables
14139 ;; and fixed-width regions are not wrapped. That function will pass
14140 ;; through to `fill-paragraph' when appropriate.
14141 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
14142 ; Adaptive filling: To get full control, first make sure that
14143 ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
14144 (org-set-local 'adaptive-fill-regexp "\000")
14145 (org-set-local 'adaptive-fill-function
14146 'org-adaptive-fill-function)
14148 'align-mode-rules-list
14149 '((org-in-buffer-settings
14150 (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
14151 (modes . '(org-mode))))))
14153 (defun org-fill-paragraph (&optional justify)
14154 "Re-align a table, pass through to fill-paragraph if no table."
14155 (let ((table-p (org-at-table-p))
14156 (table.el-p (org-at-table.el-p)))
14157 (cond ((and (equal (char-after (point-at-bol)) ?*)
14158 (save-excursion (goto-char (point-at-bol))
14159 (looking-at outline-regexp)))
14160 t) ; skip headlines
14161 (table.el-p t) ; skip table.el tables
14162 (table-p (org-table-align) t) ; align org-mode tables
14163 (t nil)))) ; call paragraph-fill
14165 ;; For reference, this is the default value of adaptive-fill-regexp
14166 ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
14168 (defun org-adaptive-fill-function ()
14169 "Return a fill prefix for org-mode files.
14170 In particular, this makes sure hanging paragraphs for hand-formatted lists
14172 (cond ((looking-at "#[ \t]+")
14174 ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
14176 (if (> (match-end 1) (+ (match-beginning 1)
14177 org-description-max-indent))
14178 (goto-char (+ (match-beginning 1) 5))
14179 (goto-char (match-end 0)))
14180 (make-string (current-column) ?\ )))
14181 ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
14183 (goto-char (match-end 0))
14184 (make-string (current-column) ?\ )))
14189 (defun org-toggle-fixed-width-section (arg)
14190 "Toggle the fixed-width export.
14191 If there is no active region, the QUOTE keyword at the current headline is
14192 inserted or removed. When present, it causes the text between this headline
14193 and the next to be exported as fixed-width text, and unmodified.
14194 If there is an active region, this command adds or removes a colon as the
14195 first character of this line. If the first character of a line is a colon,
14196 this line is also exported in fixed-width font."
14199 (regionp (org-region-active-p))
14200 (beg (if regionp (region-beginning) (point)))
14201 (end (if regionp (region-end)))
14202 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
14203 (case-fold-search nil)
14204 (re "[ \t]*\\(:\\)")
14209 (setq cc (current-column))
14210 (beginning-of-line 1)
14211 (setq off (looking-at re))
14212 (while (> nlines 0)
14213 (setq nlines (1- nlines))
14214 (beginning-of-line 1)
14217 (org-move-to-column cc t)
14220 ((and off (looking-at re))
14221 (replace-match "" t t nil 1))
14222 ((not off) (org-move-to-column cc t) (insert ":")))
14225 (org-back-to-heading)
14226 (if (looking-at (concat outline-regexp
14227 "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
14228 (replace-match "" t t nil 1)
14229 (if (looking-at outline-regexp)
14231 (goto-char (match-end 0))
14232 (insert org-quote-string " "))))))))
14234 ;;;; Functions extending outline functionality
14236 (defun org-beginning-of-line (&optional arg)
14237 "Go to the beginning of the current line. If that is invisible, continue
14238 to a visible line beginning. This makes the function of C-a more intuitive.
14239 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
14240 first attempt, and only move to after the tags when the cursor is already
14241 beyond the end of the headline."
14243 (let ((pos (point)) refpos)
14244 (beginning-of-line 1)
14248 (if (org-invisible-p)
14249 (while (and (not (bobp)) (org-invisible-p))
14251 (beginning-of-line 1))
14253 (when org-special-ctrl-a/e
14255 ((and (looking-at org-complex-heading-regexp)
14256 (= (char-after (match-end 1)) ?\ ))
14257 (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
14260 (if (eq org-special-ctrl-a/e t)
14261 (cond ((> pos refpos) refpos)
14262 ((= pos (point)) refpos)
14264 (cond ((> pos (point)) (point))
14265 ((not (eq last-command this-command)) (point))
14269 (if (eq org-special-ctrl-a/e t)
14270 (cond ((> pos (match-end 4)) (match-end 4))
14271 ((= pos (point)) (match-end 4))
14273 (cond ((> pos (point)) (point))
14274 ((not (eq last-command this-command)) (point))
14275 (t (match-end 4))))))))
14277 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
14279 (defun org-end-of-line (&optional arg)
14280 "Go to the end of the line.
14281 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
14282 first attempt, and only move to after the tags when the cursor is already
14283 beyond the end of the headline."
14285 (if (or (not org-special-ctrl-a/e)
14286 (not (org-on-heading-p)))
14288 (let ((pos (point)))
14289 (beginning-of-line 1)
14290 (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
14291 (if (eq org-special-ctrl-a/e t)
14292 (if (or (< pos (match-beginning 1))
14293 (= pos (match-end 0)))
14294 (goto-char (match-beginning 1))
14295 (goto-char (match-end 0)))
14296 (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
14297 (goto-char (match-end 0))
14298 (goto-char (match-beginning 1))))
14299 (end-of-line arg))))
14301 (and (featurep 'xemacs) (setq zmacs-region-stays t))))
14304 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
14305 (define-key org-mode-map "\C-e" 'org-end-of-line)
14307 (defun org-kill-line (&optional arg)
14308 "Kill line, to tags or end of line."
14311 ((or (not org-special-ctrl-k)
14313 (not (org-on-heading-p)))
14314 (call-interactively 'kill-line))
14315 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
14316 (kill-region (point) (match-beginning 1))
14317 (org-set-tags nil t))
14318 (t (kill-region (point) (point-at-eol)))))
14320 (define-key org-mode-map "\C-k" 'org-kill-line)
14322 (defun org-yank (&optional arg)
14323 "Yank. If the kill is a subtree, treat it specially.
14324 This command will look at the current kill and check if is a single
14325 subtree, or a series of subtrees[1]. If it passes the test, and if the
14326 cursor is at the beginning of a line or after the stars of a currently
14327 empty headline, then the yank is handled specially. How exactly depends
14328 on the value of the following variables, both set by default.
14330 org-yank-folded-subtrees
14331 When set, the subtree(s) will be folded after insertion, but only
14332 if doing so would now swallow text after the yanked text.
14334 org-yank-adjusted-subtrees
14335 When set, the subtree will be promoted or demoted in order to
14336 fit into the local outline tree structure, which means that the level
14337 will be adjusted so that it becomes the smaller one of the two
14338 *visible* surrounding headings.
14340 Any prefix to this command will cause `yank' to be called directly with
14341 no special treatment. In particular, a simple `C-u' prefix will just
14342 plainly yank the text as it is.
14344 \[1] Basically, the test checks if the first non-white line is a heading
14345 and if there are no other headings with fewer stars."
14347 (setq this-command 'yank)
14349 (call-interactively 'yank)
14350 (let ((subtreep ; is kill a subtree, and the yank position appropriate?
14351 (and (org-kill-is-subtree-p)
14353 (and (looking-at "[ \t]*$")
14356 (buffer-substring (point-at-bol) (point)))))))
14359 ((and subtreep org-yank-folded-subtrees)
14360 (let ((beg (point))
14362 (if (and subtreep org-yank-adjusted-subtrees)
14363 (org-paste-subtree nil nil 'for-yank)
14364 (call-interactively 'yank))
14367 (when (and (bolp) subtreep
14368 (not (setq swallowp
14369 (org-yank-folding-would-swallow-text beg end))))
14370 (or (looking-at outline-regexp)
14371 (re-search-forward (concat "^" outline-regexp) end t))
14372 (while (and (< (point) end) (looking-at outline-regexp))
14374 (org-cycle-show-empty-lines 'folded)
14375 (condition-case nil
14376 (outline-forward-same-level 1)
14377 (error (goto-char end)))))
14380 "Yanked text not folded because that would swallow text"))
14382 (skip-chars-forward " \t\n\r")
14383 (beginning-of-line 1)
14384 (push-mark beg 'nomsg)))
14385 ((and subtreep org-yank-adjusted-subtrees)
14386 (let ((beg (point-at-bol)))
14387 (org-paste-subtree nil nil 'for-yank)
14388 (push-mark beg 'nomsg)))
14390 (call-interactively 'yank))))))
14392 (defun org-yank-folding-would-swallow-text (beg end)
14393 "Would hide-subtree at BEG swallow any text after END?"
14397 (when (or (looking-at outline-regexp)
14398 (re-search-forward (concat "^" outline-regexp) end t))
14399 (setq level (org-outline-level)))
14401 (skip-chars-forward " \t\r\n\v\f")
14403 (and (bolp) (looking-at org-outline-regexp)
14404 (<= (org-outline-level) level)))
14405 nil ; Nothing would be swallowed
14406 t)))) ; something would swallow
14408 (define-key org-mode-map "\C-y" 'org-yank)
14410 (defun org-invisible-p ()
14411 "Check if point is at a character currently not visible."
14412 ;; Early versions of noutline don't have `outline-invisible-p'.
14413 (if (fboundp 'outline-invisible-p)
14414 (outline-invisible-p)
14415 (get-char-property (point) 'invisible)))
14417 (defun org-invisible-p2 ()
14418 "Check if point is at a character currently not visible."
14420 (if (and (eolp) (not (bobp))) (backward-char 1))
14421 ;; Early versions of noutline don't have `outline-invisible-p'.
14422 (if (fboundp 'outline-invisible-p)
14423 (outline-invisible-p)
14424 (get-char-property (point) 'invisible))))
14426 (defun org-back-to-heading (&optional invisible-ok)
14427 "Call `outline-back-to-heading', but provide a better error message."
14428 (condition-case nil
14429 (outline-back-to-heading invisible-ok)
14430 (error (error "Before first headline at position %d in buffer %s"
14431 (point) (current-buffer)))))
14433 (defun org-before-first-heading-p ()
14434 "Before first heading?"
14436 (null (re-search-backward "^\\*+ " nil t))))
14438 (defalias 'org-on-heading-p 'outline-on-heading-p)
14439 (defalias 'org-at-heading-p 'outline-on-heading-p)
14440 (defun org-at-heading-or-item-p ()
14441 (or (org-on-heading-p) (org-at-item-p)))
14443 (defun org-on-target-p ()
14444 (or (org-in-regexp org-radio-target-regexp)
14445 (org-in-regexp org-target-regexp)))
14447 (defun org-up-heading-all (arg)
14448 "Move to the heading line of which the present line is a subheading.
14449 This function considers both visible and invisible heading lines.
14450 With argument, move up ARG levels."
14451 (if (fboundp 'outline-up-heading-all)
14452 (outline-up-heading-all arg) ; emacs 21 version of outline.el
14453 (outline-up-heading arg t))) ; emacs 22 version of outline.el
14455 (defun org-up-heading-safe ()
14456 "Move to the heading line of which the present line is a subheading.
14457 This version will not throw an error. It will return the level of the
14458 headline found, or nil if no higher level is found."
14459 (let (start-level re)
14460 (org-back-to-heading t)
14461 (setq start-level (funcall outline-level))
14462 (if (equal start-level 1)
14464 (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
14465 (if (re-search-backward re nil t)
14466 (funcall outline-level)))))
14468 (defun org-first-sibling-p ()
14469 "Is this heading the first child of its parents?"
14471 (let ((re (concat "^" outline-regexp))
14473 (unless (org-at-heading-p t)
14474 (error "Not at a heading"))
14475 (setq level (funcall outline-level))
14477 (if (not (re-search-backward re nil t))
14479 (setq l (funcall outline-level))
14482 (defun org-goto-sibling (&optional previous)
14483 "Goto the next sibling, even if it is invisible.
14484 When PREVIOUS is set, go to the previous sibling instead. Returns t
14485 when a sibling was found. When none is found, return nil and don't
14487 (let ((fun (if previous 're-search-backward 're-search-forward))
14489 (re (concat "^" outline-regexp))
14491 (when (condition-case nil (org-back-to-heading t) (error nil))
14492 (setq level (funcall outline-level))
14494 (or previous (forward-char 1))
14495 (while (funcall fun re nil t)
14496 (setq l (funcall outline-level))
14497 (when (< l level) (goto-char pos) (throw 'exit nil))
14498 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
14502 (defun org-show-siblings ()
14503 "Show all siblings of the current headline."
14505 (while (org-goto-sibling) (org-flag-heading nil)))
14507 (while (org-goto-sibling 'previous)
14508 (org-flag-heading nil))))
14510 (defun org-show-hidden-entry ()
14511 "Show an entry where even the heading is hidden."
14515 (defun org-flag-heading (flag &optional entry)
14516 "Flag the current heading. FLAG non-nil means make invisible.
14517 When ENTRY is non-nil, show the entire entry."
14519 (org-back-to-heading t)
14520 ;; Check if we should show the entire entry
14525 (and (outline-next-heading)
14526 (org-flag-heading nil))))
14527 (outline-flag-region (max (point-min) (1- (point)))
14528 (save-excursion (outline-end-of-heading) (point))
14531 (defun org-forward-same-level (arg)
14532 "Move forward to the ARG'th subheading at same level as this one.
14533 Stop at the first and last subheadings of a superior heading.
14534 This is like outline-forward-same-level, but invisible headings are ok."
14536 (org-back-to-heading t)
14538 (let ((point-to-move-to (save-excursion
14539 (org-get-next-sibling))))
14540 (if point-to-move-to
14542 (goto-char point-to-move-to)
14543 (setq arg (1- arg)))
14546 (error "No following same-level heading"))))))
14548 (defun org-get-next-sibling ()
14549 "Move to next heading of the same level, and return point.
14550 If there is no such heading, return nil.
14551 This is like outline-next-sibling, but invisible headings are ok."
14552 (let ((level (funcall outline-level)))
14553 (outline-next-heading)
14554 (while (and (not (eobp)) (> (funcall outline-level) level))
14555 (outline-next-heading))
14556 (if (or (eobp) (< (funcall outline-level) level))
14560 (defun org-end-of-subtree (&optional invisible-OK to-heading)
14561 ;; This is an exact copy of the original function, but it uses
14562 ;; `org-back-to-heading', to make it work also in invisible
14563 ;; trees. And is uses an invisible-OK argument.
14564 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
14565 (org-back-to-heading invisible-OK)
14567 (level (funcall outline-level)))
14568 (while (and (not (eobp))
14569 (or first (> (funcall outline-level) level)))
14571 (outline-next-heading))
14573 (if (memq (preceding-char) '(?\n ?\^M))
14575 ;; Go to end of line before heading
14577 (if (memq (preceding-char) '(?\n ?\^M))
14578 ;; leave blank line before heading
14579 (forward-char -1))))))
14582 (defun org-show-subtree ()
14583 "Show everything after this heading at deeper levels."
14584 (outline-flag-region
14587 (outline-end-of-subtree) (outline-next-heading) (point))
14590 (defun org-show-entry ()
14591 "Show the body directly following this heading.
14592 Show the heading too, if it is currently invisible."
14595 (condition-case nil
14597 (org-back-to-heading t)
14598 (outline-flag-region
14599 (max (point-min) (1- (point)))
14602 (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
14603 (or (match-beginning 1) (point-max)))
14607 (defun org-make-options-regexp (kwds)
14608 "Make a regular expression for keyword lines."
14612 (mapconcat 'regexp-quote kwds "\\|")
14616 ;; Make isearch reveal the necessary context
14617 (defun org-isearch-end ()
14618 "Reveal context after isearch exits."
14619 (when isearch-success ; only if search was successful
14620 (if (featurep 'xemacs)
14621 ;; Under XEmacs, the hook is run in the correct place,
14622 ;; we directly show the context.
14623 (org-show-context 'isearch)
14624 ;; In Emacs the hook runs *before* restoring the overlays.
14625 ;; So we have to use a one-time post-command-hook to do this.
14626 ;; (Emacs 22 has a special variable, see function `org-mode')
14627 (unless (and (boundp 'isearch-mode-end-hook-quit)
14628 isearch-mode-end-hook-quit)
14629 ;; Only when the isearch was not quitted.
14630 (org-add-hook 'post-command-hook 'org-isearch-post-command
14631 'append 'local)))))
14633 (defun org-isearch-post-command ()
14634 "Remove self from hook, and show context."
14635 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
14636 (org-show-context 'isearch))
14639 ;;;; Integration with and fixes for other packages
14643 (defvar org-imenu-markers nil
14644 "All markers currently used by Imenu.")
14645 (make-variable-buffer-local 'org-imenu-markers)
14647 (defun org-imenu-new-marker (&optional pos)
14648 "Return a new marker for use by Imenu, and remember the marker."
14649 (let ((m (make-marker)))
14650 (move-marker m (or pos (point)))
14651 (push m org-imenu-markers)
14654 (defun org-imenu-get-tree ()
14655 "Produce the index for Imenu."
14656 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
14657 (setq org-imenu-markers nil)
14658 (let* ((n org-imenu-depth)
14659 (re (concat "^" outline-regexp))
14660 (subs (make-vector (1+ n) nil))
14666 (goto-char (point-max))
14667 (while (re-search-backward re nil t)
14668 (setq level (org-reduced-level (funcall outline-level)))
14670 (looking-at org-complex-heading-regexp)
14671 (setq head (org-link-display-format
14672 (org-match-string-no-properties 4))
14673 m (org-imenu-new-marker))
14674 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
14675 (if (>= level last-level)
14676 (push (cons head m) (aref subs level))
14677 (push (cons head (aref subs (1+ level))) (aref subs level))
14678 (loop for i from (1+ level) to n do (aset subs i nil)))
14679 (setq last-level level)))))
14682 (eval-after-load "imenu"
14684 (add-hook 'imenu-after-jump-hook
14686 (if (eq major-mode 'org-mode)
14687 (org-show-context 'org-goto))))))
14689 (defun org-link-display-format (link)
14690 "Replace a link with either the description, or the link target
14691 if no description is present"
14693 (if (string-match org-bracket-link-analytic-regexp link)
14694 (replace-match (or (match-string 5 link)
14695 (concat (match-string 1 link)
14696 (match-string 3 link)))
14700 ;; Speedbar support
14702 (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
14703 "Overlay marking the agenda restriction line in speedbar.")
14704 (org-overlay-put org-speedbar-restriction-lock-overlay
14705 'face 'org-agenda-restriction-lock)
14706 (org-overlay-put org-speedbar-restriction-lock-overlay
14707 'help-echo "Agendas are currently limited to this item.")
14708 (org-detach-overlay org-speedbar-restriction-lock-overlay)
14710 (defun org-speedbar-set-agenda-restriction ()
14711 "Restrict future agenda commands to the location at point in speedbar.
14712 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
14714 (require 'org-agenda)
14715 (let (p m tp np dir txt w)
14717 ((setq p (text-property-any (point-at-bol) (point-at-eol)
14719 (setq m (get-text-property p 'org-imenu-marker))
14722 (set-buffer (marker-buffer m))
14724 (org-agenda-set-restriction-lock 'subtree))))
14725 ((setq p (text-property-any (point-at-bol) (point-at-eol)
14726 'speedbar-function 'speedbar-find-file))
14727 (setq tp (previous-single-property-change
14728 (1+ p) 'speedbar-function)
14729 np (next-single-property-change
14730 tp 'speedbar-function)
14731 dir (speedbar-line-directory)
14732 txt (buffer-substring-no-properties (or tp (point-min))
14733 (or np (point-max))))
14736 (set-buffer (find-file-noselect
14737 (let ((default-directory dir))
14738 (expand-file-name txt))))
14739 (unless (org-mode-p)
14740 (error "Cannot restrict to non-Org-mode file"))
14741 (org-agenda-set-restriction-lock 'file))))
14742 (t (error "Don't know how to restrict Org-mode's agenda")))
14743 (org-move-overlay org-speedbar-restriction-lock-overlay
14744 (point-at-bol) (point-at-eol))
14745 (setq current-prefix-arg nil)
14746 (org-agenda-maybe-redo)))
14748 (eval-after-load "speedbar"
14750 (speedbar-add-supported-extension ".org")
14751 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
14752 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
14753 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
14754 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
14755 (add-hook 'speedbar-visiting-tag-hook
14756 (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
14759 ;;; Fixes and Hacks for problems with other packages
14761 ;; Make flyspell not check words in links, to not mess up our keymap
14762 (defun org-mode-flyspell-verify ()
14763 "Don't let flyspell put overlays at active buttons."
14764 (not (get-text-property (point) 'keymap)))
14766 ;; Make `bookmark-jump' show the jump location if it was hidden.
14767 (eval-after-load "bookmark"
14768 '(if (boundp 'bookmark-after-jump-hook)
14769 ;; We can use the hook
14770 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
14771 ;; Hook not available, use advice
14772 (defadvice bookmark-jump (after org-make-visible activate)
14773 "Make the position visible."
14774 (org-bookmark-jump-unhide))))
14776 ;; Make sure saveplace show the location if it was hidden
14777 (eval-after-load "saveplace"
14778 '(defadvice save-place-find-file-hook (after org-make-visible activate)
14779 "Make the position visible."
14780 (org-bookmark-jump-unhide)))
14782 (defun org-bookmark-jump-unhide ()
14783 "Unhide the current position, to show the bookmark location."
14785 (or (org-invisible-p)
14786 (save-excursion (goto-char (max (point-min) (1- (point))))
14787 (org-invisible-p)))
14788 (org-show-context 'bookmark-jump)))
14790 ;; Make session.el ignore our circular variable
14791 (eval-after-load "session"
14792 '(add-to-list 'session-globals-exclude 'org-mark-ring))
14794 ;;;; Experimental code
14796 (defun org-closed-in-range ()
14797 "Sparse tree of items closed in a certain time range.
14798 Still experimental, may disappear in the future."
14800 ;; Get the time interval from the user.
14801 (let* ((time1 (time-to-seconds
14802 (org-read-date nil 'to-time nil "Starting date: ")))
14803 (time2 (time-to-seconds
14804 (org-read-date nil 'to-time nil "End date:")))
14805 ;; callback function
14806 (callback (lambda ()
14809 (apply 'encode-time
14810 (org-parse-time-string
14811 (match-string 1))))))
14812 ;; check if time in interval
14813 (and (>= time time1) (<= time time2))))))
14814 ;; make tree, check each match with the callback
14815 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
14822 (run-hooks 'org-load-hook)
14824 ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
14826 ;;; org.el ends here