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.24trans"
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
" inlinetask: Tasks independent of outline hierarchy" org-inlinetask
)
171 (const :tag
" irc: Links to IRC/ERC chat sessions" org-irc
)
172 (const :tag
" mac-message: Links to messages in Apple Mail" org-mac-message
)
173 (const :tag
" mew Links to Mew folders/messages" org-mew
)
174 (const :tag
" mhe: Links to MHE folders/messages" org-mhe
)
175 (const :tag
" protocol: Intercept calls from emacsclient" org-protocol
)
176 (const :tag
" rmail: Links to RMAIL folders/messages" org-rmail
)
177 (const :tag
" vm: Links to VM folders/messages" org-vm
)
178 (const :tag
" wl: Links to Wanderlust folders/messages" org-wl
)
179 (const :tag
" w3m: Special cut/past from w3m to Org." org-w3m
)
180 (const :tag
" mouse: Additional mouse support" org-mouse
)
182 (const :tag
"C annotate-file: Annotate a file with org syntax" org-annotate-file
)
183 (const :tag
"C annotation-helper: Call Remember directly from Browser" org-annotation-helper
)
184 (const :tag
"C bookmark: Org links to bookmarks" org-bookmark
)
185 (const :tag
"C browser-url: Store link, directly from Browser" org-browser-url
)
186 (const :tag
"C choose: Use TODO keywords to mark decisions states" org-choose
)
187 (const :tag
"C collector: Collect properties into tables" org-collector
)
188 (const :tag
"C depend: TODO dependencies for Org-mode" org-depend
)
189 (const :tag
"C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol
)
190 (const :tag
"C eval: Include command output as text" org-eval
)
191 (const :tag
"C eval-light: Evaluate inbuffer-code on demand" org-eval-light
)
192 (const :tag
"C expiry: Expiry mechanism for Org entries" org-expiry
)
193 (const :tag
"C exp-bibtex: Export citations using BibTeX" org-exp-bibtex
)
194 (const :tag
"C exp-blocks: Pre-process blocks for export" org-exp-blocks
)
195 (const :tag
"C interactive-query: Interactive modification of tags query" org-interactive-query
)
196 (const :tag
"C mairix: Hook mairix search into Org for different MUAs" org-mairix
)
197 (const :tag
"C man: Support for links to manpages in Org-mode" org-man
)
198 (const :tag
"C mtags: Support for muse-like tags" org-mtags
)
199 (const :tag
"C panel: Simple routines for us with bad memory" org-panel
)
200 (const :tag
"C R: Computation using the R language" org-R
)
201 (const :tag
"C registry: A registry for Org links" org-registry
)
202 (const :tag
"C org2rem: Convert org appointments into reminders" org2rem
)
203 (const :tag
"C screen: Visit screen sessions through Org-mode links" org-screen
)
204 (const :tag
"C toc: Table of contents for Org-mode buffer" org-toc
)
205 (const :tag
"C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert
)
206 (repeat :tag
"External packages" :inline t
(symbol :tag
"Package"))))
208 (defcustom org-support-shift-select nil
209 "Non-nil means, make shift-cursor commands select text when possible.
211 In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
212 selecting a region, or enlarge thusly regions started in this way.
213 In Org-mode, in special contexts, these same keys are used for other
214 purposes, important enough to compete with shift selection. Org tries
215 to balance these needs by supporting `shift-select-mode' outside these
216 special contexts, under control of this variable.
218 The default of this variable is nil, to avoid confusing behavior. Shifted
219 cursor keys will then execute Org commands in the following contexts:
220 - on a headline, changing TODO state (left/right) and priority (up/down)
221 - on a time stamp, changing the time
222 - in a plain list item, changing the bullet type
223 - in a property definition line, switching between allowed values
224 - in the BEGIN line of a clock table (changing the time block).
225 Outside these contexts, the commands will throw an error.
227 When this variable is t and the cursor is not in a special context,
228 Org-mode will support shift-selection for making and enlarging regions.
229 To make this more effective, the bullet cycling will no longer happen
230 anywhere in an item line, but only if the cursor is exactly on the bullet.
232 If you set this variable to the symbol `always', then the keys
233 will not be special in headlines, property lines, and item lines, to make
234 shift selection work there as well. If this is what you want, you can
235 use the following alternative commands: `C-c C-t' and `C-c ,' to
236 change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
237 TODO sets, `C-c -' to cycle item bullet types, and properties can be
238 edited by hand or in column view.
240 However, when the cursor is on a timestamp, shift-cursor commands
241 will still edit the time stamp - this is just too good to give up.
243 XEmacs user should have this variable set to nil, because shift-select-mode
247 (const :tag
"Never" nil
)
248 (const :tag
"When outside special context" t
)
249 (const :tag
"Everywhere except timestamps" always
)))
251 (defgroup org-startup nil
252 "Options concerning startup of Org-mode."
256 (defcustom org-startup-folded t
257 "Non-nil means, entering Org-mode will switch to OVERVIEW.
258 This can also be configured on a per-file basis by adding one of
259 the following lines anywhere in the buffer:
266 (const :tag
"nofold: show all" nil
)
267 (const :tag
"fold: overview" t
)
268 (const :tag
"content: all headlines" content
)))
270 (defcustom org-startup-truncated t
271 "Non-nil means, entering Org-mode will set `truncate-lines'.
272 This is useful since some lines containing links can be very long and
273 uninteresting. Also tables look terrible when wrapped."
277 (defcustom org-startup-align-all-tables nil
278 "Non-nil means, align all tables when visiting a file.
279 This is useful when the column width in tables is forced with <N> cookies
280 in table fields. Such tables will look correct only after the first re-align.
281 This can also be configured on a per-file basis by adding one of
282 the following lines anywhere in the buffer:
288 (defcustom org-insert-mode-line-in-empty-file nil
289 "Non-nil means insert the first line setting Org-mode in empty files.
290 When the function `org-mode' is called interactively in an empty file, this
291 normally means that the file name does not automatically trigger Org-mode.
292 To ensure that the file will always be in Org-mode in the future, a
293 line enforcing Org-mode will be inserted into the buffer, if this option
298 (defcustom org-replace-disputed-keys nil
299 "Non-nil means use alternative key bindings for some keys.
300 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
301 These keys are also used by other packages like shift-selection-mode'
302 \(built into Emacs 23), `CUA-mode' or `windmove.el'.
303 If you want to use Org-mode together with one of these other modes,
304 or more generally if you would like to move some Org-mode commands to
305 other keys, set this variable and configure the keys with the variable
308 This option is only relevant at load-time of Org-mode, and must be set
309 *before* org.el is loaded. Changing it requires a restart of Emacs to
314 (defcustom org-use-extra-keys nil
315 "Non-nil means use extra key sequence definitions for certain
316 commands. This happens automatically if you run XEmacs or if
317 window-system is nil. This variable lets you do the same
318 manually. You must set it before loading org.
320 Example: on Carbon Emacs 22 running graphically, with an external
321 keyboard on a Powerbook, the default way of setting M-left might
322 not work for either Alt or ESC. Setting this variable will make
327 (if (fboundp 'defvaralias
)
328 (defvaralias 'org-CUA-compatible
'org-replace-disputed-keys
))
330 (defcustom org-disputed-keys
331 '(([(shift up
)] .
[(meta p
)])
332 ([(shift down
)] .
[(meta n
)])
333 ([(shift left
)] .
[(meta -
)])
334 ([(shift right
)] .
[(meta +)])
335 ([(control shift right
)] .
[(meta shift
+)])
336 ([(control shift left
)] .
[(meta shift -
)]))
337 "Keys for which Org-mode and other modes compete.
338 This is an alist, cars are the default keys, second element specifies
339 the alternative to use when `org-replace-disputed-keys' is t.
341 Keys can be specified in any syntax supported by `define-key'.
342 The value of this option takes effect only at Org-mode's startup,
343 therefore you'll have to restart Emacs to apply it after changing."
348 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
349 Or return the original if not disputed."
350 (if org-replace-disputed-keys
351 (let* ((nkey (key-description key
))
352 (x (org-find-if (lambda (x)
353 (equal (key-description (car x
)) nkey
))
358 (defun org-find-if (predicate seq
)
361 (if (funcall predicate
(car seq
))
362 (throw 'exit
(car seq
))
365 (defun org-defkey (keymap key def
)
366 "Define a key, possibly translated, as returned by `org-key'."
367 (define-key keymap
(org-key key
) def
))
369 (defcustom org-ellipsis nil
370 "The ellipsis to use in the Org-mode outline.
371 When nil, just use the standard three dots. When a string, use that instead,
372 When a face, use the standard 3 dots, but with the specified face.
373 The change affects only Org-mode (which will then use its own display table).
374 Changing this requires executing `M-x org-mode' in a buffer to become
377 :type
'(choice (const :tag
"Default" nil
)
378 (face :tag
"Face" :value org-warning
)
379 (string :tag
"String" :value
"...#")))
381 (defvar org-display-table nil
382 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
384 (defgroup org-keywords nil
385 "Keywords in Org-mode."
389 (defcustom org-deadline-string
"DEADLINE:"
390 "String to mark deadline entries.
391 A deadline is this string, followed by a time stamp. Should be a word,
392 terminated by a colon. You can insert a schedule keyword and
393 a timestamp with \\[org-deadline].
394 Changes become only effective after restarting Emacs."
398 (defcustom org-scheduled-string
"SCHEDULED:"
399 "String to mark scheduled TODO entries.
400 A schedule is this string, followed by a time stamp. Should be a word,
401 terminated by a colon. You can insert a schedule keyword and
402 a timestamp with \\[org-schedule].
403 Changes become only effective after restarting Emacs."
407 (defcustom org-closed-string
"CLOSED:"
408 "String used as the prefix for timestamps logging closing a TODO entry."
412 (defcustom org-clock-string
"CLOCK:"
413 "String used as prefix for timestamps clocking work hours on an item."
417 (defcustom org-comment-string
"COMMENT"
418 "Entries starting with this keyword will never be exported.
419 An entry can be toggled between COMMENT and normal with
420 \\[org-toggle-comment].
421 Changes become only effective after restarting Emacs."
425 (defcustom org-quote-string
"QUOTE"
426 "Entries starting with this keyword will be exported in fixed-width font.
427 Quoting applies only to the text in the entry following the headline, and does
428 not extend beyond the next headline, even if that is lower level.
429 An entry can be toggled between QUOTE and normal with
430 \\[org-toggle-fixed-width-section]."
434 (defconst org-repeat-re
435 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
436 "Regular expression for specifying repeated events.
437 After a match, group 1 contains the repeat expression.")
439 (defgroup org-structure nil
440 "Options concerning the general structure of Org-mode files."
444 (defgroup org-reveal-location nil
445 "Options about how to make context of a location visible."
446 :tag
"Org Reveal Location"
447 :group
'org-structure
)
449 (defconst org-context-choice
451 (const :tag
"Always" t
)
452 (const :tag
"Never" nil
)
453 (repeat :greedy t
:tag
"Individual contexts"
455 (choice :tag
"Context"
462 (const bookmark-jump
)
466 "Contexts for the reveal options.")
468 (defcustom org-show-hierarchy-above
'((default . t
))
469 "Non-nil means, show full hierarchy when revealing a location.
470 Org-mode often shows locations in an org-mode file which might have
471 been invisible before. When this is set, the hierarchy of headings
472 above the exposed location is shown.
473 Turning this off for example for sparse trees makes them very compact.
474 Instead of t, this can also be an alist specifying this option for different
475 contexts. Valid contexts are
476 agenda when exposing an entry from the agenda
477 org-goto when using the command `org-goto' on key C-c C-j
478 occur-tree when using the command `org-occur' on key C-c /
479 tags-tree when constructing a sparse tree based on tags matches
480 link-search when exposing search matches associated with a link
481 mark-goto when exposing the jump goal of a mark
482 bookmark-jump when exposing a bookmark location
483 isearch when exiting from an incremental search
484 default default for all contexts not set explicitly"
485 :group
'org-reveal-location
486 :type org-context-choice
)
488 (defcustom org-show-following-heading
'((default . nil
))
489 "Non-nil means, show following heading when revealing a location.
490 Org-mode often shows locations in an org-mode file which might have
491 been invisible before. When this is set, the heading following the
493 Turning this off for example for sparse trees makes them very compact,
494 but makes it harder to edit the location of the match. In such a case,
495 use the command \\[org-reveal] to show more context.
496 Instead of t, this can also be an alist specifying this option for different
497 contexts. See `org-show-hierarchy-above' for valid contexts."
498 :group
'org-reveal-location
499 :type org-context-choice
)
501 (defcustom org-show-siblings
'((default . nil
) (isearch t
))
502 "Non-nil means, show all sibling heading when revealing a location.
503 Org-mode often shows locations in an org-mode file which might have
504 been invisible before. When this is set, the sibling of the current entry
505 heading are all made visible. If `org-show-hierarchy-above' is t,
506 the same happens on each level of the hierarchy above the current entry.
508 By default this is on for the isearch context, off for all other contexts.
509 Turning this off for example for sparse trees makes them very compact,
510 but makes it harder to edit the location of the match. In such a case,
511 use the command \\[org-reveal] to show more context.
512 Instead of t, this can also be an alist specifying this option for different
513 contexts. See `org-show-hierarchy-above' for valid contexts."
514 :group
'org-reveal-location
515 :type org-context-choice
)
517 (defcustom org-show-entry-below
'((default . nil
))
518 "Non-nil means, show the entry below a headline when revealing a location.
519 Org-mode often shows locations in an org-mode file which might have
520 been invisible before. When this is set, the text below the headline that is
521 exposed is also shown.
523 By default this is off for all contexts.
524 Instead of t, this can also be an alist specifying this option for different
525 contexts. See `org-show-hierarchy-above' for valid contexts."
526 :group
'org-reveal-location
527 :type org-context-choice
)
529 (defcustom org-indirect-buffer-display
'other-window
530 "How should indirect tree buffers be displayed?
531 This applies to indirect buffers created with the commands
532 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
534 current-window Display in the current window
535 other-window Just display in another window.
536 dedicated-frame Create one new frame, and re-use it each time.
537 new-frame Make a new frame each time. Note that in this case
538 previously-made indirect buffers are kept, and you need to
539 kill these buffers yourself."
540 :group
'org-structure
541 :group
'org-agenda-windows
543 (const :tag
"In current window" current-window
)
544 (const :tag
"In current frame, other window" other-window
)
545 (const :tag
"Each time a new frame" new-frame
)
546 (const :tag
"One dedicated frame" dedicated-frame
)))
548 (defgroup org-cycle nil
549 "Options concerning visibility cycling in Org-mode."
551 :group
'org-structure
)
553 (defcustom org-cycle-max-level nil
554 "Maximum level which should still be subject to visibility cycling.
555 Levels higher than this will, for cycling, be treated as text, not a headline.
556 When `org-odd-levels-only' is set, a value of N in this variable actually
557 means 2N-1 stars as the limiting headline.
558 When nil, cycle all levels.
559 Note that the limiting level of cycling is also influenced by
560 `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
561 `org-inlinetask-min-level' is, cycling will be limited to levels one less
565 (const :tag
"No limit" nil
)
566 (integer :tag
"Maximum level")))
568 (defcustom org-drawers
'("PROPERTIES" "CLOCK" "LOGBOOK")
569 "Names of drawers. Drawers are not opened by cycling on the headline above.
570 Drawers only open with a TAB on the drawer line itself. A drawer looks like
575 The drawer \"PROPERTIES\" is special for capturing properties through
578 Drawers can be defined on the per-file basis with a line like:
580 #+DRAWERS: HIDDEN STATE PROPERTIES"
581 :group
'org-structure
583 :type
'(repeat (string :tag
"Drawer Name")))
585 (defcustom org-cycle-global-at-bob nil
586 "Cycle globally if cursor is at beginning of buffer and not at a headline.
587 This makes it possible to do global cycling without having to use S-TAB or
588 C-u TAB. For this special case to work, the first line of the buffer
589 must not be a headline - it may be empty or some other text. When used in
590 this way, `org-cycle-hook' is disables temporarily, to make sure the
591 cursor stays at the beginning of the buffer.
592 When this option is nil, don't do anything special at the beginning
597 (defcustom org-cycle-emulate-tab t
598 "Where should `org-cycle' emulate TAB.
600 white Only in completely white lines
601 whitestart Only at the beginning of lines, before the first non-white char
602 t Everywhere except in headlines
603 exc-hl-bol Everywhere except at the start of a headline
604 If TAB is used in a place where it does not emulate TAB, the current subtree
605 visibility is cycled."
607 :type
'(choice (const :tag
"Never" nil
)
608 (const :tag
"Only in completely white lines" white
)
609 (const :tag
"Before first char in a line" whitestart
)
610 (const :tag
"Everywhere except in headlines" t
)
611 (const :tag
"Everywhere except at bol in headlines" exc-hl-bol
)
614 (defcustom org-cycle-separator-lines
2
615 "Number of empty lines needed to keep an empty line between collapsed trees.
616 If you leave an empty line between the end of a subtree and the following
617 headline, this empty line is hidden when the subtree is folded.
618 Org-mode will leave (exactly) one empty line visible if the number of
619 empty lines is equal or larger to the number given in this variable.
620 So the default 2 means, at least 2 empty lines after the end of a subtree
621 are needed to produce free space between a collapsed subtree and the
624 Special case: when 0, never leave empty lines in collapsed view."
627 (put 'org-cycle-separator-lines
'safe-local-variable
'integerp
)
629 (defcustom org-cycle-hook
'(org-cycle-hide-archived-subtrees
630 org-cycle-hide-drawers
631 org-cycle-show-empty-lines
632 org-optimize-window-after-visibility-change
)
633 "Hook that is run after `org-cycle' has changed the buffer visibility.
634 The function(s) in this hook must accept a single argument which indicates
635 the new state that was set by the most recent `org-cycle' command. The
636 argument is a symbol. After a global state change, it can have the values
637 `overview', `content', or `all'. After a local state change, it can have
638 the values `folded', `children', or `subtree'."
642 (defgroup org-edit-structure nil
643 "Options concerning structure editing in Org-mode."
644 :tag
"Org Edit Structure"
645 :group
'org-structure
)
647 (defcustom org-odd-levels-only nil
648 "Non-nil means, skip even levels and only use odd levels for the outline.
649 This has the effect that two stars are being added/taken away in
650 promotion/demotion commands. It also influences how levels are
651 handled by the exporters.
652 Changing it requires restart of `font-lock-mode' to become effective
653 for fontification also in regions already fontified.
654 You may also set this on a per-file basis by adding one of the following
659 :group
'org-edit-structure
660 :group
'org-font-lock
663 (defcustom org-adapt-indentation t
664 "Non-nil means, adapt indentation when promoting and demoting.
665 When this is set and the *entire* text in an entry is indented, the
666 indentation is increased by one space in a demotion command, and
667 decreased by one in a promotion command. If any line in the entry
668 body starts at column 0, indentation is not changed at all."
669 :group
'org-edit-structure
672 (defcustom org-special-ctrl-a
/e nil
673 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
675 When t, `C-a' will bring back the cursor to the beginning of the
676 headline text, i.e. after the stars and after a possible TODO keyword.
677 In an item, this will be the position after the bullet.
678 When the cursor is already at that position, another `C-a' will bring
679 it to the beginning of the line.
681 `C-e' will jump to the end of the headline, ignoring the presence of tags
682 in the headline. A second `C-e' will then jump to the true end of the
683 line, after any tags.
685 When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
686 and only a directly following, identical keypress will bring the cursor
687 to the special positions.
689 This may also be a cons cell where the behavior for `C-a' and `C-e' is
691 :group
'org-edit-structure
693 (const :tag
"off" nil
)
694 (const :tag
"after stars/bullet and before tags first" t
)
695 (const :tag
"true line boundary first" reversed
)
696 (cons :tag
"Set C-a and C-e separately"
697 (choice :tag
"Special C-a"
698 (const :tag
"off" nil
)
699 (const :tag
"after stars/bullet first" t
)
700 (const :tag
"before stars/bullet first" reversed
))
701 (choice :tag
"Special C-e"
702 (const :tag
"off" nil
)
703 (const :tag
"before tags first" t
)
704 (const :tag
"after tags first" reversed
)))))
705 (if (fboundp 'defvaralias
)
706 (defvaralias 'org-special-ctrl-a
'org-special-ctrl-a
/e
))
708 (defcustom org-special-ctrl-k nil
709 "Non-nil means `C-k' will behave specially in headlines.
710 When nil, `C-k' will call the default `kill-line' command.
711 When t, the following will happen while the cursor is in the headline:
713 - When the cursor is at the beginning of a headline, kill the entire
714 line and possible the folded subtree below the line.
715 - When in the middle of the headline text, kill the headline up to the tags.
716 - When after the headline text, kill the tags."
717 :group
'org-edit-structure
720 (defcustom org-yank-folded-subtrees t
721 "Non-nil means, when yanking subtrees, fold them.
722 If the kill is a single subtree, or a sequence of subtrees, i.e. if
723 it starts with a heading and all other headings in it are either children
724 or siblings, then fold all the subtrees. However, do this only if no
725 text after the yank would be swallowed into a folded tree by this action."
726 :group
'org-edit-structure
729 (defcustom org-yank-adjusted-subtrees nil
730 "Non-nil means, when yanking subtrees, adjust the level.
731 With this setting, `org-paste-subtree' is used to insert the subtree, see
732 this function for details."
733 :group
'org-edit-structure
736 (defcustom org-M-RET-may-split-line
'((default . t
))
737 "Non-nil means, M-RET will split the line at the cursor position.
738 When nil, it will go to the end of the line before making a
740 You may also set this option in a different way for different
741 contexts. Valid contexts are:
743 headline when creating a new headline
744 item when creating a new item
745 table in a table field
746 default the value to be used for all contexts not explicitly
748 :group
'org-structure
751 (const :tag
"Always" t
)
752 (const :tag
"Never" nil
)
753 (repeat :greedy t
:tag
"Individual contexts"
755 (choice :tag
"Context"
763 (defcustom org-insert-heading-respect-content nil
764 "Non-nil means, insert new headings after the current subtree.
765 When nil, the new heading is created directly after the current line.
766 The commands \\[org-insert-heading-respect-content] and
767 \\[org-insert-todo-heading-respect-content] turn this variable on
768 for the duration of the command."
769 :group
'org-structure
772 (defcustom org-blank-before-new-entry
'((heading . auto
)
773 (plain-list-item . auto
))
774 "Should `org-insert-heading' leave a blank line before new heading/item?
775 The value is an alist, with `heading' and `plain-list-item' as car,
776 and a boolean flag as cdr. For plain lists, if the variable
777 `org-empty-line-terminates-plain-lists' is set, the setting here
778 is ignored and no empty line is inserted, to keep the list in tact."
779 :group
'org-edit-structure
781 (cons (const heading
)
782 (choice (const :tag
"Never" nil
)
783 (const :tag
"Always" t
)
784 (const :tag
"Auto" auto
)))
785 (cons (const plain-list-item
)
786 (choice (const :tag
"Never" nil
)
787 (const :tag
"Always" t
)
788 (const :tag
"Auto" auto
)))))
790 (defcustom org-insert-heading-hook nil
791 "Hook being run after inserting a new heading."
792 :group
'org-edit-structure
795 (defcustom org-enable-fixed-width-editor t
796 "Non-nil means, lines starting with \":\" are treated as fixed-width.
797 This currently only means, they are never auto-wrapped.
798 When nil, such lines will be treated like ordinary lines.
799 See also the QUOTE keyword."
800 :group
'org-edit-structure
803 (defcustom org-edit-src-region-extra nil
804 "Additional regexps to identify regions for editing with `org-edit-src-code'.
805 For examples see the function `org-edit-src-find-region-and-lang'.
806 The regular expression identifying the begin marker should end with a newline,
807 and the regexp marking the end line should start with a newline, to make sure
808 there are kept outside the narrowed region."
809 :group
'org-edit-structure
812 (regexp :tag
"begin regexp")
813 (regexp :tag
"end regexp")
814 (choice :tag
"language"
815 (string :tag
"specify")
816 (integer :tag
"from match group")
817 (const :tag
"from `lang' element")
818 (const :tag
"from `style' element")))))
820 (defcustom org-coderef-label-format
"(ref:%s)"
821 "The default coderef format.
822 This format string will be used to search for coderef labels in literal
823 examples (EXAMPLE and SRC blocks). The format can be overwritten
824 an individual literal example with the -f option, like
826 #+BEGIN_SRC pascal +n -r -l \"((%s))\"
830 If you want to use this for HTML export, make sure that the format does
831 not introduce special font-locking, and avoid the HTML special
832 characters `<', `>', and `&'. The reason for this restriction is that
833 the labels are searched for only after htmlize has done its job."
834 :group
'org-edit-structure
; FIXME this is not in the right group
837 (defcustom org-edit-fixed-width-region-mode
'artist-mode
838 "The mode that should be used to edit fixed-width regions.
839 These are the regions where each line starts with a colon."
840 :group
'org-edit-structure
844 (const fundamental-mode
)
845 (function :tag
"Other (specify)")))
847 (defcustom org-goto-auto-isearch t
848 "Non-nil means, typing characters in org-goto starts incremental search."
849 :group
'org-edit-structure
852 (defgroup org-sparse-trees nil
853 "Options concerning sparse trees in Org-mode."
854 :tag
"Org Sparse Trees"
855 :group
'org-structure
)
857 (defcustom org-highlight-sparse-tree-matches t
858 "Non-nil means, highlight all matches that define a sparse tree.
859 The highlights will automatically disappear the next time the buffer is
860 changed by an edit command."
861 :group
'org-sparse-trees
864 (defcustom org-remove-highlights-with-change t
865 "Non-nil means, any change to the buffer will remove temporary highlights.
866 Such highlights are created by `org-occur' and `org-clock-display'.
867 When nil, `C-c C-c needs to be used to get rid of the highlights.
868 The highlights created by `org-preview-latex-fragment' always need
869 `C-c C-c' to be removed."
870 :group
'org-sparse-trees
875 (defcustom org-occur-hook
'(org-first-headline-recenter)
876 "Hook that is run after `org-occur' has constructed a sparse tree.
877 This can be used to recenter the window to show as much of the structure
879 :group
'org-sparse-trees
882 (defgroup org-imenu-and-speedbar nil
883 "Options concerning imenu and speedbar in Org-mode."
884 :tag
"Org Imenu and Speedbar"
885 :group
'org-structure
)
887 (defcustom org-imenu-depth
2
888 "The maximum level for Imenu access to Org-mode headlines.
889 This also applied for speedbar access."
890 :group
'org-imenu-and-speedbar
893 (defgroup org-table nil
894 "Options concerning tables in Org-mode."
898 (defcustom org-enable-table-editor
'optimized
899 "Non-nil means, lines starting with \"|\" are handled by the table editor.
900 When nil, such lines will be treated like ordinary lines.
902 When equal to the symbol `optimized', the table editor will be optimized to
904 - Automatic overwrite mode in front of whitespace in table fields.
905 This makes the structure of the table stay in tact as long as the edited
906 field does not exceed the column width.
907 - Minimize the number of realigns. Normally, the table is aligned each time
908 TAB or RET are pressed to move to another field. With optimization this
909 happens only if changes to a field might have changed the column width.
910 Optimization requires replacing the functions `self-insert-command',
911 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
912 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
913 very good at guessing when a re-align will be necessary, but you can always
914 force one with \\[org-ctrl-c-ctrl-c].
916 If you would like to use the optimized version in Org-mode, but the
917 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
919 This variable can be used to turn on and off the table editor during a session,
920 but in order to toggle optimization, a restart is required.
922 See also the variable `org-table-auto-blank-field'."
925 (const :tag
"off" nil
)
927 (const :tag
"on, optimized" optimized
)))
929 (defcustom org-self-insert-cluster-for-undo t
930 "Non-nil means cluster self-insert commands for undo when possible.
931 If this is set, then, like in the Emacs command loop, 20 consequtive
932 characters will be undone together.
933 This is configurable, because there is some impact on typing performance."
937 (defcustom org-table-tab-recognizes-table.el t
938 "Non-nil means, TAB will automatically notice a table.el table.
939 When it sees such a table, it moves point into it and - if necessary -
940 calls `table-recognize-table'."
941 :group
'org-table-editing
944 (defgroup org-link nil
945 "Options concerning links in Org-mode."
949 (defvar org-link-abbrev-alist-local nil
950 "Buffer-local version of `org-link-abbrev-alist', which see.
951 The value of this is taken from the #+LINK lines.")
952 (make-variable-buffer-local 'org-link-abbrev-alist-local
)
954 (defcustom org-link-abbrev-alist nil
955 "Alist of link abbreviations.
956 The car of each element is a string, to be replaced at the start of a link.
957 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
958 links in Org-mode buffers can have an optional tag after a double colon, e.g.
960 [[linkkey:tag][description]]
962 The 'linkkey' must be a word word, starting with a letter, followed
963 by letters, numbers, '-' or '_'.
965 If REPLACE is a string, the tag will simply be appended to create the link.
966 If the string contains \"%s\", the tag will be inserted there. Alternatively,
967 the placeholder \"%h\" will cause a url-encoded version of the tag to
968 be inserted at that point (see the function `url-hexify-string').
970 REPLACE may also be a function that will be called with the tag as the
971 only argument to create the link, which should be returned as a string.
973 See the manual for examples."
977 (string :tag
"Protocol")
979 (string :tag
"Format")
982 (defcustom org-descriptive-links t
983 "Non-nil means, hide link part and only show description of bracket links.
984 Bracket links are like [[link][description]]. This variable sets the initial
985 state in new org-mode buffers. The setting can then be toggled on a
986 per-buffer basis from the Org->Hyperlinks menu."
990 (defcustom org-link-file-path-type
'adaptive
991 "How the path name in file links should be stored.
994 relative Relative to the current directory, i.e. the directory of the file
995 into which the link is being inserted.
996 absolute Absolute path, if possible with ~ for home directory.
997 noabbrev Absolute path, no abbreviation of home directory.
998 adaptive Use relative path for files in the current directory and sub-
999 directories of it. For other files, use an absolute path."
1007 (defcustom org-activate-links
'(bracket angle plain radio tag date footnote
)
1008 "Types of links that should be activated in Org-mode files.
1009 This is a list of symbols, each leading to the activation of a certain link
1010 type. In principle, it does not hurt to turn on most link types - there may
1011 be a small gain when turning off unused link types. The types are:
1013 bracket The recommended [[link][description]] or [[link]] links with hiding.
1014 angular Links in angular brackets that may contain whitespace like
1015 <bbdb:Carsten Dominik>.
1016 plain Plain links in normal text, no whitespace, like http://google.com.
1017 radio Text that is matched by a radio target, see manual for details.
1018 tag Tag settings in a headline (link to tag search).
1019 date Time stamps (link to calendar).
1020 footnote Footnote labels.
1022 Changing this variable requires a restart of Emacs to become effective."
1024 :type
'(set :greedy t
1025 (const :tag
"Double bracket links (new style)" bracket
)
1026 (const :tag
"Angular bracket links (old style)" angular
)
1027 (const :tag
"Plain text links" plain
)
1028 (const :tag
"Radio target matches" radio
)
1029 (const :tag
"Tags" tag
)
1030 (const :tag
"Timestamps" date
)
1031 (const :tag
"Footnotes" footnote
)))
1033 (defcustom org-make-link-description-function nil
1034 "Function to use to generate link descriptions from links. If
1035 nil the link location will be used. This function must take two
1036 parameters; the first is the link and the second the description
1037 org-insert-link has generated, and should return the description
1042 (defgroup org-link-store nil
1043 "Options concerning storing links in Org-mode."
1044 :tag
"Org Store Link"
1047 (defcustom org-email-link-description-format
"Email %c: %.30s"
1048 "Format of the description part of a link to an email or usenet message.
1049 The following %-escapes will be replaced by corresponding information:
1051 %F full \"From\" field
1052 %f name, taken from \"From\" field, address if no name
1053 %T full \"To\" field
1054 %t first name in \"To\" field, address if no name
1055 %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
1056 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
1060 You may use normal field width specification between the % and the letter.
1061 This is for example useful to limit the length of the subject.
1063 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
1064 :group
'org-link-store
1067 (defcustom org-from-is-user-regexp
1069 (when (and user-mail-address
(not (string= user-mail-address
"")))
1070 (setq r1
(concat "\\<" (regexp-quote user-mail-address
) "\\>")))
1071 (when (and user-full-name
(not (string= user-full-name
"")))
1072 (setq r2
(concat "\\<" (regexp-quote user-full-name
) "\\>")))
1073 (if (and r1 r2
) (concat r1
"\\|" r2
) (or r1 r2
)))
1074 "Regexp matched against the \"From:\" header of an email or usenet message.
1075 It should match if the message is from the user him/herself."
1076 :group
'org-link-store
1079 (defcustom org-link-to-org-use-id
'create-if-interactive
1080 "Non-nil means, storing a link to an Org file will use entry IDs.
1082 Note that before this variable is even considered, org-id must be loaded,
1083 to please customize `org-modules' and turn it on.
1085 The variable can have the following values:
1087 t Create an ID if needed to make a link to the current entry.
1089 create-if-interactive
1090 If `org-store-link' is called directly (interactively, as a user
1091 command), do create an ID to support the link. But when doing the
1092 job for remember, only use the ID if it already exists. The
1093 purpose of this setting is to avoid proliferation of unwanted
1094 IDs, just because you happen to be in an Org file when you
1095 call `org-remember' that automatically and preemptively
1096 creates a link. If you do want to get an ID link in a remember
1097 template to an entry not having an ID, create it first by
1098 explicitly creating a link to it, using `C-c C-l' first.
1101 Use existing ID, do not create one.
1103 nil Never use an ID to make a link, instead link using a text search for
1105 :group
'org-link-store
1107 (const :tag
"Create ID to make link" t
)
1108 (const :tag
"Create if string link interactively"
1109 'create-if-interactive
)
1110 (const :tag
"Only use existing" 'use-existing
)
1111 (const :tag
"Do not use ID to create link" nil
)))
1113 (defcustom org-context-in-file-links t
1114 "Non-nil means, file links from `org-store-link' contain context.
1115 A search string will be added to the file name with :: as separator and
1116 used to find the context when the link is activated by the command
1117 `org-open-at-point'.
1118 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1119 negates this setting for the duration of the command."
1120 :group
'org-link-store
1123 (defcustom org-keep-stored-link-after-insertion nil
1124 "Non-nil means, keep link in list for entire session.
1126 The command `org-store-link' adds a link pointing to the current
1127 location to an internal list. These links accumulate during a session.
1128 The command `org-insert-link' can be used to insert links into any
1129 Org-mode file (offering completion for all stored links). When this
1130 option is nil, every link which has been inserted once using \\[org-insert-link]
1131 will be removed from the list, to make completing the unused links
1133 :group
'org-link-store
1136 (defgroup org-link-follow nil
1137 "Options concerning following links in Org-mode."
1138 :tag
"Org Follow Link"
1141 (defcustom org-link-translation-function nil
1142 "Function to translate links with different syntax to Org syntax.
1143 This can be used to translate links created for example by the Planner
1144 or emacs-wiki packages to Org syntax.
1145 The function must accept two parameters, a TYPE containing the link
1146 protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
1147 which is everything after the link protocol. It should return a cons
1148 with possibly modified values of type and path.
1149 Org contains a function for this, so if you set this variable to
1150 `org-translate-link-from-planner', you should be able follow many
1151 links created by planner."
1152 :group
'org-link-follow
1155 (defcustom org-follow-link-hook nil
1156 "Hook that is run after a link has been followed."
1157 :group
'org-link-follow
1160 (defcustom org-tab-follows-link nil
1161 "Non-nil means, on links TAB will follow the link.
1162 Needs to be set before org.el is loaded.
1163 This really should not be used, it does not make sense, and the
1164 implementation is bad."
1165 :group
'org-link-follow
1168 (defcustom org-return-follows-link nil
1169 "Non-nil means, on links RET will follow the link.
1170 Needs to be set before org.el is loaded."
1171 :group
'org-link-follow
1174 (defcustom org-mouse-1-follows-link
1175 (if (boundp 'mouse-1-click-follows-link
) mouse-1-click-follows-link t
)
1176 "Non-nil means, mouse-1 on a link will follow the link.
1177 A longer mouse click will still set point. Does not work on XEmacs.
1178 Needs to be set before org.el is loaded."
1179 :group
'org-link-follow
1182 (defcustom org-mark-ring-length
4
1183 "Number of different positions to be recorded in the ring
1184 Changing this requires a restart of Emacs to work correctly."
1185 :group
'org-link-follow
1188 (defcustom org-link-frame-setup
1189 '((vm . vm-visit-folder-other-frame
)
1190 (gnus . gnus-other-frame
)
1191 (file . find-file-other-window
))
1192 "Setup the frame configuration for following links.
1193 When following a link with Emacs, it may often be useful to display
1194 this link in another window or frame. This variable can be used to
1195 set this up for the different types of links.
1198 `vm-visit-folder-other-frame'
1199 For Gnus, use any of
1202 `org-gnus-no-new-news'
1203 For FILE, use any of
1205 `find-file-other-window'
1206 `find-file-other-frame'
1207 For the calendar, use the variable `calendar-setup'.
1208 For BBDB, it is currently only possible to display the matches in
1210 :group
'org-link-follow
1214 (const vm-visit-folder
)
1215 (const vm-visit-folder-other-window
)
1216 (const vm-visit-folder-other-frame
)))
1220 (const gnus-other-frame
)
1221 (const org-gnus-no-new-news
)))
1225 (const find-file-other-window
)
1226 (const find-file-other-frame
)))))
1228 (defcustom org-display-internal-link-with-indirect-buffer nil
1229 "Non-nil means, use indirect buffer to display infile links.
1230 Activating internal links (from one location in a file to another location
1231 in the same file) normally just jumps to the location. When the link is
1232 activated with a C-u prefix (or with mouse-3), the link is displayed in
1233 another window. When this option is set, the other window actually displays
1234 an indirect buffer clone of the current buffer, to avoid any visibility
1235 changes to the current buffer."
1236 :group
'org-link-follow
1239 (defcustom org-open-non-existing-files nil
1240 "Non-nil means, `org-open-file' will open non-existing files.
1241 When nil, an error will be generated."
1242 :group
'org-link-follow
1245 (defcustom org-open-directory-means-index-dot-org nil
1246 "Non-nil means, a link to a directory really means to index.org.
1247 When nil, following a directory link will run dired or open a finder/explorer
1248 window on that directory."
1249 :group
'org-link-follow
1252 (defcustom org-link-mailto-program
'(browse-url "mailto:%a?subject=%s")
1253 "Function and arguments to call for following mailto links.
1254 This is a list with the first element being a lisp function, and the
1255 remaining elements being arguments to the function. In string arguments,
1256 %a will be replaced by the address, and %s will be replaced by the subject
1257 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1258 :group
'org-link-follow
1260 (const :tag
"browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1261 (const :tag
"compose-mail" (compose-mail "%a" "%s"))
1262 (const :tag
"message-mail" (message-mail "%a" "%s"))
1263 (cons :tag
"other" (function) (repeat :tag
"argument" sexp
))))
1265 (defcustom org-confirm-shell-link-function
'yes-or-no-p
1266 "Non-nil means, ask for confirmation before executing shell links.
1267 Shell links can be dangerous: just think about a link
1269 [[shell:rm -rf ~/*][Google Search]]
1271 This link would show up in your Org-mode document as \"Google Search\",
1272 but really it would remove your entire home directory.
1273 Therefore we advise against setting this variable to nil.
1274 Just change it to `y-or-n-p' if you want to confirm with a
1275 single keystroke rather than having to type \"yes\"."
1276 :group
'org-link-follow
1278 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1279 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1280 (const :tag
"no confirmation (dangerous)" nil
)))
1282 (defcustom org-confirm-elisp-link-function
'yes-or-no-p
1283 "Non-nil means, ask for confirmation before executing Emacs Lisp links.
1284 Elisp links can be dangerous: just think about a link
1286 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1288 This link would show up in your Org-mode document as \"Google Search\",
1289 but really it would remove your entire home directory.
1290 Therefore we advise against setting this variable to nil.
1291 Just change it to `y-or-n-p' if you want to confirm with a
1292 single keystroke rather than having to type \"yes\"."
1293 :group
'org-link-follow
1295 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1296 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1297 (const :tag
"no confirmation (dangerous)" nil
)))
1299 (defconst org-file-apps-defaults-gnu
1303 "Default file applications on a UNIX or GNU/Linux system.
1304 See `org-file-apps'.")
1306 (defconst org-file-apps-defaults-macosx
1309 (system .
"open %s")
1311 ("eps.gz" .
"gv %s")
1313 ("fig" .
"xfig %s"))
1314 "Default file applications on a MacOS X system.
1315 The system \"open\" is known as a default, but we use X11 applications
1316 for some files for which the OS does not have a good default.
1317 See `org-file-apps'.")
1319 (defconst org-file-apps-defaults-windowsnt
1323 (list (if (featurep 'xemacs
)
1324 'mswindows-shell-execute
1328 (list (if (featurep 'xemacs
)
1329 'mswindows-shell-execute
1332 "Default file applications on a Windows NT system.
1333 The system \"open\" is used for most files.
1334 See `org-file-apps'.")
1336 (defcustom org-file-apps
1339 ("\\.x?html?\\'" . default
)
1340 ("\\.pdf\\'" . default
)
1342 "External applications for opening `file:path' items in a document.
1343 Org-mode uses system defaults for different file types, but
1344 you can use this variable to set the application for a given file
1345 extension. The entries in this list are cons cells where the car identifies
1346 files and the cdr the corresponding command. Possible values for the
1348 \"regex\" Regular expression matched against the file name. For backward
1349 compatibility, this can also be a string with only alphanumeric
1350 characters, which is then interpreted as an extension.
1351 `directory' Matches a directory
1352 `remote' Matches a remote file, accessible through tramp or efs.
1353 Remote files most likely should be visited through Emacs
1354 because external applications cannot handle such paths.
1355 `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
1356 so all files Emacs knows how to handle. Using this with
1357 command `emacs' will open most files in Emacs. Beware that this
1358 will also open html files inside Emacs, unless you add
1359 (\"html\" . default) to the list as well.
1360 t Default for files not matched by any of the other options.
1361 `system' The system command to open files, like `open' on Windows
1362 and Mac OS X, and mailcap under GNU/Linux. This is the command
1363 that will be selected if you call `C-c C-o' with a double
1366 Possible values for the command are:
1367 `emacs' The file will be visited by the current Emacs process.
1368 `default' Use the default application for this file type, which is the
1369 association for t in the list, most likely in the system-specific
1371 This can be used to overrule an unwanted setting in the
1372 system-specific variable.
1373 `system' Use the system command for opening files, like \"open\".
1374 This command is specified by the entry whose car is `system'.
1375 Most likely, the system-specific version of this variable
1376 does define this command, but you can overrule/replace it
1378 string A command to be executed by a shell; %s will be replaced
1379 by the path to the file.
1380 sexp A Lisp form which will be evaluated. The file path will
1381 be available in the Lisp variable `file'.
1382 For more examples, see the system specific constants
1383 `org-file-apps-defaults-macosx'
1384 `org-file-apps-defaults-windowsnt'
1385 `org-file-apps-defaults-gnu'."
1386 :group
'org-link-follow
1388 (cons (choice :value
""
1389 (string :tag
"Extension")
1390 (const :tag
"System command to open files" system
)
1391 (const :tag
"Default for unrecognized files" t
)
1392 (const :tag
"Remote file" remote
)
1393 (const :tag
"Links to a directory" directory
)
1394 (const :tag
"Any files that have Emacs modes"
1397 (const :tag
"Visit with Emacs" emacs
)
1398 (const :tag
"Use default" default
)
1399 (const :tag
"Use the system command" system
)
1400 (string :tag
"Command")
1401 (sexp :tag
"Lisp form")))))
1403 (defgroup org-refile nil
1404 "Options concerning refiling entries in Org-mode."
1408 (defcustom org-directory
"~/org"
1409 "Directory with org files.
1410 This is just a default location to look for Org files. There is no need
1411 at all to put your files into this directory. It is only used in the
1412 following situations:
1414 1. When a remember template specifies a target file that is not an
1415 absolute path. The path will then be interpreted relative to
1417 2. When a remember note is filed away in an interactive way (when exiting the
1418 note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
1419 with `org-directory' as the default path."
1421 :group
'org-remember
1424 (defcustom org-default-notes-file
(convert-standard-filename "~/.notes")
1425 "Default target for storing notes.
1426 Used by the hooks for remember.el. This can be a string, or nil to mean
1427 the value of `remember-data-file'.
1428 You can set this on a per-template basis with the variable
1429 `org-remember-templates'."
1431 :group
'org-remember
1433 (const :tag
"Default from remember-data-file" nil
)
1436 (defcustom org-goto-interface
'outline
1437 "The default interface to be used for `org-goto'.
1439 outline The interface shows an outline of the relevant file
1440 and the correct heading is found by moving through
1441 the outline or by searching with incremental search.
1442 outline-path-completion Headlines in the current buffer are offered via
1443 completion. This is the interface also used by
1444 the refile command."
1447 (const :tag
"Outline" outline
)
1448 (const :tag
"Outline-path-completion" outline-path-completion
)))
1450 (defcustom org-goto-max-level
5
1451 "Maximum level to be considered when running org-goto with refile interface."
1455 (defcustom org-reverse-note-order nil
1456 "Non-nil means, store new notes at the beginning of a file or entry.
1457 When nil, new notes will be filed to the end of a file or entry.
1458 This can also be a list with cons cells of regular expressions that
1459 are matched against file names, and values."
1460 :group
'org-remember
1463 (const :tag
"Reverse always" t
)
1464 (const :tag
"Reverse never" nil
)
1465 (repeat :tag
"By file name regexp"
1466 (cons regexp boolean
))))
1468 (defcustom org-refile-targets nil
1469 "Targets for refiling entries with \\[org-refile].
1470 This is list of cons cells. Each cell contains:
1471 - a specification of the files to be considered, either a list of files,
1472 or a symbol whose function or variable value will be used to retrieve
1473 a file name or a list of file names. If you use `org-agenda-files' for
1474 that, all agenda files will be scanned for targets. Nil means, consider
1475 headings in the current buffer.
1476 - A specification of how to find candidate refile targets. This may be
1478 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
1479 This tag has to be present in all target headlines, inheritance will
1481 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
1483 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
1484 headlines that are refiling targets.
1485 - a cons cell (:level . N). Any headline of level N is considered a target.
1486 Note that, when `org-odd-levels-only' is set, level corresponds to
1487 order in hierarchy, not to the number of stars.
1488 - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
1489 Note that, when `org-odd-levels-only' is set, level corresponds to
1490 order in hierarchy, not to the number of stars.
1492 When this variable is nil, all top-level headlines in the current buffer
1493 are used, equivalent to the value `((nil . (:level . 1))'."
1497 (choice :value org-agenda-files
1498 (const :tag
"All agenda files" org-agenda-files
)
1499 (const :tag
"Current buffer" nil
)
1500 (function) (variable) (file))
1501 (choice :tag
"Identify target headline by"
1502 (cons :tag
"Specific tag" (const :value
:tag
) (string))
1503 (cons :tag
"TODO keyword" (const :value
:todo
) (string))
1504 (cons :tag
"Regular expression" (const :value
:regexp
) (regexp))
1505 (cons :tag
"Level number" (const :value
:level
) (integer))
1506 (cons :tag
"Max Level number" (const :value
:maxlevel
) (integer))))))
1508 (defcustom org-refile-use-outline-path nil
1509 "Non-nil means, provide refile targets as paths.
1510 So a level 3 headline will be available as level1/level2/level3.
1511 When the value is `file', also include the file name (without directory)
1512 into the path. When `full-file-path', include the full file path."
1515 (const :tag
"Not" nil
)
1516 (const :tag
"Yes" t
)
1517 (const :tag
"Start with file name" file
)
1518 (const :tag
"Start with full file path" full-file-path
)))
1520 (defcustom org-outline-path-complete-in-steps t
1521 "Non-nil means, complete the outline path in hierarchical steps.
1522 When Org-mode uses the refile interface to select an outline path
1523 \(see variable `org-refile-use-outline-path'), the completion of
1524 the path can be done is a single go, or if can be done in steps down
1525 the headline hierarchy. Going in steps is probably the best if you
1526 do not use a special completion package like `ido' or `icicles'.
1527 However, when using these packages, going in one step can be very
1528 fast, while still showing the whole path to the entry."
1532 (defgroup org-todo nil
1533 "Options concerning TODO items in Org-mode."
1537 (defgroup org-progress nil
1538 "Options concerning Progress logging in Org-mode."
1542 (defvar org-todo-interpretation-widgets
1544 (:tag
"Sequence (cycling hits every state)" sequence
)
1545 (:tag
"Type (cycling directly to DONE)" type
))
1546 "The available interpretation symbols for customizing
1547 `org-todo-keywords'.
1548 Interested libraries should add to this list.")
1550 (defcustom org-todo-keywords
'((sequence "TODO" "DONE"))
1551 "List of TODO entry keyword sequences and their interpretation.
1552 \\<org-mode-map>This is a list of sequences.
1554 Each sequence starts with a symbol, either `sequence' or `type',
1555 indicating if the keywords should be interpreted as a sequence of
1556 action steps, or as different types of TODO items. The first
1557 keywords are states requiring action - these states will select a headline
1558 for inclusion into the global TODO list Org-mode produces. If one of
1559 the \"keywords\" is the vertical bat \"|\" the remaining keywords
1560 signify that no further action is necessary. If \"|\" is not found,
1561 the last keyword is treated as the only DONE state of the sequence.
1563 The command \\[org-todo] cycles an entry through these states, and one
1564 additional state where no keyword is present. For details about this
1565 cycling, see the manual.
1567 TODO keywords and interpretation can also be set on a per-file basis with
1568 the special #+SEQ_TODO and #+TYP_TODO lines.
1570 Each keyword can optionally specify a character for fast state selection
1571 \(in combination with the variable `org-use-fast-todo-selection')
1572 and specifiers for state change logging, using the same syntax
1573 that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
1574 that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
1575 indicates to record a time stamp each time this state is selected.
1577 Each keyword may also specify if a timestamp or a note should be
1578 recorded when entering or leaving the state, by adding additional
1579 characters in the parenthesis after the keyword. This looks like this:
1580 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
1581 record only the time of the state change. With X and Y being either
1582 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
1583 Y when leaving the state if and only if the *target* state does not
1584 define X. You may omit any of the fast-selection key or X or /Y,
1585 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
1587 For backward compatibility, this variable may also be just a list
1588 of keywords - in this case the interpretation (sequence or type) will be
1589 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
1591 :group
'org-keywords
1593 (repeat :tag
"Old syntax, just keywords"
1594 (string :tag
"Keyword"))
1595 (repeat :tag
"New syntax"
1598 :tag
"Interpretation"
1599 ;;Quick and dirty way to see
1600 ;;`org-todo-interpretations'. This takes the
1601 ;;place of item arguments
1609 org-todo-interpretation-widgets
))
1612 (string :tag
"Keyword"))))))
1614 (defvar org-todo-keywords-1 nil
1615 "All TODO and DONE keywords active in a buffer.")
1616 (make-variable-buffer-local 'org-todo-keywords-1
)
1617 (defvar org-todo-keywords-for-agenda nil
)
1618 (defvar org-done-keywords-for-agenda nil
)
1619 (defvar org-todo-keyword-alist-for-agenda nil
)
1620 (defvar org-tag-alist-for-agenda nil
)
1621 (defvar org-agenda-contributing-files nil
)
1622 (defvar org-not-done-keywords nil
)
1623 (make-variable-buffer-local 'org-not-done-keywords
)
1624 (defvar org-done-keywords nil
)
1625 (make-variable-buffer-local 'org-done-keywords
)
1626 (defvar org-todo-heads nil
)
1627 (make-variable-buffer-local 'org-todo-heads
)
1628 (defvar org-todo-sets nil
)
1629 (make-variable-buffer-local 'org-todo-sets
)
1630 (defvar org-todo-log-states nil
)
1631 (make-variable-buffer-local 'org-todo-log-states
)
1632 (defvar org-todo-kwd-alist nil
)
1633 (make-variable-buffer-local 'org-todo-kwd-alist
)
1634 (defvar org-todo-key-alist nil
)
1635 (make-variable-buffer-local 'org-todo-key-alist
)
1636 (defvar org-todo-key-trigger nil
)
1637 (make-variable-buffer-local 'org-todo-key-trigger
)
1639 (defcustom org-todo-interpretation
'sequence
1640 "Controls how TODO keywords are interpreted.
1641 This variable is in principle obsolete and is only used for
1642 backward compatibility, if the interpretation of todo keywords is
1643 not given already in `org-todo-keywords'. See that variable for
1646 :group
'org-keywords
1647 :type
'(choice (const sequence
)
1650 (defcustom org-use-fast-todo-selection t
1651 "Non-nil means, use the fast todo selection scheme with C-c C-t.
1652 This variable describes if and under what circumstances the cycling
1653 mechanism for TODO keywords will be replaced by a single-key, direct
1656 When nil, fast selection is never used.
1658 When the symbol `prefix', it will be used when `org-todo' is called with
1659 a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
1660 in an agenda buffer.
1662 When t, fast selection is used by default. In this case, the prefix
1663 argument forces cycling instead.
1665 In all cases, the special interface is only used if access keys have actually
1666 been assigned by the user, i.e. if keywords in the configuration are followed
1667 by a letter in parenthesis, like TODO(t)."
1670 (const :tag
"Never" nil
)
1671 (const :tag
"By default" t
)
1672 (const :tag
"Only with C-u C-c C-t" prefix
)))
1674 (defcustom org-provide-todo-statistics t
1675 "Non-nil means, update todo statistics after insert and toggle.
1676 When this is set, todo statistics is updated in the parent of the current
1677 entry each time a todo state is changed."
1681 (defcustom org-after-todo-state-change-hook nil
1682 "Hook which is run after the state of a TODO item was changed.
1683 The new state (a string with a TODO keyword, or nil) is available in the
1684 Lisp variable `state'."
1688 (defvar org-blocker-hook nil
1689 "Hook for functions that are allowed to block a state change.
1691 Each function gets as its single argument a property list, see
1692 `org-trigger-hook' for more information about this list.
1694 If any of the functions in this hook returns nil, the state change
1697 (defvar org-trigger-hook nil
1698 "Hook for functions that are triggered by a state change.
1700 Each function gets as its single argument a property list with at least
1701 the following elements:
1703 (:type type-of-change :position pos-at-entry-start
1704 :from old-state :to new-state)
1706 Depending on the type, more properties may be present.
1708 This mechanism is currently implemented for:
1712 :type todo-state-change
1713 :from previous state (keyword as a string), or nil, or a symbol
1714 'todo' or 'done', to indicate the general type of state.
1715 :to new state, like in :from")
1717 (defcustom org-enforce-todo-dependencies nil
1718 "Non-nil means, undone TODO entries will block switching the parent to DONE.
1719 Also, if a parent has an :ORDERED: property, switching an entry to DONE will
1720 be blocked if any prior sibling is not yet done.
1721 This variable needs to be set before org.el is loaded, and you need to
1722 restart Emacs after a change to make the change effective. The only way
1723 to change is while Emacs is running is through the customize interface."
1724 :set
(lambda (var val
)
1727 (add-hook 'org-blocker-hook
1728 'org-block-todo-from-children-or-siblings
)
1729 (remove-hook 'org-blocker-hook
1730 'org-block-todo-from-children-or-siblings
)))
1734 (defcustom org-enforce-todo-checkbox-dependencies nil
1735 "Non-nil means, unchecked boxes will block switching the parent to DONE.
1736 When this is nil, checkboxes have no influence on switching TODO states.
1737 When non-nil, you first need to check off all check boxes before the TODO
1738 entry can be switched to DONE.
1739 This variable needs to be set before org.el is loaded, and you need to
1740 restart Emacs after a change to make the change effective. The only way
1741 to change is while Emacs is running is through the customize interface."
1742 :set
(lambda (var val
)
1745 (add-hook 'org-blocker-hook
1746 'org-block-todo-from-checkboxes
)
1747 (remove-hook 'org-blocker-hook
1748 'org-block-todo-from-checkboxes
)))
1752 (defcustom org-todo-state-tags-triggers nil
1753 "Tag changes that should be triggered by TODO state changes.
1754 This is a list. Each entry is
1756 (state-change (tag . flag) .......)
1758 State-change can be a string with a state, and empty string to indicate the
1759 state that has no TODO keyword, or it can be one of the symbols `todo'
1760 or `done', meaning any not-done or done state, respectively."
1764 (cons (choice :tag
"When changing to"
1765 (const :tag
"Not-done state" todo
)
1766 (const :tag
"Done state" done
)
1767 (string :tag
"State"))
1769 (cons :tag
"Tag action"
1771 (choice (const :tag
"Add" t
) (const :tag
"Remove" nil
)))))))
1773 (defcustom org-log-done nil
1774 "Information to record when a task moves to the DONE state.
1776 Possible values are:
1778 nil Don't add anything, just change the keyword
1779 time Add a time stamp to the task
1780 note Prompt a closing note and add it with template `org-log-note-headings'
1782 This option can also be set with on a per-file-basis with
1784 #+STARTUP: nologdone
1786 #+STARTUP: lognotedone
1788 You can have local logging settings for a subtree by setting the LOGGING
1789 property to one or more of these keywords."
1791 :group
'org-progress
1793 (const :tag
"No logging" nil
)
1794 (const :tag
"Record CLOSED timestamp" time
)
1795 (const :tag
"Record CLOSED timestamp with closing note." note
)))
1797 ;; Normalize old uses of org-log-done.
1799 ((eq org-log-done t
) (setq org-log-done
'time
))
1800 ((and (listp org-log-done
) (memq 'done org-log-done
))
1801 (setq org-log-done
'note
)))
1803 (defcustom org-log-note-clock-out nil
1804 "Non-nil means, record a note when clocking out of an item.
1805 This can also be configured on a per-file basis by adding one of
1806 the following lines anywhere in the buffer:
1808 #+STARTUP: lognoteclock-out
1809 #+STARTUP: nolognoteclock-out"
1811 :group
'org-progress
1814 (defcustom org-log-done-with-time t
1815 "Non-nil means, the CLOSED time stamp will contain date and time.
1816 When nil, only the date will be recorded."
1817 :group
'org-progress
1820 (defcustom org-log-note-headings
1821 '((done .
"CLOSING NOTE %t")
1822 (state .
"State %-12s from %-12S %t")
1823 (note .
"Note taken on %t")
1825 "Headings for notes added to entries.
1826 The value is an alist, with the car being a symbol indicating the note
1827 context, and the cdr is the heading to be used. The heading may also be the
1829 %t in the heading will be replaced by a time stamp.
1830 %s will be replaced by the new TODO state, in double quotes.
1831 %S will be replaced by the old TODO state, in double quotes.
1832 %u will be replaced by the user name.
1833 %U will be replaced by the full user name."
1835 :group
'org-progress
1836 :type
'(list :greedy t
1837 (cons (const :tag
"Heading when closing an item" done
) string
)
1839 "Heading when changing todo state (todo sequence only)"
1841 (cons (const :tag
"Heading when just taking a note" note
) string
)
1842 (cons (const :tag
"Heading when clocking out" clock-out
) string
)))
1844 (unless (assq 'note org-log-note-headings
)
1845 (push '(note .
"%t") org-log-note-headings
))
1847 (defcustom org-log-into-drawer nil
1848 "Non-nil means, insert state change notes and time stamps into a drawer.
1849 When nil, state changes notes will be inserted after the headline and
1850 any scheduling and clock lines, but not inside a drawer.
1852 The value of this variable should be the name of the drawer to use.
1853 LOGBOOK is proposed at the default drawer for this purpose, you can
1854 also set this to a string to define the drawer of your choice.
1856 A value of t is also allowed, representing \"LOGBOOK\".
1858 If this variable is set, `org-log-state-notes-insert-after-drawers'
1861 :group
'org-progress
1863 (const :tag
"Not into a drawer" nil
)
1864 (const :tag
"LOGBOOK" t
)
1865 (string :tag
"Other")))
1867 (if (fboundp 'defvaralias
)
1868 (defvaralias 'org-log-state-notes-into-drawer
'org-log-into-drawer
))
1870 (defcustom org-log-state-notes-insert-after-drawers nil
1871 "Non-nil means, insert state change notes after any drawers in entry.
1872 Only the drawers that *immediately* follow the headline and the
1873 deadline/scheduled line are skipped.
1874 When nil, insert notes right after the heading and perhaps the line
1875 with deadline/scheduling if present.
1877 This variable will have no effect if `org-log-into-drawer' is
1880 :group
'org-progress
1883 (defcustom org-log-states-order-reversed t
1884 "Non-nil means, the latest state change note will be directly after heading.
1885 When nil, the notes will be orderer according to time."
1887 :group
'org-progress
1890 (defcustom org-log-repeat
'time
1891 "Non-nil means, record moving through the DONE state when triggering repeat.
1892 An auto-repeating tasks is immediately switched back to TODO when marked
1893 done. If you are not logging state changes (by adding \"@\" or \"!\" to
1894 the TODO keyword definition, or recording a closing note by setting
1895 `org-log-done', there will be no record of the task moving through DONE.
1896 This variable forces taking a note anyway. Possible values are:
1898 nil Don't force a record
1899 time Record a time stamp
1902 This option can also be set with on a per-file-basis with
1904 #+STARTUP: logrepeat
1905 #+STARTUP: lognoterepeat
1906 #+STARTUP: nologrepeat
1908 You can have local logging settings for a subtree by setting the LOGGING
1909 property to one or more of these keywords."
1911 :group
'org-progress
1913 (const :tag
"Don't force a record" nil
)
1914 (const :tag
"Force recording the DONE state" time
)
1915 (const :tag
"Force recording a note with the DONE state" note
)))
1918 (defgroup org-priorities nil
1919 "Priorities in Org-mode."
1920 :tag
"Org Priorities"
1923 (defcustom org-highest-priority ?A
1924 "The highest priority of TODO items. A character like ?A, ?B etc.
1925 Must have a smaller ASCII number than `org-lowest-priority'."
1926 :group
'org-priorities
1929 (defcustom org-lowest-priority ?C
1930 "The lowest priority of TODO items. A character like ?A, ?B etc.
1931 Must have a larger ASCII number than `org-highest-priority'."
1932 :group
'org-priorities
1935 (defcustom org-default-priority ?B
1936 "The default priority of TODO items.
1937 This is the priority an item get if no explicit priority is given."
1938 :group
'org-priorities
1941 (defcustom org-priority-start-cycle-with-default t
1942 "Non-nil means, start with default priority when starting to cycle.
1943 When this is nil, the first step in the cycle will be (depending on the
1944 command used) one higher or lower that the default priority."
1945 :group
'org-priorities
1948 (defgroup org-time nil
1949 "Options concerning time stamps and deadlines in Org-mode."
1953 (defcustom org-insert-labeled-timestamps-at-point nil
1954 "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
1955 When nil, these labeled time stamps are forces into the second line of an
1956 entry, just after the headline. When scheduling from the global TODO list,
1957 the time stamp will always be forced into the second line."
1961 (defconst org-time-stamp-formats
'("<%Y-%m-%d %a>" .
"<%Y-%m-%d %a %H:%M>")
1962 "Formats for `format-time-string' which are used for time stamps.
1963 It is not recommended to change this constant.")
1965 (defcustom org-time-stamp-rounding-minutes
'(0 5)
1966 "Number of minutes to round time stamps to.
1967 These are two values, the first applies when first creating a time stamp.
1968 The second applies when changing it with the commands `S-up' and `S-down'.
1969 When changing the time stamp, this means that it will change in steps
1970 of N minutes, as given by the second value.
1972 When a setting is 0 or 1, insert the time unmodified. Useful rounding
1973 numbers should be factors of 60, so for example 5, 10, 15.
1975 When this is larger than 1, you can still force an exact time-stamp by using
1976 a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
1977 and by using a prefix arg to `S-up/down' to specify the exact number
1978 of minutes to shift."
1980 :get
'(lambda (var) ; Make sure all entries have 5 elements
1981 (if (integerp (default-value var
))
1982 (list (default-value var
) 5)
1983 (default-value var
)))
1985 (integer :tag
"when inserting times")
1986 (integer :tag
"when modifying times")))
1988 ;; Normalize old customizations of this variable.
1989 (when (integerp org-time-stamp-rounding-minutes
)
1990 (setq org-time-stamp-rounding-minutes
1991 (list org-time-stamp-rounding-minutes
1992 org-time-stamp-rounding-minutes
)))
1994 (defcustom org-display-custom-times nil
1995 "Non-nil means, overlay custom formats over all time stamps.
1996 The formats are defined through the variable `org-time-stamp-custom-formats'.
1997 To turn this on on a per-file basis, insert anywhere in the file:
1998 #+STARTUP: customtime"
2002 (make-variable-buffer-local 'org-display-custom-times
)
2004 (defcustom org-time-stamp-custom-formats
2005 '("<%m/%d/%y %a>" .
"<%m/%d/%y %a %H:%M>") ; american
2006 "Custom formats for time stamps. See `format-time-string' for the syntax.
2007 These are overlayed over the default ISO format if the variable
2008 `org-display-custom-times' is set. Time like %H:%M should be at the
2009 end of the second format."
2013 (defun org-time-stamp-format (&optional long inactive
)
2014 "Get the right format for a time string."
2015 (let ((f (if long
(cdr org-time-stamp-formats
)
2016 (car org-time-stamp-formats
))))
2018 (concat "[" (substring f
1 -
1) "]")
2021 (defcustom org-time-clocksum-format
"%d:%02d"
2022 "The format string used when creating CLOCKSUM lines, or when
2023 org-mode generates a time duration."
2027 (defcustom org-deadline-warning-days
14
2028 "No. of days before expiration during which a deadline becomes active.
2029 This variable governs the display in sparse trees and in the agenda.
2030 When 0 or negative, it means use this number (the absolute value of it)
2031 even if a deadline has a different individual lead time specified.
2033 Custom commands can set this variable in the options section."
2035 :group
'org-agenda-daily
/weekly
2038 (defcustom org-read-date-prefer-future t
2039 "Non-nil means, assume future for incomplete date input from user.
2040 This affects the following situations:
2041 1. The user gives a day, but no month.
2042 For example, if today is the 15th, and you enter \"3\", Org-mode will
2043 read this as the third of *next* month. However, if you enter \"17\",
2044 it will be considered as *this* month.
2045 2. The user gives a month but not a year.
2046 For example, if it is april and you enter \"feb 2\", this will be read
2047 as feb 2, *next* year. \"May 5\", however, will be this year.
2049 Currently this does not work for ISO week specifications.
2051 When this option is nil, the current month and year will always be used
2056 (defcustom org-read-date-display-live t
2057 "Non-nil means, display current interpretation of date prompt live.
2058 This display will be in an overlay, in the minibuffer."
2062 (defcustom org-read-date-popup-calendar t
2063 "Non-nil means, pop up a calendar when prompting for a date.
2064 In the calendar, the date can be selected with mouse-1. However, the
2065 minibuffer will also be active, and you can simply enter the date as well.
2066 When nil, only the minibuffer will be available."
2069 (if (fboundp 'defvaralias
)
2070 (defvaralias 'org-popup-calendar-for-date-prompt
2071 'org-read-date-popup-calendar
))
2073 (defcustom org-read-date-minibuffer-setup-hook nil
2074 "Hook to be used to set up keys for the date/time interface.
2075 Add key definitions to `minibuffer-local-map', which will be a temporary
2080 (defcustom org-extend-today-until
0
2081 "The hour when your day really ends. Must be an integer.
2082 This has influence for the following applications:
2083 - When switching the agenda to \"today\". It it is still earlier than
2084 the time given here, the day recognized as TODAY is actually yesterday.
2085 - When a date is read from the user and it is still before the time given
2086 here, the current date and time will be assumed to be yesterday, 23:59.
2087 Also, timestamps inserted in remember templates follow this rule.
2089 IMPORTANT: This is a feature whose implementation is and likely will
2090 remain incomplete. Really, it is only here because past midnight seems to
2091 be the favorite working time of John Wiegley :-)"
2095 (defcustom org-edit-timestamp-down-means-later nil
2096 "Non-nil means, S-down will increase the time in a time stamp.
2097 When nil, S-up will increase."
2101 (defcustom org-calendar-follow-timestamp-change t
2102 "Non-nil means, make the calendar window follow timestamp changes.
2103 When a timestamp is modified and the calendar window is visible, it will be
2104 moved to the new date."
2108 (defgroup org-tags nil
2109 "Options concerning tags in Org-mode."
2113 (defcustom org-tag-alist nil
2114 "List of tags allowed in Org-mode files.
2115 When this list is nil, Org-mode will base TAG input on what is already in the
2117 The value of this variable is an alist, the car of each entry must be a
2118 keyword as a string, the cdr may be a character that is used to select
2119 that tag through the fast-tag-selection interface.
2120 See the manual for details."
2124 (cons (string :tag
"Tag name")
2125 (character :tag
"Access char"))
2126 (const :tag
"Start radio group" (:startgroup
))
2127 (const :tag
"End radio group" (:endgroup
))
2128 (const :tag
"New line" (:newline
)))))
2130 (defcustom org-tag-persistent-alist nil
2131 "List of tags that will always appear in all Org-mode files.
2132 This is in addition to any in buffer settings or customizations
2134 When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
2135 The value of this variable is an alist, the car of each entry must be a
2136 keyword as a string, the cdr may be a character that is used to select
2137 that tag through the fast-tag-selection interface.
2138 See the manual for details.
2139 To disable these tags on a per-file basis, insert anywhere in the file:
2144 (cons (string :tag
"Tag name")
2145 (character :tag
"Access char"))
2146 (const :tag
"Start radio group" (:startgroup
))
2147 (const :tag
"End radio group" (:endgroup
))
2148 (const :tag
"New line" (:newline
)))))
2150 (defvar org-file-tags nil
2151 "List of tags that can be inherited by all entries in the file.
2152 The tags will be inherited if the variable `org-use-tag-inheritance'
2153 says they should be.
2154 This variable is populated from #+TAG lines.")
2156 (defcustom org-use-fast-tag-selection
'auto
2157 "Non-nil means, use fast tag selection scheme.
2158 This is a special interface to select and deselect tags with single keys.
2159 When nil, fast selection is never used.
2160 When the symbol `auto', fast selection is used if and only if selection
2161 characters for tags have been configured, either through the variable
2162 `org-tag-alist' or through a #+TAGS line in the buffer.
2163 When t, fast selection is always used and selection keys are assigned
2164 automatically if necessary."
2167 (const :tag
"Always" t
)
2168 (const :tag
"Never" nil
)
2169 (const :tag
"When selection characters are configured" 'auto
)))
2171 (defcustom org-fast-tag-selection-single-key nil
2172 "Non-nil means, fast tag selection exits after first change.
2173 When nil, you have to press RET to exit it.
2174 During fast tag selection, you can toggle this flag with `C-c'.
2175 This variable can also have the value `expert'. In this case, the window
2176 displaying the tags menu is not even shown, until you press C-c again."
2179 (const :tag
"No" nil
)
2180 (const :tag
"Yes" t
)
2181 (const :tag
"Expert" expert
)))
2183 (defvar org-fast-tag-selection-include-todo nil
2184 "Non-nil means, fast tags selection interface will also offer TODO states.
2185 This is an undocumented feature, you should not rely on it.")
2187 (defcustom org-tags-column
(if (featurep 'xemacs
) -
76 -
77)
2188 "The column to which tags should be indented in a headline.
2189 If this number is positive, it specifies the column. If it is negative,
2190 it means that the tags should be flushright to that column. For example,
2191 -80 works well for a normal 80 character screen."
2195 (defcustom org-auto-align-tags t
2196 "Non-nil means, realign tags after pro/demotion of TODO state change.
2197 These operations change the length of a headline and therefore shift
2198 the tags around. With this options turned on, after each such operation
2199 the tags are again aligned to `org-tags-column'."
2203 (defcustom org-use-tag-inheritance t
2204 "Non-nil means, tags in levels apply also for sublevels.
2205 When nil, only the tags directly given in a specific line apply there.
2206 This may also be a list of tags that should be inherited, or a regexp that
2207 matches tags that should be inherited. Additional control is possible
2208 with the variable `org-tags-exclude-from-inheritance' which gives an
2209 explicit list of tags to be excluded from inheritance., even if the value of
2210 `org-use-tag-inheritance' would select it for inheritance.
2212 If this option is t, a match early-on in a tree can lead to a large
2213 number of matches in the subtree when constructing the agenda or creating
2214 a sparse tree. If you only want to see the first match in a tree during
2215 a search, check out the variable `org-tags-match-list-sublevels'."
2218 (const :tag
"Not" nil
)
2219 (const :tag
"Always" t
)
2220 (repeat :tag
"Specific tags" (string :tag
"Tag"))
2221 (regexp :tag
"Tags matched by regexp")))
2223 (defcustom org-tags-exclude-from-inheritance nil
2224 "List of tags that should never be inherited.
2225 This is a way to exclude a few tags from inheritance. For way to do
2226 the opposite, to actively allow inheritance for selected tags,
2227 see the variable `org-use-tag-inheritance'."
2229 :type
'(repeat (string :tag
"Tag")))
2231 (defun org-tag-inherit-p (tag)
2232 "Check if TAG is one that should be inherited."
2234 ((member tag org-tags-exclude-from-inheritance
) nil
)
2235 ((eq org-use-tag-inheritance t
) t
)
2236 ((not org-use-tag-inheritance
) nil
)
2237 ((stringp org-use-tag-inheritance
)
2238 (string-match org-use-tag-inheritance tag
))
2239 ((listp org-use-tag-inheritance
)
2240 (member tag org-use-tag-inheritance
))
2241 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
2243 (defcustom org-tags-match-list-sublevels t
2244 "Non-nil means list also sublevels of headlines matching a search.
2245 This variable applies to tags/property searches, and also to stuck
2246 projects because this search is based on a tags match as well.
2248 Because of tag inheritance (see variable `org-use-tag-inheritance'),
2249 the sublevels of a headline matching a tag search often also match
2250 the same search. Listing all of them can create very long lists.
2251 Setting this variable to nil causes subtrees of a match to be skipped.
2253 As a special case, if the tag search is restricted to TODO items, the
2254 value of this variable is ignored and sublevels are always checked, to
2255 make sure all corresponding TODO items find their way into the list.
2257 This variable is semi-obsolete and probably should always be true. It
2258 is better to limit inheritance to certain tags using the variables
2259 `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
2262 (const :tag
"No, don't list them" nil
)
2263 (const :tag
"Yes, do list them" t
)
2264 (const :tag
"List them, indented with leading dots" indented
)))
2266 (defvar org-tags-history nil
2267 "History of minibuffer reads for tags.")
2268 (defvar org-last-tags-completion-table nil
2269 "The last used completion table for tags.")
2270 (defvar org-after-tags-change-hook nil
2271 "Hook that is run after the tags in a line have changed.")
2273 (defgroup org-properties nil
2274 "Options concerning properties in Org-mode."
2275 :tag
"Org Properties"
2278 (defcustom org-property-format
"%-10s %s"
2279 "How property key/value pairs should be formatted by `indent-line'.
2280 When `indent-line' hits a property definition, it will format the line
2281 according to this format, mainly to make sure that the values are
2282 lined-up with respect to each other."
2283 :group
'org-properties
2286 (defcustom org-use-property-inheritance nil
2287 "Non-nil means, properties apply also for sublevels.
2289 This setting is chiefly used during property searches. Turning it on can
2290 cause significant overhead when doing a search, which is why it is not
2293 When nil, only the properties directly given in the current entry count.
2294 When t, every property is inherited. The value may also be a list of
2295 properties that should have inheritance, or a regular expression matching
2296 properties that should be inherited.
2298 However, note that some special properties use inheritance under special
2299 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
2300 and the properties ending in \"_ALL\" when they are used as descriptor
2301 for valid values of a property.
2303 Note for programmers:
2304 When querying an entry with `org-entry-get', you can control if inheritance
2305 should be used. By default, `org-entry-get' looks only at the local
2306 properties. You can request inheritance by setting the inherit argument
2307 to t (to force inheritance) or to `selective' (to respect the setting
2309 :group
'org-properties
2311 (const :tag
"Not" nil
)
2312 (const :tag
"Always" t
)
2313 (repeat :tag
"Specific properties" (string :tag
"Property"))
2314 (regexp :tag
"Properties matched by regexp")))
2316 (defun org-property-inherit-p (property)
2317 "Check if PROPERTY is one that should be inherited."
2319 ((eq org-use-property-inheritance t
) t
)
2320 ((not org-use-property-inheritance
) nil
)
2321 ((stringp org-use-property-inheritance
)
2322 (string-match org-use-property-inheritance property
))
2323 ((listp org-use-property-inheritance
)
2324 (member property org-use-property-inheritance
))
2325 (t (error "Invalid setting of `org-use-property-inheritance'"))))
2327 (defcustom org-columns-default-format
"%25ITEM %TODO %3PRIORITY %TAGS"
2328 "The default column format, if no other format has been defined.
2329 This variable can be set on the per-file basis by inserting a line
2331 #+COLUMNS: %25ITEM ....."
2332 :group
'org-properties
2335 (defcustom org-columns-ellipses
".."
2336 "The ellipses to be used when a field in column view is truncated.
2337 When this is the empty string, as many characters as possible are shown,
2338 but then there will be no visual indication that the field has been truncated.
2339 When this is a string of length N, the last N characters of a truncated
2340 field are replaced by this string. If the column is narrower than the
2341 ellipses string, only part of the ellipses string will be shown."
2342 :group
'org-properties
2345 (defcustom org-columns-modify-value-for-display-function nil
2346 "Function that modifies values for display in column view.
2347 For example, it can be used to cut out a certain part from a time stamp.
2348 The function must take 2 arguments:
2350 column-title The title of the column (*not* the property name)
2351 value The value that should be modified.
2353 The function should return the value that should be displayed,
2354 or nil if the normal value should be used."
2355 :group
'org-properties
2358 (defcustom org-effort-property
"Effort"
2359 "The property that is being used to keep track of effort estimates.
2360 Effort estimates given in this property need to have the format H:MM."
2361 :group
'org-properties
2362 :group
'org-progress
2363 :type
'(string :tag
"Property"))
2365 (defconst org-global-properties-fixed
2366 '(("VISIBILITY_ALL" .
"folded children content all"))
2367 "List of property/value pairs that can be inherited by any entry.
2369 These are fixed values, for the preset properties. The user variable
2370 that can be used to add to this list is `org-global-properties'.
2372 The entries in this list are cons cells where the car is a property
2373 name and cdr is a string with the value. If the value represents
2374 multiple items like an \"_ALL\" property, separate the items by
2377 (defcustom org-global-properties nil
2378 "List of property/value pairs that can be inherited by any entry.
2380 This list will be combined with the constant `org-global-properties-fixed'.
2382 The entries in this list are cons cells where the car is a property
2383 name and cdr is a string with the value.
2385 You can set buffer-local values for the same purpose in the variable
2386 `org-file-properties' this by adding lines like
2388 #+PROPERTY: NAME VALUE"
2389 :group
'org-properties
2391 (cons (string :tag
"Property")
2392 (string :tag
"Value"))))
2394 (defvar org-file-properties nil
2395 "List of property/value pairs that can be inherited by any entry.
2396 Valid for the current buffer.
2397 This variable is populated from #+PROPERTY lines.")
2398 (make-variable-buffer-local 'org-file-properties
)
2400 (defgroup org-agenda nil
2401 "Options concerning agenda views in Org-mode."
2405 (defvar org-category nil
2406 "Variable used by org files to set a category for agenda display.
2407 Such files should use a file variable to set it, for example
2409 # -*- mode: org; org-category: \"ELisp\"
2411 or contain a special line
2415 If the file does not specify a category, then file's base name
2417 (make-variable-buffer-local 'org-category
)
2418 (put 'org-category
'safe-local-variable
'(lambda (x) (or (symbolp x
) (stringp x
))))
2420 (defcustom org-agenda-files nil
2421 "The files to be used for agenda display.
2422 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
2423 \\[org-remove-file]. You can also use customize to edit the list.
2425 If an entry is a directory, all files in that directory that are matched by
2426 `org-agenda-file-regexp' will be part of the file list.
2428 If the value of the variable is not a list but a single file name, then
2429 the list of agenda files is actually stored and maintained in that file, one
2430 agenda file per line."
2433 (repeat :tag
"List of files and directories" file
)
2434 (file :tag
"Store list in a file\n" :value
"~/.agenda_files")))
2436 (defcustom org-agenda-file-regexp
"\\`[^.].*\\.org\\'"
2437 "Regular expression to match files for `org-agenda-files'.
2438 If any element in the list in that variable contains a directory instead
2439 of a normal file, all files in that directory that are matched by this
2440 regular expression will be included."
2444 (defcustom org-agenda-text-search-extra-files nil
2445 "List of extra files to be searched by text search commands.
2446 These files will be search in addition to the agenda files by the
2447 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
2448 Note that these files will only be searched for text search commands,
2449 not for the other agenda views like todo lists, tag searches or the weekly
2450 agenda. This variable is intended to list notes and possibly archive files
2451 that should also be searched by these two commands.
2452 In fact, if the first element in the list is the symbol `agenda-archives',
2453 than all archive files of all agenda files will be added to the search
2456 :type
'(set :greedy t
2457 (const :tag
"Agenda Archives" agenda-archives
)
2458 (repeat :inline t
(file))))
2460 (if (fboundp 'defvaralias
)
2461 (defvaralias 'org-agenda-multi-occur-extra-files
2462 'org-agenda-text-search-extra-files
))
2464 (defcustom org-agenda-skip-unavailable-files nil
2465 "Non-nil means to just skip non-reachable files in `org-agenda-files'.
2466 A nil value means to remove them, after a query, from the list."
2470 (defcustom org-calendar-to-agenda-key
[?c
]
2471 "The key to be installed in `calendar-mode-map' for switching to the agenda.
2472 The command `org-calendar-goto-agenda' will be bound to this key. The
2473 default is the character `c' because then `c' can be used to switch back and
2474 forth between agenda and calendar."
2478 (defcustom org-calendar-agenda-action-key
[?k
]
2479 "The key to be installed in `calendar-mode-map' for agenda-action.
2480 The command `org-agenda-action' will be bound to this key. The
2481 default is the character `k' because we use the same key in the agenda."
2485 (eval-after-load "calendar"
2487 (org-defkey calendar-mode-map org-calendar-to-agenda-key
2488 'org-calendar-goto-agenda
)
2489 (org-defkey calendar-mode-map org-calendar-agenda-action-key
2490 'org-agenda-action
)))
2492 (defgroup org-latex nil
2493 "Options for embedding LaTeX code into Org-mode."
2497 (defcustom org-format-latex-options
2498 '(:foreground default
:background default
:scale
1.0
2499 :html-foreground
"Black" :html-background
"Transparent" :html-scale
1.0
2500 :matchers
("begin" "$1" "$" "$$" "\\(" "\\["))
2501 "Options for creating images from LaTeX fragments.
2502 This is a property list with the following properties:
2503 :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
2504 `default' means use the foreground of the default face.
2505 :background the background color, or \"Transparent\".
2506 `default' means use the background of the default face.
2507 :scale a scaling factor for the size of the images.
2508 :html-foreground, :html-background, :html-scale
2509 the same numbers for HTML export.
2510 :matchers a list indicating which matchers should be used to
2511 find LaTeX fragments. Valid members of this list are:
2512 \"begin\" find environments
2513 \"$1\" find single characters surrounded by $.$
2514 \"$\" find math expressions surrounded by $...$
2515 \"$$\" find math expressions surrounded by $$....$$
2516 \"\\(\" find math expressions surrounded by \\(...\\)
2517 \"\\ [\" find math expressions surrounded by \\ [...\\]"
2521 (defcustom org-format-latex-header
"\\documentclass{article}
2522 \\usepackage{fullpage} % do not remove
2523 \\usepackage{amssymb}
2524 \\usepackage[usenames]{color}
2525 \\usepackage{amsmath}
2526 \\usepackage{latexsym}
2527 \\usepackage[mathscr]{eucal}
2528 \\pagestyle{empty} % do not remove"
2529 "The document header used for processing LaTeX fragments."
2534 (defgroup org-font-lock nil
2535 "Font-lock settings for highlighting in Org-mode."
2536 :tag
"Org Font Lock"
2539 (defcustom org-level-color-stars-only nil
2540 "Non-nil means fontify only the stars in each headline.
2541 When nil, the entire headline is fontified.
2542 Changing it requires restart of `font-lock-mode' to become effective
2543 also in regions already fontified."
2544 :group
'org-font-lock
2547 (defcustom org-hide-leading-stars nil
2548 "Non-nil means, hide the first N-1 stars in a headline.
2549 This works by using the face `org-hide' for these stars. This
2550 face is white for a light background, and black for a dark
2551 background. You may have to customize the face `org-hide' to
2553 Changing it requires restart of `font-lock-mode' to become effective
2554 also in regions already fontified.
2555 You may also set this on a per-file basis by adding one of the following
2556 lines to the buffer:
2558 #+STARTUP: hidestars
2559 #+STARTUP: showstars"
2560 :group
'org-font-lock
2563 (defcustom org-fontify-done-headline nil
2564 "Non-nil means, change the face of a headline if it is marked DONE.
2565 Normally, only the TODO/DONE keyword indicates the state of a headline.
2566 When this is non-nil, the headline after the keyword is set to the
2567 `org-headline-done' as an additional indication."
2568 :group
'org-font-lock
2571 (defcustom org-fontify-emphasized-text t
2572 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
2573 Changing this variable requires a restart of Emacs to take effect."
2574 :group
'org-font-lock
2577 (defcustom org-highlight-latex-fragments-and-specials nil
2578 "Non-nil means, fontify what is treated specially by the exporters."
2579 :group
'org-font-lock
2582 (defcustom org-hide-emphasis-markers nil
2583 "Non-nil mean font-lock should hide the emphasis marker characters."
2584 :group
'org-font-lock
2587 (defvar org-emph-re nil
2588 "Regular expression for matching emphasis.")
2589 (defvar org-verbatim-re nil
2590 "Regular expression for matching verbatim text.")
2591 (defvar org-emphasis-regexp-components
) ; defined just below
2592 (defvar org-emphasis-alist
) ; defined just below
2593 (defun org-set-emph-re (var val
)
2594 "Set variable and compute the emphasis regular expression."
2596 (when (and (boundp 'org-emphasis-alist
)
2597 (boundp 'org-emphasis-regexp-components
)
2598 org-emphasis-alist org-emphasis-regexp-components
)
2599 (let* ((e org-emphasis-regexp-components
)
2605 (body1 (concat body
"*?"))
2606 (markers (mapconcat 'car org-emphasis-alist
""))
2607 (vmarkers (mapconcat
2608 (lambda (x) (if (eq (nth 4 x
) 'verbatim
) (car x
) ""))
2609 org-emphasis-alist
"")))
2610 ;; make sure special characters appear at the right position in the class
2611 (if (string-match "\\^" markers
)
2612 (setq markers
(concat (replace-match "" t t markers
) "^")))
2613 (if (string-match "-" markers
)
2614 (setq markers
(concat (replace-match "" t t markers
) "-")))
2615 (if (string-match "\\^" vmarkers
)
2616 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "^")))
2617 (if (string-match "-" vmarkers
)
2618 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "-")))
2620 (setq body1
(concat body1
"\\(?:\n" body
"*?\\)\\{0,"
2621 (int-to-string nl
) "\\}")))
2624 (concat "\\([" pre
"]\\|^\\)"
2626 "\\([" markers
"]\\)"
2634 "\\([" post
"]\\|$\\)"))
2635 (setq org-verbatim-re
2636 (concat "\\([" pre
"]\\|^\\)"
2638 "\\([" vmarkers
"]\\)"
2646 "\\([" post
"]\\|$\\)")))))
2648 (defcustom org-emphasis-regexp-components
2649 '(" \t('\"{" "- \t.,:!?;'\")}" " \t\r\n,\"'" "." 1)
2650 "Components used to build the regular expression for emphasis.
2651 This is a list with 6 entries. Terminology: In an emphasis string
2652 like \" *strong word* \", we call the initial space PREMATCH, the final
2653 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
2654 and \"trong wor\" is the body. The different components in this variable
2655 specify what is allowed/forbidden in each part:
2657 pre Chars allowed as prematch. Beginning of line will be allowed too.
2658 post Chars allowed as postmatch. End of line will be allowed too.
2659 border The chars *forbidden* as border characters.
2660 body-regexp A regexp like \".\" to match a body character. Don't use
2661 non-shy groups here, and don't allow newline here.
2662 newline The maximum number of newlines allowed in an emphasis exp.
2664 Use customize to modify this, or restart Emacs after changing it."
2665 :group
'org-font-lock
2666 :set
'org-set-emph-re
2668 (sexp :tag
"Allowed chars in pre ")
2669 (sexp :tag
"Allowed chars in post ")
2670 (sexp :tag
"Forbidden chars in border ")
2671 (sexp :tag
"Regexp for body ")
2672 (integer :tag
"number of newlines allowed")
2673 (option (boolean :tag
"Please ignore this button"))))
2675 (defcustom org-emphasis-alist
2676 `(("*" bold
"<b>" "</b>")
2677 ("/" italic
"<i>" "</i>")
2678 ("_" underline
"<span style=\"text-decoration:underline;\">" "</span>")
2679 ("=" org-code
"<code>" "</code>" verbatim
)
2680 ("~" org-verbatim
"<code>" "</code>" verbatim
)
2681 ("+" ,(if (featurep 'xemacs
) 'org-table
'(:strike-through t
))
2684 "Special syntax for emphasized text.
2685 Text starting and ending with a special character will be emphasized, for
2686 example *bold*, _underlined_ and /italic/. This variable sets the marker
2687 characters, the face to be used by font-lock for highlighting in Org-mode
2688 Emacs buffers, and the HTML tags to be used for this.
2689 For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
2690 Use customize to modify this, or restart Emacs after changing it."
2691 :group
'org-font-lock
2692 :set
'org-set-emph-re
2695 (string :tag
"Marker character")
2697 (face :tag
"Font-lock-face")
2698 (plist :tag
"Face property list"))
2699 (string :tag
"HTML start tag")
2700 (string :tag
"HTML end tag")
2701 (option (const verbatim
)))))
2703 ;;; Miscellaneous options
2705 (defgroup org-completion nil
2706 "Completion in Org-mode."
2707 :tag
"Org Completion"
2710 (defcustom org-completion-use-ido nil
2711 "Non-nil means, use ido completion wherever possible.
2712 Note that `ido-mode' must be active for this variable to be relevant.
2713 If you decide to turn this variable on, you might well want to turn off
2714 `org-outline-path-complete-in-steps'."
2715 :group
'org-completion
2718 (defcustom org-completion-fallback-command
'hippie-expand
2719 "The expansion command called by \\[org-complete] in normal context.
2720 Normal means, no org-mode-specific context."
2721 :group
'org-completion
2724 ;;; Functions and variables from ther packages
2725 ;; Declared here to avoid compiler warnings
2728 (defvar outline-mode-menu-heading
)
2729 (defvar outline-mode-menu-show
)
2730 (defvar outline-mode-menu-hide
)
2731 (defvar zmacs-regions
) ; XEmacs regions
2734 (defvar mark-active
)
2737 (declare-function find-library-name
"find-func" (library))
2738 (declare-function calendar-absolute-from-iso
"cal-iso" (date))
2739 (declare-function calendar-forward-day
"cal-move" (arg))
2740 (declare-function calendar-goto-date
"cal-move" (date))
2741 (declare-function calendar-goto-today
"cal-move" ())
2742 (declare-function calendar-iso-from-absolute
"cal-iso" (date))
2743 (defvar calc-embedded-close-formula
)
2744 (defvar calc-embedded-open-formula
)
2745 (declare-function cdlatex-tab
"ext:cdlatex" ())
2746 (declare-function dired-get-filename
"dired" (&optional localp no-error-if-not-filep
))
2747 (defvar font-lock-unfontify-region-function
)
2748 (declare-function iswitchb-mode
"iswitchb" (&optional arg
))
2749 (declare-function iswitchb-read-buffer
(prompt &optional default require-match start matches-set
))
2750 (defvar iswitchb-temp-buflist
)
2751 (declare-function org-gnus-follow-link
"org-gnus" (&optional group article
))
2752 (defvar org-agenda-tags-todo-honor-ignore-options
)
2753 (declare-function org-agenda-skip
"org-agenda" ())
2754 (declare-function org-format-agenda-item
"org-agenda"
2755 (extra txt
&optional category tags dotime noprefix remove-re
))
2756 (declare-function org-agenda-new-marker
"org-agenda" (&optional pos
))
2757 (declare-function org-agenda-change-all-lines
"org-agenda"
2758 (newhead hdmarker
&optional fixface just-this
))
2759 (declare-function org-agenda-set-restriction-lock
"org-agenda" (&optional type
))
2760 (declare-function org-agenda-maybe-redo
"org-agenda" ())
2761 (declare-function org-agenda-save-markers-for-cut-and-paste
"org-agenda"
2763 (declare-function org-agenda-copy-local-variable
"org-agenda" (var))
2764 (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
2765 "org-agenda" (&optional end
))
2767 (declare-function parse-time-string
"parse-time" (string))
2768 (declare-function remember
"remember" (&optional initial
))
2769 (declare-function remember-buffer-desc
"remember" ())
2770 (declare-function remember-finalize
"remember" ())
2771 (defvar remember-save-after-remembering
)
2772 (defvar remember-data-file
)
2773 (defvar remember-register
)
2774 (defvar remember-buffer
)
2775 (defvar remember-handler-functions
)
2776 (defvar remember-annotation-functions
)
2777 (defvar texmathp-why
)
2778 (declare-function speedbar-line-directory
"speedbar" (&optional depth
))
2779 (declare-function table--at-cell-p
"table" (position &optional object at-column
))
2781 (defvar w3m-current-url
)
2782 (defvar w3m-current-title
)
2784 (defvar org-latex-regexps
)
2786 ;;; Autoload and prepare some org modules
2788 ;; Some table stuff that needs to be defined here, because it is used
2789 ;; by the functions setting up org-mode or checking for table context.
2791 (defconst org-table-any-line-regexp
"^[ \t]*\\(|\\|\\+-[-+]\\)"
2792 "Detects an org-type or table-type table.")
2793 (defconst org-table-line-regexp
"^[ \t]*|"
2794 "Detects an org-type table line.")
2795 (defconst org-table-dataline-regexp
"^[ \t]*|[^-]"
2796 "Detects an org-type table line.")
2797 (defconst org-table-hline-regexp
"^[ \t]*|-"
2798 "Detects an org-type table hline.")
2799 (defconst org-table1-hline-regexp
"^[ \t]*\\+-[-+]"
2800 "Detects a table-type table hline.")
2801 (defconst org-table-any-border-regexp
"^[ \t]*[^|+ \t]"
2802 "Searching from within a table (any type) this finds the first line
2803 outside the table.")
2805 ;; Autoload the functions in org-table.el that are needed by functions here.
2808 (org-autoload "org-table"
2809 '(org-table-align org-table-begin org-table-blank-field
2810 org-table-convert org-table-convert-region org-table-copy-down
2811 org-table-copy-region org-table-create
2812 org-table-create-or-convert-from-region
2813 org-table-create-with-table.el org-table-current-dline
2814 org-table-cut-region org-table-delete-column org-table-edit-field
2815 org-table-edit-formulas org-table-end org-table-eval-formula
2816 org-table-export org-table-field-info
2817 org-table-get-stored-formulas org-table-goto-column
2818 org-table-hline-and-move org-table-import org-table-insert-column
2819 org-table-insert-hline org-table-insert-row org-table-iterate
2820 org-table-justify-field-maybe org-table-kill-row
2821 org-table-maybe-eval-formula org-table-maybe-recalculate-line
2822 org-table-move-column org-table-move-column-left
2823 org-table-move-column-right org-table-move-row
2824 org-table-move-row-down org-table-move-row-up
2825 org-table-next-field org-table-next-row org-table-paste-rectangle
2826 org-table-previous-field org-table-recalculate
2827 org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
2828 org-table-toggle-coordinate-overlays
2829 org-table-toggle-formula-debugger org-table-wrap-region
2830 orgtbl-mode turn-on-orgtbl org-table-to-lisp
)))
2832 (defun org-at-table-p (&optional table-type
)
2833 "Return t if the cursor is inside an org-type table.
2834 If TABLE-TYPE is non-nil, also check for table.el-type tables."
2835 (if org-enable-table-editor
2837 (beginning-of-line 1)
2838 (looking-at (if table-type org-table-any-line-regexp
2839 org-table-line-regexp
)))
2841 (defsubst org-table-p
() (org-at-table-p))
2843 (defun org-at-table.el-p
()
2844 "Return t if and only if we are at a table.el table."
2845 (and (org-at-table-p 'any
)
2847 (goto-char (org-table-begin 'any
))
2848 (looking-at org-table1-hline-regexp
))))
2849 (defun org-table-recognize-table.el
()
2850 "If there is a table.el table nearby, recognize it and move into it."
2851 (if org-table-tab-recognizes-table.el
2852 (if (org-at-table.el-p
)
2854 (beginning-of-line 1)
2855 (if (looking-at org-table-dataline-regexp
)
2857 (if (looking-at org-table1-hline-regexp
)
2859 (beginning-of-line 2)
2860 (if (looking-at org-table-any-border-regexp
)
2861 (beginning-of-line -
1)))))
2862 (if (re-search-forward "|" (org-table-end t
) t
)
2865 (if (table--at-cell-p (point))
2867 (message "recognizing table.el table...")
2868 (table-recognize-table)
2869 (message "recognizing table.el table...done")))
2870 (error "This should not happen..."))
2875 (defun org-at-table-hline-p ()
2876 "Return t if the cursor is inside a hline in a table."
2877 (if org-enable-table-editor
2879 (beginning-of-line 1)
2880 (looking-at org-table-hline-regexp
))
2883 (defvar org-table-clean-did-remove-column nil
)
2885 (defun org-table-map-tables (function)
2886 "Apply FUNCTION to the start of all tables in the buffer."
2890 (goto-char (point-min))
2891 (while (re-search-forward org-table-any-line-regexp nil t
)
2892 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
2893 (beginning-of-line 1)
2894 (if (looking-at org-table-line-regexp
)
2895 (save-excursion (funcall function
)))
2896 (re-search-forward org-table-any-border-regexp nil
1))))
2897 (message "Mapping tables: done"))
2899 ;; Declare and autoload functions from org-exp.el
2901 (declare-function org-default-export-plist
"org-exp")
2902 (declare-function org-infile-export-plist
"org-exp")
2903 (declare-function org-get-current-options
"org-exp")
2905 (org-autoload "org-exp"
2906 '(org-export org-export-as-ascii org-export-visible
2907 org-insert-export-options-template org-export-as-html-and-open
2908 org-export-as-html-batch org-export-as-html-to-buffer
2909 org-replace-region-by-html org-export-region-as-html
2910 org-export-as-html org-export-icalendar-this-file
2911 org-export-icalendar-all-agenda-files
2912 org-table-clean-before-export
2913 org-export-icalendar-combine-agenda-files org-export-as-xoxo
)))
2915 ;; Declare and autoload functions from org-agenda.el
2918 (org-autoload "org-agenda"
2919 '(org-agenda org-agenda-list org-search-view
2920 org-todo-list org-tags-view org-agenda-list-stuck-projects
2921 org-diary org-agenda-to-appt
2922 org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
)))
2924 ;; Autoload org-remember
2927 (org-autoload "org-remember"
2928 '(org-remember-insinuate org-remember-annotation
2929 org-remember-apply-template org-remember org-remember-handler
)))
2931 ;; Autoload org-clock.el
2934 (declare-function org-clock-save-markers-for-cut-and-paste
"org-clock"
2936 (declare-function org-clock-update-mode-line
"org-clock" ())
2937 (defvar org-clock-start-time
)
2938 (defvar org-clock-marker
(make-marker)
2939 "Marker recording the last clock-in.")
2944 '(org-clock-in org-clock-out org-clock-cancel
2945 org-clock-goto org-clock-sum org-clock-display
2946 org-clock-remove-overlays org-clock-report
2947 org-clocktable-shift org-dblock-write
:clocktable
2948 org-get-clocktable
)))
2950 (defun org-clock-update-time-maybe ()
2951 "If this is a CLOCK line, update it and return t.
2952 Otherwise, return nil."
2955 (beginning-of-line 1)
2956 (skip-chars-forward " \t")
2957 (when (looking-at org-clock-string
)
2958 (let ((re (concat "[ \t]*" org-clock-string
2959 " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
2960 "\\([ \t]*=>.*\\)?\\)?"))
2963 ((not (looking-at re
))
2965 ((not (match-end 2))
2966 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
2967 (> org-clock-marker
(point))
2968 (<= org-clock-marker
(point-at-eol)))
2969 ;; The clock is running here
2970 (setq org-clock-start-time
2972 (org-parse-time-string (match-string 1))))
2973 (org-clock-update-mode-line)))
2975 (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
2977 (setq ts
(match-string 1)
2978 te
(match-string 3))
2979 (setq s
(- (time-to-seconds
2980 (apply 'encode-time
(org-parse-time-string te
)))
2982 (apply 'encode-time
(org-parse-time-string ts
))))
2985 h
(floor (/ s
3600))
2989 (insert " => " (format (if neg
"-%d:%02d" "%2d:%02d") h m
))
2992 (defun org-check-running-clock ()
2993 "Check if the current buffer contains the running clock.
2994 If yes, offer to stop it and to save the buffer with the changes."
2995 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
2996 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
2999 (when (y-or-n-p "Save changed buffer?")
3002 (defun org-clocktable-try-shift (dir n
)
3003 "Check if this line starts a clock table, if yes, shift the time block."
3004 (when (org-match-line "#\\+BEGIN: clocktable\\>")
3005 (org-clocktable-shift dir n
)))
3007 ;; Autoload org-timer.el
3012 '(org-timer-start org-timer org-timer-item
3013 org-timer-change-times-in-region
)))
3015 ;; Autoload org-feed.el
3020 '(org-feed-update org-feed-update-all org-feed-goto-inbox
)))
3023 ;; Autoload archiving code
3024 ;; The stuff that is needed for cycling and tags has to be defined here.
3026 (defgroup org-archive nil
3027 "Options concerning archiving in Org-mode."
3029 :group
'org-structure
)
3031 (defcustom org-archive-location
"%s_archive::"
3032 "The location where subtrees should be archived.
3034 The value of this variable is a string, consisting of two parts,
3035 separated by a double-colon. The first part is a filename and
3036 the second part is a headline.
3038 When the filename is omitted, archiving happens in the same file.
3039 %s in the filename will be replaced by the current file
3040 name (without the directory part). Archiving to a different file
3041 is useful to keep archived entries from contributing to the
3044 The archived entries will be filed as subtrees of the specified
3045 headline. When the headline is omitted, the subtrees are simply
3046 filed away at the end of the file, as top-level entries. Also in
3047 the heading you can use %s to represent the file name, this can be
3048 useful when using the same archive for a number of different files.
3050 Here are a few examples:
3052 If the current file is Projects.org, archive in file
3053 Projects.org_archive, as top-level trees. This is the default.
3055 \"::* Archived Tasks\"
3056 Archive in the current file, under the top-level headline
3057 \"* Archived Tasks\".
3059 \"~/org/archive.org::\"
3060 Archive in file ~/org/archive.org (absolute path), as top-level trees.
3062 \"~/org/archive.org::From %s\"
3063 Archive in file ~/org/archive.org (absolute path), und headlines
3064 \"From FILENAME\" where file name is the current file name.
3066 \"basement::** Finished Tasks\"
3067 Archive in file ./basement (relative path), as level 3 trees
3068 below the level 2 heading \"** Finished Tasks\".
3070 You may set this option on a per-file basis by adding to the buffer a
3073 #+ARCHIVE: basement::** Finished Tasks
3075 You may also define it locally for a subtree by setting an ARCHIVE property
3076 in the entry. If such a property is found in an entry, or anywhere up
3077 the hierarchy, it will be used."
3081 (defcustom org-archive-tag
"ARCHIVE"
3082 "The tag that marks a subtree as archived.
3083 An archived subtree does not open during visibility cycling, and does
3084 not contribute to the agenda listings.
3085 After changing this, font-lock must be restarted in the relevant buffers to
3086 get the proper fontification."
3088 :group
'org-keywords
3091 (defcustom org-agenda-skip-archived-trees t
3092 "Non-nil means, the agenda will skip any items located in archived trees.
3093 An archived tree is a tree marked with the tag ARCHIVE. The use of this
3094 variable is no longer recommended, you should leave it at the value t.
3095 Instead, use the key `v' to cycle the archives-mode in the agenda."
3097 :group
'org-agenda-skip
3100 (defcustom org-columns-skip-arrchived-trees t
3101 "Non-nil means, irgnore archived trees when creating column view."
3103 :group
'org-properties
3106 (defcustom org-cycle-open-archived-trees nil
3107 "Non-nil means, `org-cycle' will open archived trees.
3108 An archived tree is a tree marked with the tag ARCHIVE.
3109 When nil, archived trees will stay folded. You can still open them with
3110 normal outline commands like `show-all', but not with the cycling commands."
3115 (defcustom org-sparse-tree-open-archived-trees nil
3116 "Non-nil means sparse tree construction shows matches in archived trees.
3117 When nil, matches in these trees are highlighted, but the trees are kept in
3120 :group
'org-sparse-trees
3123 (defun org-cycle-hide-archived-subtrees (state)
3124 "Re-hide all archived subtrees after a visibility state change."
3125 (when (and (not org-cycle-open-archived-trees
)
3126 (not (memq state
'(overview folded
))))
3128 (let* ((globalp (memq state
'(contents all
)))
3129 (beg (if globalp
(point-min) (point)))
3130 (end (if globalp
(point-max) (org-end-of-subtree t
))))
3131 (org-hide-archived-subtrees beg end
)
3133 (if (looking-at (concat ".*:" org-archive-tag
":"))
3134 (message "%s" (substitute-command-keys
3135 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
3137 (defun org-force-cycle-archived ()
3138 "Cycle subtree even if it is archived."
3140 (setq this-command
'org-cycle
)
3141 (let ((org-cycle-open-archived-trees t
))
3142 (call-interactively 'org-cycle
)))
3144 (defun org-hide-archived-subtrees (beg end
)
3145 "Re-hide all archived subtrees after a visibility state change."
3147 (let* ((re (concat ":" org-archive-tag
":")))
3149 (while (re-search-forward re end t
)
3150 (and (org-on-heading-p) (hide-subtree))
3151 (org-end-of-subtree t
)))))
3153 (defalias 'org-advertized-archive-subtree
'org-archive-subtree
)
3156 (org-autoload "org-archive"
3157 '(org-add-archive-files org-archive-subtree
3158 org-archive-to-archive-sibling org-toggle-archive-tag
)))
3160 ;; Autoload Column View Code
3162 (declare-function org-columns-number-to-string
"org-colview")
3163 (declare-function org-columns-get-format-and-top-level
"org-colview")
3164 (declare-function org-columns-compute
"org-colview")
3166 (org-autoload (if (featurep 'xemacs
) "org-colview-xemacs" "org-colview")
3167 '(org-columns-number-to-string org-columns-get-format-and-top-level
3168 org-columns-compute org-agenda-columns org-columns-remove-overlays
3169 org-columns org-insert-columns-dblock org-dblock-write
:columnview
))
3173 (declare-function org-id-store-link
"org-id")
3174 (declare-function org-id-locations-load
"org-id")
3175 (declare-function org-id-locations-save
"org-id")
3176 (defvar org-id-track-globally
)
3177 (org-autoload "org-id"
3178 '(org-id-get-create org-id-new org-id-copy org-id-get
3179 org-id-get-with-outline-path-completion
3180 org-id-get-with-outline-drilling
3181 org-id-goto org-id-find org-id-store-link
))
3183 ;; Autoload Plotting Code
3185 (org-autoload "org-plot"
3186 '(org-plot/gnuplot
))
3188 ;;; Variables for pre-computed regular expressions, all buffer local
3190 (defvar org-drawer-regexp nil
3191 "Matches first line of a hidden block.")
3192 (make-variable-buffer-local 'org-drawer-regexp
)
3193 (defvar org-todo-regexp nil
3194 "Matches any of the TODO state keywords.")
3195 (make-variable-buffer-local 'org-todo-regexp
)
3196 (defvar org-not-done-regexp nil
3197 "Matches any of the TODO state keywords except the last one.")
3198 (make-variable-buffer-local 'org-not-done-regexp
)
3199 (defvar org-todo-line-regexp nil
3200 "Matches a headline and puts TODO state into group 2 if present.")
3201 (make-variable-buffer-local 'org-todo-line-regexp
)
3202 (defvar org-complex-heading-regexp nil
3203 "Matches a headline and puts everything into groups:
3205 group 2: The todo keyword, maybe
3206 group 3: Priority cookie
3207 group 4: True headline
3209 (make-variable-buffer-local 'org-complex-heading-regexp
)
3210 (defvar org-todo-line-tags-regexp nil
3211 "Matches a headline and puts TODO state into group 2 if present.
3212 Also put tags into group 4 if tags are present.")
3213 (make-variable-buffer-local 'org-todo-line-tags-regexp
)
3214 (defvar org-nl-done-regexp nil
3215 "Matches newline followed by a headline with the DONE keyword.")
3216 (make-variable-buffer-local 'org-nl-done-regexp
)
3217 (defvar org-looking-at-done-regexp nil
3218 "Matches the DONE keyword a point.")
3219 (make-variable-buffer-local 'org-looking-at-done-regexp
)
3220 (defvar org-ds-keyword-length
12
3221 "Maximum length of the Deadline and SCHEDULED keywords.")
3222 (make-variable-buffer-local 'org-ds-keyword-length
)
3223 (defvar org-deadline-regexp nil
3224 "Matches the DEADLINE keyword.")
3225 (make-variable-buffer-local 'org-deadline-regexp
)
3226 (defvar org-deadline-time-regexp nil
3227 "Matches the DEADLINE keyword together with a time stamp.")
3228 (make-variable-buffer-local 'org-deadline-time-regexp
)
3229 (defvar org-deadline-line-regexp nil
3230 "Matches the DEADLINE keyword and the rest of the line.")
3231 (make-variable-buffer-local 'org-deadline-line-regexp
)
3232 (defvar org-scheduled-regexp nil
3233 "Matches the SCHEDULED keyword.")
3234 (make-variable-buffer-local 'org-scheduled-regexp
)
3235 (defvar org-scheduled-time-regexp nil
3236 "Matches the SCHEDULED keyword together with a time stamp.")
3237 (make-variable-buffer-local 'org-scheduled-time-regexp
)
3238 (defvar org-closed-time-regexp nil
3239 "Matches the CLOSED keyword together with a time stamp.")
3240 (make-variable-buffer-local 'org-closed-time-regexp
)
3242 (defvar org-keyword-time-regexp nil
3243 "Matches any of the 4 keywords, together with the time stamp.")
3244 (make-variable-buffer-local 'org-keyword-time-regexp
)
3245 (defvar org-keyword-time-not-clock-regexp nil
3246 "Matches any of the 3 keywords, together with the time stamp.")
3247 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp
)
3248 (defvar org-maybe-keyword-time-regexp nil
3249 "Matches a timestamp, possibly preceeded by a keyword.")
3250 (make-variable-buffer-local 'org-maybe-keyword-time-regexp
)
3251 (defvar org-planning-or-clock-line-re nil
3252 "Matches a line with planning or clock info.")
3253 (make-variable-buffer-local 'org-planning-or-clock-line-re
)
3255 (defconst org-plain-time-of-day-regexp
3258 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
3261 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
3263 "Regular expression to match a plain time or time range.
3264 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
3265 groups carry important information:
3267 1 the first time, range or not
3268 8 the second time, if it is a range.")
3270 (defconst org-plain-time-extension-regexp
3273 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
3274 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
3275 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
3276 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
3277 groups carry important information:
3280 9 minutes of duration")
3282 (defconst org-stamp-time-of-day-regexp
3284 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
3285 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
3287 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
3288 "Regular expression to match a timestamp time or time range.
3289 After a match, the following groups carry important information:
3291 1 date plus weekday, for backreferencing to make sure both times on same day
3292 2 the first time, range or not
3293 4 the second time, if it is a range.")
3295 (defconst org-startup-options
3296 '(("fold" org-startup-folded t
)
3297 ("overview" org-startup-folded t
)
3298 ("nofold" org-startup-folded nil
)
3299 ("showall" org-startup-folded nil
)
3300 ("content" org-startup-folded content
)
3301 ("hidestars" org-hide-leading-stars t
)
3302 ("showstars" org-hide-leading-stars nil
)
3303 ("odd" org-odd-levels-only t
)
3304 ("oddeven" org-odd-levels-only nil
)
3305 ("align" org-startup-align-all-tables t
)
3306 ("noalign" org-startup-align-all-tables nil
)
3307 ("customtime" org-display-custom-times t
)
3308 ("logdone" org-log-done time
)
3309 ("lognotedone" org-log-done note
)
3310 ("nologdone" org-log-done nil
)
3311 ("lognoteclock-out" org-log-note-clock-out t
)
3312 ("nolognoteclock-out" org-log-note-clock-out nil
)
3313 ("logrepeat" org-log-repeat state
)
3314 ("lognoterepeat" org-log-repeat note
)
3315 ("nologrepeat" org-log-repeat nil
)
3316 ("fninline" org-footnote-define-inline t
)
3317 ("nofninline" org-footnote-define-inline nil
)
3318 ("fnlocal" org-footnote-section nil
)
3319 ("fnauto" org-footnote-auto-label t
)
3320 ("fnprompt" org-footnote-auto-label nil
)
3321 ("fnconfirm" org-footnote-auto-label confirm
)
3322 ("fnplain" org-footnote-auto-label plain
)
3323 ("constcgs" constants-unit-system cgs
)
3324 ("constSI" constants-unit-system SI
)
3325 ("noptag" org-tag-persistent-alist nil
))
3326 "Variable associated with STARTUP options for org-mode.
3327 Each element is a list of three items: The startup options as written
3328 in the #+STARTUP line, the corresponding variable, and the value to
3329 set this variable to if the option is found. An optional forth element PUSH
3330 means to push this value onto the list in the variable.")
3332 (defun org-set-regexps-and-options ()
3333 "Precompute regular expressions for current buffer."
3335 (org-set-local 'org-todo-kwd-alist nil
)
3336 (org-set-local 'org-todo-key-alist nil
)
3337 (org-set-local 'org-todo-key-trigger nil
)
3338 (org-set-local 'org-todo-keywords-1 nil
)
3339 (org-set-local 'org-done-keywords nil
)
3340 (org-set-local 'org-todo-heads nil
)
3341 (org-set-local 'org-todo-sets nil
)
3342 (org-set-local 'org-todo-log-states nil
)
3343 (org-set-local 'org-file-properties nil
)
3344 (org-set-local 'org-file-tags nil
)
3345 (let ((re (org-make-options-regexp
3346 '("CATEGORY" "TODO" "COLUMNS"
3347 "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
3348 "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
3349 "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
3351 kwds kws0 kwsa key log value cat arch tags const links hw dws
3352 tail sep kws1 prio props ftags drawers
3353 ext-setup-or-nil setup-contents
(start 0))
3357 (goto-char (point-min))
3358 (while (or (and ext-setup-or-nil
3359 (string-match re ext-setup-or-nil start
)
3360 (setq start
(match-end 0)))
3361 (and (setq ext-setup-or-nil nil start
0)
3362 (re-search-forward re nil t
)))
3363 (setq key
(upcase (match-string 1 ext-setup-or-nil
))
3364 value
(org-match-string-no-properties 2 ext-setup-or-nil
))
3366 ((equal key
"CATEGORY")
3367 (if (string-match "[ \t]+$" value
)
3368 (setq value
(replace-match "" t t value
)))
3370 ((member key
'("SEQ_TODO" "TODO"))
3371 (push (cons 'sequence
(org-split-string value splitre
)) kwds
))
3372 ((equal key
"TYP_TODO")
3373 (push (cons 'type
(org-split-string value splitre
)) kwds
))
3374 ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key
)
3375 ;; general TODO-like setup
3376 (push (cons (intern (downcase (match-string 1 key
)))
3377 (org-split-string value splitre
)) kwds
))
3379 (setq tags
(append tags
(if tags
'("\\n") nil
)
3380 (org-split-string value splitre
))))
3381 ((equal key
"COLUMNS")
3382 (org-set-local 'org-columns-default-format value
))
3384 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value
)
3385 (push (cons (match-string 1 value
)
3386 (org-trim (match-string 2 value
)))
3388 ((equal key
"PRIORITIES")
3389 (setq prio
(org-split-string value
" +")))
3390 ((equal key
"PROPERTY")
3391 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
3392 (push (cons (match-string 1 value
) (match-string 2 value
))
3394 ((equal key
"FILETAGS")
3395 (when (string-match "\\S-" value
)
3400 (mapcar (lambda (x) (org-split-string x
":"))
3401 (org-split-string value
)))))))
3402 ((equal key
"DRAWERS")
3403 (setq drawers
(org-split-string value splitre
)))
3404 ((equal key
"CONSTANTS")
3405 (setq const
(append const
(org-split-string value splitre
))))
3406 ((equal key
"STARTUP")
3407 (let ((opts (org-split-string value splitre
))
3409 (while (setq l
(pop opts
))
3410 (when (setq l
(assoc l org-startup-options
))
3411 (setq var
(nth 1 l
) val
(nth 2 l
))
3413 (set (make-local-variable var
) val
)
3414 (if (not (listp (symbol-value var
)))
3415 (set (make-local-variable var
) nil
))
3416 (set (make-local-variable var
) (symbol-value var
))
3417 (add-to-list var val
))))))
3418 ((equal key
"ARCHIVE")
3419 (string-match " *$" value
)
3420 (setq arch
(replace-match "" t t value
))
3421 (remove-text-properties 0 (length arch
)
3422 '(face t fontified t
) arch
))
3423 ((equal key
"SETUPFILE")
3424 (setq setup-contents
(org-file-contents
3426 (org-remove-double-quotes value
))
3428 (if (not ext-setup-or-nil
)
3429 (setq ext-setup-or-nil setup-contents start
0)
3430 (setq ext-setup-or-nil
3431 (concat (substring ext-setup-or-nil
0 start
)
3432 "\n" setup-contents
"\n"
3433 (substring ext-setup-or-nil start
)))))
3436 (org-set-local 'org-category
(intern cat
))
3437 (push (cons "CATEGORY" cat
) props
))
3439 (if (< (length prio
) 3) (setq prio
'("A" "C" "B")))
3440 (setq prio
(mapcar 'string-to-char prio
))
3441 (org-set-local 'org-highest-priority
(nth 0 prio
))
3442 (org-set-local 'org-lowest-priority
(nth 1 prio
))
3443 (org-set-local 'org-default-priority
(nth 2 prio
)))
3444 (and props
(org-set-local 'org-file-properties
(nreverse props
)))
3445 (and ftags
(org-set-local 'org-file-tags
3446 (mapcar 'org-add-prop-inherited ftags
)))
3447 (and drawers
(org-set-local 'org-drawers drawers
))
3448 (and arch
(org-set-local 'org-archive-location arch
))
3449 (and links
(setq org-link-abbrev-alist-local
(nreverse links
)))
3450 ;; Process the TODO keywords
3452 ;; Use the global values as if they had been given locally.
3453 (setq kwds
(default-value 'org-todo-keywords
))
3454 (if (stringp (car kwds
))
3455 (setq kwds
(list (cons org-todo-interpretation
3456 (default-value 'org-todo-keywords
)))))
3457 (setq kwds
(reverse kwds
)))
3458 (setq kwds
(nreverse kwds
))
3460 (while (setq kws
(pop kwds
))
3462 (run-hook-with-args-until-success
3463 'org-todo-setup-filter-hook kws
)
3465 (setq inter
(pop kws
) sep
(member "|" kws
)
3466 kws0
(delete "|" (copy-sequence kws
))
3471 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x
)
3473 (setq kw
(match-string 1 x
)
3474 key
(and (match-end 2) (match-string 2 x
))
3475 log
(org-extract-log-state-settings x
))
3476 (push (cons kw
(and key
(string-to-char key
))) kwsa
)
3477 (and log
(push log org-todo-log-states
))
3479 (error "Invalid TODO keyword %s" x
)))
3481 kwsa
(if kwsa
(append '((:startgroup
))
3485 dws
(if sep
(org-remove-keyword-keys (cdr sep
)) (last kws1
))
3486 tail
(list inter hw
(car dws
) (org-last dws
))))
3487 (add-to-list 'org-todo-heads hw
'append
)
3488 (push kws1 org-todo-sets
)
3489 (setq org-done-keywords
(append org-done-keywords dws nil
))
3490 (setq org-todo-key-alist
(append org-todo-key-alist kwsa
))
3491 (mapc (lambda (x) (push (cons x tail
) org-todo-kwd-alist
)) kws1
)
3492 (setq org-todo-keywords-1
(append org-todo-keywords-1 kws1 nil
)))
3493 (setq org-todo-sets
(nreverse org-todo-sets
)
3494 org-todo-kwd-alist
(nreverse org-todo-kwd-alist
)
3495 org-todo-key-trigger
(delq nil
(mapcar 'cdr org-todo-key-alist
))
3496 org-todo-key-alist
(org-assign-fast-keys org-todo-key-alist
)))
3497 ;; Process the constants
3500 (while (setq e
(pop const
))
3501 (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e
)
3502 (push (cons (match-string 1 e
) (match-string 2 e
)) cst
)))
3503 (setq org-table-formula-constants-local cst
)))
3505 ;; Process the tags.
3508 (while (setq e
(pop tags
))
3510 ((equal e
"{") (push '(:startgroup
) tgs
))
3511 ((equal e
"}") (push '(:endgroup
) tgs
))
3512 ((equal e
"\\n") (push '(:newline
) tgs
))
3513 ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e
)
3514 (push (cons (match-string 1 e
)
3515 (string-to-char (match-string 2 e
)))
3517 (t (push (list e
) tgs
))))
3518 (org-set-local 'org-tag-alist nil
)
3519 (while (setq e
(pop tgs
))
3520 (or (and (stringp (car e
))
3521 (assoc (car e
) org-tag-alist
))
3522 (push e org-tag-alist
)))))
3524 ;; Compute the regular expressions and other local variables
3525 (if (not org-done-keywords
)
3526 (setq org-done-keywords
(list (org-last org-todo-keywords-1
))))
3527 (setq org-ds-keyword-length
(+ 2 (max (length org-deadline-string
)
3528 (length org-scheduled-string
)
3529 (length org-clock-string
)
3530 (length org-closed-string
)))
3532 (concat "^[ \t]*:\\("
3533 (mapconcat 'regexp-quote org-drawers
"\\|")
3535 org-not-done-keywords
3536 (org-delete-all org-done-keywords
(copy-sequence org-todo-keywords-1
))
3538 (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
3542 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
3544 org-todo-line-regexp
3545 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3546 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
3547 "\\)\\>\\)?[ \t]*\\(.*\\)")
3548 org-complex-heading-regexp
3549 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3550 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
3551 "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
3552 "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
3554 (concat "\n\\*+[ \t]+"
3555 "\\(?:" (mapconcat 'regexp-quote org-done-keywords
"\\|")
3557 org-todo-line-tags-regexp
3558 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3559 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
3561 "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
3562 org-looking-at-done-regexp
3564 (mapconcat 'regexp-quote org-done-keywords
"\\|") "\\)"
3566 org-deadline-regexp
(concat "\\<" org-deadline-string
)
3567 org-deadline-time-regexp
3568 (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>")
3569 org-deadline-line-regexp
3570 (concat "\\<\\(" org-deadline-string
"\\).*")
3571 org-scheduled-regexp
3572 (concat "\\<" org-scheduled-string
)
3573 org-scheduled-time-regexp
3574 (concat "\\<" org-scheduled-string
" *<\\([^>]+\\)>")
3575 org-closed-time-regexp
3576 (concat "\\<" org-closed-string
" *\\[\\([^]]+\\)\\]")
3577 org-keyword-time-regexp
3578 (concat "\\<\\(" org-scheduled-string
3579 "\\|" org-deadline-string
3580 "\\|" org-closed-string
3581 "\\|" org-clock-string
"\\)"
3582 " *[[<]\\([^]>]+\\)[]>]")
3583 org-keyword-time-not-clock-regexp
3584 (concat "\\<\\(" org-scheduled-string
3585 "\\|" org-deadline-string
3586 "\\|" org-closed-string
3588 " *[[<]\\([^]>]+\\)[]>]")
3589 org-maybe-keyword-time-regexp
3590 (concat "\\(\\<\\(" org-scheduled-string
3591 "\\|" org-deadline-string
3592 "\\|" org-closed-string
3593 "\\|" org-clock-string
"\\)\\)?"
3594 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
3595 org-planning-or-clock-line-re
3596 (concat "\\(?:^[ \t]*\\(" org-scheduled-string
3597 "\\|" org-deadline-string
3598 "\\|" org-closed-string
"\\|" org-clock-string
3601 (org-compute-latex-and-specials-regexp)
3602 (org-set-font-lock-defaults))))
3604 (defun org-file-contents (file &optional noerror
)
3605 "Return the contents of FILE, as a string."
3607 (not (file-readable-p file
)))
3610 (message "Cannot read file %s" file
)
3613 (error "Cannot read file %s" file
))
3615 (insert-file-contents file
)
3618 (defun org-extract-log-state-settings (x)
3619 "Extract the log state setting from a TODO keyword string.
3620 This will extract info from a string like \"WAIT(w@/!)\"."
3621 (let (kw key log1 log2
)
3622 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x
)
3623 (setq kw
(match-string 1 x
)
3624 key
(and (match-end 2) (match-string 2 x
))
3625 log1
(and (match-end 3) (match-string 3 x
))
3626 log2
(and (match-end 4) (match-string 4 x
)))
3629 (and log1
(if (equal log1
"!") 'time
'note
))
3630 (and log2
(if (equal log2
"!") 'time
'note
)))))))
3632 (defun org-remove-keyword-keys (list)
3633 "Remove a pair of parenthesis at the end of each string in LIST."
3635 (if (string-match "(.*)$" x
)
3636 (substring x
0 (match-beginning 0))
3640 ;; FIXME: this could be done much better, using second characters etc.
3641 (defun org-assign-fast-keys (alist)
3642 "Assign fast keys to a keyword-key alist.
3643 Respect keys that are already there."
3644 (let (new e k c c1 c2
(char ?a
))
3645 (while (setq e
(pop alist
))
3647 ((equal e
'(:startgroup
)) (push e new
))
3648 ((equal e
'(:endgroup
)) (push e new
))
3649 ((equal e
'(:newline
)) (push e new
))
3651 (setq k
(car e
) c2 nil
)
3654 ;; automatically assign a character.
3655 (setq c1
(string-to-char
3656 (downcase (substring
3657 k
(if (= (string-to-char k
) ?
@) 1 0)))))
3658 (if (or (rassoc c1 new
) (rassoc c1 alist
))
3659 (while (or (rassoc char new
) (rassoc char alist
))
3660 (setq char
(1+ char
)))
3662 (setq c
(or c2 char
)))
3663 (push (cons k c
) new
))))
3666 ;;; Some variables used in various places
3668 (defvar org-window-configuration nil
3669 "Used in various places to store a window configuration.")
3670 (defvar org-finish-function nil
3671 "Function to be called when `C-c C-c' is used.
3672 This is for getting out of special buffers like remember.")
3675 ;; FIXME: Occasionally check by commenting these, to make sure
3676 ;; no other functions uses these, forgetting to let-bind them.
3681 (defvar description
)
3683 ;; Defined somewhere in this file, but used before definition.
3684 (defvar org-html-entities
)
3685 (defvar org-struct-menu
)
3686 (defvar org-org-menu
)
3687 (defvar org-tbl-menu
)
3688 (defvar org-agenda-keymap
)
3690 ;;;; Define the Org-mode
3692 (if (and (not (keymapp outline-mode-map
)) (featurep 'allout
))
3693 (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."))
3696 ;; We use a before-change function to check if a table might need
3698 (defvar org-table-may-need-update t
3699 "Indicates that a table might need an update.
3700 This variable is set by `org-before-change-function'.
3701 `org-table-align' sets it back to nil.")
3702 (defun org-before-change-function (beg end
)
3703 "Every change indicates that a table might need an update."
3704 (setq org-table-may-need-update t
))
3705 (defvar org-mode-map
)
3706 (defvar org-mode-hook nil
3707 "Mode hook for Org-mode, run after the mode was turned on.")
3708 (defvar org-inhibit-startup nil
) ; Dynamically-scoped param.
3709 (defvar org-agenda-keep-modes nil
) ; Dynamically-scoped param.
3710 (defvar org-table-buffer-is-an nil
)
3711 (defconst org-outline-regexp
"\\*+ ")
3714 (define-derived-mode org-mode outline-mode
"Org"
3715 "Outline-based notes management and organizer, alias
3716 \"Carsten's outline-mode for keeping track of everything.\"
3718 Org-mode develops organizational tasks around a NOTES file which
3719 contains information about projects as plain text. Org-mode is
3720 implemented on top of outline-mode, which is ideal to keep the content
3721 of large files well structured. It supports ToDo items, deadlines and
3722 time stamps, which magically appear in the diary listing of the Emacs
3723 calendar. Tables are easily created with a built-in table editor.
3724 Plain text URL-like links connect to websites, emails (VM), Usenet
3725 messages (Gnus), BBDB entries, and any files related to the project.
3726 For printing and sharing of notes, an Org-mode file (or a part of it)
3727 can be exported as a structured ASCII or HTML file.
3729 The following commands are available:
3733 ;; Get rid of Outline menus, they are not needed
3734 ;; Need to do this here because define-derived-mode sets up
3735 ;; the keymap so late. Still, it is a waste to call this each time
3736 ;; we switch another buffer into org-mode.
3737 (if (featurep 'xemacs
)
3738 (when (boundp 'outline-mode-menu-heading
)
3739 ;; Assume this is Greg's port, it used easymenu
3740 (easy-menu-remove outline-mode-menu-heading
)
3741 (easy-menu-remove outline-mode-menu-show
)
3742 (easy-menu-remove outline-mode-menu-hide
))
3743 (define-key org-mode-map
[menu-bar headings
] 'undefined
)
3744 (define-key org-mode-map
[menu-bar hide
] 'undefined
)
3745 (define-key org-mode-map
[menu-bar show
] 'undefined
))
3747 (org-load-modules-maybe)
3748 (easy-menu-add org-org-menu
)
3749 (easy-menu-add org-tbl-menu
)
3750 (org-install-agenda-files-menu)
3751 (if org-descriptive-links
(org-add-to-invisibility-spec '(org-link)))
3752 (org-add-to-invisibility-spec '(org-cwidth))
3753 (when (featurep 'xemacs
)
3754 (org-set-local 'line-move-ignore-invisible t
))
3755 (org-set-local 'outline-regexp org-outline-regexp
)
3756 (org-set-local 'outline-level
'org-outline-level
)
3757 (when (and org-ellipsis
3758 (fboundp 'set-display-table-slot
) (boundp 'buffer-display-table
)
3759 (fboundp 'make-glyph-code
))
3760 (unless org-display-table
3761 (setq org-display-table
(make-display-table)))
3762 (set-display-table-slot
3765 (lambda (c) (make-glyph-code c
(and (not (stringp org-ellipsis
))
3767 (if (stringp org-ellipsis
) org-ellipsis
"..."))))
3768 (setq buffer-display-table org-display-table
))
3769 (org-set-regexps-and-options)
3770 (when (and org-tag-faces
(not org-tags-special-faces-re
))
3771 ;; tag faces set outside customize.... force initialization.
3772 (org-set-tag-faces 'org-tag-faces org-tag-faces
))
3774 (org-set-local 'calc-embedded-open-mode
"# ")
3775 (modify-syntax-entry ?
# "<")
3776 (modify-syntax-entry ?
@ "w")
3777 (if org-startup-truncated
(setq truncate-lines t
))
3778 (org-set-local 'font-lock-unfontify-region-function
3779 'org-unfontify-region
)
3780 ;; Activate before-change-function
3781 (org-set-local 'org-table-may-need-update t
)
3782 (org-add-hook 'before-change-functions
'org-before-change-function nil
3784 ;; Check for running clock before killing a buffer
3785 (org-add-hook 'kill-buffer-hook
'org-check-running-clock nil
'local
)
3786 ;; Paragraphs and auto-filling
3787 (org-set-autofill-regexps)
3788 (setq indent-line-function
'org-indent-line-function
)
3789 (org-update-radio-target-regexp)
3790 ;; Make sure dependence stuff works reliably, even for users who set it
3792 (if org-enforce-todo-dependencies
3793 (add-hook 'org-blocker-hook
3794 'org-block-todo-from-children-or-siblings
)
3795 (remove-hook 'org-blocker-hook
3796 'org-block-todo-from-children-or-siblings
))
3797 (if org-enforce-todo-checkbox-dependencies
3798 (add-hook 'org-blocker-hook
3799 'org-block-todo-from-checkboxes
)
3800 (remove-hook 'org-blocker-hook
3801 'org-block-todo-from-checkboxes
))
3803 ;; Comment characters
3804 ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
3805 (org-set-local 'comment-padding
" ")
3807 ;; Align options lines
3809 'align-mode-rules-list
3810 '((org-in-buffer-settings
3811 (regexp .
"^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
3812 (modes .
'(org-mode)))))
3815 (org-set-local 'imenu-create-index-function
3816 'org-imenu-get-tree
)
3818 ;; Make isearch reveal context
3819 (if (or (featurep 'xemacs
)
3820 (not (boundp 'outline-isearch-open-invisible-function
)))
3821 ;; Emacs 21 and XEmacs make use of the hook
3822 (org-add-hook 'isearch-mode-end-hook
'org-isearch-end
'append
'local
)
3823 ;; Emacs 22 deals with this through a special variable
3824 (org-set-local 'outline-isearch-open-invisible-function
3825 (lambda (&rest ignore
) (org-show-context 'isearch
))))
3827 ;; If empty file that did not turn on org-mode automatically, make it to.
3828 (if (and org-insert-mode-line-in-empty-file
3830 (= (point-min) (point-max)))
3831 (insert "# -*- mode: org -*-\n\n"))
3833 (unless org-inhibit-startup
3834 (when org-startup-align-all-tables
3835 (let ((bmp (buffer-modified-p)))
3836 (org-table-map-tables 'org-table-align
)
3837 (set-buffer-modified-p bmp
)))
3838 (org-set-startup-visibility)))
3840 (put 'org-mode
'flyspell-mode-predicate
'org-mode-flyspell-verify
)
3842 (defun org-current-time ()
3843 "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
3844 (if (> (car org-time-stamp-rounding-minutes
) 1)
3845 (let ((r (car org-time-stamp-rounding-minutes
))
3846 (time (decode-time)))
3848 (append (list 0 (* r
(floor (+ .5 (/ (float (nth 1 time
)) r
)))))
3852 ;;;; Font-Lock stuff, including the activators
3854 (defvar org-mouse-map
(make-sparse-keymap))
3855 (org-defkey org-mouse-map
3856 (if (featurep 'xemacs
) [button2] [mouse-2]) 'org-open-at-mouse)
3857 (org-defkey org-mouse-map
3858 (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
3859 (when org-mouse-1-follows-link
3860 (org-defkey org-mouse-map [follow-link] 'mouse-face))
3861 (when org-tab-follows-link
3862 (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
3863 (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
3865 (require 'font-lock)
3867 (defconst org-non-link-chars "]\t\n\r<>")
3868 (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
3870 (defvar org-link-types-re nil
3871 "Matches a link that has a url-like prefix like \"http:\"")
3872 (defvar org-link-re-with-space nil
3873 "Matches a link with spaces, optional angular brackets around it.")
3874 (defvar org-link-re-with-space2 nil
3875 "Matches a link with spaces, optional angular brackets around it.")
3876 (defvar org-link-re-with-space3 nil
3877 "Matches a link with spaces, only for internal part in bracket links.")
3878 (defvar org-angle-link-re nil
3879 "Matches link with angular brackets, spaces are allowed.")
3880 (defvar org-plain-link-re nil
3881 "Matches plain link, without spaces.")
3882 (defvar org-bracket-link-regexp nil
3883 "Matches a link in double brackets.")
3884 (defvar org-bracket-link-analytic-regexp nil
3885 "Regular expression used to analyze links.
3886 Here is what the match groups contain after a match:
3892 (defvar org-bracket-link-analytic-regexp++ nil
3893 "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
3894 (defvar org-any-link-re nil
3895 "Regular expression matching any link.")
3897 (defun org-make-link-regexps ()
3898 "Update the link regular expressions.
3899 This should be called after the variable `org-link-types' has changed."
3900 (setq org-link-types-re
3902 "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
3903 org-link-re-with-space
3905 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3906 "\\([^" org-non-link-chars " ]"
3907 "[^" org-non-link-chars "]*"
3908 "[^" org-non-link-chars " ]\\)>?")
3909 org-link-re-with-space2
3911 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3912 "\\([^" org-non-link-chars " ]"
3914 "[^" org-non-link-chars " ]\\)>?")
3915 org-link-re-with-space3
3917 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3918 "\\([^" org-non-link-chars " ]"
3922 "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3923 "\\([^" org-non-link-chars " ]"
3924 "[^" org-non-link-chars "]*"
3928 "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3929 "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
3930 org-bracket-link-regexp
3931 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
3932 org-bracket-link-analytic-regexp
3935 "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
3938 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
3940 org-bracket-link-analytic-regexp++
3943 "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
3946 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
3949 (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
3950 org-angle-link-re "\\)\\|\\("
3951 org-plain-link-re "\\)")))
3953 (org-make-link-regexps)
3955 (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
3956 "Regular expression for fast time stamp matching.")
3957 (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
3958 "Regular expression for fast time stamp matching.")
3959 (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
3960 "Regular expression matching time strings for analysis.
3961 This one does not require the space after the date, so it can be used
3962 on a string that terminates immediately after the date.")
3963 (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
3964 "Regular expression matching time strings for analysis.")
3965 (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
3966 "Regular expression matching time stamps, with groups.")
3967 (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
3968 "Regular expression matching time stamps (also [..]), with groups.")
3969 (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
3970 "Regular expression matching a time stamp range.")
3971 (defconst org-tr-regexp-both
3972 (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
3973 "Regular expression matching a time stamp range.")
3974 (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
3975 org-ts-regexp "\\)?")
3976 "Regular expression matching a time stamp or time stamp range.")
3977 (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
3978 org-ts-regexp-both "\\)?")
3979 "Regular expression matching a time stamp or time stamp range.
3980 The time stamps may be either active or inactive.")
3982 (defvar org-emph-face nil)
3984 (defun org-do-emphasis-faces (limit)
3985 "Run through the buffer and add overlays to links."
3987 (while (and (not rtn) (re-search-forward org-emph-re limit t))
3988 (if (not (= (char-after (match-beginning 3))
3989 (char-after (match-beginning 4))))
3992 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
3994 (nth 1 (assoc (match-string 3)
3995 org-emphasis-alist)))
3996 (add-text-properties (match-beginning 2) (match-end 2)
3997 '(font-lock-multiline t))
3998 (when org-hide-emphasis-markers
3999 (add-text-properties (match-end 4) (match-beginning 5)
4000 '(invisible org-link))
4001 (add-text-properties (match-beginning 3) (match-end 3)
4002 '(invisible org-link)))))
4006 (defun org-emphasize (&optional char)
4007 "Insert or change an emphasis, i.e. a font like bold or italic.
4008 If there is an active region, change that region to a new emphasis.
4009 If there is no region, just insert the marker characters and position
4010 the cursor between them.
4011 CHAR should be either the marker character, or the first character of the
4012 HTML tag associated with that emphasis. If CHAR is a space, the means
4013 to remove the emphasis of the selected region.
4014 If char is not given (for example in an interactive call) it
4015 will be prompted for."
4017 (let ((eal org-emphasis-alist) e det
4018 (erc org-emphasis-regexp-components)
4020 (string "") beg end move tag c s)
4021 (if (org-region-active-p)
4022 (setq beg (region-beginning) end (region-end)
4023 string (buffer-substring beg end))
4026 (while (setq e (pop eal))
4027 (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
4029 (push (cons c (string-to-char (car e))) det)
4030 (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
4031 (substring tag 1)))))
4032 (setq det (nreverse det))
4034 (message "%s" (concat "Emphasis marker or tag:" prompt))
4035 (setq char (read-char-exclusive)))
4036 (setq char (or (cdr (assoc char det)) char))
4037 (if (equal char ?\ )
4038 (setq s "" move nil)
4039 (unless (assoc (char-to-string char) org-emphasis-alist)
4040 (error "No such emphasis marker: \"%c\"" char))
4041 (setq s (char-to-string char)))
4042 (while (and (> (length string) 1)
4043 (equal (substring string 0 1) (substring string -1))
4044 (assoc (substring string 0 1) org-emphasis-alist))
4045 (setq string (substring string 1 -1)))
4046 (setq string (concat s string s))
4047 (if beg (delete-region beg end))
4049 (string-match (concat "[" (nth 0 erc) "\n]")
4050 (char-to-string (char-before (point)))))
4052 (unless (string-match (concat "[" (nth 1 erc) "\n]")
4053 (char-to-string (char-after (point))))
4054 (insert " ") (backward-char 1))
4056 (and move (backward-char 1))))
4058 (defconst org-nonsticky-props
4059 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
4062 (defun org-activate-plain-links (limit)
4063 "Run through the buffer and add overlays to links."
4066 (while (re-search-forward org-plain-link-re limit t)
4067 (setq f (get-text-property (match-beginning 0) 'face))
4068 (if (or (eq f 'org-tag)
4069 (and (listp f) (memq 'org-tag f)))
4071 (add-text-properties (match-beginning 0) (match-end 0)
4072 (list 'mouse-face 'highlight
4073 'rear-nonsticky org-nonsticky-props
4074 'keymap org-mouse-map
4076 (throw 'exit t))))))
4078 (defun org-activate-code (limit)
4079 (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
4081 (remove-text-properties (match-beginning 0) (match-end 0)
4082 '(display t invisible t intangible t))
4085 (defun org-activate-angle-links (limit)
4086 "Run through the buffer and add overlays to links."
4087 (if (re-search-forward org-angle-link-re limit t)
4089 (add-text-properties (match-beginning 0) (match-end 0)
4090 (list 'mouse-face 'highlight
4091 'rear-nonsticky org-nonsticky-props
4092 'keymap org-mouse-map
4096 (defun org-activate-footnote-links (limit)
4097 "Run through the buffer and add overlays to links."
4098 (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
4101 (add-text-properties (match-beginning 2) (match-end 2)
4102 (list 'mouse-face 'highlight
4103 'rear-nonsticky org-nonsticky-props
4104 'keymap org-mouse-map
4106 (if (= (point-at-bol) (match-beginning 2))
4107 "Footnote definition"
4108 "Footnote reference")
4112 (defun org-activate-bracket-links (limit)
4113 "Run through the buffer and add overlays to bracketed links."
4114 (if (re-search-forward org-bracket-link-regexp limit t)
4115 (let* ((help (concat "LINK: "
4116 (org-match-string-no-properties 1)))
4117 ;; FIXME: above we should remove the escapes.
4118 ;; but that requires another match, protecting match data,
4119 ;; a lot of overhead for font-lock.
4120 (ip (org-maybe-intangible
4121 (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
4122 'keymap org-mouse-map 'mouse-face 'highlight
4123 'font-lock-multiline t 'help-echo help)))
4124 (vp (list 'rear-nonsticky org-nonsticky-props
4125 'keymap org-mouse-map 'mouse-face 'highlight
4126 'font-lock-multiline t 'help-echo help)))
4127 ;; We need to remove the invisible property here. Table narrowing
4128 ;; may have made some of this invisible.
4129 (remove-text-properties (match-beginning 0) (match-end 0)
4133 (add-text-properties (match-beginning 0) (match-beginning 3) ip)
4134 (add-text-properties (match-beginning 3) (match-end 3) vp)
4135 (add-text-properties (match-end 3) (match-end 0) ip))
4136 (add-text-properties (match-beginning 0) (match-beginning 1) ip)
4137 (add-text-properties (match-beginning 1) (match-end 1) vp)
4138 (add-text-properties (match-end 1) (match-end 0) ip))
4141 (defun org-activate-dates (limit)
4142 "Run through the buffer and add overlays to dates."
4143 (if (re-search-forward org-tsr-regexp-both limit t)
4145 (add-text-properties (match-beginning 0) (match-end 0)
4146 (list 'mouse-face 'highlight
4147 'rear-nonsticky org-nonsticky-props
4148 'keymap org-mouse-map))
4149 (when org-display-custom-times
4151 (org-display-custom-time (match-beginning 3) (match-end 3)))
4152 (org-display-custom-time (match-beginning 1) (match-end 1)))
4155 (defvar org-target-link-regexp nil
4156 "Regular expression matching radio targets in plain text.")
4157 (make-variable-buffer-local 'org-target-link-regexp)
4158 (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
4159 "Regular expression matching a link target.")
4160 (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
4161 "Regular expression matching a radio target.")
4162 (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
4163 "Regular expression matching any target.")
4165 (defun org-activate-target-links (limit)
4166 "Run through the buffer and add overlays to target matches."
4167 (when org-target-link-regexp
4168 (let ((case-fold-search t))
4169 (if (re-search-forward org-target-link-regexp limit t)
4171 (add-text-properties (match-beginning 0) (match-end 0)
4172 (list 'mouse-face 'highlight
4173 'rear-nonsticky org-nonsticky-props
4174 'keymap org-mouse-map
4175 'help-echo "Radio target link"
4176 'org-linked-text t))
4179 (defun org-update-radio-target-regexp ()
4180 "Find all radio targets in this file and update the regular expression."
4182 (when (memq 'radio org-activate-links)
4183 (setq org-target-link-regexp
4184 (org-make-target-link-regexp (org-all-targets 'radio)))
4185 (org-restart-font-lock)))
4187 (defun org-hide-wide-columns (limit)
4189 (setq s (text-property-any (point) (or limit (point-max))
4192 (setq e (next-single-property-change s 'org-cwidth))
4193 (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
4197 (defvar org-latex-and-specials-regexp nil
4198 "Regular expression for highlighting export special stuff.")
4199 (defvar org-match-substring-regexp)
4200 (defvar org-match-substring-with-braces-regexp)
4201 (defvar org-export-html-special-string-regexps)
4203 (defun org-compute-latex-and-specials-regexp ()
4204 "Compute regular expression for stuff treated specially by exporters."
4205 (if (not org-highlight-latex-fragments-and-specials)
4206 (org-set-local 'org-latex-and-specials-regexp nil)
4209 ((matchers (plist-get org-format-latex-options :matchers))
4210 (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
4211 org-latex-regexps)))
4212 (options (org-combine-plists (org-default-export-plist)
4213 (org-infile-export-plist)))
4214 (org-export-with-sub-superscripts (plist-get options :sub-superscript))
4215 (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
4216 (org-export-with-TeX-macros (plist-get options :TeX-macros))
4217 (org-export-html-expand (plist-get options :expand-quoted-html))
4218 (org-export-with-special-strings (plist-get options :special-strings))
4221 ((equal org-export-with-sub-superscripts '{})
4222 (list org-match-substring-with-braces-regexp))
4223 (org-export-with-sub-superscripts
4224 (list org-match-substring-regexp))
4227 (if org-export-with-LaTeX-fragments
4228 (mapcar (lambda (x) (nth 1 x)) latexs)))
4230 (if org-export-with-TeX-macros
4231 (list (concat "\\\\"
4233 (append (mapcar 'car org-html-entities)
4234 (if (boundp 'org-latex-entities)
4235 org-latex-entities nil))
4238 ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
4239 (re-special (if org-export-with-special-strings
4240 (mapcar (lambda (x) (car x))
4241 org-export-html-special-string-regexps)))
4245 (if org-export-html-expand "@<[^>\n]+>")
4248 'org-latex-and-specials-regexp
4249 (mapconcat 'identity (append re-latex re-sub re-macros re-special
4252 (defun org-do-latex-and-special-faces (limit)
4253 "Run through the buffer and add overlays to links."
4254 (when org-latex-and-specials-regexp
4256 (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
4258 (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
4260 '(org-code org-verbatim underline)))
4263 d (cond ((member (char-after (1+ (match-beginning 0)))
4266 (font-lock-prepend-text-property
4267 (+ d (match-beginning 0)) (match-end 0)
4268 'face 'org-latex-and-export-specials)
4269 (add-text-properties (+ d (match-beginning 0)) (match-end 0)
4270 '(font-lock-multiline t)))))
4273 (defun org-restart-font-lock ()
4274 "Restart font-lock-mode, to force refontification."
4275 (when (and (boundp 'font-lock-mode) font-lock-mode)
4277 (font-lock-mode 1)))
4279 (defun org-all-targets (&optional radio)
4280 "Return a list of all targets in this file.
4281 With optional argument RADIO, only find radio targets."
4282 (let ((re (if radio org-radio-target-regexp org-target-regexp))
4285 (goto-char (point-min))
4286 (while (re-search-forward re nil t)
4287 (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
4290 (defun org-make-target-link-regexp (targets)
4291 "Make regular expression matching all strings in TARGETS.
4292 The regular expression finds the targets also if there is a line break
4299 (while (string-match " +" x)
4300 (setq x (replace-match "\\s-+" t t x)))
4306 (defun org-activate-tags (limit)
4307 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
4309 (add-text-properties (match-beginning 1) (match-end 1)
4310 (list 'mouse-face 'highlight
4311 'rear-nonsticky org-nonsticky-props
4312 'keymap org-mouse-map))
4315 (defun org-outline-level ()
4317 (looking-at outline-regexp)
4318 (if (match-beginning 1)
4319 (+ (org-get-string-indentation (match-string 1)) 1000)
4320 (1- (- (match-end 0) (match-beginning 0))))))
4322 (defvar org-font-lock-keywords nil)
4324 (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
4325 "Regular expression matching a property line.")
4327 (defvar org-font-lock-hook nil
4328 "Functions to be called for special font lock stuff.")
4330 (defun org-font-lock-hook (limit)
4331 (run-hook-with-args 'org-font-lock-hook limit))
4333 (defun org-set-font-lock-defaults ()
4334 (let* ((em org-fontify-emphasized-text)
4335 (lk org-activate-links)
4336 (org-font-lock-extra-keywords
4339 '(org-font-lock-hook)
4341 '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
4342 (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
4344 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
4347 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
4348 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
4349 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
4351 (list org-drawer-regexp '(0 'org-special-keyword t))
4352 (list "^[ \t]*:END:" '(0 'org-special-keyword t))
4354 (list org-property-re
4355 '(1 'org-special-keyword t)
4356 '(3 'org-property-value t))
4357 (if org-format-transports-properties-p
4358 '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
4360 (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
4361 (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
4362 (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
4363 (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
4364 (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
4365 (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
4366 (if (memq 'footnote lk) '(org-activate-footnote-links
4367 (2 'org-footnote t)))
4368 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
4369 '(org-hide-wide-columns (0 nil append))
4371 (list (concat "^\\*+[ \t]+" org-todo-regexp)
4372 '(1 (org-get-todo-face 1) t))
4374 (if org-fontify-done-headline
4375 (list (concat "^[*]+ +\\<\\("
4376 (mapconcat 'regexp-quote org-done-keywords "\\|")
4378 '(2 'org-headline-done t))
4381 '(org-font-lock-add-priority-faces)
4383 '(org-font-lock-add-tag-faces)
4385 (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
4386 (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
4387 (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
4388 (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
4391 (if (featurep 'xemacs)
4392 '(org-do-emphasis-faces (0 nil append))
4393 '(org-do-emphasis-faces)))
4395 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
4396 2 'org-checkbox prepend)
4397 (if org-provide-checkbox-statistics
4398 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
4399 (0 (org-get-checkbox-statistics-face) t)))
4400 ;; Description list items
4401 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
4403 (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
4404 '(1 'org-archived prepend))
4406 '(org-do-latex-and-special-faces)
4408 '(org-activate-code (1 'org-code t))
4410 (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
4411 "\\|" org-quote-string "\\)\\>")
4412 '(1 'org-special-keyword t))
4413 '("^#.*" (0 'font-lock-comment-face t))
4415 (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
4416 ;; Now set the full font-lock-keywords
4417 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
4418 (org-set-local 'font-lock-defaults
4419 '(org-font-lock-keywords t nil nil backward-paragraph))
4420 (kill-local-variable 'font-lock-keywords) nil))
4425 (defun org-get-level-face (n)
4426 "Get the right face for match N in font-lock matching of headlines."
4427 (setq org-l (- (match-end 2) (match-beginning 1) 1))
4428 (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
4429 (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
4431 ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
4433 (t (if org-level-color-stars-only nil org-f))))
4435 (defun org-get-todo-face (kwd)
4436 "Get the right face for a TODO keyword KWD.
4437 If KWD is a number, get the corresponding match group."
4438 (if (numberp kwd) (setq kwd (match-string kwd)))
4439 (or (cdr (assoc kwd org-todo-keyword-faces))
4440 (and (member kwd org-done-keywords) 'org-done)
4443 (defun org-font-lock-add-tag-faces (limit)
4444 "Add the special tag faces."
4445 (when (and org-tag-faces org-tags-special-faces-re)
4446 (while (re-search-forward org-tags-special-faces-re limit t)
4447 (add-text-properties (match-beginning 1) (match-end 1)
4448 (list 'face (org-get-tag-face 1)
4449 'font-lock-fontified t))
4450 (backward-char 1))))
4452 (defun org-font-lock-add-priority-faces (limit)
4453 "Add the special priority faces."
4454 (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
4455 (add-text-properties
4456 (match-beginning 0) (match-end 0)
4457 (list 'face (or (cdr (assoc (char-after (match-beginning 1))
4458 org-priority-faces))
4459 'org-special-keyword)
4460 'font-lock-fontified t))))
4462 (defun org-get-tag-face (kwd)
4463 "Get the right face for a TODO keyword KWD.
4464 If KWD is a number, get the corresponding match group."
4465 (if (numberp kwd) (setq kwd (match-string kwd)))
4466 (or (cdr (assoc kwd org-tag-faces))
4469 (defun org-unfontify-region (beg end &optional maybe_loudly)
4470 "Remove fontification and activation overlays from links."
4471 (font-lock-default-unfontify-region beg end)
4472 (let* ((buffer-undo-list t)
4473 (inhibit-read-only t) (inhibit-point-motion-hooks t)
4474 (inhibit-modification-hooks t)
4475 deactivate-mark buffer-file-name buffer-file-truename)
4476 (remove-text-properties beg end
4477 '(mouse-face t keymap t org-linked-text t
4478 invisible t intangible t))))
4480 ;;;; Visibility cycling, including org-goto and indirect buffer
4484 (defvar org-cycle-global-status nil)
4485 (make-variable-buffer-local 'org-cycle-global-status)
4486 (defvar org-cycle-subtree-status nil)
4487 (make-variable-buffer-local 'org-cycle-subtree-status)
4491 (defvar org-inlinetask-min-level)
4492 (defun org-cycle (&optional arg)
4493 "Visibility cycling for Org-mode.
4495 - When this function is called with a prefix argument, rotate the entire
4496 buffer through 3 states (global cycling)
4497 1. OVERVIEW: Show only top-level headlines.
4498 2. CONTENTS: Show all headlines of all levels, but no body text.
4499 3. SHOW ALL: Show everything.
4500 When called with two C-u C-u prefixes, switch to the startup visibility,
4501 determined by the variable `org-startup-folded', and by any VISIBILITY
4502 properties in the buffer.
4503 When called with three C-u C-u C-u prefixed, show the entire buffer,
4506 - When point is at the beginning of a headline, rotate the subtree started
4507 by this line through 3 different states (local cycling)
4508 1. FOLDED: Only the main headline is shown.
4509 2. CHILDREN: The main headline and the direct children are shown.
4510 From this state, you can move to one of the children
4511 and zoom in further.
4512 3. SUBTREE: Show the entire subtree, including body text.
4514 - When there is a numeric prefix, go up to a heading with level ARG, do
4515 a `show-subtree' and return to the previous cursor position. If ARG
4516 is negative, go up that many levels.
4518 - When point is not at the beginning of a headline, execute the global
4519 binding for TAB, which is re-indenting the line. See the option
4520 `org-cycle-emulate-tab' for details.
4522 - Special case: if point is at the beginning of the buffer and there is
4523 no headline in line 1, this function will act as if called with prefix arg.
4524 But only if also the variable `org-cycle-global-at-bob' is t."
4526 (org-load-modules-maybe)
4528 (or org-cycle-max-level
4529 (and (boundp 'org-inlinetask-min-level)
4530 org-inlinetask-min-level
4531 (1- org-inlinetask-min-level))))
4532 (nstars (and limit-level
4533 (if org-odd-levels-only
4534 (and limit-level (1- (* limit-level 2)))
4538 ((not (org-mode-p)) outline-regexp)
4539 (org-cycle-include-plain-lists
4541 (if nstars (format "\\{1,%d\\} " nstars) "+")
4542 " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
4543 (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
4544 (bob-special (and org-cycle-global-at-bob (bobp)
4545 (not (looking-at outline-regexp))))
4548 (delq 'org-optimize-window-after-visibility-change
4549 (copy-sequence org-cycle-hook))
4553 (if (or bob-special (equal arg '(4)))
4554 ;; special case: use global cycling
4560 (org-set-startup-visibility)
4561 (message "Startup visibility, plus VISIBILITY properties"))
4565 (message "Entire buffer visible, including drawers"))
4567 ((org-at-table-p 'any)
4568 ;; Enter the table or move to the next field in the table
4569 (or (org-table-recognize-table.el)
4571 (if arg (org-table-edit-field t)
4572 (org-table-justify-field-maybe)
4573 (call-interactively 'org-table-next-field)))))
4575 ((eq arg t) ;; Global cycling
4578 ((and (eq last-command this-command)
4579 (eq org-cycle-global-status 'overview))
4580 ;; We just created the overview - now do table of contents
4581 ;; This can be slow in very large buffers, so indicate action
4582 (message "CONTENTS...")
4584 (message "CONTENTS...done")
4585 (setq org-cycle-global-status 'contents)
4586 (run-hook-with-args 'org-cycle-hook 'contents))
4588 ((and (eq last-command this-command)
4589 (eq org-cycle-global-status 'contents))
4590 ;; We just showed the table of contents - now show everything
4592 (message "SHOW ALL")
4593 (setq org-cycle-global-status 'all)
4594 (run-hook-with-args 'org-cycle-hook 'all))
4597 ;; Default action: go to overview
4599 (message "OVERVIEW")
4600 (setq org-cycle-global-status 'overview)
4601 (run-hook-with-args 'org-cycle-hook 'overview))))
4603 ((and org-drawers org-drawer-regexp
4605 (beginning-of-line 1)
4606 (looking-at org-drawer-regexp)))
4607 ;; Toggle block visibility
4609 (not (get-char-property (match-end 0) 'invisible))))
4612 ;; Show-subtree, ARG levels up from here.
4614 (org-back-to-heading)
4615 (outline-up-heading (if (< arg 0) (- arg)
4616 (- (funcall outline-level) arg)))
4617 (org-show-subtree)))
4619 ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
4620 (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
4621 ;; At a heading: rotate between three different views
4622 (org-back-to-heading)
4623 (let ((goal-column 0) eoh eol eos)
4624 ;; First, some boundaries
4626 (org-back-to-heading)
4628 (beginning-of-line 2)
4629 (while (and (not (eobp)) ;; this is like `next-line'
4630 (get-char-property (1- (point)) 'invisible))
4631 (beginning-of-line 2)) (setq eol (point)))
4632 (outline-end-of-heading) (setq eoh (point))
4633 (org-end-of-subtree t)
4635 (skip-chars-forward " \t\n")
4636 (beginning-of-line 1) ; in case this is an item
4638 (setq eos (1- (point))))
4639 ;; Find out what to do next and set `this-command'
4642 ;; Nothing is hidden behind this heading
4643 (message "EMPTY ENTRY")
4644 (setq org-cycle-subtree-status nil)
4647 (outline-next-heading)
4648 (if (org-invisible-p) (org-flag-heading nil))))
4650 (not (string-match "\\S-" (buffer-substring eol eos))))
4651 ;; Entire subtree is hidden in one line: open it
4654 (message "CHILDREN")
4657 (outline-next-heading)
4658 (if (org-invisible-p) (org-flag-heading nil)))
4659 (setq org-cycle-subtree-status 'children)
4660 (run-hook-with-args 'org-cycle-hook 'children))
4661 ((and (eq last-command this-command)
4662 (eq org-cycle-subtree-status 'children))
4663 ;; We just showed the children, now show everything.
4666 (setq org-cycle-subtree-status 'subtree)
4667 (run-hook-with-args 'org-cycle-hook 'subtree))
4669 ;; Default action: hide the subtree.
4672 (setq org-cycle-subtree-status 'folded)
4673 (run-hook-with-args 'org-cycle-hook 'folded)))))
4675 ;; TAB emulation and template completion
4676 (buffer-read-only (org-back-to-heading))
4678 ((org-try-structure-completion))
4680 ((org-try-cdlatex-tab))
4682 ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
4684 (not (looking-at outline-regexp))))
4685 (call-interactively (global-key-binding "\t")))
4687 ((if (and (memq org-cycle-emulate-tab '(white whitestart))
4688 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
4689 (or (and (eq org-cycle-emulate-tab 'white)
4690 (= (match-end 0) (point-at-eol)))
4691 (and (eq org-cycle-emulate-tab 'whitestart)
4692 (>= (match-end 0) pos))))
4694 (eq org-cycle-emulate-tab t))
4695 (call-interactively (global-key-binding "\t")))
4698 (org-back-to-heading)
4702 (defun org-global-cycle (&optional arg)
4703 "Cycle the global visibility. For details see `org-cycle'.
4704 With C-u prefix arg, switch to startup visibility.
4705 With a numeric prefix, show all headlines up to that level."
4707 (let ((org-cycle-include-plain-lists
4708 (if (org-mode-p) org-cycle-include-plain-lists nil)))
4712 (hide-sublevels arg)
4713 (setq org-cycle-global-status 'contents))
4715 (org-set-startup-visibility)
4716 (message "Startup visibility, plus VISIBILITY properties."))
4718 (org-cycle '(4))))))
4720 (defun org-set-startup-visibility ()
4721 "Set the visibility required by startup options and properties."
4723 ((eq org-startup-folded t)
4725 ((eq org-startup-folded 'content)
4726 (let ((this-command 'org-cycle) (last-command 'org-cycle))
4727 (org-cycle '(4)) (org-cycle '(4)))))
4728 (org-set-visibility-according-to-property 'no-cleanup)
4729 (org-cycle-hide-archived-subtrees 'all)
4730 (org-cycle-hide-drawers 'all)
4731 (org-cycle-show-empty-lines 'all))
4733 (defun org-set-visibility-according-to-property (&optional no-cleanup)
4734 "Switch subtree visibilities according to :VISIBILITY: property."
4736 (let (org-show-entry-below state)
4738 (goto-char (point-min))
4739 (while (re-search-forward
4740 "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
4742 (setq state (match-string 1))
4744 (org-back-to-heading t)
4748 ((equal state '("fold" "folded"))
4750 ((equal state "children")
4751 (org-show-hidden-entry)
4753 ((equal state "content")
4756 (org-narrow-to-subtree)
4758 ((member state '("all" "showall"))
4761 (org-cycle-hide-archived-subtrees 'all)
4762 (org-cycle-hide-drawers 'all)
4763 (org-cycle-show-empty-lines 'all)))))
4765 (defun org-overview ()
4766 "Switch to overview mode, showing only top-level headlines.
4767 Really, this shows all headlines with level equal or greater than the level
4768 of the first headline in the buffer. This is important, because if the
4769 first headline is not level one, then (hide-sublevels 1) gives confusing
4772 (let ((level (save-excursion
4773 (goto-char (point-min))
4774 (if (re-search-forward (concat "^" outline-regexp) nil t)
4776 (goto-char (match-beginning 0))
4777 (funcall outline-level))))))
4778 (and level (hide-sublevels level))))
4780 (defun org-content (&optional arg)
4781 "Show all headlines in the buffer, like a table of contents.
4782 With numerical argument N, show content up to level N."
4785 ;; Visit all headings and show their offspring
4786 (and (integerp arg) (org-overview))
4787 (goto-char (point-max))
4789 (while (and (progn (condition-case nil
4790 (outline-previous-visible-heading 1)
4791 (error (goto-char (point-min))))
4793 (looking-at outline-regexp))
4795 (show-children (1- arg))
4797 (if (bobp) (throw 'exit nil))))))
4800 (defun org-optimize-window-after-visibility-change (state)
4801 "Adjust the window after a change in outline visibility.
4802 This function is the default value of the hook `org-cycle-hook'."
4803 (when (get-buffer-window (current-buffer))
4805 ((eq state 'content) nil)
4806 ((eq state 'all) nil)
4807 ((eq state 'folded) nil)
4808 ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
4809 ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
4811 (defun org-compact-display-after-subtree-move ()
4812 "Show a compacter version of the tree of the entry's parent."
4814 (if (org-up-heading-safe)
4819 (org-cycle-show-empty-lines 'children)
4820 (org-cycle-hide-drawers 'children))
4823 (defun org-cycle-show-empty-lines (state)
4824 "Show empty lines above all visible headlines.
4825 The region to be covered depends on STATE when called through
4826 `org-cycle-hook'. Lisp program can use t for STATE to get the
4827 entire buffer covered. Note that an empty line is only shown if there
4828 are at least `org-cycle-separator-lines' empty lines before the headline."
4829 (when (> org-cycle-separator-lines 0)
4831 (let* ((n org-cycle-separator-lines)
4833 ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
4834 ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
4835 (t (let ((ns (number-to-string (- n 2))))
4836 (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
4837 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
4840 ((memq state '(overview contents t))
4841 (setq beg (point-min) end (point-max)))
4842 ((memq state '(children folded))
4843 (setq beg (point) end (progn (org-end-of-subtree t t)
4844 (beginning-of-line 2)
4848 (while (re-search-forward re end t)
4849 (if (not (get-char-property (match-end 1) 'invisible))
4850 (outline-flag-region
4851 (match-beginning 1) (match-end 1) nil)))))))
4852 ;; Never hide empty lines at the end of the file.
4854 (goto-char (point-max))
4855 (outline-previous-heading)
4856 (outline-end-of-heading)
4857 (if (and (looking-at "[ \t\n]+")
4858 (= (match-end 0) (point-max)))
4859 (outline-flag-region (point) (match-end 0) nil))))
4861 (defun org-show-empty-lines-in-parent ()
4862 "Move to the parent and re-show empty lines before visible headlines."
4864 (let ((context (if (org-up-heading-safe) 'children 'overview)))
4865 (org-cycle-show-empty-lines context))))
4867 (defun org-cycle-hide-drawers (state)
4868 "Re-hide all drawers after a visibility state change."
4869 (when (and (org-mode-p)
4870 (not (memq state '(overview folded))))
4872 (let* ((globalp (memq state '(contents all)))
4873 (beg (if globalp (point-min) (point)))
4874 (end (if globalp (point-max) (org-end-of-subtree t))))
4876 (while (re-search-forward org-drawer-regexp end t)
4877 (org-flag-drawer t))))))
4879 (defun org-flag-drawer (flag)
4881 (beginning-of-line 1)
4882 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
4883 (let ((b (match-end 0))
4884 (outline-regexp org-outline-regexp))
4885 (if (re-search-forward
4887 (save-excursion (outline-next-heading) (point)) t)
4888 (outline-flag-region b (point-at-eol) flag)
4889 (error ":END: line missing"))))))
4891 (defun org-subtree-end-visible-p ()
4892 "Is the end of the current subtree visible?"
4893 (pos-visible-in-window-p
4894 (save-excursion (org-end-of-subtree t) (point))))
4896 (defun org-first-headline-recenter (&optional N)
4897 "Move cursor to the first headline and recenter the headline.
4898 Optional argument N means, put the headline into the Nth line of the window."
4899 (goto-char (point-min))
4900 (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
4902 (recenter (prefix-numeric-value N))))
4906 (defvar org-goto-window-configuration nil)
4907 (defvar org-goto-marker nil)
4908 (defvar org-goto-map
4909 (let ((map (make-sparse-keymap)))
4910 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
4911 (while (setq cmd (pop cmds))
4912 (substitute-key-definition cmd cmd map global-map)))
4913 (suppress-keymap map)
4914 (org-defkey map "\C-m" 'org-goto-ret)
4915 (org-defkey map [(return)] 'org-goto-ret)
4916 (org-defkey map [(left)] 'org-goto-left)
4917 (org-defkey map [(right)] 'org-goto-right)
4918 (org-defkey map [(control ?g)] 'org-goto-quit)
4919 (org-defkey map "\C-i" 'org-cycle)
4920 (org-defkey map [(tab)] 'org-cycle)
4921 (org-defkey map [(down)] 'outline-next-visible-heading)
4922 (org-defkey map [(up)] 'outline-previous-visible-heading)
4923 (if org-goto-auto-isearch
4924 (if (fboundp 'define-key-after)
4925 (define-key-after map [t] 'org-goto-local-auto-isearch)
4927 (org-defkey map "q" 'org-goto-quit)
4928 (org-defkey map "n" 'outline-next-visible-heading)
4929 (org-defkey map "p" 'outline-previous-visible-heading)
4930 (org-defkey map "f" 'outline-forward-same-level)
4931 (org-defkey map "b" 'outline-backward-same-level)
4932 (org-defkey map "u" 'outline-up-heading))
4933 (org-defkey map "/" 'org-occur)
4934 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
4935 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
4936 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
4937 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
4938 (org-defkey map "\C-c\C-u" 'outline-up-heading)
4941 (defconst org-goto-help
4942 "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
4943 RET=jump to location [Q]uit and return to previous location
4944 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
4946 (defvar org-goto-start-pos) ; dynamically scoped parameter
4948 ;; FIXME: Docstring doe not mention both interfaces
4949 (defun org-goto (&optional alternative-interface)
4950 "Look up a different location in the current file, keeping current visibility.
4952 When you want look-up or go to a different location in a document, the
4953 fastest way is often to fold the entire buffer and then dive into the tree.
4954 This method has the disadvantage, that the previous location will be folded,
4955 which may not be what you want.
4957 This command works around this by showing a copy of the current buffer
4958 in an indirect buffer, in overview mode. You can dive into the tree in
4959 that copy, use org-occur and incremental search to find a location.
4960 When pressing RET or `Q', the command returns to the original buffer in
4961 which the visibility is still unchanged. After RET is will also jump to
4962 the location selected in the indirect buffer and expose the
4963 the headline hierarchy above."
4965 (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
4966 (org-refile-use-outline-path t)
4968 (if (not alternative-interface)
4970 (if (eq org-goto-interface 'outline)
4971 'outline-path-completion
4973 (org-goto-start-pos (point))
4975 (if (eq interface 'outline)
4976 (car (org-get-location (current-buffer) org-goto-help))
4977 (nth 3 (org-refile-get-location "Goto: ")))))
4980 (org-mark-ring-push org-goto-start-pos)
4981 (goto-char selected-point)
4982 (if (or (org-invisible-p) (org-invisible-p2))
4983 (org-show-context 'org-goto)))
4986 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
4987 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
4988 (defvar org-goto-local-auto-isearch-map) ; defined below
4990 (defun org-get-location (buf help)
4991 "Let the user select a location in the Org-mode buffer BUF.
4992 This function uses a recursive edit. It returns the selected position
4994 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
4995 (isearch-hide-immediately nil)
4996 (isearch-search-fun-function
4997 (lambda () 'org-goto-local-search-headings))
4998 (org-goto-selected-point org-goto-exit-command))
5000 (save-window-excursion
5001 (delete-other-windows)
5002 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
5005 (make-indirect-buffer (current-buffer) "*org-goto*")
5006 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
5007 (with-output-to-temp-buffer "*Help*"
5009 (org-fit-window-to-buffer (get-buffer-window "*Help*"))
5010 (setq buffer-read-only nil)
5011 (let ((org-startup-truncated t)
5012 (org-startup-folded nil)
5013 (org-startup-align-all-tables nil))
5016 (setq buffer-read-only t)
5017 (if (and (boundp 'org-goto-start-pos)
5018 (integer-or-marker-p org-goto-start-pos))
5019 (let ((org-show-hierarchy-above t)
5020 (org-show-siblings t)
5021 (org-show-following-heading t))
5022 (goto-char org-goto-start-pos)
5023 (and (org-invisible-p) (org-show-context)))
5024 (goto-char (point-min)))
5025 (let (org-special-ctrl-a/e) (org-beginning-of-line))
5026 (message "Select location and press RET")
5027 (use-local-map org-goto-map)
5030 (kill-buffer "*org-goto*")
5031 (cons org-goto-selected-point org-goto-exit-command)))
5033 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
5034 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
5035 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
5036 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
5038 (defun org-goto-local-search-headings (string bound noerror)
5039 "Search and make sure that any matches are in headlines."
5041 (while (if isearch-forward
5042 (search-forward string bound noerror)
5043 (search-backward string bound noerror))
5044 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
5045 (and (member :headline context)
5046 (not (member :tags context))))
5047 (throw 'return (point))))))
5049 (defun org-goto-local-auto-isearch ()
5052 (goto-char (point-min))
5053 (let ((keys (this-command-keys)))
5054 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
5056 (isearch-process-search-char (string-to-char keys)))))
5058 (defun org-goto-ret (&optional arg)
5059 "Finish `org-goto' by going to the new location."
5061 (setq org-goto-selected-point (point)
5062 org-goto-exit-command 'return)
5065 (defun org-goto-left ()
5066 "Finish `org-goto' by going to the new location."
5068 (if (org-on-heading-p)
5070 (beginning-of-line 1)
5071 (setq org-goto-selected-point (point)
5072 org-goto-exit-command 'left)
5074 (error "Not on a heading")))
5076 (defun org-goto-right ()
5077 "Finish `org-goto' by going to the new location."
5079 (if (org-on-heading-p)
5081 (setq org-goto-selected-point (point)
5082 org-goto-exit-command 'right)
5084 (error "Not on a heading")))
5086 (defun org-goto-quit ()
5087 "Finish `org-goto' without cursor motion."
5089 (setq org-goto-selected-point nil)
5090 (setq org-goto-exit-command 'quit)
5093 ;;; Indirect buffer display of subtrees
5095 (defvar org-indirect-dedicated-frame nil
5096 "This is the frame being used for indirect tree display.")
5097 (defvar org-last-indirect-buffer nil)
5099 (defun org-tree-to-indirect-buffer (&optional arg)
5100 "Create indirect buffer and narrow it to current subtree.
5101 With numerical prefix ARG, go up to this level and then take that tree.
5102 If ARG is negative, go up that many levels.
5103 If `org-indirect-buffer-display' is not `new-frame', the command removes the
5104 indirect buffer previously made with this command, to avoid proliferation of
5105 indirect buffers. However, when you call the command with a `C-u' prefix, or
5106 when `org-indirect-buffer-display' is `new-frame', the last buffer
5107 is kept so that you can work with several indirect buffers at the same time.
5108 If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
5109 requests that a new frame be made for the new buffer, so that the dedicated
5110 frame is not changed."
5112 (let ((cbuf (current-buffer))
5113 (cwin (selected-window))
5115 beg end level heading ibuf)
5117 (org-back-to-heading t)
5119 (setq level (org-outline-level))
5120 (if (< arg 0) (setq arg (+ level arg)))
5121 (while (> (setq level (org-outline-level)) arg)
5122 (outline-up-heading 1 t)))
5124 heading (org-get-heading))
5125 (org-end-of-subtree t) (setq end (point)))
5126 (if (and (buffer-live-p org-last-indirect-buffer)
5127 (not (eq org-indirect-buffer-display 'new-frame))
5129 (kill-buffer org-last-indirect-buffer))
5130 (setq ibuf (org-get-indirect-buffer cbuf)
5131 org-last-indirect-buffer ibuf)
5133 ((or (eq org-indirect-buffer-display 'new-frame)
5134 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
5135 (select-frame (make-frame))
5136 (delete-other-windows)
5137 (switch-to-buffer ibuf)
5138 (org-set-frame-title heading))
5139 ((eq org-indirect-buffer-display 'dedicated-frame)
5141 (select-frame (or (and org-indirect-dedicated-frame
5142 (frame-live-p org-indirect-dedicated-frame)
5143 org-indirect-dedicated-frame)
5144 (setq org-indirect-dedicated-frame (make-frame)))))
5145 (delete-other-windows)
5146 (switch-to-buffer ibuf)
5147 (org-set-frame-title (concat "Indirect: " heading)))
5148 ((eq org-indirect-buffer-display 'current-window)
5149 (switch-to-buffer ibuf))
5150 ((eq org-indirect-buffer-display 'other-window)
5151 (pop-to-buffer ibuf))
5152 (t (error "Invalid value.")))
5153 (if (featurep 'xemacs)
5154 (save-excursion (org-mode) (turn-on-font-lock)))
5155 (narrow-to-region beg end)
5158 (and (window-live-p cwin) (select-window cwin))))
5160 (defun org-get-indirect-buffer (&optional buffer)
5161 (setq buffer (or buffer (current-buffer)))
5162 (let ((n 1) (base (buffer-name buffer)) bname)
5163 (while (buffer-live-p
5164 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
5167 (make-indirect-buffer buffer bname 'clone)
5168 (error (make-indirect-buffer buffer bname)))))
5170 (defun org-set-frame-title (title)
5171 "Set the title of the current frame to the string TITLE."
5172 ;; FIXME: how to name a single frame in XEmacs???
5173 (unless (featurep 'xemacs)
5174 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
5176 ;;;; Structure editing
5178 ;;; Inserting headlines
5180 (defun org-previous-line-empty-p ()
5183 (or (beginning-of-line 0) t)
5185 (looking-at "[ \t]*$")))))
5187 (defun org-insert-heading (&optional force-heading)
5188 "Insert a new heading or item with same depth at point.
5189 If point is in a plain list and FORCE-HEADING is nil, create a new list item.
5190 If point is at the beginning of a headline, insert a sibling before the
5191 current headline. If point is not at the beginning, do not split the line,
5192 but create the new headline after the current line."
5194 (if (= (buffer-size) 0)
5196 (when (or force-heading (not (org-insert-item)))
5197 (let* ((empty-line-p nil)
5198 (head (save-excursion
5201 (org-back-to-heading)
5202 (setq empty-line-p (org-previous-line-empty-p))
5205 (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
5206 (blank (if (eq blank-a 'auto) empty-line-p blank-a))
5207 pos hide-previous previous-pos)
5209 ((and (org-on-heading-p) (bolp)
5211 (save-excursion (backward-char 1) (not (org-invisible-p)))))
5212 ;; insert before the current line
5213 (open-line (if blank 2 1)))
5217 (backward-char 1) (not (org-invisible-p)))))
5218 ;; insert right here
5221 ;; somewhere in the line
5223 (setq previous-pos (point-at-bol))
5225 (setq hide-previous (org-invisible-p)))
5226 (and org-insert-heading-respect-content (org-show-subtree))
5228 (and (org-get-alist-option org-M-RET-may-split-line 'headline)
5231 (goto-char (point-at-bol))
5232 (and (looking-at org-complex-heading-regexp)
5233 (> p (match-beginning 4)))))))
5236 (org-insert-heading-respect-content
5237 (org-end-of-subtree nil t)
5238 (or (bolp) (newline))
5239 (or (org-previous-line-empty-p)
5240 (and blank (newline)))
5246 (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
5247 (setq tags (and (match-end 2) (match-string 2)))
5249 (delete-region (match-beginning 1) (match-end 1)))
5250 (setq pos (point-at-bol))
5251 (or split (end-of-line 1))
5252 (delete-horizontal-space)
5253 (newline (if blank 2 1))
5259 (org-set-tags nil 'align))))
5261 (or split (end-of-line 1))
5262 (newline (if blank 2 1)))))))
5263 (insert head) (just-one-space)
5266 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
5267 (when (and org-insert-heading-respect-content hide-previous)
5269 (goto-char previous-pos)
5271 (run-hooks 'org-insert-heading-hook)))))
5273 (defun org-get-heading (&optional no-tags)
5274 "Return the heading of the current entry, without the stars."
5276 (org-back-to-heading t)
5279 (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
5280 "\\*+[ \t]+\\([^\r\n]*\\)"))
5281 (match-string 1) "")))
5283 (defun org-heading-components ()
5284 "Return the components of the current heading.
5285 This is a list with the following elements:
5286 - the level as an integer
5287 - the reduced level, different if `org-odd-levels-only' is set.
5288 - the TODO keyword, or nil
5289 - the priority character, like ?A, or nil if no priority is given
5290 - the headline text itself, or the tags string if no headline text
5291 - the tags string, or nil."
5293 (org-back-to-heading t)
5294 (if (looking-at org-complex-heading-regexp)
5295 (list (length (match-string 1))
5296 (org-reduced-level (length (match-string 1)))
5297 (org-match-string-no-properties 2)
5298 (and (match-end 3) (aref (match-string 3) 2))
5299 (org-match-string-no-properties 4)
5300 (org-match-string-no-properties 5)))))
5302 (defun org-insert-heading-after-current ()
5303 "Insert a new heading with same level as current, after current subtree."
5305 (org-back-to-heading)
5306 (org-insert-heading)
5307 (org-move-subtree-down)
5310 (defun org-insert-heading-respect-content ()
5312 (let ((org-insert-heading-respect-content t))
5313 (org-insert-heading t)))
5315 (defun org-insert-todo-heading-respect-content (&optional force-state)
5317 (let ((org-insert-heading-respect-content t))
5318 (org-insert-todo-heading force-state t)))
5320 (defun org-insert-todo-heading (arg &optional force-heading)
5321 "Insert a new heading with the same level and TODO state as current heading.
5322 If the heading has no TODO state, or if the state is DONE, use the first
5323 state (TODO by default). Also with prefix arg, force first state."
5325 (when (or force-heading (not (org-insert-item 'checkbox)))
5326 (org-insert-heading force-heading)
5328 (org-back-to-heading)
5329 (outline-previous-heading)
5330 (looking-at org-todo-line-regexp))
5334 (not (match-beginning 2))
5335 (member (match-string 2) org-done-keywords))
5336 (car org-todo-keywords-1)
5340 (run-hook-with-args-until-success
5341 'org-todo-get-default-hook new-mark-x nil)
5343 (beginning-of-line 1)
5344 (and (looking-at "\\*+ ") (goto-char (match-end 0))
5345 (insert new-mark " ")))
5346 (when org-provide-todo-statistics
5347 (org-update-parent-todo-statistics))))
5349 (defun org-insert-subheading (arg)
5350 "Insert a new subheading and demote it.
5351 Works for outline headings and for plain lists alike."
5353 (org-insert-heading arg)
5355 ((org-on-heading-p) (org-do-demote))
5356 ((org-at-item-p) (org-indent-item 1))))
5358 (defun org-insert-todo-subheading (arg)
5359 "Insert a new subheading with TODO keyword or checkbox and demote it.
5360 Works for outline headings and for plain lists alike."
5362 (org-insert-todo-heading arg)
5364 ((org-on-heading-p) (org-do-demote))
5365 ((org-at-item-p) (org-indent-item 1))))
5367 ;;; Promotion and Demotion
5369 (defun org-promote-subtree ()
5370 "Promote the entire subtree.
5371 See also `org-promote'."
5374 (org-map-tree 'org-promote))
5375 (org-fix-position-after-promote))
5377 (defun org-demote-subtree ()
5378 "Demote the entire subtree. See `org-demote'.
5379 See also `org-promote'."
5382 (org-map-tree 'org-demote))
5383 (org-fix-position-after-promote))
5386 (defun org-do-promote ()
5387 "Promote the current heading higher up the tree.
5388 If the region is active in `transient-mark-mode', promote all headings
5392 (if (org-region-active-p)
5393 (org-map-region 'org-promote (region-beginning) (region-end))
5395 (org-fix-position-after-promote))
5397 (defun org-do-demote ()
5398 "Demote the current heading lower down the tree.
5399 If the region is active in `transient-mark-mode', demote all headings
5403 (if (org-region-active-p)
5404 (org-map-region 'org-demote (region-beginning) (region-end))
5406 (org-fix-position-after-promote))
5408 (defun org-fix-position-after-promote ()
5409 "Make sure that after pro/demotion cursor position is right."
5410 (let ((pos (point)))
5411 (when (save-excursion
5412 (beginning-of-line 1)
5413 (looking-at org-todo-line-regexp)
5414 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
5415 (cond ((eobp) (insert " "))
5416 ((eolp) (insert " "))
5417 ((equal (char-after) ?\ ) (forward-char 1))))))
5419 (defun org-reduced-level (l)
5420 "Compute the effective level of a heading.
5421 This takes into account the setting of `org-odd-levels-only'."
5422 (if org-odd-levels-only (1+ (floor (/ l 2))) l))
5424 (defun org-get-valid-level (level &optional change)
5425 "Rectify a level change under the influence of `org-odd-levels-only'
5426 LEVEL is a current level, CHANGE is by how much the level should be
5427 modified. Even if CHANGE is nil, LEVEL may be returned modified because
5428 even level numbers will become the next higher odd number."
5429 (if org-odd-levels-only
5430 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
5431 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
5432 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
5433 (max 1 (+ level (or change 0)))))
5435 (if (boundp 'define-obsolete-function-alias)
5436 (if (or (featurep 'xemacs) (< emacs-major-version 23))
5437 (define-obsolete-function-alias 'org-get-legal-level
5438 'org-get-valid-level)
5439 (define-obsolete-function-alias 'org-get-legal-level
5440 'org-get-valid-level "23.1")))
5442 (defun org-promote ()
5443 "Promote the current heading higher up the tree.
5444 If the region is active in `transient-mark-mode', promote all headings
5446 (org-back-to-heading t)
5447 (let* ((level (save-match-data (funcall outline-level)))
5448 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
5449 (diff (abs (- level (length up-head) -1))))
5450 (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
5451 (replace-match up-head nil t)
5452 ;; Fixup tag positioning
5453 (and org-auto-align-tags (org-set-tags nil t))
5454 (if org-adapt-indentation (org-fixup-indentation (- diff)))))
5456 (defun org-demote ()
5457 "Demote the current heading lower down the tree.
5458 If the region is active in `transient-mark-mode', demote all headings
5460 (org-back-to-heading t)
5461 (let* ((level (save-match-data (funcall outline-level)))
5462 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
5463 (diff (abs (- level (length down-head) -1))))
5464 (replace-match down-head nil t)
5465 ;; Fixup tag positioning
5466 (and org-auto-align-tags (org-set-tags nil t))
5467 (if org-adapt-indentation (org-fixup-indentation diff))))
5469 (defun org-map-tree (fun)
5470 "Call FUN for every heading underneath the current one."
5471 (org-back-to-heading)
5472 (let ((level (funcall outline-level)))
5476 (outline-next-heading)
5477 (> (funcall outline-level) level))
5481 (defun org-map-region (fun beg end)
5482 "Call FUN for every heading between BEG and END."
5483 (let ((org-ignore-region t))
5485 (setq end (copy-marker end))
5487 (if (and (re-search-forward (concat "^" outline-regexp) nil t)
5491 (outline-next-heading)
5496 (defun org-fixup-indentation (diff)
5497 "Change the indentation in the current entry by DIFF
5498 However, if any line in the current entry has no indentation, or if it
5499 would end up with no indentation after the change, nothing at all is done."
5501 (let ((end (save-excursion (outline-next-heading)
5503 (prohibit (if (> diff 0)
5505 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
5507 (unless (save-excursion (end-of-line 1)
5508 (re-search-forward prohibit end t))
5509 (while (and (< (point) end)
5510 (re-search-forward "^[ \t]+" end t))
5511 (goto-char (match-end 0))
5512 (setq col (current-column))
5513 (if (< diff 0) (replace-match ""))
5514 (org-indent-to-column (+ diff col))))
5515 (move-marker end nil))))
5517 (defun org-convert-to-odd-levels ()
5518 "Convert an org-mode file with all levels allowed to one with odd levels.
5519 This will leave level 1 alone, convert level 2 to level 3, level 3 to
5522 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
5523 (let ((org-odd-levels-only nil) n)
5525 (goto-char (point-min))
5526 (while (re-search-forward "^\\*\\*+ " nil t)
5527 (setq n (- (length (match-string 0)) 2))
5528 (while (>= (setq n (1- n)) 0)
5530 (end-of-line 1))))))
5533 (defun org-convert-to-oddeven-levels ()
5534 "Convert an org-mode file with only odd levels to one with odd and even levels.
5535 This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
5536 section with an even level, conversion would destroy the structure of the file. An error
5537 is signaled in this case."
5539 (goto-char (point-min))
5540 ;; First check if there are no even levels
5541 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
5542 (org-show-context t)
5543 (error "Not all levels are odd in this file. Conversion not possible."))
5544 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
5545 (let ((org-odd-levels-only nil) n)
5547 (goto-char (point-min))
5548 (while (re-search-forward "^\\*\\*+ " nil t)
5549 (setq n (/ (1- (length (match-string 0))) 2))
5550 (while (>= (setq n (1- n)) 0)
5552 (end-of-line 1))))))
5554 (defun org-tr-level (n)
5555 "Make N odd if required."
5556 (if org-odd-levels-only (1+ (/ n 2)) n))
5558 ;;; Vertical tree motion, cutting and pasting of subtrees
5560 (defun org-move-subtree-up (&optional arg)
5561 "Move the current subtree up past ARG headlines of the same level."
5563 (org-move-subtree-down (- (prefix-numeric-value arg))))
5565 (defun org-move-subtree-down (&optional arg)
5566 "Move the current subtree down past ARG headlines of the same level."
5568 (setq arg (prefix-numeric-value arg))
5569 (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
5570 'outline-get-last-sibling))
5571 (ins-point (make-marker))
5573 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
5575 (org-back-to-heading)
5578 (setq ne-beg (org-back-over-empty-lines))
5581 (save-excursion (outline-end-of-heading)
5582 (setq folded (org-invisible-p)))
5583 (outline-end-of-subtree))
5584 (outline-next-heading)
5585 (setq ne-end (org-back-over-empty-lines))
5588 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
5589 ;; include less whitespace
5592 (forward-line (- ne-beg ne-end))
5593 (setq beg (point))))
5594 ;; Find insertion point, with error handling
5596 (or (and (funcall movfunc) (looking-at outline-regexp))
5597 (progn (goto-char beg0)
5598 (error "Cannot move past superior level or buffer limit")))
5599 (setq cnt (1- cnt)))
5601 ;; Moving forward - still need to move over subtree
5602 (progn (org-end-of-subtree t t)
5604 (org-back-over-empty-lines)
5605 (or (bolp) (newline)))))
5606 (setq ne-ins (org-back-over-empty-lines))
5607 (move-marker ins-point (point))
5608 (setq txt (buffer-substring beg end))
5609 (org-save-markers-in-region beg end)
5610 (delete-region beg end)
5611 (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
5612 (or (bobp) (outline-flag-region (1- (point)) (point) nil))
5613 (and (not (bolp)) (looking-at "\n") (forward-char 1))
5614 (let ((bbb (point)))
5615 (insert-before-markers txt)
5616 (org-reinstall-markers-in-region bbb)
5617 (move-marker ins-point bbb))
5618 (or (bolp) (insert "\n"))
5619 (setq ins-end (point))
5620 (goto-char ins-point)
5621 (org-skip-whitespace)
5622 (when (and (< arg 0)
5623 (org-first-sibling-p)
5625 ;; Move whitespace back to beginning
5628 (let ((kill-whole-line t))
5629 (kill-line (- ne-ins ne-beg)) (point)))
5630 (insert (make-string (- ne-ins ne-beg) ?\n)))
5631 (move-marker ins-point nil)
5632 (org-compact-display-after-subtree-move)
5633 (org-show-empty-lines-in-parent)
5637 (org-cycle-hide-drawers 'children))))
5639 (defvar org-subtree-clip ""
5640 "Clipboard for cut and paste of subtrees.
5641 This is actually only a copy of the kill, because we use the normal kill
5642 ring. We need it to check if the kill was created by `org-copy-subtree'.")
5644 (defvar org-subtree-clip-folded nil
5645 "Was the last copied subtree folded?
5646 This is used to fold the tree back after pasting.")
5648 (defun org-cut-subtree (&optional n)
5649 "Cut the current subtree into the clipboard.
5650 With prefix arg N, cut this many sequential subtrees.
5651 This is a short-hand for marking the subtree and then cutting it."
5653 (org-copy-subtree n 'cut))
5655 (defun org-copy-subtree (&optional n cut force-store-markers)
5656 "Cut the current subtree into the clipboard.
5657 With prefix arg N, cut this many sequential subtrees.
5658 This is a short-hand for marking the subtree and then copying it.
5659 If CUT is non-nil, actually cut the subtree.
5660 If FORCE-STORE-MARKERS is non-nil, store the relative locations
5661 of some markers in the region, even if CUT is non-nil. This is
5662 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
5664 (let (beg end folded (beg0 (point)))
5666 (org-back-to-heading nil) ; take what looks like a subtree
5667 (org-back-to-heading t)) ; take what is really there
5668 (org-back-over-empty-lines)
5670 (skip-chars-forward " \t\r\n")
5672 (save-excursion (outline-end-of-heading)
5673 (setq folded (org-invisible-p)))
5675 (outline-forward-same-level (1- n))
5677 (org-end-of-subtree t t))
5678 (org-back-over-empty-lines)
5682 (setq org-subtree-clip-folded folded)
5683 (when (or cut force-store-markers)
5684 (org-save-markers-in-region beg end))
5685 (if cut (kill-region beg end) (copy-region-as-kill beg end))
5686 (setq org-subtree-clip (current-kill 0))
5687 (message "%s: Subtree(s) with %d characters"
5688 (if cut "Cut" "Copied")
5689 (length org-subtree-clip)))))
5691 (defun org-paste-subtree (&optional level tree for-yank)
5692 "Paste the clipboard as a subtree, with modification of headline level.
5693 The entire subtree is promoted or demoted in order to match a new headline
5696 If the cursor is at the beginning of a headline, the same level as
5697 that headline is used to paste the tree
5699 If not, the new level is derived from the *visible* headings
5700 before and after the insertion point, and taken to be the inferior headline
5701 level of the two. So if the previous visible heading is level 3 and the
5702 next is level 4 (or vice versa), level 4 will be used for insertion.
5703 This makes sure that the subtree remains an independent subtree and does
5704 not swallow low level entries.
5706 You can also force a different level, either by using a numeric prefix
5707 argument, or by inserting the heading marker by hand. For example, if the
5708 cursor is after \"*****\", then the tree will be shifted to level 5.
5710 If optional TREE is given, use this text instead of the kill ring.
5712 When FOR-YANK is set, this is called by `org-yank'. In this case, do not
5713 move back over whitespace before inserting, and move point to the end of
5714 the inserted text when done."
5716 (unless (org-kill-is-subtree-p tree)
5718 (substitute-command-keys
5719 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
5720 (let* ((visp (not (org-invisible-p)))
5721 (txt (or tree (and kill-ring (current-kill 0))))
5722 (^re (concat "^\\(" outline-regexp "\\)"))
5723 (re (concat "\\(" outline-regexp "\\)"))
5724 (^re_ (concat "\\(\\*+\\)[ \t]*"))
5726 (old-level (if (string-match ^re txt)
5727 (- (match-end 0) (match-beginning 0) 1)
5729 (force-level (cond (level (prefix-numeric-value level))
5730 ((and (looking-at "[ \t]*$")
5732 ^re_ (buffer-substring
5733 (point-at-bol) (point))))
5734 (- (match-end 1) (match-beginning 1)))
5736 (looking-at org-outline-regexp))
5737 (- (match-end 0) (point) 1))
5739 (previous-level (save-excursion
5742 (outline-previous-visible-heading 1)
5744 (- (match-end 0) (match-beginning 0) 1)
5747 (next-level (save-excursion
5750 (or (looking-at outline-regexp)
5751 (outline-next-visible-heading 1))
5753 (- (match-end 0) (match-beginning 0) 1)
5756 (new-level (or force-level (max previous-level next-level)))
5757 (shift (if (or (= old-level -1)
5759 (= old-level new-level))
5761 (- new-level old-level)))
5762 (delta (if (> shift 0) -1 1))
5763 (func (if (> shift 0) 'org-demote 'org-promote))
5764 (org-odd-levels-only nil)
5766 ;; Remove the forced level indicator
5768 (delete-region (point-at-bol) (point)))
5770 (beginning-of-line 1)
5771 (unless for-yank (org-back-over-empty-lines))
5773 (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
5774 (insert-before-markers txt)
5775 (unless (string-match "\n\\'" txt) (insert "\n"))
5776 (setq newend (point))
5777 (org-reinstall-markers-in-region beg)
5780 (skip-chars-forward " \t\n\r")
5782 (if (and (org-invisible-p) visp)
5783 (save-excursion (outline-show-heading)))
5784 ;; Shift if necessary
5787 (narrow-to-region beg end)
5788 (while (not (= shift 0))
5789 (org-map-region func (point-min) (point-max))
5790 (setq shift (+ delta shift)))
5791 (goto-char (point-min))
5792 (setq newend (point-max))))
5793 (when (or (interactive-p) for-yank)
5794 (message "Clipboard pasted as level %d subtree" new-level))
5795 (if (and (not for-yank) ; in this case, org-yank will decide about folding
5797 (eq org-subtree-clip (current-kill 0))
5798 org-subtree-clip-folded)
5799 ;; The tree was folded before it was killed/copied
5801 (and for-yank (goto-char newend))))
5803 (defun org-kill-is-subtree-p (&optional txt)
5804 "Check if the current kill is an outline subtree, or a set of trees.
5805 Returns nil if kill does not start with a headline, or if the first
5806 headline level is not the largest headline level in the tree.
5807 So this will actually accept several entries of equal levels as well,
5808 which is OK for `org-paste-subtree'.
5809 If optional TXT is given, check this string instead of the current kill."
5810 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
5811 (start-level (and kill
5812 (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
5813 org-outline-regexp "\\)")
5815 (- (match-end 2) (match-beginning 2) 1)))
5816 (re (concat "^" org-outline-regexp))
5817 (start (1+ (or (match-beginning 2) -1))))
5818 (if (not start-level)
5820 nil) ;; does not even start with a heading
5822 (while (setq start (string-match re kill (1+ start)))
5823 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
5827 (defvar org-markers-to-move nil
5828 "Markers that should be moved with a cut-and-paste operation.
5829 Those markers are stored together with their positions relative to
5830 the start of the region.")
5832 (defun org-save-markers-in-region (beg end)
5833 "Check markers in region.
5834 If these markers are between BEG and END, record their position relative
5835 to BEG, so that after moving the block of text, we can put the markers back
5837 This function gets called just before an entry or tree gets cut from the
5838 buffer. After re-insertion, `org-reinstall-markers-in-region' must be
5839 called immediately, to move the markers with the entries."
5840 (setq org-markers-to-move nil)
5841 (when (featurep 'org-clock)
5842 (org-clock-save-markers-for-cut-and-paste beg end))
5843 (when (featurep 'org-agenda)
5844 (org-agenda-save-markers-for-cut-and-paste beg end)))
5846 (defun org-check-and-save-marker (marker beg end)
5847 "Check if MARKER is between BEG and END.
5848 If yes, remember the marker and the distance to BEG."
5849 (when (and (marker-buffer marker)
5850 (equal (marker-buffer marker) (current-buffer)))
5851 (if (and (>= marker beg) (< marker end))
5852 (push (cons marker (- marker beg)) org-markers-to-move))))
5854 (defun org-reinstall-markers-in-region (beg)
5855 "Move all remembered markers to their position relative to BEG."
5857 (move-marker (car x) (+ beg (cdr x))))
5858 org-markers-to-move)
5859 (setq org-markers-to-move nil))
5861 (defun org-narrow-to-subtree ()
5862 "Narrow buffer to the current subtree."
5867 (progn (org-back-to-heading) (point))
5868 (progn (org-end-of-subtree t) (point))))))
5870 (defun org-clone-subtree-with-time-shift (n &optional shift)
5871 "Clone the task (subtree) at point N times.
5872 The clones will be inserted as siblings.
5874 In interactive use, the user will be prompted for the number of clones
5875 to be produced, and for a time SHIFT, which may be a repeater as used
5876 in time stamps, for example `+3d'.
5878 When a valid repeater is given and the entry contains any time stamps,
5879 the clones will become a sequence in time, with time stamps in the
5880 subtree shifted for each clone produced. If SHIFT is nil or the
5881 empty string, time stamps will be left alone.
5883 If the original subtree did contain time stamps with a repeater,
5884 the following will happen:
5885 - the repeater will be removed in each clone
5886 - an additional clone will be produced, with the current, unshifted
5887 date(s) in the entry.
5888 - the original entry will be placed *after* all the clones, with
5890 - the start days in the repeater in the original entry will be shifted
5891 to past the last clone.
5892 I this way you can spell out a number of instances of a repeating task,
5893 and still retain the repeater to cover future instances of the task."
5894 (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
5895 (let (beg end template task
5896 shift-n shift-what doshift nmin nmax (n-no-remove -1))
5897 (if (not (and (integerp n) (> n 0)))
5898 (error "Invalid number of replications %s" n))
5899 (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
5900 (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
5902 (error "Invalid shift specification %s" shift))
5904 (setq shift-n (string-to-number (match-string 1 shift))
5905 shift-what (cdr (assoc (match-string 2 shift)
5906 '(("d" . day) ("w" . week)
5907 ("m" . month) ("y" . year))))))
5908 (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
5909 (setq nmin 1 nmax n)
5910 (org-back-to-heading t)
5912 (org-end-of-subtree t t)
5914 (setq template (buffer-substring beg end))
5916 (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
5917 (delete-region beg end)
5919 (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
5921 (loop for n from nmin to nmax do
5923 (setq task template)
5927 (goto-char (point-min))
5928 (while (re-search-forward org-ts-regexp nil t)
5929 (org-timestamp-change (* n shift-n) shift-what))
5930 (unless (= n n-no-remove)
5931 (goto-char (point-min))
5932 (while (re-search-forward org-ts-regexp nil t)
5934 (goto-char (match-beginning 0))
5935 (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
5936 (delete-region (match-beginning 1) (match-end 1))))))
5937 (setq task (buffer-string))))
5943 (defun org-sort (with-case)
5944 "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
5945 Optional argument WITH-CASE means sort case-sensitively.
5946 With a double prefix argument, also remove duplicate entries."
5948 (if (org-at-table-p)
5949 (org-call-with-arg 'org-table-sort-lines with-case)
5950 (org-call-with-arg 'org-sort-entries-or-items with-case)))
5952 (defun org-sort-remove-invisible (s)
5953 (remove-text-properties 0 (length s) org-rm-props s)
5954 (while (string-match org-bracket-link-regexp s)
5955 (setq s (replace-match (if (match-end 2)
5957 (match-string 1 s)) t t s)))
5960 (defvar org-priority-regexp) ; defined later in the file
5962 (defun org-sort-entries-or-items
5963 (&optional with-case sorting-type getkey-func compare-func property)
5964 "Sort entries on a certain level of an outline tree.
5965 If there is an active region, the entries in the region are sorted.
5966 Else, if the cursor is before the first entry, sort the top-level items.
5967 Else, the children of the entry at point are sorted.
5969 Sorting can be alphabetically, numerically, and by date/time as given by
5970 the first time stamp in the entry. The command prompts for the sorting
5971 type unless it has been given to the function through the SORTING-TYPE
5972 argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
5973 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
5974 called with point at the beginning of the record. It must return either
5975 a string or a number that should serve as the sorting key for that record.
5977 Comparing entries ignores case by default. However, with an optional argument
5978 WITH-CASE, the sorting considers case as well."
5980 (let ((case-func (if with-case 'identity 'downcase))
5981 start beg end stars re re2
5982 txt what tmp plain-list-p)
5983 ;; Find beginning and end of region to sort
5985 ((org-region-active-p)
5986 ;; we will sort the region
5987 (setq end (region-end)
5989 (goto-char (region-beginning))
5990 (if (not (org-on-heading-p)) (outline-next-heading))
5991 (setq start (point)))
5993 ;; we will sort this plain list
5994 (org-beginning-of-item-list) (setq start (point))
5995 (org-end-of-item-list) (setq end (point))
5997 (setq plain-list-p t
5999 ((or (org-on-heading-p)
6000 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
6001 ;; we will sort the children of the current headline
6002 (org-back-to-heading)
6004 end (progn (org-end-of-subtree t t)
6005 (org-back-over-empty-lines)
6010 (outline-next-heading))
6012 ;; we will sort the top-level entries in this file
6013 (goto-char (point-min))
6014 (or (org-on-heading-p) (outline-next-heading))
6015 (setq start (point) end (point-max) what "top-level")
6020 (if (>= beg end) (error "Nothing to sort"))
6022 (unless plain-list-p
6023 (looking-at "\\(\\*+\\)")
6024 (setq stars (match-string 1)
6025 re (concat "^" (regexp-quote stars) " +")
6026 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
6027 txt (buffer-substring beg end))
6028 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
6029 (if (and (not (equal stars "*")) (string-match re2 txt))
6030 (error "Region to sort contains a level above the first entry")))
6032 (unless sorting-type
6035 "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
6036 "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:")
6038 (setq sorting-type (read-char-exclusive))
6040 (and (= (downcase sorting-type) ?f)
6042 (org-ido-completing-read "Sort using function: "
6043 obarray 'fboundp t nil nil))
6044 (setq getkey-func (intern getkey-func)))
6046 (and (= (downcase sorting-type) ?r)
6048 (org-ido-completing-read "Property: "
6049 (mapcar 'list (org-buffer-property-keys t))
6052 (message "Sorting entries...")
6055 (narrow-to-region start end)
6057 (let ((dcst (downcase sorting-type))
6058 (now (current-time)))
6060 (/= dcst sorting-type)
6061 ;; This function moves to the beginning character of the "record" to
6065 (if (org-at-item-p) t (goto-char (point-max))))
6067 (if (re-search-forward re nil t)
6068 (goto-char (match-beginning 0))
6069 (goto-char (point-max)))))
6070 ;; This function moves to the last character of the "record" being
6077 (outline-forward-same-level 1)
6079 (goto-char (point-max)))))))
6081 ;; This function returns the value that gets sorted against.
6084 (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
6087 (string-to-number (buffer-substring (match-end 0)
6090 (buffer-substring (match-end 0) (point-at-eol)))
6092 (if (re-search-forward org-ts-regexp
6094 (org-time-string-to-time (match-string 0))
6099 (setq tmp (funcall getkey-func))
6100 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
6102 (error "Invalid key function `%s'" getkey-func)))
6103 (t (error "Invalid sorting type `%c'" sorting-type)))))
6107 (if (looking-at org-complex-heading-regexp)
6108 (string-to-number (match-string 4))
6111 (if (looking-at org-complex-heading-regexp)
6112 (funcall case-func (match-string 4))
6115 (if (re-search-forward org-ts-regexp
6119 (org-time-string-to-time (match-string 0))
6122 (if (re-search-forward org-priority-regexp (point-at-eol) t)
6123 (string-to-char (match-string 2))
6124 org-default-priority))
6126 (or (org-entry-get nil property) ""))
6128 (if (looking-at org-complex-heading-regexp)
6129 (- 9999 (length (member (match-string 2)
6130 org-todo-keywords-1)))))
6134 (setq tmp (funcall getkey-func))
6135 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
6137 (error "Invalid key function `%s'" getkey-func)))
6138 (t (error "Invalid sorting type `%c'" sorting-type)))))
6141 ((= dcst ?a) 'string<)
6142 ((= dcst ?t) 'time-less-p)
6143 ((= dcst ?f) compare-func)
6145 (message "Sorting entries...done")))
6147 (defun org-do-sort (table what &optional with-case sorting-type)
6148 "Sort TABLE of WHAT according to SORTING-TYPE.
6149 The user will be prompted for the SORTING-TYPE if the call to this
6150 function does not specify it. WHAT is only for the prompt, to indicate
6151 what is being sorted. The sorting key will be extracted from
6152 the car of the elements of the table.
6153 If WITH-CASE is non-nil, the sorting will be case-sensitive."
6154 (unless sorting-type
6156 "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
6158 (setq sorting-type (read-char-exclusive)))
6159 (let ((dcst (downcase sorting-type))
6160 extractfun comparefun)
6161 ;; Define the appropriate functions
6164 (setq extractfun 'string-to-number
6165 comparefun (if (= dcst sorting-type) '< '>)))
6167 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
6168 (lambda(x) (downcase (org-sort-remove-invisible x))))
6169 comparefun (if (= dcst sorting-type)
6171 (lambda (a b) (and (not (string< a b))
6172 (not (string= a b)))))))
6176 (if (or (string-match org-ts-regexp x)
6177 (string-match org-ts-regexp-both x))
6179 (org-time-string-to-time (match-string 0 x)))
6181 comparefun (if (= dcst sorting-type) '< '>)))
6182 (t (error "Invalid sorting type `%c'" sorting-type)))
6184 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
6186 (lambda (a b) (funcall comparefun (car a) (car b))))))
6188 ;;; Editing source examples
6190 (defvar org-exit-edit-mode-map (make-sparse-keymap))
6191 (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
6192 (defvar org-edit-src-force-single-line nil)
6193 (defvar org-edit-src-from-org-mode nil)
6194 (defvar org-edit-src-picture nil)
6196 (define-minor-mode org-exit-edit-mode
6197 "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
6199 (defun org-edit-src-code ()
6200 "Edit the source code example at point.
6201 An indirect buffer is created, and that buffer is then narrowed to the
6202 example at point and switched to the correct language mode. When done,
6203 exit by killing the buffer with \\[org-edit-src-exit]."
6205 (let ((line (org-current-line))
6206 (case-fold-search t)
6207 (msg (substitute-command-keys
6208 "Edit, then exit with C-c ' (C-c and single quote)"))
6209 (info (org-edit-src-find-region-and-lang))
6210 (org-mode-p (eq major-mode 'org-mode))
6211 beg end lang lang-f single lfmt)
6214 (setq beg (nth 0 info)
6219 lang-f (intern (concat lang "-mode")))
6220 (unless (functionp lang-f)
6221 (error "No such language mode: %s" lang-f))
6223 (if (get-buffer "*Org Edit Src Example*")
6224 (kill-buffer "*Org Edit Src Example*"))
6225 (switch-to-buffer (make-indirect-buffer (current-buffer)
6226 "*Org Edit Src Example*"))
6227 (narrow-to-region beg end)
6228 (remove-text-properties beg end '(display nil invisible nil
6230 (let ((org-inhibit-startup t))
6232 (set (make-local-variable 'org-edit-src-force-single-line) single)
6233 (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
6235 (set (make-local-variable 'org-coderef-label-format) lfmt))
6237 (goto-char (point-min))
6238 (while (re-search-forward "^," nil t)
6239 (replace-match "")))
6241 (org-exit-edit-mode)
6242 (org-set-local 'header-line-format msg)
6246 (defun org-edit-fixed-width-region ()
6247 "Edit the fixed-width ascii drawing at point.
6248 This must be a region where each line starts with a colon followed by
6250 An indirect buffer is created, and that buffer is then narrowed to the
6251 example at point and switched to artist-mode. When done,
6252 exit by killing the buffer with \\[org-edit-src-exit]."
6254 (let ((line (org-current-line))
6255 (case-fold-search t)
6256 (msg (substitute-command-keys
6257 "Edit, then exit with C-c ' (C-c and single quote)"))
6258 (org-mode-p (eq major-mode 'org-mode))
6260 (beginning-of-line 1)
6261 (if (looking-at "[ \t]*[^:\n \t]")
6263 (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
6264 (setq beg (point) end beg)
6266 (if (re-search-backward "^[ \t]*[^:]" nil 'move)
6267 (setq beg (point-at-bol 2))
6268 (setq beg (point))))
6270 (if (re-search-forward "^[ \t]*[^:]" nil 'move)
6271 (setq end (1- (match-beginning 0)))
6272 (setq end (point))))
6274 (if (get-buffer "*Org Edit Picture*")
6275 (kill-buffer "*Org Edit Picture*"))
6276 (switch-to-buffer (make-indirect-buffer (current-buffer)
6277 "*Org Edit Picture*"))
6278 (narrow-to-region beg end)
6279 (remove-text-properties beg end '(display nil invisible nil
6281 (when (fboundp 'font-lock-unfontify-region)
6282 (font-lock-unfontify-region (point-min) (point-max)))
6284 ((eq org-edit-fixed-width-region-mode 'artist-mode)
6287 (t (funcall org-edit-fixed-width-region-mode)))
6288 (set (make-local-variable 'org-edit-src-force-single-line) nil)
6289 (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
6290 (set (make-local-variable 'org-edit-src-picture) t)
6291 (goto-char (point-min))
6292 (while (re-search-forward "^[ \t]*: ?" nil t)
6295 (org-exit-edit-mode)
6296 (org-set-local 'header-line-format msg)
6301 (defun org-edit-src-find-region-and-lang ()
6302 "Find the region and language for a local edit.
6303 Return a list with beginning and end of the region, a string representing
6304 the language, a switch telling of the content should be in a single line."
6307 org-edit-src-region-extra
6309 ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
6310 ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
6311 ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
6312 ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
6313 ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
6314 ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
6315 ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
6316 ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
6317 ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
6318 ("^#\\+html:" "\n" "html" single-line)
6319 ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
6320 ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
6321 ("^#\\+latex:" "\n" "latex" single-line)
6322 ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
6323 ("^#\\+ascii:" "\n" "ascii" single-line)
6326 re1 re2 single beg end lang lfmt match-re1)
6328 (while (setq entry (pop re-list))
6329 (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
6330 single (nth 3 entry))
6332 (if (or (looking-at re1)
6333 (re-search-backward re1 nil t))
6335 (setq match-re1 (match-string 0))
6336 (setq beg (match-end 0)
6337 lang (org-edit-src-get-lang lang)
6338 lfmt (org-edit-src-get-label-format match-re1))
6339 (if (and (re-search-forward re2 nil t)
6340 (>= (match-end 0) pos))
6341 (throw 'exit (list beg (match-beginning 0)
6342 lang single lfmt))))
6343 (if (or (looking-at re2)
6344 (re-search-forward re2 nil t))
6346 (setq end (match-beginning 0))
6347 (if (and (re-search-backward re1 nil t)
6348 (<= (match-beginning 0) pos))
6350 (setq lfmt (org-edit-src-get-label-format
6353 (list (match-end 0) end
6354 (org-edit-src-get-lang lang)
6355 single lfmt))))))))))))
6357 (defun org-edit-src-get-lang (lang)
6358 "Extract the src language."
6359 (let ((m (match-string 0)))
6361 ((stringp lang) lang)
6362 ((integerp lang) (match-string lang))
6363 ((and (eq lang 'lang)
6364 (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
6366 ((and (eq lang 'style)
6367 (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
6369 (t "fundamental"))))
6371 (defun org-edit-src-get-label-format (s)
6372 "Extract the label format."
6374 (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
6375 (match-string 1 s))))
6377 (defun org-edit-src-exit ()
6378 "Exit special edit and protect problematic lines."
6380 (unless (buffer-base-buffer (current-buffer))
6381 (error "This is not an indirect buffer, something is wrong..."))
6382 (unless (> (point-min) 1)
6383 (error "This buffer is not narrowed, something is wrong..."))
6384 (goto-char (point-min))
6385 (if (looking-at "[ \t\n]*\n") (replace-match ""))
6386 (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
6387 (when (org-bound-and-true-p org-edit-src-force-single-line)
6388 (goto-char (point-min))
6389 (while (re-search-forward "\n" nil t)
6390 (replace-match " "))
6391 (goto-char (point-min))
6392 (if (looking-at "\\s-*") (replace-match " "))
6393 (if (re-search-forward "\\s-+\\'" nil t)
6394 (replace-match "")))
6395 (when (org-bound-and-true-p org-edit-src-from-org-mode)
6396 (goto-char (point-min))
6397 (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
6398 (replace-match ",\\1"))
6399 (when font-lock-mode
6400 (font-lock-unfontify-region (point-min) (point-max)))
6401 (put-text-property (point-min) (point-max) 'font-lock-fontified t))
6402 (when (org-bound-and-true-p org-edit-src-picture)
6403 (untabify (point-min) (point-max))
6404 (goto-char (point-min))
6405 (while (re-search-forward "^" nil t)
6406 (replace-match ": "))
6407 (when font-lock-mode
6408 (font-lock-unfontify-region (point-min) (point-max)))
6409 (put-text-property (point-min) (point-max) 'font-lock-fontified t))
6410 (kill-buffer (current-buffer))
6411 (and (org-mode-p) (org-restart-font-lock)))
6414 ;;; The orgstruct minor mode
6416 ;; Define a minor mode which can be used in other modes in order to
6417 ;; integrate the org-mode structure editing commands.
6419 ;; This is really a hack, because the org-mode structure commands use
6420 ;; keys which normally belong to the major mode. Here is how it
6421 ;; works: The minor mode defines all the keys necessary to operate the
6422 ;; structure commands, but wraps the commands into a function which
6423 ;; tests if the cursor is currently at a headline or a plain list
6424 ;; item. If that is the case, the structure command is used,
6425 ;; temporarily setting many Org-mode variables like regular
6426 ;; expressions for filling etc. However, when any of those keys is
6427 ;; used at a different location, function uses `key-binding' to look
6428 ;; up if the key has an associated command in another currently active
6429 ;; keymap (minor modes, major mode, global), and executes that
6430 ;; command. There might be problems if any of the keys is otherwise
6431 ;; used as a prefix key.
6433 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
6434 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
6435 ;; addresses this by checking explicitly for both bindings.
6437 (defvar orgstruct-mode-map (make-sparse-keymap)
6438 "Keymap for the minor `orgstruct-mode'.")
6440 (defvar org-local-vars nil
6441 "List of local variables, for use by `orgstruct-mode'")
6444 (define-minor-mode orgstruct-mode
6445 "Toggle the minor more `orgstruct-mode'.
6446 This mode is for using Org-mode structure commands in other modes.
6447 The following key behave as if Org-mode was active, if the cursor
6448 is on a headline, or on a plain list item (both in the definition
6451 M-up Move entry/item up
6452 M-down Move entry/item down
6455 M-S-up Move entry/item up
6456 M-S-down Move entry/item down
6457 M-S-left Promote subtree
6458 M-S-right Demote subtree
6459 M-q Fill paragraph and items like in Org-mode
6461 C-c - Cycle list bullet
6462 TAB Cycle item visibility
6463 M-RET Insert new heading/item
6464 S-M-RET Insert new TODO heading / Checkbox item
6465 C-c C-c Set tags / toggle checkbox"
6466 nil " OrgStruct" nil
6467 (org-load-modules-maybe)
6468 (and (orgstruct-setup) (defun orgstruct-setup () nil)))
6471 (defun turn-on-orgstruct ()
6472 "Unconditionally turn on `orgstruct-mode'."
6475 (defun orgstruct++-mode (&optional arg)
6476 "Toggle `orgstruct-mode', the enhanced version of it.
6477 In addition to setting orgstruct-mode, this also exports all indentation and
6478 autofilling variables from org-mode into the buffer. It will also
6479 recognize item context in multiline items.
6480 Note that turning off orgstruct-mode will *not* remove the
6481 indentation/paragraph settings. This can only be done by refreshing the
6482 major mode, for example with \[normal-mode]."
6484 (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
6492 "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
6493 (symbol-name (car x)))
6494 (setq var (car x) val (nth 1 x))
6495 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
6497 (org-set-local 'orgstruct-is-++ t))))
6499 (defvar orgstruct-is-++ nil
6500 "Is orgstruct-mode in ++ version in the current-buffer?")
6501 (make-variable-buffer-local 'orgstruct-is-++)
6504 (defun turn-on-orgstruct++ ()
6505 "Unconditionally turn on `orgstruct++-mode'."
6506 (orgstruct++-mode 1))
6508 (defun orgstruct-error ()
6509 "Error when there is no default binding for a structure key."
6511 (error "This key has no function outside structure elements"))
6513 (defun orgstruct-setup ()
6514 "Setup orgstruct keymaps."
6518 '([(meta up)] org-metaup)
6519 '([(meta down)] org-metadown)
6520 '([(meta left)] org-metaleft)
6521 '([(meta right)] org-metaright)
6522 '([(meta shift up)] org-shiftmetaup)
6523 '([(meta shift down)] org-shiftmetadown)
6524 '([(meta shift left)] org-shiftmetaleft)
6525 '([(meta shift right)] org-shiftmetaright)
6526 '([(shift up)] org-shiftup)
6527 '([(shift down)] org-shiftdown)
6528 '([(shift left)] org-shiftleft)
6529 '([(shift right)] org-shiftright)
6530 '("\C-c\C-c" org-ctrl-c-ctrl-c)
6531 '("\M-q" fill-paragraph)
6533 '("\C-c-" org-cycle-list-bullet)))
6535 (while (setq elt (pop bindings))
6536 (setq nfunc (1+ nfunc))
6537 (setq key (org-key (car elt))
6539 cmd (orgstruct-make-binding fun nfunc key))
6540 (org-defkey orgstruct-mode-map key cmd))
6542 ;; Special treatment needed for TAB and RET
6543 (org-defkey orgstruct-mode-map [(tab)]
6544 (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
6545 (org-defkey orgstruct-mode-map "\C-i"
6546 (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
6548 (org-defkey orgstruct-mode-map "\M-\C-m"
6549 (orgstruct-make-binding 'org-insert-heading 105
6550 "\M-\C-m" [(meta return)]))
6551 (org-defkey orgstruct-mode-map [(meta return)]
6552 (orgstruct-make-binding 'org-insert-heading 106
6553 [(meta return)] "\M-\C-m"))
6555 (org-defkey orgstruct-mode-map [(shift meta return)]
6556 (orgstruct-make-binding 'org-insert-todo-heading 107
6557 [(meta return)] "\M-\C-m"))
6559 (unless org-local-vars
6560 (setq org-local-vars (org-get-local-variables)))
6564 (defun orgstruct-make-binding (fun n &rest keys)
6565 "Create a function for binding in the structure minor mode.
6566 FUN is the command to call inside a table. N is used to create a unique
6567 command name. KEYS are keys that should be checked in for a command
6568 to execute outside of tables."
6571 (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
6573 (concat "In Structure, run `" (symbol-name fun) "'.\n"
6574 "Outside of structure, run the binding of `"
6575 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
6579 `(org-context-p 'headline 'item
6580 (and orgstruct-is-++
6581 ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
6583 (list 'org-run-like-in-org-mode (list 'quote fun))
6584 (list 'let '(orgstruct-mode)
6585 (list 'call-interactively
6588 (list 'key-binding k))
6590 '('orgstruct-error))))))))
6592 (defun org-context-p (&rest contexts)
6593 "Check if local context is any of CONTEXTS.
6594 Possible values in the list of contexts are `table', `headline', and `item'."
6595 (let ((pos (point)))
6596 (goto-char (point-at-bol))
6597 (prog1 (or (and (memq 'table contexts)
6598 (looking-at "[ \t]*|"))
6599 (and (memq 'headline contexts)
6600 ;;????????? (looking-at "\\*+"))
6601 (looking-at outline-regexp))
6602 (and (memq 'item contexts)
6603 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
6604 (and (memq 'item-body contexts)
6608 (defun org-get-local-variables ()
6609 "Return a list of all local variables in an org-mode buffer."
6611 (with-current-buffer (get-buffer-create "*Org tmp*")
6614 (setq varlist (buffer-local-variables)))
6615 (kill-buffer "*Org tmp*")
6622 (list (car x) (list 'quote (cdr x)))))
6624 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
6625 (symbol-name (car x)))
6630 (defun org-run-like-in-org-mode (cmd)
6631 "Run a command, pretending that the current buffer is in Org-mode.
6632 This will temporarily bind local variables that are typically bound in
6633 Org-mode to the values they have in Org-mode, and then interactively
6635 (org-load-modules-maybe)
6636 (unless org-local-vars
6637 (setq org-local-vars (org-get-local-variables)))
6638 (eval (list 'let org-local-vars
6639 (list 'call-interactively (list 'quote cmd)))))
6643 (defun org-get-category (&optional pos)
6644 "Get the category applying to position POS."
6645 (get-text-property (or pos (point)) 'org-category))
6647 (defun org-refresh-category-properties ()
6648 "Refresh category text properties in the buffer."
6649 (let ((def-cat (cond
6650 ((null org-category)
6651 (if buffer-file-name
6652 (file-name-sans-extension
6653 (file-name-nondirectory buffer-file-name))
6655 ((symbolp org-category) (symbol-name org-category))
6657 beg end cat pos optionp)
6662 (goto-char (point-min))
6663 (put-text-property (point) (point-max) 'org-category def-cat)
6664 (while (re-search-forward
6665 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
6666 (setq pos (match-end 0)
6667 optionp (equal (char-after (match-beginning 0)) ?#)
6668 cat (org-trim (match-string 2)))
6670 (setq beg (point-at-bol) end (point-max))
6671 (org-back-to-heading t)
6672 (setq beg (point) end (org-end-of-subtree t t)))
6673 (put-text-property beg end 'org-category cat)
6674 (goto-char pos)))))))
6679 ;;; Link abbreviations
6681 (defun org-link-expand-abbrev (link)
6682 "Apply replacements as defined in `org-link-abbrev-alist."
6683 (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
6684 (let* ((key (match-string 1 link))
6685 (as (or (assoc key org-link-abbrev-alist-local)
6686 (assoc key org-link-abbrev-alist)))
6687 (tag (and (match-end 2) (match-string 3 link)))
6693 ((symbolp rpl) (funcall rpl tag))
6694 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
6695 ((string-match "%h" rpl)
6696 (replace-match (url-hexify-string (or tag "")) t t rpl))
6697 (t (concat rpl tag)))))
6700 ;;; Storing and inserting links
6702 (defvar org-insert-link-history nil
6703 "Minibuffer history for links inserted with `org-insert-link'.")
6705 (defvar org-stored-links nil
6706 "Contains the links stored with `org-store-link'.")
6708 (defvar org-store-link-plist nil
6709 "Plist with info about the most recently link created with `org-store-link'.")
6711 (defvar org-link-protocols nil
6712 "Link protocols added to Org-mode using `org-add-link-type'.")
6714 (defvar org-store-link-functions nil
6715 "List of functions that are called to create and store a link.
6716 Each function will be called in turn until one returns a non-nil
6717 value. Each function should check if it is responsible for creating
6718 this link (for example by looking at the major mode).
6719 If not, it must exit and return nil.
6720 If yes, it should return a non-nil value after a calling
6721 `org-store-link-props' with a list of properties and values.
6722 Special properties are:
6724 :type The link prefix. like \"http\". This must be given.
6725 :link The link, like \"http://www.astro.uva.nl/~dominik\".
6726 This is obligatory as well.
6727 :description Optional default description for the second pair
6728 of brackets in an Org-mode link. The user can still change
6729 this when inserting this link into an Org-mode buffer.
6731 In addition to these, any additional properties can be specified
6732 and then used in remember templates.")
6734 (defun org-add-link-type (type &optional follow export)
6735 "Add TYPE to the list of `org-link-types'.
6736 Re-compute all regular expressions depending on `org-link-types'
6738 FOLLOW and EXPORT are two functions.
6740 FOLLOW should take the link path as the single argument and do whatever
6741 is necessary to follow the link, for example find a file or display
6744 EXPORT should format the link path for export to one of the export formats.
6745 It should be a function accepting three arguments:
6747 path the path of the link, the text after the prefix (like \"http:\")
6748 desc the description of the link, if any, nil if there was no description
6749 format the export format, a symbol like `html' or `latex'.
6751 The function may use the FORMAT information to return different values
6752 depending on the format. The return value will be put literally into
6754 Org-mode has a built-in default for exporting links. If you are happy with
6755 this default, there is no need to define an export function for the link
6756 type. For a simple example of an export function, see `org-bbdb.el'."
6757 (add-to-list 'org-link-types type t)
6758 (org-make-link-regexps)
6759 (if (assoc type org-link-protocols)
6760 (setcdr (assoc type org-link-protocols) (list follow export))
6761 (push (list type follow export) org-link-protocols)))
6764 (defun org-store-link (arg)
6765 "\\<org-mode-map>Store an org-link to the current location.
6766 This link is added to `org-stored-links' and can later be inserted
6767 into an org-buffer with \\[org-insert-link].
6769 For some link types, a prefix arg is interpreted:
6770 For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
6771 For file links, arg negates `org-context-in-file-links'."
6773 (org-load-modules-maybe)
6774 (setq org-store-link-plist nil) ; reset
6775 (let (link cpltxt desc description search txt)
6778 ((run-hook-with-args-until-success 'org-store-link-functions)
6779 (setq link (plist-get org-store-link-plist :link)
6780 desc (or (plist-get org-store-link-plist :description) link)))
6782 ((equal (buffer-name) "*Org Edit Src Example*")
6784 (while (or (not label)
6788 (goto-char (point-min))
6790 (regexp-quote (format org-coderef-label-format label))
6792 (when label (message "Label exists already") (sit-for 2))
6793 (setq label (read-string "Code line label: " label)))
6795 (setq link (format org-coderef-label-format label))
6796 (setq gc (- 79 (length link)))
6797 (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
6799 (setq link (concat "(" label ")") desc nil)))
6801 ((eq major-mode 'calendar-mode)
6802 (let ((cd (calendar-cursor-to-date)))
6805 (car org-time-stamp-formats)
6807 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
6809 (org-store-link-props :type "calendar" :date cd)))
6811 ((eq major-mode 'w3-mode)
6812 (setq cpltxt (if (and (buffer-name)
6813 (not (string-match "Untitled" (buffer-name))))
6816 link (org-make-link (url-view-url t)))
6817 (org-store-link-props :type "w3" :url (url-view-url t)))
6819 ((eq major-mode 'w3m-mode)
6820 (setq cpltxt (or w3m-current-title w3m-current-url)
6821 link (org-make-link w3m-current-url))
6822 (org-store-link-props :type "w3m" :url (url-view-url t)))
6824 ((setq search (run-hook-with-args-until-success
6825 'org-create-file-search-functions))
6826 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
6828 (setq cpltxt (or description link)))
6830 ((eq major-mode 'image-mode)
6831 (setq cpltxt (concat "file:"
6832 (abbreviate-file-name buffer-file-name))
6833 link (org-make-link cpltxt))
6834 (org-store-link-props :type "image" :file buffer-file-name))
6836 ((eq major-mode 'dired-mode)
6837 ;; link to the file in the current line
6838 (setq cpltxt (concat "file:"
6839 (abbreviate-file-name
6841 (dired-get-filename nil t))))
6842 link (org-make-link cpltxt)))
6844 ((and buffer-file-name (org-mode-p))
6846 ((org-in-regexp "<<\\(.*?\\)>>")
6849 (abbreviate-file-name buffer-file-name)
6850 "::" (match-string 1))
6851 link (org-make-link cpltxt)))
6852 ((and (featurep 'org-id)
6853 (or (eq org-link-to-org-use-id t)
6854 (and (eq org-link-to-org-use-id 'create-if-interactive)
6856 (and org-link-to-org-use-id
6858 (org-entry-get nil "ID")
6860 ;; We can make a link using the ID.
6861 (setq link (condition-case nil
6862 (prog1 (org-id-store-link)
6863 (setq desc (plist-get org-store-link-plist
6866 ;; probably before first headline, link to file only
6868 (abbreviate-file-name buffer-file-name))))))
6870 ;; Just link to current headline
6871 (setq cpltxt (concat "file:"
6872 (abbreviate-file-name buffer-file-name)))
6873 ;; Add a context search string
6874 (when (org-xor org-context-in-file-links arg)
6876 ((org-on-heading-p) nil)
6877 ((org-region-active-p)
6878 (buffer-substring (region-beginning) (region-end)))
6880 (when (or (null txt) (string-match "\\S-" txt))
6884 (org-make-org-heading-search-string txt)
6887 (if (string-match "::\\'" cpltxt)
6888 (setq cpltxt (substring cpltxt 0 -2)))
6889 (setq link (org-make-link cpltxt)))))
6891 ((buffer-file-name (buffer-base-buffer))
6892 ;; Just link to this file here.
6893 (setq cpltxt (concat "file:"
6894 (abbreviate-file-name
6895 (buffer-file-name (buffer-base-buffer)))))
6896 ;; Add a context string
6897 (when (org-xor org-context-in-file-links arg)
6898 (setq txt (if (org-region-active-p)
6899 (buffer-substring (region-beginning) (region-end))
6900 (buffer-substring (point-at-bol) (point-at-eol))))
6901 ;; Only use search option if there is some text.
6902 (when (string-match "\\S-" txt)
6904 (concat cpltxt "::" (org-make-org-heading-search-string txt))
6906 (setq link (org-make-link cpltxt)))
6909 (error "Cannot link to a buffer which is not visiting a file"))
6911 (t (setq link nil)))
6913 (if (consp link) (setq cpltxt (car link) link (cdr link)))
6914 (setq link (or link cpltxt)
6915 desc (or desc cpltxt))
6916 (if (equal desc "NONE") (setq desc nil))
6918 (if (and (interactive-p) link)
6920 (setq org-stored-links
6921 (cons (list link desc) org-stored-links))
6922 (message "Stored: %s" (or desc link)))
6923 (and link (org-make-link-string link desc)))))
6925 (defun org-store-link-props (&rest plist)
6926 "Store link properties, extract names and addresses."
6928 (when (setq x (plist-get plist :from))
6929 (setq adr (mail-extract-address-components x))
6930 (setq plist (plist-put plist :fromname (car adr)))
6931 (setq plist (plist-put plist :fromaddress (nth 1 adr))))
6932 (when (setq x (plist-get plist :to))
6933 (setq adr (mail-extract-address-components x))
6934 (setq plist (plist-put plist :toname (car adr)))
6935 (setq plist (plist-put plist :toaddress (nth 1 adr)))))
6936 (let ((from (plist-get plist :from))
6937 (to (plist-get plist :to)))
6938 (when (and from to org-from-is-user-regexp)
6940 (plist-put plist :fromto
6941 (if (string-match org-from-is-user-regexp from)
6943 (concat "from %f"))))))
6944 (setq org-store-link-plist plist))
6946 (defun org-add-link-props (&rest plist)
6947 "Add these properties to the link property list."
6950 (setq key (pop plist) value (pop plist))
6951 (setq org-store-link-plist
6952 (plist-put org-store-link-plist key value)))))
6954 (defun org-email-link-description (&optional fmt)
6955 "Return the description part of an email link.
6956 This takes information from `org-store-link-plist' and formats it
6957 according to FMT (default from `org-email-link-description-format')."
6958 (setq fmt (or fmt org-email-link-description-format))
6959 (let* ((p org-store-link-plist)
6960 (to (plist-get p :toaddress))
6961 (from (plist-get p :fromaddress))
6964 (cons "%c" (plist-get p :fromto))
6965 (cons "%F" (plist-get p :from))
6966 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
6967 (cons "%T" (plist-get p :to))
6968 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
6969 (cons "%s" (plist-get p :subject))
6970 (cons "%m" (plist-get p :message-id)))))
6971 (when (string-match "%c" fmt)
6972 ;; Check if the user wrote this message
6973 (if (and org-from-is-user-regexp from to
6974 (save-match-data (string-match org-from-is-user-regexp from)))
6975 (setq fmt (replace-match "to %t" t t fmt))
6976 (setq fmt (replace-match "from %f" t t fmt))))
6977 (org-replace-escapes fmt table)))
6979 (defun org-make-org-heading-search-string (&optional string heading)
6980 "Make search string for STRING or current headline."
6982 (let ((s (or string (org-get-heading))))
6983 (unless (and string (not heading))
6984 ;; We are using a headline, clean up garbage in there.
6985 (if (string-match org-todo-regexp s)
6986 (setq s (replace-match "" t t s)))
6987 (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
6988 (setq s (replace-match "" t t s)))
6989 (setq s (org-trim s))
6990 (if (string-match (concat "^\\(" org-quote-string "\\|"
6991 org-comment-string "\\)") s)
6992 (setq s (replace-match "" t t s)))
6993 (while (string-match org-ts-regexp s)
6994 (setq s (replace-match "" t t s))))
6995 (while (string-match "[^a-zA-Z_0-9 \t]+" s)
6996 (setq s (replace-match " " t t s)))
6997 (or string (setq s (concat "*" s))) ; Add * for headlines
6998 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
7000 (defun org-make-link (&rest strings)
7001 "Concatenate STRINGS."
7002 (apply 'concat strings))
7004 (defun org-make-link-string (link &optional description)
7005 "Make a link with brackets, consisting of LINK and DESCRIPTION."
7006 (unless (string-match "\\S-" link)
7007 (error "Empty link"))
7008 (when (stringp description)
7009 ;; Remove brackets from the description, they are fatal.
7010 (while (string-match "\\[" description)
7011 (setq description (replace-match "{" t t description)))
7012 (while (string-match "\\]" description)
7013 (setq description (replace-match "}" t t description))))
7014 (when (equal (org-link-escape link) description)
7015 ;; No description needed, it is identical
7016 (setq description nil))
7017 (when (and (not description)
7018 (not (equal link (org-link-escape link))))
7019 (setq description (org-extract-attributes link)))
7020 (concat "[[" (org-link-escape link) "]"
7021 (if description (concat "[" description "]") "")
7024 (defconst org-link-escape-chars
7028 (?\340 . "%E0") ; `a
7029 (?\342 . "%E2") ; ^a
7030 (?\347 . "%E7") ; ,c
7031 (?\350 . "%E8") ; `e
7032 (?\351 . "%E9") ; 'e
7033 (?\352 . "%EA") ; ^e
7034 (?\356 . "%EE") ; ^i
7035 (?\364 . "%F4") ; ^o
7036 (?\371 . "%F9") ; `u
7037 (?\373 . "%FB") ; ^u
7043 "Association list of escapes for some characters problematic in links.
7044 This is the list that is used for internal purposes.")
7046 (defconst org-link-escape-chars-browser
7047 '((?\ . "%20")) ; 32 for the SPC char
7048 "Association list of escapes for some characters problematic in links.
7049 This is the list that is used before handing over to the browser.")
7051 (defun org-link-escape (text &optional table)
7052 "Escape characters in TEXT that are problematic for links."
7053 (setq table (or table org-link-escape-chars))
7055 (let ((re (mapconcat (lambda (x) (regexp-quote
7056 (char-to-string (car x))))
7058 (while (string-match re text)
7061 (cdr (assoc (string-to-char (match-string 0 text))
7066 (defun org-link-unescape (text &optional table)
7067 "Reverse the action of `org-link-escape'."
7068 (setq table (or table org-link-escape-chars))
7070 (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
7072 (while (string-match re text)
7075 (char-to-string (car (rassoc (match-string 0 text) table)))
7079 (defun org-xor (a b)
7083 (defun org-fixup-message-id-for-http (s)
7084 "Replace special characters in a message id, so it can be used in an http query."
7085 (while (string-match "<" s)
7086 (setq s (replace-match "%3C" t t s)))
7087 (while (string-match ">" s)
7088 (setq s (replace-match "%3E" t t s)))
7089 (while (string-match "@" s)
7090 (setq s (replace-match "%40" t t s)))
7094 (defun org-insert-link-global ()
7095 "Insert a link like Org-mode does.
7096 This command can be called in any mode to insert a link in Org-mode syntax."
7098 (org-load-modules-maybe)
7099 (org-run-like-in-org-mode 'org-insert-link))
7101 (defun org-insert-link (&optional complete-file link-location)
7102 "Insert a link. At the prompt, enter the link.
7104 Completion can be used to insert any of the link protocol prefixes like
7107 The history can be used to select a link previously stored with
7108 `org-store-link'. When the empty string is entered (i.e. if you just
7109 press RET at the prompt), the link defaults to the most recently
7110 stored link. As SPC triggers completion in the minibuffer, you need to
7111 use M-SPC or C-q SPC to force the insertion of a space character.
7113 You will also be prompted for a description, and if one is given, it will
7114 be displayed in the buffer instead of the link.
7116 If there is already a link at point, this command will allow you to edit link
7117 and description parts.
7119 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
7120 be selected using completion. The path to the file will be relative to the
7121 current directory if the file is in the current directory or a subdirectory.
7122 Otherwise, the link will be the absolute path as completed in the minibuffer
7123 \(i.e. normally ~/path/to/file). You can configure this behavior using the
7124 option `org-link-file-path-type'.
7126 With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
7127 the current directory or below.
7129 With three \\[universal-argument] prefixes, negate the meaning of
7130 `org-keep-stored-link-after-insertion'.
7132 If `org-make-link-description-function' is non-nil, this function will be
7133 called with the link target, and the result will be the default
7136 If the LINK-LOCATION parameter is non-nil, this value will be
7137 used as the link location instead of reading one interactively."
7139 (let* ((wcf (current-window-configuration))
7140 (region (if (org-region-active-p)
7141 (buffer-substring (region-beginning) (region-end))))
7142 (remove (and region (list (region-beginning) (region-end))))
7144 tmphist ; byte-compile incorrectly complains about this
7145 (link link-location)
7148 (link-location) ; specified by arg, just use it.
7149 ((org-in-regexp org-bracket-link-regexp 1)
7150 ;; We do have a link at point, and we are going to edit it.
7151 (setq remove (list (match-beginning 0) (match-end 0)))
7152 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
7153 (setq link (read-string "Link: "
7155 (org-match-string-no-properties 1)))))
7156 ((or (org-in-regexp org-angle-link-re)
7157 (org-in-regexp org-plain-link-re))
7158 ;; Convert to bracket link
7159 (setq remove (list (match-beginning 0) (match-end 0))
7160 link (read-string "Link: "
7161 (org-remove-angle-brackets (match-string 0)))))
7162 ((member complete-file '((4) (16)))
7163 ;; Completing read for file names.
7164 (setq file (read-file-name "File: "))
7165 (let ((pwd (file-name-as-directory (expand-file-name ".")))
7166 (pwd1 (file-name-as-directory (abbreviate-file-name
7167 (expand-file-name ".")))))
7169 ((equal complete-file '(16))
7170 (setq link (org-make-link
7172 (abbreviate-file-name (expand-file-name file)))))
7173 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
7174 (setq link (org-make-link "file:" (match-string 1 file))))
7175 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
7176 (expand-file-name file))
7177 (setq link (org-make-link
7178 "file:" (match-string 1 (expand-file-name file)))))
7179 (t (setq link (org-make-link "file:" file))))))
7181 ;; Read link, with completion for stored links.
7182 (with-output-to-temp-buffer "*Org Links*"
7183 (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
7184 (when org-stored-links
7185 (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
7188 (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
7189 (reverse org-stored-links) "\n"))))
7190 (let ((cw (selected-window)))
7191 (select-window (get-buffer-window "*Org Links*"))
7192 (setq truncate-lines t)
7193 (org-fit-window-to-buffer)
7195 ;; Fake a link history, containing the stored links.
7196 (setq tmphist (append (mapcar 'car org-stored-links)
7197 org-insert-link-history))
7200 (let ((org-completion-use-ido nil))
7201 (org-completing-read
7204 (mapcar (lambda (x) (list (concat (car x) ":")))
7205 (append org-link-abbrev-alist-local org-link-abbrev-alist))
7206 (mapcar (lambda (x) (list (concat x ":")))
7210 (or (car (car org-stored-links))))))
7211 (set-window-configuration wcf)
7212 (kill-buffer "*Org Links*"))
7213 (setq entry (assoc link org-stored-links))
7214 (or entry (push link org-insert-link-history))
7215 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
7216 (not org-keep-stored-link-after-insertion))
7217 (setq org-stored-links (delq (assoc link org-stored-links)
7219 (setq desc (or desc (nth 1 entry)))))
7221 (if (string-match org-plain-link-re link)
7222 ;; URL-like link, normalize the use of angular brackets.
7223 (setq link (org-make-link (org-remove-angle-brackets link))))
7225 ;; Check if we are linking to the current file with a search option
7226 ;; If yes, simplify the link by using only the search option.
7227 (when (and buffer-file-name
7228 (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
7229 (let* ((path (match-string 1 link))
7230 (case-fold-search nil)
7231 (search (match-string 2 link)))
7233 (if (equal (file-truename buffer-file-name) (file-truename path))
7234 ;; We are linking to this same file, with a search option
7235 (setq link search)))))
7237 ;; Check if we can/should use a relative path. If yes, simplify the link
7238 (when (string-match "^file:\\(.*\\)" link)
7239 (let* ((path (match-string 1 link))
7241 (case-fold-search nil))
7243 ((or (eq org-link-file-path-type 'absolute)
7244 (equal complete-file '(16)))
7245 (setq path (abbreviate-file-name (expand-file-name path))))
7246 ((eq org-link-file-path-type 'noabbrev)
7247 (setq path (expand-file-name path)))
7248 ((eq org-link-file-path-type 'relative)
7249 (setq path (file-relative-name path)))
7252 (if (string-match (concat "^" (regexp-quote
7253 (file-name-as-directory
7254 (expand-file-name "."))))
7255 (expand-file-name path))
7256 ;; We are linking a file with relative path name.
7257 (setq path (substring (expand-file-name path)
7259 (setq path (abbreviate-file-name (expand-file-name path)))))))
7260 (setq link (concat "file:" path))
7261 (if (equal desc origpath)
7264 (if org-make-link-description-function
7265 (setq desc (funcall org-make-link-description-function link desc)))
7267 (setq desc (read-string "Description: " desc))
7268 (unless (string-match "\\S-" desc) (setq desc nil))
7269 (if remove (apply 'delete-region remove))
7270 (insert (org-make-link-string link desc))))
7272 (defun org-completing-read (&rest args)
7273 "Completing-read with SPACE being a normal character."
7274 (let ((minibuffer-local-completion-map
7275 (copy-keymap minibuffer-local-completion-map)))
7276 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
7277 (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
7278 (apply 'org-ido-completing-read args)))
7280 (defun org-completing-read-no-ido (&rest args)
7281 (let (org-completion-use-ido)
7282 (apply 'org-completing-read args)))
7284 (defun org-ido-completing-read (&rest args)
7285 "Completing-read using `ido-mode' speedups if available"
7286 (if (and org-completion-use-ido
7287 (fboundp 'ido-completing-read)
7288 (boundp 'ido-mode) ido-mode
7289 (listp (second args)))
7290 (let ((ido-enter-matching-directory nil))
7291 (apply 'ido-completing-read (concat (car args))
7292 (mapcar (lambda (x) (car x)) (nth 1 args))
7294 (apply 'completing-read args)))
7296 (defun org-extract-attributes (s)
7297 "Extract the attributes cookie from a string and set as text property."
7298 (let (a attr (start 0) key value)
7300 (when (string-match "{{\\([^}]+\\)}}$" s)
7301 (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
7302 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
7303 (setq key (match-string 1 a) value (match-string 2 a)
7305 attr (plist-put attr (intern key) value))))
7306 (org-add-props s nil 'org-attr attr))
7309 (defun org-extract-attributes-from-string (tag)
7310 (let (key value attr)
7311 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
7312 (setq key (match-string 1 tag) value (match-string 2 tag)
7313 tag (replace-match "" t t tag)
7314 attr (plist-put attr (intern key) value)))
7317 (defun org-attributes-to-string (plist)
7318 "Format a property list into an HTML attribute list."
7319 (let ((s "") key value)
7321 (setq key (pop plist) value (pop plist))
7323 (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
7326 ;;; Opening/following a link
7328 (defvar org-link-search-failed nil)
7330 (defun org-next-link ()
7331 "Move forward to the next link.
7332 If the link is in hidden text, expose it."
7334 (when (and org-link-search-failed (eq this-command last-command))
7335 (goto-char (point-min))
7336 (message "Link search wrapped back to beginning of buffer"))
7337 (setq org-link-search-failed nil)
7338 (let* ((pos (point))
7340 (a (assoc :link ct)))
7341 (if a (goto-char (nth 2 a)))
7342 (if (re-search-forward org-any-link-re nil t)
7344 (goto-char (match-beginning 0))
7345 (if (org-invisible-p) (org-show-context)))
7347 (setq org-link-search-failed t)
7348 (error "No further link found"))))
7350 (defun org-previous-link ()
7351 "Move backward to the previous link.
7352 If the link is in hidden text, expose it."
7354 (when (and org-link-search-failed (eq this-command last-command))
7355 (goto-char (point-max))
7356 (message "Link search wrapped back to end of buffer"))
7357 (setq org-link-search-failed nil)
7358 (let* ((pos (point))
7360 (a (assoc :link ct)))
7361 (if a (goto-char (nth 1 a)))
7362 (if (re-search-backward org-any-link-re nil t)
7364 (goto-char (match-beginning 0))
7365 (if (org-invisible-p) (org-show-context)))
7367 (setq org-link-search-failed t)
7368 (error "No further link found"))))
7370 (defun org-translate-link (s)
7371 "Translate a link string if a translation function has been defined."
7372 (if (and org-link-translation-function
7373 (fboundp org-link-translation-function)
7374 (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
7376 (setq s (funcall org-link-translation-function
7377 (match-string 1) (match-string 2)))
7378 (concat (car s) ":" (cdr s)))
7381 (defun org-translate-link-from-planner (type path)
7382 "Translate a link from Emacs Planner syntax so that Org can follow it.
7383 This is still an experimental function, your mileage may vary."
7385 ((member type '("http" "https" "news" "ftp"))
7386 ;; standard Internet links are the same.
7388 ((and (equal type "irc") (string-match "^//" path))
7389 ;; Planner has two / at the beginning of an irc link, we have 1.
7390 ;; We should have zero, actually....
7391 (setq path (substring path 1)))
7392 ((and (equal type "lisp") (string-match "^/" path))
7393 ;; Planner has a slash, we do not.
7394 (setq type "elisp" path (substring path 1)))
7395 ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
7396 ;; A typical message link. Planner has the id after the fina slash,
7397 ;; we separate it with a hash mark
7398 (setq path (concat (match-string 1 path) "#"
7399 (org-remove-angle-brackets (match-string 2 path)))))
7403 (defun org-find-file-at-mouse (ev)
7404 "Open file link or URL at mouse."
7406 (mouse-set-point ev)
7407 (org-open-at-point 'in-emacs))
7409 (defun org-open-at-mouse (ev)
7410 "Open file link or URL at mouse."
7412 (mouse-set-point ev)
7413 (if (eq major-mode 'org-agenda-mode)
7414 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
7415 (org-open-at-point))
7417 (defvar org-window-config-before-follow-link nil
7418 "The window configuration before following a link.
7419 This is saved in case the need arises to restore it.")
7421 (defvar org-open-link-marker (make-marker)
7422 "Marker pointing to the location where `org-open-at-point; was called.")
7425 (defun org-open-at-point-global ()
7426 "Follow a link like Org-mode does.
7427 This command can be called in any mode to follow a link that has
7430 (org-run-like-in-org-mode 'org-open-at-point))
7433 (defun org-open-link-from-string (s &optional arg)
7434 "Open a link in the string S, as if it was in Org-mode."
7435 (interactive "sLink: \nP")
7437 (let ((org-inhibit-startup t))
7440 (goto-char (point-min))
7441 (org-open-at-point arg))))
7443 (defun org-open-at-point (&optional in-emacs)
7444 "Open link at or after point.
7445 If there is no link at point, this function will search forward up to
7446 the end of the current subtree.
7447 Normally, files will be opened by an appropriate application. If the
7448 optional argument IN-EMACS is non-nil, Emacs will visit the file.
7449 With a double prefix argument, try to open outside of Emacs, in the
7450 application the system uses for this file type."
7452 (org-load-modules-maybe)
7453 (move-marker org-open-link-marker (point))
7454 (setq org-window-config-before-follow-link (current-window-configuration))
7455 (org-remove-occur-highlights nil nil t)
7457 ((org-at-timestamp-p t) (org-follow-timestamp-link))
7458 ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
7459 (org-footnote-action))
7461 (let (type path link line search (pos (point)))
7464 (skip-chars-forward "^]\n\r")
7465 (when (org-in-regexp org-bracket-link-regexp)
7466 (setq link (org-extract-attributes
7467 (org-link-unescape (org-match-string-no-properties 1))))
7468 (while (string-match " *\n *" link)
7469 (setq link (replace-match " " t t link)))
7470 (setq link (org-link-expand-abbrev link))
7472 ((or (file-name-absolute-p link)
7473 (string-match "^\\.\\.?/" link))
7474 (setq type "file" path link))
7475 ((string-match org-link-re-with-space3 link)
7476 (setq type (match-string 1 link) path (match-string 2 link)))
7477 (t (setq type "thisfile" path link)))
7480 (when (get-text-property (point) 'org-linked-text)
7481 (setq type "thisfile"
7482 pos (if (get-text-property (1+ (point)) 'org-linked-text)
7483 (1+ (point)) (point))
7484 path (buffer-substring
7485 (previous-single-property-change pos 'org-linked-text)
7486 (next-single-property-change pos 'org-linked-text)))
7490 (when (or (org-in-regexp org-angle-link-re)
7491 (org-in-regexp org-plain-link-re))
7492 (setq type (match-string 1) path (match-string 2))
7495 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
7497 path (match-string 1))
7498 (while (string-match ":" path)
7499 (setq path (replace-match "+" t t path)))
7501 (when (org-in-regexp "<\\([^><\n]+\\)>")
7502 (setq type "tree-match"
7503 path (match-string 1))
7506 (error "No link found"))
7507 ;; Remove any trailing spaces in path
7508 (if (string-match " +\\'" path)
7509 (setq path (replace-match "" t t path)))
7510 (if (and org-link-translation-function
7511 (fboundp org-link-translation-function))
7512 ;; Check if we need to translate the link
7513 (let ((tmp (funcall org-link-translation-function type path)))
7514 (setq type (car tmp) path (cdr tmp))))
7518 ((assoc type org-link-protocols)
7519 (funcall (nth 1 (assoc type org-link-protocols)) path))
7521 ((equal type "mailto")
7522 (let ((cmd (car org-link-mailto-program))
7523 (args (cdr org-link-mailto-program)) args1
7524 (address path) (subject "") a)
7525 (if (string-match "\\(.*\\)::\\(.*\\)" path)
7526 (setq address (match-string 1 path)
7527 subject (org-link-escape (match-string 2 path))))
7530 ((not (stringp (car args))) (push (pop args) args1))
7531 (t (setq a (pop args))
7532 (if (string-match "%a" a)
7533 (setq a (replace-match address t t a)))
7534 (if (string-match "%s" a)
7535 (setq a (replace-match subject t t a)))
7537 (apply cmd (nreverse args1))))
7539 ((member type '("http" "https" "ftp" "news"))
7540 (browse-url (concat type ":" (org-link-escape
7541 path org-link-escape-chars-browser))))
7543 ((member type '("message"))
7544 (browse-url (concat type ":" path)))
7546 ((string= type "tags")
7547 (org-tags-view in-emacs path))
7548 ((string= type "thisfile")
7550 (switch-to-buffer-other-window
7551 (org-get-buffer-for-internal-link (current-buffer)))
7552 (org-mark-ring-push))
7553 (let ((cmd `(org-link-search
7555 ,(cond ((equal in-emacs '(4)) 'occur)
7556 ((equal in-emacs '(16)) 'org-occur)
7559 (condition-case nil (eval cmd)
7560 (error (progn (widen) (eval cmd))))))
7562 ((string= type "tree-match")
7563 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
7565 ((string= type "file")
7566 (if (string-match "::\\([0-9]+\\)\\'" path)
7567 (setq line (string-to-number (match-string 1 path))
7568 path (substring path 0 (match-beginning 0)))
7569 (if (string-match "::\\(.+\\)\\'" path)
7570 (setq search (match-string 1 path)
7571 path (substring path 0 (match-beginning 0)))))
7572 (if (string-match "[*?{]" (file-name-nondirectory path))
7574 (org-open-file path in-emacs line search)))
7576 ((string= type "news")
7578 (org-gnus-follow-link path))
7580 ((string= type "shell")
7582 (if (or (not org-confirm-shell-link-function)
7583 (funcall org-confirm-shell-link-function
7584 (format "Execute \"%s\" in shell? "
7585 (org-add-props cmd nil
7586 'face 'org-warning))))
7588 (message "Executing %s" cmd)
7589 (shell-command cmd))
7592 ((string= type "elisp")
7594 (if (or (not org-confirm-elisp-link-function)
7595 (funcall org-confirm-elisp-link-function
7596 (format "Execute \"%s\" as elisp? "
7597 (org-add-props cmd nil
7598 'face 'org-warning))))
7599 (message "%s => %s" cmd
7600 (if (equal (string-to-char cmd) ?\()
7602 (call-interactively (read cmd))))
7606 (browse-url-at-point))))))
7607 (move-marker org-open-link-marker nil)
7608 (run-hook-with-args 'org-follow-link-hook))
7612 (defun org-get-effort (&optional pom)
7613 "Get the effort estimate for the current entry."
7614 (org-entry-get pom org-effort-property))
7618 (defvar org-create-file-search-functions nil
7619 "List of functions to construct the right search string for a file link.
7620 These functions are called in turn with point at the location to
7621 which the link should point.
7623 A function in the hook should first test if it would like to
7624 handle this file type, for example by checking the major-mode or
7625 the file extension. If it decides not to handle this file, it
7626 should just return nil to give other functions a chance. If it
7627 does handle the file, it must return the search string to be used
7628 when following the link. The search string will be part of the
7629 file link, given after a double colon, and `org-open-at-point'
7630 will automatically search for it. If special measures must be
7631 taken to make the search successful, another function should be
7632 added to the companion hook `org-execute-file-search-functions',
7635 A function in this hook may also use `setq' to set the variable
7636 `description' to provide a suggestion for the descriptive text to
7637 be used for this link when it gets inserted into an Org-mode
7638 buffer with \\[org-insert-link].")
7640 (defvar org-execute-file-search-functions nil
7641 "List of functions to execute a file search triggered by a link.
7643 Functions added to this hook must accept a single argument, the
7644 search string that was part of the file link, the part after the
7645 double colon. The function must first check if it would like to
7646 handle this search, for example by checking the major-mode or the
7647 file extension. If it decides not to handle this search, it
7648 should just return nil to give other functions a chance. If it
7649 does handle the search, it must return a non-nil value to keep
7650 other functions from trying.
7652 Each function can access the current prefix argument through the
7653 variable `current-prefix-argument'. Note that a single prefix is
7654 used to force opening a link in Emacs, so it may be good to only
7655 use a numeric or double prefix to guide the search function.
7657 In case this is needed, a function in this hook can also restore
7658 the window configuration before `org-open-at-point' was called using:
7660 (set-window-configuration org-window-config-before-follow-link)")
7662 (defun org-link-search (s &optional type avoid-pos)
7663 "Search for a link search option.
7664 If S is surrounded by forward slashes, it is interpreted as a
7665 regular expression. In org-mode files, this will create an `org-occur'
7666 sparse tree. In ordinary files, `occur' will be used to list matches.
7667 If the current buffer is in `dired-mode', grep will be used to search
7668 in all files. If AVOID-POS is given, ignore matches near that position."
7669 (let ((case-fold-search t)
7670 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
7671 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
7672 (append '(("") (" ") ("\t") ("\n"))
7676 (pre nil) (post nil)
7677 words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
7679 ;; First check if there are any special
7680 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
7681 ;; Now try the builtin stuff
7683 (goto-char (point-min))
7686 (concat "<<" (regexp-quote s0) ">>") nil t)
7687 (setq type 'dedicated
7688 pos (match-beginning 0))))
7689 ;; There is an exact target for this
7691 ((and (string-match "^(\\(.*\\))$" s0)
7693 (goto-char (point-min))
7696 (concat "[^[]" (regexp-quote
7697 (format org-coderef-label-format
7698 (match-string 1 s0))))
7700 (setq type 'dedicated
7701 pos (1+ (match-beginning 0))))))
7702 ;; There is a coderef target for this
7704 ((string-match "^/\\(.*\\)/$" s)
7705 ;; A regular expression
7708 (org-occur (match-string 1 s)))
7709 ;;((eq major-mode 'dired-mode)
7710 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
7711 (t (org-do-occur (match-string 1 s)))))
7713 ;; A normal search strings
7714 (when (equal (string-to-char s) ?*)
7715 ;; Anchor on headlines, post may include tags.
7716 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
7717 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
7719 (remove-text-properties
7721 '(face nil mouse-face nil keymap nil fontified nil) s)
7722 ;; Make a series of regular expressions to find a match
7723 (setq words (org-split-string s "[ \n\r\t]+")
7725 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
7726 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
7728 re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
7729 re2a (concat "[ \t\r\n]" re2a_)
7730 re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
7731 re4 (concat "[^a-zA-Z_]" re4_)
7733 re1 (concat pre re2 post)
7734 re3 (concat pre (if pre re4_ re4) post)
7735 re5 (concat pre ".*" re4)
7736 re2 (concat pre re2)
7737 re2a (concat pre (if pre re2a_ re2a))
7738 re4 (concat pre (if pre re4_ re4))
7739 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
7740 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
7744 ((eq type 'org-occur) (org-occur reall))
7745 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
7746 (t (goto-char (point-min))
7748 (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
7749 (org-search-not-self 1 re1 nil t)
7750 (org-search-not-self 1 re2 nil t)
7751 (org-search-not-self 1 re2a nil t)
7752 (org-search-not-self 1 re3 nil t)
7753 (org-search-not-self 1 re4 nil t)
7754 (org-search-not-self 1 re5 nil t)
7756 (goto-char (match-beginning 1))
7758 (error "No match")))))
7760 ;; Normal string-search
7761 (goto-char (point-min))
7762 (if (search-forward s nil t)
7763 (goto-char (match-beginning 0))
7764 (error "No match"))))
7765 (and (org-mode-p) (org-show-context 'link-search))
7768 (defun org-search-not-self (group &rest args)
7769 "Execute `re-search-forward', but only accept matches that do not
7770 enclose the position of `org-open-link-marker'."
7771 (let ((m org-open-link-marker))
7773 (while (apply 're-search-forward args)
7774 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
7775 (goto-char (match-end group))
7776 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
7777 (> (match-beginning 0) (marker-position m))
7778 (< (match-end 0) (marker-position m)))
7780 (or (not (org-in-regexp
7781 org-bracket-link-analytic-regexp 1))
7782 (not (match-end 4)) ; no description
7783 (and (<= (match-beginning 4) (point))
7784 (>= (match-end 4) (point))))))
7785 (throw 'exit (point))))))))
7787 (defun org-get-buffer-for-internal-link (buffer)
7788 "Return a buffer to be used for displaying the link target of internal links."
7790 ((not org-display-internal-link-with-indirect-buffer)
7792 ((string-match "(Clone)$" (buffer-name buffer))
7793 (message "Buffer is already a clone, not making another one")
7794 ;; we also do not modify visibility in this case
7796 (t ; make a new indirect buffer for displaying the link
7797 (let* ((bn (buffer-name buffer))
7798 (ibn (concat bn "(Clone)"))
7799 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
7800 (with-current-buffer ib (org-overview))
7803 (defun org-do-occur (regexp &optional cleanup)
7804 "Call the Emacs command `occur'.
7805 If CLEANUP is non-nil, remove the printout of the regular expression
7806 in the *Occur* buffer. This is useful if the regex is long and not useful
7810 (let ((cwin (selected-window)) win beg end)
7811 (when (setq win (get-buffer-window "*Occur*"))
7812 (select-window win))
7813 (goto-char (point-min))
7814 (when (re-search-forward "match[a-z]+" nil t)
7815 (setq beg (match-end 0))
7816 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
7817 (setq end (1- (match-beginning 0)))))
7818 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
7819 (goto-char (point-min))
7820 (select-window cwin))))
7822 ;;; The mark ring for links jumps
7824 (defvar org-mark-ring nil
7825 "Mark ring for positions before jumps in Org-mode.")
7826 (defvar org-mark-ring-last-goto nil
7827 "Last position in the mark ring used to go back.")
7828 ;; Fill and close the ring
7829 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
7830 (loop for i from 1 to org-mark-ring-length do
7831 (push (make-marker) org-mark-ring))
7832 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
7835 (defun org-mark-ring-push (&optional pos buffer)
7836 "Put the current position or POS into the mark ring and rotate it."
7838 (setq pos (or pos (point)))
7839 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
7840 (move-marker (car org-mark-ring)
7842 (or buffer (current-buffer)))
7844 (substitute-command-keys
7845 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
7847 (defun org-mark-ring-goto (&optional n)
7848 "Jump to the previous position in the mark ring.
7849 With prefix arg N, jump back that many stored positions. When
7850 called several times in succession, walk through the entire ring.
7851 Org-mode commands jumping to a different position in the current file,
7852 or to another Org-mode file, automatically push the old position
7856 (if (eq last-command this-command)
7857 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
7858 (setq p org-mark-ring))
7859 (setq org-mark-ring-last-goto p)
7861 (switch-to-buffer (marker-buffer m))
7863 (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
7865 (defun org-remove-angle-brackets (s)
7866 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
7867 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
7869 (defun org-add-angle-brackets (s)
7870 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
7871 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
7873 (defun org-remove-double-quotes (s)
7874 (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
7875 (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
7878 ;;; Following specific links
7880 (defun org-follow-timestamp-link ()
7882 ((org-at-date-range-p t)
7883 (let ((org-agenda-start-on-weekday)
7884 (t1 (match-string 1))
7885 (t2 (match-string 2)))
7886 (setq t1 (time-to-days (org-time-string-to-time t1))
7887 t2 (time-to-days (org-time-string-to-time t2)))
7888 (org-agenda-list nil t1 (1+ (- t2 t1)))))
7889 ((org-at-timestamp-p t)
7890 (org-agenda-list nil (time-to-days (org-time-string-to-time
7891 (substring (match-string 1) 0 10)))
7893 (t (error "This should not happen"))))
7896 ;;; Following file links
7897 (defvar org-wait nil)
7898 (defun org-open-file (path &optional in-emacs line search)
7899 "Open the file at PATH.
7900 First, this expands any special file name abbreviations. Then the
7901 configuration variable `org-file-apps' is checked if it contains an
7902 entry for this file type, and if yes, the corresponding command is launched.
7904 If no application is found, Emacs simply visits the file.
7906 With optional prefix argument IN-EMACS, Emacs will visit the file.
7907 With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
7908 and o use an external application to visit the file.
7910 Optional LINE specifies a line to go to, optional SEARCH a string to
7911 search for. If LINE or SEARCH is given, the file will always be
7913 If the file does not exist, an error is thrown."
7914 (setq in-emacs (or in-emacs line search))
7915 (let* ((file (if (equal path "")
7917 (substitute-in-file-name (expand-file-name path))))
7918 (apps (append org-file-apps (org-default-apps)))
7919 (remp (and (assq 'remote apps) (org-file-remote-p file)))
7920 (dirp (if remp nil (file-directory-p file)))
7921 (file (if (and dirp org-open-directory-means-index-dot-org)
7922 (concat (file-name-as-directory file) "index.org")
7924 (a-m-a-p (assq 'auto-mode apps))
7925 (dfile (downcase file))
7926 (old-buffer (current-buffer))
7928 (old-mode major-mode)
7930 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
7931 (setq ext (match-string 1 dfile))
7932 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
7933 (setq ext (match-string 1 dfile))))
7935 ((equal in-emacs '(16))
7936 (setq cmd (cdr (assoc 'system apps))))
7937 (in-emacs (setq cmd 'emacs))
7939 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
7940 (and dirp (cdr (assoc 'directory apps)))
7941 (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
7943 (cdr (assoc ext apps))
7944 (cdr (assoc t apps))))))
7945 (when (eq cmd 'system)
7946 (setq cmd (cdr (assoc 'system apps))))
7947 (when (eq cmd 'default)
7948 (setq cmd (cdr (assoc t apps))))
7949 (when (eq cmd 'mailcap)
7951 (mailcap-parse-mailcaps)
7952 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
7953 (command (mailcap-mime-info mime-type)))
7954 (if (stringp command)
7956 (setq cmd 'emacs))))
7957 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
7958 (not (file-exists-p file))
7959 (not org-open-non-existing-files))
7960 (error "No such file: %s" file))
7962 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
7963 ;; Remove quotes around the file name - we'll use shell-quote-argument.
7964 (while (string-match "['\"]%s['\"]" cmd)
7965 (setq cmd (replace-match "%s" t t cmd)))
7966 (while (string-match "%s" cmd)
7967 (setq cmd (replace-match
7969 (shell-quote-argument
7970 (convert-standard-filename file)))
7972 (save-window-excursion
7973 (start-process-shell-command cmd nil cmd)
7974 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
7978 (funcall (cdr (assq 'file org-link-frame-setup)) file)
7980 (if line (goto-line line)
7981 (if search (org-link-search search))))
7983 (let ((file (convert-standard-filename file)))
7985 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
7986 (and (org-mode-p) (eq old-mode 'org-mode)
7987 (or (not (equal old-buffer (current-buffer)))
7988 (not (equal old-pos (point))))
7989 (org-mark-ring-push old-pos old-buffer))))
7991 (defun org-default-apps ()
7992 "Return the default applications for this operating system."
7994 ((eq system-type 'darwin)
7995 org-file-apps-defaults-macosx)
7996 ((eq system-type 'windows-nt)
7997 org-file-apps-defaults-windowsnt)
7998 (t org-file-apps-defaults-gnu)))
8000 (defun org-apps-regexp-alist (list &optional add-auto-mode)
8001 "Convert extensions to regular expressions in the cars of LIST.
8002 Also, weed out any non-string entries, because the return value is used
8003 only for regexp matching.
8004 When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
8005 point to the symbol `emacs', indicating that the file should
8006 be opened in Emacs."
8010 (if (not (stringp (car x)))
8012 (if (string-match "\\W" (car x))
8014 (cons (concat "\\." (car x) "\\'") (cdr x)))))
8017 (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
8019 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
8020 (defun org-file-remote-p (file)
8021 "Test whether FILE specifies a location on a remote system.
8022 Return non-nil if the location is indeed remote.
8024 For example, the filename \"/user@host:/foo\" specifies a location
8025 on the system \"/user@host:\"."
8026 (cond ((fboundp 'file-remote-p)
8027 (file-remote-p file))
8028 ((fboundp 'tramp-handle-file-remote-p)
8029 (tramp-handle-file-remote-p file))
8030 ((and (boundp 'ange-ftp-name-format)
8031 (string-match (car ange-ftp-name-format) file))
8038 (defun org-get-org-file ()
8039 "Read a filename, with default directory `org-directory'."
8040 (let ((default (or org-default-notes-file remember-data-file)))
8041 (read-file-name (format "File name [%s]: " default)
8042 (file-name-as-directory org-directory)
8045 (defun org-notes-order-reversed-p ()
8046 "Check if the current file should receive notes in reversed order."
8048 ((not org-reverse-note-order) nil)
8049 ((eq t org-reverse-note-order) t)
8050 ((not (listp org-reverse-note-order)) nil)
8052 (let ((all org-reverse-note-order)
8054 (while (setq entry (pop all))
8055 (if (string-match (car entry) buffer-file-name)
8056 (throw 'exit (cdr entry))))
8059 (defvar org-refile-target-table nil
8060 "The list of refile targets, created by `org-refile'.")
8062 (defvar org-agenda-new-buffers nil
8063 "Buffers created to visit agenda files.")
8065 (defun org-get-refile-targets (&optional default-buffer)
8066 "Produce a table with refile targets."
8067 (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
8068 targets txt re files f desc descre fast-path-p level)
8069 (message "Getting targets...")
8070 (with-current-buffer (or default-buffer (current-buffer))
8071 (while (setq entry (pop entries))
8072 (setq files (car entry) desc (cdr entry))
8073 (setq fast-path-p nil)
8075 ((null files) (setq files (list (current-buffer))))
8076 ((eq files 'org-agenda-files)
8077 (setq files (org-agenda-files 'unrestricted)))
8078 ((and (symbolp files) (fboundp files))
8079 (setq files (funcall files)))
8080 ((and (symbolp files) (boundp files))
8081 (setq files (symbol-value files))))
8082 (if (stringp files) (setq files (list files)))
8084 ((eq (car desc) :tag)
8085 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
8086 ((eq (car desc) :todo)
8087 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
8088 ((eq (car desc) :regexp)
8089 (setq descre (cdr desc)))
8090 ((eq (car desc) :level)
8091 (setq descre (concat "^\\*\\{" (number-to-string
8092 (if org-odd-levels-only
8093 (1- (* 2 (cdr desc)))
8096 ((eq (car desc) :maxlevel)
8097 (setq fast-path-p t)
8098 (setq descre (concat "^\\*\\{1," (number-to-string
8099 (if org-odd-levels-only
8100 (1- (* 2 (cdr desc)))
8103 (t (error "Bad refiling target description %s" desc)))
8104 (while (setq f (pop files))
8106 (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
8107 (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
8108 (setq f (expand-file-name f))
8112 (goto-char (point-min))
8113 (while (re-search-forward descre nil t)
8114 (goto-char (point-at-bol))
8115 (when (looking-at org-complex-heading-regexp)
8116 (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
8117 txt (org-link-display-format (match-string 4))
8118 re (concat "^" (regexp-quote
8119 (buffer-substring (match-beginning 1)
8121 (if (match-end 5) (setq re (concat re "[ \t]+"
8123 (match-string 5)))))
8124 (setq re (concat re "[ \t]*$"))
8125 (when org-refile-use-outline-path
8126 (setq txt (mapconcat 'org-protect-slash
8128 (if (eq org-refile-use-outline-path 'file)
8129 (list (file-name-nondirectory
8130 (buffer-file-name (buffer-base-buffer))))
8131 (if (eq org-refile-use-outline-path 'full-file-path)
8132 (list (buffer-file-name (buffer-base-buffer)))))
8133 (org-get-outline-path fast-path-p level txt)
8136 (push (list txt f re (point)) targets))
8137 (goto-char (point-at-eol))))))))
8138 (message "Getting targets...done")
8139 (nreverse targets))))
8141 (defun org-protect-slash (s)
8142 (while (string-match "/" s)
8143 (setq s (replace-match "\\" t t s)))
8146 (defvar org-olpa (make-vector 20 nil))
8148 (defun org-get-outline-path (&optional fastp level heading)
8149 "Return the outline path to the current entry, as a list."
8153 (error "Outline path failure, more than 19 levels."))
8154 (loop for i from level upto 19 do
8155 (aset org-olpa i nil))
8157 (delq nil (append org-olpa nil))
8158 (aset org-olpa level heading)))
8161 (while (org-up-heading-safe)
8162 (when (looking-at org-complex-heading-regexp)
8163 (push (org-match-string-no-properties 4) rtn)))
8166 (defvar org-refile-history nil
8167 "History for refiling operations.")
8169 (defun org-refile (&optional goto default-buffer)
8170 "Move the entry at point to another heading.
8171 The list of target headings is compiled using the information in
8172 `org-refile-targets', which see. This list is created before each use
8173 and will therefore always be up-to-date.
8175 At the target location, the entry is filed as a subitem of the target heading.
8176 Depending on `org-reverse-note-order', the new subitem will either be the
8177 first or the last subitem.
8179 If there is an active region, all entries in that region will be moved.
8180 However, the region must fulfil the requirement that the first heading
8181 is the first one sets the top-level of the moved text - at most siblings
8182 below it are allowed.
8184 With prefix arg GOTO, the command will only visit the target location,
8185 not actually move anything.
8186 With a double prefix `C-u C-u', go to the location where the last refiling
8187 operation has put the subtree."
8189 (let* ((cbuf (current-buffer))
8190 (regionp (org-region-active-p))
8191 (region-start (and regionp (region-beginning)))
8192 (region-end (and regionp (region-end)))
8193 (region-length (and regionp (- region-end region-start)))
8194 (filename (buffer-file-name (buffer-base-buffer cbuf)))
8195 pos it nbuf file re level reversed)
8196 (when regionp (goto-char region-start)
8197 (unless (org-kill-is-subtree-p
8198 (buffer-substring region-start region-end))
8199 (error "The region is not a (sequence of) subtree(s)")))
8200 (if (equal goto '(16))
8201 (org-refile-goto-last-stored)
8202 (when (setq it (org-refile-get-location
8203 (if goto "Goto: " "Refile to: ") default-buffer))
8204 (setq file (nth 1 it)
8208 (equal (buffer-file-name) file)
8210 (and (>= pos region-start)
8211 (<= pos region-end))
8212 (and (>= pos (point))
8213 (< pos (save-excursion
8214 (org-end-of-subtree t t))))))
8215 (error "Cannot refile to position inside the tree or region"))
8217 (setq nbuf (or (find-buffer-visiting file)
8218 (find-file-noselect file)))
8221 (switch-to-buffer nbuf)
8223 (org-show-context 'org-goto))
8226 (kill-new (buffer-substring region-start region-end))
8227 (org-save-markers-in-region region-start region-end))
8228 (org-copy-subtree 1 nil t))
8230 (set-buffer (setq nbuf (or (find-buffer-visiting file)
8231 (find-file-noselect file))))
8232 (setq reversed (org-notes-order-reversed-p))
8237 (looking-at outline-regexp)
8238 (setq level (org-get-valid-level (funcall outline-level) 1))
8241 (or (outline-next-heading) (point-max))
8242 (or (save-excursion (outline-get-next-sibling))
8243 (org-end-of-subtree t t)
8245 (if (not (bolp)) (newline))
8246 (bookmark-set "org-refile-last-stored")
8247 (org-paste-subtree level))))
8249 (delete-region (point) (+ (point) region-length))
8251 (setq org-markers-to-move nil)
8252 (message "Refiled to \"%s\"" (car it)))))))
8254 (defun org-refile-goto-last-stored ()
8255 "Go to the location where the last refile was stored."
8257 (bookmark-jump "org-refile-last-stored")
8258 (message "This is the location of the last refile"))
8260 (defun org-refile-get-location (&optional prompt default-buffer)
8261 "Prompt the user for a refile location, using PROMPT."
8262 (let ((org-refile-targets org-refile-targets)
8263 (org-refile-use-outline-path org-refile-use-outline-path))
8264 (setq org-refile-target-table (org-get-refile-targets default-buffer)))
8265 (unless org-refile-target-table
8266 (error "No refile targets"))
8267 (let* ((cbuf (current-buffer))
8268 (partial-completion-mode nil)
8269 (cfn (buffer-file-name (buffer-base-buffer cbuf)))
8270 (cfunc (if (and org-refile-use-outline-path
8271 org-outline-path-complete-in-steps)
8272 'org-olpath-completing-read
8273 'org-ido-completing-read))
8274 (extra (if org-refile-use-outline-path "/" ""))
8275 (filename (and cfn (expand-file-name cfn)))
8278 (if (not (equal filename (nth 1 x)))
8279 (cons (concat (car x) extra " ("
8280 (file-name-nondirectory (nth 1 x)) ")")
8282 (cons (concat (car x) extra) (cdr x))))
8283 org-refile-target-table))
8284 (completion-ignore-case t))
8285 (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
8288 (defun org-olpath-completing-read (prompt collection &rest args)
8289 "Read an outline path like a file name."
8290 (let ((thetable collection)
8291 (org-completion-use-ido nil)) ; does not work with ido.
8293 'org-ido-completing-read prompt
8294 (lambda (string predicate &optional flag)
8295 (let (rtn r f (l (length string)))
8299 (try-completion string thetable))
8302 (setq rtn (all-completions string thetable predicate))
8305 (setq r (substring x l))
8306 (if (string-match " ([^)]*)$" x)
8307 (setq f (match-string 0 x))
8309 (if (string-match "/" r)
8310 (concat string (substring r 0 (match-end 0)) f)
8315 (assoc string thetable)))
8321 (defun org-find-dblock (name)
8322 "Find the first dynamic block with name NAME in the buffer.
8323 If not found, stay at current position and return nil."
8326 (goto-char (point-min))
8327 (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
8329 (match-beginning 0))))
8330 (if pos (goto-char pos))
8333 (defconst org-dblock-start-re
8334 "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
8335 "Matches the startline of a dynamic block, with parameters.")
8337 (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
8338 "Matches the end of a dynamic block.")
8340 (defun org-create-dblock (plist)
8341 "Create a dynamic block section, with parameters taken from PLIST.
8342 PLIST must contain a :name entry which is used as name of the block."
8343 (unless (bolp) (newline))
8344 (let ((name (plist-get plist :name)))
8345 (insert "#+BEGIN: " name)
8347 (if (eq (car plist) :name)
8348 (setq plist (cddr plist))
8349 (insert " " (prin1-to-string (pop plist)))))
8350 (insert "\n\n#+END:\n")
8351 (beginning-of-line -2)))
8353 (defun org-prepare-dblock ()
8354 "Prepare dynamic block for refresh.
8355 This empties the block, puts the cursor at the insert position and returns
8356 the property list including an extra property :name with the block name."
8357 (unless (looking-at org-dblock-start-re)
8358 (error "Not at a dynamic block"))
8359 (let* ((begdel (1+ (match-end 0)))
8360 (name (org-no-properties (match-string 1)))
8361 (params (append (list :name name)
8362 (read (concat "(" (match-string 3) ")")))))
8363 (unless (re-search-forward org-dblock-end-re nil t)
8364 (error "Dynamic block not terminated"))
8367 (list :content (buffer-substring
8368 begdel (match-beginning 0)))))
8369 (delete-region begdel (match-beginning 0))
8374 (defun org-map-dblocks (&optional command)
8375 "Apply COMMAND to all dynamic blocks in the current buffer.
8376 If COMMAND is not given, use `org-update-dblock'."
8377 (let ((cmd (or command 'org-update-dblock))
8380 (goto-char (point-min))
8381 (while (re-search-forward org-dblock-start-re nil t)
8382 (goto-char (setq pos (match-beginning 0)))
8385 (error (message "Error during update of dynamic block")))
8387 (unless (re-search-forward org-dblock-end-re nil t)
8388 (error "Dynamic block not terminated"))))))
8390 (defun org-dblock-update (&optional arg)
8391 "User command for updating dynamic blocks.
8392 Update the dynamic block at point. With prefix ARG, update all dynamic
8393 blocks in the buffer."
8396 (org-update-all-dblocks)
8397 (or (looking-at org-dblock-start-re)
8398 (org-beginning-of-dblock))
8399 (org-update-dblock)))
8401 (defun org-update-dblock ()
8402 "Update the dynamic block at point
8403 This means to empty the block, parse for parameters and then call
8404 the correct writing function."
8405 (save-window-excursion
8406 (let* ((pos (point))
8407 (line (org-current-line))
8408 (params (org-prepare-dblock))
8409 (name (plist-get params :name))
8410 (cmd (intern (concat "org-dblock-write:" name))))
8411 (message "Updating dynamic block `%s' at line %d..." name line)
8412 (funcall cmd params)
8413 (message "Updating dynamic block `%s' at line %d...done" name line)
8416 (defun org-beginning-of-dblock ()
8417 "Find the beginning of the dynamic block at point.
8418 Error if there is no such block at point."
8422 (if (and (re-search-backward org-dblock-start-re nil t)
8423 (setq beg (match-beginning 0))
8424 (re-search-forward org-dblock-end-re nil t)
8425 (> (match-end 0) pos))
8428 (error "Not in a dynamic block"))))
8430 (defun org-update-all-dblocks ()
8431 "Update all dynamic blocks in the buffer.
8432 This function can be used in a hook."
8434 (org-map-dblocks 'org-update-dblock)))
8439 (defconst org-additional-option-like-keywords
8440 '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
8441 "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
8442 "BEGIN_EXAMPLE" "END_EXAMPLE"
8443 "BEGIN_QUOTE" "END_QUOTE"
8444 "BEGIN_VERSE" "END_VERSE"
8445 "BEGIN_CENTER" "END_CENTER"
8446 "BEGIN_SRC" "END_SRC"
8447 "CATEGORY" "COLUMNS"
8448 "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
8450 (defcustom org-structure-template-alist
8452 ("s" "#+begin_src ?\n\n#+end_src"
8453 "<src lang=\"?\">\n\n</src>")
8454 ("e" "#+begin_example\n?\n#+end_example"
8455 "<example>\n?\n</example>")
8456 ("q" "#+begin_quote\n?\n#+end_quote"
8457 "<quote>\n?\n</quote>")
8458 ("v" "#+begin_verse\n?\n#+end_verse"
8459 "<verse>\n?\n/verse>")
8460 ("c" "#+begin_center\n?\n#+end_center"
8461 "<center>\n?\n/center>")
8462 ("l" "#+begin_latex\n?\n#+end_latex"
8463 "<literal style=\"latex\">\n?\n</literal>")
8465 "<literal style=\"latex\">?</literal>")
8466 ("h" "#+begin_html\n?\n#+end_html"
8467 "<literal style=\"html\">\n?\n</literal>")
8469 "<literal style=\"html\">?</literal>")
8470 ("a" "#+begin_ascii\n?\n#+end_ascii")
8472 ("i" "#+include %file ?"
8473 "<include file=%file markup=\"?\">")
8475 "Structure completion elements.
8476 This is a list of abbreviation keys and values. The value gets inserted
8477 it you type @samp{.} followed by the key and then the completion key,
8478 usually `M-TAB'. %file will be replaced by a file name after prompting
8479 for the file using completion.
8480 There are two templates for each key, the first uses the original Org syntax,
8481 the second uses Emacs Muse-like syntax tags. These Muse-like tags become
8482 the default when the /org-mtags.el/ module has been loaded. See also the
8483 variable `org-mtags-prefer-muse-templates'.
8484 This is an experimental feature, it is undecided if it is going to stay in."
8485 :group 'org-completion
8488 (string :tag "Template")
8489 (string :tag "Muse Template")))
8491 (defun org-try-structure-completion ()
8492 "Try to complete a structure template before point.
8493 This looks for strings like \"<e\" on an otherwise empty line and
8495 (let ((l (buffer-substring (point-at-bol) (point)))
8497 (when (and (looking-at "[ \t]*$")
8498 (string-match "^[ \t]*<\\([a-z]+\\)$"l)
8499 (setq a (assoc (match-string 1 l) org-structure-template-alist)))
8500 (org-complete-expand-structure-template (+ -1 (point-at-bol)
8501 (match-beginning 1)) a)
8504 (defun org-complete-expand-structure-template (start cell)
8505 "Expand a structure template."
8506 (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
8507 (rpl (nth (if musep 2 1) cell)))
8508 (delete-region start (point))
8509 (when (string-match "\\`#\\+" rpl)
8512 ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
8513 (delete-region (point-at-bol) (point)))
8515 (setq start (point))
8516 (if (string-match "%file" rpl)
8517 (setq rpl (replace-match
8521 (abbreviate-file-name (read-file-name "Include file: ")))
8525 (if (re-search-backward "\\?" start t) (delete-char 1))))
8528 (defun org-complete (&optional arg)
8529 "Perform completion on word at point.
8530 At the beginning of a headline, this completes TODO keywords as given in
8531 `org-todo-keywords'.
8532 If the current word is preceded by a backslash, completes the TeX symbols
8533 that are supported for HTML support.
8534 If the current word is preceded by \"#+\", completes special words for
8535 setting file options.
8536 In the line after \"#+STARTUP:, complete valid keywords.\"
8537 At all other locations, this simply calls the value of
8538 `org-completion-fallback-command'."
8540 (org-without-partial-completion
8544 (beg1 (save-excursion
8545 (skip-chars-backward (org-re "[:alnum:]_@"))
8547 (beg (save-excursion
8548 (skip-chars-backward "a-zA-Z0-9_:$")
8550 (confirm (lambda (x) (stringp (car x))))
8551 (searchhead (equal (char-before beg) ?*))
8553 (when (and (member (char-before beg1) '(?. ?<))
8554 (setq a (assoc (buffer-substring beg1 (point))
8555 org-structure-template-alist)))
8556 (org-complete-expand-structure-template (1- beg1) a)
8558 (tag (and (equal (char-before beg1) ?:)
8559 (equal (char-after (point-at-bol)) ?*)))
8560 (prop (and (equal (char-before beg1) ?:)
8561 (not (equal (char-after (point-at-bol)) ?*))))
8562 (texp (equal (char-before beg) ?\\))
8563 (link (equal (char-before beg) ?\[))
8564 (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
8567 (startup (string-match "^#\\+STARTUP:.*"
8568 (buffer-substring (point-at-bol) (point))))
8569 (completion-ignore-case opt)
8579 (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
8580 (cons (match-string 2 x) (match-string 1 x)))
8581 (org-split-string (org-get-current-options) "\n"))
8582 (mapcar 'list org-additional-option-like-keywords)))
8584 (setq type :startup)
8585 org-startup-options)
8586 (link (append org-link-abbrev-alist-local
8587 org-link-abbrev-alist))
8591 ((string-match "\\`\\*+[ \t]+\\'"
8592 (buffer-substring (point-at-bol) beg))
8594 (mapcar 'list org-todo-keywords-1))
8596 (setq type :searchhead)
8598 (goto-char (point-min))
8599 (while (re-search-forward org-todo-line-regexp nil t)
8601 (org-make-org-heading-search-string
8602 (match-string 3) t))
8605 (tag (setq type :tag beg beg1)
8606 (or org-tag-alist (org-get-buffer-tags)))
8607 (prop (setq type :prop beg beg1)
8608 (mapcar 'list (org-buffer-property-keys nil t t)))
8610 (call-interactively org-completion-fallback-command)
8611 (throw 'exit nil)))))
8612 (pattern (buffer-substring-no-properties beg end))
8613 (completion (try-completion pattern table confirm)))
8614 (cond ((eq completion t)
8615 (if (not (assoc (upcase pattern) table))
8616 (message "Already complete")
8617 (if (and (equal type :opt)
8618 (not (member (car (assoc (upcase pattern) table))
8619 org-additional-option-like-keywords)))
8620 (insert (substring (cdr (assoc (upcase pattern) table))
8622 (if (memq type '(:tag :prop)) (insert ":")))))
8624 (message "Can't find completion for \"%s\"" pattern)
8626 ((not (string= pattern completion))
8627 (delete-region beg end)
8628 (if (string-match " +$" completion)
8629 (setq completion (replace-match "" t t completion)))
8631 (if (get-buffer-window "*Completions*")
8632 (delete-window (get-buffer-window "*Completions*")))
8633 (if (assoc completion table)
8634 (if (eq type :todo) (insert " ")
8635 (if (memq type '(:tag :prop)) (insert ":"))))
8636 (if (and (equal type :opt) (assoc completion table))
8637 (message "%s" (substitute-command-keys
8638 "Press \\[org-complete] again to insert example settings"))))
8640 (message "Making completion list...")
8641 (let ((list (sort (all-completions pattern table confirm)
8643 (with-output-to-temp-buffer "*Completions*"
8645 ;; Protection needed for XEmacs and emacs 21
8646 (display-completion-list list pattern)
8647 (error (display-completion-list list)))))
8648 (message "Making completion list...%s" "done")))))))
8650 ;;;; TODO, DEADLINE, Comments
8652 (defun org-toggle-comment ()
8653 "Change the COMMENT state of an entry."
8656 (org-back-to-heading)
8657 (let (case-fold-search)
8658 (if (looking-at (concat outline-regexp
8659 "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
8660 (replace-match "" t t nil 1)
8661 (if (looking-at outline-regexp)
8663 (goto-char (match-end 0))
8664 (insert org-comment-string " ")))))))
8666 (defvar org-last-todo-state-is-todo nil
8667 "This is non-nil when the last TODO state change led to a TODO state.
8668 If the last change removed the TODO tag or switched to DONE, then
8671 (defvar org-setting-tags nil) ; dynamically skipped
8673 (defun org-parse-local-options (string var)
8674 "Parse STRING for startup setting relevant for variable VAR."
8675 (let ((rtn (symbol-value var))
8678 (if (or (not string) (not (string-match "\\S-" string)))
8680 (setq opts (delq nil (mapcar (lambda (x)
8681 (setq e (assoc x org-startup-options))
8682 (if (eq (nth 1 e) var) e nil))
8683 (org-split-string string "[ \t]+"))))
8687 (while (setq e (pop opts))
8689 (setq rtn (nth 2 e))
8690 (if (not (listp rtn)) (setq rtn nil))
8691 (push (nth 2 e) rtn)))
8694 (defvar org-todo-setup-filter-hook nil
8695 "Hook for functions that pre-filter todo specs.
8697 Each function takes a todo spec and returns either `nil' or the spec
8698 transformed into canonical form." )
8700 (defvar org-todo-get-default-hook nil
8701 "Hook for functions that get a default item for todo.
8703 Each function takes arguments (NEW-MARK OLD-MARK) and returns either
8704 `nil' or a string to be used for the todo mark." )
8706 (defvar org-agenda-headline-snapshot-before-repeat)
8707 (defun org-todo (&optional arg)
8708 "Change the TODO state of an item.
8709 The state of an item is given by a keyword at the start of the heading,
8711 *** TODO Write paper
8714 The different keywords are specified in the variable `org-todo-keywords'.
8715 By default the available states are \"TODO\" and \"DONE\".
8716 So for this example: when the item starts with TODO, it is changed to DONE.
8717 When it starts with DONE, the DONE is removed. And when neither TODO nor
8718 DONE are present, add TODO at the beginning of the heading.
8720 With C-u prefix arg, use completion to determine the new state.
8721 With numeric prefix arg, switch to that state.
8722 With a double C-u prefix, switch to the next set of TODO keywords (nextset).
8723 With a tripple C-u prefix, circumvent any state blocking.
8725 For calling through lisp, arg is also interpreted in the following way:
8726 'none -> empty state
8727 \"\"(empty string) -> switch to empty state
8728 'done -> switch to DONE
8729 'nextset -> switch to the next set of keywords
8730 'previousset -> switch to the previous set of keywords
8731 \"WAITING\" -> switch to the specified keyword, but only if it
8732 really is a member of `org-todo-keywords'."
8734 (if (equal arg '(16)) (setq arg 'nextset))
8735 (let ((org-blocker-hook org-blocker-hook))
8736 (when (equal arg '(64))
8737 (setq arg nil org-blocker-hook nil))
8740 (org-back-to-heading)
8741 (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
8742 (or (looking-at (concat " +" org-todo-regexp " *"))
8744 (let* ((match-data (match-data))
8745 (startpos (point-at-bol))
8746 (logging (save-match-data (org-entry-get nil "LOGGING" t)))
8747 (org-log-done org-log-done)
8748 (org-log-repeat org-log-repeat)
8749 (org-todo-log-states org-todo-log-states)
8750 (this (match-string 1))
8751 (hl-pos (match-beginning 0))
8752 (head (org-get-todo-sequence-head this))
8753 (ass (assoc head org-todo-kwd-alist))
8754 (interpret (nth 1 ass))
8755 (done-word (nth 3 ass))
8756 (final-done-word (nth 4 ass))
8757 (last-state (or this ""))
8758 (completion-ignore-case t)
8759 (member (member this org-todo-keywords-1))
8762 ((and org-todo-key-trigger
8763 (or (and (equal arg '(4))
8764 (eq org-use-fast-todo-selection 'prefix))
8765 (and (not arg) org-use-fast-todo-selection
8766 (not (eq org-use-fast-todo-selection
8768 ;; Use fast selection
8769 (org-fast-todo-selection))
8770 ((and (equal arg '(4))
8771 (or (not org-use-fast-todo-selection)
8772 (not org-todo-key-trigger)))
8773 ;; Read a state with completion
8774 (org-ido-completing-read
8775 "State: " (mapcar (lambda(x) (list x))
8776 org-todo-keywords-1)
8780 (if tail (car tail) nil)
8781 (car org-todo-keywords-1)))
8783 (if (equal member org-todo-keywords-1)
8786 (nth (- (length org-todo-keywords-1)
8788 org-todo-keywords-1)
8789 (org-last org-todo-keywords-1))))
8790 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
8791 (setq arg nil))) ; hack to fall back to cycling
8793 ;; user or caller requests a specific state
8795 ((equal arg "") nil)
8796 ((eq arg 'none) nil)
8797 ((eq arg 'done) (or done-word (car org-done-keywords)))
8799 (or (car (cdr (member head org-todo-heads)))
8800 (car org-todo-heads)))
8801 ((eq arg 'previousset)
8802 (let ((org-todo-heads (reverse org-todo-heads)))
8803 (or (car (cdr (member head org-todo-heads)))
8804 (car org-todo-heads))))
8805 ((car (member arg org-todo-keywords-1)))
8806 ((nth (1- (prefix-numeric-value arg))
8807 org-todo-keywords-1))))
8808 ((null member) (or head (car org-todo-keywords-1)))
8809 ((equal this final-done-word) nil) ;; -> make empty
8810 ((null tail) nil) ;; -> first entry
8811 ((memq interpret '(type priority))
8812 (if (eq this-command last-command)
8814 (if (> (length tail) 0)
8815 (or done-word (car org-done-keywords))
8820 (run-hook-with-args-until-success
8821 'org-todo-get-default-hook state last-state)
8823 (next (if state (concat " " state " ") " "))
8824 (change-plist (list :type 'todo-state-change :from this :to state
8825 :position startpos))
8827 (when org-blocker-hook
8828 (setq org-last-todo-state-is-todo
8829 (not (member this org-done-keywords)))
8830 (unless (save-excursion
8832 (run-hook-with-args-until-failure
8833 'org-blocker-hook change-plist)))
8835 (error "TODO state change from %s to %s blocked" this state)
8837 (message "TODO state change from %s to %s blocked" this state)
8838 (throw 'exit nil))))
8839 (store-match-data match-data)
8840 (replace-match next t t)
8841 (unless (pos-visible-in-window-p hl-pos)
8842 (message "TODO state changed to %s" (org-trim next)))
8844 (setq head (org-get-todo-sequence-head state)
8845 ass (assoc head org-todo-kwd-alist)
8846 interpret (nth 1 ass)
8847 done-word (nth 3 ass)
8848 final-done-word (nth 4 ass)))
8849 (when (memq arg '(nextset previousset))
8850 (message "Keyword-Set %d/%d: %s"
8851 (- (length org-todo-sets) -1
8852 (length (memq (assoc state org-todo-sets) org-todo-sets)))
8853 (length org-todo-sets)
8854 (mapconcat 'identity (assoc state org-todo-sets) " ")))
8855 (setq org-last-todo-state-is-todo
8856 (not (member state org-done-keywords)))
8857 (setq now-done-p (and (member state org-done-keywords)
8858 (not (member this org-done-keywords))))
8859 (and logging (org-local-logging logging))
8860 (when (and (or org-todo-log-states org-log-done)
8861 (not (memq arg '(nextset previousset))))
8862 ;; we need to look at recording a time and note
8863 (setq dolog (or (nth 1 (assoc state org-todo-log-states))
8864 (nth 2 (assoc this org-todo-log-states))))
8866 (member state org-not-done-keywords)
8867 (not (member this org-not-done-keywords)))
8868 ;; This is now a todo state and was not one before
8869 ;; If there was a CLOSED time stamp, get rid of it.
8870 (org-add-planning-info nil nil 'closed))
8871 (when (and now-done-p org-log-done)
8872 ;; It is now done, and it was not done before
8873 (org-add-planning-info 'closed (org-current-time))
8874 (if (and (not dolog) (eq 'note org-log-done))
8875 (org-add-log-setup 'done state this 'findpos 'note)))
8876 (when (and state dolog)
8877 ;; This is a non-nil state, and we need to log it
8878 (org-add-log-setup 'state state this 'findpos dolog)))
8879 ;; Fixup tag positioning
8880 (org-todo-trigger-tag-changes state)
8881 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
8882 (when org-provide-todo-statistics
8883 (org-update-parent-todo-statistics))
8884 (run-hooks 'org-after-todo-state-change-hook)
8885 (if (and arg (not (member state org-done-keywords)))
8886 (setq head (org-get-todo-sequence-head state)))
8887 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
8888 ;; Do we need to trigger a repeat?
8890 (when (boundp 'org-agenda-headline-snapshot-before-repeat)
8891 ;; This is for the agenda, take a snapshot of the headline.
8893 (setq org-agenda-headline-snapshot-before-repeat
8894 (org-get-heading))))
8895 (org-auto-repeat-maybe state))
8896 ;; Fixup cursor location if close to the keyword
8897 (if (and (outline-on-heading-p)
8899 (save-excursion (beginning-of-line 1)
8900 (looking-at org-todo-line-regexp))
8901 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
8903 (goto-char (or (match-end 2) (match-end 1)))
8905 (when org-trigger-hook
8907 (run-hook-with-args 'org-trigger-hook change-plist))))))))
8909 (defun org-block-todo-from-children-or-siblings (change-plist)
8910 "Block turning an entry into a TODO, using the hierarchy.
8911 This checks whether the current task should be blocked from state
8912 changes. Such blocking occurs when:
8914 1. The task has children which are not all in a completed state.
8916 2. A task has a parent with the property :ORDERED:, and there
8917 are siblings prior to the current task with incomplete
8920 ;; If this is not a todo state change, or if this entry is already DONE,
8922 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
8923 (member (plist-get change-plist :from)
8924 (cons 'done org-done-keywords))
8925 (member (plist-get change-plist :to)
8926 (cons 'todo org-not-done-keywords)))
8927 (throw 'dont-block t))
8928 ;; If this task has children, and any are undone, it's blocked
8930 (org-back-to-heading t)
8931 (let ((this-level (funcall outline-level)))
8932 (outline-next-heading)
8933 (let ((child-level (funcall outline-level)))
8934 (while (and (not (eobp))
8935 (> child-level this-level))
8936 ;; this todo has children, check whether they are all
8938 (if (and (not (org-entry-is-done-p))
8939 (org-entry-is-todo-p))
8940 (throw 'dont-block nil))
8941 (outline-next-heading)
8942 (setq child-level (funcall outline-level))))))
8943 ;; Otherwise, if the task's parent has the :ORDERED: property, and
8944 ;; any previous siblings are undone, it's blocked
8946 (org-back-to-heading t)
8947 (when (save-excursion
8949 (org-up-heading-all 1)
8950 (org-entry-get (point) "ORDERED")))
8951 (let* ((this-level (funcall outline-level))
8952 (current-level this-level))
8953 (while (and (not (bobp))
8954 (>= current-level this-level))
8955 (outline-previous-heading)
8956 (setq current-level (funcall outline-level))
8957 (if (= current-level this-level)
8958 ;; This is a younger sibling, check if it is completed
8959 (if (and (not (org-entry-is-done-p))
8960 (org-entry-is-todo-p))
8961 (throw 'dont-block nil)))))))
8964 (defcustom org-track-ordered-property-with-tag nil
8965 "Should the ORDERED property also be shown as a tag?
8966 The ORDERED property decides if an entry should require subtasks to be
8967 completed in sequence. Since a property is not very visible, setting
8968 this option means that toggling the ORDERED property with the command
8969 `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
8970 not relevant for the behavior, but it makes things more visible.
8972 Note that toggling the tag with tags commands will not change the property
8973 and therefore not influence behavior!
8975 This can be t, meaning the tag ORDERED should be used, It can also be a
8976 string to select a different tag for this task."
8979 (const :tag "No tracking" nil)
8980 (const :tag "Track with ORDERED tag" t)
8981 (string :tag "Use other tag")))
8983 (defun org-toggle-ordered-property ()
8984 "Toggle the ORDERED property of the current entry.
8985 For better visibility, you can track the value of this property with a tag.
8986 See variable `org-track-ordered-property-with-tag'."
8988 (let* ((t1 org-track-ordered-property-with-tag)
8989 (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
8991 (org-back-to-heading)
8992 (if (org-entry-get nil "ORDERED")
8994 (org-delete-property "ORDERED")
8995 (and tag (org-toggle-tag tag 'off))
8996 (message "Subtasks can be completed in arbitrary order"))
8997 (org-entry-put nil "ORDERED" "t")
8998 (and tag (org-toggle-tag tag 'on))
8999 (message "Subtasks must be completed in sequence")))))
9001 (defvar org-blocked-by-checkboxes) ; dynamically scoped
9002 (defun org-block-todo-from-checkboxes (change-plist)
9003 "Block turning an entry into a TODO, using checkboxes.
9004 This checks whether the current task should be blocked from state
9005 changes because there are uncheckd boxes in this entry."
9007 ;; If this is not a todo state change, or if this entry is already DONE,
9009 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
9010 (member (plist-get change-plist :from)
9011 (cons 'done org-done-keywords))
9012 (member (plist-get change-plist :to)
9013 (cons 'todo org-not-done-keywords)))
9014 (throw 'dont-block t))
9015 ;; If this task has checkboxes that are not checked, it's blocked
9017 (org-back-to-heading t)
9018 (let ((beg (point)) end)
9019 (outline-next-heading)
9022 (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
9025 (if (boundp 'org-blocked-by-checkboxes)
9026 (setq org-blocked-by-checkboxes t))
9027 (throw 'dont-block nil)))))
9030 (defun org-update-parent-todo-statistics ()
9031 "Update any statistics cookie in the parent of the current headline."
9033 (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
9034 level (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
9037 (setq level (org-up-heading-safe))
9040 (while (re-search-forward box-re (point-at-eol) t)
9041 (setq cnt-all 0 cnt-done 0 cookie-present t)
9042 (setq is-percent (match-end 2))
9044 (unless (outline-next-heading) (throw 'exit nil))
9045 (while (looking-at org-todo-line-regexp)
9046 (setq kwd (match-string 2))
9047 (and kwd (setq cnt-all (1+ cnt-all)))
9048 (and (member kwd org-done-keywords)
9049 (setq cnt-done (1+ cnt-done)))
9051 (org-forward-same-level 1)
9052 (error (end-of-line 1)))))
9055 (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
9056 (format "[%d/%d]" cnt-done cnt-all))))
9057 (when cookie-present
9058 (run-hook-with-args 'org-after-todo-statistics-hook
9059 cnt-done (- cnt-all cnt-done)))))))
9061 (defvar org-after-todo-statistics-hook nil
9062 "Hook that is called after a TODO statistics cookie has been updated.
9063 Each function is called with two arguments: the number of not-done entries
9064 and the number of done entries.
9066 For example, the following function, when added to this hook, will switch
9067 an entry to DONE when all children are done, and back to TODO when new
9068 entries are set to a TODO status. Note that this hook is only called
9069 when there is a statistics cookie in the headline!
9071 (defun org-summary-todo (n-done n-not-done)
9072 \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
9073 (let (org-log-done org-log-states) ; turn off logging
9074 (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
9077 (defun org-todo-trigger-tag-changes (state)
9078 "Apply the changes defined in `org-todo-state-tags-triggers'."
9079 (let ((l org-todo-state-tags-triggers)
9081 (when (or (not state) (equal state ""))
9082 (setq changes (append changes (cdr (assoc "" l)))))
9083 (when (and (stringp state) (> (length state) 0))
9084 (setq changes (append changes (cdr (assoc state l)))))
9085 (when (member state org-not-done-keywords)
9086 (setq changes (append changes (cdr (assoc 'todo l)))))
9087 (when (member state org-done-keywords)
9088 (setq changes (append changes (cdr (assoc 'done l)))))
9090 (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
9092 (defun org-local-logging (value)
9093 "Get logging settings from a property VALUE."
9095 ;; directly set the variables, they are already local.
9096 (setq org-log-done nil
9098 org-todo-log-states nil)
9099 (setq words (org-split-string value))
9100 (while (setq w (pop words))
9102 ((setq a (assoc w org-startup-options))
9103 (and (member (nth 1 a) '(org-log-done org-log-repeat))
9104 (set (nth 1 a) (nth 2 a))))
9105 ((setq a (org-extract-log-state-settings w))
9106 (and (member (car a) org-todo-keywords-1)
9107 (push a org-todo-log-states)))))))
9109 (defun org-get-todo-sequence-head (kwd)
9110 "Return the head of the TODO sequence to which KWD belongs.
9111 If KWD is not set, check if there is a text property remembering the
9116 (or (get-text-property (point-at-bol) 'org-todo-head)
9118 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
9119 nil (point-at-eol)))
9120 (get-text-property p 'org-todo-head))))
9121 ((not (member kwd org-todo-keywords-1))
9122 (car org-todo-keywords-1))
9123 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
9125 (defun org-fast-todo-selection ()
9126 "Fast TODO keyword selection with single keys.
9127 Returns the new TODO keyword, or nil if no state change should occur."
9128 (let* ((fulltable org-todo-key-alist)
9129 (done-keywords org-done-keywords) ;; needed for the faces.
9130 (maxlen (apply 'max (mapcar
9132 (if (stringp (car x)) (string-width (car x)) 0))
9135 (fwidth (+ maxlen 3 1 3))
9136 (ncol (/ (- (window-width) 4) fwidth))
9140 (save-window-excursion
9142 (set-buffer (get-buffer-create " *Org todo*"))
9143 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
9145 (org-set-local 'org-done-keywords done-keywords)
9146 (setq tbl fulltable cnt 0)
9147 (while (setq e (pop tbl))
9149 ((equal e '(:startgroup))
9150 (push '() groups) (setq ingroup t)
9151 (when (not (= cnt 0))
9155 ((equal e '(:endgroup))
9156 (setq ingroup nil cnt 0)
9158 ((equal e '(:newline))
9159 (when (not (= cnt 0))
9163 (while (equal (car tbl) '(:newline))
9165 (setq tbl (cdr tbl)))))
9167 (setq tg (car e) c (cdr e))
9168 (if ingroup (push tg (car groups)))
9169 (setq tg (org-add-props tg nil 'face
9170 (org-get-todo-face tg)))
9171 (if (and (= cnt 0) (not ingroup)) (insert " "))
9172 (insert "[" c "] " tg (make-string
9173 (- fwidth 4 (length tg)) ?\ ))
9174 (when (= (setq cnt (1+ cnt)) ncol)
9176 (if ingroup (insert " "))
9179 (goto-char (point-min))
9180 (if (not expert) (org-fit-window-to-buffer))
9181 (message "[a-z..]:Set [SPC]:clear")
9182 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
9185 (and (= c ?q) (not (rassoc c fulltable))))
9188 ((setq e (rassoc c fulltable) tg (car e))
9190 (t (setq quit-flag t)))))))
9192 (defun org-entry-is-todo-p ()
9193 (member (org-get-todo-state) org-not-done-keywords))
9195 (defun org-entry-is-done-p ()
9196 (member (org-get-todo-state) org-done-keywords))
9198 (defun org-get-todo-state ()
9200 (org-back-to-heading t)
9201 (and (looking-at org-todo-line-regexp)
9205 (defun org-at-date-range-p (&optional inactive-ok)
9206 "Is the cursor inside a date range?"
9210 (let ((pos (point)))
9211 (skip-chars-backward "^[<\r\n")
9212 (skip-chars-backward "<[")
9213 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
9214 (>= (match-end 0) pos)
9216 (skip-chars-backward "^<[\r\n")
9217 (skip-chars-backward "<[")
9218 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
9219 (>= (match-end 0) pos)
9223 (defun org-get-repeat ()
9224 "Check if there is a deadline/schedule with repeater in this entry."
9227 (org-back-to-heading t)
9228 (if (re-search-forward
9229 org-repeat-re (save-excursion (outline-next-heading) (point)) t)
9230 (match-string 1)))))
9232 (defvar org-last-changed-timestamp)
9233 (defvar org-last-inserted-timestamp)
9234 (defvar org-log-post-message)
9235 (defvar org-log-note-purpose)
9236 (defvar org-log-note-how)
9237 (defvar org-log-note-extra)
9238 (defun org-auto-repeat-maybe (done-word)
9239 "Check if the current headline contains a repeated deadline/schedule.
9240 If yes, set TODO state back to what it was and change the base date
9241 of repeating deadline/scheduled time stamps to new date.
9242 This function is run automatically after each state change to a DONE state."
9243 ;; last-state is dynamically scoped into this function
9244 (let* ((repeat (org-get-repeat))
9245 (aa (assoc last-state org-todo-kwd-alist))
9246 (interpret (nth 1 aa))
9248 (whata '(("d" . day) ("m" . month) ("y" . year)))
9249 (msg "Entry repeats: ")
9251 (org-todo-log-states nil)
9252 (nshiftmax 10) (nshift 0)
9253 re type n what ts time)
9255 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
9256 (org-todo (if (eq interpret 'type) last-state head))
9257 (when org-log-repeat
9258 (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
9259 (memq 'org-add-log-note post-command-hook))
9260 ;; OK, we are already setup for some record
9261 (if (eq org-log-repeat 'note)
9262 ;; make sure we take a note, not only a time stamp
9263 (setq org-log-note-how 'note))
9264 ;; Set up for taking a record
9265 (org-add-log-setup 'state (or done-word (car org-done-keywords))
9267 'findpos org-log-repeat)))
9268 (org-back-to-heading t)
9269 (org-add-planning-info nil nil 'closed)
9270 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
9271 org-deadline-time-regexp "\\)\\|\\("
9272 org-ts-regexp "\\)"))
9273 (while (re-search-forward
9274 re (save-excursion (outline-next-heading) (point)) t)
9275 (setq type (if (match-end 1) org-scheduled-string
9276 (if (match-end 3) org-deadline-string "Plain:"))
9277 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
9278 (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
9279 (setq n (string-to-number (match-string 2 ts))
9280 what (match-string 3 ts))
9281 (if (equal what "w") (setq n (* n 7) what "d"))
9282 ;; Preparation, see if we need to modify the start date for the change
9284 (setq time (save-match-data (org-time-string-to-time ts)))
9286 ((equal (match-string 1 ts) ".")
9287 ;; Shift starting date to today
9288 (org-timestamp-change
9289 (- (time-to-days (current-time)) (time-to-days time))
9291 ((equal (match-string 1 ts) "+")
9292 (while (or (= nshift 0)
9293 (<= (time-to-days time) (time-to-days (current-time))))
9294 (when (= (incf nshift) nshiftmax)
9295 (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
9297 (org-timestamp-change n (cdr (assoc what whata)))
9298 (org-at-timestamp-p t)
9299 (setq ts (match-string 1))
9300 (setq time (save-match-data (org-time-string-to-time ts))))
9301 (org-timestamp-change (- n) (cdr (assoc what whata)))
9302 ;; rematch, so that we have everything in place for the real shift
9303 (org-at-timestamp-p t)
9304 (setq ts (match-string 1))
9305 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
9306 (org-timestamp-change n (cdr (assoc what whata)))
9307 (setq msg (concat msg type " " org-last-changed-timestamp " "))))
9308 (setq org-log-post-message msg)
9309 (message "%s" msg))))
9311 (defun org-show-todo-tree (arg)
9312 "Make a compact tree which shows all headlines marked with TODO.
9313 The tree will show the lines where the regexp matches, and all higher
9314 headlines above the match.
9315 With a \\[universal-argument] prefix, also show the DONE entries.
9316 With a numeric prefix N, construct a sparse tree for the Nth element
9317 of `org-todo-keywords-1'."
9319 (let ((case-fold-search nil)
9321 (cond ((null arg) org-not-done-regexp)
9323 (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
9324 (mapcar 'list org-todo-keywords-1))))
9326 (mapconcat 'identity (org-split-string kwd "|") "\\|")
9328 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
9329 (regexp-quote (nth (1- (prefix-numeric-value arg))
9330 org-todo-keywords-1)))
9331 (t (error "Invalid prefix argument: %s" arg)))))
9332 (message "%d TODO entries found"
9333 (org-occur (concat "^" outline-regexp " *" kwd-re )))))
9335 (defun org-deadline (&optional remove time)
9336 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
9337 With argument REMOVE, remove any deadline from the item.
9338 When TIME is set, it should be an internal time specification, and the
9339 scheduling will use the corresponding date."
9343 (org-remove-timestamp-with-keyword org-deadline-string)
9344 (message "Item no longer has a deadline."))
9345 (if (org-get-repeat)
9346 (error "Cannot change deadline on task with repeater, please do that by hand")
9347 (org-add-planning-info 'deadline time 'closed)
9348 (message "Deadline on %s" org-last-inserted-timestamp))))
9350 (defun org-schedule (&optional remove time)
9351 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
9352 With argument REMOVE, remove any scheduling date from the item.
9353 When TIME is set, it should be an internal time specification, and the
9354 scheduling will use the corresponding date."
9358 (org-remove-timestamp-with-keyword org-scheduled-string)
9359 (message "Item is no longer scheduled."))
9360 (if (org-get-repeat)
9361 (error "Cannot reschedule task with repeater, please do that by hand")
9362 (org-add-planning-info 'scheduled time 'closed)
9363 (message "Scheduled to %s" org-last-inserted-timestamp))))
9365 (defun org-get-scheduled-time (pom &optional inherit)
9366 "Get the scheduled time as a time tuple, of a format suitable
9367 for calling org-schedule with, or if there is no scheduling,
9369 (let ((time (org-entry-get pom "SCHEDULED" inherit)))
9371 (apply 'encode-time (org-parse-time-string time)))))
9373 (defun org-get-deadline-time (pom &optional inherit)
9374 "Get the deadine as a time tuple, of a format suitable for
9375 calling org-deadlin with, or if there is no scheduling, returns
9377 (let ((time (org-entry-get pom "DEADLINE" inherit)))
9379 (apply 'encode-time (org-parse-time-string time)))))
9381 (defun org-remove-timestamp-with-keyword (keyword)
9382 "Remove all time stamps with KEYWORD in the current entry."
9383 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
9386 (org-back-to-heading t)
9388 (org-end-of-subtree t t)
9389 (while (re-search-backward re beg t)
9391 (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
9392 (equal (char-before) ?\ ))
9393 (backward-delete-char 1)
9394 (if (string-match "^[ \t]*$" (buffer-substring
9395 (point-at-bol) (point-at-eol)))
9396 (delete-region (point-at-bol)
9397 (min (point-max) (1+ (point-at-eol))))))))))
9399 (defun org-add-planning-info (what &optional time &rest remove)
9400 "Insert new timestamp with keyword in the line directly after the headline.
9401 WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
9402 If non is given, the user is prompted for a date.
9403 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
9406 (let (org-time-was-given org-end-time-was-given ts
9407 end default-time default-input)
9410 (when (and (not time) (memq what '(scheduled deadline)))
9411 ;; Try to get a default date/time from existing timestamp
9413 (org-back-to-heading t)
9414 (setq end (save-excursion (outline-next-heading) (point)))
9415 (when (re-search-forward (if (eq what 'scheduled)
9416 org-scheduled-time-regexp
9417 org-deadline-time-regexp)
9419 (setq ts (match-string 1)
9421 (apply 'encode-time (org-parse-time-string ts))
9422 default-input (and ts (org-get-compact-tod ts))))))
9424 ;; If necessary, get the time from the user
9425 (setq time (or time (org-read-date nil 'to-time nil nil
9426 default-time default-input))))
9428 (when (and org-insert-labeled-timestamps-at-point
9429 (member what '(scheduled deadline)))
9431 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
9432 (org-insert-time-stamp time org-time-was-given
9433 nil nil nil (list org-end-time-was-given))
9437 (let (col list elt ts buffer-invisibility-spec)
9438 (org-back-to-heading t)
9439 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
9440 (goto-char (match-end 1))
9441 (setq col (current-column))
9442 (goto-char (match-end 0))
9443 (if (eobp) (insert "\n") (forward-char 1))
9444 (when (and (not what)
9447 org-keyword-time-not-clock-regexp))))
9448 ;; Nothing to add, nothing to remove...... :-)
9450 (if (and (not (looking-at outline-regexp))
9451 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
9453 (not (equal (match-string 1) org-clock-string)))
9454 (narrow-to-region (match-beginning 0) (match-end 0))
9455 (insert-before-markers "\n")
9457 (narrow-to-region (point) (point))
9458 (and org-adapt-indentation (org-indent-to-column col)))
9459 ;; Check if we have to remove something.
9460 (setq list (cons what remove))
9462 (setq elt (pop list))
9463 (goto-char (point-min))
9464 (when (or (and (eq elt 'scheduled)
9465 (re-search-forward org-scheduled-time-regexp nil t))
9466 (and (eq elt 'deadline)
9467 (re-search-forward org-deadline-time-regexp nil t))
9468 (and (eq elt 'closed)
9469 (re-search-forward org-closed-time-regexp nil t)))
9471 (if (looking-at "--+<[^>]+>") (replace-match ""))
9472 (if (looking-at " +") (replace-match ""))))
9473 (goto-char (point-max))
9476 (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
9477 (cond ((eq what 'scheduled) org-scheduled-string)
9478 ((eq what 'deadline) org-deadline-string)
9479 ((eq what 'closed) org-closed-string))
9481 (setq ts (org-insert-time-stamp
9483 (or org-time-was-given
9484 (and (eq what 'closed) org-log-done-with-time))
9486 nil nil (list org-end-time-was-given)))
9488 (goto-char (point-min))
9490 (if (and (looking-at "[ \t]+\n")
9491 (equal (char-before) ?\n))
9492 (delete-region (1- (point)) (point-at-eol)))
9495 (defvar org-log-note-marker (make-marker))
9496 (defvar org-log-note-purpose nil)
9497 (defvar org-log-note-state nil)
9498 (defvar org-log-note-previous-state nil)
9499 (defvar org-log-note-how nil)
9500 (defvar org-log-note-extra nil)
9501 (defvar org-log-note-window-configuration nil)
9502 (defvar org-log-note-return-to (make-marker))
9503 (defvar org-log-post-message nil
9504 "Message to be displayed after a log note has been stored.
9505 The auto-repeater uses this.")
9507 (defun org-add-note ()
9508 "Add a note to the current entry.
9509 This is done in the same way as adding a state change note."
9511 (org-add-log-setup 'note nil nil 'findpos nil))
9513 (defvar org-property-end-re)
9514 (defun org-add-log-setup (&optional purpose state prev-state
9515 findpos how &optional extra)
9516 "Set up the post command hook to take a note.
9517 If this is about to TODO state change, the new state is expected in STATE.
9518 When FINDPOS is non-nil, find the correct position for the note in
9519 the current entry. If not, assume that it can be inserted at point.
9520 HOW is an indicator what kind of note should be created.
9521 EXTRA is additional text that will be inserted into the notes buffer."
9522 (let ((drawer (cond ((stringp org-log-into-drawer)
9523 org-log-into-drawer)
9524 (org-log-into-drawer "LOGBOOK")
9529 (org-back-to-heading t)
9530 (narrow-to-region (point) (save-excursion
9531 (outline-next-heading) (point)))
9532 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
9533 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
9535 (goto-char (match-end 0))
9538 (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
9541 (goto-char (match-end 0))
9542 (or org-log-states-order-reversed
9543 (and (re-search-forward org-property-end-re nil t)
9544 (goto-char (1- (match-beginning 0))))))
9545 (insert "\n:" drawer ":\n:END:")
9546 (beginning-of-line 0)
9547 (org-indent-line-function)
9548 (beginning-of-line 2)
9549 (org-indent-line-function)
9551 ((and org-log-state-notes-insert-after-drawers
9553 (forward-line) (looking-at org-drawer-regexp)))
9555 (while (looking-at org-drawer-regexp)
9556 (goto-char (match-end 0))
9557 (re-search-forward org-property-end-re (point-max) t)
9560 (unless org-log-states-order-reversed
9561 (and (= (char-after) ?\n) (forward-char 1))
9562 (org-skip-over-state-notes)
9563 (skip-chars-backward " \t\n\r")))
9564 (move-marker org-log-note-marker (point))
9565 (setq org-log-note-purpose purpose
9566 org-log-note-state state
9567 org-log-note-previous-state prev-state
9568 org-log-note-how how
9569 org-log-note-extra extra)
9570 (add-hook 'post-command-hook 'org-add-log-note 'append)))))
9572 (defun org-skip-over-state-notes ()
9573 "Skip past the list of State notes in an entry."
9574 (if (looking-at "\n[ \t]*- State") (forward-char 1))
9575 (while (looking-at "[ \t]*- State")
9578 (error (org-end-of-item)))))
9580 (defun org-add-log-note (&optional purpose)
9581 "Pop up a window for taking a note, and add this note later at point."
9582 (remove-hook 'post-command-hook 'org-add-log-note)
9583 (setq org-log-note-window-configuration (current-window-configuration))
9584 (delete-other-windows)
9585 (move-marker org-log-note-return-to (point))
9586 (switch-to-buffer (marker-buffer org-log-note-marker))
9587 (goto-char org-log-note-marker)
9588 (org-switch-to-buffer-other-window "*Org Note*")
9590 (if (memq org-log-note-how '(time state))
9591 (let (current-prefix-arg) (org-store-log-note))
9592 (let ((org-inhibit-startup t)) (org-mode))
9593 (insert (format "# Insert note for %s.
9594 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
9596 ((eq org-log-note-purpose 'clock-out) "stopped clock")
9597 ((eq org-log-note-purpose 'done) "closed todo item")
9598 ((eq org-log-note-purpose 'state)
9599 (format "state change from \"%s\" to \"%s\""
9600 (or org-log-note-previous-state "")
9601 (or org-log-note-state "")))
9602 ((eq org-log-note-purpose 'note)
9604 (t (error "This should not happen")))))
9605 (if org-log-note-extra (insert org-log-note-extra))
9606 (org-set-local 'org-finish-function 'org-store-log-note)))
9608 (defvar org-note-abort nil) ; dynamically scoped
9609 (defun org-store-log-note ()
9610 "Finish taking a log note, and insert it to where it belongs."
9611 (let ((txt (buffer-string))
9612 (note (cdr (assq org-log-note-purpose org-log-note-headings)))
9614 (kill-buffer (current-buffer))
9615 (while (string-match "\\`#.*\n[ \t\n]*" txt)
9616 (setq txt (replace-match "" t t txt)))
9617 (if (string-match "\\s-+\\'" txt)
9618 (setq txt (replace-match "" t t txt)))
9619 (setq lines (org-split-string txt "\n"))
9620 (when (and note (string-match "\\S-" note))
9622 (org-replace-escapes
9624 (list (cons "%u" (user-login-name))
9625 (cons "%U" user-full-name)
9626 (cons "%t" (format-time-string
9627 (org-time-stamp-format 'long 'inactive)
9629 (cons "%s" (if org-log-note-state
9630 (concat "\"" org-log-note-state "\"")
9632 (cons "%S" (if org-log-note-previous-state
9633 (concat "\"" org-log-note-previous-state "\"")
9635 (if lines (setq note (concat note " \\\\")))
9637 (when (or current-prefix-arg org-note-abort)
9638 (when org-log-into-drawer
9639 (org-remove-empty-drawer-at
9640 (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
9641 org-log-note-marker))
9645 (set-buffer (marker-buffer org-log-note-marker))
9647 (goto-char org-log-note-marker)
9648 (move-marker org-log-note-marker nil)
9650 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
9651 (insert "- " (pop lines))
9652 (org-indent-line-function)
9653 (beginning-of-line 1)
9654 (looking-at "[ \t]*")
9655 (setq ind (concat (match-string 0) " "))
9657 (while lines (insert "\n" ind (pop lines)))
9658 (message "Note stored")
9659 (org-back-to-heading t)
9660 (org-cycle-hide-drawers 'children)))))
9661 (set-window-configuration org-log-note-window-configuration)
9662 (with-current-buffer (marker-buffer org-log-note-return-to)
9663 (goto-char org-log-note-return-to))
9664 (move-marker org-log-note-return-to nil)
9665 (and org-log-post-message (message "%s" org-log-post-message)))
9667 (defun org-remove-empty-drawer-at (drawer pos)
9668 "Remove an emptyr DARWER drawer at position POS.
9669 POS may also be a marker."
9670 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
9676 (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
9677 (replace-match ""))))))
9679 (defun org-sparse-tree (&optional arg)
9680 "Create a sparse tree, prompt for the details.
9681 This command can create sparse trees. You first need to select the type
9682 of match used to create the tree:
9684 t Show entries with a specific TODO keyword.
9685 m Show entries selected by a tags/property match.
9686 p Enter a property name and its value (both with completion on existing
9687 names/values) and show entries with that property.
9688 r Show entries matching a regular expression.
9689 d Show deadlines due within `org-deadline-warning-days'.
9690 b Show deadlines and scheduled items before a date.
9691 a Show deadlines and scheduled items after a date."
9693 (let (ans kwd value)
9694 (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
9695 (setq ans (read-char-exclusive))
9698 (call-interactively 'org-check-deadlines))
9700 (call-interactively 'org-check-before-date))
9702 (call-interactively 'org-check-after-date))
9704 (org-show-todo-tree '(4)))
9705 ((member ans '(?T ?m))
9706 (call-interactively 'org-match-sparse-tree))
9707 ((member ans '(?p ?P))
9708 (setq kwd (org-ido-completing-read "Property: "
9709 (mapcar 'list (org-buffer-property-keys))))
9710 (setq value (org-ido-completing-read "Value: "
9711 (mapcar 'list (org-property-values kwd))))
9712 (unless (string-match "\\`{.*}\\'" value)
9713 (setq value (concat "\"" value "\"")))
9714 (org-match-sparse-tree arg (concat kwd "=" value)))
9715 ((member ans '(?r ?R ?/))
9716 (call-interactively 'org-occur))
9717 (t (error "No such sparse tree command \"%c\"" ans)))))
9719 (defvar org-occur-highlights nil
9720 "List of overlays used for occur matches.")
9721 (make-variable-buffer-local 'org-occur-highlights)
9722 (defvar org-occur-parameters nil
9723 "Parameters of the active org-occur calls.
9724 This is a list, each call to org-occur pushes as cons cell,
9725 containing the regular expression and the callback, onto the list.
9726 The list can contain several entries if `org-occur' has been called
9727 several time with the KEEP-PREVIOUS argument. Otherwise, this list
9728 will only contain one set of parameters. When the highlights are
9729 removed (for example with `C-c C-c', or with the next edit (depending
9730 on `org-remove-highlights-with-change'), this variable is emptied
9732 (make-variable-buffer-local 'org-occur-parameters)
9734 (defun org-occur (regexp &optional keep-previous callback)
9735 "Make a compact tree which shows all matches of REGEXP.
9736 The tree will show the lines where the regexp matches, and all higher
9737 headlines above the match. It will also show the heading after the match,
9738 to make sure editing the matching entry is easy.
9739 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
9740 call to `org-occur' will be kept, to allow stacking of calls to this
9742 If CALLBACK is non-nil, it is a function which is called to confirm
9743 that the match should indeed be shown."
9744 (interactive "sRegexp: \nP")
9745 (unless keep-previous
9746 (org-remove-occur-highlights nil nil t))
9747 (push (cons regexp callback) org-occur-parameters)
9750 (goto-char (point-min))
9751 (if (or (not keep-previous) ; do not want to keep
9752 (not org-occur-highlights)) ; no previous matches
9755 (while (re-search-forward regexp nil t)
9756 (when (or (not callback)
9757 (save-match-data (funcall callback)))
9759 (when org-highlight-sparse-tree-matches
9760 (org-highlight-new-match (match-beginning 0) (match-end 0)))
9761 (org-show-context 'occur-tree))))
9762 (when org-remove-highlights-with-change
9763 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
9765 (unless org-sparse-tree-open-archived-trees
9766 (org-hide-archived-subtrees (point-min) (point-max)))
9767 (run-hooks 'org-occur-hook)
9769 (message "%d match(es) for regexp %s" cnt regexp))
9772 (defun org-show-context (&optional key)
9773 "Make sure point and context and visible.
9774 How much context is shown depends upon the variables
9775 `org-show-hierarchy-above', `org-show-following-heading'. and
9776 `org-show-siblings'."
9777 (let ((heading-p (org-on-heading-p t))
9778 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
9779 (following-p (org-get-alist-option org-show-following-heading key))
9780 (entry-p (org-get-alist-option org-show-entry-below key))
9781 (siblings-p (org-get-alist-option org-show-siblings key)))
9783 ;; Show heading or entry text
9784 (if (and heading-p (not entry-p))
9785 (org-flag-heading nil) ; only show the heading
9786 (and (or entry-p (org-invisible-p) (org-invisible-p2))
9787 (org-show-hidden-entry))) ; show entire entry
9789 ;; Show next sibling, or heading below text
9791 (and (if heading-p (org-goto-sibling) (outline-next-heading))
9792 (org-flag-heading nil))))
9793 (when siblings-p (org-show-siblings))
9795 ;; show all higher headings, possibly with siblings
9797 (while (and (condition-case nil
9798 (progn (org-up-heading-all 1) t)
9801 (org-flag-heading nil)
9802 (when siblings-p (org-show-siblings))))))))
9804 (defun org-reveal (&optional siblings)
9805 "Show current entry, hierarchy above it, and the following headline.
9806 This can be used to show a consistent set of context around locations
9807 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
9808 not t for the search context.
9810 With optional argument SIBLINGS, on each level of the hierarchy all
9811 siblings are shown. This repairs the tree structure to what it would
9812 look like when opened with hierarchical calls to `org-cycle'."
9814 (let ((org-show-hierarchy-above t)
9815 (org-show-following-heading t)
9816 (org-show-siblings (if siblings t org-show-siblings)))
9817 (org-show-context nil)))
9819 (defun org-highlight-new-match (beg end)
9820 "Highlight from BEG to END and mark the highlight is an occur headline."
9821 (let ((ov (org-make-overlay beg end)))
9822 (org-overlay-put ov 'face 'secondary-selection)
9823 (push ov org-occur-highlights)))
9825 (defun org-remove-occur-highlights (&optional beg end noremove)
9826 "Remove the occur highlights from the buffer.
9827 BEG and END are ignored. If NOREMOVE is nil, remove this function
9828 from the `before-change-functions' in the current buffer."
9830 (unless org-inhibit-highlight-removal
9831 (mapc 'org-delete-overlay org-occur-highlights)
9832 (setq org-occur-highlights nil)
9833 (setq org-occur-parameters nil)
9835 (remove-hook 'before-change-functions
9836 'org-remove-occur-highlights 'local))))
9840 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
9841 "Regular expression matching the priority indicator.")
9843 (defvar org-remove-priority-next-time nil)
9845 (defun org-priority-up ()
9846 "Increase the priority of the current item."
9850 (defun org-priority-down ()
9851 "Decrease the priority of the current item."
9853 (org-priority 'down))
9855 (defun org-priority (&optional action)
9856 "Change the priority of an item by ARG.
9857 ACTION can be `set', `up', `down', or a character."
9859 (setq action (or action 'set))
9860 (let (current new news have remove)
9862 (org-back-to-heading t)
9863 (if (looking-at org-priority-regexp)
9864 (setq current (string-to-char (match-string 2))
9866 (setq current org-default-priority))
9868 ((or (eq action 'set)
9869 (if (featurep 'xemacs) (characterp action) (integerp action)))
9870 (if (not (eq action 'set))
9872 (message "Priority %c-%c, SPC to remove: "
9873 org-highest-priority org-lowest-priority)
9874 (setq new (read-char-exclusive)))
9875 (if (and (= (upcase org-highest-priority) org-highest-priority)
9876 (= (upcase org-lowest-priority) org-lowest-priority))
9877 (setq new (upcase new)))
9878 (cond ((equal new ?\ ) (setq remove t))
9879 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
9880 (error "Priority must be between `%c' and `%c'"
9881 org-highest-priority org-lowest-priority))))
9883 (if (and (not have) (eq last-command this-command))
9884 (setq new org-lowest-priority)
9885 (setq new (if (and org-priority-start-cycle-with-default (not have))
9886 org-default-priority (1- current)))))
9888 (if (and (not have) (eq last-command this-command))
9889 (setq new org-highest-priority)
9890 (setq new (if (and org-priority-start-cycle-with-default (not have))
9891 org-default-priority (1+ current)))))
9892 (t (error "Invalid action")))
9893 (if (or (< (upcase new) org-highest-priority)
9894 (> (upcase new) org-lowest-priority))
9896 (setq news (format "%c" new))
9899 (replace-match "" t t nil 1)
9900 (replace-match news t t nil 2))
9902 (error "No priority cookie found in line")
9903 (looking-at org-todo-line-regexp)
9906 (goto-char (match-end 2))
9907 (insert " [#" news "]"))
9908 (goto-char (match-beginning 3))
9909 (insert "[#" news "] ")))))
9910 (org-preserve-lc (org-set-tags nil 'align))
9912 (message "Priority removed")
9913 (message "Priority of current item set to %s" news))))
9916 (defun org-get-priority (s)
9917 "Find priority cookie and return priority."
9919 (if (not (string-match org-priority-regexp s))
9920 (* 1000 (- org-lowest-priority org-default-priority))
9921 (* 1000 (- org-lowest-priority
9922 (string-to-char (match-string 2 s)))))))
9926 (defvar org-agenda-archives-mode)
9927 (defvar org-scanner-tags nil
9928 "The current tag list while the tags scanner is running.")
9929 (defvar org-trust-scanner-tags nil
9930 "Should `org-get-tags-at' use the tags fro the scanner.
9931 This is for internal dynamical scoping only.
9932 When this is non-nil, the function `org-get-tags-at' will return the value
9933 of `org-scanner-tags' instead of building the list by itself. This
9934 can lead to large speed-ups when the tags scanner is used in a file with
9935 many entries, and when the list of tags is retrieved, for example to
9936 obtain a list of properties. Building the tags list for each entry in such
9937 a file becomes an N^2 operation - but with this variable set, it scales
9940 (defun org-scan-tags (action matcher &optional todo-only)
9941 "Scan headline tags with inheritance and produce output ACTION.
9943 ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
9944 or `agenda' to produce an entry list for an agenda view. It can also be
9945 a Lisp form or a function that should be called at each matched headline, in
9946 this case the return value is a list of all return values from these calls.
9948 MATCHER is a Lisp form to be evaluated, testing if a given set of tags
9949 qualifies a headline for inclusion. When TODO-ONLY is non-nil,
9950 only lines with a TODO keyword are included in the output."
9951 (require 'org-agenda)
9952 (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
9953 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
9955 "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
9956 (props (list 'face 'default
9957 'done-face 'org-agenda-done
9958 'undone-face 'default
9959 'mouse-face 'highlight
9960 'org-not-done-regexp org-not-done-regexp
9961 'org-todo-regexp org-todo-regexp
9962 'keymap org-agenda-keymap
9964 (format "mouse-2 or RET jump to org file %s"
9965 (abbreviate-file-name
9966 (or (buffer-file-name (buffer-base-buffer))
9967 (buffer-name (buffer-base-buffer)))))))
9968 (case-fold-search nil)
9969 lspos tags tags-list
9970 (tags-alist (list (cons 0 org-file-tags)))
9971 (llast 0) rtn rtn1 level category i txt
9972 todo marker entry priority)
9973 (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
9974 (setq action (list 'lambda nil action)))
9976 (goto-char (point-min))
9977 (when (eq action 'sparse-tree)
9979 (org-remove-occur-highlights))
9980 (while (re-search-forward re nil t)
9982 (setq todo (if (match-end 1) (org-match-string-no-properties 2))
9983 tags (if (match-end 4) (org-match-string-no-properties 4)))
9984 (goto-char (setq lspos (1+ (match-beginning 0))))
9985 (setq level (org-reduced-level (funcall outline-level))
9986 category (org-get-category))
9987 (setq i llast llast level)
9988 ;; remove tag lists from same and sublevels
9990 (when (setq entry (assoc i tags-alist))
9991 (setq tags-alist (delete entry tags-alist)))
9993 ;; add the next tags
9995 (setq tags (org-split-string tags ":")
9997 (cons (cons level tags) tags-alist)))
9998 ;; compile tags for current headline
10000 (if org-use-tag-inheritance
10001 (apply 'append (mapcar 'cdr (reverse tags-alist)))
10003 org-scanner-tags tags-list)
10004 (when org-use-tag-inheritance
10005 (setcdr (car tags-alist)
10006 (mapcar (lambda (x)
10007 (setq x (copy-sequence x))
10008 (org-add-prop-inherited x))
10009 (cdar tags-alist))))
10010 (when (and tags org-use-tag-inheritance
10011 (or (not (eq t org-use-tag-inheritance))
10012 org-tags-exclude-from-inheritance))
10013 ;; selective inheritance, remove uninherited ones
10014 (setcdr (car tags-alist)
10015 (org-remove-uniherited-tags (cdar tags-alist))))
10016 (when (and (or (not todo-only)
10017 (and (member todo org-not-done-keywords)
10018 (or (not org-agenda-tags-todo-honor-ignore-options)
10019 (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
10020 (let ((case-fold-search t)) (eval matcher))
10022 (not (member org-archive-tag tags-list))
10023 ;; we have an archive tag, should we use this anyway?
10024 (or (not org-agenda-skip-archived-trees)
10025 (and (eq action 'agenda) org-agenda-archives-mode))))
10026 (unless (eq action 'sparse-tree) (org-agenda-skip))
10028 ;; select this headline
10031 ((eq action 'sparse-tree)
10032 (and org-highlight-sparse-tree-matches
10033 (org-get-heading) (match-end 0)
10034 (org-highlight-new-match
10035 (match-beginning 0) (match-beginning 1)))
10036 (org-show-context 'tags-tree))
10037 ((eq action 'agenda)
10038 (setq txt (org-format-agenda-item
10041 (if (eq org-tags-match-list-sublevels 'indented)
10042 (make-string (1- level) ?.) "")
10047 priority (org-get-priority txt))
10049 (setq marker (org-agenda-new-marker))
10050 (org-add-props txt props
10051 'org-marker marker 'org-hd-marker marker 'org-category category
10053 'priority priority 'type "tagsmatch")
10055 ((functionp action)
10057 (setq rtn1 (funcall action))
10059 (goto-char (point-at-eol)))
10060 (t (error "Invalid action")))
10062 ;; if we are to skip sublevels, jump to end of subtree
10063 (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
10064 (when (and (eq action 'sparse-tree)
10065 (not org-sparse-tree-open-archived-trees))
10066 (org-hide-archived-subtrees (point-min) (point-max)))
10069 (defun org-remove-uniherited-tags (tags)
10070 "Remove all tags that are not inherited from the list TAGS."
10072 ((eq org-use-tag-inheritance t)
10073 (if org-tags-exclude-from-inheritance
10074 (org-delete-all org-tags-exclude-from-inheritance tags)
10076 ((not org-use-tag-inheritance) nil)
10077 ((stringp org-use-tag-inheritance)
10080 (if (and (string-match org-use-tag-inheritance x)
10081 (not (member x org-tags-exclude-from-inheritance)))
10084 ((listp org-use-tag-inheritance)
10087 (if (member x org-use-tag-inheritance) x nil))
10090 (defvar todo-only) ;; dynamically scoped
10092 (defun org-match-sparse-tree (&optional todo-only match)
10093 "Create a sparse tree according to tags string MATCH.
10094 MATCH can contain positive and negative selection of tags, like
10095 \"+WORK+URGENT-WITHBOSS\".
10096 If optional argument TODO-ONLY is non-nil, only select lines that are
10099 (org-prepare-agenda-buffers (list (current-buffer)))
10100 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
10102 (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
10104 (defvar org-cached-props nil)
10105 (defun org-cached-entry-get (pom property)
10106 (if (or (eq t org-use-property-inheritance)
10107 (and (stringp org-use-property-inheritance)
10108 (string-match org-use-property-inheritance property))
10109 (and (listp org-use-property-inheritance)
10110 (member property org-use-property-inheritance)))
10111 ;; Caching is not possible, check it directly
10112 (org-entry-get pom property 'inherit)
10113 ;; Get all properties, so that we can do complicated checks easily
10114 (cdr (assoc property (or org-cached-props
10115 (setq org-cached-props
10116 (org-entry-properties pom)))))))
10118 (defun org-global-tags-completion-table (&optional files)
10119 "Return the list of all tags in all agenda buffer/files."
10126 (set-buffer (find-file-noselect file))
10127 (append (org-get-buffer-tags)
10128 (mapcar (lambda (x) (if (stringp (car-safe x))
10129 (list (car-safe x)) nil))
10131 (if (and files (car files))
10133 (org-agenda-files))))))))
10135 (defun org-make-tags-matcher (match)
10136 "Create the TAGS//TODO matcher form for the selection string MATCH."
10137 ;; todo-only is scoped dynamically into this function, and the function
10138 ;; may change it if the matcher asks for it.
10140 ;; Get a new match request, with completion
10141 (let ((org-last-tags-completion-table
10142 (org-global-tags-completion-table)))
10143 (setq match (org-completing-read-no-ido
10144 "Match: " 'org-tags-completion-function nil nil nil
10145 'org-tags-history))))
10147 ;; Parse the string and create a lisp form
10148 (let ((match0 match)
10149 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
10151 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
10152 orterms term orlist re-p str-p level-p level-op time-p
10153 prop-p pn pv po cat-p gv rest)
10154 (if (string-match "/+" match)
10155 ;; match contains also a todo-matching request
10157 (setq tagsmatch (substring match 0 (match-beginning 0))
10158 todomatch (substring match (match-end 0)))
10159 (if (string-match "^!" todomatch)
10160 (setq todo-only t todomatch (substring todomatch 1)))
10161 (if (string-match "^\\s-*$" todomatch)
10162 (setq todomatch nil)))
10163 ;; only matching tags
10164 (setq tagsmatch match todomatch nil))
10166 ;; Make the tags matcher
10167 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
10168 (setq tagsmatcher t)
10169 (setq orterms (org-split-string tagsmatch "|") orlist nil)
10170 (while (setq term (pop orterms))
10171 (while (and (equal (substring term -1) "\\") orterms)
10172 (setq term (concat term "|" (pop orterms)))) ; repair bad split
10173 (while (string-match re term)
10174 (setq rest (substring term (match-end 0))
10175 minus (and (match-end 1)
10176 (equal (match-string 1 term) "-"))
10177 tag (match-string 2 term)
10178 re-p (equal (string-to-char tag) ?{)
10179 level-p (match-end 4)
10180 prop-p (match-end 5)
10182 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
10184 (setq level-op (org-op-to-function (match-string 3 term)))
10185 `(,level-op level ,(string-to-number
10186 (match-string 4 term))))
10188 (setq pn (match-string 5 term)
10189 po (match-string 6 term)
10190 pv (match-string 7 term)
10191 cat-p (equal pn "CATEGORY")
10192 re-p (equal (string-to-char pv) ?{)
10193 str-p (equal (string-to-char pv) ?\")
10194 time-p (save-match-data
10195 (string-match "^\"[[<].*[]>]\"$" pv))
10196 pv (if (or re-p str-p) (substring pv 1 -1) pv))
10197 (if time-p (setq pv (org-matcher-time pv)))
10198 (setq po (org-op-to-function po (if time-p 'time str-p)))
10200 ((equal pn "CATEGORY")
10201 (setq gv '(get-text-property (point) 'org-category)))
10205 (setq gv `(org-cached-entry-get nil ,pn))))
10208 `(not (string-match ,pv (or ,gv "")))
10209 `(string-match ,pv (or ,gv "")))
10211 `(,po (or ,gv "") ,pv)
10212 `(,po (string-to-number (or ,gv ""))
10213 ,(string-to-number pv) ))))
10214 (t `(member ,tag tags-list)))
10215 mm (if minus (list 'not mm) mm)
10217 (push mm tagsmatcher))
10218 (push (if (> (length tagsmatcher) 1)
10219 (cons 'and tagsmatcher)
10222 (setq tagsmatcher nil))
10223 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
10225 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
10226 ;; Make the todo matcher
10227 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
10228 (setq todomatcher t)
10229 (setq orterms (org-split-string todomatch "|") orlist nil)
10230 (while (setq term (pop orterms))
10231 (while (string-match re term)
10232 (setq minus (and (match-end 1)
10233 (equal (match-string 1 term) "-"))
10234 kwd (match-string 2 term)
10235 re-p (equal (string-to-char kwd) ?{)
10236 term (substring term (match-end 0))
10238 `(string-match ,(substring kwd 1 -1) todo)
10239 (list 'equal 'todo kwd))
10240 mm (if minus (list 'not mm) mm))
10241 (push mm todomatcher))
10242 (push (if (> (length todomatcher) 1)
10243 (cons 'and todomatcher)
10246 (setq todomatcher nil))
10247 (setq todomatcher (if (> (length orlist) 1)
10248 (cons 'or orlist) (car orlist))))
10250 ;; Return the string and lisp forms of the matcher
10251 (setq matcher (if todomatcher
10252 (list 'and tagsmatcher todomatcher)
10254 (cons match0 matcher)))
10256 (defun org-op-to-function (op &optional stringp)
10257 "Turn an operator into the appropriate function."
10260 ((equal op "<" ) '(< string< org-time<))
10261 ((equal op ">" ) '(> org-string> org-time>))
10262 ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
10263 ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
10264 ((member op '("=" "==")) '(= string= org-time=))
10265 ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
10266 (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
10268 (defun org<> (a b) (not (= a b)))
10269 (defun org-string<= (a b) (or (string= a b) (string< a b)))
10270 (defun org-string>= (a b) (not (string< a b)))
10271 (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
10272 (defun org-string<> (a b) (not (string= a b)))
10273 (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
10274 (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
10275 (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
10276 (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
10277 (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
10278 (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
10280 "Convert S to a floating point time.
10281 If S is already a number, just return it. If it is a string, parse
10282 it as a time string and apply `float-time' to it. If S is nil, just return 0."
10286 (condition-case nil
10287 (float-time (apply 'encode-time (org-parse-time-string s)))
10291 (defun org-time-today ()
10292 "Time in seconds today at 0:00.
10293 Returns the float number of seconds since the beginning of the
10294 epoch to the beginning of today (00:00)."
10295 (float-time (apply 'encode-time
10296 (append '(0 0 0) (nthcdr 3 (decode-time))))))
10298 (defun org-matcher-time (s)
10299 "Interpret a time comparison value."
10302 ((string= s "<now>") (float-time))
10303 ((string= s "<today>") (org-time-today))
10304 ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
10305 ((string= s "<yesterday>") (- (org-time-today) 86400.0))
10306 ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
10307 (+ (org-time-today)
10308 (* (string-to-number (match-string 1 s))
10309 (cdr (assoc (match-string 2 s)
10310 '(("d" . 86400.0) ("w" . 604800.0)
10311 ("m" . 2678400.0) ("y" . 31557600.0)))))))
10314 (defun org-match-any-p (re list)
10315 "Does re match any element of list?"
10316 (setq list (mapcar (lambda (x) (string-match re x)) list))
10319 (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
10320 (defvar org-tags-overlay (org-make-overlay 1 1))
10321 (org-detach-overlay org-tags-overlay)
10323 (defun org-get-local-tags-at (&optional pos)
10324 "Get a list of tags defined in the current headline."
10325 (org-get-tags-at pos 'local))
10327 (defun org-get-local-tags ()
10328 "Get a list of tags defined in the current headline."
10329 (org-get-tags-at nil 'local))
10331 (defun org-get-tags-at (&optional pos local)
10332 "Get a list of all headline tags applicable at POS.
10333 POS defaults to point. If tags are inherited, the list contains
10334 the targets in the same sequence as the headlines appear, i.e.
10335 the tags of the current headline come last.
10336 When LOCAL is non-nil, only return tags from the current headline,
10337 ignore inherited ones."
10339 (if (and org-trust-scanner-tags
10340 (or (not pos) (equal pos (point)))
10343 (let (tags ltags lastpos parent)
10347 (goto-char (or pos (point)))
10350 (condition-case nil
10352 (org-back-to-heading t)
10353 (while (not (equal lastpos (point)))
10354 (setq lastpos (point))
10356 (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
10357 (setq ltags (org-split-string
10358 (org-match-string-no-properties 1) ":"))
10360 (setq ltags (mapcar 'org-add-prop-inherited ltags)))
10363 (org-remove-uniherited-tags ltags)
10366 (or org-use-tag-inheritance (throw 'done t))
10367 (if local (throw 'done t))
10368 (org-up-heading-all 1)
10371 (append (org-remove-uniherited-tags org-file-tags) tags)))))
10373 (defun org-add-prop-inherited (s)
10374 (add-text-properties 0 (length s) '(inherited t) s)
10377 (defun org-toggle-tag (tag &optional onoff)
10378 "Toggle the tag TAG for the current line.
10379 If ONOFF is `on' or `off', don't toggle but set to this state."
10382 (org-back-to-heading t)
10383 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
10386 (setq current (match-string 1))
10387 (replace-match ""))
10389 (setq current (nreverse (org-split-string current ":")))
10393 (or (member tag current) (push tag current)))
10395 (or (not (member tag current)) (setq current (delete tag current))))
10396 (t (if (member tag current)
10397 (setq current (delete tag current))
10399 (push tag current))))
10403 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
10404 (org-set-tags nil t))
10405 (delete-horizontal-space))
10406 (run-hooks 'org-after-tags-change-hook))
10409 (defun org-align-tags-here (to-col)
10410 ;; Assumes that this is a headline
10411 (let ((pos (point)) (col (current-column)) ncol tags-l p)
10412 (beginning-of-line 1)
10413 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
10414 (< pos (match-beginning 2)))
10416 (setq tags-l (- (match-end 2) (match-beginning 2)))
10417 (goto-char (match-beginning 1))
10419 (delete-region (point) (1+ (match-beginning 2)))
10420 (setq ncol (max (1+ (current-column))
10424 (- (abs to-col) tags-l))))
10426 (insert (make-string (- ncol (current-column)) ?\ ))
10427 (setq ncol (current-column))
10428 (when indent-tabs-mode (tabify p (point-at-eol)))
10429 (org-move-to-column (min ncol col) t))
10432 (defun org-set-tags-command (&optional arg just-align)
10433 "Call the set-tags command for the current entry."
10435 (if (org-on-heading-p)
10436 (org-set-tags arg just-align)
10438 (org-back-to-heading t)
10439 (org-set-tags arg just-align))))
10441 (defun org-set-tags (&optional arg just-align)
10442 "Set the tags for the current headline.
10443 With prefix ARG, realign all tags in headings in the current buffer."
10445 (let* ((re (concat "^" outline-regexp))
10446 (current (org-get-tags-string))
10447 (col (current-column))
10448 (org-setting-tags t)
10449 table current-tags inherited-tags ; computed below when needed
10453 (goto-char (point-min))
10454 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
10455 (while (re-search-forward re nil t)
10456 (org-set-tags nil t)
10458 (message "All tags realigned to column %d" org-tags-column))
10460 (setq tags current)
10461 ;; Get a new set of tags from the user
10463 (setq table (append org-tag-persistent-alist
10464 (or org-tag-alist (org-get-buffer-tags)))
10465 org-last-tags-completion-table table
10466 current-tags (org-split-string current ":")
10467 inherited-tags (nreverse
10468 (nthcdr (length current-tags)
10469 (nreverse (org-get-tags-at))))
10471 (if (or (eq t org-use-fast-tag-selection)
10472 (and org-use-fast-tag-selection
10473 (delq nil (mapcar 'cdr table))))
10474 (org-fast-tag-selection
10475 current-tags inherited-tags table
10476 (if org-fast-tag-selection-include-todo org-todo-key-alist))
10477 (let ((org-add-colon-after-tag-completion t))
10479 (org-without-partial-completion
10480 (org-ido-completing-read "Tags: " 'org-tags-completion-function
10481 nil nil current 'org-tags-history)))))))
10482 (while (string-match "[-+&]+" tags)
10483 ;; No boolean logic, just a list
10484 (setq tags (replace-match ":" t t tags))))
10486 (if (string-match "\\`[\t ]*\\'" tags)
10488 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
10489 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
10491 ;; Insert new tags at the correct column
10492 (beginning-of-line 1)
10494 ((and (equal current "") (equal tags "")))
10495 ((re-search-forward
10496 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
10498 (if (equal tags "")
10500 (goto-char (match-beginning 0))
10501 (setq c0 (current-column) p0 (point)
10502 c1 (max (1+ c0) (if (> org-tags-column 0)
10504 (- (- org-tags-column) (length tags))))
10505 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
10506 (replace-match rpl t t)
10507 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
10509 (t (error "Tags alignment failed")))
10510 (org-move-to-column col)
10512 (run-hooks 'org-after-tags-change-hook)))))
10514 (defun org-change-tag-in-region (beg end tag off)
10515 "Add or remove TAG for each entry in the region.
10516 This works in the agenda, and also in an org-mode buffer."
10518 (list (region-beginning) (region-end)
10519 (let ((org-last-tags-completion-table
10521 (org-get-buffer-tags)
10522 (org-global-tags-completion-table))))
10523 (org-ido-completing-read
10524 "Tag: " 'org-tags-completion-function nil nil nil
10525 'org-tags-history))
10527 (message "[s]et or [r]emove? ")
10528 (equal (read-char-exclusive) ?r))))
10529 (if (fboundp 'deactivate-mark) (deactivate-mark))
10530 (let ((agendap (equal major-mode 'org-agenda-mode))
10531 l1 l2 m buf pos newhead (cnt 0))
10533 (setq l2 (1- (org-current-line)))
10535 (setq l1 (org-current-line))
10536 (loop for l from l1 to l2 do
10538 (setq m (get-text-property (point) 'org-hd-marker))
10539 (when (or (and (org-mode-p) (org-on-heading-p))
10541 (setq buf (if agendap (marker-buffer m) (current-buffer))
10542 pos (if agendap m (point)))
10543 (with-current-buffer buf
10547 (setq cnt (1+ cnt))
10548 (org-toggle-tag tag (if off 'off 'on))
10549 (setq newhead (org-get-heading)))))
10550 (and agendap (org-agenda-change-all-lines newhead m))))
10551 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
10553 (defun org-tags-completion-function (string predicate &optional flag)
10554 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
10555 (confirm (lambda (x) (stringp (car x)))))
10556 (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
10557 (setq s1 (match-string 1 string)
10558 s2 (match-string 2 string))
10559 (setq s1 "" s2 string))
10563 (setq rtn (try-completion s2 ctable confirm))
10566 (concat s1 s2 (substring rtn (length s2))
10567 (if (and org-add-colon-after-tag-completion
10568 (assoc rtn ctable))
10573 (all-completions s2 ctable confirm)
10577 (assoc s2 ctable)))
10580 (defun org-fast-tag-insert (kwd tags face &optional end)
10581 "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
10582 (insert (format "%-12s" (concat kwd ":"))
10583 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
10586 (defun org-fast-tag-show-exit (flag)
10589 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
10590 (replace-match ""))
10593 (org-move-to-column (- (window-width) 19) t)
10594 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
10596 (defun org-set-current-tags-overlay (current prefix)
10597 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
10598 (if (featurep 'xemacs)
10599 (org-overlay-display org-tags-overlay (concat prefix s)
10600 'secondary-selection)
10601 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
10602 (org-overlay-display org-tags-overlay (concat prefix s)))))
10604 (defun org-fast-tag-selection (current inherited table &optional todo-table)
10605 "Fast tag selection with single keys.
10606 CURRENT is the current list of tags in the headline, INHERITED is the
10607 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
10608 possibly with grouping information. TODO-TABLE is a similar table with
10609 TODO keywords, should these have keys assigned to them.
10610 If the keys are nil, a-z are automatically assigned.
10611 Returns the new tags string, or nil to not change the current settings."
10612 (let* ((fulltable (append table todo-table))
10613 (maxlen (apply 'max (mapcar
10615 (if (stringp (car x)) (string-width (car x)) 0))
10617 (buf (current-buffer))
10618 (expert (eq org-fast-tag-selection-single-key 'expert))
10620 (fwidth (+ maxlen 3 1 3))
10621 (ncol (/ (- (window-width) 4) fwidth))
10624 tg cnt e c char c1 c2 ntable tbl rtn
10625 ov-start ov-end ov-prefix
10626 (exit-after-next org-fast-tag-selection-single-key)
10627 (done-keywords org-done-keywords)
10630 (beginning-of-line 1)
10632 (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
10633 (setq ov-start (match-beginning 1)
10634 ov-end (match-end 1)
10636 (setq ov-start (1- (point-at-eol))
10637 ov-end (1+ ov-start))
10638 (skip-chars-forward "^\n\r")
10641 (buffer-substring (1- (point)) (point))
10642 (if (> (current-column) org-tags-column)
10644 (make-string (- org-tags-column (current-column)) ?\ ))))))
10645 (org-move-overlay org-tags-overlay ov-start ov-end)
10646 (save-window-excursion
10648 (set-buffer (get-buffer-create " *Org tags*"))
10649 (delete-other-windows)
10650 (split-window-vertically)
10651 (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
10653 (org-set-local 'org-done-keywords done-keywords)
10654 (org-fast-tag-insert "Inherited" inherited i-face "\n")
10655 (org-fast-tag-insert "Current" current c-face "\n\n")
10656 (org-fast-tag-show-exit exit-after-next)
10657 (org-set-current-tags-overlay current ov-prefix)
10658 (setq tbl fulltable char ?a cnt 0)
10659 (while (setq e (pop tbl))
10661 ((equal e '(:startgroup))
10662 (push '() groups) (setq ingroup t)
10663 (when (not (= cnt 0))
10667 ((equal e '(:endgroup))
10668 (setq ingroup nil cnt 0)
10670 ((equal e '(:newline))
10671 (when (not (= cnt 0))
10675 (while (equal (car tbl) '(:newline))
10677 (setq tbl (cdr tbl)))))
10679 (setq tg (car e) c2 nil)
10682 ;; automatically assign a character.
10683 (setq c1 (string-to-char
10684 (downcase (substring
10685 tg (if (= (string-to-char tg) ?@) 1 0)))))
10686 (if (or (rassoc c1 ntable) (rassoc c1 table))
10687 (while (or (rassoc char ntable) (rassoc char table))
10688 (setq char (1+ char)))
10690 (setq c (or c2 char)))
10691 (if ingroup (push tg (car groups)))
10692 (setq tg (org-add-props tg nil 'face
10694 ((not (assoc tg table))
10695 (org-get-todo-face tg))
10696 ((member tg current) c-face)
10697 ((member tg inherited) i-face)
10699 (if (and (= cnt 0) (not ingroup)) (insert " "))
10700 (insert "[" c "] " tg (make-string
10701 (- fwidth 4 (length tg)) ?\ ))
10702 (push (cons tg c) ntable)
10703 (when (= (setq cnt (1+ cnt)) ncol)
10705 (if ingroup (insert " "))
10707 (setq ntable (nreverse ntable))
10709 (goto-char (point-min))
10710 (if (not expert) (org-fit-window-to-buffer))
10714 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
10715 (if groups " [!] no groups" " [!]groups")
10716 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
10717 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
10719 ((= c ?\r) (throw 'exit t))
10721 (setq groups (not groups))
10722 (goto-char (point-min))
10723 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
10726 (org-fast-tag-show-exit
10727 (setq exit-after-next (not exit-after-next)))
10729 (delete-other-windows)
10730 (split-window-vertically)
10731 (org-switch-to-buffer-other-window " *Org tags*")
10732 (org-fit-window-to-buffer)))
10734 (and (= c ?q) (not (rassoc c ntable))))
10735 (org-detach-overlay org-tags-overlay)
10736 (setq quit-flag t))
10739 (if exit-after-next (setq exit-after-next 'now)))
10741 (condition-case nil
10742 (setq tg (org-ido-completing-read
10745 (with-current-buffer buf
10746 (org-get-buffer-tags)))))
10747 (quit (setq tg "")))
10748 (when (string-match "\\S-" tg)
10749 (add-to-list 'buffer-tags (list tg))
10750 (if (member tg current)
10751 (setq current (delete tg current))
10752 (push tg current)))
10753 (if exit-after-next (setq exit-after-next 'now)))
10754 ((setq e (rassoc c todo-table) tg (car e))
10755 (with-current-buffer buf
10756 (save-excursion (org-todo tg)))
10757 (if exit-after-next (setq exit-after-next 'now)))
10758 ((setq e (rassoc c ntable) tg (car e))
10759 (if (member tg current)
10760 (setq current (delete tg current))
10761 (loop for g in groups do
10764 (setq current (delete x current)))
10767 (if exit-after-next (setq exit-after-next 'now))))
10769 ;; Create a sorted list
10773 (assoc b (cdr (memq (assoc a ntable) ntable))))))
10774 (if (eq exit-after-next 'now) (throw 'exit t))
10775 (goto-char (point-min))
10776 (beginning-of-line 2)
10777 (delete-region (point) (point-at-eol))
10778 (org-fast-tag-insert "Current" current c-face)
10779 (org-set-current-tags-overlay current ov-prefix)
10780 (while (re-search-forward
10781 (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
10782 (setq tg (match-string 1))
10783 (add-text-properties
10784 (match-beginning 1) (match-end 1)
10787 ((member tg current) c-face)
10788 ((member tg inherited) i-face)
10789 (t (get-text-property (match-beginning 1) 'face))))))
10790 (goto-char (point-min)))))
10791 (org-detach-overlay org-tags-overlay)
10793 (mapconcat 'identity current ":")
10796 (defun org-get-tags-string ()
10797 "Get the TAGS string in the current headline."
10798 (unless (org-on-heading-p t)
10799 (error "Not on a heading"))
10801 (beginning-of-line 1)
10802 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
10803 (org-match-string-no-properties 1)
10806 (defun org-get-tags ()
10807 "Get the list of tags specified in the current headline."
10808 (org-split-string (org-get-tags-string) ":"))
10810 (defun org-get-buffer-tags ()
10811 "Get a table of all tags used in the buffer, for completion."
10814 (goto-char (point-min))
10815 (while (re-search-forward
10816 (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
10817 (when (equal (char-after (point-at-bol 0)) ?*)
10818 (mapc (lambda (x) (add-to-list 'tags x))
10819 (org-split-string (org-match-string-no-properties 1) ":")))))
10820 (mapcar 'list tags)))
10822 ;;;; The mapping API
10825 (defun org-map-entries (func &optional match scope &rest skip)
10826 "Call FUNC at each headline selected by MATCH in SCOPE.
10828 FUNC is a function or a lisp form. The function will be called without
10829 arguments, with the cursor positioned at the beginning of the headline.
10830 The return values of all calls to the function will be collected and
10831 returned as a list.
10833 MATCH is a tags/property/todo match as it is used in the agenda tags view.
10834 Only headlines that are matched by this query will be considered during
10835 the iteration. When MATCH is nil or t, all headlines will be
10836 visited by the iteration.
10838 SCOPE determines the scope of this command. It can be any of:
10840 nil The current buffer, respecting the restriction if any
10841 tree The subtree started with the entry at point
10842 file The current buffer, without restriction
10844 The current buffer, and any archives associated with it
10845 agenda All agenda files
10846 agenda-with-archives
10847 All agenda files with any archive files associated with them
10849 If this is a list, all files in the list will be scanned
10851 The remaining args are treated as settings for the skipping facilities of
10852 the scanner. The following items can be given here:
10854 archive skip trees with the archive tag.
10855 comment skip trees with the COMMENT keyword
10856 function or Emacs Lisp form:
10857 will be used as value for `org-agenda-skip-function', so whenever
10858 the the function returns t, FUNC will not be called for that
10859 entry and search will continue from the point where the
10860 function leaves it.
10862 If your function needs to retrieve the tags including inherited tags
10863 at the *current* entry, you can use the value of the variable
10864 `org-scanner-tags' which will be much faster than getting the value
10865 with `org-get-tags-at'. If your function gets properties with
10866 `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
10867 to t around the call to `org-entry-properties' to get the same speedup.
10868 Note that if your function moves around to retrieve tags and properties at
10869 a *different* entry, you cannot use these techniques."
10870 (let* ((org-agenda-archives-mode nil) ; just to make sure
10871 (org-agenda-skip-archived-trees (memq 'archive skip))
10872 (org-agenda-skip-comment-trees (memq 'comment skip))
10873 (org-agenda-skip-function
10874 (car (org-delete-all '(comment archive) skip)))
10875 (org-tags-match-list-sublevels t)
10877 org-todo-keywords-for-agenda
10878 org-done-keywords-for-agenda
10879 org-todo-keyword-alist-for-agenda
10880 org-tag-alist-for-agenda)
10883 ((eq match t) (setq matcher t))
10884 ((eq match nil) (setq matcher t))
10885 (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
10889 (when (eq scope 'tree)
10890 (org-back-to-heading t)
10891 (org-narrow-to-subtree)
10896 (org-prepare-agenda-buffers
10897 (list (buffer-file-name (current-buffer))))
10898 (setq res (org-scan-tags func matcher)))
10899 ;; Get the right scope
10901 ((and scope (listp scope) (symbolp (car scope)))
10902 (setq scope (eval scope)))
10903 ((eq scope 'agenda)
10904 (setq scope (org-agenda-files t)))
10905 ((eq scope 'agenda-with-archives)
10906 (setq scope (org-agenda-files t))
10907 (setq scope (org-add-archive-files scope)))
10909 (setq scope (list (buffer-file-name))))
10910 ((eq scope 'file-with-archives)
10911 (setq scope (org-add-archive-files (list (buffer-file-name))))))
10912 (org-prepare-agenda-buffers scope)
10913 (while (setq file (pop scope))
10914 (with-current-buffer (org-find-base-buffer-visiting file)
10918 (goto-char (point-min))
10919 (setq res (append res (org-scan-tags func matcher))))))))))
10924 ;;; Setting and retrieving properties
10926 (defconst org-special-properties
10927 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
10928 "TIMESTAMP" "TIMESTAMP_IA")
10929 "The special properties valid in Org-mode.
10931 These are properties that are not defined in the property drawer,
10932 but in some other way.")
10934 (defconst org-default-properties
10935 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
10936 "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
10937 "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
10938 "EXPORT_FILE_NAME" "EXPORT_TITLE" "ORDERED")
10939 "Some properties that are used by Org-mode for various purposes.
10940 Being in this list makes sure that they are offered for completion.")
10942 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
10943 "Regular expression matching the first line of a property drawer.")
10945 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
10946 "Regular expression matching the first line of a property drawer.")
10948 (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
10949 "Regular expression matching the first line of a property drawer.")
10951 (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
10952 "Regular expression matching the first line of a property drawer.")
10954 (defconst org-property-drawer-re
10955 (concat "\\(" org-property-start-re "\\)[^\000]*\\("
10956 org-property-end-re "\\)\n?")
10957 "Matches an entire property drawer.")
10959 (defconst org-clock-drawer-re
10960 (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
10961 org-property-end-re "\\)\n?")
10962 "Matches an entire clock drawer.")
10964 (defun org-property-action ()
10965 "Do an action on properties."
10968 (org-at-property-p)
10969 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
10970 (setq c (read-char-exclusive))
10973 (call-interactively 'org-set-property))
10975 (call-interactively 'org-delete-property))
10977 (call-interactively 'org-delete-property-globally))
10979 (call-interactively 'org-compute-property-at-point))
10980 (t (error "No such property action %c" c)))))
10982 (defun org-at-property-p ()
10983 "Is the cursor in a property line?"
10984 ;; FIXME: Does not check if we are actually in the drawer.
10985 ;; FIXME: also returns true on any drawers.....
10986 ;; This is used by C-c C-c for property action.
10988 (beginning-of-line 1)
10989 (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
10991 (defun org-get-property-block (&optional beg end force)
10992 "Return the (beg . end) range of the body of the property drawer.
10993 BEG and END can be beginning and end of subtree, if not given
10994 they will be found.
10995 If the drawer does not exist and FORCE is non-nil, create the drawer."
10998 (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
10999 (end (or end (progn (outline-next-heading) (point)))))
11001 (if (re-search-forward org-property-start-re end t)
11002 (setq beg (1+ (match-end 0)))
11005 (org-insert-property-drawer)
11006 (setq end (progn (outline-next-heading) (point))))
11009 (if (re-search-forward org-property-start-re end t)
11010 (setq beg (1+ (match-end 0)))))
11011 (if (re-search-forward org-property-end-re end t)
11012 (setq end (match-beginning 0))
11013 (or force (throw 'exit nil))
11016 (org-indent-line-function)
11017 (insert ":END:\n"))
11020 (defun org-entry-properties (&optional pom which)
11021 "Get all properties of the entry at point-or-marker POM.
11022 This includes the TODO keyword, the tags, time strings for deadline,
11023 scheduled, and clocking, and any additional properties defined in the
11024 entry. The return value is an alist, keys may occur multiple times
11025 if the property key was used several times.
11026 POM may also be nil, in which case the current entry is used.
11027 If WHICH is nil or `all', get all properties. If WHICH is
11028 `special' or `standard', only get that subclass."
11029 (setq which (or which 'all))
11030 (org-with-point-at pom
11031 (let ((clockstr (substring org-clock-string 0 -1))
11032 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
11033 beg end range props sum-props key value string clocksum)
11035 (when (condition-case nil
11036 (and (org-mode-p) (org-back-to-heading t))
11039 (setq sum-props (get-text-property (point) 'org-summaries))
11040 (setq clocksum (get-text-property (point) :org-clock-minutes))
11041 (outline-next-heading)
11043 (when (memq which '(all special))
11044 ;; Get the special properties, like TODO and tags
11046 (when (and (looking-at org-todo-line-regexp) (match-end 2))
11047 (push (cons "TODO" (org-match-string-no-properties 2)) props))
11048 (when (looking-at org-priority-regexp)
11049 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
11050 (when (and (setq value (org-get-tags-string))
11051 (string-match "\\S-" value))
11052 (push (cons "TAGS" value) props))
11053 (when (setq value (org-get-tags-at))
11054 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
11056 (while (re-search-forward org-maybe-keyword-time-regexp end t)
11057 (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
11058 string (if (equal key clockstr)
11062 (match-beginning 3) (goto-char (point-at-eol)))))
11063 (substring (org-match-string-no-properties 3) 1 -1)))
11065 (if (= (char-after (match-beginning 3)) ?\[)
11066 (setq key "TIMESTAMP_IA")
11067 (setq key "TIMESTAMP")))
11068 (when (or (equal key clockstr) (not (assoc key props)))
11069 (push (cons key string) props)))
11073 (when (memq which '(all standard))
11074 ;; Get the standard properties, like :PROP: ...
11075 (setq range (org-get-property-block beg end))
11077 (goto-char (car range))
11078 (while (re-search-forward
11079 (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
11081 (setq key (org-match-string-no-properties 1)
11082 value (org-trim (or (org-match-string-no-properties 2) "")))
11083 (unless (member key excluded)
11084 (push (cons key (or value "")) props)))))
11086 (push (cons "CLOCKSUM"
11087 (org-columns-number-to-string (/ (float clocksum) 60.)
11090 (unless (assoc "CATEGORY" props)
11091 (setq value (or (org-get-category)
11092 (progn (org-refresh-category-properties)
11093 (org-get-category))))
11094 (push (cons "CATEGORY" value) props))
11095 (append sum-props (nreverse props)))))))
11097 (defun org-entry-get (pom property &optional inherit)
11098 "Get value of PROPERTY for entry at point-or-marker POM.
11099 If INHERIT is non-nil and the entry does not have the property,
11100 then also check higher levels of the hierarchy.
11101 If INHERIT is the symbol `selective', use inheritance only if the setting
11102 in `org-use-property-inheritance' selects PROPERTY for inheritance.
11103 If the property is present but empty, the return value is the empty string.
11104 If the property is not present at all, nil is returned."
11105 (org-with-point-at pom
11106 (if (and inherit (if (eq inherit 'selective)
11107 (org-property-inherit-p property)
11109 (org-entry-get-with-inheritance property)
11110 (if (member property org-special-properties)
11111 ;; We need a special property. Use brute force, get all properties.
11112 (cdr (assoc property (org-entry-properties nil 'special)))
11113 (let ((range (org-get-property-block)))
11115 (goto-char (car range))
11117 (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
11119 ;; Found the property, return it.
11121 (org-match-string-no-properties 1)
11124 (defun org-property-or-variable-value (var &optional inherit)
11125 "Check if there is a property fixing the value of VAR.
11126 If yes, return this value. If not, return the current value of the variable."
11127 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
11128 (if (and prop (stringp prop) (string-match "\\S-" prop))
11130 (symbol-value var))))
11132 (defun org-entry-delete (pom property)
11133 "Delete the property PROPERTY from entry at point-or-marker POM."
11134 (org-with-point-at pom
11135 (if (member property org-special-properties)
11136 nil ; cannot delete these properties.
11137 (let ((range (org-get-property-block)))
11139 (goto-char (car range))
11141 (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
11144 (delete-region (match-beginning 0) (1+ (point-at-eol)))
11148 ;; Multi-values properties are properties that contain multiple values
11149 ;; These values are assumed to be single words, separated by whitespace.
11150 (defun org-entry-add-to-multivalued-property (pom property value)
11151 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
11152 (let* ((old (org-entry-get pom property))
11153 (values (and old (org-split-string old "[ \t]"))))
11154 (setq value (org-entry-protect-space value))
11155 (unless (member value values)
11156 (setq values (cons value values))
11157 (org-entry-put pom property
11158 (mapconcat 'identity values " ")))))
11160 (defun org-entry-remove-from-multivalued-property (pom property value)
11161 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
11162 (let* ((old (org-entry-get pom property))
11163 (values (and old (org-split-string old "[ \t]"))))
11164 (setq value (org-entry-protect-space value))
11165 (when (member value values)
11166 (setq values (delete value values))
11167 (org-entry-put pom property
11168 (mapconcat 'identity values " ")))))
11170 (defun org-entry-member-in-multivalued-property (pom property value)
11171 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
11172 (let* ((old (org-entry-get pom property))
11173 (values (and old (org-split-string old "[ \t]"))))
11174 (setq value (org-entry-protect-space value))
11175 (member value values)))
11177 (defun org-entry-get-multivalued-property (pom property)
11178 "Return a list of values in a multivalued property."
11179 (let* ((value (org-entry-get pom property))
11180 (values (and value (org-split-string value "[ \t]"))))
11181 (mapcar 'org-entry-restore-space values)))
11183 (defun org-entry-put-multivalued-property (pom property &rest values)
11184 "Set multivalued PROPERTY at point-or-marker POM to VALUES.
11185 VALUES should be a list of strings. Spaces will be protected."
11186 (org-entry-put pom property
11187 (mapconcat 'org-entry-protect-space values " "))
11188 (let* ((value (org-entry-get pom property))
11189 (values (and value (org-split-string value "[ \t]"))))
11190 (mapcar 'org-entry-restore-space values)))
11192 (defun org-entry-protect-space (s)
11193 "Protect spaces and newline in string S."
11194 (while (string-match " " s)
11195 (setq s (replace-match "%20" t t s)))
11196 (while (string-match "\n" s)
11197 (setq s (replace-match "%0A" t t s)))
11200 (defun org-entry-restore-space (s)
11201 "Restore spaces and newline in string S."
11202 (while (string-match "%20" s)
11203 (setq s (replace-match " " t t s)))
11204 (while (string-match "%0A" s)
11205 (setq s (replace-match "\n" t t s)))
11208 (defvar org-entry-property-inherited-from (make-marker)
11209 "Marker pointing to the entry from where a property was inherited.
11210 Each call to `org-entry-get-with-inheritance' will set this marker to the
11211 location of the entry where the inheritance search matched. If there was
11212 no match, the marker will point nowhere.
11213 Note that also `org-entry-get' calls this function, if the INHERIT flag
11216 (defun org-entry-get-with-inheritance (property)
11217 "Get entry property, and search higher levels if not present."
11218 (move-marker org-entry-property-inherited-from nil)
11225 (when (setq tmp (org-entry-get nil property))
11226 (org-back-to-heading t)
11227 (move-marker org-entry-property-inherited-from (point))
11229 (or (org-up-heading-safe) (throw 'ex nil)))))
11231 (cdr (assoc property org-file-properties))
11232 (cdr (assoc property org-global-properties))
11233 (cdr (assoc property org-global-properties-fixed))))))
11235 (defun org-entry-put (pom property value)
11236 "Set PROPERTY to VALUE for entry at point-or-marker POM."
11237 (org-with-point-at pom
11238 (org-back-to-heading t)
11239 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
11242 ((equal property "TODO")
11243 (when (and (stringp value) (string-match "\\S-" value)
11244 (not (member value org-todo-keywords-1)))
11245 (error "\"%s\" is not a valid TODO state" value))
11246 (if (or (not value)
11247 (not (string-match "\\S-" value)))
11248 (setq value 'none))
11250 (org-set-tags nil 'align))
11251 ((equal property "PRIORITY")
11252 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
11253 (string-to-char value) ?\ ))
11254 (org-set-tags nil 'align))
11255 ((equal property "SCHEDULED")
11256 (if (re-search-forward org-scheduled-time-regexp end t)
11258 ((eq value 'earlier) (org-timestamp-change -1 'day))
11259 ((eq value 'later) (org-timestamp-change 1 'day))
11260 (t (call-interactively 'org-schedule)))
11261 (call-interactively 'org-schedule)))
11262 ((equal property "DEADLINE")
11263 (if (re-search-forward org-deadline-time-regexp end t)
11265 ((eq value 'earlier) (org-timestamp-change -1 'day))
11266 ((eq value 'later) (org-timestamp-change 1 'day))
11267 (t (call-interactively 'org-deadline)))
11268 (call-interactively 'org-deadline)))
11269 ((member property org-special-properties)
11270 (error "The %s property can not yet be set with `org-entry-put'"
11272 (t ; a non-special property
11273 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
11274 (setq range (org-get-property-block beg end 'force))
11275 (goto-char (car range))
11276 (if (re-search-forward
11277 (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
11279 (delete-region (match-beginning 1) (match-end 1))
11280 (goto-char (match-beginning 1)))
11281 (goto-char (cdr range))
11284 (org-indent-line-function)
11285 (insert ":" property ":"))
11286 (and value (insert " " value))
11287 (org-indent-line-function)))))))
11289 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
11290 "Get all property keys in the current buffer.
11291 With INCLUDE-SPECIALS, also list the special properties that reflect things
11292 like tags and TODO state.
11293 With INCLUDE-DEFAULTS, also include properties that has special meaning
11294 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
11295 With INCLUDE-COLUMNS, also include property names given in COLUMN
11296 formats in the current buffer."
11297 (let (rtn range cfmt s p)
11301 (goto-char (point-min))
11302 (while (re-search-forward org-property-start-re nil t)
11303 (setq range (org-get-property-block))
11304 (goto-char (car range))
11305 (while (re-search-forward
11306 (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
11308 (add-to-list 'rtn (org-match-string-no-properties 1)))
11309 (outline-next-heading))))
11311 (when include-specials
11312 (setq rtn (append org-special-properties rtn)))
11314 (when include-defaults
11315 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
11317 (when include-columns
11321 (goto-char (point-min))
11322 (while (re-search-forward
11323 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
11325 (setq cfmt (match-string 2) s 0)
11326 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
11328 (setq s (match-end 0)
11329 p (match-string 1 cfmt))
11330 (unless (or (equal p "ITEM")
11331 (member p org-special-properties))
11332 (add-to-list 'rtn (match-string 1 cfmt))))))))
11334 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
11336 (defun org-property-values (key)
11337 "Return a list of all values of property KEY."
11341 (goto-char (point-min))
11342 (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
11344 (while (re-search-forward re nil t)
11345 (add-to-list 'values (org-trim (match-string 1))))
11346 (delete "" values)))))
11348 (defun org-insert-property-drawer ()
11349 "Insert a property drawer into the current entry."
11351 (org-back-to-heading t)
11352 (looking-at outline-regexp)
11353 (let ((indent (- (match-end 0)(match-beginning 0)))
11355 (re (concat "^[ \t]*" org-keyword-time-regexp))
11357 (outline-next-heading)
11360 (while (re-search-forward re end t))
11361 (setq hiddenp (org-invisible-p))
11363 (and (equal (char-after) ?\n) (forward-char 1))
11364 (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
11365 (if (member (match-string 1) '("CLOCK:" ":END:"))
11366 ;; just skip this line
11367 (beginning-of-line 2)
11368 ;; Drawer start, find the end
11369 (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
11370 (beginning-of-line 1)))
11371 (org-skip-over-state-notes)
11372 (skip-chars-backward " \t\n\r")
11373 (if (eq (char-before) ?*) (forward-char 1))
11374 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
11375 (beginning-of-line 0)
11376 (org-indent-to-column indent)
11377 (beginning-of-line 2)
11378 (org-indent-to-column indent)
11379 (beginning-of-line 0)
11382 (org-back-to-heading t)
11384 (org-flag-drawer t))))
11386 (defun org-set-property (property value)
11387 "In the current entry, set PROPERTY to VALUE.
11388 When called interactively, this will prompt for a property name, offering
11389 completion on existing and default properties. And then it will prompt
11390 for a value, offering completion either on allowed values (via an inherited
11391 xxx_ALL property) or on existing values in other instances of this property
11392 in the current file."
11394 (let* ((completion-ignore-case t)
11395 (keys (org-buffer-property-keys nil t t))
11396 (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
11397 (prop (if (member prop0 keys)
11399 (or (cdr (assoc (downcase prop0)
11400 (mapcar (lambda (x) (cons (downcase x) x))
11403 (cur (org-entry-get nil prop))
11404 (allowed (org-property-get-allowed-values nil prop 'table))
11405 (existing (mapcar 'list (org-property-values prop)))
11407 (org-completing-read "Value: " allowed nil 'req-match)
11408 (let (org-completion-use-ido)
11409 (org-completing-read
11410 (concat "Value" (if (and cur (string-match "\\S-" cur))
11411 (concat "[" cur "]") "")
11413 existing nil nil "" nil cur)))))
11414 (list prop (if (equal val "") cur val))))
11415 (unless (equal (org-entry-get nil property) value)
11416 (org-entry-put nil property value)))
11418 (defun org-delete-property (property)
11419 "In the current entry, delete PROPERTY."
11421 (let* ((completion-ignore-case t)
11422 (prop (org-ido-completing-read
11423 "Property: " (org-entry-properties nil 'standard))))
11425 (message "Property %s %s" property
11426 (if (org-entry-delete nil property)
11428 "was not present in the entry")))
11430 (defun org-delete-property-globally (property)
11431 "Remove PROPERTY globally, from all entries."
11433 (let* ((completion-ignore-case t)
11434 (prop (org-ido-completing-read
11435 "Globally remove property: "
11436 (mapcar 'list (org-buffer-property-keys)))))
11441 (goto-char (point-min))
11443 (while (re-search-forward
11444 (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
11446 (setq cnt (1+ cnt))
11447 (replace-match ""))
11448 (message "Property \"%s\" removed from %d entries" property cnt)))))
11450 (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
11452 (defun org-compute-property-at-point ()
11453 "Compute the property at point.
11454 This looks for an enclosing column format, extracts the operator and
11455 then applies it to the property in the column format's scope."
11457 (unless (org-at-property-p)
11458 (error "Not at a property"))
11459 (let ((prop (org-match-string-no-properties 2)))
11460 (org-columns-get-format-and-top-level)
11461 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
11462 (error "No operator defined for property %s" prop))
11463 (org-columns-compute prop)))
11465 (defun org-property-get-allowed-values (pom property &optional table)
11466 "Get allowed values for the property PROPERTY.
11467 When TABLE is non-nil, return an alist that can directly be used for
11471 ((equal property "TODO")
11472 (setq vals (org-with-point-at pom
11473 (append org-todo-keywords-1 '("")))))
11474 ((equal property "PRIORITY")
11475 (let ((n org-lowest-priority))
11476 (while (>= n org-highest-priority)
11477 (push (char-to-string n) vals)
11479 ((member property org-special-properties))
11481 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
11483 (when (and vals (string-match "\\S-" vals))
11484 (setq vals (car (read-from-string (concat "(" vals ")"))))
11485 (setq vals (mapcar (lambda (x)
11486 (cond ((stringp x) x)
11487 ((numberp x) (number-to-string x))
11488 ((symbolp x) (symbol-name x))
11491 (if table (mapcar 'list vals) vals)))
11493 (defun org-property-previous-allowed-value (&optional previous)
11494 "Switch to the next allowed value for this property."
11496 (org-property-next-allowed-value t))
11498 (defun org-property-next-allowed-value (&optional previous)
11499 "Switch to the next allowed value for this property."
11501 (unless (org-at-property-p)
11502 (error "Not at a property"))
11503 (let* ((key (match-string 2))
11504 (value (match-string 3))
11505 (allowed (or (org-property-get-allowed-values (point) key)
11506 (and (member value '("[ ]" "[-]" "[X]"))
11510 (error "Allowed values for this property have not been defined"))
11511 (if previous (setq allowed (reverse allowed)))
11512 (if (member value allowed)
11513 (setq nval (car (cdr (member value allowed)))))
11514 (setq nval (or nval (car allowed)))
11515 (if (equal nval value)
11516 (error "Only one allowed value for this property"))
11517 (org-at-property-p)
11518 (replace-match (concat " :" key ": " nval) t t)
11519 (org-indent-line-function)
11520 (beginning-of-line 1)
11521 (skip-chars-forward " \t")))
11523 (defun org-find-entry-with-id (ident)
11524 "Locate the entry that contains the ID property with exact value IDENT.
11525 IDENT can be a string, a symbol or a number, this function will search for
11526 the string representation of it.
11527 Return the position where this entry starts, or nil if there is no such entry."
11528 (interactive "sID: ")
11530 ((stringp ident) ident)
11531 ((symbol-name ident) (symbol-name ident))
11532 ((numberp ident) (number-to-string ident))
11533 (t (error "IDENT %s must be a string, symbol or number" ident))))
11534 (case-fold-search nil))
11538 (goto-char (point-min))
11539 (when (re-search-forward
11540 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
11542 (org-back-to-heading)
11547 (defvar org-last-changed-timestamp nil)
11548 (defvar org-last-inserted-timestamp nil
11549 "The last time stamp inserted with `org-insert-time-stamp'.")
11550 (defvar org-time-was-given) ; dynamically scoped parameter
11551 (defvar org-end-time-was-given) ; dynamically scoped parameter
11552 (defvar org-ts-what) ; dynamically scoped parameter
11554 (defun org-time-stamp (arg &optional inactive)
11555 "Prompt for a date/time and insert a time stamp.
11556 If the user specifies a time like HH:MM, or if this command is called
11557 with a prefix argument, the time stamp will contain date and time.
11558 Otherwise, only the date will be included. All parts of a date not
11559 specified by the user will be filled in from the current date/time.
11560 So if you press just return without typing anything, the time stamp
11561 will represent the current date/time. If there is already a timestamp
11562 at the cursor, it will be modified."
11566 ;; Default time is either today, or, when entering a range,
11567 ;; the range start.
11568 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
11570 (re-search-backward
11571 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
11572 (- (point) 20) t)))
11573 (apply 'encode-time (org-parse-time-string (match-string 1)))
11575 (default-input (and ts (org-get-compact-tod ts)))
11576 org-time-was-given org-end-time-was-given time)
11578 ((and (org-at-timestamp-p t)
11579 (memq last-command '(org-time-stamp org-time-stamp-inactive))
11580 (memq this-command '(org-time-stamp org-time-stamp-inactive)))
11582 (setq time (let ((this-command this-command))
11583 (org-read-date arg 'totime nil nil
11584 default-time default-input)))
11585 (org-insert-time-stamp time (or org-time-was-given arg) inactive))
11586 ((org-at-timestamp-p t)
11587 (setq time (let ((this-command this-command))
11588 (org-read-date arg 'totime nil nil default-time default-input)))
11589 (when (org-at-timestamp-p t) ; just to get the match data
11590 ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
11592 (setq org-last-changed-timestamp
11593 (org-insert-time-stamp
11594 time (or org-time-was-given arg)
11595 inactive nil nil (list org-end-time-was-given))))
11596 (message "Timestamp updated"))
11598 (setq time (let ((this-command this-command))
11599 (org-read-date arg 'totime nil nil default-time default-input)))
11600 (org-insert-time-stamp time (or org-time-was-given arg) inactive
11601 nil nil (list org-end-time-was-given))))))
11603 ;; FIXME: can we use this for something else, like computing time differences?
11604 (defun org-get-compact-tod (s)
11605 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
11606 (let* ((t1 (match-string 1 s))
11607 (h1 (string-to-number (match-string 2 s)))
11608 (m1 (string-to-number (match-string 3 s)))
11609 (t2 (and (match-end 4) (match-string 5 s)))
11610 (h2 (and t2 (string-to-number (match-string 6 s))))
11611 (m2 (and t2 (string-to-number (match-string 7 s))))
11615 (setq dh (- h2 h1) dm (- m2 m1))
11616 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
11617 (concat t1 "+" (number-to-string dh)
11618 (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
11620 (defun org-time-stamp-inactive (&optional arg)
11621 "Insert an inactive time stamp.
11622 An inactive time stamp is enclosed in square brackets instead of angle
11623 brackets. It is inactive in the sense that it does not trigger agenda entries,
11624 does not link to the calendar and cannot be changed with the S-cursor keys.
11625 So these are more for recording a certain time/date."
11627 (org-time-stamp arg 'inactive))
11629 (defvar org-date-ovl (org-make-overlay 1 1))
11630 (org-overlay-put org-date-ovl 'face 'org-warning)
11631 (org-detach-overlay org-date-ovl)
11633 (defvar org-ans1) ; dynamically scoped parameter
11634 (defvar org-ans2) ; dynamically scoped parameter
11636 (defvar org-plain-time-of-day-regexp) ; defined below
11638 (defvar org-overriding-default-time nil) ; dynamically scoped
11639 (defvar org-read-date-overlay nil)
11640 (defvar org-dcst nil) ; dynamically scoped
11641 (defvar org-read-date-history nil)
11643 (defun org-read-date (&optional with-time to-time from-string prompt
11644 default-time default-input)
11645 "Read a date, possibly a time, and make things smooth for the user.
11646 The prompt will suggest to enter an ISO date, but you can also enter anything
11647 which will at least partially be understood by `parse-time-string'.
11648 Unrecognized parts of the date will default to the current day, month, year,
11649 hour and minute. If this command is called to replace a timestamp at point,
11650 of to enter the second timestamp of a range, the default time is taken from the
11651 existing stamp. For example,
11652 3-2-5 --> 2003-02-05
11653 feb 15 --> currentyear-02-15
11654 sep 12 9 --> 2009-09-12
11655 12:45 --> today 12:45
11656 22 sept 0:34 --> currentyear-09-22 0:34
11657 12 --> currentyear-currentmonth-12
11658 Fri --> nearest Friday (today or later)
11661 Furthermore you can specify a relative date by giving, as the *first* thing
11662 in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
11663 change in days weeks, months, years.
11664 With a single plus or minus, the date is relative to today. With a double
11665 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
11666 +4d --> four days from today
11667 +4 --> same as above
11668 +2w --> two weeks from today
11669 ++5 --> five days from default date
11671 The function understands only English month and weekday abbreviations,
11672 but this can be configured with the variables `parse-time-months' and
11673 `parse-time-weekdays'.
11675 While prompting, a calendar is popped up - you can also select the
11676 date with the mouse (button 1). The calendar shows a period of three
11677 months. To scroll it to other months, use the keys `>' and `<'.
11678 If you don't like the calendar, turn it off with
11679 \(setq org-read-date-popup-calendar nil)
11681 With optional argument TO-TIME, the date will immediately be converted
11682 to an internal time.
11683 With an optional argument WITH-TIME, the prompt will suggest to also
11684 insert a time. Note that when WITH-TIME is not set, you can still
11685 enter a time, and this function will inform the calling routine about
11686 this change. The calling routine may then choose to change the format
11687 used to insert the time stamp into the buffer to include the time.
11688 With optional argument FROM-STRING, read from this string instead from
11689 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
11690 the time/date that is used for everything that is not specified by the
11692 (require 'parse-time)
11693 (let* ((org-time-stamp-rounding-minutes
11694 (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
11695 (org-dcst org-display-custom-times)
11696 (ct (org-current-time))
11697 (def (or org-overriding-default-time default-time ct))
11698 (defdecode (decode-time def))
11700 (when (< (nth 2 defdecode) org-extend-today-until)
11701 (setcar (nthcdr 2 defdecode) -1)
11702 (setcar (nthcdr 1 defdecode) 59)
11703 (setq def (apply 'encode-time defdecode)
11704 defdecode (decode-time def)))))
11705 (calendar-move-hook nil)
11706 (calendar-view-diary-initially-flag nil)
11707 (view-diary-entries-initially nil)
11708 (calendar-view-holidays-initially-flag nil)
11709 (view-calendar-holidays-initially nil)
11710 (timestr (format-time-string
11711 (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
11712 (prompt (concat (if prompt (concat prompt " ") "")
11713 (format "Date+time [%s]: " timestr)))
11714 ans (org-ans0 "") org-ans1 org-ans2 final)
11717 (from-string (setq ans from-string))
11718 (org-read-date-popup-calendar
11720 (save-window-excursion
11722 (calendar-forward-day (- (time-to-days def)
11723 (calendar-absolute-from-gregorian
11724 (calendar-current-date))))
11725 (org-eval-in-calendar nil t)
11726 (let* ((old-map (current-local-map))
11727 (map (copy-keymap calendar-mode-map))
11728 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
11729 (org-defkey map (kbd "RET") 'org-calendar-select)
11730 (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
11731 'org-calendar-select-mouse)
11732 (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2
])
11733 'org-calendar-select-mouse
)
11734 (org-defkey minibuffer-local-map
[(meta shift left
)]
11735 (lambda () (interactive)
11736 (org-eval-in-calendar '(calendar-backward-month 1))))
11737 (org-defkey minibuffer-local-map
[(meta shift right
)]
11738 (lambda () (interactive)
11739 (org-eval-in-calendar '(calendar-forward-month 1))))
11740 (org-defkey minibuffer-local-map
[(meta shift up
)]
11741 (lambda () (interactive)
11742 (org-eval-in-calendar '(calendar-backward-year 1))))
11743 (org-defkey minibuffer-local-map
[(meta shift down
)]
11744 (lambda () (interactive)
11745 (org-eval-in-calendar '(calendar-forward-year 1))))
11746 (org-defkey minibuffer-local-map
[(shift up
)]
11747 (lambda () (interactive)
11748 (org-eval-in-calendar '(calendar-backward-week 1))))
11749 (org-defkey minibuffer-local-map
[(shift down
)]
11750 (lambda () (interactive)
11751 (org-eval-in-calendar '(calendar-forward-week 1))))
11752 (org-defkey minibuffer-local-map
[(shift left
)]
11753 (lambda () (interactive)
11754 (org-eval-in-calendar '(calendar-backward-day 1))))
11755 (org-defkey minibuffer-local-map
[(shift right
)]
11756 (lambda () (interactive)
11757 (org-eval-in-calendar '(calendar-forward-day 1))))
11758 (org-defkey minibuffer-local-map
">"
11759 (lambda () (interactive)
11760 (org-eval-in-calendar '(scroll-calendar-left 1))))
11761 (org-defkey minibuffer-local-map
"<"
11762 (lambda () (interactive)
11763 (org-eval-in-calendar '(scroll-calendar-right 1))))
11764 (run-hooks 'org-read-date-minibuffer-setup-hook
)
11767 (use-local-map map
)
11768 (add-hook 'post-command-hook
'org-read-date-display
)
11769 (setq org-ans0
(read-string prompt default-input
11770 'org-read-date-history nil
))
11771 ;; org-ans0: from prompt
11772 ;; org-ans1: from mouse click
11773 ;; org-ans2: from calendar motion
11774 (setq ans
(concat org-ans0
" " (or org-ans1 org-ans2
))))
11775 (remove-hook 'post-command-hook
'org-read-date-display
)
11776 (use-local-map old-map
)
11777 (when org-read-date-overlay
11778 (org-delete-overlay org-read-date-overlay
)
11779 (setq org-read-date-overlay nil
)))))))
11781 (t ; Naked prompt only
11783 (setq ans
(read-string prompt default-input
11784 'org-read-date-history timestr
))
11785 (when org-read-date-overlay
11786 (org-delete-overlay org-read-date-overlay
)
11787 (setq org-read-date-overlay nil
)))))
11789 (setq final
(org-read-date-analyze ans def defdecode
))
11792 (apply 'encode-time final
)
11793 (if (and (boundp 'org-time-was-given
) org-time-was-given
)
11794 (format "%04d-%02d-%02d %02d:%02d"
11795 (nth 5 final
) (nth 4 final
) (nth 3 final
)
11796 (nth 2 final
) (nth 1 final
))
11797 (format "%04d-%02d-%02d" (nth 5 final
) (nth 4 final
) (nth 3 final
))))))
11801 (defun org-read-date-display ()
11802 "Display the current date prompt interpretation in the minibuffer."
11803 (when org-read-date-display-live
11804 (when org-read-date-overlay
11805 (org-delete-overlay org-read-date-overlay
))
11808 (while (not (equal (buffer-substring
11809 (max (point-min) (- (point) 4)) (point))
11813 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
11814 " " (or org-ans1 org-ans2
)))
11815 (org-end-time-was-given nil
)
11816 (f (org-read-date-analyze ans def defdecode
))
11818 org-time-stamp-custom-formats
11819 org-time-stamp-formats
))
11820 (fmt (if (or with-time
11821 (and (boundp 'org-time-was-given
) org-time-was-given
))
11824 (txt (concat "=> " (format-time-string fmt
(apply 'encode-time f
)))))
11825 (when (and org-end-time-was-given
11826 (string-match org-plain-time-of-day-regexp txt
))
11827 (setq txt
(concat (substring txt
0 (match-end 0)) "-"
11828 org-end-time-was-given
11829 (substring txt
(match-end 0)))))
11830 (setq org-read-date-overlay
11831 (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
11832 (org-overlay-display org-read-date-overlay txt
'secondary-selection
))))
11834 (defun org-read-date-analyze (ans def defdecode
)
11835 "Analyse the combined answer of the date prompt."
11836 ;; FIXME: cleanup and comment
11837 (let (delta deltan deltaw deltadef year month day
11838 hour minute second wday pm h2 m2 tl wday1
11839 iso-year iso-weekday iso-week iso-year iso-date
)
11841 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans
)
11844 (when (setq delta
(org-read-date-get-relative ans
(current-time) def
))
11845 (setq ans
(replace-match "" t t ans
)
11847 deltaw
(nth 1 delta
)
11848 deltadef
(nth 2 delta
)))
11850 ;; Check if there is an iso week date in there
11851 ;; If yes, sore the info and postpone interpreting it until the rest
11852 ;; of the parsing is done
11853 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans
)
11854 (setq iso-year
(if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans
))))
11855 iso-weekday
(if (match-end 3) (string-to-number (match-string 3 ans
)))
11856 iso-week
(string-to-number (match-string 2 ans
)))
11857 (setq ans
(replace-match "" t t ans
)))
11859 ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
11860 (when (string-match
11861 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans
)
11862 (setq year
(if (match-end 2)
11863 (string-to-number (match-string 2 ans
))
11864 (string-to-number (format-time-string "%Y")))
11865 month
(string-to-number (match-string 3 ans
))
11866 day
(string-to-number (match-string 4 ans
)))
11867 (if (< year
100) (setq year
(+ 2000 year
)))
11868 (setq ans
(replace-match (format "%04d-%02d-%02d\\5" year month day
)
11870 ;; Help matching am/pm times, because `parse-time-string' does not do that.
11871 ;; If there is a time with am/pm, and *no* time without it, we convert
11872 ;; so that matching will be successful.
11873 (loop for i from
1 to
2 do
; twice, for end time as well
11874 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans
))
11875 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans
))
11876 (setq hour
(string-to-number (match-string 1 ans
))
11877 minute
(if (match-end 3)
11878 (string-to-number (match-string 3 ans
))
11881 (string-to-char (downcase (match-string 4 ans
)))))
11882 (if (and (= hour
12) (not pm
))
11884 (if (and pm
(< hour
12)) (setq hour
(+ 12 hour
))))
11885 (setq ans
(replace-match (format "%02d:%02d" hour minute
)
11888 ;; Check if a time range is given as a duration
11889 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans
)
11890 (setq hour
(string-to-number (match-string 1 ans
))
11891 h2
(+ hour
(string-to-number (match-string 3 ans
)))
11892 minute
(string-to-number (match-string 2 ans
))
11893 m2
(+ minute
(if (match-end 5) (string-to-number
11894 (match-string 5 ans
))0)))
11895 (if (>= m2
60) (setq h2
(1+ h2
) m2
(- m2
60)))
11896 (setq ans
(replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2
)
11899 ;; Check if there is a time range
11900 (when (boundp 'org-end-time-was-given
)
11901 (setq org-time-was-given nil
)
11902 (when (and (string-match org-plain-time-of-day-regexp ans
)
11904 (setq org-end-time-was-given
(match-string 8 ans
))
11905 (setq ans
(concat (substring ans
0 (match-beginning 7))
11906 (substring ans
(match-end 7))))))
11908 (setq tl
(parse-time-string ans
)
11909 day
(or (nth 3 tl
) (nth 3 defdecode
))
11910 month
(or (nth 4 tl
)
11911 (if (and org-read-date-prefer-future
11912 (nth 3 tl
) (< (nth 3 tl
) (nth 3 defdecode
)))
11913 (1+ (nth 4 defdecode
))
11914 (nth 4 defdecode
)))
11915 year
(or (nth 5 tl
)
11916 (if (and org-read-date-prefer-future
11917 (nth 4 tl
) (< (nth 4 tl
) (nth 4 defdecode
)))
11918 (1+ (nth 5 defdecode
))
11919 (nth 5 defdecode
)))
11920 hour
(or (nth 2 tl
) (nth 2 defdecode
))
11921 minute
(or (nth 1 tl
) (nth 1 defdecode
))
11922 second
(or (nth 0 tl
) 0)
11925 ;; Special date definitions below
11928 ;; There was an iso week
11929 (setq year
(or iso-year year
)
11930 day
(or iso-weekday wday
1)
11931 wday nil
; to make sure that the trigger below does not match
11932 iso-date
(calendar-gregorian-from-absolute
11933 (calendar-absolute-from-iso
11934 (list iso-week day year
))))
11935 ; FIXME: Should we also push ISO weeks into the future?
11936 ; (when (and org-read-date-prefer-future
11938 ; (< (calendar-absolute-from-gregorian iso-date)
11939 ; (time-to-days (current-time))))
11940 ; (setq year (1+ year)
11941 ; iso-date (calendar-gregorian-from-absolute
11942 ; (calendar-absolute-from-iso
11943 ; (list iso-week day year)))))
11944 (setq month
(car iso-date
)
11945 year
(nth 2 iso-date
)
11946 day
(nth 1 iso-date
)))
11949 (let ((now (decode-time (current-time))))
11950 (setq day
(nth 3 now
) month
(nth 4 now
) year
(nth 5 now
))))
11951 (cond ((member deltaw
'("d" "")) (setq day
(+ day deltan
)))
11952 ((equal deltaw
"w") (setq day
(+ day
(* 7 deltan
))))
11953 ((equal deltaw
"m") (setq month
(+ month deltan
)))
11954 ((equal deltaw
"y") (setq year
(+ year deltan
)))))
11955 ((and wday
(not (nth 3 tl
)))
11956 ;; Weekday was given, but no day, so pick that day in the week
11957 ;; on or after the derived date.
11958 (setq wday1
(nth 6 (decode-time (encode-time 0 0 0 day month year
))))
11959 (unless (equal wday wday1
)
11960 (setq day
(+ day
(%
(- wday wday1 -
7) 7))))))
11961 (if (and (boundp 'org-time-was-given
)
11963 (setq org-time-was-given t
))
11964 (if (< year
100) (setq year
(+ 2000 year
)))
11965 (if (< year
1970) (setq year
(nth 5 defdecode
))) ; not representable
11966 (list second minute hour day month year
)))
11968 (defvar parse-time-weekdays
)
11970 (defun org-read-date-get-relative (s today default
)
11971 "Check string S for special relative date string.
11972 TODAY and DEFAULT are internal times, for today and for a default.
11973 Return shift list (N what def-flag)
11974 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
11975 N is the number of WHATs to shift.
11976 DEF-FLAG is t when a double ++ or -- indicates shift relative to
11977 the DEFAULT date rather than TODAY."
11981 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
11983 "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays
"\\|") "\\)\\)?"
11984 "\\([ \t]\\|$\\)") s
)
11985 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
11986 (let* ((dir (if (> (match-end 1) (match-beginning 1))
11987 (string-to-char (substring (match-string 1 s
) -
1))
11989 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
11990 (n (if (match-end 2) (string-to-number (match-string 2 s
)) 1))
11991 (what (if (match-end 3) (match-string 3 s
) "d"))
11992 (wday1 (cdr (assoc (downcase what
) parse-time-weekdays
)))
11993 (date (if rel default today
))
11994 (wday (nth 6 (decode-time date
)))
11998 (setq delta
(mod (+ 7 (- wday1 wday
)) 7))
11999 (if (= dir ?-
) (setq delta
(- delta
7)))
12000 (if (> n
1) (setq delta
(+ delta
(* (1- n
) (if (= dir ?-
) -
7 7)))))
12001 (list delta
"d" rel
))
12002 (list (* n
(if (= dir ?-
) -
1 1)) what rel
)))))
12004 (defun org-eval-in-calendar (form &optional keepdate
)
12005 "Eval FORM in the calendar window and return to current window.
12006 Also, store the cursor date in variable org-ans2."
12007 (let ((sw (selected-window)))
12008 (select-window (get-buffer-window "*Calendar*"))
12010 (when (and (not keepdate
) (calendar-cursor-to-date))
12011 (let* ((date (calendar-cursor-to-date))
12012 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
12013 (setq org-ans2
(format-time-string "%Y-%m-%d" time
))))
12014 (org-move-overlay org-date-ovl
(1- (point)) (1+ (point)) (current-buffer))
12015 (select-window sw
)))
12017 (defun org-calendar-select ()
12018 "Return to `org-read-date' with the date currently selected.
12019 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
12021 (when (calendar-cursor-to-date)
12022 (let* ((date (calendar-cursor-to-date))
12023 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
12024 (setq org-ans1
(format-time-string "%Y-%m-%d" time
)))
12025 (if (active-minibuffer-window) (exit-minibuffer))))
12027 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra
)
12028 "Insert a date stamp for the date given by the internal TIME.
12029 WITH-HM means, use the stamp format that includes the time of the day.
12030 INACTIVE means use square brackets instead of angular ones, so that the
12031 stamp will not contribute to the agenda.
12032 PRE and POST are optional strings to be inserted before and after the
12034 The command returns the inserted time stamp."
12035 (let ((fmt (funcall (if with-hm
'cdr
'car
) org-time-stamp-formats
))
12037 (if inactive
(setq fmt
(concat "[" (substring fmt
1 -
1) "]")))
12038 (insert-before-markers (or pre
""))
12039 (insert-before-markers (setq stamp
(format-time-string fmt time
)))
12040 (when (listp extra
)
12041 (setq extra
(car extra
))
12042 (if (and (stringp extra
)
12043 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra
))
12044 (setq extra
(format "-%02d:%02d"
12045 (string-to-number (match-string 1 extra
))
12046 (string-to-number (match-string 2 extra
))))
12050 (insert-before-markers extra
)
12052 (insert-before-markers (or post
""))
12053 (setq org-last-inserted-timestamp stamp
)))
12055 (defun org-toggle-time-stamp-overlays ()
12056 "Toggle the use of custom time stamp formats."
12058 (setq org-display-custom-times
(not org-display-custom-times
))
12059 (unless org-display-custom-times
12060 (let ((p (point-min)) (bmp (buffer-modified-p)))
12061 (while (setq p
(next-single-property-change p
'display
))
12062 (if (and (get-text-property p
'display
)
12063 (eq (get-text-property p
'face
) 'org-date
))
12064 (remove-text-properties
12065 p
(setq p
(next-single-property-change p
'display
))
12067 (set-buffer-modified-p bmp
)))
12068 (if (featurep 'xemacs
)
12069 (remove-text-properties (point-min) (point-max) '(end-glyph t
)))
12070 (org-restart-font-lock)
12071 (setq org-table-may-need-update t
)
12072 (if org-display-custom-times
12073 (message "Time stamps are overlayed with custom format")
12074 (message "Time stamp overlays removed")))
12076 (defun org-display-custom-time (beg end
)
12077 "Overlay modified time stamp format over timestamp between BEG and END."
12078 (let* ((ts (buffer-substring beg end
))
12079 t1 w1 with-hm tf time str w2
(off 0))
12081 (setq t1
(org-parse-time-string ts t
))
12082 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts
)
12083 (setq off
(- (match-end 0) (match-beginning 0)))))
12084 (setq end
(- end off
))
12085 (setq w1
(- end beg
)
12086 with-hm
(and (nth 1 t1
) (nth 2 t1
))
12087 tf
(funcall (if with-hm
'cdr
'car
) org-time-stamp-custom-formats
)
12088 time
(org-fix-decoded-time t1
)
12090 (format-time-string
12091 (substring tf
1 -
1) (apply 'encode-time time
))
12092 nil
'mouse-face
'highlight
)
12094 (if (not (= w2 w1
))
12095 (add-text-properties (1+ beg
) (+ 2 beg
)
12096 (list 'org-dwidth t
'org-dwidth-n
(- w1 w2
))))
12097 (if (featurep 'xemacs
)
12099 (put-text-property beg end
'invisible t
)
12100 (put-text-property beg end
'end-glyph
(make-glyph str
)))
12101 (put-text-property beg end
'display str
))))
12103 (defun org-translate-time (string)
12104 "Translate all timestamps in STRING to custom format.
12105 But do this only if the variable `org-display-custom-times' is set."
12106 (when org-display-custom-times
12109 (re org-ts-regexp-both
)
12110 t1 with-hm inactive tf time str beg end
)
12111 (while (setq start
(string-match re string start
))
12112 (setq beg
(match-beginning 0)
12114 t1
(save-match-data
12115 (org-parse-time-string (substring string beg end
) t
))
12116 with-hm
(and (nth 1 t1
) (nth 2 t1
))
12117 inactive
(equal (substring string beg
(1+ beg
)) "[")
12118 tf
(funcall (if with-hm
'cdr
'car
)
12119 org-time-stamp-custom-formats
)
12120 time
(org-fix-decoded-time t1
)
12121 str
(format-time-string
12123 (if inactive
"[" "<") (substring tf
1 -
1)
12124 (if inactive
"]" ">"))
12125 (apply 'encode-time time
))
12126 string
(replace-match str t t string
)
12127 start
(+ start
(length str
)))))))
12130 (defun org-fix-decoded-time (time)
12131 "Set 0 instead of nil for the first 6 elements of time.
12132 Don't touch the rest."
12134 (mapcar (lambda (x) (if (< (setq n
(1+ n
)) 7) (or x
0) x
)) time
)))
12136 (defun org-days-to-time (timestamp-string)
12137 "Difference between TIMESTAMP-STRING and now in days."
12138 (- (time-to-days (org-time-string-to-time timestamp-string
))
12139 (time-to-days (current-time))))
12141 (defun org-deadline-close (timestamp-string &optional ndays
)
12142 "Is the time in TIMESTAMP-STRING close to the current date?"
12143 (setq ndays
(or ndays
(org-get-wdays timestamp-string
)))
12144 (and (< (org-days-to-time timestamp-string
) ndays
)
12145 (not (org-entry-is-done-p))))
12147 (defun org-get-wdays (ts)
12148 "Get the deadline lead time appropriate for timestring TS."
12150 ((<= org-deadline-warning-days
0)
12151 ;; 0 or negative, enforce this value no matter what
12152 (- org-deadline-warning-days
))
12153 ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts
)
12154 ;; lead time is specified.
12155 (floor (* (string-to-number (match-string 1 ts
))
12156 (cdr (assoc (match-string 2 ts
)
12157 '(("d" .
1) ("w" .
7)
12158 ("m" .
30.4) ("y" .
365.25)))))))
12159 ;; go for the default.
12160 (t org-deadline-warning-days
)))
12162 (defun org-calendar-select-mouse (ev)
12163 "Return to `org-read-date' with the date currently selected.
12164 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
12166 (mouse-set-point ev
)
12167 (when (calendar-cursor-to-date)
12168 (let* ((date (calendar-cursor-to-date))
12169 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
12170 (setq org-ans1
(format-time-string "%Y-%m-%d" time
)))
12171 (if (active-minibuffer-window) (exit-minibuffer))))
12173 (defun org-check-deadlines (ndays)
12174 "Check if there are any deadlines due or past due.
12175 A deadline is considered due if it happens within `org-deadline-warning-days'
12176 days from today's date. If the deadline appears in an entry marked DONE,
12177 it is not shown. The prefix arg NDAYS can be used to test that many
12178 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
12180 (let* ((org-warn-days
12182 ((equal ndays
'(4)) 100000)
12183 (ndays (prefix-numeric-value ndays
))
12184 (t (abs org-deadline-warning-days
))))
12185 (case-fold-search nil
)
12186 (regexp (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>"))
12188 (lambda () (org-deadline-close (match-string 1) org-warn-days
))))
12190 (message "%d deadlines past-due or due within %d days"
12191 (org-occur regexp nil callback
)
12194 (defun org-check-before-date (date)
12195 "Check if there are deadlines or scheduled entries before DATE."
12196 (interactive (list (org-read-date)))
12197 (let ((case-fold-search nil
)
12198 (regexp (concat "\\<\\(" org-deadline-string
12199 "\\|" org-scheduled-string
12200 "\\) *<\\([^>]+\\)>"))
12202 (lambda () (time-less-p
12203 (org-time-string-to-time (match-string 2))
12204 (org-time-string-to-time date
)))))
12205 (message "%d entries before %s"
12206 (org-occur regexp nil callback
) date
)))
12208 (defun org-check-after-date (date)
12209 "Check if there are deadlines or scheduled entries after DATE."
12210 (interactive (list (org-read-date)))
12211 (let ((case-fold-search nil
)
12212 (regexp (concat "\\<\\(" org-deadline-string
12213 "\\|" org-scheduled-string
12214 "\\) *<\\([^>]+\\)>"))
12218 (org-time-string-to-time (match-string 2))
12219 (org-time-string-to-time date
))))))
12220 (message "%d entries after %s"
12221 (org-occur regexp nil callback
) date
)))
12223 (defun org-evaluate-time-range (&optional to-buffer
)
12224 "Evaluate a time range by computing the difference between start and end.
12225 Normally the result is just printed in the echo area, but with prefix arg
12226 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
12227 If the time range is actually in a table, the result is inserted into the
12229 For time difference computation, a year is assumed to be exactly 365
12230 days in order to avoid rounding problems."
12233 (org-clock-update-time-maybe)
12235 (unless (org-at-date-range-p t
)
12236 (goto-char (point-at-bol))
12237 (re-search-forward org-tr-regexp-both
(point-at-eol) t
))
12238 (if (not (org-at-date-range-p t
))
12239 (error "Not at a time-stamp range, and none found in current line")))
12240 (let* ((ts1 (match-string 1))
12241 (ts2 (match-string 2))
12242 (havetime (or (> (length ts1
) 15) (> (length ts2
) 15)))
12243 (match-end (match-end 0))
12244 (time1 (org-time-string-to-time ts1
))
12245 (time2 (org-time-string-to-time ts2
))
12246 (t1 (time-to-seconds time1
))
12247 (t2 (time-to-seconds time2
))
12248 (diff (abs (- t2 t1
)))
12249 (negative (< (- t2 t1
) 0))
12250 ;; (ys (floor (* 365 24 60 60)))
12253 (fy "%dy %dd %02d:%02d")
12255 (fd "%dd %02d:%02d")
12260 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
12262 d
(floor (/ diff ds
)) diff
(mod diff ds
)
12263 h
(floor (/ diff hs
)) diff
(mod diff hs
)
12264 m
(floor (/ diff
60)))
12265 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
12267 d
(floor (+ (/ diff ds
) 0.5))
12269 (if (not to-buffer
)
12270 (message "%s" (org-make-tdiff-string y d h m
))
12271 (if (org-at-table-p)
12273 (goto-char match-end
)
12275 (and (looking-at " *|") (goto-char (match-end 0))))
12276 (goto-char match-end
))
12278 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
12279 (replace-match ""))
12280 (if negative
(insert " -"))
12281 (if (> y
0) (insert " " (format (if havetime fy fy1
) y d h m
))
12282 (if (> d
0) (insert " " (format (if havetime fd fd1
) d h m
))
12283 (insert " " (format fh h m
))))
12284 (if align
(org-table-align))
12285 (message "Time difference inserted")))))
12287 (defun org-make-tdiff-string (y d h m
)
12290 (if (> y
0) (setq fmt
(concat fmt
"%d year" (if (> y
1) "s" "") " ")
12292 (if (> d
0) (setq fmt
(concat fmt
"%d day" (if (> d
1) "s" "") " ")
12294 (if (> h
0) (setq fmt
(concat fmt
"%d hour" (if (> h
1) "s" "") " ")
12296 (if (> m
0) (setq fmt
(concat fmt
"%d minute" (if (> m
1) "s" "") " ")
12298 (apply 'format fmt
(nreverse l
))))
12300 (defun org-time-string-to-time (s)
12301 (apply 'encode-time
(org-parse-time-string s
)))
12303 (defun org-time-string-to-absolute (s &optional daynr prefer show-all
)
12304 "Convert a time stamp to an absolute day number.
12305 If there is a specifyer for a cyclic time stamp, get the closest date to
12307 PREFER and SHOW-ALL are passed through to `org-closest-date'."
12309 ((and daynr
(string-match "\\`%%\\((.*)\\)" s
))
12310 (if (org-diary-sexp-entry (match-string 1 s
) "" date
)
12313 ((and daynr
(string-match "\\+[0-9]+[dwmy]" s
))
12314 (org-closest-date s
(if (and (boundp 'daynr
) (integerp daynr
)) daynr
12315 (time-to-days (current-time))) (match-string 0 s
)
12317 (t (time-to-days (apply 'encode-time
(org-parse-time-string s
))))))
12319 (defun org-days-to-iso-week (days)
12320 "Return the iso week number."
12322 (car (calendar-iso-from-absolute days
)))
12324 (defun org-small-year-to-year (year)
12325 "Convert 2-digit years into 4-digit years.
12326 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
12327 The year 2000 cannot be abbreviated. Any year larger than 99
12328 is returned unchanged."
12330 (setq year
(+ 2000 year
))
12332 (setq year
(+ 1900 year
))))
12335 (defun org-time-from-absolute (d)
12336 "Return the time corresponding to date D.
12337 D may be an absolute day number, or a calendar-type list (month day year)."
12338 (if (numberp d
) (setq d
(calendar-gregorian-from-absolute d
)))
12339 (encode-time 0 0 0 (nth 1 d
) (car d
) (nth 2 d
)))
12341 (defun org-calendar-holiday ()
12342 "List of holidays, for Diary display in Org-mode."
12343 (require 'holidays
)
12345 (if (fboundp 'calendar-check-holidays
)
12346 'calendar-check-holidays
'check-calendar-holidays
) date
)))
12347 (if hl
(mapconcat 'identity hl
"; "))))
12349 (defun org-diary-sexp-entry (sexp entry date
)
12350 "Process a SEXP diary ENTRY for DATE."
12351 (require 'diary-lib
)
12352 (let ((result (if calendar-debug-sexp
12353 (let ((stack-trace-on-error t
))
12354 (eval (car (read-from-string sexp
))))
12355 (condition-case nil
12356 (eval (car (read-from-string sexp
)))
12359 (message "Bad sexp at line %d in %s: %s"
12361 (buffer-file-name) sexp
)
12363 (cond ((stringp result
) result
)
12364 ((and (consp result
)
12365 (stringp (cdr result
))) (cdr result
))
12369 (defun org-diary-to-ical-string (frombuf)
12370 "Get iCalendar entries from diary entries in buffer FROMBUF.
12371 This uses the icalendar.el library."
12372 (let* ((tmpdir (if (featurep 'xemacs
)
12374 temporary-file-directory
))
12375 (tmpfile (make-temp-name
12376 (expand-file-name "orgics" tmpdir
)))
12379 (set-buffer frombuf
)
12380 (icalendar-export-region (point-min) (point-max) tmpfile
)
12381 (setq buf
(find-buffer-visiting tmpfile
))
12383 (goto-char (point-min))
12384 (if (re-search-forward "^BEGIN:VEVENT" nil t
)
12385 (setq b
(match-beginning 0)))
12386 (goto-char (point-max))
12387 (if (re-search-backward "^END:VEVENT" nil t
)
12388 (setq e
(match-end 0)))
12389 (setq rtn
(if (and b e
) (concat (buffer-substring b e
) "\n") "")))
12391 (delete-file tmpfile
)
12394 (defun org-closest-date (start current change prefer show-all
)
12395 "Find the date closest to CURRENT that is consistent with START and CHANGE.
12396 When PREFER is `past' return a date that is either CURRENT or past.
12397 When PREFER is `future', return a date that is either CURRENT or future.
12398 When SHOW-ALL is nil, only return the current occurrence of a time stamp."
12399 ;; Make the proper lists from the dates
12401 (let ((a1 '(("d" . day
) ("w" . week
) ("m" . month
) ("y" . year
)))
12402 dn dw sday cday n1 n2 n0
12403 d m y y1 y2 date1 date2 nmonths nm ny m2
)
12405 (setq start
(org-date-to-gregorian start
)
12406 current
(org-date-to-gregorian
12409 (time-to-days (current-time))))
12410 sday
(calendar-absolute-from-gregorian start
)
12411 cday
(calendar-absolute-from-gregorian current
))
12413 (if (<= cday sday
) (throw 'exit sday
))
12415 (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change
)
12416 (setq dn
(string-to-number (match-string 1 change
))
12417 dw
(cdr (assoc (match-string 2 change
) a1
)))
12418 (error "Invalid change specifyer: %s" change
))
12419 (if (eq dw
'week
) (setq dw
'day dn
(* 7 dn
)))
12422 (setq n1
(+ sday
(* dn
(floor (/ (- cday sday
) dn
))))
12425 (setq d
(nth 1 start
) m
(car start
) y1
(nth 2 start
) y2
(nth 2 current
))
12426 (setq y1
(+ (* (floor (/ (- y2 y1
) dn
)) dn
) y1
))
12427 (setq date1
(list m d y1
)
12428 n1
(calendar-absolute-from-gregorian date1
)
12429 date2
(list m d
(+ y1
(* (if (< n1 cday
) 1 -
1) dn
)))
12430 n2
(calendar-absolute-from-gregorian date2
)))
12432 ;; approx number of month between the two dates
12433 (setq nmonths
(floor (/ (- cday sday
) 30.436875)))
12434 ;; How often does dn fit in there?
12435 (setq d
(nth 1 start
) m
(car start
) y
(nth 2 start
)
12436 nm
(* dn
(max 0 (1- (floor (/ nmonths dn
)))))
12438 ny
(floor (/ m
12))
12441 (while (> m
12) (setq m
(- m
12) y
(1+ y
)))
12442 (setq n1
(calendar-absolute-from-gregorian (list m d y
)))
12443 (setq m2
(+ m dn
) y2 y
)
12444 (if (> m2
12) (setq y2
(1+ y2
) m2
(- m2
12)))
12445 (setq n2
(calendar-absolute-from-gregorian (list m2 d y2
)))
12446 (while (<= n2 cday
)
12447 (setq n1 n2 m m2 y y2
)
12448 (setq m2
(+ m dn
) y2 y
)
12449 (if (> m2
12) (setq y2
(1+ y2
) m2
(- m2
12)))
12450 (setq n2
(calendar-absolute-from-gregorian (list m2 d y2
))))))
12451 ;; Make sure n1 is the earlier date
12452 (setq n0 n1 n1
(min n1 n2
) n2
(max n0 n2
))
12455 ((eq prefer
'past
) n1
)
12456 ((eq prefer
'future
) (if (= cday n1
) n1 n2
))
12457 (t (if (> (abs (- cday n1
)) (abs (- cday n2
))) n2 n1
)))
12459 ((eq prefer
'past
) n1
)
12460 ((eq prefer
'future
) (if (= cday n1
) n1 n2
))
12461 (t (if (= cday n1
) n1 n2
)))))))
12463 (defun org-date-to-gregorian (date)
12464 "Turn any specification of DATE into a gregorian date for the calendar."
12465 (cond ((integerp date
) (calendar-gregorian-from-absolute date
))
12466 ((and (listp date
) (= (length date
) 3)) date
)
12468 (setq date
(org-parse-time-string date
))
12469 (list (nth 4 date
) (nth 3 date
) (nth 5 date
)))
12471 (list (nth 4 date
) (nth 3 date
) (nth 5 date
)))))
12473 (defun org-parse-time-string (s &optional nodefault
)
12474 "Parse the standard Org-mode time string.
12475 This should be a lot faster than the normal `parse-time-string'.
12476 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
12477 hour and minute fields will be nil if not given."
12478 (if (string-match org-ts-regexp0 s
)
12480 (if (or (match-beginning 8) (not nodefault
))
12481 (string-to-number (or (match-string 8 s
) "0")))
12482 (if (or (match-beginning 7) (not nodefault
))
12483 (string-to-number (or (match-string 7 s
) "0")))
12484 (string-to-number (match-string 4 s
))
12485 (string-to-number (match-string 3 s
))
12486 (string-to-number (match-string 2 s
))
12490 (defun org-timestamp-up (&optional arg
)
12491 "Increase the date item at the cursor by one.
12492 If the cursor is on the year, change the year. If it is on the month or
12493 the day, change that.
12494 With prefix ARG, change by that many units."
12496 (org-timestamp-change (prefix-numeric-value arg
)))
12498 (defun org-timestamp-down (&optional arg
)
12499 "Decrease the date item at the cursor by one.
12500 If the cursor is on the year, change the year. If it is on the month or
12501 the day, change that.
12502 With prefix ARG, change by that many units."
12504 (org-timestamp-change (- (prefix-numeric-value arg
))))
12506 (defun org-timestamp-up-day (&optional arg
)
12507 "Increase the date in the time stamp by one day.
12508 With prefix ARG, change that many days."
12510 (if (and (not (org-at-timestamp-p t
))
12511 (org-on-heading-p))
12513 (org-timestamp-change (prefix-numeric-value arg
) 'day
)))
12515 (defun org-timestamp-down-day (&optional arg
)
12516 "Decrease the date in the time stamp by one day.
12517 With prefix ARG, change that many days."
12519 (if (and (not (org-at-timestamp-p t
))
12520 (org-on-heading-p))
12522 (org-timestamp-change (- (prefix-numeric-value arg
)) 'day
)))
12524 (defun org-at-timestamp-p (&optional inactive-ok
)
12525 "Determine if the cursor is in or at a timestamp."
12527 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2
))
12529 (ans (or (looking-at tsr
)
12531 (skip-chars-backward "^[<\n\r\t")
12532 (if (> (point) (point-min)) (backward-char 1))
12533 (and (looking-at tsr
)
12534 (> (- (match-end 0) pos
) -
1))))))
12536 (boundp 'org-ts-what
)
12539 ((= pos
(match-beginning 0)) 'bracket
)
12540 ((= pos
(1- (match-end 0))) 'bracket
)
12541 ((org-pos-in-match-range pos
2) 'year
)
12542 ((org-pos-in-match-range pos
3) 'month
)
12543 ((org-pos-in-match-range pos
7) 'hour
)
12544 ((org-pos-in-match-range pos
8) 'minute
)
12545 ((or (org-pos-in-match-range pos
4)
12546 (org-pos-in-match-range pos
5)) 'day
)
12547 ((and (> pos
(or (match-end 8) (match-end 5)))
12548 (< pos
(match-end 0)))
12549 (- pos
(or (match-end 8) (match-end 5))))
12553 (defun org-toggle-timestamp-type ()
12554 "Toggle the type (<active> or [inactive]) of a time stamp."
12556 (when (org-at-timestamp-p t
)
12557 (let ((beg (match-beginning 0)) (end (match-end 0))
12558 (map '((?\
[ .
"<") (?\
] .
">") (?
< .
"[") (?
> .
"]"))))
12561 (while (re-search-forward "[][<>]" end t
)
12562 (replace-match (cdr (assoc (char-after (match-beginning 0)) map
))
12564 (message "Timestamp is now %sactive"
12565 (if (equal (char-after beg
) ?
<) "" "in")))))
12567 (defun org-timestamp-change (n &optional what
)
12568 "Change the date in the time stamp at point.
12569 The date will be changed by N times WHAT. WHAT can be `day', `month',
12570 `year', `minute', `second'. If WHAT is not given, the cursor position
12571 in the timestamp determines what will be changed."
12572 (let ((pos (point))
12574 (dm (max (nth 1 org-time-stamp-rounding-minutes
) 1))
12578 (if (not (org-at-timestamp-p t
))
12579 (error "Not at a timestamp"))
12580 (if (and (not what
) (eq org-ts-what
'bracket
))
12581 (org-toggle-timestamp-type)
12582 (if (and (not what
) (not (eq org-ts-what
'day
))
12583 org-display-custom-times
12584 (get-text-property (point) 'display
)
12585 (not (get-text-property (1- (point)) 'display
)))
12586 (setq org-ts-what
'day
))
12587 (setq org-ts-what
(or what org-ts-what
)
12588 inactive
(= (char-after (match-beginning 0)) ?\
[)
12589 ts
(match-string 0))
12592 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
12594 (setq extra
(match-string 1 ts
)))
12595 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts
)
12597 (setq time0
(org-parse-time-string ts
))
12598 (when (and (eq org-ts-what
'minute
)
12599 (eq current-prefix-arg nil
))
12600 (setq n
(* dm
(cond ((> n
0) 1) ((< n
0) -
1) (t 0))))
12601 (when (not (= 0 (setq rem
(%
(nth 1 time0
) dm
))))
12602 (setcar (cdr time0
) (+ (nth 1 time0
)
12603 (if (> n
0) (- rem
) (- dm rem
))))))
12605 (encode-time (or (car time0
) 0)
12606 (+ (if (eq org-ts-what
'minute
) n
0) (nth 1 time0
))
12607 (+ (if (eq org-ts-what
'hour
) n
0) (nth 2 time0
))
12608 (+ (if (eq org-ts-what
'day
) n
0) (nth 3 time0
))
12609 (+ (if (eq org-ts-what
'month
) n
0) (nth 4 time0
))
12610 (+ (if (eq org-ts-what
'year
) n
0) (nth 5 time0
))
12612 (when (and (member org-ts-what
'(hour minute
))
12614 (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra
))
12615 (setq extra
(org-modify-ts-extra
12617 (if (eq org-ts-what
'hour
) 2 5)
12619 (when (integerp org-ts-what
)
12620 (setq extra
(org-modify-ts-extra extra org-ts-what n dm
)))
12621 (if (eq what
'calendar
)
12622 (let ((cal-date (org-get-date-from-calendar)))
12623 (setcar (nthcdr 4 time0
) (nth 0 cal-date
)) ; month
12624 (setcar (nthcdr 3 time0
) (nth 1 cal-date
)) ; day
12625 (setcar (nthcdr 5 time0
) (nth 2 cal-date
)) ; year
12626 (setcar time0
(or (car time0
) 0))
12627 (setcar (nthcdr 1 time0
) (or (nth 1 time0
) 0))
12628 (setcar (nthcdr 2 time0
) (or (nth 2 time0
) 0))
12629 (setq time
(apply 'encode-time time0
))))
12630 (setq org-last-changed-timestamp
12631 (org-insert-time-stamp time with-hm inactive nil nil extra
))
12632 (org-clock-update-time-maybe)
12634 ;; Try to recenter the calendar window, if any
12635 (if (and org-calendar-follow-timestamp-change
12636 (get-buffer-window "*Calendar*" t
)
12637 (memq org-ts-what
'(day month year
)))
12638 (org-recenter-calendar (time-to-days time
))))))
12640 (defun org-modify-ts-extra (s pos n dm
)
12641 "Change the different parts of the lead-time and repeat fields in timestamp."
12642 (let ((idx '(("d" .
0) ("w" .
1) ("m" .
2) ("y" .
3) ("d" . -
1) ("y" .
4)))
12644 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s
)
12646 ((or (org-pos-in-match-range pos
2)
12647 (org-pos-in-match-range pos
3))
12648 (setq m
(string-to-number (match-string 3 s
))
12649 h
(string-to-number (match-string 2 s
)))
12650 (if (org-pos-in-match-range pos
2)
12652 (setq n
(* dm
(org-no-warnings (signum n
))))
12653 (when (not (= 0 (setq rem
(% m dm
))))
12654 (setq m
(+ m
(if (> n
0) (- rem
) (- dm rem
)))))
12656 (if (< m
0) (setq m
(+ m
60) h
(1- h
)))
12657 (if (> m
59) (setq m
(- m
60) h
(1+ h
)))
12658 (setq h
(min 24 (max 0 h
)))
12659 (setq ng
1 new
(format "-%02d:%02d" h m
)))
12660 ((org-pos-in-match-range pos
6)
12661 (setq ng
6 new
(car (rassoc (+ n
(cdr (assoc (match-string 6 s
) idx
))) idx
))))
12662 ((org-pos-in-match-range pos
5)
12663 (setq ng
5 new
(format "%d" (max 1 (+ n
(string-to-number (match-string 5 s
)))))))
12665 ((org-pos-in-match-range pos
9)
12666 (setq ng
9 new
(car (rassoc (+ n
(cdr (assoc (match-string 9 s
) idx
))) idx
))))
12667 ((org-pos-in-match-range pos
8)
12668 (setq ng
8 new
(format "%d" (max 0 (+ n
(string-to-number (match-string 8 s
))))))))
12672 (substring s
0 (match-beginning ng
))
12674 (substring s
(match-end ng
))))))
12677 (defun org-recenter-calendar (date)
12678 "If the calendar is visible, recenter it to DATE."
12679 (let* ((win (selected-window))
12680 (cwin (get-buffer-window "*Calendar*" t
))
12681 (calendar-move-hook nil
))
12683 (select-window cwin
)
12684 (calendar-goto-date (if (listp date
) date
12685 (calendar-gregorian-from-absolute date
)))
12686 (select-window win
))))
12688 (defun org-goto-calendar (&optional arg
)
12689 "Go to the Emacs calendar at the current date.
12690 If there is a time stamp in the current line, go to that date.
12691 A prefix ARG can be used to force the current date."
12693 (let ((tsr org-ts-regexp
) diff
12694 (calendar-move-hook nil
)
12695 (calendar-view-holidays-initially-flag nil
)
12696 (view-calendar-holidays-initially nil
)
12697 (calendar-view-diary-initially-flag nil
)
12698 (view-diary-entries-initially nil
))
12699 (if (or (org-at-timestamp-p)
12701 (beginning-of-line 1)
12702 (looking-at (concat ".*" tsr
))))
12703 (let ((d1 (time-to-days (current-time)))
12705 (org-time-string-to-time (match-string 1)))))
12706 (setq diff
(- d2 d1
))))
12708 (calendar-goto-today)
12709 (if (and diff
(not arg
)) (calendar-forward-day diff
))))
12711 (defun org-get-date-from-calendar ()
12712 "Return a list (month day year) of date at point in calendar."
12713 (with-current-buffer "*Calendar*"
12715 (calendar-cursor-to-date))))
12717 (defun org-date-from-calendar ()
12718 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
12719 If there is already a time stamp at the cursor position, update it."
12721 (if (org-at-timestamp-p t
)
12722 (org-timestamp-change 0 'calendar
)
12723 (let ((cal-date (org-get-date-from-calendar)))
12724 (org-insert-time-stamp
12725 (encode-time 0 0 0 (nth 1 cal-date
) (car cal-date
) (nth 2 cal-date
))))))
12727 (defun org-minutes-to-hh:mm-string
(m)
12728 "Compute H:MM from a number of minutes."
12729 (let ((h (/ m
60)))
12730 (setq m
(- m
(* 60 h
)))
12731 (format org-time-clocksum-format h m
)))
12733 (defun org-hh:mm-string-to-minutes
(s)
12734 "Convert a string H:MM to a number of minutes."
12735 (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s
)
12736 (+ (* (string-to-number (match-string 1 s
)) 60)
12737 (string-to-number (match-string 2 s
)))
12742 (defun org-save-all-org-buffers ()
12743 "Save all Org-mode buffers without user confirmation."
12745 (message "Saving all Org-mode buffers...")
12746 (save-some-buffers t
'org-mode-p
)
12747 (when (featurep 'org-id
) (org-id-locations-save))
12748 (message "Saving all Org-mode buffers... done"))
12750 (defun org-revert-all-org-buffers ()
12751 "Revert all Org-mode buffers.
12752 Prompt for confirmation when there are unsaved changes.
12753 Be sure you know what you are doing before letting this function
12754 overwrite your changes.
12756 This function is useful in a setup where one tracks org files
12757 with a version control system, to revert on one machine after pulling
12758 changes from another. I believe the procedure must be like this:
12760 1. M-x org-save-all-org-buffers
12761 2. Pull changes from the other machine, resolve conflicts
12762 3. M-x org-revert-all-org-buffers"
12764 (unless (yes-or-no-p "Revert all Org buffers from their files? ")
12767 (save-window-excursion
12770 (when (and (with-current-buffer b
(org-mode-p))
12771 (with-current-buffer b buffer-file-name
))
12772 (switch-to-buffer b
)
12773 (revert-buffer t
'no-confirm
)))
12775 (when (and (featurep 'org-id
) org-id-track-globally
)
12776 (org-id-locations-load)))))
12781 (defun org-iswitchb (&optional arg
)
12782 "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
12783 With a prefix argument, restrict available to files.
12784 With two prefix arguments, restrict available buffers to agenda files.
12786 Due to some yet unresolved reason, the global function
12787 `iswitchb-mode' needs to be active for this function to work."
12789 (require 'iswitchb
)
12790 (let ((enabled iswitchb-mode
) blist
)
12791 (or enabled
(iswitchb-mode 1))
12792 (setq blist
(cond ((equal arg
'(4)) (org-buffer-list 'files
))
12793 ((equal arg
'(16)) (org-buffer-list 'agenda
))
12794 (t (org-buffer-list))))
12796 (let ((iswitchb-make-buflist-hook
12798 (setq iswitchb-temp-buflist
12799 (mapcar 'buffer-name blist
)))))
12801 (iswitchb-read-buffer
12802 "Switch-to: " nil t
))
12803 (or enabled
(iswitchb-mode -
1))))))
12806 (defun org-ido-switchb (&optional arg
)
12807 "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
12808 With a prefix argument, restrict available to files.
12809 With two prefix arguments, restrict available buffers to agenda files."
12811 (let ((blist (cond ((equal arg
'(4)) (org-buffer-list 'files
))
12812 ((equal arg
'(16)) (org-buffer-list 'agenda
))
12813 (t (org-buffer-list)))))
12815 (org-ido-completing-read "Org buffer: "
12816 (mapcar 'list
(mapcar 'buffer-name blist
))
12819 (defun org-buffer-list (&optional predicate exclude-tmp
)
12820 "Return a list of Org buffers.
12821 PREDICATE can be `export', `files' or `agenda'.
12823 export restrict the list to Export buffers.
12824 files restrict the list to buffers visiting Org files.
12825 agenda restrict the list to buffers visiting agenda files.
12827 If EXCLUDE-TMP is non-nil, ignore temporary buffers."
12829 (agenda-files (and (eq predicate
'agenda
)
12830 (mapcar 'file-truename
(org-agenda-files t
))))
12833 ((eq predicate
'files
)
12834 (lambda (b) (with-current-buffer b
(eq major-mode
'org-mode
))))
12835 ((eq predicate
'export
)
12836 (lambda (b) (string-match "\*Org .*Export" (buffer-name b
))))
12837 ((eq predicate
'agenda
)
12839 (with-current-buffer b
12840 (and (eq major-mode
'org-mode
)
12841 (setq bfn
(buffer-file-name b
))
12842 (member (file-truename bfn
) agenda-files
)))))
12843 (t (lambda (b) (with-current-buffer b
12844 (or (eq major-mode
'org-mode
)
12845 (string-match "\*Org .*Export"
12846 (buffer-name b
)))))))))
12850 (if (and (funcall filter b
)
12851 (or (not exclude-tmp
)
12852 (not (string-match "tmp" (buffer-name b
)))))
12857 (defun org-agenda-files (&optional unrestricted archives
)
12858 "Get the list of agenda files.
12859 Optional UNRESTRICTED means return the full list even if a restriction
12860 is currently in place.
12861 When ARCHIVES is t, include all archive files hat are really being
12862 used by the agenda files. If ARCHIVE is `ifmode', do this only if
12863 `org-agenda-archives-mode' is t."
12866 ((and (not unrestricted
) (get 'org-agenda-files
'org-restrict
)))
12867 ((stringp org-agenda-files
) (org-read-agenda-file-list))
12868 ((listp org-agenda-files
) org-agenda-files
)
12869 (t (error "Invalid value of `org-agenda-files'")))))
12870 (setq files
(apply 'append
12871 (mapcar (lambda (f)
12872 (if (file-directory-p f
)
12874 f t org-agenda-file-regexp
)
12877 (when org-agenda-skip-unavailable-files
12878 (setq files
(delq nil
12881 (and (file-readable-p file
) file
)))
12883 (when (or (eq archives t
)
12884 (and (eq archives
'ifmode
) (eq org-agenda-archives-mode t
)))
12885 (setq files
(org-add-archive-files files
)))
12888 (defun org-edit-agenda-file-list ()
12889 "Edit the list of agenda files.
12890 Depending on setup, this either uses customize to edit the variable
12891 `org-agenda-files', or it visits the file that is holding the list. In the
12892 latter case, the buffer is set up in a way that saving it automatically kills
12893 the buffer and restores the previous window configuration."
12895 (if (stringp org-agenda-files
)
12896 (let ((cw (current-window-configuration)))
12897 (find-file org-agenda-files
)
12898 (org-set-local 'org-window-configuration cw
)
12899 (org-add-hook 'after-save-hook
12901 (set-window-configuration
12902 (prog1 org-window-configuration
12903 (kill-buffer (current-buffer))))
12904 (org-install-agenda-files-menu)
12905 (message "New agenda file list installed"))
12907 (message "%s" (substitute-command-keys
12908 "Edit list and finish with \\[save-buffer]")))
12909 (customize-variable 'org-agenda-files
)))
12911 (defun org-store-new-agenda-file-list (list)
12912 "Set new value for the agenda file list and save it correctly."
12913 (if (stringp org-agenda-files
)
12914 (let ((f org-agenda-files
) b
)
12915 (while (setq b
(find-buffer-visiting f
)) (kill-buffer b
))
12917 (insert (mapconcat 'identity list
"\n") "\n")))
12918 (let ((org-mode-hook nil
) (default-major-mode 'fundamental-mode
))
12919 (setq org-agenda-files list
)
12920 (customize-save-variable 'org-agenda-files org-agenda-files
))))
12922 (defun org-read-agenda-file-list ()
12923 "Read the list of agenda files from a file."
12924 (when (file-directory-p org-agenda-files
)
12925 (error "`org-agenda-files' cannot be a single directory"))
12926 (when (stringp org-agenda-files
)
12928 (insert-file-contents org-agenda-files
)
12929 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
12933 (defun org-cycle-agenda-files ()
12934 "Cycle through the files in `org-agenda-files'.
12935 If the current buffer visits an agenda file, find the next one in the list.
12936 If the current buffer does not, find the first agenda file."
12938 (let* ((fs (org-agenda-files t
))
12939 (files (append fs
(list (car fs
))))
12940 (tcf (if buffer-file-name
(file-truename buffer-file-name
)))
12942 (unless files
(error "No agenda files"))
12944 (while (setq file
(pop files
))
12945 (if (equal (file-truename file
) tcf
)
12947 (find-file (car files
))
12949 (find-file (car fs
)))
12950 (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
12952 (defun org-agenda-file-to-front (&optional to-end
)
12953 "Move/add the current file to the top of the agenda file list.
12954 If the file is not present in the list, it is added to the front. If it is
12955 present, it is moved there. With optional argument TO-END, add/move to the
12958 (let ((org-agenda-skip-unavailable-files nil
)
12959 (file-alist (mapcar (lambda (x)
12960 (cons (file-truename x
) x
))
12961 (org-agenda-files t
)))
12962 (ctf (file-truename buffer-file-name
))
12964 (setq x
(assoc ctf file-alist
) had x
)
12966 (if (not x
) (setq x
(cons ctf
(abbreviate-file-name buffer-file-name
))))
12968 (setq file-alist
(append (delq x file-alist
) (list x
)))
12969 (setq file-alist
(cons x
(delq x file-alist
))))
12970 (org-store-new-agenda-file-list (mapcar 'cdr file-alist
))
12971 (org-install-agenda-files-menu)
12972 (message "File %s to %s of agenda file list"
12973 (if had
"moved" "added") (if to-end
"end" "front"))))
12975 (defun org-remove-file (&optional file
)
12976 "Remove current file from the list of files in variable `org-agenda-files'.
12977 These are the files which are being checked for agenda entries.
12978 Optional argument FILE means, use this file instead of the current."
12980 (let* ((org-agenda-skip-unavailable-files nil
)
12981 (file (or file buffer-file-name
))
12982 (true-file (file-truename file
))
12983 (afile (abbreviate-file-name file
))
12984 (files (delq nil
(mapcar
12986 (if (equal true-file
12989 (org-agenda-files t
)))))
12990 (if (not (= (length files
) (length (org-agenda-files t
))))
12992 (org-store-new-agenda-file-list files
)
12993 (org-install-agenda-files-menu)
12994 (message "Removed file: %s" afile
))
12995 (message "File was not in list: %s (not removed)" afile
))))
12997 (defun org-file-menu-entry (file)
12998 (vector file
(list 'find-file file
) t
))
13000 (defun org-check-agenda-file (file)
13001 "Make sure FILE exists. If not, ask user what to do."
13002 (when (not (file-exists-p file
))
13003 (message "non-existent file %s. [R]emove from list or [A]bort?"
13004 (abbreviate-file-name file
))
13005 (let ((r (downcase (read-char-exclusive))))
13008 (org-remove-file file
)
13009 (throw 'nextfile t
))
13010 (t (error "Abort"))))))
13012 (defun org-get-agenda-file-buffer (file)
13013 "Get a buffer visiting FILE. If the buffer needs to be created, add
13014 it to the list of buffers which might be released later."
13015 (let ((buf (org-find-base-buffer-visiting file
)))
13017 buf
; just return it
13018 ;; Make a new buffer and remember it
13019 (setq buf
(find-file-noselect file
))
13020 (if buf
(push buf org-agenda-new-buffers
))
13023 (defun org-release-buffers (blist)
13024 "Release all buffers in list, asking the user for confirmation when needed.
13025 When a buffer is unmodified, it is just killed. When modified, it is saved
13026 \(if the user agrees) and then killed."
13028 (while (setq buf
(pop blist
))
13029 (setq file
(buffer-file-name buf
))
13030 (when (and (buffer-modified-p buf
)
13032 (y-or-n-p (format "Save file %s? " file
)))
13033 (with-current-buffer buf
(save-buffer)))
13034 (kill-buffer buf
))))
13036 (defun org-prepare-agenda-buffers (files)
13037 "Create buffers for all agenda files, protect archived trees and comments."
13039 (let ((pa '(:org-archived t
))
13040 (pc '(:org-comment t
))
13041 (pall '(:org-archived t
:org-comment t
))
13042 (inhibit-read-only t
)
13043 (rea (concat ":" org-archive-tag
":"))
13047 (while (setq file
(pop files
))
13050 (org-check-agenda-file file
)
13051 (set-buffer (org-get-agenda-file-buffer file
)))
13053 (setq bmp
(buffer-modified-p))
13054 (org-refresh-category-properties)
13055 (setq org-todo-keywords-for-agenda
13056 (append org-todo-keywords-for-agenda org-todo-keywords-1
))
13057 (setq org-done-keywords-for-agenda
13058 (append org-done-keywords-for-agenda org-done-keywords
))
13059 (setq org-todo-keyword-alist-for-agenda
13060 (append org-todo-keyword-alist-for-agenda org-todo-key-alist
))
13061 (setq org-tag-alist-for-agenda
13062 (append org-tag-alist-for-agenda org-tag-alist
))
13065 (remove-text-properties (point-min) (point-max) pall
)
13066 (when org-agenda-skip-archived-trees
13067 (goto-char (point-min))
13068 (while (re-search-forward rea nil t
)
13069 (if (org-on-heading-p t
)
13070 (add-text-properties (point-at-bol) (org-end-of-subtree t
) pa
))))
13071 (goto-char (point-min))
13072 (setq re
(concat "^\\*+ +" org-comment-string
"\\>"))
13073 (while (re-search-forward re nil t
)
13074 (add-text-properties
13075 (match-beginning 0) (org-end-of-subtree t
) pc
)))
13076 (set-buffer-modified-p bmp
))))
13077 (setq org-todo-keyword-alist-for-agenda
13078 (org-uniquify org-todo-keyword-alist-for-agenda
)
13079 org-tag-alist-for-agenda
(org-uniquify org-tag-alist-for-agenda
))))
13081 ;;;; Embedded LaTeX
13083 (defvar org-cdlatex-mode-map
(make-sparse-keymap)
13084 "Keymap for the minor `org-cdlatex-mode'.")
13086 (org-defkey org-cdlatex-mode-map
"_" 'org-cdlatex-underscore-caret
)
13087 (org-defkey org-cdlatex-mode-map
"^" 'org-cdlatex-underscore-caret
)
13088 (org-defkey org-cdlatex-mode-map
"`" 'cdlatex-math-symbol
)
13089 (org-defkey org-cdlatex-mode-map
"'" 'org-cdlatex-math-modify
)
13090 (org-defkey org-cdlatex-mode-map
"\C-c{" 'cdlatex-environment
)
13092 (defvar org-cdlatex-texmathp-advice-is-done nil
13093 "Flag remembering if we have applied the advice to texmathp already.")
13095 (define-minor-mode org-cdlatex-mode
13096 "Toggle the minor `org-cdlatex-mode'.
13097 This mode supports entering LaTeX environment and math in LaTeX fragments
13099 \\{org-cdlatex-mode-map}"
13101 (when org-cdlatex-mode
(require 'cdlatex
))
13102 (unless org-cdlatex-texmathp-advice-is-done
13103 (setq org-cdlatex-texmathp-advice-is-done t
)
13104 (defadvice texmathp
(around org-math-always-on activate
)
13105 "Always return t in org-mode buffers.
13106 This is because we want to insert math symbols without dollars even outside
13107 the LaTeX math segments. If Orgmode thinks that point is actually inside
13108 an embedded LaTeX fragment, let texmathp do its job.
13109 \\[org-cdlatex-mode-map]"
13113 ((not (org-mode-p)) ad-do-it
)
13114 ((eq this-command
'cdlatex-math-symbol
)
13115 (setq ad-return-value t
13116 texmathp-why
'("cdlatex-math-symbol in org-mode" .
0)))
13118 (let ((p (org-inside-LaTeX-fragment-p)))
13119 (if (and p
(member (car p
) (plist-get org-format-latex-options
:matchers
)))
13120 (setq ad-return-value t
13121 texmathp-why
'("Org-mode embedded math" .
0))
13122 (if p ad-do-it
)))))))))
13124 (defun turn-on-org-cdlatex ()
13125 "Unconditionally turn on `org-cdlatex-mode'."
13126 (org-cdlatex-mode 1))
13128 (defun org-inside-LaTeX-fragment-p ()
13129 "Test if point is inside a LaTeX fragment.
13130 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
13131 sequence appearing also before point.
13132 Even though the matchers for math are configurable, this function assumes
13133 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
13134 delimiters are skipped when they have been removed by customization.
13135 The return value is nil, or a cons cell with the delimiter and
13136 and the position of this delimiter.
13138 This function does a reasonably good job, but can locally be fooled by
13139 for example currency specifications. For example it will assume being in
13140 inline math after \"$22.34\". The LaTeX fragment formatter will only format
13141 fragments that are properly closed, but during editing, we have to live
13142 with the uncertainty caused by missing closing delimiters. This function
13143 looks only before point, not after."
13145 (let ((pos (point))
13146 (dodollar (member "$" (plist-get org-format-latex-options
:matchers
)))
13148 (re-search-backward (concat "^\\(" paragraph-start
"\\)") nil t
)
13150 dd-on str
(start 0) m re
)
13153 (setq str
(concat (buffer-substring lim
(point)) "\000 X$.")
13154 re
(nth 1 (assoc "$" org-latex-regexps
)))
13155 (while (string-match re str start
)
13157 ((= (match-end 0) (length str
))
13158 (throw 'exit
(cons "$" (+ lim
(match-beginning 0) 1))))
13159 ((= (match-end 0) (- (length str
) 5))
13161 (t (setq start
(match-end 0))))))
13162 (when (setq m
(re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t
))
13164 (and (match-beginning 1) (throw 'exit
(cons (match-string 1) m
)))
13165 (and (match-beginning 2) (throw 'exit nil
))
13167 (while (re-search-backward "\\$\\$" lim t
)
13168 (setq dd-on
(not dd-on
)))
13170 (if dd-on
(cons "$$" m
))))))
13173 (defun org-try-cdlatex-tab ()
13174 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
13175 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
13176 - inside a LaTeX fragment, or
13177 - after the first word in a line, where an abbreviation expansion could
13178 insert a LaTeX environment."
13179 (when org-cdlatex-mode
13182 (skip-chars-backward "a-zA-Z0-9*")
13183 (skip-chars-backward " \t")
13186 ((org-inside-LaTeX-fragment-p)
13190 (defun org-cdlatex-underscore-caret (&optional arg
)
13191 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
13192 Revert to the normal definition outside of these fragments."
13194 (if (org-inside-LaTeX-fragment-p)
13195 (call-interactively 'cdlatex-sub-superscript
)
13196 (let (org-cdlatex-mode)
13197 (call-interactively (key-binding (vector last-input-event
))))))
13199 (defun org-cdlatex-math-modify (&optional arg
)
13200 "Execute `cdlatex-math-modify' in LaTeX fragments.
13201 Revert to the normal definition outside of these fragments."
13203 (if (org-inside-LaTeX-fragment-p)
13204 (call-interactively 'cdlatex-math-modify
)
13205 (let (org-cdlatex-mode)
13206 (call-interactively (key-binding (vector last-input-event
))))))
13208 (defvar org-latex-fragment-image-overlays nil
13209 "List of overlays carrying the images of latex fragments.")
13210 (make-variable-buffer-local 'org-latex-fragment-image-overlays
)
13212 (defun org-remove-latex-fragment-image-overlays ()
13213 "Remove all overlays with LaTeX fragment images in current buffer."
13214 (mapc 'org-delete-overlay org-latex-fragment-image-overlays
)
13215 (setq org-latex-fragment-image-overlays nil
))
13217 (defun org-preview-latex-fragment (&optional subtree
)
13218 "Preview the LaTeX fragment at point, or all locally or globally.
13219 If the cursor is in a LaTeX fragment, create the image and overlay
13220 it over the source code. If there is no fragment at point, display
13221 all fragments in the current text, from one headline to the next. With
13222 prefix SUBTREE, display all fragments in the current subtree. With a
13223 double prefix `C-u C-u', or when the cursor is before the first headline,
13224 display all fragments in the buffer.
13225 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
13227 (org-remove-latex-fragment-image-overlays)
13230 (let (beg end at msg
)
13232 ((or (equal subtree
'(16))
13233 (not (save-excursion
13234 (re-search-backward (concat "^" outline-regexp
) nil t
))))
13235 (setq beg
(point-min) end
(point-max)
13236 msg
"Creating images for buffer...%s"))
13237 ((equal subtree
'(4))
13238 (org-back-to-heading)
13239 (setq beg
(point) end
(org-end-of-subtree t
)
13240 msg
"Creating images for subtree...%s"))
13242 (if (setq at
(org-inside-LaTeX-fragment-p))
13243 (goto-char (max (point-min) (- (cdr at
) 2)))
13244 (org-back-to-heading))
13245 (setq beg
(point) end
(progn (outline-next-heading) (point))
13246 msg
(if at
"Creating image...%s"
13247 "Creating images for entry...%s"))))
13249 (narrow-to-region beg end
)
13252 (concat "ltxpng/" (file-name-sans-extension
13253 (file-name-nondirectory
13254 buffer-file-name
)))
13255 default-directory
'overlays msg at
'forbuffer
)
13256 (message msg
"done. Use `C-c C-c' to remove images.")))))
13258 (defvar org-latex-regexps
13259 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t
)
13260 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
13261 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
13262 ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil
)
13263 ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil
)
13264 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil
)
13265 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t
)
13266 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t
))
13267 "Regular expressions for matching embedded LaTeX.")
13269 (defun org-format-latex (prefix &optional dir overlays msg at forbuffer
)
13270 "Replace LaTeX fragments with links to an image, and produce images."
13271 (if (and overlays
(fboundp 'clear-image-cache
)) (clear-image-cache))
13272 (let* ((prefixnodir (file-name-nondirectory prefix
))
13273 (absprefix (expand-file-name prefix dir
))
13274 (todir (file-name-directory absprefix
))
13275 (opt org-format-latex-options
)
13276 (matchers (plist-get opt
:matchers
))
13277 (re-list org-latex-regexps
)
13278 (cnt 0) txt link beg end re e checkdir
13279 m n block linkfile movefile ov
)
13280 ;; Check if there are old images files with this prefix, and remove them
13281 (when (file-directory-p todir
)
13285 (concat (regexp-quote prefixnodir
) "_[0-9]+\\.png$"))))
13286 ;; Check the different regular expressions
13287 (while (setq e
(pop re-list
))
13288 (setq m
(car e
) re
(nth 1 e
) n
(nth 2 e
)
13289 block
(if (nth 3 e
) "\n\n" ""))
13290 (when (member m matchers
)
13291 (goto-char (point-min))
13292 (while (re-search-forward re nil t
)
13293 (when (and (or (not at
) (equal (cdr at
) (match-beginning n
)))
13294 (not (get-text-property (match-beginning n
)
13296 (setq txt
(match-string n
)
13297 beg
(match-beginning n
) end
(match-end n
)
13299 linkfile
(format "%s_%04d.png" prefix cnt
)
13300 movefile
(format "%s_%04d.png" absprefix cnt
)
13301 link
(concat block
"[[file:" linkfile
"]]" block
))
13302 (if msg
(message msg cnt
))
13304 (unless checkdir
; make sure the directory exists
13306 (or (file-directory-p todir
) (make-directory todir
)))
13307 (org-create-formula-image
13308 txt movefile opt forbuffer
)
13311 (setq ov
(org-make-overlay beg end
))
13312 (if (featurep 'xemacs
)
13314 (org-overlay-put ov
'invisible t
)
13317 (make-glyph (vector 'png
:file movefile
))))
13320 (list 'image
:type
'png
:file movefile
:ascent
'center
)))
13321 (push ov org-latex-fragment-image-overlays
)
13323 (delete-region beg end
)
13324 (insert link
))))))))
13326 ;; This function borrows from Ganesh Swami's latex2png.el
13327 (defun org-create-formula-image (string tofile options buffer
)
13328 (let* ((tmpdir (if (featurep 'xemacs
)
13330 temporary-file-directory
))
13331 (texfilebase (make-temp-name
13332 (expand-file-name "orgtex" tmpdir
)))
13333 (texfile (concat texfilebase
".tex"))
13334 (dvifile (concat texfilebase
".dvi"))
13335 (pngfile (concat texfilebase
".png"))
13336 (fnh (if (featurep 'xemacs
)
13337 (font-height (get-face-font 'default
))
13338 (face-attribute 'default
:height nil
)))
13339 (scale (or (plist-get options
(if buffer
:scale
:html-scale
)) 1.0))
13340 (dpi (number-to-string (* scale
(floor (* 0.9 (if buffer fnh
140.
))))))
13341 (fg (or (plist-get options
(if buffer
:foreground
:html-foreground
))
13343 (bg (or (plist-get options
(if buffer
:background
:html-background
))
13345 (if (eq fg
'default
) (setq fg
(org-dvipng-color :foreground
)))
13346 (if (eq bg
'default
) (setq bg
(org-dvipng-color :background
)))
13347 (with-temp-file texfile
13348 (insert org-format-latex-header
13349 "\n\\begin{document}\n" string
"\n\\end{document}\n"))
13350 (let ((dir default-directory
))
13351 (condition-case nil
13354 (call-process "latex" nil nil nil texfile
))
13357 (if (not (file-exists-p dvifile
))
13358 (progn (message "Failed to create dvi file from %s" texfile
) nil
)
13359 (condition-case nil
13360 (call-process "dvipng" nil nil nil
13361 "-E" "-fg" fg
"-bg" bg
13363 ;;"-x" scale "-y" scale
13368 (if (not (file-exists-p pngfile
))
13369 (progn (message "Failed to create png file from %s" texfile
) nil
)
13370 ;; Use the requested file name and clean up
13371 (copy-file pngfile tofile
'replace
)
13372 (loop for e in
'(".dvi" ".tex" ".aux" ".log" ".png") do
13373 (delete-file (concat texfilebase e
)))
13376 (defun org-dvipng-color (attr)
13377 "Return an rgb color specification for dvipng."
13378 (apply 'format
"rgb %s %s %s"
13379 (mapcar 'org-normalize-color
13380 (color-values (face-attribute 'default attr nil
)))))
13382 (defun org-normalize-color (value)
13383 "Return string to be used as color value for an RGB component."
13384 (format "%g" (/ value
65535.0)))
13388 ;; Make `C-c C-x' a prefix key
13389 (org-defkey org-mode-map
"\C-c\C-x" (make-sparse-keymap))
13391 ;; TAB key with modifiers
13392 (org-defkey org-mode-map
"\C-i" 'org-cycle
)
13393 (org-defkey org-mode-map
[(tab)] 'org-cycle
)
13394 (org-defkey org-mode-map
[(control tab
)] 'org-force-cycle-archived
)
13395 (org-defkey org-mode-map
[(meta tab
)] 'org-complete
)
13396 (org-defkey org-mode-map
"\M-\t" 'org-complete
)
13397 (org-defkey org-mode-map
"\M-\C-i" 'org-complete
)
13398 ;; The following line is necessary under Suse GNU/Linux
13399 (unless (featurep 'xemacs
)
13400 (org-defkey org-mode-map
[S-iso-lefttab
] 'org-shifttab
))
13401 (org-defkey org-mode-map
[(shift tab
)] 'org-shifttab
)
13402 (define-key org-mode-map
[backtab] 'org-shifttab)
13404 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
13405 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
13406 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
13408 ;; Cursor keys with modifiers
13409 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
13410 (org-defkey org-mode-map [(meta right)] 'org-metaright)
13411 (org-defkey org-mode-map [(meta up)] 'org-metaup)
13412 (org-defkey org-mode-map [(meta down)] 'org-metadown)
13414 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
13415 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
13416 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
13417 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
13419 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
13420 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
13421 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
13422 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
13424 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
13425 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
13427 ;;; Extra keys for tty access.
13428 ;; We only set them when really needed because otherwise the
13429 ;; menus don't show the simple keys
13431 (when (or org-use-extra-keys
13432 (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
13433 (not window-system))
13434 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
13435 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
13436 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
13437 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
13438 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
13439 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
13440 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
13441 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
13442 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
13443 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
13444 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
13445 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
13446 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
13447 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
13448 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
13449 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
13450 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
13451 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
13452 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
13453 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
13454 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
13455 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
13457 ;; All the other keys
13459 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
13460 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
13461 (if (boundp 'narrow-map)
13462 (org-defkey narrow-map "s" 'org-narrow-to-subtree)
13463 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
13464 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
13465 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
13466 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
13467 (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
13468 (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
13469 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
13470 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
13471 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
13472 (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
13473 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
13474 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
13475 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
13476 (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
13477 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
13478 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
13479 (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
13480 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
13481 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
13482 (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
13483 (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
13484 (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
13485 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
13486 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
13487 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
13488 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
13489 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
13490 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
13491 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
13492 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
13493 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
13494 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
13495 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
13496 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
13497 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
13498 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
13499 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
13500 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
13501 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
13502 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
13503 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
13504 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
13505 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
13506 (org-defkey org-mode-map "\C-c^" 'org-sort)
13507 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
13508 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
13509 (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
13510 (org-defkey org-mode-map "\C-m" 'org-return)
13511 (org-defkey org-mode-map "\C-j" 'org-return-indent)
13512 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
13513 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
13514 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
13515 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
13516 (org-defkey org-mode-map "\C-c'" 'org-edit-special)
13517 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
13518 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
13519 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
13520 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
13521 (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
13522 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
13523 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
13524 (org-defkey org-mode-map "\C-c\C-e" 'org-export)
13525 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
13526 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
13527 (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
13529 (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
13530 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
13531 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
13532 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
13534 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
13535 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
13536 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
13537 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
13538 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
13539 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
13540 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
13541 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
13542 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
13543 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
13544 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
13545 (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
13546 (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
13548 (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
13549 (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
13550 (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
13551 (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
13553 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
13555 (define-key org-mode-map "\C-c\C-xr" 'org-reload)
13557 (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
13558 (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
13560 (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
13563 (when (featurep 'xemacs)
13564 (org-defkey org-mode-map 'button3 'popup-mode-menu))
13567 (defvar org-self-insert-command-undo-counter 0)
13569 (defvar org-table-auto-blank-field) ; defined in org-table.el
13570 (defun org-self-insert-command (N)
13571 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
13572 If the cursor is in a table looking at whitespace, the whitespace is
13573 overwritten, and the table is not marked as requiring realignment."
13578 ;; check if we blank the field, and if that triggers align
13579 (and (featurep 'org-table) org-table-auto-blank-field
13580 (member last-command
13581 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
13582 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
13583 ;; got extra space, this field does not determine column width
13584 (let (org-table-may-need-update) (org-table-blank-field))
13585 ;; no extra space, this field may determine column width
13586 (org-table-blank-field)))
13589 (looking-at "[^|\n]* |"))
13590 (let (org-table-may-need-update)
13591 (goto-char (1- (match-end 0)))
13592 (delete-backward-char 1)
13593 (goto-char (match-beginning 0))
13594 (self-insert-command N))
13595 (setq org-table-may-need-update t)
13596 (self-insert-command N)
13597 (org-fix-tags-on-the-fly)
13598 (if org-self-insert-cluster-for-undo
13599 (if (not (eq last-command 'org-self-insert-command))
13600 (setq org-self-insert-command-undo-counter 1)
13601 (if (>= org-self-insert-command-undo-counter 20)
13602 (setq org-self-insert-command-undo-counter 1)
13603 (and (> org-self-insert-command-undo-counter 0)
13605 (not (cadr buffer-undo-list)) ; remove nil entry
13606 (setcdr buffer-undo-list (cddr buffer-undo-list)))
13607 (setq org-self-insert-command-undo-counter
13608 (1+ org-self-insert-command-undo-counter)))))))
13610 (defun org-fix-tags-on-the-fly ()
13611 (when (and (equal (char-after (point-at-bol)) ?*)
13612 (org-on-heading-p))
13613 (org-align-tags-here org-tags-column)))
13615 (defun org-delete-backward-char (N)
13616 "Like `delete-backward-char', insert whitespace at field end in tables.
13617 When deleting backwards, in tables this function will insert whitespace in
13618 front of the next \"|\" separator, to keep the table aligned. The table will
13619 still be marked for re-alignment if the field did fill the entire column,
13620 because, in this case the deletion might narrow the column."
13622 (if (and (org-table-p)
13624 (string-match "|" (buffer-substring (point-at-bol) (point)))
13625 (looking-at ".*?|"))
13626 (let ((pos (point))
13627 (noalign (looking-at "[^|\n\r]* |"))
13628 (c org-table-may-need-update))
13629 (backward-delete-char N)
13630 (skip-chars-forward "^|")
13632 (goto-char (1- pos))
13633 ;; noalign: if there were two spaces at the end, this field
13634 ;; does not determine the width of the column.
13635 (if noalign (setq org-table-may-need-update c)))
13636 (backward-delete-char N)
13637 (org-fix-tags-on-the-fly)))
13639 (defun org-delete-char (N)
13640 "Like `delete-char', but insert whitespace at field end in tables.
13641 When deleting characters, in tables this function will insert whitespace in
13642 front of the next \"|\" separator, to keep the table aligned. The table will
13643 still be marked for re-alignment if the field did fill the entire column,
13644 because, in this case the deletion might narrow the column."
13646 (if (and (org-table-p)
13648 (not (= (char-after) ?|))
13650 (if (looking-at ".*?|")
13651 (let ((pos (point))
13652 (noalign (looking-at "[^|\n\r]* |"))
13653 (c org-table-may-need-update))
13654 (replace-match (concat
13655 (substring (match-string 0) 1 -1)
13658 ;; noalign: if there were two spaces at the end, this field
13659 ;; does not determine the width of the column.
13660 (if noalign (setq org-table-may-need-update c)))
13663 (org-fix-tags-on-the-fly)))
13665 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
13666 (put 'org-self-insert-command 'delete-selection t)
13667 (put 'orgtbl-self-insert-command 'delete-selection t)
13668 (put 'org-delete-char 'delete-selection 'supersede)
13669 (put 'org-delete-backward-char 'delete-selection 'supersede)
13670 (put 'org-yank 'delete-selection 'yank)
13672 ;; Make `flyspell-mode' delay after some commands
13673 (put 'org-self-insert-command 'flyspell-delayed t)
13674 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
13675 (put 'org-delete-char 'flyspell-delayed t)
13676 (put 'org-delete-backward-char 'flyspell-delayed t)
13678 ;; Make pabbrev-mode expand after org-mode commands
13679 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
13680 (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
13682 ;; How to do this: Measure non-white length of current string
13683 ;; If equal to column width, we should realign.
13685 (defun org-remap (map &rest commands)
13686 "In MAP, remap the functions given in COMMANDS.
13687 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
13690 (setq old (pop commands) new (pop commands))
13691 (if (fboundp 'command-remapping)
13692 (org-defkey map (vector 'remap old) new)
13693 (substitute-key-definition old new map global-map)))))
13695 (when (eq org-enable-table-editor 'optimized)
13696 ;; If the user wants maximum table support, we need to hijack
13697 ;; some standard editing functions
13698 (org-remap org-mode-map
13699 'self-insert-command 'org-self-insert-command
13700 'delete-char 'org-delete-char
13701 'delete-backward-char 'org-delete-backward-char)
13702 (org-defkey org-mode-map "|" 'org-force-self-insert))
13704 (defvar org-ctrl-c-ctrl-c-hook nil
13705 "Hook for functions attaching themselves to `C-c C-c'.
13706 This can be used to add additional functionality to the C-c C-c key which
13707 executes context-dependent commands.
13708 Each function will be called with no arguments. The function must check
13709 if the context is appropriate for it to act. If yes, it should do its
13710 thing and then return a non-nil value. If the context is wrong,
13713 (defvar org-metaleft-hook nil
13714 "Hook for functions attaching themselves to `M-left'.
13715 See `org-ctrl-c-ctrl-c-hook' for more information.")
13716 (defvar org-metaright-hook nil
13717 "Hook for functions attaching themselves to `M-right'.
13718 See `org-ctrl-c-ctrl-c-hook' for more information.")
13719 (defvar org-metaup-hook nil
13720 "Hook for functions attaching themselves to `M-up'.
13721 See `org-ctrl-c-ctrl-c-hook' for more information.")
13722 (defvar org-metadown-hook nil
13723 "Hook for functions attaching themselves to `M-down'.
13724 See `org-ctrl-c-ctrl-c-hook' for more information.")
13725 (defvar org-shiftmetaleft-hook nil
13726 "Hook for functions attaching themselves to `M-S-left'.
13727 See `org-ctrl-c-ctrl-c-hook' for more information.")
13728 (defvar org-shiftmetaright-hook nil
13729 "Hook for functions attaching themselves to `M-S-right'.
13730 See `org-ctrl-c-ctrl-c-hook' for more information.")
13731 (defvar org-shiftmetaup-hook nil
13732 "Hook for functions attaching themselves to `M-S-up'.
13733 See `org-ctrl-c-ctrl-c-hook' for more information.")
13734 (defvar org-shiftmetadown-hook nil
13735 "Hook for functions attaching themselves to `M-S-down'.
13736 See `org-ctrl-c-ctrl-c-hook' for more information.")
13737 (defvar org-metareturn-hook nil
13738 "Hook for functions attaching themselves to `M-RET'.
13739 See `org-ctrl-c-ctrl-c-hook' for more information.")
13741 (defun org-modifier-cursor-error ()
13742 "Throw an error, a modified cursor command was applied in wrong context."
13743 (error "This command is active in special context like tables, headlines or items"))
13745 (defun org-shiftselect-error ()
13746 "Throw an error because Shift-Cursor command was applied in wrong context."
13747 (if (and (boundp 'shift-select-mode) shift-select-mode)
13748 (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
13749 (error "This command works only in special context like headlines or timestamps.")))
13751 (defun org-call-for-shift-select (cmd)
13752 (let ((this-command-keys-shift-translated t))
13753 (call-interactively cmd)))
13755 (defun org-shifttab (&optional arg)
13756 "Global visibility cycling or move to previous table field.
13757 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
13759 See the individual commands for more information."
13762 ((org-at-table-p) (call-interactively 'org-table-previous-field))
13764 (message "Content view to level: %d" arg)
13765 (org-content (prefix-numeric-value arg))
13766 (setq org-cycle-global-status 'overview))
13767 (t (call-interactively 'org-global-cycle))))
13769 (defun org-shiftmetaleft ()
13770 "Promote subtree or delete table column.
13771 Calls `org-promote-subtree', `org-outdent-item',
13772 or `org-table-delete-column', depending on context.
13773 See the individual commands for more information."
13776 ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
13777 ((org-at-table-p) (call-interactively 'org-table-delete-column))
13778 ((org-on-heading-p) (call-interactively 'org-promote-subtree))
13779 ((org-at-item-p) (call-interactively 'org-outdent-item))
13780 (t (org-modifier-cursor-error))))
13782 (defun org-shiftmetaright ()
13783 "Demote subtree or insert table column.
13784 Calls `org-demote-subtree', `org-indent-item',
13785 or `org-table-insert-column', depending on context.
13786 See the individual commands for more information."
13789 ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
13790 ((org-at-table-p) (call-interactively 'org-table-insert-column))
13791 ((org-on-heading-p) (call-interactively 'org-demote-subtree))
13792 ((org-at-item-p) (call-interactively 'org-indent-item))
13793 (t (org-modifier-cursor-error))))
13795 (defun org-shiftmetaup (&optional arg)
13796 "Move subtree up or kill table row.
13797 Calls `org-move-subtree-up' or `org-table-kill-row' or
13798 `org-move-item-up' depending on context. See the individual commands
13799 for more information."
13802 ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
13803 ((org-at-table-p) (call-interactively 'org-table-kill-row))
13804 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
13805 ((org-at-item-p) (call-interactively 'org-move-item-up))
13806 (t (org-modifier-cursor-error))))
13808 (defun org-shiftmetadown (&optional arg)
13809 "Move subtree down or insert table row.
13810 Calls `org-move-subtree-down' or `org-table-insert-row' or
13811 `org-move-item-down', depending on context. See the individual
13812 commands for more information."
13815 ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
13816 ((org-at-table-p) (call-interactively 'org-table-insert-row))
13817 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
13818 ((org-at-item-p) (call-interactively 'org-move-item-down))
13819 (t (org-modifier-cursor-error))))
13821 (defun org-metaleft (&optional arg)
13822 "Promote heading or move table column to left.
13823 Calls `org-do-promote' or `org-table-move-column', depending on context.
13824 With no specific context, calls the Emacs default `backward-word'.
13825 See the individual commands for more information."
13828 ((run-hook-with-args-until-success 'org-metaleft-hook))
13829 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
13830 ((or (org-on-heading-p)
13831 (and (org-region-active-p)
13833 (goto-char (region-beginning))
13834 (org-on-heading-p))))
13835 (call-interactively 'org-do-promote))
13836 ((or (org-at-item-p)
13837 (and (org-region-active-p)
13839 (goto-char (region-beginning))
13841 (call-interactively 'org-outdent-item))
13842 (t (call-interactively 'backward-word))))
13844 (defun org-metaright (&optional arg)
13845 "Demote subtree or move table column to right.
13846 Calls `org-do-demote' or `org-table-move-column', depending on context.
13847 With no specific context, calls the Emacs default `forward-word'.
13848 See the individual commands for more information."
13851 ((run-hook-with-args-until-success 'org-metaright-hook))
13852 ((org-at-table-p) (call-interactively 'org-table-move-column))
13853 ((or (org-on-heading-p)
13854 (and (org-region-active-p)
13856 (goto-char (region-beginning))
13857 (org-on-heading-p))))
13858 (call-interactively 'org-do-demote))
13859 ((or (org-at-item-p)
13860 (and (org-region-active-p)
13862 (goto-char (region-beginning))
13864 (call-interactively 'org-indent-item))
13865 (t (call-interactively 'forward-word))))
13867 (defun org-metaup (&optional arg)
13868 "Move subtree up or move table row up.
13869 Calls `org-move-subtree-up' or `org-table-move-row' or
13870 `org-move-item-up', depending on context. See the individual commands
13871 for more information."
13874 ((run-hook-with-args-until-success 'org-metaup-hook))
13875 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
13876 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
13877 ((org-at-item-p) (call-interactively 'org-move-item-up))
13878 (t (transpose-lines 1) (beginning-of-line -1))))
13880 (defun org-metadown (&optional arg)
13881 "Move subtree down or move table row down.
13882 Calls `org-move-subtree-down' or `org-table-move-row' or
13883 `org-move-item-down', depending on context. See the individual
13884 commands for more information."
13887 ((run-hook-with-args-until-success 'org-metadown-hook))
13888 ((org-at-table-p) (call-interactively 'org-table-move-row))
13889 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
13890 ((org-at-item-p) (call-interactively 'org-move-item-down))
13891 (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
13893 (defun org-shiftup (&optional arg)
13894 "Increase item in timestamp or increase priority of current headline.
13895 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
13896 depending on context. See the individual commands for more information."
13899 ((and org-support-shift-select (org-region-active-p))
13900 (org-call-for-shift-select 'previous-line))
13901 ((org-at-timestamp-p t)
13902 (call-interactively (if org-edit-timestamp-down-means-later
13903 'org-timestamp-down 'org-timestamp-up)))
13904 ((and (not (eq org-support-shift-select 'always))
13905 (org-on-heading-p))
13906 (call-interactively 'org-priority-up))
13907 ((and (not org-support-shift-select) (org-at-item-p))
13908 (call-interactively 'org-previous-item))
13909 ((org-clocktable-try-shift 'up arg))
13910 (org-support-shift-select
13911 (org-call-for-shift-select 'previous-line))
13912 (t (org-shiftselect-error))))
13914 (defun org-shiftdown (&optional arg)
13915 "Decrease item in timestamp or decrease priority of current headline.
13916 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
13917 depending on context. See the individual commands for more information."
13920 ((and org-support-shift-select (org-region-active-p))
13921 (org-call-for-shift-select 'next-line))
13922 ((org-at-timestamp-p t)
13923 (call-interactively (if org-edit-timestamp-down-means-later
13924 'org-timestamp-up 'org-timestamp-down)))
13925 ((and (not (eq org-support-shift-select 'always))
13926 (org-on-heading-p))
13927 (call-interactively 'org-priority-down))
13928 ((and (not org-support-shift-select) (org-at-item-p))
13929 (call-interactively 'org-next-item))
13930 ((org-clocktable-try-shift 'down arg))
13931 (org-support-shift-select
13932 (org-call-for-shift-select 'next-line))
13933 (t (org-shiftselect-error))))
13935 (defun org-shiftright (&optional arg)
13936 "Cycle the thing at point or in the current line, depending on context.
13937 Depending on context, this does one of the following:
13939 - switch a timestamp at point one day into the future
13940 - on a headline, switch to the next TODO keyword.
13941 - on an item, switch entire list to the next bullet type
13942 - on a property line, switch to the next allowed value
13943 - on a clocktable definition line, move time block into the future"
13946 ((and org-support-shift-select (org-region-active-p))
13947 (org-call-for-shift-select 'forward-char))
13948 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
13949 ((and (not (eq org-support-shift-select 'always))
13950 (org-on-heading-p))
13951 (org-call-with-arg 'org-todo 'right))
13952 ((or (and org-support-shift-select
13953 (not (eq org-support-shift-select 'always))
13954 (org-at-item-bullet-p))
13955 (and (not org-support-shift-select) (org-at-item-p)))
13956 (org-call-with-arg 'org-cycle-list-bullet nil))
13957 ((and (not (eq org-support-shift-select 'always))
13958 (org-at-property-p))
13959 (call-interactively 'org-property-next-allowed-value))
13960 ((org-clocktable-try-shift 'right arg))
13961 (org-support-shift-select
13962 (org-call-for-shift-select 'forward-char))
13963 (t (org-shiftselect-error))))
13965 (defun org-shiftleft (&optional arg)
13966 "Cycle the thing at point or in the current line, depending on context.
13967 Depending on context, this does one of the following:
13969 - switch a timestamp at point one day into the past
13970 - on a headline, switch to the previous TODO keyword.
13971 - on an item, switch entire list to the previous bullet type
13972 - on a property line, switch to the previous allowed value
13973 - on a clocktable definition line, move time block into the past"
13976 ((and org-support-shift-select (org-region-active-p))
13977 (org-call-for-shift-select 'backward-char))
13978 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
13979 ((and (not (eq org-support-shift-select 'always))
13980 (org-on-heading-p))
13981 (org-call-with-arg 'org-todo 'left))
13982 ((or (and org-support-shift-select
13983 (not (eq org-support-shift-select 'always))
13984 (org-at-item-bullet-p))
13985 (and (not org-support-shift-select) (org-at-item-p)))
13986 (org-call-with-arg 'org-cycle-list-bullet 'previous))
13987 ((and (not (eq org-support-shift-select 'always))
13988 (org-at-property-p))
13989 (call-interactively 'org-property-previous-allowed-value))
13990 ((org-clocktable-try-shift 'left arg))
13991 (org-support-shift-select
13992 (org-call-for-shift-select 'backward-char))
13993 (t (org-shiftselect-error))))
13995 (defun org-shiftcontrolright ()
13996 "Switch to next TODO set."
13999 ((and org-support-shift-select (org-region-active-p))
14000 (org-call-for-shift-select 'forward-word))
14001 ((and (not (eq org-support-shift-select 'always))
14002 (org-on-heading-p))
14003 (org-call-with-arg 'org-todo 'nextset))
14004 (org-support-shift-select
14005 (org-call-for-shift-select 'forward-word))
14006 (t (org-shiftselect-error))))
14008 (defun org-shiftcontrolleft ()
14009 "Switch to previous TODO set."
14012 ((and org-support-shift-select (org-region-active-p))
14013 (org-call-for-shift-select 'backward-word))
14014 ((and (not (eq org-support-shift-select 'always))
14015 (org-on-heading-p))
14016 (org-call-with-arg 'org-todo 'previousset))
14017 (org-support-shift-select
14018 (org-call-for-shift-select 'backward-word))
14019 (t (org-shiftselect-error))))
14021 (defun org-ctrl-c-ret ()
14022 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
14025 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
14026 (t (call-interactively 'org-insert-heading))))
14028 (defun org-copy-special ()
14029 "Copy region in table or copy current subtree.
14030 Calls `org-table-copy' or `org-copy-subtree', depending on context.
14031 See the individual commands for more information."
14033 (call-interactively
14034 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
14036 (defun org-cut-special ()
14037 "Cut region in table or cut current subtree.
14038 Calls `org-table-copy' or `org-cut-subtree', depending on context.
14039 See the individual commands for more information."
14041 (call-interactively
14042 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
14044 (defun org-paste-special (arg)
14045 "Paste rectangular region into table, or past subtree relative to level.
14046 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
14047 See the individual commands for more information."
14049 (if (org-at-table-p)
14050 (org-table-paste-rectangle)
14051 (org-paste-subtree arg)))
14053 (defun org-edit-special ()
14054 "Call a special editor for the stuff at point.
14055 When at a table, call the formula editor with `org-table-edit-formulas'.
14056 When at the first line of an src example, call `org-edit-src-code'.
14057 When in an #+include line, visit the include file. Otherwise call
14058 `ffap' to visit the file at point."
14062 (call-interactively 'org-table-edit-formulas))
14064 (beginning-of-line 1)
14065 (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
14066 (find-file (org-trim (match-string 1))))
14067 ((org-edit-src-code))
14068 ((org-edit-fixed-width-region))
14069 (t (call-interactively 'ffap))))
14072 (defun org-ctrl-c-ctrl-c (&optional arg)
14073 "Set tags in headline, or update according to changed information at point.
14075 This command does many different things, depending on context:
14077 - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
14078 this is what we do.
14080 - If the cursor is in a headline, prompt for tags and insert them
14081 into the current line, aligned to `org-tags-column'. When called
14082 with prefix arg, realign all tags in the current buffer.
14084 - If the cursor is in one of the special #+KEYWORD lines, this
14085 triggers scanning the buffer for these lines and updating the
14088 - If the cursor is inside a table, realign the table. This command
14089 works even if the automatic table editor has been turned off.
14091 - If the cursor is on a #+TBLFM line, re-apply the formulas to
14094 - If the cursor is at a footnote reference or definition, jump to
14095 the corresponding definition or references, respectively.
14097 - If the cursor is a the beginning of a dynamic block, update it.
14099 - If the cursor is inside a table created by the table.el package,
14100 activate that table.
14102 - If the current buffer is a remember buffer, close note and file
14103 it. A prefix argument of 1 files to the default location
14104 without further interaction. A prefix argument of 2 files to
14105 the currently clocking task.
14107 - If the cursor is on a <<<target>>>, update radio targets and corresponding
14108 links in this buffer.
14110 - If the cursor is on a numbered item in a plain list, renumber the
14113 - If the cursor is on a checkbox, toggle it."
14115 (let ((org-enable-table-editor t))
14117 ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
14118 org-occur-highlights
14119 org-latex-fragment-image-overlays)
14120 (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
14121 (org-remove-occur-highlights)
14122 (org-remove-latex-fragment-image-overlays)
14123 (message "Temporary highlights/overlays removed from current buffer"))
14124 ((and (local-variable-p 'org-finish-function (current-buffer))
14125 (fboundp org-finish-function))
14126 (funcall org-finish-function))
14127 ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
14128 ((org-at-property-p)
14129 (call-interactively 'org-property-action))
14130 ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
14131 ((org-on-heading-p) (call-interactively 'org-set-tags))
14132 ((org-at-table.el-p)
14134 (beginning-of-line 1)
14135 (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
14136 (call-interactively 'table-recognize-table))
14138 (org-table-maybe-eval-formula)
14140 (call-interactively 'org-table-recalculate)
14141 (org-table-maybe-recalculate-line))
14142 (call-interactively 'org-table-align))
14143 ((or (org-footnote-at-reference-p)
14144 (org-footnote-at-definition-p))
14145 (call-interactively 'org-footnote-action))
14146 ((org-at-item-checkbox-p)
14147 (call-interactively 'org-toggle-checkbox))
14150 (call-interactively 'org-toggle-checkbox)
14151 (call-interactively 'org-maybe-renumber-ordered-list)))
14152 ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
14154 (beginning-of-line 1)
14155 (save-excursion (org-update-dblock)))
14156 ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
14158 ((equal (match-string 1) "TBLFM")
14159 ;; Recalculate the table before this line
14161 (beginning-of-line 1)
14162 (skip-chars-backward " \r\n\t")
14163 (if (org-at-table-p)
14164 (org-call-with-arg 'org-table-recalculate t))))
14166 ; (org-set-regexps-and-options)
14167 ; (org-restart-font-lock)
14168 (let ((org-inhibit-startup t)) (org-mode-restart))
14169 (message "Local setup has been refreshed"))))
14170 (t (error "C-c C-c can do nothing useful at this location.")))))
14172 (defun org-mode-restart ()
14173 "Restart Org-mode, to scan again for special lines.
14174 Also updates the keyword regular expressions."
14177 (message "Org-mode restarted"))
14179 (defun org-kill-note-or-show-branches ()
14180 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
14182 (if (not org-finish-function)
14183 (call-interactively 'show-branches)
14184 (let ((org-note-abort t))
14185 (funcall org-finish-function))))
14187 (defun org-return (&optional indent)
14188 "Goto next table row or insert a newline.
14189 Calls `org-table-next-row' or `newline', depending on context.
14190 See the individual commands for more information."
14193 ((bobp) (if indent (newline-and-indent) (newline)))
14195 (org-table-justify-field-maybe)
14196 (call-interactively 'org-table-next-row))
14197 ((and org-return-follows-link
14198 (eq (get-text-property (point) 'face) 'org-link))
14199 (call-interactively 'org-open-at-point))
14200 ((and (org-at-heading-p)
14202 (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
14206 (t (if indent (newline-and-indent) (newline)))))
14208 (defun org-return-indent ()
14209 "Goto next table row or insert a newline and indent.
14210 Calls `org-table-next-row' or `newline-and-indent', depending on
14211 context. See the individual commands for more information."
14215 (defun org-ctrl-c-star ()
14216 "Compute table, or change heading status of lines.
14217 Calls `org-table-recalculate' or `org-toggle-heading',
14218 depending on context."
14222 (call-interactively 'org-table-recalculate))
14224 ;; Convert all lines in region to list items
14225 (call-interactively 'org-toggle-heading))))
14227 (defun org-ctrl-c-minus ()
14228 "Insert separator line in table or modify bullet status of line.
14229 Also turns a plain line or a region of lines into list items.
14230 Calls `org-table-insert-hline', `org-toggle-item', or
14231 `org-cycle-list-bullet', depending on context."
14235 (call-interactively 'org-table-insert-hline))
14236 ((org-region-active-p)
14237 (call-interactively 'org-toggle-item))
14239 (call-interactively 'org-cycle-list-bullet))
14241 (call-interactively 'org-toggle-item))))
14243 (defun org-toggle-item ()
14244 "Convert headings or normal lines to items, items to normal lines.
14245 If there is no active region, only the current line is considered.
14247 If the first line in the region is a headline, convert all headlines to items.
14249 If the first line in the region is an item, convert all items to normal lines.
14251 If the first line is normal text, add an item bullet to each line."
14253 (let (l2 l beg end)
14254 (if (org-region-active-p)
14255 (setq beg (region-beginning) end (region-end))
14256 (setq beg (point-at-bol)
14257 end (min (1+ (point-at-eol)) (point-max))))
14260 (setq l2 (org-current-line))
14262 (beginning-of-line 1)
14263 (setq l (1- (org-current-line)))
14264 (if (org-at-item-p)
14265 ;; We already have items, de-itemize
14266 (while (< (setq l (1+ l)) l2)
14267 (when (org-at-item-p)
14268 (goto-char (match-beginning 2))
14269 (delete-region (match-beginning 2) (match-end 2))
14270 (and (looking-at "[ \t]+") (replace-match "")))
14271 (beginning-of-line 2))
14272 (if (org-on-heading-p)
14273 ;; Headings, convert to items
14274 (while (< (setq l (1+ l)) l2)
14275 (if (looking-at org-outline-regexp)
14276 (replace-match "- " t t))
14277 (beginning-of-line 2))
14278 ;; normal lines, turn them into items
14279 (while (< (setq l (1+ l)) l2)
14280 (unless (org-at-item-p)
14281 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
14282 (replace-match "\\1- \\2")))
14283 (beginning-of-line 2)))))))
14285 (defun org-toggle-heading (&optional nstars)
14286 "Convert headings to normal text, or items or text to headings.
14287 If there is no active region, only the current line is considered.
14289 If the first line is a heading, remove the stars from all headlines
14292 If the first line is a plain list item, turn all plain list items into
14295 If the first line is a normal line, turn each and every line in the region
14298 When converting a line into a heading, the number of stars is chosen
14299 such that the lines become children of the current entry. However, when
14300 a prefix argument is given, its value determines the number of stars to add."
14302 (let (l2 l itemp beg end)
14303 (if (org-region-active-p)
14304 (setq beg (region-beginning) end (region-end))
14305 (setq beg (point-at-bol)
14306 end (min (1+ (point-at-eol)) (point-max))))
14309 (setq l2 (org-current-line))
14311 (beginning-of-line 1)
14312 (setq l (1- (org-current-line)))
14313 (if (org-on-heading-p)
14314 ;; We already have headlines, de-star them
14315 (while (< (setq l (1+ l)) l2)
14316 (when (org-on-heading-p t)
14317 (and (looking-at outline-regexp) (replace-match "")))
14318 (beginning-of-line 2))
14319 (setq itemp (org-at-item-p))
14322 (make-string (prefix-numeric-value current-prefix-arg)
14325 (re-search-backward org-complex-heading-regexp nil t)
14326 (or (match-string 1) ""))))
14327 (add-stars (cond (nstars "")
14328 ((equal stars "") "*")
14329 (org-odd-levels-only "**")
14331 (rpl (concat stars add-stars " ")))
14332 (while (< (setq l (1+ l)) l2)
14334 (and (org-at-item-p) (replace-match rpl t t))
14335 (unless (org-on-heading-p)
14336 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
14337 (replace-match (concat rpl (match-string 2))))))
14338 (beginning-of-line 2)))))))
14340 (defun org-meta-return (&optional arg)
14341 "Insert a new heading or wrap a region in a table.
14342 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
14343 See the individual commands for more information."
14346 ((run-hook-with-args-until-success 'org-metareturn-hook))
14348 (call-interactively 'org-table-wrap-region))
14349 (t (call-interactively 'org-insert-heading))))
14353 ;; Define the Org-mode menus
14354 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
14356 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
14357 ["Next Field" org-cycle (org-at-table-p)]
14358 ["Previous Field" org-shifttab (org-at-table-p)]
14359 ["Next Row" org-return (org-at-table-p)]
14361 ["Blank Field" org-table-blank-field (org-at-table-p)]
14362 ["Edit Field" org-table-edit-field (org-at-table-p)]
14363 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
14366 ["Move Column Left" org-metaleft (org-at-table-p)]
14367 ["Move Column Right" org-metaright (org-at-table-p)]
14368 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
14369 ["Insert Column" org-shiftmetaright (org-at-table-p)])
14371 ["Move Row Up" org-metaup (org-at-table-p)]
14372 ["Move Row Down" org-metadown (org-at-table-p)]
14373 ["Delete Row" org-shiftmetaup (org-at-table-p)]
14374 ["Insert Row" org-shiftmetadown (org-at-table-p)]
14375 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
14377 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
14379 ["Copy Rectangle" org-copy-special (org-at-table-p)]
14380 ["Cut Rectangle" org-cut-special (org-at-table-p)]
14381 ["Paste Rectangle" org-paste-special (org-at-table-p)]
14382 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
14385 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
14386 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
14387 ["Edit Formulas" org-edit-special (org-at-table-p)]
14389 ["Recalculate line" org-table-recalculate (org-at-table-p)]
14390 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
14391 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
14393 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
14395 ["Sum Column/Rectangle" org-table-sum
14396 (or (org-at-table-p) (org-region-active-p))]
14397 ["Which Column?" org-table-current-column (org-at-table-p)])
14399 org-table-toggle-formula-debugger
14400 :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
14401 ["Show Col/Row Numbers"
14402 org-table-toggle-coordinate-overlays
14404 :selected (org-bound-and-true-p org-table-overlay-coordinates)]
14406 ["Create" org-table-create (and (not (org-at-table-p))
14407 org-enable-table-editor)]
14408 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
14409 ["Import from File" org-table-import (not (org-at-table-p))]
14410 ["Export to File" org-table-export (org-at-table-p)]
14412 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
14414 (easy-menu-define org-org-menu org-mode-map "Org menu"
14417 ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
14418 ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
14419 ["Sparse Tree..." org-sparse-tree t]
14420 ["Reveal Context" org-reveal t]
14421 ["Show All" show-all t]
14423 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
14425 ["New Heading" org-insert-heading t]
14426 ("Navigate Headings"
14427 ["Up" outline-up-heading t]
14428 ["Next" outline-next-visible-heading t]
14429 ["Previous" outline-previous-visible-heading t]
14430 ["Next Same Level" outline-forward-same-level t]
14431 ["Previous Same Level" outline-backward-same-level t]
14433 ["Jump" org-goto t])
14435 ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
14436 ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
14438 ["Copy Subtree" org-copy-special (not (org-at-table-p))]
14439 ["Cut Subtree" org-cut-special (not (org-at-table-p))]
14440 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
14442 ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
14444 ["Promote Heading" org-metaleft (not (org-at-table-p))]
14445 ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
14446 ["Demote Heading" org-metaright (not (org-at-table-p))]
14447 ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
14449 ["Sort Region/Children" org-sort (not (org-at-table-p))]
14451 ["Convert to odd levels" org-convert-to-odd-levels t]
14452 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
14454 ["Emphasis..." org-emphasize t]
14455 ["Edit Source Example" org-edit-special t]
14457 ["Footnote new/jump" org-footnote-action t]
14458 ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
14460 ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
14461 ; ["Check and Tag Children" (org-toggle-archive-tag (4))
14462 ; :active t :keys "C-u C-c C-x C-a"]
14463 ["Sparse trees open ARCHIVE trees"
14464 (setq org-sparse-tree-open-archived-trees
14465 (not org-sparse-tree-open-archived-trees))
14466 :style toggle :selected org-sparse-tree-open-archived-trees]
14467 ["Cycling opens ARCHIVE trees"
14468 (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
14469 :style toggle :selected org-cycle-open-archived-trees]
14471 ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
14472 ["Move Subtree to Archive" org-advertized-archive-subtree t]
14473 ; ["Check and Move Children" (org-archive-subtree '(4))
14474 ; :active t :keys "C-u C-c C-x C-s"]
14478 ["Store Link (Global)" org-store-link t]
14479 ["Insert Link" org-insert-link t]
14480 ["Follow Link" org-open-at-point t]
14482 ["Next link" org-next-link t]
14483 ["Previous link" org-previous-link t]
14485 ["Descriptive Links"
14486 (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
14488 :selected (member '(org-link) buffer-invisibility-spec)]
14491 (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
14493 :selected (not (member '(org-link) buffer-invisibility-spec))])
14496 ["TODO/DONE/-" org-todo t]
14498 ["Next keyword" org-shiftright (org-on-heading-p)]
14499 ["Previous keyword" org-shiftleft (org-on-heading-p)]
14500 ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
14501 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
14502 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
14503 ["Show TODO Tree" org-show-todo-tree t]
14504 ["Global TODO list" org-todo-list t]
14506 ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
14507 :selected org-enforce-todo-dependencies :style toggle :active t]
14508 "Settings for tree at point"
14509 ["Do Children sequentially" org-toggle-ordered-property :style radio
14510 :selected (ignore-errors (org-entry-get nil "ORDERED"))
14511 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
14512 ["Do Children parallel" org-toggle-ordered-property :style radio
14513 :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
14514 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
14516 ["Set Priority" org-priority t]
14517 ["Priority Up" org-shiftup t]
14518 ["Priority Down" org-shiftdown t]
14520 ["Get news from all feeds" org-feed-update-all t]
14521 ["Go to the inbox of a feed..." org-feed-goto-inbox t]
14522 ["Customize feeds" (customize-variable 'org-feed-alist) t])
14523 ("TAGS and Properties"
14524 ["Set Tags" org-set-tags-command t]
14525 ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
14527 ["Set property" org-set-property t]
14528 ["Column view of properties" org-columns t]
14529 ["Insert Column View DBlock" org-insert-columns-dblock t])
14530 ("Dates and Scheduling"
14531 ["Timestamp" org-time-stamp t]
14532 ["Timestamp (inactive)" org-time-stamp-inactive t]
14534 ["1 Day Later" org-shiftright t]
14535 ["1 Day Earlier" org-shiftleft t]
14536 ["1 ... Later" org-shiftup t]
14537 ["1 ... Earlier" org-shiftdown t])
14538 ["Compute Time Range" org-evaluate-time-range t]
14539 ["Schedule Item" org-schedule t]
14540 ["Deadline" org-deadline t]
14542 ["Custom time format" org-toggle-time-stamp-overlays
14543 :style radio :selected org-display-custom-times]
14545 ["Goto Calendar" org-goto-calendar t]
14546 ["Date from Calendar" org-date-from-calendar t]
14548 ["Start/Restart Timer" org-timer-start t]
14549 ["Pause/Continue Timer" org-timer-pause-or-continue t]
14550 ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
14551 ["Insert Timer String" org-timer t]
14552 ["Insert Timer Item" org-timer-item t])
14554 ["Clock in" org-clock-in t]
14555 ["Clock out" org-clock-out t]
14556 ["Clock cancel" org-clock-cancel t]
14557 ["Goto running clock" org-clock-goto t]
14558 ["Display times" org-clock-display t]
14559 ["Create clock table" org-clock-report t]
14561 ["Record DONE time"
14562 (progn (setq org-log-done (not org-log-done))
14563 (message "Switching to %s will %s record a timestamp"
14564 (car org-done-keywords)
14565 (if org-log-done "automatically" "not")))
14566 :style toggle :selected org-log-done])
14568 ["Agenda Command..." org-agenda t]
14569 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
14570 ("File List for Agenda")
14571 ("Special views current file"
14572 ["TODO Tree" org-show-todo-tree t]
14573 ["Check Deadlines" org-check-deadlines t]
14574 ["Timeline" org-timeline t]
14575 ["Tags/Property tree" org-match-sparse-tree t])
14577 ["Export/Publish..." org-export t]
14579 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
14580 :selected org-cdlatex-mode]
14581 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
14582 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
14583 ["Modify math symbol" org-cdlatex-math-modify
14584 (org-inside-LaTeX-fragment-p)]
14585 ["Insert citation" org-reftex-citation t]
14587 ["Export LaTeX fragments as images"
14588 (if (featurep 'org-exp)
14589 (setq org-export-with-LaTeX-fragments
14590 (not org-export-with-LaTeX-fragments))
14591 (require 'org-exp))
14592 :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
14593 org-export-with-LaTeX-fragments)])
14596 ["Show Version" org-version t]
14597 ["Info Documentation" org-info t])
14599 ["Browse Org Group" org-customize t]
14601 ["Expand This Menu" org-create-customize-menu
14602 (fboundp 'customize-menu-create)])
14605 ["Refresh setup current buffer" org-mode-restart t]
14606 ["Reload Org (after update)" org-reload t]
14607 ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
14610 (defun org-info (&optional node)
14611 "Read documentation for Org-mode in the info system.
14612 With optional NODE, go directly to that node."
14614 (info (format "(org)%s" (or node ""))))
14616 (defun org-install-agenda-files-menu ()
14617 (let ((bl (buffer-list)))
14620 (set-buffer (pop bl))
14621 (if (org-mode-p) (setq bl nil)))
14624 '("Org") "File List for Agenda"
14627 ["Edit File List" (org-edit-agenda-file-list) t]
14628 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
14629 ["Remove Current File from List" org-remove-file t]
14630 ["Cycle through agenda files" org-cycle-agenda-files t]
14631 ["Occur in all agenda files" org-occur-in-agenda-files t]
14633 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
14638 (defun org-require-autoloaded-modules ()
14641 '(org-agenda org-archive org-attach org-clock org-colview
14642 org-exp org-id org-export-latex org-docbook
14643 org-publish org-remember org-table org-timer)))
14646 (defun org-reload (&optional uncompiled)
14647 "Reload all org lisp files.
14648 With prefix arg UNCOMPILED, load the uncompiled versions."
14650 (require 'find-func)
14651 (let* ((dir (file-name-directory (find-library-name "org")))
14652 (files (directory-files dir t "\\.el\\'"))
14653 (remove-re (concat (if (featurep 'xemacs)
14654 "org-colview" "org-colview-xemacs")
14656 (setq files (mapcar 'file-name-sans-extension files))
14657 (setq files (mapcar
14658 (lambda (x) (if (string-match remove-re x) nil x))
14660 (setq files (delq nil files))
14663 (if (and (not uncompiled)
14664 (file-exists-p (concat f ".elc")))
14665 (load (concat f ".elc") nil nil t)
14666 (load (concat f ".el") nil nil t)))
14670 (defun org-customize ()
14671 "Call the customize function with org as argument."
14673 (org-load-modules-maybe)
14674 (org-require-autoloaded-modules)
14675 (customize-browse 'org))
14677 (defun org-create-customize-menu ()
14678 "Create a full customization menu for Org-mode, insert it into the menu."
14680 (org-load-modules-maybe)
14681 (org-require-autoloaded-modules)
14682 (if (fboundp 'customize-menu-create)
14685 '("Org") "Customize"
14686 `(["Browse Org group" org-customize t]
14688 ,(customize-menu-create 'org)
14689 ["Set" Custom-set t]
14690 ["Save" Custom-save t]
14691 ["Reset to Current" Custom-reset-current t]
14692 ["Reset to Saved" Custom-reset-saved t]
14693 ["Reset to Standard Settings" Custom-reset-standard t]))
14694 (message "\"Org\"-menu now contains full customization menu"))
14695 (error "Cannot expand menu (outdated version of cus-edit.el)")))
14697 ;;;; Miscellaneous stuff
14699 ;;; Generally useful functions
14701 (defun org-find-text-property-in-string (prop s)
14702 "Return the first non-nil value of property PROP in string S."
14703 (or (get-text-property 0 prop s)
14704 (get-text-property (or (next-single-property-change 0 prop s) 0)
14707 (defun org-display-warning (message) ;; Copied from Emacs-Muse
14708 "Display the given MESSAGE as a warning."
14709 (if (fboundp 'display-warning)
14710 (display-warning 'org message
14711 (if (featurep 'xemacs)
14714 (let ((buf (get-buffer-create "*Org warnings*")))
14715 (with-current-buffer buf
14716 (goto-char (point-max))
14717 (insert "Warning (Org): " message)
14720 (display-buffer buf)
14723 (defun org-goto-marker-or-bmk (marker &optional bookmark)
14724 "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
14725 (if (and marker (marker-buffer marker)
14726 (buffer-live-p (marker-buffer marker)))
14728 (switch-to-buffer (marker-buffer marker))
14729 (if (or (> marker (point-max)) (< marker (point-min)))
14732 (org-show-context 'org-goto))
14734 (bookmark-jump bookmark)
14735 (error "Cannot find location"))))
14737 (defun org-quote-csv-field (s)
14738 "Quote field for inclusion in CSV material."
14739 (if (string-match "[\",]" s)
14740 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
14743 (defun org-plist-delete (plist property)
14744 "Delete PROPERTY from PLIST.
14745 This is in contrast to merely setting it to 0."
14748 (if (not (eq property (car plist)))
14749 (setq p (plist-put p (car plist) (nth 1 plist))))
14750 (setq plist (cddr plist)))
14753 (defun org-force-self-insert (N)
14754 "Needed to enforce self-insert under remapping."
14756 (self-insert-command N))
14758 (defun org-string-width (s)
14759 "Compute width of string, ignoring invisible characters.
14760 This ignores character with invisibility property `org-link', and also
14761 characters with property `org-cwidth', because these will become invisible
14762 upon the next fontification round."
14764 (when (or (eq t buffer-invisibility-spec)
14765 (assq 'org-link buffer-invisibility-spec))
14766 (while (setq b (text-property-any 0 (length s)
14767 'invisible 'org-link s))
14768 (setq s (concat (substring s 0 b)
14769 (substring s (or (next-single-property-change
14770 b 'invisible s) (length s)))))))
14771 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
14772 (setq s (concat (substring s 0 b)
14773 (substring s (or (next-single-property-change
14774 b 'org-cwidth s) (length s))))))
14775 (setq l (string-width s) b -1)
14776 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
14777 (setq l (- l (get-text-property b 'org-dwidth-n s))))
14780 (defun org-get-indentation (&optional line)
14781 "Get the indentation of the current line, interpreting tabs.
14782 When LINE is given, assume it represents a line and compute its indentation."
14784 (if (string-match "^ *" (org-remove-tabs line))
14787 (beginning-of-line 1)
14788 (skip-chars-forward " \t")
14789 (current-column))))
14791 (defun org-remove-tabs (s &optional width)
14792 "Replace tabulators in S with spaces.
14793 Assumes that s is a single line, starting in column 0."
14794 (setq width (or width tab-width))
14795 (while (string-match "\t" s)
14796 (setq s (replace-match
14798 (- (* width (/ (+ (match-beginning 0) width) width))
14799 (match-beginning 0)) ?\ )
14803 (defun org-fix-indentation (line ind)
14804 "Fix indentation in LINE.
14805 IND is a cons cell with target and minimum indentation.
14806 If the current indentation in LINE is smaller than the minimum,
14807 leave it alone. If it is larger than ind, set it to the target."
14808 (let* ((l (org-remove-tabs line))
14809 (i (org-get-indentation l))
14810 (i1 (car ind)) (i2 (cdr ind)))
14811 (if (>= i i2) (setq l (substring line i2)))
14813 (concat (make-string i1 ?\ ) l)
14816 (defun org-base-buffer (buffer)
14817 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
14820 (or (buffer-base-buffer buffer)
14823 (defun org-trim (s)
14824 "Remove whitespace at beginning and end of string."
14825 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
14826 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
14829 (defun org-wrap (string &optional width lines)
14830 "Wrap string to either a number of lines, or a width in characters.
14831 If WIDTH is non-nil, the string is wrapped to that width, however many lines
14832 that costs. If there is a word longer than WIDTH, the text is actually
14833 wrapped to the length of that word.
14834 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
14835 many lines, whatever width that takes.
14836 The return value is a list of lines, without newlines at the end."
14837 (let* ((words (org-split-string string "[ \t\n]+"))
14838 (maxword (apply 'max (mapcar 'org-string-width words)))
14841 (org-do-wrap words (max maxword width)))
14844 (setq ll (org-do-wrap words maxword))
14845 (if (<= (length ll) lines)
14848 (while (> (length ll) lines)
14850 (setq ll (org-do-wrap words w)))
14852 (t (error "Cannot wrap this")))))
14854 (defun org-do-wrap (words width)
14855 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
14858 (setq line (pop words))
14859 (while (and words (< (+ (length line) (length (car words))) width))
14860 (setq line (concat line " " (pop words))))
14861 (setq lines (push line lines)))
14864 (defun org-split-string (string &optional separators)
14865 "Splits STRING into substrings at SEPARATORS.
14866 No empty strings are returned if there are matches at the beginning
14867 and end of string."
14868 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
14872 (while (and (string-match rexp string
14874 (= start (match-beginning 0))
14875 (< start (length string)))
14877 (< (match-beginning 0) (length string)))
14879 (or (eq (match-beginning 0) 0)
14880 (and (eq (match-beginning 0) (match-end 0))
14881 (eq (match-beginning 0) start))
14883 (cons (substring string start (match-beginning 0))
14885 (setq start (match-end 0)))
14886 (or (eq start (length string))
14888 (cons (substring string start)
14892 (defun org-uuidgen-p (s)
14893 "Is S an ID created by UUIDGEN?"
14894 (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
14896 (defun org-context ()
14897 "Return a list of contexts of the current cursor position.
14898 If several contexts apply, all are returned.
14899 Each context entry is a list with a symbol naming the context, and
14900 two positions indicating start and end of the context. Possible
14903 :headline anywhere in a headline
14904 :headline-stars on the leading stars in a headline
14905 :todo-keyword on a TODO keyword (including DONE) in a headline
14906 :tags on the TAGS in a headline
14907 :priority on the priority cookie in a headline
14908 :item on the first line of a plain list item
14909 :item-bullet on the bullet/number of a plain list item
14910 :checkbox on the checkbox in a plain list item
14911 :table in an org-mode table
14912 :table-special on a special filed in a table
14913 :table-table in a table.el table
14914 :link on a hyperlink
14915 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
14916 :target on a <<target>>
14917 :radio-target on a <<<radio-target>>>
14918 :latex-fragment on a LaTeX fragment
14919 :latex-preview on a LaTeX fragment with overlayed preview image
14921 This function expects the position to be visible because it uses font-lock
14922 faces as a help to recognize the following contexts: :table-special, :link,
14924 (let* ((f (get-text-property (point) 'face))
14925 (faces (if (listp f) f (list f)))
14926 (p (point)) clist o)
14927 ;; First the large context
14929 ((org-on-heading-p t)
14930 (push (list :headline (point-at-bol) (point-at-eol)) clist)
14932 (beginning-of-line 1)
14933 (looking-at org-todo-line-tags-regexp))
14934 (push (org-point-in-group p 1 :headline-stars) clist)
14935 (push (org-point-in-group p 2 :todo-keyword) clist)
14936 (push (org-point-in-group p 4 :tags) clist))
14938 (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
14939 (if (looking-at "\\[#[A-Z0-9]\\]")
14940 (push (org-point-in-group p 0 :priority) clist)))
14943 (push (org-point-in-group p 2 :item-bullet) clist)
14944 (push (list :item (point-at-bol)
14945 (save-excursion (org-end-of-item) (point)))
14947 (and (org-at-item-checkbox-p)
14948 (push (org-point-in-group p 0 :checkbox) clist)))
14951 (push (list :table (org-table-begin) (org-table-end)) clist)
14952 (if (memq 'org-formula faces)
14953 (push (list :table-special
14954 (previous-single-property-change p 'face)
14955 (next-single-property-change p 'face)) clist)))
14956 ((org-at-table-p 'any)
14957 (push (list :table-table) clist)))
14960 ;; Now the small context
14962 ((org-at-timestamp-p)
14963 (push (org-point-in-group p 0 :timestamp) clist))
14964 ((memq 'org-link faces)
14966 (previous-single-property-change p 'face)
14967 (next-single-property-change p 'face)) clist))
14968 ((memq 'org-special-keyword faces)
14969 (push (list :keyword
14970 (previous-single-property-change p 'face)
14971 (next-single-property-change p 'face)) clist))
14973 (push (org-point-in-group p 0 :target) clist)
14974 (goto-char (1- (match-beginning 0)))
14975 (if (looking-at org-radio-target-regexp)
14976 (push (org-point-in-group p 0 :radio-target) clist))
14978 ((setq o (car (delq nil
14981 (if (memq x org-latex-fragment-image-overlays) x))
14982 (org-overlays-at (point))))))
14983 (push (list :latex-fragment
14984 (org-overlay-start o) (org-overlay-end o)) clist)
14985 (push (list :latex-preview
14986 (org-overlay-start o) (org-overlay-end o)) clist))
14987 ((org-inside-LaTeX-fragment-p)
14988 ;; FIXME: positions wrong.
14989 (push (list :latex-fragment (point) (point)) clist)))
14991 (setq clist (nreverse (delq nil clist)))
14994 ;; FIXME: Compare with at-regexp-p Do we need both?
14995 (defun org-in-regexp (re &optional nlines visually)
14996 "Check if point is inside a match of regexp.
14997 Normally only the current line is checked, but you can include NLINES extra
14998 lines both before and after point into the search.
14999 If VISUALLY is set, require that the cursor is not after the match but
15000 really on, so that the block visually is on the match."
15002 (let ((pos (point))
15003 (eol (point-at-eol (+ 1 (or nlines 0))))
15004 (inc (if visually 1 0)))
15006 (beginning-of-line (- 1 (or nlines 0)))
15007 (while (re-search-forward re eol t)
15008 (if (and (<= (match-beginning 0) pos)
15009 (>= (+ inc (match-end 0)) pos))
15010 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
15012 (defun org-at-regexp-p (regexp)
15013 "Is point inside a match of REGEXP in the current line?"
15016 (let ((pos (point)) (end (point-at-eol)))
15017 (beginning-of-line 1)
15018 (while (re-search-forward regexp end t)
15019 (if (and (<= (match-beginning 0) pos)
15020 (>= (match-end 0) pos))
15024 (defun org-occur-in-agenda-files (regexp &optional nlines)
15025 "Call `multi-occur' with buffers for all agenda files."
15026 (interactive "sOrg-files matching: \np")
15027 (let* ((files (org-agenda-files))
15028 (tnames (mapcar 'file-truename files))
15029 (extra org-agenda-text-search-extra-files)
15031 (when (eq (car extra) 'agenda-archives)
15032 (setq extra (cdr extra))
15033 (setq files (org-add-archive-files files)))
15034 (while (setq f (pop extra))
15035 (unless (member (file-truename f) tnames)
15036 (add-to-list 'files f 'append)
15037 (add-to-list 'tnames (file-truename f) 'append)))
15039 (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
15042 (if (boundp 'occur-mode-find-occurrence-hook)
15044 (add-hook 'occur-mode-find-occurrence-hook
15049 (defadvice occur-mode-goto-occurrence
15050 (after org-occur-reveal activate)
15051 (and (org-mode-p) (org-reveal)))
15052 (defadvice occur-mode-goto-occurrence-other-window
15053 (after org-occur-reveal activate)
15054 (and (org-mode-p) (org-reveal)))
15055 (defadvice occur-mode-display-occurrence
15056 (after org-occur-reveal activate)
15058 (let ((pos (occur-mode-find-occurrence)))
15059 (with-current-buffer (marker-buffer pos)
15064 (defun org-uniquify (list)
15065 "Remove duplicate elements from LIST."
15067 (mapc (lambda (x) (add-to-list 'res x 'append)) list)
15070 (defun org-delete-all (elts list)
15071 "Remove all elements in ELTS from LIST."
15073 (setq list (delete (pop elts) list)))
15076 (defun org-back-over-empty-lines ()
15077 "Move backwards over whitespace, to the beginning of the first empty line.
15078 Returns the number of empty lines passed."
15079 (let ((pos (point)))
15080 (skip-chars-backward " \t\n\r")
15081 (beginning-of-line 2)
15082 (goto-char (min (point) pos))
15083 (count-lines (point) pos)))
15085 (defun org-skip-whitespace ()
15086 (skip-chars-forward " \t\n\r"))
15088 (defun org-point-in-group (point group &optional context)
15089 "Check if POINT is in match-group GROUP.
15090 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
15091 match. If the match group does ot exist or point is not inside it,
15093 (and (match-beginning group)
15094 (>= point (match-beginning group))
15095 (<= point (match-end group))
15097 (list context (match-beginning group) (match-end group))
15100 (defun org-switch-to-buffer-other-window (&rest args)
15101 "Switch to buffer in a second window on the current frame.
15102 In particular, do not allow pop-up frames."
15103 (let (pop-up-frames special-display-buffer-names special-display-regexps
15104 special-display-function)
15105 (apply 'switch-to-buffer-other-window args)))
15107 (defun org-combine-plists (&rest plists)
15108 "Create a single property list from all plists in PLISTS.
15109 The process starts by copying the first list, and then setting properties
15110 from the other lists. Settings in the last list are the most significant
15111 ones and overrule settings in the other lists."
15112 (let ((rtn (copy-sequence (pop plists)))
15115 (setq ls (pop plists))
15117 (setq p (pop ls) v (pop ls))
15118 (setq rtn (plist-put rtn p v))))
15121 (defun org-move-line-down (arg)
15122 "Move the current line down. With prefix argument, move it past ARG lines."
15124 (let ((col (current-column))
15126 (beginning-of-line 1) (setq beg (point))
15127 (beginning-of-line 2) (setq end (point))
15128 (beginning-of-line (+ 1 arg))
15129 (setq pos (move-marker (make-marker) (point)))
15130 (insert (delete-and-extract-region beg end))
15132 (org-move-to-column col)))
15134 (defun org-move-line-up (arg)
15135 "Move the current line up. With prefix argument, move it past ARG lines."
15137 (let ((col (current-column))
15139 (beginning-of-line 1) (setq beg (point))
15140 (beginning-of-line 2) (setq end (point))
15141 (beginning-of-line (- arg))
15142 (setq pos (move-marker (make-marker) (point)))
15143 (insert (delete-and-extract-region beg end))
15145 (org-move-to-column col)))
15147 (defun org-replace-escapes (string table)
15148 "Replace %-escapes in STRING with values in TABLE.
15149 TABLE is an association list with keys like \"%a\" and string values.
15150 The sequences in STRING may contain normal field width and padding information,
15151 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
15152 so values can contain further %-escapes if they are define later in TABLE."
15153 (let ((case-fold-search nil)
15155 (while (setq e (pop table))
15156 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
15157 (while (string-match re string)
15158 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
15160 (setq string (replace-match rpl t t string))))
15164 (defun org-sublist (list start end)
15165 "Return a section of LIST, from START to END.
15166 Counting starts at 1."
15167 (let (rtn (c start))
15168 (setq list (nthcdr (1- start) list))
15169 (while (and list (<= c end))
15170 (push (pop list) rtn)
15174 (defun org-find-base-buffer-visiting (file)
15175 "Like `find-buffer-visiting' but alway return the base buffer and
15176 not an indirect buffer."
15177 (let ((buf (find-buffer-visiting file)))
15179 (or (buffer-base-buffer buf) buf)
15182 (defun org-image-file-name-regexp (&optional extensions)
15183 "Return regexp matching the file names of images.
15184 If EXTENSIONS is given, only match these."
15185 (if (and (not extensions) (fboundp 'image-file-name-regexp))
15186 (image-file-name-regexp)
15187 (let ((image-file-name-extensions
15189 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
15190 "xbm" "xpm" "pbm" "pgm" "ppm"))))
15192 (regexp-opt (nconc (mapcar 'upcase
15193 image-file-name-extensions)
15194 image-file-name-extensions)
15198 (defun org-file-image-p (file &optional extensions)
15199 "Return non-nil if FILE is an image."
15201 (string-match (org-image-file-name-regexp extensions) file)))
15203 (defun org-get-cursor-date ()
15204 "Return the date at cursor in as a time.
15205 This works in the calendar and in the agenda, anywhere else it just
15206 returns the current time."
15207 (let (date day defd)
15209 ((eq major-mode 'calendar-mode)
15210 (setq date (calendar-cursor-to-date)
15211 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
15212 ((eq major-mode 'org-agenda-mode)
15213 (setq day (get-text-property (point) 'day))
15215 (setq date (calendar-gregorian-from-absolute day)
15216 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
15218 (or defd (current-time))))
15220 (defvar org-agenda-action-marker (make-marker)
15221 "Marker pointing to the entry for the next agenda action.")
15223 (defun org-mark-entry-for-agenda-action ()
15224 "Mark the current entry as target of an agenda action.
15225 Agenda actions are actions executed from the agenda with the key `k',
15226 which make use of the date at the cursor."
15228 (move-marker org-agenda-action-marker
15229 (save-excursion (org-back-to-heading t) (point))
15232 "Entry marked for action; press `k' at desired date in agenda or calendar"))
15234 ;;; Paragraph filling stuff.
15235 ;; We want this to be just right, so use the full arsenal.
15237 (defun org-indent-line-function ()
15238 "Indent line like previous, but further if previous was headline or item."
15240 (let* ((pos (point))
15241 (itemp (org-at-item-p))
15242 (org-drawer-regexp (or org-drawer-regexp "\000"))
15243 column bpos bcol tpos tcol bullet btype bullet-type)
15244 ;; Find the previous relevant line
15245 (beginning-of-line 1)
15247 ((looking-at "#") (setq column 0))
15248 ((looking-at "\\*+ ") (setq column 0))
15249 ((and (looking-at "[ \t]*:END:")
15250 (save-excursion (re-search-backward org-drawer-regexp nil t)))
15252 (goto-char (1- (match-beginning 1)))
15253 (setq column (current-column))))
15255 (beginning-of-line 0)
15256 (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
15257 (not (looking-at "[ \t]*:END:"))
15258 (not (looking-at org-drawer-regexp)))
15259 (beginning-of-line 0))
15261 ((looking-at "\\*+[ \t]+")
15262 (if (not org-adapt-indentation)
15264 (goto-char (match-end 0))
15265 (setq column (current-column))))
15266 ((looking-at org-drawer-regexp)
15267 (goto-char (1- (match-beginning 1)))
15268 (setq column (current-column)))
15269 ((looking-at "\\([ \t]*\\):END:")
15270 (goto-char (match-end 1))
15271 (setq column (current-column)))
15273 (org-beginning-of-item)
15274 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
15275 (setq bpos (match-beginning 1) tpos (match-end 0)
15276 bcol (progn (goto-char bpos) (current-column))
15277 tcol (progn (goto-char tpos) (current-column))
15278 bullet (match-string 1)
15279 bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
15280 (if (> tcol (+ bcol org-description-max-indent))
15281 (setq tcol (+ bcol 5)))
15285 (beginning-of-line 1)
15286 (if (looking-at "\\S-")
15288 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
15289 (setq bullet (match-string 1)
15290 btype (if (string-match "[0-9]" bullet) "n" bullet))
15291 (setq column (if (equal btype bullet-type) bcol tcol)))
15292 (setq column (org-get-indentation)))))
15293 (t (setq column (org-get-indentation))))))
15295 (if (<= (current-column) (current-indentation))
15296 (org-indent-line-to column)
15297 (save-excursion (org-indent-line-to column)))
15298 (setq column (current-column))
15299 (beginning-of-line 1)
15301 "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
15302 (replace-match (concat "\\1" (format org-property-format
15303 (match-string 2) (match-string 3)))
15305 (org-move-to-column column)))
15307 (defun org-set-autofill-regexps ()
15309 ;; In the paragraph separator we include headlines, because filling
15310 ;; text in a line directly attached to a headline would otherwise
15311 ;; fill the headline as well.
15312 (org-set-local 'comment-start-skip "^#+[ \t]*")
15313 (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
15314 ;; The paragraph starter includes hand-formatted lists.
15315 (org-set-local 'paragraph-start
15316 "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
15317 ;; Inhibit auto-fill for headers, tables and fixed-width lines.
15318 ;; But only if the user has not turned off tables or fixed-width regions
15320 'auto-fill-inhibit-regexp
15321 (concat "\\*+ \\|#\\+"
15322 "\\|[ \t]*" org-keyword-time-regexp
15323 (if (or org-enable-table-editor org-enable-fixed-width-editor)
15326 (if org-enable-table-editor "|" "")
15327 (if org-enable-fixed-width-editor ":" "")
15329 ;; We use our own fill-paragraph function, to make sure that tables
15330 ;; and fixed-width regions are not wrapped. That function will pass
15331 ;; through to `fill-paragraph' when appropriate.
15332 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
15333 ; Adaptive filling: To get full control, first make sure that
15334 ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
15335 (org-set-local 'adaptive-fill-regexp "\000")
15336 (org-set-local 'adaptive-fill-function
15337 'org-adaptive-fill-function)
15339 'align-mode-rules-list
15340 '((org-in-buffer-settings
15341 (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
15342 (modes . '(org-mode))))))
15344 (defun org-fill-paragraph (&optional justify)
15345 "Re-align a table, pass through to fill-paragraph if no table."
15346 (let ((table-p (org-at-table-p))
15347 (table.el-p (org-at-table.el-p)))
15348 (cond ((and (equal (char-after (point-at-bol)) ?*)
15349 (save-excursion (goto-char (point-at-bol))
15350 (looking-at outline-regexp)))
15351 t) ; skip headlines
15352 (table.el-p t) ; skip table.el tables
15353 (table-p (org-table-align) t) ; align org-mode tables
15354 (t nil)))) ; call paragraph-fill
15356 ;; For reference, this is the default value of adaptive-fill-regexp
15357 ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
15359 (defun org-adaptive-fill-function ()
15360 "Return a fill prefix for org-mode files.
15361 In particular, this makes sure hanging paragraphs for hand-formatted lists
15363 (cond ((looking-at "#[ \t]+")
15365 ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
15367 (if (> (match-end 1) (+ (match-beginning 1)
15368 org-description-max-indent))
15369 (goto-char (+ (match-beginning 1) 5))
15370 (goto-char (match-end 0)))
15371 (make-string (current-column) ?\ )))
15372 ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
15374 (goto-char (match-end 0))
15375 (make-string (current-column) ?\ )))
15380 (defun org-toggle-fixed-width-section (arg)
15381 "Toggle the fixed-width export.
15382 If there is no active region, the QUOTE keyword at the current headline is
15383 inserted or removed. When present, it causes the text between this headline
15384 and the next to be exported as fixed-width text, and unmodified.
15385 If there is an active region, this command adds or removes a colon as the
15386 first character of this line. If the first character of a line is a colon,
15387 this line is also exported in fixed-width font."
15390 (regionp (org-region-active-p))
15391 (beg (if regionp (region-beginning) (point)))
15392 (end (if regionp (region-end)))
15393 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
15394 (case-fold-search nil)
15395 (re "[ \t]*\\(: \\)")
15400 (setq cc (current-column))
15401 (beginning-of-line 1)
15402 (setq off (looking-at re))
15403 (while (> nlines 0)
15404 (setq nlines (1- nlines))
15405 (beginning-of-line 1)
15408 (org-move-to-column cc t)
15411 ((and off (looking-at re))
15412 (replace-match "" t t nil 1))
15413 ((not off) (org-move-to-column cc t) (insert ": ")))
15416 (org-back-to-heading)
15417 (if (looking-at (concat outline-regexp
15418 "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
15419 (replace-match "" t t nil 1)
15420 (if (looking-at outline-regexp)
15422 (goto-char (match-end 0))
15423 (insert org-quote-string " "))))))))
15425 (defun org-reftex-citation ()
15426 "Use reftex-citation to insert a citation into the buffer.
15427 This looks for a line like
15429 #+BIBLIOGRAPHY: foo plain option:-d
15431 and derives from it that foo.bib is the bbliography file relevant
15432 for this document. It then installs the necessary environment for RefTeX
15433 to work in this buffer and calls `reftex-citation' to insert a citation
15436 Export of such citations to both LaTeX and HTML is handled by the contributed
15437 package org-exp-bibtex by Taru Karttunen."
15439 (let ((reftex-docstruct-symbol 'rds)
15440 (reftex-cite-format "\\cite{%l}")
15445 (let ((case-fold-search t)
15446 (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
15447 (if (not (save-excursion
15448 (or (re-search-forward re nil t)
15449 (re-search-backward re nil t))))
15450 (error "No bibliography defined in file")
15451 (setq bib (concat (match-string 1) ".bib")
15452 rds (list (list 'bib bib)))))))
15453 (call-interactively 'reftex-citation)))
15455 ;;;; Functions extending outline functionality
15457 (defun org-beginning-of-line (&optional arg)
15458 "Go to the beginning of the current line. If that is invisible, continue
15459 to a visible line beginning. This makes the function of C-a more intuitive.
15460 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
15461 first attempt, and only move to after the tags when the cursor is already
15462 beyond the end of the headline."
15464 (let ((pos (point))
15465 (special (if (consp org-special-ctrl-a/e)
15466 (car org-special-ctrl-a/e)
15467 org-special-ctrl-a/e))
15469 (beginning-of-line 1)
15470 (if (and arg (fboundp 'move-beginning-of-line))
15471 (call-interactively 'move-beginning-of-line)
15475 (if (org-invisible-p)
15476 (while (and (not (bobp)) (org-invisible-p))
15478 (beginning-of-line 1))
15479 (forward-char 1))))
15482 ((and (looking-at org-complex-heading-regexp)
15483 (= (char-after (match-end 1)) ?\ ))
15484 (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
15488 (cond ((> pos refpos) refpos)
15489 ((= pos (point)) refpos)
15491 (cond ((> pos (point)) (point))
15492 ((not (eq last-command this-command)) (point))
15497 (cond ((> pos (match-end 4)) (match-end 4))
15498 ((= pos (point)) (match-end 4))
15500 (cond ((> pos (point)) (point))
15501 ((not (eq last-command this-command)) (point))
15502 (t (match-end 4))))))))
15504 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
15506 (defun org-end-of-line (&optional arg)
15507 "Go to the end of the line.
15508 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
15509 first attempt, and only move to after the tags when the cursor is already
15510 beyond the end of the headline."
15512 (let ((special (if (consp org-special-ctrl-a/e)
15513 (cdr org-special-ctrl-a/e)
15514 org-special-ctrl-a/e)))
15515 (if (or (not special)
15516 (not (org-on-heading-p))
15518 (call-interactively (if (fboundp 'move-end-of-line)
15521 (let ((pos (point)))
15522 (beginning-of-line 1)
15523 (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
15525 (if (or (< pos (match-beginning 1))
15526 (= pos (match-end 0)))
15527 (goto-char (match-beginning 1))
15528 (goto-char (match-end 0)))
15529 (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
15530 (goto-char (match-end 0))
15531 (goto-char (match-beginning 1))))
15532 (call-interactively (if (fboundp 'move-end-of-line)
15536 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
15538 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
15539 (define-key org-mode-map "\C-e" 'org-end-of-line)
15541 (defun org-backward-sentence (&optional arg)
15542 "Go to beginning of sentence, or beginning of table field.
15543 This will call `backward-sentence' or `org-table-beginning-of-field',
15544 depending on context."
15547 ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
15548 (t (call-interactively 'backward-sentence))))
15550 (defun org-forward-sentence (&optional arg)
15551 "Go to end of sentence, or end of table field.
15552 This will call `forward-sentence' or `org-table-end-of-field',
15553 depending on context."
15556 ((org-at-table-p) (call-interactively 'org-table-end-of-field))
15557 (t (call-interactively 'forward-sentence))))
15559 (define-key org-mode-map "\M-a" 'org-backward-sentence)
15560 (define-key org-mode-map "\M-e" 'org-forward-sentence)
15562 (defun org-kill-line (&optional arg)
15563 "Kill line, to tags or end of line."
15566 ((or (not org-special-ctrl-k)
15568 (not (org-on-heading-p)))
15569 (call-interactively 'kill-line))
15570 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
15571 (kill-region (point) (match-beginning 1))
15572 (org-set-tags nil t))
15573 (t (kill-region (point) (point-at-eol)))))
15575 (define-key org-mode-map "\C-k" 'org-kill-line)
15577 (defun org-yank (&optional arg)
15578 "Yank. If the kill is a subtree, treat it specially.
15579 This command will look at the current kill and check if is a single
15580 subtree, or a series of subtrees[1]. If it passes the test, and if the
15581 cursor is at the beginning of a line or after the stars of a currently
15582 empty headline, then the yank is handled specially. How exactly depends
15583 on the value of the following variables, both set by default.
15585 org-yank-folded-subtrees
15586 When set, the subtree(s) will be folded after insertion, but only
15587 if doing so would now swallow text after the yanked text.
15589 org-yank-adjusted-subtrees
15590 When set, the subtree will be promoted or demoted in order to
15591 fit into the local outline tree structure, which means that the level
15592 will be adjusted so that it becomes the smaller one of the two
15593 *visible* surrounding headings.
15595 Any prefix to this command will cause `yank' to be called directly with
15596 no special treatment. In particular, a simple `C-u' prefix will just
15597 plainly yank the text as it is.
15599 \[1] Basically, the test checks if the first non-white line is a heading
15600 and if there are no other headings with fewer stars."
15602 (setq this-command 'yank)
15604 (call-interactively 'yank)
15605 (let ((subtreep ; is kill a subtree, and the yank position appropriate?
15606 (and (org-kill-is-subtree-p)
15608 (and (looking-at "[ \t]*$")
15611 (buffer-substring (point-at-bol) (point)))))))
15614 ((and subtreep org-yank-folded-subtrees)
15615 (let ((beg (point))
15617 (if (and subtreep org-yank-adjusted-subtrees)
15618 (org-paste-subtree nil nil 'for-yank)
15619 (call-interactively 'yank))
15622 (when (and (bolp) subtreep
15623 (not (setq swallowp
15624 (org-yank-folding-would-swallow-text beg end))))
15625 (or (looking-at outline-regexp)
15626 (re-search-forward (concat "^" outline-regexp) end t))
15627 (while (and (< (point) end) (looking-at outline-regexp))
15629 (org-cycle-show-empty-lines 'folded)
15630 (condition-case nil
15631 (outline-forward-same-level 1)
15632 (error (goto-char end)))))
15635 "Yanked text not folded because that would swallow text"))
15637 (skip-chars-forward " \t\n\r")
15638 (beginning-of-line 1)
15639 (push-mark beg 'nomsg)))
15640 ((and subtreep org-yank-adjusted-subtrees)
15641 (let ((beg (point-at-bol)))
15642 (org-paste-subtree nil nil 'for-yank)
15643 (push-mark beg 'nomsg)))
15645 (call-interactively 'yank))))))
15647 (defun org-yank-folding-would-swallow-text (beg end)
15648 "Would hide-subtree at BEG swallow any text after END?"
15652 (when (or (looking-at outline-regexp)
15653 (re-search-forward (concat "^" outline-regexp) end t))
15654 (setq level (org-outline-level)))
15656 (skip-chars-forward " \t\r\n\v\f")
15658 (and (bolp) (looking-at org-outline-regexp)
15659 (<= (org-outline-level) level)))
15660 nil ; Nothing would be swallowed
15661 t)))) ; something would swallow
15663 (define-key org-mode-map "\C-y" 'org-yank)
15665 (defun org-invisible-p ()
15666 "Check if point is at a character currently not visible."
15667 ;; Early versions of noutline don't have `outline-invisible-p'.
15668 (if (fboundp 'outline-invisible-p)
15669 (outline-invisible-p)
15670 (get-char-property (point) 'invisible)))
15672 (defun org-invisible-p2 ()
15673 "Check if point is at a character currently not visible."
15675 (if (and (eolp) (not (bobp))) (backward-char 1))
15676 ;; Early versions of noutline don't have `outline-invisible-p'.
15677 (if (fboundp 'outline-invisible-p)
15678 (outline-invisible-p)
15679 (get-char-property (point) 'invisible))))
15681 (defun org-back-to-heading (&optional invisible-ok)
15682 "Call `outline-back-to-heading', but provide a better error message."
15683 (condition-case nil
15684 (outline-back-to-heading invisible-ok)
15685 (error (error "Before first headline at position %d in buffer %s"
15686 (point) (current-buffer)))))
15688 (defun org-before-first-heading-p ()
15689 "Before first heading?"
15691 (null (re-search-backward "^\\*+ " nil t))))
15693 (defalias 'org-on-heading-p 'outline-on-heading-p)
15694 (defalias 'org-at-heading-p 'outline-on-heading-p)
15695 (defun org-at-heading-or-item-p ()
15696 (or (org-on-heading-p) (org-at-item-p)))
15698 (defun org-on-target-p ()
15699 (or (org-in-regexp org-radio-target-regexp)
15700 (org-in-regexp org-target-regexp)))
15702 (defun org-up-heading-all (arg)
15703 "Move to the heading line of which the present line is a subheading.
15704 This function considers both visible and invisible heading lines.
15705 With argument, move up ARG levels."
15706 (if (fboundp 'outline-up-heading-all)
15707 (outline-up-heading-all arg) ; emacs 21 version of outline.el
15708 (outline-up-heading arg t))) ; emacs 22 version of outline.el
15710 (defun org-up-heading-safe ()
15711 "Move to the heading line of which the present line is a subheading.
15712 This version will not throw an error. It will return the level of the
15713 headline found, or nil if no higher level is found."
15714 (let (start-level re)
15715 (org-back-to-heading t)
15716 (setq start-level (funcall outline-level))
15717 (if (equal start-level 1)
15719 (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
15720 (if (re-search-backward re nil t)
15721 (funcall outline-level)))))
15723 (defun org-first-sibling-p ()
15724 "Is this heading the first child of its parents?"
15726 (let ((re (concat "^" outline-regexp))
15728 (unless (org-at-heading-p t)
15729 (error "Not at a heading"))
15730 (setq level (funcall outline-level))
15732 (if (not (re-search-backward re nil t))
15734 (setq l (funcall outline-level))
15737 (defun org-goto-sibling (&optional previous)
15738 "Goto the next sibling, even if it is invisible.
15739 When PREVIOUS is set, go to the previous sibling instead. Returns t
15740 when a sibling was found. When none is found, return nil and don't
15742 (let ((fun (if previous 're-search-backward 're-search-forward))
15744 (re (concat "^" outline-regexp))
15746 (when (condition-case nil (org-back-to-heading t) (error nil))
15747 (setq level (funcall outline-level))
15749 (or previous (forward-char 1))
15750 (while (funcall fun re nil t)
15751 (setq l (funcall outline-level))
15752 (when (< l level) (goto-char pos) (throw 'exit nil))
15753 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
15757 (defun org-show-siblings ()
15758 "Show all siblings of the current headline."
15760 (while (org-goto-sibling) (org-flag-heading nil)))
15762 (while (org-goto-sibling 'previous)
15763 (org-flag-heading nil))))
15765 (defun org-show-hidden-entry ()
15766 "Show an entry where even the heading is hidden."
15770 (defun org-flag-heading (flag &optional entry)
15771 "Flag the current heading. FLAG non-nil means make invisible.
15772 When ENTRY is non-nil, show the entire entry."
15774 (org-back-to-heading t)
15775 ;; Check if we should show the entire entry
15780 (and (outline-next-heading)
15781 (org-flag-heading nil))))
15782 (outline-flag-region (max (point-min) (1- (point)))
15783 (save-excursion (outline-end-of-heading) (point))
15786 (defun org-forward-same-level (arg)
15787 "Move forward to the ARG'th subheading at same level as this one.
15788 Stop at the first and last subheadings of a superior heading.
15789 This is like outline-forward-same-level, but invisible headings are ok."
15791 (org-back-to-heading t)
15793 (let ((point-to-move-to (save-excursion
15794 (org-get-next-sibling))))
15795 (if point-to-move-to
15797 (goto-char point-to-move-to)
15798 (setq arg (1- arg)))
15801 (error "No following same-level heading"))))))
15803 (defun org-get-next-sibling ()
15804 "Move to next heading of the same level, and return point.
15805 If there is no such heading, return nil.
15806 This is like outline-next-sibling, but invisible headings are ok."
15807 (let ((level (funcall outline-level)))
15808 (outline-next-heading)
15809 (while (and (not (eobp)) (> (funcall outline-level) level))
15810 (outline-next-heading))
15811 (if (or (eobp) (< (funcall outline-level) level))
15815 (defun org-end-of-subtree (&optional invisible-OK to-heading)
15816 ;; This is an exact copy of the original function, but it uses
15817 ;; `org-back-to-heading', to make it work also in invisible
15818 ;; trees. And is uses an invisible-OK argument.
15819 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
15820 (org-back-to-heading invisible-OK)
15822 (level (funcall outline-level)))
15823 (while (and (not (eobp))
15824 (or first (> (funcall outline-level) level)))
15826 (outline-next-heading))
15828 (if (memq (preceding-char) '(?\n ?\^M))
15830 ;; Go to end of line before heading
15832 (if (memq (preceding-char) '(?\n ?\^M))
15833 ;; leave blank line before heading
15834 (forward-char -1))))))
15837 (defun org-show-subtree ()
15838 "Show everything after this heading at deeper levels."
15839 (outline-flag-region
15842 (outline-end-of-subtree) (outline-next-heading) (point))
15845 (defun org-show-entry ()
15846 "Show the body directly following this heading.
15847 Show the heading too, if it is currently invisible."
15850 (condition-case nil
15852 (org-back-to-heading t)
15853 (outline-flag-region
15854 (max (point-min) (1- (point)))
15856 (if (re-search-forward
15857 (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
15858 (match-beginning 1)
15863 (defun org-make-options-regexp (kwds &optional extra)
15864 "Make a regular expression for keyword lines."
15868 (mapconcat 'regexp-quote kwds "\\|")
15869 (if extra (concat "\\|" extra))
15873 ;; Make isearch reveal the necessary context
15874 (defun org-isearch-end ()
15875 "Reveal context after isearch exits."
15876 (when isearch-success ; only if search was successful
15877 (if (featurep 'xemacs)
15878 ;; Under XEmacs, the hook is run in the correct place,
15879 ;; we directly show the context.
15880 (org-show-context 'isearch)
15881 ;; In Emacs the hook runs *before* restoring the overlays.
15882 ;; So we have to use a one-time post-command-hook to do this.
15883 ;; (Emacs 22 has a special variable, see function `org-mode')
15884 (unless (and (boundp 'isearch-mode-end-hook-quit)
15885 isearch-mode-end-hook-quit)
15886 ;; Only when the isearch was not quitted.
15887 (org-add-hook 'post-command-hook 'org-isearch-post-command
15888 'append 'local)))))
15890 (defun org-isearch-post-command ()
15891 "Remove self from hook, and show context."
15892 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
15893 (org-show-context 'isearch))
15896 ;;;; Integration with and fixes for other packages
15900 (defvar org-imenu-markers nil
15901 "All markers currently used by Imenu.")
15902 (make-variable-buffer-local 'org-imenu-markers)
15904 (defun org-imenu-new-marker (&optional pos)
15905 "Return a new marker for use by Imenu, and remember the marker."
15906 (let ((m (make-marker)))
15907 (move-marker m (or pos (point)))
15908 (push m org-imenu-markers)
15911 (defun org-imenu-get-tree ()
15912 "Produce the index for Imenu."
15913 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
15914 (setq org-imenu-markers nil)
15915 (let* ((n org-imenu-depth)
15916 (re (concat "^" outline-regexp))
15917 (subs (make-vector (1+ n) nil))
15923 (goto-char (point-max))
15924 (while (re-search-backward re nil t)
15925 (setq level (org-reduced-level (funcall outline-level)))
15927 (looking-at org-complex-heading-regexp)
15928 (setq head (org-link-display-format
15929 (org-match-string-no-properties 4))
15930 m (org-imenu-new-marker))
15931 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
15932 (if (>= level last-level)
15933 (push (cons head m) (aref subs level))
15934 (push (cons head (aref subs (1+ level))) (aref subs level))
15935 (loop for i from (1+ level) to n do (aset subs i nil)))
15936 (setq last-level level)))))
15939 (eval-after-load "imenu"
15941 (add-hook 'imenu-after-jump-hook
15943 (if (eq major-mode 'org-mode)
15944 (org-show-context 'org-goto))))))
15946 (defun org-link-display-format (link)
15947 "Replace a link with either the description, or the link target
15948 if no description is present"
15950 (if (string-match org-bracket-link-analytic-regexp link)
15951 (replace-match (or (match-string 5 link)
15952 (concat (match-string 1 link)
15953 (match-string 3 link)))
15957 ;; Speedbar support
15959 (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
15960 "Overlay marking the agenda restriction line in speedbar.")
15961 (org-overlay-put org-speedbar-restriction-lock-overlay
15962 'face 'org-agenda-restriction-lock)
15963 (org-overlay-put org-speedbar-restriction-lock-overlay
15964 'help-echo "Agendas are currently limited to this item.")
15965 (org-detach-overlay org-speedbar-restriction-lock-overlay)
15967 (defun org-speedbar-set-agenda-restriction ()
15968 "Restrict future agenda commands to the location at point in speedbar.
15969 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
15971 (require 'org-agenda)
15972 (let (p m tp np dir txt)
15974 ((setq p (text-property-any (point-at-bol) (point-at-eol)
15976 (setq m (get-text-property p 'org-imenu-marker))
15979 (set-buffer (marker-buffer m))
15981 (org-agenda-set-restriction-lock 'subtree))))
15982 ((setq p (text-property-any (point-at-bol) (point-at-eol)
15983 'speedbar-function 'speedbar-find-file))
15984 (setq tp (previous-single-property-change
15985 (1+ p) 'speedbar-function)
15986 np (next-single-property-change
15987 tp 'speedbar-function)
15988 dir (speedbar-line-directory)
15989 txt (buffer-substring-no-properties (or tp (point-min))
15990 (or np (point-max))))
15993 (set-buffer (find-file-noselect
15994 (let ((default-directory dir))
15995 (expand-file-name txt))))
15996 (unless (org-mode-p)
15997 (error "Cannot restrict to non-Org-mode file"))
15998 (org-agenda-set-restriction-lock 'file))))
15999 (t (error "Don't know how to restrict Org-mode's agenda")))
16000 (org-move-overlay org-speedbar-restriction-lock-overlay
16001 (point-at-bol) (point-at-eol))
16002 (setq current-prefix-arg nil)
16003 (org-agenda-maybe-redo)))
16005 (eval-after-load "speedbar"
16007 (speedbar-add-supported-extension ".org")
16008 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
16009 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
16010 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
16011 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
16012 (add-hook 'speedbar-visiting-tag-hook
16013 (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
16016 ;;; Fixes and Hacks for problems with other packages
16018 ;; Make flyspell not check words in links, to not mess up our keymap
16019 (defun org-mode-flyspell-verify ()
16020 "Don't let flyspell put overlays at active buttons."
16021 (not (get-text-property (point) 'keymap)))
16023 ;; Make `bookmark-jump' show the jump location if it was hidden.
16024 (eval-after-load "bookmark"
16025 '(if (boundp 'bookmark-after-jump-hook)
16026 ;; We can use the hook
16027 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
16028 ;; Hook not available, use advice
16029 (defadvice bookmark-jump (after org-make-visible activate)
16030 "Make the position visible."
16031 (org-bookmark-jump-unhide))))
16033 ;; Make sure saveplace show the location if it was hidden
16034 (eval-after-load "saveplace"
16035 '(defadvice save-place-find-file-hook (after org-make-visible activate)
16036 "Make the position visible."
16037 (org-bookmark-jump-unhide)))
16039 (defun org-bookmark-jump-unhide ()
16040 "Unhide the current position, to show the bookmark location."
16042 (or (org-invisible-p)
16043 (save-excursion (goto-char (max (point-min) (1- (point))))
16044 (org-invisible-p)))
16045 (org-show-context 'bookmark-jump)))
16047 ;; Make session.el ignore our circular variable
16048 (eval-after-load "session"
16049 '(add-to-list 'session-globals-exclude 'org-mark-ring))
16051 ;;;; Experimental code
16053 (defun org-closed-in-range ()
16054 "Sparse tree of items closed in a certain time range.
16055 Still experimental, may disappear in the future."
16057 ;; Get the time interval from the user.
16058 (let* ((time1 (time-to-seconds
16059 (org-read-date nil 'to-time nil "Starting date: ")))
16060 (time2 (time-to-seconds
16061 (org-read-date nil 'to-time nil "End date:")))
16062 ;; callback function
16063 (callback (lambda ()
16066 (apply 'encode-time
16067 (org-parse-time-string
16068 (match-string 1))))))
16069 ;; check if time in interval
16070 (and (>= time time1) (<= time time2))))))
16071 ;; make tree, check each match with the callback
16072 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
16078 (run-hooks 'org-load-hook)
16080 ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
16082 ;;; org.el ends here