1 ;;; org.el --- Outline-based notes management and organizer
2 ;; Carstens outline-mode for keeping track of everything.
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
29 ;; project planning with a fast and effective plain-text system.
31 ;; Org-mode develops organizational tasks around NOTES files that contain
32 ;; information about projects as plain text. Org-mode is implemented on
33 ;; top of outline-mode, which makes it possible to keep the content of
34 ;; large files well structured. Visibility cycling and structure editing
35 ;; help to work with the tree. Tables are easily created with a built-in
36 ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
37 ;; and scheduling. It dynamically compiles entries into an agenda that
38 ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
39 ;; Plain text URL-like links connect to websites, emails, Usenet
40 ;; messages, BBDB entries, and any files related to the projects. For
41 ;; printing and sharing of notes, an Org-mode file can be exported as a
42 ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
43 ;; iCalendar file. It can also serve as a publishing tool for a set of
46 ;; Installation and Activation
47 ;; ---------------------------
48 ;; See the corresponding sections in the manual at
50 ;; http://orgmode.org/org.html#Installation
54 ;; The documentation of Org-mode can be found in the TeXInfo file. The
55 ;; distribution also contains a PDF version of it. At the homepage of
56 ;; Org-mode, you can read the same text online as HTML. There is also an
57 ;; excellent reference card made by Philip Rooke. This card can be found
58 ;; in the etc/ directory of Emacs 22.
60 ;; A list of recent changes can be found at
61 ;; http://orgmode.org/Changes.html
65 (defvar org-inhibit-highlight-removal nil
) ; dynamically scoped param
66 (defvar org-table-formula-constants-local nil
67 "Local version of `org-table-formula-constants'.")
68 (make-variable-buffer-local 'org-table-formula-constants-local
)
70 ;;;; Require other packages
76 ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
77 ;; the file noutline.el being loaded.
78 (if (featurep 'xemacs
) (condition-case nil
(require 'noutline
)))
79 ;; We require noutline, which might be provided in outline.el
80 (require 'outline
) (require 'noutline
)
81 ;; Other stuff we need.
83 (unless (fboundp 'time-subtract
) (defalias 'time-subtract
'subtract-time
))
90 ;;;; Customization variables
94 (defconst org-version
"6.05pre01"
95 "The version number of the file org.el.")
97 (defun org-version (&optional here
)
98 "Show the org-mode version in the echo area.
99 With prefix arg HERE, insert it at point."
101 (let ((version (format "Org-mode version %s" org-version
)))
106 ;;; Compatibility constants
108 ;;; The custom variables
111 "Outline-based notes management and organizer."
117 (defcustom org-load-hook nil
118 "Hook that is run after org.el has been loaded."
122 (defvar org-modules
) ; defined below
123 (defvar org-modules-loaded nil
124 "Have the modules been loaded already?")
126 (defun org-load-modules-maybe (&optional force
)
127 "Load all extensions listed in `org-default-extensions'."
128 (when (or force
(not org-modules-loaded
))
130 (condition-case nil
(require ext
)
131 (error (message "Problems while trying to load feature `%s'" ext
))))
133 (setq org-modules-loaded t
)))
135 (defun org-set-modules (var value
)
136 "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
138 (when (featurep 'org
)
139 (org-load-modules-maybe 'force
)))
141 (when (org-bound-and-true-p org-modules
)
142 (let ((a (member 'org-infojs org-modules
)))
143 (and a
(setcar a
'org-jsinfo
))))
145 (defcustom org-modules
'(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-wl
)
146 "Modules that should always be loaded together with org.el.
147 If a description starts with <C>, the file is not part of Emacs
148 and loading it will require that you have downloaded and properly installed
149 the org-mode distribution.
151 You can also use this system to load external packages (i.e. neither Org
152 core modules, not modules from the CONTRIB directory). Just add symbols
153 to the end of the list. If the package is called org-xyz.el, then you need
154 to add the symbol `xyz', and the package must have a call to
158 :set
'org-set-modules
161 (const :tag
" bbdb: Links to BBDB entries" org-bbdb
)
162 (const :tag
" bibtex: Links to BibTeX entries" org-bibtex
)
163 (const :tag
" gnus: Links to GNUS folders/messages" org-gnus
)
164 (const :tag
" id: Global id's for identifying entries" org-id
)
165 (const :tag
" info: Links to Info nodes" org-info
)
166 (const :tag
" jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo
)
167 (const :tag
" irc: Links to IRC/ERC chat sessions" org-irc
)
168 (const :tag
" mac-message: Links to messages in Apple Mail" org-mac-message
)
169 (const :tag
" mew Links to Mew folders/messages" org-mew
)
170 (const :tag
" mhe: Links to MHE folders/messages" org-mhe
)
171 (const :tag
" rmail: Links to RMAIL folders/messages" org-rmail
)
172 (const :tag
" vm: Links to VM folders/messages" org-vm
)
173 (const :tag
" wl: Links to Wanderlust folders/messages" org-wl
)
174 (const :tag
" mouse: Additional mouse support" org-mouse
)
176 (const :tag
"C annotate-file: Annotate a file with org syntax" org-annotate-file
)
177 (const :tag
"C annotation-helper: Call Remeber directly from Browser" org-annotation-helper
)
178 (const :tag
"C bookmark: Org links to bookmarks" org-bookmark
)
179 (const :tag
"C depend: TODO dependencies for Org-mode" org-depend
)
180 (const :tag
"C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol
)
181 (const :tag
"C eval: Include command output as text" org-eval
)
182 (const :tag
"C expiry: Expiry mechanism for Org entries" org-expiry
)
183 (const :tag
"C id: Global id's for identifying entries" org-id
)
184 (const :tag
"C interactive-query: Interactive modification of tags query" org-interactive-query
)
185 (const :tag
"C mairix: Hook mairix search into Org for different MUAs" org-mairix
)
186 (const :tag
"C man: Support for links to manpages in Org-mode" org-man
)
187 (const :tag
"C mtags: Support for muse-like tags" org-mtags
)
188 (const :tag
"C panel: Simple routines for us with bad memory" org-panel
)
189 (const :tag
"C registry: A registry for Org links" org-registry
)
190 (const :tag
"C org2rem: Convert org appointments into reminders" org2rem
)
191 (const :tag
"C screen: Visit screen sessions through Org-mode links" org-screen
)
192 (const :tag
"C toc: Table of contents for Org-mode buffer" org-toc
)
193 (const :tag
"C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert
)
194 (repeat :tag
"External packages" :inline t
(symbol :tag
"Package"))))
197 (defgroup org-startup nil
198 "Options concerning startup of Org-mode."
202 (defcustom org-startup-folded t
203 "Non-nil means, entering Org-mode will switch to OVERVIEW.
204 This can also be configured on a per-file basis by adding one of
205 the following lines anywhere in the buffer:
212 (const :tag
"nofold: show all" nil
)
213 (const :tag
"fold: overview" t
)
214 (const :tag
"content: all headlines" content
)))
216 (defcustom org-startup-truncated t
217 "Non-nil means, entering Org-mode will set `truncate-lines'.
218 This is useful since some lines containing links can be very long and
219 uninteresting. Also tables look terrible when wrapped."
223 (defcustom org-startup-indented nil
224 "Non-nil means, turn on `org-indent-mode' on startup.
225 This can also be configured on a per-file basis by adding one of
226 the following lines anywhere in the buffer:
228 #+STARTUP: localindent
231 :group
'org-structure
233 (const :tag
"Not" nil
)
234 (const :tag
"Locally" local
)
235 (const :tag
"Globally (slow on startup in large files)" t
)))
237 (defcustom org-startup-align-all-tables nil
238 "Non-nil means, align all tables when visiting a file.
239 This is useful when the column width in tables is forced with <N> cookies
240 in table fields. Such tables will look correct only after the first re-align.
241 This can also be configured on a per-file basis by adding one of
242 the following lines anywhere in the buffer:
248 (defcustom org-insert-mode-line-in-empty-file nil
249 "Non-nil means insert the first line setting Org-mode in empty files.
250 When the function `org-mode' is called interactively in an empty file, this
251 normally means that the file name does not automatically trigger Org-mode.
252 To ensure that the file will always be in Org-mode in the future, a
253 line enforcing Org-mode will be inserted into the buffer, if this option
258 (defcustom org-replace-disputed-keys nil
259 "Non-nil means use alternative key bindings for some keys.
260 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
261 These keys are also used by other packages like `CUA-mode' or `windmove.el'.
262 If you want to use Org-mode together with one of these other modes,
263 or more generally if you would like to move some Org-mode commands to
264 other keys, set this variable and configure the keys with the variable
267 This option is only relevant at load-time of Org-mode, and must be set
268 *before* org.el is loaded. Changing it requires a restart of Emacs to
273 (if (fboundp 'defvaralias
)
274 (defvaralias 'org-CUA-compatible
'org-replace-disputed-keys
))
276 (defcustom org-disputed-keys
277 '(([(shift up
)] .
[(meta p
)])
278 ([(shift down
)] .
[(meta n
)])
279 ([(shift left
)] .
[(meta -
)])
280 ([(shift right
)] .
[(meta +)])
281 ([(control shift right
)] .
[(meta shift
+)])
282 ([(control shift left
)] .
[(meta shift -
)]))
283 "Keys for which Org-mode and other modes compete.
284 This is an alist, cars are the default keys, second element specifies
285 the alternative to use when `org-replace-disputed-keys' is t.
287 Keys can be specified in any syntax supported by `define-key'.
288 The value of this option takes effect only at Org-mode's startup,
289 therefore you'll have to restart Emacs to apply it after changing."
294 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
295 Or return the original if not disputed."
296 (if org-replace-disputed-keys
297 (let* ((nkey (key-description key
))
298 (x (org-find-if (lambda (x)
299 (equal (key-description (car x
)) nkey
))
304 (defun org-find-if (predicate seq
)
307 (if (funcall predicate
(car seq
))
308 (throw 'exit
(car seq
))
311 (defun org-defkey (keymap key def
)
312 "Define a key, possibly translated, as returned by `org-key'."
313 (define-key keymap
(org-key key
) def
))
315 (defcustom org-ellipsis nil
316 "The ellipsis to use in the Org-mode outline.
317 When nil, just use the standard three dots. When a string, use that instead,
318 When a face, use the standart 3 dots, but with the specified face.
319 The change affects only Org-mode (which will then use its own display table).
320 Changing this requires executing `M-x org-mode' in a buffer to become
323 :type
'(choice (const :tag
"Default" nil
)
324 (face :tag
"Face" :value org-warning
)
325 (string :tag
"String" :value
"...#")))
327 (defvar org-display-table nil
328 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
330 (defgroup org-keywords nil
331 "Keywords in Org-mode."
335 (defcustom org-deadline-string
"DEADLINE:"
336 "String to mark deadline entries.
337 A deadline is this string, followed by a time stamp. Should be a word,
338 terminated by a colon. You can insert a schedule keyword and
339 a timestamp with \\[org-deadline].
340 Changes become only effective after restarting Emacs."
344 (defcustom org-scheduled-string
"SCHEDULED:"
345 "String to mark scheduled TODO entries.
346 A schedule is this string, followed by a time stamp. Should be a word,
347 terminated by a colon. You can insert a schedule keyword and
348 a timestamp with \\[org-schedule].
349 Changes become only effective after restarting Emacs."
353 (defcustom org-closed-string
"CLOSED:"
354 "String used as the prefix for timestamps logging closing a TODO entry."
358 (defcustom org-clock-string
"CLOCK:"
359 "String used as prefix for timestamps clocking work hours on an item."
363 (defcustom org-comment-string
"COMMENT"
364 "Entries starting with this keyword will never be exported.
365 An entry can be toggled between COMMENT and normal with
366 \\[org-toggle-comment].
367 Changes become only effective after restarting Emacs."
371 (defcustom org-quote-string
"QUOTE"
372 "Entries starting with this keyword will be exported in fixed-width font.
373 Quoting applies only to the text in the entry following the headline, and does
374 not extend beyond the next headline, even if that is lower level.
375 An entry can be toggled between QUOTE and normal with
376 \\[org-toggle-fixed-width-section]."
380 (defconst org-repeat-re
381 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
382 "Regular expression for specifying repeated events.
383 After a match, group 1 contains the repeat expression.")
385 (defgroup org-structure nil
386 "Options concerning the general structure of Org-mode files."
390 (defgroup org-reveal-location nil
391 "Options about how to make context of a location visible."
392 :tag
"Org Reveal Location"
393 :group
'org-structure
)
395 (defconst org-context-choice
397 (const :tag
"Always" t
)
398 (const :tag
"Never" nil
)
399 (repeat :greedy t
:tag
"Individual contexts"
401 (choice :tag
"Context"
408 (const bookmark-jump
)
412 "Contexts for the reveal options.")
414 (defcustom org-show-hierarchy-above
'((default . t
))
415 "Non-nil means, show full hierarchy when revealing a location.
416 Org-mode often shows locations in an org-mode file which might have
417 been invisible before. When this is set, the hierarchy of headings
418 above the exposed location is shown.
419 Turning this off for example for sparse trees makes them very compact.
420 Instead of t, this can also be an alist specifying this option for different
421 contexts. Valid contexts are
422 agenda when exposing an entry from the agenda
423 org-goto when using the command `org-goto' on key C-c C-j
424 occur-tree when using the command `org-occur' on key C-c /
425 tags-tree when constructing a sparse tree based on tags matches
426 link-search when exposing search matches associated with a link
427 mark-goto when exposing the jump goal of a mark
428 bookmark-jump when exposing a bookmark location
429 isearch when exiting from an incremental search
430 default default for all contexts not set explicitly"
431 :group
'org-reveal-location
432 :type org-context-choice
)
434 (defcustom org-show-following-heading
'((default . nil
))
435 "Non-nil means, show following heading when revealing a location.
436 Org-mode often shows locations in an org-mode file which might have
437 been invisible before. When this is set, the heading following the
439 Turning this off for example for sparse trees makes them very compact,
440 but makes it harder to edit the location of the match. In such a case,
441 use the command \\[org-reveal] to show more context.
442 Instead of t, this can also be an alist specifying this option for different
443 contexts. See `org-show-hierarchy-above' for valid contexts."
444 :group
'org-reveal-location
445 :type org-context-choice
)
447 (defcustom org-show-siblings
'((default . nil
) (isearch t
))
448 "Non-nil means, show all sibling heading when revealing a location.
449 Org-mode often shows locations in an org-mode file which might have
450 been invisible before. When this is set, the sibling of the current entry
451 heading are all made visible. If `org-show-hierarchy-above' is t,
452 the same happens on each level of the hierarchy above the current entry.
454 By default this is on for the isearch context, off for all other contexts.
455 Turning this off for example for sparse trees makes them very compact,
456 but makes it harder to edit the location of the match. In such a case,
457 use the command \\[org-reveal] to show more context.
458 Instead of t, this can also be an alist specifying this option for different
459 contexts. See `org-show-hierarchy-above' for valid contexts."
460 :group
'org-reveal-location
461 :type org-context-choice
)
463 (defcustom org-show-entry-below
'((default . nil
))
464 "Non-nil means, show the entry below a headline when revealing a location.
465 Org-mode often shows locations in an org-mode file which might have
466 been invisible before. When this is set, the text below the headline that is
467 exposed is also shown.
469 By default this is off for all contexts.
470 Instead of t, this can also be an alist specifying this option for different
471 contexts. See `org-show-hierarchy-above' for valid contexts."
472 :group
'org-reveal-location
473 :type org-context-choice
)
475 (defcustom org-indirect-buffer-display
'other-window
476 "How should indirect tree buffers be displayed?
477 This applies to indirect buffers created with the commands
478 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
480 current-window Display in the current window
481 other-window Just display in another window.
482 dedicated-frame Create one new frame, and re-use it each time.
483 new-frame Make a new frame each time. Note that in this case
484 previously-made indirect buffers are kept, and you need to
485 kill these buffers yourself."
486 :group
'org-structure
487 :group
'org-agenda-windows
489 (const :tag
"In current window" current-window
)
490 (const :tag
"In current frame, other window" other-window
)
491 (const :tag
"Each time a new frame" new-frame
)
492 (const :tag
"One dedicated frame" dedicated-frame
)))
494 (defgroup org-cycle nil
495 "Options concerning visibility cycling in Org-mode."
497 :group
'org-structure
)
499 (defcustom org-drawers
'("PROPERTIES" "CLOCK")
500 "Names of drawers. Drawers are not opened by cycling on the headline above.
501 Drawers only open with a TAB on the drawer line itself. A drawer looks like
506 The drawer \"PROPERTIES\" is special for capturing properties through
509 Drawers can be defined on the per-file basis with a line like:
511 #+DRAWERS: HIDDEN STATE PROPERTIES"
512 :group
'org-structure
513 :type
'(repeat (string :tag
"Drawer Name")))
515 (defcustom org-cycle-global-at-bob nil
516 "Cycle globally if cursor is at beginning of buffer and not at a headline.
517 This makes it possible to do global cycling without having to use S-TAB or
518 C-u TAB. For this special case to work, the first line of the buffer
519 must not be a headline - it may be empty ot some other text. When used in
520 this way, `org-cycle-hook' is disables temporarily, to make sure the
521 cursor stays at the beginning of the buffer.
522 When this option is nil, don't do anything special at the beginning
527 (defcustom org-cycle-emulate-tab t
528 "Where should `org-cycle' emulate TAB.
530 white Only in completely white lines
531 whitestart Only at the beginning of lines, before the first non-white char
532 t Everywhere except in headlines
533 exc-hl-bol Everywhere except at the start of a headline
534 If TAB is used in a place where it does not emulate TAB, the current subtree
535 visibility is cycled."
537 :type
'(choice (const :tag
"Never" nil
)
538 (const :tag
"Only in completely white lines" white
)
539 (const :tag
"Before first char in a line" whitestart
)
540 (const :tag
"Everywhere except in headlines" t
)
541 (const :tag
"Everywhere except at bol in headlines" exc-hl-bol
)
544 (defcustom org-cycle-separator-lines
2
545 "Number of empty lines needed to keep an empty line between collapsed trees.
546 If you leave an empty line between the end of a subtree and the following
547 headline, this empty line is hidden when the subtree is folded.
548 Org-mode will leave (exactly) one empty line visible if the number of
549 empty lines is equal or larger to the number given in this variable.
550 So the default 2 means, at least 2 empty lines after the end of a subtree
551 are needed to produce free space between a collapsed subtree and the
554 Special case: when 0, never leave empty lines in collapsed view."
558 (defcustom org-cycle-hook
'(org-cycle-hide-archived-subtrees
559 org-cycle-hide-drawers
560 org-cycle-show-empty-lines
561 org-optimize-window-after-visibility-change
)
562 "Hook that is run after `org-cycle' has changed the buffer visibility.
563 The function(s) in this hook must accept a single argument which indicates
564 the new state that was set by the most recent `org-cycle' command. The
565 argument is a symbol. After a global state change, it can have the values
566 `overview', `content', or `all'. After a local state change, it can have
567 the values `folded', `children', or `subtree'."
571 (defgroup org-edit-structure nil
572 "Options concerning structure editing in Org-mode."
573 :tag
"Org Edit Structure"
574 :group
'org-structure
)
576 (defcustom org-odd-levels-only nil
577 "Non-nil means, skip even levels and only use odd levels for the outline.
578 This has the effect that two stars are being added/taken away in
579 promotion/demotion commands. It also influences how levels are
580 handled by the exporters.
581 Changing it requires restart of `font-lock-mode' to become effective
582 for fontification also in regions already fontified.
583 You may also set this on a per-file basis by adding one of the following
588 :group
'org-edit-structure
589 :group
'org-font-lock
592 (defcustom org-adapt-indentation t
593 "Non-nil means, adapt indentation when promoting and demoting.
594 When this is set and the *entire* text in an entry is indented, the
595 indentation is increased by one space in a demotion command, and
596 decreased by one in a promotion command. If any line in the entry
597 body starts at column 0, indentation is not changed at all."
598 :group
'org-edit-structure
601 (defcustom org-special-ctrl-a
/e nil
602 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
603 When t, `C-a' will bring back the cursor to the beginning of the
604 headline text, i.e. after the stars and after a possible TODO keyword.
605 In an item, this will be the position after the bullet.
606 When the cursor is already at that position, another `C-a' will bring
607 it to the beginning of the line.
608 `C-e' will jump to the end of the headline, ignoring the presence of tags
609 in the headline. A second `C-e' will then jump to the true end of the
610 line, after any tags.
611 When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
612 and only a directly following, identical keypress will bring the cursor
613 to the special positions."
614 :group
'org-edit-structure
616 (const :tag
"off" nil
)
617 (const :tag
"after bullet first" t
)
618 (const :tag
"border first" reversed
)))
620 (if (fboundp 'defvaralias
)
621 (defvaralias 'org-special-ctrl-a
'org-special-ctrl-a
/e
))
623 (defcustom org-special-ctrl-k nil
624 "Non-nil means `C-k' will behave specially in headlines.
625 When nil, `C-k' will call the default `kill-line' command.
626 When t, the following will happen while the cursor is in the headline:
628 - When the cursor is at the beginning of a headline, kill the entire
629 line and possible the folded subtree below the line.
630 - When in the middle of the headline text, kill the headline up to the tags.
631 - When after the headline text, kill the tags."
632 :group
'org-edit-structure
635 (defcustom org-M-RET-may-split-line
'((default . t
))
636 "Non-nil means, M-RET will split the line at the cursor position.
637 When nil, it will go to the end of the line before making a
639 You may also set this option in a different way for different
640 contexts. Valid contexts are:
642 headline when creating a new headline
643 item when creating a new item
644 table in a table field
645 default the value to be used for all contexts not explicitly
647 :group
'org-structure
650 (const :tag
"Always" t
)
651 (const :tag
"Never" nil
)
652 (repeat :greedy t
:tag
"Individual contexts"
654 (choice :tag
"Context"
662 (defcustom org-blank-before-new-entry
'((heading . nil
)
663 (plain-list-item . nil
))
664 "Should `org-insert-heading' leave a blank line before new heading/item?
665 The value is an alist, with `heading' and `plain-list-item' as car,
666 and a boolean flag as cdr."
667 :group
'org-edit-structure
669 (cons (const heading
) (boolean))
670 (cons (const plain-list-item
) (boolean))))
672 (defcustom org-insert-heading-hook nil
673 "Hook being run after inserting a new heading."
674 :group
'org-edit-structure
677 (defcustom org-enable-fixed-width-editor t
678 "Non-nil means, lines starting with \":\" are treated as fixed-width.
679 This currently only means, they are never auto-wrapped.
680 When nil, such lines will be treated like ordinary lines.
681 See also the QUOTE keyword."
682 :group
'org-edit-structure
685 (defcustom org-goto-auto-isearch t
686 "Non-nil means, typing characters in org-goto starts incremental search."
687 :group
'org-edit-structure
690 (defgroup org-sparse-trees nil
691 "Options concerning sparse trees in Org-mode."
692 :tag
"Org Sparse Trees"
693 :group
'org-structure
)
695 (defcustom org-highlight-sparse-tree-matches t
696 "Non-nil means, highlight all matches that define a sparse tree.
697 The highlights will automatically disappear the next time the buffer is
698 changed by an edit command."
699 :group
'org-sparse-trees
702 (defcustom org-remove-highlights-with-change t
703 "Non-nil means, any change to the buffer will remove temporary highlights.
704 Such highlights are created by `org-occur' and `org-clock-display'.
705 When nil, `C-c C-c needs to be used to get rid of the highlights.
706 The highlights created by `org-preview-latex-fragment' always need
707 `C-c C-c' to be removed."
708 :group
'org-sparse-trees
713 (defcustom org-occur-hook
'(org-first-headline-recenter)
714 "Hook that is run after `org-occur' has constructed a sparse tree.
715 This can be used to recenter the window to show as much of the structure
717 :group
'org-sparse-trees
720 (defgroup org-plain-lists nil
721 "Options concerning plain lists in Org-mode."
722 :tag
"Org Plain lists"
723 :group
'org-structure
)
725 (defcustom org-cycle-include-plain-lists nil
726 "Non-nil means, include plain lists into visibility cycling.
727 This means that during cycling, plain list items will *temporarily* be
728 interpreted as outline headlines with a level given by 1000+i where i is the
729 indentation of the bullet. In all other operations, plain list items are
730 not seen as headlines. For example, you cannot assign a TODO keyword to
732 :group
'org-plain-lists
735 (defcustom org-plain-list-ordered-item-terminator t
736 "The character that makes a line with leading number an ordered list item.
737 Valid values are ?. and ?\). To get both terminators, use t. While
738 ?. may look nicer, it creates the danger that a line with leading
739 number may be incorrectly interpreted as an item. ?\) therefore is
741 :group
'org-plain-lists
742 :type
'(choice (const :tag
"dot like in \"2.\"" ?.
)
743 (const :tag
"paren like in \"2)\"" ?\
))
744 (const :tab
"both" t
)))
746 (defcustom org-empty-line-terminates-plain-lists nil
747 "Non-nil means, an empty line ends all plain list levels.
748 When nil, empty lines are part of the preceeding item."
749 :group
'org-plain-lists
752 (defcustom org-auto-renumber-ordered-lists t
753 "Non-nil means, automatically renumber ordered plain lists.
754 Renumbering happens when the sequence have been changed with
755 \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
756 use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
757 :group
'org-plain-lists
760 (defcustom org-provide-checkbox-statistics t
761 "Non-nil means, update checkbox statistics after insert and toggle.
762 When this is set, checkbox statistics is updated each time you either insert
763 a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
764 with \\[org-ctrl-c-ctrl-c\\]."
765 :group
'org-plain-lists
768 (defcustom org-description-max-indent
20
769 "Maximum indentation for the second line of a description list.
770 When the indentation would be larger than this, it will become
771 5 characters instead."
772 :group
'org-plain-lists
775 (defgroup org-imenu-and-speedbar nil
776 "Options concerning imenu and speedbar in Org-mode."
777 :tag
"Org Imenu and Speedbar"
778 :group
'org-structure
)
780 (defcustom org-imenu-depth
2
781 "The maximum level for Imenu access to Org-mode headlines.
782 This also applied for speedbar access."
783 :group
'org-imenu-and-speedbar
786 (defgroup org-table nil
787 "Options concerning tables in Org-mode."
791 (defcustom org-enable-table-editor
'optimized
792 "Non-nil means, lines starting with \"|\" are handled by the table editor.
793 When nil, such lines will be treated like ordinary lines.
795 When equal to the symbol `optimized', the table editor will be optimized to
797 - Automatic overwrite mode in front of whitespace in table fields.
798 This makes the structure of the table stay in tact as long as the edited
799 field does not exceed the column width.
800 - Minimize the number of realigns. Normally, the table is aligned each time
801 TAB or RET are pressed to move to another field. With optimization this
802 happens only if changes to a field might have changed the column width.
803 Optimization requires replacing the functions `self-insert-command',
804 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
805 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
806 very good at guessing when a re-align will be necessary, but you can always
807 force one with \\[org-ctrl-c-ctrl-c].
809 If you would like to use the optimized version in Org-mode, but the
810 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
812 This variable can be used to turn on and off the table editor during a session,
813 but in order to toggle optimization, a restart is required.
815 See also the variable `org-table-auto-blank-field'."
818 (const :tag
"off" nil
)
820 (const :tag
"on, optimized" optimized
)))
822 (defcustom org-table-tab-recognizes-table.el t
823 "Non-nil means, TAB will automatically notice a table.el table.
824 When it sees such a table, it moves point into it and - if necessary -
825 calls `table-recognize-table'."
826 :group
'org-table-editing
829 (defgroup org-link nil
830 "Options concerning links in Org-mode."
834 (defvar org-link-abbrev-alist-local nil
835 "Buffer-local version of `org-link-abbrev-alist', which see.
836 The value of this is taken from the #+LINK lines.")
837 (make-variable-buffer-local 'org-link-abbrev-alist-local
)
839 (defcustom org-link-abbrev-alist nil
840 "Alist of link abbreviations.
841 The car of each element is a string, to be replaced at the start of a link.
842 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
843 links in Org-mode buffers can have an optional tag after a double colon, e.g.
845 [[linkkey:tag][description]]
847 If REPLACE is a string, the tag will simply be appended to create the link.
848 If the string contains \"%s\", the tag will be inserted there.
850 REPLACE may also be a function that will be called with the tag as the
851 only argument to create the link, which should be returned as a string.
853 See the manual for examples."
857 (defcustom org-descriptive-links t
858 "Non-nil means, hide link part and only show description of bracket links.
859 Bracket links are like [[link][descritpion]]. This variable sets the initial
860 state in new org-mode buffers. The setting can then be toggled on a
861 per-buffer basis from the Org->Hyperlinks menu."
865 (defcustom org-link-file-path-type
'adaptive
866 "How the path name in file links should be stored.
869 relative Relative to the current directory, i.e. the directory of the file
870 into which the link is being inserted.
871 absolute Absolute path, if possible with ~ for home directory.
872 noabbrev Absolute path, no abbreviation of home directory.
873 adaptive Use relative path for files in the current directory and sub-
874 directories of it. For other files, use an absolute path."
882 (defcustom org-activate-links
'(bracket angle plain radio tag date
)
883 "Types of links that should be activated in Org-mode files.
884 This is a list of symbols, each leading to the activation of a certain link
885 type. In principle, it does not hurt to turn on most link types - there may
886 be a small gain when turning off unused link types. The types are:
888 bracket The recommended [[link][description]] or [[link]] links with hiding.
889 angular Links in angular brackes that may contain whitespace like
890 <bbdb:Carsten Dominik>.
891 plain Plain links in normal text, no whitespace, like http://google.com.
892 radio Text that is matched by a radio target, see manual for details.
893 tag Tag settings in a headline (link to tag search).
894 date Time stamps (link to calendar).
896 Changing this variable requires a restart of Emacs to become effective."
898 :type
'(set (const :tag
"Double bracket links (new style)" bracket
)
899 (const :tag
"Angular bracket links (old style)" angular
)
900 (const :tag
"Plain text links" plain
)
901 (const :tag
"Radio target matches" radio
)
902 (const :tag
"Tags" tag
)
903 (const :tag
"Timestamps" date
)))
905 (defcustom org-make-link-description-function nil
906 "Function to use to generate link descriptions from links. If
907 nil the link location will be used. This function must take two
908 parameters; the first is the link and the second the description
909 org-insert-link has generated, and should return the description
914 (defgroup org-link-store nil
915 "Options concerning storing links in Org-mode."
916 :tag
"Org Store Link"
919 (defcustom org-email-link-description-format
"Email %c: %.30s"
920 "Format of the description part of a link to an email or usenet message.
921 The following %-excapes will be replaced by corresponding information:
923 %F full \"From\" field
924 %f name, taken from \"From\" field, address if no name
926 %t first name in \"To\" field, address if no name
927 %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
928 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
932 You may use normal field width specification between the % and the letter.
933 This is for example useful to limit the length of the subject.
935 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
936 :group
'org-link-store
939 (defcustom org-from-is-user-regexp
941 (when (and user-mail-address
(not (string= user-mail-address
"")))
942 (setq r1
(concat "\\<" (regexp-quote user-mail-address
) "\\>")))
943 (when (and user-full-name
(not (string= user-full-name
"")))
944 (setq r2
(concat "\\<" (regexp-quote user-full-name
) "\\>")))
945 (if (and r1 r2
) (concat r1
"\\|" r2
) (or r1 r2
)))
946 "Regexp mached against the \"From:\" header of an email or usenet message.
947 It should match if the message is from the user him/herself."
948 :group
'org-link-store
951 (defcustom org-context-in-file-links t
952 "Non-nil means, file links from `org-store-link' contain context.
953 A search string will be added to the file name with :: as separator and
954 used to find the context when the link is activated by the command
956 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
957 negates this setting for the duration of the command."
958 :group
'org-link-store
961 (defcustom org-keep-stored-link-after-insertion nil
962 "Non-nil means, keep link in list for entire session.
964 The command `org-store-link' adds a link pointing to the current
965 location to an internal list. These links accumulate during a session.
966 The command `org-insert-link' can be used to insert links into any
967 Org-mode file (offering completion for all stored links). When this
968 option is nil, every link which has been inserted once using \\[org-insert-link]
969 will be removed from the list, to make completing the unused links
971 :group
'org-link-store
974 (defgroup org-link-follow nil
975 "Options concerning following links in Org-mode."
976 :tag
"Org Follow Link"
979 (defcustom org-follow-link-hook nil
980 "Hook that is run after a link has been followed."
981 :group
'org-link-follow
984 (defcustom org-tab-follows-link nil
985 "Non-nil means, on links TAB will follow the link.
986 Needs to be set before org.el is loaded."
987 :group
'org-link-follow
990 (defcustom org-return-follows-link nil
991 "Non-nil means, on links RET will follow the link.
992 Needs to be set before org.el is loaded."
993 :group
'org-link-follow
996 (defcustom org-mouse-1-follows-link
997 (if (boundp 'mouse-1-click-follows-link
) mouse-1-click-follows-link t
)
998 "Non-nil means, mouse-1 on a link will follow the link.
999 A longer mouse click will still set point. Does not work on XEmacs.
1000 Needs to be set before org.el is loaded."
1001 :group
'org-link-follow
1004 (defcustom org-mark-ring-length
4
1005 "Number of different positions to be recorded in the ring
1006 Changing this requires a restart of Emacs to work correctly."
1007 :group
'org-link-follow
1010 (defcustom org-link-frame-setup
1011 '((vm . vm-visit-folder-other-frame
)
1012 (gnus . gnus-other-frame
)
1013 (file . find-file-other-window
))
1014 "Setup the frame configuration for following links.
1015 When following a link with Emacs, it may often be useful to display
1016 this link in another window or frame. This variable can be used to
1017 set this up for the different types of links.
1020 `vm-visit-folder-other-frame'
1021 For Gnus, use any of
1024 For FILE, use any of
1026 `find-file-other-window'
1027 `find-file-other-frame'
1028 For the calendar, use the variable `calendar-setup'.
1029 For BBDB, it is currently only possible to display the matches in
1031 :group
'org-link-follow
1035 (const vm-visit-folder
)
1036 (const vm-visit-folder-other-window
)
1037 (const vm-visit-folder-other-frame
)))
1041 (const gnus-other-frame
)))
1045 (const find-file-other-window
)
1046 (const find-file-other-frame
)))))
1048 (defcustom org-display-internal-link-with-indirect-buffer nil
1049 "Non-nil means, use indirect buffer to display infile links.
1050 Activating internal links (from one location in a file to another location
1051 in the same file) normally just jumps to the location. When the link is
1052 activated with a C-u prefix (or with mouse-3), the link is displayed in
1053 another window. When this option is set, the other window actually displays
1054 an indirect buffer clone of the current buffer, to avoid any visibility
1055 changes to the current buffer."
1056 :group
'org-link-follow
1059 (defcustom org-open-non-existing-files nil
1060 "Non-nil means, `org-open-file' will open non-existing files.
1061 When nil, an error will be generated."
1062 :group
'org-link-follow
1065 (defcustom org-link-mailto-program
'(browse-url "mailto:%a?subject=%s")
1066 "Function and arguments to call for following mailto links.
1067 This is a list with the first element being a lisp function, and the
1068 remaining elements being arguments to the function. In string arguments,
1069 %a will be replaced by the address, and %s will be replaced by the subject
1070 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1071 :group
'org-link-follow
1073 (const :tag
"browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1074 (const :tag
"compose-mail" (compose-mail "%a" "%s"))
1075 (const :tag
"message-mail" (message-mail "%a" "%s"))
1076 (cons :tag
"other" (function) (repeat :tag
"argument" sexp
))))
1078 (defcustom org-confirm-shell-link-function
'yes-or-no-p
1079 "Non-nil means, ask for confirmation before executing shell links.
1080 Shell links can be dangerous: just think about a link
1082 [[shell:rm -rf ~/*][Google Search]]
1084 This link would show up in your Org-mode document as \"Google Search\",
1085 but really it would remove your entire home directory.
1086 Therefore we advise against setting this variable to nil.
1087 Just change it to `y-or-n-p' of you want to confirm with a
1088 single keystroke rather than having to type \"yes\"."
1089 :group
'org-link-follow
1091 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1092 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1093 (const :tag
"no confirmation (dangerous)" nil
)))
1095 (defcustom org-confirm-elisp-link-function
'yes-or-no-p
1096 "Non-nil means, ask for confirmation before executing Emacs Lisp links.
1097 Elisp links can be dangerous: just think about a link
1099 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1101 This link would show up in your Org-mode document as \"Google Search\",
1102 but really it would remove your entire home directory.
1103 Therefore we advise against setting this variable to nil.
1104 Just change it to `y-or-n-p' of you want to confirm with a
1105 single keystroke rather than having to type \"yes\"."
1106 :group
'org-link-follow
1108 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1109 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1110 (const :tag
"no confirmation (dangerous)" nil
)))
1112 (defconst org-file-apps-defaults-gnu
1115 "Default file applications on a UNIX or GNU/Linux system.
1116 See `org-file-apps'.")
1118 (defconst org-file-apps-defaults-macosx
1124 ("eps.gz" .
"gv %s")
1126 ("fig" .
"xfig %s"))
1127 "Default file applications on a MacOS X system.
1128 The system \"open\" is known as a default, but we use X11 applications
1129 for some files for which the OS does not have a good default.
1130 See `org-file-apps'.")
1132 (defconst org-file-apps-defaults-windowsnt
1136 (list (if (featurep 'xemacs
)
1137 'mswindows-shell-execute
1140 "Default file applications on a Windows NT system.
1141 The system \"open\" is used for most files.
1142 See `org-file-apps'.")
1144 (defcustom org-file-apps
1153 "External applications for opening `file:path' items in a document.
1154 Org-mode uses system defaults for different file types, but
1155 you can use this variable to set the application for a given file
1156 extension. The entries in this list are cons cells where the car identifies
1157 files and the cdr the corresponding command. Possible values for the
1159 \"ext\" A string identifying an extension
1160 `directory' Matches a directory
1161 `remote' Matches a remote file, accessible through tramp or efs.
1162 Remote files most likely should be visited through Emacs
1163 because external applications cannot handle such paths.
1164 t Default for all remaining files
1166 Possible values for the command are:
1167 `emacs' The file will be visited by the current Emacs process.
1168 `default' Use the default application for this file type.
1169 string A command to be executed by a shell; %s will be replaced
1170 by the path to the file.
1171 sexp A Lisp form which will be evaluated. The file path will
1172 be available in the Lisp variable `file'.
1173 For more examples, see the system specific constants
1174 `org-file-apps-defaults-macosx'
1175 `org-file-apps-defaults-windowsnt'
1176 `org-file-apps-defaults-gnu'."
1177 :group
'org-link-follow
1179 (cons (choice :value
""
1180 (string :tag
"Extension")
1181 (const :tag
"Default for unrecognized files" t
)
1182 (const :tag
"Remote file" remote
)
1183 (const :tag
"Links to a directory" directory
))
1185 (const :tag
"Visit with Emacs" emacs
)
1186 (const :tag
"Use system default" default
)
1187 (string :tag
"Command")
1188 (sexp :tag
"Lisp form")))))
1190 (defgroup org-refile nil
1191 "Options concerning refiling entries in Org-mode."
1195 (defcustom org-directory
"~/org"
1196 "Directory with org files.
1197 This directory will be used as default to prompt for org files.
1198 Used by the hooks for remember.el."
1200 :group
'org-remember
1203 (defcustom org-default-notes-file
"~/.notes"
1204 "Default target for storing notes.
1205 Used by the hooks for remember.el. This can be a string, or nil to mean
1206 the value of `remember-data-file'.
1207 You can set this on a per-template basis with the variable
1208 `org-remember-templates'."
1210 :group
'org-remember
1212 (const :tag
"Default from remember-data-file" nil
)
1215 (defcustom org-goto-interface
'outline
1216 "The default interface to be used for `org-goto'.
1218 outline The interface shows an outline of the relevant file
1219 and the correct heading is found by moving through
1220 the outline or by searching with incremental search.
1221 outline-path-completion Headlines in the current buffer are offered via
1225 (const :tag
"Outline" outline
)
1226 (const :tag
"Outline-path-completion" outline-path-completion
)))
1228 (defcustom org-reverse-note-order nil
1229 "Non-nil means, store new notes at the beginning of a file or entry.
1230 When nil, new notes will be filed to the end of a file or entry.
1231 This can also be a list with cons cells of regular expressions that
1232 are matched against file names, and values."
1233 :group
'org-remember
1235 (const :tag
"Reverse always" t
)
1236 (const :tag
"Reverse never" nil
)
1237 (repeat :tag
"By file name regexp"
1238 (cons regexp boolean
))))
1240 (defcustom org-refile-targets nil
1241 "Targets for refiling entries with \\[org-refile].
1242 This is list of cons cells. Each cell contains:
1243 - a specification of the files to be considered, either a list of files,
1244 or a symbol whose function or variable value will be used to retrieve
1245 a file name or a list of file names. Nil means, refile to a different
1246 heading in the current buffer.
1247 - A specification of how to find candidate refile targets. This may be
1249 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
1250 This tag has to be present in all target headlines, inheritance will
1252 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
1254 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
1255 headlines that are refiling targets.
1256 - a cons cell (:level . N). Any headline of level N is considered a target.
1257 - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
1258 :group
'org-remember
1261 (choice :value org-agenda-files
1262 (const :tag
"All agenda files" org-agenda-files
)
1263 (const :tag
"Current buffer" nil
)
1264 (function) (variable) (file))
1265 (choice :tag
"Identify target headline by"
1266 (cons :tag
"Specific tag" (const :tag
) (string))
1267 (cons :tag
"TODO keyword" (const :todo
) (string))
1268 (cons :tag
"Regular expression" (const :regexp
) (regexp))
1269 (cons :tag
"Level number" (const :level
) (integer))
1270 (cons :tag
"Max Level number" (const :maxlevel
) (integer))))))
1272 (defcustom org-refile-use-outline-path nil
1273 "Non-nil means, provide refile targets as paths.
1274 So a level 3 headline will be available as level1/level2/level3.
1275 When the value is `file', also include the file name (without directory)
1276 into the path. When `full-file-path', include the full file path."
1277 :group
'org-remember
1279 (const :tag
"Not" nil
)
1280 (const :tag
"Yes" t
)
1281 (const :tag
"Start with file name" file
)
1282 (const :tag
"Start with full file path" full-file-path
)))
1284 (defgroup org-todo nil
1285 "Options concerning TODO items in Org-mode."
1289 (defgroup org-progress nil
1290 "Options concerning Progress logging in Org-mode."
1294 (defcustom org-todo-keywords
'((sequence "TODO" "DONE"))
1295 "List of TODO entry keyword sequences and their interpretation.
1296 \\<org-mode-map>This is a list of sequences.
1298 Each sequence starts with a symbol, either `sequence' or `type',
1299 indicating if the keywords should be interpreted as a sequence of
1300 action steps, or as different types of TODO items. The first
1301 keywords are states requiring action - these states will select a headline
1302 for inclusion into the global TODO list Org-mode produces. If one of
1303 the \"keywords\" is the vertical bat \"|\" the remaining keywords
1304 signify that no further action is necessary. If \"|\" is not found,
1305 the last keyword is treated as the only DONE state of the sequence.
1307 The command \\[org-todo] cycles an entry through these states, and one
1308 additional state where no keyword is present. For details about this
1309 cycling, see the manual.
1311 TODO keywords and interpretation can also be set on a per-file basis with
1312 the special #+SEQ_TODO and #+TYP_TODO lines.
1314 Each keyword can optionally specify a character for fast state selection
1315 \(in combination with the variable `org-use-fast-todo-selection')
1316 and specifiers for state change logging, using the same syntax
1317 that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
1318 that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
1319 indicates to record a time stamp each time this state is selected.
1321 Each keyword may also specify if a timestamp or a note should be
1322 recorded when entering or leaving the state, by adding additional
1323 characters in the parenthesis after the keyword. This looks like this:
1324 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
1325 record only the time of the state change. With X and Y being either
1326 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
1327 Y when leaving the state if and only if the *target* state does not
1328 define X. You may omit any of the fast-selection key or X or /Y,
1329 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
1331 For backward compatibility, this variable may also be just a list
1332 of keywords - in this case the interptetation (sequence or type) will be
1333 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
1335 :group
'org-keywords
1337 (repeat :tag
"Old syntax, just keywords"
1338 (string :tag
"Keyword"))
1339 (repeat :tag
"New syntax"
1342 :tag
"Interpretation"
1343 (const :tag
"Sequence (cycling hits every state)" sequence
)
1344 (const :tag
"Type (cycling directly to DONE)" type
))
1346 (string :tag
"Keyword"))))))
1348 (defvar org-todo-keywords-1 nil
1349 "All TODO and DONE keywords active in a buffer.")
1350 (make-variable-buffer-local 'org-todo-keywords-1
)
1351 (defvar org-todo-keywords-for-agenda nil
)
1352 (defvar org-done-keywords-for-agenda nil
)
1353 (defvar org-agenda-contributing-files nil
)
1354 (defvar org-not-done-keywords nil
)
1355 (make-variable-buffer-local 'org-not-done-keywords
)
1356 (defvar org-done-keywords nil
)
1357 (make-variable-buffer-local 'org-done-keywords
)
1358 (defvar org-todo-heads nil
)
1359 (make-variable-buffer-local 'org-todo-heads
)
1360 (defvar org-todo-sets nil
)
1361 (make-variable-buffer-local 'org-todo-sets
)
1362 (defvar org-todo-log-states nil
)
1363 (make-variable-buffer-local 'org-todo-log-states
)
1364 (defvar org-todo-kwd-alist nil
)
1365 (make-variable-buffer-local 'org-todo-kwd-alist
)
1366 (defvar org-todo-key-alist nil
)
1367 (make-variable-buffer-local 'org-todo-key-alist
)
1368 (defvar org-todo-key-trigger nil
)
1369 (make-variable-buffer-local 'org-todo-key-trigger
)
1371 (defcustom org-todo-interpretation
'sequence
1372 "Controls how TODO keywords are interpreted.
1373 This variable is in principle obsolete and is only used for
1374 backward compatibility, if the interpretation of todo keywords is
1375 not given already in `org-todo-keywords'. See that variable for
1378 :group
'org-keywords
1379 :type
'(choice (const sequence
)
1382 (defcustom org-use-fast-todo-selection
'prefix
1383 "Non-nil means, use the fast todo selection scheme with C-c C-t.
1384 This variable describes if and under what circumstances the cycling
1385 mechanism for TODO keywords will be replaced by a single-key, direct
1388 When nil, fast selection is never used.
1390 When the symbol `prefix', it will be used when `org-todo' is called with
1391 a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
1392 in an agenda buffer.
1394 When t, fast selection is used by default. In this case, the prefix
1395 argument forces cycling instead.
1397 In all cases, the special interface is only used if access keys have actually
1398 been assigned by the user, i.e. if keywords in the configuration are followed
1399 by a letter in parenthesis, like TODO(t)."
1402 (const :tag
"Never" nil
)
1403 (const :tag
"By default" t
)
1404 (const :tag
"Only with C-u C-c C-t" prefix
)))
1406 (defcustom org-provide-todo-statistics t
1407 "Non-nil means, update todo statistics after insert and toggle.
1408 When this is set, todo statistics is updated in the parent of the current
1409 entry each time a todo state is changed."
1413 (defcustom org-after-todo-state-change-hook nil
1414 "Hook which is run after the state of a TODO item was changed.
1415 The new state (a string with a TODO keyword, or nil) is available in the
1416 Lisp variable `state'."
1420 (defcustom org-log-done nil
1421 "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
1422 When equal to the list (done), also prompt for a closing note.
1423 This can also be configured on a per-file basis by adding one of
1424 the following lines anywhere in the buffer:
1427 #+STARTUP: lognotedone
1428 #+STARTUP: nologdone"
1430 :group
'org-progress
1432 (const :tag
"No logging" nil
)
1433 (const :tag
"Record CLOSED timestamp" time
)
1434 (const :tag
"Record CLOSED timestamp with closing note." note
)))
1436 ;; Normalize old uses of org-log-done.
1438 ((eq org-log-done t
) (setq org-log-done
'time
))
1439 ((and (listp org-log-done
) (memq 'done org-log-done
))
1440 (setq org-log-done
'note
)))
1442 (defcustom org-log-note-clock-out nil
1443 "Non-nil means, recored a note when clocking out of an item.
1444 This can also be configured on a per-file basis by adding one of
1445 the following lines anywhere in the buffer:
1447 #+STARTUP: lognoteclock-out
1448 #+STARTUP: nolognoteclock-out"
1450 :group
'org-progress
1453 (defcustom org-log-done-with-time t
1454 "Non-nil means, the CLOSED time stamp will contain date and time.
1455 When nil, only the date will be recorded."
1456 :group
'org-progress
1459 (defcustom org-log-note-headings
1460 '((done .
"CLOSING NOTE %t")
1461 (state .
"State %-12s %t")
1462 (note .
"Note taken on %t")
1464 "Headings for notes added to entries.
1465 The value is an alist, with the car being a symbol indicating the note
1466 context, and the cdr is the heading to be used. The heading may also be the
1468 %t in the heading will be replaced by a time stamp.
1469 %s will be replaced by the new TODO state, in double quotes.
1470 %u will be replaced by the user name.
1471 %U will be replaced by the full user name."
1473 :group
'org-progress
1474 :type
'(list :greedy t
1475 (cons (const :tag
"Heading when closing an item" done
) string
)
1477 "Heading when changing todo state (todo sequence only)"
1479 (cons (const :tag
"Heading when just taking a note" note
) string
)
1480 (cons (const :tag
"Heading when clocking out" clock-out
) string
)))
1482 (unless (assq 'note org-log-note-headings
)
1483 (push '(note .
"%t") org-log-note-headings
))
1485 (defcustom org-log-states-order-reversed t
1486 "Non-nil means, the latest state change note will be directly after heading.
1487 When nil, the notes will be orderer according to time."
1489 :group
'org-progress
1492 (defcustom org-log-repeat
'time
1493 "Non-nil means, record moving through the DONE state when triggering repeat.
1494 An auto-repeating tasks is immediately switched back to TODO when marked
1495 done. If you are not logging state changes (by adding \"@\" or \"!\" to
1496 the TODO keyword definition, or recording a closing note by setting
1497 `org-log-done', there will be no record of the task moving through DONE.
1498 This variable forces taking a note anyway. Possible values are:
1500 nil Don't force a record
1501 time Record a time stamp
1504 This option can also be set with on a per-file-basis with
1506 #+STARTUP: logrepeat
1507 #+STARTUP: lognoterepeat
1508 #+STARTUP: nologrepeat
1510 You can have local logging settings for a subtree by setting the LOGGING
1511 property to one or more of these keywords."
1513 :group
'org-progress
1515 (const :tag
"Don't force a record" nil
)
1516 (const :tag
"Force recording the DONE state" time
)
1517 (const :tag
"Force recording a note with the DONE state" note
)))
1520 (defgroup org-priorities nil
1521 "Priorities in Org-mode."
1522 :tag
"Org Priorities"
1525 (defcustom org-highest-priority ?A
1526 "The highest priority of TODO items. A character like ?A, ?B etc.
1527 Must have a smaller ASCII number than `org-lowest-priority'."
1528 :group
'org-priorities
1531 (defcustom org-lowest-priority ?C
1532 "The lowest priority of TODO items. A character like ?A, ?B etc.
1533 Must have a larger ASCII number than `org-highest-priority'."
1534 :group
'org-priorities
1537 (defcustom org-default-priority ?B
1538 "The default priority of TODO items.
1539 This is the priority an item get if no explicit priority is given."
1540 :group
'org-priorities
1543 (defcustom org-priority-start-cycle-with-default t
1544 "Non-nil means, start with default priority when starting to cycle.
1545 When this is nil, the first step in the cycle will be (depending on the
1546 command used) one higher or lower that the default priority."
1547 :group
'org-priorities
1550 (defgroup org-time nil
1551 "Options concerning time stamps and deadlines in Org-mode."
1555 (defcustom org-insert-labeled-timestamps-at-point nil
1556 "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
1557 When nil, these labeled time stamps are forces into the second line of an
1558 entry, just after the headline. When scheduling from the global TODO list,
1559 the time stamp will always be forced into the second line."
1563 (defconst org-time-stamp-formats
'("<%Y-%m-%d %a>" .
"<%Y-%m-%d %a %H:%M>")
1564 "Formats for `format-time-string' which are used for time stamps.
1565 It is not recommended to change this constant.")
1567 (defcustom org-time-stamp-rounding-minutes
'(0 5)
1568 "Number of minutes to round time stamps to.
1569 These are two values, the first applies when first creating a time stamp.
1570 The second applies when changing it with the commands `S-up' and `S-down'.
1571 When changing the time stamp, this means that it will change in steps
1572 of N minutes, as given by the second value.
1574 When a setting is 0 or 1, insert the time unmodified. Useful rounding
1575 numbers should be factors of 60, so for example 5, 10, 15.
1577 When this is larger than 1, you can still force an exact time-stamp by using
1578 a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
1579 and by using a prefix arg to `S-up/down' to specify the exact number
1580 of minutes to shift."
1582 :get
'(lambda (var) ; Make sure all entries have 5 elements
1583 (if (integerp (default-value var
))
1584 (list (default-value var
) 5)
1585 (default-value var
)))
1587 (integer :tag
"when inserting times")
1588 (integer :tag
"when modifying times")))
1590 ;; Normalize old customizations of this variable.
1591 (when (integerp org-time-stamp-rounding-minutes
)
1592 (setq org-time-stamp-rounding-minutes
1593 (list org-time-stamp-rounding-minutes
1594 org-time-stamp-rounding-minutes
)))
1596 (defcustom org-display-custom-times nil
1597 "Non-nil means, overlay custom formats over all time stamps.
1598 The formats are defined through the variable `org-time-stamp-custom-formats'.
1599 To turn this on on a per-file basis, insert anywhere in the file:
1600 #+STARTUP: customtime"
1604 (make-variable-buffer-local 'org-display-custom-times
)
1606 (defcustom org-time-stamp-custom-formats
1607 '("<%m/%d/%y %a>" .
"<%m/%d/%y %a %H:%M>") ; american
1608 "Custom formats for time stamps. See `format-time-string' for the syntax.
1609 These are overlayed over the default ISO format if the variable
1610 `org-display-custom-times' is set. Time like %H:%M should be at the
1611 end of the second format."
1615 (defun org-time-stamp-format (&optional long inactive
)
1616 "Get the right format for a time string."
1617 (let ((f (if long
(cdr org-time-stamp-formats
)
1618 (car org-time-stamp-formats
))))
1620 (concat "[" (substring f
1 -
1) "]")
1623 (defcustom org-time-clocksum-format
"%d:%02d"
1624 "The format string used when creating CLOCKSUM lines, or when
1625 org-mode generates a time duration."
1629 (defcustom org-deadline-warning-days
14
1630 "No. of days before expiration during which a deadline becomes active.
1631 This variable governs the display in sparse trees and in the agenda.
1632 When 0 or negative, it means use this number (the absolute value of it)
1633 even if a deadline has a different individual lead time specified."
1635 :group
'org-agenda-daily
/weekly
1638 (defcustom org-read-date-prefer-future t
1639 "Non-nil means, assume future for incomplete date input from user.
1640 This affects the following situations:
1641 1. The user gives a day, but no month.
1642 For example, if today is the 15th, and you enter \"3\", Org-mode will
1643 read this as the third of *next* month. However, if you enter \"17\",
1644 it will be considered as *this* month.
1645 2. The user gives a month but not a year.
1646 For example, if it is april and you enter \"feb 2\", this will be read
1647 as feb 2, *next* year. \"May 5\", however, will be this year.
1649 Currently this does not work for ISO week specifications.
1651 When this option is nil, the current month and year will always be used
1656 (defcustom org-read-date-display-live t
1657 "Non-nil means, display current interpretation of date prompt live.
1658 This display will be in an overlay, in the minibuffer."
1662 (defcustom org-read-date-popup-calendar t
1663 "Non-nil means, pop up a calendar when prompting for a date.
1664 In the calendar, the date can be selected with mouse-1. However, the
1665 minibuffer will also be active, and you can simply enter the date as well.
1666 When nil, only the minibuffer will be available."
1669 (if (fboundp 'defvaralias
)
1670 (defvaralias 'org-popup-calendar-for-date-prompt
1671 'org-read-date-popup-calendar
))
1673 (defcustom org-extend-today-until
0
1674 "The hour when your day really ends.
1675 This has influence for the following applications:
1676 - When switching the agenda to \"today\". It it is still earlier than
1677 the time given here, the day recognized as TODAY is actually yesterday.
1678 - When a date is read from the user and it is still before the time given
1679 here, the current date and time will be assumed to be yesterday, 23:59.
1682 IMPORTANT: This is still a very experimental feature, it may disappear
1683 again or it may be extended to mean more things."
1687 (defcustom org-edit-timestamp-down-means-later nil
1688 "Non-nil means, S-down will increase the time in a time stamp.
1689 When nil, S-up will increase."
1693 (defcustom org-calendar-follow-timestamp-change t
1694 "Non-nil means, make the calendar window follow timestamp changes.
1695 When a timestamp is modified and the calendar window is visible, it will be
1696 moved to the new date."
1700 (defgroup org-tags nil
1701 "Options concerning tags in Org-mode."
1705 (defcustom org-tag-alist nil
1706 "List of tags allowed in Org-mode files.
1707 When this list is nil, Org-mode will base TAG input on what is already in the
1709 The value of this variable is an alist, the car of each entry must be a
1710 keyword as a string, the cdr may be a character that is used to select
1711 that tag through the fast-tag-selection interface.
1712 See the manual for details."
1716 (cons (string :tag
"Tag name")
1717 (character :tag
"Access char"))
1718 (const :tag
"Start radio group" (:startgroup
))
1719 (const :tag
"End radio group" (:endgroup
)))))
1721 (defvar org-file-tags nil
1722 "List of tags that can be inherited by all entries in the file.
1723 The tags will be inherited if the variable `org-use-tag-inheritance'
1724 says they should be.
1725 This variable is populated from #+TAG lines.")
1727 (defcustom org-use-fast-tag-selection
'auto
1728 "Non-nil means, use fast tag selection scheme.
1729 This is a special interface to select and deselect tags with single keys.
1730 When nil, fast selection is never used.
1731 When the symbol `auto', fast selection is used if and only if selection
1732 characters for tags have been configured, either through the variable
1733 `org-tag-alist' or through a #+TAGS line in the buffer.
1734 When t, fast selection is always used and selection keys are assigned
1735 automatically if necessary."
1738 (const :tag
"Always" t
)
1739 (const :tag
"Never" nil
)
1740 (const :tag
"When selection characters are configured" 'auto
)))
1742 (defcustom org-fast-tag-selection-single-key nil
1743 "Non-nil means, fast tag selection exits after first change.
1744 When nil, you have to press RET to exit it.
1745 During fast tag selection, you can toggle this flag with `C-c'.
1746 This variable can also have the value `expert'. In this case, the window
1747 displaying the tags menu is not even shown, until you press C-c again."
1750 (const :tag
"No" nil
)
1751 (const :tag
"Yes" t
)
1752 (const :tag
"Expert" expert
)))
1754 (defvar org-fast-tag-selection-include-todo nil
1755 "Non-nil means, fast tags selection interface will also offer TODO states.
1756 This is an undocumented feature, you should not rely on it.")
1758 (defcustom org-tags-column
(if (featurep 'xemacs
) -
79 -
80)
1759 "The column to which tags should be indented in a headline.
1760 If this number is positive, it specifies the column. If it is negative,
1761 it means that the tags should be flushright to that column. For example,
1762 -80 works well for a normal 80 character screen."
1766 (defcustom org-auto-align-tags t
1767 "Non-nil means, realign tags after pro/demotion of TODO state change.
1768 These operations change the length of a headline and therefore shift
1769 the tags around. With this options turned on, after each such operation
1770 the tags are again aligned to `org-tags-column'."
1774 (defcustom org-use-tag-inheritance t
1775 "Non-nil means, tags in levels apply also for sublevels.
1776 When nil, only the tags directly given in a specific line apply there.
1777 If this option is t, a match early-on in a tree can lead to a large
1778 number of matches in the subtree. If you only want to see the first
1779 match in a tree during a search, check out the variable
1780 `org-tags-match-list-sublevels'.
1782 This may also be a list of tags that should be inherited, or a regexp that
1783 matches tags that should be inherited."
1786 (const :tag
"Not" nil
)
1787 (const :tag
"Always" t
)
1788 (repeat :tag
"Specific tags" (string :tag
"Tag"))
1789 (regexp :tag
"Tags matched by regexp")))
1791 (defun org-tag-inherit-p (tag)
1792 "Check if TAG is one that should be inherited."
1794 ((eq org-use-tag-inheritance t
) t
)
1795 ((not org-use-tag-inheritance
) nil
)
1796 ((stringp org-use-tag-inheritance
)
1797 (string-match org-use-tag-inheritance tag
))
1798 ((listp org-use-tag-inheritance
)
1799 (member tag org-use-tag-inheritance
))
1800 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
1802 (defcustom org-tags-match-list-sublevels t
1803 "Non-nil means list also sublevels of headlines matching tag search.
1804 Because of tag inheritance (see variable `org-use-tag-inheritance'),
1805 the sublevels of a headline matching a tag search often also match
1806 the same search. Listing all of them can create very long lists.
1807 Setting this variable to nil causes subtrees of a match to be skipped.
1808 This option is off by default, because inheritance in on. If you turn
1809 inheritance off, you very likely want to turn this option on.
1811 As a special case, if the tag search is restricted to TODO items, the
1812 value of this variable is ignored and sublevels are always checked, to
1813 make sure all corresponding TODO items find their way into the list."
1817 (defvar org-tags-history nil
1818 "History of minibuffer reads for tags.")
1819 (defvar org-last-tags-completion-table nil
1820 "The last used completion table for tags.")
1821 (defvar org-after-tags-change-hook nil
1822 "Hook that is run after the tags in a line have changed.")
1824 (defgroup org-properties nil
1825 "Options concerning properties in Org-mode."
1826 :tag
"Org Properties"
1829 (defcustom org-property-format
"%-10s %s"
1830 "How property key/value pairs should be formatted by `indent-line'.
1831 When `indent-line' hits a property definition, it will format the line
1832 according to this format, mainly to make sure that the values are
1833 lined-up with respect to each other."
1834 :group
'org-properties
1837 (defcustom org-use-property-inheritance nil
1838 "Non-nil means, properties apply also for sublevels.
1840 This setting is chiefly used during property searches. Turning it on can
1841 cause significant overhead when doing a search, which is why it is not
1844 When nil, only the properties directly given in the current entry count.
1845 When t, every property is inherited. The value may also be a list of
1846 properties that should have inheritance, or a regular expression matching
1847 properties that should be inherited.
1849 However, note that some special properties use inheritance under special
1850 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
1851 and the properties ending in \"_ALL\" when they are used as descriptor
1852 for valid values of a property.
1854 Note for programmers:
1855 When querying an entry with `org-entry-get', you can control if inheritance
1856 should be used. By default, `org-entry-get' looks only at the local
1857 properties. You can request inheritance by setting the inherit argument
1858 to t (to force inheritance) or to `selective' (to respect the setting
1860 :group
'org-properties
1862 (const :tag
"Not" nil
)
1863 (const :tag
"Always" t
)
1864 (repeat :tag
"Specific properties" (string :tag
"Property"))
1865 (regexp :tag
"Properties matched by regexp")))
1867 (defun org-property-inherit-p (property)
1868 "Check if PROPERTY is one that should be inherited."
1870 ((eq org-use-property-inheritance t
) t
)
1871 ((not org-use-property-inheritance
) nil
)
1872 ((stringp org-use-property-inheritance
)
1873 (string-match org-use-property-inheritance property
))
1874 ((listp org-use-property-inheritance
)
1875 (member property org-use-property-inheritance
))
1876 (t (error "Invalid setting of `org-use-property-inheritance'"))))
1878 (defcustom org-columns-default-format
"%25ITEM %TODO %3PRIORITY %TAGS"
1879 "The default column format, if no other format has been defined.
1880 This variable can be set on the per-file basis by inserting a line
1882 #+COLUMNS: %25ITEM ....."
1883 :group
'org-properties
1886 (defcustom org-columns-ellipses
".."
1887 "The ellipses to be used when a field in column view is truncated.
1888 When this is the empty string, as many characters as possible are shown,
1889 but then there will be no visual indication that the field has been truncated.
1890 When this is a string of length N, the last N characters of a truncated
1891 field are replaced by this string. If the column is narrower than the
1892 ellipses string, only part of the ellipses string will be shown."
1893 :group
'org-properties
1897 (defcustom org-effort-property
"Effort"
1898 "The property that is being used to keep track of effort estimates.
1899 Effort estimates given in this property need to have the format H:MM."
1900 :group
'org-properties
1901 :group
'org-progress
1902 :type
'(string :tag
"Property"))
1904 (defconst org-global-properties-fixed
1905 '(("VISIBILITY_ALL" .
"folded children content all"))
1906 "List of property/value pairs that can be inherited by any entry.
1907 These are fixed values, for the preset properties.")
1910 (defcustom org-global-properties nil
1911 "List of property/value pairs that can be inherited by any entry.
1912 You can set buffer-local values for this by adding lines like
1914 #+PROPERTY: NAME VALUE"
1915 :group
'org-properties
1917 (cons (string :tag
"Property")
1918 (string :tag
"Value"))))
1920 (defvar org-file-properties nil
1921 "List of property/value pairs that can be inherited by any entry.
1922 Valid for the current buffer.
1923 This variable is populated from #+PROPERTY lines.")
1924 (make-variable-buffer-local 'org-file-properties
)
1926 (defgroup org-agenda nil
1927 "Options concerning agenda views in Org-mode."
1931 (defvar org-category nil
1932 "Variable used by org files to set a category for agenda display.
1933 Such files should use a file variable to set it, for example
1935 # -*- mode: org; org-category: \"ELisp\"
1937 or contain a special line
1941 If the file does not specify a category, then file's base name
1943 (make-variable-buffer-local 'org-category
)
1945 (defcustom org-agenda-files nil
1946 "The files to be used for agenda display.
1947 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
1948 \\[org-remove-file]. You can also use customize to edit the list.
1950 If an entry is a directory, all files in that directory that are matched by
1951 `org-agenda-file-regexp' will be part of the file list.
1953 If the value of the variable is not a list but a single file name, then
1954 the list of agenda files is actually stored and maintained in that file, one
1955 agenda file per line."
1958 (repeat :tag
"List of files and directories" file
)
1959 (file :tag
"Store list in a file\n" :value
"~/.agenda_files")))
1961 (defcustom org-agenda-file-regexp
"\\`[^.].*\\.org\\'"
1962 "Regular expression to match files for `org-agenda-files'.
1963 If any element in the list in that variable contains a directory instead
1964 of a normal file, all files in that directory that are matched by this
1965 regular expression will be included."
1969 (defcustom org-agenda-text-search-extra-files nil
1970 "List of extra files to be searched by text search commands.
1971 These files will be search in addition to the agenda files by the
1972 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
1973 Note that these files will only be searched for text search commands,
1974 not for the other agenda views like todo lists, tag searches or the weekly
1975 agenda. This variable is intended to list notes and possibly archive files
1976 that should also be searched by these two commands.
1977 In fact, if the first element in the list is the symbol `agenda-archives',
1978 than all archive files of all agenda files will be added to the search
1981 :type
'(set :greedy t
1982 (const :tag
"Agenda Archives" agenda-archives
)
1983 (repeat :inline t
(file))))
1985 (if (fboundp 'defvaralias
)
1986 (defvaralias 'org-agenda-multi-occur-extra-files
1987 'org-agenda-text-search-extra-files
))
1989 (defcustom org-agenda-skip-unavailable-files nil
1990 "t means to just skip non-reachable files in `org-agenda-files'.
1991 Nil means to remove them, after a query, from the list."
1995 (defcustom org-calendar-to-agenda-key
[?c
]
1996 "The key to be installed in `calendar-mode-map' for switching to the agenda.
1997 The command `org-calendar-goto-agenda' will be bound to this key. The
1998 default is the character `c' because then `c' can be used to switch back and
1999 forth between agenda and calendar."
2003 (defcustom org-calendar-agenda-action-key
[?k
]
2004 "The key to be installed in `calendar-mode-map' for agenda-action.
2005 The command `org-agenda-action' will be bound to this key. The
2006 default is the character `k' because we use the same key in the agenda."
2010 (eval-after-load "calendar"
2012 (org-defkey calendar-mode-map org-calendar-to-agenda-key
2013 'org-calendar-goto-agenda
)
2014 (org-defkey calendar-mode-map org-calendar-agenda-action-key
2015 'org-agenda-action
)))
2017 (defgroup org-latex nil
2018 "Options for embedding LaTeX code into Org-mode."
2022 (defcustom org-format-latex-options
2023 '(:foreground default
:background default
:scale
1.0
2024 :html-foreground
"Black" :html-background
"Transparent" :html-scale
1.0
2025 :matchers
("begin" "$" "$$" "\\(" "\\["))
2026 "Options for creating images from LaTeX fragments.
2027 This is a property list with the following properties:
2028 :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
2029 `default' means use the foreground of the default face.
2030 :background the background color, or \"Transparent\".
2031 `default' means use the background of the default face.
2032 :scale a scaling factor for the size of the images.
2033 :html-foreground, :html-background, :html-scale
2034 the same numbers for HTML export.
2035 :matchers a list indicating which matchers should be used to
2036 find LaTeX fragments. Valid members of this list are:
2037 \"begin\" find environments
2038 \"$\" find math expressions surrounded by $...$
2039 \"$$\" find math expressions surrounded by $$....$$
2040 \"\\(\" find math expressions surrounded by \\(...\\)
2041 \"\\ [\" find math expressions surrounded by \\ [...\\]"
2045 (defcustom org-format-latex-header
"\\documentclass{article}
2046 \\usepackage{fullpage} % do not remove
2047 \\usepackage{amssymb}
2048 \\usepackage[usenames]{color}
2049 \\usepackage{amsmath}
2050 \\usepackage{latexsym}
2051 \\usepackage[mathscr]{eucal}
2052 \\pagestyle{empty} % do not remove"
2053 "The document header used for processing LaTeX fragments."
2058 (defgroup org-font-lock nil
2059 "Font-lock settings for highlighting in Org-mode."
2060 :tag
"Org Font Lock"
2063 (defcustom org-level-color-stars-only nil
2064 "Non-nil means fontify only the stars in each headline.
2065 When nil, the entire headline is fontified.
2066 Changing it requires restart of `font-lock-mode' to become effective
2067 also in regions already fontified."
2068 :group
'org-font-lock
2071 (defcustom org-hide-leading-stars nil
2072 "Non-nil means, hide the first N-1 stars in a headline.
2073 This works by using the face `org-hide' for these stars. This
2074 face is white for a light background, and black for a dark
2075 background. You may have to customize the face `org-hide' to
2077 Changing it requires restart of `font-lock-mode' to become effective
2078 also in regions already fontified.
2079 You may also set this on a per-file basis by adding one of the following
2080 lines to the buffer:
2082 #+STARTUP: hidestars
2083 #+STARTUP: showstars"
2084 :group
'org-font-lock
2087 (defcustom org-fontify-done-headline nil
2088 "Non-nil means, change the face of a headline if it is marked DONE.
2089 Normally, only the TODO/DONE keyword indicates the state of a headline.
2090 When this is non-nil, the headline after the keyword is set to the
2091 `org-headline-done' as an additional indication."
2092 :group
'org-font-lock
2095 (defcustom org-fontify-emphasized-text t
2096 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
2097 Changing this variable requires a restart of Emacs to take effect."
2098 :group
'org-font-lock
2101 (defcustom org-highlight-latex-fragments-and-specials nil
2102 "Non-nil means, fontify what is treated specially by the exporters."
2103 :group
'org-font-lock
2106 (defcustom org-hide-emphasis-markers nil
2107 "Non-nil mean font-lock should hide the emphasis marker characters."
2108 :group
'org-font-lock
2111 (defvar org-emph-re nil
2112 "Regular expression for matching emphasis.")
2113 (defvar org-verbatim-re nil
2114 "Regular expression for matching verbatim text.")
2115 (defvar org-emphasis-regexp-components
) ; defined just below
2116 (defvar org-emphasis-alist
) ; defined just below
2117 (defun org-set-emph-re (var val
)
2118 "Set variable and compute the emphasis regular expression."
2120 (when (and (boundp 'org-emphasis-alist
)
2121 (boundp 'org-emphasis-regexp-components
)
2122 org-emphasis-alist org-emphasis-regexp-components
)
2123 (let* ((e org-emphasis-regexp-components
)
2129 (stacked (and nil
(nth 5 e
))) ; stacked is no longer allowed, forced to nil
2130 (body1 (concat body
"*?"))
2131 (markers (mapconcat 'car org-emphasis-alist
""))
2132 (vmarkers (mapconcat
2133 (lambda (x) (if (eq (nth 4 x
) 'verbatim
) (car x
) ""))
2134 org-emphasis-alist
"")))
2135 ;; make sure special characters appear at the right position in the class
2136 (if (string-match "\\^" markers
)
2137 (setq markers
(concat (replace-match "" t t markers
) "^")))
2138 (if (string-match "-" markers
)
2139 (setq markers
(concat (replace-match "" t t markers
) "-")))
2140 (if (string-match "\\^" vmarkers
)
2141 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "^")))
2142 (if (string-match "-" vmarkers
)
2143 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "-")))
2145 (setq body1
(concat body1
"\\(?:\n" body
"*?\\)\\{0,"
2146 (int-to-string nl
) "\\}")))
2149 (concat "\\([" pre
(if (and nil stacked
) markers
) "]\\|^\\)"
2151 "\\([" markers
"]\\)"
2154 "[^" border
(if (and nil stacked
) markers
) "]"
2156 "[^" border
(if (and nil stacked
) markers
) "]"
2159 "\\([" post
(if (and nil stacked
) markers
) "]\\|$\\)"))
2160 (setq org-verbatim-re
2161 (concat "\\([" pre
"]\\|^\\)"
2163 "\\([" vmarkers
"]\\)"
2171 "\\([" post
"]\\|$\\)")))))
2173 (defcustom org-emphasis-regexp-components
2174 '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
2175 "Components used to build the regular expression for emphasis.
2176 This is a list with 6 entries. Terminology: In an emphasis string
2177 like \" *strong word* \", we call the initial space PREMATCH, the final
2178 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
2179 and \"trong wor\" is the body. The different components in this variable
2180 specify what is allowed/forbidden in each part:
2182 pre Chars allowed as prematch. Beginning of line will be allowed too.
2183 post Chars allowed as postmatch. End of line will be allowed too.
2184 border The chars *forbidden* as border characters.
2185 body-regexp A regexp like \".\" to match a body character. Don't use
2186 non-shy groups here, and don't allow newline here.
2187 newline The maximum number of newlines allowed in an emphasis exp.
2189 Use customize to modify this, or restart Emacs after changing it."
2190 :group
'org-font-lock
2191 :set
'org-set-emph-re
2193 (sexp :tag
"Allowed chars in pre ")
2194 (sexp :tag
"Allowed chars in post ")
2195 (sexp :tag
"Forbidden chars in border ")
2196 (sexp :tag
"Regexp for body ")
2197 (integer :tag
"number of newlines allowed")
2198 (option (boolean :tag
"Please ignore this button"))))
2200 (defcustom org-emphasis-alist
2201 `(("*" bold
"<b>" "</b>")
2202 ("/" italic
"<i>" "</i>")
2203 ("_" underline
"<u>" "</u>")
2204 ("=" org-code
"<code>" "</code>" verbatim
)
2205 ("~" org-verbatim
"" "" verbatim
)
2206 ("+" ,(if (featurep 'xemacs
) 'org-table
'(:strike-through t
))
2209 "Special syntax for emphasized text.
2210 Text starting and ending with a special character will be emphasized, for
2211 example *bold*, _underlined_ and /italic/. This variable sets the marker
2212 characters, the face to be used by font-lock for highlighting in Org-mode
2213 Emacs buffers, and the HTML tags to be used for this.
2214 Use customize to modify this, or restart Emacs after changing it."
2215 :group
'org-font-lock
2216 :set
'org-set-emph-re
2219 (string :tag
"Marker character")
2221 (face :tag
"Font-lock-face")
2222 (plist :tag
"Face property list"))
2223 (string :tag
"HTML start tag")
2224 (string :tag
"HTML end tag")
2225 (option (const verbatim
)))))
2227 ;;; Miscellaneous options
2229 (defgroup org-completion nil
2230 "Completion in Org-mode."
2231 :tag
"Org Completion"
2234 (defcustom org-completion-fallback-command
'hippie-expand
2235 "The expansion command called by \\[org-complete] in normal context.
2236 Normal means, no org-mode-specific context."
2237 :group
'org-completion
2240 ;;; Functions and variables from ther packages
2241 ;; Declared here to avoid compiler warnings
2244 (defvar outline-mode-menu-heading
)
2245 (defvar outline-mode-menu-show
)
2246 (defvar outline-mode-menu-hide
)
2247 (defvar zmacs-regions
) ; XEmacs regions
2250 (defvar mark-active
)
2253 (declare-function calendar-absolute-from-iso
"cal-iso" (date))
2254 (declare-function calendar-forward-day
"cal-move" (arg))
2255 (declare-function calendar-goto-date
"cal-move" (date))
2256 (declare-function calendar-goto-today
"cal-move" ())
2257 (declare-function calendar-iso-from-absolute
"cal-iso" (date))
2258 (defvar calc-embedded-close-formula
)
2259 (defvar calc-embedded-open-formula
)
2260 (declare-function cdlatex-tab
"ext:cdlatex" ())
2261 (declare-function dired-get-filename
"dired" (&optional localp no-error-if-not-filep
))
2262 (defvar font-lock-unfontify-region-function
)
2263 (declare-function iswitchb-mode
"iswitchb" (&optional arg
))
2264 (declare-function iswitchb-read-buffer
(prompt &optional default require-match start matches-set
))
2265 (defvar iswitchb-temp-buflist
)
2266 (declare-function org-gnus-follow-link
"org-gnus" (&optional group article
))
2267 (declare-function org-agenda-skip
"org-agenda" ())
2268 (declare-function org-format-agenda-item
"org-agenda"
2269 (extra txt
&optional category tags dotime noprefix remove-re
))
2270 (declare-function org-agenda-new-marker
"org-agenda" (&optional pos
))
2271 (declare-function org-agenda-change-all-lines
"org-agenda"
2272 (newhead hdmarker
&optional fixface
))
2273 (declare-function org-agenda-set-restriction-lock
"org-agenda" (&optional type
))
2274 (declare-function org-agenda-maybe-redo
"org-agenda" ())
2275 (declare-function org-agenda-save-markers-for-cut-and-paste
"org-agenda"
2277 (declare-function parse-time-string
"parse-time" (string))
2278 (declare-function remember
"remember" (&optional initial
))
2279 (declare-function remember-buffer-desc
"remember" ())
2280 (declare-function remember-finalize
"remember" ())
2281 (defvar remember-save-after-remembering
)
2282 (defvar remember-data-file
)
2283 (defvar remember-register
)
2284 (defvar remember-buffer
)
2285 (defvar remember-handler-functions
)
2286 (defvar remember-annotation-functions
)
2287 (defvar texmathp-why
)
2288 (declare-function speedbar-line-directory
"speedbar" (&optional depth
))
2289 (declare-function table--at-cell-p
"table" (position &optional object at-column
))
2291 (defvar w3m-current-url
)
2292 (defvar w3m-current-title
)
2294 (defvar org-latex-regexps
)
2296 ;;; Autoload and prepare some org modules
2298 ;; Some table stuff that needs to be defined here, because it is used
2299 ;; by the functions setting up org-mode or checking for table context.
2301 (defconst org-table-any-line-regexp
"^[ \t]*\\(|\\|\\+-[-+]\\)"
2302 "Detects an org-type or table-type table.")
2303 (defconst org-table-line-regexp
"^[ \t]*|"
2304 "Detects an org-type table line.")
2305 (defconst org-table-dataline-regexp
"^[ \t]*|[^-]"
2306 "Detects an org-type table line.")
2307 (defconst org-table-hline-regexp
"^[ \t]*|-"
2308 "Detects an org-type table hline.")
2309 (defconst org-table1-hline-regexp
"^[ \t]*\\+-[-+]"
2310 "Detects a table-type table hline.")
2311 (defconst org-table-any-border-regexp
"^[ \t]*[^|+ \t]"
2312 "Searching from within a table (any type) this finds the first line
2313 outside the table.")
2315 ;; Autoload the functions in org-table.el that are needed by functions here.
2318 (org-autoload "org-table"
2319 '(org-table-align org-table-begin org-table-blank-field
2320 org-table-convert org-table-convert-region org-table-copy-down
2321 org-table-copy-region org-table-create
2322 org-table-create-or-convert-from-region
2323 org-table-create-with-table.el org-table-current-dline
2324 org-table-cut-region org-table-delete-column org-table-edit-field
2325 org-table-edit-formulas org-table-end org-table-eval-formula
2326 org-table-export org-table-field-info
2327 org-table-get-stored-formulas org-table-goto-column
2328 org-table-hline-and-move org-table-import org-table-insert-column
2329 org-table-insert-hline org-table-insert-row org-table-iterate
2330 org-table-justify-field-maybe org-table-kill-row
2331 org-table-maybe-eval-formula org-table-maybe-recalculate-line
2332 org-table-move-column org-table-move-column-left
2333 org-table-move-column-right org-table-move-row
2334 org-table-move-row-down org-table-move-row-up
2335 org-table-next-field org-table-next-row org-table-paste-rectangle
2336 org-table-previous-field org-table-recalculate
2337 org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
2338 org-table-toggle-coordinate-overlays
2339 org-table-toggle-formula-debugger org-table-wrap-region
2340 orgtbl-mode turn-on-orgtbl
)))
2342 (defun org-at-table-p (&optional table-type
)
2343 "Return t if the cursor is inside an org-type table.
2344 If TABLE-TYPE is non-nil, also check for table.el-type tables."
2345 (if org-enable-table-editor
2347 (beginning-of-line 1)
2348 (looking-at (if table-type org-table-any-line-regexp
2349 org-table-line-regexp
)))
2351 (defsubst org-table-p
() (org-at-table-p))
2353 (defun org-at-table.el-p
()
2354 "Return t if and only if we are at a table.el table."
2355 (and (org-at-table-p 'any
)
2357 (goto-char (org-table-begin 'any
))
2358 (looking-at org-table1-hline-regexp
))))
2359 (defun org-table-recognize-table.el
()
2360 "If there is a table.el table nearby, recognize it and move into it."
2361 (if org-table-tab-recognizes-table.el
2362 (if (org-at-table.el-p
)
2364 (beginning-of-line 1)
2365 (if (looking-at org-table-dataline-regexp
)
2367 (if (looking-at org-table1-hline-regexp
)
2369 (beginning-of-line 2)
2370 (if (looking-at org-table-any-border-regexp
)
2371 (beginning-of-line -
1)))))
2372 (if (re-search-forward "|" (org-table-end t
) t
)
2375 (if (table--at-cell-p (point))
2377 (message "recognizing table.el table...")
2378 (table-recognize-table)
2379 (message "recognizing table.el table...done")))
2380 (error "This should not happen..."))
2385 (defun org-at-table-hline-p ()
2386 "Return t if the cursor is inside a hline in a table."
2387 (if org-enable-table-editor
2389 (beginning-of-line 1)
2390 (looking-at org-table-hline-regexp
))
2393 (defvar org-table-clean-did-remove-column nil
)
2395 (defun org-table-map-tables (function)
2396 "Apply FUNCTION to the start of all tables in the buffer."
2400 (goto-char (point-min))
2401 (while (re-search-forward org-table-any-line-regexp nil t
)
2402 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
2403 (beginning-of-line 1)
2404 (if (looking-at org-table-line-regexp
)
2405 (save-excursion (funcall function
)))
2406 (re-search-forward org-table-any-border-regexp nil
1))))
2407 (message "Mapping tables: done"))
2409 ;; Declare and autoload functions from org-exp.el
2411 (declare-function org-default-export-plist
"org-exp")
2412 (declare-function org-infile-export-plist
"org-exp")
2413 (declare-function org-get-current-options
"org-exp")
2415 (org-autoload "org-exp"
2416 '(org-export org-export-as-ascii org-export-visible
2417 org-insert-export-options-template org-export-as-html-and-open
2418 org-export-as-html-batch org-export-as-html-to-buffer
2419 org-replace-region-by-html org-export-region-as-html
2420 org-export-as-html org-export-icalendar-this-file
2421 org-export-icalendar-all-agenda-files
2422 org-table-clean-before-export
2423 org-export-icalendar-combine-agenda-files org-export-as-xoxo
)))
2425 ;; Declare and autoload functions from org-exp.el
2428 (org-autoload "org-exp"
2429 '(org-agenda org-agenda-list org-search-view
2430 org-todo-list org-tags-view org-agenda-list-stuck-projects
2431 org-diary org-agenda-to-appt
)))
2433 ;; Autoload org-remember
2436 (org-autoload "org-remember"
2437 '(org-remember-insinuate org-remember-annotation
2438 org-remember-apply-template org-remember org-remember-handler
)))
2440 ;; Autoload org-clock.el
2443 (declare-function org-clock-save-markers-for-cut-and-paste
"org-clock"
2445 (declare-function org-update-mode-line
"org-clock" ())
2446 (defvar org-clock-start-time
)
2447 (defvar org-clock-marker
(make-marker)
2448 "Marker recording the last clock-in.")
2453 '(org-clock-in org-clock-out org-clock-cancel
2454 org-clock-goto org-clock-sum org-clock-display
2455 org-remove-clock-overlays org-clock-report
2456 org-clocktable-shift org-dblock-write
:clocktable
2457 org-get-clocktable
)))
2459 (defun org-clock-update-time-maybe ()
2460 "If this is a CLOCK line, update it and return t.
2461 Otherwise, return nil."
2464 (beginning-of-line 1)
2465 (skip-chars-forward " \t")
2466 (when (looking-at org-clock-string
)
2467 (let ((re (concat "[ \t]*" org-clock-string
2468 " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
2469 "\\([ \t]*=>.*\\)?\\)?"))
2472 ((not (looking-at re
))
2474 ((not (match-end 2))
2475 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
2476 (> org-clock-marker
(point))
2477 (<= org-clock-marker
(point-at-eol)))
2478 ;; The clock is running here
2479 (setq org-clock-start-time
2481 (org-parse-time-string (match-string 1))))
2482 (org-update-mode-line)))
2484 (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
2486 (setq ts
(match-string 1)
2487 te
(match-string 3))
2488 (setq s
(- (time-to-seconds
2489 (apply 'encode-time
(org-parse-time-string te
)))
2491 (apply 'encode-time
(org-parse-time-string ts
))))
2492 h
(floor (/ s
3600))
2496 (insert " => " (format "%2d:%02d" h m
))
2499 (defun org-check-running-clock ()
2500 "Check if the current buffer contains the running clock.
2501 If yes, offer to stop it and to save the buffer with the changes."
2502 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
2503 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
2506 (when (y-or-n-p "Save changed buffer?")
2509 (defun org-clocktable-try-shift (dir n
)
2510 "Check if this line starts a clock table, if yes, shift the time block."
2511 (when (org-match-line "#\\+BEGIN: clocktable\\>")
2512 (org-clocktable-shift dir n
)))
2514 ;; Autoload archiving code
2515 ;; The stuff that is needed for cycling and tags has to be defined here.
2517 (defgroup org-archive nil
2518 "Options concerning archiving in Org-mode."
2520 :group
'org-structure
)
2522 (defcustom org-archive-location
"%s_archive::"
2523 "The location where subtrees should be archived.
2525 Otherwise, the value of this variable is a string, consisting of two
2526 parts, separated by a double-colon.
2528 The first part is a file name - when omitted, archiving happens in the same
2529 file. %s will be replaced by the current file name (without directory part).
2530 Archiving to a different file is useful to keep archived entries from
2531 contributing to the Org-mode Agenda.
2533 The part after the double colon is a headline. The archived entries will be
2534 filed under that headline. When omitted, the subtrees are simply filed away
2535 at the end of the file, as top-level entries.
2537 Here are a few examples:
2539 If the current file is Projects.org, archive in file
2540 Projects.org_archive, as top-level trees. This is the default.
2542 \"::* Archived Tasks\"
2543 Archive in the current file, under the top-level headline
2544 \"* Archived Tasks\".
2546 \"~/org/archive.org::\"
2547 Archive in file ~/org/archive.org (absolute path), as top-level trees.
2549 \"basement::** Finished Tasks\"
2550 Archive in file ./basement (relative path), as level 3 trees
2551 below the level 2 heading \"** Finished Tasks\".
2553 You may set this option on a per-file basis by adding to the buffer a
2556 #+ARCHIVE: basement::** Finished Tasks
2558 You may also define it locally for a subtree by setting an ARCHIVE property
2559 in the entry. If such a property is found in an entry, or anywhere up
2560 the hierarchy, it will be used."
2564 (defcustom org-archive-tag
"ARCHIVE"
2565 "The tag that marks a subtree as archived.
2566 An archived subtree does not open during visibility cycling, and does
2567 not contribute to the agenda listings.
2568 After changing this, font-lock must be restarted in the relevant buffers to
2569 get the proper fontification."
2571 :group
'org-keywords
2574 (defcustom org-agenda-skip-archived-trees t
2575 "Non-nil means, the agenda will skip any items located in archived trees.
2576 An archived tree is a tree marked with the tag ARCHIVE."
2578 :group
'org-agenda-skip
2581 (defcustom org-cycle-open-archived-trees nil
2582 "Non-nil means, `org-cycle' will open archived trees.
2583 An archived tree is a tree marked with the tag ARCHIVE.
2584 When nil, archived trees will stay folded. You can still open them with
2585 normal outline commands like `show-all', but not with the cycling commands."
2590 (defcustom org-sparse-tree-open-archived-trees nil
2591 "Non-nil means sparse tree construction shows matches in archived trees.
2592 When nil, matches in these trees are highlighted, but the trees are kept in
2595 :group
'org-sparse-trees
2598 (defun org-cycle-hide-archived-subtrees (state)
2599 "Re-hide all archived subtrees after a visibility state change."
2600 (when (and (not org-cycle-open-archived-trees
)
2601 (not (memq state
'(overview folded
))))
2603 (let* ((globalp (memq state
'(contents all
)))
2604 (beg (if globalp
(point-min) (point)))
2605 (end (if globalp
(point-max) (org-end-of-subtree t
))))
2606 (org-hide-archived-subtrees beg end
)
2608 (if (looking-at (concat ".*:" org-archive-tag
":"))
2609 (message "%s" (substitute-command-keys
2610 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
2612 (defun org-force-cycle-archived ()
2613 "Cycle subtree even if it is archived."
2615 (setq this-command
'org-cycle
)
2616 (let ((org-cycle-open-archived-trees t
))
2617 (call-interactively 'org-cycle
)))
2619 (defun org-hide-archived-subtrees (beg end
)
2620 "Re-hide all archived subtrees after a visibility state change."
2622 (let* ((re (concat ":" org-archive-tag
":")))
2624 (while (re-search-forward re end t
)
2625 (and (org-on-heading-p) (hide-subtree))
2626 (org-end-of-subtree t
)))))
2628 (defalias 'org-advertized-archive-subtree
'org-archive-subtree
)
2631 (org-autoload "org-archive"
2632 '(org-add-archive-files org-archive-subtree
2633 org-archive-to-archive-sibling org-toggle-archive-tag
)))
2635 ;; Autoload Column View Code
2637 (declare-function org-columns-number-to-string
"org-colview")
2638 (declare-function org-columns-get-format-and-top-level
"org-colview")
2639 (declare-function org-columns-compute
"org-colview")
2641 (org-autoload (if (featurep 'xemacs
) "org-colview-xemacs" "org-colview")
2642 '(org-columns-number-to-string org-columns-get-format-and-top-level
2643 org-columns-compute org-agenda-columns org-columns-remove-overlays
2644 org-columns org-insert-columns-dblock
))
2648 (org-autoload "org-id"
2649 '(org-id-get-create org-id-new org-id-copy org-id-get
2650 org-id-get-with-outline-path-completion
2651 org-id-get-with-outline-drilling
2652 org-id-goto org-id-find
))
2654 ;;; Variables for pre-computed regular expressions, all buffer local
2656 (defvar org-drawer-regexp nil
2657 "Matches first line of a hidden block.")
2658 (make-variable-buffer-local 'org-drawer-regexp
)
2659 (defvar org-todo-regexp nil
2660 "Matches any of the TODO state keywords.")
2661 (make-variable-buffer-local 'org-todo-regexp
)
2662 (defvar org-not-done-regexp nil
2663 "Matches any of the TODO state keywords except the last one.")
2664 (make-variable-buffer-local 'org-not-done-regexp
)
2665 (defvar org-todo-line-regexp nil
2666 "Matches a headline and puts TODO state into group 2 if present.")
2667 (make-variable-buffer-local 'org-todo-line-regexp
)
2668 (defvar org-complex-heading-regexp nil
2669 "Matches a headline and puts everything into groups:
2671 group 2: The todo keyword, maybe
2672 group 3: Priority cookie
2673 group 4: True headline
2675 (make-variable-buffer-local 'org-complex-heading-regexp
)
2676 (defvar org-todo-line-tags-regexp nil
2677 "Matches a headline and puts TODO state into group 2 if present.
2678 Also put tags into group 4 if tags are present.")
2679 (make-variable-buffer-local 'org-todo-line-tags-regexp
)
2680 (defvar org-nl-done-regexp nil
2681 "Matches newline followed by a headline with the DONE keyword.")
2682 (make-variable-buffer-local 'org-nl-done-regexp
)
2683 (defvar org-looking-at-done-regexp nil
2684 "Matches the DONE keyword a point.")
2685 (make-variable-buffer-local 'org-looking-at-done-regexp
)
2686 (defvar org-ds-keyword-length
12
2687 "Maximum length of the Deadline and SCHEDULED keywords.")
2688 (make-variable-buffer-local 'org-ds-keyword-length
)
2689 (defvar org-deadline-regexp nil
2690 "Matches the DEADLINE keyword.")
2691 (make-variable-buffer-local 'org-deadline-regexp
)
2692 (defvar org-deadline-time-regexp nil
2693 "Matches the DEADLINE keyword together with a time stamp.")
2694 (make-variable-buffer-local 'org-deadline-time-regexp
)
2695 (defvar org-deadline-line-regexp nil
2696 "Matches the DEADLINE keyword and the rest of the line.")
2697 (make-variable-buffer-local 'org-deadline-line-regexp
)
2698 (defvar org-scheduled-regexp nil
2699 "Matches the SCHEDULED keyword.")
2700 (make-variable-buffer-local 'org-scheduled-regexp
)
2701 (defvar org-scheduled-time-regexp nil
2702 "Matches the SCHEDULED keyword together with a time stamp.")
2703 (make-variable-buffer-local 'org-scheduled-time-regexp
)
2704 (defvar org-closed-time-regexp nil
2705 "Matches the CLOSED keyword together with a time stamp.")
2706 (make-variable-buffer-local 'org-closed-time-regexp
)
2708 (defvar org-keyword-time-regexp nil
2709 "Matches any of the 4 keywords, together with the time stamp.")
2710 (make-variable-buffer-local 'org-keyword-time-regexp
)
2711 (defvar org-keyword-time-not-clock-regexp nil
2712 "Matches any of the 3 keywords, together with the time stamp.")
2713 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp
)
2714 (defvar org-maybe-keyword-time-regexp nil
2715 "Matches a timestamp, possibly preceeded by a keyword.")
2716 (make-variable-buffer-local 'org-maybe-keyword-time-regexp
)
2717 (defvar org-planning-or-clock-line-re nil
2718 "Matches a line with planning or clock info.")
2719 (make-variable-buffer-local 'org-planning-or-clock-line-re
)
2721 (defconst org-plain-time-of-day-regexp
2724 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
2727 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
2729 "Regular expression to match a plain time or time range.
2730 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
2731 groups carry important information:
2733 1 the first time, range or not
2734 8 the second time, if it is a range.")
2736 (defconst org-plain-time-extension-regexp
2739 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
2740 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
2741 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
2742 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
2743 groups carry important information:
2746 9 minutes of duration")
2748 (defconst org-stamp-time-of-day-regexp
2750 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
2751 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
2753 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
2754 "Regular expression to match a timestamp time or time range.
2755 After a match, the following groups carry important information:
2757 1 date plus weekday, for backreferencing to make sure both times on same day
2758 2 the first time, range or not
2759 4 the second time, if it is a range.")
2761 (defconst org-startup-options
2762 '(("fold" org-startup-folded t
)
2763 ("overview" org-startup-folded t
)
2764 ("nofold" org-startup-folded nil
)
2765 ("showall" org-startup-folded nil
)
2766 ("content" org-startup-folded content
)
2767 ("hidestars" org-hide-leading-stars t
)
2768 ("showstars" org-hide-leading-stars nil
)
2769 ("odd" org-odd-levels-only t
)
2770 ("oddeven" org-odd-levels-only nil
)
2771 ("align" org-startup-align-all-tables t
)
2772 ("noalign" org-startup-align-all-tables nil
)
2773 ("customtime" org-display-custom-times t
)
2774 ("logdone" org-log-done time
)
2775 ("lognotedone" org-log-done note
)
2776 ("nologdone" org-log-done nil
)
2777 ("lognoteclock-out" org-log-note-clock-out t
)
2778 ("nolognoteclock-out" org-log-note-clock-out nil
)
2779 ("logrepeat" org-log-repeat state
)
2780 ("lognoterepeat" org-log-repeat note
)
2781 ("nologrepeat" org-log-repeat nil
)
2782 ("constcgs" constants-unit-system cgs
)
2783 ("constSI" constants-unit-system SI
))
2784 "Variable associated with STARTUP options for org-mode.
2785 Each element is a list of three items: The startup options as written
2786 in the #+STARTUP line, the corresponding variable, and the value to
2787 set this variable to if the option is found. An optional forth element PUSH
2788 means to push this value onto the list in the variable.")
2790 (defun org-set-regexps-and-options ()
2791 "Precompute regular expressions for current buffer."
2793 (org-set-local 'org-todo-kwd-alist nil
)
2794 (org-set-local 'org-todo-key-alist nil
)
2795 (org-set-local 'org-todo-key-trigger nil
)
2796 (org-set-local 'org-todo-keywords-1 nil
)
2797 (org-set-local 'org-done-keywords nil
)
2798 (org-set-local 'org-todo-heads nil
)
2799 (org-set-local 'org-todo-sets nil
)
2800 (org-set-local 'org-todo-log-states nil
)
2801 (org-set-local 'org-file-properties nil
)
2802 (org-set-local 'org-file-tags nil
)
2803 (let ((re (org-make-options-regexp
2804 '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
2805 "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
2806 "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
2808 kwds kws0 kwsa key log value cat arch tags const links hw dws
2809 tail sep kws1 prio props ftags drawers
2810 ext-setup-or-nil setup-contents
(start 0))
2814 (goto-char (point-min))
2815 (while (or (and ext-setup-or-nil
2816 (string-match re ext-setup-or-nil start
)
2817 (setq start
(match-end 0)))
2818 (and (setq ext-setup-or-nil nil start
0)
2819 (re-search-forward re nil t
)))
2820 (setq key
(upcase (match-string 1 ext-setup-or-nil
))
2821 value
(org-match-string-no-properties 2 ext-setup-or-nil
))
2823 ((equal key
"CATEGORY")
2824 (if (string-match "[ \t]+$" value
)
2825 (setq value
(replace-match "" t t value
)))
2827 ((member key
'("SEQ_TODO" "TODO"))
2828 (push (cons 'sequence
(org-split-string value splitre
)) kwds
))
2829 ((equal key
"TYP_TODO")
2830 (push (cons 'type
(org-split-string value splitre
)) kwds
))
2832 (setq tags
(append tags
(org-split-string value splitre
))))
2833 ((equal key
"COLUMNS")
2834 (org-set-local 'org-columns-default-format value
))
2836 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value
)
2837 (push (cons (match-string 1 value
)
2838 (org-trim (match-string 2 value
)))
2840 ((equal key
"PRIORITIES")
2841 (setq prio
(org-split-string value
" +")))
2842 ((equal key
"PROPERTY")
2843 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
2844 (push (cons (match-string 1 value
) (match-string 2 value
))
2846 ((equal key
"FILETAGS")
2847 (when (string-match "\\S-" value
)
2852 (mapcar (lambda (x) (org-split-string x
":"))
2853 (org-split-string value
)))))))
2854 ((equal key
"DRAWERS")
2855 (setq drawers
(org-split-string value splitre
)))
2856 ((equal key
"CONSTANTS")
2857 (setq const
(append const
(org-split-string value splitre
))))
2858 ((equal key
"STARTUP")
2859 (let ((opts (org-split-string value splitre
))
2861 (while (setq l
(pop opts
))
2862 (when (setq l
(assoc l org-startup-options
))
2863 (setq var
(nth 1 l
) val
(nth 2 l
))
2865 (set (make-local-variable var
) val
)
2866 (if (not (listp (symbol-value var
)))
2867 (set (make-local-variable var
) nil
))
2868 (set (make-local-variable var
) (symbol-value var
))
2869 (add-to-list var val
))))))
2870 ((equal key
"ARCHIVE")
2871 (string-match " *$" value
)
2872 (setq arch
(replace-match "" t t value
))
2873 (remove-text-properties 0 (length arch
)
2874 '(face t fontified t
) arch
))
2875 ((equal key
"SETUPFILE")
2876 (setq setup-contents
(org-file-contents
2878 (org-remove-double-quotes value
))
2880 (if (not ext-setup-or-nil
)
2881 (setq ext-setup-or-nil setup-contents start
0)
2882 (setq ext-setup-or-nil
2883 (concat (substring ext-setup-or-nil
0 start
)
2884 "\n" setup-contents
"\n"
2885 (substring ext-setup-or-nil start
)))))
2888 (org-set-local 'org-category
(intern cat
))
2889 (push (cons "CATEGORY" cat
) props
))
2891 (if (< (length prio
) 3) (setq prio
'("A" "C" "B")))
2892 (setq prio
(mapcar 'string-to-char prio
))
2893 (org-set-local 'org-highest-priority
(nth 0 prio
))
2894 (org-set-local 'org-lowest-priority
(nth 1 prio
))
2895 (org-set-local 'org-default-priority
(nth 2 prio
)))
2896 (and props
(org-set-local 'org-file-properties
(nreverse props
)))
2897 (and ftags
(org-set-local 'org-file-tags ftags
))
2898 (and drawers
(org-set-local 'org-drawers drawers
))
2899 (and arch
(org-set-local 'org-archive-location arch
))
2900 (and links
(setq org-link-abbrev-alist-local
(nreverse links
)))
2901 ;; Process the TODO keywords
2903 ;; Use the global values as if they had been given locally.
2904 (setq kwds
(default-value 'org-todo-keywords
))
2905 (if (stringp (car kwds
))
2906 (setq kwds
(list (cons org-todo-interpretation
2907 (default-value 'org-todo-keywords
)))))
2908 (setq kwds
(reverse kwds
)))
2909 (setq kwds
(nreverse kwds
))
2911 (while (setq kws
(pop kwds
))
2912 (setq inter
(pop kws
) sep
(member "|" kws
)
2913 kws0
(delete "|" (copy-sequence kws
))
2918 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x
)
2920 (setq kw
(match-string 1 x
)
2921 key
(and (match-end 2) (match-string 2 x
))
2922 log
(org-extract-log-state-settings x
))
2923 (push (cons kw
(and key
(string-to-char key
))) kwsa
)
2924 (and log
(push log org-todo-log-states
))
2926 (error "Invalid TODO keyword %s" x
)))
2928 kwsa
(if kwsa
(append '((:startgroup
))
2932 dws
(if sep
(org-remove-keyword-keys (cdr sep
)) (last kws1
))
2933 tail
(list inter hw
(car dws
) (org-last dws
)))
2934 (add-to-list 'org-todo-heads hw
'append
)
2935 (push kws1 org-todo-sets
)
2936 (setq org-done-keywords
(append org-done-keywords dws nil
))
2937 (setq org-todo-key-alist
(append org-todo-key-alist kwsa
))
2938 (mapc (lambda (x) (push (cons x tail
) org-todo-kwd-alist
)) kws1
)
2939 (setq org-todo-keywords-1
(append org-todo-keywords-1 kws1 nil
)))
2940 (setq org-todo-sets
(nreverse org-todo-sets
)
2941 org-todo-kwd-alist
(nreverse org-todo-kwd-alist
)
2942 org-todo-key-trigger
(delq nil
(mapcar 'cdr org-todo-key-alist
))
2943 org-todo-key-alist
(org-assign-fast-keys org-todo-key-alist
)))
2944 ;; Process the constants
2947 (while (setq e
(pop const
))
2948 (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e
)
2949 (push (cons (match-string 1 e
) (match-string 2 e
)) cst
)))
2950 (setq org-table-formula-constants-local cst
)))
2952 ;; Process the tags.
2955 (while (setq e
(pop tags
))
2957 ((equal e
"{") (push '(:startgroup
) tgs
))
2958 ((equal e
"}") (push '(:endgroup
) tgs
))
2959 ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e
)
2960 (push (cons (match-string 1 e
)
2961 (string-to-char (match-string 2 e
)))
2963 (t (push (list e
) tgs
))))
2964 (org-set-local 'org-tag-alist nil
)
2965 (while (setq e
(pop tgs
))
2966 (or (and (stringp (car e
))
2967 (assoc (car e
) org-tag-alist
))
2968 (push e org-tag-alist
)))))
2970 ;; Compute the regular expressions and other local variables
2971 (if (not org-done-keywords
)
2972 (setq org-done-keywords
(list (org-last org-todo-keywords-1
))))
2973 (setq org-ds-keyword-length
(+ 2 (max (length org-deadline-string
)
2974 (length org-scheduled-string
)
2975 (length org-clock-string
)
2976 (length org-closed-string
)))
2978 (concat "^[ \t]*:\\("
2979 (mapconcat 'regexp-quote org-drawers
"\\|")
2981 org-not-done-keywords
2982 (org-delete-all org-done-keywords
(copy-sequence org-todo-keywords-1
))
2984 (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
2988 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
2990 org-todo-line-regexp
2991 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
2992 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
2993 "\\)\\>\\)?[ \t]*\\(.*\\)")
2994 org-complex-heading-regexp
2995 (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
2996 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
2997 "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
2998 "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
3000 (concat "\n\\*+[ \t]+"
3001 "\\(?:" (mapconcat 'regexp-quote org-done-keywords
"\\|")
3003 org-todo-line-tags-regexp
3004 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3005 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
3007 "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
3008 org-looking-at-done-regexp
3010 (mapconcat 'regexp-quote org-done-keywords
"\\|") "\\)"
3012 org-deadline-regexp
(concat "\\<" org-deadline-string
)
3013 org-deadline-time-regexp
3014 (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>")
3015 org-deadline-line-regexp
3016 (concat "\\<\\(" org-deadline-string
"\\).*")
3017 org-scheduled-regexp
3018 (concat "\\<" org-scheduled-string
)
3019 org-scheduled-time-regexp
3020 (concat "\\<" org-scheduled-string
" *<\\([^>]+\\)>")
3021 org-closed-time-regexp
3022 (concat "\\<" org-closed-string
" *\\[\\([^]]+\\)\\]")
3023 org-keyword-time-regexp
3024 (concat "\\<\\(" org-scheduled-string
3025 "\\|" org-deadline-string
3026 "\\|" org-closed-string
3027 "\\|" org-clock-string
"\\)"
3028 " *[[<]\\([^]>]+\\)[]>]")
3029 org-keyword-time-not-clock-regexp
3030 (concat "\\<\\(" org-scheduled-string
3031 "\\|" org-deadline-string
3032 "\\|" org-closed-string
3034 " *[[<]\\([^]>]+\\)[]>]")
3035 org-maybe-keyword-time-regexp
3036 (concat "\\(\\<\\(" org-scheduled-string
3037 "\\|" org-deadline-string
3038 "\\|" org-closed-string
3039 "\\|" org-clock-string
"\\)\\)?"
3040 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
3041 org-planning-or-clock-line-re
3042 (concat "\\(?:^[ \t]*\\(" org-scheduled-string
3043 "\\|" org-deadline-string
3044 "\\|" org-closed-string
"\\|" org-clock-string
3047 (org-compute-latex-and-specials-regexp)
3048 (org-set-font-lock-defaults))))
3050 (defun org-file-contents (file &optional noerror
)
3051 "Return the contents of FILE, as a string."
3053 (not (file-readable-p file
)))
3056 (message "Cannot read file %s" file
)
3059 (error "Cannot read file %s" file
))
3061 (insert-file-contents file
)
3064 (defun org-extract-log-state-settings (x)
3065 "Extract the log state setting from a TODO keyword string.
3066 This will extract info from a string like \"WAIT(w@/!)\"."
3067 (let (kw key log1 log2
)
3068 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x
)
3069 (setq kw
(match-string 1 x
)
3070 key
(and (match-end 2) (match-string 2 x
))
3071 log1
(and (match-end 3) (match-string 3 x
))
3072 log2
(and (match-end 4) (match-string 4 x
)))
3075 (and log1
(if (equal log1
"!") 'time
'note
))
3076 (and log2
(if (equal log2
"!") 'time
'note
)))))))
3078 (defun org-remove-keyword-keys (list)
3079 "Remove a pair of parenthesis at the end of each string in LIST."
3081 (if (string-match "(.*)$" x
)
3082 (substring x
0 (match-beginning 0))
3086 ;; FIXME: this could be done much better, using second characters etc.
3087 (defun org-assign-fast-keys (alist)
3088 "Assign fast keys to a keyword-key alist.
3089 Respect keys that are already there."
3090 (let (new e k c c1 c2
(char ?a
))
3091 (while (setq e
(pop alist
))
3093 ((equal e
'(:startgroup
)) (push e new
))
3094 ((equal e
'(:endgroup
)) (push e new
))
3096 (setq k
(car e
) c2 nil
)
3099 ;; automatically assign a character.
3100 (setq c1
(string-to-char
3101 (downcase (substring
3102 k
(if (= (string-to-char k
) ?
@) 1 0)))))
3103 (if (or (rassoc c1 new
) (rassoc c1 alist
))
3104 (while (or (rassoc char new
) (rassoc char alist
))
3105 (setq char
(1+ char
)))
3107 (setq c
(or c2 char
)))
3108 (push (cons k c
) new
))))
3111 ;;; Some variables used in various places
3113 (defvar org-window-configuration nil
3114 "Used in various places to store a window configuration.")
3115 (defvar org-finish-function nil
3116 "Function to be called when `C-c C-c' is used.
3117 This is for getting out of special buffers like remember.")
3120 ;; FIXME: Occasionally check by commenting these, to make sure
3121 ;; no other functions uses these, forgetting to let-bind them.
3126 (defvar description
)
3128 ;; Defined somewhere in this file, but used before definition.
3129 (defvar org-html-entities
)
3130 (defvar org-struct-menu
)
3131 (defvar org-org-menu
)
3132 (defvar org-tbl-menu
)
3133 (defvar org-agenda-keymap
)
3135 ;;;; Define the Org-mode
3137 (if (and (not (keymapp outline-mode-map
)) (featurep 'allout
))
3138 (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or ugrade to newer allout, for example by switching to Emacs 22."))
3141 ;; We use a before-change function to check if a table might need
3143 (defvar org-table-may-need-update t
3144 "Indicates that a table might need an update.
3145 This variable is set by `org-before-change-function'.
3146 `org-table-align' sets it back to nil.")
3147 (defun org-before-change-function (beg end
)
3148 "Every change indicates that a table might need an update."
3149 (setq org-table-may-need-update t
))
3150 (defvar org-mode-map
)
3151 (defvar org-mode-hook nil
)
3152 (defvar org-inhibit-startup nil
) ; Dynamically-scoped param.
3153 (defvar org-agenda-keep-modes nil
) ; Dynamically-scoped param.
3154 (defvar org-table-buffer-is-an nil
)
3155 (defconst org-outline-regexp
"\\*+ ")
3158 (define-derived-mode org-mode outline-mode
"Org"
3159 "Outline-based notes management and organizer, alias
3160 \"Carsten's outline-mode for keeping track of everything.\"
3162 Org-mode develops organizational tasks around a NOTES file which
3163 contains information about projects as plain text. Org-mode is
3164 implemented on top of outline-mode, which is ideal to keep the content
3165 of large files well structured. It supports ToDo items, deadlines and
3166 time stamps, which magically appear in the diary listing of the Emacs
3167 calendar. Tables are easily created with a built-in table editor.
3168 Plain text URL-like links connect to websites, emails (VM), Usenet
3169 messages (Gnus), BBDB entries, and any files related to the project.
3170 For printing and sharing of notes, an Org-mode file (or a part of it)
3171 can be exported as a structured ASCII or HTML file.
3173 The following commands are available:
3177 ;; Get rid of Outline menus, they are not needed
3178 ;; Need to do this here because define-derived-mode sets up
3179 ;; the keymap so late. Still, it is a waste to call this each time
3180 ;; we switch another buffer into org-mode.
3181 (if (featurep 'xemacs
)
3182 (when (boundp 'outline-mode-menu-heading
)
3183 ;; Assume this is Greg's port, it used easymenu
3184 (easy-menu-remove outline-mode-menu-heading
)
3185 (easy-menu-remove outline-mode-menu-show
)
3186 (easy-menu-remove outline-mode-menu-hide
))
3187 (define-key org-mode-map
[menu-bar headings
] 'undefined
)
3188 (define-key org-mode-map
[menu-bar hide
] 'undefined
)
3189 (define-key org-mode-map
[menu-bar show
] 'undefined
))
3191 (org-load-modules-maybe)
3192 (easy-menu-add org-org-menu
)
3193 (easy-menu-add org-tbl-menu
)
3194 (org-install-agenda-files-menu)
3195 (if org-descriptive-links
(org-add-to-invisibility-spec '(org-link)))
3196 (org-add-to-invisibility-spec '(org-cwidth))
3197 (when (featurep 'xemacs
)
3198 (org-set-local 'line-move-ignore-invisible t
))
3199 (org-set-local 'outline-regexp org-outline-regexp
)
3200 (org-set-local 'outline-level
'org-outline-level
)
3201 (when (and org-ellipsis
3202 (fboundp 'set-display-table-slot
) (boundp 'buffer-display-table
)
3203 (fboundp 'make-glyph-code
))
3204 (unless org-display-table
3205 (setq org-display-table
(make-display-table)))
3206 (set-display-table-slot
3209 (lambda (c) (make-glyph-code c
(and (not (stringp org-ellipsis
))
3211 (if (stringp org-ellipsis
) org-ellipsis
"..."))))
3212 (setq buffer-display-table org-display-table
))
3213 (org-set-regexps-and-options)
3215 (org-set-local 'calc-embedded-open-mode
"# ")
3216 (modify-syntax-entry ?
# "<")
3217 (modify-syntax-entry ?
@ "w")
3218 (if org-startup-truncated
(setq truncate-lines t
))
3219 (org-set-local 'font-lock-unfontify-region-function
3220 'org-unfontify-region
)
3221 ;; Activate before-change-function
3222 (org-set-local 'org-table-may-need-update t
)
3223 (org-add-hook 'before-change-functions
'org-before-change-function nil
3225 ;; Check for running clock before killing a buffer
3226 (org-add-hook 'kill-buffer-hook
'org-check-running-clock nil
'local
)
3227 ;; Paragraphs and auto-filling
3228 (org-set-autofill-regexps)
3229 (setq indent-line-function
'org-indent-line-function
)
3230 (org-update-radio-target-regexp)
3232 ;; Comment characters
3233 ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
3234 (org-set-local 'comment-padding
" ")
3236 ;; Align options lines
3238 'align-mode-rules-list
3239 '((org-in-buffer-settings
3240 (regexp .
"^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
3241 (modes .
'(org-mode)))))
3244 (org-set-local 'imenu-create-index-function
3245 'org-imenu-get-tree
)
3247 ;; Make isearch reveal context
3248 (if (or (featurep 'xemacs
)
3249 (not (boundp 'outline-isearch-open-invisible-function
)))
3250 ;; Emacs 21 and XEmacs make use of the hook
3251 (org-add-hook 'isearch-mode-end-hook
'org-isearch-end
'append
'local
)
3252 ;; Emacs 22 deals with this through a special variable
3253 (org-set-local 'outline-isearch-open-invisible-function
3254 (lambda (&rest ignore
) (org-show-context 'isearch
))))
3256 ;; If empty file that did not turn on org-mode automatically, make it to.
3257 (if (and org-insert-mode-line-in-empty-file
3259 (= (point-min) (point-max)))
3260 (insert "# -*- mode: org -*-\n\n"))
3262 (unless org-inhibit-startup
3263 (when org-startup-align-all-tables
3264 (let ((bmp (buffer-modified-p)))
3265 (org-table-map-tables 'org-table-align
)
3266 (set-buffer-modified-p bmp
)))
3267 (org-set-startup-visibility)))
3269 (put 'org-mode
'flyspell-mode-predicate
'org-mode-flyspell-verify
)
3271 (defun org-current-time ()
3272 "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
3273 (if (> (car org-time-stamp-rounding-minutes
) 1)
3274 (let ((r (car org-time-stamp-rounding-minutes
))
3275 (time (decode-time)))
3277 (append (list 0 (* r
(floor (+ .5 (/ (float (nth 1 time
)) r
)))))
3281 ;;;; Font-Lock stuff, including the activators
3283 (defvar org-mouse-map
(make-sparse-keymap))
3284 (org-defkey org-mouse-map
3285 (if (featurep 'xemacs
) [button2] [mouse-2]) 'org-open-at-mouse)
3286 (org-defkey org-mouse-map
3287 (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
3288 (when org-mouse-1-follows-link
3289 (org-defkey org-mouse-map [follow-link] 'mouse-face))
3290 (when org-tab-follows-link
3291 (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
3292 (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
3293 (when org-return-follows-link
3294 (org-defkey org-mouse-map [(return)] 'org-open-at-point)
3295 (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
3297 (require 'font-lock)
3299 (defconst org-non-link-chars "]\t\n\r<>")
3300 (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
3302 (defvar org-link-types-re nil
3303 "Matches a link that has a url-like prefix like \"http:\"")
3304 (defvar org-link-re-with-space nil
3305 "Matches a link with spaces, optional angular brackets around it.")
3306 (defvar org-link-re-with-space2 nil
3307 "Matches a link with spaces, optional angular brackets around it.")
3308 (defvar org-angle-link-re nil
3309 "Matches link with angular brackets, spaces are allowed.")
3310 (defvar org-plain-link-re nil
3311 "Matches plain link, without spaces.")
3312 (defvar org-bracket-link-regexp nil
3313 "Matches a link in double brackets.")
3314 (defvar org-bracket-link-analytic-regexp nil
3315 "Regular expression used to analyze links.
3316 Here is what the match groups contain after a match:
3322 (defvar org-any-link-re nil
3323 "Regular expression matching any link.")
3325 (defun org-make-link-regexps ()
3326 "Update the link regular expressions.
3327 This should be called after the variable `org-link-types' has changed."
3328 (setq org-link-types-re
3330 "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
3331 org-link-re-with-space
3333 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3334 "\\([^" org-non-link-chars " ]"
3335 "[^" org-non-link-chars "]*"
3336 "[^" org-non-link-chars " ]\\)>?")
3337 org-link-re-with-space2
3339 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3340 "\\([^" org-non-link-chars " ]"
3342 "[^" org-non-link-chars " ]\\)>?")
3345 "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3346 "\\([^" org-non-link-chars " ]"
3347 "[^" org-non-link-chars "]*"
3351 "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3352 "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
3353 org-bracket-link-regexp
3354 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
3355 org-bracket-link-analytic-regexp
3358 "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
3361 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
3364 (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
3365 org-angle-link-re "\\)\\|\\("
3366 org-plain-link-re "\\)")))
3368 (org-make-link-regexps)
3370 (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
3371 "Regular expression for fast time stamp matching.")
3372 (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
3373 "Regular expression for fast time stamp matching.")
3374 (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
3375 "Regular expression matching time strings for analysis.
3376 This one does not require the space after the date, so it can be used
3377 on a string that terminates immediately after the date.")
3378 (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
3379 "Regular expression matching time strings for analysis.")
3380 (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
3381 "Regular expression matching time stamps, with groups.")
3382 (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
3383 "Regular expression matching time stamps (also [..]), with groups.")
3384 (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
3385 "Regular expression matching a time stamp range.")
3386 (defconst org-tr-regexp-both
3387 (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
3388 "Regular expression matching a time stamp range.")
3389 (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
3390 org-ts-regexp "\\)?")
3391 "Regular expression matching a time stamp or time stamp range.")
3392 (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
3393 org-ts-regexp-both "\\)?")
3394 "Regular expression matching a time stamp or time stamp range.
3395 The time stamps may be either active or inactive.")
3397 (defvar org-emph-face nil)
3399 (defun org-do-emphasis-faces (limit)
3400 "Run through the buffer and add overlays to links."
3402 (while (and (not rtn) (re-search-forward org-emph-re limit t))
3403 (if (not (= (char-after (match-beginning 3))
3404 (char-after (match-beginning 4))))
3407 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
3409 (nth 1 (assoc (match-string 3)
3410 org-emphasis-alist)))
3411 (add-text-properties (match-beginning 2) (match-end 2)
3412 '(font-lock-multiline t))
3413 (when org-hide-emphasis-markers
3414 (add-text-properties (match-end 4) (match-beginning 5)
3415 '(invisible org-link))
3416 (add-text-properties (match-beginning 3) (match-end 3)
3417 '(invisible org-link)))))
3421 (defun org-emphasize (&optional char)
3422 "Insert or change an emphasis, i.e. a font like bold or italic.
3423 If there is an active region, change that region to a new emphasis.
3424 If there is no region, just insert the marker characters and position
3425 the cursor between them.
3426 CHAR should be either the marker character, or the first character of the
3427 HTML tag associated with that emphasis. If CHAR is a space, the means
3428 to remove the emphasis of the selected region.
3429 If char is not given (for example in an interactive call) it
3430 will be prompted for."
3432 (let ((eal org-emphasis-alist) e det
3433 (erc org-emphasis-regexp-components)
3435 (string "") beg end move tag c s)
3436 (if (org-region-active-p)
3437 (setq beg (region-beginning) end (region-end)
3438 string (buffer-substring beg end))
3441 (while (setq e (pop eal))
3442 (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
3444 (push (cons c (string-to-char (car e))) det)
3445 (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
3446 (substring tag 1)))))
3448 (message "%s" (concat "Emphasis marker or tag:" prompt))
3449 (setq char (read-char-exclusive)))
3450 (setq char (or (cdr (assoc char det)) char))
3451 (if (equal char ?\ )
3452 (setq s "" move nil)
3453 (unless (assoc (char-to-string char) org-emphasis-alist)
3454 (error "No such emphasis marker: \"%c\"" char))
3455 (setq s (char-to-string char)))
3456 (while (and (> (length string) 1)
3457 (equal (substring string 0 1) (substring string -1))
3458 (assoc (substring string 0 1) org-emphasis-alist))
3459 (setq string (substring string 1 -1)))
3460 (setq string (concat s string s))
3461 (if beg (delete-region beg end))
3463 (string-match (concat "[" (nth 0 erc) "\n]")
3464 (char-to-string (char-before (point)))))
3466 (unless (string-match (concat "[" (nth 1 erc) "\n]")
3467 (char-to-string (char-after (point))))
3468 (insert " ") (backward-char 1))
3470 (and move (backward-char 1))))
3472 (defconst org-nonsticky-props
3473 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
3476 (defun org-activate-plain-links (limit)
3477 "Run through the buffer and add overlays to links."
3480 (while (re-search-forward org-plain-link-re limit t)
3481 (setq f (get-text-property (match-beginning 0) 'face))
3482 (if (or (eq f 'org-tag)
3483 (and (listp f) (memq 'org-tag f)))
3485 (add-text-properties (match-beginning 0) (match-end 0)
3486 (list 'mouse-face 'highlight
3487 'rear-nonsticky org-nonsticky-props
3488 'keymap org-mouse-map
3490 (throw 'exit t))))))
3492 (defun org-activate-code (limit)
3493 (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
3494 (unless (get-text-property (match-beginning 1) 'face)
3495 (remove-text-properties (match-beginning 0) (match-end 0)
3496 '(display t invisible t intangible t))
3499 (defun org-activate-angle-links (limit)
3500 "Run through the buffer and add overlays to links."
3501 (if (re-search-forward org-angle-link-re limit t)
3503 (add-text-properties (match-beginning 0) (match-end 0)
3504 (list 'mouse-face 'highlight
3505 'rear-nonsticky org-nonsticky-props
3506 'keymap org-mouse-map
3510 (defun org-activate-bracket-links (limit)
3511 "Run through the buffer and add overlays to bracketed links."
3512 (if (re-search-forward org-bracket-link-regexp limit t)
3513 (let* ((help (concat "LINK: "
3514 (org-match-string-no-properties 1)))
3515 ;; FIXME: above we should remove the escapes.
3516 ;; but that requires another match, protecting match data,
3517 ;; a lot of overhead for font-lock.
3518 (ip (org-maybe-intangible
3519 (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
3520 'keymap org-mouse-map 'mouse-face 'highlight
3521 'font-lock-multiline t 'help-echo help)))
3522 (vp (list 'rear-nonsticky org-nonsticky-props
3523 'keymap org-mouse-map 'mouse-face 'highlight
3524 ' font-lock-multiline t 'help-echo help)))
3525 ;; We need to remove the invisible property here. Table narrowing
3526 ;; may have made some of this invisible.
3527 (remove-text-properties (match-beginning 0) (match-end 0)
3531 (add-text-properties (match-beginning 0) (match-beginning 3) ip)
3532 (add-text-properties (match-beginning 3) (match-end 3) vp)
3533 (add-text-properties (match-end 3) (match-end 0) ip))
3534 (add-text-properties (match-beginning 0) (match-beginning 1) ip)
3535 (add-text-properties (match-beginning 1) (match-end 1) vp)
3536 (add-text-properties (match-end 1) (match-end 0) ip))
3539 (defun org-activate-dates (limit)
3540 "Run through the buffer and add overlays to dates."
3541 (if (re-search-forward org-tsr-regexp-both limit t)
3543 (add-text-properties (match-beginning 0) (match-end 0)
3544 (list 'mouse-face 'highlight
3545 'rear-nonsticky org-nonsticky-props
3546 'keymap org-mouse-map))
3547 (when org-display-custom-times
3549 (org-display-custom-time (match-beginning 3) (match-end 3)))
3550 (org-display-custom-time (match-beginning 1) (match-end 1)))
3553 (defvar org-target-link-regexp nil
3554 "Regular expression matching radio targets in plain text.")
3555 (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
3556 "Regular expression matching a link target.")
3557 (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
3558 "Regular expression matching a radio target.")
3559 (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
3560 "Regular expression matching any target.")
3562 (defun org-activate-target-links (limit)
3563 "Run through the buffer and add overlays to target matches."
3564 (when org-target-link-regexp
3565 (let ((case-fold-search t))
3566 (if (re-search-forward org-target-link-regexp limit t)
3568 (add-text-properties (match-beginning 0) (match-end 0)
3569 (list 'mouse-face 'highlight
3570 'rear-nonsticky org-nonsticky-props
3571 'keymap org-mouse-map
3572 'help-echo "Radio target link"
3573 'org-linked-text t))
3576 (defun org-update-radio-target-regexp ()
3577 "Find all radio targets in this file and update the regular expression."
3579 (when (memq 'radio org-activate-links)
3580 (setq org-target-link-regexp
3581 (org-make-target-link-regexp (org-all-targets 'radio)))
3582 (org-restart-font-lock)))
3584 (defun org-hide-wide-columns (limit)
3586 (setq s (text-property-any (point) (or limit (point-max))
3589 (setq e (next-single-property-change s 'org-cwidth))
3590 (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
3594 (defvar org-latex-and-specials-regexp nil
3595 "Regular expression for highlighting export special stuff.")
3596 (defvar org-match-substring-regexp)
3597 (defvar org-match-substring-with-braces-regexp)
3598 (defvar org-export-html-special-string-regexps)
3600 (defun org-compute-latex-and-specials-regexp ()
3601 "Compute regular expression for stuff treated specially by exporters."
3602 (if (not org-highlight-latex-fragments-and-specials)
3603 (org-set-local 'org-latex-and-specials-regexp nil)
3606 ((matchers (plist-get org-format-latex-options :matchers))
3607 (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
3608 org-latex-regexps)))
3609 (options (org-combine-plists (org-default-export-plist)
3610 (org-infile-export-plist)))
3611 (org-export-with-sub-superscripts (plist-get options :sub-superscript))
3612 (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
3613 (org-export-with-TeX-macros (plist-get options :TeX-macros))
3614 (org-export-html-expand (plist-get options :expand-quoted-html))
3615 (org-export-with-special-strings (plist-get options :special-strings))
3618 ((equal org-export-with-sub-superscripts '{})
3619 (list org-match-substring-with-braces-regexp))
3620 (org-export-with-sub-superscripts
3621 (list org-match-substring-regexp))
3624 (if org-export-with-LaTeX-fragments
3625 (mapcar (lambda (x) (nth 1 x)) latexs)))
3627 (if org-export-with-TeX-macros
3628 (list (concat "\\\\"
3630 (append (mapcar 'car org-html-entities)
3631 (if (boundp 'org-latex-entities)
3632 org-latex-entities nil))
3635 ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
3636 (re-special (if org-export-with-special-strings
3637 (mapcar (lambda (x) (car x))
3638 org-export-html-special-string-regexps)))
3642 (if org-export-html-expand "@<[^>\n]+>")
3645 'org-latex-and-specials-regexp
3646 (mapconcat 'identity (append re-latex re-sub re-macros re-special
3649 (defun org-do-latex-and-special-faces (limit)
3650 "Run through the buffer and add overlays to links."
3651 (when org-latex-and-specials-regexp
3653 (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
3655 (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
3657 '(org-code org-verbatim underline)))
3660 d (cond ((member (char-after (1+ (match-beginning 0)))
3663 (font-lock-prepend-text-property
3664 (+ d (match-beginning 0)) (match-end 0)
3665 'face 'org-latex-and-export-specials)
3666 (add-text-properties (+ d (match-beginning 0)) (match-end 0)
3667 '(font-lock-multiline t)))))
3670 (defun org-restart-font-lock ()
3671 "Restart font-lock-mode, to force refontification."
3672 (when (and (boundp 'font-lock-mode) font-lock-mode)
3674 (font-lock-mode 1)))
3676 (defun org-all-targets (&optional radio)
3677 "Return a list of all targets in this file.
3678 With optional argument RADIO, only find radio targets."
3679 (let ((re (if radio org-radio-target-regexp org-target-regexp))
3682 (goto-char (point-min))
3683 (while (re-search-forward re nil t)
3684 (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
3687 (defun org-make-target-link-regexp (targets)
3688 "Make regular expression matching all strings in TARGETS.
3689 The regular expression finds the targets also if there is a line break
3696 (while (string-match " +" x)
3697 (setq x (replace-match "\\s-+" t t x)))
3703 (defun org-activate-tags (limit)
3704 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
3706 (add-text-properties (match-beginning 1) (match-end 1)
3707 (list 'mouse-face 'highlight
3708 'rear-nonsticky org-nonsticky-props
3709 'keymap org-mouse-map))
3712 (defun org-outline-level ()
3714 (looking-at outline-regexp)
3715 (if (match-beginning 1)
3716 (+ (org-get-string-indentation (match-string 1)) 1000)
3717 (1- (- (match-end 0) (match-beginning 0))))))
3719 (defvar org-font-lock-keywords nil)
3721 (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
3722 "Regular expression matching a property line.")
3724 (defvar org-font-lock-hook nil
3725 "Functions to be called for special font lock stuff.")
3727 (defun org-font-lock-hook (limit)
3728 (run-hook-with-args 'org-font-lock-hook limit))
3730 (defun org-set-font-lock-defaults ()
3731 (let* ((em org-fontify-emphasized-text)
3732 (lk org-activate-links)
3733 (org-font-lock-extra-keywords
3736 '(org-font-lock-hook)
3738 '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
3739 (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
3741 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
3744 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
3745 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
3746 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
3748 (list org-drawer-regexp '(0 'org-special-keyword t))
3749 (list "^[ \t]*:END:" '(0 'org-special-keyword t))
3751 (list org-property-re
3752 '(1 'org-special-keyword t)
3753 '(3 'org-property-value t))
3754 (if org-format-transports-properties-p
3755 '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
3757 (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
3758 (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
3759 (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
3760 (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
3761 (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
3762 (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
3763 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
3764 '(org-hide-wide-columns (0 nil append))
3766 (list (concat "^\\*+[ \t]+" org-todo-regexp)
3767 '(1 (org-get-todo-face 1) t))
3769 (if org-fontify-done-headline
3770 (list (concat "^[*]+ +\\<\\("
3771 (mapconcat 'regexp-quote org-done-keywords "\\|")
3773 '(2 'org-headline-done t))
3776 (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
3778 (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
3779 (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
3780 (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
3781 (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
3784 (if (featurep 'xemacs)
3785 '(org-do-emphasis-faces (0 nil append))
3786 '(org-do-emphasis-faces)))
3788 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
3790 (if org-provide-checkbox-statistics
3791 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
3792 (0 (org-get-checkbox-statistics-face) t)))
3793 ;; Description list items
3794 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
3796 (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
3797 '(1 'org-archived prepend))
3799 '(org-do-latex-and-special-faces)
3801 '(org-activate-code (1 'org-code t))
3803 (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
3804 "\\|" org-quote-string "\\)\\>")
3805 '(1 'org-special-keyword t))
3806 '("^#.*" (0 'font-lock-comment-face t))
3808 (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
3809 ;; Now set the full font-lock-keywords
3810 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
3811 (org-set-local 'font-lock-defaults
3812 '(org-font-lock-keywords t nil nil backward-paragraph))
3813 (kill-local-variable 'font-lock-keywords) nil))
3818 (defun org-get-level-face (n)
3819 "Get the right face for match N in font-lock matching of healdines."
3820 (setq org-l (- (match-end 2) (match-beginning 1) 1))
3821 (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
3822 (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
3824 ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
3826 (t (if org-level-color-stars-only nil org-f))))
3828 (defun org-get-todo-face (kwd)
3829 "Get the right face for a TODO keyword KWD.
3830 If KWD is a number, get the corresponding match group."
3831 (if (numberp kwd) (setq kwd (match-string kwd)))
3832 (or (cdr (assoc kwd org-todo-keyword-faces))
3833 (and (member kwd org-done-keywords) 'org-done)
3836 (defun org-unfontify-region (beg end &optional maybe_loudly)
3837 "Remove fontification and activation overlays from links."
3838 (font-lock-default-unfontify-region beg end)
3839 (let* ((buffer-undo-list t)
3840 (inhibit-read-only t) (inhibit-point-motion-hooks t)
3841 (inhibit-modification-hooks t)
3842 deactivate-mark buffer-file-name buffer-file-truename)
3843 (remove-text-properties beg end
3844 '(mouse-face t keymap t org-linked-text t
3845 invisible t intangible t))))
3847 ;;;; Visibility cycling, including org-goto and indirect buffer
3851 (defvar org-cycle-global-status nil)
3852 (make-variable-buffer-local 'org-cycle-global-status)
3853 (defvar org-cycle-subtree-status nil)
3854 (make-variable-buffer-local 'org-cycle-subtree-status)
3857 (defun org-cycle (&optional arg)
3858 "Visibility cycling for Org-mode.
3860 - When this function is called with a prefix argument, rotate the entire
3861 buffer through 3 states (global cycling)
3862 1. OVERVIEW: Show only top-level headlines.
3863 2. CONTENTS: Show all headlines of all levels, but no body text.
3864 3. SHOW ALL: Show everything.
3865 When called with two C-c C-u prefixes, switch to the startup visibility,
3866 determined by the variable `org-startup-folded', and by any VISIBILITY
3867 properties in the buffer.
3869 - When point is at the beginning of a headline, rotate the subtree started
3870 by this line through 3 different states (local cycling)
3871 1. FOLDED: Only the main headline is shown.
3872 2. CHILDREN: The main headline and the direct children are shown.
3873 From this state, you can move to one of the children
3874 and zoom in further.
3875 3. SUBTREE: Show the entire subtree, including body text.
3877 - When there is a numeric prefix, go up to a heading with level ARG, do
3878 a `show-subtree' and return to the previous cursor position. If ARG
3879 is negative, go up that many levels.
3881 - When point is not at the beginning of a headline, execute the global
3882 binding for TAB, which is re-indenting the line. See the option
3883 `org-cycle-emulate-tab' for details.
3885 - Special case: if point is at the beginning of the buffer and there is
3886 no headline in line 1, this function will act as if called with prefix arg.
3887 But only if also the variable `org-cycle-global-at-bob' is t."
3889 (org-load-modules-maybe)
3890 (let* ((outline-regexp
3891 (if (and (org-mode-p) org-cycle-include-plain-lists)
3892 "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
3894 (bob-special (and org-cycle-global-at-bob (bobp)
3895 (not (looking-at outline-regexp))))
3898 (delq 'org-optimize-window-after-visibility-change
3899 (copy-sequence org-cycle-hook))
3903 (if (or bob-special (equal arg '(4)))
3904 ;; special case: use global cycling
3910 (org-set-startup-visibility)
3911 (message "Startup visibility, plus VISIBILITY properties."))
3913 ((org-at-table-p 'any)
3914 ;; Enter the table or move to the next field in the table
3915 (or (org-table-recognize-table.el)
3917 (if arg (org-table-edit-field t)
3918 (org-table-justify-field-maybe)
3919 (call-interactively 'org-table-next-field)))))
3921 ((eq arg t) ;; Global cycling
3924 ((and (eq last-command this-command)
3925 (eq org-cycle-global-status 'overview))
3926 ;; We just created the overview - now do table of contents
3927 ;; This can be slow in very large buffers, so indicate action
3928 (message "CONTENTS...")
3930 (message "CONTENTS...done")
3931 (setq org-cycle-global-status 'contents)
3932 (run-hook-with-args 'org-cycle-hook 'contents))
3934 ((and (eq last-command this-command)
3935 (eq org-cycle-global-status 'contents))
3936 ;; We just showed the table of contents - now show everything
3938 (message "SHOW ALL")
3939 (setq org-cycle-global-status 'all)
3940 (run-hook-with-args 'org-cycle-hook 'all))
3943 ;; Default action: go to overview
3945 (message "OVERVIEW")
3946 (setq org-cycle-global-status 'overview)
3947 (run-hook-with-args 'org-cycle-hook 'overview))))
3949 ((and org-drawers org-drawer-regexp
3951 (beginning-of-line 1)
3952 (looking-at org-drawer-regexp)))
3953 ;; Toggle block visibility
3955 (not (get-char-property (match-end 0) 'invisible))))
3958 ;; Show-subtree, ARG levels up from here.
3960 (org-back-to-heading)
3961 (outline-up-heading (if (< arg 0) (- arg)
3962 (- (funcall outline-level) arg)))
3963 (org-show-subtree)))
3965 ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
3966 (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
3967 ;; At a heading: rotate between three different views
3968 (org-back-to-heading)
3969 (let ((goal-column 0) eoh eol eos)
3970 ;; First, some boundaries
3972 (org-back-to-heading)
3974 (beginning-of-line 2)
3975 (while (and (not (eobp)) ;; this is like `next-line'
3976 (get-char-property (1- (point)) 'invisible))
3977 (beginning-of-line 2)) (setq eol (point)))
3978 (outline-end-of-heading) (setq eoh (point))
3979 (org-end-of-subtree t)
3981 (skip-chars-forward " \t\n")
3982 (beginning-of-line 1) ; in case this is an item
3984 (setq eos (1- (point))))
3985 ;; Find out what to do next and set `this-command'
3988 ;; Nothing is hidden behind this heading
3989 (message "EMPTY ENTRY")
3990 (setq org-cycle-subtree-status nil)
3993 (outline-next-heading)
3994 (if (org-invisible-p) (org-flag-heading nil))))
3996 (not (string-match "\\S-" (buffer-substring eol eos))))
3997 ;; Entire subtree is hidden in one line: open it
4000 (message "CHILDREN")
4003 (outline-next-heading)
4004 (if (org-invisible-p) (org-flag-heading nil)))
4005 (setq org-cycle-subtree-status 'children)
4006 (run-hook-with-args 'org-cycle-hook 'children))
4007 ((and (eq last-command this-command)
4008 (eq org-cycle-subtree-status 'children))
4009 ;; We just showed the children, now show everything.
4012 (setq org-cycle-subtree-status 'subtree)
4013 (run-hook-with-args 'org-cycle-hook 'subtree))
4015 ;; Default action: hide the subtree.
4018 (setq org-cycle-subtree-status 'folded)
4019 (run-hook-with-args 'org-cycle-hook 'folded)))))
4021 ;; TAB emulation and template completion
4022 (buffer-read-only (org-back-to-heading))
4024 ((org-try-structure-completion))
4026 ((org-try-cdlatex-tab))
4028 ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
4030 (not (looking-at outline-regexp))))
4031 (call-interactively (global-key-binding "\t")))
4033 ((if (and (memq org-cycle-emulate-tab '(white whitestart))
4034 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
4035 (or (and (eq org-cycle-emulate-tab 'white)
4036 (= (match-end 0) (point-at-eol)))
4037 (and (eq org-cycle-emulate-tab 'whitestart)
4038 (>= (match-end 0) pos))))
4040 (eq org-cycle-emulate-tab t))
4041 (call-interactively (global-key-binding "\t")))
4044 (org-back-to-heading)
4048 (defun org-global-cycle (&optional arg)
4049 "Cycle the global visibility. For details see `org-cycle'.
4050 With C-u prefix arg, switch to startup visibility.
4051 With a numeric prefix, show all headlines up to that level."
4053 (let ((org-cycle-include-plain-lists
4054 (if (org-mode-p) org-cycle-include-plain-lists nil)))
4058 (hide-sublevels arg)
4059 (setq org-cycle-global-status 'contents))
4061 (org-set-startup-visibility)
4062 (message "Startup visibility, plus VISIBILITY properties."))
4064 (org-cycle '(4))))))
4066 (defun org-set-startup-visibility ()
4067 "Set the visibility required by startup options and properties."
4069 ((eq org-startup-folded t)
4071 ((eq org-startup-folded 'content)
4072 (let ((this-command 'org-cycle) (last-command 'org-cycle))
4073 (org-cycle '(4)) (org-cycle '(4)))))
4074 (org-set-visibility-according-to-property 'no-cleanup)
4075 (org-cycle-hide-archived-subtrees 'all)
4076 (org-cycle-hide-drawers 'all)
4077 (org-cycle-show-empty-lines 'all))
4079 (defun org-set-visibility-according-to-property (&optional no-cleanup)
4080 "Switch subtree visibilities according to :VISIBILITY: property."
4084 (goto-char (point-min))
4085 (while (re-search-forward
4086 "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
4088 (setq state (match-string 1))
4090 (org-back-to-heading t)
4094 ((equal state '("fold" "folded"))
4096 ((equal state "children")
4097 (org-show-hidden-entry)
4099 ((equal state "content")
4102 (org-narrow-to-subtree)
4104 ((member state '("all" "showall"))
4107 (org-cycle-hide-archived-subtrees 'all)
4108 (org-cycle-hide-drawers 'all)
4109 (org-cycle-show-empty-lines 'all)))))
4111 (defun org-overview ()
4112 "Switch to overview mode, shoing only top-level headlines.
4113 Really, this shows all headlines with level equal or greater than the level
4114 of the first headline in the buffer. This is important, because if the
4115 first headline is not level one, then (hide-sublevels 1) gives confusing
4118 (let ((level (save-excursion
4119 (goto-char (point-min))
4120 (if (re-search-forward (concat "^" outline-regexp) nil t)
4122 (goto-char (match-beginning 0))
4123 (funcall outline-level))))))
4124 (and level (hide-sublevels level))))
4126 (defun org-content (&optional arg)
4127 "Show all headlines in the buffer, like a table of contents.
4128 With numerical argument N, show content up to level N."
4131 ;; Visit all headings and show their offspring
4132 (and (integerp arg) (org-overview))
4133 (goto-char (point-max))
4135 (while (and (progn (condition-case nil
4136 (outline-previous-visible-heading 1)
4137 (error (goto-char (point-min))))
4139 (looking-at outline-regexp))
4141 (show-children (1- arg))
4143 (if (bobp) (throw 'exit nil))))))
4146 (defun org-optimize-window-after-visibility-change (state)
4147 "Adjust the window after a change in outline visibility.
4148 This function is the default value of the hook `org-cycle-hook'."
4149 (when (get-buffer-window (current-buffer))
4151 ; ((eq state 'overview) (org-first-headline-recenter 1))
4152 ; ((eq state 'overview) (org-beginning-of-line))
4153 ((eq state 'content) nil)
4154 ((eq state 'all) nil)
4155 ((eq state 'folded) nil)
4156 ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
4157 ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
4159 (defun org-compact-display-after-subtree-move ()
4162 (if (org-up-heading-safe)
4167 (org-cycle-show-empty-lines 'children)
4168 (org-cycle-hide-drawers 'children))
4171 (defun org-cycle-show-empty-lines (state)
4172 "Show empty lines above all visible headlines.
4173 The region to be covered depends on STATE when called through
4174 `org-cycle-hook'. Lisp program can use t for STATE to get the
4175 entire buffer covered. Note that an empty line is only shown if there
4176 are at least `org-cycle-separator-lines' empty lines before the headeline."
4177 (when (> org-cycle-separator-lines 0)
4179 (let* ((n org-cycle-separator-lines)
4181 ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
4182 ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
4183 (t (let ((ns (number-to-string (- n 2))))
4184 (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
4185 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
4188 ((memq state '(overview contents t))
4189 (setq beg (point-min) end (point-max)))
4190 ((memq state '(children folded))
4191 (setq beg (point) end (progn (org-end-of-subtree t t)
4192 (beginning-of-line 2)
4196 (while (re-search-forward re end t)
4197 (if (not (get-char-property (match-end 1) 'invisible))
4198 (outline-flag-region
4199 (match-beginning 1) (match-end 1) nil)))))))
4200 ;; Never hide empty lines at the end of the file.
4202 (goto-char (point-max))
4203 (outline-previous-heading)
4204 (outline-end-of-heading)
4205 (if (and (looking-at "[ \t\n]+")
4206 (= (match-end 0) (point-max)))
4207 (outline-flag-region (point) (match-end 0) nil))))
4209 (defun org-cycle-hide-drawers (state)
4210 "Re-hide all drawers after a visibility state change."
4211 (when (and (org-mode-p)
4212 (not (memq state '(overview folded))))
4214 (let* ((globalp (memq state '(contents all)))
4215 (beg (if globalp (point-min) (point)))
4216 (end (if globalp (point-max) (org-end-of-subtree t))))
4218 (while (re-search-forward org-drawer-regexp end t)
4219 (org-flag-drawer t))))))
4221 (defun org-flag-drawer (flag)
4223 (beginning-of-line 1)
4224 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
4225 (let ((b (match-end 0))
4226 (outline-regexp org-outline-regexp))
4227 (if (re-search-forward
4229 (save-excursion (outline-next-heading) (point)) t)
4230 (outline-flag-region b (point-at-eol) flag)
4231 (error ":END: line missing"))))))
4233 (defun org-subtree-end-visible-p ()
4234 "Is the end of the current subtree visible?"
4235 (pos-visible-in-window-p
4236 (save-excursion (org-end-of-subtree t) (point))))
4238 (defun org-first-headline-recenter (&optional N)
4239 "Move cursor to the first headline and recenter the headline.
4240 Optional argument N means, put the headline into the Nth line of the window."
4241 (goto-char (point-min))
4242 (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
4244 (recenter (prefix-numeric-value N))))
4248 (defvar org-goto-window-configuration nil)
4249 (defvar org-goto-marker nil)
4250 (defvar org-goto-map
4251 (let ((map (make-sparse-keymap)))
4252 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
4253 (while (setq cmd (pop cmds))
4254 (substitute-key-definition cmd cmd map global-map)))
4255 (suppress-keymap map)
4256 (org-defkey map "\C-m" 'org-goto-ret)
4257 (org-defkey map [(return)] 'org-goto-ret)
4258 (org-defkey map [(left)] 'org-goto-left)
4259 (org-defkey map [(right)] 'org-goto-right)
4260 (org-defkey map [(control ?g)] 'org-goto-quit)
4261 (org-defkey map "\C-i" 'org-cycle)
4262 (org-defkey map [(tab)] 'org-cycle)
4263 (org-defkey map [(down)] 'outline-next-visible-heading)
4264 (org-defkey map [(up)] 'outline-previous-visible-heading)
4265 (if org-goto-auto-isearch
4266 (if (fboundp 'define-key-after)
4267 (define-key-after map [t] 'org-goto-local-auto-isearch)
4269 (org-defkey map "q" 'org-goto-quit)
4270 (org-defkey map "n" 'outline-next-visible-heading)
4271 (org-defkey map "p" 'outline-previous-visible-heading)
4272 (org-defkey map "f" 'outline-forward-same-level)
4273 (org-defkey map "b" 'outline-backward-same-level)
4274 (org-defkey map "u" 'outline-up-heading))
4275 (org-defkey map "/" 'org-occur)
4276 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
4277 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
4278 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
4279 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
4280 (org-defkey map "\C-c\C-u" 'outline-up-heading)
4283 (defconst org-goto-help
4284 "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
4285 RET=jump to location [Q]uit and return to previous location
4286 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
4288 (defvar org-goto-start-pos) ; dynamically scoped parameter
4290 ;; FIXME: Docstring doe not mention both interfaces
4291 (defun org-goto (&optional alternative-interface)
4292 "Look up a different location in the current file, keeping current visibility.
4294 When you want look-up or go to a different location in a document, the
4295 fastest way is often to fold the entire buffer and then dive into the tree.
4296 This method has the disadvantage, that the previous location will be folded,
4297 which may not be what you want.
4299 This command works around this by showing a copy of the current buffer
4300 in an indirect buffer, in overview mode. You can dive into the tree in
4301 that copy, use org-occur and incremental search to find a location.
4302 When pressing RET or `Q', the command returns to the original buffer in
4303 which the visibility is still unchanged. After RET is will also jump to
4304 the location selected in the indirect buffer and expose the
4305 the headline hierarchy above."
4307 (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
4308 (org-refile-use-outline-path t)
4310 (if (not alternative-interface)
4312 (if (eq org-goto-interface 'outline)
4313 'outline-path-completion
4315 (org-goto-start-pos (point))
4317 (if (eq interface 'outline)
4318 (car (org-get-location (current-buffer) org-goto-help))
4319 (nth 3 (org-refile-get-location "Goto: ")))))
4322 (org-mark-ring-push org-goto-start-pos)
4323 (goto-char selected-point)
4324 (if (or (org-invisible-p) (org-invisible-p2))
4325 (org-show-context 'org-goto)))
4328 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
4329 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
4330 (defvar org-goto-local-auto-isearch-map) ; defined below
4332 (defun org-get-location (buf help)
4333 "Let the user select a location in the Org-mode buffer BUF.
4334 This function uses a recursive edit. It returns the selected position
4336 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
4337 (isearch-hide-immediately nil)
4338 (isearch-search-fun-function
4339 (lambda () 'org-goto-local-search-forward-headings))
4340 (org-goto-selected-point org-goto-exit-command))
4342 (save-window-excursion
4343 (delete-other-windows)
4344 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
4347 (make-indirect-buffer (current-buffer) "*org-goto*")
4348 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
4349 (with-output-to-temp-buffer "*Help*"
4351 (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
4352 (setq buffer-read-only nil)
4353 (let ((org-startup-truncated t)
4354 (org-startup-folded nil)
4355 (org-startup-align-all-tables nil))
4358 (setq buffer-read-only t)
4359 (if (and (boundp 'org-goto-start-pos)
4360 (integer-or-marker-p org-goto-start-pos))
4361 (let ((org-show-hierarchy-above t)
4362 (org-show-siblings t)
4363 (org-show-following-heading t))
4364 (goto-char org-goto-start-pos)
4365 (and (org-invisible-p) (org-show-context)))
4366 (goto-char (point-min)))
4367 (org-beginning-of-line)
4368 (message "Select location and press RET")
4369 (use-local-map org-goto-map)
4372 (kill-buffer "*org-goto*")
4373 (cons org-goto-selected-point org-goto-exit-command)))
4375 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
4376 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
4377 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
4378 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
4380 (defun org-goto-local-search-forward-headings (string bound noerror)
4381 "Search and make sure that anu matches are in headlines."
4383 (while (search-forward string bound noerror)
4384 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
4385 (and (member :headline context)
4386 (not (member :tags context))))
4387 (throw 'return (point))))))
4389 (defun org-goto-local-auto-isearch ()
4392 (goto-char (point-min))
4393 (let ((keys (this-command-keys)))
4394 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
4396 (isearch-process-search-char (string-to-char keys)))))
4398 (defun org-goto-ret (&optional arg)
4399 "Finish `org-goto' by going to the new location."
4401 (setq org-goto-selected-point (point)
4402 org-goto-exit-command 'return)
4405 (defun org-goto-left ()
4406 "Finish `org-goto' by going to the new location."
4408 (if (org-on-heading-p)
4410 (beginning-of-line 1)
4411 (setq org-goto-selected-point (point)
4412 org-goto-exit-command 'left)
4414 (error "Not on a heading")))
4416 (defun org-goto-right ()
4417 "Finish `org-goto' by going to the new location."
4419 (if (org-on-heading-p)
4421 (setq org-goto-selected-point (point)
4422 org-goto-exit-command 'right)
4424 (error "Not on a heading")))
4426 (defun org-goto-quit ()
4427 "Finish `org-goto' without cursor motion."
4429 (setq org-goto-selected-point nil)
4430 (setq org-goto-exit-command 'quit)
4433 ;;; Indirect buffer display of subtrees
4435 (defvar org-indirect-dedicated-frame nil
4436 "This is the frame being used for indirect tree display.")
4437 (defvar org-last-indirect-buffer nil)
4439 (defun org-tree-to-indirect-buffer (&optional arg)
4440 "Create indirect buffer and narrow it to current subtree.
4441 With numerical prefix ARG, go up to this level and then take that tree.
4442 If ARG is negative, go up that many levels.
4443 If `org-indirect-buffer-display' is not `new-frame', the command removes the
4444 indirect buffer previously made with this command, to avoid proliferation of
4445 indirect buffers. However, when you call the command with a `C-u' prefix, or
4446 when `org-indirect-buffer-display' is `new-frame', the last buffer
4447 is kept so that you can work with several indirect buffers at the same time.
4448 If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
4449 requests that a new frame be made for the new buffer, so that the dedicated
4450 frame is not changed."
4452 (let ((cbuf (current-buffer))
4453 (cwin (selected-window))
4455 beg end level heading ibuf)
4457 (org-back-to-heading t)
4459 (setq level (org-outline-level))
4460 (if (< arg 0) (setq arg (+ level arg)))
4461 (while (> (setq level (org-outline-level)) arg)
4462 (outline-up-heading 1 t)))
4464 heading (org-get-heading))
4465 (org-end-of-subtree t) (setq end (point)))
4466 (if (and (buffer-live-p org-last-indirect-buffer)
4467 (not (eq org-indirect-buffer-display 'new-frame))
4469 (kill-buffer org-last-indirect-buffer))
4470 (setq ibuf (org-get-indirect-buffer cbuf)
4471 org-last-indirect-buffer ibuf)
4473 ((or (eq org-indirect-buffer-display 'new-frame)
4474 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
4475 (select-frame (make-frame))
4476 (delete-other-windows)
4477 (switch-to-buffer ibuf)
4478 (org-set-frame-title heading))
4479 ((eq org-indirect-buffer-display 'dedicated-frame)
4481 (select-frame (or (and org-indirect-dedicated-frame
4482 (frame-live-p org-indirect-dedicated-frame)
4483 org-indirect-dedicated-frame)
4484 (setq org-indirect-dedicated-frame (make-frame)))))
4485 (delete-other-windows)
4486 (switch-to-buffer ibuf)
4487 (org-set-frame-title (concat "Indirect: " heading)))
4488 ((eq org-indirect-buffer-display 'current-window)
4489 (switch-to-buffer ibuf))
4490 ((eq org-indirect-buffer-display 'other-window)
4491 (pop-to-buffer ibuf))
4492 (t (error "Invalid value.")))
4493 (if (featurep 'xemacs)
4494 (save-excursion (org-mode) (turn-on-font-lock)))
4495 (narrow-to-region beg end)
4498 (and (window-live-p cwin) (select-window cwin))))
4500 (defun org-get-indirect-buffer (&optional buffer)
4501 (setq buffer (or buffer (current-buffer)))
4502 (let ((n 1) (base (buffer-name buffer)) bname)
4503 (while (buffer-live-p
4504 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
4507 (make-indirect-buffer buffer bname 'clone)
4508 (error (make-indirect-buffer buffer bname)))))
4510 (defun org-set-frame-title (title)
4511 "Set the title of the current frame to the string TITLE."
4512 ;; FIXME: how to name a single frame in XEmacs???
4513 (unless (featurep 'xemacs)
4514 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
4516 ;;;; Structure editing
4518 ;;; Inserting headlines
4520 (defun org-insert-heading (&optional force-heading)
4521 "Insert a new heading or item with same depth at point.
4522 If point is in a plain list and FORCE-HEADING is nil, create a new list item.
4523 If point is at the beginning of a headline, insert a sibling before the
4524 current headline. If point is not at the beginning, do not split the line,
4525 but create the new hedline after the current line."
4527 (if (= (buffer-size) 0)
4529 (when (or force-heading (not (org-insert-item)))
4530 (let* ((head (save-excursion
4533 (org-back-to-heading)
4536 (blank (cdr (assq 'heading org-blank-before-new-entry)))
4539 ((and (org-on-heading-p) (bolp)
4541 (save-excursion (backward-char 1) (not (org-invisible-p)))))
4542 ;; insert before the current line
4543 (open-line (if blank 2 1)))
4547 (backward-char 1) (not (org-invisible-p)))))
4548 ;; insert right here
4551 ;; in the middle of the line
4554 (org-get-alist-option org-M-RET-may-split-line 'headline))
4556 (if (org-on-heading-p)
4558 (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
4559 (setq tags (and (match-end 2) (match-string 2)))
4561 (delete-region (match-beginning 1) (match-end 1)))
4562 (setq pos (point-at-bol))
4563 (or split (end-of-line 1))
4564 (delete-horizontal-space)
4565 (newline (if blank 2 1))
4571 (org-set-tags nil 'align))))
4572 (or split (end-of-line 1))
4573 (newline (if blank 2 1))))))
4574 (insert head) (just-one-space)
4577 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
4578 (run-hooks 'org-insert-heading-hook)))))
4580 (defun org-get-heading (&optional no-tags)
4581 "Return the heading of the current entry, without the stars."
4583 (org-back-to-heading t)
4586 (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
4587 "\\*+[ \t]+\\([^\r\n]*\\)"))
4588 (match-string 1) "")))
4590 (defun org-insert-heading-after-current ()
4591 "Insert a new heading with same level as current, after current subtree."
4593 (org-back-to-heading)
4594 (org-insert-heading)
4595 (org-move-subtree-down)
4598 (defun org-insert-todo-heading (arg)
4599 "Insert a new heading with the same level and TODO state as current heading.
4600 If the heading has no TODO state, or if the state is DONE, use the first
4601 state (TODO by default). Also with prefix arg, force first state."
4603 (when (not (org-insert-item 'checkbox))
4604 (org-insert-heading)
4606 (org-back-to-heading)
4607 (outline-previous-heading)
4608 (looking-at org-todo-line-regexp))
4610 (not (match-beginning 2))
4611 (member (match-string 2) org-done-keywords))
4612 (insert (car org-todo-keywords-1) " ")
4613 (insert (match-string 2) " "))
4614 (when org-provide-todo-statistics
4615 (org-update-parent-todo-statistics))))
4617 (defun org-insert-subheading (arg)
4618 "Insert a new subheading and demote it.
4619 Works for outline headings and for plain lists alike."
4621 (org-insert-heading arg)
4623 ((org-on-heading-p) (org-do-demote))
4624 ((org-at-item-p) (org-indent-item 1))))
4626 (defun org-insert-todo-subheading (arg)
4627 "Insert a new subheading with TODO keyword or checkbox and demote it.
4628 Works for outline headings and for plain lists alike."
4630 (org-insert-todo-heading arg)
4632 ((org-on-heading-p) (org-do-demote))
4633 ((org-at-item-p) (org-indent-item 1))))
4635 ;;; Promotion and Demotion
4637 (defun org-promote-subtree ()
4638 "Promote the entire subtree.
4639 See also `org-promote'."
4642 (org-map-tree 'org-promote))
4643 (org-fix-position-after-promote))
4645 (defun org-demote-subtree ()
4646 "Demote the entire subtree. See `org-demote'.
4647 See also `org-promote'."
4650 (org-map-tree 'org-demote))
4651 (org-fix-position-after-promote))
4654 (defun org-do-promote ()
4655 "Promote the current heading higher up the tree.
4656 If the region is active in `transient-mark-mode', promote all headings
4660 (if (org-region-active-p)
4661 (org-map-region 'org-promote (region-beginning) (region-end))
4663 (org-fix-position-after-promote))
4665 (defun org-do-demote ()
4666 "Demote the current heading lower down the tree.
4667 If the region is active in `transient-mark-mode', demote all headings
4671 (if (org-region-active-p)
4672 (org-map-region 'org-demote (region-beginning) (region-end))
4674 (org-fix-position-after-promote))
4676 (defun org-fix-position-after-promote ()
4677 "Make sure that after pro/demotion cursor position is right."
4678 (let ((pos (point)))
4679 (when (save-excursion
4680 (beginning-of-line 1)
4681 (looking-at org-todo-line-regexp)
4682 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
4683 (cond ((eobp) (insert " "))
4684 ((eolp) (insert " "))
4685 ((equal (char-after) ?\ ) (forward-char 1))))))
4687 (defun org-reduced-level (l)
4688 (if org-odd-levels-only (1+ (floor (/ l 2))) l))
4690 (defun org-get-valid-level (level &optional change)
4691 "Rectify a level change under the influence of `org-odd-levels-only'
4692 LEVEL is a current level, CHANGE is by how much the level should be
4693 modified. Even if CHANGE is nil, LEVEL may be returned modified because
4694 even level numbers will become the next higher odd number."
4695 (if org-odd-levels-only
4696 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
4697 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
4698 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
4699 (max 1 (+ level change))))
4701 (if (boundp 'define-obsolete-function-alias)
4702 (if (or (featurep 'xemacs) (< emacs-major-version 23))
4703 (define-obsolete-function-alias 'org-get-legal-level
4704 'org-get-valid-level)
4705 (define-obsolete-function-alias 'org-get-legal-level
4706 'org-get-valid-level "23.1")))
4708 (defun org-promote ()
4709 "Promote the current heading higher up the tree.
4710 If the region is active in `transient-mark-mode', promote all headings
4712 (org-back-to-heading t)
4713 (let* ((level (save-match-data (funcall outline-level)))
4714 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
4715 (diff (abs (- level (length up-head) -1))))
4716 (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
4717 (replace-match up-head nil t)
4718 ;; Fixup tag positioning
4719 (and org-auto-align-tags (org-set-tags nil t))
4720 (if org-adapt-indentation (org-fixup-indentation (- diff)))))
4722 (defun org-demote ()
4723 "Demote the current heading lower down the tree.
4724 If the region is active in `transient-mark-mode', demote all headings
4726 (org-back-to-heading t)
4727 (let* ((level (save-match-data (funcall outline-level)))
4728 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
4729 (diff (abs (- level (length down-head) -1))))
4730 (replace-match down-head nil t)
4731 ;; Fixup tag positioning
4732 (and org-auto-align-tags (org-set-tags nil t))
4733 (if org-adapt-indentation (org-fixup-indentation diff))))
4735 (defun org-map-tree (fun)
4736 "Call FUN for every heading underneath the current one."
4737 (org-back-to-heading)
4738 (let ((level (funcall outline-level)))
4742 (outline-next-heading)
4743 (> (funcall outline-level) level))
4747 (defun org-map-region (fun beg end)
4748 "Call FUN for every heading between BEG and END."
4749 (let ((org-ignore-region t))
4751 (setq end (copy-marker end))
4753 (if (and (re-search-forward (concat "^" outline-regexp) nil t)
4757 (outline-next-heading)
4762 (defun org-fixup-indentation (diff)
4763 "Change the indentation in the current entry by DIFF
4764 However, if any line in the current entry has no indentation, or if it
4765 would end up with no indentation after the change, nothing at all is done."
4767 (let ((end (save-excursion (outline-next-heading)
4769 (prohibit (if (> diff 0)
4771 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
4773 (unless (save-excursion (end-of-line 1)
4774 (re-search-forward prohibit end t))
4775 (while (and (< (point) end)
4776 (re-search-forward "^[ \t]+" end t))
4777 (goto-char (match-end 0))
4778 (setq col (current-column))
4779 (if (< diff 0) (replace-match ""))
4780 (indent-to (+ diff col))))
4781 (move-marker end nil))))
4783 (defun org-convert-to-odd-levels ()
4784 "Convert an org-mode file with all levels allowed to one with odd levels.
4785 This will leave level 1 alone, convert level 2 to level 3, level 3 to
4788 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
4789 (let ((org-odd-levels-only nil) n)
4791 (goto-char (point-min))
4792 (while (re-search-forward "^\\*\\*+ " nil t)
4793 (setq n (- (length (match-string 0)) 2))
4794 (while (>= (setq n (1- n)) 0)
4796 (end-of-line 1))))))
4799 (defun org-convert-to-oddeven-levels ()
4800 "Convert an org-mode file with only odd levels to one with odd and even levels.
4801 This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
4802 section with an even level, conversion would destroy the structure of the file. An error
4803 is signaled in this case."
4805 (goto-char (point-min))
4806 ;; First check if there are no even levels
4807 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
4808 (org-show-context t)
4809 (error "Not all levels are odd in this file. Conversion not possible."))
4810 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
4811 (let ((org-odd-levels-only nil) n)
4813 (goto-char (point-min))
4814 (while (re-search-forward "^\\*\\*+ " nil t)
4815 (setq n (/ (1- (length (match-string 0))) 2))
4816 (while (>= (setq n (1- n)) 0)
4818 (end-of-line 1))))))
4820 (defun org-tr-level (n)
4821 "Make N odd if required."
4822 (if org-odd-levels-only (1+ (/ n 2)) n))
4824 ;;; Vertical tree motion, cutting and pasting of subtrees
4826 (defun org-move-subtree-up (&optional arg)
4827 "Move the current subtree up past ARG headlines of the same level."
4829 (org-move-subtree-down (- (prefix-numeric-value arg))))
4831 (defun org-move-subtree-down (&optional arg)
4832 "Move the current subtree down past ARG headlines of the same level."
4834 (setq arg (prefix-numeric-value arg))
4835 (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
4836 'outline-get-last-sibling))
4837 (ins-point (make-marker))
4839 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
4841 (org-back-to-heading)
4844 (setq ne-beg (org-back-over-empty-lines))
4847 (save-excursion (outline-end-of-heading)
4848 (setq folded (org-invisible-p)))
4849 (outline-end-of-subtree))
4850 (outline-next-heading)
4851 (setq ne-end (org-back-over-empty-lines))
4854 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
4855 ;; include less whitespace
4858 (forward-line (- ne-beg ne-end))
4859 (setq beg (point))))
4860 ;; Find insertion point, with error handling
4862 (or (and (funcall movfunc) (looking-at outline-regexp))
4863 (progn (goto-char beg0)
4864 (error "Cannot move past superior level or buffer limit")))
4865 (setq cnt (1- cnt)))
4867 ;; Moving forward - still need to move over subtree
4868 (progn (org-end-of-subtree t t)
4870 (org-back-over-empty-lines)
4871 (or (bolp) (newline)))))
4872 (setq ne-ins (org-back-over-empty-lines))
4873 (move-marker ins-point (point))
4874 (setq txt (buffer-substring beg end))
4875 (org-save-markers-in-region beg end)
4876 (delete-region beg end)
4877 (outline-flag-region (1- beg) beg nil)
4878 (outline-flag-region (1- (point)) (point) nil)
4879 (let ((bbb (point)))
4880 (insert-before-markers txt)
4881 (org-reinstall-markers-in-region bbb)
4882 (move-marker ins-point bbb))
4883 (or (bolp) (insert "\n"))
4884 (setq ins-end (point))
4885 (goto-char ins-point)
4886 (org-skip-whitespace)
4887 (when (and (< arg 0)
4888 (org-first-sibling-p)
4890 ;; Move whitespace back to beginning
4893 (let ((kill-whole-line t))
4894 (kill-line (- ne-ins ne-beg)) (point)))
4895 (insert (make-string (- ne-ins ne-beg) ?\n)))
4896 (move-marker ins-point nil)
4897 (org-compact-display-after-subtree-move)
4901 (org-cycle-hide-drawers 'children))))
4903 (defvar org-subtree-clip ""
4904 "Clipboard for cut and paste of subtrees.
4905 This is actually only a copy of the kill, because we use the normal kill
4906 ring. We need it to check if the kill was created by `org-copy-subtree'.")
4908 (defvar org-subtree-clip-folded nil
4909 "Was the last copied subtree folded?
4910 This is used to fold the tree back after pasting.")
4912 (defun org-cut-subtree (&optional n)
4913 "Cut the current subtree into the clipboard.
4914 With prefix arg N, cut this many sequential subtrees.
4915 This is a short-hand for marking the subtree and then cutting it."
4917 (org-copy-subtree n 'cut))
4919 (defun org-copy-subtree (&optional n cut force-store-markers)
4920 "Cut the current subtree into the clipboard.
4921 With prefix arg N, cut this many sequential subtrees.
4922 This is a short-hand for marking the subtree and then copying it.
4923 If CUT is non-nil, actually cut the subtree.
4924 If FORCE-STORE-MARKERS is non-nil, store the relative locations
4925 of some markers in the region, even if CUT is non-nil. This is
4926 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
4928 (let (beg end folded (beg0 (point)))
4930 (org-back-to-heading nil) ; take what looks like a subtree
4931 (org-back-to-heading t)) ; take what is really there
4932 (org-back-over-empty-lines)
4934 (skip-chars-forward " \t\r\n")
4936 (save-excursion (outline-end-of-heading)
4937 (setq folded (org-invisible-p)))
4939 (outline-forward-same-level (1- n))
4941 (org-end-of-subtree t t))
4942 (org-back-over-empty-lines)
4946 (setq org-subtree-clip-folded folded)
4947 (when (or cut force-store-markers)
4948 (org-save-markers-in-region beg end))
4949 (if cut (kill-region beg end) (copy-region-as-kill beg end))
4950 (setq org-subtree-clip (current-kill 0))
4951 (message "%s: Subtree(s) with %d characters"
4952 (if cut "Cut" "Copied")
4953 (length org-subtree-clip)))))
4955 (defun org-paste-subtree (&optional level tree)
4956 "Paste the clipboard as a subtree, with modification of headline level.
4957 The entire subtree is promoted or demoted in order to match a new headline
4958 level. By default, the new level is derived from the visible headings
4959 before and after the insertion point, and taken to be the inferior headline
4960 level of the two. So if the previous visible heading is level 3 and the
4961 next is level 4 (or vice versa), level 4 will be used for insertion.
4962 This makes sure that the subtree remains an independent subtree and does
4963 not swallow low level entries.
4965 You can also force a different level, either by using a numeric prefix
4966 argument, or by inserting the heading marker by hand. For example, if the
4967 cursor is after \"*****\", then the tree will be shifted to level 5.
4969 If you want to insert the tree as is, just use \\[yank].
4971 If optional TREE is given, use this text instead of the kill ring."
4973 (unless (org-kill-is-subtree-p tree)
4975 (substitute-command-keys
4976 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
4977 (let* ((txt (or tree (and kill-ring (current-kill 0))))
4978 (^re (concat "^\\(" outline-regexp "\\)"))
4979 (re (concat "\\(" outline-regexp "\\)"))
4980 (^re_ (concat "\\(\\*+\\)[ \t]*"))
4982 (old-level (if (string-match ^re txt)
4983 (- (match-end 0) (match-beginning 0) 1)
4985 (force-level (cond (level (prefix-numeric-value level))
4987 ^re_ (buffer-substring (point-at-bol) (point)))
4988 (- (match-end 1) (match-beginning 1)))
4990 (previous-level (save-excursion
4993 (outline-previous-visible-heading 1)
4995 (- (match-end 0) (match-beginning 0) 1)
4998 (next-level (save-excursion
5001 (or (looking-at outline-regexp)
5002 (outline-next-visible-heading 1))
5004 (- (match-end 0) (match-beginning 0) 1)
5007 (new-level (or force-level (max previous-level next-level)))
5008 (shift (if (or (= old-level -1)
5010 (= old-level new-level))
5012 (- new-level old-level)))
5013 (delta (if (> shift 0) -1 1))
5014 (func (if (> shift 0) 'org-demote 'org-promote))
5015 (org-odd-levels-only nil)
5017 ;; Remove the forced level indicator
5019 (delete-region (point-at-bol) (point)))
5021 (beginning-of-line 1)
5022 (org-back-over-empty-lines)
5024 (insert-before-markers txt)
5025 (unless (string-match "\n\\'" txt) (insert "\n"))
5026 (org-reinstall-markers-in-region beg)
5029 (skip-chars-forward " \t\n\r")
5031 ;; Shift if necessary
5034 (narrow-to-region beg end)
5035 (while (not (= shift 0))
5036 (org-map-region func (point-min) (point-max))
5037 (setq shift (+ delta shift)))
5038 (goto-char (point-min))))
5039 (when (interactive-p)
5040 (message "Clipboard pasted as level %d subtree" new-level))
5042 (eq org-subtree-clip (current-kill 0))
5043 org-subtree-clip-folded)
5044 ;; The tree was folded before it was killed/copied
5047 (defun org-kill-is-subtree-p (&optional txt)
5048 "Check if the current kill is an outline subtree, or a set of trees.
5049 Returns nil if kill does not start with a headline, or if the first
5050 headline level is not the largest headline level in the tree.
5051 So this will actually accept several entries of equal levels as well,
5052 which is OK for `org-paste-subtree'.
5053 If optional TXT is given, check this string instead of the current kill."
5054 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
5055 (start-level (and kill
5056 (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
5057 org-outline-regexp "\\)")
5059 (- (match-end 2) (match-beginning 2) 1)))
5060 (re (concat "^" org-outline-regexp))
5061 (start (1+ (match-beginning 2))))
5062 (if (not start-level)
5064 nil) ;; does not even start with a heading
5066 (while (setq start (string-match re kill (1+ start)))
5067 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
5071 (defvar org-markers-to-move nil
5072 "Markers that should be moved with a cut-and-paste operation.
5073 Those markers are stored together with their positions relative to
5074 the start of the region.")
5076 (defun org-save-markers-in-region (beg end)
5077 "Check markers in region.
5078 If these markers are between BEG and END, record their position relative
5079 to BEG, so that after moving the block of text, we can put the markers back
5081 This function gets called just before an entry or tree gets cut from the
5082 buffer. After re-insertion, `org-reinstall-markers-in-region' must be
5083 called immediately, to move the markers with the entries."
5084 (setq org-markers-to-move nil)
5085 (when (featurep 'org-clock)
5086 (org-clock-save-markers-for-cut-and-paste beg end))
5087 (when (featurep 'org-agenda)
5088 (org-agenda-save-markers-for-cut-and-paste beg end)))
5090 (defun org-check-and-save-marker (marker beg end)
5091 "Check if MARKER is between BEG and END.
5092 If yes, remember the marker and the distance to BEG."
5093 (when (and (marker-buffer marker)
5094 (equal (marker-buffer marker) (current-buffer)))
5095 (if (and (>= marker beg) (< marker end))
5096 (push (cons marker (- marker beg)) org-markers-to-move))))
5098 (defun org-reinstall-markers-in-region (beg)
5099 "Move all remembered markers to their position relative to BEG."
5101 (move-marker (car x) (+ beg (cdr x))))
5102 org-markers-to-move)
5103 (setq org-markers-to-move nil))
5105 (defun org-narrow-to-subtree ()
5106 "Narrow buffer to the current subtree."
5111 (progn (org-back-to-heading) (point))
5112 (progn (org-end-of-subtree t t) (point))))))
5117 (defun org-sort (with-case)
5118 "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
5119 Optional argument WITH-CASE means sort case-sensitively."
5121 (if (org-at-table-p)
5122 (org-call-with-arg 'org-table-sort-lines with-case)
5123 (org-call-with-arg 'org-sort-entries-or-items with-case)))
5125 (defun org-sort-remove-invisible (s)
5126 (remove-text-properties 0 (length s) org-rm-props s)
5127 (while (string-match org-bracket-link-regexp s)
5128 (setq s (replace-match (if (match-end 2)
5130 (match-string 1 s)) t t s)))
5133 (defvar org-priority-regexp) ; defined later in the file
5135 (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
5136 "Sort entries on a certain level of an outline tree.
5137 If there is an active region, the entries in the region are sorted.
5138 Else, if the cursor is before the first entry, sort the top-level items.
5139 Else, the children of the entry at point are sorted.
5141 Sorting can be alphabetically, numerically, and by date/time as given by
5142 the first time stamp in the entry. The command prompts for the sorting
5143 type unless it has been given to the function through the SORTING-TYPE
5144 argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
5145 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
5146 called with point at the beginning of the record. It must return either
5147 a string or a number that should serve as the sorting key for that record.
5149 Comparing entries ignores case by default. However, with an optional argument
5150 WITH-CASE, the sorting considers case as well."
5152 (let ((case-func (if with-case 'identity 'downcase))
5153 start beg end stars re re2
5154 txt what tmp plain-list-p)
5155 ;; Find beginning and end of region to sort
5157 ((org-region-active-p)
5158 ;; we will sort the region
5159 (setq end (region-end)
5161 (goto-char (region-beginning))
5162 (if (not (org-on-heading-p)) (outline-next-heading))
5163 (setq start (point)))
5165 ;; we will sort this plain list
5166 (org-beginning-of-item-list) (setq start (point))
5167 (org-end-of-item-list) (setq end (point))
5169 (setq plain-list-p t
5171 ((or (org-on-heading-p)
5172 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
5173 ;; we will sort the children of the current headline
5174 (org-back-to-heading)
5176 end (progn (org-end-of-subtree t t)
5177 (org-back-over-empty-lines)
5182 (outline-next-heading))
5184 ;; we will sort the top-level entries in this file
5185 (goto-char (point-min))
5186 (or (org-on-heading-p) (outline-next-heading))
5187 (setq start (point) end (point-max) what "top-level")
5192 (if (>= beg end) (error "Nothing to sort"))
5194 (unless plain-list-p
5195 (looking-at "\\(\\*+\\)")
5196 (setq stars (match-string 1)
5197 re (concat "^" (regexp-quote stars) " +")
5198 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
5199 txt (buffer-substring beg end))
5200 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
5201 (if (and (not (equal stars "*")) (string-match re2 txt))
5202 (error "Region to sort contains a level above the first entry")))
5204 (unless sorting-type
5207 "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
5208 "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty todo[o]rder [f]unc A/N/T/P/O/F means reversed:")
5210 (setq sorting-type (read-char-exclusive))
5212 (and (= (downcase sorting-type) ?f)
5214 (completing-read "Sort using function: "
5215 obarray 'fboundp t nil nil))
5216 (setq getkey-func (intern getkey-func)))
5218 (and (= (downcase sorting-type) ?r)
5220 (completing-read "Property: "
5221 (mapcar 'list (org-buffer-property-keys t))
5224 (message "Sorting entries...")
5227 (narrow-to-region start end)
5229 (let ((dcst (downcase sorting-type))
5230 (now (current-time)))
5232 (/= dcst sorting-type)
5233 ;; This function moves to the beginning character of the "record" to
5237 (if (org-at-item-p) t (goto-char (point-max))))
5239 (if (re-search-forward re nil t)
5240 (goto-char (match-beginning 0))
5241 (goto-char (point-max)))))
5242 ;; This function moves to the last character of the "record" being
5249 (outline-forward-same-level 1)
5251 (goto-char (point-max)))))))
5253 ;; This function returns the value that gets sorted against.
5256 (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
5259 (string-to-number (buffer-substring (match-end 0)
5262 (buffer-substring (match-end 0) (point-at-eol)))
5264 (if (re-search-forward org-ts-regexp
5266 (org-time-string-to-time (match-string 0))
5271 (setq tmp (funcall getkey-func))
5272 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
5274 (error "Invalid key function `%s'" getkey-func)))
5275 (t (error "Invalid sorting type `%c'" sorting-type)))))
5279 (if (looking-at outline-regexp)
5280 (string-to-number (buffer-substring (match-end 0)
5284 (funcall case-func (buffer-substring (point-at-bol)
5287 (if (re-search-forward org-ts-regexp
5291 (org-time-string-to-time (match-string 0))
5294 (if (re-search-forward org-priority-regexp (point-at-eol) t)
5295 (string-to-char (match-string 2))
5296 org-default-priority))
5298 (or (org-entry-get nil property) ""))
5300 (if (looking-at org-complex-heading-regexp)
5301 (- 9999 (length (member (match-string 2)
5302 org-todo-keywords-1)))))
5306 (setq tmp (funcall getkey-func))
5307 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
5309 (error "Invalid key function `%s'" getkey-func)))
5310 (t (error "Invalid sorting type `%c'" sorting-type)))))
5313 ((= dcst ?a) 'string<)
5314 ((= dcst ?t) 'time-less-p)
5316 (message "Sorting entries...done")))
5318 (defun org-do-sort (table what &optional with-case sorting-type)
5319 "Sort TABLE of WHAT according to SORTING-TYPE.
5320 The user will be prompted for the SORTING-TYPE if the call to this
5321 function does not specify it. WHAT is only for the prompt, to indicate
5322 what is being sorted. The sorting key will be extracted from
5323 the car of the elements of the table.
5324 If WITH-CASE is non-nil, the sorting will be case-sensitive."
5325 (unless sorting-type
5327 "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
5329 (setq sorting-type (read-char-exclusive)))
5330 (let ((dcst (downcase sorting-type))
5331 extractfun comparefun)
5332 ;; Define the appropriate functions
5335 (setq extractfun 'string-to-number
5336 comparefun (if (= dcst sorting-type) '< '>)))
5338 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
5339 (lambda(x) (downcase (org-sort-remove-invisible x))))
5340 comparefun (if (= dcst sorting-type)
5342 (lambda (a b) (and (not (string< a b))
5343 (not (string= a b)))))))
5347 (if (string-match org-ts-regexp x)
5349 (org-time-string-to-time (match-string 0 x)))
5351 comparefun (if (= dcst sorting-type) '< '>)))
5352 (t (error "Invalid sorting type `%c'" sorting-type)))
5354 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
5356 (lambda (a b) (funcall comparefun (car a) (car b))))))
5358 ;;; Editing source examples
5360 (defvar org-exit-edit-mode-map (make-sparse-keymap))
5361 (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
5362 (defvar org-edit-src-force-single-line nil)
5363 (defvar org-edit-src-from-org-mode nil)
5365 (define-minor-mode org-exit-edit-mode
5366 "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
5368 (defun org-edit-src-code ()
5369 "Edit the source code example at point.
5370 An indirect buffer is created, and that buffer is then narrowed to the
5371 example at point and switched to the correct language mode. When done,
5372 exit by killing the buffer with \\[org-edit-src-exit]."
5374 (let ((line (org-current-line))
5375 (case-fold-search t)
5376 (msg (substitute-command-keys
5377 "Edit, then exit with C-c ' (C-c and single quote)"))
5378 (info (org-edit-src-find-region-and-lang))
5379 (org-mode-p (eq major-mode 'org-mode))
5380 beg end lang lang-f single)
5383 (setq beg (nth 0 info)
5387 lang-f (intern (concat lang "-mode")))
5388 (unless (functionp lang-f)
5389 (error "No such language mode: %s" lang-f))
5391 (if (get-buffer "*Org Edit Src Example*")
5392 (kill-buffer "*Org Edit Src Example*"))
5393 (switch-to-buffer (make-indirect-buffer (current-buffer)
5394 "*Org Edit Src Example*"))
5395 (narrow-to-region beg end)
5396 (remove-text-properties beg end '(display nil invisible nil
5398 (let ((org-inhibit-startup t))
5400 (set (make-local-variable 'org-edit-src-force-single-line) single)
5401 (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
5403 (goto-char (point-min))
5404 (while (re-search-forward "^," nil t)
5405 (replace-match "")))
5407 (org-exit-edit-mode)
5408 (org-set-local 'header-line-format msg)
5412 (defun org-edit-src-find-region-and-lang ()
5413 "Find the region and language for a local edit.
5414 Return a list with beginning and end of the region, a string representing
5415 the language, a switch telling of the content should be in a single line."
5418 ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
5419 ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
5420 ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
5421 ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
5422 ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
5423 ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
5424 ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
5425 ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
5426 ("^#\\+begin_example.*\n" "^#\\+end_example" "fundamental")
5427 ("^#\\+html:" "\n" "html" single-line)
5428 ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
5429 ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
5430 ("^#\\+latex:" "\n" "latex" single-line)
5431 ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
5432 ("^#\\+ascii:" "\n" "ascii" single-line)
5435 re re1 re2 single beg end lang)
5437 (while (setq entry (pop re-list))
5438 (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
5439 single (nth 3 entry))
5441 (if (or (looking-at re1)
5442 (re-search-backward re1 nil t))
5444 (setq beg (match-end 0) lang (org-edit-src-get-lang lang))
5445 (if (and (re-search-forward re2 nil t)
5446 (>= (match-end 0) pos))
5447 (throw 'exit (list beg (match-beginning 0) lang single))))
5448 (if (or (looking-at re2)
5449 (re-search-forward re2 nil t))
5451 (setq end (match-beginning 0))
5452 (if (and (re-search-backward re1 nil t)
5453 (<= (match-beginning 0) pos))
5455 (list (match-end 0) end
5456 (org-edit-src-get-lang lang) single)))))))))))
5458 (defun org-edit-src-get-lang (lang)
5459 "Extract the src language."
5460 (let ((m (match-string 0)))
5462 ((stringp lang) lang)
5463 ((integerp lang) (match-string lang))
5464 ((and (eq lang lang)
5465 (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
5467 ((and (eq lang lang)
5468 (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
5470 (t "fundamental"))))
5472 (defun org-edit-src-exit ()
5473 "Exit special edit and protect problematic lines."
5475 (unless (buffer-base-buffer (current-buffer))
5476 (error "This is not an indirect buffer, something is wrong..."))
5477 (unless (> (point-min) 1)
5478 (error "This buffer is not narrowed, something is wrong..."))
5479 (goto-char (point-min))
5480 (if (looking-at "[ \t\n]*\n") (replace-match ""))
5481 (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
5482 (when (org-bound-and-true-p org-edit-src-force-single-line)
5483 (goto-char (point-min))
5484 (while (re-search-forward "\n" nil t)
5485 (replace-match " "))
5486 (goto-char (point-min))
5487 (if (looking-at "\\s-*") (replace-match " "))
5488 (if (re-search-forward "\\s-+\\'" nil t)
5489 (replace-match "")))
5490 (when (org-bound-and-true-p org-edit-src-from-org-mode)
5491 (goto-char (point-min))
5492 (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
5493 (replace-match ",\\1"))
5494 (when font-lock-mode
5495 (font-lock-unfontify-region (point-min) (point-max)))
5496 (put-text-property (point-min) (point-max) 'font-lock-fontified t))
5497 (kill-buffer (current-buffer)))
5499 ;;;; Plain list items, including checkboxes
5501 ;;; Plain list items
5503 (defun org-at-item-p ()
5504 "Is point in a line starting a hand-formatted item?"
5505 (let ((llt org-plain-list-ordered-item-terminator))
5507 (goto-char (point-at-bol))
5510 ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
5511 ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
5512 ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
5513 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
5515 (defun org-in-item-p ()
5516 "It the cursor inside a plain list item.
5517 Does not have to be the first line."
5521 (org-beginning-of-item)
5526 (defun org-insert-item (&optional checkbox)
5527 "Insert a new item at the current level.
5528 Return t when things worked, nil when we are not in an item."
5529 (when (save-excursion
5532 (org-beginning-of-item)
5534 (if (org-invisible-p) (error "Invisible item"))
5537 (let* ((bul (match-string 0))
5538 (descp (save-excursion (goto-char (match-beginning 0))
5539 (beginning-of-line 1)
5541 (looking-at "[ \t]*.*? ::"))))
5542 (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
5544 (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
5546 (if descp (setq checkbox nil))
5548 ((and (org-at-item-p) (<= (point) eow))
5549 ;; before the bullet
5550 (beginning-of-line 1)
5551 (open-line (if blank 2 1)))
5553 (beginning-of-line 1))
5555 (unless (org-get-alist-option org-M-RET-may-split-line 'item)
5557 (delete-horizontal-space))
5558 (newline (if blank 2 1))))
5560 (if checkbox "[ ]" "")
5561 (if descp (concat (if checkbox " " "")
5562 (read-string "Term: ") " :: ") ""))
5566 (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
5567 (org-maybe-renumber-ordered-list)
5568 (and checkbox (org-update-checkbox-count-maybe))
5573 (defun org-at-item-checkbox-p ()
5574 "Is point at a line starting a plain-list item with a checklet?"
5575 (and (org-at-item-p)
5577 (goto-char (match-end 0))
5578 (skip-chars-forward " \t")
5579 (looking-at "\\[[- X]\\]"))))
5581 (defun org-toggle-checkbox (&optional arg)
5582 "Toggle the checkbox in the current line."
5585 (let (beg end status (firstnew 'unknown))
5587 ((org-region-active-p)
5588 (setq beg (region-beginning) end (region-end)))
5590 (setq beg (point) end (save-excursion (outline-next-heading) (point))))
5591 ((org-at-item-checkbox-p)
5592 (let ((pos (point)))
5595 ((member (match-string 0) '("[ ]" "[-]")) "[X]")
5600 (t (error "Not at a checkbox or heading, and no active region")))
5603 (while (< (point) end)
5604 (when (org-at-item-checkbox-p)
5605 (setq status (equal (match-string 0) "[X]"))
5606 (when (eq firstnew 'unknown)
5607 (setq firstnew (not status)))
5609 (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
5610 (beginning-of-line 2)))))
5611 (org-update-checkbox-count-maybe))
5613 (defun org-update-checkbox-count-maybe ()
5614 "Update checkbox statistics unless turned off by user."
5615 (when org-provide-checkbox-statistics
5616 (org-update-checkbox-count)))
5618 (defun org-update-checkbox-count (&optional all)
5619 "Update the checkbox statistics in the current section.
5620 This will find all statistic cookies like [57%] and [6/12] and update them
5621 with the current numbers. With optional prefix argument ALL, do this for
5625 (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
5626 (beg (condition-case nil
5627 (progn (outline-back-to-heading) (point))
5628 (error (point-min))))
5629 (end (move-marker (make-marker)
5630 (progn (outline-next-heading) (point))))
5631 (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
5632 (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
5633 (re-find (concat re "\\|" re-box))
5634 beg-cookie end-cookie is-percent c-on c-off lim
5635 eline curr-ind next-ind continue-from startsearch
5639 (goto-char (point-min))
5640 (outline-next-heading)
5641 (setq beg (point) end (point-max)))
5643 ;; find each statistic cookie
5644 (while (re-search-backward re-find beg t)
5645 (setq beg-cookie (match-beginning 1)
5646 end-cookie (match-end 1)
5647 cstat (+ cstat (if end-cookie 1 0))
5648 startsearch (point-at-eol)
5649 continue-from (point-at-bol)
5650 is-percent (match-beginning 2)
5652 ((org-on-heading-p) (outline-next-heading) (point))
5653 ((org-at-item-p) (org-end-of-item) (point))
5658 ;; find first checkbox for this cookie and gather
5659 ;; statistics from all that are at this indentation level
5660 (goto-char startsearch)
5661 (if (re-search-forward re-box lim t)
5663 (org-beginning-of-item)
5664 (setq curr-ind (org-get-indentation))
5665 (setq next-ind curr-ind)
5666 (while (and (bolp) (org-at-item-p) (= curr-ind next-ind))
5667 (save-excursion (end-of-line) (setq eline (point)))
5668 (if (re-search-forward re-box eline t)
5669 (if (member (match-string 2) '("[ ]" "[-]"))
5670 (setq c-off (1+ c-off))
5671 (setq c-on (1+ c-on))
5675 (setq next-ind (org-get-indentation))
5677 (goto-char continue-from)
5680 (delete-region beg-cookie end-cookie)
5681 (goto-char beg-cookie)
5684 (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
5685 (format "[%d/%d]" c-on (+ c-on c-off)))))
5686 ;; update items checkbox if it has one
5687 (when (org-at-item-p)
5688 (org-beginning-of-item)
5689 (when (and (> (+ c-on c-off) 0)
5690 (re-search-forward re-box (point-at-eol) t))
5691 (setq beg-cookie (match-beginning 2)
5692 end-cookie (match-end 2))
5693 (delete-region beg-cookie end-cookie)
5694 (goto-char beg-cookie)
5695 (cond ((= c-off 0) (insert "[X]"))
5696 ((= c-on 0) (insert "[ ]"))
5699 (goto-char continue-from))
5700 (when (interactive-p)
5701 (message "Checkbox satistics updated %s (%d places)"
5702 (if all "in entire file" "in current outline entry") cstat)))))
5704 (defun org-get-checkbox-statistics-face ()
5705 "Select the face for checkbox statistics.
5706 The face will be `org-done' when all relevant boxes are checked. Otherwise
5707 it will be `org-todo'."
5709 (if (equal (match-string 1) "100%") 'org-done 'org-todo)
5710 (if (and (> (match-end 2) (match-beginning 2))
5711 (equal (match-string 2) (match-string 3)))
5715 (defun org-get-indentation (&optional line)
5716 "Get the indentation of the current line, interpreting tabs.
5717 When LINE is given, assume it represents a line and compute its indentation."
5719 (if (string-match "^ *" (org-remove-tabs line))
5722 (beginning-of-line 1)
5723 (skip-chars-forward " \t")
5726 (defun org-remove-tabs (s &optional width)
5727 "Replace tabulators in S with spaces.
5728 Assumes that s is a single line, starting in column 0."
5729 (setq width (or width tab-width))
5730 (while (string-match "\t" s)
5731 (setq s (replace-match
5733 (- (* width (/ (+ (match-beginning 0) width) width))
5734 (match-beginning 0)) ?\ )
5738 (defun org-fix-indentation (line ind)
5739 "Fix indentation in LINE.
5740 IND is a cons cell with target and minimum indentation.
5741 If the current indenation in LINE is smaller than the minimum,
5742 leave it alone. If it is larger than ind, set it to the target."
5743 (let* ((l (org-remove-tabs line))
5744 (i (org-get-indentation l))
5745 (i1 (car ind)) (i2 (cdr ind)))
5746 (if (>= i i2) (setq l (substring line i2)))
5748 (concat (make-string i1 ?\ ) l)
5751 (defun org-beginning-of-item ()
5752 "Go to the beginning of the current hand-formatted item.
5753 If the cursor is not in an item, throw an error."
5756 (limit (save-excursion
5759 (org-back-to-heading)
5760 (beginning-of-line 2) (point))
5761 (error (point-min)))))
5762 (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
5765 (beginning-of-line 1)
5766 (beginning-of-line 1)
5767 (skip-chars-forward " \t")
5768 (setq ind (current-column))
5771 (beginning-of-line 0)
5772 (if (or (bobp) (< (point) limit)) (throw 'exit nil))
5774 (if (looking-at "[ \t]*$")
5775 (setq ind1 ind-empty)
5776 (skip-chars-forward " \t")
5777 (setq ind1 (current-column)))
5779 (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
5782 (error "Not in an item")))))
5784 (defun org-end-of-item ()
5785 "Go to the end of the current hand-formatted item.
5786 If the cursor is not in an item, throw an error."
5788 (let* ((pos (point))
5790 (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
5791 (limit (save-excursion (outline-next-heading) (point)))
5792 (ind (save-excursion
5793 (org-beginning-of-item)
5794 (skip-chars-forward " \t")
5798 (beginning-of-line 2)
5799 (if (eobp) (throw 'exit (point)))
5800 (if (>= (point) limit) (throw 'exit (point-at-bol)))
5801 (if (looking-at "[ \t]*$")
5802 (setq ind1 ind-empty)
5803 (skip-chars-forward " \t")
5804 (setq ind1 (current-column)))
5806 (throw 'exit (point-at-bol)))))))
5810 (error "Not in an item"))))
5812 (defun org-next-item ()
5813 "Move to the beginning of the next item in the current plain list.
5814 Error if not at a plain list, or if this is the last item in the list."
5816 (let (ind ind1 (pos (point)))
5817 (org-beginning-of-item)
5818 (setq ind (org-get-indentation))
5820 (setq ind1 (org-get-indentation))
5821 (unless (and (org-at-item-p) (= ind ind1))
5823 (error "On last item"))))
5825 (defun org-previous-item ()
5826 "Move to the beginning of the previous item in the current plain list.
5827 Error if not at a plain list, or if this is the first item in the list."
5829 (let (beg ind ind1 (pos (point)))
5830 (org-beginning-of-item)
5832 (setq ind (org-get-indentation))
5836 (beginning-of-line 0)
5837 (if (looking-at "[ \t]*$")
5839 (if (<= (setq ind1 (org-get-indentation)) ind)
5842 (if (or (not (org-at-item-p))
5845 (org-beginning-of-item))
5846 (error (goto-char pos)
5847 (error "On first item")))))
5849 (defun org-first-list-item-p ()
5850 "Is this heading the item in a plain list?"
5851 (unless (org-at-item-p)
5852 (error "Not at a plain list item"))
5853 (org-beginning-of-item)
5854 (= (point) (save-excursion (org-beginning-of-item-list))))
5856 (defun org-move-item-down ()
5857 "Move the plain list item at point down, i.e. swap with following item.
5858 Subitems (items with larger indentation) are considered part of the item,
5859 so this really moves item trees."
5861 (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
5862 (org-beginning-of-item)
5865 (setq ne-beg (org-back-over-empty-lines))
5868 (setq ind (org-get-indentation))
5871 (setq ind1 (org-get-indentation))
5872 (setq ne-end (org-back-over-empty-lines))
5875 (when (and (org-first-list-item-p) (< ne-end ne-beg))
5876 ;; include less whitespace
5879 (forward-line (- ne-beg ne-end))
5880 (setq beg (point))))
5882 (if (and (org-at-item-p) (= ind ind1))
5885 (org-back-over-empty-lines)
5886 (setq txt (buffer-substring beg end))
5888 (delete-region beg end))
5891 (goto-char pos) (org-skip-whitespace)
5892 (org-maybe-renumber-ordered-list))
5894 (error "Cannot move this item further down"))))
5896 (defun org-move-item-up (arg)
5897 "Move the plain list item at point up, i.e. swap with previous item.
5898 Subitems (items with larger indentation) are considered part of the item,
5899 so this really moves item trees."
5901 (let (beg beg0 end ind ind1 (pos (point)) txt
5902 ne-beg ne-ins ins-end)
5903 (org-beginning-of-item)
5905 (setq ind (org-get-indentation))
5907 (setq ne-beg (org-back-over-empty-lines))
5915 (beginning-of-line 0)
5916 (if (looking-at "[ \t]*$")
5917 (if org-empty-line-terminates-plain-lists
5920 (error "Cannot move this item further up"))
5922 (if (<= (setq ind1 (org-get-indentation)) ind)
5925 (org-beginning-of-item)
5926 (error (goto-char beg)
5927 (error "Cannot move this item further up")))
5928 (setq ind1 (org-get-indentation))
5929 (if (and (org-at-item-p) (= ind ind1))
5931 (setq ne-ins (org-back-over-empty-lines))
5932 (setq txt (buffer-substring beg end))
5934 (delete-region beg end))
5937 (setq ins-end (point))
5938 (goto-char pos) (org-skip-whitespace)
5940 (when (and (org-first-list-item-p) (> ne-ins ne-beg))
5941 ;; Move whitespace back to beginning
5944 (let ((kill-whole-line t))
5945 (kill-line (- ne-ins ne-beg)) (point)))
5946 (insert (make-string (- ne-ins ne-beg) ?\n)))
5948 (org-maybe-renumber-ordered-list))
5950 (error "Cannot move this item further up"))))
5952 (defun org-maybe-renumber-ordered-list ()
5953 "Renumber the ordered list at point if setup allows it.
5954 This tests the user option `org-auto-renumber-ordered-lists' before
5955 doing the renumbering."
5957 (when (and org-auto-renumber-ordered-lists
5959 (if (match-beginning 3)
5960 (org-renumber-ordered-list 1)
5961 (org-fix-bullet-type))))
5963 (defun org-maybe-renumber-ordered-list-safe ()
5966 (org-maybe-renumber-ordered-list))
5969 (defun org-cycle-list-bullet (&optional which)
5970 "Cycle through the different itemize/enumerate bullets.
5971 This cycle the entire list level through the sequence:
5973 `-' -> `+' -> `*' -> `1.' -> `1)'
5975 If WHICH is a string, use that as the new bullet. If WHICH is an integer,
5976 0 meand `-', 1 means `+' etc."
5979 (org-beginning-of-item-list)
5981 (beginning-of-line 1)
5982 (let ((current (match-string 0))
5983 (prevp (eq which 'previous))
5986 ((and (numberp which)
5987 (nth (1- which) '("-" "+" "*" "1." "1)"))))
5988 ((string-match "-" current) (if prevp "1)" "+"))
5989 ((string-match "\\+" current)
5990 (if prevp "-" (if (looking-at "\\S-") "1." "*")))
5991 ((string-match "\\*" current) (if prevp "+" "1."))
5992 ((string-match "\\." current) (if prevp "*" "1)"))
5993 ((string-match ")" current) (if prevp "1." "-"))
5994 (t (error "This should not happen"))))
5995 (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
5996 (org-fix-bullet-type)
5997 (org-maybe-renumber-ordered-list))))
5999 (defun org-get-string-indentation (s)
6000 "What indentation has S due to SPACE and TAB at the beginning of the string?"
6001 (let ((n -1) (i 0) (w tab-width) c)
6003 (while (< (setq n (1+ n)) (length s))
6005 (cond ((= c ?\ ) (setq i (1+ i)))
6006 ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
6007 (t (throw 'exit t)))))
6010 (defun org-renumber-ordered-list (arg)
6011 "Renumber an ordered plain list.
6012 Cursor needs to be in the first line of an item, the line that starts
6013 with something like \"1.\" or \"2)\"."
6015 (unless (and (org-at-item-p)
6016 (match-beginning 3))
6017 (error "This is not an ordered list"))
6018 (let ((line (org-current-line))
6019 (col (current-column))
6020 (ind (org-get-string-indentation
6021 (buffer-substring (point-at-bol) (match-beginning 3))))
6022 ;; (term (substring (match-string 3) -1))
6025 ;; find where this list begins
6026 (org-beginning-of-item-list)
6027 (looking-at "[ \t]*[0-9]+\\([.)]\\)")
6028 (setq fmt (concat "%d" (match-string 1)))
6029 (beginning-of-line 0)
6030 ;; walk forward and replace these numbers
6034 (beginning-of-line 2)
6035 (if (eobp) (throw 'exit nil))
6036 (if (looking-at "[ \t]*$") (throw 'next nil))
6037 (skip-chars-forward " \t") (setq ind1 (current-column))
6038 (if (> ind1 ind) (throw 'next t))
6039 (if (< ind1 ind) (throw 'exit t))
6040 (if (not (org-at-item-p)) (throw 'exit nil))
6041 (delete-region (match-beginning 2) (match-end 2))
6042 (goto-char (match-beginning 2))
6043 (insert (format fmt (setq n (1+ n)))))))
6045 (org-move-to-column col)))
6047 (defun org-fix-bullet-type ()
6048 "Make sure all items in this list have the same bullet as the firsst item."
6050 (unless (org-at-item-p) (error "This is not a list"))
6051 (let ((line (org-current-line))
6052 (col (current-column))
6053 (ind (current-indentation))
6055 ;; find where this list begins
6056 (org-beginning-of-item-list)
6057 (beginning-of-line 1)
6058 ;; find out what the bullet type is
6059 (looking-at "[ \t]*\\(\\S-+\\)")
6060 (setq bullet (match-string 1))
6061 ;; walk forward and replace these numbers
6062 (beginning-of-line 0)
6066 (beginning-of-line 2)
6067 (if (eobp) (throw 'exit nil))
6068 (if (looking-at "[ \t]*$") (throw 'next nil))
6069 (skip-chars-forward " \t") (setq ind1 (current-column))
6070 (if (> ind1 ind) (throw 'next t))
6071 (if (< ind1 ind) (throw 'exit t))
6072 (if (not (org-at-item-p)) (throw 'exit nil))
6073 (skip-chars-forward " \t")
6074 (looking-at "\\S-+")
6075 (replace-match bullet))))
6077 (org-move-to-column col)
6078 (if (string-match "[0-9]" bullet)
6079 (org-renumber-ordered-list 1))))
6081 (defun org-beginning-of-item-list ()
6082 "Go to the beginning of the current item list.
6083 I.e. to the first item in this list."
6085 (org-beginning-of-item)
6086 (let ((pos (point-at-bol))
6087 (ind (org-get-indentation))
6089 ;; find where this list begins
6093 (beginning-of-line 0)
6094 (if (looking-at "[ \t]*$")
6095 (throw (if (bobp) 'exit 'next) t))
6096 (skip-chars-forward " \t") (setq ind1 (current-column))
6097 (if (or (< ind1 ind)
6099 (not (org-at-item-p)))
6102 (when (org-at-item-p) (setq pos (point-at-bol)))))))
6106 (defun org-end-of-item-list ()
6107 "Go to the end of the current item list.
6108 I.e. to the text after the last item."
6110 (org-beginning-of-item)
6111 (let ((pos (point-at-bol))
6112 (ind (org-get-indentation))
6114 ;; find where this list begins
6118 (beginning-of-line 2)
6119 (if (looking-at "[ \t]*$")
6120 (throw (if (eobp) 'exit 'next) t))
6121 (skip-chars-forward " \t") (setq ind1 (current-column))
6122 (if (or (< ind1 ind)
6124 (not (org-at-item-p)))
6127 (setq pos (point-at-bol))
6128 (throw 'exit t))))))
6132 (defvar org-last-indent-begin-marker (make-marker))
6133 (defvar org-last-indent-end-marker (make-marker))
6135 (defun org-outdent-item (arg)
6136 "Outdent a local list item."
6138 (org-indent-item (- arg)))
6140 (defun org-indent-item (arg)
6141 "Indent a local list item."
6143 (unless (org-at-item-p)
6144 (error "Not on an item"))
6146 (let (beg end ind ind1 tmp delta ind-down ind-up)
6147 (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
6148 (setq beg org-last-indent-begin-marker
6149 end org-last-indent-end-marker)
6150 (org-beginning-of-item)
6151 (setq beg (move-marker org-last-indent-begin-marker (point)))
6153 (setq end (move-marker org-last-indent-end-marker (point))))
6155 (setq tmp (org-item-indent-positions)
6157 ind-down (nth 2 tmp)
6160 (if ind-down (- ind-down ind) 2)
6161 (if ind-up (- ind-up ind) -2)))
6162 (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
6163 (while (< (point) end)
6164 (beginning-of-line 1)
6165 (skip-chars-forward " \t") (setq ind1 (current-column))
6166 (delete-region (point-at-bol) (point))
6167 (or (eolp) (org-indent-to-column (+ ind1 delta)))
6168 (beginning-of-line 2))))
6169 (org-fix-bullet-type)
6170 (org-maybe-renumber-ordered-list-safe)
6172 (beginning-of-line 0)
6173 (condition-case nil (org-beginning-of-item) (error nil))
6174 (org-maybe-renumber-ordered-list-safe)))
6176 (defun org-item-indent-positions ()
6177 "Return indentation for plain list items.
6178 This returns a list with three values: The current indentation, the
6179 parent indentation and the indentation a child should habe.
6180 Assumes cursor in item line."
6181 (let* ((bolpos (point-at-bol))
6182 (ind (org-get-indentation))
6183 ind-down ind-up pos)
6185 (org-beginning-of-item-list)
6186 (skip-chars-backward "\n\r \t")
6187 (when (org-in-item-p)
6188 (org-beginning-of-item)
6189 (setq ind-up (org-get-indentation))))
6193 ((and (condition-case nil (progn (org-previous-item) t)
6195 (or (forward-char 1) t)
6196 (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
6197 (setq ind-down (org-get-indentation)))
6198 ((and (goto-char pos)
6200 (goto-char (match-end 0))
6201 (skip-chars-forward " \t")
6202 (setq ind-down (current-column)))))
6203 (list ind ind-up ind-down)))
6205 ;;; The orgstruct minor mode
6207 ;; Define a minor mode which can be used in other modes in order to
6208 ;; integrate the org-mode structure editing commands.
6210 ;; This is really a hack, because the org-mode structure commands use
6211 ;; keys which normally belong to the major mode. Here is how it
6212 ;; works: The minor mode defines all the keys necessary to operate the
6213 ;; structure commands, but wraps the commands into a function which
6214 ;; tests if the cursor is currently at a headline or a plain list
6215 ;; item. If that is the case, the structure command is used,
6216 ;; temporarily setting many Org-mode variables like regular
6217 ;; expressions for filling etc. However, when any of those keys is
6218 ;; used at a different location, function uses `key-binding' to look
6219 ;; up if the key has an associated command in another currently active
6220 ;; keymap (minor modes, major mode, global), and executes that
6221 ;; command. There might be problems if any of the keys is otherwise
6222 ;; used as a prefix key.
6224 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
6225 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
6226 ;; addresses this by checking explicitly for both bindings.
6228 (defvar orgstruct-mode-map (make-sparse-keymap)
6229 "Keymap for the minor `orgstruct-mode'.")
6231 (defvar org-local-vars nil
6232 "List of local variables, for use by `orgstruct-mode'")
6235 (define-minor-mode orgstruct-mode
6236 "Toggle the minor more `orgstruct-mode'.
6237 This mode is for using Org-mode structure commands in other modes.
6238 The following key behave as if Org-mode was active, if the cursor
6239 is on a headline, or on a plain list item (both in the definition
6242 M-up Move entry/item up
6243 M-down Move entry/item down
6246 M-S-up Move entry/item up
6247 M-S-down Move entry/item down
6248 M-S-left Promote subtree
6249 M-S-right Demote subtree
6250 M-q Fill paragraph and items like in Org-mode
6252 C-c - Cycle list bullet
6253 TAB Cycle item visibility
6254 M-RET Insert new heading/item
6255 S-M-RET Insert new TODO heading / Chekbox item
6256 C-c C-c Set tags / toggle checkbox"
6257 nil " OrgStruct" nil
6258 (org-load-modules-maybe)
6259 (and (orgstruct-setup) (defun orgstruct-setup () nil)))
6262 (defun turn-on-orgstruct ()
6263 "Unconditionally turn on `orgstruct-mode'."
6267 (defun turn-on-orgstruct++ ()
6268 "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
6269 In addition to setting orgstruct-mode, this also exports all indentation and
6270 autofilling variables from org-mode into the buffer. Note that turning
6271 off orgstruct-mode will *not* remove these additional settings."
6277 "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
6278 (symbol-name (car x)))
6279 (setq var (car x) val (nth 1 x))
6280 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
6283 (defun orgstruct-error ()
6284 "Error when there is no default binding for a structure key."
6286 (error "This key has no function outside structure elements"))
6288 (defun orgstruct-setup ()
6289 "Setup orgstruct keymaps."
6293 '([(meta up)] org-metaup)
6294 '([(meta down)] org-metadown)
6295 '([(meta left)] org-metaleft)
6296 '([(meta right)] org-metaright)
6297 '([(meta shift up)] org-shiftmetaup)
6298 '([(meta shift down)] org-shiftmetadown)
6299 '([(meta shift left)] org-shiftmetaleft)
6300 '([(meta shift right)] org-shiftmetaright)
6301 '([(shift up)] org-shiftup)
6302 '([(shift down)] org-shiftdown)
6303 '("\C-c\C-c" org-ctrl-c-ctrl-c)
6304 '("\M-q" fill-paragraph)
6306 '("\C-c-" org-cycle-list-bullet)))
6308 (while (setq elt (pop bindings))
6309 (setq nfunc (1+ nfunc))
6310 (setq key (org-key (car elt))
6312 cmd (orgstruct-make-binding fun nfunc key))
6313 (org-defkey orgstruct-mode-map key cmd))
6315 ;; Special treatment needed for TAB and RET
6316 (org-defkey orgstruct-mode-map [(tab)]
6317 (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
6318 (org-defkey orgstruct-mode-map "\C-i"
6319 (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
6321 (org-defkey orgstruct-mode-map "\M-\C-m"
6322 (orgstruct-make-binding 'org-insert-heading 105
6323 "\M-\C-m" [(meta return)]))
6324 (org-defkey orgstruct-mode-map [(meta return)]
6325 (orgstruct-make-binding 'org-insert-heading 106
6326 [(meta return)] "\M-\C-m"))
6328 (org-defkey orgstruct-mode-map [(shift meta return)]
6329 (orgstruct-make-binding 'org-insert-todo-heading 107
6330 [(meta return)] "\M-\C-m"))
6332 (unless org-local-vars
6333 (setq org-local-vars (org-get-local-variables)))
6337 (defun orgstruct-make-binding (fun n &rest keys)
6338 "Create a function for binding in the structure minor mode.
6339 FUN is the command to call inside a table. N is used to create a unique
6340 command name. KEYS are keys that should be checked in for a command
6341 to execute outside of tables."
6344 (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
6346 (concat "In Structure, run `" (symbol-name fun) "'.\n"
6347 "Outside of structure, run the binding of `"
6348 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
6352 '(org-context-p 'headline 'item)
6353 (list 'org-run-like-in-org-mode (list 'quote fun))
6354 (list 'let '(orgstruct-mode)
6355 (list 'call-interactively
6358 (list 'key-binding k))
6360 '('orgstruct-error))))))))
6362 (defun org-context-p (&rest contexts)
6363 "Check if local context is and of CONTEXTS.
6364 Possible values in the list of contexts are `table', `headline', and `item'."
6365 (let ((pos (point)))
6366 (goto-char (point-at-bol))
6367 (prog1 (or (and (memq 'table contexts)
6368 (looking-at "[ \t]*|"))
6369 (and (memq 'headline contexts)
6370 (looking-at "\\*+"))
6371 (and (memq 'item contexts)
6372 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
6375 (defun org-get-local-variables ()
6376 "Return a list of all local variables in an org-mode buffer."
6378 (with-current-buffer (get-buffer-create "*Org tmp*")
6381 (setq varlist (buffer-local-variables)))
6382 (kill-buffer "*Org tmp*")
6389 (list (car x) (list 'quote (cdr x)))))
6391 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
6392 (symbol-name (car x)))
6397 (defun org-run-like-in-org-mode (cmd)
6398 (org-load-modules-maybe)
6399 (unless org-local-vars
6400 (setq org-local-vars (org-get-local-variables)))
6401 (eval (list 'let org-local-vars
6402 (list 'call-interactively (list 'quote cmd)))))
6406 (defun org-get-category (&optional pos)
6407 "Get the category applying to position POS."
6408 (get-text-property (or pos (point)) 'org-category))
6410 (defun org-refresh-category-properties ()
6411 "Refresh category text properties in the buffer."
6412 (let ((def-cat (cond
6413 ((null org-category)
6414 (if buffer-file-name
6415 (file-name-sans-extension
6416 (file-name-nondirectory buffer-file-name))
6418 ((symbolp org-category) (symbol-name org-category))
6420 beg end cat pos optionp)
6425 (goto-char (point-min))
6426 (put-text-property (point) (point-max) 'org-category def-cat)
6427 (while (re-search-forward
6428 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
6429 (setq pos (match-end 0)
6430 optionp (equal (char-after (match-beginning 0)) ?#)
6431 cat (org-trim (match-string 2)))
6433 (setq beg (point-at-bol) end (point-max))
6434 (org-back-to-heading t)
6435 (setq beg (point) end (org-end-of-subtree t t)))
6436 (put-text-property beg end 'org-category cat)
6437 (goto-char pos)))))))
6442 ;;; Link abbreviations
6444 (defun org-link-expand-abbrev (link)
6445 "Apply replacements as defined in `org-link-abbrev-alist."
6446 (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
6447 (let* ((key (match-string 1 link))
6448 (as (or (assoc key org-link-abbrev-alist-local)
6449 (assoc key org-link-abbrev-alist)))
6450 (tag (and (match-end 2) (match-string 3 link)))
6456 ((symbolp rpl) (funcall rpl tag))
6457 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
6458 (t (concat rpl tag)))))
6461 ;;; Storing and inserting links
6463 (defvar org-insert-link-history nil
6464 "Minibuffer history for links inserted with `org-insert-link'.")
6466 (defvar org-stored-links nil
6467 "Contains the links stored with `org-store-link'.")
6469 (defvar org-store-link-plist nil
6470 "Plist with info about the most recently link created with `org-store-link'.")
6472 (defvar org-link-protocols nil
6473 "Link protocols added to Org-mode using `org-add-link-type'.")
6475 (defvar org-store-link-functions nil
6476 "List of functions that are called to create and store a link.
6477 Each function will be called in turn until one returns a non-nil
6478 value. Each function should check if it is responsible for creating
6479 this link (for example by looking at the major mode).
6480 If not, it must exit and return nil.
6481 If yes, it should return a non-nil value after a calling
6482 `org-store-link-props' with a list of properties and values.
6483 Special properties are:
6485 :type The link prefix. like \"http\". This must be given.
6486 :link The link, like \"http://www.astro.uva.nl/~dominik\".
6487 This is obligatory as well.
6488 :description Optional default description for the second pair
6489 of brackets in an Org-mode link. The user can still change
6490 this when inserting this link into an Org-mode buffer.
6492 In addition to these, any additional properties can be specified
6493 and then used in remember templates.")
6495 (defun org-add-link-type (type &optional follow export)
6496 "Add TYPE to the list of `org-link-types'.
6497 Re-compute all regular expressions depending on `org-link-types'
6499 FOLLOW and EXPORT are two functions.
6501 FOLLOW should take the link path as the single argument and do whatever
6502 is necessary to follow the link, for example find a file or display
6505 EXPORT should format the link path for export to one of the export formats.
6506 It should be a function accepting three arguments:
6508 path the path of the link, the text after the prefix (like \"http:\")
6509 desc the description of the link, if any, nil if there was no descripton
6510 format the export format, a symbol like `html' or `latex'.
6512 The function may use the FORMAT information to return different values
6513 depending on the format. The return value will be put literally into
6515 Org-mode has a built-in default for exporting links. If you are happy with
6516 this default, there is no need to define an export function for the link
6517 type. For a simple example of an export function, see `org-bbdb.el'."
6518 (add-to-list 'org-link-types type t)
6519 (org-make-link-regexps)
6520 (if (assoc type org-link-protocols)
6521 (setcdr (assoc type org-link-protocols) (list follow export))
6522 (push (list type follow export) org-link-protocols)))
6526 (defun org-store-link (arg)
6527 "\\<org-mode-map>Store an org-link to the current location.
6528 This link is added to `org-stored-links' and can later be inserted
6529 into an org-buffer with \\[org-insert-link].
6531 For some link types, a prefix arg is interpreted:
6532 For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
6533 For file links, arg negates `org-context-in-file-links'."
6535 (org-load-modules-maybe)
6536 (setq org-store-link-plist nil) ; reset
6537 (let (link cpltxt desc description search txt)
6540 ((run-hook-with-args-until-success 'org-store-link-functions)
6541 (setq link (plist-get org-store-link-plist :link)
6542 desc (or (plist-get org-store-link-plist :description) link)))
6544 ((eq major-mode 'calendar-mode)
6545 (let ((cd (calendar-cursor-to-date)))
6548 (car org-time-stamp-formats)
6550 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
6552 (org-store-link-props :type "calendar" :date cd)))
6554 ((eq major-mode 'w3-mode)
6555 (setq cpltxt (url-view-url t)
6556 link (org-make-link cpltxt))
6557 (org-store-link-props :type "w3" :url (url-view-url t)))
6559 ((eq major-mode 'w3m-mode)
6560 (setq cpltxt (or w3m-current-title w3m-current-url)
6561 link (org-make-link w3m-current-url))
6562 (org-store-link-props :type "w3m" :url (url-view-url t)))
6564 ((setq search (run-hook-with-args-until-success
6565 'org-create-file-search-functions))
6566 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
6568 (setq cpltxt (or description link)))
6570 ((eq major-mode 'image-mode)
6571 (setq cpltxt (concat "file:"
6572 (abbreviate-file-name buffer-file-name))
6573 link (org-make-link cpltxt))
6574 (org-store-link-props :type "image" :file buffer-file-name))
6576 ((eq major-mode 'dired-mode)
6577 ;; link to the file in the current line
6578 (setq cpltxt (concat "file:"
6579 (abbreviate-file-name
6581 (dired-get-filename nil t))))
6582 link (org-make-link cpltxt)))
6584 ((and buffer-file-name (org-mode-p))
6585 ;; Just link to current headline
6586 (setq cpltxt (concat "file:"
6587 (abbreviate-file-name buffer-file-name)))
6588 ;; Add a context search string
6589 (when (org-xor org-context-in-file-links arg)
6590 ;; Check if we are on a target
6591 (if (org-in-regexp "<<\\(.*?\\)>>")
6592 (setq cpltxt (concat cpltxt "::" (match-string 1)))
6594 ((org-on-heading-p) nil)
6595 ((org-region-active-p)
6596 (buffer-substring (region-beginning) (region-end)))
6598 (when (or (null txt) (string-match "\\S-" txt))
6602 (org-make-org-heading-search-string txt)
6605 (if (string-match "::\\'" cpltxt)
6606 (setq cpltxt (substring cpltxt 0 -2)))
6607 (setq link (org-make-link cpltxt)))
6609 ((buffer-file-name (buffer-base-buffer))
6610 ;; Just link to this file here.
6611 (setq cpltxt (concat "file:"
6612 (abbreviate-file-name
6613 (buffer-file-name (buffer-base-buffer)))))
6614 ;; Add a context string
6615 (when (org-xor org-context-in-file-links arg)
6616 (setq txt (if (org-region-active-p)
6617 (buffer-substring (region-beginning) (region-end))
6618 (buffer-substring (point-at-bol) (point-at-eol))))
6619 ;; Only use search option if there is some text.
6620 (when (string-match "\\S-" txt)
6622 (concat cpltxt "::" (org-make-org-heading-search-string txt))
6624 (setq link (org-make-link cpltxt)))
6627 (error "Cannot link to a buffer which is not visiting a file"))
6629 (t (setq link nil)))
6631 (if (consp link) (setq cpltxt (car link) link (cdr link)))
6632 (setq link (or link cpltxt)
6633 desc (or desc cpltxt))
6634 (if (equal desc "NONE") (setq desc nil))
6636 (if (and (interactive-p) link)
6638 (setq org-stored-links
6639 (cons (list link desc) org-stored-links))
6640 (message "Stored: %s" (or desc link)))
6641 (and link (org-make-link-string link desc)))))
6643 (defun org-store-link-props (&rest plist)
6644 "Store link properties, extract names and addresses."
6646 (when (setq x (plist-get plist :from))
6647 (setq adr (mail-extract-address-components x))
6648 (plist-put plist :fromname (car adr))
6649 (plist-put plist :fromaddress (nth 1 adr)))
6650 (when (setq x (plist-get plist :to))
6651 (setq adr (mail-extract-address-components x))
6652 (plist-put plist :toname (car adr))
6653 (plist-put plist :toaddress (nth 1 adr))))
6654 (let ((from (plist-get plist :from))
6655 (to (plist-get plist :to)))
6656 (when (and from to org-from-is-user-regexp)
6657 (plist-put plist :fromto
6658 (if (string-match org-from-is-user-regexp from)
6660 (concat "from %f")))))
6661 (setq org-store-link-plist plist))
6663 (defun org-add-link-props (&rest plist)
6664 "Add these properties to the link property list."
6667 (setq key (pop plist) value (pop plist))
6668 (setq org-store-link-plist
6669 (plist-put org-store-link-plist key value)))))
6671 (defun org-email-link-description (&optional fmt)
6672 "Return the description part of an email link.
6673 This takes information from `org-store-link-plist' and formats it
6674 according to FMT (default from `org-email-link-description-format')."
6675 (setq fmt (or fmt org-email-link-description-format))
6676 (let* ((p org-store-link-plist)
6677 (to (plist-get p :toaddress))
6678 (from (plist-get p :fromaddress))
6681 (cons "%c" (plist-get p :fromto))
6682 (cons "%F" (plist-get p :from))
6683 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
6684 (cons "%T" (plist-get p :to))
6685 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
6686 (cons "%s" (plist-get p :subject))
6687 (cons "%m" (plist-get p :message-id)))))
6688 (when (string-match "%c" fmt)
6689 ;; Check if the user wrote this message
6690 (if (and org-from-is-user-regexp from to
6691 (save-match-data (string-match org-from-is-user-regexp from)))
6692 (setq fmt (replace-match "to %t" t t fmt))
6693 (setq fmt (replace-match "from %f" t t fmt))))
6694 (org-replace-escapes fmt table)))
6696 (defun org-make-org-heading-search-string (&optional string heading)
6697 "Make search string for STRING or current headline."
6699 (let ((s (or string (org-get-heading))))
6700 (unless (and string (not heading))
6701 ;; We are using a headline, clean up garbage in there.
6702 (if (string-match org-todo-regexp s)
6703 (setq s (replace-match "" t t s)))
6704 (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
6705 (setq s (replace-match "" t t s)))
6706 (setq s (org-trim s))
6707 (if (string-match (concat "^\\(" org-quote-string "\\|"
6708 org-comment-string "\\)") s)
6709 (setq s (replace-match "" t t s)))
6710 (while (string-match org-ts-regexp s)
6711 (setq s (replace-match "" t t s))))
6712 (while (string-match "[^a-zA-Z_0-9 \t]+" s)
6713 (setq s (replace-match " " t t s)))
6714 (or string (setq s (concat "*" s))) ; Add * for headlines
6715 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
6717 (defun org-make-link (&rest strings)
6718 "Concatenate STRINGS."
6719 (apply 'concat strings))
6721 (defun org-make-link-string (link &optional description)
6722 "Make a link with brackets, consisting of LINK and DESCRIPTION."
6723 (unless (string-match "\\S-" link)
6724 (error "Empty link"))
6725 (when (stringp description)
6726 ;; Remove brackets from the description, they are fatal.
6727 (while (string-match "\\[" description)
6728 (setq description (replace-match "{" t t description)))
6729 (while (string-match "\\]" description)
6730 (setq description (replace-match "}" t t description))))
6731 (when (equal (org-link-escape link) description)
6732 ;; No description needed, it is identical
6733 (setq description nil))
6734 (when (and (not description)
6735 (not (equal link (org-link-escape link))))
6736 (setq description link))
6737 (concat "[[" (org-link-escape link) "]"
6738 (if description (concat "[" description "]") "")
6741 (defconst org-link-escape-chars
6745 (?\340 . "%E0") ; `a
6746 (?\342 . "%E2") ; ^a
6747 (?\347 . "%E7") ; ,c
6748 (?\350 . "%E8") ; `e
6749 (?\351 . "%E9") ; 'e
6750 (?\352 . "%EA") ; ^e
6751 (?\356 . "%EE") ; ^i
6752 (?\364 . "%F4") ; ^o
6753 (?\371 . "%F9") ; `u
6754 (?\373 . "%FB") ; ^u
6760 "Association list of escapes for some characters problematic in links.
6761 This is the list that is used for internal purposes.")
6763 (defconst org-link-escape-chars-browser
6764 '((?\ . "%20")) ; 32 for the SPC char
6765 "Association list of escapes for some characters problematic in links.
6766 This is the list that is used before handing over to the browser.")
6768 (defun org-link-escape (text &optional table)
6769 "Escape charaters in TEXT that are problematic for links."
6770 (setq table (or table org-link-escape-chars))
6772 (let ((re (mapconcat (lambda (x) (regexp-quote
6773 (char-to-string (car x))))
6775 (while (string-match re text)
6778 (cdr (assoc (string-to-char (match-string 0 text))
6783 (defun org-link-unescape (text &optional table)
6784 "Reverse the action of `org-link-escape'."
6785 (setq table (or table org-link-escape-chars))
6787 (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
6789 (while (string-match re text)
6792 (char-to-string (car (rassoc (match-string 0 text) table)))
6796 (defun org-xor (a b)
6800 (defun org-get-header (header)
6801 "Find a header field in the current buffer."
6803 (goto-char (point-min))
6804 (let ((case-fold-search t) s)
6807 (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
6808 (setq s (match-string 1)))
6809 (while (string-match "\"" s)
6810 (setq s (replace-match "" t t s)))
6811 (if (string-match "[<(].*" s)
6812 (setq s (replace-match "" t t s))))
6813 ((eq header 'message-id)
6814 (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
6815 (setq s (match-string 1))))
6816 ((eq header 'subject)
6817 (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
6818 (setq s (match-string 1)))))
6819 (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
6820 (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
6824 (defun org-fixup-message-id-for-http (s)
6825 "Replace special characters in a message id, so it can be used in an http query."
6826 (while (string-match "<" s)
6827 (setq s (replace-match "%3C" t t s)))
6828 (while (string-match ">" s)
6829 (setq s (replace-match "%3E" t t s)))
6830 (while (string-match "@" s)
6831 (setq s (replace-match "%40" t t s)))
6835 (defun org-insert-link-global ()
6836 "Insert a link like Org-mode does.
6837 This command can be called in any mode to insert a link in Org-mode syntax."
6839 (org-load-modules-maybe)
6840 (org-run-like-in-org-mode 'org-insert-link))
6842 (defun org-insert-link (&optional complete-file link-location)
6843 "Insert a link. At the prompt, enter the link.
6845 Completion can be used to select a link previously stored with
6846 `org-store-link'. When the empty string is entered (i.e. if you just
6847 press RET at the prompt), the link defaults to the most recently
6848 stored link. As SPC triggers completion in the minibuffer, you need to
6849 use M-SPC or C-q SPC to force the insertion of a space character.
6851 You will also be prompted for a description, and if one is given, it will
6852 be displayed in the buffer instead of the link.
6854 If there is already a link at point, this command will allow you to edit link
6855 and description parts.
6857 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
6858 be selected using completion. The path to the file will be relative to the
6859 current directory if the file is in the current directory or a subdirectory.
6860 Otherwise, the link will be the absolute path as completed in the minibuffer
6861 \(i.e. normally ~/path/to/file).
6863 With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
6864 the current directory or below. With three \\[universal-argument] prefixes, negate the meaning
6865 of `org-keep-stored-link-after-insertion'.
6867 If `org-make-link-description-function' is non-nil, this function will be
6868 called with the link target, and the result will be the default
6871 If the LINK-LOCATION parameter is non-nil, this value will be
6872 used as the link location instead of reading one interactively."
6874 (let* ((wcf (current-window-configuration))
6875 (region (if (org-region-active-p)
6876 (buffer-substring (region-beginning) (region-end))))
6877 (remove (and region (list (region-beginning) (region-end))))
6879 tmphist ; byte-compile incorrectly complains about this
6880 (link link-location)
6883 (link-location) ; specified by arg, just use it.
6884 ((org-in-regexp org-bracket-link-regexp 1)
6885 ;; We do have a link at point, and we are going to edit it.
6886 (setq remove (list (match-beginning 0) (match-end 0)))
6887 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
6888 (setq link (read-string "Link: "
6890 (org-match-string-no-properties 1)))))
6891 ((or (org-in-regexp org-angle-link-re)
6892 (org-in-regexp org-plain-link-re))
6893 ;; Convert to bracket link
6894 (setq remove (list (match-beginning 0) (match-end 0))
6895 link (read-string "Link: "
6896 (org-remove-angle-brackets (match-string 0)))))
6897 ((equal complete-file '(4))
6898 ;; Completing read for file names.
6899 (setq file (read-file-name "File: "))
6900 (let ((pwd (file-name-as-directory (expand-file-name ".")))
6901 (pwd1 (file-name-as-directory (abbreviate-file-name
6902 (expand-file-name ".")))))
6904 ((equal complete-file '(16))
6905 (setq link (org-make-link
6907 (abbreviate-file-name (expand-file-name file)))))
6908 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
6909 (setq link (org-make-link "file:" (match-string 1 file))))
6910 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
6911 (expand-file-name file))
6912 (setq link (org-make-link
6913 "file:" (match-string 1 (expand-file-name file)))))
6914 (t (setq link (org-make-link "file:" file))))))
6916 ;; Read link, with completion for stored links.
6917 (with-output-to-temp-buffer "*Org Links*"
6918 (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
6919 (when org-stored-links
6920 (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
6923 (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
6924 (reverse org-stored-links) "\n"))))
6925 (let ((cw (selected-window)))
6926 (select-window (get-buffer-window "*Org Links*"))
6927 (shrink-window-if-larger-than-buffer)
6928 (setq truncate-lines t)
6930 ;; Fake a link history, containing the stored links.
6931 (setq tmphist (append (mapcar 'car org-stored-links)
6932 org-insert-link-history))
6934 (setq link (org-completing-read
6937 (mapcar (lambda (x) (list (concat (car x) ":")))
6938 (append org-link-abbrev-alist-local org-link-abbrev-alist))
6939 (mapcar (lambda (x) (list (concat x ":")))
6943 (or (car (car org-stored-links)))))
6944 (set-window-configuration wcf)
6945 (kill-buffer "*Org Links*"))
6946 (setq entry (assoc link org-stored-links))
6947 (or entry (push link org-insert-link-history))
6948 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
6949 (not org-keep-stored-link-after-insertion))
6950 (setq org-stored-links (delq (assoc link org-stored-links)
6952 (setq desc (or desc (nth 1 entry)))))
6954 (if (string-match org-plain-link-re link)
6955 ;; URL-like link, normalize the use of angular brackets.
6956 (setq link (org-make-link (org-remove-angle-brackets link))))
6958 ;; Check if we are linking to the current file with a search option
6959 ;; If yes, simplify the link by using only the search option.
6960 (when (and buffer-file-name
6961 (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
6962 (let* ((path (match-string 1 link))
6963 (case-fold-search nil)
6964 (search (match-string 2 link)))
6966 (if (equal (file-truename buffer-file-name) (file-truename path))
6967 ;; We are linking to this same file, with a search option
6968 (setq link search)))))
6970 ;; Check if we can/should use a relative path. If yes, simplify the link
6971 (when (string-match "\\<file:\\(.*\\)" link)
6972 (let* ((path (match-string 1 link))
6974 (case-fold-search nil))
6976 ((eq org-link-file-path-type 'absolute)
6977 (setq path (abbreviate-file-name (expand-file-name path))))
6978 ((eq org-link-file-path-type 'noabbrev)
6979 (setq path (expand-file-name path)))
6980 ((eq org-link-file-path-type 'relative)
6981 (setq path (file-relative-name path)))
6984 (if (string-match (concat "^" (regexp-quote
6985 (file-name-as-directory
6986 (expand-file-name "."))))
6987 (expand-file-name path))
6988 ;; We are linking a file with relative path name.
6989 (setq path (substring (expand-file-name path)
6991 (setq link (concat "file:" path))
6992 (if (equal desc origpath)
6995 (if org-make-link-description-function
6996 (setq desc (funcall org-make-link-description-function link desc)))
6998 (setq desc (read-string "Description: " desc))
6999 (unless (string-match "\\S-" desc) (setq desc nil))
7000 (if remove (apply 'delete-region remove))
7001 (insert (org-make-link-string link desc))))
7003 (defun org-completing-read (&rest args)
7004 (let ((minibuffer-local-completion-map
7005 (copy-keymap minibuffer-local-completion-map)))
7006 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
7007 (apply 'completing-read args)))
7009 ;;; Opening/following a link
7011 (defvar org-link-search-failed nil)
7013 (defun org-next-link ()
7014 "Move forward to the next link.
7015 If the link is in hidden text, expose it."
7017 (when (and org-link-search-failed (eq this-command last-command))
7018 (goto-char (point-min))
7019 (message "Link search wrapped back to beginning of buffer"))
7020 (setq org-link-search-failed nil)
7021 (let* ((pos (point))
7023 (a (assoc :link ct)))
7024 (if a (goto-char (nth 2 a)))
7025 (if (re-search-forward org-any-link-re nil t)
7027 (goto-char (match-beginning 0))
7028 (if (org-invisible-p) (org-show-context)))
7030 (setq org-link-search-failed t)
7031 (error "No further link found"))))
7033 (defun org-previous-link ()
7034 "Move backward to the previous link.
7035 If the link is in hidden text, expose it."
7037 (when (and org-link-search-failed (eq this-command last-command))
7038 (goto-char (point-max))
7039 (message "Link search wrapped back to end of buffer"))
7040 (setq org-link-search-failed nil)
7041 (let* ((pos (point))
7043 (a (assoc :link ct)))
7044 (if a (goto-char (nth 1 a)))
7045 (if (re-search-backward org-any-link-re nil t)
7047 (goto-char (match-beginning 0))
7048 (if (org-invisible-p) (org-show-context)))
7050 (setq org-link-search-failed t)
7051 (error "No further link found"))))
7053 (defun org-find-file-at-mouse (ev)
7054 "Open file link or URL at mouse."
7056 (mouse-set-point ev)
7057 (org-open-at-point 'in-emacs))
7059 (defun org-open-at-mouse (ev)
7060 "Open file link or URL at mouse."
7062 (mouse-set-point ev)
7063 (org-open-at-point))
7065 (defvar org-window-config-before-follow-link nil
7066 "The window configuration before following a link.
7067 This is saved in case the need arises to restore it.")
7069 (defvar org-open-link-marker (make-marker)
7070 "Marker pointing to the location where `org-open-at-point; was called.")
7073 (defun org-open-at-point-global ()
7074 "Follow a link like Org-mode does.
7075 This command can be called in any mode to follow a link that has
7078 (org-run-like-in-org-mode 'org-open-at-point))
7081 (defun org-open-link-from-string (s &optional arg)
7082 "Open a link in the string S, as if it was in Org-mode."
7083 (interactive "sLink: \nP")
7085 (let ((org-inhibit-startup t))
7088 (goto-char (point-min))
7089 (org-open-at-point arg))))
7091 (defun org-open-at-point (&optional in-emacs)
7092 "Open link at or after point.
7093 If there is no link at point, this function will search forward up to
7094 the end of the current subtree.
7095 Normally, files will be opened by an appropriate application. If the
7096 optional argument IN-EMACS is non-nil, Emacs will visit the file."
7098 (org-load-modules-maybe)
7099 (move-marker org-open-link-marker (point))
7100 (setq org-window-config-before-follow-link (current-window-configuration))
7101 (org-remove-occur-highlights nil nil t)
7102 (if (org-at-timestamp-p t)
7103 (org-follow-timestamp-link)
7104 (let (type path link line search (pos (point)))
7107 (skip-chars-forward "^]\n\r")
7108 (when (org-in-regexp org-bracket-link-regexp)
7109 (setq link (org-link-unescape (org-match-string-no-properties 1)))
7110 (while (string-match " *\n *" link)
7111 (setq link (replace-match " " t t link)))
7112 (setq link (org-link-expand-abbrev link))
7113 (if (string-match org-link-re-with-space2 link)
7114 (setq type (match-string 1 link) path (match-string 2 link))
7115 (setq type "thisfile" path link))
7118 (when (get-text-property (point) 'org-linked-text)
7119 (setq type "thisfile"
7120 pos (if (get-text-property (1+ (point)) 'org-linked-text)
7121 (1+ (point)) (point))
7122 path (buffer-substring
7123 (previous-single-property-change pos 'org-linked-text)
7124 (next-single-property-change pos 'org-linked-text)))
7128 (when (or (org-in-regexp org-angle-link-re)
7129 (org-in-regexp org-plain-link-re))
7130 (setq type (match-string 1) path (match-string 2))
7132 (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
7133 (setq type "tree-match"
7134 path (match-string 1))
7137 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
7139 path (match-string 1))
7140 (while (string-match ":" path)
7141 (setq path (replace-match "+" t t path)))
7144 (error "No link found"))
7145 ;; Remove any trailing spaces in path
7146 (if (string-match " +\\'" path)
7147 (setq path (replace-match "" t t path)))
7151 ((assoc type org-link-protocols)
7152 (funcall (nth 1 (assoc type org-link-protocols)) path))
7154 ((equal type "mailto")
7155 (let ((cmd (car org-link-mailto-program))
7156 (args (cdr org-link-mailto-program)) args1
7157 (address path) (subject "") a)
7158 (if (string-match "\\(.*\\)::\\(.*\\)" path)
7159 (setq address (match-string 1 path)
7160 subject (org-link-escape (match-string 2 path))))
7163 ((not (stringp (car args))) (push (pop args) args1))
7164 (t (setq a (pop args))
7165 (if (string-match "%a" a)
7166 (setq a (replace-match address t t a)))
7167 (if (string-match "%s" a)
7168 (setq a (replace-match subject t t a)))
7170 (apply cmd (nreverse args1))))
7172 ((member type '("http" "https" "ftp" "news"))
7173 (browse-url (concat type ":" (org-link-escape
7174 path org-link-escape-chars-browser))))
7176 ((member type '("message"))
7177 (browse-url (concat type ":" path)))
7179 ((string= type "tags")
7180 (org-tags-view in-emacs path))
7181 ((string= type "thisfile")
7183 (switch-to-buffer-other-window
7184 (org-get-buffer-for-internal-link (current-buffer)))
7185 (org-mark-ring-push))
7186 (let ((cmd `(org-link-search
7188 ,(cond ((equal in-emacs '(4)) 'occur)
7189 ((equal in-emacs '(16)) 'org-occur)
7192 (condition-case nil (eval cmd)
7193 (error (progn (widen) (eval cmd))))))
7195 ((string= type "tree-match")
7196 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
7198 ((string= type "file")
7199 (if (string-match "::\\([0-9]+\\)\\'" path)
7200 (setq line (string-to-number (match-string 1 path))
7201 path (substring path 0 (match-beginning 0)))
7202 (if (string-match "::\\(.+\\)\\'" path)
7203 (setq search (match-string 1 path)
7204 path (substring path 0 (match-beginning 0)))))
7205 (if (string-match "[*?{]" (file-name-nondirectory path))
7207 (org-open-file path in-emacs line search)))
7209 ((string= type "news")
7211 (org-gnus-follow-link path))
7213 ((string= type "shell")
7215 (if (or (not org-confirm-shell-link-function)
7216 (funcall org-confirm-shell-link-function
7217 (format "Execute \"%s\" in shell? "
7218 (org-add-props cmd nil
7219 'face 'org-warning))))
7221 (message "Executing %s" cmd)
7222 (shell-command cmd))
7225 ((string= type "elisp")
7227 (if (or (not org-confirm-elisp-link-function)
7228 (funcall org-confirm-elisp-link-function
7229 (format "Execute \"%s\" as elisp? "
7230 (org-add-props cmd nil
7231 'face 'org-warning))))
7232 (message "%s => %s" cmd (eval (read cmd)))
7236 (browse-url-at-point)))))
7237 (move-marker org-open-link-marker nil)
7238 (run-hook-with-args 'org-follow-link-hook))
7242 (defun org-get-effort (&optional pom)
7243 "Get the effort estimate for the current entry."
7244 (org-entry-get pom org-effort-property))
7248 (defvar org-create-file-search-functions nil
7249 "List of functions to construct the right search string for a file link.
7250 These functions are called in turn with point at the location to
7251 which the link should point.
7253 A function in the hook should first test if it would like to
7254 handle this file type, for example by checking the major-mode or
7255 the file extension. If it decides not to handle this file, it
7256 should just return nil to give other functions a chance. If it
7257 does handle the file, it must return the search string to be used
7258 when following the link. The search string will be part of the
7259 file link, given after a double colon, and `org-open-at-point'
7260 will automatically search for it. If special measures must be
7261 taken to make the search successful, another function should be
7262 added to the companion hook `org-execute-file-search-functions',
7265 A function in this hook may also use `setq' to set the variable
7266 `description' to provide a suggestion for the descriptive text to
7267 be used for this link when it gets inserted into an Org-mode
7268 buffer with \\[org-insert-link].")
7270 (defvar org-execute-file-search-functions nil
7271 "List of functions to execute a file search triggered by a link.
7273 Functions added to this hook must accept a single argument, the
7274 search string that was part of the file link, the part after the
7275 double colon. The function must first check if it would like to
7276 handle this search, for example by checking the major-mode or the
7277 file extension. If it decides not to handle this search, it
7278 should just return nil to give other functions a chance. If it
7279 does handle the search, it must return a non-nil value to keep
7280 other functions from trying.
7282 Each function can access the current prefix argument through the
7283 variable `current-prefix-argument'. Note that a single prefix is
7284 used to force opening a link in Emacs, so it may be good to only
7285 use a numeric or double prefix to guide the search function.
7287 In case this is needed, a function in this hook can also restore
7288 the window configuration before `org-open-at-point' was called using:
7290 (set-window-configuration org-window-config-before-follow-link)")
7292 (defun org-link-search (s &optional type avoid-pos)
7293 "Search for a link search option.
7294 If S is surrounded by forward slashes, it is interpreted as a
7295 regular expression. In org-mode files, this will create an `org-occur'
7296 sparse tree. In ordinary files, `occur' will be used to list matches.
7297 If the current buffer is in `dired-mode', grep will be used to search
7298 in all files. If AVOID-POS is given, ignore matches near that position."
7299 (let ((case-fold-search t)
7300 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
7301 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
7302 (append '(("") (" ") ("\t") ("\n"))
7306 (pre nil) (post nil)
7307 words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
7309 ;; First check if there are any special
7310 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
7311 ;; Now try the builtin stuff
7313 (goto-char (point-min))
7316 (concat "<<" (regexp-quote s0) ">>") nil t)
7317 (setq type 'dedicated
7318 pos (match-beginning 0))))
7319 ;; There is an exact target for this
7321 ((string-match "^/\\(.*\\)/$" s)
7322 ;; A regular expression
7325 (org-occur (match-string 1 s)))
7326 ;;((eq major-mode 'dired-mode)
7327 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
7328 (t (org-do-occur (match-string 1 s)))))
7330 ;; A normal search strings
7331 (when (equal (string-to-char s) ?*)
7332 ;; Anchor on headlines, post may include tags.
7333 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
7334 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
7336 (remove-text-properties
7338 '(face nil mouse-face nil keymap nil fontified nil) s)
7339 ;; Make a series of regular expressions to find a match
7340 (setq words (org-split-string s "[ \n\r\t]+")
7342 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
7343 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
7345 re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
7346 re2a (concat "[ \t\r\n]" re2a_)
7347 re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
7348 re4 (concat "[^a-zA-Z_]" re4_)
7350 re1 (concat pre re2 post)
7351 re3 (concat pre (if pre re4_ re4) post)
7352 re5 (concat pre ".*" re4)
7353 re2 (concat pre re2)
7354 re2a (concat pre (if pre re2a_ re2a))
7355 re4 (concat pre (if pre re4_ re4))
7356 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
7357 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
7361 ((eq type 'org-occur) (org-occur reall))
7362 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
7363 (t (goto-char (point-min))
7365 (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
7366 (org-search-not-self 1 re1 nil t)
7367 (org-search-not-self 1 re2 nil t)
7368 (org-search-not-self 1 re2a nil t)
7369 (org-search-not-self 1 re3 nil t)
7370 (org-search-not-self 1 re4 nil t)
7371 (org-search-not-self 1 re5 nil t)
7373 (goto-char (match-beginning 1))
7375 (error "No match")))))
7377 ;; Normal string-search
7378 (goto-char (point-min))
7379 (if (search-forward s nil t)
7380 (goto-char (match-beginning 0))
7381 (error "No match"))))
7382 (and (org-mode-p) (org-show-context 'link-search))
7385 (defun org-search-not-self (group &rest args)
7386 "Execute `re-search-forward', but only accept matches that do not
7387 enclose the position of `org-open-link-marker'."
7388 (let ((m org-open-link-marker))
7390 (while (apply 're-search-forward args)
7391 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
7392 (goto-char (match-end group))
7393 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
7394 (> (match-beginning 0) (marker-position m))
7395 (< (match-end 0) (marker-position m)))
7397 (or (not (org-in-regexp
7398 org-bracket-link-analytic-regexp 1))
7399 (not (match-end 4)) ; no description
7400 (and (<= (match-beginning 4) (point))
7401 (>= (match-end 4) (point))))))
7402 (throw 'exit (point))))))))
7404 (defun org-get-buffer-for-internal-link (buffer)
7405 "Return a buffer to be used for displaying the link target of internal links."
7407 ((not org-display-internal-link-with-indirect-buffer)
7409 ((string-match "(Clone)$" (buffer-name buffer))
7410 (message "Buffer is already a clone, not making another one")
7411 ;; we also do not modify visibility in this case
7413 (t ; make a new indirect buffer for displaying the link
7414 (let* ((bn (buffer-name buffer))
7415 (ibn (concat bn "(Clone)"))
7416 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
7417 (with-current-buffer ib (org-overview))
7420 (defun org-do-occur (regexp &optional cleanup)
7421 "Call the Emacs command `occur'.
7422 If CLEANUP is non-nil, remove the printout of the regular expression
7423 in the *Occur* buffer. This is useful if the regex is long and not useful
7427 (let ((cwin (selected-window)) win beg end)
7428 (when (setq win (get-buffer-window "*Occur*"))
7429 (select-window win))
7430 (goto-char (point-min))
7431 (when (re-search-forward "match[a-z]+" nil t)
7432 (setq beg (match-end 0))
7433 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
7434 (setq end (1- (match-beginning 0)))))
7435 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
7436 (goto-char (point-min))
7437 (select-window cwin))))
7439 ;;; The mark ring for links jumps
7441 (defvar org-mark-ring nil
7442 "Mark ring for positions before jumps in Org-mode.")
7443 (defvar org-mark-ring-last-goto nil
7444 "Last position in the mark ring used to go back.")
7445 ;; Fill and close the ring
7446 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
7447 (loop for i from 1 to org-mark-ring-length do
7448 (push (make-marker) org-mark-ring))
7449 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
7452 (defun org-mark-ring-push (&optional pos buffer)
7453 "Put the current position or POS into the mark ring and rotate it."
7455 (setq pos (or pos (point)))
7456 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
7457 (move-marker (car org-mark-ring)
7459 (or buffer (current-buffer)))
7461 (substitute-command-keys
7462 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
7464 (defun org-mark-ring-goto (&optional n)
7465 "Jump to the previous position in the mark ring.
7466 With prefix arg N, jump back that many stored positions. When
7467 called several times in succession, walk through the entire ring.
7468 Org-mode commands jumping to a different position in the current file,
7469 or to another Org-mode file, automatically push the old position
7473 (if (eq last-command this-command)
7474 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
7475 (setq p org-mark-ring))
7476 (setq org-mark-ring-last-goto p)
7478 (switch-to-buffer (marker-buffer m))
7480 (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
7482 (defun org-remove-angle-brackets (s)
7483 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
7484 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
7486 (defun org-add-angle-brackets (s)
7487 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
7488 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
7490 (defun org-remove-double-quotes (s)
7491 (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
7492 (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
7495 ;;; Following specific links
7497 (defun org-follow-timestamp-link ()
7499 ((org-at-date-range-p t)
7500 (let ((org-agenda-start-on-weekday)
7501 (t1 (match-string 1))
7502 (t2 (match-string 2)))
7503 (setq t1 (time-to-days (org-time-string-to-time t1))
7504 t2 (time-to-days (org-time-string-to-time t2)))
7505 (org-agenda-list nil t1 (1+ (- t2 t1)))))
7506 ((org-at-timestamp-p t)
7507 (org-agenda-list nil (time-to-days (org-time-string-to-time
7508 (substring (match-string 1) 0 10)))
7510 (t (error "This should not happen"))))
7513 ;;; Following file links
7514 (defvar org-wait nil)
7515 (defun org-open-file (path &optional in-emacs line search)
7516 "Open the file at PATH.
7517 First, this expands any special file name abbreviations. Then the
7518 configuration variable `org-file-apps' is checked if it contains an
7519 entry for this file type, and if yes, the corresponding command is launched.
7520 If no application is found, Emacs simply visits the file.
7521 With optional argument IN-EMACS, Emacs will visit the file.
7522 Optional LINE specifies a line to go to, optional SEARCH a string to
7523 search for. If LINE or SEARCH is given, the file will always be
7525 If the file does not exist, an error is thrown."
7526 (setq in-emacs (or in-emacs line search))
7527 (let* ((file (if (equal path "")
7529 (substitute-in-file-name (expand-file-name path))))
7530 (apps (append org-file-apps (org-default-apps)))
7531 (remp (and (assq 'remote apps) (org-file-remote-p file)))
7532 (dirp (if remp nil (file-directory-p file)))
7533 (dfile (downcase file))
7534 (old-buffer (current-buffer))
7536 (old-mode major-mode)
7538 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
7539 (setq ext (match-string 1 dfile))
7540 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
7541 (setq ext (match-string 1 dfile))))
7544 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
7545 (and dirp (cdr (assoc 'directory apps)))
7546 (cdr (assoc ext apps))
7547 (cdr (assoc t apps)))))
7548 (when (eq cmd 'mailcap)
7550 (mailcap-parse-mailcaps)
7551 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
7552 (command (mailcap-mime-info mime-type)))
7553 (if (stringp command)
7555 (setq cmd 'emacs))))
7556 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
7557 (not (file-exists-p file))
7558 (not org-open-non-existing-files))
7559 (error "No such file: %s" file))
7561 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
7562 ;; Remove quotes around the file name - we'll use shell-quote-argument.
7563 (while (string-match "['\"]%s['\"]" cmd)
7564 (setq cmd (replace-match "%s" t t cmd)))
7565 (while (string-match "%s" cmd)
7566 (setq cmd (replace-match
7568 (shell-quote-argument
7569 (convert-standard-filename file)))
7571 (save-window-excursion
7572 (start-process-shell-command cmd nil cmd)
7573 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
7577 (funcall (cdr (assq 'file org-link-frame-setup)) file)
7579 (if line (goto-line line)
7580 (if search (org-link-search search))))
7582 (let ((file (convert-standard-filename file)))
7584 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
7585 (and (org-mode-p) (eq old-mode 'org-mode)
7586 (or (not (equal old-buffer (current-buffer)))
7587 (not (equal old-pos (point))))
7588 (org-mark-ring-push old-pos old-buffer))))
7590 (defun org-default-apps ()
7591 "Return the default applications for this operating system."
7593 ((eq system-type 'darwin)
7594 org-file-apps-defaults-macosx)
7595 ((eq system-type 'windows-nt)
7596 org-file-apps-defaults-windowsnt)
7597 (t org-file-apps-defaults-gnu)))
7599 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
7600 (defun org-file-remote-p (file)
7601 "Test whether FILE specifies a location on a remote system.
7602 Return non-nil if the location is indeed remote.
7604 For example, the filename \"/user@host:/foo\" specifies a location
7605 on the system \"/user@host:\"."
7606 (cond ((fboundp 'file-remote-p)
7607 (file-remote-p file))
7608 ((fboundp 'tramp-handle-file-remote-p)
7609 (tramp-handle-file-remote-p file))
7610 ((and (boundp 'ange-ftp-name-format)
7611 (string-match (car ange-ftp-name-format) file))
7618 (defun org-get-org-file ()
7619 "Read a filename, with default directory `org-directory'."
7620 (let ((default (or org-default-notes-file remember-data-file)))
7621 (read-file-name (format "File name [%s]: " default)
7622 (file-name-as-directory org-directory)
7625 (defun org-notes-order-reversed-p ()
7626 "Check if the current file should receive notes in reversed order."
7628 ((not org-reverse-note-order) nil)
7629 ((eq t org-reverse-note-order) t)
7630 ((not (listp org-reverse-note-order)) nil)
7632 (let ((all org-reverse-note-order)
7634 (while (setq entry (pop all))
7635 (if (string-match (car entry) buffer-file-name)
7636 (throw 'exit (cdr entry))))
7639 (defvar org-refile-target-table nil
7640 "The list of refile targets, created by `org-refile'.")
7642 (defvar org-agenda-new-buffers nil
7643 "Buffers created to visit agenda files.")
7645 (defun org-get-refile-targets (&optional default-buffer)
7646 "Produce a table with refile targets."
7647 (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
7648 targets txt re files f desc descre)
7649 (with-current-buffer (or default-buffer (current-buffer))
7650 (while (setq entry (pop entries))
7651 (setq files (car entry) desc (cdr entry))
7653 ((null files) (setq files (list (current-buffer))))
7654 ((eq files 'org-agenda-files)
7655 (setq files (org-agenda-files 'unrestricted)))
7656 ((and (symbolp files) (fboundp files))
7657 (setq files (funcall files)))
7658 ((and (symbolp files) (boundp files))
7659 (setq files (symbol-value files))))
7660 (if (stringp files) (setq files (list files)))
7662 ((eq (car desc) :tag)
7663 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
7664 ((eq (car desc) :todo)
7665 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
7666 ((eq (car desc) :regexp)
7667 (setq descre (cdr desc)))
7668 ((eq (car desc) :level)
7669 (setq descre (concat "^\\*\\{" (number-to-string
7670 (if org-odd-levels-only
7671 (1- (* 2 (cdr desc)))
7674 ((eq (car desc) :maxlevel)
7675 (setq descre (concat "^\\*\\{1," (number-to-string
7676 (if org-odd-levels-only
7677 (1- (* 2 (cdr desc)))
7680 (t (error "Bad refiling target description %s" desc)))
7681 (while (setq f (pop files))
7683 (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
7684 (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
7688 (goto-char (point-min))
7689 (while (re-search-forward descre nil t)
7690 (goto-char (point-at-bol))
7691 (when (looking-at org-complex-heading-regexp)
7692 (setq txt (match-string 4)
7693 re (concat "^" (regexp-quote
7694 (buffer-substring (match-beginning 1)
7696 (if (match-end 5) (setq re (concat re "[ \t]+"
7698 (match-string 5)))))
7699 (setq re (concat re "[ \t]*$"))
7700 (when org-refile-use-outline-path
7701 (setq txt (mapconcat 'identity
7703 (if (eq org-refile-use-outline-path 'file)
7704 (list (file-name-nondirectory
7705 (buffer-file-name (buffer-base-buffer))))
7706 (if (eq org-refile-use-outline-path 'full-file-path)
7707 (list (buffer-file-name (buffer-base-buffer)))))
7708 (org-get-outline-path)
7711 (push (list txt f re (point)) targets))
7712 (goto-char (point-at-eol))))))))
7713 (nreverse targets))))
7715 (defun org-get-outline-path ()
7716 "Return the outline path to the current entry, as a list."
7719 (while (org-up-heading-safe)
7720 (when (looking-at org-complex-heading-regexp)
7721 (push (org-match-string-no-properties 4) rtn)))
7724 (defvar org-refile-history nil
7725 "History for refiling operations.")
7727 (defun org-refile (&optional goto default-buffer)
7728 "Move the entry at point to another heading.
7729 The list of target headings is compiled using the information in
7730 `org-refile-targets', which see. This list is created before each use
7731 and will therefore always be up-to-date.
7733 At the target location, the entry is filed as a subitem of the target heading.
7734 Depending on `org-reverse-note-order', the new subitem will either be the
7735 first of the last subitem.
7737 With prefix arg GOTO, the command will only visit the target location,
7738 not actually move anything.
7739 With a double prefix `C-c C-c', go to the location where the last refiling
7740 operation has put the subtree."
7742 (let* ((cbuf (current-buffer))
7743 (filename (buffer-file-name (buffer-base-buffer cbuf)))
7744 pos it nbuf file re level reversed)
7745 (if (equal goto '(16))
7746 (org-refile-goto-last-stored)
7747 (when (setq it (org-refile-get-location
7748 (if goto "Goto: " "Refile to: ") default-buffer))
7749 (setq file (nth 1 it)
7752 (setq nbuf (or (find-buffer-visiting file)
7753 (find-file-noselect file)))
7756 (switch-to-buffer nbuf)
7758 (org-show-context 'org-goto))
7759 (org-copy-subtree 1 nil t)
7761 (set-buffer (setq nbuf (or (find-buffer-visiting file)
7762 (find-file-noselect file))))
7763 (setq reversed (org-notes-order-reversed-p))
7768 (looking-at outline-regexp)
7769 (setq level (org-get-valid-level (funcall outline-level) 1))
7772 (outline-next-heading)
7773 (or (save-excursion (outline-get-next-sibling))
7774 (org-end-of-subtree t t)
7776 (bookmark-set "org-refile-last-stored")
7777 (org-paste-subtree level))))
7779 (setq org-markers-to-move nil)
7780 (message "Entry refiled to \"%s\"" (car it)))))))
7782 (defun org-refile-goto-last-stored ()
7783 "Go to the location where the last refile was stored."
7785 (bookmark-jump "org-refile-last-stored")
7786 (message "This is the location of the last refile"))
7788 (defun org-refile-get-location (&optional prompt default-buffer)
7789 "Prompt the user for a refile location, using PROMPT."
7790 (let ((org-refile-targets org-refile-targets)
7791 (org-refile-use-outline-path org-refile-use-outline-path))
7792 (setq org-refile-target-table (org-get-refile-targets default-buffer)))
7793 (unless org-refile-target-table
7794 (error "No refile targets"))
7795 (let* ((cbuf (current-buffer))
7796 (cfunc (if org-refile-use-outline-path
7797 'org-olpath-completing-read
7799 (extra (if org-refile-use-outline-path "/" ""))
7800 (filename (buffer-file-name (buffer-base-buffer cbuf)))
7801 (fname (and filename (file-truename filename)))
7804 (if (not (equal fname (file-truename (nth 1 x))))
7805 (cons (concat (car x) extra " ("
7806 (file-name-nondirectory (nth 1 x)) ")")
7808 (cons (concat (car x) extra) (cdr x))))
7809 org-refile-target-table))
7810 (completion-ignore-case t))
7811 (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
7814 (defun org-olpath-completing-read (prompt collection &rest args)
7815 "Read an outline path like a file name."
7816 (let ((thetable collection))
7818 'completing-read prompt
7819 (lambda (string predicate &optional flag)
7820 (let (rtn r s f (l (length string)))
7824 (try-completion string thetable))
7827 (setq rtn (all-completions string thetable predicate))
7830 (setq r (substring x l))
7831 (if (string-match " ([^)]*)$" x)
7832 (setq f (match-string 0 x))
7834 (if (string-match "/" r)
7835 (concat string (substring r 0 (match-end 0)) f)
7840 (assoc string thetable)))
7846 (defun org-find-dblock (name)
7847 "Find the first dynamic block with name NAME in the buffer.
7848 If not found, stay at current position and return nil."
7851 (goto-char (point-min))
7852 (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
7854 (match-beginning 0))))
7855 (if pos (goto-char pos))
7858 (defconst org-dblock-start-re
7859 "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
7860 "Matches the startline of a dynamic block, with parameters.")
7862 (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
7863 "Matches the end of a dyhamic block.")
7865 (defun org-create-dblock (plist)
7866 "Create a dynamic block section, with parameters taken from PLIST.
7867 PLIST must containe a :name entry which is used as name of the block."
7868 (unless (bolp) (newline))
7869 (let ((name (plist-get plist :name)))
7870 (insert "#+BEGIN: " name)
7872 (if (eq (car plist) :name)
7873 (setq plist (cddr plist))
7874 (insert " " (prin1-to-string (pop plist)))))
7875 (insert "\n\n#+END:\n")
7876 (beginning-of-line -2)))
7878 (defun org-prepare-dblock ()
7879 "Prepare dynamic block for refresh.
7880 This empties the block, puts the cursor at the insert position and returns
7881 the property list including an extra property :name with the block name."
7882 (unless (looking-at org-dblock-start-re)
7883 (error "Not at a dynamic block"))
7884 (let* ((begdel (1+ (match-end 0)))
7885 (name (org-no-properties (match-string 1)))
7886 (params (append (list :name name)
7887 (read (concat "(" (match-string 3) ")")))))
7888 (unless (re-search-forward org-dblock-end-re nil t)
7889 (error "Dynamic block not terminated"))
7892 (list :content (buffer-substring
7893 begdel (match-beginning 0)))))
7894 (delete-region begdel (match-beginning 0))
7899 (defun org-map-dblocks (&optional command)
7900 "Apply COMMAND to all dynamic blocks in the current buffer.
7901 If COMMAND is not given, use `org-update-dblock'."
7902 (let ((cmd (or command 'org-update-dblock))
7905 (goto-char (point-min))
7906 (while (re-search-forward org-dblock-start-re nil t)
7907 (goto-char (setq pos (match-beginning 0)))
7910 (error (message "Error during update of dynamic block")))
7912 (unless (re-search-forward org-dblock-end-re nil t)
7913 (error "Dynamic block not terminated"))))))
7915 (defun org-dblock-update (&optional arg)
7916 "User command for updating dynamic blocks.
7917 Update the dynamic block at point. With prefix ARG, update all dynamic
7918 blocks in the buffer."
7921 (org-update-all-dblocks)
7922 (or (looking-at org-dblock-start-re)
7923 (org-beginning-of-dblock))
7924 (org-update-dblock)))
7926 (defun org-update-dblock ()
7927 "Update the dynamic block at point
7928 This means to empty the block, parse for parameters and then call
7929 the correct writing function."
7930 (save-window-excursion
7931 (let* ((pos (point))
7932 (line (org-current-line))
7933 (params (org-prepare-dblock))
7934 (name (plist-get params :name))
7935 (cmd (intern (concat "org-dblock-write:" name))))
7936 (message "Updating dynamic block `%s' at line %d..." name line)
7937 (funcall cmd params)
7938 (message "Updating dynamic block `%s' at line %d...done" name line)
7941 (defun org-beginning-of-dblock ()
7942 "Find the beginning of the dynamic block at point.
7943 Error if there is no scuh block at point."
7947 (if (and (re-search-backward org-dblock-start-re nil t)
7948 (setq beg (match-beginning 0))
7949 (re-search-forward org-dblock-end-re nil t)
7950 (> (match-end 0) pos))
7953 (error "Not in a dynamic block"))))
7955 (defun org-update-all-dblocks ()
7956 "Update all dynamic blocks in the buffer.
7957 This function can be used in a hook."
7959 (org-map-dblocks 'org-update-dblock)))
7964 (defconst org-additional-option-like-keywords
7965 '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
7966 "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
7967 "BEGIN_EXAMPLE" "END_EXAMPLE"))
7969 (defcustom org-structure-template-alist
7971 ("s" "#+begin_src ?\n\n#+end_src"
7972 "<src lang=\"?\">\n\n</src>")
7973 ("e" "#+begin_example\n?\n#+end_example"
7974 "<example>\n?\n</example>")
7975 ("q" "#+begin_quote\n?\n#+end_quote"
7976 "<quote>\n?\n</quote>")
7977 ("v" "#+begin_verse\n?\n#+end_verse"
7978 "<verse>\n?\n/verse>")
7979 ("l" "#+begin_latex\n?\n#+end_latex"
7980 "<literal style=\"latex\">\n?\n</literal>")
7982 "<literal style=\"latex\">?</literal>")
7983 ("h" "#+begin_html\n?\n#+end_html"
7984 "<literal style=\"html\">\n?\n</literal>")
7986 "<literal style=\"html\">?</literal>")
7987 ("a" "#+begin_ascii\n?\n#+end_ascii")
7989 ("i" "#+include %file ?"
7990 "<include file=%file markup=\"?\">")
7992 "Structure completion elements.
7993 This is a list of abbreviation keys and values. The value gets inserted
7994 it you type @samp{.} followed by the key and then the completion key,
7995 usually `M-TAB'. %file will be replaced by a file name after prompting
7996 for the file uning completion.
7997 There are two templates for each key, the first uses the original Org syntax,
7998 the second uses Emacs Muse-like syntax tags. These Muse-like tags become
7999 the default when the /org-mtags.el/ module has been loaded. See also the
8000 variable `org-mtags-prefere-muse-templates'.
8001 This is an experimental feature, it is undecided if it is going to stay in."
8002 :group 'org-completion
8005 (string :tag "Template")
8006 (string :tag "Muse Template")))
8008 (defun org-try-structure-completion ()
8009 "Try to complete a structure template before point.
8010 This looks for strings like \"<e\" on an otherwise empty line and
8012 (let ((l (buffer-substring (point-at-bol) (point)))
8014 (when (and (looking-at "[ \t]*$")
8015 (string-match "^[ \t]*<\\([a-z]+\\)$"l)
8016 (setq a (assoc (match-string 1 l) org-structure-template-alist)))
8017 (org-complete-expand-structure-template (+ -1 (point-at-bol)
8018 (match-beginning 1)) a)
8021 (defun org-complete-expand-structure-template (start cell)
8022 "Expand a structure template."
8023 (let* ((musep (org-bound-and-true-p org-mtags-prefere-muse-templates))
8024 (rpl (nth (if musep 2 1) cell)))
8025 (delete-region start (point))
8026 (when (string-match "\\`#\\+" rpl)
8029 ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
8030 (delete-region (point-at-bol) (point)))
8032 (setq start (point))
8033 (if (string-match "%file" rpl)
8034 (setq rpl (replace-match
8038 (abbreviate-file-name (read-file-name "Include file: ")))
8042 (if (re-search-backward "\\?" start t) (delete-char 1))))
8045 (defun org-complete (&optional arg)
8046 "Perform completion on word at point.
8047 At the beginning of a headline, this completes TODO keywords as given in
8048 `org-todo-keywords'.
8049 If the current word is preceded by a backslash, completes the TeX symbols
8050 that are supported for HTML support.
8051 If the current word is preceded by \"#+\", completes special words for
8052 setting file options.
8053 In the line after \"#+STARTUP:, complete valid keywords.\"
8054 At all other locations, this simply calls the value of
8055 `org-completion-fallback-command'."
8057 (org-without-partial-completion
8061 (beg1 (save-excursion
8062 (skip-chars-backward (org-re "[:alnum:]_@"))
8064 (beg (save-excursion
8065 (skip-chars-backward "a-zA-Z0-9_:$")
8067 (confirm (lambda (x) (stringp (car x))))
8068 (searchhead (equal (char-before beg) ?*))
8070 (when (and (member (char-before beg1) '(?. ?<))
8071 (setq a (assoc (buffer-substring beg1 (point))
8072 org-structure-template-alist)))
8073 (org-complete-expand-structure-template (1- beg1) a)
8075 (tag (and (equal (char-before beg1) ?:)
8076 (equal (char-after (point-at-bol)) ?*)))
8077 (prop (and (equal (char-before beg1) ?:)
8078 (not (equal (char-after (point-at-bol)) ?*))))
8079 (texp (equal (char-before beg) ?\\))
8080 (link (equal (char-before beg) ?\[))
8081 (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
8084 (startup (string-match "^#\\+STARTUP:.*"
8085 (buffer-substring (point-at-bol) (point))))
8086 (completion-ignore-case opt)
8096 (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
8097 (cons (match-string 2 x) (match-string 1 x)))
8098 (org-split-string (org-get-current-options) "\n"))
8099 (mapcar 'list org-additional-option-like-keywords)))
8101 (setq type :startup)
8102 org-startup-options)
8103 (link (append org-link-abbrev-alist-local
8104 org-link-abbrev-alist))
8108 ((string-match "\\`\\*+[ \t]+\\'"
8109 (buffer-substring (point-at-bol) beg))
8111 (mapcar 'list org-todo-keywords-1))
8113 (setq type :searchhead)
8115 (goto-char (point-min))
8116 (while (re-search-forward org-todo-line-regexp nil t)
8118 (org-make-org-heading-search-string
8119 (match-string 3) t))
8122 (tag (setq type :tag beg beg1)
8123 (or org-tag-alist (org-get-buffer-tags)))
8124 (prop (setq type :prop beg beg1)
8125 (mapcar 'list (org-buffer-property-keys nil t t)))
8127 (call-interactively org-completion-fallback-command)
8128 (throw 'exit nil)))))
8129 (pattern (buffer-substring-no-properties beg end))
8130 (completion (try-completion pattern table confirm)))
8131 (cond ((eq completion t)
8132 (if (not (assoc (upcase pattern) table))
8133 (message "Already complete")
8134 (if (and (equal type :opt)
8135 (not (member (car (assoc (upcase pattern) table))
8136 org-additional-option-like-keywords)))
8137 (insert (substring (cdr (assoc (upcase pattern) table))
8139 (if (memq type '(:tag :prop)) (insert ":")))))
8141 (message "Can't find completion for \"%s\"" pattern)
8143 ((not (string= pattern completion))
8144 (delete-region beg end)
8145 (if (string-match " +$" completion)
8146 (setq completion (replace-match "" t t completion)))
8148 (if (get-buffer-window "*Completions*")
8149 (delete-window (get-buffer-window "*Completions*")))
8150 (if (assoc completion table)
8151 (if (eq type :todo) (insert " ")
8152 (if (memq type '(:tag :prop)) (insert ":"))))
8153 (if (and (equal type :opt) (assoc completion table))
8154 (message "%s" (substitute-command-keys
8155 "Press \\[org-complete] again to insert example settings"))))
8157 (message "Making completion list...")
8158 (let ((list (sort (all-completions pattern table confirm)
8160 (with-output-to-temp-buffer "*Completions*"
8162 ;; Protection needed for XEmacs and emacs 21
8163 (display-completion-list list pattern)
8164 (error (display-completion-list list)))))
8165 (message "Making completion list...%s" "done")))))))
8167 ;;;; TODO, DEADLINE, Comments
8169 (defun org-toggle-comment ()
8170 "Change the COMMENT state of an entry."
8173 (org-back-to-heading)
8174 (let (case-fold-search)
8175 (if (looking-at (concat outline-regexp
8176 "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
8177 (replace-match "" t t nil 1)
8178 (if (looking-at outline-regexp)
8180 (goto-char (match-end 0))
8181 (insert org-comment-string " ")))))))
8183 (defvar org-last-todo-state-is-todo nil
8184 "This is non-nil when the last TODO state change led to a TODO state.
8185 If the last change removed the TODO tag or switched to DONE, then
8188 (defvar org-setting-tags nil) ; dynamically skiped
8190 (defun org-parse-local-options (string var)
8191 "Parse STRING for startup setting relevant for variable VAR."
8192 (let ((rtn (symbol-value var))
8195 (if (or (not string) (not (string-match "\\S-" string)))
8197 (setq opts (delq nil (mapcar (lambda (x)
8198 (setq e (assoc x org-startup-options))
8199 (if (eq (nth 1 e) var) e nil))
8200 (org-split-string string "[ \t]+"))))
8204 (while (setq e (pop opts))
8206 (setq rtn (nth 2 e))
8207 (if (not (listp rtn)) (setq rtn nil))
8208 (push (nth 2 e) rtn)))
8211 (defvar org-blocker-hook nil
8212 "Hook for functions that are allowed to block a state change.
8214 Each function gets as its single argument a property list, see
8215 `org-trigger-hook' for more information about this list.
8217 If any of the functions in this hook returns nil, the state change
8220 (defvar org-trigger-hook nil
8221 "Hook for functions that are triggered by a state change.
8223 Each function gets as its single argument a property list with at least
8224 the following elements:
8226 (:type type-of-change :position pos-at-entry-start
8227 :from old-state :to new-state)
8229 Depending on the type, more properties may be present.
8231 This mechanism is currently implemented for:
8235 :type todo-state-change
8236 :from previous state (keyword as a string), or nil
8237 :to new state (keyword as a string), or nil")
8240 (defun org-todo (&optional arg)
8241 "Change the TODO state of an item.
8242 The state of an item is given by a keyword at the start of the heading,
8244 *** TODO Write paper
8247 The different keywords are specified in the variable `org-todo-keywords'.
8248 By default the available states are \"TODO\" and \"DONE\".
8249 So for this example: when the item starts with TODO, it is changed to DONE.
8250 When it starts with DONE, the DONE is removed. And when neither TODO nor
8251 DONE are present, add TODO at the beginning of the heading.
8253 With C-u prefix arg, use completion to determine the new state.
8254 With numeric prefix arg, switch to that state.
8256 For calling through lisp, arg is also interpreted in the following way:
8257 'none -> empty state
8258 \"\"(empty string) -> switch to empty state
8259 'done -> switch to DONE
8260 'nextset -> switch to the next set of keywords
8261 'previousset -> switch to the previous set of keywords
8262 \"WAITING\" -> switch to the specified keyword, but only if it
8263 really is a member of `org-todo-keywords'."
8267 (org-back-to-heading)
8268 (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
8269 (or (looking-at (concat " +" org-todo-regexp " *"))
8271 (let* ((match-data (match-data))
8272 (startpos (point-at-bol))
8273 (logging (save-match-data (org-entry-get nil "LOGGING" t)))
8274 (org-log-done org-log-done)
8275 (org-log-repeat org-log-repeat)
8276 (org-todo-log-states org-todo-log-states)
8277 (this (match-string 1))
8278 (hl-pos (match-beginning 0))
8279 (head (org-get-todo-sequence-head this))
8280 (ass (assoc head org-todo-kwd-alist))
8281 (interpret (nth 1 ass))
8282 (done-word (nth 3 ass))
8283 (final-done-word (nth 4 ass))
8284 (last-state (or this ""))
8285 (completion-ignore-case t)
8286 (member (member this org-todo-keywords-1))
8289 ((and org-todo-key-trigger
8290 (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
8291 (and (not arg) org-use-fast-todo-selection
8292 (not (eq org-use-fast-todo-selection 'prefix)))))
8293 ;; Use fast selection
8294 (org-fast-todo-selection))
8295 ((and (equal arg '(4))
8296 (or (not org-use-fast-todo-selection)
8297 (not org-todo-key-trigger)))
8298 ;; Read a state with completion
8299 (completing-read "State: " (mapcar (lambda(x) (list x))
8300 org-todo-keywords-1)
8304 (if tail (car tail) nil)
8305 (car org-todo-keywords-1)))
8307 (if (equal member org-todo-keywords-1)
8310 (nth (- (length org-todo-keywords-1) (length tail) 2)
8311 org-todo-keywords-1)
8312 (org-last org-todo-keywords-1))))
8313 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
8314 (setq arg nil))) ; hack to fall back to cycling
8316 ;; user or caller requests a specific state
8318 ((equal arg "") nil)
8319 ((eq arg 'none) nil)
8320 ((eq arg 'done) (or done-word (car org-done-keywords)))
8322 (or (car (cdr (member head org-todo-heads)))
8323 (car org-todo-heads)))
8324 ((eq arg 'previousset)
8325 (let ((org-todo-heads (reverse org-todo-heads)))
8326 (or (car (cdr (member head org-todo-heads)))
8327 (car org-todo-heads))))
8328 ((car (member arg org-todo-keywords-1)))
8329 ((nth (1- (prefix-numeric-value arg))
8330 org-todo-keywords-1))))
8331 ((null member) (or head (car org-todo-keywords-1)))
8332 ((equal this final-done-word) nil) ;; -> make empty
8333 ((null tail) nil) ;; -> first entry
8334 ((eq interpret 'sequence)
8336 ((memq interpret '(type priority))
8337 (if (eq this-command last-command)
8339 (if (> (length tail) 0)
8340 (or done-word (car org-done-keywords))
8343 (next (if state (concat " " state " ") " "))
8344 (change-plist (list :type 'todo-state-change :from this :to state
8345 :position startpos))
8347 (when org-blocker-hook
8348 (unless (save-excursion
8350 (run-hook-with-args-until-failure
8351 'org-blocker-hook change-plist)))
8353 (error "TODO state change from %s to %s blocked" this state)
8355 (message "TODO state change from %s to %s blocked" this state)
8356 (throw 'exit nil))))
8357 (store-match-data match-data)
8358 (replace-match next t t)
8359 (unless (pos-visible-in-window-p hl-pos)
8360 (message "TODO state changed to %s" (org-trim next)))
8362 (setq head (org-get-todo-sequence-head state)
8363 ass (assoc head org-todo-kwd-alist)
8364 interpret (nth 1 ass)
8365 done-word (nth 3 ass)
8366 final-done-word (nth 4 ass)))
8367 (when (memq arg '(nextset previousset))
8368 (message "Keyword-Set %d/%d: %s"
8369 (- (length org-todo-sets) -1
8370 (length (memq (assoc state org-todo-sets) org-todo-sets)))
8371 (length org-todo-sets)
8372 (mapconcat 'identity (assoc state org-todo-sets) " ")))
8373 (setq org-last-todo-state-is-todo
8374 (not (member state org-done-keywords)))
8375 (setq now-done-p (and (member state org-done-keywords)
8376 (not (member this org-done-keywords))))
8377 (and logging (org-local-logging logging))
8378 (when (and (or org-todo-log-states org-log-done)
8379 (not (memq arg '(nextset previousset))))
8380 ;; we need to look at recording a time and note
8381 (setq dolog (or (nth 1 (assoc state org-todo-log-states))
8382 (nth 2 (assoc this org-todo-log-states))))
8384 (member state org-not-done-keywords)
8385 (not (member this org-not-done-keywords)))
8386 ;; This is now a todo state and was not one before
8387 ;; If there was a CLOSED time stamp, get rid of it.
8388 (org-add-planning-info nil nil 'closed))
8389 (when (and now-done-p org-log-done)
8390 ;; It is now done, and it was not done before
8391 (org-add-planning-info 'closed (org-current-time))
8392 (if (and (not dolog) (eq 'note org-log-done))
8393 (org-add-log-setup 'done state 'findpos 'note)))
8394 (when (and state dolog)
8395 ;; This is a non-nil state, and we need to log it
8396 (org-add-log-setup 'state state 'findpos dolog)))
8397 ;; Fixup tag positioning
8398 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
8399 (when org-provide-todo-statistics
8400 (org-update-parent-todo-statistics))
8401 (run-hooks 'org-after-todo-state-change-hook)
8402 (if (and arg (not (member state org-done-keywords)))
8403 (setq head (org-get-todo-sequence-head state)))
8404 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
8405 ;; Do we need to trigger a repeat?
8406 (when now-done-p (org-auto-repeat-maybe state))
8407 ;; Fixup cursor location if close to the keyword
8408 (if (and (outline-on-heading-p)
8410 (save-excursion (beginning-of-line 1)
8411 (looking-at org-todo-line-regexp))
8412 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
8414 (goto-char (or (match-end 2) (match-end 1)))
8416 (when org-trigger-hook
8418 (run-hook-with-args 'org-trigger-hook change-plist)))))))
8420 (defun org-update-parent-todo-statistics ()
8421 "Update any statistics cookie in the parent of the current headline."
8423 (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
8424 level (cnt-all 0) (cnt-done 0) is-percent kwd)
8427 (setq level (org-up-heading-safe))
8429 (re-search-forward box-re (point-at-eol) t))
8431 (setq is-percent (match-end 2))
8433 (unless (outline-next-heading) (throw 'exit nil))
8434 (while (looking-at org-todo-line-regexp)
8435 (setq kwd (match-string 2))
8436 (and kwd (setq cnt-all (1+ cnt-all)))
8437 (and (member kwd org-done-keywords)
8438 (setq cnt-done (1+ cnt-done)))
8440 (outline-forward-same-level 1)
8441 (error (end-of-line 1)))))
8444 (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
8445 (format "[%d/%d]" cnt-done cnt-all)))
8446 (run-hook-with-args 'org-after-todo-statistics-hook
8447 cnt-done (- cnt-all cnt-done))))))
8449 (defvar org-after-todo-statistics-hook nil
8450 "Hook that is called after a TODO statistics cookie has been updated.
8451 Each function is called with two arguments: the number of not-done entries
8452 and the number of done entries.
8454 For example, the following function, when added to this hook, will switch
8455 an entry to DONE when all children are done, and back to TODO when new
8456 entries are set to a TODO status. Note that this hook is only called
8457 when there is a statistics cookie in the headline!
8459 (defun org-summary-todo (n-done n-not-done)
8460 \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
8461 (let (org-log-done org-log-states) ; turn off logging
8462 (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
8465 (defun org-local-logging (value)
8466 "Get logging settings from a property VALUE."
8468 ;; directly set the variables, they are already local.
8469 (setq org-log-done nil
8471 org-todo-log-states nil)
8472 (setq words (org-split-string value))
8473 (while (setq w (pop words))
8475 ((setq a (assoc w org-startup-options))
8476 (and (member (nth 1 a) '(org-log-done org-log-repeat))
8477 (set (nth 1 a) (nth 2 a))))
8478 ((setq a (org-extract-log-state-settings w))
8479 (and (member (car a) org-todo-keywords-1)
8480 (push a org-todo-log-states)))))))
8482 (defun org-get-todo-sequence-head (kwd)
8483 "Return the head of the TODO sequence to which KWD belongs.
8484 If KWD is not set, check if there is a text property remembering the
8489 (or (get-text-property (point-at-bol) 'org-todo-head)
8491 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
8492 nil (point-at-eol)))
8493 (get-text-property p 'org-todo-head))))
8494 ((not (member kwd org-todo-keywords-1))
8495 (car org-todo-keywords-1))
8496 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
8498 (defun org-fast-todo-selection ()
8499 "Fast TODO keyword selection with single keys.
8500 Returns the new TODO keyword, or nil if no state change should occur."
8501 (let* ((fulltable org-todo-key-alist)
8502 (done-keywords org-done-keywords) ;; needed for the faces.
8503 (maxlen (apply 'max (mapcar
8505 (if (stringp (car x)) (string-width (car x)) 0))
8508 (fwidth (+ maxlen 3 1 3))
8509 (ncol (/ (- (window-width) 4) fwidth))
8512 (save-window-excursion
8514 (set-buffer (get-buffer-create " *Org todo*"))
8515 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
8517 (org-set-local 'org-done-keywords done-keywords)
8518 (setq tbl fulltable cnt 0)
8519 (while (setq e (pop tbl))
8521 ((equal e '(:startgroup))
8522 (push '() groups) (setq ingroup t)
8523 (when (not (= cnt 0))
8527 ((equal e '(:endgroup))
8528 (setq ingroup nil cnt 0)
8531 (setq tg (car e) c (cdr e))
8532 (if ingroup (push tg (car groups)))
8533 (setq tg (org-add-props tg nil 'face
8534 (org-get-todo-face tg)))
8535 (if (and (= cnt 0) (not ingroup)) (insert " "))
8536 (insert "[" c "] " tg (make-string
8537 (- fwidth 4 (length tg)) ?\ ))
8538 (when (= (setq cnt (1+ cnt)) ncol)
8540 (if ingroup (insert " "))
8543 (goto-char (point-min))
8544 (if (and (not expert) (fboundp 'fit-window-to-buffer))
8545 (fit-window-to-buffer))
8546 (message "[a-z..]:Set [SPC]:clear")
8547 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
8550 (and (= c ?q) (not (rassoc c fulltable))))
8553 ((setq e (rassoc c fulltable) tg (car e))
8555 (t (setq quit-flag t))))))
8557 (defun org-entry-is-todo-p ()
8558 (member (org-get-todo-state) org-not-done-keywords))
8560 (defun org-entry-is-done-p ()
8561 (member (org-get-todo-state) org-done-keywords))
8563 (defun org-get-todo-state ()
8565 (org-back-to-heading t)
8566 (and (looking-at org-todo-line-regexp)
8570 (defun org-at-date-range-p (&optional inactive-ok)
8571 "Is the cursor inside a date range?"
8575 (let ((pos (point)))
8576 (skip-chars-backward "^[<\r\n")
8577 (skip-chars-backward "<[")
8578 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
8579 (>= (match-end 0) pos)
8581 (skip-chars-backward "^<[\r\n")
8582 (skip-chars-backward "<[")
8583 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
8584 (>= (match-end 0) pos)
8588 (defun org-get-repeat ()
8589 "Check if tere is a deadline/schedule with repeater in this entry."
8592 (org-back-to-heading t)
8593 (if (re-search-forward
8594 org-repeat-re (save-excursion (outline-next-heading) (point)) t)
8595 (match-string 1)))))
8597 (defvar org-last-changed-timestamp)
8598 (defvar org-log-post-message)
8599 (defvar org-log-note-purpose)
8600 (defvar org-log-note-how)
8601 (defun org-auto-repeat-maybe (done-word)
8602 "Check if the current headline contains a repeated deadline/schedule.
8603 If yes, set TODO state back to what it was and change the base date
8604 of repeating deadline/scheduled time stamps to new date.
8605 This function is run automatically after each state change to a DONE state."
8606 ;; last-state is dynamically scoped into this function
8607 (let* ((repeat (org-get-repeat))
8608 (aa (assoc last-state org-todo-kwd-alist))
8609 (interpret (nth 1 aa))
8611 (whata '(("d" . day) ("m" . month) ("y" . year)))
8612 (msg "Entry repeats: ")
8614 (org-todo-log-states nil)
8615 (nshiftmax 10) (nshift 0)
8616 re type n what ts mb0 time)
8618 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
8619 (org-todo (if (eq interpret 'type) last-state head))
8620 (when org-log-repeat
8621 (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
8622 (memq 'org-add-log-note post-command-hook))
8623 ;; OK, we are already setup for some record
8624 (if (eq org-log-repeat 'note)
8625 ;; make sure we take a note, not only a time stamp
8626 (setq org-log-note-how 'note))
8627 ;; Set up for taking a record
8628 (org-add-log-setup 'state (or done-word (car org-done-keywords))
8629 'findpos org-log-repeat)))
8630 (org-back-to-heading t)
8631 (org-add-planning-info nil nil 'closed)
8632 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
8633 org-deadline-time-regexp "\\)\\|\\("
8634 org-ts-regexp "\\)"))
8635 (while (re-search-forward
8636 re (save-excursion (outline-next-heading) (point)) t)
8637 (setq type (if (match-end 1) org-scheduled-string
8638 (if (match-end 3) org-deadline-string "Plain:"))
8639 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
8640 mb0 (match-beginning 0))
8641 (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
8642 (setq n (string-to-number (match-string 2 ts))
8643 what (match-string 3 ts))
8644 (if (equal what "w") (setq n (* n 7) what "d"))
8645 ;; Preparation, see if we need to modify the start date for the change
8647 (setq time (save-match-data (org-time-string-to-time ts)))
8649 ((equal (match-string 1 ts) ".")
8650 ;; Shift starting date to today
8651 (org-timestamp-change
8652 (- (time-to-days (current-time)) (time-to-days time))
8654 ((equal (match-string 1 ts) "+")
8655 (while (or (= nshift 0)
8656 (<= (time-to-days time) (time-to-days (current-time))))
8657 (when (= (incf nshift) nshiftmax)
8658 (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
8660 (org-timestamp-change n (cdr (assoc what whata)))
8661 (org-at-timestamp-p t)
8662 (setq ts (match-string 1))
8663 (setq time (save-match-data (org-time-string-to-time ts))))
8664 (org-timestamp-change (- n) (cdr (assoc what whata)))
8665 ;; rematch, so that we have everything in place for the real shift
8666 (org-at-timestamp-p t)
8667 (setq ts (match-string 1))
8668 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
8669 (org-timestamp-change n (cdr (assoc what whata)))
8670 (setq msg (concat msg type org-last-changed-timestamp " "))))
8671 (setq org-log-post-message msg)
8672 (message "%s" msg))))
8674 (defun org-show-todo-tree (arg)
8675 "Make a compact tree which shows all headlines marked with TODO.
8676 The tree will show the lines where the regexp matches, and all higher
8677 headlines above the match.
8678 With a \\[universal-argument] prefix, also show the DONE entries.
8679 With a numeric prefix N, construct a sparse tree for the Nth element
8680 of `org-todo-keywords-1'."
8682 (let ((case-fold-search nil)
8684 (cond ((null arg) org-not-done-regexp)
8686 (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
8687 (mapcar 'list org-todo-keywords-1))))
8689 (mapconcat 'identity (org-split-string kwd "|") "\\|")
8691 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
8692 (regexp-quote (nth (1- (prefix-numeric-value arg))
8693 org-todo-keywords-1)))
8694 (t (error "Invalid prefix argument: %s" arg)))))
8695 (message "%d TODO entries found"
8696 (org-occur (concat "^" outline-regexp " *" kwd-re )))))
8698 (defun org-deadline (&optional remove time)
8699 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
8700 With argument REMOVE, remove any deadline from the item.
8701 When TIME is set, it should be an internal time specification, and the
8702 scheduling will use the corresponding date."
8706 (org-remove-timestamp-with-keyword org-deadline-string)
8707 (message "Item no longer has a deadline."))
8708 (if (org-get-repeat)
8709 (error "Cannot change deadline on task with repeater, please do that by hand")
8710 (org-add-planning-info 'deadline time 'closed)
8711 (message "Deadline on %s" org-last-inserted-timestamp))))
8713 (defun org-schedule (&optional remove time)
8714 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
8715 With argument REMOVE, remove any scheduling date from the item.
8716 When TIME is set, it should be an internal time specification, and the
8717 scheduling will use the corresponding date."
8721 (org-remove-timestamp-with-keyword org-scheduled-string)
8722 (message "Item is no longer scheduled."))
8723 (if (org-get-repeat)
8724 (error "Cannot reschedule task with repeater, please do that by hand")
8725 (org-add-planning-info 'scheduled time 'closed)
8726 (message "Scheduled to %s" org-last-inserted-timestamp))))
8728 (defun org-remove-timestamp-with-keyword (keyword)
8729 "Remove all time stamps with KEYWORD in the current entry."
8730 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
8733 (org-back-to-heading t)
8735 (org-end-of-subtree t t)
8736 (while (re-search-backward re beg t)
8738 (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
8739 (equal (char-before) ?\ ))
8740 (backward-delete-char 1)
8741 (if (string-match "^[ \t]*$" (buffer-substring
8742 (point-at-bol) (point-at-eol)))
8743 (delete-region (point-at-bol)
8744 (min (point-max) (1+ (point-at-eol))))))))))
8746 (defun org-add-planning-info (what &optional time &rest remove)
8747 "Insert new timestamp with keyword in the line directly after the headline.
8748 WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
8749 If non is given, the user is prompted for a date.
8750 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
8753 (let (org-time-was-given org-end-time-was-given ts
8754 end default-time default-input)
8756 (when (and (not time) (memq what '(scheduled deadline)))
8757 ;; Try to get a default date/time from existing timestamp
8759 (org-back-to-heading t)
8760 (setq end (save-excursion (outline-next-heading) (point)))
8761 (when (re-search-forward (if (eq what 'scheduled)
8762 org-scheduled-time-regexp
8763 org-deadline-time-regexp)
8765 (setq ts (match-string 1)
8767 (apply 'encode-time (org-parse-time-string ts))
8768 default-input (and ts (org-get-compact-tod ts))))))
8770 ;; If necessary, get the time from the user
8771 (setq time (or time (org-read-date nil 'to-time nil nil
8772 default-time default-input))))
8774 (when (and org-insert-labeled-timestamps-at-point
8775 (member what '(scheduled deadline)))
8777 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
8778 (org-insert-time-stamp time org-time-was-given
8779 nil nil nil (list org-end-time-was-given))
8783 (let (col list elt ts buffer-invisibility-spec)
8784 (org-back-to-heading t)
8785 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
8786 (goto-char (match-end 1))
8787 (setq col (current-column))
8788 (goto-char (match-end 0))
8789 (if (eobp) (insert "\n") (forward-char 1))
8790 (if (and (not (looking-at outline-regexp))
8791 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
8793 (not (equal (match-string 1) org-clock-string)))
8794 (narrow-to-region (match-beginning 0) (match-end 0))
8795 (insert-before-markers "\n")
8797 (narrow-to-region (point) (point))
8798 (and org-adapt-indentation (org-indent-to-column col)))
8799 ;; Check if we have to remove something.
8800 (setq list (cons what remove))
8802 (setq elt (pop list))
8803 (goto-char (point-min))
8804 (when (or (and (eq elt 'scheduled)
8805 (re-search-forward org-scheduled-time-regexp nil t))
8806 (and (eq elt 'deadline)
8807 (re-search-forward org-deadline-time-regexp nil t))
8808 (and (eq elt 'closed)
8809 (re-search-forward org-closed-time-regexp nil t)))
8811 (if (looking-at "--+<[^>]+>") (replace-match ""))
8812 (if (looking-at " +") (replace-match ""))))
8813 (goto-char (point-max))
8816 (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
8817 (cond ((eq what 'scheduled) org-scheduled-string)
8818 ((eq what 'deadline) org-deadline-string)
8819 ((eq what 'closed) org-closed-string))
8821 (setq ts (org-insert-time-stamp
8823 (or org-time-was-given
8824 (and (eq what 'closed) org-log-done-with-time))
8826 nil nil (list org-end-time-was-given)))
8828 (goto-char (point-min))
8830 (if (and (looking-at "[ \t]+\n")
8831 (equal (char-before) ?\n))
8832 (delete-region (1- (point)) (point-at-eol)))
8835 (defvar org-log-note-marker (make-marker))
8836 (defvar org-log-note-purpose nil)
8837 (defvar org-log-note-state nil)
8838 (defvar org-log-note-how nil)
8839 (defvar org-log-note-window-configuration nil)
8840 (defvar org-log-note-return-to (make-marker))
8841 (defvar org-log-post-message nil
8842 "Message to be displayed after a log note has been stored.
8843 The auto-repeater uses this.")
8845 (defun org-add-note ()
8846 "Add a note to the current entry.
8847 This is done in the same way as adding a state change note."
8849 (org-add-log-setup 'note nil t nil))
8851 (defun org-add-log-setup (&optional purpose state findpos how)
8852 "Set up the post command hook to take a note.
8853 If this is about to TODO state change, the new state is expected in STATE.
8854 When FINDPOS is non-nil, find the correct position for the note in
8855 the current entry. If not, assume that it can be inserted at point."
8858 (org-back-to-heading t)
8859 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
8860 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
8862 (goto-char (match-end 0))
8863 (unless org-log-states-order-reversed
8864 (and (= (char-after) ?\n) (forward-char 1))
8865 (org-skip-over-state-notes)
8866 (skip-chars-backward " \t\n\r")))
8867 (move-marker org-log-note-marker (point))
8868 (setq org-log-note-purpose purpose
8869 org-log-note-state state
8870 org-log-note-how how)
8871 (add-hook 'post-command-hook 'org-add-log-note 'append)))
8873 (defun org-skip-over-state-notes ()
8874 "Skip past the list of State notes in an entry."
8875 (if (looking-at "\n[ \t]*- State") (forward-char 1))
8876 (while (looking-at "[ \t]*- State")
8879 (error (org-end-of-item)))))
8881 (defun org-add-log-note (&optional purpose)
8882 "Pop up a window for taking a note, and add this note later at point."
8883 (remove-hook 'post-command-hook 'org-add-log-note)
8884 (setq org-log-note-window-configuration (current-window-configuration))
8885 (delete-other-windows)
8886 (move-marker org-log-note-return-to (point))
8887 (switch-to-buffer (marker-buffer org-log-note-marker))
8888 (goto-char org-log-note-marker)
8889 (org-switch-to-buffer-other-window "*Org Note*")
8891 (if (memq org-log-note-how '(time state))
8892 (org-store-log-note)
8893 (let ((org-inhibit-startup t)) (org-mode))
8894 (insert (format "# Insert note for %s.
8895 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
8897 ((eq org-log-note-purpose 'clock-out) "stopped clock")
8898 ((eq org-log-note-purpose 'done) "closed todo item")
8899 ((eq org-log-note-purpose 'state)
8900 (format "state change to \"%s\"" org-log-note-state))
8901 ((eq org-log-note-purpose 'note)
8903 (t (error "This should not happen")))))
8904 (org-set-local 'org-finish-function 'org-store-log-note)))
8906 (defvar org-note-abort nil) ; dynamically scoped
8907 (defun org-store-log-note ()
8908 "Finish taking a log note, and insert it to where it belongs."
8909 (let ((txt (buffer-string))
8910 (note (cdr (assq org-log-note-purpose org-log-note-headings)))
8912 (kill-buffer (current-buffer))
8913 (while (string-match "\\`#.*\n[ \t\n]*" txt)
8914 (setq txt (replace-match "" t t txt)))
8915 (if (string-match "\\s-+\\'" txt)
8916 (setq txt (replace-match "" t t txt)))
8917 (setq lines (org-split-string txt "\n"))
8918 (when (and note (string-match "\\S-" note))
8920 (org-replace-escapes
8922 (list (cons "%u" (user-login-name))
8923 (cons "%U" user-full-name)
8924 (cons "%t" (format-time-string
8925 (org-time-stamp-format 'long 'inactive)
8927 (cons "%s" (if org-log-note-state
8928 (concat "\"" org-log-note-state "\"")
8930 (if lines (setq note (concat note " \\\\")))
8932 (when (or current-prefix-arg org-note-abort) (setq lines nil))
8935 (set-buffer (marker-buffer org-log-note-marker))
8937 (goto-char org-log-note-marker)
8938 (move-marker org-log-note-marker nil)
8940 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
8941 (indent-relative nil)
8942 (insert "- " (pop lines))
8943 (org-indent-line-function)
8944 (beginning-of-line 1)
8945 (looking-at "[ \t]*")
8946 (setq ind (concat (match-string 0) " "))
8948 (while lines (insert "\n" ind (pop lines)))))))
8949 (set-window-configuration org-log-note-window-configuration)
8950 (with-current-buffer (marker-buffer org-log-note-return-to)
8951 (goto-char org-log-note-return-to))
8952 (move-marker org-log-note-return-to nil)
8953 (and org-log-post-message (message "%s" org-log-post-message)))
8955 (defun org-sparse-tree (&optional arg)
8956 "Create a sparse tree, prompt for the details.
8957 This command can create sparse trees. You first need to select the type
8958 of match used to create the tree:
8960 t Show entries with a specific TODO keyword.
8961 T Show entries selected by a tags match.
8962 p Enter a property name and its value (both with completion on existing
8963 names/values) and show entries with that property.
8964 r Show entries matching a regular expression
8965 d Show deadlines due within `org-deadline-warning-days'."
8967 (let (ans kwd value)
8968 (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
8969 (setq ans (read-char-exclusive))
8972 (call-interactively 'org-check-deadlines))
8974 (call-interactively 'org-check-before-date))
8976 (org-show-todo-tree '(4)))
8978 (call-interactively 'org-tags-sparse-tree))
8979 ((member ans '(?p ?P))
8980 (setq kwd (completing-read "Property: "
8981 (mapcar 'list (org-buffer-property-keys))))
8982 (setq value (completing-read "Value: "
8983 (mapcar 'list (org-property-values kwd))))
8984 (unless (string-match "\\`{.*}\\'" value)
8985 (setq value (concat "\"" value "\"")))
8986 (org-tags-sparse-tree arg (concat kwd "=" value)))
8987 ((member ans '(?r ?R ?/))
8988 (call-interactively 'org-occur))
8989 (t (error "No such sparse tree command \"%c\"" ans)))))
8991 (defvar org-occur-highlights nil
8992 "List of overlays used for occur matches.")
8993 (make-variable-buffer-local 'org-occur-highlights)
8994 (defvar org-occur-parameters nil
8995 "Parameters of the active org-occur calls.
8996 This is a list, each call to org-occur pushes as cons cell,
8997 containing the regular expression and the callback, onto the list.
8998 The list can contain several entries if `org-occur' has been called
8999 several time with the KEEP-PREVIOUS argument. Otherwise, this list
9000 will only contain one set of parameters. When the highlights are
9001 removed (for example with `C-c C-c', or with the next edit (depending
9002 on `org-remove-highlights-with-change'), this variable is emptied
9004 (make-variable-buffer-local 'org-occur-parameters)
9006 (defun org-occur (regexp &optional keep-previous callback)
9007 "Make a compact tree which shows all matches of REGEXP.
9008 The tree will show the lines where the regexp matches, and all higher
9009 headlines above the match. It will also show the heading after the match,
9010 to make sure editing the matching entry is easy.
9011 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
9012 call to `org-occur' will be kept, to allow stacking of calls to this
9014 If CALLBACK is non-nil, it is a function which is called to confirm
9015 that the match should indeed be shown."
9016 (interactive "sRegexp: \nP")
9017 (unless keep-previous
9018 (org-remove-occur-highlights nil nil t))
9019 (push (cons regexp callback) org-occur-parameters)
9022 (goto-char (point-min))
9023 (if (or (not keep-previous) ; do not want to keep
9024 (not org-occur-highlights)) ; no previous matches
9027 (while (re-search-forward regexp nil t)
9028 (when (or (not callback)
9029 (save-match-data (funcall callback)))
9031 (when org-highlight-sparse-tree-matches
9032 (org-highlight-new-match (match-beginning 0) (match-end 0)))
9033 (org-show-context 'occur-tree))))
9034 (when org-remove-highlights-with-change
9035 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
9037 (unless org-sparse-tree-open-archived-trees
9038 (org-hide-archived-subtrees (point-min) (point-max)))
9039 (run-hooks 'org-occur-hook)
9041 (message "%d match(es) for regexp %s" cnt regexp))
9044 (defun org-show-context (&optional key)
9045 "Make sure point and context and visible.
9046 How much context is shown depends upon the variables
9047 `org-show-hierarchy-above', `org-show-following-heading'. and
9048 `org-show-siblings'."
9049 (let ((heading-p (org-on-heading-p t))
9050 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
9051 (following-p (org-get-alist-option org-show-following-heading key))
9052 (entry-p (org-get-alist-option org-show-entry-below key))
9053 (siblings-p (org-get-alist-option org-show-siblings key)))
9055 ;; Show heading or entry text
9056 (if (and heading-p (not entry-p))
9057 (org-flag-heading nil) ; only show the heading
9058 (and (or entry-p (org-invisible-p) (org-invisible-p2))
9059 (org-show-hidden-entry))) ; show entire entry
9061 ;; Show next sibling, or heading below text
9063 (and (if heading-p (org-goto-sibling) (outline-next-heading))
9064 (org-flag-heading nil))))
9065 (when siblings-p (org-show-siblings))
9067 ;; show all higher headings, possibly with siblings
9069 (while (and (condition-case nil
9070 (progn (org-up-heading-all 1) t)
9073 (org-flag-heading nil)
9074 (when siblings-p (org-show-siblings))))))))
9076 (defun org-reveal (&optional siblings)
9077 "Show current entry, hierarchy above it, and the following headline.
9078 This can be used to show a consistent set of context around locations
9079 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
9080 not t for the search context.
9082 With optional argument SIBLINGS, on each level of the hierarchy all
9083 siblings are shown. This repairs the tree structure to what it would
9084 look like when opened with hierarchical calls to `org-cycle'."
9086 (let ((org-show-hierarchy-above t)
9087 (org-show-following-heading t)
9088 (org-show-siblings (if siblings t org-show-siblings)))
9089 (org-show-context nil)))
9091 (defun org-highlight-new-match (beg end)
9092 "Highlight from BEG to END and mark the highlight is an occur headline."
9093 (let ((ov (org-make-overlay beg end)))
9094 (org-overlay-put ov 'face 'secondary-selection)
9095 (push ov org-occur-highlights)))
9097 (defun org-remove-occur-highlights (&optional beg end noremove)
9098 "Remove the occur highlights from the buffer.
9099 BEG and END are ignored. If NOREMOVE is nil, remove this function
9100 from the `before-change-functions' in the current buffer."
9102 (unless org-inhibit-highlight-removal
9103 (mapc 'org-delete-overlay org-occur-highlights)
9104 (setq org-occur-highlights nil)
9105 (setq org-occur-parameters nil)
9107 (remove-hook 'before-change-functions
9108 'org-remove-occur-highlights 'local))))
9112 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
9113 "Regular expression matching the priority indicator.")
9115 (defvar org-remove-priority-next-time nil)
9117 (defun org-priority-up ()
9118 "Increase the priority of the current item."
9122 (defun org-priority-down ()
9123 "Decrease the priority of the current item."
9125 (org-priority 'down))
9127 (defun org-priority (&optional action)
9128 "Change the priority of an item by ARG.
9129 ACTION can be `set', `up', `down', or a character."
9131 (setq action (or action 'set))
9132 (let (current new news have remove)
9134 (org-back-to-heading)
9135 (if (looking-at org-priority-regexp)
9136 (setq current (string-to-char (match-string 2))
9138 (setq current org-default-priority))
9140 ((or (eq action 'set)
9141 (if (featurep 'xemacs) (characterp action) (integerp action)))
9142 (if (not (eq action 'set))
9144 (message "Priority %c-%c, SPC to remove: "
9145 org-highest-priority org-lowest-priority)
9146 (setq new (read-char-exclusive)))
9147 (if (and (= (upcase org-highest-priority) org-highest-priority)
9148 (= (upcase org-lowest-priority) org-lowest-priority))
9149 (setq new (upcase new)))
9150 (cond ((equal new ?\ ) (setq remove t))
9151 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
9152 (error "Priority must be between `%c' and `%c'"
9153 org-highest-priority org-lowest-priority))))
9155 (if (and (not have) (eq last-command this-command))
9156 (setq new org-lowest-priority)
9157 (setq new (if (and org-priority-start-cycle-with-default (not have))
9158 org-default-priority (1- current)))))
9160 (if (and (not have) (eq last-command this-command))
9161 (setq new org-highest-priority)
9162 (setq new (if (and org-priority-start-cycle-with-default (not have))
9163 org-default-priority (1+ current)))))
9164 (t (error "Invalid action")))
9165 (if (or (< (upcase new) org-highest-priority)
9166 (> (upcase new) org-lowest-priority))
9168 (setq news (format "%c" new))
9171 (replace-match "" t t nil 1)
9172 (replace-match news t t nil 2))
9174 (error "No priority cookie found in line")
9175 (looking-at org-todo-line-regexp)
9178 (goto-char (match-end 2))
9179 (insert " [#" news "]"))
9180 (goto-char (match-beginning 3))
9181 (insert "[#" news "] ")))))
9182 (org-preserve-lc (org-set-tags nil 'align))
9184 (message "Priority removed")
9185 (message "Priority of current item set to %s" news))))
9188 (defun org-get-priority (s)
9189 "Find priority cookie and return priority."
9191 (if (not (string-match org-priority-regexp s))
9192 (* 1000 (- org-lowest-priority org-default-priority))
9193 (* 1000 (- org-lowest-priority
9194 (string-to-char (match-string 2 s)))))))
9198 (defun org-scan-tags (action matcher &optional todo-only)
9199 "Scan headline tags with inheritance and produce output ACTION.
9201 ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
9202 or `agenda' to produce an entry list for an agenda view. It can also be
9203 a Lisp form or a function that should be called at each matched headline, in
9204 this case the return value is a list of all return values from these calls.
9206 MATCHER is a Lisp form to be evaluated, testing if a given set of tags
9207 qualifies a headline for inclusion. When TODO-ONLY is non-nil,
9208 only lines with a TODO keyword are included in the output."
9209 (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
9210 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
9212 "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
9213 (props (list 'face nil
9214 'done-face 'org-done
9216 'mouse-face 'highlight
9217 'org-not-done-regexp org-not-done-regexp
9218 'org-todo-regexp org-todo-regexp
9219 'keymap org-agenda-keymap
9221 (format "mouse-2 or RET jump to org file %s"
9222 (abbreviate-file-name
9223 (or (buffer-file-name (buffer-base-buffer))
9224 (buffer-name (buffer-base-buffer)))))))
9225 (case-fold-search nil)
9226 lspos tags tags-list
9227 (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
9228 (llast 0) rtn rtn1 level category i txt
9229 todo marker entry priority)
9230 (when (not (member action '(agenda sparse-tree)))
9231 (setq action (list 'lambda nil action)))
9233 (goto-char (point-min))
9234 (when (eq action 'sparse-tree)
9236 (org-remove-occur-highlights))
9237 (while (re-search-forward re nil t)
9239 (setq todo (if (match-end 1) (match-string 2))
9240 tags (if (match-end 4) (match-string 4)))
9241 (goto-char (setq lspos (1+ (match-beginning 0))))
9242 (setq level (org-reduced-level (funcall outline-level))
9243 category (org-get-category))
9244 (setq i llast llast level)
9245 ;; remove tag lists from same and sublevels
9247 (when (setq entry (assoc i tags-alist))
9248 (setq tags-alist (delete entry tags-alist)))
9250 ;; add the next tags
9252 (setq tags (mapcar 'downcase (org-split-string tags ":"))
9254 (cons (cons level tags) tags-alist)))
9255 ;; compile tags for current headline
9257 (if org-use-tag-inheritance
9258 (apply 'append (mapcar 'cdr tags-alist))
9260 (when (and tags org-use-tag-inheritance
9261 (not (eq t org-use-tag-inheritance)))
9262 ;; selective inheritance, remove uninherited ones
9263 (setcdr (car tags-alist)
9264 (org-remove-uniherited-tags (cdar tags-alist))))
9265 (when (and (or (not todo-only) (member todo org-not-done-keywords))
9267 (or (not org-agenda-skip-archived-trees)
9268 (not (member org-archive-tag tags-list))))
9269 (unless (eq action 'sparse-tree) (org-agenda-skip))
9271 ;; select this headline
9274 ((eq action 'sparse-tree)
9275 (and org-highlight-sparse-tree-matches
9276 (org-get-heading) (match-end 0)
9277 (org-highlight-new-match
9278 (match-beginning 0) (match-beginning 1)))
9279 (org-show-context 'tags-tree))
9280 ((eq action 'agenda)
9281 (setq txt (org-format-agenda-item
9284 (if org-tags-match-list-sublevels
9285 (make-string (1- level) ?.) "")
9288 priority (org-get-priority txt))
9290 (setq marker (org-agenda-new-marker))
9291 (org-add-props txt props
9292 'org-marker marker 'org-hd-marker marker 'org-category category
9293 'priority priority 'type "tagsmatch")
9297 (setq rtn1 (funcall action))
9299 (goto-char (point-at-eol)))
9300 (t (error "Invalid action")))
9302 ;; if we are to skip sublevels, jump to end of subtree
9303 (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
9304 (when (and (eq action 'sparse-tree)
9305 (not org-sparse-tree-open-archived-trees))
9306 (org-hide-archived-subtrees (point-min) (point-max)))
9309 (defun org-remove-uniherited-tags (tags)
9310 "Remove all tags that are not inherited from the list TAGS."
9312 ((eq org-use-tag-inheritance t) tags)
9313 ((not org-use-tag-inheritance) nil)
9314 ((stringp org-use-tag-inheritance)
9316 (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
9318 ((listp org-use-tag-inheritance)
9319 (org-delete-all org-use-tag-inheritance tags))))
9321 (defvar todo-only) ;; dynamically scoped
9323 (defun org-tags-sparse-tree (&optional todo-only match)
9324 "Create a sparse tree according to tags string MATCH.
9325 MATCH can contain positive and negative selection of tags, like
9326 \"+WORK+URGENT-WITHBOSS\".
9327 If optional argument TODO_ONLY is non-nil, only select lines that are
9330 (org-prepare-agenda-buffers (list (current-buffer)))
9331 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
9333 (defvar org-cached-props nil)
9334 (defun org-cached-entry-get (pom property)
9335 (if (or (eq t org-use-property-inheritance)
9336 (and (stringp org-use-property-inheritance)
9337 (string-match org-use-property-inheritance property))
9338 (and (listp org-use-property-inheritance)
9339 (member property org-use-property-inheritance)))
9340 ;; Caching is not possible, check it directly
9341 (org-entry-get pom property 'inherit)
9342 ;; Get all properties, so that we can do complicated checks easily
9343 (cdr (assoc property (or org-cached-props
9344 (setq org-cached-props
9345 (org-entry-properties pom)))))))
9347 (defun org-global-tags-completion-table (&optional files)
9348 "Return the list of all tags in all agenda buffer/files."
9355 (set-buffer (find-file-noselect file))
9356 (append (org-get-buffer-tags)
9357 (mapcar (lambda (x) (if (stringp (car-safe x))
9358 (list (car-safe x)) nil))
9360 (if (and files (car files))
9362 (org-agenda-files))))))))
9364 (defun org-make-tags-matcher (match)
9365 "Create the TAGS//TODO matcher form for the selection string MATCH."
9366 ;; todo-only is scoped dynamically into this function, and the function
9367 ;; may change it it the matcher asksk for it.
9369 ;; Get a new match request, with completion
9370 (let ((org-last-tags-completion-table
9371 (org-global-tags-completion-table)))
9372 (setq match (completing-read
9373 "Match: " 'org-tags-completion-function nil nil nil
9374 'org-tags-history))))
9376 ;; Parse the string and create a lisp form
9377 (let ((match0 match)
9378 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
9380 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
9381 orterms term orlist re-p str-p level-p level-op
9382 prop-p pn pv po cat-p gv)
9383 (if (string-match "/+" match)
9384 ;; match contains also a todo-matching request
9386 (setq tagsmatch (substring match 0 (match-beginning 0))
9387 todomatch (substring match (match-end 0)))
9388 (if (string-match "^!" todomatch)
9389 (setq todo-only t todomatch (substring todomatch 1)))
9390 (if (string-match "^\\s-*$" todomatch)
9391 (setq todomatch nil)))
9392 ;; only matching tags
9393 (setq tagsmatch match todomatch nil))
9395 ;; Make the tags matcher
9396 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
9397 (setq tagsmatcher t)
9398 (setq orterms (org-split-string tagsmatch "|") orlist nil)
9399 (while (setq term (pop orterms))
9400 (while (and (equal (substring term -1) "\\") orterms)
9401 (setq term (concat term "|" (pop orterms)))) ; repair bad split
9402 (while (string-match re term)
9403 (setq minus (and (match-end 1)
9404 (equal (match-string 1 term) "-"))
9405 tag (match-string 2 term)
9406 re-p (equal (string-to-char tag) ?{)
9407 level-p (match-end 4)
9408 prop-p (match-end 5)
9410 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
9412 (setq level-op (org-op-to-function (match-string 3 term)))
9413 `(,level-op level ,(string-to-number
9414 (match-string 4 term))))
9416 (setq pn (match-string 5 term)
9417 po (match-string 6 term)
9418 pv (match-string 7 term)
9419 cat-p (equal pn "CATEGORY")
9420 re-p (equal (string-to-char pv) ?{)
9421 str-p (equal (string-to-char pv) ?\")
9422 pv (if (or re-p str-p) (substring pv 1 -1) pv))
9423 (setq po (org-op-to-function po str-p))
9424 (if (equal pn "CATEGORY")
9425 (setq gv '(get-text-property (point) 'org-category))
9426 (setq gv `(org-cached-entry-get nil ,pn)))
9429 `(not (string-match ,pv (or ,gv "")))
9430 `(string-match ,pv (or ,gv "")))
9432 `(,po (or ,gv "") ,pv)
9433 `(,po (string-to-number (or ,gv ""))
9434 ,(string-to-number pv) ))))
9435 (t `(member ,(downcase tag) tags-list)))
9436 mm (if minus (list 'not mm) mm)
9437 term (substring term (match-end 0)))
9438 (push mm tagsmatcher))
9439 (push (if (> (length tagsmatcher) 1)
9440 (cons 'and tagsmatcher)
9443 (setq tagsmatcher nil))
9444 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
9446 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
9447 ;; Make the todo matcher
9448 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
9449 (setq todomatcher t)
9450 (setq orterms (org-split-string todomatch "|") orlist nil)
9451 (while (setq term (pop orterms))
9452 (while (string-match re term)
9453 (setq minus (and (match-end 1)
9454 (equal (match-string 1 term) "-"))
9455 kwd (match-string 2 term)
9456 re-p (equal (string-to-char kwd) ?{)
9457 term (substring term (match-end 0))
9459 `(string-match ,(substring kwd 1 -1) todo)
9460 (list 'equal 'todo kwd))
9461 mm (if minus (list 'not mm) mm))
9462 (push mm todomatcher))
9463 (push (if (> (length todomatcher) 1)
9464 (cons 'and todomatcher)
9467 (setq todomatcher nil))
9468 (setq todomatcher (if (> (length orlist) 1)
9469 (cons 'or orlist) (car orlist))))
9471 ;; Return the string and lisp forms of the matcher
9472 (setq matcher (if todomatcher
9473 (list 'and tagsmatcher todomatcher)
9475 (cons match0 matcher)))
9477 (defun org-op-to-function (op &optional stringp)
9480 ((equal op "<" ) '(< string< ))
9481 ((equal op ">" ) '(> org-string> ))
9482 ((member op '("<=" "=<")) '(<= org-string<= ))
9483 ((member op '(">=" "=>")) '(>= org-string>= ))
9484 ((member op '("=" "==")) '(= string= ))
9485 ((member op '("<>" "!=")) '(org<> org-string<> ))))
9486 (nth (if stringp 1 0) op))
9488 (defun org<> (a b) (not (= a b)))
9489 (defun org-string<= (a b) (or (string= a b) (string< a b)))
9490 (defun org-string>= (a b) (not (string< a b)))
9491 (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
9492 (defun org-string<> (a b) (not (string= a b)))
9494 (defun org-match-any-p (re list)
9495 "Does re match any element of list?"
9496 (setq list (mapcar (lambda (x) (string-match re x)) list))
9499 (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
9500 (defvar org-tags-overlay (org-make-overlay 1 1))
9501 (org-detach-overlay org-tags-overlay)
9503 (defun org-get-tags-at (&optional pos)
9504 "Get a list of all headline tags applicable at POS.
9505 POS defaults to point. If tags are inherited, the list contains
9506 the targets in the same sequence as the headlines appear, i.e.
9507 the tags of the current headline come last."
9509 (let (tags ltags lastpos parent)
9513 (goto-char (or pos (point)))
9517 (org-back-to-heading t)
9518 (while (not (equal lastpos (point)))
9519 (setq lastpos (point))
9520 (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
9521 (setq ltags (org-split-string
9522 (org-match-string-no-properties 1) ":"))
9523 (setq tags (append (org-remove-uniherited-tags ltags)
9525 (or org-use-tag-inheritance (error ""))
9526 (org-up-heading-all 1)
9529 (append (org-remove-uniherited-tags org-file-tags) tags))))
9531 (defun org-toggle-tag (tag &optional onoff)
9532 "Toggle the tag TAG for the current line.
9533 If ONOFF is `on' or `off', don't toggle but set to this state."
9534 (unless (org-on-heading-p t) (error "Not on headling"))
9538 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
9541 (setq current (match-string 1))
9544 (setq current (nreverse (org-split-string current ":")))
9548 (or (member tag current) (push tag current)))
9550 (or (not (member tag current)) (setq current (delete tag current))))
9551 (t (if (member tag current)
9552 (setq current (delete tag current))
9554 (push tag current))))
9558 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
9559 (org-set-tags nil t))
9560 (delete-horizontal-space))
9561 (run-hooks 'org-after-tags-change-hook))
9564 (defun org-align-tags-here (to-col)
9565 ;; Assumes that this is a headline
9566 (let ((pos (point)) (col (current-column)) ncol tags-l p)
9567 (beginning-of-line 1)
9568 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
9569 (< pos (match-beginning 2)))
9571 (setq tags-l (- (match-end 2) (match-beginning 2)))
9572 (goto-char (match-beginning 1))
9574 (delete-region (point) (1+ (match-beginning 2)))
9575 (setq ncol (max (1+ (current-column))
9579 (- (abs to-col) tags-l))))
9581 (insert (make-string (- ncol (current-column)) ?\ ))
9582 (setq ncol (current-column))
9583 (when indent-tabs-mode (tabify p (point-at-eol)))
9584 (org-move-to-column (min ncol col) t))
9587 (defun org-set-tags (&optional arg just-align)
9588 "Set the tags for the current headline.
9589 With prefix ARG, realign all tags in headings in the current buffer."
9591 (let* ((re (concat "^" outline-regexp))
9592 (current (org-get-tags-string))
9593 (col (current-column))
9594 (org-setting-tags t)
9595 table current-tags inherited-tags ; computed below when needed
9599 (goto-char (point-min))
9600 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
9601 (while (re-search-forward re nil t)
9602 (org-set-tags nil t)
9604 (message "All tags realigned to column %d" org-tags-column))
9607 ;; Get a new set of tags from the user
9609 (setq table (or org-tag-alist (org-get-buffer-tags))
9610 org-last-tags-completion-table table
9611 current-tags (org-split-string current ":")
9612 inherited-tags (nreverse
9613 (nthcdr (length current-tags)
9614 (nreverse (org-get-tags-at))))
9616 (if (or (eq t org-use-fast-tag-selection)
9617 (and org-use-fast-tag-selection
9618 (delq nil (mapcar 'cdr table))))
9619 (org-fast-tag-selection
9620 current-tags inherited-tags table
9621 (if org-fast-tag-selection-include-todo org-todo-key-alist))
9622 (let ((org-add-colon-after-tag-completion t))
9624 (org-without-partial-completion
9625 (completing-read "Tags: " 'org-tags-completion-function
9626 nil nil current 'org-tags-history)))))))
9627 (while (string-match "[-+&]+" tags)
9628 ;; No boolean logic, just a list
9629 (setq tags (replace-match ":" t t tags))))
9631 (if (string-match "\\`[\t ]*\\'" tags)
9633 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
9634 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
9636 ;; Insert new tags at the correct column
9637 (beginning-of-line 1)
9639 ((and (equal current "") (equal tags "")))
9641 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
9645 (goto-char (match-beginning 0))
9646 (setq c0 (current-column) p0 (point)
9647 c1 (max (1+ c0) (if (> org-tags-column 0)
9649 (- (- org-tags-column) (length tags))))
9650 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
9651 (replace-match rpl t t)
9652 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
9654 (t (error "Tags alignment failed")))
9655 (org-move-to-column col)
9657 (run-hooks 'org-after-tags-change-hook)))))
9659 (defun org-change-tag-in-region (beg end tag off)
9660 "Add or remove TAG for each entry in the region.
9661 This works in the agenda, and also in an org-mode buffer."
9663 (list (region-beginning) (region-end)
9664 (let ((org-last-tags-completion-table
9666 (org-get-buffer-tags)
9667 (org-global-tags-completion-table))))
9669 "Tag: " 'org-tags-completion-function nil nil nil
9672 (message "[s]et or [r]emove? ")
9673 (equal (read-char-exclusive) ?r))))
9674 (if (fboundp 'deactivate-mark) (deactivate-mark))
9675 (let ((agendap (equal major-mode 'org-agenda-mode))
9676 l1 l2 m buf pos newhead (cnt 0))
9678 (setq l2 (1- (org-current-line)))
9680 (setq l1 (org-current-line))
9681 (loop for l from l1 to l2 do
9683 (setq m (get-text-property (point) 'org-hd-marker))
9684 (when (or (and (org-mode-p) (org-on-heading-p))
9686 (setq buf (if agendap (marker-buffer m) (current-buffer))
9687 pos (if agendap m (point)))
9688 (with-current-buffer buf
9693 (org-toggle-tag tag (if off 'off 'on))
9694 (setq newhead (org-get-heading)))))
9695 (and agendap (org-agenda-change-all-lines newhead m))))
9696 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
9698 (defun org-tags-completion-function (string predicate &optional flag)
9699 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
9700 (confirm (lambda (x) (stringp (car x)))))
9701 (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
9702 (setq s1 (match-string 1 string)
9703 s2 (match-string 2 string))
9704 (setq s1 "" s2 string))
9708 (setq rtn (try-completion s2 ctable confirm))
9711 (concat s1 s2 (substring rtn (length s2))
9712 (if (and org-add-colon-after-tag-completion
9718 (all-completions s2 ctable confirm)
9725 (defun org-fast-tag-insert (kwd tags face &optional end)
9726 "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
9727 (insert (format "%-12s" (concat kwd ":"))
9728 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
9731 (defun org-fast-tag-show-exit (flag)
9734 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
9738 (org-move-to-column (- (window-width) 19) t)
9739 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
9741 (defun org-set-current-tags-overlay (current prefix)
9742 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
9743 (if (featurep 'xemacs)
9744 (org-overlay-display org-tags-overlay (concat prefix s)
9745 'secondary-selection)
9746 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
9747 (org-overlay-display org-tags-overlay (concat prefix s)))))
9749 (defun org-fast-tag-selection (current inherited table &optional todo-table)
9750 "Fast tag selection with single keys.
9751 CURRENT is the current list of tags in the headline, INHERITED is the
9752 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
9753 possibly with grouping information. TODO-TABLE is a similar table with
9754 TODO keywords, should these have keys assigned to them.
9755 If the keys are nil, a-z are automatically assigned.
9756 Returns the new tags string, or nil to not change the current settings."
9757 (let* ((fulltable (append table todo-table))
9758 (maxlen (apply 'max (mapcar
9760 (if (stringp (car x)) (string-width (car x)) 0))
9762 (buf (current-buffer))
9763 (expert (eq org-fast-tag-selection-single-key 'expert))
9765 (fwidth (+ maxlen 3 1 3))
9766 (ncol (/ (- (window-width) 4) fwidth))
9769 tg cnt e c char c1 c2 ntable tbl rtn
9770 ov-start ov-end ov-prefix
9771 (exit-after-next org-fast-tag-selection-single-key)
9772 (done-keywords org-done-keywords)
9775 (beginning-of-line 1)
9777 (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
9778 (setq ov-start (match-beginning 1)
9779 ov-end (match-end 1)
9781 (setq ov-start (1- (point-at-eol))
9782 ov-end (1+ ov-start))
9783 (skip-chars-forward "^\n\r")
9786 (buffer-substring (1- (point)) (point))
9787 (if (> (current-column) org-tags-column)
9789 (make-string (- org-tags-column (current-column)) ?\ ))))))
9790 (org-move-overlay org-tags-overlay ov-start ov-end)
9791 (save-window-excursion
9793 (set-buffer (get-buffer-create " *Org tags*"))
9794 (delete-other-windows)
9795 (split-window-vertically)
9796 (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
9798 (org-set-local 'org-done-keywords done-keywords)
9799 (org-fast-tag-insert "Inherited" inherited i-face "\n")
9800 (org-fast-tag-insert "Current" current c-face "\n\n")
9801 (org-fast-tag-show-exit exit-after-next)
9802 (org-set-current-tags-overlay current ov-prefix)
9803 (setq tbl fulltable char ?a cnt 0)
9804 (while (setq e (pop tbl))
9806 ((equal e '(:startgroup))
9807 (push '() groups) (setq ingroup t)
9808 (when (not (= cnt 0))
9812 ((equal e '(:endgroup))
9813 (setq ingroup nil cnt 0)
9816 (setq tg (car e) c2 nil)
9819 ;; automatically assign a character.
9820 (setq c1 (string-to-char
9821 (downcase (substring
9822 tg (if (= (string-to-char tg) ?@) 1 0)))))
9823 (if (or (rassoc c1 ntable) (rassoc c1 table))
9824 (while (or (rassoc char ntable) (rassoc char table))
9825 (setq char (1+ char)))
9827 (setq c (or c2 char)))
9828 (if ingroup (push tg (car groups)))
9829 (setq tg (org-add-props tg nil 'face
9831 ((not (assoc tg table))
9832 (org-get-todo-face tg))
9833 ((member tg current) c-face)
9834 ((member tg inherited) i-face)
9836 (if (and (= cnt 0) (not ingroup)) (insert " "))
9837 (insert "[" c "] " tg (make-string
9838 (- fwidth 4 (length tg)) ?\ ))
9839 (push (cons tg c) ntable)
9840 (when (= (setq cnt (1+ cnt)) ncol)
9842 (if ingroup (insert " "))
9844 (setq ntable (nreverse ntable))
9846 (goto-char (point-min))
9847 (if (and (not expert) (fboundp 'fit-window-to-buffer))
9848 (fit-window-to-buffer))
9852 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
9853 (if groups " [!] no groups" " [!]groups")
9854 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
9855 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
9857 ((= c ?\r) (throw 'exit t))
9859 (setq groups (not groups))
9860 (goto-char (point-min))
9861 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
9864 (org-fast-tag-show-exit
9865 (setq exit-after-next (not exit-after-next)))
9867 (delete-other-windows)
9868 (split-window-vertically)
9869 (org-switch-to-buffer-other-window " *Org tags*")
9870 (and (fboundp 'fit-window-to-buffer)
9871 (fit-window-to-buffer))))
9873 (and (= c ?q) (not (rassoc c ntable))))
9874 (org-detach-overlay org-tags-overlay)
9878 (if exit-after-next (setq exit-after-next 'now)))
9881 (setq tg (completing-read
9884 (with-current-buffer buf
9885 (org-get-buffer-tags)))))
9886 (quit (setq tg "")))
9887 (when (string-match "\\S-" tg)
9888 (add-to-list 'buffer-tags (list tg))
9889 (if (member tg current)
9890 (setq current (delete tg current))
9892 (if exit-after-next (setq exit-after-next 'now)))
9893 ((setq e (rassoc c todo-table) tg (car e))
9894 (with-current-buffer buf
9895 (save-excursion (org-todo tg)))
9896 (if exit-after-next (setq exit-after-next 'now)))
9897 ((setq e (rassoc c ntable) tg (car e))
9898 (if (member tg current)
9899 (setq current (delete tg current))
9900 (loop for g in groups do
9903 (setq current (delete x current)))
9906 (if exit-after-next (setq exit-after-next 'now))))
9908 ;; Create a sorted list
9912 (assoc b (cdr (memq (assoc a ntable) ntable))))))
9913 (if (eq exit-after-next 'now) (throw 'exit t))
9914 (goto-char (point-min))
9915 (beginning-of-line 2)
9916 (delete-region (point) (point-at-eol))
9917 (org-fast-tag-insert "Current" current c-face)
9918 (org-set-current-tags-overlay current ov-prefix)
9919 (while (re-search-forward
9920 (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
9921 (setq tg (match-string 1))
9922 (add-text-properties
9923 (match-beginning 1) (match-end 1)
9926 ((member tg current) c-face)
9927 ((member tg inherited) i-face)
9928 (t (get-text-property (match-beginning 1) 'face))))))
9929 (goto-char (point-min)))))
9930 (org-detach-overlay org-tags-overlay)
9932 (mapconcat 'identity current ":")
9935 (defun org-get-tags-string ()
9936 "Get the TAGS string in the current headline."
9937 (unless (org-on-heading-p t)
9938 (error "Not on a heading"))
9940 (beginning-of-line 1)
9941 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
9942 (org-match-string-no-properties 1)
9945 (defun org-get-tags ()
9946 "Get the list of tags specified in the current headline."
9947 (org-split-string (org-get-tags-string) ":"))
9949 (defun org-get-buffer-tags ()
9950 "Get a table of all tags used in the buffer, for completion."
9953 (goto-char (point-min))
9954 (while (re-search-forward
9955 (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
9956 (when (equal (char-after (point-at-bol 0)) ?*)
9957 (mapc (lambda (x) (add-to-list 'tags x))
9958 (org-split-string (org-match-string-no-properties 1) ":")))))
9959 (mapcar 'list tags)))
9961 ;;;; The mapping API
9964 (defun org-map-entries (func &optional match scope &rest skip)
9965 "Call FUNC at each headline selected by MATCH in SCOPE.
9967 FUNC is a function or a lisp form. The function will be called without
9968 arguments, with the cursor positioned at the beginning of the headline.
9969 The return values of all calls to the function will be collected and
9972 MATCH is a tags/property/todo match as it is used in the agenda tags view.
9973 Only headlines that are matched by this query will be considered during
9974 the iteration. When MATCH is nil or t, all headlines will be
9975 visited by the iteration.
9977 SCOPE determines the scope of this command. It can be any of:
9979 nil The current buffer, respecting the restriction if any
9980 tree The subtree started with the entry at point
9981 file The current buffer, without restriction
9983 The current buffer, and any archives associated with it
9984 agenda All agenda files
9985 agenda-with-archives
9986 All agenda files with any archive files associated with them
9988 If this is a list, all files in the list will be scanned
9990 The remaining args are treated as settings for the skipping facilities of
9991 the scanner. The following items can be given here:
9993 archive skip trees with the archive tag.
9994 comment skip trees with the COMMENT keyword
9995 function or Emacs Lisp form:
9996 will be used as value for `org-agenda-skip-function', so whenever
9997 the the function returns t, FUNC will not be called for that
9998 entry and search will continue from the point where the
9999 function leaves it."
10000 (let* ((org-agenda-skip-archived-trees (memq 'archive skip))
10001 (org-agenda-skip-comment-trees (memq 'comment skip))
10002 (org-agenda-skip-function
10003 (car (org-delete-all '(comment archive) skip)))
10004 (org-tags-match-list-sublevels t)
10008 ((eq match t) (setq matcher t))
10009 ((eq match nil) (setq matcher t))
10010 (t (setq matcher (if match (org-make-tags-matcher match) t))))
10012 (when (eq scope 'tree)
10013 (org-back-to-heading t)
10014 (org-narrow-to-subtree)
10019 (org-prepare-agenda-buffers
10020 (list (buffer-file-name (current-buffer))))
10021 (org-scan-tags func matcher))
10022 ;; Get the right scope
10025 ((and scope (listp scope) (symbolp (car scope)))
10026 (setq scope (eval scope)))
10027 ((eq scope 'agenda)
10028 (setq scope (org-agenda-files t)))
10029 ((eq scope 'agenda-with-archives)
10030 (setq scope (org-agenda-files t))
10031 (setq scope (org-add-archive-files scope)))
10033 (setq scope (list (buffer-file-name))))
10034 ((eq scope 'file-with-archives)
10035 (setq scope (org-add-archive-files (list (buffer-file-name))))))
10036 (org-prepare-agenda-buffers scope)
10037 (while (setq file (pop scope))
10038 (with-current-buffer (org-find-base-buffer-visiting file)
10042 (goto-char (point-min))
10043 (org-scan-tags func matcher))))))))
10047 ;;; Setting and retrieving properties
10049 (defconst org-special-properties
10050 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
10051 "TIMESTAMP" "TIMESTAMP_IA")
10052 "The special properties valid in Org-mode.
10054 These are properties that are not defined in the property drawer,
10055 but in some other way.")
10057 (defconst org-default-properties
10058 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
10059 "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
10060 "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
10061 "EXPORT_FILE_NAME" "EXPORT_TITLE")
10062 "Some properties that are used by Org-mode for various purposes.
10063 Being in this list makes sure that they are offered for completion.")
10065 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
10066 "Regular expression matching the first line of a property drawer.")
10068 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
10069 "Regular expression matching the first line of a property drawer.")
10071 (defun org-property-action ()
10072 "Do an action on properties."
10075 (org-at-property-p)
10076 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
10077 (setq c (read-char-exclusive))
10080 (call-interactively 'org-set-property))
10082 (call-interactively 'org-delete-property))
10084 (call-interactively 'org-delete-property-globally))
10086 (call-interactively 'org-compute-property-at-point))
10087 (t (error "No such property action %c" c)))))
10089 (defun org-at-property-p ()
10090 "Is the cursor in a property line?"
10091 ;; FIXME: Does not check if we are actually in the drawer.
10092 ;; FIXME: also returns true on any drawers.....
10093 ;; This is used by C-c C-c for property action.
10095 (beginning-of-line 1)
10096 (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
10098 (defun org-get-property-block (&optional beg end force)
10099 "Return the (beg . end) range of the body of the property drawer.
10100 BEG and END can be beginning and end of subtree, if not given
10101 they will be found.
10102 If the drawer does not exist and FORCE is non-nil, create the drawer."
10105 (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
10106 (end (or end (progn (outline-next-heading) (point)))))
10108 (if (re-search-forward org-property-start-re end t)
10109 (setq beg (1+ (match-end 0)))
10112 (org-insert-property-drawer)
10113 (setq end (progn (outline-next-heading) (point))))
10116 (if (re-search-forward org-property-start-re end t)
10117 (setq beg (1+ (match-end 0)))))
10118 (if (re-search-forward org-property-end-re end t)
10119 (setq end (match-beginning 0))
10120 (or force (throw 'exit nil))
10123 (org-indent-line-function)
10124 (insert ":END:\n"))
10127 (defun org-entry-properties (&optional pom which)
10128 "Get all properties of the entry at point-or-marker POM.
10129 This includes the TODO keyword, the tags, time strings for deadline,
10130 scheduled, and clocking, and any additional properties defined in the
10131 entry. The return value is an alist, keys may occur multiple times
10132 if the property key was used several times.
10133 POM may also be nil, in which case the current entry is used.
10134 If WHICH is nil or `all', get all properties. If WHICH is
10135 `special' or `standard', only get that subclass."
10136 (setq which (or which 'all))
10137 (org-with-point-at pom
10138 (let ((clockstr (substring org-clock-string 0 -1))
10139 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
10140 beg end range props sum-props key value string clocksum)
10142 (when (condition-case nil (org-back-to-heading t) (error nil))
10144 (setq sum-props (get-text-property (point) 'org-summaries))
10145 (setq clocksum (get-text-property (point) :org-clock-minutes))
10146 (outline-next-heading)
10148 (when (memq which '(all special))
10149 ;; Get the special properties, like TODO and tags
10151 (when (and (looking-at org-todo-line-regexp) (match-end 2))
10152 (push (cons "TODO" (org-match-string-no-properties 2)) props))
10153 (when (looking-at org-priority-regexp)
10154 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
10155 (when (and (setq value (org-get-tags-string))
10156 (string-match "\\S-" value))
10157 (push (cons "TAGS" value) props))
10158 (when (setq value (org-get-tags-at))
10159 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
10161 (while (re-search-forward org-maybe-keyword-time-regexp end t)
10162 (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
10163 string (if (equal key clockstr)
10167 (match-beginning 3) (goto-char (point-at-eol)))))
10168 (substring (org-match-string-no-properties 3) 1 -1)))
10170 (if (= (char-after (match-beginning 3)) ?\[)
10171 (setq key "TIMESTAMP_IA")
10172 (setq key "TIMESTAMP")))
10173 (when (or (equal key clockstr) (not (assoc key props)))
10174 (push (cons key string) props)))
10178 (when (memq which '(all standard))
10179 ;; Get the standard properties, like :PORP: ...
10180 (setq range (org-get-property-block beg end))
10182 (goto-char (car range))
10183 (while (re-search-forward
10184 (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
10186 (setq key (org-match-string-no-properties 1)
10187 value (org-trim (or (org-match-string-no-properties 2) "")))
10188 (unless (member key excluded)
10189 (push (cons key (or value "")) props)))))
10191 (push (cons "CLOCKSUM"
10192 (org-columns-number-to-string (/ (float clocksum) 60.)
10195 (append sum-props (nreverse props)))))))
10197 (defun org-entry-get (pom property &optional inherit)
10198 "Get value of PROPERTY for entry at point-or-marker POM.
10199 If INHERIT is non-nil and the entry does not have the property,
10200 then also check higher levels of the hierarchy.
10201 If INHERIT is the symbol `selective', use inheritance only if the setting
10202 in `org-use-property-inheritance' selects PROPERTY for inheritance.
10203 If the property is present but empty, the return value is the empty string.
10204 If the property is not present at all, nil is returned."
10205 (org-with-point-at pom
10206 (if (and inherit (if (eq inherit 'selective)
10207 (org-property-inherit-p property)
10209 (org-entry-get-with-inheritance property)
10210 (if (member property org-special-properties)
10211 ;; We need a special property. Use brute force, get all properties.
10212 (cdr (assoc property (org-entry-properties nil 'special)))
10213 (let ((range (org-get-property-block)))
10215 (goto-char (car range))
10217 (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
10219 ;; Found the property, return it.
10221 (org-match-string-no-properties 1)
10224 (defun org-property-or-variable-value (var &optional inherit)
10225 "Check if there is a property fixing the value of VAR.
10226 If yes, return this value. If not, return the current value of the variable."
10227 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
10228 (if (and prop (stringp prop) (string-match "\\S-" prop))
10230 (symbol-value var))))
10232 (defun org-entry-delete (pom property)
10233 "Delete the property PROPERTY from entry at point-or-marker POM."
10234 (org-with-point-at pom
10235 (if (member property org-special-properties)
10236 nil ; cannot delete these properties.
10237 (let ((range (org-get-property-block)))
10239 (goto-char (car range))
10241 (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
10244 (delete-region (match-beginning 0) (1+ (point-at-eol)))
10248 ;; Multi-values properties are properties that contain multiple values
10249 ;; These values are assumed to be single words, separated by whitespace.
10250 (defun org-entry-add-to-multivalued-property (pom property value)
10251 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
10252 (let* ((old (org-entry-get pom property))
10253 (values (and old (org-split-string old "[ \t]"))))
10254 (unless (member value values)
10255 (setq values (cons value values))
10256 (org-entry-put pom property
10257 (mapconcat 'identity values " ")))))
10259 (defun org-entry-remove-from-multivalued-property (pom property value)
10260 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
10261 (let* ((old (org-entry-get pom property))
10262 (values (and old (org-split-string old "[ \t]"))))
10263 (when (member value values)
10264 (setq values (delete value values))
10265 (org-entry-put pom property
10266 (mapconcat 'identity values " ")))))
10268 (defun org-entry-member-in-multivalued-property (pom property value)
10269 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
10270 (let* ((old (org-entry-get pom property))
10271 (values (and old (org-split-string old "[ \t]"))))
10272 (member value values)))
10274 (defvar org-entry-property-inherited-from (make-marker))
10276 (defun org-entry-get-with-inheritance (property)
10277 "Get entry property, and search higher levels if not present."
10284 (when (setq tmp (org-entry-get nil property))
10285 (org-back-to-heading t)
10286 (move-marker org-entry-property-inherited-from (point))
10288 (or (org-up-heading-safe) (throw 'ex nil)))))
10290 (cdr (assoc property org-file-properties))
10291 (cdr (assoc property org-global-properties))
10292 (cdr (assoc property org-global-properties-fixed))))))
10294 (defun org-entry-put (pom property value)
10295 "Set PROPERTY to VALUE for entry at point-or-marker POM."
10296 (org-with-point-at pom
10297 (org-back-to-heading t)
10298 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
10301 ((equal property "TODO")
10302 (when (and (stringp value) (string-match "\\S-" value)
10303 (not (member value org-todo-keywords-1)))
10304 (error "\"%s\" is not a valid TODO state" value))
10305 (if (or (not value)
10306 (not (string-match "\\S-" value)))
10307 (setq value 'none))
10309 (org-set-tags nil 'align))
10310 ((equal property "PRIORITY")
10311 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
10312 (string-to-char value) ?\ ))
10313 (org-set-tags nil 'align))
10314 ((equal property "SCHEDULED")
10315 (if (re-search-forward org-scheduled-time-regexp end t)
10317 ((eq value 'earlier) (org-timestamp-change -1 'day))
10318 ((eq value 'later) (org-timestamp-change 1 'day))
10319 (t (call-interactively 'org-schedule)))
10320 (call-interactively 'org-schedule)))
10321 ((equal property "DEADLINE")
10322 (if (re-search-forward org-deadline-time-regexp end t)
10324 ((eq value 'earlier) (org-timestamp-change -1 'day))
10325 ((eq value 'later) (org-timestamp-change 1 'day))
10326 (t (call-interactively 'org-deadline)))
10327 (call-interactively 'org-deadline)))
10328 ((member property org-special-properties)
10329 (error "The %s property can not yet be set with `org-entry-put'"
10331 (t ; a non-special property
10332 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
10333 (setq range (org-get-property-block beg end 'force))
10334 (goto-char (car range))
10335 (if (re-search-forward
10336 (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
10338 (delete-region (match-beginning 1) (match-end 1))
10339 (goto-char (match-beginning 1)))
10340 (goto-char (cdr range))
10343 (org-indent-line-function)
10344 (insert ":" property ":"))
10345 (and value (insert " " value))
10346 (org-indent-line-function)))))))
10348 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
10349 "Get all property keys in the current buffer.
10350 With INCLUDE-SPECIALS, also list the special properties that relect things
10351 like tags and TODO state.
10352 With INCLUDE-DEFAULTS, also include properties that has special meaning
10353 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
10354 With INCLUDE-COLUMNS, also include property names given in COLUMN
10355 formats in the current buffer."
10356 (let (rtn range cfmt cols s p)
10360 (goto-char (point-min))
10361 (while (re-search-forward org-property-start-re nil t)
10362 (setq range (org-get-property-block))
10363 (goto-char (car range))
10364 (while (re-search-forward
10365 (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
10367 (add-to-list 'rtn (org-match-string-no-properties 1)))
10368 (outline-next-heading))))
10370 (when include-specials
10371 (setq rtn (append org-special-properties rtn)))
10373 (when include-defaults
10374 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
10376 (when include-columns
10380 (goto-char (point-min))
10381 (while (re-search-forward
10382 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
10384 (setq cfmt (match-string 2) s 0)
10385 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
10387 (setq s (match-end 0)
10388 p (match-string 1 cfmt))
10389 (unless (or (equal p "ITEM")
10390 (member p org-special-properties))
10391 (add-to-list 'rtn (match-string 1 cfmt))))))))
10393 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
10395 (defun org-property-values (key)
10396 "Return a list of all values of property KEY."
10400 (goto-char (point-min))
10401 (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
10403 (while (re-search-forward re nil t)
10404 (add-to-list 'values (org-trim (match-string 1))))
10405 (delete "" values)))))
10407 (defun org-insert-property-drawer ()
10408 "Insert a property drawer into the current entry."
10410 (org-back-to-heading t)
10411 (looking-at outline-regexp)
10412 (let ((indent (- (match-end 0)(match-beginning 0)))
10414 (re (concat "^[ \t]*" org-keyword-time-regexp))
10416 (outline-next-heading)
10419 (while (re-search-forward re end t))
10420 (setq hiddenp (org-invisible-p))
10422 (and (equal (char-after) ?\n) (forward-char 1))
10423 (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
10424 (beginning-of-line 2))
10425 (org-skip-over-state-notes)
10426 (skip-chars-backward " \t\n\r")
10427 (if (eq (char-before) ?*) (forward-char 1))
10428 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
10429 (beginning-of-line 0)
10430 (org-indent-to-column indent)
10431 (beginning-of-line 2)
10432 (org-indent-to-column indent)
10433 (beginning-of-line 0)
10436 (org-back-to-heading t)
10438 (org-flag-drawer t))))
10440 (defun org-set-property (property value)
10441 "In the current entry, set PROPERTY to VALUE.
10442 When called interactively, this will prompt for a property name, offering
10443 completion on existing and default properties. And then it will prompt
10444 for a value, offering competion either on allowed values (via an inherited
10445 xxx_ALL property) or on existing values in other instances of this property
10446 in the current file."
10448 (let* ((completion-ignore-case t)
10449 (keys (org-buffer-property-keys nil t t))
10450 (prop0 (completing-read "Property: " (mapcar 'list keys)))
10451 (prop (if (member prop0 keys)
10453 (or (cdr (assoc (downcase prop0)
10454 (mapcar (lambda (x) (cons (downcase x) x))
10457 (cur (org-entry-get nil prop))
10458 (allowed (org-property-get-allowed-values nil prop 'table))
10459 (existing (mapcar 'list (org-property-values prop)))
10461 (org-completing-read "Value: " allowed nil 'req-match)
10462 (org-completing-read
10463 (concat "Value" (if (and cur (string-match "\\S-" cur))
10464 (concat "[" cur "]") "")
10466 existing nil nil "" nil cur))))
10467 (list prop (if (equal val "") cur val))))
10468 (unless (equal (org-entry-get nil property) value)
10469 (org-entry-put nil property value)))
10471 (defun org-delete-property (property)
10472 "In the current entry, delete PROPERTY."
10474 (let* ((completion-ignore-case t)
10475 (prop (completing-read
10476 "Property: " (org-entry-properties nil 'standard))))
10478 (message "Property %s %s" property
10479 (if (org-entry-delete nil property)
10481 "was not present in the entry")))
10483 (defun org-delete-property-globally (property)
10484 "Remove PROPERTY globally, from all entries."
10486 (let* ((completion-ignore-case t)
10487 (prop (completing-read
10488 "Globally remove property: "
10489 (mapcar 'list (org-buffer-property-keys)))))
10494 (goto-char (point-min))
10496 (while (re-search-forward
10497 (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
10499 (setq cnt (1+ cnt))
10500 (replace-match ""))
10501 (message "Property \"%s\" removed from %d entries" property cnt)))))
10503 (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
10505 (defun org-compute-property-at-point ()
10506 "Compute the property at point.
10507 This looks for an enclosing column format, extracts the operator and
10508 then applies it to the proerty in the column format's scope."
10510 (unless (org-at-property-p)
10511 (error "Not at a property"))
10512 (let ((prop (org-match-string-no-properties 2)))
10513 (org-columns-get-format-and-top-level)
10514 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
10515 (error "No operator defined for property %s" prop))
10516 (org-columns-compute prop)))
10518 (defun org-property-get-allowed-values (pom property &optional table)
10519 "Get allowed values for the property PROPERTY.
10520 When TABLE is non-nil, return an alist that can directly be used for
10524 ((equal property "TODO")
10525 (setq vals (org-with-point-at pom
10526 (append org-todo-keywords-1 '("")))))
10527 ((equal property "PRIORITY")
10528 (let ((n org-lowest-priority))
10529 (while (>= n org-highest-priority)
10530 (push (char-to-string n) vals)
10532 ((member property org-special-properties))
10534 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
10536 (when (and vals (string-match "\\S-" vals))
10537 (setq vals (car (read-from-string (concat "(" vals ")"))))
10538 (setq vals (mapcar (lambda (x)
10539 (cond ((stringp x) x)
10540 ((numberp x) (number-to-string x))
10541 ((symbolp x) (symbol-name x))
10544 (if table (mapcar 'list vals) vals)))
10546 (defun org-property-previous-allowed-value (&optional previous)
10547 "Switch to the next allowed value for this property."
10549 (org-property-next-allowed-value t))
10551 (defun org-property-next-allowed-value (&optional previous)
10552 "Switch to the next allowed value for this property."
10554 (unless (org-at-property-p)
10555 (error "Not at a property"))
10556 (let* ((key (match-string 2))
10557 (value (match-string 3))
10558 (allowed (or (org-property-get-allowed-values (point) key)
10559 (and (member value '("[ ]" "[-]" "[X]"))
10563 (error "Allowed values for this property have not been defined"))
10564 (if previous (setq allowed (reverse allowed)))
10565 (if (member value allowed)
10566 (setq nval (car (cdr (member value allowed)))))
10567 (setq nval (or nval (car allowed)))
10568 (if (equal nval value)
10569 (error "Only one allowed value for this property"))
10570 (org-at-property-p)
10571 (replace-match (concat " :" key ": " nval) t t)
10572 (org-indent-line-function)
10573 (beginning-of-line 1)
10574 (skip-chars-forward " \t")))
10576 (defun org-find-entry-with-id (ident)
10577 "Locate the entry that contains the ID property with exact value IDENT.
10578 IDENT can be a string, a symbol or a number, this function will search for
10579 the string representation of it.
10580 Return the position where this entry starts, or nil if there is no such entry."
10582 ((stringp ident) ident)
10583 ((symbol-name ident) (symbol-name ident))
10584 ((numberp ident) (number-to-string ident))
10585 (t (error "IDENT %s must be a string, symbol or number" ident))))
10586 (case-fold-search nil))
10590 (goto-char (point-min))
10591 (when (re-search-forward
10592 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
10594 (org-back-to-heading)
10599 (defvar org-last-changed-timestamp nil)
10600 (defvar org-last-inserted-timestamp nil
10601 "The last time stamp inserted with `org-insert-time-stamp'.")
10602 (defvar org-time-was-given) ; dynamically scoped parameter
10603 (defvar org-end-time-was-given) ; dynamically scoped parameter
10604 (defvar org-ts-what) ; dynamically scoped parameter
10606 (defun org-time-stamp (arg)
10607 "Prompt for a date/time and insert a time stamp.
10608 If the user specifies a time like HH:MM, or if this command is called
10609 with a prefix argument, the time stamp will contain date and time.
10610 Otherwise, only the date will be included. All parts of a date not
10611 specified by the user will be filled in from the current date/time.
10612 So if you press just return without typing anything, the time stamp
10613 will represent the current date/time. If there is already a timestamp
10614 at the cursor, it will be modified."
10618 ;; Default time is either today, or, when entering a range,
10619 ;; the range start.
10620 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
10622 (re-search-backward
10623 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
10624 (- (point) 20) t)))
10625 (apply 'encode-time (org-parse-time-string (match-string 1)))
10627 (default-input (and ts (org-get-compact-tod ts)))
10628 org-time-was-given org-end-time-was-given time)
10630 ((and (org-at-timestamp-p)
10631 (eq last-command 'org-time-stamp)
10632 (eq this-command 'org-time-stamp))
10634 (setq time (let ((this-command this-command))
10635 (org-read-date arg 'totime nil nil default-time default-input)))
10636 (org-insert-time-stamp time (or org-time-was-given arg)))
10637 ((org-at-timestamp-p)
10638 (setq time (let ((this-command this-command))
10639 (org-read-date arg 'totime nil nil default-time default-input)))
10640 (when (org-at-timestamp-p) ; just to get the match data
10642 (setq org-last-changed-timestamp
10643 (org-insert-time-stamp
10644 time (or org-time-was-given arg)
10645 nil nil nil (list org-end-time-was-given))))
10646 (message "Timestamp updated"))
10648 (setq time (let ((this-command this-command))
10649 (org-read-date arg 'totime nil nil default-time default-input)))
10650 (org-insert-time-stamp time (or org-time-was-given arg)
10651 nil nil nil (list org-end-time-was-given))))))
10653 ;; FIXME: can we use this for something else, like computing time differences?
10654 (defun org-get-compact-tod (s)
10655 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
10656 (let* ((t1 (match-string 1 s))
10657 (h1 (string-to-number (match-string 2 s)))
10658 (m1 (string-to-number (match-string 3 s)))
10659 (t2 (and (match-end 4) (match-string 5 s)))
10660 (h2 (and t2 (string-to-number (match-string 6 s))))
10661 (m2 (and t2 (string-to-number (match-string 7 s))))
10665 (setq dh (- h2 h1) dm (- m2 m1))
10666 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
10667 (concat t1 "+" (number-to-string dh)
10668 (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
10670 (defun org-time-stamp-inactive (&optional arg)
10671 "Insert an inactive time stamp.
10672 An inactive time stamp is enclosed in square brackets instead of angle
10673 brackets. It is inactive in the sense that it does not trigger agenda entries,
10674 does not link to the calendar and cannot be changed with the S-cursor keys.
10675 So these are more for recording a certain time/date."
10677 (let (org-time-was-given org-end-time-was-given time)
10678 (setq time (org-read-date arg 'totime))
10679 (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
10680 nil nil (list org-end-time-was-given))))
10682 (defvar org-date-ovl (org-make-overlay 1 1))
10683 (org-overlay-put org-date-ovl 'face 'org-warning)
10684 (org-detach-overlay org-date-ovl)
10686 (defvar org-ans1) ; dynamically scoped parameter
10687 (defvar org-ans2) ; dynamically scoped parameter
10689 (defvar org-plain-time-of-day-regexp) ; defined below
10691 (defvar org-overriding-default-time nil) ; dynamically scoped
10692 (defvar org-read-date-overlay nil)
10693 (defvar org-dcst nil) ; dynamically scoped
10695 (defun org-read-date (&optional with-time to-time from-string prompt
10696 default-time default-input)
10697 "Read a date, possibly a time, and make things smooth for the user.
10698 The prompt will suggest to enter an ISO date, but you can also enter anything
10699 which will at least partially be understood by `parse-time-string'.
10700 Unrecognized parts of the date will default to the current day, month, year,
10701 hour and minute. If this command is called to replace a timestamp at point,
10702 of to enter the second timestamp of a range, the default time is taken from the
10703 existing stamp. For example,
10704 3-2-5 --> 2003-02-05
10705 feb 15 --> currentyear-02-15
10706 sep 12 9 --> 2009-09-12
10707 12:45 --> today 12:45
10708 22 sept 0:34 --> currentyear-09-22 0:34
10709 12 --> currentyear-currentmonth-12
10710 Fri --> nearest Friday (today or later)
10713 Furthermore you can specify a relative date by giving, as the *first* thing
10714 in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
10715 change in days weeks, months, years.
10716 With a single plus or minus, the date is relative to today. With a double
10717 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
10718 +4d --> four days from today
10719 +4 --> same as above
10720 +2w --> two weeks from today
10721 ++5 --> five days from default date
10723 The function understands only English month and weekday abbreviations,
10724 but this can be configured with the variables `parse-time-months' and
10725 `parse-time-weekdays'.
10727 While prompting, a calendar is popped up - you can also select the
10728 date with the mouse (button 1). The calendar shows a period of three
10729 months. To scroll it to other months, use the keys `>' and `<'.
10730 If you don't like the calendar, turn it off with
10731 \(setq org-read-date-popup-calendar nil)
10733 With optional argument TO-TIME, the date will immediately be converted
10734 to an internal time.
10735 With an optional argument WITH-TIME, the prompt will suggest to also
10736 insert a time. Note that when WITH-TIME is not set, you can still
10737 enter a time, and this function will inform the calling routine about
10738 this change. The calling routine may then choose to change the format
10739 used to insert the time stamp into the buffer to include the time.
10740 With optional argument FROM-STRING, read from this string instead from
10741 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
10742 the time/date that is used for everything that is not specified by the
10744 (require 'parse-time)
10745 (let* ((org-time-stamp-rounding-minutes
10746 (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
10747 (org-dcst org-display-custom-times)
10748 (ct (org-current-time))
10749 (def (or org-overriding-default-time default-time ct))
10750 (defdecode (decode-time def))
10752 (when (< (nth 2 defdecode) org-extend-today-until)
10753 (setcar (nthcdr 2 defdecode) -1)
10754 (setcar (nthcdr 1 defdecode) 59)
10755 (setq def (apply 'encode-time defdecode)
10756 defdecode (decode-time def)))))
10757 (calendar-move-hook nil)
10758 (calendar-view-diary-initially-flag nil)
10759 (view-diary-entries-initially nil)
10760 (calendar-view-holidays-initially-flag nil)
10761 (view-calendar-holidays-initially nil)
10762 (timestr (format-time-string
10763 (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
10764 (prompt (concat (if prompt (concat prompt " ") "")
10765 (format "Date+time [%s]: " timestr)))
10766 ans (org-ans0 "") org-ans1 org-ans2 final)
10769 (from-string (setq ans from-string))
10770 (org-read-date-popup-calendar
10772 (save-window-excursion
10774 (calendar-forward-day (- (time-to-days def)
10775 (calendar-absolute-from-gregorian
10776 (calendar-current-date))))
10777 (org-eval-in-calendar nil t)
10778 (let* ((old-map (current-local-map))
10779 (map (copy-keymap calendar-mode-map))
10780 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
10781 (org-defkey map (kbd "RET") 'org-calendar-select)
10782 (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
10783 'org-calendar-select-mouse)
10784 (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2
])
10785 'org-calendar-select-mouse
)
10786 (org-defkey minibuffer-local-map
[(meta shift left
)]
10787 (lambda () (interactive)
10788 (org-eval-in-calendar '(calendar-backward-month 1))))
10789 (org-defkey minibuffer-local-map
[(meta shift right
)]
10790 (lambda () (interactive)
10791 (org-eval-in-calendar '(calendar-forward-month 1))))
10792 (org-defkey minibuffer-local-map
[(meta shift up
)]
10793 (lambda () (interactive)
10794 (org-eval-in-calendar '(calendar-backward-year 1))))
10795 (org-defkey minibuffer-local-map
[(meta shift down
)]
10796 (lambda () (interactive)
10797 (org-eval-in-calendar '(calendar-forward-year 1))))
10798 (org-defkey minibuffer-local-map
[(shift up
)]
10799 (lambda () (interactive)
10800 (org-eval-in-calendar '(calendar-backward-week 1))))
10801 (org-defkey minibuffer-local-map
[(shift down
)]
10802 (lambda () (interactive)
10803 (org-eval-in-calendar '(calendar-forward-week 1))))
10804 (org-defkey minibuffer-local-map
[(shift left
)]
10805 (lambda () (interactive)
10806 (org-eval-in-calendar '(calendar-backward-day 1))))
10807 (org-defkey minibuffer-local-map
[(shift right
)]
10808 (lambda () (interactive)
10809 (org-eval-in-calendar '(calendar-forward-day 1))))
10810 (org-defkey minibuffer-local-map
">"
10811 (lambda () (interactive)
10812 (org-eval-in-calendar '(scroll-calendar-left 1))))
10813 (org-defkey minibuffer-local-map
"<"
10814 (lambda () (interactive)
10815 (org-eval-in-calendar '(scroll-calendar-right 1))))
10818 (use-local-map map
)
10819 (add-hook 'post-command-hook
'org-read-date-display
)
10820 (setq org-ans0
(read-string prompt default-input nil nil
))
10821 ;; org-ans0: from prompt
10822 ;; org-ans1: from mouse click
10823 ;; org-ans2: from calendar motion
10824 (setq ans
(concat org-ans0
" " (or org-ans1 org-ans2
))))
10825 (remove-hook 'post-command-hook
'org-read-date-display
)
10826 (use-local-map old-map
)
10827 (when org-read-date-overlay
10828 (org-delete-overlay org-read-date-overlay
)
10829 (setq org-read-date-overlay nil
)))))))
10831 (t ; Naked prompt only
10833 (setq ans
(read-string prompt default-input nil timestr
))
10834 (when org-read-date-overlay
10835 (org-delete-overlay org-read-date-overlay
)
10836 (setq org-read-date-overlay nil
)))))
10838 (setq final
(org-read-date-analyze ans def defdecode
))
10841 (apply 'encode-time final
)
10842 (if (and (boundp 'org-time-was-given
) org-time-was-given
)
10843 (format "%04d-%02d-%02d %02d:%02d"
10844 (nth 5 final
) (nth 4 final
) (nth 3 final
)
10845 (nth 2 final
) (nth 1 final
))
10846 (format "%04d-%02d-%02d" (nth 5 final
) (nth 4 final
) (nth 3 final
))))))
10850 (defun org-read-date-display ()
10851 "Display the currrent date prompt interpretation in the minibuffer."
10852 (when org-read-date-display-live
10853 (when org-read-date-overlay
10854 (org-delete-overlay org-read-date-overlay
))
10857 (while (not (equal (buffer-substring
10858 (max (point-min) (- (point) 4)) (point))
10862 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
10863 " " (or org-ans1 org-ans2
)))
10864 (org-end-time-was-given nil
)
10865 (f (org-read-date-analyze ans def defdecode
))
10867 org-time-stamp-custom-formats
10868 org-time-stamp-formats
))
10869 (fmt (if (or with-time
10870 (and (boundp 'org-time-was-given
) org-time-was-given
))
10873 (txt (concat "=> " (format-time-string fmt
(apply 'encode-time f
)))))
10874 (when (and org-end-time-was-given
10875 (string-match org-plain-time-of-day-regexp txt
))
10876 (setq txt
(concat (substring txt
0 (match-end 0)) "-"
10877 org-end-time-was-given
10878 (substring txt
(match-end 0)))))
10879 (setq org-read-date-overlay
10880 (make-overlay (1- (point-at-eol)) (point-at-eol)))
10881 (org-overlay-display org-read-date-overlay txt
'secondary-selection
))))
10883 (defun org-read-date-analyze (ans def defdecode
)
10884 "Analyze the combined answer of the date prompt."
10885 ;; FIXME: cleanup and comment
10886 (let (delta deltan deltaw deltadef year month day
10887 hour minute second wday pm h2 m2 tl wday1
10888 iso-year iso-weekday iso-week iso-year iso-date
)
10890 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans
)
10893 (when (setq delta
(org-read-date-get-relative ans
(current-time) def
))
10894 (setq ans
(replace-match "" t t ans
)
10896 deltaw
(nth 1 delta
)
10897 deltadef
(nth 2 delta
)))
10899 ;; Check if there is an iso week date in there
10900 ;; If yes, sore the info and ostpone interpreting it until the rest
10901 ;; of the parsing is done
10902 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans
)
10903 (setq iso-year
(if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans
))))
10904 iso-weekday
(if (match-end 3) (string-to-number (match-string 3 ans
)))
10905 iso-week
(string-to-number (match-string 2 ans
)))
10906 (setq ans
(replace-match "" t t ans
)))
10908 ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
10909 (when (string-match
10910 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans
)
10911 (setq year
(if (match-end 2)
10912 (string-to-number (match-string 2 ans
))
10913 (string-to-number (format-time-string "%Y")))
10914 month
(string-to-number (match-string 3 ans
))
10915 day
(string-to-number (match-string 4 ans
)))
10916 (if (< year
100) (setq year
(+ 2000 year
)))
10917 (setq ans
(replace-match (format "%04d-%02d-%02d\\5" year month day
)
10919 ;; Help matching am/pm times, because `parse-time-string' does not do that.
10920 ;; If there is a time with am/pm, and *no* time without it, we convert
10921 ;; so that matching will be successful.
10922 (loop for i from
1 to
2 do
; twice, for end time as well
10923 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans
))
10924 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans
))
10925 (setq hour
(string-to-number (match-string 1 ans
))
10926 minute
(if (match-end 3)
10927 (string-to-number (match-string 3 ans
))
10930 (string-to-char (downcase (match-string 4 ans
)))))
10931 (if (and (= hour
12) (not pm
))
10933 (if (and pm
(< hour
12)) (setq hour
(+ 12 hour
))))
10934 (setq ans
(replace-match (format "%02d:%02d" hour minute
)
10937 ;; Check if a time range is given as a duration
10938 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans
)
10939 (setq hour
(string-to-number (match-string 1 ans
))
10940 h2
(+ hour
(string-to-number (match-string 3 ans
)))
10941 minute
(string-to-number (match-string 2 ans
))
10942 m2
(+ minute
(if (match-end 5) (string-to-number
10943 (match-string 5 ans
))0)))
10944 (if (>= m2
60) (setq h2
(1+ h2
) m2
(- m2
60)))
10945 (setq ans
(replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2
)
10948 ;; Check if there is a time range
10949 (when (boundp 'org-end-time-was-given
)
10950 (setq org-time-was-given nil
)
10951 (when (and (string-match org-plain-time-of-day-regexp ans
)
10953 (setq org-end-time-was-given
(match-string 8 ans
))
10954 (setq ans
(concat (substring ans
0 (match-beginning 7))
10955 (substring ans
(match-end 7))))))
10957 (setq tl
(parse-time-string ans
)
10958 day
(or (nth 3 tl
) (nth 3 defdecode
))
10959 month
(or (nth 4 tl
)
10960 (if (and org-read-date-prefer-future
10961 (nth 3 tl
) (< (nth 3 tl
) (nth 3 defdecode
)))
10962 (1+ (nth 4 defdecode
))
10963 (nth 4 defdecode
)))
10964 year
(or (nth 5 tl
)
10965 (if (and org-read-date-prefer-future
10966 (nth 4 tl
) (< (nth 4 tl
) (nth 4 defdecode
)))
10967 (1+ (nth 5 defdecode
))
10968 (nth 5 defdecode
)))
10969 hour
(or (nth 2 tl
) (nth 2 defdecode
))
10970 minute
(or (nth 1 tl
) (nth 1 defdecode
))
10971 second
(or (nth 0 tl
) 0)
10974 ;; Special date definitions below
10977 ;; There was an iso week
10978 (setq year
(or iso-year year
)
10979 day
(or iso-weekday wday
1)
10980 wday nil
; to make sure that the trigger below does not match
10981 iso-date
(calendar-gregorian-from-absolute
10982 (calendar-absolute-from-iso
10983 (list iso-week day year
))))
10984 ; FIXME: Should we also push ISO weeks into the future?
10985 ; (when (and org-read-date-prefer-future
10987 ; (< (calendar-absolute-from-gregorian iso-date)
10988 ; (time-to-days (current-time))))
10989 ; (setq year (1+ year)
10990 ; iso-date (calendar-gregorian-from-absolute
10991 ; (calendar-absolute-from-iso
10992 ; (list iso-week day year)))))
10993 (setq month
(car iso-date
)
10994 year
(nth 2 iso-date
)
10995 day
(nth 1 iso-date
)))
10998 (let ((now (decode-time (current-time))))
10999 (setq day
(nth 3 now
) month
(nth 4 now
) year
(nth 5 now
))))
11000 (cond ((member deltaw
'("d" "")) (setq day
(+ day deltan
)))
11001 ((equal deltaw
"w") (setq day
(+ day
(* 7 deltan
))))
11002 ((equal deltaw
"m") (setq month
(+ month deltan
)))
11003 ((equal deltaw
"y") (setq year
(+ year deltan
)))))
11004 ((and wday
(not (nth 3 tl
)))
11005 ;; Weekday was given, but no day, so pick that day in the week
11006 ;; on or after the derived date.
11007 (setq wday1
(nth 6 (decode-time (encode-time 0 0 0 day month year
))))
11008 (unless (equal wday wday1
)
11009 (setq day
(+ day
(%
(- wday wday1 -
7) 7))))))
11010 (if (and (boundp 'org-time-was-given
)
11012 (setq org-time-was-given t
))
11013 (if (< year
100) (setq year
(+ 2000 year
)))
11014 (if (< year
1970) (setq year
(nth 5 defdecode
))) ; not representable
11015 (list second minute hour day month year
)))
11017 (defvar parse-time-weekdays
)
11019 (defun org-read-date-get-relative (s today default
)
11020 "Check string S for special relative date string.
11021 TODAY and DEFAULT are internal times, for today and for a default.
11022 Return shift list (N what def-flag)
11023 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
11024 N is the number of WHATs to shift.
11025 DEF-FLAG is t when a double ++ or -- indicates shift relative to
11026 the DEFAULT date rather than TODAY."
11030 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
11032 "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays
"\\|") "\\)\\)?"
11033 "\\([ \t]\\|$\\)") s
)
11034 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
11035 (let* ((dir (if (> (match-end 1) (match-beginning 1))
11036 (string-to-char (substring (match-string 1 s
) -
1))
11038 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
11039 (n (if (match-end 2) (string-to-number (match-string 2 s
)) 1))
11040 (what (if (match-end 3) (match-string 3 s
) "d"))
11041 (wday1 (cdr (assoc (downcase what
) parse-time-weekdays
)))
11042 (date (if rel default today
))
11043 (wday (nth 6 (decode-time date
)))
11047 (setq delta
(mod (+ 7 (- wday1 wday
)) 7))
11048 (if (= dir ?-
) (setq delta
(- delta
7)))
11049 (if (> n
1) (setq delta
(+ delta
(* (1- n
) (if (= dir ?-
) -
7 7)))))
11050 (list delta
"d" rel
))
11051 (list (* n
(if (= dir ?-
) -
1 1)) what rel
)))))
11053 (defun org-eval-in-calendar (form &optional keepdate
)
11054 "Eval FORM in the calendar window and return to current window.
11055 Also, store the cursor date in variable org-ans2."
11056 (let ((sw (selected-window)))
11057 (select-window (get-buffer-window "*Calendar*"))
11059 (when (and (not keepdate
) (calendar-cursor-to-date))
11060 (let* ((date (calendar-cursor-to-date))
11061 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
11062 (setq org-ans2
(format-time-string "%Y-%m-%d" time
))))
11063 (org-move-overlay org-date-ovl
(1- (point)) (1+ (point)) (current-buffer))
11064 (select-window sw
)))
11066 ; ;; Update the prompt to show new default date
11068 ; (goto-char (point-min))
11069 ; (when (and org-ans2
11070 ; (re-search-forward "\\[[-0-9]+\\]" nil t)
11071 ; (get-text-property (match-end 0) 'field))
11072 ; (let ((inhibit-read-only t))
11073 ; (replace-match (concat "[" org-ans2 "]") t t)
11074 ; (add-text-properties (point-min) (1+ (match-end 0))
11075 ; (text-properties-at (1+ (point-min)))))))))
11077 (defun org-calendar-select ()
11078 "Return to `org-read-date' with the date currently selected.
11079 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
11081 (when (calendar-cursor-to-date)
11082 (let* ((date (calendar-cursor-to-date))
11083 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
11084 (setq org-ans1
(format-time-string "%Y-%m-%d" time
)))
11085 (if (active-minibuffer-window) (exit-minibuffer))))
11087 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra
)
11088 "Insert a date stamp for the date given by the internal TIME.
11089 WITH-HM means, use the stamp format that includes the time of the day.
11090 INACTIVE means use square brackets instead of angular ones, so that the
11091 stamp will not contribute to the agenda.
11092 PRE and POST are optional strings to be inserted before and after the
11094 The command returns the inserted time stamp."
11095 (let ((fmt (funcall (if with-hm
'cdr
'car
) org-time-stamp-formats
))
11097 (if inactive
(setq fmt
(concat "[" (substring fmt
1 -
1) "]")))
11098 (insert-before-markers (or pre
""))
11099 (insert-before-markers (setq stamp
(format-time-string fmt time
)))
11100 (when (listp extra
)
11101 (setq extra
(car extra
))
11102 (if (and (stringp extra
)
11103 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra
))
11104 (setq extra
(format "-%02d:%02d"
11105 (string-to-number (match-string 1 extra
))
11106 (string-to-number (match-string 2 extra
))))
11110 (insert-before-markers extra
)
11112 (insert-before-markers (or post
""))
11113 (setq org-last-inserted-timestamp stamp
)))
11115 (defun org-toggle-time-stamp-overlays ()
11116 "Toggle the use of custom time stamp formats."
11118 (setq org-display-custom-times
(not org-display-custom-times
))
11119 (unless org-display-custom-times
11120 (let ((p (point-min)) (bmp (buffer-modified-p)))
11121 (while (setq p
(next-single-property-change p
'display
))
11122 (if (and (get-text-property p
'display
)
11123 (eq (get-text-property p
'face
) 'org-date
))
11124 (remove-text-properties
11125 p
(setq p
(next-single-property-change p
'display
))
11127 (set-buffer-modified-p bmp
)))
11128 (if (featurep 'xemacs
)
11129 (remove-text-properties (point-min) (point-max) '(end-glyph t
)))
11130 (org-restart-font-lock)
11131 (setq org-table-may-need-update t
)
11132 (if org-display-custom-times
11133 (message "Time stamps are overlayed with custom format")
11134 (message "Time stamp overlays removed")))
11136 (defun org-display-custom-time (beg end
)
11137 "Overlay modified time stamp format over timestamp between BEG and END."
11138 (let* ((ts (buffer-substring beg end
))
11139 t1 w1 with-hm tf time str w2
(off 0))
11141 (setq t1
(org-parse-time-string ts t
))
11142 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts
)
11143 (setq off
(- (match-end 0) (match-beginning 0)))))
11144 (setq end
(- end off
))
11145 (setq w1
(- end beg
)
11146 with-hm
(and (nth 1 t1
) (nth 2 t1
))
11147 tf
(funcall (if with-hm
'cdr
'car
) org-time-stamp-custom-formats
)
11148 time
(org-fix-decoded-time t1
)
11150 (format-time-string
11151 (substring tf
1 -
1) (apply 'encode-time time
))
11152 nil
'mouse-face
'highlight
)
11154 (if (not (= w2 w1
))
11155 (add-text-properties (1+ beg
) (+ 2 beg
)
11156 (list 'org-dwidth t
'org-dwidth-n
(- w1 w2
))))
11157 (if (featurep 'xemacs
)
11159 (put-text-property beg end
'invisible t
)
11160 (put-text-property beg end
'end-glyph
(make-glyph str
)))
11161 (put-text-property beg end
'display str
))))
11163 (defun org-translate-time (string)
11164 "Translate all timestamps in STRING to custom format.
11165 But do this only if the variable `org-display-custom-times' is set."
11166 (when org-display-custom-times
11169 (re org-ts-regexp-both
)
11170 t1 with-hm inactive tf time str beg end
)
11171 (while (setq start
(string-match re string start
))
11172 (setq beg
(match-beginning 0)
11174 t1
(save-match-data
11175 (org-parse-time-string (substring string beg end
) t
))
11176 with-hm
(and (nth 1 t1
) (nth 2 t1
))
11177 inactive
(equal (substring string beg
(1+ beg
)) "[")
11178 tf
(funcall (if with-hm
'cdr
'car
)
11179 org-time-stamp-custom-formats
)
11180 time
(org-fix-decoded-time t1
)
11181 str
(format-time-string
11183 (if inactive
"[" "<") (substring tf
1 -
1)
11184 (if inactive
"]" ">"))
11185 (apply 'encode-time time
))
11186 string
(replace-match str t t string
)
11187 start
(+ start
(length str
)))))))
11190 (defun org-fix-decoded-time (time)
11191 "Set 0 instead of nil for the first 6 elements of time.
11192 Don't touch the rest."
11194 (mapcar (lambda (x) (if (< (setq n
(1+ n
)) 7) (or x
0) x
)) time
)))
11196 (defun org-days-to-time (timestamp-string)
11197 "Difference between TIMESTAMP-STRING and now in days."
11198 (- (time-to-days (org-time-string-to-time timestamp-string
))
11199 (time-to-days (current-time))))
11201 (defun org-deadline-close (timestamp-string &optional ndays
)
11202 "Is the time in TIMESTAMP-STRING close to the current date?"
11203 (setq ndays
(or ndays
(org-get-wdays timestamp-string
)))
11204 (and (< (org-days-to-time timestamp-string
) ndays
)
11205 (not (org-entry-is-done-p))))
11207 (defun org-get-wdays (ts)
11208 "Get the deadline lead time appropriate for timestring TS."
11210 ((<= org-deadline-warning-days
0)
11211 ;; 0 or negative, enforce this value no matter what
11212 (- org-deadline-warning-days
))
11213 ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts
)
11214 ;; lead time is specified.
11215 (floor (* (string-to-number (match-string 1 ts
))
11216 (cdr (assoc (match-string 2 ts
)
11217 '(("d" .
1) ("w" .
7)
11218 ("m" .
30.4) ("y" .
365.25)))))))
11219 ;; go for the default.
11220 (t org-deadline-warning-days
)))
11222 (defun org-calendar-select-mouse (ev)
11223 "Return to `org-read-date' with the date currently selected.
11224 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
11226 (mouse-set-point ev
)
11227 (when (calendar-cursor-to-date)
11228 (let* ((date (calendar-cursor-to-date))
11229 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
11230 (setq org-ans1
(format-time-string "%Y-%m-%d" time
)))
11231 (if (active-minibuffer-window) (exit-minibuffer))))
11233 (defun org-check-deadlines (ndays)
11234 "Check if there are any deadlines due or past due.
11235 A deadline is considered due if it happens within `org-deadline-warning-days'
11236 days from today's date. If the deadline appears in an entry marked DONE,
11237 it is not shown. The prefix arg NDAYS can be used to test that many
11238 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
11240 (let* ((org-warn-days
11242 ((equal ndays
'(4)) 100000)
11243 (ndays (prefix-numeric-value ndays
))
11244 (t (abs org-deadline-warning-days
))))
11245 (case-fold-search nil
)
11246 (regexp (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>"))
11248 (lambda () (org-deadline-close (match-string 1) org-warn-days
))))
11250 (message "%d deadlines past-due or due within %d days"
11251 (org-occur regexp nil callback
)
11254 (defun org-check-before-date (date)
11255 "Check if there are deadlines or scheduled entries before DATE."
11256 (interactive (list (org-read-date)))
11257 (let ((case-fold-search nil
)
11258 (regexp (concat "\\<\\(" org-deadline-string
11259 "\\|" org-scheduled-string
11260 "\\) *<\\([^>]+\\)>"))
11262 (lambda () (time-less-p
11263 (org-time-string-to-time (match-string 2))
11264 (org-time-string-to-time date
)))))
11265 (message "%d entries before %s"
11266 (org-occur regexp nil callback
) date
)))
11268 (defun org-evaluate-time-range (&optional to-buffer
)
11269 "Evaluate a time range by computing the difference between start and end.
11270 Normally the result is just printed in the echo area, but with prefix arg
11271 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
11272 If the time range is actually in a table, the result is inserted into the
11274 For time difference computation, a year is assumed to be exactly 365
11275 days in order to avoid rounding problems."
11278 (org-clock-update-time-maybe)
11280 (unless (org-at-date-range-p t
)
11281 (goto-char (point-at-bol))
11282 (re-search-forward org-tr-regexp-both
(point-at-eol) t
))
11283 (if (not (org-at-date-range-p t
))
11284 (error "Not at a time-stamp range, and none found in current line")))
11285 (let* ((ts1 (match-string 1))
11286 (ts2 (match-string 2))
11287 (havetime (or (> (length ts1
) 15) (> (length ts2
) 15)))
11288 (match-end (match-end 0))
11289 (time1 (org-time-string-to-time ts1
))
11290 (time2 (org-time-string-to-time ts2
))
11291 (t1 (time-to-seconds time1
))
11292 (t2 (time-to-seconds time2
))
11293 (diff (abs (- t2 t1
)))
11294 (negative (< (- t2 t1
) 0))
11295 ;; (ys (floor (* 365 24 60 60)))
11298 (fy "%dy %dd %02d:%02d")
11300 (fd "%dd %02d:%02d")
11305 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
11307 d
(floor (/ diff ds
)) diff
(mod diff ds
)
11308 h
(floor (/ diff hs
)) diff
(mod diff hs
)
11309 m
(floor (/ diff
60)))
11310 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
11312 d
(floor (+ (/ diff ds
) 0.5))
11314 (if (not to-buffer
)
11315 (message "%s" (org-make-tdiff-string y d h m
))
11316 (if (org-at-table-p)
11318 (goto-char match-end
)
11320 (and (looking-at " *|") (goto-char (match-end 0))))
11321 (goto-char match-end
))
11323 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
11324 (replace-match ""))
11325 (if negative
(insert " -"))
11326 (if (> y
0) (insert " " (format (if havetime fy fy1
) y d h m
))
11327 (if (> d
0) (insert " " (format (if havetime fd fd1
) d h m
))
11328 (insert " " (format fh h m
))))
11329 (if align
(org-table-align))
11330 (message "Time difference inserted")))))
11332 (defun org-make-tdiff-string (y d h m
)
11335 (if (> y
0) (setq fmt
(concat fmt
"%d year" (if (> y
1) "s" "") " ")
11337 (if (> d
0) (setq fmt
(concat fmt
"%d day" (if (> d
1) "s" "") " ")
11339 (if (> h
0) (setq fmt
(concat fmt
"%d hour" (if (> h
1) "s" "") " ")
11341 (if (> m
0) (setq fmt
(concat fmt
"%d minute" (if (> m
1) "s" "") " ")
11343 (apply 'format fmt
(nreverse l
))))
11345 (defun org-time-string-to-time (s)
11346 (apply 'encode-time
(org-parse-time-string s
)))
11348 (defun org-time-string-to-absolute (s &optional daynr prefer show-all
)
11349 "Convert a time stamp to an absolute day number.
11350 If there is a specifyer for a cyclic time stamp, get the closest date to
11352 PREFER and SHOW_ALL are passed through to `org-closest-date'."
11354 ((and daynr
(string-match "\\`%%\\((.*)\\)" s
))
11355 (if (org-diary-sexp-entry (match-string 1 s
) "" date
)
11358 ((and daynr
(string-match "\\+[0-9]+[dwmy]" s
))
11359 (org-closest-date s
(if (and (boundp 'daynr
) (integerp daynr
)) daynr
11360 (time-to-days (current-time))) (match-string 0 s
)
11362 (t (time-to-days (apply 'encode-time
(org-parse-time-string s
))))))
11364 (defun org-days-to-iso-week (days)
11365 "Return the iso week number."
11367 (car (calendar-iso-from-absolute days
)))
11369 (defun org-small-year-to-year (year)
11370 "Convert 2-digit years into 4-digit years.
11371 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
11372 The year 2000 cannot be abbreviated. Any year lager than 99
11373 is retrned unchanged."
11375 (setq year
(+ 2000 year
))
11377 (setq year
(+ 1900 year
))))
11380 (defun org-time-from-absolute (d)
11381 "Return the time corresponding to date D.
11382 D may be an absolute day number, or a calendar-type list (month day year)."
11383 (if (numberp d
) (setq d
(calendar-gregorian-from-absolute d
)))
11384 (encode-time 0 0 0 (nth 1 d
) (car d
) (nth 2 d
)))
11386 (defun org-calendar-holiday ()
11387 "List of holidays, for Diary display in Org-mode."
11388 (require 'holidays
)
11390 (if (fboundp 'calendar-check-holidays
)
11391 'calendar-check-holidays
'check-calendar-holidays
) date
)))
11392 (if hl
(mapconcat 'identity hl
"; "))))
11394 (defun org-diary-sexp-entry (sexp entry date
)
11395 "Process a SEXP diary ENTRY for DATE."
11396 (require 'diary-lib
)
11397 (let ((result (if calendar-debug-sexp
11398 (let ((stack-trace-on-error t
))
11399 (eval (car (read-from-string sexp
))))
11400 (condition-case nil
11401 (eval (car (read-from-string sexp
)))
11404 (message "Bad sexp at line %d in %s: %s"
11406 (buffer-file-name) sexp
)
11408 (cond ((stringp result
) result
)
11409 ((and (consp result
)
11410 (stringp (cdr result
))) (cdr result
))
11414 (defun org-diary-to-ical-string (frombuf)
11415 "Get iCalendar entries from diary entries in buffer FROMBUF.
11416 This uses the icalendar.el library."
11417 (let* ((tmpdir (if (featurep 'xemacs
)
11419 temporary-file-directory
))
11420 (tmpfile (make-temp-name
11421 (expand-file-name "orgics" tmpdir
)))
11424 (set-buffer frombuf
)
11425 (icalendar-export-region (point-min) (point-max) tmpfile
)
11426 (setq buf
(find-buffer-visiting tmpfile
))
11428 (goto-char (point-min))
11429 (if (re-search-forward "^BEGIN:VEVENT" nil t
)
11430 (setq b
(match-beginning 0)))
11431 (goto-char (point-max))
11432 (if (re-search-backward "^END:VEVENT" nil t
)
11433 (setq e
(match-end 0)))
11434 (setq rtn
(if (and b e
) (concat (buffer-substring b e
) "\n") "")))
11436 (delete-file tmpfile
)
11439 (defun org-closest-date (start current change prefer show-all
)
11440 "Find the date closest to CURRENT that is consistent with START and CHANGE.
11441 When PREFER is `past' return a date that is either CURRENT or past.
11442 When PREFER is `future', return a date that is either CURRENT or future.
11443 When SHOW-ALL is nil, only return the current occurence of a time stamp."
11444 ;; Make the proper lists from the dates
11446 (let ((a1 '(("d" . day
) ("w" . week
) ("m" . month
) ("y" . year
)))
11447 dn dw sday cday n1 n2
11448 d m y y1 y2 date1 date2 nmonths nm ny m2
)
11450 (setq start
(org-date-to-gregorian start
)
11451 current
(org-date-to-gregorian
11454 (time-to-days (current-time))))
11455 sday
(calendar-absolute-from-gregorian start
)
11456 cday
(calendar-absolute-from-gregorian current
))
11458 (if (<= cday sday
) (throw 'exit sday
))
11460 (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change
)
11461 (setq dn
(string-to-number (match-string 1 change
))
11462 dw
(cdr (assoc (match-string 2 change
) a1
)))
11463 (error "Invalid change specifyer: %s" change
))
11464 (if (eq dw
'week
) (setq dw
'day dn
(* 7 dn
)))
11467 (setq n1
(+ sday
(* dn
(floor (/ (- cday sday
) dn
))))
11470 (setq d
(nth 1 start
) m
(car start
) y1
(nth 2 start
) y2
(nth 2 current
))
11471 (setq y1
(+ (* (floor (/ (- y2 y1
) dn
)) dn
) y1
))
11472 (setq date1
(list m d y1
)
11473 n1
(calendar-absolute-from-gregorian date1
)
11474 date2
(list m d
(+ y1
(* (if (< n1 cday
) 1 -
1) dn
)))
11475 n2
(calendar-absolute-from-gregorian date2
)))
11477 ;; approx number of month between the tow dates
11478 (setq nmonths
(floor (/ (- cday sday
) 30.436875)))
11479 ;; How often does dn fit in there?
11480 (setq d
(nth 1 start
) m
(car start
) y
(nth 2 start
)
11481 nm
(* dn
(max 0 (1- (floor (/ nmonths dn
)))))
11483 ny
(floor (/ m
12))
11486 (while (> m
12) (setq m
(- m
12) y
(1+ y
)))
11487 (setq n1
(calendar-absolute-from-gregorian (list m d y
)))
11488 (setq m2
(+ m dn
) y2 y
)
11489 (if (> m2
12) (setq y2
(1+ y2
) m2
(- m2
12)))
11490 (setq n2
(calendar-absolute-from-gregorian (list m2 d y2
)))
11492 (setq n1 n2 m m2 y y2
)
11493 (setq m2
(+ m dn
) y2 y
)
11494 (if (> m2
12) (setq y2
(1+ y2
) m2
(- m2
12)))
11495 (setq n2
(calendar-absolute-from-gregorian (list m2 d y2
))))))
11499 ((eq prefer
'past
) n1
)
11500 ((eq prefer
'future
) (if (= cday n1
) n1 n2
))
11501 (t (if (> (abs (- cday n1
)) (abs (- cday n2
))) n2 n1
)))
11503 ((eq prefer
'past
) n1
)
11504 ((eq prefer
'future
) (if (= cday n1
) n1 n2
))
11505 (t (if (= cday n1
) n1 n2
)))))))
11507 (defun org-date-to-gregorian (date)
11508 "Turn any specification of DATE into a gregorian date for the calendar."
11509 (cond ((integerp date
) (calendar-gregorian-from-absolute date
))
11510 ((and (listp date
) (= (length date
) 3)) date
)
11512 (setq date
(org-parse-time-string date
))
11513 (list (nth 4 date
) (nth 3 date
) (nth 5 date
)))
11515 (list (nth 4 date
) (nth 3 date
) (nth 5 date
)))))
11517 (defun org-parse-time-string (s &optional nodefault
)
11518 "Parse the standard Org-mode time string.
11519 This should be a lot faster than the normal `parse-time-string'.
11520 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
11521 hour and minute fields will be nil if not given."
11522 (if (string-match org-ts-regexp0 s
)
11524 (if (or (match-beginning 8) (not nodefault
))
11525 (string-to-number (or (match-string 8 s
) "0")))
11526 (if (or (match-beginning 7) (not nodefault
))
11527 (string-to-number (or (match-string 7 s
) "0")))
11528 (string-to-number (match-string 4 s
))
11529 (string-to-number (match-string 3 s
))
11530 (string-to-number (match-string 2 s
))
11534 (defun org-timestamp-up (&optional arg
)
11535 "Increase the date item at the cursor by one.
11536 If the cursor is on the year, change the year. If it is on the month or
11537 the day, change that.
11538 With prefix ARG, change by that many units."
11540 (org-timestamp-change (prefix-numeric-value arg
)))
11542 (defun org-timestamp-down (&optional arg
)
11543 "Decrease the date item at the cursor by one.
11544 If the cursor is on the year, change the year. If it is on the month or
11545 the day, change that.
11546 With prefix ARG, change by that many units."
11548 (org-timestamp-change (- (prefix-numeric-value arg
))))
11550 (defun org-timestamp-up-day (&optional arg
)
11551 "Increase the date in the time stamp by one day.
11552 With prefix ARG, change that many days."
11554 (if (and (not (org-at-timestamp-p t
))
11555 (org-on-heading-p))
11557 (org-timestamp-change (prefix-numeric-value arg
) 'day
)))
11559 (defun org-timestamp-down-day (&optional arg
)
11560 "Decrease the date in the time stamp by one day.
11561 With prefix ARG, change that many days."
11563 (if (and (not (org-at-timestamp-p t
))
11564 (org-on-heading-p))
11566 (org-timestamp-change (- (prefix-numeric-value arg
)) 'day
)))
11568 (defun org-at-timestamp-p (&optional inactive-ok
)
11569 "Determine if the cursor is in or at a timestamp."
11571 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2
))
11573 (ans (or (looking-at tsr
)
11575 (skip-chars-backward "^[<\n\r\t")
11576 (if (> (point) (point-min)) (backward-char 1))
11577 (and (looking-at tsr
)
11578 (> (- (match-end 0) pos
) -
1))))))
11580 (boundp 'org-ts-what
)
11583 ((= pos
(match-beginning 0)) 'bracket
)
11584 ((= pos
(1- (match-end 0))) 'bracket
)
11585 ((org-pos-in-match-range pos
2) 'year
)
11586 ((org-pos-in-match-range pos
3) 'month
)
11587 ((org-pos-in-match-range pos
7) 'hour
)
11588 ((org-pos-in-match-range pos
8) 'minute
)
11589 ((or (org-pos-in-match-range pos
4)
11590 (org-pos-in-match-range pos
5)) 'day
)
11591 ((and (> pos
(or (match-end 8) (match-end 5)))
11592 (< pos
(match-end 0)))
11593 (- pos
(or (match-end 8) (match-end 5))))
11597 (defun org-toggle-timestamp-type ()
11598 "Toggle the type (<active> or [inactive]) of a time stamp."
11600 (when (org-at-timestamp-p t
)
11602 (goto-char (match-beginning 0))
11603 (insert (if (equal (char-after) ?
<) "[" "<")) (delete-char 1)
11604 (goto-char (1- (match-end 0)))
11605 (insert (if (equal (char-after) ?
>) "]" ">")) (delete-char 1))
11606 (message "Timestamp is now %sactive"
11607 (if (equal (char-before) ?
>) "in" ""))))
11609 (defun org-timestamp-change (n &optional what
)
11610 "Change the date in the time stamp at point.
11611 The date will be changed by N times WHAT. WHAT can be `day', `month',
11612 `year', `minute', `second'. If WHAT is not given, the cursor position
11613 in the timestamp determines what will be changed."
11614 (let ((pos (point))
11616 (dm (max (nth 1 org-time-stamp-rounding-minutes
) 1))
11620 (if (not (org-at-timestamp-p t
))
11621 (error "Not at a timestamp"))
11622 (if (and (not what
) (eq org-ts-what
'bracket
))
11623 (org-toggle-timestamp-type)
11624 (if (and (not what
) (not (eq org-ts-what
'day
))
11625 org-display-custom-times
11626 (get-text-property (point) 'display
)
11627 (not (get-text-property (1- (point)) 'display
)))
11628 (setq org-ts-what
'day
))
11629 (setq org-ts-what
(or what org-ts-what
)
11630 inactive
(= (char-after (match-beginning 0)) ?\
[)
11631 ts
(match-string 0))
11634 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
11636 (setq extra
(match-string 1 ts
)))
11637 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts
)
11639 (setq time0
(org-parse-time-string ts
))
11640 (when (and (eq org-ts-what
'minute
)
11641 (eq current-prefix-arg nil
))
11642 (setq n
(* dm
(cond ((> n
0) 1) ((< n
0) -
1) (t 0))))
11643 (when (not (= 0 (setq rem
(%
(nth 1 time0
) dm
))))
11644 (setcar (cdr time0
) (+ (nth 1 time0
)
11645 (if (> n
0) (- rem
) (- dm rem
))))))
11647 (encode-time (or (car time0
) 0)
11648 (+ (if (eq org-ts-what
'minute
) n
0) (nth 1 time0
))
11649 (+ (if (eq org-ts-what
'hour
) n
0) (nth 2 time0
))
11650 (+ (if (eq org-ts-what
'day
) n
0) (nth 3 time0
))
11651 (+ (if (eq org-ts-what
'month
) n
0) (nth 4 time0
))
11652 (+ (if (eq org-ts-what
'year
) n
0) (nth 5 time0
))
11654 (when (integerp org-ts-what
)
11655 (setq extra
(org-modify-ts-extra extra org-ts-what n dm
)))
11656 (if (eq what
'calendar
)
11657 (let ((cal-date (org-get-date-from-calendar)))
11658 (setcar (nthcdr 4 time0
) (nth 0 cal-date
)) ; month
11659 (setcar (nthcdr 3 time0
) (nth 1 cal-date
)) ; day
11660 (setcar (nthcdr 5 time0
) (nth 2 cal-date
)) ; year
11661 (setcar time0
(or (car time0
) 0))
11662 (setcar (nthcdr 1 time0
) (or (nth 1 time0
) 0))
11663 (setcar (nthcdr 2 time0
) (or (nth 2 time0
) 0))
11664 (setq time
(apply 'encode-time time0
))))
11665 (setq org-last-changed-timestamp
11666 (org-insert-time-stamp time with-hm inactive nil nil extra
))
11667 (org-clock-update-time-maybe)
11669 ;; Try to recenter the calendar window, if any
11670 (if (and org-calendar-follow-timestamp-change
11671 (get-buffer-window "*Calendar*" t
)
11672 (memq org-ts-what
'(day month year
)))
11673 (org-recenter-calendar (time-to-days time
))))))
11675 (defun org-modify-ts-extra (s pos n dm
)
11676 "Change the different parts of the lead-time and repeat fields in timestamp."
11677 (let ((idx '(("d" .
0) ("w" .
1) ("m" .
2) ("y" .
3) ("d" . -
1) ("y" .
4)))
11679 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s
)
11681 ((or (org-pos-in-match-range pos
2)
11682 (org-pos-in-match-range pos
3))
11683 (setq m
(string-to-number (match-string 3 s
))
11684 h
(string-to-number (match-string 2 s
)))
11685 (if (org-pos-in-match-range pos
2)
11687 (setq n
(* dm
(org-no-warnings (signum n
))))
11688 (when (not (= 0 (setq rem
(% m dm
))))
11689 (setq m
(+ m
(if (> n
0) (- rem
) (- dm rem
)))))
11691 (if (< m
0) (setq m
(+ m
60) h
(1- h
)))
11692 (if (> m
59) (setq m
(- m
60) h
(1+ h
)))
11693 (setq h
(min 24 (max 0 h
)))
11694 (setq ng
1 new
(format "-%02d:%02d" h m
)))
11695 ((org-pos-in-match-range pos
6)
11696 (setq ng
6 new
(car (rassoc (+ n
(cdr (assoc (match-string 6 s
) idx
))) idx
))))
11697 ((org-pos-in-match-range pos
5)
11698 (setq ng
5 new
(format "%d" (max 1 (+ n
(string-to-number (match-string 5 s
)))))))
11700 ((org-pos-in-match-range pos
9)
11701 (setq ng
9 new
(car (rassoc (+ n
(cdr (assoc (match-string 9 s
) idx
))) idx
))))
11702 ((org-pos-in-match-range pos
8)
11703 (setq ng
8 new
(format "%d" (max 0 (+ n
(string-to-number (match-string 8 s
))))))))
11707 (substring s
0 (match-beginning ng
))
11709 (substring s
(match-end ng
))))))
11712 (defun org-recenter-calendar (date)
11713 "If the calendar is visible, recenter it to DATE."
11714 (let* ((win (selected-window))
11715 (cwin (get-buffer-window "*Calendar*" t
))
11716 (calendar-move-hook nil
))
11718 (select-window cwin
)
11719 (calendar-goto-date (if (listp date
) date
11720 (calendar-gregorian-from-absolute date
)))
11721 (select-window win
))))
11723 (defun org-goto-calendar (&optional arg
)
11724 "Go to the Emacs calendar at the current date.
11725 If there is a time stamp in the current line, go to that date.
11726 A prefix ARG can be used to force the current date."
11728 (let ((tsr org-ts-regexp
) diff
11729 (calendar-move-hook nil
)
11730 (calendar-view-holidays-initially-flag nil
)
11731 (view-calendar-holidays-initially nil
)
11732 (calendar-view-diary-initially-flag nil
)
11733 (view-diary-entries-initially nil
))
11734 (if (or (org-at-timestamp-p)
11736 (beginning-of-line 1)
11737 (looking-at (concat ".*" tsr
))))
11738 (let ((d1 (time-to-days (current-time)))
11740 (org-time-string-to-time (match-string 1)))))
11741 (setq diff
(- d2 d1
))))
11743 (calendar-goto-today)
11744 (if (and diff
(not arg
)) (calendar-forward-day diff
))))
11746 (defun org-get-date-from-calendar ()
11747 "Return a list (month day year) of date at point in calendar."
11748 (with-current-buffer "*Calendar*"
11750 (calendar-cursor-to-date))))
11752 (defun org-date-from-calendar ()
11753 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
11754 If there is already a time stamp at the cursor position, update it."
11756 (if (org-at-timestamp-p t
)
11757 (org-timestamp-change 0 'calendar
)
11758 (let ((cal-date (org-get-date-from-calendar)))
11759 (org-insert-time-stamp
11760 (encode-time 0 0 0 (nth 1 cal-date
) (car cal-date
) (nth 2 cal-date
))))))
11762 (defun org-minutes-to-hh:mm-string
(m)
11763 "Compute H:MM from a number of minutes."
11764 (let ((h (/ m
60)))
11765 (setq m
(- m
(* 60 h
)))
11766 (format org-time-clocksum-format h m
)))
11768 (defun org-hh:mm-string-to-minutes
(s)
11769 "Convert a string H:MM to a number of minutes."
11770 (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s
)
11771 (+ (* (string-to-number (match-string 1 s
)) 60)
11772 (string-to-number (match-string 2 s
)))
11778 (defun org-iswitchb (&optional arg
)
11779 "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
11780 With a prefix argument, restrict available to files.
11781 With two prefix arguments, restrict available buffers to agenda files.
11783 Due to some yet unresolved reason, global function
11784 `iswitchb-mode' needs to be active for this function to work."
11786 (require 'iswitchb
)
11787 (let ((enabled iswitchb-mode
) blist
)
11788 (or enabled
(iswitchb-mode 1))
11789 (setq blist
(cond ((equal arg
'(4)) (org-buffer-list 'files
))
11790 ((equal arg
'(16)) (org-buffer-list 'agenda
))
11791 (t (org-buffer-list))))
11793 (let ((iswitchb-make-buflist-hook
11795 (setq iswitchb-temp-buflist
11796 (mapcar 'buffer-name blist
)))))
11798 (iswitchb-read-buffer
11799 "Switch-to: " nil t
))
11800 (or enabled
(iswitchb-mode -
1))))))
11802 (defun org-buffer-list (&optional predicate tmp
)
11803 "Return a list of Org buffers.
11804 PREDICATE can be either 'export, 'files or 'agenda.
11806 'export restrict the list to Export buffers.
11807 'files restrict the list to buffers visiting Org files.
11808 'agenda restrict the list to buffers visiting agenda files.
11810 If TMP is non-nil, don't include temporary buffers."
11811 (let (filter blist
)
11813 (cond ((eq predicate
'files
) "\.org$")
11814 ((eq predicate
'export
) "\*Org .*Export")
11815 (t "\*Org \\|\.org$")))
11819 (let ((bname (buffer-name b
))
11820 (bfile (buffer-file-name b
)))
11821 (if (and (string-match filter bname
)
11822 (if (eq predicate
'agenda
)
11824 (mapcar (lambda(f) (file-truename f
))
11825 org-agenda-files
)) t
)
11826 (if tmp
(not (string-match "tmp" bname
)) t
)) b
)))
11828 (delete nil blist
)))
11830 (defun org-agenda-files (&optional unrestricted ext
)
11831 "Get the list of agenda files.
11832 Optional UNRESTRICTED means return the full list even if a restriction
11833 is currently in place.
11834 When EXT is non-nil, try to add all files that are created by adding EXT
11835 to the file nemes. Basically, this is a way to add the archive files
11836 to the list, by setting EXT to \"_archive\" If EXT is non-nil, but not
11837 a string, \"_archive\" will be used."
11840 ((and (not unrestricted
) (get 'org-agenda-files
'org-restrict
)))
11841 ((stringp org-agenda-files
) (org-read-agenda-file-list))
11842 ((listp org-agenda-files
) org-agenda-files
)
11843 (t (error "Invalid value of `org-agenda-files'")))))
11844 (setq files
(apply 'append
11845 (mapcar (lambda (f)
11846 (if (file-directory-p f
)
11848 f t org-agenda-file-regexp
)
11851 (when org-agenda-skip-unavailable-files
11852 (setq files
(delq nil
11855 (and (file-readable-p file
) file
)))
11858 (setq ext
(if (and (stringp ext
) (string-match "\\S-" ext
))
11860 (setq files
(apply 'append
11863 (if (file-exists-p (concat f ext
))
11864 (list f
(concat f ext
))
11869 (defun org-edit-agenda-file-list ()
11870 "Edit the list of agenda files.
11871 Depending on setup, this either uses customize to edit the variable
11872 `org-agenda-files', or it visits the file that is holding the list. In the
11873 latter case, the buffer is set up in a way that saving it automatically kills
11874 the buffer and restores the previous window configuration."
11876 (if (stringp org-agenda-files
)
11877 (let ((cw (current-window-configuration)))
11878 (find-file org-agenda-files
)
11879 (org-set-local 'org-window-configuration cw
)
11880 (org-add-hook 'after-save-hook
11882 (set-window-configuration
11883 (prog1 org-window-configuration
11884 (kill-buffer (current-buffer))))
11885 (org-install-agenda-files-menu)
11886 (message "New agenda file list installed"))
11888 (message "%s" (substitute-command-keys
11889 "Edit list and finish with \\[save-buffer]")))
11890 (customize-variable 'org-agenda-files
)))
11892 (defun org-store-new-agenda-file-list (list)
11893 "Set new value for the agenda file list and save it correcly."
11894 (if (stringp org-agenda-files
)
11895 (let ((f org-agenda-files
) b
)
11896 (while (setq b
(find-buffer-visiting f
)) (kill-buffer b
))
11898 (insert (mapconcat 'identity list
"\n") "\n")))
11899 (let ((org-mode-hook nil
) (default-major-mode 'fundamental-mode
))
11900 (setq org-agenda-files list
)
11901 (customize-save-variable 'org-agenda-files org-agenda-files
))))
11903 (defun org-read-agenda-file-list ()
11904 "Read the list of agenda files from a file."
11905 (when (file-directory-p org-agenda-files
)
11906 (error "`org-agenda-files' cannot be a single directory"))
11907 (when (stringp org-agenda-files
)
11909 (insert-file-contents org-agenda-files
)
11910 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
11914 (defun org-cycle-agenda-files ()
11915 "Cycle through the files in `org-agenda-files'.
11916 If the current buffer visits an agenda file, find the next one in the list.
11917 If the current buffer does not, find the first agenda file."
11919 (let* ((fs (org-agenda-files t
))
11920 (files (append fs
(list (car fs
))))
11921 (tcf (if buffer-file-name
(file-truename buffer-file-name
)))
11923 (unless files
(error "No agenda files"))
11925 (while (setq file
(pop files
))
11926 (if (equal (file-truename file
) tcf
)
11928 (find-file (car files
))
11930 (find-file (car fs
)))
11931 (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
11933 (defun org-agenda-file-to-front (&optional to-end
)
11934 "Move/add the current file to the top of the agenda file list.
11935 If the file is not present in the list, it is added to the front. If it is
11936 present, it is moved there. With optional argument TO-END, add/move to the
11939 (let ((org-agenda-skip-unavailable-files nil
)
11940 (file-alist (mapcar (lambda (x)
11941 (cons (file-truename x
) x
))
11942 (org-agenda-files t
)))
11943 (ctf (file-truename buffer-file-name
))
11945 (setq x
(assoc ctf file-alist
) had x
)
11947 (if (not x
) (setq x
(cons ctf
(abbreviate-file-name buffer-file-name
))))
11949 (setq file-alist
(append (delq x file-alist
) (list x
)))
11950 (setq file-alist
(cons x
(delq x file-alist
))))
11951 (org-store-new-agenda-file-list (mapcar 'cdr file-alist
))
11952 (org-install-agenda-files-menu)
11953 (message "File %s to %s of agenda file list"
11954 (if had
"moved" "added") (if to-end
"end" "front"))))
11956 (defun org-remove-file (&optional file
)
11957 "Remove current file from the list of files in variable `org-agenda-files'.
11958 These are the files which are being checked for agenda entries.
11959 Optional argument FILE means, use this file instead of the current."
11961 (let* ((org-agenda-skip-unavailable-files nil
)
11962 (file (or file buffer-file-name
))
11963 (true-file (file-truename file
))
11964 (afile (abbreviate-file-name file
))
11965 (files (delq nil
(mapcar
11967 (if (equal true-file
11970 (org-agenda-files t
)))))
11971 (if (not (= (length files
) (length (org-agenda-files t
))))
11973 (org-store-new-agenda-file-list files
)
11974 (org-install-agenda-files-menu)
11975 (message "Removed file: %s" afile
))
11976 (message "File was not in list: %s (not removed)" afile
))))
11978 (defun org-file-menu-entry (file)
11979 (vector file
(list 'find-file file
) t
))
11981 (defun org-check-agenda-file (file)
11982 "Make sure FILE exists. If not, ask user what to do."
11983 (when (not (file-exists-p file
))
11984 (message "non-existent file %s. [R]emove from list or [A]bort?"
11985 (abbreviate-file-name file
))
11986 (let ((r (downcase (read-char-exclusive))))
11989 (org-remove-file file
)
11990 (throw 'nextfile t
))
11991 (t (error "Abort"))))))
11993 (defun org-get-agenda-file-buffer (file)
11994 "Get a buffer visiting FILE. If the buffer needs to be created, add
11995 it to the list of buffers which might be released later."
11996 (let ((buf (org-find-base-buffer-visiting file
)))
11998 buf
; just return it
11999 ;; Make a new buffer and remember it
12000 (setq buf
(find-file-noselect file
))
12001 (if buf
(push buf org-agenda-new-buffers
))
12004 (defun org-release-buffers (blist)
12005 "Release all buffers in list, asking the user for confirmation when needed.
12006 When a buffer is unmodified, it is just killed. When modified, it is saved
12007 \(if the user agrees) and then killed."
12009 (while (setq buf
(pop blist
))
12010 (setq file
(buffer-file-name buf
))
12011 (when (and (buffer-modified-p buf
)
12013 (y-or-n-p (format "Save file %s? " file
)))
12014 (with-current-buffer buf
(save-buffer)))
12015 (kill-buffer buf
))))
12017 (defun org-prepare-agenda-buffers (files)
12018 "Create buffers for all agenda files, protect archived trees and comments."
12020 (let ((pa '(:org-archived t
))
12021 (pc '(:org-comment t
))
12022 (pall '(:org-archived t
:org-comment t
))
12023 (inhibit-read-only t
)
12024 (rea (concat ":" org-archive-tag
":"))
12028 (while (setq file
(pop files
))
12031 (org-check-agenda-file file
)
12032 (set-buffer (org-get-agenda-file-buffer file
)))
12034 (setq bmp
(buffer-modified-p))
12035 (org-refresh-category-properties)
12036 (setq org-todo-keywords-for-agenda
12037 (append org-todo-keywords-for-agenda org-todo-keywords-1
))
12038 (setq org-done-keywords-for-agenda
12039 (append org-done-keywords-for-agenda org-done-keywords
))
12041 (remove-text-properties (point-min) (point-max) pall
)
12042 (when org-agenda-skip-archived-trees
12043 (goto-char (point-min))
12044 (while (re-search-forward rea nil t
)
12045 (if (org-on-heading-p t
)
12046 (add-text-properties (point-at-bol) (org-end-of-subtree t
) pa
))))
12047 (goto-char (point-min))
12048 (setq re
(concat "^\\*+ +" org-comment-string
"\\>"))
12049 (while (re-search-forward re nil t
)
12050 (add-text-properties
12051 (match-beginning 0) (org-end-of-subtree t
) pc
)))
12052 (set-buffer-modified-p bmp
))))))
12054 ;;;; Embedded LaTeX
12056 (defvar org-cdlatex-mode-map
(make-sparse-keymap)
12057 "Keymap for the minor `org-cdlatex-mode'.")
12059 (org-defkey org-cdlatex-mode-map
"_" 'org-cdlatex-underscore-caret
)
12060 (org-defkey org-cdlatex-mode-map
"^" 'org-cdlatex-underscore-caret
)
12061 (org-defkey org-cdlatex-mode-map
"`" 'cdlatex-math-symbol
)
12062 (org-defkey org-cdlatex-mode-map
"'" 'org-cdlatex-math-modify
)
12063 (org-defkey org-cdlatex-mode-map
"\C-c{" 'cdlatex-environment
)
12065 (defvar org-cdlatex-texmathp-advice-is-done nil
12066 "Flag remembering if we have applied the advice to texmathp already.")
12068 (define-minor-mode org-cdlatex-mode
12069 "Toggle the minor `org-cdlatex-mode'.
12070 This mode supports entering LaTeX environment and math in LaTeX fragments
12072 \\{org-cdlatex-mode-map}"
12074 (when org-cdlatex-mode
(require 'cdlatex
))
12075 (unless org-cdlatex-texmathp-advice-is-done
12076 (setq org-cdlatex-texmathp-advice-is-done t
)
12077 (defadvice texmathp
(around org-math-always-on activate
)
12078 "Always return t in org-mode buffers.
12079 This is because we want to insert math symbols without dollars even outside
12080 the LaTeX math segments. If Orgmode thinks that point is actually inside
12081 en embedded LaTeX fragement, let texmathp do its job.
12082 \\[org-cdlatex-mode-map]"
12086 ((not (org-mode-p)) ad-do-it
)
12087 ((eq this-command
'cdlatex-math-symbol
)
12088 (setq ad-return-value t
12089 texmathp-why
'("cdlatex-math-symbol in org-mode" .
0)))
12091 (let ((p (org-inside-LaTeX-fragment-p)))
12092 (if (and p
(member (car p
) (plist-get org-format-latex-options
:matchers
)))
12093 (setq ad-return-value t
12094 texmathp-why
'("Org-mode embedded math" .
0))
12095 (if p ad-do-it
)))))))))
12097 (defun turn-on-org-cdlatex ()
12098 "Unconditionally turn on `org-cdlatex-mode'."
12099 (org-cdlatex-mode 1))
12101 (defun org-inside-LaTeX-fragment-p ()
12102 "Test if point is inside a LaTeX fragment.
12103 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
12104 sequence appearing also before point.
12105 Even though the matchers for math are configurable, this function assumes
12106 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
12107 delimiters are skipped when they have been removed by customization.
12108 The return value is nil, or a cons cell with the delimiter and
12109 and the position of this delimiter.
12111 This function does a reasonably good job, but can locally be fooled by
12112 for example currency specifications. For example it will assume being in
12113 inline math after \"$22.34\". The LaTeX fragment formatter will only format
12114 fragments that are properly closed, but during editing, we have to live
12115 with the uncertainty caused by missing closing delimiters. This function
12116 looks only before point, not after."
12118 (let ((pos (point))
12119 (dodollar (member "$" (plist-get org-format-latex-options
:matchers
)))
12121 (re-search-backward (concat "^\\(" paragraph-start
"\\)") nil t
)
12123 dd-on str
(start 0) m re
)
12126 (setq str
(concat (buffer-substring lim
(point)) "\000 X$.")
12127 re
(nth 1 (assoc "$" org-latex-regexps
)))
12128 (while (string-match re str start
)
12130 ((= (match-end 0) (length str
))
12131 (throw 'exit
(cons "$" (+ lim
(match-beginning 0) 1))))
12132 ((= (match-end 0) (- (length str
) 5))
12134 (t (setq start
(match-end 0))))))
12135 (when (setq m
(re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t
))
12137 (and (match-beginning 1) (throw 'exit
(cons (match-string 1) m
)))
12138 (and (match-beginning 2) (throw 'exit nil
))
12140 (while (re-search-backward "\\$\\$" lim t
)
12141 (setq dd-on
(not dd-on
)))
12143 (if dd-on
(cons "$$" m
))))))
12146 (defun org-try-cdlatex-tab ()
12147 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
12148 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
12149 - inside a LaTeX fragment, or
12150 - after the first word in a line, where an abbreviation expansion could
12151 insert a LaTeX environment."
12152 (when org-cdlatex-mode
12155 (skip-chars-backward "a-zA-Z0-9*")
12156 (skip-chars-backward " \t")
12159 ((org-inside-LaTeX-fragment-p)
12163 (defun org-cdlatex-underscore-caret (&optional arg
)
12164 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
12165 Revert to the normal definition outside of these fragments."
12167 (if (org-inside-LaTeX-fragment-p)
12168 (call-interactively 'cdlatex-sub-superscript
)
12169 (let (org-cdlatex-mode)
12170 (call-interactively (key-binding (vector last-input-event
))))))
12172 (defun org-cdlatex-math-modify (&optional arg
)
12173 "Execute `cdlatex-math-modify' in LaTeX fragments.
12174 Revert to the normal definition outside of these fragments."
12176 (if (org-inside-LaTeX-fragment-p)
12177 (call-interactively 'cdlatex-math-modify
)
12178 (let (org-cdlatex-mode)
12179 (call-interactively (key-binding (vector last-input-event
))))))
12181 (defvar org-latex-fragment-image-overlays nil
12182 "List of overlays carrying the images of latex fragments.")
12183 (make-variable-buffer-local 'org-latex-fragment-image-overlays
)
12185 (defun org-remove-latex-fragment-image-overlays ()
12186 "Remove all overlays with LaTeX fragment images in current buffer."
12187 (mapc 'org-delete-overlay org-latex-fragment-image-overlays
)
12188 (setq org-latex-fragment-image-overlays nil
))
12190 (defun org-preview-latex-fragment (&optional subtree
)
12191 "Preview the LaTeX fragment at point, or all locally or globally.
12192 If the cursor is in a LaTeX fragment, create the image and overlay
12193 it over the source code. If there is no fragment at point, display
12194 all fragments in the current text, from one headline to the next. With
12195 prefix SUBTREE, display all fragments in the current subtree. With a
12196 double prefix `C-u C-u', or when the cursor is before the first headline,
12197 display all fragments in the buffer.
12198 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
12200 (org-remove-latex-fragment-image-overlays)
12203 (let (beg end at msg
)
12205 ((or (equal subtree
'(16))
12206 (not (save-excursion
12207 (re-search-backward (concat "^" outline-regexp
) nil t
))))
12208 (setq beg
(point-min) end
(point-max)
12209 msg
"Creating images for buffer...%s"))
12210 ((equal subtree
'(4))
12211 (org-back-to-heading)
12212 (setq beg
(point) end
(org-end-of-subtree t
)
12213 msg
"Creating images for subtree...%s"))
12215 (if (setq at
(org-inside-LaTeX-fragment-p))
12216 (goto-char (max (point-min) (- (cdr at
) 2)))
12217 (org-back-to-heading))
12218 (setq beg
(point) end
(progn (outline-next-heading) (point))
12219 msg
(if at
"Creating image...%s"
12220 "Creating images for entry...%s"))))
12222 (narrow-to-region beg end
)
12225 (concat "ltxpng/" (file-name-sans-extension
12226 (file-name-nondirectory
12227 buffer-file-name
)))
12228 default-directory
'overlays msg at
'forbuffer
)
12229 (message msg
"done. Use `C-c C-c' to remove images.")))))
12231 (defvar org-latex-regexps
12232 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t
)
12233 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
12234 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
12235 ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil
)
12236 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil
)
12237 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t
)
12238 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t
))
12239 "Regular expressions for matching embedded LaTeX.")
12241 (defun org-format-latex (prefix &optional dir overlays msg at forbuffer
)
12242 "Replace LaTeX fragments with links to an image, and produce images."
12243 (if (and overlays
(fboundp 'clear-image-cache
)) (clear-image-cache))
12244 (let* ((prefixnodir (file-name-nondirectory prefix
))
12245 (absprefix (expand-file-name prefix dir
))
12246 (todir (file-name-directory absprefix
))
12247 (opt org-format-latex-options
)
12248 (matchers (plist-get opt
:matchers
))
12249 (re-list org-latex-regexps
)
12250 (cnt 0) txt link beg end re e checkdir
12251 m n block linkfile movefile ov
)
12252 ;; Check if there are old images files with this prefix, and remove them
12253 (when (file-directory-p todir
)
12257 (concat (regexp-quote prefixnodir
) "_[0-9]+\\.png$"))))
12258 ;; Check the different regular expressions
12259 (while (setq e
(pop re-list
))
12260 (setq m
(car e
) re
(nth 1 e
) n
(nth 2 e
)
12261 block
(if (nth 3 e
) "\n\n" ""))
12262 (when (member m matchers
)
12263 (goto-char (point-min))
12264 (while (re-search-forward re nil t
)
12265 (when (or (not at
) (equal (cdr at
) (match-beginning n
)))
12266 (setq txt
(match-string n
)
12267 beg
(match-beginning n
) end
(match-end n
)
12269 linkfile
(format "%s_%04d.png" prefix cnt
)
12270 movefile
(format "%s_%04d.png" absprefix cnt
)
12271 link
(concat block
"[[file:" linkfile
"]]" block
))
12272 (if msg
(message msg cnt
))
12274 (unless checkdir
; make sure the directory exists
12276 (or (file-directory-p todir
) (make-directory todir
)))
12277 (org-create-formula-image
12278 txt movefile opt forbuffer
)
12281 (setq ov
(org-make-overlay beg end
))
12282 (if (featurep 'xemacs
)
12284 (org-overlay-put ov
'invisible t
)
12287 (make-glyph (vector 'png
:file movefile
))))
12290 (list 'image
:type
'png
:file movefile
:ascent
'center
)))
12291 (push ov org-latex-fragment-image-overlays
)
12293 (delete-region beg end
)
12294 (insert link
))))))))
12296 ;; This function borrows from Ganesh Swami's latex2png.el
12297 (defun org-create-formula-image (string tofile options buffer
)
12298 (let* ((tmpdir (if (featurep 'xemacs
)
12300 temporary-file-directory
))
12301 (texfilebase (make-temp-name
12302 (expand-file-name "orgtex" tmpdir
)))
12303 (texfile (concat texfilebase
".tex"))
12304 (dvifile (concat texfilebase
".dvi"))
12305 (pngfile (concat texfilebase
".png"))
12306 (fnh (if (featurep 'xemacs
)
12307 (font-height (get-face-font 'default
))
12308 (face-attribute 'default
:height nil
)))
12309 (scale (or (plist-get options
(if buffer
:scale
:html-scale
)) 1.0))
12310 (dpi (number-to-string (* scale
(floor (* 0.9 (if buffer fnh
140.
))))))
12311 (fg (or (plist-get options
(if buffer
:foreground
:html-foreground
))
12313 (bg (or (plist-get options
(if buffer
:background
:html-background
))
12315 (if (eq fg
'default
) (setq fg
(org-dvipng-color :foreground
)))
12316 (if (eq bg
'default
) (setq bg
(org-dvipng-color :background
)))
12317 (with-temp-file texfile
12318 (insert org-format-latex-header
12319 "\n\\begin{document}\n" string
"\n\\end{document}\n"))
12320 (let ((dir default-directory
))
12321 (condition-case nil
12324 (call-process "latex" nil nil nil texfile
))
12327 (if (not (file-exists-p dvifile
))
12328 (progn (message "Failed to create dvi file from %s" texfile
) nil
)
12329 (call-process "dvipng" nil nil nil
12330 "-E" "-fg" fg
"-bg" bg
12332 ;;"-x" scale "-y" scale
12336 (if (not (file-exists-p pngfile
))
12337 (progn (message "Failed to create png file from %s" texfile
) nil
)
12338 ;; Use the requested file name and clean up
12339 (copy-file pngfile tofile
'replace
)
12340 (loop for e in
'(".dvi" ".tex" ".aux" ".log" ".png") do
12341 (delete-file (concat texfilebase e
)))
12344 (defun org-dvipng-color (attr)
12345 "Return an rgb color specification for dvipng."
12346 (apply 'format
"rgb %s %s %s"
12347 (mapcar 'org-normalize-color
12348 (color-values (face-attribute 'default attr nil
)))))
12350 (defun org-normalize-color (value)
12351 "Return string to be used as color value for an RGB component."
12352 (format "%g" (/ value
65535.0)))
12357 ;; Make `C-c C-x' a prefix key
12358 (org-defkey org-mode-map
"\C-c\C-x" (make-sparse-keymap))
12360 ;; TAB key with modifiers
12361 (org-defkey org-mode-map
"\C-i" 'org-cycle
)
12362 (org-defkey org-mode-map
[(tab)] 'org-cycle
)
12363 (org-defkey org-mode-map
[(control tab
)] 'org-force-cycle-archived
)
12364 (org-defkey org-mode-map
[(meta tab
)] 'org-complete
)
12365 (org-defkey org-mode-map
"\M-\t" 'org-complete
)
12366 (org-defkey org-mode-map
"\M-\C-i" 'org-complete
)
12367 ;; The following line is necessary under Suse GNU/Linux
12368 (unless (featurep 'xemacs
)
12369 (org-defkey org-mode-map
[S-iso-lefttab
] 'org-shifttab
))
12370 (org-defkey org-mode-map
[(shift tab
)] 'org-shifttab
)
12371 (define-key org-mode-map
[backtab] 'org-shifttab)
12373 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
12374 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
12375 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
12377 ;; Cursor keys with modifiers
12378 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
12379 (org-defkey org-mode-map [(meta right)] 'org-metaright)
12380 (org-defkey org-mode-map [(meta up)] 'org-metaup)
12381 (org-defkey org-mode-map [(meta down)] 'org-metadown)
12383 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
12384 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
12385 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
12386 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
12388 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
12389 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
12390 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
12391 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
12393 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
12394 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
12396 ;;; Extra keys for tty access.
12397 ;; We only set them when really needed because otherwise the
12398 ;; menus don't show the simple keys
12400 (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
12401 (not window-system))
12402 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
12403 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
12404 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
12405 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
12406 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
12407 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
12408 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
12409 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
12410 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
12411 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
12412 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
12413 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
12414 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
12415 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
12416 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
12417 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
12418 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
12419 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
12420 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
12421 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
12422 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
12423 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
12425 ;; All the other keys
12427 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
12428 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
12429 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
12430 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
12431 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
12432 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
12433 (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
12434 (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
12435 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
12436 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
12437 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
12438 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
12439 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
12440 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
12441 (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
12442 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
12443 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
12444 (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
12445 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
12446 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
12447 (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
12448 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
12449 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
12450 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
12451 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
12452 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
12453 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
12454 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
12455 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
12456 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
12457 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
12458 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
12459 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
12460 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
12461 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
12462 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
12463 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
12464 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
12465 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
12466 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
12467 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
12468 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
12469 (org-defkey org-mode-map "\C-c^" 'org-sort)
12470 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
12471 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
12472 (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
12473 (org-defkey org-mode-map "\C-m" 'org-return)
12474 (org-defkey org-mode-map "\C-j" 'org-return-indent)
12475 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
12476 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
12477 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
12478 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
12479 (org-defkey org-mode-map "\C-c'" 'org-edit-special)
12480 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
12481 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
12482 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
12483 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
12484 (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
12485 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
12486 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
12487 (org-defkey org-mode-map "\C-c\C-e" 'org-export)
12488 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
12489 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
12491 (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
12492 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
12493 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
12494 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
12496 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
12497 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
12498 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
12499 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
12500 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
12501 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
12502 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
12503 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
12504 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
12505 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
12506 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
12507 (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
12509 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
12511 (when (featurep 'xemacs)
12512 (org-defkey org-mode-map 'button3 'popup-mode-menu))
12514 (defvar org-table-auto-blank-field) ; defined in org-table.el
12515 (defun org-self-insert-command (N)
12516 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
12517 If the cursor is in a table looking at whitespace, the whitespace is
12518 overwritten, and the table is not marked as requiring realignment."
12520 (if (and (org-table-p)
12522 ;; check if we blank the field, and if that triggers align
12523 (and (featurep 'org-table) org-table-auto-blank-field
12524 (member last-command
12525 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
12526 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
12527 ;; got extra space, this field does not determine column width
12528 (let (org-table-may-need-update) (org-table-blank-field))
12529 ;; no extra space, this field may determine column width
12530 (org-table-blank-field)))
12533 (looking-at "[^|\n]* |"))
12534 (let (org-table-may-need-update)
12535 (goto-char (1- (match-end 0)))
12536 (delete-backward-char 1)
12537 (goto-char (match-beginning 0))
12538 (self-insert-command N))
12539 (setq org-table-may-need-update t)
12540 (self-insert-command N)
12541 (org-fix-tags-on-the-fly)))
12543 (defun org-fix-tags-on-the-fly ()
12544 (when (and (equal (char-after (point-at-bol)) ?*)
12545 (org-on-heading-p))
12546 (org-align-tags-here org-tags-column)))
12548 (defun org-delete-backward-char (N)
12549 "Like `delete-backward-char', insert whitespace at field end in tables.
12550 When deleting backwards, in tables this function will insert whitespace in
12551 front of the next \"|\" separator, to keep the table aligned. The table will
12552 still be marked for re-alignment if the field did fill the entire column,
12553 because, in this case the deletion might narrow the column."
12555 (if (and (org-table-p)
12557 (string-match "|" (buffer-substring (point-at-bol) (point)))
12558 (looking-at ".*?|"))
12559 (let ((pos (point))
12560 (noalign (looking-at "[^|\n\r]* |"))
12561 (c org-table-may-need-update))
12562 (backward-delete-char N)
12563 (skip-chars-forward "^|")
12565 (goto-char (1- pos))
12566 ;; noalign: if there were two spaces at the end, this field
12567 ;; does not determine the width of the column.
12568 (if noalign (setq org-table-may-need-update c)))
12569 (backward-delete-char N)
12570 (org-fix-tags-on-the-fly)))
12572 (defun org-delete-char (N)
12573 "Like `delete-char', but insert whitespace at field end in tables.
12574 When deleting characters, in tables this function will insert whitespace in
12575 front of the next \"|\" separator, to keep the table aligned. The table will
12576 still be marked for re-alignment if the field did fill the entire column,
12577 because, in this case the deletion might narrow the column."
12579 (if (and (org-table-p)
12581 (not (= (char-after) ?|))
12583 (if (looking-at ".*?|")
12584 (let ((pos (point))
12585 (noalign (looking-at "[^|\n\r]* |"))
12586 (c org-table-may-need-update))
12587 (replace-match (concat
12588 (substring (match-string 0) 1 -1)
12591 ;; noalign: if there were two spaces at the end, this field
12592 ;; does not determine the width of the column.
12593 (if noalign (setq org-table-may-need-update c)))
12596 (org-fix-tags-on-the-fly)))
12598 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
12599 (put 'org-self-insert-command 'delete-selection t)
12600 (put 'orgtbl-self-insert-command 'delete-selection t)
12601 (put 'org-delete-char 'delete-selection 'supersede)
12602 (put 'org-delete-backward-char 'delete-selection 'supersede)
12604 ;; Make `flyspell-mode' delay after some commands
12605 (put 'org-self-insert-command 'flyspell-delayed t)
12606 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
12607 (put 'org-delete-char 'flyspell-delayed t)
12608 (put 'org-delete-backward-char 'flyspell-delayed t)
12610 ;; Make pabbrev-mode expand after org-mode commands
12611 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
12612 (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
12614 ;; How to do this: Measure non-white length of current string
12615 ;; If equal to column width, we should realign.
12617 (defun org-remap (map &rest commands)
12618 "In MAP, remap the functions given in COMMANDS.
12619 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
12622 (setq old (pop commands) new (pop commands))
12623 (if (fboundp 'command-remapping)
12624 (org-defkey map (vector 'remap old) new)
12625 (substitute-key-definition old new map global-map)))))
12627 (when (eq org-enable-table-editor 'optimized)
12628 ;; If the user wants maximum table support, we need to hijack
12629 ;; some standard editing functions
12630 (org-remap org-mode-map
12631 'self-insert-command 'org-self-insert-command
12632 'delete-char 'org-delete-char
12633 'delete-backward-char 'org-delete-backward-char)
12634 (org-defkey org-mode-map "|" 'org-force-self-insert))
12636 (defun org-shiftcursor-error ()
12637 "Throw an error because Shift-Cursor command was applied in wrong context."
12638 (error "This command is active in special context like tables, headlines or timestamps"))
12640 (defun org-shifttab (&optional arg)
12641 "Global visibility cycling or move to previous table field.
12642 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
12644 See the individual commands for more information."
12647 ((org-at-table-p) (call-interactively 'org-table-previous-field))
12649 (message "Content view to level: %d" arg)
12650 (org-content (prefix-numeric-value arg))
12651 (setq org-cycle-global-status 'overview))
12652 (t (call-interactively 'org-global-cycle))))
12654 (defun org-shiftmetaleft ()
12655 "Promote subtree or delete table column.
12656 Calls `org-promote-subtree', `org-outdent-item',
12657 or `org-table-delete-column', depending on context.
12658 See the individual commands for more information."
12661 ((org-at-table-p) (call-interactively 'org-table-delete-column))
12662 ((org-on-heading-p) (call-interactively 'org-promote-subtree))
12663 ((org-at-item-p) (call-interactively 'org-outdent-item))
12664 (t (org-shiftcursor-error))))
12666 (defun org-shiftmetaright ()
12667 "Demote subtree or insert table column.
12668 Calls `org-demote-subtree', `org-indent-item',
12669 or `org-table-insert-column', depending on context.
12670 See the individual commands for more information."
12673 ((org-at-table-p) (call-interactively 'org-table-insert-column))
12674 ((org-on-heading-p) (call-interactively 'org-demote-subtree))
12675 ((org-at-item-p) (call-interactively 'org-indent-item))
12676 (t (org-shiftcursor-error))))
12678 (defun org-shiftmetaup (&optional arg)
12679 "Move subtree up or kill table row.
12680 Calls `org-move-subtree-up' or `org-table-kill-row' or
12681 `org-move-item-up' depending on context. See the individual commands
12682 for more information."
12685 ((org-at-table-p) (call-interactively 'org-table-kill-row))
12686 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
12687 ((org-at-item-p) (call-interactively 'org-move-item-up))
12688 (t (org-shiftcursor-error))))
12689 (defun org-shiftmetadown (&optional arg)
12690 "Move subtree down or insert table row.
12691 Calls `org-move-subtree-down' or `org-table-insert-row' or
12692 `org-move-item-down', depending on context. See the individual
12693 commands for more information."
12696 ((org-at-table-p) (call-interactively 'org-table-insert-row))
12697 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
12698 ((org-at-item-p) (call-interactively 'org-move-item-down))
12699 (t (org-shiftcursor-error))))
12701 (defun org-metaleft (&optional arg)
12702 "Promote heading or move table column to left.
12703 Calls `org-do-promote' or `org-table-move-column', depending on context.
12704 With no specific context, calls the Emacs default `backward-word'.
12705 See the individual commands for more information."
12708 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
12709 ((or (org-on-heading-p) (org-region-active-p))
12710 (call-interactively 'org-do-promote))
12711 ((org-at-item-p) (call-interactively 'org-outdent-item))
12712 (t (call-interactively 'backward-word))))
12714 (defun org-metaright (&optional arg)
12715 "Demote subtree or move table column to right.
12716 Calls `org-do-demote' or `org-table-move-column', depending on context.
12717 With no specific context, calls the Emacs default `forward-word'.
12718 See the individual commands for more information."
12721 ((org-at-table-p) (call-interactively 'org-table-move-column))
12722 ((or (org-on-heading-p) (org-region-active-p))
12723 (call-interactively 'org-do-demote))
12724 ((org-at-item-p) (call-interactively 'org-indent-item))
12725 (t (call-interactively 'forward-word))))
12727 (defun org-metaup (&optional arg)
12728 "Move subtree up or move table row up.
12729 Calls `org-move-subtree-up' or `org-table-move-row' or
12730 `org-move-item-up', depending on context. See the individual commands
12731 for more information."
12734 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
12735 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
12736 ((org-at-item-p) (call-interactively 'org-move-item-up))
12737 (t (transpose-lines 1) (beginning-of-line -1))))
12739 (defun org-metadown (&optional arg)
12740 "Move subtree down or move table row down.
12741 Calls `org-move-subtree-down' or `org-table-move-row' or
12742 `org-move-item-down', depending on context. See the individual
12743 commands for more information."
12746 ((org-at-table-p) (call-interactively 'org-table-move-row))
12747 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
12748 ((org-at-item-p) (call-interactively 'org-move-item-down))
12749 (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
12751 (defun org-shiftup (&optional arg)
12752 "Increase item in timestamp or increase priority of current headline.
12753 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
12754 depending on context. See the individual commands for more information."
12757 ((org-at-timestamp-p t)
12758 (call-interactively (if org-edit-timestamp-down-means-later
12759 'org-timestamp-down 'org-timestamp-up)))
12760 ((org-on-heading-p) (call-interactively 'org-priority-up))
12761 ((org-at-item-p) (call-interactively 'org-previous-item))
12762 ((org-clocktable-try-shift 'up arg))
12763 (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
12765 (defun org-shiftdown (&optional arg)
12766 "Decrease item in timestamp or decrease priority of current headline.
12767 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
12768 depending on context. See the individual commands for more information."
12771 ((org-at-timestamp-p t)
12772 (call-interactively (if org-edit-timestamp-down-means-later
12773 'org-timestamp-up 'org-timestamp-down)))
12774 ((org-on-heading-p) (call-interactively 'org-priority-down))
12775 ((org-clocktable-try-shift 'down arg))
12776 (t (call-interactively 'org-next-item))))
12778 (defun org-shiftright (&optional arg)
12779 "Next TODO keyword or timestamp one day later, depending on context."
12782 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
12783 ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
12784 ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
12785 ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
12786 ((org-clocktable-try-shift 'right arg))
12787 (t (org-shiftcursor-error))))
12789 (defun org-shiftleft (&optional arg)
12790 "Previous TODO keyword or timestamp one day earlier, depending on context."
12793 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
12794 ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
12795 ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
12796 ((org-at-property-p)
12797 (call-interactively 'org-property-previous-allowed-value))
12798 ((org-clocktable-try-shift 'left arg))
12799 (t (org-shiftcursor-error))))
12801 (defun org-shiftcontrolright ()
12802 "Switch to next TODO set."
12805 ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
12806 (t (org-shiftcursor-error))))
12808 (defun org-shiftcontrolleft ()
12809 "Switch to previous TODO set."
12812 ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
12813 (t (org-shiftcursor-error))))
12815 (defun org-ctrl-c-ret ()
12816 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
12819 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
12820 (t (call-interactively 'org-insert-heading))))
12822 (defun org-copy-special ()
12823 "Copy region in table or copy current subtree.
12824 Calls `org-table-copy' or `org-copy-subtree', depending on context.
12825 See the individual commands for more information."
12827 (call-interactively
12828 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
12830 (defun org-cut-special ()
12831 "Cut region in table or cut current subtree.
12832 Calls `org-table-copy' or `org-cut-subtree', depending on context.
12833 See the individual commands for more information."
12835 (call-interactively
12836 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
12838 (defun org-paste-special (arg)
12839 "Paste rectangular region into table, or past subtree relative to level.
12840 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
12841 See the individual commands for more information."
12843 (if (org-at-table-p)
12844 (org-table-paste-rectangle)
12845 (org-paste-subtree arg)))
12847 (defun org-edit-special ()
12848 "Call a special editor for the stuff at point.
12849 When at a table, call the formula editor with `org-table-edit-formulas'.
12850 When at the first line of an src example, call `org-edit-src-code'.
12851 When in an #+include line, visit the include file. Otherwise call
12852 `ffap' to visit the file at point."
12856 (call-interactively 'org-table-edit-formulas))
12858 (beginning-of-line 1)
12859 (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
12860 (find-file (org-trim (match-string 1))))
12861 ((org-edit-src-code))
12862 (t (call-interactively 'ffap))))
12864 (defun org-ctrl-c-ctrl-c (&optional arg)
12865 "Set tags in headline, or update according to changed information at point.
12867 This command does many different things, depending on context:
12869 - If the cursor is in a headline, prompt for tags and insert them
12870 into the current line, aligned to `org-tags-column'. When called
12871 with prefix arg, realign all tags in the current buffer.
12873 - If the cursor is in one of the special #+KEYWORD lines, this
12874 triggers scanning the buffer for these lines and updating the
12877 - If the cursor is inside a table, realign the table. This command
12878 works even if the automatic table editor has been turned off.
12880 - If the cursor is on a #+TBLFM line, re-apply the formulas to
12883 - If the cursor is a the beginning of a dynamic block, update it.
12885 - If the cursor is inside a table created by the table.el package,
12886 activate that table.
12888 - If the current buffer is a remember buffer, close note and file it.
12889 with a prefix argument, file it without further interaction to the default
12892 - If the cursor is on a <<<target>>>, update radio targets and corresponding
12893 links in this buffer.
12895 - If the cursor is on a numbered item in a plain list, renumber the
12898 - If the cursor is on a checkbox, toggle it."
12900 (let ((org-enable-table-editor t))
12902 ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
12903 org-occur-highlights
12904 org-latex-fragment-image-overlays)
12905 (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
12906 (org-remove-occur-highlights)
12907 (org-remove-latex-fragment-image-overlays)
12908 (message "Temporary highlights/overlays removed from current buffer"))
12909 ((and (local-variable-p 'org-finish-function (current-buffer))
12910 (fboundp org-finish-function))
12911 (funcall org-finish-function))
12912 ((org-at-property-p)
12913 (call-interactively 'org-property-action))
12914 ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
12915 ((org-on-heading-p) (call-interactively 'org-set-tags))
12916 ((org-at-table.el-p)
12918 (beginning-of-line 1)
12919 (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
12920 (call-interactively 'table-recognize-table))
12922 (org-table-maybe-eval-formula)
12924 (call-interactively 'org-table-recalculate)
12925 (org-table-maybe-recalculate-line))
12926 (call-interactively 'org-table-align))
12927 ((org-at-item-checkbox-p)
12928 (call-interactively 'org-toggle-checkbox))
12930 (call-interactively 'org-maybe-renumber-ordered-list))
12931 ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
12933 (beginning-of-line 1)
12934 (org-update-dblock))
12935 ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
12937 ((equal (match-string 1) "TBLFM")
12938 ;; Recalculate the table before this line
12940 (beginning-of-line 1)
12941 (skip-chars-backward " \r\n\t")
12942 (if (org-at-table-p)
12943 (org-call-with-arg 'org-table-recalculate t))))
12945 ; (org-set-regexps-and-options)
12946 ; (org-restart-font-lock)
12947 (let ((org-inhibit-startup t)) (org-mode-restart))
12948 (message "Local setup has been refreshed"))))
12949 (t (error "C-c C-c can do nothing useful at this location.")))))
12951 (defun org-mode-restart ()
12952 "Restart Org-mode, to scan again for special lines.
12953 Also updates the keyword regular expressions."
12956 (message "Org-mode restarted"))
12958 (defun org-kill-note-or-show-branches ()
12959 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
12961 (if (not org-finish-function)
12962 (call-interactively 'show-branches)
12963 (let ((org-note-abort t))
12964 (funcall org-finish-function))))
12966 (defun org-return (&optional indent)
12967 "Goto next table row or insert a newline.
12968 Calls `org-table-next-row' or `newline', depending on context.
12969 See the individual commands for more information."
12972 ((bobp) (if indent (newline-and-indent) (newline)))
12973 ((and (org-at-heading-p)
12975 (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
12980 (org-table-justify-field-maybe)
12981 (call-interactively 'org-table-next-row))
12982 (t (if indent (newline-and-indent) (newline)))))
12984 (defun org-return-indent ()
12985 "Goto next table row or insert a newline and indent.
12986 Calls `org-table-next-row' or `newline-and-indent', depending on
12987 context. See the individual commands for more information."
12991 (defun org-ctrl-c-star ()
12992 "Compute table, or change heading status of lines.
12993 Calls `org-table-recalculate' or `org-toggle-region-headings',
12994 depending on context. This will also turn a plain list item or a normal
12995 line into a subheading."
12999 (call-interactively 'org-table-recalculate))
13000 ((org-region-active-p)
13001 ;; Convert all lines in region to list items
13002 (call-interactively 'org-toggle-region-headings))
13003 ((org-on-heading-p)
13004 (org-toggle-region-headings (point-at-bol)
13005 (min (1+ (point-at-eol)) (point-max))))
13007 ;; Convert to heading
13008 (let ((level (save-match-data
13010 (condition-case nil
13012 (org-back-to-heading t)
13013 (funcall outline-level))
13016 (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
13017 (t (org-toggle-region-headings (point-at-bol)
13018 (min (1+ (point-at-eol)) (point-max))))))
13020 (defun org-ctrl-c-minus ()
13021 "Insert separator line in table or modify bullet status of line.
13022 Also turns a plain line or a region of lines into list items.
13023 Calls `org-table-insert-hline', `org-toggle-region-items', or
13024 `org-cycle-list-bullet', depending on context."
13028 (call-interactively 'org-table-insert-hline))
13029 ((org-on-heading-p)
13032 (beginning-of-line 1)
13033 (if (looking-at "\\*+ ")
13034 (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
13035 ((org-region-active-p)
13036 ;; Convert all lines in region to list items
13037 (call-interactively 'org-toggle-region-items))
13039 (call-interactively 'org-cycle-list-bullet))
13040 (t (org-toggle-region-items (point-at-bol)
13041 (min (1+ (point-at-eol)) (point-max))))))
13043 (defun org-toggle-region-items (beg end)
13044 "Convert all lines in region to list items.
13045 If the first line is already an item, convert all list items in the region
13051 (setq l2 (org-current-line))
13053 (beginning-of-line 1)
13054 (setq l (1- (org-current-line)))
13055 (if (org-at-item-p)
13056 ;; We already have items, de-itemize
13057 (while (< (setq l (1+ l)) l2)
13058 (when (org-at-item-p)
13059 (goto-char (match-beginning 2))
13060 (delete-region (match-beginning 2) (match-end 2))
13061 (and (looking-at "[ \t]+") (replace-match "")))
13062 (beginning-of-line 2))
13063 (while (< (setq l (1+ l)) l2)
13064 (unless (org-at-item-p)
13065 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
13066 (replace-match "\\1- \\2")))
13067 (beginning-of-line 2))))))
13069 (defun org-toggle-region-headings (beg end)
13070 "Convert all lines in region to list items.
13071 If the first line is already an item, convert all list items in the region
13077 (setq l2 (org-current-line))
13079 (beginning-of-line 1)
13080 (setq l (1- (org-current-line)))
13081 (if (org-on-heading-p)
13082 ;; We already have headlines, de-star them
13083 (while (< (setq l (1+ l)) l2)
13084 (when (org-on-heading-p t)
13085 (and (looking-at outline-regexp) (replace-match "")))
13086 (beginning-of-line 2))
13087 (let* ((stars (save-excursion
13088 (re-search-backward org-complex-heading-regexp nil t)
13089 (or (match-string 1) "*")))
13090 (add-stars (if org-odd-levels-only "**" "*"))
13091 (rpl (concat stars add-stars " \\2")))
13092 (while (< (setq l (1+ l)) l2)
13093 (unless (org-on-heading-p)
13094 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
13095 (replace-match rpl)))
13096 (beginning-of-line 2)))))))
13098 (defun org-meta-return (&optional arg)
13099 "Insert a new heading or wrap a region in a table.
13100 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
13101 See the individual commands for more information."
13105 (call-interactively 'org-table-wrap-region))
13106 (t (call-interactively 'org-insert-heading))))
13110 ;; Define the Org-mode menus
13111 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
13113 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
13114 ["Next Field" org-cycle (org-at-table-p)]
13115 ["Previous Field" org-shifttab (org-at-table-p)]
13116 ["Next Row" org-return (org-at-table-p)]
13118 ["Blank Field" org-table-blank-field (org-at-table-p)]
13119 ["Edit Field" org-table-edit-field (org-at-table-p)]
13120 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
13123 ["Move Column Left" org-metaleft (org-at-table-p)]
13124 ["Move Column Right" org-metaright (org-at-table-p)]
13125 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
13126 ["Insert Column" org-shiftmetaright (org-at-table-p)])
13128 ["Move Row Up" org-metaup (org-at-table-p)]
13129 ["Move Row Down" org-metadown (org-at-table-p)]
13130 ["Delete Row" org-shiftmetaup (org-at-table-p)]
13131 ["Insert Row" org-shiftmetadown (org-at-table-p)]
13132 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
13134 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
13136 ["Copy Rectangle" org-copy-special (org-at-table-p)]
13137 ["Cut Rectangle" org-cut-special (org-at-table-p)]
13138 ["Paste Rectangle" org-paste-special (org-at-table-p)]
13139 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
13142 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
13143 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
13144 ["Edit Formulas" org-edit-special (org-at-table-p)]
13146 ["Recalculate line" org-table-recalculate (org-at-table-p)]
13147 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
13148 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
13150 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
13152 ["Sum Column/Rectangle" org-table-sum
13153 (or (org-at-table-p) (org-region-active-p))]
13154 ["Which Column?" org-table-current-column (org-at-table-p)])
13156 org-table-toggle-formula-debugger
13157 :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
13158 ["Show Col/Row Numbers"
13159 org-table-toggle-coordinate-overlays
13161 :selected (org-bound-and-true-p org-table-overlay-coordinates)]
13163 ["Create" org-table-create (and (not (org-at-table-p))
13164 org-enable-table-editor)]
13165 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
13166 ["Import from File" org-table-import (not (org-at-table-p))]
13167 ["Export to File" org-table-export (org-at-table-p)]
13169 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
13171 (easy-menu-define org-org-menu org-mode-map "Org menu"
13174 ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
13175 ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
13176 ["Sparse Tree..." org-sparse-tree t]
13177 ["Reveal Context" org-reveal t]
13178 ["Show All" show-all t]
13180 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
13182 ["New Heading" org-insert-heading t]
13183 ("Navigate Headings"
13184 ["Up" outline-up-heading t]
13185 ["Next" outline-next-visible-heading t]
13186 ["Previous" outline-previous-visible-heading t]
13187 ["Next Same Level" outline-forward-same-level t]
13188 ["Previous Same Level" outline-backward-same-level t]
13190 ["Jump" org-goto t])
13192 ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
13193 ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
13195 ["Copy Subtree" org-copy-special (not (org-at-table-p))]
13196 ["Cut Subtree" org-cut-special (not (org-at-table-p))]
13197 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
13199 ["Promote Heading" org-metaleft (not (org-at-table-p))]
13200 ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
13201 ["Demote Heading" org-metaright (not (org-at-table-p))]
13202 ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
13204 ["Sort Region/Children" org-sort (not (org-at-table-p))]
13206 ["Convert to odd levels" org-convert-to-odd-levels t]
13207 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
13209 ["Emphasis..." org-emphasize t]
13210 ["Edit Source Example" org-edit-special t])
13212 ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
13213 ; ["Check and Tag Children" (org-toggle-archive-tag (4))
13214 ; :active t :keys "C-u C-c C-x C-a"]
13215 ["Sparse trees open ARCHIVE trees"
13216 (setq org-sparse-tree-open-archived-trees
13217 (not org-sparse-tree-open-archived-trees))
13218 :style toggle :selected org-sparse-tree-open-archived-trees]
13219 ["Cycling opens ARCHIVE trees"
13220 (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
13221 :style toggle :selected org-cycle-open-archived-trees]
13222 ["Agenda includes ARCHIVE trees"
13223 (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
13224 :style toggle :selected (not org-agenda-skip-archived-trees)]
13226 ["Move Subtree to Archive" org-advertized-archive-subtree t]
13227 ; ["Check and Move Children" (org-archive-subtree '(4))
13228 ; :active t :keys "C-u C-c C-x C-s"]
13232 ["TODO/DONE/-" org-todo t]
13234 ["Next keyword" org-shiftright (org-on-heading-p)]
13235 ["Previous keyword" org-shiftleft (org-on-heading-p)]
13236 ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
13237 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
13238 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
13239 ["Show TODO Tree" org-show-todo-tree t]
13240 ["Global TODO list" org-todo-list t]
13242 ["Set Priority" org-priority t]
13243 ["Priority Up" org-shiftup t]
13244 ["Priority Down" org-shiftdown t])
13245 ("TAGS and Properties"
13246 ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
13247 ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
13249 ["Set property" 'org-set-property t]
13250 ["Column view of properties" org-columns t]
13251 ["Insert Column View DBlock" org-insert-columns-dblock t])
13252 ("Dates and Scheduling"
13253 ["Timestamp" org-time-stamp t]
13254 ["Timestamp (inactive)" org-time-stamp-inactive t]
13256 ["1 Day Later" org-shiftright t]
13257 ["1 Day Earlier" org-shiftleft t]
13258 ["1 ... Later" org-shiftup t]
13259 ["1 ... Earlier" org-shiftdown t])
13260 ["Compute Time Range" org-evaluate-time-range t]
13261 ["Schedule Item" org-schedule t]
13262 ["Deadline" org-deadline t]
13264 ["Custom time format" org-toggle-time-stamp-overlays
13265 :style radio :selected org-display-custom-times]
13267 ["Goto Calendar" org-goto-calendar t]
13268 ["Date from Calendar" org-date-from-calendar t])
13270 ["Clock in" org-clock-in t]
13271 ["Clock out" org-clock-out t]
13272 ["Clock cancel" org-clock-cancel t]
13273 ["Goto running clock" org-clock-goto t]
13274 ["Display times" org-clock-display t]
13275 ["Create clock table" org-clock-report t]
13277 ["Record DONE time"
13278 (progn (setq org-log-done (not org-log-done))
13279 (message "Switching to %s will %s record a timestamp"
13280 (car org-done-keywords)
13281 (if org-log-done "automatically" "not")))
13282 :style toggle :selected org-log-done])
13284 ["Agenda Command..." org-agenda t]
13285 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
13286 ("File List for Agenda")
13287 ("Special views current file"
13288 ["TODO Tree" org-show-todo-tree t]
13289 ["Check Deadlines" org-check-deadlines t]
13290 ["Timeline" org-timeline t]
13291 ["Tags Tree" org-tags-sparse-tree t])
13294 ["Store Link (Global)" org-store-link t]
13295 ["Insert Link" org-insert-link t]
13296 ["Follow Link" org-open-at-point t]
13298 ["Next link" org-next-link t]
13299 ["Previous link" org-previous-link t]
13301 ["Descriptive Links"
13302 (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
13304 :selected (member '(org-link) buffer-invisibility-spec)]
13307 (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
13309 :selected (not (member '(org-link) buffer-invisibility-spec))])
13311 ["Export/Publish..." org-export t]
13313 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
13314 :selected org-cdlatex-mode]
13315 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
13316 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
13317 ["Modify math symbol" org-cdlatex-math-modify
13318 (org-inside-LaTeX-fragment-p)]
13319 ["Export LaTeX fragments as images"
13320 (if (featurep 'org-exp)
13321 (setq org-export-with-LaTeX-fragments
13322 (not org-export-with-LaTeX-fragments))
13323 (require 'org-exp))
13324 :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
13325 org-export-with-LaTeX-fragments)])
13328 ["Show Version" org-version t]
13329 ["Info Documentation" org-info t])
13331 ["Browse Org Group" org-customize t]
13333 ["Expand This Menu" org-create-customize-menu
13334 (fboundp 'customize-menu-create)])
13336 ["Refresh setup" org-mode-restart t]
13339 (defun org-info (&optional node)
13340 "Read documentation for Org-mode in the info system.
13341 With optional NODE, go directly to that node."
13343 (info (format "(org)%s" (or node ""))))
13345 (defun org-install-agenda-files-menu ()
13346 (let ((bl (buffer-list)))
13349 (set-buffer (pop bl))
13350 (if (org-mode-p) (setq bl nil)))
13353 '("Org") "File List for Agenda"
13356 ["Edit File List" (org-edit-agenda-file-list) t]
13357 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
13358 ["Remove Current File from List" org-remove-file t]
13359 ["Cycle through agenda files" org-cycle-agenda-files t]
13360 ["Occur in all agenda files" org-occur-in-agenda-files t]
13362 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
13367 (defun org-require-autoloaded-modules ()
13370 '(org-agenda org-archive org-clock org-colview
13371 org-exp org-id org-export-latex org-publish
13372 org-remember org-table)))
13375 (defun org-customize ()
13376 "Call the customize function with org as argument."
13378 (org-load-modules-maybe)
13379 (org-require-autoloaded-modules)
13380 (customize-browse 'org))
13382 (defun org-create-customize-menu ()
13383 "Create a full customization menu for Org-mode, insert it into the menu."
13385 (org-load-modules-maybe)
13386 (org-require-autoloaded-modules)
13387 (if (fboundp 'customize-menu-create)
13390 '("Org") "Customize"
13391 `(["Browse Org group" org-customize t]
13393 ,(customize-menu-create 'org)
13394 ["Set" Custom-set t]
13395 ["Save" Custom-save t]
13396 ["Reset to Current" Custom-reset-current t]
13397 ["Reset to Saved" Custom-reset-saved t]
13398 ["Reset to Standard Settings" Custom-reset-standard t]))
13399 (message "\"Org\"-menu now contains full customization menu"))
13400 (error "Cannot expand menu (outdated version of cus-edit.el)")))
13402 ;;;; Miscellaneous stuff
13404 ;;; Generally useful functions
13406 (defun org-display-warning (message) ;; Copied from Emacs-Muse
13407 "Display the given MESSAGE as a warning."
13408 (if (fboundp 'display-warning)
13409 (display-warning 'org message
13410 (if (featurep 'xemacs)
13413 (let ((buf (get-buffer-create "*Org warnings*")))
13414 (with-current-buffer buf
13415 (goto-char (point-max))
13416 (insert "Warning (Org): " message)
13419 (display-buffer buf)
13422 (defun org-goto-marker-or-bmk (marker &optional bookmark)
13423 "Go to MARKER, widen if necesary. When marker is not live, try BOOKMARK."
13424 (if (and marker (marker-buffer marker)
13425 (buffer-live-p (marker-buffer marker)))
13427 (switch-to-buffer (marker-buffer marker))
13428 (if (or (> marker (point-max)) (< marker (point-min)))
13430 (goto-char marker))
13432 (bookmark-jump bookmark)
13433 (error "Cannot find location"))))
13435 (defun org-quote-csv-field (s)
13436 "Quote field for inclusion in CSV material."
13437 (if (string-match "[\",]" s)
13438 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
13441 (defun org-plist-delete (plist property)
13442 "Delete PROPERTY from PLIST.
13443 This is in contrast to merely setting it to 0."
13446 (if (not (eq property (car plist)))
13447 (setq p (plist-put p (car plist) (nth 1 plist))))
13448 (setq plist (cddr plist)))
13451 (defun org-force-self-insert (N)
13452 "Needed to enforce self-insert under remapping."
13454 (self-insert-command N))
13456 (defun org-string-width (s)
13457 "Compute width of string, ignoring invisible characters.
13458 This ignores character with invisibility property `org-link', and also
13459 characters with property `org-cwidth', because these will become invisible
13460 upon the next fontification round."
13462 (when (or (eq t buffer-invisibility-spec)
13463 (assq 'org-link buffer-invisibility-spec))
13464 (while (setq b (text-property-any 0 (length s)
13465 'invisible 'org-link s))
13466 (setq s (concat (substring s 0 b)
13467 (substring s (or (next-single-property-change
13468 b 'invisible s) (length s)))))))
13469 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
13470 (setq s (concat (substring s 0 b)
13471 (substring s (or (next-single-property-change
13472 b 'org-cwidth s) (length s))))))
13473 (setq l (string-width s) b -1)
13474 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
13475 (setq l (- l (get-text-property b 'org-dwidth-n s))))
13478 (defun org-base-buffer (buffer)
13479 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
13482 (or (buffer-base-buffer buffer)
13485 (defun org-trim (s)
13486 "Remove whitespace at beginning and end of string."
13487 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
13488 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
13491 (defun org-wrap (string &optional width lines)
13492 "Wrap string to either a number of lines, or a width in characters.
13493 If WIDTH is non-nil, the string is wrapped to that width, however many lines
13494 that costs. If there is a word longer than WIDTH, the text is actually
13495 wrapped to the length of that word.
13496 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
13497 many lines, whatever width that takes.
13498 The return value is a list of lines, without newlines at the end."
13499 (let* ((words (org-split-string string "[ \t\n]+"))
13500 (maxword (apply 'max (mapcar 'org-string-width words)))
13503 (org-do-wrap words (max maxword width)))
13506 (setq ll (org-do-wrap words maxword))
13507 (if (<= (length ll) lines)
13510 (while (> (length ll) lines)
13512 (setq ll (org-do-wrap words w)))
13514 (t (error "Cannot wrap this")))))
13516 (defun org-do-wrap (words width)
13517 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
13520 (setq line (pop words))
13521 (while (and words (< (+ (length line) (length (car words))) width))
13522 (setq line (concat line " " (pop words))))
13523 (setq lines (push line lines)))
13526 (defun org-split-string (string &optional separators)
13527 "Splits STRING into substrings at SEPARATORS.
13528 No empty strings are returned if there are matches at the beginning
13529 and end of string."
13530 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
13534 (while (and (string-match rexp string
13536 (= start (match-beginning 0))
13537 (< start (length string)))
13539 (< (match-beginning 0) (length string)))
13541 (or (eq (match-beginning 0) 0)
13542 (and (eq (match-beginning 0) (match-end 0))
13543 (eq (match-beginning 0) start))
13545 (cons (substring string start (match-beginning 0))
13547 (setq start (match-end 0)))
13548 (or (eq start (length string))
13550 (cons (substring string start)
13554 (defun org-context ()
13555 "Return a list of contexts of the current cursor position.
13556 If several contexts apply, all are returned.
13557 Each context entry is a list with a symbol naming the context, and
13558 two positions indicating start and end of the context. Possible
13561 :headline anywhere in a headline
13562 :headline-stars on the leading stars in a headline
13563 :todo-keyword on a TODO keyword (including DONE) in a headline
13564 :tags on the TAGS in a headline
13565 :priority on the priority cookie in a headline
13566 :item on the first line of a plain list item
13567 :item-bullet on the bullet/number of a plain list item
13568 :checkbox on the checkbox in a plain list item
13569 :table in an org-mode table
13570 :table-special on a special filed in a table
13571 :table-table in a table.el table
13572 :link on a hyperlink
13573 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
13574 :target on a <<target>>
13575 :radio-target on a <<<radio-target>>>
13576 :latex-fragment on a LaTeX fragment
13577 :latex-preview on a LaTeX fragment with overlayed preview image
13579 This function expects the position to be visible because it uses font-lock
13580 faces as a help to recognize the following contexts: :table-special, :link,
13582 (let* ((f (get-text-property (point) 'face))
13583 (faces (if (listp f) f (list f)))
13584 (p (point)) clist o)
13585 ;; First the large context
13587 ((org-on-heading-p t)
13588 (push (list :headline (point-at-bol) (point-at-eol)) clist)
13590 (beginning-of-line 1)
13591 (looking-at org-todo-line-tags-regexp))
13592 (push (org-point-in-group p 1 :headline-stars) clist)
13593 (push (org-point-in-group p 2 :todo-keyword) clist)
13594 (push (org-point-in-group p 4 :tags) clist))
13596 (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
13597 (if (looking-at "\\[#[A-Z0-9]\\]")
13598 (push (org-point-in-group p 0 :priority) clist)))
13601 (push (org-point-in-group p 2 :item-bullet) clist)
13602 (push (list :item (point-at-bol)
13603 (save-excursion (org-end-of-item) (point)))
13605 (and (org-at-item-checkbox-p)
13606 (push (org-point-in-group p 0 :checkbox) clist)))
13609 (push (list :table (org-table-begin) (org-table-end)) clist)
13610 (if (memq 'org-formula faces)
13611 (push (list :table-special
13612 (previous-single-property-change p 'face)
13613 (next-single-property-change p 'face)) clist)))
13614 ((org-at-table-p 'any)
13615 (push (list :table-table) clist)))
13618 ;; Now the small context
13620 ((org-at-timestamp-p)
13621 (push (org-point-in-group p 0 :timestamp) clist))
13622 ((memq 'org-link faces)
13624 (previous-single-property-change p 'face)
13625 (next-single-property-change p 'face)) clist))
13626 ((memq 'org-special-keyword faces)
13627 (push (list :keyword
13628 (previous-single-property-change p 'face)
13629 (next-single-property-change p 'face)) clist))
13631 (push (org-point-in-group p 0 :target) clist)
13632 (goto-char (1- (match-beginning 0)))
13633 (if (looking-at org-radio-target-regexp)
13634 (push (org-point-in-group p 0 :radio-target) clist))
13636 ((setq o (car (delq nil
13639 (if (memq x org-latex-fragment-image-overlays) x))
13640 (org-overlays-at (point))))))
13641 (push (list :latex-fragment
13642 (org-overlay-start o) (org-overlay-end o)) clist)
13643 (push (list :latex-preview
13644 (org-overlay-start o) (org-overlay-end o)) clist))
13645 ((org-inside-LaTeX-fragment-p)
13646 ;; FIXME: positions wrong.
13647 (push (list :latex-fragment (point) (point)) clist)))
13649 (setq clist (nreverse (delq nil clist)))
13652 ;; FIXME: Compare with at-regexp-p Do we need both?
13653 (defun org-in-regexp (re &optional nlines visually)
13654 "Check if point is inside a match of regexp.
13655 Normally only the current line is checked, but you can include NLINES extra
13656 lines both before and after point into the search.
13657 If VISUALLY is set, require that the cursor is not after the match but
13658 really on, so that the block visually is on the match."
13660 (let ((pos (point))
13661 (eol (point-at-eol (+ 1 (or nlines 0))))
13662 (inc (if visually 1 0)))
13664 (beginning-of-line (- 1 (or nlines 0)))
13665 (while (re-search-forward re eol t)
13666 (if (and (<= (match-beginning 0) pos)
13667 (>= (+ inc (match-end 0)) pos))
13668 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
13670 (defun org-at-regexp-p (regexp)
13671 "Is point inside a match of REGEXP in the current line?"
13674 (let ((pos (point)) (end (point-at-eol)))
13675 (beginning-of-line 1)
13676 (while (re-search-forward regexp end t)
13677 (if (and (<= (match-beginning 0) pos)
13678 (>= (match-end 0) pos))
13682 (defun org-occur-in-agenda-files (regexp &optional nlines)
13683 "Call `multi-occur' with buffers for all agenda files."
13684 (interactive "sOrg-files matching: \np")
13685 (let* ((files (org-agenda-files))
13686 (tnames (mapcar 'file-truename files))
13687 (extra org-agenda-text-search-extra-files)
13689 (when (eq (car extra) 'agenda-archives)
13690 (setq extra (cdr extra))
13691 (setq files (org-add-archive-files files)))
13692 (while (setq f (pop extra))
13693 (unless (member (file-truename f) tnames)
13694 (add-to-list 'files f 'append)
13695 (add-to-list 'tnames (file-truename f) 'append)))
13697 (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
13700 (if (boundp 'occur-mode-find-occurrence-hook)
13702 (add-hook 'occur-mode-find-occurrence-hook
13707 (defadvice occur-mode-goto-occurrence
13708 (after org-occur-reveal activate)
13709 (and (org-mode-p) (org-reveal)))
13710 (defadvice occur-mode-goto-occurrence-other-window
13711 (after org-occur-reveal activate)
13712 (and (org-mode-p) (org-reveal)))
13713 (defadvice occur-mode-display-occurrence
13714 (after org-occur-reveal activate)
13716 (let ((pos (occur-mode-find-occurrence)))
13717 (with-current-buffer (marker-buffer pos)
13722 (defun org-uniquify (list)
13723 "Remove duplicate elements from LIST."
13725 (mapc (lambda (x) (add-to-list 'res x 'append)) list)
13728 (defun org-delete-all (elts list)
13729 "Remove all elements in ELTS from LIST."
13731 (setq list (delete (pop elts) list)))
13734 (defun org-back-over-empty-lines ()
13735 "Move backwards over witespace, to the beginning of the first empty line.
13736 Returns the number of empty lines passed."
13737 (let ((pos (point)))
13738 (skip-chars-backward " \t\n\r")
13739 (beginning-of-line 2)
13740 (goto-char (min (point) pos))
13741 (count-lines (point) pos)))
13743 (defun org-skip-whitespace ()
13744 (skip-chars-forward " \t\n\r"))
13746 (defun org-point-in-group (point group &optional context)
13747 "Check if POINT is in match-group GROUP.
13748 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
13749 match. If the match group does ot exist or point is not inside it,
13751 (and (match-beginning group)
13752 (>= point (match-beginning group))
13753 (<= point (match-end group))
13755 (list context (match-beginning group) (match-end group))
13758 (defun org-switch-to-buffer-other-window (&rest args)
13759 "Switch to buffer in a second window on the current frame.
13760 In particular, do not allow pop-up frames."
13761 (let (pop-up-frames special-display-buffer-names special-display-regexps
13762 special-display-function)
13763 (apply 'switch-to-buffer-other-window args)))
13765 (defun org-combine-plists (&rest plists)
13766 "Create a single property list from all plists in PLISTS.
13767 The process starts by copying the first list, and then setting properties
13768 from the other lists. Settings in the last list are the most significant
13769 ones and overrule settings in the other lists."
13770 (let ((rtn (copy-sequence (pop plists)))
13773 (setq ls (pop plists))
13775 (setq p (pop ls) v (pop ls))
13776 (setq rtn (plist-put rtn p v))))
13779 (defun org-move-line-down (arg)
13780 "Move the current line down. With prefix argument, move it past ARG lines."
13782 (let ((col (current-column))
13784 (beginning-of-line 1) (setq beg (point))
13785 (beginning-of-line 2) (setq end (point))
13786 (beginning-of-line (+ 1 arg))
13787 (setq pos (move-marker (make-marker) (point)))
13788 (insert (delete-and-extract-region beg end))
13790 (org-move-to-column col)))
13792 (defun org-move-line-up (arg)
13793 "Move the current line up. With prefix argument, move it past ARG lines."
13795 (let ((col (current-column))
13797 (beginning-of-line 1) (setq beg (point))
13798 (beginning-of-line 2) (setq end (point))
13799 (beginning-of-line (- arg))
13800 (setq pos (move-marker (make-marker) (point)))
13801 (insert (delete-and-extract-region beg end))
13803 (org-move-to-column col)))
13805 (defun org-replace-escapes (string table)
13806 "Replace %-escapes in STRING with values in TABLE.
13807 TABLE is an association list with keys like \"%a\" and string values.
13808 The sequences in STRING may contain normal field width and padding information,
13809 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
13810 so values can contain further %-escapes if they are define later in TABLE."
13811 (let ((case-fold-search nil)
13813 (while (setq e (pop table))
13814 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
13815 (while (string-match re string)
13816 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
13818 (setq string (replace-match rpl t t string))))
13822 (defun org-sublist (list start end)
13823 "Return a section of LIST, from START to END.
13824 Counting starts at 1."
13825 (let (rtn (c start))
13826 (setq list (nthcdr (1- start) list))
13827 (while (and list (<= c end))
13828 (push (pop list) rtn)
13832 (defun org-find-base-buffer-visiting (file)
13833 "Like `find-buffer-visiting' but alway return the base buffer and
13834 not an indirect buffer."
13835 (let ((buf (find-buffer-visiting file)))
13837 (or (buffer-base-buffer buf) buf)
13840 (defun org-image-file-name-regexp ()
13841 "Return regexp matching the file names of images."
13842 (if (fboundp 'image-file-name-regexp)
13843 (image-file-name-regexp)
13844 (let ((image-file-name-extensions
13845 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
13846 "xbm" "xpm" "pbm" "pgm" "ppm")))
13848 (regexp-opt (nconc (mapcar 'upcase
13849 image-file-name-extensions)
13850 image-file-name-extensions)
13854 (defun org-file-image-p (file)
13855 "Return non-nil if FILE is an image."
13857 (string-match (org-image-file-name-regexp) file)))
13859 (defun org-get-cursor-date ()
13860 "Return the date at cursor in as a time.
13861 This works in the calendar and in the agenda, anywhere else it just
13862 returns the current time."
13863 (let (date day defd)
13865 ((eq major-mode 'calendar-mode)
13866 (setq date (calendar-cursor-to-date)
13867 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
13868 ((eq major-mode 'org-agenda-mode)
13869 (setq day (get-text-property (point) 'day))
13871 (setq date (calendar-gregorian-from-absolute day)
13872 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
13874 (or defd (current-time))))
13876 (defvar org-agenda-action-marker (make-marker)
13877 "Marker pointing to the entry for the next agenda action.")
13879 (defun org-mark-entry-for-agenda-action ()
13880 "Mark the current entry as target of an agenda action.
13881 Agenda actions are actions executed from the agenda with the key `k',
13882 which make use of the date at the cursor."
13884 (move-marker org-agenda-action-marker
13885 (save-excursion (org-back-to-heading t) (point))
13888 "Entry marked for action; press `k' at desired date in agenda or calendar"))
13890 ;;; Paragraph filling stuff.
13891 ;; We want this to be just right, so use the full arsenal.
13893 (defun org-indent-line-function ()
13894 "Indent line like previous, but further if previous was headline or item."
13896 (let* ((pos (point))
13897 (itemp (org-at-item-p))
13898 column bpos bcol tpos tcol bullet btype bullet-type)
13899 ;; Find the previous relevant line
13900 (beginning-of-line 1)
13902 ((looking-at "#") (setq column 0))
13903 ((looking-at "\\*+ ") (setq column 0))
13905 (beginning-of-line 0)
13906 (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
13907 (beginning-of-line 0))
13909 ((looking-at "\\*+[ \t]+")
13910 (if (not org-adapt-indentation)
13912 (goto-char (match-end 0))
13913 (setq column (current-column))))
13915 (org-beginning-of-item)
13916 ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
13917 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
13918 (setq bpos (match-beginning 1) tpos (match-end 0)
13919 bcol (progn (goto-char bpos) (current-column))
13920 tcol (progn (goto-char tpos) (current-column))
13921 bullet (match-string 1)
13922 bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
13923 (if (> tcol (+ bcol org-description-max-indent))
13924 (setq tcol (+ bcol 5)))
13928 (beginning-of-line 1)
13929 (if (looking-at "\\S-")
13931 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
13932 (setq bullet (match-string 1)
13933 btype (if (string-match "[0-9]" bullet) "n" bullet))
13934 (setq column (if (equal btype bullet-type) bcol tcol)))
13935 (setq column (org-get-indentation)))))
13936 (t (setq column (org-get-indentation))))))
13938 (if (<= (current-column) (current-indentation))
13939 (org-indent-line-to column)
13940 (save-excursion (org-indent-line-to column)))
13941 (setq column (current-column))
13942 (beginning-of-line 1)
13944 "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
13945 (replace-match (concat "\\1" (format org-property-format
13946 (match-string 2) (match-string 3)))
13948 (org-move-to-column column)))
13950 (defun org-set-autofill-regexps ()
13952 ;; In the paragraph separator we include headlines, because filling
13953 ;; text in a line directly attached to a headline would otherwise
13954 ;; fill the headline as well.
13955 (org-set-local 'comment-start-skip "^#+[ \t]*")
13956 (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
13957 ;; The paragraph starter includes hand-formatted lists.
13958 (org-set-local 'paragraph-start
13959 "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
13960 ;; Inhibit auto-fill for headers, tables and fixed-width lines.
13961 ;; But only if the user has not turned off tables or fixed-width regions
13963 'auto-fill-inhibit-regexp
13964 (concat "\\*+ \\|#\\+"
13965 "\\|[ \t]*" org-keyword-time-regexp
13966 (if (or org-enable-table-editor org-enable-fixed-width-editor)
13969 (if org-enable-table-editor "|" "")
13970 (if org-enable-fixed-width-editor ":" "")
13972 ;; We use our own fill-paragraph function, to make sure that tables
13973 ;; and fixed-width regions are not wrapped. That function will pass
13974 ;; through to `fill-paragraph' when appropriate.
13975 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
13976 ; Adaptive filling: To get full control, first make sure that
13977 ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
13978 (org-set-local 'adaptive-fill-regexp "\000")
13979 (org-set-local 'adaptive-fill-function
13980 'org-adaptive-fill-function)
13982 'align-mode-rules-list
13983 '((org-in-buffer-settings
13984 (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
13985 (modes . '(org-mode))))))
13987 (defun org-fill-paragraph (&optional justify)
13988 "Re-align a table, pass through to fill-paragraph if no table."
13989 (let ((table-p (org-at-table-p))
13990 (table.el-p (org-at-table.el-p)))
13991 (cond ((and (equal (char-after (point-at-bol)) ?*)
13992 (save-excursion (goto-char (point-at-bol))
13993 (looking-at outline-regexp)))
13994 t) ; skip headlines
13995 (table.el-p t) ; skip table.el tables
13996 (table-p (org-table-align) t) ; align org-mode tables
13997 (t nil)))) ; call paragraph-fill
13999 ;; For reference, this is the default value of adaptive-fill-regexp
14000 ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
14002 (defun org-adaptive-fill-function ()
14003 "Return a fill prefix for org-mode files.
14004 In particular, this makes sure hanging paragraphs for hand-formatted lists
14006 (cond ((looking-at "#[ \t]+")
14008 ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
14010 (if (> (match-end 1) (+ (match-beginning 1)
14011 org-description-max-indent))
14012 (goto-char (+ (match-beginning 1) 5))
14013 (goto-char (match-end 0)))
14014 (make-string (current-column) ?\ )))
14015 ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
14017 (goto-char (match-end 0))
14018 (make-string (current-column) ?\ )))
14023 (defun org-toggle-fixed-width-section (arg)
14024 "Toggle the fixed-width export.
14025 If there is no active region, the QUOTE keyword at the current headline is
14026 inserted or removed. When present, it causes the text between this headline
14027 and the next to be exported as fixed-width text, and unmodified.
14028 If there is an active region, this command adds or removes a colon as the
14029 first character of this line. If the first character of a line is a colon,
14030 this line is also exported in fixed-width font."
14033 (regionp (org-region-active-p))
14034 (beg (if regionp (region-beginning) (point)))
14035 (end (if regionp (region-end)))
14036 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
14037 (case-fold-search nil)
14038 (re "[ \t]*\\(:\\)")
14043 (setq cc (current-column))
14044 (beginning-of-line 1)
14045 (setq off (looking-at re))
14046 (while (> nlines 0)
14047 (setq nlines (1- nlines))
14048 (beginning-of-line 1)
14051 (org-move-to-column cc t)
14054 ((and off (looking-at re))
14055 (replace-match "" t t nil 1))
14056 ((not off) (org-move-to-column cc t) (insert ":")))
14059 (org-back-to-heading)
14060 (if (looking-at (concat outline-regexp
14061 "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
14062 (replace-match "" t t nil 1)
14063 (if (looking-at outline-regexp)
14065 (goto-char (match-end 0))
14066 (insert org-quote-string " "))))))))
14068 ;;;; Functions extending outline functionality
14070 (defun org-beginning-of-line (&optional arg)
14071 "Go to the beginning of the current line. If that is invisible, continue
14072 to a visible line beginning. This makes the function of C-a more intuitive.
14073 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
14074 first attempt, and only move to after the tags when the cursor is already
14075 beyond the end of the headline."
14077 (let ((pos (point)) refpos)
14078 (beginning-of-line 1)
14082 (if (org-invisible-p)
14083 (while (and (not (bobp)) (org-invisible-p))
14085 (beginning-of-line 1))
14087 (when org-special-ctrl-a/e
14089 ((and (looking-at org-complex-heading-regexp)
14090 (= (char-after (match-end 1)) ?\ ))
14091 (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
14094 (if (eq org-special-ctrl-a/e t)
14095 (cond ((> pos refpos) refpos)
14096 ((= pos (point)) refpos)
14098 (cond ((> pos (point)) (point))
14099 ((not (eq last-command this-command)) (point))
14103 (if (eq org-special-ctrl-a/e t)
14104 (cond ((> pos (match-end 4)) (match-end 4))
14105 ((= pos (point)) (match-end 4))
14107 (cond ((> pos (point)) (point))
14108 ((not (eq last-command this-command)) (point))
14109 (t (match-end 4))))))))
14111 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
14113 (defun org-end-of-line (&optional arg)
14114 "Go to the end of the line.
14115 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
14116 first attempt, and only move to after the tags when the cursor is already
14117 beyond the end of the headline."
14119 (if (or (not org-special-ctrl-a/e)
14120 (not (org-on-heading-p)))
14122 (let ((pos (point)))
14123 (beginning-of-line 1)
14124 (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
14125 (if (eq org-special-ctrl-a/e t)
14126 (if (or (< pos (match-beginning 1))
14127 (= pos (match-end 0)))
14128 (goto-char (match-beginning 1))
14129 (goto-char (match-end 0)))
14130 (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
14131 (goto-char (match-end 0))
14132 (goto-char (match-beginning 1))))
14133 (end-of-line arg))))
14135 (and (featurep 'xemacs) (setq zmacs-region-stays t))))
14138 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
14139 (define-key org-mode-map "\C-e" 'org-end-of-line)
14141 (defun org-kill-line (&optional arg)
14142 "Kill line, to tags or end of line."
14145 ((or (not org-special-ctrl-k)
14147 (not (org-on-heading-p)))
14148 (call-interactively 'kill-line))
14149 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
14150 (kill-region (point) (match-beginning 1))
14151 (org-set-tags nil t))
14152 (t (kill-region (point) (point-at-eol)))))
14154 (define-key org-mode-map "\C-k" 'org-kill-line)
14156 (defun org-invisible-p ()
14157 "Check if point is at a character currently not visible."
14158 ;; Early versions of noutline don't have `outline-invisible-p'.
14159 (if (fboundp 'outline-invisible-p)
14160 (outline-invisible-p)
14161 (get-char-property (point) 'invisible)))
14163 (defun org-invisible-p2 ()
14164 "Check if point is at a character currently not visible."
14166 (if (and (eolp) (not (bobp))) (backward-char 1))
14167 ;; Early versions of noutline don't have `outline-invisible-p'.
14168 (if (fboundp 'outline-invisible-p)
14169 (outline-invisible-p)
14170 (get-char-property (point) 'invisible))))
14172 (defalias 'org-back-to-heading 'outline-back-to-heading)
14173 (defalias 'org-on-heading-p 'outline-on-heading-p)
14174 (defalias 'org-at-heading-p 'outline-on-heading-p)
14175 (defun org-at-heading-or-item-p ()
14176 (or (org-on-heading-p) (org-at-item-p)))
14178 (defun org-on-target-p ()
14179 (or (org-in-regexp org-radio-target-regexp)
14180 (org-in-regexp org-target-regexp)))
14182 (defun org-up-heading-all (arg)
14183 "Move to the heading line of which the present line is a subheading.
14184 This function considers both visible and invisible heading lines.
14185 With argument, move up ARG levels."
14186 (if (fboundp 'outline-up-heading-all)
14187 (outline-up-heading-all arg) ; emacs 21 version of outline.el
14188 (outline-up-heading arg t))) ; emacs 22 version of outline.el
14190 (defun org-up-heading-safe ()
14191 "Move to the heading line of which the present line is a subheading.
14192 This version will not throw an error. It will return the level of the
14193 headline found, or nil if no higher level is found."
14194 (let ((pos (point)) start-level level
14195 (re (concat "^" outline-regexp)))
14197 (outline-back-to-heading t)
14198 (setq start-level (funcall outline-level))
14199 (if (equal start-level 1) (throw 'exit nil))
14200 (while (re-search-backward re nil t)
14201 (setq level (funcall outline-level))
14202 (if (< level start-level) (throw 'exit level)))
14205 (defun org-first-sibling-p ()
14206 "Is this heading the first child of its parents?"
14208 (let ((re (concat "^" outline-regexp))
14210 (unless (org-at-heading-p t)
14211 (error "Not at a heading"))
14212 (setq level (funcall outline-level))
14214 (if (not (re-search-backward re nil t))
14216 (setq l (funcall outline-level))
14219 (defun org-goto-sibling (&optional previous)
14220 "Goto the next sibling, even if it is invisible.
14221 When PREVIOUS is set, go to the previous sibling instead. Returns t
14222 when a sibling was found. When none is found, return nil and don't
14224 (let ((fun (if previous 're-search-backward 're-search-forward))
14226 (re (concat "^" outline-regexp))
14228 (when (condition-case nil (org-back-to-heading t) (error nil))
14229 (setq level (funcall outline-level))
14231 (or previous (forward-char 1))
14232 (while (funcall fun re nil t)
14233 (setq l (funcall outline-level))
14234 (when (< l level) (goto-char pos) (throw 'exit nil))
14235 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
14239 (defun org-show-siblings ()
14240 "Show all siblings of the current headline."
14242 (while (org-goto-sibling) (org-flag-heading nil)))
14244 (while (org-goto-sibling 'previous)
14245 (org-flag-heading nil))))
14247 (defun org-show-hidden-entry ()
14248 "Show an entry where even the heading is hidden."
14252 (defun org-flag-heading (flag &optional entry)
14253 "Flag the current heading. FLAG non-nil means make invisible.
14254 When ENTRY is non-nil, show the entire entry."
14256 (org-back-to-heading t)
14257 ;; Check if we should show the entire entry
14262 (and (outline-next-heading)
14263 (org-flag-heading nil))))
14264 (outline-flag-region (max (point-min) (1- (point)))
14265 (save-excursion (outline-end-of-heading) (point))
14268 (defun org-end-of-subtree (&optional invisible-OK to-heading)
14269 ;; This is an exact copy of the original function, but it uses
14270 ;; `org-back-to-heading', to make it work also in invisible
14271 ;; trees. And is uses an invisible-OK argument.
14272 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
14273 (org-back-to-heading invisible-OK)
14275 (level (funcall outline-level)))
14276 (while (and (not (eobp))
14277 (or first (> (funcall outline-level) level)))
14279 (outline-next-heading))
14281 (if (memq (preceding-char) '(?\n ?\^M))
14283 ;; Go to end of line before heading
14285 (if (memq (preceding-char) '(?\n ?\^M))
14286 ;; leave blank line before heading
14287 (forward-char -1))))))
14290 (defun org-show-subtree ()
14291 "Show everything after this heading at deeper levels."
14292 (outline-flag-region
14295 (outline-end-of-subtree) (outline-next-heading) (point))
14298 (defun org-show-entry ()
14299 "Show the body directly following this heading.
14300 Show the heading too, if it is currently invisible."
14303 (condition-case nil
14305 (org-back-to-heading t)
14306 (outline-flag-region
14307 (max (point-min) (1- (point)))
14310 (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
14311 (or (match-beginning 1) (point-max)))
14315 (defun org-make-options-regexp (kwds)
14316 "Make a regular expression for keyword lines."
14320 (mapconcat 'regexp-quote kwds "\\|")
14324 ;; Make isearch reveal the necessary context
14325 (defun org-isearch-end ()
14326 "Reveal context after isearch exits."
14327 (when isearch-success ; only if search was successful
14328 (if (featurep 'xemacs)
14329 ;; Under XEmacs, the hook is run in the correct place,
14330 ;; we directly show the context.
14331 (org-show-context 'isearch)
14332 ;; In Emacs the hook runs *before* restoring the overlays.
14333 ;; So we have to use a one-time post-command-hook to do this.
14334 ;; (Emacs 22 has a special variable, see function `org-mode')
14335 (unless (and (boundp 'isearch-mode-end-hook-quit)
14336 isearch-mode-end-hook-quit)
14337 ;; Only when the isearch was not quitted.
14338 (org-add-hook 'post-command-hook 'org-isearch-post-command
14339 'append 'local)))))
14341 (defun org-isearch-post-command ()
14342 "Remove self from hook, and show context."
14343 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
14344 (org-show-context 'isearch))
14347 ;;;; Integration with and fixes for other packages
14351 (defvar org-imenu-markers nil
14352 "All markers currently used by Imenu.")
14353 (make-variable-buffer-local 'org-imenu-markers)
14355 (defun org-imenu-new-marker (&optional pos)
14356 "Return a new marker for use by Imenu, and remember the marker."
14357 (let ((m (make-marker)))
14358 (move-marker m (or pos (point)))
14359 (push m org-imenu-markers)
14362 (defun org-imenu-get-tree ()
14363 "Produce the index for Imenu."
14364 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
14365 (setq org-imenu-markers nil)
14366 (let* ((n org-imenu-depth)
14367 (re (concat "^" outline-regexp))
14368 (subs (make-vector (1+ n) nil))
14374 (goto-char (point-max))
14375 (while (re-search-backward re nil t)
14376 (setq level (org-reduced-level (funcall outline-level)))
14378 (looking-at org-complex-heading-regexp)
14379 (setq head (org-match-string-no-properties 4)
14380 m (org-imenu-new-marker))
14381 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
14382 (if (>= level last-level)
14383 (push (cons head m) (aref subs level))
14384 (push (cons head (aref subs (1+ level))) (aref subs level))
14385 (loop for i from (1+ level) to n do (aset subs i nil)))
14386 (setq last-level level)))))
14389 (eval-after-load "imenu"
14391 (add-hook 'imenu-after-jump-hook
14392 (lambda () (org-show-context 'org-goto)))))
14394 ;; Speedbar support
14396 (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
14397 "Overlay marking the agenda restriction line in speedbar.")
14398 (org-overlay-put org-speedbar-restriction-lock-overlay
14399 'face 'org-agenda-restriction-lock)
14400 (org-overlay-put org-speedbar-restriction-lock-overlay
14401 'help-echo "Agendas are currently limited to this item.")
14402 (org-detach-overlay org-speedbar-restriction-lock-overlay)
14404 (defun org-speedbar-set-agenda-restriction ()
14405 "Restrict future agenda commands to the location at point in speedbar.
14406 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
14408 (require 'org-agenda)
14409 (let (p m tp np dir txt w)
14411 ((setq p (text-property-any (point-at-bol) (point-at-eol)
14413 (setq m (get-text-property p 'org-imenu-marker))
14416 (set-buffer (marker-buffer m))
14418 (org-agenda-set-restriction-lock 'subtree))))
14419 ((setq p (text-property-any (point-at-bol) (point-at-eol)
14420 'speedbar-function 'speedbar-find-file))
14421 (setq tp (previous-single-property-change
14422 (1+ p) 'speedbar-function)
14423 np (next-single-property-change
14424 tp 'speedbar-function)
14425 dir (speedbar-line-directory)
14426 txt (buffer-substring-no-properties (or tp (point-min))
14427 (or np (point-max))))
14430 (set-buffer (find-file-noselect
14431 (let ((default-directory dir))
14432 (expand-file-name txt))))
14433 (unless (org-mode-p)
14434 (error "Cannot restrict to non-Org-mode file"))
14435 (org-agenda-set-restriction-lock 'file))))
14436 (t (error "Don't know how to restrict Org-mode's agenda")))
14437 (org-move-overlay org-speedbar-restriction-lock-overlay
14438 (point-at-bol) (point-at-eol))
14439 (setq current-prefix-arg nil)
14440 (org-agenda-maybe-redo)))
14442 (eval-after-load "speedbar"
14444 (speedbar-add-supported-extension ".org")
14445 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
14446 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
14447 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
14448 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
14449 (add-hook 'speedbar-visiting-tag-hook
14450 (lambda () (org-show-context 'org-goto)))))
14453 ;;; Fixes and Hacks for problems with other packages
14455 ;; Make flyspell not check words in links, to not mess up our keymap
14456 (defun org-mode-flyspell-verify ()
14457 "Don't let flyspell put overlays at active buttons."
14458 (not (get-text-property (point) 'keymap)))
14460 ;; Make `bookmark-jump' show the jump location if it was hidden.
14461 (eval-after-load "bookmark"
14462 '(if (boundp 'bookmark-after-jump-hook)
14463 ;; We can use the hook
14464 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
14465 ;; Hook not available, use advice
14466 (defadvice bookmark-jump (after org-make-visible activate)
14467 "Make the position visible."
14468 (org-bookmark-jump-unhide))))
14470 (defun org-bookmark-jump-unhide ()
14471 "Unhide the current position, to show the bookmark location."
14473 (or (org-invisible-p)
14474 (save-excursion (goto-char (max (point-min) (1- (point))))
14475 (org-invisible-p)))
14476 (org-show-context 'bookmark-jump)))
14478 ;; Make session.el ignore our circular variable
14479 (eval-after-load "session"
14480 '(add-to-list 'session-globals-exclude 'org-mark-ring))
14482 ;;;; Experimental code
14484 (defun org-closed-in-range ()
14485 "Sparse tree of items closed in a certain time range.
14486 Still experimental, may disappear in the future."
14488 ;; Get the time interval from the user.
14489 (let* ((time1 (time-to-seconds
14490 (org-read-date nil 'to-time nil "Starting date: ")))
14491 (time2 (time-to-seconds
14492 (org-read-date nil 'to-time nil "End date:")))
14493 ;; callback function
14494 (callback (lambda ()
14497 (apply 'encode-time
14498 (org-parse-time-string
14499 (match-string 1))))))
14500 ;; check if time in interval
14501 (and (>= time time1) (<= time time2))))))
14502 ;; make tree, check each match with the callback
14503 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
14510 (run-hooks 'org-load-hook)
14512 ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
14514 ;;; org.el ends here