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
))
92 (require 'org-footnote
)
94 ;;;; Customization variables
98 (defconst org-version
"6.28trans"
99 "The version number of the file org.el.")
101 (defun org-version (&optional here
)
102 "Show the org-mode version in the echo area.
103 With prefix arg HERE, insert it at point."
105 (let ((version (format "Org-mode version %s" org-version
)))
110 ;;; Compatibility constants
112 ;;; The custom variables
115 "Outline-based notes management and organizer."
121 (defcustom org-load-hook nil
122 "Hook that is run after org.el has been loaded."
126 (defvar org-modules
) ; defined below
127 (defvar org-modules-loaded nil
128 "Have the modules been loaded already?")
130 (defun org-load-modules-maybe (&optional force
)
131 "Load all extensions listed in `org-modules'."
132 (when (or force
(not org-modules-loaded
))
134 (condition-case nil
(require ext
)
135 (error (message "Problems while trying to load feature `%s'" ext
))))
137 (setq org-modules-loaded t
)))
139 (defun org-set-modules (var value
)
140 "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
142 (when (featurep 'org
)
143 (org-load-modules-maybe 'force
)))
145 (when (org-bound-and-true-p org-modules
)
146 (let ((a (member 'org-infojs org-modules
)))
147 (and a
(setcar a
'org-jsinfo
))))
149 (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
)
150 "Modules that should always be loaded together with org.el.
151 If a description starts with <C>, the file is not part of Emacs
152 and loading it will require that you have downloaded and properly installed
153 the org-mode distribution.
155 You can also use this system to load external packages (i.e. neither Org
156 core modules, not modules from the CONTRIB directory). Just add symbols
157 to the end of the list. If the package is called org-xyz.el, then you need
158 to add the symbol `xyz', and the package must have a call to
162 :set
'org-set-modules
165 (const :tag
" bbdb: Links to BBDB entries" org-bbdb
)
166 (const :tag
" bibtex: Links to BibTeX entries" org-bibtex
)
167 (const :tag
" gnus: Links to GNUS folders/messages" org-gnus
)
168 (const :tag
" id: Global IDs for identifying entries" org-id
)
169 (const :tag
" info: Links to Info nodes" org-info
)
170 (const :tag
" jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo
)
171 (const :tag
" inlinetask: Tasks independent of outline hierarchy" org-inlinetask
)
172 (const :tag
" irc: Links to IRC/ERC chat sessions" org-irc
)
173 (const :tag
" mac-message: Links to messages in Apple Mail" org-mac-message
)
174 (const :tag
" mew Links to Mew folders/messages" org-mew
)
175 (const :tag
" mhe: Links to MHE folders/messages" org-mhe
)
176 (const :tag
" protocol: Intercept calls from emacsclient" org-protocol
)
177 (const :tag
" rmail: Links to RMAIL folders/messages" org-rmail
)
178 (const :tag
" vm: Links to VM folders/messages" org-vm
)
179 (const :tag
" wl: Links to Wanderlust folders/messages" org-wl
)
180 (const :tag
" w3m: Special cut/paste from w3m to Org." org-w3m
)
181 (const :tag
" mouse: Additional mouse support" org-mouse
)
183 (const :tag
"C annotate-file: Annotate a file with org syntax" org-annotate-file
)
184 (const :tag
"C annotation-helper: Call Remember directly from Browser (OBSOLETE, use org-protocol)" org-annotation-helper
)
185 (const :tag
"C bookmark: Org links to bookmarks" org-bookmark
)
186 (const :tag
"C browser-url: Store link, directly from Browser (OBSOLETE, use org-protocol)" org-browser-url
)
187 (const :tag
"C checklist: Extra functions for checklists in repeated tasks" org-checklist
)
188 (const :tag
"C choose: Use TODO keywords to mark decisions states" org-choose
)
189 (const :tag
"C collector: Collect properties into tables" org-collector
)
190 (const :tag
"C depend: TODO dependencies for Org-mode (PARTIALLY OBSOLETE, see built-in dependency support))" org-depend
)
191 (const :tag
"C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol
)
192 (const :tag
"C eval: Include command output as text" org-eval
)
193 (const :tag
"C eval-light: Evaluate inbuffer-code on demand" org-eval-light
)
194 (const :tag
"C expiry: Expiry mechanism for Org entries" org-expiry
)
195 (const :tag
"C exp-bibtex: Export citations using BibTeX" org-exp-bibtex
)
196 (const :tag
"C interactive-query: Interactive modification of tags query (PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query
)
197 (const :tag
"C jira Add a jira:ticket protocol to Org" org-jira
)
198 (const :tag
"C mairix: Hook mairix search into Org for different MUAs" org-mairix
)
199 (const :tag
"C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal
)
200 (const :tag
"C man: Support for links to manpages in Org-mode" org-man
)
201 (const :tag
"C mtags: Support for muse-like tags" org-mtags
)
202 (const :tag
"C panel: Simple routines for us with bad memory" org-panel
)
203 (const :tag
"C R: Computation using the R language" org-R
)
204 (const :tag
"C registry: A registry for Org links" org-registry
)
205 (const :tag
"C org2rem: Convert org appointments into reminders" org2rem
)
206 (const :tag
"C screen: Visit screen sessions through Org-mode links" org-screen
)
207 (const :tag
"C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks
)
208 (const :tag
"C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert
)
209 (const :tag
"C toc: Table of contents for Org-mode buffer" org-toc
)
210 (repeat :tag
"External packages" :inline t
(symbol :tag
"Package"))))
212 (defcustom org-support-shift-select nil
213 "Non-nil means, make shift-cursor commands select text when possible.
215 In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
216 selecting a region, or enlarge thusly regions started in this way.
217 In Org-mode, in special contexts, these same keys are used for other
218 purposes, important enough to compete with shift selection. Org tries
219 to balance these needs by supporting `shift-select-mode' outside these
220 special contexts, under control of this variable.
222 The default of this variable is nil, to avoid confusing behavior. Shifted
223 cursor keys will then execute Org commands in the following contexts:
224 - on a headline, changing TODO state (left/right) and priority (up/down)
225 - on a time stamp, changing the time
226 - in a plain list item, changing the bullet type
227 - in a property definition line, switching between allowed values
228 - in the BEGIN line of a clock table (changing the time block).
229 Outside these contexts, the commands will throw an error.
231 When this variable is t and the cursor is not in a special context,
232 Org-mode will support shift-selection for making and enlarging regions.
233 To make this more effective, the bullet cycling will no longer happen
234 anywhere in an item line, but only if the cursor is exactly on the bullet.
236 If you set this variable to the symbol `always', then the keys
237 will not be special in headlines, property lines, and item lines, to make
238 shift selection work there as well. If this is what you want, you can
239 use the following alternative commands: `C-c C-t' and `C-c ,' to
240 change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
241 TODO sets, `C-c -' to cycle item bullet types, and properties can be
242 edited by hand or in column view.
244 However, when the cursor is on a timestamp, shift-cursor commands
245 will still edit the time stamp - this is just too good to give up.
247 XEmacs user should have this variable set to nil, because shift-select-mode
251 (const :tag
"Never" nil
)
252 (const :tag
"When outside special context" t
)
253 (const :tag
"Everywhere except timestamps" always
)))
255 (defgroup org-startup nil
256 "Options concerning startup of Org-mode."
260 (defcustom org-startup-folded t
261 "Non-nil means, entering Org-mode will switch to OVERVIEW.
262 This can also be configured on a per-file basis by adding one of
263 the following lines anywhere in the buffer:
270 (const :tag
"nofold: show all" nil
)
271 (const :tag
"fold: overview" t
)
272 (const :tag
"content: all headlines" content
)))
274 (defcustom org-startup-truncated t
275 "Non-nil means, entering Org-mode will set `truncate-lines'.
276 This is useful since some lines containing links can be very long and
277 uninteresting. Also tables look terrible when wrapped."
281 (defcustom org-startup-indented nil
282 "Non-nil means, turn on `org-indent-mode' on startup.
283 This can also be configured on a per-file basis by adding one of
284 the following lines anywhere in the buffer:
288 :group
'org-structure
290 (const :tag
"Not" nil
)
291 (const :tag
"Globally (slow on startup in large files)" t
)))
293 (defcustom org-startup-align-all-tables nil
294 "Non-nil means, align all tables when visiting a file.
295 This is useful when the column width in tables is forced with <N> cookies
296 in table fields. Such tables will look correct only after the first re-align.
297 This can also be configured on a per-file basis by adding one of
298 the following lines anywhere in the buffer:
304 (defcustom org-insert-mode-line-in-empty-file nil
305 "Non-nil means insert the first line setting Org-mode in empty files.
306 When the function `org-mode' is called interactively in an empty file, this
307 normally means that the file name does not automatically trigger Org-mode.
308 To ensure that the file will always be in Org-mode in the future, a
309 line enforcing Org-mode will be inserted into the buffer, if this option
314 (defcustom org-replace-disputed-keys nil
315 "Non-nil means use alternative key bindings for some keys.
316 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
317 These keys are also used by other packages like shift-selection-mode'
318 \(built into Emacs 23), `CUA-mode' or `windmove.el'.
319 If you want to use Org-mode together with one of these other modes,
320 or more generally if you would like to move some Org-mode commands to
321 other keys, set this variable and configure the keys with the variable
324 This option is only relevant at load-time of Org-mode, and must be set
325 *before* org.el is loaded. Changing it requires a restart of Emacs to
330 (defcustom org-use-extra-keys nil
331 "Non-nil means use extra key sequence definitions for certain
332 commands. This happens automatically if you run XEmacs or if
333 window-system is nil. This variable lets you do the same
334 manually. You must set it before loading org.
336 Example: on Carbon Emacs 22 running graphically, with an external
337 keyboard on a Powerbook, the default way of setting M-left might
338 not work for either Alt or ESC. Setting this variable will make
343 (if (fboundp 'defvaralias
)
344 (defvaralias 'org-CUA-compatible
'org-replace-disputed-keys
))
346 (defcustom org-disputed-keys
347 '(([(shift up
)] .
[(meta p
)])
348 ([(shift down
)] .
[(meta n
)])
349 ([(shift left
)] .
[(meta -
)])
350 ([(shift right
)] .
[(meta +)])
351 ([(control shift right
)] .
[(meta shift
+)])
352 ([(control shift left
)] .
[(meta shift -
)]))
353 "Keys for which Org-mode and other modes compete.
354 This is an alist, cars are the default keys, second element specifies
355 the alternative to use when `org-replace-disputed-keys' is t.
357 Keys can be specified in any syntax supported by `define-key'.
358 The value of this option takes effect only at Org-mode's startup,
359 therefore you'll have to restart Emacs to apply it after changing."
364 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
365 Or return the original if not disputed."
366 (if org-replace-disputed-keys
367 (let* ((nkey (key-description key
))
368 (x (org-find-if (lambda (x)
369 (equal (key-description (car x
)) nkey
))
374 (defun org-find-if (predicate seq
)
377 (if (funcall predicate
(car seq
))
378 (throw 'exit
(car seq
))
381 (defun org-defkey (keymap key def
)
382 "Define a key, possibly translated, as returned by `org-key'."
383 (define-key keymap
(org-key key
) def
))
385 (defcustom org-ellipsis nil
386 "The ellipsis to use in the Org-mode outline.
387 When nil, just use the standard three dots. When a string, use that instead,
388 When a face, use the standard 3 dots, but with the specified face.
389 The change affects only Org-mode (which will then use its own display table).
390 Changing this requires executing `M-x org-mode' in a buffer to become
393 :type
'(choice (const :tag
"Default" nil
)
394 (face :tag
"Face" :value org-warning
)
395 (string :tag
"String" :value
"...#")))
397 (defvar org-display-table nil
398 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
400 (defgroup org-keywords nil
401 "Keywords in Org-mode."
405 (defcustom org-deadline-string
"DEADLINE:"
406 "String to mark deadline entries.
407 A deadline is this string, followed by a time stamp. Should be a word,
408 terminated by a colon. You can insert a schedule keyword and
409 a timestamp with \\[org-deadline].
410 Changes become only effective after restarting Emacs."
414 (defcustom org-scheduled-string
"SCHEDULED:"
415 "String to mark scheduled TODO entries.
416 A schedule is this string, followed by a time stamp. Should be a word,
417 terminated by a colon. You can insert a schedule keyword and
418 a timestamp with \\[org-schedule].
419 Changes become only effective after restarting Emacs."
423 (defcustom org-closed-string
"CLOSED:"
424 "String used as the prefix for timestamps logging closing a TODO entry."
428 (defcustom org-clock-string
"CLOCK:"
429 "String used as prefix for timestamps clocking work hours on an item."
433 (defcustom org-comment-string
"COMMENT"
434 "Entries starting with this keyword will never be exported.
435 An entry can be toggled between COMMENT and normal with
436 \\[org-toggle-comment].
437 Changes become only effective after restarting Emacs."
441 (defcustom org-quote-string
"QUOTE"
442 "Entries starting with this keyword will be exported in fixed-width font.
443 Quoting applies only to the text in the entry following the headline, and does
444 not extend beyond the next headline, even if that is lower level.
445 An entry can be toggled between QUOTE and normal with
446 \\[org-toggle-fixed-width-section]."
450 (defconst org-repeat-re
451 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
452 "Regular expression for specifying repeated events.
453 After a match, group 1 contains the repeat expression.")
455 (defgroup org-structure nil
456 "Options concerning the general structure of Org-mode files."
460 (defgroup org-reveal-location nil
461 "Options about how to make context of a location visible."
462 :tag
"Org Reveal Location"
463 :group
'org-structure
)
465 (defconst org-context-choice
467 (const :tag
"Always" t
)
468 (const :tag
"Never" nil
)
469 (repeat :greedy t
:tag
"Individual contexts"
471 (choice :tag
"Context"
478 (const bookmark-jump
)
482 "Contexts for the reveal options.")
484 (defcustom org-show-hierarchy-above
'((default . t
))
485 "Non-nil means, show full hierarchy when revealing a location.
486 Org-mode often shows locations in an org-mode file which might have
487 been invisible before. When this is set, the hierarchy of headings
488 above the exposed location is shown.
489 Turning this off for example for sparse trees makes them very compact.
490 Instead of t, this can also be an alist specifying this option for different
491 contexts. Valid contexts are
492 agenda when exposing an entry from the agenda
493 org-goto when using the command `org-goto' on key C-c C-j
494 occur-tree when using the command `org-occur' on key C-c /
495 tags-tree when constructing a sparse tree based on tags matches
496 link-search when exposing search matches associated with a link
497 mark-goto when exposing the jump goal of a mark
498 bookmark-jump when exposing a bookmark location
499 isearch when exiting from an incremental search
500 default default for all contexts not set explicitly"
501 :group
'org-reveal-location
502 :type org-context-choice
)
504 (defcustom org-show-following-heading
'((default . nil
))
505 "Non-nil means, show following heading when revealing a location.
506 Org-mode often shows locations in an org-mode file which might have
507 been invisible before. When this is set, the heading following the
509 Turning this off for example for sparse trees makes them very compact,
510 but makes it harder to edit the location of the match. In such a case,
511 use the command \\[org-reveal] to show more context.
512 Instead of t, this can also be an alist specifying this option for different
513 contexts. See `org-show-hierarchy-above' for valid contexts."
514 :group
'org-reveal-location
515 :type org-context-choice
)
517 (defcustom org-show-siblings
'((default . nil
) (isearch t
))
518 "Non-nil means, show all sibling heading when revealing a location.
519 Org-mode often shows locations in an org-mode file which might have
520 been invisible before. When this is set, the sibling of the current entry
521 heading are all made visible. If `org-show-hierarchy-above' is t,
522 the same happens on each level of the hierarchy above the current entry.
524 By default this is on for the isearch context, off for all other contexts.
525 Turning this off for example for sparse trees makes them very compact,
526 but makes it harder to edit the location of the match. In such a case,
527 use the command \\[org-reveal] to show more context.
528 Instead of t, this can also be an alist specifying this option for different
529 contexts. See `org-show-hierarchy-above' for valid contexts."
530 :group
'org-reveal-location
531 :type org-context-choice
)
533 (defcustom org-show-entry-below
'((default . nil
))
534 "Non-nil means, show the entry below a headline when revealing a location.
535 Org-mode often shows locations in an org-mode file which might have
536 been invisible before. When this is set, the text below the headline that is
537 exposed is also shown.
539 By default this is off for all contexts.
540 Instead of t, this can also be an alist specifying this option for different
541 contexts. See `org-show-hierarchy-above' for valid contexts."
542 :group
'org-reveal-location
543 :type org-context-choice
)
545 (defcustom org-indirect-buffer-display
'other-window
546 "How should indirect tree buffers be displayed?
547 This applies to indirect buffers created with the commands
548 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
550 current-window Display in the current window
551 other-window Just display in another window.
552 dedicated-frame Create one new frame, and re-use it each time.
553 new-frame Make a new frame each time. Note that in this case
554 previously-made indirect buffers are kept, and you need to
555 kill these buffers yourself."
556 :group
'org-structure
557 :group
'org-agenda-windows
559 (const :tag
"In current window" current-window
)
560 (const :tag
"In current frame, other window" other-window
)
561 (const :tag
"Each time a new frame" new-frame
)
562 (const :tag
"One dedicated frame" dedicated-frame
)))
564 (defgroup org-cycle nil
565 "Options concerning visibility cycling in Org-mode."
567 :group
'org-structure
)
569 (defcustom org-cycle-skip-children-state-if-no-children t
570 "Non-nil means, skip CHILDREN state in entries that don't have any."
574 (defcustom org-cycle-max-level nil
575 "Maximum level which should still be subject to visibility cycling.
576 Levels higher than this will, for cycling, be treated as text, not a headline.
577 When `org-odd-levels-only' is set, a value of N in this variable actually
578 means 2N-1 stars as the limiting headline.
579 When nil, cycle all levels.
580 Note that the limiting level of cycling is also influenced by
581 `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
582 `org-inlinetask-min-level' is, cycling will be limited to levels one less
586 (const :tag
"No limit" nil
)
587 (integer :tag
"Maximum level")))
589 (defcustom org-drawers
'("PROPERTIES" "CLOCK" "LOGBOOK")
590 "Names of drawers. Drawers are not opened by cycling on the headline above.
591 Drawers only open with a TAB on the drawer line itself. A drawer looks like
596 The drawer \"PROPERTIES\" is special for capturing properties through
599 Drawers can be defined on the per-file basis with a line like:
601 #+DRAWERS: HIDDEN STATE PROPERTIES"
602 :group
'org-structure
604 :type
'(repeat (string :tag
"Drawer Name")))
606 (defcustom org-hide-block-startup nil
607 "Non-nil means, , entering Org-mode will fold all blocks.
608 This can also be set in on a per-file basis with
610 #+STARTUP: hideblocks
611 #+STARTUP: showblocks"
616 (defcustom org-cycle-global-at-bob nil
617 "Cycle globally if cursor is at beginning of buffer and not at a headline.
618 This makes it possible to do global cycling without having to use S-TAB or
619 C-u TAB. For this special case to work, the first line of the buffer
620 must not be a headline - it may be empty or some other text. When used in
621 this way, `org-cycle-hook' is disables temporarily, to make sure the
622 cursor stays at the beginning of the buffer.
623 When this option is nil, don't do anything special at the beginning
628 (defcustom org-cycle-emulate-tab t
629 "Where should `org-cycle' emulate TAB.
631 white Only in completely white lines
632 whitestart Only at the beginning of lines, before the first non-white char
633 t Everywhere except in headlines
634 exc-hl-bol Everywhere except at the start of a headline
635 If TAB is used in a place where it does not emulate TAB, the current subtree
636 visibility is cycled."
638 :type
'(choice (const :tag
"Never" nil
)
639 (const :tag
"Only in completely white lines" white
)
640 (const :tag
"Before first char in a line" whitestart
)
641 (const :tag
"Everywhere except in headlines" t
)
642 (const :tag
"Everywhere except at bol in headlines" exc-hl-bol
)
645 (defcustom org-cycle-separator-lines
2
646 "Number of empty lines needed to keep an empty line between collapsed trees.
647 If you leave an empty line between the end of a subtree and the following
648 headline, this empty line is hidden when the subtree is folded.
649 Org-mode will leave (exactly) one empty line visible if the number of
650 empty lines is equal or larger to the number given in this variable.
651 So the default 2 means, at least 2 empty lines after the end of a subtree
652 are needed to produce free space between a collapsed subtree and the
655 Special case: when 0, never leave empty lines in collapsed view."
658 (put 'org-cycle-separator-lines
'safe-local-variable
'integerp
)
660 (defcustom org-pre-cycle-hook nil
661 "Hook that is run before visibility cycling is happening.
662 The function(s) in this hook must accept a single argument which indicates
663 the new state that will be set right after running this hook. The
664 argument is a symbol. Before a global state change, it can have the values
665 `overview', `content', or `all'. Before a local state change, it can have
666 the values `folded', `children', or `subtree'."
670 (defcustom org-cycle-hook
'(org-cycle-hide-archived-subtrees
671 org-cycle-hide-drawers
672 org-cycle-show-empty-lines
673 org-optimize-window-after-visibility-change
)
674 "Hook that is run after `org-cycle' has changed the buffer visibility.
675 The function(s) in this hook must accept a single argument which indicates
676 the new state that was set by the most recent `org-cycle' command. The
677 argument is a symbol. After a global state change, it can have the values
678 `overview', `content', or `all'. After a local state change, it can have
679 the values `folded', `children', or `subtree'."
683 (defgroup org-edit-structure nil
684 "Options concerning structure editing in Org-mode."
685 :tag
"Org Edit Structure"
686 :group
'org-structure
)
688 (defcustom org-odd-levels-only nil
689 "Non-nil means, skip even levels and only use odd levels for the outline.
690 This has the effect that two stars are being added/taken away in
691 promotion/demotion commands. It also influences how levels are
692 handled by the exporters.
693 Changing it requires restart of `font-lock-mode' to become effective
694 for fontification also in regions already fontified.
695 You may also set this on a per-file basis by adding one of the following
700 :group
'org-edit-structure
701 :group
'org-font-lock
704 (defcustom org-adapt-indentation t
705 "Non-nil means, adapt indentation to outline node level.
707 When this variable is set, Org assumes that you write outlines by
708 indenting text in each node to align with the headline (after the stars).
709 The following issues are influenced by this variable:
711 - When this is set and the *entire* text in an entry is indented, the
712 indentation is increased by one space in a demotion command, and
713 decreased by one in a promotion command. If any line in the entry
714 body starts with text at column 0, indentation is not changed at all.
716 - Property drawers and planning information is inserted indented when
717 this variable s set. When nil, they will not be indented.
719 - TAB indents a line relative to context. The lines below a headline
720 will be indented when this variable is set.
722 Note that this is all about true indentation, by adding and removing
723 space characters. See also `org-indent.el' which does level-dependent
724 indentation in a virtual way, i.e. at display time in Emacs."
725 :group
'org-edit-structure
728 (defcustom org-special-ctrl-a
/e nil
729 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
731 When t, `C-a' will bring back the cursor to the beginning of the
732 headline text, i.e. after the stars and after a possible TODO keyword.
733 In an item, this will be the position after the bullet.
734 When the cursor is already at that position, another `C-a' will bring
735 it to the beginning of the line.
737 `C-e' will jump to the end of the headline, ignoring the presence of tags
738 in the headline. A second `C-e' will then jump to the true end of the
739 line, after any tags.
741 When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
742 going to the true line boundary first. Only a directly following, identical
743 keypress will bring the cursor to the special positions.
745 This may also be a cons cell where the behavior for `C-a' and `C-e' is
747 :group
'org-edit-structure
749 (const :tag
"off" nil
)
750 (const :tag
"after stars/bullet and before tags first" t
)
751 (const :tag
"true line boundary first" reversed
)
752 (cons :tag
"Set C-a and C-e separately"
753 (choice :tag
"Special C-a"
754 (const :tag
"off" nil
)
755 (const :tag
"after stars/bullet first" t
)
756 (const :tag
"before stars/bullet first" reversed
))
757 (choice :tag
"Special C-e"
758 (const :tag
"off" nil
)
759 (const :tag
"before tags first" t
)
760 (const :tag
"after tags first" reversed
)))))
761 (if (fboundp 'defvaralias
)
762 (defvaralias 'org-special-ctrl-a
'org-special-ctrl-a
/e
))
764 (defcustom org-special-ctrl-k nil
765 "Non-nil means `C-k' will behave specially in headlines.
766 When nil, `C-k' will call the default `kill-line' command.
767 When t, the following will happen while the cursor is in the headline:
769 - When the cursor is at the beginning of a headline, kill the entire
770 line and possible the folded subtree below the line.
771 - When in the middle of the headline text, kill the headline up to the tags.
772 - When after the headline text, kill the tags."
773 :group
'org-edit-structure
776 (defcustom org-yank-folded-subtrees t
777 "Non-nil means, when yanking subtrees, fold them.
778 If the kill is a single subtree, or a sequence of subtrees, i.e. if
779 it starts with a heading and all other headings in it are either children
780 or siblings, then fold all the subtrees. However, do this only if no
781 text after the yank would be swallowed into a folded tree by this action."
782 :group
'org-edit-structure
785 (defcustom org-yank-adjusted-subtrees nil
786 "Non-nil means, when yanking subtrees, adjust the level.
787 With this setting, `org-paste-subtree' is used to insert the subtree, see
788 this function for details."
789 :group
'org-edit-structure
792 (defcustom org-M-RET-may-split-line
'((default . t
))
793 "Non-nil means, M-RET will split the line at the cursor position.
794 When nil, it will go to the end of the line before making a
796 You may also set this option in a different way for different
797 contexts. Valid contexts are:
799 headline when creating a new headline
800 item when creating a new item
801 table in a table field
802 default the value to be used for all contexts not explicitly
804 :group
'org-structure
807 (const :tag
"Always" t
)
808 (const :tag
"Never" nil
)
809 (repeat :greedy t
:tag
"Individual contexts"
811 (choice :tag
"Context"
819 (defcustom org-insert-heading-respect-content nil
820 "Non-nil means, insert new headings after the current subtree.
821 When nil, the new heading is created directly after the current line.
822 The commands \\[org-insert-heading-respect-content] and
823 \\[org-insert-todo-heading-respect-content] turn this variable on
824 for the duration of the command."
825 :group
'org-structure
828 (defcustom org-blank-before-new-entry
'((heading . auto
)
829 (plain-list-item . auto
))
830 "Should `org-insert-heading' leave a blank line before new heading/item?
831 The value is an alist, with `heading' and `plain-list-item' as car,
832 and a boolean flag as cdr. For plain lists, if the variable
833 `org-empty-line-terminates-plain-lists' is set, the setting here
834 is ignored and no empty line is inserted, to keep the list in tact."
835 :group
'org-edit-structure
837 (cons (const heading
)
838 (choice (const :tag
"Never" nil
)
839 (const :tag
"Always" t
)
840 (const :tag
"Auto" auto
)))
841 (cons (const plain-list-item
)
842 (choice (const :tag
"Never" nil
)
843 (const :tag
"Always" t
)
844 (const :tag
"Auto" auto
)))))
846 (defcustom org-insert-heading-hook nil
847 "Hook being run after inserting a new heading."
848 :group
'org-edit-structure
851 (defcustom org-enable-fixed-width-editor t
852 "Non-nil means, lines starting with \":\" are treated as fixed-width.
853 This currently only means, they are never auto-wrapped.
854 When nil, such lines will be treated like ordinary lines.
855 See also the QUOTE keyword."
856 :group
'org-edit-structure
860 (defcustom org-goto-auto-isearch t
861 "Non-nil means, typing characters in org-goto starts incremental search."
862 :group
'org-edit-structure
865 (defgroup org-sparse-trees nil
866 "Options concerning sparse trees in Org-mode."
867 :tag
"Org Sparse Trees"
868 :group
'org-structure
)
870 (defcustom org-highlight-sparse-tree-matches t
871 "Non-nil means, highlight all matches that define a sparse tree.
872 The highlights will automatically disappear the next time the buffer is
873 changed by an edit command."
874 :group
'org-sparse-trees
877 (defcustom org-remove-highlights-with-change t
878 "Non-nil means, any change to the buffer will remove temporary highlights.
879 Such highlights are created by `org-occur' and `org-clock-display'.
880 When nil, `C-c C-c needs to be used to get rid of the highlights.
881 The highlights created by `org-preview-latex-fragment' always need
882 `C-c C-c' to be removed."
883 :group
'org-sparse-trees
888 (defcustom org-occur-hook
'(org-first-headline-recenter)
889 "Hook that is run after `org-occur' has constructed a sparse tree.
890 This can be used to recenter the window to show as much of the structure
892 :group
'org-sparse-trees
895 (defgroup org-imenu-and-speedbar nil
896 "Options concerning imenu and speedbar in Org-mode."
897 :tag
"Org Imenu and Speedbar"
898 :group
'org-structure
)
900 (defcustom org-imenu-depth
2
901 "The maximum level for Imenu access to Org-mode headlines.
902 This also applied for speedbar access."
903 :group
'org-imenu-and-speedbar
906 (defgroup org-table nil
907 "Options concerning tables in Org-mode."
911 (defcustom org-enable-table-editor
'optimized
912 "Non-nil means, lines starting with \"|\" are handled by the table editor.
913 When nil, such lines will be treated like ordinary lines.
915 When equal to the symbol `optimized', the table editor will be optimized to
917 - Automatic overwrite mode in front of whitespace in table fields.
918 This makes the structure of the table stay in tact as long as the edited
919 field does not exceed the column width.
920 - Minimize the number of realigns. Normally, the table is aligned each time
921 TAB or RET are pressed to move to another field. With optimization this
922 happens only if changes to a field might have changed the column width.
923 Optimization requires replacing the functions `self-insert-command',
924 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
925 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
926 very good at guessing when a re-align will be necessary, but you can always
927 force one with \\[org-ctrl-c-ctrl-c].
929 If you would like to use the optimized version in Org-mode, but the
930 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
932 This variable can be used to turn on and off the table editor during a session,
933 but in order to toggle optimization, a restart is required.
935 See also the variable `org-table-auto-blank-field'."
938 (const :tag
"off" nil
)
940 (const :tag
"on, optimized" optimized
)))
942 (defcustom org-self-insert-cluster-for-undo t
943 "Non-nil means cluster self-insert commands for undo when possible.
944 If this is set, then, like in the Emacs command loop, 20 consequtive
945 characters will be undone together.
946 This is configurable, because there is some impact on typing performance."
950 (defcustom org-table-tab-recognizes-table.el t
951 "Non-nil means, TAB will automatically notice a table.el table.
952 When it sees such a table, it moves point into it and - if necessary -
953 calls `table-recognize-table'."
954 :group
'org-table-editing
957 (defgroup org-link nil
958 "Options concerning links in Org-mode."
962 (defvar org-link-abbrev-alist-local nil
963 "Buffer-local version of `org-link-abbrev-alist', which see.
964 The value of this is taken from the #+LINK lines.")
965 (make-variable-buffer-local 'org-link-abbrev-alist-local
)
967 (defcustom org-link-abbrev-alist nil
968 "Alist of link abbreviations.
969 The car of each element is a string, to be replaced at the start of a link.
970 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
971 links in Org-mode buffers can have an optional tag after a double colon, e.g.
973 [[linkkey:tag][description]]
975 The 'linkkey' must be a word word, starting with a letter, followed
976 by letters, numbers, '-' or '_'.
978 If REPLACE is a string, the tag will simply be appended to create the link.
979 If the string contains \"%s\", the tag will be inserted there. Alternatively,
980 the placeholder \"%h\" will cause a url-encoded version of the tag to
981 be inserted at that point (see the function `url-hexify-string').
983 REPLACE may also be a function that will be called with the tag as the
984 only argument to create the link, which should be returned as a string.
986 See the manual for examples."
990 (string :tag
"Protocol")
992 (string :tag
"Format")
995 (defcustom org-descriptive-links t
996 "Non-nil means, hide link part and only show description of bracket links.
997 Bracket links are like [[link][description]]. This variable sets the initial
998 state in new org-mode buffers. The setting can then be toggled on a
999 per-buffer basis from the Org->Hyperlinks menu."
1003 (defcustom org-link-file-path-type
'adaptive
1004 "How the path name in file links should be stored.
1007 relative Relative to the current directory, i.e. the directory of the file
1008 into which the link is being inserted.
1009 absolute Absolute path, if possible with ~ for home directory.
1010 noabbrev Absolute path, no abbreviation of home directory.
1011 adaptive Use relative path for files in the current directory and sub-
1012 directories of it. For other files, use an absolute path."
1020 (defcustom org-activate-links
'(bracket angle plain radio tag date footnote
)
1021 "Types of links that should be activated in Org-mode files.
1022 This is a list of symbols, each leading to the activation of a certain link
1023 type. In principle, it does not hurt to turn on most link types - there may
1024 be a small gain when turning off unused link types. The types are:
1026 bracket The recommended [[link][description]] or [[link]] links with hiding.
1027 angular Links in angular brackets that may contain whitespace like
1028 <bbdb:Carsten Dominik>.
1029 plain Plain links in normal text, no whitespace, like http://google.com.
1030 radio Text that is matched by a radio target, see manual for details.
1031 tag Tag settings in a headline (link to tag search).
1032 date Time stamps (link to calendar).
1033 footnote Footnote labels.
1035 Changing this variable requires a restart of Emacs to become effective."
1037 :type
'(set :greedy t
1038 (const :tag
"Double bracket links (new style)" bracket
)
1039 (const :tag
"Angular bracket links (old style)" angular
)
1040 (const :tag
"Plain text links" plain
)
1041 (const :tag
"Radio target matches" radio
)
1042 (const :tag
"Tags" tag
)
1043 (const :tag
"Timestamps" date
)
1044 (const :tag
"Footnotes" footnote
)))
1046 (defcustom org-make-link-description-function nil
1047 "Function to use to generate link descriptions from links. If
1048 nil the link location will be used. This function must take two
1049 parameters; the first is the link and the second the description
1050 org-insert-link has generated, and should return the description
1055 (defgroup org-link-store nil
1056 "Options concerning storing links in Org-mode."
1057 :tag
"Org Store Link"
1060 (defcustom org-email-link-description-format
"Email %c: %.30s"
1061 "Format of the description part of a link to an email or usenet message.
1062 The following %-escapes will be replaced by corresponding information:
1064 %F full \"From\" field
1065 %f name, taken from \"From\" field, address if no name
1066 %T full \"To\" field
1067 %t first name in \"To\" field, address if no name
1068 %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
1069 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
1073 You may use normal field width specification between the % and the letter.
1074 This is for example useful to limit the length of the subject.
1076 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
1077 :group
'org-link-store
1080 (defcustom org-from-is-user-regexp
1082 (when (and user-mail-address
(not (string= user-mail-address
"")))
1083 (setq r1
(concat "\\<" (regexp-quote user-mail-address
) "\\>")))
1084 (when (and user-full-name
(not (string= user-full-name
"")))
1085 (setq r2
(concat "\\<" (regexp-quote user-full-name
) "\\>")))
1086 (if (and r1 r2
) (concat r1
"\\|" r2
) (or r1 r2
)))
1087 "Regexp matched against the \"From:\" header of an email or usenet message.
1088 It should match if the message is from the user him/herself."
1089 :group
'org-link-store
1092 (defcustom org-link-to-org-use-id
'create-if-interactive-and-no-custom-id
1093 "Non-nil means, storing a link to an Org file will use entry IDs.
1095 Note that before this variable is even considered, org-id must be loaded,
1096 so please customize `org-modules' and turn it on.
1098 The variable can have the following values:
1100 t Create an ID if needed to make a link to the current entry.
1102 create-if-interactive
1103 If `org-store-link' is called directly (interactively, as a user
1104 command), do create an ID to support the link. But when doing the
1105 job for remember, only use the ID if it already exists. The
1106 purpose of this setting is to avoid proliferation of unwanted
1107 IDs, just because you happen to be in an Org file when you
1108 call `org-remember' that automatically and preemptively
1109 creates a link. If you do want to get an ID link in a remember
1110 template to an entry not having an ID, create it first by
1111 explicitly creating a link to it, using `C-c C-l' first.
1113 create-if-interactive-and-no-custom-id
1114 Like create-if-interactive, but do not create an ID if there is
1115 a CUSTOM_ID property defined in the entry. This is the default.
1118 Use existing ID, do not create one.
1120 nil Never use an ID to make a link, instead link using a text search for
1122 :group
'org-link-store
1124 (const :tag
"Create ID to make link" t
)
1125 (const :tag
"Create if storing link interactively"
1126 create-if-interactive
)
1127 (const :tag
"Create if storing link interactively and no CUSTOM_ID is present"
1128 create-if-interactive-and-no-custom-id
)
1129 (const :tag
"Only use existing" use-existing
)
1130 (const :tag
"Do not use ID to create link" nil
)))
1132 (defcustom org-context-in-file-links t
1133 "Non-nil means, file links from `org-store-link' contain context.
1134 A search string will be added to the file name with :: as separator and
1135 used to find the context when the link is activated by the command
1136 `org-open-at-point'.
1137 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1138 negates this setting for the duration of the command."
1139 :group
'org-link-store
1142 (defcustom org-keep-stored-link-after-insertion nil
1143 "Non-nil means, keep link in list for entire session.
1145 The command `org-store-link' adds a link pointing to the current
1146 location to an internal list. These links accumulate during a session.
1147 The command `org-insert-link' can be used to insert links into any
1148 Org-mode file (offering completion for all stored links). When this
1149 option is nil, every link which has been inserted once using \\[org-insert-link]
1150 will be removed from the list, to make completing the unused links
1152 :group
'org-link-store
1155 (defgroup org-link-follow nil
1156 "Options concerning following links in Org-mode."
1157 :tag
"Org Follow Link"
1160 (defcustom org-link-translation-function nil
1161 "Function to translate links with different syntax to Org syntax.
1162 This can be used to translate links created for example by the Planner
1163 or emacs-wiki packages to Org syntax.
1164 The function must accept two parameters, a TYPE containing the link
1165 protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
1166 which is everything after the link protocol. It should return a cons
1167 with possibly modified values of type and path.
1168 Org contains a function for this, so if you set this variable to
1169 `org-translate-link-from-planner', you should be able follow many
1170 links created by planner."
1171 :group
'org-link-follow
1174 (defcustom org-follow-link-hook nil
1175 "Hook that is run after a link has been followed."
1176 :group
'org-link-follow
1179 (defcustom org-tab-follows-link nil
1180 "Non-nil means, on links TAB will follow the link.
1181 Needs to be set before org.el is loaded.
1182 This really should not be used, it does not make sense, and the
1183 implementation is bad."
1184 :group
'org-link-follow
1187 (defcustom org-return-follows-link nil
1188 "Non-nil means, on links RET will follow the link.
1189 Needs to be set before org.el is loaded."
1190 :group
'org-link-follow
1193 (defcustom org-mouse-1-follows-link
1194 (if (boundp 'mouse-1-click-follows-link
) mouse-1-click-follows-link t
)
1195 "Non-nil means, mouse-1 on a link will follow the link.
1196 A longer mouse click will still set point. Does not work on XEmacs.
1197 Needs to be set before org.el is loaded."
1198 :group
'org-link-follow
1201 (defcustom org-mark-ring-length
4
1202 "Number of different positions to be recorded in the ring
1203 Changing this requires a restart of Emacs to work correctly."
1204 :group
'org-link-follow
1207 (defcustom org-link-frame-setup
1208 '((vm . vm-visit-folder-other-frame
)
1209 (gnus . gnus-other-frame
)
1210 (file . find-file-other-window
))
1211 "Setup the frame configuration for following links.
1212 When following a link with Emacs, it may often be useful to display
1213 this link in another window or frame. This variable can be used to
1214 set this up for the different types of links.
1217 `vm-visit-folder-other-frame'
1218 For Gnus, use any of
1221 `org-gnus-no-new-news'
1222 For FILE, use any of
1224 `find-file-other-window'
1225 `find-file-other-frame'
1226 For the calendar, use the variable `calendar-setup'.
1227 For BBDB, it is currently only possible to display the matches in
1229 :group
'org-link-follow
1233 (const vm-visit-folder
)
1234 (const vm-visit-folder-other-window
)
1235 (const vm-visit-folder-other-frame
)))
1239 (const gnus-other-frame
)
1240 (const org-gnus-no-new-news
)))
1244 (const find-file-other-window
)
1245 (const find-file-other-frame
)))))
1247 (defcustom org-display-internal-link-with-indirect-buffer nil
1248 "Non-nil means, use indirect buffer to display infile links.
1249 Activating internal links (from one location in a file to another location
1250 in the same file) normally just jumps to the location. When the link is
1251 activated with a C-u prefix (or with mouse-3), the link is displayed in
1252 another window. When this option is set, the other window actually displays
1253 an indirect buffer clone of the current buffer, to avoid any visibility
1254 changes to the current buffer."
1255 :group
'org-link-follow
1258 (defcustom org-open-non-existing-files nil
1259 "Non-nil means, `org-open-file' will open non-existing files.
1260 When nil, an error will be generated.
1261 This variable applies only to external applications because they
1262 might choke on non-existing files. If the link is to a file that
1263 will be openend in Emacs, the variable is ignored."
1264 :group
'org-link-follow
1267 (defcustom org-open-directory-means-index-dot-org nil
1268 "Non-nil means, a link to a directory really means to index.org.
1269 When nil, following a directory link will run dired or open a finder/explorer
1270 window on that directory."
1271 :group
'org-link-follow
1274 (defcustom org-link-mailto-program
'(browse-url "mailto:%a?subject=%s")
1275 "Function and arguments to call for following mailto links.
1276 This is a list with the first element being a lisp function, and the
1277 remaining elements being arguments to the function. In string arguments,
1278 %a will be replaced by the address, and %s will be replaced by the subject
1279 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1280 :group
'org-link-follow
1282 (const :tag
"browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1283 (const :tag
"compose-mail" (compose-mail "%a" "%s"))
1284 (const :tag
"message-mail" (message-mail "%a" "%s"))
1285 (cons :tag
"other" (function) (repeat :tag
"argument" sexp
))))
1287 (defcustom org-confirm-shell-link-function
'yes-or-no-p
1288 "Non-nil means, ask for confirmation before executing shell links.
1289 Shell links can be dangerous: just think about a link
1291 [[shell:rm -rf ~/*][Google Search]]
1293 This link would show up in your Org-mode document as \"Google Search\",
1294 but really it would remove your entire home directory.
1295 Therefore we advise against setting this variable to nil.
1296 Just change it to `y-or-n-p' if you want to confirm with a
1297 single keystroke rather than having to type \"yes\"."
1298 :group
'org-link-follow
1300 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1301 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1302 (const :tag
"no confirmation (dangerous)" nil
)))
1304 (defcustom org-confirm-elisp-link-function
'yes-or-no-p
1305 "Non-nil means, ask for confirmation before executing Emacs Lisp links.
1306 Elisp links can be dangerous: just think about a link
1308 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1310 This link would show up in your Org-mode document as \"Google Search\",
1311 but really it would remove your entire home directory.
1312 Therefore we advise against setting this variable to nil.
1313 Just change it to `y-or-n-p' if you want to confirm with a
1314 single keystroke rather than having to type \"yes\"."
1315 :group
'org-link-follow
1317 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1318 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1319 (const :tag
"no confirmation (dangerous)" nil
)))
1321 (defconst org-file-apps-defaults-gnu
1325 "Default file applications on a UNIX or GNU/Linux system.
1326 See `org-file-apps'.")
1328 (defconst org-file-apps-defaults-macosx
1331 (system .
"open %s")
1333 ("eps.gz" .
"gv %s")
1335 ("fig" .
"xfig %s"))
1336 "Default file applications on a MacOS X system.
1337 The system \"open\" is known as a default, but we use X11 applications
1338 for some files for which the OS does not have a good default.
1339 See `org-file-apps'.")
1341 (defconst org-file-apps-defaults-windowsnt
1345 (list (if (featurep 'xemacs
)
1346 'mswindows-shell-execute
1350 (list (if (featurep 'xemacs
)
1351 'mswindows-shell-execute
1354 "Default file applications on a Windows NT system.
1355 The system \"open\" is used for most files.
1356 See `org-file-apps'.")
1358 (defcustom org-file-apps
1361 ("\\.x?html?\\'" . default
)
1362 ("\\.pdf\\'" . default
)
1364 "External applications for opening `file:path' items in a document.
1365 Org-mode uses system defaults for different file types, but
1366 you can use this variable to set the application for a given file
1367 extension. The entries in this list are cons cells where the car identifies
1368 files and the cdr the corresponding command. Possible values for the
1370 \"regex\" Regular expression matched against the file name. For backward
1371 compatibility, this can also be a string with only alphanumeric
1372 characters, which is then interpreted as an extension.
1373 `directory' Matches a directory
1374 `remote' Matches a remote file, accessible through tramp or efs.
1375 Remote files most likely should be visited through Emacs
1376 because external applications cannot handle such paths.
1377 `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
1378 so all files Emacs knows how to handle. Using this with
1379 command `emacs' will open most files in Emacs. Beware that this
1380 will also open html files inside Emacs, unless you add
1381 (\"html\" . default) to the list as well.
1382 t Default for files not matched by any of the other options.
1383 `system' The system command to open files, like `open' on Windows
1384 and Mac OS X, and mailcap under GNU/Linux. This is the command
1385 that will be selected if you call `C-c C-o' with a double
1388 Possible values for the command are:
1389 `emacs' The file will be visited by the current Emacs process.
1390 `default' Use the default application for this file type, which is the
1391 association for t in the list, most likely in the system-specific
1393 This can be used to overrule an unwanted setting in the
1394 system-specific variable.
1395 `system' Use the system command for opening files, like \"open\".
1396 This command is specified by the entry whose car is `system'.
1397 Most likely, the system-specific version of this variable
1398 does define this command, but you can overrule/replace it
1400 string A command to be executed by a shell; %s will be replaced
1401 by the path to the file.
1402 sexp A Lisp form which will be evaluated. The file path will
1403 be available in the Lisp variable `file'.
1404 For more examples, see the system specific constants
1405 `org-file-apps-defaults-macosx'
1406 `org-file-apps-defaults-windowsnt'
1407 `org-file-apps-defaults-gnu'."
1408 :group
'org-link-follow
1410 (cons (choice :value
""
1411 (string :tag
"Extension")
1412 (const :tag
"System command to open files" system
)
1413 (const :tag
"Default for unrecognized files" t
)
1414 (const :tag
"Remote file" remote
)
1415 (const :tag
"Links to a directory" directory
)
1416 (const :tag
"Any files that have Emacs modes"
1419 (const :tag
"Visit with Emacs" emacs
)
1420 (const :tag
"Use default" default
)
1421 (const :tag
"Use the system command" system
)
1422 (string :tag
"Command")
1423 (sexp :tag
"Lisp form")))))
1425 (defgroup org-refile nil
1426 "Options concerning refiling entries in Org-mode."
1430 (defcustom org-directory
"~/org"
1431 "Directory with org files.
1432 This is just a default location to look for Org files. There is no need
1433 at all to put your files into this directory. It is only used in the
1434 following situations:
1436 1. When a remember template specifies a target file that is not an
1437 absolute path. The path will then be interpreted relative to
1439 2. When a remember note is filed away in an interactive way (when exiting the
1440 note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
1441 with `org-directory' as the default path."
1443 :group
'org-remember
1446 (defcustom org-default-notes-file
(convert-standard-filename "~/.notes")
1447 "Default target for storing notes.
1448 Used by the hooks for remember.el. This can be a string, or nil to mean
1449 the value of `remember-data-file'.
1450 You can set this on a per-template basis with the variable
1451 `org-remember-templates'."
1453 :group
'org-remember
1455 (const :tag
"Default from remember-data-file" nil
)
1458 (defcustom org-goto-interface
'outline
1459 "The default interface to be used for `org-goto'.
1461 outline The interface shows an outline of the relevant file
1462 and the correct heading is found by moving through
1463 the outline or by searching with incremental search.
1464 outline-path-completion Headlines in the current buffer are offered via
1465 completion. This is the interface also used by
1466 the refile command."
1469 (const :tag
"Outline" outline
)
1470 (const :tag
"Outline-path-completion" outline-path-completion
)))
1472 (defcustom org-goto-max-level
5
1473 "Maximum level to be considered when running org-goto with refile interface."
1477 (defcustom org-reverse-note-order nil
1478 "Non-nil means, store new notes at the beginning of a file or entry.
1479 When nil, new notes will be filed to the end of a file or entry.
1480 This can also be a list with cons cells of regular expressions that
1481 are matched against file names, and values."
1482 :group
'org-remember
1485 (const :tag
"Reverse always" t
)
1486 (const :tag
"Reverse never" nil
)
1487 (repeat :tag
"By file name regexp"
1488 (cons regexp boolean
))))
1490 (defcustom org-refile-targets nil
1491 "Targets for refiling entries with \\[org-refile].
1492 This is list of cons cells. Each cell contains:
1493 - a specification of the files to be considered, either a list of files,
1494 or a symbol whose function or variable value will be used to retrieve
1495 a file name or a list of file names. If you use `org-agenda-files' for
1496 that, all agenda files will be scanned for targets. Nil means, consider
1497 headings in the current buffer.
1498 - A specification of how to find candidate refile targets. This may be
1500 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
1501 This tag has to be present in all target headlines, inheritance will
1503 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
1505 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
1506 headlines that are refiling targets.
1507 - a cons cell (:level . N). Any headline of level N is considered a target.
1508 Note that, when `org-odd-levels-only' is set, level corresponds to
1509 order in hierarchy, not to the number of stars.
1510 - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
1511 Note that, when `org-odd-levels-only' is set, level corresponds to
1512 order in hierarchy, not to the number of stars.
1514 You can set the variable `org-refile-target-verify-function' to a function
1515 to verify each headline found by the simple critery above.
1517 When this variable is nil, all top-level headlines in the current buffer
1518 are used, equivalent to the value `((nil . (:level . 1))'."
1522 (choice :value org-agenda-files
1523 (const :tag
"All agenda files" org-agenda-files
)
1524 (const :tag
"Current buffer" nil
)
1525 (function) (variable) (file))
1526 (choice :tag
"Identify target headline by"
1527 (cons :tag
"Specific tag" (const :value
:tag
) (string))
1528 (cons :tag
"TODO keyword" (const :value
:todo
) (string))
1529 (cons :tag
"Regular expression" (const :value
:regexp
) (regexp))
1530 (cons :tag
"Level number" (const :value
:level
) (integer))
1531 (cons :tag
"Max Level number" (const :value
:maxlevel
) (integer))))))
1533 (defcustom org-refile-target-verify-function nil
1534 "Function to verify if the headline at point should be a refile target.
1535 The function will be called without arguments, with point at the
1536 beginning of the headline. It should return t and leave point
1537 where it is if the headline is a valid target for refiling.
1539 If the target should not be selected, the function must return nil.
1540 In addition to this, it may move point to a place from where the search
1541 should be continued. For example, the function may decide that the entire
1542 subtree of the current entry should be excluded and move point to the end
1547 (defcustom org-refile-use-outline-path nil
1548 "Non-nil means, provide refile targets as paths.
1549 So a level 3 headline will be available as level1/level2/level3.
1551 When the value is `file', also include the file name (without directory)
1552 into the path. In this case, you can also stop the completion after
1553 the file name, to get entries inserted as top level in the file.
1555 When `full-file-path', include the full file path."
1558 (const :tag
"Not" nil
)
1559 (const :tag
"Yes" t
)
1560 (const :tag
"Start with file name" file
)
1561 (const :tag
"Start with full file path" full-file-path
)))
1563 (defcustom org-outline-path-complete-in-steps t
1564 "Non-nil means, complete the outline path in hierarchical steps.
1565 When Org-mode uses the refile interface to select an outline path
1566 \(see variable `org-refile-use-outline-path'), the completion of
1567 the path can be done is a single go, or if can be done in steps down
1568 the headline hierarchy. Going in steps is probably the best if you
1569 do not use a special completion package like `ido' or `icicles'.
1570 However, when using these packages, going in one step can be very
1571 fast, while still showing the whole path to the entry."
1575 (defcustom org-refile-allow-creating-parent-nodes nil
1576 "Non-nil means, allow to create new nodes as refile targets.
1577 New nodes are then created by adding \"/new node name\" to the completion
1578 of an existing node. When the value of this variable is `confirm',
1579 new node creation must be confirmed by the user (recommended)
1580 When nil, the completion must match an existing entry.
1582 Note that, if the new heading is not seen by the criteria
1583 listed in `org-refile-targets', multiple instances of the same
1584 heading would be created by trying again to file under the new
1588 (const :tag
"Never" nil
)
1589 (const :tag
"Always" t
)
1590 (const :tag
"Prompt for confirmation" confirm
)))
1592 (defgroup org-todo nil
1593 "Options concerning TODO items in Org-mode."
1597 (defgroup org-progress nil
1598 "Options concerning Progress logging in Org-mode."
1602 (defvar org-todo-interpretation-widgets
1604 (:tag
"Sequence (cycling hits every state)" sequence
)
1605 (:tag
"Type (cycling directly to DONE)" type
))
1606 "The available interpretation symbols for customizing
1607 `org-todo-keywords'.
1608 Interested libraries should add to this list.")
1610 (defcustom org-todo-keywords
'((sequence "TODO" "DONE"))
1611 "List of TODO entry keyword sequences and their interpretation.
1612 \\<org-mode-map>This is a list of sequences.
1614 Each sequence starts with a symbol, either `sequence' or `type',
1615 indicating if the keywords should be interpreted as a sequence of
1616 action steps, or as different types of TODO items. The first
1617 keywords are states requiring action - these states will select a headline
1618 for inclusion into the global TODO list Org-mode produces. If one of
1619 the \"keywords\" is the vertical bat \"|\" the remaining keywords
1620 signify that no further action is necessary. If \"|\" is not found,
1621 the last keyword is treated as the only DONE state of the sequence.
1623 The command \\[org-todo] cycles an entry through these states, and one
1624 additional state where no keyword is present. For details about this
1625 cycling, see the manual.
1627 TODO keywords and interpretation can also be set on a per-file basis with
1628 the special #+SEQ_TODO and #+TYP_TODO lines.
1630 Each keyword can optionally specify a character for fast state selection
1631 \(in combination with the variable `org-use-fast-todo-selection')
1632 and specifiers for state change logging, using the same syntax
1633 that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
1634 that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
1635 indicates to record a time stamp each time this state is selected.
1637 Each keyword may also specify if a timestamp or a note should be
1638 recorded when entering or leaving the state, by adding additional
1639 characters in the parenthesis after the keyword. This looks like this:
1640 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
1641 record only the time of the state change. With X and Y being either
1642 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
1643 Y when leaving the state if and only if the *target* state does not
1644 define X. You may omit any of the fast-selection key or X or /Y,
1645 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
1647 For backward compatibility, this variable may also be just a list
1648 of keywords - in this case the interpretation (sequence or type) will be
1649 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
1651 :group
'org-keywords
1653 (repeat :tag
"Old syntax, just keywords"
1654 (string :tag
"Keyword"))
1655 (repeat :tag
"New syntax"
1658 :tag
"Interpretation"
1659 ;;Quick and dirty way to see
1660 ;;`org-todo-interpretations'. This takes the
1661 ;;place of item arguments
1669 org-todo-interpretation-widgets
))
1672 (string :tag
"Keyword"))))))
1674 (defvar org-todo-keywords-1 nil
1675 "All TODO and DONE keywords active in a buffer.")
1676 (make-variable-buffer-local 'org-todo-keywords-1
)
1677 (defvar org-todo-keywords-for-agenda nil
)
1678 (defvar org-done-keywords-for-agenda nil
)
1679 (defvar org-todo-keyword-alist-for-agenda nil
)
1680 (defvar org-tag-alist-for-agenda nil
)
1681 (defvar org-agenda-contributing-files nil
)
1682 (defvar org-not-done-keywords nil
)
1683 (make-variable-buffer-local 'org-not-done-keywords
)
1684 (defvar org-done-keywords nil
)
1685 (make-variable-buffer-local 'org-done-keywords
)
1686 (defvar org-todo-heads nil
)
1687 (make-variable-buffer-local 'org-todo-heads
)
1688 (defvar org-todo-sets nil
)
1689 (make-variable-buffer-local 'org-todo-sets
)
1690 (defvar org-todo-log-states nil
)
1691 (make-variable-buffer-local 'org-todo-log-states
)
1692 (defvar org-todo-kwd-alist nil
)
1693 (make-variable-buffer-local 'org-todo-kwd-alist
)
1694 (defvar org-todo-key-alist nil
)
1695 (make-variable-buffer-local 'org-todo-key-alist
)
1696 (defvar org-todo-key-trigger nil
)
1697 (make-variable-buffer-local 'org-todo-key-trigger
)
1699 (defcustom org-todo-interpretation
'sequence
1700 "Controls how TODO keywords are interpreted.
1701 This variable is in principle obsolete and is only used for
1702 backward compatibility, if the interpretation of todo keywords is
1703 not given already in `org-todo-keywords'. See that variable for
1706 :group
'org-keywords
1707 :type
'(choice (const sequence
)
1710 (defcustom org-use-fast-todo-selection t
1711 "Non-nil means, use the fast todo selection scheme with C-c C-t.
1712 This variable describes if and under what circumstances the cycling
1713 mechanism for TODO keywords will be replaced by a single-key, direct
1716 When nil, fast selection is never used.
1718 When the symbol `prefix', it will be used when `org-todo' is called with
1719 a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
1720 in an agenda buffer.
1722 When t, fast selection is used by default. In this case, the prefix
1723 argument forces cycling instead.
1725 In all cases, the special interface is only used if access keys have actually
1726 been assigned by the user, i.e. if keywords in the configuration are followed
1727 by a letter in parenthesis, like TODO(t)."
1730 (const :tag
"Never" nil
)
1731 (const :tag
"By default" t
)
1732 (const :tag
"Only with C-u C-c C-t" prefix
)))
1734 (defcustom org-provide-todo-statistics t
1735 "Non-nil means, update todo statistics after insert and toggle.
1736 ALL-HEADLINES means update todo statistics by including headlines
1737 with no TODO keyword as well, counting them as not done.
1738 A list of TODO keywords means the same, but skip keywords that are
1741 When this is set, todo statistics is updated in the parent of the
1742 current entry each time a todo state is changed."
1745 (const :tag
"Yes, only for TODO entries" t
)
1746 (const :tag
"Yes, including all entries" 'all-headlines
)
1747 (repeat :tag
"Yes, for TODOs in this list"
1748 (string :tag
"TODO keyword"))
1749 (other :tag
"No TODO statistics" nil
)))
1751 (defcustom org-hierarchical-todo-statistics t
1752 "Non-nil means, TODO statistics covers just direct children.
1753 When nil, all entries in the subtree are considered.
1754 This has only an effect if `org-provide-todo-statistics' is set."
1758 (defcustom org-after-todo-state-change-hook nil
1759 "Hook which is run after the state of a TODO item was changed.
1760 The new state (a string with a TODO keyword, or nil) is available in the
1761 Lisp variable `state'."
1765 (defvar org-blocker-hook nil
1766 "Hook for functions that are allowed to block a state change.
1768 Each function gets as its single argument a property list, see
1769 `org-trigger-hook' for more information about this list.
1771 If any of the functions in this hook returns nil, the state change
1774 (defvar org-trigger-hook nil
1775 "Hook for functions that are triggered by a state change.
1777 Each function gets as its single argument a property list with at least
1778 the following elements:
1780 (:type type-of-change :position pos-at-entry-start
1781 :from old-state :to new-state)
1783 Depending on the type, more properties may be present.
1785 This mechanism is currently implemented for:
1789 :type todo-state-change
1790 :from previous state (keyword as a string), or nil, or a symbol
1791 'todo' or 'done', to indicate the general type of state.
1792 :to new state, like in :from")
1794 (defcustom org-enforce-todo-dependencies nil
1795 "Non-nil means, undone TODO entries will block switching the parent to DONE.
1796 Also, if a parent has an :ORDERED: property, switching an entry to DONE will
1797 be blocked if any prior sibling is not yet done.
1798 Finally, if the parent is blocked because of ordered siblings of its own,
1799 the child will also be blocked.
1800 This variable needs to be set before org.el is loaded, and you need to
1801 restart Emacs after a change to make the change effective. The only way
1802 to change is while Emacs is running is through the customize interface."
1803 :set
(lambda (var val
)
1806 (add-hook 'org-blocker-hook
1807 'org-block-todo-from-children-or-siblings-or-parent
)
1808 (remove-hook 'org-blocker-hook
1809 'org-block-todo-from-children-or-siblings-or-parent
)))
1813 (defcustom org-enforce-todo-checkbox-dependencies nil
1814 "Non-nil means, unchecked boxes will block switching the parent to DONE.
1815 When this is nil, checkboxes have no influence on switching TODO states.
1816 When non-nil, you first need to check off all check boxes before the TODO
1817 entry can be switched to DONE.
1818 This variable needs to be set before org.el is loaded, and you need to
1819 restart Emacs after a change to make the change effective. The only way
1820 to change is while Emacs is running is through the customize interface."
1821 :set
(lambda (var val
)
1824 (add-hook 'org-blocker-hook
1825 'org-block-todo-from-checkboxes
)
1826 (remove-hook 'org-blocker-hook
1827 'org-block-todo-from-checkboxes
)))
1831 (defcustom org-treat-insert-todo-heading-as-state-change nil
1832 "Non-nil means, inserting a TODO heading is treated as state change.
1833 So when the command \\[org-insert-todo-heading] is used, state change
1834 logging will apply if appropriate. When nil, the new TODO item will
1835 be inserted directly, and no logging will take place."
1839 (defcustom org-treat-S-cursor-todo-selection-as-state-change t
1840 "Non-nil means, switching TODO states with S-cursor counts as state change.
1841 This is the default behavior. However, setting this to nil allows a
1842 convenient way to select a TODO state and bypass any logging associated
1847 (defcustom org-todo-state-tags-triggers nil
1848 "Tag changes that should be triggered by TODO state changes.
1849 This is a list. Each entry is
1851 (state-change (tag . flag) .......)
1853 State-change can be a string with a state, and empty string to indicate the
1854 state that has no TODO keyword, or it can be one of the symbols `todo'
1855 or `done', meaning any not-done or done state, respectively."
1859 (cons (choice :tag
"When changing to"
1860 (const :tag
"Not-done state" todo
)
1861 (const :tag
"Done state" done
)
1862 (string :tag
"State"))
1864 (cons :tag
"Tag action"
1866 (choice (const :tag
"Add" t
) (const :tag
"Remove" nil
)))))))
1868 (defcustom org-log-done nil
1869 "Information to record when a task moves to the DONE state.
1871 Possible values are:
1873 nil Don't add anything, just change the keyword
1874 time Add a time stamp to the task
1875 note Prompt a closing note and add it with template `org-log-note-headings'
1877 This option can also be set with on a per-file-basis with
1879 #+STARTUP: nologdone
1881 #+STARTUP: lognotedone
1883 You can have local logging settings for a subtree by setting the LOGGING
1884 property to one or more of these keywords."
1886 :group
'org-progress
1888 (const :tag
"No logging" nil
)
1889 (const :tag
"Record CLOSED timestamp" time
)
1890 (const :tag
"Record CLOSED timestamp with closing note." note
)))
1892 ;; Normalize old uses of org-log-done.
1894 ((eq org-log-done t
) (setq org-log-done
'time
))
1895 ((and (listp org-log-done
) (memq 'done org-log-done
))
1896 (setq org-log-done
'note
)))
1898 (defcustom org-log-note-clock-out nil
1899 "Non-nil means, record a note when clocking out of an item.
1900 This can also be configured on a per-file basis by adding one of
1901 the following lines anywhere in the buffer:
1903 #+STARTUP: lognoteclock-out
1904 #+STARTUP: nolognoteclock-out"
1906 :group
'org-progress
1909 (defcustom org-log-done-with-time t
1910 "Non-nil means, the CLOSED time stamp will contain date and time.
1911 When nil, only the date will be recorded."
1912 :group
'org-progress
1915 (defcustom org-log-note-headings
1916 '((done .
"CLOSING NOTE %t")
1917 (state .
"State %-12s from %-12S %t")
1918 (note .
"Note taken on %t")
1920 "Headings for notes added to entries.
1921 The value is an alist, with the car being a symbol indicating the note
1922 context, and the cdr is the heading to be used. The heading may also be the
1924 %t in the heading will be replaced by a time stamp.
1925 %s will be replaced by the new TODO state, in double quotes.
1926 %S will be replaced by the old TODO state, in double quotes.
1927 %u will be replaced by the user name.
1928 %U will be replaced by the full user name."
1930 :group
'org-progress
1931 :type
'(list :greedy t
1932 (cons (const :tag
"Heading when closing an item" done
) string
)
1934 "Heading when changing todo state (todo sequence only)"
1936 (cons (const :tag
"Heading when just taking a note" note
) string
)
1937 (cons (const :tag
"Heading when clocking out" clock-out
) string
)))
1939 (unless (assq 'note org-log-note-headings
)
1940 (push '(note .
"%t") org-log-note-headings
))
1942 (defcustom org-log-into-drawer nil
1943 "Non-nil means, insert state change notes and time stamps into a drawer.
1944 When nil, state changes notes will be inserted after the headline and
1945 any scheduling and clock lines, but not inside a drawer.
1947 The value of this variable should be the name of the drawer to use.
1948 LOGBOOK is proposed at the default drawer for this purpose, you can
1949 also set this to a string to define the drawer of your choice.
1951 A value of t is also allowed, representing \"LOGBOOK\".
1953 If this variable is set, `org-log-state-notes-insert-after-drawers'
1956 You can set the property LOG_INTO_DRAWER to overrule this setting for
1959 :group
'org-progress
1961 (const :tag
"Not into a drawer" nil
)
1962 (const :tag
"LOGBOOK" t
)
1963 (string :tag
"Other")))
1965 (if (fboundp 'defvaralias
)
1966 (defvaralias 'org-log-state-notes-into-drawer
'org-log-into-drawer
))
1968 (defun org-log-into-drawer ()
1969 "Return the value of `org-log-into-drawer', but let properties overrule.
1970 If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
1971 used instead of the default value."
1972 (let ((p (ignore-errors (org-entry-get nil
"LOG_INTO_DRAWER" 'inherit
))))
1974 ((or (not p
) (equal p
"nil")) org-log-into-drawer
)
1975 ((equal p
"t") "LOGBOOK")
1978 (defcustom org-log-state-notes-insert-after-drawers nil
1979 "Non-nil means, insert state change notes after any drawers in entry.
1980 Only the drawers that *immediately* follow the headline and the
1981 deadline/scheduled line are skipped.
1982 When nil, insert notes right after the heading and perhaps the line
1983 with deadline/scheduling if present.
1985 This variable will have no effect if `org-log-into-drawer' is
1988 :group
'org-progress
1991 (defcustom org-log-states-order-reversed t
1992 "Non-nil means, the latest state change note will be directly after heading.
1993 When nil, the notes will be orderer according to time."
1995 :group
'org-progress
1998 (defcustom org-log-repeat
'time
1999 "Non-nil means, record moving through the DONE state when triggering repeat.
2000 An auto-repeating tasks is immediately switched back to TODO when marked
2001 done. If you are not logging state changes (by adding \"@\" or \"!\" to
2002 the TODO keyword definition, or recording a closing note by setting
2003 `org-log-done', there will be no record of the task moving through DONE.
2004 This variable forces taking a note anyway. Possible values are:
2006 nil Don't force a record
2007 time Record a time stamp
2010 This option can also be set with on a per-file-basis with
2012 #+STARTUP: logrepeat
2013 #+STARTUP: lognoterepeat
2014 #+STARTUP: nologrepeat
2016 You can have local logging settings for a subtree by setting the LOGGING
2017 property to one or more of these keywords."
2019 :group
'org-progress
2021 (const :tag
"Don't force a record" nil
)
2022 (const :tag
"Force recording the DONE state" time
)
2023 (const :tag
"Force recording a note with the DONE state" note
)))
2026 (defgroup org-priorities nil
2027 "Priorities in Org-mode."
2028 :tag
"Org Priorities"
2031 (defcustom org-enable-priority-commands t
2032 "Non-nil means, priority commands are active.
2033 When nil, these commands will be disabled, so that you never accidentally
2035 :group
'org-priorities
2038 (defcustom org-highest-priority ?A
2039 "The highest priority of TODO items. A character like ?A, ?B etc.
2040 Must have a smaller ASCII number than `org-lowest-priority'."
2041 :group
'org-priorities
2044 (defcustom org-lowest-priority ?C
2045 "The lowest priority of TODO items. A character like ?A, ?B etc.
2046 Must have a larger ASCII number than `org-highest-priority'."
2047 :group
'org-priorities
2050 (defcustom org-default-priority ?B
2051 "The default priority of TODO items.
2052 This is the priority an item get if no explicit priority is given."
2053 :group
'org-priorities
2056 (defcustom org-priority-start-cycle-with-default t
2057 "Non-nil means, start with default priority when starting to cycle.
2058 When this is nil, the first step in the cycle will be (depending on the
2059 command used) one higher or lower that the default priority."
2060 :group
'org-priorities
2063 (defgroup org-time nil
2064 "Options concerning time stamps and deadlines in Org-mode."
2068 (defcustom org-insert-labeled-timestamps-at-point nil
2069 "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
2070 When nil, these labeled time stamps are forces into the second line of an
2071 entry, just after the headline. When scheduling from the global TODO list,
2072 the time stamp will always be forced into the second line."
2076 (defconst org-time-stamp-formats
'("<%Y-%m-%d %a>" .
"<%Y-%m-%d %a %H:%M>")
2077 "Formats for `format-time-string' which are used for time stamps.
2078 It is not recommended to change this constant.")
2080 (defcustom org-time-stamp-rounding-minutes
'(0 5)
2081 "Number of minutes to round time stamps to.
2082 These are two values, the first applies when first creating a time stamp.
2083 The second applies when changing it with the commands `S-up' and `S-down'.
2084 When changing the time stamp, this means that it will change in steps
2085 of N minutes, as given by the second value.
2087 When a setting is 0 or 1, insert the time unmodified. Useful rounding
2088 numbers should be factors of 60, so for example 5, 10, 15.
2090 When this is larger than 1, you can still force an exact time-stamp by using
2091 a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
2092 and by using a prefix arg to `S-up/down' to specify the exact number
2093 of minutes to shift."
2095 :get
'(lambda (var) ; Make sure all entries have 5 elements
2096 (if (integerp (default-value var
))
2097 (list (default-value var
) 5)
2098 (default-value var
)))
2100 (integer :tag
"when inserting times")
2101 (integer :tag
"when modifying times")))
2103 ;; Normalize old customizations of this variable.
2104 (when (integerp org-time-stamp-rounding-minutes
)
2105 (setq org-time-stamp-rounding-minutes
2106 (list org-time-stamp-rounding-minutes
2107 org-time-stamp-rounding-minutes
)))
2109 (defcustom org-display-custom-times nil
2110 "Non-nil means, overlay custom formats over all time stamps.
2111 The formats are defined through the variable `org-time-stamp-custom-formats'.
2112 To turn this on on a per-file basis, insert anywhere in the file:
2113 #+STARTUP: customtime"
2117 (make-variable-buffer-local 'org-display-custom-times
)
2119 (defcustom org-time-stamp-custom-formats
2120 '("<%m/%d/%y %a>" .
"<%m/%d/%y %a %H:%M>") ; american
2121 "Custom formats for time stamps. See `format-time-string' for the syntax.
2122 These are overlayed over the default ISO format if the variable
2123 `org-display-custom-times' is set. Time like %H:%M should be at the
2124 end of the second format. The custom formats are also honored by export
2125 commands, if custom time display is turned on at the time of export."
2129 (defun org-time-stamp-format (&optional long inactive
)
2130 "Get the right format for a time string."
2131 (let ((f (if long
(cdr org-time-stamp-formats
)
2132 (car org-time-stamp-formats
))))
2134 (concat "[" (substring f
1 -
1) "]")
2137 (defcustom org-time-clocksum-format
"%d:%02d"
2138 "The format string used when creating CLOCKSUM lines, or when
2139 org-mode generates a time duration."
2143 (defcustom org-deadline-warning-days
14
2144 "No. of days before expiration during which a deadline becomes active.
2145 This variable governs the display in sparse trees and in the agenda.
2146 When 0 or negative, it means use this number (the absolute value of it)
2147 even if a deadline has a different individual lead time specified.
2149 Custom commands can set this variable in the options section."
2151 :group
'org-agenda-daily
/weekly
2154 (defcustom org-read-date-prefer-future t
2155 "Non-nil means, assume future for incomplete date input from user.
2156 This affects the following situations:
2157 1. The user gives a day, but no month.
2158 For example, if today is the 15th, and you enter \"3\", Org-mode will
2159 read this as the third of *next* month. However, if you enter \"17\",
2160 it will be considered as *this* month.
2161 2. The user gives a month but not a year.
2162 For example, if it is april and you enter \"feb 2\", this will be read
2163 as feb 2, *next* year. \"May 5\", however, will be this year.
2165 Currently this does not work for ISO week specifications.
2167 When this option is nil, the current month and year will always be used
2172 (defcustom org-read-date-display-live t
2173 "Non-nil means, display current interpretation of date prompt live.
2174 This display will be in an overlay, in the minibuffer."
2178 (defcustom org-read-date-popup-calendar t
2179 "Non-nil means, pop up a calendar when prompting for a date.
2180 In the calendar, the date can be selected with mouse-1. However, the
2181 minibuffer will also be active, and you can simply enter the date as well.
2182 When nil, only the minibuffer will be available."
2185 (if (fboundp 'defvaralias
)
2186 (defvaralias 'org-popup-calendar-for-date-prompt
2187 'org-read-date-popup-calendar
))
2189 (defcustom org-read-date-minibuffer-setup-hook nil
2190 "Hook to be used to set up keys for the date/time interface.
2191 Add key definitions to `minibuffer-local-map', which will be a temporary
2196 (defcustom org-extend-today-until
0
2197 "The hour when your day really ends. Must be an integer.
2198 This has influence for the following applications:
2199 - When switching the agenda to \"today\". It it is still earlier than
2200 the time given here, the day recognized as TODAY is actually yesterday.
2201 - When a date is read from the user and it is still before the time given
2202 here, the current date and time will be assumed to be yesterday, 23:59.
2203 Also, timestamps inserted in remember templates follow this rule.
2205 IMPORTANT: This is a feature whose implementation is and likely will
2206 remain incomplete. Really, it is only here because past midnight seems to
2207 be the favorite working time of John Wiegley :-)"
2211 (defcustom org-edit-timestamp-down-means-later nil
2212 "Non-nil means, S-down will increase the time in a time stamp.
2213 When nil, S-up will increase."
2217 (defcustom org-calendar-follow-timestamp-change t
2218 "Non-nil means, make the calendar window follow timestamp changes.
2219 When a timestamp is modified and the calendar window is visible, it will be
2220 moved to the new date."
2224 (defgroup org-tags nil
2225 "Options concerning tags in Org-mode."
2229 (defcustom org-tag-alist nil
2230 "List of tags allowed in Org-mode files.
2231 When this list is nil, Org-mode will base TAG input on what is already in the
2233 The value of this variable is an alist, the car of each entry must be a
2234 keyword as a string, the cdr may be a character that is used to select
2235 that tag through the fast-tag-selection interface.
2236 See the manual for details."
2240 (cons (string :tag
"Tag name")
2241 (character :tag
"Access char"))
2242 (const :tag
"Start radio group" (:startgroup
))
2243 (const :tag
"End radio group" (:endgroup
))
2244 (const :tag
"New line" (:newline
)))))
2246 (defcustom org-tag-persistent-alist nil
2247 "List of tags that will always appear in all Org-mode files.
2248 This is in addition to any in buffer settings or customizations
2250 When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
2251 The value of this variable is an alist, the car of each entry must be a
2252 keyword as a string, the cdr may be a character that is used to select
2253 that tag through the fast-tag-selection interface.
2254 See the manual for details.
2255 To disable these tags on a per-file basis, insert anywhere in the file:
2260 (cons (string :tag
"Tag name")
2261 (character :tag
"Access char"))
2262 (const :tag
"Start radio group" (:startgroup
))
2263 (const :tag
"End radio group" (:endgroup
))
2264 (const :tag
"New line" (:newline
)))))
2266 (defvar org-file-tags nil
2267 "List of tags that can be inherited by all entries in the file.
2268 The tags will be inherited if the variable `org-use-tag-inheritance'
2269 says they should be.
2270 This variable is populated from #+TAG lines.")
2272 (defcustom org-use-fast-tag-selection
'auto
2273 "Non-nil means, use fast tag selection scheme.
2274 This is a special interface to select and deselect tags with single keys.
2275 When nil, fast selection is never used.
2276 When the symbol `auto', fast selection is used if and only if selection
2277 characters for tags have been configured, either through the variable
2278 `org-tag-alist' or through a #+TAGS line in the buffer.
2279 When t, fast selection is always used and selection keys are assigned
2280 automatically if necessary."
2283 (const :tag
"Always" t
)
2284 (const :tag
"Never" nil
)
2285 (const :tag
"When selection characters are configured" 'auto
)))
2287 (defcustom org-fast-tag-selection-single-key nil
2288 "Non-nil means, fast tag selection exits after first change.
2289 When nil, you have to press RET to exit it.
2290 During fast tag selection, you can toggle this flag with `C-c'.
2291 This variable can also have the value `expert'. In this case, the window
2292 displaying the tags menu is not even shown, until you press C-c again."
2295 (const :tag
"No" nil
)
2296 (const :tag
"Yes" t
)
2297 (const :tag
"Expert" expert
)))
2299 (defvar org-fast-tag-selection-include-todo nil
2300 "Non-nil means, fast tags selection interface will also offer TODO states.
2301 This is an undocumented feature, you should not rely on it.")
2303 (defcustom org-tags-column
(if (featurep 'xemacs
) -
76 -
77)
2304 "The column to which tags should be indented in a headline.
2305 If this number is positive, it specifies the column. If it is negative,
2306 it means that the tags should be flushright to that column. For example,
2307 -80 works well for a normal 80 character screen."
2311 (defcustom org-auto-align-tags t
2312 "Non-nil means, realign tags after pro/demotion of TODO state change.
2313 These operations change the length of a headline and therefore shift
2314 the tags around. With this options turned on, after each such operation
2315 the tags are again aligned to `org-tags-column'."
2319 (defcustom org-use-tag-inheritance t
2320 "Non-nil means, tags in levels apply also for sublevels.
2321 When nil, only the tags directly given in a specific line apply there.
2322 This may also be a list of tags that should be inherited, or a regexp that
2323 matches tags that should be inherited. Additional control is possible
2324 with the variable `org-tags-exclude-from-inheritance' which gives an
2325 explicit list of tags to be excluded from inheritance., even if the value of
2326 `org-use-tag-inheritance' would select it for inheritance.
2328 If this option is t, a match early-on in a tree can lead to a large
2329 number of matches in the subtree when constructing the agenda or creating
2330 a sparse tree. If you only want to see the first match in a tree during
2331 a search, check out the variable `org-tags-match-list-sublevels'."
2334 (const :tag
"Not" nil
)
2335 (const :tag
"Always" t
)
2336 (repeat :tag
"Specific tags" (string :tag
"Tag"))
2337 (regexp :tag
"Tags matched by regexp")))
2339 (defcustom org-tags-exclude-from-inheritance nil
2340 "List of tags that should never be inherited.
2341 This is a way to exclude a few tags from inheritance. For way to do
2342 the opposite, to actively allow inheritance for selected tags,
2343 see the variable `org-use-tag-inheritance'."
2345 :type
'(repeat (string :tag
"Tag")))
2347 (defun org-tag-inherit-p (tag)
2348 "Check if TAG is one that should be inherited."
2350 ((member tag org-tags-exclude-from-inheritance
) nil
)
2351 ((eq org-use-tag-inheritance t
) t
)
2352 ((not org-use-tag-inheritance
) nil
)
2353 ((stringp org-use-tag-inheritance
)
2354 (string-match org-use-tag-inheritance tag
))
2355 ((listp org-use-tag-inheritance
)
2356 (member tag org-use-tag-inheritance
))
2357 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
2359 (defcustom org-tags-match-list-sublevels t
2360 "Non-nil means list also sublevels of headlines matching a search.
2361 This variable applies to tags/property searches, and also to stuck
2362 projects because this search is based on a tags match as well.
2364 When set to the symbol `indented', sublevels are indented with
2367 Because of tag inheritance (see variable `org-use-tag-inheritance'),
2368 the sublevels of a headline matching a tag search often also match
2369 the same search. Listing all of them can create very long lists.
2370 Setting this variable to nil causes subtrees of a match to be skipped.
2372 This variable is semi-obsolete and probably should always be true. It
2373 is better to limit inheritance to certain tags using the variables
2374 `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
2377 (const :tag
"No, don't list them" nil
)
2378 (const :tag
"Yes, do list them" t
)
2379 (const :tag
"List them, indented with leading dots" indented
)))
2381 (defcustom org-tags-sort-function nil
2382 "When set, tags are sorted using this function as a comparator"
2385 (const :tag
"No sorting" nil
)
2386 (const :tag
"Alphabetical" string
<)
2387 (const :tag
"Reverse alphabetical" string
>)
2388 (function :tag
"Custom function" nil
)))
2390 (defvar org-tags-history nil
2391 "History of minibuffer reads for tags.")
2392 (defvar org-last-tags-completion-table nil
2393 "The last used completion table for tags.")
2394 (defvar org-after-tags-change-hook nil
2395 "Hook that is run after the tags in a line have changed.")
2397 (defgroup org-properties nil
2398 "Options concerning properties in Org-mode."
2399 :tag
"Org Properties"
2402 (defcustom org-property-format
"%-10s %s"
2403 "How property key/value pairs should be formatted by `indent-line'.
2404 When `indent-line' hits a property definition, it will format the line
2405 according to this format, mainly to make sure that the values are
2406 lined-up with respect to each other."
2407 :group
'org-properties
2410 (defcustom org-use-property-inheritance nil
2411 "Non-nil means, properties apply also for sublevels.
2413 This setting is chiefly used during property searches. Turning it on can
2414 cause significant overhead when doing a search, which is why it is not
2417 When nil, only the properties directly given in the current entry count.
2418 When t, every property is inherited. The value may also be a list of
2419 properties that should have inheritance, or a regular expression matching
2420 properties that should be inherited.
2422 However, note that some special properties use inheritance under special
2423 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
2424 and the properties ending in \"_ALL\" when they are used as descriptor
2425 for valid values of a property.
2427 Note for programmers:
2428 When querying an entry with `org-entry-get', you can control if inheritance
2429 should be used. By default, `org-entry-get' looks only at the local
2430 properties. You can request inheritance by setting the inherit argument
2431 to t (to force inheritance) or to `selective' (to respect the setting
2433 :group
'org-properties
2435 (const :tag
"Not" nil
)
2436 (const :tag
"Always" t
)
2437 (repeat :tag
"Specific properties" (string :tag
"Property"))
2438 (regexp :tag
"Properties matched by regexp")))
2440 (defun org-property-inherit-p (property)
2441 "Check if PROPERTY is one that should be inherited."
2443 ((eq org-use-property-inheritance t
) t
)
2444 ((not org-use-property-inheritance
) nil
)
2445 ((stringp org-use-property-inheritance
)
2446 (string-match org-use-property-inheritance property
))
2447 ((listp org-use-property-inheritance
)
2448 (member property org-use-property-inheritance
))
2449 (t (error "Invalid setting of `org-use-property-inheritance'"))))
2451 (defcustom org-columns-default-format
"%25ITEM %TODO %3PRIORITY %TAGS"
2452 "The default column format, if no other format has been defined.
2453 This variable can be set on the per-file basis by inserting a line
2455 #+COLUMNS: %25ITEM ....."
2456 :group
'org-properties
2459 (defcustom org-columns-ellipses
".."
2460 "The ellipses to be used when a field in column view is truncated.
2461 When this is the empty string, as many characters as possible are shown,
2462 but then there will be no visual indication that the field has been truncated.
2463 When this is a string of length N, the last N characters of a truncated
2464 field are replaced by this string. If the column is narrower than the
2465 ellipses string, only part of the ellipses string will be shown."
2466 :group
'org-properties
2469 (defcustom org-columns-modify-value-for-display-function nil
2470 "Function that modifies values for display in column view.
2471 For example, it can be used to cut out a certain part from a time stamp.
2472 The function must take 2 arguments:
2474 column-title The title of the column (*not* the property name)
2475 value The value that should be modified.
2477 The function should return the value that should be displayed,
2478 or nil if the normal value should be used."
2479 :group
'org-properties
2482 (defcustom org-effort-property
"Effort"
2483 "The property that is being used to keep track of effort estimates.
2484 Effort estimates given in this property need to have the format H:MM."
2485 :group
'org-properties
2486 :group
'org-progress
2487 :type
'(string :tag
"Property"))
2489 (defconst org-global-properties-fixed
2490 '(("VISIBILITY_ALL" .
"folded children content all")
2491 ("CLOCK_MODELINE_TOTAL_ALL" .
"current today repeat all auto"))
2492 "List of property/value pairs that can be inherited by any entry.
2494 These are fixed values, for the preset properties. The user variable
2495 that can be used to add to this list is `org-global-properties'.
2497 The entries in this list are cons cells where the car is a property
2498 name and cdr is a string with the value. If the value represents
2499 multiple items like an \"_ALL\" property, separate the items by
2502 (defcustom org-global-properties nil
2503 "List of property/value pairs that can be inherited by any entry.
2505 This list will be combined with the constant `org-global-properties-fixed'.
2507 The entries in this list are cons cells where the car is a property
2508 name and cdr is a string with the value.
2510 You can set buffer-local values for the same purpose in the variable
2511 `org-file-properties' this by adding lines like
2513 #+PROPERTY: NAME VALUE"
2514 :group
'org-properties
2516 (cons (string :tag
"Property")
2517 (string :tag
"Value"))))
2519 (defvar org-file-properties nil
2520 "List of property/value pairs that can be inherited by any entry.
2521 Valid for the current buffer.
2522 This variable is populated from #+PROPERTY lines.")
2523 (make-variable-buffer-local 'org-file-properties
)
2525 (defgroup org-agenda nil
2526 "Options concerning agenda views in Org-mode."
2530 (defvar org-category nil
2531 "Variable used by org files to set a category for agenda display.
2532 Such files should use a file variable to set it, for example
2534 # -*- mode: org; org-category: \"ELisp\"
2536 or contain a special line
2540 If the file does not specify a category, then file's base name
2542 (make-variable-buffer-local 'org-category
)
2543 (put 'org-category
'safe-local-variable
'(lambda (x) (or (symbolp x
) (stringp x
))))
2545 (defcustom org-agenda-files nil
2546 "The files to be used for agenda display.
2547 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
2548 \\[org-remove-file]. You can also use customize to edit the list.
2550 If an entry is a directory, all files in that directory that are matched by
2551 `org-agenda-file-regexp' will be part of the file list.
2553 If the value of the variable is not a list but a single file name, then
2554 the list of agenda files is actually stored and maintained in that file, one
2555 agenda file per line."
2558 (repeat :tag
"List of files and directories" file
)
2559 (file :tag
"Store list in a file\n" :value
"~/.agenda_files")))
2561 (defcustom org-agenda-file-regexp
"\\`[^.].*\\.org\\'"
2562 "Regular expression to match files for `org-agenda-files'.
2563 If any element in the list in that variable contains a directory instead
2564 of a normal file, all files in that directory that are matched by this
2565 regular expression will be included."
2569 (defcustom org-agenda-text-search-extra-files nil
2570 "List of extra files to be searched by text search commands.
2571 These files will be search in addition to the agenda files by the
2572 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
2573 Note that these files will only be searched for text search commands,
2574 not for the other agenda views like todo lists, tag searches or the weekly
2575 agenda. This variable is intended to list notes and possibly archive files
2576 that should also be searched by these two commands.
2577 In fact, if the first element in the list is the symbol `agenda-archives',
2578 than all archive files of all agenda files will be added to the search
2581 :type
'(set :greedy t
2582 (const :tag
"Agenda Archives" agenda-archives
)
2583 (repeat :inline t
(file))))
2585 (if (fboundp 'defvaralias
)
2586 (defvaralias 'org-agenda-multi-occur-extra-files
2587 'org-agenda-text-search-extra-files
))
2589 (defcustom org-agenda-skip-unavailable-files nil
2590 "Non-nil means to just skip non-reachable files in `org-agenda-files'.
2591 A nil value means to remove them, after a query, from the list."
2595 (defcustom org-calendar-to-agenda-key
[?c
]
2596 "The key to be installed in `calendar-mode-map' for switching to the agenda.
2597 The command `org-calendar-goto-agenda' will be bound to this key. The
2598 default is the character `c' because then `c' can be used to switch back and
2599 forth between agenda and calendar."
2603 (defcustom org-calendar-agenda-action-key
[?k
]
2604 "The key to be installed in `calendar-mode-map' for agenda-action.
2605 The command `org-agenda-action' will be bound to this key. The
2606 default is the character `k' because we use the same key in the agenda."
2610 (eval-after-load "calendar"
2612 (org-defkey calendar-mode-map org-calendar-to-agenda-key
2613 'org-calendar-goto-agenda
)
2614 (org-defkey calendar-mode-map org-calendar-agenda-action-key
2615 'org-agenda-action
)))
2617 (defgroup org-latex nil
2618 "Options for embedding LaTeX code into Org-mode."
2622 (defcustom org-format-latex-options
2623 '(:foreground default
:background default
:scale
1.0
2624 :html-foreground
"Black" :html-background
"Transparent" :html-scale
1.0
2625 :matchers
("begin" "$1" "$" "$$" "\\(" "\\["))
2626 "Options for creating images from LaTeX fragments.
2627 This is a property list with the following properties:
2628 :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
2629 `default' means use the foreground of the default face.
2630 :background the background color, or \"Transparent\".
2631 `default' means use the background of the default face.
2632 :scale a scaling factor for the size of the images.
2633 :html-foreground, :html-background, :html-scale
2634 the same numbers for HTML export.
2635 :matchers a list indicating which matchers should be used to
2636 find LaTeX fragments. Valid members of this list are:
2637 \"begin\" find environments
2638 \"$1\" find single characters surrounded by $.$
2639 \"$\" find math expressions surrounded by $...$
2640 \"$$\" find math expressions surrounded by $$....$$
2641 \"\\(\" find math expressions surrounded by \\(...\\)
2642 \"\\ [\" find math expressions surrounded by \\ [...\\]"
2646 (defcustom org-format-latex-header
"\\documentclass{article}
2647 \\usepackage{fullpage} % do not remove
2648 \\usepackage{amssymb}
2649 \\usepackage[usenames]{color}
2650 \\usepackage{amsmath}
2651 \\usepackage{latexsym}
2652 \\usepackage[mathscr]{eucal}
2653 \\pagestyle{empty} % do not remove"
2654 "The document header used for processing LaTeX fragments."
2659 (defgroup org-font-lock nil
2660 "Font-lock settings for highlighting in Org-mode."
2661 :tag
"Org Font Lock"
2664 (defcustom org-level-color-stars-only nil
2665 "Non-nil means fontify only the stars in each headline.
2666 When nil, the entire headline is fontified.
2667 Changing it requires restart of `font-lock-mode' to become effective
2668 also in regions already fontified."
2669 :group
'org-font-lock
2672 (defcustom org-hide-leading-stars nil
2673 "Non-nil means, hide the first N-1 stars in a headline.
2674 This works by using the face `org-hide' for these stars. This
2675 face is white for a light background, and black for a dark
2676 background. You may have to customize the face `org-hide' to
2678 Changing it requires restart of `font-lock-mode' to become effective
2679 also in regions already fontified.
2680 You may also set this on a per-file basis by adding one of the following
2681 lines to the buffer:
2683 #+STARTUP: hidestars
2684 #+STARTUP: showstars"
2685 :group
'org-font-lock
2688 (defcustom org-fontify-done-headline nil
2689 "Non-nil means, change the face of a headline if it is marked DONE.
2690 Normally, only the TODO/DONE keyword indicates the state of a headline.
2691 When this is non-nil, the headline after the keyword is set to the
2692 `org-headline-done' as an additional indication."
2693 :group
'org-font-lock
2696 (defcustom org-fontify-emphasized-text t
2697 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
2698 Changing this variable requires a restart of Emacs to take effect."
2699 :group
'org-font-lock
2702 (defcustom org-fontify-whole-heading-line nil
2703 "Non-nil means fontify the whole line for headings.
2704 This is useful when setting a background color for the
2706 :group
'org-font-lock
2709 (defcustom org-highlight-latex-fragments-and-specials nil
2710 "Non-nil means, fontify what is treated specially by the exporters."
2711 :group
'org-font-lock
2714 (defcustom org-hide-emphasis-markers nil
2715 "Non-nil mean font-lock should hide the emphasis marker characters."
2716 :group
'org-font-lock
2719 (defvar org-emph-re nil
2720 "Regular expression for matching emphasis.")
2721 (defvar org-verbatim-re nil
2722 "Regular expression for matching verbatim text.")
2723 (defvar org-emphasis-regexp-components
) ; defined just below
2724 (defvar org-emphasis-alist
) ; defined just below
2725 (defun org-set-emph-re (var val
)
2726 "Set variable and compute the emphasis regular expression."
2728 (when (and (boundp 'org-emphasis-alist
)
2729 (boundp 'org-emphasis-regexp-components
)
2730 org-emphasis-alist org-emphasis-regexp-components
)
2731 (let* ((e org-emphasis-regexp-components
)
2737 (body1 (concat body
"*?"))
2738 (markers (mapconcat 'car org-emphasis-alist
""))
2739 (vmarkers (mapconcat
2740 (lambda (x) (if (eq (nth 4 x
) 'verbatim
) (car x
) ""))
2741 org-emphasis-alist
"")))
2742 ;; make sure special characters appear at the right position in the class
2743 (if (string-match "\\^" markers
)
2744 (setq markers
(concat (replace-match "" t t markers
) "^")))
2745 (if (string-match "-" markers
)
2746 (setq markers
(concat (replace-match "" t t markers
) "-")))
2747 (if (string-match "\\^" vmarkers
)
2748 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "^")))
2749 (if (string-match "-" vmarkers
)
2750 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "-")))
2752 (setq body1
(concat body1
"\\(?:\n" body
"*?\\)\\{0,"
2753 (int-to-string nl
) "\\}")))
2756 (concat "\\([" pre
"]\\|^\\)"
2758 "\\([" markers
"]\\)"
2766 "\\([" post
"]\\|$\\)"))
2767 (setq org-verbatim-re
2768 (concat "\\([" pre
"]\\|^\\)"
2770 "\\([" vmarkers
"]\\)"
2778 "\\([" post
"]\\|$\\)")))))
2780 (defcustom org-emphasis-regexp-components
2781 '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
2782 "Components used to build the regular expression for emphasis.
2783 This is a list with 6 entries. Terminology: In an emphasis string
2784 like \" *strong word* \", we call the initial space PREMATCH, the final
2785 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
2786 and \"trong wor\" is the body. The different components in this variable
2787 specify what is allowed/forbidden in each part:
2789 pre Chars allowed as prematch. Beginning of line will be allowed too.
2790 post Chars allowed as postmatch. End of line will be allowed too.
2791 border The chars *forbidden* as border characters.
2792 body-regexp A regexp like \".\" to match a body character. Don't use
2793 non-shy groups here, and don't allow newline here.
2794 newline The maximum number of newlines allowed in an emphasis exp.
2796 Use customize to modify this, or restart Emacs after changing it."
2797 :group
'org-font-lock
2798 :set
'org-set-emph-re
2800 (sexp :tag
"Allowed chars in pre ")
2801 (sexp :tag
"Allowed chars in post ")
2802 (sexp :tag
"Forbidden chars in border ")
2803 (sexp :tag
"Regexp for body ")
2804 (integer :tag
"number of newlines allowed")
2805 (option (boolean :tag
"Please ignore this button"))))
2807 (defcustom org-emphasis-alist
2808 `(("*" bold
"<b>" "</b>")
2809 ("/" italic
"<i>" "</i>")
2810 ("_" underline
"<span style=\"text-decoration:underline;\">" "</span>")
2811 ("=" org-code
"<code>" "</code>" verbatim
)
2812 ("~" org-verbatim
"<code>" "</code>" verbatim
)
2813 ("+" ,(if (featurep 'xemacs
) 'org-table
'(:strike-through t
))
2816 "Special syntax for emphasized text.
2817 Text starting and ending with a special character will be emphasized, for
2818 example *bold*, _underlined_ and /italic/. This variable sets the marker
2819 characters, the face to be used by font-lock for highlighting in Org-mode
2820 Emacs buffers, and the HTML tags to be used for this.
2821 For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
2822 Use customize to modify this, or restart Emacs after changing it."
2823 :group
'org-font-lock
2824 :set
'org-set-emph-re
2827 (string :tag
"Marker character")
2829 (face :tag
"Font-lock-face")
2830 (plist :tag
"Face property list"))
2831 (string :tag
"HTML start tag")
2832 (string :tag
"HTML end tag")
2833 (option (const verbatim
)))))
2835 (defvar org-protecting-blocks
2836 '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
2837 "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
2838 This is needed for font-lock setup.")
2840 ;;; Miscellaneous options
2842 (defgroup org-completion nil
2843 "Completion in Org-mode."
2844 :tag
"Org Completion"
2847 (defcustom org-completion-use-ido nil
2848 "Non-nil means, use ido completion wherever possible.
2849 Note that `ido-mode' must be active for this variable to be relevant.
2850 If you decide to turn this variable on, you might well want to turn off
2851 `org-outline-path-complete-in-steps'."
2852 :group
'org-completion
2855 (defcustom org-completion-fallback-command
'hippie-expand
2856 "The expansion command called by \\[org-complete] in normal context.
2857 Normal means, no org-mode-specific context."
2858 :group
'org-completion
2861 ;;; Functions and variables from ther packages
2862 ;; Declared here to avoid compiler warnings
2865 (defvar outline-mode-menu-heading
)
2866 (defvar outline-mode-menu-show
)
2867 (defvar outline-mode-menu-hide
)
2868 (defvar zmacs-regions
) ; XEmacs regions
2871 (defvar mark-active
)
2874 (declare-function calendar-absolute-from-iso
"cal-iso" (date))
2875 (declare-function calendar-forward-day
"cal-move" (arg))
2876 (declare-function calendar-goto-date
"cal-move" (date))
2877 (declare-function calendar-goto-today
"cal-move" ())
2878 (declare-function calendar-iso-from-absolute
"cal-iso" (date))
2879 (defvar calc-embedded-close-formula
)
2880 (defvar calc-embedded-open-formula
)
2881 (declare-function cdlatex-tab
"ext:cdlatex" ())
2882 (declare-function dired-get-filename
"dired" (&optional localp no-error-if-not-filep
))
2883 (defvar font-lock-unfontify-region-function
)
2884 (declare-function iswitchb-mode
"iswitchb" (&optional arg
))
2885 (declare-function iswitchb-read-buffer
(prompt &optional default require-match start matches-set
))
2886 (defvar iswitchb-temp-buflist
)
2887 (declare-function org-gnus-follow-link
"org-gnus" (&optional group article
))
2888 (defvar org-agenda-tags-todo-honor-ignore-options
)
2889 (declare-function org-agenda-skip
"org-agenda" ())
2890 (declare-function org-format-agenda-item
"org-agenda"
2891 (extra txt
&optional category tags dotime noprefix remove-re
))
2892 (declare-function org-agenda-new-marker
"org-agenda" (&optional pos
))
2893 (declare-function org-agenda-change-all-lines
"org-agenda"
2894 (newhead hdmarker
&optional fixface just-this
))
2895 (declare-function org-agenda-set-restriction-lock
"org-agenda" (&optional type
))
2896 (declare-function org-agenda-maybe-redo
"org-agenda" ())
2897 (declare-function org-agenda-save-markers-for-cut-and-paste
"org-agenda"
2899 (declare-function org-agenda-copy-local-variable
"org-agenda" (var))
2900 (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
2901 "org-agenda" (&optional end
))
2902 (declare-function org-inlinetask-remove-END-maybe
"org-inlinetask" ())
2903 (declare-function org-indent-mode
"org-indent" (arg))
2904 (declare-function parse-time-string
"parse-time" (string))
2905 (declare-function remember
"remember" (&optional initial
))
2906 (declare-function remember-buffer-desc
"remember" ())
2907 (declare-function remember-finalize
"remember" ())
2908 (defvar remember-save-after-remembering
)
2909 (defvar remember-data-file
)
2910 (defvar remember-register
)
2911 (defvar remember-buffer
)
2912 (defvar remember-handler-functions
)
2913 (defvar remember-annotation-functions
)
2914 (defvar texmathp-why
)
2915 (declare-function speedbar-line-directory
"speedbar" (&optional depth
))
2916 (declare-function table--at-cell-p
"table" (position &optional object at-column
))
2918 (defvar w3m-current-url
)
2919 (defvar w3m-current-title
)
2921 (defvar org-latex-regexps
)
2923 ;;; Autoload and prepare some org modules
2925 ;; Some table stuff that needs to be defined here, because it is used
2926 ;; by the functions setting up org-mode or checking for table context.
2928 (defconst org-table-any-line-regexp
"^[ \t]*\\(|\\|\\+-[-+]\\)"
2929 "Detects an org-type or table-type table.")
2930 (defconst org-table-line-regexp
"^[ \t]*|"
2931 "Detects an org-type table line.")
2932 (defconst org-table-dataline-regexp
"^[ \t]*|[^-]"
2933 "Detects an org-type table line.")
2934 (defconst org-table-hline-regexp
"^[ \t]*|-"
2935 "Detects an org-type table hline.")
2936 (defconst org-table1-hline-regexp
"^[ \t]*\\+-[-+]"
2937 "Detects a table-type table hline.")
2938 (defconst org-table-any-border-regexp
"^[ \t]*[^|+ \t]"
2939 "Searching from within a table (any type) this finds the first line
2940 outside the table.")
2942 ;; Autoload the functions in org-table.el that are needed by functions here.
2945 (org-autoload "org-table"
2946 '(org-table-align org-table-begin org-table-blank-field
2947 org-table-convert org-table-convert-region org-table-copy-down
2948 org-table-copy-region org-table-create
2949 org-table-create-or-convert-from-region
2950 org-table-create-with-table.el org-table-current-dline
2951 org-table-cut-region org-table-delete-column org-table-edit-field
2952 org-table-edit-formulas org-table-end org-table-eval-formula
2953 org-table-export org-table-field-info
2954 org-table-get-stored-formulas org-table-goto-column
2955 org-table-hline-and-move org-table-import org-table-insert-column
2956 org-table-insert-hline org-table-insert-row org-table-iterate
2957 org-table-justify-field-maybe org-table-kill-row
2958 org-table-maybe-eval-formula org-table-maybe-recalculate-line
2959 org-table-move-column org-table-move-column-left
2960 org-table-move-column-right org-table-move-row
2961 org-table-move-row-down org-table-move-row-up
2962 org-table-next-field org-table-next-row org-table-paste-rectangle
2963 org-table-previous-field org-table-recalculate
2964 org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
2965 org-table-toggle-coordinate-overlays
2966 org-table-toggle-formula-debugger org-table-wrap-region
2967 orgtbl-mode turn-on-orgtbl org-table-to-lisp
)))
2969 (defun org-at-table-p (&optional table-type
)
2970 "Return t if the cursor is inside an org-type table.
2971 If TABLE-TYPE is non-nil, also check for table.el-type tables."
2972 (if org-enable-table-editor
2974 (beginning-of-line 1)
2975 (looking-at (if table-type org-table-any-line-regexp
2976 org-table-line-regexp
)))
2978 (defsubst org-table-p
() (org-at-table-p))
2980 (defun org-at-table.el-p
()
2981 "Return t if and only if we are at a table.el table."
2982 (and (org-at-table-p 'any
)
2984 (goto-char (org-table-begin 'any
))
2985 (looking-at org-table1-hline-regexp
))))
2986 (defun org-table-recognize-table.el
()
2987 "If there is a table.el table nearby, recognize it and move into it."
2988 (if org-table-tab-recognizes-table.el
2989 (if (org-at-table.el-p
)
2991 (beginning-of-line 1)
2992 (if (looking-at org-table-dataline-regexp
)
2994 (if (looking-at org-table1-hline-regexp
)
2996 (beginning-of-line 2)
2997 (if (looking-at org-table-any-border-regexp
)
2998 (beginning-of-line -
1)))))
2999 (if (re-search-forward "|" (org-table-end t
) t
)
3002 (if (table--at-cell-p (point))
3004 (message "recognizing table.el table...")
3005 (table-recognize-table)
3006 (message "recognizing table.el table...done")))
3007 (error "This should not happen..."))
3012 (defun org-at-table-hline-p ()
3013 "Return t if the cursor is inside a hline in a table."
3014 (if org-enable-table-editor
3016 (beginning-of-line 1)
3017 (looking-at org-table-hline-regexp
))
3020 (defvar org-table-clean-did-remove-column nil
)
3022 (defun org-table-map-tables (function)
3023 "Apply FUNCTION to the start of all tables in the buffer."
3027 (goto-char (point-min))
3028 (while (re-search-forward org-table-any-line-regexp nil t
)
3029 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
3030 (beginning-of-line 1)
3031 (if (looking-at org-table-line-regexp
)
3032 (save-excursion (funcall function
)))
3033 (re-search-forward org-table-any-border-regexp nil
1))))
3034 (message "Mapping tables: done"))
3036 ;; Declare and autoload functions from org-exp.el & Co
3038 (declare-function org-default-export-plist
"org-exp")
3039 (declare-function org-infile-export-plist
"org-exp")
3040 (declare-function org-get-current-options
"org-exp")
3042 (org-autoload "org-exp"
3043 '(org-export org-export-visible
3044 org-insert-export-options-template
3045 org-table-clean-before-export
))
3046 (org-autoload "org-ascii"
3047 '(org-export-as-ascii org-export-ascii-preprocess
3048 org-export-as-ascii-to-buffer org-replace-region-by-ascii
3049 org-export-region-as-ascii
))
3050 (org-autoload "org-html"
3051 '(org-export-as-html-and-open
3052 org-export-as-html-batch org-export-as-html-to-buffer
3053 org-replace-region-by-html org-export-region-as-html
3054 org-export-as-html
))
3055 (org-autoload "org-icalendar"
3056 '(org-export-icalendar-this-file
3057 org-export-icalendar-all-agenda-files
3058 org-export-icalendar-combine-agenda-files
))
3059 (org-autoload "org-xoxo" '(org-export-as-xoxo)))
3061 ;; Declare and autoload functions from org-agenda.el
3064 (org-autoload "org-agenda"
3065 '(org-agenda org-agenda-list org-search-view
3066 org-todo-list org-tags-view org-agenda-list-stuck-projects
3067 org-diary org-agenda-to-appt
3068 org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
)))
3070 ;; Autoload org-remember
3073 (org-autoload "org-remember"
3074 '(org-remember-insinuate org-remember-annotation
3075 org-remember-apply-template org-remember org-remember-handler
)))
3077 ;; Autoload org-clock.el
3080 (declare-function org-clock-save-markers-for-cut-and-paste
"org-clock"
3082 (declare-function org-clock-update-mode-line
"org-clock" ())
3083 (defvar org-clock-start-time
)
3084 (defvar org-clock-marker
(make-marker)
3085 "Marker recording the last clock-in.")
3086 (defun org-clock-is-active ()
3087 "Return non-nil if clock is currently running.
3088 The return value is actually the clock marker."
3089 (marker-buffer org-clock-marker
))
3094 '(org-clock-in org-clock-out org-clock-cancel
3095 org-clock-goto org-clock-sum org-clock-display
3096 org-clock-remove-overlays org-clock-report
3097 org-clocktable-shift org-dblock-write
:clocktable
3098 org-get-clocktable
)))
3100 (defun org-clock-update-time-maybe ()
3101 "If this is a CLOCK line, update it and return t.
3102 Otherwise, return nil."
3105 (beginning-of-line 1)
3106 (skip-chars-forward " \t")
3107 (when (looking-at org-clock-string
)
3108 (let ((re (concat "[ \t]*" org-clock-string
3109 " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
3110 "\\([ \t]*=>.*\\)?\\)?"))
3113 ((not (looking-at re
))
3115 ((not (match-end 2))
3116 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
3117 (> org-clock-marker
(point))
3118 (<= org-clock-marker
(point-at-eol)))
3119 ;; The clock is running here
3120 (setq org-clock-start-time
3122 (org-parse-time-string (match-string 1))))
3123 (org-clock-update-mode-line)))
3125 (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
3127 (setq ts
(match-string 1)
3128 te
(match-string 3))
3129 (setq s
(- (time-to-seconds
3130 (apply 'encode-time
(org-parse-time-string te
)))
3132 (apply 'encode-time
(org-parse-time-string ts
))))
3135 h
(floor (/ s
3600))
3139 (insert " => " (format (if neg
"-%d:%02d" "%2d:%02d") h m
))
3142 (defun org-check-running-clock ()
3143 "Check if the current buffer contains the running clock.
3144 If yes, offer to stop it and to save the buffer with the changes."
3145 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
3146 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
3149 (when (y-or-n-p "Save changed buffer?")
3152 (defun org-clocktable-try-shift (dir n
)
3153 "Check if this line starts a clock table, if yes, shift the time block."
3154 (when (org-match-line "#\\+BEGIN: clocktable\\>")
3155 (org-clocktable-shift dir n
)))
3157 ;; Autoload org-timer.el
3162 '(org-timer-start org-timer org-timer-item
3163 org-timer-change-times-in-region
3165 org-timer-reset-timers
3166 org-timer-show-remaining-time
)))
3168 ;; Autoload org-feed.el
3173 '(org-feed-update org-feed-update-all org-feed-goto-inbox
)))
3176 ;; Autoload org-indent.el
3181 '(org-indent-mode)))
3183 ;; Autoload archiving code
3184 ;; The stuff that is needed for cycling and tags has to be defined here.
3186 (defgroup org-archive nil
3187 "Options concerning archiving in Org-mode."
3189 :group
'org-structure
)
3191 (defcustom org-archive-location
"%s_archive::"
3192 "The location where subtrees should be archived.
3194 The value of this variable is a string, consisting of two parts,
3195 separated by a double-colon. The first part is a filename and
3196 the second part is a headline.
3198 When the filename is omitted, archiving happens in the same file.
3199 %s in the filename will be replaced by the current file
3200 name (without the directory part). Archiving to a different file
3201 is useful to keep archived entries from contributing to the
3204 The archived entries will be filed as subtrees of the specified
3205 headline. When the headline is omitted, the subtrees are simply
3206 filed away at the end of the file, as top-level entries. Also in
3207 the heading you can use %s to represent the file name, this can be
3208 useful when using the same archive for a number of different files.
3210 Here are a few examples:
3212 If the current file is Projects.org, archive in file
3213 Projects.org_archive, as top-level trees. This is the default.
3215 \"::* Archived Tasks\"
3216 Archive in the current file, under the top-level headline
3217 \"* Archived Tasks\".
3219 \"~/org/archive.org::\"
3220 Archive in file ~/org/archive.org (absolute path), as top-level trees.
3222 \"~/org/archive.org::From %s\"
3223 Archive in file ~/org/archive.org (absolute path), und headlines
3224 \"From FILENAME\" where file name is the current file name.
3226 \"basement::** Finished Tasks\"
3227 Archive in file ./basement (relative path), as level 3 trees
3228 below the level 2 heading \"** Finished Tasks\".
3230 You may set this option on a per-file basis by adding to the buffer a
3233 #+ARCHIVE: basement::** Finished Tasks
3235 You may also define it locally for a subtree by setting an ARCHIVE property
3236 in the entry. If such a property is found in an entry, or anywhere up
3237 the hierarchy, it will be used."
3241 (defcustom org-archive-tag
"ARCHIVE"
3242 "The tag that marks a subtree as archived.
3243 An archived subtree does not open during visibility cycling, and does
3244 not contribute to the agenda listings.
3245 After changing this, font-lock must be restarted in the relevant buffers to
3246 get the proper fontification."
3248 :group
'org-keywords
3251 (defcustom org-agenda-skip-archived-trees t
3252 "Non-nil means, the agenda will skip any items located in archived trees.
3253 An archived tree is a tree marked with the tag ARCHIVE. The use of this
3254 variable is no longer recommended, you should leave it at the value t.
3255 Instead, use the key `v' to cycle the archives-mode in the agenda."
3257 :group
'org-agenda-skip
3260 (defcustom org-columns-skip-arrchived-trees t
3261 "Non-nil means, irgnore archived trees when creating column view."
3263 :group
'org-properties
3266 (defcustom org-cycle-open-archived-trees nil
3267 "Non-nil means, `org-cycle' will open archived trees.
3268 An archived tree is a tree marked with the tag ARCHIVE.
3269 When nil, archived trees will stay folded. You can still open them with
3270 normal outline commands like `show-all', but not with the cycling commands."
3275 (defcustom org-sparse-tree-open-archived-trees nil
3276 "Non-nil means sparse tree construction shows matches in archived trees.
3277 When nil, matches in these trees are highlighted, but the trees are kept in
3280 :group
'org-sparse-trees
3283 (defun org-cycle-hide-archived-subtrees (state)
3284 "Re-hide all archived subtrees after a visibility state change."
3285 (when (and (not org-cycle-open-archived-trees
)
3286 (not (memq state
'(overview folded
))))
3288 (let* ((globalp (memq state
'(contents all
)))
3289 (beg (if globalp
(point-min) (point)))
3290 (end (if globalp
(point-max) (org-end-of-subtree t
))))
3291 (org-hide-archived-subtrees beg end
)
3293 (if (looking-at (concat ".*:" org-archive-tag
":"))
3294 (message "%s" (substitute-command-keys
3295 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
3297 (defun org-force-cycle-archived ()
3298 "Cycle subtree even if it is archived."
3300 (setq this-command
'org-cycle
)
3301 (let ((org-cycle-open-archived-trees t
))
3302 (call-interactively 'org-cycle
)))
3304 (defun org-hide-archived-subtrees (beg end
)
3305 "Re-hide all archived subtrees after a visibility state change."
3307 (let* ((re (concat ":" org-archive-tag
":")))
3309 (while (re-search-forward re end t
)
3310 (and (org-on-heading-p) (hide-subtree))
3311 (org-end-of-subtree t
)))))
3313 (defalias 'org-advertized-archive-subtree
'org-archive-subtree
)
3316 (org-autoload "org-archive"
3317 '(org-add-archive-files org-archive-subtree
3318 org-archive-to-archive-sibling org-toggle-archive-tag
)))
3320 ;; Autoload Column View Code
3322 (declare-function org-columns-number-to-string
"org-colview")
3323 (declare-function org-columns-get-format-and-top-level
"org-colview")
3324 (declare-function org-columns-compute
"org-colview")
3326 (org-autoload (if (featurep 'xemacs
) "org-colview-xemacs" "org-colview")
3327 '(org-columns-number-to-string org-columns-get-format-and-top-level
3328 org-columns-compute org-agenda-columns org-columns-remove-overlays
3329 org-columns org-insert-columns-dblock org-dblock-write
:columnview
))
3333 (declare-function org-id-store-link
"org-id")
3334 (declare-function org-id-locations-load
"org-id")
3335 (declare-function org-id-locations-save
"org-id")
3336 (defvar org-id-track-globally
)
3337 (org-autoload "org-id"
3338 '(org-id-get-create org-id-new org-id-copy org-id-get
3339 org-id-get-with-outline-path-completion
3340 org-id-get-with-outline-drilling
3341 org-id-goto org-id-find org-id-store-link
))
3343 ;; Autoload Plotting Code
3345 (org-autoload "org-plot"
3346 '(org-plot/gnuplot
))
3348 ;;; Variables for pre-computed regular expressions, all buffer local
3350 (defvar org-drawer-regexp nil
3351 "Matches first line of a hidden block.")
3352 (make-variable-buffer-local 'org-drawer-regexp
)
3353 (defvar org-todo-regexp nil
3354 "Matches any of the TODO state keywords.")
3355 (make-variable-buffer-local 'org-todo-regexp
)
3356 (defvar org-not-done-regexp nil
3357 "Matches any of the TODO state keywords except the last one.")
3358 (make-variable-buffer-local 'org-not-done-regexp
)
3359 (defvar org-not-done-heading-regexp nil
3360 "Matches a TODO headline that is not done.")
3361 (make-variable-buffer-local 'org-not-done-regexp
)
3362 (defvar org-todo-line-regexp nil
3363 "Matches a headline and puts TODO state into group 2 if present.")
3364 (make-variable-buffer-local 'org-todo-line-regexp
)
3365 (defvar org-complex-heading-regexp nil
3366 "Matches a headline and puts everything into groups:
3368 group 2: The todo keyword, maybe
3369 group 3: Priority cookie
3370 group 4: True headline
3372 (make-variable-buffer-local 'org-complex-heading-regexp
)
3373 (defvar org-todo-line-tags-regexp nil
3374 "Matches a headline and puts TODO state into group 2 if present.
3375 Also put tags into group 4 if tags are present.")
3376 (make-variable-buffer-local 'org-todo-line-tags-regexp
)
3377 (defvar org-nl-done-regexp nil
3378 "Matches newline followed by a headline with the DONE keyword.")
3379 (make-variable-buffer-local 'org-nl-done-regexp
)
3380 (defvar org-looking-at-done-regexp nil
3381 "Matches the DONE keyword a point.")
3382 (make-variable-buffer-local 'org-looking-at-done-regexp
)
3383 (defvar org-ds-keyword-length
12
3384 "Maximum length of the Deadline and SCHEDULED keywords.")
3385 (make-variable-buffer-local 'org-ds-keyword-length
)
3386 (defvar org-deadline-regexp nil
3387 "Matches the DEADLINE keyword.")
3388 (make-variable-buffer-local 'org-deadline-regexp
)
3389 (defvar org-deadline-time-regexp nil
3390 "Matches the DEADLINE keyword together with a time stamp.")
3391 (make-variable-buffer-local 'org-deadline-time-regexp
)
3392 (defvar org-deadline-line-regexp nil
3393 "Matches the DEADLINE keyword and the rest of the line.")
3394 (make-variable-buffer-local 'org-deadline-line-regexp
)
3395 (defvar org-scheduled-regexp nil
3396 "Matches the SCHEDULED keyword.")
3397 (make-variable-buffer-local 'org-scheduled-regexp
)
3398 (defvar org-scheduled-time-regexp nil
3399 "Matches the SCHEDULED keyword together with a time stamp.")
3400 (make-variable-buffer-local 'org-scheduled-time-regexp
)
3401 (defvar org-closed-time-regexp nil
3402 "Matches the CLOSED keyword together with a time stamp.")
3403 (make-variable-buffer-local 'org-closed-time-regexp
)
3405 (defvar org-keyword-time-regexp nil
3406 "Matches any of the 4 keywords, together with the time stamp.")
3407 (make-variable-buffer-local 'org-keyword-time-regexp
)
3408 (defvar org-keyword-time-not-clock-regexp nil
3409 "Matches any of the 3 keywords, together with the time stamp.")
3410 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp
)
3411 (defvar org-maybe-keyword-time-regexp nil
3412 "Matches a timestamp, possibly preceeded by a keyword.")
3413 (make-variable-buffer-local 'org-maybe-keyword-time-regexp
)
3414 (defvar org-planning-or-clock-line-re nil
3415 "Matches a line with planning or clock info.")
3416 (make-variable-buffer-local 'org-planning-or-clock-line-re
)
3418 (defconst org-plain-time-of-day-regexp
3421 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
3424 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
3426 "Regular expression to match a plain time or time range.
3427 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
3428 groups carry important information:
3430 1 the first time, range or not
3431 8 the second time, if it is a range.")
3433 (defconst org-plain-time-extension-regexp
3436 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
3437 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
3438 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
3439 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
3440 groups carry important information:
3443 9 minutes of duration")
3445 (defconst org-stamp-time-of-day-regexp
3447 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
3448 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
3450 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
3451 "Regular expression to match a timestamp time or time range.
3452 After a match, the following groups carry important information:
3454 1 date plus weekday, for backreferencing to make sure both times on same day
3455 2 the first time, range or not
3456 4 the second time, if it is a range.")
3458 (defconst org-startup-options
3459 '(("fold" org-startup-folded t
)
3460 ("overview" org-startup-folded t
)
3461 ("nofold" org-startup-folded nil
)
3462 ("showall" org-startup-folded nil
)
3463 ("content" org-startup-folded content
)
3464 ("indent" org-startup-indented t
)
3465 ("noindent" org-startup-indented nil
)
3466 ("hidestars" org-hide-leading-stars t
)
3467 ("showstars" org-hide-leading-stars nil
)
3468 ("odd" org-odd-levels-only t
)
3469 ("oddeven" org-odd-levels-only nil
)
3470 ("align" org-startup-align-all-tables t
)
3471 ("noalign" org-startup-align-all-tables nil
)
3472 ("customtime" org-display-custom-times t
)
3473 ("logdone" org-log-done time
)
3474 ("lognotedone" org-log-done note
)
3475 ("nologdone" org-log-done nil
)
3476 ("lognoteclock-out" org-log-note-clock-out t
)
3477 ("nolognoteclock-out" org-log-note-clock-out nil
)
3478 ("logrepeat" org-log-repeat state
)
3479 ("lognoterepeat" org-log-repeat note
)
3480 ("nologrepeat" org-log-repeat nil
)
3481 ("fninline" org-footnote-define-inline t
)
3482 ("nofninline" org-footnote-define-inline nil
)
3483 ("fnlocal" org-footnote-section nil
)
3484 ("fnauto" org-footnote-auto-label t
)
3485 ("fnprompt" org-footnote-auto-label nil
)
3486 ("fnconfirm" org-footnote-auto-label confirm
)
3487 ("fnplain" org-footnote-auto-label plain
)
3488 ("fnadjust" org-footnote-auto-adjust t
)
3489 ("nofnadjust" org-footnote-auto-adjust nil
)
3490 ("constcgs" constants-unit-system cgs
)
3491 ("constSI" constants-unit-system SI
)
3492 ("noptag" org-tag-persistent-alist nil
)
3493 ("hideblocks" org-hide-block-startup t
)
3494 ("nohideblocks" org-hide-block-startup nil
))
3495 "Variable associated with STARTUP options for org-mode.
3496 Each element is a list of three items: The startup options as written
3497 in the #+STARTUP line, the corresponding variable, and the value to
3498 set this variable to if the option is found. An optional forth element PUSH
3499 means to push this value onto the list in the variable.")
3501 (defun org-set-regexps-and-options ()
3502 "Precompute regular expressions for current buffer."
3504 (org-set-local 'org-todo-kwd-alist nil
)
3505 (org-set-local 'org-todo-key-alist nil
)
3506 (org-set-local 'org-todo-key-trigger nil
)
3507 (org-set-local 'org-todo-keywords-1 nil
)
3508 (org-set-local 'org-done-keywords nil
)
3509 (org-set-local 'org-todo-heads nil
)
3510 (org-set-local 'org-todo-sets nil
)
3511 (org-set-local 'org-todo-log-states nil
)
3512 (org-set-local 'org-file-properties nil
)
3513 (org-set-local 'org-file-tags nil
)
3514 (let ((re (org-make-options-regexp
3515 '("CATEGORY" "TODO" "COLUMNS"
3516 "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
3517 "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
3518 "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
3520 kwds kws0 kwsa key log value cat arch tags const links hw dws
3521 tail sep kws1 prio props ftags drawers
3522 ext-setup-or-nil setup-contents
(start 0))
3526 (goto-char (point-min))
3527 (while (or (and ext-setup-or-nil
3528 (string-match re ext-setup-or-nil start
)
3529 (setq start
(match-end 0)))
3530 (and (setq ext-setup-or-nil nil start
0)
3531 (re-search-forward re nil t
)))
3532 (setq key
(upcase (match-string 1 ext-setup-or-nil
))
3533 value
(org-match-string-no-properties 2 ext-setup-or-nil
))
3535 ((equal key
"CATEGORY")
3536 (if (string-match "[ \t]+$" value
)
3537 (setq value
(replace-match "" t t value
)))
3539 ((member key
'("SEQ_TODO" "TODO"))
3540 (push (cons 'sequence
(org-split-string value splitre
)) kwds
))
3541 ((equal key
"TYP_TODO")
3542 (push (cons 'type
(org-split-string value splitre
)) kwds
))
3543 ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key
)
3544 ;; general TODO-like setup
3545 (push (cons (intern (downcase (match-string 1 key
)))
3546 (org-split-string value splitre
)) kwds
))
3548 (setq tags
(append tags
(if tags
'("\\n") nil
)
3549 (org-split-string value splitre
))))
3550 ((equal key
"COLUMNS")
3551 (org-set-local 'org-columns-default-format value
))
3553 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value
)
3554 (push (cons (match-string 1 value
)
3555 (org-trim (match-string 2 value
)))
3557 ((equal key
"PRIORITIES")
3558 (setq prio
(org-split-string value
" +")))
3559 ((equal key
"PROPERTY")
3560 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
3561 (push (cons (match-string 1 value
) (match-string 2 value
))
3563 ((equal key
"FILETAGS")
3564 (when (string-match "\\S-" value
)
3569 (mapcar (lambda (x) (org-split-string x
":"))
3570 (org-split-string value
)))))))
3571 ((equal key
"DRAWERS")
3572 (setq drawers
(org-split-string value splitre
)))
3573 ((equal key
"CONSTANTS")
3574 (setq const
(append const
(org-split-string value splitre
))))
3575 ((equal key
"STARTUP")
3576 (let ((opts (org-split-string value splitre
))
3578 (while (setq l
(pop opts
))
3579 (when (setq l
(assoc l org-startup-options
))
3580 (setq var
(nth 1 l
) val
(nth 2 l
))
3582 (set (make-local-variable var
) val
)
3583 (if (not (listp (symbol-value var
)))
3584 (set (make-local-variable var
) nil
))
3585 (set (make-local-variable var
) (symbol-value var
))
3586 (add-to-list var val
))))))
3587 ((equal key
"ARCHIVE")
3588 (string-match " *$" value
)
3589 (setq arch
(replace-match "" t t value
))
3590 (remove-text-properties 0 (length arch
)
3591 '(face t fontified t
) arch
))
3592 ((equal key
"SETUPFILE")
3593 (setq setup-contents
(org-file-contents
3595 (org-remove-double-quotes value
))
3597 (if (not ext-setup-or-nil
)
3598 (setq ext-setup-or-nil setup-contents start
0)
3599 (setq ext-setup-or-nil
3600 (concat (substring ext-setup-or-nil
0 start
)
3601 "\n" setup-contents
"\n"
3602 (substring ext-setup-or-nil start
)))))
3605 (org-set-local 'org-category
(intern cat
))
3606 (push (cons "CATEGORY" cat
) props
))
3608 (if (< (length prio
) 3) (setq prio
'("A" "C" "B")))
3609 (setq prio
(mapcar 'string-to-char prio
))
3610 (org-set-local 'org-highest-priority
(nth 0 prio
))
3611 (org-set-local 'org-lowest-priority
(nth 1 prio
))
3612 (org-set-local 'org-default-priority
(nth 2 prio
)))
3613 (and props
(org-set-local 'org-file-properties
(nreverse props
)))
3614 (and ftags
(org-set-local 'org-file-tags
3615 (mapcar 'org-add-prop-inherited ftags
)))
3616 (and drawers
(org-set-local 'org-drawers drawers
))
3617 (and arch
(org-set-local 'org-archive-location arch
))
3618 (and links
(setq org-link-abbrev-alist-local
(nreverse links
)))
3619 ;; Process the TODO keywords
3621 ;; Use the global values as if they had been given locally.
3622 (setq kwds
(default-value 'org-todo-keywords
))
3623 (if (stringp (car kwds
))
3624 (setq kwds
(list (cons org-todo-interpretation
3625 (default-value 'org-todo-keywords
)))))
3626 (setq kwds
(reverse kwds
)))
3627 (setq kwds
(nreverse kwds
))
3629 (while (setq kws
(pop kwds
))
3631 (run-hook-with-args-until-success
3632 'org-todo-setup-filter-hook kws
)
3634 (setq inter
(pop kws
) sep
(member "|" kws
)
3635 kws0
(delete "|" (copy-sequence kws
))
3640 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x
)
3642 (setq kw
(match-string 1 x
)
3643 key
(and (match-end 2) (match-string 2 x
))
3644 log
(org-extract-log-state-settings x
))
3645 (push (cons kw
(and key
(string-to-char key
))) kwsa
)
3646 (and log
(push log org-todo-log-states
))
3648 (error "Invalid TODO keyword %s" x
)))
3650 kwsa
(if kwsa
(append '((:startgroup
))
3654 dws
(if sep
(org-remove-keyword-keys (cdr sep
)) (last kws1
))
3655 tail
(list inter hw
(car dws
) (org-last dws
))))
3656 (add-to-list 'org-todo-heads hw
'append
)
3657 (push kws1 org-todo-sets
)
3658 (setq org-done-keywords
(append org-done-keywords dws nil
))
3659 (setq org-todo-key-alist
(append org-todo-key-alist kwsa
))
3660 (mapc (lambda (x) (push (cons x tail
) org-todo-kwd-alist
)) kws1
)
3661 (setq org-todo-keywords-1
(append org-todo-keywords-1 kws1 nil
)))
3662 (setq org-todo-sets
(nreverse org-todo-sets
)
3663 org-todo-kwd-alist
(nreverse org-todo-kwd-alist
)
3664 org-todo-key-trigger
(delq nil
(mapcar 'cdr org-todo-key-alist
))
3665 org-todo-key-alist
(org-assign-fast-keys org-todo-key-alist
)))
3666 ;; Process the constants
3669 (while (setq e
(pop const
))
3670 (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e
)
3671 (push (cons (match-string 1 e
) (match-string 2 e
)) cst
)))
3672 (setq org-table-formula-constants-local cst
)))
3674 ;; Process the tags.
3677 (while (setq e
(pop tags
))
3679 ((equal e
"{") (push '(:startgroup
) tgs
))
3680 ((equal e
"}") (push '(:endgroup
) tgs
))
3681 ((equal e
"\\n") (push '(:newline
) tgs
))
3682 ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e
)
3683 (push (cons (match-string 1 e
)
3684 (string-to-char (match-string 2 e
)))
3686 (t (push (list e
) tgs
))))
3687 (org-set-local 'org-tag-alist nil
)
3688 (while (setq e
(pop tgs
))
3689 (or (and (stringp (car e
))
3690 (assoc (car e
) org-tag-alist
))
3691 (push e org-tag-alist
)))))
3693 ;; Compute the regular expressions and other local variables
3694 (if (not org-done-keywords
)
3695 (setq org-done-keywords
(list (org-last org-todo-keywords-1
))))
3696 (setq org-ds-keyword-length
(+ 2 (max (length org-deadline-string
)
3697 (length org-scheduled-string
)
3698 (length org-clock-string
)
3699 (length org-closed-string
)))
3701 (concat "^[ \t]*:\\("
3702 (mapconcat 'regexp-quote org-drawers
"\\|")
3704 org-not-done-keywords
3705 (org-delete-all org-done-keywords
(copy-sequence org-todo-keywords-1
))
3707 (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
3711 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
3713 org-not-done-heading-regexp
3714 (concat "^\\(\\*+\\)[ \t]+\\("
3715 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
3717 org-todo-line-regexp
3718 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3719 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
3720 "\\)\\>\\)?[ \t]*\\(.*\\)")
3721 org-complex-heading-regexp
3722 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3723 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
3724 "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
3725 "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
3727 (concat "\n\\*+[ \t]+"
3728 "\\(?:" (mapconcat 'regexp-quote org-done-keywords
"\\|")
3730 org-todo-line-tags-regexp
3731 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3732 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
3734 "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
3735 org-looking-at-done-regexp
3737 (mapconcat 'regexp-quote org-done-keywords
"\\|") "\\)"
3739 org-deadline-regexp
(concat "\\<" org-deadline-string
)
3740 org-deadline-time-regexp
3741 (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>")
3742 org-deadline-line-regexp
3743 (concat "\\<\\(" org-deadline-string
"\\).*")
3744 org-scheduled-regexp
3745 (concat "\\<" org-scheduled-string
)
3746 org-scheduled-time-regexp
3747 (concat "\\<" org-scheduled-string
" *<\\([^>]+\\)>")
3748 org-closed-time-regexp
3749 (concat "\\<" org-closed-string
" *\\[\\([^]]+\\)\\]")
3750 org-keyword-time-regexp
3751 (concat "\\<\\(" org-scheduled-string
3752 "\\|" org-deadline-string
3753 "\\|" org-closed-string
3754 "\\|" org-clock-string
"\\)"
3755 " *[[<]\\([^]>]+\\)[]>]")
3756 org-keyword-time-not-clock-regexp
3757 (concat "\\<\\(" org-scheduled-string
3758 "\\|" org-deadline-string
3759 "\\|" org-closed-string
3761 " *[[<]\\([^]>]+\\)[]>]")
3762 org-maybe-keyword-time-regexp
3763 (concat "\\(\\<\\(" org-scheduled-string
3764 "\\|" org-deadline-string
3765 "\\|" org-closed-string
3766 "\\|" org-clock-string
"\\)\\)?"
3767 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
3768 org-planning-or-clock-line-re
3769 (concat "\\(?:^[ \t]*\\(" org-scheduled-string
3770 "\\|" org-deadline-string
3771 "\\|" org-closed-string
"\\|" org-clock-string
3774 (org-compute-latex-and-specials-regexp)
3775 (org-set-font-lock-defaults))))
3777 (defun org-file-contents (file &optional noerror
)
3778 "Return the contents of FILE, as a string."
3780 (not (file-readable-p file
)))
3783 (message "Cannot read file %s" file
)
3786 (error "Cannot read file %s" file
))
3788 (insert-file-contents file
)
3791 (defun org-extract-log-state-settings (x)
3792 "Extract the log state setting from a TODO keyword string.
3793 This will extract info from a string like \"WAIT(w@/!)\"."
3794 (let (kw key log1 log2
)
3795 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x
)
3796 (setq kw
(match-string 1 x
)
3797 key
(and (match-end 2) (match-string 2 x
))
3798 log1
(and (match-end 3) (match-string 3 x
))
3799 log2
(and (match-end 4) (match-string 4 x
)))
3802 (and log1
(if (equal log1
"!") 'time
'note
))
3803 (and log2
(if (equal log2
"!") 'time
'note
)))))))
3805 (defun org-remove-keyword-keys (list)
3806 "Remove a pair of parenthesis at the end of each string in LIST."
3808 (if (string-match "(.*)$" x
)
3809 (substring x
0 (match-beginning 0))
3813 ;; FIXME: this could be done much better, using second characters etc.
3814 (defun org-assign-fast-keys (alist)
3815 "Assign fast keys to a keyword-key alist.
3816 Respect keys that are already there."
3817 (let (new e k c c1 c2
(char ?a
))
3818 (while (setq e
(pop alist
))
3820 ((equal e
'(:startgroup
)) (push e new
))
3821 ((equal e
'(:endgroup
)) (push e new
))
3822 ((equal e
'(:newline
)) (push e new
))
3824 (setq k
(car e
) c2 nil
)
3827 ;; automatically assign a character.
3828 (setq c1
(string-to-char
3829 (downcase (substring
3830 k
(if (= (string-to-char k
) ?
@) 1 0)))))
3831 (if (or (rassoc c1 new
) (rassoc c1 alist
))
3832 (while (or (rassoc char new
) (rassoc char alist
))
3833 (setq char
(1+ char
)))
3835 (setq c
(or c2 char
)))
3836 (push (cons k c
) new
))))
3839 ;;; Some variables used in various places
3841 (defvar org-window-configuration nil
3842 "Used in various places to store a window configuration.")
3843 (defvar org-finish-function nil
3844 "Function to be called when `C-c C-c' is used.
3845 This is for getting out of special buffers like remember.")
3848 ;; FIXME: Occasionally check by commenting these, to make sure
3849 ;; no other functions uses these, forgetting to let-bind them.
3854 ;; Defined somewhere in this file, but used before definition.
3855 (defvar org-html-entities
)
3856 (defvar org-struct-menu
)
3857 (defvar org-org-menu
)
3858 (defvar org-tbl-menu
)
3859 (defvar org-agenda-keymap
)
3861 ;;;; Define the Org-mode
3863 (if (and (not (keymapp outline-mode-map
)) (featurep 'allout
))
3864 (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."))
3867 ;; We use a before-change function to check if a table might need
3869 (defvar org-table-may-need-update t
3870 "Indicates that a table might need an update.
3871 This variable is set by `org-before-change-function'.
3872 `org-table-align' sets it back to nil.")
3873 (defun org-before-change-function (beg end
)
3874 "Every change indicates that a table might need an update."
3875 (setq org-table-may-need-update t
))
3876 (defvar org-mode-map
)
3877 (defvar org-mode-hook nil
3878 "Mode hook for Org-mode, run after the mode was turned on.")
3879 (defvar org-inhibit-startup nil
) ; Dynamically-scoped param.
3880 (defvar org-agenda-keep-modes nil
) ; Dynamically-scoped param.
3881 (defvar org-inhibit-logging nil
) ; Dynamically-scoped param.
3882 (defvar org-inhibit-blocking nil
) ; Dynamically-scoped param.
3883 (defvar org-table-buffer-is-an nil
)
3884 (defconst org-outline-regexp
"\\*+ ")
3887 (define-derived-mode org-mode outline-mode
"Org"
3888 "Outline-based notes management and organizer, alias
3889 \"Carsten's outline-mode for keeping track of everything.\"
3891 Org-mode develops organizational tasks around a NOTES file which
3892 contains information about projects as plain text. Org-mode is
3893 implemented on top of outline-mode, which is ideal to keep the content
3894 of large files well structured. It supports ToDo items, deadlines and
3895 time stamps, which magically appear in the diary listing of the Emacs
3896 calendar. Tables are easily created with a built-in table editor.
3897 Plain text URL-like links connect to websites, emails (VM), Usenet
3898 messages (Gnus), BBDB entries, and any files related to the project.
3899 For printing and sharing of notes, an Org-mode file (or a part of it)
3900 can be exported as a structured ASCII or HTML file.
3902 The following commands are available:
3906 ;; Get rid of Outline menus, they are not needed
3907 ;; Need to do this here because define-derived-mode sets up
3908 ;; the keymap so late. Still, it is a waste to call this each time
3909 ;; we switch another buffer into org-mode.
3910 (if (featurep 'xemacs
)
3911 (when (boundp 'outline-mode-menu-heading
)
3912 ;; Assume this is Greg's port, it used easymenu
3913 (easy-menu-remove outline-mode-menu-heading
)
3914 (easy-menu-remove outline-mode-menu-show
)
3915 (easy-menu-remove outline-mode-menu-hide
))
3916 (define-key org-mode-map
[menu-bar headings
] 'undefined
)
3917 (define-key org-mode-map
[menu-bar hide
] 'undefined
)
3918 (define-key org-mode-map
[menu-bar show
] 'undefined
))
3920 (org-load-modules-maybe)
3921 (easy-menu-add org-org-menu
)
3922 (easy-menu-add org-tbl-menu
)
3923 (org-install-agenda-files-menu)
3924 (if org-descriptive-links
(org-add-to-invisibility-spec '(org-link)))
3925 (org-add-to-invisibility-spec '(org-cwidth))
3926 (org-add-to-invisibility-spec '(org-hide-block . t
))
3927 (when (featurep 'xemacs
)
3928 (org-set-local 'line-move-ignore-invisible t
))
3929 (org-set-local 'outline-regexp org-outline-regexp
)
3930 (org-set-local 'outline-level
'org-outline-level
)
3931 (when (and org-ellipsis
3932 (fboundp 'set-display-table-slot
) (boundp 'buffer-display-table
)
3933 (fboundp 'make-glyph-code
))
3934 (unless org-display-table
3935 (setq org-display-table
(make-display-table)))
3936 (set-display-table-slot
3939 (lambda (c) (make-glyph-code c
(and (not (stringp org-ellipsis
))
3941 (if (stringp org-ellipsis
) org-ellipsis
"..."))))
3942 (setq buffer-display-table org-display-table
))
3943 (org-set-regexps-and-options)
3944 (when (and org-tag-faces
(not org-tags-special-faces-re
))
3945 ;; tag faces set outside customize.... force initialization.
3946 (org-set-tag-faces 'org-tag-faces org-tag-faces
))
3948 (org-set-local 'calc-embedded-open-mode
"# ")
3949 (modify-syntax-entry ?
# "<")
3950 (modify-syntax-entry ?
@ "w")
3951 (if org-startup-truncated
(setq truncate-lines t
))
3952 (org-set-local 'font-lock-unfontify-region-function
3953 'org-unfontify-region
)
3954 ;; Activate before-change-function
3955 (org-set-local 'org-table-may-need-update t
)
3956 (org-add-hook 'before-change-functions
'org-before-change-function nil
3958 ;; Check for running clock before killing a buffer
3959 (org-add-hook 'kill-buffer-hook
'org-check-running-clock nil
'local
)
3960 ;; Paragraphs and auto-filling
3961 (org-set-autofill-regexps)
3962 (setq indent-line-function
'org-indent-line-function
)
3963 (org-update-radio-target-regexp)
3964 ;; Make sure dependence stuff works reliably, even for users who set it
3966 (if org-enforce-todo-dependencies
3967 (add-hook 'org-blocker-hook
3968 'org-block-todo-from-children-or-siblings-or-parent
)
3969 (remove-hook 'org-blocker-hook
3970 'org-block-todo-from-children-or-siblings-or-parent
))
3971 (if org-enforce-todo-checkbox-dependencies
3972 (add-hook 'org-blocker-hook
3973 'org-block-todo-from-checkboxes
)
3974 (remove-hook 'org-blocker-hook
3975 'org-block-todo-from-checkboxes
))
3977 ;; Comment characters
3978 ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
3979 (org-set-local 'comment-padding
" ")
3981 ;; Align options lines
3983 'align-mode-rules-list
3984 '((org-in-buffer-settings
3985 (regexp .
"^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
3986 (modes .
'(org-mode)))))
3989 (org-set-local 'imenu-create-index-function
3990 'org-imenu-get-tree
)
3992 ;; Make isearch reveal context
3993 (if (or (featurep 'xemacs
)
3994 (not (boundp 'outline-isearch-open-invisible-function
)))
3995 ;; Emacs 21 and XEmacs make use of the hook
3996 (org-add-hook 'isearch-mode-end-hook
'org-isearch-end
'append
'local
)
3997 ;; Emacs 22 deals with this through a special variable
3998 (org-set-local 'outline-isearch-open-invisible-function
3999 (lambda (&rest ignore
) (org-show-context 'isearch
))))
4001 ;; If empty file that did not turn on org-mode automatically, make it to.
4002 (if (and org-insert-mode-line-in-empty-file
4004 (= (point-min) (point-max)))
4005 (insert "# -*- mode: org -*-\n\n"))
4007 (unless org-inhibit-startup
4008 (when org-startup-align-all-tables
4009 (let ((bmp (buffer-modified-p)))
4010 (org-table-map-tables 'org-table-align
)
4011 (set-buffer-modified-p bmp
)))
4012 (when org-startup-indented
4013 (require 'org-indent
)
4014 (org-indent-mode 1))
4015 (org-set-startup-visibility)))
4017 (put 'org-mode
'flyspell-mode-predicate
'org-mode-flyspell-verify
)
4019 (defun org-current-time ()
4020 "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
4021 (if (> (car org-time-stamp-rounding-minutes
) 1)
4022 (let ((r (car org-time-stamp-rounding-minutes
))
4023 (time (decode-time)))
4025 (append (list 0 (* r
(floor (+ .5 (/ (float (nth 1 time
)) r
)))))
4029 ;;;; Font-Lock stuff, including the activators
4031 (defvar org-mouse-map
(make-sparse-keymap))
4032 (org-defkey org-mouse-map
4033 (if (featurep 'xemacs
) [button2] [mouse-2]) 'org-open-at-mouse)
4034 (org-defkey org-mouse-map
4035 (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
4036 (when org-mouse-1-follows-link
4037 (org-defkey org-mouse-map [follow-link] 'mouse-face))
4038 (when org-tab-follows-link
4039 (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
4040 (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
4042 (require 'font-lock)
4044 (defconst org-non-link-chars "]\t\n\r<>")
4045 (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
4047 (defvar org-link-types-re nil
4048 "Matches a link that has a url-like prefix like \"http:\"")
4049 (defvar org-link-re-with-space nil
4050 "Matches a link with spaces, optional angular brackets around it.")
4051 (defvar org-link-re-with-space2 nil
4052 "Matches a link with spaces, optional angular brackets around it.")
4053 (defvar org-link-re-with-space3 nil
4054 "Matches a link with spaces, only for internal part in bracket links.")
4055 (defvar org-angle-link-re nil
4056 "Matches link with angular brackets, spaces are allowed.")
4057 (defvar org-plain-link-re nil
4058 "Matches plain link, without spaces.")
4059 (defvar org-bracket-link-regexp nil
4060 "Matches a link in double brackets.")
4061 (defvar org-bracket-link-analytic-regexp nil
4062 "Regular expression used to analyze links.
4063 Here is what the match groups contain after a match:
4069 (defvar org-bracket-link-analytic-regexp++ nil
4070 "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
4071 (defvar org-any-link-re nil
4072 "Regular expression matching any link.")
4074 (defun org-make-link-regexps ()
4075 "Update the link regular expressions.
4076 This should be called after the variable `org-link-types' has changed."
4077 (setq org-link-types-re
4079 "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
4080 org-link-re-with-space
4082 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
4083 "\\([^" org-non-link-chars " ]"
4084 "[^" org-non-link-chars "]*"
4085 "[^" org-non-link-chars " ]\\)>?")
4086 org-link-re-with-space2
4088 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
4089 "\\([^" org-non-link-chars " ]"
4091 "[^" org-non-link-chars " ]\\)>?")
4092 org-link-re-with-space3
4094 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
4095 "\\([^" org-non-link-chars " ]"
4099 "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
4100 "\\([^" org-non-link-chars " ]"
4101 "[^" org-non-link-chars "]*"
4105 "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
4106 "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
4107 org-bracket-link-regexp
4108 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
4109 org-bracket-link-analytic-regexp
4112 "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
4115 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
4117 org-bracket-link-analytic-regexp++
4120 "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
4123 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
4126 (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
4127 org-angle-link-re "\\)\\|\\("
4128 org-plain-link-re "\\)")))
4130 (org-make-link-regexps)
4132 (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
4133 "Regular expression for fast time stamp matching.")
4134 (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
4135 "Regular expression for fast time stamp matching.")
4136 (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
4137 "Regular expression matching time strings for analysis.
4138 This one does not require the space after the date, so it can be used
4139 on a string that terminates immediately after the date.")
4140 (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
4141 "Regular expression matching time strings for analysis.")
4142 (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
4143 "Regular expression matching time stamps, with groups.")
4144 (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
4145 "Regular expression matching time stamps (also [..]), with groups.")
4146 (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
4147 "Regular expression matching a time stamp range.")
4148 (defconst org-tr-regexp-both
4149 (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
4150 "Regular expression matching a time stamp range.")
4151 (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
4152 org-ts-regexp "\\)?")
4153 "Regular expression matching a time stamp or time stamp range.")
4154 (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
4155 org-ts-regexp-both "\\)?")
4156 "Regular expression matching a time stamp or time stamp range.
4157 The time stamps may be either active or inactive.")
4159 (defvar org-emph-face nil)
4161 (defun org-do-emphasis-faces (limit)
4162 "Run through the buffer and add overlays to links."
4164 (while (and (not rtn) (re-search-forward org-emph-re limit t))
4165 (if (not (= (char-after (match-beginning 3))
4166 (char-after (match-beginning 4))))
4169 (setq a (assoc (match-string 3) org-emphasis-alist))
4170 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
4174 (org-remove-flyspell-overlays-in
4175 (match-beginning 0) (match-end 0)))
4176 (add-text-properties (match-beginning 2) (match-end 2)
4177 '(font-lock-multiline t))
4178 (when org-hide-emphasis-markers
4179 (add-text-properties (match-end 4) (match-beginning 5)
4180 '(invisible org-link))
4181 (add-text-properties (match-beginning 3) (match-end 3)
4182 '(invisible org-link)))))
4186 (defun org-emphasize (&optional char)
4187 "Insert or change an emphasis, i.e. a font like bold or italic.
4188 If there is an active region, change that region to a new emphasis.
4189 If there is no region, just insert the marker characters and position
4190 the cursor between them.
4191 CHAR should be either the marker character, or the first character of the
4192 HTML tag associated with that emphasis. If CHAR is a space, the means
4193 to remove the emphasis of the selected region.
4194 If char is not given (for example in an interactive call) it
4195 will be prompted for."
4197 (let ((eal org-emphasis-alist) e det
4198 (erc org-emphasis-regexp-components)
4200 (string "") beg end move tag c s)
4201 (if (org-region-active-p)
4202 (setq beg (region-beginning) end (region-end)
4203 string (buffer-substring beg end))
4206 (while (setq e (pop eal))
4207 (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
4209 (push (cons c (string-to-char (car e))) det)
4210 (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
4211 (substring tag 1)))))
4212 (setq det (nreverse det))
4214 (message "%s" (concat "Emphasis marker or tag:" prompt))
4215 (setq char (read-char-exclusive)))
4216 (setq char (or (cdr (assoc char det)) char))
4217 (if (equal char ?\ )
4218 (setq s "" move nil)
4219 (unless (assoc (char-to-string char) org-emphasis-alist)
4220 (error "No such emphasis marker: \"%c\"" char))
4221 (setq s (char-to-string char)))
4222 (while (and (> (length string) 1)
4223 (equal (substring string 0 1) (substring string -1))
4224 (assoc (substring string 0 1) org-emphasis-alist))
4225 (setq string (substring string 1 -1)))
4226 (setq string (concat s string s))
4227 (if beg (delete-region beg end))
4229 (string-match (concat "[" (nth 0 erc) "\n]")
4230 (char-to-string (char-before (point)))))
4232 (unless (string-match (concat "[" (nth 1 erc) "\n]")
4233 (char-to-string (char-after (point))))
4234 (insert " ") (backward-char 1))
4236 (and move (backward-char 1))))
4238 (defconst org-nonsticky-props
4239 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
4241 (defsubst org-rear-nonsticky-at (pos)
4242 (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
4244 (defun org-activate-plain-links (limit)
4245 "Run through the buffer and add overlays to links."
4248 (if (re-search-forward org-plain-link-re limit t)
4250 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4251 (setq f (get-text-property (match-beginning 0) 'face))
4252 (if (or (eq f 'org-tag)
4253 (and (listp f) (memq 'org-tag f)))
4255 (add-text-properties (match-beginning 0) (match-end 0)
4256 (list 'mouse-face 'highlight
4257 'keymap org-mouse-map))
4258 (org-rear-nonsticky-at (match-end 0)))
4261 (defun org-activate-code (limit)
4262 (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
4264 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4265 (remove-text-properties (match-beginning 0) (match-end 0)
4266 '(display t invisible t intangible t))
4269 (defun org-fontify-meta-lines-and-blocks (limit)
4270 "Fontify #+ lines and blocks, in the correct ways."
4271 (let ((case-fold-search t))
4272 (if (re-search-forward
4273 "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
4275 (let ((beg (match-beginning 0))
4276 (beg1 (line-beginning-position 2))
4277 (dc1 (downcase (match-string 2)))
4278 (dc3 (downcase (match-string 3)))
4281 ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
4282 ;; a single line of backend-specific content
4283 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4284 (remove-text-properties (match-beginning 0) (match-end 0)
4285 '(display t invisible t intangible t))
4286 (add-text-properties (match-beginning 1) (match-end 3)
4287 '(font-lock-fontified t face org-meta-line))
4288 (add-text-properties (match-beginning 6) (match-end 6)
4289 '(font-lock-fontified t face org-block))
4291 ((and (match-end 4) (equal dc3 "begin"))
4293 (setq quoting (member (downcase (match-string 5))
4294 org-protecting-blocks))
4295 (when (re-search-forward
4296 (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
4297 nil t) ;; on purpose, we look further than LIMIT
4298 (setq end (match-end 0) end1 (1- (match-beginning 0)))
4300 (remove-text-properties beg end
4301 '(display t invisible t intangible t)))
4302 (add-text-properties
4304 '(font-lock-fontified t font-lock-multiline t))
4305 (add-text-properties beg beg1 '(face org-meta-line))
4306 (add-text-properties end1 end '(face org-meta-line))
4308 (add-text-properties beg1 end1 '(face org-block)))
4310 ((not (member (char-after beg) '(?\ ?\t)))
4311 ;; just any other in-buffer setting, but not indented
4312 (add-text-properties
4314 '(font-lock-fontified t face org-meta-line))
4316 ((or (member dc1 '("caption:" "label:" "orgtbl:" "tblfm:" "tblname:"))
4317 (and (match-end 4) (equal dc3 "attr")))
4318 (add-text-properties
4320 '(font-lock-fontified t face org-meta-line))
4324 (defun org-activate-angle-links (limit)
4325 "Run through the buffer and add overlays to links."
4326 (if (re-search-forward org-angle-link-re limit t)
4328 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4329 (add-text-properties (match-beginning 0) (match-end 0)
4330 (list 'mouse-face 'highlight
4331 'keymap org-mouse-map))
4332 (org-rear-nonsticky-at (match-end 0))
4335 (defun org-activate-footnote-links (limit)
4336 "Run through the buffer and add overlays to links."
4337 (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
4340 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4341 (add-text-properties (match-beginning 2) (match-end 2)
4342 (list 'mouse-face 'highlight
4343 'keymap org-mouse-map
4345 (if (= (point-at-bol) (match-beginning 2))
4346 "Footnote definition"
4347 "Footnote reference")
4349 (org-rear-nonsticky-at (match-end 2))
4352 (defun org-activate-bracket-links (limit)
4353 "Run through the buffer and add overlays to bracketed links."
4354 (if (re-search-forward org-bracket-link-regexp limit t)
4355 (let* ((help (concat "LINK: "
4356 (org-match-string-no-properties 1)))
4357 ;; FIXME: above we should remove the escapes.
4358 ;; but that requires another match, protecting match data,
4359 ;; a lot of overhead for font-lock.
4360 (ip (org-maybe-intangible
4361 (list 'invisible 'org-link
4362 'keymap org-mouse-map 'mouse-face 'highlight
4363 'font-lock-multiline t 'help-echo help)))
4364 (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
4365 'font-lock-multiline t 'help-echo help)))
4366 ;; We need to remove the invisible property here. Table narrowing
4367 ;; may have made some of this invisible.
4368 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4369 (remove-text-properties (match-beginning 0) (match-end 0)
4373 (add-text-properties (match-beginning 0) (match-beginning 3) ip)
4374 (org-rear-nonsticky-at (match-beginning 3))
4375 (add-text-properties (match-beginning 3) (match-end 3) vp)
4376 (org-rear-nonsticky-at (match-end 3))
4377 (add-text-properties (match-end 3) (match-end 0) ip)
4378 (org-rear-nonsticky-at (match-end 0)))
4379 (add-text-properties (match-beginning 0) (match-beginning 1) ip)
4380 (org-rear-nonsticky-at (match-beginning 1))
4381 (add-text-properties (match-beginning 1) (match-end 1) vp)
4382 (org-rear-nonsticky-at (match-end 1))
4383 (add-text-properties (match-end 1) (match-end 0) ip)
4384 (org-rear-nonsticky-at (match-end 0)))
4387 (defun org-activate-dates (limit)
4388 "Run through the buffer and add overlays to dates."
4389 (if (re-search-forward org-tsr-regexp-both limit t)
4391 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4392 (add-text-properties (match-beginning 0) (match-end 0)
4393 (list 'mouse-face 'highlight
4394 'keymap org-mouse-map))
4395 (org-rear-nonsticky-at (match-end 0))
4396 (when org-display-custom-times
4398 (org-display-custom-time (match-beginning 3) (match-end 3)))
4399 (org-display-custom-time (match-beginning 1) (match-end 1)))
4402 (defvar org-target-link-regexp nil
4403 "Regular expression matching radio targets in plain text.")
4404 (make-variable-buffer-local 'org-target-link-regexp)
4405 (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
4406 "Regular expression matching a link target.")
4407 (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
4408 "Regular expression matching a radio target.")
4409 (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
4410 "Regular expression matching any target.")
4412 (defun org-activate-target-links (limit)
4413 "Run through the buffer and add overlays to target matches."
4414 (when org-target-link-regexp
4415 (let ((case-fold-search t))
4416 (if (re-search-forward org-target-link-regexp limit t)
4418 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4419 (add-text-properties (match-beginning 0) (match-end 0)
4420 (list 'mouse-face 'highlight
4421 'keymap org-mouse-map
4422 'help-echo "Radio target link"
4423 'org-linked-text t))
4424 (org-rear-nonsticky-at (match-end 0))
4427 (defun org-update-radio-target-regexp ()
4428 "Find all radio targets in this file and update the regular expression."
4430 (when (memq 'radio org-activate-links)
4431 (setq org-target-link-regexp
4432 (org-make-target-link-regexp (org-all-targets 'radio)))
4433 (org-restart-font-lock)))
4435 (defun org-hide-wide-columns (limit)
4437 (setq s (text-property-any (point) (or limit (point-max))
4440 (setq e (next-single-property-change s 'org-cwidth))
4441 (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
4445 (defvar org-latex-and-specials-regexp nil
4446 "Regular expression for highlighting export special stuff.")
4447 (defvar org-match-substring-regexp)
4448 (defvar org-match-substring-with-braces-regexp)
4449 (defvar org-export-html-special-string-regexps)
4451 (defun org-compute-latex-and-specials-regexp ()
4452 "Compute regular expression for stuff treated specially by exporters."
4453 (if (not org-highlight-latex-fragments-and-specials)
4454 (org-set-local 'org-latex-and-specials-regexp nil)
4457 ((matchers (plist-get org-format-latex-options :matchers))
4458 (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
4459 org-latex-regexps)))
4460 (options (org-combine-plists (org-default-export-plist)
4461 (org-infile-export-plist)))
4462 (org-export-with-sub-superscripts (plist-get options :sub-superscript))
4463 (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
4464 (org-export-with-TeX-macros (plist-get options :TeX-macros))
4465 (org-export-html-expand (plist-get options :expand-quoted-html))
4466 (org-export-with-special-strings (plist-get options :special-strings))
4469 ((equal org-export-with-sub-superscripts '{})
4470 (list org-match-substring-with-braces-regexp))
4471 (org-export-with-sub-superscripts
4472 (list org-match-substring-regexp))
4475 (if org-export-with-LaTeX-fragments
4476 (mapcar (lambda (x) (nth 1 x)) latexs)))
4478 (if org-export-with-TeX-macros
4479 (list (concat "\\\\"
4481 (append (mapcar 'car org-html-entities)
4482 (if (boundp 'org-latex-entities)
4484 (or (car-safe x) x))
4489 ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
4490 (re-special (if org-export-with-special-strings
4491 (mapcar (lambda (x) (car x))
4492 org-export-html-special-string-regexps)))
4496 (if org-export-html-expand "@<[^>\n]+>")
4499 'org-latex-and-specials-regexp
4500 (mapconcat 'identity (append re-latex re-sub re-macros re-special
4503 (defun org-do-latex-and-special-faces (limit)
4504 "Run through the buffer and add overlays to links."
4505 (when org-latex-and-specials-regexp
4507 (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
4509 (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
4511 '(org-code org-verbatim underline)))
4514 d (cond ((member (char-after (1+ (match-beginning 0)))
4517 (font-lock-prepend-text-property
4518 (+ d (match-beginning 0)) (match-end 0)
4519 'face 'org-latex-and-export-specials)
4520 (add-text-properties (+ d (match-beginning 0)) (match-end 0)
4521 '(font-lock-multiline t)))))
4524 (defun org-restart-font-lock ()
4525 "Restart font-lock-mode, to force refontification."
4526 (when (and (boundp 'font-lock-mode) font-lock-mode)
4528 (font-lock-mode 1)))
4530 (defun org-all-targets (&optional radio)
4531 "Return a list of all targets in this file.
4532 With optional argument RADIO, only find radio targets."
4533 (let ((re (if radio org-radio-target-regexp org-target-regexp))
4536 (goto-char (point-min))
4537 (while (re-search-forward re nil t)
4538 (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
4541 (defun org-make-target-link-regexp (targets)
4542 "Make regular expression matching all strings in TARGETS.
4543 The regular expression finds the targets also if there is a line break
4550 (while (string-match " +" x)
4551 (setq x (replace-match "\\s-+" t t x)))
4557 (defun org-activate-tags (limit)
4558 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
4560 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4561 (add-text-properties (match-beginning 1) (match-end 1)
4562 (list 'mouse-face 'highlight
4563 'keymap org-mouse-map))
4564 (org-rear-nonsticky-at (match-end 1))
4567 (defun org-outline-level ()
4569 (looking-at outline-regexp)
4570 (if (match-beginning 1)
4571 (+ (org-get-string-indentation (match-string 1)) 1000)
4572 (1- (- (match-end 0) (match-beginning 0))))))
4574 (defvar org-font-lock-keywords nil)
4576 (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
4577 "Regular expression matching a property line.")
4579 (defvar org-font-lock-hook nil
4580 "Functions to be called for special font lock stuff.")
4582 (defun org-font-lock-hook (limit)
4583 (run-hook-with-args 'org-font-lock-hook limit))
4585 (defun org-set-font-lock-defaults ()
4586 (let* ((em org-fontify-emphasized-text)
4587 (lk org-activate-links)
4588 (org-font-lock-extra-keywords
4591 '(org-font-lock-hook)
4593 `(,(if org-fontify-whole-heading-line
4594 "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
4595 "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
4596 (1 (org-get-level-face 1))
4597 (2 (org-get-level-face 2))
4598 (3 (org-get-level-face 3)))
4600 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
4603 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
4604 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
4605 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
4606 '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
4608 (list org-drawer-regexp '(0 'org-special-keyword t))
4609 (list "^[ \t]*:END:" '(0 'org-special-keyword t))
4611 (list org-property-re
4612 '(1 'org-special-keyword t)
4613 '(3 'org-property-value t))
4615 (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
4616 (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
4617 (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
4618 (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
4619 (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
4620 (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
4621 (if (memq 'footnote lk) '(org-activate-footnote-links
4622 (2 'org-footnote t)))
4623 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
4624 '(org-hide-wide-columns (0 nil append))
4626 (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
4627 '(1 (org-get-todo-face 1) t))
4629 (if org-fontify-done-headline
4630 (list (concat "^[*]+ +\\<\\("
4631 (mapconcat 'regexp-quote org-done-keywords "\\|")
4633 '(2 'org-headline-done t))
4636 '(org-font-lock-add-priority-faces)
4638 '(org-font-lock-add-tag-faces)
4640 (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
4641 (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
4642 (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
4643 (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
4646 (if (featurep 'xemacs)
4647 '(org-do-emphasis-faces (0 nil append))
4648 '(org-do-emphasis-faces)))
4650 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
4651 2 'org-checkbox prepend)
4652 (if org-provide-checkbox-statistics
4653 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
4654 (0 (org-get-checkbox-statistics-face) t)))
4655 ;; Description list items
4656 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
4658 ;; ARCHIVEd headings
4659 (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
4660 '(1 'org-archived prepend))
4662 '(org-do-latex-and-special-faces)
4664 '(org-activate-code (1 'org-code t))
4666 (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
4667 "\\|" org-quote-string "\\)\\>")
4668 '(1 'org-special-keyword t))
4669 '("^#.*" (0 'font-lock-comment-face t))
4670 ;; Blocks and meta lines
4671 '(org-fontify-meta-lines-and-blocks)
4673 (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
4674 ;; Now set the full font-lock-keywords
4675 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
4676 (org-set-local 'font-lock-defaults
4677 '(org-font-lock-keywords t nil nil backward-paragraph))
4678 (kill-local-variable 'font-lock-keywords) nil))
4680 (defun org-fontify-like-in-org-mode (s &optional odd-levels)
4681 "Fontify string S like in Org-mode"
4684 (let ((org-odd-levels-only odd-levels))
4686 (font-lock-fontify-buffer)
4692 (defun org-get-level-face (n)
4693 "Get the right face for match N in font-lock matching of headlines."
4694 (setq org-l (- (match-end 2) (match-beginning 1) 1))
4695 (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
4696 (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
4698 ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
4700 (t (if org-level-color-stars-only nil org-f))))
4702 (defun org-get-todo-face (kwd)
4703 "Get the right face for a TODO keyword KWD.
4704 If KWD is a number, get the corresponding match group."
4705 (if (numberp kwd) (setq kwd (match-string kwd)))
4706 (or (cdr (assoc kwd org-todo-keyword-faces))
4707 (and (member kwd org-done-keywords) 'org-done)
4710 (defun org-font-lock-add-tag-faces (limit)
4711 "Add the special tag faces."
4712 (when (and org-tag-faces org-tags-special-faces-re)
4713 (while (re-search-forward org-tags-special-faces-re limit t)
4714 (add-text-properties (match-beginning 1) (match-end 1)
4715 (list 'face (org-get-tag-face 1)
4716 'font-lock-fontified t))
4717 (backward-char 1))))
4719 (defun org-font-lock-add-priority-faces (limit)
4720 "Add the special priority faces."
4721 (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
4722 (add-text-properties
4723 (match-beginning 0) (match-end 0)
4724 (list 'face (or (cdr (assoc (char-after (match-beginning 1))
4725 org-priority-faces))
4726 'org-special-keyword)
4727 'font-lock-fontified t))))
4729 (defun org-get-tag-face (kwd)
4730 "Get the right face for a TODO keyword KWD.
4731 If KWD is a number, get the corresponding match group."
4732 (if (numberp kwd) (setq kwd (match-string kwd)))
4733 (or (cdr (assoc kwd org-tag-faces))
4736 (defun org-unfontify-region (beg end &optional maybe_loudly)
4737 "Remove fontification and activation overlays from links."
4738 (font-lock-default-unfontify-region beg end)
4739 (let* ((buffer-undo-list t)
4740 (inhibit-read-only t) (inhibit-point-motion-hooks t)
4741 (inhibit-modification-hooks t)
4742 deactivate-mark buffer-file-name buffer-file-truename)
4743 (remove-text-properties beg end
4744 '(mouse-face t keymap t org-linked-text t
4745 invisible t intangible t
4746 line-prefix t wrap-prefix t
4747 org-no-flyspell t))))
4749 ;;;; Visibility cycling, including org-goto and indirect buffer
4753 (defvar org-cycle-global-status nil)
4754 (make-variable-buffer-local 'org-cycle-global-status)
4755 (defvar org-cycle-subtree-status nil)
4756 (make-variable-buffer-local 'org-cycle-subtree-status)
4760 (defvar org-inlinetask-min-level)
4762 (defun org-cycle (&optional arg)
4763 "TAB-action and visibility cycling for Org-mode.
4765 This is the command invoked in Org-moe by the TAB key. It's main purpose
4766 is outine visibility cycling, but it also invokes other actions
4767 in special contexts.
4769 - When this function is called with a prefix argument, rotate the entire
4770 buffer through 3 states (global cycling)
4771 1. OVERVIEW: Show only top-level headlines.
4772 2. CONTENTS: Show all headlines of all levels, but no body text.
4773 3. SHOW ALL: Show everything.
4774 When called with two `C-u C-u' prefixes, switch to the startup visibility,
4775 determined by the variable `org-startup-folded', and by any VISIBILITY
4776 properties in the buffer.
4777 When called with three `C-u C-u C-u' prefixed, show the entire buffer,
4778 including any drawers.
4780 - When inside a table, re-align the table and move to the next field.
4782 - When point is at the beginning of a headline, rotate the subtree started
4783 by this line through 3 different states (local cycling)
4784 1. FOLDED: Only the main headline is shown.
4785 2. CHILDREN: The main headline and the direct children are shown.
4786 From this state, you can move to one of the children
4787 and zoom in further.
4788 3. SUBTREE: Show the entire subtree, including body text.
4789 If there is no subtree, switch directly from CHILDREN to FOLDED.
4791 - When there is a numeric prefix, go up to a heading with level ARG, do
4792 a `show-subtree' and return to the previous cursor position. If ARG
4793 is negative, go up that many levels.
4795 - When point is not at the beginning of a headline, execute the global
4796 binding for TAB, which is re-indenting the line. See the option
4797 `org-cycle-emulate-tab' for details.
4799 - Special case: if point is at the beginning of the buffer and there is
4800 no headline in line 1, this function will act as if called with prefix arg.
4801 But only if also the variable `org-cycle-global-at-bob' is t."
4803 (org-load-modules-maybe)
4804 (unless (run-hook-with-args-until-success 'org-tab-first-hook)
4806 (or org-cycle-max-level
4807 (and (boundp 'org-inlinetask-min-level)
4808 org-inlinetask-min-level
4809 (1- org-inlinetask-min-level))))
4810 (nstars (and limit-level
4811 (if org-odd-levels-only
4812 (and limit-level (1- (* limit-level 2)))
4816 ((not (org-mode-p)) outline-regexp)
4817 ((or (eq org-cycle-include-plain-lists 'integrate)
4818 (and org-cycle-include-plain-lists (org-at-item-p)))
4820 (if nstars (format "\\{1,%d\\}" nstars) "+")
4821 " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
4822 (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
4823 (bob-special (and org-cycle-global-at-bob (bobp)
4824 (not (looking-at outline-regexp))))
4827 (delq 'org-optimize-window-after-visibility-change
4828 (copy-sequence org-cycle-hook))
4832 (if (or bob-special (equal arg '(4)))
4833 ;; special case: use global cycling
4839 (org-set-startup-visibility)
4840 (message "Startup visibility, plus VISIBILITY properties"))
4844 (message "Entire buffer visible, including drawers"))
4846 ((org-at-table-p 'any)
4847 ;; Enter the table or move to the next field in the table
4848 (or (org-table-recognize-table.el)
4850 (if arg (org-table-edit-field t)
4851 (org-table-justify-field-maybe)
4852 (call-interactively 'org-table-next-field)))))
4854 ((run-hook-with-args-until-success
4855 'org-tab-after-check-for-table-hook))
4857 ((eq arg t) ;; Global cycling
4858 (org-cycle-internal-global))
4860 ((and org-drawers org-drawer-regexp
4862 (beginning-of-line 1)
4863 (looking-at org-drawer-regexp)))
4864 ;; Toggle block visibility
4866 (not (get-char-property (match-end 0) 'invisible))))
4869 ;; Show-subtree, ARG levels up from here.
4871 (org-back-to-heading)
4872 (outline-up-heading (if (< arg 0) (- arg)
4873 (- (funcall outline-level) arg)))
4874 (org-show-subtree)))
4876 ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
4877 (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
4879 (org-cycle-internal-local))
4881 ;; TAB emulation and template completion
4882 (buffer-read-only (org-back-to-heading))
4884 ((run-hook-with-args-until-success
4885 'org-tab-after-check-for-cycling-hook))
4887 ((org-try-structure-completion))
4889 ((org-try-cdlatex-tab))
4891 ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
4893 (not (looking-at outline-regexp))))
4894 (call-interactively (global-key-binding "\t")))
4896 ((if (and (memq org-cycle-emulate-tab '(white whitestart))
4897 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
4898 (or (and (eq org-cycle-emulate-tab 'white)
4899 (= (match-end 0) (point-at-eol)))
4900 (and (eq org-cycle-emulate-tab 'whitestart)
4901 (>= (match-end 0) pos))))
4903 (eq org-cycle-emulate-tab t))
4904 (call-interactively (global-key-binding "\t")))
4907 (org-back-to-heading)
4910 (defun org-cycle-internal-global ()
4911 "Do the global cycling action."
4913 ((and (eq last-command this-command)
4914 (eq org-cycle-global-status 'overview))
4915 ;; We just created the overview - now do table of contents
4916 ;; This can be slow in very large buffers, so indicate action
4917 (run-hook-with-args 'org-pre-cycle-hook 'contents)
4918 (message "CONTENTS...")
4920 (message "CONTENTS...done")
4921 (setq org-cycle-global-status 'contents)
4922 (run-hook-with-args 'org-cycle-hook 'contents))
4924 ((and (eq last-command this-command)
4925 (eq org-cycle-global-status 'contents))
4926 ;; We just showed the table of contents - now show everything
4927 (run-hook-with-args 'org-pre-cycle-hook 'all)
4929 (message "SHOW ALL")
4930 (setq org-cycle-global-status 'all)
4931 (run-hook-with-args 'org-cycle-hook 'all))
4934 ;; Default action: go to overview
4935 (run-hook-with-args 'org-pre-cycle-hook 'overview)
4937 (message "OVERVIEW")
4938 (setq org-cycle-global-status 'overview)
4939 (run-hook-with-args 'org-cycle-hook 'overview))))
4941 (defun org-cycle-internal-local ()
4942 "Do the local cycling action."
4943 (org-back-to-heading)
4944 (let ((goal-column 0) eoh eol eos level has-children children-skipped)
4945 ;; First, some boundaries
4947 (org-back-to-heading)
4948 (setq level (funcall outline-level))
4950 (beginning-of-line 2)
4951 (if (or (featurep 'xemacs) (<= emacs-major-version 21))
4952 ; XEmacs does not have `next-single-char-property-change'
4953 ; I'm not sure about Emacs 21.
4954 (while (and (not (eobp)) ;; this is like `next-line'
4955 (get-char-property (1- (point)) 'invisible))
4956 (beginning-of-line 2))
4957 (while (and (not (eobp)) ;; this is like `next-line'
4958 (get-char-property (1- (point)) 'invisible))
4959 (goto-char (next-single-char-property-change (point) 'invisible))
4960 (or (bolp) (beginning-of-line 2))))
4962 (outline-end-of-heading) (setq eoh (point))
4964 (outline-next-heading)
4965 (setq has-children (and (org-at-heading-p t)
4966 (> (funcall outline-level) level))))
4967 (org-end-of-subtree t)
4969 (skip-chars-forward " \t\n")
4970 (beginning-of-line 1) ; in case this is an item
4972 (setq eos (1- (point))))
4973 ;; Find out what to do next and set `this-command'
4976 ;; Nothing is hidden behind this heading
4977 (run-hook-with-args 'org-pre-cycle-hook 'empty)
4978 (message "EMPTY ENTRY")
4979 (setq org-cycle-subtree-status nil)
4982 (outline-next-heading)
4983 (if (org-invisible-p) (org-flag-heading nil))))
4984 ((and (or (>= eol eos)
4985 (not (string-match "\\S-" (buffer-substring eol eos))))
4987 (not (setq children-skipped
4988 org-cycle-skip-children-state-if-no-children))))
4989 ;; Entire subtree is hidden in one line: children view
4990 (run-hook-with-args 'org-pre-cycle-hook 'children)
4993 (message "CHILDREN")
4996 (outline-next-heading)
4997 (if (org-invisible-p) (org-flag-heading nil)))
4998 (setq org-cycle-subtree-status 'children)
4999 (run-hook-with-args 'org-cycle-hook 'children))
5000 ((and (eq last-command this-command)
5001 (or children-skipped
5002 (eq org-cycle-subtree-status 'children)))
5003 ;; We just showed the children, now show everything.
5004 (run-hook-with-args 'org-pre-cycle-hook 'subtree)
5006 (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
5007 (setq org-cycle-subtree-status 'subtree)
5008 (run-hook-with-args 'org-cycle-hook 'subtree))
5010 ;; Default action: hide the subtree.
5011 (run-hook-with-args 'org-pre-cycle-hook 'folded)
5014 (setq org-cycle-subtree-status 'folded)
5015 (run-hook-with-args 'org-cycle-hook 'folded)))))
5018 (defun org-global-cycle (&optional arg)
5019 "Cycle the global visibility. For details see `org-cycle'.
5020 With C-u prefix arg, switch to startup visibility.
5021 With a numeric prefix, show all headlines up to that level."
5023 (let ((org-cycle-include-plain-lists
5024 (if (org-mode-p) org-cycle-include-plain-lists nil)))
5028 (hide-sublevels arg)
5029 (setq org-cycle-global-status 'contents))
5031 (org-set-startup-visibility)
5032 (message "Startup visibility, plus VISIBILITY properties."))
5034 (org-cycle '(4))))))
5036 (defun org-set-startup-visibility ()
5037 "Set the visibility required by startup options and properties."
5039 ((eq org-startup-folded t)
5041 ((eq org-startup-folded 'content)
5042 (let ((this-command 'org-cycle) (last-command 'org-cycle))
5043 (org-cycle '(4)) (org-cycle '(4)))))
5044 (if org-hide-block-startup (org-hide-block-all))
5045 (org-set-visibility-according-to-property 'no-cleanup)
5046 (org-cycle-hide-archived-subtrees 'all)
5047 (org-cycle-hide-drawers 'all)
5048 (org-cycle-show-empty-lines 'all))
5050 (defun org-set-visibility-according-to-property (&optional no-cleanup)
5051 "Switch subtree visibilities according to :VISIBILITY: property."
5053 (let (org-show-entry-below state)
5055 (goto-char (point-min))
5056 (while (re-search-forward
5057 "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
5059 (setq state (match-string 1))
5061 (org-back-to-heading t)
5065 ((equal state '("fold" "folded"))
5067 ((equal state "children")
5068 (org-show-hidden-entry)
5070 ((equal state "content")
5073 (org-narrow-to-subtree)
5075 ((member state '("all" "showall"))
5078 (org-cycle-hide-archived-subtrees 'all)
5079 (org-cycle-hide-drawers 'all)
5080 (org-cycle-show-empty-lines 'all)))))
5082 (defun org-overview ()
5083 "Switch to overview mode, showing only top-level headlines.
5084 Really, this shows all headlines with level equal or greater than the level
5085 of the first headline in the buffer. This is important, because if the
5086 first headline is not level one, then (hide-sublevels 1) gives confusing
5089 (let ((level (save-excursion
5090 (goto-char (point-min))
5091 (if (re-search-forward (concat "^" outline-regexp) nil t)
5093 (goto-char (match-beginning 0))
5094 (funcall outline-level))))))
5095 (and level (hide-sublevels level))))
5097 (defun org-content (&optional arg)
5098 "Show all headlines in the buffer, like a table of contents.
5099 With numerical argument N, show content up to level N."
5102 ;; Visit all headings and show their offspring
5103 (and (integerp arg) (org-overview))
5104 (goto-char (point-max))
5106 (while (and (progn (condition-case nil
5107 (outline-previous-visible-heading 1)
5108 (error (goto-char (point-min))))
5110 (looking-at outline-regexp))
5112 (show-children (1- arg))
5114 (if (bobp) (throw 'exit nil))))))
5117 (defun org-optimize-window-after-visibility-change (state)
5118 "Adjust the window after a change in outline visibility.
5119 This function is the default value of the hook `org-cycle-hook'."
5120 (when (get-buffer-window (current-buffer))
5122 ((eq state 'content) nil)
5123 ((eq state 'all) nil)
5124 ((eq state 'folded) nil)
5125 ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
5126 ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
5128 ;; FIXME: no longer in use
5129 (defun org-compact-display-after-subtree-move ()
5130 "Show a compacter version of the tree of the entry's parent."
5132 (if (org-up-heading-safe)
5137 (org-cycle-show-empty-lines 'children)
5138 (org-cycle-hide-drawers 'children))
5141 (defun org-remove-empty-overlays-at (pos)
5142 "Remove outline overlays that do not contain non-white stuff."
5145 (and (eq 'outline (org-overlay-get o 'invisible))
5146 (not (string-match "\\S-" (buffer-substring (org-overlay-start o)
5147 (org-overlay-end o))))
5148 (org-delete-overlay o)))
5149 (org-overlays-at pos)))
5151 (defun org-clean-visibility-after-subtree-move ()
5152 "Fix visibility issues after moving a subtree."
5153 ;; First, find a reasonable region to look at:
5154 ;; Start two siblings above, end three below
5155 (let* ((beg (save-excursion
5156 (and (outline-get-last-sibling)
5157 (outline-get-last-sibling))
5159 (end (save-excursion
5160 (and (outline-get-next-sibling)
5161 (outline-get-next-sibling)
5162 (outline-get-next-sibling))
5163 (if (org-at-heading-p)
5166 (level (looking-at "\\*+"))
5167 (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
5170 (narrow-to-region beg end)
5172 ;; Properly fold already folded siblings
5173 (goto-char (point-min))
5174 (while (re-search-forward re nil t)
5175 (if (save-excursion (goto-char (point-at-eol)) (org-invisible-p))
5177 (org-cycle-show-empty-lines 'overview)
5178 (org-cycle-hide-drawers 'overview)))))
5180 (defun org-cycle-show-empty-lines (state)
5181 "Show empty lines above all visible headlines.
5182 The region to be covered depends on STATE when called through
5183 `org-cycle-hook'. Lisp program can use t for STATE to get the
5184 entire buffer covered. Note that an empty line is only shown if there
5185 are at least `org-cycle-separator-lines' empty lines before the headline."
5186 (when (> org-cycle-separator-lines 0)
5188 (let* ((n org-cycle-separator-lines)
5190 ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
5191 ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
5192 (t (let ((ns (number-to-string (- n 2))))
5193 (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
5194 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
5197 ((memq state '(overview contents t))
5198 (setq beg (point-min) end (point-max)))
5199 ((memq state '(children folded))
5200 (setq beg (point) end (progn (org-end-of-subtree t t)
5201 (beginning-of-line 2)
5205 (while (re-search-forward re end t)
5206 (if (not (get-char-property (match-end 1) 'invisible))
5207 (outline-flag-region
5208 (match-beginning 1) (match-end 1) nil)))))))
5209 ;; Never hide empty lines at the end of the file.
5211 (goto-char (point-max))
5212 (outline-previous-heading)
5213 (outline-end-of-heading)
5214 (if (and (looking-at "[ \t\n]+")
5215 (= (match-end 0) (point-max)))
5216 (outline-flag-region (point) (match-end 0) nil))))
5218 (defun org-show-empty-lines-in-parent ()
5219 "Move to the parent and re-show empty lines before visible headlines."
5221 (let ((context (if (org-up-heading-safe) 'children 'overview)))
5222 (org-cycle-show-empty-lines context))))
5224 (defun org-cycle-hide-drawers (state)
5225 "Re-hide all drawers after a visibility state change."
5226 (when (and (org-mode-p)
5227 (not (memq state '(overview folded contents))))
5229 (let* ((globalp (memq state '(contents all)))
5230 (beg (if globalp (point-min) (point)))
5231 (end (if globalp (point-max)
5232 (if (eq state 'children)
5233 (save-excursion (outline-next-heading) (point))
5234 (org-end-of-subtree t)))))
5236 (while (re-search-forward org-drawer-regexp end t)
5237 (org-flag-drawer t))))))
5239 (defun org-flag-drawer (flag)
5241 (beginning-of-line 1)
5242 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
5243 (let ((b (match-end 0))
5244 (outline-regexp org-outline-regexp))
5245 (if (re-search-forward
5247 (save-excursion (outline-next-heading) (point)) t)
5248 (outline-flag-region b (point-at-eol) flag)
5249 (error ":END: line missing"))))))
5251 (defun org-subtree-end-visible-p ()
5252 "Is the end of the current subtree visible?"
5253 (pos-visible-in-window-p
5254 (save-excursion (org-end-of-subtree t) (point))))
5256 (defun org-first-headline-recenter (&optional N)
5257 "Move cursor to the first headline and recenter the headline.
5258 Optional argument N means, put the headline into the Nth line of the window."
5259 (goto-char (point-min))
5260 (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
5262 (recenter (prefix-numeric-value N))))
5264 ;;; Folding of blocks
5266 (defconst org-block-regexp
5268 "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
5269 "Regular expression for hiding blocks.")
5271 (defvar org-hide-block-overlays nil
5272 "Overays hiding blocks.")
5273 (make-variable-buffer-local 'org-hide-block-overlays)
5275 (defun org-block-map (function &optional start end)
5276 "Call func at the head of all source blocks in the current
5277 buffer. Optional arguments START and END can be used to limit
5279 (let ((start (or start (point-min)))
5280 (end (or end (point-max))))
5283 (while (and (< (point) end) (re-search-forward org-block-regexp end t))
5286 (goto-char (match-beginning 0))
5287 (funcall function)))))))
5289 (defun org-hide-block-toggle-all ()
5290 "Toggle the visibility of all blocks in the current buffer."
5291 (org-block-map #'org-hide-block-toggle))
5293 (defun org-hide-block-all ()
5294 "Fold all blocks in the current buffer."
5296 (org-show-block-all)
5297 (org-block-map #'org-hide-block-toggle-maybe))
5299 (defun org-show-block-all ()
5300 "Unfold all blocks in the current buffer."
5301 (mapc 'org-delete-overlay org-hide-block-overlays)
5302 (setq org-hide-block-overlays nil))
5304 (defun org-hide-block-toggle-maybe ()
5305 "Toggle visibility of block at point."
5307 (let ((case-fold-search t))
5309 (beginning-of-line 1)
5310 (looking-at org-block-regexp))
5311 (progn (org-hide-block-toggle)
5312 t) ;; to signal that we took action
5313 nil))) ;; to signal that we did not
5315 (defun org-hide-block-toggle (&optional force)
5316 "Toggle the visibility of the current block."
5320 (if (re-search-forward org-block-regexp nil t)
5321 (let ((start (- (match-beginning 4) 1)) ;; beginning of body
5323 ov) ;; end of entire body
5324 (if (memq t (mapcar (lambda (overlay)
5325 (eq (org-overlay-get overlay 'invisible)
5327 (org-overlays-at start)))
5328 (if (or (not force) (eq force 'off))
5330 (when (member ov org-hide-block-overlays)
5331 (setq org-hide-block-overlays
5332 (delq ov org-hide-block-overlays)))
5333 (when (eq (org-overlay-get ov 'invisible)
5335 (org-delete-overlay ov)))
5336 (org-overlays-at start)))
5337 (setq ov (org-make-overlay start end))
5338 (org-overlay-put ov 'invisible 'org-hide-block)
5339 (push ov org-hide-block-overlays)))
5340 (error "Not looking at a source block"))))
5342 ;; org-tab-after-check-for-cycling-hook
5343 (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
5344 ;; Remove overlays when changing major mode
5345 (add-hook 'org-mode-hook
5346 (lambda () (org-add-hook 'change-major-mode-hook
5347 'org-show-block-all 'append 'local)))
5351 (defvar org-goto-window-configuration nil)
5352 (defvar org-goto-marker nil)
5353 (defvar org-goto-map
5354 (let ((map (make-sparse-keymap)))
5355 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
5356 (while (setq cmd (pop cmds))
5357 (substitute-key-definition cmd cmd map global-map)))
5358 (suppress-keymap map)
5359 (org-defkey map "\C-m" 'org-goto-ret)
5360 (org-defkey map [(return)] 'org-goto-ret)
5361 (org-defkey map [(left)] 'org-goto-left)
5362 (org-defkey map [(right)] 'org-goto-right)
5363 (org-defkey map [(control ?g)] 'org-goto-quit)
5364 (org-defkey map "\C-i" 'org-cycle)
5365 (org-defkey map [(tab)] 'org-cycle)
5366 (org-defkey map [(down)] 'outline-next-visible-heading)
5367 (org-defkey map [(up)] 'outline-previous-visible-heading)
5368 (if org-goto-auto-isearch
5369 (if (fboundp 'define-key-after)
5370 (define-key-after map [t] 'org-goto-local-auto-isearch)
5372 (org-defkey map "q" 'org-goto-quit)
5373 (org-defkey map "n" 'outline-next-visible-heading)
5374 (org-defkey map "p" 'outline-previous-visible-heading)
5375 (org-defkey map "f" 'outline-forward-same-level)
5376 (org-defkey map "b" 'outline-backward-same-level)
5377 (org-defkey map "u" 'outline-up-heading))
5378 (org-defkey map "/" 'org-occur)
5379 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
5380 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
5381 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
5382 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
5383 (org-defkey map "\C-c\C-u" 'outline-up-heading)
5386 (defconst org-goto-help
5387 "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
5388 RET=jump to location [Q]uit and return to previous location
5389 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
5391 (defvar org-goto-start-pos) ; dynamically scoped parameter
5393 ;; FIXME: Docstring doe not mention both interfaces
5394 (defun org-goto (&optional alternative-interface)
5395 "Look up a different location in the current file, keeping current visibility.
5397 When you want look-up or go to a different location in a document, the
5398 fastest way is often to fold the entire buffer and then dive into the tree.
5399 This method has the disadvantage, that the previous location will be folded,
5400 which may not be what you want.
5402 This command works around this by showing a copy of the current buffer
5403 in an indirect buffer, in overview mode. You can dive into the tree in
5404 that copy, use org-occur and incremental search to find a location.
5405 When pressing RET or `Q', the command returns to the original buffer in
5406 which the visibility is still unchanged. After RET is will also jump to
5407 the location selected in the indirect buffer and expose the
5408 the headline hierarchy above."
5410 (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
5411 (org-refile-use-outline-path t)
5412 (org-refile-target-verify-function nil)
5414 (if (not alternative-interface)
5416 (if (eq org-goto-interface 'outline)
5417 'outline-path-completion
5419 (org-goto-start-pos (point))
5421 (if (eq interface 'outline)
5422 (car (org-get-location (current-buffer) org-goto-help))
5423 (nth 3 (org-refile-get-location "Goto: ")))))
5426 (org-mark-ring-push org-goto-start-pos)
5427 (goto-char selected-point)
5428 (if (or (org-invisible-p) (org-invisible-p2))
5429 (org-show-context 'org-goto)))
5432 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
5433 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
5434 (defvar org-goto-local-auto-isearch-map) ; defined below
5436 (defun org-get-location (buf help)
5437 "Let the user select a location in the Org-mode buffer BUF.
5438 This function uses a recursive edit. It returns the selected position
5440 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
5441 (isearch-hide-immediately nil)
5442 (isearch-search-fun-function
5443 (lambda () 'org-goto-local-search-headings))
5444 (org-goto-selected-point org-goto-exit-command))
5446 (save-window-excursion
5447 (delete-other-windows)
5448 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
5451 (make-indirect-buffer (current-buffer) "*org-goto*")
5452 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
5453 (with-output-to-temp-buffer "*Help*"
5455 (org-fit-window-to-buffer (get-buffer-window "*Help*"))
5456 (setq buffer-read-only nil)
5457 (let ((org-startup-truncated t)
5458 (org-startup-folded nil)
5459 (org-startup-align-all-tables nil))
5462 (setq buffer-read-only t)
5463 (if (and (boundp 'org-goto-start-pos)
5464 (integer-or-marker-p org-goto-start-pos))
5465 (let ((org-show-hierarchy-above t)
5466 (org-show-siblings t)
5467 (org-show-following-heading t))
5468 (goto-char org-goto-start-pos)
5469 (and (org-invisible-p) (org-show-context)))
5470 (goto-char (point-min)))
5471 (let (org-special-ctrl-a/e) (org-beginning-of-line))
5472 (message "Select location and press RET")
5473 (use-local-map org-goto-map)
5476 (kill-buffer "*org-goto*")
5477 (cons org-goto-selected-point org-goto-exit-command)))
5479 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
5480 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
5481 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
5482 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
5484 (defun org-goto-local-search-headings (string bound noerror)
5485 "Search and make sure that any matches are in headlines."
5487 (while (if isearch-forward
5488 (search-forward string bound noerror)
5489 (search-backward string bound noerror))
5490 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
5491 (and (member :headline context)
5492 (not (member :tags context))))
5493 (throw 'return (point))))))
5495 (defun org-goto-local-auto-isearch ()
5498 (goto-char (point-min))
5499 (let ((keys (this-command-keys)))
5500 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
5502 (isearch-process-search-char (string-to-char keys)))))
5504 (defun org-goto-ret (&optional arg)
5505 "Finish `org-goto' by going to the new location."
5507 (setq org-goto-selected-point (point)
5508 org-goto-exit-command 'return)
5511 (defun org-goto-left ()
5512 "Finish `org-goto' by going to the new location."
5514 (if (org-on-heading-p)
5516 (beginning-of-line 1)
5517 (setq org-goto-selected-point (point)
5518 org-goto-exit-command 'left)
5520 (error "Not on a heading")))
5522 (defun org-goto-right ()
5523 "Finish `org-goto' by going to the new location."
5525 (if (org-on-heading-p)
5527 (setq org-goto-selected-point (point)
5528 org-goto-exit-command 'right)
5530 (error "Not on a heading")))
5532 (defun org-goto-quit ()
5533 "Finish `org-goto' without cursor motion."
5535 (setq org-goto-selected-point nil)
5536 (setq org-goto-exit-command 'quit)
5539 ;;; Indirect buffer display of subtrees
5541 (defvar org-indirect-dedicated-frame nil
5542 "This is the frame being used for indirect tree display.")
5543 (defvar org-last-indirect-buffer nil)
5545 (defun org-tree-to-indirect-buffer (&optional arg)
5546 "Create indirect buffer and narrow it to current subtree.
5547 With numerical prefix ARG, go up to this level and then take that tree.
5548 If ARG is negative, go up that many levels.
5549 If `org-indirect-buffer-display' is not `new-frame', the command removes the
5550 indirect buffer previously made with this command, to avoid proliferation of
5551 indirect buffers. However, when you call the command with a `C-u' prefix, or
5552 when `org-indirect-buffer-display' is `new-frame', the last buffer
5553 is kept so that you can work with several indirect buffers at the same time.
5554 If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
5555 requests that a new frame be made for the new buffer, so that the dedicated
5556 frame is not changed."
5558 (let ((cbuf (current-buffer))
5559 (cwin (selected-window))
5561 beg end level heading ibuf)
5563 (org-back-to-heading t)
5565 (setq level (org-outline-level))
5566 (if (< arg 0) (setq arg (+ level arg)))
5567 (while (> (setq level (org-outline-level)) arg)
5568 (outline-up-heading 1 t)))
5570 heading (org-get-heading))
5571 (org-end-of-subtree t) (setq end (point)))
5572 (if (and (buffer-live-p org-last-indirect-buffer)
5573 (not (eq org-indirect-buffer-display 'new-frame))
5575 (kill-buffer org-last-indirect-buffer))
5576 (setq ibuf (org-get-indirect-buffer cbuf)
5577 org-last-indirect-buffer ibuf)
5579 ((or (eq org-indirect-buffer-display 'new-frame)
5580 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
5581 (select-frame (make-frame))
5582 (delete-other-windows)
5583 (switch-to-buffer ibuf)
5584 (org-set-frame-title heading))
5585 ((eq org-indirect-buffer-display 'dedicated-frame)
5587 (select-frame (or (and org-indirect-dedicated-frame
5588 (frame-live-p org-indirect-dedicated-frame)
5589 org-indirect-dedicated-frame)
5590 (setq org-indirect-dedicated-frame (make-frame)))))
5591 (delete-other-windows)
5592 (switch-to-buffer ibuf)
5593 (org-set-frame-title (concat "Indirect: " heading)))
5594 ((eq org-indirect-buffer-display 'current-window)
5595 (switch-to-buffer ibuf))
5596 ((eq org-indirect-buffer-display 'other-window)
5597 (pop-to-buffer ibuf))
5598 (t (error "Invalid value.")))
5599 (if (featurep 'xemacs)
5600 (save-excursion (org-mode) (turn-on-font-lock)))
5601 (narrow-to-region beg end)
5604 (and (window-live-p cwin) (select-window cwin))))
5606 (defun org-get-indirect-buffer (&optional buffer)
5607 (setq buffer (or buffer (current-buffer)))
5608 (let ((n 1) (base (buffer-name buffer)) bname)
5609 (while (buffer-live-p
5610 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
5613 (make-indirect-buffer buffer bname 'clone)
5614 (error (make-indirect-buffer buffer bname)))))
5616 (defun org-set-frame-title (title)
5617 "Set the title of the current frame to the string TITLE."
5618 ;; FIXME: how to name a single frame in XEmacs???
5619 (unless (featurep 'xemacs)
5620 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
5622 ;;;; Structure editing
5624 ;;; Inserting headlines
5626 (defun org-previous-line-empty-p ()
5629 (or (beginning-of-line 0) t)
5631 (looking-at "[ \t]*$")))))
5633 (defun org-insert-heading (&optional force-heading)
5634 "Insert a new heading or item with same depth at point.
5635 If point is in a plain list and FORCE-HEADING is nil, create a new list item.
5636 If point is at the beginning of a headline, insert a sibling before the
5637 current headline. If point is not at the beginning, do not split the line,
5638 but create the new headline after the current line."
5640 (if (= (buffer-size) 0)
5642 (when (or force-heading (not (org-insert-item)))
5643 (let* ((empty-line-p nil)
5644 (head (save-excursion
5647 (org-back-to-heading)
5648 (setq empty-line-p (org-previous-line-empty-p))
5651 (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
5652 (blank (if (eq blank-a 'auto) empty-line-p blank-a))
5653 pos hide-previous previous-pos)
5655 ((and (org-on-heading-p) (bolp)
5657 (save-excursion (backward-char 1) (not (org-invisible-p)))))
5658 ;; insert before the current line
5659 (open-line (if blank 2 1)))
5663 (backward-char 1) (not (org-invisible-p)))))
5664 ;; insert right here
5667 ;; somewhere in the line
5669 (setq previous-pos (point-at-bol))
5671 (setq hide-previous (org-invisible-p)))
5672 (and org-insert-heading-respect-content (org-show-subtree))
5674 (and (org-get-alist-option org-M-RET-may-split-line 'headline)
5677 (goto-char (point-at-bol))
5678 (and (looking-at org-complex-heading-regexp)
5679 (> p (match-beginning 4)))))))
5682 (org-insert-heading-respect-content
5683 (org-end-of-subtree nil t)
5684 (or (bolp) (newline))
5685 (or (org-previous-line-empty-p)
5686 (and blank (newline)))
5692 (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
5693 (setq tags (and (match-end 2) (match-string 2)))
5695 (delete-region (match-beginning 1) (match-end 1)))
5696 (setq pos (point-at-bol))
5697 (or split (end-of-line 1))
5698 (delete-horizontal-space)
5699 (newline (if blank 2 1))
5705 (org-set-tags nil 'align))))
5707 (or split (end-of-line 1))
5708 (newline (if blank 2 1)))))))
5709 (insert head) (just-one-space)
5712 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
5713 (when (and org-insert-heading-respect-content hide-previous)
5715 (goto-char previous-pos)
5717 (run-hooks 'org-insert-heading-hook)))))
5719 (defun org-get-heading (&optional no-tags)
5720 "Return the heading of the current entry, without the stars."
5722 (org-back-to-heading t)
5725 (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
5726 "\\*+[ \t]+\\([^\r\n]*\\)"))
5727 (match-string 1) "")))
5729 (defun org-heading-components ()
5730 "Return the components of the current heading.
5731 This is a list with the following elements:
5732 - the level as an integer
5733 - the reduced level, different if `org-odd-levels-only' is set.
5734 - the TODO keyword, or nil
5735 - the priority character, like ?A, or nil if no priority is given
5736 - the headline text itself, or the tags string if no headline text
5737 - the tags string, or nil."
5739 (org-back-to-heading t)
5740 (if (looking-at org-complex-heading-regexp)
5741 (list (length (match-string 1))
5742 (org-reduced-level (length (match-string 1)))
5743 (org-match-string-no-properties 2)
5744 (and (match-end 3) (aref (match-string 3) 2))
5745 (org-match-string-no-properties 4)
5746 (org-match-string-no-properties 5)))))
5748 (defun org-get-entry ()
5749 "Get the entry text, after heading, entire subtree."
5751 (org-back-to-heading t)
5752 (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
5754 (defun org-insert-heading-after-current ()
5755 "Insert a new heading with same level as current, after current subtree."
5757 (org-back-to-heading)
5758 (org-insert-heading)
5759 (org-move-subtree-down)
5762 (defun org-insert-heading-respect-content ()
5764 (let ((org-insert-heading-respect-content t))
5765 (org-insert-heading t)))
5767 (defun org-insert-todo-heading-respect-content (&optional force-state)
5769 (let ((org-insert-heading-respect-content t))
5770 (org-insert-todo-heading force-state t)))
5772 (defun org-insert-todo-heading (arg &optional force-heading)
5773 "Insert a new heading with the same level and TODO state as current heading.
5774 If the heading has no TODO state, or if the state is DONE, use the first
5775 state (TODO by default). Also with prefix arg, force first state."
5777 (when (or force-heading (not (org-insert-item 'checkbox)))
5778 (org-insert-heading force-heading)
5780 (org-back-to-heading)
5781 (outline-previous-heading)
5782 (looking-at org-todo-line-regexp))
5786 (not (match-beginning 2))
5787 (member (match-string 2) org-done-keywords))
5788 (car org-todo-keywords-1)
5792 (run-hook-with-args-until-success
5793 'org-todo-get-default-hook new-mark-x nil)
5795 (beginning-of-line 1)
5796 (and (looking-at "\\*+ ") (goto-char (match-end 0))
5797 (if org-treat-insert-todo-heading-as-state-change
5799 (insert new-mark " "))))
5800 (when org-provide-todo-statistics
5801 (org-update-parent-todo-statistics))))
5803 (defun org-insert-subheading (arg)
5804 "Insert a new subheading and demote it.
5805 Works for outline headings and for plain lists alike."
5807 (org-insert-heading arg)
5809 ((org-on-heading-p) (org-do-demote))
5810 ((org-at-item-p) (org-indent-item 1))))
5812 (defun org-insert-todo-subheading (arg)
5813 "Insert a new subheading with TODO keyword or checkbox and demote it.
5814 Works for outline headings and for plain lists alike."
5816 (org-insert-todo-heading arg)
5818 ((org-on-heading-p) (org-do-demote))
5819 ((org-at-item-p) (org-indent-item 1))))
5821 ;;; Promotion and Demotion
5823 (defvar org-after-demote-entry-hook nil
5824 "Hook run after an entry has been demoted.
5825 The cursor will be at the beginning of the entry.
5826 When a subtree is being demoted, the hook will be called for each node.")
5828 (defvar org-after-promote-entry-hook nil
5829 "Hook run after an entry has been promoted.
5830 The cursor will be at the beginning of the entry.
5831 When a subtree is being promoted, the hook will be called for each node.")
5833 (defun org-promote-subtree ()
5834 "Promote the entire subtree.
5835 See also `org-promote'."
5838 (org-map-tree 'org-promote))
5839 (org-fix-position-after-promote))
5841 (defun org-demote-subtree ()
5842 "Demote the entire subtree. See `org-demote'.
5843 See also `org-promote'."
5846 (org-map-tree 'org-demote))
5847 (org-fix-position-after-promote))
5850 (defun org-do-promote ()
5851 "Promote the current heading higher up the tree.
5852 If the region is active in `transient-mark-mode', promote all headings
5856 (if (org-region-active-p)
5857 (org-map-region 'org-promote (region-beginning) (region-end))
5859 (org-fix-position-after-promote))
5861 (defun org-do-demote ()
5862 "Demote the current heading lower down the tree.
5863 If the region is active in `transient-mark-mode', demote all headings
5867 (if (org-region-active-p)
5868 (org-map-region 'org-demote (region-beginning) (region-end))
5870 (org-fix-position-after-promote))
5872 (defun org-fix-position-after-promote ()
5873 "Make sure that after pro/demotion cursor position is right."
5874 (let ((pos (point)))
5875 (when (save-excursion
5876 (beginning-of-line 1)
5877 (looking-at org-todo-line-regexp)
5878 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
5879 (cond ((eobp) (insert " "))
5880 ((eolp) (insert " "))
5881 ((equal (char-after) ?\ ) (forward-char 1))))))
5883 (defun org-reduced-level (l)
5884 "Compute the effective level of a heading.
5885 This takes into account the setting of `org-odd-levels-only'."
5886 (if org-odd-levels-only (1+ (floor (/ l 2))) l))
5888 (defun org-get-valid-level (level &optional change)
5889 "Rectify a level change under the influence of `org-odd-levels-only'
5890 LEVEL is a current level, CHANGE is by how much the level should be
5891 modified. Even if CHANGE is nil, LEVEL may be returned modified because
5892 even level numbers will become the next higher odd number."
5893 (if org-odd-levels-only
5894 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
5895 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
5896 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
5897 (max 1 (+ level (or change 0)))))
5899 (if (boundp 'define-obsolete-function-alias)
5900 (if (or (featurep 'xemacs) (< emacs-major-version 23))
5901 (define-obsolete-function-alias 'org-get-legal-level
5902 'org-get-valid-level)
5903 (define-obsolete-function-alias 'org-get-legal-level
5904 'org-get-valid-level "23.1")))
5906 (defun org-promote ()
5907 "Promote the current heading higher up the tree.
5908 If the region is active in `transient-mark-mode', promote all headings
5910 (org-back-to-heading t)
5911 (let* ((level (save-match-data (funcall outline-level)))
5912 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
5913 (diff (abs (- level (length up-head) -1))))
5914 (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
5915 (replace-match up-head nil t)
5916 ;; Fixup tag positioning
5917 (and org-auto-align-tags (org-set-tags nil t))
5918 (if org-adapt-indentation (org-fixup-indentation (- diff)))
5919 (run-hooks 'org-after-promote-entry-hook)))
5921 (defun org-demote ()
5922 "Demote the current heading lower down the tree.
5923 If the region is active in `transient-mark-mode', demote all headings
5925 (org-back-to-heading t)
5926 (let* ((level (save-match-data (funcall outline-level)))
5927 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
5928 (diff (abs (- level (length down-head) -1))))
5929 (replace-match down-head nil t)
5930 ;; Fixup tag positioning
5931 (and org-auto-align-tags (org-set-tags nil t))
5932 (if org-adapt-indentation (org-fixup-indentation diff))
5933 (run-hooks 'org-after-demote-entry-hook)))
5935 (defun org-map-tree (fun)
5936 "Call FUN for every heading underneath the current one."
5937 (org-back-to-heading)
5938 (let ((level (funcall outline-level)))
5942 (outline-next-heading)
5943 (> (funcall outline-level) level))
5947 (defun org-map-region (fun beg end)
5948 "Call FUN for every heading between BEG and END."
5949 (let ((org-ignore-region t))
5951 (setq end (copy-marker end))
5953 (if (and (re-search-forward (concat "^" outline-regexp) nil t)
5957 (outline-next-heading)
5962 (defun org-fixup-indentation (diff)
5963 "Change the indentation in the current entry by DIFF
5964 However, if any line in the current entry has no indentation, or if it
5965 would end up with no indentation after the change, nothing at all is done."
5967 (let ((end (save-excursion (outline-next-heading)
5969 (prohibit (if (> diff 0)
5971 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
5973 (unless (save-excursion (end-of-line 1)
5974 (re-search-forward prohibit end t))
5975 (while (and (< (point) end)
5976 (re-search-forward "^[ \t]+" end t))
5977 (goto-char (match-end 0))
5978 (setq col (current-column))
5979 (if (< diff 0) (replace-match ""))
5980 (org-indent-to-column (+ diff col))))
5981 (move-marker end nil))))
5983 (defun org-convert-to-odd-levels ()
5984 "Convert an org-mode file with all levels allowed to one with odd levels.
5985 This will leave level 1 alone, convert level 2 to level 3, level 3 to
5988 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
5989 (let ((org-odd-levels-only nil) n)
5991 (goto-char (point-min))
5992 (while (re-search-forward "^\\*\\*+ " nil t)
5993 (setq n (- (length (match-string 0)) 2))
5994 (while (>= (setq n (1- n)) 0)
5996 (end-of-line 1))))))
5999 (defun org-convert-to-oddeven-levels ()
6000 "Convert an org-mode file with only odd levels to one with odd and even levels.
6001 This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
6002 section with an even level, conversion would destroy the structure of the file. An error
6003 is signaled in this case."
6005 (goto-char (point-min))
6006 ;; First check if there are no even levels
6007 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
6008 (org-show-context t)
6009 (error "Not all levels are odd in this file. Conversion not possible."))
6010 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
6011 (let ((org-odd-levels-only nil) n)
6013 (goto-char (point-min))
6014 (while (re-search-forward "^\\*\\*+ " nil t)
6015 (setq n (/ (1- (length (match-string 0))) 2))
6016 (while (>= (setq n (1- n)) 0)
6018 (end-of-line 1))))))
6020 (defun org-tr-level (n)
6021 "Make N odd if required."
6022 (if org-odd-levels-only (1+ (/ n 2)) n))
6024 ;;; Vertical tree motion, cutting and pasting of subtrees
6026 (defun org-move-subtree-up (&optional arg)
6027 "Move the current subtree up past ARG headlines of the same level."
6029 (org-move-subtree-down (- (prefix-numeric-value arg))))
6031 (defun org-move-subtree-down (&optional arg)
6032 "Move the current subtree down past ARG headlines of the same level."
6034 (setq arg (prefix-numeric-value arg))
6035 (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
6036 'outline-get-last-sibling))
6037 (ins-point (make-marker))
6039 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
6041 (org-back-to-heading)
6044 (setq ne-beg (org-back-over-empty-lines))
6047 (save-excursion (outline-end-of-heading)
6048 (setq folded (org-invisible-p)))
6049 (outline-end-of-subtree))
6050 (outline-next-heading)
6051 (setq ne-end (org-back-over-empty-lines))
6054 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
6055 ;; include less whitespace
6058 (forward-line (- ne-beg ne-end))
6059 (setq beg (point))))
6060 ;; Find insertion point, with error handling
6062 (or (and (funcall movfunc) (looking-at outline-regexp))
6063 (progn (goto-char beg0)
6064 (error "Cannot move past superior level or buffer limit")))
6065 (setq cnt (1- cnt)))
6067 ;; Moving forward - still need to move over subtree
6068 (progn (org-end-of-subtree t t)
6070 (org-back-over-empty-lines)
6071 (or (bolp) (newline)))))
6072 (setq ne-ins (org-back-over-empty-lines))
6073 (move-marker ins-point (point))
6074 (setq txt (buffer-substring beg end))
6075 (org-save-markers-in-region beg end)
6076 (delete-region beg end)
6077 (org-remove-empty-overlays-at beg)
6078 (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
6079 (or (bobp) (outline-flag-region (1- (point)) (point) nil))
6080 (and (not (bolp)) (looking-at "\n") (forward-char 1))
6081 (let ((bbb (point)))
6082 (insert-before-markers txt)
6083 (org-reinstall-markers-in-region bbb)
6084 (move-marker ins-point bbb))
6085 (or (bolp) (insert "\n"))
6086 (setq ins-end (point))
6087 (goto-char ins-point)
6088 (org-skip-whitespace)
6089 (when (and (< arg 0)
6090 (org-first-sibling-p)
6092 ;; Move whitespace back to beginning
6095 (let ((kill-whole-line t))
6096 (kill-line (- ne-ins ne-beg)) (point)))
6097 (insert (make-string (- ne-ins ne-beg) ?\n)))
6098 (move-marker ins-point nil)
6103 (org-cycle-hide-drawers 'children))
6104 (org-clean-visibility-after-subtree-move)))
6106 (defvar org-subtree-clip ""
6107 "Clipboard for cut and paste of subtrees.
6108 This is actually only a copy of the kill, because we use the normal kill
6109 ring. We need it to check if the kill was created by `org-copy-subtree'.")
6111 (defvar org-subtree-clip-folded nil
6112 "Was the last copied subtree folded?
6113 This is used to fold the tree back after pasting.")
6115 (defun org-cut-subtree (&optional n)
6116 "Cut the current subtree into the clipboard.
6117 With prefix arg N, cut this many sequential subtrees.
6118 This is a short-hand for marking the subtree and then cutting it."
6120 (org-copy-subtree n 'cut))
6122 (defun org-copy-subtree (&optional n cut force-store-markers)
6123 "Cut the current subtree into the clipboard.
6124 With prefix arg N, cut this many sequential subtrees.
6125 This is a short-hand for marking the subtree and then copying it.
6126 If CUT is non-nil, actually cut the subtree.
6127 If FORCE-STORE-MARKERS is non-nil, store the relative locations
6128 of some markers in the region, even if CUT is non-nil. This is
6129 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
6131 (let (beg end folded (beg0 (point)))
6133 (org-back-to-heading nil) ; take what looks like a subtree
6134 (org-back-to-heading t)) ; take what is really there
6135 (org-back-over-empty-lines)
6137 (skip-chars-forward " \t\r\n")
6139 (save-excursion (outline-end-of-heading)
6140 (setq folded (org-invisible-p)))
6142 (outline-forward-same-level (1- n))
6144 (org-end-of-subtree t t))
6145 (org-back-over-empty-lines)
6149 (setq org-subtree-clip-folded folded)
6150 (when (or cut force-store-markers)
6151 (org-save-markers-in-region beg end))
6152 (if cut (kill-region beg end) (copy-region-as-kill beg end))
6153 (setq org-subtree-clip (current-kill 0))
6154 (message "%s: Subtree(s) with %d characters"
6155 (if cut "Cut" "Copied")
6156 (length org-subtree-clip)))))
6158 (defun org-paste-subtree (&optional level tree for-yank)
6159 "Paste the clipboard as a subtree, with modification of headline level.
6160 The entire subtree is promoted or demoted in order to match a new headline
6163 If the cursor is at the beginning of a headline, the same level as
6164 that headline is used to paste the tree
6166 If not, the new level is derived from the *visible* headings
6167 before and after the insertion point, and taken to be the inferior headline
6168 level of the two. So if the previous visible heading is level 3 and the
6169 next is level 4 (or vice versa), level 4 will be used for insertion.
6170 This makes sure that the subtree remains an independent subtree and does
6171 not swallow low level entries.
6173 You can also force a different level, either by using a numeric prefix
6174 argument, or by inserting the heading marker by hand. For example, if the
6175 cursor is after \"*****\", then the tree will be shifted to level 5.
6177 If optional TREE is given, use this text instead of the kill ring.
6179 When FOR-YANK is set, this is called by `org-yank'. In this case, do not
6180 move back over whitespace before inserting, and move point to the end of
6181 the inserted text when done."
6183 (unless (org-kill-is-subtree-p tree)
6185 (substitute-command-keys
6186 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
6187 (let* ((visp (not (org-invisible-p)))
6188 (txt (or tree (and kill-ring (current-kill 0))))
6189 (^re (concat "^\\(" outline-regexp "\\)"))
6190 (re (concat "\\(" outline-regexp "\\)"))
6191 (^re_ (concat "\\(\\*+\\)[ \t]*"))
6193 (old-level (if (string-match ^re txt)
6194 (- (match-end 0) (match-beginning 0) 1)
6196 (force-level (cond (level (prefix-numeric-value level))
6197 ((and (looking-at "[ \t]*$")
6199 ^re_ (buffer-substring
6200 (point-at-bol) (point))))
6201 (- (match-end 1) (match-beginning 1)))
6203 (looking-at org-outline-regexp))
6204 (- (match-end 0) (point) 1))
6206 (previous-level (save-excursion
6209 (outline-previous-visible-heading 1)
6211 (- (match-end 0) (match-beginning 0) 1)
6214 (next-level (save-excursion
6217 (or (looking-at outline-regexp)
6218 (outline-next-visible-heading 1))
6220 (- (match-end 0) (match-beginning 0) 1)
6223 (new-level (or force-level (max previous-level next-level)))
6224 (shift (if (or (= old-level -1)
6226 (= old-level new-level))
6228 (- new-level old-level)))
6229 (delta (if (> shift 0) -1 1))
6230 (func (if (> shift 0) 'org-demote 'org-promote))
6231 (org-odd-levels-only nil)
6233 ;; Remove the forced level indicator
6235 (delete-region (point-at-bol) (point)))
6237 (beginning-of-line 1)
6238 (unless for-yank (org-back-over-empty-lines))
6240 (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
6241 (insert-before-markers txt)
6242 (unless (string-match "\n\\'" txt) (insert "\n"))
6243 (setq newend (point))
6244 (org-reinstall-markers-in-region beg)
6247 (skip-chars-forward " \t\n\r")
6249 (if (and (org-invisible-p) visp)
6250 (save-excursion (outline-show-heading)))
6251 ;; Shift if necessary
6254 (narrow-to-region beg end)
6255 (while (not (= shift 0))
6256 (org-map-region func (point-min) (point-max))
6257 (setq shift (+ delta shift)))
6258 (goto-char (point-min))
6259 (setq newend (point-max))))
6260 (when (or (interactive-p) for-yank)
6261 (message "Clipboard pasted as level %d subtree" new-level))
6262 (if (and (not for-yank) ; in this case, org-yank will decide about folding
6264 (eq org-subtree-clip (current-kill 0))
6265 org-subtree-clip-folded)
6266 ;; The tree was folded before it was killed/copied
6268 (and for-yank (goto-char newend))))
6270 (defun org-kill-is-subtree-p (&optional txt)
6271 "Check if the current kill is an outline subtree, or a set of trees.
6272 Returns nil if kill does not start with a headline, or if the first
6273 headline level is not the largest headline level in the tree.
6274 So this will actually accept several entries of equal levels as well,
6275 which is OK for `org-paste-subtree'.
6276 If optional TXT is given, check this string instead of the current kill."
6277 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
6278 (start-level (and kill
6279 (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
6280 org-outline-regexp "\\)")
6282 (- (match-end 2) (match-beginning 2) 1)))
6283 (re (concat "^" org-outline-regexp))
6284 (start (1+ (or (match-beginning 2) -1))))
6285 (if (not start-level)
6287 nil) ;; does not even start with a heading
6289 (while (setq start (string-match re kill (1+ start)))
6290 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
6294 (defvar org-markers-to-move nil
6295 "Markers that should be moved with a cut-and-paste operation.
6296 Those markers are stored together with their positions relative to
6297 the start of the region.")
6299 (defun org-save-markers-in-region (beg end)
6300 "Check markers in region.
6301 If these markers are between BEG and END, record their position relative
6302 to BEG, so that after moving the block of text, we can put the markers back
6304 This function gets called just before an entry or tree gets cut from the
6305 buffer. After re-insertion, `org-reinstall-markers-in-region' must be
6306 called immediately, to move the markers with the entries."
6307 (setq org-markers-to-move nil)
6308 (when (featurep 'org-clock)
6309 (org-clock-save-markers-for-cut-and-paste beg end))
6310 (when (featurep 'org-agenda)
6311 (org-agenda-save-markers-for-cut-and-paste beg end)))
6313 (defun org-check-and-save-marker (marker beg end)
6314 "Check if MARKER is between BEG and END.
6315 If yes, remember the marker and the distance to BEG."
6316 (when (and (marker-buffer marker)
6317 (equal (marker-buffer marker) (current-buffer)))
6318 (if (and (>= marker beg) (< marker end))
6319 (push (cons marker (- marker beg)) org-markers-to-move))))
6321 (defun org-reinstall-markers-in-region (beg)
6322 "Move all remembered markers to their position relative to BEG."
6324 (move-marker (car x) (+ beg (cdr x))))
6325 org-markers-to-move)
6326 (setq org-markers-to-move nil))
6328 (defun org-narrow-to-subtree ()
6329 "Narrow buffer to the current subtree."
6334 (progn (org-back-to-heading t) (point))
6335 (progn (org-end-of-subtree t) (point))))))
6337 (defun org-clone-subtree-with-time-shift (n &optional shift)
6338 "Clone the task (subtree) at point N times.
6339 The clones will be inserted as siblings.
6341 In interactive use, the user will be prompted for the number of clones
6342 to be produced, and for a time SHIFT, which may be a repeater as used
6343 in time stamps, for example `+3d'.
6345 When a valid repeater is given and the entry contains any time stamps,
6346 the clones will become a sequence in time, with time stamps in the
6347 subtree shifted for each clone produced. If SHIFT is nil or the
6348 empty string, time stamps will be left alone.
6350 If the original subtree did contain time stamps with a repeater,
6351 the following will happen:
6352 - the repeater will be removed in each clone
6353 - an additional clone will be produced, with the current, unshifted
6354 date(s) in the entry.
6355 - the original entry will be placed *after* all the clones, with
6357 - the start days in the repeater in the original entry will be shifted
6358 to past the last clone.
6359 I this way you can spell out a number of instances of a repeating task,
6360 and still retain the repeater to cover future instances of the task."
6361 (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
6362 (let (beg end template task
6363 shift-n shift-what doshift nmin nmax (n-no-remove -1))
6364 (if (not (and (integerp n) (> n 0)))
6365 (error "Invalid number of replications %s" n))
6366 (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
6367 (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
6369 (error "Invalid shift specification %s" shift))
6371 (setq shift-n (string-to-number (match-string 1 shift))
6372 shift-what (cdr (assoc (match-string 2 shift)
6373 '(("d" . day) ("w" . week)
6374 ("m" . month) ("y" . year))))))
6375 (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
6376 (setq nmin 1 nmax n)
6377 (org-back-to-heading t)
6379 (org-end-of-subtree t t)
6381 (setq template (buffer-substring beg end))
6383 (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
6384 (delete-region beg end)
6386 (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
6388 (loop for n from nmin to nmax do
6390 (setq task template)
6394 (goto-char (point-min))
6395 (while (re-search-forward org-ts-regexp-both nil t)
6396 (org-timestamp-change (* n shift-n) shift-what))
6397 (unless (= n n-no-remove)
6398 (goto-char (point-min))
6399 (while (re-search-forward org-ts-regexp nil t)
6401 (goto-char (match-beginning 0))
6402 (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
6403 (delete-region (match-beginning 1) (match-end 1))))))
6404 (setq task (buffer-string))))
6410 (defun org-sort (with-case)
6411 "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
6412 Optional argument WITH-CASE means sort case-sensitively.
6413 With a double prefix argument, also remove duplicate entries."
6415 (if (org-at-table-p)
6416 (org-call-with-arg 'org-table-sort-lines with-case)
6417 (org-call-with-arg 'org-sort-entries-or-items with-case)))
6419 (defun org-sort-remove-invisible (s)
6420 (remove-text-properties 0 (length s) org-rm-props s)
6421 (while (string-match org-bracket-link-regexp s)
6422 (setq s (replace-match (if (match-end 2)
6424 (match-string 1 s)) t t s)))
6427 (defvar org-priority-regexp) ; defined later in the file
6429 (defvar org-after-sorting-entries-or-items-hook nil
6430 "Hook that is run after a bunch of entries or items have been sorted.
6431 When children are sorted, the cursor is in the parent line when this
6432 hook gets called. When a region or a plain list is sorted, the cursor
6433 will be in the first entry of the sorted region/list.")
6435 (defun org-sort-entries-or-items
6436 (&optional with-case sorting-type getkey-func compare-func property)
6437 "Sort entries on a certain level of an outline tree, or plain list items.
6438 If there is an active region, the entries in the region are sorted.
6439 Else, if the cursor is before the first entry, sort the top-level items.
6440 Else, the children of the entry at point are sorted.
6441 If the cursor is at the first item in a plain list, the list items will be
6444 Sorting can be alphabetically, numerically, by date/time as given by
6445 a time stamp, by a property or by priority.
6447 The command prompts for the sorting type unless it has been given to the
6448 function through the SORTING-TYPE argument, which needs to a character,
6449 \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
6450 precise meaning of each character:
6452 n Numerically, by converting the beginning of the entry/item to a number.
6453 a Alphabetically, ignoring the TODO keyword and the priority, if any.
6454 t By date/time, either the first active time stamp in the entry, or, if
6455 none exist, by the first inactive one.
6456 In items, only the first line will be chekced.
6457 s By the scheduled date/time.
6458 d By deadline date/time.
6459 c By creation time, which is assumed to be the first inactive time stamp
6460 at the beginning of a line.
6461 p By priority according to the cookie.
6462 r By the value of a property.
6464 Capital letters will reverse the sort order.
6466 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
6467 called with point at the beginning of the record. It must return either
6468 a string or a number that should serve as the sorting key for that record.
6470 Comparing entries ignores case by default. However, with an optional argument
6471 WITH-CASE, the sorting considers case as well."
6473 (let ((case-func (if with-case 'identity 'downcase))
6474 start beg end stars re re2
6475 txt what tmp plain-list-p)
6476 ;; Find beginning and end of region to sort
6478 ((org-region-active-p)
6479 ;; we will sort the region
6480 (setq end (region-end)
6482 (goto-char (region-beginning))
6483 (if (not (org-on-heading-p)) (outline-next-heading))
6484 (setq start (point)))
6486 ;; we will sort this plain list
6487 (org-beginning-of-item-list) (setq start (point))
6488 (org-end-of-item-list) (setq end (point))
6490 (setq plain-list-p t
6492 ((or (org-on-heading-p)
6493 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
6494 ;; we will sort the children of the current headline
6495 (org-back-to-heading)
6497 end (progn (org-end-of-subtree t t)
6498 (org-back-over-empty-lines)
6503 (outline-next-heading))
6505 ;; we will sort the top-level entries in this file
6506 (goto-char (point-min))
6507 (or (org-on-heading-p) (outline-next-heading))
6508 (setq start (point) end (point-max) what "top-level")
6513 (if (>= beg end) (error "Nothing to sort"))
6515 (unless plain-list-p
6516 (looking-at "\\(\\*+\\)")
6517 (setq stars (match-string 1)
6518 re (concat "^" (regexp-quote stars) " +")
6519 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
6520 txt (buffer-substring beg end))
6521 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
6522 (if (and (not (equal stars "*")) (string-match re2 txt))
6523 (error "Region to sort contains a level above the first entry")))
6525 (unless sorting-type
6528 "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
6529 "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
6530 [t]ime [s]cheduled [d]eadline [c]reated
6531 A/N/T/S/D/C/P/O/F means reversed:")
6533 (setq sorting-type (read-char-exclusive))
6535 (and (= (downcase sorting-type) ?f)
6537 (org-ido-completing-read "Sort using function: "
6538 obarray 'fboundp t nil nil))
6539 (setq getkey-func (intern getkey-func)))
6541 (and (= (downcase sorting-type) ?r)
6543 (org-ido-completing-read "Property: "
6544 (mapcar 'list (org-buffer-property-keys t))
6547 (message "Sorting entries...")
6550 (narrow-to-region start end)
6552 (let ((dcst (downcase sorting-type))
6553 (case-fold-search nil)
6554 (now (current-time)))
6556 (/= dcst sorting-type)
6557 ;; This function moves to the beginning character of the "record" to
6561 (if (org-at-item-p) t (goto-char (point-max))))
6563 (if (re-search-forward re nil t)
6564 (goto-char (match-beginning 0))
6565 (goto-char (point-max)))))
6566 ;; This function moves to the last character of the "record" being
6573 (outline-forward-same-level 1)
6575 (goto-char (point-max)))))))
6577 ;; This function returns the value that gets sorted against.
6580 (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
6583 (string-to-number (buffer-substring (match-end 0)
6586 (buffer-substring (match-end 0) (point-at-eol)))
6588 (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
6589 (re-search-forward org-ts-regexp-both
6591 (org-time-string-to-seconds (match-string 0))
6592 (time-to-seconds now)))
6596 (setq tmp (funcall getkey-func))
6597 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
6599 (error "Invalid key function `%s'" getkey-func)))
6600 (t (error "Invalid sorting type `%c'" sorting-type)))))
6604 (if (looking-at org-complex-heading-regexp)
6605 (string-to-number (match-string 4))
6608 (if (looking-at org-complex-heading-regexp)
6609 (funcall case-func (match-string 4))
6612 (let ((end (save-excursion (outline-next-heading) (point))))
6613 (if (or (re-search-forward org-ts-regexp end t)
6614 (re-search-forward org-ts-regexp-both end t))
6615 (org-time-string-to-seconds (match-string 0))
6616 (time-to-seconds now))))
6618 (let ((end (save-excursion (outline-next-heading) (point))))
6619 (if (re-search-forward
6620 (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
6622 (org-time-string-to-seconds (match-string 0))
6623 (time-to-seconds now))))
6625 (let ((end (save-excursion (outline-next-heading) (point))))
6626 (if (re-search-forward org-scheduled-time-regexp end t)
6627 (org-time-string-to-seconds (match-string 1))
6628 (time-to-seconds now))))
6630 (let ((end (save-excursion (outline-next-heading) (point))))
6631 (if (re-search-forward org-deadline-time-regexp end t)
6632 (org-time-string-to-seconds (match-string 1))
6633 (time-to-seconds now))))
6635 (if (re-search-forward org-priority-regexp (point-at-eol) t)
6636 (string-to-char (match-string 2))
6637 org-default-priority))
6639 (or (org-entry-get nil property) ""))
6641 (if (looking-at org-complex-heading-regexp)
6642 (- 9999 (length (member (match-string 2)
6643 org-todo-keywords-1)))))
6647 (setq tmp (funcall getkey-func))
6648 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
6650 (error "Invalid key function `%s'" getkey-func)))
6651 (t (error "Invalid sorting type `%c'" sorting-type)))))
6654 ((= dcst ?a) 'string<)
6655 ((= dcst ?f) compare-func)
6656 ((member dcst '(?p ?t ?s ?d ?c)) '<)
6658 (run-hooks 'org-after-sorting-entries-or-items-hook)
6659 (message "Sorting entries...done")))
6661 (defun org-do-sort (table what &optional with-case sorting-type)
6662 "Sort TABLE of WHAT according to SORTING-TYPE.
6663 The user will be prompted for the SORTING-TYPE if the call to this
6664 function does not specify it. WHAT is only for the prompt, to indicate
6665 what is being sorted. The sorting key will be extracted from
6666 the car of the elements of the table.
6667 If WITH-CASE is non-nil, the sorting will be case-sensitive."
6668 (unless sorting-type
6670 "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
6672 (setq sorting-type (read-char-exclusive)))
6673 (let ((dcst (downcase sorting-type))
6674 extractfun comparefun)
6675 ;; Define the appropriate functions
6678 (setq extractfun 'string-to-number
6679 comparefun (if (= dcst sorting-type) '< '>)))
6681 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
6682 (lambda(x) (downcase (org-sort-remove-invisible x))))
6683 comparefun (if (= dcst sorting-type)
6685 (lambda (a b) (and (not (string< a b))
6686 (not (string= a b)))))))
6690 (if (or (string-match org-ts-regexp x)
6691 (string-match org-ts-regexp-both x))
6693 (org-time-string-to-time (match-string 0 x)))
6695 comparefun (if (= dcst sorting-type) '< '>)))
6696 (t (error "Invalid sorting type `%c'" sorting-type)))
6698 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
6700 (lambda (a b) (funcall comparefun (car a) (car b))))))
6703 ;;; The orgstruct minor mode
6705 ;; Define a minor mode which can be used in other modes in order to
6706 ;; integrate the org-mode structure editing commands.
6708 ;; This is really a hack, because the org-mode structure commands use
6709 ;; keys which normally belong to the major mode. Here is how it
6710 ;; works: The minor mode defines all the keys necessary to operate the
6711 ;; structure commands, but wraps the commands into a function which
6712 ;; tests if the cursor is currently at a headline or a plain list
6713 ;; item. If that is the case, the structure command is used,
6714 ;; temporarily setting many Org-mode variables like regular
6715 ;; expressions for filling etc. However, when any of those keys is
6716 ;; used at a different location, function uses `key-binding' to look
6717 ;; up if the key has an associated command in another currently active
6718 ;; keymap (minor modes, major mode, global), and executes that
6719 ;; command. There might be problems if any of the keys is otherwise
6720 ;; used as a prefix key.
6722 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
6723 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
6724 ;; addresses this by checking explicitly for both bindings.
6726 (defvar orgstruct-mode-map (make-sparse-keymap)
6727 "Keymap for the minor `orgstruct-mode'.")
6729 (defvar org-local-vars nil
6730 "List of local variables, for use by `orgstruct-mode'")
6733 (define-minor-mode orgstruct-mode
6734 "Toggle the minor more `orgstruct-mode'.
6735 This mode is for using Org-mode structure commands in other modes.
6736 The following key behave as if Org-mode was active, if the cursor
6737 is on a headline, or on a plain list item (both in the definition
6740 M-up Move entry/item up
6741 M-down Move entry/item down
6744 M-S-up Move entry/item up
6745 M-S-down Move entry/item down
6746 M-S-left Promote subtree
6747 M-S-right Demote subtree
6748 M-q Fill paragraph and items like in Org-mode
6750 C-c - Cycle list bullet
6751 TAB Cycle item visibility
6752 M-RET Insert new heading/item
6753 S-M-RET Insert new TODO heading / Checkbox item
6754 C-c C-c Set tags / toggle checkbox"
6755 nil " OrgStruct" nil
6756 (org-load-modules-maybe)
6757 (and (orgstruct-setup) (defun orgstruct-setup () nil)))
6760 (defun turn-on-orgstruct ()
6761 "Unconditionally turn on `orgstruct-mode'."
6764 (defun orgstruct++-mode (&optional arg)
6765 "Toggle `orgstruct-mode', the enhanced version of it.
6766 In addition to setting orgstruct-mode, this also exports all indentation
6767 and autofilling variables from org-mode into the buffer. It will also
6768 recognize item context in multiline items.
6769 Note that turning off orgstruct-mode will *not* remove the
6770 indentation/paragraph settings. This can only be done by refreshing the
6771 major mode, for example with \\[normal-mode]."
6773 (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
6781 "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
6782 (symbol-name (car x)))
6783 (setq var (car x) val (nth 1 x))
6784 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
6786 (org-set-local 'orgstruct-is-++ t))))
6788 (defvar orgstruct-is-++ nil
6789 "Is orgstruct-mode in ++ version in the current-buffer?")
6790 (make-variable-buffer-local 'orgstruct-is-++)
6793 (defun turn-on-orgstruct++ ()
6794 "Unconditionally turn on `orgstruct++-mode'."
6795 (orgstruct++-mode 1))
6797 (defun orgstruct-error ()
6798 "Error when there is no default binding for a structure key."
6800 (error "This key has no function outside structure elements"))
6802 (defun orgstruct-setup ()
6803 "Setup orgstruct keymaps."
6807 '([(meta up)] org-metaup)
6808 '([(meta down)] org-metadown)
6809 '([(meta left)] org-metaleft)
6810 '([(meta right)] org-metaright)
6811 '([(meta shift up)] org-shiftmetaup)
6812 '([(meta shift down)] org-shiftmetadown)
6813 '([(meta shift left)] org-shiftmetaleft)
6814 '([(meta shift right)] org-shiftmetaright)
6815 '([?\e (up)] org-metaup)
6816 '([?\e (down)] org-metadown)
6817 '([?\e (left)] org-metaleft)
6818 '([?\e (right)] org-metaright)
6819 '([?\e (shift up)] org-shiftmetaup)
6820 '([?\e (shift down)] org-shiftmetadown)
6821 '([?\e (shift left)] org-shiftmetaleft)
6822 '([?\e (shift right)] org-shiftmetaright)
6823 '([(shift up)] org-shiftup)
6824 '([(shift down)] org-shiftdown)
6825 '([(shift left)] org-shiftleft)
6826 '([(shift right)] org-shiftright)
6827 '("\C-c\C-c" org-ctrl-c-ctrl-c)
6828 '("\M-q" fill-paragraph)
6830 '("\C-c-" org-cycle-list-bullet)))
6832 (while (setq elt (pop bindings))
6833 (setq nfunc (1+ nfunc))
6834 (setq key (org-key (car elt))
6836 cmd (orgstruct-make-binding fun nfunc key))
6837 (org-defkey orgstruct-mode-map key cmd))
6839 ;; Special treatment needed for TAB and RET
6840 (org-defkey orgstruct-mode-map [(tab)]
6841 (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
6842 (org-defkey orgstruct-mode-map "\C-i"
6843 (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
6845 (org-defkey orgstruct-mode-map "\M-\C-m"
6846 (orgstruct-make-binding 'org-insert-heading 105
6847 "\M-\C-m" [(meta return)]))
6848 (org-defkey orgstruct-mode-map [(meta return)]
6849 (orgstruct-make-binding 'org-insert-heading 106
6850 [(meta return)] "\M-\C-m"))
6852 (org-defkey orgstruct-mode-map [(shift meta return)]
6853 (orgstruct-make-binding 'org-insert-todo-heading 107
6854 [(meta return)] "\M-\C-m"))
6856 (org-defkey orgstruct-mode-map "\e\C-m"
6857 (orgstruct-make-binding 'org-insert-heading 108
6858 "\e\C-m" [?\e (return)]))
6859 (org-defkey orgstruct-mode-map [?\e (return)]
6860 (orgstruct-make-binding 'org-insert-heading 109
6861 [?\e (return)] "\e\C-m"))
6862 (org-defkey orgstruct-mode-map [?\e (shift return)]
6863 (orgstruct-make-binding 'org-insert-todo-heading 110
6864 [?\e (return)] "\e\C-m"))
6866 (unless org-local-vars
6867 (setq org-local-vars (org-get-local-variables)))
6871 (defun orgstruct-make-binding (fun n &rest keys)
6872 "Create a function for binding in the structure minor mode.
6873 FUN is the command to call inside a table. N is used to create a unique
6874 command name. KEYS are keys that should be checked in for a command
6875 to execute outside of tables."
6878 (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
6880 (concat "In Structure, run `" (symbol-name fun) "'.\n"
6881 "Outside of structure, run the binding of `"
6882 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
6886 `(org-context-p 'headline 'item
6887 (and orgstruct-is-++
6888 ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
6890 (list 'org-run-like-in-org-mode (list 'quote fun))
6891 (list 'let '(orgstruct-mode)
6892 (list 'call-interactively
6895 (list 'key-binding k))
6897 '('orgstruct-error))))))))
6899 (defun org-context-p (&rest contexts)
6900 "Check if local context is any of CONTEXTS.
6901 Possible values in the list of contexts are `table', `headline', and `item'."
6902 (let ((pos (point)))
6903 (goto-char (point-at-bol))
6904 (prog1 (or (and (memq 'table contexts)
6905 (looking-at "[ \t]*|"))
6906 (and (memq 'headline contexts)
6907 ;;????????? (looking-at "\\*+"))
6908 (looking-at outline-regexp))
6909 (and (memq 'item contexts)
6910 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
6911 (and (memq 'item-body contexts)
6915 (defun org-get-local-variables ()
6916 "Return a list of all local variables in an org-mode buffer."
6918 (with-current-buffer (get-buffer-create "*Org tmp*")
6921 (setq varlist (buffer-local-variables)))
6922 (kill-buffer "*Org tmp*")
6929 (list (car x) (list 'quote (cdr x)))))
6931 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
6932 (symbol-name (car x)))
6937 (defun org-run-like-in-org-mode (cmd)
6938 "Run a command, pretending that the current buffer is in Org-mode.
6939 This will temporarily bind local variables that are typically bound in
6940 Org-mode to the values they have in Org-mode, and then interactively
6942 (org-load-modules-maybe)
6943 (unless org-local-vars
6944 (setq org-local-vars (org-get-local-variables)))
6945 (eval (list 'let org-local-vars
6946 (list 'call-interactively (list 'quote cmd)))))
6950 (defun org-get-category (&optional pos)
6951 "Get the category applying to position POS."
6952 (get-text-property (or pos (point)) 'org-category))
6954 (defun org-refresh-category-properties ()
6955 "Refresh category text properties in the buffer."
6956 (let ((def-cat (cond
6957 ((null org-category)
6958 (if buffer-file-name
6959 (file-name-sans-extension
6960 (file-name-nondirectory buffer-file-name))
6962 ((symbolp org-category) (symbol-name org-category))
6964 beg end cat pos optionp)
6969 (goto-char (point-min))
6970 (put-text-property (point) (point-max) 'org-category def-cat)
6971 (while (re-search-forward
6972 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
6973 (setq pos (match-end 0)
6974 optionp (equal (char-after (match-beginning 0)) ?#)
6975 cat (org-trim (match-string 2)))
6977 (setq beg (point-at-bol) end (point-max))
6978 (org-back-to-heading t)
6979 (setq beg (point) end (org-end-of-subtree t t)))
6980 (put-text-property beg end 'org-category cat)
6981 (goto-char pos)))))))
6986 ;;; Link abbreviations
6988 (defun org-link-expand-abbrev (link)
6989 "Apply replacements as defined in `org-link-abbrev-alist."
6990 (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
6991 (let* ((key (match-string 1 link))
6992 (as (or (assoc key org-link-abbrev-alist-local)
6993 (assoc key org-link-abbrev-alist)))
6994 (tag (and (match-end 2) (match-string 3 link)))
7000 ((symbolp rpl) (funcall rpl tag))
7001 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
7002 ((string-match "%h" rpl)
7003 (replace-match (url-hexify-string (or tag "")) t t rpl))
7004 (t (concat rpl tag)))))
7007 ;;; Storing and inserting links
7009 (defvar org-insert-link-history nil
7010 "Minibuffer history for links inserted with `org-insert-link'.")
7012 (defvar org-stored-links nil
7013 "Contains the links stored with `org-store-link'.")
7015 (defvar org-store-link-plist nil
7016 "Plist with info about the most recently link created with `org-store-link'.")
7018 (defvar org-link-protocols nil
7019 "Link protocols added to Org-mode using `org-add-link-type'.")
7021 (defvar org-store-link-functions nil
7022 "List of functions that are called to create and store a link.
7023 Each function will be called in turn until one returns a non-nil
7024 value. Each function should check if it is responsible for creating
7025 this link (for example by looking at the major mode).
7026 If not, it must exit and return nil.
7027 If yes, it should return a non-nil value after a calling
7028 `org-store-link-props' with a list of properties and values.
7029 Special properties are:
7031 :type The link prefix. like \"http\". This must be given.
7032 :link The link, like \"http://www.astro.uva.nl/~dominik\".
7033 This is obligatory as well.
7034 :description Optional default description for the second pair
7035 of brackets in an Org-mode link. The user can still change
7036 this when inserting this link into an Org-mode buffer.
7038 In addition to these, any additional properties can be specified
7039 and then used in remember templates.")
7041 (defun org-add-link-type (type &optional follow export)
7042 "Add TYPE to the list of `org-link-types'.
7043 Re-compute all regular expressions depending on `org-link-types'
7045 FOLLOW and EXPORT are two functions.
7047 FOLLOW should take the link path as the single argument and do whatever
7048 is necessary to follow the link, for example find a file or display
7051 EXPORT should format the link path for export to one of the export formats.
7052 It should be a function accepting three arguments:
7054 path the path of the link, the text after the prefix (like \"http:\")
7055 desc the description of the link, if any, nil if there was no description
7056 format the export format, a symbol like `html' or `latex'.
7058 The function may use the FORMAT information to return different values
7059 depending on the format. The return value will be put literally into
7061 Org-mode has a built-in default for exporting links. If you are happy with
7062 this default, there is no need to define an export function for the link
7063 type. For a simple example of an export function, see `org-bbdb.el'."
7064 (add-to-list 'org-link-types type t)
7065 (org-make-link-regexps)
7066 (if (assoc type org-link-protocols)
7067 (setcdr (assoc type org-link-protocols) (list follow export))
7068 (push (list type follow export) org-link-protocols)))
7071 (defun org-store-link (arg)
7072 "\\<org-mode-map>Store an org-link to the current location.
7073 This link is added to `org-stored-links' and can later be inserted
7074 into an org-buffer with \\[org-insert-link].
7076 For some link types, a prefix arg is interpreted:
7077 For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
7078 For file links, arg negates `org-context-in-file-links'."
7080 (org-load-modules-maybe)
7081 (setq org-store-link-plist nil) ; reset
7082 (let ((outline-regexp (org-get-limited-outline-regexp))
7083 link cpltxt desc description search txt custom-id)
7086 ((run-hook-with-args-until-success 'org-store-link-functions)
7087 (setq link (plist-get org-store-link-plist :link)
7088 desc (or (plist-get org-store-link-plist :description) link)))
7090 ((equal (buffer-name) "*Org Edit Src Example*")
7092 (while (or (not label)
7096 (goto-char (point-min))
7098 (regexp-quote (format org-coderef-label-format label))
7100 (when label (message "Label exists already") (sit-for 2))
7101 (setq label (read-string "Code line label: " label)))
7103 (setq link (format org-coderef-label-format label))
7104 (setq gc (- 79 (length link)))
7105 (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
7107 (setq link (concat "(" label ")") desc nil)))
7109 ((eq major-mode 'calendar-mode)
7110 (let ((cd (calendar-cursor-to-date)))
7113 (car org-time-stamp-formats)
7115 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
7117 (org-store-link-props :type "calendar" :date cd)))
7119 ((eq major-mode 'w3-mode)
7120 (setq cpltxt (if (and (buffer-name)
7121 (not (string-match "Untitled" (buffer-name))))
7124 link (org-make-link (url-view-url t)))
7125 (org-store-link-props :type "w3" :url (url-view-url t)))
7127 ((eq major-mode 'w3m-mode)
7128 (setq cpltxt (or w3m-current-title w3m-current-url)
7129 link (org-make-link w3m-current-url))
7130 (org-store-link-props :type "w3m" :url (url-view-url t)))
7132 ((setq search (run-hook-with-args-until-success
7133 'org-create-file-search-functions))
7134 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
7136 (setq cpltxt (or description link)))
7138 ((eq major-mode 'image-mode)
7139 (setq cpltxt (concat "file:"
7140 (abbreviate-file-name buffer-file-name))
7141 link (org-make-link cpltxt))
7142 (org-store-link-props :type "image" :file buffer-file-name))
7144 ((eq major-mode 'dired-mode)
7145 ;; link to the file in the current line
7146 (setq cpltxt (concat "file:"
7147 (abbreviate-file-name
7149 (dired-get-filename nil t))))
7150 link (org-make-link cpltxt)))
7152 ((and buffer-file-name (org-mode-p))
7153 (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
7155 ((org-in-regexp "<<\\(.*?\\)>>")
7158 (abbreviate-file-name buffer-file-name)
7159 "::" (match-string 1))
7160 link (org-make-link cpltxt)))
7161 ((and (featurep 'org-id)
7162 (or (eq org-link-to-org-use-id t)
7163 (and (eq org-link-to-org-use-id 'create-if-interactive)
7165 (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
7168 (and org-link-to-org-use-id
7170 (org-entry-get nil "ID")
7172 ;; We can make a link using the ID.
7173 (setq link (condition-case nil
7174 (prog1 (org-id-store-link)
7175 (setq desc (plist-get org-store-link-plist
7178 ;; probably before first headline, link to file only
7180 (abbreviate-file-name buffer-file-name))))))
7182 ;; Just link to current headline
7183 (setq cpltxt (concat "file:"
7184 (abbreviate-file-name buffer-file-name)))
7185 ;; Add a context search string
7186 (when (org-xor org-context-in-file-links arg)
7188 ((org-on-heading-p) nil)
7189 ((org-region-active-p)
7190 (buffer-substring (region-beginning) (region-end)))
7192 (when (or (null txt) (string-match "\\S-" txt))
7196 (org-make-org-heading-search-string txt)
7198 desc (or (nth 4 (org-heading-components)) "NONE"))))
7199 (if (string-match "::\\'" cpltxt)
7200 (setq cpltxt (substring cpltxt 0 -2)))
7201 (setq link (org-make-link cpltxt)))))
7203 ((buffer-file-name (buffer-base-buffer))
7204 ;; Just link to this file here.
7205 (setq cpltxt (concat "file:"
7206 (abbreviate-file-name
7207 (buffer-file-name (buffer-base-buffer)))))
7208 ;; Add a context string
7209 (when (org-xor org-context-in-file-links arg)
7210 (setq txt (if (org-region-active-p)
7211 (buffer-substring (region-beginning) (region-end))
7212 (buffer-substring (point-at-bol) (point-at-eol))))
7213 ;; Only use search option if there is some text.
7214 (when (string-match "\\S-" txt)
7216 (concat cpltxt "::" (org-make-org-heading-search-string txt))
7218 (setq link (org-make-link cpltxt)))
7221 (error "Cannot link to a buffer which is not visiting a file"))
7223 (t (setq link nil)))
7225 (if (consp link) (setq cpltxt (car link) link (cdr link)))
7226 (setq link (or link cpltxt)
7227 desc (or desc cpltxt))
7228 (if (equal desc "NONE") (setq desc nil))
7230 (if (and (or (interactive-p) executing-kbd-macro) link)
7232 (setq org-stored-links
7233 (cons (list link desc) org-stored-links))
7234 (message "Stored: %s" (or desc link))
7236 (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
7238 (setq org-stored-links
7239 (cons (list link desc) org-stored-links))))
7240 (and link (org-make-link-string link desc)))))
7242 (defun org-store-link-props (&rest plist)
7243 "Store link properties, extract names and addresses."
7245 (when (setq x (plist-get plist :from))
7246 (setq adr (mail-extract-address-components x))
7247 (setq plist (plist-put plist :fromname (car adr)))
7248 (setq plist (plist-put plist :fromaddress (nth 1 adr))))
7249 (when (setq x (plist-get plist :to))
7250 (setq adr (mail-extract-address-components x))
7251 (setq plist (plist-put plist :toname (car adr)))
7252 (setq plist (plist-put plist :toaddress (nth 1 adr)))))
7253 (let ((from (plist-get plist :from))
7254 (to (plist-get plist :to)))
7255 (when (and from to org-from-is-user-regexp)
7257 (plist-put plist :fromto
7258 (if (string-match org-from-is-user-regexp from)
7260 (concat "from %f"))))))
7261 (setq org-store-link-plist plist))
7263 (defun org-add-link-props (&rest plist)
7264 "Add these properties to the link property list."
7267 (setq key (pop plist) value (pop plist))
7268 (setq org-store-link-plist
7269 (plist-put org-store-link-plist key value)))))
7271 (defun org-email-link-description (&optional fmt)
7272 "Return the description part of an email link.
7273 This takes information from `org-store-link-plist' and formats it
7274 according to FMT (default from `org-email-link-description-format')."
7275 (setq fmt (or fmt org-email-link-description-format))
7276 (let* ((p org-store-link-plist)
7277 (to (plist-get p :toaddress))
7278 (from (plist-get p :fromaddress))
7281 (cons "%c" (plist-get p :fromto))
7282 (cons "%F" (plist-get p :from))
7283 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
7284 (cons "%T" (plist-get p :to))
7285 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
7286 (cons "%s" (plist-get p :subject))
7287 (cons "%m" (plist-get p :message-id)))))
7288 (when (string-match "%c" fmt)
7289 ;; Check if the user wrote this message
7290 (if (and org-from-is-user-regexp from to
7291 (save-match-data (string-match org-from-is-user-regexp from)))
7292 (setq fmt (replace-match "to %t" t t fmt))
7293 (setq fmt (replace-match "from %f" t t fmt))))
7294 (org-replace-escapes fmt table)))
7296 (defun org-make-org-heading-search-string (&optional string heading)
7297 "Make search string for STRING or current headline."
7299 (let ((s (or string (org-get-heading))))
7300 (unless (and string (not heading))
7301 ;; We are using a headline, clean up garbage in there.
7302 (if (string-match org-todo-regexp s)
7303 (setq s (replace-match "" t t s)))
7304 (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
7305 (setq s (replace-match "" t t s)))
7306 (setq s (org-trim s))
7307 (if (string-match (concat "^\\(" org-quote-string "\\|"
7308 org-comment-string "\\)") s)
7309 (setq s (replace-match "" t t s)))
7310 (while (string-match org-ts-regexp s)
7311 (setq s (replace-match "" t t s))))
7312 (while (string-match "[^a-zA-Z_0-9 \t]+" s)
7313 (setq s (replace-match " " t t s)))
7314 (or string (setq s (concat "*" s))) ; Add * for headlines
7315 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
7317 (defun org-make-link (&rest strings)
7318 "Concatenate STRINGS."
7319 (apply 'concat strings))
7321 (defun org-make-link-string (link &optional description)
7322 "Make a link with brackets, consisting of LINK and DESCRIPTION."
7323 (unless (string-match "\\S-" link)
7324 (error "Empty link"))
7325 (when (stringp description)
7326 ;; Remove brackets from the description, they are fatal.
7327 (while (string-match "\\[" description)
7328 (setq description (replace-match "{" t t description)))
7329 (while (string-match "\\]" description)
7330 (setq description (replace-match "}" t t description))))
7331 (when (equal (org-link-escape link) description)
7332 ;; No description needed, it is identical
7333 (setq description nil))
7334 (when (and (not description)
7335 (not (equal link (org-link-escape link))))
7336 (setq description (org-extract-attributes link)))
7337 (concat "[[" (org-link-escape link) "]"
7338 (if description (concat "[" description "]") "")
7341 (defconst org-link-escape-chars
7345 (?\340 . "%E0") ; `a
7346 (?\342 . "%E2") ; ^a
7347 (?\347 . "%E7") ; ,c
7348 (?\350 . "%E8") ; `e
7349 (?\351 . "%E9") ; 'e
7350 (?\352 . "%EA") ; ^e
7351 (?\356 . "%EE") ; ^i
7352 (?\364 . "%F4") ; ^o
7353 (?\371 . "%F9") ; `u
7354 (?\373 . "%FB") ; ^u
7360 "Association list of escapes for some characters problematic in links.
7361 This is the list that is used for internal purposes.")
7363 (defvar org-url-encoding-use-url-hexify nil)
7365 (defconst org-link-escape-chars-browser
7366 '((?\ . "%20")) ; 32 for the SPC char
7367 "Association list of escapes for some characters problematic in links.
7368 This is the list that is used before handing over to the browser.")
7370 (defun org-link-escape (text &optional table)
7371 "Escape characters in TEXT that are problematic for links."
7372 (if org-url-encoding-use-url-hexify
7373 (url-hexify-string text)
7374 (setq table (or table org-link-escape-chars))
7376 (let ((re (mapconcat (lambda (x) (regexp-quote
7377 (char-to-string (car x))))
7379 (while (string-match re text)
7382 (cdr (assoc (string-to-char (match-string 0 text))
7387 (defun org-link-unescape (text &optional table)
7388 "Reverse the action of `org-link-escape'."
7389 (if org-url-encoding-use-url-hexify
7390 (url-unhex-string text)
7391 (setq table (or table org-link-escape-chars))
7393 (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
7395 (while (string-match re text)
7398 (char-to-string (car (rassoc (match-string 0 text) table)))
7402 (defun org-xor (a b)
7406 (defun org-fixup-message-id-for-http (s)
7407 "Replace special characters in a message id, so it can be used in an http query."
7408 (while (string-match "<" s)
7409 (setq s (replace-match "%3C" t t s)))
7410 (while (string-match ">" s)
7411 (setq s (replace-match "%3E" t t s)))
7412 (while (string-match "@" s)
7413 (setq s (replace-match "%40" t t s)))
7417 (defun org-insert-link-global ()
7418 "Insert a link like Org-mode does.
7419 This command can be called in any mode to insert a link in Org-mode syntax."
7421 (org-load-modules-maybe)
7422 (org-run-like-in-org-mode 'org-insert-link))
7424 (defun org-insert-link (&optional complete-file link-location)
7425 "Insert a link. At the prompt, enter the link.
7427 Completion can be used to insert any of the link protocol prefixes like
7430 The history can be used to select a link previously stored with
7431 `org-store-link'. When the empty string is entered (i.e. if you just
7432 press RET at the prompt), the link defaults to the most recently
7433 stored link. As SPC triggers completion in the minibuffer, you need to
7434 use M-SPC or C-q SPC to force the insertion of a space character.
7436 You will also be prompted for a description, and if one is given, it will
7437 be displayed in the buffer instead of the link.
7439 If there is already a link at point, this command will allow you to edit link
7440 and description parts.
7442 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
7443 be selected using completion. The path to the file will be relative to the
7444 current directory if the file is in the current directory or a subdirectory.
7445 Otherwise, the link will be the absolute path as completed in the minibuffer
7446 \(i.e. normally ~/path/to/file). You can configure this behavior using the
7447 option `org-link-file-path-type'.
7449 With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
7450 the current directory or below.
7452 With three \\[universal-argument] prefixes, negate the meaning of
7453 `org-keep-stored-link-after-insertion'.
7455 If `org-make-link-description-function' is non-nil, this function will be
7456 called with the link target, and the result will be the default
7459 If the LINK-LOCATION parameter is non-nil, this value will be
7460 used as the link location instead of reading one interactively."
7462 (let* ((wcf (current-window-configuration))
7463 (region (if (org-region-active-p)
7464 (buffer-substring (region-beginning) (region-end))))
7465 (remove (and region (list (region-beginning) (region-end))))
7467 tmphist ; byte-compile incorrectly complains about this
7468 (link link-location)
7469 entry file all-prefixes)
7471 (link-location) ; specified by arg, just use it.
7472 ((org-in-regexp org-bracket-link-regexp 1)
7473 ;; We do have a link at point, and we are going to edit it.
7474 (setq remove (list (match-beginning 0) (match-end 0)))
7475 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
7476 (setq link (read-string "Link: "
7478 (org-match-string-no-properties 1)))))
7479 ((or (org-in-regexp org-angle-link-re)
7480 (org-in-regexp org-plain-link-re))
7481 ;; Convert to bracket link
7482 (setq remove (list (match-beginning 0) (match-end 0))
7483 link (read-string "Link: "
7484 (org-remove-angle-brackets (match-string 0)))))
7485 ((member complete-file '((4) (16)))
7486 ;; Completing read for file names.
7487 (setq link (org-file-complete-link complete-file)))
7489 ;; Read link, with completion for stored links.
7490 (with-output-to-temp-buffer "*Org Links*"
7491 (princ "Insert a link.
7492 Use TAB to complete link prefixes, then RET for type-specific completion support\n")
7493 (when org-stored-links
7494 (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
7497 (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
7498 (reverse org-stored-links) "\n"))))
7499 (let ((cw (selected-window)))
7500 (select-window (get-buffer-window "*Org Links*"))
7501 (setq truncate-lines t)
7502 (unless (pos-visible-in-window-p (point-max))
7503 (org-fit-window-to-buffer))
7504 (and (window-live-p cw) (select-window cw)))
7505 ;; Fake a link history, containing the stored links.
7506 (setq tmphist (append (mapcar 'car org-stored-links)
7507 org-insert-link-history))
7508 (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
7509 (mapcar 'car org-link-abbrev-alist)
7514 (let ((org-completion-use-ido nil))
7515 (org-completing-read
7518 (mapcar (lambda (x) (list (concat x ":")))
7520 (mapcar 'car org-stored-links))
7523 (car (car org-stored-links)))))
7524 (if (or (member link all-prefixes)
7525 (and (equal ":" (substring link -1))
7526 (member (substring link 0 -1) all-prefixes)
7527 (setq link (substring link 0 -1))))
7528 (setq link (org-link-try-special-completion link))))
7529 (set-window-configuration wcf)
7530 (kill-buffer "*Org Links*"))
7531 (setq entry (assoc link org-stored-links))
7532 (or entry (push link org-insert-link-history))
7533 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
7534 (not org-keep-stored-link-after-insertion))
7535 (setq org-stored-links (delq (assoc link org-stored-links)
7537 (setq desc (or desc (nth 1 entry)))))
7539 (if (string-match org-plain-link-re link)
7540 ;; URL-like link, normalize the use of angular brackets.
7541 (setq link (org-make-link (org-remove-angle-brackets link))))
7543 ;; Check if we are linking to the current file with a search option
7544 ;; If yes, simplify the link by using only the search option.
7545 (when (and buffer-file-name
7546 (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
7547 (let* ((path (match-string 1 link))
7548 (case-fold-search nil)
7549 (search (match-string 2 link)))
7551 (if (equal (file-truename buffer-file-name) (file-truename path))
7552 ;; We are linking to this same file, with a search option
7553 (setq link search)))))
7555 ;; Check if we can/should use a relative path. If yes, simplify the link
7556 (when (string-match "^file:\\(.*\\)" link)
7557 (let* ((path (match-string 1 link))
7559 (case-fold-search nil))
7561 ((or (eq org-link-file-path-type 'absolute)
7562 (equal complete-file '(16)))
7563 (setq path (abbreviate-file-name (expand-file-name path))))
7564 ((eq org-link-file-path-type 'noabbrev)
7565 (setq path (expand-file-name path)))
7566 ((eq org-link-file-path-type 'relative)
7567 (setq path (file-relative-name path)))
7570 (if (string-match (concat "^" (regexp-quote
7571 (file-name-as-directory
7572 (expand-file-name "."))))
7573 (expand-file-name path))
7574 ;; We are linking a file with relative path name.
7575 (setq path (substring (expand-file-name path)
7577 (setq path (abbreviate-file-name (expand-file-name path)))))))
7578 (setq link (concat "file:" path))
7579 (if (equal desc origpath)
7582 (if org-make-link-description-function
7583 (setq desc (funcall org-make-link-description-function link desc)))
7585 (setq desc (read-string "Description: " desc))
7586 (unless (string-match "\\S-" desc) (setq desc nil))
7587 (if remove (apply 'delete-region remove))
7588 (insert (org-make-link-string link desc))))
7590 (defun org-link-try-special-completion (type)
7591 "If there is completion support for link type TYPE, offer it."
7592 (let ((fun (intern (concat "org-" type "-complete-link"))))
7595 (read-string "Link (no completion support): " (concat type ":")))))
7597 (defun org-file-complete-link (&optional arg)
7598 "Create a file link using completion."
7600 (setq file (read-file-name "File: "))
7601 (let ((pwd (file-name-as-directory (expand-file-name ".")))
7602 (pwd1 (file-name-as-directory (abbreviate-file-name
7603 (expand-file-name ".")))))
7606 (setq link (org-make-link
7608 (abbreviate-file-name (expand-file-name file)))))
7609 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
7610 (setq link (org-make-link "file:" (match-string 1 file))))
7611 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
7612 (expand-file-name file))
7613 (setq link (org-make-link
7614 "file:" (match-string 1 (expand-file-name file)))))
7615 (t (setq link (org-make-link "file:" file)))))
7618 (defun org-completing-read (&rest args)
7619 "Completing-read with SPACE being a normal character."
7620 (let ((minibuffer-local-completion-map
7621 (copy-keymap minibuffer-local-completion-map)))
7622 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
7623 (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
7624 (apply 'org-ido-completing-read args)))
7626 (defun org-completing-read-no-ido (&rest args)
7627 (let (org-completion-use-ido)
7628 (apply 'org-completing-read args)))
7630 (defun org-ido-completing-read (&rest args)
7631 "Completing-read using `ido-mode' speedups if available"
7632 (if (and org-completion-use-ido
7633 (fboundp 'ido-completing-read)
7634 (boundp 'ido-mode) ido-mode
7635 (listp (second args)))
7636 (let ((ido-enter-matching-directory nil))
7637 (apply 'ido-completing-read (concat (car args))
7638 (mapcar (lambda (x) (car x)) (nth 1 args))
7640 (apply 'completing-read args)))
7642 (defun org-extract-attributes (s)
7643 "Extract the attributes cookie from a string and set as text property."
7644 (let (a attr (start 0) key value)
7646 (when (string-match "{{\\([^}]+\\)}}$" s)
7647 (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
7648 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
7649 (setq key (match-string 1 a) value (match-string 2 a)
7651 attr (plist-put attr (intern key) value))))
7652 (org-add-props s nil 'org-attr attr))
7655 (defun org-extract-attributes-from-string (tag)
7656 (let (key value attr)
7657 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
7658 (setq key (match-string 1 tag) value (match-string 2 tag)
7659 tag (replace-match "" t t tag)
7660 attr (plist-put attr (intern key) value)))
7663 (defun org-attributes-to-string (plist)
7664 "Format a property list into an HTML attribute list."
7665 (let ((s "") key value)
7667 (setq key (pop plist) value (pop plist))
7669 (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
7672 ;;; Opening/following a link
7674 (defvar org-link-search-failed nil)
7676 (defun org-next-link ()
7677 "Move forward to the next link.
7678 If the link is in hidden text, expose it."
7680 (when (and org-link-search-failed (eq this-command last-command))
7681 (goto-char (point-min))
7682 (message "Link search wrapped back to beginning of buffer"))
7683 (setq org-link-search-failed nil)
7684 (let* ((pos (point))
7686 (a (assoc :link ct)))
7687 (if a (goto-char (nth 2 a)))
7688 (if (re-search-forward org-any-link-re nil t)
7690 (goto-char (match-beginning 0))
7691 (if (org-invisible-p) (org-show-context)))
7693 (setq org-link-search-failed t)
7694 (error "No further link found"))))
7696 (defun org-previous-link ()
7697 "Move backward to the previous link.
7698 If the link is in hidden text, expose it."
7700 (when (and org-link-search-failed (eq this-command last-command))
7701 (goto-char (point-max))
7702 (message "Link search wrapped back to end of buffer"))
7703 (setq org-link-search-failed nil)
7704 (let* ((pos (point))
7706 (a (assoc :link ct)))
7707 (if a (goto-char (nth 1 a)))
7708 (if (re-search-backward org-any-link-re nil t)
7710 (goto-char (match-beginning 0))
7711 (if (org-invisible-p) (org-show-context)))
7713 (setq org-link-search-failed t)
7714 (error "No further link found"))))
7716 (defun org-translate-link (s)
7717 "Translate a link string if a translation function has been defined."
7718 (if (and org-link-translation-function
7719 (fboundp org-link-translation-function)
7720 (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
7722 (setq s (funcall org-link-translation-function
7723 (match-string 1) (match-string 2)))
7724 (concat (car s) ":" (cdr s)))
7727 (defun org-translate-link-from-planner (type path)
7728 "Translate a link from Emacs Planner syntax so that Org can follow it.
7729 This is still an experimental function, your mileage may vary."
7731 ((member type '("http" "https" "news" "ftp"))
7732 ;; standard Internet links are the same.
7734 ((and (equal type "irc") (string-match "^//" path))
7735 ;; Planner has two / at the beginning of an irc link, we have 1.
7736 ;; We should have zero, actually....
7737 (setq path (substring path 1)))
7738 ((and (equal type "lisp") (string-match "^/" path))
7739 ;; Planner has a slash, we do not.
7740 (setq type "elisp" path (substring path 1)))
7741 ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
7742 ;; A typical message link. Planner has the id after the fina slash,
7743 ;; we separate it with a hash mark
7744 (setq path (concat (match-string 1 path) "#"
7745 (org-remove-angle-brackets (match-string 2 path)))))
7749 (defun org-find-file-at-mouse (ev)
7750 "Open file link or URL at mouse."
7752 (mouse-set-point ev)
7753 (org-open-at-point 'in-emacs))
7755 (defun org-open-at-mouse (ev)
7756 "Open file link or URL at mouse."
7758 (mouse-set-point ev)
7759 (if (eq major-mode 'org-agenda-mode)
7760 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
7761 (org-open-at-point))
7763 (defvar org-window-config-before-follow-link nil
7764 "The window configuration before following a link.
7765 This is saved in case the need arises to restore it.")
7767 (defvar org-open-link-marker (make-marker)
7768 "Marker pointing to the location where `org-open-at-point; was called.")
7771 (defun org-open-at-point-global ()
7772 "Follow a link like Org-mode does.
7773 This command can be called in any mode to follow a link that has
7776 (org-run-like-in-org-mode 'org-open-at-point))
7779 (defun org-open-link-from-string (s &optional arg)
7780 "Open a link in the string S, as if it was in Org-mode."
7781 (interactive "sLink: \nP")
7782 (let ((reference-buffer (current-buffer)))
7784 (let ((org-inhibit-startup t))
7787 (goto-char (point-min))
7788 (org-open-at-point arg reference-buffer)))))
7790 (defun org-open-at-point (&optional in-emacs reference-buffer)
7791 "Open link at or after point.
7792 If there is no link at point, this function will search forward up to
7793 the end of the current line.
7794 Normally, files will be opened by an appropriate application. If the
7795 optional argument IN-EMACS is non-nil, Emacs will visit the file.
7796 With a double prefix argument, try to open outside of Emacs, in the
7797 application the system uses for this file type."
7799 (org-load-modules-maybe)
7800 (move-marker org-open-link-marker (point))
7801 (setq org-window-config-before-follow-link (current-window-configuration))
7802 (org-remove-occur-highlights nil nil t)
7804 ((org-at-timestamp-p t) (org-follow-timestamp-link))
7805 ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
7806 (org-footnote-action))
7808 (let (type path link line search (pos (point)))
7811 (skip-chars-forward "^]\n\r")
7812 (when (org-in-regexp org-bracket-link-regexp)
7813 (setq link (org-extract-attributes
7814 (org-link-unescape (org-match-string-no-properties 1))))
7815 (while (string-match " *\n *" link)
7816 (setq link (replace-match " " t t link)))
7817 (setq link (org-link-expand-abbrev link))
7819 ((or (file-name-absolute-p link)
7820 (string-match "^\\.\\.?/" link))
7821 (setq type "file" path link))
7822 ((string-match org-link-re-with-space3 link)
7823 (setq type (match-string 1 link) path (match-string 2 link)))
7824 (t (setq type "thisfile" path link)))
7827 (when (get-text-property (point) 'org-linked-text)
7828 (setq type "thisfile"
7829 pos (if (get-text-property (1+ (point)) 'org-linked-text)
7830 (1+ (point)) (point))
7831 path (buffer-substring
7832 (previous-single-property-change pos 'org-linked-text)
7833 (next-single-property-change pos 'org-linked-text)))
7837 (when (or (org-in-regexp org-angle-link-re)
7838 (org-in-regexp org-plain-link-re))
7839 (setq type (match-string 1) path (match-string 2))
7842 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
7844 path (match-string 1))
7845 (while (string-match ":" path)
7846 (setq path (replace-match "+" t t path)))
7848 (when (org-in-regexp "<\\([^><\n]+\\)>")
7849 (setq type "tree-match"
7850 path (match-string 1))
7853 (error "No link found"))
7855 ;; switch back to reference buffer
7856 ;; needed when if called in a temporary buffer through
7857 ;; org-open-link-from-string
7858 (and reference-buffer (switch-to-buffer reference-buffer))
7860 ;; Remove any trailing spaces in path
7861 (if (string-match " +\\'" path)
7862 (setq path (replace-match "" t t path)))
7863 (if (and org-link-translation-function
7864 (fboundp org-link-translation-function))
7865 ;; Check if we need to translate the link
7866 (let ((tmp (funcall org-link-translation-function type path)))
7867 (setq type (car tmp) path (cdr tmp))))
7871 ((assoc type org-link-protocols)
7872 (funcall (nth 1 (assoc type org-link-protocols)) path))
7874 ((equal type "mailto")
7875 (let ((cmd (car org-link-mailto-program))
7876 (args (cdr org-link-mailto-program)) args1
7877 (address path) (subject "") a)
7878 (if (string-match "\\(.*\\)::\\(.*\\)" path)
7879 (setq address (match-string 1 path)
7880 subject (org-link-escape (match-string 2 path))))
7883 ((not (stringp (car args))) (push (pop args) args1))
7884 (t (setq a (pop args))
7885 (if (string-match "%a" a)
7886 (setq a (replace-match address t t a)))
7887 (if (string-match "%s" a)
7888 (setq a (replace-match subject t t a)))
7890 (apply cmd (nreverse args1))))
7892 ((member type '("http" "https" "ftp" "news"))
7893 (browse-url (concat type ":" (org-link-escape
7894 path org-link-escape-chars-browser))))
7896 ((member type '("message"))
7897 (browse-url (concat type ":" path)))
7899 ((string= type "tags")
7900 (org-tags-view in-emacs path))
7901 ((string= type "thisfile")
7903 (switch-to-buffer-other-window
7904 (org-get-buffer-for-internal-link (current-buffer)))
7905 (org-mark-ring-push))
7906 (let ((cmd `(org-link-search
7908 ,(cond ((equal in-emacs '(4)) 'occur)
7909 ((equal in-emacs '(16)) 'org-occur)
7912 (condition-case nil (eval cmd)
7913 (error (progn (widen) (eval cmd))))))
7915 ((string= type "tree-match")
7916 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
7918 ((string= type "file")
7919 (if (string-match "::\\([0-9]+\\)\\'" path)
7920 (setq line (string-to-number (match-string 1 path))
7921 path (substring path 0 (match-beginning 0)))
7922 (if (string-match "::\\(.+\\)\\'" path)
7923 (setq search (match-string 1 path)
7924 path (substring path 0 (match-beginning 0)))))
7925 (if (string-match "[*?{]" (file-name-nondirectory path))
7927 (org-open-file path in-emacs line search)))
7929 ((string= type "news")
7931 (org-gnus-follow-link path))
7933 ((string= type "shell")
7935 (if (or (not org-confirm-shell-link-function)
7936 (funcall org-confirm-shell-link-function
7937 (format "Execute \"%s\" in shell? "
7938 (org-add-props cmd nil
7939 'face 'org-warning))))
7941 (message "Executing %s" cmd)
7942 (shell-command cmd))
7945 ((string= type "elisp")
7947 (if (or (not org-confirm-elisp-link-function)
7948 (funcall org-confirm-elisp-link-function
7949 (format "Execute \"%s\" as elisp? "
7950 (org-add-props cmd nil
7951 'face 'org-warning))))
7952 (message "%s => %s" cmd
7953 (if (equal (string-to-char cmd) ?\()
7955 (call-interactively (read cmd))))
7959 (browse-url-at-point))))))
7960 (move-marker org-open-link-marker nil)
7961 (run-hook-with-args 'org-follow-link-hook))
7965 (defun org-get-effort (&optional pom)
7966 "Get the effort estimate for the current entry."
7967 (org-entry-get pom org-effort-property))
7971 (defvar org-create-file-search-functions nil
7972 "List of functions to construct the right search string for a file link.
7973 These functions are called in turn with point at the location to
7974 which the link should point.
7976 A function in the hook should first test if it would like to
7977 handle this file type, for example by checking the major-mode or
7978 the file extension. If it decides not to handle this file, it
7979 should just return nil to give other functions a chance. If it
7980 does handle the file, it must return the search string to be used
7981 when following the link. The search string will be part of the
7982 file link, given after a double colon, and `org-open-at-point'
7983 will automatically search for it. If special measures must be
7984 taken to make the search successful, another function should be
7985 added to the companion hook `org-execute-file-search-functions',
7988 A function in this hook may also use `setq' to set the variable
7989 `description' to provide a suggestion for the descriptive text to
7990 be used for this link when it gets inserted into an Org-mode
7991 buffer with \\[org-insert-link].")
7993 (defvar org-execute-file-search-functions nil
7994 "List of functions to execute a file search triggered by a link.
7996 Functions added to this hook must accept a single argument, the
7997 search string that was part of the file link, the part after the
7998 double colon. The function must first check if it would like to
7999 handle this search, for example by checking the major-mode or the
8000 file extension. If it decides not to handle this search, it
8001 should just return nil to give other functions a chance. If it
8002 does handle the search, it must return a non-nil value to keep
8003 other functions from trying.
8005 Each function can access the current prefix argument through the
8006 variable `current-prefix-argument'. Note that a single prefix is
8007 used to force opening a link in Emacs, so it may be good to only
8008 use a numeric or double prefix to guide the search function.
8010 In case this is needed, a function in this hook can also restore
8011 the window configuration before `org-open-at-point' was called using:
8013 (set-window-configuration org-window-config-before-follow-link)")
8015 (defun org-link-search (s &optional type avoid-pos)
8016 "Search for a link search option.
8017 If S is surrounded by forward slashes, it is interpreted as a
8018 regular expression. In org-mode files, this will create an `org-occur'
8019 sparse tree. In ordinary files, `occur' will be used to list matches.
8020 If the current buffer is in `dired-mode', grep will be used to search
8021 in all files. If AVOID-POS is given, ignore matches near that position."
8022 (let ((case-fold-search t)
8023 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
8024 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
8025 (append '(("") (" ") ("\t") ("\n"))
8029 (pre nil) (post nil)
8030 words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
8032 ;; First check if there are any special
8033 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
8034 ;; Now try the builtin stuff
8035 ((and (equal (string-to-char s0) ?#)
8038 (goto-char (point-min))
8041 (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
8042 (setq type 'dedicated
8043 pos (match-beginning 0))))
8044 ;; There is an exact target for this
8046 (org-back-to-heading t)))
8048 (goto-char (point-min))
8051 (concat "<<" (regexp-quote s0) ">>") nil t)
8052 (setq type 'dedicated
8053 pos (match-beginning 0))))
8054 ;; There is an exact target for this
8056 ((and (string-match "^(\\(.*\\))$" s0)
8058 (goto-char (point-min))
8061 (concat "[^[]" (regexp-quote
8062 (format org-coderef-label-format
8063 (match-string 1 s0))))
8065 (setq type 'dedicated
8066 pos (1+ (match-beginning 0))))))
8067 ;; There is a coderef target for this
8069 ((string-match "^/\\(.*\\)/$" s)
8070 ;; A regular expression
8073 (org-occur (match-string 1 s)))
8074 ;;((eq major-mode 'dired-mode)
8075 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
8076 (t (org-do-occur (match-string 1 s)))))
8078 ;; A normal search strings
8079 (when (equal (string-to-char s) ?*)
8080 ;; Anchor on headlines, post may include tags.
8081 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
8082 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
8084 (remove-text-properties
8086 '(face nil mouse-face nil keymap nil fontified nil) s)
8087 ;; Make a series of regular expressions to find a match
8088 (setq words (org-split-string s "[ \n\r\t]+")
8090 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
8091 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
8093 re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
8094 re2a (concat "[ \t\r\n]" re2a_)
8095 re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
8096 re4 (concat "[^a-zA-Z_]" re4_)
8098 re1 (concat pre re2 post)
8099 re3 (concat pre (if pre re4_ re4) post)
8100 re5 (concat pre ".*" re4)
8101 re2 (concat pre re2)
8102 re2a (concat pre (if pre re2a_ re2a))
8103 re4 (concat pre (if pre re4_ re4))
8104 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
8105 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
8109 ((eq type 'org-occur) (org-occur reall))
8110 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
8111 (t (goto-char (point-min))
8113 (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
8114 (org-search-not-self 1 re1 nil t)
8115 (org-search-not-self 1 re2 nil t)
8116 (org-search-not-self 1 re2a nil t)
8117 (org-search-not-self 1 re3 nil t)
8118 (org-search-not-self 1 re4 nil t)
8119 (org-search-not-self 1 re5 nil t)
8121 (goto-char (match-beginning 1))
8123 (error "No match")))))
8125 ;; Normal string-search
8126 (goto-char (point-min))
8127 (if (search-forward s nil t)
8128 (goto-char (match-beginning 0))
8129 (error "No match"))))
8130 (and (org-mode-p) (org-show-context 'link-search))
8133 (defun org-search-not-self (group &rest args)
8134 "Execute `re-search-forward', but only accept matches that do not
8135 enclose the position of `org-open-link-marker'."
8136 (let ((m org-open-link-marker))
8138 (while (apply 're-search-forward args)
8139 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
8140 (goto-char (match-end group))
8141 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
8142 (> (match-beginning 0) (marker-position m))
8143 (< (match-end 0) (marker-position m)))
8145 (or (not (org-in-regexp
8146 org-bracket-link-analytic-regexp 1))
8147 (not (match-end 4)) ; no description
8148 (and (<= (match-beginning 4) (point))
8149 (>= (match-end 4) (point))))))
8150 (throw 'exit (point))))))))
8152 (defun org-get-buffer-for-internal-link (buffer)
8153 "Return a buffer to be used for displaying the link target of internal links."
8155 ((not org-display-internal-link-with-indirect-buffer)
8157 ((string-match "(Clone)$" (buffer-name buffer))
8158 (message "Buffer is already a clone, not making another one")
8159 ;; we also do not modify visibility in this case
8161 (t ; make a new indirect buffer for displaying the link
8162 (let* ((bn (buffer-name buffer))
8163 (ibn (concat bn "(Clone)"))
8164 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
8165 (with-current-buffer ib (org-overview))
8168 (defun org-do-occur (regexp &optional cleanup)
8169 "Call the Emacs command `occur'.
8170 If CLEANUP is non-nil, remove the printout of the regular expression
8171 in the *Occur* buffer. This is useful if the regex is long and not useful
8175 (let ((cwin (selected-window)) win beg end)
8176 (when (setq win (get-buffer-window "*Occur*"))
8177 (select-window win))
8178 (goto-char (point-min))
8179 (when (re-search-forward "match[a-z]+" nil t)
8180 (setq beg (match-end 0))
8181 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
8182 (setq end (1- (match-beginning 0)))))
8183 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
8184 (goto-char (point-min))
8185 (select-window cwin))))
8187 ;;; The mark ring for links jumps
8189 (defvar org-mark-ring nil
8190 "Mark ring for positions before jumps in Org-mode.")
8191 (defvar org-mark-ring-last-goto nil
8192 "Last position in the mark ring used to go back.")
8193 ;; Fill and close the ring
8194 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
8195 (loop for i from 1 to org-mark-ring-length do
8196 (push (make-marker) org-mark-ring))
8197 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
8200 (defun org-mark-ring-push (&optional pos buffer)
8201 "Put the current position or POS into the mark ring and rotate it."
8203 (setq pos (or pos (point)))
8204 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
8205 (move-marker (car org-mark-ring)
8207 (or buffer (current-buffer)))
8209 (substitute-command-keys
8210 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
8212 (defun org-mark-ring-goto (&optional n)
8213 "Jump to the previous position in the mark ring.
8214 With prefix arg N, jump back that many stored positions. When
8215 called several times in succession, walk through the entire ring.
8216 Org-mode commands jumping to a different position in the current file,
8217 or to another Org-mode file, automatically push the old position
8221 (if (eq last-command this-command)
8222 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
8223 (setq p org-mark-ring))
8224 (setq org-mark-ring-last-goto p)
8226 (switch-to-buffer (marker-buffer m))
8228 (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
8230 (defun org-remove-angle-brackets (s)
8231 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
8232 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
8234 (defun org-add-angle-brackets (s)
8235 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
8236 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
8238 (defun org-remove-double-quotes (s)
8239 (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
8240 (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
8243 ;;; Following specific links
8245 (defun org-follow-timestamp-link ()
8247 ((org-at-date-range-p t)
8248 (let ((org-agenda-start-on-weekday)
8249 (t1 (match-string 1))
8250 (t2 (match-string 2)))
8251 (setq t1 (time-to-days (org-time-string-to-time t1))
8252 t2 (time-to-days (org-time-string-to-time t2)))
8253 (org-agenda-list nil t1 (1+ (- t2 t1)))))
8254 ((org-at-timestamp-p t)
8255 (org-agenda-list nil (time-to-days (org-time-string-to-time
8256 (substring (match-string 1) 0 10)))
8258 (t (error "This should not happen"))))
8261 ;;; Following file links
8262 (defvar org-wait nil)
8263 (defun org-open-file (path &optional in-emacs line search)
8264 "Open the file at PATH.
8265 First, this expands any special file name abbreviations. Then the
8266 configuration variable `org-file-apps' is checked if it contains an
8267 entry for this file type, and if yes, the corresponding command is launched.
8269 If no application is found, Emacs simply visits the file.
8271 With optional prefix argument IN-EMACS, Emacs will visit the file.
8272 With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
8273 and o use an external application to visit the file.
8275 Optional LINE specifies a line to go to, optional SEARCH a string to
8276 search for. If LINE or SEARCH is given, the file will always be
8278 If the file does not exist, an error is thrown."
8279 (setq in-emacs (or in-emacs line search))
8280 (let* ((file (if (equal path "")
8282 (substitute-in-file-name (expand-file-name path))))
8283 (apps (append org-file-apps (org-default-apps)))
8284 (remp (and (assq 'remote apps) (org-file-remote-p file)))
8285 (dirp (if remp nil (file-directory-p file)))
8286 (file (if (and dirp org-open-directory-means-index-dot-org)
8287 (concat (file-name-as-directory file) "index.org")
8289 (a-m-a-p (assq 'auto-mode apps))
8290 (dfile (downcase file))
8291 (old-buffer (current-buffer))
8293 (old-mode major-mode)
8295 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
8296 (setq ext (match-string 1 dfile))
8297 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
8298 (setq ext (match-string 1 dfile))))
8300 ((equal in-emacs '(16))
8301 (setq cmd (cdr (assoc 'system apps))))
8302 (in-emacs (setq cmd 'emacs))
8304 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
8305 (and dirp (cdr (assoc 'directory apps)))
8306 (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
8308 (cdr (assoc ext apps))
8309 (cdr (assoc t apps))))))
8310 (when (eq cmd 'system)
8311 (setq cmd (cdr (assoc 'system apps))))
8312 (when (eq cmd 'default)
8313 (setq cmd (cdr (assoc t apps))))
8314 (when (eq cmd 'mailcap)
8316 (mailcap-parse-mailcaps)
8317 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
8318 (command (mailcap-mime-info mime-type)))
8319 (if (stringp command)
8321 (setq cmd 'emacs))))
8322 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
8323 (not (file-exists-p file))
8324 (not org-open-non-existing-files))
8325 (error "No such file: %s" file))
8327 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
8328 ;; Remove quotes around the file name - we'll use shell-quote-argument.
8329 (while (string-match "['\"]%s['\"]" cmd)
8330 (setq cmd (replace-match "%s" t t cmd)))
8331 (while (string-match "%s" cmd)
8332 (setq cmd (replace-match
8334 (shell-quote-argument
8335 (convert-standard-filename file)))
8337 (save-window-excursion
8338 (start-process-shell-command cmd nil cmd)
8339 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
8343 (funcall (cdr (assq 'file org-link-frame-setup)) file)
8345 (if line (goto-line line)
8346 (if search (org-link-search search))))
8348 (let ((file (convert-standard-filename file)))
8350 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
8351 (and (org-mode-p) (eq old-mode 'org-mode)
8352 (or (not (equal old-buffer (current-buffer)))
8353 (not (equal old-pos (point))))
8354 (org-mark-ring-push old-pos old-buffer))))
8356 (defun org-default-apps ()
8357 "Return the default applications for this operating system."
8359 ((eq system-type 'darwin)
8360 org-file-apps-defaults-macosx)
8361 ((eq system-type 'windows-nt)
8362 org-file-apps-defaults-windowsnt)
8363 (t org-file-apps-defaults-gnu)))
8365 (defun org-apps-regexp-alist (list &optional add-auto-mode)
8366 "Convert extensions to regular expressions in the cars of LIST.
8367 Also, weed out any non-string entries, because the return value is used
8368 only for regexp matching.
8369 When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
8370 point to the symbol `emacs', indicating that the file should
8371 be opened in Emacs."
8375 (if (not (stringp (car x)))
8377 (if (string-match "\\W" (car x))
8379 (cons (concat "\\." (car x) "\\'") (cdr x)))))
8382 (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
8384 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
8385 (defun org-file-remote-p (file)
8386 "Test whether FILE specifies a location on a remote system.
8387 Return non-nil if the location is indeed remote.
8389 For example, the filename \"/user@host:/foo\" specifies a location
8390 on the system \"/user@host:\"."
8391 (cond ((fboundp 'file-remote-p)
8392 (file-remote-p file))
8393 ((fboundp 'tramp-handle-file-remote-p)
8394 (tramp-handle-file-remote-p file))
8395 ((and (boundp 'ange-ftp-name-format)
8396 (string-match (car ange-ftp-name-format) file))
8403 (defun org-get-org-file ()
8404 "Read a filename, with default directory `org-directory'."
8405 (let ((default (or org-default-notes-file remember-data-file)))
8406 (read-file-name (format "File name [%s]: " default)
8407 (file-name-as-directory org-directory)
8410 (defun org-notes-order-reversed-p ()
8411 "Check if the current file should receive notes in reversed order."
8413 ((not org-reverse-note-order) nil)
8414 ((eq t org-reverse-note-order) t)
8415 ((not (listp org-reverse-note-order)) nil)
8417 (let ((all org-reverse-note-order)
8419 (while (setq entry (pop all))
8420 (if (string-match (car entry) buffer-file-name)
8421 (throw 'exit (cdr entry))))
8424 (defvar org-refile-target-table nil
8425 "The list of refile targets, created by `org-refile'.")
8427 (defvar org-agenda-new-buffers nil
8428 "Buffers created to visit agenda files.")
8430 (defun org-get-refile-targets (&optional default-buffer)
8431 "Produce a table with refile targets."
8432 (let ((case-fold-search nil)
8433 ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
8434 (entries (or org-refile-targets '((nil . (:level . 1)))))
8435 targets txt re files f desc descre fast-path-p level pos0)
8436 (message "Getting targets...")
8437 (with-current-buffer (or default-buffer (current-buffer))
8438 (while (setq entry (pop entries))
8439 (setq files (car entry) desc (cdr entry))
8440 (setq fast-path-p nil)
8442 ((null files) (setq files (list (current-buffer))))
8443 ((eq files 'org-agenda-files)
8444 (setq files (org-agenda-files 'unrestricted)))
8445 ((and (symbolp files) (fboundp files))
8446 (setq files (funcall files)))
8447 ((and (symbolp files) (boundp files))
8448 (setq files (symbol-value files))))
8449 (if (stringp files) (setq files (list files)))
8451 ((eq (car desc) :tag)
8452 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
8453 ((eq (car desc) :todo)
8454 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
8455 ((eq (car desc) :regexp)
8456 (setq descre (cdr desc)))
8457 ((eq (car desc) :level)
8458 (setq descre (concat "^\\*\\{" (number-to-string
8459 (if org-odd-levels-only
8460 (1- (* 2 (cdr desc)))
8463 ((eq (car desc) :maxlevel)
8464 (setq fast-path-p t)
8465 (setq descre (concat "^\\*\\{1," (number-to-string
8466 (if org-odd-levels-only
8467 (1- (* 2 (cdr desc)))
8470 (t (error "Bad refiling target description %s" desc)))
8471 (while (setq f (pop files))
8473 (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
8474 (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
8475 (setq f (expand-file-name f))
8476 (if (eq org-refile-use-outline-path 'file)
8477 (push (list (file-name-nondirectory f) f nil nil) targets))
8481 (goto-char (point-min))
8482 (while (re-search-forward descre nil t)
8483 (goto-char (setq pos0 (point-at-bol)))
8485 (when org-refile-target-verify-function
8487 (or (funcall org-refile-target-verify-function)
8489 (when (looking-at org-complex-heading-regexp)
8490 (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
8491 txt (org-link-display-format (match-string 4))
8492 re (concat "^" (regexp-quote
8493 (buffer-substring (match-beginning 1)
8495 (if (match-end 5) (setq re (concat re "[ \t]+"
8497 (match-string 5)))))
8498 (setq re (concat re "[ \t]*$"))
8499 (when org-refile-use-outline-path
8500 (setq txt (mapconcat 'org-protect-slash
8502 (if (eq org-refile-use-outline-path 'file)
8503 (list (file-name-nondirectory
8504 (buffer-file-name (buffer-base-buffer))))
8505 (if (eq org-refile-use-outline-path 'full-file-path)
8506 (list (buffer-file-name (buffer-base-buffer)))))
8507 (org-get-outline-path fast-path-p level txt)
8510 (push (list txt f re (point)) targets)))
8511 (when (= (point) pos0)
8512 ;; verification function has not moved point
8513 (goto-char (point-at-eol))))))))))
8514 (message "Getting targets...done")
8515 (nreverse targets)))
8517 (defun org-protect-slash (s)
8518 (while (string-match "/" s)
8519 (setq s (replace-match "\\" t t s)))
8522 (defvar org-olpa (make-vector 20 nil))
8524 (defun org-get-outline-path (&optional fastp level heading)
8525 "Return the outline path to the current entry, as a list."
8529 (error "Outline path failure, more than 19 levels."))
8530 (loop for i from level upto 19 do
8531 (aset org-olpa i nil))
8533 (delq nil (append org-olpa nil))
8534 (aset org-olpa level heading)))
8537 (while (org-up-heading-safe)
8538 (when (looking-at org-complex-heading-regexp)
8539 (push (org-match-string-no-properties 4) rtn)))
8542 (defvar org-refile-history nil
8543 "History for refiling operations.")
8545 (defvar org-after-refile-insert-hook nil
8546 "Hook run after `org-refile' has inserted its stuff at the new location.
8547 Note that this is still *before* the stuff will be removed from
8548 the *old* location.")
8550 (defun org-refile (&optional goto default-buffer rfloc)
8551 "Move the entry at point to another heading.
8552 The list of target headings is compiled using the information in
8553 `org-refile-targets', which see. This list is created before each use
8554 and will therefore always be up-to-date.
8556 At the target location, the entry is filed as a subitem of the target heading.
8557 Depending on `org-reverse-note-order', the new subitem will either be the
8558 first or the last subitem.
8560 If there is an active region, all entries in that region will be moved.
8561 However, the region must fulfil the requirement that the first heading
8562 is the first one sets the top-level of the moved text - at most siblings
8563 below it are allowed.
8565 With prefix arg GOTO, the command will only visit the target location,
8566 not actually move anything.
8567 With a double prefix `C-u C-u', go to the location where the last refiling
8568 operation has put the subtree.
8570 RFLOC can be a refile location obtained in a different way.
8572 See also `org-refile-use-outline-path' and `org-completion-use-ido'"
8574 (let* ((cbuf (current-buffer))
8575 (regionp (org-region-active-p))
8576 (region-start (and regionp (region-beginning)))
8577 (region-end (and regionp (region-end)))
8578 (region-length (and regionp (- region-end region-start)))
8579 (filename (buffer-file-name (buffer-base-buffer cbuf)))
8580 pos it nbuf file re level reversed)
8581 (when regionp (goto-char region-start)
8582 (unless (org-kill-is-subtree-p
8583 (buffer-substring region-start region-end))
8584 (error "The region is not a (sequence of) subtree(s)")))
8585 (if (equal goto '(16))
8586 (org-refile-goto-last-stored)
8587 (when (setq it (or rfloc
8589 (org-refile-get-location
8590 (if goto "Goto: " "Refile to: ") default-buffer
8591 org-refile-allow-creating-parent-nodes))))
8592 (setq file (nth 1 it)
8597 (equal (buffer-file-name) file)
8599 (and (>= pos region-start)
8600 (<= pos region-end))
8601 (and (>= pos (point))
8602 (< pos (save-excursion
8603 (org-end-of-subtree t t))))))
8604 (error "Cannot refile to position inside the tree or region"))
8606 (setq nbuf (or (find-buffer-visiting file)
8607 (find-file-noselect file)))
8610 (switch-to-buffer nbuf)
8612 (org-show-context 'org-goto))
8615 (org-kill-new (buffer-substring region-start region-end))
8616 (org-save-markers-in-region region-start region-end))
8617 (org-copy-subtree 1 nil t))
8619 (set-buffer (setq nbuf (or (find-buffer-visiting file)
8620 (find-file-noselect file))))
8621 (setq reversed (org-notes-order-reversed-p))
8628 (looking-at outline-regexp)
8629 (setq level (org-get-valid-level (funcall outline-level) 1))
8632 (or (outline-next-heading) (point-max))
8633 (or (save-excursion (outline-get-next-sibling))
8634 (org-end-of-subtree t t)
8638 (goto-char (point-max))
8639 (goto-char (point-min))
8640 (or (outline-next-heading) (goto-char (point-max)))))
8641 (if (not (bolp)) (newline))
8642 (bookmark-set "org-refile-last-stored")
8643 (org-paste-subtree level)
8644 (if (fboundp 'deactivate-mark) (deactivate-mark))
8645 (run-hooks 'org-after-refile-insert-hook))))
8647 (delete-region (point) (+ (point) region-length))
8649 (when (featurep 'org-inlinetask)
8650 (org-inlinetask-remove-END-maybe))
8651 (setq org-markers-to-move nil)
8652 (message "Refiled to \"%s\"" (car it))))))
8655 (defun org-refile-goto-last-stored ()
8656 "Go to the location where the last refile was stored."
8658 (bookmark-jump "org-refile-last-stored")
8659 (message "This is the location of the last refile"))
8661 (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
8662 "Prompt the user for a refile location, using PROMPT."
8663 (let ((org-refile-targets org-refile-targets)
8664 (org-refile-use-outline-path org-refile-use-outline-path))
8665 (setq org-refile-target-table (org-get-refile-targets default-buffer)))
8666 (unless org-refile-target-table
8667 (error "No refile targets"))
8668 (let* ((cbuf (current-buffer))
8669 (partial-completion-mode nil)
8670 (cfn (buffer-file-name (buffer-base-buffer cbuf)))
8671 (cfunc (if (and org-refile-use-outline-path
8672 org-outline-path-complete-in-steps)
8673 'org-olpath-completing-read
8674 'org-ido-completing-read))
8675 (extra (if org-refile-use-outline-path "/" ""))
8676 (filename (and cfn (expand-file-name cfn)))
8679 (if (and (not (member org-refile-use-outline-path
8680 '(file full-file-path)))
8681 (not (equal filename (nth 1 x))))
8682 (cons (concat (car x) extra " ("
8683 (file-name-nondirectory (nth 1 x)) ")")
8685 (cons (concat (car x) extra) (cdr x))))
8686 org-refile-target-table))
8687 (completion-ignore-case t)
8688 pa answ parent-target child parent)
8689 (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
8690 nil 'org-refile-history))
8691 (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
8694 (setcar org-refile-history (car pa))
8696 (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
8697 (setq parent (match-string 1 answ)
8698 child (match-string 2 answ))
8699 (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
8700 (when (and parent-target
8701 (or (eq new-nodes t)
8702 (and (eq new-nodes 'confirm)
8703 (y-or-n-p (format "Create new node \"%s\"? " child)))))
8704 (org-refile-new-child parent-target child))))))
8706 (defun org-refile-new-child (parent-target child)
8707 "Use refile target PARENT-TARGET to add new CHILD below it."
8708 (unless parent-target
8709 (error "Cannot find parent for new node"))
8710 (let ((file (nth 1 parent-target))
8711 (pos (nth 3 parent-target))
8713 (with-current-buffer (or (find-buffer-visiting file)
8714 (find-file-noselect file))
8720 (goto-char (point-max))
8721 (if (not (bolp)) (newline)))
8722 (when (looking-at outline-regexp)
8723 (setq level (funcall outline-level))
8724 (org-end-of-subtree t t))
8725 (org-back-over-empty-lines)
8726 (insert "\n" (make-string
8727 (if pos (org-get-valid-level level 1) 1) ?*)
8729 (beginning-of-line 0)
8730 (list (concat (car parent-target) "/" child) file "" (point)))))))
8732 (defun org-olpath-completing-read (prompt collection &rest args)
8733 "Read an outline path like a file name."
8734 (let ((thetable collection)
8735 (org-completion-use-ido nil)) ; does not work with ido.
8737 'org-ido-completing-read prompt
8738 (lambda (string predicate &optional flag)
8739 (let (rtn r f (l (length string)))
8743 (try-completion string thetable))
8746 (setq rtn (all-completions string thetable predicate))
8749 (setq r (substring x l))
8750 (if (string-match " ([^)]*)$" x)
8751 (setq f (match-string 0 x))
8753 (if (string-match "/" r)
8754 (concat string (substring r 0 (match-end 0)) f)
8759 (assoc string thetable)))
8765 (defun org-find-dblock (name)
8766 "Find the first dynamic block with name NAME in the buffer.
8767 If not found, stay at current position and return nil."
8770 (goto-char (point-min))
8771 (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
8773 (match-beginning 0))))
8774 (if pos (goto-char pos))
8777 (defconst org-dblock-start-re
8778 "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
8779 "Matches the startline of a dynamic block, with parameters.")
8781 (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
8782 "Matches the end of a dynamic block.")
8784 (defun org-create-dblock (plist)
8785 "Create a dynamic block section, with parameters taken from PLIST.
8786 PLIST must contain a :name entry which is used as name of the block."
8787 (unless (bolp) (newline))
8788 (let ((name (plist-get plist :name)))
8789 (insert "#+BEGIN: " name)
8791 (if (eq (car plist) :name)
8792 (setq plist (cddr plist))
8793 (insert " " (prin1-to-string (pop plist)))))
8794 (insert "\n\n#+END:\n")
8795 (beginning-of-line -2)))
8797 (defun org-prepare-dblock ()
8798 "Prepare dynamic block for refresh.
8799 This empties the block, puts the cursor at the insert position and returns
8800 the property list including an extra property :name with the block name."
8801 (unless (looking-at org-dblock-start-re)
8802 (error "Not at a dynamic block"))
8803 (let* ((begdel (1+ (match-end 0)))
8804 (name (org-no-properties (match-string 1)))
8805 (params (append (list :name name)
8806 (read (concat "(" (match-string 3) ")")))))
8807 (unless (re-search-forward org-dblock-end-re nil t)
8808 (error "Dynamic block not terminated"))
8811 (list :content (buffer-substring
8812 begdel (match-beginning 0)))))
8813 (delete-region begdel (match-beginning 0))
8818 (defun org-map-dblocks (&optional command)
8819 "Apply COMMAND to all dynamic blocks in the current buffer.
8820 If COMMAND is not given, use `org-update-dblock'."
8821 (let ((cmd (or command 'org-update-dblock))
8824 (goto-char (point-min))
8825 (while (re-search-forward org-dblock-start-re nil t)
8826 (goto-char (setq pos (match-beginning 0)))
8829 (error (message "Error during update of dynamic block")))
8831 (unless (re-search-forward org-dblock-end-re nil t)
8832 (error "Dynamic block not terminated"))))))
8834 (defun org-dblock-update (&optional arg)
8835 "User command for updating dynamic blocks.
8836 Update the dynamic block at point. With prefix ARG, update all dynamic
8837 blocks in the buffer."
8840 (org-update-all-dblocks)
8841 (or (looking-at org-dblock-start-re)
8842 (org-beginning-of-dblock))
8843 (org-update-dblock)))
8845 (defun org-update-dblock ()
8846 "Update the dynamic block at point
8847 This means to empty the block, parse for parameters and then call
8848 the correct writing function."
8849 (save-window-excursion
8850 (let* ((pos (point))
8851 (line (org-current-line))
8852 (params (org-prepare-dblock))
8853 (name (plist-get params :name))
8854 (cmd (intern (concat "org-dblock-write:" name))))
8855 (message "Updating dynamic block `%s' at line %d..." name line)
8856 (funcall cmd params)
8857 (message "Updating dynamic block `%s' at line %d...done" name line)
8860 (defun org-beginning-of-dblock ()
8861 "Find the beginning of the dynamic block at point.
8862 Error if there is no such block at point."
8866 (if (and (re-search-backward org-dblock-start-re nil t)
8867 (setq beg (match-beginning 0))
8868 (re-search-forward org-dblock-end-re nil t)
8869 (> (match-end 0) pos))
8872 (error "Not in a dynamic block"))))
8874 (defun org-update-all-dblocks ()
8875 "Update all dynamic blocks in the buffer.
8876 This function can be used in a hook."
8878 (org-map-dblocks 'org-update-dblock)))
8883 (defconst org-additional-option-like-keywords
8884 '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
8885 "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
8886 "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:" "ATTR_LaTeX"
8888 "ORGTBL" "TBLFM:" "TBLNAME:"
8889 "BEGIN_EXAMPLE" "END_EXAMPLE"
8890 "BEGIN_QUOTE" "END_QUOTE"
8891 "BEGIN_VERSE" "END_VERSE"
8892 "BEGIN_CENTER" "END_CENTER"
8893 "BEGIN_SRC" "END_SRC"
8894 "CATEGORY" "COLUMNS"
8898 (defcustom org-structure-template-alist
8900 ("s" "#+begin_src ?\n\n#+end_src"
8901 "<src lang=\"?\">\n\n</src>")
8902 ("e" "#+begin_example\n?\n#+end_example"
8903 "<example>\n?\n</example>")
8904 ("q" "#+begin_quote\n?\n#+end_quote"
8905 "<quote>\n?\n</quote>")
8906 ("v" "#+begin_verse\n?\n#+end_verse"
8907 "<verse>\n?\n/verse>")
8908 ("c" "#+begin_center\n?\n#+end_center"
8909 "<center>\n?\n/center>")
8910 ("l" "#+begin_latex\n?\n#+end_latex"
8911 "<literal style=\"latex\">\n?\n</literal>")
8913 "<literal style=\"latex\">?</literal>")
8914 ("h" "#+begin_html\n?\n#+end_html"
8915 "<literal style=\"html\">\n?\n</literal>")
8917 "<literal style=\"html\">?</literal>")
8918 ("a" "#+begin_ascii\n?\n#+end_ascii")
8920 ("i" "#+include %file ?"
8921 "<include file=%file markup=\"?\">")
8923 "Structure completion elements.
8924 This is a list of abbreviation keys and values. The value gets inserted
8925 it you type @samp{.} followed by the key and then the completion key,
8926 usually `M-TAB'. %file will be replaced by a file name after prompting
8927 for the file using completion.
8928 There are two templates for each key, the first uses the original Org syntax,
8929 the second uses Emacs Muse-like syntax tags. These Muse-like tags become
8930 the default when the /org-mtags.el/ module has been loaded. See also the
8931 variable `org-mtags-prefer-muse-templates'.
8932 This is an experimental feature, it is undecided if it is going to stay in."
8933 :group 'org-completion
8936 (string :tag "Template")
8937 (string :tag "Muse Template")))
8939 (defun org-try-structure-completion ()
8940 "Try to complete a structure template before point.
8941 This looks for strings like \"<e\" on an otherwise empty line and
8943 (let ((l (buffer-substring (point-at-bol) (point)))
8945 (when (and (looking-at "[ \t]*$")
8946 (string-match "^[ \t]*<\\([a-z]+\\)$"l)
8947 (setq a (assoc (match-string 1 l) org-structure-template-alist)))
8948 (org-complete-expand-structure-template (+ -1 (point-at-bol)
8949 (match-beginning 1)) a)
8952 (defun org-complete-expand-structure-template (start cell)
8953 "Expand a structure template."
8954 (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
8955 (rpl (nth (if musep 2 1) cell))
8957 (delete-region start (point))
8958 (when (string-match "\\`#\\+" rpl)
8961 ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
8962 (setq ind (buffer-substring (point-at-bol) (point))))
8964 (setq start (point))
8965 (if (string-match "%file" rpl)
8966 (setq rpl (replace-match
8970 (abbreviate-file-name (read-file-name "Include file: ")))
8973 (setq rpl (mapconcat 'identity (split-string rpl "\n")
8976 (if (re-search-backward "\\?" start t) (delete-char 1))))
8979 (defun org-complete (&optional arg)
8980 "Perform completion on word at point.
8981 At the beginning of a headline, this completes TODO keywords as given in
8982 `org-todo-keywords'.
8983 If the current word is preceded by a backslash, completes the TeX symbols
8984 that are supported for HTML support.
8985 If the current word is preceded by \"#+\", completes special words for
8986 setting file options.
8987 In the line after \"#+STARTUP:, complete valid keywords.\"
8988 At all other locations, this simply calls the value of
8989 `org-completion-fallback-command'."
8991 (org-without-partial-completion
8995 (beg1 (save-excursion
8996 (skip-chars-backward (org-re "[:alnum:]_@"))
8998 (beg (save-excursion
8999 (skip-chars-backward "a-zA-Z0-9_:$")
9001 (confirm (lambda (x) (stringp (car x))))
9002 (searchhead (equal (char-before beg) ?*))
9004 (when (and (member (char-before beg1) '(?. ?<))
9005 (setq a (assoc (buffer-substring beg1 (point))
9006 org-structure-template-alist)))
9007 (org-complete-expand-structure-template (1- beg1) a)
9009 (tag (and (equal (char-before beg1) ?:)
9010 (equal (char-after (point-at-bol)) ?*)))
9011 (prop (and (equal (char-before beg1) ?:)
9012 (not (equal (char-after (point-at-bol)) ?*))))
9013 (texp (equal (char-before beg) ?\\))
9014 (link (equal (char-before beg) ?\[))
9015 (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
9018 (startup (string-match "^#\\+STARTUP:.*"
9019 (buffer-substring (point-at-bol) (point))))
9020 (completion-ignore-case opt)
9030 (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
9031 (cons (match-string 2 x) (match-string 1 x)))
9032 (org-split-string (org-get-current-options) "\n"))
9033 (mapcar 'list org-additional-option-like-keywords)))
9035 (setq type :startup)
9036 org-startup-options)
9037 (link (append org-link-abbrev-alist-local
9038 org-link-abbrev-alist))
9042 ((string-match "\\`\\*+[ \t]+\\'"
9043 (buffer-substring (point-at-bol) beg))
9045 (mapcar 'list org-todo-keywords-1))
9047 (setq type :searchhead)
9049 (goto-char (point-min))
9050 (while (re-search-forward org-todo-line-regexp nil t)
9052 (org-make-org-heading-search-string
9053 (match-string 3) t))
9056 (tag (setq type :tag beg beg1)
9057 (or org-tag-alist (org-get-buffer-tags)))
9058 (prop (setq type :prop beg beg1)
9059 (mapcar 'list (org-buffer-property-keys nil t t)))
9061 (call-interactively org-completion-fallback-command)
9062 (throw 'exit nil)))))
9063 (pattern (buffer-substring-no-properties beg end))
9064 (completion (try-completion pattern table confirm)))
9065 (cond ((eq completion t)
9066 (if (not (assoc (upcase pattern) table))
9067 (message "Already complete")
9068 (if (and (equal type :opt)
9069 (not (member (car (assoc (upcase pattern) table))
9070 org-additional-option-like-keywords)))
9071 (insert (substring (cdr (assoc (upcase pattern) table))
9073 (if (memq type '(:tag :prop)) (insert ":")))))
9075 (message "Can't find completion for \"%s\"" pattern)
9077 ((not (string= pattern completion))
9078 (delete-region beg end)
9079 (if (string-match " +$" completion)
9080 (setq completion (replace-match "" t t completion)))
9082 (if (get-buffer-window "*Completions*")
9083 (delete-window (get-buffer-window "*Completions*")))
9084 (if (assoc completion table)
9085 (if (eq type :todo) (insert " ")
9086 (if (memq type '(:tag :prop)) (insert ":"))))
9087 (if (and (equal type :opt) (assoc completion table))
9088 (message "%s" (substitute-command-keys
9089 "Press \\[org-complete] again to insert example settings"))))
9091 (message "Making completion list...")
9092 (let ((list (sort (all-completions pattern table confirm)
9094 (with-output-to-temp-buffer "*Completions*"
9096 ;; Protection needed for XEmacs and emacs 21
9097 (display-completion-list list pattern)
9098 (error (display-completion-list list)))))
9099 (message "Making completion list...%s" "done")))))))
9101 ;;;; TODO, DEADLINE, Comments
9103 (defun org-toggle-comment ()
9104 "Change the COMMENT state of an entry."
9107 (org-back-to-heading)
9108 (let (case-fold-search)
9109 (if (looking-at (concat outline-regexp
9110 "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
9111 (replace-match "" t t nil 1)
9112 (if (looking-at outline-regexp)
9114 (goto-char (match-end 0))
9115 (insert org-comment-string " ")))))))
9117 (defvar org-last-todo-state-is-todo nil
9118 "This is non-nil when the last TODO state change led to a TODO state.
9119 If the last change removed the TODO tag or switched to DONE, then
9122 (defvar org-setting-tags nil) ; dynamically skipped
9124 (defun org-parse-local-options (string var)
9125 "Parse STRING for startup setting relevant for variable VAR."
9126 (let ((rtn (symbol-value var))
9129 (if (or (not string) (not (string-match "\\S-" string)))
9131 (setq opts (delq nil (mapcar (lambda (x)
9132 (setq e (assoc x org-startup-options))
9133 (if (eq (nth 1 e) var) e nil))
9134 (org-split-string string "[ \t]+"))))
9138 (while (setq e (pop opts))
9140 (setq rtn (nth 2 e))
9141 (if (not (listp rtn)) (setq rtn nil))
9142 (push (nth 2 e) rtn)))
9145 (defvar org-todo-setup-filter-hook nil
9146 "Hook for functions that pre-filter todo specs.
9148 Each function takes a todo spec and returns either `nil' or the spec
9149 transformed into canonical form." )
9151 (defvar org-todo-get-default-hook nil
9152 "Hook for functions that get a default item for todo.
9154 Each function takes arguments (NEW-MARK OLD-MARK) and returns either
9155 `nil' or a string to be used for the todo mark." )
9157 (defvar org-agenda-headline-snapshot-before-repeat)
9159 (defun org-todo (&optional arg)
9160 "Change the TODO state of an item.
9161 The state of an item is given by a keyword at the start of the heading,
9163 *** TODO Write paper
9166 The different keywords are specified in the variable `org-todo-keywords'.
9167 By default the available states are \"TODO\" and \"DONE\".
9168 So for this example: when the item starts with TODO, it is changed to DONE.
9169 When it starts with DONE, the DONE is removed. And when neither TODO nor
9170 DONE are present, add TODO at the beginning of the heading.
9172 With C-u prefix arg, use completion to determine the new state.
9173 With numeric prefix arg, switch to that state.
9174 With a double C-u prefix, switch to the next set of TODO keywords (nextset).
9175 With a tripple C-u prefix, circumvent any state blocking.
9177 For calling through lisp, arg is also interpreted in the following way:
9178 'none -> empty state
9179 \"\"(empty string) -> switch to empty state
9180 'done -> switch to DONE
9181 'nextset -> switch to the next set of keywords
9182 'previousset -> switch to the previous set of keywords
9183 \"WAITING\" -> switch to the specified keyword, but only if it
9184 really is a member of `org-todo-keywords'."
9186 (if (equal arg '(16)) (setq arg 'nextset))
9187 (let ((org-blocker-hook org-blocker-hook)
9188 (case-fold-search nil))
9189 (when (equal arg '(64))
9190 (setq arg nil org-blocker-hook nil))
9191 (when (and org-blocker-hook
9192 (or org-inhibit-blocking
9193 (org-entry-get nil "NOBLOCKING")))
9194 (setq org-blocker-hook nil))
9197 (org-back-to-heading)
9198 (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
9199 (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
9201 (let* ((match-data (match-data))
9202 (startpos (point-at-bol))
9203 (logging (save-match-data (org-entry-get nil "LOGGING" t)))
9204 (org-log-done org-log-done)
9205 (org-log-repeat org-log-repeat)
9206 (org-todo-log-states org-todo-log-states)
9207 (this (match-string 1))
9208 (hl-pos (match-beginning 0))
9209 (head (org-get-todo-sequence-head this))
9210 (ass (assoc head org-todo-kwd-alist))
9211 (interpret (nth 1 ass))
9212 (done-word (nth 3 ass))
9213 (final-done-word (nth 4 ass))
9214 (last-state (or this ""))
9215 (completion-ignore-case t)
9216 (member (member this org-todo-keywords-1))
9219 ((and org-todo-key-trigger
9220 (or (and (equal arg '(4))
9221 (eq org-use-fast-todo-selection 'prefix))
9222 (and (not arg) org-use-fast-todo-selection
9223 (not (eq org-use-fast-todo-selection
9225 ;; Use fast selection
9226 (org-fast-todo-selection))
9227 ((and (equal arg '(4))
9228 (or (not org-use-fast-todo-selection)
9229 (not org-todo-key-trigger)))
9230 ;; Read a state with completion
9231 (org-ido-completing-read
9232 "State: " (mapcar (lambda(x) (list x))
9233 org-todo-keywords-1)
9237 (if tail (car tail) nil)
9238 (car org-todo-keywords-1)))
9240 (if (equal member org-todo-keywords-1)
9243 (nth (- (length org-todo-keywords-1)
9245 org-todo-keywords-1)
9246 (org-last org-todo-keywords-1))))
9247 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
9248 (setq arg nil))) ; hack to fall back to cycling
9250 ;; user or caller requests a specific state
9252 ((equal arg "") nil)
9253 ((eq arg 'none) nil)
9254 ((eq arg 'done) (or done-word (car org-done-keywords)))
9256 (or (car (cdr (member head org-todo-heads)))
9257 (car org-todo-heads)))
9258 ((eq arg 'previousset)
9259 (let ((org-todo-heads (reverse org-todo-heads)))
9260 (or (car (cdr (member head org-todo-heads)))
9261 (car org-todo-heads))))
9262 ((car (member arg org-todo-keywords-1)))
9263 ((nth (1- (prefix-numeric-value arg))
9264 org-todo-keywords-1))))
9265 ((null member) (or head (car org-todo-keywords-1)))
9266 ((equal this final-done-word) nil) ;; -> make empty
9267 ((null tail) nil) ;; -> first entry
9268 ((memq interpret '(type priority))
9269 (if (eq this-command last-command)
9271 (if (> (length tail) 0)
9272 (or done-word (car org-done-keywords))
9277 (run-hook-with-args-until-success
9278 'org-todo-get-default-hook state last-state)
9280 (next (if state (concat " " state " ") " "))
9281 (change-plist (list :type 'todo-state-change :from this :to state
9282 :position startpos))
9284 (when org-blocker-hook
9285 (setq org-last-todo-state-is-todo
9286 (not (member this org-done-keywords)))
9287 (unless (save-excursion
9289 (run-hook-with-args-until-failure
9290 'org-blocker-hook change-plist)))
9292 (error "TODO state change from %s to %s blocked" this state)
9294 (message "TODO state change from %s to %s blocked" this state)
9295 (throw 'exit nil))))
9296 (store-match-data match-data)
9297 (replace-match next t t)
9298 (unless (pos-visible-in-window-p hl-pos)
9299 (message "TODO state changed to %s" (org-trim next)))
9301 (setq head (org-get-todo-sequence-head state)
9302 ass (assoc head org-todo-kwd-alist)
9303 interpret (nth 1 ass)
9304 done-word (nth 3 ass)
9305 final-done-word (nth 4 ass)))
9306 (when (memq arg '(nextset previousset))
9307 (message "Keyword-Set %d/%d: %s"
9308 (- (length org-todo-sets) -1
9309 (length (memq (assoc state org-todo-sets) org-todo-sets)))
9310 (length org-todo-sets)
9311 (mapconcat 'identity (assoc state org-todo-sets) " ")))
9312 (setq org-last-todo-state-is-todo
9313 (not (member state org-done-keywords)))
9314 (setq now-done-p (and (member state org-done-keywords)
9315 (not (member this org-done-keywords))))
9316 (and logging (org-local-logging logging))
9317 (when (and (or org-todo-log-states org-log-done)
9318 (not (eq org-inhibit-logging t))
9319 (not (memq arg '(nextset previousset))))
9320 ;; we need to look at recording a time and note
9321 (setq dolog (or (nth 1 (assoc state org-todo-log-states))
9322 (nth 2 (assoc this org-todo-log-states))))
9323 (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
9326 (member state org-not-done-keywords)
9327 (not (member this org-not-done-keywords)))
9328 ;; This is now a todo state and was not one before
9329 ;; If there was a CLOSED time stamp, get rid of it.
9330 (org-add-planning-info nil nil 'closed))
9331 (when (and now-done-p org-log-done)
9332 ;; It is now done, and it was not done before
9333 (org-add-planning-info 'closed (org-current-time))
9334 (if (and (not dolog) (eq 'note org-log-done))
9335 (org-add-log-setup 'done state this 'findpos 'note)))
9336 (when (and state dolog)
9337 ;; This is a non-nil state, and we need to log it
9338 (org-add-log-setup 'state state this 'findpos dolog)))
9339 ;; Fixup tag positioning
9340 (org-todo-trigger-tag-changes state)
9341 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
9342 (when org-provide-todo-statistics
9343 (org-update-parent-todo-statistics))
9344 (run-hooks 'org-after-todo-state-change-hook)
9345 (if (and arg (not (member state org-done-keywords)))
9346 (setq head (org-get-todo-sequence-head state)))
9347 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
9348 ;; Do we need to trigger a repeat?
9350 (when (boundp 'org-agenda-headline-snapshot-before-repeat)
9351 ;; This is for the agenda, take a snapshot of the headline.
9353 (setq org-agenda-headline-snapshot-before-repeat
9354 (org-get-heading))))
9355 (org-auto-repeat-maybe state))
9356 ;; Fixup cursor location if close to the keyword
9357 (if (and (outline-on-heading-p)
9359 (save-excursion (beginning-of-line 1)
9360 (looking-at org-todo-line-regexp))
9361 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
9363 (goto-char (or (match-end 2) (match-end 1)))
9364 (and (looking-at " ") (just-one-space))))
9365 (when org-trigger-hook
9367 (run-hook-with-args 'org-trigger-hook change-plist))))))))
9369 (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
9370 "Block turning an entry into a TODO, using the hierarchy.
9371 This checks whether the current task should be blocked from state
9372 changes. Such blocking occurs when:
9374 1. The task has children which are not all in a completed state.
9376 2. A task has a parent with the property :ORDERED:, and there
9377 are siblings prior to the current task with incomplete
9380 3. The parent of the task is blocked because it has siblings that should
9381 be done first, or is child of a block grandparent TODO entry."
9384 ;; If this is not a todo state change, or if this entry is already DONE,
9386 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
9387 (member (plist-get change-plist :from)
9388 (cons 'done org-done-keywords))
9389 (member (plist-get change-plist :to)
9390 (cons 'todo org-not-done-keywords)))
9391 (throw 'dont-block t))
9392 ;; If this task has children, and any are undone, it's blocked
9394 (org-back-to-heading t)
9395 (let ((this-level (funcall outline-level)))
9396 (outline-next-heading)
9397 (let ((child-level (funcall outline-level)))
9398 (while (and (not (eobp))
9399 (> child-level this-level))
9400 ;; this todo has children, check whether they are all
9402 (if (and (not (org-entry-is-done-p))
9403 (org-entry-is-todo-p))
9404 (throw 'dont-block nil))
9405 (outline-next-heading)
9406 (setq child-level (funcall outline-level))))))
9407 ;; Otherwise, if the task's parent has the :ORDERED: property, and
9408 ;; any previous siblings are undone, it's blocked
9410 (org-back-to-heading t)
9411 (let* ((pos (point))
9412 (parent-pos (and (org-up-heading-safe) (point))))
9413 (if (not parent-pos) (throw 'dont-block t)) ; no parent
9414 (when (and (org-entry-get (point) "ORDERED")
9416 (re-search-forward org-not-done-heading-regexp pos t))
9417 (throw 'dont-block nil)) ; block, there is an older sibling not done.
9418 ;; Search further up the hierarchy, to see if an anchestor is blocked
9420 (goto-char parent-pos)
9421 (if (not (looking-at org-not-done-heading-regexp))
9422 (throw 'dont-block t)) ; do not block, parent is not a TODO
9424 (setq parent-pos (and (org-up-heading-safe) (point)))
9425 (if (not parent-pos) (throw 'dont-block t)) ; no parent
9426 (when (and (org-entry-get (point) "ORDERED")
9428 (re-search-forward org-not-done-heading-regexp pos t))
9429 (throw 'dont-block nil))))))) ; block, older sibling not done.
9431 (defcustom org-track-ordered-property-with-tag nil
9432 "Should the ORDERED property also be shown as a tag?
9433 The ORDERED property decides if an entry should require subtasks to be
9434 completed in sequence. Since a property is not very visible, setting
9435 this option means that toggling the ORDERED property with the command
9436 `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
9437 not relevant for the behavior, but it makes things more visible.
9439 Note that toggling the tag with tags commands will not change the property
9440 and therefore not influence behavior!
9442 This can be t, meaning the tag ORDERED should be used, It can also be a
9443 string to select a different tag for this task."
9446 (const :tag "No tracking" nil)
9447 (const :tag "Track with ORDERED tag" t)
9448 (string :tag "Use other tag")))
9450 (defun org-toggle-ordered-property ()
9451 "Toggle the ORDERED property of the current entry.
9452 For better visibility, you can track the value of this property with a tag.
9453 See variable `org-track-ordered-property-with-tag'."
9455 (let* ((t1 org-track-ordered-property-with-tag)
9456 (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
9458 (org-back-to-heading)
9459 (if (org-entry-get nil "ORDERED")
9461 (org-delete-property "ORDERED")
9462 (and tag (org-toggle-tag tag 'off))
9463 (message "Subtasks can be completed in arbitrary order"))
9464 (org-entry-put nil "ORDERED" "t")
9465 (and tag (org-toggle-tag tag 'on))
9466 (message "Subtasks must be completed in sequence")))))
9468 (defvar org-blocked-by-checkboxes) ; dynamically scoped
9469 (defun org-block-todo-from-checkboxes (change-plist)
9470 "Block turning an entry into a TODO, using checkboxes.
9471 This checks whether the current task should be blocked from state
9472 changes because there are uncheckd boxes in this entry."
9474 ;; If this is not a todo state change, or if this entry is already DONE,
9476 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
9477 (member (plist-get change-plist :from)
9478 (cons 'done org-done-keywords))
9479 (member (plist-get change-plist :to)
9480 (cons 'todo org-not-done-keywords)))
9481 (throw 'dont-block t))
9482 ;; If this task has checkboxes that are not checked, it's blocked
9484 (org-back-to-heading t)
9485 (let ((beg (point)) end)
9486 (outline-next-heading)
9489 (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
9492 (if (boundp 'org-blocked-by-checkboxes)
9493 (setq org-blocked-by-checkboxes t))
9494 (throw 'dont-block nil)))))
9497 (defvar org-entry-property-inherited-from) ;; defined below
9498 (defun org-update-parent-todo-statistics ()
9499 "Update any statistics cookie in the parent of the current headline.
9500 When `org-hierarchical-todo-statistics' is nil, statistics will cover
9501 the entire subtree and this will travel up the hierarchy and update
9502 statistics everywhere."
9505 (recursive (or (not org-hierarchical-todo-statistics)
9508 (or (setq prop (org-entry-get
9509 nil "COOKIE_DATA" 'inherit)) ""))))
9510 (lim (or (and prop (marker-position
9511 org-entry-property-inherited-from))
9514 (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
9516 (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
9519 (beginning-of-line 1)
9520 (if (org-at-heading-p)
9521 (setq ltoggle (funcall outline-level))
9522 (error "This should not happen"))
9523 (while (and (setq level (org-up-heading-safe))
9524 (or recursive first)
9535 (while (re-search-forward box-re (point-at-eol) t)
9536 (setq cnt-all 0 cnt-done 0 cookie-present t)
9537 (setq is-percent (match-end 2))
9539 (unless (outline-next-heading) (throw 'exit nil))
9540 (while (and (looking-at org-complex-heading-regexp)
9541 (> (setq l1 (length (match-string 1))) level))
9542 (setq kwd (and (or recursive (= l1 ltoggle))
9544 (if (or (eq org-provide-todo-statistics 'all-headlines)
9545 (and (listp org-provide-todo-statistics)
9546 (or (member kwd org-provide-todo-statistics)
9547 (member kwd org-done-keywords))))
9548 (setq cnt-all (1+ cnt-all))
9549 (if (eq org-provide-todo-statistics t)
9550 (and kwd (setq cnt-all (1+ cnt-all)))))
9551 (and (member kwd org-done-keywords)
9552 (setq cnt-done (1+ cnt-done)))
9553 (outline-next-heading)))
9556 (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
9557 (format "[%d/%d]" cnt-done cnt-all)))))
9558 (when cookie-present
9559 (run-hook-with-args 'org-after-todo-statistics-hook
9560 cnt-done (- cnt-all cnt-done)))))
9561 (run-hooks 'org-todo-statistics-hook)))
9563 (defvar org-after-todo-statistics-hook nil
9564 "Hook that is called after a TODO statistics cookie has been updated.
9565 Each function is called with two arguments: the number of not-done entries
9566 and the number of done entries.
9568 For example, the following function, when added to this hook, will switch
9569 an entry to DONE when all children are done, and back to TODO when new
9570 entries are set to a TODO status. Note that this hook is only called
9571 when there is a statistics cookie in the headline!
9573 (defun org-summary-todo (n-done n-not-done)
9574 \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
9575 (let (org-log-done org-log-states) ; turn off logging
9576 (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
9579 (defvar org-todo-statistics-hook nil
9580 "Hook that is run whenever Org thinks TODO statistics should be updated.
9581 This hook runs even if there is no statisics cookie present, in which case
9582 `org-after-todo-statistics-hook' would not run.")
9584 (defun org-todo-trigger-tag-changes (state)
9585 "Apply the changes defined in `org-todo-state-tags-triggers'."
9586 (let ((l org-todo-state-tags-triggers)
9588 (when (or (not state) (equal state ""))
9589 (setq changes (append changes (cdr (assoc "" l)))))
9590 (when (and (stringp state) (> (length state) 0))
9591 (setq changes (append changes (cdr (assoc state l)))))
9592 (when (member state org-not-done-keywords)
9593 (setq changes (append changes (cdr (assoc 'todo l)))))
9594 (when (member state org-done-keywords)
9595 (setq changes (append changes (cdr (assoc 'done l)))))
9597 (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
9599 (defun org-local-logging (value)
9600 "Get logging settings from a property VALUE."
9602 ;; directly set the variables, they are already local.
9603 (setq org-log-done nil
9605 org-todo-log-states nil)
9606 (setq words (org-split-string value))
9607 (while (setq w (pop words))
9609 ((setq a (assoc w org-startup-options))
9610 (and (member (nth 1 a) '(org-log-done org-log-repeat))
9611 (set (nth 1 a) (nth 2 a))))
9612 ((setq a (org-extract-log-state-settings w))
9613 (and (member (car a) org-todo-keywords-1)
9614 (push a org-todo-log-states)))))))
9616 (defun org-get-todo-sequence-head (kwd)
9617 "Return the head of the TODO sequence to which KWD belongs.
9618 If KWD is not set, check if there is a text property remembering the
9623 (or (get-text-property (point-at-bol) 'org-todo-head)
9625 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
9626 nil (point-at-eol)))
9627 (get-text-property p 'org-todo-head))))
9628 ((not (member kwd org-todo-keywords-1))
9629 (car org-todo-keywords-1))
9630 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
9632 (defun org-fast-todo-selection ()
9633 "Fast TODO keyword selection with single keys.
9634 Returns the new TODO keyword, or nil if no state change should occur."
9635 (let* ((fulltable org-todo-key-alist)
9636 (done-keywords org-done-keywords) ;; needed for the faces.
9637 (maxlen (apply 'max (mapcar
9639 (if (stringp (car x)) (string-width (car x)) 0))
9642 (fwidth (+ maxlen 3 1 3))
9643 (ncol (/ (- (window-width) 4) fwidth))
9647 (save-window-excursion
9649 (set-buffer (get-buffer-create " *Org todo*"))
9650 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
9652 (org-set-local 'org-done-keywords done-keywords)
9653 (setq tbl fulltable cnt 0)
9654 (while (setq e (pop tbl))
9656 ((equal e '(:startgroup))
9657 (push '() groups) (setq ingroup t)
9658 (when (not (= cnt 0))
9662 ((equal e '(:endgroup))
9663 (setq ingroup nil cnt 0)
9665 ((equal e '(:newline))
9666 (when (not (= cnt 0))
9670 (while (equal (car tbl) '(:newline))
9672 (setq tbl (cdr tbl)))))
9674 (setq tg (car e) c (cdr e))
9675 (if ingroup (push tg (car groups)))
9676 (setq tg (org-add-props tg nil 'face
9677 (org-get-todo-face tg)))
9678 (if (and (= cnt 0) (not ingroup)) (insert " "))
9679 (insert "[" c "] " tg (make-string
9680 (- fwidth 4 (length tg)) ?\ ))
9681 (when (= (setq cnt (1+ cnt)) ncol)
9683 (if ingroup (insert " "))
9686 (goto-char (point-min))
9687 (if (not expert) (org-fit-window-to-buffer))
9688 (message "[a-z..]:Set [SPC]:clear")
9689 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
9692 (and (= c ?q) (not (rassoc c fulltable))))
9695 ((setq e (rassoc c fulltable) tg (car e))
9697 (t (setq quit-flag t)))))))
9699 (defun org-entry-is-todo-p ()
9700 (member (org-get-todo-state) org-not-done-keywords))
9702 (defun org-entry-is-done-p ()
9703 (member (org-get-todo-state) org-done-keywords))
9705 (defun org-get-todo-state ()
9707 (org-back-to-heading t)
9708 (and (looking-at org-todo-line-regexp)
9712 (defun org-at-date-range-p (&optional inactive-ok)
9713 "Is the cursor inside a date range?"
9717 (let ((pos (point)))
9718 (skip-chars-backward "^[<\r\n")
9719 (skip-chars-backward "<[")
9720 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
9721 (>= (match-end 0) pos)
9723 (skip-chars-backward "^<[\r\n")
9724 (skip-chars-backward "<[")
9725 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
9726 (>= (match-end 0) pos)
9730 (defun org-get-repeat ()
9731 "Check if there is a deadline/schedule with repeater in this entry."
9734 (org-back-to-heading t)
9735 (if (re-search-forward
9736 org-repeat-re (save-excursion (outline-next-heading) (point)) t)
9737 (match-string 1)))))
9739 (defvar org-last-changed-timestamp)
9740 (defvar org-last-inserted-timestamp)
9741 (defvar org-log-post-message)
9742 (defvar org-log-note-purpose)
9743 (defvar org-log-note-how)
9744 (defvar org-log-note-extra)
9745 (defun org-auto-repeat-maybe (done-word)
9746 "Check if the current headline contains a repeated deadline/schedule.
9747 If yes, set TODO state back to what it was and change the base date
9748 of repeating deadline/scheduled time stamps to new date.
9749 This function is run automatically after each state change to a DONE state."
9750 ;; last-state is dynamically scoped into this function
9751 (let* ((repeat (org-get-repeat))
9752 (aa (assoc last-state org-todo-kwd-alist))
9753 (interpret (nth 1 aa))
9755 (whata '(("d" . day) ("m" . month) ("y" . year)))
9756 (msg "Entry repeats: ")
9758 (org-todo-log-states nil)
9759 (nshiftmax 10) (nshift 0)
9760 re type n what ts time)
9762 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
9763 (org-todo (if (eq interpret 'type) last-state head))
9764 (org-entry-put nil "LAST_REPEAT" (format-time-string
9765 (org-time-stamp-format t t)))
9766 (when org-log-repeat
9767 (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
9768 (memq 'org-add-log-note post-command-hook))
9769 ;; OK, we are already setup for some record
9770 (if (eq org-log-repeat 'note)
9771 ;; make sure we take a note, not only a time stamp
9772 (setq org-log-note-how 'note))
9773 ;; Set up for taking a record
9774 (org-add-log-setup 'state (or done-word (car org-done-keywords))
9776 'findpos org-log-repeat)))
9777 (org-back-to-heading t)
9778 (org-add-planning-info nil nil 'closed)
9779 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
9780 org-deadline-time-regexp "\\)\\|\\("
9781 org-ts-regexp "\\)"))
9782 (while (re-search-forward
9783 re (save-excursion (outline-next-heading) (point)) t)
9784 (setq type (if (match-end 1) org-scheduled-string
9785 (if (match-end 3) org-deadline-string "Plain:"))
9786 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
9787 (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
9788 (setq n (string-to-number (match-string 2 ts))
9789 what (match-string 3 ts))
9790 (if (equal what "w") (setq n (* n 7) what "d"))
9791 ;; Preparation, see if we need to modify the start date for the change
9793 (setq time (save-match-data (org-time-string-to-time ts)))
9795 ((equal (match-string 1 ts) ".")
9796 ;; Shift starting date to today
9797 (org-timestamp-change
9798 (- (time-to-days (current-time)) (time-to-days time))
9800 ((equal (match-string 1 ts) "+")
9801 (while (or (= nshift 0)
9802 (<= (time-to-days time) (time-to-days (current-time))))
9803 (when (= (incf nshift) nshiftmax)
9804 (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
9806 (org-timestamp-change n (cdr (assoc what whata)))
9807 (org-at-timestamp-p t)
9808 (setq ts (match-string 1))
9809 (setq time (save-match-data (org-time-string-to-time ts))))
9810 (org-timestamp-change (- n) (cdr (assoc what whata)))
9811 ;; rematch, so that we have everything in place for the real shift
9812 (org-at-timestamp-p t)
9813 (setq ts (match-string 1))
9814 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
9815 (org-timestamp-change n (cdr (assoc what whata)))
9816 (setq msg (concat msg type " " org-last-changed-timestamp " "))))
9817 (setq org-log-post-message msg)
9818 (message "%s" msg))))
9820 (defun org-show-todo-tree (arg)
9821 "Make a compact tree which shows all headlines marked with TODO.
9822 The tree will show the lines where the regexp matches, and all higher
9823 headlines above the match.
9824 With a \\[universal-argument] prefix, prompt for a regexp to match.
9825 With a numeric prefix N, construct a sparse tree for the Nth element
9826 of `org-todo-keywords-1'."
9828 (let ((case-fold-search nil)
9830 (cond ((null arg) org-not-done-regexp)
9832 (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
9833 (mapcar 'list org-todo-keywords-1))))
9835 (mapconcat 'identity (org-split-string kwd "|") "\\|")
9837 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
9838 (regexp-quote (nth (1- (prefix-numeric-value arg))
9839 org-todo-keywords-1)))
9840 (t (error "Invalid prefix argument: %s" arg)))))
9841 (message "%d TODO entries found"
9842 (org-occur (concat "^" outline-regexp " *" kwd-re )))))
9844 (defun org-deadline (&optional remove time)
9845 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
9846 With argument REMOVE, remove any deadline from the item.
9847 When TIME is set, it should be an internal time specification, and the
9848 scheduling will use the corresponding date."
9852 (org-remove-timestamp-with-keyword org-deadline-string)
9853 (message "Item no longer has a deadline."))
9854 (if (org-get-repeat)
9855 (error "Cannot change deadline on task with repeater, please do that by hand")
9856 (org-add-planning-info 'deadline time 'closed)
9857 (message "Deadline on %s" org-last-inserted-timestamp))))
9859 (defun org-schedule (&optional remove time)
9860 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
9861 With argument REMOVE, remove any scheduling date from the item.
9862 When TIME is set, it should be an internal time specification, and the
9863 scheduling will use the corresponding date."
9867 (org-remove-timestamp-with-keyword org-scheduled-string)
9868 (message "Item is no longer scheduled."))
9869 (if (org-get-repeat)
9870 (error "Cannot reschedule task with repeater, please do that by hand")
9871 (org-add-planning-info 'scheduled time 'closed)
9872 (message "Scheduled to %s" org-last-inserted-timestamp))))
9874 (defun org-get-scheduled-time (pom &optional inherit)
9875 "Get the scheduled time as a time tuple, of a format suitable
9876 for calling org-schedule with, or if there is no scheduling,
9878 (let ((time (org-entry-get pom "SCHEDULED" inherit)))
9880 (apply 'encode-time (org-parse-time-string time)))))
9882 (defun org-get-deadline-time (pom &optional inherit)
9883 "Get the deadine as a time tuple, of a format suitable for
9884 calling org-deadlin with, or if there is no scheduling, returns
9886 (let ((time (org-entry-get pom "DEADLINE" inherit)))
9888 (apply 'encode-time (org-parse-time-string time)))))
9890 (defun org-remove-timestamp-with-keyword (keyword)
9891 "Remove all time stamps with KEYWORD in the current entry."
9892 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
9895 (org-back-to-heading t)
9897 (org-end-of-subtree t t)
9898 (while (re-search-backward re beg t)
9900 (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
9901 (equal (char-before) ?\ ))
9902 (backward-delete-char 1)
9903 (if (string-match "^[ \t]*$" (buffer-substring
9904 (point-at-bol) (point-at-eol)))
9905 (delete-region (point-at-bol)
9906 (min (point-max) (1+ (point-at-eol))))))))))
9908 (defun org-add-planning-info (what &optional time &rest remove)
9909 "Insert new timestamp with keyword in the line directly after the headline.
9910 WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
9911 If non is given, the user is prompted for a date.
9912 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
9915 (let (org-time-was-given org-end-time-was-given ts
9916 end default-time default-input)
9919 (when (and (not time) (memq what '(scheduled deadline)))
9920 ;; Try to get a default date/time from existing timestamp
9922 (org-back-to-heading t)
9923 (setq end (save-excursion (outline-next-heading) (point)))
9924 (when (re-search-forward (if (eq what 'scheduled)
9925 org-scheduled-time-regexp
9926 org-deadline-time-regexp)
9928 (setq ts (match-string 1)
9930 (apply 'encode-time (org-parse-time-string ts))
9931 default-input (and ts (org-get-compact-tod ts))))))
9933 ;; If necessary, get the time from the user
9934 (setq time (or time (org-read-date nil 'to-time nil nil
9935 default-time default-input))))
9937 (when (and org-insert-labeled-timestamps-at-point
9938 (member what '(scheduled deadline)))
9940 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
9941 (org-insert-time-stamp time org-time-was-given
9942 nil nil nil (list org-end-time-was-given))
9946 (let (col list elt ts buffer-invisibility-spec)
9947 (org-back-to-heading t)
9948 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
9949 (goto-char (match-end 1))
9950 (setq col (current-column))
9951 (goto-char (match-end 0))
9952 (if (eobp) (insert "\n") (forward-char 1))
9953 (when (and (not what)
9956 org-keyword-time-not-clock-regexp))))
9957 ;; Nothing to add, nothing to remove...... :-)
9959 (if (and (not (looking-at outline-regexp))
9960 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
9962 (not (equal (match-string 1) org-clock-string)))
9963 (narrow-to-region (match-beginning 0) (match-end 0))
9964 (insert-before-markers "\n")
9966 (narrow-to-region (point) (point))
9967 (and org-adapt-indentation (org-indent-to-column col)))
9968 ;; Check if we have to remove something.
9969 (setq list (cons what remove))
9971 (setq elt (pop list))
9972 (goto-char (point-min))
9973 (when (or (and (eq elt 'scheduled)
9974 (re-search-forward org-scheduled-time-regexp nil t))
9975 (and (eq elt 'deadline)
9976 (re-search-forward org-deadline-time-regexp nil t))
9977 (and (eq elt 'closed)
9978 (re-search-forward org-closed-time-regexp nil t)))
9980 (if (looking-at "--+<[^>]+>") (replace-match ""))
9981 (if (looking-at " +") (replace-match ""))))
9982 (goto-char (point-max))
9985 (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
9986 (cond ((eq what 'scheduled) org-scheduled-string)
9987 ((eq what 'deadline) org-deadline-string)
9988 ((eq what 'closed) org-closed-string))
9990 (setq ts (org-insert-time-stamp
9992 (or org-time-was-given
9993 (and (eq what 'closed) org-log-done-with-time))
9995 nil nil (list org-end-time-was-given)))
9997 (goto-char (point-min))
9999 (if (and (looking-at "[ \t]+\n")
10000 (equal (char-before) ?\n))
10001 (delete-region (1- (point)) (point-at-eol)))
10004 (defvar org-log-note-marker (make-marker))
10005 (defvar org-log-note-purpose nil)
10006 (defvar org-log-note-state nil)
10007 (defvar org-log-note-previous-state nil)
10008 (defvar org-log-note-how nil)
10009 (defvar org-log-note-extra nil)
10010 (defvar org-log-note-window-configuration nil)
10011 (defvar org-log-note-return-to (make-marker))
10012 (defvar org-log-post-message nil
10013 "Message to be displayed after a log note has been stored.
10014 The auto-repeater uses this.")
10016 (defun org-add-note ()
10017 "Add a note to the current entry.
10018 This is done in the same way as adding a state change note."
10020 (org-add-log-setup 'note nil nil 'findpos nil))
10022 (defvar org-property-end-re)
10023 (defun org-add-log-setup (&optional purpose state prev-state
10024 findpos how &optional extra)
10025 "Set up the post command hook to take a note.
10026 If this is about to TODO state change, the new state is expected in STATE.
10027 When FINDPOS is non-nil, find the correct position for the note in
10028 the current entry. If not, assume that it can be inserted at point.
10029 HOW is an indicator what kind of note should be created.
10030 EXTRA is additional text that will be inserted into the notes buffer."
10031 (let* ((org-log-into-drawer (org-log-into-drawer))
10032 (drawer (cond ((stringp org-log-into-drawer)
10033 org-log-into-drawer)
10034 (org-log-into-drawer "LOGBOOK")
10039 (org-back-to-heading t)
10040 (narrow-to-region (point) (save-excursion
10041 (outline-next-heading) (point)))
10042 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
10043 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
10045 (goto-char (match-end 0))
10048 (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
10051 (goto-char (match-end 0))
10052 (or org-log-states-order-reversed
10053 (and (re-search-forward org-property-end-re nil t)
10054 (goto-char (1- (match-beginning 0))))))
10055 (insert "\n:" drawer ":\n:END:")
10056 (beginning-of-line 0)
10057 (org-indent-line-function)
10058 (beginning-of-line 2)
10059 (org-indent-line-function)
10061 ((and org-log-state-notes-insert-after-drawers
10063 (forward-line) (looking-at org-drawer-regexp)))
10065 (while (looking-at org-drawer-regexp)
10066 (goto-char (match-end 0))
10067 (re-search-forward org-property-end-re (point-max) t)
10069 (forward-line -1)))
10070 (unless org-log-states-order-reversed
10071 (and (= (char-after) ?\n) (forward-char 1))
10072 (org-skip-over-state-notes)
10073 (skip-chars-backward " \t\n\r")))
10074 (move-marker org-log-note-marker (point))
10075 (setq org-log-note-purpose purpose
10076 org-log-note-state state
10077 org-log-note-previous-state prev-state
10078 org-log-note-how how
10079 org-log-note-extra extra)
10080 (add-hook 'post-command-hook 'org-add-log-note 'append)))))
10082 (defun org-skip-over-state-notes ()
10083 "Skip past the list of State notes in an entry."
10084 (if (looking-at "\n[ \t]*- State") (forward-char 1))
10085 (while (looking-at "[ \t]*- State")
10086 (condition-case nil
10088 (error (org-end-of-item)))))
10090 (defun org-add-log-note (&optional purpose)
10091 "Pop up a window for taking a note, and add this note later at point."
10092 (remove-hook 'post-command-hook 'org-add-log-note)
10093 (setq org-log-note-window-configuration (current-window-configuration))
10094 (delete-other-windows)
10095 (move-marker org-log-note-return-to (point))
10096 (switch-to-buffer (marker-buffer org-log-note-marker))
10097 (goto-char org-log-note-marker)
10098 (org-switch-to-buffer-other-window "*Org Note*")
10100 (if (memq org-log-note-how '(time state))
10101 (let (current-prefix-arg) (org-store-log-note))
10102 (let ((org-inhibit-startup t)) (org-mode))
10103 (insert (format "# Insert note for %s.
10104 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
10106 ((eq org-log-note-purpose 'clock-out) "stopped clock")
10107 ((eq org-log-note-purpose 'done) "closed todo item")
10108 ((eq org-log-note-purpose 'state)
10109 (format "state change from \"%s\" to \"%s\""
10110 (or org-log-note-previous-state "")
10111 (or org-log-note-state "")))
10112 ((eq org-log-note-purpose 'note)
10114 (t (error "This should not happen")))))
10115 (if org-log-note-extra (insert org-log-note-extra))
10116 (org-set-local 'org-finish-function 'org-store-log-note)))
10118 (defvar org-note-abort nil) ; dynamically scoped
10119 (defun org-store-log-note ()
10120 "Finish taking a log note, and insert it to where it belongs."
10121 (let ((txt (buffer-string))
10122 (note (cdr (assq org-log-note-purpose org-log-note-headings)))
10124 (kill-buffer (current-buffer))
10125 (while (string-match "\\`#.*\n[ \t\n]*" txt)
10126 (setq txt (replace-match "" t t txt)))
10127 (if (string-match "\\s-+\\'" txt)
10128 (setq txt (replace-match "" t t txt)))
10129 (setq lines (org-split-string txt "\n"))
10130 (when (and note (string-match "\\S-" note))
10132 (org-replace-escapes
10134 (list (cons "%u" (user-login-name))
10135 (cons "%U" user-full-name)
10136 (cons "%t" (format-time-string
10137 (org-time-stamp-format 'long 'inactive)
10139 (cons "%s" (if org-log-note-state
10140 (concat "\"" org-log-note-state "\"")
10142 (cons "%S" (if org-log-note-previous-state
10143 (concat "\"" org-log-note-previous-state "\"")
10145 (if lines (setq note (concat note " \\\\")))
10147 (when (or current-prefix-arg org-note-abort)
10148 (when org-log-into-drawer
10149 (org-remove-empty-drawer-at
10150 (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
10151 org-log-note-marker))
10155 (set-buffer (marker-buffer org-log-note-marker))
10157 (goto-char org-log-note-marker)
10158 (move-marker org-log-note-marker nil)
10160 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
10161 (insert "- " (pop lines))
10162 (org-indent-line-function)
10163 (beginning-of-line 1)
10164 (looking-at "[ \t]*")
10165 (setq ind (concat (match-string 0) " "))
10167 (while lines (insert "\n" ind (pop lines)))
10168 (message "Note stored")
10169 (org-back-to-heading t)
10170 (org-cycle-hide-drawers 'children)))))
10171 (set-window-configuration org-log-note-window-configuration)
10172 (with-current-buffer (marker-buffer org-log-note-return-to)
10173 (goto-char org-log-note-return-to))
10174 (move-marker org-log-note-return-to nil)
10175 (and org-log-post-message (message "%s" org-log-post-message)))
10177 (defun org-remove-empty-drawer-at (drawer pos)
10178 "Remove an emptyr DARWER drawer at position POS.
10179 POS may also be a marker."
10180 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
10186 (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
10187 (replace-match ""))))))
10189 (defun org-sparse-tree (&optional arg)
10190 "Create a sparse tree, prompt for the details.
10191 This command can create sparse trees. You first need to select the type
10192 of match used to create the tree:
10194 t Show entries with a specific TODO keyword.
10195 m Show entries selected by a tags/property match.
10196 p Enter a property name and its value (both with completion on existing
10197 names/values) and show entries with that property.
10198 r Show entries matching a regular expression.
10199 d Show deadlines due within `org-deadline-warning-days'.
10200 b Show deadlines and scheduled items before a date.
10201 a Show deadlines and scheduled items after a date."
10203 (let (ans kwd value)
10204 (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
10205 (setq ans (read-char-exclusive))
10208 (call-interactively 'org-check-deadlines))
10210 (call-interactively 'org-check-before-date))
10212 (call-interactively 'org-check-after-date))
10214 (org-show-todo-tree '(4)))
10215 ((member ans '(?T ?m))
10216 (call-interactively 'org-match-sparse-tree))
10217 ((member ans '(?p ?P))
10218 (setq kwd (org-ido-completing-read "Property: "
10219 (mapcar 'list (org-buffer-property-keys))))
10220 (setq value (org-ido-completing-read "Value: "
10221 (mapcar 'list (org-property-values kwd))))
10222 (unless (string-match "\\`{.*}\\'" value)
10223 (setq value (concat "\"" value "\"")))
10224 (org-match-sparse-tree arg (concat kwd "=" value)))
10225 ((member ans '(?r ?R ?/))
10226 (call-interactively 'org-occur))
10227 (t (error "No such sparse tree command \"%c\"" ans)))))
10229 (defvar org-occur-highlights nil
10230 "List of overlays used for occur matches.")
10231 (make-variable-buffer-local 'org-occur-highlights)
10232 (defvar org-occur-parameters nil
10233 "Parameters of the active org-occur calls.
10234 This is a list, each call to org-occur pushes as cons cell,
10235 containing the regular expression and the callback, onto the list.
10236 The list can contain several entries if `org-occur' has been called
10237 several time with the KEEP-PREVIOUS argument. Otherwise, this list
10238 will only contain one set of parameters. When the highlights are
10239 removed (for example with `C-c C-c', or with the next edit (depending
10240 on `org-remove-highlights-with-change'), this variable is emptied
10242 (make-variable-buffer-local 'org-occur-parameters)
10244 (defun org-occur (regexp &optional keep-previous callback)
10245 "Make a compact tree which shows all matches of REGEXP.
10246 The tree will show the lines where the regexp matches, and all higher
10247 headlines above the match. It will also show the heading after the match,
10248 to make sure editing the matching entry is easy.
10249 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
10250 call to `org-occur' will be kept, to allow stacking of calls to this
10252 If CALLBACK is non-nil, it is a function which is called to confirm
10253 that the match should indeed be shown."
10254 (interactive "sRegexp: \nP")
10255 (when (equal regexp "")
10256 (error "Regexp cannot be empty"))
10257 (unless keep-previous
10258 (org-remove-occur-highlights nil nil t))
10259 (push (cons regexp callback) org-occur-parameters)
10262 (goto-char (point-min))
10263 (if (or (not keep-previous) ; do not want to keep
10264 (not org-occur-highlights)) ; no previous matches
10267 (while (re-search-forward regexp nil t)
10268 (when (or (not callback)
10269 (save-match-data (funcall callback)))
10270 (setq cnt (1+ cnt))
10271 (when org-highlight-sparse-tree-matches
10272 (org-highlight-new-match (match-beginning 0) (match-end 0)))
10273 (org-show-context 'occur-tree))))
10274 (when org-remove-highlights-with-change
10275 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
10277 (unless org-sparse-tree-open-archived-trees
10278 (org-hide-archived-subtrees (point-min) (point-max)))
10279 (run-hooks 'org-occur-hook)
10280 (if (interactive-p)
10281 (message "%d match(es) for regexp %s" cnt regexp))
10284 (defun org-show-context (&optional key)
10285 "Make sure point and context and visible.
10286 How much context is shown depends upon the variables
10287 `org-show-hierarchy-above', `org-show-following-heading'. and
10288 `org-show-siblings'."
10289 (let ((heading-p (org-on-heading-p t))
10290 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
10291 (following-p (org-get-alist-option org-show-following-heading key))
10292 (entry-p (org-get-alist-option org-show-entry-below key))
10293 (siblings-p (org-get-alist-option org-show-siblings key)))
10295 ;; Show heading or entry text
10296 (if (and heading-p (not entry-p))
10297 (org-flag-heading nil) ; only show the heading
10298 (and (or entry-p (org-invisible-p) (org-invisible-p2))
10299 (org-show-hidden-entry))) ; show entire entry
10301 ;; Show next sibling, or heading below text
10303 (and (if heading-p (org-goto-sibling) (outline-next-heading))
10304 (org-flag-heading nil))))
10305 (when siblings-p (org-show-siblings))
10307 ;; show all higher headings, possibly with siblings
10309 (while (and (condition-case nil
10310 (progn (org-up-heading-all 1) t)
10313 (org-flag-heading nil)
10314 (when siblings-p (org-show-siblings))))))))
10316 (defun org-reveal (&optional siblings)
10317 "Show current entry, hierarchy above it, and the following headline.
10318 This can be used to show a consistent set of context around locations
10319 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
10320 not t for the search context.
10322 With optional argument SIBLINGS, on each level of the hierarchy all
10323 siblings are shown. This repairs the tree structure to what it would
10324 look like when opened with hierarchical calls to `org-cycle'."
10326 (let ((org-show-hierarchy-above t)
10327 (org-show-following-heading t)
10328 (org-show-siblings (if siblings t org-show-siblings)))
10329 (org-show-context nil)))
10331 (defun org-highlight-new-match (beg end)
10332 "Highlight from BEG to END and mark the highlight is an occur headline."
10333 (let ((ov (org-make-overlay beg end)))
10334 (org-overlay-put ov 'face 'secondary-selection)
10335 (push ov org-occur-highlights)))
10337 (defun org-remove-occur-highlights (&optional beg end noremove)
10338 "Remove the occur highlights from the buffer.
10339 BEG and END are ignored. If NOREMOVE is nil, remove this function
10340 from the `before-change-functions' in the current buffer."
10342 (unless org-inhibit-highlight-removal
10343 (mapc 'org-delete-overlay org-occur-highlights)
10344 (setq org-occur-highlights nil)
10345 (setq org-occur-parameters nil)
10347 (remove-hook 'before-change-functions
10348 'org-remove-occur-highlights 'local))))
10352 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
10353 "Regular expression matching the priority indicator.")
10355 (defvar org-remove-priority-next-time nil)
10357 (defun org-priority-up ()
10358 "Increase the priority of the current item."
10360 (org-priority 'up))
10362 (defun org-priority-down ()
10363 "Decrease the priority of the current item."
10365 (org-priority 'down))
10367 (defun org-priority (&optional action)
10368 "Change the priority of an item by ARG.
10369 ACTION can be `set', `up', `down', or a character."
10371 (unless org-enable-priority-commands
10372 (error "Priority commands are disabled"))
10373 (setq action (or action 'set))
10374 (let (current new news have remove)
10376 (org-back-to-heading t)
10377 (if (looking-at org-priority-regexp)
10378 (setq current (string-to-char (match-string 2))
10380 (setq current org-default-priority))
10382 ((or (eq action 'set)
10383 (if (featurep 'xemacs) (characterp action) (integerp action)))
10384 (if (not (eq action 'set))
10386 (message "Priority %c-%c, SPC to remove: "
10387 org-highest-priority org-lowest-priority)
10388 (setq new (read-char-exclusive)))
10389 (if (and (= (upcase org-highest-priority) org-highest-priority)
10390 (= (upcase org-lowest-priority) org-lowest-priority))
10391 (setq new (upcase new)))
10392 (cond ((equal new ?\ ) (setq remove t))
10393 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
10394 (error "Priority must be between `%c' and `%c'"
10395 org-highest-priority org-lowest-priority))))
10397 (if (and (not have) (eq last-command this-command))
10398 (setq new org-lowest-priority)
10399 (setq new (if (and org-priority-start-cycle-with-default (not have))
10400 org-default-priority (1- current)))))
10402 (if (and (not have) (eq last-command this-command))
10403 (setq new org-highest-priority)
10404 (setq new (if (and org-priority-start-cycle-with-default (not have))
10405 org-default-priority (1+ current)))))
10406 (t (error "Invalid action")))
10407 (if (or (< (upcase new) org-highest-priority)
10408 (> (upcase new) org-lowest-priority))
10410 (setq news (format "%c" new))
10413 (replace-match "" t t nil 1)
10414 (replace-match news t t nil 2))
10416 (error "No priority cookie found in line")
10417 (let ((case-fold-search nil))
10418 (looking-at org-todo-line-regexp))
10421 (goto-char (match-end 2))
10422 (insert " [#" news "]"))
10423 (goto-char (match-beginning 3))
10424 (insert "[#" news "] "))))
10425 (org-preserve-lc (org-set-tags nil 'align)))
10427 (message "Priority removed")
10428 (message "Priority of current item set to %s" news))))
10430 (defun org-get-priority (s)
10431 "Find priority cookie and return priority."
10433 (if (not (string-match org-priority-regexp s))
10434 (* 1000 (- org-lowest-priority org-default-priority))
10435 (* 1000 (- org-lowest-priority
10436 (string-to-char (match-string 2 s)))))))
10440 (defvar org-agenda-archives-mode)
10441 (defvar org-map-continue-from nil
10442 "Position from where mapping should continue.
10443 Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
10445 (defvar org-scanner-tags nil
10446 "The current tag list while the tags scanner is running.")
10447 (defvar org-trust-scanner-tags nil
10448 "Should `org-get-tags-at' use the tags fro the scanner.
10449 This is for internal dynamical scoping only.
10450 When this is non-nil, the function `org-get-tags-at' will return the value
10451 of `org-scanner-tags' instead of building the list by itself. This
10452 can lead to large speed-ups when the tags scanner is used in a file with
10453 many entries, and when the list of tags is retrieved, for example to
10454 obtain a list of properties. Building the tags list for each entry in such
10455 a file becomes an N^2 operation - but with this variable set, it scales
10458 (defun org-scan-tags (action matcher &optional todo-only)
10459 "Scan headline tags with inheritance and produce output ACTION.
10461 ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
10462 or `agenda' to produce an entry list for an agenda view. It can also be
10463 a Lisp form or a function that should be called at each matched headline, in
10464 this case the return value is a list of all return values from these calls.
10466 MATCHER is a Lisp form to be evaluated, testing if a given set of tags
10467 qualifies a headline for inclusion. When TODO-ONLY is non-nil,
10468 only lines with a TODO keyword are included in the output."
10469 (require 'org-agenda)
10470 (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
10471 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
10473 "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
10474 (props (list 'face 'default
10475 'done-face 'org-agenda-done
10476 'undone-face 'default
10477 'mouse-face 'highlight
10478 'org-not-done-regexp org-not-done-regexp
10479 'org-todo-regexp org-todo-regexp
10480 'keymap org-agenda-keymap
10482 (format "mouse-2 or RET jump to org file %s"
10483 (abbreviate-file-name
10484 (or (buffer-file-name (buffer-base-buffer))
10485 (buffer-name (buffer-base-buffer)))))))
10486 (case-fold-search nil)
10487 (org-map-continue-from nil)
10488 lspos tags tags-list
10489 (tags-alist (list (cons 0 org-file-tags)))
10490 (llast 0) rtn rtn1 level category i txt
10491 todo marker entry priority)
10492 (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
10493 (setq action (list 'lambda nil action)))
10495 (goto-char (point-min))
10496 (when (eq action 'sparse-tree)
10498 (org-remove-occur-highlights))
10499 (while (re-search-forward re nil t)
10501 (setq todo (if (match-end 1) (org-match-string-no-properties 2))
10502 tags (if (match-end 4) (org-match-string-no-properties 4)))
10503 (goto-char (setq lspos (match-beginning 0)))
10504 (setq level (org-reduced-level (funcall outline-level))
10505 category (org-get-category))
10506 (setq i llast llast level)
10507 ;; remove tag lists from same and sublevels
10508 (while (>= i level)
10509 (when (setq entry (assoc i tags-alist))
10510 (setq tags-alist (delete entry tags-alist)))
10512 ;; add the next tags
10514 (setq tags (org-split-string tags ":")
10516 (cons (cons level tags) tags-alist)))
10517 ;; compile tags for current headline
10519 (if org-use-tag-inheritance
10520 (apply 'append (mapcar 'cdr (reverse tags-alist)))
10522 org-scanner-tags tags-list)
10523 (when org-use-tag-inheritance
10524 (setcdr (car tags-alist)
10525 (mapcar (lambda (x)
10526 (setq x (copy-sequence x))
10527 (org-add-prop-inherited x))
10528 (cdar tags-alist))))
10529 (when (and tags org-use-tag-inheritance
10530 (or (not (eq t org-use-tag-inheritance))
10531 org-tags-exclude-from-inheritance))
10532 ;; selective inheritance, remove uninherited ones
10533 (setcdr (car tags-alist)
10534 (org-remove-uniherited-tags (cdar tags-alist))))
10535 (when (and (or (not todo-only)
10536 (and (member todo org-not-done-keywords)
10537 (or (not org-agenda-tags-todo-honor-ignore-options)
10538 (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
10539 (let ((case-fold-search t)) (eval matcher))
10541 (not (member org-archive-tag tags-list))
10542 ;; we have an archive tag, should we use this anyway?
10543 (or (not org-agenda-skip-archived-trees)
10544 (and (eq action 'agenda) org-agenda-archives-mode))))
10545 (unless (eq action 'sparse-tree) (org-agenda-skip))
10547 ;; select this headline
10550 ((eq action 'sparse-tree)
10551 (and org-highlight-sparse-tree-matches
10552 (org-get-heading) (match-end 0)
10553 (org-highlight-new-match
10554 (match-beginning 0) (match-beginning 1)))
10555 (org-show-context 'tags-tree))
10556 ((eq action 'agenda)
10557 (setq txt (org-format-agenda-item
10560 (if (eq org-tags-match-list-sublevels 'indented)
10561 (make-string (1- level) ?.) "")
10566 priority (org-get-priority txt))
10568 (setq marker (org-agenda-new-marker))
10569 (org-add-props txt props
10570 'org-marker marker 'org-hd-marker marker 'org-category category
10572 'priority priority 'type "tagsmatch")
10574 ((functionp action)
10575 (setq org-map-continue-from nil)
10577 (setq rtn1 (funcall action))
10579 (t (error "Invalid action")))
10581 ;; if we are to skip sublevels, jump to end of subtree
10582 (unless org-tags-match-list-sublevels
10583 (org-end-of-subtree t)
10584 (backward-char 1))))
10585 ;; Get the correct position from where to continue
10586 (if org-map-continue-from
10587 (goto-char org-map-continue-from)
10588 (and (= (point) lspos) (end-of-line 1)))))
10589 (when (and (eq action 'sparse-tree)
10590 (not org-sparse-tree-open-archived-trees))
10591 (org-hide-archived-subtrees (point-min) (point-max)))
10594 (defun org-remove-uniherited-tags (tags)
10595 "Remove all tags that are not inherited from the list TAGS."
10597 ((eq org-use-tag-inheritance t)
10598 (if org-tags-exclude-from-inheritance
10599 (org-delete-all org-tags-exclude-from-inheritance tags)
10601 ((not org-use-tag-inheritance) nil)
10602 ((stringp org-use-tag-inheritance)
10605 (if (and (string-match org-use-tag-inheritance x)
10606 (not (member x org-tags-exclude-from-inheritance)))
10609 ((listp org-use-tag-inheritance)
10612 (if (member x org-use-tag-inheritance) x nil))
10615 (defvar todo-only) ;; dynamically scoped
10617 (defun org-match-sparse-tree (&optional todo-only match)
10618 "Create a sparse tree according to tags string MATCH.
10619 MATCH can contain positive and negative selection of tags, like
10620 \"+WORK+URGENT-WITHBOSS\".
10621 If optional argument TODO-ONLY is non-nil, only select lines that are
10624 (org-prepare-agenda-buffers (list (current-buffer)))
10625 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
10627 (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
10629 (defvar org-cached-props nil)
10630 (defun org-cached-entry-get (pom property)
10631 (if (or (eq t org-use-property-inheritance)
10632 (and (stringp org-use-property-inheritance)
10633 (string-match org-use-property-inheritance property))
10634 (and (listp org-use-property-inheritance)
10635 (member property org-use-property-inheritance)))
10636 ;; Caching is not possible, check it directly
10637 (org-entry-get pom property 'inherit)
10638 ;; Get all properties, so that we can do complicated checks easily
10639 (cdr (assoc property (or org-cached-props
10640 (setq org-cached-props
10641 (org-entry-properties pom)))))))
10643 (defun org-global-tags-completion-table (&optional files)
10644 "Return the list of all tags in all agenda buffer/files."
10651 (set-buffer (find-file-noselect file))
10652 (append (org-get-buffer-tags)
10653 (mapcar (lambda (x) (if (stringp (car-safe x))
10654 (list (car-safe x)) nil))
10656 (if (and files (car files))
10658 (org-agenda-files))))))))
10660 (defun org-make-tags-matcher (match)
10661 "Create the TAGS//TODO matcher form for the selection string MATCH."
10662 ;; todo-only is scoped dynamically into this function, and the function
10663 ;; may change it if the matcher asks for it.
10665 ;; Get a new match request, with completion
10666 (let ((org-last-tags-completion-table
10667 (org-global-tags-completion-table)))
10668 (setq match (org-completing-read-no-ido
10669 "Match: " 'org-tags-completion-function nil nil nil
10670 'org-tags-history))))
10672 ;; Parse the string and create a lisp form
10673 (let ((match0 match)
10674 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
10676 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
10677 orterms term orlist re-p str-p level-p level-op time-p
10678 prop-p pn pv po cat-p gv rest)
10679 (if (string-match "/+" match)
10680 ;; match contains also a todo-matching request
10682 (setq tagsmatch (substring match 0 (match-beginning 0))
10683 todomatch (substring match (match-end 0)))
10684 (if (string-match "^!" todomatch)
10685 (setq todo-only t todomatch (substring todomatch 1)))
10686 (if (string-match "^\\s-*$" todomatch)
10687 (setq todomatch nil)))
10688 ;; only matching tags
10689 (setq tagsmatch match todomatch nil))
10691 ;; Make the tags matcher
10692 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
10693 (setq tagsmatcher t)
10694 (setq orterms (org-split-string tagsmatch "|") orlist nil)
10695 (while (setq term (pop orterms))
10696 (while (and (equal (substring term -1) "\\") orterms)
10697 (setq term (concat term "|" (pop orterms)))) ; repair bad split
10698 (while (string-match re term)
10699 (setq rest (substring term (match-end 0))
10700 minus (and (match-end 1)
10701 (equal (match-string 1 term) "-"))
10702 tag (match-string 2 term)
10703 re-p (equal (string-to-char tag) ?{)
10704 level-p (match-end 4)
10705 prop-p (match-end 5)
10707 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
10709 (setq level-op (org-op-to-function (match-string 3 term)))
10710 `(,level-op level ,(string-to-number
10711 (match-string 4 term))))
10713 (setq pn (match-string 5 term)
10714 po (match-string 6 term)
10715 pv (match-string 7 term)
10716 cat-p (equal pn "CATEGORY")
10717 re-p (equal (string-to-char pv) ?{)
10718 str-p (equal (string-to-char pv) ?\")
10719 time-p (save-match-data
10720 (string-match "^\"[[<].*[]>]\"$" pv))
10721 pv (if (or re-p str-p) (substring pv 1 -1) pv))
10722 (if time-p (setq pv (org-matcher-time pv)))
10723 (setq po (org-op-to-function po (if time-p 'time str-p)))
10725 ((equal pn "CATEGORY")
10726 (setq gv '(get-text-property (point) 'org-category)))
10730 (setq gv `(org-cached-entry-get nil ,pn))))
10733 `(not (string-match ,pv (or ,gv "")))
10734 `(string-match ,pv (or ,gv "")))
10736 `(,po (or ,gv "") ,pv)
10737 `(,po (string-to-number (or ,gv ""))
10738 ,(string-to-number pv) ))))
10739 (t `(member ,tag tags-list)))
10740 mm (if minus (list 'not mm) mm)
10742 (push mm tagsmatcher))
10743 (push (if (> (length tagsmatcher) 1)
10744 (cons 'and tagsmatcher)
10747 (setq tagsmatcher nil))
10748 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
10750 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
10751 ;; Make the todo matcher
10752 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
10753 (setq todomatcher t)
10754 (setq orterms (org-split-string todomatch "|") orlist nil)
10755 (while (setq term (pop orterms))
10756 (while (string-match re term)
10757 (setq minus (and (match-end 1)
10758 (equal (match-string 1 term) "-"))
10759 kwd (match-string 2 term)
10760 re-p (equal (string-to-char kwd) ?{)
10761 term (substring term (match-end 0))
10763 `(string-match ,(substring kwd 1 -1) todo)
10764 (list 'equal 'todo kwd))
10765 mm (if minus (list 'not mm) mm))
10766 (push mm todomatcher))
10767 (push (if (> (length todomatcher) 1)
10768 (cons 'and todomatcher)
10771 (setq todomatcher nil))
10772 (setq todomatcher (if (> (length orlist) 1)
10773 (cons 'or orlist) (car orlist))))
10775 ;; Return the string and lisp forms of the matcher
10776 (setq matcher (if todomatcher
10777 (list 'and tagsmatcher todomatcher)
10779 (cons match0 matcher)))
10781 (defun org-op-to-function (op &optional stringp)
10782 "Turn an operator into the appropriate function."
10785 ((equal op "<" ) '(< string< org-time<))
10786 ((equal op ">" ) '(> org-string> org-time>))
10787 ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
10788 ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
10789 ((member op '("=" "==")) '(= string= org-time=))
10790 ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
10791 (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
10793 (defun org<> (a b) (not (= a b)))
10794 (defun org-string<= (a b) (or (string= a b) (string< a b)))
10795 (defun org-string>= (a b) (not (string< a b)))
10796 (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
10797 (defun org-string<> (a b) (not (string= a b)))
10798 (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
10799 (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
10800 (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
10801 (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
10802 (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
10803 (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
10805 "Convert S to a floating point time.
10806 If S is already a number, just return it. If it is a string, parse
10807 it as a time string and apply `float-time' to it. If S is nil, just return 0."
10811 (condition-case nil
10812 (float-time (apply 'encode-time (org-parse-time-string s)))
10816 (defun org-time-today ()
10817 "Time in seconds today at 0:00.
10818 Returns the float number of seconds since the beginning of the
10819 epoch to the beginning of today (00:00)."
10820 (float-time (apply 'encode-time
10821 (append '(0 0 0) (nthcdr 3 (decode-time))))))
10823 (defun org-matcher-time (s)
10824 "Interpret a time comparison value."
10827 ((string= s "<now>") (float-time))
10828 ((string= s "<today>") (org-time-today))
10829 ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
10830 ((string= s "<yesterday>") (- (org-time-today) 86400.0))
10831 ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
10832 (+ (org-time-today)
10833 (* (string-to-number (match-string 1 s))
10834 (cdr (assoc (match-string 2 s)
10835 '(("d" . 86400.0) ("w" . 604800.0)
10836 ("m" . 2678400.0) ("y" . 31557600.0)))))))
10839 (defun org-match-any-p (re list)
10840 "Does re match any element of list?"
10841 (setq list (mapcar (lambda (x) (string-match re x)) list))
10844 (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
10845 (defvar org-tags-overlay (org-make-overlay 1 1))
10846 (org-detach-overlay org-tags-overlay)
10848 (defun org-get-local-tags-at (&optional pos)
10849 "Get a list of tags defined in the current headline."
10850 (org-get-tags-at pos 'local))
10852 (defun org-get-local-tags ()
10853 "Get a list of tags defined in the current headline."
10854 (org-get-tags-at nil 'local))
10856 (defun org-get-tags-at (&optional pos local)
10857 "Get a list of all headline tags applicable at POS.
10858 POS defaults to point. If tags are inherited, the list contains
10859 the targets in the same sequence as the headlines appear, i.e.
10860 the tags of the current headline come last.
10861 When LOCAL is non-nil, only return tags from the current headline,
10862 ignore inherited ones."
10864 (if (and org-trust-scanner-tags
10865 (or (not pos) (equal pos (point)))
10868 (let (tags ltags lastpos parent)
10872 (goto-char (or pos (point)))
10875 (condition-case nil
10877 (org-back-to-heading t)
10878 (while (not (equal lastpos (point)))
10879 (setq lastpos (point))
10881 (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
10882 (setq ltags (org-split-string
10883 (org-match-string-no-properties 1) ":"))
10885 (setq ltags (mapcar 'org-add-prop-inherited ltags)))
10888 (org-remove-uniherited-tags ltags)
10891 (or org-use-tag-inheritance (throw 'done t))
10892 (if local (throw 'done t))
10893 (or (org-up-heading-safe) (error nil))
10896 (append (org-remove-uniherited-tags org-file-tags) tags)))))
10898 (defun org-add-prop-inherited (s)
10899 (add-text-properties 0 (length s) '(inherited t) s)
10902 (defun org-toggle-tag (tag &optional onoff)
10903 "Toggle the tag TAG for the current line.
10904 If ONOFF is `on' or `off', don't toggle but set to this state."
10907 (org-back-to-heading t)
10908 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
10911 (setq current (match-string 1))
10912 (replace-match ""))
10914 (setq current (nreverse (org-split-string current ":")))
10918 (or (member tag current) (push tag current)))
10920 (or (not (member tag current)) (setq current (delete tag current))))
10921 (t (if (member tag current)
10922 (setq current (delete tag current))
10924 (push tag current))))
10928 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
10929 (org-set-tags nil t))
10930 (delete-horizontal-space))
10931 (run-hooks 'org-after-tags-change-hook))
10934 (defun org-align-tags-here (to-col)
10935 ;; Assumes that this is a headline
10936 (let ((pos (point)) (col (current-column)) ncol tags-l p)
10937 (beginning-of-line 1)
10938 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
10939 (< pos (match-beginning 2)))
10941 (setq tags-l (- (match-end 2) (match-beginning 2)))
10942 (goto-char (match-beginning 1))
10944 (delete-region (point) (1+ (match-beginning 2)))
10945 (setq ncol (max (1+ (current-column))
10949 (- (abs to-col) tags-l))))
10951 (insert (make-string (- ncol (current-column)) ?\ ))
10952 (setq ncol (current-column))
10953 (when indent-tabs-mode (tabify p (point-at-eol)))
10954 (org-move-to-column (min ncol col) t))
10957 (defun org-set-tags-command (&optional arg just-align)
10958 "Call the set-tags command for the current entry."
10960 (if (org-on-heading-p)
10961 (org-set-tags arg just-align)
10963 (org-back-to-heading t)
10964 (org-set-tags arg just-align))))
10966 (defun org-set-tags (&optional arg just-align)
10967 "Set the tags for the current headline.
10968 With prefix ARG, realign all tags in headings in the current buffer."
10970 (let* ((re (concat "^" outline-regexp))
10971 (current (org-get-tags-string))
10972 (col (current-column))
10973 (org-setting-tags t)
10974 table current-tags inherited-tags ; computed below when needed
10978 (goto-char (point-min))
10979 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
10980 (while (re-search-forward re nil t)
10981 (org-set-tags nil t)
10983 (message "All tags realigned to column %d" org-tags-column))
10985 (setq tags current)
10986 ;; Get a new set of tags from the user
10988 (setq table (append org-tag-persistent-alist
10989 (or org-tag-alist (org-get-buffer-tags)))
10990 org-last-tags-completion-table table
10991 current-tags (org-split-string current ":")
10992 inherited-tags (nreverse
10993 (nthcdr (length current-tags)
10994 (nreverse (org-get-tags-at))))
10996 (if (or (eq t org-use-fast-tag-selection)
10997 (and org-use-fast-tag-selection
10998 (delq nil (mapcar 'cdr table))))
10999 (org-fast-tag-selection
11000 current-tags inherited-tags table
11001 (if org-fast-tag-selection-include-todo org-todo-key-alist))
11002 (let ((org-add-colon-after-tag-completion t))
11004 (org-without-partial-completion
11005 (org-ido-completing-read "Tags: " 'org-tags-completion-function
11006 nil nil current 'org-tags-history)))))))
11007 (while (string-match "[-+&]+" tags)
11008 ;; No boolean logic, just a list
11009 (setq tags (replace-match ":" t t tags))))
11011 (if org-tags-sort-function
11012 (setq tags (mapconcat 'identity
11013 (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
11014 org-tags-sort-function) ":")))
11016 (if (string-match "\\`[\t ]*\\'" tags)
11018 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
11019 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
11021 ;; Insert new tags at the correct column
11022 (beginning-of-line 1)
11024 ((and (equal current "") (equal tags "")))
11025 ((re-search-forward
11026 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
11028 (if (equal tags "")
11030 (goto-char (match-beginning 0))
11031 (setq c0 (current-column) p0 (point)
11032 c1 (max (1+ c0) (if (> org-tags-column 0)
11034 (- (- org-tags-column) (length tags))))
11035 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
11036 (replace-match rpl t t)
11037 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
11039 (t (error "Tags alignment failed")))
11040 (org-move-to-column col)
11042 (run-hooks 'org-after-tags-change-hook)))))
11044 (defun org-change-tag-in-region (beg end tag off)
11045 "Add or remove TAG for each entry in the region.
11046 This works in the agenda, and also in an org-mode buffer."
11048 (list (region-beginning) (region-end)
11049 (let ((org-last-tags-completion-table
11051 (org-get-buffer-tags)
11052 (org-global-tags-completion-table))))
11053 (org-ido-completing-read
11054 "Tag: " 'org-tags-completion-function nil nil nil
11055 'org-tags-history))
11057 (message "[s]et or [r]emove? ")
11058 (equal (read-char-exclusive) ?r))))
11059 (if (fboundp 'deactivate-mark) (deactivate-mark))
11060 (let ((agendap (equal major-mode 'org-agenda-mode))
11061 l1 l2 m buf pos newhead (cnt 0))
11063 (setq l2 (1- (org-current-line)))
11065 (setq l1 (org-current-line))
11066 (loop for l from l1 to l2 do
11068 (setq m (get-text-property (point) 'org-hd-marker))
11069 (when (or (and (org-mode-p) (org-on-heading-p))
11071 (setq buf (if agendap (marker-buffer m) (current-buffer))
11072 pos (if agendap m (point)))
11073 (with-current-buffer buf
11077 (setq cnt (1+ cnt))
11078 (org-toggle-tag tag (if off 'off 'on))
11079 (setq newhead (org-get-heading)))))
11080 (and agendap (org-agenda-change-all-lines newhead m))))
11081 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
11083 (defun org-tags-completion-function (string predicate &optional flag)
11084 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
11085 (confirm (lambda (x) (stringp (car x)))))
11086 (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
11087 (setq s1 (match-string 1 string)
11088 s2 (match-string 2 string))
11089 (setq s1 "" s2 string))
11093 (setq rtn (try-completion s2 ctable confirm))
11096 (concat s1 s2 (substring rtn (length s2))
11097 (if (and org-add-colon-after-tag-completion
11098 (assoc rtn ctable))
11103 (all-completions s2 ctable confirm)
11107 (assoc s2 ctable)))
11110 (defun org-fast-tag-insert (kwd tags face &optional end)
11111 "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
11112 (insert (format "%-12s" (concat kwd ":"))
11113 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
11116 (defun org-fast-tag-show-exit (flag)
11119 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
11120 (replace-match ""))
11123 (org-move-to-column (- (window-width) 19) t)
11124 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
11126 (defun org-set-current-tags-overlay (current prefix)
11127 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
11128 (if (featurep 'xemacs)
11129 (org-overlay-display org-tags-overlay (concat prefix s)
11130 'secondary-selection)
11131 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
11132 (org-overlay-display org-tags-overlay (concat prefix s)))))
11134 (defun org-fast-tag-selection (current inherited table &optional todo-table)
11135 "Fast tag selection with single keys.
11136 CURRENT is the current list of tags in the headline, INHERITED is the
11137 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
11138 possibly with grouping information. TODO-TABLE is a similar table with
11139 TODO keywords, should these have keys assigned to them.
11140 If the keys are nil, a-z are automatically assigned.
11141 Returns the new tags string, or nil to not change the current settings."
11142 (let* ((fulltable (append table todo-table))
11143 (maxlen (apply 'max (mapcar
11145 (if (stringp (car x)) (string-width (car x)) 0))
11147 (buf (current-buffer))
11148 (expert (eq org-fast-tag-selection-single-key 'expert))
11150 (fwidth (+ maxlen 3 1 3))
11151 (ncol (/ (- (window-width) 4) fwidth))
11154 tg cnt e c char c1 c2 ntable tbl rtn
11155 ov-start ov-end ov-prefix
11156 (exit-after-next org-fast-tag-selection-single-key)
11157 (done-keywords org-done-keywords)
11160 (beginning-of-line 1)
11162 (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
11163 (setq ov-start (match-beginning 1)
11164 ov-end (match-end 1)
11166 (setq ov-start (1- (point-at-eol))
11167 ov-end (1+ ov-start))
11168 (skip-chars-forward "^\n\r")
11171 (buffer-substring (1- (point)) (point))
11172 (if (> (current-column) org-tags-column)
11174 (make-string (- org-tags-column (current-column)) ?\ ))))))
11175 (org-move-overlay org-tags-overlay ov-start ov-end)
11176 (save-window-excursion
11178 (set-buffer (get-buffer-create " *Org tags*"))
11179 (delete-other-windows)
11180 (split-window-vertically)
11181 (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
11183 (org-set-local 'org-done-keywords done-keywords)
11184 (org-fast-tag-insert "Inherited" inherited i-face "\n")
11185 (org-fast-tag-insert "Current" current c-face "\n\n")
11186 (org-fast-tag-show-exit exit-after-next)
11187 (org-set-current-tags-overlay current ov-prefix)
11188 (setq tbl fulltable char ?a cnt 0)
11189 (while (setq e (pop tbl))
11191 ((equal e '(:startgroup))
11192 (push '() groups) (setq ingroup t)
11193 (when (not (= cnt 0))
11197 ((equal e '(:endgroup))
11198 (setq ingroup nil cnt 0)
11200 ((equal e '(:newline))
11201 (when (not (= cnt 0))
11205 (while (equal (car tbl) '(:newline))
11207 (setq tbl (cdr tbl)))))
11209 (setq tg (car e) c2 nil)
11212 ;; automatically assign a character.
11213 (setq c1 (string-to-char
11214 (downcase (substring
11215 tg (if (= (string-to-char tg) ?@) 1 0)))))
11216 (if (or (rassoc c1 ntable) (rassoc c1 table))
11217 (while (or (rassoc char ntable) (rassoc char table))
11218 (setq char (1+ char)))
11220 (setq c (or c2 char)))
11221 (if ingroup (push tg (car groups)))
11222 (setq tg (org-add-props tg nil 'face
11224 ((not (assoc tg table))
11225 (org-get-todo-face tg))
11226 ((member tg current) c-face)
11227 ((member tg inherited) i-face)
11229 (if (and (= cnt 0) (not ingroup)) (insert " "))
11230 (insert "[" c "] " tg (make-string
11231 (- fwidth 4 (length tg)) ?\ ))
11232 (push (cons tg c) ntable)
11233 (when (= (setq cnt (1+ cnt)) ncol)
11235 (if ingroup (insert " "))
11237 (setq ntable (nreverse ntable))
11239 (goto-char (point-min))
11240 (if (not expert) (org-fit-window-to-buffer))
11244 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
11245 (if groups " [!] no groups" " [!]groups")
11246 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
11247 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
11249 ((= c ?\r) (throw 'exit t))
11251 (setq groups (not groups))
11252 (goto-char (point-min))
11253 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
11256 (org-fast-tag-show-exit
11257 (setq exit-after-next (not exit-after-next)))
11259 (delete-other-windows)
11260 (split-window-vertically)
11261 (org-switch-to-buffer-other-window " *Org tags*")
11262 (org-fit-window-to-buffer)))
11264 (and (= c ?q) (not (rassoc c ntable))))
11265 (org-detach-overlay org-tags-overlay)
11266 (setq quit-flag t))
11269 (if exit-after-next (setq exit-after-next 'now)))
11271 (condition-case nil
11272 (setq tg (org-ido-completing-read
11275 (with-current-buffer buf
11276 (org-get-buffer-tags)))))
11277 (quit (setq tg "")))
11278 (when (string-match "\\S-" tg)
11279 (add-to-list 'buffer-tags (list tg))
11280 (if (member tg current)
11281 (setq current (delete tg current))
11282 (push tg current)))
11283 (if exit-after-next (setq exit-after-next 'now)))
11284 ((setq e (rassoc c todo-table) tg (car e))
11285 (with-current-buffer buf
11286 (save-excursion (org-todo tg)))
11287 (if exit-after-next (setq exit-after-next 'now)))
11288 ((setq e (rassoc c ntable) tg (car e))
11289 (if (member tg current)
11290 (setq current (delete tg current))
11291 (loop for g in groups do
11294 (setq current (delete x current)))
11297 (if exit-after-next (setq exit-after-next 'now))))
11299 ;; Create a sorted list
11303 (assoc b (cdr (memq (assoc a ntable) ntable))))))
11304 (if (eq exit-after-next 'now) (throw 'exit t))
11305 (goto-char (point-min))
11306 (beginning-of-line 2)
11307 (delete-region (point) (point-at-eol))
11308 (org-fast-tag-insert "Current" current c-face)
11309 (org-set-current-tags-overlay current ov-prefix)
11310 (while (re-search-forward
11311 (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
11312 (setq tg (match-string 1))
11313 (add-text-properties
11314 (match-beginning 1) (match-end 1)
11317 ((member tg current) c-face)
11318 ((member tg inherited) i-face)
11319 (t (get-text-property (match-beginning 1) 'face))))))
11320 (goto-char (point-min)))))
11321 (org-detach-overlay org-tags-overlay)
11323 (mapconcat 'identity current ":")
11326 (defun org-get-tags-string ()
11327 "Get the TAGS string in the current headline."
11328 (unless (org-on-heading-p t)
11329 (error "Not on a heading"))
11331 (beginning-of-line 1)
11332 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
11333 (org-match-string-no-properties 1)
11336 (defun org-get-tags ()
11337 "Get the list of tags specified in the current headline."
11338 (org-split-string (org-get-tags-string) ":"))
11340 (defun org-get-buffer-tags ()
11341 "Get a table of all tags used in the buffer, for completion."
11344 (goto-char (point-min))
11345 (while (re-search-forward
11346 (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
11347 (when (equal (char-after (point-at-bol 0)) ?*)
11348 (mapc (lambda (x) (add-to-list 'tags x))
11349 (org-split-string (org-match-string-no-properties 1) ":")))))
11350 (mapcar 'list tags)))
11352 ;;;; The mapping API
11355 (defun org-map-entries (func &optional match scope &rest skip)
11356 "Call FUNC at each headline selected by MATCH in SCOPE.
11358 FUNC is a function or a lisp form. The function will be called without
11359 arguments, with the cursor positioned at the beginning of the headline.
11360 The return values of all calls to the function will be collected and
11361 returned as a list.
11363 The call to FUNC will be wrapped into a save-excursion form, so FUNC
11364 does not need to preserve point. After evaluation, the cursor will be
11365 moved to the end of the line (presumably of the headline of the
11366 processed entry) and search continues from there. Under some
11367 circumstances, this may not produce the wanted results. For example,
11368 if you have removed (e.g. archived) the current (sub)tree it could
11369 mean that the next entry will be skipped entirely. In such cases, you
11370 can specify the position from where search should continue by making
11371 FUNC set the variable `org-map-continue-from' to the desired buffer
11374 MATCH is a tags/property/todo match as it is used in the agenda tags view.
11375 Only headlines that are matched by this query will be considered during
11376 the iteration. When MATCH is nil or t, all headlines will be
11377 visited by the iteration.
11379 SCOPE determines the scope of this command. It can be any of:
11381 nil The current buffer, respecting the restriction if any
11382 tree The subtree started with the entry at point
11383 file The current buffer, without restriction
11385 The current buffer, and any archives associated with it
11386 agenda All agenda files
11387 agenda-with-archives
11388 All agenda files with any archive files associated with them
11390 If this is a list, all files in the list will be scanned
11392 The remaining args are treated as settings for the skipping facilities of
11393 the scanner. The following items can be given here:
11395 archive skip trees with the archive tag.
11396 comment skip trees with the COMMENT keyword
11397 function or Emacs Lisp form:
11398 will be used as value for `org-agenda-skip-function', so whenever
11399 the the function returns t, FUNC will not be called for that
11400 entry and search will continue from the point where the
11401 function leaves it.
11403 If your function needs to retrieve the tags including inherited tags
11404 at the *current* entry, you can use the value of the variable
11405 `org-scanner-tags' which will be much faster than getting the value
11406 with `org-get-tags-at'. If your function gets properties with
11407 `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
11408 to t around the call to `org-entry-properties' to get the same speedup.
11409 Note that if your function moves around to retrieve tags and properties at
11410 a *different* entry, you cannot use these techniques."
11411 (let* ((org-agenda-archives-mode nil) ; just to make sure
11412 (org-agenda-skip-archived-trees (memq 'archive skip))
11413 (org-agenda-skip-comment-trees (memq 'comment skip))
11414 (org-agenda-skip-function
11415 (car (org-delete-all '(comment archive) skip)))
11416 (org-tags-match-list-sublevels t)
11418 org-todo-keywords-for-agenda
11419 org-done-keywords-for-agenda
11420 org-todo-keyword-alist-for-agenda
11421 org-tag-alist-for-agenda)
11424 ((eq match t) (setq matcher t))
11425 ((eq match nil) (setq matcher t))
11426 (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
11430 (when (eq scope 'tree)
11431 (org-back-to-heading t)
11432 (org-narrow-to-subtree)
11437 (org-prepare-agenda-buffers
11438 (list (buffer-file-name (current-buffer))))
11439 (setq res (org-scan-tags func matcher)))
11440 ;; Get the right scope
11442 ((and scope (listp scope) (symbolp (car scope)))
11443 (setq scope (eval scope)))
11444 ((eq scope 'agenda)
11445 (setq scope (org-agenda-files t)))
11446 ((eq scope 'agenda-with-archives)
11447 (setq scope (org-agenda-files t))
11448 (setq scope (org-add-archive-files scope)))
11450 (setq scope (list (buffer-file-name))))
11451 ((eq scope 'file-with-archives)
11452 (setq scope (org-add-archive-files (list (buffer-file-name))))))
11453 (org-prepare-agenda-buffers scope)
11454 (while (setq file (pop scope))
11455 (with-current-buffer (org-find-base-buffer-visiting file)
11459 (goto-char (point-min))
11460 (setq res (append res (org-scan-tags func matcher))))))))))
11465 ;;; Setting and retrieving properties
11467 (defconst org-special-properties
11468 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
11469 "TIMESTAMP" "TIMESTAMP_IA")
11470 "The special properties valid in Org-mode.
11472 These are properties that are not defined in the property drawer,
11473 but in some other way.")
11475 (defconst org-default-properties
11476 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
11477 "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
11478 "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
11479 "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
11480 "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
11481 "CLOCK_MODELINE_TOTAL")
11482 "Some properties that are used by Org-mode for various purposes.
11483 Being in this list makes sure that they are offered for completion.")
11485 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
11486 "Regular expression matching the first line of a property drawer.")
11488 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
11489 "Regular expression matching the first line of a property drawer.")
11491 (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
11492 "Regular expression matching the first line of a property drawer.")
11494 (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
11495 "Regular expression matching the first line of a property drawer.")
11497 (defconst org-property-drawer-re
11498 (concat "\\(" org-property-start-re "\\)[^\000]*\\("
11499 org-property-end-re "\\)\n?")
11500 "Matches an entire property drawer.")
11502 (defconst org-clock-drawer-re
11503 (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
11504 org-property-end-re "\\)\n?")
11505 "Matches an entire clock drawer.")
11507 (defun org-property-action ()
11508 "Do an action on properties."
11511 (org-at-property-p)
11512 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
11513 (setq c (read-char-exclusive))
11516 (call-interactively 'org-set-property))
11518 (call-interactively 'org-delete-property))
11520 (call-interactively 'org-delete-property-globally))
11522 (call-interactively 'org-compute-property-at-point))
11523 (t (error "No such property action %c" c)))))
11525 (defun org-at-property-p ()
11526 "Is the cursor in a property line?"
11527 ;; FIXME: Does not check if we are actually in the drawer.
11528 ;; FIXME: also returns true on any drawers.....
11529 ;; This is used by C-c C-c for property action.
11531 (beginning-of-line 1)
11532 (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
11534 (defun org-get-property-block (&optional beg end force)
11535 "Return the (beg . end) range of the body of the property drawer.
11536 BEG and END can be beginning and end of subtree, if not given
11537 they will be found.
11538 If the drawer does not exist and FORCE is non-nil, create the drawer."
11541 (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
11542 (end (or end (progn (outline-next-heading) (point)))))
11544 (if (re-search-forward org-property-start-re end t)
11545 (setq beg (1+ (match-end 0)))
11548 (org-insert-property-drawer)
11549 (setq end (progn (outline-next-heading) (point))))
11552 (if (re-search-forward org-property-start-re end t)
11553 (setq beg (1+ (match-end 0)))))
11554 (if (re-search-forward org-property-end-re end t)
11555 (setq end (match-beginning 0))
11556 (or force (throw 'exit nil))
11559 (org-indent-line-function)
11560 (insert ":END:\n"))
11563 (defun org-entry-properties (&optional pom which)
11564 "Get all properties of the entry at point-or-marker POM.
11565 This includes the TODO keyword, the tags, time strings for deadline,
11566 scheduled, and clocking, and any additional properties defined in the
11567 entry. The return value is an alist, keys may occur multiple times
11568 if the property key was used several times.
11569 POM may also be nil, in which case the current entry is used.
11570 If WHICH is nil or `all', get all properties. If WHICH is
11571 `special' or `standard', only get that subclass."
11572 (setq which (or which 'all))
11573 (org-with-point-at pom
11574 (let ((clockstr (substring org-clock-string 0 -1))
11575 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
11576 beg end range props sum-props key value string clocksum)
11578 (when (condition-case nil
11579 (and (org-mode-p) (org-back-to-heading t))
11582 (setq sum-props (get-text-property (point) 'org-summaries))
11583 (setq clocksum (get-text-property (point) :org-clock-minutes))
11584 (outline-next-heading)
11586 (when (memq which '(all special))
11587 ;; Get the special properties, like TODO and tags
11589 (when (and (looking-at org-todo-line-regexp) (match-end 2))
11590 (push (cons "TODO" (org-match-string-no-properties 2)) props))
11591 (when (looking-at org-priority-regexp)
11592 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
11593 (when (and (setq value (org-get-tags-string))
11594 (string-match "\\S-" value))
11595 (push (cons "TAGS" value) props))
11596 (when (setq value (org-get-tags-at))
11597 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
11599 (while (re-search-forward org-maybe-keyword-time-regexp end t)
11600 (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
11601 string (if (equal key clockstr)
11605 (match-beginning 3) (goto-char (point-at-eol)))))
11606 (substring (org-match-string-no-properties 3) 1 -1)))
11608 (if (= (char-after (match-beginning 3)) ?\[)
11609 (setq key "TIMESTAMP_IA")
11610 (setq key "TIMESTAMP")))
11611 (when (or (equal key clockstr) (not (assoc key props)))
11612 (push (cons key string) props)))
11616 (when (memq which '(all standard))
11617 ;; Get the standard properties, like :PROP: ...
11618 (setq range (org-get-property-block beg end))
11620 (goto-char (car range))
11621 (while (re-search-forward
11622 (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
11624 (setq key (org-match-string-no-properties 1)
11625 value (org-trim (or (org-match-string-no-properties 2) "")))
11626 (unless (member key excluded)
11627 (push (cons key (or value "")) props)))))
11629 (push (cons "CLOCKSUM"
11630 (org-columns-number-to-string (/ (float clocksum) 60.)
11633 (unless (assoc "CATEGORY" props)
11634 (setq value (or (org-get-category)
11635 (progn (org-refresh-category-properties)
11636 (org-get-category))))
11637 (push (cons "CATEGORY" value) props))
11638 (append sum-props (nreverse props)))))))
11640 (defun org-entry-get (pom property &optional inherit)
11641 "Get value of PROPERTY for entry at point-or-marker POM.
11642 If INHERIT is non-nil and the entry does not have the property,
11643 then also check higher levels of the hierarchy.
11644 If INHERIT is the symbol `selective', use inheritance only if the setting
11645 in `org-use-property-inheritance' selects PROPERTY for inheritance.
11646 If the property is present but empty, the return value is the empty string.
11647 If the property is not present at all, nil is returned."
11648 (org-with-point-at pom
11649 (if (and inherit (if (eq inherit 'selective)
11650 (org-property-inherit-p property)
11652 (org-entry-get-with-inheritance property)
11653 (if (member property org-special-properties)
11654 ;; We need a special property. Use brute force, get all properties.
11655 (cdr (assoc property (org-entry-properties nil 'special)))
11656 (let ((range (org-get-property-block)))
11658 (goto-char (car range))
11660 (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
11662 ;; Found the property, return it.
11664 (org-match-string-no-properties 1)
11667 (defun org-property-or-variable-value (var &optional inherit)
11668 "Check if there is a property fixing the value of VAR.
11669 If yes, return this value. If not, return the current value of the variable."
11670 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
11671 (if (and prop (stringp prop) (string-match "\\S-" prop))
11673 (symbol-value var))))
11675 (defun org-entry-delete (pom property)
11676 "Delete the property PROPERTY from entry at point-or-marker POM."
11677 (org-with-point-at pom
11678 (if (member property org-special-properties)
11679 nil ; cannot delete these properties.
11680 (let ((range (org-get-property-block)))
11682 (goto-char (car range))
11684 (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
11687 (delete-region (match-beginning 0) (1+ (point-at-eol)))
11691 ;; Multi-values properties are properties that contain multiple values
11692 ;; These values are assumed to be single words, separated by whitespace.
11693 (defun org-entry-add-to-multivalued-property (pom property value)
11694 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
11695 (let* ((old (org-entry-get pom property))
11696 (values (and old (org-split-string old "[ \t]"))))
11697 (setq value (org-entry-protect-space value))
11698 (unless (member value values)
11699 (setq values (cons value values))
11700 (org-entry-put pom property
11701 (mapconcat 'identity values " ")))))
11703 (defun org-entry-remove-from-multivalued-property (pom property value)
11704 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
11705 (let* ((old (org-entry-get pom property))
11706 (values (and old (org-split-string old "[ \t]"))))
11707 (setq value (org-entry-protect-space value))
11708 (when (member value values)
11709 (setq values (delete value values))
11710 (org-entry-put pom property
11711 (mapconcat 'identity values " ")))))
11713 (defun org-entry-member-in-multivalued-property (pom property value)
11714 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
11715 (let* ((old (org-entry-get pom property))
11716 (values (and old (org-split-string old "[ \t]"))))
11717 (setq value (org-entry-protect-space value))
11718 (member value values)))
11720 (defun org-entry-get-multivalued-property (pom property)
11721 "Return a list of values in a multivalued property."
11722 (let* ((value (org-entry-get pom property))
11723 (values (and value (org-split-string value "[ \t]"))))
11724 (mapcar 'org-entry-restore-space values)))
11726 (defun org-entry-put-multivalued-property (pom property &rest values)
11727 "Set multivalued PROPERTY at point-or-marker POM to VALUES.
11728 VALUES should be a list of strings. Spaces will be protected."
11729 (org-entry-put pom property
11730 (mapconcat 'org-entry-protect-space values " "))
11731 (let* ((value (org-entry-get pom property))
11732 (values (and value (org-split-string value "[ \t]"))))
11733 (mapcar 'org-entry-restore-space values)))
11735 (defun org-entry-protect-space (s)
11736 "Protect spaces and newline in string S."
11737 (while (string-match " " s)
11738 (setq s (replace-match "%20" t t s)))
11739 (while (string-match "\n" s)
11740 (setq s (replace-match "%0A" t t s)))
11743 (defun org-entry-restore-space (s)
11744 "Restore spaces and newline in string S."
11745 (while (string-match "%20" s)
11746 (setq s (replace-match " " t t s)))
11747 (while (string-match "%0A" s)
11748 (setq s (replace-match "\n" t t s)))
11751 (defvar org-entry-property-inherited-from (make-marker)
11752 "Marker pointing to the entry from where a property was inherited.
11753 Each call to `org-entry-get-with-inheritance' will set this marker to the
11754 location of the entry where the inheritance search matched. If there was
11755 no match, the marker will point nowhere.
11756 Note that also `org-entry-get' calls this function, if the INHERIT flag
11759 (defun org-entry-get-with-inheritance (property)
11760 "Get entry property, and search higher levels if not present."
11761 (move-marker org-entry-property-inherited-from nil)
11768 (when (setq tmp (org-entry-get nil property))
11769 (org-back-to-heading t)
11770 (move-marker org-entry-property-inherited-from (point))
11772 (or (org-up-heading-safe) (throw 'ex nil)))))
11774 (cdr (assoc property org-file-properties))
11775 (cdr (assoc property org-global-properties))
11776 (cdr (assoc property org-global-properties-fixed))))))
11778 (defun org-entry-put (pom property value)
11779 "Set PROPERTY to VALUE for entry at point-or-marker POM."
11780 (org-with-point-at pom
11781 (org-back-to-heading t)
11782 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
11785 ((equal property "TODO")
11786 (when (and (stringp value) (string-match "\\S-" value)
11787 (not (member value org-todo-keywords-1)))
11788 (error "\"%s\" is not a valid TODO state" value))
11789 (if (or (not value)
11790 (not (string-match "\\S-" value)))
11791 (setq value 'none))
11793 (org-set-tags nil 'align))
11794 ((equal property "PRIORITY")
11795 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
11796 (string-to-char value) ?\ ))
11797 (org-set-tags nil 'align))
11798 ((equal property "SCHEDULED")
11799 (if (re-search-forward org-scheduled-time-regexp end t)
11801 ((eq value 'earlier) (org-timestamp-change -1 'day))
11802 ((eq value 'later) (org-timestamp-change 1 'day))
11803 (t (call-interactively 'org-schedule)))
11804 (call-interactively 'org-schedule)))
11805 ((equal property "DEADLINE")
11806 (if (re-search-forward org-deadline-time-regexp end t)
11808 ((eq value 'earlier) (org-timestamp-change -1 'day))
11809 ((eq value 'later) (org-timestamp-change 1 'day))
11810 (t (call-interactively 'org-deadline)))
11811 (call-interactively 'org-deadline)))
11812 ((member property org-special-properties)
11813 (error "The %s property can not yet be set with `org-entry-put'"
11815 (t ; a non-special property
11816 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
11817 (setq range (org-get-property-block beg end 'force))
11818 (goto-char (car range))
11819 (if (re-search-forward
11820 (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
11822 (delete-region (match-beginning 1) (match-end 1))
11823 (goto-char (match-beginning 1)))
11824 (goto-char (cdr range))
11827 (org-indent-line-function)
11828 (insert ":" property ":"))
11829 (and value (insert " " value))
11830 (org-indent-line-function)))))))
11832 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
11833 "Get all property keys in the current buffer.
11834 With INCLUDE-SPECIALS, also list the special properties that reflect things
11835 like tags and TODO state.
11836 With INCLUDE-DEFAULTS, also include properties that has special meaning
11837 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
11838 With INCLUDE-COLUMNS, also include property names given in COLUMN
11839 formats in the current buffer."
11840 (let (rtn range cfmt s p)
11844 (goto-char (point-min))
11845 (while (re-search-forward org-property-start-re nil t)
11846 (setq range (org-get-property-block))
11847 (goto-char (car range))
11848 (while (re-search-forward
11849 (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
11851 (add-to-list 'rtn (org-match-string-no-properties 1)))
11852 (outline-next-heading))))
11854 (when include-specials
11855 (setq rtn (append org-special-properties rtn)))
11857 (when include-defaults
11858 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
11859 (add-to-list 'rtn org-effort-property))
11861 (when include-columns
11865 (goto-char (point-min))
11866 (while (re-search-forward
11867 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
11869 (setq cfmt (match-string 2) s 0)
11870 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
11872 (setq s (match-end 0)
11873 p (match-string 1 cfmt))
11874 (unless (or (equal p "ITEM")
11875 (member p org-special-properties))
11876 (add-to-list 'rtn (match-string 1 cfmt))))))))
11878 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
11880 (defun org-property-values (key)
11881 "Return a list of all values of property KEY."
11885 (goto-char (point-min))
11886 (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
11888 (while (re-search-forward re nil t)
11889 (add-to-list 'values (org-trim (match-string 1))))
11890 (delete "" values)))))
11892 (defun org-insert-property-drawer ()
11893 "Insert a property drawer into the current entry."
11895 (org-back-to-heading t)
11896 (looking-at outline-regexp)
11897 (let ((indent (if org-adapt-indentation
11898 (- (match-end 0)(match-beginning 0))
11901 (re (concat "^[ \t]*" org-keyword-time-regexp))
11903 (outline-next-heading)
11906 (while (re-search-forward re end t))
11907 (setq hiddenp (org-invisible-p))
11909 (and (equal (char-after) ?\n) (forward-char 1))
11910 (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
11911 (if (member (match-string 1) '("CLOCK:" ":END:"))
11912 ;; just skip this line
11913 (beginning-of-line 2)
11914 ;; Drawer start, find the end
11915 (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
11916 (beginning-of-line 1)))
11917 (org-skip-over-state-notes)
11918 (skip-chars-backward " \t\n\r")
11919 (if (eq (char-before) ?*) (forward-char 1))
11920 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
11921 (beginning-of-line 0)
11922 (org-indent-to-column indent)
11923 (beginning-of-line 2)
11924 (org-indent-to-column indent)
11925 (beginning-of-line 0)
11928 (org-back-to-heading t)
11930 (org-flag-drawer t))))
11932 (defun org-set-property (property value)
11933 "In the current entry, set PROPERTY to VALUE.
11934 When called interactively, this will prompt for a property name, offering
11935 completion on existing and default properties. And then it will prompt
11936 for a value, offering completion either on allowed values (via an inherited
11937 xxx_ALL property) or on existing values in other instances of this property
11938 in the current file."
11940 (let* ((completion-ignore-case t)
11941 (keys (org-buffer-property-keys nil t t))
11942 (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
11943 (prop (if (member prop0 keys)
11945 (or (cdr (assoc (downcase prop0)
11946 (mapcar (lambda (x) (cons (downcase x) x))
11949 (cur (org-entry-get nil prop))
11950 (allowed (org-property-get-allowed-values nil prop 'table))
11951 (existing (mapcar 'list (org-property-values prop)))
11953 (org-completing-read "Value: " allowed nil 'req-match)
11954 (let (org-completion-use-ido)
11955 (org-completing-read
11956 (concat "Value" (if (and cur (string-match "\\S-" cur))
11957 (concat "[" cur "]") "")
11959 existing nil nil "" nil cur)))))
11960 (list prop (if (equal val "") cur val))))
11961 (unless (equal (org-entry-get nil property) value)
11962 (org-entry-put nil property value)))
11964 (defun org-delete-property (property)
11965 "In the current entry, delete PROPERTY."
11967 (let* ((completion-ignore-case t)
11968 (prop (org-ido-completing-read
11969 "Property: " (org-entry-properties nil 'standard))))
11971 (message "Property %s %s" property
11972 (if (org-entry-delete nil property)
11974 "was not present in the entry")))
11976 (defun org-delete-property-globally (property)
11977 "Remove PROPERTY globally, from all entries."
11979 (let* ((completion-ignore-case t)
11980 (prop (org-ido-completing-read
11981 "Globally remove property: "
11982 (mapcar 'list (org-buffer-property-keys)))))
11987 (goto-char (point-min))
11989 (while (re-search-forward
11990 (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
11992 (setq cnt (1+ cnt))
11993 (replace-match ""))
11994 (message "Property \"%s\" removed from %d entries" property cnt)))))
11996 (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
11998 (defun org-compute-property-at-point ()
11999 "Compute the property at point.
12000 This looks for an enclosing column format, extracts the operator and
12001 then applies it to the property in the column format's scope."
12003 (unless (org-at-property-p)
12004 (error "Not at a property"))
12005 (let ((prop (org-match-string-no-properties 2)))
12006 (org-columns-get-format-and-top-level)
12007 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
12008 (error "No operator defined for property %s" prop))
12009 (org-columns-compute prop)))
12011 (defun org-property-get-allowed-values (pom property &optional table)
12012 "Get allowed values for the property PROPERTY.
12013 When TABLE is non-nil, return an alist that can directly be used for
12017 ((equal property "TODO")
12018 (setq vals (org-with-point-at pom
12019 (append org-todo-keywords-1 '("")))))
12020 ((equal property "PRIORITY")
12021 (let ((n org-lowest-priority))
12022 (while (>= n org-highest-priority)
12023 (push (char-to-string n) vals)
12025 ((member property org-special-properties))
12027 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
12029 (when (and vals (string-match "\\S-" vals))
12030 (setq vals (car (read-from-string (concat "(" vals ")"))))
12031 (setq vals (mapcar (lambda (x)
12032 (cond ((stringp x) x)
12033 ((numberp x) (number-to-string x))
12034 ((symbolp x) (symbol-name x))
12037 (if table (mapcar 'list vals) vals)))
12039 (defun org-property-previous-allowed-value (&optional previous)
12040 "Switch to the next allowed value for this property."
12042 (org-property-next-allowed-value t))
12044 (defun org-property-next-allowed-value (&optional previous)
12045 "Switch to the next allowed value for this property."
12047 (unless (org-at-property-p)
12048 (error "Not at a property"))
12049 (let* ((key (match-string 2))
12050 (value (match-string 3))
12051 (allowed (or (org-property-get-allowed-values (point) key)
12052 (and (member value '("[ ]" "[-]" "[X]"))
12056 (error "Allowed values for this property have not been defined"))
12057 (if previous (setq allowed (reverse allowed)))
12058 (if (member value allowed)
12059 (setq nval (car (cdr (member value allowed)))))
12060 (setq nval (or nval (car allowed)))
12061 (if (equal nval value)
12062 (error "Only one allowed value for this property"))
12063 (org-at-property-p)
12064 (replace-match (concat " :" key ": " nval) t t)
12065 (org-indent-line-function)
12066 (beginning-of-line 1)
12067 (skip-chars-forward " \t")))
12069 (defun org-find-entry-with-id (ident)
12070 "Locate the entry that contains the ID property with exact value IDENT.
12071 IDENT can be a string, a symbol or a number, this function will search for
12072 the string representation of it.
12073 Return the position where this entry starts, or nil if there is no such entry."
12074 (interactive "sID: ")
12076 ((stringp ident) ident)
12077 ((symbol-name ident) (symbol-name ident))
12078 ((numberp ident) (number-to-string ident))
12079 (t (error "IDENT %s must be a string, symbol or number" ident))))
12080 (case-fold-search nil))
12084 (goto-char (point-min))
12085 (when (re-search-forward
12086 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
12088 (org-back-to-heading t)
12093 (defvar org-last-changed-timestamp nil)
12094 (defvar org-last-inserted-timestamp nil
12095 "The last time stamp inserted with `org-insert-time-stamp'.")
12096 (defvar org-time-was-given) ; dynamically scoped parameter
12097 (defvar org-end-time-was-given) ; dynamically scoped parameter
12098 (defvar org-ts-what) ; dynamically scoped parameter
12100 (defun org-time-stamp (arg &optional inactive)
12101 "Prompt for a date/time and insert a time stamp.
12102 If the user specifies a time like HH:MM, or if this command is called
12103 with a prefix argument, the time stamp will contain date and time.
12104 Otherwise, only the date will be included. All parts of a date not
12105 specified by the user will be filled in from the current date/time.
12106 So if you press just return without typing anything, the time stamp
12107 will represent the current date/time. If there is already a timestamp
12108 at the cursor, it will be modified."
12112 ;; Default time is either today, or, when entering a range,
12113 ;; the range start.
12114 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
12116 (re-search-backward
12117 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
12118 (- (point) 20) t)))
12119 (apply 'encode-time (org-parse-time-string (match-string 1)))
12121 (default-input (and ts (org-get-compact-tod ts)))
12122 org-time-was-given org-end-time-was-given time)
12124 ((and (org-at-timestamp-p t)
12125 (memq last-command '(org-time-stamp org-time-stamp-inactive))
12126 (memq this-command '(org-time-stamp org-time-stamp-inactive)))
12128 (setq time (let ((this-command this-command))
12129 (org-read-date arg 'totime nil nil
12130 default-time default-input)))
12131 (org-insert-time-stamp time (or org-time-was-given arg) inactive))
12132 ((org-at-timestamp-p t)
12133 (setq time (let ((this-command this-command))
12134 (org-read-date arg 'totime nil nil default-time default-input)))
12135 (when (org-at-timestamp-p t) ; just to get the match data
12136 ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
12138 (setq org-last-changed-timestamp
12139 (org-insert-time-stamp
12140 time (or org-time-was-given arg)
12141 inactive nil nil (list org-end-time-was-given))))
12142 (message "Timestamp updated"))
12144 (setq time (let ((this-command this-command))
12145 (org-read-date arg 'totime nil nil default-time default-input)))
12146 (org-insert-time-stamp time (or org-time-was-given arg) inactive
12147 nil nil (list org-end-time-was-given))))))
12149 ;; FIXME: can we use this for something else, like computing time differences?
12150 (defun org-get-compact-tod (s)
12151 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
12152 (let* ((t1 (match-string 1 s))
12153 (h1 (string-to-number (match-string 2 s)))
12154 (m1 (string-to-number (match-string 3 s)))
12155 (t2 (and (match-end 4) (match-string 5 s)))
12156 (h2 (and t2 (string-to-number (match-string 6 s))))
12157 (m2 (and t2 (string-to-number (match-string 7 s))))
12161 (setq dh (- h2 h1) dm (- m2 m1))
12162 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
12163 (concat t1 "+" (number-to-string dh)
12164 (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
12166 (defun org-time-stamp-inactive (&optional arg)
12167 "Insert an inactive time stamp.
12168 An inactive time stamp is enclosed in square brackets instead of angle
12169 brackets. It is inactive in the sense that it does not trigger agenda entries,
12170 does not link to the calendar and cannot be changed with the S-cursor keys.
12171 So these are more for recording a certain time/date."
12173 (org-time-stamp arg 'inactive))
12175 (defvar org-date-ovl (org-make-overlay 1 1))
12176 (org-overlay-put org-date-ovl 'face 'org-warning)
12177 (org-detach-overlay org-date-ovl)
12179 (defvar org-ans1) ; dynamically scoped parameter
12180 (defvar org-ans2) ; dynamically scoped parameter
12182 (defvar org-plain-time-of-day-regexp) ; defined below
12184 (defvar org-overriding-default-time nil) ; dynamically scoped
12185 (defvar org-read-date-overlay nil)
12186 (defvar org-dcst nil) ; dynamically scoped
12187 (defvar org-read-date-history nil)
12188 (defvar org-read-date-final-answer nil)
12190 (defun org-read-date (&optional with-time to-time from-string prompt
12191 default-time default-input)
12192 "Read a date, possibly a time, and make things smooth for the user.
12193 The prompt will suggest to enter an ISO date, but you can also enter anything
12194 which will at least partially be understood by `parse-time-string'.
12195 Unrecognized parts of the date will default to the current day, month, year,
12196 hour and minute. If this command is called to replace a timestamp at point,
12197 of to enter the second timestamp of a range, the default time is taken from the
12198 existing stamp. For example,
12199 3-2-5 --> 2003-02-05
12200 feb 15 --> currentyear-02-15
12201 sep 12 9 --> 2009-09-12
12202 12:45 --> today 12:45
12203 22 sept 0:34 --> currentyear-09-22 0:34
12204 12 --> currentyear-currentmonth-12
12205 Fri --> nearest Friday (today or later)
12208 Furthermore you can specify a relative date by giving, as the *first* thing
12209 in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
12210 change in days weeks, months, years.
12211 With a single plus or minus, the date is relative to today. With a double
12212 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
12213 +4d --> four days from today
12214 +4 --> same as above
12215 +2w --> two weeks from today
12216 ++5 --> five days from default date
12218 The function understands only English month and weekday abbreviations,
12219 but this can be configured with the variables `parse-time-months' and
12220 `parse-time-weekdays'.
12222 While prompting, a calendar is popped up - you can also select the
12223 date with the mouse (button 1). The calendar shows a period of three
12224 months. To scroll it to other months, use the keys `>' and `<'.
12225 If you don't like the calendar, turn it off with
12226 \(setq org-read-date-popup-calendar nil)
12228 With optional argument TO-TIME, the date will immediately be converted
12229 to an internal time.
12230 With an optional argument WITH-TIME, the prompt will suggest to also
12231 insert a time. Note that when WITH-TIME is not set, you can still
12232 enter a time, and this function will inform the calling routine about
12233 this change. The calling routine may then choose to change the format
12234 used to insert the time stamp into the buffer to include the time.
12235 With optional argument FROM-STRING, read from this string instead from
12236 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
12237 the time/date that is used for everything that is not specified by the
12239 (require 'parse-time)
12240 (let* ((org-time-stamp-rounding-minutes
12241 (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
12242 (org-dcst org-display-custom-times)
12243 (ct (org-current-time))
12244 (def (or org-overriding-default-time default-time ct))
12245 (defdecode (decode-time def))
12247 (when (< (nth 2 defdecode) org-extend-today-until)
12248 (setcar (nthcdr 2 defdecode) -1)
12249 (setcar (nthcdr 1 defdecode) 59)
12250 (setq def (apply 'encode-time defdecode)
12251 defdecode (decode-time def)))))
12252 (calendar-frame-setup nil)
12253 (calendar-move-hook nil)
12254 (calendar-view-diary-initially-flag nil)
12255 (view-diary-entries-initially nil)
12256 (calendar-view-holidays-initially-flag nil)
12257 (view-calendar-holidays-initially nil)
12258 (timestr (format-time-string
12259 (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
12260 (prompt (concat (if prompt (concat prompt " ") "")
12261 (format "Date+time [%s]: " timestr)))
12262 ans (org-ans0 "") org-ans1 org-ans2 final)
12265 (from-string (setq ans from-string))
12266 (org-read-date-popup-calendar
12268 (save-window-excursion
12270 (calendar-forward-day (- (time-to-days def)
12271 (calendar-absolute-from-gregorian
12272 (calendar-current-date))))
12273 (org-eval-in-calendar nil t)
12274 (let* ((old-map (current-local-map))
12275 (map (copy-keymap calendar-mode-map))
12276 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
12277 (org-defkey map (kbd "RET") 'org-calendar-select)
12278 (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
12279 'org-calendar-select-mouse)
12280 (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2
])
12281 'org-calendar-select-mouse
)
12282 (org-defkey minibuffer-local-map
[(meta shift left
)]
12283 (lambda () (interactive)
12284 (org-eval-in-calendar '(calendar-backward-month 1))))
12285 (org-defkey minibuffer-local-map
[(meta shift right
)]
12286 (lambda () (interactive)
12287 (org-eval-in-calendar '(calendar-forward-month 1))))
12288 (org-defkey minibuffer-local-map
[(meta shift up
)]
12289 (lambda () (interactive)
12290 (org-eval-in-calendar '(calendar-backward-year 1))))
12291 (org-defkey minibuffer-local-map
[(meta shift down
)]
12292 (lambda () (interactive)
12293 (org-eval-in-calendar '(calendar-forward-year 1))))
12294 (org-defkey minibuffer-local-map
[?\e
(shift left
)]
12295 (lambda () (interactive)
12296 (org-eval-in-calendar '(calendar-backward-month 1))))
12297 (org-defkey minibuffer-local-map
[?\e
(shift right
)]
12298 (lambda () (interactive)
12299 (org-eval-in-calendar '(calendar-forward-month 1))))
12300 (org-defkey minibuffer-local-map
[?\e
(shift up
)]
12301 (lambda () (interactive)
12302 (org-eval-in-calendar '(calendar-backward-year 1))))
12303 (org-defkey minibuffer-local-map
[?\e
(shift down
)]
12304 (lambda () (interactive)
12305 (org-eval-in-calendar '(calendar-forward-year 1))))
12306 (org-defkey minibuffer-local-map
[(shift up
)]
12307 (lambda () (interactive)
12308 (org-eval-in-calendar '(calendar-backward-week 1))))
12309 (org-defkey minibuffer-local-map
[(shift down
)]
12310 (lambda () (interactive)
12311 (org-eval-in-calendar '(calendar-forward-week 1))))
12312 (org-defkey minibuffer-local-map
[(shift left
)]
12313 (lambda () (interactive)
12314 (org-eval-in-calendar '(calendar-backward-day 1))))
12315 (org-defkey minibuffer-local-map
[(shift right
)]
12316 (lambda () (interactive)
12317 (org-eval-in-calendar '(calendar-forward-day 1))))
12318 (org-defkey minibuffer-local-map
">"
12319 (lambda () (interactive)
12320 (org-eval-in-calendar '(scroll-calendar-left 1))))
12321 (org-defkey minibuffer-local-map
"<"
12322 (lambda () (interactive)
12323 (org-eval-in-calendar '(scroll-calendar-right 1))))
12324 (run-hooks 'org-read-date-minibuffer-setup-hook
)
12327 (use-local-map map
)
12328 (add-hook 'post-command-hook
'org-read-date-display
)
12329 (setq org-ans0
(read-string prompt default-input
12330 'org-read-date-history nil
))
12331 ;; org-ans0: from prompt
12332 ;; org-ans1: from mouse click
12333 ;; org-ans2: from calendar motion
12334 (setq ans
(concat org-ans0
" " (or org-ans1 org-ans2
))))
12335 (remove-hook 'post-command-hook
'org-read-date-display
)
12336 (use-local-map old-map
)
12337 (when org-read-date-overlay
12338 (org-delete-overlay org-read-date-overlay
)
12339 (setq org-read-date-overlay nil
)))))))
12341 (t ; Naked prompt only
12343 (setq ans
(read-string prompt default-input
12344 'org-read-date-history timestr
))
12345 (when org-read-date-overlay
12346 (org-delete-overlay org-read-date-overlay
)
12347 (setq org-read-date-overlay nil
)))))
12349 (setq final
(org-read-date-analyze ans def defdecode
))
12350 (setq org-read-date-final-answer ans
)
12353 (apply 'encode-time final
)
12354 (if (and (boundp 'org-time-was-given
) org-time-was-given
)
12355 (format "%04d-%02d-%02d %02d:%02d"
12356 (nth 5 final
) (nth 4 final
) (nth 3 final
)
12357 (nth 2 final
) (nth 1 final
))
12358 (format "%04d-%02d-%02d" (nth 5 final
) (nth 4 final
) (nth 3 final
))))))
12363 (defun org-read-date-display ()
12364 "Display the current date prompt interpretation in the minibuffer."
12365 (when org-read-date-display-live
12366 (when org-read-date-overlay
12367 (org-delete-overlay org-read-date-overlay
))
12370 (while (not (equal (buffer-substring
12371 (max (point-min) (- (point) 4)) (point))
12375 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
12376 " " (or org-ans1 org-ans2
)))
12377 (org-end-time-was-given nil
)
12378 (f (org-read-date-analyze ans def defdecode
))
12380 org-time-stamp-custom-formats
12381 org-time-stamp-formats
))
12382 (fmt (if (or with-time
12383 (and (boundp 'org-time-was-given
) org-time-was-given
))
12386 (txt (concat "=> " (format-time-string fmt
(apply 'encode-time f
)))))
12387 (when (and org-end-time-was-given
12388 (string-match org-plain-time-of-day-regexp txt
))
12389 (setq txt
(concat (substring txt
0 (match-end 0)) "-"
12390 org-end-time-was-given
12391 (substring txt
(match-end 0)))))
12392 (setq org-read-date-overlay
12393 (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
12394 (org-overlay-display org-read-date-overlay txt
'secondary-selection
))))
12396 (defun org-read-date-analyze (ans def defdecode
)
12397 "Analyse the combined answer of the date prompt."
12398 ;; FIXME: cleanup and comment
12399 (let (delta deltan deltaw deltadef year month day
12400 hour minute second wday pm h2 m2 tl wday1
12401 iso-year iso-weekday iso-week iso-year iso-date
)
12403 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans
)
12406 (when (setq delta
(org-read-date-get-relative ans
(current-time) def
))
12407 (setq ans
(replace-match "" t t ans
)
12409 deltaw
(nth 1 delta
)
12410 deltadef
(nth 2 delta
)))
12412 ;; Check if there is an iso week date in there
12413 ;; If yes, sore the info and postpone interpreting it until the rest
12414 ;; of the parsing is done
12415 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans
)
12416 (setq iso-year
(if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans
))))
12417 iso-weekday
(if (match-end 3) (string-to-number (match-string 3 ans
)))
12418 iso-week
(string-to-number (match-string 2 ans
)))
12419 (setq ans
(replace-match "" t t ans
)))
12421 ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
12422 (when (string-match
12423 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans
)
12424 (setq year
(if (match-end 2)
12425 (string-to-number (match-string 2 ans
))
12426 (string-to-number (format-time-string "%Y")))
12427 month
(string-to-number (match-string 3 ans
))
12428 day
(string-to-number (match-string 4 ans
)))
12429 (if (< year
100) (setq year
(+ 2000 year
)))
12430 (setq ans
(replace-match (format "%04d-%02d-%02d\\5" year month day
)
12432 ;; Help matching am/pm times, because `parse-time-string' does not do that.
12433 ;; If there is a time with am/pm, and *no* time without it, we convert
12434 ;; so that matching will be successful.
12435 (loop for i from
1 to
2 do
; twice, for end time as well
12436 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans
))
12437 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans
))
12438 (setq hour
(string-to-number (match-string 1 ans
))
12439 minute
(if (match-end 3)
12440 (string-to-number (match-string 3 ans
))
12443 (string-to-char (downcase (match-string 4 ans
)))))
12444 (if (and (= hour
12) (not pm
))
12446 (if (and pm
(< hour
12)) (setq hour
(+ 12 hour
))))
12447 (setq ans
(replace-match (format "%02d:%02d" hour minute
)
12450 ;; Check if a time range is given as a duration
12451 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans
)
12452 (setq hour
(string-to-number (match-string 1 ans
))
12453 h2
(+ hour
(string-to-number (match-string 3 ans
)))
12454 minute
(string-to-number (match-string 2 ans
))
12455 m2
(+ minute
(if (match-end 5) (string-to-number
12456 (match-string 5 ans
))0)))
12457 (if (>= m2
60) (setq h2
(1+ h2
) m2
(- m2
60)))
12458 (setq ans
(replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2
)
12461 ;; Check if there is a time range
12462 (when (boundp 'org-end-time-was-given
)
12463 (setq org-time-was-given nil
)
12464 (when (and (string-match org-plain-time-of-day-regexp ans
)
12466 (setq org-end-time-was-given
(match-string 8 ans
))
12467 (setq ans
(concat (substring ans
0 (match-beginning 7))
12468 (substring ans
(match-end 7))))))
12470 (setq tl
(parse-time-string ans
)
12471 day
(or (nth 3 tl
) (nth 3 defdecode
))
12472 month
(or (nth 4 tl
)
12473 (if (and org-read-date-prefer-future
12474 (nth 3 tl
) (< (nth 3 tl
) (nth 3 defdecode
)))
12475 (1+ (nth 4 defdecode
))
12476 (nth 4 defdecode
)))
12477 year
(or (nth 5 tl
)
12478 (if (and org-read-date-prefer-future
12479 (nth 4 tl
) (< (nth 4 tl
) (nth 4 defdecode
)))
12480 (1+ (nth 5 defdecode
))
12481 (nth 5 defdecode
)))
12482 hour
(or (nth 2 tl
) (nth 2 defdecode
))
12483 minute
(or (nth 1 tl
) (nth 1 defdecode
))
12484 second
(or (nth 0 tl
) 0)
12487 ;; Special date definitions below
12490 ;; There was an iso week
12491 (setq year
(or iso-year year
)
12492 day
(or iso-weekday wday
1)
12493 wday nil
; to make sure that the trigger below does not match
12494 iso-date
(calendar-gregorian-from-absolute
12495 (calendar-absolute-from-iso
12496 (list iso-week day year
))))
12497 ; FIXME: Should we also push ISO weeks into the future?
12498 ; (when (and org-read-date-prefer-future
12500 ; (< (calendar-absolute-from-gregorian iso-date)
12501 ; (time-to-days (current-time))))
12502 ; (setq year (1+ year)
12503 ; iso-date (calendar-gregorian-from-absolute
12504 ; (calendar-absolute-from-iso
12505 ; (list iso-week day year)))))
12506 (setq month
(car iso-date
)
12507 year
(nth 2 iso-date
)
12508 day
(nth 1 iso-date
)))
12511 (let ((now (decode-time (current-time))))
12512 (setq day
(nth 3 now
) month
(nth 4 now
) year
(nth 5 now
))))
12513 (cond ((member deltaw
'("d" "")) (setq day
(+ day deltan
)))
12514 ((equal deltaw
"w") (setq day
(+ day
(* 7 deltan
))))
12515 ((equal deltaw
"m") (setq month
(+ month deltan
)))
12516 ((equal deltaw
"y") (setq year
(+ year deltan
)))))
12517 ((and wday
(not (nth 3 tl
)))
12518 ;; Weekday was given, but no day, so pick that day in the week
12519 ;; on or after the derived date.
12520 (setq wday1
(nth 6 (decode-time (encode-time 0 0 0 day month year
))))
12521 (unless (equal wday wday1
)
12522 (setq day
(+ day
(%
(- wday wday1 -
7) 7))))))
12523 (if (and (boundp 'org-time-was-given
)
12525 (setq org-time-was-given t
))
12526 (if (< year
100) (setq year
(+ 2000 year
)))
12527 (if (< year
1970) (setq year
(nth 5 defdecode
))) ; not representable
12528 (list second minute hour day month year
)))
12530 (defvar parse-time-weekdays
)
12532 (defun org-read-date-get-relative (s today default
)
12533 "Check string S for special relative date string.
12534 TODAY and DEFAULT are internal times, for today and for a default.
12535 Return shift list (N what def-flag)
12536 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
12537 N is the number of WHATs to shift.
12538 DEF-FLAG is t when a double ++ or -- indicates shift relative to
12539 the DEFAULT date rather than TODAY."
12543 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
12545 "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays
"\\|") "\\)\\)?"
12546 "\\([ \t]\\|$\\)") s
)
12547 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
12548 (let* ((dir (if (> (match-end 1) (match-beginning 1))
12549 (string-to-char (substring (match-string 1 s
) -
1))
12551 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
12552 (n (if (match-end 2) (string-to-number (match-string 2 s
)) 1))
12553 (what (if (match-end 3) (match-string 3 s
) "d"))
12554 (wday1 (cdr (assoc (downcase what
) parse-time-weekdays
)))
12555 (date (if rel default today
))
12556 (wday (nth 6 (decode-time date
)))
12560 (setq delta
(mod (+ 7 (- wday1 wday
)) 7))
12561 (if (= dir ?-
) (setq delta
(- delta
7)))
12562 (if (> n
1) (setq delta
(+ delta
(* (1- n
) (if (= dir ?-
) -
7 7)))))
12563 (list delta
"d" rel
))
12564 (list (* n
(if (= dir ?-
) -
1 1)) what rel
)))))
12566 (defun org-eval-in-calendar (form &optional keepdate
)
12567 "Eval FORM in the calendar window and return to current window.
12568 Also, store the cursor date in variable org-ans2."
12569 (let ((sf (selected-frame))
12570 (sw (selected-window)))
12571 (select-window (get-buffer-window "*Calendar*" t
))
12573 (when (and (not keepdate
) (calendar-cursor-to-date))
12574 (let* ((date (calendar-cursor-to-date))
12575 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
12576 (setq org-ans2
(format-time-string "%Y-%m-%d" time
))))
12577 (org-move-overlay org-date-ovl
(1- (point)) (1+ (point)) (current-buffer))
12579 (select-frame-set-input-focus sf
)))
12581 (defun org-calendar-select ()
12582 "Return to `org-read-date' with the date currently selected.
12583 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
12585 (when (calendar-cursor-to-date)
12586 (let* ((date (calendar-cursor-to-date))
12587 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
12588 (setq org-ans1
(format-time-string "%Y-%m-%d" time
)))
12589 (if (active-minibuffer-window) (exit-minibuffer))))
12591 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra
)
12592 "Insert a date stamp for the date given by the internal TIME.
12593 WITH-HM means, use the stamp format that includes the time of the day.
12594 INACTIVE means use square brackets instead of angular ones, so that the
12595 stamp will not contribute to the agenda.
12596 PRE and POST are optional strings to be inserted before and after the
12598 The command returns the inserted time stamp."
12599 (let ((fmt (funcall (if with-hm
'cdr
'car
) org-time-stamp-formats
))
12601 (if inactive
(setq fmt
(concat "[" (substring fmt
1 -
1) "]")))
12602 (insert-before-markers (or pre
""))
12603 (insert-before-markers (setq stamp
(format-time-string fmt time
)))
12604 (when (listp extra
)
12605 (setq extra
(car extra
))
12606 (if (and (stringp extra
)
12607 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra
))
12608 (setq extra
(format "-%02d:%02d"
12609 (string-to-number (match-string 1 extra
))
12610 (string-to-number (match-string 2 extra
))))
12614 (insert-before-markers extra
)
12616 (insert-before-markers (or post
""))
12617 (setq org-last-inserted-timestamp stamp
)))
12619 (defun org-toggle-time-stamp-overlays ()
12620 "Toggle the use of custom time stamp formats."
12622 (setq org-display-custom-times
(not org-display-custom-times
))
12623 (unless org-display-custom-times
12624 (let ((p (point-min)) (bmp (buffer-modified-p)))
12625 (while (setq p
(next-single-property-change p
'display
))
12626 (if (and (get-text-property p
'display
)
12627 (eq (get-text-property p
'face
) 'org-date
))
12628 (remove-text-properties
12629 p
(setq p
(next-single-property-change p
'display
))
12631 (set-buffer-modified-p bmp
)))
12632 (if (featurep 'xemacs
)
12633 (remove-text-properties (point-min) (point-max) '(end-glyph t
)))
12634 (org-restart-font-lock)
12635 (setq org-table-may-need-update t
)
12636 (if org-display-custom-times
12637 (message "Time stamps are overlayed with custom format")
12638 (message "Time stamp overlays removed")))
12640 (defun org-display-custom-time (beg end
)
12641 "Overlay modified time stamp format over timestamp between BEG and END."
12642 (let* ((ts (buffer-substring beg end
))
12643 t1 w1 with-hm tf time str w2
(off 0))
12645 (setq t1
(org-parse-time-string ts t
))
12646 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts
)
12647 (setq off
(- (match-end 0) (match-beginning 0)))))
12648 (setq end
(- end off
))
12649 (setq w1
(- end beg
)
12650 with-hm
(and (nth 1 t1
) (nth 2 t1
))
12651 tf
(funcall (if with-hm
'cdr
'car
) org-time-stamp-custom-formats
)
12652 time
(org-fix-decoded-time t1
)
12654 (format-time-string
12655 (substring tf
1 -
1) (apply 'encode-time time
))
12656 nil
'mouse-face
'highlight
)
12658 (if (not (= w2 w1
))
12659 (add-text-properties (1+ beg
) (+ 2 beg
)
12660 (list 'org-dwidth t
'org-dwidth-n
(- w1 w2
))))
12661 (if (featurep 'xemacs
)
12663 (put-text-property beg end
'invisible t
)
12664 (put-text-property beg end
'end-glyph
(make-glyph str
)))
12665 (put-text-property beg end
'display str
))))
12667 (defun org-translate-time (string)
12668 "Translate all timestamps in STRING to custom format.
12669 But do this only if the variable `org-display-custom-times' is set."
12670 (when org-display-custom-times
12673 (re org-ts-regexp-both
)
12674 t1 with-hm inactive tf time str beg end
)
12675 (while (setq start
(string-match re string start
))
12676 (setq beg
(match-beginning 0)
12678 t1
(save-match-data
12679 (org-parse-time-string (substring string beg end
) t
))
12680 with-hm
(and (nth 1 t1
) (nth 2 t1
))
12681 inactive
(equal (substring string beg
(1+ beg
)) "[")
12682 tf
(funcall (if with-hm
'cdr
'car
)
12683 org-time-stamp-custom-formats
)
12684 time
(org-fix-decoded-time t1
)
12685 str
(format-time-string
12687 (if inactive
"[" "<") (substring tf
1 -
1)
12688 (if inactive
"]" ">"))
12689 (apply 'encode-time time
))
12690 string
(replace-match str t t string
)
12691 start
(+ start
(length str
)))))))
12694 (defun org-fix-decoded-time (time)
12695 "Set 0 instead of nil for the first 6 elements of time.
12696 Don't touch the rest."
12698 (mapcar (lambda (x) (if (< (setq n
(1+ n
)) 7) (or x
0) x
)) time
)))
12700 (defun org-days-to-time (timestamp-string)
12701 "Difference between TIMESTAMP-STRING and now in days."
12702 (- (time-to-days (org-time-string-to-time timestamp-string
))
12703 (time-to-days (current-time))))
12705 (defun org-deadline-close (timestamp-string &optional ndays
)
12706 "Is the time in TIMESTAMP-STRING close to the current date?"
12707 (setq ndays
(or ndays
(org-get-wdays timestamp-string
)))
12708 (and (< (org-days-to-time timestamp-string
) ndays
)
12709 (not (org-entry-is-done-p))))
12711 (defun org-get-wdays (ts)
12712 "Get the deadline lead time appropriate for timestring TS."
12714 ((<= org-deadline-warning-days
0)
12715 ;; 0 or negative, enforce this value no matter what
12716 (- org-deadline-warning-days
))
12717 ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts
)
12718 ;; lead time is specified.
12719 (floor (* (string-to-number (match-string 1 ts
))
12720 (cdr (assoc (match-string 2 ts
)
12721 '(("d" .
1) ("w" .
7)
12722 ("m" .
30.4) ("y" .
365.25)))))))
12723 ;; go for the default.
12724 (t org-deadline-warning-days
)))
12726 (defun org-calendar-select-mouse (ev)
12727 "Return to `org-read-date' with the date currently selected.
12728 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
12730 (mouse-set-point ev
)
12731 (when (calendar-cursor-to-date)
12732 (let* ((date (calendar-cursor-to-date))
12733 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
12734 (setq org-ans1
(format-time-string "%Y-%m-%d" time
)))
12735 (if (active-minibuffer-window) (exit-minibuffer))))
12737 (defun org-check-deadlines (ndays)
12738 "Check if there are any deadlines due or past due.
12739 A deadline is considered due if it happens within `org-deadline-warning-days'
12740 days from today's date. If the deadline appears in an entry marked DONE,
12741 it is not shown. The prefix arg NDAYS can be used to test that many
12742 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
12744 (let* ((org-warn-days
12746 ((equal ndays
'(4)) 100000)
12747 (ndays (prefix-numeric-value ndays
))
12748 (t (abs org-deadline-warning-days
))))
12749 (case-fold-search nil
)
12750 (regexp (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>"))
12752 (lambda () (org-deadline-close (match-string 1) org-warn-days
))))
12754 (message "%d deadlines past-due or due within %d days"
12755 (org-occur regexp nil callback
)
12758 (defun org-check-before-date (date)
12759 "Check if there are deadlines or scheduled entries before DATE."
12760 (interactive (list (org-read-date)))
12761 (let ((case-fold-search nil
)
12762 (regexp (concat "\\<\\(" org-deadline-string
12763 "\\|" org-scheduled-string
12764 "\\) *<\\([^>]+\\)>"))
12766 (lambda () (time-less-p
12767 (org-time-string-to-time (match-string 2))
12768 (org-time-string-to-time date
)))))
12769 (message "%d entries before %s"
12770 (org-occur regexp nil callback
) date
)))
12772 (defun org-check-after-date (date)
12773 "Check if there are deadlines or scheduled entries after DATE."
12774 (interactive (list (org-read-date)))
12775 (let ((case-fold-search nil
)
12776 (regexp (concat "\\<\\(" org-deadline-string
12777 "\\|" org-scheduled-string
12778 "\\) *<\\([^>]+\\)>"))
12782 (org-time-string-to-time (match-string 2))
12783 (org-time-string-to-time date
))))))
12784 (message "%d entries after %s"
12785 (org-occur regexp nil callback
) date
)))
12787 (defun org-evaluate-time-range (&optional to-buffer
)
12788 "Evaluate a time range by computing the difference between start and end.
12789 Normally the result is just printed in the echo area, but with prefix arg
12790 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
12791 If the time range is actually in a table, the result is inserted into the
12793 For time difference computation, a year is assumed to be exactly 365
12794 days in order to avoid rounding problems."
12797 (org-clock-update-time-maybe)
12799 (unless (org-at-date-range-p t
)
12800 (goto-char (point-at-bol))
12801 (re-search-forward org-tr-regexp-both
(point-at-eol) t
))
12802 (if (not (org-at-date-range-p t
))
12803 (error "Not at a time-stamp range, and none found in current line")))
12804 (let* ((ts1 (match-string 1))
12805 (ts2 (match-string 2))
12806 (havetime (or (> (length ts1
) 15) (> (length ts2
) 15)))
12807 (match-end (match-end 0))
12808 (time1 (org-time-string-to-time ts1
))
12809 (time2 (org-time-string-to-time ts2
))
12810 (t1 (time-to-seconds time1
))
12811 (t2 (time-to-seconds time2
))
12812 (diff (abs (- t2 t1
)))
12813 (negative (< (- t2 t1
) 0))
12814 ;; (ys (floor (* 365 24 60 60)))
12817 (fy "%dy %dd %02d:%02d")
12819 (fd "%dd %02d:%02d")
12824 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
12826 d
(floor (/ diff ds
)) diff
(mod diff ds
)
12827 h
(floor (/ diff hs
)) diff
(mod diff hs
)
12828 m
(floor (/ diff
60)))
12829 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
12831 d
(floor (+ (/ diff ds
) 0.5))
12833 (if (not to-buffer
)
12834 (message "%s" (org-make-tdiff-string y d h m
))
12835 (if (org-at-table-p)
12837 (goto-char match-end
)
12839 (and (looking-at " *|") (goto-char (match-end 0))))
12840 (goto-char match-end
))
12842 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
12843 (replace-match ""))
12844 (if negative
(insert " -"))
12845 (if (> y
0) (insert " " (format (if havetime fy fy1
) y d h m
))
12846 (if (> d
0) (insert " " (format (if havetime fd fd1
) d h m
))
12847 (insert " " (format fh h m
))))
12848 (if align
(org-table-align))
12849 (message "Time difference inserted")))))
12851 (defun org-make-tdiff-string (y d h m
)
12854 (if (> y
0) (setq fmt
(concat fmt
"%d year" (if (> y
1) "s" "") " ")
12856 (if (> d
0) (setq fmt
(concat fmt
"%d day" (if (> d
1) "s" "") " ")
12858 (if (> h
0) (setq fmt
(concat fmt
"%d hour" (if (> h
1) "s" "") " ")
12860 (if (> m
0) (setq fmt
(concat fmt
"%d minute" (if (> m
1) "s" "") " ")
12862 (apply 'format fmt
(nreverse l
))))
12864 (defun org-time-string-to-time (s)
12865 (apply 'encode-time
(org-parse-time-string s
)))
12866 (defun org-time-string-to-seconds (s)
12867 (time-to-seconds (org-time-string-to-time s
)))
12869 (defun org-time-string-to-absolute (s &optional daynr prefer show-all
)
12870 "Convert a time stamp to an absolute day number.
12871 If there is a specifyer for a cyclic time stamp, get the closest date to
12873 PREFER and SHOW-ALL are passed through to `org-closest-date'.
12874 the variable date is bound by the calendar when this is called."
12876 ((and daynr
(string-match "\\`%%\\((.*)\\)" s
))
12877 (if (org-diary-sexp-entry (match-string 1 s
) "" date
)
12880 ((and daynr
(string-match "\\+[0-9]+[dwmy]" s
))
12881 (org-closest-date s
(if (and (boundp 'daynr
) (integerp daynr
)) daynr
12882 (time-to-days (current-time))) (match-string 0 s
)
12884 (t (time-to-days (apply 'encode-time
(org-parse-time-string s
))))))
12886 (defun org-days-to-iso-week (days)
12887 "Return the iso week number."
12889 (car (calendar-iso-from-absolute days
)))
12891 (defun org-small-year-to-year (year)
12892 "Convert 2-digit years into 4-digit years.
12893 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
12894 The year 2000 cannot be abbreviated. Any year larger than 99
12895 is returned unchanged."
12897 (setq year
(+ 2000 year
))
12899 (setq year
(+ 1900 year
))))
12902 (defun org-time-from-absolute (d)
12903 "Return the time corresponding to date D.
12904 D may be an absolute day number, or a calendar-type list (month day year)."
12905 (if (numberp d
) (setq d
(calendar-gregorian-from-absolute d
)))
12906 (encode-time 0 0 0 (nth 1 d
) (car d
) (nth 2 d
)))
12908 (defun org-calendar-holiday ()
12909 "List of holidays, for Diary display in Org-mode."
12910 (require 'holidays
)
12912 (if (fboundp 'calendar-check-holidays
)
12913 'calendar-check-holidays
'check-calendar-holidays
) date
)))
12914 (if hl
(mapconcat 'identity hl
"; "))))
12916 (defun org-diary-sexp-entry (sexp entry date
)
12917 "Process a SEXP diary ENTRY for DATE."
12918 (require 'diary-lib
)
12919 (let ((result (if calendar-debug-sexp
12920 (let ((stack-trace-on-error t
))
12921 (eval (car (read-from-string sexp
))))
12922 (condition-case nil
12923 (eval (car (read-from-string sexp
)))
12926 (message "Bad sexp at line %d in %s: %s"
12928 (buffer-file-name) sexp
)
12930 (cond ((stringp result
) result
)
12931 ((and (consp result
)
12932 (stringp (cdr result
))) (cdr result
))
12936 (defun org-diary-to-ical-string (frombuf)
12937 "Get iCalendar entries from diary entries in buffer FROMBUF.
12938 This uses the icalendar.el library."
12939 (let* ((tmpdir (if (featurep 'xemacs
)
12941 temporary-file-directory
))
12942 (tmpfile (make-temp-name
12943 (expand-file-name "orgics" tmpdir
)))
12946 (set-buffer frombuf
)
12947 (icalendar-export-region (point-min) (point-max) tmpfile
)
12948 (setq buf
(find-buffer-visiting tmpfile
))
12950 (goto-char (point-min))
12951 (if (re-search-forward "^BEGIN:VEVENT" nil t
)
12952 (setq b
(match-beginning 0)))
12953 (goto-char (point-max))
12954 (if (re-search-backward "^END:VEVENT" nil t
)
12955 (setq e
(match-end 0)))
12956 (setq rtn
(if (and b e
) (concat (buffer-substring b e
) "\n") "")))
12958 (delete-file tmpfile
)
12961 (defun org-closest-date (start current change prefer show-all
)
12962 "Find the date closest to CURRENT that is consistent with START and CHANGE.
12963 When PREFER is `past' return a date that is either CURRENT or past.
12964 When PREFER is `future', return a date that is either CURRENT or future.
12965 When SHOW-ALL is nil, only return the current occurrence of a time stamp."
12966 ;; Make the proper lists from the dates
12968 (let ((a1 '(("d" . day
) ("w" . week
) ("m" . month
) ("y" . year
)))
12969 dn dw sday cday n1 n2 n0
12970 d m y y1 y2 date1 date2 nmonths nm ny m2
)
12972 (setq start
(org-date-to-gregorian start
)
12973 current
(org-date-to-gregorian
12976 (time-to-days (current-time))))
12977 sday
(calendar-absolute-from-gregorian start
)
12978 cday
(calendar-absolute-from-gregorian current
))
12980 (if (<= cday sday
) (throw 'exit sday
))
12982 (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change
)
12983 (setq dn
(string-to-number (match-string 1 change
))
12984 dw
(cdr (assoc (match-string 2 change
) a1
)))
12985 (error "Invalid change specifyer: %s" change
))
12986 (if (eq dw
'week
) (setq dw
'day dn
(* 7 dn
)))
12989 (setq n1
(+ sday
(* dn
(floor (/ (- cday sday
) dn
))))
12992 (setq d
(nth 1 start
) m
(car start
) y1
(nth 2 start
) y2
(nth 2 current
))
12993 (setq y1
(+ (* (floor (/ (- y2 y1
) dn
)) dn
) y1
))
12994 (setq date1
(list m d y1
)
12995 n1
(calendar-absolute-from-gregorian date1
)
12996 date2
(list m d
(+ y1
(* (if (< n1 cday
) 1 -
1) dn
)))
12997 n2
(calendar-absolute-from-gregorian date2
)))
12999 ;; approx number of month between the two dates
13000 (setq nmonths
(floor (/ (- cday sday
) 30.436875)))
13001 ;; How often does dn fit in there?
13002 (setq d
(nth 1 start
) m
(car start
) y
(nth 2 start
)
13003 nm
(* dn
(max 0 (1- (floor (/ nmonths dn
)))))
13005 ny
(floor (/ m
12))
13008 (while (> m
12) (setq m
(- m
12) y
(1+ y
)))
13009 (setq n1
(calendar-absolute-from-gregorian (list m d y
)))
13010 (setq m2
(+ m dn
) y2 y
)
13011 (if (> m2
12) (setq y2
(1+ y2
) m2
(- m2
12)))
13012 (setq n2
(calendar-absolute-from-gregorian (list m2 d y2
)))
13013 (while (<= n2 cday
)
13014 (setq n1 n2 m m2 y y2
)
13015 (setq m2
(+ m dn
) y2 y
)
13016 (if (> m2
12) (setq y2
(1+ y2
) m2
(- m2
12)))
13017 (setq n2
(calendar-absolute-from-gregorian (list m2 d y2
))))))
13018 ;; Make sure n1 is the earlier date
13019 (setq n0 n1 n1
(min n1 n2
) n2
(max n0 n2
))
13022 ((eq prefer
'past
) n1
)
13023 ((eq prefer
'future
) (if (= cday n1
) n1 n2
))
13024 (t (if (> (abs (- cday n1
)) (abs (- cday n2
))) n2 n1
)))
13026 ((eq prefer
'past
) n1
)
13027 ((eq prefer
'future
) (if (= cday n1
) n1 n2
))
13028 (t (if (= cday n1
) n1 n2
)))))))
13030 (defun org-date-to-gregorian (date)
13031 "Turn any specification of DATE into a gregorian date for the calendar."
13032 (cond ((integerp date
) (calendar-gregorian-from-absolute date
))
13033 ((and (listp date
) (= (length date
) 3)) date
)
13035 (setq date
(org-parse-time-string date
))
13036 (list (nth 4 date
) (nth 3 date
) (nth 5 date
)))
13038 (list (nth 4 date
) (nth 3 date
) (nth 5 date
)))))
13040 (defun org-parse-time-string (s &optional nodefault
)
13041 "Parse the standard Org-mode time string.
13042 This should be a lot faster than the normal `parse-time-string'.
13043 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
13044 hour and minute fields will be nil if not given."
13045 (if (string-match org-ts-regexp0 s
)
13047 (if (or (match-beginning 8) (not nodefault
))
13048 (string-to-number (or (match-string 8 s
) "0")))
13049 (if (or (match-beginning 7) (not nodefault
))
13050 (string-to-number (or (match-string 7 s
) "0")))
13051 (string-to-number (match-string 4 s
))
13052 (string-to-number (match-string 3 s
))
13053 (string-to-number (match-string 2 s
))
13057 (defun org-timestamp-up (&optional arg
)
13058 "Increase the date item at the cursor by one.
13059 If the cursor is on the year, change the year. If it is on the month or
13060 the day, change that.
13061 With prefix ARG, change by that many units."
13063 (org-timestamp-change (prefix-numeric-value arg
)))
13065 (defun org-timestamp-down (&optional arg
)
13066 "Decrease the date item at the cursor by one.
13067 If the cursor is on the year, change the year. If it is on the month or
13068 the day, change that.
13069 With prefix ARG, change by that many units."
13071 (org-timestamp-change (- (prefix-numeric-value arg
))))
13073 (defun org-timestamp-up-day (&optional arg
)
13074 "Increase the date in the time stamp by one day.
13075 With prefix ARG, change that many days."
13077 (if (and (not (org-at-timestamp-p t
))
13078 (org-on-heading-p))
13080 (org-timestamp-change (prefix-numeric-value arg
) 'day
)))
13082 (defun org-timestamp-down-day (&optional arg
)
13083 "Decrease the date in the time stamp by one day.
13084 With prefix ARG, change that many days."
13086 (if (and (not (org-at-timestamp-p t
))
13087 (org-on-heading-p))
13089 (org-timestamp-change (- (prefix-numeric-value arg
)) 'day
)))
13091 (defun org-at-timestamp-p (&optional inactive-ok
)
13092 "Determine if the cursor is in or at a timestamp."
13094 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2
))
13096 (ans (or (looking-at tsr
)
13098 (skip-chars-backward "^[<\n\r\t")
13099 (if (> (point) (point-min)) (backward-char 1))
13100 (and (looking-at tsr
)
13101 (> (- (match-end 0) pos
) -
1))))))
13103 (boundp 'org-ts-what
)
13106 ((= pos
(match-beginning 0)) 'bracket
)
13107 ((= pos
(1- (match-end 0))) 'bracket
)
13108 ((org-pos-in-match-range pos
2) 'year
)
13109 ((org-pos-in-match-range pos
3) 'month
)
13110 ((org-pos-in-match-range pos
7) 'hour
)
13111 ((org-pos-in-match-range pos
8) 'minute
)
13112 ((or (org-pos-in-match-range pos
4)
13113 (org-pos-in-match-range pos
5)) 'day
)
13114 ((and (> pos
(or (match-end 8) (match-end 5)))
13115 (< pos
(match-end 0)))
13116 (- pos
(or (match-end 8) (match-end 5))))
13120 (defun org-toggle-timestamp-type ()
13121 "Toggle the type (<active> or [inactive]) of a time stamp."
13123 (when (org-at-timestamp-p t
)
13124 (let ((beg (match-beginning 0)) (end (match-end 0))
13125 (map '((?\
[ .
"<") (?\
] .
">") (?
< .
"[") (?
> .
"]"))))
13128 (while (re-search-forward "[][<>]" end t
)
13129 (replace-match (cdr (assoc (char-after (match-beginning 0)) map
))
13131 (message "Timestamp is now %sactive"
13132 (if (equal (char-after beg
) ?
<) "" "in")))))
13134 (defun org-timestamp-change (n &optional what
)
13135 "Change the date in the time stamp at point.
13136 The date will be changed by N times WHAT. WHAT can be `day', `month',
13137 `year', `minute', `second'. If WHAT is not given, the cursor position
13138 in the timestamp determines what will be changed."
13139 (let ((pos (point))
13141 (dm (max (nth 1 org-time-stamp-rounding-minutes
) 1))
13145 (if (not (org-at-timestamp-p t
))
13146 (error "Not at a timestamp"))
13147 (if (and (not what
) (eq org-ts-what
'bracket
))
13148 (org-toggle-timestamp-type)
13149 (if (and (not what
) (not (eq org-ts-what
'day
))
13150 org-display-custom-times
13151 (get-text-property (point) 'display
)
13152 (not (get-text-property (1- (point)) 'display
)))
13153 (setq org-ts-what
'day
))
13154 (setq org-ts-what
(or what org-ts-what
)
13155 inactive
(= (char-after (match-beginning 0)) ?\
[)
13156 ts
(match-string 0))
13159 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
13161 (setq extra
(match-string 1 ts
)))
13162 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts
)
13164 (setq time0
(org-parse-time-string ts
))
13165 (when (and (eq org-ts-what
'minute
)
13166 (eq current-prefix-arg nil
))
13167 (setq n
(* dm
(cond ((> n
0) 1) ((< n
0) -
1) (t 0))))
13168 (when (not (= 0 (setq rem
(%
(nth 1 time0
) dm
))))
13169 (setcar (cdr time0
) (+ (nth 1 time0
)
13170 (if (> n
0) (- rem
) (- dm rem
))))))
13172 (encode-time (or (car time0
) 0)
13173 (+ (if (eq org-ts-what
'minute
) n
0) (nth 1 time0
))
13174 (+ (if (eq org-ts-what
'hour
) n
0) (nth 2 time0
))
13175 (+ (if (eq org-ts-what
'day
) n
0) (nth 3 time0
))
13176 (+ (if (eq org-ts-what
'month
) n
0) (nth 4 time0
))
13177 (+ (if (eq org-ts-what
'year
) n
0) (nth 5 time0
))
13179 (when (and (member org-ts-what
'(hour minute
))
13181 (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra
))
13182 (setq extra
(org-modify-ts-extra
13184 (if (eq org-ts-what
'hour
) 2 5)
13186 (when (integerp org-ts-what
)
13187 (setq extra
(org-modify-ts-extra extra org-ts-what n dm
)))
13188 (if (eq what
'calendar
)
13189 (let ((cal-date (org-get-date-from-calendar)))
13190 (setcar (nthcdr 4 time0
) (nth 0 cal-date
)) ; month
13191 (setcar (nthcdr 3 time0
) (nth 1 cal-date
)) ; day
13192 (setcar (nthcdr 5 time0
) (nth 2 cal-date
)) ; year
13193 (setcar time0
(or (car time0
) 0))
13194 (setcar (nthcdr 1 time0
) (or (nth 1 time0
) 0))
13195 (setcar (nthcdr 2 time0
) (or (nth 2 time0
) 0))
13196 (setq time
(apply 'encode-time time0
))))
13197 (setq org-last-changed-timestamp
13198 (org-insert-time-stamp time with-hm inactive nil nil extra
))
13199 (org-clock-update-time-maybe)
13201 ;; Try to recenter the calendar window, if any
13202 (if (and org-calendar-follow-timestamp-change
13203 (get-buffer-window "*Calendar*" t
)
13204 (memq org-ts-what
'(day month year
)))
13205 (org-recenter-calendar (time-to-days time
))))))
13207 (defun org-modify-ts-extra (s pos n dm
)
13208 "Change the different parts of the lead-time and repeat fields in timestamp."
13209 (let ((idx '(("d" .
0) ("w" .
1) ("m" .
2) ("y" .
3) ("d" . -
1) ("y" .
4)))
13211 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s
)
13213 ((or (org-pos-in-match-range pos
2)
13214 (org-pos-in-match-range pos
3))
13215 (setq m
(string-to-number (match-string 3 s
))
13216 h
(string-to-number (match-string 2 s
)))
13217 (if (org-pos-in-match-range pos
2)
13219 (setq n
(* dm
(org-no-warnings (signum n
))))
13220 (when (not (= 0 (setq rem
(% m dm
))))
13221 (setq m
(+ m
(if (> n
0) (- rem
) (- dm rem
)))))
13223 (if (< m
0) (setq m
(+ m
60) h
(1- h
)))
13224 (if (> m
59) (setq m
(- m
60) h
(1+ h
)))
13225 (setq h
(min 24 (max 0 h
)))
13226 (setq ng
1 new
(format "-%02d:%02d" h m
)))
13227 ((org-pos-in-match-range pos
6)
13228 (setq ng
6 new
(car (rassoc (+ n
(cdr (assoc (match-string 6 s
) idx
))) idx
))))
13229 ((org-pos-in-match-range pos
5)
13230 (setq ng
5 new
(format "%d" (max 1 (+ n
(string-to-number (match-string 5 s
)))))))
13232 ((org-pos-in-match-range pos
9)
13233 (setq ng
9 new
(car (rassoc (+ n
(cdr (assoc (match-string 9 s
) idx
))) idx
))))
13234 ((org-pos-in-match-range pos
8)
13235 (setq ng
8 new
(format "%d" (max 0 (+ n
(string-to-number (match-string 8 s
))))))))
13239 (substring s
0 (match-beginning ng
))
13241 (substring s
(match-end ng
))))))
13244 (defun org-recenter-calendar (date)
13245 "If the calendar is visible, recenter it to DATE."
13246 (let* ((win (selected-window))
13247 (cwin (get-buffer-window "*Calendar*" t
))
13248 (calendar-move-hook nil
))
13250 (select-window cwin
)
13251 (calendar-goto-date (if (listp date
) date
13252 (calendar-gregorian-from-absolute date
)))
13253 (select-window win
))))
13255 (defun org-goto-calendar (&optional arg
)
13256 "Go to the Emacs calendar at the current date.
13257 If there is a time stamp in the current line, go to that date.
13258 A prefix ARG can be used to force the current date."
13260 (let ((tsr org-ts-regexp
) diff
13261 (calendar-move-hook nil
)
13262 (calendar-view-holidays-initially-flag nil
)
13263 (view-calendar-holidays-initially nil
)
13264 (calendar-view-diary-initially-flag nil
)
13265 (view-diary-entries-initially nil
))
13266 (if (or (org-at-timestamp-p)
13268 (beginning-of-line 1)
13269 (looking-at (concat ".*" tsr
))))
13270 (let ((d1 (time-to-days (current-time)))
13272 (org-time-string-to-time (match-string 1)))))
13273 (setq diff
(- d2 d1
))))
13275 (calendar-goto-today)
13276 (if (and diff
(not arg
)) (calendar-forward-day diff
))))
13278 (defun org-get-date-from-calendar ()
13279 "Return a list (month day year) of date at point in calendar."
13280 (with-current-buffer "*Calendar*"
13282 (calendar-cursor-to-date))))
13284 (defun org-date-from-calendar ()
13285 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
13286 If there is already a time stamp at the cursor position, update it."
13288 (if (org-at-timestamp-p t
)
13289 (org-timestamp-change 0 'calendar
)
13290 (let ((cal-date (org-get-date-from-calendar)))
13291 (org-insert-time-stamp
13292 (encode-time 0 0 0 (nth 1 cal-date
) (car cal-date
) (nth 2 cal-date
))))))
13294 (defun org-minutes-to-hh:mm-string
(m)
13295 "Compute H:MM from a number of minutes."
13296 (let ((h (/ m
60)))
13297 (setq m
(- m
(* 60 h
)))
13298 (format org-time-clocksum-format h m
)))
13300 (defun org-hh:mm-string-to-minutes
(s)
13301 "Convert a string H:MM to a number of minutes.
13302 If the string is just a number, interprete it as minutes.
13303 In fact, the first hh:mm or number in the string will be taken,
13304 there can be extra stuff in the string.
13305 If no number is found, the return value is 0."
13307 ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s
)
13308 (+ (* (string-to-number (match-string 1 s
)) 60)
13309 (string-to-number (match-string 2 s
))))
13310 ((string-match "\\([0-9]+\\)" s
)
13311 (string-to-number (match-string 1 s
)))
13316 (defun org-save-all-org-buffers ()
13317 "Save all Org-mode buffers without user confirmation."
13319 (message "Saving all Org-mode buffers...")
13320 (save-some-buffers t
'org-mode-p
)
13321 (when (featurep 'org-id
) (org-id-locations-save))
13322 (message "Saving all Org-mode buffers... done"))
13324 (defun org-revert-all-org-buffers ()
13325 "Revert all Org-mode buffers.
13326 Prompt for confirmation when there are unsaved changes.
13327 Be sure you know what you are doing before letting this function
13328 overwrite your changes.
13330 This function is useful in a setup where one tracks org files
13331 with a version control system, to revert on one machine after pulling
13332 changes from another. I believe the procedure must be like this:
13334 1. M-x org-save-all-org-buffers
13335 2. Pull changes from the other machine, resolve conflicts
13336 3. M-x org-revert-all-org-buffers"
13338 (unless (yes-or-no-p "Revert all Org buffers from their files? ")
13341 (save-window-excursion
13344 (when (and (with-current-buffer b
(org-mode-p))
13345 (with-current-buffer b buffer-file-name
))
13346 (switch-to-buffer b
)
13347 (revert-buffer t
'no-confirm
)))
13349 (when (and (featurep 'org-id
) org-id-track-globally
)
13350 (org-id-locations-load)))))
13355 (defun org-iswitchb (&optional arg
)
13356 "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
13357 With a prefix argument, restrict available to files.
13358 With two prefix arguments, restrict available buffers to agenda files.
13360 Due to some yet unresolved reason, the global function
13361 `iswitchb-mode' needs to be active for this function to work."
13363 (require 'iswitchb
)
13364 (let ((enabled iswitchb-mode
) blist
)
13365 (or enabled
(iswitchb-mode 1))
13366 (setq blist
(cond ((equal arg
'(4)) (org-buffer-list 'files
))
13367 ((equal arg
'(16)) (org-buffer-list 'agenda
))
13368 (t (org-buffer-list))))
13370 (let ((iswitchb-make-buflist-hook
13372 (setq iswitchb-temp-buflist
13373 (mapcar 'buffer-name blist
)))))
13375 (iswitchb-read-buffer
13376 "Switch-to: " nil t
))
13377 (or enabled
(iswitchb-mode -
1))))))
13380 (defun org-ido-switchb (&optional arg
)
13381 "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
13382 With a prefix argument, restrict available to files.
13383 With two prefix arguments, restrict available buffers to agenda files."
13385 (let ((blist (cond ((equal arg
'(4)) (org-buffer-list 'files
))
13386 ((equal arg
'(16)) (org-buffer-list 'agenda
))
13387 (t (org-buffer-list)))))
13389 (org-ido-completing-read "Org buffer: "
13390 (mapcar 'list
(mapcar 'buffer-name blist
))
13393 (defun org-buffer-list (&optional predicate exclude-tmp
)
13394 "Return a list of Org buffers.
13395 PREDICATE can be `export', `files' or `agenda'.
13397 export restrict the list to Export buffers.
13398 files restrict the list to buffers visiting Org files.
13399 agenda restrict the list to buffers visiting agenda files.
13401 If EXCLUDE-TMP is non-nil, ignore temporary buffers."
13403 (agenda-files (and (eq predicate
'agenda
)
13404 (mapcar 'file-truename
(org-agenda-files t
))))
13407 ((eq predicate
'files
)
13408 (lambda (b) (with-current-buffer b
(eq major-mode
'org-mode
))))
13409 ((eq predicate
'export
)
13410 (lambda (b) (string-match "\*Org .*Export" (buffer-name b
))))
13411 ((eq predicate
'agenda
)
13413 (with-current-buffer b
13414 (and (eq major-mode
'org-mode
)
13415 (setq bfn
(buffer-file-name b
))
13416 (member (file-truename bfn
) agenda-files
)))))
13417 (t (lambda (b) (with-current-buffer b
13418 (or (eq major-mode
'org-mode
)
13419 (string-match "\*Org .*Export"
13420 (buffer-name b
)))))))))
13424 (if (and (funcall filter b
)
13425 (or (not exclude-tmp
)
13426 (not (string-match "tmp" (buffer-name b
)))))
13431 (defun org-agenda-files (&optional unrestricted archives
)
13432 "Get the list of agenda files.
13433 Optional UNRESTRICTED means return the full list even if a restriction
13434 is currently in place.
13435 When ARCHIVES is t, include all archive files hat are really being
13436 used by the agenda files. If ARCHIVE is `ifmode', do this only if
13437 `org-agenda-archives-mode' is t."
13440 ((and (not unrestricted
) (get 'org-agenda-files
'org-restrict
)))
13441 ((stringp org-agenda-files
) (org-read-agenda-file-list))
13442 ((listp org-agenda-files
) org-agenda-files
)
13443 (t (error "Invalid value of `org-agenda-files'")))))
13444 (setq files
(apply 'append
13445 (mapcar (lambda (f)
13446 (if (file-directory-p f
)
13448 f t org-agenda-file-regexp
)
13451 (when org-agenda-skip-unavailable-files
13452 (setq files
(delq nil
13455 (and (file-readable-p file
) file
)))
13457 (when (or (eq archives t
)
13458 (and (eq archives
'ifmode
) (eq org-agenda-archives-mode t
)))
13459 (setq files
(org-add-archive-files files
)))
13462 (defun org-edit-agenda-file-list ()
13463 "Edit the list of agenda files.
13464 Depending on setup, this either uses customize to edit the variable
13465 `org-agenda-files', or it visits the file that is holding the list. In the
13466 latter case, the buffer is set up in a way that saving it automatically kills
13467 the buffer and restores the previous window configuration."
13469 (if (stringp org-agenda-files
)
13470 (let ((cw (current-window-configuration)))
13471 (find-file org-agenda-files
)
13472 (org-set-local 'org-window-configuration cw
)
13473 (org-add-hook 'after-save-hook
13475 (set-window-configuration
13476 (prog1 org-window-configuration
13477 (kill-buffer (current-buffer))))
13478 (org-install-agenda-files-menu)
13479 (message "New agenda file list installed"))
13481 (message "%s" (substitute-command-keys
13482 "Edit list and finish with \\[save-buffer]")))
13483 (customize-variable 'org-agenda-files
)))
13485 (defun org-store-new-agenda-file-list (list)
13486 "Set new value for the agenda file list and save it correctly."
13487 (if (stringp org-agenda-files
)
13488 (let ((f org-agenda-files
) b
)
13489 (while (setq b
(find-buffer-visiting f
)) (kill-buffer b
))
13491 (insert (mapconcat 'identity list
"\n") "\n")))
13492 (let ((org-mode-hook nil
) (default-major-mode 'fundamental-mode
))
13493 (setq org-agenda-files list
)
13494 (customize-save-variable 'org-agenda-files org-agenda-files
))))
13496 (defun org-read-agenda-file-list ()
13497 "Read the list of agenda files from a file."
13498 (when (file-directory-p org-agenda-files
)
13499 (error "`org-agenda-files' cannot be a single directory"))
13500 (when (stringp org-agenda-files
)
13502 (insert-file-contents org-agenda-files
)
13503 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
13507 (defun org-cycle-agenda-files ()
13508 "Cycle through the files in `org-agenda-files'.
13509 If the current buffer visits an agenda file, find the next one in the list.
13510 If the current buffer does not, find the first agenda file."
13512 (let* ((fs (org-agenda-files t
))
13513 (files (append fs
(list (car fs
))))
13514 (tcf (if buffer-file-name
(file-truename buffer-file-name
)))
13516 (unless files
(error "No agenda files"))
13518 (while (setq file
(pop files
))
13519 (if (equal (file-truename file
) tcf
)
13521 (find-file (car files
))
13523 (find-file (car fs
)))
13524 (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
13526 (defun org-agenda-file-to-front (&optional to-end
)
13527 "Move/add the current file to the top of the agenda file list.
13528 If the file is not present in the list, it is added to the front. If it is
13529 present, it is moved there. With optional argument TO-END, add/move to the
13532 (let ((org-agenda-skip-unavailable-files nil
)
13533 (file-alist (mapcar (lambda (x)
13534 (cons (file-truename x
) x
))
13535 (org-agenda-files t
)))
13536 (ctf (file-truename buffer-file-name
))
13538 (setq x
(assoc ctf file-alist
) had x
)
13540 (if (not x
) (setq x
(cons ctf
(abbreviate-file-name buffer-file-name
))))
13542 (setq file-alist
(append (delq x file-alist
) (list x
)))
13543 (setq file-alist
(cons x
(delq x file-alist
))))
13544 (org-store-new-agenda-file-list (mapcar 'cdr file-alist
))
13545 (org-install-agenda-files-menu)
13546 (message "File %s to %s of agenda file list"
13547 (if had
"moved" "added") (if to-end
"end" "front"))))
13549 (defun org-remove-file (&optional file
)
13550 "Remove current file from the list of files in variable `org-agenda-files'.
13551 These are the files which are being checked for agenda entries.
13552 Optional argument FILE means, use this file instead of the current."
13554 (let* ((org-agenda-skip-unavailable-files nil
)
13555 (file (or file buffer-file-name
))
13556 (true-file (file-truename file
))
13557 (afile (abbreviate-file-name file
))
13558 (files (delq nil
(mapcar
13560 (if (equal true-file
13563 (org-agenda-files t
)))))
13564 (if (not (= (length files
) (length (org-agenda-files t
))))
13566 (org-store-new-agenda-file-list files
)
13567 (org-install-agenda-files-menu)
13568 (message "Removed file: %s" afile
))
13569 (message "File was not in list: %s (not removed)" afile
))))
13571 (defun org-file-menu-entry (file)
13572 (vector file
(list 'find-file file
) t
))
13574 (defun org-check-agenda-file (file)
13575 "Make sure FILE exists. If not, ask user what to do."
13576 (when (not (file-exists-p file
))
13577 (message "non-existent file %s. [R]emove from list or [A]bort?"
13578 (abbreviate-file-name file
))
13579 (let ((r (downcase (read-char-exclusive))))
13582 (org-remove-file file
)
13583 (throw 'nextfile t
))
13584 (t (error "Abort"))))))
13586 (defun org-get-agenda-file-buffer (file)
13587 "Get a buffer visiting FILE. If the buffer needs to be created, add
13588 it to the list of buffers which might be released later."
13589 (let ((buf (org-find-base-buffer-visiting file
)))
13591 buf
; just return it
13592 ;; Make a new buffer and remember it
13593 (setq buf
(find-file-noselect file
))
13594 (if buf
(push buf org-agenda-new-buffers
))
13597 (defun org-release-buffers (blist)
13598 "Release all buffers in list, asking the user for confirmation when needed.
13599 When a buffer is unmodified, it is just killed. When modified, it is saved
13600 \(if the user agrees) and then killed."
13602 (while (setq buf
(pop blist
))
13603 (setq file
(buffer-file-name buf
))
13604 (when (and (buffer-modified-p buf
)
13606 (y-or-n-p (format "Save file %s? " file
)))
13607 (with-current-buffer buf
(save-buffer)))
13608 (kill-buffer buf
))))
13610 (defun org-prepare-agenda-buffers (files)
13611 "Create buffers for all agenda files, protect archived trees and comments."
13613 (let ((pa '(:org-archived t
))
13614 (pc '(:org-comment t
))
13615 (pall '(:org-archived t
:org-comment t
))
13616 (inhibit-read-only t
)
13617 (rea (concat ":" org-archive-tag
":"))
13621 (while (setq file
(pop files
))
13625 (org-check-agenda-file file
)
13626 (set-buffer (org-get-agenda-file-buffer file
)))
13628 (setq bmp
(buffer-modified-p))
13629 (org-refresh-category-properties)
13630 (setq org-todo-keywords-for-agenda
13631 (append org-todo-keywords-for-agenda org-todo-keywords-1
))
13632 (setq org-done-keywords-for-agenda
13633 (append org-done-keywords-for-agenda org-done-keywords
))
13634 (setq org-todo-keyword-alist-for-agenda
13635 (append org-todo-keyword-alist-for-agenda org-todo-key-alist
))
13636 (setq org-tag-alist-for-agenda
13637 (append org-tag-alist-for-agenda org-tag-alist
))
13640 (remove-text-properties (point-min) (point-max) pall
)
13641 (when org-agenda-skip-archived-trees
13642 (goto-char (point-min))
13643 (while (re-search-forward rea nil t
)
13644 (if (org-on-heading-p t
)
13645 (add-text-properties (point-at-bol) (org-end-of-subtree t
) pa
))))
13646 (goto-char (point-min))
13647 (setq re
(concat "^\\*+ +" org-comment-string
"\\>"))
13648 (while (re-search-forward re nil t
)
13649 (add-text-properties
13650 (match-beginning 0) (org-end-of-subtree t
) pc
)))
13651 (set-buffer-modified-p bmp
)))))
13652 (setq org-todo-keyword-alist-for-agenda
13653 (org-uniquify org-todo-keyword-alist-for-agenda
)
13654 org-tag-alist-for-agenda
(org-uniquify org-tag-alist-for-agenda
))))
13656 ;;;; Embedded LaTeX
13658 (defvar org-cdlatex-mode-map
(make-sparse-keymap)
13659 "Keymap for the minor `org-cdlatex-mode'.")
13661 (org-defkey org-cdlatex-mode-map
"_" 'org-cdlatex-underscore-caret
)
13662 (org-defkey org-cdlatex-mode-map
"^" 'org-cdlatex-underscore-caret
)
13663 (org-defkey org-cdlatex-mode-map
"`" 'cdlatex-math-symbol
)
13664 (org-defkey org-cdlatex-mode-map
"'" 'org-cdlatex-math-modify
)
13665 (org-defkey org-cdlatex-mode-map
"\C-c{" 'cdlatex-environment
)
13667 (defvar org-cdlatex-texmathp-advice-is-done nil
13668 "Flag remembering if we have applied the advice to texmathp already.")
13670 (define-minor-mode org-cdlatex-mode
13671 "Toggle the minor `org-cdlatex-mode'.
13672 This mode supports entering LaTeX environment and math in LaTeX fragments
13674 \\{org-cdlatex-mode-map}"
13676 (when org-cdlatex-mode
(require 'cdlatex
))
13677 (unless org-cdlatex-texmathp-advice-is-done
13678 (setq org-cdlatex-texmathp-advice-is-done t
)
13679 (defadvice texmathp
(around org-math-always-on activate
)
13680 "Always return t in org-mode buffers.
13681 This is because we want to insert math symbols without dollars even outside
13682 the LaTeX math segments. If Orgmode thinks that point is actually inside
13683 an embedded LaTeX fragment, let texmathp do its job.
13684 \\[org-cdlatex-mode-map]"
13688 ((not (org-mode-p)) ad-do-it
)
13689 ((eq this-command
'cdlatex-math-symbol
)
13690 (setq ad-return-value t
13691 texmathp-why
'("cdlatex-math-symbol in org-mode" .
0)))
13693 (let ((p (org-inside-LaTeX-fragment-p)))
13694 (if (and p
(member (car p
) (plist-get org-format-latex-options
:matchers
)))
13695 (setq ad-return-value t
13696 texmathp-why
'("Org-mode embedded math" .
0))
13697 (if p ad-do-it
)))))))))
13699 (defun turn-on-org-cdlatex ()
13700 "Unconditionally turn on `org-cdlatex-mode'."
13701 (org-cdlatex-mode 1))
13703 (defun org-inside-LaTeX-fragment-p ()
13704 "Test if point is inside a LaTeX fragment.
13705 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
13706 sequence appearing also before point.
13707 Even though the matchers for math are configurable, this function assumes
13708 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
13709 delimiters are skipped when they have been removed by customization.
13710 The return value is nil, or a cons cell with the delimiter and
13711 and the position of this delimiter.
13713 This function does a reasonably good job, but can locally be fooled by
13714 for example currency specifications. For example it will assume being in
13715 inline math after \"$22.34\". The LaTeX fragment formatter will only format
13716 fragments that are properly closed, but during editing, we have to live
13717 with the uncertainty caused by missing closing delimiters. This function
13718 looks only before point, not after."
13720 (let ((pos (point))
13721 (dodollar (member "$" (plist-get org-format-latex-options
:matchers
)))
13723 (re-search-backward (concat "^\\(" paragraph-start
"\\)") nil t
)
13725 dd-on str
(start 0) m re
)
13728 (setq str
(concat (buffer-substring lim
(point)) "\000 X$.")
13729 re
(nth 1 (assoc "$" org-latex-regexps
)))
13730 (while (string-match re str start
)
13732 ((= (match-end 0) (length str
))
13733 (throw 'exit
(cons "$" (+ lim
(match-beginning 0) 1))))
13734 ((= (match-end 0) (- (length str
) 5))
13736 (t (setq start
(match-end 0))))))
13737 (when (setq m
(re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t
))
13739 (and (match-beginning 1) (throw 'exit
(cons (match-string 1) m
)))
13740 (and (match-beginning 2) (throw 'exit nil
))
13742 (while (re-search-backward "\\$\\$" lim t
)
13743 (setq dd-on
(not dd-on
)))
13745 (if dd-on
(cons "$$" m
))))))
13748 (defun org-try-cdlatex-tab ()
13749 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
13750 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
13751 - inside a LaTeX fragment, or
13752 - after the first word in a line, where an abbreviation expansion could
13753 insert a LaTeX environment."
13754 (when org-cdlatex-mode
13757 (skip-chars-backward "a-zA-Z0-9*")
13758 (skip-chars-backward " \t")
13761 ((org-inside-LaTeX-fragment-p)
13765 (defun org-cdlatex-underscore-caret (&optional arg
)
13766 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
13767 Revert to the normal definition outside of these fragments."
13769 (if (org-inside-LaTeX-fragment-p)
13770 (call-interactively 'cdlatex-sub-superscript
)
13771 (let (org-cdlatex-mode)
13772 (call-interactively (key-binding (vector last-input-event
))))))
13774 (defun org-cdlatex-math-modify (&optional arg
)
13775 "Execute `cdlatex-math-modify' in LaTeX fragments.
13776 Revert to the normal definition outside of these fragments."
13778 (if (org-inside-LaTeX-fragment-p)
13779 (call-interactively 'cdlatex-math-modify
)
13780 (let (org-cdlatex-mode)
13781 (call-interactively (key-binding (vector last-input-event
))))))
13783 (defvar org-latex-fragment-image-overlays nil
13784 "List of overlays carrying the images of latex fragments.")
13785 (make-variable-buffer-local 'org-latex-fragment-image-overlays
)
13787 (defun org-remove-latex-fragment-image-overlays ()
13788 "Remove all overlays with LaTeX fragment images in current buffer."
13789 (mapc 'org-delete-overlay org-latex-fragment-image-overlays
)
13790 (setq org-latex-fragment-image-overlays nil
))
13792 (defun org-preview-latex-fragment (&optional subtree
)
13793 "Preview the LaTeX fragment at point, or all locally or globally.
13794 If the cursor is in a LaTeX fragment, create the image and overlay
13795 it over the source code. If there is no fragment at point, display
13796 all fragments in the current text, from one headline to the next. With
13797 prefix SUBTREE, display all fragments in the current subtree. With a
13798 double prefix `C-u C-u', or when the cursor is before the first headline,
13799 display all fragments in the buffer.
13800 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
13802 (org-remove-latex-fragment-image-overlays)
13805 (let (beg end at msg
)
13807 ((or (equal subtree
'(16))
13808 (not (save-excursion
13809 (re-search-backward (concat "^" outline-regexp
) nil t
))))
13810 (setq beg
(point-min) end
(point-max)
13811 msg
"Creating images for buffer...%s"))
13812 ((equal subtree
'(4))
13813 (org-back-to-heading)
13814 (setq beg
(point) end
(org-end-of-subtree t
)
13815 msg
"Creating images for subtree...%s"))
13817 (if (setq at
(org-inside-LaTeX-fragment-p))
13818 (goto-char (max (point-min) (- (cdr at
) 2)))
13819 (org-back-to-heading))
13820 (setq beg
(point) end
(progn (outline-next-heading) (point))
13821 msg
(if at
"Creating image...%s"
13822 "Creating images for entry...%s"))))
13824 (narrow-to-region beg end
)
13827 (concat "ltxpng/" (file-name-sans-extension
13828 (file-name-nondirectory
13829 buffer-file-name
)))
13830 default-directory
'overlays msg at
'forbuffer
)
13831 (message msg
"done. Use `C-c C-c' to remove images.")))))
13833 (defvar org-latex-regexps
13834 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t
)
13835 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
13836 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
13837 ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil
)
13838 ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil
)
13839 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil
)
13840 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t
)
13841 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t
))
13842 "Regular expressions for matching embedded LaTeX.")
13844 (defun org-format-latex (prefix &optional dir overlays msg at forbuffer
)
13845 "Replace LaTeX fragments with links to an image, and produce images."
13846 (if (and overlays
(fboundp 'clear-image-cache
)) (clear-image-cache))
13847 (let* ((prefixnodir (file-name-nondirectory prefix
))
13848 (absprefix (expand-file-name prefix dir
))
13849 (todir (file-name-directory absprefix
))
13850 (opt org-format-latex-options
)
13851 (matchers (plist-get opt
:matchers
))
13852 (re-list org-latex-regexps
)
13853 (cnt 0) txt link beg end re e checkdir
13854 executables-checked
13855 m n block linkfile movefile ov
)
13856 ;; Check if there are old images files with this prefix, and remove them
13857 (when (file-directory-p todir
)
13861 (concat (regexp-quote prefixnodir
) "_[0-9]+\\.png$"))))
13862 ;; Check the different regular expressions
13863 (while (setq e
(pop re-list
))
13864 (setq m
(car e
) re
(nth 1 e
) n
(nth 2 e
)
13865 block
(if (nth 3 e
) "\n\n" ""))
13866 (when (member m matchers
)
13867 (goto-char (point-min))
13868 (while (re-search-forward re nil t
)
13869 (when (and (or (not at
) (equal (cdr at
) (match-beginning n
)))
13870 (not (get-text-property (match-beginning n
)
13872 (setq txt
(match-string n
)
13873 beg
(match-beginning n
) end
(match-end n
)
13875 linkfile
(format "%s_%04d.png" prefix cnt
)
13876 movefile
(format "%s_%04d.png" absprefix cnt
)
13877 link
(concat block
"[[file:" linkfile
"]]" block
))
13878 (if msg
(message msg cnt
))
13880 (unless checkdir
; make sure the directory exists
13882 (or (file-directory-p todir
) (make-directory todir
)))
13884 (unless executables-checked
13885 (org-check-external-command
13886 "latex" "needed to convert LaTeX fragments to images")
13887 (org-check-external-command
13888 "dvipng" "needed to convert LaTeX fragments to images")
13889 (setq executables-checked t
))
13891 (org-create-formula-image
13892 txt movefile opt forbuffer
)
13895 (setq ov
(org-make-overlay beg end
))
13896 (if (featurep 'xemacs
)
13898 (org-overlay-put ov
'invisible t
)
13901 (make-glyph (vector 'png
:file movefile
))))
13904 (list 'image
:type
'png
:file movefile
:ascent
'center
)))
13905 (push ov org-latex-fragment-image-overlays
)
13907 (delete-region beg end
)
13908 (insert link
))))))))
13910 ;; This function borrows from Ganesh Swami's latex2png.el
13911 (defun org-create-formula-image (string tofile options buffer
)
13912 (let* ((tmpdir (if (featurep 'xemacs
)
13914 temporary-file-directory
))
13915 (texfilebase (make-temp-name
13916 (expand-file-name "orgtex" tmpdir
)))
13917 (texfile (concat texfilebase
".tex"))
13918 (dvifile (concat texfilebase
".dvi"))
13919 (pngfile (concat texfilebase
".png"))
13920 (fnh (if (featurep 'xemacs
)
13921 (font-height (get-face-font 'default
))
13922 (face-attribute 'default
:height nil
)))
13923 (scale (or (plist-get options
(if buffer
:scale
:html-scale
)) 1.0))
13924 (dpi (number-to-string (* scale
(floor (* 0.9 (if buffer fnh
140.
))))))
13925 (fg (or (plist-get options
(if buffer
:foreground
:html-foreground
))
13927 (bg (or (plist-get options
(if buffer
:background
:html-background
))
13929 (if (eq fg
'default
) (setq fg
(org-dvipng-color :foreground
)))
13930 (if (eq bg
'default
) (setq bg
(org-dvipng-color :background
)))
13931 (with-temp-file texfile
13932 (insert org-format-latex-header
13933 "\n\\begin{document}\n" string
"\n\\end{document}\n"))
13934 (let ((dir default-directory
))
13935 (condition-case nil
13938 (call-process "latex" nil nil nil texfile
))
13941 (if (not (file-exists-p dvifile
))
13942 (progn (message "Failed to create dvi file from %s" texfile
) nil
)
13943 (condition-case nil
13944 (call-process "dvipng" nil nil nil
13945 "-E" "-fg" fg
"-bg" bg
13947 ;;"-x" scale "-y" scale
13952 (if (not (file-exists-p pngfile
))
13953 (progn (message "Failed to create png file from %s" texfile
) nil
)
13954 ;; Use the requested file name and clean up
13955 (copy-file pngfile tofile
'replace
)
13956 (loop for e in
'(".dvi" ".tex" ".aux" ".log" ".png") do
13957 (delete-file (concat texfilebase e
)))
13960 (defun org-dvipng-color (attr)
13961 "Return an rgb color specification for dvipng."
13962 (apply 'format
"rgb %s %s %s"
13963 (mapcar 'org-normalize-color
13964 (color-values (face-attribute 'default attr nil
)))))
13966 (defun org-normalize-color (value)
13967 "Return string to be used as color value for an RGB component."
13968 (format "%g" (/ value
65535.0)))
13972 ;; Make `C-c C-x' a prefix key
13973 (org-defkey org-mode-map
"\C-c\C-x" (make-sparse-keymap))
13975 ;; TAB key with modifiers
13976 (org-defkey org-mode-map
"\C-i" 'org-cycle
)
13977 (org-defkey org-mode-map
[(tab)] 'org-cycle
)
13978 (org-defkey org-mode-map
[(control tab
)] 'org-force-cycle-archived
)
13979 (org-defkey org-mode-map
[(meta tab
)] 'org-complete
)
13980 (org-defkey org-mode-map
"\M-\t" 'org-complete
)
13981 (org-defkey org-mode-map
"\M-\C-i" 'org-complete
)
13982 ;; The following line is necessary under Suse GNU/Linux
13983 (unless (featurep 'xemacs
)
13984 (org-defkey org-mode-map
[S-iso-lefttab
] 'org-shifttab
))
13985 (org-defkey org-mode-map
[(shift tab
)] 'org-shifttab
)
13986 (define-key org-mode-map
[backtab] 'org-shifttab)
13988 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
13989 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
13990 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
13992 ;; Cursor keys with modifiers
13993 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
13994 (org-defkey org-mode-map [(meta right)] 'org-metaright)
13995 (org-defkey org-mode-map [(meta up)] 'org-metaup)
13996 (org-defkey org-mode-map [(meta down)] 'org-metadown)
13998 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
13999 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
14000 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
14001 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
14003 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
14004 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
14005 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
14006 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
14008 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
14009 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
14011 ;;; Extra keys for tty access.
14012 ;; We only set them when really needed because otherwise the
14013 ;; menus don't show the simple keys
14015 (when (or org-use-extra-keys
14016 (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
14017 (not window-system))
14018 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
14019 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
14020 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
14021 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
14022 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
14023 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
14024 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
14025 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
14026 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
14027 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
14028 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
14029 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
14030 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
14031 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
14032 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
14033 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
14034 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
14035 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
14036 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
14037 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
14038 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
14039 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
14040 (org-defkey org-mode-map [?\e (tab)] 'org-complete)
14041 (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
14042 (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
14043 (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
14044 (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
14045 (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
14047 ;; All the other keys
14049 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
14050 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
14051 (if (boundp 'narrow-map)
14052 (org-defkey narrow-map "s" 'org-narrow-to-subtree)
14053 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
14054 (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
14055 (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
14056 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
14057 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
14058 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
14059 (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
14060 (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
14061 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
14062 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
14063 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
14064 (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
14065 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
14066 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
14067 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
14068 (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
14069 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
14070 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
14071 (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
14072 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
14073 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
14074 (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
14075 (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
14076 (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
14077 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
14078 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
14079 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
14080 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
14081 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
14082 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
14083 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
14084 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
14085 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
14086 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
14087 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
14088 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
14089 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
14090 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
14091 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
14092 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
14093 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
14094 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
14095 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
14096 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
14097 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
14098 (org-defkey org-mode-map "\C-c^" 'org-sort)
14099 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
14100 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
14101 (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
14102 (org-defkey org-mode-map "\C-m" 'org-return)
14103 (org-defkey org-mode-map "\C-j" 'org-return-indent)
14104 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
14105 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
14106 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
14107 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
14108 (org-defkey org-mode-map "\C-c'" 'org-edit-special)
14109 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
14110 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
14111 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
14112 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
14113 (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
14114 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
14115 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
14116 (org-defkey org-mode-map "\C-c\C-e" 'org-export)
14117 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
14118 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
14119 (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
14120 (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
14121 ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
14123 (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
14124 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
14125 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
14126 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
14128 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
14129 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
14130 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
14131 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
14132 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
14133 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
14134 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
14135 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
14136 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
14137 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
14138 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
14139 (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
14140 (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
14141 (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
14143 (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
14144 (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
14145 (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
14146 (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
14148 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
14150 (define-key org-mode-map "\C-c\C-x!" 'org-reload)
14152 (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
14153 (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
14155 (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
14158 (when (featurep 'xemacs)
14159 (org-defkey org-mode-map 'button3 'popup-mode-menu))
14162 (defvar org-self-insert-command-undo-counter 0)
14164 (defvar org-table-auto-blank-field) ; defined in org-table.el
14165 (defun org-self-insert-command (N)
14166 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
14167 If the cursor is in a table looking at whitespace, the whitespace is
14168 overwritten, and the table is not marked as requiring realignment."
14173 ;; check if we blank the field, and if that triggers align
14174 (and (featurep 'org-table) org-table-auto-blank-field
14175 (member last-command
14176 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
14177 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
14178 ;; got extra space, this field does not determine column width
14179 (let (org-table-may-need-update) (org-table-blank-field))
14180 ;; no extra space, this field may determine column width
14181 (org-table-blank-field)))
14184 (looking-at "[^|\n]* |"))
14185 (let (org-table-may-need-update)
14186 (goto-char (1- (match-end 0)))
14187 (delete-backward-char 1)
14188 (goto-char (match-beginning 0))
14189 (self-insert-command N))
14190 (setq org-table-may-need-update t)
14191 (self-insert-command N)
14192 (org-fix-tags-on-the-fly)
14193 (if org-self-insert-cluster-for-undo
14194 (if (not (eq last-command 'org-self-insert-command))
14195 (setq org-self-insert-command-undo-counter 1)
14196 (if (>= org-self-insert-command-undo-counter 20)
14197 (setq org-self-insert-command-undo-counter 1)
14198 (and (> org-self-insert-command-undo-counter 0)
14200 (not (cadr buffer-undo-list)) ; remove nil entry
14201 (setcdr buffer-undo-list (cddr buffer-undo-list)))
14202 (setq org-self-insert-command-undo-counter
14203 (1+ org-self-insert-command-undo-counter)))))))
14205 (defun org-fix-tags-on-the-fly ()
14206 (when (and (equal (char-after (point-at-bol)) ?*)
14207 (org-on-heading-p))
14208 (org-align-tags-here org-tags-column)))
14210 (defun org-delete-backward-char (N)
14211 "Like `delete-backward-char', insert whitespace at field end in tables.
14212 When deleting backwards, in tables this function will insert whitespace in
14213 front of the next \"|\" separator, to keep the table aligned. The table will
14214 still be marked for re-alignment if the field did fill the entire column,
14215 because, in this case the deletion might narrow the column."
14217 (if (and (org-table-p)
14219 (string-match "|" (buffer-substring (point-at-bol) (point)))
14220 (looking-at ".*?|"))
14221 (let ((pos (point))
14222 (noalign (looking-at "[^|\n\r]* |"))
14223 (c org-table-may-need-update))
14224 (backward-delete-char N)
14225 (skip-chars-forward "^|")
14227 (goto-char (1- pos))
14228 ;; noalign: if there were two spaces at the end, this field
14229 ;; does not determine the width of the column.
14230 (if noalign (setq org-table-may-need-update c)))
14231 (backward-delete-char N)
14232 (org-fix-tags-on-the-fly)))
14234 (defun org-delete-char (N)
14235 "Like `delete-char', but insert whitespace at field end in tables.
14236 When deleting characters, in tables this function will insert whitespace in
14237 front of the next \"|\" separator, to keep the table aligned. The table will
14238 still be marked for re-alignment if the field did fill the entire column,
14239 because, in this case the deletion might narrow the column."
14241 (if (and (org-table-p)
14243 (not (= (char-after) ?|))
14245 (if (looking-at ".*?|")
14246 (let ((pos (point))
14247 (noalign (looking-at "[^|\n\r]* |"))
14248 (c org-table-may-need-update))
14249 (replace-match (concat
14250 (substring (match-string 0) 1 -1)
14253 ;; noalign: if there were two spaces at the end, this field
14254 ;; does not determine the width of the column.
14255 (if noalign (setq org-table-may-need-update c)))
14258 (org-fix-tags-on-the-fly)))
14260 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
14261 (put 'org-self-insert-command 'delete-selection t)
14262 (put 'orgtbl-self-insert-command 'delete-selection t)
14263 (put 'org-delete-char 'delete-selection 'supersede)
14264 (put 'org-delete-backward-char 'delete-selection 'supersede)
14265 (put 'org-yank 'delete-selection 'yank)
14267 ;; Make `flyspell-mode' delay after some commands
14268 (put 'org-self-insert-command 'flyspell-delayed t)
14269 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
14270 (put 'org-delete-char 'flyspell-delayed t)
14271 (put 'org-delete-backward-char 'flyspell-delayed t)
14273 ;; Make pabbrev-mode expand after org-mode commands
14274 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
14275 (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
14277 ;; How to do this: Measure non-white length of current string
14278 ;; If equal to column width, we should realign.
14280 (defun org-remap (map &rest commands)
14281 "In MAP, remap the functions given in COMMANDS.
14282 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
14285 (setq old (pop commands) new (pop commands))
14286 (if (fboundp 'command-remapping)
14287 (org-defkey map (vector 'remap old) new)
14288 (substitute-key-definition old new map global-map)))))
14290 (when (eq org-enable-table-editor 'optimized)
14291 ;; If the user wants maximum table support, we need to hijack
14292 ;; some standard editing functions
14293 (org-remap org-mode-map
14294 'self-insert-command 'org-self-insert-command
14295 'delete-char 'org-delete-char
14296 'delete-backward-char 'org-delete-backward-char)
14297 (org-defkey org-mode-map "|" 'org-force-self-insert))
14299 (defvar org-ctrl-c-ctrl-c-hook nil
14300 "Hook for functions attaching themselves to `C-c C-c'.
14301 This can be used to add additional functionality to the C-c C-c key which
14302 executes context-dependent commands.
14303 Each function will be called with no arguments. The function must check
14304 if the context is appropriate for it to act. If yes, it should do its
14305 thing and then return a non-nil value. If the context is wrong,
14306 just do nothing and return nil.")
14308 (defvar org-tab-first-hook nil
14309 "Hook for functions to attach themselves to TAB.
14310 See `org-ctrl-c-ctrl-c-hook' for more information.
14311 This hook runs as the first action when TAB is pressed, even before
14312 `org-cycle' messes around with the `outline-regexp' to cater for
14313 inline tasks and plain list item folding.
14314 If any function in this hook returns t, not other actions like table
14315 field motion visibility cycling will be done.")
14317 (defvar org-tab-after-check-for-table-hook nil
14318 "Hook for functions to attach themselves to TAB.
14319 See `org-ctrl-c-ctrl-c-hook' for more information.
14320 This hook runs after it has been established that the cursor is not in a
14321 table, but before checking if the cursor is in a headline or if global cycling
14323 If any function in this hook returns t, not other actions like visibility
14324 cycling will be done.")
14326 (defvar org-tab-after-check-for-cycling-hook nil
14327 "Hook for functions to attach themselves to TAB.
14328 See `org-ctrl-c-ctrl-c-hook' for more information.
14329 This hook runs after it has been established that not table field motion and
14330 not visibility should be done because of current context. This is probably
14331 the place where a package like yasnippets can hook in.")
14333 (defvar org-metaleft-hook nil
14334 "Hook for functions attaching themselves to `M-left'.
14335 See `org-ctrl-c-ctrl-c-hook' for more information.")
14336 (defvar org-metaright-hook nil
14337 "Hook for functions attaching themselves to `M-right'.
14338 See `org-ctrl-c-ctrl-c-hook' for more information.")
14339 (defvar org-metaup-hook nil
14340 "Hook for functions attaching themselves to `M-up'.
14341 See `org-ctrl-c-ctrl-c-hook' for more information.")
14342 (defvar org-metadown-hook nil
14343 "Hook for functions attaching themselves to `M-down'.
14344 See `org-ctrl-c-ctrl-c-hook' for more information.")
14345 (defvar org-shiftmetaleft-hook nil
14346 "Hook for functions attaching themselves to `M-S-left'.
14347 See `org-ctrl-c-ctrl-c-hook' for more information.")
14348 (defvar org-shiftmetaright-hook nil
14349 "Hook for functions attaching themselves to `M-S-right'.
14350 See `org-ctrl-c-ctrl-c-hook' for more information.")
14351 (defvar org-shiftmetaup-hook nil
14352 "Hook for functions attaching themselves to `M-S-up'.
14353 See `org-ctrl-c-ctrl-c-hook' for more information.")
14354 (defvar org-shiftmetadown-hook nil
14355 "Hook for functions attaching themselves to `M-S-down'.
14356 See `org-ctrl-c-ctrl-c-hook' for more information.")
14357 (defvar org-metareturn-hook nil
14358 "Hook for functions attaching themselves to `M-RET'.
14359 See `org-ctrl-c-ctrl-c-hook' for more information.")
14361 (defun org-modifier-cursor-error ()
14362 "Throw an error, a modified cursor command was applied in wrong context."
14363 (error "This command is active in special context like tables, headlines or items"))
14365 (defun org-shiftselect-error ()
14366 "Throw an error because Shift-Cursor command was applied in wrong context."
14367 (if (and (boundp 'shift-select-mode) shift-select-mode)
14368 (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
14369 (error "This command works only in special context like headlines or timestamps.")))
14371 (defun org-call-for-shift-select (cmd)
14372 (let ((this-command-keys-shift-translated t))
14373 (call-interactively cmd)))
14375 (defun org-shifttab (&optional arg)
14376 "Global visibility cycling or move to previous table field.
14377 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
14379 See the individual commands for more information."
14382 ((org-at-table-p) (call-interactively 'org-table-previous-field))
14384 (message "Content view to level: %d" arg)
14385 (org-content (prefix-numeric-value arg))
14386 (setq org-cycle-global-status 'overview))
14387 (t (call-interactively 'org-global-cycle))))
14389 (defun org-shiftmetaleft ()
14390 "Promote subtree or delete table column.
14391 Calls `org-promote-subtree', `org-outdent-item',
14392 or `org-table-delete-column', depending on context.
14393 See the individual commands for more information."
14396 ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
14397 ((org-at-table-p) (call-interactively 'org-table-delete-column))
14398 ((org-on-heading-p) (call-interactively 'org-promote-subtree))
14399 ((org-at-item-p) (call-interactively 'org-outdent-item))
14400 (t (org-modifier-cursor-error))))
14402 (defun org-shiftmetaright ()
14403 "Demote subtree or insert table column.
14404 Calls `org-demote-subtree', `org-indent-item',
14405 or `org-table-insert-column', depending on context.
14406 See the individual commands for more information."
14409 ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
14410 ((org-at-table-p) (call-interactively 'org-table-insert-column))
14411 ((org-on-heading-p) (call-interactively 'org-demote-subtree))
14412 ((org-at-item-p) (call-interactively 'org-indent-item))
14413 (t (org-modifier-cursor-error))))
14415 (defun org-shiftmetaup (&optional arg)
14416 "Move subtree up or kill table row.
14417 Calls `org-move-subtree-up' or `org-table-kill-row' or
14418 `org-move-item-up' depending on context. See the individual commands
14419 for more information."
14422 ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
14423 ((org-at-table-p) (call-interactively 'org-table-kill-row))
14424 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
14425 ((org-at-item-p) (call-interactively 'org-move-item-up))
14426 (t (org-modifier-cursor-error))))
14428 (defun org-shiftmetadown (&optional arg)
14429 "Move subtree down or insert table row.
14430 Calls `org-move-subtree-down' or `org-table-insert-row' or
14431 `org-move-item-down', depending on context. See the individual
14432 commands for more information."
14435 ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
14436 ((org-at-table-p) (call-interactively 'org-table-insert-row))
14437 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
14438 ((org-at-item-p) (call-interactively 'org-move-item-down))
14439 (t (org-modifier-cursor-error))))
14441 (defun org-metaleft (&optional arg)
14442 "Promote heading or move table column to left.
14443 Calls `org-do-promote' or `org-table-move-column', depending on context.
14444 With no specific context, calls the Emacs default `backward-word'.
14445 See the individual commands for more information."
14448 ((run-hook-with-args-until-success 'org-metaleft-hook))
14449 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
14450 ((or (org-on-heading-p)
14451 (and (org-region-active-p)
14453 (goto-char (region-beginning))
14454 (org-on-heading-p))))
14455 (call-interactively 'org-do-promote))
14456 ((or (org-at-item-p)
14457 (and (org-region-active-p)
14459 (goto-char (region-beginning))
14461 (call-interactively 'org-outdent-item))
14462 (t (call-interactively 'backward-word))))
14464 (defun org-metaright (&optional arg)
14465 "Demote subtree or move table column to right.
14466 Calls `org-do-demote' or `org-table-move-column', depending on context.
14467 With no specific context, calls the Emacs default `forward-word'.
14468 See the individual commands for more information."
14471 ((run-hook-with-args-until-success 'org-metaright-hook))
14472 ((org-at-table-p) (call-interactively 'org-table-move-column))
14473 ((or (org-on-heading-p)
14474 (and (org-region-active-p)
14476 (goto-char (region-beginning))
14477 (org-on-heading-p))))
14478 (call-interactively 'org-do-demote))
14479 ((or (org-at-item-p)
14480 (and (org-region-active-p)
14482 (goto-char (region-beginning))
14484 (call-interactively 'org-indent-item))
14485 (t (call-interactively 'forward-word))))
14487 (defun org-metaup (&optional arg)
14488 "Move subtree up or move table row up.
14489 Calls `org-move-subtree-up' or `org-table-move-row' or
14490 `org-move-item-up', depending on context. See the individual commands
14491 for more information."
14494 ((run-hook-with-args-until-success 'org-metaup-hook))
14495 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
14496 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
14497 ((org-at-item-p) (call-interactively 'org-move-item-up))
14498 (t (transpose-lines 1) (beginning-of-line -1))))
14500 (defun org-metadown (&optional arg)
14501 "Move subtree down or move table row down.
14502 Calls `org-move-subtree-down' or `org-table-move-row' or
14503 `org-move-item-down', depending on context. See the individual
14504 commands for more information."
14507 ((run-hook-with-args-until-success 'org-metadown-hook))
14508 ((org-at-table-p) (call-interactively 'org-table-move-row))
14509 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
14510 ((org-at-item-p) (call-interactively 'org-move-item-down))
14511 (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
14513 (defun org-shiftup (&optional arg)
14514 "Increase item in timestamp or increase priority of current headline.
14515 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
14516 depending on context. See the individual commands for more information."
14519 ((and org-support-shift-select (org-region-active-p))
14520 (org-call-for-shift-select 'previous-line))
14521 ((org-at-timestamp-p t)
14522 (call-interactively (if org-edit-timestamp-down-means-later
14523 'org-timestamp-down 'org-timestamp-up)))
14524 ((and (not (eq org-support-shift-select 'always))
14525 org-enable-priority-commands
14526 (org-on-heading-p))
14527 (call-interactively 'org-priority-up))
14528 ((and (not org-support-shift-select) (org-at-item-p))
14529 (call-interactively 'org-previous-item))
14530 ((org-clocktable-try-shift 'up arg))
14531 (org-support-shift-select
14532 (org-call-for-shift-select 'previous-line))
14533 (t (org-shiftselect-error))))
14535 (defun org-shiftdown (&optional arg)
14536 "Decrease item in timestamp or decrease priority of current headline.
14537 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
14538 depending on context. See the individual commands for more information."
14541 ((and org-support-shift-select (org-region-active-p))
14542 (org-call-for-shift-select 'next-line))
14543 ((org-at-timestamp-p t)
14544 (call-interactively (if org-edit-timestamp-down-means-later
14545 'org-timestamp-up 'org-timestamp-down)))
14546 ((and (not (eq org-support-shift-select 'always))
14547 org-enable-priority-commands
14548 (org-on-heading-p))
14549 (call-interactively 'org-priority-down))
14550 ((and (not org-support-shift-select) (org-at-item-p))
14551 (call-interactively 'org-next-item))
14552 ((org-clocktable-try-shift 'down arg))
14553 (org-support-shift-select
14554 (org-call-for-shift-select 'next-line))
14555 (t (org-shiftselect-error))))
14557 (defun org-shiftright (&optional arg)
14558 "Cycle the thing at point or in the current line, depending on context.
14559 Depending on context, this does one of the following:
14561 - switch a timestamp at point one day into the future
14562 - on a headline, switch to the next TODO keyword.
14563 - on an item, switch entire list to the next bullet type
14564 - on a property line, switch to the next allowed value
14565 - on a clocktable definition line, move time block into the future"
14568 ((and org-support-shift-select (org-region-active-p))
14569 (org-call-for-shift-select 'forward-char))
14570 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
14571 ((and (not (eq org-support-shift-select 'always))
14572 (org-on-heading-p))
14573 (let ((org-inhibit-logging
14574 (not org-treat-S-cursor-todo-selection-as-state-change))
14575 (org-inhibit-blocking
14576 (not org-treat-S-cursor-todo-selection-as-state-change)))
14577 (org-call-with-arg 'org-todo 'right)))
14578 ((or (and org-support-shift-select
14579 (not (eq org-support-shift-select 'always))
14580 (org-at-item-bullet-p))
14581 (and (not org-support-shift-select) (org-at-item-p)))
14582 (org-call-with-arg 'org-cycle-list-bullet nil))
14583 ((and (not (eq org-support-shift-select 'always))
14584 (org-at-property-p))
14585 (call-interactively 'org-property-next-allowed-value))
14586 ((org-clocktable-try-shift 'right arg))
14587 (org-support-shift-select
14588 (org-call-for-shift-select 'forward-char))
14589 (t (org-shiftselect-error))))
14591 (defun org-shiftleft (&optional arg)
14592 "Cycle the thing at point or in the current line, depending on context.
14593 Depending on context, this does one of the following:
14595 - switch a timestamp at point one day into the past
14596 - on a headline, switch to the previous TODO keyword.
14597 - on an item, switch entire list to the previous bullet type
14598 - on a property line, switch to the previous allowed value
14599 - on a clocktable definition line, move time block into the past"
14602 ((and org-support-shift-select (org-region-active-p))
14603 (org-call-for-shift-select 'backward-char))
14604 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
14605 ((and (not (eq org-support-shift-select 'always))
14606 (org-on-heading-p))
14607 (let ((org-inhibit-logging
14608 (not org-treat-S-cursor-todo-selection-as-state-change))
14609 (org-inhibit-blocking
14610 (not org-treat-S-cursor-todo-selection-as-state-change)))
14611 (org-call-with-arg 'org-todo 'left)))
14612 ((or (and org-support-shift-select
14613 (not (eq org-support-shift-select 'always))
14614 (org-at-item-bullet-p))
14615 (and (not org-support-shift-select) (org-at-item-p)))
14616 (org-call-with-arg 'org-cycle-list-bullet 'previous))
14617 ((and (not (eq org-support-shift-select 'always))
14618 (org-at-property-p))
14619 (call-interactively 'org-property-previous-allowed-value))
14620 ((org-clocktable-try-shift 'left arg))
14621 (org-support-shift-select
14622 (org-call-for-shift-select 'backward-char))
14623 (t (org-shiftselect-error))))
14625 (defun org-shiftcontrolright ()
14626 "Switch to next TODO set."
14629 ((and org-support-shift-select (org-region-active-p))
14630 (org-call-for-shift-select 'forward-word))
14631 ((and (not (eq org-support-shift-select 'always))
14632 (org-on-heading-p))
14633 (org-call-with-arg 'org-todo 'nextset))
14634 (org-support-shift-select
14635 (org-call-for-shift-select 'forward-word))
14636 (t (org-shiftselect-error))))
14638 (defun org-shiftcontrolleft ()
14639 "Switch to previous TODO set."
14642 ((and org-support-shift-select (org-region-active-p))
14643 (org-call-for-shift-select 'backward-word))
14644 ((and (not (eq org-support-shift-select 'always))
14645 (org-on-heading-p))
14646 (org-call-with-arg 'org-todo 'previousset))
14647 (org-support-shift-select
14648 (org-call-for-shift-select 'backward-word))
14649 (t (org-shiftselect-error))))
14651 (defun org-ctrl-c-ret ()
14652 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
14655 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
14656 (t (call-interactively 'org-insert-heading))))
14658 (defun org-copy-special ()
14659 "Copy region in table or copy current subtree.
14660 Calls `org-table-copy' or `org-copy-subtree', depending on context.
14661 See the individual commands for more information."
14663 (call-interactively
14664 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
14666 (defun org-cut-special ()
14667 "Cut region in table or cut current subtree.
14668 Calls `org-table-copy' or `org-cut-subtree', depending on context.
14669 See the individual commands for more information."
14671 (call-interactively
14672 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
14674 (defun org-paste-special (arg)
14675 "Paste rectangular region into table, or past subtree relative to level.
14676 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
14677 See the individual commands for more information."
14679 (if (org-at-table-p)
14680 (org-table-paste-rectangle)
14681 (org-paste-subtree arg)))
14683 (defun org-edit-special ()
14684 "Call a special editor for the stuff at point.
14685 When at a table, call the formula editor with `org-table-edit-formulas'.
14686 When at the first line of an src example, call `org-edit-src-code'.
14687 When in an #+include line, visit the include file. Otherwise call
14688 `ffap' to visit the file at point."
14692 (call-interactively 'org-table-edit-formulas))
14694 (beginning-of-line 1)
14695 (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
14696 (find-file (org-trim (match-string 1))))
14697 ((org-edit-src-code))
14698 ((org-edit-fixed-width-region))
14699 (t (call-interactively 'ffap))))
14702 (defun org-ctrl-c-ctrl-c (&optional arg)
14703 "Set tags in headline, or update according to changed information at point.
14705 This command does many different things, depending on context:
14707 - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
14708 this is what we do.
14710 - If the cursor is in a headline, prompt for tags and insert them
14711 into the current line, aligned to `org-tags-column'. When called
14712 with prefix arg, realign all tags in the current buffer.
14714 - If the cursor is in one of the special #+KEYWORD lines, this
14715 triggers scanning the buffer for these lines and updating the
14718 - If the cursor is inside a table, realign the table. This command
14719 works even if the automatic table editor has been turned off.
14721 - If the cursor is on a #+TBLFM line, re-apply the formulas to
14724 - If the cursor is at a footnote reference or definition, jump to
14725 the corresponding definition or references, respectively.
14727 - If the cursor is a the beginning of a dynamic block, update it.
14729 - If the cursor is inside a table created by the table.el package,
14730 activate that table.
14732 - If the current buffer is a remember buffer, close note and file
14733 it. A prefix argument of 1 files to the default location
14734 without further interaction. A prefix argument of 2 files to
14735 the currently clocking task.
14737 - If the cursor is on a <<<target>>>, update radio targets and corresponding
14738 links in this buffer.
14740 - If the cursor is on a numbered item in a plain list, renumber the
14743 - If the cursor is on a checkbox, toggle it."
14745 (let ((org-enable-table-editor t))
14747 ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
14748 org-occur-highlights
14749 org-latex-fragment-image-overlays)
14750 (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
14751 (org-remove-occur-highlights)
14752 (org-remove-latex-fragment-image-overlays)
14753 (message "Temporary highlights/overlays removed from current buffer"))
14754 ((and (local-variable-p 'org-finish-function (current-buffer))
14755 (fboundp org-finish-function))
14756 (funcall org-finish-function))
14757 ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
14758 ((org-at-property-p)
14759 (call-interactively 'org-property-action))
14760 ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
14761 ((org-on-heading-p) (call-interactively 'org-set-tags))
14762 ((org-at-table.el-p)
14764 (beginning-of-line 1)
14765 (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
14766 (call-interactively 'table-recognize-table))
14768 (org-table-maybe-eval-formula)
14770 (call-interactively 'org-table-recalculate)
14771 (org-table-maybe-recalculate-line))
14772 (call-interactively 'org-table-align))
14773 ((or (org-footnote-at-reference-p)
14774 (org-footnote-at-definition-p))
14775 (call-interactively 'org-footnote-action))
14776 ((org-at-item-checkbox-p)
14777 (call-interactively 'org-toggle-checkbox))
14780 (call-interactively 'org-toggle-checkbox)
14781 (call-interactively 'org-maybe-renumber-ordered-list)))
14782 ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
14784 (beginning-of-line 1)
14785 (save-excursion (org-update-dblock)))
14787 (beginning-of-line 1)
14788 (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
14790 ((equal (match-string 1) "TBLFM")
14791 ;; Recalculate the table before this line
14793 (beginning-of-line 1)
14794 (skip-chars-backward " \r\n\t")
14795 (if (org-at-table-p)
14796 (org-call-with-arg 'org-table-recalculate t))))
14798 ; (org-set-regexps-and-options)
14799 ; (org-restart-font-lock)
14800 (let ((org-inhibit-startup t)) (org-mode-restart))
14801 (message "Local setup has been refreshed"))))
14802 ((org-clock-update-time-maybe))
14803 (t (error "C-c C-c can do nothing useful at this location.")))))
14805 (defun org-mode-restart ()
14806 "Restart Org-mode, to scan again for special lines.
14807 Also updates the keyword regular expressions."
14810 (message "Org-mode restarted"))
14812 (defun org-kill-note-or-show-branches ()
14813 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
14815 (if (not org-finish-function)
14816 (call-interactively 'show-branches)
14817 (let ((org-note-abort t))
14818 (funcall org-finish-function))))
14820 (defun org-return (&optional indent)
14821 "Goto next table row or insert a newline.
14822 Calls `org-table-next-row' or `newline', depending on context.
14823 See the individual commands for more information."
14826 ((bobp) (if indent (newline-and-indent) (newline)))
14828 (org-table-justify-field-maybe)
14829 (call-interactively 'org-table-next-row))
14830 ((and org-return-follows-link
14831 (eq (get-text-property (point) 'face) 'org-link))
14832 (call-interactively 'org-open-at-point))
14833 ((and (org-at-heading-p)
14835 (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
14839 (t (if indent (newline-and-indent) (newline)))))
14841 (defun org-return-indent ()
14842 "Goto next table row or insert a newline and indent.
14843 Calls `org-table-next-row' or `newline-and-indent', depending on
14844 context. See the individual commands for more information."
14848 (defun org-ctrl-c-star ()
14849 "Compute table, or change heading status of lines.
14850 Calls `org-table-recalculate' or `org-toggle-heading',
14851 depending on context."
14855 (call-interactively 'org-table-recalculate))
14857 ;; Convert all lines in region to list items
14858 (call-interactively 'org-toggle-heading))))
14860 (defun org-ctrl-c-minus ()
14861 "Insert separator line in table or modify bullet status of line.
14862 Also turns a plain line or a region of lines into list items.
14863 Calls `org-table-insert-hline', `org-toggle-item', or
14864 `org-cycle-list-bullet', depending on context."
14868 (call-interactively 'org-table-insert-hline))
14869 ((org-region-active-p)
14870 (call-interactively 'org-toggle-item))
14872 (call-interactively 'org-cycle-list-bullet))
14874 (call-interactively 'org-toggle-item))))
14876 (defun org-toggle-item ()
14877 "Convert headings or normal lines to items, items to normal lines.
14878 If there is no active region, only the current line is considered.
14880 If the first line in the region is a headline, convert all headlines to items.
14882 If the first line in the region is an item, convert all items to normal lines.
14884 If the first line is normal text, add an item bullet to each line."
14886 (let (l2 l beg end)
14887 (if (org-region-active-p)
14888 (setq beg (region-beginning) end (region-end))
14889 (setq beg (point-at-bol)
14890 end (min (1+ (point-at-eol)) (point-max))))
14893 (setq l2 (org-current-line))
14895 (beginning-of-line 1)
14896 (setq l (1- (org-current-line)))
14897 (if (org-at-item-p)
14898 ;; We already have items, de-itemize
14899 (while (< (setq l (1+ l)) l2)
14900 (when (org-at-item-p)
14901 (goto-char (match-beginning 2))
14902 (delete-region (match-beginning 2) (match-end 2))
14903 (and (looking-at "[ \t]+") (replace-match "")))
14904 (beginning-of-line 2))
14905 (if (org-on-heading-p)
14906 ;; Headings, convert to items
14907 (while (< (setq l (1+ l)) l2)
14908 (if (looking-at org-outline-regexp)
14909 (replace-match "- " t t))
14910 (beginning-of-line 2))
14911 ;; normal lines, turn them into items
14912 (while (< (setq l (1+ l)) l2)
14913 (unless (org-at-item-p)
14914 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
14915 (replace-match "\\1- \\2")))
14916 (beginning-of-line 2)))))))
14918 (defun org-toggle-heading (&optional nstars)
14919 "Convert headings to normal text, or items or text to headings.
14920 If there is no active region, only the current line is considered.
14922 If the first line is a heading, remove the stars from all headlines
14925 If the first line is a plain list item, turn all plain list items
14928 If the first line is a normal line, turn each and every line in the
14929 region into a heading.
14931 When converting a line into a heading, the number of stars is chosen
14932 such that the lines become children of the current entry. However,
14933 when a prefix argument is given, its value determines the number of
14936 (let (l2 l itemp beg end)
14937 (if (org-region-active-p)
14938 (setq beg (region-beginning) end (region-end))
14939 (setq beg (point-at-bol)
14940 end (min (1+ (point-at-eol)) (point-max))))
14943 (setq l2 (org-current-line))
14945 (beginning-of-line 1)
14946 (setq l (1- (org-current-line)))
14947 (if (org-on-heading-p)
14948 ;; We already have headlines, de-star them
14949 (while (< (setq l (1+ l)) l2)
14950 (when (org-on-heading-p t)
14951 (and (looking-at outline-regexp) (replace-match "")))
14952 (beginning-of-line 2))
14953 (setq itemp (org-at-item-p))
14956 (make-string (prefix-numeric-value current-prefix-arg)
14959 (if (re-search-backward org-complex-heading-regexp nil t)
14960 (match-string 1) ""))))
14961 (add-stars (cond (nstars "")
14962 ((equal stars "") "*")
14963 (org-odd-levels-only "**")
14965 (rpl (concat stars add-stars " ")))
14966 (while (< (setq l (1+ l)) l2)
14968 (and (org-at-item-p) (replace-match rpl t t))
14969 (unless (org-on-heading-p)
14970 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
14971 (replace-match (concat rpl (match-string 2))))))
14972 (beginning-of-line 2)))))))
14974 (defun org-meta-return (&optional arg)
14975 "Insert a new heading or wrap a region in a table.
14976 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
14977 See the individual commands for more information."
14980 ((run-hook-with-args-until-success 'org-metareturn-hook))
14982 (call-interactively 'org-table-wrap-region))
14983 (t (call-interactively 'org-insert-heading))))
14987 ;; Define the Org-mode menus
14988 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
14990 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
14991 ["Next Field" org-cycle (org-at-table-p)]
14992 ["Previous Field" org-shifttab (org-at-table-p)]
14993 ["Next Row" org-return (org-at-table-p)]
14995 ["Blank Field" org-table-blank-field (org-at-table-p)]
14996 ["Edit Field" org-table-edit-field (org-at-table-p)]
14997 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
15000 ["Move Column Left" org-metaleft (org-at-table-p)]
15001 ["Move Column Right" org-metaright (org-at-table-p)]
15002 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
15003 ["Insert Column" org-shiftmetaright (org-at-table-p)])
15005 ["Move Row Up" org-metaup (org-at-table-p)]
15006 ["Move Row Down" org-metadown (org-at-table-p)]
15007 ["Delete Row" org-shiftmetaup (org-at-table-p)]
15008 ["Insert Row" org-shiftmetadown (org-at-table-p)]
15009 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
15011 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
15013 ["Copy Rectangle" org-copy-special (org-at-table-p)]
15014 ["Cut Rectangle" org-cut-special (org-at-table-p)]
15015 ["Paste Rectangle" org-paste-special (org-at-table-p)]
15016 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
15019 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
15020 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
15021 ["Edit Formulas" org-edit-special (org-at-table-p)]
15023 ["Recalculate line" org-table-recalculate (org-at-table-p)]
15024 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
15025 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
15027 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
15029 ["Sum Column/Rectangle" org-table-sum
15030 (or (org-at-table-p) (org-region-active-p))]
15031 ["Which Column?" org-table-current-column (org-at-table-p)])
15033 org-table-toggle-formula-debugger
15034 :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
15035 ["Show Col/Row Numbers"
15036 org-table-toggle-coordinate-overlays
15038 :selected (org-bound-and-true-p org-table-overlay-coordinates)]
15040 ["Create" org-table-create (and (not (org-at-table-p))
15041 org-enable-table-editor)]
15042 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
15043 ["Import from File" org-table-import (not (org-at-table-p))]
15044 ["Export to File" org-table-export (org-at-table-p)]
15046 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
15048 (easy-menu-define org-org-menu org-mode-map "Org menu"
15051 ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
15052 ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
15053 ["Sparse Tree..." org-sparse-tree t]
15054 ["Reveal Context" org-reveal t]
15055 ["Show All" show-all t]
15057 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
15059 ["New Heading" org-insert-heading t]
15060 ("Navigate Headings"
15061 ["Up" outline-up-heading t]
15062 ["Next" outline-next-visible-heading t]
15063 ["Previous" outline-previous-visible-heading t]
15064 ["Next Same Level" outline-forward-same-level t]
15065 ["Previous Same Level" outline-backward-same-level t]
15067 ["Jump" org-goto t])
15069 ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
15070 ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
15072 ["Copy Subtree" org-copy-special (not (org-at-table-p))]
15073 ["Cut Subtree" org-cut-special (not (org-at-table-p))]
15074 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
15076 ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
15078 ["Promote Heading" org-metaleft (not (org-at-table-p))]
15079 ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
15080 ["Demote Heading" org-metaright (not (org-at-table-p))]
15081 ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
15083 ["Sort Region/Children" org-sort (not (org-at-table-p))]
15085 ["Convert to odd levels" org-convert-to-odd-levels t]
15086 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
15088 ["Emphasis..." org-emphasize t]
15089 ["Edit Source Example" org-edit-special t]
15091 ["Footnote new/jump" org-footnote-action t]
15092 ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
15094 ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
15095 ; ["Check and Tag Children" (org-toggle-archive-tag (4))
15096 ; :active t :keys "C-u C-c C-x C-a"]
15097 ["Sparse trees open ARCHIVE trees"
15098 (setq org-sparse-tree-open-archived-trees
15099 (not org-sparse-tree-open-archived-trees))
15100 :style toggle :selected org-sparse-tree-open-archived-trees]
15101 ["Cycling opens ARCHIVE trees"
15102 (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
15103 :style toggle :selected org-cycle-open-archived-trees]
15105 ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
15106 ["Move Subtree to Archive" org-advertized-archive-subtree t]
15107 ; ["Check and Move Children" (org-archive-subtree '(4))
15108 ; :active t :keys "C-u C-c C-x C-s"]
15112 ["Store Link (Global)" org-store-link t]
15113 ["Find existing link to here" org-occur-link-in-agenda-files t]
15114 ["Insert Link" org-insert-link t]
15115 ["Follow Link" org-open-at-point t]
15117 ["Next link" org-next-link t]
15118 ["Previous link" org-previous-link t]
15120 ["Descriptive Links"
15121 (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
15123 :selected (member '(org-link) buffer-invisibility-spec)]
15126 (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
15128 :selected (not (member '(org-link) buffer-invisibility-spec))])
15131 ["TODO/DONE/-" org-todo t]
15133 ["Next keyword" org-shiftright (org-on-heading-p)]
15134 ["Previous keyword" org-shiftleft (org-on-heading-p)]
15135 ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
15136 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
15137 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
15138 ["Show TODO Tree" org-show-todo-tree t]
15139 ["Global TODO list" org-todo-list t]
15141 ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
15142 :selected org-enforce-todo-dependencies :style toggle :active t]
15143 "Settings for tree at point"
15144 ["Do Children sequentially" org-toggle-ordered-property :style radio
15145 :selected (ignore-errors (org-entry-get nil "ORDERED"))
15146 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
15147 ["Do Children parallel" org-toggle-ordered-property :style radio
15148 :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
15149 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
15151 ["Set Priority" org-priority t]
15152 ["Priority Up" org-shiftup t]
15153 ["Priority Down" org-shiftdown t]
15155 ["Get news from all feeds" org-feed-update-all t]
15156 ["Go to the inbox of a feed..." org-feed-goto-inbox t]
15157 ["Customize feeds" (customize-variable 'org-feed-alist) t])
15158 ("TAGS and Properties"
15159 ["Set Tags" org-set-tags-command t]
15160 ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
15162 ["Set property" org-set-property t]
15163 ["Column view of properties" org-columns t]
15164 ["Insert Column View DBlock" org-insert-columns-dblock t])
15165 ("Dates and Scheduling"
15166 ["Timestamp" org-time-stamp t]
15167 ["Timestamp (inactive)" org-time-stamp-inactive t]
15169 ["1 Day Later" org-shiftright t]
15170 ["1 Day Earlier" org-shiftleft t]
15171 ["1 ... Later" org-shiftup t]
15172 ["1 ... Earlier" org-shiftdown t])
15173 ["Compute Time Range" org-evaluate-time-range t]
15174 ["Schedule Item" org-schedule t]
15175 ["Deadline" org-deadline t]
15177 ["Custom time format" org-toggle-time-stamp-overlays
15178 :style radio :selected org-display-custom-times]
15180 ["Goto Calendar" org-goto-calendar t]
15181 ["Date from Calendar" org-date-from-calendar t]
15183 ["Start/Restart Timer" org-timer-start t]
15184 ["Pause/Continue Timer" org-timer-pause-or-continue t]
15185 ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
15186 ["Insert Timer String" org-timer t]
15187 ["Insert Timer Item" org-timer-item t])
15189 ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
15190 ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
15191 ["Clock out" org-clock-out t]
15192 ["Clock cancel" org-clock-cancel t]
15194 ["Mark as default task" org-clock-mark-default-task t]
15195 ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
15196 ["Goto running clock" org-clock-goto t]
15198 ["Display times" org-clock-display t]
15199 ["Create clock table" org-clock-report t]
15201 ["Record DONE time"
15202 (progn (setq org-log-done (not org-log-done))
15203 (message "Switching to %s will %s record a timestamp"
15204 (car org-done-keywords)
15205 (if org-log-done "automatically" "not")))
15206 :style toggle :selected org-log-done])
15208 ["Agenda Command..." org-agenda t]
15209 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
15210 ("File List for Agenda")
15211 ("Special views current file"
15212 ["TODO Tree" org-show-todo-tree t]
15213 ["Check Deadlines" org-check-deadlines t]
15214 ["Timeline" org-timeline t]
15215 ["Tags/Property tree" org-match-sparse-tree t])
15217 ["Export/Publish..." org-export t]
15219 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
15220 :selected org-cdlatex-mode]
15221 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
15222 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
15223 ["Modify math symbol" org-cdlatex-math-modify
15224 (org-inside-LaTeX-fragment-p)]
15225 ["Insert citation" org-reftex-citation t]
15227 ["Export LaTeX fragments as images"
15228 (if (featurep 'org-exp)
15229 (setq org-export-with-LaTeX-fragments
15230 (not org-export-with-LaTeX-fragments))
15231 (require 'org-exp))
15232 :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
15233 org-export-with-LaTeX-fragments)])
15236 ["Show Version" org-version t]
15237 ["Info Documentation" org-info t])
15239 ["Browse Org Group" org-customize t]
15241 ["Expand This Menu" org-create-customize-menu
15242 (fboundp 'customize-menu-create)])
15245 ["Refresh setup current buffer" org-mode-restart t]
15246 ["Reload Org (after update)" org-reload t]
15247 ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
15250 (defun org-info (&optional node)
15251 "Read documentation for Org-mode in the info system.
15252 With optional NODE, go directly to that node."
15254 (info (format "(org)%s" (or node ""))))
15256 (defun org-install-agenda-files-menu ()
15257 (let ((bl (buffer-list)))
15260 (set-buffer (pop bl))
15261 (if (org-mode-p) (setq bl nil)))
15264 '("Org") "File List for Agenda"
15267 ["Edit File List" (org-edit-agenda-file-list) t]
15268 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
15269 ["Remove Current File from List" org-remove-file t]
15270 ["Cycle through agenda files" org-cycle-agenda-files t]
15271 ["Occur in all agenda files" org-occur-in-agenda-files t]
15273 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
15278 (defun org-require-autoloaded-modules ()
15281 '(org-agenda org-archive org-ascii org-attach org-clock org-colview
15282 org-docbook org-exp org-html org-icalendar
15284 org-publish org-remember org-table
15285 org-timer org-xoxo)))
15288 (defun org-reload (&optional uncompiled)
15289 "Reload all org lisp files.
15290 With prefix arg UNCOMPILED, load the uncompiled versions."
15292 (require 'find-func)
15293 (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
15294 (dir-org (file-name-directory (org-find-library-name "org")))
15295 (dir-org-contrib (ignore-errors
15296 (file-name-directory
15297 (org-find-library-name "org-contribdir"))))
15299 (append (directory-files dir-org t file-re)
15300 (and dir-org-contrib
15301 (directory-files dir-org-contrib t file-re))))
15302 (remove-re (concat (if (featurep 'xemacs)
15303 "org-colview" "org-colview-xemacs")
15305 (setq files (mapcar 'file-name-sans-extension files))
15306 (setq files (mapcar
15307 (lambda (x) (if (string-match remove-re x) nil x))
15309 (setq files (delq nil files))
15312 (when (featurep (intern (file-name-nondirectory f)))
15313 (if (and (not uncompiled)
15314 (file-exists-p (concat f ".elc")))
15315 (load (concat f ".elc") nil nil t)
15316 (load (concat f ".el") nil nil t))))
15321 (defun org-customize ()
15322 "Call the customize function with org as argument."
15324 (org-load-modules-maybe)
15325 (org-require-autoloaded-modules)
15326 (customize-browse 'org))
15328 (defun org-create-customize-menu ()
15329 "Create a full customization menu for Org-mode, insert it into the menu."
15331 (org-load-modules-maybe)
15332 (org-require-autoloaded-modules)
15333 (if (fboundp 'customize-menu-create)
15336 '("Org") "Customize"
15337 `(["Browse Org group" org-customize t]
15339 ,(customize-menu-create 'org)
15340 ["Set" Custom-set t]
15341 ["Save" Custom-save t]
15342 ["Reset to Current" Custom-reset-current t]
15343 ["Reset to Saved" Custom-reset-saved t]
15344 ["Reset to Standard Settings" Custom-reset-standard t]))
15345 (message "\"Org\"-menu now contains full customization menu"))
15346 (error "Cannot expand menu (outdated version of cus-edit.el)")))
15348 ;;;; Miscellaneous stuff
15350 ;;; Generally useful functions
15352 (defun org-find-text-property-in-string (prop s)
15353 "Return the first non-nil value of property PROP in string S."
15354 (or (get-text-property 0 prop s)
15355 (get-text-property (or (next-single-property-change 0 prop s) 0)
15358 (defun org-display-warning (message) ;; Copied from Emacs-Muse
15359 "Display the given MESSAGE as a warning."
15360 (if (fboundp 'display-warning)
15361 (display-warning 'org message
15362 (if (featurep 'xemacs)
15365 (let ((buf (get-buffer-create "*Org warnings*")))
15366 (with-current-buffer buf
15367 (goto-char (point-max))
15368 (insert "Warning (Org): " message)
15371 (display-buffer buf)
15374 (defun org-goto-marker-or-bmk (marker &optional bookmark)
15375 "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
15376 (if (and marker (marker-buffer marker)
15377 (buffer-live-p (marker-buffer marker)))
15379 (switch-to-buffer (marker-buffer marker))
15380 (if (or (> marker (point-max)) (< marker (point-min)))
15383 (org-show-context 'org-goto))
15385 (bookmark-jump bookmark)
15386 (error "Cannot find location"))))
15388 (defun org-quote-csv-field (s)
15389 "Quote field for inclusion in CSV material."
15390 (if (string-match "[\",]" s)
15391 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
15394 (defun org-plist-delete (plist property)
15395 "Delete PROPERTY from PLIST.
15396 This is in contrast to merely setting it to 0."
15399 (if (not (eq property (car plist)))
15400 (setq p (plist-put p (car plist) (nth 1 plist))))
15401 (setq plist (cddr plist)))
15404 (defun org-force-self-insert (N)
15405 "Needed to enforce self-insert under remapping."
15407 (self-insert-command N))
15409 (defun org-string-width (s)
15410 "Compute width of string, ignoring invisible characters.
15411 This ignores character with invisibility property `org-link', and also
15412 characters with property `org-cwidth', because these will become invisible
15413 upon the next fontification round."
15415 (when (or (eq t buffer-invisibility-spec)
15416 (assq 'org-link buffer-invisibility-spec))
15417 (while (setq b (text-property-any 0 (length s)
15418 'invisible 'org-link s))
15419 (setq s (concat (substring s 0 b)
15420 (substring s (or (next-single-property-change
15421 b 'invisible s) (length s)))))))
15422 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
15423 (setq s (concat (substring s 0 b)
15424 (substring s (or (next-single-property-change
15425 b 'org-cwidth s) (length s))))))
15426 (setq l (string-width s) b -1)
15427 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
15428 (setq l (- l (get-text-property b 'org-dwidth-n s))))
15431 (defun org-get-indentation (&optional line)
15432 "Get the indentation of the current line, interpreting tabs.
15433 When LINE is given, assume it represents a line and compute its indentation."
15435 (if (string-match "^ *" (org-remove-tabs line))
15438 (beginning-of-line 1)
15439 (skip-chars-forward " \t")
15440 (current-column))))
15442 (defun org-remove-tabs (s &optional width)
15443 "Replace tabulators in S with spaces.
15444 Assumes that s is a single line, starting in column 0."
15445 (setq width (or width tab-width))
15446 (while (string-match "\t" s)
15447 (setq s (replace-match
15449 (- (* width (/ (+ (match-beginning 0) width) width))
15450 (match-beginning 0)) ?\ )
15454 (defun org-fix-indentation (line ind)
15455 "Fix indentation in LINE.
15456 IND is a cons cell with target and minimum indentation.
15457 If the current indentation in LINE is smaller than the minimum,
15458 leave it alone. If it is larger than ind, set it to the target."
15459 (let* ((l (org-remove-tabs line))
15460 (i (org-get-indentation l))
15461 (i1 (car ind)) (i2 (cdr ind)))
15462 (if (>= i i2) (setq l (substring line i2)))
15464 (concat (make-string i1 ?\ ) l)
15467 (defun org-remove-indentation (code &optional n)
15468 "Remove the maximum common indentation from the lines in CODE.
15469 N may optionally be the number of spaces to remove."
15472 (org-do-remove-indentation n)
15475 (defun org-do-remove-indentation (&optional n)
15476 "Remove the maximum common indentation from the buffer."
15477 (untabify (point-min) (point-max))
15478 (let ((min 10000) re)
15481 (goto-char (point-min))
15482 (while (re-search-forward "^ *[^ \n]" nil t)
15483 (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
15484 (unless (or (= min 0) (= min 10000))
15485 (setq re (format "^ \\{%d\\}" min))
15486 (goto-char (point-min))
15487 (while (re-search-forward re nil t)
15492 (defun org-base-buffer (buffer)
15493 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
15496 (or (buffer-base-buffer buffer)
15499 (defun org-trim (s)
15500 "Remove whitespace at beginning and end of string."
15501 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
15502 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
15505 (defun org-wrap (string &optional width lines)
15506 "Wrap string to either a number of lines, or a width in characters.
15507 If WIDTH is non-nil, the string is wrapped to that width, however many lines
15508 that costs. If there is a word longer than WIDTH, the text is actually
15509 wrapped to the length of that word.
15510 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
15511 many lines, whatever width that takes.
15512 The return value is a list of lines, without newlines at the end."
15513 (let* ((words (org-split-string string "[ \t\n]+"))
15514 (maxword (apply 'max (mapcar 'org-string-width words)))
15517 (org-do-wrap words (max maxword width)))
15520 (setq ll (org-do-wrap words maxword))
15521 (if (<= (length ll) lines)
15524 (while (> (length ll) lines)
15526 (setq ll (org-do-wrap words w)))
15528 (t (error "Cannot wrap this")))))
15530 (defun org-do-wrap (words width)
15531 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
15534 (setq line (pop words))
15535 (while (and words (< (+ (length line) (length (car words))) width))
15536 (setq line (concat line " " (pop words))))
15537 (setq lines (push line lines)))
15540 (defun org-split-string (string &optional separators)
15541 "Splits STRING into substrings at SEPARATORS.
15542 No empty strings are returned if there are matches at the beginning
15543 and end of string."
15544 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
15548 (while (and (string-match rexp string
15550 (= start (match-beginning 0))
15551 (< start (length string)))
15553 (< (match-beginning 0) (length string)))
15555 (or (eq (match-beginning 0) 0)
15556 (and (eq (match-beginning 0) (match-end 0))
15557 (eq (match-beginning 0) start))
15559 (cons (substring string start (match-beginning 0))
15561 (setq start (match-end 0)))
15562 (or (eq start (length string))
15564 (cons (substring string start)
15568 (defun org-quote-vert (s)
15569 "Replace \"|\" with \"\\vert\"."
15570 (while (string-match "|" s)
15571 (setq s (replace-match "\\vert" t t s)))
15574 (defun org-uuidgen-p (s)
15575 "Is S an ID created by UUIDGEN?"
15576 (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
15578 (defun org-context ()
15579 "Return a list of contexts of the current cursor position.
15580 If several contexts apply, all are returned.
15581 Each context entry is a list with a symbol naming the context, and
15582 two positions indicating start and end of the context. Possible
15585 :headline anywhere in a headline
15586 :headline-stars on the leading stars in a headline
15587 :todo-keyword on a TODO keyword (including DONE) in a headline
15588 :tags on the TAGS in a headline
15589 :priority on the priority cookie in a headline
15590 :item on the first line of a plain list item
15591 :item-bullet on the bullet/number of a plain list item
15592 :checkbox on the checkbox in a plain list item
15593 :table in an org-mode table
15594 :table-special on a special filed in a table
15595 :table-table in a table.el table
15596 :link on a hyperlink
15597 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
15598 :target on a <<target>>
15599 :radio-target on a <<<radio-target>>>
15600 :latex-fragment on a LaTeX fragment
15601 :latex-preview on a LaTeX fragment with overlayed preview image
15603 This function expects the position to be visible because it uses font-lock
15604 faces as a help to recognize the following contexts: :table-special, :link,
15606 (let* ((f (get-text-property (point) 'face))
15607 (faces (if (listp f) f (list f)))
15608 (p (point)) clist o)
15609 ;; First the large context
15611 ((org-on-heading-p t)
15612 (push (list :headline (point-at-bol) (point-at-eol)) clist)
15614 (beginning-of-line 1)
15615 (looking-at org-todo-line-tags-regexp))
15616 (push (org-point-in-group p 1 :headline-stars) clist)
15617 (push (org-point-in-group p 2 :todo-keyword) clist)
15618 (push (org-point-in-group p 4 :tags) clist))
15620 (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
15621 (if (looking-at "\\[#[A-Z0-9]\\]")
15622 (push (org-point-in-group p 0 :priority) clist)))
15625 (push (org-point-in-group p 2 :item-bullet) clist)
15626 (push (list :item (point-at-bol)
15627 (save-excursion (org-end-of-item) (point)))
15629 (and (org-at-item-checkbox-p)
15630 (push (org-point-in-group p 0 :checkbox) clist)))
15633 (push (list :table (org-table-begin) (org-table-end)) clist)
15634 (if (memq 'org-formula faces)
15635 (push (list :table-special
15636 (previous-single-property-change p 'face)
15637 (next-single-property-change p 'face)) clist)))
15638 ((org-at-table-p 'any)
15639 (push (list :table-table) clist)))
15642 ;; Now the small context
15644 ((org-at-timestamp-p)
15645 (push (org-point-in-group p 0 :timestamp) clist))
15646 ((memq 'org-link faces)
15648 (previous-single-property-change p 'face)
15649 (next-single-property-change p 'face)) clist))
15650 ((memq 'org-special-keyword faces)
15651 (push (list :keyword
15652 (previous-single-property-change p 'face)
15653 (next-single-property-change p 'face)) clist))
15655 (push (org-point-in-group p 0 :target) clist)
15656 (goto-char (1- (match-beginning 0)))
15657 (if (looking-at org-radio-target-regexp)
15658 (push (org-point-in-group p 0 :radio-target) clist))
15660 ((setq o (car (delq nil
15663 (if (memq x org-latex-fragment-image-overlays) x))
15664 (org-overlays-at (point))))))
15665 (push (list :latex-fragment
15666 (org-overlay-start o) (org-overlay-end o)) clist)
15667 (push (list :latex-preview
15668 (org-overlay-start o) (org-overlay-end o)) clist))
15669 ((org-inside-LaTeX-fragment-p)
15670 ;; FIXME: positions wrong.
15671 (push (list :latex-fragment (point) (point)) clist)))
15673 (setq clist (nreverse (delq nil clist)))
15676 ;; FIXME: Compare with at-regexp-p Do we need both?
15677 (defun org-in-regexp (re &optional nlines visually)
15678 "Check if point is inside a match of regexp.
15679 Normally only the current line is checked, but you can include NLINES extra
15680 lines both before and after point into the search.
15681 If VISUALLY is set, require that the cursor is not after the match but
15682 really on, so that the block visually is on the match."
15684 (let ((pos (point))
15685 (eol (point-at-eol (+ 1 (or nlines 0))))
15686 (inc (if visually 1 0)))
15688 (beginning-of-line (- 1 (or nlines 0)))
15689 (while (re-search-forward re eol t)
15690 (if (and (<= (match-beginning 0) pos)
15691 (>= (+ inc (match-end 0)) pos))
15692 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
15694 (defun org-at-regexp-p (regexp)
15695 "Is point inside a match of REGEXP in the current line?"
15698 (let ((pos (point)) (end (point-at-eol)))
15699 (beginning-of-line 1)
15700 (while (re-search-forward regexp end t)
15701 (if (and (<= (match-beginning 0) pos)
15702 (>= (match-end 0) pos))
15706 (defun org-occur-in-agenda-files (regexp &optional nlines)
15707 "Call `multi-occur' with buffers for all agenda files."
15708 (interactive "sOrg-files matching: \np")
15709 (let* ((files (org-agenda-files))
15710 (tnames (mapcar 'file-truename files))
15711 (extra org-agenda-text-search-extra-files)
15713 (when (eq (car extra) 'agenda-archives)
15714 (setq extra (cdr extra))
15715 (setq files (org-add-archive-files files)))
15716 (while (setq f (pop extra))
15717 (unless (member (file-truename f) tnames)
15718 (add-to-list 'files f 'append)
15719 (add-to-list 'tnames (file-truename f) 'append)))
15721 (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
15724 (if (boundp 'occur-mode-find-occurrence-hook)
15726 (add-hook 'occur-mode-find-occurrence-hook
15731 (defadvice occur-mode-goto-occurrence
15732 (after org-occur-reveal activate)
15733 (and (org-mode-p) (org-reveal)))
15734 (defadvice occur-mode-goto-occurrence-other-window
15735 (after org-occur-reveal activate)
15736 (and (org-mode-p) (org-reveal)))
15737 (defadvice occur-mode-display-occurrence
15738 (after org-occur-reveal activate)
15740 (let ((pos (occur-mode-find-occurrence)))
15741 (with-current-buffer (marker-buffer pos)
15746 (defun org-occur-link-in-agenda-files ()
15747 "Create a link and search for it in the agendas.
15748 The link is not stored in `org-stored-links', it is just created
15749 for the search purpose."
15751 (let ((link (condition-case nil
15752 (org-store-link nil)
15753 (error "Unable to create a link to here"))))
15754 (org-occur-in-agenda-files (regexp-quote link))))
15756 (defun org-uniquify (list)
15757 "Remove duplicate elements from LIST."
15759 (mapc (lambda (x) (add-to-list 'res x 'append)) list)
15762 (defun org-delete-all (elts list)
15763 "Remove all elements in ELTS from LIST."
15765 (setq list (delete (pop elts) list)))
15768 (defun org-back-over-empty-lines ()
15769 "Move backwards over whitespace, to the beginning of the first empty line.
15770 Returns the number of empty lines passed."
15771 (let ((pos (point)))
15772 (skip-chars-backward " \t\n\r")
15773 (beginning-of-line 2)
15774 (goto-char (min (point) pos))
15775 (count-lines (point) pos)))
15777 (defun org-skip-whitespace ()
15778 (skip-chars-forward " \t\n\r"))
15780 (defun org-point-in-group (point group &optional context)
15781 "Check if POINT is in match-group GROUP.
15782 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
15783 match. If the match group does ot exist or point is not inside it,
15785 (and (match-beginning group)
15786 (>= point (match-beginning group))
15787 (<= point (match-end group))
15789 (list context (match-beginning group) (match-end group))
15792 (defun org-switch-to-buffer-other-window (&rest args)
15793 "Switch to buffer in a second window on the current frame.
15794 In particular, do not allow pop-up frames."
15795 (let (pop-up-frames special-display-buffer-names special-display-regexps
15796 special-display-function)
15797 (apply 'switch-to-buffer-other-window args)))
15799 (defun org-combine-plists (&rest plists)
15800 "Create a single property list from all plists in PLISTS.
15801 The process starts by copying the first list, and then setting properties
15802 from the other lists. Settings in the last list are the most significant
15803 ones and overrule settings in the other lists."
15804 (let ((rtn (copy-sequence (pop plists)))
15807 (setq ls (pop plists))
15809 (setq p (pop ls) v (pop ls))
15810 (setq rtn (plist-put rtn p v))))
15813 (defun org-move-line-down (arg)
15814 "Move the current line down. With prefix argument, move it past ARG lines."
15816 (let ((col (current-column))
15818 (beginning-of-line 1) (setq beg (point))
15819 (beginning-of-line 2) (setq end (point))
15820 (beginning-of-line (+ 1 arg))
15821 (setq pos (move-marker (make-marker) (point)))
15822 (insert (delete-and-extract-region beg end))
15824 (org-move-to-column col)))
15826 (defun org-move-line-up (arg)
15827 "Move the current line up. With prefix argument, move it past ARG lines."
15829 (let ((col (current-column))
15831 (beginning-of-line 1) (setq beg (point))
15832 (beginning-of-line 2) (setq end (point))
15833 (beginning-of-line (- arg))
15834 (setq pos (move-marker (make-marker) (point)))
15835 (insert (delete-and-extract-region beg end))
15837 (org-move-to-column col)))
15839 (defun org-replace-escapes (string table)
15840 "Replace %-escapes in STRING with values in TABLE.
15841 TABLE is an association list with keys like \"%a\" and string values.
15842 The sequences in STRING may contain normal field width and padding information,
15843 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
15844 so values can contain further %-escapes if they are define later in TABLE."
15845 (let ((case-fold-search nil)
15847 (while (setq e (pop table))
15848 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
15849 (while (string-match re string)
15850 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
15852 (setq string (replace-match rpl t t string))))
15856 (defun org-sublist (list start end)
15857 "Return a section of LIST, from START to END.
15858 Counting starts at 1."
15859 (let (rtn (c start))
15860 (setq list (nthcdr (1- start) list))
15861 (while (and list (<= c end))
15862 (push (pop list) rtn)
15866 (defun org-find-base-buffer-visiting (file)
15867 "Like `find-buffer-visiting' but always return the base buffer and
15868 not an indirect buffer."
15869 (let ((buf (or (get-file-buffer file)
15870 (find-buffer-visiting file))))
15872 (or (buffer-base-buffer buf) buf)
15875 (defun org-image-file-name-regexp (&optional extensions)
15876 "Return regexp matching the file names of images.
15877 If EXTENSIONS is given, only match these."
15878 (if (and (not extensions) (fboundp 'image-file-name-regexp))
15879 (image-file-name-regexp)
15880 (let ((image-file-name-extensions
15882 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
15883 "xbm" "xpm" "pbm" "pgm" "ppm"))))
15885 (regexp-opt (nconc (mapcar 'upcase
15886 image-file-name-extensions)
15887 image-file-name-extensions)
15891 (defun org-file-image-p (file &optional extensions)
15892 "Return non-nil if FILE is an image."
15894 (string-match (org-image-file-name-regexp extensions) file)))
15896 (defun org-get-cursor-date ()
15897 "Return the date at cursor in as a time.
15898 This works in the calendar and in the agenda, anywhere else it just
15899 returns the current time."
15900 (let (date day defd)
15902 ((eq major-mode 'calendar-mode)
15903 (setq date (calendar-cursor-to-date)
15904 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
15905 ((eq major-mode 'org-agenda-mode)
15906 (setq day (get-text-property (point) 'day))
15908 (setq date (calendar-gregorian-from-absolute day)
15909 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
15911 (or defd (current-time))))
15913 (defvar org-agenda-action-marker (make-marker)
15914 "Marker pointing to the entry for the next agenda action.")
15916 (defun org-mark-entry-for-agenda-action ()
15917 "Mark the current entry as target of an agenda action.
15918 Agenda actions are actions executed from the agenda with the key `k',
15919 which make use of the date at the cursor."
15921 (move-marker org-agenda-action-marker
15922 (save-excursion (org-back-to-heading t) (point))
15925 "Entry marked for action; press `k' at desired date in agenda or calendar"))
15927 ;;; Paragraph filling stuff.
15928 ;; We want this to be just right, so use the full arsenal.
15930 (defun org-indent-line-function ()
15931 "Indent line like previous, but further if previous was headline or item."
15933 (let* ((pos (point))
15934 (itemp (org-at-item-p))
15935 (case-fold-search t)
15936 (org-drawer-regexp (or org-drawer-regexp "\000"))
15937 column bpos bcol tpos tcol bullet btype bullet-type)
15938 ;; Find the previous relevant line
15939 (beginning-of-line 1)
15941 ((looking-at "#") (setq column 0))
15942 ((looking-at "\\*+ ") (setq column 0))
15943 ((and (looking-at "[ \t]*:END:")
15944 (save-excursion (re-search-backward org-drawer-regexp nil t)))
15946 (goto-char (1- (match-beginning 1)))
15947 (setq column (current-column))))
15948 ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
15950 (re-search-backward
15951 (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
15952 (setq column (org-get-indentation (match-string 0))))
15954 (beginning-of-line 0)
15955 (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
15956 (not (looking-at "[ \t]*:END:"))
15957 (not (looking-at org-drawer-regexp)))
15958 (beginning-of-line 0))
15960 ((looking-at "\\*+[ \t]+")
15961 (if (not org-adapt-indentation)
15963 (goto-char (match-end 0))
15964 (setq column (current-column))))
15965 ((looking-at org-drawer-regexp)
15966 (goto-char (1- (match-beginning 1)))
15967 (setq column (current-column)))
15968 ((looking-at "\\([ \t]*\\):END:")
15969 (goto-char (match-end 1))
15970 (setq column (current-column)))
15972 (org-beginning-of-item)
15973 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
15974 (setq bpos (match-beginning 1) tpos (match-end 0)
15975 bcol (progn (goto-char bpos) (current-column))
15976 tcol (progn (goto-char tpos) (current-column))
15977 bullet (match-string 1)
15978 bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
15979 (if (> tcol (+ bcol org-description-max-indent))
15980 (setq tcol (+ bcol 5)))
15984 (beginning-of-line 1)
15985 (if (looking-at "\\S-")
15987 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
15988 (setq bullet (match-string 1)
15989 btype (if (string-match "[0-9]" bullet) "n" bullet))
15990 (setq column (if (equal btype bullet-type) bcol tcol)))
15991 (setq column (org-get-indentation)))))
15992 (t (setq column (org-get-indentation))))))
15994 (if (<= (current-column) (current-indentation))
15995 (org-indent-line-to column)
15996 (save-excursion (org-indent-line-to column)))
15997 (setq column (current-column))
15998 (beginning-of-line 1)
16000 "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
16001 (replace-match (concat "\\1" (format org-property-format
16002 (match-string 2) (match-string 3)))
16004 (org-move-to-column column)))
16006 (defun org-set-autofill-regexps ()
16008 ;; In the paragraph separator we include headlines, because filling
16009 ;; text in a line directly attached to a headline would otherwise
16010 ;; fill the headline as well.
16011 (org-set-local 'comment-start-skip "^#+[ \t]*")
16012 (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
16013 ;; The paragraph starter includes hand-formatted lists.
16020 "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
16023 "\\\\\\(begin\\|end\\|[][]\\)"))
16024 ;; Inhibit auto-fill for headers, tables and fixed-width lines.
16025 ;; But only if the user has not turned off tables or fixed-width regions
16027 'auto-fill-inhibit-regexp
16028 (concat "\\*+ \\|#\\+"
16029 "\\|[ \t]*" org-keyword-time-regexp
16030 (if (or org-enable-table-editor org-enable-fixed-width-editor)
16033 (if org-enable-table-editor "|" "")
16034 (if org-enable-fixed-width-editor ":" "")
16036 ;; We use our own fill-paragraph function, to make sure that tables
16037 ;; and fixed-width regions are not wrapped. That function will pass
16038 ;; through to `fill-paragraph' when appropriate.
16039 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
16040 ; Adaptive filling: To get full control, first make sure that
16041 ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
16042 (org-set-local 'adaptive-fill-regexp "\000")
16043 (org-set-local 'adaptive-fill-function
16044 'org-adaptive-fill-function)
16046 'align-mode-rules-list
16047 '((org-in-buffer-settings
16048 (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
16049 (modes . '(org-mode))))))
16051 (defun org-fill-paragraph (&optional justify)
16052 "Re-align a table, pass through to fill-paragraph if no table."
16053 (let ((table-p (org-at-table-p))
16054 (table.el-p (org-at-table.el-p)))
16055 (cond ((and (equal (char-after (point-at-bol)) ?*)
16056 (save-excursion (goto-char (point-at-bol))
16057 (looking-at outline-regexp)))
16058 t) ; skip headlines
16059 (table.el-p t) ; skip table.el tables
16060 (table-p (org-table-align) t) ; align org-mode tables
16061 (t nil)))) ; call paragraph-fill
16063 ;; For reference, this is the default value of adaptive-fill-regexp
16064 ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
16066 (defun org-adaptive-fill-function ()
16067 "Return a fill prefix for org-mode files.
16068 In particular, this makes sure hanging paragraphs for hand-formatted lists
16070 (cond ((looking-at "#[ \t]+")
16072 ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
16074 (if (> (match-end 1) (+ (match-beginning 1)
16075 org-description-max-indent))
16076 (goto-char (+ (match-beginning 1) 5))
16077 (goto-char (match-end 0)))
16078 (make-string (current-column) ?\ )))
16079 ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
16081 (goto-char (match-end 0))
16082 (make-string (current-column) ?\ )))
16087 (defun org-toggle-fixed-width-section (arg)
16088 "Toggle the fixed-width export.
16089 If there is no active region, the QUOTE keyword at the current headline is
16090 inserted or removed. When present, it causes the text between this headline
16091 and the next to be exported as fixed-width text, and unmodified.
16092 If there is an active region, this command adds or removes a colon as the
16093 first character of this line. If the first character of a line is a colon,
16094 this line is also exported in fixed-width font."
16097 (regionp (org-region-active-p))
16098 (beg (if regionp (region-beginning) (point)))
16099 (end (if regionp (region-end)))
16100 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
16101 (case-fold-search nil)
16102 (re "[ \t]*\\(: \\)")
16107 (setq cc (current-column))
16108 (beginning-of-line 1)
16109 (setq off (looking-at re))
16110 (while (> nlines 0)
16111 (setq nlines (1- nlines))
16112 (beginning-of-line 1)
16115 (org-move-to-column cc t)
16118 ((and off (looking-at re))
16119 (replace-match "" t t nil 1))
16120 ((not off) (org-move-to-column cc t) (insert ": ")))
16123 (org-back-to-heading)
16124 (if (looking-at (concat outline-regexp
16125 "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
16126 (replace-match "" t t nil 1)
16127 (if (looking-at outline-regexp)
16129 (goto-char (match-end 0))
16130 (insert org-quote-string " "))))))))
16132 (defun org-reftex-citation ()
16133 "Use reftex-citation to insert a citation into the buffer.
16134 This looks for a line like
16136 #+BIBLIOGRAPHY: foo plain option:-d
16138 and derives from it that foo.bib is the bbliography file relevant
16139 for this document. It then installs the necessary environment for RefTeX
16140 to work in this buffer and calls `reftex-citation' to insert a citation
16143 Export of such citations to both LaTeX and HTML is handled by the contributed
16144 package org-exp-bibtex by Taru Karttunen."
16146 (let ((reftex-docstruct-symbol 'rds)
16147 (reftex-cite-format "\\cite{%l}")
16152 (let ((case-fold-search t)
16153 (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
16154 (if (not (save-excursion
16155 (or (re-search-forward re nil t)
16156 (re-search-backward re nil t))))
16157 (error "No bibliography defined in file")
16158 (setq bib (concat (match-string 1) ".bib")
16159 rds (list (list 'bib bib)))))))
16160 (call-interactively 'reftex-citation)))
16162 ;;;; Functions extending outline functionality
16164 (defun org-beginning-of-line (&optional arg)
16165 "Go to the beginning of the current line. If that is invisible, continue
16166 to a visible line beginning. This makes the function of C-a more intuitive.
16167 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
16168 first attempt, and only move to after the tags when the cursor is already
16169 beyond the end of the headline."
16171 (let ((pos (point))
16172 (special (if (consp org-special-ctrl-a/e)
16173 (car org-special-ctrl-a/e)
16174 org-special-ctrl-a/e))
16176 (if (org-bound-and-true-p line-move-visual)
16177 (beginning-of-visual-line 1)
16178 (beginning-of-line 1))
16179 (if (and arg (fboundp 'move-beginning-of-line))
16180 (call-interactively 'move-beginning-of-line)
16184 (if (org-invisible-p)
16185 (while (and (not (bobp)) (org-invisible-p))
16187 (beginning-of-line 1))
16188 (forward-char 1))))
16191 ((and (looking-at org-complex-heading-regexp)
16192 (= (char-after (match-end 1)) ?\ ))
16193 (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
16197 (cond ((> pos refpos) refpos)
16198 ((= pos (point)) refpos)
16200 (cond ((> pos (point)) (point))
16201 ((not (eq last-command this-command)) (point))
16206 (cond ((> pos (match-end 4)) (match-end 4))
16207 ((= pos (point)) (match-end 4))
16209 (cond ((> pos (point)) (point))
16210 ((not (eq last-command this-command)) (point))
16211 (t (match-end 4))))))))
16213 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
16215 (defun org-end-of-line (&optional arg)
16216 "Go to the end of the line.
16217 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
16218 first attempt, and only move to after the tags when the cursor is already
16219 beyond the end of the headline."
16221 (let ((special (if (consp org-special-ctrl-a/e)
16222 (cdr org-special-ctrl-a/e)
16223 org-special-ctrl-a/e)))
16224 (if (or (not special)
16225 (not (org-on-heading-p))
16227 (call-interactively
16228 (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
16229 ((fboundp 'move-end-of-line) 'move-end-of-line)
16231 (let ((pos (point)))
16232 (beginning-of-line 1)
16233 (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
16235 (if (or (< pos (match-beginning 1))
16236 (= pos (match-end 0)))
16237 (goto-char (match-beginning 1))
16238 (goto-char (match-end 0)))
16239 (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
16240 (goto-char (match-end 0))
16241 (goto-char (match-beginning 1))))
16242 (call-interactively (if (fboundp 'move-end-of-line)
16246 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
16248 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
16249 (define-key org-mode-map "\C-e" 'org-end-of-line)
16251 (defun org-backward-sentence (&optional arg)
16252 "Go to beginning of sentence, or beginning of table field.
16253 This will call `backward-sentence' or `org-table-beginning-of-field',
16254 depending on context."
16257 ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
16258 (t (call-interactively 'backward-sentence))))
16260 (defun org-forward-sentence (&optional arg)
16261 "Go to end of sentence, or end of table field.
16262 This will call `forward-sentence' or `org-table-end-of-field',
16263 depending on context."
16266 ((org-at-table-p) (call-interactively 'org-table-end-of-field))
16267 (t (call-interactively 'forward-sentence))))
16269 (define-key org-mode-map "\M-a" 'org-backward-sentence)
16270 (define-key org-mode-map "\M-e" 'org-forward-sentence)
16272 (defun org-kill-line (&optional arg)
16273 "Kill line, to tags or end of line."
16276 ((or (not org-special-ctrl-k)
16278 (not (org-on-heading-p)))
16279 (call-interactively 'kill-line))
16280 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
16281 (kill-region (point) (match-beginning 1))
16282 (org-set-tags nil t))
16283 (t (kill-region (point) (point-at-eol)))))
16285 (define-key org-mode-map "\C-k" 'org-kill-line)
16287 (defun org-yank (&optional arg)
16288 "Yank. If the kill is a subtree, treat it specially.
16289 This command will look at the current kill and check if is a single
16290 subtree, or a series of subtrees[1]. If it passes the test, and if the
16291 cursor is at the beginning of a line or after the stars of a currently
16292 empty headline, then the yank is handled specially. How exactly depends
16293 on the value of the following variables, both set by default.
16295 org-yank-folded-subtrees
16296 When set, the subtree(s) will be folded after insertion, but only
16297 if doing so would now swallow text after the yanked text.
16299 org-yank-adjusted-subtrees
16300 When set, the subtree will be promoted or demoted in order to
16301 fit into the local outline tree structure, which means that the level
16302 will be adjusted so that it becomes the smaller one of the two
16303 *visible* surrounding headings.
16305 Any prefix to this command will cause `yank' to be called directly with
16306 no special treatment. In particular, a simple `C-u' prefix will just
16307 plainly yank the text as it is.
16309 \[1] The test checks if the first non-white line is a heading
16310 and if there are no other headings with fewer stars."
16312 (org-yank-generic 'yank arg))
16314 (defun org-yank-generic (command arg)
16315 "Perform some yank-like command.
16317 This function implements the behavior described in the `org-yank'
16318 documentation. However, it has been generalized to work for any
16319 interactive command with similar behavior."
16321 ;; pretend to be command COMMAND
16322 (setq this-command command)
16325 (call-interactively command)
16327 (let ((subtreep ; is kill a subtree, and the yank position appropriate?
16328 (and (org-kill-is-subtree-p)
16330 (and (looking-at "[ \t]*$")
16333 (buffer-substring (point-at-bol) (point)))))))
16336 ((and subtreep org-yank-folded-subtrees)
16337 (let ((beg (point))
16339 (if (and subtreep org-yank-adjusted-subtrees)
16340 (org-paste-subtree nil nil 'for-yank)
16341 (call-interactively command))
16345 (when (and (bolp) subtreep
16346 (not (setq swallowp
16347 (org-yank-folding-would-swallow-text beg end))))
16348 (or (looking-at outline-regexp)
16349 (re-search-forward (concat "^" outline-regexp) end t))
16350 (while (and (< (point) end) (looking-at outline-regexp))
16352 (org-cycle-show-empty-lines 'folded)
16353 (condition-case nil
16354 (outline-forward-same-level 1)
16355 (error (goto-char end)))))
16358 "Inserted text not folded because that would swallow text"))
16361 (skip-chars-forward " \t\n\r")
16362 (beginning-of-line 1)
16363 (push-mark beg 'nomsg)))
16364 ((and subtreep org-yank-adjusted-subtrees)
16365 (let ((beg (point-at-bol)))
16366 (org-paste-subtree nil nil 'for-yank)
16367 (push-mark beg 'nomsg)))
16369 (call-interactively command))))))
16371 (defun org-yank-folding-would-swallow-text (beg end)
16372 "Would hide-subtree at BEG swallow any text after END?"
16376 (when (or (looking-at outline-regexp)
16377 (re-search-forward (concat "^" outline-regexp) end t))
16378 (setq level (org-outline-level)))
16380 (skip-chars-forward " \t\r\n\v\f")
16382 (and (bolp) (looking-at org-outline-regexp)
16383 (<= (org-outline-level) level)))
16384 nil ; Nothing would be swallowed
16385 t)))) ; something would swallow
16387 (define-key org-mode-map "\C-y" 'org-yank)
16389 (defun org-invisible-p ()
16390 "Check if point is at a character currently not visible."
16391 ;; Early versions of noutline don't have `outline-invisible-p'.
16392 (if (fboundp 'outline-invisible-p)
16393 (outline-invisible-p)
16394 (get-char-property (point) 'invisible)))
16396 (defun org-invisible-p2 ()
16397 "Check if point is at a character currently not visible."
16399 (if (and (eolp) (not (bobp))) (backward-char 1))
16400 ;; Early versions of noutline don't have `outline-invisible-p'.
16401 (if (fboundp 'outline-invisible-p)
16402 (outline-invisible-p)
16403 (get-char-property (point) 'invisible))))
16405 (defun org-back-to-heading (&optional invisible-ok)
16406 "Call `outline-back-to-heading', but provide a better error message."
16407 (condition-case nil
16408 (outline-back-to-heading invisible-ok)
16409 (error (error "Before first headline at position %d in buffer %s"
16410 (point) (current-buffer)))))
16412 (defun org-before-first-heading-p ()
16413 "Before first heading?"
16415 (null (re-search-backward "^\\*+ " nil t))))
16417 (defalias 'org-on-heading-p 'outline-on-heading-p)
16418 (defalias 'org-at-heading-p 'outline-on-heading-p)
16419 (defun org-at-heading-or-item-p ()
16420 (or (org-on-heading-p) (org-at-item-p)))
16422 (defun org-on-target-p ()
16423 (or (org-in-regexp org-radio-target-regexp)
16424 (org-in-regexp org-target-regexp)))
16426 (defun org-up-heading-all (arg)
16427 "Move to the heading line of which the present line is a subheading.
16428 This function considers both visible and invisible heading lines.
16429 With argument, move up ARG levels."
16430 (if (fboundp 'outline-up-heading-all)
16431 (outline-up-heading-all arg) ; emacs 21 version of outline.el
16432 (outline-up-heading arg t))) ; emacs 22 version of outline.el
16434 (defun org-up-heading-safe ()
16435 "Move to the heading line of which the present line is a subheading.
16436 This version will not throw an error. It will return the level of the
16437 headline found, or nil if no higher level is found.
16439 Also, this function will be a lot faster than `outline-up-heading',
16440 because it relies on stars being the outline starters. This can really
16441 make a significant difference in outlines with very many siblings."
16442 (let (start-level re)
16443 (org-back-to-heading t)
16444 (setq start-level (funcall outline-level))
16445 (if (equal start-level 1)
16447 (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
16448 (if (re-search-backward re nil t)
16449 (funcall outline-level)))))
16451 (defun org-first-sibling-p ()
16452 "Is this heading the first child of its parents?"
16454 (let ((re (concat "^" outline-regexp))
16456 (unless (org-at-heading-p t)
16457 (error "Not at a heading"))
16458 (setq level (funcall outline-level))
16460 (if (not (re-search-backward re nil t))
16462 (setq l (funcall outline-level))
16465 (defun org-goto-sibling (&optional previous)
16466 "Goto the next sibling, even if it is invisible.
16467 When PREVIOUS is set, go to the previous sibling instead. Returns t
16468 when a sibling was found. When none is found, return nil and don't
16470 (let ((fun (if previous 're-search-backward 're-search-forward))
16472 (re (concat "^" outline-regexp))
16474 (when (condition-case nil (org-back-to-heading t) (error nil))
16475 (setq level (funcall outline-level))
16477 (or previous (forward-char 1))
16478 (while (funcall fun re nil t)
16479 (setq l (funcall outline-level))
16480 (when (< l level) (goto-char pos) (throw 'exit nil))
16481 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
16485 (defun org-show-siblings ()
16486 "Show all siblings of the current headline."
16488 (while (org-goto-sibling) (org-flag-heading nil)))
16490 (while (org-goto-sibling 'previous)
16491 (org-flag-heading nil))))
16493 (defun org-show-hidden-entry ()
16494 "Show an entry where even the heading is hidden."
16498 (defun org-flag-heading (flag &optional entry)
16499 "Flag the current heading. FLAG non-nil means make invisible.
16500 When ENTRY is non-nil, show the entire entry."
16502 (org-back-to-heading t)
16503 ;; Check if we should show the entire entry
16508 (and (outline-next-heading)
16509 (org-flag-heading nil))))
16510 (outline-flag-region (max (point-min) (1- (point)))
16511 (save-excursion (outline-end-of-heading) (point))
16514 (defun org-get-next-sibling ()
16515 "Move to next heading of the same level, and return point.
16516 If there is no such heading, return nil.
16517 This is like outline-next-sibling, but invisible headings are ok."
16518 (let ((level (funcall outline-level)))
16519 (outline-next-heading)
16520 (while (and (not (eobp)) (> (funcall outline-level) level))
16521 (outline-next-heading))
16522 (if (or (eobp) (< (funcall outline-level) level))
16526 (defun org-end-of-subtree (&optional invisible-OK to-heading)
16527 ;; This contains an exact copy of the original function, but it uses
16528 ;; `org-back-to-heading', to make it work also in invisible
16529 ;; trees. And is uses an invisible-OK argument.
16530 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
16531 ;; Furthermore, when used inside Org, finding the end of a large subtree
16532 ;; with many children and grandchildren etc, this can be much faster
16533 ;; than the outline version.
16534 (org-back-to-heading invisible-OK)
16536 (level (funcall outline-level)))
16537 (if (and (org-mode-p) (< level 1000))
16538 ;; A true heading (not a plain list item), in Org-mode
16539 ;; This means we can easily find the end by looking
16540 ;; only for the right number of stars. Using a regexp to do
16541 ;; this is so much faster than using a Lisp loop.
16542 (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
16544 (and (re-search-forward re nil 'move) (beginning-of-line 1)))
16545 ;; something else, do it the slow way
16546 (while (and (not (eobp))
16547 (or first (> (funcall outline-level) level)))
16549 (outline-next-heading)))
16551 (if (memq (preceding-char) '(?\n ?\^M))
16553 ;; Go to end of line before heading
16555 (if (memq (preceding-char) '(?\n ?\^M))
16556 ;; leave blank line before heading
16557 (forward-char -1))))))
16560 (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
16561 "Use Org version in org-mode, for dramatic speed-up."
16562 (if (eq major-mode 'org-mode)
16563 (org-end-of-subtree)
16566 (defun org-forward-same-level (arg &optional invisible-ok)
16567 "Move forward to the arg'th subheading at same level as this one.
16568 Stop at the first and last subheadings of a superior heading."
16570 (org-back-to-heading)
16572 (let* ((level (- (match-end 0) (match-beginning 0) 1))
16573 (re (format "^\\*\\{1,%d\\} " level))
16577 (while (and (re-search-forward re nil 'move)
16578 (setq l (- (match-end 0) (match-beginning 0) 1))
16582 (if (< l level) (setq arg 1)))
16583 (setq arg (1- arg)))
16584 (beginning-of-line 1)))
16586 (defun org-backward-same-level (arg &optional invisible-ok)
16587 "Move backward to the arg'th subheading at same level as this one.
16588 Stop at the first and last subheadings of a superior heading."
16590 (org-back-to-heading)
16592 (let* ((level (- (match-end 0) (match-beginning 0) 1))
16593 (re (format "^\\*\\{1,%d\\} " level))
16596 (while (and (re-search-backward re nil 'move)
16597 (setq l (- (match-end 0) (match-beginning 0) 1))
16601 (if (< l level) (setq arg 1)))
16602 (setq arg (1- arg)))))
16604 (defun org-show-subtree ()
16605 "Show everything after this heading at deeper levels."
16606 (outline-flag-region
16609 (outline-end-of-subtree) (outline-next-heading) (point))
16612 (defun org-show-entry ()
16613 "Show the body directly following this heading.
16614 Show the heading too, if it is currently invisible."
16617 (condition-case nil
16619 (org-back-to-heading t)
16620 (outline-flag-region
16621 (max (point-min) (1- (point)))
16623 (if (re-search-forward
16624 (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
16625 (match-beginning 1)
16628 (org-cycle-hide-drawers 'children))
16631 (defun org-make-options-regexp (kwds &optional extra)
16632 "Make a regular expression for keyword lines."
16636 (mapconcat 'regexp-quote kwds "\\|")
16637 (if extra (concat "\\|" extra))
16641 ;; Make isearch reveal the necessary context
16642 (defun org-isearch-end ()
16643 "Reveal context after isearch exits."
16644 (when isearch-success ; only if search was successful
16645 (if (featurep 'xemacs)
16646 ;; Under XEmacs, the hook is run in the correct place,
16647 ;; we directly show the context.
16648 (org-show-context 'isearch)
16649 ;; In Emacs the hook runs *before* restoring the overlays.
16650 ;; So we have to use a one-time post-command-hook to do this.
16651 ;; (Emacs 22 has a special variable, see function `org-mode')
16652 (unless (and (boundp 'isearch-mode-end-hook-quit)
16653 isearch-mode-end-hook-quit)
16654 ;; Only when the isearch was not quitted.
16655 (org-add-hook 'post-command-hook 'org-isearch-post-command
16656 'append 'local)))))
16658 (defun org-isearch-post-command ()
16659 "Remove self from hook, and show context."
16660 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
16661 (org-show-context 'isearch))
16664 ;;;; Integration with and fixes for other packages
16668 (defvar org-imenu-markers nil
16669 "All markers currently used by Imenu.")
16670 (make-variable-buffer-local 'org-imenu-markers)
16672 (defun org-imenu-new-marker (&optional pos)
16673 "Return a new marker for use by Imenu, and remember the marker."
16674 (let ((m (make-marker)))
16675 (move-marker m (or pos (point)))
16676 (push m org-imenu-markers)
16679 (defun org-imenu-get-tree ()
16680 "Produce the index for Imenu."
16681 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
16682 (setq org-imenu-markers nil)
16683 (let* ((n org-imenu-depth)
16684 (re (concat "^" outline-regexp))
16685 (subs (make-vector (1+ n) nil))
16691 (goto-char (point-max))
16692 (while (re-search-backward re nil t)
16693 (setq level (org-reduced-level (funcall outline-level)))
16695 (looking-at org-complex-heading-regexp)
16696 (setq head (org-link-display-format
16697 (org-match-string-no-properties 4))
16698 m (org-imenu-new-marker))
16699 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
16700 (if (>= level last-level)
16701 (push (cons head m) (aref subs level))
16702 (push (cons head (aref subs (1+ level))) (aref subs level))
16703 (loop for i from (1+ level) to n do (aset subs i nil)))
16704 (setq last-level level)))))
16707 (eval-after-load "imenu"
16709 (add-hook 'imenu-after-jump-hook
16711 (if (eq major-mode 'org-mode)
16712 (org-show-context 'org-goto))))))
16714 (defun org-link-display-format (link)
16715 "Replace a link with either the description, or the link target
16716 if no description is present"
16718 (if (string-match org-bracket-link-analytic-regexp link)
16719 (replace-match (or (match-string 5 link)
16720 (concat (match-string 1 link)
16721 (match-string 3 link)))
16725 ;; Speedbar support
16727 (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
16728 "Overlay marking the agenda restriction line in speedbar.")
16729 (org-overlay-put org-speedbar-restriction-lock-overlay
16730 'face 'org-agenda-restriction-lock)
16731 (org-overlay-put org-speedbar-restriction-lock-overlay
16732 'help-echo "Agendas are currently limited to this item.")
16733 (org-detach-overlay org-speedbar-restriction-lock-overlay)
16735 (defun org-speedbar-set-agenda-restriction ()
16736 "Restrict future agenda commands to the location at point in speedbar.
16737 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
16739 (require 'org-agenda)
16740 (let (p m tp np dir txt)
16742 ((setq p (text-property-any (point-at-bol) (point-at-eol)
16744 (setq m (get-text-property p 'org-imenu-marker))
16747 (set-buffer (marker-buffer m))
16749 (org-agenda-set-restriction-lock 'subtree))))
16750 ((setq p (text-property-any (point-at-bol) (point-at-eol)
16751 'speedbar-function 'speedbar-find-file))
16752 (setq tp (previous-single-property-change
16753 (1+ p) 'speedbar-function)
16754 np (next-single-property-change
16755 tp 'speedbar-function)
16756 dir (speedbar-line-directory)
16757 txt (buffer-substring-no-properties (or tp (point-min))
16758 (or np (point-max))))
16761 (set-buffer (find-file-noselect
16762 (let ((default-directory dir))
16763 (expand-file-name txt))))
16764 (unless (org-mode-p)
16765 (error "Cannot restrict to non-Org-mode file"))
16766 (org-agenda-set-restriction-lock 'file))))
16767 (t (error "Don't know how to restrict Org-mode's agenda")))
16768 (org-move-overlay org-speedbar-restriction-lock-overlay
16769 (point-at-bol) (point-at-eol))
16770 (setq current-prefix-arg nil)
16771 (org-agenda-maybe-redo)))
16773 (eval-after-load "speedbar"
16775 (speedbar-add-supported-extension ".org")
16776 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
16777 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
16778 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
16779 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
16780 (add-hook 'speedbar-visiting-tag-hook
16781 (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
16784 ;;; Fixes and Hacks for problems with other packages
16786 ;; Make flyspell not check words in links, to not mess up our keymap
16787 (defun org-mode-flyspell-verify ()
16788 "Don't let flyspell put overlays at active buttons."
16789 (and (not (get-text-property (point) 'keymap))
16790 (not (get-text-property (point) 'org-no-flyspell))))
16792 (defun org-remove-flyspell-overlays-in (beg end)
16793 "Remove flyspell overlays in region."
16794 (and (org-bound-and-true-p flyspell-mode)
16795 (fboundp 'flyspell-delete-region-overlays)
16796 (flyspell-delete-region-overlays beg end))
16797 (add-text-properties beg end '(org-no-flyspell t)))
16799 ;; Make `bookmark-jump' show the jump location if it was hidden.
16800 (eval-after-load "bookmark"
16801 '(if (boundp 'bookmark-after-jump-hook)
16802 ;; We can use the hook
16803 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
16804 ;; Hook not available, use advice
16805 (defadvice bookmark-jump (after org-make-visible activate)
16806 "Make the position visible."
16807 (org-bookmark-jump-unhide))))
16809 ;; Make sure saveplace show the location if it was hidden
16810 (eval-after-load "saveplace"
16811 '(defadvice save-place-find-file-hook (after org-make-visible activate)
16812 "Make the position visible."
16813 (org-bookmark-jump-unhide)))
16815 (defun org-bookmark-jump-unhide ()
16816 "Unhide the current position, to show the bookmark location."
16818 (or (org-invisible-p)
16819 (save-excursion (goto-char (max (point-min) (1- (point))))
16820 (org-invisible-p)))
16821 (org-show-context 'bookmark-jump)))
16823 ;; Make session.el ignore our circular variable
16824 (eval-after-load "session"
16825 '(add-to-list 'session-globals-exclude 'org-mark-ring))
16827 ;;;; Experimental code
16829 (defun org-closed-in-range ()
16830 "Sparse tree of items closed in a certain time range.
16831 Still experimental, may disappear in the future."
16833 ;; Get the time interval from the user.
16834 (let* ((time1 (time-to-seconds
16835 (org-read-date nil 'to-time nil "Starting date: ")))
16836 (time2 (time-to-seconds
16837 (org-read-date nil 'to-time nil "End date:")))
16838 ;; callback function
16839 (callback (lambda ()
16842 (apply 'encode-time
16843 (org-parse-time-string
16844 (match-string 1))))))
16845 ;; check if time in interval
16846 (and (>= time time1) (<= time time2))))))
16847 ;; make tree, check each match with the callback
16848 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
16854 (run-hooks 'org-load-hook)
16856 ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
16858 ;;; org.el ends here