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, 2009
4 ;; Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
29 ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
30 ;; project planning with a fast and effective plain-text system.
32 ;; Org-mode develops organizational tasks around NOTES files that contain
33 ;; information about projects as plain text. Org-mode is implemented on
34 ;; top of outline-mode, which makes it possible to keep the content of
35 ;; large files well structured. Visibility cycling and structure editing
36 ;; help to work with the tree. Tables are easily created with a built-in
37 ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
38 ;; and scheduling. It dynamically compiles entries into an agenda that
39 ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
40 ;; Plain text URL-like links connect to websites, emails, Usenet
41 ;; messages, BBDB entries, and any files related to the projects. For
42 ;; printing and sharing of notes, an Org-mode file can be exported as a
43 ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
44 ;; iCalendar file. It can also serve as a publishing tool for a set of
47 ;; Installation and Activation
48 ;; ---------------------------
49 ;; See the corresponding sections in the manual at
51 ;; http://orgmode.org/org.html#Installation
55 ;; The documentation of Org-mode can be found in the TeXInfo file. The
56 ;; distribution also contains a PDF version of it. At the homepage of
57 ;; Org-mode, you can read the same text online as HTML. There is also an
58 ;; excellent reference card made by Philip Rooke. This card can be found
59 ;; in the etc/ directory of Emacs 22.
61 ;; A list of recent changes can be found at
62 ;; http://orgmode.org/Changes.html
66 (defvar org-inhibit-highlight-removal nil
) ; dynamically scoped param
67 (defvar org-table-formula-constants-local nil
68 "Local version of `org-table-formula-constants'.")
69 (make-variable-buffer-local 'org-table-formula-constants-local
)
71 ;;;; Require other packages
77 ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
78 ;; the file noutline.el being loaded.
79 (if (featurep 'xemacs
) (condition-case nil
(require 'noutline
)))
80 ;; We require noutline, which might be provided in outline.el
81 (require 'outline
) (require 'noutline
)
82 ;; Other stuff we need.
84 (unless (fboundp 'time-subtract
) (defalias 'time-subtract
'subtract-time
))
91 (require 'org-footnote
)
93 ;;;; Customization variables
97 (defconst org-version
"6.19e"
98 "The version number of the file org.el.")
100 (defun org-version (&optional here
)
101 "Show the org-mode version in the echo area.
102 With prefix arg HERE, insert it at point."
104 (let ((version (format "Org-mode version %s" org-version
)))
109 ;;; Compatibility constants
111 ;;; The custom variables
114 "Outline-based notes management and organizer."
120 (defcustom org-load-hook nil
121 "Hook that is run after org.el has been loaded."
125 (defvar org-modules
) ; defined below
126 (defvar org-modules-loaded nil
127 "Have the modules been loaded already?")
129 (defun org-load-modules-maybe (&optional force
)
130 "Load all extensions listed in `org-modules'."
131 (when (or force
(not org-modules-loaded
))
133 (condition-case nil
(require ext
)
134 (error (message "Problems while trying to load feature `%s'" ext
))))
136 (setq org-modules-loaded t
)))
138 (defun org-set-modules (var value
)
139 "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
141 (when (featurep 'org
)
142 (org-load-modules-maybe 'force
)))
144 (when (org-bound-and-true-p org-modules
)
145 (let ((a (member 'org-infojs org-modules
)))
146 (and a
(setcar a
'org-jsinfo
))))
148 (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
)
149 "Modules that should always be loaded together with org.el.
150 If a description starts with <C>, the file is not part of Emacs
151 and loading it will require that you have downloaded and properly installed
152 the org-mode distribution.
154 You can also use this system to load external packages (i.e. neither Org
155 core modules, not modules from the CONTRIB directory). Just add symbols
156 to the end of the list. If the package is called org-xyz.el, then you need
157 to add the symbol `xyz', and the package must have a call to
161 :set
'org-set-modules
164 (const :tag
" bbdb: Links to BBDB entries" org-bbdb
)
165 (const :tag
" bibtex: Links to BibTeX entries" org-bibtex
)
166 (const :tag
" gnus: Links to GNUS folders/messages" org-gnus
)
167 (const :tag
" id: Global IDs for identifying entries" org-id
)
168 (const :tag
" info: Links to Info nodes" org-info
)
169 (const :tag
" jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo
)
170 (const :tag
" irc: Links to IRC/ERC chat sessions" org-irc
)
171 (const :tag
" mac-message: Links to messages in Apple Mail" org-mac-message
)
172 (const :tag
" mew Links to Mew folders/messages" org-mew
)
173 (const :tag
" mhe: Links to MHE folders/messages" org-mhe
)
174 (const :tag
" rmail: Links to RMAIL folders/messages" org-rmail
)
175 (const :tag
" vm: Links to VM folders/messages" org-vm
)
176 (const :tag
" wl: Links to Wanderlust folders/messages" org-wl
)
177 (const :tag
" w3m: Special cut/past from w3m to Org." org-w3m
)
178 (const :tag
" mouse: Additional mouse support" org-mouse
)
180 (const :tag
"C annotate-file: Annotate a file with org syntax" org-annotate-file
)
181 (const :tag
"C annotation-helper: Call Remember directly from Browser" org-annotation-helper
)
182 (const :tag
"C bookmark: Org links to bookmarks" org-bookmark
)
183 (const :tag
"C browser-url: Store link, directly from Browser" org-browser-url
)
184 (const :tag
"C depend: TODO dependencies for Org-mode" org-depend
)
185 (const :tag
"C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol
)
186 (const :tag
"C eval: Include command output as text" org-eval
)
187 (const :tag
"C eval-light: Evaluate inbuffer-code on demand" org-eval-light
)
188 (const :tag
"C expiry: Expiry mechanism for Org entries" org-expiry
)
189 (const :tag
"C exp-blocks: Pre-process blocks for export" org-exp-blocks
)
190 (const :tag
"C interactive-query: Interactive modification of tags query" org-interactive-query
)
191 (const :tag
"C mairix: Hook mairix search into Org for different MUAs" org-mairix
)
192 (const :tag
"C man: Support for links to manpages in Org-mode" org-man
)
193 (const :tag
"C mtags: Support for muse-like tags" org-mtags
)
194 (const :tag
"C panel: Simple routines for us with bad memory" org-panel
)
195 (const :tag
"C registry: A registry for Org links" org-registry
)
196 (const :tag
"C org2rem: Convert org appointments into reminders" org2rem
)
197 (const :tag
"C screen: Visit screen sessions through Org-mode links" org-screen
)
198 (const :tag
"C toc: Table of contents for Org-mode buffer" org-toc
)
199 (const :tag
"C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert
)
200 (repeat :tag
"External packages" :inline t
(symbol :tag
"Package"))))
202 (defcustom org-support-shift-select nil
203 "Non-nil means, make shift-cursor commands select text when possible.
205 In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
206 selecting a region, or enlarge thusly regions started in this way.
207 In Org-mode, in special contexts, these same keys are used for other
208 purposes, important enough to compete with shift selection. Org tries
209 to balance these needs by supporting `shift-select-mode' outside these
210 special contexts, under control of this variable.
212 The default of this variable is nil, to avoid confusing behavior. Shifted
213 cursor keys will then execute Org commands in the following contexts:
214 - on a headline, changing TODO state (left/right) and priority (up/down)
215 - on a time stamp, changing the time
216 - in a plain list item, changing the bullet type
217 - in a property definition line, switching between allowed values
218 - in the BEGIN line of a clock table (changing the time block).
219 Outside these contexts, the commands will throw an error.
221 When this variable is t and the cursor is not in a special context,
222 Org-mode will support shift-selection for making and enlarging regions.
223 To make this more effective, the bullet cycling will no longer happen
224 anywhere in an item line, but only if the cursor is exactly on the bullet.
226 If you set this variable to the symbol `always', then the keys
227 will not be special in headlines, property lines, and item lines, to make
228 shift selection work there as well. If this is what you want, you can
229 use the following alternative commands: `C-c C-t' and `C-c ,' to
230 change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
231 TODO sets, `C-c -' to cycle item bullet types, and properties can be
232 edited by hand or in column view.
234 However, when the cursor is on a timestamp, shift-cursor commands
235 will still edit the time stamp - this is just too good to give up.
237 XEmacs user should have this variable set to nil, because shift-select-mode
241 (const :tag
"Never" nil
)
242 (const :tag
"When outside special context" t
)
243 (const :tag
"Everywhere except timestamps" always
)))
245 (defgroup org-startup nil
246 "Options concerning startup of Org-mode."
250 (defcustom org-startup-folded t
251 "Non-nil means, entering Org-mode will switch to OVERVIEW.
252 This can also be configured on a per-file basis by adding one of
253 the following lines anywhere in the buffer:
260 (const :tag
"nofold: show all" nil
)
261 (const :tag
"fold: overview" t
)
262 (const :tag
"content: all headlines" content
)))
264 (defcustom org-startup-truncated t
265 "Non-nil means, entering Org-mode will set `truncate-lines'.
266 This is useful since some lines containing links can be very long and
267 uninteresting. Also tables look terrible when wrapped."
271 (defcustom org-startup-align-all-tables nil
272 "Non-nil means, align all tables when visiting a file.
273 This is useful when the column width in tables is forced with <N> cookies
274 in table fields. Such tables will look correct only after the first re-align.
275 This can also be configured on a per-file basis by adding one of
276 the following lines anywhere in the buffer:
282 (defcustom org-insert-mode-line-in-empty-file nil
283 "Non-nil means insert the first line setting Org-mode in empty files.
284 When the function `org-mode' is called interactively in an empty file, this
285 normally means that the file name does not automatically trigger Org-mode.
286 To ensure that the file will always be in Org-mode in the future, a
287 line enforcing Org-mode will be inserted into the buffer, if this option
292 (defcustom org-replace-disputed-keys nil
293 "Non-nil means use alternative key bindings for some keys.
294 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
295 These keys are also used by other packages like `CUA-mode' or `windmove.el'.
296 If you want to use Org-mode together with one of these other modes,
297 or more generally if you would like to move some Org-mode commands to
298 other keys, set this variable and configure the keys with the variable
301 This option is only relevant at load-time of Org-mode, and must be set
302 *before* org.el is loaded. Changing it requires a restart of Emacs to
307 (defcustom org-use-extra-keys nil
308 "Non-nil means use extra key sequence definitions for certain
309 commands. This happens automatically if you run XEmacs or if
310 window-system is nil. This variable lets you do the same
311 manually. You must set it before loading org.
313 Example: on Carbon Emacs 22 running graphically, with an external
314 keyboard on a Powerbook, the default way of setting M-left might
315 not work for either Alt or ESC. Setting this variable will make
320 (if (fboundp 'defvaralias
)
321 (defvaralias 'org-CUA-compatible
'org-replace-disputed-keys
))
323 (defcustom org-disputed-keys
324 '(([(shift up
)] .
[(meta p
)])
325 ([(shift down
)] .
[(meta n
)])
326 ([(shift left
)] .
[(meta -
)])
327 ([(shift right
)] .
[(meta +)])
328 ([(control shift right
)] .
[(meta shift
+)])
329 ([(control shift left
)] .
[(meta shift -
)]))
330 "Keys for which Org-mode and other modes compete.
331 This is an alist, cars are the default keys, second element specifies
332 the alternative to use when `org-replace-disputed-keys' is t.
334 Keys can be specified in any syntax supported by `define-key'.
335 The value of this option takes effect only at Org-mode's startup,
336 therefore you'll have to restart Emacs to apply it after changing."
341 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
342 Or return the original if not disputed."
343 (if org-replace-disputed-keys
344 (let* ((nkey (key-description key
))
345 (x (org-find-if (lambda (x)
346 (equal (key-description (car x
)) nkey
))
351 (defun org-find-if (predicate seq
)
354 (if (funcall predicate
(car seq
))
355 (throw 'exit
(car seq
))
358 (defun org-defkey (keymap key def
)
359 "Define a key, possibly translated, as returned by `org-key'."
360 (define-key keymap
(org-key key
) def
))
362 (defcustom org-ellipsis nil
363 "The ellipsis to use in the Org-mode outline.
364 When nil, just use the standard three dots. When a string, use that instead,
365 When a face, use the standard 3 dots, but with the specified face.
366 The change affects only Org-mode (which will then use its own display table).
367 Changing this requires executing `M-x org-mode' in a buffer to become
370 :type
'(choice (const :tag
"Default" nil
)
371 (face :tag
"Face" :value org-warning
)
372 (string :tag
"String" :value
"...#")))
374 (defvar org-display-table nil
375 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
377 (defgroup org-keywords nil
378 "Keywords in Org-mode."
382 (defcustom org-deadline-string
"DEADLINE:"
383 "String to mark deadline entries.
384 A deadline is this string, followed by a time stamp. Should be a word,
385 terminated by a colon. You can insert a schedule keyword and
386 a timestamp with \\[org-deadline].
387 Changes become only effective after restarting Emacs."
391 (defcustom org-scheduled-string
"SCHEDULED:"
392 "String to mark scheduled TODO entries.
393 A schedule is this string, followed by a time stamp. Should be a word,
394 terminated by a colon. You can insert a schedule keyword and
395 a timestamp with \\[org-schedule].
396 Changes become only effective after restarting Emacs."
400 (defcustom org-closed-string
"CLOSED:"
401 "String used as the prefix for timestamps logging closing a TODO entry."
405 (defcustom org-clock-string
"CLOCK:"
406 "String used as prefix for timestamps clocking work hours on an item."
410 (defcustom org-comment-string
"COMMENT"
411 "Entries starting with this keyword will never be exported.
412 An entry can be toggled between COMMENT and normal with
413 \\[org-toggle-comment].
414 Changes become only effective after restarting Emacs."
418 (defcustom org-quote-string
"QUOTE"
419 "Entries starting with this keyword will be exported in fixed-width font.
420 Quoting applies only to the text in the entry following the headline, and does
421 not extend beyond the next headline, even if that is lower level.
422 An entry can be toggled between QUOTE and normal with
423 \\[org-toggle-fixed-width-section]."
427 (defconst org-repeat-re
428 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
429 "Regular expression for specifying repeated events.
430 After a match, group 1 contains the repeat expression.")
432 (defgroup org-structure nil
433 "Options concerning the general structure of Org-mode files."
437 (defgroup org-reveal-location nil
438 "Options about how to make context of a location visible."
439 :tag
"Org Reveal Location"
440 :group
'org-structure
)
442 (defconst org-context-choice
444 (const :tag
"Always" t
)
445 (const :tag
"Never" nil
)
446 (repeat :greedy t
:tag
"Individual contexts"
448 (choice :tag
"Context"
455 (const bookmark-jump
)
459 "Contexts for the reveal options.")
461 (defcustom org-show-hierarchy-above
'((default . t
))
462 "Non-nil means, show full hierarchy when revealing a location.
463 Org-mode often shows locations in an org-mode file which might have
464 been invisible before. When this is set, the hierarchy of headings
465 above the exposed location is shown.
466 Turning this off for example for sparse trees makes them very compact.
467 Instead of t, this can also be an alist specifying this option for different
468 contexts. Valid contexts are
469 agenda when exposing an entry from the agenda
470 org-goto when using the command `org-goto' on key C-c C-j
471 occur-tree when using the command `org-occur' on key C-c /
472 tags-tree when constructing a sparse tree based on tags matches
473 link-search when exposing search matches associated with a link
474 mark-goto when exposing the jump goal of a mark
475 bookmark-jump when exposing a bookmark location
476 isearch when exiting from an incremental search
477 default default for all contexts not set explicitly"
478 :group
'org-reveal-location
479 :type org-context-choice
)
481 (defcustom org-show-following-heading
'((default . nil
))
482 "Non-nil means, show following heading when revealing a location.
483 Org-mode often shows locations in an org-mode file which might have
484 been invisible before. When this is set, the heading following the
486 Turning this off for example for sparse trees makes them very compact,
487 but makes it harder to edit the location of the match. In such a case,
488 use the command \\[org-reveal] to show more context.
489 Instead of t, this can also be an alist specifying this option for different
490 contexts. See `org-show-hierarchy-above' for valid contexts."
491 :group
'org-reveal-location
492 :type org-context-choice
)
494 (defcustom org-show-siblings
'((default . nil
) (isearch t
))
495 "Non-nil means, show all sibling heading when revealing a location.
496 Org-mode often shows locations in an org-mode file which might have
497 been invisible before. When this is set, the sibling of the current entry
498 heading are all made visible. If `org-show-hierarchy-above' is t,
499 the same happens on each level of the hierarchy above the current entry.
501 By default this is on for the isearch context, off for all other contexts.
502 Turning this off for example for sparse trees makes them very compact,
503 but makes it harder to edit the location of the match. In such a case,
504 use the command \\[org-reveal] to show more context.
505 Instead of t, this can also be an alist specifying this option for different
506 contexts. See `org-show-hierarchy-above' for valid contexts."
507 :group
'org-reveal-location
508 :type org-context-choice
)
510 (defcustom org-show-entry-below
'((default . nil
))
511 "Non-nil means, show the entry below a headline when revealing a location.
512 Org-mode often shows locations in an org-mode file which might have
513 been invisible before. When this is set, the text below the headline that is
514 exposed is also shown.
516 By default this is off for all contexts.
517 Instead of t, this can also be an alist specifying this option for different
518 contexts. See `org-show-hierarchy-above' for valid contexts."
519 :group
'org-reveal-location
520 :type org-context-choice
)
522 (defcustom org-indirect-buffer-display
'other-window
523 "How should indirect tree buffers be displayed?
524 This applies to indirect buffers created with the commands
525 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
527 current-window Display in the current window
528 other-window Just display in another window.
529 dedicated-frame Create one new frame, and re-use it each time.
530 new-frame Make a new frame each time. Note that in this case
531 previously-made indirect buffers are kept, and you need to
532 kill these buffers yourself."
533 :group
'org-structure
534 :group
'org-agenda-windows
536 (const :tag
"In current window" current-window
)
537 (const :tag
"In current frame, other window" other-window
)
538 (const :tag
"Each time a new frame" new-frame
)
539 (const :tag
"One dedicated frame" dedicated-frame
)))
541 (defgroup org-cycle nil
542 "Options concerning visibility cycling in Org-mode."
544 :group
'org-structure
)
546 (defcustom org-drawers
'("PROPERTIES" "CLOCK")
547 "Names of drawers. Drawers are not opened by cycling on the headline above.
548 Drawers only open with a TAB on the drawer line itself. A drawer looks like
553 The drawer \"PROPERTIES\" is special for capturing properties through
556 Drawers can be defined on the per-file basis with a line like:
558 #+DRAWERS: HIDDEN STATE PROPERTIES"
559 :group
'org-structure
560 :type
'(repeat (string :tag
"Drawer Name")))
562 (defcustom org-cycle-global-at-bob nil
563 "Cycle globally if cursor is at beginning of buffer and not at a headline.
564 This makes it possible to do global cycling without having to use S-TAB or
565 C-u TAB. For this special case to work, the first line of the buffer
566 must not be a headline - it may be empty ot some other text. When used in
567 this way, `org-cycle-hook' is disables temporarily, to make sure the
568 cursor stays at the beginning of the buffer.
569 When this option is nil, don't do anything special at the beginning
574 (defcustom org-cycle-emulate-tab t
575 "Where should `org-cycle' emulate TAB.
577 white Only in completely white lines
578 whitestart Only at the beginning of lines, before the first non-white char
579 t Everywhere except in headlines
580 exc-hl-bol Everywhere except at the start of a headline
581 If TAB is used in a place where it does not emulate TAB, the current subtree
582 visibility is cycled."
584 :type
'(choice (const :tag
"Never" nil
)
585 (const :tag
"Only in completely white lines" white
)
586 (const :tag
"Before first char in a line" whitestart
)
587 (const :tag
"Everywhere except in headlines" t
)
588 (const :tag
"Everywhere except at bol in headlines" exc-hl-bol
)
591 (defcustom org-cycle-separator-lines
2
592 "Number of empty lines needed to keep an empty line between collapsed trees.
593 If you leave an empty line between the end of a subtree and the following
594 headline, this empty line is hidden when the subtree is folded.
595 Org-mode will leave (exactly) one empty line visible if the number of
596 empty lines is equal or larger to the number given in this variable.
597 So the default 2 means, at least 2 empty lines after the end of a subtree
598 are needed to produce free space between a collapsed subtree and the
601 Special case: when 0, never leave empty lines in collapsed view."
604 (put 'org-cycle-separator-lines
'safe-local-variable
'integerp
)
606 (defcustom org-cycle-hook
'(org-cycle-hide-archived-subtrees
607 org-cycle-hide-drawers
608 org-cycle-show-empty-lines
609 org-optimize-window-after-visibility-change
)
610 "Hook that is run after `org-cycle' has changed the buffer visibility.
611 The function(s) in this hook must accept a single argument which indicates
612 the new state that was set by the most recent `org-cycle' command. The
613 argument is a symbol. After a global state change, it can have the values
614 `overview', `content', or `all'. After a local state change, it can have
615 the values `folded', `children', or `subtree'."
619 (defgroup org-edit-structure nil
620 "Options concerning structure editing in Org-mode."
621 :tag
"Org Edit Structure"
622 :group
'org-structure
)
624 (defcustom org-odd-levels-only nil
625 "Non-nil means, skip even levels and only use odd levels for the outline.
626 This has the effect that two stars are being added/taken away in
627 promotion/demotion commands. It also influences how levels are
628 handled by the exporters.
629 Changing it requires restart of `font-lock-mode' to become effective
630 for fontification also in regions already fontified.
631 You may also set this on a per-file basis by adding one of the following
636 :group
'org-edit-structure
637 :group
'org-font-lock
640 (defcustom org-adapt-indentation t
641 "Non-nil means, adapt indentation when promoting and demoting.
642 When this is set and the *entire* text in an entry is indented, the
643 indentation is increased by one space in a demotion command, and
644 decreased by one in a promotion command. If any line in the entry
645 body starts at column 0, indentation is not changed at all."
646 :group
'org-edit-structure
649 (defcustom org-special-ctrl-a
/e nil
650 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
651 When t, `C-a' will bring back the cursor to the beginning of the
652 headline text, i.e. after the stars and after a possible TODO keyword.
653 In an item, this will be the position after the bullet.
654 When the cursor is already at that position, another `C-a' will bring
655 it to the beginning of the line.
656 `C-e' will jump to the end of the headline, ignoring the presence of tags
657 in the headline. A second `C-e' will then jump to the true end of the
658 line, after any tags.
659 When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
660 and only a directly following, identical keypress will bring the cursor
661 to the special positions."
662 :group
'org-edit-structure
664 (const :tag
"off" nil
)
665 (const :tag
"after bullet first" t
)
666 (const :tag
"border first" reversed
)))
668 (if (fboundp 'defvaralias
)
669 (defvaralias 'org-special-ctrl-a
'org-special-ctrl-a
/e
))
671 (defcustom org-special-ctrl-k nil
672 "Non-nil means `C-k' will behave specially in headlines.
673 When nil, `C-k' will call the default `kill-line' command.
674 When t, the following will happen while the cursor is in the headline:
676 - When the cursor is at the beginning of a headline, kill the entire
677 line and possible the folded subtree below the line.
678 - When in the middle of the headline text, kill the headline up to the tags.
679 - When after the headline text, kill the tags."
680 :group
'org-edit-structure
683 (defcustom org-yank-folded-subtrees t
684 "Non-nil means, when yanking subtrees, fold them.
685 If the kill is a single subtree, or a sequence of subtrees, i.e. if
686 it starts with a heading and all other headings in it are either children
687 or siblings, then fold all the subtrees. However, do this only if no
688 text after the yank would be swallowed into a folded tree by this action."
689 :group
'org-edit-structure
692 (defcustom org-yank-adjusted-subtrees t
693 "Non-nil means, when yanking subtrees, adjust the level.
694 With this setting, `org-paste-subtree' is used to insert the subtree, see
695 this function for details."
696 :group
'org-edit-structure
699 (defcustom org-M-RET-may-split-line
'((default . t
))
700 "Non-nil means, M-RET will split the line at the cursor position.
701 When nil, it will go to the end of the line before making a
703 You may also set this option in a different way for different
704 contexts. Valid contexts are:
706 headline when creating a new headline
707 item when creating a new item
708 table in a table field
709 default the value to be used for all contexts not explicitly
711 :group
'org-structure
714 (const :tag
"Always" t
)
715 (const :tag
"Never" nil
)
716 (repeat :greedy t
:tag
"Individual contexts"
718 (choice :tag
"Context"
726 (defcustom org-insert-heading-respect-content nil
727 "Non-nil means, insert new headings after the current subtree.
728 When nil, the new heading is created directly after the current line.
729 The commands \\[org-insert-heading-respect-content] and
730 \\[org-insert-todo-heading-respect-content] turn this variable on
731 for the duration of the command."
732 :group
'org-structure
735 (defcustom org-blank-before-new-entry
'((heading . auto
)
736 (plain-list-item . auto
))
737 "Should `org-insert-heading' leave a blank line before new heading/item?
738 The value is an alist, with `heading' and `plain-list-item' as car,
739 and a boolean flag as cdr."
740 :group
'org-edit-structure
742 (cons (const heading
)
743 (choice (const :tag
"Never" nil
)
744 (const :tag
"Always" t
)
745 (const :tag
"Auto" auto
)))
746 (cons (const plain-list-item
)
747 (choice (const :tag
"Never" nil
)
748 (const :tag
"Always" t
)
749 (const :tag
"Auto" auto
)))))
751 (defcustom org-insert-heading-hook nil
752 "Hook being run after inserting a new heading."
753 :group
'org-edit-structure
756 (defcustom org-enable-fixed-width-editor t
757 "Non-nil means, lines starting with \":\" are treated as fixed-width.
758 This currently only means, they are never auto-wrapped.
759 When nil, such lines will be treated like ordinary lines.
760 See also the QUOTE keyword."
761 :group
'org-edit-structure
764 (defcustom org-edit-src-region-extra nil
765 "Additional regexps to identify regions for editing with `org-edit-src-code'.
766 For examples see the function `org-edit-src-find-region-and-lang'.
767 The regular expression identifying the begin marker should end with a newline,
768 and the regexp marking the end line should start with a newline, to make sure
769 there are kept outside the narrowed region."
770 :group
'org-edit-structure
773 (regexp :tag
"begin regexp")
774 (regexp :tag
"end regexp")
775 (choice :tag
"language"
776 (string :tag
"specify")
777 (integer :tag
"from match group")
778 (const :tag
"from `lang' element")
779 (const :tag
"from `style' element")))))
781 (defcustom org-coderef-label-format
"(ref:%s)"
782 "The default coderef format.
783 This format string will be used to search for coderef labels in literal
784 examples (EXAMPLE and SRC blocks). The format can be overwritten
785 an individual literal example with the -f option, like
787 #+BEGIN_SRC pascal +n -r -l \"((%s))\"
791 If you want to use this for HTML export, make sure that the format does
792 not introduce special font-locking, and avoid the HTML special
793 characters `<', `>', and `&'. The reason for this restriction is that
794 the labels are searched for only after htmlize has done its job."
795 :group
'org-edit-structure
; FIXME this is not in the right group
798 (defcustom org-edit-fixed-width-region-mode
'artist-mode
799 "The mode that should be used to edit fixed-width regions.
800 These are the regions where each line starts with a colon."
801 :group
'org-edit-structure
805 (const fundamental-mode
)
806 (function :tag
"Other (specify)")))
808 (defcustom org-goto-auto-isearch t
809 "Non-nil means, typing characters in org-goto starts incremental search."
810 :group
'org-edit-structure
813 (defgroup org-sparse-trees nil
814 "Options concerning sparse trees in Org-mode."
815 :tag
"Org Sparse Trees"
816 :group
'org-structure
)
818 (defcustom org-highlight-sparse-tree-matches t
819 "Non-nil means, highlight all matches that define a sparse tree.
820 The highlights will automatically disappear the next time the buffer is
821 changed by an edit command."
822 :group
'org-sparse-trees
825 (defcustom org-remove-highlights-with-change t
826 "Non-nil means, any change to the buffer will remove temporary highlights.
827 Such highlights are created by `org-occur' and `org-clock-display'.
828 When nil, `C-c C-c needs to be used to get rid of the highlights.
829 The highlights created by `org-preview-latex-fragment' always need
830 `C-c C-c' to be removed."
831 :group
'org-sparse-trees
836 (defcustom org-occur-hook
'(org-first-headline-recenter)
837 "Hook that is run after `org-occur' has constructed a sparse tree.
838 This can be used to recenter the window to show as much of the structure
840 :group
'org-sparse-trees
843 (defgroup org-imenu-and-speedbar nil
844 "Options concerning imenu and speedbar in Org-mode."
845 :tag
"Org Imenu and Speedbar"
846 :group
'org-structure
)
848 (defcustom org-imenu-depth
2
849 "The maximum level for Imenu access to Org-mode headlines.
850 This also applied for speedbar access."
851 :group
'org-imenu-and-speedbar
854 (defgroup org-table nil
855 "Options concerning tables in Org-mode."
859 (defcustom org-enable-table-editor
'optimized
860 "Non-nil means, lines starting with \"|\" are handled by the table editor.
861 When nil, such lines will be treated like ordinary lines.
863 When equal to the symbol `optimized', the table editor will be optimized to
865 - Automatic overwrite mode in front of whitespace in table fields.
866 This makes the structure of the table stay in tact as long as the edited
867 field does not exceed the column width.
868 - Minimize the number of realigns. Normally, the table is aligned each time
869 TAB or RET are pressed to move to another field. With optimization this
870 happens only if changes to a field might have changed the column width.
871 Optimization requires replacing the functions `self-insert-command',
872 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
873 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
874 very good at guessing when a re-align will be necessary, but you can always
875 force one with \\[org-ctrl-c-ctrl-c].
877 If you would like to use the optimized version in Org-mode, but the
878 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
880 This variable can be used to turn on and off the table editor during a session,
881 but in order to toggle optimization, a restart is required.
883 See also the variable `org-table-auto-blank-field'."
886 (const :tag
"off" nil
)
888 (const :tag
"on, optimized" optimized
)))
890 (defcustom org-table-tab-recognizes-table.el t
891 "Non-nil means, TAB will automatically notice a table.el table.
892 When it sees such a table, it moves point into it and - if necessary -
893 calls `table-recognize-table'."
894 :group
'org-table-editing
897 (defgroup org-link nil
898 "Options concerning links in Org-mode."
902 (defvar org-link-abbrev-alist-local nil
903 "Buffer-local version of `org-link-abbrev-alist', which see.
904 The value of this is taken from the #+LINK lines.")
905 (make-variable-buffer-local 'org-link-abbrev-alist-local
)
907 (defcustom org-link-abbrev-alist nil
908 "Alist of link abbreviations.
909 The car of each element is a string, to be replaced at the start of a link.
910 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
911 links in Org-mode buffers can have an optional tag after a double colon, e.g.
913 [[linkkey:tag][description]]
915 If REPLACE is a string, the tag will simply be appended to create the link.
916 If the string contains \"%s\", the tag will be inserted there. Alternatively,
917 the placeholder \"%h\" will cause a url-encoded version of the tag to
918 be inserted at that point (see the function `url-hexify-string').
920 REPLACE may also be a function that will be called with the tag as the
921 only argument to create the link, which should be returned as a string.
923 See the manual for examples."
927 (string :tag
"Protocol")
929 (string :tag
"Format")
932 (defcustom org-descriptive-links t
933 "Non-nil means, hide link part and only show description of bracket links.
934 Bracket links are like [[link][description]]. This variable sets the initial
935 state in new org-mode buffers. The setting can then be toggled on a
936 per-buffer basis from the Org->Hyperlinks menu."
940 (defcustom org-link-file-path-type
'adaptive
941 "How the path name in file links should be stored.
944 relative Relative to the current directory, i.e. the directory of the file
945 into which the link is being inserted.
946 absolute Absolute path, if possible with ~ for home directory.
947 noabbrev Absolute path, no abbreviation of home directory.
948 adaptive Use relative path for files in the current directory and sub-
949 directories of it. For other files, use an absolute path."
957 (defcustom org-activate-links
'(bracket angle plain radio tag date footnote
)
958 "Types of links that should be activated in Org-mode files.
959 This is a list of symbols, each leading to the activation of a certain link
960 type. In principle, it does not hurt to turn on most link types - there may
961 be a small gain when turning off unused link types. The types are:
963 bracket The recommended [[link][description]] or [[link]] links with hiding.
964 angular Links in angular brackets that may contain whitespace like
965 <bbdb:Carsten Dominik>.
966 plain Plain links in normal text, no whitespace, like http://google.com.
967 radio Text that is matched by a radio target, see manual for details.
968 tag Tag settings in a headline (link to tag search).
969 date Time stamps (link to calendar).
970 footnote Footnote labels.
972 Changing this variable requires a restart of Emacs to become effective."
974 :type
'(set :greedy t
975 (const :tag
"Double bracket links (new style)" bracket
)
976 (const :tag
"Angular bracket links (old style)" angular
)
977 (const :tag
"Plain text links" plain
)
978 (const :tag
"Radio target matches" radio
)
979 (const :tag
"Tags" tag
)
980 (const :tag
"Timestamps" date
)
981 (const :tag
"Footnotes" footnote
)))
983 (defcustom org-make-link-description-function nil
984 "Function to use to generate link descriptions from links. If
985 nil the link location will be used. This function must take two
986 parameters; the first is the link and the second the description
987 org-insert-link has generated, and should return the description
992 (defgroup org-link-store nil
993 "Options concerning storing links in Org-mode."
994 :tag
"Org Store Link"
997 (defcustom org-email-link-description-format
"Email %c: %.30s"
998 "Format of the description part of a link to an email or usenet message.
999 The following %-escapes will be replaced by corresponding information:
1001 %F full \"From\" field
1002 %f name, taken from \"From\" field, address if no name
1003 %T full \"To\" field
1004 %t first name in \"To\" field, address if no name
1005 %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
1006 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
1010 You may use normal field width specification between the % and the letter.
1011 This is for example useful to limit the length of the subject.
1013 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
1014 :group
'org-link-store
1017 (defcustom org-from-is-user-regexp
1019 (when (and user-mail-address
(not (string= user-mail-address
"")))
1020 (setq r1
(concat "\\<" (regexp-quote user-mail-address
) "\\>")))
1021 (when (and user-full-name
(not (string= user-full-name
"")))
1022 (setq r2
(concat "\\<" (regexp-quote user-full-name
) "\\>")))
1023 (if (and r1 r2
) (concat r1
"\\|" r2
) (or r1 r2
)))
1024 "Regexp matched against the \"From:\" header of an email or usenet message.
1025 It should match if the message is from the user him/herself."
1026 :group
'org-link-store
1029 (defcustom org-link-to-org-use-id
'create-if-interactive
1030 "Non-nil means, storing a link to an Org file will use entry IDs.
1032 Note that before this variable is even considered, org-id must be loaded,
1033 to please customize `org-modules' and turn it on.
1035 The variable can have the following values:
1037 t Create an ID if needed to make a link to the current entry.
1039 create-if-interactive
1040 If `org-store-link' is called directly (interactively, as a user
1041 command), do create an ID to support the link. But when doing the
1042 job for remember, only use the ID if it already exists. The
1043 purpose of this setting is to avoid proliferation of unwanted
1044 IDs, just because you happen to be in an Org file when you
1045 call `org-remember' that automatically and preemptively
1046 creates a link. If you do want to get an ID link in a remember
1047 template to an entry not having an ID, create it first by
1048 explicitly creating a link to it, using `C-c C-l' first.
1051 Use existing ID, do not create one.
1053 nil Never use an ID to make a link, instead link using a text search for
1055 :group
'org-link-store
1057 (const :tag
"Create ID to make link" t
)
1058 (const :tag
"Create if string link interactively"
1059 'create-if-interactive
)
1060 (const :tag
"Only use existing" 'use-existing
)
1061 (const :tag
"Do not use ID to create link" nil
)))
1063 (defcustom org-context-in-file-links t
1064 "Non-nil means, file links from `org-store-link' contain context.
1065 A search string will be added to the file name with :: as separator and
1066 used to find the context when the link is activated by the command
1067 `org-open-at-point'.
1068 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1069 negates this setting for the duration of the command."
1070 :group
'org-link-store
1073 (defcustom org-keep-stored-link-after-insertion nil
1074 "Non-nil means, keep link in list for entire session.
1076 The command `org-store-link' adds a link pointing to the current
1077 location to an internal list. These links accumulate during a session.
1078 The command `org-insert-link' can be used to insert links into any
1079 Org-mode file (offering completion for all stored links). When this
1080 option is nil, every link which has been inserted once using \\[org-insert-link]
1081 will be removed from the list, to make completing the unused links
1083 :group
'org-link-store
1086 (defgroup org-link-follow nil
1087 "Options concerning following links in Org-mode."
1088 :tag
"Org Follow Link"
1091 (defcustom org-link-translation-function nil
1092 "Function to translate links with different syntax to Org syntax.
1093 This can be used to translate links created for example by the Planner
1094 or emacs-wiki packages to Org syntax.
1095 The function must accept two parameters, a TYPE containing the link
1096 protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
1097 which is everything after the link protocol. It should return a cons
1098 with possibly modified values of type and path.
1099 Org contains a function for this, so if you set this variable to
1100 `org-translate-link-from-planner', you should be able follow many
1101 links created by planner."
1102 :group
'org-link-follow
1105 (defcustom org-follow-link-hook nil
1106 "Hook that is run after a link has been followed."
1107 :group
'org-link-follow
1110 (defcustom org-tab-follows-link nil
1111 "Non-nil means, on links TAB will follow the link.
1112 Needs to be set before org.el is loaded."
1113 :group
'org-link-follow
1116 (defcustom org-return-follows-link nil
1117 "Non-nil means, on links RET will follow the link.
1118 Needs to be set before org.el is loaded."
1119 :group
'org-link-follow
1122 (defcustom org-mouse-1-follows-link
1123 (if (boundp 'mouse-1-click-follows-link
) mouse-1-click-follows-link t
)
1124 "Non-nil means, mouse-1 on a link will follow the link.
1125 A longer mouse click will still set point. Does not work on XEmacs.
1126 Needs to be set before org.el is loaded."
1127 :group
'org-link-follow
1130 (defcustom org-mark-ring-length
4
1131 "Number of different positions to be recorded in the ring
1132 Changing this requires a restart of Emacs to work correctly."
1133 :group
'org-link-follow
1136 (defcustom org-link-frame-setup
1137 '((vm . vm-visit-folder-other-frame
)
1138 (gnus . gnus-other-frame
)
1139 (file . find-file-other-window
))
1140 "Setup the frame configuration for following links.
1141 When following a link with Emacs, it may often be useful to display
1142 this link in another window or frame. This variable can be used to
1143 set this up for the different types of links.
1146 `vm-visit-folder-other-frame'
1147 For Gnus, use any of
1150 `org-gnus-no-new-news'
1151 For FILE, use any of
1153 `find-file-other-window'
1154 `find-file-other-frame'
1155 For the calendar, use the variable `calendar-setup'.
1156 For BBDB, it is currently only possible to display the matches in
1158 :group
'org-link-follow
1162 (const vm-visit-folder
)
1163 (const vm-visit-folder-other-window
)
1164 (const vm-visit-folder-other-frame
)))
1168 (const gnus-other-frame
)
1169 (const org-gnus-no-new-news
)))
1173 (const find-file-other-window
)
1174 (const find-file-other-frame
)))))
1176 (defcustom org-display-internal-link-with-indirect-buffer nil
1177 "Non-nil means, use indirect buffer to display infile links.
1178 Activating internal links (from one location in a file to another location
1179 in the same file) normally just jumps to the location. When the link is
1180 activated with a C-u prefix (or with mouse-3), the link is displayed in
1181 another window. When this option is set, the other window actually displays
1182 an indirect buffer clone of the current buffer, to avoid any visibility
1183 changes to the current buffer."
1184 :group
'org-link-follow
1187 (defcustom org-open-non-existing-files nil
1188 "Non-nil means, `org-open-file' will open non-existing files.
1189 When nil, an error will be generated."
1190 :group
'org-link-follow
1193 (defcustom org-open-directory-means-index-dot-org nil
1194 "Non-nil means, a link to a directory really means to index.org.
1195 When nil, following a directory link will run dired or open a finder/explorer
1196 window on that directory."
1197 :group
'org-link-follow
1200 (defcustom org-link-mailto-program
'(browse-url "mailto:%a?subject=%s")
1201 "Function and arguments to call for following mailto links.
1202 This is a list with the first element being a lisp function, and the
1203 remaining elements being arguments to the function. In string arguments,
1204 %a will be replaced by the address, and %s will be replaced by the subject
1205 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1206 :group
'org-link-follow
1208 (const :tag
"browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1209 (const :tag
"compose-mail" (compose-mail "%a" "%s"))
1210 (const :tag
"message-mail" (message-mail "%a" "%s"))
1211 (cons :tag
"other" (function) (repeat :tag
"argument" sexp
))))
1213 (defcustom org-confirm-shell-link-function
'yes-or-no-p
1214 "Non-nil means, ask for confirmation before executing shell links.
1215 Shell links can be dangerous: just think about a link
1217 [[shell:rm -rf ~/*][Google Search]]
1219 This link would show up in your Org-mode document as \"Google Search\",
1220 but really it would remove your entire home directory.
1221 Therefore we advise against setting this variable to nil.
1222 Just change it to `y-or-n-p' of you want to confirm with a
1223 single keystroke rather than having to type \"yes\"."
1224 :group
'org-link-follow
1226 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1227 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1228 (const :tag
"no confirmation (dangerous)" nil
)))
1230 (defcustom org-confirm-elisp-link-function
'yes-or-no-p
1231 "Non-nil means, ask for confirmation before executing Emacs Lisp links.
1232 Elisp links can be dangerous: just think about a link
1234 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1236 This link would show up in your Org-mode document as \"Google Search\",
1237 but really it would remove your entire home directory.
1238 Therefore we advise against setting this variable to nil.
1239 Just change it to `y-or-n-p' of you want to confirm with a
1240 single keystroke rather than having to type \"yes\"."
1241 :group
'org-link-follow
1243 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1244 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1245 (const :tag
"no confirmation (dangerous)" nil
)))
1247 (defconst org-file-apps-defaults-gnu
1251 "Default file applications on a UNIX or GNU/Linux system.
1252 See `org-file-apps'.")
1254 (defconst org-file-apps-defaults-macosx
1257 (system .
"open %s")
1259 ("eps.gz" .
"gv %s")
1261 ("fig" .
"xfig %s"))
1262 "Default file applications on a MacOS X system.
1263 The system \"open\" is known as a default, but we use X11 applications
1264 for some files for which the OS does not have a good default.
1265 See `org-file-apps'.")
1267 (defconst org-file-apps-defaults-windowsnt
1271 (list (if (featurep 'xemacs
)
1272 'mswindows-shell-execute
1276 (list (if (featurep 'xemacs
)
1277 'mswindows-shell-execute
1280 "Default file applications on a Windows NT system.
1281 The system \"open\" is used for most files.
1282 See `org-file-apps'.")
1284 (defcustom org-file-apps
1287 ("\\.x?html?\\'" . default
)
1288 ("\\.pdf\\'" . default
)
1290 "External applications for opening `file:path' items in a document.
1291 Org-mode uses system defaults for different file types, but
1292 you can use this variable to set the application for a given file
1293 extension. The entries in this list are cons cells where the car identifies
1294 files and the cdr the corresponding command. Possible values for the
1296 \"regex\" Regular expression matched against the file name. For backward
1297 compatibility, this can also be a string with only alphanumeric
1298 characters, which is then interpreted as an extension.
1299 `directory' Matches a directory
1300 `remote' Matches a remote file, accessible through tramp or efs.
1301 Remote files most likely should be visited through Emacs
1302 because external applications cannot handle such paths.
1303 `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
1304 so all files Emacs knows how to handle. Using this with
1305 command `emacs' will open most files in Emacs. Beware that this
1306 will also open html files inside Emacs, unless you add
1307 (\"html\" . default) to the list as well.
1308 t Default for files not matched by any of the other options.
1309 `system' The system command to open files, like `open' on Windows
1310 and Mac OS X, and mailcap under GNU/Linux. This is the command
1311 that will be selected if you call `C-c C-o' with a double
1314 Possible values for the command are:
1315 `emacs' The file will be visited by the current Emacs process.
1316 `default' Use the default application for this file type, which is the
1317 association for t in the list, most likely in the system-specific
1319 This can be used to overrule an unwanted setting in the
1320 system-specific variable.
1321 `system' Use the system command for opening files, like \"open\".
1322 This command is specified by the entry whose car is `system'.
1323 Most likely, the system-specific version of this variable
1324 does define this command, but you can overrule/replace it
1326 string A command to be executed by a shell; %s will be replaced
1327 by the path to the file.
1328 sexp A Lisp form which will be evaluated. The file path will
1329 be available in the Lisp variable `file'.
1330 For more examples, see the system specific constants
1331 `org-file-apps-defaults-macosx'
1332 `org-file-apps-defaults-windowsnt'
1333 `org-file-apps-defaults-gnu'."
1334 :group
'org-link-follow
1336 (cons (choice :value
""
1337 (string :tag
"Extension")
1338 (const :tag
"System command to open files" system
)
1339 (const :tag
"Default for unrecognized files" t
)
1340 (const :tag
"Remote file" remote
)
1341 (const :tag
"Links to a directory" directory
)
1342 (const :tag
"Any files that have Emacs modes"
1345 (const :tag
"Visit with Emacs" emacs
)
1346 (const :tag
"Use default" default
)
1347 (const :tag
"Use the system command" system
)
1348 (string :tag
"Command")
1349 (sexp :tag
"Lisp form")))))
1351 (defgroup org-refile nil
1352 "Options concerning refiling entries in Org-mode."
1356 (defcustom org-directory
"~/org"
1357 "Directory with org files.
1358 This directory will be used as default to prompt for org files.
1359 Used by the hooks for remember.el."
1361 :group
'org-remember
1364 (defcustom org-default-notes-file
(convert-standard-filename "~/.notes")
1365 "Default target for storing notes.
1366 Used by the hooks for remember.el. This can be a string, or nil to mean
1367 the value of `remember-data-file'.
1368 You can set this on a per-template basis with the variable
1369 `org-remember-templates'."
1371 :group
'org-remember
1373 (const :tag
"Default from remember-data-file" nil
)
1376 (defcustom org-goto-interface
'outline
1377 "The default interface to be used for `org-goto'.
1379 outline The interface shows an outline of the relevant file
1380 and the correct heading is found by moving through
1381 the outline or by searching with incremental search.
1382 outline-path-completion Headlines in the current buffer are offered via
1383 completion. This is the interface also used by
1384 the refile command."
1387 (const :tag
"Outline" outline
)
1388 (const :tag
"Outline-path-completion" outline-path-completion
)))
1390 (defcustom org-goto-max-level
5
1391 "Maximum level to be considered when running org-goto with refile interface."
1395 (defcustom org-reverse-note-order nil
1396 "Non-nil means, store new notes at the beginning of a file or entry.
1397 When nil, new notes will be filed to the end of a file or entry.
1398 This can also be a list with cons cells of regular expressions that
1399 are matched against file names, and values."
1400 :group
'org-remember
1403 (const :tag
"Reverse always" t
)
1404 (const :tag
"Reverse never" nil
)
1405 (repeat :tag
"By file name regexp"
1406 (cons regexp boolean
))))
1408 (defcustom org-refile-targets nil
1409 "Targets for refiling entries with \\[org-refile].
1410 This is list of cons cells. Each cell contains:
1411 - a specification of the files to be considered, either a list of files,
1412 or a symbol whose function or variable value will be used to retrieve
1413 a file name or a list of file names. If you use `org-agenda-files' for
1414 that, all agenda files will be scanned for targets. Nil means, consider
1415 headings in the current buffer.
1416 - A specification of how to select find candidate refile targets. This
1418 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
1419 This tag has to be present in all target headlines, inheritance will
1421 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
1423 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
1424 headlines that are refiling targets.
1425 - a cons cell (:level . N). Any headline of level N is considered a target.
1426 - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
1428 When this variable is nil, all top-level headlines in the current buffer
1429 are used, equivalent to the value `((nil . (:level . 1))'."
1433 (choice :value org-agenda-files
1434 (const :tag
"All agenda files" org-agenda-files
)
1435 (const :tag
"Current buffer" nil
)
1436 (function) (variable) (file))
1437 (choice :tag
"Identify target headline by"
1438 (cons :tag
"Specific tag" (const :value
:tag
) (string))
1439 (cons :tag
"TODO keyword" (const :value
:todo
) (string))
1440 (cons :tag
"Regular expression" (const :value
:regexp
) (regexp))
1441 (cons :tag
"Level number" (const :value
:level
) (integer))
1442 (cons :tag
"Max Level number" (const :value
:maxlevel
) (integer))))))
1444 (defcustom org-refile-use-outline-path nil
1445 "Non-nil means, provide refile targets as paths.
1446 So a level 3 headline will be available as level1/level2/level3.
1447 When the value is `file', also include the file name (without directory)
1448 into the path. When `full-file-path', include the full file path."
1451 (const :tag
"Not" nil
)
1452 (const :tag
"Yes" t
)
1453 (const :tag
"Start with file name" file
)
1454 (const :tag
"Start with full file path" full-file-path
)))
1456 (defcustom org-outline-path-complete-in-steps t
1457 "Non-nil means, complete the outline path in hierarchical steps.
1458 When Org-mode uses the refile interface to select an outline path
1459 \(see variable `org-refile-use-outline-path'), the completion of
1460 the path can be done is a single go, or if can be done in steps down
1461 the headline hierarchy. Going in steps is probably the best if you
1462 do not use a special completion package like `ido' or `icicles'.
1463 However, when using these packages, going in one step can be very
1464 fast, while still showing the whole path to the entry."
1468 (defgroup org-todo nil
1469 "Options concerning TODO items in Org-mode."
1473 (defgroup org-progress nil
1474 "Options concerning Progress logging in Org-mode."
1478 (defcustom org-todo-keywords
'((sequence "TODO" "DONE"))
1479 "List of TODO entry keyword sequences and their interpretation.
1480 \\<org-mode-map>This is a list of sequences.
1482 Each sequence starts with a symbol, either `sequence' or `type',
1483 indicating if the keywords should be interpreted as a sequence of
1484 action steps, or as different types of TODO items. The first
1485 keywords are states requiring action - these states will select a headline
1486 for inclusion into the global TODO list Org-mode produces. If one of
1487 the \"keywords\" is the vertical bat \"|\" the remaining keywords
1488 signify that no further action is necessary. If \"|\" is not found,
1489 the last keyword is treated as the only DONE state of the sequence.
1491 The command \\[org-todo] cycles an entry through these states, and one
1492 additional state where no keyword is present. For details about this
1493 cycling, see the manual.
1495 TODO keywords and interpretation can also be set on a per-file basis with
1496 the special #+SEQ_TODO and #+TYP_TODO lines.
1498 Each keyword can optionally specify a character for fast state selection
1499 \(in combination with the variable `org-use-fast-todo-selection')
1500 and specifiers for state change logging, using the same syntax
1501 that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
1502 that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
1503 indicates to record a time stamp each time this state is selected.
1505 Each keyword may also specify if a timestamp or a note should be
1506 recorded when entering or leaving the state, by adding additional
1507 characters in the parenthesis after the keyword. This looks like this:
1508 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
1509 record only the time of the state change. With X and Y being either
1510 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
1511 Y when leaving the state if and only if the *target* state does not
1512 define X. You may omit any of the fast-selection key or X or /Y,
1513 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
1515 For backward compatibility, this variable may also be just a list
1516 of keywords - in this case the interpretation (sequence or type) will be
1517 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
1519 :group
'org-keywords
1521 (repeat :tag
"Old syntax, just keywords"
1522 (string :tag
"Keyword"))
1523 (repeat :tag
"New syntax"
1526 :tag
"Interpretation"
1527 (const :tag
"Sequence (cycling hits every state)" sequence
)
1528 (const :tag
"Type (cycling directly to DONE)" type
))
1530 (string :tag
"Keyword"))))))
1532 (defvar org-todo-keywords-1 nil
1533 "All TODO and DONE keywords active in a buffer.")
1534 (make-variable-buffer-local 'org-todo-keywords-1
)
1535 (defvar org-todo-keywords-for-agenda nil
)
1536 (defvar org-done-keywords-for-agenda nil
)
1537 (defvar org-todo-keyword-alist-for-agenda nil
)
1538 (defvar org-tag-alist-for-agenda nil
)
1539 (defvar org-agenda-contributing-files nil
)
1540 (defvar org-not-done-keywords nil
)
1541 (make-variable-buffer-local 'org-not-done-keywords
)
1542 (defvar org-done-keywords nil
)
1543 (make-variable-buffer-local 'org-done-keywords
)
1544 (defvar org-todo-heads nil
)
1545 (make-variable-buffer-local 'org-todo-heads
)
1546 (defvar org-todo-sets nil
)
1547 (make-variable-buffer-local 'org-todo-sets
)
1548 (defvar org-todo-log-states nil
)
1549 (make-variable-buffer-local 'org-todo-log-states
)
1550 (defvar org-todo-kwd-alist nil
)
1551 (make-variable-buffer-local 'org-todo-kwd-alist
)
1552 (defvar org-todo-key-alist nil
)
1553 (make-variable-buffer-local 'org-todo-key-alist
)
1554 (defvar org-todo-key-trigger nil
)
1555 (make-variable-buffer-local 'org-todo-key-trigger
)
1557 (defcustom org-todo-interpretation
'sequence
1558 "Controls how TODO keywords are interpreted.
1559 This variable is in principle obsolete and is only used for
1560 backward compatibility, if the interpretation of todo keywords is
1561 not given already in `org-todo-keywords'. See that variable for
1564 :group
'org-keywords
1565 :type
'(choice (const sequence
)
1568 (defcustom org-use-fast-todo-selection
'prefix
1569 "Non-nil means, use the fast todo selection scheme with C-c C-t.
1570 This variable describes if and under what circumstances the cycling
1571 mechanism for TODO keywords will be replaced by a single-key, direct
1574 When nil, fast selection is never used.
1576 When the symbol `prefix', it will be used when `org-todo' is called with
1577 a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
1578 in an agenda buffer.
1580 When t, fast selection is used by default. In this case, the prefix
1581 argument forces cycling instead.
1583 In all cases, the special interface is only used if access keys have actually
1584 been assigned by the user, i.e. if keywords in the configuration are followed
1585 by a letter in parenthesis, like TODO(t)."
1588 (const :tag
"Never" nil
)
1589 (const :tag
"By default" t
)
1590 (const :tag
"Only with C-u C-c C-t" prefix
)))
1592 (defcustom org-provide-todo-statistics t
1593 "Non-nil means, update todo statistics after insert and toggle.
1594 When this is set, todo statistics is updated in the parent of the current
1595 entry each time a todo state is changed."
1599 (defcustom org-after-todo-state-change-hook nil
1600 "Hook which is run after the state of a TODO item was changed.
1601 The new state (a string with a TODO keyword, or nil) is available in the
1602 Lisp variable `state'."
1606 (defvar org-blocker-hook nil
1607 "Hook for functions that are allowed to block a state change.
1609 Each function gets as its single argument a property list, see
1610 `org-trigger-hook' for more information about this list.
1612 If any of the functions in this hook returns nil, the state change
1615 (defvar org-trigger-hook nil
1616 "Hook for functions that are triggered by a state change.
1618 Each function gets as its single argument a property list with at least
1619 the following elements:
1621 (:type type-of-change :position pos-at-entry-start
1622 :from old-state :to new-state)
1624 Depending on the type, more properties may be present.
1626 This mechanism is currently implemented for:
1630 :type todo-state-change
1631 :from previous state (keyword as a string), or nil, or a symbol
1632 'todo' or 'done', to indicate the general type of state.
1633 :to new state, like in :from")
1635 (defcustom org-enforce-todo-dependencies nil
1636 "Non-nil means, undone TODO entries will block switching the parent to DONE.
1637 Also, if a parent has an :ORDERED: property, switching an entry to DONE will
1638 be blocked if any prior sibling is not yet done.
1639 You need to set this variable through the customize interface, or to
1640 restart emacs after changing the value."
1641 :set
(lambda (var val
)
1644 (add-hook 'org-blocker-hook
'org-block-todo-from-children-or-siblings
)
1645 (remove-hook 'org-blocker-hook
'org-block-todo-from-children-or-siblings
)))
1649 (defcustom org-todo-state-tags-triggers nil
1650 "Tag changes that should be triggered by TODO state changes.
1651 This is a list. Each entry is
1653 (state-change (tag . flag) .......)
1655 State-change can be a string with a state, and empty string to indicate the
1656 state that has no TODO keyword, or it can be one of the symbols `todo'
1657 or `done', meaning any not-done or done state, respectively."
1661 (cons (choice :tag
"When changing to"
1662 (const :tag
"Not-done state" todo
)
1663 (const :tag
"Done state" done
)
1664 (string :tag
"State"))
1666 (cons :tag
"Tag action"
1668 (choice (const :tag
"Add" t
) (const :tag
"Remove" nil
)))))))
1670 (defcustom org-log-done nil
1671 "Information to record when a task moves to the DONE state.
1673 Possible values are:
1675 nil Don't add anything, just change the keyword
1676 time Add a time stamp to the task
1677 note Prompt a closing note and add it with template `org-log-note-headings'
1679 This option can also be set with on a per-file-basis with
1681 #+STARTUP: nologdone
1683 #+STARTUP: lognotedone
1685 You can have local logging settings for a subtree by setting the LOGGING
1686 property to one or more of these keywords."
1688 :group
'org-progress
1690 (const :tag
"No logging" nil
)
1691 (const :tag
"Record CLOSED timestamp" time
)
1692 (const :tag
"Record CLOSED timestamp with closing note." note
)))
1694 ;; Normalize old uses of org-log-done.
1696 ((eq org-log-done t
) (setq org-log-done
'time
))
1697 ((and (listp org-log-done
) (memq 'done org-log-done
))
1698 (setq org-log-done
'note
)))
1700 (defcustom org-log-note-clock-out nil
1701 "Non-nil means, record a note when clocking out of an item.
1702 This can also be configured on a per-file basis by adding one of
1703 the following lines anywhere in the buffer:
1705 #+STARTUP: lognoteclock-out
1706 #+STARTUP: nolognoteclock-out"
1708 :group
'org-progress
1711 (defcustom org-log-done-with-time t
1712 "Non-nil means, the CLOSED time stamp will contain date and time.
1713 When nil, only the date will be recorded."
1714 :group
'org-progress
1717 (defcustom org-log-note-headings
1718 '((done .
"CLOSING NOTE %t")
1719 (state .
"State %-12s %t")
1720 (note .
"Note taken on %t")
1722 "Headings for notes added to entries.
1723 The value is an alist, with the car being a symbol indicating the note
1724 context, and the cdr is the heading to be used. The heading may also be the
1726 %t in the heading will be replaced by a time stamp.
1727 %s will be replaced by the new TODO state, in double quotes.
1728 %u will be replaced by the user name.
1729 %U will be replaced by the full user name."
1731 :group
'org-progress
1732 :type
'(list :greedy t
1733 (cons (const :tag
"Heading when closing an item" done
) string
)
1735 "Heading when changing todo state (todo sequence only)"
1737 (cons (const :tag
"Heading when just taking a note" note
) string
)
1738 (cons (const :tag
"Heading when clocking out" clock-out
) string
)))
1740 (unless (assq 'note org-log-note-headings
)
1741 (push '(note .
"%t") org-log-note-headings
))
1743 (defcustom org-log-state-notes-insert-after-drawers nil
1744 "Non-nil means, insert state change notes after any drawers in entry.
1745 Only the drawers that *immediately* follow the headline and the
1746 deadline/scheduled line are skipped.
1747 When nil, insert notes right after the heading and perhaps the line
1748 with deadline/scheduling if present."
1750 :group
'org-progress
1753 (defcustom org-log-states-order-reversed t
1754 "Non-nil means, the latest state change note will be directly after heading.
1755 When nil, the notes will be orderer according to time."
1757 :group
'org-progress
1760 (defcustom org-log-repeat
'time
1761 "Non-nil means, record moving through the DONE state when triggering repeat.
1762 An auto-repeating tasks is immediately switched back to TODO when marked
1763 done. If you are not logging state changes (by adding \"@\" or \"!\" to
1764 the TODO keyword definition, or recording a closing note by setting
1765 `org-log-done', there will be no record of the task moving through DONE.
1766 This variable forces taking a note anyway. Possible values are:
1768 nil Don't force a record
1769 time Record a time stamp
1772 This option can also be set with on a per-file-basis with
1774 #+STARTUP: logrepeat
1775 #+STARTUP: lognoterepeat
1776 #+STARTUP: nologrepeat
1778 You can have local logging settings for a subtree by setting the LOGGING
1779 property to one or more of these keywords."
1781 :group
'org-progress
1783 (const :tag
"Don't force a record" nil
)
1784 (const :tag
"Force recording the DONE state" time
)
1785 (const :tag
"Force recording a note with the DONE state" note
)))
1788 (defgroup org-priorities nil
1789 "Priorities in Org-mode."
1790 :tag
"Org Priorities"
1793 (defcustom org-highest-priority ?A
1794 "The highest priority of TODO items. A character like ?A, ?B etc.
1795 Must have a smaller ASCII number than `org-lowest-priority'."
1796 :group
'org-priorities
1799 (defcustom org-lowest-priority ?C
1800 "The lowest priority of TODO items. A character like ?A, ?B etc.
1801 Must have a larger ASCII number than `org-highest-priority'."
1802 :group
'org-priorities
1805 (defcustom org-default-priority ?B
1806 "The default priority of TODO items.
1807 This is the priority an item get if no explicit priority is given."
1808 :group
'org-priorities
1811 (defcustom org-priority-start-cycle-with-default t
1812 "Non-nil means, start with default priority when starting to cycle.
1813 When this is nil, the first step in the cycle will be (depending on the
1814 command used) one higher or lower that the default priority."
1815 :group
'org-priorities
1818 (defgroup org-time nil
1819 "Options concerning time stamps and deadlines in Org-mode."
1823 (defcustom org-insert-labeled-timestamps-at-point nil
1824 "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
1825 When nil, these labeled time stamps are forces into the second line of an
1826 entry, just after the headline. When scheduling from the global TODO list,
1827 the time stamp will always be forced into the second line."
1831 (defconst org-time-stamp-formats
'("<%Y-%m-%d %a>" .
"<%Y-%m-%d %a %H:%M>")
1832 "Formats for `format-time-string' which are used for time stamps.
1833 It is not recommended to change this constant.")
1835 (defcustom org-time-stamp-rounding-minutes
'(0 5)
1836 "Number of minutes to round time stamps to.
1837 These are two values, the first applies when first creating a time stamp.
1838 The second applies when changing it with the commands `S-up' and `S-down'.
1839 When changing the time stamp, this means that it will change in steps
1840 of N minutes, as given by the second value.
1842 When a setting is 0 or 1, insert the time unmodified. Useful rounding
1843 numbers should be factors of 60, so for example 5, 10, 15.
1845 When this is larger than 1, you can still force an exact time-stamp by using
1846 a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
1847 and by using a prefix arg to `S-up/down' to specify the exact number
1848 of minutes to shift."
1850 :get
'(lambda (var) ; Make sure all entries have 5 elements
1851 (if (integerp (default-value var
))
1852 (list (default-value var
) 5)
1853 (default-value var
)))
1855 (integer :tag
"when inserting times")
1856 (integer :tag
"when modifying times")))
1858 ;; Normalize old customizations of this variable.
1859 (when (integerp org-time-stamp-rounding-minutes
)
1860 (setq org-time-stamp-rounding-minutes
1861 (list org-time-stamp-rounding-minutes
1862 org-time-stamp-rounding-minutes
)))
1864 (defcustom org-display-custom-times nil
1865 "Non-nil means, overlay custom formats over all time stamps.
1866 The formats are defined through the variable `org-time-stamp-custom-formats'.
1867 To turn this on on a per-file basis, insert anywhere in the file:
1868 #+STARTUP: customtime"
1872 (make-variable-buffer-local 'org-display-custom-times
)
1874 (defcustom org-time-stamp-custom-formats
1875 '("<%m/%d/%y %a>" .
"<%m/%d/%y %a %H:%M>") ; american
1876 "Custom formats for time stamps. See `format-time-string' for the syntax.
1877 These are overlayed over the default ISO format if the variable
1878 `org-display-custom-times' is set. Time like %H:%M should be at the
1879 end of the second format."
1883 (defun org-time-stamp-format (&optional long inactive
)
1884 "Get the right format for a time string."
1885 (let ((f (if long
(cdr org-time-stamp-formats
)
1886 (car org-time-stamp-formats
))))
1888 (concat "[" (substring f
1 -
1) "]")
1891 (defcustom org-time-clocksum-format
"%d:%02d"
1892 "The format string used when creating CLOCKSUM lines, or when
1893 org-mode generates a time duration."
1897 (defcustom org-deadline-warning-days
14
1898 "No. of days before expiration during which a deadline becomes active.
1899 This variable governs the display in sparse trees and in the agenda.
1900 When 0 or negative, it means use this number (the absolute value of it)
1901 even if a deadline has a different individual lead time specified."
1903 :group
'org-agenda-daily
/weekly
1906 (defcustom org-read-date-prefer-future t
1907 "Non-nil means, assume future for incomplete date input from user.
1908 This affects the following situations:
1909 1. The user gives a day, but no month.
1910 For example, if today is the 15th, and you enter \"3\", Org-mode will
1911 read this as the third of *next* month. However, if you enter \"17\",
1912 it will be considered as *this* month.
1913 2. The user gives a month but not a year.
1914 For example, if it is april and you enter \"feb 2\", this will be read
1915 as feb 2, *next* year. \"May 5\", however, will be this year.
1917 Currently this does not work for ISO week specifications.
1919 When this option is nil, the current month and year will always be used
1924 (defcustom org-read-date-display-live t
1925 "Non-nil means, display current interpretation of date prompt live.
1926 This display will be in an overlay, in the minibuffer."
1930 (defcustom org-read-date-popup-calendar t
1931 "Non-nil means, pop up a calendar when prompting for a date.
1932 In the calendar, the date can be selected with mouse-1. However, the
1933 minibuffer will also be active, and you can simply enter the date as well.
1934 When nil, only the minibuffer will be available."
1937 (if (fboundp 'defvaralias
)
1938 (defvaralias 'org-popup-calendar-for-date-prompt
1939 'org-read-date-popup-calendar
))
1941 (defcustom org-extend-today-until
0
1942 "The hour when your day really ends. Must be an integer.
1943 This has influence for the following applications:
1944 - When switching the agenda to \"today\". It it is still earlier than
1945 the time given here, the day recognized as TODAY is actually yesterday.
1946 - When a date is read from the user and it is still before the time given
1947 here, the current date and time will be assumed to be yesterday, 23:59.
1948 Also, timestamps inserted in remember templates follow this rule.
1950 IMPORTANT: This is a feature whose implementation is and likely will
1951 remain incomplete. Really, it is only here because past midnight seems to
1952 be the favorite working time of John Wiegley :-)"
1956 (defcustom org-edit-timestamp-down-means-later nil
1957 "Non-nil means, S-down will increase the time in a time stamp.
1958 When nil, S-up will increase."
1962 (defcustom org-calendar-follow-timestamp-change t
1963 "Non-nil means, make the calendar window follow timestamp changes.
1964 When a timestamp is modified and the calendar window is visible, it will be
1965 moved to the new date."
1969 (defgroup org-tags nil
1970 "Options concerning tags in Org-mode."
1974 (defcustom org-tag-alist nil
1975 "List of tags allowed in Org-mode files.
1976 When this list is nil, Org-mode will base TAG input on what is already in the
1978 The value of this variable is an alist, the car of each entry must be a
1979 keyword as a string, the cdr may be a character that is used to select
1980 that tag through the fast-tag-selection interface.
1981 See the manual for details."
1985 (cons (string :tag
"Tag name")
1986 (character :tag
"Access char"))
1987 (const :tag
"Start radio group" (:startgroup
))
1988 (const :tag
"End radio group" (:endgroup
)))))
1990 (defvar org-file-tags nil
1991 "List of tags that can be inherited by all entries in the file.
1992 The tags will be inherited if the variable `org-use-tag-inheritance'
1993 says they should be.
1994 This variable is populated from #+TAG lines.")
1996 (defcustom org-use-fast-tag-selection
'auto
1997 "Non-nil means, use fast tag selection scheme.
1998 This is a special interface to select and deselect tags with single keys.
1999 When nil, fast selection is never used.
2000 When the symbol `auto', fast selection is used if and only if selection
2001 characters for tags have been configured, either through the variable
2002 `org-tag-alist' or through a #+TAGS line in the buffer.
2003 When t, fast selection is always used and selection keys are assigned
2004 automatically if necessary."
2007 (const :tag
"Always" t
)
2008 (const :tag
"Never" nil
)
2009 (const :tag
"When selection characters are configured" 'auto
)))
2011 (defcustom org-fast-tag-selection-single-key nil
2012 "Non-nil means, fast tag selection exits after first change.
2013 When nil, you have to press RET to exit it.
2014 During fast tag selection, you can toggle this flag with `C-c'.
2015 This variable can also have the value `expert'. In this case, the window
2016 displaying the tags menu is not even shown, until you press C-c again."
2019 (const :tag
"No" nil
)
2020 (const :tag
"Yes" t
)
2021 (const :tag
"Expert" expert
)))
2023 (defvar org-fast-tag-selection-include-todo nil
2024 "Non-nil means, fast tags selection interface will also offer TODO states.
2025 This is an undocumented feature, you should not rely on it.")
2027 (defcustom org-tags-column
(if (featurep 'xemacs
) -
79 -
80)
2028 "The column to which tags should be indented in a headline.
2029 If this number is positive, it specifies the column. If it is negative,
2030 it means that the tags should be flushright to that column. For example,
2031 -80 works well for a normal 80 character screen."
2035 (defcustom org-auto-align-tags t
2036 "Non-nil means, realign tags after pro/demotion of TODO state change.
2037 These operations change the length of a headline and therefore shift
2038 the tags around. With this options turned on, after each such operation
2039 the tags are again aligned to `org-tags-column'."
2043 (defcustom org-use-tag-inheritance t
2044 "Non-nil means, tags in levels apply also for sublevels.
2045 When nil, only the tags directly given in a specific line apply there.
2046 This may also be a list of tags that should be inherited, or a regexp that
2047 matches tags that should be inherited. Additional control is possible
2048 with the variable `org-tags-exclude-from-inheritance' which gives an
2049 explicit list of tags to be excluded from inheritance., even if the value of
2050 `org-use-tag-inheritance' would select it for inheritance.
2052 If this option is t, a match early-on in a tree can lead to a large
2053 number of matches in the subtree when constructing the agenda or creating
2054 a sparse tree. If you only want to see the first match in a tree during
2055 a search, check out the variable `org-tags-match-list-sublevels'."
2058 (const :tag
"Not" nil
)
2059 (const :tag
"Always" t
)
2060 (repeat :tag
"Specific tags" (string :tag
"Tag"))
2061 (regexp :tag
"Tags matched by regexp")))
2063 (defcustom org-tags-exclude-from-inheritance nil
2064 "List of tags that should never be inherited.
2065 This is a way to exclude a few tags from inheritance. For way to do
2066 the opposite, to actively allow inheritance for selected tags,
2067 see the variable `org-use-tag-inheritance'."
2069 :type
'(repeat (string :tag
"Tag")))
2071 (defun org-tag-inherit-p (tag)
2072 "Check if TAG is one that should be inherited."
2074 ((member tag org-tags-exclude-from-inheritance
) nil
)
2075 ((eq org-use-tag-inheritance t
) t
)
2076 ((not org-use-tag-inheritance
) nil
)
2077 ((stringp org-use-tag-inheritance
)
2078 (string-match org-use-tag-inheritance tag
))
2079 ((listp org-use-tag-inheritance
)
2080 (member tag org-use-tag-inheritance
))
2081 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
2083 (defcustom org-tags-match-list-sublevels t
2084 "Non-nil means list also sublevels of headlines matching tag search.
2085 Because of tag inheritance (see variable `org-use-tag-inheritance'),
2086 the sublevels of a headline matching a tag search often also match
2087 the same search. Listing all of them can create very long lists.
2088 Setting this variable to nil causes subtrees of a match to be skipped.
2089 This option is off by default, because inheritance in on. If you turn
2090 inheritance off, you very likely want to turn this option on.
2092 As a special case, if the tag search is restricted to TODO items, the
2093 value of this variable is ignored and sublevels are always checked, to
2094 make sure all corresponding TODO items find their way into the list.
2096 This variable is semi-obsolete and probably should always be true. It
2097 is better to limit inheritance to certain tags using the variables
2098 `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
2102 (defvar org-tags-history nil
2103 "History of minibuffer reads for tags.")
2104 (defvar org-last-tags-completion-table nil
2105 "The last used completion table for tags.")
2106 (defvar org-after-tags-change-hook nil
2107 "Hook that is run after the tags in a line have changed.")
2109 (defgroup org-properties nil
2110 "Options concerning properties in Org-mode."
2111 :tag
"Org Properties"
2114 (defcustom org-property-format
"%-10s %s"
2115 "How property key/value pairs should be formatted by `indent-line'.
2116 When `indent-line' hits a property definition, it will format the line
2117 according to this format, mainly to make sure that the values are
2118 lined-up with respect to each other."
2119 :group
'org-properties
2122 (defcustom org-use-property-inheritance nil
2123 "Non-nil means, properties apply also for sublevels.
2125 This setting is chiefly used during property searches. Turning it on can
2126 cause significant overhead when doing a search, which is why it is not
2129 When nil, only the properties directly given in the current entry count.
2130 When t, every property is inherited. The value may also be a list of
2131 properties that should have inheritance, or a regular expression matching
2132 properties that should be inherited.
2134 However, note that some special properties use inheritance under special
2135 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
2136 and the properties ending in \"_ALL\" when they are used as descriptor
2137 for valid values of a property.
2139 Note for programmers:
2140 When querying an entry with `org-entry-get', you can control if inheritance
2141 should be used. By default, `org-entry-get' looks only at the local
2142 properties. You can request inheritance by setting the inherit argument
2143 to t (to force inheritance) or to `selective' (to respect the setting
2145 :group
'org-properties
2147 (const :tag
"Not" nil
)
2148 (const :tag
"Always" t
)
2149 (repeat :tag
"Specific properties" (string :tag
"Property"))
2150 (regexp :tag
"Properties matched by regexp")))
2152 (defun org-property-inherit-p (property)
2153 "Check if PROPERTY is one that should be inherited."
2155 ((eq org-use-property-inheritance t
) t
)
2156 ((not org-use-property-inheritance
) nil
)
2157 ((stringp org-use-property-inheritance
)
2158 (string-match org-use-property-inheritance property
))
2159 ((listp org-use-property-inheritance
)
2160 (member property org-use-property-inheritance
))
2161 (t (error "Invalid setting of `org-use-property-inheritance'"))))
2163 (defcustom org-columns-default-format
"%25ITEM %TODO %3PRIORITY %TAGS"
2164 "The default column format, if no other format has been defined.
2165 This variable can be set on the per-file basis by inserting a line
2167 #+COLUMNS: %25ITEM ....."
2168 :group
'org-properties
2171 (defcustom org-columns-ellipses
".."
2172 "The ellipses to be used when a field in column view is truncated.
2173 When this is the empty string, as many characters as possible are shown,
2174 but then there will be no visual indication that the field has been truncated.
2175 When this is a string of length N, the last N characters of a truncated
2176 field are replaced by this string. If the column is narrower than the
2177 ellipses string, only part of the ellipses string will be shown."
2178 :group
'org-properties
2181 (defcustom org-columns-modify-value-for-display-function nil
2182 "Function that modifies values for display in column view.
2183 For example, it can be used to cut out a certain part from a time stamp.
2184 The function must take 2 arguments:
2186 column-title The title of the column (*not* the property name)
2187 value The value that should be modified.
2189 The function should return the value that should be displayed,
2190 or nil if the normal value should be used."
2191 :group
'org-properties
2194 (defcustom org-effort-property
"Effort"
2195 "The property that is being used to keep track of effort estimates.
2196 Effort estimates given in this property need to have the format H:MM."
2197 :group
'org-properties
2198 :group
'org-progress
2199 :type
'(string :tag
"Property"))
2201 (defconst org-global-properties-fixed
2202 '(("VISIBILITY_ALL" .
"folded children content all"))
2203 "List of property/value pairs that can be inherited by any entry.
2204 These are fixed values, for the preset properties.")
2207 (defcustom org-global-properties nil
2208 "List of property/value pairs that can be inherited by any entry.
2209 You can set buffer-local values for the same purpose in the variable
2210 `org-file-properties' this by adding lines like
2212 #+PROPERTY: NAME VALUE"
2213 :group
'org-properties
2215 (cons (string :tag
"Property")
2216 (string :tag
"Value"))))
2218 (defvar org-file-properties nil
2219 "List of property/value pairs that can be inherited by any entry.
2220 Valid for the current buffer.
2221 This variable is populated from #+PROPERTY lines.")
2222 (make-variable-buffer-local 'org-file-properties
)
2224 (defgroup org-agenda nil
2225 "Options concerning agenda views in Org-mode."
2229 (defvar org-category nil
2230 "Variable used by org files to set a category for agenda display.
2231 Such files should use a file variable to set it, for example
2233 # -*- mode: org; org-category: \"ELisp\"
2235 or contain a special line
2239 If the file does not specify a category, then file's base name
2241 (make-variable-buffer-local 'org-category
)
2242 (put 'org-category
'safe-local-variable
'(lambda (x) (or (symbolp x
) (stringp x
))))
2244 (defcustom org-agenda-files nil
2245 "The files to be used for agenda display.
2246 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
2247 \\[org-remove-file]. You can also use customize to edit the list.
2249 If an entry is a directory, all files in that directory that are matched by
2250 `org-agenda-file-regexp' will be part of the file list.
2252 If the value of the variable is not a list but a single file name, then
2253 the list of agenda files is actually stored and maintained in that file, one
2254 agenda file per line."
2257 (repeat :tag
"List of files and directories" file
)
2258 (file :tag
"Store list in a file\n" :value
"~/.agenda_files")))
2260 (defcustom org-agenda-file-regexp
"\\`[^.].*\\.org\\'"
2261 "Regular expression to match files for `org-agenda-files'.
2262 If any element in the list in that variable contains a directory instead
2263 of a normal file, all files in that directory that are matched by this
2264 regular expression will be included."
2268 (defcustom org-agenda-text-search-extra-files nil
2269 "List of extra files to be searched by text search commands.
2270 These files will be search in addition to the agenda files by the
2271 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
2272 Note that these files will only be searched for text search commands,
2273 not for the other agenda views like todo lists, tag searches or the weekly
2274 agenda. This variable is intended to list notes and possibly archive files
2275 that should also be searched by these two commands.
2276 In fact, if the first element in the list is the symbol `agenda-archives',
2277 than all archive files of all agenda files will be added to the search
2280 :type
'(set :greedy t
2281 (const :tag
"Agenda Archives" agenda-archives
)
2282 (repeat :inline t
(file))))
2284 (if (fboundp 'defvaralias
)
2285 (defvaralias 'org-agenda-multi-occur-extra-files
2286 'org-agenda-text-search-extra-files
))
2288 (defcustom org-agenda-skip-unavailable-files nil
2289 "Non-nil means to just skip non-reachable files in `org-agenda-files'.
2290 A nil value means to remove them, after a query, from the list."
2294 (defcustom org-calendar-to-agenda-key
[?c
]
2295 "The key to be installed in `calendar-mode-map' for switching to the agenda.
2296 The command `org-calendar-goto-agenda' will be bound to this key. The
2297 default is the character `c' because then `c' can be used to switch back and
2298 forth between agenda and calendar."
2302 (defcustom org-calendar-agenda-action-key
[?k
]
2303 "The key to be installed in `calendar-mode-map' for agenda-action.
2304 The command `org-agenda-action' will be bound to this key. The
2305 default is the character `k' because we use the same key in the agenda."
2309 (eval-after-load "calendar"
2311 (org-defkey calendar-mode-map org-calendar-to-agenda-key
2312 'org-calendar-goto-agenda
)
2313 (org-defkey calendar-mode-map org-calendar-agenda-action-key
2314 'org-agenda-action
)))
2316 (defgroup org-latex nil
2317 "Options for embedding LaTeX code into Org-mode."
2321 (defcustom org-format-latex-options
2322 '(:foreground default
:background default
:scale
1.0
2323 :html-foreground
"Black" :html-background
"Transparent" :html-scale
1.0
2324 :matchers
("begin" "$1" "$" "$$" "\\(" "\\["))
2325 "Options for creating images from LaTeX fragments.
2326 This is a property list with the following properties:
2327 :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
2328 `default' means use the foreground of the default face.
2329 :background the background color, or \"Transparent\".
2330 `default' means use the background of the default face.
2331 :scale a scaling factor for the size of the images.
2332 :html-foreground, :html-background, :html-scale
2333 the same numbers for HTML export.
2334 :matchers a list indicating which matchers should be used to
2335 find LaTeX fragments. Valid members of this list are:
2336 \"begin\" find environments
2337 \"$1\" find single characters surrounded by $.$
2338 \"$\" find math expressions surrounded by $...$
2339 \"$$\" find math expressions surrounded by $$....$$
2340 \"\\(\" find math expressions surrounded by \\(...\\)
2341 \"\\ [\" find math expressions surrounded by \\ [...\\]"
2345 (defcustom org-format-latex-header
"\\documentclass{article}
2346 \\usepackage{fullpage} % do not remove
2347 \\usepackage{amssymb}
2348 \\usepackage[usenames]{color}
2349 \\usepackage{amsmath}
2350 \\usepackage{latexsym}
2351 \\usepackage[mathscr]{eucal}
2352 \\pagestyle{empty} % do not remove"
2353 "The document header used for processing LaTeX fragments."
2358 (defgroup org-font-lock nil
2359 "Font-lock settings for highlighting in Org-mode."
2360 :tag
"Org Font Lock"
2363 (defcustom org-level-color-stars-only nil
2364 "Non-nil means fontify only the stars in each headline.
2365 When nil, the entire headline is fontified.
2366 Changing it requires restart of `font-lock-mode' to become effective
2367 also in regions already fontified."
2368 :group
'org-font-lock
2371 (defcustom org-hide-leading-stars nil
2372 "Non-nil means, hide the first N-1 stars in a headline.
2373 This works by using the face `org-hide' for these stars. This
2374 face is white for a light background, and black for a dark
2375 background. You may have to customize the face `org-hide' to
2377 Changing it requires restart of `font-lock-mode' to become effective
2378 also in regions already fontified.
2379 You may also set this on a per-file basis by adding one of the following
2380 lines to the buffer:
2382 #+STARTUP: hidestars
2383 #+STARTUP: showstars"
2384 :group
'org-font-lock
2387 (defcustom org-fontify-done-headline nil
2388 "Non-nil means, change the face of a headline if it is marked DONE.
2389 Normally, only the TODO/DONE keyword indicates the state of a headline.
2390 When this is non-nil, the headline after the keyword is set to the
2391 `org-headline-done' as an additional indication."
2392 :group
'org-font-lock
2395 (defcustom org-fontify-emphasized-text t
2396 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
2397 Changing this variable requires a restart of Emacs to take effect."
2398 :group
'org-font-lock
2401 (defcustom org-highlight-latex-fragments-and-specials nil
2402 "Non-nil means, fontify what is treated specially by the exporters."
2403 :group
'org-font-lock
2406 (defcustom org-hide-emphasis-markers nil
2407 "Non-nil mean font-lock should hide the emphasis marker characters."
2408 :group
'org-font-lock
2411 (defvar org-emph-re nil
2412 "Regular expression for matching emphasis.")
2413 (defvar org-verbatim-re nil
2414 "Regular expression for matching verbatim text.")
2415 (defvar org-emphasis-regexp-components
) ; defined just below
2416 (defvar org-emphasis-alist
) ; defined just below
2417 (defun org-set-emph-re (var val
)
2418 "Set variable and compute the emphasis regular expression."
2420 (when (and (boundp 'org-emphasis-alist
)
2421 (boundp 'org-emphasis-regexp-components
)
2422 org-emphasis-alist org-emphasis-regexp-components
)
2423 (let* ((e org-emphasis-regexp-components
)
2429 (body1 (concat body
"*?"))
2430 (markers (mapconcat 'car org-emphasis-alist
""))
2431 (vmarkers (mapconcat
2432 (lambda (x) (if (eq (nth 4 x
) 'verbatim
) (car x
) ""))
2433 org-emphasis-alist
"")))
2434 ;; make sure special characters appear at the right position in the class
2435 (if (string-match "\\^" markers
)
2436 (setq markers
(concat (replace-match "" t t markers
) "^")))
2437 (if (string-match "-" markers
)
2438 (setq markers
(concat (replace-match "" t t markers
) "-")))
2439 (if (string-match "\\^" vmarkers
)
2440 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "^")))
2441 (if (string-match "-" vmarkers
)
2442 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "-")))
2444 (setq body1
(concat body1
"\\(?:\n" body
"*?\\)\\{0,"
2445 (int-to-string nl
) "\\}")))
2448 (concat "\\([" pre
"]\\|^\\)"
2450 "\\([" markers
"]\\)"
2458 "\\([" post
"]\\|$\\)"))
2459 (setq org-verbatim-re
2460 (concat "\\([" pre
"]\\|^\\)"
2462 "\\([" vmarkers
"]\\)"
2470 "\\([" post
"]\\|$\\)")))))
2472 (defcustom org-emphasis-regexp-components
2473 '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
2474 "Components used to build the regular expression for emphasis.
2475 This is a list with 6 entries. Terminology: In an emphasis string
2476 like \" *strong word* \", we call the initial space PREMATCH, the final
2477 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
2478 and \"trong wor\" is the body. The different components in this variable
2479 specify what is allowed/forbidden in each part:
2481 pre Chars allowed as prematch. Beginning of line will be allowed too.
2482 post Chars allowed as postmatch. End of line will be allowed too.
2483 border The chars *forbidden* as border characters.
2484 body-regexp A regexp like \".\" to match a body character. Don't use
2485 non-shy groups here, and don't allow newline here.
2486 newline The maximum number of newlines allowed in an emphasis exp.
2488 Use customize to modify this, or restart Emacs after changing it."
2489 :group
'org-font-lock
2490 :set
'org-set-emph-re
2492 (sexp :tag
"Allowed chars in pre ")
2493 (sexp :tag
"Allowed chars in post ")
2494 (sexp :tag
"Forbidden chars in border ")
2495 (sexp :tag
"Regexp for body ")
2496 (integer :tag
"number of newlines allowed")
2497 (option (boolean :tag
"Please ignore this button"))))
2499 (defcustom org-emphasis-alist
2500 `(("*" bold
"<b>" "</b>")
2501 ("/" italic
"<i>" "</i>")
2502 ("_" underline
"<span style=\"text-decoration:underline;\">" "</span>")
2503 ("=" org-code
"<code>" "</code>" verbatim
)
2504 ("~" org-verbatim
"<code>" "</code>" verbatim
)
2505 ("+" ,(if (featurep 'xemacs
) 'org-table
'(:strike-through t
))
2508 "Special syntax for emphasized text.
2509 Text starting and ending with a special character will be emphasized, for
2510 example *bold*, _underlined_ and /italic/. This variable sets the marker
2511 characters, the face to be used by font-lock for highlighting in Org-mode
2512 Emacs buffers, and the HTML tags to be used for this.
2513 Use customize to modify this, or restart Emacs after changing it."
2514 :group
'org-font-lock
2515 :set
'org-set-emph-re
2518 (string :tag
"Marker character")
2520 (face :tag
"Font-lock-face")
2521 (plist :tag
"Face property list"))
2522 (string :tag
"HTML start tag")
2523 (string :tag
"HTML end tag")
2524 (option (const verbatim
)))))
2526 ;;; Miscellaneous options
2528 (defgroup org-completion nil
2529 "Completion in Org-mode."
2530 :tag
"Org Completion"
2533 (defcustom org-completion-use-ido nil
2534 "Non-nil means, use ido completion wherever possible.
2535 Note that `ido-mode' must be active for this variable to be relevant.
2536 If you decide to turn this variable on, you might well want to turn off
2537 `org-outline-path-complete-in-steps'."
2538 :group
'org-completion
2541 (defcustom org-completion-fallback-command
'hippie-expand
2542 "The expansion command called by \\[org-complete] in normal context.
2543 Normal means, no org-mode-specific context."
2544 :group
'org-completion
2547 ;;; Functions and variables from ther packages
2548 ;; Declared here to avoid compiler warnings
2551 (defvar outline-mode-menu-heading
)
2552 (defvar outline-mode-menu-show
)
2553 (defvar outline-mode-menu-hide
)
2554 (defvar zmacs-regions
) ; XEmacs regions
2557 (defvar mark-active
)
2560 (declare-function calendar-absolute-from-iso
"cal-iso" (date))
2561 (declare-function calendar-forward-day
"cal-move" (arg))
2562 (declare-function calendar-goto-date
"cal-move" (date))
2563 (declare-function calendar-goto-today
"cal-move" ())
2564 (declare-function calendar-iso-from-absolute
"cal-iso" (date))
2565 (defvar calc-embedded-close-formula
)
2566 (defvar calc-embedded-open-formula
)
2567 (declare-function cdlatex-tab
"ext:cdlatex" ())
2568 (declare-function dired-get-filename
"dired" (&optional localp no-error-if-not-filep
))
2569 (defvar font-lock-unfontify-region-function
)
2570 (declare-function iswitchb-mode
"iswitchb" (&optional arg
))
2571 (declare-function iswitchb-read-buffer
(prompt &optional default require-match start matches-set
))
2572 (defvar iswitchb-temp-buflist
)
2573 (declare-function org-gnus-follow-link
"org-gnus" (&optional group article
))
2574 (defvar org-agenda-tags-todo-honor-ignore-options
)
2575 (declare-function org-agenda-skip
"org-agenda" ())
2576 (declare-function org-format-agenda-item
"org-agenda"
2577 (extra txt
&optional category tags dotime noprefix remove-re
))
2578 (declare-function org-agenda-new-marker
"org-agenda" (&optional pos
))
2579 (declare-function org-agenda-change-all-lines
"org-agenda"
2580 (newhead hdmarker
&optional fixface just-this
))
2581 (declare-function org-agenda-set-restriction-lock
"org-agenda" (&optional type
))
2582 (declare-function org-agenda-maybe-redo
"org-agenda" ())
2583 (declare-function org-agenda-save-markers-for-cut-and-paste
"org-agenda"
2585 (declare-function org-agenda-copy-local-variable
"org-agenda" (var))
2586 (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
2587 "org-agenda" (&optional end
))
2589 (declare-function parse-time-string
"parse-time" (string))
2590 (declare-function remember
"remember" (&optional initial
))
2591 (declare-function remember-buffer-desc
"remember" ())
2592 (declare-function remember-finalize
"remember" ())
2593 (defvar remember-save-after-remembering
)
2594 (defvar remember-data-file
)
2595 (defvar remember-register
)
2596 (defvar remember-buffer
)
2597 (defvar remember-handler-functions
)
2598 (defvar remember-annotation-functions
)
2599 (defvar texmathp-why
)
2600 (declare-function speedbar-line-directory
"speedbar" (&optional depth
))
2601 (declare-function table--at-cell-p
"table" (position &optional object at-column
))
2603 (defvar w3m-current-url
)
2604 (defvar w3m-current-title
)
2606 (defvar org-latex-regexps
)
2608 ;;; Autoload and prepare some org modules
2610 ;; Some table stuff that needs to be defined here, because it is used
2611 ;; by the functions setting up org-mode or checking for table context.
2613 (defconst org-table-any-line-regexp
"^[ \t]*\\(|\\|\\+-[-+]\\)"
2614 "Detects an org-type or table-type table.")
2615 (defconst org-table-line-regexp
"^[ \t]*|"
2616 "Detects an org-type table line.")
2617 (defconst org-table-dataline-regexp
"^[ \t]*|[^-]"
2618 "Detects an org-type table line.")
2619 (defconst org-table-hline-regexp
"^[ \t]*|-"
2620 "Detects an org-type table hline.")
2621 (defconst org-table1-hline-regexp
"^[ \t]*\\+-[-+]"
2622 "Detects a table-type table hline.")
2623 (defconst org-table-any-border-regexp
"^[ \t]*[^|+ \t]"
2624 "Searching from within a table (any type) this finds the first line
2625 outside the table.")
2627 ;; Autoload the functions in org-table.el that are needed by functions here.
2630 (org-autoload "org-table"
2631 '(org-table-align org-table-begin org-table-blank-field
2632 org-table-convert org-table-convert-region org-table-copy-down
2633 org-table-copy-region org-table-create
2634 org-table-create-or-convert-from-region
2635 org-table-create-with-table.el org-table-current-dline
2636 org-table-cut-region org-table-delete-column org-table-edit-field
2637 org-table-edit-formulas org-table-end org-table-eval-formula
2638 org-table-export org-table-field-info
2639 org-table-get-stored-formulas org-table-goto-column
2640 org-table-hline-and-move org-table-import org-table-insert-column
2641 org-table-insert-hline org-table-insert-row org-table-iterate
2642 org-table-justify-field-maybe org-table-kill-row
2643 org-table-maybe-eval-formula org-table-maybe-recalculate-line
2644 org-table-move-column org-table-move-column-left
2645 org-table-move-column-right org-table-move-row
2646 org-table-move-row-down org-table-move-row-up
2647 org-table-next-field org-table-next-row org-table-paste-rectangle
2648 org-table-previous-field org-table-recalculate
2649 org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
2650 org-table-toggle-coordinate-overlays
2651 org-table-toggle-formula-debugger org-table-wrap-region
2652 orgtbl-mode turn-on-orgtbl org-table-to-lisp
)))
2654 (defun org-at-table-p (&optional table-type
)
2655 "Return t if the cursor is inside an org-type table.
2656 If TABLE-TYPE is non-nil, also check for table.el-type tables."
2657 (if org-enable-table-editor
2659 (beginning-of-line 1)
2660 (looking-at (if table-type org-table-any-line-regexp
2661 org-table-line-regexp
)))
2663 (defsubst org-table-p
() (org-at-table-p))
2665 (defun org-at-table.el-p
()
2666 "Return t if and only if we are at a table.el table."
2667 (and (org-at-table-p 'any
)
2669 (goto-char (org-table-begin 'any
))
2670 (looking-at org-table1-hline-regexp
))))
2671 (defun org-table-recognize-table.el
()
2672 "If there is a table.el table nearby, recognize it and move into it."
2673 (if org-table-tab-recognizes-table.el
2674 (if (org-at-table.el-p
)
2676 (beginning-of-line 1)
2677 (if (looking-at org-table-dataline-regexp
)
2679 (if (looking-at org-table1-hline-regexp
)
2681 (beginning-of-line 2)
2682 (if (looking-at org-table-any-border-regexp
)
2683 (beginning-of-line -
1)))))
2684 (if (re-search-forward "|" (org-table-end t
) t
)
2687 (if (table--at-cell-p (point))
2689 (message "recognizing table.el table...")
2690 (table-recognize-table)
2691 (message "recognizing table.el table...done")))
2692 (error "This should not happen..."))
2697 (defun org-at-table-hline-p ()
2698 "Return t if the cursor is inside a hline in a table."
2699 (if org-enable-table-editor
2701 (beginning-of-line 1)
2702 (looking-at org-table-hline-regexp
))
2705 (defvar org-table-clean-did-remove-column nil
)
2707 (defun org-table-map-tables (function)
2708 "Apply FUNCTION to the start of all tables in the buffer."
2712 (goto-char (point-min))
2713 (while (re-search-forward org-table-any-line-regexp nil t
)
2714 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
2715 (beginning-of-line 1)
2716 (if (looking-at org-table-line-regexp
)
2717 (save-excursion (funcall function
)))
2718 (re-search-forward org-table-any-border-regexp nil
1))))
2719 (message "Mapping tables: done"))
2721 ;; Declare and autoload functions from org-exp.el
2723 (declare-function org-default-export-plist
"org-exp")
2724 (declare-function org-infile-export-plist
"org-exp")
2725 (declare-function org-get-current-options
"org-exp")
2727 (org-autoload "org-exp"
2728 '(org-export org-export-as-ascii org-export-visible
2729 org-insert-export-options-template org-export-as-html-and-open
2730 org-export-as-html-batch org-export-as-html-to-buffer
2731 org-replace-region-by-html org-export-region-as-html
2732 org-export-as-html org-export-icalendar-this-file
2733 org-export-icalendar-all-agenda-files
2734 org-table-clean-before-export
2735 org-export-icalendar-combine-agenda-files org-export-as-xoxo
)))
2737 ;; Declare and autoload functions from org-agenda.el
2740 (org-autoload "org-agenda"
2741 '(org-agenda org-agenda-list org-search-view
2742 org-todo-list org-tags-view org-agenda-list-stuck-projects
2743 org-diary org-agenda-to-appt
2744 org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
)))
2746 ;; Autoload org-remember
2749 (org-autoload "org-remember"
2750 '(org-remember-insinuate org-remember-annotation
2751 org-remember-apply-template org-remember org-remember-handler
)))
2753 ;; Autoload org-clock.el
2756 (declare-function org-clock-save-markers-for-cut-and-paste
"org-clock"
2758 (declare-function org-clock-update-mode-line
"org-clock" ())
2759 (defvar org-clock-start-time
)
2760 (defvar org-clock-marker
(make-marker)
2761 "Marker recording the last clock-in.")
2766 '(org-clock-in org-clock-out org-clock-cancel
2767 org-clock-goto org-clock-sum org-clock-display
2768 org-clock-remove-overlays org-clock-report
2769 org-clocktable-shift org-dblock-write
:clocktable
2770 org-get-clocktable
)))
2772 (defun org-clock-update-time-maybe ()
2773 "If this is a CLOCK line, update it and return t.
2774 Otherwise, return nil."
2777 (beginning-of-line 1)
2778 (skip-chars-forward " \t")
2779 (when (looking-at org-clock-string
)
2780 (let ((re (concat "[ \t]*" org-clock-string
2781 " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
2782 "\\([ \t]*=>.*\\)?\\)?"))
2785 ((not (looking-at re
))
2787 ((not (match-end 2))
2788 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
2789 (> org-clock-marker
(point))
2790 (<= org-clock-marker
(point-at-eol)))
2791 ;; The clock is running here
2792 (setq org-clock-start-time
2794 (org-parse-time-string (match-string 1))))
2795 (org-clock-update-mode-line)))
2797 (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
2799 (setq ts
(match-string 1)
2800 te
(match-string 3))
2801 (setq s
(- (time-to-seconds
2802 (apply 'encode-time
(org-parse-time-string te
)))
2804 (apply 'encode-time
(org-parse-time-string ts
))))
2807 h
(floor (/ s
3600))
2811 (insert " => " (format (if neg
"-%d:%02d" "%2d:%02d") h m
))
2814 (defun org-check-running-clock ()
2815 "Check if the current buffer contains the running clock.
2816 If yes, offer to stop it and to save the buffer with the changes."
2817 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
2818 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
2821 (when (y-or-n-p "Save changed buffer?")
2824 (defun org-clocktable-try-shift (dir n
)
2825 "Check if this line starts a clock table, if yes, shift the time block."
2826 (when (org-match-line "#\\+BEGIN: clocktable\\>")
2827 (org-clocktable-shift dir n
)))
2829 ;; Autoload org-timer.el
2831 ;(declare-function org-timer "org-timer")
2836 '(org-timer-start org-timer org-timer-item
2837 org-timer-change-times-in-region
)))
2840 ;; Autoload archiving code
2841 ;; The stuff that is needed for cycling and tags has to be defined here.
2843 (defgroup org-archive nil
2844 "Options concerning archiving in Org-mode."
2846 :group
'org-structure
)
2848 (defcustom org-archive-location
"%s_archive::"
2849 "The location where subtrees should be archived.
2851 The value of this variable is a string, consisting of two parts,
2852 separated by a double-colon. The first part is a filename and
2853 the second part is a headline.
2855 When the filename is omitted, archiving happens in the same file.
2856 %s in the filename will be replaced by the current file
2857 name (without the directory part). Archiving to a different file
2858 is useful to keep archived entries from contributing to the
2861 The archived entries will be filed as subtrees of the specified
2862 headline. When the headline is omitted, the subtrees are simply
2863 filed away at the end of the file, as top-level entries. Also in
2864 the heading you can use %s to represent the file name, this can be
2865 useful when using the same archive for a number of different files.
2867 Here are a few examples:
2869 If the current file is Projects.org, archive in file
2870 Projects.org_archive, as top-level trees. This is the default.
2872 \"::* Archived Tasks\"
2873 Archive in the current file, under the top-level headline
2874 \"* Archived Tasks\".
2876 \"~/org/archive.org::\"
2877 Archive in file ~/org/archive.org (absolute path), as top-level trees.
2879 \"~/org/archive.org::From %s\"
2880 Archive in file ~/org/archive.org (absolute path), und headlines
2881 \"From FILENAME\" where file name is the current file name.
2883 \"basement::** Finished Tasks\"
2884 Archive in file ./basement (relative path), as level 3 trees
2885 below the level 2 heading \"** Finished Tasks\".
2887 You may set this option on a per-file basis by adding to the buffer a
2890 #+ARCHIVE: basement::** Finished Tasks
2892 You may also define it locally for a subtree by setting an ARCHIVE property
2893 in the entry. If such a property is found in an entry, or anywhere up
2894 the hierarchy, it will be used."
2898 (defcustom org-archive-tag
"ARCHIVE"
2899 "The tag that marks a subtree as archived.
2900 An archived subtree does not open during visibility cycling, and does
2901 not contribute to the agenda listings.
2902 After changing this, font-lock must be restarted in the relevant buffers to
2903 get the proper fontification."
2905 :group
'org-keywords
2908 (defcustom org-agenda-skip-archived-trees t
2909 "Non-nil means, the agenda will skip any items located in archived trees.
2910 An archived tree is a tree marked with the tag ARCHIVE. The use of this
2911 variable is no longer recommended, you should leave it at the value t.
2912 Instead, use the key `v' to cycle the archives-mode in the agenda."
2914 :group
'org-agenda-skip
2917 (defcustom org-cycle-open-archived-trees nil
2918 "Non-nil means, `org-cycle' will open archived trees.
2919 An archived tree is a tree marked with the tag ARCHIVE.
2920 When nil, archived trees will stay folded. You can still open them with
2921 normal outline commands like `show-all', but not with the cycling commands."
2926 (defcustom org-sparse-tree-open-archived-trees nil
2927 "Non-nil means sparse tree construction shows matches in archived trees.
2928 When nil, matches in these trees are highlighted, but the trees are kept in
2931 :group
'org-sparse-trees
2934 (defun org-cycle-hide-archived-subtrees (state)
2935 "Re-hide all archived subtrees after a visibility state change."
2936 (when (and (not org-cycle-open-archived-trees
)
2937 (not (memq state
'(overview folded
))))
2939 (let* ((globalp (memq state
'(contents all
)))
2940 (beg (if globalp
(point-min) (point)))
2941 (end (if globalp
(point-max) (org-end-of-subtree t
))))
2942 (org-hide-archived-subtrees beg end
)
2944 (if (looking-at (concat ".*:" org-archive-tag
":"))
2945 (message "%s" (substitute-command-keys
2946 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
2948 (defun org-force-cycle-archived ()
2949 "Cycle subtree even if it is archived."
2951 (setq this-command
'org-cycle
)
2952 (let ((org-cycle-open-archived-trees t
))
2953 (call-interactively 'org-cycle
)))
2955 (defun org-hide-archived-subtrees (beg end
)
2956 "Re-hide all archived subtrees after a visibility state change."
2958 (let* ((re (concat ":" org-archive-tag
":")))
2960 (while (re-search-forward re end t
)
2961 (and (org-on-heading-p) (hide-subtree))
2962 (org-end-of-subtree t
)))))
2964 (defalias 'org-advertized-archive-subtree
'org-archive-subtree
)
2967 (org-autoload "org-archive"
2968 '(org-add-archive-files org-archive-subtree
2969 org-archive-to-archive-sibling org-toggle-archive-tag
)))
2971 ;; Autoload Column View Code
2973 (declare-function org-columns-number-to-string
"org-colview")
2974 (declare-function org-columns-get-format-and-top-level
"org-colview")
2975 (declare-function org-columns-compute
"org-colview")
2977 (org-autoload (if (featurep 'xemacs
) "org-colview-xemacs" "org-colview")
2978 '(org-columns-number-to-string org-columns-get-format-and-top-level
2979 org-columns-compute org-agenda-columns org-columns-remove-overlays
2980 org-columns org-insert-columns-dblock org-dblock-write
:columnview
))
2984 (declare-function org-id-store-link
"org-id")
2985 (org-autoload "org-id"
2986 '(org-id-get-create org-id-new org-id-copy org-id-get
2987 org-id-get-with-outline-path-completion
2988 org-id-get-with-outline-drilling
2989 org-id-goto org-id-find org-id-store-link
))
2991 ;;; Variables for pre-computed regular expressions, all buffer local
2993 (defvar org-drawer-regexp nil
2994 "Matches first line of a hidden block.")
2995 (make-variable-buffer-local 'org-drawer-regexp
)
2996 (defvar org-todo-regexp nil
2997 "Matches any of the TODO state keywords.")
2998 (make-variable-buffer-local 'org-todo-regexp
)
2999 (defvar org-not-done-regexp nil
3000 "Matches any of the TODO state keywords except the last one.")
3001 (make-variable-buffer-local 'org-not-done-regexp
)
3002 (defvar org-todo-line-regexp nil
3003 "Matches a headline and puts TODO state into group 2 if present.")
3004 (make-variable-buffer-local 'org-todo-line-regexp
)
3005 (defvar org-complex-heading-regexp nil
3006 "Matches a headline and puts everything into groups:
3008 group 2: The todo keyword, maybe
3009 group 3: Priority cookie
3010 group 4: True headline
3012 (make-variable-buffer-local 'org-complex-heading-regexp
)
3013 (defvar org-todo-line-tags-regexp nil
3014 "Matches a headline and puts TODO state into group 2 if present.
3015 Also put tags into group 4 if tags are present.")
3016 (make-variable-buffer-local 'org-todo-line-tags-regexp
)
3017 (defvar org-nl-done-regexp nil
3018 "Matches newline followed by a headline with the DONE keyword.")
3019 (make-variable-buffer-local 'org-nl-done-regexp
)
3020 (defvar org-looking-at-done-regexp nil
3021 "Matches the DONE keyword a point.")
3022 (make-variable-buffer-local 'org-looking-at-done-regexp
)
3023 (defvar org-ds-keyword-length
12
3024 "Maximum length of the Deadline and SCHEDULED keywords.")
3025 (make-variable-buffer-local 'org-ds-keyword-length
)
3026 (defvar org-deadline-regexp nil
3027 "Matches the DEADLINE keyword.")
3028 (make-variable-buffer-local 'org-deadline-regexp
)
3029 (defvar org-deadline-time-regexp nil
3030 "Matches the DEADLINE keyword together with a time stamp.")
3031 (make-variable-buffer-local 'org-deadline-time-regexp
)
3032 (defvar org-deadline-line-regexp nil
3033 "Matches the DEADLINE keyword and the rest of the line.")
3034 (make-variable-buffer-local 'org-deadline-line-regexp
)
3035 (defvar org-scheduled-regexp nil
3036 "Matches the SCHEDULED keyword.")
3037 (make-variable-buffer-local 'org-scheduled-regexp
)
3038 (defvar org-scheduled-time-regexp nil
3039 "Matches the SCHEDULED keyword together with a time stamp.")
3040 (make-variable-buffer-local 'org-scheduled-time-regexp
)
3041 (defvar org-closed-time-regexp nil
3042 "Matches the CLOSED keyword together with a time stamp.")
3043 (make-variable-buffer-local 'org-closed-time-regexp
)
3045 (defvar org-keyword-time-regexp nil
3046 "Matches any of the 4 keywords, together with the time stamp.")
3047 (make-variable-buffer-local 'org-keyword-time-regexp
)
3048 (defvar org-keyword-time-not-clock-regexp nil
3049 "Matches any of the 3 keywords, together with the time stamp.")
3050 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp
)
3051 (defvar org-maybe-keyword-time-regexp nil
3052 "Matches a timestamp, possibly preceeded by a keyword.")
3053 (make-variable-buffer-local 'org-maybe-keyword-time-regexp
)
3054 (defvar org-planning-or-clock-line-re nil
3055 "Matches a line with planning or clock info.")
3056 (make-variable-buffer-local 'org-planning-or-clock-line-re
)
3058 (defconst org-plain-time-of-day-regexp
3061 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
3064 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
3066 "Regular expression to match a plain time or time range.
3067 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
3068 groups carry important information:
3070 1 the first time, range or not
3071 8 the second time, if it is a range.")
3073 (defconst org-plain-time-extension-regexp
3076 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
3077 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
3078 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
3079 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
3080 groups carry important information:
3083 9 minutes of duration")
3085 (defconst org-stamp-time-of-day-regexp
3087 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
3088 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
3090 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
3091 "Regular expression to match a timestamp time or time range.
3092 After a match, the following groups carry important information:
3094 1 date plus weekday, for backreferencing to make sure both times on same day
3095 2 the first time, range or not
3096 4 the second time, if it is a range.")
3098 (defconst org-startup-options
3099 '(("fold" org-startup-folded t
)
3100 ("overview" org-startup-folded t
)
3101 ("nofold" org-startup-folded nil
)
3102 ("showall" org-startup-folded nil
)
3103 ("content" org-startup-folded content
)
3104 ("hidestars" org-hide-leading-stars t
)
3105 ("showstars" org-hide-leading-stars nil
)
3106 ("odd" org-odd-levels-only t
)
3107 ("oddeven" org-odd-levels-only nil
)
3108 ("align" org-startup-align-all-tables t
)
3109 ("noalign" org-startup-align-all-tables nil
)
3110 ("customtime" org-display-custom-times t
)
3111 ("logdone" org-log-done time
)
3112 ("lognotedone" org-log-done note
)
3113 ("nologdone" org-log-done nil
)
3114 ("lognoteclock-out" org-log-note-clock-out t
)
3115 ("nolognoteclock-out" org-log-note-clock-out nil
)
3116 ("logrepeat" org-log-repeat state
)
3117 ("lognoterepeat" org-log-repeat note
)
3118 ("nologrepeat" org-log-repeat nil
)
3119 ("fninline" org-footnote-define-inline t
)
3120 ("nofninline" org-footnote-define-inline nil
)
3121 ("fnlocal" org-footnote-section nil
)
3122 ("fnauto" org-footnote-auto-label t
)
3123 ("fnprompt" org-footnote-auto-label nil
)
3124 ("fnconfirm" org-footnote-auto-label confirm
)
3125 ("fnplain" org-footnote-auto-label plain
)
3126 ("constcgs" constants-unit-system cgs
)
3127 ("constSI" constants-unit-system SI
))
3128 "Variable associated with STARTUP options for org-mode.
3129 Each element is a list of three items: The startup options as written
3130 in the #+STARTUP line, the corresponding variable, and the value to
3131 set this variable to if the option is found. An optional forth element PUSH
3132 means to push this value onto the list in the variable.")
3134 (defun org-set-regexps-and-options ()
3135 "Precompute regular expressions for current buffer."
3137 (org-set-local 'org-todo-kwd-alist nil
)
3138 (org-set-local 'org-todo-key-alist nil
)
3139 (org-set-local 'org-todo-key-trigger nil
)
3140 (org-set-local 'org-todo-keywords-1 nil
)
3141 (org-set-local 'org-done-keywords nil
)
3142 (org-set-local 'org-todo-heads nil
)
3143 (org-set-local 'org-todo-sets nil
)
3144 (org-set-local 'org-todo-log-states nil
)
3145 (org-set-local 'org-file-properties nil
)
3146 (org-set-local 'org-file-tags nil
)
3147 (let ((re (org-make-options-regexp
3148 '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
3149 "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
3150 "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
3152 kwds kws0 kwsa key log value cat arch tags const links hw dws
3153 tail sep kws1 prio props ftags drawers
3154 ext-setup-or-nil setup-contents
(start 0))
3158 (goto-char (point-min))
3159 (while (or (and ext-setup-or-nil
3160 (string-match re ext-setup-or-nil start
)
3161 (setq start
(match-end 0)))
3162 (and (setq ext-setup-or-nil nil start
0)
3163 (re-search-forward re nil t
)))
3164 (setq key
(upcase (match-string 1 ext-setup-or-nil
))
3165 value
(org-match-string-no-properties 2 ext-setup-or-nil
))
3167 ((equal key
"CATEGORY")
3168 (if (string-match "[ \t]+$" value
)
3169 (setq value
(replace-match "" t t value
)))
3171 ((member key
'("SEQ_TODO" "TODO"))
3172 (push (cons 'sequence
(org-split-string value splitre
)) kwds
))
3173 ((equal key
"TYP_TODO")
3174 (push (cons 'type
(org-split-string value splitre
)) kwds
))
3176 (setq tags
(append tags
(org-split-string value splitre
))))
3177 ((equal key
"COLUMNS")
3178 (org-set-local 'org-columns-default-format value
))
3180 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value
)
3181 (push (cons (match-string 1 value
)
3182 (org-trim (match-string 2 value
)))
3184 ((equal key
"PRIORITIES")
3185 (setq prio
(org-split-string value
" +")))
3186 ((equal key
"PROPERTY")
3187 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
3188 (push (cons (match-string 1 value
) (match-string 2 value
))
3190 ((equal key
"FILETAGS")
3191 (when (string-match "\\S-" value
)
3196 (mapcar (lambda (x) (org-split-string x
":"))
3197 (org-split-string value
)))))))
3198 ((equal key
"DRAWERS")
3199 (setq drawers
(org-split-string value splitre
)))
3200 ((equal key
"CONSTANTS")
3201 (setq const
(append const
(org-split-string value splitre
))))
3202 ((equal key
"STARTUP")
3203 (let ((opts (org-split-string value splitre
))
3205 (while (setq l
(pop opts
))
3206 (when (setq l
(assoc l org-startup-options
))
3207 (setq var
(nth 1 l
) val
(nth 2 l
))
3209 (set (make-local-variable var
) val
)
3210 (if (not (listp (symbol-value var
)))
3211 (set (make-local-variable var
) nil
))
3212 (set (make-local-variable var
) (symbol-value var
))
3213 (add-to-list var val
))))))
3214 ((equal key
"ARCHIVE")
3215 (string-match " *$" value
)
3216 (setq arch
(replace-match "" t t value
))
3217 (remove-text-properties 0 (length arch
)
3218 '(face t fontified t
) arch
))
3219 ((equal key
"SETUPFILE")
3220 (setq setup-contents
(org-file-contents
3222 (org-remove-double-quotes value
))
3224 (if (not ext-setup-or-nil
)
3225 (setq ext-setup-or-nil setup-contents start
0)
3226 (setq ext-setup-or-nil
3227 (concat (substring ext-setup-or-nil
0 start
)
3228 "\n" setup-contents
"\n"
3229 (substring ext-setup-or-nil start
)))))
3232 (org-set-local 'org-category
(intern cat
))
3233 (push (cons "CATEGORY" cat
) props
))
3235 (if (< (length prio
) 3) (setq prio
'("A" "C" "B")))
3236 (setq prio
(mapcar 'string-to-char prio
))
3237 (org-set-local 'org-highest-priority
(nth 0 prio
))
3238 (org-set-local 'org-lowest-priority
(nth 1 prio
))
3239 (org-set-local 'org-default-priority
(nth 2 prio
)))
3240 (and props
(org-set-local 'org-file-properties
(nreverse props
)))
3241 (and ftags
(org-set-local 'org-file-tags ftags
))
3242 (and drawers
(org-set-local 'org-drawers drawers
))
3243 (and arch
(org-set-local 'org-archive-location arch
))
3244 (and links
(setq org-link-abbrev-alist-local
(nreverse links
)))
3245 ;; Process the TODO keywords
3247 ;; Use the global values as if they had been given locally.
3248 (setq kwds
(default-value 'org-todo-keywords
))
3249 (if (stringp (car kwds
))
3250 (setq kwds
(list (cons org-todo-interpretation
3251 (default-value 'org-todo-keywords
)))))
3252 (setq kwds
(reverse kwds
)))
3253 (setq kwds
(nreverse kwds
))
3255 (while (setq kws
(pop kwds
))
3256 (setq inter
(pop kws
) sep
(member "|" kws
)
3257 kws0
(delete "|" (copy-sequence kws
))
3262 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x
)
3264 (setq kw
(match-string 1 x
)
3265 key
(and (match-end 2) (match-string 2 x
))
3266 log
(org-extract-log-state-settings x
))
3267 (push (cons kw
(and key
(string-to-char key
))) kwsa
)
3268 (and log
(push log org-todo-log-states
))
3270 (error "Invalid TODO keyword %s" x
)))
3272 kwsa
(if kwsa
(append '((:startgroup
))
3276 dws
(if sep
(org-remove-keyword-keys (cdr sep
)) (last kws1
))
3277 tail
(list inter hw
(car dws
) (org-last dws
)))
3278 (add-to-list 'org-todo-heads hw
'append
)
3279 (push kws1 org-todo-sets
)
3280 (setq org-done-keywords
(append org-done-keywords dws nil
))
3281 (setq org-todo-key-alist
(append org-todo-key-alist kwsa
))
3282 (mapc (lambda (x) (push (cons x tail
) org-todo-kwd-alist
)) kws1
)
3283 (setq org-todo-keywords-1
(append org-todo-keywords-1 kws1 nil
)))
3284 (setq org-todo-sets
(nreverse org-todo-sets
)
3285 org-todo-kwd-alist
(nreverse org-todo-kwd-alist
)
3286 org-todo-key-trigger
(delq nil
(mapcar 'cdr org-todo-key-alist
))
3287 org-todo-key-alist
(org-assign-fast-keys org-todo-key-alist
)))
3288 ;; Process the constants
3291 (while (setq e
(pop const
))
3292 (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e
)
3293 (push (cons (match-string 1 e
) (match-string 2 e
)) cst
)))
3294 (setq org-table-formula-constants-local cst
)))
3296 ;; Process the tags.
3299 (while (setq e
(pop tags
))
3301 ((equal e
"{") (push '(:startgroup
) tgs
))
3302 ((equal e
"}") (push '(:endgroup
) tgs
))
3303 ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e
)
3304 (push (cons (match-string 1 e
)
3305 (string-to-char (match-string 2 e
)))
3307 (t (push (list e
) tgs
))))
3308 (org-set-local 'org-tag-alist nil
)
3309 (while (setq e
(pop tgs
))
3310 (or (and (stringp (car e
))
3311 (assoc (car e
) org-tag-alist
))
3312 (push e org-tag-alist
)))))
3314 ;; Compute the regular expressions and other local variables
3315 (if (not org-done-keywords
)
3316 (setq org-done-keywords
(list (org-last org-todo-keywords-1
))))
3317 (setq org-ds-keyword-length
(+ 2 (max (length org-deadline-string
)
3318 (length org-scheduled-string
)
3319 (length org-clock-string
)
3320 (length org-closed-string
)))
3322 (concat "^[ \t]*:\\("
3323 (mapconcat 'regexp-quote org-drawers
"\\|")
3325 org-not-done-keywords
3326 (org-delete-all org-done-keywords
(copy-sequence org-todo-keywords-1
))
3328 (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
3332 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
3334 org-todo-line-regexp
3335 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3336 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
3337 "\\)\\>\\)?[ \t]*\\(.*\\)")
3338 org-complex-heading-regexp
3339 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3340 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
3341 "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
3342 "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
3344 (concat "\n\\*+[ \t]+"
3345 "\\(?:" (mapconcat 'regexp-quote org-done-keywords
"\\|")
3347 org-todo-line-tags-regexp
3348 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3349 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
3351 "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
3352 org-looking-at-done-regexp
3354 (mapconcat 'regexp-quote org-done-keywords
"\\|") "\\)"
3356 org-deadline-regexp
(concat "\\<" org-deadline-string
)
3357 org-deadline-time-regexp
3358 (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>")
3359 org-deadline-line-regexp
3360 (concat "\\<\\(" org-deadline-string
"\\).*")
3361 org-scheduled-regexp
3362 (concat "\\<" org-scheduled-string
)
3363 org-scheduled-time-regexp
3364 (concat "\\<" org-scheduled-string
" *<\\([^>]+\\)>")
3365 org-closed-time-regexp
3366 (concat "\\<" org-closed-string
" *\\[\\([^]]+\\)\\]")
3367 org-keyword-time-regexp
3368 (concat "\\<\\(" org-scheduled-string
3369 "\\|" org-deadline-string
3370 "\\|" org-closed-string
3371 "\\|" org-clock-string
"\\)"
3372 " *[[<]\\([^]>]+\\)[]>]")
3373 org-keyword-time-not-clock-regexp
3374 (concat "\\<\\(" org-scheduled-string
3375 "\\|" org-deadline-string
3376 "\\|" org-closed-string
3378 " *[[<]\\([^]>]+\\)[]>]")
3379 org-maybe-keyword-time-regexp
3380 (concat "\\(\\<\\(" org-scheduled-string
3381 "\\|" org-deadline-string
3382 "\\|" org-closed-string
3383 "\\|" org-clock-string
"\\)\\)?"
3384 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
3385 org-planning-or-clock-line-re
3386 (concat "\\(?:^[ \t]*\\(" org-scheduled-string
3387 "\\|" org-deadline-string
3388 "\\|" org-closed-string
"\\|" org-clock-string
3391 (org-compute-latex-and-specials-regexp)
3392 (org-set-font-lock-defaults))))
3394 (defun org-file-contents (file &optional noerror
)
3395 "Return the contents of FILE, as a string."
3397 (not (file-readable-p file
)))
3400 (message "Cannot read file %s" file
)
3403 (error "Cannot read file %s" file
))
3405 (insert-file-contents file
)
3408 (defun org-extract-log-state-settings (x)
3409 "Extract the log state setting from a TODO keyword string.
3410 This will extract info from a string like \"WAIT(w@/!)\"."
3411 (let (kw key log1 log2
)
3412 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x
)
3413 (setq kw
(match-string 1 x
)
3414 key
(and (match-end 2) (match-string 2 x
))
3415 log1
(and (match-end 3) (match-string 3 x
))
3416 log2
(and (match-end 4) (match-string 4 x
)))
3419 (and log1
(if (equal log1
"!") 'time
'note
))
3420 (and log2
(if (equal log2
"!") 'time
'note
)))))))
3422 (defun org-remove-keyword-keys (list)
3423 "Remove a pair of parenthesis at the end of each string in LIST."
3425 (if (string-match "(.*)$" x
)
3426 (substring x
0 (match-beginning 0))
3430 ;; FIXME: this could be done much better, using second characters etc.
3431 (defun org-assign-fast-keys (alist)
3432 "Assign fast keys to a keyword-key alist.
3433 Respect keys that are already there."
3434 (let (new e k c c1 c2
(char ?a
))
3435 (while (setq e
(pop alist
))
3437 ((equal e
'(:startgroup
)) (push e new
))
3438 ((equal e
'(:endgroup
)) (push e new
))
3440 (setq k
(car e
) c2 nil
)
3443 ;; automatically assign a character.
3444 (setq c1
(string-to-char
3445 (downcase (substring
3446 k
(if (= (string-to-char k
) ?
@) 1 0)))))
3447 (if (or (rassoc c1 new
) (rassoc c1 alist
))
3448 (while (or (rassoc char new
) (rassoc char alist
))
3449 (setq char
(1+ char
)))
3451 (setq c
(or c2 char
)))
3452 (push (cons k c
) new
))))
3455 ;;; Some variables used in various places
3457 (defvar org-window-configuration nil
3458 "Used in various places to store a window configuration.")
3459 (defvar org-finish-function nil
3460 "Function to be called when `C-c C-c' is used.
3461 This is for getting out of special buffers like remember.")
3464 ;; FIXME: Occasionally check by commenting these, to make sure
3465 ;; no other functions uses these, forgetting to let-bind them.
3470 (defvar description
)
3472 ;; Defined somewhere in this file, but used before definition.
3473 (defvar org-html-entities
)
3474 (defvar org-struct-menu
)
3475 (defvar org-org-menu
)
3476 (defvar org-tbl-menu
)
3477 (defvar org-agenda-keymap
)
3479 ;;;; Define the Org-mode
3481 (if (and (not (keymapp outline-mode-map
)) (featurep 'allout
))
3482 (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."))
3485 ;; We use a before-change function to check if a table might need
3487 (defvar org-table-may-need-update t
3488 "Indicates that a table might need an update.
3489 This variable is set by `org-before-change-function'.
3490 `org-table-align' sets it back to nil.")
3491 (defun org-before-change-function (beg end
)
3492 "Every change indicates that a table might need an update."
3493 (setq org-table-may-need-update t
))
3494 (defvar org-mode-map
)
3495 (defvar org-mode-hook nil
3496 "Mode hook for Org-mode, run after the mode was turned on.")
3497 (defvar org-inhibit-startup nil
) ; Dynamically-scoped param.
3498 (defvar org-agenda-keep-modes nil
) ; Dynamically-scoped param.
3499 (defvar org-table-buffer-is-an nil
)
3500 (defconst org-outline-regexp
"\\*+ ")
3503 (define-derived-mode org-mode outline-mode
"Org"
3504 "Outline-based notes management and organizer, alias
3505 \"Carsten's outline-mode for keeping track of everything.\"
3507 Org-mode develops organizational tasks around a NOTES file which
3508 contains information about projects as plain text. Org-mode is
3509 implemented on top of outline-mode, which is ideal to keep the content
3510 of large files well structured. It supports ToDo items, deadlines and
3511 time stamps, which magically appear in the diary listing of the Emacs
3512 calendar. Tables are easily created with a built-in table editor.
3513 Plain text URL-like links connect to websites, emails (VM), Usenet
3514 messages (Gnus), BBDB entries, and any files related to the project.
3515 For printing and sharing of notes, an Org-mode file (or a part of it)
3516 can be exported as a structured ASCII or HTML file.
3518 The following commands are available:
3522 ;; Get rid of Outline menus, they are not needed
3523 ;; Need to do this here because define-derived-mode sets up
3524 ;; the keymap so late. Still, it is a waste to call this each time
3525 ;; we switch another buffer into org-mode.
3526 (if (featurep 'xemacs
)
3527 (when (boundp 'outline-mode-menu-heading
)
3528 ;; Assume this is Greg's port, it used easymenu
3529 (easy-menu-remove outline-mode-menu-heading
)
3530 (easy-menu-remove outline-mode-menu-show
)
3531 (easy-menu-remove outline-mode-menu-hide
))
3532 (define-key org-mode-map
[menu-bar headings
] 'undefined
)
3533 (define-key org-mode-map
[menu-bar hide
] 'undefined
)
3534 (define-key org-mode-map
[menu-bar show
] 'undefined
))
3536 (org-load-modules-maybe)
3537 (easy-menu-add org-org-menu
)
3538 (easy-menu-add org-tbl-menu
)
3539 (org-install-agenda-files-menu)
3540 (if org-descriptive-links
(org-add-to-invisibility-spec '(org-link)))
3541 (org-add-to-invisibility-spec '(org-cwidth))
3542 (when (featurep 'xemacs
)
3543 (org-set-local 'line-move-ignore-invisible t
))
3544 (org-set-local 'outline-regexp org-outline-regexp
)
3545 (org-set-local 'outline-level
'org-outline-level
)
3546 (when (and org-ellipsis
3547 (fboundp 'set-display-table-slot
) (boundp 'buffer-display-table
)
3548 (fboundp 'make-glyph-code
))
3549 (unless org-display-table
3550 (setq org-display-table
(make-display-table)))
3551 (set-display-table-slot
3554 (lambda (c) (make-glyph-code c
(and (not (stringp org-ellipsis
))
3556 (if (stringp org-ellipsis
) org-ellipsis
"..."))))
3557 (setq buffer-display-table org-display-table
))
3558 (org-set-regexps-and-options)
3559 (when (and org-tag-faces
(not org-tags-special-faces-re
))
3560 ;; tag faces set outside customize.... force initialization.
3561 (org-set-tag-faces 'org-tag-faces org-tag-faces
))
3563 (org-set-local 'calc-embedded-open-mode
"# ")
3564 (modify-syntax-entry ?
# "<")
3565 (modify-syntax-entry ?
@ "w")
3566 (if org-startup-truncated
(setq truncate-lines t
))
3567 (org-set-local 'font-lock-unfontify-region-function
3568 'org-unfontify-region
)
3569 ;; Activate before-change-function
3570 (org-set-local 'org-table-may-need-update t
)
3571 (org-add-hook 'before-change-functions
'org-before-change-function nil
3573 ;; Check for running clock before killing a buffer
3574 (org-add-hook 'kill-buffer-hook
'org-check-running-clock nil
'local
)
3575 ;; Paragraphs and auto-filling
3576 (org-set-autofill-regexps)
3577 (setq indent-line-function
'org-indent-line-function
)
3578 (org-update-radio-target-regexp)
3580 ;; Comment characters
3581 ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
3582 (org-set-local 'comment-padding
" ")
3584 ;; Align options lines
3586 'align-mode-rules-list
3587 '((org-in-buffer-settings
3588 (regexp .
"^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
3589 (modes .
'(org-mode)))))
3592 (org-set-local 'imenu-create-index-function
3593 'org-imenu-get-tree
)
3595 ;; Make isearch reveal context
3596 (if (or (featurep 'xemacs
)
3597 (not (boundp 'outline-isearch-open-invisible-function
)))
3598 ;; Emacs 21 and XEmacs make use of the hook
3599 (org-add-hook 'isearch-mode-end-hook
'org-isearch-end
'append
'local
)
3600 ;; Emacs 22 deals with this through a special variable
3601 (org-set-local 'outline-isearch-open-invisible-function
3602 (lambda (&rest ignore
) (org-show-context 'isearch
))))
3604 ;; If empty file that did not turn on org-mode automatically, make it to.
3605 (if (and org-insert-mode-line-in-empty-file
3607 (= (point-min) (point-max)))
3608 (insert "# -*- mode: org -*-\n\n"))
3610 (unless org-inhibit-startup
3611 (when org-startup-align-all-tables
3612 (let ((bmp (buffer-modified-p)))
3613 (org-table-map-tables 'org-table-align
)
3614 (set-buffer-modified-p bmp
)))
3615 (org-set-startup-visibility)))
3617 (put 'org-mode
'flyspell-mode-predicate
'org-mode-flyspell-verify
)
3619 (defun org-current-time ()
3620 "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
3621 (if (> (car org-time-stamp-rounding-minutes
) 1)
3622 (let ((r (car org-time-stamp-rounding-minutes
))
3623 (time (decode-time)))
3625 (append (list 0 (* r
(floor (+ .5 (/ (float (nth 1 time
)) r
)))))
3629 ;;;; Font-Lock stuff, including the activators
3631 (defvar org-mouse-map
(make-sparse-keymap))
3632 (org-defkey org-mouse-map
3633 (if (featurep 'xemacs
) [button2] [mouse-2]) 'org-open-at-mouse)
3634 (org-defkey org-mouse-map
3635 (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
3636 (when org-mouse-1-follows-link
3637 (org-defkey org-mouse-map [follow-link] 'mouse-face))
3638 (when org-tab-follows-link
3639 (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
3640 (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
3641 (when org-return-follows-link
3642 (org-defkey org-mouse-map [(return)] 'org-open-at-point)
3643 (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
3645 (require 'font-lock)
3647 (defconst org-non-link-chars "]\t\n\r<>")
3648 (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
3650 (defvar org-link-types-re nil
3651 "Matches a link that has a url-like prefix like \"http:\"")
3652 (defvar org-link-re-with-space nil
3653 "Matches a link with spaces, optional angular brackets around it.")
3654 (defvar org-link-re-with-space2 nil
3655 "Matches a link with spaces, optional angular brackets around it.")
3656 (defvar org-link-re-with-space3 nil
3657 "Matches a link with spaces, only for internal part in bracket links.")
3658 (defvar org-angle-link-re nil
3659 "Matches link with angular brackets, spaces are allowed.")
3660 (defvar org-plain-link-re nil
3661 "Matches plain link, without spaces.")
3662 (defvar org-bracket-link-regexp nil
3663 "Matches a link in double brackets.")
3664 (defvar org-bracket-link-analytic-regexp nil
3665 "Regular expression used to analyze links.
3666 Here is what the match groups contain after a match:
3672 (defvar org-bracket-link-analytic-regexp++ nil
3673 "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
3674 (defvar org-any-link-re nil
3675 "Regular expression matching any link.")
3677 (defun org-make-link-regexps ()
3678 "Update the link regular expressions.
3679 This should be called after the variable `org-link-types' has changed."
3680 (setq org-link-types-re
3682 "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
3683 org-link-re-with-space
3685 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3686 "\\([^" org-non-link-chars " ]"
3687 "[^" org-non-link-chars "]*"
3688 "[^" org-non-link-chars " ]\\)>?")
3689 org-link-re-with-space2
3691 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3692 "\\([^" org-non-link-chars " ]"
3694 "[^" org-non-link-chars " ]\\)>?")
3695 org-link-re-with-space3
3697 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3698 "\\([^" org-non-link-chars " ]"
3702 "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3703 "\\([^" org-non-link-chars " ]"
3704 "[^" org-non-link-chars "]*"
3708 "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3709 "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
3710 org-bracket-link-regexp
3711 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
3712 org-bracket-link-analytic-regexp
3715 "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
3718 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
3720 org-bracket-link-analytic-regexp++
3723 "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
3726 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
3729 (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
3730 org-angle-link-re "\\)\\|\\("
3731 org-plain-link-re "\\)")))
3733 (org-make-link-regexps)
3735 (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
3736 "Regular expression for fast time stamp matching.")
3737 (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
3738 "Regular expression for fast time stamp matching.")
3739 (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
3740 "Regular expression matching time strings for analysis.
3741 This one does not require the space after the date, so it can be used
3742 on a string that terminates immediately after the date.")
3743 (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
3744 "Regular expression matching time strings for analysis.")
3745 (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
3746 "Regular expression matching time stamps, with groups.")
3747 (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
3748 "Regular expression matching time stamps (also [..]), with groups.")
3749 (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
3750 "Regular expression matching a time stamp range.")
3751 (defconst org-tr-regexp-both
3752 (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
3753 "Regular expression matching a time stamp range.")
3754 (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
3755 org-ts-regexp "\\)?")
3756 "Regular expression matching a time stamp or time stamp range.")
3757 (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
3758 org-ts-regexp-both "\\)?")
3759 "Regular expression matching a time stamp or time stamp range.
3760 The time stamps may be either active or inactive.")
3762 (defvar org-emph-face nil)
3764 (defun org-do-emphasis-faces (limit)
3765 "Run through the buffer and add overlays to links."
3767 (while (and (not rtn) (re-search-forward org-emph-re limit t))
3768 (if (not (= (char-after (match-beginning 3))
3769 (char-after (match-beginning 4))))
3772 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
3774 (nth 1 (assoc (match-string 3)
3775 org-emphasis-alist)))
3776 (add-text-properties (match-beginning 2) (match-end 2)
3777 '(font-lock-multiline t))
3778 (when org-hide-emphasis-markers
3779 (add-text-properties (match-end 4) (match-beginning 5)
3780 '(invisible org-link))
3781 (add-text-properties (match-beginning 3) (match-end 3)
3782 '(invisible org-link)))))
3786 (defun org-emphasize (&optional char)
3787 "Insert or change an emphasis, i.e. a font like bold or italic.
3788 If there is an active region, change that region to a new emphasis.
3789 If there is no region, just insert the marker characters and position
3790 the cursor between them.
3791 CHAR should be either the marker character, or the first character of the
3792 HTML tag associated with that emphasis. If CHAR is a space, the means
3793 to remove the emphasis of the selected region.
3794 If char is not given (for example in an interactive call) it
3795 will be prompted for."
3797 (let ((eal org-emphasis-alist) e det
3798 (erc org-emphasis-regexp-components)
3800 (string "") beg end move tag c s)
3801 (if (org-region-active-p)
3802 (setq beg (region-beginning) end (region-end)
3803 string (buffer-substring beg end))
3806 (while (setq e (pop eal))
3807 (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
3809 (push (cons c (string-to-char (car e))) det)
3810 (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
3811 (substring tag 1)))))
3812 (setq det (nreverse det))
3814 (message "%s" (concat "Emphasis marker or tag:" prompt))
3815 (setq char (read-char-exclusive)))
3816 (setq char (or (cdr (assoc char det)) char))
3817 (if (equal char ?\ )
3818 (setq s "" move nil)
3819 (unless (assoc (char-to-string char) org-emphasis-alist)
3820 (error "No such emphasis marker: \"%c\"" char))
3821 (setq s (char-to-string char)))
3822 (while (and (> (length string) 1)
3823 (equal (substring string 0 1) (substring string -1))
3824 (assoc (substring string 0 1) org-emphasis-alist))
3825 (setq string (substring string 1 -1)))
3826 (setq string (concat s string s))
3827 (if beg (delete-region beg end))
3829 (string-match (concat "[" (nth 0 erc) "\n]")
3830 (char-to-string (char-before (point)))))
3832 (unless (string-match (concat "[" (nth 1 erc) "\n]")
3833 (char-to-string (char-after (point))))
3834 (insert " ") (backward-char 1))
3836 (and move (backward-char 1))))
3838 (defconst org-nonsticky-props
3839 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
3842 (defun org-activate-plain-links (limit)
3843 "Run through the buffer and add overlays to links."
3846 (while (re-search-forward org-plain-link-re limit t)
3847 (setq f (get-text-property (match-beginning 0) 'face))
3848 (if (or (eq f 'org-tag)
3849 (and (listp f) (memq 'org-tag f)))
3851 (add-text-properties (match-beginning 0) (match-end 0)
3852 (list 'mouse-face 'highlight
3853 'rear-nonsticky org-nonsticky-props
3854 'keymap org-mouse-map
3856 (throw 'exit t))))))
3858 (defun org-activate-code (limit)
3859 (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
3861 (remove-text-properties (match-beginning 0) (match-end 0)
3862 '(display t invisible t intangible t))
3865 (defun org-activate-angle-links (limit)
3866 "Run through the buffer and add overlays to links."
3867 (if (re-search-forward org-angle-link-re limit t)
3869 (add-text-properties (match-beginning 0) (match-end 0)
3870 (list 'mouse-face 'highlight
3871 'rear-nonsticky org-nonsticky-props
3872 'keymap org-mouse-map
3876 (defun org-activate-footnote-links (limit)
3877 "Run through the buffer and add overlays to links."
3878 (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
3881 (add-text-properties (match-beginning 2) (match-end 2)
3882 (list 'mouse-face 'highlight
3883 'rear-nonsticky org-nonsticky-props
3884 'keymap org-mouse-map
3886 (if (= (point-at-bol) (match-beginning 2))
3887 "Footnote definition"
3888 "Footnote reference")
3892 (defun org-activate-bracket-links (limit)
3893 "Run through the buffer and add overlays to bracketed links."
3894 (if (re-search-forward org-bracket-link-regexp limit t)
3895 (let* ((help (concat "LINK: "
3896 (org-match-string-no-properties 1)))
3897 ;; FIXME: above we should remove the escapes.
3898 ;; but that requires another match, protecting match data,
3899 ;; a lot of overhead for font-lock.
3900 (ip (org-maybe-intangible
3901 (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
3902 'keymap org-mouse-map 'mouse-face 'highlight
3903 'font-lock-multiline t 'help-echo help)))
3904 (vp (list 'rear-nonsticky org-nonsticky-props
3905 'keymap org-mouse-map 'mouse-face 'highlight
3906 ' font-lock-multiline t 'help-echo help)))
3907 ;; We need to remove the invisible property here. Table narrowing
3908 ;; may have made some of this invisible.
3909 (remove-text-properties (match-beginning 0) (match-end 0)
3913 (add-text-properties (match-beginning 0) (match-beginning 3) ip)
3914 (add-text-properties (match-beginning 3) (match-end 3) vp)
3915 (add-text-properties (match-end 3) (match-end 0) ip))
3916 (add-text-properties (match-beginning 0) (match-beginning 1) ip)
3917 (add-text-properties (match-beginning 1) (match-end 1) vp)
3918 (add-text-properties (match-end 1) (match-end 0) ip))
3921 (defun org-activate-dates (limit)
3922 "Run through the buffer and add overlays to dates."
3923 (if (re-search-forward org-tsr-regexp-both limit t)
3925 (add-text-properties (match-beginning 0) (match-end 0)
3926 (list 'mouse-face 'highlight
3927 'rear-nonsticky org-nonsticky-props
3928 'keymap org-mouse-map))
3929 (when org-display-custom-times
3931 (org-display-custom-time (match-beginning 3) (match-end 3)))
3932 (org-display-custom-time (match-beginning 1) (match-end 1)))
3935 (defvar org-target-link-regexp nil
3936 "Regular expression matching radio targets in plain text.")
3937 (make-variable-buffer-local 'org-target-link-regexp)
3938 (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
3939 "Regular expression matching a link target.")
3940 (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
3941 "Regular expression matching a radio target.")
3942 (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
3943 "Regular expression matching any target.")
3945 (defun org-activate-target-links (limit)
3946 "Run through the buffer and add overlays to target matches."
3947 (when org-target-link-regexp
3948 (let ((case-fold-search t))
3949 (if (re-search-forward org-target-link-regexp limit t)
3951 (add-text-properties (match-beginning 0) (match-end 0)
3952 (list 'mouse-face 'highlight
3953 'rear-nonsticky org-nonsticky-props
3954 'keymap org-mouse-map
3955 'help-echo "Radio target link"
3956 'org-linked-text t))
3959 (defun org-update-radio-target-regexp ()
3960 "Find all radio targets in this file and update the regular expression."
3962 (when (memq 'radio org-activate-links)
3963 (setq org-target-link-regexp
3964 (org-make-target-link-regexp (org-all-targets 'radio)))
3965 (org-restart-font-lock)))
3967 (defun org-hide-wide-columns (limit)
3969 (setq s (text-property-any (point) (or limit (point-max))
3972 (setq e (next-single-property-change s 'org-cwidth))
3973 (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
3977 (defvar org-latex-and-specials-regexp nil
3978 "Regular expression for highlighting export special stuff.")
3979 (defvar org-match-substring-regexp)
3980 (defvar org-match-substring-with-braces-regexp)
3981 (defvar org-export-html-special-string-regexps)
3983 (defun org-compute-latex-and-specials-regexp ()
3984 "Compute regular expression for stuff treated specially by exporters."
3985 (if (not org-highlight-latex-fragments-and-specials)
3986 (org-set-local 'org-latex-and-specials-regexp nil)
3989 ((matchers (plist-get org-format-latex-options :matchers))
3990 (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
3991 org-latex-regexps)))
3992 (options (org-combine-plists (org-default-export-plist)
3993 (org-infile-export-plist)))
3994 (org-export-with-sub-superscripts (plist-get options :sub-superscript))
3995 (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
3996 (org-export-with-TeX-macros (plist-get options :TeX-macros))
3997 (org-export-html-expand (plist-get options :expand-quoted-html))
3998 (org-export-with-special-strings (plist-get options :special-strings))
4001 ((equal org-export-with-sub-superscripts '{})
4002 (list org-match-substring-with-braces-regexp))
4003 (org-export-with-sub-superscripts
4004 (list org-match-substring-regexp))
4007 (if org-export-with-LaTeX-fragments
4008 (mapcar (lambda (x) (nth 1 x)) latexs)))
4010 (if org-export-with-TeX-macros
4011 (list (concat "\\\\"
4013 (append (mapcar 'car org-html-entities)
4014 (if (boundp 'org-latex-entities)
4015 org-latex-entities nil))
4018 ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
4019 (re-special (if org-export-with-special-strings
4020 (mapcar (lambda (x) (car x))
4021 org-export-html-special-string-regexps)))
4025 (if org-export-html-expand "@<[^>\n]+>")
4028 'org-latex-and-specials-regexp
4029 (mapconcat 'identity (append re-latex re-sub re-macros re-special
4032 (defun org-do-latex-and-special-faces (limit)
4033 "Run through the buffer and add overlays to links."
4034 (when org-latex-and-specials-regexp
4036 (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
4038 (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
4040 '(org-code org-verbatim underline)))
4043 d (cond ((member (char-after (1+ (match-beginning 0)))
4046 (font-lock-prepend-text-property
4047 (+ d (match-beginning 0)) (match-end 0)
4048 'face 'org-latex-and-export-specials)
4049 (add-text-properties (+ d (match-beginning 0)) (match-end 0)
4050 '(font-lock-multiline t)))))
4053 (defun org-restart-font-lock ()
4054 "Restart font-lock-mode, to force refontification."
4055 (when (and (boundp 'font-lock-mode) font-lock-mode)
4057 (font-lock-mode 1)))
4059 (defun org-all-targets (&optional radio)
4060 "Return a list of all targets in this file.
4061 With optional argument RADIO, only find radio targets."
4062 (let ((re (if radio org-radio-target-regexp org-target-regexp))
4065 (goto-char (point-min))
4066 (while (re-search-forward re nil t)
4067 (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
4070 (defun org-make-target-link-regexp (targets)
4071 "Make regular expression matching all strings in TARGETS.
4072 The regular expression finds the targets also if there is a line break
4079 (while (string-match " +" x)
4080 (setq x (replace-match "\\s-+" t t x)))
4086 (defun org-activate-tags (limit)
4087 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
4089 (add-text-properties (match-beginning 1) (match-end 1)
4090 (list 'mouse-face 'highlight
4091 'rear-nonsticky org-nonsticky-props
4092 'keymap org-mouse-map))
4095 (defun org-outline-level ()
4097 (looking-at outline-regexp)
4098 (if (match-beginning 1)
4099 (+ (org-get-string-indentation (match-string 1)) 1000)
4100 (1- (- (match-end 0) (match-beginning 0))))))
4102 (defvar org-font-lock-keywords nil)
4104 (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
4105 "Regular expression matching a property line.")
4107 (defvar org-font-lock-hook nil
4108 "Functions to be called for special font lock stuff.")
4110 (defun org-font-lock-hook (limit)
4111 (run-hook-with-args 'org-font-lock-hook limit))
4113 (defun org-set-font-lock-defaults ()
4114 (let* ((em org-fontify-emphasized-text)
4115 (lk org-activate-links)
4116 (org-font-lock-extra-keywords
4119 '(org-font-lock-hook)
4121 '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
4122 (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
4124 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
4127 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
4128 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
4129 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
4131 (list org-drawer-regexp '(0 'org-special-keyword t))
4132 (list "^[ \t]*:END:" '(0 'org-special-keyword t))
4134 (list org-property-re
4135 '(1 'org-special-keyword t)
4136 '(3 'org-property-value t))
4137 (if org-format-transports-properties-p
4138 '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
4140 (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
4141 (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
4142 (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
4143 (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
4144 (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
4145 (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
4146 (if (memq 'footnote lk) '(org-activate-footnote-links
4147 (2 'org-footnote t)))
4148 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
4149 '(org-hide-wide-columns (0 nil append))
4151 (list (concat "^\\*+[ \t]+" org-todo-regexp)
4152 '(1 (org-get-todo-face 1) t))
4154 (if org-fontify-done-headline
4155 (list (concat "^[*]+ +\\<\\("
4156 (mapconcat 'regexp-quote org-done-keywords "\\|")
4158 '(2 'org-headline-done t))
4161 (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
4163 '(org-font-lock-add-tag-faces)
4165 (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
4166 (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
4167 (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
4168 (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
4171 (if (featurep 'xemacs)
4172 '(org-do-emphasis-faces (0 nil append))
4173 '(org-do-emphasis-faces)))
4175 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
4177 (if org-provide-checkbox-statistics
4178 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
4179 (0 (org-get-checkbox-statistics-face) t)))
4180 ;; Description list items
4181 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
4183 (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
4184 '(1 'org-archived prepend))
4186 '(org-do-latex-and-special-faces)
4188 '(org-activate-code (1 'org-code t))
4190 (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
4191 "\\|" org-quote-string "\\)\\>")
4192 '(1 'org-special-keyword t))
4193 '("^#.*" (0 'font-lock-comment-face t))
4195 (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
4196 ;; Now set the full font-lock-keywords
4197 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
4198 (org-set-local 'font-lock-defaults
4199 '(org-font-lock-keywords t nil nil backward-paragraph))
4200 (kill-local-variable 'font-lock-keywords) nil))
4205 (defun org-get-level-face (n)
4206 "Get the right face for match N in font-lock matching of headlines."
4207 (setq org-l (- (match-end 2) (match-beginning 1) 1))
4208 (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
4209 (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
4211 ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
4213 (t (if org-level-color-stars-only nil org-f))))
4215 (defun org-get-todo-face (kwd)
4216 "Get the right face for a TODO keyword KWD.
4217 If KWD is a number, get the corresponding match group."
4218 (if (numberp kwd) (setq kwd (match-string kwd)))
4219 (or (cdr (assoc kwd org-todo-keyword-faces))
4220 (and (member kwd org-done-keywords) 'org-done)
4223 (defun org-font-lock-add-tag-faces (limit)
4224 "Add the special tag faces."
4225 (when (and org-tag-faces org-tags-special-faces-re)
4226 (while (re-search-forward org-tags-special-faces-re limit t)
4227 (add-text-properties (match-beginning 1) (match-end 1)
4228 (list 'face (org-get-tag-face 1)
4229 'font-lock-fontified t))
4230 (backward-char 1))))
4232 (defun org-get-tag-face (kwd)
4233 "Get the right face for a TODO keyword KWD.
4234 If KWD is a number, get the corresponding match group."
4235 (if (numberp kwd) (setq kwd (match-string kwd)))
4236 (or (cdr (assoc kwd org-tag-faces))
4239 (defun org-unfontify-region (beg end &optional maybe_loudly)
4240 "Remove fontification and activation overlays from links."
4241 (font-lock-default-unfontify-region beg end)
4242 (let* ((buffer-undo-list t)
4243 (inhibit-read-only t) (inhibit-point-motion-hooks t)
4244 (inhibit-modification-hooks t)
4245 deactivate-mark buffer-file-name buffer-file-truename)
4246 (remove-text-properties beg end
4247 '(mouse-face t keymap t org-linked-text t
4248 invisible t intangible t))))
4250 ;;;; Visibility cycling, including org-goto and indirect buffer
4254 (defvar org-cycle-global-status nil)
4255 (make-variable-buffer-local 'org-cycle-global-status)
4256 (defvar org-cycle-subtree-status nil)
4257 (make-variable-buffer-local 'org-cycle-subtree-status)
4260 (defun org-cycle (&optional arg)
4261 "Visibility cycling for Org-mode.
4263 - When this function is called with a prefix argument, rotate the entire
4264 buffer through 3 states (global cycling)
4265 1. OVERVIEW: Show only top-level headlines.
4266 2. CONTENTS: Show all headlines of all levels, but no body text.
4267 3. SHOW ALL: Show everything.
4268 When called with two C-u C-u prefixes, switch to the startup visibility,
4269 determined by the variable `org-startup-folded', and by any VISIBILITY
4270 properties in the buffer.
4271 When called with three C-u C-u C-u prefixed, show the entire buffer,
4274 - When point is at the beginning of a headline, rotate the subtree started
4275 by this line through 3 different states (local cycling)
4276 1. FOLDED: Only the main headline is shown.
4277 2. CHILDREN: The main headline and the direct children are shown.
4278 From this state, you can move to one of the children
4279 and zoom in further.
4280 3. SUBTREE: Show the entire subtree, including body text.
4282 - When there is a numeric prefix, go up to a heading with level ARG, do
4283 a `show-subtree' and return to the previous cursor position. If ARG
4284 is negative, go up that many levels.
4286 - When point is not at the beginning of a headline, execute the global
4287 binding for TAB, which is re-indenting the line. See the option
4288 `org-cycle-emulate-tab' for details.
4290 - Special case: if point is at the beginning of the buffer and there is
4291 no headline in line 1, this function will act as if called with prefix arg.
4292 But only if also the variable `org-cycle-global-at-bob' is t."
4294 (org-load-modules-maybe)
4295 (let* ((outline-regexp
4296 (if (and (org-mode-p) org-cycle-include-plain-lists)
4297 "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
4299 (bob-special (and org-cycle-global-at-bob (bobp)
4300 (not (looking-at outline-regexp))))
4303 (delq 'org-optimize-window-after-visibility-change
4304 (copy-sequence org-cycle-hook))
4308 (if (or bob-special (equal arg '(4)))
4309 ;; special case: use global cycling
4315 (org-set-startup-visibility)
4316 (message "Startup visibility, plus VISIBILITY properties"))
4320 (message "Entire buffer visible, including drawers"))
4322 ((org-at-table-p 'any)
4323 ;; Enter the table or move to the next field in the table
4324 (or (org-table-recognize-table.el)
4326 (if arg (org-table-edit-field t)
4327 (org-table-justify-field-maybe)
4328 (call-interactively 'org-table-next-field)))))
4330 ((eq arg t) ;; Global cycling
4333 ((and (eq last-command this-command)
4334 (eq org-cycle-global-status 'overview))
4335 ;; We just created the overview - now do table of contents
4336 ;; This can be slow in very large buffers, so indicate action
4337 (message "CONTENTS...")
4339 (message "CONTENTS...done")
4340 (setq org-cycle-global-status 'contents)
4341 (run-hook-with-args 'org-cycle-hook 'contents))
4343 ((and (eq last-command this-command)
4344 (eq org-cycle-global-status 'contents))
4345 ;; We just showed the table of contents - now show everything
4347 (message "SHOW ALL")
4348 (setq org-cycle-global-status 'all)
4349 (run-hook-with-args 'org-cycle-hook 'all))
4352 ;; Default action: go to overview
4354 (message "OVERVIEW")
4355 (setq org-cycle-global-status 'overview)
4356 (run-hook-with-args 'org-cycle-hook 'overview))))
4358 ((and org-drawers org-drawer-regexp
4360 (beginning-of-line 1)
4361 (looking-at org-drawer-regexp)))
4362 ;; Toggle block visibility
4364 (not (get-char-property (match-end 0) 'invisible))))
4367 ;; Show-subtree, ARG levels up from here.
4369 (org-back-to-heading)
4370 (outline-up-heading (if (< arg 0) (- arg)
4371 (- (funcall outline-level) arg)))
4372 (org-show-subtree)))
4374 ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
4375 (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
4376 ;; At a heading: rotate between three different views
4377 (org-back-to-heading)
4378 (let ((goal-column 0) eoh eol eos)
4379 ;; First, some boundaries
4381 (org-back-to-heading)
4383 (beginning-of-line 2)
4384 (while (and (not (eobp)) ;; this is like `next-line'
4385 (get-char-property (1- (point)) 'invisible))
4386 (beginning-of-line 2)) (setq eol (point)))
4387 (outline-end-of-heading) (setq eoh (point))
4388 (org-end-of-subtree t)
4390 (skip-chars-forward " \t\n")
4391 (beginning-of-line 1) ; in case this is an item
4393 (setq eos (1- (point))))
4394 ;; Find out what to do next and set `this-command'
4397 ;; Nothing is hidden behind this heading
4398 (message "EMPTY ENTRY")
4399 (setq org-cycle-subtree-status nil)
4402 (outline-next-heading)
4403 (if (org-invisible-p) (org-flag-heading nil))))
4405 (not (string-match "\\S-" (buffer-substring eol eos))))
4406 ;; Entire subtree is hidden in one line: open it
4409 (message "CHILDREN")
4412 (outline-next-heading)
4413 (if (org-invisible-p) (org-flag-heading nil)))
4414 (setq org-cycle-subtree-status 'children)
4415 (run-hook-with-args 'org-cycle-hook 'children))
4416 ((and (eq last-command this-command)
4417 (eq org-cycle-subtree-status 'children))
4418 ;; We just showed the children, now show everything.
4421 (setq org-cycle-subtree-status 'subtree)
4422 (run-hook-with-args 'org-cycle-hook 'subtree))
4424 ;; Default action: hide the subtree.
4427 (setq org-cycle-subtree-status 'folded)
4428 (run-hook-with-args 'org-cycle-hook 'folded)))))
4430 ;; TAB emulation and template completion
4431 (buffer-read-only (org-back-to-heading))
4433 ((org-try-structure-completion))
4435 ((org-try-cdlatex-tab))
4437 ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
4439 (not (looking-at outline-regexp))))
4440 (call-interactively (global-key-binding "\t")))
4442 ((if (and (memq org-cycle-emulate-tab '(white whitestart))
4443 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
4444 (or (and (eq org-cycle-emulate-tab 'white)
4445 (= (match-end 0) (point-at-eol)))
4446 (and (eq org-cycle-emulate-tab 'whitestart)
4447 (>= (match-end 0) pos))))
4449 (eq org-cycle-emulate-tab t))
4450 (call-interactively (global-key-binding "\t")))
4453 (org-back-to-heading)
4457 (defun org-global-cycle (&optional arg)
4458 "Cycle the global visibility. For details see `org-cycle'.
4459 With C-u prefix arg, switch to startup visibility.
4460 With a numeric prefix, show all headlines up to that level."
4462 (let ((org-cycle-include-plain-lists
4463 (if (org-mode-p) org-cycle-include-plain-lists nil)))
4467 (hide-sublevels arg)
4468 (setq org-cycle-global-status 'contents))
4470 (org-set-startup-visibility)
4471 (message "Startup visibility, plus VISIBILITY properties."))
4473 (org-cycle '(4))))))
4475 (defun org-set-startup-visibility ()
4476 "Set the visibility required by startup options and properties."
4478 ((eq org-startup-folded t)
4480 ((eq org-startup-folded 'content)
4481 (let ((this-command 'org-cycle) (last-command 'org-cycle))
4482 (org-cycle '(4)) (org-cycle '(4)))))
4483 (org-set-visibility-according-to-property 'no-cleanup)
4484 (org-cycle-hide-archived-subtrees 'all)
4485 (org-cycle-hide-drawers 'all)
4486 (org-cycle-show-empty-lines 'all))
4488 (defun org-set-visibility-according-to-property (&optional no-cleanup)
4489 "Switch subtree visibilities according to :VISIBILITY: property."
4491 (let (org-show-entry-below state)
4493 (goto-char (point-min))
4494 (while (re-search-forward
4495 "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
4497 (setq state (match-string 1))
4499 (org-back-to-heading t)
4503 ((equal state '("fold" "folded"))
4505 ((equal state "children")
4506 (org-show-hidden-entry)
4508 ((equal state "content")
4511 (org-narrow-to-subtree)
4513 ((member state '("all" "showall"))
4516 (org-cycle-hide-archived-subtrees 'all)
4517 (org-cycle-hide-drawers 'all)
4518 (org-cycle-show-empty-lines 'all)))))
4520 (defun org-overview ()
4521 "Switch to overview mode, showing only top-level headlines.
4522 Really, this shows all headlines with level equal or greater than the level
4523 of the first headline in the buffer. This is important, because if the
4524 first headline is not level one, then (hide-sublevels 1) gives confusing
4527 (let ((level (save-excursion
4528 (goto-char (point-min))
4529 (if (re-search-forward (concat "^" outline-regexp) nil t)
4531 (goto-char (match-beginning 0))
4532 (funcall outline-level))))))
4533 (and level (hide-sublevels level))))
4535 (defun org-content (&optional arg)
4536 "Show all headlines in the buffer, like a table of contents.
4537 With numerical argument N, show content up to level N."
4540 ;; Visit all headings and show their offspring
4541 (and (integerp arg) (org-overview))
4542 (goto-char (point-max))
4544 (while (and (progn (condition-case nil
4545 (outline-previous-visible-heading 1)
4546 (error (goto-char (point-min))))
4548 (looking-at outline-regexp))
4550 (show-children (1- arg))
4552 (if (bobp) (throw 'exit nil))))))
4555 (defun org-optimize-window-after-visibility-change (state)
4556 "Adjust the window after a change in outline visibility.
4557 This function is the default value of the hook `org-cycle-hook'."
4558 (when (get-buffer-window (current-buffer))
4560 ; ((eq state 'overview) (org-first-headline-recenter 1))
4561 ; ((eq state 'overview) (org-beginning-of-line))
4562 ((eq state 'content) nil)
4563 ((eq state 'all) nil)
4564 ((eq state 'folded) nil)
4565 ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
4566 ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
4568 (defun org-compact-display-after-subtree-move ()
4569 "Show a compacter version of the tree of the entry's parent."
4571 (if (org-up-heading-safe)
4576 (org-cycle-show-empty-lines 'children)
4577 (org-cycle-hide-drawers 'children))
4580 (defun org-cycle-show-empty-lines (state)
4581 "Show empty lines above all visible headlines.
4582 The region to be covered depends on STATE when called through
4583 `org-cycle-hook'. Lisp program can use t for STATE to get the
4584 entire buffer covered. Note that an empty line is only shown if there
4585 are at least `org-cycle-separator-lines' empty lines before the headline."
4586 (when (> org-cycle-separator-lines 0)
4588 (let* ((n org-cycle-separator-lines)
4590 ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
4591 ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
4592 (t (let ((ns (number-to-string (- n 2))))
4593 (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
4594 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
4597 ((memq state '(overview contents t))
4598 (setq beg (point-min) end (point-max)))
4599 ((memq state '(children folded))
4600 (setq beg (point) end (progn (org-end-of-subtree t t)
4601 (beginning-of-line 2)
4605 (while (re-search-forward re end t)
4606 (if (not (get-char-property (match-end 1) 'invisible))
4607 (outline-flag-region
4608 (match-beginning 1) (match-end 1) nil)))))))
4609 ;; Never hide empty lines at the end of the file.
4611 (goto-char (point-max))
4612 (outline-previous-heading)
4613 (outline-end-of-heading)
4614 (if (and (looking-at "[ \t\n]+")
4615 (= (match-end 0) (point-max)))
4616 (outline-flag-region (point) (match-end 0) nil))))
4618 (defun org-show-empty-lines-in-parent ()
4619 "Move to the parent and re-show empty lines before visible headlines."
4621 (let ((context (if (org-up-heading-safe) 'children 'overview)))
4622 (org-cycle-show-empty-lines context))))
4624 (defun org-cycle-hide-drawers (state)
4625 "Re-hide all drawers after a visibility state change."
4626 (when (and (org-mode-p)
4627 (not (memq state '(overview folded))))
4629 (let* ((globalp (memq state '(contents all)))
4630 (beg (if globalp (point-min) (point)))
4631 (end (if globalp (point-max) (org-end-of-subtree t))))
4633 (while (re-search-forward org-drawer-regexp end t)
4634 (org-flag-drawer t))))))
4636 (defun org-flag-drawer (flag)
4638 (beginning-of-line 1)
4639 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
4640 (let ((b (match-end 0))
4641 (outline-regexp org-outline-regexp))
4642 (if (re-search-forward
4644 (save-excursion (outline-next-heading) (point)) t)
4645 (outline-flag-region b (point-at-eol) flag)
4646 (error ":END: line missing"))))))
4648 (defun org-subtree-end-visible-p ()
4649 "Is the end of the current subtree visible?"
4650 (pos-visible-in-window-p
4651 (save-excursion (org-end-of-subtree t) (point))))
4653 (defun org-first-headline-recenter (&optional N)
4654 "Move cursor to the first headline and recenter the headline.
4655 Optional argument N means, put the headline into the Nth line of the window."
4656 (goto-char (point-min))
4657 (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
4659 (recenter (prefix-numeric-value N))))
4663 (defvar org-goto-window-configuration nil)
4664 (defvar org-goto-marker nil)
4665 (defvar org-goto-map
4666 (let ((map (make-sparse-keymap)))
4667 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
4668 (while (setq cmd (pop cmds))
4669 (substitute-key-definition cmd cmd map global-map)))
4670 (suppress-keymap map)
4671 (org-defkey map "\C-m" 'org-goto-ret)
4672 (org-defkey map [(return)] 'org-goto-ret)
4673 (org-defkey map [(left)] 'org-goto-left)
4674 (org-defkey map [(right)] 'org-goto-right)
4675 (org-defkey map [(control ?g)] 'org-goto-quit)
4676 (org-defkey map "\C-i" 'org-cycle)
4677 (org-defkey map [(tab)] 'org-cycle)
4678 (org-defkey map [(down)] 'outline-next-visible-heading)
4679 (org-defkey map [(up)] 'outline-previous-visible-heading)
4680 (if org-goto-auto-isearch
4681 (if (fboundp 'define-key-after)
4682 (define-key-after map [t] 'org-goto-local-auto-isearch)
4684 (org-defkey map "q" 'org-goto-quit)
4685 (org-defkey map "n" 'outline-next-visible-heading)
4686 (org-defkey map "p" 'outline-previous-visible-heading)
4687 (org-defkey map "f" 'outline-forward-same-level)
4688 (org-defkey map "b" 'outline-backward-same-level)
4689 (org-defkey map "u" 'outline-up-heading))
4690 (org-defkey map "/" 'org-occur)
4691 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
4692 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
4693 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
4694 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
4695 (org-defkey map "\C-c\C-u" 'outline-up-heading)
4698 (defconst org-goto-help
4699 "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
4700 RET=jump to location [Q]uit and return to previous location
4701 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
4703 (defvar org-goto-start-pos) ; dynamically scoped parameter
4705 ;; FIXME: Docstring doe not mention both interfaces
4706 (defun org-goto (&optional alternative-interface)
4707 "Look up a different location in the current file, keeping current visibility.
4709 When you want look-up or go to a different location in a document, the
4710 fastest way is often to fold the entire buffer and then dive into the tree.
4711 This method has the disadvantage, that the previous location will be folded,
4712 which may not be what you want.
4714 This command works around this by showing a copy of the current buffer
4715 in an indirect buffer, in overview mode. You can dive into the tree in
4716 that copy, use org-occur and incremental search to find a location.
4717 When pressing RET or `Q', the command returns to the original buffer in
4718 which the visibility is still unchanged. After RET is will also jump to
4719 the location selected in the indirect buffer and expose the
4720 the headline hierarchy above."
4722 (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
4723 (org-refile-use-outline-path t)
4725 (if (not alternative-interface)
4727 (if (eq org-goto-interface 'outline)
4728 'outline-path-completion
4730 (org-goto-start-pos (point))
4732 (if (eq interface 'outline)
4733 (car (org-get-location (current-buffer) org-goto-help))
4734 (nth 3 (org-refile-get-location "Goto: ")))))
4737 (org-mark-ring-push org-goto-start-pos)
4738 (goto-char selected-point)
4739 (if (or (org-invisible-p) (org-invisible-p2))
4740 (org-show-context 'org-goto)))
4743 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
4744 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
4745 (defvar org-goto-local-auto-isearch-map) ; defined below
4747 (defun org-get-location (buf help)
4748 "Let the user select a location in the Org-mode buffer BUF.
4749 This function uses a recursive edit. It returns the selected position
4751 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
4752 (isearch-hide-immediately nil)
4753 (isearch-search-fun-function
4754 (lambda () 'org-goto-local-search-headings))
4755 (org-goto-selected-point org-goto-exit-command))
4757 (save-window-excursion
4758 (delete-other-windows)
4759 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
4762 (make-indirect-buffer (current-buffer) "*org-goto*")
4763 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
4764 (with-output-to-temp-buffer "*Help*"
4766 (org-fit-window-to-buffer (get-buffer-window "*Help*"))
4767 (setq buffer-read-only nil)
4768 (let ((org-startup-truncated t)
4769 (org-startup-folded nil)
4770 (org-startup-align-all-tables nil))
4773 (setq buffer-read-only t)
4774 (if (and (boundp 'org-goto-start-pos)
4775 (integer-or-marker-p org-goto-start-pos))
4776 (let ((org-show-hierarchy-above t)
4777 (org-show-siblings t)
4778 (org-show-following-heading t))
4779 (goto-char org-goto-start-pos)
4780 (and (org-invisible-p) (org-show-context)))
4781 (goto-char (point-min)))
4782 (org-beginning-of-line)
4783 (message "Select location and press RET")
4784 (use-local-map org-goto-map)
4787 (kill-buffer "*org-goto*")
4788 (cons org-goto-selected-point org-goto-exit-command)))
4790 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
4791 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
4792 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
4793 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
4795 (defun org-goto-local-search-headings (string bound noerror)
4796 "Search and make sure that any matches are in headlines."
4798 (while (if isearch-forward
4799 (search-forward string bound noerror)
4800 (search-backward string bound noerror))
4801 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
4802 (and (member :headline context)
4803 (not (member :tags context))))
4804 (throw 'return (point))))))
4806 (defun org-goto-local-auto-isearch ()
4809 (goto-char (point-min))
4810 (let ((keys (this-command-keys)))
4811 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
4813 (isearch-process-search-char (string-to-char keys)))))
4815 (defun org-goto-ret (&optional arg)
4816 "Finish `org-goto' by going to the new location."
4818 (setq org-goto-selected-point (point)
4819 org-goto-exit-command 'return)
4822 (defun org-goto-left ()
4823 "Finish `org-goto' by going to the new location."
4825 (if (org-on-heading-p)
4827 (beginning-of-line 1)
4828 (setq org-goto-selected-point (point)
4829 org-goto-exit-command 'left)
4831 (error "Not on a heading")))
4833 (defun org-goto-right ()
4834 "Finish `org-goto' by going to the new location."
4836 (if (org-on-heading-p)
4838 (setq org-goto-selected-point (point)
4839 org-goto-exit-command 'right)
4841 (error "Not on a heading")))
4843 (defun org-goto-quit ()
4844 "Finish `org-goto' without cursor motion."
4846 (setq org-goto-selected-point nil)
4847 (setq org-goto-exit-command 'quit)
4850 ;;; Indirect buffer display of subtrees
4852 (defvar org-indirect-dedicated-frame nil
4853 "This is the frame being used for indirect tree display.")
4854 (defvar org-last-indirect-buffer nil)
4856 (defun org-tree-to-indirect-buffer (&optional arg)
4857 "Create indirect buffer and narrow it to current subtree.
4858 With numerical prefix ARG, go up to this level and then take that tree.
4859 If ARG is negative, go up that many levels.
4860 If `org-indirect-buffer-display' is not `new-frame', the command removes the
4861 indirect buffer previously made with this command, to avoid proliferation of
4862 indirect buffers. However, when you call the command with a `C-u' prefix, or
4863 when `org-indirect-buffer-display' is `new-frame', the last buffer
4864 is kept so that you can work with several indirect buffers at the same time.
4865 If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
4866 requests that a new frame be made for the new buffer, so that the dedicated
4867 frame is not changed."
4869 (let ((cbuf (current-buffer))
4870 (cwin (selected-window))
4872 beg end level heading ibuf)
4874 (org-back-to-heading t)
4876 (setq level (org-outline-level))
4877 (if (< arg 0) (setq arg (+ level arg)))
4878 (while (> (setq level (org-outline-level)) arg)
4879 (outline-up-heading 1 t)))
4881 heading (org-get-heading))
4882 (org-end-of-subtree t) (setq end (point)))
4883 (if (and (buffer-live-p org-last-indirect-buffer)
4884 (not (eq org-indirect-buffer-display 'new-frame))
4886 (kill-buffer org-last-indirect-buffer))
4887 (setq ibuf (org-get-indirect-buffer cbuf)
4888 org-last-indirect-buffer ibuf)
4890 ((or (eq org-indirect-buffer-display 'new-frame)
4891 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
4892 (select-frame (make-frame))
4893 (delete-other-windows)
4894 (switch-to-buffer ibuf)
4895 (org-set-frame-title heading))
4896 ((eq org-indirect-buffer-display 'dedicated-frame)
4898 (select-frame (or (and org-indirect-dedicated-frame
4899 (frame-live-p org-indirect-dedicated-frame)
4900 org-indirect-dedicated-frame)
4901 (setq org-indirect-dedicated-frame (make-frame)))))
4902 (delete-other-windows)
4903 (switch-to-buffer ibuf)
4904 (org-set-frame-title (concat "Indirect: " heading)))
4905 ((eq org-indirect-buffer-display 'current-window)
4906 (switch-to-buffer ibuf))
4907 ((eq org-indirect-buffer-display 'other-window)
4908 (pop-to-buffer ibuf))
4909 (t (error "Invalid value.")))
4910 (if (featurep 'xemacs)
4911 (save-excursion (org-mode) (turn-on-font-lock)))
4912 (narrow-to-region beg end)
4915 (and (window-live-p cwin) (select-window cwin))))
4917 (defun org-get-indirect-buffer (&optional buffer)
4918 (setq buffer (or buffer (current-buffer)))
4919 (let ((n 1) (base (buffer-name buffer)) bname)
4920 (while (buffer-live-p
4921 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
4924 (make-indirect-buffer buffer bname 'clone)
4925 (error (make-indirect-buffer buffer bname)))))
4927 (defun org-set-frame-title (title)
4928 "Set the title of the current frame to the string TITLE."
4929 ;; FIXME: how to name a single frame in XEmacs???
4930 (unless (featurep 'xemacs)
4931 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
4933 ;;;; Structure editing
4935 ;;; Inserting headlines
4937 (defun org-previous-line-empty-p ()
4940 (or (beginning-of-line 0) t)
4942 (looking-at "[ \t]*$")))))
4944 (defun org-insert-heading (&optional force-heading)
4945 "Insert a new heading or item with same depth at point.
4946 If point is in a plain list and FORCE-HEADING is nil, create a new list item.
4947 If point is at the beginning of a headline, insert a sibling before the
4948 current headline. If point is not at the beginning, do not split the line,
4949 but create the new headline after the current line."
4951 (if (= (buffer-size) 0)
4953 (when (or force-heading (not (org-insert-item)))
4954 (let* ((empty-line-p nil)
4955 (head (save-excursion
4958 (org-back-to-heading)
4959 (setq empty-line-p (org-previous-line-empty-p))
4962 (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
4963 (blank (if (eq blank-a 'auto) empty-line-p blank-a))
4964 pos hide-previous previous-pos)
4966 ((and (org-on-heading-p) (bolp)
4968 (save-excursion (backward-char 1) (not (org-invisible-p)))))
4969 ;; insert before the current line
4970 (open-line (if blank 2 1)))
4974 (backward-char 1) (not (org-invisible-p)))))
4975 ;; insert right here
4978 ;; somewhere in the line
4980 (setq previous-pos (point-at-bol))
4982 (setq hide-previous (org-invisible-p)))
4983 (and org-insert-heading-respect-content (org-show-subtree))
4985 (and (org-get-alist-option org-M-RET-may-split-line 'headline)
4988 (goto-char (point-at-bol))
4989 (and (looking-at org-complex-heading-regexp)
4990 (> p (match-beginning 4)))))))
4993 (org-insert-heading-respect-content
4994 (org-end-of-subtree nil t)
4995 (or (bolp) (newline))
4996 (or (org-previous-line-empty-p)
4997 (and blank (newline)))
5003 (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
5004 (setq tags (and (match-end 2) (match-string 2)))
5006 (delete-region (match-beginning 1) (match-end 1)))
5007 (setq pos (point-at-bol))
5008 (or split (end-of-line 1))
5009 (delete-horizontal-space)
5010 (newline (if blank 2 1))
5016 (org-set-tags nil 'align))))
5018 (or split (end-of-line 1))
5019 (newline (if blank 2 1)))))))
5020 (insert head) (just-one-space)
5023 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
5024 (when (and org-insert-heading-respect-content hide-previous)
5026 (goto-char previous-pos)
5028 (run-hooks 'org-insert-heading-hook)))))
5030 (defun org-get-heading (&optional no-tags)
5031 "Return the heading of the current entry, without the stars."
5033 (org-back-to-heading t)
5036 (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
5037 "\\*+[ \t]+\\([^\r\n]*\\)"))
5038 (match-string 1) "")))
5040 (defun org-heading-components ()
5041 "Return the components of the current heading.
5042 This is a list with the following elements:
5043 - the level as an integer
5044 - the reduced level, different if `org-odd-levels-only' is set.
5045 - the TODO keyword, or nil
5046 - the priority character, like ?A, or nil if no priority is given
5047 - the headline text itself, or the tags string if no headline text
5048 - the tags string, or nil."
5050 (org-back-to-heading t)
5051 (if (looking-at org-complex-heading-regexp)
5052 (list (length (match-string 1))
5053 (org-reduced-level (length (match-string 1)))
5054 (org-match-string-no-properties 2)
5055 (and (match-end 3) (aref (match-string 3) 2))
5056 (org-match-string-no-properties 4)
5057 (org-match-string-no-properties 5)))))
5059 (defun org-insert-heading-after-current ()
5060 "Insert a new heading with same level as current, after current subtree."
5062 (org-back-to-heading)
5063 (org-insert-heading)
5064 (org-move-subtree-down)
5067 (defun org-insert-heading-respect-content ()
5069 (let ((org-insert-heading-respect-content t))
5070 (org-insert-heading t)))
5072 (defun org-insert-todo-heading-respect-content (&optional force-state)
5074 (let ((org-insert-heading-respect-content t))
5075 (org-insert-todo-heading force-state t)))
5077 (defun org-insert-todo-heading (arg &optional force-heading)
5078 "Insert a new heading with the same level and TODO state as current heading.
5079 If the heading has no TODO state, or if the state is DONE, use the first
5080 state (TODO by default). Also with prefix arg, force first state."
5082 (when (or force-heading (not (org-insert-item 'checkbox)))
5083 (org-insert-heading force-heading)
5085 (org-back-to-heading)
5086 (outline-previous-heading)
5087 (looking-at org-todo-line-regexp))
5089 (not (match-beginning 2))
5090 (member (match-string 2) org-done-keywords))
5091 (insert (car org-todo-keywords-1) " ")
5092 (insert (match-string 2) " "))
5093 (when org-provide-todo-statistics
5094 (org-update-parent-todo-statistics))))
5096 (defun org-insert-subheading (arg)
5097 "Insert a new subheading and demote it.
5098 Works for outline headings and for plain lists alike."
5100 (org-insert-heading arg)
5102 ((org-on-heading-p) (org-do-demote))
5103 ((org-at-item-p) (org-indent-item 1))))
5105 (defun org-insert-todo-subheading (arg)
5106 "Insert a new subheading with TODO keyword or checkbox and demote it.
5107 Works for outline headings and for plain lists alike."
5109 (org-insert-todo-heading arg)
5111 ((org-on-heading-p) (org-do-demote))
5112 ((org-at-item-p) (org-indent-item 1))))
5114 ;;; Promotion and Demotion
5116 (defun org-promote-subtree ()
5117 "Promote the entire subtree.
5118 See also `org-promote'."
5121 (org-map-tree 'org-promote))
5122 (org-fix-position-after-promote))
5124 (defun org-demote-subtree ()
5125 "Demote the entire subtree. See `org-demote'.
5126 See also `org-promote'."
5129 (org-map-tree 'org-demote))
5130 (org-fix-position-after-promote))
5133 (defun org-do-promote ()
5134 "Promote the current heading higher up the tree.
5135 If the region is active in `transient-mark-mode', promote all headings
5139 (if (org-region-active-p)
5140 (org-map-region 'org-promote (region-beginning) (region-end))
5142 (org-fix-position-after-promote))
5144 (defun org-do-demote ()
5145 "Demote the current heading lower down the tree.
5146 If the region is active in `transient-mark-mode', demote all headings
5150 (if (org-region-active-p)
5151 (org-map-region 'org-demote (region-beginning) (region-end))
5153 (org-fix-position-after-promote))
5155 (defun org-fix-position-after-promote ()
5156 "Make sure that after pro/demotion cursor position is right."
5157 (let ((pos (point)))
5158 (when (save-excursion
5159 (beginning-of-line 1)
5160 (looking-at org-todo-line-regexp)
5161 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
5162 (cond ((eobp) (insert " "))
5163 ((eolp) (insert " "))
5164 ((equal (char-after) ?\ ) (forward-char 1))))))
5166 (defun org-reduced-level (l)
5167 "Compute the effective level of a heading.
5168 This takes into account the setting of `org-odd-levels-only'."
5169 (if org-odd-levels-only (1+ (floor (/ l 2))) l))
5171 (defun org-get-valid-level (level &optional change)
5172 "Rectify a level change under the influence of `org-odd-levels-only'
5173 LEVEL is a current level, CHANGE is by how much the level should be
5174 modified. Even if CHANGE is nil, LEVEL may be returned modified because
5175 even level numbers will become the next higher odd number."
5176 (if org-odd-levels-only
5177 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
5178 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
5179 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
5180 (max 1 (+ level change))))
5182 (if (boundp 'define-obsolete-function-alias)
5183 (if (or (featurep 'xemacs) (< emacs-major-version 23))
5184 (define-obsolete-function-alias 'org-get-legal-level
5185 'org-get-valid-level)
5186 (define-obsolete-function-alias 'org-get-legal-level
5187 'org-get-valid-level "23.1")))
5189 (defun org-promote ()
5190 "Promote the current heading higher up the tree.
5191 If the region is active in `transient-mark-mode', promote all headings
5193 (org-back-to-heading t)
5194 (let* ((level (save-match-data (funcall outline-level)))
5195 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
5196 (diff (abs (- level (length up-head) -1))))
5197 (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
5198 (replace-match up-head nil t)
5199 ;; Fixup tag positioning
5200 (and org-auto-align-tags (org-set-tags nil t))
5201 (if org-adapt-indentation (org-fixup-indentation (- diff)))))
5203 (defun org-demote ()
5204 "Demote the current heading lower down the tree.
5205 If the region is active in `transient-mark-mode', demote all headings
5207 (org-back-to-heading t)
5208 (let* ((level (save-match-data (funcall outline-level)))
5209 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
5210 (diff (abs (- level (length down-head) -1))))
5211 (replace-match down-head nil t)
5212 ;; Fixup tag positioning
5213 (and org-auto-align-tags (org-set-tags nil t))
5214 (if org-adapt-indentation (org-fixup-indentation diff))))
5216 (defun org-map-tree (fun)
5217 "Call FUN for every heading underneath the current one."
5218 (org-back-to-heading)
5219 (let ((level (funcall outline-level)))
5223 (outline-next-heading)
5224 (> (funcall outline-level) level))
5228 (defun org-map-region (fun beg end)
5229 "Call FUN for every heading between BEG and END."
5230 (let ((org-ignore-region t))
5232 (setq end (copy-marker end))
5234 (if (and (re-search-forward (concat "^" outline-regexp) nil t)
5238 (outline-next-heading)
5243 (defun org-fixup-indentation (diff)
5244 "Change the indentation in the current entry by DIFF
5245 However, if any line in the current entry has no indentation, or if it
5246 would end up with no indentation after the change, nothing at all is done."
5248 (let ((end (save-excursion (outline-next-heading)
5250 (prohibit (if (> diff 0)
5252 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
5254 (unless (save-excursion (end-of-line 1)
5255 (re-search-forward prohibit end t))
5256 (while (and (< (point) end)
5257 (re-search-forward "^[ \t]+" end t))
5258 (goto-char (match-end 0))
5259 (setq col (current-column))
5260 (if (< diff 0) (replace-match ""))
5261 (org-indent-to-column (+ diff col))))
5262 (move-marker end nil))))
5264 (defun org-convert-to-odd-levels ()
5265 "Convert an org-mode file with all levels allowed to one with odd levels.
5266 This will leave level 1 alone, convert level 2 to level 3, level 3 to
5269 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
5270 (let ((org-odd-levels-only nil) n)
5272 (goto-char (point-min))
5273 (while (re-search-forward "^\\*\\*+ " nil t)
5274 (setq n (- (length (match-string 0)) 2))
5275 (while (>= (setq n (1- n)) 0)
5277 (end-of-line 1))))))
5280 (defun org-convert-to-oddeven-levels ()
5281 "Convert an org-mode file with only odd levels to one with odd and even levels.
5282 This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
5283 section with an even level, conversion would destroy the structure of the file. An error
5284 is signaled in this case."
5286 (goto-char (point-min))
5287 ;; First check if there are no even levels
5288 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
5289 (org-show-context t)
5290 (error "Not all levels are odd in this file. Conversion not possible."))
5291 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
5292 (let ((org-odd-levels-only nil) n)
5294 (goto-char (point-min))
5295 (while (re-search-forward "^\\*\\*+ " nil t)
5296 (setq n (/ (1- (length (match-string 0))) 2))
5297 (while (>= (setq n (1- n)) 0)
5299 (end-of-line 1))))))
5301 (defun org-tr-level (n)
5302 "Make N odd if required."
5303 (if org-odd-levels-only (1+ (/ n 2)) n))
5305 ;;; Vertical tree motion, cutting and pasting of subtrees
5307 (defun org-move-subtree-up (&optional arg)
5308 "Move the current subtree up past ARG headlines of the same level."
5310 (org-move-subtree-down (- (prefix-numeric-value arg))))
5312 (defun org-move-subtree-down (&optional arg)
5313 "Move the current subtree down past ARG headlines of the same level."
5315 (setq arg (prefix-numeric-value arg))
5316 (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
5317 'outline-get-last-sibling))
5318 (ins-point (make-marker))
5320 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
5322 (org-back-to-heading)
5325 (setq ne-beg (org-back-over-empty-lines))
5328 (save-excursion (outline-end-of-heading)
5329 (setq folded (org-invisible-p)))
5330 (outline-end-of-subtree))
5331 (outline-next-heading)
5332 (setq ne-end (org-back-over-empty-lines))
5335 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
5336 ;; include less whitespace
5339 (forward-line (- ne-beg ne-end))
5340 (setq beg (point))))
5341 ;; Find insertion point, with error handling
5343 (or (and (funcall movfunc) (looking-at outline-regexp))
5344 (progn (goto-char beg0)
5345 (error "Cannot move past superior level or buffer limit")))
5346 (setq cnt (1- cnt)))
5348 ;; Moving forward - still need to move over subtree
5349 (progn (org-end-of-subtree t t)
5351 (org-back-over-empty-lines)
5352 (or (bolp) (newline)))))
5353 (setq ne-ins (org-back-over-empty-lines))
5354 (move-marker ins-point (point))
5355 (setq txt (buffer-substring beg end))
5356 (org-save-markers-in-region beg end)
5357 (delete-region beg end)
5358 (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
5359 (or (bobp) (outline-flag-region (1- (point)) (point) nil))
5360 (let ((bbb (point)))
5361 (insert-before-markers txt)
5362 (org-reinstall-markers-in-region bbb)
5363 (move-marker ins-point bbb))
5364 (or (bolp) (insert "\n"))
5365 (setq ins-end (point))
5366 (goto-char ins-point)
5367 (org-skip-whitespace)
5368 (when (and (< arg 0)
5369 (org-first-sibling-p)
5371 ;; Move whitespace back to beginning
5374 (let ((kill-whole-line t))
5375 (kill-line (- ne-ins ne-beg)) (point)))
5376 (insert (make-string (- ne-ins ne-beg) ?\n)))
5377 (move-marker ins-point nil)
5378 (org-compact-display-after-subtree-move)
5379 (org-show-empty-lines-in-parent)
5383 (org-cycle-hide-drawers 'children))))
5385 (defvar org-subtree-clip ""
5386 "Clipboard for cut and paste of subtrees.
5387 This is actually only a copy of the kill, because we use the normal kill
5388 ring. We need it to check if the kill was created by `org-copy-subtree'.")
5390 (defvar org-subtree-clip-folded nil
5391 "Was the last copied subtree folded?
5392 This is used to fold the tree back after pasting.")
5394 (defun org-cut-subtree (&optional n)
5395 "Cut the current subtree into the clipboard.
5396 With prefix arg N, cut this many sequential subtrees.
5397 This is a short-hand for marking the subtree and then cutting it."
5399 (org-copy-subtree n 'cut))
5401 (defun org-copy-subtree (&optional n cut force-store-markers)
5402 "Cut the current subtree into the clipboard.
5403 With prefix arg N, cut this many sequential subtrees.
5404 This is a short-hand for marking the subtree and then copying it.
5405 If CUT is non-nil, actually cut the subtree.
5406 If FORCE-STORE-MARKERS is non-nil, store the relative locations
5407 of some markers in the region, even if CUT is non-nil. This is
5408 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
5410 (let (beg end folded (beg0 (point)))
5412 (org-back-to-heading nil) ; take what looks like a subtree
5413 (org-back-to-heading t)) ; take what is really there
5414 (org-back-over-empty-lines)
5416 (skip-chars-forward " \t\r\n")
5418 (save-excursion (outline-end-of-heading)
5419 (setq folded (org-invisible-p)))
5421 (outline-forward-same-level (1- n))
5423 (org-end-of-subtree t t))
5424 (org-back-over-empty-lines)
5428 (setq org-subtree-clip-folded folded)
5429 (when (or cut force-store-markers)
5430 (org-save-markers-in-region beg end))
5431 (if cut (kill-region beg end) (copy-region-as-kill beg end))
5432 (setq org-subtree-clip (current-kill 0))
5433 (message "%s: Subtree(s) with %d characters"
5434 (if cut "Cut" "Copied")
5435 (length org-subtree-clip)))))
5437 (defun org-paste-subtree (&optional level tree for-yank)
5438 "Paste the clipboard as a subtree, with modification of headline level.
5439 The entire subtree is promoted or demoted in order to match a new headline
5442 If the cursor is at the beginning of a headline, the same level as
5443 that headline is used to paste the tree
5445 If not, the new level is derived from the *visible* headings
5446 before and after the insertion point, and taken to be the inferior headline
5447 level of the two. So if the previous visible heading is level 3 and the
5448 next is level 4 (or vice versa), level 4 will be used for insertion.
5449 This makes sure that the subtree remains an independent subtree and does
5450 not swallow low level entries.
5452 You can also force a different level, either by using a numeric prefix
5453 argument, or by inserting the heading marker by hand. For example, if the
5454 cursor is after \"*****\", then the tree will be shifted to level 5.
5456 If optional TREE is given, use this text instead of the kill ring.
5458 When FOR-YANK is set, this is called by `org-yank'. In this case, do not
5459 move back over whitespace before inserting, and move point to the end of
5460 the inserted text when done."
5462 (unless (org-kill-is-subtree-p tree)
5464 (substitute-command-keys
5465 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
5466 (let* ((visp (not (org-invisible-p)))
5467 (txt (or tree (and kill-ring (current-kill 0))))
5468 (^re (concat "^\\(" outline-regexp "\\)"))
5469 (re (concat "\\(" outline-regexp "\\)"))
5470 (^re_ (concat "\\(\\*+\\)[ \t]*"))
5472 (old-level (if (string-match ^re txt)
5473 (- (match-end 0) (match-beginning 0) 1)
5475 (force-level (cond (level (prefix-numeric-value level))
5476 ((and (looking-at "[ \t]*$")
5478 ^re_ (buffer-substring
5479 (point-at-bol) (point))))
5480 (- (match-end 1) (match-beginning 1)))
5482 (looking-at org-outline-regexp))
5483 (- (match-end 0) (point) 1))
5485 (previous-level (save-excursion
5488 (outline-previous-visible-heading 1)
5490 (- (match-end 0) (match-beginning 0) 1)
5493 (next-level (save-excursion
5496 (or (looking-at outline-regexp)
5497 (outline-next-visible-heading 1))
5499 (- (match-end 0) (match-beginning 0) 1)
5502 (new-level (or force-level (max previous-level next-level)))
5503 (shift (if (or (= old-level -1)
5505 (= old-level new-level))
5507 (- new-level old-level)))
5508 (delta (if (> shift 0) -1 1))
5509 (func (if (> shift 0) 'org-demote 'org-promote))
5510 (org-odd-levels-only nil)
5512 ;; Remove the forced level indicator
5514 (delete-region (point-at-bol) (point)))
5516 (beginning-of-line 1)
5517 (unless for-yank (org-back-over-empty-lines))
5519 (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
5520 (insert-before-markers txt)
5521 (unless (string-match "\n\\'" txt) (insert "\n"))
5522 (setq newend (point))
5523 (org-reinstall-markers-in-region beg)
5526 (skip-chars-forward " \t\n\r")
5528 (if (and (org-invisible-p) visp)
5529 (save-excursion (outline-show-heading)))
5530 ;; Shift if necessary
5533 (narrow-to-region beg end)
5534 (while (not (= shift 0))
5535 (org-map-region func (point-min) (point-max))
5536 (setq shift (+ delta shift)))
5537 (goto-char (point-min))
5538 (setq newend (point-max))))
5539 (when (or (interactive-p) for-yank)
5540 (message "Clipboard pasted as level %d subtree" new-level))
5541 (if (and (not for-yank) ; in this case, org-yank will decide about folding
5543 (eq org-subtree-clip (current-kill 0))
5544 org-subtree-clip-folded)
5545 ;; The tree was folded before it was killed/copied
5547 (and for-yank (goto-char newend))))
5549 (defun org-kill-is-subtree-p (&optional txt)
5550 "Check if the current kill is an outline subtree, or a set of trees.
5551 Returns nil if kill does not start with a headline, or if the first
5552 headline level is not the largest headline level in the tree.
5553 So this will actually accept several entries of equal levels as well,
5554 which is OK for `org-paste-subtree'.
5555 If optional TXT is given, check this string instead of the current kill."
5556 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
5557 (start-level (and kill
5558 (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
5559 org-outline-regexp "\\)")
5561 (- (match-end 2) (match-beginning 2) 1)))
5562 (re (concat "^" org-outline-regexp))
5563 (start (1+ (or (match-beginning 2) -1))))
5564 (if (not start-level)
5566 nil) ;; does not even start with a heading
5568 (while (setq start (string-match re kill (1+ start)))
5569 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
5573 (defvar org-markers-to-move nil
5574 "Markers that should be moved with a cut-and-paste operation.
5575 Those markers are stored together with their positions relative to
5576 the start of the region.")
5578 (defun org-save-markers-in-region (beg end)
5579 "Check markers in region.
5580 If these markers are between BEG and END, record their position relative
5581 to BEG, so that after moving the block of text, we can put the markers back
5583 This function gets called just before an entry or tree gets cut from the
5584 buffer. After re-insertion, `org-reinstall-markers-in-region' must be
5585 called immediately, to move the markers with the entries."
5586 (setq org-markers-to-move nil)
5587 (when (featurep 'org-clock)
5588 (org-clock-save-markers-for-cut-and-paste beg end))
5589 (when (featurep 'org-agenda)
5590 (org-agenda-save-markers-for-cut-and-paste beg end)))
5592 (defun org-check-and-save-marker (marker beg end)
5593 "Check if MARKER is between BEG and END.
5594 If yes, remember the marker and the distance to BEG."
5595 (when (and (marker-buffer marker)
5596 (equal (marker-buffer marker) (current-buffer)))
5597 (if (and (>= marker beg) (< marker end))
5598 (push (cons marker (- marker beg)) org-markers-to-move))))
5600 (defun org-reinstall-markers-in-region (beg)
5601 "Move all remembered markers to their position relative to BEG."
5603 (move-marker (car x) (+ beg (cdr x))))
5604 org-markers-to-move)
5605 (setq org-markers-to-move nil))
5607 (defun org-narrow-to-subtree ()
5608 "Narrow buffer to the current subtree."
5613 (progn (org-back-to-heading) (point))
5614 (progn (org-end-of-subtree t) (point))))))
5619 (defun org-sort (with-case)
5620 "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
5621 Optional argument WITH-CASE means sort case-sensitively."
5623 (if (org-at-table-p)
5624 (org-call-with-arg 'org-table-sort-lines with-case)
5625 (org-call-with-arg 'org-sort-entries-or-items with-case)))
5627 (defun org-sort-remove-invisible (s)
5628 (remove-text-properties 0 (length s) org-rm-props s)
5629 (while (string-match org-bracket-link-regexp s)
5630 (setq s (replace-match (if (match-end 2)
5632 (match-string 1 s)) t t s)))
5635 (defvar org-priority-regexp) ; defined later in the file
5637 (defun org-sort-entries-or-items
5638 (&optional with-case sorting-type getkey-func compare-func property)
5639 "Sort entries on a certain level of an outline tree.
5640 If there is an active region, the entries in the region are sorted.
5641 Else, if the cursor is before the first entry, sort the top-level items.
5642 Else, the children of the entry at point are sorted.
5644 Sorting can be alphabetically, numerically, and by date/time as given by
5645 the first time stamp in the entry. The command prompts for the sorting
5646 type unless it has been given to the function through the SORTING-TYPE
5647 argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
5648 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
5649 called with point at the beginning of the record. It must return either
5650 a string or a number that should serve as the sorting key for that record.
5652 Comparing entries ignores case by default. However, with an optional argument
5653 WITH-CASE, the sorting considers case as well."
5655 (let ((case-func (if with-case 'identity 'downcase))
5656 start beg end stars re re2
5657 txt what tmp plain-list-p)
5658 ;; Find beginning and end of region to sort
5660 ((org-region-active-p)
5661 ;; we will sort the region
5662 (setq end (region-end)
5664 (goto-char (region-beginning))
5665 (if (not (org-on-heading-p)) (outline-next-heading))
5666 (setq start (point)))
5668 ;; we will sort this plain list
5669 (org-beginning-of-item-list) (setq start (point))
5670 (org-end-of-item-list) (setq end (point))
5672 (setq plain-list-p t
5674 ((or (org-on-heading-p)
5675 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
5676 ;; we will sort the children of the current headline
5677 (org-back-to-heading)
5679 end (progn (org-end-of-subtree t t)
5680 (org-back-over-empty-lines)
5685 (outline-next-heading))
5687 ;; we will sort the top-level entries in this file
5688 (goto-char (point-min))
5689 (or (org-on-heading-p) (outline-next-heading))
5690 (setq start (point) end (point-max) what "top-level")
5695 (if (>= beg end) (error "Nothing to sort"))
5697 (unless plain-list-p
5698 (looking-at "\\(\\*+\\)")
5699 (setq stars (match-string 1)
5700 re (concat "^" (regexp-quote stars) " +")
5701 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
5702 txt (buffer-substring beg end))
5703 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
5704 (if (and (not (equal stars "*")) (string-match re2 txt))
5705 (error "Region to sort contains a level above the first entry")))
5707 (unless sorting-type
5710 "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
5711 "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:")
5713 (setq sorting-type (read-char-exclusive))
5715 (and (= (downcase sorting-type) ?f)
5717 (org-ido-completing-read "Sort using function: "
5718 obarray 'fboundp t nil nil))
5719 (setq getkey-func (intern getkey-func)))
5721 (and (= (downcase sorting-type) ?r)
5723 (org-ido-completing-read "Property: "
5724 (mapcar 'list (org-buffer-property-keys t))
5727 (message "Sorting entries...")
5730 (narrow-to-region start end)
5732 (let ((dcst (downcase sorting-type))
5733 (now (current-time)))
5735 (/= dcst sorting-type)
5736 ;; This function moves to the beginning character of the "record" to
5740 (if (org-at-item-p) t (goto-char (point-max))))
5742 (if (re-search-forward re nil t)
5743 (goto-char (match-beginning 0))
5744 (goto-char (point-max)))))
5745 ;; This function moves to the last character of the "record" being
5752 (outline-forward-same-level 1)
5754 (goto-char (point-max)))))))
5756 ;; This function returns the value that gets sorted against.
5759 (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
5762 (string-to-number (buffer-substring (match-end 0)
5765 (buffer-substring (match-end 0) (point-at-eol)))
5767 (if (re-search-forward org-ts-regexp
5769 (org-time-string-to-time (match-string 0))
5774 (setq tmp (funcall getkey-func))
5775 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
5777 (error "Invalid key function `%s'" getkey-func)))
5778 (t (error "Invalid sorting type `%c'" sorting-type)))))
5782 (if (looking-at org-complex-heading-regexp)
5783 (string-to-number (match-string 4))
5786 (if (looking-at org-complex-heading-regexp)
5787 (funcall case-func (match-string 4))
5790 (if (re-search-forward org-ts-regexp
5794 (org-time-string-to-time (match-string 0))
5797 (if (re-search-forward org-priority-regexp (point-at-eol) t)
5798 (string-to-char (match-string 2))
5799 org-default-priority))
5801 (or (org-entry-get nil property) ""))
5803 (if (looking-at org-complex-heading-regexp)
5804 (- 9999 (length (member (match-string 2)
5805 org-todo-keywords-1)))))
5809 (setq tmp (funcall getkey-func))
5810 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
5812 (error "Invalid key function `%s'" getkey-func)))
5813 (t (error "Invalid sorting type `%c'" sorting-type)))))
5816 ((= dcst ?a) 'string<)
5817 ((= dcst ?t) 'time-less-p)
5818 ((= dcst ?f) compare-func)
5820 (message "Sorting entries...done")))
5822 (defun org-do-sort (table what &optional with-case sorting-type)
5823 "Sort TABLE of WHAT according to SORTING-TYPE.
5824 The user will be prompted for the SORTING-TYPE if the call to this
5825 function does not specify it. WHAT is only for the prompt, to indicate
5826 what is being sorted. The sorting key will be extracted from
5827 the car of the elements of the table.
5828 If WITH-CASE is non-nil, the sorting will be case-sensitive."
5829 (unless sorting-type
5831 "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
5833 (setq sorting-type (read-char-exclusive)))
5834 (let ((dcst (downcase sorting-type))
5835 extractfun comparefun)
5836 ;; Define the appropriate functions
5839 (setq extractfun 'string-to-number
5840 comparefun (if (= dcst sorting-type) '< '>)))
5842 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
5843 (lambda(x) (downcase (org-sort-remove-invisible x))))
5844 comparefun (if (= dcst sorting-type)
5846 (lambda (a b) (and (not (string< a b))
5847 (not (string= a b)))))))
5851 (if (string-match org-ts-regexp x)
5853 (org-time-string-to-time (match-string 0 x)))
5855 comparefun (if (= dcst sorting-type) '< '>)))
5856 (t (error "Invalid sorting type `%c'" sorting-type)))
5858 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
5860 (lambda (a b) (funcall comparefun (car a) (car b))))))
5862 ;;; Editing source examples
5864 (defvar org-exit-edit-mode-map (make-sparse-keymap))
5865 (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
5866 (defvar org-edit-src-force-single-line nil)
5867 (defvar org-edit-src-from-org-mode nil)
5868 (defvar org-edit-src-picture nil)
5870 (define-minor-mode org-exit-edit-mode
5871 "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
5873 (defun org-edit-src-code ()
5874 "Edit the source code example at point.
5875 An indirect buffer is created, and that buffer is then narrowed to the
5876 example at point and switched to the correct language mode. When done,
5877 exit by killing the buffer with \\[org-edit-src-exit]."
5879 (let ((line (org-current-line))
5880 (case-fold-search t)
5881 (msg (substitute-command-keys
5882 "Edit, then exit with C-c ' (C-c and single quote)"))
5883 (info (org-edit-src-find-region-and-lang))
5884 (org-mode-p (eq major-mode 'org-mode))
5885 beg end lang lang-f single lfmt)
5888 (setq beg (nth 0 info)
5893 lang-f (intern (concat lang "-mode")))
5894 (unless (functionp lang-f)
5895 (error "No such language mode: %s" lang-f))
5897 (if (get-buffer "*Org Edit Src Example*")
5898 (kill-buffer "*Org Edit Src Example*"))
5899 (switch-to-buffer (make-indirect-buffer (current-buffer)
5900 "*Org Edit Src Example*"))
5901 (narrow-to-region beg end)
5902 (remove-text-properties beg end '(display nil invisible nil
5904 (let ((org-inhibit-startup t))
5906 (set (make-local-variable 'org-edit-src-force-single-line) single)
5907 (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
5909 (set (make-local-variable 'org-coderef-label-format) lfmt))
5911 (goto-char (point-min))
5912 (while (re-search-forward "^," nil t)
5913 (replace-match "")))
5915 (org-exit-edit-mode)
5916 (org-set-local 'header-line-format msg)
5920 (defun org-edit-fixed-width-region ()
5921 "Edit the fixed-width ascii drawing at point.
5922 This must be a region where each line starts with a colon followed by
5924 An indirect buffer is created, and that buffer is then narrowed to the
5925 example at point and switched to artist-mode. When done,
5926 exit by killing the buffer with \\[org-edit-src-exit]."
5928 (let ((line (org-current-line))
5929 (case-fold-search t)
5930 (msg (substitute-command-keys
5931 "Edit, then exit with C-c ' (C-c and single quote)"))
5932 (org-mode-p (eq major-mode 'org-mode))
5934 (beginning-of-line 1)
5935 (if (looking-at "[ \t]*[^:\n \t]")
5937 (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
5938 (setq beg (point) end beg)
5940 (if (re-search-backward "^[ \t]*[^:]" nil 'move)
5941 (setq beg (point-at-bol 2))
5942 (setq beg (point))))
5944 (if (re-search-forward "^[ \t]*[^:]" nil 'move)
5945 (setq end (1- (match-beginning 0)))
5946 (setq end (point))))
5948 (if (get-buffer "*Org Edit Picture*")
5949 (kill-buffer "*Org Edit Picture*"))
5950 (switch-to-buffer (make-indirect-buffer (current-buffer)
5951 "*Org Edit Picture*"))
5952 (narrow-to-region beg end)
5953 (remove-text-properties beg end '(display nil invisible nil
5955 (when (fboundp 'font-lock-unfontify-region)
5956 (font-lock-unfontify-region (point-min) (point-max)))
5958 ((eq org-edit-fixed-width-region-mode 'artist-mode)
5961 (t (funcall org-edit-fixed-width-region-mode)))
5962 (set (make-local-variable 'org-edit-src-force-single-line) nil)
5963 (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
5964 (set (make-local-variable 'org-edit-src-picture) t)
5965 (goto-char (point-min))
5966 (while (re-search-forward "^[ \t]*: ?" nil t)
5969 (org-exit-edit-mode)
5970 (org-set-local 'header-line-format msg)
5975 (defun org-edit-src-find-region-and-lang ()
5976 "Find the region and language for a local edit.
5977 Return a list with beginning and end of the region, a string representing
5978 the language, a switch telling of the content should be in a single line."
5981 org-edit-src-region-extra
5983 ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
5984 ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
5985 ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
5986 ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
5987 ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
5988 ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
5989 ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
5990 ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
5991 ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
5992 ("^#\\+html:" "\n" "html" single-line)
5993 ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
5994 ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
5995 ("^#\\+latex:" "\n" "latex" single-line)
5996 ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
5997 ("^#\\+ascii:" "\n" "ascii" single-line)
6000 re1 re2 single beg end lang lfmt match-re1)
6002 (while (setq entry (pop re-list))
6003 (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
6004 single (nth 3 entry))
6006 (if (or (looking-at re1)
6007 (re-search-backward re1 nil t))
6009 (setq match-re1 (match-string 0))
6010 (setq beg (match-end 0)
6011 lang (org-edit-src-get-lang lang)
6012 lfmt (org-edit-src-get-label-format match-re1))
6013 (if (and (re-search-forward re2 nil t)
6014 (>= (match-end 0) pos))
6015 (throw 'exit (list beg (match-beginning 0)
6016 lang single lfmt))))
6017 (if (or (looking-at re2)
6018 (re-search-forward re2 nil t))
6020 (setq end (match-beginning 0))
6021 (if (and (re-search-backward re1 nil t)
6022 (<= (match-beginning 0) pos))
6024 (setq lfmt (org-edit-src-get-label-format
6027 (list (match-end 0) end
6028 (org-edit-src-get-lang lang)
6029 single lfmt))))))))))))
6031 (defun org-edit-src-get-lang (lang)
6032 "Extract the src language."
6033 (let ((m (match-string 0)))
6035 ((stringp lang) lang)
6036 ((integerp lang) (match-string lang))
6037 ((and (eq lang 'lang)
6038 (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
6040 ((and (eq lang 'style)
6041 (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
6043 (t "fundamental"))))
6045 (defun org-edit-src-get-label-format (s)
6046 "Extract the label format."
6048 (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
6049 (match-string 1 s))))
6051 (defun org-edit-src-exit ()
6052 "Exit special edit and protect problematic lines."
6054 (unless (buffer-base-buffer (current-buffer))
6055 (error "This is not an indirect buffer, something is wrong..."))
6056 (unless (> (point-min) 1)
6057 (error "This buffer is not narrowed, something is wrong..."))
6058 (goto-char (point-min))
6059 (if (looking-at "[ \t\n]*\n") (replace-match ""))
6060 (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
6061 (when (org-bound-and-true-p org-edit-src-force-single-line)
6062 (goto-char (point-min))
6063 (while (re-search-forward "\n" nil t)
6064 (replace-match " "))
6065 (goto-char (point-min))
6066 (if (looking-at "\\s-*") (replace-match " "))
6067 (if (re-search-forward "\\s-+\\'" nil t)
6068 (replace-match "")))
6069 (when (org-bound-and-true-p org-edit-src-from-org-mode)
6070 (goto-char (point-min))
6071 (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
6072 (replace-match ",\\1"))
6073 (when font-lock-mode
6074 (font-lock-unfontify-region (point-min) (point-max)))
6075 (put-text-property (point-min) (point-max) 'font-lock-fontified t))
6076 (when (org-bound-and-true-p org-edit-src-picture)
6077 (untabify (point-min) (point-max))
6078 (goto-char (point-min))
6079 (while (re-search-forward "^" nil t)
6080 (replace-match ": "))
6081 (when font-lock-mode
6082 (font-lock-unfontify-region (point-min) (point-max)))
6083 (put-text-property (point-min) (point-max) 'font-lock-fontified t))
6084 (kill-buffer (current-buffer))
6085 (and (org-mode-p) (org-restart-font-lock)))
6088 ;;; The orgstruct minor mode
6090 ;; Define a minor mode which can be used in other modes in order to
6091 ;; integrate the org-mode structure editing commands.
6093 ;; This is really a hack, because the org-mode structure commands use
6094 ;; keys which normally belong to the major mode. Here is how it
6095 ;; works: The minor mode defines all the keys necessary to operate the
6096 ;; structure commands, but wraps the commands into a function which
6097 ;; tests if the cursor is currently at a headline or a plain list
6098 ;; item. If that is the case, the structure command is used,
6099 ;; temporarily setting many Org-mode variables like regular
6100 ;; expressions for filling etc. However, when any of those keys is
6101 ;; used at a different location, function uses `key-binding' to look
6102 ;; up if the key has an associated command in another currently active
6103 ;; keymap (minor modes, major mode, global), and executes that
6104 ;; command. There might be problems if any of the keys is otherwise
6105 ;; used as a prefix key.
6107 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
6108 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
6109 ;; addresses this by checking explicitly for both bindings.
6111 (defvar orgstruct-mode-map (make-sparse-keymap)
6112 "Keymap for the minor `orgstruct-mode'.")
6114 (defvar org-local-vars nil
6115 "List of local variables, for use by `orgstruct-mode'")
6118 (define-minor-mode orgstruct-mode
6119 "Toggle the minor more `orgstruct-mode'.
6120 This mode is for using Org-mode structure commands in other modes.
6121 The following key behave as if Org-mode was active, if the cursor
6122 is on a headline, or on a plain list item (both in the definition
6125 M-up Move entry/item up
6126 M-down Move entry/item down
6129 M-S-up Move entry/item up
6130 M-S-down Move entry/item down
6131 M-S-left Promote subtree
6132 M-S-right Demote subtree
6133 M-q Fill paragraph and items like in Org-mode
6135 C-c - Cycle list bullet
6136 TAB Cycle item visibility
6137 M-RET Insert new heading/item
6138 S-M-RET Insert new TODO heading / Checkbox item
6139 C-c C-c Set tags / toggle checkbox"
6140 nil " OrgStruct" nil
6141 (org-load-modules-maybe)
6142 (and (orgstruct-setup) (defun orgstruct-setup () nil)))
6145 (defun turn-on-orgstruct ()
6146 "Unconditionally turn on `orgstruct-mode'."
6150 (defun turn-on-orgstruct++ ()
6151 "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
6152 In addition to setting orgstruct-mode, this also exports all indentation and
6153 autofilling variables from org-mode into the buffer. Note that turning
6154 off orgstruct-mode will *not* remove these additional settings."
6160 "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
6161 (symbol-name (car x)))
6162 (setq var (car x) val (nth 1 x))
6163 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
6166 (defun orgstruct-error ()
6167 "Error when there is no default binding for a structure key."
6169 (error "This key has no function outside structure elements"))
6171 (defun orgstruct-setup ()
6172 "Setup orgstruct keymaps."
6176 '([(meta up)] org-metaup)
6177 '([(meta down)] org-metadown)
6178 '([(meta left)] org-metaleft)
6179 '([(meta right)] org-metaright)
6180 '([(meta shift up)] org-shiftmetaup)
6181 '([(meta shift down)] org-shiftmetadown)
6182 '([(meta shift left)] org-shiftmetaleft)
6183 '([(meta shift right)] org-shiftmetaright)
6184 '([(shift up)] org-shiftup)
6185 '([(shift down)] org-shiftdown)
6186 '([(shift left)] org-shiftleft)
6187 '([(shift right)] org-shiftright)
6188 '("\C-c\C-c" org-ctrl-c-ctrl-c)
6189 '("\M-q" fill-paragraph)
6191 '("\C-c-" org-cycle-list-bullet)))
6193 (while (setq elt (pop bindings))
6194 (setq nfunc (1+ nfunc))
6195 (setq key (org-key (car elt))
6197 cmd (orgstruct-make-binding fun nfunc key))
6198 (org-defkey orgstruct-mode-map key cmd))
6200 ;; Special treatment needed for TAB and RET
6201 (org-defkey orgstruct-mode-map [(tab)]
6202 (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
6203 (org-defkey orgstruct-mode-map "\C-i"
6204 (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
6206 (org-defkey orgstruct-mode-map "\M-\C-m"
6207 (orgstruct-make-binding 'org-insert-heading 105
6208 "\M-\C-m" [(meta return)]))
6209 (org-defkey orgstruct-mode-map [(meta return)]
6210 (orgstruct-make-binding 'org-insert-heading 106
6211 [(meta return)] "\M-\C-m"))
6213 (org-defkey orgstruct-mode-map [(shift meta return)]
6214 (orgstruct-make-binding 'org-insert-todo-heading 107
6215 [(meta return)] "\M-\C-m"))
6217 (unless org-local-vars
6218 (setq org-local-vars (org-get-local-variables)))
6222 (defun orgstruct-make-binding (fun n &rest keys)
6223 "Create a function for binding in the structure minor mode.
6224 FUN is the command to call inside a table. N is used to create a unique
6225 command name. KEYS are keys that should be checked in for a command
6226 to execute outside of tables."
6229 (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
6231 (concat "In Structure, run `" (symbol-name fun) "'.\n"
6232 "Outside of structure, run the binding of `"
6233 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
6237 '(org-context-p 'headline 'item)
6238 (list 'org-run-like-in-org-mode (list 'quote fun))
6239 (list 'let '(orgstruct-mode)
6240 (list 'call-interactively
6243 (list 'key-binding k))
6245 '('orgstruct-error))))))))
6247 (defun org-context-p (&rest contexts)
6248 "Check if local context is any of CONTEXTS.
6249 Possible values in the list of contexts are `table', `headline', and `item'."
6250 (let ((pos (point)))
6251 (goto-char (point-at-bol))
6252 (prog1 (or (and (memq 'table contexts)
6253 (looking-at "[ \t]*|"))
6254 (and (memq 'headline contexts)
6255 ;;????????? (looking-at "\\*+"))
6256 (looking-at outline-regexp))
6257 (and (memq 'item contexts)
6258 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
6261 (defun org-get-local-variables ()
6262 "Return a list of all local variables in an org-mode buffer."
6264 (with-current-buffer (get-buffer-create "*Org tmp*")
6267 (setq varlist (buffer-local-variables)))
6268 (kill-buffer "*Org tmp*")
6275 (list (car x) (list 'quote (cdr x)))))
6277 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
6278 (symbol-name (car x)))
6283 (defun org-run-like-in-org-mode (cmd)
6284 (org-load-modules-maybe)
6285 (unless org-local-vars
6286 (setq org-local-vars (org-get-local-variables)))
6287 (eval (list 'let org-local-vars
6288 (list 'call-interactively (list 'quote cmd)))))
6292 (defun org-get-category (&optional pos)
6293 "Get the category applying to position POS."
6294 (get-text-property (or pos (point)) 'org-category))
6296 (defun org-refresh-category-properties ()
6297 "Refresh category text properties in the buffer."
6298 (let ((def-cat (cond
6299 ((null org-category)
6300 (if buffer-file-name
6301 (file-name-sans-extension
6302 (file-name-nondirectory buffer-file-name))
6304 ((symbolp org-category) (symbol-name org-category))
6306 beg end cat pos optionp)
6311 (goto-char (point-min))
6312 (put-text-property (point) (point-max) 'org-category def-cat)
6313 (while (re-search-forward
6314 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
6315 (setq pos (match-end 0)
6316 optionp (equal (char-after (match-beginning 0)) ?#)
6317 cat (org-trim (match-string 2)))
6319 (setq beg (point-at-bol) end (point-max))
6320 (org-back-to-heading t)
6321 (setq beg (point) end (org-end-of-subtree t t)))
6322 (put-text-property beg end 'org-category cat)
6323 (goto-char pos)))))))
6328 ;;; Link abbreviations
6330 (defun org-link-expand-abbrev (link)
6331 "Apply replacements as defined in `org-link-abbrev-alist."
6332 (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
6333 (let* ((key (match-string 1 link))
6334 (as (or (assoc key org-link-abbrev-alist-local)
6335 (assoc key org-link-abbrev-alist)))
6336 (tag (and (match-end 2) (match-string 3 link)))
6342 ((symbolp rpl) (funcall rpl tag))
6343 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
6344 ((string-match "%h" rpl)
6345 (replace-match (url-hexify-string (or tag "")) t t rpl))
6346 (t (concat rpl tag)))))
6349 ;;; Storing and inserting links
6351 (defvar org-insert-link-history nil
6352 "Minibuffer history for links inserted with `org-insert-link'.")
6354 (defvar org-stored-links nil
6355 "Contains the links stored with `org-store-link'.")
6357 (defvar org-store-link-plist nil
6358 "Plist with info about the most recently link created with `org-store-link'.")
6360 (defvar org-link-protocols nil
6361 "Link protocols added to Org-mode using `org-add-link-type'.")
6363 (defvar org-store-link-functions nil
6364 "List of functions that are called to create and store a link.
6365 Each function will be called in turn until one returns a non-nil
6366 value. Each function should check if it is responsible for creating
6367 this link (for example by looking at the major mode).
6368 If not, it must exit and return nil.
6369 If yes, it should return a non-nil value after a calling
6370 `org-store-link-props' with a list of properties and values.
6371 Special properties are:
6373 :type The link prefix. like \"http\". This must be given.
6374 :link The link, like \"http://www.astro.uva.nl/~dominik\".
6375 This is obligatory as well.
6376 :description Optional default description for the second pair
6377 of brackets in an Org-mode link. The user can still change
6378 this when inserting this link into an Org-mode buffer.
6380 In addition to these, any additional properties can be specified
6381 and then used in remember templates.")
6383 (defun org-add-link-type (type &optional follow export)
6384 "Add TYPE to the list of `org-link-types'.
6385 Re-compute all regular expressions depending on `org-link-types'
6387 FOLLOW and EXPORT are two functions.
6389 FOLLOW should take the link path as the single argument and do whatever
6390 is necessary to follow the link, for example find a file or display
6393 EXPORT should format the link path for export to one of the export formats.
6394 It should be a function accepting three arguments:
6396 path the path of the link, the text after the prefix (like \"http:\")
6397 desc the description of the link, if any, nil if there was no description
6398 format the export format, a symbol like `html' or `latex'.
6400 The function may use the FORMAT information to return different values
6401 depending on the format. The return value will be put literally into
6403 Org-mode has a built-in default for exporting links. If you are happy with
6404 this default, there is no need to define an export function for the link
6405 type. For a simple example of an export function, see `org-bbdb.el'."
6406 (add-to-list 'org-link-types type t)
6407 (org-make-link-regexps)
6408 (if (assoc type org-link-protocols)
6409 (setcdr (assoc type org-link-protocols) (list follow export))
6410 (push (list type follow export) org-link-protocols)))
6413 (defun org-store-link (arg)
6414 "\\<org-mode-map>Store an org-link to the current location.
6415 This link is added to `org-stored-links' and can later be inserted
6416 into an org-buffer with \\[org-insert-link].
6418 For some link types, a prefix arg is interpreted:
6419 For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
6420 For file links, arg negates `org-context-in-file-links'."
6422 (org-load-modules-maybe)
6423 (setq org-store-link-plist nil) ; reset
6424 (let (link cpltxt desc description search txt)
6427 ((run-hook-with-args-until-success 'org-store-link-functions)
6428 (setq link (plist-get org-store-link-plist :link)
6429 desc (or (plist-get org-store-link-plist :description) link)))
6431 ((equal (buffer-name) "*Org Edit Src Example*")
6433 (while (or (not label)
6437 (goto-char (point-min))
6439 (regexp-quote (format org-coderef-label-format label))
6441 (when label (message "Label exists already") (sit-for 2))
6442 (setq label (read-string "Code line label: " label)))
6444 (setq link (format org-coderef-label-format label))
6445 (setq gc (- 79 (length link)))
6446 (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
6448 (setq link (concat "(" label ")") desc nil)))
6450 ((eq major-mode 'calendar-mode)
6451 (let ((cd (calendar-cursor-to-date)))
6454 (car org-time-stamp-formats)
6456 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
6458 (org-store-link-props :type "calendar" :date cd)))
6460 ((eq major-mode 'w3-mode)
6461 (setq cpltxt (url-view-url t)
6462 link (org-make-link cpltxt))
6463 (org-store-link-props :type "w3" :url (url-view-url t)))
6465 ((eq major-mode 'w3m-mode)
6466 (setq cpltxt (or w3m-current-title w3m-current-url)
6467 link (org-make-link w3m-current-url))
6468 (org-store-link-props :type "w3m" :url (url-view-url t)))
6470 ((setq search (run-hook-with-args-until-success
6471 'org-create-file-search-functions))
6472 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
6474 (setq cpltxt (or description link)))
6476 ((eq major-mode 'image-mode)
6477 (setq cpltxt (concat "file:"
6478 (abbreviate-file-name buffer-file-name))
6479 link (org-make-link cpltxt))
6480 (org-store-link-props :type "image" :file buffer-file-name))
6482 ((eq major-mode 'dired-mode)
6483 ;; link to the file in the current line
6484 (setq cpltxt (concat "file:"
6485 (abbreviate-file-name
6487 (dired-get-filename nil t))))
6488 link (org-make-link cpltxt)))
6490 ((and buffer-file-name (org-mode-p))
6492 ((org-in-regexp "<<\\(.*?\\)>>")
6495 (abbreviate-file-name buffer-file-name)
6496 "::" (match-string 1))
6497 link (org-make-link cpltxt)))
6498 ((and (featurep 'org-id)
6499 (or (eq org-link-to-org-use-id t)
6500 (and (eq org-link-to-org-use-id 'create-if-interactive)
6502 (and org-link-to-org-use-id
6504 (org-entry-get nil "ID")
6506 ;; We can make a link using the ID.
6507 (setq link (condition-case nil
6508 (prog1 (org-id-store-link)
6509 (setq desc (plist-get org-store-link-plist
6512 ;; probably before first headline, link to file only
6514 (abbreviate-file-name buffer-file-name))))))
6516 ;; Just link to current headline
6517 (setq cpltxt (concat "file:"
6518 (abbreviate-file-name buffer-file-name)))
6519 ;; Add a context search string
6520 (when (org-xor org-context-in-file-links arg)
6522 ((org-on-heading-p) nil)
6523 ((org-region-active-p)
6524 (buffer-substring (region-beginning) (region-end)))
6526 (when (or (null txt) (string-match "\\S-" txt))
6530 (org-make-org-heading-search-string txt)
6533 (if (string-match "::\\'" cpltxt)
6534 (setq cpltxt (substring cpltxt 0 -2)))
6535 (setq link (org-make-link cpltxt)))))
6537 ((buffer-file-name (buffer-base-buffer))
6538 ;; Just link to this file here.
6539 (setq cpltxt (concat "file:"
6540 (abbreviate-file-name
6541 (buffer-file-name (buffer-base-buffer)))))
6542 ;; Add a context string
6543 (when (org-xor org-context-in-file-links arg)
6544 (setq txt (if (org-region-active-p)
6545 (buffer-substring (region-beginning) (region-end))
6546 (buffer-substring (point-at-bol) (point-at-eol))))
6547 ;; Only use search option if there is some text.
6548 (when (string-match "\\S-" txt)
6550 (concat cpltxt "::" (org-make-org-heading-search-string txt))
6552 (setq link (org-make-link cpltxt)))
6555 (error "Cannot link to a buffer which is not visiting a file"))
6557 (t (setq link nil)))
6559 (if (consp link) (setq cpltxt (car link) link (cdr link)))
6560 (setq link (or link cpltxt)
6561 desc (or desc cpltxt))
6562 (if (equal desc "NONE") (setq desc nil))
6564 (if (and (interactive-p) link)
6566 (setq org-stored-links
6567 (cons (list link desc) org-stored-links))
6568 (message "Stored: %s" (or desc link)))
6569 (and link (org-make-link-string link desc)))))
6571 (defun org-store-link-props (&rest plist)
6572 "Store link properties, extract names and addresses."
6574 (when (setq x (plist-get plist :from))
6575 (setq adr (mail-extract-address-components x))
6576 (setq plist (plist-put plist :fromname (car adr)))
6577 (setq plist (plist-put plist :fromaddress (nth 1 adr))))
6578 (when (setq x (plist-get plist :to))
6579 (setq adr (mail-extract-address-components x))
6580 (setq plist (plist-put plist :toname (car adr)))
6581 (setq plist (plist-put plist :toaddress (nth 1 adr)))))
6582 (let ((from (plist-get plist :from))
6583 (to (plist-get plist :to)))
6584 (when (and from to org-from-is-user-regexp)
6586 (plist-put plist :fromto
6587 (if (string-match org-from-is-user-regexp from)
6589 (concat "from %f"))))))
6590 (setq org-store-link-plist plist))
6592 (defun org-add-link-props (&rest plist)
6593 "Add these properties to the link property list."
6596 (setq key (pop plist) value (pop plist))
6597 (setq org-store-link-plist
6598 (plist-put org-store-link-plist key value)))))
6600 (defun org-email-link-description (&optional fmt)
6601 "Return the description part of an email link.
6602 This takes information from `org-store-link-plist' and formats it
6603 according to FMT (default from `org-email-link-description-format')."
6604 (setq fmt (or fmt org-email-link-description-format))
6605 (let* ((p org-store-link-plist)
6606 (to (plist-get p :toaddress))
6607 (from (plist-get p :fromaddress))
6610 (cons "%c" (plist-get p :fromto))
6611 (cons "%F" (plist-get p :from))
6612 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
6613 (cons "%T" (plist-get p :to))
6614 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
6615 (cons "%s" (plist-get p :subject))
6616 (cons "%m" (plist-get p :message-id)))))
6617 (when (string-match "%c" fmt)
6618 ;; Check if the user wrote this message
6619 (if (and org-from-is-user-regexp from to
6620 (save-match-data (string-match org-from-is-user-regexp from)))
6621 (setq fmt (replace-match "to %t" t t fmt))
6622 (setq fmt (replace-match "from %f" t t fmt))))
6623 (org-replace-escapes fmt table)))
6625 (defun org-make-org-heading-search-string (&optional string heading)
6626 "Make search string for STRING or current headline."
6628 (let ((s (or string (org-get-heading))))
6629 (unless (and string (not heading))
6630 ;; We are using a headline, clean up garbage in there.
6631 (if (string-match org-todo-regexp s)
6632 (setq s (replace-match "" t t s)))
6633 (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
6634 (setq s (replace-match "" t t s)))
6635 (setq s (org-trim s))
6636 (if (string-match (concat "^\\(" org-quote-string "\\|"
6637 org-comment-string "\\)") s)
6638 (setq s (replace-match "" t t s)))
6639 (while (string-match org-ts-regexp s)
6640 (setq s (replace-match "" t t s))))
6641 (while (string-match "[^a-zA-Z_0-9 \t]+" s)
6642 (setq s (replace-match " " t t s)))
6643 (or string (setq s (concat "*" s))) ; Add * for headlines
6644 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
6646 (defun org-make-link (&rest strings)
6647 "Concatenate STRINGS."
6648 (apply 'concat strings))
6650 (defun org-make-link-string (link &optional description)
6651 "Make a link with brackets, consisting of LINK and DESCRIPTION."
6652 (unless (string-match "\\S-" link)
6653 (error "Empty link"))
6654 (when (stringp description)
6655 ;; Remove brackets from the description, they are fatal.
6656 (while (string-match "\\[" description)
6657 (setq description (replace-match "{" t t description)))
6658 (while (string-match "\\]" description)
6659 (setq description (replace-match "}" t t description))))
6660 (when (equal (org-link-escape link) description)
6661 ;; No description needed, it is identical
6662 (setq description nil))
6663 (when (and (not description)
6664 (not (equal link (org-link-escape link))))
6665 (setq description (org-extract-attributes link)))
6666 (concat "[[" (org-link-escape link) "]"
6667 (if description (concat "[" description "]") "")
6670 (defconst org-link-escape-chars
6674 (?\340 . "%E0") ; `a
6675 (?\342 . "%E2") ; ^a
6676 (?\347 . "%E7") ; ,c
6677 (?\350 . "%E8") ; `e
6678 (?\351 . "%E9") ; 'e
6679 (?\352 . "%EA") ; ^e
6680 (?\356 . "%EE") ; ^i
6681 (?\364 . "%F4") ; ^o
6682 (?\371 . "%F9") ; `u
6683 (?\373 . "%FB") ; ^u
6689 "Association list of escapes for some characters problematic in links.
6690 This is the list that is used for internal purposes.")
6692 (defconst org-link-escape-chars-browser
6693 '((?\ . "%20")) ; 32 for the SPC char
6694 "Association list of escapes for some characters problematic in links.
6695 This is the list that is used before handing over to the browser.")
6697 (defun org-link-escape (text &optional table)
6698 "Escape characters in TEXT that are problematic for links."
6699 (setq table (or table org-link-escape-chars))
6701 (let ((re (mapconcat (lambda (x) (regexp-quote
6702 (char-to-string (car x))))
6704 (while (string-match re text)
6707 (cdr (assoc (string-to-char (match-string 0 text))
6712 (defun org-link-unescape (text &optional table)
6713 "Reverse the action of `org-link-escape'."
6714 (setq table (or table org-link-escape-chars))
6716 (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
6718 (while (string-match re text)
6721 (char-to-string (car (rassoc (match-string 0 text) table)))
6725 (defun org-xor (a b)
6729 (defun org-fixup-message-id-for-http (s)
6730 "Replace special characters in a message id, so it can be used in an http query."
6731 (while (string-match "<" s)
6732 (setq s (replace-match "%3C" t t s)))
6733 (while (string-match ">" s)
6734 (setq s (replace-match "%3E" t t s)))
6735 (while (string-match "@" s)
6736 (setq s (replace-match "%40" t t s)))
6740 (defun org-insert-link-global ()
6741 "Insert a link like Org-mode does.
6742 This command can be called in any mode to insert a link in Org-mode syntax."
6744 (org-load-modules-maybe)
6745 (org-run-like-in-org-mode 'org-insert-link))
6747 (defun org-insert-link (&optional complete-file link-location)
6748 "Insert a link. At the prompt, enter the link.
6750 Completion can be used to insert any of the link protocol prefixes like
6753 The history can be used to select a link previously stored with
6754 `org-store-link'. When the empty string is entered (i.e. if you just
6755 press RET at the prompt), the link defaults to the most recently
6756 stored link. As SPC triggers completion in the minibuffer, you need to
6757 use M-SPC or C-q SPC to force the insertion of a space character.
6759 You will also be prompted for a description, and if one is given, it will
6760 be displayed in the buffer instead of the link.
6762 If there is already a link at point, this command will allow you to edit link
6763 and description parts.
6765 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
6766 be selected using completion. The path to the file will be relative to the
6767 current directory if the file is in the current directory or a subdirectory.
6768 Otherwise, the link will be the absolute path as completed in the minibuffer
6769 \(i.e. normally ~/path/to/file). You can configure this behavior using the
6770 option `org-link-file-path-type'.
6772 With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
6773 the current directory or below.
6775 With three \\[universal-argument] prefixes, negate the meaning of
6776 `org-keep-stored-link-after-insertion'.
6778 If `org-make-link-description-function' is non-nil, this function will be
6779 called with the link target, and the result will be the default
6782 If the LINK-LOCATION parameter is non-nil, this value will be
6783 used as the link location instead of reading one interactively."
6785 (let* ((wcf (current-window-configuration))
6786 (region (if (org-region-active-p)
6787 (buffer-substring (region-beginning) (region-end))))
6788 (remove (and region (list (region-beginning) (region-end))))
6790 tmphist ; byte-compile incorrectly complains about this
6791 (link link-location)
6794 (link-location) ; specified by arg, just use it.
6795 ((org-in-regexp org-bracket-link-regexp 1)
6796 ;; We do have a link at point, and we are going to edit it.
6797 (setq remove (list (match-beginning 0) (match-end 0)))
6798 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
6799 (setq link (read-string "Link: "
6801 (org-match-string-no-properties 1)))))
6802 ((or (org-in-regexp org-angle-link-re)
6803 (org-in-regexp org-plain-link-re))
6804 ;; Convert to bracket link
6805 (setq remove (list (match-beginning 0) (match-end 0))
6806 link (read-string "Link: "
6807 (org-remove-angle-brackets (match-string 0)))))
6808 ((member complete-file '((4) (16)))
6809 ;; Completing read for file names.
6810 (setq file (read-file-name "File: "))
6811 (let ((pwd (file-name-as-directory (expand-file-name ".")))
6812 (pwd1 (file-name-as-directory (abbreviate-file-name
6813 (expand-file-name ".")))))
6815 ((equal complete-file '(16))
6816 (setq link (org-make-link
6818 (abbreviate-file-name (expand-file-name file)))))
6819 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
6820 (setq link (org-make-link "file:" (match-string 1 file))))
6821 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
6822 (expand-file-name file))
6823 (setq link (org-make-link
6824 "file:" (match-string 1 (expand-file-name file)))))
6825 (t (setq link (org-make-link "file:" file))))))
6827 ;; Read link, with completion for stored links.
6828 (with-output-to-temp-buffer "*Org Links*"
6829 (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
6830 (when org-stored-links
6831 (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
6834 (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
6835 (reverse org-stored-links) "\n"))))
6836 (let ((cw (selected-window)))
6837 (select-window (get-buffer-window "*Org Links*"))
6838 (setq truncate-lines t)
6839 (org-fit-window-to-buffer)
6841 ;; Fake a link history, containing the stored links.
6842 (setq tmphist (append (mapcar 'car org-stored-links)
6843 org-insert-link-history))
6846 (let ((org-completion-use-ido nil))
6847 (org-completing-read
6850 (mapcar (lambda (x) (list (concat (car x) ":")))
6851 (append org-link-abbrev-alist-local org-link-abbrev-alist))
6852 (mapcar (lambda (x) (list (concat x ":")))
6856 (or (car (car org-stored-links))))))
6857 (set-window-configuration wcf)
6858 (kill-buffer "*Org Links*"))
6859 (setq entry (assoc link org-stored-links))
6860 (or entry (push link org-insert-link-history))
6861 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
6862 (not org-keep-stored-link-after-insertion))
6863 (setq org-stored-links (delq (assoc link org-stored-links)
6865 (setq desc (or desc (nth 1 entry)))))
6867 (if (string-match org-plain-link-re link)
6868 ;; URL-like link, normalize the use of angular brackets.
6869 (setq link (org-make-link (org-remove-angle-brackets link))))
6871 ;; Check if we are linking to the current file with a search option
6872 ;; If yes, simplify the link by using only the search option.
6873 (when (and buffer-file-name
6874 (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
6875 (let* ((path (match-string 1 link))
6876 (case-fold-search nil)
6877 (search (match-string 2 link)))
6879 (if (equal (file-truename buffer-file-name) (file-truename path))
6880 ;; We are linking to this same file, with a search option
6881 (setq link search)))))
6883 ;; Check if we can/should use a relative path. If yes, simplify the link
6884 (when (string-match "^file:\\(.*\\)" link)
6885 (let* ((path (match-string 1 link))
6887 (case-fold-search nil))
6889 ((or (eq org-link-file-path-type 'absolute)
6890 (equal complete-file '(16)))
6891 (setq path (abbreviate-file-name (expand-file-name path))))
6892 ((eq org-link-file-path-type 'noabbrev)
6893 (setq path (expand-file-name path)))
6894 ((eq org-link-file-path-type 'relative)
6895 (setq path (file-relative-name path)))
6898 (if (string-match (concat "^" (regexp-quote
6899 (file-name-as-directory
6900 (expand-file-name "."))))
6901 (expand-file-name path))
6902 ;; We are linking a file with relative path name.
6903 (setq path (substring (expand-file-name path)
6905 (setq path (abbreviate-file-name (expand-file-name path)))))))
6906 (setq link (concat "file:" path))
6907 (if (equal desc origpath)
6910 (if org-make-link-description-function
6911 (setq desc (funcall org-make-link-description-function link desc)))
6913 (setq desc (read-string "Description: " desc))
6914 (unless (string-match "\\S-" desc) (setq desc nil))
6915 (if remove (apply 'delete-region remove))
6916 (insert (org-make-link-string link desc))))
6918 (defun org-completing-read (&rest args)
6919 "Completing-read with SPACE being a normal character."
6920 (let ((minibuffer-local-completion-map
6921 (copy-keymap minibuffer-local-completion-map)))
6922 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
6923 (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
6924 (apply 'org-ido-completing-read args)))
6926 (defun org-ido-completing-read (&rest args)
6927 "Completing-read using `ido-mode' speedups if available"
6928 (if (and org-completion-use-ido
6929 (fboundp 'ido-completing-read)
6930 (boundp 'ido-mode) ido-mode
6931 (listp (second args)))
6932 (apply 'ido-completing-read (concat (car args)) (cdr args))
6933 (apply 'completing-read args)))
6935 (defun org-extract-attributes (s)
6936 "Extract the attributes cookie from a string and set as text property."
6937 (let (a attr (start 0) key value)
6939 (when (string-match "{{\\([^}]+\\)}}$" s)
6940 (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
6941 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
6942 (setq key (match-string 1 a) value (match-string 2 a)
6944 attr (plist-put attr (intern key) value))))
6945 (org-add-props s nil 'org-attr attr))
6948 (defun org-attributes-to-string (plist)
6949 "Format a property list into an HTML attribute list."
6950 (let ((s "") key value)
6952 (setq key (pop plist) value (pop plist))
6954 (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
6957 ;;; Opening/following a link
6959 (defvar org-link-search-failed nil)
6961 (defun org-next-link ()
6962 "Move forward to the next link.
6963 If the link is in hidden text, expose it."
6965 (when (and org-link-search-failed (eq this-command last-command))
6966 (goto-char (point-min))
6967 (message "Link search wrapped back to beginning of buffer"))
6968 (setq org-link-search-failed nil)
6969 (let* ((pos (point))
6971 (a (assoc :link ct)))
6972 (if a (goto-char (nth 2 a)))
6973 (if (re-search-forward org-any-link-re nil t)
6975 (goto-char (match-beginning 0))
6976 (if (org-invisible-p) (org-show-context)))
6978 (setq org-link-search-failed t)
6979 (error "No further link found"))))
6981 (defun org-previous-link ()
6982 "Move backward to the previous link.
6983 If the link is in hidden text, expose it."
6985 (when (and org-link-search-failed (eq this-command last-command))
6986 (goto-char (point-max))
6987 (message "Link search wrapped back to end of buffer"))
6988 (setq org-link-search-failed nil)
6989 (let* ((pos (point))
6991 (a (assoc :link ct)))
6992 (if a (goto-char (nth 1 a)))
6993 (if (re-search-backward org-any-link-re nil t)
6995 (goto-char (match-beginning 0))
6996 (if (org-invisible-p) (org-show-context)))
6998 (setq org-link-search-failed t)
6999 (error "No further link found"))))
7001 (defun org-translate-link (s)
7002 "Translate a link string if a translation function has been defined."
7003 (if (and org-link-translation-function
7004 (fboundp org-link-translation-function)
7005 (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
7007 (setq s (funcall org-link-translation-function
7008 (match-string 1) (match-string 2)))
7009 (concat (car s) ":" (cdr s)))
7012 (defun org-translate-link-from-planner (type path)
7013 "Translate a link from Emacs Planner syntax so that Org can follow it.
7014 This is still an experimental function, your mileage may vary."
7016 ((member type '("http" "https" "news" "ftp"))
7017 ;; standard Internet links are the same.
7019 ((and (equal type "irc") (string-match "^//" path))
7020 ;; Planner has two / at the beginning of an irc link, we have 1.
7021 ;; We should have zero, actually....
7022 (setq path (substring path 1)))
7023 ((and (equal type "lisp") (string-match "^/" path))
7024 ;; Planner has a slash, we do not.
7025 (setq type "elisp" path (substring path 1)))
7026 ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
7027 ;; A typical message link. Planner has the id after the fina slash,
7028 ;; we separate it with a hash mark
7029 (setq path (concat (match-string 1 path) "#"
7030 (org-remove-angle-brackets (match-string 2 path)))))
7034 (defun org-find-file-at-mouse (ev)
7035 "Open file link or URL at mouse."
7037 (mouse-set-point ev)
7038 (org-open-at-point 'in-emacs))
7040 (defun org-open-at-mouse (ev)
7041 "Open file link or URL at mouse."
7043 (mouse-set-point ev)
7044 (if (eq major-mode 'org-agenda-mode)
7045 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
7046 (org-open-at-point))
7048 (defvar org-window-config-before-follow-link nil
7049 "The window configuration before following a link.
7050 This is saved in case the need arises to restore it.")
7052 (defvar org-open-link-marker (make-marker)
7053 "Marker pointing to the location where `org-open-at-point; was called.")
7056 (defun org-open-at-point-global ()
7057 "Follow a link like Org-mode does.
7058 This command can be called in any mode to follow a link that has
7061 (org-run-like-in-org-mode 'org-open-at-point))
7064 (defun org-open-link-from-string (s &optional arg)
7065 "Open a link in the string S, as if it was in Org-mode."
7066 (interactive "sLink: \nP")
7068 (let ((org-inhibit-startup t))
7071 (goto-char (point-min))
7072 (org-open-at-point arg))))
7074 (defun org-open-at-point (&optional in-emacs)
7075 "Open link at or after point.
7076 If there is no link at point, this function will search forward up to
7077 the end of the current subtree.
7078 Normally, files will be opened by an appropriate application. If the
7079 optional argument IN-EMACS is non-nil, Emacs will visit the file.
7080 With a double prefix argument, try to open outside of Emacs, in the
7081 application the system uses for this file type."
7083 (org-load-modules-maybe)
7084 (move-marker org-open-link-marker (point))
7085 (setq org-window-config-before-follow-link (current-window-configuration))
7086 (org-remove-occur-highlights nil nil t)
7088 ((org-at-timestamp-p t) (org-follow-timestamp-link))
7089 ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
7090 (org-footnote-action))
7092 (let (type path link line search (pos (point)))
7095 (skip-chars-forward "^]\n\r")
7096 (when (org-in-regexp org-bracket-link-regexp)
7097 (setq link (org-extract-attributes
7098 (org-link-unescape (org-match-string-no-properties 1))))
7099 (while (string-match " *\n *" link)
7100 (setq link (replace-match " " t t link)))
7101 (setq link (org-link-expand-abbrev link))
7103 ((or (file-name-absolute-p link)
7104 (string-match "^\\.\\.?/" link))
7105 (setq type "file" path link))
7106 ((string-match org-link-re-with-space3 link)
7107 (setq type (match-string 1 link) path (match-string 2 link)))
7108 (t (setq type "thisfile" path link)))
7111 (when (get-text-property (point) 'org-linked-text)
7112 (setq type "thisfile"
7113 pos (if (get-text-property (1+ (point)) 'org-linked-text)
7114 (1+ (point)) (point))
7115 path (buffer-substring
7116 (previous-single-property-change pos 'org-linked-text)
7117 (next-single-property-change pos 'org-linked-text)))
7121 (when (or (org-in-regexp org-angle-link-re)
7122 (org-in-regexp org-plain-link-re))
7123 (setq type (match-string 1) path (match-string 2))
7125 (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
7126 (setq type "tree-match"
7127 path (match-string 1))
7130 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
7132 path (match-string 1))
7133 (while (string-match ":" path)
7134 (setq path (replace-match "+" t t path)))
7137 (error "No link found"))
7138 ;; Remove any trailing spaces in path
7139 (if (string-match " +\\'" path)
7140 (setq path (replace-match "" t t path)))
7141 (if (and org-link-translation-function
7142 (fboundp org-link-translation-function))
7143 ;; Check if we need to translate the link
7144 (let ((tmp (funcall org-link-translation-function type path)))
7145 (setq type (car tmp) path (cdr tmp))))
7149 ((assoc type org-link-protocols)
7150 (funcall (nth 1 (assoc type org-link-protocols)) path))
7152 ((equal type "mailto")
7153 (let ((cmd (car org-link-mailto-program))
7154 (args (cdr org-link-mailto-program)) args1
7155 (address path) (subject "") a)
7156 (if (string-match "\\(.*\\)::\\(.*\\)" path)
7157 (setq address (match-string 1 path)
7158 subject (org-link-escape (match-string 2 path))))
7161 ((not (stringp (car args))) (push (pop args) args1))
7162 (t (setq a (pop args))
7163 (if (string-match "%a" a)
7164 (setq a (replace-match address t t a)))
7165 (if (string-match "%s" a)
7166 (setq a (replace-match subject t t a)))
7168 (apply cmd (nreverse args1))))
7170 ((member type '("http" "https" "ftp" "news"))
7171 (browse-url (concat type ":" (org-link-escape
7172 path org-link-escape-chars-browser))))
7174 ((member type '("message"))
7175 (browse-url (concat type ":" path)))
7177 ((string= type "tags")
7178 (org-tags-view in-emacs path))
7179 ((string= type "thisfile")
7181 (switch-to-buffer-other-window
7182 (org-get-buffer-for-internal-link (current-buffer)))
7183 (org-mark-ring-push))
7184 (let ((cmd `(org-link-search
7186 ,(cond ((equal in-emacs '(4)) 'occur)
7187 ((equal in-emacs '(16)) 'org-occur)
7190 (condition-case nil (eval cmd)
7191 (error (progn (widen) (eval cmd))))))
7193 ((string= type "tree-match")
7194 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
7196 ((string= type "file")
7197 (if (string-match "::\\([0-9]+\\)\\'" path)
7198 (setq line (string-to-number (match-string 1 path))
7199 path (substring path 0 (match-beginning 0)))
7200 (if (string-match "::\\(.+\\)\\'" path)
7201 (setq search (match-string 1 path)
7202 path (substring path 0 (match-beginning 0)))))
7203 (if (string-match "[*?{]" (file-name-nondirectory path))
7205 (org-open-file path in-emacs line search)))
7207 ((string= type "news")
7209 (org-gnus-follow-link path))
7211 ((string= type "shell")
7213 (if (or (not org-confirm-shell-link-function)
7214 (funcall org-confirm-shell-link-function
7215 (format "Execute \"%s\" in shell? "
7216 (org-add-props cmd nil
7217 'face 'org-warning))))
7219 (message "Executing %s" cmd)
7220 (shell-command cmd))
7223 ((string= type "elisp")
7225 (if (or (not org-confirm-elisp-link-function)
7226 (funcall org-confirm-elisp-link-function
7227 (format "Execute \"%s\" as elisp? "
7228 (org-add-props cmd nil
7229 'face 'org-warning))))
7230 (message "%s => %s" cmd
7231 (if (equal (string-to-char cmd) ?\()
7233 (call-interactively (read cmd))))
7237 (browse-url-at-point))))))
7238 (move-marker org-open-link-marker nil)
7239 (run-hook-with-args 'org-follow-link-hook))
7243 (defun org-get-effort (&optional pom)
7244 "Get the effort estimate for the current entry."
7245 (org-entry-get pom org-effort-property))
7249 (defvar org-create-file-search-functions nil
7250 "List of functions to construct the right search string for a file link.
7251 These functions are called in turn with point at the location to
7252 which the link should point.
7254 A function in the hook should first test if it would like to
7255 handle this file type, for example by checking the major-mode or
7256 the file extension. If it decides not to handle this file, it
7257 should just return nil to give other functions a chance. If it
7258 does handle the file, it must return the search string to be used
7259 when following the link. The search string will be part of the
7260 file link, given after a double colon, and `org-open-at-point'
7261 will automatically search for it. If special measures must be
7262 taken to make the search successful, another function should be
7263 added to the companion hook `org-execute-file-search-functions',
7266 A function in this hook may also use `setq' to set the variable
7267 `description' to provide a suggestion for the descriptive text to
7268 be used for this link when it gets inserted into an Org-mode
7269 buffer with \\[org-insert-link].")
7271 (defvar org-execute-file-search-functions nil
7272 "List of functions to execute a file search triggered by a link.
7274 Functions added to this hook must accept a single argument, the
7275 search string that was part of the file link, the part after the
7276 double colon. The function must first check if it would like to
7277 handle this search, for example by checking the major-mode or the
7278 file extension. If it decides not to handle this search, it
7279 should just return nil to give other functions a chance. If it
7280 does handle the search, it must return a non-nil value to keep
7281 other functions from trying.
7283 Each function can access the current prefix argument through the
7284 variable `current-prefix-argument'. Note that a single prefix is
7285 used to force opening a link in Emacs, so it may be good to only
7286 use a numeric or double prefix to guide the search function.
7288 In case this is needed, a function in this hook can also restore
7289 the window configuration before `org-open-at-point' was called using:
7291 (set-window-configuration org-window-config-before-follow-link)")
7293 (defun org-link-search (s &optional type avoid-pos)
7294 "Search for a link search option.
7295 If S is surrounded by forward slashes, it is interpreted as a
7296 regular expression. In org-mode files, this will create an `org-occur'
7297 sparse tree. In ordinary files, `occur' will be used to list matches.
7298 If the current buffer is in `dired-mode', grep will be used to search
7299 in all files. If AVOID-POS is given, ignore matches near that position."
7300 (let ((case-fold-search t)
7301 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
7302 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
7303 (append '(("") (" ") ("\t") ("\n"))
7307 (pre nil) (post nil)
7308 words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
7310 ;; First check if there are any special
7311 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
7312 ;; Now try the builtin stuff
7314 (goto-char (point-min))
7317 (concat "<<" (regexp-quote s0) ">>") nil t)
7318 (setq type 'dedicated
7319 pos (match-beginning 0))))
7320 ;; There is an exact target for this
7322 ((and (string-match "^(\\(.*\\))$" s0)
7324 (goto-char (point-min))
7327 (concat "[^[]" (regexp-quote
7328 (format org-coderef-label-format
7329 (match-string 1 s0))))
7331 (setq type 'dedicated
7332 pos (1+ (match-beginning 0))))))
7333 ;; There is a coderef target for this
7335 ((string-match "^/\\(.*\\)/$" s)
7336 ;; A regular expression
7339 (org-occur (match-string 1 s)))
7340 ;;((eq major-mode 'dired-mode)
7341 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
7342 (t (org-do-occur (match-string 1 s)))))
7344 ;; A normal search strings
7345 (when (equal (string-to-char s) ?*)
7346 ;; Anchor on headlines, post may include tags.
7347 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
7348 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
7350 (remove-text-properties
7352 '(face nil mouse-face nil keymap nil fontified nil) s)
7353 ;; Make a series of regular expressions to find a match
7354 (setq words (org-split-string s "[ \n\r\t]+")
7356 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
7357 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
7359 re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
7360 re2a (concat "[ \t\r\n]" re2a_)
7361 re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
7362 re4 (concat "[^a-zA-Z_]" re4_)
7364 re1 (concat pre re2 post)
7365 re3 (concat pre (if pre re4_ re4) post)
7366 re5 (concat pre ".*" re4)
7367 re2 (concat pre re2)
7368 re2a (concat pre (if pre re2a_ re2a))
7369 re4 (concat pre (if pre re4_ re4))
7370 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
7371 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
7375 ((eq type 'org-occur) (org-occur reall))
7376 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
7377 (t (goto-char (point-min))
7379 (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
7380 (org-search-not-self 1 re1 nil t)
7381 (org-search-not-self 1 re2 nil t)
7382 (org-search-not-self 1 re2a nil t)
7383 (org-search-not-self 1 re3 nil t)
7384 (org-search-not-self 1 re4 nil t)
7385 (org-search-not-self 1 re5 nil t)
7387 (goto-char (match-beginning 1))
7389 (error "No match")))))
7391 ;; Normal string-search
7392 (goto-char (point-min))
7393 (if (search-forward s nil t)
7394 (goto-char (match-beginning 0))
7395 (error "No match"))))
7396 (and (org-mode-p) (org-show-context 'link-search))
7399 (defun org-search-not-self (group &rest args)
7400 "Execute `re-search-forward', but only accept matches that do not
7401 enclose the position of `org-open-link-marker'."
7402 (let ((m org-open-link-marker))
7404 (while (apply 're-search-forward args)
7405 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
7406 (goto-char (match-end group))
7407 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
7408 (> (match-beginning 0) (marker-position m))
7409 (< (match-end 0) (marker-position m)))
7411 (or (not (org-in-regexp
7412 org-bracket-link-analytic-regexp 1))
7413 (not (match-end 4)) ; no description
7414 (and (<= (match-beginning 4) (point))
7415 (>= (match-end 4) (point))))))
7416 (throw 'exit (point))))))))
7418 (defun org-get-buffer-for-internal-link (buffer)
7419 "Return a buffer to be used for displaying the link target of internal links."
7421 ((not org-display-internal-link-with-indirect-buffer)
7423 ((string-match "(Clone)$" (buffer-name buffer))
7424 (message "Buffer is already a clone, not making another one")
7425 ;; we also do not modify visibility in this case
7427 (t ; make a new indirect buffer for displaying the link
7428 (let* ((bn (buffer-name buffer))
7429 (ibn (concat bn "(Clone)"))
7430 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
7431 (with-current-buffer ib (org-overview))
7434 (defun org-do-occur (regexp &optional cleanup)
7435 "Call the Emacs command `occur'.
7436 If CLEANUP is non-nil, remove the printout of the regular expression
7437 in the *Occur* buffer. This is useful if the regex is long and not useful
7441 (let ((cwin (selected-window)) win beg end)
7442 (when (setq win (get-buffer-window "*Occur*"))
7443 (select-window win))
7444 (goto-char (point-min))
7445 (when (re-search-forward "match[a-z]+" nil t)
7446 (setq beg (match-end 0))
7447 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
7448 (setq end (1- (match-beginning 0)))))
7449 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
7450 (goto-char (point-min))
7451 (select-window cwin))))
7453 ;;; The mark ring for links jumps
7455 (defvar org-mark-ring nil
7456 "Mark ring for positions before jumps in Org-mode.")
7457 (defvar org-mark-ring-last-goto nil
7458 "Last position in the mark ring used to go back.")
7459 ;; Fill and close the ring
7460 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
7461 (loop for i from 1 to org-mark-ring-length do
7462 (push (make-marker) org-mark-ring))
7463 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
7466 (defun org-mark-ring-push (&optional pos buffer)
7467 "Put the current position or POS into the mark ring and rotate it."
7469 (setq pos (or pos (point)))
7470 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
7471 (move-marker (car org-mark-ring)
7473 (or buffer (current-buffer)))
7475 (substitute-command-keys
7476 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
7478 (defun org-mark-ring-goto (&optional n)
7479 "Jump to the previous position in the mark ring.
7480 With prefix arg N, jump back that many stored positions. When
7481 called several times in succession, walk through the entire ring.
7482 Org-mode commands jumping to a different position in the current file,
7483 or to another Org-mode file, automatically push the old position
7487 (if (eq last-command this-command)
7488 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
7489 (setq p org-mark-ring))
7490 (setq org-mark-ring-last-goto p)
7492 (switch-to-buffer (marker-buffer m))
7494 (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
7496 (defun org-remove-angle-brackets (s)
7497 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
7498 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
7500 (defun org-add-angle-brackets (s)
7501 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
7502 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
7504 (defun org-remove-double-quotes (s)
7505 (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
7506 (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
7509 ;;; Following specific links
7511 (defun org-follow-timestamp-link ()
7513 ((org-at-date-range-p t)
7514 (let ((org-agenda-start-on-weekday)
7515 (t1 (match-string 1))
7516 (t2 (match-string 2)))
7517 (setq t1 (time-to-days (org-time-string-to-time t1))
7518 t2 (time-to-days (org-time-string-to-time t2)))
7519 (org-agenda-list nil t1 (1+ (- t2 t1)))))
7520 ((org-at-timestamp-p t)
7521 (org-agenda-list nil (time-to-days (org-time-string-to-time
7522 (substring (match-string 1) 0 10)))
7524 (t (error "This should not happen"))))
7527 ;;; Following file links
7528 (defvar org-wait nil)
7529 (defun org-open-file (path &optional in-emacs line search)
7530 "Open the file at PATH.
7531 First, this expands any special file name abbreviations. Then the
7532 configuration variable `org-file-apps' is checked if it contains an
7533 entry for this file type, and if yes, the corresponding command is launched.
7535 If no application is found, Emacs simply visits the file.
7537 With optional prefix argument IN-EMACS, Emacs will visit the file.
7538 With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
7539 and o use an external application to visit the file.
7541 Optional LINE specifies a line to go to, optional SEARCH a string to
7542 search for. If LINE or SEARCH is given, the file will always be
7544 If the file does not exist, an error is thrown."
7545 (setq in-emacs (or in-emacs line search))
7546 (let* ((file (if (equal path "")
7548 (substitute-in-file-name (expand-file-name path))))
7549 (apps (append org-file-apps (org-default-apps)))
7550 (remp (and (assq 'remote apps) (org-file-remote-p file)))
7551 (dirp (if remp nil (file-directory-p file)))
7552 (file (if (and dirp org-open-directory-means-index-dot-org)
7553 (concat (file-name-as-directory file) "index.org")
7555 (a-m-a-p (assq 'auto-mode apps))
7556 (dfile (downcase file))
7557 (old-buffer (current-buffer))
7559 (old-mode major-mode)
7561 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
7562 (setq ext (match-string 1 dfile))
7563 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
7564 (setq ext (match-string 1 dfile))))
7566 ((equal in-emacs '(16))
7567 (setq cmd (cdr (assoc 'system apps))))
7568 (in-emacs (setq cmd 'emacs))
7570 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
7571 (and dirp (cdr (assoc 'directory apps)))
7572 (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
7574 (cdr (assoc ext apps))
7575 (cdr (assoc t apps))))))
7576 (when (eq cmd 'system)
7577 (setq cmd (cdr (assoc 'system apps))))
7578 (when (eq cmd 'default)
7579 (setq cmd (cdr (assoc t apps))))
7580 (when (eq cmd 'mailcap)
7582 (mailcap-parse-mailcaps)
7583 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
7584 (command (mailcap-mime-info mime-type)))
7585 (if (stringp command)
7587 (setq cmd 'emacs))))
7588 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
7589 (not (file-exists-p file))
7590 (not org-open-non-existing-files))
7591 (error "No such file: %s" file))
7593 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
7594 ;; Remove quotes around the file name - we'll use shell-quote-argument.
7595 (while (string-match "['\"]%s['\"]" cmd)
7596 (setq cmd (replace-match "%s" t t cmd)))
7597 (while (string-match "%s" cmd)
7598 (setq cmd (replace-match
7600 (shell-quote-argument
7601 (convert-standard-filename file)))
7603 (save-window-excursion
7604 (start-process-shell-command cmd nil cmd)
7605 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
7609 (funcall (cdr (assq 'file org-link-frame-setup)) file)
7611 (if line (goto-line line)
7612 (if search (org-link-search search))))
7614 (let ((file (convert-standard-filename file)))
7616 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
7617 (and (org-mode-p) (eq old-mode 'org-mode)
7618 (or (not (equal old-buffer (current-buffer)))
7619 (not (equal old-pos (point))))
7620 (org-mark-ring-push old-pos old-buffer))))
7622 (defun org-default-apps ()
7623 "Return the default applications for this operating system."
7625 ((eq system-type 'darwin)
7626 org-file-apps-defaults-macosx)
7627 ((eq system-type 'windows-nt)
7628 org-file-apps-defaults-windowsnt)
7629 (t org-file-apps-defaults-gnu)))
7631 (defun org-apps-regexp-alist (list &optional add-auto-mode)
7632 "Convert extensions to regular expressions in the cars of LIST.
7633 Also, weed out any non-string entries, because the return value is used
7634 only for regexp matching.
7635 When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
7636 point to the symbol `emacs', indicating that the file should
7637 be opened in Emacs."
7641 (if (not (stringp (car x)))
7643 (if (string-match "\\W" (car x))
7645 (cons (concat "\\." (car x) "\\'") (cdr x)))))
7648 (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
7650 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
7651 (defun org-file-remote-p (file)
7652 "Test whether FILE specifies a location on a remote system.
7653 Return non-nil if the location is indeed remote.
7655 For example, the filename \"/user@host:/foo\" specifies a location
7656 on the system \"/user@host:\"."
7657 (cond ((fboundp 'file-remote-p)
7658 (file-remote-p file))
7659 ((fboundp 'tramp-handle-file-remote-p)
7660 (tramp-handle-file-remote-p file))
7661 ((and (boundp 'ange-ftp-name-format)
7662 (string-match (car ange-ftp-name-format) file))
7669 (defun org-get-org-file ()
7670 "Read a filename, with default directory `org-directory'."
7671 (let ((default (or org-default-notes-file remember-data-file)))
7672 (read-file-name (format "File name [%s]: " default)
7673 (file-name-as-directory org-directory)
7676 (defun org-notes-order-reversed-p ()
7677 "Check if the current file should receive notes in reversed order."
7679 ((not org-reverse-note-order) nil)
7680 ((eq t org-reverse-note-order) t)
7681 ((not (listp org-reverse-note-order)) nil)
7683 (let ((all org-reverse-note-order)
7685 (while (setq entry (pop all))
7686 (if (string-match (car entry) buffer-file-name)
7687 (throw 'exit (cdr entry))))
7690 (defvar org-refile-target-table nil
7691 "The list of refile targets, created by `org-refile'.")
7693 (defvar org-agenda-new-buffers nil
7694 "Buffers created to visit agenda files.")
7696 (defun org-get-refile-targets (&optional default-buffer)
7697 "Produce a table with refile targets."
7698 (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
7699 targets txt re files f desc descre fast-path-p level)
7700 (message "Getting targets...")
7701 (with-current-buffer (or default-buffer (current-buffer))
7702 (while (setq entry (pop entries))
7703 (setq files (car entry) desc (cdr entry))
7704 (setq fast-path-p nil)
7706 ((null files) (setq files (list (current-buffer))))
7707 ((eq files 'org-agenda-files)
7708 (setq files (org-agenda-files 'unrestricted)))
7709 ((and (symbolp files) (fboundp files))
7710 (setq files (funcall files)))
7711 ((and (symbolp files) (boundp files))
7712 (setq files (symbol-value files))))
7713 (if (stringp files) (setq files (list files)))
7715 ((eq (car desc) :tag)
7716 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
7717 ((eq (car desc) :todo)
7718 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
7719 ((eq (car desc) :regexp)
7720 (setq descre (cdr desc)))
7721 ((eq (car desc) :level)
7722 (setq descre (concat "^\\*\\{" (number-to-string
7723 (if org-odd-levels-only
7724 (1- (* 2 (cdr desc)))
7727 ((eq (car desc) :maxlevel)
7728 (setq fast-path-p t)
7729 (setq descre (concat "^\\*\\{1," (number-to-string
7730 (if org-odd-levels-only
7731 (1- (* 2 (cdr desc)))
7734 (t (error "Bad refiling target description %s" desc)))
7735 (while (setq f (pop files))
7737 (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
7738 (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
7739 (setq f (expand-file-name f))
7743 (goto-char (point-min))
7744 (while (re-search-forward descre nil t)
7745 (goto-char (point-at-bol))
7746 (when (looking-at org-complex-heading-regexp)
7747 (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
7748 txt (org-link-display-format (match-string 4))
7749 re (concat "^" (regexp-quote
7750 (buffer-substring (match-beginning 1)
7752 (if (match-end 5) (setq re (concat re "[ \t]+"
7754 (match-string 5)))))
7755 (setq re (concat re "[ \t]*$"))
7756 (when org-refile-use-outline-path
7757 (setq txt (mapconcat 'org-protect-slash
7759 (if (eq org-refile-use-outline-path 'file)
7760 (list (file-name-nondirectory
7761 (buffer-file-name (buffer-base-buffer))))
7762 (if (eq org-refile-use-outline-path 'full-file-path)
7763 (list (buffer-file-name (buffer-base-buffer)))))
7764 (org-get-outline-path fast-path-p level txt)
7767 (push (list txt f re (point)) targets))
7768 (goto-char (point-at-eol))))))))
7769 (message "Getting targets...done")
7770 (nreverse targets))))
7772 (defun org-protect-slash (s)
7773 (while (string-match "/" s)
7774 (setq s (replace-match "\\" t t s)))
7777 (defvar org-olpa (make-vector 20 nil))
7779 (defun org-get-outline-path (&optional fastp level heading)
7780 "Return the outline path to the current entry, as a list."
7784 (error "Outline path failure, more than 19 levels."))
7785 (loop for i from level upto 19 do
7786 (aset org-olpa i nil))
7788 (delq nil (append org-olpa nil))
7789 (aset org-olpa level heading)))
7792 (while (org-up-heading-safe)
7793 (when (looking-at org-complex-heading-regexp)
7794 (push (org-match-string-no-properties 4) rtn)))
7797 (defvar org-refile-history nil
7798 "History for refiling operations.")
7800 (defun org-refile (&optional goto default-buffer)
7801 "Move the entry at point to another heading.
7802 The list of target headings is compiled using the information in
7803 `org-refile-targets', which see. This list is created before each use
7804 and will therefore always be up-to-date.
7806 At the target location, the entry is filed as a subitem of the target heading.
7807 Depending on `org-reverse-note-order', the new subitem will either be the
7808 first or the last subitem.
7810 If there is an active region, all entries in that region will be moved.
7811 However, the region must fulfil the requirement that the first heading
7812 is the first one sets the top-level of the moved text - at most siblings
7813 below it are allowed.
7815 With prefix arg GOTO, the command will only visit the target location,
7816 not actually move anything.
7817 With a double prefix `C-u C-u', go to the location where the last refiling
7818 operation has put the subtree."
7820 (let* ((cbuf (current-buffer))
7821 (regionp (org-region-active-p))
7822 (region-start (and regionp (region-beginning)))
7823 (region-end (and regionp (region-end)))
7824 (region-length (and regionp (- region-end region-start)))
7825 (filename (buffer-file-name (buffer-base-buffer cbuf)))
7826 pos it nbuf file re level reversed)
7827 (when regionp (goto-char region-start)
7828 (unless (org-kill-is-subtree-p
7829 (buffer-substring region-start region-end))
7830 (error "The region is not a (sequence of) subtree(s)")))
7831 (if (equal goto '(16))
7832 (org-refile-goto-last-stored)
7833 (when (setq it (org-refile-get-location
7834 (if goto "Goto: " "Refile to: ") default-buffer))
7835 (setq file (nth 1 it)
7838 (if (and (equal (buffer-file-name) file)
7840 (and (>= pos region-start)
7841 (<= pos region-end))
7842 (and (>= pos (point))
7843 (< pos (save-excursion
7844 (org-end-of-subtree t t))))))
7845 (error "Cannot refile to position inside the tree or region"))
7847 (setq nbuf (or (find-buffer-visiting file)
7848 (find-file-noselect file)))
7851 (switch-to-buffer nbuf)
7853 (org-show-context 'org-goto))
7856 (kill-new (buffer-substring region-start region-end))
7857 (org-save-markers-in-region region-start region-end))
7858 (org-copy-subtree 1 nil t))
7860 (set-buffer (setq nbuf (or (find-buffer-visiting file)
7861 (find-file-noselect file))))
7862 (setq reversed (org-notes-order-reversed-p))
7867 (looking-at outline-regexp)
7868 (setq level (org-get-valid-level (funcall outline-level) 1))
7871 (or (outline-next-heading) (point-max))
7872 (or (save-excursion (outline-get-next-sibling))
7873 (org-end-of-subtree t t)
7875 (if (not (bolp)) (newline))
7876 (bookmark-set "org-refile-last-stored")
7877 (org-paste-subtree level))))
7879 (delete-region (point) (+ (point) region-length))
7881 (setq org-markers-to-move nil)
7882 (message "Refiled to \"%s\"" (car it)))))))
7884 (defun org-refile-goto-last-stored ()
7885 "Go to the location where the last refile was stored."
7887 (bookmark-jump "org-refile-last-stored")
7888 (message "This is the location of the last refile"))
7890 (defun org-refile-get-location (&optional prompt default-buffer)
7891 "Prompt the user for a refile location, using PROMPT."
7892 (let ((org-refile-targets org-refile-targets)
7893 (org-refile-use-outline-path org-refile-use-outline-path))
7894 (setq org-refile-target-table (org-get-refile-targets default-buffer)))
7895 (unless org-refile-target-table
7896 (error "No refile targets"))
7897 (let* ((cbuf (current-buffer))
7898 (cfn (buffer-file-name (buffer-base-buffer cbuf)))
7899 (cfunc (if (and org-refile-use-outline-path
7900 org-outline-path-complete-in-steps)
7901 'org-olpath-completing-read
7902 'org-ido-completing-read))
7903 (extra (if org-refile-use-outline-path "/" ""))
7904 (filename (and cfn (expand-file-name cfn)))
7907 (if (not (equal filename (nth 1 x)))
7908 (cons (concat (car x) extra " ("
7909 (file-name-nondirectory (nth 1 x)) ")")
7911 (cons (concat (car x) extra) (cdr x))))
7912 org-refile-target-table))
7913 (completion-ignore-case t))
7914 (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
7917 (defun org-olpath-completing-read (prompt collection &rest args)
7918 "Read an outline path like a file name."
7919 (let ((thetable collection))
7921 'org-ido-completing-read prompt
7922 (lambda (string predicate &optional flag)
7923 (let (rtn r f (l (length string)))
7927 (try-completion string thetable))
7930 (setq rtn (all-completions string thetable predicate))
7933 (setq r (substring x l))
7934 (if (string-match " ([^)]*)$" x)
7935 (setq f (match-string 0 x))
7937 (if (string-match "/" r)
7938 (concat string (substring r 0 (match-end 0)) f)
7943 (assoc string thetable)))
7949 (defun org-find-dblock (name)
7950 "Find the first dynamic block with name NAME in the buffer.
7951 If not found, stay at current position and return nil."
7954 (goto-char (point-min))
7955 (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
7957 (match-beginning 0))))
7958 (if pos (goto-char pos))
7961 (defconst org-dblock-start-re
7962 "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
7963 "Matches the startline of a dynamic block, with parameters.")
7965 (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
7966 "Matches the end of a dynamic block.")
7968 (defun org-create-dblock (plist)
7969 "Create a dynamic block section, with parameters taken from PLIST.
7970 PLIST must contain a :name entry which is used as name of the block."
7971 (unless (bolp) (newline))
7972 (let ((name (plist-get plist :name)))
7973 (insert "#+BEGIN: " name)
7975 (if (eq (car plist) :name)
7976 (setq plist (cddr plist))
7977 (insert " " (prin1-to-string (pop plist)))))
7978 (insert "\n\n#+END:\n")
7979 (beginning-of-line -2)))
7981 (defun org-prepare-dblock ()
7982 "Prepare dynamic block for refresh.
7983 This empties the block, puts the cursor at the insert position and returns
7984 the property list including an extra property :name with the block name."
7985 (unless (looking-at org-dblock-start-re)
7986 (error "Not at a dynamic block"))
7987 (let* ((begdel (1+ (match-end 0)))
7988 (name (org-no-properties (match-string 1)))
7989 (params (append (list :name name)
7990 (read (concat "(" (match-string 3) ")")))))
7991 (unless (re-search-forward org-dblock-end-re nil t)
7992 (error "Dynamic block not terminated"))
7995 (list :content (buffer-substring
7996 begdel (match-beginning 0)))))
7997 (delete-region begdel (match-beginning 0))
8002 (defun org-map-dblocks (&optional command)
8003 "Apply COMMAND to all dynamic blocks in the current buffer.
8004 If COMMAND is not given, use `org-update-dblock'."
8005 (let ((cmd (or command 'org-update-dblock))
8008 (goto-char (point-min))
8009 (while (re-search-forward org-dblock-start-re nil t)
8010 (goto-char (setq pos (match-beginning 0)))
8013 (error (message "Error during update of dynamic block")))
8015 (unless (re-search-forward org-dblock-end-re nil t)
8016 (error "Dynamic block not terminated"))))))
8018 (defun org-dblock-update (&optional arg)
8019 "User command for updating dynamic blocks.
8020 Update the dynamic block at point. With prefix ARG, update all dynamic
8021 blocks in the buffer."
8024 (org-update-all-dblocks)
8025 (or (looking-at org-dblock-start-re)
8026 (org-beginning-of-dblock))
8027 (org-update-dblock)))
8029 (defun org-update-dblock ()
8030 "Update the dynamic block at point
8031 This means to empty the block, parse for parameters and then call
8032 the correct writing function."
8033 (save-window-excursion
8034 (let* ((pos (point))
8035 (line (org-current-line))
8036 (params (org-prepare-dblock))
8037 (name (plist-get params :name))
8038 (cmd (intern (concat "org-dblock-write:" name))))
8039 (message "Updating dynamic block `%s' at line %d..." name line)
8040 (funcall cmd params)
8041 (message "Updating dynamic block `%s' at line %d...done" name line)
8044 (defun org-beginning-of-dblock ()
8045 "Find the beginning of the dynamic block at point.
8046 Error if there is no such block at point."
8050 (if (and (re-search-backward org-dblock-start-re nil t)
8051 (setq beg (match-beginning 0))
8052 (re-search-forward org-dblock-end-re nil t)
8053 (> (match-end 0) pos))
8056 (error "Not in a dynamic block"))))
8058 (defun org-update-all-dblocks ()
8059 "Update all dynamic blocks in the buffer.
8060 This function can be used in a hook."
8062 (org-map-dblocks 'org-update-dblock)))
8067 (defconst org-additional-option-like-keywords
8068 '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
8069 "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
8070 "BEGIN_EXAMPLE" "END_EXAMPLE"
8071 "BEGIN_QUOTE" "END_QUOTE"
8072 "BEGIN_VERSE" "END_VERSE"
8073 "BEGIN_SRC" "END_SRC"
8074 "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
8076 (defcustom org-structure-template-alist
8078 ("s" "#+begin_src ?\n\n#+end_src"
8079 "<src lang=\"?\">\n\n</src>")
8080 ("e" "#+begin_example\n?\n#+end_example"
8081 "<example>\n?\n</example>")
8082 ("q" "#+begin_quote\n?\n#+end_quote"
8083 "<quote>\n?\n</quote>")
8084 ("v" "#+begin_verse\n?\n#+end_verse"
8085 "<verse>\n?\n/verse>")
8086 ("l" "#+begin_latex\n?\n#+end_latex"
8087 "<literal style=\"latex\">\n?\n</literal>")
8089 "<literal style=\"latex\">?</literal>")
8090 ("h" "#+begin_html\n?\n#+end_html"
8091 "<literal style=\"html\">\n?\n</literal>")
8093 "<literal style=\"html\">?</literal>")
8094 ("a" "#+begin_ascii\n?\n#+end_ascii")
8096 ("i" "#+include %file ?"
8097 "<include file=%file markup=\"?\">")
8099 "Structure completion elements.
8100 This is a list of abbreviation keys and values. The value gets inserted
8101 it you type @samp{.} followed by the key and then the completion key,
8102 usually `M-TAB'. %file will be replaced by a file name after prompting
8103 for the file using completion.
8104 There are two templates for each key, the first uses the original Org syntax,
8105 the second uses Emacs Muse-like syntax tags. These Muse-like tags become
8106 the default when the /org-mtags.el/ module has been loaded. See also the
8107 variable `org-mtags-prefer-muse-templates'.
8108 This is an experimental feature, it is undecided if it is going to stay in."
8109 :group 'org-completion
8112 (string :tag "Template")
8113 (string :tag "Muse Template")))
8115 (defun org-try-structure-completion ()
8116 "Try to complete a structure template before point.
8117 This looks for strings like \"<e\" on an otherwise empty line and
8119 (let ((l (buffer-substring (point-at-bol) (point)))
8121 (when (and (looking-at "[ \t]*$")
8122 (string-match "^[ \t]*<\\([a-z]+\\)$"l)
8123 (setq a (assoc (match-string 1 l) org-structure-template-alist)))
8124 (org-complete-expand-structure-template (+ -1 (point-at-bol)
8125 (match-beginning 1)) a)
8128 (defun org-complete-expand-structure-template (start cell)
8129 "Expand a structure template."
8130 (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
8131 (rpl (nth (if musep 2 1) cell)))
8132 (delete-region start (point))
8133 (when (string-match "\\`#\\+" rpl)
8136 ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
8137 (delete-region (point-at-bol) (point)))
8139 (setq start (point))
8140 (if (string-match "%file" rpl)
8141 (setq rpl (replace-match
8145 (abbreviate-file-name (read-file-name "Include file: ")))
8149 (if (re-search-backward "\\?" start t) (delete-char 1))))
8152 (defun org-complete (&optional arg)
8153 "Perform completion on word at point.
8154 At the beginning of a headline, this completes TODO keywords as given in
8155 `org-todo-keywords'.
8156 If the current word is preceded by a backslash, completes the TeX symbols
8157 that are supported for HTML support.
8158 If the current word is preceded by \"#+\", completes special words for
8159 setting file options.
8160 In the line after \"#+STARTUP:, complete valid keywords.\"
8161 At all other locations, this simply calls the value of
8162 `org-completion-fallback-command'."
8164 (org-without-partial-completion
8168 (beg1 (save-excursion
8169 (skip-chars-backward (org-re "[:alnum:]_@"))
8171 (beg (save-excursion
8172 (skip-chars-backward "a-zA-Z0-9_:$")
8174 (confirm (lambda (x) (stringp (car x))))
8175 (searchhead (equal (char-before beg) ?*))
8177 (when (and (member (char-before beg1) '(?. ?<))
8178 (setq a (assoc (buffer-substring beg1 (point))
8179 org-structure-template-alist)))
8180 (org-complete-expand-structure-template (1- beg1) a)
8182 (tag (and (equal (char-before beg1) ?:)
8183 (equal (char-after (point-at-bol)) ?*)))
8184 (prop (and (equal (char-before beg1) ?:)
8185 (not (equal (char-after (point-at-bol)) ?*))))
8186 (texp (equal (char-before beg) ?\\))
8187 (link (equal (char-before beg) ?\[))
8188 (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
8191 (startup (string-match "^#\\+STARTUP:.*"
8192 (buffer-substring (point-at-bol) (point))))
8193 (completion-ignore-case opt)
8203 (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
8204 (cons (match-string 2 x) (match-string 1 x)))
8205 (org-split-string (org-get-current-options) "\n"))
8206 (mapcar 'list org-additional-option-like-keywords)))
8208 (setq type :startup)
8209 org-startup-options)
8210 (link (append org-link-abbrev-alist-local
8211 org-link-abbrev-alist))
8215 ((string-match "\\`\\*+[ \t]+\\'"
8216 (buffer-substring (point-at-bol) beg))
8218 (mapcar 'list org-todo-keywords-1))
8220 (setq type :searchhead)
8222 (goto-char (point-min))
8223 (while (re-search-forward org-todo-line-regexp nil t)
8225 (org-make-org-heading-search-string
8226 (match-string 3) t))
8229 (tag (setq type :tag beg beg1)
8230 (or org-tag-alist (org-get-buffer-tags)))
8231 (prop (setq type :prop beg beg1)
8232 (mapcar 'list (org-buffer-property-keys nil t t)))
8234 (call-interactively org-completion-fallback-command)
8235 (throw 'exit nil)))))
8236 (pattern (buffer-substring-no-properties beg end))
8237 (completion (try-completion pattern table confirm)))
8238 (cond ((eq completion t)
8239 (if (not (assoc (upcase pattern) table))
8240 (message "Already complete")
8241 (if (and (equal type :opt)
8242 (not (member (car (assoc (upcase pattern) table))
8243 org-additional-option-like-keywords)))
8244 (insert (substring (cdr (assoc (upcase pattern) table))
8246 (if (memq type '(:tag :prop)) (insert ":")))))
8248 (message "Can't find completion for \"%s\"" pattern)
8250 ((not (string= pattern completion))
8251 (delete-region beg end)
8252 (if (string-match " +$" completion)
8253 (setq completion (replace-match "" t t completion)))
8255 (if (get-buffer-window "*Completions*")
8256 (delete-window (get-buffer-window "*Completions*")))
8257 (if (assoc completion table)
8258 (if (eq type :todo) (insert " ")
8259 (if (memq type '(:tag :prop)) (insert ":"))))
8260 (if (and (equal type :opt) (assoc completion table))
8261 (message "%s" (substitute-command-keys
8262 "Press \\[org-complete] again to insert example settings"))))
8264 (message "Making completion list...")
8265 (let ((list (sort (all-completions pattern table confirm)
8267 (with-output-to-temp-buffer "*Completions*"
8269 ;; Protection needed for XEmacs and emacs 21
8270 (display-completion-list list pattern)
8271 (error (display-completion-list list)))))
8272 (message "Making completion list...%s" "done")))))))
8274 ;;;; TODO, DEADLINE, Comments
8276 (defun org-toggle-comment ()
8277 "Change the COMMENT state of an entry."
8280 (org-back-to-heading)
8281 (let (case-fold-search)
8282 (if (looking-at (concat outline-regexp
8283 "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
8284 (replace-match "" t t nil 1)
8285 (if (looking-at outline-regexp)
8287 (goto-char (match-end 0))
8288 (insert org-comment-string " ")))))))
8290 (defvar org-last-todo-state-is-todo nil
8291 "This is non-nil when the last TODO state change led to a TODO state.
8292 If the last change removed the TODO tag or switched to DONE, then
8295 (defvar org-setting-tags nil) ; dynamically skipped
8297 (defun org-parse-local-options (string var)
8298 "Parse STRING for startup setting relevant for variable VAR."
8299 (let ((rtn (symbol-value var))
8302 (if (or (not string) (not (string-match "\\S-" string)))
8304 (setq opts (delq nil (mapcar (lambda (x)
8305 (setq e (assoc x org-startup-options))
8306 (if (eq (nth 1 e) var) e nil))
8307 (org-split-string string "[ \t]+"))))
8311 (while (setq e (pop opts))
8313 (setq rtn (nth 2 e))
8314 (if (not (listp rtn)) (setq rtn nil))
8315 (push (nth 2 e) rtn)))
8318 (defvar org-agenda-headline-snapshot-before-repeat)
8319 (defun org-todo (&optional arg)
8320 "Change the TODO state of an item.
8321 The state of an item is given by a keyword at the start of the heading,
8323 *** TODO Write paper
8326 The different keywords are specified in the variable `org-todo-keywords'.
8327 By default the available states are \"TODO\" and \"DONE\".
8328 So for this example: when the item starts with TODO, it is changed to DONE.
8329 When it starts with DONE, the DONE is removed. And when neither TODO nor
8330 DONE are present, add TODO at the beginning of the heading.
8332 With C-u prefix arg, use completion to determine the new state.
8333 With numeric prefix arg, switch to that state.
8334 With a double C-u prefix, switch to the next set of TODO keywords (nextset).
8336 For calling through lisp, arg is also interpreted in the following way:
8337 'none -> empty state
8338 \"\"(empty string) -> switch to empty state
8339 'done -> switch to DONE
8340 'nextset -> switch to the next set of keywords
8341 'previousset -> switch to the previous set of keywords
8342 \"WAITING\" -> switch to the specified keyword, but only if it
8343 really is a member of `org-todo-keywords'."
8345 (if (equal arg '(16)) (setq arg 'nextset))
8348 (org-back-to-heading)
8349 (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
8350 (or (looking-at (concat " +" org-todo-regexp " *"))
8352 (let* ((match-data (match-data))
8353 (startpos (point-at-bol))
8354 (logging (save-match-data (org-entry-get nil "LOGGING" t)))
8355 (org-log-done org-log-done)
8356 (org-log-repeat org-log-repeat)
8357 (org-todo-log-states org-todo-log-states)
8358 (this (match-string 1))
8359 (hl-pos (match-beginning 0))
8360 (head (org-get-todo-sequence-head this))
8361 (ass (assoc head org-todo-kwd-alist))
8362 (interpret (nth 1 ass))
8363 (done-word (nth 3 ass))
8364 (final-done-word (nth 4 ass))
8365 (last-state (or this ""))
8366 (completion-ignore-case t)
8367 (member (member this org-todo-keywords-1))
8370 ((and org-todo-key-trigger
8371 (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
8372 (and (not arg) org-use-fast-todo-selection
8373 (not (eq org-use-fast-todo-selection 'prefix)))))
8374 ;; Use fast selection
8375 (org-fast-todo-selection))
8376 ((and (equal arg '(4))
8377 (or (not org-use-fast-todo-selection)
8378 (not org-todo-key-trigger)))
8379 ;; Read a state with completion
8380 (org-ido-completing-read "State: " (mapcar (lambda(x) (list x))
8381 org-todo-keywords-1)
8385 (if tail (car tail) nil)
8386 (car org-todo-keywords-1)))
8388 (if (equal member org-todo-keywords-1)
8391 (nth (- (length org-todo-keywords-1) (length tail) 2)
8392 org-todo-keywords-1)
8393 (org-last org-todo-keywords-1))))
8394 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
8395 (setq arg nil))) ; hack to fall back to cycling
8397 ;; user or caller requests a specific state
8399 ((equal arg "") nil)
8400 ((eq arg 'none) nil)
8401 ((eq arg 'done) (or done-word (car org-done-keywords)))
8403 (or (car (cdr (member head org-todo-heads)))
8404 (car org-todo-heads)))
8405 ((eq arg 'previousset)
8406 (let ((org-todo-heads (reverse org-todo-heads)))
8407 (or (car (cdr (member head org-todo-heads)))
8408 (car org-todo-heads))))
8409 ((car (member arg org-todo-keywords-1)))
8410 ((nth (1- (prefix-numeric-value arg))
8411 org-todo-keywords-1))))
8412 ((null member) (or head (car org-todo-keywords-1)))
8413 ((equal this final-done-word) nil) ;; -> make empty
8414 ((null tail) nil) ;; -> first entry
8415 ((eq interpret 'sequence)
8417 ((memq interpret '(type priority))
8418 (if (eq this-command last-command)
8420 (if (> (length tail) 0)
8421 (or done-word (car org-done-keywords))
8424 (next (if state (concat " " state " ") " "))
8425 (change-plist (list :type 'todo-state-change :from this :to state
8426 :position startpos))
8428 (when org-blocker-hook
8429 (setq org-last-todo-state-is-todo
8430 (not (member this org-done-keywords)))
8431 (unless (save-excursion
8433 (run-hook-with-args-until-failure
8434 'org-blocker-hook change-plist)))
8436 (error "TODO state change from %s to %s blocked" this state)
8438 (message "TODO state change from %s to %s blocked" this state)
8439 (throw 'exit nil))))
8440 (store-match-data match-data)
8441 (replace-match next t t)
8442 (unless (pos-visible-in-window-p hl-pos)
8443 (message "TODO state changed to %s" (org-trim next)))
8445 (setq head (org-get-todo-sequence-head state)
8446 ass (assoc head org-todo-kwd-alist)
8447 interpret (nth 1 ass)
8448 done-word (nth 3 ass)
8449 final-done-word (nth 4 ass)))
8450 (when (memq arg '(nextset previousset))
8451 (message "Keyword-Set %d/%d: %s"
8452 (- (length org-todo-sets) -1
8453 (length (memq (assoc state org-todo-sets) org-todo-sets)))
8454 (length org-todo-sets)
8455 (mapconcat 'identity (assoc state org-todo-sets) " ")))
8456 (setq org-last-todo-state-is-todo
8457 (not (member state org-done-keywords)))
8458 (setq now-done-p (and (member state org-done-keywords)
8459 (not (member this org-done-keywords))))
8460 (and logging (org-local-logging logging))
8461 (when (and (or org-todo-log-states org-log-done)
8462 (not (memq arg '(nextset previousset))))
8463 ;; we need to look at recording a time and note
8464 (setq dolog (or (nth 1 (assoc state org-todo-log-states))
8465 (nth 2 (assoc this org-todo-log-states))))
8467 (member state org-not-done-keywords)
8468 (not (member this org-not-done-keywords)))
8469 ;; This is now a todo state and was not one before
8470 ;; If there was a CLOSED time stamp, get rid of it.
8471 (org-add-planning-info nil nil 'closed))
8472 (when (and now-done-p org-log-done)
8473 ;; It is now done, and it was not done before
8474 (org-add-planning-info 'closed (org-current-time))
8475 (if (and (not dolog) (eq 'note org-log-done))
8476 (org-add-log-setup 'done state 'findpos 'note)))
8477 (when (and state dolog)
8478 ;; This is a non-nil state, and we need to log it
8479 (org-add-log-setup 'state state 'findpos dolog)))
8480 ;; Fixup tag positioning
8481 (org-todo-trigger-tag-changes state)
8482 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
8483 (when org-provide-todo-statistics
8484 (org-update-parent-todo-statistics))
8485 (run-hooks 'org-after-todo-state-change-hook)
8486 (if (and arg (not (member state org-done-keywords)))
8487 (setq head (org-get-todo-sequence-head state)))
8488 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
8489 ;; Do we need to trigger a repeat?
8491 (when (boundp 'org-agenda-headline-snapshot-before-repeat)
8492 ;; This is for the agenda, take a snapshot of the headline.
8494 (setq org-agenda-headline-snapshot-before-repeat
8495 (org-get-heading))))
8496 (org-auto-repeat-maybe state))
8497 ;; Fixup cursor location if close to the keyword
8498 (if (and (outline-on-heading-p)
8500 (save-excursion (beginning-of-line 1)
8501 (looking-at org-todo-line-regexp))
8502 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
8504 (goto-char (or (match-end 2) (match-end 1)))
8506 (when org-trigger-hook
8508 (run-hook-with-args 'org-trigger-hook change-plist)))))))
8510 (defun org-block-todo-from-children-or-siblings (change-plist)
8511 "Block turning an entry into a TODO, using the hierarchy.
8512 This checks whether the current task should be blocked from state
8513 changes. Such blocking occurs when:
8515 1. The task has children which are not all in a completed state.
8517 2. A task has a parent with the property :ORDERED:, and there
8518 are siblings prior to the current task with incomplete
8521 ;; If this is not a todo state change, or if this entry is already DONE,
8523 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
8524 (member (plist-get change-plist :from)
8525 (cons 'done org-done-keywords)))
8526 (throw 'dont-block t))
8527 ;; If this task has children, and any are undone, it's blocked
8529 (org-back-to-heading t)
8530 (let ((this-level (funcall outline-level)))
8531 (outline-next-heading)
8532 (let ((child-level (funcall outline-level)))
8533 (while (and (not (eobp))
8534 (> child-level this-level))
8535 ;; this todo has children, check whether they are all
8537 (if (and (not (org-entry-is-done-p))
8538 (org-entry-is-todo-p))
8539 (throw 'dont-block nil))
8540 (outline-next-heading)
8541 (setq child-level (funcall outline-level))))))
8542 ;; Otherwise, if the task's parent has the :ORDERED: property, and
8543 ;; any previous siblings are undone, it's blocked
8545 (org-back-to-heading t)
8546 (when (save-excursion
8548 (outline-up-heading 1)
8549 (org-entry-get (point) "ORDERED")))
8550 (let* ((this-level (funcall outline-level))
8551 (current-level this-level))
8552 (while (and (not (bobp))
8553 (= current-level this-level))
8554 (outline-previous-heading)
8555 (setq current-level (funcall outline-level))
8556 (if (= current-level this-level)
8557 ;; this todo has children, check whether they are all
8559 (if (and (not (org-entry-is-done-p))
8560 (org-entry-is-todo-p))
8561 (throw 'dont-block nil)))))))
8564 (defun org-update-parent-todo-statistics ()
8565 "Update any statistics cookie in the parent of the current headline."
8567 (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
8568 level (cnt-all 0) (cnt-done 0) is-percent kwd)
8571 (setq level (org-up-heading-safe))
8573 (re-search-forward box-re (point-at-eol) t))
8575 (setq is-percent (match-end 2))
8577 (unless (outline-next-heading) (throw 'exit nil))
8578 (while (looking-at org-todo-line-regexp)
8579 (setq kwd (match-string 2))
8580 (and kwd (setq cnt-all (1+ cnt-all)))
8581 (and (member kwd org-done-keywords)
8582 (setq cnt-done (1+ cnt-done)))
8584 (org-forward-same-level 1)
8585 (error (end-of-line 1)))))
8588 (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
8589 (format "[%d/%d]" cnt-done cnt-all)))
8590 (run-hook-with-args 'org-after-todo-statistics-hook
8591 cnt-done (- cnt-all cnt-done))))))
8593 (defvar org-after-todo-statistics-hook nil
8594 "Hook that is called after a TODO statistics cookie has been updated.
8595 Each function is called with two arguments: the number of not-done entries
8596 and the number of done entries.
8598 For example, the following function, when added to this hook, will switch
8599 an entry to DONE when all children are done, and back to TODO when new
8600 entries are set to a TODO status. Note that this hook is only called
8601 when there is a statistics cookie in the headline!
8603 (defun org-summary-todo (n-done n-not-done)
8604 \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
8605 (let (org-log-done org-log-states) ; turn off logging
8606 (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
8609 (defun org-todo-trigger-tag-changes (state)
8610 "Apply the changes defined in `org-todo-state-tags-triggers'."
8611 (let ((l org-todo-state-tags-triggers)
8613 (when (or (not state) (equal state ""))
8614 (setq changes (append changes (cdr (assoc "" l)))))
8615 (when (and (stringp state) (> (length state) 0))
8616 (setq changes (append changes (cdr (assoc state l)))))
8617 (when (member state org-not-done-keywords)
8618 (setq changes (append changes (cdr (assoc 'todo l)))))
8619 (when (member state org-done-keywords)
8620 (setq changes (append changes (cdr (assoc 'done l)))))
8622 (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
8624 (defun org-local-logging (value)
8625 "Get logging settings from a property VALUE."
8627 ;; directly set the variables, they are already local.
8628 (setq org-log-done nil
8630 org-todo-log-states nil)
8631 (setq words (org-split-string value))
8632 (while (setq w (pop words))
8634 ((setq a (assoc w org-startup-options))
8635 (and (member (nth 1 a) '(org-log-done org-log-repeat))
8636 (set (nth 1 a) (nth 2 a))))
8637 ((setq a (org-extract-log-state-settings w))
8638 (and (member (car a) org-todo-keywords-1)
8639 (push a org-todo-log-states)))))))
8641 (defun org-get-todo-sequence-head (kwd)
8642 "Return the head of the TODO sequence to which KWD belongs.
8643 If KWD is not set, check if there is a text property remembering the
8648 (or (get-text-property (point-at-bol) 'org-todo-head)
8650 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
8651 nil (point-at-eol)))
8652 (get-text-property p 'org-todo-head))))
8653 ((not (member kwd org-todo-keywords-1))
8654 (car org-todo-keywords-1))
8655 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
8657 (defun org-fast-todo-selection ()
8658 "Fast TODO keyword selection with single keys.
8659 Returns the new TODO keyword, or nil if no state change should occur."
8660 (let* ((fulltable org-todo-key-alist)
8661 (done-keywords org-done-keywords) ;; needed for the faces.
8662 (maxlen (apply 'max (mapcar
8664 (if (stringp (car x)) (string-width (car x)) 0))
8667 (fwidth (+ maxlen 3 1 3))
8668 (ncol (/ (- (window-width) 4) fwidth))
8671 (save-window-excursion
8673 (set-buffer (get-buffer-create " *Org todo*"))
8674 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
8676 (org-set-local 'org-done-keywords done-keywords)
8677 (setq tbl fulltable cnt 0)
8678 (while (setq e (pop tbl))
8680 ((equal e '(:startgroup))
8681 (push '() groups) (setq ingroup t)
8682 (when (not (= cnt 0))
8686 ((equal e '(:endgroup))
8687 (setq ingroup nil cnt 0)
8690 (setq tg (car e) c (cdr e))
8691 (if ingroup (push tg (car groups)))
8692 (setq tg (org-add-props tg nil 'face
8693 (org-get-todo-face tg)))
8694 (if (and (= cnt 0) (not ingroup)) (insert " "))
8695 (insert "[" c "] " tg (make-string
8696 (- fwidth 4 (length tg)) ?\ ))
8697 (when (= (setq cnt (1+ cnt)) ncol)
8699 (if ingroup (insert " "))
8702 (goto-char (point-min))
8703 (if (not expert) (org-fit-window-to-buffer))
8704 (message "[a-z..]:Set [SPC]:clear")
8705 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
8708 (and (= c ?q) (not (rassoc c fulltable))))
8711 ((setq e (rassoc c fulltable) tg (car e))
8713 (t (setq quit-flag t))))))
8715 (defun org-entry-is-todo-p ()
8716 (member (org-get-todo-state) org-not-done-keywords))
8718 (defun org-entry-is-done-p ()
8719 (member (org-get-todo-state) org-done-keywords))
8721 (defun org-get-todo-state ()
8723 (org-back-to-heading t)
8724 (and (looking-at org-todo-line-regexp)
8728 (defun org-at-date-range-p (&optional inactive-ok)
8729 "Is the cursor inside a date range?"
8733 (let ((pos (point)))
8734 (skip-chars-backward "^[<\r\n")
8735 (skip-chars-backward "<[")
8736 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
8737 (>= (match-end 0) pos)
8739 (skip-chars-backward "^<[\r\n")
8740 (skip-chars-backward "<[")
8741 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
8742 (>= (match-end 0) pos)
8746 (defun org-get-repeat ()
8747 "Check if there is a deadline/schedule with repeater in this entry."
8750 (org-back-to-heading t)
8751 (if (re-search-forward
8752 org-repeat-re (save-excursion (outline-next-heading) (point)) t)
8753 (match-string 1)))))
8755 (defvar org-last-changed-timestamp)
8756 (defvar org-last-inserted-timestamp)
8757 (defvar org-log-post-message)
8758 (defvar org-log-note-purpose)
8759 (defvar org-log-note-how)
8760 (defvar org-log-note-extra)
8761 (defun org-auto-repeat-maybe (done-word)
8762 "Check if the current headline contains a repeated deadline/schedule.
8763 If yes, set TODO state back to what it was and change the base date
8764 of repeating deadline/scheduled time stamps to new date.
8765 This function is run automatically after each state change to a DONE state."
8766 ;; last-state is dynamically scoped into this function
8767 (let* ((repeat (org-get-repeat))
8768 (aa (assoc last-state org-todo-kwd-alist))
8769 (interpret (nth 1 aa))
8771 (whata '(("d" . day) ("m" . month) ("y" . year)))
8772 (msg "Entry repeats: ")
8774 (org-todo-log-states nil)
8775 (nshiftmax 10) (nshift 0)
8776 re type n what ts time)
8778 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
8779 (org-todo (if (eq interpret 'type) last-state head))
8780 (when org-log-repeat
8781 (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
8782 (memq 'org-add-log-note post-command-hook))
8783 ;; OK, we are already setup for some record
8784 (if (eq org-log-repeat 'note)
8785 ;; make sure we take a note, not only a time stamp
8786 (setq org-log-note-how 'note))
8787 ;; Set up for taking a record
8788 (org-add-log-setup 'state (or done-word (car org-done-keywords))
8789 'findpos org-log-repeat)))
8790 (org-back-to-heading t)
8791 (org-add-planning-info nil nil 'closed)
8792 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
8793 org-deadline-time-regexp "\\)\\|\\("
8794 org-ts-regexp "\\)"))
8795 (while (re-search-forward
8796 re (save-excursion (outline-next-heading) (point)) t)
8797 (setq type (if (match-end 1) org-scheduled-string
8798 (if (match-end 3) org-deadline-string "Plain:"))
8799 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
8800 (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
8801 (setq n (string-to-number (match-string 2 ts))
8802 what (match-string 3 ts))
8803 (if (equal what "w") (setq n (* n 7) what "d"))
8804 ;; Preparation, see if we need to modify the start date for the change
8806 (setq time (save-match-data (org-time-string-to-time ts)))
8808 ((equal (match-string 1 ts) ".")
8809 ;; Shift starting date to today
8810 (org-timestamp-change
8811 (- (time-to-days (current-time)) (time-to-days time))
8813 ((equal (match-string 1 ts) "+")
8814 (while (or (= nshift 0)
8815 (<= (time-to-days time) (time-to-days (current-time))))
8816 (when (= (incf nshift) nshiftmax)
8817 (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
8819 (org-timestamp-change n (cdr (assoc what whata)))
8820 (org-at-timestamp-p t)
8821 (setq ts (match-string 1))
8822 (setq time (save-match-data (org-time-string-to-time ts))))
8823 (org-timestamp-change (- n) (cdr (assoc what whata)))
8824 ;; rematch, so that we have everything in place for the real shift
8825 (org-at-timestamp-p t)
8826 (setq ts (match-string 1))
8827 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
8828 (org-timestamp-change n (cdr (assoc what whata)))
8829 (setq msg (concat msg type " " org-last-changed-timestamp " "))))
8830 (setq org-log-post-message msg)
8831 (message "%s" msg))))
8833 (defun org-show-todo-tree (arg)
8834 "Make a compact tree which shows all headlines marked with TODO.
8835 The tree will show the lines where the regexp matches, and all higher
8836 headlines above the match.
8837 With a \\[universal-argument] prefix, also show the DONE entries.
8838 With a numeric prefix N, construct a sparse tree for the Nth element
8839 of `org-todo-keywords-1'."
8841 (let ((case-fold-search nil)
8843 (cond ((null arg) org-not-done-regexp)
8845 (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
8846 (mapcar 'list org-todo-keywords-1))))
8848 (mapconcat 'identity (org-split-string kwd "|") "\\|")
8850 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
8851 (regexp-quote (nth (1- (prefix-numeric-value arg))
8852 org-todo-keywords-1)))
8853 (t (error "Invalid prefix argument: %s" arg)))))
8854 (message "%d TODO entries found"
8855 (org-occur (concat "^" outline-regexp " *" kwd-re )))))
8857 (defun org-deadline (&optional remove time)
8858 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
8859 With argument REMOVE, remove any deadline from the item.
8860 When TIME is set, it should be an internal time specification, and the
8861 scheduling will use the corresponding date."
8865 (org-remove-timestamp-with-keyword org-deadline-string)
8866 (message "Item no longer has a deadline."))
8867 (if (org-get-repeat)
8868 (error "Cannot change deadline on task with repeater, please do that by hand")
8869 (org-add-planning-info 'deadline time 'closed)
8870 (message "Deadline on %s" org-last-inserted-timestamp))))
8872 (defun org-schedule (&optional remove time)
8873 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
8874 With argument REMOVE, remove any scheduling date from the item.
8875 When TIME is set, it should be an internal time specification, and the
8876 scheduling will use the corresponding date."
8880 (org-remove-timestamp-with-keyword org-scheduled-string)
8881 (message "Item is no longer scheduled."))
8882 (if (org-get-repeat)
8883 (error "Cannot reschedule task with repeater, please do that by hand")
8884 (org-add-planning-info 'scheduled time 'closed)
8885 (message "Scheduled to %s" org-last-inserted-timestamp))))
8887 (defun org-remove-timestamp-with-keyword (keyword)
8888 "Remove all time stamps with KEYWORD in the current entry."
8889 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
8892 (org-back-to-heading t)
8894 (org-end-of-subtree t t)
8895 (while (re-search-backward re beg t)
8897 (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
8898 (equal (char-before) ?\ ))
8899 (backward-delete-char 1)
8900 (if (string-match "^[ \t]*$" (buffer-substring
8901 (point-at-bol) (point-at-eol)))
8902 (delete-region (point-at-bol)
8903 (min (point-max) (1+ (point-at-eol))))))))))
8905 (defun org-add-planning-info (what &optional time &rest remove)
8906 "Insert new timestamp with keyword in the line directly after the headline.
8907 WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
8908 If non is given, the user is prompted for a date.
8909 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
8912 (let (org-time-was-given org-end-time-was-given ts
8913 end default-time default-input)
8915 (when (and (not time) (memq what '(scheduled deadline)))
8916 ;; Try to get a default date/time from existing timestamp
8918 (org-back-to-heading t)
8919 (setq end (save-excursion (outline-next-heading) (point)))
8920 (when (re-search-forward (if (eq what 'scheduled)
8921 org-scheduled-time-regexp
8922 org-deadline-time-regexp)
8924 (setq ts (match-string 1)
8926 (apply 'encode-time (org-parse-time-string ts))
8927 default-input (and ts (org-get-compact-tod ts))))))
8929 ;; If necessary, get the time from the user
8930 (setq time (or time (org-read-date nil 'to-time nil nil
8931 default-time default-input))))
8933 (when (and org-insert-labeled-timestamps-at-point
8934 (member what '(scheduled deadline)))
8936 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
8937 (org-insert-time-stamp time org-time-was-given
8938 nil nil nil (list org-end-time-was-given))
8942 (let (col list elt ts buffer-invisibility-spec)
8943 (org-back-to-heading t)
8944 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
8945 (goto-char (match-end 1))
8946 (setq col (current-column))
8947 (goto-char (match-end 0))
8948 (if (eobp) (insert "\n") (forward-char 1))
8949 (if (and (not (looking-at outline-regexp))
8950 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
8952 (not (equal (match-string 1) org-clock-string)))
8953 (narrow-to-region (match-beginning 0) (match-end 0))
8954 (insert-before-markers "\n")
8956 (narrow-to-region (point) (point))
8957 (and org-adapt-indentation (org-indent-to-column col)))
8958 ;; Check if we have to remove something.
8959 (setq list (cons what remove))
8961 (setq elt (pop list))
8962 (goto-char (point-min))
8963 (when (or (and (eq elt 'scheduled)
8964 (re-search-forward org-scheduled-time-regexp nil t))
8965 (and (eq elt 'deadline)
8966 (re-search-forward org-deadline-time-regexp nil t))
8967 (and (eq elt 'closed)
8968 (re-search-forward org-closed-time-regexp nil t)))
8970 (if (looking-at "--+<[^>]+>") (replace-match ""))
8971 (if (looking-at " +") (replace-match ""))))
8972 (goto-char (point-max))
8975 (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
8976 (cond ((eq what 'scheduled) org-scheduled-string)
8977 ((eq what 'deadline) org-deadline-string)
8978 ((eq what 'closed) org-closed-string))
8980 (setq ts (org-insert-time-stamp
8982 (or org-time-was-given
8983 (and (eq what 'closed) org-log-done-with-time))
8985 nil nil (list org-end-time-was-given)))
8987 (goto-char (point-min))
8989 (if (and (looking-at "[ \t]+\n")
8990 (equal (char-before) ?\n))
8991 (delete-region (1- (point)) (point-at-eol)))
8994 (defvar org-log-note-marker (make-marker))
8995 (defvar org-log-note-purpose nil)
8996 (defvar org-log-note-state nil)
8997 (defvar org-log-note-how nil)
8998 (defvar org-log-note-extra nil)
8999 (defvar org-log-note-window-configuration nil)
9000 (defvar org-log-note-return-to (make-marker))
9001 (defvar org-log-post-message nil
9002 "Message to be displayed after a log note has been stored.
9003 The auto-repeater uses this.")
9005 (defun org-add-note ()
9006 "Add a note to the current entry.
9007 This is done in the same way as adding a state change note."
9009 (org-add-log-setup 'note nil 'findpos nil))
9011 (defvar org-property-end-re)
9012 (defun org-add-log-setup (&optional purpose state findpos how &optional extra)
9013 "Set up the post command hook to take a note.
9014 If this is about to TODO state change, the new state is expected in STATE.
9015 When FINDPOS is non-nil, find the correct position for the note in
9016 the current entry. If not, assume that it can be inserted at point.
9017 HOW is an indicator what kind of note should be created.
9018 EXTRA is additional text that will be inserted into the notes buffer."
9022 (org-back-to-heading t)
9023 (narrow-to-region (point) (save-excursion
9024 (outline-next-heading) (point)))
9025 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
9026 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
9028 (goto-char (match-end 0))
9029 (when (and org-log-state-notes-insert-after-drawers
9031 (forward-line) (looking-at org-drawer-regexp)))
9032 (progn (forward-line)
9033 (while (looking-at org-drawer-regexp)
9034 (goto-char (match-end 0))
9035 (re-search-forward org-property-end-re (point-max) t)
9038 (unless org-log-states-order-reversed
9039 (and (= (char-after) ?\n) (forward-char 1))
9040 (org-skip-over-state-notes)
9041 (skip-chars-backward " \t\n\r")))
9042 (move-marker org-log-note-marker (point))
9043 (setq org-log-note-purpose purpose
9044 org-log-note-state state
9045 org-log-note-how how
9046 org-log-note-extra extra)
9047 (add-hook 'post-command-hook 'org-add-log-note 'append))))
9049 (defun org-skip-over-state-notes ()
9050 "Skip past the list of State notes in an entry."
9051 (if (looking-at "\n[ \t]*- State") (forward-char 1))
9052 (while (looking-at "[ \t]*- State")
9055 (error (org-end-of-item)))))
9057 (defun org-add-log-note (&optional purpose)
9058 "Pop up a window for taking a note, and add this note later at point."
9059 (remove-hook 'post-command-hook 'org-add-log-note)
9060 (setq org-log-note-window-configuration (current-window-configuration))
9061 (delete-other-windows)
9062 (move-marker org-log-note-return-to (point))
9063 (switch-to-buffer (marker-buffer org-log-note-marker))
9064 (goto-char org-log-note-marker)
9065 (org-switch-to-buffer-other-window "*Org Note*")
9067 (if (memq org-log-note-how '(time state))
9068 (let (current-prefix-arg) (org-store-log-note))
9069 (let ((org-inhibit-startup t)) (org-mode))
9070 (insert (format "# Insert note for %s.
9071 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
9073 ((eq org-log-note-purpose 'clock-out) "stopped clock")
9074 ((eq org-log-note-purpose 'done) "closed todo item")
9075 ((eq org-log-note-purpose 'state)
9076 (format "state change to \"%s\"" org-log-note-state))
9077 ((eq org-log-note-purpose 'note)
9079 (t (error "This should not happen")))))
9080 (if org-log-note-extra (insert org-log-note-extra))
9081 (org-set-local 'org-finish-function 'org-store-log-note)))
9083 (defvar org-note-abort nil) ; dynamically scoped
9084 (defun org-store-log-note ()
9085 "Finish taking a log note, and insert it to where it belongs."
9086 (let ((txt (buffer-string))
9087 (note (cdr (assq org-log-note-purpose org-log-note-headings)))
9089 (kill-buffer (current-buffer))
9090 (while (string-match "\\`#.*\n[ \t\n]*" txt)
9091 (setq txt (replace-match "" t t txt)))
9092 (if (string-match "\\s-+\\'" txt)
9093 (setq txt (replace-match "" t t txt)))
9094 (setq lines (org-split-string txt "\n"))
9095 (when (and note (string-match "\\S-" note))
9097 (org-replace-escapes
9099 (list (cons "%u" (user-login-name))
9100 (cons "%U" user-full-name)
9101 (cons "%t" (format-time-string
9102 (org-time-stamp-format 'long 'inactive)
9104 (cons "%s" (if org-log-note-state
9105 (concat "\"" org-log-note-state "\"")
9107 (if lines (setq note (concat note " \\\\")))
9109 (when (or current-prefix-arg org-note-abort) (setq lines nil))
9112 (set-buffer (marker-buffer org-log-note-marker))
9114 (goto-char org-log-note-marker)
9115 (move-marker org-log-note-marker nil)
9117 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
9118 (indent-relative nil)
9119 (insert "- " (pop lines))
9120 (org-indent-line-function)
9121 (beginning-of-line 1)
9122 (looking-at "[ \t]*")
9123 (setq ind (concat (match-string 0) " "))
9125 (while lines (insert "\n" ind (pop lines)))))))
9126 (set-window-configuration org-log-note-window-configuration)
9127 (with-current-buffer (marker-buffer org-log-note-return-to)
9128 (goto-char org-log-note-return-to))
9129 (move-marker org-log-note-return-to nil)
9130 (and org-log-post-message (message "%s" org-log-post-message)))
9132 (defun org-sparse-tree (&optional arg)
9133 "Create a sparse tree, prompt for the details.
9134 This command can create sparse trees. You first need to select the type
9135 of match used to create the tree:
9137 t Show entries with a specific TODO keyword.
9138 T Show entries selected by a tags match.
9139 p Enter a property name and its value (both with completion on existing
9140 names/values) and show entries with that property.
9141 r Show entries matching a regular expression
9142 d Show deadlines due within `org-deadline-warning-days'."
9144 (let (ans kwd value)
9145 (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
9146 (setq ans (read-char-exclusive))
9149 (call-interactively 'org-check-deadlines))
9151 (call-interactively 'org-check-before-date))
9153 (org-show-todo-tree '(4)))
9155 (call-interactively 'org-tags-sparse-tree))
9156 ((member ans '(?p ?P))
9157 (setq kwd (org-ido-completing-read "Property: "
9158 (mapcar 'list (org-buffer-property-keys))))
9159 (setq value (org-ido-completing-read "Value: "
9160 (mapcar 'list (org-property-values kwd))))
9161 (unless (string-match "\\`{.*}\\'" value)
9162 (setq value (concat "\"" value "\"")))
9163 (org-tags-sparse-tree arg (concat kwd "=" value)))
9164 ((member ans '(?r ?R ?/))
9165 (call-interactively 'org-occur))
9166 (t (error "No such sparse tree command \"%c\"" ans)))))
9168 (defvar org-occur-highlights nil
9169 "List of overlays used for occur matches.")
9170 (make-variable-buffer-local 'org-occur-highlights)
9171 (defvar org-occur-parameters nil
9172 "Parameters of the active org-occur calls.
9173 This is a list, each call to org-occur pushes as cons cell,
9174 containing the regular expression and the callback, onto the list.
9175 The list can contain several entries if `org-occur' has been called
9176 several time with the KEEP-PREVIOUS argument. Otherwise, this list
9177 will only contain one set of parameters. When the highlights are
9178 removed (for example with `C-c C-c', or with the next edit (depending
9179 on `org-remove-highlights-with-change'), this variable is emptied
9181 (make-variable-buffer-local 'org-occur-parameters)
9183 (defun org-occur (regexp &optional keep-previous callback)
9184 "Make a compact tree which shows all matches of REGEXP.
9185 The tree will show the lines where the regexp matches, and all higher
9186 headlines above the match. It will also show the heading after the match,
9187 to make sure editing the matching entry is easy.
9188 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
9189 call to `org-occur' will be kept, to allow stacking of calls to this
9191 If CALLBACK is non-nil, it is a function which is called to confirm
9192 that the match should indeed be shown."
9193 (interactive "sRegexp: \nP")
9194 (unless keep-previous
9195 (org-remove-occur-highlights nil nil t))
9196 (push (cons regexp callback) org-occur-parameters)
9199 (goto-char (point-min))
9200 (if (or (not keep-previous) ; do not want to keep
9201 (not org-occur-highlights)) ; no previous matches
9204 (while (re-search-forward regexp nil t)
9205 (when (or (not callback)
9206 (save-match-data (funcall callback)))
9208 (when org-highlight-sparse-tree-matches
9209 (org-highlight-new-match (match-beginning 0) (match-end 0)))
9210 (org-show-context 'occur-tree))))
9211 (when org-remove-highlights-with-change
9212 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
9214 (unless org-sparse-tree-open-archived-trees
9215 (org-hide-archived-subtrees (point-min) (point-max)))
9216 (run-hooks 'org-occur-hook)
9218 (message "%d match(es) for regexp %s" cnt regexp))
9221 (defun org-show-context (&optional key)
9222 "Make sure point and context and visible.
9223 How much context is shown depends upon the variables
9224 `org-show-hierarchy-above', `org-show-following-heading'. and
9225 `org-show-siblings'."
9226 (let ((heading-p (org-on-heading-p t))
9227 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
9228 (following-p (org-get-alist-option org-show-following-heading key))
9229 (entry-p (org-get-alist-option org-show-entry-below key))
9230 (siblings-p (org-get-alist-option org-show-siblings key)))
9232 ;; Show heading or entry text
9233 (if (and heading-p (not entry-p))
9234 (org-flag-heading nil) ; only show the heading
9235 (and (or entry-p (org-invisible-p) (org-invisible-p2))
9236 (org-show-hidden-entry))) ; show entire entry
9238 ;; Show next sibling, or heading below text
9240 (and (if heading-p (org-goto-sibling) (outline-next-heading))
9241 (org-flag-heading nil))))
9242 (when siblings-p (org-show-siblings))
9244 ;; show all higher headings, possibly with siblings
9246 (while (and (condition-case nil
9247 (progn (org-up-heading-all 1) t)
9250 (org-flag-heading nil)
9251 (when siblings-p (org-show-siblings))))))))
9253 (defun org-reveal (&optional siblings)
9254 "Show current entry, hierarchy above it, and the following headline.
9255 This can be used to show a consistent set of context around locations
9256 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
9257 not t for the search context.
9259 With optional argument SIBLINGS, on each level of the hierarchy all
9260 siblings are shown. This repairs the tree structure to what it would
9261 look like when opened with hierarchical calls to `org-cycle'."
9263 (let ((org-show-hierarchy-above t)
9264 (org-show-following-heading t)
9265 (org-show-siblings (if siblings t org-show-siblings)))
9266 (org-show-context nil)))
9268 (defun org-highlight-new-match (beg end)
9269 "Highlight from BEG to END and mark the highlight is an occur headline."
9270 (let ((ov (org-make-overlay beg end)))
9271 (org-overlay-put ov 'face 'secondary-selection)
9272 (push ov org-occur-highlights)))
9274 (defun org-remove-occur-highlights (&optional beg end noremove)
9275 "Remove the occur highlights from the buffer.
9276 BEG and END are ignored. If NOREMOVE is nil, remove this function
9277 from the `before-change-functions' in the current buffer."
9279 (unless org-inhibit-highlight-removal
9280 (mapc 'org-delete-overlay org-occur-highlights)
9281 (setq org-occur-highlights nil)
9282 (setq org-occur-parameters nil)
9284 (remove-hook 'before-change-functions
9285 'org-remove-occur-highlights 'local))))
9289 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
9290 "Regular expression matching the priority indicator.")
9292 (defvar org-remove-priority-next-time nil)
9294 (defun org-priority-up ()
9295 "Increase the priority of the current item."
9299 (defun org-priority-down ()
9300 "Decrease the priority of the current item."
9302 (org-priority 'down))
9304 (defun org-priority (&optional action)
9305 "Change the priority of an item by ARG.
9306 ACTION can be `set', `up', `down', or a character."
9308 (setq action (or action 'set))
9309 (let (current new news have remove)
9311 (org-back-to-heading)
9312 (if (looking-at org-priority-regexp)
9313 (setq current (string-to-char (match-string 2))
9315 (setq current org-default-priority))
9317 ((or (eq action 'set)
9318 (if (featurep 'xemacs) (characterp action) (integerp action)))
9319 (if (not (eq action 'set))
9321 (message "Priority %c-%c, SPC to remove: "
9322 org-highest-priority org-lowest-priority)
9323 (setq new (read-char-exclusive)))
9324 (if (and (= (upcase org-highest-priority) org-highest-priority)
9325 (= (upcase org-lowest-priority) org-lowest-priority))
9326 (setq new (upcase new)))
9327 (cond ((equal new ?\ ) (setq remove t))
9328 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
9329 (error "Priority must be between `%c' and `%c'"
9330 org-highest-priority org-lowest-priority))))
9332 (if (and (not have) (eq last-command this-command))
9333 (setq new org-lowest-priority)
9334 (setq new (if (and org-priority-start-cycle-with-default (not have))
9335 org-default-priority (1- current)))))
9337 (if (and (not have) (eq last-command this-command))
9338 (setq new org-highest-priority)
9339 (setq new (if (and org-priority-start-cycle-with-default (not have))
9340 org-default-priority (1+ current)))))
9341 (t (error "Invalid action")))
9342 (if (or (< (upcase new) org-highest-priority)
9343 (> (upcase new) org-lowest-priority))
9345 (setq news (format "%c" new))
9348 (replace-match "" t t nil 1)
9349 (replace-match news t t nil 2))
9351 (error "No priority cookie found in line")
9352 (looking-at org-todo-line-regexp)
9355 (goto-char (match-end 2))
9356 (insert " [#" news "]"))
9357 (goto-char (match-beginning 3))
9358 (insert "[#" news "] ")))))
9359 (org-preserve-lc (org-set-tags nil 'align))
9361 (message "Priority removed")
9362 (message "Priority of current item set to %s" news))))
9365 (defun org-get-priority (s)
9366 "Find priority cookie and return priority."
9368 (if (not (string-match org-priority-regexp s))
9369 (* 1000 (- org-lowest-priority org-default-priority))
9370 (* 1000 (- org-lowest-priority
9371 (string-to-char (match-string 2 s)))))))
9375 (defvar org-agenda-archives-mode)
9376 (defun org-scan-tags (action matcher &optional todo-only)
9377 "Scan headline tags with inheritance and produce output ACTION.
9379 ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
9380 or `agenda' to produce an entry list for an agenda view. It can also be
9381 a Lisp form or a function that should be called at each matched headline, in
9382 this case the return value is a list of all return values from these calls.
9384 MATCHER is a Lisp form to be evaluated, testing if a given set of tags
9385 qualifies a headline for inclusion. When TODO-ONLY is non-nil,
9386 only lines with a TODO keyword are included in the output."
9387 (require 'org-agenda)
9388 (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
9389 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
9391 "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
9392 (props (list 'face 'default
9393 'done-face 'org-done
9394 'undone-face 'default
9395 'mouse-face 'highlight
9396 'org-not-done-regexp org-not-done-regexp
9397 'org-todo-regexp org-todo-regexp
9398 'keymap org-agenda-keymap
9400 (format "mouse-2 or RET jump to org file %s"
9401 (abbreviate-file-name
9402 (or (buffer-file-name (buffer-base-buffer))
9403 (buffer-name (buffer-base-buffer)))))))
9404 (case-fold-search nil)
9405 lspos tags tags-list
9406 (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
9407 (llast 0) rtn rtn1 level category i txt
9408 todo marker entry priority)
9409 (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
9410 (setq action (list 'lambda nil action)))
9412 (goto-char (point-min))
9413 (when (eq action 'sparse-tree)
9415 (org-remove-occur-highlights))
9416 (while (re-search-forward re nil t)
9418 (setq todo (if (match-end 1) (match-string 2))
9419 tags (if (match-end 4) (match-string 4)))
9420 (goto-char (setq lspos (1+ (match-beginning 0))))
9421 (setq level (org-reduced-level (funcall outline-level))
9422 category (org-get-category))
9423 (setq i llast llast level)
9424 ;; remove tag lists from same and sublevels
9426 (when (setq entry (assoc i tags-alist))
9427 (setq tags-alist (delete entry tags-alist)))
9429 ;; add the next tags
9431 (setq tags (mapcar 'downcase (org-split-string tags ":"))
9433 (cons (cons level tags) tags-alist)))
9434 ;; compile tags for current headline
9436 (if org-use-tag-inheritance
9437 (apply 'append (mapcar 'cdr (reverse tags-alist)))
9439 (when org-use-tag-inheritance
9440 (setcdr (car tags-alist)
9442 (setq x (copy-sequence x))
9443 (org-add-prop-inherited x))
9444 (cdar tags-alist))))
9445 (when (and tags org-use-tag-inheritance
9446 (not (eq t org-use-tag-inheritance)))
9447 ;; selective inheritance, remove uninherited ones
9448 (setcdr (car tags-alist)
9449 (org-remove-uniherited-tags (cdar tags-alist))))
9450 (when (and (or (not todo-only)
9451 (and (member todo org-not-done-keywords)
9452 (or (not org-agenda-tags-todo-honor-ignore-options)
9453 (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
9454 (let ((case-fold-search t)) (eval matcher))
9456 (not (member org-archive-tag tags-list))
9457 ;; we have an archive tag, should we use this anyway?
9458 (or (not org-agenda-skip-archived-trees)
9459 (and (eq action 'agenda) org-agenda-archives-mode))))
9460 (unless (eq action 'sparse-tree) (org-agenda-skip))
9462 ;; select this headline
9465 ((eq action 'sparse-tree)
9466 (and org-highlight-sparse-tree-matches
9467 (org-get-heading) (match-end 0)
9468 (org-highlight-new-match
9469 (match-beginning 0) (match-beginning 1)))
9470 (org-show-context 'tags-tree))
9471 ((eq action 'agenda)
9472 (setq txt (org-format-agenda-item
9475 (if org-tags-match-list-sublevels
9476 (make-string (1- level) ?.) "")
9478 category (org-get-tags-at))
9479 priority (org-get-priority txt))
9481 (setq marker (org-agenda-new-marker))
9482 (org-add-props txt props
9483 'org-marker marker 'org-hd-marker marker 'org-category category
9484 'priority priority 'type "tagsmatch")
9488 (setq rtn1 (funcall action))
9490 (goto-char (point-at-eol)))
9491 (t (error "Invalid action")))
9493 ;; if we are to skip sublevels, jump to end of subtree
9494 (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
9495 (when (and (eq action 'sparse-tree)
9496 (not org-sparse-tree-open-archived-trees))
9497 (org-hide-archived-subtrees (point-min) (point-max)))
9500 (defun org-remove-uniherited-tags (tags)
9501 "Remove all tags that are not inherited from the list TAGS."
9503 ((eq org-use-tag-inheritance t)
9504 (if org-tags-exclude-from-inheritance
9505 (org-delete-all org-tags-exclude-from-inheritance tags)
9507 ((not org-use-tag-inheritance) nil)
9508 ((stringp org-use-tag-inheritance)
9511 (if (and (string-match org-use-tag-inheritance x)
9512 (not (member x org-tags-exclude-from-inheritance)))
9515 ((listp org-use-tag-inheritance)
9518 (if (member x org-use-tag-inheritance) x nil))
9521 (defvar todo-only) ;; dynamically scoped
9523 (defun org-tags-sparse-tree (&optional todo-only match)
9524 "Create a sparse tree according to tags string MATCH.
9525 MATCH can contain positive and negative selection of tags, like
9526 \"+WORK+URGENT-WITHBOSS\".
9527 If optional argument TODO-ONLY is non-nil, only select lines that are
9530 (org-prepare-agenda-buffers (list (current-buffer)))
9531 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
9533 (defvar org-cached-props nil)
9534 (defun org-cached-entry-get (pom property)
9535 (if (or (eq t org-use-property-inheritance)
9536 (and (stringp org-use-property-inheritance)
9537 (string-match org-use-property-inheritance property))
9538 (and (listp org-use-property-inheritance)
9539 (member property org-use-property-inheritance)))
9540 ;; Caching is not possible, check it directly
9541 (org-entry-get pom property 'inherit)
9542 ;; Get all properties, so that we can do complicated checks easily
9543 (cdr (assoc property (or org-cached-props
9544 (setq org-cached-props
9545 (org-entry-properties pom)))))))
9547 (defun org-global-tags-completion-table (&optional files)
9548 "Return the list of all tags in all agenda buffer/files."
9555 (set-buffer (find-file-noselect file))
9556 (append (org-get-buffer-tags)
9557 (mapcar (lambda (x) (if (stringp (car-safe x))
9558 (list (car-safe x)) nil))
9560 (if (and files (car files))
9562 (org-agenda-files))))))))
9564 (defun org-make-tags-matcher (match)
9565 "Create the TAGS//TODO matcher form for the selection string MATCH."
9566 ;; todo-only is scoped dynamically into this function, and the function
9567 ;; may change it if the matcher asks for it.
9569 ;; Get a new match request, with completion
9570 (let ((org-last-tags-completion-table
9571 (org-global-tags-completion-table)))
9572 (setq match (org-completing-read
9573 "Match: " 'org-tags-completion-function nil nil nil
9574 'org-tags-history))))
9576 ;; Parse the string and create a lisp form
9577 (let ((match0 match)
9578 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
9580 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
9581 orterms term orlist re-p str-p level-p level-op time-p
9582 prop-p pn pv po cat-p gv rest)
9583 (if (string-match "/+" match)
9584 ;; match contains also a todo-matching request
9586 (setq tagsmatch (substring match 0 (match-beginning 0))
9587 todomatch (substring match (match-end 0)))
9588 (if (string-match "^!" todomatch)
9589 (setq todo-only t todomatch (substring todomatch 1)))
9590 (if (string-match "^\\s-*$" todomatch)
9591 (setq todomatch nil)))
9592 ;; only matching tags
9593 (setq tagsmatch match todomatch nil))
9595 ;; Make the tags matcher
9596 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
9597 (setq tagsmatcher t)
9598 (setq orterms (org-split-string tagsmatch "|") orlist nil)
9599 (while (setq term (pop orterms))
9600 (while (and (equal (substring term -1) "\\") orterms)
9601 (setq term (concat term "|" (pop orterms)))) ; repair bad split
9602 (while (string-match re term)
9603 (setq rest (substring term (match-end 0))
9604 minus (and (match-end 1)
9605 (equal (match-string 1 term) "-"))
9606 tag (match-string 2 term)
9607 re-p (equal (string-to-char tag) ?{)
9608 level-p (match-end 4)
9609 prop-p (match-end 5)
9611 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
9613 (setq level-op (org-op-to-function (match-string 3 term)))
9614 `(,level-op level ,(string-to-number
9615 (match-string 4 term))))
9617 (setq pn (match-string 5 term)
9618 po (match-string 6 term)
9619 pv (match-string 7 term)
9620 cat-p (equal pn "CATEGORY")
9621 re-p (equal (string-to-char pv) ?{)
9622 str-p (equal (string-to-char pv) ?\")
9623 time-p (save-match-data
9624 (string-match "^\"[[<].*[]>]\"$" pv))
9625 pv (if (or re-p str-p) (substring pv 1 -1) pv))
9626 (if time-p (setq pv (org-matcher-time pv)))
9627 (setq po (org-op-to-function po (if time-p 'time str-p)))
9629 ((equal pn "CATEGORY")
9630 (setq gv '(get-text-property (point) 'org-category)))
9634 (setq gv `(org-cached-entry-get nil ,pn))))
9637 `(not (string-match ,pv (or ,gv "")))
9638 `(string-match ,pv (or ,gv "")))
9640 `(,po (or ,gv "") ,pv)
9641 `(,po (string-to-number (or ,gv ""))
9642 ,(string-to-number pv) ))))
9643 (t `(member ,(downcase tag) tags-list)))
9644 mm (if minus (list 'not mm) mm)
9646 (push mm tagsmatcher))
9647 (push (if (> (length tagsmatcher) 1)
9648 (cons 'and tagsmatcher)
9651 (setq tagsmatcher nil))
9652 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
9654 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
9655 ;; Make the todo matcher
9656 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
9657 (setq todomatcher t)
9658 (setq orterms (org-split-string todomatch "|") orlist nil)
9659 (while (setq term (pop orterms))
9660 (while (string-match re term)
9661 (setq minus (and (match-end 1)
9662 (equal (match-string 1 term) "-"))
9663 kwd (match-string 2 term)
9664 re-p (equal (string-to-char kwd) ?{)
9665 term (substring term (match-end 0))
9667 `(string-match ,(substring kwd 1 -1) todo)
9668 (list 'equal 'todo kwd))
9669 mm (if minus (list 'not mm) mm))
9670 (push mm todomatcher))
9671 (push (if (> (length todomatcher) 1)
9672 (cons 'and todomatcher)
9675 (setq todomatcher nil))
9676 (setq todomatcher (if (> (length orlist) 1)
9677 (cons 'or orlist) (car orlist))))
9679 ;; Return the string and lisp forms of the matcher
9680 (setq matcher (if todomatcher
9681 (list 'and tagsmatcher todomatcher)
9683 (cons match0 matcher)))
9685 (defun org-op-to-function (op &optional stringp)
9686 "Turn an operator into the appropriate function."
9689 ((equal op "<" ) '(< string< org-time<))
9690 ((equal op ">" ) '(> org-string> org-time>))
9691 ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
9692 ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
9693 ((member op '("=" "==")) '(= string= org-time=))
9694 ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
9695 (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
9697 (defun org<> (a b) (not (= a b)))
9698 (defun org-string<= (a b) (or (string= a b) (string< a b)))
9699 (defun org-string>= (a b) (not (string< a b)))
9700 (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
9701 (defun org-string<> (a b) (not (string= a b)))
9702 (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
9703 (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
9704 (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
9705 (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
9706 (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
9707 (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
9709 "Convert S to a floating point time.
9710 If S is already a number, just return it. If it is a string, parse
9711 it as a time string and apply `float-time' to it. If S is nil, just return 0."
9716 (float-time (apply 'encode-time (org-parse-time-string s)))
9720 (defun org-time-today ()
9721 "Time in seconds today at 0:00.
9722 Returns the float number of seconds since the beginning of the
9723 epoch to the beginning of today (00:00)."
9724 (float-time (apply 'encode-time
9725 (append '(0 0 0) (nthcdr 3 (decode-time))))))
9727 (defun org-matcher-time (s)
9728 "Interpret a time comparison value."
9731 ((string= s "<now>") (float-time))
9732 ((string= s "<today>") (org-time-today))
9733 ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
9734 ((string= s "<yesterday>") (- (org-time-today) 86400.0))
9735 ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
9737 (* (string-to-number (match-string 1 s))
9738 (cdr (assoc (match-string 2 s)
9739 '(("d" . 86400.0) ("w" . 604800.0)
9740 ("m" . 2678400.0) ("y" . 31557600.0)))))))
9743 (defun org-match-any-p (re list)
9744 "Does re match any element of list?"
9745 (setq list (mapcar (lambda (x) (string-match re x)) list))
9748 (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
9749 (defvar org-tags-overlay (org-make-overlay 1 1))
9750 (org-detach-overlay org-tags-overlay)
9752 (defun org-get-local-tags-at (&optional pos)
9753 "Get a list of tags defined in the current headline."
9754 (org-get-tags-at pos 'local))
9756 (defun org-get-local-tags ()
9757 "Get a list of tags defined in the current headline."
9758 (org-get-tags-at nil 'local))
9760 (defun org-get-tags-at (&optional pos local)
9761 "Get a list of all headline tags applicable at POS.
9762 POS defaults to point. If tags are inherited, the list contains
9763 the targets in the same sequence as the headlines appear, i.e.
9764 the tags of the current headline come last.
9765 When LOCAL is non-nil, only return tags from the current headline,
9766 ignore inherited ones."
9768 (let (tags ltags lastpos parent)
9772 (goto-char (or pos (point)))
9777 (org-back-to-heading t)
9778 (while (not (equal lastpos (point)))
9779 (setq lastpos (point))
9780 (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
9781 (setq ltags (org-split-string
9782 (org-match-string-no-properties 1) ":"))
9784 (setq ltags (mapcar 'org-add-prop-inherited ltags)))
9787 (org-remove-uniherited-tags ltags)
9790 (or org-use-tag-inheritance (throw 'done t))
9791 (if local (throw 'done t))
9792 (org-up-heading-all 1)
9795 (append (org-remove-uniherited-tags org-file-tags) tags))))
9797 (defun org-add-prop-inherited (s)
9798 (add-text-properties 0 (length s) '(inherited t) s)
9801 (defun org-toggle-tag (tag &optional onoff)
9802 "Toggle the tag TAG for the current line.
9803 If ONOFF is `on' or `off', don't toggle but set to this state."
9806 (org-back-to-heading t)
9807 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
9810 (setq current (match-string 1))
9813 (setq current (nreverse (org-split-string current ":")))
9817 (or (member tag current) (push tag current)))
9819 (or (not (member tag current)) (setq current (delete tag current))))
9820 (t (if (member tag current)
9821 (setq current (delete tag current))
9823 (push tag current))))
9827 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
9828 (org-set-tags nil t))
9829 (delete-horizontal-space))
9830 (run-hooks 'org-after-tags-change-hook))
9833 (defun org-align-tags-here (to-col)
9834 ;; Assumes that this is a headline
9835 (let ((pos (point)) (col (current-column)) ncol tags-l p)
9836 (beginning-of-line 1)
9837 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
9838 (< pos (match-beginning 2)))
9840 (setq tags-l (- (match-end 2) (match-beginning 2)))
9841 (goto-char (match-beginning 1))
9843 (delete-region (point) (1+ (match-beginning 2)))
9844 (setq ncol (max (1+ (current-column))
9848 (- (abs to-col) tags-l))))
9850 (insert (make-string (- ncol (current-column)) ?\ ))
9851 (setq ncol (current-column))
9852 (when indent-tabs-mode (tabify p (point-at-eol)))
9853 (org-move-to-column (min ncol col) t))
9856 (defun org-set-tags-command (&optional arg just-align)
9857 "Call the set-tags command for the current entry."
9859 (if (org-on-heading-p)
9860 (org-set-tags arg just-align)
9862 (org-back-to-heading t)
9863 (org-set-tags arg just-align))))
9865 (defun org-set-tags (&optional arg just-align)
9866 "Set the tags for the current headline.
9867 With prefix ARG, realign all tags in headings in the current buffer."
9869 (let* ((re (concat "^" outline-regexp))
9870 (current (org-get-tags-string))
9871 (col (current-column))
9872 (org-setting-tags t)
9873 table current-tags inherited-tags ; computed below when needed
9877 (goto-char (point-min))
9878 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
9879 (while (re-search-forward re nil t)
9880 (org-set-tags nil t)
9882 (message "All tags realigned to column %d" org-tags-column))
9885 ;; Get a new set of tags from the user
9887 (setq table (or org-tag-alist (org-get-buffer-tags))
9888 org-last-tags-completion-table table
9889 current-tags (org-split-string current ":")
9890 inherited-tags (nreverse
9891 (nthcdr (length current-tags)
9892 (nreverse (org-get-tags-at))))
9894 (if (or (eq t org-use-fast-tag-selection)
9895 (and org-use-fast-tag-selection
9896 (delq nil (mapcar 'cdr table))))
9897 (org-fast-tag-selection
9898 current-tags inherited-tags table
9899 (if org-fast-tag-selection-include-todo org-todo-key-alist))
9900 (let ((org-add-colon-after-tag-completion t))
9902 (org-without-partial-completion
9903 (org-ido-completing-read "Tags: " 'org-tags-completion-function
9904 nil nil current 'org-tags-history)))))))
9905 (while (string-match "[-+&]+" tags)
9906 ;; No boolean logic, just a list
9907 (setq tags (replace-match ":" t t tags))))
9909 (if (string-match "\\`[\t ]*\\'" tags)
9911 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
9912 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
9914 ;; Insert new tags at the correct column
9915 (beginning-of-line 1)
9917 ((and (equal current "") (equal tags "")))
9919 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
9923 (goto-char (match-beginning 0))
9924 (setq c0 (current-column) p0 (point)
9925 c1 (max (1+ c0) (if (> org-tags-column 0)
9927 (- (- org-tags-column) (length tags))))
9928 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
9929 (replace-match rpl t t)
9930 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
9932 (t (error "Tags alignment failed")))
9933 (org-move-to-column col)
9935 (run-hooks 'org-after-tags-change-hook)))))
9937 (defun org-change-tag-in-region (beg end tag off)
9938 "Add or remove TAG for each entry in the region.
9939 This works in the agenda, and also in an org-mode buffer."
9941 (list (region-beginning) (region-end)
9942 (let ((org-last-tags-completion-table
9944 (org-get-buffer-tags)
9945 (org-global-tags-completion-table))))
9946 (org-ido-completing-read
9947 "Tag: " 'org-tags-completion-function nil nil nil
9950 (message "[s]et or [r]emove? ")
9951 (equal (read-char-exclusive) ?r))))
9952 (if (fboundp 'deactivate-mark) (deactivate-mark))
9953 (let ((agendap (equal major-mode 'org-agenda-mode))
9954 l1 l2 m buf pos newhead (cnt 0))
9956 (setq l2 (1- (org-current-line)))
9958 (setq l1 (org-current-line))
9959 (loop for l from l1 to l2 do
9961 (setq m (get-text-property (point) 'org-hd-marker))
9962 (when (or (and (org-mode-p) (org-on-heading-p))
9964 (setq buf (if agendap (marker-buffer m) (current-buffer))
9965 pos (if agendap m (point)))
9966 (with-current-buffer buf
9971 (org-toggle-tag tag (if off 'off 'on))
9972 (setq newhead (org-get-heading)))))
9973 (and agendap (org-agenda-change-all-lines newhead m))))
9974 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
9976 (defun org-tags-completion-function (string predicate &optional flag)
9977 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
9978 (confirm (lambda (x) (stringp (car x)))))
9979 (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
9980 (setq s1 (match-string 1 string)
9981 s2 (match-string 2 string))
9982 (setq s1 "" s2 string))
9986 (setq rtn (try-completion s2 ctable confirm))
9989 (concat s1 s2 (substring rtn (length s2))
9990 (if (and org-add-colon-after-tag-completion
9996 (all-completions s2 ctable confirm)
10000 (assoc s2 ctable)))
10003 (defun org-fast-tag-insert (kwd tags face &optional end)
10004 "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
10005 (insert (format "%-12s" (concat kwd ":"))
10006 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
10009 (defun org-fast-tag-show-exit (flag)
10012 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
10013 (replace-match ""))
10016 (org-move-to-column (- (window-width) 19) t)
10017 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
10019 (defun org-set-current-tags-overlay (current prefix)
10020 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
10021 (if (featurep 'xemacs)
10022 (org-overlay-display org-tags-overlay (concat prefix s)
10023 'secondary-selection)
10024 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
10025 (org-overlay-display org-tags-overlay (concat prefix s)))))
10027 (defun org-fast-tag-selection (current inherited table &optional todo-table)
10028 "Fast tag selection with single keys.
10029 CURRENT is the current list of tags in the headline, INHERITED is the
10030 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
10031 possibly with grouping information. TODO-TABLE is a similar table with
10032 TODO keywords, should these have keys assigned to them.
10033 If the keys are nil, a-z are automatically assigned.
10034 Returns the new tags string, or nil to not change the current settings."
10035 (let* ((fulltable (append table todo-table))
10036 (maxlen (apply 'max (mapcar
10038 (if (stringp (car x)) (string-width (car x)) 0))
10040 (buf (current-buffer))
10041 (expert (eq org-fast-tag-selection-single-key 'expert))
10043 (fwidth (+ maxlen 3 1 3))
10044 (ncol (/ (- (window-width) 4) fwidth))
10047 tg cnt e c char c1 c2 ntable tbl rtn
10048 ov-start ov-end ov-prefix
10049 (exit-after-next org-fast-tag-selection-single-key)
10050 (done-keywords org-done-keywords)
10053 (beginning-of-line 1)
10055 (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
10056 (setq ov-start (match-beginning 1)
10057 ov-end (match-end 1)
10059 (setq ov-start (1- (point-at-eol))
10060 ov-end (1+ ov-start))
10061 (skip-chars-forward "^\n\r")
10064 (buffer-substring (1- (point)) (point))
10065 (if (> (current-column) org-tags-column)
10067 (make-string (- org-tags-column (current-column)) ?\ ))))))
10068 (org-move-overlay org-tags-overlay ov-start ov-end)
10069 (save-window-excursion
10071 (set-buffer (get-buffer-create " *Org tags*"))
10072 (delete-other-windows)
10073 (split-window-vertically)
10074 (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
10076 (org-set-local 'org-done-keywords done-keywords)
10077 (org-fast-tag-insert "Inherited" inherited i-face "\n")
10078 (org-fast-tag-insert "Current" current c-face "\n\n")
10079 (org-fast-tag-show-exit exit-after-next)
10080 (org-set-current-tags-overlay current ov-prefix)
10081 (setq tbl fulltable char ?a cnt 0)
10082 (while (setq e (pop tbl))
10084 ((equal e '(:startgroup))
10085 (push '() groups) (setq ingroup t)
10086 (when (not (= cnt 0))
10090 ((equal e '(:endgroup))
10091 (setq ingroup nil cnt 0)
10094 (setq tg (car e) c2 nil)
10097 ;; automatically assign a character.
10098 (setq c1 (string-to-char
10099 (downcase (substring
10100 tg (if (= (string-to-char tg) ?@) 1 0)))))
10101 (if (or (rassoc c1 ntable) (rassoc c1 table))
10102 (while (or (rassoc char ntable) (rassoc char table))
10103 (setq char (1+ char)))
10105 (setq c (or c2 char)))
10106 (if ingroup (push tg (car groups)))
10107 (setq tg (org-add-props tg nil 'face
10109 ((not (assoc tg table))
10110 (org-get-todo-face tg))
10111 ((member tg current) c-face)
10112 ((member tg inherited) i-face)
10114 (if (and (= cnt 0) (not ingroup)) (insert " "))
10115 (insert "[" c "] " tg (make-string
10116 (- fwidth 4 (length tg)) ?\ ))
10117 (push (cons tg c) ntable)
10118 (when (= (setq cnt (1+ cnt)) ncol)
10120 (if ingroup (insert " "))
10122 (setq ntable (nreverse ntable))
10124 (goto-char (point-min))
10125 (if (not expert) (org-fit-window-to-buffer))
10129 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
10130 (if groups " [!] no groups" " [!]groups")
10131 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
10132 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
10134 ((= c ?\r) (throw 'exit t))
10136 (setq groups (not groups))
10137 (goto-char (point-min))
10138 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
10141 (org-fast-tag-show-exit
10142 (setq exit-after-next (not exit-after-next)))
10144 (delete-other-windows)
10145 (split-window-vertically)
10146 (org-switch-to-buffer-other-window " *Org tags*")
10147 (org-fit-window-to-buffer)))
10149 (and (= c ?q) (not (rassoc c ntable))))
10150 (org-detach-overlay org-tags-overlay)
10151 (setq quit-flag t))
10154 (if exit-after-next (setq exit-after-next 'now)))
10156 (condition-case nil
10157 (setq tg (org-ido-completing-read
10160 (with-current-buffer buf
10161 (org-get-buffer-tags)))))
10162 (quit (setq tg "")))
10163 (when (string-match "\\S-" tg)
10164 (add-to-list 'buffer-tags (list tg))
10165 (if (member tg current)
10166 (setq current (delete tg current))
10167 (push tg current)))
10168 (if exit-after-next (setq exit-after-next 'now)))
10169 ((setq e (rassoc c todo-table) tg (car e))
10170 (with-current-buffer buf
10171 (save-excursion (org-todo tg)))
10172 (if exit-after-next (setq exit-after-next 'now)))
10173 ((setq e (rassoc c ntable) tg (car e))
10174 (if (member tg current)
10175 (setq current (delete tg current))
10176 (loop for g in groups do
10179 (setq current (delete x current)))
10182 (if exit-after-next (setq exit-after-next 'now))))
10184 ;; Create a sorted list
10188 (assoc b (cdr (memq (assoc a ntable) ntable))))))
10189 (if (eq exit-after-next 'now) (throw 'exit t))
10190 (goto-char (point-min))
10191 (beginning-of-line 2)
10192 (delete-region (point) (point-at-eol))
10193 (org-fast-tag-insert "Current" current c-face)
10194 (org-set-current-tags-overlay current ov-prefix)
10195 (while (re-search-forward
10196 (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
10197 (setq tg (match-string 1))
10198 (add-text-properties
10199 (match-beginning 1) (match-end 1)
10202 ((member tg current) c-face)
10203 ((member tg inherited) i-face)
10204 (t (get-text-property (match-beginning 1) 'face))))))
10205 (goto-char (point-min)))))
10206 (org-detach-overlay org-tags-overlay)
10208 (mapconcat 'identity current ":")
10211 (defun org-get-tags-string ()
10212 "Get the TAGS string in the current headline."
10213 (unless (org-on-heading-p t)
10214 (error "Not on a heading"))
10216 (beginning-of-line 1)
10217 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
10218 (org-match-string-no-properties 1)
10221 (defun org-get-tags ()
10222 "Get the list of tags specified in the current headline."
10223 (org-split-string (org-get-tags-string) ":"))
10225 (defun org-get-buffer-tags ()
10226 "Get a table of all tags used in the buffer, for completion."
10229 (goto-char (point-min))
10230 (while (re-search-forward
10231 (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
10232 (when (equal (char-after (point-at-bol 0)) ?*)
10233 (mapc (lambda (x) (add-to-list 'tags x))
10234 (org-split-string (org-match-string-no-properties 1) ":")))))
10235 (mapcar 'list tags)))
10237 ;;;; The mapping API
10240 (defun org-map-entries (func &optional match scope &rest skip)
10241 "Call FUNC at each headline selected by MATCH in SCOPE.
10243 FUNC is a function or a lisp form. The function will be called without
10244 arguments, with the cursor positioned at the beginning of the headline.
10245 The return values of all calls to the function will be collected and
10246 returned as a list.
10248 MATCH is a tags/property/todo match as it is used in the agenda tags view.
10249 Only headlines that are matched by this query will be considered during
10250 the iteration. When MATCH is nil or t, all headlines will be
10251 visited by the iteration.
10253 SCOPE determines the scope of this command. It can be any of:
10255 nil The current buffer, respecting the restriction if any
10256 tree The subtree started with the entry at point
10257 file The current buffer, without restriction
10259 The current buffer, and any archives associated with it
10260 agenda All agenda files
10261 agenda-with-archives
10262 All agenda files with any archive files associated with them
10264 If this is a list, all files in the list will be scanned
10266 The remaining args are treated as settings for the skipping facilities of
10267 the scanner. The following items can be given here:
10269 archive skip trees with the archive tag.
10270 comment skip trees with the COMMENT keyword
10271 function or Emacs Lisp form:
10272 will be used as value for `org-agenda-skip-function', so whenever
10273 the the function returns t, FUNC will not be called for that
10274 entry and search will continue from the point where the
10275 function leaves it."
10276 (let* ((org-agenda-archives-mode nil) ; just to make sure
10277 (org-agenda-skip-archived-trees (memq 'archive skip))
10278 (org-agenda-skip-comment-trees (memq 'comment skip))
10279 (org-agenda-skip-function
10280 (car (org-delete-all '(comment archive) skip)))
10281 (org-tags-match-list-sublevels t)
10283 org-todo-keywords-for-agenda
10284 org-done-keywords-for-agenda
10285 org-todo-keyword-alist-for-agenda
10286 org-tag-alist-for-agenda)
10289 ((eq match t) (setq matcher t))
10290 ((eq match nil) (setq matcher t))
10291 (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
10295 (when (eq scope 'tree)
10296 (org-back-to-heading t)
10297 (org-narrow-to-subtree)
10302 (org-prepare-agenda-buffers
10303 (list (buffer-file-name (current-buffer))))
10304 (setq res (org-scan-tags func matcher)))
10305 ;; Get the right scope
10307 ((and scope (listp scope) (symbolp (car scope)))
10308 (setq scope (eval scope)))
10309 ((eq scope 'agenda)
10310 (setq scope (org-agenda-files t)))
10311 ((eq scope 'agenda-with-archives)
10312 (setq scope (org-agenda-files t))
10313 (setq scope (org-add-archive-files scope)))
10315 (setq scope (list (buffer-file-name))))
10316 ((eq scope 'file-with-archives)
10317 (setq scope (org-add-archive-files (list (buffer-file-name))))))
10318 (org-prepare-agenda-buffers scope)
10319 (while (setq file (pop scope))
10320 (with-current-buffer (org-find-base-buffer-visiting file)
10324 (goto-char (point-min))
10325 (setq res (append res (org-scan-tags func matcher))))))))))
10330 ;;; Setting and retrieving properties
10332 (defconst org-special-properties
10333 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
10334 "TIMESTAMP" "TIMESTAMP_IA")
10335 "The special properties valid in Org-mode.
10337 These are properties that are not defined in the property drawer,
10338 but in some other way.")
10340 (defconst org-default-properties
10341 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
10342 "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
10343 "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
10344 "EXPORT_FILE_NAME" "EXPORT_TITLE")
10345 "Some properties that are used by Org-mode for various purposes.
10346 Being in this list makes sure that they are offered for completion.")
10348 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
10349 "Regular expression matching the first line of a property drawer.")
10351 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
10352 "Regular expression matching the first line of a property drawer.")
10354 (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
10355 "Regular expression matching the first line of a property drawer.")
10357 (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
10358 "Regular expression matching the first line of a property drawer.")
10360 (defconst org-property-drawer-re
10361 (concat "\\(" org-property-start-re "\\)[^\000]*\\("
10362 org-property-end-re "\\)\n?")
10363 "Matches an entire property drawer.")
10365 (defconst org-clock-drawer-re
10366 (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
10367 org-property-end-re "\\)\n?")
10368 "Matches an entire clock drawer.")
10370 (defun org-property-action ()
10371 "Do an action on properties."
10374 (org-at-property-p)
10375 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
10376 (setq c (read-char-exclusive))
10379 (call-interactively 'org-set-property))
10381 (call-interactively 'org-delete-property))
10383 (call-interactively 'org-delete-property-globally))
10385 (call-interactively 'org-compute-property-at-point))
10386 (t (error "No such property action %c" c)))))
10388 (defun org-at-property-p ()
10389 "Is the cursor in a property line?"
10390 ;; FIXME: Does not check if we are actually in the drawer.
10391 ;; FIXME: also returns true on any drawers.....
10392 ;; This is used by C-c C-c for property action.
10394 (beginning-of-line 1)
10395 (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
10397 (defun org-get-property-block (&optional beg end force)
10398 "Return the (beg . end) range of the body of the property drawer.
10399 BEG and END can be beginning and end of subtree, if not given
10400 they will be found.
10401 If the drawer does not exist and FORCE is non-nil, create the drawer."
10404 (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
10405 (end (or end (progn (outline-next-heading) (point)))))
10407 (if (re-search-forward org-property-start-re end t)
10408 (setq beg (1+ (match-end 0)))
10411 (org-insert-property-drawer)
10412 (setq end (progn (outline-next-heading) (point))))
10415 (if (re-search-forward org-property-start-re end t)
10416 (setq beg (1+ (match-end 0)))))
10417 (if (re-search-forward org-property-end-re end t)
10418 (setq end (match-beginning 0))
10419 (or force (throw 'exit nil))
10422 (org-indent-line-function)
10423 (insert ":END:\n"))
10426 (defun org-entry-properties (&optional pom which)
10427 "Get all properties of the entry at point-or-marker POM.
10428 This includes the TODO keyword, the tags, time strings for deadline,
10429 scheduled, and clocking, and any additional properties defined in the
10430 entry. The return value is an alist, keys may occur multiple times
10431 if the property key was used several times.
10432 POM may also be nil, in which case the current entry is used.
10433 If WHICH is nil or `all', get all properties. If WHICH is
10434 `special' or `standard', only get that subclass."
10435 (setq which (or which 'all))
10436 (org-with-point-at pom
10437 (let ((clockstr (substring org-clock-string 0 -1))
10438 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
10439 beg end range props sum-props key value string clocksum)
10441 (when (condition-case nil
10442 (and (org-mode-p) (org-back-to-heading t))
10445 (setq sum-props (get-text-property (point) 'org-summaries))
10446 (setq clocksum (get-text-property (point) :org-clock-minutes))
10447 (outline-next-heading)
10449 (when (memq which '(all special))
10450 ;; Get the special properties, like TODO and tags
10452 (when (and (looking-at org-todo-line-regexp) (match-end 2))
10453 (push (cons "TODO" (org-match-string-no-properties 2)) props))
10454 (when (looking-at org-priority-regexp)
10455 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
10456 (when (and (setq value (org-get-tags-string))
10457 (string-match "\\S-" value))
10458 (push (cons "TAGS" value) props))
10459 (when (setq value (org-get-tags-at))
10460 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
10462 (while (re-search-forward org-maybe-keyword-time-regexp end t)
10463 (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
10464 string (if (equal key clockstr)
10468 (match-beginning 3) (goto-char (point-at-eol)))))
10469 (substring (org-match-string-no-properties 3) 1 -1)))
10471 (if (= (char-after (match-beginning 3)) ?\[)
10472 (setq key "TIMESTAMP_IA")
10473 (setq key "TIMESTAMP")))
10474 (when (or (equal key clockstr) (not (assoc key props)))
10475 (push (cons key string) props)))
10479 (when (memq which '(all standard))
10480 ;; Get the standard properties, like :PORP: ...
10481 (setq range (org-get-property-block beg end))
10483 (goto-char (car range))
10484 (while (re-search-forward
10485 (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
10487 (setq key (org-match-string-no-properties 1)
10488 value (org-trim (or (org-match-string-no-properties 2) "")))
10489 (unless (member key excluded)
10490 (push (cons key (or value "")) props)))))
10492 (push (cons "CLOCKSUM"
10493 (org-columns-number-to-string (/ (float clocksum) 60.)
10496 (unless (assoc "CATEGORY" props)
10497 (setq value (or (org-get-category)
10498 (progn (org-refresh-category-properties)
10499 (org-get-category))))
10500 (push (cons "CATEGORY" value) props))
10501 (append sum-props (nreverse props)))))))
10503 (defun org-entry-get (pom property &optional inherit)
10504 "Get value of PROPERTY for entry at point-or-marker POM.
10505 If INHERIT is non-nil and the entry does not have the property,
10506 then also check higher levels of the hierarchy.
10507 If INHERIT is the symbol `selective', use inheritance only if the setting
10508 in `org-use-property-inheritance' selects PROPERTY for inheritance.
10509 If the property is present but empty, the return value is the empty string.
10510 If the property is not present at all, nil is returned."
10511 (org-with-point-at pom
10512 (if (and inherit (if (eq inherit 'selective)
10513 (org-property-inherit-p property)
10515 (org-entry-get-with-inheritance property)
10516 (if (member property org-special-properties)
10517 ;; We need a special property. Use brute force, get all properties.
10518 (cdr (assoc property (org-entry-properties nil 'special)))
10519 (let ((range (org-get-property-block)))
10521 (goto-char (car range))
10523 (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
10525 ;; Found the property, return it.
10527 (org-match-string-no-properties 1)
10530 (defun org-property-or-variable-value (var &optional inherit)
10531 "Check if there is a property fixing the value of VAR.
10532 If yes, return this value. If not, return the current value of the variable."
10533 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
10534 (if (and prop (stringp prop) (string-match "\\S-" prop))
10536 (symbol-value var))))
10538 (defun org-entry-delete (pom property)
10539 "Delete the property PROPERTY from entry at point-or-marker POM."
10540 (org-with-point-at pom
10541 (if (member property org-special-properties)
10542 nil ; cannot delete these properties.
10543 (let ((range (org-get-property-block)))
10545 (goto-char (car range))
10547 (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
10550 (delete-region (match-beginning 0) (1+ (point-at-eol)))
10554 ;; Multi-values properties are properties that contain multiple values
10555 ;; These values are assumed to be single words, separated by whitespace.
10556 (defun org-entry-add-to-multivalued-property (pom property value)
10557 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
10558 (let* ((old (org-entry-get pom property))
10559 (values (and old (org-split-string old "[ \t]"))))
10560 (setq value (org-entry-protect-space value))
10561 (unless (member value values)
10562 (setq values (cons value values))
10563 (org-entry-put pom property
10564 (mapconcat 'identity values " ")))))
10566 (defun org-entry-remove-from-multivalued-property (pom property value)
10567 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
10568 (let* ((old (org-entry-get pom property))
10569 (values (and old (org-split-string old "[ \t]"))))
10570 (setq value (org-entry-protect-space value))
10571 (when (member value values)
10572 (setq values (delete value values))
10573 (org-entry-put pom property
10574 (mapconcat 'identity values " ")))))
10576 (defun org-entry-member-in-multivalued-property (pom property value)
10577 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
10578 (let* ((old (org-entry-get pom property))
10579 (values (and old (org-split-string old "[ \t]"))))
10580 (setq value (org-entry-protect-space value))
10581 (member value values)))
10583 (defun org-entry-get-multivalued-property (pom property)
10584 "Return a list of values in a multivalued property."
10585 (let* ((value (org-entry-get pom property))
10586 (values (and value (org-split-string value "[ \t]"))))
10587 (mapcar 'org-entry-restore-space values)))
10589 (defun org-entry-put-multivalued-property (pom property &rest values)
10590 "Set multivalued PROPERTY at point-or-marker POM to VALUES.
10591 VALUES should be a list of strings. Spaces will be protected."
10592 (org-entry-put pom property
10593 (mapconcat 'org-entry-protect-space values " "))
10594 (let* ((value (org-entry-get pom property))
10595 (values (and value (org-split-string value "[ \t]"))))
10596 (mapcar 'org-entry-restore-space values)))
10598 (defun org-entry-protect-space (s)
10599 "Protect spaces and newline in string S."
10600 (while (string-match " " s)
10601 (setq s (replace-match "%20" t t s)))
10602 (while (string-match "\n" s)
10603 (setq s (replace-match "%0A" t t s)))
10606 (defun org-entry-restore-space (s)
10607 "Restore spaces and newline in string S."
10608 (while (string-match "%20" s)
10609 (setq s (replace-match " " t t s)))
10610 (while (string-match "%0A" s)
10611 (setq s (replace-match "\n" t t s)))
10614 (defvar org-entry-property-inherited-from (make-marker)
10615 "Marker pointing to the entry from where a property was inherited.
10616 Each call to `org-entry-get-with-inheritance' will set this marker to the
10617 location of the entry where the inheritance search matched. If there was
10618 no match, the marker will point nowhere.
10619 Note that also `org-entry-get' calls this function, if the INHERIT flag
10622 (defun org-entry-get-with-inheritance (property)
10623 "Get entry property, and search higher levels if not present."
10624 (move-marker org-entry-property-inherited-from nil)
10631 (when (setq tmp (org-entry-get nil property))
10632 (org-back-to-heading t)
10633 (move-marker org-entry-property-inherited-from (point))
10635 (or (org-up-heading-safe) (throw 'ex nil)))))
10637 (cdr (assoc property org-file-properties))
10638 (cdr (assoc property org-global-properties))
10639 (cdr (assoc property org-global-properties-fixed))))))
10641 (defun org-entry-put (pom property value)
10642 "Set PROPERTY to VALUE for entry at point-or-marker POM."
10643 (org-with-point-at pom
10644 (org-back-to-heading t)
10645 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
10648 ((equal property "TODO")
10649 (when (and (stringp value) (string-match "\\S-" value)
10650 (not (member value org-todo-keywords-1)))
10651 (error "\"%s\" is not a valid TODO state" value))
10652 (if (or (not value)
10653 (not (string-match "\\S-" value)))
10654 (setq value 'none))
10656 (org-set-tags nil 'align))
10657 ((equal property "PRIORITY")
10658 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
10659 (string-to-char value) ?\ ))
10660 (org-set-tags nil 'align))
10661 ((equal property "SCHEDULED")
10662 (if (re-search-forward org-scheduled-time-regexp end t)
10664 ((eq value 'earlier) (org-timestamp-change -1 'day))
10665 ((eq value 'later) (org-timestamp-change 1 'day))
10666 (t (call-interactively 'org-schedule)))
10667 (call-interactively 'org-schedule)))
10668 ((equal property "DEADLINE")
10669 (if (re-search-forward org-deadline-time-regexp end t)
10671 ((eq value 'earlier) (org-timestamp-change -1 'day))
10672 ((eq value 'later) (org-timestamp-change 1 'day))
10673 (t (call-interactively 'org-deadline)))
10674 (call-interactively 'org-deadline)))
10675 ((member property org-special-properties)
10676 (error "The %s property can not yet be set with `org-entry-put'"
10678 (t ; a non-special property
10679 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
10680 (setq range (org-get-property-block beg end 'force))
10681 (goto-char (car range))
10682 (if (re-search-forward
10683 (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
10685 (delete-region (match-beginning 1) (match-end 1))
10686 (goto-char (match-beginning 1)))
10687 (goto-char (cdr range))
10690 (org-indent-line-function)
10691 (insert ":" property ":"))
10692 (and value (insert " " value))
10693 (org-indent-line-function)))))))
10695 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
10696 "Get all property keys in the current buffer.
10697 With INCLUDE-SPECIALS, also list the special properties that reflect things
10698 like tags and TODO state.
10699 With INCLUDE-DEFAULTS, also include properties that has special meaning
10700 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
10701 With INCLUDE-COLUMNS, also include property names given in COLUMN
10702 formats in the current buffer."
10703 (let (rtn range cfmt s p)
10707 (goto-char (point-min))
10708 (while (re-search-forward org-property-start-re nil t)
10709 (setq range (org-get-property-block))
10710 (goto-char (car range))
10711 (while (re-search-forward
10712 (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
10714 (add-to-list 'rtn (org-match-string-no-properties 1)))
10715 (outline-next-heading))))
10717 (when include-specials
10718 (setq rtn (append org-special-properties rtn)))
10720 (when include-defaults
10721 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
10723 (when include-columns
10727 (goto-char (point-min))
10728 (while (re-search-forward
10729 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
10731 (setq cfmt (match-string 2) s 0)
10732 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
10734 (setq s (match-end 0)
10735 p (match-string 1 cfmt))
10736 (unless (or (equal p "ITEM")
10737 (member p org-special-properties))
10738 (add-to-list 'rtn (match-string 1 cfmt))))))))
10740 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
10742 (defun org-property-values (key)
10743 "Return a list of all values of property KEY."
10747 (goto-char (point-min))
10748 (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
10750 (while (re-search-forward re nil t)
10751 (add-to-list 'values (org-trim (match-string 1))))
10752 (delete "" values)))))
10754 (defun org-insert-property-drawer ()
10755 "Insert a property drawer into the current entry."
10757 (org-back-to-heading t)
10758 (looking-at outline-regexp)
10759 (let ((indent (- (match-end 0)(match-beginning 0)))
10761 (re (concat "^[ \t]*" org-keyword-time-regexp))
10763 (outline-next-heading)
10766 (while (re-search-forward re end t))
10767 (setq hiddenp (org-invisible-p))
10769 (and (equal (char-after) ?\n) (forward-char 1))
10770 (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
10771 (beginning-of-line 2))
10772 (org-skip-over-state-notes)
10773 (skip-chars-backward " \t\n\r")
10774 (if (eq (char-before) ?*) (forward-char 1))
10775 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
10776 (beginning-of-line 0)
10777 (org-indent-to-column indent)
10778 (beginning-of-line 2)
10779 (org-indent-to-column indent)
10780 (beginning-of-line 0)
10783 (org-back-to-heading t)
10785 (org-flag-drawer t))))
10787 (defun org-set-property (property value)
10788 "In the current entry, set PROPERTY to VALUE.
10789 When called interactively, this will prompt for a property name, offering
10790 completion on existing and default properties. And then it will prompt
10791 for a value, offering completion either on allowed values (via an inherited
10792 xxx_ALL property) or on existing values in other instances of this property
10793 in the current file."
10795 (let* ((completion-ignore-case t)
10796 (keys (org-buffer-property-keys nil t t))
10797 (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
10798 (prop (if (member prop0 keys)
10800 (or (cdr (assoc (downcase prop0)
10801 (mapcar (lambda (x) (cons (downcase x) x))
10804 (cur (org-entry-get nil prop))
10805 (allowed (org-property-get-allowed-values nil prop 'table))
10806 (existing (mapcar 'list (org-property-values prop)))
10808 (org-completing-read "Value: " allowed nil 'req-match)
10809 (org-completing-read
10810 (concat "Value" (if (and cur (string-match "\\S-" cur))
10811 (concat "[" cur "]") "")
10813 existing nil nil "" nil cur))))
10814 (list prop (if (equal val "") cur val))))
10815 (unless (equal (org-entry-get nil property) value)
10816 (org-entry-put nil property value)))
10818 (defun org-delete-property (property)
10819 "In the current entry, delete PROPERTY."
10821 (let* ((completion-ignore-case t)
10822 (prop (org-ido-completing-read
10823 "Property: " (org-entry-properties nil 'standard))))
10825 (message "Property %s %s" property
10826 (if (org-entry-delete nil property)
10828 "was not present in the entry")))
10830 (defun org-delete-property-globally (property)
10831 "Remove PROPERTY globally, from all entries."
10833 (let* ((completion-ignore-case t)
10834 (prop (org-ido-completing-read
10835 "Globally remove property: "
10836 (mapcar 'list (org-buffer-property-keys)))))
10841 (goto-char (point-min))
10843 (while (re-search-forward
10844 (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
10846 (setq cnt (1+ cnt))
10847 (replace-match ""))
10848 (message "Property \"%s\" removed from %d entries" property cnt)))))
10850 (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
10852 (defun org-compute-property-at-point ()
10853 "Compute the property at point.
10854 This looks for an enclosing column format, extracts the operator and
10855 then applies it to the property in the column format's scope."
10857 (unless (org-at-property-p)
10858 (error "Not at a property"))
10859 (let ((prop (org-match-string-no-properties 2)))
10860 (org-columns-get-format-and-top-level)
10861 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
10862 (error "No operator defined for property %s" prop))
10863 (org-columns-compute prop)))
10865 (defun org-property-get-allowed-values (pom property &optional table)
10866 "Get allowed values for the property PROPERTY.
10867 When TABLE is non-nil, return an alist that can directly be used for
10871 ((equal property "TODO")
10872 (setq vals (org-with-point-at pom
10873 (append org-todo-keywords-1 '("")))))
10874 ((equal property "PRIORITY")
10875 (let ((n org-lowest-priority))
10876 (while (>= n org-highest-priority)
10877 (push (char-to-string n) vals)
10879 ((member property org-special-properties))
10881 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
10883 (when (and vals (string-match "\\S-" vals))
10884 (setq vals (car (read-from-string (concat "(" vals ")"))))
10885 (setq vals (mapcar (lambda (x)
10886 (cond ((stringp x) x)
10887 ((numberp x) (number-to-string x))
10888 ((symbolp x) (symbol-name x))
10891 (if table (mapcar 'list vals) vals)))
10893 (defun org-property-previous-allowed-value (&optional previous)
10894 "Switch to the next allowed value for this property."
10896 (org-property-next-allowed-value t))
10898 (defun org-property-next-allowed-value (&optional previous)
10899 "Switch to the next allowed value for this property."
10901 (unless (org-at-property-p)
10902 (error "Not at a property"))
10903 (let* ((key (match-string 2))
10904 (value (match-string 3))
10905 (allowed (or (org-property-get-allowed-values (point) key)
10906 (and (member value '("[ ]" "[-]" "[X]"))
10910 (error "Allowed values for this property have not been defined"))
10911 (if previous (setq allowed (reverse allowed)))
10912 (if (member value allowed)
10913 (setq nval (car (cdr (member value allowed)))))
10914 (setq nval (or nval (car allowed)))
10915 (if (equal nval value)
10916 (error "Only one allowed value for this property"))
10917 (org-at-property-p)
10918 (replace-match (concat " :" key ": " nval) t t)
10919 (org-indent-line-function)
10920 (beginning-of-line 1)
10921 (skip-chars-forward " \t")))
10923 (defun org-find-entry-with-id (ident)
10924 "Locate the entry that contains the ID property with exact value IDENT.
10925 IDENT can be a string, a symbol or a number, this function will search for
10926 the string representation of it.
10927 Return the position where this entry starts, or nil if there is no such entry."
10928 (interactive "sID: ")
10930 ((stringp ident) ident)
10931 ((symbol-name ident) (symbol-name ident))
10932 ((numberp ident) (number-to-string ident))
10933 (t (error "IDENT %s must be a string, symbol or number" ident))))
10934 (case-fold-search nil))
10938 (goto-char (point-min))
10939 (when (re-search-forward
10940 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
10942 (org-back-to-heading)
10947 (defvar org-last-changed-timestamp nil)
10948 (defvar org-last-inserted-timestamp nil
10949 "The last time stamp inserted with `org-insert-time-stamp'.")
10950 (defvar org-time-was-given) ; dynamically scoped parameter
10951 (defvar org-end-time-was-given) ; dynamically scoped parameter
10952 (defvar org-ts-what) ; dynamically scoped parameter
10954 (defun org-time-stamp (arg &optional inactive)
10955 "Prompt for a date/time and insert a time stamp.
10956 If the user specifies a time like HH:MM, or if this command is called
10957 with a prefix argument, the time stamp will contain date and time.
10958 Otherwise, only the date will be included. All parts of a date not
10959 specified by the user will be filled in from the current date/time.
10960 So if you press just return without typing anything, the time stamp
10961 will represent the current date/time. If there is already a timestamp
10962 at the cursor, it will be modified."
10966 ;; Default time is either today, or, when entering a range,
10967 ;; the range start.
10968 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
10970 (re-search-backward
10971 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
10972 (- (point) 20) t)))
10973 (apply 'encode-time (org-parse-time-string (match-string 1)))
10975 (default-input (and ts (org-get-compact-tod ts)))
10976 org-time-was-given org-end-time-was-given time)
10978 ((and (org-at-timestamp-p t)
10979 (memq last-command '(org-time-stamp org-time-stamp-inactive))
10980 (memq this-command '(org-time-stamp org-time-stamp-inactive)))
10982 (setq time (let ((this-command this-command))
10983 (org-read-date arg 'totime nil nil
10984 default-time default-input)))
10985 (org-insert-time-stamp time (or org-time-was-given arg) inactive))
10986 ((org-at-timestamp-p t)
10987 (setq time (let ((this-command this-command))
10988 (org-read-date arg 'totime nil nil default-time default-input)))
10989 (when (org-at-timestamp-p t) ; just to get the match data
10990 ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
10992 (setq org-last-changed-timestamp
10993 (org-insert-time-stamp
10994 time (or org-time-was-given arg)
10995 inactive nil nil (list org-end-time-was-given))))
10996 (message "Timestamp updated"))
10998 (setq time (let ((this-command this-command))
10999 (org-read-date arg 'totime nil nil default-time default-input)))
11000 (org-insert-time-stamp time (or org-time-was-given arg) inactive
11001 nil nil (list org-end-time-was-given))))))
11003 ;; FIXME: can we use this for something else, like computing time differences?
11004 (defun org-get-compact-tod (s)
11005 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
11006 (let* ((t1 (match-string 1 s))
11007 (h1 (string-to-number (match-string 2 s)))
11008 (m1 (string-to-number (match-string 3 s)))
11009 (t2 (and (match-end 4) (match-string 5 s)))
11010 (h2 (and t2 (string-to-number (match-string 6 s))))
11011 (m2 (and t2 (string-to-number (match-string 7 s))))
11015 (setq dh (- h2 h1) dm (- m2 m1))
11016 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
11017 (concat t1 "+" (number-to-string dh)
11018 (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
11020 (defun org-time-stamp-inactive (&optional arg)
11021 "Insert an inactive time stamp.
11022 An inactive time stamp is enclosed in square brackets instead of angle
11023 brackets. It is inactive in the sense that it does not trigger agenda entries,
11024 does not link to the calendar and cannot be changed with the S-cursor keys.
11025 So these are more for recording a certain time/date."
11027 (org-time-stamp arg 'inactive))
11029 (defvar org-date-ovl (org-make-overlay 1 1))
11030 (org-overlay-put org-date-ovl 'face 'org-warning)
11031 (org-detach-overlay org-date-ovl)
11033 (defvar org-ans1) ; dynamically scoped parameter
11034 (defvar org-ans2) ; dynamically scoped parameter
11036 (defvar org-plain-time-of-day-regexp) ; defined below
11038 (defvar org-overriding-default-time nil) ; dynamically scoped
11039 (defvar org-read-date-overlay nil)
11040 (defvar org-dcst nil) ; dynamically scoped
11042 (defun org-read-date (&optional with-time to-time from-string prompt
11043 default-time default-input)
11044 "Read a date, possibly a time, and make things smooth for the user.
11045 The prompt will suggest to enter an ISO date, but you can also enter anything
11046 which will at least partially be understood by `parse-time-string'.
11047 Unrecognized parts of the date will default to the current day, month, year,
11048 hour and minute. If this command is called to replace a timestamp at point,
11049 of to enter the second timestamp of a range, the default time is taken from the
11050 existing stamp. For example,
11051 3-2-5 --> 2003-02-05
11052 feb 15 --> currentyear-02-15
11053 sep 12 9 --> 2009-09-12
11054 12:45 --> today 12:45
11055 22 sept 0:34 --> currentyear-09-22 0:34
11056 12 --> currentyear-currentmonth-12
11057 Fri --> nearest Friday (today or later)
11060 Furthermore you can specify a relative date by giving, as the *first* thing
11061 in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
11062 change in days weeks, months, years.
11063 With a single plus or minus, the date is relative to today. With a double
11064 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
11065 +4d --> four days from today
11066 +4 --> same as above
11067 +2w --> two weeks from today
11068 ++5 --> five days from default date
11070 The function understands only English month and weekday abbreviations,
11071 but this can be configured with the variables `parse-time-months' and
11072 `parse-time-weekdays'.
11074 While prompting, a calendar is popped up - you can also select the
11075 date with the mouse (button 1). The calendar shows a period of three
11076 months. To scroll it to other months, use the keys `>' and `<'.
11077 If you don't like the calendar, turn it off with
11078 \(setq org-read-date-popup-calendar nil)
11080 With optional argument TO-TIME, the date will immediately be converted
11081 to an internal time.
11082 With an optional argument WITH-TIME, the prompt will suggest to also
11083 insert a time. Note that when WITH-TIME is not set, you can still
11084 enter a time, and this function will inform the calling routine about
11085 this change. The calling routine may then choose to change the format
11086 used to insert the time stamp into the buffer to include the time.
11087 With optional argument FROM-STRING, read from this string instead from
11088 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
11089 the time/date that is used for everything that is not specified by the
11091 (require 'parse-time)
11092 (let* ((org-time-stamp-rounding-minutes
11093 (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
11094 (org-dcst org-display-custom-times)
11095 (ct (org-current-time))
11096 (def (or org-overriding-default-time default-time ct))
11097 (defdecode (decode-time def))
11099 (when (< (nth 2 defdecode) org-extend-today-until)
11100 (setcar (nthcdr 2 defdecode) -1)
11101 (setcar (nthcdr 1 defdecode) 59)
11102 (setq def (apply 'encode-time defdecode)
11103 defdecode (decode-time def)))))
11104 (calendar-move-hook nil)
11105 (calendar-view-diary-initially-flag nil)
11106 (view-diary-entries-initially nil)
11107 (calendar-view-holidays-initially-flag nil)
11108 (view-calendar-holidays-initially nil)
11109 (timestr (format-time-string
11110 (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
11111 (prompt (concat (if prompt (concat prompt " ") "")
11112 (format "Date+time [%s]: " timestr)))
11113 ans (org-ans0 "") org-ans1 org-ans2 final)
11116 (from-string (setq ans from-string))
11117 (org-read-date-popup-calendar
11119 (save-window-excursion
11121 (calendar-forward-day (- (time-to-days def)
11122 (calendar-absolute-from-gregorian
11123 (calendar-current-date))))
11124 (org-eval-in-calendar nil t)
11125 (let* ((old-map (current-local-map))
11126 (map (copy-keymap calendar-mode-map))
11127 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
11128 (org-defkey map (kbd "RET") 'org-calendar-select)
11129 (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
11130 'org-calendar-select-mouse)
11131 (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2
])
11132 'org-calendar-select-mouse
)
11133 (org-defkey minibuffer-local-map
[(meta shift left
)]
11134 (lambda () (interactive)
11135 (org-eval-in-calendar '(calendar-backward-month 1))))
11136 (org-defkey minibuffer-local-map
[(meta shift right
)]
11137 (lambda () (interactive)
11138 (org-eval-in-calendar '(calendar-forward-month 1))))
11139 (org-defkey minibuffer-local-map
[(meta shift up
)]
11140 (lambda () (interactive)
11141 (org-eval-in-calendar '(calendar-backward-year 1))))
11142 (org-defkey minibuffer-local-map
[(meta shift down
)]
11143 (lambda () (interactive)
11144 (org-eval-in-calendar '(calendar-forward-year 1))))
11145 (org-defkey minibuffer-local-map
[(shift up
)]
11146 (lambda () (interactive)
11147 (org-eval-in-calendar '(calendar-backward-week 1))))
11148 (org-defkey minibuffer-local-map
[(shift down
)]
11149 (lambda () (interactive)
11150 (org-eval-in-calendar '(calendar-forward-week 1))))
11151 (org-defkey minibuffer-local-map
[(shift left
)]
11152 (lambda () (interactive)
11153 (org-eval-in-calendar '(calendar-backward-day 1))))
11154 (org-defkey minibuffer-local-map
[(shift right
)]
11155 (lambda () (interactive)
11156 (org-eval-in-calendar '(calendar-forward-day 1))))
11157 (org-defkey minibuffer-local-map
">"
11158 (lambda () (interactive)
11159 (org-eval-in-calendar '(scroll-calendar-left 1))))
11160 (org-defkey minibuffer-local-map
"<"
11161 (lambda () (interactive)
11162 (org-eval-in-calendar '(scroll-calendar-right 1))))
11165 (use-local-map map
)
11166 (add-hook 'post-command-hook
'org-read-date-display
)
11167 (setq org-ans0
(read-string prompt default-input nil nil
))
11168 ;; org-ans0: from prompt
11169 ;; org-ans1: from mouse click
11170 ;; org-ans2: from calendar motion
11171 (setq ans
(concat org-ans0
" " (or org-ans1 org-ans2
))))
11172 (remove-hook 'post-command-hook
'org-read-date-display
)
11173 (use-local-map old-map
)
11174 (when org-read-date-overlay
11175 (org-delete-overlay org-read-date-overlay
)
11176 (setq org-read-date-overlay nil
)))))))
11178 (t ; Naked prompt only
11180 (setq ans
(read-string prompt default-input nil timestr
))
11181 (when org-read-date-overlay
11182 (org-delete-overlay org-read-date-overlay
)
11183 (setq org-read-date-overlay nil
)))))
11185 (setq final
(org-read-date-analyze ans def defdecode
))
11188 (apply 'encode-time final
)
11189 (if (and (boundp 'org-time-was-given
) org-time-was-given
)
11190 (format "%04d-%02d-%02d %02d:%02d"
11191 (nth 5 final
) (nth 4 final
) (nth 3 final
)
11192 (nth 2 final
) (nth 1 final
))
11193 (format "%04d-%02d-%02d" (nth 5 final
) (nth 4 final
) (nth 3 final
))))))
11197 (defun org-read-date-display ()
11198 "Display the current date prompt interpretation in the minibuffer."
11199 (when org-read-date-display-live
11200 (when org-read-date-overlay
11201 (org-delete-overlay org-read-date-overlay
))
11204 (while (not (equal (buffer-substring
11205 (max (point-min) (- (point) 4)) (point))
11209 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
11210 " " (or org-ans1 org-ans2
)))
11211 (org-end-time-was-given nil
)
11212 (f (org-read-date-analyze ans def defdecode
))
11214 org-time-stamp-custom-formats
11215 org-time-stamp-formats
))
11216 (fmt (if (or with-time
11217 (and (boundp 'org-time-was-given
) org-time-was-given
))
11220 (txt (concat "=> " (format-time-string fmt
(apply 'encode-time f
)))))
11221 (when (and org-end-time-was-given
11222 (string-match org-plain-time-of-day-regexp txt
))
11223 (setq txt
(concat (substring txt
0 (match-end 0)) "-"
11224 org-end-time-was-given
11225 (substring txt
(match-end 0)))))
11226 (setq org-read-date-overlay
11227 (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
11228 (org-overlay-display org-read-date-overlay txt
'secondary-selection
))))
11230 (defun org-read-date-analyze (ans def defdecode
)
11231 "Analyse the combined answer of the date prompt."
11232 ;; FIXME: cleanup and comment
11233 (let (delta deltan deltaw deltadef year month day
11234 hour minute second wday pm h2 m2 tl wday1
11235 iso-year iso-weekday iso-week iso-year iso-date
)
11237 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans
)
11240 (when (setq delta
(org-read-date-get-relative ans
(current-time) def
))
11241 (setq ans
(replace-match "" t t ans
)
11243 deltaw
(nth 1 delta
)
11244 deltadef
(nth 2 delta
)))
11246 ;; Check if there is an iso week date in there
11247 ;; If yes, sore the info and postpone interpreting it until the rest
11248 ;; of the parsing is done
11249 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans
)
11250 (setq iso-year
(if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans
))))
11251 iso-weekday
(if (match-end 3) (string-to-number (match-string 3 ans
)))
11252 iso-week
(string-to-number (match-string 2 ans
)))
11253 (setq ans
(replace-match "" t t ans
)))
11255 ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
11256 (when (string-match
11257 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans
)
11258 (setq year
(if (match-end 2)
11259 (string-to-number (match-string 2 ans
))
11260 (string-to-number (format-time-string "%Y")))
11261 month
(string-to-number (match-string 3 ans
))
11262 day
(string-to-number (match-string 4 ans
)))
11263 (if (< year
100) (setq year
(+ 2000 year
)))
11264 (setq ans
(replace-match (format "%04d-%02d-%02d\\5" year month day
)
11266 ;; Help matching am/pm times, because `parse-time-string' does not do that.
11267 ;; If there is a time with am/pm, and *no* time without it, we convert
11268 ;; so that matching will be successful.
11269 (loop for i from
1 to
2 do
; twice, for end time as well
11270 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans
))
11271 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans
))
11272 (setq hour
(string-to-number (match-string 1 ans
))
11273 minute
(if (match-end 3)
11274 (string-to-number (match-string 3 ans
))
11277 (string-to-char (downcase (match-string 4 ans
)))))
11278 (if (and (= hour
12) (not pm
))
11280 (if (and pm
(< hour
12)) (setq hour
(+ 12 hour
))))
11281 (setq ans
(replace-match (format "%02d:%02d" hour minute
)
11284 ;; Check if a time range is given as a duration
11285 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans
)
11286 (setq hour
(string-to-number (match-string 1 ans
))
11287 h2
(+ hour
(string-to-number (match-string 3 ans
)))
11288 minute
(string-to-number (match-string 2 ans
))
11289 m2
(+ minute
(if (match-end 5) (string-to-number
11290 (match-string 5 ans
))0)))
11291 (if (>= m2
60) (setq h2
(1+ h2
) m2
(- m2
60)))
11292 (setq ans
(replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2
)
11295 ;; Check if there is a time range
11296 (when (boundp 'org-end-time-was-given
)
11297 (setq org-time-was-given nil
)
11298 (when (and (string-match org-plain-time-of-day-regexp ans
)
11300 (setq org-end-time-was-given
(match-string 8 ans
))
11301 (setq ans
(concat (substring ans
0 (match-beginning 7))
11302 (substring ans
(match-end 7))))))
11304 (setq tl
(parse-time-string ans
)
11305 day
(or (nth 3 tl
) (nth 3 defdecode
))
11306 month
(or (nth 4 tl
)
11307 (if (and org-read-date-prefer-future
11308 (nth 3 tl
) (< (nth 3 tl
) (nth 3 defdecode
)))
11309 (1+ (nth 4 defdecode
))
11310 (nth 4 defdecode
)))
11311 year
(or (nth 5 tl
)
11312 (if (and org-read-date-prefer-future
11313 (nth 4 tl
) (< (nth 4 tl
) (nth 4 defdecode
)))
11314 (1+ (nth 5 defdecode
))
11315 (nth 5 defdecode
)))
11316 hour
(or (nth 2 tl
) (nth 2 defdecode
))
11317 minute
(or (nth 1 tl
) (nth 1 defdecode
))
11318 second
(or (nth 0 tl
) 0)
11321 ;; Special date definitions below
11324 ;; There was an iso week
11325 (setq year
(or iso-year year
)
11326 day
(or iso-weekday wday
1)
11327 wday nil
; to make sure that the trigger below does not match
11328 iso-date
(calendar-gregorian-from-absolute
11329 (calendar-absolute-from-iso
11330 (list iso-week day year
))))
11331 ; FIXME: Should we also push ISO weeks into the future?
11332 ; (when (and org-read-date-prefer-future
11334 ; (< (calendar-absolute-from-gregorian iso-date)
11335 ; (time-to-days (current-time))))
11336 ; (setq year (1+ year)
11337 ; iso-date (calendar-gregorian-from-absolute
11338 ; (calendar-absolute-from-iso
11339 ; (list iso-week day year)))))
11340 (setq month
(car iso-date
)
11341 year
(nth 2 iso-date
)
11342 day
(nth 1 iso-date
)))
11345 (let ((now (decode-time (current-time))))
11346 (setq day
(nth 3 now
) month
(nth 4 now
) year
(nth 5 now
))))
11347 (cond ((member deltaw
'("d" "")) (setq day
(+ day deltan
)))
11348 ((equal deltaw
"w") (setq day
(+ day
(* 7 deltan
))))
11349 ((equal deltaw
"m") (setq month
(+ month deltan
)))
11350 ((equal deltaw
"y") (setq year
(+ year deltan
)))))
11351 ((and wday
(not (nth 3 tl
)))
11352 ;; Weekday was given, but no day, so pick that day in the week
11353 ;; on or after the derived date.
11354 (setq wday1
(nth 6 (decode-time (encode-time 0 0 0 day month year
))))
11355 (unless (equal wday wday1
)
11356 (setq day
(+ day
(%
(- wday wday1 -
7) 7))))))
11357 (if (and (boundp 'org-time-was-given
)
11359 (setq org-time-was-given t
))
11360 (if (< year
100) (setq year
(+ 2000 year
)))
11361 (if (< year
1970) (setq year
(nth 5 defdecode
))) ; not representable
11362 (list second minute hour day month year
)))
11364 (defvar parse-time-weekdays
)
11366 (defun org-read-date-get-relative (s today default
)
11367 "Check string S for special relative date string.
11368 TODAY and DEFAULT are internal times, for today and for a default.
11369 Return shift list (N what def-flag)
11370 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
11371 N is the number of WHATs to shift.
11372 DEF-FLAG is t when a double ++ or -- indicates shift relative to
11373 the DEFAULT date rather than TODAY."
11377 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
11379 "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays
"\\|") "\\)\\)?"
11380 "\\([ \t]\\|$\\)") s
)
11381 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
11382 (let* ((dir (if (> (match-end 1) (match-beginning 1))
11383 (string-to-char (substring (match-string 1 s
) -
1))
11385 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
11386 (n (if (match-end 2) (string-to-number (match-string 2 s
)) 1))
11387 (what (if (match-end 3) (match-string 3 s
) "d"))
11388 (wday1 (cdr (assoc (downcase what
) parse-time-weekdays
)))
11389 (date (if rel default today
))
11390 (wday (nth 6 (decode-time date
)))
11394 (setq delta
(mod (+ 7 (- wday1 wday
)) 7))
11395 (if (= dir ?-
) (setq delta
(- delta
7)))
11396 (if (> n
1) (setq delta
(+ delta
(* (1- n
) (if (= dir ?-
) -
7 7)))))
11397 (list delta
"d" rel
))
11398 (list (* n
(if (= dir ?-
) -
1 1)) what rel
)))))
11400 (defun org-eval-in-calendar (form &optional keepdate
)
11401 "Eval FORM in the calendar window and return to current window.
11402 Also, store the cursor date in variable org-ans2."
11403 (let ((sw (selected-window)))
11404 (select-window (get-buffer-window "*Calendar*"))
11406 (when (and (not keepdate
) (calendar-cursor-to-date))
11407 (let* ((date (calendar-cursor-to-date))
11408 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
11409 (setq org-ans2
(format-time-string "%Y-%m-%d" time
))))
11410 (org-move-overlay org-date-ovl
(1- (point)) (1+ (point)) (current-buffer))
11411 (select-window sw
)))
11413 (defun org-calendar-select ()
11414 "Return to `org-read-date' with the date currently selected.
11415 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
11417 (when (calendar-cursor-to-date)
11418 (let* ((date (calendar-cursor-to-date))
11419 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
11420 (setq org-ans1
(format-time-string "%Y-%m-%d" time
)))
11421 (if (active-minibuffer-window) (exit-minibuffer))))
11423 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra
)
11424 "Insert a date stamp for the date given by the internal TIME.
11425 WITH-HM means, use the stamp format that includes the time of the day.
11426 INACTIVE means use square brackets instead of angular ones, so that the
11427 stamp will not contribute to the agenda.
11428 PRE and POST are optional strings to be inserted before and after the
11430 The command returns the inserted time stamp."
11431 (let ((fmt (funcall (if with-hm
'cdr
'car
) org-time-stamp-formats
))
11433 (if inactive
(setq fmt
(concat "[" (substring fmt
1 -
1) "]")))
11434 (insert-before-markers (or pre
""))
11435 (insert-before-markers (setq stamp
(format-time-string fmt time
)))
11436 (when (listp extra
)
11437 (setq extra
(car extra
))
11438 (if (and (stringp extra
)
11439 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra
))
11440 (setq extra
(format "-%02d:%02d"
11441 (string-to-number (match-string 1 extra
))
11442 (string-to-number (match-string 2 extra
))))
11446 (insert-before-markers extra
)
11448 (insert-before-markers (or post
""))
11449 (setq org-last-inserted-timestamp stamp
)))
11451 (defun org-toggle-time-stamp-overlays ()
11452 "Toggle the use of custom time stamp formats."
11454 (setq org-display-custom-times
(not org-display-custom-times
))
11455 (unless org-display-custom-times
11456 (let ((p (point-min)) (bmp (buffer-modified-p)))
11457 (while (setq p
(next-single-property-change p
'display
))
11458 (if (and (get-text-property p
'display
)
11459 (eq (get-text-property p
'face
) 'org-date
))
11460 (remove-text-properties
11461 p
(setq p
(next-single-property-change p
'display
))
11463 (set-buffer-modified-p bmp
)))
11464 (if (featurep 'xemacs
)
11465 (remove-text-properties (point-min) (point-max) '(end-glyph t
)))
11466 (org-restart-font-lock)
11467 (setq org-table-may-need-update t
)
11468 (if org-display-custom-times
11469 (message "Time stamps are overlayed with custom format")
11470 (message "Time stamp overlays removed")))
11472 (defun org-display-custom-time (beg end
)
11473 "Overlay modified time stamp format over timestamp between BEG and END."
11474 (let* ((ts (buffer-substring beg end
))
11475 t1 w1 with-hm tf time str w2
(off 0))
11477 (setq t1
(org-parse-time-string ts t
))
11478 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts
)
11479 (setq off
(- (match-end 0) (match-beginning 0)))))
11480 (setq end
(- end off
))
11481 (setq w1
(- end beg
)
11482 with-hm
(and (nth 1 t1
) (nth 2 t1
))
11483 tf
(funcall (if with-hm
'cdr
'car
) org-time-stamp-custom-formats
)
11484 time
(org-fix-decoded-time t1
)
11486 (format-time-string
11487 (substring tf
1 -
1) (apply 'encode-time time
))
11488 nil
'mouse-face
'highlight
)
11490 (if (not (= w2 w1
))
11491 (add-text-properties (1+ beg
) (+ 2 beg
)
11492 (list 'org-dwidth t
'org-dwidth-n
(- w1 w2
))))
11493 (if (featurep 'xemacs
)
11495 (put-text-property beg end
'invisible t
)
11496 (put-text-property beg end
'end-glyph
(make-glyph str
)))
11497 (put-text-property beg end
'display str
))))
11499 (defun org-translate-time (string)
11500 "Translate all timestamps in STRING to custom format.
11501 But do this only if the variable `org-display-custom-times' is set."
11502 (when org-display-custom-times
11505 (re org-ts-regexp-both
)
11506 t1 with-hm inactive tf time str beg end
)
11507 (while (setq start
(string-match re string start
))
11508 (setq beg
(match-beginning 0)
11510 t1
(save-match-data
11511 (org-parse-time-string (substring string beg end
) t
))
11512 with-hm
(and (nth 1 t1
) (nth 2 t1
))
11513 inactive
(equal (substring string beg
(1+ beg
)) "[")
11514 tf
(funcall (if with-hm
'cdr
'car
)
11515 org-time-stamp-custom-formats
)
11516 time
(org-fix-decoded-time t1
)
11517 str
(format-time-string
11519 (if inactive
"[" "<") (substring tf
1 -
1)
11520 (if inactive
"]" ">"))
11521 (apply 'encode-time time
))
11522 string
(replace-match str t t string
)
11523 start
(+ start
(length str
)))))))
11526 (defun org-fix-decoded-time (time)
11527 "Set 0 instead of nil for the first 6 elements of time.
11528 Don't touch the rest."
11530 (mapcar (lambda (x) (if (< (setq n
(1+ n
)) 7) (or x
0) x
)) time
)))
11532 (defun org-days-to-time (timestamp-string)
11533 "Difference between TIMESTAMP-STRING and now in days."
11534 (- (time-to-days (org-time-string-to-time timestamp-string
))
11535 (time-to-days (current-time))))
11537 (defun org-deadline-close (timestamp-string &optional ndays
)
11538 "Is the time in TIMESTAMP-STRING close to the current date?"
11539 (setq ndays
(or ndays
(org-get-wdays timestamp-string
)))
11540 (and (< (org-days-to-time timestamp-string
) ndays
)
11541 (not (org-entry-is-done-p))))
11543 (defun org-get-wdays (ts)
11544 "Get the deadline lead time appropriate for timestring TS."
11546 ((<= org-deadline-warning-days
0)
11547 ;; 0 or negative, enforce this value no matter what
11548 (- org-deadline-warning-days
))
11549 ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts
)
11550 ;; lead time is specified.
11551 (floor (* (string-to-number (match-string 1 ts
))
11552 (cdr (assoc (match-string 2 ts
)
11553 '(("d" .
1) ("w" .
7)
11554 ("m" .
30.4) ("y" .
365.25)))))))
11555 ;; go for the default.
11556 (t org-deadline-warning-days
)))
11558 (defun org-calendar-select-mouse (ev)
11559 "Return to `org-read-date' with the date currently selected.
11560 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
11562 (mouse-set-point ev
)
11563 (when (calendar-cursor-to-date)
11564 (let* ((date (calendar-cursor-to-date))
11565 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
11566 (setq org-ans1
(format-time-string "%Y-%m-%d" time
)))
11567 (if (active-minibuffer-window) (exit-minibuffer))))
11569 (defun org-check-deadlines (ndays)
11570 "Check if there are any deadlines due or past due.
11571 A deadline is considered due if it happens within `org-deadline-warning-days'
11572 days from today's date. If the deadline appears in an entry marked DONE,
11573 it is not shown. The prefix arg NDAYS can be used to test that many
11574 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
11576 (let* ((org-warn-days
11578 ((equal ndays
'(4)) 100000)
11579 (ndays (prefix-numeric-value ndays
))
11580 (t (abs org-deadline-warning-days
))))
11581 (case-fold-search nil
)
11582 (regexp (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>"))
11584 (lambda () (org-deadline-close (match-string 1) org-warn-days
))))
11586 (message "%d deadlines past-due or due within %d days"
11587 (org-occur regexp nil callback
)
11590 (defun org-check-before-date (date)
11591 "Check if there are deadlines or scheduled entries before DATE."
11592 (interactive (list (org-read-date)))
11593 (let ((case-fold-search nil
)
11594 (regexp (concat "\\<\\(" org-deadline-string
11595 "\\|" org-scheduled-string
11596 "\\) *<\\([^>]+\\)>"))
11598 (lambda () (time-less-p
11599 (org-time-string-to-time (match-string 2))
11600 (org-time-string-to-time date
)))))
11601 (message "%d entries before %s"
11602 (org-occur regexp nil callback
) date
)))
11604 (defun org-evaluate-time-range (&optional to-buffer
)
11605 "Evaluate a time range by computing the difference between start and end.
11606 Normally the result is just printed in the echo area, but with prefix arg
11607 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
11608 If the time range is actually in a table, the result is inserted into the
11610 For time difference computation, a year is assumed to be exactly 365
11611 days in order to avoid rounding problems."
11614 (org-clock-update-time-maybe)
11616 (unless (org-at-date-range-p t
)
11617 (goto-char (point-at-bol))
11618 (re-search-forward org-tr-regexp-both
(point-at-eol) t
))
11619 (if (not (org-at-date-range-p t
))
11620 (error "Not at a time-stamp range, and none found in current line")))
11621 (let* ((ts1 (match-string 1))
11622 (ts2 (match-string 2))
11623 (havetime (or (> (length ts1
) 15) (> (length ts2
) 15)))
11624 (match-end (match-end 0))
11625 (time1 (org-time-string-to-time ts1
))
11626 (time2 (org-time-string-to-time ts2
))
11627 (t1 (time-to-seconds time1
))
11628 (t2 (time-to-seconds time2
))
11629 (diff (abs (- t2 t1
)))
11630 (negative (< (- t2 t1
) 0))
11631 ;; (ys (floor (* 365 24 60 60)))
11634 (fy "%dy %dd %02d:%02d")
11636 (fd "%dd %02d:%02d")
11641 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
11643 d
(floor (/ diff ds
)) diff
(mod diff ds
)
11644 h
(floor (/ diff hs
)) diff
(mod diff hs
)
11645 m
(floor (/ diff
60)))
11646 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
11648 d
(floor (+ (/ diff ds
) 0.5))
11650 (if (not to-buffer
)
11651 (message "%s" (org-make-tdiff-string y d h m
))
11652 (if (org-at-table-p)
11654 (goto-char match-end
)
11656 (and (looking-at " *|") (goto-char (match-end 0))))
11657 (goto-char match-end
))
11659 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
11660 (replace-match ""))
11661 (if negative
(insert " -"))
11662 (if (> y
0) (insert " " (format (if havetime fy fy1
) y d h m
))
11663 (if (> d
0) (insert " " (format (if havetime fd fd1
) d h m
))
11664 (insert " " (format fh h m
))))
11665 (if align
(org-table-align))
11666 (message "Time difference inserted")))))
11668 (defun org-make-tdiff-string (y d h m
)
11671 (if (> y
0) (setq fmt
(concat fmt
"%d year" (if (> y
1) "s" "") " ")
11673 (if (> d
0) (setq fmt
(concat fmt
"%d day" (if (> d
1) "s" "") " ")
11675 (if (> h
0) (setq fmt
(concat fmt
"%d hour" (if (> h
1) "s" "") " ")
11677 (if (> m
0) (setq fmt
(concat fmt
"%d minute" (if (> m
1) "s" "") " ")
11679 (apply 'format fmt
(nreverse l
))))
11681 (defun org-time-string-to-time (s)
11682 (apply 'encode-time
(org-parse-time-string s
)))
11684 (defun org-time-string-to-absolute (s &optional daynr prefer show-all
)
11685 "Convert a time stamp to an absolute day number.
11686 If there is a specifyer for a cyclic time stamp, get the closest date to
11688 PREFER and SHOW-ALL are passed through to `org-closest-date'."
11690 ((and daynr
(string-match "\\`%%\\((.*)\\)" s
))
11691 (if (org-diary-sexp-entry (match-string 1 s
) "" date
)
11694 ((and daynr
(string-match "\\+[0-9]+[dwmy]" s
))
11695 (org-closest-date s
(if (and (boundp 'daynr
) (integerp daynr
)) daynr
11696 (time-to-days (current-time))) (match-string 0 s
)
11698 (t (time-to-days (apply 'encode-time
(org-parse-time-string s
))))))
11700 (defun org-days-to-iso-week (days)
11701 "Return the iso week number."
11703 (car (calendar-iso-from-absolute days
)))
11705 (defun org-small-year-to-year (year)
11706 "Convert 2-digit years into 4-digit years.
11707 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
11708 The year 2000 cannot be abbreviated. Any year larger than 99
11709 is returned unchanged."
11711 (setq year
(+ 2000 year
))
11713 (setq year
(+ 1900 year
))))
11716 (defun org-time-from-absolute (d)
11717 "Return the time corresponding to date D.
11718 D may be an absolute day number, or a calendar-type list (month day year)."
11719 (if (numberp d
) (setq d
(calendar-gregorian-from-absolute d
)))
11720 (encode-time 0 0 0 (nth 1 d
) (car d
) (nth 2 d
)))
11722 (defun org-calendar-holiday ()
11723 "List of holidays, for Diary display in Org-mode."
11724 (require 'holidays
)
11726 (if (fboundp 'calendar-check-holidays
)
11727 'calendar-check-holidays
'check-calendar-holidays
) date
)))
11728 (if hl
(mapconcat 'identity hl
"; "))))
11730 (defun org-diary-sexp-entry (sexp entry date
)
11731 "Process a SEXP diary ENTRY for DATE."
11732 (require 'diary-lib
)
11733 (let ((result (if calendar-debug-sexp
11734 (let ((stack-trace-on-error t
))
11735 (eval (car (read-from-string sexp
))))
11736 (condition-case nil
11737 (eval (car (read-from-string sexp
)))
11740 (message "Bad sexp at line %d in %s: %s"
11742 (buffer-file-name) sexp
)
11744 (cond ((stringp result
) result
)
11745 ((and (consp result
)
11746 (stringp (cdr result
))) (cdr result
))
11750 (defun org-diary-to-ical-string (frombuf)
11751 "Get iCalendar entries from diary entries in buffer FROMBUF.
11752 This uses the icalendar.el library."
11753 (let* ((tmpdir (if (featurep 'xemacs
)
11755 temporary-file-directory
))
11756 (tmpfile (make-temp-name
11757 (expand-file-name "orgics" tmpdir
)))
11760 (set-buffer frombuf
)
11761 (icalendar-export-region (point-min) (point-max) tmpfile
)
11762 (setq buf
(find-buffer-visiting tmpfile
))
11764 (goto-char (point-min))
11765 (if (re-search-forward "^BEGIN:VEVENT" nil t
)
11766 (setq b
(match-beginning 0)))
11767 (goto-char (point-max))
11768 (if (re-search-backward "^END:VEVENT" nil t
)
11769 (setq e
(match-end 0)))
11770 (setq rtn
(if (and b e
) (concat (buffer-substring b e
) "\n") "")))
11772 (delete-file tmpfile
)
11775 (defun org-closest-date (start current change prefer show-all
)
11776 "Find the date closest to CURRENT that is consistent with START and CHANGE.
11777 When PREFER is `past' return a date that is either CURRENT or past.
11778 When PREFER is `future', return a date that is either CURRENT or future.
11779 When SHOW-ALL is nil, only return the current occurrence of a time stamp."
11780 ;; Make the proper lists from the dates
11782 (let ((a1 '(("d" . day
) ("w" . week
) ("m" . month
) ("y" . year
)))
11783 dn dw sday cday n1 n2 n0
11784 d m y y1 y2 date1 date2 nmonths nm ny m2
)
11786 (setq start
(org-date-to-gregorian start
)
11787 current
(org-date-to-gregorian
11790 (time-to-days (current-time))))
11791 sday
(calendar-absolute-from-gregorian start
)
11792 cday
(calendar-absolute-from-gregorian current
))
11794 (if (<= cday sday
) (throw 'exit sday
))
11796 (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change
)
11797 (setq dn
(string-to-number (match-string 1 change
))
11798 dw
(cdr (assoc (match-string 2 change
) a1
)))
11799 (error "Invalid change specifyer: %s" change
))
11800 (if (eq dw
'week
) (setq dw
'day dn
(* 7 dn
)))
11803 (setq n1
(+ sday
(* dn
(floor (/ (- cday sday
) dn
))))
11806 (setq d
(nth 1 start
) m
(car start
) y1
(nth 2 start
) y2
(nth 2 current
))
11807 (setq y1
(+ (* (floor (/ (- y2 y1
) dn
)) dn
) y1
))
11808 (setq date1
(list m d y1
)
11809 n1
(calendar-absolute-from-gregorian date1
)
11810 date2
(list m d
(+ y1
(* (if (< n1 cday
) 1 -
1) dn
)))
11811 n2
(calendar-absolute-from-gregorian date2
)))
11813 ;; approx number of month between the two dates
11814 (setq nmonths
(floor (/ (- cday sday
) 30.436875)))
11815 ;; How often does dn fit in there?
11816 (setq d
(nth 1 start
) m
(car start
) y
(nth 2 start
)
11817 nm
(* dn
(max 0 (1- (floor (/ nmonths dn
)))))
11819 ny
(floor (/ m
12))
11822 (while (> m
12) (setq m
(- m
12) y
(1+ y
)))
11823 (setq n1
(calendar-absolute-from-gregorian (list m d y
)))
11824 (setq m2
(+ m dn
) y2 y
)
11825 (if (> m2
12) (setq y2
(1+ y2
) m2
(- m2
12)))
11826 (setq n2
(calendar-absolute-from-gregorian (list m2 d y2
)))
11827 (while (<= n2 cday
)
11828 (setq n1 n2 m m2 y y2
)
11829 (setq m2
(+ m dn
) y2 y
)
11830 (if (> m2
12) (setq y2
(1+ y2
) m2
(- m2
12)))
11831 (setq n2
(calendar-absolute-from-gregorian (list m2 d y2
))))))
11832 ;; Make sure n1 is the earlier date
11833 (setq n0 n1 n1
(min n1 n2
) n2
(max n0 n2
))
11836 ((eq prefer
'past
) n1
)
11837 ((eq prefer
'future
) (if (= cday n1
) n1 n2
))
11838 (t (if (> (abs (- cday n1
)) (abs (- cday n2
))) n2 n1
)))
11840 ((eq prefer
'past
) n1
)
11841 ((eq prefer
'future
) (if (= cday n1
) n1 n2
))
11842 (t (if (= cday n1
) n1 n2
)))))))
11844 (defun org-date-to-gregorian (date)
11845 "Turn any specification of DATE into a gregorian date for the calendar."
11846 (cond ((integerp date
) (calendar-gregorian-from-absolute date
))
11847 ((and (listp date
) (= (length date
) 3)) date
)
11849 (setq date
(org-parse-time-string date
))
11850 (list (nth 4 date
) (nth 3 date
) (nth 5 date
)))
11852 (list (nth 4 date
) (nth 3 date
) (nth 5 date
)))))
11854 (defun org-parse-time-string (s &optional nodefault
)
11855 "Parse the standard Org-mode time string.
11856 This should be a lot faster than the normal `parse-time-string'.
11857 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
11858 hour and minute fields will be nil if not given."
11859 (if (string-match org-ts-regexp0 s
)
11861 (if (or (match-beginning 8) (not nodefault
))
11862 (string-to-number (or (match-string 8 s
) "0")))
11863 (if (or (match-beginning 7) (not nodefault
))
11864 (string-to-number (or (match-string 7 s
) "0")))
11865 (string-to-number (match-string 4 s
))
11866 (string-to-number (match-string 3 s
))
11867 (string-to-number (match-string 2 s
))
11871 (defun org-timestamp-up (&optional arg
)
11872 "Increase the date item at the cursor by one.
11873 If the cursor is on the year, change the year. If it is on the month or
11874 the day, change that.
11875 With prefix ARG, change by that many units."
11877 (org-timestamp-change (prefix-numeric-value arg
)))
11879 (defun org-timestamp-down (&optional arg
)
11880 "Decrease the date item at the cursor by one.
11881 If the cursor is on the year, change the year. If it is on the month or
11882 the day, change that.
11883 With prefix ARG, change by that many units."
11885 (org-timestamp-change (- (prefix-numeric-value arg
))))
11887 (defun org-timestamp-up-day (&optional arg
)
11888 "Increase the date in the time stamp by one day.
11889 With prefix ARG, change that many days."
11891 (if (and (not (org-at-timestamp-p t
))
11892 (org-on-heading-p))
11894 (org-timestamp-change (prefix-numeric-value arg
) 'day
)))
11896 (defun org-timestamp-down-day (&optional arg
)
11897 "Decrease the date in the time stamp by one day.
11898 With prefix ARG, change that many days."
11900 (if (and (not (org-at-timestamp-p t
))
11901 (org-on-heading-p))
11903 (org-timestamp-change (- (prefix-numeric-value arg
)) 'day
)))
11905 (defun org-at-timestamp-p (&optional inactive-ok
)
11906 "Determine if the cursor is in or at a timestamp."
11908 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2
))
11910 (ans (or (looking-at tsr
)
11912 (skip-chars-backward "^[<\n\r\t")
11913 (if (> (point) (point-min)) (backward-char 1))
11914 (and (looking-at tsr
)
11915 (> (- (match-end 0) pos
) -
1))))))
11917 (boundp 'org-ts-what
)
11920 ((= pos
(match-beginning 0)) 'bracket
)
11921 ((= pos
(1- (match-end 0))) 'bracket
)
11922 ((org-pos-in-match-range pos
2) 'year
)
11923 ((org-pos-in-match-range pos
3) 'month
)
11924 ((org-pos-in-match-range pos
7) 'hour
)
11925 ((org-pos-in-match-range pos
8) 'minute
)
11926 ((or (org-pos-in-match-range pos
4)
11927 (org-pos-in-match-range pos
5)) 'day
)
11928 ((and (> pos
(or (match-end 8) (match-end 5)))
11929 (< pos
(match-end 0)))
11930 (- pos
(or (match-end 8) (match-end 5))))
11934 (defun org-toggle-timestamp-type ()
11935 "Toggle the type (<active> or [inactive]) of a time stamp."
11937 (when (org-at-timestamp-p t
)
11938 (let ((beg (match-beginning 0)) (end (match-end 0))
11939 (map '((?\
[ .
"<") (?\
] .
">") (?
< .
"[") (?
> .
"]"))))
11942 (while (re-search-forward "[][<>]" end t
)
11943 (replace-match (cdr (assoc (char-after (match-beginning 0)) map
))
11945 (message "Timestamp is now %sactive"
11946 (if (equal (char-after beg
) ?
<) "" "in")))))
11948 (defun org-timestamp-change (n &optional what
)
11949 "Change the date in the time stamp at point.
11950 The date will be changed by N times WHAT. WHAT can be `day', `month',
11951 `year', `minute', `second'. If WHAT is not given, the cursor position
11952 in the timestamp determines what will be changed."
11953 (let ((pos (point))
11955 (dm (max (nth 1 org-time-stamp-rounding-minutes
) 1))
11959 (if (not (org-at-timestamp-p t
))
11960 (error "Not at a timestamp"))
11961 (if (and (not what
) (eq org-ts-what
'bracket
))
11962 (org-toggle-timestamp-type)
11963 (if (and (not what
) (not (eq org-ts-what
'day
))
11964 org-display-custom-times
11965 (get-text-property (point) 'display
)
11966 (not (get-text-property (1- (point)) 'display
)))
11967 (setq org-ts-what
'day
))
11968 (setq org-ts-what
(or what org-ts-what
)
11969 inactive
(= (char-after (match-beginning 0)) ?\
[)
11970 ts
(match-string 0))
11973 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
11975 (setq extra
(match-string 1 ts
)))
11976 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts
)
11978 (setq time0
(org-parse-time-string ts
))
11979 (when (and (eq org-ts-what
'minute
)
11980 (eq current-prefix-arg nil
))
11981 (setq n
(* dm
(cond ((> n
0) 1) ((< n
0) -
1) (t 0))))
11982 (when (not (= 0 (setq rem
(%
(nth 1 time0
) dm
))))
11983 (setcar (cdr time0
) (+ (nth 1 time0
)
11984 (if (> n
0) (- rem
) (- dm rem
))))))
11986 (encode-time (or (car time0
) 0)
11987 (+ (if (eq org-ts-what
'minute
) n
0) (nth 1 time0
))
11988 (+ (if (eq org-ts-what
'hour
) n
0) (nth 2 time0
))
11989 (+ (if (eq org-ts-what
'day
) n
0) (nth 3 time0
))
11990 (+ (if (eq org-ts-what
'month
) n
0) (nth 4 time0
))
11991 (+ (if (eq org-ts-what
'year
) n
0) (nth 5 time0
))
11993 (when (integerp org-ts-what
)
11994 (setq extra
(org-modify-ts-extra extra org-ts-what n dm
)))
11995 (if (eq what
'calendar
)
11996 (let ((cal-date (org-get-date-from-calendar)))
11997 (setcar (nthcdr 4 time0
) (nth 0 cal-date
)) ; month
11998 (setcar (nthcdr 3 time0
) (nth 1 cal-date
)) ; day
11999 (setcar (nthcdr 5 time0
) (nth 2 cal-date
)) ; year
12000 (setcar time0
(or (car time0
) 0))
12001 (setcar (nthcdr 1 time0
) (or (nth 1 time0
) 0))
12002 (setcar (nthcdr 2 time0
) (or (nth 2 time0
) 0))
12003 (setq time
(apply 'encode-time time0
))))
12004 (setq org-last-changed-timestamp
12005 (org-insert-time-stamp time with-hm inactive nil nil extra
))
12006 (org-clock-update-time-maybe)
12008 ;; Try to recenter the calendar window, if any
12009 (if (and org-calendar-follow-timestamp-change
12010 (get-buffer-window "*Calendar*" t
)
12011 (memq org-ts-what
'(day month year
)))
12012 (org-recenter-calendar (time-to-days time
))))))
12014 (defun org-modify-ts-extra (s pos n dm
)
12015 "Change the different parts of the lead-time and repeat fields in timestamp."
12016 (let ((idx '(("d" .
0) ("w" .
1) ("m" .
2) ("y" .
3) ("d" . -
1) ("y" .
4)))
12018 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s
)
12020 ((or (org-pos-in-match-range pos
2)
12021 (org-pos-in-match-range pos
3))
12022 (setq m
(string-to-number (match-string 3 s
))
12023 h
(string-to-number (match-string 2 s
)))
12024 (if (org-pos-in-match-range pos
2)
12026 (setq n
(* dm
(org-no-warnings (signum n
))))
12027 (when (not (= 0 (setq rem
(% m dm
))))
12028 (setq m
(+ m
(if (> n
0) (- rem
) (- dm rem
)))))
12030 (if (< m
0) (setq m
(+ m
60) h
(1- h
)))
12031 (if (> m
59) (setq m
(- m
60) h
(1+ h
)))
12032 (setq h
(min 24 (max 0 h
)))
12033 (setq ng
1 new
(format "-%02d:%02d" h m
)))
12034 ((org-pos-in-match-range pos
6)
12035 (setq ng
6 new
(car (rassoc (+ n
(cdr (assoc (match-string 6 s
) idx
))) idx
))))
12036 ((org-pos-in-match-range pos
5)
12037 (setq ng
5 new
(format "%d" (max 1 (+ n
(string-to-number (match-string 5 s
)))))))
12039 ((org-pos-in-match-range pos
9)
12040 (setq ng
9 new
(car (rassoc (+ n
(cdr (assoc (match-string 9 s
) idx
))) idx
))))
12041 ((org-pos-in-match-range pos
8)
12042 (setq ng
8 new
(format "%d" (max 0 (+ n
(string-to-number (match-string 8 s
))))))))
12046 (substring s
0 (match-beginning ng
))
12048 (substring s
(match-end ng
))))))
12051 (defun org-recenter-calendar (date)
12052 "If the calendar is visible, recenter it to DATE."
12053 (let* ((win (selected-window))
12054 (cwin (get-buffer-window "*Calendar*" t
))
12055 (calendar-move-hook nil
))
12057 (select-window cwin
)
12058 (calendar-goto-date (if (listp date
) date
12059 (calendar-gregorian-from-absolute date
)))
12060 (select-window win
))))
12062 (defun org-goto-calendar (&optional arg
)
12063 "Go to the Emacs calendar at the current date.
12064 If there is a time stamp in the current line, go to that date.
12065 A prefix ARG can be used to force the current date."
12067 (let ((tsr org-ts-regexp
) diff
12068 (calendar-move-hook nil
)
12069 (calendar-view-holidays-initially-flag nil
)
12070 (view-calendar-holidays-initially nil
)
12071 (calendar-view-diary-initially-flag nil
)
12072 (view-diary-entries-initially nil
))
12073 (if (or (org-at-timestamp-p)
12075 (beginning-of-line 1)
12076 (looking-at (concat ".*" tsr
))))
12077 (let ((d1 (time-to-days (current-time)))
12079 (org-time-string-to-time (match-string 1)))))
12080 (setq diff
(- d2 d1
))))
12082 (calendar-goto-today)
12083 (if (and diff
(not arg
)) (calendar-forward-day diff
))))
12085 (defun org-get-date-from-calendar ()
12086 "Return a list (month day year) of date at point in calendar."
12087 (with-current-buffer "*Calendar*"
12089 (calendar-cursor-to-date))))
12091 (defun org-date-from-calendar ()
12092 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
12093 If there is already a time stamp at the cursor position, update it."
12095 (if (org-at-timestamp-p t
)
12096 (org-timestamp-change 0 'calendar
)
12097 (let ((cal-date (org-get-date-from-calendar)))
12098 (org-insert-time-stamp
12099 (encode-time 0 0 0 (nth 1 cal-date
) (car cal-date
) (nth 2 cal-date
))))))
12101 (defun org-minutes-to-hh:mm-string
(m)
12102 "Compute H:MM from a number of minutes."
12103 (let ((h (/ m
60)))
12104 (setq m
(- m
(* 60 h
)))
12105 (format org-time-clocksum-format h m
)))
12107 (defun org-hh:mm-string-to-minutes
(s)
12108 "Convert a string H:MM to a number of minutes."
12109 (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s
)
12110 (+ (* (string-to-number (match-string 1 s
)) 60)
12111 (string-to-number (match-string 2 s
)))
12117 (defun org-iswitchb (&optional arg
)
12118 "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
12119 With a prefix argument, restrict available to files.
12120 With two prefix arguments, restrict available buffers to agenda files.
12122 Due to some yet unresolved reason, the global function
12123 `iswitchb-mode' needs to be active for this function to work."
12125 (require 'iswitchb
)
12126 (let ((enabled iswitchb-mode
) blist
)
12127 (or enabled
(iswitchb-mode 1))
12128 (setq blist
(cond ((equal arg
'(4)) (org-buffer-list 'files
))
12129 ((equal arg
'(16)) (org-buffer-list 'agenda
))
12130 (t (org-buffer-list))))
12132 (let ((iswitchb-make-buflist-hook
12134 (setq iswitchb-temp-buflist
12135 (mapcar 'buffer-name blist
)))))
12137 (iswitchb-read-buffer
12138 "Switch-to: " nil t
))
12139 (or enabled
(iswitchb-mode -
1))))))
12142 (defun org-ido-switchb (&optional arg
)
12143 "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
12144 With a prefix argument, restrict available to files.
12145 With two prefix arguments, restrict available buffers to agenda files."
12147 (let ((blist (cond ((equal arg
'(4)) (org-buffer-list 'files
))
12148 ((equal arg
'(16)) (org-buffer-list 'agenda
))
12149 (t (org-buffer-list)))))
12151 (org-ido-completing-read "Org buffer: "
12152 (mapcar 'buffer-name blist
)
12155 (defun org-buffer-list (&optional predicate exclude-tmp
)
12156 "Return a list of Org buffers.
12157 PREDICATE can be `export', `files' or `agenda'.
12159 export restrict the list to Export buffers.
12160 files restrict the list to buffers visiting Org files.
12161 agenda restrict the list to buffers visiting agenda files.
12163 If EXCLUDE-TMP is non-nil, ignore temporary buffers."
12165 (agenda-files (and (eq predicate
'agenda
)
12166 (mapcar 'file-truename
(org-agenda-files t
))))
12169 ((eq predicate
'files
)
12170 (lambda (b) (with-current-buffer b
(eq major-mode
'org-mode
))))
12171 ((eq predicate
'export
)
12172 (lambda (b) (string-match "\*Org .*Export" (buffer-name b
))))
12173 ((eq predicate
'agenda
)
12175 (with-current-buffer b
12176 (and (eq major-mode
'org-mode
)
12177 (setq bfn
(buffer-file-name b
))
12178 (member (file-truename bfn
) agenda-files
)))))
12179 (t (lambda (b) (with-current-buffer b
12180 (or (eq major-mode
'org-mode
)
12181 (string-match "\*Org .*Export"
12182 (buffer-name b
)))))))))
12186 (if (and (funcall filter b
)
12187 (or (not exclude-tmp
)
12188 (not (string-match "tmp" (buffer-name b
)))))
12193 (defun org-agenda-files (&optional unrestricted archives
)
12194 "Get the list of agenda files.
12195 Optional UNRESTRICTED means return the full list even if a restriction
12196 is currently in place.
12197 When ARCHIVES is t, include all archive files hat are really being
12198 used by the agenda files. If ARCHIVE is `ifmode', do this only if
12199 `org-agenda-archives-mode' is t."
12202 ((and (not unrestricted
) (get 'org-agenda-files
'org-restrict
)))
12203 ((stringp org-agenda-files
) (org-read-agenda-file-list))
12204 ((listp org-agenda-files
) org-agenda-files
)
12205 (t (error "Invalid value of `org-agenda-files'")))))
12206 (setq files
(apply 'append
12207 (mapcar (lambda (f)
12208 (if (file-directory-p f
)
12210 f t org-agenda-file-regexp
)
12213 (when org-agenda-skip-unavailable-files
12214 (setq files
(delq nil
12217 (and (file-readable-p file
) file
)))
12219 (when (or (eq archives t
)
12220 (and (eq archives
'ifmode
) (eq org-agenda-archives-mode t
)))
12221 (setq files
(org-add-archive-files files
)))
12224 (defun org-edit-agenda-file-list ()
12225 "Edit the list of agenda files.
12226 Depending on setup, this either uses customize to edit the variable
12227 `org-agenda-files', or it visits the file that is holding the list. In the
12228 latter case, the buffer is set up in a way that saving it automatically kills
12229 the buffer and restores the previous window configuration."
12231 (if (stringp org-agenda-files
)
12232 (let ((cw (current-window-configuration)))
12233 (find-file org-agenda-files
)
12234 (org-set-local 'org-window-configuration cw
)
12235 (org-add-hook 'after-save-hook
12237 (set-window-configuration
12238 (prog1 org-window-configuration
12239 (kill-buffer (current-buffer))))
12240 (org-install-agenda-files-menu)
12241 (message "New agenda file list installed"))
12243 (message "%s" (substitute-command-keys
12244 "Edit list and finish with \\[save-buffer]")))
12245 (customize-variable 'org-agenda-files
)))
12247 (defun org-store-new-agenda-file-list (list)
12248 "Set new value for the agenda file list and save it correctly."
12249 (if (stringp org-agenda-files
)
12250 (let ((f org-agenda-files
) b
)
12251 (while (setq b
(find-buffer-visiting f
)) (kill-buffer b
))
12253 (insert (mapconcat 'identity list
"\n") "\n")))
12254 (let ((org-mode-hook nil
) (default-major-mode 'fundamental-mode
))
12255 (setq org-agenda-files list
)
12256 (customize-save-variable 'org-agenda-files org-agenda-files
))))
12258 (defun org-read-agenda-file-list ()
12259 "Read the list of agenda files from a file."
12260 (when (file-directory-p org-agenda-files
)
12261 (error "`org-agenda-files' cannot be a single directory"))
12262 (when (stringp org-agenda-files
)
12264 (insert-file-contents org-agenda-files
)
12265 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
12269 (defun org-cycle-agenda-files ()
12270 "Cycle through the files in `org-agenda-files'.
12271 If the current buffer visits an agenda file, find the next one in the list.
12272 If the current buffer does not, find the first agenda file."
12274 (let* ((fs (org-agenda-files t
))
12275 (files (append fs
(list (car fs
))))
12276 (tcf (if buffer-file-name
(file-truename buffer-file-name
)))
12278 (unless files
(error "No agenda files"))
12280 (while (setq file
(pop files
))
12281 (if (equal (file-truename file
) tcf
)
12283 (find-file (car files
))
12285 (find-file (car fs
)))
12286 (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
12288 (defun org-agenda-file-to-front (&optional to-end
)
12289 "Move/add the current file to the top of the agenda file list.
12290 If the file is not present in the list, it is added to the front. If it is
12291 present, it is moved there. With optional argument TO-END, add/move to the
12294 (let ((org-agenda-skip-unavailable-files nil
)
12295 (file-alist (mapcar (lambda (x)
12296 (cons (file-truename x
) x
))
12297 (org-agenda-files t
)))
12298 (ctf (file-truename buffer-file-name
))
12300 (setq x
(assoc ctf file-alist
) had x
)
12302 (if (not x
) (setq x
(cons ctf
(abbreviate-file-name buffer-file-name
))))
12304 (setq file-alist
(append (delq x file-alist
) (list x
)))
12305 (setq file-alist
(cons x
(delq x file-alist
))))
12306 (org-store-new-agenda-file-list (mapcar 'cdr file-alist
))
12307 (org-install-agenda-files-menu)
12308 (message "File %s to %s of agenda file list"
12309 (if had
"moved" "added") (if to-end
"end" "front"))))
12311 (defun org-remove-file (&optional file
)
12312 "Remove current file from the list of files in variable `org-agenda-files'.
12313 These are the files which are being checked for agenda entries.
12314 Optional argument FILE means, use this file instead of the current."
12316 (let* ((org-agenda-skip-unavailable-files nil
)
12317 (file (or file buffer-file-name
))
12318 (true-file (file-truename file
))
12319 (afile (abbreviate-file-name file
))
12320 (files (delq nil
(mapcar
12322 (if (equal true-file
12325 (org-agenda-files t
)))))
12326 (if (not (= (length files
) (length (org-agenda-files t
))))
12328 (org-store-new-agenda-file-list files
)
12329 (org-install-agenda-files-menu)
12330 (message "Removed file: %s" afile
))
12331 (message "File was not in list: %s (not removed)" afile
))))
12333 (defun org-file-menu-entry (file)
12334 (vector file
(list 'find-file file
) t
))
12336 (defun org-check-agenda-file (file)
12337 "Make sure FILE exists. If not, ask user what to do."
12338 (when (not (file-exists-p file
))
12339 (message "non-existent file %s. [R]emove from list or [A]bort?"
12340 (abbreviate-file-name file
))
12341 (let ((r (downcase (read-char-exclusive))))
12344 (org-remove-file file
)
12345 (throw 'nextfile t
))
12346 (t (error "Abort"))))))
12348 (defun org-get-agenda-file-buffer (file)
12349 "Get a buffer visiting FILE. If the buffer needs to be created, add
12350 it to the list of buffers which might be released later."
12351 (let ((buf (org-find-base-buffer-visiting file
)))
12353 buf
; just return it
12354 ;; Make a new buffer and remember it
12355 (setq buf
(find-file-noselect file
))
12356 (if buf
(push buf org-agenda-new-buffers
))
12359 (defun org-release-buffers (blist)
12360 "Release all buffers in list, asking the user for confirmation when needed.
12361 When a buffer is unmodified, it is just killed. When modified, it is saved
12362 \(if the user agrees) and then killed."
12364 (while (setq buf
(pop blist
))
12365 (setq file
(buffer-file-name buf
))
12366 (when (and (buffer-modified-p buf
)
12368 (y-or-n-p (format "Save file %s? " file
)))
12369 (with-current-buffer buf
(save-buffer)))
12370 (kill-buffer buf
))))
12372 (defun org-prepare-agenda-buffers (files)
12373 "Create buffers for all agenda files, protect archived trees and comments."
12375 (let ((pa '(:org-archived t
))
12376 (pc '(:org-comment t
))
12377 (pall '(:org-archived t
:org-comment t
))
12378 (inhibit-read-only t
)
12379 (rea (concat ":" org-archive-tag
":"))
12383 (while (setq file
(pop files
))
12386 (org-check-agenda-file file
)
12387 (set-buffer (org-get-agenda-file-buffer file
)))
12389 (setq bmp
(buffer-modified-p))
12390 (org-refresh-category-properties)
12391 (setq org-todo-keywords-for-agenda
12392 (append org-todo-keywords-for-agenda org-todo-keywords-1
))
12393 (setq org-done-keywords-for-agenda
12394 (append org-done-keywords-for-agenda org-done-keywords
))
12395 (setq org-todo-keyword-alist-for-agenda
12396 (append org-todo-keyword-alist-for-agenda org-todo-key-alist
))
12397 (setq org-tag-alist-for-agenda
12398 (append org-tag-alist-for-agenda org-tag-alist
))
12401 (remove-text-properties (point-min) (point-max) pall
)
12402 (when org-agenda-skip-archived-trees
12403 (goto-char (point-min))
12404 (while (re-search-forward rea nil t
)
12405 (if (org-on-heading-p t
)
12406 (add-text-properties (point-at-bol) (org-end-of-subtree t
) pa
))))
12407 (goto-char (point-min))
12408 (setq re
(concat "^\\*+ +" org-comment-string
"\\>"))
12409 (while (re-search-forward re nil t
)
12410 (add-text-properties
12411 (match-beginning 0) (org-end-of-subtree t
) pc
)))
12412 (set-buffer-modified-p bmp
))))
12413 (setq org-todo-keyword-alist-for-agenda
12414 (org-uniquify org-todo-keyword-alist-for-agenda
)
12415 org-tag-alist-for-agenda
(org-uniquify org-tag-alist-for-agenda
))))
12417 ;;;; Embedded LaTeX
12419 (defvar org-cdlatex-mode-map
(make-sparse-keymap)
12420 "Keymap for the minor `org-cdlatex-mode'.")
12422 (org-defkey org-cdlatex-mode-map
"_" 'org-cdlatex-underscore-caret
)
12423 (org-defkey org-cdlatex-mode-map
"^" 'org-cdlatex-underscore-caret
)
12424 (org-defkey org-cdlatex-mode-map
"`" 'cdlatex-math-symbol
)
12425 (org-defkey org-cdlatex-mode-map
"'" 'org-cdlatex-math-modify
)
12426 (org-defkey org-cdlatex-mode-map
"\C-c{" 'cdlatex-environment
)
12428 (defvar org-cdlatex-texmathp-advice-is-done nil
12429 "Flag remembering if we have applied the advice to texmathp already.")
12431 (define-minor-mode org-cdlatex-mode
12432 "Toggle the minor `org-cdlatex-mode'.
12433 This mode supports entering LaTeX environment and math in LaTeX fragments
12435 \\{org-cdlatex-mode-map}"
12437 (when org-cdlatex-mode
(require 'cdlatex
))
12438 (unless org-cdlatex-texmathp-advice-is-done
12439 (setq org-cdlatex-texmathp-advice-is-done t
)
12440 (defadvice texmathp
(around org-math-always-on activate
)
12441 "Always return t in org-mode buffers.
12442 This is because we want to insert math symbols without dollars even outside
12443 the LaTeX math segments. If Orgmode thinks that point is actually inside
12444 an embedded LaTeX fragment, let texmathp do its job.
12445 \\[org-cdlatex-mode-map]"
12449 ((not (org-mode-p)) ad-do-it
)
12450 ((eq this-command
'cdlatex-math-symbol
)
12451 (setq ad-return-value t
12452 texmathp-why
'("cdlatex-math-symbol in org-mode" .
0)))
12454 (let ((p (org-inside-LaTeX-fragment-p)))
12455 (if (and p
(member (car p
) (plist-get org-format-latex-options
:matchers
)))
12456 (setq ad-return-value t
12457 texmathp-why
'("Org-mode embedded math" .
0))
12458 (if p ad-do-it
)))))))))
12460 (defun turn-on-org-cdlatex ()
12461 "Unconditionally turn on `org-cdlatex-mode'."
12462 (org-cdlatex-mode 1))
12464 (defun org-inside-LaTeX-fragment-p ()
12465 "Test if point is inside a LaTeX fragment.
12466 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
12467 sequence appearing also before point.
12468 Even though the matchers for math are configurable, this function assumes
12469 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
12470 delimiters are skipped when they have been removed by customization.
12471 The return value is nil, or a cons cell with the delimiter and
12472 and the position of this delimiter.
12474 This function does a reasonably good job, but can locally be fooled by
12475 for example currency specifications. For example it will assume being in
12476 inline math after \"$22.34\". The LaTeX fragment formatter will only format
12477 fragments that are properly closed, but during editing, we have to live
12478 with the uncertainty caused by missing closing delimiters. This function
12479 looks only before point, not after."
12481 (let ((pos (point))
12482 (dodollar (member "$" (plist-get org-format-latex-options
:matchers
)))
12484 (re-search-backward (concat "^\\(" paragraph-start
"\\)") nil t
)
12486 dd-on str
(start 0) m re
)
12489 (setq str
(concat (buffer-substring lim
(point)) "\000 X$.")
12490 re
(nth 1 (assoc "$" org-latex-regexps
)))
12491 (while (string-match re str start
)
12493 ((= (match-end 0) (length str
))
12494 (throw 'exit
(cons "$" (+ lim
(match-beginning 0) 1))))
12495 ((= (match-end 0) (- (length str
) 5))
12497 (t (setq start
(match-end 0))))))
12498 (when (setq m
(re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t
))
12500 (and (match-beginning 1) (throw 'exit
(cons (match-string 1) m
)))
12501 (and (match-beginning 2) (throw 'exit nil
))
12503 (while (re-search-backward "\\$\\$" lim t
)
12504 (setq dd-on
(not dd-on
)))
12506 (if dd-on
(cons "$$" m
))))))
12509 (defun org-try-cdlatex-tab ()
12510 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
12511 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
12512 - inside a LaTeX fragment, or
12513 - after the first word in a line, where an abbreviation expansion could
12514 insert a LaTeX environment."
12515 (when org-cdlatex-mode
12518 (skip-chars-backward "a-zA-Z0-9*")
12519 (skip-chars-backward " \t")
12522 ((org-inside-LaTeX-fragment-p)
12526 (defun org-cdlatex-underscore-caret (&optional arg
)
12527 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
12528 Revert to the normal definition outside of these fragments."
12530 (if (org-inside-LaTeX-fragment-p)
12531 (call-interactively 'cdlatex-sub-superscript
)
12532 (let (org-cdlatex-mode)
12533 (call-interactively (key-binding (vector last-input-event
))))))
12535 (defun org-cdlatex-math-modify (&optional arg
)
12536 "Execute `cdlatex-math-modify' in LaTeX fragments.
12537 Revert to the normal definition outside of these fragments."
12539 (if (org-inside-LaTeX-fragment-p)
12540 (call-interactively 'cdlatex-math-modify
)
12541 (let (org-cdlatex-mode)
12542 (call-interactively (key-binding (vector last-input-event
))))))
12544 (defvar org-latex-fragment-image-overlays nil
12545 "List of overlays carrying the images of latex fragments.")
12546 (make-variable-buffer-local 'org-latex-fragment-image-overlays
)
12548 (defun org-remove-latex-fragment-image-overlays ()
12549 "Remove all overlays with LaTeX fragment images in current buffer."
12550 (mapc 'org-delete-overlay org-latex-fragment-image-overlays
)
12551 (setq org-latex-fragment-image-overlays nil
))
12553 (defun org-preview-latex-fragment (&optional subtree
)
12554 "Preview the LaTeX fragment at point, or all locally or globally.
12555 If the cursor is in a LaTeX fragment, create the image and overlay
12556 it over the source code. If there is no fragment at point, display
12557 all fragments in the current text, from one headline to the next. With
12558 prefix SUBTREE, display all fragments in the current subtree. With a
12559 double prefix `C-u C-u', or when the cursor is before the first headline,
12560 display all fragments in the buffer.
12561 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
12563 (org-remove-latex-fragment-image-overlays)
12566 (let (beg end at msg
)
12568 ((or (equal subtree
'(16))
12569 (not (save-excursion
12570 (re-search-backward (concat "^" outline-regexp
) nil t
))))
12571 (setq beg
(point-min) end
(point-max)
12572 msg
"Creating images for buffer...%s"))
12573 ((equal subtree
'(4))
12574 (org-back-to-heading)
12575 (setq beg
(point) end
(org-end-of-subtree t
)
12576 msg
"Creating images for subtree...%s"))
12578 (if (setq at
(org-inside-LaTeX-fragment-p))
12579 (goto-char (max (point-min) (- (cdr at
) 2)))
12580 (org-back-to-heading))
12581 (setq beg
(point) end
(progn (outline-next-heading) (point))
12582 msg
(if at
"Creating image...%s"
12583 "Creating images for entry...%s"))))
12585 (narrow-to-region beg end
)
12588 (concat "ltxpng/" (file-name-sans-extension
12589 (file-name-nondirectory
12590 buffer-file-name
)))
12591 default-directory
'overlays msg at
'forbuffer
)
12592 (message msg
"done. Use `C-c C-c' to remove images.")))))
12594 (defvar org-latex-regexps
12595 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t
)
12596 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
12597 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
12598 ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil
)
12599 ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil
)
12600 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil
)
12601 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t
)
12602 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t
))
12603 "Regular expressions for matching embedded LaTeX.")
12605 (defun org-format-latex (prefix &optional dir overlays msg at forbuffer
)
12606 "Replace LaTeX fragments with links to an image, and produce images."
12607 (if (and overlays
(fboundp 'clear-image-cache
)) (clear-image-cache))
12608 (let* ((prefixnodir (file-name-nondirectory prefix
))
12609 (absprefix (expand-file-name prefix dir
))
12610 (todir (file-name-directory absprefix
))
12611 (opt org-format-latex-options
)
12612 (matchers (plist-get opt
:matchers
))
12613 (re-list org-latex-regexps
)
12614 (cnt 0) txt link beg end re e checkdir
12615 m n block linkfile movefile ov
)
12616 ;; Check if there are old images files with this prefix, and remove them
12617 (when (file-directory-p todir
)
12621 (concat (regexp-quote prefixnodir
) "_[0-9]+\\.png$"))))
12622 ;; Check the different regular expressions
12623 (while (setq e
(pop re-list
))
12624 (setq m
(car e
) re
(nth 1 e
) n
(nth 2 e
)
12625 block
(if (nth 3 e
) "\n\n" ""))
12626 (when (member m matchers
)
12627 (goto-char (point-min))
12628 (while (re-search-forward re nil t
)
12629 (when (or (not at
) (equal (cdr at
) (match-beginning n
)))
12630 (setq txt
(match-string n
)
12631 beg
(match-beginning n
) end
(match-end n
)
12633 linkfile
(format "%s_%04d.png" prefix cnt
)
12634 movefile
(format "%s_%04d.png" absprefix cnt
)
12635 link
(concat block
"[[file:" linkfile
"]]" block
))
12636 (if msg
(message msg cnt
))
12638 (unless checkdir
; make sure the directory exists
12640 (or (file-directory-p todir
) (make-directory todir
)))
12641 (org-create-formula-image
12642 txt movefile opt forbuffer
)
12645 (setq ov
(org-make-overlay beg end
))
12646 (if (featurep 'xemacs
)
12648 (org-overlay-put ov
'invisible t
)
12651 (make-glyph (vector 'png
:file movefile
))))
12654 (list 'image
:type
'png
:file movefile
:ascent
'center
)))
12655 (push ov org-latex-fragment-image-overlays
)
12657 (delete-region beg end
)
12658 (insert link
))))))))
12660 ;; This function borrows from Ganesh Swami's latex2png.el
12661 (defun org-create-formula-image (string tofile options buffer
)
12662 (let* ((tmpdir (if (featurep 'xemacs
)
12664 temporary-file-directory
))
12665 (texfilebase (make-temp-name
12666 (expand-file-name "orgtex" tmpdir
)))
12667 (texfile (concat texfilebase
".tex"))
12668 (dvifile (concat texfilebase
".dvi"))
12669 (pngfile (concat texfilebase
".png"))
12670 (fnh (if (featurep 'xemacs
)
12671 (font-height (get-face-font 'default
))
12672 (face-attribute 'default
:height nil
)))
12673 (scale (or (plist-get options
(if buffer
:scale
:html-scale
)) 1.0))
12674 (dpi (number-to-string (* scale
(floor (* 0.9 (if buffer fnh
140.
))))))
12675 (fg (or (plist-get options
(if buffer
:foreground
:html-foreground
))
12677 (bg (or (plist-get options
(if buffer
:background
:html-background
))
12679 (if (eq fg
'default
) (setq fg
(org-dvipng-color :foreground
)))
12680 (if (eq bg
'default
) (setq bg
(org-dvipng-color :background
)))
12681 (with-temp-file texfile
12682 (insert org-format-latex-header
12683 "\n\\begin{document}\n" string
"\n\\end{document}\n"))
12684 (let ((dir default-directory
))
12685 (condition-case nil
12688 (call-process "latex" nil nil nil texfile
))
12691 (if (not (file-exists-p dvifile
))
12692 (progn (message "Failed to create dvi file from %s" texfile
) nil
)
12693 (condition-case nil
12694 (call-process "dvipng" nil nil nil
12695 "-E" "-fg" fg
"-bg" bg
12697 ;;"-x" scale "-y" scale
12702 (if (not (file-exists-p pngfile
))
12703 (progn (message "Failed to create png file from %s" texfile
) nil
)
12704 ;; Use the requested file name and clean up
12705 (copy-file pngfile tofile
'replace
)
12706 (loop for e in
'(".dvi" ".tex" ".aux" ".log" ".png") do
12707 (delete-file (concat texfilebase e
)))
12710 (defun org-dvipng-color (attr)
12711 "Return an rgb color specification for dvipng."
12712 (apply 'format
"rgb %s %s %s"
12713 (mapcar 'org-normalize-color
12714 (color-values (face-attribute 'default attr nil
)))))
12716 (defun org-normalize-color (value)
12717 "Return string to be used as color value for an RGB component."
12718 (format "%g" (/ value
65535.0)))
12722 ;; Make `C-c C-x' a prefix key
12723 (org-defkey org-mode-map
"\C-c\C-x" (make-sparse-keymap))
12725 ;; TAB key with modifiers
12726 (org-defkey org-mode-map
"\C-i" 'org-cycle
)
12727 (org-defkey org-mode-map
[(tab)] 'org-cycle
)
12728 (org-defkey org-mode-map
[(control tab
)] 'org-force-cycle-archived
)
12729 (org-defkey org-mode-map
[(meta tab
)] 'org-complete
)
12730 (org-defkey org-mode-map
"\M-\t" 'org-complete
)
12731 (org-defkey org-mode-map
"\M-\C-i" 'org-complete
)
12732 ;; The following line is necessary under Suse GNU/Linux
12733 (unless (featurep 'xemacs
)
12734 (org-defkey org-mode-map
[S-iso-lefttab
] 'org-shifttab
))
12735 (org-defkey org-mode-map
[(shift tab
)] 'org-shifttab
)
12736 (define-key org-mode-map
[backtab] 'org-shifttab)
12738 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
12739 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
12740 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
12742 ;; Cursor keys with modifiers
12743 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
12744 (org-defkey org-mode-map [(meta right)] 'org-metaright)
12745 (org-defkey org-mode-map [(meta up)] 'org-metaup)
12746 (org-defkey org-mode-map [(meta down)] 'org-metadown)
12748 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
12749 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
12750 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
12751 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
12753 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
12754 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
12755 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
12756 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
12758 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
12759 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
12761 ;;; Extra keys for tty access.
12762 ;; We only set them when really needed because otherwise the
12763 ;; menus don't show the simple keys
12765 (when (or org-use-extra-keys
12766 (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
12767 (not window-system))
12768 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
12769 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
12770 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
12771 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
12772 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
12773 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
12774 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
12775 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
12776 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
12777 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
12778 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
12779 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
12780 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
12781 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
12782 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
12783 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
12784 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
12785 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
12786 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
12787 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
12788 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
12789 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
12791 ;; All the other keys
12793 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
12794 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
12795 (if (boundp 'narrow-map)
12796 (org-defkey narrow-map "s" 'org-narrow-to-subtree)
12797 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
12798 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
12799 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
12800 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
12801 (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
12802 (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
12803 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
12804 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
12805 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
12806 (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
12807 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
12808 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
12809 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
12810 (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
12811 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
12812 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
12813 (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
12814 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
12815 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
12816 (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
12817 (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
12818 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
12819 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
12820 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
12821 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
12822 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
12823 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
12824 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
12825 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
12826 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
12827 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
12828 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
12829 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
12830 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
12831 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
12832 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
12833 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
12834 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
12835 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
12836 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
12837 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
12838 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
12839 (org-defkey org-mode-map "\C-c^" 'org-sort)
12840 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
12841 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
12842 (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
12843 (org-defkey org-mode-map "\C-m" 'org-return)
12844 (org-defkey org-mode-map "\C-j" 'org-return-indent)
12845 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
12846 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
12847 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
12848 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
12849 (org-defkey org-mode-map "\C-c'" 'org-edit-special)
12850 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
12851 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
12852 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
12853 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
12854 (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
12855 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
12856 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
12857 (org-defkey org-mode-map "\C-c\C-e" 'org-export)
12858 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
12859 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
12860 (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
12862 (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
12863 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
12864 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
12865 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
12867 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
12868 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
12869 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
12870 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
12871 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
12872 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
12873 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
12874 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
12875 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
12876 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
12877 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
12878 (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
12880 (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
12881 (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
12882 (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
12883 (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
12885 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
12887 (when (featurep 'xemacs)
12888 (org-defkey org-mode-map 'button3 'popup-mode-menu))
12890 (defvar org-table-auto-blank-field) ; defined in org-table.el
12891 (defun org-self-insert-command (N)
12892 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
12893 If the cursor is in a table looking at whitespace, the whitespace is
12894 overwritten, and the table is not marked as requiring realignment."
12896 (if (and (org-table-p)
12898 ;; check if we blank the field, and if that triggers align
12899 (and (featurep 'org-table) org-table-auto-blank-field
12900 (member last-command
12901 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
12902 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
12903 ;; got extra space, this field does not determine column width
12904 (let (org-table-may-need-update) (org-table-blank-field))
12905 ;; no extra space, this field may determine column width
12906 (org-table-blank-field)))
12909 (looking-at "[^|\n]* |"))
12910 (let (org-table-may-need-update)
12911 (goto-char (1- (match-end 0)))
12912 (delete-backward-char 1)
12913 (goto-char (match-beginning 0))
12914 (self-insert-command N))
12915 (setq org-table-may-need-update t)
12916 (self-insert-command N)
12917 (org-fix-tags-on-the-fly)))
12919 (defun org-fix-tags-on-the-fly ()
12920 (when (and (equal (char-after (point-at-bol)) ?*)
12921 (org-on-heading-p))
12922 (org-align-tags-here org-tags-column)))
12924 (defun org-delete-backward-char (N)
12925 "Like `delete-backward-char', insert whitespace at field end in tables.
12926 When deleting backwards, in tables this function will insert whitespace in
12927 front of the next \"|\" separator, to keep the table aligned. The table will
12928 still be marked for re-alignment if the field did fill the entire column,
12929 because, in this case the deletion might narrow the column."
12931 (if (and (org-table-p)
12933 (string-match "|" (buffer-substring (point-at-bol) (point)))
12934 (looking-at ".*?|"))
12935 (let ((pos (point))
12936 (noalign (looking-at "[^|\n\r]* |"))
12937 (c org-table-may-need-update))
12938 (backward-delete-char N)
12939 (skip-chars-forward "^|")
12941 (goto-char (1- pos))
12942 ;; noalign: if there were two spaces at the end, this field
12943 ;; does not determine the width of the column.
12944 (if noalign (setq org-table-may-need-update c)))
12945 (backward-delete-char N)
12946 (org-fix-tags-on-the-fly)))
12948 (defun org-delete-char (N)
12949 "Like `delete-char', but insert whitespace at field end in tables.
12950 When deleting characters, in tables this function will insert whitespace in
12951 front of the next \"|\" separator, to keep the table aligned. The table will
12952 still be marked for re-alignment if the field did fill the entire column,
12953 because, in this case the deletion might narrow the column."
12955 (if (and (org-table-p)
12957 (not (= (char-after) ?|))
12959 (if (looking-at ".*?|")
12960 (let ((pos (point))
12961 (noalign (looking-at "[^|\n\r]* |"))
12962 (c org-table-may-need-update))
12963 (replace-match (concat
12964 (substring (match-string 0) 1 -1)
12967 ;; noalign: if there were two spaces at the end, this field
12968 ;; does not determine the width of the column.
12969 (if noalign (setq org-table-may-need-update c)))
12972 (org-fix-tags-on-the-fly)))
12974 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
12975 (put 'org-self-insert-command 'delete-selection t)
12976 (put 'orgtbl-self-insert-command 'delete-selection t)
12977 (put 'org-delete-char 'delete-selection 'supersede)
12978 (put 'org-delete-backward-char 'delete-selection 'supersede)
12979 (put 'org-yank 'delete-selection 'yank)
12981 ;; Make `flyspell-mode' delay after some commands
12982 (put 'org-self-insert-command 'flyspell-delayed t)
12983 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
12984 (put 'org-delete-char 'flyspell-delayed t)
12985 (put 'org-delete-backward-char 'flyspell-delayed t)
12987 ;; Make pabbrev-mode expand after org-mode commands
12988 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
12989 (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
12991 ;; How to do this: Measure non-white length of current string
12992 ;; If equal to column width, we should realign.
12994 (defun org-remap (map &rest commands)
12995 "In MAP, remap the functions given in COMMANDS.
12996 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
12999 (setq old (pop commands) new (pop commands))
13000 (if (fboundp 'command-remapping)
13001 (org-defkey map (vector 'remap old) new)
13002 (substitute-key-definition old new map global-map)))))
13004 (when (eq org-enable-table-editor 'optimized)
13005 ;; If the user wants maximum table support, we need to hijack
13006 ;; some standard editing functions
13007 (org-remap org-mode-map
13008 'self-insert-command 'org-self-insert-command
13009 'delete-char 'org-delete-char
13010 'delete-backward-char 'org-delete-backward-char)
13011 (org-defkey org-mode-map "|" 'org-force-self-insert))
13013 (defun org-modifier-cursor-error ()
13014 "Throw an error, a modified cursor command was applied in wrong context."
13015 (error "This command is active in special context like tables, headlines or items"))
13017 (defun org-shiftselect-error ()
13018 "Throw an error because Shift-Cursor command was applied in wrong context."
13019 (if (and (boundp 'shift-select-mode) shift-select-mode)
13020 (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
13021 (error "This command works only in special context like headlines or timestamps.")))
13023 (defun org-call-for-shift-select (cmd)
13024 (let ((this-command-keys-shift-translated t))
13025 (call-interactively cmd)))
13027 (defun org-shifttab (&optional arg)
13028 "Global visibility cycling or move to previous table field.
13029 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
13031 See the individual commands for more information."
13034 ((org-at-table-p) (call-interactively 'org-table-previous-field))
13036 (message "Content view to level: %d" arg)
13037 (org-content (prefix-numeric-value arg))
13038 (setq org-cycle-global-status 'overview))
13039 (t (call-interactively 'org-global-cycle))))
13041 (defun org-shiftmetaleft ()
13042 "Promote subtree or delete table column.
13043 Calls `org-promote-subtree', `org-outdent-item',
13044 or `org-table-delete-column', depending on context.
13045 See the individual commands for more information."
13048 ((org-at-table-p) (call-interactively 'org-table-delete-column))
13049 ((org-on-heading-p) (call-interactively 'org-promote-subtree))
13050 ((org-at-item-p) (call-interactively 'org-outdent-item))
13051 (t (org-modifier-cursor-error))))
13053 (defun org-shiftmetaright ()
13054 "Demote subtree or insert table column.
13055 Calls `org-demote-subtree', `org-indent-item',
13056 or `org-table-insert-column', depending on context.
13057 See the individual commands for more information."
13060 ((org-at-table-p) (call-interactively 'org-table-insert-column))
13061 ((org-on-heading-p) (call-interactively 'org-demote-subtree))
13062 ((org-at-item-p) (call-interactively 'org-indent-item))
13063 (t (org-modifier-cursor-error))))
13065 (defun org-shiftmetaup (&optional arg)
13066 "Move subtree up or kill table row.
13067 Calls `org-move-subtree-up' or `org-table-kill-row' or
13068 `org-move-item-up' depending on context. See the individual commands
13069 for more information."
13072 ((org-at-table-p) (call-interactively 'org-table-kill-row))
13073 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
13074 ((org-at-item-p) (call-interactively 'org-move-item-up))
13075 (t (org-modifier-cursor-error))))
13076 (defun org-shiftmetadown (&optional arg)
13077 "Move subtree down or insert table row.
13078 Calls `org-move-subtree-down' or `org-table-insert-row' or
13079 `org-move-item-down', depending on context. See the individual
13080 commands for more information."
13083 ((org-at-table-p) (call-interactively 'org-table-insert-row))
13084 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
13085 ((org-at-item-p) (call-interactively 'org-move-item-down))
13086 (t (org-modifier-cursor-error))))
13088 (defun org-metaleft (&optional arg)
13089 "Promote heading or move table column to left.
13090 Calls `org-do-promote' or `org-table-move-column', depending on context.
13091 With no specific context, calls the Emacs default `backward-word'.
13092 See the individual commands for more information."
13095 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
13096 ((or (org-on-heading-p) (org-region-active-p))
13097 (call-interactively 'org-do-promote))
13098 ((org-at-item-p) (call-interactively 'org-outdent-item))
13099 (t (call-interactively 'backward-word))))
13101 (defun org-metaright (&optional arg)
13102 "Demote subtree or move table column to right.
13103 Calls `org-do-demote' or `org-table-move-column', depending on context.
13104 With no specific context, calls the Emacs default `forward-word'.
13105 See the individual commands for more information."
13108 ((org-at-table-p) (call-interactively 'org-table-move-column))
13109 ((or (org-on-heading-p) (org-region-active-p))
13110 (call-interactively 'org-do-demote))
13111 ((org-at-item-p) (call-interactively 'org-indent-item))
13112 (t (call-interactively 'forward-word))))
13114 (defun org-metaup (&optional arg)
13115 "Move subtree up or move table row up.
13116 Calls `org-move-subtree-up' or `org-table-move-row' or
13117 `org-move-item-up', depending on context. See the individual commands
13118 for more information."
13121 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
13122 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
13123 ((org-at-item-p) (call-interactively 'org-move-item-up))
13124 (t (transpose-lines 1) (beginning-of-line -1))))
13126 (defun org-metadown (&optional arg)
13127 "Move subtree down or move table row down.
13128 Calls `org-move-subtree-down' or `org-table-move-row' or
13129 `org-move-item-down', depending on context. See the individual
13130 commands for more information."
13133 ((org-at-table-p) (call-interactively 'org-table-move-row))
13134 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
13135 ((org-at-item-p) (call-interactively 'org-move-item-down))
13136 (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
13138 (defun org-shiftup (&optional arg)
13139 "Increase item in timestamp or increase priority of current headline.
13140 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
13141 depending on context. See the individual commands for more information."
13144 ((and org-support-shift-select (org-region-active-p))
13145 (org-call-for-shift-select 'previous-line))
13146 ((org-at-timestamp-p t)
13147 (call-interactively (if org-edit-timestamp-down-means-later
13148 'org-timestamp-down 'org-timestamp-up)))
13149 ((and (not (eq org-support-shift-select 'always))
13150 (org-on-heading-p))
13151 (call-interactively 'org-priority-up))
13152 ((and (not org-support-shift-select) (org-at-item-p))
13153 (call-interactively 'org-previous-item))
13154 ((org-clocktable-try-shift 'up arg))
13155 (org-support-shift-select
13156 (org-call-for-shift-select 'previous-line))
13157 (t (org-shiftselect-error))))
13159 (defun org-shiftdown (&optional arg)
13160 "Decrease item in timestamp or decrease priority of current headline.
13161 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
13162 depending on context. See the individual commands for more information."
13165 ((and org-support-shift-select (org-region-active-p))
13166 (org-call-for-shift-select 'next-line))
13167 ((org-at-timestamp-p t)
13168 (call-interactively (if org-edit-timestamp-down-means-later
13169 'org-timestamp-up 'org-timestamp-down)))
13170 ((and (not (eq org-support-shift-select 'always))
13171 (org-on-heading-p))
13172 (call-interactively 'org-priority-down))
13173 ((and (not org-support-shift-select) (org-at-item-p))
13174 (call-interactively 'org-next-item))
13175 ((org-clocktable-try-shift 'down arg))
13176 (org-support-shift-select
13177 (org-call-for-shift-select 'next-line))
13178 (t (org-shiftselect-error))))
13180 (defun org-shiftright (&optional arg)
13181 "Cycle the thing at point or in the current line, depending on context.
13182 Depending on context, this does one of the following:
13184 - switch a timestamp at point one day into the future
13185 - on a headline, switch to the next TODO keyword.
13186 - on an item, switch entire list to the next bullet type
13187 - on a property line, switch to the next allowed value
13188 - on a clocktable definition line, move time block into the future"
13191 ((and org-support-shift-select (org-region-active-p))
13192 (org-call-for-shift-select 'forward-char))
13193 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
13194 ((and (not (eq org-support-shift-select 'always))
13195 (org-on-heading-p))
13196 (org-call-with-arg 'org-todo 'right))
13197 ((or (and org-support-shift-select
13198 (not (eq org-support-shift-select 'always))
13199 (org-at-item-bullet-p))
13200 (and (not org-support-shift-select) (org-at-item-p)))
13201 (org-call-with-arg 'org-cycle-list-bullet nil))
13202 ((and (not (eq org-support-shift-select 'always))
13203 (org-at-property-p))
13204 (call-interactively 'org-property-next-allowed-value))
13205 ((org-clocktable-try-shift 'right arg))
13206 (org-support-shift-select
13207 (org-call-for-shift-select 'forward-char))
13208 (t (org-shiftselect-error))))
13210 (defun org-shiftleft (&optional arg)
13211 "Cycle the thing at point or in the current line, depending on context.
13212 Depending on context, this does one of the following:
13214 - switch a timestamp at point one day into the past
13215 - on a headline, switch to the previous TODO keyword.
13216 - on an item, switch entire list to the previous bullet type
13217 - on a property line, switch to the previous allowed value
13218 - on a clocktable definition line, move time block into the past"
13221 ((and org-support-shift-select (org-region-active-p))
13222 (org-call-for-shift-select 'backward-char))
13223 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
13224 ((and (not (eq org-support-shift-select 'always))
13225 (org-on-heading-p))
13226 (org-call-with-arg 'org-todo 'left))
13227 ((or (and org-support-shift-select
13228 (not (eq org-support-shift-select 'always))
13229 (org-at-item-bullet-p))
13230 (and (not org-support-shift-select) (org-at-item-p)))
13231 (org-call-with-arg 'org-cycle-list-bullet 'previous))
13232 ((and (not (eq org-support-shift-select 'always))
13233 (org-at-property-p))
13234 (call-interactively 'org-property-previous-allowed-value))
13235 ((org-clocktable-try-shift 'left arg))
13236 (org-support-shift-select
13237 (org-call-for-shift-select 'backward-char))
13238 (t (org-shiftselect-error))))
13240 (defun org-shiftcontrolright ()
13241 "Switch to next TODO set."
13244 ((and org-support-shift-select (org-region-active-p))
13245 (org-call-for-shift-select 'forward-word))
13246 ((and (not (eq org-support-shift-select 'always))
13247 (org-on-heading-p))
13248 (org-call-with-arg 'org-todo 'nextset))
13249 (org-support-shift-select
13250 (org-call-for-shift-select 'forward-word))
13251 (t (org-shiftselect-error))))
13253 (defun org-shiftcontrolleft ()
13254 "Switch to previous TODO set."
13257 ((and org-support-shift-select (org-region-active-p))
13258 (org-call-for-shift-select 'backward-word))
13259 ((and (not (eq org-support-shift-select 'always))
13260 (org-on-heading-p))
13261 (org-call-with-arg 'org-todo 'previousset))
13262 (org-support-shift-select
13263 (org-call-for-shift-select 'backward-word))
13264 (t (org-shiftselect-error))))
13266 (defun org-ctrl-c-ret ()
13267 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
13270 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
13271 (t (call-interactively 'org-insert-heading))))
13273 (defun org-copy-special ()
13274 "Copy region in table or copy current subtree.
13275 Calls `org-table-copy' or `org-copy-subtree', depending on context.
13276 See the individual commands for more information."
13278 (call-interactively
13279 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
13281 (defun org-cut-special ()
13282 "Cut region in table or cut current subtree.
13283 Calls `org-table-copy' or `org-cut-subtree', depending on context.
13284 See the individual commands for more information."
13286 (call-interactively
13287 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
13289 (defun org-paste-special (arg)
13290 "Paste rectangular region into table, or past subtree relative to level.
13291 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
13292 See the individual commands for more information."
13294 (if (org-at-table-p)
13295 (org-table-paste-rectangle)
13296 (org-paste-subtree arg)))
13298 (defun org-edit-special ()
13299 "Call a special editor for the stuff at point.
13300 When at a table, call the formula editor with `org-table-edit-formulas'.
13301 When at the first line of an src example, call `org-edit-src-code'.
13302 When in an #+include line, visit the include file. Otherwise call
13303 `ffap' to visit the file at point."
13307 (call-interactively 'org-table-edit-formulas))
13309 (beginning-of-line 1)
13310 (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
13311 (find-file (org-trim (match-string 1))))
13312 ((org-edit-src-code))
13313 ((org-edit-fixed-width-region))
13314 (t (call-interactively 'ffap))))
13316 (defun org-ctrl-c-ctrl-c (&optional arg)
13317 "Set tags in headline, or update according to changed information at point.
13319 This command does many different things, depending on context:
13321 - If the cursor is in a headline, prompt for tags and insert them
13322 into the current line, aligned to `org-tags-column'. When called
13323 with prefix arg, realign all tags in the current buffer.
13325 - If the cursor is in one of the special #+KEYWORD lines, this
13326 triggers scanning the buffer for these lines and updating the
13329 - If the cursor is inside a table, realign the table. This command
13330 works even if the automatic table editor has been turned off.
13332 - If the cursor is on a #+TBLFM line, re-apply the formulas to
13335 - If the cursor is at a footnote reference or definition, jump to
13336 the corresponding definition or references, respectively.
13338 - If the cursor is a the beginning of a dynamic block, update it.
13340 - If the cursor is inside a table created by the table.el package,
13341 activate that table.
13343 - If the current buffer is a remember buffer, close note and file
13344 it. A prefix argument of 1 files to the default location
13345 without further interaction. A prefix argument of 2 files to
13346 the currently clocking task.
13348 - If the cursor is on a <<<target>>>, update radio targets and corresponding
13349 links in this buffer.
13351 - If the cursor is on a numbered item in a plain list, renumber the
13354 - If the cursor is on a checkbox, toggle it."
13356 (let ((org-enable-table-editor t))
13358 ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
13359 org-occur-highlights
13360 org-latex-fragment-image-overlays)
13361 (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
13362 (org-remove-occur-highlights)
13363 (org-remove-latex-fragment-image-overlays)
13364 (message "Temporary highlights/overlays removed from current buffer"))
13365 ((and (local-variable-p 'org-finish-function (current-buffer))
13366 (fboundp org-finish-function))
13367 (funcall org-finish-function))
13368 ((org-at-property-p)
13369 (call-interactively 'org-property-action))
13370 ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
13371 ((org-on-heading-p) (call-interactively 'org-set-tags))
13372 ((org-at-table.el-p)
13374 (beginning-of-line 1)
13375 (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
13376 (call-interactively 'table-recognize-table))
13378 (org-table-maybe-eval-formula)
13380 (call-interactively 'org-table-recalculate)
13381 (org-table-maybe-recalculate-line))
13382 (call-interactively 'org-table-align))
13383 ((or (org-footnote-at-reference-p)
13384 (org-footnote-at-definition-p))
13385 (call-interactively 'org-footnote-action))
13386 ((org-at-item-checkbox-p)
13387 (call-interactively 'org-toggle-checkbox))
13389 (call-interactively 'org-maybe-renumber-ordered-list))
13390 ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
13392 (beginning-of-line 1)
13393 (save-excursion (org-update-dblock)))
13394 ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
13396 ((equal (match-string 1) "TBLFM")
13397 ;; Recalculate the table before this line
13399 (beginning-of-line 1)
13400 (skip-chars-backward " \r\n\t")
13401 (if (org-at-table-p)
13402 (org-call-with-arg 'org-table-recalculate t))))
13404 ; (org-set-regexps-and-options)
13405 ; (org-restart-font-lock)
13406 (let ((org-inhibit-startup t)) (org-mode-restart))
13407 (message "Local setup has been refreshed"))))
13408 (t (error "C-c C-c can do nothing useful at this location.")))))
13410 (defun org-mode-restart ()
13411 "Restart Org-mode, to scan again for special lines.
13412 Also updates the keyword regular expressions."
13415 (message "Org-mode restarted"))
13417 (defun org-kill-note-or-show-branches ()
13418 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
13420 (if (not org-finish-function)
13421 (call-interactively 'show-branches)
13422 (let ((org-note-abort t))
13423 (funcall org-finish-function))))
13425 (defun org-return (&optional indent)
13426 "Goto next table row or insert a newline.
13427 Calls `org-table-next-row' or `newline', depending on context.
13428 See the individual commands for more information."
13431 ((bobp) (if indent (newline-and-indent) (newline)))
13432 ((and (org-at-heading-p)
13434 (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
13439 (org-table-justify-field-maybe)
13440 (call-interactively 'org-table-next-row))
13441 (t (if indent (newline-and-indent) (newline)))))
13443 (defun org-return-indent ()
13444 "Goto next table row or insert a newline and indent.
13445 Calls `org-table-next-row' or `newline-and-indent', depending on
13446 context. See the individual commands for more information."
13450 (defun org-ctrl-c-star ()
13451 "Compute table, or change heading status of lines.
13452 Calls `org-table-recalculate' or `org-toggle-heading',
13453 depending on context."
13457 (call-interactively 'org-table-recalculate))
13459 ;; Convert all lines in region to list items
13460 (call-interactively 'org-toggle-heading))))
13462 (defun org-ctrl-c-minus ()
13463 "Insert separator line in table or modify bullet status of line.
13464 Also turns a plain line or a region of lines into list items.
13465 Calls `org-table-insert-hline', `org-toggle-item', or
13466 `org-cycle-list-bullet', depending on context."
13470 (call-interactively 'org-table-insert-hline))
13471 ((org-region-active-p)
13472 (call-interactively 'org-toggle-item))
13474 (call-interactively 'org-cycle-list-bullet))
13476 (call-interactively 'org-toggle-item))))
13478 (defun org-toggle-item ()
13479 "Convert headings or normal lines to items, items to normal lines.
13480 If there is no active region, only the current line is considered.
13482 If the first line in the region is a headline, convert all headlines to items.
13484 If the first line in the region is an item, convert all items to normal lines.
13486 If the first line is normal text, add an item bullet to each line."
13488 (let (l2 l beg end)
13489 (if (org-region-active-p)
13490 (setq beg (region-beginning) end (region-end))
13491 (setq beg (point-at-bol)
13492 end (min (1+ (point-at-eol)) (point-max))))
13495 (setq l2 (org-current-line))
13497 (beginning-of-line 1)
13498 (setq l (1- (org-current-line)))
13499 (if (org-at-item-p)
13500 ;; We already have items, de-itemize
13501 (while (< (setq l (1+ l)) l2)
13502 (when (org-at-item-p)
13503 (goto-char (match-beginning 2))
13504 (delete-region (match-beginning 2) (match-end 2))
13505 (and (looking-at "[ \t]+") (replace-match "")))
13506 (beginning-of-line 2))
13507 (if (org-on-heading-p)
13508 ;; Headings, convert to items
13509 (while (< (setq l (1+ l)) l2)
13510 (if (looking-at org-outline-regexp)
13511 (replace-match "- " t t))
13512 (beginning-of-line 2))
13513 ;; normal lines, turn them into items
13514 (while (< (setq l (1+ l)) l2)
13515 (unless (org-at-item-p)
13516 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
13517 (replace-match "\\1- \\2")))
13518 (beginning-of-line 2)))))))
13520 (defun org-toggle-heading (&optional nstars)
13521 "Convert headings to normal text, or items or text to headings.
13522 If there is no active region, only the current line is considered.
13524 If the first line is a heading, remove the stars from all headlines
13527 If the first line is a plain list item, turn all plain list items into
13530 If the first line is a normal line, turn each and every line in the region
13533 When converting a line into a heading, the number of stars is chosen
13534 such that the lines become children of the current entry. However, when
13535 a prefix argument is given, its value determines the number of stars to add."
13537 (let (l2 l itemp beg end)
13538 (if (org-region-active-p)
13539 (setq beg (region-beginning) end (region-end))
13540 (setq beg (point-at-bol)
13541 end (min (1+ (point-at-eol)) (point-max))))
13544 (setq l2 (org-current-line))
13546 (beginning-of-line 1)
13547 (setq l (1- (org-current-line)))
13548 (if (org-on-heading-p)
13549 ;; We already have headlines, de-star them
13550 (while (< (setq l (1+ l)) l2)
13551 (when (org-on-heading-p t)
13552 (and (looking-at outline-regexp) (replace-match "")))
13553 (beginning-of-line 2))
13554 (setq itemp (org-at-item-p))
13557 (make-string (prefix-numeric-value current-prefix-arg)
13560 (re-search-backward org-complex-heading-regexp nil t)
13561 (or (match-string 1) "*"))))
13562 (add-stars (if nstars "" (if org-odd-levels-only "**" "*")))
13563 (rpl (concat stars add-stars " ")))
13564 (while (< (setq l (1+ l)) l2)
13566 (and (org-at-item-p) (replace-match rpl t t))
13567 (unless (org-on-heading-p)
13568 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
13569 (replace-match (concat rpl (match-string 2))))))
13570 (beginning-of-line 2)))))))
13572 (defun org-meta-return (&optional arg)
13573 "Insert a new heading or wrap a region in a table.
13574 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
13575 See the individual commands for more information."
13579 (call-interactively 'org-table-wrap-region))
13580 (t (call-interactively 'org-insert-heading))))
13584 ;; Define the Org-mode menus
13585 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
13587 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
13588 ["Next Field" org-cycle (org-at-table-p)]
13589 ["Previous Field" org-shifttab (org-at-table-p)]
13590 ["Next Row" org-return (org-at-table-p)]
13592 ["Blank Field" org-table-blank-field (org-at-table-p)]
13593 ["Edit Field" org-table-edit-field (org-at-table-p)]
13594 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
13597 ["Move Column Left" org-metaleft (org-at-table-p)]
13598 ["Move Column Right" org-metaright (org-at-table-p)]
13599 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
13600 ["Insert Column" org-shiftmetaright (org-at-table-p)])
13602 ["Move Row Up" org-metaup (org-at-table-p)]
13603 ["Move Row Down" org-metadown (org-at-table-p)]
13604 ["Delete Row" org-shiftmetaup (org-at-table-p)]
13605 ["Insert Row" org-shiftmetadown (org-at-table-p)]
13606 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
13608 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
13610 ["Copy Rectangle" org-copy-special (org-at-table-p)]
13611 ["Cut Rectangle" org-cut-special (org-at-table-p)]
13612 ["Paste Rectangle" org-paste-special (org-at-table-p)]
13613 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
13616 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
13617 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
13618 ["Edit Formulas" org-edit-special (org-at-table-p)]
13620 ["Recalculate line" org-table-recalculate (org-at-table-p)]
13621 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
13622 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
13624 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
13626 ["Sum Column/Rectangle" org-table-sum
13627 (or (org-at-table-p) (org-region-active-p))]
13628 ["Which Column?" org-table-current-column (org-at-table-p)])
13630 org-table-toggle-formula-debugger
13631 :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
13632 ["Show Col/Row Numbers"
13633 org-table-toggle-coordinate-overlays
13635 :selected (org-bound-and-true-p org-table-overlay-coordinates)]
13637 ["Create" org-table-create (and (not (org-at-table-p))
13638 org-enable-table-editor)]
13639 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
13640 ["Import from File" org-table-import (not (org-at-table-p))]
13641 ["Export to File" org-table-export (org-at-table-p)]
13643 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
13645 (easy-menu-define org-org-menu org-mode-map "Org menu"
13648 ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
13649 ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
13650 ["Sparse Tree..." org-sparse-tree t]
13651 ["Reveal Context" org-reveal t]
13652 ["Show All" show-all t]
13654 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
13656 ["New Heading" org-insert-heading t]
13657 ("Navigate Headings"
13658 ["Up" outline-up-heading t]
13659 ["Next" outline-next-visible-heading t]
13660 ["Previous" outline-previous-visible-heading t]
13661 ["Next Same Level" outline-forward-same-level t]
13662 ["Previous Same Level" outline-backward-same-level t]
13664 ["Jump" org-goto t])
13666 ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
13667 ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
13669 ["Copy Subtree" org-copy-special (not (org-at-table-p))]
13670 ["Cut Subtree" org-cut-special (not (org-at-table-p))]
13671 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
13673 ["Promote Heading" org-metaleft (not (org-at-table-p))]
13674 ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
13675 ["Demote Heading" org-metaright (not (org-at-table-p))]
13676 ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
13678 ["Sort Region/Children" org-sort (not (org-at-table-p))]
13680 ["Convert to odd levels" org-convert-to-odd-levels t]
13681 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
13683 ["Emphasis..." org-emphasize t]
13684 ["Edit Source Example" org-edit-special t]
13686 ["Footnote new/jump" org-footnote-action t]
13687 ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
13689 ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
13690 ; ["Check and Tag Children" (org-toggle-archive-tag (4))
13691 ; :active t :keys "C-u C-c C-x C-a"]
13692 ["Sparse trees open ARCHIVE trees"
13693 (setq org-sparse-tree-open-archived-trees
13694 (not org-sparse-tree-open-archived-trees))
13695 :style toggle :selected org-sparse-tree-open-archived-trees]
13696 ["Cycling opens ARCHIVE trees"
13697 (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
13698 :style toggle :selected org-cycle-open-archived-trees]
13700 ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
13701 ["Move Subtree to Archive" org-advertized-archive-subtree t]
13702 ; ["Check and Move Children" (org-archive-subtree '(4))
13703 ; :active t :keys "C-u C-c C-x C-s"]
13707 ["TODO/DONE/-" org-todo t]
13709 ["Next keyword" org-shiftright (org-on-heading-p)]
13710 ["Previous keyword" org-shiftleft (org-on-heading-p)]
13711 ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
13712 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
13713 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
13714 ["Show TODO Tree" org-show-todo-tree t]
13715 ["Global TODO list" org-todo-list t]
13717 ["Set Priority" org-priority t]
13718 ["Priority Up" org-shiftup t]
13719 ["Priority Down" org-shiftdown t])
13720 ("TAGS and Properties"
13721 ["Set Tags" org-set-tags-command t]
13722 ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
13724 ["Set property" org-set-property t]
13725 ["Column view of properties" org-columns t]
13726 ["Insert Column View DBlock" org-insert-columns-dblock t])
13727 ("Dates and Scheduling"
13728 ["Timestamp" org-time-stamp t]
13729 ["Timestamp (inactive)" org-time-stamp-inactive t]
13731 ["1 Day Later" org-shiftright t]
13732 ["1 Day Earlier" org-shiftleft t]
13733 ["1 ... Later" org-shiftup t]
13734 ["1 ... Earlier" org-shiftdown t])
13735 ["Compute Time Range" org-evaluate-time-range t]
13736 ["Schedule Item" org-schedule t]
13737 ["Deadline" org-deadline t]
13739 ["Custom time format" org-toggle-time-stamp-overlays
13740 :style radio :selected org-display-custom-times]
13742 ["Goto Calendar" org-goto-calendar t]
13743 ["Date from Calendar" org-date-from-calendar t]
13745 ["Start/Restart Timer" org-timer-start t]
13746 ["Pause/Continue Timer" org-timer-pause-or-continue t]
13747 ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
13748 ["Insert Timer String" org-timer t]
13749 ["Insert Timer Item" org-timer-item t])
13751 ["Clock in" org-clock-in t]
13752 ["Clock out" org-clock-out t]
13753 ["Clock cancel" org-clock-cancel t]
13754 ["Goto running clock" org-clock-goto t]
13755 ["Display times" org-clock-display t]
13756 ["Create clock table" org-clock-report t]
13758 ["Record DONE time"
13759 (progn (setq org-log-done (not org-log-done))
13760 (message "Switching to %s will %s record a timestamp"
13761 (car org-done-keywords)
13762 (if org-log-done "automatically" "not")))
13763 :style toggle :selected org-log-done])
13765 ["Agenda Command..." org-agenda t]
13766 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
13767 ("File List for Agenda")
13768 ("Special views current file"
13769 ["TODO Tree" org-show-todo-tree t]
13770 ["Check Deadlines" org-check-deadlines t]
13771 ["Timeline" org-timeline t]
13772 ["Tags Tree" org-tags-sparse-tree t])
13775 ["Store Link (Global)" org-store-link t]
13776 ["Insert Link" org-insert-link t]
13777 ["Follow Link" org-open-at-point t]
13779 ["Next link" org-next-link t]
13780 ["Previous link" org-previous-link t]
13782 ["Descriptive Links"
13783 (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
13785 :selected (member '(org-link) buffer-invisibility-spec)]
13788 (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
13790 :selected (not (member '(org-link) buffer-invisibility-spec))])
13792 ["Export/Publish..." org-export t]
13794 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
13795 :selected org-cdlatex-mode]
13796 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
13797 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
13798 ["Modify math symbol" org-cdlatex-math-modify
13799 (org-inside-LaTeX-fragment-p)]
13800 ["Export LaTeX fragments as images"
13801 (if (featurep 'org-exp)
13802 (setq org-export-with-LaTeX-fragments
13803 (not org-export-with-LaTeX-fragments))
13804 (require 'org-exp))
13805 :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
13806 org-export-with-LaTeX-fragments)])
13809 ["Show Version" org-version t]
13810 ["Info Documentation" org-info t])
13812 ["Browse Org Group" org-customize t]
13814 ["Expand This Menu" org-create-customize-menu
13815 (fboundp 'customize-menu-create)])
13817 ["Refresh setup" org-mode-restart t]
13820 (defun org-info (&optional node)
13821 "Read documentation for Org-mode in the info system.
13822 With optional NODE, go directly to that node."
13824 (info (format "(org)%s" (or node ""))))
13826 (defun org-install-agenda-files-menu ()
13827 (let ((bl (buffer-list)))
13830 (set-buffer (pop bl))
13831 (if (org-mode-p) (setq bl nil)))
13834 '("Org") "File List for Agenda"
13837 ["Edit File List" (org-edit-agenda-file-list) t]
13838 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
13839 ["Remove Current File from List" org-remove-file t]
13840 ["Cycle through agenda files" org-cycle-agenda-files t]
13841 ["Occur in all agenda files" org-occur-in-agenda-files t]
13843 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
13848 (defun org-require-autoloaded-modules ()
13851 '(org-agenda org-archive org-clock org-colview
13852 org-exp org-id org-export-latex org-publish
13853 org-remember org-table)))
13856 (defun org-customize ()
13857 "Call the customize function with org as argument."
13859 (org-load-modules-maybe)
13860 (org-require-autoloaded-modules)
13861 (customize-browse 'org))
13863 (defun org-create-customize-menu ()
13864 "Create a full customization menu for Org-mode, insert it into the menu."
13866 (org-load-modules-maybe)
13867 (org-require-autoloaded-modules)
13868 (if (fboundp 'customize-menu-create)
13871 '("Org") "Customize"
13872 `(["Browse Org group" org-customize t]
13874 ,(customize-menu-create 'org)
13875 ["Set" Custom-set t]
13876 ["Save" Custom-save t]
13877 ["Reset to Current" Custom-reset-current t]
13878 ["Reset to Saved" Custom-reset-saved t]
13879 ["Reset to Standard Settings" Custom-reset-standard t]))
13880 (message "\"Org\"-menu now contains full customization menu"))
13881 (error "Cannot expand menu (outdated version of cus-edit.el)")))
13883 ;;;; Miscellaneous stuff
13885 ;;; Generally useful functions
13887 (defun org-find-text-property-in-string (prop s)
13888 "Return the first non-nil value of property PROP in string S."
13889 (or (get-text-property 0 prop s)
13890 (get-text-property (or (next-single-property-change 0 prop s) 0)
13893 (defun org-display-warning (message) ;; Copied from Emacs-Muse
13894 "Display the given MESSAGE as a warning."
13895 (if (fboundp 'display-warning)
13896 (display-warning 'org message
13897 (if (featurep 'xemacs)
13900 (let ((buf (get-buffer-create "*Org warnings*")))
13901 (with-current-buffer buf
13902 (goto-char (point-max))
13903 (insert "Warning (Org): " message)
13906 (display-buffer buf)
13909 (defun org-goto-marker-or-bmk (marker &optional bookmark)
13910 "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
13911 (if (and marker (marker-buffer marker)
13912 (buffer-live-p (marker-buffer marker)))
13914 (switch-to-buffer (marker-buffer marker))
13915 (if (or (> marker (point-max)) (< marker (point-min)))
13918 (org-show-context 'org-goto))
13920 (bookmark-jump bookmark)
13921 (error "Cannot find location"))))
13923 (defun org-quote-csv-field (s)
13924 "Quote field for inclusion in CSV material."
13925 (if (string-match "[\",]" s)
13926 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
13929 (defun org-plist-delete (plist property)
13930 "Delete PROPERTY from PLIST.
13931 This is in contrast to merely setting it to 0."
13934 (if (not (eq property (car plist)))
13935 (setq p (plist-put p (car plist) (nth 1 plist))))
13936 (setq plist (cddr plist)))
13939 (defun org-force-self-insert (N)
13940 "Needed to enforce self-insert under remapping."
13942 (self-insert-command N))
13944 (defun org-string-width (s)
13945 "Compute width of string, ignoring invisible characters.
13946 This ignores character with invisibility property `org-link', and also
13947 characters with property `org-cwidth', because these will become invisible
13948 upon the next fontification round."
13950 (when (or (eq t buffer-invisibility-spec)
13951 (assq 'org-link buffer-invisibility-spec))
13952 (while (setq b (text-property-any 0 (length s)
13953 'invisible 'org-link s))
13954 (setq s (concat (substring s 0 b)
13955 (substring s (or (next-single-property-change
13956 b 'invisible s) (length s)))))))
13957 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
13958 (setq s (concat (substring s 0 b)
13959 (substring s (or (next-single-property-change
13960 b 'org-cwidth s) (length s))))))
13961 (setq l (string-width s) b -1)
13962 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
13963 (setq l (- l (get-text-property b 'org-dwidth-n s))))
13966 (defun org-get-indentation (&optional line)
13967 "Get the indentation of the current line, interpreting tabs.
13968 When LINE is given, assume it represents a line and compute its indentation."
13970 (if (string-match "^ *" (org-remove-tabs line))
13973 (beginning-of-line 1)
13974 (skip-chars-forward " \t")
13975 (current-column))))
13977 (defun org-remove-tabs (s &optional width)
13978 "Replace tabulators in S with spaces.
13979 Assumes that s is a single line, starting in column 0."
13980 (setq width (or width tab-width))
13981 (while (string-match "\t" s)
13982 (setq s (replace-match
13984 (- (* width (/ (+ (match-beginning 0) width) width))
13985 (match-beginning 0)) ?\ )
13989 (defun org-fix-indentation (line ind)
13990 "Fix indentation in LINE.
13991 IND is a cons cell with target and minimum indentation.
13992 If the current indentation in LINE is smaller than the minimum,
13993 leave it alone. If it is larger than ind, set it to the target."
13994 (let* ((l (org-remove-tabs line))
13995 (i (org-get-indentation l))
13996 (i1 (car ind)) (i2 (cdr ind)))
13997 (if (>= i i2) (setq l (substring line i2)))
13999 (concat (make-string i1 ?\ ) l)
14002 (defun org-base-buffer (buffer)
14003 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
14006 (or (buffer-base-buffer buffer)
14009 (defun org-trim (s)
14010 "Remove whitespace at beginning and end of string."
14011 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
14012 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
14015 (defun org-wrap (string &optional width lines)
14016 "Wrap string to either a number of lines, or a width in characters.
14017 If WIDTH is non-nil, the string is wrapped to that width, however many lines
14018 that costs. If there is a word longer than WIDTH, the text is actually
14019 wrapped to the length of that word.
14020 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
14021 many lines, whatever width that takes.
14022 The return value is a list of lines, without newlines at the end."
14023 (let* ((words (org-split-string string "[ \t\n]+"))
14024 (maxword (apply 'max (mapcar 'org-string-width words)))
14027 (org-do-wrap words (max maxword width)))
14030 (setq ll (org-do-wrap words maxword))
14031 (if (<= (length ll) lines)
14034 (while (> (length ll) lines)
14036 (setq ll (org-do-wrap words w)))
14038 (t (error "Cannot wrap this")))))
14040 (defun org-do-wrap (words width)
14041 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
14044 (setq line (pop words))
14045 (while (and words (< (+ (length line) (length (car words))) width))
14046 (setq line (concat line " " (pop words))))
14047 (setq lines (push line lines)))
14050 (defun org-split-string (string &optional separators)
14051 "Splits STRING into substrings at SEPARATORS.
14052 No empty strings are returned if there are matches at the beginning
14053 and end of string."
14054 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
14058 (while (and (string-match rexp string
14060 (= start (match-beginning 0))
14061 (< start (length string)))
14063 (< (match-beginning 0) (length string)))
14065 (or (eq (match-beginning 0) 0)
14066 (and (eq (match-beginning 0) (match-end 0))
14067 (eq (match-beginning 0) start))
14069 (cons (substring string start (match-beginning 0))
14071 (setq start (match-end 0)))
14072 (or (eq start (length string))
14074 (cons (substring string start)
14078 (defun org-context ()
14079 "Return a list of contexts of the current cursor position.
14080 If several contexts apply, all are returned.
14081 Each context entry is a list with a symbol naming the context, and
14082 two positions indicating start and end of the context. Possible
14085 :headline anywhere in a headline
14086 :headline-stars on the leading stars in a headline
14087 :todo-keyword on a TODO keyword (including DONE) in a headline
14088 :tags on the TAGS in a headline
14089 :priority on the priority cookie in a headline
14090 :item on the first line of a plain list item
14091 :item-bullet on the bullet/number of a plain list item
14092 :checkbox on the checkbox in a plain list item
14093 :table in an org-mode table
14094 :table-special on a special filed in a table
14095 :table-table in a table.el table
14096 :link on a hyperlink
14097 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
14098 :target on a <<target>>
14099 :radio-target on a <<<radio-target>>>
14100 :latex-fragment on a LaTeX fragment
14101 :latex-preview on a LaTeX fragment with overlayed preview image
14103 This function expects the position to be visible because it uses font-lock
14104 faces as a help to recognize the following contexts: :table-special, :link,
14106 (let* ((f (get-text-property (point) 'face))
14107 (faces (if (listp f) f (list f)))
14108 (p (point)) clist o)
14109 ;; First the large context
14111 ((org-on-heading-p t)
14112 (push (list :headline (point-at-bol) (point-at-eol)) clist)
14114 (beginning-of-line 1)
14115 (looking-at org-todo-line-tags-regexp))
14116 (push (org-point-in-group p 1 :headline-stars) clist)
14117 (push (org-point-in-group p 2 :todo-keyword) clist)
14118 (push (org-point-in-group p 4 :tags) clist))
14120 (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
14121 (if (looking-at "\\[#[A-Z0-9]\\]")
14122 (push (org-point-in-group p 0 :priority) clist)))
14125 (push (org-point-in-group p 2 :item-bullet) clist)
14126 (push (list :item (point-at-bol)
14127 (save-excursion (org-end-of-item) (point)))
14129 (and (org-at-item-checkbox-p)
14130 (push (org-point-in-group p 0 :checkbox) clist)))
14133 (push (list :table (org-table-begin) (org-table-end)) clist)
14134 (if (memq 'org-formula faces)
14135 (push (list :table-special
14136 (previous-single-property-change p 'face)
14137 (next-single-property-change p 'face)) clist)))
14138 ((org-at-table-p 'any)
14139 (push (list :table-table) clist)))
14142 ;; Now the small context
14144 ((org-at-timestamp-p)
14145 (push (org-point-in-group p 0 :timestamp) clist))
14146 ((memq 'org-link faces)
14148 (previous-single-property-change p 'face)
14149 (next-single-property-change p 'face)) clist))
14150 ((memq 'org-special-keyword faces)
14151 (push (list :keyword
14152 (previous-single-property-change p 'face)
14153 (next-single-property-change p 'face)) clist))
14155 (push (org-point-in-group p 0 :target) clist)
14156 (goto-char (1- (match-beginning 0)))
14157 (if (looking-at org-radio-target-regexp)
14158 (push (org-point-in-group p 0 :radio-target) clist))
14160 ((setq o (car (delq nil
14163 (if (memq x org-latex-fragment-image-overlays) x))
14164 (org-overlays-at (point))))))
14165 (push (list :latex-fragment
14166 (org-overlay-start o) (org-overlay-end o)) clist)
14167 (push (list :latex-preview
14168 (org-overlay-start o) (org-overlay-end o)) clist))
14169 ((org-inside-LaTeX-fragment-p)
14170 ;; FIXME: positions wrong.
14171 (push (list :latex-fragment (point) (point)) clist)))
14173 (setq clist (nreverse (delq nil clist)))
14176 ;; FIXME: Compare with at-regexp-p Do we need both?
14177 (defun org-in-regexp (re &optional nlines visually)
14178 "Check if point is inside a match of regexp.
14179 Normally only the current line is checked, but you can include NLINES extra
14180 lines both before and after point into the search.
14181 If VISUALLY is set, require that the cursor is not after the match but
14182 really on, so that the block visually is on the match."
14184 (let ((pos (point))
14185 (eol (point-at-eol (+ 1 (or nlines 0))))
14186 (inc (if visually 1 0)))
14188 (beginning-of-line (- 1 (or nlines 0)))
14189 (while (re-search-forward re eol t)
14190 (if (and (<= (match-beginning 0) pos)
14191 (>= (+ inc (match-end 0)) pos))
14192 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
14194 (defun org-at-regexp-p (regexp)
14195 "Is point inside a match of REGEXP in the current line?"
14198 (let ((pos (point)) (end (point-at-eol)))
14199 (beginning-of-line 1)
14200 (while (re-search-forward regexp end t)
14201 (if (and (<= (match-beginning 0) pos)
14202 (>= (match-end 0) pos))
14206 (defun org-occur-in-agenda-files (regexp &optional nlines)
14207 "Call `multi-occur' with buffers for all agenda files."
14208 (interactive "sOrg-files matching: \np")
14209 (let* ((files (org-agenda-files))
14210 (tnames (mapcar 'file-truename files))
14211 (extra org-agenda-text-search-extra-files)
14213 (when (eq (car extra) 'agenda-archives)
14214 (setq extra (cdr extra))
14215 (setq files (org-add-archive-files files)))
14216 (while (setq f (pop extra))
14217 (unless (member (file-truename f) tnames)
14218 (add-to-list 'files f 'append)
14219 (add-to-list 'tnames (file-truename f) 'append)))
14221 (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
14224 (if (boundp 'occur-mode-find-occurrence-hook)
14226 (add-hook 'occur-mode-find-occurrence-hook
14231 (defadvice occur-mode-goto-occurrence
14232 (after org-occur-reveal activate)
14233 (and (org-mode-p) (org-reveal)))
14234 (defadvice occur-mode-goto-occurrence-other-window
14235 (after org-occur-reveal activate)
14236 (and (org-mode-p) (org-reveal)))
14237 (defadvice occur-mode-display-occurrence
14238 (after org-occur-reveal activate)
14240 (let ((pos (occur-mode-find-occurrence)))
14241 (with-current-buffer (marker-buffer pos)
14246 (defun org-uniquify (list)
14247 "Remove duplicate elements from LIST."
14249 (mapc (lambda (x) (add-to-list 'res x 'append)) list)
14252 (defun org-delete-all (elts list)
14253 "Remove all elements in ELTS from LIST."
14255 (setq list (delete (pop elts) list)))
14258 (defun org-back-over-empty-lines ()
14259 "Move backwards over whitespace, to the beginning of the first empty line.
14260 Returns the number of empty lines passed."
14261 (let ((pos (point)))
14262 (skip-chars-backward " \t\n\r")
14263 (beginning-of-line 2)
14264 (goto-char (min (point) pos))
14265 (count-lines (point) pos)))
14267 (defun org-skip-whitespace ()
14268 (skip-chars-forward " \t\n\r"))
14270 (defun org-point-in-group (point group &optional context)
14271 "Check if POINT is in match-group GROUP.
14272 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
14273 match. If the match group does ot exist or point is not inside it,
14275 (and (match-beginning group)
14276 (>= point (match-beginning group))
14277 (<= point (match-end group))
14279 (list context (match-beginning group) (match-end group))
14282 (defun org-switch-to-buffer-other-window (&rest args)
14283 "Switch to buffer in a second window on the current frame.
14284 In particular, do not allow pop-up frames."
14285 (let (pop-up-frames special-display-buffer-names special-display-regexps
14286 special-display-function)
14287 (apply 'switch-to-buffer-other-window args)))
14289 (defun org-combine-plists (&rest plists)
14290 "Create a single property list from all plists in PLISTS.
14291 The process starts by copying the first list, and then setting properties
14292 from the other lists. Settings in the last list are the most significant
14293 ones and overrule settings in the other lists."
14294 (let ((rtn (copy-sequence (pop plists)))
14297 (setq ls (pop plists))
14299 (setq p (pop ls) v (pop ls))
14300 (setq rtn (plist-put rtn p v))))
14303 (defun org-move-line-down (arg)
14304 "Move the current line down. With prefix argument, move it past ARG lines."
14306 (let ((col (current-column))
14308 (beginning-of-line 1) (setq beg (point))
14309 (beginning-of-line 2) (setq end (point))
14310 (beginning-of-line (+ 1 arg))
14311 (setq pos (move-marker (make-marker) (point)))
14312 (insert (delete-and-extract-region beg end))
14314 (org-move-to-column col)))
14316 (defun org-move-line-up (arg)
14317 "Move the current line up. With prefix argument, move it past ARG lines."
14319 (let ((col (current-column))
14321 (beginning-of-line 1) (setq beg (point))
14322 (beginning-of-line 2) (setq end (point))
14323 (beginning-of-line (- arg))
14324 (setq pos (move-marker (make-marker) (point)))
14325 (insert (delete-and-extract-region beg end))
14327 (org-move-to-column col)))
14329 (defun org-replace-escapes (string table)
14330 "Replace %-escapes in STRING with values in TABLE.
14331 TABLE is an association list with keys like \"%a\" and string values.
14332 The sequences in STRING may contain normal field width and padding information,
14333 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
14334 so values can contain further %-escapes if they are define later in TABLE."
14335 (let ((case-fold-search nil)
14337 (while (setq e (pop table))
14338 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
14339 (while (string-match re string)
14340 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
14342 (setq string (replace-match rpl t t string))))
14346 (defun org-sublist (list start end)
14347 "Return a section of LIST, from START to END.
14348 Counting starts at 1."
14349 (let (rtn (c start))
14350 (setq list (nthcdr (1- start) list))
14351 (while (and list (<= c end))
14352 (push (pop list) rtn)
14356 (defun org-find-base-buffer-visiting (file)
14357 "Like `find-buffer-visiting' but alway return the base buffer and
14358 not an indirect buffer."
14359 (let ((buf (find-buffer-visiting file)))
14361 (or (buffer-base-buffer buf) buf)
14364 (defun org-image-file-name-regexp (&optional extensions)
14365 "Return regexp matching the file names of images.
14366 If EXTENSIONS is given, only match these."
14367 (if (and (not extensions) (fboundp 'image-file-name-regexp))
14368 (image-file-name-regexp)
14369 (let ((image-file-name-extensions
14371 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
14372 "xbm" "xpm" "pbm" "pgm" "ppm"))))
14374 (regexp-opt (nconc (mapcar 'upcase
14375 image-file-name-extensions)
14376 image-file-name-extensions)
14380 (defun org-file-image-p (file &optional extensions)
14381 "Return non-nil if FILE is an image."
14383 (string-match (org-image-file-name-regexp extensions) file)))
14385 (defun org-get-cursor-date ()
14386 "Return the date at cursor in as a time.
14387 This works in the calendar and in the agenda, anywhere else it just
14388 returns the current time."
14389 (let (date day defd)
14391 ((eq major-mode 'calendar-mode)
14392 (setq date (calendar-cursor-to-date)
14393 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
14394 ((eq major-mode 'org-agenda-mode)
14395 (setq day (get-text-property (point) 'day))
14397 (setq date (calendar-gregorian-from-absolute day)
14398 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
14400 (or defd (current-time))))
14402 (defvar org-agenda-action-marker (make-marker)
14403 "Marker pointing to the entry for the next agenda action.")
14405 (defun org-mark-entry-for-agenda-action ()
14406 "Mark the current entry as target of an agenda action.
14407 Agenda actions are actions executed from the agenda with the key `k',
14408 which make use of the date at the cursor."
14410 (move-marker org-agenda-action-marker
14411 (save-excursion (org-back-to-heading t) (point))
14414 "Entry marked for action; press `k' at desired date in agenda or calendar"))
14416 ;;; Paragraph filling stuff.
14417 ;; We want this to be just right, so use the full arsenal.
14419 (defun org-indent-line-function ()
14420 "Indent line like previous, but further if previous was headline or item."
14422 (let* ((pos (point))
14423 (itemp (org-at-item-p))
14424 column bpos bcol tpos tcol bullet btype bullet-type)
14425 ;; Find the previous relevant line
14426 (beginning-of-line 1)
14428 ((looking-at "#") (setq column 0))
14429 ((looking-at "\\*+ ") (setq column 0))
14431 (beginning-of-line 0)
14432 (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
14433 (beginning-of-line 0))
14435 ((looking-at "\\*+[ \t]+")
14436 (if (not org-adapt-indentation)
14438 (goto-char (match-end 0))
14439 (setq column (current-column))))
14441 (org-beginning-of-item)
14442 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
14443 (setq bpos (match-beginning 1) tpos (match-end 0)
14444 bcol (progn (goto-char bpos) (current-column))
14445 tcol (progn (goto-char tpos) (current-column))
14446 bullet (match-string 1)
14447 bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
14448 (if (> tcol (+ bcol org-description-max-indent))
14449 (setq tcol (+ bcol 5)))
14453 (beginning-of-line 1)
14454 (if (looking-at "\\S-")
14456 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
14457 (setq bullet (match-string 1)
14458 btype (if (string-match "[0-9]" bullet) "n" bullet))
14459 (setq column (if (equal btype bullet-type) bcol tcol)))
14460 (setq column (org-get-indentation)))))
14461 (t (setq column (org-get-indentation))))))
14463 (if (<= (current-column) (current-indentation))
14464 (org-indent-line-to column)
14465 (save-excursion (org-indent-line-to column)))
14466 (setq column (current-column))
14467 (beginning-of-line 1)
14469 "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
14470 (replace-match (concat "\\1" (format org-property-format
14471 (match-string 2) (match-string 3)))
14473 (org-move-to-column column)))
14475 (defun org-set-autofill-regexps ()
14477 ;; In the paragraph separator we include headlines, because filling
14478 ;; text in a line directly attached to a headline would otherwise
14479 ;; fill the headline as well.
14480 (org-set-local 'comment-start-skip "^#+[ \t]*")
14481 (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
14482 ;; The paragraph starter includes hand-formatted lists.
14483 (org-set-local 'paragraph-start
14484 "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
14485 ;; Inhibit auto-fill for headers, tables and fixed-width lines.
14486 ;; But only if the user has not turned off tables or fixed-width regions
14488 'auto-fill-inhibit-regexp
14489 (concat "\\*+ \\|#\\+"
14490 "\\|[ \t]*" org-keyword-time-regexp
14491 (if (or org-enable-table-editor org-enable-fixed-width-editor)
14494 (if org-enable-table-editor "|" "")
14495 (if org-enable-fixed-width-editor ":" "")
14497 ;; We use our own fill-paragraph function, to make sure that tables
14498 ;; and fixed-width regions are not wrapped. That function will pass
14499 ;; through to `fill-paragraph' when appropriate.
14500 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
14501 ; Adaptive filling: To get full control, first make sure that
14502 ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
14503 (org-set-local 'adaptive-fill-regexp "\000")
14504 (org-set-local 'adaptive-fill-function
14505 'org-adaptive-fill-function)
14507 'align-mode-rules-list
14508 '((org-in-buffer-settings
14509 (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
14510 (modes . '(org-mode))))))
14512 (defun org-fill-paragraph (&optional justify)
14513 "Re-align a table, pass through to fill-paragraph if no table."
14514 (let ((table-p (org-at-table-p))
14515 (table.el-p (org-at-table.el-p)))
14516 (cond ((and (equal (char-after (point-at-bol)) ?*)
14517 (save-excursion (goto-char (point-at-bol))
14518 (looking-at outline-regexp)))
14519 t) ; skip headlines
14520 (table.el-p t) ; skip table.el tables
14521 (table-p (org-table-align) t) ; align org-mode tables
14522 (t nil)))) ; call paragraph-fill
14524 ;; For reference, this is the default value of adaptive-fill-regexp
14525 ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
14527 (defun org-adaptive-fill-function ()
14528 "Return a fill prefix for org-mode files.
14529 In particular, this makes sure hanging paragraphs for hand-formatted lists
14531 (cond ((looking-at "#[ \t]+")
14533 ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
14535 (if (> (match-end 1) (+ (match-beginning 1)
14536 org-description-max-indent))
14537 (goto-char (+ (match-beginning 1) 5))
14538 (goto-char (match-end 0)))
14539 (make-string (current-column) ?\ )))
14540 ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
14542 (goto-char (match-end 0))
14543 (make-string (current-column) ?\ )))
14548 (defun org-toggle-fixed-width-section (arg)
14549 "Toggle the fixed-width export.
14550 If there is no active region, the QUOTE keyword at the current headline is
14551 inserted or removed. When present, it causes the text between this headline
14552 and the next to be exported as fixed-width text, and unmodified.
14553 If there is an active region, this command adds or removes a colon as the
14554 first character of this line. If the first character of a line is a colon,
14555 this line is also exported in fixed-width font."
14558 (regionp (org-region-active-p))
14559 (beg (if regionp (region-beginning) (point)))
14560 (end (if regionp (region-end)))
14561 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
14562 (case-fold-search nil)
14563 (re "[ \t]*\\(:\\)")
14568 (setq cc (current-column))
14569 (beginning-of-line 1)
14570 (setq off (looking-at re))
14571 (while (> nlines 0)
14572 (setq nlines (1- nlines))
14573 (beginning-of-line 1)
14576 (org-move-to-column cc t)
14579 ((and off (looking-at re))
14580 (replace-match "" t t nil 1))
14581 ((not off) (org-move-to-column cc t) (insert ":")))
14584 (org-back-to-heading)
14585 (if (looking-at (concat outline-regexp
14586 "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
14587 (replace-match "" t t nil 1)
14588 (if (looking-at outline-regexp)
14590 (goto-char (match-end 0))
14591 (insert org-quote-string " "))))))))
14593 ;;;; Functions extending outline functionality
14595 (defun org-beginning-of-line (&optional arg)
14596 "Go to the beginning of the current line. If that is invisible, continue
14597 to a visible line beginning. This makes the function of C-a more intuitive.
14598 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
14599 first attempt, and only move to after the tags when the cursor is already
14600 beyond the end of the headline."
14602 (let ((pos (point)) refpos)
14603 (beginning-of-line 1)
14607 (if (org-invisible-p)
14608 (while (and (not (bobp)) (org-invisible-p))
14610 (beginning-of-line 1))
14612 (when org-special-ctrl-a/e
14614 ((and (looking-at org-complex-heading-regexp)
14615 (= (char-after (match-end 1)) ?\ ))
14616 (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
14619 (if (eq org-special-ctrl-a/e t)
14620 (cond ((> pos refpos) refpos)
14621 ((= pos (point)) refpos)
14623 (cond ((> pos (point)) (point))
14624 ((not (eq last-command this-command)) (point))
14628 (if (eq org-special-ctrl-a/e t)
14629 (cond ((> pos (match-end 4)) (match-end 4))
14630 ((= pos (point)) (match-end 4))
14632 (cond ((> pos (point)) (point))
14633 ((not (eq last-command this-command)) (point))
14634 (t (match-end 4))))))))
14636 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
14638 (defun org-end-of-line (&optional arg)
14639 "Go to the end of the line.
14640 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
14641 first attempt, and only move to after the tags when the cursor is already
14642 beyond the end of the headline."
14644 (if (or (not org-special-ctrl-a/e)
14645 (not (org-on-heading-p)))
14647 (let ((pos (point)))
14648 (beginning-of-line 1)
14649 (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
14650 (if (eq org-special-ctrl-a/e t)
14651 (if (or (< pos (match-beginning 1))
14652 (= pos (match-end 0)))
14653 (goto-char (match-beginning 1))
14654 (goto-char (match-end 0)))
14655 (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
14656 (goto-char (match-end 0))
14657 (goto-char (match-beginning 1))))
14658 (end-of-line arg))))
14660 (and (featurep 'xemacs) (setq zmacs-region-stays t))))
14663 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
14664 (define-key org-mode-map "\C-e" 'org-end-of-line)
14666 (defun org-kill-line (&optional arg)
14667 "Kill line, to tags or end of line."
14670 ((or (not org-special-ctrl-k)
14672 (not (org-on-heading-p)))
14673 (call-interactively 'kill-line))
14674 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
14675 (kill-region (point) (match-beginning 1))
14676 (org-set-tags nil t))
14677 (t (kill-region (point) (point-at-eol)))))
14679 (define-key org-mode-map "\C-k" 'org-kill-line)
14681 (defun org-yank (&optional arg)
14682 "Yank. If the kill is a subtree, treat it specially.
14683 This command will look at the current kill and check if is a single
14684 subtree, or a series of subtrees[1]. If it passes the test, and if the
14685 cursor is at the beginning of a line or after the stars of a currently
14686 empty headline, then the yank is handled specially. How exactly depends
14687 on the value of the following variables, both set by default.
14689 org-yank-folded-subtrees
14690 When set, the subtree(s) will be folded after insertion, but only
14691 if doing so would now swallow text after the yanked text.
14693 org-yank-adjusted-subtrees
14694 When set, the subtree will be promoted or demoted in order to
14695 fit into the local outline tree structure, which means that the level
14696 will be adjusted so that it becomes the smaller one of the two
14697 *visible* surrounding headings.
14699 Any prefix to this command will cause `yank' to be called directly with
14700 no special treatment. In particular, a simple `C-u' prefix will just
14701 plainly yank the text as it is.
14703 \[1] Basically, the test checks if the first non-white line is a heading
14704 and if there are no other headings with fewer stars."
14706 (setq this-command 'yank)
14708 (call-interactively 'yank)
14709 (let ((subtreep ; is kill a subtree, and the yank position appropriate?
14710 (and (org-kill-is-subtree-p)
14712 (and (looking-at "[ \t]*$")
14715 (buffer-substring (point-at-bol) (point)))))))
14718 ((and subtreep org-yank-folded-subtrees)
14719 (let ((beg (point))
14721 (if (and subtreep org-yank-adjusted-subtrees)
14722 (org-paste-subtree nil nil 'for-yank)
14723 (call-interactively 'yank))
14726 (when (and (bolp) subtreep
14727 (not (setq swallowp
14728 (org-yank-folding-would-swallow-text beg end))))
14729 (or (looking-at outline-regexp)
14730 (re-search-forward (concat "^" outline-regexp) end t))
14731 (while (and (< (point) end) (looking-at outline-regexp))
14733 (org-cycle-show-empty-lines 'folded)
14734 (condition-case nil
14735 (outline-forward-same-level 1)
14736 (error (goto-char end)))))
14739 "Yanked text not folded because that would swallow text"))
14741 (skip-chars-forward " \t\n\r")
14742 (beginning-of-line 1)
14743 (push-mark beg 'nomsg)))
14744 ((and subtreep org-yank-adjusted-subtrees)
14745 (let ((beg (point-at-bol)))
14746 (org-paste-subtree nil nil 'for-yank)
14747 (push-mark beg 'nomsg)))
14749 (call-interactively 'yank))))))
14751 (defun org-yank-folding-would-swallow-text (beg end)
14752 "Would hide-subtree at BEG swallow any text after END?"
14756 (when (or (looking-at outline-regexp)
14757 (re-search-forward (concat "^" outline-regexp) end t))
14758 (setq level (org-outline-level)))
14760 (skip-chars-forward " \t\r\n\v\f")
14762 (and (bolp) (looking-at org-outline-regexp)
14763 (<= (org-outline-level) level)))
14764 nil ; Nothing would be swallowed
14765 t)))) ; something would swallow
14767 (define-key org-mode-map "\C-y" 'org-yank)
14769 (defun org-invisible-p ()
14770 "Check if point is at a character currently not visible."
14771 ;; Early versions of noutline don't have `outline-invisible-p'.
14772 (if (fboundp 'outline-invisible-p)
14773 (outline-invisible-p)
14774 (get-char-property (point) 'invisible)))
14776 (defun org-invisible-p2 ()
14777 "Check if point is at a character currently not visible."
14779 (if (and (eolp) (not (bobp))) (backward-char 1))
14780 ;; Early versions of noutline don't have `outline-invisible-p'.
14781 (if (fboundp 'outline-invisible-p)
14782 (outline-invisible-p)
14783 (get-char-property (point) 'invisible))))
14785 (defun org-back-to-heading (&optional invisible-ok)
14786 "Call `outline-back-to-heading', but provide a better error message."
14787 (condition-case nil
14788 (outline-back-to-heading invisible-ok)
14789 (error (error "Before first headline at position %d in buffer %s"
14790 (point) (current-buffer)))))
14792 (defun org-before-first-heading-p ()
14793 "Before first heading?"
14795 (null (re-search-backward "^\\*+ " nil t))))
14797 (defalias 'org-on-heading-p 'outline-on-heading-p)
14798 (defalias 'org-at-heading-p 'outline-on-heading-p)
14799 (defun org-at-heading-or-item-p ()
14800 (or (org-on-heading-p) (org-at-item-p)))
14802 (defun org-on-target-p ()
14803 (or (org-in-regexp org-radio-target-regexp)
14804 (org-in-regexp org-target-regexp)))
14806 (defun org-up-heading-all (arg)
14807 "Move to the heading line of which the present line is a subheading.
14808 This function considers both visible and invisible heading lines.
14809 With argument, move up ARG levels."
14810 (if (fboundp 'outline-up-heading-all)
14811 (outline-up-heading-all arg) ; emacs 21 version of outline.el
14812 (outline-up-heading arg t))) ; emacs 22 version of outline.el
14814 (defun org-up-heading-safe ()
14815 "Move to the heading line of which the present line is a subheading.
14816 This version will not throw an error. It will return the level of the
14817 headline found, or nil if no higher level is found."
14818 (let (start-level re)
14819 (org-back-to-heading t)
14820 (setq start-level (funcall outline-level))
14821 (if (equal start-level 1)
14823 (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
14824 (if (re-search-backward re nil t)
14825 (funcall outline-level)))))
14827 (defun org-first-sibling-p ()
14828 "Is this heading the first child of its parents?"
14830 (let ((re (concat "^" outline-regexp))
14832 (unless (org-at-heading-p t)
14833 (error "Not at a heading"))
14834 (setq level (funcall outline-level))
14836 (if (not (re-search-backward re nil t))
14838 (setq l (funcall outline-level))
14841 (defun org-goto-sibling (&optional previous)
14842 "Goto the next sibling, even if it is invisible.
14843 When PREVIOUS is set, go to the previous sibling instead. Returns t
14844 when a sibling was found. When none is found, return nil and don't
14846 (let ((fun (if previous 're-search-backward 're-search-forward))
14848 (re (concat "^" outline-regexp))
14850 (when (condition-case nil (org-back-to-heading t) (error nil))
14851 (setq level (funcall outline-level))
14853 (or previous (forward-char 1))
14854 (while (funcall fun re nil t)
14855 (setq l (funcall outline-level))
14856 (when (< l level) (goto-char pos) (throw 'exit nil))
14857 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
14861 (defun org-show-siblings ()
14862 "Show all siblings of the current headline."
14864 (while (org-goto-sibling) (org-flag-heading nil)))
14866 (while (org-goto-sibling 'previous)
14867 (org-flag-heading nil))))
14869 (defun org-show-hidden-entry ()
14870 "Show an entry where even the heading is hidden."
14874 (defun org-flag-heading (flag &optional entry)
14875 "Flag the current heading. FLAG non-nil means make invisible.
14876 When ENTRY is non-nil, show the entire entry."
14878 (org-back-to-heading t)
14879 ;; Check if we should show the entire entry
14884 (and (outline-next-heading)
14885 (org-flag-heading nil))))
14886 (outline-flag-region (max (point-min) (1- (point)))
14887 (save-excursion (outline-end-of-heading) (point))
14890 (defun org-forward-same-level (arg)
14891 "Move forward to the ARG'th subheading at same level as this one.
14892 Stop at the first and last subheadings of a superior heading.
14893 This is like outline-forward-same-level, but invisible headings are ok."
14895 (org-back-to-heading t)
14897 (let ((point-to-move-to (save-excursion
14898 (org-get-next-sibling))))
14899 (if point-to-move-to
14901 (goto-char point-to-move-to)
14902 (setq arg (1- arg)))
14905 (error "No following same-level heading"))))))
14907 (defun org-get-next-sibling ()
14908 "Move to next heading of the same level, and return point.
14909 If there is no such heading, return nil.
14910 This is like outline-next-sibling, but invisible headings are ok."
14911 (let ((level (funcall outline-level)))
14912 (outline-next-heading)
14913 (while (and (not (eobp)) (> (funcall outline-level) level))
14914 (outline-next-heading))
14915 (if (or (eobp) (< (funcall outline-level) level))
14919 (defun org-end-of-subtree (&optional invisible-OK to-heading)
14920 ;; This is an exact copy of the original function, but it uses
14921 ;; `org-back-to-heading', to make it work also in invisible
14922 ;; trees. And is uses an invisible-OK argument.
14923 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
14924 (org-back-to-heading invisible-OK)
14926 (level (funcall outline-level)))
14927 (while (and (not (eobp))
14928 (or first (> (funcall outline-level) level)))
14930 (outline-next-heading))
14932 (if (memq (preceding-char) '(?\n ?\^M))
14934 ;; Go to end of line before heading
14936 (if (memq (preceding-char) '(?\n ?\^M))
14937 ;; leave blank line before heading
14938 (forward-char -1))))))
14941 (defun org-show-subtree ()
14942 "Show everything after this heading at deeper levels."
14943 (outline-flag-region
14946 (outline-end-of-subtree) (outline-next-heading) (point))
14949 (defun org-show-entry ()
14950 "Show the body directly following this heading.
14951 Show the heading too, if it is currently invisible."
14954 (condition-case nil
14956 (org-back-to-heading t)
14957 (outline-flag-region
14958 (max (point-min) (1- (point)))
14961 (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
14962 (or (match-beginning 1) (point-max)))
14966 (defun org-make-options-regexp (kwds)
14967 "Make a regular expression for keyword lines."
14971 (mapconcat 'regexp-quote kwds "\\|")
14975 ;; Make isearch reveal the necessary context
14976 (defun org-isearch-end ()
14977 "Reveal context after isearch exits."
14978 (when isearch-success ; only if search was successful
14979 (if (featurep 'xemacs)
14980 ;; Under XEmacs, the hook is run in the correct place,
14981 ;; we directly show the context.
14982 (org-show-context 'isearch)
14983 ;; In Emacs the hook runs *before* restoring the overlays.
14984 ;; So we have to use a one-time post-command-hook to do this.
14985 ;; (Emacs 22 has a special variable, see function `org-mode')
14986 (unless (and (boundp 'isearch-mode-end-hook-quit)
14987 isearch-mode-end-hook-quit)
14988 ;; Only when the isearch was not quitted.
14989 (org-add-hook 'post-command-hook 'org-isearch-post-command
14990 'append 'local)))))
14992 (defun org-isearch-post-command ()
14993 "Remove self from hook, and show context."
14994 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
14995 (org-show-context 'isearch))
14998 ;;;; Integration with and fixes for other packages
15002 (defvar org-imenu-markers nil
15003 "All markers currently used by Imenu.")
15004 (make-variable-buffer-local 'org-imenu-markers)
15006 (defun org-imenu-new-marker (&optional pos)
15007 "Return a new marker for use by Imenu, and remember the marker."
15008 (let ((m (make-marker)))
15009 (move-marker m (or pos (point)))
15010 (push m org-imenu-markers)
15013 (defun org-imenu-get-tree ()
15014 "Produce the index for Imenu."
15015 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
15016 (setq org-imenu-markers nil)
15017 (let* ((n org-imenu-depth)
15018 (re (concat "^" outline-regexp))
15019 (subs (make-vector (1+ n) nil))
15025 (goto-char (point-max))
15026 (while (re-search-backward re nil t)
15027 (setq level (org-reduced-level (funcall outline-level)))
15029 (looking-at org-complex-heading-regexp)
15030 (setq head (org-link-display-format
15031 (org-match-string-no-properties 4))
15032 m (org-imenu-new-marker))
15033 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
15034 (if (>= level last-level)
15035 (push (cons head m) (aref subs level))
15036 (push (cons head (aref subs (1+ level))) (aref subs level))
15037 (loop for i from (1+ level) to n do (aset subs i nil)))
15038 (setq last-level level)))))
15041 (eval-after-load "imenu"
15043 (add-hook 'imenu-after-jump-hook
15045 (if (eq major-mode 'org-mode)
15046 (org-show-context 'org-goto))))))
15048 (defun org-link-display-format (link)
15049 "Replace a link with either the description, or the link target
15050 if no description is present"
15052 (if (string-match org-bracket-link-analytic-regexp link)
15053 (replace-match (or (match-string 5 link)
15054 (concat (match-string 1 link)
15055 (match-string 3 link)))
15059 ;; Speedbar support
15061 (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
15062 "Overlay marking the agenda restriction line in speedbar.")
15063 (org-overlay-put org-speedbar-restriction-lock-overlay
15064 'face 'org-agenda-restriction-lock)
15065 (org-overlay-put org-speedbar-restriction-lock-overlay
15066 'help-echo "Agendas are currently limited to this item.")
15067 (org-detach-overlay org-speedbar-restriction-lock-overlay)
15069 (defun org-speedbar-set-agenda-restriction ()
15070 "Restrict future agenda commands to the location at point in speedbar.
15071 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
15073 (require 'org-agenda)
15074 (let (p m tp np dir txt)
15076 ((setq p (text-property-any (point-at-bol) (point-at-eol)
15078 (setq m (get-text-property p 'org-imenu-marker))
15081 (set-buffer (marker-buffer m))
15083 (org-agenda-set-restriction-lock 'subtree))))
15084 ((setq p (text-property-any (point-at-bol) (point-at-eol)
15085 'speedbar-function 'speedbar-find-file))
15086 (setq tp (previous-single-property-change
15087 (1+ p) 'speedbar-function)
15088 np (next-single-property-change
15089 tp 'speedbar-function)
15090 dir (speedbar-line-directory)
15091 txt (buffer-substring-no-properties (or tp (point-min))
15092 (or np (point-max))))
15095 (set-buffer (find-file-noselect
15096 (let ((default-directory dir))
15097 (expand-file-name txt))))
15098 (unless (org-mode-p)
15099 (error "Cannot restrict to non-Org-mode file"))
15100 (org-agenda-set-restriction-lock 'file))))
15101 (t (error "Don't know how to restrict Org-mode's agenda")))
15102 (org-move-overlay org-speedbar-restriction-lock-overlay
15103 (point-at-bol) (point-at-eol))
15104 (setq current-prefix-arg nil)
15105 (org-agenda-maybe-redo)))
15107 (eval-after-load "speedbar"
15109 (speedbar-add-supported-extension ".org")
15110 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
15111 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
15112 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
15113 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
15114 (add-hook 'speedbar-visiting-tag-hook
15115 (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
15118 ;;; Fixes and Hacks for problems with other packages
15120 ;; Make flyspell not check words in links, to not mess up our keymap
15121 (defun org-mode-flyspell-verify ()
15122 "Don't let flyspell put overlays at active buttons."
15123 (not (get-text-property (point) 'keymap)))
15125 ;; Make `bookmark-jump' show the jump location if it was hidden.
15126 (eval-after-load "bookmark"
15127 '(if (boundp 'bookmark-after-jump-hook)
15128 ;; We can use the hook
15129 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
15130 ;; Hook not available, use advice
15131 (defadvice bookmark-jump (after org-make-visible activate)
15132 "Make the position visible."
15133 (org-bookmark-jump-unhide))))
15135 ;; Make sure saveplace show the location if it was hidden
15136 (eval-after-load "saveplace"
15137 '(defadvice save-place-find-file-hook (after org-make-visible activate)
15138 "Make the position visible."
15139 (org-bookmark-jump-unhide)))
15141 (defun org-bookmark-jump-unhide ()
15142 "Unhide the current position, to show the bookmark location."
15144 (or (org-invisible-p)
15145 (save-excursion (goto-char (max (point-min) (1- (point))))
15146 (org-invisible-p)))
15147 (org-show-context 'bookmark-jump)))
15149 ;; Make session.el ignore our circular variable
15150 (eval-after-load "session"
15151 '(add-to-list 'session-globals-exclude 'org-mark-ring))
15153 ;;;; Experimental code
15155 (defun org-closed-in-range ()
15156 "Sparse tree of items closed in a certain time range.
15157 Still experimental, may disappear in the future."
15159 ;; Get the time interval from the user.
15160 (let* ((time1 (time-to-seconds
15161 (org-read-date nil 'to-time nil "Starting date: ")))
15162 (time2 (time-to-seconds
15163 (org-read-date nil 'to-time nil "End date:")))
15164 ;; callback function
15165 (callback (lambda ()
15168 (apply 'encode-time
15169 (org-parse-time-string
15170 (match-string 1))))))
15171 ;; check if time in interval
15172 (and (>= time time1) (<= time time2))))))
15173 ;; make tree, check each match with the callback
15174 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
15181 (run-hooks 'org-load-hook)
15183 ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
15185 ;;; org.el ends here