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
" irc: Links to IRC/ERC chat sessions" org-irc
)
171 (const :tag
" mac-message: Links to messages in Apple Mail" org-mac-message
)
172 (const :tag
" mew Links to Mew folders/messages" org-mew
)
173 (const :tag
" mhe: Links to MHE folders/messages" org-mhe
)
174 (const :tag
" rmail: Links to RMAIL folders/messages" org-rmail
)
175 (const :tag
" vm: Links to VM folders/messages" org-vm
)
176 (const :tag
" wl: Links to Wanderlust folders/messages" org-wl
)
177 (const :tag
" w3m: Special cut/past from w3m to Org." org-w3m
)
178 (const :tag
" mouse: Additional mouse support" org-mouse
)
180 (const :tag
"C annotate-file: Annotate a file with org syntax" org-annotate-file
)
181 (const :tag
"C annotation-helper: Call Remember directly from Browser" org-annotation-helper
)
182 (const :tag
"C bookmark: Org links to bookmarks" org-bookmark
)
183 (const :tag
"C browser-url: Store link, directly from Browser" org-browser-url
)
184 (const :tag
"C choose: Use TODO keywords to mark decisions states" org-choose
)
185 (const :tag
"C collector: Collect properties into tables" org-collector
)
186 (const :tag
"C depend: TODO dependencies for Org-mode" org-depend
)
187 (const :tag
"C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol
)
188 (const :tag
"C eval: Include command output as text" org-eval
)
189 (const :tag
"C eval-light: Evaluate inbuffer-code on demand" org-eval-light
)
190 (const :tag
"C expiry: Expiry mechanism for Org entries" org-expiry
)
191 (const :tag
"C exp-bibtex: Export citations using BibTeX" org-exp-bibtex
)
192 (const :tag
"C exp-blocks: Pre-process blocks for export" org-exp-blocks
)
193 (const :tag
"C interactive-query: Interactive modification of tags query" org-interactive-query
)
194 (const :tag
"C mairix: Hook mairix search into Org for different MUAs" org-mairix
)
195 (const :tag
"C man: Support for links to manpages in Org-mode" org-man
)
196 (const :tag
"C mtags: Support for muse-like tags" org-mtags
)
197 (const :tag
"C panel: Simple routines for us with bad memory" org-panel
)
198 (const :tag
"C R: Computation using the R language" org-R
)
199 (const :tag
"C registry: A registry for Org links" org-registry
)
200 (const :tag
"C org2rem: Convert org appointments into reminders" org2rem
)
201 (const :tag
"C screen: Visit screen sessions through Org-mode links" org-screen
)
202 (const :tag
"C toc: Table of contents for Org-mode buffer" org-toc
)
203 (const :tag
"C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert
)
204 (repeat :tag
"External packages" :inline t
(symbol :tag
"Package"))))
206 (defcustom org-support-shift-select nil
207 "Non-nil means, make shift-cursor commands select text when possible.
209 In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
210 selecting a region, or enlarge thusly regions started in this way.
211 In Org-mode, in special contexts, these same keys are used for other
212 purposes, important enough to compete with shift selection. Org tries
213 to balance these needs by supporting `shift-select-mode' outside these
214 special contexts, under control of this variable.
216 The default of this variable is nil, to avoid confusing behavior. Shifted
217 cursor keys will then execute Org commands in the following contexts:
218 - on a headline, changing TODO state (left/right) and priority (up/down)
219 - on a time stamp, changing the time
220 - in a plain list item, changing the bullet type
221 - in a property definition line, switching between allowed values
222 - in the BEGIN line of a clock table (changing the time block).
223 Outside these contexts, the commands will throw an error.
225 When this variable is t and the cursor is not in a special context,
226 Org-mode will support shift-selection for making and enlarging regions.
227 To make this more effective, the bullet cycling will no longer happen
228 anywhere in an item line, but only if the cursor is exactly on the bullet.
230 If you set this variable to the symbol `always', then the keys
231 will not be special in headlines, property lines, and item lines, to make
232 shift selection work there as well. If this is what you want, you can
233 use the following alternative commands: `C-c C-t' and `C-c ,' to
234 change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
235 TODO sets, `C-c -' to cycle item bullet types, and properties can be
236 edited by hand or in column view.
238 However, when the cursor is on a timestamp, shift-cursor commands
239 will still edit the time stamp - this is just too good to give up.
241 XEmacs user should have this variable set to nil, because shift-select-mode
245 (const :tag
"Never" nil
)
246 (const :tag
"When outside special context" t
)
247 (const :tag
"Everywhere except timestamps" always
)))
249 (defgroup org-startup nil
250 "Options concerning startup of Org-mode."
254 (defcustom org-startup-folded t
255 "Non-nil means, entering Org-mode will switch to OVERVIEW.
256 This can also be configured on a per-file basis by adding one of
257 the following lines anywhere in the buffer:
264 (const :tag
"nofold: show all" nil
)
265 (const :tag
"fold: overview" t
)
266 (const :tag
"content: all headlines" content
)))
268 (defcustom org-startup-truncated t
269 "Non-nil means, entering Org-mode will set `truncate-lines'.
270 This is useful since some lines containing links can be very long and
271 uninteresting. Also tables look terrible when wrapped."
275 (defcustom org-startup-align-all-tables nil
276 "Non-nil means, align all tables when visiting a file.
277 This is useful when the column width in tables is forced with <N> cookies
278 in table fields. Such tables will look correct only after the first re-align.
279 This can also be configured on a per-file basis by adding one of
280 the following lines anywhere in the buffer:
286 (defcustom org-insert-mode-line-in-empty-file nil
287 "Non-nil means insert the first line setting Org-mode in empty files.
288 When the function `org-mode' is called interactively in an empty file, this
289 normally means that the file name does not automatically trigger Org-mode.
290 To ensure that the file will always be in Org-mode in the future, a
291 line enforcing Org-mode will be inserted into the buffer, if this option
296 (defcustom org-replace-disputed-keys nil
297 "Non-nil means use alternative key bindings for some keys.
298 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
299 These keys are also used by other packages like `CUA-mode' or `windmove.el'.
300 If you want to use Org-mode together with one of these other modes,
301 or more generally if you would like to move some Org-mode commands to
302 other keys, set this variable and configure the keys with the variable
305 This option is only relevant at load-time of Org-mode, and must be set
306 *before* org.el is loaded. Changing it requires a restart of Emacs to
311 (defcustom org-use-extra-keys nil
312 "Non-nil means use extra key sequence definitions for certain
313 commands. This happens automatically if you run XEmacs or if
314 window-system is nil. This variable lets you do the same
315 manually. You must set it before loading org.
317 Example: on Carbon Emacs 22 running graphically, with an external
318 keyboard on a Powerbook, the default way of setting M-left might
319 not work for either Alt or ESC. Setting this variable will make
324 (if (fboundp 'defvaralias
)
325 (defvaralias 'org-CUA-compatible
'org-replace-disputed-keys
))
327 (defcustom org-disputed-keys
328 '(([(shift up
)] .
[(meta p
)])
329 ([(shift down
)] .
[(meta n
)])
330 ([(shift left
)] .
[(meta -
)])
331 ([(shift right
)] .
[(meta +)])
332 ([(control shift right
)] .
[(meta shift
+)])
333 ([(control shift left
)] .
[(meta shift -
)]))
334 "Keys for which Org-mode and other modes compete.
335 This is an alist, cars are the default keys, second element specifies
336 the alternative to use when `org-replace-disputed-keys' is t.
338 Keys can be specified in any syntax supported by `define-key'.
339 The value of this option takes effect only at Org-mode's startup,
340 therefore you'll have to restart Emacs to apply it after changing."
345 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
346 Or return the original if not disputed."
347 (if org-replace-disputed-keys
348 (let* ((nkey (key-description key
))
349 (x (org-find-if (lambda (x)
350 (equal (key-description (car x
)) nkey
))
355 (defun org-find-if (predicate seq
)
358 (if (funcall predicate
(car seq
))
359 (throw 'exit
(car seq
))
362 (defun org-defkey (keymap key def
)
363 "Define a key, possibly translated, as returned by `org-key'."
364 (define-key keymap
(org-key key
) def
))
366 (defcustom org-ellipsis nil
367 "The ellipsis to use in the Org-mode outline.
368 When nil, just use the standard three dots. When a string, use that instead,
369 When a face, use the standard 3 dots, but with the specified face.
370 The change affects only Org-mode (which will then use its own display table).
371 Changing this requires executing `M-x org-mode' in a buffer to become
374 :type
'(choice (const :tag
"Default" nil
)
375 (face :tag
"Face" :value org-warning
)
376 (string :tag
"String" :value
"...#")))
378 (defvar org-display-table nil
379 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
381 (defgroup org-keywords nil
382 "Keywords in Org-mode."
386 (defcustom org-deadline-string
"DEADLINE:"
387 "String to mark deadline entries.
388 A deadline is this string, followed by a time stamp. Should be a word,
389 terminated by a colon. You can insert a schedule keyword and
390 a timestamp with \\[org-deadline].
391 Changes become only effective after restarting Emacs."
395 (defcustom org-scheduled-string
"SCHEDULED:"
396 "String to mark scheduled TODO entries.
397 A schedule is this string, followed by a time stamp. Should be a word,
398 terminated by a colon. You can insert a schedule keyword and
399 a timestamp with \\[org-schedule].
400 Changes become only effective after restarting Emacs."
404 (defcustom org-closed-string
"CLOSED:"
405 "String used as the prefix for timestamps logging closing a TODO entry."
409 (defcustom org-clock-string
"CLOCK:"
410 "String used as prefix for timestamps clocking work hours on an item."
414 (defcustom org-comment-string
"COMMENT"
415 "Entries starting with this keyword will never be exported.
416 An entry can be toggled between COMMENT and normal with
417 \\[org-toggle-comment].
418 Changes become only effective after restarting Emacs."
422 (defcustom org-quote-string
"QUOTE"
423 "Entries starting with this keyword will be exported in fixed-width font.
424 Quoting applies only to the text in the entry following the headline, and does
425 not extend beyond the next headline, even if that is lower level.
426 An entry can be toggled between QUOTE and normal with
427 \\[org-toggle-fixed-width-section]."
431 (defconst org-repeat-re
432 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
433 "Regular expression for specifying repeated events.
434 After a match, group 1 contains the repeat expression.")
436 (defgroup org-structure nil
437 "Options concerning the general structure of Org-mode files."
441 (defgroup org-reveal-location nil
442 "Options about how to make context of a location visible."
443 :tag
"Org Reveal Location"
444 :group
'org-structure
)
446 (defconst org-context-choice
448 (const :tag
"Always" t
)
449 (const :tag
"Never" nil
)
450 (repeat :greedy t
:tag
"Individual contexts"
452 (choice :tag
"Context"
459 (const bookmark-jump
)
463 "Contexts for the reveal options.")
465 (defcustom org-show-hierarchy-above
'((default . t
))
466 "Non-nil means, show full hierarchy when revealing a location.
467 Org-mode often shows locations in an org-mode file which might have
468 been invisible before. When this is set, the hierarchy of headings
469 above the exposed location is shown.
470 Turning this off for example for sparse trees makes them very compact.
471 Instead of t, this can also be an alist specifying this option for different
472 contexts. Valid contexts are
473 agenda when exposing an entry from the agenda
474 org-goto when using the command `org-goto' on key C-c C-j
475 occur-tree when using the command `org-occur' on key C-c /
476 tags-tree when constructing a sparse tree based on tags matches
477 link-search when exposing search matches associated with a link
478 mark-goto when exposing the jump goal of a mark
479 bookmark-jump when exposing a bookmark location
480 isearch when exiting from an incremental search
481 default default for all contexts not set explicitly"
482 :group
'org-reveal-location
483 :type org-context-choice
)
485 (defcustom org-show-following-heading
'((default . nil
))
486 "Non-nil means, show following heading when revealing a location.
487 Org-mode often shows locations in an org-mode file which might have
488 been invisible before. When this is set, the heading following the
490 Turning this off for example for sparse trees makes them very compact,
491 but makes it harder to edit the location of the match. In such a case,
492 use the command \\[org-reveal] to show more context.
493 Instead of t, this can also be an alist specifying this option for different
494 contexts. See `org-show-hierarchy-above' for valid contexts."
495 :group
'org-reveal-location
496 :type org-context-choice
)
498 (defcustom org-show-siblings
'((default . nil
) (isearch t
))
499 "Non-nil means, show all sibling heading when revealing a location.
500 Org-mode often shows locations in an org-mode file which might have
501 been invisible before. When this is set, the sibling of the current entry
502 heading are all made visible. If `org-show-hierarchy-above' is t,
503 the same happens on each level of the hierarchy above the current entry.
505 By default this is on for the isearch context, off for all other contexts.
506 Turning this off for example for sparse trees makes them very compact,
507 but makes it harder to edit the location of the match. In such a case,
508 use the command \\[org-reveal] to show more context.
509 Instead of t, this can also be an alist specifying this option for different
510 contexts. See `org-show-hierarchy-above' for valid contexts."
511 :group
'org-reveal-location
512 :type org-context-choice
)
514 (defcustom org-show-entry-below
'((default . nil
))
515 "Non-nil means, show the entry below a headline when revealing a location.
516 Org-mode often shows locations in an org-mode file which might have
517 been invisible before. When this is set, the text below the headline that is
518 exposed is also shown.
520 By default this is off for all contexts.
521 Instead of t, this can also be an alist specifying this option for different
522 contexts. See `org-show-hierarchy-above' for valid contexts."
523 :group
'org-reveal-location
524 :type org-context-choice
)
526 (defcustom org-indirect-buffer-display
'other-window
527 "How should indirect tree buffers be displayed?
528 This applies to indirect buffers created with the commands
529 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
531 current-window Display in the current window
532 other-window Just display in another window.
533 dedicated-frame Create one new frame, and re-use it each time.
534 new-frame Make a new frame each time. Note that in this case
535 previously-made indirect buffers are kept, and you need to
536 kill these buffers yourself."
537 :group
'org-structure
538 :group
'org-agenda-windows
540 (const :tag
"In current window" current-window
)
541 (const :tag
"In current frame, other window" other-window
)
542 (const :tag
"Each time a new frame" new-frame
)
543 (const :tag
"One dedicated frame" dedicated-frame
)))
545 (defgroup org-cycle nil
546 "Options concerning visibility cycling in Org-mode."
548 :group
'org-structure
)
550 (defcustom org-drawers
'("PROPERTIES" "CLOCK" "LOGBOOK")
551 "Names of drawers. Drawers are not opened by cycling on the headline above.
552 Drawers only open with a TAB on the drawer line itself. A drawer looks like
557 The drawer \"PROPERTIES\" is special for capturing properties through
560 Drawers can be defined on the per-file basis with a line like:
562 #+DRAWERS: HIDDEN STATE PROPERTIES"
563 :group
'org-structure
564 :type
'(repeat (string :tag
"Drawer Name")))
566 (defcustom org-cycle-global-at-bob nil
567 "Cycle globally if cursor is at beginning of buffer and not at a headline.
568 This makes it possible to do global cycling without having to use S-TAB or
569 C-u TAB. For this special case to work, the first line of the buffer
570 must not be a headline - it may be empty or some other text. When used in
571 this way, `org-cycle-hook' is disables temporarily, to make sure the
572 cursor stays at the beginning of the buffer.
573 When this option is nil, don't do anything special at the beginning
578 (defcustom org-cycle-emulate-tab t
579 "Where should `org-cycle' emulate TAB.
581 white Only in completely white lines
582 whitestart Only at the beginning of lines, before the first non-white char
583 t Everywhere except in headlines
584 exc-hl-bol Everywhere except at the start of a headline
585 If TAB is used in a place where it does not emulate TAB, the current subtree
586 visibility is cycled."
588 :type
'(choice (const :tag
"Never" nil
)
589 (const :tag
"Only in completely white lines" white
)
590 (const :tag
"Before first char in a line" whitestart
)
591 (const :tag
"Everywhere except in headlines" t
)
592 (const :tag
"Everywhere except at bol in headlines" exc-hl-bol
)
595 (defcustom org-cycle-separator-lines
2
596 "Number of empty lines needed to keep an empty line between collapsed trees.
597 If you leave an empty line between the end of a subtree and the following
598 headline, this empty line is hidden when the subtree is folded.
599 Org-mode will leave (exactly) one empty line visible if the number of
600 empty lines is equal or larger to the number given in this variable.
601 So the default 2 means, at least 2 empty lines after the end of a subtree
602 are needed to produce free space between a collapsed subtree and the
605 Special case: when 0, never leave empty lines in collapsed view."
608 (put 'org-cycle-separator-lines
'safe-local-variable
'integerp
)
610 (defcustom org-cycle-hook
'(org-cycle-hide-archived-subtrees
611 org-cycle-hide-drawers
612 org-cycle-show-empty-lines
613 org-optimize-window-after-visibility-change
)
614 "Hook that is run after `org-cycle' has changed the buffer visibility.
615 The function(s) in this hook must accept a single argument which indicates
616 the new state that was set by the most recent `org-cycle' command. The
617 argument is a symbol. After a global state change, it can have the values
618 `overview', `content', or `all'. After a local state change, it can have
619 the values `folded', `children', or `subtree'."
623 (defgroup org-edit-structure nil
624 "Options concerning structure editing in Org-mode."
625 :tag
"Org Edit Structure"
626 :group
'org-structure
)
628 (defcustom org-odd-levels-only nil
629 "Non-nil means, skip even levels and only use odd levels for the outline.
630 This has the effect that two stars are being added/taken away in
631 promotion/demotion commands. It also influences how levels are
632 handled by the exporters.
633 Changing it requires restart of `font-lock-mode' to become effective
634 for fontification also in regions already fontified.
635 You may also set this on a per-file basis by adding one of the following
640 :group
'org-edit-structure
641 :group
'org-font-lock
644 (defcustom org-adapt-indentation t
645 "Non-nil means, adapt indentation when promoting and demoting.
646 When this is set and the *entire* text in an entry is indented, the
647 indentation is increased by one space in a demotion command, and
648 decreased by one in a promotion command. If any line in the entry
649 body starts at column 0, indentation is not changed at all."
650 :group
'org-edit-structure
653 (defcustom org-special-ctrl-a
/e nil
654 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
656 When t, `C-a' will bring back the cursor to the beginning of the
657 headline text, i.e. after the stars and after a possible TODO keyword.
658 In an item, this will be the position after the bullet.
659 When the cursor is already at that position, another `C-a' will bring
660 it to the beginning of the line.
662 `C-e' will jump to the end of the headline, ignoring the presence of tags
663 in the headline. A second `C-e' will then jump to the true end of the
664 line, after any tags.
666 When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
667 and only a directly following, identical keypress will bring the cursor
668 to the special positions.
670 This may also be a cons cell where the behavior for `C-a' and `C-e' is
672 :group
'org-edit-structure
674 (const :tag
"off" nil
)
675 (const :tag
"after stars/bullet and before tags first" t
)
676 (const :tag
"true line boundary first" reversed
)
677 (cons :tag
"Set C-a and C-e separately"
678 (choice :tag
"Special C-a"
679 (const :tag
"off" nil
)
680 (const :tag
"after stars/bullet first" t
)
681 (const :tag
"before stars/bullet first" reversed
))
682 (choice :tag
"Special C-e"
683 (const :tag
"off" nil
)
684 (const :tag
"before tags first" t
)
685 (const :tag
"after tags first" reversed
)))))
686 (if (fboundp 'defvaralias
)
687 (defvaralias 'org-special-ctrl-a
'org-special-ctrl-a
/e
))
689 (defcustom org-special-ctrl-k nil
690 "Non-nil means `C-k' will behave specially in headlines.
691 When nil, `C-k' will call the default `kill-line' command.
692 When t, the following will happen while the cursor is in the headline:
694 - When the cursor is at the beginning of a headline, kill the entire
695 line and possible the folded subtree below the line.
696 - When in the middle of the headline text, kill the headline up to the tags.
697 - When after the headline text, kill the tags."
698 :group
'org-edit-structure
701 (defcustom org-yank-folded-subtrees t
702 "Non-nil means, when yanking subtrees, fold them.
703 If the kill is a single subtree, or a sequence of subtrees, i.e. if
704 it starts with a heading and all other headings in it are either children
705 or siblings, then fold all the subtrees. However, do this only if no
706 text after the yank would be swallowed into a folded tree by this action."
707 :group
'org-edit-structure
710 (defcustom org-yank-adjusted-subtrees nil
711 "Non-nil means, when yanking subtrees, adjust the level.
712 With this setting, `org-paste-subtree' is used to insert the subtree, see
713 this function for details."
714 :group
'org-edit-structure
717 (defcustom org-M-RET-may-split-line
'((default . t
))
718 "Non-nil means, M-RET will split the line at the cursor position.
719 When nil, it will go to the end of the line before making a
721 You may also set this option in a different way for different
722 contexts. Valid contexts are:
724 headline when creating a new headline
725 item when creating a new item
726 table in a table field
727 default the value to be used for all contexts not explicitly
729 :group
'org-structure
732 (const :tag
"Always" t
)
733 (const :tag
"Never" nil
)
734 (repeat :greedy t
:tag
"Individual contexts"
736 (choice :tag
"Context"
744 (defcustom org-insert-heading-respect-content nil
745 "Non-nil means, insert new headings after the current subtree.
746 When nil, the new heading is created directly after the current line.
747 The commands \\[org-insert-heading-respect-content] and
748 \\[org-insert-todo-heading-respect-content] turn this variable on
749 for the duration of the command."
750 :group
'org-structure
753 (defcustom org-blank-before-new-entry
'((heading . auto
)
754 (plain-list-item . auto
))
755 "Should `org-insert-heading' leave a blank line before new heading/item?
756 The value is an alist, with `heading' and `plain-list-item' as car,
757 and a boolean flag as cdr. For plain lists, if the variable
758 `org-empty-line-terminates-plain-lists' is set, the setting here
759 is ignored and no empty line is inserted, to keep the list in tact."
760 :group
'org-edit-structure
762 (cons (const heading
)
763 (choice (const :tag
"Never" nil
)
764 (const :tag
"Always" t
)
765 (const :tag
"Auto" auto
)))
766 (cons (const plain-list-item
)
767 (choice (const :tag
"Never" nil
)
768 (const :tag
"Always" t
)
769 (const :tag
"Auto" auto
)))))
771 (defcustom org-insert-heading-hook nil
772 "Hook being run after inserting a new heading."
773 :group
'org-edit-structure
776 (defcustom org-enable-fixed-width-editor t
777 "Non-nil means, lines starting with \":\" are treated as fixed-width.
778 This currently only means, they are never auto-wrapped.
779 When nil, such lines will be treated like ordinary lines.
780 See also the QUOTE keyword."
781 :group
'org-edit-structure
784 (defcustom org-edit-src-region-extra nil
785 "Additional regexps to identify regions for editing with `org-edit-src-code'.
786 For examples see the function `org-edit-src-find-region-and-lang'.
787 The regular expression identifying the begin marker should end with a newline,
788 and the regexp marking the end line should start with a newline, to make sure
789 there are kept outside the narrowed region."
790 :group
'org-edit-structure
793 (regexp :tag
"begin regexp")
794 (regexp :tag
"end regexp")
795 (choice :tag
"language"
796 (string :tag
"specify")
797 (integer :tag
"from match group")
798 (const :tag
"from `lang' element")
799 (const :tag
"from `style' element")))))
801 (defcustom org-coderef-label-format
"(ref:%s)"
802 "The default coderef format.
803 This format string will be used to search for coderef labels in literal
804 examples (EXAMPLE and SRC blocks). The format can be overwritten
805 an individual literal example with the -f option, like
807 #+BEGIN_SRC pascal +n -r -l \"((%s))\"
811 If you want to use this for HTML export, make sure that the format does
812 not introduce special font-locking, and avoid the HTML special
813 characters `<', `>', and `&'. The reason for this restriction is that
814 the labels are searched for only after htmlize has done its job."
815 :group
'org-edit-structure
; FIXME this is not in the right group
818 (defcustom org-edit-fixed-width-region-mode
'artist-mode
819 "The mode that should be used to edit fixed-width regions.
820 These are the regions where each line starts with a colon."
821 :group
'org-edit-structure
825 (const fundamental-mode
)
826 (function :tag
"Other (specify)")))
828 (defcustom org-goto-auto-isearch t
829 "Non-nil means, typing characters in org-goto starts incremental search."
830 :group
'org-edit-structure
833 (defgroup org-sparse-trees nil
834 "Options concerning sparse trees in Org-mode."
835 :tag
"Org Sparse Trees"
836 :group
'org-structure
)
838 (defcustom org-highlight-sparse-tree-matches t
839 "Non-nil means, highlight all matches that define a sparse tree.
840 The highlights will automatically disappear the next time the buffer is
841 changed by an edit command."
842 :group
'org-sparse-trees
845 (defcustom org-remove-highlights-with-change t
846 "Non-nil means, any change to the buffer will remove temporary highlights.
847 Such highlights are created by `org-occur' and `org-clock-display'.
848 When nil, `C-c C-c needs to be used to get rid of the highlights.
849 The highlights created by `org-preview-latex-fragment' always need
850 `C-c C-c' to be removed."
851 :group
'org-sparse-trees
856 (defcustom org-occur-hook
'(org-first-headline-recenter)
857 "Hook that is run after `org-occur' has constructed a sparse tree.
858 This can be used to recenter the window to show as much of the structure
860 :group
'org-sparse-trees
863 (defgroup org-imenu-and-speedbar nil
864 "Options concerning imenu and speedbar in Org-mode."
865 :tag
"Org Imenu and Speedbar"
866 :group
'org-structure
)
868 (defcustom org-imenu-depth
2
869 "The maximum level for Imenu access to Org-mode headlines.
870 This also applied for speedbar access."
871 :group
'org-imenu-and-speedbar
874 (defgroup org-table nil
875 "Options concerning tables in Org-mode."
879 (defcustom org-enable-table-editor
'optimized
880 "Non-nil means, lines starting with \"|\" are handled by the table editor.
881 When nil, such lines will be treated like ordinary lines.
883 When equal to the symbol `optimized', the table editor will be optimized to
885 - Automatic overwrite mode in front of whitespace in table fields.
886 This makes the structure of the table stay in tact as long as the edited
887 field does not exceed the column width.
888 - Minimize the number of realigns. Normally, the table is aligned each time
889 TAB or RET are pressed to move to another field. With optimization this
890 happens only if changes to a field might have changed the column width.
891 Optimization requires replacing the functions `self-insert-command',
892 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
893 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
894 very good at guessing when a re-align will be necessary, but you can always
895 force one with \\[org-ctrl-c-ctrl-c].
897 If you would like to use the optimized version in Org-mode, but the
898 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
900 This variable can be used to turn on and off the table editor during a session,
901 but in order to toggle optimization, a restart is required.
903 See also the variable `org-table-auto-blank-field'."
906 (const :tag
"off" nil
)
908 (const :tag
"on, optimized" optimized
)))
910 (defcustom org-self-insert-cluster-for-undo t
911 "Non-nil means cluster self-insert commands for undo when possible.
912 If this is set, then, like in the Emacs command loop, 20 consequtive
913 characters will be undone together.
914 This is configurable, because there is some impact on typing performance."
918 (defcustom org-table-tab-recognizes-table.el t
919 "Non-nil means, TAB will automatically notice a table.el table.
920 When it sees such a table, it moves point into it and - if necessary -
921 calls `table-recognize-table'."
922 :group
'org-table-editing
925 (defgroup org-link nil
926 "Options concerning links in Org-mode."
930 (defvar org-link-abbrev-alist-local nil
931 "Buffer-local version of `org-link-abbrev-alist', which see.
932 The value of this is taken from the #+LINK lines.")
933 (make-variable-buffer-local 'org-link-abbrev-alist-local
)
935 (defcustom org-link-abbrev-alist nil
936 "Alist of link abbreviations.
937 The car of each element is a string, to be replaced at the start of a link.
938 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
939 links in Org-mode buffers can have an optional tag after a double colon, e.g.
941 [[linkkey:tag][description]]
943 The 'linkkey' must be a word word, starting with a letter, followed
944 by letters, numbers, '-' or '_'.
946 If REPLACE is a string, the tag will simply be appended to create the link.
947 If the string contains \"%s\", the tag will be inserted there. Alternatively,
948 the placeholder \"%h\" will cause a url-encoded version of the tag to
949 be inserted at that point (see the function `url-hexify-string').
951 REPLACE may also be a function that will be called with the tag as the
952 only argument to create the link, which should be returned as a string.
954 See the manual for examples."
958 (string :tag
"Protocol")
960 (string :tag
"Format")
963 (defcustom org-descriptive-links t
964 "Non-nil means, hide link part and only show description of bracket links.
965 Bracket links are like [[link][description]]. This variable sets the initial
966 state in new org-mode buffers. The setting can then be toggled on a
967 per-buffer basis from the Org->Hyperlinks menu."
971 (defcustom org-link-file-path-type
'adaptive
972 "How the path name in file links should be stored.
975 relative Relative to the current directory, i.e. the directory of the file
976 into which the link is being inserted.
977 absolute Absolute path, if possible with ~ for home directory.
978 noabbrev Absolute path, no abbreviation of home directory.
979 adaptive Use relative path for files in the current directory and sub-
980 directories of it. For other files, use an absolute path."
988 (defcustom org-activate-links
'(bracket angle plain radio tag date footnote
)
989 "Types of links that should be activated in Org-mode files.
990 This is a list of symbols, each leading to the activation of a certain link
991 type. In principle, it does not hurt to turn on most link types - there may
992 be a small gain when turning off unused link types. The types are:
994 bracket The recommended [[link][description]] or [[link]] links with hiding.
995 angular Links in angular brackets that may contain whitespace like
996 <bbdb:Carsten Dominik>.
997 plain Plain links in normal text, no whitespace, like http://google.com.
998 radio Text that is matched by a radio target, see manual for details.
999 tag Tag settings in a headline (link to tag search).
1000 date Time stamps (link to calendar).
1001 footnote Footnote labels.
1003 Changing this variable requires a restart of Emacs to become effective."
1005 :type
'(set :greedy t
1006 (const :tag
"Double bracket links (new style)" bracket
)
1007 (const :tag
"Angular bracket links (old style)" angular
)
1008 (const :tag
"Plain text links" plain
)
1009 (const :tag
"Radio target matches" radio
)
1010 (const :tag
"Tags" tag
)
1011 (const :tag
"Timestamps" date
)
1012 (const :tag
"Footnotes" footnote
)))
1014 (defcustom org-make-link-description-function nil
1015 "Function to use to generate link descriptions from links. If
1016 nil the link location will be used. This function must take two
1017 parameters; the first is the link and the second the description
1018 org-insert-link has generated, and should return the description
1023 (defgroup org-link-store nil
1024 "Options concerning storing links in Org-mode."
1025 :tag
"Org Store Link"
1028 (defcustom org-email-link-description-format
"Email %c: %.30s"
1029 "Format of the description part of a link to an email or usenet message.
1030 The following %-escapes will be replaced by corresponding information:
1032 %F full \"From\" field
1033 %f name, taken from \"From\" field, address if no name
1034 %T full \"To\" field
1035 %t first name in \"To\" field, address if no name
1036 %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
1037 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
1041 You may use normal field width specification between the % and the letter.
1042 This is for example useful to limit the length of the subject.
1044 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
1045 :group
'org-link-store
1048 (defcustom org-from-is-user-regexp
1050 (when (and user-mail-address
(not (string= user-mail-address
"")))
1051 (setq r1
(concat "\\<" (regexp-quote user-mail-address
) "\\>")))
1052 (when (and user-full-name
(not (string= user-full-name
"")))
1053 (setq r2
(concat "\\<" (regexp-quote user-full-name
) "\\>")))
1054 (if (and r1 r2
) (concat r1
"\\|" r2
) (or r1 r2
)))
1055 "Regexp matched against the \"From:\" header of an email or usenet message.
1056 It should match if the message is from the user him/herself."
1057 :group
'org-link-store
1060 (defcustom org-link-to-org-use-id
'create-if-interactive
1061 "Non-nil means, storing a link to an Org file will use entry IDs.
1063 Note that before this variable is even considered, org-id must be loaded,
1064 to please customize `org-modules' and turn it on.
1066 The variable can have the following values:
1068 t Create an ID if needed to make a link to the current entry.
1070 create-if-interactive
1071 If `org-store-link' is called directly (interactively, as a user
1072 command), do create an ID to support the link. But when doing the
1073 job for remember, only use the ID if it already exists. The
1074 purpose of this setting is to avoid proliferation of unwanted
1075 IDs, just because you happen to be in an Org file when you
1076 call `org-remember' that automatically and preemptively
1077 creates a link. If you do want to get an ID link in a remember
1078 template to an entry not having an ID, create it first by
1079 explicitly creating a link to it, using `C-c C-l' first.
1082 Use existing ID, do not create one.
1084 nil Never use an ID to make a link, instead link using a text search for
1086 :group
'org-link-store
1088 (const :tag
"Create ID to make link" t
)
1089 (const :tag
"Create if string link interactively"
1090 'create-if-interactive
)
1091 (const :tag
"Only use existing" 'use-existing
)
1092 (const :tag
"Do not use ID to create link" nil
)))
1094 (defcustom org-context-in-file-links t
1095 "Non-nil means, file links from `org-store-link' contain context.
1096 A search string will be added to the file name with :: as separator and
1097 used to find the context when the link is activated by the command
1098 `org-open-at-point'.
1099 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1100 negates this setting for the duration of the command."
1101 :group
'org-link-store
1104 (defcustom org-keep-stored-link-after-insertion nil
1105 "Non-nil means, keep link in list for entire session.
1107 The command `org-store-link' adds a link pointing to the current
1108 location to an internal list. These links accumulate during a session.
1109 The command `org-insert-link' can be used to insert links into any
1110 Org-mode file (offering completion for all stored links). When this
1111 option is nil, every link which has been inserted once using \\[org-insert-link]
1112 will be removed from the list, to make completing the unused links
1114 :group
'org-link-store
1117 (defgroup org-link-follow nil
1118 "Options concerning following links in Org-mode."
1119 :tag
"Org Follow Link"
1122 (defcustom org-link-translation-function nil
1123 "Function to translate links with different syntax to Org syntax.
1124 This can be used to translate links created for example by the Planner
1125 or emacs-wiki packages to Org syntax.
1126 The function must accept two parameters, a TYPE containing the link
1127 protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
1128 which is everything after the link protocol. It should return a cons
1129 with possibly modified values of type and path.
1130 Org contains a function for this, so if you set this variable to
1131 `org-translate-link-from-planner', you should be able follow many
1132 links created by planner."
1133 :group
'org-link-follow
1136 (defcustom org-follow-link-hook nil
1137 "Hook that is run after a link has been followed."
1138 :group
'org-link-follow
1141 (defcustom org-tab-follows-link nil
1142 "Non-nil means, on links TAB will follow the link.
1143 Needs to be set before org.el is loaded.
1144 This really should not be used, it does not make sense, and the
1145 implementation is bad."
1146 :group
'org-link-follow
1149 (defcustom org-return-follows-link nil
1150 "Non-nil means, on links RET will follow the link.
1151 Needs to be set before org.el is loaded."
1152 :group
'org-link-follow
1155 (defcustom org-mouse-1-follows-link
1156 (if (boundp 'mouse-1-click-follows-link
) mouse-1-click-follows-link t
)
1157 "Non-nil means, mouse-1 on a link will follow the link.
1158 A longer mouse click will still set point. Does not work on XEmacs.
1159 Needs to be set before org.el is loaded."
1160 :group
'org-link-follow
1163 (defcustom org-mark-ring-length
4
1164 "Number of different positions to be recorded in the ring
1165 Changing this requires a restart of Emacs to work correctly."
1166 :group
'org-link-follow
1169 (defcustom org-link-frame-setup
1170 '((vm . vm-visit-folder-other-frame
)
1171 (gnus . gnus-other-frame
)
1172 (file . find-file-other-window
))
1173 "Setup the frame configuration for following links.
1174 When following a link with Emacs, it may often be useful to display
1175 this link in another window or frame. This variable can be used to
1176 set this up for the different types of links.
1179 `vm-visit-folder-other-frame'
1180 For Gnus, use any of
1183 `org-gnus-no-new-news'
1184 For FILE, use any of
1186 `find-file-other-window'
1187 `find-file-other-frame'
1188 For the calendar, use the variable `calendar-setup'.
1189 For BBDB, it is currently only possible to display the matches in
1191 :group
'org-link-follow
1195 (const vm-visit-folder
)
1196 (const vm-visit-folder-other-window
)
1197 (const vm-visit-folder-other-frame
)))
1201 (const gnus-other-frame
)
1202 (const org-gnus-no-new-news
)))
1206 (const find-file-other-window
)
1207 (const find-file-other-frame
)))))
1209 (defcustom org-display-internal-link-with-indirect-buffer nil
1210 "Non-nil means, use indirect buffer to display infile links.
1211 Activating internal links (from one location in a file to another location
1212 in the same file) normally just jumps to the location. When the link is
1213 activated with a C-u prefix (or with mouse-3), the link is displayed in
1214 another window. When this option is set, the other window actually displays
1215 an indirect buffer clone of the current buffer, to avoid any visibility
1216 changes to the current buffer."
1217 :group
'org-link-follow
1220 (defcustom org-open-non-existing-files nil
1221 "Non-nil means, `org-open-file' will open non-existing files.
1222 When nil, an error will be generated."
1223 :group
'org-link-follow
1226 (defcustom org-open-directory-means-index-dot-org nil
1227 "Non-nil means, a link to a directory really means to index.org.
1228 When nil, following a directory link will run dired or open a finder/explorer
1229 window on that directory."
1230 :group
'org-link-follow
1233 (defcustom org-link-mailto-program
'(browse-url "mailto:%a?subject=%s")
1234 "Function and arguments to call for following mailto links.
1235 This is a list with the first element being a lisp function, and the
1236 remaining elements being arguments to the function. In string arguments,
1237 %a will be replaced by the address, and %s will be replaced by the subject
1238 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1239 :group
'org-link-follow
1241 (const :tag
"browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1242 (const :tag
"compose-mail" (compose-mail "%a" "%s"))
1243 (const :tag
"message-mail" (message-mail "%a" "%s"))
1244 (cons :tag
"other" (function) (repeat :tag
"argument" sexp
))))
1246 (defcustom org-confirm-shell-link-function
'yes-or-no-p
1247 "Non-nil means, ask for confirmation before executing shell links.
1248 Shell links can be dangerous: just think about a link
1250 [[shell:rm -rf ~/*][Google Search]]
1252 This link would show up in your Org-mode document as \"Google Search\",
1253 but really it would remove your entire home directory.
1254 Therefore we advise against setting this variable to nil.
1255 Just change it to `y-or-n-p' if you want to confirm with a
1256 single keystroke rather than having to type \"yes\"."
1257 :group
'org-link-follow
1259 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1260 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1261 (const :tag
"no confirmation (dangerous)" nil
)))
1263 (defcustom org-confirm-elisp-link-function
'yes-or-no-p
1264 "Non-nil means, ask for confirmation before executing Emacs Lisp links.
1265 Elisp links can be dangerous: just think about a link
1267 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1269 This link would show up in your Org-mode document as \"Google Search\",
1270 but really it would remove your entire home directory.
1271 Therefore we advise against setting this variable to nil.
1272 Just change it to `y-or-n-p' if you want to confirm with a
1273 single keystroke rather than having to type \"yes\"."
1274 :group
'org-link-follow
1276 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1277 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1278 (const :tag
"no confirmation (dangerous)" nil
)))
1280 (defconst org-file-apps-defaults-gnu
1284 "Default file applications on a UNIX or GNU/Linux system.
1285 See `org-file-apps'.")
1287 (defconst org-file-apps-defaults-macosx
1290 (system .
"open %s")
1292 ("eps.gz" .
"gv %s")
1294 ("fig" .
"xfig %s"))
1295 "Default file applications on a MacOS X system.
1296 The system \"open\" is known as a default, but we use X11 applications
1297 for some files for which the OS does not have a good default.
1298 See `org-file-apps'.")
1300 (defconst org-file-apps-defaults-windowsnt
1304 (list (if (featurep 'xemacs
)
1305 'mswindows-shell-execute
1309 (list (if (featurep 'xemacs
)
1310 'mswindows-shell-execute
1313 "Default file applications on a Windows NT system.
1314 The system \"open\" is used for most files.
1315 See `org-file-apps'.")
1317 (defcustom org-file-apps
1320 ("\\.x?html?\\'" . default
)
1321 ("\\.pdf\\'" . default
)
1323 "External applications for opening `file:path' items in a document.
1324 Org-mode uses system defaults for different file types, but
1325 you can use this variable to set the application for a given file
1326 extension. The entries in this list are cons cells where the car identifies
1327 files and the cdr the corresponding command. Possible values for the
1329 \"regex\" Regular expression matched against the file name. For backward
1330 compatibility, this can also be a string with only alphanumeric
1331 characters, which is then interpreted as an extension.
1332 `directory' Matches a directory
1333 `remote' Matches a remote file, accessible through tramp or efs.
1334 Remote files most likely should be visited through Emacs
1335 because external applications cannot handle such paths.
1336 `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
1337 so all files Emacs knows how to handle. Using this with
1338 command `emacs' will open most files in Emacs. Beware that this
1339 will also open html files inside Emacs, unless you add
1340 (\"html\" . default) to the list as well.
1341 t Default for files not matched by any of the other options.
1342 `system' The system command to open files, like `open' on Windows
1343 and Mac OS X, and mailcap under GNU/Linux. This is the command
1344 that will be selected if you call `C-c C-o' with a double
1347 Possible values for the command are:
1348 `emacs' The file will be visited by the current Emacs process.
1349 `default' Use the default application for this file type, which is the
1350 association for t in the list, most likely in the system-specific
1352 This can be used to overrule an unwanted setting in the
1353 system-specific variable.
1354 `system' Use the system command for opening files, like \"open\".
1355 This command is specified by the entry whose car is `system'.
1356 Most likely, the system-specific version of this variable
1357 does define this command, but you can overrule/replace it
1359 string A command to be executed by a shell; %s will be replaced
1360 by the path to the file.
1361 sexp A Lisp form which will be evaluated. The file path will
1362 be available in the Lisp variable `file'.
1363 For more examples, see the system specific constants
1364 `org-file-apps-defaults-macosx'
1365 `org-file-apps-defaults-windowsnt'
1366 `org-file-apps-defaults-gnu'."
1367 :group
'org-link-follow
1369 (cons (choice :value
""
1370 (string :tag
"Extension")
1371 (const :tag
"System command to open files" system
)
1372 (const :tag
"Default for unrecognized files" t
)
1373 (const :tag
"Remote file" remote
)
1374 (const :tag
"Links to a directory" directory
)
1375 (const :tag
"Any files that have Emacs modes"
1378 (const :tag
"Visit with Emacs" emacs
)
1379 (const :tag
"Use default" default
)
1380 (const :tag
"Use the system command" system
)
1381 (string :tag
"Command")
1382 (sexp :tag
"Lisp form")))))
1384 (defgroup org-refile nil
1385 "Options concerning refiling entries in Org-mode."
1389 (defcustom org-directory
"~/org"
1390 "Directory with org files.
1391 This is just a default location to look for Org files. There is no need
1392 at all to put your files into this directory. It is only used in the
1393 following situations:
1395 1. When a remember template specifies a target file that is not an
1396 absolute path. The path will then be interpreted relative to
1398 2. When a remember note is filed away in an interactive way (when exiting the
1399 note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
1400 with `org-directory' as the default path."
1402 :group
'org-remember
1405 (defcustom org-default-notes-file
(convert-standard-filename "~/.notes")
1406 "Default target for storing notes.
1407 Used by the hooks for remember.el. This can be a string, or nil to mean
1408 the value of `remember-data-file'.
1409 You can set this on a per-template basis with the variable
1410 `org-remember-templates'."
1412 :group
'org-remember
1414 (const :tag
"Default from remember-data-file" nil
)
1417 (defcustom org-goto-interface
'outline
1418 "The default interface to be used for `org-goto'.
1420 outline The interface shows an outline of the relevant file
1421 and the correct heading is found by moving through
1422 the outline or by searching with incremental search.
1423 outline-path-completion Headlines in the current buffer are offered via
1424 completion. This is the interface also used by
1425 the refile command."
1428 (const :tag
"Outline" outline
)
1429 (const :tag
"Outline-path-completion" outline-path-completion
)))
1431 (defcustom org-goto-max-level
5
1432 "Maximum level to be considered when running org-goto with refile interface."
1436 (defcustom org-reverse-note-order nil
1437 "Non-nil means, store new notes at the beginning of a file or entry.
1438 When nil, new notes will be filed to the end of a file or entry.
1439 This can also be a list with cons cells of regular expressions that
1440 are matched against file names, and values."
1441 :group
'org-remember
1444 (const :tag
"Reverse always" t
)
1445 (const :tag
"Reverse never" nil
)
1446 (repeat :tag
"By file name regexp"
1447 (cons regexp boolean
))))
1449 (defcustom org-refile-targets nil
1450 "Targets for refiling entries with \\[org-refile].
1451 This is list of cons cells. Each cell contains:
1452 - a specification of the files to be considered, either a list of files,
1453 or a symbol whose function or variable value will be used to retrieve
1454 a file name or a list of file names. If you use `org-agenda-files' for
1455 that, all agenda files will be scanned for targets. Nil means, consider
1456 headings in the current buffer.
1457 - A specification of how to find candidate refile targets. This may be
1459 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
1460 This tag has to be present in all target headlines, inheritance will
1462 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
1464 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
1465 headlines that are refiling targets.
1466 - a cons cell (:level . N). Any headline of level N is considered a target.
1467 Note that, when `org-odd-levels-only' is set, level corresponds to
1468 order in hierarchy, not to the number of stars.
1469 - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
1470 Note that, when `org-odd-levels-only' is set, level corresponds to
1471 order in hierarchy, not to the number of stars.
1473 When this variable is nil, all top-level headlines in the current buffer
1474 are used, equivalent to the value `((nil . (:level . 1))'."
1478 (choice :value org-agenda-files
1479 (const :tag
"All agenda files" org-agenda-files
)
1480 (const :tag
"Current buffer" nil
)
1481 (function) (variable) (file))
1482 (choice :tag
"Identify target headline by"
1483 (cons :tag
"Specific tag" (const :value
:tag
) (string))
1484 (cons :tag
"TODO keyword" (const :value
:todo
) (string))
1485 (cons :tag
"Regular expression" (const :value
:regexp
) (regexp))
1486 (cons :tag
"Level number" (const :value
:level
) (integer))
1487 (cons :tag
"Max Level number" (const :value
:maxlevel
) (integer))))))
1489 (defcustom org-refile-use-outline-path nil
1490 "Non-nil means, provide refile targets as paths.
1491 So a level 3 headline will be available as level1/level2/level3.
1492 When the value is `file', also include the file name (without directory)
1493 into the path. When `full-file-path', include the full file path."
1496 (const :tag
"Not" nil
)
1497 (const :tag
"Yes" t
)
1498 (const :tag
"Start with file name" file
)
1499 (const :tag
"Start with full file path" full-file-path
)))
1501 (defcustom org-outline-path-complete-in-steps t
1502 "Non-nil means, complete the outline path in hierarchical steps.
1503 When Org-mode uses the refile interface to select an outline path
1504 \(see variable `org-refile-use-outline-path'), the completion of
1505 the path can be done is a single go, or if can be done in steps down
1506 the headline hierarchy. Going in steps is probably the best if you
1507 do not use a special completion package like `ido' or `icicles'.
1508 However, when using these packages, going in one step can be very
1509 fast, while still showing the whole path to the entry."
1513 (defgroup org-todo nil
1514 "Options concerning TODO items in Org-mode."
1518 (defgroup org-progress nil
1519 "Options concerning Progress logging in Org-mode."
1523 (defvar org-todo-interpretation-widgets
1525 (:tag
"Sequence (cycling hits every state)" sequence
)
1526 (:tag
"Type (cycling directly to DONE)" type
))
1527 "The available interpretation symbols for customizing
1528 `org-todo-keywords'.
1529 Interested libraries should add to this list.")
1531 (defcustom org-todo-keywords
'((sequence "TODO" "DONE"))
1532 "List of TODO entry keyword sequences and their interpretation.
1533 \\<org-mode-map>This is a list of sequences.
1535 Each sequence starts with a symbol, either `sequence' or `type',
1536 indicating if the keywords should be interpreted as a sequence of
1537 action steps, or as different types of TODO items. The first
1538 keywords are states requiring action - these states will select a headline
1539 for inclusion into the global TODO list Org-mode produces. If one of
1540 the \"keywords\" is the vertical bat \"|\" the remaining keywords
1541 signify that no further action is necessary. If \"|\" is not found,
1542 the last keyword is treated as the only DONE state of the sequence.
1544 The command \\[org-todo] cycles an entry through these states, and one
1545 additional state where no keyword is present. For details about this
1546 cycling, see the manual.
1548 TODO keywords and interpretation can also be set on a per-file basis with
1549 the special #+SEQ_TODO and #+TYP_TODO lines.
1551 Each keyword can optionally specify a character for fast state selection
1552 \(in combination with the variable `org-use-fast-todo-selection')
1553 and specifiers for state change logging, using the same syntax
1554 that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
1555 that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
1556 indicates to record a time stamp each time this state is selected.
1558 Each keyword may also specify if a timestamp or a note should be
1559 recorded when entering or leaving the state, by adding additional
1560 characters in the parenthesis after the keyword. This looks like this:
1561 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
1562 record only the time of the state change. With X and Y being either
1563 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
1564 Y when leaving the state if and only if the *target* state does not
1565 define X. You may omit any of the fast-selection key or X or /Y,
1566 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
1568 For backward compatibility, this variable may also be just a list
1569 of keywords - in this case the interpretation (sequence or type) will be
1570 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
1572 :group
'org-keywords
1574 (repeat :tag
"Old syntax, just keywords"
1575 (string :tag
"Keyword"))
1576 (repeat :tag
"New syntax"
1579 :tag
"Interpretation"
1580 ;;Quick and dirty way to see
1581 ;;`org-todo-interpretations'. This takes the
1582 ;;place of item arguments
1590 org-todo-interpretation-widgets
))
1593 (string :tag
"Keyword"))))))
1595 (defvar org-todo-keywords-1 nil
1596 "All TODO and DONE keywords active in a buffer.")
1597 (make-variable-buffer-local 'org-todo-keywords-1
)
1598 (defvar org-todo-keywords-for-agenda nil
)
1599 (defvar org-done-keywords-for-agenda nil
)
1600 (defvar org-todo-keyword-alist-for-agenda nil
)
1601 (defvar org-tag-alist-for-agenda nil
)
1602 (defvar org-agenda-contributing-files nil
)
1603 (defvar org-not-done-keywords nil
)
1604 (make-variable-buffer-local 'org-not-done-keywords
)
1605 (defvar org-done-keywords nil
)
1606 (make-variable-buffer-local 'org-done-keywords
)
1607 (defvar org-todo-heads nil
)
1608 (make-variable-buffer-local 'org-todo-heads
)
1609 (defvar org-todo-sets nil
)
1610 (make-variable-buffer-local 'org-todo-sets
)
1611 (defvar org-todo-log-states nil
)
1612 (make-variable-buffer-local 'org-todo-log-states
)
1613 (defvar org-todo-kwd-alist nil
)
1614 (make-variable-buffer-local 'org-todo-kwd-alist
)
1615 (defvar org-todo-key-alist nil
)
1616 (make-variable-buffer-local 'org-todo-key-alist
)
1617 (defvar org-todo-key-trigger nil
)
1618 (make-variable-buffer-local 'org-todo-key-trigger
)
1620 (defcustom org-todo-interpretation
'sequence
1621 "Controls how TODO keywords are interpreted.
1622 This variable is in principle obsolete and is only used for
1623 backward compatibility, if the interpretation of todo keywords is
1624 not given already in `org-todo-keywords'. See that variable for
1627 :group
'org-keywords
1628 :type
'(choice (const sequence
)
1631 (defcustom org-use-fast-todo-selection t
1632 "Non-nil means, use the fast todo selection scheme with C-c C-t.
1633 This variable describes if and under what circumstances the cycling
1634 mechanism for TODO keywords will be replaced by a single-key, direct
1637 When nil, fast selection is never used.
1639 When the symbol `prefix', it will be used when `org-todo' is called with
1640 a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
1641 in an agenda buffer.
1643 When t, fast selection is used by default. In this case, the prefix
1644 argument forces cycling instead.
1646 In all cases, the special interface is only used if access keys have actually
1647 been assigned by the user, i.e. if keywords in the configuration are followed
1648 by a letter in parenthesis, like TODO(t)."
1651 (const :tag
"Never" nil
)
1652 (const :tag
"By default" t
)
1653 (const :tag
"Only with C-u C-c C-t" prefix
)))
1655 (defcustom org-provide-todo-statistics t
1656 "Non-nil means, update todo statistics after insert and toggle.
1657 When this is set, todo statistics is updated in the parent of the current
1658 entry each time a todo state is changed."
1662 (defcustom org-after-todo-state-change-hook nil
1663 "Hook which is run after the state of a TODO item was changed.
1664 The new state (a string with a TODO keyword, or nil) is available in the
1665 Lisp variable `state'."
1669 (defvar org-blocker-hook nil
1670 "Hook for functions that are allowed to block a state change.
1672 Each function gets as its single argument a property list, see
1673 `org-trigger-hook' for more information about this list.
1675 If any of the functions in this hook returns nil, the state change
1678 (defvar org-trigger-hook nil
1679 "Hook for functions that are triggered by a state change.
1681 Each function gets as its single argument a property list with at least
1682 the following elements:
1684 (:type type-of-change :position pos-at-entry-start
1685 :from old-state :to new-state)
1687 Depending on the type, more properties may be present.
1689 This mechanism is currently implemented for:
1693 :type todo-state-change
1694 :from previous state (keyword as a string), or nil, or a symbol
1695 'todo' or 'done', to indicate the general type of state.
1696 :to new state, like in :from")
1698 (defcustom org-enforce-todo-dependencies nil
1699 "Non-nil means, undone TODO entries will block switching the parent to DONE.
1700 Also, if a parent has an :ORDERED: property, switching an entry to DONE will
1701 be blocked if any prior sibling is not yet done.
1702 This variable needs to be set before org.el is loaded, and you need to
1703 restart Emacs after a change to make the change effective. The only way
1704 to change is while Emacs is running is through the customize interface."
1705 :set
(lambda (var val
)
1708 (add-hook 'org-blocker-hook
1709 'org-block-todo-from-children-or-siblings
)
1710 (remove-hook 'org-blocker-hook
1711 'org-block-todo-from-children-or-siblings
)))
1715 (defcustom org-enforce-todo-checkbox-dependencies nil
1716 "Non-nil means, unchecked boxes will block switching the parent to DONE.
1717 When this is nil, checkboxes have no influence on switching TODO states.
1718 When non-nil, you first need to check off all check boxes before the TODO
1719 entry can be switched to DONE.
1720 This variable needs to be set before org.el is loaded, and you need to
1721 restart Emacs after a change to make the change effective. The only way
1722 to change is while Emacs is running is through the customize interface."
1723 :set
(lambda (var val
)
1726 (add-hook 'org-blocker-hook
1727 'org-block-todo-from-checkboxes
)
1728 (remove-hook 'org-blocker-hook
1729 'org-block-todo-from-checkboxes
)))
1733 (defcustom org-todo-state-tags-triggers nil
1734 "Tag changes that should be triggered by TODO state changes.
1735 This is a list. Each entry is
1737 (state-change (tag . flag) .......)
1739 State-change can be a string with a state, and empty string to indicate the
1740 state that has no TODO keyword, or it can be one of the symbols `todo'
1741 or `done', meaning any not-done or done state, respectively."
1745 (cons (choice :tag
"When changing to"
1746 (const :tag
"Not-done state" todo
)
1747 (const :tag
"Done state" done
)
1748 (string :tag
"State"))
1750 (cons :tag
"Tag action"
1752 (choice (const :tag
"Add" t
) (const :tag
"Remove" nil
)))))))
1754 (defcustom org-log-done nil
1755 "Information to record when a task moves to the DONE state.
1757 Possible values are:
1759 nil Don't add anything, just change the keyword
1760 time Add a time stamp to the task
1761 note Prompt a closing note and add it with template `org-log-note-headings'
1763 This option can also be set with on a per-file-basis with
1765 #+STARTUP: nologdone
1767 #+STARTUP: lognotedone
1769 You can have local logging settings for a subtree by setting the LOGGING
1770 property to one or more of these keywords."
1772 :group
'org-progress
1774 (const :tag
"No logging" nil
)
1775 (const :tag
"Record CLOSED timestamp" time
)
1776 (const :tag
"Record CLOSED timestamp with closing note." note
)))
1778 ;; Normalize old uses of org-log-done.
1780 ((eq org-log-done t
) (setq org-log-done
'time
))
1781 ((and (listp org-log-done
) (memq 'done org-log-done
))
1782 (setq org-log-done
'note
)))
1784 (defcustom org-log-note-clock-out nil
1785 "Non-nil means, record a note when clocking out of an item.
1786 This can also be configured on a per-file basis by adding one of
1787 the following lines anywhere in the buffer:
1789 #+STARTUP: lognoteclock-out
1790 #+STARTUP: nolognoteclock-out"
1792 :group
'org-progress
1795 (defcustom org-log-done-with-time t
1796 "Non-nil means, the CLOSED time stamp will contain date and time.
1797 When nil, only the date will be recorded."
1798 :group
'org-progress
1801 (defcustom org-log-note-headings
1802 '((done .
"CLOSING NOTE %t")
1803 (state .
"State %-12s from %-12S %t")
1804 (note .
"Note taken on %t")
1806 "Headings for notes added to entries.
1807 The value is an alist, with the car being a symbol indicating the note
1808 context, and the cdr is the heading to be used. The heading may also be the
1810 %t in the heading will be replaced by a time stamp.
1811 %s will be replaced by the new TODO state, in double quotes.
1812 %S will be replaced by the old TODO state, in double quotes.
1813 %u will be replaced by the user name.
1814 %U will be replaced by the full user name."
1816 :group
'org-progress
1817 :type
'(list :greedy t
1818 (cons (const :tag
"Heading when closing an item" done
) string
)
1820 "Heading when changing todo state (todo sequence only)"
1822 (cons (const :tag
"Heading when just taking a note" note
) string
)
1823 (cons (const :tag
"Heading when clocking out" clock-out
) string
)))
1825 (unless (assq 'note org-log-note-headings
)
1826 (push '(note .
"%t") org-log-note-headings
))
1828 (defcustom org-log-into-drawer nil
1829 "Non-nil means, insert state change notes and time stamps into a drawer.
1830 When nil, state changes notes will be inserted after the headline and
1831 any scheduling and clock lines, but not inside a drawer.
1833 The value of this variable should be the name of the drawer to use.
1834 LOGBOOK is proposed at the default drawer for this purpose, you can
1835 also set this to a string to define the drawer of your choice.
1837 A value of t is also allowed, representing \"LOGBOOK\".
1839 If this variable is set, `org-log-state-notes-insert-after-drawers'
1842 :group
'org-progress
1844 (const :tag
"Not into a drawer" nil
)
1845 (const :tag
"LOGBOOK" t
)
1846 (string :tag
"Other")))
1848 (if (fboundp 'defvaralias
)
1849 (defvaralias 'org-log-state-notes-into-drawer
'org-log-into-drawer
))
1851 (defcustom org-log-state-notes-insert-after-drawers nil
1852 "Non-nil means, insert state change notes after any drawers in entry.
1853 Only the drawers that *immediately* follow the headline and the
1854 deadline/scheduled line are skipped.
1855 When nil, insert notes right after the heading and perhaps the line
1856 with deadline/scheduling if present.
1858 This variable will have no effect if `org-log-into-drawer' is
1861 :group
'org-progress
1864 (defcustom org-log-states-order-reversed t
1865 "Non-nil means, the latest state change note will be directly after heading.
1866 When nil, the notes will be orderer according to time."
1868 :group
'org-progress
1871 (defcustom org-log-repeat
'time
1872 "Non-nil means, record moving through the DONE state when triggering repeat.
1873 An auto-repeating tasks is immediately switched back to TODO when marked
1874 done. If you are not logging state changes (by adding \"@\" or \"!\" to
1875 the TODO keyword definition, or recording a closing note by setting
1876 `org-log-done', there will be no record of the task moving through DONE.
1877 This variable forces taking a note anyway. Possible values are:
1879 nil Don't force a record
1880 time Record a time stamp
1883 This option can also be set with on a per-file-basis with
1885 #+STARTUP: logrepeat
1886 #+STARTUP: lognoterepeat
1887 #+STARTUP: nologrepeat
1889 You can have local logging settings for a subtree by setting the LOGGING
1890 property to one or more of these keywords."
1892 :group
'org-progress
1894 (const :tag
"Don't force a record" nil
)
1895 (const :tag
"Force recording the DONE state" time
)
1896 (const :tag
"Force recording a note with the DONE state" note
)))
1899 (defgroup org-priorities nil
1900 "Priorities in Org-mode."
1901 :tag
"Org Priorities"
1904 (defcustom org-highest-priority ?A
1905 "The highest priority of TODO items. A character like ?A, ?B etc.
1906 Must have a smaller ASCII number than `org-lowest-priority'."
1907 :group
'org-priorities
1910 (defcustom org-lowest-priority ?C
1911 "The lowest priority of TODO items. A character like ?A, ?B etc.
1912 Must have a larger ASCII number than `org-highest-priority'."
1913 :group
'org-priorities
1916 (defcustom org-default-priority ?B
1917 "The default priority of TODO items.
1918 This is the priority an item get if no explicit priority is given."
1919 :group
'org-priorities
1922 (defcustom org-priority-start-cycle-with-default t
1923 "Non-nil means, start with default priority when starting to cycle.
1924 When this is nil, the first step in the cycle will be (depending on the
1925 command used) one higher or lower that the default priority."
1926 :group
'org-priorities
1929 (defgroup org-time nil
1930 "Options concerning time stamps and deadlines in Org-mode."
1934 (defcustom org-insert-labeled-timestamps-at-point nil
1935 "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
1936 When nil, these labeled time stamps are forces into the second line of an
1937 entry, just after the headline. When scheduling from the global TODO list,
1938 the time stamp will always be forced into the second line."
1942 (defconst org-time-stamp-formats
'("<%Y-%m-%d %a>" .
"<%Y-%m-%d %a %H:%M>")
1943 "Formats for `format-time-string' which are used for time stamps.
1944 It is not recommended to change this constant.")
1946 (defcustom org-time-stamp-rounding-minutes
'(0 5)
1947 "Number of minutes to round time stamps to.
1948 These are two values, the first applies when first creating a time stamp.
1949 The second applies when changing it with the commands `S-up' and `S-down'.
1950 When changing the time stamp, this means that it will change in steps
1951 of N minutes, as given by the second value.
1953 When a setting is 0 or 1, insert the time unmodified. Useful rounding
1954 numbers should be factors of 60, so for example 5, 10, 15.
1956 When this is larger than 1, you can still force an exact time-stamp by using
1957 a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
1958 and by using a prefix arg to `S-up/down' to specify the exact number
1959 of minutes to shift."
1961 :get
'(lambda (var) ; Make sure all entries have 5 elements
1962 (if (integerp (default-value var
))
1963 (list (default-value var
) 5)
1964 (default-value var
)))
1966 (integer :tag
"when inserting times")
1967 (integer :tag
"when modifying times")))
1969 ;; Normalize old customizations of this variable.
1970 (when (integerp org-time-stamp-rounding-minutes
)
1971 (setq org-time-stamp-rounding-minutes
1972 (list org-time-stamp-rounding-minutes
1973 org-time-stamp-rounding-minutes
)))
1975 (defcustom org-display-custom-times nil
1976 "Non-nil means, overlay custom formats over all time stamps.
1977 The formats are defined through the variable `org-time-stamp-custom-formats'.
1978 To turn this on on a per-file basis, insert anywhere in the file:
1979 #+STARTUP: customtime"
1983 (make-variable-buffer-local 'org-display-custom-times
)
1985 (defcustom org-time-stamp-custom-formats
1986 '("<%m/%d/%y %a>" .
"<%m/%d/%y %a %H:%M>") ; american
1987 "Custom formats for time stamps. See `format-time-string' for the syntax.
1988 These are overlayed over the default ISO format if the variable
1989 `org-display-custom-times' is set. Time like %H:%M should be at the
1990 end of the second format."
1994 (defun org-time-stamp-format (&optional long inactive
)
1995 "Get the right format for a time string."
1996 (let ((f (if long
(cdr org-time-stamp-formats
)
1997 (car org-time-stamp-formats
))))
1999 (concat "[" (substring f
1 -
1) "]")
2002 (defcustom org-time-clocksum-format
"%d:%02d"
2003 "The format string used when creating CLOCKSUM lines, or when
2004 org-mode generates a time duration."
2008 (defcustom org-deadline-warning-days
14
2009 "No. of days before expiration during which a deadline becomes active.
2010 This variable governs the display in sparse trees and in the agenda.
2011 When 0 or negative, it means use this number (the absolute value of it)
2012 even if a deadline has a different individual lead time specified.
2014 Custom commands can set this variable in the options section."
2016 :group
'org-agenda-daily
/weekly
2019 (defcustom org-read-date-prefer-future t
2020 "Non-nil means, assume future for incomplete date input from user.
2021 This affects the following situations:
2022 1. The user gives a day, but no month.
2023 For example, if today is the 15th, and you enter \"3\", Org-mode will
2024 read this as the third of *next* month. However, if you enter \"17\",
2025 it will be considered as *this* month.
2026 2. The user gives a month but not a year.
2027 For example, if it is april and you enter \"feb 2\", this will be read
2028 as feb 2, *next* year. \"May 5\", however, will be this year.
2030 Currently this does not work for ISO week specifications.
2032 When this option is nil, the current month and year will always be used
2037 (defcustom org-read-date-display-live t
2038 "Non-nil means, display current interpretation of date prompt live.
2039 This display will be in an overlay, in the minibuffer."
2043 (defcustom org-read-date-popup-calendar t
2044 "Non-nil means, pop up a calendar when prompting for a date.
2045 In the calendar, the date can be selected with mouse-1. However, the
2046 minibuffer will also be active, and you can simply enter the date as well.
2047 When nil, only the minibuffer will be available."
2050 (if (fboundp 'defvaralias
)
2051 (defvaralias 'org-popup-calendar-for-date-prompt
2052 'org-read-date-popup-calendar
))
2054 (defcustom org-extend-today-until
0
2055 "The hour when your day really ends. Must be an integer.
2056 This has influence for the following applications:
2057 - When switching the agenda to \"today\". It it is still earlier than
2058 the time given here, the day recognized as TODAY is actually yesterday.
2059 - When a date is read from the user and it is still before the time given
2060 here, the current date and time will be assumed to be yesterday, 23:59.
2061 Also, timestamps inserted in remember templates follow this rule.
2063 IMPORTANT: This is a feature whose implementation is and likely will
2064 remain incomplete. Really, it is only here because past midnight seems to
2065 be the favorite working time of John Wiegley :-)"
2069 (defcustom org-edit-timestamp-down-means-later nil
2070 "Non-nil means, S-down will increase the time in a time stamp.
2071 When nil, S-up will increase."
2075 (defcustom org-calendar-follow-timestamp-change t
2076 "Non-nil means, make the calendar window follow timestamp changes.
2077 When a timestamp is modified and the calendar window is visible, it will be
2078 moved to the new date."
2082 (defgroup org-tags nil
2083 "Options concerning tags in Org-mode."
2087 (defcustom org-tag-alist nil
2088 "List of tags allowed in Org-mode files.
2089 When this list is nil, Org-mode will base TAG input on what is already in the
2091 The value of this variable is an alist, the car of each entry must be a
2092 keyword as a string, the cdr may be a character that is used to select
2093 that tag through the fast-tag-selection interface.
2094 See the manual for details."
2098 (cons (string :tag
"Tag name")
2099 (character :tag
"Access char"))
2100 (const :tag
"Start radio group" (:startgroup
))
2101 (const :tag
"End radio group" (:endgroup
))
2102 (const :tag
"New line" (:newline
)))))
2104 (defcustom org-tag-persistent-alist nil
2105 "List of tags that will always appear in all Org-mode files.
2106 This is in addition to any in buffer settings or customizations
2108 When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
2109 The value of this variable is an alist, the car of each entry must be a
2110 keyword as a string, the cdr may be a character that is used to select
2111 that tag through the fast-tag-selection interface.
2112 See the manual for details.
2113 To disable these tags on a per-file basis, insert anywhere in the file:
2118 (cons (string :tag
"Tag name")
2119 (character :tag
"Access char"))
2120 (const :tag
"Start radio group" (:startgroup
))
2121 (const :tag
"End radio group" (:endgroup
))
2122 (const :tag
"New line" (:newline
)))))
2124 (defvar org-file-tags nil
2125 "List of tags that can be inherited by all entries in the file.
2126 The tags will be inherited if the variable `org-use-tag-inheritance'
2127 says they should be.
2128 This variable is populated from #+TAG lines.")
2130 (defcustom org-use-fast-tag-selection
'auto
2131 "Non-nil means, use fast tag selection scheme.
2132 This is a special interface to select and deselect tags with single keys.
2133 When nil, fast selection is never used.
2134 When the symbol `auto', fast selection is used if and only if selection
2135 characters for tags have been configured, either through the variable
2136 `org-tag-alist' or through a #+TAGS line in the buffer.
2137 When t, fast selection is always used and selection keys are assigned
2138 automatically if necessary."
2141 (const :tag
"Always" t
)
2142 (const :tag
"Never" nil
)
2143 (const :tag
"When selection characters are configured" 'auto
)))
2145 (defcustom org-fast-tag-selection-single-key nil
2146 "Non-nil means, fast tag selection exits after first change.
2147 When nil, you have to press RET to exit it.
2148 During fast tag selection, you can toggle this flag with `C-c'.
2149 This variable can also have the value `expert'. In this case, the window
2150 displaying the tags menu is not even shown, until you press C-c again."
2153 (const :tag
"No" nil
)
2154 (const :tag
"Yes" t
)
2155 (const :tag
"Expert" expert
)))
2157 (defvar org-fast-tag-selection-include-todo nil
2158 "Non-nil means, fast tags selection interface will also offer TODO states.
2159 This is an undocumented feature, you should not rely on it.")
2161 (defcustom org-tags-column
(if (featurep 'xemacs
) -
76 -
77)
2162 "The column to which tags should be indented in a headline.
2163 If this number is positive, it specifies the column. If it is negative,
2164 it means that the tags should be flushright to that column. For example,
2165 -80 works well for a normal 80 character screen."
2169 (defcustom org-auto-align-tags t
2170 "Non-nil means, realign tags after pro/demotion of TODO state change.
2171 These operations change the length of a headline and therefore shift
2172 the tags around. With this options turned on, after each such operation
2173 the tags are again aligned to `org-tags-column'."
2177 (defcustom org-use-tag-inheritance t
2178 "Non-nil means, tags in levels apply also for sublevels.
2179 When nil, only the tags directly given in a specific line apply there.
2180 This may also be a list of tags that should be inherited, or a regexp that
2181 matches tags that should be inherited. Additional control is possible
2182 with the variable `org-tags-exclude-from-inheritance' which gives an
2183 explicit list of tags to be excluded from inheritance., even if the value of
2184 `org-use-tag-inheritance' would select it for inheritance.
2186 If this option is t, a match early-on in a tree can lead to a large
2187 number of matches in the subtree when constructing the agenda or creating
2188 a sparse tree. If you only want to see the first match in a tree during
2189 a search, check out the variable `org-tags-match-list-sublevels'."
2192 (const :tag
"Not" nil
)
2193 (const :tag
"Always" t
)
2194 (repeat :tag
"Specific tags" (string :tag
"Tag"))
2195 (regexp :tag
"Tags matched by regexp")))
2197 (defcustom org-tags-exclude-from-inheritance nil
2198 "List of tags that should never be inherited.
2199 This is a way to exclude a few tags from inheritance. For way to do
2200 the opposite, to actively allow inheritance for selected tags,
2201 see the variable `org-use-tag-inheritance'."
2203 :type
'(repeat (string :tag
"Tag")))
2205 (defun org-tag-inherit-p (tag)
2206 "Check if TAG is one that should be inherited."
2208 ((member tag org-tags-exclude-from-inheritance
) nil
)
2209 ((eq org-use-tag-inheritance t
) t
)
2210 ((not org-use-tag-inheritance
) nil
)
2211 ((stringp org-use-tag-inheritance
)
2212 (string-match org-use-tag-inheritance tag
))
2213 ((listp org-use-tag-inheritance
)
2214 (member tag org-use-tag-inheritance
))
2215 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
2217 (defcustom org-tags-match-list-sublevels t
2218 "Non-nil means list also sublevels of headlines matching a search.
2219 This variable applies to tags/property searches, and also to stuck
2220 projects because this search is based on a tags match as well.
2222 Because of tag inheritance (see variable `org-use-tag-inheritance'),
2223 the sublevels of a headline matching a tag search often also match
2224 the same search. Listing all of them can create very long lists.
2225 Setting this variable to nil causes subtrees of a match to be skipped.
2227 As a special case, if the tag search is restricted to TODO items, the
2228 value of this variable is ignored and sublevels are always checked, to
2229 make sure all corresponding TODO items find their way into the list.
2231 This variable is semi-obsolete and probably should always be true. It
2232 is better to limit inheritance to certain tags using the variables
2233 `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
2236 (const :tag
"No, don't list them" nil
)
2237 (const :tag
"Yes, do list them" t
)
2238 (const :tag
"List them, indented with leading dots" indented
)))
2240 (defvar org-tags-history nil
2241 "History of minibuffer reads for tags.")
2242 (defvar org-last-tags-completion-table nil
2243 "The last used completion table for tags.")
2244 (defvar org-after-tags-change-hook nil
2245 "Hook that is run after the tags in a line have changed.")
2247 (defgroup org-properties nil
2248 "Options concerning properties in Org-mode."
2249 :tag
"Org Properties"
2252 (defcustom org-property-format
"%-10s %s"
2253 "How property key/value pairs should be formatted by `indent-line'.
2254 When `indent-line' hits a property definition, it will format the line
2255 according to this format, mainly to make sure that the values are
2256 lined-up with respect to each other."
2257 :group
'org-properties
2260 (defcustom org-use-property-inheritance nil
2261 "Non-nil means, properties apply also for sublevels.
2263 This setting is chiefly used during property searches. Turning it on can
2264 cause significant overhead when doing a search, which is why it is not
2267 When nil, only the properties directly given in the current entry count.
2268 When t, every property is inherited. The value may also be a list of
2269 properties that should have inheritance, or a regular expression matching
2270 properties that should be inherited.
2272 However, note that some special properties use inheritance under special
2273 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
2274 and the properties ending in \"_ALL\" when they are used as descriptor
2275 for valid values of a property.
2277 Note for programmers:
2278 When querying an entry with `org-entry-get', you can control if inheritance
2279 should be used. By default, `org-entry-get' looks only at the local
2280 properties. You can request inheritance by setting the inherit argument
2281 to t (to force inheritance) or to `selective' (to respect the setting
2283 :group
'org-properties
2285 (const :tag
"Not" nil
)
2286 (const :tag
"Always" t
)
2287 (repeat :tag
"Specific properties" (string :tag
"Property"))
2288 (regexp :tag
"Properties matched by regexp")))
2290 (defun org-property-inherit-p (property)
2291 "Check if PROPERTY is one that should be inherited."
2293 ((eq org-use-property-inheritance t
) t
)
2294 ((not org-use-property-inheritance
) nil
)
2295 ((stringp org-use-property-inheritance
)
2296 (string-match org-use-property-inheritance property
))
2297 ((listp org-use-property-inheritance
)
2298 (member property org-use-property-inheritance
))
2299 (t (error "Invalid setting of `org-use-property-inheritance'"))))
2301 (defcustom org-columns-default-format
"%25ITEM %TODO %3PRIORITY %TAGS"
2302 "The default column format, if no other format has been defined.
2303 This variable can be set on the per-file basis by inserting a line
2305 #+COLUMNS: %25ITEM ....."
2306 :group
'org-properties
2309 (defcustom org-columns-ellipses
".."
2310 "The ellipses to be used when a field in column view is truncated.
2311 When this is the empty string, as many characters as possible are shown,
2312 but then there will be no visual indication that the field has been truncated.
2313 When this is a string of length N, the last N characters of a truncated
2314 field are replaced by this string. If the column is narrower than the
2315 ellipses string, only part of the ellipses string will be shown."
2316 :group
'org-properties
2319 (defcustom org-columns-modify-value-for-display-function nil
2320 "Function that modifies values for display in column view.
2321 For example, it can be used to cut out a certain part from a time stamp.
2322 The function must take 2 arguments:
2324 column-title The title of the column (*not* the property name)
2325 value The value that should be modified.
2327 The function should return the value that should be displayed,
2328 or nil if the normal value should be used."
2329 :group
'org-properties
2332 (defcustom org-effort-property
"Effort"
2333 "The property that is being used to keep track of effort estimates.
2334 Effort estimates given in this property need to have the format H:MM."
2335 :group
'org-properties
2336 :group
'org-progress
2337 :type
'(string :tag
"Property"))
2339 (defconst org-global-properties-fixed
2340 '(("VISIBILITY_ALL" .
"folded children content all"))
2341 "List of property/value pairs that can be inherited by any entry.
2343 These are fixed values, for the preset properties. The user variable
2344 that can be used to add to this list is `org-global-properties'.
2346 The entries in this list are cons cells where the car is a property
2347 name and cdr is a string with the value. If the value represents
2348 multiple items like an \"_ALL\" property, separate the items by
2351 (defcustom org-global-properties nil
2352 "List of property/value pairs that can be inherited by any entry.
2354 This list will be combined with the constant `org-global-properties-fixed'.
2356 The entries in this list are cons cells where the car is a property
2357 name and cdr is a string with the value.
2359 You can set buffer-local values for the same purpose in the variable
2360 `org-file-properties' this by adding lines like
2362 #+PROPERTY: NAME VALUE"
2363 :group
'org-properties
2365 (cons (string :tag
"Property")
2366 (string :tag
"Value"))))
2368 (defvar org-file-properties nil
2369 "List of property/value pairs that can be inherited by any entry.
2370 Valid for the current buffer.
2371 This variable is populated from #+PROPERTY lines.")
2372 (make-variable-buffer-local 'org-file-properties
)
2374 (defgroup org-agenda nil
2375 "Options concerning agenda views in Org-mode."
2379 (defvar org-category nil
2380 "Variable used by org files to set a category for agenda display.
2381 Such files should use a file variable to set it, for example
2383 # -*- mode: org; org-category: \"ELisp\"
2385 or contain a special line
2389 If the file does not specify a category, then file's base name
2391 (make-variable-buffer-local 'org-category
)
2392 (put 'org-category
'safe-local-variable
'(lambda (x) (or (symbolp x
) (stringp x
))))
2394 (defcustom org-agenda-files nil
2395 "The files to be used for agenda display.
2396 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
2397 \\[org-remove-file]. You can also use customize to edit the list.
2399 If an entry is a directory, all files in that directory that are matched by
2400 `org-agenda-file-regexp' will be part of the file list.
2402 If the value of the variable is not a list but a single file name, then
2403 the list of agenda files is actually stored and maintained in that file, one
2404 agenda file per line."
2407 (repeat :tag
"List of files and directories" file
)
2408 (file :tag
"Store list in a file\n" :value
"~/.agenda_files")))
2410 (defcustom org-agenda-file-regexp
"\\`[^.].*\\.org\\'"
2411 "Regular expression to match files for `org-agenda-files'.
2412 If any element in the list in that variable contains a directory instead
2413 of a normal file, all files in that directory that are matched by this
2414 regular expression will be included."
2418 (defcustom org-agenda-text-search-extra-files nil
2419 "List of extra files to be searched by text search commands.
2420 These files will be search in addition to the agenda files by the
2421 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
2422 Note that these files will only be searched for text search commands,
2423 not for the other agenda views like todo lists, tag searches or the weekly
2424 agenda. This variable is intended to list notes and possibly archive files
2425 that should also be searched by these two commands.
2426 In fact, if the first element in the list is the symbol `agenda-archives',
2427 than all archive files of all agenda files will be added to the search
2430 :type
'(set :greedy t
2431 (const :tag
"Agenda Archives" agenda-archives
)
2432 (repeat :inline t
(file))))
2434 (if (fboundp 'defvaralias
)
2435 (defvaralias 'org-agenda-multi-occur-extra-files
2436 'org-agenda-text-search-extra-files
))
2438 (defcustom org-agenda-skip-unavailable-files nil
2439 "Non-nil means to just skip non-reachable files in `org-agenda-files'.
2440 A nil value means to remove them, after a query, from the list."
2444 (defcustom org-calendar-to-agenda-key
[?c
]
2445 "The key to be installed in `calendar-mode-map' for switching to the agenda.
2446 The command `org-calendar-goto-agenda' will be bound to this key. The
2447 default is the character `c' because then `c' can be used to switch back and
2448 forth between agenda and calendar."
2452 (defcustom org-calendar-agenda-action-key
[?k
]
2453 "The key to be installed in `calendar-mode-map' for agenda-action.
2454 The command `org-agenda-action' will be bound to this key. The
2455 default is the character `k' because we use the same key in the agenda."
2459 (eval-after-load "calendar"
2461 (org-defkey calendar-mode-map org-calendar-to-agenda-key
2462 'org-calendar-goto-agenda
)
2463 (org-defkey calendar-mode-map org-calendar-agenda-action-key
2464 'org-agenda-action
)))
2466 (defgroup org-latex nil
2467 "Options for embedding LaTeX code into Org-mode."
2471 (defcustom org-format-latex-options
2472 '(:foreground default
:background default
:scale
1.0
2473 :html-foreground
"Black" :html-background
"Transparent" :html-scale
1.0
2474 :matchers
("begin" "$1" "$" "$$" "\\(" "\\["))
2475 "Options for creating images from LaTeX fragments.
2476 This is a property list with the following properties:
2477 :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
2478 `default' means use the foreground of the default face.
2479 :background the background color, or \"Transparent\".
2480 `default' means use the background of the default face.
2481 :scale a scaling factor for the size of the images.
2482 :html-foreground, :html-background, :html-scale
2483 the same numbers for HTML export.
2484 :matchers a list indicating which matchers should be used to
2485 find LaTeX fragments. Valid members of this list are:
2486 \"begin\" find environments
2487 \"$1\" find single characters surrounded by $.$
2488 \"$\" find math expressions surrounded by $...$
2489 \"$$\" find math expressions surrounded by $$....$$
2490 \"\\(\" find math expressions surrounded by \\(...\\)
2491 \"\\ [\" find math expressions surrounded by \\ [...\\]"
2495 (defcustom org-format-latex-header
"\\documentclass{article}
2496 \\usepackage{fullpage} % do not remove
2497 \\usepackage{amssymb}
2498 \\usepackage[usenames]{color}
2499 \\usepackage{amsmath}
2500 \\usepackage{latexsym}
2501 \\usepackage[mathscr]{eucal}
2502 \\pagestyle{empty} % do not remove"
2503 "The document header used for processing LaTeX fragments."
2508 (defgroup org-font-lock nil
2509 "Font-lock settings for highlighting in Org-mode."
2510 :tag
"Org Font Lock"
2513 (defcustom org-level-color-stars-only nil
2514 "Non-nil means fontify only the stars in each headline.
2515 When nil, the entire headline is fontified.
2516 Changing it requires restart of `font-lock-mode' to become effective
2517 also in regions already fontified."
2518 :group
'org-font-lock
2521 (defcustom org-hide-leading-stars nil
2522 "Non-nil means, hide the first N-1 stars in a headline.
2523 This works by using the face `org-hide' for these stars. This
2524 face is white for a light background, and black for a dark
2525 background. You may have to customize the face `org-hide' to
2527 Changing it requires restart of `font-lock-mode' to become effective
2528 also in regions already fontified.
2529 You may also set this on a per-file basis by adding one of the following
2530 lines to the buffer:
2532 #+STARTUP: hidestars
2533 #+STARTUP: showstars"
2534 :group
'org-font-lock
2537 (defcustom org-fontify-done-headline nil
2538 "Non-nil means, change the face of a headline if it is marked DONE.
2539 Normally, only the TODO/DONE keyword indicates the state of a headline.
2540 When this is non-nil, the headline after the keyword is set to the
2541 `org-headline-done' as an additional indication."
2542 :group
'org-font-lock
2545 (defcustom org-fontify-emphasized-text t
2546 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
2547 Changing this variable requires a restart of Emacs to take effect."
2548 :group
'org-font-lock
2551 (defcustom org-highlight-latex-fragments-and-specials nil
2552 "Non-nil means, fontify what is treated specially by the exporters."
2553 :group
'org-font-lock
2556 (defcustom org-hide-emphasis-markers nil
2557 "Non-nil mean font-lock should hide the emphasis marker characters."
2558 :group
'org-font-lock
2561 (defvar org-emph-re nil
2562 "Regular expression for matching emphasis.")
2563 (defvar org-verbatim-re nil
2564 "Regular expression for matching verbatim text.")
2565 (defvar org-emphasis-regexp-components
) ; defined just below
2566 (defvar org-emphasis-alist
) ; defined just below
2567 (defun org-set-emph-re (var val
)
2568 "Set variable and compute the emphasis regular expression."
2570 (when (and (boundp 'org-emphasis-alist
)
2571 (boundp 'org-emphasis-regexp-components
)
2572 org-emphasis-alist org-emphasis-regexp-components
)
2573 (let* ((e org-emphasis-regexp-components
)
2579 (body1 (concat body
"*?"))
2580 (markers (mapconcat 'car org-emphasis-alist
""))
2581 (vmarkers (mapconcat
2582 (lambda (x) (if (eq (nth 4 x
) 'verbatim
) (car x
) ""))
2583 org-emphasis-alist
"")))
2584 ;; make sure special characters appear at the right position in the class
2585 (if (string-match "\\^" markers
)
2586 (setq markers
(concat (replace-match "" t t markers
) "^")))
2587 (if (string-match "-" markers
)
2588 (setq markers
(concat (replace-match "" t t markers
) "-")))
2589 (if (string-match "\\^" vmarkers
)
2590 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "^")))
2591 (if (string-match "-" vmarkers
)
2592 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "-")))
2594 (setq body1
(concat body1
"\\(?:\n" body
"*?\\)\\{0,"
2595 (int-to-string nl
) "\\}")))
2598 (concat "\\([" pre
"]\\|^\\)"
2600 "\\([" markers
"]\\)"
2608 "\\([" post
"]\\|$\\)"))
2609 (setq org-verbatim-re
2610 (concat "\\([" pre
"]\\|^\\)"
2612 "\\([" vmarkers
"]\\)"
2620 "\\([" post
"]\\|$\\)")))))
2622 (defcustom org-emphasis-regexp-components
2623 '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
2624 "Components used to build the regular expression for emphasis.
2625 This is a list with 6 entries. Terminology: In an emphasis string
2626 like \" *strong word* \", we call the initial space PREMATCH, the final
2627 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
2628 and \"trong wor\" is the body. The different components in this variable
2629 specify what is allowed/forbidden in each part:
2631 pre Chars allowed as prematch. Beginning of line will be allowed too.
2632 post Chars allowed as postmatch. End of line will be allowed too.
2633 border The chars *forbidden* as border characters.
2634 body-regexp A regexp like \".\" to match a body character. Don't use
2635 non-shy groups here, and don't allow newline here.
2636 newline The maximum number of newlines allowed in an emphasis exp.
2638 Use customize to modify this, or restart Emacs after changing it."
2639 :group
'org-font-lock
2640 :set
'org-set-emph-re
2642 (sexp :tag
"Allowed chars in pre ")
2643 (sexp :tag
"Allowed chars in post ")
2644 (sexp :tag
"Forbidden chars in border ")
2645 (sexp :tag
"Regexp for body ")
2646 (integer :tag
"number of newlines allowed")
2647 (option (boolean :tag
"Please ignore this button"))))
2649 (defcustom org-emphasis-alist
2650 `(("*" bold
"<b>" "</b>")
2651 ("/" italic
"<i>" "</i>")
2652 ("_" underline
"<span style=\"text-decoration:underline;\">" "</span>")
2653 ("=" org-code
"<code>" "</code>" verbatim
)
2654 ("~" org-verbatim
"<code>" "</code>" verbatim
)
2655 ("+" ,(if (featurep 'xemacs
) 'org-table
'(:strike-through t
))
2658 "Special syntax for emphasized text.
2659 Text starting and ending with a special character will be emphasized, for
2660 example *bold*, _underlined_ and /italic/. This variable sets the marker
2661 characters, the face to be used by font-lock for highlighting in Org-mode
2662 Emacs buffers, and the HTML tags to be used for this.
2663 For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
2664 Use customize to modify this, or restart Emacs after changing it."
2665 :group
'org-font-lock
2666 :set
'org-set-emph-re
2669 (string :tag
"Marker character")
2671 (face :tag
"Font-lock-face")
2672 (plist :tag
"Face property list"))
2673 (string :tag
"HTML start tag")
2674 (string :tag
"HTML end tag")
2675 (option (const verbatim
)))))
2677 ;;; Miscellaneous options
2679 (defgroup org-completion nil
2680 "Completion in Org-mode."
2681 :tag
"Org Completion"
2684 (defcustom org-completion-use-ido nil
2685 "Non-nil means, use ido completion wherever possible.
2686 Note that `ido-mode' must be active for this variable to be relevant.
2687 If you decide to turn this variable on, you might well want to turn off
2688 `org-outline-path-complete-in-steps'."
2689 :group
'org-completion
2692 (defcustom org-completion-fallback-command
'hippie-expand
2693 "The expansion command called by \\[org-complete] in normal context.
2694 Normal means, no org-mode-specific context."
2695 :group
'org-completion
2698 ;;; Functions and variables from ther packages
2699 ;; Declared here to avoid compiler warnings
2702 (defvar outline-mode-menu-heading
)
2703 (defvar outline-mode-menu-show
)
2704 (defvar outline-mode-menu-hide
)
2705 (defvar zmacs-regions
) ; XEmacs regions
2708 (defvar mark-active
)
2711 (declare-function find-library-name
"find-func" (library))
2712 (declare-function calendar-absolute-from-iso
"cal-iso" (date))
2713 (declare-function calendar-forward-day
"cal-move" (arg))
2714 (declare-function calendar-goto-date
"cal-move" (date))
2715 (declare-function calendar-goto-today
"cal-move" ())
2716 (declare-function calendar-iso-from-absolute
"cal-iso" (date))
2717 (defvar calc-embedded-close-formula
)
2718 (defvar calc-embedded-open-formula
)
2719 (declare-function cdlatex-tab
"ext:cdlatex" ())
2720 (declare-function dired-get-filename
"dired" (&optional localp no-error-if-not-filep
))
2721 (defvar font-lock-unfontify-region-function
)
2722 (declare-function iswitchb-mode
"iswitchb" (&optional arg
))
2723 (declare-function iswitchb-read-buffer
(prompt &optional default require-match start matches-set
))
2724 (defvar iswitchb-temp-buflist
)
2725 (declare-function org-gnus-follow-link
"org-gnus" (&optional group article
))
2726 (defvar org-agenda-tags-todo-honor-ignore-options
)
2727 (declare-function org-agenda-skip
"org-agenda" ())
2728 (declare-function org-format-agenda-item
"org-agenda"
2729 (extra txt
&optional category tags dotime noprefix remove-re
))
2730 (declare-function org-agenda-new-marker
"org-agenda" (&optional pos
))
2731 (declare-function org-agenda-change-all-lines
"org-agenda"
2732 (newhead hdmarker
&optional fixface just-this
))
2733 (declare-function org-agenda-set-restriction-lock
"org-agenda" (&optional type
))
2734 (declare-function org-agenda-maybe-redo
"org-agenda" ())
2735 (declare-function org-agenda-save-markers-for-cut-and-paste
"org-agenda"
2737 (declare-function org-agenda-copy-local-variable
"org-agenda" (var))
2738 (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
2739 "org-agenda" (&optional end
))
2741 (declare-function parse-time-string
"parse-time" (string))
2742 (declare-function remember
"remember" (&optional initial
))
2743 (declare-function remember-buffer-desc
"remember" ())
2744 (declare-function remember-finalize
"remember" ())
2745 (defvar remember-save-after-remembering
)
2746 (defvar remember-data-file
)
2747 (defvar remember-register
)
2748 (defvar remember-buffer
)
2749 (defvar remember-handler-functions
)
2750 (defvar remember-annotation-functions
)
2751 (defvar texmathp-why
)
2752 (declare-function speedbar-line-directory
"speedbar" (&optional depth
))
2753 (declare-function table--at-cell-p
"table" (position &optional object at-column
))
2755 (defvar w3m-current-url
)
2756 (defvar w3m-current-title
)
2758 (defvar org-latex-regexps
)
2760 ;;; Autoload and prepare some org modules
2762 ;; Some table stuff that needs to be defined here, because it is used
2763 ;; by the functions setting up org-mode or checking for table context.
2765 (defconst org-table-any-line-regexp
"^[ \t]*\\(|\\|\\+-[-+]\\)"
2766 "Detects an org-type or table-type table.")
2767 (defconst org-table-line-regexp
"^[ \t]*|"
2768 "Detects an org-type table line.")
2769 (defconst org-table-dataline-regexp
"^[ \t]*|[^-]"
2770 "Detects an org-type table line.")
2771 (defconst org-table-hline-regexp
"^[ \t]*|-"
2772 "Detects an org-type table hline.")
2773 (defconst org-table1-hline-regexp
"^[ \t]*\\+-[-+]"
2774 "Detects a table-type table hline.")
2775 (defconst org-table-any-border-regexp
"^[ \t]*[^|+ \t]"
2776 "Searching from within a table (any type) this finds the first line
2777 outside the table.")
2779 ;; Autoload the functions in org-table.el that are needed by functions here.
2782 (org-autoload "org-table"
2783 '(org-table-align org-table-begin org-table-blank-field
2784 org-table-convert org-table-convert-region org-table-copy-down
2785 org-table-copy-region org-table-create
2786 org-table-create-or-convert-from-region
2787 org-table-create-with-table.el org-table-current-dline
2788 org-table-cut-region org-table-delete-column org-table-edit-field
2789 org-table-edit-formulas org-table-end org-table-eval-formula
2790 org-table-export org-table-field-info
2791 org-table-get-stored-formulas org-table-goto-column
2792 org-table-hline-and-move org-table-import org-table-insert-column
2793 org-table-insert-hline org-table-insert-row org-table-iterate
2794 org-table-justify-field-maybe org-table-kill-row
2795 org-table-maybe-eval-formula org-table-maybe-recalculate-line
2796 org-table-move-column org-table-move-column-left
2797 org-table-move-column-right org-table-move-row
2798 org-table-move-row-down org-table-move-row-up
2799 org-table-next-field org-table-next-row org-table-paste-rectangle
2800 org-table-previous-field org-table-recalculate
2801 org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
2802 org-table-toggle-coordinate-overlays
2803 org-table-toggle-formula-debugger org-table-wrap-region
2804 orgtbl-mode turn-on-orgtbl org-table-to-lisp
)))
2806 (defun org-at-table-p (&optional table-type
)
2807 "Return t if the cursor is inside an org-type table.
2808 If TABLE-TYPE is non-nil, also check for table.el-type tables."
2809 (if org-enable-table-editor
2811 (beginning-of-line 1)
2812 (looking-at (if table-type org-table-any-line-regexp
2813 org-table-line-regexp
)))
2815 (defsubst org-table-p
() (org-at-table-p))
2817 (defun org-at-table.el-p
()
2818 "Return t if and only if we are at a table.el table."
2819 (and (org-at-table-p 'any
)
2821 (goto-char (org-table-begin 'any
))
2822 (looking-at org-table1-hline-regexp
))))
2823 (defun org-table-recognize-table.el
()
2824 "If there is a table.el table nearby, recognize it and move into it."
2825 (if org-table-tab-recognizes-table.el
2826 (if (org-at-table.el-p
)
2828 (beginning-of-line 1)
2829 (if (looking-at org-table-dataline-regexp
)
2831 (if (looking-at org-table1-hline-regexp
)
2833 (beginning-of-line 2)
2834 (if (looking-at org-table-any-border-regexp
)
2835 (beginning-of-line -
1)))))
2836 (if (re-search-forward "|" (org-table-end t
) t
)
2839 (if (table--at-cell-p (point))
2841 (message "recognizing table.el table...")
2842 (table-recognize-table)
2843 (message "recognizing table.el table...done")))
2844 (error "This should not happen..."))
2849 (defun org-at-table-hline-p ()
2850 "Return t if the cursor is inside a hline in a table."
2851 (if org-enable-table-editor
2853 (beginning-of-line 1)
2854 (looking-at org-table-hline-regexp
))
2857 (defvar org-table-clean-did-remove-column nil
)
2859 (defun org-table-map-tables (function)
2860 "Apply FUNCTION to the start of all tables in the buffer."
2864 (goto-char (point-min))
2865 (while (re-search-forward org-table-any-line-regexp nil t
)
2866 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
2867 (beginning-of-line 1)
2868 (if (looking-at org-table-line-regexp
)
2869 (save-excursion (funcall function
)))
2870 (re-search-forward org-table-any-border-regexp nil
1))))
2871 (message "Mapping tables: done"))
2873 ;; Declare and autoload functions from org-exp.el
2875 (declare-function org-default-export-plist
"org-exp")
2876 (declare-function org-infile-export-plist
"org-exp")
2877 (declare-function org-get-current-options
"org-exp")
2879 (org-autoload "org-exp"
2880 '(org-export org-export-as-ascii org-export-visible
2881 org-insert-export-options-template org-export-as-html-and-open
2882 org-export-as-html-batch org-export-as-html-to-buffer
2883 org-replace-region-by-html org-export-region-as-html
2884 org-export-as-html org-export-icalendar-this-file
2885 org-export-icalendar-all-agenda-files
2886 org-table-clean-before-export
2887 org-export-icalendar-combine-agenda-files org-export-as-xoxo
)))
2889 ;; Declare and autoload functions from org-agenda.el
2892 (org-autoload "org-agenda"
2893 '(org-agenda org-agenda-list org-search-view
2894 org-todo-list org-tags-view org-agenda-list-stuck-projects
2895 org-diary org-agenda-to-appt
2896 org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
)))
2898 ;; Autoload org-remember
2901 (org-autoload "org-remember"
2902 '(org-remember-insinuate org-remember-annotation
2903 org-remember-apply-template org-remember org-remember-handler
)))
2905 ;; Autoload org-clock.el
2908 (declare-function org-clock-save-markers-for-cut-and-paste
"org-clock"
2910 (declare-function org-clock-update-mode-line
"org-clock" ())
2911 (defvar org-clock-start-time
)
2912 (defvar org-clock-marker
(make-marker)
2913 "Marker recording the last clock-in.")
2918 '(org-clock-in org-clock-out org-clock-cancel
2919 org-clock-goto org-clock-sum org-clock-display
2920 org-clock-remove-overlays org-clock-report
2921 org-clocktable-shift org-dblock-write
:clocktable
2922 org-get-clocktable
)))
2924 (defun org-clock-update-time-maybe ()
2925 "If this is a CLOCK line, update it and return t.
2926 Otherwise, return nil."
2929 (beginning-of-line 1)
2930 (skip-chars-forward " \t")
2931 (when (looking-at org-clock-string
)
2932 (let ((re (concat "[ \t]*" org-clock-string
2933 " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
2934 "\\([ \t]*=>.*\\)?\\)?"))
2937 ((not (looking-at re
))
2939 ((not (match-end 2))
2940 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
2941 (> org-clock-marker
(point))
2942 (<= org-clock-marker
(point-at-eol)))
2943 ;; The clock is running here
2944 (setq org-clock-start-time
2946 (org-parse-time-string (match-string 1))))
2947 (org-clock-update-mode-line)))
2949 (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
2951 (setq ts
(match-string 1)
2952 te
(match-string 3))
2953 (setq s
(- (time-to-seconds
2954 (apply 'encode-time
(org-parse-time-string te
)))
2956 (apply 'encode-time
(org-parse-time-string ts
))))
2959 h
(floor (/ s
3600))
2963 (insert " => " (format (if neg
"-%d:%02d" "%2d:%02d") h m
))
2966 (defun org-check-running-clock ()
2967 "Check if the current buffer contains the running clock.
2968 If yes, offer to stop it and to save the buffer with the changes."
2969 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
2970 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
2973 (when (y-or-n-p "Save changed buffer?")
2976 (defun org-clocktable-try-shift (dir n
)
2977 "Check if this line starts a clock table, if yes, shift the time block."
2978 (when (org-match-line "#\\+BEGIN: clocktable\\>")
2979 (org-clocktable-shift dir n
)))
2981 ;; Autoload org-timer.el
2983 ;(declare-function org-timer "org-timer")
2988 '(org-timer-start org-timer org-timer-item
2989 org-timer-change-times-in-region
)))
2992 ;; Autoload archiving code
2993 ;; The stuff that is needed for cycling and tags has to be defined here.
2995 (defgroup org-archive nil
2996 "Options concerning archiving in Org-mode."
2998 :group
'org-structure
)
3000 (defcustom org-archive-location
"%s_archive::"
3001 "The location where subtrees should be archived.
3003 The value of this variable is a string, consisting of two parts,
3004 separated by a double-colon. The first part is a filename and
3005 the second part is a headline.
3007 When the filename is omitted, archiving happens in the same file.
3008 %s in the filename will be replaced by the current file
3009 name (without the directory part). Archiving to a different file
3010 is useful to keep archived entries from contributing to the
3013 The archived entries will be filed as subtrees of the specified
3014 headline. When the headline is omitted, the subtrees are simply
3015 filed away at the end of the file, as top-level entries. Also in
3016 the heading you can use %s to represent the file name, this can be
3017 useful when using the same archive for a number of different files.
3019 Here are a few examples:
3021 If the current file is Projects.org, archive in file
3022 Projects.org_archive, as top-level trees. This is the default.
3024 \"::* Archived Tasks\"
3025 Archive in the current file, under the top-level headline
3026 \"* Archived Tasks\".
3028 \"~/org/archive.org::\"
3029 Archive in file ~/org/archive.org (absolute path), as top-level trees.
3031 \"~/org/archive.org::From %s\"
3032 Archive in file ~/org/archive.org (absolute path), und headlines
3033 \"From FILENAME\" where file name is the current file name.
3035 \"basement::** Finished Tasks\"
3036 Archive in file ./basement (relative path), as level 3 trees
3037 below the level 2 heading \"** Finished Tasks\".
3039 You may set this option on a per-file basis by adding to the buffer a
3042 #+ARCHIVE: basement::** Finished Tasks
3044 You may also define it locally for a subtree by setting an ARCHIVE property
3045 in the entry. If such a property is found in an entry, or anywhere up
3046 the hierarchy, it will be used."
3050 (defcustom org-archive-tag
"ARCHIVE"
3051 "The tag that marks a subtree as archived.
3052 An archived subtree does not open during visibility cycling, and does
3053 not contribute to the agenda listings.
3054 After changing this, font-lock must be restarted in the relevant buffers to
3055 get the proper fontification."
3057 :group
'org-keywords
3060 (defcustom org-agenda-skip-archived-trees t
3061 "Non-nil means, the agenda will skip any items located in archived trees.
3062 An archived tree is a tree marked with the tag ARCHIVE. The use of this
3063 variable is no longer recommended, you should leave it at the value t.
3064 Instead, use the key `v' to cycle the archives-mode in the agenda."
3066 :group
'org-agenda-skip
3069 (defcustom org-cycle-open-archived-trees nil
3070 "Non-nil means, `org-cycle' will open archived trees.
3071 An archived tree is a tree marked with the tag ARCHIVE.
3072 When nil, archived trees will stay folded. You can still open them with
3073 normal outline commands like `show-all', but not with the cycling commands."
3078 (defcustom org-sparse-tree-open-archived-trees nil
3079 "Non-nil means sparse tree construction shows matches in archived trees.
3080 When nil, matches in these trees are highlighted, but the trees are kept in
3083 :group
'org-sparse-trees
3086 (defun org-cycle-hide-archived-subtrees (state)
3087 "Re-hide all archived subtrees after a visibility state change."
3088 (when (and (not org-cycle-open-archived-trees
)
3089 (not (memq state
'(overview folded
))))
3091 (let* ((globalp (memq state
'(contents all
)))
3092 (beg (if globalp
(point-min) (point)))
3093 (end (if globalp
(point-max) (org-end-of-subtree t
))))
3094 (org-hide-archived-subtrees beg end
)
3096 (if (looking-at (concat ".*:" org-archive-tag
":"))
3097 (message "%s" (substitute-command-keys
3098 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
3100 (defun org-force-cycle-archived ()
3101 "Cycle subtree even if it is archived."
3103 (setq this-command
'org-cycle
)
3104 (let ((org-cycle-open-archived-trees t
))
3105 (call-interactively 'org-cycle
)))
3107 (defun org-hide-archived-subtrees (beg end
)
3108 "Re-hide all archived subtrees after a visibility state change."
3110 (let* ((re (concat ":" org-archive-tag
":")))
3112 (while (re-search-forward re end t
)
3113 (and (org-on-heading-p) (hide-subtree))
3114 (org-end-of-subtree t
)))))
3116 (defalias 'org-advertized-archive-subtree
'org-archive-subtree
)
3119 (org-autoload "org-archive"
3120 '(org-add-archive-files org-archive-subtree
3121 org-archive-to-archive-sibling org-toggle-archive-tag
)))
3123 ;; Autoload Column View Code
3125 (declare-function org-columns-number-to-string
"org-colview")
3126 (declare-function org-columns-get-format-and-top-level
"org-colview")
3127 (declare-function org-columns-compute
"org-colview")
3129 (org-autoload (if (featurep 'xemacs
) "org-colview-xemacs" "org-colview")
3130 '(org-columns-number-to-string org-columns-get-format-and-top-level
3131 org-columns-compute org-agenda-columns org-columns-remove-overlays
3132 org-columns org-insert-columns-dblock org-dblock-write
:columnview
))
3136 (declare-function org-id-store-link
"org-id")
3137 (declare-function org-id-locations-load
"org-id")
3138 (declare-function org-id-locations-save
"org-id")
3139 (defvar org-id-track-globally
)
3140 (org-autoload "org-id"
3141 '(org-id-get-create org-id-new org-id-copy org-id-get
3142 org-id-get-with-outline-path-completion
3143 org-id-get-with-outline-drilling
3144 org-id-goto org-id-find org-id-store-link
))
3146 ;; Autoload Plotting Code
3148 (org-autoload "org-plot"
3149 '(org-plot/gnuplot
))
3151 ;;; Variables for pre-computed regular expressions, all buffer local
3153 (defvar org-drawer-regexp nil
3154 "Matches first line of a hidden block.")
3155 (make-variable-buffer-local 'org-drawer-regexp
)
3156 (defvar org-todo-regexp nil
3157 "Matches any of the TODO state keywords.")
3158 (make-variable-buffer-local 'org-todo-regexp
)
3159 (defvar org-not-done-regexp nil
3160 "Matches any of the TODO state keywords except the last one.")
3161 (make-variable-buffer-local 'org-not-done-regexp
)
3162 (defvar org-todo-line-regexp nil
3163 "Matches a headline and puts TODO state into group 2 if present.")
3164 (make-variable-buffer-local 'org-todo-line-regexp
)
3165 (defvar org-complex-heading-regexp nil
3166 "Matches a headline and puts everything into groups:
3168 group 2: The todo keyword, maybe
3169 group 3: Priority cookie
3170 group 4: True headline
3172 (make-variable-buffer-local 'org-complex-heading-regexp
)
3173 (defvar org-todo-line-tags-regexp nil
3174 "Matches a headline and puts TODO state into group 2 if present.
3175 Also put tags into group 4 if tags are present.")
3176 (make-variable-buffer-local 'org-todo-line-tags-regexp
)
3177 (defvar org-nl-done-regexp nil
3178 "Matches newline followed by a headline with the DONE keyword.")
3179 (make-variable-buffer-local 'org-nl-done-regexp
)
3180 (defvar org-looking-at-done-regexp nil
3181 "Matches the DONE keyword a point.")
3182 (make-variable-buffer-local 'org-looking-at-done-regexp
)
3183 (defvar org-ds-keyword-length
12
3184 "Maximum length of the Deadline and SCHEDULED keywords.")
3185 (make-variable-buffer-local 'org-ds-keyword-length
)
3186 (defvar org-deadline-regexp nil
3187 "Matches the DEADLINE keyword.")
3188 (make-variable-buffer-local 'org-deadline-regexp
)
3189 (defvar org-deadline-time-regexp nil
3190 "Matches the DEADLINE keyword together with a time stamp.")
3191 (make-variable-buffer-local 'org-deadline-time-regexp
)
3192 (defvar org-deadline-line-regexp nil
3193 "Matches the DEADLINE keyword and the rest of the line.")
3194 (make-variable-buffer-local 'org-deadline-line-regexp
)
3195 (defvar org-scheduled-regexp nil
3196 "Matches the SCHEDULED keyword.")
3197 (make-variable-buffer-local 'org-scheduled-regexp
)
3198 (defvar org-scheduled-time-regexp nil
3199 "Matches the SCHEDULED keyword together with a time stamp.")
3200 (make-variable-buffer-local 'org-scheduled-time-regexp
)
3201 (defvar org-closed-time-regexp nil
3202 "Matches the CLOSED keyword together with a time stamp.")
3203 (make-variable-buffer-local 'org-closed-time-regexp
)
3205 (defvar org-keyword-time-regexp nil
3206 "Matches any of the 4 keywords, together with the time stamp.")
3207 (make-variable-buffer-local 'org-keyword-time-regexp
)
3208 (defvar org-keyword-time-not-clock-regexp nil
3209 "Matches any of the 3 keywords, together with the time stamp.")
3210 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp
)
3211 (defvar org-maybe-keyword-time-regexp nil
3212 "Matches a timestamp, possibly preceeded by a keyword.")
3213 (make-variable-buffer-local 'org-maybe-keyword-time-regexp
)
3214 (defvar org-planning-or-clock-line-re nil
3215 "Matches a line with planning or clock info.")
3216 (make-variable-buffer-local 'org-planning-or-clock-line-re
)
3218 (defconst org-plain-time-of-day-regexp
3221 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
3224 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
3226 "Regular expression to match a plain time or time range.
3227 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
3228 groups carry important information:
3230 1 the first time, range or not
3231 8 the second time, if it is a range.")
3233 (defconst org-plain-time-extension-regexp
3236 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
3237 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
3238 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
3239 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
3240 groups carry important information:
3243 9 minutes of duration")
3245 (defconst org-stamp-time-of-day-regexp
3247 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
3248 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
3250 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
3251 "Regular expression to match a timestamp time or time range.
3252 After a match, the following groups carry important information:
3254 1 date plus weekday, for backreferencing to make sure both times on same day
3255 2 the first time, range or not
3256 4 the second time, if it is a range.")
3258 (defconst org-startup-options
3259 '(("fold" org-startup-folded t
)
3260 ("overview" org-startup-folded t
)
3261 ("nofold" org-startup-folded nil
)
3262 ("showall" org-startup-folded nil
)
3263 ("content" org-startup-folded content
)
3264 ("hidestars" org-hide-leading-stars t
)
3265 ("showstars" org-hide-leading-stars nil
)
3266 ("odd" org-odd-levels-only t
)
3267 ("oddeven" org-odd-levels-only nil
)
3268 ("align" org-startup-align-all-tables t
)
3269 ("noalign" org-startup-align-all-tables nil
)
3270 ("customtime" org-display-custom-times t
)
3271 ("logdone" org-log-done time
)
3272 ("lognotedone" org-log-done note
)
3273 ("nologdone" org-log-done nil
)
3274 ("lognoteclock-out" org-log-note-clock-out t
)
3275 ("nolognoteclock-out" org-log-note-clock-out nil
)
3276 ("logrepeat" org-log-repeat state
)
3277 ("lognoterepeat" org-log-repeat note
)
3278 ("nologrepeat" org-log-repeat nil
)
3279 ("fninline" org-footnote-define-inline t
)
3280 ("nofninline" org-footnote-define-inline nil
)
3281 ("fnlocal" org-footnote-section nil
)
3282 ("fnauto" org-footnote-auto-label t
)
3283 ("fnprompt" org-footnote-auto-label nil
)
3284 ("fnconfirm" org-footnote-auto-label confirm
)
3285 ("fnplain" org-footnote-auto-label plain
)
3286 ("constcgs" constants-unit-system cgs
)
3287 ("constSI" constants-unit-system SI
)
3288 ("noptag" org-tag-persistent-alist nil
))
3289 "Variable associated with STARTUP options for org-mode.
3290 Each element is a list of three items: The startup options as written
3291 in the #+STARTUP line, the corresponding variable, and the value to
3292 set this variable to if the option is found. An optional forth element PUSH
3293 means to push this value onto the list in the variable.")
3295 (defun org-set-regexps-and-options ()
3296 "Precompute regular expressions for current buffer."
3298 (org-set-local 'org-todo-kwd-alist nil
)
3299 (org-set-local 'org-todo-key-alist nil
)
3300 (org-set-local 'org-todo-key-trigger nil
)
3301 (org-set-local 'org-todo-keywords-1 nil
)
3302 (org-set-local 'org-done-keywords nil
)
3303 (org-set-local 'org-todo-heads nil
)
3304 (org-set-local 'org-todo-sets nil
)
3305 (org-set-local 'org-todo-log-states nil
)
3306 (org-set-local 'org-file-properties nil
)
3307 (org-set-local 'org-file-tags nil
)
3308 (let ((re (org-make-options-regexp
3309 '("CATEGORY" "TODO" "COLUMNS"
3310 "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
3311 "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
3312 "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
3314 kwds kws0 kwsa key log value cat arch tags const links hw dws
3315 tail sep kws1 prio props ftags drawers
3316 ext-setup-or-nil setup-contents
(start 0))
3320 (goto-char (point-min))
3321 (while (or (and ext-setup-or-nil
3322 (string-match re ext-setup-or-nil start
)
3323 (setq start
(match-end 0)))
3324 (and (setq ext-setup-or-nil nil start
0)
3325 (re-search-forward re nil t
)))
3326 (setq key
(upcase (match-string 1 ext-setup-or-nil
))
3327 value
(org-match-string-no-properties 2 ext-setup-or-nil
))
3329 ((equal key
"CATEGORY")
3330 (if (string-match "[ \t]+$" value
)
3331 (setq value
(replace-match "" t t value
)))
3333 ((member key
'("SEQ_TODO" "TODO"))
3334 (push (cons 'sequence
(org-split-string value splitre
)) kwds
))
3335 ((equal key
"TYP_TODO")
3336 (push (cons 'type
(org-split-string value splitre
)) kwds
))
3337 ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key
)
3338 ;; general TODO-like setup
3339 (push (cons (intern (downcase (match-string 1 key
)))
3340 (org-split-string value splitre
)) kwds
))
3342 (setq tags
(append tags
(if tags
'("\\n") nil
)
3343 (org-split-string value splitre
))))
3344 ((equal key
"COLUMNS")
3345 (org-set-local 'org-columns-default-format value
))
3347 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value
)
3348 (push (cons (match-string 1 value
)
3349 (org-trim (match-string 2 value
)))
3351 ((equal key
"PRIORITIES")
3352 (setq prio
(org-split-string value
" +")))
3353 ((equal key
"PROPERTY")
3354 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
3355 (push (cons (match-string 1 value
) (match-string 2 value
))
3357 ((equal key
"FILETAGS")
3358 (when (string-match "\\S-" value
)
3363 (mapcar (lambda (x) (org-split-string x
":"))
3364 (org-split-string value
)))))))
3365 ((equal key
"DRAWERS")
3366 (setq drawers
(org-split-string value splitre
)))
3367 ((equal key
"CONSTANTS")
3368 (setq const
(append const
(org-split-string value splitre
))))
3369 ((equal key
"STARTUP")
3370 (let ((opts (org-split-string value splitre
))
3372 (while (setq l
(pop opts
))
3373 (when (setq l
(assoc l org-startup-options
))
3374 (setq var
(nth 1 l
) val
(nth 2 l
))
3376 (set (make-local-variable var
) val
)
3377 (if (not (listp (symbol-value var
)))
3378 (set (make-local-variable var
) nil
))
3379 (set (make-local-variable var
) (symbol-value var
))
3380 (add-to-list var val
))))))
3381 ((equal key
"ARCHIVE")
3382 (string-match " *$" value
)
3383 (setq arch
(replace-match "" t t value
))
3384 (remove-text-properties 0 (length arch
)
3385 '(face t fontified t
) arch
))
3386 ((equal key
"SETUPFILE")
3387 (setq setup-contents
(org-file-contents
3389 (org-remove-double-quotes value
))
3391 (if (not ext-setup-or-nil
)
3392 (setq ext-setup-or-nil setup-contents start
0)
3393 (setq ext-setup-or-nil
3394 (concat (substring ext-setup-or-nil
0 start
)
3395 "\n" setup-contents
"\n"
3396 (substring ext-setup-or-nil start
)))))
3399 (org-set-local 'org-category
(intern cat
))
3400 (push (cons "CATEGORY" cat
) props
))
3402 (if (< (length prio
) 3) (setq prio
'("A" "C" "B")))
3403 (setq prio
(mapcar 'string-to-char prio
))
3404 (org-set-local 'org-highest-priority
(nth 0 prio
))
3405 (org-set-local 'org-lowest-priority
(nth 1 prio
))
3406 (org-set-local 'org-default-priority
(nth 2 prio
)))
3407 (and props
(org-set-local 'org-file-properties
(nreverse props
)))
3408 (and ftags
(org-set-local 'org-file-tags ftags
))
3409 (and drawers
(org-set-local 'org-drawers drawers
))
3410 (and arch
(org-set-local 'org-archive-location arch
))
3411 (and links
(setq org-link-abbrev-alist-local
(nreverse links
)))
3412 ;; Process the TODO keywords
3414 ;; Use the global values as if they had been given locally.
3415 (setq kwds
(default-value 'org-todo-keywords
))
3416 (if (stringp (car kwds
))
3417 (setq kwds
(list (cons org-todo-interpretation
3418 (default-value 'org-todo-keywords
)))))
3419 (setq kwds
(reverse kwds
)))
3420 (setq kwds
(nreverse kwds
))
3422 (while (setq kws
(pop kwds
))
3424 (run-hook-with-args-until-success
3425 'org-todo-setup-filter-hook kws
)
3427 (setq inter
(pop kws
) sep
(member "|" kws
)
3428 kws0
(delete "|" (copy-sequence kws
))
3433 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x
)
3435 (setq kw
(match-string 1 x
)
3436 key
(and (match-end 2) (match-string 2 x
))
3437 log
(org-extract-log-state-settings x
))
3438 (push (cons kw
(and key
(string-to-char key
))) kwsa
)
3439 (and log
(push log org-todo-log-states
))
3441 (error "Invalid TODO keyword %s" x
)))
3443 kwsa
(if kwsa
(append '((:startgroup
))
3447 dws
(if sep
(org-remove-keyword-keys (cdr sep
)) (last kws1
))
3448 tail
(list inter hw
(car dws
) (org-last dws
))))
3449 (add-to-list 'org-todo-heads hw
'append
)
3450 (push kws1 org-todo-sets
)
3451 (setq org-done-keywords
(append org-done-keywords dws nil
))
3452 (setq org-todo-key-alist
(append org-todo-key-alist kwsa
))
3453 (mapc (lambda (x) (push (cons x tail
) org-todo-kwd-alist
)) kws1
)
3454 (setq org-todo-keywords-1
(append org-todo-keywords-1 kws1 nil
)))
3455 (setq org-todo-sets
(nreverse org-todo-sets
)
3456 org-todo-kwd-alist
(nreverse org-todo-kwd-alist
)
3457 org-todo-key-trigger
(delq nil
(mapcar 'cdr org-todo-key-alist
))
3458 org-todo-key-alist
(org-assign-fast-keys org-todo-key-alist
)))
3459 ;; Process the constants
3462 (while (setq e
(pop const
))
3463 (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e
)
3464 (push (cons (match-string 1 e
) (match-string 2 e
)) cst
)))
3465 (setq org-table-formula-constants-local cst
)))
3467 ;; Process the tags.
3470 (while (setq e
(pop tags
))
3472 ((equal e
"{") (push '(:startgroup
) tgs
))
3473 ((equal e
"}") (push '(:endgroup
) tgs
))
3474 ((equal e
"\\n") (push '(:newline
) tgs
))
3475 ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e
)
3476 (push (cons (match-string 1 e
)
3477 (string-to-char (match-string 2 e
)))
3479 (t (push (list e
) tgs
))))
3480 (org-set-local 'org-tag-alist nil
)
3481 (while (setq e
(pop tgs
))
3482 (or (and (stringp (car e
))
3483 (assoc (car e
) org-tag-alist
))
3484 (push e org-tag-alist
)))))
3486 ;; Compute the regular expressions and other local variables
3487 (if (not org-done-keywords
)
3488 (setq org-done-keywords
(list (org-last org-todo-keywords-1
))))
3489 (setq org-ds-keyword-length
(+ 2 (max (length org-deadline-string
)
3490 (length org-scheduled-string
)
3491 (length org-clock-string
)
3492 (length org-closed-string
)))
3494 (concat "^[ \t]*:\\("
3495 (mapconcat 'regexp-quote org-drawers
"\\|")
3497 org-not-done-keywords
3498 (org-delete-all org-done-keywords
(copy-sequence org-todo-keywords-1
))
3500 (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
3504 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
3506 org-todo-line-regexp
3507 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3508 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
3509 "\\)\\>\\)?[ \t]*\\(.*\\)")
3510 org-complex-heading-regexp
3511 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3512 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
3513 "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
3514 "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
3516 (concat "\n\\*+[ \t]+"
3517 "\\(?:" (mapconcat 'regexp-quote org-done-keywords
"\\|")
3519 org-todo-line-tags-regexp
3520 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3521 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
3523 "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
3524 org-looking-at-done-regexp
3526 (mapconcat 'regexp-quote org-done-keywords
"\\|") "\\)"
3528 org-deadline-regexp
(concat "\\<" org-deadline-string
)
3529 org-deadline-time-regexp
3530 (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>")
3531 org-deadline-line-regexp
3532 (concat "\\<\\(" org-deadline-string
"\\).*")
3533 org-scheduled-regexp
3534 (concat "\\<" org-scheduled-string
)
3535 org-scheduled-time-regexp
3536 (concat "\\<" org-scheduled-string
" *<\\([^>]+\\)>")
3537 org-closed-time-regexp
3538 (concat "\\<" org-closed-string
" *\\[\\([^]]+\\)\\]")
3539 org-keyword-time-regexp
3540 (concat "\\<\\(" org-scheduled-string
3541 "\\|" org-deadline-string
3542 "\\|" org-closed-string
3543 "\\|" org-clock-string
"\\)"
3544 " *[[<]\\([^]>]+\\)[]>]")
3545 org-keyword-time-not-clock-regexp
3546 (concat "\\<\\(" org-scheduled-string
3547 "\\|" org-deadline-string
3548 "\\|" org-closed-string
3550 " *[[<]\\([^]>]+\\)[]>]")
3551 org-maybe-keyword-time-regexp
3552 (concat "\\(\\<\\(" org-scheduled-string
3553 "\\|" org-deadline-string
3554 "\\|" org-closed-string
3555 "\\|" org-clock-string
"\\)\\)?"
3556 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
3557 org-planning-or-clock-line-re
3558 (concat "\\(?:^[ \t]*\\(" org-scheduled-string
3559 "\\|" org-deadline-string
3560 "\\|" org-closed-string
"\\|" org-clock-string
3563 (org-compute-latex-and-specials-regexp)
3564 (org-set-font-lock-defaults))))
3566 (defun org-file-contents (file &optional noerror
)
3567 "Return the contents of FILE, as a string."
3569 (not (file-readable-p file
)))
3572 (message "Cannot read file %s" file
)
3575 (error "Cannot read file %s" file
))
3577 (insert-file-contents file
)
3580 (defun org-extract-log-state-settings (x)
3581 "Extract the log state setting from a TODO keyword string.
3582 This will extract info from a string like \"WAIT(w@/!)\"."
3583 (let (kw key log1 log2
)
3584 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x
)
3585 (setq kw
(match-string 1 x
)
3586 key
(and (match-end 2) (match-string 2 x
))
3587 log1
(and (match-end 3) (match-string 3 x
))
3588 log2
(and (match-end 4) (match-string 4 x
)))
3591 (and log1
(if (equal log1
"!") 'time
'note
))
3592 (and log2
(if (equal log2
"!") 'time
'note
)))))))
3594 (defun org-remove-keyword-keys (list)
3595 "Remove a pair of parenthesis at the end of each string in LIST."
3597 (if (string-match "(.*)$" x
)
3598 (substring x
0 (match-beginning 0))
3602 ;; FIXME: this could be done much better, using second characters etc.
3603 (defun org-assign-fast-keys (alist)
3604 "Assign fast keys to a keyword-key alist.
3605 Respect keys that are already there."
3606 (let (new e k c c1 c2
(char ?a
))
3607 (while (setq e
(pop alist
))
3609 ((equal e
'(:startgroup
)) (push e new
))
3610 ((equal e
'(:endgroup
)) (push e new
))
3611 ((equal e
'(:newline
)) (push e new
))
3613 (setq k
(car e
) c2 nil
)
3616 ;; automatically assign a character.
3617 (setq c1
(string-to-char
3618 (downcase (substring
3619 k
(if (= (string-to-char k
) ?
@) 1 0)))))
3620 (if (or (rassoc c1 new
) (rassoc c1 alist
))
3621 (while (or (rassoc char new
) (rassoc char alist
))
3622 (setq char
(1+ char
)))
3624 (setq c
(or c2 char
)))
3625 (push (cons k c
) new
))))
3628 ;;; Some variables used in various places
3630 (defvar org-window-configuration nil
3631 "Used in various places to store a window configuration.")
3632 (defvar org-finish-function nil
3633 "Function to be called when `C-c C-c' is used.
3634 This is for getting out of special buffers like remember.")
3637 ;; FIXME: Occasionally check by commenting these, to make sure
3638 ;; no other functions uses these, forgetting to let-bind them.
3643 (defvar description
)
3645 ;; Defined somewhere in this file, but used before definition.
3646 (defvar org-html-entities
)
3647 (defvar org-struct-menu
)
3648 (defvar org-org-menu
)
3649 (defvar org-tbl-menu
)
3650 (defvar org-agenda-keymap
)
3652 ;;;; Define the Org-mode
3654 (if (and (not (keymapp outline-mode-map
)) (featurep 'allout
))
3655 (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."))
3658 ;; We use a before-change function to check if a table might need
3660 (defvar org-table-may-need-update t
3661 "Indicates that a table might need an update.
3662 This variable is set by `org-before-change-function'.
3663 `org-table-align' sets it back to nil.")
3664 (defun org-before-change-function (beg end
)
3665 "Every change indicates that a table might need an update."
3666 (setq org-table-may-need-update t
))
3667 (defvar org-mode-map
)
3668 (defvar org-mode-hook nil
3669 "Mode hook for Org-mode, run after the mode was turned on.")
3670 (defvar org-inhibit-startup nil
) ; Dynamically-scoped param.
3671 (defvar org-agenda-keep-modes nil
) ; Dynamically-scoped param.
3672 (defvar org-table-buffer-is-an nil
)
3673 (defconst org-outline-regexp
"\\*+ ")
3676 (define-derived-mode org-mode outline-mode
"Org"
3677 "Outline-based notes management and organizer, alias
3678 \"Carsten's outline-mode for keeping track of everything.\"
3680 Org-mode develops organizational tasks around a NOTES file which
3681 contains information about projects as plain text. Org-mode is
3682 implemented on top of outline-mode, which is ideal to keep the content
3683 of large files well structured. It supports ToDo items, deadlines and
3684 time stamps, which magically appear in the diary listing of the Emacs
3685 calendar. Tables are easily created with a built-in table editor.
3686 Plain text URL-like links connect to websites, emails (VM), Usenet
3687 messages (Gnus), BBDB entries, and any files related to the project.
3688 For printing and sharing of notes, an Org-mode file (or a part of it)
3689 can be exported as a structured ASCII or HTML file.
3691 The following commands are available:
3695 ;; Get rid of Outline menus, they are not needed
3696 ;; Need to do this here because define-derived-mode sets up
3697 ;; the keymap so late. Still, it is a waste to call this each time
3698 ;; we switch another buffer into org-mode.
3699 (if (featurep 'xemacs
)
3700 (when (boundp 'outline-mode-menu-heading
)
3701 ;; Assume this is Greg's port, it used easymenu
3702 (easy-menu-remove outline-mode-menu-heading
)
3703 (easy-menu-remove outline-mode-menu-show
)
3704 (easy-menu-remove outline-mode-menu-hide
))
3705 (define-key org-mode-map
[menu-bar headings
] 'undefined
)
3706 (define-key org-mode-map
[menu-bar hide
] 'undefined
)
3707 (define-key org-mode-map
[menu-bar show
] 'undefined
))
3709 (org-load-modules-maybe)
3710 (easy-menu-add org-org-menu
)
3711 (easy-menu-add org-tbl-menu
)
3712 (org-install-agenda-files-menu)
3713 (if org-descriptive-links
(org-add-to-invisibility-spec '(org-link)))
3714 (org-add-to-invisibility-spec '(org-cwidth))
3715 (when (featurep 'xemacs
)
3716 (org-set-local 'line-move-ignore-invisible t
))
3717 (org-set-local 'outline-regexp org-outline-regexp
)
3718 (org-set-local 'outline-level
'org-outline-level
)
3719 (when (and org-ellipsis
3720 (fboundp 'set-display-table-slot
) (boundp 'buffer-display-table
)
3721 (fboundp 'make-glyph-code
))
3722 (unless org-display-table
3723 (setq org-display-table
(make-display-table)))
3724 (set-display-table-slot
3727 (lambda (c) (make-glyph-code c
(and (not (stringp org-ellipsis
))
3729 (if (stringp org-ellipsis
) org-ellipsis
"..."))))
3730 (setq buffer-display-table org-display-table
))
3731 (org-set-regexps-and-options)
3732 (when (and org-tag-faces
(not org-tags-special-faces-re
))
3733 ;; tag faces set outside customize.... force initialization.
3734 (org-set-tag-faces 'org-tag-faces org-tag-faces
))
3736 (org-set-local 'calc-embedded-open-mode
"# ")
3737 (modify-syntax-entry ?
# "<")
3738 (modify-syntax-entry ?
@ "w")
3739 (if org-startup-truncated
(setq truncate-lines t
))
3740 (org-set-local 'font-lock-unfontify-region-function
3741 'org-unfontify-region
)
3742 ;; Activate before-change-function
3743 (org-set-local 'org-table-may-need-update t
)
3744 (org-add-hook 'before-change-functions
'org-before-change-function nil
3746 ;; Check for running clock before killing a buffer
3747 (org-add-hook 'kill-buffer-hook
'org-check-running-clock nil
'local
)
3748 ;; Paragraphs and auto-filling
3749 (org-set-autofill-regexps)
3750 (setq indent-line-function
'org-indent-line-function
)
3751 (org-update-radio-target-regexp)
3752 ;; Make sure dependence stuff works reliably, even for users who set it
3754 (if org-enforce-todo-dependencies
3755 (add-hook 'org-blocker-hook
3756 'org-block-todo-from-children-or-siblings
)
3757 (remove-hook 'org-blocker-hook
3758 'org-block-todo-from-children-or-siblings
))
3759 (if org-enforce-todo-checkbox-dependencies
3760 (add-hook 'org-blocker-hook
3761 'org-block-todo-from-checkboxes
)
3762 (remove-hook 'org-blocker-hook
3763 'org-block-todo-from-checkboxes
))
3765 ;; Comment characters
3766 ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
3767 (org-set-local 'comment-padding
" ")
3769 ;; Align options lines
3771 'align-mode-rules-list
3772 '((org-in-buffer-settings
3773 (regexp .
"^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
3774 (modes .
'(org-mode)))))
3777 (org-set-local 'imenu-create-index-function
3778 'org-imenu-get-tree
)
3780 ;; Make isearch reveal context
3781 (if (or (featurep 'xemacs
)
3782 (not (boundp 'outline-isearch-open-invisible-function
)))
3783 ;; Emacs 21 and XEmacs make use of the hook
3784 (org-add-hook 'isearch-mode-end-hook
'org-isearch-end
'append
'local
)
3785 ;; Emacs 22 deals with this through a special variable
3786 (org-set-local 'outline-isearch-open-invisible-function
3787 (lambda (&rest ignore
) (org-show-context 'isearch
))))
3789 ;; If empty file that did not turn on org-mode automatically, make it to.
3790 (if (and org-insert-mode-line-in-empty-file
3792 (= (point-min) (point-max)))
3793 (insert "# -*- mode: org -*-\n\n"))
3795 (unless org-inhibit-startup
3796 (when org-startup-align-all-tables
3797 (let ((bmp (buffer-modified-p)))
3798 (org-table-map-tables 'org-table-align
)
3799 (set-buffer-modified-p bmp
)))
3800 (org-set-startup-visibility)))
3802 (put 'org-mode
'flyspell-mode-predicate
'org-mode-flyspell-verify
)
3804 (defun org-current-time ()
3805 "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
3806 (if (> (car org-time-stamp-rounding-minutes
) 1)
3807 (let ((r (car org-time-stamp-rounding-minutes
))
3808 (time (decode-time)))
3810 (append (list 0 (* r
(floor (+ .5 (/ (float (nth 1 time
)) r
)))))
3814 ;;;; Font-Lock stuff, including the activators
3816 (defvar org-mouse-map
(make-sparse-keymap))
3817 (org-defkey org-mouse-map
3818 (if (featurep 'xemacs
) [button2] [mouse-2]) 'org-open-at-mouse)
3819 (org-defkey org-mouse-map
3820 (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
3821 (when org-mouse-1-follows-link
3822 (org-defkey org-mouse-map [follow-link] 'mouse-face))
3823 (when org-tab-follows-link
3824 (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
3825 (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
3827 (require 'font-lock)
3829 (defconst org-non-link-chars "]\t\n\r<>")
3830 (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
3832 (defvar org-link-types-re nil
3833 "Matches a link that has a url-like prefix like \"http:\"")
3834 (defvar org-link-re-with-space nil
3835 "Matches a link with spaces, optional angular brackets around it.")
3836 (defvar org-link-re-with-space2 nil
3837 "Matches a link with spaces, optional angular brackets around it.")
3838 (defvar org-link-re-with-space3 nil
3839 "Matches a link with spaces, only for internal part in bracket links.")
3840 (defvar org-angle-link-re nil
3841 "Matches link with angular brackets, spaces are allowed.")
3842 (defvar org-plain-link-re nil
3843 "Matches plain link, without spaces.")
3844 (defvar org-bracket-link-regexp nil
3845 "Matches a link in double brackets.")
3846 (defvar org-bracket-link-analytic-regexp nil
3847 "Regular expression used to analyze links.
3848 Here is what the match groups contain after a match:
3854 (defvar org-bracket-link-analytic-regexp++ nil
3855 "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
3856 (defvar org-any-link-re nil
3857 "Regular expression matching any link.")
3859 (defun org-make-link-regexps ()
3860 "Update the link regular expressions.
3861 This should be called after the variable `org-link-types' has changed."
3862 (setq org-link-types-re
3864 "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
3865 org-link-re-with-space
3867 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3868 "\\([^" org-non-link-chars " ]"
3869 "[^" org-non-link-chars "]*"
3870 "[^" org-non-link-chars " ]\\)>?")
3871 org-link-re-with-space2
3873 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3874 "\\([^" org-non-link-chars " ]"
3876 "[^" org-non-link-chars " ]\\)>?")
3877 org-link-re-with-space3
3879 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3880 "\\([^" org-non-link-chars " ]"
3884 "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3885 "\\([^" org-non-link-chars " ]"
3886 "[^" org-non-link-chars "]*"
3890 "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3891 "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
3892 org-bracket-link-regexp
3893 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
3894 org-bracket-link-analytic-regexp
3897 "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
3900 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
3902 org-bracket-link-analytic-regexp++
3905 "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
3908 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
3911 (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
3912 org-angle-link-re "\\)\\|\\("
3913 org-plain-link-re "\\)")))
3915 (org-make-link-regexps)
3917 (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
3918 "Regular expression for fast time stamp matching.")
3919 (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
3920 "Regular expression for fast time stamp matching.")
3921 (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
3922 "Regular expression matching time strings for analysis.
3923 This one does not require the space after the date, so it can be used
3924 on a string that terminates immediately after the date.")
3925 (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
3926 "Regular expression matching time strings for analysis.")
3927 (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
3928 "Regular expression matching time stamps, with groups.")
3929 (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
3930 "Regular expression matching time stamps (also [..]), with groups.")
3931 (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
3932 "Regular expression matching a time stamp range.")
3933 (defconst org-tr-regexp-both
3934 (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
3935 "Regular expression matching a time stamp range.")
3936 (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
3937 org-ts-regexp "\\)?")
3938 "Regular expression matching a time stamp or time stamp range.")
3939 (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
3940 org-ts-regexp-both "\\)?")
3941 "Regular expression matching a time stamp or time stamp range.
3942 The time stamps may be either active or inactive.")
3944 (defvar org-emph-face nil)
3946 (defun org-do-emphasis-faces (limit)
3947 "Run through the buffer and add overlays to links."
3949 (while (and (not rtn) (re-search-forward org-emph-re limit t))
3950 (if (not (= (char-after (match-beginning 3))
3951 (char-after (match-beginning 4))))
3954 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
3956 (nth 1 (assoc (match-string 3)
3957 org-emphasis-alist)))
3958 (add-text-properties (match-beginning 2) (match-end 2)
3959 '(font-lock-multiline t))
3960 (when org-hide-emphasis-markers
3961 (add-text-properties (match-end 4) (match-beginning 5)
3962 '(invisible org-link))
3963 (add-text-properties (match-beginning 3) (match-end 3)
3964 '(invisible org-link)))))
3968 (defun org-emphasize (&optional char)
3969 "Insert or change an emphasis, i.e. a font like bold or italic.
3970 If there is an active region, change that region to a new emphasis.
3971 If there is no region, just insert the marker characters and position
3972 the cursor between them.
3973 CHAR should be either the marker character, or the first character of the
3974 HTML tag associated with that emphasis. If CHAR is a space, the means
3975 to remove the emphasis of the selected region.
3976 If char is not given (for example in an interactive call) it
3977 will be prompted for."
3979 (let ((eal org-emphasis-alist) e det
3980 (erc org-emphasis-regexp-components)
3982 (string "") beg end move tag c s)
3983 (if (org-region-active-p)
3984 (setq beg (region-beginning) end (region-end)
3985 string (buffer-substring beg end))
3988 (while (setq e (pop eal))
3989 (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
3991 (push (cons c (string-to-char (car e))) det)
3992 (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
3993 (substring tag 1)))))
3994 (setq det (nreverse det))
3996 (message "%s" (concat "Emphasis marker or tag:" prompt))
3997 (setq char (read-char-exclusive)))
3998 (setq char (or (cdr (assoc char det)) char))
3999 (if (equal char ?\ )
4000 (setq s "" move nil)
4001 (unless (assoc (char-to-string char) org-emphasis-alist)
4002 (error "No such emphasis marker: \"%c\"" char))
4003 (setq s (char-to-string char)))
4004 (while (and (> (length string) 1)
4005 (equal (substring string 0 1) (substring string -1))
4006 (assoc (substring string 0 1) org-emphasis-alist))
4007 (setq string (substring string 1 -1)))
4008 (setq string (concat s string s))
4009 (if beg (delete-region beg end))
4011 (string-match (concat "[" (nth 0 erc) "\n]")
4012 (char-to-string (char-before (point)))))
4014 (unless (string-match (concat "[" (nth 1 erc) "\n]")
4015 (char-to-string (char-after (point))))
4016 (insert " ") (backward-char 1))
4018 (and move (backward-char 1))))
4020 (defconst org-nonsticky-props
4021 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
4024 (defun org-activate-plain-links (limit)
4025 "Run through the buffer and add overlays to links."
4028 (while (re-search-forward org-plain-link-re limit t)
4029 (setq f (get-text-property (match-beginning 0) 'face))
4030 (if (or (eq f 'org-tag)
4031 (and (listp f) (memq 'org-tag f)))
4033 (add-text-properties (match-beginning 0) (match-end 0)
4034 (list 'mouse-face 'highlight
4035 'rear-nonsticky org-nonsticky-props
4036 'keymap org-mouse-map
4038 (throw 'exit t))))))
4040 (defun org-activate-code (limit)
4041 (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
4043 (remove-text-properties (match-beginning 0) (match-end 0)
4044 '(display t invisible t intangible t))
4047 (defun org-activate-angle-links (limit)
4048 "Run through the buffer and add overlays to links."
4049 (if (re-search-forward org-angle-link-re limit t)
4051 (add-text-properties (match-beginning 0) (match-end 0)
4052 (list 'mouse-face 'highlight
4053 'rear-nonsticky org-nonsticky-props
4054 'keymap org-mouse-map
4058 (defun org-activate-footnote-links (limit)
4059 "Run through the buffer and add overlays to links."
4060 (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
4063 (add-text-properties (match-beginning 2) (match-end 2)
4064 (list 'mouse-face 'highlight
4065 'rear-nonsticky org-nonsticky-props
4066 'keymap org-mouse-map
4068 (if (= (point-at-bol) (match-beginning 2))
4069 "Footnote definition"
4070 "Footnote reference")
4074 (defun org-activate-bracket-links (limit)
4075 "Run through the buffer and add overlays to bracketed links."
4076 (if (re-search-forward org-bracket-link-regexp limit t)
4077 (let* ((help (concat "LINK: "
4078 (org-match-string-no-properties 1)))
4079 ;; FIXME: above we should remove the escapes.
4080 ;; but that requires another match, protecting match data,
4081 ;; a lot of overhead for font-lock.
4082 (ip (org-maybe-intangible
4083 (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
4084 'keymap org-mouse-map 'mouse-face 'highlight
4085 'font-lock-multiline t 'help-echo help)))
4086 (vp (list 'rear-nonsticky org-nonsticky-props
4087 'keymap org-mouse-map 'mouse-face 'highlight
4088 'font-lock-multiline t 'help-echo help)))
4089 ;; We need to remove the invisible property here. Table narrowing
4090 ;; may have made some of this invisible.
4091 (remove-text-properties (match-beginning 0) (match-end 0)
4095 (add-text-properties (match-beginning 0) (match-beginning 3) ip)
4096 (add-text-properties (match-beginning 3) (match-end 3) vp)
4097 (add-text-properties (match-end 3) (match-end 0) ip))
4098 (add-text-properties (match-beginning 0) (match-beginning 1) ip)
4099 (add-text-properties (match-beginning 1) (match-end 1) vp)
4100 (add-text-properties (match-end 1) (match-end 0) ip))
4103 (defun org-activate-dates (limit)
4104 "Run through the buffer and add overlays to dates."
4105 (if (re-search-forward org-tsr-regexp-both limit t)
4107 (add-text-properties (match-beginning 0) (match-end 0)
4108 (list 'mouse-face 'highlight
4109 'rear-nonsticky org-nonsticky-props
4110 'keymap org-mouse-map))
4111 (when org-display-custom-times
4113 (org-display-custom-time (match-beginning 3) (match-end 3)))
4114 (org-display-custom-time (match-beginning 1) (match-end 1)))
4117 (defvar org-target-link-regexp nil
4118 "Regular expression matching radio targets in plain text.")
4119 (make-variable-buffer-local 'org-target-link-regexp)
4120 (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
4121 "Regular expression matching a link target.")
4122 (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
4123 "Regular expression matching a radio target.")
4124 (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
4125 "Regular expression matching any target.")
4127 (defun org-activate-target-links (limit)
4128 "Run through the buffer and add overlays to target matches."
4129 (when org-target-link-regexp
4130 (let ((case-fold-search t))
4131 (if (re-search-forward org-target-link-regexp limit t)
4133 (add-text-properties (match-beginning 0) (match-end 0)
4134 (list 'mouse-face 'highlight
4135 'rear-nonsticky org-nonsticky-props
4136 'keymap org-mouse-map
4137 'help-echo "Radio target link"
4138 'org-linked-text t))
4141 (defun org-update-radio-target-regexp ()
4142 "Find all radio targets in this file and update the regular expression."
4144 (when (memq 'radio org-activate-links)
4145 (setq org-target-link-regexp
4146 (org-make-target-link-regexp (org-all-targets 'radio)))
4147 (org-restart-font-lock)))
4149 (defun org-hide-wide-columns (limit)
4151 (setq s (text-property-any (point) (or limit (point-max))
4154 (setq e (next-single-property-change s 'org-cwidth))
4155 (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
4159 (defvar org-latex-and-specials-regexp nil
4160 "Regular expression for highlighting export special stuff.")
4161 (defvar org-match-substring-regexp)
4162 (defvar org-match-substring-with-braces-regexp)
4163 (defvar org-export-html-special-string-regexps)
4165 (defun org-compute-latex-and-specials-regexp ()
4166 "Compute regular expression for stuff treated specially by exporters."
4167 (if (not org-highlight-latex-fragments-and-specials)
4168 (org-set-local 'org-latex-and-specials-regexp nil)
4171 ((matchers (plist-get org-format-latex-options :matchers))
4172 (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
4173 org-latex-regexps)))
4174 (options (org-combine-plists (org-default-export-plist)
4175 (org-infile-export-plist)))
4176 (org-export-with-sub-superscripts (plist-get options :sub-superscript))
4177 (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
4178 (org-export-with-TeX-macros (plist-get options :TeX-macros))
4179 (org-export-html-expand (plist-get options :expand-quoted-html))
4180 (org-export-with-special-strings (plist-get options :special-strings))
4183 ((equal org-export-with-sub-superscripts '{})
4184 (list org-match-substring-with-braces-regexp))
4185 (org-export-with-sub-superscripts
4186 (list org-match-substring-regexp))
4189 (if org-export-with-LaTeX-fragments
4190 (mapcar (lambda (x) (nth 1 x)) latexs)))
4192 (if org-export-with-TeX-macros
4193 (list (concat "\\\\"
4195 (append (mapcar 'car org-html-entities)
4196 (if (boundp 'org-latex-entities)
4197 org-latex-entities nil))
4200 ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
4201 (re-special (if org-export-with-special-strings
4202 (mapcar (lambda (x) (car x))
4203 org-export-html-special-string-regexps)))
4207 (if org-export-html-expand "@<[^>\n]+>")
4210 'org-latex-and-specials-regexp
4211 (mapconcat 'identity (append re-latex re-sub re-macros re-special
4214 (defun org-do-latex-and-special-faces (limit)
4215 "Run through the buffer and add overlays to links."
4216 (when org-latex-and-specials-regexp
4218 (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
4220 (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
4222 '(org-code org-verbatim underline)))
4225 d (cond ((member (char-after (1+ (match-beginning 0)))
4228 (font-lock-prepend-text-property
4229 (+ d (match-beginning 0)) (match-end 0)
4230 'face 'org-latex-and-export-specials)
4231 (add-text-properties (+ d (match-beginning 0)) (match-end 0)
4232 '(font-lock-multiline t)))))
4235 (defun org-restart-font-lock ()
4236 "Restart font-lock-mode, to force refontification."
4237 (when (and (boundp 'font-lock-mode) font-lock-mode)
4239 (font-lock-mode 1)))
4241 (defun org-all-targets (&optional radio)
4242 "Return a list of all targets in this file.
4243 With optional argument RADIO, only find radio targets."
4244 (let ((re (if radio org-radio-target-regexp org-target-regexp))
4247 (goto-char (point-min))
4248 (while (re-search-forward re nil t)
4249 (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
4252 (defun org-make-target-link-regexp (targets)
4253 "Make regular expression matching all strings in TARGETS.
4254 The regular expression finds the targets also if there is a line break
4261 (while (string-match " +" x)
4262 (setq x (replace-match "\\s-+" t t x)))
4268 (defun org-activate-tags (limit)
4269 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
4271 (add-text-properties (match-beginning 1) (match-end 1)
4272 (list 'mouse-face 'highlight
4273 'rear-nonsticky org-nonsticky-props
4274 'keymap org-mouse-map))
4277 (defun org-outline-level ()
4279 (looking-at outline-regexp)
4280 (if (match-beginning 1)
4281 (+ (org-get-string-indentation (match-string 1)) 1000)
4282 (1- (- (match-end 0) (match-beginning 0))))))
4284 (defvar org-font-lock-keywords nil)
4286 (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
4287 "Regular expression matching a property line.")
4289 (defvar org-font-lock-hook nil
4290 "Functions to be called for special font lock stuff.")
4292 (defun org-font-lock-hook (limit)
4293 (run-hook-with-args 'org-font-lock-hook limit))
4295 (defun org-set-font-lock-defaults ()
4296 (let* ((em org-fontify-emphasized-text)
4297 (lk org-activate-links)
4298 (org-font-lock-extra-keywords
4301 '(org-font-lock-hook)
4303 '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
4304 (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
4306 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
4309 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
4310 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
4311 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
4313 (list org-drawer-regexp '(0 'org-special-keyword t))
4314 (list "^[ \t]*:END:" '(0 'org-special-keyword t))
4316 (list org-property-re
4317 '(1 'org-special-keyword t)
4318 '(3 'org-property-value t))
4319 (if org-format-transports-properties-p
4320 '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
4322 (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
4323 (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
4324 (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
4325 (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
4326 (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
4327 (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
4328 (if (memq 'footnote lk) '(org-activate-footnote-links
4329 (2 'org-footnote t)))
4330 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
4331 '(org-hide-wide-columns (0 nil append))
4333 (list (concat "^\\*+[ \t]+" org-todo-regexp)
4334 '(1 (org-get-todo-face 1) t))
4336 (if org-fontify-done-headline
4337 (list (concat "^[*]+ +\\<\\("
4338 (mapconcat 'regexp-quote org-done-keywords "\\|")
4340 '(2 'org-headline-done t))
4343 '(org-font-lock-add-priority-faces)
4345 '(org-font-lock-add-tag-faces)
4347 (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
4348 (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
4349 (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
4350 (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
4353 (if (featurep 'xemacs)
4354 '(org-do-emphasis-faces (0 nil append))
4355 '(org-do-emphasis-faces)))
4357 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
4358 2 'org-checkbox prepend)
4359 (if org-provide-checkbox-statistics
4360 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
4361 (0 (org-get-checkbox-statistics-face) t)))
4362 ;; Description list items
4363 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
4365 (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
4366 '(1 'org-archived prepend))
4368 '(org-do-latex-and-special-faces)
4370 '(org-activate-code (1 'org-code t))
4372 (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
4373 "\\|" org-quote-string "\\)\\>")
4374 '(1 'org-special-keyword t))
4375 '("^#.*" (0 'font-lock-comment-face t))
4377 (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
4378 ;; Now set the full font-lock-keywords
4379 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
4380 (org-set-local 'font-lock-defaults
4381 '(org-font-lock-keywords t nil nil backward-paragraph))
4382 (kill-local-variable 'font-lock-keywords) nil))
4387 (defun org-get-level-face (n)
4388 "Get the right face for match N in font-lock matching of headlines."
4389 (setq org-l (- (match-end 2) (match-beginning 1) 1))
4390 (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
4391 (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
4393 ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
4395 (t (if org-level-color-stars-only nil org-f))))
4397 (defun org-get-todo-face (kwd)
4398 "Get the right face for a TODO keyword KWD.
4399 If KWD is a number, get the corresponding match group."
4400 (if (numberp kwd) (setq kwd (match-string kwd)))
4401 (or (cdr (assoc kwd org-todo-keyword-faces))
4402 (and (member kwd org-done-keywords) 'org-done)
4405 (defun org-font-lock-add-tag-faces (limit)
4406 "Add the special tag faces."
4407 (when (and org-tag-faces org-tags-special-faces-re)
4408 (while (re-search-forward org-tags-special-faces-re limit t)
4409 (add-text-properties (match-beginning 1) (match-end 1)
4410 (list 'face (org-get-tag-face 1)
4411 'font-lock-fontified t))
4412 (backward-char 1))))
4414 (defun org-font-lock-add-priority-faces (limit)
4415 "Add the special priority faces."
4416 (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
4417 (add-text-properties
4418 (match-beginning 0) (match-end 0)
4419 (list 'face (or (cdr (assoc (char-after (match-beginning 1))
4420 org-priority-faces))
4421 'org-special-keyword)
4422 'font-lock-fontified t))))
4424 (defun org-get-tag-face (kwd)
4425 "Get the right face for a TODO keyword KWD.
4426 If KWD is a number, get the corresponding match group."
4427 (if (numberp kwd) (setq kwd (match-string kwd)))
4428 (or (cdr (assoc kwd org-tag-faces))
4431 (defun org-unfontify-region (beg end &optional maybe_loudly)
4432 "Remove fontification and activation overlays from links."
4433 (font-lock-default-unfontify-region beg end)
4434 (let* ((buffer-undo-list t)
4435 (inhibit-read-only t) (inhibit-point-motion-hooks t)
4436 (inhibit-modification-hooks t)
4437 deactivate-mark buffer-file-name buffer-file-truename)
4438 (remove-text-properties beg end
4439 '(mouse-face t keymap t org-linked-text t
4440 invisible t intangible t))))
4442 ;;;; Visibility cycling, including org-goto and indirect buffer
4446 (defvar org-cycle-global-status nil)
4447 (make-variable-buffer-local 'org-cycle-global-status)
4448 (defvar org-cycle-subtree-status nil)
4449 (make-variable-buffer-local 'org-cycle-subtree-status)
4452 (defun org-cycle (&optional arg)
4453 "Visibility cycling for Org-mode.
4455 - When this function is called with a prefix argument, rotate the entire
4456 buffer through 3 states (global cycling)
4457 1. OVERVIEW: Show only top-level headlines.
4458 2. CONTENTS: Show all headlines of all levels, but no body text.
4459 3. SHOW ALL: Show everything.
4460 When called with two C-u C-u prefixes, switch to the startup visibility,
4461 determined by the variable `org-startup-folded', and by any VISIBILITY
4462 properties in the buffer.
4463 When called with three C-u C-u C-u prefixed, show the entire buffer,
4466 - When point is at the beginning of a headline, rotate the subtree started
4467 by this line through 3 different states (local cycling)
4468 1. FOLDED: Only the main headline is shown.
4469 2. CHILDREN: The main headline and the direct children are shown.
4470 From this state, you can move to one of the children
4471 and zoom in further.
4472 3. SUBTREE: Show the entire subtree, including body text.
4474 - When there is a numeric prefix, go up to a heading with level ARG, do
4475 a `show-subtree' and return to the previous cursor position. If ARG
4476 is negative, go up that many levels.
4478 - When point is not at the beginning of a headline, execute the global
4479 binding for TAB, which is re-indenting the line. See the option
4480 `org-cycle-emulate-tab' for details.
4482 - Special case: if point is at the beginning of the buffer and there is
4483 no headline in line 1, this function will act as if called with prefix arg.
4484 But only if also the variable `org-cycle-global-at-bob' is t."
4486 (org-load-modules-maybe)
4487 (let* ((outline-regexp
4488 (if (and (org-mode-p) org-cycle-include-plain-lists)
4489 "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
4491 (bob-special (and org-cycle-global-at-bob (bobp)
4492 (not (looking-at outline-regexp))))
4495 (delq 'org-optimize-window-after-visibility-change
4496 (copy-sequence org-cycle-hook))
4500 (if (or bob-special (equal arg '(4)))
4501 ;; special case: use global cycling
4507 (org-set-startup-visibility)
4508 (message "Startup visibility, plus VISIBILITY properties"))
4512 (message "Entire buffer visible, including drawers"))
4514 ((org-at-table-p 'any)
4515 ;; Enter the table or move to the next field in the table
4516 (or (org-table-recognize-table.el)
4518 (if arg (org-table-edit-field t)
4519 (org-table-justify-field-maybe)
4520 (call-interactively 'org-table-next-field)))))
4522 ((eq arg t) ;; Global cycling
4525 ((and (eq last-command this-command)
4526 (eq org-cycle-global-status 'overview))
4527 ;; We just created the overview - now do table of contents
4528 ;; This can be slow in very large buffers, so indicate action
4529 (message "CONTENTS...")
4531 (message "CONTENTS...done")
4532 (setq org-cycle-global-status 'contents)
4533 (run-hook-with-args 'org-cycle-hook 'contents))
4535 ((and (eq last-command this-command)
4536 (eq org-cycle-global-status 'contents))
4537 ;; We just showed the table of contents - now show everything
4539 (message "SHOW ALL")
4540 (setq org-cycle-global-status 'all)
4541 (run-hook-with-args 'org-cycle-hook 'all))
4544 ;; Default action: go to overview
4546 (message "OVERVIEW")
4547 (setq org-cycle-global-status 'overview)
4548 (run-hook-with-args 'org-cycle-hook 'overview))))
4550 ((and org-drawers org-drawer-regexp
4552 (beginning-of-line 1)
4553 (looking-at org-drawer-regexp)))
4554 ;; Toggle block visibility
4556 (not (get-char-property (match-end 0) 'invisible))))
4559 ;; Show-subtree, ARG levels up from here.
4561 (org-back-to-heading)
4562 (outline-up-heading (if (< arg 0) (- arg)
4563 (- (funcall outline-level) arg)))
4564 (org-show-subtree)))
4566 ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
4567 (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
4568 ;; At a heading: rotate between three different views
4569 (org-back-to-heading)
4570 (let ((goal-column 0) eoh eol eos)
4571 ;; First, some boundaries
4573 (org-back-to-heading)
4575 (beginning-of-line 2)
4576 (while (and (not (eobp)) ;; this is like `next-line'
4577 (get-char-property (1- (point)) 'invisible))
4578 (beginning-of-line 2)) (setq eol (point)))
4579 (outline-end-of-heading) (setq eoh (point))
4580 (org-end-of-subtree t)
4582 (skip-chars-forward " \t\n")
4583 (beginning-of-line 1) ; in case this is an item
4585 (setq eos (1- (point))))
4586 ;; Find out what to do next and set `this-command'
4589 ;; Nothing is hidden behind this heading
4590 (message "EMPTY ENTRY")
4591 (setq org-cycle-subtree-status nil)
4594 (outline-next-heading)
4595 (if (org-invisible-p) (org-flag-heading nil))))
4597 (not (string-match "\\S-" (buffer-substring eol eos))))
4598 ;; Entire subtree is hidden in one line: open it
4601 (message "CHILDREN")
4604 (outline-next-heading)
4605 (if (org-invisible-p) (org-flag-heading nil)))
4606 (setq org-cycle-subtree-status 'children)
4607 (run-hook-with-args 'org-cycle-hook 'children))
4608 ((and (eq last-command this-command)
4609 (eq org-cycle-subtree-status 'children))
4610 ;; We just showed the children, now show everything.
4613 (setq org-cycle-subtree-status 'subtree)
4614 (run-hook-with-args 'org-cycle-hook 'subtree))
4616 ;; Default action: hide the subtree.
4619 (setq org-cycle-subtree-status 'folded)
4620 (run-hook-with-args 'org-cycle-hook 'folded)))))
4622 ;; TAB emulation and template completion
4623 (buffer-read-only (org-back-to-heading))
4625 ((org-try-structure-completion))
4627 ((org-try-cdlatex-tab))
4629 ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
4631 (not (looking-at outline-regexp))))
4632 (call-interactively (global-key-binding "\t")))
4634 ((if (and (memq org-cycle-emulate-tab '(white whitestart))
4635 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
4636 (or (and (eq org-cycle-emulate-tab 'white)
4637 (= (match-end 0) (point-at-eol)))
4638 (and (eq org-cycle-emulate-tab 'whitestart)
4639 (>= (match-end 0) pos))))
4641 (eq org-cycle-emulate-tab t))
4642 (call-interactively (global-key-binding "\t")))
4645 (org-back-to-heading)
4649 (defun org-global-cycle (&optional arg)
4650 "Cycle the global visibility. For details see `org-cycle'.
4651 With C-u prefix arg, switch to startup visibility.
4652 With a numeric prefix, show all headlines up to that level."
4654 (let ((org-cycle-include-plain-lists
4655 (if (org-mode-p) org-cycle-include-plain-lists nil)))
4659 (hide-sublevels arg)
4660 (setq org-cycle-global-status 'contents))
4662 (org-set-startup-visibility)
4663 (message "Startup visibility, plus VISIBILITY properties."))
4665 (org-cycle '(4))))))
4667 (defun org-set-startup-visibility ()
4668 "Set the visibility required by startup options and properties."
4670 ((eq org-startup-folded t)
4672 ((eq org-startup-folded 'content)
4673 (let ((this-command 'org-cycle) (last-command 'org-cycle))
4674 (org-cycle '(4)) (org-cycle '(4)))))
4675 (org-set-visibility-according-to-property 'no-cleanup)
4676 (org-cycle-hide-archived-subtrees 'all)
4677 (org-cycle-hide-drawers 'all)
4678 (org-cycle-show-empty-lines 'all))
4680 (defun org-set-visibility-according-to-property (&optional no-cleanup)
4681 "Switch subtree visibilities according to :VISIBILITY: property."
4683 (let (org-show-entry-below state)
4685 (goto-char (point-min))
4686 (while (re-search-forward
4687 "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
4689 (setq state (match-string 1))
4691 (org-back-to-heading t)
4695 ((equal state '("fold" "folded"))
4697 ((equal state "children")
4698 (org-show-hidden-entry)
4700 ((equal state "content")
4703 (org-narrow-to-subtree)
4705 ((member state '("all" "showall"))
4708 (org-cycle-hide-archived-subtrees 'all)
4709 (org-cycle-hide-drawers 'all)
4710 (org-cycle-show-empty-lines 'all)))))
4712 (defun org-overview ()
4713 "Switch to overview mode, showing only top-level headlines.
4714 Really, this shows all headlines with level equal or greater than the level
4715 of the first headline in the buffer. This is important, because if the
4716 first headline is not level one, then (hide-sublevels 1) gives confusing
4719 (let ((level (save-excursion
4720 (goto-char (point-min))
4721 (if (re-search-forward (concat "^" outline-regexp) nil t)
4723 (goto-char (match-beginning 0))
4724 (funcall outline-level))))))
4725 (and level (hide-sublevels level))))
4727 (defun org-content (&optional arg)
4728 "Show all headlines in the buffer, like a table of contents.
4729 With numerical argument N, show content up to level N."
4732 ;; Visit all headings and show their offspring
4733 (and (integerp arg) (org-overview))
4734 (goto-char (point-max))
4736 (while (and (progn (condition-case nil
4737 (outline-previous-visible-heading 1)
4738 (error (goto-char (point-min))))
4740 (looking-at outline-regexp))
4742 (show-children (1- arg))
4744 (if (bobp) (throw 'exit nil))))))
4747 (defun org-optimize-window-after-visibility-change (state)
4748 "Adjust the window after a change in outline visibility.
4749 This function is the default value of the hook `org-cycle-hook'."
4750 (when (get-buffer-window (current-buffer))
4752 ((eq state 'content) nil)
4753 ((eq state 'all) nil)
4754 ((eq state 'folded) nil)
4755 ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
4756 ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
4758 (defun org-compact-display-after-subtree-move ()
4759 "Show a compacter version of the tree of the entry's parent."
4761 (if (org-up-heading-safe)
4766 (org-cycle-show-empty-lines 'children)
4767 (org-cycle-hide-drawers 'children))
4770 (defun org-cycle-show-empty-lines (state)
4771 "Show empty lines above all visible headlines.
4772 The region to be covered depends on STATE when called through
4773 `org-cycle-hook'. Lisp program can use t for STATE to get the
4774 entire buffer covered. Note that an empty line is only shown if there
4775 are at least `org-cycle-separator-lines' empty lines before the headline."
4776 (when (> org-cycle-separator-lines 0)
4778 (let* ((n org-cycle-separator-lines)
4780 ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
4781 ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
4782 (t (let ((ns (number-to-string (- n 2))))
4783 (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
4784 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
4787 ((memq state '(overview contents t))
4788 (setq beg (point-min) end (point-max)))
4789 ((memq state '(children folded))
4790 (setq beg (point) end (progn (org-end-of-subtree t t)
4791 (beginning-of-line 2)
4795 (while (re-search-forward re end t)
4796 (if (not (get-char-property (match-end 1) 'invisible))
4797 (outline-flag-region
4798 (match-beginning 1) (match-end 1) nil)))))))
4799 ;; Never hide empty lines at the end of the file.
4801 (goto-char (point-max))
4802 (outline-previous-heading)
4803 (outline-end-of-heading)
4804 (if (and (looking-at "[ \t\n]+")
4805 (= (match-end 0) (point-max)))
4806 (outline-flag-region (point) (match-end 0) nil))))
4808 (defun org-show-empty-lines-in-parent ()
4809 "Move to the parent and re-show empty lines before visible headlines."
4811 (let ((context (if (org-up-heading-safe) 'children 'overview)))
4812 (org-cycle-show-empty-lines context))))
4814 (defun org-cycle-hide-drawers (state)
4815 "Re-hide all drawers after a visibility state change."
4816 (when (and (org-mode-p)
4817 (not (memq state '(overview folded))))
4819 (let* ((globalp (memq state '(contents all)))
4820 (beg (if globalp (point-min) (point)))
4821 (end (if globalp (point-max) (org-end-of-subtree t))))
4823 (while (re-search-forward org-drawer-regexp end t)
4824 (org-flag-drawer t))))))
4826 (defun org-flag-drawer (flag)
4828 (beginning-of-line 1)
4829 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
4830 (let ((b (match-end 0))
4831 (outline-regexp org-outline-regexp))
4832 (if (re-search-forward
4834 (save-excursion (outline-next-heading) (point)) t)
4835 (outline-flag-region b (point-at-eol) flag)
4836 (error ":END: line missing"))))))
4838 (defun org-subtree-end-visible-p ()
4839 "Is the end of the current subtree visible?"
4840 (pos-visible-in-window-p
4841 (save-excursion (org-end-of-subtree t) (point))))
4843 (defun org-first-headline-recenter (&optional N)
4844 "Move cursor to the first headline and recenter the headline.
4845 Optional argument N means, put the headline into the Nth line of the window."
4846 (goto-char (point-min))
4847 (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
4849 (recenter (prefix-numeric-value N))))
4853 (defvar org-goto-window-configuration nil)
4854 (defvar org-goto-marker nil)
4855 (defvar org-goto-map
4856 (let ((map (make-sparse-keymap)))
4857 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
4858 (while (setq cmd (pop cmds))
4859 (substitute-key-definition cmd cmd map global-map)))
4860 (suppress-keymap map)
4861 (org-defkey map "\C-m" 'org-goto-ret)
4862 (org-defkey map [(return)] 'org-goto-ret)
4863 (org-defkey map [(left)] 'org-goto-left)
4864 (org-defkey map [(right)] 'org-goto-right)
4865 (org-defkey map [(control ?g)] 'org-goto-quit)
4866 (org-defkey map "\C-i" 'org-cycle)
4867 (org-defkey map [(tab)] 'org-cycle)
4868 (org-defkey map [(down)] 'outline-next-visible-heading)
4869 (org-defkey map [(up)] 'outline-previous-visible-heading)
4870 (if org-goto-auto-isearch
4871 (if (fboundp 'define-key-after)
4872 (define-key-after map [t] 'org-goto-local-auto-isearch)
4874 (org-defkey map "q" 'org-goto-quit)
4875 (org-defkey map "n" 'outline-next-visible-heading)
4876 (org-defkey map "p" 'outline-previous-visible-heading)
4877 (org-defkey map "f" 'outline-forward-same-level)
4878 (org-defkey map "b" 'outline-backward-same-level)
4879 (org-defkey map "u" 'outline-up-heading))
4880 (org-defkey map "/" 'org-occur)
4881 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
4882 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
4883 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
4884 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
4885 (org-defkey map "\C-c\C-u" 'outline-up-heading)
4888 (defconst org-goto-help
4889 "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
4890 RET=jump to location [Q]uit and return to previous location
4891 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
4893 (defvar org-goto-start-pos) ; dynamically scoped parameter
4895 ;; FIXME: Docstring doe not mention both interfaces
4896 (defun org-goto (&optional alternative-interface)
4897 "Look up a different location in the current file, keeping current visibility.
4899 When you want look-up or go to a different location in a document, the
4900 fastest way is often to fold the entire buffer and then dive into the tree.
4901 This method has the disadvantage, that the previous location will be folded,
4902 which may not be what you want.
4904 This command works around this by showing a copy of the current buffer
4905 in an indirect buffer, in overview mode. You can dive into the tree in
4906 that copy, use org-occur and incremental search to find a location.
4907 When pressing RET or `Q', the command returns to the original buffer in
4908 which the visibility is still unchanged. After RET is will also jump to
4909 the location selected in the indirect buffer and expose the
4910 the headline hierarchy above."
4912 (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
4913 (org-refile-use-outline-path t)
4915 (if (not alternative-interface)
4917 (if (eq org-goto-interface 'outline)
4918 'outline-path-completion
4920 (org-goto-start-pos (point))
4922 (if (eq interface 'outline)
4923 (car (org-get-location (current-buffer) org-goto-help))
4924 (nth 3 (org-refile-get-location "Goto: ")))))
4927 (org-mark-ring-push org-goto-start-pos)
4928 (goto-char selected-point)
4929 (if (or (org-invisible-p) (org-invisible-p2))
4930 (org-show-context 'org-goto)))
4933 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
4934 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
4935 (defvar org-goto-local-auto-isearch-map) ; defined below
4937 (defun org-get-location (buf help)
4938 "Let the user select a location in the Org-mode buffer BUF.
4939 This function uses a recursive edit. It returns the selected position
4941 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
4942 (isearch-hide-immediately nil)
4943 (isearch-search-fun-function
4944 (lambda () 'org-goto-local-search-headings))
4945 (org-goto-selected-point org-goto-exit-command))
4947 (save-window-excursion
4948 (delete-other-windows)
4949 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
4952 (make-indirect-buffer (current-buffer) "*org-goto*")
4953 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
4954 (with-output-to-temp-buffer "*Help*"
4956 (org-fit-window-to-buffer (get-buffer-window "*Help*"))
4957 (setq buffer-read-only nil)
4958 (let ((org-startup-truncated t)
4959 (org-startup-folded nil)
4960 (org-startup-align-all-tables nil))
4963 (setq buffer-read-only t)
4964 (if (and (boundp 'org-goto-start-pos)
4965 (integer-or-marker-p org-goto-start-pos))
4966 (let ((org-show-hierarchy-above t)
4967 (org-show-siblings t)
4968 (org-show-following-heading t))
4969 (goto-char org-goto-start-pos)
4970 (and (org-invisible-p) (org-show-context)))
4971 (goto-char (point-min)))
4972 (let (org-special-ctrl-a/e) (org-beginning-of-line))
4973 (message "Select location and press RET")
4974 (use-local-map org-goto-map)
4977 (kill-buffer "*org-goto*")
4978 (cons org-goto-selected-point org-goto-exit-command)))
4980 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
4981 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
4982 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
4983 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
4985 (defun org-goto-local-search-headings (string bound noerror)
4986 "Search and make sure that any matches are in headlines."
4988 (while (if isearch-forward
4989 (search-forward string bound noerror)
4990 (search-backward string bound noerror))
4991 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
4992 (and (member :headline context)
4993 (not (member :tags context))))
4994 (throw 'return (point))))))
4996 (defun org-goto-local-auto-isearch ()
4999 (goto-char (point-min))
5000 (let ((keys (this-command-keys)))
5001 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
5003 (isearch-process-search-char (string-to-char keys)))))
5005 (defun org-goto-ret (&optional arg)
5006 "Finish `org-goto' by going to the new location."
5008 (setq org-goto-selected-point (point)
5009 org-goto-exit-command 'return)
5012 (defun org-goto-left ()
5013 "Finish `org-goto' by going to the new location."
5015 (if (org-on-heading-p)
5017 (beginning-of-line 1)
5018 (setq org-goto-selected-point (point)
5019 org-goto-exit-command 'left)
5021 (error "Not on a heading")))
5023 (defun org-goto-right ()
5024 "Finish `org-goto' by going to the new location."
5026 (if (org-on-heading-p)
5028 (setq org-goto-selected-point (point)
5029 org-goto-exit-command 'right)
5031 (error "Not on a heading")))
5033 (defun org-goto-quit ()
5034 "Finish `org-goto' without cursor motion."
5036 (setq org-goto-selected-point nil)
5037 (setq org-goto-exit-command 'quit)
5040 ;;; Indirect buffer display of subtrees
5042 (defvar org-indirect-dedicated-frame nil
5043 "This is the frame being used for indirect tree display.")
5044 (defvar org-last-indirect-buffer nil)
5046 (defun org-tree-to-indirect-buffer (&optional arg)
5047 "Create indirect buffer and narrow it to current subtree.
5048 With numerical prefix ARG, go up to this level and then take that tree.
5049 If ARG is negative, go up that many levels.
5050 If `org-indirect-buffer-display' is not `new-frame', the command removes the
5051 indirect buffer previously made with this command, to avoid proliferation of
5052 indirect buffers. However, when you call the command with a `C-u' prefix, or
5053 when `org-indirect-buffer-display' is `new-frame', the last buffer
5054 is kept so that you can work with several indirect buffers at the same time.
5055 If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
5056 requests that a new frame be made for the new buffer, so that the dedicated
5057 frame is not changed."
5059 (let ((cbuf (current-buffer))
5060 (cwin (selected-window))
5062 beg end level heading ibuf)
5064 (org-back-to-heading t)
5066 (setq level (org-outline-level))
5067 (if (< arg 0) (setq arg (+ level arg)))
5068 (while (> (setq level (org-outline-level)) arg)
5069 (outline-up-heading 1 t)))
5071 heading (org-get-heading))
5072 (org-end-of-subtree t) (setq end (point)))
5073 (if (and (buffer-live-p org-last-indirect-buffer)
5074 (not (eq org-indirect-buffer-display 'new-frame))
5076 (kill-buffer org-last-indirect-buffer))
5077 (setq ibuf (org-get-indirect-buffer cbuf)
5078 org-last-indirect-buffer ibuf)
5080 ((or (eq org-indirect-buffer-display 'new-frame)
5081 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
5082 (select-frame (make-frame))
5083 (delete-other-windows)
5084 (switch-to-buffer ibuf)
5085 (org-set-frame-title heading))
5086 ((eq org-indirect-buffer-display 'dedicated-frame)
5088 (select-frame (or (and org-indirect-dedicated-frame
5089 (frame-live-p org-indirect-dedicated-frame)
5090 org-indirect-dedicated-frame)
5091 (setq org-indirect-dedicated-frame (make-frame)))))
5092 (delete-other-windows)
5093 (switch-to-buffer ibuf)
5094 (org-set-frame-title (concat "Indirect: " heading)))
5095 ((eq org-indirect-buffer-display 'current-window)
5096 (switch-to-buffer ibuf))
5097 ((eq org-indirect-buffer-display 'other-window)
5098 (pop-to-buffer ibuf))
5099 (t (error "Invalid value.")))
5100 (if (featurep 'xemacs)
5101 (save-excursion (org-mode) (turn-on-font-lock)))
5102 (narrow-to-region beg end)
5105 (and (window-live-p cwin) (select-window cwin))))
5107 (defun org-get-indirect-buffer (&optional buffer)
5108 (setq buffer (or buffer (current-buffer)))
5109 (let ((n 1) (base (buffer-name buffer)) bname)
5110 (while (buffer-live-p
5111 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
5114 (make-indirect-buffer buffer bname 'clone)
5115 (error (make-indirect-buffer buffer bname)))))
5117 (defun org-set-frame-title (title)
5118 "Set the title of the current frame to the string TITLE."
5119 ;; FIXME: how to name a single frame in XEmacs???
5120 (unless (featurep 'xemacs)
5121 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
5123 ;;;; Structure editing
5125 ;;; Inserting headlines
5127 (defun org-previous-line-empty-p ()
5130 (or (beginning-of-line 0) t)
5132 (looking-at "[ \t]*$")))))
5134 (defun org-insert-heading (&optional force-heading)
5135 "Insert a new heading or item with same depth at point.
5136 If point is in a plain list and FORCE-HEADING is nil, create a new list item.
5137 If point is at the beginning of a headline, insert a sibling before the
5138 current headline. If point is not at the beginning, do not split the line,
5139 but create the new headline after the current line."
5141 (if (= (buffer-size) 0)
5143 (when (or force-heading (not (org-insert-item)))
5144 (let* ((empty-line-p nil)
5145 (head (save-excursion
5148 (org-back-to-heading)
5149 (setq empty-line-p (org-previous-line-empty-p))
5152 (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
5153 (blank (if (eq blank-a 'auto) empty-line-p blank-a))
5154 pos hide-previous previous-pos)
5156 ((and (org-on-heading-p) (bolp)
5158 (save-excursion (backward-char 1) (not (org-invisible-p)))))
5159 ;; insert before the current line
5160 (open-line (if blank 2 1)))
5164 (backward-char 1) (not (org-invisible-p)))))
5165 ;; insert right here
5168 ;; somewhere in the line
5170 (setq previous-pos (point-at-bol))
5172 (setq hide-previous (org-invisible-p)))
5173 (and org-insert-heading-respect-content (org-show-subtree))
5175 (and (org-get-alist-option org-M-RET-may-split-line 'headline)
5178 (goto-char (point-at-bol))
5179 (and (looking-at org-complex-heading-regexp)
5180 (> p (match-beginning 4)))))))
5183 (org-insert-heading-respect-content
5184 (org-end-of-subtree nil t)
5185 (or (bolp) (newline))
5186 (or (org-previous-line-empty-p)
5187 (and blank (newline)))
5193 (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
5194 (setq tags (and (match-end 2) (match-string 2)))
5196 (delete-region (match-beginning 1) (match-end 1)))
5197 (setq pos (point-at-bol))
5198 (or split (end-of-line 1))
5199 (delete-horizontal-space)
5200 (newline (if blank 2 1))
5206 (org-set-tags nil 'align))))
5208 (or split (end-of-line 1))
5209 (newline (if blank 2 1)))))))
5210 (insert head) (just-one-space)
5213 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
5214 (when (and org-insert-heading-respect-content hide-previous)
5216 (goto-char previous-pos)
5218 (run-hooks 'org-insert-heading-hook)))))
5220 (defun org-get-heading (&optional no-tags)
5221 "Return the heading of the current entry, without the stars."
5223 (org-back-to-heading t)
5226 (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
5227 "\\*+[ \t]+\\([^\r\n]*\\)"))
5228 (match-string 1) "")))
5230 (defun org-heading-components ()
5231 "Return the components of the current heading.
5232 This is a list with the following elements:
5233 - the level as an integer
5234 - the reduced level, different if `org-odd-levels-only' is set.
5235 - the TODO keyword, or nil
5236 - the priority character, like ?A, or nil if no priority is given
5237 - the headline text itself, or the tags string if no headline text
5238 - the tags string, or nil."
5240 (org-back-to-heading t)
5241 (if (looking-at org-complex-heading-regexp)
5242 (list (length (match-string 1))
5243 (org-reduced-level (length (match-string 1)))
5244 (org-match-string-no-properties 2)
5245 (and (match-end 3) (aref (match-string 3) 2))
5246 (org-match-string-no-properties 4)
5247 (org-match-string-no-properties 5)))))
5249 (defun org-insert-heading-after-current ()
5250 "Insert a new heading with same level as current, after current subtree."
5252 (org-back-to-heading)
5253 (org-insert-heading)
5254 (org-move-subtree-down)
5257 (defun org-insert-heading-respect-content ()
5259 (let ((org-insert-heading-respect-content t))
5260 (org-insert-heading t)))
5262 (defun org-insert-todo-heading-respect-content (&optional force-state)
5264 (let ((org-insert-heading-respect-content t))
5265 (org-insert-todo-heading force-state t)))
5267 (defun org-insert-todo-heading (arg &optional force-heading)
5268 "Insert a new heading with the same level and TODO state as current heading.
5269 If the heading has no TODO state, or if the state is DONE, use the first
5270 state (TODO by default). Also with prefix arg, force first state."
5272 (when (or force-heading (not (org-insert-item 'checkbox)))
5273 (org-insert-heading force-heading)
5275 (org-back-to-heading)
5276 (outline-previous-heading)
5277 (looking-at org-todo-line-regexp))
5281 (not (match-beginning 2))
5282 (member (match-string 2) org-done-keywords))
5283 (car org-todo-keywords-1)
5287 (run-hook-with-args-until-success
5288 'org-todo-get-default-hook new-mark-x nil)
5290 (beginning-of-line 1)
5291 (and (looking-at "\\*+ ") (goto-char (match-end 0))
5292 (insert new-mark " ")))
5293 (when org-provide-todo-statistics
5294 (org-update-parent-todo-statistics))))
5296 (defun org-insert-subheading (arg)
5297 "Insert a new subheading and demote it.
5298 Works for outline headings and for plain lists alike."
5300 (org-insert-heading arg)
5302 ((org-on-heading-p) (org-do-demote))
5303 ((org-at-item-p) (org-indent-item 1))))
5305 (defun org-insert-todo-subheading (arg)
5306 "Insert a new subheading with TODO keyword or checkbox and demote it.
5307 Works for outline headings and for plain lists alike."
5309 (org-insert-todo-heading arg)
5311 ((org-on-heading-p) (org-do-demote))
5312 ((org-at-item-p) (org-indent-item 1))))
5314 ;;; Promotion and Demotion
5316 (defun org-promote-subtree ()
5317 "Promote the entire subtree.
5318 See also `org-promote'."
5321 (org-map-tree 'org-promote))
5322 (org-fix-position-after-promote))
5324 (defun org-demote-subtree ()
5325 "Demote the entire subtree. See `org-demote'.
5326 See also `org-promote'."
5329 (org-map-tree 'org-demote))
5330 (org-fix-position-after-promote))
5333 (defun org-do-promote ()
5334 "Promote the current heading higher up the tree.
5335 If the region is active in `transient-mark-mode', promote all headings
5339 (if (org-region-active-p)
5340 (org-map-region 'org-promote (region-beginning) (region-end))
5342 (org-fix-position-after-promote))
5344 (defun org-do-demote ()
5345 "Demote the current heading lower down the tree.
5346 If the region is active in `transient-mark-mode', demote all headings
5350 (if (org-region-active-p)
5351 (org-map-region 'org-demote (region-beginning) (region-end))
5353 (org-fix-position-after-promote))
5355 (defun org-fix-position-after-promote ()
5356 "Make sure that after pro/demotion cursor position is right."
5357 (let ((pos (point)))
5358 (when (save-excursion
5359 (beginning-of-line 1)
5360 (looking-at org-todo-line-regexp)
5361 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
5362 (cond ((eobp) (insert " "))
5363 ((eolp) (insert " "))
5364 ((equal (char-after) ?\ ) (forward-char 1))))))
5366 (defun org-reduced-level (l)
5367 "Compute the effective level of a heading.
5368 This takes into account the setting of `org-odd-levels-only'."
5369 (if org-odd-levels-only (1+ (floor (/ l 2))) l))
5371 (defun org-get-valid-level (level &optional change)
5372 "Rectify a level change under the influence of `org-odd-levels-only'
5373 LEVEL is a current level, CHANGE is by how much the level should be
5374 modified. Even if CHANGE is nil, LEVEL may be returned modified because
5375 even level numbers will become the next higher odd number."
5376 (if org-odd-levels-only
5377 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
5378 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
5379 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
5380 (max 1 (+ level (or change 0)))))
5382 (if (boundp 'define-obsolete-function-alias)
5383 (if (or (featurep 'xemacs) (< emacs-major-version 23))
5384 (define-obsolete-function-alias 'org-get-legal-level
5385 'org-get-valid-level)
5386 (define-obsolete-function-alias 'org-get-legal-level
5387 'org-get-valid-level "23.1")))
5389 (defun org-promote ()
5390 "Promote the current heading higher up the tree.
5391 If the region is active in `transient-mark-mode', promote all headings
5393 (org-back-to-heading t)
5394 (let* ((level (save-match-data (funcall outline-level)))
5395 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
5396 (diff (abs (- level (length up-head) -1))))
5397 (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
5398 (replace-match up-head nil t)
5399 ;; Fixup tag positioning
5400 (and org-auto-align-tags (org-set-tags nil t))
5401 (if org-adapt-indentation (org-fixup-indentation (- diff)))))
5403 (defun org-demote ()
5404 "Demote the current heading lower down the tree.
5405 If the region is active in `transient-mark-mode', demote all headings
5407 (org-back-to-heading t)
5408 (let* ((level (save-match-data (funcall outline-level)))
5409 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
5410 (diff (abs (- level (length down-head) -1))))
5411 (replace-match down-head nil t)
5412 ;; Fixup tag positioning
5413 (and org-auto-align-tags (org-set-tags nil t))
5414 (if org-adapt-indentation (org-fixup-indentation diff))))
5416 (defun org-map-tree (fun)
5417 "Call FUN for every heading underneath the current one."
5418 (org-back-to-heading)
5419 (let ((level (funcall outline-level)))
5423 (outline-next-heading)
5424 (> (funcall outline-level) level))
5428 (defun org-map-region (fun beg end)
5429 "Call FUN for every heading between BEG and END."
5430 (let ((org-ignore-region t))
5432 (setq end (copy-marker end))
5434 (if (and (re-search-forward (concat "^" outline-regexp) nil t)
5438 (outline-next-heading)
5443 (defun org-fixup-indentation (diff)
5444 "Change the indentation in the current entry by DIFF
5445 However, if any line in the current entry has no indentation, or if it
5446 would end up with no indentation after the change, nothing at all is done."
5448 (let ((end (save-excursion (outline-next-heading)
5450 (prohibit (if (> diff 0)
5452 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
5454 (unless (save-excursion (end-of-line 1)
5455 (re-search-forward prohibit end t))
5456 (while (and (< (point) end)
5457 (re-search-forward "^[ \t]+" end t))
5458 (goto-char (match-end 0))
5459 (setq col (current-column))
5460 (if (< diff 0) (replace-match ""))
5461 (org-indent-to-column (+ diff col))))
5462 (move-marker end nil))))
5464 (defun org-convert-to-odd-levels ()
5465 "Convert an org-mode file with all levels allowed to one with odd levels.
5466 This will leave level 1 alone, convert level 2 to level 3, level 3 to
5469 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
5470 (let ((org-odd-levels-only nil) n)
5472 (goto-char (point-min))
5473 (while (re-search-forward "^\\*\\*+ " nil t)
5474 (setq n (- (length (match-string 0)) 2))
5475 (while (>= (setq n (1- n)) 0)
5477 (end-of-line 1))))))
5480 (defun org-convert-to-oddeven-levels ()
5481 "Convert an org-mode file with only odd levels to one with odd and even levels.
5482 This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
5483 section with an even level, conversion would destroy the structure of the file. An error
5484 is signaled in this case."
5486 (goto-char (point-min))
5487 ;; First check if there are no even levels
5488 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
5489 (org-show-context t)
5490 (error "Not all levels are odd in this file. Conversion not possible."))
5491 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
5492 (let ((org-odd-levels-only nil) n)
5494 (goto-char (point-min))
5495 (while (re-search-forward "^\\*\\*+ " nil t)
5496 (setq n (/ (1- (length (match-string 0))) 2))
5497 (while (>= (setq n (1- n)) 0)
5499 (end-of-line 1))))))
5501 (defun org-tr-level (n)
5502 "Make N odd if required."
5503 (if org-odd-levels-only (1+ (/ n 2)) n))
5505 ;;; Vertical tree motion, cutting and pasting of subtrees
5507 (defun org-move-subtree-up (&optional arg)
5508 "Move the current subtree up past ARG headlines of the same level."
5510 (org-move-subtree-down (- (prefix-numeric-value arg))))
5512 (defun org-move-subtree-down (&optional arg)
5513 "Move the current subtree down past ARG headlines of the same level."
5515 (setq arg (prefix-numeric-value arg))
5516 (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
5517 'outline-get-last-sibling))
5518 (ins-point (make-marker))
5520 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
5522 (org-back-to-heading)
5525 (setq ne-beg (org-back-over-empty-lines))
5528 (save-excursion (outline-end-of-heading)
5529 (setq folded (org-invisible-p)))
5530 (outline-end-of-subtree))
5531 (outline-next-heading)
5532 (setq ne-end (org-back-over-empty-lines))
5535 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
5536 ;; include less whitespace
5539 (forward-line (- ne-beg ne-end))
5540 (setq beg (point))))
5541 ;; Find insertion point, with error handling
5543 (or (and (funcall movfunc) (looking-at outline-regexp))
5544 (progn (goto-char beg0)
5545 (error "Cannot move past superior level or buffer limit")))
5546 (setq cnt (1- cnt)))
5548 ;; Moving forward - still need to move over subtree
5549 (progn (org-end-of-subtree t t)
5551 (org-back-over-empty-lines)
5552 (or (bolp) (newline)))))
5553 (setq ne-ins (org-back-over-empty-lines))
5554 (move-marker ins-point (point))
5555 (setq txt (buffer-substring beg end))
5556 (org-save-markers-in-region beg end)
5557 (delete-region beg end)
5558 (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
5559 (or (bobp) (outline-flag-region (1- (point)) (point) nil))
5560 (let ((bbb (point)))
5561 (insert-before-markers txt)
5562 (org-reinstall-markers-in-region bbb)
5563 (move-marker ins-point bbb))
5564 (or (bolp) (insert "\n"))
5565 (setq ins-end (point))
5566 (goto-char ins-point)
5567 (org-skip-whitespace)
5568 (when (and (< arg 0)
5569 (org-first-sibling-p)
5571 ;; Move whitespace back to beginning
5574 (let ((kill-whole-line t))
5575 (kill-line (- ne-ins ne-beg)) (point)))
5576 (insert (make-string (- ne-ins ne-beg) ?\n)))
5577 (move-marker ins-point nil)
5578 (org-compact-display-after-subtree-move)
5579 (org-show-empty-lines-in-parent)
5583 (org-cycle-hide-drawers 'children))))
5585 (defvar org-subtree-clip ""
5586 "Clipboard for cut and paste of subtrees.
5587 This is actually only a copy of the kill, because we use the normal kill
5588 ring. We need it to check if the kill was created by `org-copy-subtree'.")
5590 (defvar org-subtree-clip-folded nil
5591 "Was the last copied subtree folded?
5592 This is used to fold the tree back after pasting.")
5594 (defun org-cut-subtree (&optional n)
5595 "Cut the current subtree into the clipboard.
5596 With prefix arg N, cut this many sequential subtrees.
5597 This is a short-hand for marking the subtree and then cutting it."
5599 (org-copy-subtree n 'cut))
5601 (defun org-copy-subtree (&optional n cut force-store-markers)
5602 "Cut the current subtree into the clipboard.
5603 With prefix arg N, cut this many sequential subtrees.
5604 This is a short-hand for marking the subtree and then copying it.
5605 If CUT is non-nil, actually cut the subtree.
5606 If FORCE-STORE-MARKERS is non-nil, store the relative locations
5607 of some markers in the region, even if CUT is non-nil. This is
5608 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
5610 (let (beg end folded (beg0 (point)))
5612 (org-back-to-heading nil) ; take what looks like a subtree
5613 (org-back-to-heading t)) ; take what is really there
5614 (org-back-over-empty-lines)
5616 (skip-chars-forward " \t\r\n")
5618 (save-excursion (outline-end-of-heading)
5619 (setq folded (org-invisible-p)))
5621 (outline-forward-same-level (1- n))
5623 (org-end-of-subtree t t))
5624 (org-back-over-empty-lines)
5628 (setq org-subtree-clip-folded folded)
5629 (when (or cut force-store-markers)
5630 (org-save-markers-in-region beg end))
5631 (if cut (kill-region beg end) (copy-region-as-kill beg end))
5632 (setq org-subtree-clip (current-kill 0))
5633 (message "%s: Subtree(s) with %d characters"
5634 (if cut "Cut" "Copied")
5635 (length org-subtree-clip)))))
5637 (defun org-paste-subtree (&optional level tree for-yank)
5638 "Paste the clipboard as a subtree, with modification of headline level.
5639 The entire subtree is promoted or demoted in order to match a new headline
5642 If the cursor is at the beginning of a headline, the same level as
5643 that headline is used to paste the tree
5645 If not, the new level is derived from the *visible* headings
5646 before and after the insertion point, and taken to be the inferior headline
5647 level of the two. So if the previous visible heading is level 3 and the
5648 next is level 4 (or vice versa), level 4 will be used for insertion.
5649 This makes sure that the subtree remains an independent subtree and does
5650 not swallow low level entries.
5652 You can also force a different level, either by using a numeric prefix
5653 argument, or by inserting the heading marker by hand. For example, if the
5654 cursor is after \"*****\", then the tree will be shifted to level 5.
5656 If optional TREE is given, use this text instead of the kill ring.
5658 When FOR-YANK is set, this is called by `org-yank'. In this case, do not
5659 move back over whitespace before inserting, and move point to the end of
5660 the inserted text when done."
5662 (unless (org-kill-is-subtree-p tree)
5664 (substitute-command-keys
5665 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
5666 (let* ((visp (not (org-invisible-p)))
5667 (txt (or tree (and kill-ring (current-kill 0))))
5668 (^re (concat "^\\(" outline-regexp "\\)"))
5669 (re (concat "\\(" outline-regexp "\\)"))
5670 (^re_ (concat "\\(\\*+\\)[ \t]*"))
5672 (old-level (if (string-match ^re txt)
5673 (- (match-end 0) (match-beginning 0) 1)
5675 (force-level (cond (level (prefix-numeric-value level))
5676 ((and (looking-at "[ \t]*$")
5678 ^re_ (buffer-substring
5679 (point-at-bol) (point))))
5680 (- (match-end 1) (match-beginning 1)))
5682 (looking-at org-outline-regexp))
5683 (- (match-end 0) (point) 1))
5685 (previous-level (save-excursion
5688 (outline-previous-visible-heading 1)
5690 (- (match-end 0) (match-beginning 0) 1)
5693 (next-level (save-excursion
5696 (or (looking-at outline-regexp)
5697 (outline-next-visible-heading 1))
5699 (- (match-end 0) (match-beginning 0) 1)
5702 (new-level (or force-level (max previous-level next-level)))
5703 (shift (if (or (= old-level -1)
5705 (= old-level new-level))
5707 (- new-level old-level)))
5708 (delta (if (> shift 0) -1 1))
5709 (func (if (> shift 0) 'org-demote 'org-promote))
5710 (org-odd-levels-only nil)
5712 ;; Remove the forced level indicator
5714 (delete-region (point-at-bol) (point)))
5716 (beginning-of-line 1)
5717 (unless for-yank (org-back-over-empty-lines))
5719 (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
5720 (insert-before-markers txt)
5721 (unless (string-match "\n\\'" txt) (insert "\n"))
5722 (setq newend (point))
5723 (org-reinstall-markers-in-region beg)
5726 (skip-chars-forward " \t\n\r")
5728 (if (and (org-invisible-p) visp)
5729 (save-excursion (outline-show-heading)))
5730 ;; Shift if necessary
5733 (narrow-to-region beg end)
5734 (while (not (= shift 0))
5735 (org-map-region func (point-min) (point-max))
5736 (setq shift (+ delta shift)))
5737 (goto-char (point-min))
5738 (setq newend (point-max))))
5739 (when (or (interactive-p) for-yank)
5740 (message "Clipboard pasted as level %d subtree" new-level))
5741 (if (and (not for-yank) ; in this case, org-yank will decide about folding
5743 (eq org-subtree-clip (current-kill 0))
5744 org-subtree-clip-folded)
5745 ;; The tree was folded before it was killed/copied
5747 (and for-yank (goto-char newend))))
5749 (defun org-kill-is-subtree-p (&optional txt)
5750 "Check if the current kill is an outline subtree, or a set of trees.
5751 Returns nil if kill does not start with a headline, or if the first
5752 headline level is not the largest headline level in the tree.
5753 So this will actually accept several entries of equal levels as well,
5754 which is OK for `org-paste-subtree'.
5755 If optional TXT is given, check this string instead of the current kill."
5756 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
5757 (start-level (and kill
5758 (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
5759 org-outline-regexp "\\)")
5761 (- (match-end 2) (match-beginning 2) 1)))
5762 (re (concat "^" org-outline-regexp))
5763 (start (1+ (or (match-beginning 2) -1))))
5764 (if (not start-level)
5766 nil) ;; does not even start with a heading
5768 (while (setq start (string-match re kill (1+ start)))
5769 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
5773 (defvar org-markers-to-move nil
5774 "Markers that should be moved with a cut-and-paste operation.
5775 Those markers are stored together with their positions relative to
5776 the start of the region.")
5778 (defun org-save-markers-in-region (beg end)
5779 "Check markers in region.
5780 If these markers are between BEG and END, record their position relative
5781 to BEG, so that after moving the block of text, we can put the markers back
5783 This function gets called just before an entry or tree gets cut from the
5784 buffer. After re-insertion, `org-reinstall-markers-in-region' must be
5785 called immediately, to move the markers with the entries."
5786 (setq org-markers-to-move nil)
5787 (when (featurep 'org-clock)
5788 (org-clock-save-markers-for-cut-and-paste beg end))
5789 (when (featurep 'org-agenda)
5790 (org-agenda-save-markers-for-cut-and-paste beg end)))
5792 (defun org-check-and-save-marker (marker beg end)
5793 "Check if MARKER is between BEG and END.
5794 If yes, remember the marker and the distance to BEG."
5795 (when (and (marker-buffer marker)
5796 (equal (marker-buffer marker) (current-buffer)))
5797 (if (and (>= marker beg) (< marker end))
5798 (push (cons marker (- marker beg)) org-markers-to-move))))
5800 (defun org-reinstall-markers-in-region (beg)
5801 "Move all remembered markers to their position relative to BEG."
5803 (move-marker (car x) (+ beg (cdr x))))
5804 org-markers-to-move)
5805 (setq org-markers-to-move nil))
5807 (defun org-narrow-to-subtree ()
5808 "Narrow buffer to the current subtree."
5813 (progn (org-back-to-heading) (point))
5814 (progn (org-end-of-subtree t) (point))))))
5816 (defun org-clone-subtree-with-time-shift (n &optional shift)
5817 "Clone the task (subtree) at point N times.
5818 The clones will be inserted as siblings.
5820 In interactive use, the user will be prompted for the number of clones
5821 to be produced, and for a time SHIFT, which may be a repeater as used
5822 in time stamps, for example `+3d'.
5824 When a valid repeater is given and the entry contains any time stamps,
5825 the clones will become a sequence in time, with time stamps in the
5826 subtree shifted for each clone produced. If SHIFT is nil or the
5827 empty string, time stamps will be left alone.
5829 If the original subtree did contain time stamps with a repeater,
5830 the following will happen:
5831 - the repeater will be removed in each clone
5832 - an additional clone will be produced, with the current, unshifted
5833 date(s) in the entry.
5834 - the original entry will be placed *after* all the clones, with
5836 - the start days in the repeater in the original entry will be shifted
5837 to past the last clone.
5838 I this way you can spell out a number of instances of a repeating task,
5839 and still retain the repeater to cover future instances of the task."
5840 (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
5841 (let (beg end template task
5842 shift-n shift-what doshift nmin nmax (n-no-remove -1))
5843 (if (not (and (integerp n) (> n 0)))
5844 (error "Invalid number of replications %s" n))
5845 (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
5846 (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
5848 (error "Invalid shift specification %s" shift))
5850 (setq shift-n (string-to-number (match-string 1 shift))
5851 shift-what (cdr (assoc (match-string 2 shift)
5852 '(("d" . day) ("w" . week)
5853 ("m" . month) ("y" . year))))))
5854 (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
5855 (setq nmin 1 nmax n)
5856 (org-back-to-heading t)
5858 (org-end-of-subtree t t)
5860 (setq template (buffer-substring beg end))
5862 (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
5863 (delete-region beg end)
5865 (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
5867 (loop for n from nmin to nmax do
5869 (setq task template)
5873 (goto-char (point-min))
5874 (while (re-search-forward org-ts-regexp nil t)
5875 (org-timestamp-change (* n shift-n) shift-what))
5876 (unless (= n n-no-remove)
5877 (goto-char (point-min))
5878 (while (re-search-forward org-ts-regexp nil t)
5880 (goto-char (match-beginning 0))
5881 (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
5882 (delete-region (match-beginning 1) (match-end 1))))))
5883 (setq task (buffer-string))))
5889 (defun org-sort (with-case)
5890 "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
5891 Optional argument WITH-CASE means sort case-sensitively.
5892 With a double prefix argument, also remove duplicate entries."
5894 (if (org-at-table-p)
5895 (org-call-with-arg 'org-table-sort-lines with-case)
5896 (org-call-with-arg 'org-sort-entries-or-items with-case)))
5898 (defun org-sort-remove-invisible (s)
5899 (remove-text-properties 0 (length s) org-rm-props s)
5900 (while (string-match org-bracket-link-regexp s)
5901 (setq s (replace-match (if (match-end 2)
5903 (match-string 1 s)) t t s)))
5906 (defvar org-priority-regexp) ; defined later in the file
5908 (defun org-sort-entries-or-items
5909 (&optional with-case sorting-type getkey-func compare-func property)
5910 "Sort entries on a certain level of an outline tree.
5911 If there is an active region, the entries in the region are sorted.
5912 Else, if the cursor is before the first entry, sort the top-level items.
5913 Else, the children of the entry at point are sorted.
5915 Sorting can be alphabetically, numerically, and by date/time as given by
5916 the first time stamp in the entry. The command prompts for the sorting
5917 type unless it has been given to the function through the SORTING-TYPE
5918 argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
5919 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
5920 called with point at the beginning of the record. It must return either
5921 a string or a number that should serve as the sorting key for that record.
5923 Comparing entries ignores case by default. However, with an optional argument
5924 WITH-CASE, the sorting considers case as well."
5926 (let ((case-func (if with-case 'identity 'downcase))
5927 start beg end stars re re2
5928 txt what tmp plain-list-p)
5929 ;; Find beginning and end of region to sort
5931 ((org-region-active-p)
5932 ;; we will sort the region
5933 (setq end (region-end)
5935 (goto-char (region-beginning))
5936 (if (not (org-on-heading-p)) (outline-next-heading))
5937 (setq start (point)))
5939 ;; we will sort this plain list
5940 (org-beginning-of-item-list) (setq start (point))
5941 (org-end-of-item-list) (setq end (point))
5943 (setq plain-list-p t
5945 ((or (org-on-heading-p)
5946 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
5947 ;; we will sort the children of the current headline
5948 (org-back-to-heading)
5950 end (progn (org-end-of-subtree t t)
5951 (org-back-over-empty-lines)
5956 (outline-next-heading))
5958 ;; we will sort the top-level entries in this file
5959 (goto-char (point-min))
5960 (or (org-on-heading-p) (outline-next-heading))
5961 (setq start (point) end (point-max) what "top-level")
5966 (if (>= beg end) (error "Nothing to sort"))
5968 (unless plain-list-p
5969 (looking-at "\\(\\*+\\)")
5970 (setq stars (match-string 1)
5971 re (concat "^" (regexp-quote stars) " +")
5972 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
5973 txt (buffer-substring beg end))
5974 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
5975 (if (and (not (equal stars "*")) (string-match re2 txt))
5976 (error "Region to sort contains a level above the first entry")))
5978 (unless sorting-type
5981 "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
5982 "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:")
5984 (setq sorting-type (read-char-exclusive))
5986 (and (= (downcase sorting-type) ?f)
5988 (org-ido-completing-read "Sort using function: "
5989 obarray 'fboundp t nil nil))
5990 (setq getkey-func (intern getkey-func)))
5992 (and (= (downcase sorting-type) ?r)
5994 (org-ido-completing-read "Property: "
5995 (mapcar 'list (org-buffer-property-keys t))
5998 (message "Sorting entries...")
6001 (narrow-to-region start end)
6003 (let ((dcst (downcase sorting-type))
6004 (now (current-time)))
6006 (/= dcst sorting-type)
6007 ;; This function moves to the beginning character of the "record" to
6011 (if (org-at-item-p) t (goto-char (point-max))))
6013 (if (re-search-forward re nil t)
6014 (goto-char (match-beginning 0))
6015 (goto-char (point-max)))))
6016 ;; This function moves to the last character of the "record" being
6023 (outline-forward-same-level 1)
6025 (goto-char (point-max)))))))
6027 ;; This function returns the value that gets sorted against.
6030 (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
6033 (string-to-number (buffer-substring (match-end 0)
6036 (buffer-substring (match-end 0) (point-at-eol)))
6038 (if (re-search-forward org-ts-regexp
6040 (org-time-string-to-time (match-string 0))
6045 (setq tmp (funcall getkey-func))
6046 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
6048 (error "Invalid key function `%s'" getkey-func)))
6049 (t (error "Invalid sorting type `%c'" sorting-type)))))
6053 (if (looking-at org-complex-heading-regexp)
6054 (string-to-number (match-string 4))
6057 (if (looking-at org-complex-heading-regexp)
6058 (funcall case-func (match-string 4))
6061 (if (re-search-forward org-ts-regexp
6065 (org-time-string-to-time (match-string 0))
6068 (if (re-search-forward org-priority-regexp (point-at-eol) t)
6069 (string-to-char (match-string 2))
6070 org-default-priority))
6072 (or (org-entry-get nil property) ""))
6074 (if (looking-at org-complex-heading-regexp)
6075 (- 9999 (length (member (match-string 2)
6076 org-todo-keywords-1)))))
6080 (setq tmp (funcall getkey-func))
6081 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
6083 (error "Invalid key function `%s'" getkey-func)))
6084 (t (error "Invalid sorting type `%c'" sorting-type)))))
6087 ((= dcst ?a) 'string<)
6088 ((= dcst ?t) 'time-less-p)
6089 ((= dcst ?f) compare-func)
6091 (message "Sorting entries...done")))
6093 (defun org-do-sort (table what &optional with-case sorting-type)
6094 "Sort TABLE of WHAT according to SORTING-TYPE.
6095 The user will be prompted for the SORTING-TYPE if the call to this
6096 function does not specify it. WHAT is only for the prompt, to indicate
6097 what is being sorted. The sorting key will be extracted from
6098 the car of the elements of the table.
6099 If WITH-CASE is non-nil, the sorting will be case-sensitive."
6100 (unless sorting-type
6102 "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
6104 (setq sorting-type (read-char-exclusive)))
6105 (let ((dcst (downcase sorting-type))
6106 extractfun comparefun)
6107 ;; Define the appropriate functions
6110 (setq extractfun 'string-to-number
6111 comparefun (if (= dcst sorting-type) '< '>)))
6113 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
6114 (lambda(x) (downcase (org-sort-remove-invisible x))))
6115 comparefun (if (= dcst sorting-type)
6117 (lambda (a b) (and (not (string< a b))
6118 (not (string= a b)))))))
6122 (if (or (string-match org-ts-regexp x)
6123 (string-match org-ts-regexp-both x))
6125 (org-time-string-to-time (match-string 0 x)))
6127 comparefun (if (= dcst sorting-type) '< '>)))
6128 (t (error "Invalid sorting type `%c'" sorting-type)))
6130 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
6132 (lambda (a b) (funcall comparefun (car a) (car b))))))
6134 ;;; Editing source examples
6136 (defvar org-exit-edit-mode-map (make-sparse-keymap))
6137 (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
6138 (defvar org-edit-src-force-single-line nil)
6139 (defvar org-edit-src-from-org-mode nil)
6140 (defvar org-edit-src-picture nil)
6142 (define-minor-mode org-exit-edit-mode
6143 "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
6145 (defun org-edit-src-code ()
6146 "Edit the source code example at point.
6147 An indirect buffer is created, and that buffer is then narrowed to the
6148 example at point and switched to the correct language mode. When done,
6149 exit by killing the buffer with \\[org-edit-src-exit]."
6151 (let ((line (org-current-line))
6152 (case-fold-search t)
6153 (msg (substitute-command-keys
6154 "Edit, then exit with C-c ' (C-c and single quote)"))
6155 (info (org-edit-src-find-region-and-lang))
6156 (org-mode-p (eq major-mode 'org-mode))
6157 beg end lang lang-f single lfmt)
6160 (setq beg (nth 0 info)
6165 lang-f (intern (concat lang "-mode")))
6166 (unless (functionp lang-f)
6167 (error "No such language mode: %s" lang-f))
6169 (if (get-buffer "*Org Edit Src Example*")
6170 (kill-buffer "*Org Edit Src Example*"))
6171 (switch-to-buffer (make-indirect-buffer (current-buffer)
6172 "*Org Edit Src Example*"))
6173 (narrow-to-region beg end)
6174 (remove-text-properties beg end '(display nil invisible nil
6176 (let ((org-inhibit-startup t))
6178 (set (make-local-variable 'org-edit-src-force-single-line) single)
6179 (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
6181 (set (make-local-variable 'org-coderef-label-format) lfmt))
6183 (goto-char (point-min))
6184 (while (re-search-forward "^," nil t)
6185 (replace-match "")))
6187 (org-exit-edit-mode)
6188 (org-set-local 'header-line-format msg)
6192 (defun org-edit-fixed-width-region ()
6193 "Edit the fixed-width ascii drawing at point.
6194 This must be a region where each line starts with a colon followed by
6196 An indirect buffer is created, and that buffer is then narrowed to the
6197 example at point and switched to artist-mode. When done,
6198 exit by killing the buffer with \\[org-edit-src-exit]."
6200 (let ((line (org-current-line))
6201 (case-fold-search t)
6202 (msg (substitute-command-keys
6203 "Edit, then exit with C-c ' (C-c and single quote)"))
6204 (org-mode-p (eq major-mode 'org-mode))
6206 (beginning-of-line 1)
6207 (if (looking-at "[ \t]*[^:\n \t]")
6209 (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
6210 (setq beg (point) end beg)
6212 (if (re-search-backward "^[ \t]*[^:]" nil 'move)
6213 (setq beg (point-at-bol 2))
6214 (setq beg (point))))
6216 (if (re-search-forward "^[ \t]*[^:]" nil 'move)
6217 (setq end (1- (match-beginning 0)))
6218 (setq end (point))))
6220 (if (get-buffer "*Org Edit Picture*")
6221 (kill-buffer "*Org Edit Picture*"))
6222 (switch-to-buffer (make-indirect-buffer (current-buffer)
6223 "*Org Edit Picture*"))
6224 (narrow-to-region beg end)
6225 (remove-text-properties beg end '(display nil invisible nil
6227 (when (fboundp 'font-lock-unfontify-region)
6228 (font-lock-unfontify-region (point-min) (point-max)))
6230 ((eq org-edit-fixed-width-region-mode 'artist-mode)
6233 (t (funcall org-edit-fixed-width-region-mode)))
6234 (set (make-local-variable 'org-edit-src-force-single-line) nil)
6235 (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
6236 (set (make-local-variable 'org-edit-src-picture) t)
6237 (goto-char (point-min))
6238 (while (re-search-forward "^[ \t]*: ?" nil t)
6241 (org-exit-edit-mode)
6242 (org-set-local 'header-line-format msg)
6247 (defun org-edit-src-find-region-and-lang ()
6248 "Find the region and language for a local edit.
6249 Return a list with beginning and end of the region, a string representing
6250 the language, a switch telling of the content should be in a single line."
6253 org-edit-src-region-extra
6255 ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
6256 ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
6257 ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
6258 ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
6259 ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
6260 ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
6261 ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
6262 ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
6263 ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
6264 ("^#\\+html:" "\n" "html" single-line)
6265 ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
6266 ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
6267 ("^#\\+latex:" "\n" "latex" single-line)
6268 ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
6269 ("^#\\+ascii:" "\n" "ascii" single-line)
6272 re1 re2 single beg end lang lfmt match-re1)
6274 (while (setq entry (pop re-list))
6275 (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
6276 single (nth 3 entry))
6278 (if (or (looking-at re1)
6279 (re-search-backward re1 nil t))
6281 (setq match-re1 (match-string 0))
6282 (setq beg (match-end 0)
6283 lang (org-edit-src-get-lang lang)
6284 lfmt (org-edit-src-get-label-format match-re1))
6285 (if (and (re-search-forward re2 nil t)
6286 (>= (match-end 0) pos))
6287 (throw 'exit (list beg (match-beginning 0)
6288 lang single lfmt))))
6289 (if (or (looking-at re2)
6290 (re-search-forward re2 nil t))
6292 (setq end (match-beginning 0))
6293 (if (and (re-search-backward re1 nil t)
6294 (<= (match-beginning 0) pos))
6296 (setq lfmt (org-edit-src-get-label-format
6299 (list (match-end 0) end
6300 (org-edit-src-get-lang lang)
6301 single lfmt))))))))))))
6303 (defun org-edit-src-get-lang (lang)
6304 "Extract the src language."
6305 (let ((m (match-string 0)))
6307 ((stringp lang) lang)
6308 ((integerp lang) (match-string lang))
6309 ((and (eq lang 'lang)
6310 (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
6312 ((and (eq lang 'style)
6313 (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
6315 (t "fundamental"))))
6317 (defun org-edit-src-get-label-format (s)
6318 "Extract the label format."
6320 (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
6321 (match-string 1 s))))
6323 (defun org-edit-src-exit ()
6324 "Exit special edit and protect problematic lines."
6326 (unless (buffer-base-buffer (current-buffer))
6327 (error "This is not an indirect buffer, something is wrong..."))
6328 (unless (> (point-min) 1)
6329 (error "This buffer is not narrowed, something is wrong..."))
6330 (goto-char (point-min))
6331 (if (looking-at "[ \t\n]*\n") (replace-match ""))
6332 (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
6333 (when (org-bound-and-true-p org-edit-src-force-single-line)
6334 (goto-char (point-min))
6335 (while (re-search-forward "\n" nil t)
6336 (replace-match " "))
6337 (goto-char (point-min))
6338 (if (looking-at "\\s-*") (replace-match " "))
6339 (if (re-search-forward "\\s-+\\'" nil t)
6340 (replace-match "")))
6341 (when (org-bound-and-true-p org-edit-src-from-org-mode)
6342 (goto-char (point-min))
6343 (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
6344 (replace-match ",\\1"))
6345 (when font-lock-mode
6346 (font-lock-unfontify-region (point-min) (point-max)))
6347 (put-text-property (point-min) (point-max) 'font-lock-fontified t))
6348 (when (org-bound-and-true-p org-edit-src-picture)
6349 (untabify (point-min) (point-max))
6350 (goto-char (point-min))
6351 (while (re-search-forward "^" nil t)
6352 (replace-match ": "))
6353 (when font-lock-mode
6354 (font-lock-unfontify-region (point-min) (point-max)))
6355 (put-text-property (point-min) (point-max) 'font-lock-fontified t))
6356 (kill-buffer (current-buffer))
6357 (and (org-mode-p) (org-restart-font-lock)))
6360 ;;; The orgstruct minor mode
6362 ;; Define a minor mode which can be used in other modes in order to
6363 ;; integrate the org-mode structure editing commands.
6365 ;; This is really a hack, because the org-mode structure commands use
6366 ;; keys which normally belong to the major mode. Here is how it
6367 ;; works: The minor mode defines all the keys necessary to operate the
6368 ;; structure commands, but wraps the commands into a function which
6369 ;; tests if the cursor is currently at a headline or a plain list
6370 ;; item. If that is the case, the structure command is used,
6371 ;; temporarily setting many Org-mode variables like regular
6372 ;; expressions for filling etc. However, when any of those keys is
6373 ;; used at a different location, function uses `key-binding' to look
6374 ;; up if the key has an associated command in another currently active
6375 ;; keymap (minor modes, major mode, global), and executes that
6376 ;; command. There might be problems if any of the keys is otherwise
6377 ;; used as a prefix key.
6379 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
6380 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
6381 ;; addresses this by checking explicitly for both bindings.
6383 (defvar orgstruct-mode-map (make-sparse-keymap)
6384 "Keymap for the minor `orgstruct-mode'.")
6386 (defvar org-local-vars nil
6387 "List of local variables, for use by `orgstruct-mode'")
6390 (define-minor-mode orgstruct-mode
6391 "Toggle the minor more `orgstruct-mode'.
6392 This mode is for using Org-mode structure commands in other modes.
6393 The following key behave as if Org-mode was active, if the cursor
6394 is on a headline, or on a plain list item (both in the definition
6397 M-up Move entry/item up
6398 M-down Move entry/item down
6401 M-S-up Move entry/item up
6402 M-S-down Move entry/item down
6403 M-S-left Promote subtree
6404 M-S-right Demote subtree
6405 M-q Fill paragraph and items like in Org-mode
6407 C-c - Cycle list bullet
6408 TAB Cycle item visibility
6409 M-RET Insert new heading/item
6410 S-M-RET Insert new TODO heading / Checkbox item
6411 C-c C-c Set tags / toggle checkbox"
6412 nil " OrgStruct" nil
6413 (org-load-modules-maybe)
6414 (and (orgstruct-setup) (defun orgstruct-setup () nil)))
6417 (defun turn-on-orgstruct ()
6418 "Unconditionally turn on `orgstruct-mode'."
6421 (defun orgstruct++-mode (&optional arg)
6422 "Toggle `orgstruct-mode', the enhanced version of it.
6423 In addition to setting orgstruct-mode, this also exports all indentation and
6424 autofilling variables from org-mode into the buffer. It will also
6425 recognize item context in multiline items.
6426 Note that turning off orgstruct-mode will *not* remove the
6427 indentation/paragraph settings. This can only be done by refreshing the
6428 major mode, for example with \[normal-mode]."
6430 (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
6438 "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
6439 (symbol-name (car x)))
6440 (setq var (car x) val (nth 1 x))
6441 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
6443 (org-set-local 'orgstruct-is-++ t))))
6445 (defvar orgstruct-is-++ nil
6446 "Is orgstruct-mode in ++ version in the current-buffer?")
6447 (make-variable-buffer-local 'orgstruct-is-++)
6450 (defun turn-on-orgstruct++ ()
6451 "Unconditionally turn on `orgstruct++-mode'."
6452 (orgstruct++-mode 1))
6454 (defun orgstruct-error ()
6455 "Error when there is no default binding for a structure key."
6457 (error "This key has no function outside structure elements"))
6459 (defun orgstruct-setup ()
6460 "Setup orgstruct keymaps."
6464 '([(meta up)] org-metaup)
6465 '([(meta down)] org-metadown)
6466 '([(meta left)] org-metaleft)
6467 '([(meta right)] org-metaright)
6468 '([(meta shift up)] org-shiftmetaup)
6469 '([(meta shift down)] org-shiftmetadown)
6470 '([(meta shift left)] org-shiftmetaleft)
6471 '([(meta shift right)] org-shiftmetaright)
6472 '([(shift up)] org-shiftup)
6473 '([(shift down)] org-shiftdown)
6474 '([(shift left)] org-shiftleft)
6475 '([(shift right)] org-shiftright)
6476 '("\C-c\C-c" org-ctrl-c-ctrl-c)
6477 '("\M-q" fill-paragraph)
6479 '("\C-c-" org-cycle-list-bullet)))
6481 (while (setq elt (pop bindings))
6482 (setq nfunc (1+ nfunc))
6483 (setq key (org-key (car elt))
6485 cmd (orgstruct-make-binding fun nfunc key))
6486 (org-defkey orgstruct-mode-map key cmd))
6488 ;; Special treatment needed for TAB and RET
6489 (org-defkey orgstruct-mode-map [(tab)]
6490 (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
6491 (org-defkey orgstruct-mode-map "\C-i"
6492 (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
6494 (org-defkey orgstruct-mode-map "\M-\C-m"
6495 (orgstruct-make-binding 'org-insert-heading 105
6496 "\M-\C-m" [(meta return)]))
6497 (org-defkey orgstruct-mode-map [(meta return)]
6498 (orgstruct-make-binding 'org-insert-heading 106
6499 [(meta return)] "\M-\C-m"))
6501 (org-defkey orgstruct-mode-map [(shift meta return)]
6502 (orgstruct-make-binding 'org-insert-todo-heading 107
6503 [(meta return)] "\M-\C-m"))
6505 (unless org-local-vars
6506 (setq org-local-vars (org-get-local-variables)))
6510 (defun orgstruct-make-binding (fun n &rest keys)
6511 "Create a function for binding in the structure minor mode.
6512 FUN is the command to call inside a table. N is used to create a unique
6513 command name. KEYS are keys that should be checked in for a command
6514 to execute outside of tables."
6517 (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
6519 (concat "In Structure, run `" (symbol-name fun) "'.\n"
6520 "Outside of structure, run the binding of `"
6521 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
6525 `(org-context-p 'headline 'item
6526 (and orgstruct-is-++
6527 ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
6529 (list 'org-run-like-in-org-mode (list 'quote fun))
6530 (list 'let '(orgstruct-mode)
6531 (list 'call-interactively
6534 (list 'key-binding k))
6536 '('orgstruct-error))))))))
6538 (defun org-context-p (&rest contexts)
6539 "Check if local context is any of CONTEXTS.
6540 Possible values in the list of contexts are `table', `headline', and `item'."
6541 (let ((pos (point)))
6542 (goto-char (point-at-bol))
6543 (prog1 (or (and (memq 'table contexts)
6544 (looking-at "[ \t]*|"))
6545 (and (memq 'headline contexts)
6546 ;;????????? (looking-at "\\*+"))
6547 (looking-at outline-regexp))
6548 (and (memq 'item contexts)
6549 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
6550 (and (memq 'item-body contexts)
6554 (defun org-get-local-variables ()
6555 "Return a list of all local variables in an org-mode buffer."
6557 (with-current-buffer (get-buffer-create "*Org tmp*")
6560 (setq varlist (buffer-local-variables)))
6561 (kill-buffer "*Org tmp*")
6568 (list (car x) (list 'quote (cdr x)))))
6570 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
6571 (symbol-name (car x)))
6576 (defun org-run-like-in-org-mode (cmd)
6577 "Run a command, pretending that the current buffer is in Org-mode.
6578 This will temporarily bind local variables that are typically bound in
6579 Org-mode to the values they have in Org-mode, and then interactively
6581 (org-load-modules-maybe)
6582 (unless org-local-vars
6583 (setq org-local-vars (org-get-local-variables)))
6584 (eval (list 'let org-local-vars
6585 (list 'call-interactively (list 'quote cmd)))))
6589 (defun org-get-category (&optional pos)
6590 "Get the category applying to position POS."
6591 (get-text-property (or pos (point)) 'org-category))
6593 (defun org-refresh-category-properties ()
6594 "Refresh category text properties in the buffer."
6595 (let ((def-cat (cond
6596 ((null org-category)
6597 (if buffer-file-name
6598 (file-name-sans-extension
6599 (file-name-nondirectory buffer-file-name))
6601 ((symbolp org-category) (symbol-name org-category))
6603 beg end cat pos optionp)
6608 (goto-char (point-min))
6609 (put-text-property (point) (point-max) 'org-category def-cat)
6610 (while (re-search-forward
6611 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
6612 (setq pos (match-end 0)
6613 optionp (equal (char-after (match-beginning 0)) ?#)
6614 cat (org-trim (match-string 2)))
6616 (setq beg (point-at-bol) end (point-max))
6617 (org-back-to-heading t)
6618 (setq beg (point) end (org-end-of-subtree t t)))
6619 (put-text-property beg end 'org-category cat)
6620 (goto-char pos)))))))
6625 ;;; Link abbreviations
6627 (defun org-link-expand-abbrev (link)
6628 "Apply replacements as defined in `org-link-abbrev-alist."
6629 (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
6630 (let* ((key (match-string 1 link))
6631 (as (or (assoc key org-link-abbrev-alist-local)
6632 (assoc key org-link-abbrev-alist)))
6633 (tag (and (match-end 2) (match-string 3 link)))
6639 ((symbolp rpl) (funcall rpl tag))
6640 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
6641 ((string-match "%h" rpl)
6642 (replace-match (url-hexify-string (or tag "")) t t rpl))
6643 (t (concat rpl tag)))))
6646 ;;; Storing and inserting links
6648 (defvar org-insert-link-history nil
6649 "Minibuffer history for links inserted with `org-insert-link'.")
6651 (defvar org-stored-links nil
6652 "Contains the links stored with `org-store-link'.")
6654 (defvar org-store-link-plist nil
6655 "Plist with info about the most recently link created with `org-store-link'.")
6657 (defvar org-link-protocols nil
6658 "Link protocols added to Org-mode using `org-add-link-type'.")
6660 (defvar org-store-link-functions nil
6661 "List of functions that are called to create and store a link.
6662 Each function will be called in turn until one returns a non-nil
6663 value. Each function should check if it is responsible for creating
6664 this link (for example by looking at the major mode).
6665 If not, it must exit and return nil.
6666 If yes, it should return a non-nil value after a calling
6667 `org-store-link-props' with a list of properties and values.
6668 Special properties are:
6670 :type The link prefix. like \"http\". This must be given.
6671 :link The link, like \"http://www.astro.uva.nl/~dominik\".
6672 This is obligatory as well.
6673 :description Optional default description for the second pair
6674 of brackets in an Org-mode link. The user can still change
6675 this when inserting this link into an Org-mode buffer.
6677 In addition to these, any additional properties can be specified
6678 and then used in remember templates.")
6680 (defun org-add-link-type (type &optional follow export)
6681 "Add TYPE to the list of `org-link-types'.
6682 Re-compute all regular expressions depending on `org-link-types'
6684 FOLLOW and EXPORT are two functions.
6686 FOLLOW should take the link path as the single argument and do whatever
6687 is necessary to follow the link, for example find a file or display
6690 EXPORT should format the link path for export to one of the export formats.
6691 It should be a function accepting three arguments:
6693 path the path of the link, the text after the prefix (like \"http:\")
6694 desc the description of the link, if any, nil if there was no description
6695 format the export format, a symbol like `html' or `latex'.
6697 The function may use the FORMAT information to return different values
6698 depending on the format. The return value will be put literally into
6700 Org-mode has a built-in default for exporting links. If you are happy with
6701 this default, there is no need to define an export function for the link
6702 type. For a simple example of an export function, see `org-bbdb.el'."
6703 (add-to-list 'org-link-types type t)
6704 (org-make-link-regexps)
6705 (if (assoc type org-link-protocols)
6706 (setcdr (assoc type org-link-protocols) (list follow export))
6707 (push (list type follow export) org-link-protocols)))
6710 (defun org-store-link (arg)
6711 "\\<org-mode-map>Store an org-link to the current location.
6712 This link is added to `org-stored-links' and can later be inserted
6713 into an org-buffer with \\[org-insert-link].
6715 For some link types, a prefix arg is interpreted:
6716 For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
6717 For file links, arg negates `org-context-in-file-links'."
6719 (org-load-modules-maybe)
6720 (setq org-store-link-plist nil) ; reset
6721 (let (link cpltxt desc description search txt)
6724 ((run-hook-with-args-until-success 'org-store-link-functions)
6725 (setq link (plist-get org-store-link-plist :link)
6726 desc (or (plist-get org-store-link-plist :description) link)))
6728 ((equal (buffer-name) "*Org Edit Src Example*")
6730 (while (or (not label)
6734 (goto-char (point-min))
6736 (regexp-quote (format org-coderef-label-format label))
6738 (when label (message "Label exists already") (sit-for 2))
6739 (setq label (read-string "Code line label: " label)))
6741 (setq link (format org-coderef-label-format label))
6742 (setq gc (- 79 (length link)))
6743 (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
6745 (setq link (concat "(" label ")") desc nil)))
6747 ((eq major-mode 'calendar-mode)
6748 (let ((cd (calendar-cursor-to-date)))
6751 (car org-time-stamp-formats)
6753 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
6755 (org-store-link-props :type "calendar" :date cd)))
6757 ((eq major-mode 'w3-mode)
6758 (setq cpltxt (url-view-url t)
6759 link (org-make-link cpltxt))
6760 (org-store-link-props :type "w3" :url (url-view-url t)))
6762 ((eq major-mode 'w3m-mode)
6763 (setq cpltxt (or w3m-current-title w3m-current-url)
6764 link (org-make-link w3m-current-url))
6765 (org-store-link-props :type "w3m" :url (url-view-url t)))
6767 ((setq search (run-hook-with-args-until-success
6768 'org-create-file-search-functions))
6769 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
6771 (setq cpltxt (or description link)))
6773 ((eq major-mode 'image-mode)
6774 (setq cpltxt (concat "file:"
6775 (abbreviate-file-name buffer-file-name))
6776 link (org-make-link cpltxt))
6777 (org-store-link-props :type "image" :file buffer-file-name))
6779 ((eq major-mode 'dired-mode)
6780 ;; link to the file in the current line
6781 (setq cpltxt (concat "file:"
6782 (abbreviate-file-name
6784 (dired-get-filename nil t))))
6785 link (org-make-link cpltxt)))
6787 ((and buffer-file-name (org-mode-p))
6789 ((org-in-regexp "<<\\(.*?\\)>>")
6792 (abbreviate-file-name buffer-file-name)
6793 "::" (match-string 1))
6794 link (org-make-link cpltxt)))
6795 ((and (featurep 'org-id)
6796 (or (eq org-link-to-org-use-id t)
6797 (and (eq org-link-to-org-use-id 'create-if-interactive)
6799 (and org-link-to-org-use-id
6801 (org-entry-get nil "ID")
6803 ;; We can make a link using the ID.
6804 (setq link (condition-case nil
6805 (prog1 (org-id-store-link)
6806 (setq desc (plist-get org-store-link-plist
6809 ;; probably before first headline, link to file only
6811 (abbreviate-file-name buffer-file-name))))))
6813 ;; Just link to current headline
6814 (setq cpltxt (concat "file:"
6815 (abbreviate-file-name buffer-file-name)))
6816 ;; Add a context search string
6817 (when (org-xor org-context-in-file-links arg)
6819 ((org-on-heading-p) nil)
6820 ((org-region-active-p)
6821 (buffer-substring (region-beginning) (region-end)))
6823 (when (or (null txt) (string-match "\\S-" txt))
6827 (org-make-org-heading-search-string txt)
6830 (if (string-match "::\\'" cpltxt)
6831 (setq cpltxt (substring cpltxt 0 -2)))
6832 (setq link (org-make-link cpltxt)))))
6834 ((buffer-file-name (buffer-base-buffer))
6835 ;; Just link to this file here.
6836 (setq cpltxt (concat "file:"
6837 (abbreviate-file-name
6838 (buffer-file-name (buffer-base-buffer)))))
6839 ;; Add a context string
6840 (when (org-xor org-context-in-file-links arg)
6841 (setq txt (if (org-region-active-p)
6842 (buffer-substring (region-beginning) (region-end))
6843 (buffer-substring (point-at-bol) (point-at-eol))))
6844 ;; Only use search option if there is some text.
6845 (when (string-match "\\S-" txt)
6847 (concat cpltxt "::" (org-make-org-heading-search-string txt))
6849 (setq link (org-make-link cpltxt)))
6852 (error "Cannot link to a buffer which is not visiting a file"))
6854 (t (setq link nil)))
6856 (if (consp link) (setq cpltxt (car link) link (cdr link)))
6857 (setq link (or link cpltxt)
6858 desc (or desc cpltxt))
6859 (if (equal desc "NONE") (setq desc nil))
6861 (if (and (interactive-p) link)
6863 (setq org-stored-links
6864 (cons (list link desc) org-stored-links))
6865 (message "Stored: %s" (or desc link)))
6866 (and link (org-make-link-string link desc)))))
6868 (defun org-store-link-props (&rest plist)
6869 "Store link properties, extract names and addresses."
6871 (when (setq x (plist-get plist :from))
6872 (setq adr (mail-extract-address-components x))
6873 (setq plist (plist-put plist :fromname (car adr)))
6874 (setq plist (plist-put plist :fromaddress (nth 1 adr))))
6875 (when (setq x (plist-get plist :to))
6876 (setq adr (mail-extract-address-components x))
6877 (setq plist (plist-put plist :toname (car adr)))
6878 (setq plist (plist-put plist :toaddress (nth 1 adr)))))
6879 (let ((from (plist-get plist :from))
6880 (to (plist-get plist :to)))
6881 (when (and from to org-from-is-user-regexp)
6883 (plist-put plist :fromto
6884 (if (string-match org-from-is-user-regexp from)
6886 (concat "from %f"))))))
6887 (setq org-store-link-plist plist))
6889 (defun org-add-link-props (&rest plist)
6890 "Add these properties to the link property list."
6893 (setq key (pop plist) value (pop plist))
6894 (setq org-store-link-plist
6895 (plist-put org-store-link-plist key value)))))
6897 (defun org-email-link-description (&optional fmt)
6898 "Return the description part of an email link.
6899 This takes information from `org-store-link-plist' and formats it
6900 according to FMT (default from `org-email-link-description-format')."
6901 (setq fmt (or fmt org-email-link-description-format))
6902 (let* ((p org-store-link-plist)
6903 (to (plist-get p :toaddress))
6904 (from (plist-get p :fromaddress))
6907 (cons "%c" (plist-get p :fromto))
6908 (cons "%F" (plist-get p :from))
6909 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
6910 (cons "%T" (plist-get p :to))
6911 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
6912 (cons "%s" (plist-get p :subject))
6913 (cons "%m" (plist-get p :message-id)))))
6914 (when (string-match "%c" fmt)
6915 ;; Check if the user wrote this message
6916 (if (and org-from-is-user-regexp from to
6917 (save-match-data (string-match org-from-is-user-regexp from)))
6918 (setq fmt (replace-match "to %t" t t fmt))
6919 (setq fmt (replace-match "from %f" t t fmt))))
6920 (org-replace-escapes fmt table)))
6922 (defun org-make-org-heading-search-string (&optional string heading)
6923 "Make search string for STRING or current headline."
6925 (let ((s (or string (org-get-heading))))
6926 (unless (and string (not heading))
6927 ;; We are using a headline, clean up garbage in there.
6928 (if (string-match org-todo-regexp s)
6929 (setq s (replace-match "" t t s)))
6930 (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
6931 (setq s (replace-match "" t t s)))
6932 (setq s (org-trim s))
6933 (if (string-match (concat "^\\(" org-quote-string "\\|"
6934 org-comment-string "\\)") s)
6935 (setq s (replace-match "" t t s)))
6936 (while (string-match org-ts-regexp s)
6937 (setq s (replace-match "" t t s))))
6938 (while (string-match "[^a-zA-Z_0-9 \t]+" s)
6939 (setq s (replace-match " " t t s)))
6940 (or string (setq s (concat "*" s))) ; Add * for headlines
6941 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
6943 (defun org-make-link (&rest strings)
6944 "Concatenate STRINGS."
6945 (apply 'concat strings))
6947 (defun org-make-link-string (link &optional description)
6948 "Make a link with brackets, consisting of LINK and DESCRIPTION."
6949 (unless (string-match "\\S-" link)
6950 (error "Empty link"))
6951 (when (stringp description)
6952 ;; Remove brackets from the description, they are fatal.
6953 (while (string-match "\\[" description)
6954 (setq description (replace-match "{" t t description)))
6955 (while (string-match "\\]" description)
6956 (setq description (replace-match "}" t t description))))
6957 (when (equal (org-link-escape link) description)
6958 ;; No description needed, it is identical
6959 (setq description nil))
6960 (when (and (not description)
6961 (not (equal link (org-link-escape link))))
6962 (setq description (org-extract-attributes link)))
6963 (concat "[[" (org-link-escape link) "]"
6964 (if description (concat "[" description "]") "")
6967 (defconst org-link-escape-chars
6971 (?\340 . "%E0") ; `a
6972 (?\342 . "%E2") ; ^a
6973 (?\347 . "%E7") ; ,c
6974 (?\350 . "%E8") ; `e
6975 (?\351 . "%E9") ; 'e
6976 (?\352 . "%EA") ; ^e
6977 (?\356 . "%EE") ; ^i
6978 (?\364 . "%F4") ; ^o
6979 (?\371 . "%F9") ; `u
6980 (?\373 . "%FB") ; ^u
6986 "Association list of escapes for some characters problematic in links.
6987 This is the list that is used for internal purposes.")
6989 (defconst org-link-escape-chars-browser
6990 '((?\ . "%20")) ; 32 for the SPC char
6991 "Association list of escapes for some characters problematic in links.
6992 This is the list that is used before handing over to the browser.")
6994 (defun org-link-escape (text &optional table)
6995 "Escape characters in TEXT that are problematic for links."
6996 (setq table (or table org-link-escape-chars))
6998 (let ((re (mapconcat (lambda (x) (regexp-quote
6999 (char-to-string (car x))))
7001 (while (string-match re text)
7004 (cdr (assoc (string-to-char (match-string 0 text))
7009 (defun org-link-unescape (text &optional table)
7010 "Reverse the action of `org-link-escape'."
7011 (setq table (or table org-link-escape-chars))
7013 (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
7015 (while (string-match re text)
7018 (char-to-string (car (rassoc (match-string 0 text) table)))
7022 (defun org-xor (a b)
7026 (defun org-fixup-message-id-for-http (s)
7027 "Replace special characters in a message id, so it can be used in an http query."
7028 (while (string-match "<" s)
7029 (setq s (replace-match "%3C" t t s)))
7030 (while (string-match ">" s)
7031 (setq s (replace-match "%3E" t t s)))
7032 (while (string-match "@" s)
7033 (setq s (replace-match "%40" t t s)))
7037 (defun org-insert-link-global ()
7038 "Insert a link like Org-mode does.
7039 This command can be called in any mode to insert a link in Org-mode syntax."
7041 (org-load-modules-maybe)
7042 (org-run-like-in-org-mode 'org-insert-link))
7044 (defun org-insert-link (&optional complete-file link-location)
7045 "Insert a link. At the prompt, enter the link.
7047 Completion can be used to insert any of the link protocol prefixes like
7050 The history can be used to select a link previously stored with
7051 `org-store-link'. When the empty string is entered (i.e. if you just
7052 press RET at the prompt), the link defaults to the most recently
7053 stored link. As SPC triggers completion in the minibuffer, you need to
7054 use M-SPC or C-q SPC to force the insertion of a space character.
7056 You will also be prompted for a description, and if one is given, it will
7057 be displayed in the buffer instead of the link.
7059 If there is already a link at point, this command will allow you to edit link
7060 and description parts.
7062 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
7063 be selected using completion. The path to the file will be relative to the
7064 current directory if the file is in the current directory or a subdirectory.
7065 Otherwise, the link will be the absolute path as completed in the minibuffer
7066 \(i.e. normally ~/path/to/file). You can configure this behavior using the
7067 option `org-link-file-path-type'.
7069 With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
7070 the current directory or below.
7072 With three \\[universal-argument] prefixes, negate the meaning of
7073 `org-keep-stored-link-after-insertion'.
7075 If `org-make-link-description-function' is non-nil, this function will be
7076 called with the link target, and the result will be the default
7079 If the LINK-LOCATION parameter is non-nil, this value will be
7080 used as the link location instead of reading one interactively."
7082 (let* ((wcf (current-window-configuration))
7083 (region (if (org-region-active-p)
7084 (buffer-substring (region-beginning) (region-end))))
7085 (remove (and region (list (region-beginning) (region-end))))
7087 tmphist ; byte-compile incorrectly complains about this
7088 (link link-location)
7091 (link-location) ; specified by arg, just use it.
7092 ((org-in-regexp org-bracket-link-regexp 1)
7093 ;; We do have a link at point, and we are going to edit it.
7094 (setq remove (list (match-beginning 0) (match-end 0)))
7095 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
7096 (setq link (read-string "Link: "
7098 (org-match-string-no-properties 1)))))
7099 ((or (org-in-regexp org-angle-link-re)
7100 (org-in-regexp org-plain-link-re))
7101 ;; Convert to bracket link
7102 (setq remove (list (match-beginning 0) (match-end 0))
7103 link (read-string "Link: "
7104 (org-remove-angle-brackets (match-string 0)))))
7105 ((member complete-file '((4) (16)))
7106 ;; Completing read for file names.
7107 (setq file (read-file-name "File: "))
7108 (let ((pwd (file-name-as-directory (expand-file-name ".")))
7109 (pwd1 (file-name-as-directory (abbreviate-file-name
7110 (expand-file-name ".")))))
7112 ((equal complete-file '(16))
7113 (setq link (org-make-link
7115 (abbreviate-file-name (expand-file-name file)))))
7116 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
7117 (setq link (org-make-link "file:" (match-string 1 file))))
7118 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
7119 (expand-file-name file))
7120 (setq link (org-make-link
7121 "file:" (match-string 1 (expand-file-name file)))))
7122 (t (setq link (org-make-link "file:" file))))))
7124 ;; Read link, with completion for stored links.
7125 (with-output-to-temp-buffer "*Org Links*"
7126 (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
7127 (when org-stored-links
7128 (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
7131 (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
7132 (reverse org-stored-links) "\n"))))
7133 (let ((cw (selected-window)))
7134 (select-window (get-buffer-window "*Org Links*"))
7135 (setq truncate-lines t)
7136 (org-fit-window-to-buffer)
7138 ;; Fake a link history, containing the stored links.
7139 (setq tmphist (append (mapcar 'car org-stored-links)
7140 org-insert-link-history))
7143 (let ((org-completion-use-ido nil))
7144 (org-completing-read
7147 (mapcar (lambda (x) (list (concat (car x) ":")))
7148 (append org-link-abbrev-alist-local org-link-abbrev-alist))
7149 (mapcar (lambda (x) (list (concat x ":")))
7153 (or (car (car org-stored-links))))))
7154 (set-window-configuration wcf)
7155 (kill-buffer "*Org Links*"))
7156 (setq entry (assoc link org-stored-links))
7157 (or entry (push link org-insert-link-history))
7158 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
7159 (not org-keep-stored-link-after-insertion))
7160 (setq org-stored-links (delq (assoc link org-stored-links)
7162 (setq desc (or desc (nth 1 entry)))))
7164 (if (string-match org-plain-link-re link)
7165 ;; URL-like link, normalize the use of angular brackets.
7166 (setq link (org-make-link (org-remove-angle-brackets link))))
7168 ;; Check if we are linking to the current file with a search option
7169 ;; If yes, simplify the link by using only the search option.
7170 (when (and buffer-file-name
7171 (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
7172 (let* ((path (match-string 1 link))
7173 (case-fold-search nil)
7174 (search (match-string 2 link)))
7176 (if (equal (file-truename buffer-file-name) (file-truename path))
7177 ;; We are linking to this same file, with a search option
7178 (setq link search)))))
7180 ;; Check if we can/should use a relative path. If yes, simplify the link
7181 (when (string-match "^file:\\(.*\\)" link)
7182 (let* ((path (match-string 1 link))
7184 (case-fold-search nil))
7186 ((or (eq org-link-file-path-type 'absolute)
7187 (equal complete-file '(16)))
7188 (setq path (abbreviate-file-name (expand-file-name path))))
7189 ((eq org-link-file-path-type 'noabbrev)
7190 (setq path (expand-file-name path)))
7191 ((eq org-link-file-path-type 'relative)
7192 (setq path (file-relative-name path)))
7195 (if (string-match (concat "^" (regexp-quote
7196 (file-name-as-directory
7197 (expand-file-name "."))))
7198 (expand-file-name path))
7199 ;; We are linking a file with relative path name.
7200 (setq path (substring (expand-file-name path)
7202 (setq path (abbreviate-file-name (expand-file-name path)))))))
7203 (setq link (concat "file:" path))
7204 (if (equal desc origpath)
7207 (if org-make-link-description-function
7208 (setq desc (funcall org-make-link-description-function link desc)))
7210 (setq desc (read-string "Description: " desc))
7211 (unless (string-match "\\S-" desc) (setq desc nil))
7212 (if remove (apply 'delete-region remove))
7213 (insert (org-make-link-string link desc))))
7215 (defun org-completing-read (&rest args)
7216 "Completing-read with SPACE being a normal character."
7217 (let ((minibuffer-local-completion-map
7218 (copy-keymap minibuffer-local-completion-map)))
7219 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
7220 (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
7221 (apply 'org-ido-completing-read args)))
7223 (defun org-completing-read-no-ido (&rest args)
7224 (let (org-completion-use-ido)
7225 (apply 'org-completing-read args)))
7227 (defun org-ido-completing-read (&rest args)
7228 "Completing-read using `ido-mode' speedups if available"
7229 (if (and org-completion-use-ido
7230 (fboundp 'ido-completing-read)
7231 (boundp 'ido-mode) ido-mode
7232 (listp (second args)))
7233 (let ((ido-enter-matching-directory nil))
7234 (apply 'ido-completing-read (concat (car args))
7235 (mapcar (lambda (x) (car x)) (nth 1 args))
7237 (apply 'completing-read args)))
7240 (defun org-extract-attributes (s)
7241 "Extract the attributes cookie from a string and set as text property."
7242 (let (a attr (start 0) key value)
7244 (when (string-match "{{\\([^}]+\\)}}$" s)
7245 (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
7246 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
7247 (setq key (match-string 1 a) value (match-string 2 a)
7249 attr (plist-put attr (intern key) value))))
7250 (org-add-props s nil 'org-attr attr))
7253 (defun org-attributes-to-string (plist)
7254 "Format a property list into an HTML attribute list."
7255 (let ((s "") key value)
7257 (setq key (pop plist) value (pop plist))
7259 (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
7262 ;;; Opening/following a link
7264 (defvar org-link-search-failed nil)
7266 (defun org-next-link ()
7267 "Move forward to the next link.
7268 If the link is in hidden text, expose it."
7270 (when (and org-link-search-failed (eq this-command last-command))
7271 (goto-char (point-min))
7272 (message "Link search wrapped back to beginning of buffer"))
7273 (setq org-link-search-failed nil)
7274 (let* ((pos (point))
7276 (a (assoc :link ct)))
7277 (if a (goto-char (nth 2 a)))
7278 (if (re-search-forward org-any-link-re nil t)
7280 (goto-char (match-beginning 0))
7281 (if (org-invisible-p) (org-show-context)))
7283 (setq org-link-search-failed t)
7284 (error "No further link found"))))
7286 (defun org-previous-link ()
7287 "Move backward to the previous link.
7288 If the link is in hidden text, expose it."
7290 (when (and org-link-search-failed (eq this-command last-command))
7291 (goto-char (point-max))
7292 (message "Link search wrapped back to end of buffer"))
7293 (setq org-link-search-failed nil)
7294 (let* ((pos (point))
7296 (a (assoc :link ct)))
7297 (if a (goto-char (nth 1 a)))
7298 (if (re-search-backward org-any-link-re nil t)
7300 (goto-char (match-beginning 0))
7301 (if (org-invisible-p) (org-show-context)))
7303 (setq org-link-search-failed t)
7304 (error "No further link found"))))
7306 (defun org-translate-link (s)
7307 "Translate a link string if a translation function has been defined."
7308 (if (and org-link-translation-function
7309 (fboundp org-link-translation-function)
7310 (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
7312 (setq s (funcall org-link-translation-function
7313 (match-string 1) (match-string 2)))
7314 (concat (car s) ":" (cdr s)))
7317 (defun org-translate-link-from-planner (type path)
7318 "Translate a link from Emacs Planner syntax so that Org can follow it.
7319 This is still an experimental function, your mileage may vary."
7321 ((member type '("http" "https" "news" "ftp"))
7322 ;; standard Internet links are the same.
7324 ((and (equal type "irc") (string-match "^//" path))
7325 ;; Planner has two / at the beginning of an irc link, we have 1.
7326 ;; We should have zero, actually....
7327 (setq path (substring path 1)))
7328 ((and (equal type "lisp") (string-match "^/" path))
7329 ;; Planner has a slash, we do not.
7330 (setq type "elisp" path (substring path 1)))
7331 ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
7332 ;; A typical message link. Planner has the id after the fina slash,
7333 ;; we separate it with a hash mark
7334 (setq path (concat (match-string 1 path) "#"
7335 (org-remove-angle-brackets (match-string 2 path)))))
7339 (defun org-find-file-at-mouse (ev)
7340 "Open file link or URL at mouse."
7342 (mouse-set-point ev)
7343 (org-open-at-point 'in-emacs))
7345 (defun org-open-at-mouse (ev)
7346 "Open file link or URL at mouse."
7348 (mouse-set-point ev)
7349 (if (eq major-mode 'org-agenda-mode)
7350 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
7351 (org-open-at-point))
7353 (defvar org-window-config-before-follow-link nil
7354 "The window configuration before following a link.
7355 This is saved in case the need arises to restore it.")
7357 (defvar org-open-link-marker (make-marker)
7358 "Marker pointing to the location where `org-open-at-point; was called.")
7361 (defun org-open-at-point-global ()
7362 "Follow a link like Org-mode does.
7363 This command can be called in any mode to follow a link that has
7366 (org-run-like-in-org-mode 'org-open-at-point))
7369 (defun org-open-link-from-string (s &optional arg)
7370 "Open a link in the string S, as if it was in Org-mode."
7371 (interactive "sLink: \nP")
7373 (let ((org-inhibit-startup t))
7376 (goto-char (point-min))
7377 (org-open-at-point arg))))
7379 (defun org-open-at-point (&optional in-emacs)
7380 "Open link at or after point.
7381 If there is no link at point, this function will search forward up to
7382 the end of the current subtree.
7383 Normally, files will be opened by an appropriate application. If the
7384 optional argument IN-EMACS is non-nil, Emacs will visit the file.
7385 With a double prefix argument, try to open outside of Emacs, in the
7386 application the system uses for this file type."
7388 (org-load-modules-maybe)
7389 (move-marker org-open-link-marker (point))
7390 (setq org-window-config-before-follow-link (current-window-configuration))
7391 (org-remove-occur-highlights nil nil t)
7393 ((org-at-timestamp-p t) (org-follow-timestamp-link))
7394 ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
7395 (org-footnote-action))
7397 (let (type path link line search (pos (point)))
7400 (skip-chars-forward "^]\n\r")
7401 (when (org-in-regexp org-bracket-link-regexp)
7402 (setq link (org-extract-attributes
7403 (org-link-unescape (org-match-string-no-properties 1))))
7404 (while (string-match " *\n *" link)
7405 (setq link (replace-match " " t t link)))
7406 (setq link (org-link-expand-abbrev link))
7408 ((or (file-name-absolute-p link)
7409 (string-match "^\\.\\.?/" link))
7410 (setq type "file" path link))
7411 ((string-match org-link-re-with-space3 link)
7412 (setq type (match-string 1 link) path (match-string 2 link)))
7413 (t (setq type "thisfile" path link)))
7416 (when (get-text-property (point) 'org-linked-text)
7417 (setq type "thisfile"
7418 pos (if (get-text-property (1+ (point)) 'org-linked-text)
7419 (1+ (point)) (point))
7420 path (buffer-substring
7421 (previous-single-property-change pos 'org-linked-text)
7422 (next-single-property-change pos 'org-linked-text)))
7426 (when (or (org-in-regexp org-angle-link-re)
7427 (org-in-regexp org-plain-link-re))
7428 (setq type (match-string 1) path (match-string 2))
7431 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
7433 path (match-string 1))
7434 (while (string-match ":" path)
7435 (setq path (replace-match "+" t t path)))
7437 (when (org-in-regexp "<\\([^><\n]+\\)>")
7438 (setq type "tree-match"
7439 path (match-string 1))
7442 (error "No link found"))
7443 ;; Remove any trailing spaces in path
7444 (if (string-match " +\\'" path)
7445 (setq path (replace-match "" t t path)))
7446 (if (and org-link-translation-function
7447 (fboundp org-link-translation-function))
7448 ;; Check if we need to translate the link
7449 (let ((tmp (funcall org-link-translation-function type path)))
7450 (setq type (car tmp) path (cdr tmp))))
7454 ((assoc type org-link-protocols)
7455 (funcall (nth 1 (assoc type org-link-protocols)) path))
7457 ((equal type "mailto")
7458 (let ((cmd (car org-link-mailto-program))
7459 (args (cdr org-link-mailto-program)) args1
7460 (address path) (subject "") a)
7461 (if (string-match "\\(.*\\)::\\(.*\\)" path)
7462 (setq address (match-string 1 path)
7463 subject (org-link-escape (match-string 2 path))))
7466 ((not (stringp (car args))) (push (pop args) args1))
7467 (t (setq a (pop args))
7468 (if (string-match "%a" a)
7469 (setq a (replace-match address t t a)))
7470 (if (string-match "%s" a)
7471 (setq a (replace-match subject t t a)))
7473 (apply cmd (nreverse args1))))
7475 ((member type '("http" "https" "ftp" "news"))
7476 (browse-url (concat type ":" (org-link-escape
7477 path org-link-escape-chars-browser))))
7479 ((member type '("message"))
7480 (browse-url (concat type ":" path)))
7482 ((string= type "tags")
7483 (org-tags-view in-emacs path))
7484 ((string= type "thisfile")
7486 (switch-to-buffer-other-window
7487 (org-get-buffer-for-internal-link (current-buffer)))
7488 (org-mark-ring-push))
7489 (let ((cmd `(org-link-search
7491 ,(cond ((equal in-emacs '(4)) 'occur)
7492 ((equal in-emacs '(16)) 'org-occur)
7495 (condition-case nil (eval cmd)
7496 (error (progn (widen) (eval cmd))))))
7498 ((string= type "tree-match")
7499 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
7501 ((string= type "file")
7502 (if (string-match "::\\([0-9]+\\)\\'" path)
7503 (setq line (string-to-number (match-string 1 path))
7504 path (substring path 0 (match-beginning 0)))
7505 (if (string-match "::\\(.+\\)\\'" path)
7506 (setq search (match-string 1 path)
7507 path (substring path 0 (match-beginning 0)))))
7508 (if (string-match "[*?{]" (file-name-nondirectory path))
7510 (org-open-file path in-emacs line search)))
7512 ((string= type "news")
7514 (org-gnus-follow-link path))
7516 ((string= type "shell")
7518 (if (or (not org-confirm-shell-link-function)
7519 (funcall org-confirm-shell-link-function
7520 (format "Execute \"%s\" in shell? "
7521 (org-add-props cmd nil
7522 'face 'org-warning))))
7524 (message "Executing %s" cmd)
7525 (shell-command cmd))
7528 ((string= type "elisp")
7530 (if (or (not org-confirm-elisp-link-function)
7531 (funcall org-confirm-elisp-link-function
7532 (format "Execute \"%s\" as elisp? "
7533 (org-add-props cmd nil
7534 'face 'org-warning))))
7535 (message "%s => %s" cmd
7536 (if (equal (string-to-char cmd) ?\()
7538 (call-interactively (read cmd))))
7542 (browse-url-at-point))))))
7543 (move-marker org-open-link-marker nil)
7544 (run-hook-with-args 'org-follow-link-hook))
7548 (defun org-get-effort (&optional pom)
7549 "Get the effort estimate for the current entry."
7550 (org-entry-get pom org-effort-property))
7554 (defvar org-create-file-search-functions nil
7555 "List of functions to construct the right search string for a file link.
7556 These functions are called in turn with point at the location to
7557 which the link should point.
7559 A function in the hook should first test if it would like to
7560 handle this file type, for example by checking the major-mode or
7561 the file extension. If it decides not to handle this file, it
7562 should just return nil to give other functions a chance. If it
7563 does handle the file, it must return the search string to be used
7564 when following the link. The search string will be part of the
7565 file link, given after a double colon, and `org-open-at-point'
7566 will automatically search for it. If special measures must be
7567 taken to make the search successful, another function should be
7568 added to the companion hook `org-execute-file-search-functions',
7571 A function in this hook may also use `setq' to set the variable
7572 `description' to provide a suggestion for the descriptive text to
7573 be used for this link when it gets inserted into an Org-mode
7574 buffer with \\[org-insert-link].")
7576 (defvar org-execute-file-search-functions nil
7577 "List of functions to execute a file search triggered by a link.
7579 Functions added to this hook must accept a single argument, the
7580 search string that was part of the file link, the part after the
7581 double colon. The function must first check if it would like to
7582 handle this search, for example by checking the major-mode or the
7583 file extension. If it decides not to handle this search, it
7584 should just return nil to give other functions a chance. If it
7585 does handle the search, it must return a non-nil value to keep
7586 other functions from trying.
7588 Each function can access the current prefix argument through the
7589 variable `current-prefix-argument'. Note that a single prefix is
7590 used to force opening a link in Emacs, so it may be good to only
7591 use a numeric or double prefix to guide the search function.
7593 In case this is needed, a function in this hook can also restore
7594 the window configuration before `org-open-at-point' was called using:
7596 (set-window-configuration org-window-config-before-follow-link)")
7598 (defun org-link-search (s &optional type avoid-pos)
7599 "Search for a link search option.
7600 If S is surrounded by forward slashes, it is interpreted as a
7601 regular expression. In org-mode files, this will create an `org-occur'
7602 sparse tree. In ordinary files, `occur' will be used to list matches.
7603 If the current buffer is in `dired-mode', grep will be used to search
7604 in all files. If AVOID-POS is given, ignore matches near that position."
7605 (let ((case-fold-search t)
7606 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
7607 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
7608 (append '(("") (" ") ("\t") ("\n"))
7612 (pre nil) (post nil)
7613 words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
7615 ;; First check if there are any special
7616 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
7617 ;; Now try the builtin stuff
7619 (goto-char (point-min))
7622 (concat "<<" (regexp-quote s0) ">>") nil t)
7623 (setq type 'dedicated
7624 pos (match-beginning 0))))
7625 ;; There is an exact target for this
7627 ((and (string-match "^(\\(.*\\))$" s0)
7629 (goto-char (point-min))
7632 (concat "[^[]" (regexp-quote
7633 (format org-coderef-label-format
7634 (match-string 1 s0))))
7636 (setq type 'dedicated
7637 pos (1+ (match-beginning 0))))))
7638 ;; There is a coderef target for this
7640 ((string-match "^/\\(.*\\)/$" s)
7641 ;; A regular expression
7644 (org-occur (match-string 1 s)))
7645 ;;((eq major-mode 'dired-mode)
7646 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
7647 (t (org-do-occur (match-string 1 s)))))
7649 ;; A normal search strings
7650 (when (equal (string-to-char s) ?*)
7651 ;; Anchor on headlines, post may include tags.
7652 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
7653 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
7655 (remove-text-properties
7657 '(face nil mouse-face nil keymap nil fontified nil) s)
7658 ;; Make a series of regular expressions to find a match
7659 (setq words (org-split-string s "[ \n\r\t]+")
7661 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
7662 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
7664 re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
7665 re2a (concat "[ \t\r\n]" re2a_)
7666 re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
7667 re4 (concat "[^a-zA-Z_]" re4_)
7669 re1 (concat pre re2 post)
7670 re3 (concat pre (if pre re4_ re4) post)
7671 re5 (concat pre ".*" re4)
7672 re2 (concat pre re2)
7673 re2a (concat pre (if pre re2a_ re2a))
7674 re4 (concat pre (if pre re4_ re4))
7675 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
7676 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
7680 ((eq type 'org-occur) (org-occur reall))
7681 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
7682 (t (goto-char (point-min))
7684 (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
7685 (org-search-not-self 1 re1 nil t)
7686 (org-search-not-self 1 re2 nil t)
7687 (org-search-not-self 1 re2a nil t)
7688 (org-search-not-self 1 re3 nil t)
7689 (org-search-not-self 1 re4 nil t)
7690 (org-search-not-self 1 re5 nil t)
7692 (goto-char (match-beginning 1))
7694 (error "No match")))))
7696 ;; Normal string-search
7697 (goto-char (point-min))
7698 (if (search-forward s nil t)
7699 (goto-char (match-beginning 0))
7700 (error "No match"))))
7701 (and (org-mode-p) (org-show-context 'link-search))
7704 (defun org-search-not-self (group &rest args)
7705 "Execute `re-search-forward', but only accept matches that do not
7706 enclose the position of `org-open-link-marker'."
7707 (let ((m org-open-link-marker))
7709 (while (apply 're-search-forward args)
7710 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
7711 (goto-char (match-end group))
7712 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
7713 (> (match-beginning 0) (marker-position m))
7714 (< (match-end 0) (marker-position m)))
7716 (or (not (org-in-regexp
7717 org-bracket-link-analytic-regexp 1))
7718 (not (match-end 4)) ; no description
7719 (and (<= (match-beginning 4) (point))
7720 (>= (match-end 4) (point))))))
7721 (throw 'exit (point))))))))
7723 (defun org-get-buffer-for-internal-link (buffer)
7724 "Return a buffer to be used for displaying the link target of internal links."
7726 ((not org-display-internal-link-with-indirect-buffer)
7728 ((string-match "(Clone)$" (buffer-name buffer))
7729 (message "Buffer is already a clone, not making another one")
7730 ;; we also do not modify visibility in this case
7732 (t ; make a new indirect buffer for displaying the link
7733 (let* ((bn (buffer-name buffer))
7734 (ibn (concat bn "(Clone)"))
7735 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
7736 (with-current-buffer ib (org-overview))
7739 (defun org-do-occur (regexp &optional cleanup)
7740 "Call the Emacs command `occur'.
7741 If CLEANUP is non-nil, remove the printout of the regular expression
7742 in the *Occur* buffer. This is useful if the regex is long and not useful
7746 (let ((cwin (selected-window)) win beg end)
7747 (when (setq win (get-buffer-window "*Occur*"))
7748 (select-window win))
7749 (goto-char (point-min))
7750 (when (re-search-forward "match[a-z]+" nil t)
7751 (setq beg (match-end 0))
7752 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
7753 (setq end (1- (match-beginning 0)))))
7754 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
7755 (goto-char (point-min))
7756 (select-window cwin))))
7758 ;;; The mark ring for links jumps
7760 (defvar org-mark-ring nil
7761 "Mark ring for positions before jumps in Org-mode.")
7762 (defvar org-mark-ring-last-goto nil
7763 "Last position in the mark ring used to go back.")
7764 ;; Fill and close the ring
7765 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
7766 (loop for i from 1 to org-mark-ring-length do
7767 (push (make-marker) org-mark-ring))
7768 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
7771 (defun org-mark-ring-push (&optional pos buffer)
7772 "Put the current position or POS into the mark ring and rotate it."
7774 (setq pos (or pos (point)))
7775 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
7776 (move-marker (car org-mark-ring)
7778 (or buffer (current-buffer)))
7780 (substitute-command-keys
7781 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
7783 (defun org-mark-ring-goto (&optional n)
7784 "Jump to the previous position in the mark ring.
7785 With prefix arg N, jump back that many stored positions. When
7786 called several times in succession, walk through the entire ring.
7787 Org-mode commands jumping to a different position in the current file,
7788 or to another Org-mode file, automatically push the old position
7792 (if (eq last-command this-command)
7793 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
7794 (setq p org-mark-ring))
7795 (setq org-mark-ring-last-goto p)
7797 (switch-to-buffer (marker-buffer m))
7799 (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
7801 (defun org-remove-angle-brackets (s)
7802 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
7803 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
7805 (defun org-add-angle-brackets (s)
7806 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
7807 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
7809 (defun org-remove-double-quotes (s)
7810 (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
7811 (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
7814 ;;; Following specific links
7816 (defun org-follow-timestamp-link ()
7818 ((org-at-date-range-p t)
7819 (let ((org-agenda-start-on-weekday)
7820 (t1 (match-string 1))
7821 (t2 (match-string 2)))
7822 (setq t1 (time-to-days (org-time-string-to-time t1))
7823 t2 (time-to-days (org-time-string-to-time t2)))
7824 (org-agenda-list nil t1 (1+ (- t2 t1)))))
7825 ((org-at-timestamp-p t)
7826 (org-agenda-list nil (time-to-days (org-time-string-to-time
7827 (substring (match-string 1) 0 10)))
7829 (t (error "This should not happen"))))
7832 ;;; Following file links
7833 (defvar org-wait nil)
7834 (defun org-open-file (path &optional in-emacs line search)
7835 "Open the file at PATH.
7836 First, this expands any special file name abbreviations. Then the
7837 configuration variable `org-file-apps' is checked if it contains an
7838 entry for this file type, and if yes, the corresponding command is launched.
7840 If no application is found, Emacs simply visits the file.
7842 With optional prefix argument IN-EMACS, Emacs will visit the file.
7843 With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
7844 and o use an external application to visit the file.
7846 Optional LINE specifies a line to go to, optional SEARCH a string to
7847 search for. If LINE or SEARCH is given, the file will always be
7849 If the file does not exist, an error is thrown."
7850 (setq in-emacs (or in-emacs line search))
7851 (let* ((file (if (equal path "")
7853 (substitute-in-file-name (expand-file-name path))))
7854 (apps (append org-file-apps (org-default-apps)))
7855 (remp (and (assq 'remote apps) (org-file-remote-p file)))
7856 (dirp (if remp nil (file-directory-p file)))
7857 (file (if (and dirp org-open-directory-means-index-dot-org)
7858 (concat (file-name-as-directory file) "index.org")
7860 (a-m-a-p (assq 'auto-mode apps))
7861 (dfile (downcase file))
7862 (old-buffer (current-buffer))
7864 (old-mode major-mode)
7866 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
7867 (setq ext (match-string 1 dfile))
7868 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
7869 (setq ext (match-string 1 dfile))))
7871 ((equal in-emacs '(16))
7872 (setq cmd (cdr (assoc 'system apps))))
7873 (in-emacs (setq cmd 'emacs))
7875 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
7876 (and dirp (cdr (assoc 'directory apps)))
7877 (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
7879 (cdr (assoc ext apps))
7880 (cdr (assoc t apps))))))
7881 (when (eq cmd 'system)
7882 (setq cmd (cdr (assoc 'system apps))))
7883 (when (eq cmd 'default)
7884 (setq cmd (cdr (assoc t apps))))
7885 (when (eq cmd 'mailcap)
7887 (mailcap-parse-mailcaps)
7888 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
7889 (command (mailcap-mime-info mime-type)))
7890 (if (stringp command)
7892 (setq cmd 'emacs))))
7893 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
7894 (not (file-exists-p file))
7895 (not org-open-non-existing-files))
7896 (error "No such file: %s" file))
7898 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
7899 ;; Remove quotes around the file name - we'll use shell-quote-argument.
7900 (while (string-match "['\"]%s['\"]" cmd)
7901 (setq cmd (replace-match "%s" t t cmd)))
7902 (while (string-match "%s" cmd)
7903 (setq cmd (replace-match
7905 (shell-quote-argument
7906 (convert-standard-filename file)))
7908 (save-window-excursion
7909 (start-process-shell-command cmd nil cmd)
7910 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
7914 (funcall (cdr (assq 'file org-link-frame-setup)) file)
7916 (if line (goto-line line)
7917 (if search (org-link-search search))))
7919 (let ((file (convert-standard-filename file)))
7921 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
7922 (and (org-mode-p) (eq old-mode 'org-mode)
7923 (or (not (equal old-buffer (current-buffer)))
7924 (not (equal old-pos (point))))
7925 (org-mark-ring-push old-pos old-buffer))))
7927 (defun org-default-apps ()
7928 "Return the default applications for this operating system."
7930 ((eq system-type 'darwin)
7931 org-file-apps-defaults-macosx)
7932 ((eq system-type 'windows-nt)
7933 org-file-apps-defaults-windowsnt)
7934 (t org-file-apps-defaults-gnu)))
7936 (defun org-apps-regexp-alist (list &optional add-auto-mode)
7937 "Convert extensions to regular expressions in the cars of LIST.
7938 Also, weed out any non-string entries, because the return value is used
7939 only for regexp matching.
7940 When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
7941 point to the symbol `emacs', indicating that the file should
7942 be opened in Emacs."
7946 (if (not (stringp (car x)))
7948 (if (string-match "\\W" (car x))
7950 (cons (concat "\\." (car x) "\\'") (cdr x)))))
7953 (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
7955 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
7956 (defun org-file-remote-p (file)
7957 "Test whether FILE specifies a location on a remote system.
7958 Return non-nil if the location is indeed remote.
7960 For example, the filename \"/user@host:/foo\" specifies a location
7961 on the system \"/user@host:\"."
7962 (cond ((fboundp 'file-remote-p)
7963 (file-remote-p file))
7964 ((fboundp 'tramp-handle-file-remote-p)
7965 (tramp-handle-file-remote-p file))
7966 ((and (boundp 'ange-ftp-name-format)
7967 (string-match (car ange-ftp-name-format) file))
7974 (defun org-get-org-file ()
7975 "Read a filename, with default directory `org-directory'."
7976 (let ((default (or org-default-notes-file remember-data-file)))
7977 (read-file-name (format "File name [%s]: " default)
7978 (file-name-as-directory org-directory)
7981 (defun org-notes-order-reversed-p ()
7982 "Check if the current file should receive notes in reversed order."
7984 ((not org-reverse-note-order) nil)
7985 ((eq t org-reverse-note-order) t)
7986 ((not (listp org-reverse-note-order)) nil)
7988 (let ((all org-reverse-note-order)
7990 (while (setq entry (pop all))
7991 (if (string-match (car entry) buffer-file-name)
7992 (throw 'exit (cdr entry))))
7995 (defvar org-refile-target-table nil
7996 "The list of refile targets, created by `org-refile'.")
7998 (defvar org-agenda-new-buffers nil
7999 "Buffers created to visit agenda files.")
8001 (defun org-get-refile-targets (&optional default-buffer)
8002 "Produce a table with refile targets."
8003 (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
8004 targets txt re files f desc descre fast-path-p level)
8005 (message "Getting targets...")
8006 (with-current-buffer (or default-buffer (current-buffer))
8007 (while (setq entry (pop entries))
8008 (setq files (car entry) desc (cdr entry))
8009 (setq fast-path-p nil)
8011 ((null files) (setq files (list (current-buffer))))
8012 ((eq files 'org-agenda-files)
8013 (setq files (org-agenda-files 'unrestricted)))
8014 ((and (symbolp files) (fboundp files))
8015 (setq files (funcall files)))
8016 ((and (symbolp files) (boundp files))
8017 (setq files (symbol-value files))))
8018 (if (stringp files) (setq files (list files)))
8020 ((eq (car desc) :tag)
8021 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
8022 ((eq (car desc) :todo)
8023 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
8024 ((eq (car desc) :regexp)
8025 (setq descre (cdr desc)))
8026 ((eq (car desc) :level)
8027 (setq descre (concat "^\\*\\{" (number-to-string
8028 (if org-odd-levels-only
8029 (1- (* 2 (cdr desc)))
8032 ((eq (car desc) :maxlevel)
8033 (setq fast-path-p t)
8034 (setq descre (concat "^\\*\\{1," (number-to-string
8035 (if org-odd-levels-only
8036 (1- (* 2 (cdr desc)))
8039 (t (error "Bad refiling target description %s" desc)))
8040 (while (setq f (pop files))
8042 (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
8043 (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
8044 (setq f (expand-file-name f))
8048 (goto-char (point-min))
8049 (while (re-search-forward descre nil t)
8050 (goto-char (point-at-bol))
8051 (when (looking-at org-complex-heading-regexp)
8052 (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
8053 txt (org-link-display-format (match-string 4))
8054 re (concat "^" (regexp-quote
8055 (buffer-substring (match-beginning 1)
8057 (if (match-end 5) (setq re (concat re "[ \t]+"
8059 (match-string 5)))))
8060 (setq re (concat re "[ \t]*$"))
8061 (when org-refile-use-outline-path
8062 (setq txt (mapconcat 'org-protect-slash
8064 (if (eq org-refile-use-outline-path 'file)
8065 (list (file-name-nondirectory
8066 (buffer-file-name (buffer-base-buffer))))
8067 (if (eq org-refile-use-outline-path 'full-file-path)
8068 (list (buffer-file-name (buffer-base-buffer)))))
8069 (org-get-outline-path fast-path-p level txt)
8072 (push (list txt f re (point)) targets))
8073 (goto-char (point-at-eol))))))))
8074 (message "Getting targets...done")
8075 (nreverse targets))))
8077 (defun org-protect-slash (s)
8078 (while (string-match "/" s)
8079 (setq s (replace-match "\\" t t s)))
8082 (defvar org-olpa (make-vector 20 nil))
8084 (defun org-get-outline-path (&optional fastp level heading)
8085 "Return the outline path to the current entry, as a list."
8089 (error "Outline path failure, more than 19 levels."))
8090 (loop for i from level upto 19 do
8091 (aset org-olpa i nil))
8093 (delq nil (append org-olpa nil))
8094 (aset org-olpa level heading)))
8097 (while (org-up-heading-safe)
8098 (when (looking-at org-complex-heading-regexp)
8099 (push (org-match-string-no-properties 4) rtn)))
8102 (defvar org-refile-history nil
8103 "History for refiling operations.")
8105 (defun org-refile (&optional goto default-buffer)
8106 "Move the entry at point to another heading.
8107 The list of target headings is compiled using the information in
8108 `org-refile-targets', which see. This list is created before each use
8109 and will therefore always be up-to-date.
8111 At the target location, the entry is filed as a subitem of the target heading.
8112 Depending on `org-reverse-note-order', the new subitem will either be the
8113 first or the last subitem.
8115 If there is an active region, all entries in that region will be moved.
8116 However, the region must fulfil the requirement that the first heading
8117 is the first one sets the top-level of the moved text - at most siblings
8118 below it are allowed.
8120 With prefix arg GOTO, the command will only visit the target location,
8121 not actually move anything.
8122 With a double prefix `C-u C-u', go to the location where the last refiling
8123 operation has put the subtree."
8125 (let* ((cbuf (current-buffer))
8126 (regionp (org-region-active-p))
8127 (region-start (and regionp (region-beginning)))
8128 (region-end (and regionp (region-end)))
8129 (region-length (and regionp (- region-end region-start)))
8130 (filename (buffer-file-name (buffer-base-buffer cbuf)))
8131 pos it nbuf file re level reversed)
8132 (when regionp (goto-char region-start)
8133 (unless (org-kill-is-subtree-p
8134 (buffer-substring region-start region-end))
8135 (error "The region is not a (sequence of) subtree(s)")))
8136 (if (equal goto '(16))
8137 (org-refile-goto-last-stored)
8138 (when (setq it (org-refile-get-location
8139 (if goto "Goto: " "Refile to: ") default-buffer))
8140 (setq file (nth 1 it)
8144 (equal (buffer-file-name) file)
8146 (and (>= pos region-start)
8147 (<= pos region-end))
8148 (and (>= pos (point))
8149 (< pos (save-excursion
8150 (org-end-of-subtree t t))))))
8151 (error "Cannot refile to position inside the tree or region"))
8153 (setq nbuf (or (find-buffer-visiting file)
8154 (find-file-noselect file)))
8157 (switch-to-buffer nbuf)
8159 (org-show-context 'org-goto))
8162 (kill-new (buffer-substring region-start region-end))
8163 (org-save-markers-in-region region-start region-end))
8164 (org-copy-subtree 1 nil t))
8166 (set-buffer (setq nbuf (or (find-buffer-visiting file)
8167 (find-file-noselect file))))
8168 (setq reversed (org-notes-order-reversed-p))
8173 (looking-at outline-regexp)
8174 (setq level (org-get-valid-level (funcall outline-level) 1))
8177 (or (outline-next-heading) (point-max))
8178 (or (save-excursion (outline-get-next-sibling))
8179 (org-end-of-subtree t t)
8181 (if (not (bolp)) (newline))
8182 (bookmark-set "org-refile-last-stored")
8183 (org-paste-subtree level))))
8185 (delete-region (point) (+ (point) region-length))
8187 (setq org-markers-to-move nil)
8188 (message "Refiled to \"%s\"" (car it)))))))
8190 (defun org-refile-goto-last-stored ()
8191 "Go to the location where the last refile was stored."
8193 (bookmark-jump "org-refile-last-stored")
8194 (message "This is the location of the last refile"))
8196 (defun org-refile-get-location (&optional prompt default-buffer)
8197 "Prompt the user for a refile location, using PROMPT."
8198 (let ((org-refile-targets org-refile-targets)
8199 (org-refile-use-outline-path org-refile-use-outline-path))
8200 (setq org-refile-target-table (org-get-refile-targets default-buffer)))
8201 (unless org-refile-target-table
8202 (error "No refile targets"))
8203 (let* ((cbuf (current-buffer))
8204 (partial-completion-mode nil)
8205 (cfn (buffer-file-name (buffer-base-buffer cbuf)))
8206 (cfunc (if (and org-refile-use-outline-path
8207 org-outline-path-complete-in-steps)
8208 'org-olpath-completing-read
8209 'org-ido-completing-read))
8210 (extra (if org-refile-use-outline-path "/" ""))
8211 (filename (and cfn (expand-file-name cfn)))
8214 (if (not (equal filename (nth 1 x)))
8215 (cons (concat (car x) extra " ("
8216 (file-name-nondirectory (nth 1 x)) ")")
8218 (cons (concat (car x) extra) (cdr x))))
8219 org-refile-target-table))
8220 (completion-ignore-case t))
8221 (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
8224 (defun org-olpath-completing-read (prompt collection &rest args)
8225 "Read an outline path like a file name."
8226 (let ((thetable collection)
8227 (org-completion-use-ido nil)) ; does not work with ido.
8229 'org-ido-completing-read prompt
8230 (lambda (string predicate &optional flag)
8231 (let (rtn r f (l (length string)))
8235 (try-completion string thetable))
8238 (setq rtn (all-completions string thetable predicate))
8241 (setq r (substring x l))
8242 (if (string-match " ([^)]*)$" x)
8243 (setq f (match-string 0 x))
8245 (if (string-match "/" r)
8246 (concat string (substring r 0 (match-end 0)) f)
8251 (assoc string thetable)))
8257 (defun org-find-dblock (name)
8258 "Find the first dynamic block with name NAME in the buffer.
8259 If not found, stay at current position and return nil."
8262 (goto-char (point-min))
8263 (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
8265 (match-beginning 0))))
8266 (if pos (goto-char pos))
8269 (defconst org-dblock-start-re
8270 "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
8271 "Matches the startline of a dynamic block, with parameters.")
8273 (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
8274 "Matches the end of a dynamic block.")
8276 (defun org-create-dblock (plist)
8277 "Create a dynamic block section, with parameters taken from PLIST.
8278 PLIST must contain a :name entry which is used as name of the block."
8279 (unless (bolp) (newline))
8280 (let ((name (plist-get plist :name)))
8281 (insert "#+BEGIN: " name)
8283 (if (eq (car plist) :name)
8284 (setq plist (cddr plist))
8285 (insert " " (prin1-to-string (pop plist)))))
8286 (insert "\n\n#+END:\n")
8287 (beginning-of-line -2)))
8289 (defun org-prepare-dblock ()
8290 "Prepare dynamic block for refresh.
8291 This empties the block, puts the cursor at the insert position and returns
8292 the property list including an extra property :name with the block name."
8293 (unless (looking-at org-dblock-start-re)
8294 (error "Not at a dynamic block"))
8295 (let* ((begdel (1+ (match-end 0)))
8296 (name (org-no-properties (match-string 1)))
8297 (params (append (list :name name)
8298 (read (concat "(" (match-string 3) ")")))))
8299 (unless (re-search-forward org-dblock-end-re nil t)
8300 (error "Dynamic block not terminated"))
8303 (list :content (buffer-substring
8304 begdel (match-beginning 0)))))
8305 (delete-region begdel (match-beginning 0))
8310 (defun org-map-dblocks (&optional command)
8311 "Apply COMMAND to all dynamic blocks in the current buffer.
8312 If COMMAND is not given, use `org-update-dblock'."
8313 (let ((cmd (or command 'org-update-dblock))
8316 (goto-char (point-min))
8317 (while (re-search-forward org-dblock-start-re nil t)
8318 (goto-char (setq pos (match-beginning 0)))
8321 (error (message "Error during update of dynamic block")))
8323 (unless (re-search-forward org-dblock-end-re nil t)
8324 (error "Dynamic block not terminated"))))))
8326 (defun org-dblock-update (&optional arg)
8327 "User command for updating dynamic blocks.
8328 Update the dynamic block at point. With prefix ARG, update all dynamic
8329 blocks in the buffer."
8332 (org-update-all-dblocks)
8333 (or (looking-at org-dblock-start-re)
8334 (org-beginning-of-dblock))
8335 (org-update-dblock)))
8337 (defun org-update-dblock ()
8338 "Update the dynamic block at point
8339 This means to empty the block, parse for parameters and then call
8340 the correct writing function."
8341 (save-window-excursion
8342 (let* ((pos (point))
8343 (line (org-current-line))
8344 (params (org-prepare-dblock))
8345 (name (plist-get params :name))
8346 (cmd (intern (concat "org-dblock-write:" name))))
8347 (message "Updating dynamic block `%s' at line %d..." name line)
8348 (funcall cmd params)
8349 (message "Updating dynamic block `%s' at line %d...done" name line)
8352 (defun org-beginning-of-dblock ()
8353 "Find the beginning of the dynamic block at point.
8354 Error if there is no such block at point."
8358 (if (and (re-search-backward org-dblock-start-re nil t)
8359 (setq beg (match-beginning 0))
8360 (re-search-forward org-dblock-end-re nil t)
8361 (> (match-end 0) pos))
8364 (error "Not in a dynamic block"))))
8366 (defun org-update-all-dblocks ()
8367 "Update all dynamic blocks in the buffer.
8368 This function can be used in a hook."
8370 (org-map-dblocks 'org-update-dblock)))
8375 (defconst org-additional-option-like-keywords
8376 '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
8377 "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
8378 "BEGIN_EXAMPLE" "END_EXAMPLE"
8379 "BEGIN_QUOTE" "END_QUOTE"
8380 "BEGIN_VERSE" "END_VERSE"
8381 "BEGIN_CENTER" "END_CENTER"
8382 "BEGIN_SRC" "END_SRC"
8383 "CATEGORY" "COLUMNS"
8384 "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
8386 (defcustom org-structure-template-alist
8388 ("s" "#+begin_src ?\n\n#+end_src"
8389 "<src lang=\"?\">\n\n</src>")
8390 ("e" "#+begin_example\n?\n#+end_example"
8391 "<example>\n?\n</example>")
8392 ("q" "#+begin_quote\n?\n#+end_quote"
8393 "<quote>\n?\n</quote>")
8394 ("v" "#+begin_verse\n?\n#+end_verse"
8395 "<verse>\n?\n/verse>")
8396 ("c" "#+begin_center\n?\n#+end_center"
8397 "<center>\n?\n/center>")
8398 ("l" "#+begin_latex\n?\n#+end_latex"
8399 "<literal style=\"latex\">\n?\n</literal>")
8401 "<literal style=\"latex\">?</literal>")
8402 ("h" "#+begin_html\n?\n#+end_html"
8403 "<literal style=\"html\">\n?\n</literal>")
8405 "<literal style=\"html\">?</literal>")
8406 ("a" "#+begin_ascii\n?\n#+end_ascii")
8408 ("i" "#+include %file ?"
8409 "<include file=%file markup=\"?\">")
8411 "Structure completion elements.
8412 This is a list of abbreviation keys and values. The value gets inserted
8413 it you type @samp{.} followed by the key and then the completion key,
8414 usually `M-TAB'. %file will be replaced by a file name after prompting
8415 for the file using completion.
8416 There are two templates for each key, the first uses the original Org syntax,
8417 the second uses Emacs Muse-like syntax tags. These Muse-like tags become
8418 the default when the /org-mtags.el/ module has been loaded. See also the
8419 variable `org-mtags-prefer-muse-templates'.
8420 This is an experimental feature, it is undecided if it is going to stay in."
8421 :group 'org-completion
8424 (string :tag "Template")
8425 (string :tag "Muse Template")))
8427 (defun org-try-structure-completion ()
8428 "Try to complete a structure template before point.
8429 This looks for strings like \"<e\" on an otherwise empty line and
8431 (let ((l (buffer-substring (point-at-bol) (point)))
8433 (when (and (looking-at "[ \t]*$")
8434 (string-match "^[ \t]*<\\([a-z]+\\)$"l)
8435 (setq a (assoc (match-string 1 l) org-structure-template-alist)))
8436 (org-complete-expand-structure-template (+ -1 (point-at-bol)
8437 (match-beginning 1)) a)
8440 (defun org-complete-expand-structure-template (start cell)
8441 "Expand a structure template."
8442 (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
8443 (rpl (nth (if musep 2 1) cell)))
8444 (delete-region start (point))
8445 (when (string-match "\\`#\\+" rpl)
8448 ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
8449 (delete-region (point-at-bol) (point)))
8451 (setq start (point))
8452 (if (string-match "%file" rpl)
8453 (setq rpl (replace-match
8457 (abbreviate-file-name (read-file-name "Include file: ")))
8461 (if (re-search-backward "\\?" start t) (delete-char 1))))
8464 (defun org-complete (&optional arg)
8465 "Perform completion on word at point.
8466 At the beginning of a headline, this completes TODO keywords as given in
8467 `org-todo-keywords'.
8468 If the current word is preceded by a backslash, completes the TeX symbols
8469 that are supported for HTML support.
8470 If the current word is preceded by \"#+\", completes special words for
8471 setting file options.
8472 In the line after \"#+STARTUP:, complete valid keywords.\"
8473 At all other locations, this simply calls the value of
8474 `org-completion-fallback-command'."
8476 (org-without-partial-completion
8480 (beg1 (save-excursion
8481 (skip-chars-backward (org-re "[:alnum:]_@"))
8483 (beg (save-excursion
8484 (skip-chars-backward "a-zA-Z0-9_:$")
8486 (confirm (lambda (x) (stringp (car x))))
8487 (searchhead (equal (char-before beg) ?*))
8489 (when (and (member (char-before beg1) '(?. ?<))
8490 (setq a (assoc (buffer-substring beg1 (point))
8491 org-structure-template-alist)))
8492 (org-complete-expand-structure-template (1- beg1) a)
8494 (tag (and (equal (char-before beg1) ?:)
8495 (equal (char-after (point-at-bol)) ?*)))
8496 (prop (and (equal (char-before beg1) ?:)
8497 (not (equal (char-after (point-at-bol)) ?*))))
8498 (texp (equal (char-before beg) ?\\))
8499 (link (equal (char-before beg) ?\[))
8500 (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
8503 (startup (string-match "^#\\+STARTUP:.*"
8504 (buffer-substring (point-at-bol) (point))))
8505 (completion-ignore-case opt)
8515 (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
8516 (cons (match-string 2 x) (match-string 1 x)))
8517 (org-split-string (org-get-current-options) "\n"))
8518 (mapcar 'list org-additional-option-like-keywords)))
8520 (setq type :startup)
8521 org-startup-options)
8522 (link (append org-link-abbrev-alist-local
8523 org-link-abbrev-alist))
8527 ((string-match "\\`\\*+[ \t]+\\'"
8528 (buffer-substring (point-at-bol) beg))
8530 (mapcar 'list org-todo-keywords-1))
8532 (setq type :searchhead)
8534 (goto-char (point-min))
8535 (while (re-search-forward org-todo-line-regexp nil t)
8537 (org-make-org-heading-search-string
8538 (match-string 3) t))
8541 (tag (setq type :tag beg beg1)
8542 (or org-tag-alist (org-get-buffer-tags)))
8543 (prop (setq type :prop beg beg1)
8544 (mapcar 'list (org-buffer-property-keys nil t t)))
8546 (call-interactively org-completion-fallback-command)
8547 (throw 'exit nil)))))
8548 (pattern (buffer-substring-no-properties beg end))
8549 (completion (try-completion pattern table confirm)))
8550 (cond ((eq completion t)
8551 (if (not (assoc (upcase pattern) table))
8552 (message "Already complete")
8553 (if (and (equal type :opt)
8554 (not (member (car (assoc (upcase pattern) table))
8555 org-additional-option-like-keywords)))
8556 (insert (substring (cdr (assoc (upcase pattern) table))
8558 (if (memq type '(:tag :prop)) (insert ":")))))
8560 (message "Can't find completion for \"%s\"" pattern)
8562 ((not (string= pattern completion))
8563 (delete-region beg end)
8564 (if (string-match " +$" completion)
8565 (setq completion (replace-match "" t t completion)))
8567 (if (get-buffer-window "*Completions*")
8568 (delete-window (get-buffer-window "*Completions*")))
8569 (if (assoc completion table)
8570 (if (eq type :todo) (insert " ")
8571 (if (memq type '(:tag :prop)) (insert ":"))))
8572 (if (and (equal type :opt) (assoc completion table))
8573 (message "%s" (substitute-command-keys
8574 "Press \\[org-complete] again to insert example settings"))))
8576 (message "Making completion list...")
8577 (let ((list (sort (all-completions pattern table confirm)
8579 (with-output-to-temp-buffer "*Completions*"
8581 ;; Protection needed for XEmacs and emacs 21
8582 (display-completion-list list pattern)
8583 (error (display-completion-list list)))))
8584 (message "Making completion list...%s" "done")))))))
8586 ;;;; TODO, DEADLINE, Comments
8588 (defun org-toggle-comment ()
8589 "Change the COMMENT state of an entry."
8592 (org-back-to-heading)
8593 (let (case-fold-search)
8594 (if (looking-at (concat outline-regexp
8595 "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
8596 (replace-match "" t t nil 1)
8597 (if (looking-at outline-regexp)
8599 (goto-char (match-end 0))
8600 (insert org-comment-string " ")))))))
8602 (defvar org-last-todo-state-is-todo nil
8603 "This is non-nil when the last TODO state change led to a TODO state.
8604 If the last change removed the TODO tag or switched to DONE, then
8607 (defvar org-setting-tags nil) ; dynamically skipped
8609 (defun org-parse-local-options (string var)
8610 "Parse STRING for startup setting relevant for variable VAR."
8611 (let ((rtn (symbol-value var))
8614 (if (or (not string) (not (string-match "\\S-" string)))
8616 (setq opts (delq nil (mapcar (lambda (x)
8617 (setq e (assoc x org-startup-options))
8618 (if (eq (nth 1 e) var) e nil))
8619 (org-split-string string "[ \t]+"))))
8623 (while (setq e (pop opts))
8625 (setq rtn (nth 2 e))
8626 (if (not (listp rtn)) (setq rtn nil))
8627 (push (nth 2 e) rtn)))
8630 (defvar org-todo-setup-filter-hook nil
8631 "Hook for functions that pre-filter todo specs.
8633 Each function takes a todo spec and returns either `nil' or the spec
8634 transformed into canonical form." )
8636 (defvar org-todo-get-default-hook nil
8637 "Hook for functions that get a default item for todo.
8639 Each function takes arguments (NEW-MARK OLD-MARK) and returns either
8640 `nil' or a string to be used for the todo mark." )
8642 (defvar org-agenda-headline-snapshot-before-repeat)
8643 (defun org-todo (&optional arg)
8644 "Change the TODO state of an item.
8645 The state of an item is given by a keyword at the start of the heading,
8647 *** TODO Write paper
8650 The different keywords are specified in the variable `org-todo-keywords'.
8651 By default the available states are \"TODO\" and \"DONE\".
8652 So for this example: when the item starts with TODO, it is changed to DONE.
8653 When it starts with DONE, the DONE is removed. And when neither TODO nor
8654 DONE are present, add TODO at the beginning of the heading.
8656 With C-u prefix arg, use completion to determine the new state.
8657 With numeric prefix arg, switch to that state.
8658 With a double C-u prefix, switch to the next set of TODO keywords (nextset).
8659 With a tripple C-u prefix, circumvent any state blocking.
8661 For calling through lisp, arg is also interpreted in the following way:
8662 'none -> empty state
8663 \"\"(empty string) -> switch to empty state
8664 'done -> switch to DONE
8665 'nextset -> switch to the next set of keywords
8666 'previousset -> switch to the previous set of keywords
8667 \"WAITING\" -> switch to the specified keyword, but only if it
8668 really is a member of `org-todo-keywords'."
8670 (if (equal arg '(16)) (setq arg 'nextset))
8671 (let ((org-blocker-hook org-blocker-hook))
8672 (when (equal arg '(64))
8673 (setq arg nil org-blocker-hook nil))
8676 (org-back-to-heading)
8677 (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
8678 (or (looking-at (concat " +" org-todo-regexp " *"))
8680 (let* ((match-data (match-data))
8681 (startpos (point-at-bol))
8682 (logging (save-match-data (org-entry-get nil "LOGGING" t)))
8683 (org-log-done org-log-done)
8684 (org-log-repeat org-log-repeat)
8685 (org-todo-log-states org-todo-log-states)
8686 (this (match-string 1))
8687 (hl-pos (match-beginning 0))
8688 (head (org-get-todo-sequence-head this))
8689 (ass (assoc head org-todo-kwd-alist))
8690 (interpret (nth 1 ass))
8691 (done-word (nth 3 ass))
8692 (final-done-word (nth 4 ass))
8693 (last-state (or this ""))
8694 (completion-ignore-case t)
8695 (member (member this org-todo-keywords-1))
8698 ((and org-todo-key-trigger
8699 (or (and (equal arg '(4))
8700 (eq org-use-fast-todo-selection 'prefix))
8701 (and (not arg) org-use-fast-todo-selection
8702 (not (eq org-use-fast-todo-selection
8704 ;; Use fast selection
8705 (org-fast-todo-selection))
8706 ((and (equal arg '(4))
8707 (or (not org-use-fast-todo-selection)
8708 (not org-todo-key-trigger)))
8709 ;; Read a state with completion
8710 (org-ido-completing-read
8711 "State: " (mapcar (lambda(x) (list x))
8712 org-todo-keywords-1)
8716 (if tail (car tail) nil)
8717 (car org-todo-keywords-1)))
8719 (if (equal member org-todo-keywords-1)
8722 (nth (- (length org-todo-keywords-1)
8724 org-todo-keywords-1)
8725 (org-last org-todo-keywords-1))))
8726 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
8727 (setq arg nil))) ; hack to fall back to cycling
8729 ;; user or caller requests a specific state
8731 ((equal arg "") nil)
8732 ((eq arg 'none) nil)
8733 ((eq arg 'done) (or done-word (car org-done-keywords)))
8735 (or (car (cdr (member head org-todo-heads)))
8736 (car org-todo-heads)))
8737 ((eq arg 'previousset)
8738 (let ((org-todo-heads (reverse org-todo-heads)))
8739 (or (car (cdr (member head org-todo-heads)))
8740 (car org-todo-heads))))
8741 ((car (member arg org-todo-keywords-1)))
8742 ((nth (1- (prefix-numeric-value arg))
8743 org-todo-keywords-1))))
8744 ((null member) (or head (car org-todo-keywords-1)))
8745 ((equal this final-done-word) nil) ;; -> make empty
8746 ((null tail) nil) ;; -> first entry
8747 ((memq interpret '(type priority))
8748 (if (eq this-command last-command)
8750 (if (> (length tail) 0)
8751 (or done-word (car org-done-keywords))
8756 (run-hook-with-args-until-success
8757 'org-todo-get-default-hook state last-state)
8759 (next (if state (concat " " state " ") " "))
8760 (change-plist (list :type 'todo-state-change :from this :to state
8761 :position startpos))
8763 (when org-blocker-hook
8764 (setq org-last-todo-state-is-todo
8765 (not (member this org-done-keywords)))
8766 (unless (save-excursion
8768 (run-hook-with-args-until-failure
8769 'org-blocker-hook change-plist)))
8771 (error "TODO state change from %s to %s blocked" this state)
8773 (message "TODO state change from %s to %s blocked" this state)
8774 (throw 'exit nil))))
8775 (store-match-data match-data)
8776 (replace-match next t t)
8777 (unless (pos-visible-in-window-p hl-pos)
8778 (message "TODO state changed to %s" (org-trim next)))
8780 (setq head (org-get-todo-sequence-head state)
8781 ass (assoc head org-todo-kwd-alist)
8782 interpret (nth 1 ass)
8783 done-word (nth 3 ass)
8784 final-done-word (nth 4 ass)))
8785 (when (memq arg '(nextset previousset))
8786 (message "Keyword-Set %d/%d: %s"
8787 (- (length org-todo-sets) -1
8788 (length (memq (assoc state org-todo-sets) org-todo-sets)))
8789 (length org-todo-sets)
8790 (mapconcat 'identity (assoc state org-todo-sets) " ")))
8791 (setq org-last-todo-state-is-todo
8792 (not (member state org-done-keywords)))
8793 (setq now-done-p (and (member state org-done-keywords)
8794 (not (member this org-done-keywords))))
8795 (and logging (org-local-logging logging))
8796 (when (and (or org-todo-log-states org-log-done)
8797 (not (memq arg '(nextset previousset))))
8798 ;; we need to look at recording a time and note
8799 (setq dolog (or (nth 1 (assoc state org-todo-log-states))
8800 (nth 2 (assoc this org-todo-log-states))))
8802 (member state org-not-done-keywords)
8803 (not (member this org-not-done-keywords)))
8804 ;; This is now a todo state and was not one before
8805 ;; If there was a CLOSED time stamp, get rid of it.
8806 (org-add-planning-info nil nil 'closed))
8807 (when (and now-done-p org-log-done)
8808 ;; It is now done, and it was not done before
8809 (org-add-planning-info 'closed (org-current-time))
8810 (if (and (not dolog) (eq 'note org-log-done))
8811 (org-add-log-setup 'done state this 'findpos 'note)))
8812 (when (and state dolog)
8813 ;; This is a non-nil state, and we need to log it
8814 (org-add-log-setup 'state state this 'findpos dolog)))
8815 ;; Fixup tag positioning
8816 (org-todo-trigger-tag-changes state)
8817 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
8818 (when org-provide-todo-statistics
8819 (org-update-parent-todo-statistics))
8820 (run-hooks 'org-after-todo-state-change-hook)
8821 (if (and arg (not (member state org-done-keywords)))
8822 (setq head (org-get-todo-sequence-head state)))
8823 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
8824 ;; Do we need to trigger a repeat?
8826 (when (boundp 'org-agenda-headline-snapshot-before-repeat)
8827 ;; This is for the agenda, take a snapshot of the headline.
8829 (setq org-agenda-headline-snapshot-before-repeat
8830 (org-get-heading))))
8831 (org-auto-repeat-maybe state))
8832 ;; Fixup cursor location if close to the keyword
8833 (if (and (outline-on-heading-p)
8835 (save-excursion (beginning-of-line 1)
8836 (looking-at org-todo-line-regexp))
8837 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
8839 (goto-char (or (match-end 2) (match-end 1)))
8841 (when org-trigger-hook
8843 (run-hook-with-args 'org-trigger-hook change-plist))))))))
8845 (defun org-block-todo-from-children-or-siblings (change-plist)
8846 "Block turning an entry into a TODO, using the hierarchy.
8847 This checks whether the current task should be blocked from state
8848 changes. Such blocking occurs when:
8850 1. The task has children which are not all in a completed state.
8852 2. A task has a parent with the property :ORDERED:, and there
8853 are siblings prior to the current task with incomplete
8856 ;; If this is not a todo state change, or if this entry is already DONE,
8858 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
8859 (member (plist-get change-plist :from)
8860 (cons 'done org-done-keywords))
8861 (member (plist-get change-plist :to)
8862 (cons 'todo org-not-done-keywords)))
8863 (throw 'dont-block t))
8864 ;; If this task has children, and any are undone, it's blocked
8866 (org-back-to-heading t)
8867 (let ((this-level (funcall outline-level)))
8868 (outline-next-heading)
8869 (let ((child-level (funcall outline-level)))
8870 (while (and (not (eobp))
8871 (> child-level this-level))
8872 ;; this todo has children, check whether they are all
8874 (if (and (not (org-entry-is-done-p))
8875 (org-entry-is-todo-p))
8876 (throw 'dont-block nil))
8877 (outline-next-heading)
8878 (setq child-level (funcall outline-level))))))
8879 ;; Otherwise, if the task's parent has the :ORDERED: property, and
8880 ;; any previous siblings are undone, it's blocked
8882 (org-back-to-heading t)
8883 (when (save-excursion
8885 (org-up-heading-all 1)
8886 (org-entry-get (point) "ORDERED")))
8887 (let* ((this-level (funcall outline-level))
8888 (current-level this-level))
8889 (while (and (not (bobp))
8890 (= current-level this-level))
8891 (outline-previous-heading)
8892 (setq current-level (funcall outline-level))
8893 (if (= current-level this-level)
8894 ;; this todo has children, check whether they are all
8896 (if (and (not (org-entry-is-done-p))
8897 (org-entry-is-todo-p))
8898 (throw 'dont-block nil)))))))
8901 (defcustom org-track-ordered-property-with-tag nil
8902 "Should the ORDERED property also be shown as a tag?
8903 The ORDERED property decides if an entry should require subtasks to be
8904 completed in sequence. Since a property is not very visible, setting
8905 this option means that toggling the ORDERED property with the command
8906 `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
8907 not relevant for the behavior, but it makes things more visible.
8909 Note that toggling the tag with tags commands will not change the property
8910 and therefore not influence behavior!
8912 This can be t, meaning the tag ORDERED should be used, It can also be a
8913 string to select a different tag for this task."
8916 (const :tag "No tracking" nil)
8917 (const :tag "Track with ORDERED tag" t)
8918 (string :tag "Use other tag")))
8920 (defun org-toggle-ordered-property ()
8921 "Toggle the ORDERED property of the current entry.
8922 For better visibility, you can track the value of this property with a tag.
8923 See variable `org-track-ordered-property-with-tag'."
8925 (let* ((t1 org-track-ordered-property-with-tag)
8926 (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
8928 (org-back-to-heading)
8929 (if (org-entry-get nil "ORDERED")
8931 (org-delete-property "ORDERED")
8932 (and tag (org-toggle-tag tag 'off))
8933 (message "Subtasks can be completed in arbitrary order"))
8934 (org-entry-put nil "ORDERED" "t")
8935 (and tag (org-toggle-tag tag 'on))
8936 (message "Subtasks must be completed in sequence")))))
8938 (defvar org-blocked-by-checkboxes) ; dynamically scoped
8939 (defun org-block-todo-from-checkboxes (change-plist)
8940 "Block turning an entry into a TODO, using checkboxes.
8941 This checks whether the current task should be blocked from state
8942 changes because there are uncheckd boxes in this entry."
8944 ;; If this is not a todo state change, or if this entry is already DONE,
8946 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
8947 (member (plist-get change-plist :from)
8948 (cons 'done org-done-keywords))
8949 (member (plist-get change-plist :to)
8950 (cons 'todo org-not-done-keywords)))
8951 (throw 'dont-block t))
8952 ;; If this task has checkboxes that are not checked, it's blocked
8954 (org-back-to-heading t)
8955 (let ((beg (point)) end)
8956 (outline-next-heading)
8959 (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
8962 (if (boundp 'org-blocked-by-checkboxes)
8963 (setq org-blocked-by-checkboxes t))
8964 (throw 'dont-block nil)))))
8967 (defun org-update-parent-todo-statistics ()
8968 "Update any statistics cookie in the parent of the current headline."
8970 (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
8971 level (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
8974 (setq level (org-up-heading-safe))
8977 (while (re-search-forward box-re (point-at-eol) t)
8978 (setq cnt-all 0 cnt-done 0 cookie-present t)
8979 (setq is-percent (match-end 2))
8981 (unless (outline-next-heading) (throw 'exit nil))
8982 (while (looking-at org-todo-line-regexp)
8983 (setq kwd (match-string 2))
8984 (and kwd (setq cnt-all (1+ cnt-all)))
8985 (and (member kwd org-done-keywords)
8986 (setq cnt-done (1+ cnt-done)))
8988 (org-forward-same-level 1)
8989 (error (end-of-line 1)))))
8992 (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
8993 (format "[%d/%d]" cnt-done cnt-all))))
8994 (when cookie-present
8995 (run-hook-with-args 'org-after-todo-statistics-hook
8996 cnt-done (- cnt-all cnt-done)))))))
8998 (defvar org-after-todo-statistics-hook nil
8999 "Hook that is called after a TODO statistics cookie has been updated.
9000 Each function is called with two arguments: the number of not-done entries
9001 and the number of done entries.
9003 For example, the following function, when added to this hook, will switch
9004 an entry to DONE when all children are done, and back to TODO when new
9005 entries are set to a TODO status. Note that this hook is only called
9006 when there is a statistics cookie in the headline!
9008 (defun org-summary-todo (n-done n-not-done)
9009 \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
9010 (let (org-log-done org-log-states) ; turn off logging
9011 (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
9014 (defun org-todo-trigger-tag-changes (state)
9015 "Apply the changes defined in `org-todo-state-tags-triggers'."
9016 (let ((l org-todo-state-tags-triggers)
9018 (when (or (not state) (equal state ""))
9019 (setq changes (append changes (cdr (assoc "" l)))))
9020 (when (and (stringp state) (> (length state) 0))
9021 (setq changes (append changes (cdr (assoc state l)))))
9022 (when (member state org-not-done-keywords)
9023 (setq changes (append changes (cdr (assoc 'todo l)))))
9024 (when (member state org-done-keywords)
9025 (setq changes (append changes (cdr (assoc 'done l)))))
9027 (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
9029 (defun org-local-logging (value)
9030 "Get logging settings from a property VALUE."
9032 ;; directly set the variables, they are already local.
9033 (setq org-log-done nil
9035 org-todo-log-states nil)
9036 (setq words (org-split-string value))
9037 (while (setq w (pop words))
9039 ((setq a (assoc w org-startup-options))
9040 (and (member (nth 1 a) '(org-log-done org-log-repeat))
9041 (set (nth 1 a) (nth 2 a))))
9042 ((setq a (org-extract-log-state-settings w))
9043 (and (member (car a) org-todo-keywords-1)
9044 (push a org-todo-log-states)))))))
9046 (defun org-get-todo-sequence-head (kwd)
9047 "Return the head of the TODO sequence to which KWD belongs.
9048 If KWD is not set, check if there is a text property remembering the
9053 (or (get-text-property (point-at-bol) 'org-todo-head)
9055 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
9056 nil (point-at-eol)))
9057 (get-text-property p 'org-todo-head))))
9058 ((not (member kwd org-todo-keywords-1))
9059 (car org-todo-keywords-1))
9060 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
9062 (defun org-fast-todo-selection ()
9063 "Fast TODO keyword selection with single keys.
9064 Returns the new TODO keyword, or nil if no state change should occur."
9065 (let* ((fulltable org-todo-key-alist)
9066 (done-keywords org-done-keywords) ;; needed for the faces.
9067 (maxlen (apply 'max (mapcar
9069 (if (stringp (car x)) (string-width (car x)) 0))
9072 (fwidth (+ maxlen 3 1 3))
9073 (ncol (/ (- (window-width) 4) fwidth))
9077 (save-window-excursion
9079 (set-buffer (get-buffer-create " *Org todo*"))
9080 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
9082 (org-set-local 'org-done-keywords done-keywords)
9083 (setq tbl fulltable cnt 0)
9084 (while (setq e (pop tbl))
9086 ((equal e '(:startgroup))
9087 (push '() groups) (setq ingroup t)
9088 (when (not (= cnt 0))
9092 ((equal e '(:endgroup))
9093 (setq ingroup nil cnt 0)
9095 ((equal e '(:newline))
9096 (when (not (= cnt 0))
9100 (while (equal (car tbl) '(:newline))
9102 (setq tbl (cdr tbl)))))
9104 (setq tg (car e) c (cdr e))
9105 (if ingroup (push tg (car groups)))
9106 (setq tg (org-add-props tg nil 'face
9107 (org-get-todo-face tg)))
9108 (if (and (= cnt 0) (not ingroup)) (insert " "))
9109 (insert "[" c "] " tg (make-string
9110 (- fwidth 4 (length tg)) ?\ ))
9111 (when (= (setq cnt (1+ cnt)) ncol)
9113 (if ingroup (insert " "))
9116 (goto-char (point-min))
9117 (if (not expert) (org-fit-window-to-buffer))
9118 (message "[a-z..]:Set [SPC]:clear")
9119 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
9122 (and (= c ?q) (not (rassoc c fulltable))))
9125 ((setq e (rassoc c fulltable) tg (car e))
9127 (t (setq quit-flag t)))))))
9129 (defun org-entry-is-todo-p ()
9130 (member (org-get-todo-state) org-not-done-keywords))
9132 (defun org-entry-is-done-p ()
9133 (member (org-get-todo-state) org-done-keywords))
9135 (defun org-get-todo-state ()
9137 (org-back-to-heading t)
9138 (and (looking-at org-todo-line-regexp)
9142 (defun org-at-date-range-p (&optional inactive-ok)
9143 "Is the cursor inside a date range?"
9147 (let ((pos (point)))
9148 (skip-chars-backward "^[<\r\n")
9149 (skip-chars-backward "<[")
9150 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
9151 (>= (match-end 0) pos)
9153 (skip-chars-backward "^<[\r\n")
9154 (skip-chars-backward "<[")
9155 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
9156 (>= (match-end 0) pos)
9160 (defun org-get-repeat ()
9161 "Check if there is a deadline/schedule with repeater in this entry."
9164 (org-back-to-heading t)
9165 (if (re-search-forward
9166 org-repeat-re (save-excursion (outline-next-heading) (point)) t)
9167 (match-string 1)))))
9169 (defvar org-last-changed-timestamp)
9170 (defvar org-last-inserted-timestamp)
9171 (defvar org-log-post-message)
9172 (defvar org-log-note-purpose)
9173 (defvar org-log-note-how)
9174 (defvar org-log-note-extra)
9175 (defun org-auto-repeat-maybe (done-word)
9176 "Check if the current headline contains a repeated deadline/schedule.
9177 If yes, set TODO state back to what it was and change the base date
9178 of repeating deadline/scheduled time stamps to new date.
9179 This function is run automatically after each state change to a DONE state."
9180 ;; last-state is dynamically scoped into this function
9181 (let* ((repeat (org-get-repeat))
9182 (aa (assoc last-state org-todo-kwd-alist))
9183 (interpret (nth 1 aa))
9185 (whata '(("d" . day) ("m" . month) ("y" . year)))
9186 (msg "Entry repeats: ")
9188 (org-todo-log-states nil)
9189 (nshiftmax 10) (nshift 0)
9190 re type n what ts time)
9192 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
9193 (org-todo (if (eq interpret 'type) last-state head))
9194 (when org-log-repeat
9195 (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
9196 (memq 'org-add-log-note post-command-hook))
9197 ;; OK, we are already setup for some record
9198 (if (eq org-log-repeat 'note)
9199 ;; make sure we take a note, not only a time stamp
9200 (setq org-log-note-how 'note))
9201 ;; Set up for taking a record
9202 (org-add-log-setup 'state (or done-word (car org-done-keywords))
9204 'findpos org-log-repeat)))
9205 (org-back-to-heading t)
9206 (org-add-planning-info nil nil 'closed)
9207 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
9208 org-deadline-time-regexp "\\)\\|\\("
9209 org-ts-regexp "\\)"))
9210 (while (re-search-forward
9211 re (save-excursion (outline-next-heading) (point)) t)
9212 (setq type (if (match-end 1) org-scheduled-string
9213 (if (match-end 3) org-deadline-string "Plain:"))
9214 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
9215 (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
9216 (setq n (string-to-number (match-string 2 ts))
9217 what (match-string 3 ts))
9218 (if (equal what "w") (setq n (* n 7) what "d"))
9219 ;; Preparation, see if we need to modify the start date for the change
9221 (setq time (save-match-data (org-time-string-to-time ts)))
9223 ((equal (match-string 1 ts) ".")
9224 ;; Shift starting date to today
9225 (org-timestamp-change
9226 (- (time-to-days (current-time)) (time-to-days time))
9228 ((equal (match-string 1 ts) "+")
9229 (while (or (= nshift 0)
9230 (<= (time-to-days time) (time-to-days (current-time))))
9231 (when (= (incf nshift) nshiftmax)
9232 (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
9234 (org-timestamp-change n (cdr (assoc what whata)))
9235 (org-at-timestamp-p t)
9236 (setq ts (match-string 1))
9237 (setq time (save-match-data (org-time-string-to-time ts))))
9238 (org-timestamp-change (- n) (cdr (assoc what whata)))
9239 ;; rematch, so that we have everything in place for the real shift
9240 (org-at-timestamp-p t)
9241 (setq ts (match-string 1))
9242 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
9243 (org-timestamp-change n (cdr (assoc what whata)))
9244 (setq msg (concat msg type " " org-last-changed-timestamp " "))))
9245 (setq org-log-post-message msg)
9246 (message "%s" msg))))
9248 (defun org-show-todo-tree (arg)
9249 "Make a compact tree which shows all headlines marked with TODO.
9250 The tree will show the lines where the regexp matches, and all higher
9251 headlines above the match.
9252 With a \\[universal-argument] prefix, also show the DONE entries.
9253 With a numeric prefix N, construct a sparse tree for the Nth element
9254 of `org-todo-keywords-1'."
9256 (let ((case-fold-search nil)
9258 (cond ((null arg) org-not-done-regexp)
9260 (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
9261 (mapcar 'list org-todo-keywords-1))))
9263 (mapconcat 'identity (org-split-string kwd "|") "\\|")
9265 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
9266 (regexp-quote (nth (1- (prefix-numeric-value arg))
9267 org-todo-keywords-1)))
9268 (t (error "Invalid prefix argument: %s" arg)))))
9269 (message "%d TODO entries found"
9270 (org-occur (concat "^" outline-regexp " *" kwd-re )))))
9272 (defun org-deadline (&optional remove time)
9273 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
9274 With argument REMOVE, remove any deadline from the item.
9275 When TIME is set, it should be an internal time specification, and the
9276 scheduling will use the corresponding date."
9280 (org-remove-timestamp-with-keyword org-deadline-string)
9281 (message "Item no longer has a deadline."))
9282 (if (org-get-repeat)
9283 (error "Cannot change deadline on task with repeater, please do that by hand")
9284 (org-add-planning-info 'deadline time 'closed)
9285 (message "Deadline on %s" org-last-inserted-timestamp))))
9287 (defun org-schedule (&optional remove time)
9288 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
9289 With argument REMOVE, remove any scheduling date from the item.
9290 When TIME is set, it should be an internal time specification, and the
9291 scheduling will use the corresponding date."
9295 (org-remove-timestamp-with-keyword org-scheduled-string)
9296 (message "Item is no longer scheduled."))
9297 (if (org-get-repeat)
9298 (error "Cannot reschedule task with repeater, please do that by hand")
9299 (org-add-planning-info 'scheduled time 'closed)
9300 (message "Scheduled to %s" org-last-inserted-timestamp))))
9302 (defun org-get-scheduled-time (pom &optional inherit)
9303 "Get the scheduled time as a time tuple, of a format suitable
9304 for calling org-schedule with, or if there is no scheduling,
9306 (let ((time (org-entry-get pom "SCHEDULED" inherit)))
9308 (apply 'encode-time (org-parse-time-string time)))))
9310 (defun org-get-deadline-time (pom &optional inherit)
9311 "Get the deadine as a time tuple, of a format suitable for
9312 calling org-deadlin with, or if there is no scheduling, returns
9314 (let ((time (org-entry-get pom "DEADLINE" inherit)))
9316 (apply 'encode-time (org-parse-time-string time)))))
9318 (defun org-remove-timestamp-with-keyword (keyword)
9319 "Remove all time stamps with KEYWORD in the current entry."
9320 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
9323 (org-back-to-heading t)
9325 (org-end-of-subtree t t)
9326 (while (re-search-backward re beg t)
9328 (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
9329 (equal (char-before) ?\ ))
9330 (backward-delete-char 1)
9331 (if (string-match "^[ \t]*$" (buffer-substring
9332 (point-at-bol) (point-at-eol)))
9333 (delete-region (point-at-bol)
9334 (min (point-max) (1+ (point-at-eol))))))))))
9336 (defun org-add-planning-info (what &optional time &rest remove)
9337 "Insert new timestamp with keyword in the line directly after the headline.
9338 WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
9339 If non is given, the user is prompted for a date.
9340 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
9343 (let (org-time-was-given org-end-time-was-given ts
9344 end default-time default-input)
9347 (when (and (not time) (memq what '(scheduled deadline)))
9348 ;; Try to get a default date/time from existing timestamp
9350 (org-back-to-heading t)
9351 (setq end (save-excursion (outline-next-heading) (point)))
9352 (when (re-search-forward (if (eq what 'scheduled)
9353 org-scheduled-time-regexp
9354 org-deadline-time-regexp)
9356 (setq ts (match-string 1)
9358 (apply 'encode-time (org-parse-time-string ts))
9359 default-input (and ts (org-get-compact-tod ts))))))
9361 ;; If necessary, get the time from the user
9362 (setq time (or time (org-read-date nil 'to-time nil nil
9363 default-time default-input))))
9365 (when (and org-insert-labeled-timestamps-at-point
9366 (member what '(scheduled deadline)))
9368 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
9369 (org-insert-time-stamp time org-time-was-given
9370 nil nil nil (list org-end-time-was-given))
9374 (let (col list elt ts buffer-invisibility-spec)
9375 (org-back-to-heading t)
9376 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
9377 (goto-char (match-end 1))
9378 (setq col (current-column))
9379 (goto-char (match-end 0))
9380 (if (eobp) (insert "\n") (forward-char 1))
9381 (when (and (not what)
9384 org-keyword-time-not-clock-regexp))))
9385 ;; Nothing to add, nothing to remove...... :-)
9387 (if (and (not (looking-at outline-regexp))
9388 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
9390 (not (equal (match-string 1) org-clock-string)))
9391 (narrow-to-region (match-beginning 0) (match-end 0))
9392 (insert-before-markers "\n")
9394 (narrow-to-region (point) (point))
9395 (and org-adapt-indentation (org-indent-to-column col)))
9396 ;; Check if we have to remove something.
9397 (setq list (cons what remove))
9399 (setq elt (pop list))
9400 (goto-char (point-min))
9401 (when (or (and (eq elt 'scheduled)
9402 (re-search-forward org-scheduled-time-regexp nil t))
9403 (and (eq elt 'deadline)
9404 (re-search-forward org-deadline-time-regexp nil t))
9405 (and (eq elt 'closed)
9406 (re-search-forward org-closed-time-regexp nil t)))
9408 (if (looking-at "--+<[^>]+>") (replace-match ""))
9409 (if (looking-at " +") (replace-match ""))))
9410 (goto-char (point-max))
9413 (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
9414 (cond ((eq what 'scheduled) org-scheduled-string)
9415 ((eq what 'deadline) org-deadline-string)
9416 ((eq what 'closed) org-closed-string))
9418 (setq ts (org-insert-time-stamp
9420 (or org-time-was-given
9421 (and (eq what 'closed) org-log-done-with-time))
9423 nil nil (list org-end-time-was-given)))
9425 (goto-char (point-min))
9427 (if (and (looking-at "[ \t]+\n")
9428 (equal (char-before) ?\n))
9429 (delete-region (1- (point)) (point-at-eol)))
9432 (defvar org-log-note-marker (make-marker))
9433 (defvar org-log-note-purpose nil)
9434 (defvar org-log-note-state nil)
9435 (defvar org-log-note-previous-state nil)
9436 (defvar org-log-note-how nil)
9437 (defvar org-log-note-extra nil)
9438 (defvar org-log-note-window-configuration nil)
9439 (defvar org-log-note-return-to (make-marker))
9440 (defvar org-log-post-message nil
9441 "Message to be displayed after a log note has been stored.
9442 The auto-repeater uses this.")
9444 (defun org-add-note ()
9445 "Add a note to the current entry.
9446 This is done in the same way as adding a state change note."
9448 (org-add-log-setup 'note nil nil 'findpos nil))
9450 (defvar org-property-end-re)
9451 (defun org-add-log-setup (&optional purpose state prev-state
9452 findpos how &optional extra)
9453 "Set up the post command hook to take a note.
9454 If this is about to TODO state change, the new state is expected in STATE.
9455 When FINDPOS is non-nil, find the correct position for the note in
9456 the current entry. If not, assume that it can be inserted at point.
9457 HOW is an indicator what kind of note should be created.
9458 EXTRA is additional text that will be inserted into the notes buffer."
9459 (let ((drawer (cond ((stringp org-log-into-drawer)
9460 org-log-into-drawer)
9461 (org-log-into-drawer "LOGBOOK")
9466 (org-back-to-heading t)
9467 (narrow-to-region (point) (save-excursion
9468 (outline-next-heading) (point)))
9469 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
9470 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
9472 (goto-char (match-end 0))
9475 (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
9478 (goto-char (match-end 0))
9479 (or org-log-states-order-reversed
9480 (and (re-search-forward org-property-end-re nil t)
9481 (goto-char (1- (match-beginning 0))))))
9482 (insert "\n:" drawer ":\n:END:")
9483 (beginning-of-line 0)
9484 (org-indent-line-function)
9485 (beginning-of-line 2)
9486 (org-indent-line-function)
9488 ((and org-log-state-notes-insert-after-drawers
9490 (forward-line) (looking-at org-drawer-regexp)))
9492 (while (looking-at org-drawer-regexp)
9493 (goto-char (match-end 0))
9494 (re-search-forward org-property-end-re (point-max) t)
9497 (unless org-log-states-order-reversed
9498 (and (= (char-after) ?\n) (forward-char 1))
9499 (org-skip-over-state-notes)
9500 (skip-chars-backward " \t\n\r")))
9501 (move-marker org-log-note-marker (point))
9502 (setq org-log-note-purpose purpose
9503 org-log-note-state state
9504 org-log-note-previous-state prev-state
9505 org-log-note-how how
9506 org-log-note-extra extra)
9507 (add-hook 'post-command-hook 'org-add-log-note 'append)))))
9509 (defun org-skip-over-state-notes ()
9510 "Skip past the list of State notes in an entry."
9511 (if (looking-at "\n[ \t]*- State") (forward-char 1))
9512 (while (looking-at "[ \t]*- State")
9515 (error (org-end-of-item)))))
9517 (defun org-add-log-note (&optional purpose)
9518 "Pop up a window for taking a note, and add this note later at point."
9519 (remove-hook 'post-command-hook 'org-add-log-note)
9520 (setq org-log-note-window-configuration (current-window-configuration))
9521 (delete-other-windows)
9522 (move-marker org-log-note-return-to (point))
9523 (switch-to-buffer (marker-buffer org-log-note-marker))
9524 (goto-char org-log-note-marker)
9525 (org-switch-to-buffer-other-window "*Org Note*")
9527 (if (memq org-log-note-how '(time state))
9528 (let (current-prefix-arg) (org-store-log-note))
9529 (let ((org-inhibit-startup t)) (org-mode))
9530 (insert (format "# Insert note for %s.
9531 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
9533 ((eq org-log-note-purpose 'clock-out) "stopped clock")
9534 ((eq org-log-note-purpose 'done) "closed todo item")
9535 ((eq org-log-note-purpose 'state)
9536 (format "state change from \"%s\" to \"%s\""
9537 (or org-log-note-previous-state "")
9538 (or org-log-note-state "")))
9539 ((eq org-log-note-purpose 'note)
9541 (t (error "This should not happen")))))
9542 (if org-log-note-extra (insert org-log-note-extra))
9543 (org-set-local 'org-finish-function 'org-store-log-note)))
9545 (defvar org-note-abort nil) ; dynamically scoped
9546 (defun org-store-log-note ()
9547 "Finish taking a log note, and insert it to where it belongs."
9548 (let ((txt (buffer-string))
9549 (note (cdr (assq org-log-note-purpose org-log-note-headings)))
9551 (kill-buffer (current-buffer))
9552 (while (string-match "\\`#.*\n[ \t\n]*" txt)
9553 (setq txt (replace-match "" t t txt)))
9554 (if (string-match "\\s-+\\'" txt)
9555 (setq txt (replace-match "" t t txt)))
9556 (setq lines (org-split-string txt "\n"))
9557 (when (and note (string-match "\\S-" note))
9559 (org-replace-escapes
9561 (list (cons "%u" (user-login-name))
9562 (cons "%U" user-full-name)
9563 (cons "%t" (format-time-string
9564 (org-time-stamp-format 'long 'inactive)
9566 (cons "%s" (if org-log-note-state
9567 (concat "\"" org-log-note-state "\"")
9569 (cons "%S" (if org-log-note-previous-state
9570 (concat "\"" org-log-note-previous-state "\"")
9572 (if lines (setq note (concat note " \\\\")))
9574 (when (or current-prefix-arg org-note-abort)
9575 (when org-log-into-drawer
9576 (org-remove-empty-drawer-at
9577 (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
9578 org-log-note-marker))
9582 (set-buffer (marker-buffer org-log-note-marker))
9584 (goto-char org-log-note-marker)
9585 (move-marker org-log-note-marker nil)
9587 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
9588 (insert "- " (pop lines))
9589 (org-indent-line-function)
9590 (beginning-of-line 1)
9591 (looking-at "[ \t]*")
9592 (setq ind (concat (match-string 0) " "))
9594 (while lines (insert "\n" ind (pop lines)))
9595 (message "Note stored")
9596 (org-back-to-heading t)
9597 (org-cycle-hide-drawers 'children)))))
9598 (set-window-configuration org-log-note-window-configuration)
9599 (with-current-buffer (marker-buffer org-log-note-return-to)
9600 (goto-char org-log-note-return-to))
9601 (move-marker org-log-note-return-to nil)
9602 (and org-log-post-message (message "%s" org-log-post-message)))
9604 (defun org-remove-empty-drawer-at (drawer pos)
9605 "Remove an emptyr DARWER drawer at position POS.
9606 POS may also be a marker."
9607 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
9613 (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
9614 (replace-match ""))))))
9616 (defun org-sparse-tree (&optional arg)
9617 "Create a sparse tree, prompt for the details.
9618 This command can create sparse trees. You first need to select the type
9619 of match used to create the tree:
9621 t Show entries with a specific TODO keyword.
9622 m Show entries selected by a tags/property match.
9623 p Enter a property name and its value (both with completion on existing
9624 names/values) and show entries with that property.
9625 r Show entries matching a regular expression.
9626 d Show deadlines due within `org-deadline-warning-days'.
9627 b Show deadlines and scheduled items before a date.
9628 a Show deadlines and scheduled items after a date."
9630 (let (ans kwd value)
9631 (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
9632 (setq ans (read-char-exclusive))
9635 (call-interactively 'org-check-deadlines))
9637 (call-interactively 'org-check-before-date))
9639 (call-interactively 'org-check-after-date))
9641 (org-show-todo-tree '(4)))
9642 ((member ans '(?T ?m))
9643 (call-interactively 'org-match-sparse-tree))
9644 ((member ans '(?p ?P))
9645 (setq kwd (org-ido-completing-read "Property: "
9646 (mapcar 'list (org-buffer-property-keys))))
9647 (setq value (org-ido-completing-read "Value: "
9648 (mapcar 'list (org-property-values kwd))))
9649 (unless (string-match "\\`{.*}\\'" value)
9650 (setq value (concat "\"" value "\"")))
9651 (org-match-sparse-tree arg (concat kwd "=" value)))
9652 ((member ans '(?r ?R ?/))
9653 (call-interactively 'org-occur))
9654 (t (error "No such sparse tree command \"%c\"" ans)))))
9656 (defvar org-occur-highlights nil
9657 "List of overlays used for occur matches.")
9658 (make-variable-buffer-local 'org-occur-highlights)
9659 (defvar org-occur-parameters nil
9660 "Parameters of the active org-occur calls.
9661 This is a list, each call to org-occur pushes as cons cell,
9662 containing the regular expression and the callback, onto the list.
9663 The list can contain several entries if `org-occur' has been called
9664 several time with the KEEP-PREVIOUS argument. Otherwise, this list
9665 will only contain one set of parameters. When the highlights are
9666 removed (for example with `C-c C-c', or with the next edit (depending
9667 on `org-remove-highlights-with-change'), this variable is emptied
9669 (make-variable-buffer-local 'org-occur-parameters)
9671 (defun org-occur (regexp &optional keep-previous callback)
9672 "Make a compact tree which shows all matches of REGEXP.
9673 The tree will show the lines where the regexp matches, and all higher
9674 headlines above the match. It will also show the heading after the match,
9675 to make sure editing the matching entry is easy.
9676 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
9677 call to `org-occur' will be kept, to allow stacking of calls to this
9679 If CALLBACK is non-nil, it is a function which is called to confirm
9680 that the match should indeed be shown."
9681 (interactive "sRegexp: \nP")
9682 (unless keep-previous
9683 (org-remove-occur-highlights nil nil t))
9684 (push (cons regexp callback) org-occur-parameters)
9687 (goto-char (point-min))
9688 (if (or (not keep-previous) ; do not want to keep
9689 (not org-occur-highlights)) ; no previous matches
9692 (while (re-search-forward regexp nil t)
9693 (when (or (not callback)
9694 (save-match-data (funcall callback)))
9696 (when org-highlight-sparse-tree-matches
9697 (org-highlight-new-match (match-beginning 0) (match-end 0)))
9698 (org-show-context 'occur-tree))))
9699 (when org-remove-highlights-with-change
9700 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
9702 (unless org-sparse-tree-open-archived-trees
9703 (org-hide-archived-subtrees (point-min) (point-max)))
9704 (run-hooks 'org-occur-hook)
9706 (message "%d match(es) for regexp %s" cnt regexp))
9709 (defun org-show-context (&optional key)
9710 "Make sure point and context and visible.
9711 How much context is shown depends upon the variables
9712 `org-show-hierarchy-above', `org-show-following-heading'. and
9713 `org-show-siblings'."
9714 (let ((heading-p (org-on-heading-p t))
9715 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
9716 (following-p (org-get-alist-option org-show-following-heading key))
9717 (entry-p (org-get-alist-option org-show-entry-below key))
9718 (siblings-p (org-get-alist-option org-show-siblings key)))
9720 ;; Show heading or entry text
9721 (if (and heading-p (not entry-p))
9722 (org-flag-heading nil) ; only show the heading
9723 (and (or entry-p (org-invisible-p) (org-invisible-p2))
9724 (org-show-hidden-entry))) ; show entire entry
9726 ;; Show next sibling, or heading below text
9728 (and (if heading-p (org-goto-sibling) (outline-next-heading))
9729 (org-flag-heading nil))))
9730 (when siblings-p (org-show-siblings))
9732 ;; show all higher headings, possibly with siblings
9734 (while (and (condition-case nil
9735 (progn (org-up-heading-all 1) t)
9738 (org-flag-heading nil)
9739 (when siblings-p (org-show-siblings))))))))
9741 (defun org-reveal (&optional siblings)
9742 "Show current entry, hierarchy above it, and the following headline.
9743 This can be used to show a consistent set of context around locations
9744 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
9745 not t for the search context.
9747 With optional argument SIBLINGS, on each level of the hierarchy all
9748 siblings are shown. This repairs the tree structure to what it would
9749 look like when opened with hierarchical calls to `org-cycle'."
9751 (let ((org-show-hierarchy-above t)
9752 (org-show-following-heading t)
9753 (org-show-siblings (if siblings t org-show-siblings)))
9754 (org-show-context nil)))
9756 (defun org-highlight-new-match (beg end)
9757 "Highlight from BEG to END and mark the highlight is an occur headline."
9758 (let ((ov (org-make-overlay beg end)))
9759 (org-overlay-put ov 'face 'secondary-selection)
9760 (push ov org-occur-highlights)))
9762 (defun org-remove-occur-highlights (&optional beg end noremove)
9763 "Remove the occur highlights from the buffer.
9764 BEG and END are ignored. If NOREMOVE is nil, remove this function
9765 from the `before-change-functions' in the current buffer."
9767 (unless org-inhibit-highlight-removal
9768 (mapc 'org-delete-overlay org-occur-highlights)
9769 (setq org-occur-highlights nil)
9770 (setq org-occur-parameters nil)
9772 (remove-hook 'before-change-functions
9773 'org-remove-occur-highlights 'local))))
9777 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
9778 "Regular expression matching the priority indicator.")
9780 (defvar org-remove-priority-next-time nil)
9782 (defun org-priority-up ()
9783 "Increase the priority of the current item."
9787 (defun org-priority-down ()
9788 "Decrease the priority of the current item."
9790 (org-priority 'down))
9792 (defun org-priority (&optional action)
9793 "Change the priority of an item by ARG.
9794 ACTION can be `set', `up', `down', or a character."
9796 (setq action (or action 'set))
9797 (let (current new news have remove)
9799 (org-back-to-heading t)
9800 (if (looking-at org-priority-regexp)
9801 (setq current (string-to-char (match-string 2))
9803 (setq current org-default-priority))
9805 ((or (eq action 'set)
9806 (if (featurep 'xemacs) (characterp action) (integerp action)))
9807 (if (not (eq action 'set))
9809 (message "Priority %c-%c, SPC to remove: "
9810 org-highest-priority org-lowest-priority)
9811 (setq new (read-char-exclusive)))
9812 (if (and (= (upcase org-highest-priority) org-highest-priority)
9813 (= (upcase org-lowest-priority) org-lowest-priority))
9814 (setq new (upcase new)))
9815 (cond ((equal new ?\ ) (setq remove t))
9816 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
9817 (error "Priority must be between `%c' and `%c'"
9818 org-highest-priority org-lowest-priority))))
9820 (if (and (not have) (eq last-command this-command))
9821 (setq new org-lowest-priority)
9822 (setq new (if (and org-priority-start-cycle-with-default (not have))
9823 org-default-priority (1- current)))))
9825 (if (and (not have) (eq last-command this-command))
9826 (setq new org-highest-priority)
9827 (setq new (if (and org-priority-start-cycle-with-default (not have))
9828 org-default-priority (1+ current)))))
9829 (t (error "Invalid action")))
9830 (if (or (< (upcase new) org-highest-priority)
9831 (> (upcase new) org-lowest-priority))
9833 (setq news (format "%c" new))
9836 (replace-match "" t t nil 1)
9837 (replace-match news t t nil 2))
9839 (error "No priority cookie found in line")
9840 (looking-at org-todo-line-regexp)
9843 (goto-char (match-end 2))
9844 (insert " [#" news "]"))
9845 (goto-char (match-beginning 3))
9846 (insert "[#" news "] ")))))
9847 (org-preserve-lc (org-set-tags nil 'align))
9849 (message "Priority removed")
9850 (message "Priority of current item set to %s" news))))
9853 (defun org-get-priority (s)
9854 "Find priority cookie and return priority."
9856 (if (not (string-match org-priority-regexp s))
9857 (* 1000 (- org-lowest-priority org-default-priority))
9858 (* 1000 (- org-lowest-priority
9859 (string-to-char (match-string 2 s)))))))
9863 (defvar org-agenda-archives-mode)
9864 (defvar org-scanner-tags nil
9865 "The current tag list while the tags scanner is running.")
9866 (defvar org-trust-scanner-tags nil
9867 "Should `org-get-tags-at' use the tags fro the scanner.
9868 This is for internal dynamical scoping only.
9869 When this is non-nil, the function `org-get-tags-at' will return the value
9870 of `org-scanner-tags' instead of building the list by itself. This
9871 can lead to large speed-ups when the tags scanner is used in a file with
9872 many entries, and when the list of tags is retrieved, for example to
9873 obtain a list of properties. Building the tags list for each entry in such
9874 a file becomes an N^2 operation - but with this variable set, it scales
9877 (defun org-scan-tags (action matcher &optional todo-only)
9878 "Scan headline tags with inheritance and produce output ACTION.
9880 ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
9881 or `agenda' to produce an entry list for an agenda view. It can also be
9882 a Lisp form or a function that should be called at each matched headline, in
9883 this case the return value is a list of all return values from these calls.
9885 MATCHER is a Lisp form to be evaluated, testing if a given set of tags
9886 qualifies a headline for inclusion. When TODO-ONLY is non-nil,
9887 only lines with a TODO keyword are included in the output."
9888 (require 'org-agenda)
9889 (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
9890 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
9892 "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
9893 (props (list 'face 'default
9894 'done-face 'org-agenda-done
9895 'undone-face 'default
9896 'mouse-face 'highlight
9897 'org-not-done-regexp org-not-done-regexp
9898 'org-todo-regexp org-todo-regexp
9899 'keymap org-agenda-keymap
9901 (format "mouse-2 or RET jump to org file %s"
9902 (abbreviate-file-name
9903 (or (buffer-file-name (buffer-base-buffer))
9904 (buffer-name (buffer-base-buffer)))))))
9905 (case-fold-search nil)
9906 lspos tags tags-list
9907 (tags-alist (list (cons 0 org-file-tags)))
9908 (llast 0) rtn rtn1 level category i txt
9909 todo marker entry priority)
9910 (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
9911 (setq action (list 'lambda nil action)))
9913 (goto-char (point-min))
9914 (when (eq action 'sparse-tree)
9916 (org-remove-occur-highlights))
9917 (while (re-search-forward re nil t)
9919 (setq todo (if (match-end 1) (org-match-string-no-properties 2))
9920 tags (if (match-end 4) (org-match-string-no-properties 4)))
9921 (goto-char (setq lspos (1+ (match-beginning 0))))
9922 (setq level (org-reduced-level (funcall outline-level))
9923 category (org-get-category))
9924 (setq i llast llast level)
9925 ;; remove tag lists from same and sublevels
9927 (when (setq entry (assoc i tags-alist))
9928 (setq tags-alist (delete entry tags-alist)))
9930 ;; add the next tags
9932 (setq tags (org-split-string tags ":")
9934 (cons (cons level tags) tags-alist)))
9935 ;; compile tags for current headline
9937 (if org-use-tag-inheritance
9938 (apply 'append (mapcar 'cdr (reverse tags-alist)))
9940 org-scanner-tags tags-list)
9941 (when org-use-tag-inheritance
9942 (setcdr (car tags-alist)
9944 (setq x (copy-sequence x))
9945 (org-add-prop-inherited x))
9946 (cdar tags-alist))))
9947 (when (and tags org-use-tag-inheritance
9948 (or (not (eq t org-use-tag-inheritance))
9949 org-tags-exclude-from-inheritance))
9950 ;; selective inheritance, remove uninherited ones
9951 (setcdr (car tags-alist)
9952 (org-remove-uniherited-tags (cdar tags-alist))))
9953 (when (and (or (not todo-only)
9954 (and (member todo org-not-done-keywords)
9955 (or (not org-agenda-tags-todo-honor-ignore-options)
9956 (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
9957 (let ((case-fold-search t)) (eval matcher))
9959 (not (member org-archive-tag tags-list))
9960 ;; we have an archive tag, should we use this anyway?
9961 (or (not org-agenda-skip-archived-trees)
9962 (and (eq action 'agenda) org-agenda-archives-mode))))
9963 (unless (eq action 'sparse-tree) (org-agenda-skip))
9965 ;; select this headline
9968 ((eq action 'sparse-tree)
9969 (and org-highlight-sparse-tree-matches
9970 (org-get-heading) (match-end 0)
9971 (org-highlight-new-match
9972 (match-beginning 0) (match-beginning 1)))
9973 (org-show-context 'tags-tree))
9974 ((eq action 'agenda)
9975 (setq txt (org-format-agenda-item
9978 (if (eq org-tags-match-list-sublevels 'indented)
9979 (make-string (1- level) ?.) "")
9984 priority (org-get-priority txt))
9986 (setq marker (org-agenda-new-marker))
9987 (org-add-props txt props
9988 'org-marker marker 'org-hd-marker marker 'org-category category
9990 'priority priority 'type "tagsmatch")
9994 (setq rtn1 (funcall action))
9996 (goto-char (point-at-eol)))
9997 (t (error "Invalid action")))
9999 ;; if we are to skip sublevels, jump to end of subtree
10000 (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
10001 (when (and (eq action 'sparse-tree)
10002 (not org-sparse-tree-open-archived-trees))
10003 (org-hide-archived-subtrees (point-min) (point-max)))
10006 (defun org-remove-uniherited-tags (tags)
10007 "Remove all tags that are not inherited from the list TAGS."
10009 ((eq org-use-tag-inheritance t)
10010 (if org-tags-exclude-from-inheritance
10011 (org-delete-all org-tags-exclude-from-inheritance tags)
10013 ((not org-use-tag-inheritance) nil)
10014 ((stringp org-use-tag-inheritance)
10017 (if (and (string-match org-use-tag-inheritance x)
10018 (not (member x org-tags-exclude-from-inheritance)))
10021 ((listp org-use-tag-inheritance)
10024 (if (member x org-use-tag-inheritance) x nil))
10027 (defvar todo-only) ;; dynamically scoped
10029 (defun org-match-sparse-tree (&optional todo-only match)
10030 "Create a sparse tree according to tags string MATCH.
10031 MATCH can contain positive and negative selection of tags, like
10032 \"+WORK+URGENT-WITHBOSS\".
10033 If optional argument TODO-ONLY is non-nil, only select lines that are
10036 (org-prepare-agenda-buffers (list (current-buffer)))
10037 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
10039 (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
10041 (defvar org-cached-props nil)
10042 (defun org-cached-entry-get (pom property)
10043 (if (or (eq t org-use-property-inheritance)
10044 (and (stringp org-use-property-inheritance)
10045 (string-match org-use-property-inheritance property))
10046 (and (listp org-use-property-inheritance)
10047 (member property org-use-property-inheritance)))
10048 ;; Caching is not possible, check it directly
10049 (org-entry-get pom property 'inherit)
10050 ;; Get all properties, so that we can do complicated checks easily
10051 (cdr (assoc property (or org-cached-props
10052 (setq org-cached-props
10053 (org-entry-properties pom)))))))
10055 (defun org-global-tags-completion-table (&optional files)
10056 "Return the list of all tags in all agenda buffer/files."
10063 (set-buffer (find-file-noselect file))
10064 (append (org-get-buffer-tags)
10065 (mapcar (lambda (x) (if (stringp (car-safe x))
10066 (list (car-safe x)) nil))
10068 (if (and files (car files))
10070 (org-agenda-files))))))))
10072 (defun org-make-tags-matcher (match)
10073 "Create the TAGS//TODO matcher form for the selection string MATCH."
10074 ;; todo-only is scoped dynamically into this function, and the function
10075 ;; may change it if the matcher asks for it.
10077 ;; Get a new match request, with completion
10078 (let ((org-last-tags-completion-table
10079 (org-global-tags-completion-table)))
10080 (setq match (org-completing-read-no-ido
10081 "Match: " 'org-tags-completion-function nil nil nil
10082 'org-tags-history))))
10084 ;; Parse the string and create a lisp form
10085 (let ((match0 match)
10086 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
10088 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
10089 orterms term orlist re-p str-p level-p level-op time-p
10090 prop-p pn pv po cat-p gv rest)
10091 (if (string-match "/+" match)
10092 ;; match contains also a todo-matching request
10094 (setq tagsmatch (substring match 0 (match-beginning 0))
10095 todomatch (substring match (match-end 0)))
10096 (if (string-match "^!" todomatch)
10097 (setq todo-only t todomatch (substring todomatch 1)))
10098 (if (string-match "^\\s-*$" todomatch)
10099 (setq todomatch nil)))
10100 ;; only matching tags
10101 (setq tagsmatch match todomatch nil))
10103 ;; Make the tags matcher
10104 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
10105 (setq tagsmatcher t)
10106 (setq orterms (org-split-string tagsmatch "|") orlist nil)
10107 (while (setq term (pop orterms))
10108 (while (and (equal (substring term -1) "\\") orterms)
10109 (setq term (concat term "|" (pop orterms)))) ; repair bad split
10110 (while (string-match re term)
10111 (setq rest (substring term (match-end 0))
10112 minus (and (match-end 1)
10113 (equal (match-string 1 term) "-"))
10114 tag (match-string 2 term)
10115 re-p (equal (string-to-char tag) ?{)
10116 level-p (match-end 4)
10117 prop-p (match-end 5)
10119 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
10121 (setq level-op (org-op-to-function (match-string 3 term)))
10122 `(,level-op level ,(string-to-number
10123 (match-string 4 term))))
10125 (setq pn (match-string 5 term)
10126 po (match-string 6 term)
10127 pv (match-string 7 term)
10128 cat-p (equal pn "CATEGORY")
10129 re-p (equal (string-to-char pv) ?{)
10130 str-p (equal (string-to-char pv) ?\")
10131 time-p (save-match-data
10132 (string-match "^\"[[<].*[]>]\"$" pv))
10133 pv (if (or re-p str-p) (substring pv 1 -1) pv))
10134 (if time-p (setq pv (org-matcher-time pv)))
10135 (setq po (org-op-to-function po (if time-p 'time str-p)))
10137 ((equal pn "CATEGORY")
10138 (setq gv '(get-text-property (point) 'org-category)))
10142 (setq gv `(org-cached-entry-get nil ,pn))))
10145 `(not (string-match ,pv (or ,gv "")))
10146 `(string-match ,pv (or ,gv "")))
10148 `(,po (or ,gv "") ,pv)
10149 `(,po (string-to-number (or ,gv ""))
10150 ,(string-to-number pv) ))))
10151 (t `(member ,tag tags-list)))
10152 mm (if minus (list 'not mm) mm)
10154 (push mm tagsmatcher))
10155 (push (if (> (length tagsmatcher) 1)
10156 (cons 'and tagsmatcher)
10159 (setq tagsmatcher nil))
10160 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
10162 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
10163 ;; Make the todo matcher
10164 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
10165 (setq todomatcher t)
10166 (setq orterms (org-split-string todomatch "|") orlist nil)
10167 (while (setq term (pop orterms))
10168 (while (string-match re term)
10169 (setq minus (and (match-end 1)
10170 (equal (match-string 1 term) "-"))
10171 kwd (match-string 2 term)
10172 re-p (equal (string-to-char kwd) ?{)
10173 term (substring term (match-end 0))
10175 `(string-match ,(substring kwd 1 -1) todo)
10176 (list 'equal 'todo kwd))
10177 mm (if minus (list 'not mm) mm))
10178 (push mm todomatcher))
10179 (push (if (> (length todomatcher) 1)
10180 (cons 'and todomatcher)
10183 (setq todomatcher nil))
10184 (setq todomatcher (if (> (length orlist) 1)
10185 (cons 'or orlist) (car orlist))))
10187 ;; Return the string and lisp forms of the matcher
10188 (setq matcher (if todomatcher
10189 (list 'and tagsmatcher todomatcher)
10191 (cons match0 matcher)))
10193 (defun org-op-to-function (op &optional stringp)
10194 "Turn an operator into the appropriate function."
10197 ((equal op "<" ) '(< string< org-time<))
10198 ((equal op ">" ) '(> org-string> org-time>))
10199 ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
10200 ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
10201 ((member op '("=" "==")) '(= string= org-time=))
10202 ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
10203 (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
10205 (defun org<> (a b) (not (= a b)))
10206 (defun org-string<= (a b) (or (string= a b) (string< a b)))
10207 (defun org-string>= (a b) (not (string< a b)))
10208 (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
10209 (defun org-string<> (a b) (not (string= a b)))
10210 (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
10211 (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
10212 (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
10213 (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
10214 (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
10215 (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
10217 "Convert S to a floating point time.
10218 If S is already a number, just return it. If it is a string, parse
10219 it as a time string and apply `float-time' to it. If S is nil, just return 0."
10223 (condition-case nil
10224 (float-time (apply 'encode-time (org-parse-time-string s)))
10228 (defun org-time-today ()
10229 "Time in seconds today at 0:00.
10230 Returns the float number of seconds since the beginning of the
10231 epoch to the beginning of today (00:00)."
10232 (float-time (apply 'encode-time
10233 (append '(0 0 0) (nthcdr 3 (decode-time))))))
10235 (defun org-matcher-time (s)
10236 "Interpret a time comparison value."
10239 ((string= s "<now>") (float-time))
10240 ((string= s "<today>") (org-time-today))
10241 ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
10242 ((string= s "<yesterday>") (- (org-time-today) 86400.0))
10243 ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
10244 (+ (org-time-today)
10245 (* (string-to-number (match-string 1 s))
10246 (cdr (assoc (match-string 2 s)
10247 '(("d" . 86400.0) ("w" . 604800.0)
10248 ("m" . 2678400.0) ("y" . 31557600.0)))))))
10251 (defun org-match-any-p (re list)
10252 "Does re match any element of list?"
10253 (setq list (mapcar (lambda (x) (string-match re x)) list))
10256 (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
10257 (defvar org-tags-overlay (org-make-overlay 1 1))
10258 (org-detach-overlay org-tags-overlay)
10260 (defun org-get-local-tags-at (&optional pos)
10261 "Get a list of tags defined in the current headline."
10262 (org-get-tags-at pos 'local))
10264 (defun org-get-local-tags ()
10265 "Get a list of tags defined in the current headline."
10266 (org-get-tags-at nil 'local))
10268 (defun org-get-tags-at (&optional pos local)
10269 "Get a list of all headline tags applicable at POS.
10270 POS defaults to point. If tags are inherited, the list contains
10271 the targets in the same sequence as the headlines appear, i.e.
10272 the tags of the current headline come last.
10273 When LOCAL is non-nil, only return tags from the current headline,
10274 ignore inherited ones."
10276 (if (and org-trust-scanner-tags
10277 (or (not pos) (equal pos (point)))
10280 (let (tags ltags lastpos parent)
10284 (goto-char (or pos (point)))
10287 (condition-case nil
10289 (org-back-to-heading t)
10290 (while (not (equal lastpos (point)))
10291 (setq lastpos (point))
10293 (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
10294 (setq ltags (org-split-string
10295 (org-match-string-no-properties 1) ":"))
10297 (setq ltags (mapcar 'org-add-prop-inherited ltags)))
10300 (org-remove-uniherited-tags ltags)
10303 (or org-use-tag-inheritance (throw 'done t))
10304 (if local (throw 'done t))
10305 (org-up-heading-all 1)
10308 (append (org-remove-uniherited-tags org-file-tags) tags)))))
10310 (defun org-add-prop-inherited (s)
10311 (add-text-properties 0 (length s) '(inherited t) s)
10314 (defun org-toggle-tag (tag &optional onoff)
10315 "Toggle the tag TAG for the current line.
10316 If ONOFF is `on' or `off', don't toggle but set to this state."
10319 (org-back-to-heading t)
10320 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
10323 (setq current (match-string 1))
10324 (replace-match ""))
10326 (setq current (nreverse (org-split-string current ":")))
10330 (or (member tag current) (push tag current)))
10332 (or (not (member tag current)) (setq current (delete tag current))))
10333 (t (if (member tag current)
10334 (setq current (delete tag current))
10336 (push tag current))))
10340 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
10341 (org-set-tags nil t))
10342 (delete-horizontal-space))
10343 (run-hooks 'org-after-tags-change-hook))
10346 (defun org-align-tags-here (to-col)
10347 ;; Assumes that this is a headline
10348 (let ((pos (point)) (col (current-column)) ncol tags-l p)
10349 (beginning-of-line 1)
10350 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
10351 (< pos (match-beginning 2)))
10353 (setq tags-l (- (match-end 2) (match-beginning 2)))
10354 (goto-char (match-beginning 1))
10356 (delete-region (point) (1+ (match-beginning 2)))
10357 (setq ncol (max (1+ (current-column))
10361 (- (abs to-col) tags-l))))
10363 (insert (make-string (- ncol (current-column)) ?\ ))
10364 (setq ncol (current-column))
10365 (when indent-tabs-mode (tabify p (point-at-eol)))
10366 (org-move-to-column (min ncol col) t))
10369 (defun org-set-tags-command (&optional arg just-align)
10370 "Call the set-tags command for the current entry."
10372 (if (org-on-heading-p)
10373 (org-set-tags arg just-align)
10375 (org-back-to-heading t)
10376 (org-set-tags arg just-align))))
10378 (defun org-set-tags (&optional arg just-align)
10379 "Set the tags for the current headline.
10380 With prefix ARG, realign all tags in headings in the current buffer."
10382 (let* ((re (concat "^" outline-regexp))
10383 (current (org-get-tags-string))
10384 (col (current-column))
10385 (org-setting-tags t)
10386 table current-tags inherited-tags ; computed below when needed
10390 (goto-char (point-min))
10391 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
10392 (while (re-search-forward re nil t)
10393 (org-set-tags nil t)
10395 (message "All tags realigned to column %d" org-tags-column))
10397 (setq tags current)
10398 ;; Get a new set of tags from the user
10400 (setq table (append org-tag-persistent-alist
10401 (or org-tag-alist (org-get-buffer-tags)))
10402 org-last-tags-completion-table table
10403 current-tags (org-split-string current ":")
10404 inherited-tags (nreverse
10405 (nthcdr (length current-tags)
10406 (nreverse (org-get-tags-at))))
10408 (if (or (eq t org-use-fast-tag-selection)
10409 (and org-use-fast-tag-selection
10410 (delq nil (mapcar 'cdr table))))
10411 (org-fast-tag-selection
10412 current-tags inherited-tags table
10413 (if org-fast-tag-selection-include-todo org-todo-key-alist))
10414 (let ((org-add-colon-after-tag-completion t))
10416 (org-without-partial-completion
10417 (org-ido-completing-read "Tags: " 'org-tags-completion-function
10418 nil nil current 'org-tags-history)))))))
10419 (while (string-match "[-+&]+" tags)
10420 ;; No boolean logic, just a list
10421 (setq tags (replace-match ":" t t tags))))
10423 (if (string-match "\\`[\t ]*\\'" tags)
10425 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
10426 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
10428 ;; Insert new tags at the correct column
10429 (beginning-of-line 1)
10431 ((and (equal current "") (equal tags "")))
10432 ((re-search-forward
10433 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
10435 (if (equal tags "")
10437 (goto-char (match-beginning 0))
10438 (setq c0 (current-column) p0 (point)
10439 c1 (max (1+ c0) (if (> org-tags-column 0)
10441 (- (- org-tags-column) (length tags))))
10442 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
10443 (replace-match rpl t t)
10444 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
10446 (t (error "Tags alignment failed")))
10447 (org-move-to-column col)
10449 (run-hooks 'org-after-tags-change-hook)))))
10451 (defun org-change-tag-in-region (beg end tag off)
10452 "Add or remove TAG for each entry in the region.
10453 This works in the agenda, and also in an org-mode buffer."
10455 (list (region-beginning) (region-end)
10456 (let ((org-last-tags-completion-table
10458 (org-get-buffer-tags)
10459 (org-global-tags-completion-table))))
10460 (org-ido-completing-read
10461 "Tag: " 'org-tags-completion-function nil nil nil
10462 'org-tags-history))
10464 (message "[s]et or [r]emove? ")
10465 (equal (read-char-exclusive) ?r))))
10466 (if (fboundp 'deactivate-mark) (deactivate-mark))
10467 (let ((agendap (equal major-mode 'org-agenda-mode))
10468 l1 l2 m buf pos newhead (cnt 0))
10470 (setq l2 (1- (org-current-line)))
10472 (setq l1 (org-current-line))
10473 (loop for l from l1 to l2 do
10475 (setq m (get-text-property (point) 'org-hd-marker))
10476 (when (or (and (org-mode-p) (org-on-heading-p))
10478 (setq buf (if agendap (marker-buffer m) (current-buffer))
10479 pos (if agendap m (point)))
10480 (with-current-buffer buf
10484 (setq cnt (1+ cnt))
10485 (org-toggle-tag tag (if off 'off 'on))
10486 (setq newhead (org-get-heading)))))
10487 (and agendap (org-agenda-change-all-lines newhead m))))
10488 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
10490 (defun org-tags-completion-function (string predicate &optional flag)
10491 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
10492 (confirm (lambda (x) (stringp (car x)))))
10493 (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
10494 (setq s1 (match-string 1 string)
10495 s2 (match-string 2 string))
10496 (setq s1 "" s2 string))
10500 (setq rtn (try-completion s2 ctable confirm))
10503 (concat s1 s2 (substring rtn (length s2))
10504 (if (and org-add-colon-after-tag-completion
10505 (assoc rtn ctable))
10510 (all-completions s2 ctable confirm)
10514 (assoc s2 ctable)))
10517 (defun org-fast-tag-insert (kwd tags face &optional end)
10518 "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
10519 (insert (format "%-12s" (concat kwd ":"))
10520 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
10523 (defun org-fast-tag-show-exit (flag)
10526 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
10527 (replace-match ""))
10530 (org-move-to-column (- (window-width) 19) t)
10531 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
10533 (defun org-set-current-tags-overlay (current prefix)
10534 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
10535 (if (featurep 'xemacs)
10536 (org-overlay-display org-tags-overlay (concat prefix s)
10537 'secondary-selection)
10538 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
10539 (org-overlay-display org-tags-overlay (concat prefix s)))))
10541 (defun org-fast-tag-selection (current inherited table &optional todo-table)
10542 "Fast tag selection with single keys.
10543 CURRENT is the current list of tags in the headline, INHERITED is the
10544 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
10545 possibly with grouping information. TODO-TABLE is a similar table with
10546 TODO keywords, should these have keys assigned to them.
10547 If the keys are nil, a-z are automatically assigned.
10548 Returns the new tags string, or nil to not change the current settings."
10549 (let* ((fulltable (append table todo-table))
10550 (maxlen (apply 'max (mapcar
10552 (if (stringp (car x)) (string-width (car x)) 0))
10554 (buf (current-buffer))
10555 (expert (eq org-fast-tag-selection-single-key 'expert))
10557 (fwidth (+ maxlen 3 1 3))
10558 (ncol (/ (- (window-width) 4) fwidth))
10561 tg cnt e c char c1 c2 ntable tbl rtn
10562 ov-start ov-end ov-prefix
10563 (exit-after-next org-fast-tag-selection-single-key)
10564 (done-keywords org-done-keywords)
10567 (beginning-of-line 1)
10569 (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
10570 (setq ov-start (match-beginning 1)
10571 ov-end (match-end 1)
10573 (setq ov-start (1- (point-at-eol))
10574 ov-end (1+ ov-start))
10575 (skip-chars-forward "^\n\r")
10578 (buffer-substring (1- (point)) (point))
10579 (if (> (current-column) org-tags-column)
10581 (make-string (- org-tags-column (current-column)) ?\ ))))))
10582 (org-move-overlay org-tags-overlay ov-start ov-end)
10583 (save-window-excursion
10585 (set-buffer (get-buffer-create " *Org tags*"))
10586 (delete-other-windows)
10587 (split-window-vertically)
10588 (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
10590 (org-set-local 'org-done-keywords done-keywords)
10591 (org-fast-tag-insert "Inherited" inherited i-face "\n")
10592 (org-fast-tag-insert "Current" current c-face "\n\n")
10593 (org-fast-tag-show-exit exit-after-next)
10594 (org-set-current-tags-overlay current ov-prefix)
10595 (setq tbl fulltable char ?a cnt 0)
10596 (while (setq e (pop tbl))
10598 ((equal e '(:startgroup))
10599 (push '() groups) (setq ingroup t)
10600 (when (not (= cnt 0))
10604 ((equal e '(:endgroup))
10605 (setq ingroup nil cnt 0)
10607 ((equal e '(:newline))
10608 (when (not (= cnt 0))
10612 (while (equal (car tbl) '(:newline))
10614 (setq tbl (cdr tbl)))))
10616 (setq tg (car e) c2 nil)
10619 ;; automatically assign a character.
10620 (setq c1 (string-to-char
10621 (downcase (substring
10622 tg (if (= (string-to-char tg) ?@) 1 0)))))
10623 (if (or (rassoc c1 ntable) (rassoc c1 table))
10624 (while (or (rassoc char ntable) (rassoc char table))
10625 (setq char (1+ char)))
10627 (setq c (or c2 char)))
10628 (if ingroup (push tg (car groups)))
10629 (setq tg (org-add-props tg nil 'face
10631 ((not (assoc tg table))
10632 (org-get-todo-face tg))
10633 ((member tg current) c-face)
10634 ((member tg inherited) i-face)
10636 (if (and (= cnt 0) (not ingroup)) (insert " "))
10637 (insert "[" c "] " tg (make-string
10638 (- fwidth 4 (length tg)) ?\ ))
10639 (push (cons tg c) ntable)
10640 (when (= (setq cnt (1+ cnt)) ncol)
10642 (if ingroup (insert " "))
10644 (setq ntable (nreverse ntable))
10646 (goto-char (point-min))
10647 (if (not expert) (org-fit-window-to-buffer))
10651 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
10652 (if groups " [!] no groups" " [!]groups")
10653 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
10654 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
10656 ((= c ?\r) (throw 'exit t))
10658 (setq groups (not groups))
10659 (goto-char (point-min))
10660 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
10663 (org-fast-tag-show-exit
10664 (setq exit-after-next (not exit-after-next)))
10666 (delete-other-windows)
10667 (split-window-vertically)
10668 (org-switch-to-buffer-other-window " *Org tags*")
10669 (org-fit-window-to-buffer)))
10671 (and (= c ?q) (not (rassoc c ntable))))
10672 (org-detach-overlay org-tags-overlay)
10673 (setq quit-flag t))
10676 (if exit-after-next (setq exit-after-next 'now)))
10678 (condition-case nil
10679 (setq tg (org-ido-completing-read
10682 (with-current-buffer buf
10683 (org-get-buffer-tags)))))
10684 (quit (setq tg "")))
10685 (when (string-match "\\S-" tg)
10686 (add-to-list 'buffer-tags (list tg))
10687 (if (member tg current)
10688 (setq current (delete tg current))
10689 (push tg current)))
10690 (if exit-after-next (setq exit-after-next 'now)))
10691 ((setq e (rassoc c todo-table) tg (car e))
10692 (with-current-buffer buf
10693 (save-excursion (org-todo tg)))
10694 (if exit-after-next (setq exit-after-next 'now)))
10695 ((setq e (rassoc c ntable) tg (car e))
10696 (if (member tg current)
10697 (setq current (delete tg current))
10698 (loop for g in groups do
10701 (setq current (delete x current)))
10704 (if exit-after-next (setq exit-after-next 'now))))
10706 ;; Create a sorted list
10710 (assoc b (cdr (memq (assoc a ntable) ntable))))))
10711 (if (eq exit-after-next 'now) (throw 'exit t))
10712 (goto-char (point-min))
10713 (beginning-of-line 2)
10714 (delete-region (point) (point-at-eol))
10715 (org-fast-tag-insert "Current" current c-face)
10716 (org-set-current-tags-overlay current ov-prefix)
10717 (while (re-search-forward
10718 (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
10719 (setq tg (match-string 1))
10720 (add-text-properties
10721 (match-beginning 1) (match-end 1)
10724 ((member tg current) c-face)
10725 ((member tg inherited) i-face)
10726 (t (get-text-property (match-beginning 1) 'face))))))
10727 (goto-char (point-min)))))
10728 (org-detach-overlay org-tags-overlay)
10730 (mapconcat 'identity current ":")
10733 (defun org-get-tags-string ()
10734 "Get the TAGS string in the current headline."
10735 (unless (org-on-heading-p t)
10736 (error "Not on a heading"))
10738 (beginning-of-line 1)
10739 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
10740 (org-match-string-no-properties 1)
10743 (defun org-get-tags ()
10744 "Get the list of tags specified in the current headline."
10745 (org-split-string (org-get-tags-string) ":"))
10747 (defun org-get-buffer-tags ()
10748 "Get a table of all tags used in the buffer, for completion."
10751 (goto-char (point-min))
10752 (while (re-search-forward
10753 (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
10754 (when (equal (char-after (point-at-bol 0)) ?*)
10755 (mapc (lambda (x) (add-to-list 'tags x))
10756 (org-split-string (org-match-string-no-properties 1) ":")))))
10757 (mapcar 'list tags)))
10759 ;;;; The mapping API
10762 (defun org-map-entries (func &optional match scope &rest skip)
10763 "Call FUNC at each headline selected by MATCH in SCOPE.
10765 FUNC is a function or a lisp form. The function will be called without
10766 arguments, with the cursor positioned at the beginning of the headline.
10767 The return values of all calls to the function will be collected and
10768 returned as a list.
10770 MATCH is a tags/property/todo match as it is used in the agenda tags view.
10771 Only headlines that are matched by this query will be considered during
10772 the iteration. When MATCH is nil or t, all headlines will be
10773 visited by the iteration.
10775 SCOPE determines the scope of this command. It can be any of:
10777 nil The current buffer, respecting the restriction if any
10778 tree The subtree started with the entry at point
10779 file The current buffer, without restriction
10781 The current buffer, and any archives associated with it
10782 agenda All agenda files
10783 agenda-with-archives
10784 All agenda files with any archive files associated with them
10786 If this is a list, all files in the list will be scanned
10788 The remaining args are treated as settings for the skipping facilities of
10789 the scanner. The following items can be given here:
10791 archive skip trees with the archive tag.
10792 comment skip trees with the COMMENT keyword
10793 function or Emacs Lisp form:
10794 will be used as value for `org-agenda-skip-function', so whenever
10795 the the function returns t, FUNC will not be called for that
10796 entry and search will continue from the point where the
10797 function leaves it.
10799 If your function needs to retrieve the tags including inherited tags
10800 at the *current* entry, you can use the value of the variable
10801 `org-scanner-tags' which will be much faster than getting the value
10802 with `org-get-tags-at'. If your function gets properties with
10803 `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
10804 to t around the call to `org-entry-properties' to get the same speedup.
10805 Note that if your function moves around to retrieve tags and properties at
10806 a *different* entry, you cannot use these techniques."
10807 (let* ((org-agenda-archives-mode nil) ; just to make sure
10808 (org-agenda-skip-archived-trees (memq 'archive skip))
10809 (org-agenda-skip-comment-trees (memq 'comment skip))
10810 (org-agenda-skip-function
10811 (car (org-delete-all '(comment archive) skip)))
10812 (org-tags-match-list-sublevels t)
10814 org-todo-keywords-for-agenda
10815 org-done-keywords-for-agenda
10816 org-todo-keyword-alist-for-agenda
10817 org-tag-alist-for-agenda)
10820 ((eq match t) (setq matcher t))
10821 ((eq match nil) (setq matcher t))
10822 (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
10826 (when (eq scope 'tree)
10827 (org-back-to-heading t)
10828 (org-narrow-to-subtree)
10833 (org-prepare-agenda-buffers
10834 (list (buffer-file-name (current-buffer))))
10835 (setq res (org-scan-tags func matcher)))
10836 ;; Get the right scope
10838 ((and scope (listp scope) (symbolp (car scope)))
10839 (setq scope (eval scope)))
10840 ((eq scope 'agenda)
10841 (setq scope (org-agenda-files t)))
10842 ((eq scope 'agenda-with-archives)
10843 (setq scope (org-agenda-files t))
10844 (setq scope (org-add-archive-files scope)))
10846 (setq scope (list (buffer-file-name))))
10847 ((eq scope 'file-with-archives)
10848 (setq scope (org-add-archive-files (list (buffer-file-name))))))
10849 (org-prepare-agenda-buffers scope)
10850 (while (setq file (pop scope))
10851 (with-current-buffer (org-find-base-buffer-visiting file)
10855 (goto-char (point-min))
10856 (setq res (append res (org-scan-tags func matcher))))))))))
10861 ;;; Setting and retrieving properties
10863 (defconst org-special-properties
10864 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
10865 "TIMESTAMP" "TIMESTAMP_IA")
10866 "The special properties valid in Org-mode.
10868 These are properties that are not defined in the property drawer,
10869 but in some other way.")
10871 (defconst org-default-properties
10872 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
10873 "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
10874 "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
10875 "EXPORT_FILE_NAME" "EXPORT_TITLE" "ORDERED")
10876 "Some properties that are used by Org-mode for various purposes.
10877 Being in this list makes sure that they are offered for completion.")
10879 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
10880 "Regular expression matching the first line of a property drawer.")
10882 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
10883 "Regular expression matching the first line of a property drawer.")
10885 (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
10886 "Regular expression matching the first line of a property drawer.")
10888 (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
10889 "Regular expression matching the first line of a property drawer.")
10891 (defconst org-property-drawer-re
10892 (concat "\\(" org-property-start-re "\\)[^\000]*\\("
10893 org-property-end-re "\\)\n?")
10894 "Matches an entire property drawer.")
10896 (defconst org-clock-drawer-re
10897 (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
10898 org-property-end-re "\\)\n?")
10899 "Matches an entire clock drawer.")
10901 (defun org-property-action ()
10902 "Do an action on properties."
10905 (org-at-property-p)
10906 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
10907 (setq c (read-char-exclusive))
10910 (call-interactively 'org-set-property))
10912 (call-interactively 'org-delete-property))
10914 (call-interactively 'org-delete-property-globally))
10916 (call-interactively 'org-compute-property-at-point))
10917 (t (error "No such property action %c" c)))))
10919 (defun org-at-property-p ()
10920 "Is the cursor in a property line?"
10921 ;; FIXME: Does not check if we are actually in the drawer.
10922 ;; FIXME: also returns true on any drawers.....
10923 ;; This is used by C-c C-c for property action.
10925 (beginning-of-line 1)
10926 (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
10928 (defun org-get-property-block (&optional beg end force)
10929 "Return the (beg . end) range of the body of the property drawer.
10930 BEG and END can be beginning and end of subtree, if not given
10931 they will be found.
10932 If the drawer does not exist and FORCE is non-nil, create the drawer."
10935 (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
10936 (end (or end (progn (outline-next-heading) (point)))))
10938 (if (re-search-forward org-property-start-re end t)
10939 (setq beg (1+ (match-end 0)))
10942 (org-insert-property-drawer)
10943 (setq end (progn (outline-next-heading) (point))))
10946 (if (re-search-forward org-property-start-re end t)
10947 (setq beg (1+ (match-end 0)))))
10948 (if (re-search-forward org-property-end-re end t)
10949 (setq end (match-beginning 0))
10950 (or force (throw 'exit nil))
10953 (org-indent-line-function)
10954 (insert ":END:\n"))
10957 (defun org-entry-properties (&optional pom which)
10958 "Get all properties of the entry at point-or-marker POM.
10959 This includes the TODO keyword, the tags, time strings for deadline,
10960 scheduled, and clocking, and any additional properties defined in the
10961 entry. The return value is an alist, keys may occur multiple times
10962 if the property key was used several times.
10963 POM may also be nil, in which case the current entry is used.
10964 If WHICH is nil or `all', get all properties. If WHICH is
10965 `special' or `standard', only get that subclass."
10966 (setq which (or which 'all))
10967 (org-with-point-at pom
10968 (let ((clockstr (substring org-clock-string 0 -1))
10969 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
10970 beg end range props sum-props key value string clocksum)
10972 (when (condition-case nil
10973 (and (org-mode-p) (org-back-to-heading t))
10976 (setq sum-props (get-text-property (point) 'org-summaries))
10977 (setq clocksum (get-text-property (point) :org-clock-minutes))
10978 (outline-next-heading)
10980 (when (memq which '(all special))
10981 ;; Get the special properties, like TODO and tags
10983 (when (and (looking-at org-todo-line-regexp) (match-end 2))
10984 (push (cons "TODO" (org-match-string-no-properties 2)) props))
10985 (when (looking-at org-priority-regexp)
10986 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
10987 (when (and (setq value (org-get-tags-string))
10988 (string-match "\\S-" value))
10989 (push (cons "TAGS" value) props))
10990 (when (setq value (org-get-tags-at))
10991 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
10993 (while (re-search-forward org-maybe-keyword-time-regexp end t)
10994 (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
10995 string (if (equal key clockstr)
10999 (match-beginning 3) (goto-char (point-at-eol)))))
11000 (substring (org-match-string-no-properties 3) 1 -1)))
11002 (if (= (char-after (match-beginning 3)) ?\[)
11003 (setq key "TIMESTAMP_IA")
11004 (setq key "TIMESTAMP")))
11005 (when (or (equal key clockstr) (not (assoc key props)))
11006 (push (cons key string) props)))
11010 (when (memq which '(all standard))
11011 ;; Get the standard properties, like :PROP: ...
11012 (setq range (org-get-property-block beg end))
11014 (goto-char (car range))
11015 (while (re-search-forward
11016 (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
11018 (setq key (org-match-string-no-properties 1)
11019 value (org-trim (or (org-match-string-no-properties 2) "")))
11020 (unless (member key excluded)
11021 (push (cons key (or value "")) props)))))
11023 (push (cons "CLOCKSUM"
11024 (org-columns-number-to-string (/ (float clocksum) 60.)
11027 (unless (assoc "CATEGORY" props)
11028 (setq value (or (org-get-category)
11029 (progn (org-refresh-category-properties)
11030 (org-get-category))))
11031 (push (cons "CATEGORY" value) props))
11032 (append sum-props (nreverse props)))))))
11034 (defun org-entry-get (pom property &optional inherit)
11035 "Get value of PROPERTY for entry at point-or-marker POM.
11036 If INHERIT is non-nil and the entry does not have the property,
11037 then also check higher levels of the hierarchy.
11038 If INHERIT is the symbol `selective', use inheritance only if the setting
11039 in `org-use-property-inheritance' selects PROPERTY for inheritance.
11040 If the property is present but empty, the return value is the empty string.
11041 If the property is not present at all, nil is returned."
11042 (org-with-point-at pom
11043 (if (and inherit (if (eq inherit 'selective)
11044 (org-property-inherit-p property)
11046 (org-entry-get-with-inheritance property)
11047 (if (member property org-special-properties)
11048 ;; We need a special property. Use brute force, get all properties.
11049 (cdr (assoc property (org-entry-properties nil 'special)))
11050 (let ((range (org-get-property-block)))
11052 (goto-char (car range))
11054 (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
11056 ;; Found the property, return it.
11058 (org-match-string-no-properties 1)
11061 (defun org-property-or-variable-value (var &optional inherit)
11062 "Check if there is a property fixing the value of VAR.
11063 If yes, return this value. If not, return the current value of the variable."
11064 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
11065 (if (and prop (stringp prop) (string-match "\\S-" prop))
11067 (symbol-value var))))
11069 (defun org-entry-delete (pom property)
11070 "Delete the property PROPERTY from entry at point-or-marker POM."
11071 (org-with-point-at pom
11072 (if (member property org-special-properties)
11073 nil ; cannot delete these properties.
11074 (let ((range (org-get-property-block)))
11076 (goto-char (car range))
11078 (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
11081 (delete-region (match-beginning 0) (1+ (point-at-eol)))
11085 ;; Multi-values properties are properties that contain multiple values
11086 ;; These values are assumed to be single words, separated by whitespace.
11087 (defun org-entry-add-to-multivalued-property (pom property value)
11088 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
11089 (let* ((old (org-entry-get pom property))
11090 (values (and old (org-split-string old "[ \t]"))))
11091 (setq value (org-entry-protect-space value))
11092 (unless (member value values)
11093 (setq values (cons value values))
11094 (org-entry-put pom property
11095 (mapconcat 'identity values " ")))))
11097 (defun org-entry-remove-from-multivalued-property (pom property value)
11098 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
11099 (let* ((old (org-entry-get pom property))
11100 (values (and old (org-split-string old "[ \t]"))))
11101 (setq value (org-entry-protect-space value))
11102 (when (member value values)
11103 (setq values (delete value values))
11104 (org-entry-put pom property
11105 (mapconcat 'identity values " ")))))
11107 (defun org-entry-member-in-multivalued-property (pom property value)
11108 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
11109 (let* ((old (org-entry-get pom property))
11110 (values (and old (org-split-string old "[ \t]"))))
11111 (setq value (org-entry-protect-space value))
11112 (member value values)))
11114 (defun org-entry-get-multivalued-property (pom property)
11115 "Return a list of values in a multivalued property."
11116 (let* ((value (org-entry-get pom property))
11117 (values (and value (org-split-string value "[ \t]"))))
11118 (mapcar 'org-entry-restore-space values)))
11120 (defun org-entry-put-multivalued-property (pom property &rest values)
11121 "Set multivalued PROPERTY at point-or-marker POM to VALUES.
11122 VALUES should be a list of strings. Spaces will be protected."
11123 (org-entry-put pom property
11124 (mapconcat 'org-entry-protect-space values " "))
11125 (let* ((value (org-entry-get pom property))
11126 (values (and value (org-split-string value "[ \t]"))))
11127 (mapcar 'org-entry-restore-space values)))
11129 (defun org-entry-protect-space (s)
11130 "Protect spaces and newline in string S."
11131 (while (string-match " " s)
11132 (setq s (replace-match "%20" t t s)))
11133 (while (string-match "\n" s)
11134 (setq s (replace-match "%0A" t t s)))
11137 (defun org-entry-restore-space (s)
11138 "Restore spaces and newline in string S."
11139 (while (string-match "%20" s)
11140 (setq s (replace-match " " t t s)))
11141 (while (string-match "%0A" s)
11142 (setq s (replace-match "\n" t t s)))
11145 (defvar org-entry-property-inherited-from (make-marker)
11146 "Marker pointing to the entry from where a property was inherited.
11147 Each call to `org-entry-get-with-inheritance' will set this marker to the
11148 location of the entry where the inheritance search matched. If there was
11149 no match, the marker will point nowhere.
11150 Note that also `org-entry-get' calls this function, if the INHERIT flag
11153 (defun org-entry-get-with-inheritance (property)
11154 "Get entry property, and search higher levels if not present."
11155 (move-marker org-entry-property-inherited-from nil)
11162 (when (setq tmp (org-entry-get nil property))
11163 (org-back-to-heading t)
11164 (move-marker org-entry-property-inherited-from (point))
11166 (or (org-up-heading-safe) (throw 'ex nil)))))
11168 (cdr (assoc property org-file-properties))
11169 (cdr (assoc property org-global-properties))
11170 (cdr (assoc property org-global-properties-fixed))))))
11172 (defun org-entry-put (pom property value)
11173 "Set PROPERTY to VALUE for entry at point-or-marker POM."
11174 (org-with-point-at pom
11175 (org-back-to-heading t)
11176 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
11179 ((equal property "TODO")
11180 (when (and (stringp value) (string-match "\\S-" value)
11181 (not (member value org-todo-keywords-1)))
11182 (error "\"%s\" is not a valid TODO state" value))
11183 (if (or (not value)
11184 (not (string-match "\\S-" value)))
11185 (setq value 'none))
11187 (org-set-tags nil 'align))
11188 ((equal property "PRIORITY")
11189 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
11190 (string-to-char value) ?\ ))
11191 (org-set-tags nil 'align))
11192 ((equal property "SCHEDULED")
11193 (if (re-search-forward org-scheduled-time-regexp end t)
11195 ((eq value 'earlier) (org-timestamp-change -1 'day))
11196 ((eq value 'later) (org-timestamp-change 1 'day))
11197 (t (call-interactively 'org-schedule)))
11198 (call-interactively 'org-schedule)))
11199 ((equal property "DEADLINE")
11200 (if (re-search-forward org-deadline-time-regexp end t)
11202 ((eq value 'earlier) (org-timestamp-change -1 'day))
11203 ((eq value 'later) (org-timestamp-change 1 'day))
11204 (t (call-interactively 'org-deadline)))
11205 (call-interactively 'org-deadline)))
11206 ((member property org-special-properties)
11207 (error "The %s property can not yet be set with `org-entry-put'"
11209 (t ; a non-special property
11210 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
11211 (setq range (org-get-property-block beg end 'force))
11212 (goto-char (car range))
11213 (if (re-search-forward
11214 (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
11216 (delete-region (match-beginning 1) (match-end 1))
11217 (goto-char (match-beginning 1)))
11218 (goto-char (cdr range))
11221 (org-indent-line-function)
11222 (insert ":" property ":"))
11223 (and value (insert " " value))
11224 (org-indent-line-function)))))))
11226 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
11227 "Get all property keys in the current buffer.
11228 With INCLUDE-SPECIALS, also list the special properties that reflect things
11229 like tags and TODO state.
11230 With INCLUDE-DEFAULTS, also include properties that has special meaning
11231 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
11232 With INCLUDE-COLUMNS, also include property names given in COLUMN
11233 formats in the current buffer."
11234 (let (rtn range cfmt s p)
11238 (goto-char (point-min))
11239 (while (re-search-forward org-property-start-re nil t)
11240 (setq range (org-get-property-block))
11241 (goto-char (car range))
11242 (while (re-search-forward
11243 (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
11245 (add-to-list 'rtn (org-match-string-no-properties 1)))
11246 (outline-next-heading))))
11248 (when include-specials
11249 (setq rtn (append org-special-properties rtn)))
11251 (when include-defaults
11252 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
11254 (when include-columns
11258 (goto-char (point-min))
11259 (while (re-search-forward
11260 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
11262 (setq cfmt (match-string 2) s 0)
11263 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
11265 (setq s (match-end 0)
11266 p (match-string 1 cfmt))
11267 (unless (or (equal p "ITEM")
11268 (member p org-special-properties))
11269 (add-to-list 'rtn (match-string 1 cfmt))))))))
11271 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
11273 (defun org-property-values (key)
11274 "Return a list of all values of property KEY."
11278 (goto-char (point-min))
11279 (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
11281 (while (re-search-forward re nil t)
11282 (add-to-list 'values (org-trim (match-string 1))))
11283 (delete "" values)))))
11285 (defun org-insert-property-drawer ()
11286 "Insert a property drawer into the current entry."
11288 (org-back-to-heading t)
11289 (looking-at outline-regexp)
11290 (let ((indent (- (match-end 0)(match-beginning 0)))
11292 (re (concat "^[ \t]*" org-keyword-time-regexp))
11294 (outline-next-heading)
11297 (while (re-search-forward re end t))
11298 (setq hiddenp (org-invisible-p))
11300 (and (equal (char-after) ?\n) (forward-char 1))
11301 (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
11302 (if (member (match-string 1) '("CLOCK:" ":END:"))
11303 ;; just skip this line
11304 (beginning-of-line 2)
11305 ;; Drawer start, find the end
11306 (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
11307 (beginning-of-line 1)))
11308 (org-skip-over-state-notes)
11309 (skip-chars-backward " \t\n\r")
11310 (if (eq (char-before) ?*) (forward-char 1))
11311 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
11312 (beginning-of-line 0)
11313 (org-indent-to-column indent)
11314 (beginning-of-line 2)
11315 (org-indent-to-column indent)
11316 (beginning-of-line 0)
11319 (org-back-to-heading t)
11321 (org-flag-drawer t))))
11323 (defun org-set-property (property value)
11324 "In the current entry, set PROPERTY to VALUE.
11325 When called interactively, this will prompt for a property name, offering
11326 completion on existing and default properties. And then it will prompt
11327 for a value, offering completion either on allowed values (via an inherited
11328 xxx_ALL property) or on existing values in other instances of this property
11329 in the current file."
11331 (let* ((completion-ignore-case t)
11332 (keys (org-buffer-property-keys nil t t))
11333 (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
11334 (prop (if (member prop0 keys)
11336 (or (cdr (assoc (downcase prop0)
11337 (mapcar (lambda (x) (cons (downcase x) x))
11340 (cur (org-entry-get nil prop))
11341 (allowed (org-property-get-allowed-values nil prop 'table))
11342 (existing (mapcar 'list (org-property-values prop)))
11344 (org-completing-read "Value: " allowed nil 'req-match)
11345 (let (org-completion-use-ido)
11346 (org-completing-read
11347 (concat "Value" (if (and cur (string-match "\\S-" cur))
11348 (concat "[" cur "]") "")
11350 existing nil nil "" nil cur)))))
11351 (list prop (if (equal val "") cur val))))
11352 (unless (equal (org-entry-get nil property) value)
11353 (org-entry-put nil property value)))
11355 (defun org-delete-property (property)
11356 "In the current entry, delete PROPERTY."
11358 (let* ((completion-ignore-case t)
11359 (prop (org-ido-completing-read
11360 "Property: " (org-entry-properties nil 'standard))))
11362 (message "Property %s %s" property
11363 (if (org-entry-delete nil property)
11365 "was not present in the entry")))
11367 (defun org-delete-property-globally (property)
11368 "Remove PROPERTY globally, from all entries."
11370 (let* ((completion-ignore-case t)
11371 (prop (org-ido-completing-read
11372 "Globally remove property: "
11373 (mapcar 'list (org-buffer-property-keys)))))
11378 (goto-char (point-min))
11380 (while (re-search-forward
11381 (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
11383 (setq cnt (1+ cnt))
11384 (replace-match ""))
11385 (message "Property \"%s\" removed from %d entries" property cnt)))))
11387 (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
11389 (defun org-compute-property-at-point ()
11390 "Compute the property at point.
11391 This looks for an enclosing column format, extracts the operator and
11392 then applies it to the property in the column format's scope."
11394 (unless (org-at-property-p)
11395 (error "Not at a property"))
11396 (let ((prop (org-match-string-no-properties 2)))
11397 (org-columns-get-format-and-top-level)
11398 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
11399 (error "No operator defined for property %s" prop))
11400 (org-columns-compute prop)))
11402 (defun org-property-get-allowed-values (pom property &optional table)
11403 "Get allowed values for the property PROPERTY.
11404 When TABLE is non-nil, return an alist that can directly be used for
11408 ((equal property "TODO")
11409 (setq vals (org-with-point-at pom
11410 (append org-todo-keywords-1 '("")))))
11411 ((equal property "PRIORITY")
11412 (let ((n org-lowest-priority))
11413 (while (>= n org-highest-priority)
11414 (push (char-to-string n) vals)
11416 ((member property org-special-properties))
11418 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
11420 (when (and vals (string-match "\\S-" vals))
11421 (setq vals (car (read-from-string (concat "(" vals ")"))))
11422 (setq vals (mapcar (lambda (x)
11423 (cond ((stringp x) x)
11424 ((numberp x) (number-to-string x))
11425 ((symbolp x) (symbol-name x))
11428 (if table (mapcar 'list vals) vals)))
11430 (defun org-property-previous-allowed-value (&optional previous)
11431 "Switch to the next allowed value for this property."
11433 (org-property-next-allowed-value t))
11435 (defun org-property-next-allowed-value (&optional previous)
11436 "Switch to the next allowed value for this property."
11438 (unless (org-at-property-p)
11439 (error "Not at a property"))
11440 (let* ((key (match-string 2))
11441 (value (match-string 3))
11442 (allowed (or (org-property-get-allowed-values (point) key)
11443 (and (member value '("[ ]" "[-]" "[X]"))
11447 (error "Allowed values for this property have not been defined"))
11448 (if previous (setq allowed (reverse allowed)))
11449 (if (member value allowed)
11450 (setq nval (car (cdr (member value allowed)))))
11451 (setq nval (or nval (car allowed)))
11452 (if (equal nval value)
11453 (error "Only one allowed value for this property"))
11454 (org-at-property-p)
11455 (replace-match (concat " :" key ": " nval) t t)
11456 (org-indent-line-function)
11457 (beginning-of-line 1)
11458 (skip-chars-forward " \t")))
11460 (defun org-find-entry-with-id (ident)
11461 "Locate the entry that contains the ID property with exact value IDENT.
11462 IDENT can be a string, a symbol or a number, this function will search for
11463 the string representation of it.
11464 Return the position where this entry starts, or nil if there is no such entry."
11465 (interactive "sID: ")
11467 ((stringp ident) ident)
11468 ((symbol-name ident) (symbol-name ident))
11469 ((numberp ident) (number-to-string ident))
11470 (t (error "IDENT %s must be a string, symbol or number" ident))))
11471 (case-fold-search nil))
11475 (goto-char (point-min))
11476 (when (re-search-forward
11477 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
11479 (org-back-to-heading)
11484 (defvar org-last-changed-timestamp nil)
11485 (defvar org-last-inserted-timestamp nil
11486 "The last time stamp inserted with `org-insert-time-stamp'.")
11487 (defvar org-time-was-given) ; dynamically scoped parameter
11488 (defvar org-end-time-was-given) ; dynamically scoped parameter
11489 (defvar org-ts-what) ; dynamically scoped parameter
11491 (defun org-time-stamp (arg &optional inactive)
11492 "Prompt for a date/time and insert a time stamp.
11493 If the user specifies a time like HH:MM, or if this command is called
11494 with a prefix argument, the time stamp will contain date and time.
11495 Otherwise, only the date will be included. All parts of a date not
11496 specified by the user will be filled in from the current date/time.
11497 So if you press just return without typing anything, the time stamp
11498 will represent the current date/time. If there is already a timestamp
11499 at the cursor, it will be modified."
11503 ;; Default time is either today, or, when entering a range,
11504 ;; the range start.
11505 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
11507 (re-search-backward
11508 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
11509 (- (point) 20) t)))
11510 (apply 'encode-time (org-parse-time-string (match-string 1)))
11512 (default-input (and ts (org-get-compact-tod ts)))
11513 org-time-was-given org-end-time-was-given time)
11515 ((and (org-at-timestamp-p t)
11516 (memq last-command '(org-time-stamp org-time-stamp-inactive))
11517 (memq this-command '(org-time-stamp org-time-stamp-inactive)))
11519 (setq time (let ((this-command this-command))
11520 (org-read-date arg 'totime nil nil
11521 default-time default-input)))
11522 (org-insert-time-stamp time (or org-time-was-given arg) inactive))
11523 ((org-at-timestamp-p t)
11524 (setq time (let ((this-command this-command))
11525 (org-read-date arg 'totime nil nil default-time default-input)))
11526 (when (org-at-timestamp-p t) ; just to get the match data
11527 ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
11529 (setq org-last-changed-timestamp
11530 (org-insert-time-stamp
11531 time (or org-time-was-given arg)
11532 inactive nil nil (list org-end-time-was-given))))
11533 (message "Timestamp updated"))
11535 (setq time (let ((this-command this-command))
11536 (org-read-date arg 'totime nil nil default-time default-input)))
11537 (org-insert-time-stamp time (or org-time-was-given arg) inactive
11538 nil nil (list org-end-time-was-given))))))
11540 ;; FIXME: can we use this for something else, like computing time differences?
11541 (defun org-get-compact-tod (s)
11542 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
11543 (let* ((t1 (match-string 1 s))
11544 (h1 (string-to-number (match-string 2 s)))
11545 (m1 (string-to-number (match-string 3 s)))
11546 (t2 (and (match-end 4) (match-string 5 s)))
11547 (h2 (and t2 (string-to-number (match-string 6 s))))
11548 (m2 (and t2 (string-to-number (match-string 7 s))))
11552 (setq dh (- h2 h1) dm (- m2 m1))
11553 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
11554 (concat t1 "+" (number-to-string dh)
11555 (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
11557 (defun org-time-stamp-inactive (&optional arg)
11558 "Insert an inactive time stamp.
11559 An inactive time stamp is enclosed in square brackets instead of angle
11560 brackets. It is inactive in the sense that it does not trigger agenda entries,
11561 does not link to the calendar and cannot be changed with the S-cursor keys.
11562 So these are more for recording a certain time/date."
11564 (org-time-stamp arg 'inactive))
11566 (defvar org-date-ovl (org-make-overlay 1 1))
11567 (org-overlay-put org-date-ovl 'face 'org-warning)
11568 (org-detach-overlay org-date-ovl)
11570 (defvar org-ans1) ; dynamically scoped parameter
11571 (defvar org-ans2) ; dynamically scoped parameter
11573 (defvar org-plain-time-of-day-regexp) ; defined below
11575 (defvar org-overriding-default-time nil) ; dynamically scoped
11576 (defvar org-read-date-overlay nil)
11577 (defvar org-dcst nil) ; dynamically scoped
11578 (defvar org-read-date-history nil)
11580 (defun org-read-date (&optional with-time to-time from-string prompt
11581 default-time default-input)
11582 "Read a date, possibly a time, and make things smooth for the user.
11583 The prompt will suggest to enter an ISO date, but you can also enter anything
11584 which will at least partially be understood by `parse-time-string'.
11585 Unrecognized parts of the date will default to the current day, month, year,
11586 hour and minute. If this command is called to replace a timestamp at point,
11587 of to enter the second timestamp of a range, the default time is taken from the
11588 existing stamp. For example,
11589 3-2-5 --> 2003-02-05
11590 feb 15 --> currentyear-02-15
11591 sep 12 9 --> 2009-09-12
11592 12:45 --> today 12:45
11593 22 sept 0:34 --> currentyear-09-22 0:34
11594 12 --> currentyear-currentmonth-12
11595 Fri --> nearest Friday (today or later)
11598 Furthermore you can specify a relative date by giving, as the *first* thing
11599 in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
11600 change in days weeks, months, years.
11601 With a single plus or minus, the date is relative to today. With a double
11602 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
11603 +4d --> four days from today
11604 +4 --> same as above
11605 +2w --> two weeks from today
11606 ++5 --> five days from default date
11608 The function understands only English month and weekday abbreviations,
11609 but this can be configured with the variables `parse-time-months' and
11610 `parse-time-weekdays'.
11612 While prompting, a calendar is popped up - you can also select the
11613 date with the mouse (button 1). The calendar shows a period of three
11614 months. To scroll it to other months, use the keys `>' and `<'.
11615 If you don't like the calendar, turn it off with
11616 \(setq org-read-date-popup-calendar nil)
11618 With optional argument TO-TIME, the date will immediately be converted
11619 to an internal time.
11620 With an optional argument WITH-TIME, the prompt will suggest to also
11621 insert a time. Note that when WITH-TIME is not set, you can still
11622 enter a time, and this function will inform the calling routine about
11623 this change. The calling routine may then choose to change the format
11624 used to insert the time stamp into the buffer to include the time.
11625 With optional argument FROM-STRING, read from this string instead from
11626 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
11627 the time/date that is used for everything that is not specified by the
11629 (require 'parse-time)
11630 (let* ((org-time-stamp-rounding-minutes
11631 (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
11632 (org-dcst org-display-custom-times)
11633 (ct (org-current-time))
11634 (def (or org-overriding-default-time default-time ct))
11635 (defdecode (decode-time def))
11637 (when (< (nth 2 defdecode) org-extend-today-until)
11638 (setcar (nthcdr 2 defdecode) -1)
11639 (setcar (nthcdr 1 defdecode) 59)
11640 (setq def (apply 'encode-time defdecode)
11641 defdecode (decode-time def)))))
11642 (calendar-move-hook nil)
11643 (calendar-view-diary-initially-flag nil)
11644 (view-diary-entries-initially nil)
11645 (calendar-view-holidays-initially-flag nil)
11646 (view-calendar-holidays-initially nil)
11647 (timestr (format-time-string
11648 (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
11649 (prompt (concat (if prompt (concat prompt " ") "")
11650 (format "Date+time [%s]: " timestr)))
11651 ans (org-ans0 "") org-ans1 org-ans2 final)
11654 (from-string (setq ans from-string))
11655 (org-read-date-popup-calendar
11657 (save-window-excursion
11659 (calendar-forward-day (- (time-to-days def)
11660 (calendar-absolute-from-gregorian
11661 (calendar-current-date))))
11662 (org-eval-in-calendar nil t)
11663 (let* ((old-map (current-local-map))
11664 (map (copy-keymap calendar-mode-map))
11665 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
11666 (org-defkey map (kbd "RET") 'org-calendar-select)
11667 (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
11668 'org-calendar-select-mouse)
11669 (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2
])
11670 'org-calendar-select-mouse
)
11671 (org-defkey minibuffer-local-map
[(meta shift left
)]
11672 (lambda () (interactive)
11673 (org-eval-in-calendar '(calendar-backward-month 1))))
11674 (org-defkey minibuffer-local-map
[(meta shift right
)]
11675 (lambda () (interactive)
11676 (org-eval-in-calendar '(calendar-forward-month 1))))
11677 (org-defkey minibuffer-local-map
[(meta shift up
)]
11678 (lambda () (interactive)
11679 (org-eval-in-calendar '(calendar-backward-year 1))))
11680 (org-defkey minibuffer-local-map
[(meta shift down
)]
11681 (lambda () (interactive)
11682 (org-eval-in-calendar '(calendar-forward-year 1))))
11683 (org-defkey minibuffer-local-map
[(shift up
)]
11684 (lambda () (interactive)
11685 (org-eval-in-calendar '(calendar-backward-week 1))))
11686 (org-defkey minibuffer-local-map
[(shift down
)]
11687 (lambda () (interactive)
11688 (org-eval-in-calendar '(calendar-forward-week 1))))
11689 (org-defkey minibuffer-local-map
[(shift left
)]
11690 (lambda () (interactive)
11691 (org-eval-in-calendar '(calendar-backward-day 1))))
11692 (org-defkey minibuffer-local-map
[(shift right
)]
11693 (lambda () (interactive)
11694 (org-eval-in-calendar '(calendar-forward-day 1))))
11695 (org-defkey minibuffer-local-map
">"
11696 (lambda () (interactive)
11697 (org-eval-in-calendar '(scroll-calendar-left 1))))
11698 (org-defkey minibuffer-local-map
"<"
11699 (lambda () (interactive)
11700 (org-eval-in-calendar '(scroll-calendar-right 1))))
11703 (use-local-map map
)
11704 (add-hook 'post-command-hook
'org-read-date-display
)
11705 (setq org-ans0
(read-string prompt default-input
11706 'org-read-date-history nil
))
11707 ;; org-ans0: from prompt
11708 ;; org-ans1: from mouse click
11709 ;; org-ans2: from calendar motion
11710 (setq ans
(concat org-ans0
" " (or org-ans1 org-ans2
))))
11711 (remove-hook 'post-command-hook
'org-read-date-display
)
11712 (use-local-map old-map
)
11713 (when org-read-date-overlay
11714 (org-delete-overlay org-read-date-overlay
)
11715 (setq org-read-date-overlay nil
)))))))
11717 (t ; Naked prompt only
11719 (setq ans
(read-string prompt default-input
11720 'org-read-date-history timestr
))
11721 (when org-read-date-overlay
11722 (org-delete-overlay org-read-date-overlay
)
11723 (setq org-read-date-overlay nil
)))))
11725 (setq final
(org-read-date-analyze ans def defdecode
))
11728 (apply 'encode-time final
)
11729 (if (and (boundp 'org-time-was-given
) org-time-was-given
)
11730 (format "%04d-%02d-%02d %02d:%02d"
11731 (nth 5 final
) (nth 4 final
) (nth 3 final
)
11732 (nth 2 final
) (nth 1 final
))
11733 (format "%04d-%02d-%02d" (nth 5 final
) (nth 4 final
) (nth 3 final
))))))
11737 (defun org-read-date-display ()
11738 "Display the current date prompt interpretation in the minibuffer."
11739 (when org-read-date-display-live
11740 (when org-read-date-overlay
11741 (org-delete-overlay org-read-date-overlay
))
11744 (while (not (equal (buffer-substring
11745 (max (point-min) (- (point) 4)) (point))
11749 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
11750 " " (or org-ans1 org-ans2
)))
11751 (org-end-time-was-given nil
)
11752 (f (org-read-date-analyze ans def defdecode
))
11754 org-time-stamp-custom-formats
11755 org-time-stamp-formats
))
11756 (fmt (if (or with-time
11757 (and (boundp 'org-time-was-given
) org-time-was-given
))
11760 (txt (concat "=> " (format-time-string fmt
(apply 'encode-time f
)))))
11761 (when (and org-end-time-was-given
11762 (string-match org-plain-time-of-day-regexp txt
))
11763 (setq txt
(concat (substring txt
0 (match-end 0)) "-"
11764 org-end-time-was-given
11765 (substring txt
(match-end 0)))))
11766 (setq org-read-date-overlay
11767 (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
11768 (org-overlay-display org-read-date-overlay txt
'secondary-selection
))))
11770 (defun org-read-date-analyze (ans def defdecode
)
11771 "Analyse the combined answer of the date prompt."
11772 ;; FIXME: cleanup and comment
11773 (let (delta deltan deltaw deltadef year month day
11774 hour minute second wday pm h2 m2 tl wday1
11775 iso-year iso-weekday iso-week iso-year iso-date
)
11777 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans
)
11780 (when (setq delta
(org-read-date-get-relative ans
(current-time) def
))
11781 (setq ans
(replace-match "" t t ans
)
11783 deltaw
(nth 1 delta
)
11784 deltadef
(nth 2 delta
)))
11786 ;; Check if there is an iso week date in there
11787 ;; If yes, sore the info and postpone interpreting it until the rest
11788 ;; of the parsing is done
11789 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans
)
11790 (setq iso-year
(if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans
))))
11791 iso-weekday
(if (match-end 3) (string-to-number (match-string 3 ans
)))
11792 iso-week
(string-to-number (match-string 2 ans
)))
11793 (setq ans
(replace-match "" t t ans
)))
11795 ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
11796 (when (string-match
11797 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans
)
11798 (setq year
(if (match-end 2)
11799 (string-to-number (match-string 2 ans
))
11800 (string-to-number (format-time-string "%Y")))
11801 month
(string-to-number (match-string 3 ans
))
11802 day
(string-to-number (match-string 4 ans
)))
11803 (if (< year
100) (setq year
(+ 2000 year
)))
11804 (setq ans
(replace-match (format "%04d-%02d-%02d\\5" year month day
)
11806 ;; Help matching am/pm times, because `parse-time-string' does not do that.
11807 ;; If there is a time with am/pm, and *no* time without it, we convert
11808 ;; so that matching will be successful.
11809 (loop for i from
1 to
2 do
; twice, for end time as well
11810 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans
))
11811 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans
))
11812 (setq hour
(string-to-number (match-string 1 ans
))
11813 minute
(if (match-end 3)
11814 (string-to-number (match-string 3 ans
))
11817 (string-to-char (downcase (match-string 4 ans
)))))
11818 (if (and (= hour
12) (not pm
))
11820 (if (and pm
(< hour
12)) (setq hour
(+ 12 hour
))))
11821 (setq ans
(replace-match (format "%02d:%02d" hour minute
)
11824 ;; Check if a time range is given as a duration
11825 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans
)
11826 (setq hour
(string-to-number (match-string 1 ans
))
11827 h2
(+ hour
(string-to-number (match-string 3 ans
)))
11828 minute
(string-to-number (match-string 2 ans
))
11829 m2
(+ minute
(if (match-end 5) (string-to-number
11830 (match-string 5 ans
))0)))
11831 (if (>= m2
60) (setq h2
(1+ h2
) m2
(- m2
60)))
11832 (setq ans
(replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2
)
11835 ;; Check if there is a time range
11836 (when (boundp 'org-end-time-was-given
)
11837 (setq org-time-was-given nil
)
11838 (when (and (string-match org-plain-time-of-day-regexp ans
)
11840 (setq org-end-time-was-given
(match-string 8 ans
))
11841 (setq ans
(concat (substring ans
0 (match-beginning 7))
11842 (substring ans
(match-end 7))))))
11844 (setq tl
(parse-time-string ans
)
11845 day
(or (nth 3 tl
) (nth 3 defdecode
))
11846 month
(or (nth 4 tl
)
11847 (if (and org-read-date-prefer-future
11848 (nth 3 tl
) (< (nth 3 tl
) (nth 3 defdecode
)))
11849 (1+ (nth 4 defdecode
))
11850 (nth 4 defdecode
)))
11851 year
(or (nth 5 tl
)
11852 (if (and org-read-date-prefer-future
11853 (nth 4 tl
) (< (nth 4 tl
) (nth 4 defdecode
)))
11854 (1+ (nth 5 defdecode
))
11855 (nth 5 defdecode
)))
11856 hour
(or (nth 2 tl
) (nth 2 defdecode
))
11857 minute
(or (nth 1 tl
) (nth 1 defdecode
))
11858 second
(or (nth 0 tl
) 0)
11861 ;; Special date definitions below
11864 ;; There was an iso week
11865 (setq year
(or iso-year year
)
11866 day
(or iso-weekday wday
1)
11867 wday nil
; to make sure that the trigger below does not match
11868 iso-date
(calendar-gregorian-from-absolute
11869 (calendar-absolute-from-iso
11870 (list iso-week day year
))))
11871 ; FIXME: Should we also push ISO weeks into the future?
11872 ; (when (and org-read-date-prefer-future
11874 ; (< (calendar-absolute-from-gregorian iso-date)
11875 ; (time-to-days (current-time))))
11876 ; (setq year (1+ year)
11877 ; iso-date (calendar-gregorian-from-absolute
11878 ; (calendar-absolute-from-iso
11879 ; (list iso-week day year)))))
11880 (setq month
(car iso-date
)
11881 year
(nth 2 iso-date
)
11882 day
(nth 1 iso-date
)))
11885 (let ((now (decode-time (current-time))))
11886 (setq day
(nth 3 now
) month
(nth 4 now
) year
(nth 5 now
))))
11887 (cond ((member deltaw
'("d" "")) (setq day
(+ day deltan
)))
11888 ((equal deltaw
"w") (setq day
(+ day
(* 7 deltan
))))
11889 ((equal deltaw
"m") (setq month
(+ month deltan
)))
11890 ((equal deltaw
"y") (setq year
(+ year deltan
)))))
11891 ((and wday
(not (nth 3 tl
)))
11892 ;; Weekday was given, but no day, so pick that day in the week
11893 ;; on or after the derived date.
11894 (setq wday1
(nth 6 (decode-time (encode-time 0 0 0 day month year
))))
11895 (unless (equal wday wday1
)
11896 (setq day
(+ day
(%
(- wday wday1 -
7) 7))))))
11897 (if (and (boundp 'org-time-was-given
)
11899 (setq org-time-was-given t
))
11900 (if (< year
100) (setq year
(+ 2000 year
)))
11901 (if (< year
1970) (setq year
(nth 5 defdecode
))) ; not representable
11902 (list second minute hour day month year
)))
11904 (defvar parse-time-weekdays
)
11906 (defun org-read-date-get-relative (s today default
)
11907 "Check string S for special relative date string.
11908 TODAY and DEFAULT are internal times, for today and for a default.
11909 Return shift list (N what def-flag)
11910 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
11911 N is the number of WHATs to shift.
11912 DEF-FLAG is t when a double ++ or -- indicates shift relative to
11913 the DEFAULT date rather than TODAY."
11917 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
11919 "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays
"\\|") "\\)\\)?"
11920 "\\([ \t]\\|$\\)") s
)
11921 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
11922 (let* ((dir (if (> (match-end 1) (match-beginning 1))
11923 (string-to-char (substring (match-string 1 s
) -
1))
11925 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
11926 (n (if (match-end 2) (string-to-number (match-string 2 s
)) 1))
11927 (what (if (match-end 3) (match-string 3 s
) "d"))
11928 (wday1 (cdr (assoc (downcase what
) parse-time-weekdays
)))
11929 (date (if rel default today
))
11930 (wday (nth 6 (decode-time date
)))
11934 (setq delta
(mod (+ 7 (- wday1 wday
)) 7))
11935 (if (= dir ?-
) (setq delta
(- delta
7)))
11936 (if (> n
1) (setq delta
(+ delta
(* (1- n
) (if (= dir ?-
) -
7 7)))))
11937 (list delta
"d" rel
))
11938 (list (* n
(if (= dir ?-
) -
1 1)) what rel
)))))
11940 (defun org-eval-in-calendar (form &optional keepdate
)
11941 "Eval FORM in the calendar window and return to current window.
11942 Also, store the cursor date in variable org-ans2."
11943 (let ((sw (selected-window)))
11944 (select-window (get-buffer-window "*Calendar*"))
11946 (when (and (not keepdate
) (calendar-cursor-to-date))
11947 (let* ((date (calendar-cursor-to-date))
11948 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
11949 (setq org-ans2
(format-time-string "%Y-%m-%d" time
))))
11950 (org-move-overlay org-date-ovl
(1- (point)) (1+ (point)) (current-buffer))
11951 (select-window sw
)))
11953 (defun org-calendar-select ()
11954 "Return to `org-read-date' with the date currently selected.
11955 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
11957 (when (calendar-cursor-to-date)
11958 (let* ((date (calendar-cursor-to-date))
11959 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
11960 (setq org-ans1
(format-time-string "%Y-%m-%d" time
)))
11961 (if (active-minibuffer-window) (exit-minibuffer))))
11963 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra
)
11964 "Insert a date stamp for the date given by the internal TIME.
11965 WITH-HM means, use the stamp format that includes the time of the day.
11966 INACTIVE means use square brackets instead of angular ones, so that the
11967 stamp will not contribute to the agenda.
11968 PRE and POST are optional strings to be inserted before and after the
11970 The command returns the inserted time stamp."
11971 (let ((fmt (funcall (if with-hm
'cdr
'car
) org-time-stamp-formats
))
11973 (if inactive
(setq fmt
(concat "[" (substring fmt
1 -
1) "]")))
11974 (insert-before-markers (or pre
""))
11975 (insert-before-markers (setq stamp
(format-time-string fmt time
)))
11976 (when (listp extra
)
11977 (setq extra
(car extra
))
11978 (if (and (stringp extra
)
11979 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra
))
11980 (setq extra
(format "-%02d:%02d"
11981 (string-to-number (match-string 1 extra
))
11982 (string-to-number (match-string 2 extra
))))
11986 (insert-before-markers extra
)
11988 (insert-before-markers (or post
""))
11989 (setq org-last-inserted-timestamp stamp
)))
11991 (defun org-toggle-time-stamp-overlays ()
11992 "Toggle the use of custom time stamp formats."
11994 (setq org-display-custom-times
(not org-display-custom-times
))
11995 (unless org-display-custom-times
11996 (let ((p (point-min)) (bmp (buffer-modified-p)))
11997 (while (setq p
(next-single-property-change p
'display
))
11998 (if (and (get-text-property p
'display
)
11999 (eq (get-text-property p
'face
) 'org-date
))
12000 (remove-text-properties
12001 p
(setq p
(next-single-property-change p
'display
))
12003 (set-buffer-modified-p bmp
)))
12004 (if (featurep 'xemacs
)
12005 (remove-text-properties (point-min) (point-max) '(end-glyph t
)))
12006 (org-restart-font-lock)
12007 (setq org-table-may-need-update t
)
12008 (if org-display-custom-times
12009 (message "Time stamps are overlayed with custom format")
12010 (message "Time stamp overlays removed")))
12012 (defun org-display-custom-time (beg end
)
12013 "Overlay modified time stamp format over timestamp between BEG and END."
12014 (let* ((ts (buffer-substring beg end
))
12015 t1 w1 with-hm tf time str w2
(off 0))
12017 (setq t1
(org-parse-time-string ts t
))
12018 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts
)
12019 (setq off
(- (match-end 0) (match-beginning 0)))))
12020 (setq end
(- end off
))
12021 (setq w1
(- end beg
)
12022 with-hm
(and (nth 1 t1
) (nth 2 t1
))
12023 tf
(funcall (if with-hm
'cdr
'car
) org-time-stamp-custom-formats
)
12024 time
(org-fix-decoded-time t1
)
12026 (format-time-string
12027 (substring tf
1 -
1) (apply 'encode-time time
))
12028 nil
'mouse-face
'highlight
)
12030 (if (not (= w2 w1
))
12031 (add-text-properties (1+ beg
) (+ 2 beg
)
12032 (list 'org-dwidth t
'org-dwidth-n
(- w1 w2
))))
12033 (if (featurep 'xemacs
)
12035 (put-text-property beg end
'invisible t
)
12036 (put-text-property beg end
'end-glyph
(make-glyph str
)))
12037 (put-text-property beg end
'display str
))))
12039 (defun org-translate-time (string)
12040 "Translate all timestamps in STRING to custom format.
12041 But do this only if the variable `org-display-custom-times' is set."
12042 (when org-display-custom-times
12045 (re org-ts-regexp-both
)
12046 t1 with-hm inactive tf time str beg end
)
12047 (while (setq start
(string-match re string start
))
12048 (setq beg
(match-beginning 0)
12050 t1
(save-match-data
12051 (org-parse-time-string (substring string beg end
) t
))
12052 with-hm
(and (nth 1 t1
) (nth 2 t1
))
12053 inactive
(equal (substring string beg
(1+ beg
)) "[")
12054 tf
(funcall (if with-hm
'cdr
'car
)
12055 org-time-stamp-custom-formats
)
12056 time
(org-fix-decoded-time t1
)
12057 str
(format-time-string
12059 (if inactive
"[" "<") (substring tf
1 -
1)
12060 (if inactive
"]" ">"))
12061 (apply 'encode-time time
))
12062 string
(replace-match str t t string
)
12063 start
(+ start
(length str
)))))))
12066 (defun org-fix-decoded-time (time)
12067 "Set 0 instead of nil for the first 6 elements of time.
12068 Don't touch the rest."
12070 (mapcar (lambda (x) (if (< (setq n
(1+ n
)) 7) (or x
0) x
)) time
)))
12072 (defun org-days-to-time (timestamp-string)
12073 "Difference between TIMESTAMP-STRING and now in days."
12074 (- (time-to-days (org-time-string-to-time timestamp-string
))
12075 (time-to-days (current-time))))
12077 (defun org-deadline-close (timestamp-string &optional ndays
)
12078 "Is the time in TIMESTAMP-STRING close to the current date?"
12079 (setq ndays
(or ndays
(org-get-wdays timestamp-string
)))
12080 (and (< (org-days-to-time timestamp-string
) ndays
)
12081 (not (org-entry-is-done-p))))
12083 (defun org-get-wdays (ts)
12084 "Get the deadline lead time appropriate for timestring TS."
12086 ((<= org-deadline-warning-days
0)
12087 ;; 0 or negative, enforce this value no matter what
12088 (- org-deadline-warning-days
))
12089 ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts
)
12090 ;; lead time is specified.
12091 (floor (* (string-to-number (match-string 1 ts
))
12092 (cdr (assoc (match-string 2 ts
)
12093 '(("d" .
1) ("w" .
7)
12094 ("m" .
30.4) ("y" .
365.25)))))))
12095 ;; go for the default.
12096 (t org-deadline-warning-days
)))
12098 (defun org-calendar-select-mouse (ev)
12099 "Return to `org-read-date' with the date currently selected.
12100 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
12102 (mouse-set-point ev
)
12103 (when (calendar-cursor-to-date)
12104 (let* ((date (calendar-cursor-to-date))
12105 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
12106 (setq org-ans1
(format-time-string "%Y-%m-%d" time
)))
12107 (if (active-minibuffer-window) (exit-minibuffer))))
12109 (defun org-check-deadlines (ndays)
12110 "Check if there are any deadlines due or past due.
12111 A deadline is considered due if it happens within `org-deadline-warning-days'
12112 days from today's date. If the deadline appears in an entry marked DONE,
12113 it is not shown. The prefix arg NDAYS can be used to test that many
12114 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
12116 (let* ((org-warn-days
12118 ((equal ndays
'(4)) 100000)
12119 (ndays (prefix-numeric-value ndays
))
12120 (t (abs org-deadline-warning-days
))))
12121 (case-fold-search nil
)
12122 (regexp (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>"))
12124 (lambda () (org-deadline-close (match-string 1) org-warn-days
))))
12126 (message "%d deadlines past-due or due within %d days"
12127 (org-occur regexp nil callback
)
12130 (defun org-check-before-date (date)
12131 "Check if there are deadlines or scheduled entries before DATE."
12132 (interactive (list (org-read-date)))
12133 (let ((case-fold-search nil
)
12134 (regexp (concat "\\<\\(" org-deadline-string
12135 "\\|" org-scheduled-string
12136 "\\) *<\\([^>]+\\)>"))
12138 (lambda () (time-less-p
12139 (org-time-string-to-time (match-string 2))
12140 (org-time-string-to-time date
)))))
12141 (message "%d entries before %s"
12142 (org-occur regexp nil callback
) date
)))
12144 (defun org-check-after-date (date)
12145 "Check if there are deadlines or scheduled entries after DATE."
12146 (interactive (list (org-read-date)))
12147 (let ((case-fold-search nil
)
12148 (regexp (concat "\\<\\(" org-deadline-string
12149 "\\|" org-scheduled-string
12150 "\\) *<\\([^>]+\\)>"))
12154 (org-time-string-to-time (match-string 2))
12155 (org-time-string-to-time date
))))))
12156 (message "%d entries after %s"
12157 (org-occur regexp nil callback
) date
)))
12159 (defun org-evaluate-time-range (&optional to-buffer
)
12160 "Evaluate a time range by computing the difference between start and end.
12161 Normally the result is just printed in the echo area, but with prefix arg
12162 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
12163 If the time range is actually in a table, the result is inserted into the
12165 For time difference computation, a year is assumed to be exactly 365
12166 days in order to avoid rounding problems."
12169 (org-clock-update-time-maybe)
12171 (unless (org-at-date-range-p t
)
12172 (goto-char (point-at-bol))
12173 (re-search-forward org-tr-regexp-both
(point-at-eol) t
))
12174 (if (not (org-at-date-range-p t
))
12175 (error "Not at a time-stamp range, and none found in current line")))
12176 (let* ((ts1 (match-string 1))
12177 (ts2 (match-string 2))
12178 (havetime (or (> (length ts1
) 15) (> (length ts2
) 15)))
12179 (match-end (match-end 0))
12180 (time1 (org-time-string-to-time ts1
))
12181 (time2 (org-time-string-to-time ts2
))
12182 (t1 (time-to-seconds time1
))
12183 (t2 (time-to-seconds time2
))
12184 (diff (abs (- t2 t1
)))
12185 (negative (< (- t2 t1
) 0))
12186 ;; (ys (floor (* 365 24 60 60)))
12189 (fy "%dy %dd %02d:%02d")
12191 (fd "%dd %02d:%02d")
12196 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
12198 d
(floor (/ diff ds
)) diff
(mod diff ds
)
12199 h
(floor (/ diff hs
)) diff
(mod diff hs
)
12200 m
(floor (/ diff
60)))
12201 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
12203 d
(floor (+ (/ diff ds
) 0.5))
12205 (if (not to-buffer
)
12206 (message "%s" (org-make-tdiff-string y d h m
))
12207 (if (org-at-table-p)
12209 (goto-char match-end
)
12211 (and (looking-at " *|") (goto-char (match-end 0))))
12212 (goto-char match-end
))
12214 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
12215 (replace-match ""))
12216 (if negative
(insert " -"))
12217 (if (> y
0) (insert " " (format (if havetime fy fy1
) y d h m
))
12218 (if (> d
0) (insert " " (format (if havetime fd fd1
) d h m
))
12219 (insert " " (format fh h m
))))
12220 (if align
(org-table-align))
12221 (message "Time difference inserted")))))
12223 (defun org-make-tdiff-string (y d h m
)
12226 (if (> y
0) (setq fmt
(concat fmt
"%d year" (if (> y
1) "s" "") " ")
12228 (if (> d
0) (setq fmt
(concat fmt
"%d day" (if (> d
1) "s" "") " ")
12230 (if (> h
0) (setq fmt
(concat fmt
"%d hour" (if (> h
1) "s" "") " ")
12232 (if (> m
0) (setq fmt
(concat fmt
"%d minute" (if (> m
1) "s" "") " ")
12234 (apply 'format fmt
(nreverse l
))))
12236 (defun org-time-string-to-time (s)
12237 (apply 'encode-time
(org-parse-time-string s
)))
12239 (defun org-time-string-to-absolute (s &optional daynr prefer show-all
)
12240 "Convert a time stamp to an absolute day number.
12241 If there is a specifyer for a cyclic time stamp, get the closest date to
12243 PREFER and SHOW-ALL are passed through to `org-closest-date'."
12245 ((and daynr
(string-match "\\`%%\\((.*)\\)" s
))
12246 (if (org-diary-sexp-entry (match-string 1 s
) "" date
)
12249 ((and daynr
(string-match "\\+[0-9]+[dwmy]" s
))
12250 (org-closest-date s
(if (and (boundp 'daynr
) (integerp daynr
)) daynr
12251 (time-to-days (current-time))) (match-string 0 s
)
12253 (t (time-to-days (apply 'encode-time
(org-parse-time-string s
))))))
12255 (defun org-days-to-iso-week (days)
12256 "Return the iso week number."
12258 (car (calendar-iso-from-absolute days
)))
12260 (defun org-small-year-to-year (year)
12261 "Convert 2-digit years into 4-digit years.
12262 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
12263 The year 2000 cannot be abbreviated. Any year larger than 99
12264 is returned unchanged."
12266 (setq year
(+ 2000 year
))
12268 (setq year
(+ 1900 year
))))
12271 (defun org-time-from-absolute (d)
12272 "Return the time corresponding to date D.
12273 D may be an absolute day number, or a calendar-type list (month day year)."
12274 (if (numberp d
) (setq d
(calendar-gregorian-from-absolute d
)))
12275 (encode-time 0 0 0 (nth 1 d
) (car d
) (nth 2 d
)))
12277 (defun org-calendar-holiday ()
12278 "List of holidays, for Diary display in Org-mode."
12279 (require 'holidays
)
12281 (if (fboundp 'calendar-check-holidays
)
12282 'calendar-check-holidays
'check-calendar-holidays
) date
)))
12283 (if hl
(mapconcat 'identity hl
"; "))))
12285 (defun org-diary-sexp-entry (sexp entry date
)
12286 "Process a SEXP diary ENTRY for DATE."
12287 (require 'diary-lib
)
12288 (let ((result (if calendar-debug-sexp
12289 (let ((stack-trace-on-error t
))
12290 (eval (car (read-from-string sexp
))))
12291 (condition-case nil
12292 (eval (car (read-from-string sexp
)))
12295 (message "Bad sexp at line %d in %s: %s"
12297 (buffer-file-name) sexp
)
12299 (cond ((stringp result
) result
)
12300 ((and (consp result
)
12301 (stringp (cdr result
))) (cdr result
))
12305 (defun org-diary-to-ical-string (frombuf)
12306 "Get iCalendar entries from diary entries in buffer FROMBUF.
12307 This uses the icalendar.el library."
12308 (let* ((tmpdir (if (featurep 'xemacs
)
12310 temporary-file-directory
))
12311 (tmpfile (make-temp-name
12312 (expand-file-name "orgics" tmpdir
)))
12315 (set-buffer frombuf
)
12316 (icalendar-export-region (point-min) (point-max) tmpfile
)
12317 (setq buf
(find-buffer-visiting tmpfile
))
12319 (goto-char (point-min))
12320 (if (re-search-forward "^BEGIN:VEVENT" nil t
)
12321 (setq b
(match-beginning 0)))
12322 (goto-char (point-max))
12323 (if (re-search-backward "^END:VEVENT" nil t
)
12324 (setq e
(match-end 0)))
12325 (setq rtn
(if (and b e
) (concat (buffer-substring b e
) "\n") "")))
12327 (delete-file tmpfile
)
12330 (defun org-closest-date (start current change prefer show-all
)
12331 "Find the date closest to CURRENT that is consistent with START and CHANGE.
12332 When PREFER is `past' return a date that is either CURRENT or past.
12333 When PREFER is `future', return a date that is either CURRENT or future.
12334 When SHOW-ALL is nil, only return the current occurrence of a time stamp."
12335 ;; Make the proper lists from the dates
12337 (let ((a1 '(("d" . day
) ("w" . week
) ("m" . month
) ("y" . year
)))
12338 dn dw sday cday n1 n2 n0
12339 d m y y1 y2 date1 date2 nmonths nm ny m2
)
12341 (setq start
(org-date-to-gregorian start
)
12342 current
(org-date-to-gregorian
12345 (time-to-days (current-time))))
12346 sday
(calendar-absolute-from-gregorian start
)
12347 cday
(calendar-absolute-from-gregorian current
))
12349 (if (<= cday sday
) (throw 'exit sday
))
12351 (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change
)
12352 (setq dn
(string-to-number (match-string 1 change
))
12353 dw
(cdr (assoc (match-string 2 change
) a1
)))
12354 (error "Invalid change specifyer: %s" change
))
12355 (if (eq dw
'week
) (setq dw
'day dn
(* 7 dn
)))
12358 (setq n1
(+ sday
(* dn
(floor (/ (- cday sday
) dn
))))
12361 (setq d
(nth 1 start
) m
(car start
) y1
(nth 2 start
) y2
(nth 2 current
))
12362 (setq y1
(+ (* (floor (/ (- y2 y1
) dn
)) dn
) y1
))
12363 (setq date1
(list m d y1
)
12364 n1
(calendar-absolute-from-gregorian date1
)
12365 date2
(list m d
(+ y1
(* (if (< n1 cday
) 1 -
1) dn
)))
12366 n2
(calendar-absolute-from-gregorian date2
)))
12368 ;; approx number of month between the two dates
12369 (setq nmonths
(floor (/ (- cday sday
) 30.436875)))
12370 ;; How often does dn fit in there?
12371 (setq d
(nth 1 start
) m
(car start
) y
(nth 2 start
)
12372 nm
(* dn
(max 0 (1- (floor (/ nmonths dn
)))))
12374 ny
(floor (/ m
12))
12377 (while (> m
12) (setq m
(- m
12) y
(1+ y
)))
12378 (setq n1
(calendar-absolute-from-gregorian (list m d y
)))
12379 (setq m2
(+ m dn
) y2 y
)
12380 (if (> m2
12) (setq y2
(1+ y2
) m2
(- m2
12)))
12381 (setq n2
(calendar-absolute-from-gregorian (list m2 d y2
)))
12382 (while (<= n2 cday
)
12383 (setq n1 n2 m m2 y y2
)
12384 (setq m2
(+ m dn
) y2 y
)
12385 (if (> m2
12) (setq y2
(1+ y2
) m2
(- m2
12)))
12386 (setq n2
(calendar-absolute-from-gregorian (list m2 d y2
))))))
12387 ;; Make sure n1 is the earlier date
12388 (setq n0 n1 n1
(min n1 n2
) n2
(max n0 n2
))
12391 ((eq prefer
'past
) n1
)
12392 ((eq prefer
'future
) (if (= cday n1
) n1 n2
))
12393 (t (if (> (abs (- cday n1
)) (abs (- cday n2
))) n2 n1
)))
12395 ((eq prefer
'past
) n1
)
12396 ((eq prefer
'future
) (if (= cday n1
) n1 n2
))
12397 (t (if (= cday n1
) n1 n2
)))))))
12399 (defun org-date-to-gregorian (date)
12400 "Turn any specification of DATE into a gregorian date for the calendar."
12401 (cond ((integerp date
) (calendar-gregorian-from-absolute date
))
12402 ((and (listp date
) (= (length date
) 3)) date
)
12404 (setq date
(org-parse-time-string date
))
12405 (list (nth 4 date
) (nth 3 date
) (nth 5 date
)))
12407 (list (nth 4 date
) (nth 3 date
) (nth 5 date
)))))
12409 (defun org-parse-time-string (s &optional nodefault
)
12410 "Parse the standard Org-mode time string.
12411 This should be a lot faster than the normal `parse-time-string'.
12412 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
12413 hour and minute fields will be nil if not given."
12414 (if (string-match org-ts-regexp0 s
)
12416 (if (or (match-beginning 8) (not nodefault
))
12417 (string-to-number (or (match-string 8 s
) "0")))
12418 (if (or (match-beginning 7) (not nodefault
))
12419 (string-to-number (or (match-string 7 s
) "0")))
12420 (string-to-number (match-string 4 s
))
12421 (string-to-number (match-string 3 s
))
12422 (string-to-number (match-string 2 s
))
12426 (defun org-timestamp-up (&optional arg
)
12427 "Increase the date item at the cursor by one.
12428 If the cursor is on the year, change the year. If it is on the month or
12429 the day, change that.
12430 With prefix ARG, change by that many units."
12432 (org-timestamp-change (prefix-numeric-value arg
)))
12434 (defun org-timestamp-down (&optional arg
)
12435 "Decrease the date item at the cursor by one.
12436 If the cursor is on the year, change the year. If it is on the month or
12437 the day, change that.
12438 With prefix ARG, change by that many units."
12440 (org-timestamp-change (- (prefix-numeric-value arg
))))
12442 (defun org-timestamp-up-day (&optional arg
)
12443 "Increase the date in the time stamp by one day.
12444 With prefix ARG, change that many days."
12446 (if (and (not (org-at-timestamp-p t
))
12447 (org-on-heading-p))
12449 (org-timestamp-change (prefix-numeric-value arg
) 'day
)))
12451 (defun org-timestamp-down-day (&optional arg
)
12452 "Decrease the date in the time stamp by one day.
12453 With prefix ARG, change that many days."
12455 (if (and (not (org-at-timestamp-p t
))
12456 (org-on-heading-p))
12458 (org-timestamp-change (- (prefix-numeric-value arg
)) 'day
)))
12460 (defun org-at-timestamp-p (&optional inactive-ok
)
12461 "Determine if the cursor is in or at a timestamp."
12463 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2
))
12465 (ans (or (looking-at tsr
)
12467 (skip-chars-backward "^[<\n\r\t")
12468 (if (> (point) (point-min)) (backward-char 1))
12469 (and (looking-at tsr
)
12470 (> (- (match-end 0) pos
) -
1))))))
12472 (boundp 'org-ts-what
)
12475 ((= pos
(match-beginning 0)) 'bracket
)
12476 ((= pos
(1- (match-end 0))) 'bracket
)
12477 ((org-pos-in-match-range pos
2) 'year
)
12478 ((org-pos-in-match-range pos
3) 'month
)
12479 ((org-pos-in-match-range pos
7) 'hour
)
12480 ((org-pos-in-match-range pos
8) 'minute
)
12481 ((or (org-pos-in-match-range pos
4)
12482 (org-pos-in-match-range pos
5)) 'day
)
12483 ((and (> pos
(or (match-end 8) (match-end 5)))
12484 (< pos
(match-end 0)))
12485 (- pos
(or (match-end 8) (match-end 5))))
12489 (defun org-toggle-timestamp-type ()
12490 "Toggle the type (<active> or [inactive]) of a time stamp."
12492 (when (org-at-timestamp-p t
)
12493 (let ((beg (match-beginning 0)) (end (match-end 0))
12494 (map '((?\
[ .
"<") (?\
] .
">") (?
< .
"[") (?
> .
"]"))))
12497 (while (re-search-forward "[][<>]" end t
)
12498 (replace-match (cdr (assoc (char-after (match-beginning 0)) map
))
12500 (message "Timestamp is now %sactive"
12501 (if (equal (char-after beg
) ?
<) "" "in")))))
12503 (defun org-timestamp-change (n &optional what
)
12504 "Change the date in the time stamp at point.
12505 The date will be changed by N times WHAT. WHAT can be `day', `month',
12506 `year', `minute', `second'. If WHAT is not given, the cursor position
12507 in the timestamp determines what will be changed."
12508 (let ((pos (point))
12510 (dm (max (nth 1 org-time-stamp-rounding-minutes
) 1))
12514 (if (not (org-at-timestamp-p t
))
12515 (error "Not at a timestamp"))
12516 (if (and (not what
) (eq org-ts-what
'bracket
))
12517 (org-toggle-timestamp-type)
12518 (if (and (not what
) (not (eq org-ts-what
'day
))
12519 org-display-custom-times
12520 (get-text-property (point) 'display
)
12521 (not (get-text-property (1- (point)) 'display
)))
12522 (setq org-ts-what
'day
))
12523 (setq org-ts-what
(or what org-ts-what
)
12524 inactive
(= (char-after (match-beginning 0)) ?\
[)
12525 ts
(match-string 0))
12528 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
12530 (setq extra
(match-string 1 ts
)))
12531 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts
)
12533 (setq time0
(org-parse-time-string ts
))
12534 (when (and (eq org-ts-what
'minute
)
12535 (eq current-prefix-arg nil
))
12536 (setq n
(* dm
(cond ((> n
0) 1) ((< n
0) -
1) (t 0))))
12537 (when (not (= 0 (setq rem
(%
(nth 1 time0
) dm
))))
12538 (setcar (cdr time0
) (+ (nth 1 time0
)
12539 (if (> n
0) (- rem
) (- dm rem
))))))
12541 (encode-time (or (car time0
) 0)
12542 (+ (if (eq org-ts-what
'minute
) n
0) (nth 1 time0
))
12543 (+ (if (eq org-ts-what
'hour
) n
0) (nth 2 time0
))
12544 (+ (if (eq org-ts-what
'day
) n
0) (nth 3 time0
))
12545 (+ (if (eq org-ts-what
'month
) n
0) (nth 4 time0
))
12546 (+ (if (eq org-ts-what
'year
) n
0) (nth 5 time0
))
12548 (when (and (member org-ts-what
'(hour minute
))
12550 (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra
))
12551 (setq extra
(org-modify-ts-extra
12553 (if (eq org-ts-what
'hour
) 2 5)
12555 (when (integerp org-ts-what
)
12556 (setq extra
(org-modify-ts-extra extra org-ts-what n dm
)))
12557 (if (eq what
'calendar
)
12558 (let ((cal-date (org-get-date-from-calendar)))
12559 (setcar (nthcdr 4 time0
) (nth 0 cal-date
)) ; month
12560 (setcar (nthcdr 3 time0
) (nth 1 cal-date
)) ; day
12561 (setcar (nthcdr 5 time0
) (nth 2 cal-date
)) ; year
12562 (setcar time0
(or (car time0
) 0))
12563 (setcar (nthcdr 1 time0
) (or (nth 1 time0
) 0))
12564 (setcar (nthcdr 2 time0
) (or (nth 2 time0
) 0))
12565 (setq time
(apply 'encode-time time0
))))
12566 (setq org-last-changed-timestamp
12567 (org-insert-time-stamp time with-hm inactive nil nil extra
))
12568 (org-clock-update-time-maybe)
12570 ;; Try to recenter the calendar window, if any
12571 (if (and org-calendar-follow-timestamp-change
12572 (get-buffer-window "*Calendar*" t
)
12573 (memq org-ts-what
'(day month year
)))
12574 (org-recenter-calendar (time-to-days time
))))))
12576 (defun org-modify-ts-extra (s pos n dm
)
12577 "Change the different parts of the lead-time and repeat fields in timestamp."
12578 (let ((idx '(("d" .
0) ("w" .
1) ("m" .
2) ("y" .
3) ("d" . -
1) ("y" .
4)))
12580 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s
)
12582 ((or (org-pos-in-match-range pos
2)
12583 (org-pos-in-match-range pos
3))
12584 (setq m
(string-to-number (match-string 3 s
))
12585 h
(string-to-number (match-string 2 s
)))
12586 (if (org-pos-in-match-range pos
2)
12588 (setq n
(* dm
(org-no-warnings (signum n
))))
12589 (when (not (= 0 (setq rem
(% m dm
))))
12590 (setq m
(+ m
(if (> n
0) (- rem
) (- dm rem
)))))
12592 (if (< m
0) (setq m
(+ m
60) h
(1- h
)))
12593 (if (> m
59) (setq m
(- m
60) h
(1+ h
)))
12594 (setq h
(min 24 (max 0 h
)))
12595 (setq ng
1 new
(format "-%02d:%02d" h m
)))
12596 ((org-pos-in-match-range pos
6)
12597 (setq ng
6 new
(car (rassoc (+ n
(cdr (assoc (match-string 6 s
) idx
))) idx
))))
12598 ((org-pos-in-match-range pos
5)
12599 (setq ng
5 new
(format "%d" (max 1 (+ n
(string-to-number (match-string 5 s
)))))))
12601 ((org-pos-in-match-range pos
9)
12602 (setq ng
9 new
(car (rassoc (+ n
(cdr (assoc (match-string 9 s
) idx
))) idx
))))
12603 ((org-pos-in-match-range pos
8)
12604 (setq ng
8 new
(format "%d" (max 0 (+ n
(string-to-number (match-string 8 s
))))))))
12608 (substring s
0 (match-beginning ng
))
12610 (substring s
(match-end ng
))))))
12613 (defun org-recenter-calendar (date)
12614 "If the calendar is visible, recenter it to DATE."
12615 (let* ((win (selected-window))
12616 (cwin (get-buffer-window "*Calendar*" t
))
12617 (calendar-move-hook nil
))
12619 (select-window cwin
)
12620 (calendar-goto-date (if (listp date
) date
12621 (calendar-gregorian-from-absolute date
)))
12622 (select-window win
))))
12624 (defun org-goto-calendar (&optional arg
)
12625 "Go to the Emacs calendar at the current date.
12626 If there is a time stamp in the current line, go to that date.
12627 A prefix ARG can be used to force the current date."
12629 (let ((tsr org-ts-regexp
) diff
12630 (calendar-move-hook nil
)
12631 (calendar-view-holidays-initially-flag nil
)
12632 (view-calendar-holidays-initially nil
)
12633 (calendar-view-diary-initially-flag nil
)
12634 (view-diary-entries-initially nil
))
12635 (if (or (org-at-timestamp-p)
12637 (beginning-of-line 1)
12638 (looking-at (concat ".*" tsr
))))
12639 (let ((d1 (time-to-days (current-time)))
12641 (org-time-string-to-time (match-string 1)))))
12642 (setq diff
(- d2 d1
))))
12644 (calendar-goto-today)
12645 (if (and diff
(not arg
)) (calendar-forward-day diff
))))
12647 (defun org-get-date-from-calendar ()
12648 "Return a list (month day year) of date at point in calendar."
12649 (with-current-buffer "*Calendar*"
12651 (calendar-cursor-to-date))))
12653 (defun org-date-from-calendar ()
12654 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
12655 If there is already a time stamp at the cursor position, update it."
12657 (if (org-at-timestamp-p t
)
12658 (org-timestamp-change 0 'calendar
)
12659 (let ((cal-date (org-get-date-from-calendar)))
12660 (org-insert-time-stamp
12661 (encode-time 0 0 0 (nth 1 cal-date
) (car cal-date
) (nth 2 cal-date
))))))
12663 (defun org-minutes-to-hh:mm-string
(m)
12664 "Compute H:MM from a number of minutes."
12665 (let ((h (/ m
60)))
12666 (setq m
(- m
(* 60 h
)))
12667 (format org-time-clocksum-format h m
)))
12669 (defun org-hh:mm-string-to-minutes
(s)
12670 "Convert a string H:MM to a number of minutes."
12671 (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s
)
12672 (+ (* (string-to-number (match-string 1 s
)) 60)
12673 (string-to-number (match-string 2 s
)))
12678 (defun org-save-all-org-buffers ()
12679 "Save all Org-mode buffers without user confirmation."
12681 (message "Saving all Org-mode buffers...")
12682 (save-some-buffers t
'org-mode-p
)
12683 (when (featurep 'org-id
) (org-id-locations-save))
12684 (message "Saving all Org-mode buffers... done"))
12686 (defun org-revert-all-org-buffers ()
12687 "Revert all Org-mode buffers.
12688 Prompt for confirmation when there are unsaved changes.
12689 Be sure you know what you are doing before letting this function
12690 overwrite your changes.
12692 This function is useful in a setup where one tracks org files
12693 with a version control system, to revert on one machine after pulling
12694 changes from another. I believe the procedure must be like this:
12696 1. M-x org-save-all-org-buffers
12697 2. Pull changes from the other machine, resolve conflicts
12698 3. M-x org-revert-all-org-buffers"
12700 (unless (yes-or-no-p "Revert all Org buffers from their files? ")
12703 (save-window-excursion
12706 (when (and (with-current-buffer b
(org-mode-p))
12707 (with-current-buffer b buffer-file-name
))
12708 (switch-to-buffer b
)
12709 (revert-buffer t
'no-confirm
)))
12711 (when (and (featurep 'org-id
) org-id-track-globally
)
12712 (org-id-locations-load)))))
12717 (defun org-iswitchb (&optional arg
)
12718 "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
12719 With a prefix argument, restrict available to files.
12720 With two prefix arguments, restrict available buffers to agenda files.
12722 Due to some yet unresolved reason, the global function
12723 `iswitchb-mode' needs to be active for this function to work."
12725 (require 'iswitchb
)
12726 (let ((enabled iswitchb-mode
) blist
)
12727 (or enabled
(iswitchb-mode 1))
12728 (setq blist
(cond ((equal arg
'(4)) (org-buffer-list 'files
))
12729 ((equal arg
'(16)) (org-buffer-list 'agenda
))
12730 (t (org-buffer-list))))
12732 (let ((iswitchb-make-buflist-hook
12734 (setq iswitchb-temp-buflist
12735 (mapcar 'buffer-name blist
)))))
12737 (iswitchb-read-buffer
12738 "Switch-to: " nil t
))
12739 (or enabled
(iswitchb-mode -
1))))))
12742 (defun org-ido-switchb (&optional arg
)
12743 "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
12744 With a prefix argument, restrict available to files.
12745 With two prefix arguments, restrict available buffers to agenda files."
12747 (let ((blist (cond ((equal arg
'(4)) (org-buffer-list 'files
))
12748 ((equal arg
'(16)) (org-buffer-list 'agenda
))
12749 (t (org-buffer-list)))))
12751 (org-ido-completing-read "Org buffer: "
12752 (mapcar 'buffer-name blist
)
12755 (defun org-buffer-list (&optional predicate exclude-tmp
)
12756 "Return a list of Org buffers.
12757 PREDICATE can be `export', `files' or `agenda'.
12759 export restrict the list to Export buffers.
12760 files restrict the list to buffers visiting Org files.
12761 agenda restrict the list to buffers visiting agenda files.
12763 If EXCLUDE-TMP is non-nil, ignore temporary buffers."
12765 (agenda-files (and (eq predicate
'agenda
)
12766 (mapcar 'file-truename
(org-agenda-files t
))))
12769 ((eq predicate
'files
)
12770 (lambda (b) (with-current-buffer b
(eq major-mode
'org-mode
))))
12771 ((eq predicate
'export
)
12772 (lambda (b) (string-match "\*Org .*Export" (buffer-name b
))))
12773 ((eq predicate
'agenda
)
12775 (with-current-buffer b
12776 (and (eq major-mode
'org-mode
)
12777 (setq bfn
(buffer-file-name b
))
12778 (member (file-truename bfn
) agenda-files
)))))
12779 (t (lambda (b) (with-current-buffer b
12780 (or (eq major-mode
'org-mode
)
12781 (string-match "\*Org .*Export"
12782 (buffer-name b
)))))))))
12786 (if (and (funcall filter b
)
12787 (or (not exclude-tmp
)
12788 (not (string-match "tmp" (buffer-name b
)))))
12793 (defun org-agenda-files (&optional unrestricted archives
)
12794 "Get the list of agenda files.
12795 Optional UNRESTRICTED means return the full list even if a restriction
12796 is currently in place.
12797 When ARCHIVES is t, include all archive files hat are really being
12798 used by the agenda files. If ARCHIVE is `ifmode', do this only if
12799 `org-agenda-archives-mode' is t."
12802 ((and (not unrestricted
) (get 'org-agenda-files
'org-restrict
)))
12803 ((stringp org-agenda-files
) (org-read-agenda-file-list))
12804 ((listp org-agenda-files
) org-agenda-files
)
12805 (t (error "Invalid value of `org-agenda-files'")))))
12806 (setq files
(apply 'append
12807 (mapcar (lambda (f)
12808 (if (file-directory-p f
)
12810 f t org-agenda-file-regexp
)
12813 (when org-agenda-skip-unavailable-files
12814 (setq files
(delq nil
12817 (and (file-readable-p file
) file
)))
12819 (when (or (eq archives t
)
12820 (and (eq archives
'ifmode
) (eq org-agenda-archives-mode t
)))
12821 (setq files
(org-add-archive-files files
)))
12824 (defun org-edit-agenda-file-list ()
12825 "Edit the list of agenda files.
12826 Depending on setup, this either uses customize to edit the variable
12827 `org-agenda-files', or it visits the file that is holding the list. In the
12828 latter case, the buffer is set up in a way that saving it automatically kills
12829 the buffer and restores the previous window configuration."
12831 (if (stringp org-agenda-files
)
12832 (let ((cw (current-window-configuration)))
12833 (find-file org-agenda-files
)
12834 (org-set-local 'org-window-configuration cw
)
12835 (org-add-hook 'after-save-hook
12837 (set-window-configuration
12838 (prog1 org-window-configuration
12839 (kill-buffer (current-buffer))))
12840 (org-install-agenda-files-menu)
12841 (message "New agenda file list installed"))
12843 (message "%s" (substitute-command-keys
12844 "Edit list and finish with \\[save-buffer]")))
12845 (customize-variable 'org-agenda-files
)))
12847 (defun org-store-new-agenda-file-list (list)
12848 "Set new value for the agenda file list and save it correctly."
12849 (if (stringp org-agenda-files
)
12850 (let ((f org-agenda-files
) b
)
12851 (while (setq b
(find-buffer-visiting f
)) (kill-buffer b
))
12853 (insert (mapconcat 'identity list
"\n") "\n")))
12854 (let ((org-mode-hook nil
) (default-major-mode 'fundamental-mode
))
12855 (setq org-agenda-files list
)
12856 (customize-save-variable 'org-agenda-files org-agenda-files
))))
12858 (defun org-read-agenda-file-list ()
12859 "Read the list of agenda files from a file."
12860 (when (file-directory-p org-agenda-files
)
12861 (error "`org-agenda-files' cannot be a single directory"))
12862 (when (stringp org-agenda-files
)
12864 (insert-file-contents org-agenda-files
)
12865 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
12869 (defun org-cycle-agenda-files ()
12870 "Cycle through the files in `org-agenda-files'.
12871 If the current buffer visits an agenda file, find the next one in the list.
12872 If the current buffer does not, find the first agenda file."
12874 (let* ((fs (org-agenda-files t
))
12875 (files (append fs
(list (car fs
))))
12876 (tcf (if buffer-file-name
(file-truename buffer-file-name
)))
12878 (unless files
(error "No agenda files"))
12880 (while (setq file
(pop files
))
12881 (if (equal (file-truename file
) tcf
)
12883 (find-file (car files
))
12885 (find-file (car fs
)))
12886 (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
12888 (defun org-agenda-file-to-front (&optional to-end
)
12889 "Move/add the current file to the top of the agenda file list.
12890 If the file is not present in the list, it is added to the front. If it is
12891 present, it is moved there. With optional argument TO-END, add/move to the
12894 (let ((org-agenda-skip-unavailable-files nil
)
12895 (file-alist (mapcar (lambda (x)
12896 (cons (file-truename x
) x
))
12897 (org-agenda-files t
)))
12898 (ctf (file-truename buffer-file-name
))
12900 (setq x
(assoc ctf file-alist
) had x
)
12902 (if (not x
) (setq x
(cons ctf
(abbreviate-file-name buffer-file-name
))))
12904 (setq file-alist
(append (delq x file-alist
) (list x
)))
12905 (setq file-alist
(cons x
(delq x file-alist
))))
12906 (org-store-new-agenda-file-list (mapcar 'cdr file-alist
))
12907 (org-install-agenda-files-menu)
12908 (message "File %s to %s of agenda file list"
12909 (if had
"moved" "added") (if to-end
"end" "front"))))
12911 (defun org-remove-file (&optional file
)
12912 "Remove current file from the list of files in variable `org-agenda-files'.
12913 These are the files which are being checked for agenda entries.
12914 Optional argument FILE means, use this file instead of the current."
12916 (let* ((org-agenda-skip-unavailable-files nil
)
12917 (file (or file buffer-file-name
))
12918 (true-file (file-truename file
))
12919 (afile (abbreviate-file-name file
))
12920 (files (delq nil
(mapcar
12922 (if (equal true-file
12925 (org-agenda-files t
)))))
12926 (if (not (= (length files
) (length (org-agenda-files t
))))
12928 (org-store-new-agenda-file-list files
)
12929 (org-install-agenda-files-menu)
12930 (message "Removed file: %s" afile
))
12931 (message "File was not in list: %s (not removed)" afile
))))
12933 (defun org-file-menu-entry (file)
12934 (vector file
(list 'find-file file
) t
))
12936 (defun org-check-agenda-file (file)
12937 "Make sure FILE exists. If not, ask user what to do."
12938 (when (not (file-exists-p file
))
12939 (message "non-existent file %s. [R]emove from list or [A]bort?"
12940 (abbreviate-file-name file
))
12941 (let ((r (downcase (read-char-exclusive))))
12944 (org-remove-file file
)
12945 (throw 'nextfile t
))
12946 (t (error "Abort"))))))
12948 (defun org-get-agenda-file-buffer (file)
12949 "Get a buffer visiting FILE. If the buffer needs to be created, add
12950 it to the list of buffers which might be released later."
12951 (let ((buf (org-find-base-buffer-visiting file
)))
12953 buf
; just return it
12954 ;; Make a new buffer and remember it
12955 (setq buf
(find-file-noselect file
))
12956 (if buf
(push buf org-agenda-new-buffers
))
12959 (defun org-release-buffers (blist)
12960 "Release all buffers in list, asking the user for confirmation when needed.
12961 When a buffer is unmodified, it is just killed. When modified, it is saved
12962 \(if the user agrees) and then killed."
12964 (while (setq buf
(pop blist
))
12965 (setq file
(buffer-file-name buf
))
12966 (when (and (buffer-modified-p buf
)
12968 (y-or-n-p (format "Save file %s? " file
)))
12969 (with-current-buffer buf
(save-buffer)))
12970 (kill-buffer buf
))))
12972 (defun org-prepare-agenda-buffers (files)
12973 "Create buffers for all agenda files, protect archived trees and comments."
12975 (let ((pa '(:org-archived t
))
12976 (pc '(:org-comment t
))
12977 (pall '(:org-archived t
:org-comment t
))
12978 (inhibit-read-only t
)
12979 (rea (concat ":" org-archive-tag
":"))
12983 (while (setq file
(pop files
))
12986 (org-check-agenda-file file
)
12987 (set-buffer (org-get-agenda-file-buffer file
)))
12989 (setq bmp
(buffer-modified-p))
12990 (org-refresh-category-properties)
12991 (setq org-todo-keywords-for-agenda
12992 (append org-todo-keywords-for-agenda org-todo-keywords-1
))
12993 (setq org-done-keywords-for-agenda
12994 (append org-done-keywords-for-agenda org-done-keywords
))
12995 (setq org-todo-keyword-alist-for-agenda
12996 (append org-todo-keyword-alist-for-agenda org-todo-key-alist
))
12997 (setq org-tag-alist-for-agenda
12998 (append org-tag-alist-for-agenda org-tag-alist
))
13001 (remove-text-properties (point-min) (point-max) pall
)
13002 (when org-agenda-skip-archived-trees
13003 (goto-char (point-min))
13004 (while (re-search-forward rea nil t
)
13005 (if (org-on-heading-p t
)
13006 (add-text-properties (point-at-bol) (org-end-of-subtree t
) pa
))))
13007 (goto-char (point-min))
13008 (setq re
(concat "^\\*+ +" org-comment-string
"\\>"))
13009 (while (re-search-forward re nil t
)
13010 (add-text-properties
13011 (match-beginning 0) (org-end-of-subtree t
) pc
)))
13012 (set-buffer-modified-p bmp
))))
13013 (setq org-todo-keyword-alist-for-agenda
13014 (org-uniquify org-todo-keyword-alist-for-agenda
)
13015 org-tag-alist-for-agenda
(org-uniquify org-tag-alist-for-agenda
))))
13017 ;;;; Embedded LaTeX
13019 (defvar org-cdlatex-mode-map
(make-sparse-keymap)
13020 "Keymap for the minor `org-cdlatex-mode'.")
13022 (org-defkey org-cdlatex-mode-map
"_" 'org-cdlatex-underscore-caret
)
13023 (org-defkey org-cdlatex-mode-map
"^" 'org-cdlatex-underscore-caret
)
13024 (org-defkey org-cdlatex-mode-map
"`" 'cdlatex-math-symbol
)
13025 (org-defkey org-cdlatex-mode-map
"'" 'org-cdlatex-math-modify
)
13026 (org-defkey org-cdlatex-mode-map
"\C-c{" 'cdlatex-environment
)
13028 (defvar org-cdlatex-texmathp-advice-is-done nil
13029 "Flag remembering if we have applied the advice to texmathp already.")
13031 (define-minor-mode org-cdlatex-mode
13032 "Toggle the minor `org-cdlatex-mode'.
13033 This mode supports entering LaTeX environment and math in LaTeX fragments
13035 \\{org-cdlatex-mode-map}"
13037 (when org-cdlatex-mode
(require 'cdlatex
))
13038 (unless org-cdlatex-texmathp-advice-is-done
13039 (setq org-cdlatex-texmathp-advice-is-done t
)
13040 (defadvice texmathp
(around org-math-always-on activate
)
13041 "Always return t in org-mode buffers.
13042 This is because we want to insert math symbols without dollars even outside
13043 the LaTeX math segments. If Orgmode thinks that point is actually inside
13044 an embedded LaTeX fragment, let texmathp do its job.
13045 \\[org-cdlatex-mode-map]"
13049 ((not (org-mode-p)) ad-do-it
)
13050 ((eq this-command
'cdlatex-math-symbol
)
13051 (setq ad-return-value t
13052 texmathp-why
'("cdlatex-math-symbol in org-mode" .
0)))
13054 (let ((p (org-inside-LaTeX-fragment-p)))
13055 (if (and p
(member (car p
) (plist-get org-format-latex-options
:matchers
)))
13056 (setq ad-return-value t
13057 texmathp-why
'("Org-mode embedded math" .
0))
13058 (if p ad-do-it
)))))))))
13060 (defun turn-on-org-cdlatex ()
13061 "Unconditionally turn on `org-cdlatex-mode'."
13062 (org-cdlatex-mode 1))
13064 (defun org-inside-LaTeX-fragment-p ()
13065 "Test if point is inside a LaTeX fragment.
13066 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
13067 sequence appearing also before point.
13068 Even though the matchers for math are configurable, this function assumes
13069 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
13070 delimiters are skipped when they have been removed by customization.
13071 The return value is nil, or a cons cell with the delimiter and
13072 and the position of this delimiter.
13074 This function does a reasonably good job, but can locally be fooled by
13075 for example currency specifications. For example it will assume being in
13076 inline math after \"$22.34\". The LaTeX fragment formatter will only format
13077 fragments that are properly closed, but during editing, we have to live
13078 with the uncertainty caused by missing closing delimiters. This function
13079 looks only before point, not after."
13081 (let ((pos (point))
13082 (dodollar (member "$" (plist-get org-format-latex-options
:matchers
)))
13084 (re-search-backward (concat "^\\(" paragraph-start
"\\)") nil t
)
13086 dd-on str
(start 0) m re
)
13089 (setq str
(concat (buffer-substring lim
(point)) "\000 X$.")
13090 re
(nth 1 (assoc "$" org-latex-regexps
)))
13091 (while (string-match re str start
)
13093 ((= (match-end 0) (length str
))
13094 (throw 'exit
(cons "$" (+ lim
(match-beginning 0) 1))))
13095 ((= (match-end 0) (- (length str
) 5))
13097 (t (setq start
(match-end 0))))))
13098 (when (setq m
(re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t
))
13100 (and (match-beginning 1) (throw 'exit
(cons (match-string 1) m
)))
13101 (and (match-beginning 2) (throw 'exit nil
))
13103 (while (re-search-backward "\\$\\$" lim t
)
13104 (setq dd-on
(not dd-on
)))
13106 (if dd-on
(cons "$$" m
))))))
13109 (defun org-try-cdlatex-tab ()
13110 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
13111 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
13112 - inside a LaTeX fragment, or
13113 - after the first word in a line, where an abbreviation expansion could
13114 insert a LaTeX environment."
13115 (when org-cdlatex-mode
13118 (skip-chars-backward "a-zA-Z0-9*")
13119 (skip-chars-backward " \t")
13122 ((org-inside-LaTeX-fragment-p)
13126 (defun org-cdlatex-underscore-caret (&optional arg
)
13127 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
13128 Revert to the normal definition outside of these fragments."
13130 (if (org-inside-LaTeX-fragment-p)
13131 (call-interactively 'cdlatex-sub-superscript
)
13132 (let (org-cdlatex-mode)
13133 (call-interactively (key-binding (vector last-input-event
))))))
13135 (defun org-cdlatex-math-modify (&optional arg
)
13136 "Execute `cdlatex-math-modify' in LaTeX fragments.
13137 Revert to the normal definition outside of these fragments."
13139 (if (org-inside-LaTeX-fragment-p)
13140 (call-interactively 'cdlatex-math-modify
)
13141 (let (org-cdlatex-mode)
13142 (call-interactively (key-binding (vector last-input-event
))))))
13144 (defvar org-latex-fragment-image-overlays nil
13145 "List of overlays carrying the images of latex fragments.")
13146 (make-variable-buffer-local 'org-latex-fragment-image-overlays
)
13148 (defun org-remove-latex-fragment-image-overlays ()
13149 "Remove all overlays with LaTeX fragment images in current buffer."
13150 (mapc 'org-delete-overlay org-latex-fragment-image-overlays
)
13151 (setq org-latex-fragment-image-overlays nil
))
13153 (defun org-preview-latex-fragment (&optional subtree
)
13154 "Preview the LaTeX fragment at point, or all locally or globally.
13155 If the cursor is in a LaTeX fragment, create the image and overlay
13156 it over the source code. If there is no fragment at point, display
13157 all fragments in the current text, from one headline to the next. With
13158 prefix SUBTREE, display all fragments in the current subtree. With a
13159 double prefix `C-u C-u', or when the cursor is before the first headline,
13160 display all fragments in the buffer.
13161 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
13163 (org-remove-latex-fragment-image-overlays)
13166 (let (beg end at msg
)
13168 ((or (equal subtree
'(16))
13169 (not (save-excursion
13170 (re-search-backward (concat "^" outline-regexp
) nil t
))))
13171 (setq beg
(point-min) end
(point-max)
13172 msg
"Creating images for buffer...%s"))
13173 ((equal subtree
'(4))
13174 (org-back-to-heading)
13175 (setq beg
(point) end
(org-end-of-subtree t
)
13176 msg
"Creating images for subtree...%s"))
13178 (if (setq at
(org-inside-LaTeX-fragment-p))
13179 (goto-char (max (point-min) (- (cdr at
) 2)))
13180 (org-back-to-heading))
13181 (setq beg
(point) end
(progn (outline-next-heading) (point))
13182 msg
(if at
"Creating image...%s"
13183 "Creating images for entry...%s"))))
13185 (narrow-to-region beg end
)
13188 (concat "ltxpng/" (file-name-sans-extension
13189 (file-name-nondirectory
13190 buffer-file-name
)))
13191 default-directory
'overlays msg at
'forbuffer
)
13192 (message msg
"done. Use `C-c C-c' to remove images.")))))
13194 (defvar org-latex-regexps
13195 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t
)
13196 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
13197 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
13198 ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil
)
13199 ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil
)
13200 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil
)
13201 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t
)
13202 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t
))
13203 "Regular expressions for matching embedded LaTeX.")
13205 (defun org-format-latex (prefix &optional dir overlays msg at forbuffer
)
13206 "Replace LaTeX fragments with links to an image, and produce images."
13207 (if (and overlays
(fboundp 'clear-image-cache
)) (clear-image-cache))
13208 (let* ((prefixnodir (file-name-nondirectory prefix
))
13209 (absprefix (expand-file-name prefix dir
))
13210 (todir (file-name-directory absprefix
))
13211 (opt org-format-latex-options
)
13212 (matchers (plist-get opt
:matchers
))
13213 (re-list org-latex-regexps
)
13214 (cnt 0) txt link beg end re e checkdir
13215 m n block linkfile movefile ov
)
13216 ;; Check if there are old images files with this prefix, and remove them
13217 (when (file-directory-p todir
)
13221 (concat (regexp-quote prefixnodir
) "_[0-9]+\\.png$"))))
13222 ;; Check the different regular expressions
13223 (while (setq e
(pop re-list
))
13224 (setq m
(car e
) re
(nth 1 e
) n
(nth 2 e
)
13225 block
(if (nth 3 e
) "\n\n" ""))
13226 (when (member m matchers
)
13227 (goto-char (point-min))
13228 (while (re-search-forward re nil t
)
13229 (when (and (or (not at
) (equal (cdr at
) (match-beginning n
)))
13230 (not (get-text-property (match-beginning n
)
13232 (setq txt
(match-string n
)
13233 beg
(match-beginning n
) end
(match-end n
)
13235 linkfile
(format "%s_%04d.png" prefix cnt
)
13236 movefile
(format "%s_%04d.png" absprefix cnt
)
13237 link
(concat block
"[[file:" linkfile
"]]" block
))
13238 (if msg
(message msg cnt
))
13240 (unless checkdir
; make sure the directory exists
13242 (or (file-directory-p todir
) (make-directory todir
)))
13243 (org-create-formula-image
13244 txt movefile opt forbuffer
)
13247 (setq ov
(org-make-overlay beg end
))
13248 (if (featurep 'xemacs
)
13250 (org-overlay-put ov
'invisible t
)
13253 (make-glyph (vector 'png
:file movefile
))))
13256 (list 'image
:type
'png
:file movefile
:ascent
'center
)))
13257 (push ov org-latex-fragment-image-overlays
)
13259 (delete-region beg end
)
13260 (insert link
))))))))
13262 ;; This function borrows from Ganesh Swami's latex2png.el
13263 (defun org-create-formula-image (string tofile options buffer
)
13264 (let* ((tmpdir (if (featurep 'xemacs
)
13266 temporary-file-directory
))
13267 (texfilebase (make-temp-name
13268 (expand-file-name "orgtex" tmpdir
)))
13269 (texfile (concat texfilebase
".tex"))
13270 (dvifile (concat texfilebase
".dvi"))
13271 (pngfile (concat texfilebase
".png"))
13272 (fnh (if (featurep 'xemacs
)
13273 (font-height (get-face-font 'default
))
13274 (face-attribute 'default
:height nil
)))
13275 (scale (or (plist-get options
(if buffer
:scale
:html-scale
)) 1.0))
13276 (dpi (number-to-string (* scale
(floor (* 0.9 (if buffer fnh
140.
))))))
13277 (fg (or (plist-get options
(if buffer
:foreground
:html-foreground
))
13279 (bg (or (plist-get options
(if buffer
:background
:html-background
))
13281 (if (eq fg
'default
) (setq fg
(org-dvipng-color :foreground
)))
13282 (if (eq bg
'default
) (setq bg
(org-dvipng-color :background
)))
13283 (with-temp-file texfile
13284 (insert org-format-latex-header
13285 "\n\\begin{document}\n" string
"\n\\end{document}\n"))
13286 (let ((dir default-directory
))
13287 (condition-case nil
13290 (call-process "latex" nil nil nil texfile
))
13293 (if (not (file-exists-p dvifile
))
13294 (progn (message "Failed to create dvi file from %s" texfile
) nil
)
13295 (condition-case nil
13296 (call-process "dvipng" nil nil nil
13297 "-E" "-fg" fg
"-bg" bg
13299 ;;"-x" scale "-y" scale
13304 (if (not (file-exists-p pngfile
))
13305 (progn (message "Failed to create png file from %s" texfile
) nil
)
13306 ;; Use the requested file name and clean up
13307 (copy-file pngfile tofile
'replace
)
13308 (loop for e in
'(".dvi" ".tex" ".aux" ".log" ".png") do
13309 (delete-file (concat texfilebase e
)))
13312 (defun org-dvipng-color (attr)
13313 "Return an rgb color specification for dvipng."
13314 (apply 'format
"rgb %s %s %s"
13315 (mapcar 'org-normalize-color
13316 (color-values (face-attribute 'default attr nil
)))))
13318 (defun org-normalize-color (value)
13319 "Return string to be used as color value for an RGB component."
13320 (format "%g" (/ value
65535.0)))
13324 ;; Make `C-c C-x' a prefix key
13325 (org-defkey org-mode-map
"\C-c\C-x" (make-sparse-keymap))
13327 ;; TAB key with modifiers
13328 (org-defkey org-mode-map
"\C-i" 'org-cycle
)
13329 (org-defkey org-mode-map
[(tab)] 'org-cycle
)
13330 (org-defkey org-mode-map
[(control tab
)] 'org-force-cycle-archived
)
13331 (org-defkey org-mode-map
[(meta tab
)] 'org-complete
)
13332 (org-defkey org-mode-map
"\M-\t" 'org-complete
)
13333 (org-defkey org-mode-map
"\M-\C-i" 'org-complete
)
13334 ;; The following line is necessary under Suse GNU/Linux
13335 (unless (featurep 'xemacs
)
13336 (org-defkey org-mode-map
[S-iso-lefttab
] 'org-shifttab
))
13337 (org-defkey org-mode-map
[(shift tab
)] 'org-shifttab
)
13338 (define-key org-mode-map
[backtab] 'org-shifttab)
13340 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
13341 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
13342 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
13344 ;; Cursor keys with modifiers
13345 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
13346 (org-defkey org-mode-map [(meta right)] 'org-metaright)
13347 (org-defkey org-mode-map [(meta up)] 'org-metaup)
13348 (org-defkey org-mode-map [(meta down)] 'org-metadown)
13350 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
13351 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
13352 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
13353 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
13355 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
13356 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
13357 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
13358 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
13360 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
13361 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
13363 ;;; Extra keys for tty access.
13364 ;; We only set them when really needed because otherwise the
13365 ;; menus don't show the simple keys
13367 (when (or org-use-extra-keys
13368 (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
13369 (not window-system))
13370 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
13371 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
13372 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
13373 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
13374 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
13375 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
13376 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
13377 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
13378 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
13379 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
13380 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
13381 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
13382 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
13383 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
13384 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
13385 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
13386 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
13387 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
13388 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
13389 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
13390 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
13391 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
13393 ;; All the other keys
13395 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
13396 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
13397 (if (boundp 'narrow-map)
13398 (org-defkey narrow-map "s" 'org-narrow-to-subtree)
13399 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
13400 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
13401 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
13402 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
13403 (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
13404 (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
13405 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
13406 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
13407 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
13408 (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
13409 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
13410 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
13411 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
13412 (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
13413 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
13414 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
13415 (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
13416 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
13417 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
13418 (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
13419 (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
13420 (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
13421 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
13422 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
13423 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
13424 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
13425 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
13426 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
13427 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
13428 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
13429 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
13430 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
13431 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
13432 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
13433 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
13434 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
13435 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
13436 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
13437 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
13438 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
13439 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
13440 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
13441 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
13442 (org-defkey org-mode-map "\C-c^" 'org-sort)
13443 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
13444 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
13445 (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
13446 (org-defkey org-mode-map "\C-m" 'org-return)
13447 (org-defkey org-mode-map "\C-j" 'org-return-indent)
13448 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
13449 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
13450 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
13451 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
13452 (org-defkey org-mode-map "\C-c'" 'org-edit-special)
13453 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
13454 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
13455 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
13456 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
13457 (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
13458 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
13459 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
13460 (org-defkey org-mode-map "\C-c\C-e" 'org-export)
13461 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
13462 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
13463 (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
13465 (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
13466 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
13467 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
13468 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
13470 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
13471 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
13472 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
13473 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
13474 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
13475 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
13476 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
13477 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
13478 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
13479 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
13480 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
13481 (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
13482 (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
13484 (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
13485 (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
13486 (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
13487 (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
13489 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
13491 (define-key org-mode-map "\C-c\C-xr" 'org-reload)
13493 (when (featurep 'xemacs)
13494 (org-defkey org-mode-map 'button3 'popup-mode-menu))
13497 (defvar org-self-insert-command-undo-counter 0)
13499 (defvar org-table-auto-blank-field) ; defined in org-table.el
13500 (defun org-self-insert-command (N)
13501 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
13502 If the cursor is in a table looking at whitespace, the whitespace is
13503 overwritten, and the table is not marked as requiring realignment."
13508 ;; check if we blank the field, and if that triggers align
13509 (and (featurep 'org-table) org-table-auto-blank-field
13510 (member last-command
13511 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
13512 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
13513 ;; got extra space, this field does not determine column width
13514 (let (org-table-may-need-update) (org-table-blank-field))
13515 ;; no extra space, this field may determine column width
13516 (org-table-blank-field)))
13519 (looking-at "[^|\n]* |"))
13520 (let (org-table-may-need-update)
13521 (goto-char (1- (match-end 0)))
13522 (delete-backward-char 1)
13523 (goto-char (match-beginning 0))
13524 (self-insert-command N))
13525 (setq org-table-may-need-update t)
13526 (self-insert-command N)
13527 (org-fix-tags-on-the-fly)
13528 (if org-self-insert-cluster-for-undo
13529 (if (not (eq last-command 'org-self-insert-command))
13530 (setq org-self-insert-command-undo-counter 1)
13531 (if (>= org-self-insert-command-undo-counter 20)
13532 (setq org-self-insert-command-undo-counter 1)
13533 (and (> org-self-insert-command-undo-counter 0)
13535 (not (cadr buffer-undo-list)) ; remove nil entry
13536 (setcdr buffer-undo-list (cddr buffer-undo-list)))
13537 (setq org-self-insert-command-undo-counter
13538 (1+ org-self-insert-command-undo-counter)))))))
13540 (defun org-fix-tags-on-the-fly ()
13541 (when (and (equal (char-after (point-at-bol)) ?*)
13542 (org-on-heading-p))
13543 (org-align-tags-here org-tags-column)))
13545 (defun org-delete-backward-char (N)
13546 "Like `delete-backward-char', insert whitespace at field end in tables.
13547 When deleting backwards, in tables this function will insert whitespace in
13548 front of the next \"|\" separator, to keep the table aligned. The table will
13549 still be marked for re-alignment if the field did fill the entire column,
13550 because, in this case the deletion might narrow the column."
13552 (if (and (org-table-p)
13554 (string-match "|" (buffer-substring (point-at-bol) (point)))
13555 (looking-at ".*?|"))
13556 (let ((pos (point))
13557 (noalign (looking-at "[^|\n\r]* |"))
13558 (c org-table-may-need-update))
13559 (backward-delete-char N)
13560 (skip-chars-forward "^|")
13562 (goto-char (1- pos))
13563 ;; noalign: if there were two spaces at the end, this field
13564 ;; does not determine the width of the column.
13565 (if noalign (setq org-table-may-need-update c)))
13566 (backward-delete-char N)
13567 (org-fix-tags-on-the-fly)))
13569 (defun org-delete-char (N)
13570 "Like `delete-char', but insert whitespace at field end in tables.
13571 When deleting characters, in tables this function will insert whitespace in
13572 front of the next \"|\" separator, to keep the table aligned. The table will
13573 still be marked for re-alignment if the field did fill the entire column,
13574 because, in this case the deletion might narrow the column."
13576 (if (and (org-table-p)
13578 (not (= (char-after) ?|))
13580 (if (looking-at ".*?|")
13581 (let ((pos (point))
13582 (noalign (looking-at "[^|\n\r]* |"))
13583 (c org-table-may-need-update))
13584 (replace-match (concat
13585 (substring (match-string 0) 1 -1)
13588 ;; noalign: if there were two spaces at the end, this field
13589 ;; does not determine the width of the column.
13590 (if noalign (setq org-table-may-need-update c)))
13593 (org-fix-tags-on-the-fly)))
13595 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
13596 (put 'org-self-insert-command 'delete-selection t)
13597 (put 'orgtbl-self-insert-command 'delete-selection t)
13598 (put 'org-delete-char 'delete-selection 'supersede)
13599 (put 'org-delete-backward-char 'delete-selection 'supersede)
13600 (put 'org-yank 'delete-selection 'yank)
13602 ;; Make `flyspell-mode' delay after some commands
13603 (put 'org-self-insert-command 'flyspell-delayed t)
13604 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
13605 (put 'org-delete-char 'flyspell-delayed t)
13606 (put 'org-delete-backward-char 'flyspell-delayed t)
13608 ;; Make pabbrev-mode expand after org-mode commands
13609 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
13610 (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
13612 ;; How to do this: Measure non-white length of current string
13613 ;; If equal to column width, we should realign.
13615 (defun org-remap (map &rest commands)
13616 "In MAP, remap the functions given in COMMANDS.
13617 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
13620 (setq old (pop commands) new (pop commands))
13621 (if (fboundp 'command-remapping)
13622 (org-defkey map (vector 'remap old) new)
13623 (substitute-key-definition old new map global-map)))))
13625 (when (eq org-enable-table-editor 'optimized)
13626 ;; If the user wants maximum table support, we need to hijack
13627 ;; some standard editing functions
13628 (org-remap org-mode-map
13629 'self-insert-command 'org-self-insert-command
13630 'delete-char 'org-delete-char
13631 'delete-backward-char 'org-delete-backward-char)
13632 (org-defkey org-mode-map "|" 'org-force-self-insert))
13634 (defvar org-ctrl-c-ctrl-c-hook nil
13635 "Hook for functions attaching themselves to `C-c C-c'.
13636 This can be used to add additional functionality to the C-c C-c key which
13637 executes context-dependent commands.
13638 Each function will be called with no arguments. The function must check
13639 if the context is appropriate for it to act. If yes, it should do its
13640 thing and then return a non-nil value. If the context is wrong,
13643 (defvar org-metaleft-hook nil
13644 "Hook for functions attaching themselves to `M-left'.
13645 See `org-ctrl-c-ctrl-c-hook' for more information.")
13646 (defvar org-metaright-hook nil
13647 "Hook for functions attaching themselves to `M-right'.
13648 See `org-ctrl-c-ctrl-c-hook' for more information.")
13649 (defvar org-metaup-hook nil
13650 "Hook for functions attaching themselves to `M-up'.
13651 See `org-ctrl-c-ctrl-c-hook' for more information.")
13652 (defvar org-metadown-hook nil
13653 "Hook for functions attaching themselves to `M-down'.
13654 See `org-ctrl-c-ctrl-c-hook' for more information.")
13655 (defvar org-shiftmetaleft-hook nil
13656 "Hook for functions attaching themselves to `M-S-left'.
13657 See `org-ctrl-c-ctrl-c-hook' for more information.")
13658 (defvar org-shiftmetaright-hook nil
13659 "Hook for functions attaching themselves to `M-S-right'.
13660 See `org-ctrl-c-ctrl-c-hook' for more information.")
13661 (defvar org-shiftmetaup-hook nil
13662 "Hook for functions attaching themselves to `M-S-up'.
13663 See `org-ctrl-c-ctrl-c-hook' for more information.")
13664 (defvar org-shiftmetadown-hook nil
13665 "Hook for functions attaching themselves to `M-S-down'.
13666 See `org-ctrl-c-ctrl-c-hook' for more information.")
13667 (defvar org-metareturn-hook nil
13668 "Hook for functions attaching themselves to `M-RET'.
13669 See `org-ctrl-c-ctrl-c-hook' for more information.")
13671 (defun org-modifier-cursor-error ()
13672 "Throw an error, a modified cursor command was applied in wrong context."
13673 (error "This command is active in special context like tables, headlines or items"))
13675 (defun org-shiftselect-error ()
13676 "Throw an error because Shift-Cursor command was applied in wrong context."
13677 (if (and (boundp 'shift-select-mode) shift-select-mode)
13678 (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
13679 (error "This command works only in special context like headlines or timestamps.")))
13681 (defun org-call-for-shift-select (cmd)
13682 (let ((this-command-keys-shift-translated t))
13683 (call-interactively cmd)))
13685 (defun org-shifttab (&optional arg)
13686 "Global visibility cycling or move to previous table field.
13687 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
13689 See the individual commands for more information."
13692 ((org-at-table-p) (call-interactively 'org-table-previous-field))
13694 (message "Content view to level: %d" arg)
13695 (org-content (prefix-numeric-value arg))
13696 (setq org-cycle-global-status 'overview))
13697 (t (call-interactively 'org-global-cycle))))
13699 (defun org-shiftmetaleft ()
13700 "Promote subtree or delete table column.
13701 Calls `org-promote-subtree', `org-outdent-item',
13702 or `org-table-delete-column', depending on context.
13703 See the individual commands for more information."
13706 ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
13707 ((org-at-table-p) (call-interactively 'org-table-delete-column))
13708 ((org-on-heading-p) (call-interactively 'org-promote-subtree))
13709 ((org-at-item-p) (call-interactively 'org-outdent-item))
13710 (t (org-modifier-cursor-error))))
13712 (defun org-shiftmetaright ()
13713 "Demote subtree or insert table column.
13714 Calls `org-demote-subtree', `org-indent-item',
13715 or `org-table-insert-column', depending on context.
13716 See the individual commands for more information."
13719 ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
13720 ((org-at-table-p) (call-interactively 'org-table-insert-column))
13721 ((org-on-heading-p) (call-interactively 'org-demote-subtree))
13722 ((org-at-item-p) (call-interactively 'org-indent-item))
13723 (t (org-modifier-cursor-error))))
13725 (defun org-shiftmetaup (&optional arg)
13726 "Move subtree up or kill table row.
13727 Calls `org-move-subtree-up' or `org-table-kill-row' or
13728 `org-move-item-up' depending on context. See the individual commands
13729 for more information."
13732 ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
13733 ((org-at-table-p) (call-interactively 'org-table-kill-row))
13734 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
13735 ((org-at-item-p) (call-interactively 'org-move-item-up))
13736 (t (org-modifier-cursor-error))))
13738 (defun org-shiftmetadown (&optional arg)
13739 "Move subtree down or insert table row.
13740 Calls `org-move-subtree-down' or `org-table-insert-row' or
13741 `org-move-item-down', depending on context. See the individual
13742 commands for more information."
13745 ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
13746 ((org-at-table-p) (call-interactively 'org-table-insert-row))
13747 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
13748 ((org-at-item-p) (call-interactively 'org-move-item-down))
13749 (t (org-modifier-cursor-error))))
13751 (defun org-metaleft (&optional arg)
13752 "Promote heading or move table column to left.
13753 Calls `org-do-promote' or `org-table-move-column', depending on context.
13754 With no specific context, calls the Emacs default `backward-word'.
13755 See the individual commands for more information."
13758 ((run-hook-with-args-until-success 'org-metaleft-hook))
13759 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
13760 ((or (org-on-heading-p)
13761 (and (org-region-active-p)
13763 (goto-char (region-beginning))
13764 (org-on-heading-p))))
13765 (call-interactively 'org-do-promote))
13766 ((or (org-at-item-p)
13767 (and (org-region-active-p)
13769 (goto-char (region-beginning))
13771 (call-interactively 'org-outdent-item))
13772 (t (call-interactively 'backward-word))))
13774 (defun org-metaright (&optional arg)
13775 "Demote subtree or move table column to right.
13776 Calls `org-do-demote' or `org-table-move-column', depending on context.
13777 With no specific context, calls the Emacs default `forward-word'.
13778 See the individual commands for more information."
13781 ((run-hook-with-args-until-success 'org-metaright-hook))
13782 ((org-at-table-p) (call-interactively 'org-table-move-column))
13783 ((or (org-on-heading-p)
13784 (and (org-region-active-p)
13786 (goto-char (region-beginning))
13787 (org-on-heading-p))))
13788 (call-interactively 'org-do-demote))
13789 ((or (org-at-item-p)
13790 (and (org-region-active-p)
13792 (goto-char (region-beginning))
13794 (call-interactively 'org-indent-item))
13795 (t (call-interactively 'forward-word))))
13797 (defun org-metaup (&optional arg)
13798 "Move subtree up or move table row up.
13799 Calls `org-move-subtree-up' or `org-table-move-row' or
13800 `org-move-item-up', depending on context. See the individual commands
13801 for more information."
13804 ((run-hook-with-args-until-success 'org-metaup-hook))
13805 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
13806 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
13807 ((org-at-item-p) (call-interactively 'org-move-item-up))
13808 (t (transpose-lines 1) (beginning-of-line -1))))
13810 (defun org-metadown (&optional arg)
13811 "Move subtree down or move table row down.
13812 Calls `org-move-subtree-down' or `org-table-move-row' or
13813 `org-move-item-down', depending on context. See the individual
13814 commands for more information."
13817 ((run-hook-with-args-until-success 'org-metadown-hook))
13818 ((org-at-table-p) (call-interactively 'org-table-move-row))
13819 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
13820 ((org-at-item-p) (call-interactively 'org-move-item-down))
13821 (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
13823 (defun org-shiftup (&optional arg)
13824 "Increase item in timestamp or increase priority of current headline.
13825 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
13826 depending on context. See the individual commands for more information."
13829 ((and org-support-shift-select (org-region-active-p))
13830 (org-call-for-shift-select 'previous-line))
13831 ((org-at-timestamp-p t)
13832 (call-interactively (if org-edit-timestamp-down-means-later
13833 'org-timestamp-down 'org-timestamp-up)))
13834 ((and (not (eq org-support-shift-select 'always))
13835 (org-on-heading-p))
13836 (call-interactively 'org-priority-up))
13837 ((and (not org-support-shift-select) (org-at-item-p))
13838 (call-interactively 'org-previous-item))
13839 ((org-clocktable-try-shift 'up arg))
13840 (org-support-shift-select
13841 (org-call-for-shift-select 'previous-line))
13842 (t (org-shiftselect-error))))
13844 (defun org-shiftdown (&optional arg)
13845 "Decrease item in timestamp or decrease priority of current headline.
13846 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
13847 depending on context. See the individual commands for more information."
13850 ((and org-support-shift-select (org-region-active-p))
13851 (org-call-for-shift-select 'next-line))
13852 ((org-at-timestamp-p t)
13853 (call-interactively (if org-edit-timestamp-down-means-later
13854 'org-timestamp-up 'org-timestamp-down)))
13855 ((and (not (eq org-support-shift-select 'always))
13856 (org-on-heading-p))
13857 (call-interactively 'org-priority-down))
13858 ((and (not org-support-shift-select) (org-at-item-p))
13859 (call-interactively 'org-next-item))
13860 ((org-clocktable-try-shift 'down arg))
13861 (org-support-shift-select
13862 (org-call-for-shift-select 'next-line))
13863 (t (org-shiftselect-error))))
13865 (defun org-shiftright (&optional arg)
13866 "Cycle the thing at point or in the current line, depending on context.
13867 Depending on context, this does one of the following:
13869 - switch a timestamp at point one day into the future
13870 - on a headline, switch to the next TODO keyword.
13871 - on an item, switch entire list to the next bullet type
13872 - on a property line, switch to the next allowed value
13873 - on a clocktable definition line, move time block into the future"
13876 ((and org-support-shift-select (org-region-active-p))
13877 (org-call-for-shift-select 'forward-char))
13878 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
13879 ((and (not (eq org-support-shift-select 'always))
13880 (org-on-heading-p))
13881 (org-call-with-arg 'org-todo 'right))
13882 ((or (and org-support-shift-select
13883 (not (eq org-support-shift-select 'always))
13884 (org-at-item-bullet-p))
13885 (and (not org-support-shift-select) (org-at-item-p)))
13886 (org-call-with-arg 'org-cycle-list-bullet nil))
13887 ((and (not (eq org-support-shift-select 'always))
13888 (org-at-property-p))
13889 (call-interactively 'org-property-next-allowed-value))
13890 ((org-clocktable-try-shift 'right arg))
13891 (org-support-shift-select
13892 (org-call-for-shift-select 'forward-char))
13893 (t (org-shiftselect-error))))
13895 (defun org-shiftleft (&optional arg)
13896 "Cycle the thing at point or in the current line, depending on context.
13897 Depending on context, this does one of the following:
13899 - switch a timestamp at point one day into the past
13900 - on a headline, switch to the previous TODO keyword.
13901 - on an item, switch entire list to the previous bullet type
13902 - on a property line, switch to the previous allowed value
13903 - on a clocktable definition line, move time block into the past"
13906 ((and org-support-shift-select (org-region-active-p))
13907 (org-call-for-shift-select 'backward-char))
13908 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
13909 ((and (not (eq org-support-shift-select 'always))
13910 (org-on-heading-p))
13911 (org-call-with-arg 'org-todo 'left))
13912 ((or (and org-support-shift-select
13913 (not (eq org-support-shift-select 'always))
13914 (org-at-item-bullet-p))
13915 (and (not org-support-shift-select) (org-at-item-p)))
13916 (org-call-with-arg 'org-cycle-list-bullet 'previous))
13917 ((and (not (eq org-support-shift-select 'always))
13918 (org-at-property-p))
13919 (call-interactively 'org-property-previous-allowed-value))
13920 ((org-clocktable-try-shift 'left arg))
13921 (org-support-shift-select
13922 (org-call-for-shift-select 'backward-char))
13923 (t (org-shiftselect-error))))
13925 (defun org-shiftcontrolright ()
13926 "Switch to next TODO set."
13929 ((and org-support-shift-select (org-region-active-p))
13930 (org-call-for-shift-select 'forward-word))
13931 ((and (not (eq org-support-shift-select 'always))
13932 (org-on-heading-p))
13933 (org-call-with-arg 'org-todo 'nextset))
13934 (org-support-shift-select
13935 (org-call-for-shift-select 'forward-word))
13936 (t (org-shiftselect-error))))
13938 (defun org-shiftcontrolleft ()
13939 "Switch to previous TODO set."
13942 ((and org-support-shift-select (org-region-active-p))
13943 (org-call-for-shift-select 'backward-word))
13944 ((and (not (eq org-support-shift-select 'always))
13945 (org-on-heading-p))
13946 (org-call-with-arg 'org-todo 'previousset))
13947 (org-support-shift-select
13948 (org-call-for-shift-select 'backward-word))
13949 (t (org-shiftselect-error))))
13951 (defun org-ctrl-c-ret ()
13952 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
13955 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
13956 (t (call-interactively 'org-insert-heading))))
13958 (defun org-copy-special ()
13959 "Copy region in table or copy current subtree.
13960 Calls `org-table-copy' or `org-copy-subtree', depending on context.
13961 See the individual commands for more information."
13963 (call-interactively
13964 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
13966 (defun org-cut-special ()
13967 "Cut region in table or cut current subtree.
13968 Calls `org-table-copy' or `org-cut-subtree', depending on context.
13969 See the individual commands for more information."
13971 (call-interactively
13972 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
13974 (defun org-paste-special (arg)
13975 "Paste rectangular region into table, or past subtree relative to level.
13976 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
13977 See the individual commands for more information."
13979 (if (org-at-table-p)
13980 (org-table-paste-rectangle)
13981 (org-paste-subtree arg)))
13983 (defun org-edit-special ()
13984 "Call a special editor for the stuff at point.
13985 When at a table, call the formula editor with `org-table-edit-formulas'.
13986 When at the first line of an src example, call `org-edit-src-code'.
13987 When in an #+include line, visit the include file. Otherwise call
13988 `ffap' to visit the file at point."
13992 (call-interactively 'org-table-edit-formulas))
13994 (beginning-of-line 1)
13995 (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
13996 (find-file (org-trim (match-string 1))))
13997 ((org-edit-src-code))
13998 ((org-edit-fixed-width-region))
13999 (t (call-interactively 'ffap))))
14002 (defun org-ctrl-c-ctrl-c (&optional arg)
14003 "Set tags in headline, or update according to changed information at point.
14005 This command does many different things, depending on context:
14007 - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
14008 this is what we do.
14010 - If the cursor is in a headline, prompt for tags and insert them
14011 into the current line, aligned to `org-tags-column'. When called
14012 with prefix arg, realign all tags in the current buffer.
14014 - If the cursor is in one of the special #+KEYWORD lines, this
14015 triggers scanning the buffer for these lines and updating the
14018 - If the cursor is inside a table, realign the table. This command
14019 works even if the automatic table editor has been turned off.
14021 - If the cursor is on a #+TBLFM line, re-apply the formulas to
14024 - If the cursor is at a footnote reference or definition, jump to
14025 the corresponding definition or references, respectively.
14027 - If the cursor is a the beginning of a dynamic block, update it.
14029 - If the cursor is inside a table created by the table.el package,
14030 activate that table.
14032 - If the current buffer is a remember buffer, close note and file
14033 it. A prefix argument of 1 files to the default location
14034 without further interaction. A prefix argument of 2 files to
14035 the currently clocking task.
14037 - If the cursor is on a <<<target>>>, update radio targets and corresponding
14038 links in this buffer.
14040 - If the cursor is on a numbered item in a plain list, renumber the
14043 - If the cursor is on a checkbox, toggle it."
14045 (let ((org-enable-table-editor t))
14047 ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
14048 org-occur-highlights
14049 org-latex-fragment-image-overlays)
14050 (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
14051 (org-remove-occur-highlights)
14052 (org-remove-latex-fragment-image-overlays)
14053 (message "Temporary highlights/overlays removed from current buffer"))
14054 ((and (local-variable-p 'org-finish-function (current-buffer))
14055 (fboundp org-finish-function))
14056 (funcall org-finish-function))
14057 ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
14058 ((org-at-property-p)
14059 (call-interactively 'org-property-action))
14060 ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
14061 ((org-on-heading-p) (call-interactively 'org-set-tags))
14062 ((org-at-table.el-p)
14064 (beginning-of-line 1)
14065 (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
14066 (call-interactively 'table-recognize-table))
14068 (org-table-maybe-eval-formula)
14070 (call-interactively 'org-table-recalculate)
14071 (org-table-maybe-recalculate-line))
14072 (call-interactively 'org-table-align))
14073 ((or (org-footnote-at-reference-p)
14074 (org-footnote-at-definition-p))
14075 (call-interactively 'org-footnote-action))
14076 ((org-at-item-checkbox-p)
14077 (call-interactively 'org-toggle-checkbox))
14080 (call-interactively 'org-toggle-checkbox)
14081 (call-interactively 'org-maybe-renumber-ordered-list)))
14082 ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
14084 (beginning-of-line 1)
14085 (save-excursion (org-update-dblock)))
14086 ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
14088 ((equal (match-string 1) "TBLFM")
14089 ;; Recalculate the table before this line
14091 (beginning-of-line 1)
14092 (skip-chars-backward " \r\n\t")
14093 (if (org-at-table-p)
14094 (org-call-with-arg 'org-table-recalculate t))))
14096 ; (org-set-regexps-and-options)
14097 ; (org-restart-font-lock)
14098 (let ((org-inhibit-startup t)) (org-mode-restart))
14099 (message "Local setup has been refreshed"))))
14100 (t (error "C-c C-c can do nothing useful at this location.")))))
14102 (defun org-mode-restart ()
14103 "Restart Org-mode, to scan again for special lines.
14104 Also updates the keyword regular expressions."
14107 (message "Org-mode restarted"))
14109 (defun org-kill-note-or-show-branches ()
14110 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
14112 (if (not org-finish-function)
14113 (call-interactively 'show-branches)
14114 (let ((org-note-abort t))
14115 (funcall org-finish-function))))
14117 (defun org-return (&optional indent)
14118 "Goto next table row or insert a newline.
14119 Calls `org-table-next-row' or `newline', depending on context.
14120 See the individual commands for more information."
14123 ((bobp) (if indent (newline-and-indent) (newline)))
14125 (org-table-justify-field-maybe)
14126 (call-interactively 'org-table-next-row))
14127 ((and org-return-follows-link
14128 (eq (get-text-property (point) 'face) 'org-link))
14129 (call-interactively 'org-open-at-point))
14130 ((and (org-at-heading-p)
14132 (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
14136 (t (if indent (newline-and-indent) (newline)))))
14138 (defun org-return-indent ()
14139 "Goto next table row or insert a newline and indent.
14140 Calls `org-table-next-row' or `newline-and-indent', depending on
14141 context. See the individual commands for more information."
14145 (defun org-ctrl-c-star ()
14146 "Compute table, or change heading status of lines.
14147 Calls `org-table-recalculate' or `org-toggle-heading',
14148 depending on context."
14152 (call-interactively 'org-table-recalculate))
14154 ;; Convert all lines in region to list items
14155 (call-interactively 'org-toggle-heading))))
14157 (defun org-ctrl-c-minus ()
14158 "Insert separator line in table or modify bullet status of line.
14159 Also turns a plain line or a region of lines into list items.
14160 Calls `org-table-insert-hline', `org-toggle-item', or
14161 `org-cycle-list-bullet', depending on context."
14165 (call-interactively 'org-table-insert-hline))
14166 ((org-region-active-p)
14167 (call-interactively 'org-toggle-item))
14169 (call-interactively 'org-cycle-list-bullet))
14171 (call-interactively 'org-toggle-item))))
14173 (defun org-toggle-item ()
14174 "Convert headings or normal lines to items, items to normal lines.
14175 If there is no active region, only the current line is considered.
14177 If the first line in the region is a headline, convert all headlines to items.
14179 If the first line in the region is an item, convert all items to normal lines.
14181 If the first line is normal text, add an item bullet to each line."
14183 (let (l2 l beg end)
14184 (if (org-region-active-p)
14185 (setq beg (region-beginning) end (region-end))
14186 (setq beg (point-at-bol)
14187 end (min (1+ (point-at-eol)) (point-max))))
14190 (setq l2 (org-current-line))
14192 (beginning-of-line 1)
14193 (setq l (1- (org-current-line)))
14194 (if (org-at-item-p)
14195 ;; We already have items, de-itemize
14196 (while (< (setq l (1+ l)) l2)
14197 (when (org-at-item-p)
14198 (goto-char (match-beginning 2))
14199 (delete-region (match-beginning 2) (match-end 2))
14200 (and (looking-at "[ \t]+") (replace-match "")))
14201 (beginning-of-line 2))
14202 (if (org-on-heading-p)
14203 ;; Headings, convert to items
14204 (while (< (setq l (1+ l)) l2)
14205 (if (looking-at org-outline-regexp)
14206 (replace-match "- " t t))
14207 (beginning-of-line 2))
14208 ;; normal lines, turn them into items
14209 (while (< (setq l (1+ l)) l2)
14210 (unless (org-at-item-p)
14211 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
14212 (replace-match "\\1- \\2")))
14213 (beginning-of-line 2)))))))
14215 (defun org-toggle-heading (&optional nstars)
14216 "Convert headings to normal text, or items or text to headings.
14217 If there is no active region, only the current line is considered.
14219 If the first line is a heading, remove the stars from all headlines
14222 If the first line is a plain list item, turn all plain list items into
14225 If the first line is a normal line, turn each and every line in the region
14228 When converting a line into a heading, the number of stars is chosen
14229 such that the lines become children of the current entry. However, when
14230 a prefix argument is given, its value determines the number of stars to add."
14232 (let (l2 l itemp beg end)
14233 (if (org-region-active-p)
14234 (setq beg (region-beginning) end (region-end))
14235 (setq beg (point-at-bol)
14236 end (min (1+ (point-at-eol)) (point-max))))
14239 (setq l2 (org-current-line))
14241 (beginning-of-line 1)
14242 (setq l (1- (org-current-line)))
14243 (if (org-on-heading-p)
14244 ;; We already have headlines, de-star them
14245 (while (< (setq l (1+ l)) l2)
14246 (when (org-on-heading-p t)
14247 (and (looking-at outline-regexp) (replace-match "")))
14248 (beginning-of-line 2))
14249 (setq itemp (org-at-item-p))
14252 (make-string (prefix-numeric-value current-prefix-arg)
14255 (re-search-backward org-complex-heading-regexp nil t)
14256 (or (match-string 1) ""))))
14257 (add-stars (cond (nstars "")
14258 ((equal stars "") "*")
14259 (org-odd-levels-only "**")
14261 (rpl (concat stars add-stars " ")))
14262 (while (< (setq l (1+ l)) l2)
14264 (and (org-at-item-p) (replace-match rpl t t))
14265 (unless (org-on-heading-p)
14266 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
14267 (replace-match (concat rpl (match-string 2))))))
14268 (beginning-of-line 2)))))))
14270 (defun org-meta-return (&optional arg)
14271 "Insert a new heading or wrap a region in a table.
14272 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
14273 See the individual commands for more information."
14276 ((run-hook-with-args-until-success 'org-metareturn-hook))
14278 (call-interactively 'org-table-wrap-region))
14279 (t (call-interactively 'org-insert-heading))))
14283 ;; Define the Org-mode menus
14284 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
14286 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
14287 ["Next Field" org-cycle (org-at-table-p)]
14288 ["Previous Field" org-shifttab (org-at-table-p)]
14289 ["Next Row" org-return (org-at-table-p)]
14291 ["Blank Field" org-table-blank-field (org-at-table-p)]
14292 ["Edit Field" org-table-edit-field (org-at-table-p)]
14293 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
14296 ["Move Column Left" org-metaleft (org-at-table-p)]
14297 ["Move Column Right" org-metaright (org-at-table-p)]
14298 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
14299 ["Insert Column" org-shiftmetaright (org-at-table-p)])
14301 ["Move Row Up" org-metaup (org-at-table-p)]
14302 ["Move Row Down" org-metadown (org-at-table-p)]
14303 ["Delete Row" org-shiftmetaup (org-at-table-p)]
14304 ["Insert Row" org-shiftmetadown (org-at-table-p)]
14305 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
14307 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
14309 ["Copy Rectangle" org-copy-special (org-at-table-p)]
14310 ["Cut Rectangle" org-cut-special (org-at-table-p)]
14311 ["Paste Rectangle" org-paste-special (org-at-table-p)]
14312 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
14315 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
14316 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
14317 ["Edit Formulas" org-edit-special (org-at-table-p)]
14319 ["Recalculate line" org-table-recalculate (org-at-table-p)]
14320 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
14321 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
14323 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
14325 ["Sum Column/Rectangle" org-table-sum
14326 (or (org-at-table-p) (org-region-active-p))]
14327 ["Which Column?" org-table-current-column (org-at-table-p)])
14329 org-table-toggle-formula-debugger
14330 :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
14331 ["Show Col/Row Numbers"
14332 org-table-toggle-coordinate-overlays
14334 :selected (org-bound-and-true-p org-table-overlay-coordinates)]
14336 ["Create" org-table-create (and (not (org-at-table-p))
14337 org-enable-table-editor)]
14338 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
14339 ["Import from File" org-table-import (not (org-at-table-p))]
14340 ["Export to File" org-table-export (org-at-table-p)]
14342 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
14344 (easy-menu-define org-org-menu org-mode-map "Org menu"
14347 ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
14348 ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
14349 ["Sparse Tree..." org-sparse-tree t]
14350 ["Reveal Context" org-reveal t]
14351 ["Show All" show-all t]
14353 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
14355 ["New Heading" org-insert-heading t]
14356 ("Navigate Headings"
14357 ["Up" outline-up-heading t]
14358 ["Next" outline-next-visible-heading t]
14359 ["Previous" outline-previous-visible-heading t]
14360 ["Next Same Level" outline-forward-same-level t]
14361 ["Previous Same Level" outline-backward-same-level t]
14363 ["Jump" org-goto t])
14365 ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
14366 ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
14368 ["Copy Subtree" org-copy-special (not (org-at-table-p))]
14369 ["Cut Subtree" org-cut-special (not (org-at-table-p))]
14370 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
14372 ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
14374 ["Promote Heading" org-metaleft (not (org-at-table-p))]
14375 ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
14376 ["Demote Heading" org-metaright (not (org-at-table-p))]
14377 ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
14379 ["Sort Region/Children" org-sort (not (org-at-table-p))]
14381 ["Convert to odd levels" org-convert-to-odd-levels t]
14382 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
14384 ["Emphasis..." org-emphasize t]
14385 ["Edit Source Example" org-edit-special t]
14387 ["Footnote new/jump" org-footnote-action t]
14388 ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
14390 ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
14391 ; ["Check and Tag Children" (org-toggle-archive-tag (4))
14392 ; :active t :keys "C-u C-c C-x C-a"]
14393 ["Sparse trees open ARCHIVE trees"
14394 (setq org-sparse-tree-open-archived-trees
14395 (not org-sparse-tree-open-archived-trees))
14396 :style toggle :selected org-sparse-tree-open-archived-trees]
14397 ["Cycling opens ARCHIVE trees"
14398 (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
14399 :style toggle :selected org-cycle-open-archived-trees]
14401 ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
14402 ["Move Subtree to Archive" org-advertized-archive-subtree t]
14403 ; ["Check and Move Children" (org-archive-subtree '(4))
14404 ; :active t :keys "C-u C-c C-x C-s"]
14408 ["TODO/DONE/-" org-todo t]
14410 ["Next keyword" org-shiftright (org-on-heading-p)]
14411 ["Previous keyword" org-shiftleft (org-on-heading-p)]
14412 ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
14413 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
14414 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
14415 ["Show TODO Tree" org-show-todo-tree t]
14416 ["Global TODO list" org-todo-list t]
14418 ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
14419 :selected org-enforce-todo-dependencies :style toggle :active t]
14420 "Settings for tree at point"
14421 ["Do Children sequentially" org-toggle-ordered-property :style radio
14422 :selected (ignore-errors (org-entry-get nil "ORDERED"))
14423 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
14424 ["Do Children parallel" org-toggle-ordered-property :style radio
14425 :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
14426 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
14428 ["Set Priority" org-priority t]
14429 ["Priority Up" org-shiftup t]
14430 ["Priority Down" org-shiftdown t])
14431 ("TAGS and Properties"
14432 ["Set Tags" org-set-tags-command t]
14433 ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
14435 ["Set property" org-set-property t]
14436 ["Column view of properties" org-columns t]
14437 ["Insert Column View DBlock" org-insert-columns-dblock t])
14438 ("Dates and Scheduling"
14439 ["Timestamp" org-time-stamp t]
14440 ["Timestamp (inactive)" org-time-stamp-inactive t]
14442 ["1 Day Later" org-shiftright t]
14443 ["1 Day Earlier" org-shiftleft t]
14444 ["1 ... Later" org-shiftup t]
14445 ["1 ... Earlier" org-shiftdown t])
14446 ["Compute Time Range" org-evaluate-time-range t]
14447 ["Schedule Item" org-schedule t]
14448 ["Deadline" org-deadline t]
14450 ["Custom time format" org-toggle-time-stamp-overlays
14451 :style radio :selected org-display-custom-times]
14453 ["Goto Calendar" org-goto-calendar t]
14454 ["Date from Calendar" org-date-from-calendar t]
14456 ["Start/Restart Timer" org-timer-start t]
14457 ["Pause/Continue Timer" org-timer-pause-or-continue t]
14458 ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
14459 ["Insert Timer String" org-timer t]
14460 ["Insert Timer Item" org-timer-item t])
14462 ["Clock in" org-clock-in t]
14463 ["Clock out" org-clock-out t]
14464 ["Clock cancel" org-clock-cancel t]
14465 ["Goto running clock" org-clock-goto t]
14466 ["Display times" org-clock-display t]
14467 ["Create clock table" org-clock-report t]
14469 ["Record DONE time"
14470 (progn (setq org-log-done (not org-log-done))
14471 (message "Switching to %s will %s record a timestamp"
14472 (car org-done-keywords)
14473 (if org-log-done "automatically" "not")))
14474 :style toggle :selected org-log-done])
14476 ["Agenda Command..." org-agenda t]
14477 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
14478 ("File List for Agenda")
14479 ("Special views current file"
14480 ["TODO Tree" org-show-todo-tree t]
14481 ["Check Deadlines" org-check-deadlines t]
14482 ["Timeline" org-timeline t]
14483 ["Tags/Property tree" org-match-sparse-tree t])
14486 ["Store Link (Global)" org-store-link t]
14487 ["Insert Link" org-insert-link t]
14488 ["Follow Link" org-open-at-point t]
14490 ["Next link" org-next-link t]
14491 ["Previous link" org-previous-link t]
14493 ["Descriptive Links"
14494 (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
14496 :selected (member '(org-link) buffer-invisibility-spec)]
14499 (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
14501 :selected (not (member '(org-link) buffer-invisibility-spec))])
14503 ["Export/Publish..." org-export t]
14505 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
14506 :selected org-cdlatex-mode]
14507 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
14508 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
14509 ["Modify math symbol" org-cdlatex-math-modify
14510 (org-inside-LaTeX-fragment-p)]
14511 ["Export LaTeX fragments as images"
14512 (if (featurep 'org-exp)
14513 (setq org-export-with-LaTeX-fragments
14514 (not org-export-with-LaTeX-fragments))
14515 (require 'org-exp))
14516 :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
14517 org-export-with-LaTeX-fragments)])
14520 ["Show Version" org-version t]
14521 ["Info Documentation" org-info t])
14523 ["Browse Org Group" org-customize t]
14525 ["Expand This Menu" org-create-customize-menu
14526 (fboundp 'customize-menu-create)])
14529 ["Refresh setup current buffer" org-mode-restart t]
14530 ["Reload Org (after update)" org-reload t]
14531 ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
14534 (defun org-info (&optional node)
14535 "Read documentation for Org-mode in the info system.
14536 With optional NODE, go directly to that node."
14538 (info (format "(org)%s" (or node ""))))
14540 (defun org-install-agenda-files-menu ()
14541 (let ((bl (buffer-list)))
14544 (set-buffer (pop bl))
14545 (if (org-mode-p) (setq bl nil)))
14548 '("Org") "File List for Agenda"
14551 ["Edit File List" (org-edit-agenda-file-list) t]
14552 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
14553 ["Remove Current File from List" org-remove-file t]
14554 ["Cycle through agenda files" org-cycle-agenda-files t]
14555 ["Occur in all agenda files" org-occur-in-agenda-files t]
14557 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
14562 (defun org-require-autoloaded-modules ()
14565 '(org-agenda org-archive org-attach org-clock org-colview
14566 org-exp org-id org-export-latex org-publish
14567 org-remember org-table org-timer)))
14570 (defun org-reload (&optional uncompiled)
14571 "Reload all org lisp files.
14572 With prefix arg UNCOMPILED, load the uncompiled versions."
14574 (require 'find-func)
14575 (let* ((dir (file-name-directory (find-library-name "org")))
14576 (files (directory-files dir t "\\.el\\'"))
14577 (remove-re (concat (if (featurep 'xemacs)
14578 "org-colview" "org-colview-xemacs")
14580 (setq files (mapcar 'file-name-sans-extension files))
14581 (setq files (mapcar
14582 (lambda (x) (if (string-match remove-re x) nil x))
14584 (setq files (delq nil files))
14587 (if (and (not uncompiled)
14588 (file-exists-p (concat f ".elc")))
14589 (load (concat f ".elc") nil nil t)
14590 (load (concat f ".el") nil nil t)))
14594 (defun org-customize ()
14595 "Call the customize function with org as argument."
14597 (org-load-modules-maybe)
14598 (org-require-autoloaded-modules)
14599 (customize-browse 'org))
14601 (defun org-create-customize-menu ()
14602 "Create a full customization menu for Org-mode, insert it into the menu."
14604 (org-load-modules-maybe)
14605 (org-require-autoloaded-modules)
14606 (if (fboundp 'customize-menu-create)
14609 '("Org") "Customize"
14610 `(["Browse Org group" org-customize t]
14612 ,(customize-menu-create 'org)
14613 ["Set" Custom-set t]
14614 ["Save" Custom-save t]
14615 ["Reset to Current" Custom-reset-current t]
14616 ["Reset to Saved" Custom-reset-saved t]
14617 ["Reset to Standard Settings" Custom-reset-standard t]))
14618 (message "\"Org\"-menu now contains full customization menu"))
14619 (error "Cannot expand menu (outdated version of cus-edit.el)")))
14621 ;;;; Miscellaneous stuff
14623 ;;; Generally useful functions
14625 (defun org-find-text-property-in-string (prop s)
14626 "Return the first non-nil value of property PROP in string S."
14627 (or (get-text-property 0 prop s)
14628 (get-text-property (or (next-single-property-change 0 prop s) 0)
14631 (defun org-display-warning (message) ;; Copied from Emacs-Muse
14632 "Display the given MESSAGE as a warning."
14633 (if (fboundp 'display-warning)
14634 (display-warning 'org message
14635 (if (featurep 'xemacs)
14638 (let ((buf (get-buffer-create "*Org warnings*")))
14639 (with-current-buffer buf
14640 (goto-char (point-max))
14641 (insert "Warning (Org): " message)
14644 (display-buffer buf)
14647 (defun org-goto-marker-or-bmk (marker &optional bookmark)
14648 "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
14649 (if (and marker (marker-buffer marker)
14650 (buffer-live-p (marker-buffer marker)))
14652 (switch-to-buffer (marker-buffer marker))
14653 (if (or (> marker (point-max)) (< marker (point-min)))
14656 (org-show-context 'org-goto))
14658 (bookmark-jump bookmark)
14659 (error "Cannot find location"))))
14661 (defun org-quote-csv-field (s)
14662 "Quote field for inclusion in CSV material."
14663 (if (string-match "[\",]" s)
14664 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
14667 (defun org-plist-delete (plist property)
14668 "Delete PROPERTY from PLIST.
14669 This is in contrast to merely setting it to 0."
14672 (if (not (eq property (car plist)))
14673 (setq p (plist-put p (car plist) (nth 1 plist))))
14674 (setq plist (cddr plist)))
14677 (defun org-force-self-insert (N)
14678 "Needed to enforce self-insert under remapping."
14680 (self-insert-command N))
14682 (defun org-string-width (s)
14683 "Compute width of string, ignoring invisible characters.
14684 This ignores character with invisibility property `org-link', and also
14685 characters with property `org-cwidth', because these will become invisible
14686 upon the next fontification round."
14688 (when (or (eq t buffer-invisibility-spec)
14689 (assq 'org-link buffer-invisibility-spec))
14690 (while (setq b (text-property-any 0 (length s)
14691 'invisible 'org-link s))
14692 (setq s (concat (substring s 0 b)
14693 (substring s (or (next-single-property-change
14694 b 'invisible s) (length s)))))))
14695 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
14696 (setq s (concat (substring s 0 b)
14697 (substring s (or (next-single-property-change
14698 b 'org-cwidth s) (length s))))))
14699 (setq l (string-width s) b -1)
14700 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
14701 (setq l (- l (get-text-property b 'org-dwidth-n s))))
14704 (defun org-get-indentation (&optional line)
14705 "Get the indentation of the current line, interpreting tabs.
14706 When LINE is given, assume it represents a line and compute its indentation."
14708 (if (string-match "^ *" (org-remove-tabs line))
14711 (beginning-of-line 1)
14712 (skip-chars-forward " \t")
14713 (current-column))))
14715 (defun org-remove-tabs (s &optional width)
14716 "Replace tabulators in S with spaces.
14717 Assumes that s is a single line, starting in column 0."
14718 (setq width (or width tab-width))
14719 (while (string-match "\t" s)
14720 (setq s (replace-match
14722 (- (* width (/ (+ (match-beginning 0) width) width))
14723 (match-beginning 0)) ?\ )
14727 (defun org-fix-indentation (line ind)
14728 "Fix indentation in LINE.
14729 IND is a cons cell with target and minimum indentation.
14730 If the current indentation in LINE is smaller than the minimum,
14731 leave it alone. If it is larger than ind, set it to the target."
14732 (let* ((l (org-remove-tabs line))
14733 (i (org-get-indentation l))
14734 (i1 (car ind)) (i2 (cdr ind)))
14735 (if (>= i i2) (setq l (substring line i2)))
14737 (concat (make-string i1 ?\ ) l)
14740 (defun org-base-buffer (buffer)
14741 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
14744 (or (buffer-base-buffer buffer)
14747 (defun org-trim (s)
14748 "Remove whitespace at beginning and end of string."
14749 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
14750 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
14753 (defun org-wrap (string &optional width lines)
14754 "Wrap string to either a number of lines, or a width in characters.
14755 If WIDTH is non-nil, the string is wrapped to that width, however many lines
14756 that costs. If there is a word longer than WIDTH, the text is actually
14757 wrapped to the length of that word.
14758 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
14759 many lines, whatever width that takes.
14760 The return value is a list of lines, without newlines at the end."
14761 (let* ((words (org-split-string string "[ \t\n]+"))
14762 (maxword (apply 'max (mapcar 'org-string-width words)))
14765 (org-do-wrap words (max maxword width)))
14768 (setq ll (org-do-wrap words maxword))
14769 (if (<= (length ll) lines)
14772 (while (> (length ll) lines)
14774 (setq ll (org-do-wrap words w)))
14776 (t (error "Cannot wrap this")))))
14778 (defun org-do-wrap (words width)
14779 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
14782 (setq line (pop words))
14783 (while (and words (< (+ (length line) (length (car words))) width))
14784 (setq line (concat line " " (pop words))))
14785 (setq lines (push line lines)))
14788 (defun org-split-string (string &optional separators)
14789 "Splits STRING into substrings at SEPARATORS.
14790 No empty strings are returned if there are matches at the beginning
14791 and end of string."
14792 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
14796 (while (and (string-match rexp string
14798 (= start (match-beginning 0))
14799 (< start (length string)))
14801 (< (match-beginning 0) (length string)))
14803 (or (eq (match-beginning 0) 0)
14804 (and (eq (match-beginning 0) (match-end 0))
14805 (eq (match-beginning 0) start))
14807 (cons (substring string start (match-beginning 0))
14809 (setq start (match-end 0)))
14810 (or (eq start (length string))
14812 (cons (substring string start)
14816 (defun org-uuidgen-p (s)
14817 "Is S an ID created by UUIDGEN?"
14818 (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
14820 (defun org-context ()
14821 "Return a list of contexts of the current cursor position.
14822 If several contexts apply, all are returned.
14823 Each context entry is a list with a symbol naming the context, and
14824 two positions indicating start and end of the context. Possible
14827 :headline anywhere in a headline
14828 :headline-stars on the leading stars in a headline
14829 :todo-keyword on a TODO keyword (including DONE) in a headline
14830 :tags on the TAGS in a headline
14831 :priority on the priority cookie in a headline
14832 :item on the first line of a plain list item
14833 :item-bullet on the bullet/number of a plain list item
14834 :checkbox on the checkbox in a plain list item
14835 :table in an org-mode table
14836 :table-special on a special filed in a table
14837 :table-table in a table.el table
14838 :link on a hyperlink
14839 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
14840 :target on a <<target>>
14841 :radio-target on a <<<radio-target>>>
14842 :latex-fragment on a LaTeX fragment
14843 :latex-preview on a LaTeX fragment with overlayed preview image
14845 This function expects the position to be visible because it uses font-lock
14846 faces as a help to recognize the following contexts: :table-special, :link,
14848 (let* ((f (get-text-property (point) 'face))
14849 (faces (if (listp f) f (list f)))
14850 (p (point)) clist o)
14851 ;; First the large context
14853 ((org-on-heading-p t)
14854 (push (list :headline (point-at-bol) (point-at-eol)) clist)
14856 (beginning-of-line 1)
14857 (looking-at org-todo-line-tags-regexp))
14858 (push (org-point-in-group p 1 :headline-stars) clist)
14859 (push (org-point-in-group p 2 :todo-keyword) clist)
14860 (push (org-point-in-group p 4 :tags) clist))
14862 (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
14863 (if (looking-at "\\[#[A-Z0-9]\\]")
14864 (push (org-point-in-group p 0 :priority) clist)))
14867 (push (org-point-in-group p 2 :item-bullet) clist)
14868 (push (list :item (point-at-bol)
14869 (save-excursion (org-end-of-item) (point)))
14871 (and (org-at-item-checkbox-p)
14872 (push (org-point-in-group p 0 :checkbox) clist)))
14875 (push (list :table (org-table-begin) (org-table-end)) clist)
14876 (if (memq 'org-formula faces)
14877 (push (list :table-special
14878 (previous-single-property-change p 'face)
14879 (next-single-property-change p 'face)) clist)))
14880 ((org-at-table-p 'any)
14881 (push (list :table-table) clist)))
14884 ;; Now the small context
14886 ((org-at-timestamp-p)
14887 (push (org-point-in-group p 0 :timestamp) clist))
14888 ((memq 'org-link faces)
14890 (previous-single-property-change p 'face)
14891 (next-single-property-change p 'face)) clist))
14892 ((memq 'org-special-keyword faces)
14893 (push (list :keyword
14894 (previous-single-property-change p 'face)
14895 (next-single-property-change p 'face)) clist))
14897 (push (org-point-in-group p 0 :target) clist)
14898 (goto-char (1- (match-beginning 0)))
14899 (if (looking-at org-radio-target-regexp)
14900 (push (org-point-in-group p 0 :radio-target) clist))
14902 ((setq o (car (delq nil
14905 (if (memq x org-latex-fragment-image-overlays) x))
14906 (org-overlays-at (point))))))
14907 (push (list :latex-fragment
14908 (org-overlay-start o) (org-overlay-end o)) clist)
14909 (push (list :latex-preview
14910 (org-overlay-start o) (org-overlay-end o)) clist))
14911 ((org-inside-LaTeX-fragment-p)
14912 ;; FIXME: positions wrong.
14913 (push (list :latex-fragment (point) (point)) clist)))
14915 (setq clist (nreverse (delq nil clist)))
14918 ;; FIXME: Compare with at-regexp-p Do we need both?
14919 (defun org-in-regexp (re &optional nlines visually)
14920 "Check if point is inside a match of regexp.
14921 Normally only the current line is checked, but you can include NLINES extra
14922 lines both before and after point into the search.
14923 If VISUALLY is set, require that the cursor is not after the match but
14924 really on, so that the block visually is on the match."
14926 (let ((pos (point))
14927 (eol (point-at-eol (+ 1 (or nlines 0))))
14928 (inc (if visually 1 0)))
14930 (beginning-of-line (- 1 (or nlines 0)))
14931 (while (re-search-forward re eol t)
14932 (if (and (<= (match-beginning 0) pos)
14933 (>= (+ inc (match-end 0)) pos))
14934 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
14936 (defun org-at-regexp-p (regexp)
14937 "Is point inside a match of REGEXP in the current line?"
14940 (let ((pos (point)) (end (point-at-eol)))
14941 (beginning-of-line 1)
14942 (while (re-search-forward regexp end t)
14943 (if (and (<= (match-beginning 0) pos)
14944 (>= (match-end 0) pos))
14948 (defun org-occur-in-agenda-files (regexp &optional nlines)
14949 "Call `multi-occur' with buffers for all agenda files."
14950 (interactive "sOrg-files matching: \np")
14951 (let* ((files (org-agenda-files))
14952 (tnames (mapcar 'file-truename files))
14953 (extra org-agenda-text-search-extra-files)
14955 (when (eq (car extra) 'agenda-archives)
14956 (setq extra (cdr extra))
14957 (setq files (org-add-archive-files files)))
14958 (while (setq f (pop extra))
14959 (unless (member (file-truename f) tnames)
14960 (add-to-list 'files f 'append)
14961 (add-to-list 'tnames (file-truename f) 'append)))
14963 (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
14966 (if (boundp 'occur-mode-find-occurrence-hook)
14968 (add-hook 'occur-mode-find-occurrence-hook
14973 (defadvice occur-mode-goto-occurrence
14974 (after org-occur-reveal activate)
14975 (and (org-mode-p) (org-reveal)))
14976 (defadvice occur-mode-goto-occurrence-other-window
14977 (after org-occur-reveal activate)
14978 (and (org-mode-p) (org-reveal)))
14979 (defadvice occur-mode-display-occurrence
14980 (after org-occur-reveal activate)
14982 (let ((pos (occur-mode-find-occurrence)))
14983 (with-current-buffer (marker-buffer pos)
14988 (defun org-uniquify (list)
14989 "Remove duplicate elements from LIST."
14991 (mapc (lambda (x) (add-to-list 'res x 'append)) list)
14994 (defun org-delete-all (elts list)
14995 "Remove all elements in ELTS from LIST."
14997 (setq list (delete (pop elts) list)))
15000 (defun org-back-over-empty-lines ()
15001 "Move backwards over whitespace, to the beginning of the first empty line.
15002 Returns the number of empty lines passed."
15003 (let ((pos (point)))
15004 (skip-chars-backward " \t\n\r")
15005 (beginning-of-line 2)
15006 (goto-char (min (point) pos))
15007 (count-lines (point) pos)))
15009 (defun org-skip-whitespace ()
15010 (skip-chars-forward " \t\n\r"))
15012 (defun org-point-in-group (point group &optional context)
15013 "Check if POINT is in match-group GROUP.
15014 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
15015 match. If the match group does ot exist or point is not inside it,
15017 (and (match-beginning group)
15018 (>= point (match-beginning group))
15019 (<= point (match-end group))
15021 (list context (match-beginning group) (match-end group))
15024 (defun org-switch-to-buffer-other-window (&rest args)
15025 "Switch to buffer in a second window on the current frame.
15026 In particular, do not allow pop-up frames."
15027 (let (pop-up-frames special-display-buffer-names special-display-regexps
15028 special-display-function)
15029 (apply 'switch-to-buffer-other-window args)))
15031 (defun org-combine-plists (&rest plists)
15032 "Create a single property list from all plists in PLISTS.
15033 The process starts by copying the first list, and then setting properties
15034 from the other lists. Settings in the last list are the most significant
15035 ones and overrule settings in the other lists."
15036 (let ((rtn (copy-sequence (pop plists)))
15039 (setq ls (pop plists))
15041 (setq p (pop ls) v (pop ls))
15042 (setq rtn (plist-put rtn p v))))
15045 (defun org-move-line-down (arg)
15046 "Move the current line down. With prefix argument, move it past ARG lines."
15048 (let ((col (current-column))
15050 (beginning-of-line 1) (setq beg (point))
15051 (beginning-of-line 2) (setq end (point))
15052 (beginning-of-line (+ 1 arg))
15053 (setq pos (move-marker (make-marker) (point)))
15054 (insert (delete-and-extract-region beg end))
15056 (org-move-to-column col)))
15058 (defun org-move-line-up (arg)
15059 "Move the current line up. With prefix argument, move it past ARG lines."
15061 (let ((col (current-column))
15063 (beginning-of-line 1) (setq beg (point))
15064 (beginning-of-line 2) (setq end (point))
15065 (beginning-of-line (- arg))
15066 (setq pos (move-marker (make-marker) (point)))
15067 (insert (delete-and-extract-region beg end))
15069 (org-move-to-column col)))
15071 (defun org-replace-escapes (string table)
15072 "Replace %-escapes in STRING with values in TABLE.
15073 TABLE is an association list with keys like \"%a\" and string values.
15074 The sequences in STRING may contain normal field width and padding information,
15075 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
15076 so values can contain further %-escapes if they are define later in TABLE."
15077 (let ((case-fold-search nil)
15079 (while (setq e (pop table))
15080 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
15081 (while (string-match re string)
15082 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
15084 (setq string (replace-match rpl t t string))))
15088 (defun org-sublist (list start end)
15089 "Return a section of LIST, from START to END.
15090 Counting starts at 1."
15091 (let (rtn (c start))
15092 (setq list (nthcdr (1- start) list))
15093 (while (and list (<= c end))
15094 (push (pop list) rtn)
15098 (defun org-find-base-buffer-visiting (file)
15099 "Like `find-buffer-visiting' but alway return the base buffer and
15100 not an indirect buffer."
15101 (let ((buf (find-buffer-visiting file)))
15103 (or (buffer-base-buffer buf) buf)
15106 (defun org-image-file-name-regexp (&optional extensions)
15107 "Return regexp matching the file names of images.
15108 If EXTENSIONS is given, only match these."
15109 (if (and (not extensions) (fboundp 'image-file-name-regexp))
15110 (image-file-name-regexp)
15111 (let ((image-file-name-extensions
15113 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
15114 "xbm" "xpm" "pbm" "pgm" "ppm"))))
15116 (regexp-opt (nconc (mapcar 'upcase
15117 image-file-name-extensions)
15118 image-file-name-extensions)
15122 (defun org-file-image-p (file &optional extensions)
15123 "Return non-nil if FILE is an image."
15125 (string-match (org-image-file-name-regexp extensions) file)))
15127 (defun org-get-cursor-date ()
15128 "Return the date at cursor in as a time.
15129 This works in the calendar and in the agenda, anywhere else it just
15130 returns the current time."
15131 (let (date day defd)
15133 ((eq major-mode 'calendar-mode)
15134 (setq date (calendar-cursor-to-date)
15135 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
15136 ((eq major-mode 'org-agenda-mode)
15137 (setq day (get-text-property (point) 'day))
15139 (setq date (calendar-gregorian-from-absolute day)
15140 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
15142 (or defd (current-time))))
15144 (defvar org-agenda-action-marker (make-marker)
15145 "Marker pointing to the entry for the next agenda action.")
15147 (defun org-mark-entry-for-agenda-action ()
15148 "Mark the current entry as target of an agenda action.
15149 Agenda actions are actions executed from the agenda with the key `k',
15150 which make use of the date at the cursor."
15152 (move-marker org-agenda-action-marker
15153 (save-excursion (org-back-to-heading t) (point))
15156 "Entry marked for action; press `k' at desired date in agenda or calendar"))
15158 ;;; Paragraph filling stuff.
15159 ;; We want this to be just right, so use the full arsenal.
15161 (defun org-indent-line-function ()
15162 "Indent line like previous, but further if previous was headline or item."
15164 (let* ((pos (point))
15165 (itemp (org-at-item-p))
15166 (org-drawer-regexp (or org-drawer-regexp "\000"))
15167 column bpos bcol tpos tcol bullet btype bullet-type)
15168 ;; Find the previous relevant line
15169 (beginning-of-line 1)
15171 ((looking-at "#") (setq column 0))
15172 ((looking-at "\\*+ ") (setq column 0))
15173 ((and (looking-at "[ \t]*:END:")
15174 (save-excursion (re-search-backward org-drawer-regexp nil t)))
15176 (goto-char (1- (match-beginning 1)))
15177 (setq column (current-column))))
15179 (beginning-of-line 0)
15180 (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
15181 (not (looking-at "[ \t]*:END:"))
15182 (not (looking-at org-drawer-regexp)))
15183 (beginning-of-line 0))
15185 ((looking-at "\\*+[ \t]+")
15186 (if (not org-adapt-indentation)
15188 (goto-char (match-end 0))
15189 (setq column (current-column))))
15190 ((looking-at org-drawer-regexp)
15191 (goto-char (1- (match-beginning 1)))
15192 (setq column (current-column)))
15193 ((looking-at "\\([ \t]*\\):END:")
15194 (goto-char (match-end 1))
15195 (setq column (current-column)))
15197 (org-beginning-of-item)
15198 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
15199 (setq bpos (match-beginning 1) tpos (match-end 0)
15200 bcol (progn (goto-char bpos) (current-column))
15201 tcol (progn (goto-char tpos) (current-column))
15202 bullet (match-string 1)
15203 bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
15204 (if (> tcol (+ bcol org-description-max-indent))
15205 (setq tcol (+ bcol 5)))
15209 (beginning-of-line 1)
15210 (if (looking-at "\\S-")
15212 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
15213 (setq bullet (match-string 1)
15214 btype (if (string-match "[0-9]" bullet) "n" bullet))
15215 (setq column (if (equal btype bullet-type) bcol tcol)))
15216 (setq column (org-get-indentation)))))
15217 (t (setq column (org-get-indentation))))))
15219 (if (<= (current-column) (current-indentation))
15220 (org-indent-line-to column)
15221 (save-excursion (org-indent-line-to column)))
15222 (setq column (current-column))
15223 (beginning-of-line 1)
15225 "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
15226 (replace-match (concat "\\1" (format org-property-format
15227 (match-string 2) (match-string 3)))
15229 (org-move-to-column column)))
15231 (defun org-set-autofill-regexps ()
15233 ;; In the paragraph separator we include headlines, because filling
15234 ;; text in a line directly attached to a headline would otherwise
15235 ;; fill the headline as well.
15236 (org-set-local 'comment-start-skip "^#+[ \t]*")
15237 (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
15238 ;; The paragraph starter includes hand-formatted lists.
15239 (org-set-local 'paragraph-start
15240 "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
15241 ;; Inhibit auto-fill for headers, tables and fixed-width lines.
15242 ;; But only if the user has not turned off tables or fixed-width regions
15244 'auto-fill-inhibit-regexp
15245 (concat "\\*+ \\|#\\+"
15246 "\\|[ \t]*" org-keyword-time-regexp
15247 (if (or org-enable-table-editor org-enable-fixed-width-editor)
15250 (if org-enable-table-editor "|" "")
15251 (if org-enable-fixed-width-editor ":" "")
15253 ;; We use our own fill-paragraph function, to make sure that tables
15254 ;; and fixed-width regions are not wrapped. That function will pass
15255 ;; through to `fill-paragraph' when appropriate.
15256 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
15257 ; Adaptive filling: To get full control, first make sure that
15258 ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
15259 (org-set-local 'adaptive-fill-regexp "\000")
15260 (org-set-local 'adaptive-fill-function
15261 'org-adaptive-fill-function)
15263 'align-mode-rules-list
15264 '((org-in-buffer-settings
15265 (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
15266 (modes . '(org-mode))))))
15268 (defun org-fill-paragraph (&optional justify)
15269 "Re-align a table, pass through to fill-paragraph if no table."
15270 (let ((table-p (org-at-table-p))
15271 (table.el-p (org-at-table.el-p)))
15272 (cond ((and (equal (char-after (point-at-bol)) ?*)
15273 (save-excursion (goto-char (point-at-bol))
15274 (looking-at outline-regexp)))
15275 t) ; skip headlines
15276 (table.el-p t) ; skip table.el tables
15277 (table-p (org-table-align) t) ; align org-mode tables
15278 (t nil)))) ; call paragraph-fill
15280 ;; For reference, this is the default value of adaptive-fill-regexp
15281 ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
15283 (defun org-adaptive-fill-function ()
15284 "Return a fill prefix for org-mode files.
15285 In particular, this makes sure hanging paragraphs for hand-formatted lists
15287 (cond ((looking-at "#[ \t]+")
15289 ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
15291 (if (> (match-end 1) (+ (match-beginning 1)
15292 org-description-max-indent))
15293 (goto-char (+ (match-beginning 1) 5))
15294 (goto-char (match-end 0)))
15295 (make-string (current-column) ?\ )))
15296 ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
15298 (goto-char (match-end 0))
15299 (make-string (current-column) ?\ )))
15304 (defun org-toggle-fixed-width-section (arg)
15305 "Toggle the fixed-width export.
15306 If there is no active region, the QUOTE keyword at the current headline is
15307 inserted or removed. When present, it causes the text between this headline
15308 and the next to be exported as fixed-width text, and unmodified.
15309 If there is an active region, this command adds or removes a colon as the
15310 first character of this line. If the first character of a line is a colon,
15311 this line is also exported in fixed-width font."
15314 (regionp (org-region-active-p))
15315 (beg (if regionp (region-beginning) (point)))
15316 (end (if regionp (region-end)))
15317 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
15318 (case-fold-search nil)
15319 (re "[ \t]*\\(: \\)")
15324 (setq cc (current-column))
15325 (beginning-of-line 1)
15326 (setq off (looking-at re))
15327 (while (> nlines 0)
15328 (setq nlines (1- nlines))
15329 (beginning-of-line 1)
15332 (org-move-to-column cc t)
15335 ((and off (looking-at re))
15336 (replace-match "" t t nil 1))
15337 ((not off) (org-move-to-column cc t) (insert ": ")))
15340 (org-back-to-heading)
15341 (if (looking-at (concat outline-regexp
15342 "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
15343 (replace-match "" t t nil 1)
15344 (if (looking-at outline-regexp)
15346 (goto-char (match-end 0))
15347 (insert org-quote-string " "))))))))
15349 ;;;; Functions extending outline functionality
15351 (defun org-beginning-of-line (&optional arg)
15352 "Go to the beginning of the current line. If that is invisible, continue
15353 to a visible line beginning. This makes the function of C-a more intuitive.
15354 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
15355 first attempt, and only move to after the tags when the cursor is already
15356 beyond the end of the headline."
15358 (let ((pos (point))
15359 (special (if (consp org-special-ctrl-a/e)
15360 (car org-special-ctrl-a/e)
15361 org-special-ctrl-a/e))
15363 (beginning-of-line 1)
15364 (if (and arg (fboundp 'move-beginning-of-line))
15365 (call-interactively 'move-beginning-of-line)
15369 (if (org-invisible-p)
15370 (while (and (not (bobp)) (org-invisible-p))
15372 (beginning-of-line 1))
15373 (forward-char 1))))
15376 ((and (looking-at org-complex-heading-regexp)
15377 (= (char-after (match-end 1)) ?\ ))
15378 (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
15382 (cond ((> pos refpos) refpos)
15383 ((= pos (point)) refpos)
15385 (cond ((> pos (point)) (point))
15386 ((not (eq last-command this-command)) (point))
15391 (cond ((> pos (match-end 4)) (match-end 4))
15392 ((= pos (point)) (match-end 4))
15394 (cond ((> pos (point)) (point))
15395 ((not (eq last-command this-command)) (point))
15396 (t (match-end 4))))))))
15398 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
15400 (defun org-end-of-line (&optional arg)
15401 "Go to the end of the line.
15402 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
15403 first attempt, and only move to after the tags when the cursor is already
15404 beyond the end of the headline."
15406 (let ((special (if (consp org-special-ctrl-a/e)
15407 (cdr org-special-ctrl-a/e)
15408 org-special-ctrl-a/e)))
15409 (if (or (not special)
15410 (not (org-on-heading-p))
15412 (call-interactively (if (fboundp 'move-end-of-line)
15415 (let ((pos (point)))
15416 (beginning-of-line 1)
15417 (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
15419 (if (or (< pos (match-beginning 1))
15420 (= pos (match-end 0)))
15421 (goto-char (match-beginning 1))
15422 (goto-char (match-end 0)))
15423 (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
15424 (goto-char (match-end 0))
15425 (goto-char (match-beginning 1))))
15426 (call-interactively (if (fboundp 'move-end-of-line)
15430 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
15432 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
15433 (define-key org-mode-map "\C-e" 'org-end-of-line)
15435 (defun org-kill-line (&optional arg)
15436 "Kill line, to tags or end of line."
15439 ((or (not org-special-ctrl-k)
15441 (not (org-on-heading-p)))
15442 (call-interactively 'kill-line))
15443 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
15444 (kill-region (point) (match-beginning 1))
15445 (org-set-tags nil t))
15446 (t (kill-region (point) (point-at-eol)))))
15448 (define-key org-mode-map "\C-k" 'org-kill-line)
15450 (defun org-yank (&optional arg)
15451 "Yank. If the kill is a subtree, treat it specially.
15452 This command will look at the current kill and check if is a single
15453 subtree, or a series of subtrees[1]. If it passes the test, and if the
15454 cursor is at the beginning of a line or after the stars of a currently
15455 empty headline, then the yank is handled specially. How exactly depends
15456 on the value of the following variables, both set by default.
15458 org-yank-folded-subtrees
15459 When set, the subtree(s) will be folded after insertion, but only
15460 if doing so would now swallow text after the yanked text.
15462 org-yank-adjusted-subtrees
15463 When set, the subtree will be promoted or demoted in order to
15464 fit into the local outline tree structure, which means that the level
15465 will be adjusted so that it becomes the smaller one of the two
15466 *visible* surrounding headings.
15468 Any prefix to this command will cause `yank' to be called directly with
15469 no special treatment. In particular, a simple `C-u' prefix will just
15470 plainly yank the text as it is.
15472 \[1] Basically, the test checks if the first non-white line is a heading
15473 and if there are no other headings with fewer stars."
15475 (setq this-command 'yank)
15477 (call-interactively 'yank)
15478 (let ((subtreep ; is kill a subtree, and the yank position appropriate?
15479 (and (org-kill-is-subtree-p)
15481 (and (looking-at "[ \t]*$")
15484 (buffer-substring (point-at-bol) (point)))))))
15487 ((and subtreep org-yank-folded-subtrees)
15488 (let ((beg (point))
15490 (if (and subtreep org-yank-adjusted-subtrees)
15491 (org-paste-subtree nil nil 'for-yank)
15492 (call-interactively 'yank))
15495 (when (and (bolp) subtreep
15496 (not (setq swallowp
15497 (org-yank-folding-would-swallow-text beg end))))
15498 (or (looking-at outline-regexp)
15499 (re-search-forward (concat "^" outline-regexp) end t))
15500 (while (and (< (point) end) (looking-at outline-regexp))
15502 (org-cycle-show-empty-lines 'folded)
15503 (condition-case nil
15504 (outline-forward-same-level 1)
15505 (error (goto-char end)))))
15508 "Yanked text not folded because that would swallow text"))
15510 (skip-chars-forward " \t\n\r")
15511 (beginning-of-line 1)
15512 (push-mark beg 'nomsg)))
15513 ((and subtreep org-yank-adjusted-subtrees)
15514 (let ((beg (point-at-bol)))
15515 (org-paste-subtree nil nil 'for-yank)
15516 (push-mark beg 'nomsg)))
15518 (call-interactively 'yank))))))
15520 (defun org-yank-folding-would-swallow-text (beg end)
15521 "Would hide-subtree at BEG swallow any text after END?"
15525 (when (or (looking-at outline-regexp)
15526 (re-search-forward (concat "^" outline-regexp) end t))
15527 (setq level (org-outline-level)))
15529 (skip-chars-forward " \t\r\n\v\f")
15531 (and (bolp) (looking-at org-outline-regexp)
15532 (<= (org-outline-level) level)))
15533 nil ; Nothing would be swallowed
15534 t)))) ; something would swallow
15536 (define-key org-mode-map "\C-y" 'org-yank)
15538 (defun org-invisible-p ()
15539 "Check if point is at a character currently not visible."
15540 ;; Early versions of noutline don't have `outline-invisible-p'.
15541 (if (fboundp 'outline-invisible-p)
15542 (outline-invisible-p)
15543 (get-char-property (point) 'invisible)))
15545 (defun org-invisible-p2 ()
15546 "Check if point is at a character currently not visible."
15548 (if (and (eolp) (not (bobp))) (backward-char 1))
15549 ;; Early versions of noutline don't have `outline-invisible-p'.
15550 (if (fboundp 'outline-invisible-p)
15551 (outline-invisible-p)
15552 (get-char-property (point) 'invisible))))
15554 (defun org-back-to-heading (&optional invisible-ok)
15555 "Call `outline-back-to-heading', but provide a better error message."
15556 (condition-case nil
15557 (outline-back-to-heading invisible-ok)
15558 (error (error "Before first headline at position %d in buffer %s"
15559 (point) (current-buffer)))))
15561 (defun org-before-first-heading-p ()
15562 "Before first heading?"
15564 (null (re-search-backward "^\\*+ " nil t))))
15566 (defalias 'org-on-heading-p 'outline-on-heading-p)
15567 (defalias 'org-at-heading-p 'outline-on-heading-p)
15568 (defun org-at-heading-or-item-p ()
15569 (or (org-on-heading-p) (org-at-item-p)))
15571 (defun org-on-target-p ()
15572 (or (org-in-regexp org-radio-target-regexp)
15573 (org-in-regexp org-target-regexp)))
15575 (defun org-up-heading-all (arg)
15576 "Move to the heading line of which the present line is a subheading.
15577 This function considers both visible and invisible heading lines.
15578 With argument, move up ARG levels."
15579 (if (fboundp 'outline-up-heading-all)
15580 (outline-up-heading-all arg) ; emacs 21 version of outline.el
15581 (outline-up-heading arg t))) ; emacs 22 version of outline.el
15583 (defun org-up-heading-safe ()
15584 "Move to the heading line of which the present line is a subheading.
15585 This version will not throw an error. It will return the level of the
15586 headline found, or nil if no higher level is found."
15587 (let (start-level re)
15588 (org-back-to-heading t)
15589 (setq start-level (funcall outline-level))
15590 (if (equal start-level 1)
15592 (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
15593 (if (re-search-backward re nil t)
15594 (funcall outline-level)))))
15596 (defun org-first-sibling-p ()
15597 "Is this heading the first child of its parents?"
15599 (let ((re (concat "^" outline-regexp))
15601 (unless (org-at-heading-p t)
15602 (error "Not at a heading"))
15603 (setq level (funcall outline-level))
15605 (if (not (re-search-backward re nil t))
15607 (setq l (funcall outline-level))
15610 (defun org-goto-sibling (&optional previous)
15611 "Goto the next sibling, even if it is invisible.
15612 When PREVIOUS is set, go to the previous sibling instead. Returns t
15613 when a sibling was found. When none is found, return nil and don't
15615 (let ((fun (if previous 're-search-backward 're-search-forward))
15617 (re (concat "^" outline-regexp))
15619 (when (condition-case nil (org-back-to-heading t) (error nil))
15620 (setq level (funcall outline-level))
15622 (or previous (forward-char 1))
15623 (while (funcall fun re nil t)
15624 (setq l (funcall outline-level))
15625 (when (< l level) (goto-char pos) (throw 'exit nil))
15626 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
15630 (defun org-show-siblings ()
15631 "Show all siblings of the current headline."
15633 (while (org-goto-sibling) (org-flag-heading nil)))
15635 (while (org-goto-sibling 'previous)
15636 (org-flag-heading nil))))
15638 (defun org-show-hidden-entry ()
15639 "Show an entry where even the heading is hidden."
15643 (defun org-flag-heading (flag &optional entry)
15644 "Flag the current heading. FLAG non-nil means make invisible.
15645 When ENTRY is non-nil, show the entire entry."
15647 (org-back-to-heading t)
15648 ;; Check if we should show the entire entry
15653 (and (outline-next-heading)
15654 (org-flag-heading nil))))
15655 (outline-flag-region (max (point-min) (1- (point)))
15656 (save-excursion (outline-end-of-heading) (point))
15659 (defun org-forward-same-level (arg)
15660 "Move forward to the ARG'th subheading at same level as this one.
15661 Stop at the first and last subheadings of a superior heading.
15662 This is like outline-forward-same-level, but invisible headings are ok."
15664 (org-back-to-heading t)
15666 (let ((point-to-move-to (save-excursion
15667 (org-get-next-sibling))))
15668 (if point-to-move-to
15670 (goto-char point-to-move-to)
15671 (setq arg (1- arg)))
15674 (error "No following same-level heading"))))))
15676 (defun org-get-next-sibling ()
15677 "Move to next heading of the same level, and return point.
15678 If there is no such heading, return nil.
15679 This is like outline-next-sibling, but invisible headings are ok."
15680 (let ((level (funcall outline-level)))
15681 (outline-next-heading)
15682 (while (and (not (eobp)) (> (funcall outline-level) level))
15683 (outline-next-heading))
15684 (if (or (eobp) (< (funcall outline-level) level))
15688 (defun org-end-of-subtree (&optional invisible-OK to-heading)
15689 ;; This is an exact copy of the original function, but it uses
15690 ;; `org-back-to-heading', to make it work also in invisible
15691 ;; trees. And is uses an invisible-OK argument.
15692 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
15693 (org-back-to-heading invisible-OK)
15695 (level (funcall outline-level)))
15696 (while (and (not (eobp))
15697 (or first (> (funcall outline-level) level)))
15699 (outline-next-heading))
15701 (if (memq (preceding-char) '(?\n ?\^M))
15703 ;; Go to end of line before heading
15705 (if (memq (preceding-char) '(?\n ?\^M))
15706 ;; leave blank line before heading
15707 (forward-char -1))))))
15710 (defun org-show-subtree ()
15711 "Show everything after this heading at deeper levels."
15712 (outline-flag-region
15715 (outline-end-of-subtree) (outline-next-heading) (point))
15718 (defun org-show-entry ()
15719 "Show the body directly following this heading.
15720 Show the heading too, if it is currently invisible."
15723 (condition-case nil
15725 (org-back-to-heading t)
15726 (outline-flag-region
15727 (max (point-min) (1- (point)))
15729 (if (re-search-forward
15730 (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
15731 (match-beginning 1)
15736 (defun org-make-options-regexp (kwds &optional extra)
15737 "Make a regular expression for keyword lines."
15741 (mapconcat 'regexp-quote kwds "\\|")
15742 (if extra (concat "\\|" extra))
15746 ;; Make isearch reveal the necessary context
15747 (defun org-isearch-end ()
15748 "Reveal context after isearch exits."
15749 (when isearch-success ; only if search was successful
15750 (if (featurep 'xemacs)
15751 ;; Under XEmacs, the hook is run in the correct place,
15752 ;; we directly show the context.
15753 (org-show-context 'isearch)
15754 ;; In Emacs the hook runs *before* restoring the overlays.
15755 ;; So we have to use a one-time post-command-hook to do this.
15756 ;; (Emacs 22 has a special variable, see function `org-mode')
15757 (unless (and (boundp 'isearch-mode-end-hook-quit)
15758 isearch-mode-end-hook-quit)
15759 ;; Only when the isearch was not quitted.
15760 (org-add-hook 'post-command-hook 'org-isearch-post-command
15761 'append 'local)))))
15763 (defun org-isearch-post-command ()
15764 "Remove self from hook, and show context."
15765 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
15766 (org-show-context 'isearch))
15769 ;;;; Integration with and fixes for other packages
15773 (defvar org-imenu-markers nil
15774 "All markers currently used by Imenu.")
15775 (make-variable-buffer-local 'org-imenu-markers)
15777 (defun org-imenu-new-marker (&optional pos)
15778 "Return a new marker for use by Imenu, and remember the marker."
15779 (let ((m (make-marker)))
15780 (move-marker m (or pos (point)))
15781 (push m org-imenu-markers)
15784 (defun org-imenu-get-tree ()
15785 "Produce the index for Imenu."
15786 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
15787 (setq org-imenu-markers nil)
15788 (let* ((n org-imenu-depth)
15789 (re (concat "^" outline-regexp))
15790 (subs (make-vector (1+ n) nil))
15796 (goto-char (point-max))
15797 (while (re-search-backward re nil t)
15798 (setq level (org-reduced-level (funcall outline-level)))
15800 (looking-at org-complex-heading-regexp)
15801 (setq head (org-link-display-format
15802 (org-match-string-no-properties 4))
15803 m (org-imenu-new-marker))
15804 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
15805 (if (>= level last-level)
15806 (push (cons head m) (aref subs level))
15807 (push (cons head (aref subs (1+ level))) (aref subs level))
15808 (loop for i from (1+ level) to n do (aset subs i nil)))
15809 (setq last-level level)))))
15812 (eval-after-load "imenu"
15814 (add-hook 'imenu-after-jump-hook
15816 (if (eq major-mode 'org-mode)
15817 (org-show-context 'org-goto))))))
15819 (defun org-link-display-format (link)
15820 "Replace a link with either the description, or the link target
15821 if no description is present"
15823 (if (string-match org-bracket-link-analytic-regexp link)
15824 (replace-match (or (match-string 5 link)
15825 (concat (match-string 1 link)
15826 (match-string 3 link)))
15830 ;; Speedbar support
15832 (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
15833 "Overlay marking the agenda restriction line in speedbar.")
15834 (org-overlay-put org-speedbar-restriction-lock-overlay
15835 'face 'org-agenda-restriction-lock)
15836 (org-overlay-put org-speedbar-restriction-lock-overlay
15837 'help-echo "Agendas are currently limited to this item.")
15838 (org-detach-overlay org-speedbar-restriction-lock-overlay)
15840 (defun org-speedbar-set-agenda-restriction ()
15841 "Restrict future agenda commands to the location at point in speedbar.
15842 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
15844 (require 'org-agenda)
15845 (let (p m tp np dir txt)
15847 ((setq p (text-property-any (point-at-bol) (point-at-eol)
15849 (setq m (get-text-property p 'org-imenu-marker))
15852 (set-buffer (marker-buffer m))
15854 (org-agenda-set-restriction-lock 'subtree))))
15855 ((setq p (text-property-any (point-at-bol) (point-at-eol)
15856 'speedbar-function 'speedbar-find-file))
15857 (setq tp (previous-single-property-change
15858 (1+ p) 'speedbar-function)
15859 np (next-single-property-change
15860 tp 'speedbar-function)
15861 dir (speedbar-line-directory)
15862 txt (buffer-substring-no-properties (or tp (point-min))
15863 (or np (point-max))))
15866 (set-buffer (find-file-noselect
15867 (let ((default-directory dir))
15868 (expand-file-name txt))))
15869 (unless (org-mode-p)
15870 (error "Cannot restrict to non-Org-mode file"))
15871 (org-agenda-set-restriction-lock 'file))))
15872 (t (error "Don't know how to restrict Org-mode's agenda")))
15873 (org-move-overlay org-speedbar-restriction-lock-overlay
15874 (point-at-bol) (point-at-eol))
15875 (setq current-prefix-arg nil)
15876 (org-agenda-maybe-redo)))
15878 (eval-after-load "speedbar"
15880 (speedbar-add-supported-extension ".org")
15881 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
15882 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
15883 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
15884 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
15885 (add-hook 'speedbar-visiting-tag-hook
15886 (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
15889 ;;; Fixes and Hacks for problems with other packages
15891 ;; Make flyspell not check words in links, to not mess up our keymap
15892 (defun org-mode-flyspell-verify ()
15893 "Don't let flyspell put overlays at active buttons."
15894 (not (get-text-property (point) 'keymap)))
15896 ;; Make `bookmark-jump' show the jump location if it was hidden.
15897 (eval-after-load "bookmark"
15898 '(if (boundp 'bookmark-after-jump-hook)
15899 ;; We can use the hook
15900 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
15901 ;; Hook not available, use advice
15902 (defadvice bookmark-jump (after org-make-visible activate)
15903 "Make the position visible."
15904 (org-bookmark-jump-unhide))))
15906 ;; Make sure saveplace show the location if it was hidden
15907 (eval-after-load "saveplace"
15908 '(defadvice save-place-find-file-hook (after org-make-visible activate)
15909 "Make the position visible."
15910 (org-bookmark-jump-unhide)))
15912 (defun org-bookmark-jump-unhide ()
15913 "Unhide the current position, to show the bookmark location."
15915 (or (org-invisible-p)
15916 (save-excursion (goto-char (max (point-min) (1- (point))))
15917 (org-invisible-p)))
15918 (org-show-context 'bookmark-jump)))
15920 ;; Make session.el ignore our circular variable
15921 (eval-after-load "session"
15922 '(add-to-list 'session-globals-exclude 'org-mark-ring))
15924 ;;;; Experimental code
15926 (defun org-closed-in-range ()
15927 "Sparse tree of items closed in a certain time range.
15928 Still experimental, may disappear in the future."
15930 ;; Get the time interval from the user.
15931 (let* ((time1 (time-to-seconds
15932 (org-read-date nil 'to-time nil "Starting date: ")))
15933 (time2 (time-to-seconds
15934 (org-read-date nil 'to-time nil "End date:")))
15935 ;; callback function
15936 (callback (lambda ()
15939 (apply 'encode-time
15940 (org-parse-time-string
15941 (match-string 1))))))
15942 ;; check if time in interval
15943 (and (>= time time1) (<= time time2))))))
15944 ;; make tree, check each match with the callback
15945 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
15952 (run-hooks 'org-load-hook)
15954 ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
15956 ;;; org.el ends here