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, or (at your option)
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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
31 ;; project planning with a fast and effective plain-text system.
33 ;; Org-mode develops organizational tasks around NOTES files that contain
34 ;; information about projects as plain text. Org-mode is implemented on
35 ;; top of outline-mode, which makes it possible to keep the content of
36 ;; large files well structured. Visibility cycling and structure editing
37 ;; help to work with the tree. Tables are easily created with a built-in
38 ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
39 ;; and scheduling. It dynamically compiles entries into an agenda that
40 ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
41 ;; Plain text URL-like links connect to websites, emails, Usenet
42 ;; messages, BBDB entries, and any files related to the projects. For
43 ;; printing and sharing of notes, an Org-mode file can be exported as a
44 ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
45 ;; iCalendar file. It can also serve as a publishing tool for a set of
48 ;; Installation and Activation
49 ;; ---------------------------
50 ;; See the corresponding sections in the manual at
52 ;; http://orgmode.org/org.html#Installation
56 ;; The documentation of Org-mode can be found in the TeXInfo file. The
57 ;; distribution also contains a PDF version of it. At the homepage of
58 ;; Org-mode, you can read the same text online as HTML. There is also an
59 ;; excellent reference card made by Philip Rooke. This card can be found
60 ;; in the etc/ directory of Emacs 22.
62 ;; A list of recent changes can be found at
63 ;; http://orgmode.org/Changes.html
67 (defvar org-inhibit-highlight-removal nil
) ; dynamically scoped param
68 (defvar org-table-formula-constants-local nil
69 "Local version of `org-table-formula-constants'.")
70 (make-variable-buffer-local 'org-table-formula-constants-local
)
72 ;;;; Require other packages
78 ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
79 ;; the file noutline.el being loaded.
80 (if (featurep 'xemacs
) (condition-case nil
(require 'noutline
)))
81 ;; We require noutline, which might be provided in outline.el
82 (require 'outline
) (require 'noutline
)
83 ;; Other stuff we need.
85 (unless (fboundp 'time-subtract
) (defalias 'time-subtract
'subtract-time
))
91 ;;;; Customization variables
95 (defconst org-version
"6.00pre-2"
96 "The version number of the file org.el.")
98 (defun org-version (&optional here
)
99 "Show the org-mode version in the echo area.
100 With prefix arg HERE, insert it at point."
102 (let ((version (format "Org-mode version %s" org-version
)))
107 ;;; Compatibility constants
109 ;;; The custom variables
112 "Outline-based notes management and organizer."
118 (defcustom org-load-hook nil
119 "Hook that is run after org.el has been loaded."
123 (defvar org-modules
) ; defined below
124 (defvar org-modules-loaded nil
125 "Have the modules been loaded already?")
127 (defun org-load-modules-maybe (&optional force
)
128 "Load all extensions listed in `org-default-extensions'."
129 (when (or force
(not org-modules-loaded
))
131 (condition-case nil
(require ext
)
132 (error (message "Problems while trying to load feature `%s'" ext
))))
134 (setq org-modules-loaded t
)))
136 (defun org-set-modules (var value
)
137 "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
139 (when (featurep 'org
)
140 (org-load-modules-maybe 'force
)))
142 (defcustom org-modules
'(org-bbdb org-bibtex org-gnus org-info org-infojs org-irc org-mhe org-rmail org-vm org-wl
)
143 "Modules that should always be loaded together with org.el.
144 If a description starts with <C>, the file is not part of emacs
145 and loading it will require that you have downloaded and properly installed
146 the org-mode distribution.
148 You can also use this system to load external packages (i.e. neither Org
149 core modules, not modules from the CONTRIB directory). Just add symbols
150 to the end of the list. If the package is called org-xyz.e, then you need
151 to add the symbol `xyz', and the package must have a call to
155 :set
'org-set-modules
158 (const :tag
" bbdb: Links to BBDB entries" org-bbdb
)
159 (const :tag
" bibtex: Links to BibTeX entries" org-bibtex
)
160 (const :tag
" gnus: Links to GNUS folders/messages" org-gnus
)
161 (const :tag
" info: Links to Info nodes" org-info
)
162 (const :tag
" infojs: Set up Sebastian Rose's JavaScript org-info.js" org-infojs
)
163 (const :tag
" irc: Links to IRC/ERC chat sessions" org-irc
)
164 (const :tag
" mac-message: Links to messages in Apple Mail" org-mac-message
)
165 (const :tag
" mhe: Links to MHE folders/messages" org-mhe
)
166 (const :tag
" rmail: Links to RMAIL folders/messages" org-rmail
)
167 (const :tag
" vm: Links to VM folders/messages" org-vm
)
168 (const :tag
" wl: Links to Wanderlust folders/messages" org-wl
)
169 (const :tag
" mouse: Additional mouse support" org-mouse
)
171 (const :tag
"C annotate-file: Annotate a file with org syntax" org-annotate-file
)
172 (const :tag
"C bookmark: Org links to bookmarks" org-bookmark
)
173 (const :tag
"C depend: TODO dependencies for Org-mode" org-depend
)
174 (const :tag
"C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol
)
175 (const :tag
"C expiry: Expiry mechanism for Org entries" org-expiry
)
176 (const :tag
"C id: Global id's for identifying entries" org-id
)
177 (const :tag
"C interactive-query: Interactive modification of tags query" org-interactive-query
)
178 (const :tag
"C iswitchb: Use iswitchb to select Org buffer" org-iswitchb
)
179 (const :tag
"C mairix: Hook mairix search into Org for different MUAs" org-mairix
)
180 (const :tag
"C man: Support for links to manpages in Org-mode" org-man
)
181 (const :tag
"C mew: Support for links to messages in Mew" org-mew
)
182 (const :tag
"C panel: Simple routines for us with bad memory" org-panel
)
183 (const :tag
"C registry: A registry for Org links" org-registry
)
184 (const :tag
"C org2rem: Convert org appointments into reminders" org2rem
)
185 (const :tag
"C screen: Visit screen sessions through Org-mode links" org-screen
)
186 (const :tag
"C toc: Table of contents for Org-mode buffer" org-toc
)
187 (repeat :tag
"External packages" :inline t
(symbol :tag
"Package"))))
190 (defgroup org-startup nil
191 "Options concerning startup of Org-mode."
195 (defcustom org-startup-folded t
196 "Non-nil means, entering Org-mode will switch to OVERVIEW.
197 This can also be configured on a per-file basis by adding one of
198 the following lines anywhere in the buffer:
205 (const :tag
"nofold: show all" nil
)
206 (const :tag
"fold: overview" t
)
207 (const :tag
"content: all headlines" content
)))
209 (defcustom org-startup-truncated t
210 "Non-nil means, entering Org-mode will set `truncate-lines'.
211 This is useful since some lines containing links can be very long and
212 uninteresting. Also tables look terrible when wrapped."
216 (defcustom org-startup-align-all-tables nil
217 "Non-nil means, align all tables when visiting a file.
218 This is useful when the column width in tables is forced with <N> cookies
219 in table fields. Such tables will look correct only after the first re-align.
220 This can also be configured on a per-file basis by adding one of
221 the following lines anywhere in the buffer:
227 (defcustom org-insert-mode-line-in-empty-file nil
228 "Non-nil means insert the first line setting Org-mode in empty files.
229 When the function `org-mode' is called interactively in an empty file, this
230 normally means that the file name does not automatically trigger Org-mode.
231 To ensure that the file will always be in Org-mode in the future, a
232 line enforcing Org-mode will be inserted into the buffer, if this option
237 (defcustom org-replace-disputed-keys nil
238 "Non-nil means use alternative key bindings for some keys.
239 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
240 These keys are also used by other packages like `CUA-mode' or `windmove.el'.
241 If you want to use Org-mode together with one of these other modes,
242 or more generally if you would like to move some Org-mode commands to
243 other keys, set this variable and configure the keys with the variable
246 This option is only relevant at load-time of Org-mode, and must be set
247 *before* org.el is loaded. Changing it requires a restart of Emacs to
252 (if (fboundp 'defvaralias
)
253 (defvaralias 'org-CUA-compatible
'org-replace-disputed-keys
))
255 (defcustom org-disputed-keys
256 '(([(shift up
)] .
[(meta p
)])
257 ([(shift down
)] .
[(meta n
)])
258 ([(shift left
)] .
[(meta -
)])
259 ([(shift right
)] .
[(meta +)])
260 ([(control shift right
)] .
[(meta shift
+)])
261 ([(control shift left
)] .
[(meta shift -
)]))
262 "Keys for which Org-mode and other modes compete.
263 This is an alist, cars are the default keys, second element specifies
264 the alternative to use when `org-replace-disputed-keys' is t.
266 Keys can be specified in any syntax supported by `define-key'.
267 The value of this option takes effect only at Org-mode's startup,
268 therefore you'll have to restart Emacs to apply it after changing."
273 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
274 Or return the original if not disputed."
275 (if org-replace-disputed-keys
276 (let* ((nkey (key-description key
))
277 (x (org-find-if (lambda (x)
278 (equal (key-description (car x
)) nkey
))
283 (defun org-find-if (predicate seq
)
286 (if (funcall predicate
(car seq
))
287 (throw 'exit
(car seq
))
290 (defun org-defkey (keymap key def
)
291 "Define a key, possibly translated, as returned by `org-key'."
292 (define-key keymap
(org-key key
) def
))
294 (defcustom org-ellipsis nil
295 "The ellipsis to use in the Org-mode outline.
296 When nil, just use the standard three dots. When a string, use that instead,
297 When a face, use the standart 3 dots, but with the specified face.
298 The change affects only Org-mode (which will then use its own display table).
299 Changing this requires executing `M-x org-mode' in a buffer to become
302 :type
'(choice (const :tag
"Default" nil
)
303 (face :tag
"Face" :value org-warning
)
304 (string :tag
"String" :value
"...#")))
306 (defvar org-display-table nil
307 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
309 (defgroup org-keywords nil
310 "Keywords in Org-mode."
314 (defcustom org-deadline-string
"DEADLINE:"
315 "String to mark deadline entries.
316 A deadline is this string, followed by a time stamp. Should be a word,
317 terminated by a colon. You can insert a schedule keyword and
318 a timestamp with \\[org-deadline].
319 Changes become only effective after restarting Emacs."
323 (defcustom org-scheduled-string
"SCHEDULED:"
324 "String to mark scheduled TODO entries.
325 A schedule is this string, followed by a time stamp. Should be a word,
326 terminated by a colon. You can insert a schedule keyword and
327 a timestamp with \\[org-schedule].
328 Changes become only effective after restarting Emacs."
332 (defcustom org-closed-string
"CLOSED:"
333 "String used as the prefix for timestamps logging closing a TODO entry."
337 (defcustom org-clock-string
"CLOCK:"
338 "String used as prefix for timestamps clocking work hours on an item."
342 (defcustom org-comment-string
"COMMENT"
343 "Entries starting with this keyword will never be exported.
344 An entry can be toggled between COMMENT and normal with
345 \\[org-toggle-comment].
346 Changes become only effective after restarting Emacs."
350 (defcustom org-quote-string
"QUOTE"
351 "Entries starting with this keyword will be exported in fixed-width font.
352 Quoting applies only to the text in the entry following the headline, and does
353 not extend beyond the next headline, even if that is lower level.
354 An entry can be toggled between QUOTE and normal with
355 \\[org-toggle-fixed-width-section]."
359 (defconst org-repeat-re
360 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
361 "Regular expression for specifying repeated events.
362 After a match, group 1 contains the repeat expression.")
364 (defgroup org-structure nil
365 "Options concerning the general structure of Org-mode files."
369 (defgroup org-reveal-location nil
370 "Options about how to make context of a location visible."
371 :tag
"Org Reveal Location"
372 :group
'org-structure
)
374 (defconst org-context-choice
376 (const :tag
"Always" t
)
377 (const :tag
"Never" nil
)
378 (repeat :greedy t
:tag
"Individual contexts"
380 (choice :tag
"Context"
387 (const bookmark-jump
)
391 "Contexts for the reveal options.")
393 (defcustom org-show-hierarchy-above
'((default . t
))
394 "Non-nil means, show full hierarchy when revealing a location.
395 Org-mode often shows locations in an org-mode file which might have
396 been invisible before. When this is set, the hierarchy of headings
397 above the exposed location is shown.
398 Turning this off for example for sparse trees makes them very compact.
399 Instead of t, this can also be an alist specifying this option for different
400 contexts. Valid contexts are
401 agenda when exposing an entry from the agenda
402 org-goto when using the command `org-goto' on key C-c C-j
403 occur-tree when using the command `org-occur' on key C-c /
404 tags-tree when constructing a sparse tree based on tags matches
405 link-search when exposing search matches associated with a link
406 mark-goto when exposing the jump goal of a mark
407 bookmark-jump when exposing a bookmark location
408 isearch when exiting from an incremental search
409 default default for all contexts not set explicitly"
410 :group
'org-reveal-location
411 :type org-context-choice
)
413 (defcustom org-show-following-heading
'((default . nil
))
414 "Non-nil means, show following heading when revealing a location.
415 Org-mode often shows locations in an org-mode file which might have
416 been invisible before. When this is set, the heading following the
418 Turning this off for example for sparse trees makes them very compact,
419 but makes it harder to edit the location of the match. In such a case,
420 use the command \\[org-reveal] to show more context.
421 Instead of t, this can also be an alist specifying this option for different
422 contexts. See `org-show-hierarchy-above' for valid contexts."
423 :group
'org-reveal-location
424 :type org-context-choice
)
426 (defcustom org-show-siblings
'((default . nil
) (isearch t
))
427 "Non-nil means, show all sibling heading when revealing a location.
428 Org-mode often shows locations in an org-mode file which might have
429 been invisible before. When this is set, the sibling of the current entry
430 heading are all made visible. If `org-show-hierarchy-above' is t,
431 the same happens on each level of the hierarchy above the current entry.
433 By default this is on for the isearch context, off for all other contexts.
434 Turning this off for example for sparse trees makes them very compact,
435 but makes it harder to edit the location of the match. In such a case,
436 use the command \\[org-reveal] to show more context.
437 Instead of t, this can also be an alist specifying this option for different
438 contexts. See `org-show-hierarchy-above' for valid contexts."
439 :group
'org-reveal-location
440 :type org-context-choice
)
442 (defcustom org-show-entry-below
'((default . nil
))
443 "Non-nil means, show the entry below a headline when revealing a location.
444 Org-mode often shows locations in an org-mode file which might have
445 been invisible before. When this is set, the text below the headline that is
446 exposed is also shown.
448 By default this is off for all contexts.
449 Instead of t, this can also be an alist specifying this option for different
450 contexts. See `org-show-hierarchy-above' for valid contexts."
451 :group
'org-reveal-location
452 :type org-context-choice
)
454 (defcustom org-indirect-buffer-display
'other-window
455 "How should indirect tree buffers be displayed?
456 This applies to indirect buffers created with the commands
457 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
459 current-window Display in the current window
460 other-window Just display in another window.
461 dedicated-frame Create one new frame, and re-use it each time.
462 new-frame Make a new frame each time. Note that in this case
463 previously-made indirect buffers are kept, and you need to
464 kill these buffers yourself."
465 :group
'org-structure
466 :group
'org-agenda-windows
468 (const :tag
"In current window" current-window
)
469 (const :tag
"In current frame, other window" other-window
)
470 (const :tag
"Each time a new frame" new-frame
)
471 (const :tag
"One dedicated frame" dedicated-frame
)))
473 (defgroup org-cycle nil
474 "Options concerning visibility cycling in Org-mode."
476 :group
'org-structure
)
478 (defcustom org-drawers
'("PROPERTIES" "CLOCK")
479 "Names of drawers. Drawers are not opened by cycling on the headline above.
480 Drawers only open with a TAB on the drawer line itself. A drawer looks like
485 The drawer \"PROPERTIES\" is special for capturing properties through
488 Drawers can be defined on the per-file basis with a line like:
490 #+DRAWERS: HIDDEN STATE PROPERTIES"
491 :group
'org-structure
492 :type
'(repeat (string :tag
"Drawer Name")))
494 (defcustom org-cycle-global-at-bob nil
495 "Cycle globally if cursor is at beginning of buffer and not at a headline.
496 This makes it possible to do global cycling without having to use S-TAB or
497 C-u TAB. For this special case to work, the first line of the buffer
498 must not be a headline - it may be empty ot some other text. When used in
499 this way, `org-cycle-hook' is disables temporarily, to make sure the
500 cursor stays at the beginning of the buffer.
501 When this option is nil, don't do anything special at the beginning
506 (defcustom org-cycle-emulate-tab t
507 "Where should `org-cycle' emulate TAB.
509 white Only in completely white lines
510 whitestart Only at the beginning of lines, before the first non-white char
511 t Everywhere except in headlines
512 exc-hl-bol Everywhere except at the start of a headline
513 If TAB is used in a place where it does not emulate TAB, the current subtree
514 visibility is cycled."
516 :type
'(choice (const :tag
"Never" nil
)
517 (const :tag
"Only in completely white lines" white
)
518 (const :tag
"Before first char in a line" whitestart
)
519 (const :tag
"Everywhere except in headlines" t
)
520 (const :tag
"Everywhere except at bol in headlines" exc-hl-bol
)
523 (defcustom org-cycle-separator-lines
2
524 "Number of empty lines needed to keep an empty line between collapsed trees.
525 If you leave an empty line between the end of a subtree and the following
526 headline, this empty line is hidden when the subtree is folded.
527 Org-mode will leave (exactly) one empty line visible if the number of
528 empty lines is equal or larger to the number given in this variable.
529 So the default 2 means, at least 2 empty lines after the end of a subtree
530 are needed to produce free space between a collapsed subtree and the
533 Special case: when 0, never leave empty lines in collapsed view."
537 (defcustom org-cycle-hook
'(org-cycle-hide-archived-subtrees
538 org-cycle-hide-drawers
539 org-cycle-show-empty-lines
540 org-optimize-window-after-visibility-change
)
541 "Hook that is run after `org-cycle' has changed the buffer visibility.
542 The function(s) in this hook must accept a single argument which indicates
543 the new state that was set by the most recent `org-cycle' command. The
544 argument is a symbol. After a global state change, it can have the values
545 `overview', `content', or `all'. After a local state change, it can have
546 the values `folded', `children', or `subtree'."
550 (defgroup org-edit-structure nil
551 "Options concerning structure editing in Org-mode."
552 :tag
"Org Edit Structure"
553 :group
'org-structure
)
555 (defcustom org-odd-levels-only nil
556 "Non-nil means, skip even levels and only use odd levels for the outline.
557 This has the effect that two stars are being added/taken away in
558 promotion/demotion commands. It also influences how levels are
559 handled by the exporters.
560 Changing it requires restart of `font-lock-mode' to become effective
561 for fontification also in regions already fontified.
562 You may also set this on a per-file basis by adding one of the following
567 :group
'org-edit-structure
568 :group
'org-font-lock
571 (defcustom org-adapt-indentation t
572 "Non-nil means, adapt indentation when promoting and demoting.
573 When this is set and the *entire* text in an entry is indented, the
574 indentation is increased by one space in a demotion command, and
575 decreased by one in a promotion command. If any line in the entry
576 body starts at column 0, indentation is not changed at all."
577 :group
'org-edit-structure
580 (defcustom org-special-ctrl-a
/e nil
581 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
582 When t, `C-a' will bring back the cursor to the beginning of the
583 headline text, i.e. after the stars and after a possible TODO keyword.
584 In an item, this will be the position after the bullet.
585 When the cursor is already at that position, another `C-a' will bring
586 it to the beginning of the line.
587 `C-e' will jump to the end of the headline, ignoring the presence of tags
588 in the headline. A second `C-e' will then jump to the true end of the
589 line, after any tags.
590 When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
591 and only a directly following, identical keypress will bring the cursor
592 to the special positions."
593 :group
'org-edit-structure
595 (const :tag
"off" nil
)
596 (const :tag
"after bullet first" t
)
597 (const :tag
"border first" reversed
)))
599 (if (fboundp 'defvaralias
)
600 (defvaralias 'org-special-ctrl-a
'org-special-ctrl-a
/e
))
602 (defcustom org-special-ctrl-k nil
603 "Non-nil means `C-k' will behave specially in headlines.
604 When nil, `C-k' will call the default `kill-line' command.
605 When t, the following will happen while the cursor is in the headline:
607 - When the cursor is at the beginning of a headline, kill the entire
608 line and possible the folded subtree below the line.
609 - When in the middle of the headline text, kill the headline up to the tags.
610 - When after the headline text, kill the tags."
611 :group
'org-edit-structure
614 (defcustom org-M-RET-may-split-line
'((default . t
))
615 "Non-nil means, M-RET will split the line at the cursor position.
616 When nil, it will go to the end of the line before making a
618 You may also set this option in a different way for different
619 contexts. Valid contexts are:
621 headline when creating a new headline
622 item when creating a new item
623 table in a table field
624 default the value to be used for all contexts not explicitly
626 :group
'org-structure
629 (const :tag
"Always" t
)
630 (const :tag
"Never" nil
)
631 (repeat :greedy t
:tag
"Individual contexts"
633 (choice :tag
"Context"
641 (defcustom org-blank-before-new-entry
'((heading . nil
)
642 (plain-list-item . nil
))
643 "Should `org-insert-heading' leave a blank line before new heading/item?
644 The value is an alist, with `heading' and `plain-list-item' as car,
645 and a boolean flag as cdr."
646 :group
'org-edit-structure
648 (cons (const heading
) (boolean))
649 (cons (const plain-list-item
) (boolean))))
651 (defcustom org-insert-heading-hook nil
652 "Hook being run after inserting a new heading."
653 :group
'org-edit-structure
656 (defcustom org-enable-fixed-width-editor t
657 "Non-nil means, lines starting with \":\" are treated as fixed-width.
658 This currently only means, they are never auto-wrapped.
659 When nil, such lines will be treated like ordinary lines.
660 See also the QUOTE keyword."
661 :group
'org-edit-structure
664 (defcustom org-goto-auto-isearch t
665 "Non-nil means, typing characters in org-goto starts incremental search."
666 :group
'org-edit-structure
669 (defgroup org-sparse-trees nil
670 "Options concerning sparse trees in Org-mode."
671 :tag
"Org Sparse Trees"
672 :group
'org-structure
)
674 (defcustom org-highlight-sparse-tree-matches t
675 "Non-nil means, highlight all matches that define a sparse tree.
676 The highlights will automatically disappear the next time the buffer is
677 changed by an edit command."
678 :group
'org-sparse-trees
681 (defcustom org-remove-highlights-with-change t
682 "Non-nil means, any change to the buffer will remove temporary highlights.
683 Such highlights are created by `org-occur' and `org-clock-display'.
684 When nil, `C-c C-c needs to be used to get rid of the highlights.
685 The highlights created by `org-preview-latex-fragment' always need
686 `C-c C-c' to be removed."
687 :group
'org-sparse-trees
692 (defcustom org-occur-hook
'(org-first-headline-recenter)
693 "Hook that is run after `org-occur' has constructed a sparse tree.
694 This can be used to recenter the window to show as much of the structure
696 :group
'org-sparse-trees
699 (defgroup org-plain-lists nil
700 "Options concerning plain lists in Org-mode."
701 :tag
"Org Plain lists"
702 :group
'org-structure
)
704 (defcustom org-cycle-include-plain-lists nil
705 "Non-nil means, include plain lists into visibility cycling.
706 This means that during cycling, plain list items will *temporarily* be
707 interpreted as outline headlines with a level given by 1000+i where i is the
708 indentation of the bullet. In all other operations, plain list items are
709 not seen as headlines. For example, you cannot assign a TODO keyword to
711 :group
'org-plain-lists
714 (defcustom org-plain-list-ordered-item-terminator t
715 "The character that makes a line with leading number an ordered list item.
716 Valid values are ?. and ?\). To get both terminators, use t. While
717 ?. may look nicer, it creates the danger that a line with leading
718 number may be incorrectly interpreted as an item. ?\) therefore is
720 :group
'org-plain-lists
721 :type
'(choice (const :tag
"dot like in \"2.\"" ?.
)
722 (const :tag
"paren like in \"2)\"" ?\
))
723 (const :tab
"both" t
)))
725 (defcustom org-auto-renumber-ordered-lists t
726 "Non-nil means, automatically renumber ordered plain lists.
727 Renumbering happens when the sequence have been changed with
728 \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
729 use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
730 :group
'org-plain-lists
733 (defcustom org-provide-checkbox-statistics t
734 "Non-nil means, update checkbox statistics after insert and toggle.
735 When this is set, checkbox statistics is updated each time you either insert
736 a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
737 with \\[org-ctrl-c-ctrl-c\\]."
738 :group
'org-plain-lists
741 (defgroup org-archive nil
742 "Options concerning archiving in Org-mode."
744 :group
'org-structure
)
746 (defcustom org-archive-tag
"ARCHIVE"
747 "The tag that marks a subtree as archived.
748 An archived subtree does not open during visibility cycling, and does
749 not contribute to the agenda listings.
750 After changing this, font-lock must be restarted in the relevant buffers to
751 get the proper fontification."
756 (defcustom org-agenda-skip-archived-trees t
757 "Non-nil means, the agenda will skip any items located in archived trees.
758 An archived tree is a tree marked with the tag ARCHIVE."
760 :group
'org-agenda-skip
763 (defcustom org-cycle-open-archived-trees nil
764 "Non-nil means, `org-cycle' will open archived trees.
765 An archived tree is a tree marked with the tag ARCHIVE.
766 When nil, archived trees will stay folded. You can still open them with
767 normal outline commands like `show-all', but not with the cycling commands."
772 (defcustom org-sparse-tree-open-archived-trees nil
773 "Non-nil means sparse tree construction shows matches in archived trees.
774 When nil, matches in these trees are highlighted, but the trees are kept in
777 :group
'org-sparse-trees
780 (defcustom org-archive-location
"%s_archive::"
781 "The location where subtrees should be archived.
782 This string consists of two parts, separated by a double-colon.
784 The first part is a file name - when omitted, archiving happens in the same
785 file. %s will be replaced by the current file name (without directory part).
786 Archiving to a different file is useful to keep archived entries from
787 contributing to the Org-mode Agenda.
789 The part after the double colon is a headline. The archived entries will be
790 filed under that headline. When omitted, the subtrees are simply filed away
791 at the end of the file, as top-level entries.
793 Here are a few examples:
795 If the current file is Projects.org, archive in file
796 Projects.org_archive, as top-level trees. This is the default.
798 \"::* Archived Tasks\"
799 Archive in the current file, under the top-level headline
800 \"* Archived Tasks\".
802 \"~/org/archive.org::\"
803 Archive in file ~/org/archive.org (absolute path), as top-level trees.
805 \"basement::** Finished Tasks\"
806 Archive in file ./basement (relative path), as level 3 trees
807 below the level 2 heading \"** Finished Tasks\".
809 You may set this option on a per-file basis by adding to the buffer a
812 #+ARCHIVE: basement::** Finished Tasks"
816 (defcustom org-archive-mark-done t
817 "Non-nil means, mark entries as DONE when they are moved to the archive file.
818 This can be a string to set the keyword to use. When t, Org-mode will
819 use the first keyword in its list that means done."
822 (const :tag
"No" nil
)
824 (string :tag
"Use this keyword")))
826 (defcustom org-archive-stamp-time t
827 "Non-nil means, add a time stamp to entries moved to an archive file.
828 This variable is obsolete and has no effect anymore, instead add ot remove
829 `time' from the variablle `org-archive-save-context-info'."
833 (defcustom org-archive-save-context-info
'(time file olpath category todo itags
)
834 "Parts of context info that should be stored as properties when archiving.
835 When a subtree is moved to an archive file, it looses information given by
836 context, like inherited tags, the category, and possibly also the TODO
837 state (depending on the variable `org-archive-mark-done').
838 This variable can be a list of any of the following symbols:
840 time The time of archiving.
841 file The file where the entry originates.
842 itags The local tags, in the headline of the subtree.
843 ltags The tags the subtree inherits from further up the hierarchy.
844 todo The pre-archive TODO state.
845 category The category, taken from file name or #+CATEGORY lines.
846 olpath The outline path to the item. These are all headlines above
847 the current item, separated by /, like a file path.
849 For each symbol present in the list, a property will be created in
850 the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
853 :type
'(set :greedy t
854 (const :tag
"Time" time
)
855 (const :tag
"File" file
)
856 (const :tag
"Category" category
)
857 (const :tag
"TODO state" todo
)
858 (const :tag
"TODO state" priority
)
859 (const :tag
"Inherited tags" itags
)
860 (const :tag
"Outline path" olpath
)
861 (const :tag
"Local tags" ltags
)))
863 (defgroup org-imenu-and-speedbar nil
864 "Options concerning imenu and speedbar in Org-mode."
865 :tag
"Org Imenu and Speedbar"
866 :group
'org-structure
)
868 (defcustom org-imenu-depth
2
869 "The maximum level for Imenu access to Org-mode headlines.
870 This also applied for speedbar access."
871 :group
'org-imenu-and-speedbar
874 (defgroup org-table nil
875 "Options concerning tables in Org-mode."
879 (defcustom org-enable-table-editor
'optimized
880 "Non-nil means, lines starting with \"|\" are handled by the table editor.
881 When nil, such lines will be treated like ordinary lines.
883 When equal to the symbol `optimized', the table editor will be optimized to
885 - Automatic overwrite mode in front of whitespace in table fields.
886 This makes the structure of the table stay in tact as long as the edited
887 field does not exceed the column width.
888 - Minimize the number of realigns. Normally, the table is aligned each time
889 TAB or RET are pressed to move to another field. With optimization this
890 happens only if changes to a field might have changed the column width.
891 Optimization requires replacing the functions `self-insert-command',
892 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
893 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
894 very good at guessing when a re-align will be necessary, but you can always
895 force one with \\[org-ctrl-c-ctrl-c].
897 If you would like to use the optimized version in Org-mode, but the
898 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
900 This variable can be used to turn on and off the table editor during a session,
901 but in order to toggle optimization, a restart is required.
903 See also the variable `org-table-auto-blank-field'."
906 (const :tag
"off" nil
)
908 (const :tag
"on, optimized" optimized
)))
910 (defcustom org-table-tab-recognizes-table.el t
911 "Non-nil means, TAB will automatically notice a table.el table.
912 When it sees such a table, it moves point into it and - if necessary -
913 calls `table-recognize-table'."
914 :group
'org-table-editing
917 (defgroup org-link nil
918 "Options concerning links in Org-mode."
922 (defvar org-link-abbrev-alist-local nil
923 "Buffer-local version of `org-link-abbrev-alist', which see.
924 The value of this is taken from the #+LINK lines.")
925 (make-variable-buffer-local 'org-link-abbrev-alist-local
)
927 (defcustom org-link-abbrev-alist nil
928 "Alist of link abbreviations.
929 The car of each element is a string, to be replaced at the start of a link.
930 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
931 links in Org-mode buffers can have an optional tag after a double colon, e.g.
933 [[linkkey:tag][description]]
935 If REPLACE is a string, the tag will simply be appended to create the link.
936 If the string contains \"%s\", the tag will be inserted there.
938 REPLACE may also be a function that will be called with the tag as the
939 only argument to create the link, which should be returned as a string.
941 See the manual for examples."
945 (defcustom org-descriptive-links t
946 "Non-nil means, hide link part and only show description of bracket links.
947 Bracket links are like [[link][descritpion]]. This variable sets the initial
948 state in new org-mode buffers. The setting can then be toggled on a
949 per-buffer basis from the Org->Hyperlinks menu."
953 (defcustom org-link-file-path-type
'adaptive
954 "How the path name in file links should be stored.
957 relative Relative to the current directory, i.e. the directory of the file
958 into which the link is being inserted.
959 absolute Absolute path, if possible with ~ for home directory.
960 noabbrev Absolute path, no abbreviation of home directory.
961 adaptive Use relative path for files in the current directory and sub-
962 directories of it. For other files, use an absolute path."
970 (defcustom org-activate-links
'(bracket angle plain radio tag date
)
971 "Types of links that should be activated in Org-mode files.
972 This is a list of symbols, each leading to the activation of a certain link
973 type. In principle, it does not hurt to turn on most link types - there may
974 be a small gain when turning off unused link types. The types are:
976 bracket The recommended [[link][description]] or [[link]] links with hiding.
977 angular Links in angular brackes that may contain whitespace like
978 <bbdb:Carsten Dominik>.
979 plain Plain links in normal text, no whitespace, like http://google.com.
980 radio Text that is matched by a radio target, see manual for details.
981 tag Tag settings in a headline (link to tag search).
982 date Time stamps (link to calendar).
984 Changing this variable requires a restart of Emacs to become effective."
986 :type
'(set (const :tag
"Double bracket links (new style)" bracket
)
987 (const :tag
"Angular bracket links (old style)" angular
)
988 (const :tag
"Plain text links" plain
)
989 (const :tag
"Radio target matches" radio
)
990 (const :tag
"Tags" tag
)
991 (const :tag
"Timestamps" date
)))
993 (defgroup org-link-store nil
994 "Options concerning storing links in Org-mode."
995 :tag
"Org Store Link"
998 (defcustom org-email-link-description-format
"Email %c: %.30s"
999 "Format of the description part of a link to an email or usenet message.
1000 The following %-excapes will be replaced by corresponding information:
1002 %F full \"From\" field
1003 %f name, taken from \"From\" field, address if no name
1004 %T full \"To\" field
1005 %t first name in \"To\" field, address if no name
1006 %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
1007 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
1011 You may use normal field width specification between the % and the letter.
1012 This is for example useful to limit the length of the subject.
1014 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
1015 :group
'org-link-store
1018 (defcustom org-from-is-user-regexp
1020 (when (and user-mail-address
(not (string= user-mail-address
"")))
1021 (setq r1
(concat "\\<" (regexp-quote user-mail-address
) "\\>")))
1022 (when (and user-full-name
(not (string= user-full-name
"")))
1023 (setq r2
(concat "\\<" (regexp-quote user-full-name
) "\\>")))
1024 (if (and r1 r2
) (concat r1
"\\|" r2
) (or r1 r2
)))
1025 "Regexp mached against the \"From:\" header of an email or usenet message.
1026 It should match if the message is from the user him/herself."
1027 :group
'org-link-store
1030 (defcustom org-context-in-file-links t
1031 "Non-nil means, file links from `org-store-link' contain context.
1032 A search string will be added to the file name with :: as separator and
1033 used to find the context when the link is activated by the command
1034 `org-open-at-point'.
1035 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1036 negates this setting for the duration of the command."
1037 :group
'org-link-store
1040 (defcustom org-keep-stored-link-after-insertion nil
1041 "Non-nil means, keep link in list for entire session.
1043 The command `org-store-link' adds a link pointing to the current
1044 location to an internal list. These links accumulate during a session.
1045 The command `org-insert-link' can be used to insert links into any
1046 Org-mode file (offering completion for all stored links). When this
1047 option is nil, every link which has been inserted once using \\[org-insert-link]
1048 will be removed from the list, to make completing the unused links
1050 :group
'org-link-store
1053 (defgroup org-link-follow nil
1054 "Options concerning following links in Org-mode."
1055 :tag
"Org Follow Link"
1058 (defcustom org-follow-link-hook nil
1059 "Hook that is run after a link has been followed."
1060 :group
'org-link-follow
1063 (defcustom org-tab-follows-link nil
1064 "Non-nil means, on links TAB will follow the link.
1065 Needs to be set before org.el is loaded."
1066 :group
'org-link-follow
1069 (defcustom org-return-follows-link nil
1070 "Non-nil means, on links RET will follow the link.
1071 Needs to be set before org.el is loaded."
1072 :group
'org-link-follow
1075 (defcustom org-mouse-1-follows-link
1076 (if (boundp 'mouse-1-click-follows-link
) mouse-1-click-follows-link t
)
1077 "Non-nil means, mouse-1 on a link will follow the link.
1078 A longer mouse click will still set point. Does not work on XEmacs.
1079 Needs to be set before org.el is loaded."
1080 :group
'org-link-follow
1083 (defcustom org-mark-ring-length
4
1084 "Number of different positions to be recorded in the ring
1085 Changing this requires a restart of Emacs to work correctly."
1086 :group
'org-link-follow
1089 (defcustom org-link-frame-setup
1090 '((vm . vm-visit-folder-other-frame
)
1091 (gnus . gnus-other-frame
)
1092 (file . find-file-other-window
))
1093 "Setup the frame configuration for following links.
1094 When following a link with Emacs, it may often be useful to display
1095 this link in another window or frame. This variable can be used to
1096 set this up for the different types of links.
1099 `vm-visit-folder-other-frame'
1100 For Gnus, use any of
1103 For FILE, use any of
1105 `find-file-other-window'
1106 `find-file-other-frame'
1107 For the calendar, use the variable `calendar-setup'.
1108 For BBDB, it is currently only possible to display the matches in
1110 :group
'org-link-follow
1114 (const vm-visit-folder
)
1115 (const vm-visit-folder-other-window
)
1116 (const vm-visit-folder-other-frame
)))
1120 (const gnus-other-frame
)))
1124 (const find-file-other-window
)
1125 (const find-file-other-frame
)))))
1127 (defcustom org-display-internal-link-with-indirect-buffer nil
1128 "Non-nil means, use indirect buffer to display infile links.
1129 Activating internal links (from one location in a file to another location
1130 in the same file) normally just jumps to the location. When the link is
1131 activated with a C-u prefix (or with mouse-3), the link is displayed in
1132 another window. When this option is set, the other window actually displays
1133 an indirect buffer clone of the current buffer, to avoid any visibility
1134 changes to the current buffer."
1135 :group
'org-link-follow
1138 (defcustom org-open-non-existing-files nil
1139 "Non-nil means, `org-open-file' will open non-existing files.
1140 When nil, an error will be generated."
1141 :group
'org-link-follow
1144 (defcustom org-link-mailto-program
'(browse-url "mailto:%a?subject=%s")
1145 "Function and arguments to call for following mailto links.
1146 This is a list with the first element being a lisp function, and the
1147 remaining elements being arguments to the function. In string arguments,
1148 %a will be replaced by the address, and %s will be replaced by the subject
1149 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1150 :group
'org-link-follow
1152 (const :tag
"browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1153 (const :tag
"compose-mail" (compose-mail "%a" "%s"))
1154 (const :tag
"message-mail" (message-mail "%a" "%s"))
1155 (cons :tag
"other" (function) (repeat :tag
"argument" sexp
))))
1157 (defcustom org-confirm-shell-link-function
'yes-or-no-p
1158 "Non-nil means, ask for confirmation before executing shell links.
1159 Shell links can be dangerous: just think about a link
1161 [[shell:rm -rf ~/*][Google Search]]
1163 This link would show up in your Org-mode document as \"Google Search\",
1164 but really it would remove your entire home directory.
1165 Therefore we advise against setting this variable to nil.
1166 Just change it to `y-or-n-p' of you want to confirm with a
1167 single keystroke rather than having to type \"yes\"."
1168 :group
'org-link-follow
1170 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1171 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1172 (const :tag
"no confirmation (dangerous)" nil
)))
1174 (defcustom org-confirm-elisp-link-function
'yes-or-no-p
1175 "Non-nil means, ask for confirmation before executing Emacs Lisp links.
1176 Elisp links can be dangerous: just think about a link
1178 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1180 This link would show up in your Org-mode document as \"Google Search\",
1181 but really it would remove your entire home directory.
1182 Therefore we advise against setting this variable to nil.
1183 Just change it to `y-or-n-p' of you want to confirm with a
1184 single keystroke rather than having to type \"yes\"."
1185 :group
'org-link-follow
1187 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1188 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1189 (const :tag
"no confirmation (dangerous)" nil
)))
1191 (defconst org-file-apps-defaults-gnu
1194 "Default file applications on a UNIX or GNU/Linux system.
1195 See `org-file-apps'.")
1197 (defconst org-file-apps-defaults-macosx
1203 ("eps.gz" .
"gv %s")
1205 ("fig" .
"xfig %s"))
1206 "Default file applications on a MacOS X system.
1207 The system \"open\" is known as a default, but we use X11 applications
1208 for some files for which the OS does not have a good default.
1209 See `org-file-apps'.")
1211 (defconst org-file-apps-defaults-windowsnt
1215 (list (if (featurep 'xemacs
)
1216 'mswindows-shell-execute
1219 "Default file applications on a Windows NT system.
1220 The system \"open\" is used for most files.
1221 See `org-file-apps'.")
1223 (defcustom org-file-apps
1232 "External applications for opening `file:path' items in a document.
1233 Org-mode uses system defaults for different file types, but
1234 you can use this variable to set the application for a given file
1235 extension. The entries in this list are cons cells where the car identifies
1236 files and the cdr the corresponding command. Possible values for the
1238 \"ext\" A string identifying an extension
1239 `directory' Matches a directory
1240 `remote' Matches a remote file, accessible through tramp or efs.
1241 Remote files most likely should be visited through Emacs
1242 because external applications cannot handle such paths.
1243 t Default for all remaining files
1245 Possible values for the command are:
1246 `emacs' The file will be visited by the current Emacs process.
1247 `default' Use the default application for this file type.
1248 string A command to be executed by a shell; %s will be replaced
1249 by the path to the file.
1250 sexp A Lisp form which will be evaluated. The file path will
1251 be available in the Lisp variable `file'.
1252 For more examples, see the system specific constants
1253 `org-file-apps-defaults-macosx'
1254 `org-file-apps-defaults-windowsnt'
1255 `org-file-apps-defaults-gnu'."
1256 :group
'org-link-follow
1258 (cons (choice :value
""
1259 (string :tag
"Extension")
1260 (const :tag
"Default for unrecognized files" t
)
1261 (const :tag
"Remote file" remote
)
1262 (const :tag
"Links to a directory" directory
))
1264 (const :tag
"Visit with Emacs" emacs
)
1265 (const :tag
"Use system default" default
)
1266 (string :tag
"Command")
1267 (sexp :tag
"Lisp form")))))
1269 (defgroup org-refile nil
1270 "Options concerning refiling entries in Org-mode."
1274 (defcustom org-directory
"~/org"
1275 "Directory with org files.
1276 This directory will be used as default to prompt for org files.
1277 Used by the hooks for remember.el."
1279 :group
'org-remember
1282 (defcustom org-default-notes-file
"~/.notes"
1283 "Default target for storing notes.
1284 Used by the hooks for remember.el. This can be a string, or nil to mean
1285 the value of `remember-data-file'.
1286 You can set this on a per-template basis with the variable
1287 `org-remember-templates'."
1289 :group
'org-remember
1291 (const :tag
"Default from remember-data-file" nil
)
1294 (defcustom org-goto-interface
'outline
1295 "The default interface to be used for `org-goto'.
1297 outline The interface shows an outline of the relevant file
1298 and the correct heading is found by moving through
1299 the outline or by searching with incremental search.
1300 outline-path-completion Headlines in the current buffer are offered via
1304 (const :tag
"Outline" outline
)
1305 (const :tag
"Outline-path-completion" outline-path-completion
)))
1307 (defcustom org-reverse-note-order nil
1308 "Non-nil means, store new notes at the beginning of a file or entry.
1309 When nil, new notes will be filed to the end of a file or entry.
1310 This can also be a list with cons cells of regular expressions that
1311 are matched against file names, and values."
1312 :group
'org-remember
1314 (const :tag
"Reverse always" t
)
1315 (const :tag
"Reverse never" nil
)
1316 (repeat :tag
"By file name regexp"
1317 (cons regexp boolean
))))
1319 (defcustom org-refile-targets nil
1320 "Targets for refiling entries with \\[org-refile].
1321 This is list of cons cells. Each cell contains:
1322 - a specification of the files to be considered, either a list of files,
1323 or a symbol whose function or variable value will be used to retrieve
1324 a file name or a list of file names. Nil means, refile to a different
1325 heading in the current buffer.
1326 - A specification of how to find candidate refile targets. This may be
1328 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
1329 This tag has to be present in all target headlines, inheritance will
1331 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
1333 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
1334 headlines that are refiling targets.
1335 - a cons cell (:level . N). Any headline of level N is considered a target.
1336 - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
1337 :group
'org-remember
1340 (choice :value org-agenda-files
1341 (const :tag
"All agenda files" org-agenda-files
)
1342 (const :tag
"Current buffer" nil
)
1343 (function) (variable) (file))
1344 (choice :tag
"Identify target headline by"
1345 (cons :tag
"Specific tag" (const :tag
) (string))
1346 (cons :tag
"TODO keyword" (const :todo
) (string))
1347 (cons :tag
"Regular expression" (const :regexp
) (regexp))
1348 (cons :tag
"Level number" (const :level
) (integer))
1349 (cons :tag
"Max Level number" (const :maxlevel
) (integer))))))
1351 (defcustom org-refile-use-outline-path nil
1352 "Non-nil means, provide refile targets as paths.
1353 So a level 3 headline will be available as level1/level2/level3.
1354 When the value is `file', also include the file name (without directory)
1355 into the path. When `full-file-path', include the full file path."
1356 :group
'org-remember
1358 (const :tag
"Not" nil
)
1359 (const :tag
"Yes" t
)
1360 (const :tag
"Start with file name" file
)
1361 (const :tag
"Start with full file path" full-file-path
)))
1363 (defgroup org-todo nil
1364 "Options concerning TODO items in Org-mode."
1368 (defgroup org-progress nil
1369 "Options concerning Progress logging in Org-mode."
1373 (defcustom org-todo-keywords
'((sequence "TODO" "DONE"))
1374 "List of TODO entry keyword sequences and their interpretation.
1375 \\<org-mode-map>This is a list of sequences.
1377 Each sequence starts with a symbol, either `sequence' or `type',
1378 indicating if the keywords should be interpreted as a sequence of
1379 action steps, or as different types of TODO items. The first
1380 keywords are states requiring action - these states will select a headline
1381 for inclusion into the global TODO list Org-mode produces. If one of
1382 the \"keywords\" is the vertical bat \"|\" the remaining keywords
1383 signify that no further action is necessary. If \"|\" is not found,
1384 the last keyword is treated as the only DONE state of the sequence.
1386 The command \\[org-todo] cycles an entry through these states, and one
1387 additional state where no keyword is present. For details about this
1388 cycling, see the manual.
1390 TODO keywords and interpretation can also be set on a per-file basis with
1391 the special #+SEQ_TODO and #+TYP_TODO lines.
1393 Each keyword can optionally specify a character for fast state selection
1394 \(in combination with the variable `org-use-fast-todo-selection')
1395 and specifiers for state change logging, using the same syntax
1396 that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
1397 that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
1398 indicates to record a time stamp each time this state is selected.
1400 Each keyword may also specify if a timestamp or a note should be
1401 recorded when entering or leaving the state, by adding additional
1402 characters in the parenthesis after the keyword. This looks like this:
1403 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
1404 record only the time of the state change. With X and Y being either
1405 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
1406 Y when leaving the state if and only if the *target* state does not
1407 define X. You may omit any of the fast-selection key or X or /Y,
1408 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
1410 For backward compatibility, this variable may also be just a list
1411 of keywords - in this case the interptetation (sequence or type) will be
1412 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
1414 :group
'org-keywords
1416 (repeat :tag
"Old syntax, just keywords"
1417 (string :tag
"Keyword"))
1418 (repeat :tag
"New syntax"
1421 :tag
"Interpretation"
1422 (const :tag
"Sequence (cycling hits every state)" sequence
)
1423 (const :tag
"Type (cycling directly to DONE)" type
))
1425 (string :tag
"Keyword"))))))
1427 (defvar org-todo-keywords-1 nil
1428 "All TODO and DONE keywords active in a buffer.")
1429 (make-variable-buffer-local 'org-todo-keywords-1
)
1430 (defvar org-todo-keywords-for-agenda nil
)
1431 (defvar org-done-keywords-for-agenda nil
)
1432 (defvar org-not-done-keywords nil
)
1433 (make-variable-buffer-local 'org-not-done-keywords
)
1434 (defvar org-done-keywords nil
)
1435 (make-variable-buffer-local 'org-done-keywords
)
1436 (defvar org-todo-heads nil
)
1437 (make-variable-buffer-local 'org-todo-heads
)
1438 (defvar org-todo-sets nil
)
1439 (make-variable-buffer-local 'org-todo-sets
)
1440 (defvar org-todo-log-states nil
)
1441 (make-variable-buffer-local 'org-todo-log-states
)
1442 (defvar org-todo-kwd-alist nil
)
1443 (make-variable-buffer-local 'org-todo-kwd-alist
)
1444 (defvar org-todo-key-alist nil
)
1445 (make-variable-buffer-local 'org-todo-key-alist
)
1446 (defvar org-todo-key-trigger nil
)
1447 (make-variable-buffer-local 'org-todo-key-trigger
)
1449 (defcustom org-todo-interpretation
'sequence
1450 "Controls how TODO keywords are interpreted.
1451 This variable is in principle obsolete and is only used for
1452 backward compatibility, if the interpretation of todo keywords is
1453 not given already in `org-todo-keywords'. See that variable for
1456 :group
'org-keywords
1457 :type
'(choice (const sequence
)
1460 (defcustom org-use-fast-todo-selection
'prefix
1461 "Non-nil means, use the fast todo selection scheme with C-c C-t.
1462 This variable describes if and under what circumstances the cycling
1463 mechanism for TODO keywords will be replaced by a single-key, direct
1466 When nil, fast selection is never used.
1468 When the symbol `prefix', it will be used when `org-todo' is called with
1469 a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
1470 in an agenda buffer.
1472 When t, fast selection is used by default. In this case, the prefix
1473 argument forces cycling instead.
1475 In all cases, the special interface is only used if access keys have actually
1476 been assigned by the user, i.e. if keywords in the configuration are followed
1477 by a letter in parenthesis, like TODO(t)."
1480 (const :tag
"Never" nil
)
1481 (const :tag
"By default" t
)
1482 (const :tag
"Only with C-u C-c C-t" prefix
)))
1484 (defcustom org-after-todo-state-change-hook nil
1485 "Hook which is run after the state of a TODO item was changed.
1486 The new state (a string with a TODO keyword, or nil) is available in the
1487 Lisp variable `state'."
1491 (defcustom org-log-done nil
1492 "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
1493 When equal to the list (done), also prompt for a closing note.
1494 This can also be configured on a per-file basis by adding one of
1495 the following lines anywhere in the buffer:
1498 #+STARTUP: lognotedone
1499 #+STARTUP: nologdone"
1501 :group
'org-progress
1503 (const :tag
"No logging" nil
)
1504 (const :tag
"Record CLOSED timestamp" time
)
1505 (const :tag
"Record CLOSED timestamp with closing note." note
)))
1507 ;; Normalize old uses of org-log-done.
1509 ((eq org-log-done t
) (setq org-log-done
'time
))
1510 ((and (listp org-log-done
) (memq 'done org-log-done
))
1511 (setq org-log-done
'note
)))
1513 (defcustom org-log-note-clock-out nil
1514 "Non-nil means, recored a note when clocking out of an item.
1515 This can also be configured on a per-file basis by adding one of
1516 the following lines anywhere in the buffer:
1518 #+STARTUP: lognoteclock-out
1519 #+STARTUP: nolognoteclock-out"
1521 :group
'org-progress
1524 (defcustom org-log-done-with-time t
1525 "Non-nil means, the CLOSED time stamp will contain date and time.
1526 When nil, only the date will be recorded."
1527 :group
'org-progress
1530 (defcustom org-log-note-headings
1531 '((done .
"CLOSING NOTE %t")
1532 (state .
"State %-12s %t")
1533 (note .
"Note taken on %t")
1535 "Headings for notes added to entries.
1536 The value is an alist, with the car being a symbol indicating the note
1537 context, and the cdr is the heading to be used. The heading may also be the
1539 %t in the heading will be replaced by a time stamp.
1540 %s will be replaced by the new TODO state, in double quotes.
1541 %u will be replaced by the user name.
1542 %U will be replaced by the full user name."
1544 :group
'org-progress
1545 :type
'(list :greedy t
1546 (cons (const :tag
"Heading when closing an item" done
) string
)
1548 "Heading when changing todo state (todo sequence only)"
1550 (cons (const :tag
"Heading when just taking a note" note
) string
)
1551 (cons (const :tag
"Heading when clocking out" clock-out
) string
)))
1553 (unless (assq 'note org-log-note-headings
)
1554 (push '(note .
"%t") org-log-note-headings
))
1556 (defcustom org-log-states-order-reversed t
1557 "Non-nil means, the latest state change note will be directly after heading.
1558 When nil, the notes will be orderer according to time."
1560 :group
'org-progress
1563 (defcustom org-log-repeat
'time
1564 "Non-nil means, record moving through the DONE state when triggering repeat.
1565 An auto-repeating tasks is immediately switched back to TODO when marked
1566 done. If you are not logging state changes (by adding \"@\" or \"!\" to
1567 the TODO keyword definition, or recording a cloing note by setting
1568 `org-log-done', there will be no record of the task moving trhough DONE.
1569 This variable forces taking a note anyway. Possible values are:
1571 nil Don't force a record
1572 time Record a time stamp
1575 This option can also be set with on a per-file-basis with
1577 #+STARTUP: logrepeat
1578 #+STARTUP: lognoterepeat
1579 #+STARTUP: nologrepeat
1581 You can have local logging settings for a subtree by setting the LOGGING
1582 property to one or more of these keywords."
1584 :group
'org-progress
1586 (const :tag
"Don't force a record" nil
)
1587 (const :tag
"Force recording the DONE state" time
)
1588 (const :tag
"Force recording a note with the DONE state" note
)))
1591 (defgroup org-priorities nil
1592 "Priorities in Org-mode."
1593 :tag
"Org Priorities"
1596 (defcustom org-highest-priority ?A
1597 "The highest priority of TODO items. A character like ?A, ?B etc.
1598 Must have a smaller ASCII number than `org-lowest-priority'."
1599 :group
'org-priorities
1602 (defcustom org-lowest-priority ?C
1603 "The lowest priority of TODO items. A character like ?A, ?B etc.
1604 Must have a larger ASCII number than `org-highest-priority'."
1605 :group
'org-priorities
1608 (defcustom org-default-priority ?B
1609 "The default priority of TODO items.
1610 This is the priority an item get if no explicit priority is given."
1611 :group
'org-priorities
1614 (defcustom org-priority-start-cycle-with-default t
1615 "Non-nil means, start with default priority when starting to cycle.
1616 When this is nil, the first step in the cycle will be (depending on the
1617 command used) one higher or lower that the default priority."
1618 :group
'org-priorities
1621 (defgroup org-time nil
1622 "Options concerning time stamps and deadlines in Org-mode."
1626 (defcustom org-insert-labeled-timestamps-at-point nil
1627 "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
1628 When nil, these labeled time stamps are forces into the second line of an
1629 entry, just after the headline. When scheduling from the global TODO list,
1630 the time stamp will always be forced into the second line."
1634 (defconst org-time-stamp-formats
'("<%Y-%m-%d %a>" .
"<%Y-%m-%d %a %H:%M>")
1635 "Formats for `format-time-string' which are used for time stamps.
1636 It is not recommended to change this constant.")
1638 (defcustom org-time-stamp-rounding-minutes
'(0 5)
1639 "Number of minutes to round time stamps to.
1640 These are two values, the first applies when first creating a time stamp.
1641 The second applies when changing it with the commands `S-up' and `S-down'.
1642 When changing the time stamp, this means that it will change in steps
1643 of N minutes, as given by the second value.
1645 When a setting is 0 or 1, insert the time unmodified. Useful rounding
1646 numbers should be factors of 60, so for example 5, 10, 15.
1648 When this is larger than 1, you can still force an exact time-stamp by using
1649 a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
1650 and by using a prefix arg to `S-up/down' to specify the exact number
1651 of minutes to shift."
1653 :get
'(lambda (var) ; Make sure all entries have 5 elements
1654 (if (integerp (default-value var
))
1655 (list (default-value var
) 5)
1656 (default-value var
)))
1658 (integer :tag
"when inserting times")
1659 (integer :tag
"when modifying times")))
1661 ;; Normalize old customizations of this variable.
1662 (when (integerp org-time-stamp-rounding-minutes
)
1663 (setq org-time-stamp-rounding-minutes
1664 (list org-time-stamp-rounding-minutes
1665 org-time-stamp-rounding-minutes
)))
1667 (defcustom org-display-custom-times nil
1668 "Non-nil means, overlay custom formats over all time stamps.
1669 The formats are defined through the variable `org-time-stamp-custom-formats'.
1670 To turn this on on a per-file basis, insert anywhere in the file:
1671 #+STARTUP: customtime"
1675 (make-variable-buffer-local 'org-display-custom-times
)
1677 (defcustom org-time-stamp-custom-formats
1678 '("<%m/%d/%y %a>" .
"<%m/%d/%y %a %H:%M>") ; american
1679 "Custom formats for time stamps. See `format-time-string' for the syntax.
1680 These are overlayed over the default ISO format if the variable
1681 `org-display-custom-times' is set. Time like %H:%M should be at the
1682 end of the second format."
1686 (defun org-time-stamp-format (&optional long inactive
)
1687 "Get the right format for a time string."
1688 (let ((f (if long
(cdr org-time-stamp-formats
)
1689 (car org-time-stamp-formats
))))
1691 (concat "[" (substring f
1 -
1) "]")
1694 (defcustom org-deadline-warning-days
14
1695 "No. of days before expiration during which a deadline becomes active.
1696 This variable governs the display in sparse trees and in the agenda.
1697 When 0 or negative, it means use this number (the absolute value of it)
1698 even if a deadline has a different individual lead time specified."
1700 :group
'org-agenda-daily
/weekly
1703 (defcustom org-read-date-prefer-future t
1704 "Non-nil means, assume future for incomplete date input from user.
1705 This affects the following situations:
1706 1. The user gives a day, but no month.
1707 For example, if today is the 15th, and you enter \"3\", Org-mode will
1708 read this as the third of *next* month. However, if you enter \"17\",
1709 it will be considered as *this* month.
1710 2. The user gives a month but not a year.
1711 For example, if it is april and you enter \"feb 2\", this will be read
1712 as feb 2, *next* year. \"May 5\", however, will be this year.
1714 Currently this does not work for ISO week specifications.
1716 When this option is nil, the current month and year will always be used
1721 (defcustom org-read-date-display-live t
1722 "Non-nil means, display current interpretation of date prompt live.
1723 This display will be in an overlay, in the minibuffer."
1727 (defcustom org-read-date-popup-calendar t
1728 "Non-nil means, pop up a calendar when prompting for a date.
1729 In the calendar, the date can be selected with mouse-1. However, the
1730 minibuffer will also be active, and you can simply enter the date as well.
1731 When nil, only the minibuffer will be available."
1734 (if (fboundp 'defvaralias
)
1735 (defvaralias 'org-popup-calendar-for-date-prompt
1736 'org-read-date-popup-calendar
))
1738 (defcustom org-extend-today-until
0
1739 "The hour when your day really ends.
1740 This has influence for the following applications:
1741 - When switching the agenda to \"today\". It it is still earlier than
1742 the time given here, the day recognized as TODAY is actually yesterday.
1743 - When a date is read from the user and it is still before the time given
1744 here, the current date and time will be assumed to be yesterday, 23:59.
1747 IMPORTANT: This is still a very experimental feature, it may disappear
1748 again or it may be extended to mean more things."
1752 (defcustom org-edit-timestamp-down-means-later nil
1753 "Non-nil means, S-down will increase the time in a time stamp.
1754 When nil, S-up will increase."
1758 (defcustom org-calendar-follow-timestamp-change t
1759 "Non-nil means, make the calendar window follow timestamp changes.
1760 When a timestamp is modified and the calendar window is visible, it will be
1761 moved to the new date."
1765 (defgroup org-tags nil
1766 "Options concerning tags in Org-mode."
1770 (defcustom org-tag-alist nil
1771 "List of tags allowed in Org-mode files.
1772 When this list is nil, Org-mode will base TAG input on what is already in the
1774 The value of this variable is an alist, the car of each entry must be a
1775 keyword as a string, the cdr may be a character that is used to select
1776 that tag through the fast-tag-selection interface.
1777 See the manual for details."
1781 (cons (string :tag
"Tag name")
1782 (character :tag
"Access char"))
1783 (const :tag
"Start radio group" (:startgroup
))
1784 (const :tag
"End radio group" (:endgroup
)))))
1786 (defcustom org-use-fast-tag-selection
'auto
1787 "Non-nil means, use fast tag selection scheme.
1788 This is a special interface to select and deselect tags with single keys.
1789 When nil, fast selection is never used.
1790 When the symbol `auto', fast selection is used if and only if selection
1791 characters for tags have been configured, either through the variable
1792 `org-tag-alist' or through a #+TAGS line in the buffer.
1793 When t, fast selection is always used and selection keys are assigned
1794 automatically if necessary."
1797 (const :tag
"Always" t
)
1798 (const :tag
"Never" nil
)
1799 (const :tag
"When selection characters are configured" 'auto
)))
1801 (defcustom org-fast-tag-selection-single-key nil
1802 "Non-nil means, fast tag selection exits after first change.
1803 When nil, you have to press RET to exit it.
1804 During fast tag selection, you can toggle this flag with `C-c'.
1805 This variable can also have the value `expert'. In this case, the window
1806 displaying the tags menu is not even shown, until you press C-c again."
1809 (const :tag
"No" nil
)
1810 (const :tag
"Yes" t
)
1811 (const :tag
"Expert" expert
)))
1813 (defvar org-fast-tag-selection-include-todo nil
1814 "Non-nil means, fast tags selection interface will also offer TODO states.
1815 This is an undocumented feature, you should not rely on it.")
1817 (defcustom org-tags-column -
80
1818 "The column to which tags should be indented in a headline.
1819 If this number is positive, it specifies the column. If it is negative,
1820 it means that the tags should be flushright to that column. For example,
1821 -80 works well for a normal 80 character screen."
1825 (defcustom org-auto-align-tags t
1826 "Non-nil means, realign tags after pro/demotion of TODO state change.
1827 These operations change the length of a headline and therefore shift
1828 the tags around. With this options turned on, after each such operation
1829 the tags are again aligned to `org-tags-column'."
1833 (defcustom org-use-tag-inheritance t
1834 "Non-nil means, tags in levels apply also for sublevels.
1835 When nil, only the tags directly given in a specific line apply there.
1836 If you turn off this option, you very likely want to turn on the
1837 companion option `org-tags-match-list-sublevels'.
1839 This may also be a list of tags that should be inherited, or a regexp that
1840 matches tags that should be inherited."
1843 (const :tag
"Not" nil
)
1844 (const :tag
"Always" t
)
1845 (repeat :tag
"Specific tags" (string :tag
"Tag"))
1846 (regexp :tag
"Tags matched by regexp")))
1848 (defun org-tag-inherit-p (tag)
1849 "Check if TAG is one that should be inherited."
1851 ((eq org-use-tag-inheritance t
) t
)
1852 ((not org-use-tag-inheritance
) nil
)
1853 ((stringp org-use-tag-inheritance
)
1854 (string-match org-use-tag-inheritance tag
))
1855 ((listp org-use-tag-inheritance
)
1856 (member tag org-use-tag-inheritance
))
1857 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
1859 (defcustom org-tags-match-list-sublevels nil
1860 "Non-nil means list also sublevels of headlines matching tag search.
1861 Because of tag inheritance (see variable `org-use-tag-inheritance'),
1862 the sublevels of a headline matching a tag search often also match
1863 the same search. Listing all of them can create very long lists.
1864 Setting this variable to nil causes subtrees of a match to be skipped.
1865 This option is off by default, because inheritance in on. If you turn
1866 inheritance off, you very likely want to turn this option on.
1868 As a special case, if the tag search is restricted to TODO items, the
1869 value of this variable is ignored and sublevels are always checked, to
1870 make sure all corresponding TODO items find their way into the list."
1874 (defvar org-tags-history nil
1875 "History of minibuffer reads for tags.")
1876 (defvar org-last-tags-completion-table nil
1877 "The last used completion table for tags.")
1878 (defvar org-after-tags-change-hook nil
1879 "Hook that is run after the tags in a line have changed.")
1881 (defgroup org-properties nil
1882 "Options concerning properties in Org-mode."
1883 :tag
"Org Properties"
1886 (defcustom org-property-format
"%-10s %s"
1887 "How property key/value pairs should be formatted by `indent-line'.
1888 When `indent-line' hits a property definition, it will format the line
1889 according to this format, mainly to make sure that the values are
1890 lined-up with respect to each other."
1891 :group
'org-properties
1894 (defcustom org-use-property-inheritance nil
1895 "Non-nil means, properties apply also for sublevels.
1897 This setting is chiefly used during property searches. Turning it on can
1898 cause significant overhead when doing a search, which is why it is not
1901 When nil, only the properties directly given in the current entry count.
1902 When t, every property is inherited. The value may also be a list of
1903 properties that should have inheritance, or a regular expression matching
1904 properties that should be inherited.
1906 However, note that some special properties use inheritance under special
1907 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
1908 and the properties ending in \"_ALL\" when they are used as descriptor
1909 for valid values of a property.
1911 Note for programmers:
1912 When querying an entry with `org-entry-get', you can control if inheritance
1913 should be used. By default, `org-entry-get' looks only at the local
1914 properties. You can request inheritance by setting the inherit argument
1915 to t (to force inheritance) or to `selective' (to respect the setting
1917 :group
'org-properties
1919 (const :tag
"Not" nil
)
1920 (const :tag
"Always" t
)
1921 (repeat :tag
"Specific properties" (string :tag
"Property"))
1922 (regexp :tag
"Properties matched by regexp")))
1924 (defun org-property-inherit-p (property)
1925 "Check if PROPERTY is one that should be inherited."
1927 ((eq org-use-property-inheritance t
) t
)
1928 ((not org-use-property-inheritance
) nil
)
1929 ((stringp org-use-property-inheritance
)
1930 (string-match org-use-property-inheritance property
))
1931 ((listp org-use-property-inheritance
)
1932 (member property org-use-property-inheritance
))
1933 (t (error "Invalid setting of `org-use-property-inheritance'"))))
1935 (defcustom org-columns-default-format
"%25ITEM %TODO %3PRIORITY %TAGS"
1936 "The default column format, if no other format has been defined.
1937 This variable can be set on the per-file basis by inserting a line
1939 #+COLUMNS: %25ITEM ....."
1940 :group
'org-properties
1943 (defcustom org-global-properties nil
1944 "List of property/value pairs that can be inherited by any entry.
1945 You can set buffer-local values for this by adding lines like
1947 #+PROPERTY: NAME VALUE"
1948 :group
'org-properties
1950 (cons (string :tag
"Property")
1951 (string :tag
"Value"))))
1953 (defvar org-local-properties nil
1954 "List of property/value pairs that can be inherited by any entry.
1955 Valid for the current buffer.
1956 This variable is populated from #+PROPERTY lines.")
1958 (defgroup org-agenda nil
1959 "Options concerning agenda views in Org-mode."
1963 (defvar org-category nil
1964 "Variable used by org files to set a category for agenda display.
1965 Such files should use a file variable to set it, for example
1967 # -*- mode: org; org-category: \"ELisp\"
1969 or contain a special line
1973 If the file does not specify a category, then file's base name
1975 (make-variable-buffer-local 'org-category
)
1977 (defcustom org-agenda-files nil
1978 "The files to be used for agenda display.
1979 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
1980 \\[org-remove-file]. You can also use customize to edit the list.
1982 If an entry is a directory, all files in that directory that are matched by
1983 `org-agenda-file-regexp' will be part of the file list.
1985 If the value of the variable is not a list but a single file name, then
1986 the list of agenda files is actually stored and maintained in that file, one
1987 agenda file per line."
1990 (repeat :tag
"List of files and directories" file
)
1991 (file :tag
"Store list in a file\n" :value
"~/.agenda_files")))
1993 (defcustom org-agenda-file-regexp
"\\`[^.].*\\.org\\'"
1994 "Regular expression to match files for `org-agenda-files'.
1995 If any element in the list in that variable contains a directory instead
1996 of a normal file, all files in that directory that are matched by this
1997 regular expression will be included."
2001 (defcustom org-agenda-text-search-extra-files nil
2002 "List of extra files to be searched by text search commands.
2003 These files will be search in addition to the agenda files bu the
2004 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
2005 Note that these files will only be searched for text search commands,
2006 not for the other agenda views like todo lists, tag earches or the weekly
2007 agenda. This variable is intended to list notes and possibly archive files
2008 that should also be searched by these two commands."
2010 :type
'(repeat file
))
2012 (if (fboundp 'defvaralias
)
2013 (defvaralias 'org-agenda-multi-occur-extra-files
2014 'org-agenda-text-search-extra-files
))
2016 (defcustom org-agenda-skip-unavailable-files nil
2017 "t means to just skip non-reachable files in `org-agenda-files'.
2018 Nil means to remove them, after a query, from the list."
2022 (defcustom org-calendar-to-agenda-key
[?c
]
2023 "The key to be installed in `calendar-mode-map' for switching to the agenda.
2024 The command `org-calendar-goto-agenda' will be bound to this key. The
2025 default is the character `c' because then `c' can be used to switch back and
2026 forth between agenda and calendar."
2030 (eval-after-load "calendar"
2031 '(org-defkey calendar-mode-map org-calendar-to-agenda-key
2032 'org-calendar-goto-agenda
))
2034 (defgroup org-latex nil
2035 "Options for embedding LaTeX code into Org-mode."
2039 (defcustom org-format-latex-options
2040 '(:foreground default
:background default
:scale
1.0
2041 :html-foreground
"Black" :html-background
"Transparent" :html-scale
1.0
2042 :matchers
("begin" "$" "$$" "\\(" "\\["))
2043 "Options for creating images from LaTeX fragments.
2044 This is a property list with the following properties:
2045 :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
2046 `default' means use the forground of the default face.
2047 :background the background color, or \"Transparent\".
2048 `default' means use the background of the default face.
2049 :scale a scaling factor for the size of the images
2050 :html-foreground, :html-background, :html-scale
2051 The same numbers for HTML export.
2052 :matchers a list indicating which matchers should be used to
2053 find LaTeX fragments. Valid members of this list are:
2054 \"begin\" find environments
2055 \"$\" find math expressions surrounded by $...$
2056 \"$$\" find math expressions surrounded by $$....$$
2057 \"\\(\" find math expressions surrounded by \\(...\\)
2058 \"\\ [\" find math expressions surrounded by \\ [...\\]"
2062 (defcustom org-format-latex-header
"\\documentclass{article}
2063 \\usepackage{fullpage} % do not remove
2064 \\usepackage{amssymb}
2065 \\usepackage[usenames]{color}
2066 \\usepackage{amsmath}
2067 \\usepackage{latexsym}
2068 \\usepackage[mathscr]{eucal}
2069 \\pagestyle{empty} % do not remove"
2070 "The document header used for processing LaTeX fragments."
2075 (defgroup org-font-lock nil
2076 "Font-lock settings for highlighting in Org-mode."
2077 :tag
"Org Font Lock"
2080 (defcustom org-level-color-stars-only nil
2081 "Non-nil means fontify only the stars in each headline.
2082 When nil, the entire headline is fontified.
2083 Changing it requires restart of `font-lock-mode' to become effective
2084 also in regions already fontified."
2085 :group
'org-font-lock
2088 (defcustom org-hide-leading-stars nil
2089 "Non-nil means, hide the first N-1 stars in a headline.
2090 This works by using the face `org-hide' for these stars. This
2091 face is white for a light background, and black for a dark
2092 background. You may have to customize the face `org-hide' to
2094 Changing it requires restart of `font-lock-mode' to become effective
2095 also in regions already fontified.
2096 You may also set this on a per-file basis by adding one of the following
2097 lines to the buffer:
2099 #+STARTUP: hidestars
2100 #+STARTUP: showstars"
2101 :group
'org-font-lock
2104 (defcustom org-fontify-done-headline nil
2105 "Non-nil means, change the face of a headline if it is marked DONE.
2106 Normally, only the TODO/DONE keyword indicates the state of a headline.
2107 When this is non-nil, the headline after the keyword is set to the
2108 `org-headline-done' as an additional indication."
2109 :group
'org-font-lock
2112 (defcustom org-fontify-emphasized-text t
2113 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
2114 Changing this variable requires a restart of Emacs to take effect."
2115 :group
'org-font-lock
2118 (defcustom org-highlight-latex-fragments-and-specials nil
2119 "Non-nil means, fontify what is treated specially by the exporters."
2120 :group
'org-font-lock
2123 (defcustom org-hide-emphasis-markers nil
2124 "Non-nil mean font-lock should hide the emphasis marker characters."
2125 :group
'org-font-lock
2128 (defvar org-emph-re nil
2129 "Regular expression for matching emphasis.")
2130 (defvar org-verbatim-re nil
2131 "Regular expression for matching verbatim text.")
2132 (defvar org-emphasis-regexp-components
) ; defined just below
2133 (defvar org-emphasis-alist
) ; defined just below
2134 (defun org-set-emph-re (var val
)
2135 "Set variable and compute the emphasis regular expression."
2137 (when (and (boundp 'org-emphasis-alist
)
2138 (boundp 'org-emphasis-regexp-components
)
2139 org-emphasis-alist org-emphasis-regexp-components
)
2140 (let* ((e org-emphasis-regexp-components
)
2146 (stacked (and nil
(nth 5 e
))) ; stacked is no longer allowed, forced to nil
2147 (body1 (concat body
"*?"))
2148 (markers (mapconcat 'car org-emphasis-alist
""))
2149 (vmarkers (mapconcat
2150 (lambda (x) (if (eq (nth 4 x
) 'verbatim
) (car x
) ""))
2151 org-emphasis-alist
"")))
2152 ;; make sure special characters appear at the right position in the class
2153 (if (string-match "\\^" markers
)
2154 (setq markers
(concat (replace-match "" t t markers
) "^")))
2155 (if (string-match "-" markers
)
2156 (setq markers
(concat (replace-match "" t t markers
) "-")))
2157 (if (string-match "\\^" vmarkers
)
2158 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "^")))
2159 (if (string-match "-" vmarkers
)
2160 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "-")))
2162 (setq body1
(concat body1
"\\(?:\n" body
"*?\\)\\{0,"
2163 (int-to-string nl
) "\\}")))
2166 (concat "\\([" pre
(if (and nil stacked
) markers
) "]\\|^\\)"
2168 "\\([" markers
"]\\)"
2171 "[^" border
(if (and nil stacked
) markers
) "]"
2173 "[^" border
(if (and nil stacked
) markers
) "]"
2176 "\\([" post
(if (and nil stacked
) markers
) "]\\|$\\)"))
2177 (setq org-verbatim-re
2178 (concat "\\([" pre
"]\\|^\\)"
2180 "\\([" vmarkers
"]\\)"
2188 "\\([" post
"]\\|$\\)")))))
2190 (defcustom org-emphasis-regexp-components
2191 '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
2192 "Components used to build the regular expression for emphasis.
2193 This is a list with 6 entries. Terminology: In an emphasis string
2194 like \" *strong word* \", we call the initial space PREMATCH, the final
2195 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
2196 and \"trong wor\" is the body. The different components in this variable
2197 specify what is allowed/forbidden in each part:
2199 pre Chars allowed as prematch. Beginning of line will be allowed too.
2200 post Chars allowed as postmatch. End of line will be allowed too.
2201 border The chars *forbidden* as border characters.
2202 body-regexp A regexp like \".\" to match a body character. Don't use
2203 non-shy groups here, and don't allow newline here.
2204 newline The maximum number of newlines allowed in an emphasis exp.
2206 Use customize to modify this, or restart Emacs after changing it."
2207 :group
'org-font-lock
2208 :set
'org-set-emph-re
2210 (sexp :tag
"Allowed chars in pre ")
2211 (sexp :tag
"Allowed chars in post ")
2212 (sexp :tag
"Forbidden chars in border ")
2213 (sexp :tag
"Regexp for body ")
2214 (integer :tag
"number of newlines allowed")
2215 (option (boolean :tag
"Stacking (DISABLED) "))))
2217 (defcustom org-emphasis-alist
2218 '(("*" bold
"<b>" "</b>")
2219 ("/" italic
"<i>" "</i>")
2220 ("_" underline
"<u>" "</u>")
2221 ("=" org-code
"<code>" "</code>" verbatim
)
2222 ("~" org-verbatim
"" "" verbatim
)
2223 ("+" (:strike-through t
) "<del>" "</del>")
2225 "Special syntax for emphasized text.
2226 Text starting and ending with a special character will be emphasized, for
2227 example *bold*, _underlined_ and /italic/. This variable sets the marker
2228 characters, the face to be used by font-lock for highlighting in Org-mode
2229 Emacs buffers, and the HTML tags to be used for this.
2230 Use customize to modify this, or restart Emacs after changing it."
2231 :group
'org-font-lock
2232 :set
'org-set-emph-re
2235 (string :tag
"Marker character")
2237 (face :tag
"Font-lock-face")
2238 (plist :tag
"Face property list"))
2239 (string :tag
"HTML start tag")
2240 (string :tag
"HTML end tag")
2241 (option (const verbatim
)))))
2243 ;;; Miscellaneous options
2245 (defgroup org-completion nil
2246 "Completion in Org-mode."
2247 :tag
"Org Completion"
2250 (defcustom org-completion-fallback-command
'hippie-expand
2251 "The expansion command called by \\[org-complete] in normal context.
2252 Normal means, no org-mode-specific context."
2253 :group
'org-completion
2258 (defgroup org-faces nil
2259 "Faces in Org-mode."
2261 :group
'org-font-lock
)
2264 '((((background light
)) (:foreground
"white"))
2265 (((background dark
)) (:foreground
"black")))
2266 "Face used to hide leading stars in headlines.
2267 The forground color of this face should be equal to the background
2268 color of the frame."
2271 (defface org-level-1
;; font-lock-function-name-face
2272 (org-compatible-face 'outline-1
2273 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Blue1"))
2274 (((class color
) (min-colors 88) (background dark
)) (:foreground
"LightSkyBlue"))
2275 (((class color
) (min-colors 16) (background light
)) (:foreground
"Blue"))
2276 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSkyBlue"))
2277 (((class color
) (min-colors 8)) (:foreground
"blue" :bold t
))
2279 "Face used for level 1 headlines."
2282 (defface org-level-2
;; font-lock-variable-name-face
2283 (org-compatible-face 'outline-2
2284 '((((class color
) (min-colors 16) (background light
)) (:foreground
"DarkGoldenrod"))
2285 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightGoldenrod"))
2286 (((class color
) (min-colors 8) (background light
)) (:foreground
"yellow"))
2287 (((class color
) (min-colors 8) (background dark
)) (:foreground
"yellow" :bold t
))
2289 "Face used for level 2 headlines."
2292 (defface org-level-3
;; font-lock-keyword-face
2293 (org-compatible-face 'outline-3
2294 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Purple"))
2295 (((class color
) (min-colors 88) (background dark
)) (:foreground
"Cyan1"))
2296 (((class color
) (min-colors 16) (background light
)) (:foreground
"Purple"))
2297 (((class color
) (min-colors 16) (background dark
)) (:foreground
"Cyan"))
2298 (((class color
) (min-colors 8) (background light
)) (:foreground
"purple" :bold t
))
2299 (((class color
) (min-colors 8) (background dark
)) (:foreground
"cyan" :bold t
))
2301 "Face used for level 3 headlines."
2304 (defface org-level-4
;; font-lock-comment-face
2305 (org-compatible-face 'outline-4
2306 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Firebrick"))
2307 (((class color
) (min-colors 88) (background dark
)) (:foreground
"chocolate1"))
2308 (((class color
) (min-colors 16) (background light
)) (:foreground
"red"))
2309 (((class color
) (min-colors 16) (background dark
)) (:foreground
"red1"))
2310 (((class color
) (min-colors 8) (background light
)) (:foreground
"red" :bold t
))
2311 (((class color
) (min-colors 8) (background dark
)) (:foreground
"red" :bold t
))
2313 "Face used for level 4 headlines."
2316 (defface org-level-5
;; font-lock-type-face
2317 (org-compatible-face 'outline-5
2318 '((((class color
) (min-colors 16) (background light
)) (:foreground
"ForestGreen"))
2319 (((class color
) (min-colors 16) (background dark
)) (:foreground
"PaleGreen"))
2320 (((class color
) (min-colors 8)) (:foreground
"green"))))
2321 "Face used for level 5 headlines."
2324 (defface org-level-6
;; font-lock-constant-face
2325 (org-compatible-face 'outline-6
2326 '((((class color
) (min-colors 16) (background light
)) (:foreground
"CadetBlue"))
2327 (((class color
) (min-colors 16) (background dark
)) (:foreground
"Aquamarine"))
2328 (((class color
) (min-colors 8)) (:foreground
"magenta"))))
2329 "Face used for level 6 headlines."
2332 (defface org-level-7
;; font-lock-builtin-face
2333 (org-compatible-face 'outline-7
2334 '((((class color
) (min-colors 16) (background light
)) (:foreground
"Orchid"))
2335 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSteelBlue"))
2336 (((class color
) (min-colors 8)) (:foreground
"blue"))))
2337 "Face used for level 7 headlines."
2340 (defface org-level-8
;; font-lock-string-face
2341 (org-compatible-face 'outline-8
2342 '((((class color
) (min-colors 16) (background light
)) (:foreground
"RosyBrown"))
2343 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSalmon"))
2344 (((class color
) (min-colors 8)) (:foreground
"green"))))
2345 "Face used for level 8 headlines."
2348 (defface org-special-keyword
;; font-lock-string-face
2349 (org-compatible-face nil
2350 '((((class color
) (min-colors 16) (background light
)) (:foreground
"RosyBrown"))
2351 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSalmon"))
2353 "Face used for special keywords."
2356 (defface org-drawer
;; font-lock-function-name-face
2357 (org-compatible-face nil
2358 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Blue1"))
2359 (((class color
) (min-colors 88) (background dark
)) (:foreground
"LightSkyBlue"))
2360 (((class color
) (min-colors 16) (background light
)) (:foreground
"Blue"))
2361 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSkyBlue"))
2362 (((class color
) (min-colors 8)) (:foreground
"blue" :bold t
))
2364 "Face used for drawers."
2367 (defface org-property-value nil
2368 "Face used for the value of a property."
2372 (org-compatible-face nil
2373 '((((class color
) (min-colors 16) (background light
))
2374 (:background
"grey90"))
2375 (((class color
) (min-colors 16) (background dark
))
2376 (:background
"grey30"))
2377 (((class color
) (min-colors 8))
2378 (:background
"cyan" :foreground
"black"))
2379 (t (:inverse-video t
))))
2380 "Face for column display of entry properties."
2383 (when (fboundp 'set-face-attribute
)
2384 ;; Make sure that a fixed-width face is used when we have a column table.
2385 (set-face-attribute 'org-column nil
2386 :height
(face-attribute 'default
:height
)
2387 :family
(face-attribute 'default
:family
)))
2389 (defface org-warning
2390 (org-compatible-face 'font-lock-warning-face
2391 '((((class color
) (min-colors 16) (background light
)) (:foreground
"Red1" :bold t
))
2392 (((class color
) (min-colors 16) (background dark
)) (:foreground
"Pink" :bold t
))
2393 (((class color
) (min-colors 8) (background light
)) (:foreground
"red" :bold t
))
2394 (((class color
) (min-colors 8) (background dark
)) (:foreground
"red" :bold t
))
2396 "Face for deadlines and TODO keywords."
2399 (defface org-archived
; similar to shadow
2400 (org-compatible-face 'shadow
2401 '((((class color grayscale
) (min-colors 88) (background light
))
2402 (:foreground
"grey50"))
2403 (((class color grayscale
) (min-colors 88) (background dark
))
2404 (:foreground
"grey70"))
2405 (((class color
) (min-colors 8) (background light
))
2406 (:foreground
"green"))
2407 (((class color
) (min-colors 8) (background dark
))
2408 (:foreground
"yellow"))))
2409 "Face for headline with the ARCHIVE tag."
2413 '((((class color
) (background light
)) (:foreground
"Purple" :underline t
))
2414 (((class color
) (background dark
)) (:foreground
"Cyan" :underline t
))
2419 (defface org-ellipsis
2420 '((((class color
) (background light
)) (:foreground
"DarkGoldenrod" :underline t
))
2421 (((class color
) (background dark
)) (:foreground
"LightGoldenrod" :underline t
))
2422 (t (:strike-through t
)))
2423 "Face for the ellipsis in folded text."
2427 '((((class color
) (background light
)) (:underline t
))
2428 (((class color
) (background dark
)) (:underline t
))
2434 '((((class color
) (background light
)) (:foreground
"Purple" :underline t
))
2435 (((class color
) (background dark
)) (:foreground
"Cyan" :underline t
))
2440 (defface org-sexp-date
2441 '((((class color
) (background light
)) (:foreground
"Purple"))
2442 (((class color
) (background dark
)) (:foreground
"Cyan"))
2452 (defface org-todo
; font-lock-warning-face
2453 (org-compatible-face nil
2454 '((((class color
) (min-colors 16) (background light
)) (:foreground
"Red1" :bold t
))
2455 (((class color
) (min-colors 16) (background dark
)) (:foreground
"Pink" :bold t
))
2456 (((class color
) (min-colors 8) (background light
)) (:foreground
"red" :bold t
))
2457 (((class color
) (min-colors 8) (background dark
)) (:foreground
"red" :bold t
))
2458 (t (:inverse-video t
:bold t
))))
2459 "Face for TODO keywords."
2462 (defface org-done
;; font-lock-type-face
2463 (org-compatible-face nil
2464 '((((class color
) (min-colors 16) (background light
)) (:foreground
"ForestGreen" :bold t
))
2465 (((class color
) (min-colors 16) (background dark
)) (:foreground
"PaleGreen" :bold t
))
2466 (((class color
) (min-colors 8)) (:foreground
"green"))
2468 "Face used for todo keywords that indicate DONE items."
2471 (defface org-headline-done
;; font-lock-string-face
2472 (org-compatible-face nil
2473 '((((class color
) (min-colors 16) (background light
)) (:foreground
"RosyBrown"))
2474 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSalmon"))
2475 (((class color
) (min-colors 8) (background light
)) (:bold nil
))))
2476 "Face used to indicate that a headline is DONE.
2477 This face is only used if `org-fontify-done-headline' is set. If applies
2478 to the part of the headline after the DONE keyword."
2481 (defcustom org-todo-keyword-faces nil
2482 "Faces for specific TODO keywords.
2483 This is a list of cons cells, with TODO keywords in the car
2484 and faces in the cdr. The face can be a symbol, or a property
2485 list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
2490 (string :tag
"keyword")
2491 (sexp :tag
"face"))))
2493 (defface org-table
;; font-lock-function-name-face
2494 (org-compatible-face nil
2495 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Blue1"))
2496 (((class color
) (min-colors 88) (background dark
)) (:foreground
"LightSkyBlue"))
2497 (((class color
) (min-colors 16) (background light
)) (:foreground
"Blue"))
2498 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSkyBlue"))
2499 (((class color
) (min-colors 8) (background light
)) (:foreground
"blue"))
2500 (((class color
) (min-colors 8) (background dark
)))))
2501 "Face used for tables."
2504 (defface org-formula
2505 (org-compatible-face nil
2506 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Firebrick"))
2507 (((class color
) (min-colors 88) (background dark
)) (:foreground
"chocolate1"))
2508 (((class color
) (min-colors 8) (background light
)) (:foreground
"red"))
2509 (((class color
) (min-colors 8) (background dark
)) (:foreground
"red"))
2510 (t (:bold t
:italic t
))))
2511 "Face for formulas."
2515 (org-compatible-face nil
2516 '((((class color grayscale
) (min-colors 88) (background light
))
2517 (:foreground
"grey50"))
2518 (((class color grayscale
) (min-colors 88) (background dark
))
2519 (:foreground
"grey70"))
2520 (((class color
) (min-colors 8) (background light
))
2521 (:foreground
"green"))
2522 (((class color
) (min-colors 8) (background dark
))
2523 (:foreground
"yellow"))))
2524 "Face for fixed-with text like code snippets."
2528 (defface org-verbatim
2529 (org-compatible-face nil
2530 '((((class color grayscale
) (min-colors 88) (background light
))
2531 (:foreground
"grey50" :underline t
))
2532 (((class color grayscale
) (min-colors 88) (background dark
))
2533 (:foreground
"grey70" :underline t
))
2534 (((class color
) (min-colors 8) (background light
))
2535 (:foreground
"green" :underline t
))
2536 (((class color
) (min-colors 8) (background dark
))
2537 (:foreground
"yellow" :underline t
))))
2538 "Face for fixed-with text like code snippets."
2542 (defface org-agenda-structure
;; font-lock-function-name-face
2543 (org-compatible-face nil
2544 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Blue1"))
2545 (((class color
) (min-colors 88) (background dark
)) (:foreground
"LightSkyBlue"))
2546 (((class color
) (min-colors 16) (background light
)) (:foreground
"Blue"))
2547 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSkyBlue"))
2548 (((class color
) (min-colors 8)) (:foreground
"blue" :bold t
))
2550 "Face used in agenda for captions and dates."
2553 (defface org-scheduled-today
2554 (org-compatible-face nil
2555 '((((class color
) (min-colors 88) (background light
)) (:foreground
"DarkGreen"))
2556 (((class color
) (min-colors 88) (background dark
)) (:foreground
"PaleGreen"))
2557 (((class color
) (min-colors 8)) (:foreground
"green"))
2558 (t (:bold t
:italic t
))))
2559 "Face for items scheduled for a certain day."
2562 (defface org-scheduled-previously
2563 (org-compatible-face nil
2564 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Firebrick"))
2565 (((class color
) (min-colors 88) (background dark
)) (:foreground
"chocolate1"))
2566 (((class color
) (min-colors 8) (background light
)) (:foreground
"red"))
2567 (((class color
) (min-colors 8) (background dark
)) (:foreground
"red" :bold t
))
2569 "Face for items scheduled previously, and not yet done."
2572 (defface org-upcoming-deadline
2573 (org-compatible-face nil
2574 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Firebrick"))
2575 (((class color
) (min-colors 88) (background dark
)) (:foreground
"chocolate1"))
2576 (((class color
) (min-colors 8) (background light
)) (:foreground
"red"))
2577 (((class color
) (min-colors 8) (background dark
)) (:foreground
"red" :bold t
))
2579 "Face for items scheduled previously, and not yet done."
2582 (defcustom org-agenda-deadline-faces
2583 '((1.0 . org-warning
)
2584 (0.5 . org-upcoming-deadline
)
2586 "Faces for showing deadlines in the agenda.
2587 This is a list of cons cells. The cdr of each cell is a face to be used,
2588 and it can also just be like '(:foreground \"yellow\").
2589 Each car is a fraction of the head-warning time that must have passed for
2590 this the face in the cdr to be used for display. The numbers must be
2591 given in descending order. The head-warning time is normally taken
2592 from `org-deadline-warning-days', but can also be specified in the deadline
2593 timestamp itself, like this:
2595 DEADLINE: <2007-08-13 Mon -8d>
2597 You may use d for days, w for weeks, m for months and y for years. Months
2598 and years will only be treated in an approximate fashion (30.4 days for a
2599 month and 365.24 days for a year)."
2601 :group
'org-agenda-daily
/weekly
2604 (number :tag
"Fraction of head-warning time passed")
2605 (sexp :tag
"Face"))))
2607 (defface org-agenda-restriction-lock
2608 (org-compatible-face nil
2609 '((((class color
) (min-colors 88) (background light
)) (:background
"yellow1"))
2610 (((class color
) (min-colors 88) (background dark
)) (:background
"skyblue4"))
2611 (((class color
) (min-colors 16) (background light
)) (:background
"yellow1"))
2612 (((class color
) (min-colors 16) (background dark
)) (:background
"skyblue4"))
2613 (((class color
) (min-colors 8)) (:background
"cyan" :foreground
"black"))
2614 (t (:inverse-video t
))))
2615 "Face for showing the agenda restriction lock."
2618 (defface org-time-grid
;; font-lock-variable-name-face
2619 (org-compatible-face nil
2620 '((((class color
) (min-colors 16) (background light
)) (:foreground
"DarkGoldenrod"))
2621 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightGoldenrod"))
2622 (((class color
) (min-colors 8)) (:foreground
"yellow" :weight light
))))
2623 "Face used for time grids."
2626 (defconst org-level-faces
2627 '(org-level-1 org-level-2 org-level-3 org-level-4
2628 org-level-5 org-level-6 org-level-7 org-level-8
2631 (defcustom org-n-level-faces
(length org-level-faces
)
2632 "The number of different faces to be used for headlines.
2633 Org-mode defines 8 different headline faces, so this can be at most 8.
2634 If it is less than 8, the level-1 face gets re-used for level N+1 etc."
2638 ;;; Functions and variables from ther packages
2639 ;; Declared here to avoid compiler warnings
2642 (defvar outline-mode-menu-heading
)
2643 (defvar outline-mode-menu-show
)
2644 (defvar outline-mode-menu-hide
)
2645 (defvar zmacs-regions
) ; XEmacs regions
2648 (defvar mark-active
)
2651 (declare-function calendar-absolute-from-iso
"cal-iso" (&optional date
))
2652 (declare-function calendar-forward-day
"cal-move" (arg))
2653 (declare-function calendar-goto-date
"cal-move" (date))
2654 (declare-function calendar-goto-today
"cal-move" ())
2655 (declare-function calendar-iso-from-absolute
"cal-iso" (&optional date
))
2656 (defvar calc-embedded-close-formula
)
2657 (defvar calc-embedded-open-formula
)
2658 (declare-function cdlatex-tab
"ext:cdlatex" ())
2659 (declare-function dired-get-filename
"dired" (&optional localp no-error-if-not-filep
))
2660 (defvar font-lock-unfontify-region-function
)
2661 (declare-function org-gnus-follow-link
"org-gnus" (&optional group article
))
2662 (declare-function org-agenda-skip
"org-agenda" ())
2663 (declare-function org-format-agenda-item
"org-agenda"
2664 (extra txt
&optional category tags dotime noprefix remove-re
))
2665 (declare-function org-agenda-new-marker
"org-agenda" (&optional pos
))
2666 (declare-function org-agenda-change-all-lines
"org-agenda"
2667 (newhead hdmarker
&optional fixface
))
2668 (declare-function org-agenda-set-restriction-lock
"org-agenda" (&optional type
))
2669 (declare-function org-agenda-maybe-redo
"org-agenda" ())
2670 (declare-function parse-time-string
"parse-time" (string))
2671 (declare-function remember
"remember" (&optional initial
))
2672 (declare-function remember-buffer-desc
"remember" ())
2673 (declare-function remember-finalize
"remember" ())
2674 (defvar remember-save-after-remembering
)
2675 (defvar remember-data-file
)
2676 (defvar remember-register
)
2677 (defvar remember-buffer
)
2678 (defvar remember-handler-functions
)
2679 (defvar remember-annotation-functions
)
2680 (defvar texmathp-why
)
2681 (declare-function speedbar-line-directory
"speedbar" (&optional depth
))
2682 (declare-function table--at-cell-p
"table" (position &optional object at-column
))
2684 (defvar w3m-current-url
)
2685 (defvar w3m-current-title
)
2687 (defvar org-latex-regexps
)
2689 ;;; Autoload and prepare some org modules
2691 ;; Some table stuff that needs to be defined here, because it is used
2692 ;; by the functions setting up org-mode or checking for table context.
2694 (defconst org-table-any-line-regexp
"^[ \t]*\\(|\\|\\+-[-+]\\)"
2695 "Detects an org-type or table-type table.")
2696 (defconst org-table-line-regexp
"^[ \t]*|"
2697 "Detects an org-type table line.")
2698 (defconst org-table-dataline-regexp
"^[ \t]*|[^-]"
2699 "Detects an org-type table line.")
2700 (defconst org-table-hline-regexp
"^[ \t]*|-"
2701 "Detects an org-type table hline.")
2702 (defconst org-table1-hline-regexp
"^[ \t]*\\+-[-+]"
2703 "Detects a table-type table hline.")
2704 (defconst org-table-any-border-regexp
"^[ \t]*[^|+ \t]"
2705 "Searching from within a table (any type) this finds the first line
2706 outside the table.")
2708 ;; Autoload the functions in org-table.el that are needed by functions here.
2711 (org-autoload "org-table"
2712 '(org-table-align org-table-begin org-table-blank-field
2713 org-table-convert org-table-convert-region org-table-copy-down
2714 org-table-copy-region org-table-create
2715 org-table-create-or-convert-from-region
2716 org-table-create-with-table.el org-table-current-dline
2717 org-table-cut-region org-table-delete-column org-table-edit-field
2718 org-table-edit-formulas org-table-end org-table-eval-formula
2719 org-table-export org-table-field-info
2720 org-table-get-stored-formulas org-table-goto-column
2721 org-table-hline-and-move org-table-import org-table-insert-column
2722 org-table-insert-hline org-table-insert-row org-table-iterate
2723 org-table-justify-field-maybe org-table-kill-row
2724 org-table-maybe-eval-formula org-table-maybe-recalculate-line
2725 org-table-move-column org-table-move-column-left
2726 org-table-move-column-right org-table-move-row
2727 org-table-move-row-down org-table-move-row-up
2728 org-table-next-field org-table-next-row org-table-paste-rectangle
2729 org-table-previous-field org-table-recalculate
2730 org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
2731 org-table-toggle-coordinate-overlays
2732 org-table-toggle-formula-debugger org-table-wrap-region
2733 orgtbl-mode turn-on-orgtbl
)))
2735 (defun org-at-table-p (&optional table-type
)
2736 "Return t if the cursor is inside an org-type table.
2737 If TABLE-TYPE is non-nil, also check for table.el-type tables."
2738 (if org-enable-table-editor
2740 (beginning-of-line 1)
2741 (looking-at (if table-type org-table-any-line-regexp
2742 org-table-line-regexp
)))
2744 (defsubst org-table-p
() (org-at-table-p))
2746 (defun org-at-table.el-p
()
2747 "Return t if and only if we are at a table.el table."
2748 (and (org-at-table-p 'any
)
2750 (goto-char (org-table-begin 'any
))
2751 (looking-at org-table1-hline-regexp
))))
2752 (defun org-table-recognize-table.el
()
2753 "If there is a table.el table nearby, recognize it and move into it."
2754 (if org-table-tab-recognizes-table.el
2755 (if (org-at-table.el-p
)
2757 (beginning-of-line 1)
2758 (if (looking-at org-table-dataline-regexp
)
2760 (if (looking-at org-table1-hline-regexp
)
2762 (beginning-of-line 2)
2763 (if (looking-at org-table-any-border-regexp
)
2764 (beginning-of-line -
1)))))
2765 (if (re-search-forward "|" (org-table-end t
) t
)
2768 (if (table--at-cell-p (point))
2770 (message "recognizing table.el table...")
2771 (table-recognize-table)
2772 (message "recognizing table.el table...done")))
2773 (error "This should not happen..."))
2778 (defun org-at-table-hline-p ()
2779 "Return t if the cursor is inside a hline in a table."
2780 (if org-enable-table-editor
2782 (beginning-of-line 1)
2783 (looking-at org-table-hline-regexp
))
2786 (defvar org-table-clean-did-remove-column nil
)
2788 (defun org-table-map-tables (function)
2789 "Apply FUNCTION to the start of all tables in the buffer."
2793 (goto-char (point-min))
2794 (while (re-search-forward org-table-any-line-regexp nil t
)
2795 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
2796 (beginning-of-line 1)
2797 (if (looking-at org-table-line-regexp
)
2798 (save-excursion (funcall function
)))
2799 (re-search-forward org-table-any-border-regexp nil
1))))
2800 (message "Mapping tables: done"))
2802 ;; Declare and autoload functions from org-exp.el
2804 (declare-function org-default-export-plist
"org-exp")
2805 (declare-function org-infile-export-plist
"org-exp")
2806 (declare-function org-get-current-options
"org-exp")
2808 (org-autoload "org-exp"
2809 '(org-export org-export-as-ascii org-export-visible
2810 org-insert-export-options-template org-export-as-html-and-open
2811 org-export-as-html-batch org-export-as-html-to-buffer
2812 org-replace-region-by-html org-export-region-as-html
2813 org-export-as-html org-export-icalendar-this-file
2814 org-export-icalendar-all-agenda-files
2815 org-export-icalendar-combine-agenda-files org-export-as-xoxo
)))
2817 ;; Autoload org-remember
2820 (org-autoload "org-remember"
2821 '(org-remember-insinuate org-remember-annotation
2822 org-remember-apply-template org-remember org-remember-handler
)))
2824 ;; Autoload org-clock.el
2826 (defvar org-clock-marker
(make-marker)
2827 "Marker recording the last clock-in.")
2832 '(org-clock-in org-clock-out org-clock-cancel
2833 org-clock-goto org-clock-sum org-clock-display
2834 org-remove-clock-overlays org-clock-report
2835 org-clocktable-shift org-dblock-write
:clocktable
2836 org-get-clocktable
)))
2838 (defun org-clock-update-time-maybe ()
2839 "If this is a CLOCK line, update it and return t.
2840 Otherwise, return nil."
2843 (beginning-of-line 1)
2844 (skip-chars-forward " \t")
2845 (when (looking-at org-clock-string
)
2846 (let ((re (concat "[ \t]*" org-clock-string
2847 " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
2848 "\\([ \t]*=>.*\\)?"))
2850 (if (not (looking-at re
))
2852 (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
2854 (setq ts
(match-string 1)
2855 te
(match-string 2))
2856 (setq s
(- (time-to-seconds
2857 (apply 'encode-time
(org-parse-time-string te
)))
2859 (apply 'encode-time
(org-parse-time-string ts
))))
2860 h
(floor (/ s
3600))
2864 (insert " => " (format "%2d:%02d" h m
))
2867 (defun org-check-running-clock ()
2868 "Check if the current buffer contains the running clock.
2869 If yes, offer to stop it and to save the buffer with the changes."
2870 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
2871 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
2874 (when (y-or-n-p "Save changed buffer?")
2877 (defun org-clocktable-try-shift (dir n
)
2878 "Check if this line starts a clock table, if yes, shift the time block."
2879 (when (org-match-line "#\\+BEGIN: clocktable\\>")
2880 (org-clocktable-shift dir n
)))
2882 ;;; Variables for pre-computed regular expressions, all buffer local
2884 (defvar org-drawer-regexp nil
2885 "Matches first line of a hidden block.")
2886 (make-variable-buffer-local 'org-drawer-regexp
)
2887 (defvar org-todo-regexp nil
2888 "Matches any of the TODO state keywords.")
2889 (make-variable-buffer-local 'org-todo-regexp
)
2890 (defvar org-not-done-regexp nil
2891 "Matches any of the TODO state keywords except the last one.")
2892 (make-variable-buffer-local 'org-not-done-regexp
)
2893 (defvar org-todo-line-regexp nil
2894 "Matches a headline and puts TODO state into group 2 if present.")
2895 (make-variable-buffer-local 'org-todo-line-regexp
)
2896 (defvar org-complex-heading-regexp nil
2897 "Matches a headline and puts everything into groups:
2899 group 2: The todo keyword, maybe
2900 group 3: Priority cookie
2901 group 4: True headline
2903 (make-variable-buffer-local 'org-complex-heading-regexp
)
2904 (defvar org-todo-line-tags-regexp nil
2905 "Matches a headline and puts TODO state into group 2 if present.
2906 Also put tags into group 4 if tags are present.")
2907 (make-variable-buffer-local 'org-todo-line-tags-regexp
)
2908 (defvar org-nl-done-regexp nil
2909 "Matches newline followed by a headline with the DONE keyword.")
2910 (make-variable-buffer-local 'org-nl-done-regexp
)
2911 (defvar org-looking-at-done-regexp nil
2912 "Matches the DONE keyword a point.")
2913 (make-variable-buffer-local 'org-looking-at-done-regexp
)
2914 (defvar org-ds-keyword-length
12
2915 "Maximum length of the Deadline and SCHEDULED keywords.")
2916 (make-variable-buffer-local 'org-ds-keyword-length
)
2917 (defvar org-deadline-regexp nil
2918 "Matches the DEADLINE keyword.")
2919 (make-variable-buffer-local 'org-deadline-regexp
)
2920 (defvar org-deadline-time-regexp nil
2921 "Matches the DEADLINE keyword together with a time stamp.")
2922 (make-variable-buffer-local 'org-deadline-time-regexp
)
2923 (defvar org-deadline-line-regexp nil
2924 "Matches the DEADLINE keyword and the rest of the line.")
2925 (make-variable-buffer-local 'org-deadline-line-regexp
)
2926 (defvar org-scheduled-regexp nil
2927 "Matches the SCHEDULED keyword.")
2928 (make-variable-buffer-local 'org-scheduled-regexp
)
2929 (defvar org-scheduled-time-regexp nil
2930 "Matches the SCHEDULED keyword together with a time stamp.")
2931 (make-variable-buffer-local 'org-scheduled-time-regexp
)
2932 (defvar org-closed-time-regexp nil
2933 "Matches the CLOSED keyword together with a time stamp.")
2934 (make-variable-buffer-local 'org-closed-time-regexp
)
2936 (defvar org-keyword-time-regexp nil
2937 "Matches any of the 4 keywords, together with the time stamp.")
2938 (make-variable-buffer-local 'org-keyword-time-regexp
)
2939 (defvar org-keyword-time-not-clock-regexp nil
2940 "Matches any of the 3 keywords, together with the time stamp.")
2941 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp
)
2942 (defvar org-maybe-keyword-time-regexp nil
2943 "Matches a timestamp, possibly preceeded by a keyword.")
2944 (make-variable-buffer-local 'org-maybe-keyword-time-regexp
)
2945 (defvar org-planning-or-clock-line-re nil
2946 "Matches a line with planning or clock info.")
2947 (make-variable-buffer-local 'org-planning-or-clock-line-re
)
2949 (defconst org-plain-time-of-day-regexp
2952 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
2955 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
2957 "Regular expression to match a plain time or time range.
2958 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
2959 groups carry important information:
2961 1 the first time, range or not
2962 8 the second time, if it is a range.")
2964 (defconst org-plain-time-extension-regexp
2967 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
2968 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
2969 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
2970 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
2971 groups carry important information:
2974 9 minutes of duration")
2976 (defconst org-stamp-time-of-day-regexp
2978 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
2979 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
2981 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
2982 "Regular expression to match a timestamp time or time range.
2983 After a match, the following groups carry important information:
2985 1 date plus weekday, for backreferencing to make sure both times on same day
2986 2 the first time, range or not
2987 4 the second time, if it is a range.")
2989 (defconst org-startup-options
2990 '(("fold" org-startup-folded t
)
2991 ("overview" org-startup-folded t
)
2992 ("nofold" org-startup-folded nil
)
2993 ("showall" org-startup-folded nil
)
2994 ("content" org-startup-folded content
)
2995 ("hidestars" org-hide-leading-stars t
)
2996 ("showstars" org-hide-leading-stars nil
)
2997 ("odd" org-odd-levels-only t
)
2998 ("oddeven" org-odd-levels-only nil
)
2999 ("align" org-startup-align-all-tables t
)
3000 ("noalign" org-startup-align-all-tables nil
)
3001 ("customtime" org-display-custom-times t
)
3002 ("logdone" org-log-done time
)
3003 ("lognotedone" org-log-done note
)
3004 ("nologdone" org-log-done nil
)
3005 ("lognoteclock-out" org-log-note-clock-out t
)
3006 ("nolognoteclock-out" org-log-note-clock-out nil
)
3007 ("logrepeat" org-log-repeat state
)
3008 ("lognoterepeat" org-log-repeat note
)
3009 ("nologrepeat" org-log-repeat nil
)
3010 ("constcgs" constants-unit-system cgs
)
3011 ("constSI" constants-unit-system SI
))
3012 "Variable associated with STARTUP options for org-mode.
3013 Each element is a list of three items: The startup options as written
3014 in the #+STARTUP line, the corresponding variable, and the value to
3015 set this variable to if the option is found. An optional forth element PUSH
3016 means to push this value onto the list in the variable.")
3018 (defun org-set-regexps-and-options ()
3019 "Precompute regular expressions for current buffer."
3021 (org-set-local 'org-todo-kwd-alist nil
)
3022 (org-set-local 'org-todo-key-alist nil
)
3023 (org-set-local 'org-todo-key-trigger nil
)
3024 (org-set-local 'org-todo-keywords-1 nil
)
3025 (org-set-local 'org-done-keywords nil
)
3026 (org-set-local 'org-todo-heads nil
)
3027 (org-set-local 'org-todo-sets nil
)
3028 (org-set-local 'org-todo-log-states nil
)
3029 (let ((re (org-make-options-regexp
3030 '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
3031 "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
3032 "CONSTANTS" "PROPERTY" "DRAWERS")))
3034 kwds kws0 kwsa key log value cat arch tags const links hw dws
3035 tail sep kws1 prio props drawers
)
3039 (goto-char (point-min))
3040 (while (re-search-forward re nil t
)
3041 (setq key
(match-string 1) value
(org-match-string-no-properties 2))
3043 ((equal key
"CATEGORY")
3044 (if (string-match "[ \t]+$" value
)
3045 (setq value
(replace-match "" t t value
)))
3047 ((member key
'("SEQ_TODO" "TODO"))
3048 (push (cons 'sequence
(org-split-string value splitre
)) kwds
))
3049 ((equal key
"TYP_TODO")
3050 (push (cons 'type
(org-split-string value splitre
)) kwds
))
3052 (setq tags
(append tags
(org-split-string value splitre
))))
3053 ((equal key
"COLUMNS")
3054 (org-set-local 'org-columns-default-format value
))
3056 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value
)
3057 (push (cons (match-string 1 value
)
3058 (org-trim (match-string 2 value
)))
3060 ((equal key
"PRIORITIES")
3061 (setq prio
(org-split-string value
" +")))
3062 ((equal key
"PROPERTY")
3063 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
3064 (push (cons (match-string 1 value
) (match-string 2 value
))
3066 ((equal key
"DRAWERS")
3067 (setq drawers
(org-split-string value splitre
)))
3068 ((equal key
"CONSTANTS")
3069 (setq const
(append const
(org-split-string value splitre
))))
3070 ((equal key
"STARTUP")
3071 (let ((opts (org-split-string value splitre
))
3073 (while (setq l
(pop opts
))
3074 (when (setq l
(assoc l org-startup-options
))
3075 (setq var
(nth 1 l
) val
(nth 2 l
))
3077 (set (make-local-variable var
) val
)
3078 (if (not (listp (symbol-value var
)))
3079 (set (make-local-variable var
) nil
))
3080 (set (make-local-variable var
) (symbol-value var
))
3081 (add-to-list var val
))))))
3082 ((equal key
"ARCHIVE")
3083 (string-match " *$" value
)
3084 (setq arch
(replace-match "" t t value
))
3085 (remove-text-properties 0 (length arch
)
3086 '(face t fontified t
) arch
)))
3089 (org-set-local 'org-category
(intern cat
))
3090 (push (cons "CATEGORY" cat
) props
))
3092 (if (< (length prio
) 3) (setq prio
'("A" "C" "B")))
3093 (setq prio
(mapcar 'string-to-char prio
))
3094 (org-set-local 'org-highest-priority
(nth 0 prio
))
3095 (org-set-local 'org-lowest-priority
(nth 1 prio
))
3096 (org-set-local 'org-default-priority
(nth 2 prio
)))
3097 (and props
(org-set-local 'org-local-properties
(nreverse props
)))
3098 (and drawers
(org-set-local 'org-drawers drawers
))
3099 (and arch
(org-set-local 'org-archive-location arch
))
3100 (and links
(setq org-link-abbrev-alist-local
(nreverse links
)))
3101 ;; Process the TODO keywords
3103 ;; Use the global values as if they had been given locally.
3104 (setq kwds
(default-value 'org-todo-keywords
))
3105 (if (stringp (car kwds
))
3106 (setq kwds
(list (cons org-todo-interpretation
3107 (default-value 'org-todo-keywords
)))))
3108 (setq kwds
(reverse kwds
)))
3109 (setq kwds
(nreverse kwds
))
3111 (while (setq kws
(pop kwds
))
3112 (setq inter
(pop kws
) sep
(member "|" kws
)
3113 kws0
(delete "|" (copy-sequence kws
))
3118 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x
)
3120 (setq kw
(match-string 1 x
)
3121 key
(and (match-end 2) (match-string 2 x
))
3122 log
(org-extract-log-state-settings x
))
3123 (push (cons kw
(and key
(string-to-char key
))) kwsa
)
3124 (and log
(push log org-todo-log-states
))
3126 (error "Invalid TODO keyword %s" x
)))
3128 kwsa
(if kwsa
(append '((:startgroup
))
3132 dws
(if sep
(org-remove-keyword-keys (cdr sep
)) (last kws1
))
3133 tail
(list inter hw
(car dws
) (org-last dws
)))
3134 (add-to-list 'org-todo-heads hw
'append
)
3135 (push kws1 org-todo-sets
)
3136 (setq org-done-keywords
(append org-done-keywords dws nil
))
3137 (setq org-todo-key-alist
(append org-todo-key-alist kwsa
))
3138 (mapc (lambda (x) (push (cons x tail
) org-todo-kwd-alist
)) kws1
)
3139 (setq org-todo-keywords-1
(append org-todo-keywords-1 kws1 nil
)))
3140 (setq org-todo-sets
(nreverse org-todo-sets
)
3141 org-todo-kwd-alist
(nreverse org-todo-kwd-alist
)
3142 org-todo-key-trigger
(delq nil
(mapcar 'cdr org-todo-key-alist
))
3143 org-todo-key-alist
(org-assign-fast-keys org-todo-key-alist
)))
3144 ;; Process the constants
3147 (while (setq e
(pop const
))
3148 (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e
)
3149 (push (cons (match-string 1 e
) (match-string 2 e
)) cst
)))
3150 (setq org-table-formula-constants-local cst
)))
3152 ;; Process the tags.
3155 (while (setq e
(pop tags
))
3157 ((equal e
"{") (push '(:startgroup
) tgs
))
3158 ((equal e
"}") (push '(:endgroup
) tgs
))
3159 ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e
)
3160 (push (cons (match-string 1 e
)
3161 (string-to-char (match-string 2 e
)))
3163 (t (push (list e
) tgs
))))
3164 (org-set-local 'org-tag-alist nil
)
3165 (while (setq e
(pop tgs
))
3166 (or (and (stringp (car e
))
3167 (assoc (car e
) org-tag-alist
))
3168 (push e org-tag-alist
))))))
3170 ;; Compute the regular expressions and other local variables
3171 (if (not org-done-keywords
)
3172 (setq org-done-keywords
(list (org-last org-todo-keywords-1
))))
3173 (setq org-ds-keyword-length
(+ 2 (max (length org-deadline-string
)
3174 (length org-scheduled-string
)))
3176 (concat "^[ \t]*:\\("
3177 (mapconcat 'regexp-quote org-drawers
"\\|")
3179 org-not-done-keywords
3180 (org-delete-all org-done-keywords
(copy-sequence org-todo-keywords-1
))
3182 (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
3186 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
3188 org-todo-line-regexp
3189 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3190 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
3191 "\\)\\>\\)?[ \t]*\\(.*\\)")
3192 org-complex-heading-regexp
3193 (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
3194 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
3195 "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
3196 "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
3198 (concat "\n\\*+[ \t]+"
3199 "\\(?:" (mapconcat 'regexp-quote org-done-keywords
"\\|")
3201 org-todo-line-tags-regexp
3202 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3203 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
3205 "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
3206 org-looking-at-done-regexp
3208 (mapconcat 'regexp-quote org-done-keywords
"\\|") "\\)"
3210 org-deadline-regexp
(concat "\\<" org-deadline-string
)
3211 org-deadline-time-regexp
3212 (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>")
3213 org-deadline-line-regexp
3214 (concat "\\<\\(" org-deadline-string
"\\).*")
3215 org-scheduled-regexp
3216 (concat "\\<" org-scheduled-string
)
3217 org-scheduled-time-regexp
3218 (concat "\\<" org-scheduled-string
" *<\\([^>]+\\)>")
3219 org-closed-time-regexp
3220 (concat "\\<" org-closed-string
" *\\[\\([^]]+\\)\\]")
3221 org-keyword-time-regexp
3222 (concat "\\<\\(" org-scheduled-string
3223 "\\|" org-deadline-string
3224 "\\|" org-closed-string
3225 "\\|" org-clock-string
"\\)"
3226 " *[[<]\\([^]>]+\\)[]>]")
3227 org-keyword-time-not-clock-regexp
3228 (concat "\\<\\(" org-scheduled-string
3229 "\\|" org-deadline-string
3230 "\\|" org-closed-string
3232 " *[[<]\\([^]>]+\\)[]>]")
3233 org-maybe-keyword-time-regexp
3234 (concat "\\(\\<\\(" org-scheduled-string
3235 "\\|" org-deadline-string
3236 "\\|" org-closed-string
3237 "\\|" org-clock-string
"\\)\\)?"
3238 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
3239 org-planning-or-clock-line-re
3240 (concat "\\(?:^[ \t]*\\(" org-scheduled-string
3241 "\\|" org-deadline-string
3242 "\\|" org-closed-string
"\\|" org-clock-string
3245 (org-compute-latex-and-specials-regexp)
3246 (org-set-font-lock-defaults)))
3248 (defun org-extract-log-state-settings (x)
3249 "Extract the log state setting from a TODO keyword string.
3250 This will extract info from a string like \"WAIT(w@/!)\"."
3251 (let (kw key log1 log2
)
3252 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x
)
3253 (setq kw
(match-string 1 x
)
3254 key
(and (match-end 2) (match-string 2 x
))
3255 log1
(and (match-end 3) (match-string 3 x
))
3256 log2
(and (match-end 4) (match-string 4 x
)))
3259 (and log1
(if (equal log1
"!") 'time
'note
))
3260 (and log2
(if (equal log2
"!") 'time
'note
)))))))
3262 (defun org-remove-keyword-keys (list)
3263 "Remove a pair of parenthesis at the end of each string in LIST."
3265 (if (string-match "(.*)$" x
)
3266 (substring x
0 (match-beginning 0))
3270 ;; FIXME: this could be done much better, using second characters etc.
3271 (defun org-assign-fast-keys (alist)
3272 "Assign fast keys to a keyword-key alist.
3273 Respect keys that are already there."
3274 (let (new e k c c1 c2
(char ?a
))
3275 (while (setq e
(pop alist
))
3277 ((equal e
'(:startgroup
)) (push e new
))
3278 ((equal e
'(:endgroup
)) (push e new
))
3280 (setq k
(car e
) c2 nil
)
3283 ;; automatically assign a character.
3284 (setq c1
(string-to-char
3285 (downcase (substring
3286 k
(if (= (string-to-char k
) ?
@) 1 0)))))
3287 (if (or (rassoc c1 new
) (rassoc c1 alist
))
3288 (while (or (rassoc char new
) (rassoc char alist
))
3289 (setq char
(1+ char
)))
3291 (setq c
(or c2 char
)))
3292 (push (cons k c
) new
))))
3295 ;;; Some variables used in various places
3297 (defvar org-window-configuration nil
3298 "Used in various places to store a window configuration.")
3299 (defvar org-finish-function nil
3300 "Function to be called when `C-c C-c' is used.
3301 This is for getting out of special buffers like remember.")
3304 ;; FIXME: Occasionally check by commenting these, to make sure
3305 ;; no other functions uses these, forgetting to let-bind them.
3310 (defvar description
)
3312 ;; Defined somewhere in this file, but used before definition.
3313 (defvar org-html-entities
)
3314 (defvar org-struct-menu
)
3315 (defvar org-org-menu
)
3316 (defvar org-tbl-menu
)
3317 (defvar org-agenda-keymap
)
3319 ;;;; Define the Org-mode
3321 (if (and (not (keymapp outline-mode-map
)) (featurep 'allout
))
3322 (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."))
3325 ;; We use a before-change function to check if a table might need
3327 (defvar org-table-may-need-update t
3328 "Indicates that a table might need an update.
3329 This variable is set by `org-before-change-function'.
3330 `org-table-align' sets it back to nil.")
3331 (defun org-before-change-function (beg end
)
3332 "Every change indicates that a table might need an update."
3333 (setq org-table-may-need-update t
))
3334 (defvar org-mode-map
)
3335 (defvar org-mode-hook nil
)
3336 (defvar org-inhibit-startup nil
) ; Dynamically-scoped param.
3337 (defvar org-agenda-keep-modes nil
) ; Dynamically-scoped param.
3338 (defvar org-table-buffer-is-an nil
)
3339 (defconst org-outline-regexp
"\\*+ ")
3342 (define-derived-mode org-mode outline-mode
"Org"
3343 "Outline-based notes management and organizer, alias
3344 \"Carsten's outline-mode for keeping track of everything.\"
3346 Org-mode develops organizational tasks around a NOTES file which
3347 contains information about projects as plain text. Org-mode is
3348 implemented on top of outline-mode, which is ideal to keep the content
3349 of large files well structured. It supports ToDo items, deadlines and
3350 time stamps, which magically appear in the diary listing of the Emacs
3351 calendar. Tables are easily created with a built-in table editor.
3352 Plain text URL-like links connect to websites, emails (VM), Usenet
3353 messages (Gnus), BBDB entries, and any files related to the project.
3354 For printing and sharing of notes, an Org-mode file (or a part of it)
3355 can be exported as a structured ASCII or HTML file.
3357 The following commands are available:
3361 ;; Get rid of Outline menus, they are not needed
3362 ;; Need to do this here because define-derived-mode sets up
3363 ;; the keymap so late. Still, it is a waste to call this each time
3364 ;; we switch another buffer into org-mode.
3365 (if (featurep 'xemacs
)
3366 (when (boundp 'outline-mode-menu-heading
)
3367 ;; Assume this is Greg's port, it used easymenu
3368 (easy-menu-remove outline-mode-menu-heading
)
3369 (easy-menu-remove outline-mode-menu-show
)
3370 (easy-menu-remove outline-mode-menu-hide
))
3371 (define-key org-mode-map
[menu-bar headings
] 'undefined
)
3372 (define-key org-mode-map
[menu-bar hide
] 'undefined
)
3373 (define-key org-mode-map
[menu-bar show
] 'undefined
))
3375 (org-load-modules-maybe)
3376 (easy-menu-add org-org-menu
)
3377 (easy-menu-add org-tbl-menu
)
3378 (org-install-agenda-files-menu)
3379 (if org-descriptive-links
(org-add-to-invisibility-spec '(org-link)))
3380 (org-add-to-invisibility-spec '(org-cwidth))
3381 (when (featurep 'xemacs
)
3382 (org-set-local 'line-move-ignore-invisible t
))
3383 (org-set-local 'outline-regexp org-outline-regexp
)
3384 (org-set-local 'outline-level
'org-outline-level
)
3385 (when (and org-ellipsis
3386 (fboundp 'set-display-table-slot
) (boundp 'buffer-display-table
)
3387 (fboundp 'make-glyph-code
))
3388 (unless org-display-table
3389 (setq org-display-table
(make-display-table)))
3390 (set-display-table-slot
3393 (lambda (c) (make-glyph-code c
(and (not (stringp org-ellipsis
))
3395 (if (stringp org-ellipsis
) org-ellipsis
"..."))))
3396 (setq buffer-display-table org-display-table
))
3397 (org-set-regexps-and-options)
3399 (org-set-local 'calc-embedded-open-mode
"# ")
3400 (modify-syntax-entry ?
# "<")
3401 (modify-syntax-entry ?
@ "w")
3402 (if org-startup-truncated
(setq truncate-lines t
))
3403 (org-set-local 'font-lock-unfontify-region-function
3404 'org-unfontify-region
)
3405 ;; Activate before-change-function
3406 (org-set-local 'org-table-may-need-update t
)
3407 (org-add-hook 'before-change-functions
'org-before-change-function nil
3409 ;; Check for running clock before killing a buffer
3410 (org-add-hook 'kill-buffer-hook
'org-check-running-clock nil
'local
)
3411 ;; Paragraphs and auto-filling
3412 (org-set-autofill-regexps)
3413 (setq indent-line-function
'org-indent-line-function
)
3414 (org-update-radio-target-regexp)
3416 ;; Comment characters
3417 ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
3418 (org-set-local 'comment-padding
" ")
3420 ;; Align options lines
3422 'align-mode-rules-list
3423 '((org-in-buffer-settings
3424 (regexp .
"^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
3425 (modes .
'(org-mode)))))
3428 (org-set-local 'imenu-create-index-function
3429 'org-imenu-get-tree
)
3431 ;; Make isearch reveal context
3432 (if (or (featurep 'xemacs
)
3433 (not (boundp 'outline-isearch-open-invisible-function
)))
3434 ;; Emacs 21 and XEmacs make use of the hook
3435 (org-add-hook 'isearch-mode-end-hook
'org-isearch-end
'append
'local
)
3436 ;; Emacs 22 deals with this through a special variable
3437 (org-set-local 'outline-isearch-open-invisible-function
3438 (lambda (&rest ignore
) (org-show-context 'isearch
))))
3440 ;; If empty file that did not turn on org-mode automatically, make it to.
3441 (if (and org-insert-mode-line-in-empty-file
3443 (= (point-min) (point-max)))
3444 (insert "# -*- mode: org -*-\n\n"))
3446 (unless org-inhibit-startup
3447 (when org-startup-align-all-tables
3448 (let ((bmp (buffer-modified-p)))
3449 (org-table-map-tables 'org-table-align
)
3450 (set-buffer-modified-p bmp
)))
3451 (org-cycle-hide-drawers 'all
)
3453 ((eq org-startup-folded t
)
3455 ((eq org-startup-folded
'content
)
3456 (let ((this-command 'org-cycle
) (last-command 'org-cycle
))
3457 (org-cycle '(4)) (org-cycle '(4)))))))
3459 (put 'org-mode
'flyspell-mode-predicate
'org-mode-flyspell-verify
)
3461 (defun org-current-time ()
3462 "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
3463 (if (> (car org-time-stamp-rounding-minutes
) 1)
3464 (let ((r (car org-time-stamp-rounding-minutes
))
3465 (time (decode-time)))
3467 (append (list 0 (* r
(floor (+ .5 (/ (float (nth 1 time
)) r
)))))
3471 ;;;; Font-Lock stuff, including the activators
3473 (defvar org-mouse-map
(make-sparse-keymap))
3474 (org-defkey org-mouse-map
3475 (if (featurep 'xemacs
) [button2] [mouse-2]) 'org-open-at-mouse)
3476 (org-defkey org-mouse-map
3477 (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
3478 (when org-mouse-1-follows-link
3479 (org-defkey org-mouse-map [follow-link] 'mouse-face))
3480 (when org-tab-follows-link
3481 (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
3482 (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
3483 (when org-return-follows-link
3484 (org-defkey org-mouse-map [(return)] 'org-open-at-point)
3485 (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
3487 (require 'font-lock)
3489 (defconst org-non-link-chars "]\t\n\r<>")
3490 (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
3492 (defvar org-link-re-with-space nil
3493 "Matches a link with spaces, optional angular brackets around it.")
3494 (defvar org-link-re-with-space2 nil
3495 "Matches a link with spaces, optional angular brackets around it.")
3496 (defvar org-angle-link-re nil
3497 "Matches link with angular brackets, spaces are allowed.")
3498 (defvar org-plain-link-re nil
3499 "Matches plain link, without spaces.")
3500 (defvar org-bracket-link-regexp nil
3501 "Matches a link in double brackets.")
3502 (defvar org-bracket-link-analytic-regexp nil
3503 "Regular expression used to analyze links.
3504 Here is what the match groups contain after a match:
3510 (defvar org-any-link-re nil
3511 "Regular expression matching any link.")
3513 (defun org-make-link-regexps ()
3514 "Update the link regular expressions.
3515 This should be called after the variable `org-link-types' has changed."
3516 (setq org-link-re-with-space
3518 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3519 "\\([^" org-non-link-chars " ]"
3520 "[^" org-non-link-chars "]*"
3521 "[^" org-non-link-chars " ]\\)>?")
3522 org-link-re-with-space2
3524 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3525 "\\([^" org-non-link-chars " ]"
3527 "[^" org-non-link-chars " ]\\)>?")
3530 "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3531 "\\([^" org-non-link-chars " ]"
3532 "[^" org-non-link-chars "]*"
3536 "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3537 "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
3538 org-bracket-link-regexp
3539 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
3540 org-bracket-link-analytic-regexp
3543 "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
3546 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
3549 (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
3550 org-angle-link-re "\\)\\|\\("
3551 org-plain-link-re "\\)")))
3553 (org-make-link-regexps)
3555 (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
3556 "Regular expression for fast time stamp matching.")
3557 (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
3558 "Regular expression for fast time stamp matching.")
3559 (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
3560 "Regular expression matching time strings for analysis.
3561 This one does not require the space after the date, so it can be used
3562 on a string that terminates immediately after the date.")
3563 (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
3564 "Regular expression matching time strings for analysis.")
3565 (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
3566 "Regular expression matching time stamps, with groups.")
3567 (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
3568 "Regular expression matching time stamps (also [..]), with groups.")
3569 (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
3570 "Regular expression matching a time stamp range.")
3571 (defconst org-tr-regexp-both
3572 (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
3573 "Regular expression matching a time stamp range.")
3574 (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
3575 org-ts-regexp "\\)?")
3576 "Regular expression matching a time stamp or time stamp range.")
3577 (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
3578 org-ts-regexp-both "\\)?")
3579 "Regular expression matching a time stamp or time stamp range.
3580 The time stamps may be either active or inactive.")
3582 (defvar org-emph-face nil)
3584 (defun org-do-emphasis-faces (limit)
3585 "Run through the buffer and add overlays to links."
3587 (while (and (not rtn) (re-search-forward org-emph-re limit t))
3588 (if (not (= (char-after (match-beginning 3))
3589 (char-after (match-beginning 4))))
3592 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
3594 (nth 1 (assoc (match-string 3)
3595 org-emphasis-alist)))
3596 (add-text-properties (match-beginning 2) (match-end 2)
3597 '(font-lock-multiline t))
3598 (when org-hide-emphasis-markers
3599 (add-text-properties (match-end 4) (match-beginning 5)
3600 '(invisible org-link))
3601 (add-text-properties (match-beginning 3) (match-end 3)
3602 '(invisible org-link)))))
3606 (defun org-emphasize (&optional char)
3607 "Insert or change an emphasis, i.e. a font like bold or italic.
3608 If there is an active region, change that region to a new emphasis.
3609 If there is no region, just insert the marker characters and position
3610 the cursor between them.
3611 CHAR should be either the marker character, or the first character of the
3612 HTML tag associated with that emphasis. If CHAR is a space, the means
3613 to remove the emphasis of the selected region.
3614 If char is not given (for example in an interactive call) it
3615 will be prompted for."
3617 (let ((eal org-emphasis-alist) e det
3618 (erc org-emphasis-regexp-components)
3620 (string "") beg end move tag c s)
3621 (if (org-region-active-p)
3622 (setq beg (region-beginning) end (region-end)
3623 string (buffer-substring beg end))
3626 (while (setq e (pop eal))
3627 (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
3629 (push (cons c (string-to-char (car e))) det)
3630 (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
3631 (substring tag 1)))))
3633 (message "%s" (concat "Emphasis marker or tag:" prompt))
3634 (setq char (read-char-exclusive)))
3635 (setq char (or (cdr (assoc char det)) char))
3636 (if (equal char ?\ )
3637 (setq s "" move nil)
3638 (unless (assoc (char-to-string char) org-emphasis-alist)
3639 (error "No such emphasis marker: \"%c\"" char))
3640 (setq s (char-to-string char)))
3641 (while (and (> (length string) 1)
3642 (equal (substring string 0 1) (substring string -1))
3643 (assoc (substring string 0 1) org-emphasis-alist))
3644 (setq string (substring string 1 -1)))
3645 (setq string (concat s string s))
3646 (if beg (delete-region beg end))
3648 (string-match (concat "[" (nth 0 erc) "\n]")
3649 (char-to-string (char-before (point)))))
3651 (unless (string-match (concat "[" (nth 1 erc) "\n]")
3652 (char-to-string (char-after (point))))
3653 (insert " ") (backward-char 1))
3655 (and move (backward-char 1))))
3657 (defconst org-nonsticky-props
3658 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
3661 (defun org-activate-plain-links (limit)
3662 "Run through the buffer and add overlays to links."
3665 (while (re-search-forward org-plain-link-re limit t)
3666 (setq f (get-text-property (match-beginning 0) 'face))
3667 (if (or (eq f 'org-tag)
3668 (and (listp f) (memq 'org-tag f)))
3670 (add-text-properties (match-beginning 0) (match-end 0)
3671 (list 'mouse-face 'highlight
3672 'rear-nonsticky org-nonsticky-props
3673 'keymap org-mouse-map
3675 (throw 'exit t))))))
3677 (defun org-activate-code (limit)
3678 (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
3679 (unless (get-text-property (match-beginning 1) 'face)
3680 (remove-text-properties (match-beginning 0) (match-end 0)
3681 '(display t invisible t intangible t))
3684 (defun org-activate-angle-links (limit)
3685 "Run through the buffer and add overlays to links."
3686 (if (re-search-forward org-angle-link-re limit t)
3688 (add-text-properties (match-beginning 0) (match-end 0)
3689 (list 'mouse-face 'highlight
3690 'rear-nonsticky org-nonsticky-props
3691 'keymap org-mouse-map
3695 (defun org-activate-bracket-links (limit)
3696 "Run through the buffer and add overlays to bracketed links."
3697 (if (re-search-forward org-bracket-link-regexp limit t)
3698 (let* ((help (concat "LINK: "
3699 (org-match-string-no-properties 1)))
3700 ;; FIXME: above we should remove the escapes.
3701 ;; but that requires another match, protecting match data,
3702 ;; a lot of overhead for font-lock.
3703 (ip (org-maybe-intangible
3704 (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
3705 'keymap org-mouse-map 'mouse-face 'highlight
3706 'font-lock-multiline t 'help-echo help)))
3707 (vp (list 'rear-nonsticky org-nonsticky-props
3708 'keymap org-mouse-map 'mouse-face 'highlight
3709 ' font-lock-multiline t 'help-echo help)))
3710 ;; We need to remove the invisible property here. Table narrowing
3711 ;; may have made some of this invisible.
3712 (remove-text-properties (match-beginning 0) (match-end 0)
3716 (add-text-properties (match-beginning 0) (match-beginning 3) ip)
3717 (add-text-properties (match-beginning 3) (match-end 3) vp)
3718 (add-text-properties (match-end 3) (match-end 0) ip))
3719 (add-text-properties (match-beginning 0) (match-beginning 1) ip)
3720 (add-text-properties (match-beginning 1) (match-end 1) vp)
3721 (add-text-properties (match-end 1) (match-end 0) ip))
3724 (defun org-activate-dates (limit)
3725 "Run through the buffer and add overlays to dates."
3726 (if (re-search-forward org-tsr-regexp-both limit t)
3728 (add-text-properties (match-beginning 0) (match-end 0)
3729 (list 'mouse-face 'highlight
3730 'rear-nonsticky org-nonsticky-props
3731 'keymap org-mouse-map))
3732 (when org-display-custom-times
3734 (org-display-custom-time (match-beginning 3) (match-end 3)))
3735 (org-display-custom-time (match-beginning 1) (match-end 1)))
3738 (defvar org-target-link-regexp nil
3739 "Regular expression matching radio targets in plain text.")
3740 (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
3741 "Regular expression matching a link target.")
3742 (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
3743 "Regular expression matching a radio target.")
3744 (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
3745 "Regular expression matching any target.")
3747 (defun org-activate-target-links (limit)
3748 "Run through the buffer and add overlays to target matches."
3749 (when org-target-link-regexp
3750 (let ((case-fold-search t))
3751 (if (re-search-forward org-target-link-regexp limit t)
3753 (add-text-properties (match-beginning 0) (match-end 0)
3754 (list 'mouse-face 'highlight
3755 'rear-nonsticky org-nonsticky-props
3756 'keymap org-mouse-map
3757 'help-echo "Radio target link"
3758 'org-linked-text t))
3761 (defun org-update-radio-target-regexp ()
3762 "Find all radio targets in this file and update the regular expression."
3764 (when (memq 'radio org-activate-links)
3765 (setq org-target-link-regexp
3766 (org-make-target-link-regexp (org-all-targets 'radio)))
3767 (org-restart-font-lock)))
3769 (defun org-hide-wide-columns (limit)
3771 (setq s (text-property-any (point) (or limit (point-max))
3774 (setq e (next-single-property-change s 'org-cwidth))
3775 (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
3779 (defvar org-latex-and-specials-regexp nil
3780 "Regular expression for highlighting export special stuff.")
3781 (defvar org-match-substring-regexp)
3782 (defvar org-match-substring-with-braces-regexp)
3783 (defvar org-export-html-special-string-regexps)
3785 (defun org-compute-latex-and-specials-regexp ()
3786 "Compute regular expression for stuff treated specially by exporters."
3787 (if (not org-highlight-latex-fragments-and-specials)
3788 (org-set-local 'org-latex-and-specials-regexp nil)
3791 ((matchers (plist-get org-format-latex-options :matchers))
3792 (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
3793 org-latex-regexps)))
3794 (options (org-combine-plists (org-default-export-plist)
3795 (org-infile-export-plist)))
3796 (org-export-with-sub-superscripts (plist-get options :sub-superscript))
3797 (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
3798 (org-export-with-TeX-macros (plist-get options :TeX-macros))
3799 (org-export-html-expand (plist-get options :expand-quoted-html))
3800 (org-export-with-special-strings (plist-get options :special-strings))
3803 ((equal org-export-with-sub-superscripts '{})
3804 (list org-match-substring-with-braces-regexp))
3805 (org-export-with-sub-superscripts
3806 (list org-match-substring-regexp))
3809 (if org-export-with-LaTeX-fragments
3810 (mapcar (lambda (x) (nth 1 x)) latexs)))
3812 (if org-export-with-TeX-macros
3813 (list (concat "\\\\"
3815 (append (mapcar 'car org-html-entities)
3816 (if (boundp 'org-latex-entities)
3817 org-latex-entities nil))
3820 ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
3821 (re-special (if org-export-with-special-strings
3822 (mapcar (lambda (x) (car x))
3823 org-export-html-special-string-regexps)))
3827 (if org-export-html-expand "@<[^>\n]+>")
3830 'org-latex-and-specials-regexp
3831 (mapconcat 'identity (append re-latex re-sub re-macros re-special
3834 (defface org-latex-and-export-specials
3835 (let ((font (cond ((assq :inherit custom-face-attributes)
3836 '(:inherit underline))
3837 (t '(:underline t)))))
3838 `((((class grayscale) (background light))
3839 (:foreground "DimGray" ,@font))
3840 (((class grayscale) (background dark))
3841 (:foreground "LightGray" ,@font))
3842 (((class color) (background light))
3843 (:foreground "SaddleBrown"))
3844 (((class color) (background dark))
3845 (:foreground "burlywood"))
3847 "Face used to highlight math latex and other special exporter stuff."
3850 (defun org-do-latex-and-special-faces (limit)
3851 "Run through the buffer and add overlays to links."
3852 (when org-latex-and-specials-regexp
3854 (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
3856 (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
3858 '(org-code org-verbatim underline)))
3861 d (cond ((member (char-after (1+ (match-beginning 0)))
3864 (font-lock-prepend-text-property
3865 (+ d (match-beginning 0)) (match-end 0)
3866 'face 'org-latex-and-export-specials)
3867 (add-text-properties (+ d (match-beginning 0)) (match-end 0)
3868 '(font-lock-multiline t)))))
3871 (defun org-restart-font-lock ()
3872 "Restart font-lock-mode, to force refontification."
3873 (when (and (boundp 'font-lock-mode) font-lock-mode)
3875 (font-lock-mode 1)))
3877 (defun org-all-targets (&optional radio)
3878 "Return a list of all targets in this file.
3879 With optional argument RADIO, only find radio targets."
3880 (let ((re (if radio org-radio-target-regexp org-target-regexp))
3883 (goto-char (point-min))
3884 (while (re-search-forward re nil t)
3885 (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
3888 (defun org-make-target-link-regexp (targets)
3889 "Make regular expression matching all strings in TARGETS.
3890 The regular expression finds the targets also if there is a line break
3897 (while (string-match " +" x)
3898 (setq x (replace-match "\\s-+" t t x)))
3904 (defun org-activate-tags (limit)
3905 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
3907 (add-text-properties (match-beginning 1) (match-end 1)
3908 (list 'mouse-face 'highlight
3909 'rear-nonsticky org-nonsticky-props
3910 'keymap org-mouse-map))
3913 (defun org-outline-level ()
3915 (looking-at outline-regexp)
3916 (if (match-beginning 1)
3917 (+ (org-get-string-indentation (match-string 1)) 1000)
3918 (1- (- (match-end 0) (match-beginning 0))))))
3920 (defvar org-font-lock-keywords nil)
3922 (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
3923 "Regular expression matching a property line.")
3925 (defun org-set-font-lock-defaults ()
3926 (let* ((em org-fontify-emphasized-text)
3927 (lk org-activate-links)
3928 (org-font-lock-extra-keywords
3931 '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
3932 (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
3934 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
3937 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
3938 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
3939 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
3941 (list org-drawer-regexp '(0 'org-special-keyword t))
3942 (list "^[ \t]*:END:" '(0 'org-special-keyword t))
3944 (list org-property-re
3945 '(1 'org-special-keyword t)
3946 '(3 'org-property-value t))
3947 (if org-format-transports-properties-p
3948 '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
3950 (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
3951 (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
3952 (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
3953 (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
3954 (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
3955 (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
3956 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
3957 '(org-hide-wide-columns (0 nil append))
3959 (list (concat "^\\*+[ \t]+" org-todo-regexp)
3960 '(1 (org-get-todo-face 1) t))
3962 (if org-fontify-done-headline
3963 (list (concat "^[*]+ +\\<\\("
3964 (mapconcat 'regexp-quote org-done-keywords "\\|")
3966 '(2 'org-headline-done t))
3969 (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
3971 (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
3972 (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
3973 (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
3974 (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
3977 (if (featurep 'xemacs)
3978 '(org-do-emphasis-faces (0 nil append))
3979 '(org-do-emphasis-faces)))
3981 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
3983 (if org-provide-checkbox-statistics
3984 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
3985 (0 (org-get-checkbox-statistics-face) t)))
3986 (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
3987 '(1 'org-archived prepend))
3989 '(org-do-latex-and-special-faces)
3991 '(org-activate-code (1 'org-code t))
3993 (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
3994 "\\|" org-quote-string "\\)\\>")
3995 '(1 'org-special-keyword t))
3996 '("^#.*" (0 'font-lock-comment-face t))
3998 (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
3999 ;; Now set the full font-lock-keywords
4000 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
4001 (org-set-local 'font-lock-defaults
4002 '(org-font-lock-keywords t nil nil backward-paragraph))
4003 (kill-local-variable 'font-lock-keywords) nil))
4008 (defun org-get-level-face (n)
4009 "Get the right face for match N in font-lock matching of healdines."
4010 (setq org-l (- (match-end 2) (match-beginning 1) 1))
4011 (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
4012 (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
4014 ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
4016 (t (if org-level-color-stars-only nil org-f))))
4018 (defun org-get-todo-face (kwd)
4019 "Get the right face for a TODO keyword KWD.
4020 If KWD is a number, get the corresponding match group."
4021 (if (numberp kwd) (setq kwd (match-string kwd)))
4022 (or (cdr (assoc kwd org-todo-keyword-faces))
4023 (and (member kwd org-done-keywords) 'org-done)
4026 (defun org-unfontify-region (beg end &optional maybe_loudly)
4027 "Remove fontification and activation overlays from links."
4028 (font-lock-default-unfontify-region beg end)
4029 (let* ((buffer-undo-list t)
4030 (inhibit-read-only t) (inhibit-point-motion-hooks t)
4031 (inhibit-modification-hooks t)
4032 deactivate-mark buffer-file-name buffer-file-truename)
4033 (remove-text-properties beg end
4034 '(mouse-face t keymap t org-linked-text t
4035 invisible t intangible t))))
4037 ;;;; Visibility cycling, including org-goto and indirect buffer
4041 (defvar org-cycle-global-status nil)
4042 (make-variable-buffer-local 'org-cycle-global-status)
4043 (defvar org-cycle-subtree-status nil)
4044 (make-variable-buffer-local 'org-cycle-subtree-status)
4047 (defun org-cycle (&optional arg)
4048 "Visibility cycling for Org-mode.
4050 - When this function is called with a prefix argument, rotate the entire
4051 buffer through 3 states (global cycling)
4052 1. OVERVIEW: Show only top-level headlines.
4053 2. CONTENTS: Show all headlines of all levels, but no body text.
4054 3. SHOW ALL: Show everything.
4056 - When point is at the beginning of a headline, rotate the subtree started
4057 by this line through 3 different states (local cycling)
4058 1. FOLDED: Only the main headline is shown.
4059 2. CHILDREN: The main headline and the direct children are shown.
4060 From this state, you can move to one of the children
4061 and zoom in further.
4062 3. SUBTREE: Show the entire subtree, including body text.
4064 - When there is a numeric prefix, go up to a heading with level ARG, do
4065 a `show-subtree' and return to the previous cursor position. If ARG
4066 is negative, go up that many levels.
4068 - When point is not at the beginning of a headline, execute
4069 `indent-relative', like TAB normally does. See the option
4070 `org-cycle-emulate-tab' for details.
4072 - Special case: if point is at the beginning of the buffer and there is
4073 no headline in line 1, this function will act as if called with prefix arg.
4074 But only if also the variable `org-cycle-global-at-bob' is t."
4076 (org-load-modules-maybe)
4077 (let* ((outline-regexp
4078 (if (and (org-mode-p) org-cycle-include-plain-lists)
4079 "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
4081 (bob-special (and org-cycle-global-at-bob (bobp)
4082 (not (looking-at outline-regexp))))
4085 (delq 'org-optimize-window-after-visibility-change
4086 (copy-sequence org-cycle-hook))
4090 (if (or bob-special (equal arg '(4)))
4091 ;; special case: use global cycling
4096 ((org-at-table-p 'any)
4097 ;; Enter the table or move to the next field in the table
4098 (or (org-table-recognize-table.el)
4100 (if arg (org-table-edit-field t)
4101 (org-table-justify-field-maybe)
4102 (call-interactively 'org-table-next-field)))))
4104 ((eq arg t) ;; Global cycling
4107 ((and (eq last-command this-command)
4108 (eq org-cycle-global-status 'overview))
4109 ;; We just created the overview - now do table of contents
4110 ;; This can be slow in very large buffers, so indicate action
4111 (message "CONTENTS...")
4113 (message "CONTENTS...done")
4114 (setq org-cycle-global-status 'contents)
4115 (run-hook-with-args 'org-cycle-hook 'contents))
4117 ((and (eq last-command this-command)
4118 (eq org-cycle-global-status 'contents))
4119 ;; We just showed the table of contents - now show everything
4121 (message "SHOW ALL")
4122 (setq org-cycle-global-status 'all)
4123 (run-hook-with-args 'org-cycle-hook 'all))
4126 ;; Default action: go to overview
4128 (message "OVERVIEW")
4129 (setq org-cycle-global-status 'overview)
4130 (run-hook-with-args 'org-cycle-hook 'overview))))
4132 ((and org-drawers org-drawer-regexp
4134 (beginning-of-line 1)
4135 (looking-at org-drawer-regexp)))
4136 ;; Toggle block visibility
4138 (not (get-char-property (match-end 0) 'invisible))))
4141 ;; Show-subtree, ARG levels up from here.
4143 (org-back-to-heading)
4144 (outline-up-heading (if (< arg 0) (- arg)
4145 (- (funcall outline-level) arg)))
4146 (org-show-subtree)))
4148 ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
4149 (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
4150 ;; At a heading: rotate between three different views
4151 (org-back-to-heading)
4152 (let ((goal-column 0) eoh eol eos)
4153 ;; First, some boundaries
4155 (org-back-to-heading)
4157 (beginning-of-line 2)
4158 (while (and (not (eobp)) ;; this is like `next-line'
4159 (get-char-property (1- (point)) 'invisible))
4160 (beginning-of-line 2)) (setq eol (point)))
4161 (outline-end-of-heading) (setq eoh (point))
4162 (org-end-of-subtree t)
4164 (skip-chars-forward " \t\n")
4165 (beginning-of-line 1) ; in case this is an item
4167 (setq eos (1- (point))))
4168 ;; Find out what to do next and set `this-command'
4171 ;; Nothing is hidden behind this heading
4172 (message "EMPTY ENTRY")
4173 (setq org-cycle-subtree-status nil)
4176 (outline-next-heading)
4177 (if (org-invisible-p) (org-flag-heading nil))))
4179 (not (string-match "\\S-" (buffer-substring eol eos))))
4180 ;; Entire subtree is hidden in one line: open it
4183 (message "CHILDREN")
4186 (outline-next-heading)
4187 (if (org-invisible-p) (org-flag-heading nil)))
4188 (setq org-cycle-subtree-status 'children)
4189 (run-hook-with-args 'org-cycle-hook 'children))
4190 ((and (eq last-command this-command)
4191 (eq org-cycle-subtree-status 'children))
4192 ;; We just showed the children, now show everything.
4195 (setq org-cycle-subtree-status 'subtree)
4196 (run-hook-with-args 'org-cycle-hook 'subtree))
4198 ;; Default action: hide the subtree.
4201 (setq org-cycle-subtree-status 'folded)
4202 (run-hook-with-args 'org-cycle-hook 'folded)))))
4205 (buffer-read-only (org-back-to-heading))
4207 ((org-try-cdlatex-tab))
4209 ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
4211 (not (looking-at outline-regexp))))
4212 (call-interactively (global-key-binding "\t")))
4214 ((if (and (memq org-cycle-emulate-tab '(white whitestart))
4215 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
4216 (or (and (eq org-cycle-emulate-tab 'white)
4217 (= (match-end 0) (point-at-eol)))
4218 (and (eq org-cycle-emulate-tab 'whitestart)
4219 (>= (match-end 0) pos))))
4221 (eq org-cycle-emulate-tab t))
4222 (call-interactively (global-key-binding "\t")))
4225 (org-back-to-heading)
4229 (defun org-global-cycle (&optional arg)
4230 "Cycle the global visibility. For details see `org-cycle'."
4232 (let ((org-cycle-include-plain-lists
4233 (if (org-mode-p) org-cycle-include-plain-lists nil)))
4237 (hide-sublevels arg)
4238 (setq org-cycle-global-status 'contents))
4241 (defun org-overview ()
4242 "Switch to overview mode, shoing only top-level headlines.
4243 Really, this shows all headlines with level equal or greater than the level
4244 of the first headline in the buffer. This is important, because if the
4245 first headline is not level one, then (hide-sublevels 1) gives confusing
4248 (let ((level (save-excursion
4249 (goto-char (point-min))
4250 (if (re-search-forward (concat "^" outline-regexp) nil t)
4252 (goto-char (match-beginning 0))
4253 (funcall outline-level))))))
4254 (and level (hide-sublevels level))))
4256 (defun org-content (&optional arg)
4257 "Show all headlines in the buffer, like a table of contents.
4258 With numerical argument N, show content up to level N."
4261 ;; Visit all headings and show their offspring
4262 (and (integerp arg) (org-overview))
4263 (goto-char (point-max))
4265 (while (and (progn (condition-case nil
4266 (outline-previous-visible-heading 1)
4267 (error (goto-char (point-min))))
4269 (looking-at outline-regexp))
4271 (show-children (1- arg))
4273 (if (bobp) (throw 'exit nil))))))
4276 (defun org-optimize-window-after-visibility-change (state)
4277 "Adjust the window after a change in outline visibility.
4278 This function is the default value of the hook `org-cycle-hook'."
4279 (when (get-buffer-window (current-buffer))
4281 ; ((eq state 'overview) (org-first-headline-recenter 1))
4282 ; ((eq state 'overview) (org-beginning-of-line))
4283 ((eq state 'content) nil)
4284 ((eq state 'all) nil)
4285 ((eq state 'folded) nil)
4286 ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
4287 ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
4289 (defun org-compact-display-after-subtree-move ()
4292 (if (org-up-heading-safe)
4297 (org-cycle-show-empty-lines 'children)
4298 (org-cycle-hide-drawers 'children))
4301 (defun org-cycle-show-empty-lines (state)
4302 "Show empty lines above all visible headlines.
4303 The region to be covered depends on STATE when called through
4304 `org-cycle-hook'. Lisp program can use t for STATE to get the
4305 entire buffer covered. Note that an empty line is only shown if there
4306 are at least `org-cycle-separator-lines' empty lines before the headeline."
4307 (when (> org-cycle-separator-lines 0)
4309 (let* ((n org-cycle-separator-lines)
4311 ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
4312 ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
4313 (t (let ((ns (number-to-string (- n 2))))
4314 (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
4315 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
4318 ((memq state '(overview contents t))
4319 (setq beg (point-min) end (point-max)))
4320 ((memq state '(children folded))
4321 (setq beg (point) end (progn (org-end-of-subtree t t)
4322 (beginning-of-line 2)
4326 (while (re-search-forward re end t)
4327 (if (not (get-char-property (match-end 1) 'invisible))
4328 (outline-flag-region
4329 (match-beginning 1) (match-end 1) nil)))))))
4330 ;; Never hide empty lines at the end of the file.
4332 (goto-char (point-max))
4333 (outline-previous-heading)
4334 (outline-end-of-heading)
4335 (if (and (looking-at "[ \t\n]+")
4336 (= (match-end 0) (point-max)))
4337 (outline-flag-region (point) (match-end 0) nil))))
4339 (defun org-subtree-end-visible-p ()
4340 "Is the end of the current subtree visible?"
4341 (pos-visible-in-window-p
4342 (save-excursion (org-end-of-subtree t) (point))))
4344 (defun org-first-headline-recenter (&optional N)
4345 "Move cursor to the first headline and recenter the headline.
4346 Optional argument N means, put the headline into the Nth line of the window."
4347 (goto-char (point-min))
4348 (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
4350 (recenter (prefix-numeric-value N))))
4354 (defvar org-goto-window-configuration nil)
4355 (defvar org-goto-marker nil)
4356 (defvar org-goto-map
4357 (let ((map (make-sparse-keymap)))
4358 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
4359 (while (setq cmd (pop cmds))
4360 (substitute-key-definition cmd cmd map global-map)))
4361 (suppress-keymap map)
4362 (org-defkey map "\C-m" 'org-goto-ret)
4363 (org-defkey map [(return)] 'org-goto-ret)
4364 (org-defkey map [(left)] 'org-goto-left)
4365 (org-defkey map [(right)] 'org-goto-right)
4366 (org-defkey map [(control ?g)] 'org-goto-quit)
4367 (org-defkey map "\C-i" 'org-cycle)
4368 (org-defkey map [(tab)] 'org-cycle)
4369 (org-defkey map [(down)] 'outline-next-visible-heading)
4370 (org-defkey map [(up)] 'outline-previous-visible-heading)
4371 (if org-goto-auto-isearch
4372 (if (fboundp 'define-key-after)
4373 (define-key-after map [t] 'org-goto-local-auto-isearch)
4375 (org-defkey map "q" 'org-goto-quit)
4376 (org-defkey map "n" 'outline-next-visible-heading)
4377 (org-defkey map "p" 'outline-previous-visible-heading)
4378 (org-defkey map "f" 'outline-forward-same-level)
4379 (org-defkey map "b" 'outline-backward-same-level)
4380 (org-defkey map "u" 'outline-up-heading))
4381 (org-defkey map "/" 'org-occur)
4382 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
4383 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
4384 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
4385 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
4386 (org-defkey map "\C-c\C-u" 'outline-up-heading)
4389 (defconst org-goto-help
4390 "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
4391 RET=jump to location [Q]uit and return to previous location
4392 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
4394 (defvar org-goto-start-pos) ; dynamically scoped parameter
4396 (defun org-goto (&optional alternative-interface)
4397 "Look up a different location in the current file, keeping current visibility.
4399 When you want look-up or go to a different location in a document, the
4400 fastest way is often to fold the entire buffer and then dive into the tree.
4401 This method has the disadvantage, that the previous location will be folded,
4402 which may not be what you want.
4404 This command works around this by showing a copy of the current buffer
4405 in an indirect buffer, in overview mode. You can dive into the tree in
4406 that copy, use org-occur and incremental search to find a location.
4407 When pressing RET or `Q', the command returns to the original buffer in
4408 which the visibility is still unchanged. After RET is will also jump to
4409 the location selected in the indirect buffer and expose the
4410 the headline hierarchy above."
4412 (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
4413 (org-refile-use-outline-path t)
4415 (if (not alternative-interface)
4417 (if (eq org-goto-interface 'outline)
4418 'outline-path-completion
4420 (org-goto-start-pos (point))
4422 (if (eq interface 'outline)
4423 (car (org-get-location (current-buffer) org-goto-help))
4424 (nth 3 (org-refile-get-location "Goto: ")))))
4427 (org-mark-ring-push org-goto-start-pos)
4428 (goto-char selected-point)
4429 (if (or (org-invisible-p) (org-invisible-p2))
4430 (org-show-context 'org-goto)))
4433 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
4434 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
4435 (defvar org-goto-local-auto-isearch-map) ; defined below
4437 (defun org-get-location (buf help)
4438 "Let the user select a location in the Org-mode buffer BUF.
4439 This function uses a recursive edit. It returns the selected position
4441 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
4442 (isearch-hide-immediately nil)
4443 (isearch-search-fun-function
4444 (lambda () 'org-goto-local-search-forward-headings))
4445 (org-goto-selected-point org-goto-exit-command))
4447 (save-window-excursion
4448 (delete-other-windows)
4449 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
4452 (make-indirect-buffer (current-buffer) "*org-goto*")
4453 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
4454 (with-output-to-temp-buffer "*Help*"
4456 (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
4457 (setq buffer-read-only nil)
4458 (let ((org-startup-truncated t)
4459 (org-startup-folded nil)
4460 (org-startup-align-all-tables nil))
4463 (setq buffer-read-only t)
4464 (if (and (boundp 'org-goto-start-pos)
4465 (integer-or-marker-p org-goto-start-pos))
4466 (let ((org-show-hierarchy-above t)
4467 (org-show-siblings t)
4468 (org-show-following-heading t))
4469 (goto-char org-goto-start-pos)
4470 (and (org-invisible-p) (org-show-context)))
4471 (goto-char (point-min)))
4472 (org-beginning-of-line)
4473 (message "Select location and press RET")
4474 (use-local-map org-goto-map)
4477 (kill-buffer "*org-goto*")
4478 (cons org-goto-selected-point org-goto-exit-command)))
4480 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
4481 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
4482 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
4483 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
4485 (defun org-goto-local-search-forward-headings (string bound noerror)
4486 "Search and make sure that anu matches are in headlines."
4488 (while (search-forward string bound noerror)
4489 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
4490 (and (member :headline context)
4491 (not (member :tags context))))
4492 (throw 'return (point))))))
4494 (defun org-goto-local-auto-isearch ()
4497 (goto-char (point-min))
4498 (let ((keys (this-command-keys)))
4499 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
4501 (isearch-process-search-char (string-to-char keys)))))
4503 (defun org-goto-ret (&optional arg)
4504 "Finish `org-goto' by going to the new location."
4506 (setq org-goto-selected-point (point)
4507 org-goto-exit-command 'return)
4510 (defun org-goto-left ()
4511 "Finish `org-goto' by going to the new location."
4513 (if (org-on-heading-p)
4515 (beginning-of-line 1)
4516 (setq org-goto-selected-point (point)
4517 org-goto-exit-command 'left)
4519 (error "Not on a heading")))
4521 (defun org-goto-right ()
4522 "Finish `org-goto' by going to the new location."
4524 (if (org-on-heading-p)
4526 (setq org-goto-selected-point (point)
4527 org-goto-exit-command 'right)
4529 (error "Not on a heading")))
4531 (defun org-goto-quit ()
4532 "Finish `org-goto' without cursor motion."
4534 (setq org-goto-selected-point nil)
4535 (setq org-goto-exit-command 'quit)
4538 ;;; Indirect buffer display of subtrees
4540 (defvar org-indirect-dedicated-frame nil
4541 "This is the frame being used for indirect tree display.")
4542 (defvar org-last-indirect-buffer nil)
4544 (defun org-tree-to-indirect-buffer (&optional arg)
4545 "Create indirect buffer and narrow it to current subtree.
4546 With numerical prefix ARG, go up to this level and then take that tree.
4547 If ARG is negative, go up that many levels.
4548 If `org-indirect-buffer-display' is not `new-frame', the command removes the
4549 indirect buffer previously made with this command, to avoid proliferation of
4550 indirect buffers. However, when you call the command with a `C-u' prefix, or
4551 when `org-indirect-buffer-display' is `new-frame', the last buffer
4552 is kept so that you can work with several indirect buffers at the same time.
4553 If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
4554 requests that a new frame be made for the new buffer, so that the dedicated
4555 frame is not changed."
4557 (let ((cbuf (current-buffer))
4558 (cwin (selected-window))
4560 beg end level heading ibuf)
4562 (org-back-to-heading t)
4564 (setq level (org-outline-level))
4565 (if (< arg 0) (setq arg (+ level arg)))
4566 (while (> (setq level (org-outline-level)) arg)
4567 (outline-up-heading 1 t)))
4569 heading (org-get-heading))
4570 (org-end-of-subtree t) (setq end (point)))
4571 (if (and (buffer-live-p org-last-indirect-buffer)
4572 (not (eq org-indirect-buffer-display 'new-frame))
4574 (kill-buffer org-last-indirect-buffer))
4575 (setq ibuf (org-get-indirect-buffer cbuf)
4576 org-last-indirect-buffer ibuf)
4578 ((or (eq org-indirect-buffer-display 'new-frame)
4579 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
4580 (select-frame (make-frame))
4581 (delete-other-windows)
4582 (switch-to-buffer ibuf)
4583 (org-set-frame-title heading))
4584 ((eq org-indirect-buffer-display 'dedicated-frame)
4586 (select-frame (or (and org-indirect-dedicated-frame
4587 (frame-live-p org-indirect-dedicated-frame)
4588 org-indirect-dedicated-frame)
4589 (setq org-indirect-dedicated-frame (make-frame)))))
4590 (delete-other-windows)
4591 (switch-to-buffer ibuf)
4592 (org-set-frame-title (concat "Indirect: " heading)))
4593 ((eq org-indirect-buffer-display 'current-window)
4594 (switch-to-buffer ibuf))
4595 ((eq org-indirect-buffer-display 'other-window)
4596 (pop-to-buffer ibuf))
4597 (t (error "Invalid value.")))
4598 (if (featurep 'xemacs)
4599 (save-excursion (org-mode) (turn-on-font-lock)))
4600 (narrow-to-region beg end)
4603 (and (window-live-p cwin) (select-window cwin))))
4605 (defun org-get-indirect-buffer (&optional buffer)
4606 (setq buffer (or buffer (current-buffer)))
4607 (let ((n 1) (base (buffer-name buffer)) bname)
4608 (while (buffer-live-p
4609 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
4612 (make-indirect-buffer buffer bname 'clone)
4613 (error (make-indirect-buffer buffer bname)))))
4615 (defun org-set-frame-title (title)
4616 "Set the title of the current frame to the string TITLE."
4617 ;; FIXME: how to name a single frame in XEmacs???
4618 (unless (featurep 'xemacs)
4619 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
4621 ;;;; Structure editing
4623 ;;; Inserting headlines
4625 (defun org-insert-heading (&optional force-heading)
4626 "Insert a new heading or item with same depth at point.
4627 If point is in a plain list and FORCE-HEADING is nil, create a new list item.
4628 If point is at the beginning of a headline, insert a sibling before the
4629 current headline. If point is not at the beginning, do not split the line,
4630 but create the new hedline after the current line."
4632 (if (= (buffer-size) 0)
4634 (when (or force-heading (not (org-insert-item)))
4635 (let* ((head (save-excursion
4638 (org-back-to-heading)
4641 (blank (cdr (assq 'heading org-blank-before-new-entry)))
4644 ((and (org-on-heading-p) (bolp)
4646 (save-excursion (backward-char 1) (not (org-invisible-p)))))
4647 ;; insert before the current line
4648 (open-line (if blank 2 1)))
4652 (backward-char 1) (not (org-invisible-p)))))
4653 ;; insert right here
4656 ;; in the middle of the line
4659 (org-get-alist-option org-M-RET-may-split-line 'headline))
4661 (if (org-on-heading-p)
4663 (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
4664 (setq tags (and (match-end 2) (match-string 2)))
4666 (delete-region (match-beginning 1) (match-end 1)))
4667 (setq pos (point-at-bol))
4668 (or split (end-of-line 1))
4669 (delete-horizontal-space)
4670 (newline (if blank 2 1))
4676 (org-set-tags nil 'align))))
4677 (or split (end-of-line 1))
4678 (newline (if blank 2 1))))))
4679 (insert head) (just-one-space)
4682 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
4683 (run-hooks 'org-insert-heading-hook)))))
4685 (defun org-get-heading (&optional no-tags)
4686 "Return the heading of the current entry, without the stars."
4688 (org-back-to-heading t)
4691 (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
4692 "\\*+[ \t]+\\([^\r\n]*\\)"))
4693 (match-string 1) "")))
4695 (defun org-insert-heading-after-current ()
4696 "Insert a new heading with same level as current, after current subtree."
4698 (org-back-to-heading)
4699 (org-insert-heading)
4700 (org-move-subtree-down)
4703 (defun org-insert-todo-heading (arg)
4704 "Insert a new heading with the same level and TODO state as current heading.
4705 If the heading has no TODO state, or if the state is DONE, use the first
4706 state (TODO by default). Also with prefix arg, force first state."
4708 (when (not (org-insert-item 'checkbox))
4709 (org-insert-heading)
4711 (org-back-to-heading)
4712 (outline-previous-heading)
4713 (looking-at org-todo-line-regexp))
4715 (not (match-beginning 2))
4716 (member (match-string 2) org-done-keywords))
4717 (insert (car org-todo-keywords-1) " ")
4718 (insert (match-string 2) " "))))
4720 (defun org-insert-subheading (arg)
4721 "Insert a new subheading and demote it.
4722 Works for outline headings and for plain lists alike."
4724 (org-insert-heading arg)
4726 ((org-on-heading-p) (org-do-demote))
4727 ((org-at-item-p) (org-indent-item 1))))
4729 (defun org-insert-todo-subheading (arg)
4730 "Insert a new subheading with TODO keyword or checkbox and demote it.
4731 Works for outline headings and for plain lists alike."
4733 (org-insert-todo-heading arg)
4735 ((org-on-heading-p) (org-do-demote))
4736 ((org-at-item-p) (org-indent-item 1))))
4738 ;;; Promotion and Demotion
4740 (defun org-promote-subtree ()
4741 "Promote the entire subtree.
4742 See also `org-promote'."
4745 (org-map-tree 'org-promote))
4746 (org-fix-position-after-promote))
4748 (defun org-demote-subtree ()
4749 "Demote the entire subtree. See `org-demote'.
4750 See also `org-promote'."
4753 (org-map-tree 'org-demote))
4754 (org-fix-position-after-promote))
4757 (defun org-do-promote ()
4758 "Promote the current heading higher up the tree.
4759 If the region is active in `transient-mark-mode', promote all headings
4763 (if (org-region-active-p)
4764 (org-map-region 'org-promote (region-beginning) (region-end))
4766 (org-fix-position-after-promote))
4768 (defun org-do-demote ()
4769 "Demote the current heading lower down the tree.
4770 If the region is active in `transient-mark-mode', demote all headings
4774 (if (org-region-active-p)
4775 (org-map-region 'org-demote (region-beginning) (region-end))
4777 (org-fix-position-after-promote))
4779 (defun org-fix-position-after-promote ()
4780 "Make sure that after pro/demotion cursor position is right."
4781 (let ((pos (point)))
4782 (when (save-excursion
4783 (beginning-of-line 1)
4784 (looking-at org-todo-line-regexp)
4785 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
4786 (cond ((eobp) (insert " "))
4787 ((eolp) (insert " "))
4788 ((equal (char-after) ?\ ) (forward-char 1))))))
4790 (defun org-reduced-level (l)
4791 (if org-odd-levels-only (1+ (floor (/ l 2))) l))
4793 (defun org-get-valid-level (level &optional change)
4794 "Rectify a level change under the influence of `org-odd-levels-only'
4795 LEVEL is a current level, CHANGE is by how much the level should be
4796 modified. Even if CHANGE is nil, LEVEL may be returned modified because
4797 even level numbers will become the next higher odd number."
4798 (if org-odd-levels-only
4799 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
4800 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
4801 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
4802 (max 1 (+ level change))))
4804 (if (boundp 'define-obsolete-function-alias)
4805 (if (or (featurep 'xemacs) (< emacs-major-version 23))
4806 (define-obsolete-function-alias 'org-get-legal-level
4807 'org-get-valid-level)
4808 (define-obsolete-function-alias 'org-get-legal-level
4809 'org-get-valid-level "23.1")))
4811 (defun org-promote ()
4812 "Promote the current heading higher up the tree.
4813 If the region is active in `transient-mark-mode', promote all headings
4815 (org-back-to-heading t)
4816 (let* ((level (save-match-data (funcall outline-level)))
4817 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
4818 (diff (abs (- level (length up-head) -1))))
4819 (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
4820 (replace-match up-head nil t)
4821 ;; Fixup tag positioning
4822 (and org-auto-align-tags (org-set-tags nil t))
4823 (if org-adapt-indentation (org-fixup-indentation (- diff)))))
4825 (defun org-demote ()
4826 "Demote the current heading lower down the tree.
4827 If the region is active in `transient-mark-mode', demote all headings
4829 (org-back-to-heading t)
4830 (let* ((level (save-match-data (funcall outline-level)))
4831 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
4832 (diff (abs (- level (length down-head) -1))))
4833 (replace-match down-head nil t)
4834 ;; Fixup tag positioning
4835 (and org-auto-align-tags (org-set-tags nil t))
4836 (if org-adapt-indentation (org-fixup-indentation diff))))
4838 (defun org-map-tree (fun)
4839 "Call FUN for every heading underneath the current one."
4840 (org-back-to-heading)
4841 (let ((level (funcall outline-level)))
4845 (outline-next-heading)
4846 (> (funcall outline-level) level))
4850 (defun org-map-region (fun beg end)
4851 "Call FUN for every heading between BEG and END."
4852 (let ((org-ignore-region t))
4854 (setq end (copy-marker end))
4856 (if (and (re-search-forward (concat "^" outline-regexp) nil t)
4860 (outline-next-heading)
4865 (defun org-fixup-indentation (diff)
4866 "Change the indentation in the current entry by DIFF
4867 However, if any line in the current entry has no indentation, or if it
4868 would end up with no indentation after the change, nothing at all is done."
4870 (let ((end (save-excursion (outline-next-heading)
4872 (prohibit (if (> diff 0)
4874 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
4876 (unless (save-excursion (end-of-line 1)
4877 (re-search-forward prohibit end t))
4878 (while (and (< (point) end)
4879 (re-search-forward "^[ \t]+" end t))
4880 (goto-char (match-end 0))
4881 (setq col (current-column))
4882 (if (< diff 0) (replace-match ""))
4883 (indent-to (+ diff col))))
4884 (move-marker end nil))))
4886 (defun org-convert-to-odd-levels ()
4887 "Convert an org-mode file with all levels allowed to one with odd levels.
4888 This will leave level 1 alone, convert level 2 to level 3, level 3 to
4891 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
4892 (let ((org-odd-levels-only nil) n)
4894 (goto-char (point-min))
4895 (while (re-search-forward "^\\*\\*+ " nil t)
4896 (setq n (- (length (match-string 0)) 2))
4897 (while (>= (setq n (1- n)) 0)
4899 (end-of-line 1))))))
4902 (defun org-convert-to-oddeven-levels ()
4903 "Convert an org-mode file with only odd levels to one with odd and even levels.
4904 This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
4905 section with an even level, conversion would destroy the structure of the file. An error
4906 is signaled in this case."
4908 (goto-char (point-min))
4909 ;; First check if there are no even levels
4910 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
4911 (org-show-context t)
4912 (error "Not all levels are odd in this file. Conversion not possible."))
4913 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
4914 (let ((org-odd-levels-only nil) n)
4916 (goto-char (point-min))
4917 (while (re-search-forward "^\\*\\*+ " nil t)
4918 (setq n (/ (1- (length (match-string 0))) 2))
4919 (while (>= (setq n (1- n)) 0)
4921 (end-of-line 1))))))
4923 (defun org-tr-level (n)
4924 "Make N odd if required."
4925 (if org-odd-levels-only (1+ (/ n 2)) n))
4927 ;;; Vertical tree motion, cutting and pasting of subtrees
4929 (defun org-move-subtree-up (&optional arg)
4930 "Move the current subtree up past ARG headlines of the same level."
4932 (org-move-subtree-down (- (prefix-numeric-value arg))))
4934 (defun org-move-subtree-down (&optional arg)
4935 "Move the current subtree down past ARG headlines of the same level."
4937 (setq arg (prefix-numeric-value arg))
4938 (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
4939 'outline-get-last-sibling))
4940 (ins-point (make-marker))
4942 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
4944 (org-back-to-heading)
4947 (setq ne-beg (org-back-over-empty-lines))
4950 (save-excursion (outline-end-of-heading)
4951 (setq folded (org-invisible-p)))
4952 (outline-end-of-subtree))
4953 (outline-next-heading)
4954 (setq ne-end (org-back-over-empty-lines))
4957 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
4958 ;; include less whitespace
4961 (forward-line (- ne-beg ne-end))
4962 (setq beg (point))))
4963 ;; Find insertion point, with error handling
4965 (or (and (funcall movfunc) (looking-at outline-regexp))
4966 (progn (goto-char beg0)
4967 (error "Cannot move past superior level or buffer limit")))
4968 (setq cnt (1- cnt)))
4970 ;; Moving forward - still need to move over subtree
4971 (progn (org-end-of-subtree t t)
4973 (org-back-over-empty-lines)
4974 (or (bolp) (newline)))))
4975 (setq ne-ins (org-back-over-empty-lines))
4976 (move-marker ins-point (point))
4977 (setq txt (buffer-substring beg end))
4978 (delete-region beg end)
4979 (outline-flag-region (1- beg) beg nil)
4980 (outline-flag-region (1- (point)) (point) nil)
4982 (or (bolp) (insert "\n"))
4983 (setq ins-end (point))
4984 (goto-char ins-point)
4985 (org-skip-whitespace)
4986 (when (and (< arg 0)
4987 (org-first-sibling-p)
4989 ;; Move whitespace back to beginning
4992 (let ((kill-whole-line t))
4993 (kill-line (- ne-ins ne-beg)) (point)))
4994 (insert (make-string (- ne-ins ne-beg) ?\n)))
4995 (move-marker ins-point nil)
4996 (org-compact-display-after-subtree-move)
5000 (org-cycle-hide-drawers 'children))))
5002 (defvar org-subtree-clip ""
5003 "Clipboard for cut and paste of subtrees.
5004 This is actually only a copy of the kill, because we use the normal kill
5005 ring. We need it to check if the kill was created by `org-copy-subtree'.")
5007 (defvar org-subtree-clip-folded nil
5008 "Was the last copied subtree folded?
5009 This is used to fold the tree back after pasting.")
5011 (defun org-cut-subtree (&optional n)
5012 "Cut the current subtree into the clipboard.
5013 With prefix arg N, cut this many sequential subtrees.
5014 This is a short-hand for marking the subtree and then cutting it."
5016 (org-copy-subtree n 'cut))
5018 (defun org-copy-subtree (&optional n cut)
5019 "Cut the current subtree into the clipboard.
5020 With prefix arg N, cut this many sequential subtrees.
5021 This is a short-hand for marking the subtree and then copying it.
5022 If CUT is non-nil, actually cut the subtree."
5024 (let (beg end folded (beg0 (point)))
5026 (org-back-to-heading nil) ; take what looks like a subtree
5027 (org-back-to-heading t)) ; take what is really there
5028 (org-back-over-empty-lines)
5030 (skip-chars-forward " \t\r\n")
5032 (save-excursion (outline-end-of-heading)
5033 (setq folded (org-invisible-p)))
5035 (outline-forward-same-level (1- n))
5037 (org-end-of-subtree t t))
5038 (org-back-over-empty-lines)
5042 (setq org-subtree-clip-folded folded)
5043 (if cut (kill-region beg end) (copy-region-as-kill beg end))
5044 (setq org-subtree-clip (current-kill 0))
5045 (message "%s: Subtree(s) with %d characters"
5046 (if cut "Cut" "Copied")
5047 (length org-subtree-clip)))))
5049 (defun org-paste-subtree (&optional level tree)
5050 "Paste the clipboard as a subtree, with modification of headline level.
5051 The entire subtree is promoted or demoted in order to match a new headline
5052 level. By default, the new level is derived from the visible headings
5053 before and after the insertion point, and taken to be the inferior headline
5054 level of the two. So if the previous visible heading is level 3 and the
5055 next is level 4 (or vice versa), level 4 will be used for insertion.
5056 This makes sure that the subtree remains an independent subtree and does
5057 not swallow low level entries.
5059 You can also force a different level, either by using a numeric prefix
5060 argument, or by inserting the heading marker by hand. For example, if the
5061 cursor is after \"*****\", then the tree will be shifted to level 5.
5063 If you want to insert the tree as is, just use \\[yank].
5065 If optional TREE is given, use this text instead of the kill ring."
5067 (unless (org-kill-is-subtree-p tree)
5069 (substitute-command-keys
5070 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
5071 (let* ((txt (or tree (and kill-ring (current-kill 0))))
5072 (^re (concat "^\\(" outline-regexp "\\)"))
5073 (re (concat "\\(" outline-regexp "\\)"))
5074 (^re_ (concat "\\(\\*+\\)[ \t]*"))
5076 (old-level (if (string-match ^re txt)
5077 (- (match-end 0) (match-beginning 0) 1)
5079 (force-level (cond (level (prefix-numeric-value level))
5081 ^re_ (buffer-substring (point-at-bol) (point)))
5082 (- (match-end 1) (match-beginning 1)))
5084 (previous-level (save-excursion
5087 (outline-previous-visible-heading 1)
5089 (- (match-end 0) (match-beginning 0) 1)
5092 (next-level (save-excursion
5095 (or (looking-at outline-regexp)
5096 (outline-next-visible-heading 1))
5098 (- (match-end 0) (match-beginning 0) 1)
5101 (new-level (or force-level (max previous-level next-level)))
5102 (shift (if (or (= old-level -1)
5104 (= old-level new-level))
5106 (- new-level old-level)))
5107 (delta (if (> shift 0) -1 1))
5108 (func (if (> shift 0) 'org-demote 'org-promote))
5109 (org-odd-levels-only nil)
5111 ;; Remove the forced level indicator
5113 (delete-region (point-at-bol) (point)))
5115 (beginning-of-line 1)
5116 (org-back-over-empty-lines)
5118 (insert-before-markers txt)
5119 (unless (string-match "\n\\'" txt) (insert "\n"))
5122 (skip-chars-forward " \t\n\r")
5124 ;; Shift if necessary
5127 (narrow-to-region beg end)
5128 (while (not (= shift 0))
5129 (org-map-region func (point-min) (point-max))
5130 (setq shift (+ delta shift)))
5131 (goto-char (point-min))))
5132 (when (interactive-p)
5133 (message "Clipboard pasted as level %d subtree" new-level))
5135 (eq org-subtree-clip (current-kill 0))
5136 org-subtree-clip-folded)
5137 ;; The tree was folded before it was killed/copied
5140 (defun org-kill-is-subtree-p (&optional txt)
5141 "Check if the current kill is an outline subtree, or a set of trees.
5142 Returns nil if kill does not start with a headline, or if the first
5143 headline level is not the largest headline level in the tree.
5144 So this will actually accept several entries of equal levels as well,
5145 which is OK for `org-paste-subtree'.
5146 If optional TXT is given, check this string instead of the current kill."
5147 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
5148 (start-level (and kill
5149 (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
5150 org-outline-regexp "\\)")
5152 (- (match-end 2) (match-beginning 2) 1)))
5153 (re (concat "^" org-outline-regexp))
5154 (start (1+ (match-beginning 2))))
5155 (if (not start-level)
5157 nil) ;; does not even start with a heading
5159 (while (setq start (string-match re kill (1+ start)))
5160 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
5164 (defun org-narrow-to-subtree ()
5165 "Narrow buffer to the current subtree."
5170 (progn (org-back-to-heading) (point))
5171 (progn (org-end-of-subtree t t) (point))))))
5176 (defun org-sort (with-case)
5177 "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
5178 Optional argument WITH-CASE means sort case-sensitively."
5180 (if (org-at-table-p)
5181 (org-call-with-arg 'org-table-sort-lines with-case)
5182 (org-call-with-arg 'org-sort-entries-or-items with-case)))
5184 (defun org-sort-remove-invisible (s)
5185 (remove-text-properties 0 (length s) org-rm-props s)
5186 (while (string-match org-bracket-link-regexp s)
5187 (setq s (replace-match (if (match-end 2)
5189 (match-string 1 s)) t t s)))
5192 (defvar org-priority-regexp) ; defined later in the file
5194 (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
5195 "Sort entries on a certain level of an outline tree.
5196 If there is an active region, the entries in the region are sorted.
5197 Else, if the cursor is before the first entry, sort the top-level items.
5198 Else, the children of the entry at point are sorted.
5200 Sorting can be alphabetically, numerically, and by date/time as given by
5201 the first time stamp in the entry. The command prompts for the sorting
5202 type unless it has been given to the function through the SORTING-TYPE
5203 argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
5204 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
5205 called with point at the beginning of the record. It must return either
5206 a string or a number that should serve as the sorting key for that record.
5208 Comparing entries ignores case by default. However, with an optional argument
5209 WITH-CASE, the sorting considers case as well."
5211 (let ((case-func (if with-case 'identity 'downcase))
5212 start beg end stars re re2
5213 txt what tmp plain-list-p)
5214 ;; Find beginning and end of region to sort
5216 ((org-region-active-p)
5217 ;; we will sort the region
5218 (setq end (region-end)
5220 (goto-char (region-beginning))
5221 (if (not (org-on-heading-p)) (outline-next-heading))
5222 (setq start (point)))
5224 ;; we will sort this plain list
5225 (org-beginning-of-item-list) (setq start (point))
5226 (org-end-of-item-list) (setq end (point))
5228 (setq plain-list-p t
5230 ((or (org-on-heading-p)
5231 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
5232 ;; we will sort the children of the current headline
5233 (org-back-to-heading)
5235 end (progn (org-end-of-subtree t t)
5236 (org-back-over-empty-lines)
5241 (outline-next-heading))
5243 ;; we will sort the top-level entries in this file
5244 (goto-char (point-min))
5245 (or (org-on-heading-p) (outline-next-heading))
5246 (setq start (point) end (point-max) what "top-level")
5251 (if (>= beg end) (error "Nothing to sort"))
5253 (unless plain-list-p
5254 (looking-at "\\(\\*+\\)")
5255 (setq stars (match-string 1)
5256 re (concat "^" (regexp-quote stars) " +")
5257 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
5258 txt (buffer-substring beg end))
5259 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
5260 (if (and (not (equal stars "*")) (string-match re2 txt))
5261 (error "Region to sort contains a level above the first entry")))
5263 (unless sorting-type
5266 "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
5267 "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
5269 (setq sorting-type (read-char-exclusive))
5271 (and (= (downcase sorting-type) ?f)
5273 (completing-read "Sort using function: "
5274 obarray 'fboundp t nil nil))
5275 (setq getkey-func (intern getkey-func)))
5277 (and (= (downcase sorting-type) ?r)
5279 (completing-read "Property: "
5280 (mapcar 'list (org-buffer-property-keys t))
5283 (message "Sorting entries...")
5286 (narrow-to-region start end)
5288 (let ((dcst (downcase sorting-type))
5289 (now (current-time)))
5291 (/= dcst sorting-type)
5292 ;; This function moves to the beginning character of the "record" to
5296 (if (org-at-item-p) t (goto-char (point-max))))
5298 (if (re-search-forward re nil t)
5299 (goto-char (match-beginning 0))
5300 (goto-char (point-max)))))
5301 ;; This function moves to the last character of the "record" being
5308 (outline-forward-same-level 1)
5310 (goto-char (point-max)))))))
5312 ;; This function returns the value that gets sorted against.
5315 (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
5318 (string-to-number (buffer-substring (match-end 0)
5321 (buffer-substring (match-end 0) (point-at-eol)))
5323 (if (re-search-forward org-ts-regexp
5325 (org-time-string-to-time (match-string 0))
5330 (setq tmp (funcall getkey-func))
5331 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
5333 (error "Invalid key function `%s'" getkey-func)))
5334 (t (error "Invalid sorting type `%c'" sorting-type)))))
5338 (if (looking-at outline-regexp)
5339 (string-to-number (buffer-substring (match-end 0)
5343 (funcall case-func (buffer-substring (point-at-bol)
5346 (if (re-search-forward org-ts-regexp
5350 (org-time-string-to-time (match-string 0))
5353 (if (re-search-forward org-priority-regexp (point-at-eol) t)
5354 (string-to-char (match-string 2))
5355 org-default-priority))
5357 (or (org-entry-get nil property) ""))
5361 (setq tmp (funcall getkey-func))
5362 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
5364 (error "Invalid key function `%s'" getkey-func)))
5365 (t (error "Invalid sorting type `%c'" sorting-type)))))
5368 ((= dcst ?a) 'string<)
5369 ((= dcst ?t) 'time-less-p)
5371 (message "Sorting entries...done")))
5373 (defun org-do-sort (table what &optional with-case sorting-type)
5374 "Sort TABLE of WHAT according to SORTING-TYPE.
5375 The user will be prompted for the SORTING-TYPE if the call to this
5376 function does not specify it. WHAT is only for the prompt, to indicate
5377 what is being sorted. The sorting key will be extracted from
5378 the car of the elements of the table.
5379 If WITH-CASE is non-nil, the sorting will be case-sensitive."
5380 (unless sorting-type
5382 "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
5384 (setq sorting-type (read-char-exclusive)))
5385 (let ((dcst (downcase sorting-type))
5386 extractfun comparefun)
5387 ;; Define the appropriate functions
5390 (setq extractfun 'string-to-number
5391 comparefun (if (= dcst sorting-type) '< '>)))
5393 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
5394 (lambda(x) (downcase (org-sort-remove-invisible x))))
5395 comparefun (if (= dcst sorting-type)
5397 (lambda (a b) (and (not (string< a b))
5398 (not (string= a b)))))))
5402 (if (string-match org-ts-regexp x)
5404 (org-time-string-to-time (match-string 0 x)))
5406 comparefun (if (= dcst sorting-type) '< '>)))
5407 (t (error "Invalid sorting type `%c'" sorting-type)))
5409 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
5411 (lambda (a b) (funcall comparefun (car a) (car b))))))
5413 ;;;; Plain list items, including checkboxes
5415 ;;; Plain list items
5417 (defun org-at-item-p ()
5418 "Is point in a line starting a hand-formatted item?"
5419 (let ((llt org-plain-list-ordered-item-terminator))
5421 (goto-char (point-at-bol))
5424 ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
5425 ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
5426 ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
5427 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
5429 (defun org-in-item-p ()
5430 "It the cursor inside a plain list item.
5431 Does not have to be the first line."
5435 (org-beginning-of-item)
5440 (defun org-insert-item (&optional checkbox)
5441 "Insert a new item at the current level.
5442 Return t when things worked, nil when we are not in an item."
5443 (when (save-excursion
5446 (org-beginning-of-item)
5448 (if (org-invisible-p) (error "Invisible item"))
5451 (let* ((bul (match-string 0))
5452 (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
5454 (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
5457 ((and (org-at-item-p) (<= (point) eow))
5458 ;; before the bullet
5459 (beginning-of-line 1)
5460 (open-line (if blank 2 1)))
5462 (beginning-of-line 1))
5464 (unless (org-get-alist-option org-M-RET-may-split-line 'item)
5466 (delete-horizontal-space))
5467 (newline (if blank 2 1))))
5468 (insert bul (if checkbox "[ ]" ""))
5472 (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
5473 (org-maybe-renumber-ordered-list)
5474 (and checkbox (org-update-checkbox-count-maybe))
5479 (defun org-at-item-checkbox-p ()
5480 "Is point at a line starting a plain-list item with a checklet?"
5481 (and (org-at-item-p)
5483 (goto-char (match-end 0))
5484 (skip-chars-forward " \t")
5485 (looking-at "\\[[- X]\\]"))))
5487 (defun org-toggle-checkbox (&optional arg)
5488 "Toggle the checkbox in the current line."
5491 (let (beg end status (firstnew 'unknown))
5493 ((org-region-active-p)
5494 (setq beg (region-beginning) end (region-end)))
5496 (setq beg (point) end (save-excursion (outline-next-heading) (point))))
5497 ((org-at-item-checkbox-p)
5498 (let ((pos (point)))
5501 ((member (match-string 0) '("[ ]" "[-]")) "[X]")
5506 (t (error "Not at a checkbox or heading, and no active region")))
5509 (while (< (point) end)
5510 (when (org-at-item-checkbox-p)
5511 (setq status (equal (match-string 0) "[X]"))
5512 (when (eq firstnew 'unknown)
5513 (setq firstnew (not status)))
5515 (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
5516 (beginning-of-line 2)))))
5517 (org-update-checkbox-count-maybe))
5519 (defun org-update-checkbox-count-maybe ()
5520 "Update checkbox statistics unless turned off by user."
5521 (when org-provide-checkbox-statistics
5522 (org-update-checkbox-count)))
5524 (defun org-update-checkbox-count (&optional all)
5525 "Update the checkbox statistics in the current section.
5526 This will find all statistic cookies like [57%] and [6/12] and update them
5527 with the current numbers. With optional prefix argument ALL, do this for
5531 (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
5532 (beg (condition-case nil
5533 (progn (outline-back-to-heading) (point))
5534 (error (point-min))))
5535 (end (move-marker (make-marker)
5536 (progn (outline-next-heading) (point))))
5537 (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
5538 (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
5539 (re-find (concat re "\\|" re-box))
5540 beg-cookie end-cookie is-percent c-on c-off lim
5541 eline curr-ind next-ind continue-from startsearch
5545 (goto-char (point-min))
5546 (outline-next-heading)
5547 (setq beg (point) end (point-max)))
5549 ;; find each statistic cookie
5550 (while (re-search-backward re-find beg t)
5551 (setq beg-cookie (match-beginning 1)
5552 end-cookie (match-end 1)
5553 cstat (+ cstat (if end-cookie 1 0))
5554 startsearch (point-at-eol)
5555 continue-from (point-at-bol)
5556 is-percent (match-beginning 2)
5558 ((org-on-heading-p) (outline-next-heading) (point))
5559 ((org-at-item-p) (org-end-of-item) (point))
5564 ;; find first checkbox for this cookie and gather
5565 ;; statistics from all that are at this indentation level
5566 (goto-char startsearch)
5567 (if (re-search-forward re-box lim t)
5569 (org-beginning-of-item)
5570 (setq curr-ind (org-get-indentation))
5571 (setq next-ind curr-ind)
5572 (while (and (bolp) (org-at-item-p) (= curr-ind next-ind))
5573 (save-excursion (end-of-line) (setq eline (point)))
5574 (if (re-search-forward re-box eline t)
5575 (if (member (match-string 2) '("[ ]" "[-]"))
5576 (setq c-off (1+ c-off))
5577 (setq c-on (1+ c-on))
5581 (setq next-ind (org-get-indentation))
5583 (goto-char continue-from)
5586 (delete-region beg-cookie end-cookie)
5587 (goto-char beg-cookie)
5590 (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
5591 (format "[%d/%d]" c-on (+ c-on c-off)))))
5592 ;; update items checkbox if it has one
5593 (when (org-at-item-p)
5594 (org-beginning-of-item)
5595 (when (and (> (+ c-on c-off) 0)
5596 (re-search-forward re-box (point-at-eol) t))
5597 (setq beg-cookie (match-beginning 2)
5598 end-cookie (match-end 2))
5599 (delete-region beg-cookie end-cookie)
5600 (goto-char beg-cookie)
5601 (cond ((= c-off 0) (insert "[X]"))
5602 ((= c-on 0) (insert "[ ]"))
5605 (goto-char continue-from))
5606 (when (interactive-p)
5607 (message "Checkbox satistics updated %s (%d places)"
5608 (if all "in entire file" "in current outline entry") cstat)))))
5610 (defun org-get-checkbox-statistics-face ()
5611 "Select the face for checkbox statistics.
5612 The face will be `org-done' when all relevant boxes are checked. Otherwise
5613 it will be `org-todo'."
5615 (if (equal (match-string 1) "100%") 'org-done 'org-todo)
5616 (if (and (> (match-end 2) (match-beginning 2))
5617 (equal (match-string 2) (match-string 3)))
5621 (defun org-get-indentation (&optional line)
5622 "Get the indentation of the current line, interpreting tabs.
5623 When LINE is given, assume it represents a line and compute its indentation."
5625 (if (string-match "^ *" (org-remove-tabs line))
5628 (beginning-of-line 1)
5629 (skip-chars-forward " \t")
5632 (defun org-remove-tabs (s &optional width)
5633 "Replace tabulators in S with spaces.
5634 Assumes that s is a single line, starting in column 0."
5635 (setq width (or width tab-width))
5636 (while (string-match "\t" s)
5637 (setq s (replace-match
5639 (- (* width (/ (+ (match-beginning 0) width) width))
5640 (match-beginning 0)) ?\ )
5644 (defun org-fix-indentation (line ind)
5645 "Fix indentation in LINE.
5646 IND is a cons cell with target and minimum indentation.
5647 If the current indenation in LINE is smaller than the minimum,
5648 leave it alone. If it is larger than ind, set it to the target."
5649 (let* ((l (org-remove-tabs line))
5650 (i (org-get-indentation l))
5651 (i1 (car ind)) (i2 (cdr ind)))
5652 (if (>= i i2) (setq l (substring line i2)))
5654 (concat (make-string i1 ?\ ) l)
5657 (defcustom org-empty-line-terminates-plain-lists nil
5658 "Non-nil means, an empty line ends all plain list levels.
5659 When nil, empty lines are part of the preceeding item."
5660 :group 'org-plain-lists
5663 (defun org-beginning-of-item ()
5664 "Go to the beginning of the current hand-formatted item.
5665 If the cursor is not in an item, throw an error."
5668 (limit (save-excursion
5671 (org-back-to-heading)
5672 (beginning-of-line 2) (point))
5673 (error (point-min)))))
5674 (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
5677 (beginning-of-line 1)
5678 (beginning-of-line 1)
5679 (skip-chars-forward " \t")
5680 (setq ind (current-column))
5683 (beginning-of-line 0)
5684 (if (or (bobp) (< (point) limit)) (throw 'exit nil))
5686 (if (looking-at "[ \t]*$")
5687 (setq ind1 ind-empty)
5688 (skip-chars-forward " \t")
5689 (setq ind1 (current-column)))
5691 (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
5694 (error "Not in an item")))))
5696 (defun org-end-of-item ()
5697 "Go to the end of the current hand-formatted item.
5698 If the cursor is not in an item, throw an error."
5700 (let* ((pos (point))
5702 (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
5703 (limit (save-excursion (outline-next-heading) (point)))
5704 (ind (save-excursion
5705 (org-beginning-of-item)
5706 (skip-chars-forward " \t")
5710 (beginning-of-line 2)
5711 (if (eobp) (throw 'exit (point)))
5712 (if (>= (point) limit) (throw 'exit (point-at-bol)))
5713 (if (looking-at "[ \t]*$")
5714 (setq ind1 ind-empty)
5715 (skip-chars-forward " \t")
5716 (setq ind1 (current-column)))
5718 (throw 'exit (point-at-bol)))))))
5722 (error "Not in an item"))))
5724 (defun org-next-item ()
5725 "Move to the beginning of the next item in the current plain list.
5726 Error if not at a plain list, or if this is the last item in the list."
5728 (let (ind ind1 (pos (point)))
5729 (org-beginning-of-item)
5730 (setq ind (org-get-indentation))
5732 (setq ind1 (org-get-indentation))
5733 (unless (and (org-at-item-p) (= ind ind1))
5735 (error "On last item"))))
5737 (defun org-previous-item ()
5738 "Move to the beginning of the previous item in the current plain list.
5739 Error if not at a plain list, or if this is the first item in the list."
5741 (let (beg ind ind1 (pos (point)))
5742 (org-beginning-of-item)
5744 (setq ind (org-get-indentation))
5748 (beginning-of-line 0)
5749 (if (looking-at "[ \t]*$")
5751 (if (<= (setq ind1 (org-get-indentation)) ind)
5754 (if (or (not (org-at-item-p))
5757 (org-beginning-of-item))
5758 (error (goto-char pos)
5759 (error "On first item")))))
5761 (defun org-first-list-item-p ()
5762 "Is this heading the item in a plain list?"
5763 (unless (org-at-item-p)
5764 (error "Not at a plain list item"))
5765 (org-beginning-of-item)
5766 (= (point) (save-excursion (org-beginning-of-item-list))))
5768 (defun org-move-item-down ()
5769 "Move the plain list item at point down, i.e. swap with following item.
5770 Subitems (items with larger indentation) are considered part of the item,
5771 so this really moves item trees."
5773 (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
5774 (org-beginning-of-item)
5777 (setq ne-beg (org-back-over-empty-lines))
5780 (setq ind (org-get-indentation))
5783 (setq ind1 (org-get-indentation))
5784 (setq ne-end (org-back-over-empty-lines))
5787 (when (and (org-first-list-item-p) (< ne-end ne-beg))
5788 ;; include less whitespace
5791 (forward-line (- ne-beg ne-end))
5792 (setq beg (point))))
5794 (if (and (org-at-item-p) (= ind ind1))
5797 (org-back-over-empty-lines)
5798 (setq txt (buffer-substring beg end))
5800 (delete-region beg end))
5803 (goto-char pos) (org-skip-whitespace)
5804 (org-maybe-renumber-ordered-list))
5806 (error "Cannot move this item further down"))))
5808 (defun org-move-item-up (arg)
5809 "Move the plain list item at point up, i.e. swap with previous item.
5810 Subitems (items with larger indentation) are considered part of the item,
5811 so this really moves item trees."
5813 (let (beg beg0 end ind ind1 (pos (point)) txt
5814 ne-beg ne-ins ins-end)
5815 (org-beginning-of-item)
5817 (setq ind (org-get-indentation))
5819 (setq ne-beg (org-back-over-empty-lines))
5827 (beginning-of-line 0)
5828 (if (looking-at "[ \t]*$")
5829 (if org-empty-line-terminates-plain-lists
5832 (error "Cannot move this item further up"))
5834 (if (<= (setq ind1 (org-get-indentation)) ind)
5837 (org-beginning-of-item)
5838 (error (goto-char beg)
5839 (error "Cannot move this item further up")))
5840 (setq ind1 (org-get-indentation))
5841 (if (and (org-at-item-p) (= ind ind1))
5843 (setq ne-ins (org-back-over-empty-lines))
5844 (setq txt (buffer-substring beg end))
5846 (delete-region beg end))
5849 (setq ins-end (point))
5850 (goto-char pos) (org-skip-whitespace)
5852 (when (and (org-first-list-item-p) (> ne-ins ne-beg))
5853 ;; Move whitespace back to beginning
5856 (let ((kill-whole-line t))
5857 (kill-line (- ne-ins ne-beg)) (point)))
5858 (insert (make-string (- ne-ins ne-beg) ?\n)))
5860 (org-maybe-renumber-ordered-list))
5862 (error "Cannot move this item further up"))))
5864 (defun org-maybe-renumber-ordered-list ()
5865 "Renumber the ordered list at point if setup allows it.
5866 This tests the user option `org-auto-renumber-ordered-lists' before
5867 doing the renumbering."
5869 (when (and org-auto-renumber-ordered-lists
5871 (if (match-beginning 3)
5872 (org-renumber-ordered-list 1)
5873 (org-fix-bullet-type))))
5875 (defun org-maybe-renumber-ordered-list-safe ()
5878 (org-maybe-renumber-ordered-list))
5881 (defun org-cycle-list-bullet (&optional which)
5882 "Cycle through the different itemize/enumerate bullets.
5883 This cycle the entire list level through the sequence:
5885 `-' -> `+' -> `*' -> `1.' -> `1)'
5887 If WHICH is a string, use that as the new bullet. If WHICH is an integer,
5888 0 meand `-', 1 means `+' etc."
5891 (org-beginning-of-item-list)
5893 (beginning-of-line 1)
5894 (let ((current (match-string 0))
5895 (prevp (eq which 'previous))
5898 ((and (numberp which)
5899 (nth (1- which) '("-" "+" "*" "1." "1)"))))
5900 ((string-match "-" current) (if prevp "1)" "+"))
5901 ((string-match "\\+" current)
5902 (if prevp "-" (if (looking-at "\\S-") "1." "*")))
5903 ((string-match "\\*" current) (if prevp "+" "1."))
5904 ((string-match "\\." current) (if prevp "*" "1)"))
5905 ((string-match ")" current) (if prevp "1." "-"))
5906 (t (error "This should not happen"))))
5907 (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
5908 (org-fix-bullet-type)
5909 (org-maybe-renumber-ordered-list))))
5911 (defun org-get-string-indentation (s)
5912 "What indentation has S due to SPACE and TAB at the beginning of the string?"
5913 (let ((n -1) (i 0) (w tab-width) c)
5915 (while (< (setq n (1+ n)) (length s))
5917 (cond ((= c ?\ ) (setq i (1+ i)))
5918 ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
5919 (t (throw 'exit t)))))
5922 (defun org-renumber-ordered-list (arg)
5923 "Renumber an ordered plain list.
5924 Cursor needs to be in the first line of an item, the line that starts
5925 with something like \"1.\" or \"2)\"."
5927 (unless (and (org-at-item-p)
5928 (match-beginning 3))
5929 (error "This is not an ordered list"))
5930 (let ((line (org-current-line))
5931 (col (current-column))
5932 (ind (org-get-string-indentation
5933 (buffer-substring (point-at-bol) (match-beginning 3))))
5934 ;; (term (substring (match-string 3) -1))
5937 ;; find where this list begins
5938 (org-beginning-of-item-list)
5939 (looking-at "[ \t]*[0-9]+\\([.)]\\)")
5940 (setq fmt (concat "%d" (match-string 1)))
5941 (beginning-of-line 0)
5942 ;; walk forward and replace these numbers
5946 (beginning-of-line 2)
5947 (if (eobp) (throw 'exit nil))
5948 (if (looking-at "[ \t]*$") (throw 'next nil))
5949 (skip-chars-forward " \t") (setq ind1 (current-column))
5950 (if (> ind1 ind) (throw 'next t))
5951 (if (< ind1 ind) (throw 'exit t))
5952 (if (not (org-at-item-p)) (throw 'exit nil))
5953 (delete-region (match-beginning 2) (match-end 2))
5954 (goto-char (match-beginning 2))
5955 (insert (format fmt (setq n (1+ n)))))))
5957 (move-to-column col)))
5959 (defun org-fix-bullet-type ()
5960 "Make sure all items in this list have the same bullet as the firsst item."
5962 (unless (org-at-item-p) (error "This is not a list"))
5963 (let ((line (org-current-line))
5964 (col (current-column))
5965 (ind (current-indentation))
5967 ;; find where this list begins
5968 (org-beginning-of-item-list)
5969 (beginning-of-line 1)
5970 ;; find out what the bullet type is
5971 (looking-at "[ \t]*\\(\\S-+\\)")
5972 (setq bullet (match-string 1))
5973 ;; walk forward and replace these numbers
5974 (beginning-of-line 0)
5978 (beginning-of-line 2)
5979 (if (eobp) (throw 'exit nil))
5980 (if (looking-at "[ \t]*$") (throw 'next nil))
5981 (skip-chars-forward " \t") (setq ind1 (current-column))
5982 (if (> ind1 ind) (throw 'next t))
5983 (if (< ind1 ind) (throw 'exit t))
5984 (if (not (org-at-item-p)) (throw 'exit nil))
5985 (skip-chars-forward " \t")
5986 (looking-at "\\S-+")
5987 (replace-match bullet))))
5989 (move-to-column col)
5990 (if (string-match "[0-9]" bullet)
5991 (org-renumber-ordered-list 1))))
5993 (defun org-beginning-of-item-list ()
5994 "Go to the beginning of the current item list.
5995 I.e. to the first item in this list."
5997 (org-beginning-of-item)
5998 (let ((pos (point-at-bol))
5999 (ind (org-get-indentation))
6001 ;; find where this list begins
6005 (beginning-of-line 0)
6006 (if (looking-at "[ \t]*$")
6007 (throw (if (bobp) 'exit 'next) t))
6008 (skip-chars-forward " \t") (setq ind1 (current-column))
6009 (if (or (< ind1 ind)
6011 (not (org-at-item-p)))
6014 (when (org-at-item-p) (setq pos (point-at-bol)))))))
6018 (defun org-end-of-item-list ()
6019 "Go to the end of the current item list.
6020 I.e. to the text after the last item."
6022 (org-beginning-of-item)
6023 (let ((pos (point-at-bol))
6024 (ind (org-get-indentation))
6026 ;; find where this list begins
6030 (beginning-of-line 2)
6031 (if (looking-at "[ \t]*$")
6032 (throw (if (eobp) 'exit 'next) t))
6033 (skip-chars-forward " \t") (setq ind1 (current-column))
6034 (if (or (< ind1 ind)
6036 (not (org-at-item-p)))
6039 (setq pos (point-at-bol))
6040 (throw 'exit t))))))
6044 (defvar org-last-indent-begin-marker (make-marker))
6045 (defvar org-last-indent-end-marker (make-marker))
6047 (defun org-outdent-item (arg)
6048 "Outdent a local list item."
6050 (org-indent-item (- arg)))
6052 (defun org-indent-item (arg)
6053 "Indent a local list item."
6055 (unless (org-at-item-p)
6056 (error "Not on an item"))
6058 (let (beg end ind ind1 tmp delta ind-down ind-up)
6059 (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
6060 (setq beg org-last-indent-begin-marker
6061 end org-last-indent-end-marker)
6062 (org-beginning-of-item)
6063 (setq beg (move-marker org-last-indent-begin-marker (point)))
6065 (setq end (move-marker org-last-indent-end-marker (point))))
6067 (setq tmp (org-item-indent-positions)
6069 ind-down (nth 2 tmp)
6072 (if ind-down (- ind-down ind) 2)
6073 (if ind-up (- ind-up ind) -2)))
6074 (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
6075 (while (< (point) end)
6076 (beginning-of-line 1)
6077 (skip-chars-forward " \t") (setq ind1 (current-column))
6078 (delete-region (point-at-bol) (point))
6079 (or (eolp) (indent-to-column (+ ind1 delta)))
6080 (beginning-of-line 2))))
6081 (org-fix-bullet-type)
6082 (org-maybe-renumber-ordered-list-safe)
6084 (beginning-of-line 0)
6085 (condition-case nil (org-beginning-of-item) (error nil))
6086 (org-maybe-renumber-ordered-list-safe)))
6088 (defun org-item-indent-positions ()
6089 "Return indentation for plain list items.
6090 This returns a list with three values: The current indentation, the
6091 parent indentation and the indentation a child should habe.
6092 Assumes cursor in item line."
6093 (let* ((bolpos (point-at-bol))
6094 (ind (org-get-indentation))
6095 ind-down ind-up pos)
6097 (org-beginning-of-item-list)
6098 (skip-chars-backward "\n\r \t")
6099 (when (org-in-item-p)
6100 (org-beginning-of-item)
6101 (setq ind-up (org-get-indentation))))
6105 ((and (condition-case nil (progn (org-previous-item) t)
6107 (or (forward-char 1) t)
6108 (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
6109 (setq ind-down (org-get-indentation)))
6110 ((and (goto-char pos)
6112 (goto-char (match-end 0))
6113 (skip-chars-forward " \t")
6114 (setq ind-down (current-column)))))
6115 (list ind ind-up ind-down)))
6117 ;;; The orgstruct minor mode
6119 ;; Define a minor mode which can be used in other modes in order to
6120 ;; integrate the org-mode structure editing commands.
6122 ;; This is really a hack, because the org-mode structure commands use
6123 ;; keys which normally belong to the major mode. Here is how it
6124 ;; works: The minor mode defines all the keys necessary to operate the
6125 ;; structure commands, but wraps the commands into a function which
6126 ;; tests if the cursor is currently at a headline or a plain list
6127 ;; item. If that is the case, the structure command is used,
6128 ;; temporarily setting many Org-mode variables like regular
6129 ;; expressions for filling etc. However, when any of those keys is
6130 ;; used at a different location, function uses `key-binding' to look
6131 ;; up if the key has an associated command in another currently active
6132 ;; keymap (minor modes, major mode, global), and executes that
6133 ;; command. There might be problems if any of the keys is otherwise
6134 ;; used as a prefix key.
6136 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
6137 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
6138 ;; addresses this by checking explicitly for both bindings.
6140 (defvar orgstruct-mode-map (make-sparse-keymap)
6141 "Keymap for the minor `orgstruct-mode'.")
6143 (defvar org-local-vars nil
6144 "List of local variables, for use by `orgstruct-mode'")
6147 (define-minor-mode orgstruct-mode
6148 "Toggle the minor more `orgstruct-mode'.
6149 This mode is for using Org-mode structure commands in other modes.
6150 The following key behave as if Org-mode was active, if the cursor
6151 is on a headline, or on a plain list item (both in the definition
6154 M-up Move entry/item up
6155 M-down Move entry/item down
6158 M-S-up Move entry/item up
6159 M-S-down Move entry/item down
6160 M-S-left Promote subtree
6161 M-S-right Demote subtree
6162 M-q Fill paragraph and items like in Org-mode
6164 C-c - Cycle list bullet
6165 TAB Cycle item visibility
6166 M-RET Insert new heading/item
6167 S-M-RET Insert new TODO heading / Chekbox item
6168 C-c C-c Set tags / toggle checkbox"
6169 nil " OrgStruct" nil
6170 (org-load-modules-maybe)
6171 (and (orgstruct-setup) (defun orgstruct-setup () nil)))
6174 (defun turn-on-orgstruct ()
6175 "Unconditionally turn on `orgstruct-mode'."
6179 (defun turn-on-orgstruct++ ()
6180 "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
6181 In addition to setting orgstruct-mode, this also exports all indentation and
6182 autofilling variables from org-mode into the buffer. Note that turning
6183 off orgstruct-mode will *not* remove these additional settings."
6189 "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
6190 (symbol-name (car x)))
6191 (setq var (car x) val (nth 1 x))
6192 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
6195 (defun orgstruct-error ()
6196 "Error when there is no default binding for a structure key."
6198 (error "This key has no function outside structure elements"))
6200 (defun orgstruct-setup ()
6201 "Setup orgstruct keymaps."
6205 '([(meta up)] org-metaup)
6206 '([(meta down)] org-metadown)
6207 '([(meta left)] org-metaleft)
6208 '([(meta right)] org-metaright)
6209 '([(meta shift up)] org-shiftmetaup)
6210 '([(meta shift down)] org-shiftmetadown)
6211 '([(meta shift left)] org-shiftmetaleft)
6212 '([(meta shift right)] org-shiftmetaright)
6213 '([(shift up)] org-shiftup)
6214 '([(shift down)] org-shiftdown)
6215 '("\C-c\C-c" org-ctrl-c-ctrl-c)
6216 '("\M-q" fill-paragraph)
6218 '("\C-c-" org-cycle-list-bullet)))
6220 (while (setq elt (pop bindings))
6221 (setq nfunc (1+ nfunc))
6222 (setq key (org-key (car elt))
6224 cmd (orgstruct-make-binding fun nfunc key))
6225 (org-defkey orgstruct-mode-map key cmd))
6227 ;; Special treatment needed for TAB and RET
6228 (org-defkey orgstruct-mode-map [(tab)]
6229 (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
6230 (org-defkey orgstruct-mode-map "\C-i"
6231 (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
6233 (org-defkey orgstruct-mode-map "\M-\C-m"
6234 (orgstruct-make-binding 'org-insert-heading 105
6235 "\M-\C-m" [(meta return)]))
6236 (org-defkey orgstruct-mode-map [(meta return)]
6237 (orgstruct-make-binding 'org-insert-heading 106
6238 [(meta return)] "\M-\C-m"))
6240 (org-defkey orgstruct-mode-map [(shift meta return)]
6241 (orgstruct-make-binding 'org-insert-todo-heading 107
6242 [(meta return)] "\M-\C-m"))
6244 (unless org-local-vars
6245 (setq org-local-vars (org-get-local-variables)))
6249 (defun orgstruct-make-binding (fun n &rest keys)
6250 "Create a function for binding in the structure minor mode.
6251 FUN is the command to call inside a table. N is used to create a unique
6252 command name. KEYS are keys that should be checked in for a command
6253 to execute outside of tables."
6256 (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
6258 (concat "In Structure, run `" (symbol-name fun) "'.\n"
6259 "Outside of structure, run the binding of `"
6260 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
6264 '(org-context-p 'headline 'item)
6265 (list 'org-run-like-in-org-mode (list 'quote fun))
6266 (list 'let '(orgstruct-mode)
6267 (list 'call-interactively
6270 (list 'key-binding k))
6272 '('orgstruct-error))))))))
6274 (defun org-context-p (&rest contexts)
6275 "Check if local context is and of CONTEXTS.
6276 Possible values in the list of contexts are `table', `headline', and `item'."
6277 (let ((pos (point)))
6278 (goto-char (point-at-bol))
6279 (prog1 (or (and (memq 'table contexts)
6280 (looking-at "[ \t]*|"))
6281 (and (memq 'headline contexts)
6282 (looking-at "\\*+"))
6283 (and (memq 'item contexts)
6284 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
6287 (defun org-get-local-variables ()
6288 "Return a list of all local variables in an org-mode buffer."
6290 (with-current-buffer (get-buffer-create "*Org tmp*")
6293 (setq varlist (buffer-local-variables)))
6294 (kill-buffer "*Org tmp*")
6301 (list (car x) (list 'quote (cdr x)))))
6303 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
6304 (symbol-name (car x)))
6309 (defun org-run-like-in-org-mode (cmd)
6310 (org-load-modules-maybe)
6311 (unless org-local-vars
6312 (setq org-local-vars (org-get-local-variables)))
6313 (eval (list 'let org-local-vars
6314 (list 'call-interactively (list 'quote cmd)))))
6318 (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
6320 (defun org-archive-subtree (&optional find-done)
6321 "Move the current subtree to the archive.
6322 The archive can be a certain top-level heading in the current file, or in
6323 a different file. The tree will be moved to that location, the subtree
6324 heading be marked DONE, and the current time will be added.
6326 When called with prefix argument FIND-DONE, find whole trees without any
6327 open TODO items and archive them (after getting confirmation from the user).
6328 If the cursor is not at a headline when this comand is called, try all level
6329 1 trees. If the cursor is on a headline, only try the direct children of
6333 (org-archive-all-done)
6334 ;; Save all relevant TODO keyword-relatex variables
6336 (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
6337 (tr-org-todo-keywords-1 org-todo-keywords-1)
6338 (tr-org-todo-kwd-alist org-todo-kwd-alist)
6339 (tr-org-done-keywords org-done-keywords)
6340 (tr-org-todo-regexp org-todo-regexp)
6341 (tr-org-todo-line-regexp org-todo-line-regexp)
6342 (tr-org-odd-levels-only org-odd-levels-only)
6343 (this-buffer (current-buffer))
6344 (org-archive-location org-archive-location)
6345 (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
6346 ;; start of variables that will be used for saving context
6347 ;; The compiler complains about them - keep them anyway!
6348 (file (abbreviate-file-name (buffer-file-name)))
6349 (olpath (mapconcat 'identity (org-get-outline-path) "/"))
6350 (time (format-time-string
6351 (substring (cdr org-time-stamp-formats) 1 -1)
6353 afile heading buffer level newfile-p
6354 category todo priority
6355 ;; start of variables that will be used for savind context
6358 ;; Try to find a local archive location
6362 (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
6363 (if (and prop (string-match "\\S-" prop))
6364 (setq org-archive-location prop)
6365 (if (or (re-search-backward re nil t)
6366 (re-search-forward re nil t))
6367 (setq org-archive-location (match-string 1))))))
6369 (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
6371 (setq afile (format (match-string 1 org-archive-location)
6372 (file-name-nondirectory buffer-file-name))
6373 heading (match-string 2 org-archive-location)))
6374 (error "Invalid `org-archive-location'"))
6375 (if (> (length afile) 0)
6376 (setq newfile-p (not (file-exists-p afile))
6377 buffer (find-file-noselect afile))
6378 (setq buffer (current-buffer)))
6380 (error "Cannot access file \"%s\"" afile))
6381 (if (and (> (length heading) 0)
6382 (string-match "^\\*+" heading))
6383 (setq level (match-end 0))
6384 (setq heading nil level 0))
6386 (org-back-to-heading t)
6387 ;; Get context information that will be lost by moving the tree
6388 (org-refresh-category-properties)
6389 (setq category (org-get-category)
6390 todo (and (looking-at org-todo-line-regexp)
6392 priority (org-get-priority (if (match-end 3) (match-string 3) ""))
6393 ltags (org-get-tags)
6394 itags (org-delete-all ltags (org-get-tags-at)))
6395 (setq ltags (mapconcat 'identity ltags " ")
6396 itags (mapconcat 'identity itags " "))
6397 ;; We first only copy, in case something goes wrong
6398 ;; we need to protect this-command, to avoid kill-region sets it,
6399 ;; which would lead to duplication of subtrees
6400 (let (this-command) (org-copy-subtree))
6402 ;; Enforce org-mode for the archive buffer
6403 (if (not (org-mode-p))
6404 ;; Force the mode for future visits.
6405 (let ((org-insert-mode-line-in-empty-file t)
6406 (org-inhibit-startup t))
6407 (call-interactively 'org-mode)))
6409 (goto-char (point-max))
6410 (insert (format "\nArchived entries from file %s\n\n"
6411 (buffer-file-name this-buffer))))
6412 ;; Force the TODO keywords of the original buffer
6413 (let ((org-todo-line-regexp tr-org-todo-line-regexp)
6414 (org-todo-keywords-1 tr-org-todo-keywords-1)
6415 (org-todo-kwd-alist tr-org-todo-kwd-alist)
6416 (org-done-keywords tr-org-done-keywords)
6417 (org-todo-regexp tr-org-todo-regexp)
6418 (org-todo-line-regexp tr-org-todo-line-regexp)
6419 (org-odd-levels-only
6420 (if (local-variable-p 'org-odd-levels-only (current-buffer))
6422 tr-org-odd-levels-only)))
6423 (goto-char (point-min))
6427 (if (re-search-forward
6428 (concat "^" (regexp-quote heading)
6429 (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
6431 (goto-char (match-end 0))
6432 ;; Heading not found, just insert it at the end
6433 (goto-char (point-max))
6434 (or (bolp) (insert "\n"))
6435 (insert "\n" heading "\n")
6437 ;; Make the subtree visible
6439 (org-end-of-subtree t)
6440 (skip-chars-backward " \t\r\n")
6441 (and (looking-at "[ \t\r\n]*")
6442 (replace-match "\n\n")))
6443 ;; No specific heading, just go to end of file.
6444 (goto-char (point-max)) (insert "\n"))
6446 (org-paste-subtree (org-get-valid-level level 1))
6448 ;; Mark the entry as done
6449 (when (and org-archive-mark-done
6450 (looking-at org-todo-line-regexp)
6451 (or (not (match-end 2))
6452 (not (member (match-string 2) org-done-keywords))))
6453 (let (org-log-done org-todo-log-states)
6455 (car (or (member org-archive-mark-done org-done-keywords)
6456 org-done-keywords)))))
6458 ;; Add the context info
6459 (when org-archive-save-context-info
6460 (let ((l org-archive-save-context-info) e n v)
6461 (while (setq e (pop l))
6462 (when (and (setq v (symbol-value e))
6463 (stringp v) (string-match "\\S-" v))
6464 (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
6465 (org-entry-put (point) n v)))))
6467 ;; Save and kill the buffer, if it is not the same buffer.
6468 (if (not (eq this-buffer buffer))
6469 (progn (save-buffer) (kill-buffer buffer)))))
6470 ;; Here we are back in the original buffer. Everything seems to have
6471 ;; worked. So now cut the tree and finish up.
6472 (let (this-command) (org-cut-subtree))
6473 (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
6474 (message "Subtree archived %s"
6475 (if (eq this-buffer buffer)
6476 (concat "under heading: " heading)
6477 (concat "in file: " (abbreviate-file-name afile)))))))
6479 (defun org-get-category (&optional pos)
6480 "Get the category applying to position POS."
6481 (get-text-property (or pos (point)) 'org-category))
6483 (defun org-refresh-category-properties ()
6484 "Refresh category text properties in the buffer."
6485 (let ((def-cat (cond
6486 ((null org-category)
6487 (if buffer-file-name
6488 (file-name-sans-extension
6489 (file-name-nondirectory buffer-file-name))
6491 ((symbolp org-category) (symbol-name org-category))
6493 beg end cat pos optionp)
6498 (goto-char (point-min))
6499 (put-text-property (point) (point-max) 'org-category def-cat)
6500 (while (re-search-forward
6501 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
6502 (setq pos (match-end 0)
6503 optionp (equal (char-after (match-beginning 0)) ?#)
6504 cat (org-trim (match-string 2)))
6506 (setq beg (point-at-bol) end (point-max))
6507 (org-back-to-heading t)
6508 (setq beg (point) end (org-end-of-subtree t t)))
6509 (put-text-property beg end 'org-category cat)
6510 (goto-char pos)))))))
6512 (defun org-archive-all-done (&optional tag)
6513 "Archive sublevels of the current tree without open TODO items.
6514 If the cursor is not on a headline, try all level 1 trees. If
6515 it is on a headline, try all direct children.
6516 When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
6517 (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
6518 (rea (concat ".*:" org-archive-tag ":"))
6519 (begm (make-marker))
6520 (endm (make-marker))
6521 (question (if tag "Set ARCHIVE tag (no open TODO items)? "
6522 "Move subtree to archive (no open TODO items)? "))
6523 beg end (cntarch 0))
6524 (if (org-on-heading-p)
6526 (setq re1 (concat "^" (regexp-quote
6528 (1+ (- (match-end 0) (match-beginning 0) 1))
6531 (move-marker begm (point))
6532 (move-marker endm (org-end-of-subtree t)))
6534 (move-marker begm (point-min))
6535 (move-marker endm (point-max)))
6538 (while (re-search-forward re1 endm t)
6539 (setq beg (match-beginning 0)
6540 end (save-excursion (org-end-of-subtree t) (point)))
6542 (if (re-search-forward re end t)
6545 (if (and (or (not tag) (not (looking-at rea)))
6546 (y-or-n-p question))
6549 (org-toggle-tag org-archive-tag 'on)
6550 (org-archive-subtree))
6551 (setq cntarch (1+ cntarch)))
6553 (message "%d trees archived" cntarch)))
6555 (defun org-cycle-hide-drawers (state)
6556 "Re-hide all drawers after a visibility state change."
6557 (when (and (org-mode-p)
6558 (not (memq state '(overview folded))))
6560 (let* ((globalp (memq state '(contents all)))
6561 (beg (if globalp (point-min) (point)))
6562 (end (if globalp (point-max) (org-end-of-subtree t))))
6564 (while (re-search-forward org-drawer-regexp end t)
6565 (org-flag-drawer t))))))
6567 (defun org-flag-drawer (flag)
6569 (beginning-of-line 1)
6570 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
6571 (let ((b (match-end 0))
6572 (outline-regexp org-outline-regexp))
6573 (if (re-search-forward
6575 (save-excursion (outline-next-heading) (point)) t)
6576 (outline-flag-region b (point-at-eol) flag)
6577 (error ":END: line missing"))))))
6579 (defun org-cycle-hide-archived-subtrees (state)
6580 "Re-hide all archived subtrees after a visibility state change."
6581 (when (and (not org-cycle-open-archived-trees)
6582 (not (memq state '(overview folded))))
6584 (let* ((globalp (memq state '(contents all)))
6585 (beg (if globalp (point-min) (point)))
6586 (end (if globalp (point-max) (org-end-of-subtree t))))
6587 (org-hide-archived-subtrees beg end)
6589 (if (looking-at (concat ".*:" org-archive-tag ":"))
6590 (message "%s" (substitute-command-keys
6591 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
6593 (defun org-force-cycle-archived ()
6594 "Cycle subtree even if it is archived."
6596 (setq this-command 'org-cycle)
6597 (let ((org-cycle-open-archived-trees t))
6598 (call-interactively 'org-cycle)))
6600 (defun org-hide-archived-subtrees (beg end)
6601 "Re-hide all archived subtrees after a visibility state change."
6603 (let* ((re (concat ":" org-archive-tag ":")))
6605 (while (re-search-forward re end t)
6606 (and (org-on-heading-p) (hide-subtree))
6607 (org-end-of-subtree t)))))
6609 (defun org-toggle-tag (tag &optional onoff)
6610 "Toggle the tag TAG for the current line.
6611 If ONOFF is `on' or `off', don't toggle but set to this state."
6612 (unless (org-on-heading-p t) (error "Not on headling"))
6616 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
6619 (setq current (match-string 1))
6622 (setq current (nreverse (org-split-string current ":")))
6626 (or (member tag current) (push tag current)))
6628 (or (not (member tag current)) (setq current (delete tag current))))
6629 (t (if (member tag current)
6630 (setq current (delete tag current))
6632 (push tag current))))
6636 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
6637 (org-set-tags nil t))
6638 (delete-horizontal-space))
6639 (run-hooks 'org-after-tags-change-hook))
6642 (defun org-toggle-archive-tag (&optional arg)
6643 "Toggle the archive tag for the current headline.
6644 With prefix ARG, check all children of current headline and offer tagging
6645 the children that do not contain any open TODO items."
6648 (org-archive-all-done 'tag)
6651 (org-back-to-heading t)
6652 (setq set (org-toggle-tag org-archive-tag))
6653 (when set (hide-subtree)))
6654 (and set (beginning-of-line 1))
6655 (message "Subtree %s" (if set "archived" "unarchived")))))
6659 ;;; Link abbreviations
6661 (defun org-link-expand-abbrev (link)
6662 "Apply replacements as defined in `org-link-abbrev-alist."
6663 (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
6664 (let* ((key (match-string 1 link))
6665 (as (or (assoc key org-link-abbrev-alist-local)
6666 (assoc key org-link-abbrev-alist)))
6667 (tag (and (match-end 2) (match-string 3 link)))
6673 ((symbolp rpl) (funcall rpl tag))
6674 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
6675 (t (concat rpl tag)))))
6678 ;;; Storing and inserting links
6680 (defvar org-insert-link-history nil
6681 "Minibuffer history for links inserted with `org-insert-link'.")
6683 (defvar org-stored-links nil
6684 "Contains the links stored with `org-store-link'.")
6686 (defvar org-store-link-plist nil
6687 "Plist with info about the most recently link created with `org-store-link'.")
6689 (defvar org-link-protocols nil
6690 "Link protocols added to Org-mode using `org-add-link-type'.")
6692 (defvar org-store-link-functions nil
6693 "List of functions that are called to create and store a link.
6694 Each function will be called in turn until one returns a non-nil
6695 value. Each function should check if it is responsible for creating
6696 this link (for example by looking at the major mode).
6697 If not, it must exit and return nil.
6698 If yes, it should return a non-nil value after a calling
6699 `org-store-link-props' with a list of properties and values.
6700 Special properties are:
6702 :type The link prefix. like \"http\". This must be given.
6703 :link The link, like \"http://www.astro.uva.nl/~dominik\".
6704 This is obligatory as well.
6705 :description Optional default description for the second pair
6706 of brackets in an Org-mode link. The user can still change
6707 this when inserting this link into an Org-mode buffer.
6709 In addition to these, any additional properties can be specified
6710 and then used in remember templates.")
6712 (defun org-add-link-type (type &optional follow export)
6713 "Add TYPE to the list of `org-link-types'.
6714 Re-compute all regular expressions depending on `org-link-types'
6716 FOLLOW and EXPORT are two functions.
6718 FOLLOW should take the link path as the single argument and do whatever
6719 is necessary to follow the link, for example find a file or display
6722 EXPORT should format the link path for export to one of the export formats.
6723 It should be a function accepting three arguments:
6725 path the path of the link, the text after the prefix (like \"http:\")
6726 desc the description of the link, if any, nil if there was no descripton
6727 format the export format, a symbol like `html' or `latex'.
6729 The function may use the FORMAT information to return different values
6730 depending on the format. The return value will be put literally into
6732 Org-mode has a built-in default for exporting links. If you are happy with
6733 this default, there is no need to define an export function for the link
6734 type. For a simple example of an export function, see `org-bbdb.el'."
6735 (add-to-list 'org-link-types type t)
6736 (org-make-link-regexps)
6737 (if (assoc type org-link-protocols)
6738 (setcdr (assoc type org-link-protocols) (list follow export))
6739 (push (list type follow export) org-link-protocols)))
6743 (defun org-store-link (arg)
6744 "\\<org-mode-map>Store an org-link to the current location.
6745 This link is added to `org-stored-links' and can later be inserted
6746 into an org-buffer with \\[org-insert-link].
6748 For some link types, a prefix arg is interpreted:
6749 For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
6750 For file links, arg negates `org-context-in-file-links'."
6752 (org-load-modules-maybe)
6753 (setq org-store-link-plist nil) ; reset
6754 (let (link cpltxt desc description search txt)
6757 ((run-hook-with-args-until-success 'org-store-link-functions)
6758 (setq link (plist-get org-store-link-plist :link)
6759 desc (or (plist-get org-store-link-plist :description) link)))
6761 ((eq major-mode 'calendar-mode)
6762 (let ((cd (calendar-cursor-to-date)))
6765 (car org-time-stamp-formats)
6767 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
6769 (org-store-link-props :type "calendar" :date cd)))
6771 ((eq major-mode 'w3-mode)
6772 (setq cpltxt (url-view-url t)
6773 link (org-make-link cpltxt))
6774 (org-store-link-props :type "w3" :url (url-view-url t)))
6776 ((eq major-mode 'w3m-mode)
6777 (setq cpltxt (or w3m-current-title w3m-current-url)
6778 link (org-make-link w3m-current-url))
6779 (org-store-link-props :type "w3m" :url (url-view-url t)))
6781 ((setq search (run-hook-with-args-until-success
6782 'org-create-file-search-functions))
6783 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
6785 (setq cpltxt (or description link)))
6787 ((eq major-mode 'image-mode)
6788 (setq cpltxt (concat "file:"
6789 (abbreviate-file-name buffer-file-name))
6790 link (org-make-link cpltxt))
6791 (org-store-link-props :type "image" :file buffer-file-name))
6793 ((eq major-mode 'dired-mode)
6794 ;; link to the file in the current line
6795 (setq cpltxt (concat "file:"
6796 (abbreviate-file-name
6798 (dired-get-filename nil t))))
6799 link (org-make-link cpltxt)))
6801 ((and buffer-file-name (org-mode-p))
6802 ;; Just link to current headline
6803 (setq cpltxt (concat "file:"
6804 (abbreviate-file-name buffer-file-name)))
6805 ;; Add a context search string
6806 (when (org-xor org-context-in-file-links arg)
6807 ;; Check if we are on a target
6808 (if (org-in-regexp "<<\\(.*?\\)>>")
6809 (setq cpltxt (concat cpltxt "::" (match-string 1)))
6811 ((org-on-heading-p) nil)
6812 ((org-region-active-p)
6813 (buffer-substring (region-beginning) (region-end)))
6814 (t (buffer-substring (point-at-bol) (point-at-eol)))))
6815 (when (or (null txt) (string-match "\\S-" txt))
6817 (concat cpltxt "::" (org-make-org-heading-search-string txt))
6819 (if (string-match "::\\'" cpltxt)
6820 (setq cpltxt (substring cpltxt 0 -2)))
6821 (setq link (org-make-link cpltxt)))
6823 ((buffer-file-name (buffer-base-buffer))
6824 ;; Just link to this file here.
6825 (setq cpltxt (concat "file:"
6826 (abbreviate-file-name
6827 (buffer-file-name (buffer-base-buffer)))))
6828 ;; Add a context string
6829 (when (org-xor org-context-in-file-links arg)
6830 (setq txt (if (org-region-active-p)
6831 (buffer-substring (region-beginning) (region-end))
6832 (buffer-substring (point-at-bol) (point-at-eol))))
6833 ;; Only use search option if there is some text.
6834 (when (string-match "\\S-" txt)
6836 (concat cpltxt "::" (org-make-org-heading-search-string txt))
6838 (setq link (org-make-link cpltxt)))
6841 (error "Cannot link to a buffer which is not visiting a file"))
6843 (t (setq link nil)))
6845 (if (consp link) (setq cpltxt (car link) link (cdr link)))
6846 (setq link (or link cpltxt)
6847 desc (or desc cpltxt))
6848 (if (equal desc "NONE") (setq desc nil))
6850 (if (and (interactive-p) link)
6852 (setq org-stored-links
6853 (cons (list link desc) org-stored-links))
6854 (message "Stored: %s" (or desc link)))
6855 (and link (org-make-link-string link desc)))))
6857 (defun org-store-link-props (&rest plist)
6858 "Store link properties, extract names and addresses."
6860 (when (setq x (plist-get plist :from))
6861 (setq adr (mail-extract-address-components x))
6862 (plist-put plist :fromname (car adr))
6863 (plist-put plist :fromaddress (nth 1 adr)))
6864 (when (setq x (plist-get plist :to))
6865 (setq adr (mail-extract-address-components x))
6866 (plist-put plist :toname (car adr))
6867 (plist-put plist :toaddress (nth 1 adr))))
6868 (let ((from (plist-get plist :from))
6869 (to (plist-get plist :to)))
6870 (when (and from to org-from-is-user-regexp)
6871 (plist-put plist :fromto
6872 (if (string-match org-from-is-user-regexp from)
6874 (concat "from %f")))))
6875 (setq org-store-link-plist plist))
6877 (defun org-add-link-props (&rest plist)
6878 "Add these properties to the link property list."
6881 (setq key (pop plist) value (pop plist))
6882 (setq org-store-link-plist
6883 (plist-put org-store-link-plist key value)))))
6885 (defun org-email-link-description (&optional fmt)
6886 "Return the description part of an email link.
6887 This takes information from `org-store-link-plist' and formats it
6888 according to FMT (default from `org-email-link-description-format')."
6889 (setq fmt (or fmt org-email-link-description-format))
6890 (let* ((p org-store-link-plist)
6891 (to (plist-get p :toaddress))
6892 (from (plist-get p :fromaddress))
6895 (cons "%c" (plist-get p :fromto))
6896 (cons "%F" (plist-get p :from))
6897 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
6898 (cons "%T" (plist-get p :to))
6899 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
6900 (cons "%s" (plist-get p :subject))
6901 (cons "%m" (plist-get p :message-id)))))
6902 (when (string-match "%c" fmt)
6903 ;; Check if the user wrote this message
6904 (if (and org-from-is-user-regexp from to
6905 (save-match-data (string-match org-from-is-user-regexp from)))
6906 (setq fmt (replace-match "to %t" t t fmt))
6907 (setq fmt (replace-match "from %f" t t fmt))))
6908 (org-replace-escapes fmt table)))
6910 (defun org-make-org-heading-search-string (&optional string heading)
6911 "Make search string for STRING or current headline."
6913 (let ((s (or string (org-get-heading))))
6914 (unless (and string (not heading))
6915 ;; We are using a headline, clean up garbage in there.
6916 (if (string-match org-todo-regexp s)
6917 (setq s (replace-match "" t t s)))
6918 (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
6919 (setq s (replace-match "" t t s)))
6920 (setq s (org-trim s))
6921 (if (string-match (concat "^\\(" org-quote-string "\\|"
6922 org-comment-string "\\)") s)
6923 (setq s (replace-match "" t t s)))
6924 (while (string-match org-ts-regexp s)
6925 (setq s (replace-match "" t t s))))
6926 (while (string-match "[^a-zA-Z_0-9 \t]+" s)
6927 (setq s (replace-match " " t t s)))
6928 (or string (setq s (concat "*" s))) ; Add * for headlines
6929 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
6931 (defun org-make-link (&rest strings)
6932 "Concatenate STRINGS."
6933 (apply 'concat strings))
6935 (defun org-make-link-string (link &optional description)
6936 "Make a link with brackets, consisting of LINK and DESCRIPTION."
6937 (unless (string-match "\\S-" link)
6938 (error "Empty link"))
6939 (when (stringp description)
6940 ;; Remove brackets from the description, they are fatal.
6941 (while (string-match "\\[" description)
6942 (setq description (replace-match "{" t t description)))
6943 (while (string-match "\\]" description)
6944 (setq description (replace-match "}" t t description))))
6945 (when (equal (org-link-escape link) description)
6946 ;; No description needed, it is identical
6947 (setq description nil))
6948 (when (and (not description)
6949 (not (equal link (org-link-escape link))))
6950 (setq description link))
6951 (concat "[[" (org-link-escape link) "]"
6952 (if description (concat "[" description "]") "")
6955 (defconst org-link-escape-chars
6959 (?\340 . "%E0") ; `a
6960 (?\342 . "%E2") ; ^a
6961 (?\347 . "%E7") ; ,c
6962 (?\350 . "%E8") ; `e
6963 (?\351 . "%E9") ; 'e
6964 (?\352 . "%EA") ; ^e
6965 (?\356 . "%EE") ; ^i
6966 (?\364 . "%F4") ; ^o
6967 (?\371 . "%F9") ; `u
6968 (?\373 . "%FB") ; ^u
6974 "Association list of escapes for some characters problematic in links.
6975 This is the list that is used for internal purposes.")
6977 (defconst org-link-escape-chars-browser
6978 '((?\ . "%20")) ; 32 for the SPC char
6979 "Association list of escapes for some characters problematic in links.
6980 This is the list that is used before handing over to the browser.")
6982 (defun org-link-escape (text &optional table)
6983 "Escape charaters in TEXT that are problematic for links."
6984 (setq table (or table org-link-escape-chars))
6986 (let ((re (mapconcat (lambda (x) (regexp-quote
6987 (char-to-string (car x))))
6989 (while (string-match re text)
6992 (cdr (assoc (string-to-char (match-string 0 text))
6997 (defun org-link-unescape (text &optional table)
6998 "Reverse the action of `org-link-escape'."
6999 (setq table (or table org-link-escape-chars))
7001 (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
7003 (while (string-match re text)
7006 (char-to-string (car (rassoc (match-string 0 text) table)))
7010 (defun org-xor (a b)
7014 (defun org-get-header (header)
7015 "Find a header field in the current buffer."
7017 (goto-char (point-min))
7018 (let ((case-fold-search t) s)
7021 (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
7022 (setq s (match-string 1)))
7023 (while (string-match "\"" s)
7024 (setq s (replace-match "" t t s)))
7025 (if (string-match "[<(].*" s)
7026 (setq s (replace-match "" t t s))))
7027 ((eq header 'message-id)
7028 (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
7029 (setq s (match-string 1))))
7030 ((eq header 'subject)
7031 (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
7032 (setq s (match-string 1)))))
7033 (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
7034 (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
7038 (defun org-fixup-message-id-for-http (s)
7039 "Replace special characters in a message id, so it can be used in an http query."
7040 (while (string-match "<" s)
7041 (setq s (replace-match "%3C" t t s)))
7042 (while (string-match ">" s)
7043 (setq s (replace-match "%3E" t t s)))
7044 (while (string-match "@" s)
7045 (setq s (replace-match "%40" t t s)))
7049 (defun org-insert-link-global ()
7050 "Insert a link like Org-mode does.
7051 This command can be called in any mode to insert a link in Org-mode syntax."
7053 (org-load-modules-maybe)
7054 (org-run-like-in-org-mode 'org-insert-link))
7056 (defun org-insert-link (&optional complete-file)
7057 "Insert a link. At the prompt, enter the link.
7059 Completion can be used to select a link previously stored with
7060 `org-store-link'. When the empty string is entered (i.e. if you just
7061 press RET at the prompt), the link defaults to the most recently
7062 stored link. As SPC triggers completion in the minibuffer, you need to
7063 use M-SPC or C-q SPC to force the insertion of a space character.
7065 You will also be prompted for a description, and if one is given, it will
7066 be displayed in the buffer instead of the link.
7068 If there is already a link at point, this command will allow you to edit link
7069 and description parts.
7071 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
7072 selected using completion. The path to the file will be relative to
7073 the current directory if the file is in the current directory or a
7074 subdirectory. Otherwise, the link will be the absolute path as
7075 completed in the minibuffer (i.e. normally ~/path/to/file).
7077 With two \\[universal-argument] prefixes, enforce an absolute path even if the file
7078 is in the current directory or below.
7079 With three \\[universal-argument] prefixes, negate the meaning of
7080 `org-keep-stored-link-after-insertion'."
7082 (let* ((wcf (current-window-configuration))
7083 (region (if (org-region-active-p)
7084 (buffer-substring (region-beginning) (region-end))))
7085 (remove (and region (list (region-beginning) (region-end))))
7087 tmphist ; byte-compile incorrectly complains about this
7090 ((org-in-regexp org-bracket-link-regexp 1)
7091 ;; We do have a link at point, and we are going to edit it.
7092 (setq remove (list (match-beginning 0) (match-end 0)))
7093 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
7094 (setq link (read-string "Link: "
7096 (org-match-string-no-properties 1)))))
7097 ((or (org-in-regexp org-angle-link-re)
7098 (org-in-regexp org-plain-link-re))
7099 ;; Convert to bracket link
7100 (setq remove (list (match-beginning 0) (match-end 0))
7101 link (read-string "Link: "
7102 (org-remove-angle-brackets (match-string 0)))))
7103 ((equal complete-file '(4))
7104 ;; Completing read for file names.
7105 (setq file (read-file-name "File: "))
7106 (let ((pwd (file-name-as-directory (expand-file-name ".")))
7107 (pwd1 (file-name-as-directory (abbreviate-file-name
7108 (expand-file-name ".")))))
7110 ((equal complete-file '(16))
7111 (setq link (org-make-link
7113 (abbreviate-file-name (expand-file-name file)))))
7114 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
7115 (setq link (org-make-link "file:" (match-string 1 file))))
7116 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
7117 (expand-file-name file))
7118 (setq link (org-make-link
7119 "file:" (match-string 1 (expand-file-name file)))))
7120 (t (setq link (org-make-link "file:" file))))))
7122 ;; Read link, with completion for stored links.
7123 (with-output-to-temp-buffer "*Org Links*"
7124 (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
7125 (when org-stored-links
7126 (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
7129 (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
7130 (reverse org-stored-links) "\n"))))
7131 (let ((cw (selected-window)))
7132 (select-window (get-buffer-window "*Org Links*"))
7133 (shrink-window-if-larger-than-buffer)
7134 (setq truncate-lines t)
7136 ;; Fake a link history, containing the stored links.
7137 (setq tmphist (append (mapcar 'car org-stored-links)
7138 org-insert-link-history))
7140 (setq link (org-completing-read
7143 (mapcar (lambda (x) (list (concat (car x) ":")))
7144 (append org-link-abbrev-alist-local org-link-abbrev-alist))
7145 (mapcar (lambda (x) (list (concat x ":")))
7149 (or (car (car org-stored-links)))))
7150 (set-window-configuration wcf)
7151 (kill-buffer "*Org Links*"))
7152 (setq entry (assoc link org-stored-links))
7153 (or entry (push link org-insert-link-history))
7154 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
7155 (not org-keep-stored-link-after-insertion))
7156 (setq org-stored-links (delq (assoc link org-stored-links)
7158 (setq desc (or desc (nth 1 entry)))))
7160 (if (string-match org-plain-link-re link)
7161 ;; URL-like link, normalize the use of angular brackets.
7162 (setq link (org-make-link (org-remove-angle-brackets link))))
7164 ;; Check if we are linking to the current file with a search option
7165 ;; If yes, simplify the link by using only the search option.
7166 (when (and buffer-file-name
7167 (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
7168 (let* ((path (match-string 1 link))
7169 (case-fold-search nil)
7170 (search (match-string 2 link)))
7172 (if (equal (file-truename buffer-file-name) (file-truename path))
7173 ;; We are linking to this same file, with a search option
7174 (setq link search)))))
7176 ;; Check if we can/should use a relative path. If yes, simplify the link
7177 (when (string-match "\\<file:\\(.*\\)" link)
7178 (let* ((path (match-string 1 link))
7180 (case-fold-search nil))
7182 ((eq org-link-file-path-type 'absolute)
7183 (setq path (abbreviate-file-name (expand-file-name path))))
7184 ((eq org-link-file-path-type 'noabbrev)
7185 (setq path (expand-file-name path)))
7186 ((eq org-link-file-path-type 'relative)
7187 (setq path (file-relative-name path)))
7190 (if (string-match (concat "^" (regexp-quote
7191 (file-name-as-directory
7192 (expand-file-name "."))))
7193 (expand-file-name path))
7194 ;; We are linking a file with relative path name.
7195 (setq path (substring (expand-file-name path)
7197 (setq link (concat "file:" path))
7198 (if (equal desc origpath)
7201 (setq desc (read-string "Description: " desc))
7202 (unless (string-match "\\S-" desc) (setq desc nil))
7203 (if remove (apply 'delete-region remove))
7204 (insert (org-make-link-string link desc))))
7206 (defun org-completing-read (&rest args)
7207 (let ((minibuffer-local-completion-map
7208 (copy-keymap minibuffer-local-completion-map)))
7209 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
7210 (apply 'completing-read args)))
7212 ;;; Opening/following a link
7214 (defvar org-link-search-failed nil)
7216 (defun org-next-link ()
7217 "Move forward to the next link.
7218 If the link is in hidden text, expose it."
7220 (when (and org-link-search-failed (eq this-command last-command))
7221 (goto-char (point-min))
7222 (message "Link search wrapped back to beginning of buffer"))
7223 (setq org-link-search-failed nil)
7224 (let* ((pos (point))
7226 (a (assoc :link ct)))
7227 (if a (goto-char (nth 2 a)))
7228 (if (re-search-forward org-any-link-re nil t)
7230 (goto-char (match-beginning 0))
7231 (if (org-invisible-p) (org-show-context)))
7233 (setq org-link-search-failed t)
7234 (error "No further link found"))))
7236 (defun org-previous-link ()
7237 "Move backward to the previous link.
7238 If the link is in hidden text, expose it."
7240 (when (and org-link-search-failed (eq this-command last-command))
7241 (goto-char (point-max))
7242 (message "Link search wrapped back to end of buffer"))
7243 (setq org-link-search-failed nil)
7244 (let* ((pos (point))
7246 (a (assoc :link ct)))
7247 (if a (goto-char (nth 1 a)))
7248 (if (re-search-backward org-any-link-re nil t)
7250 (goto-char (match-beginning 0))
7251 (if (org-invisible-p) (org-show-context)))
7253 (setq org-link-search-failed t)
7254 (error "No further link found"))))
7256 (defun org-find-file-at-mouse (ev)
7257 "Open file link or URL at mouse."
7259 (mouse-set-point ev)
7260 (org-open-at-point 'in-emacs))
7262 (defun org-open-at-mouse (ev)
7263 "Open file link or URL at mouse."
7265 (mouse-set-point ev)
7266 (org-open-at-point))
7268 (defvar org-window-config-before-follow-link nil
7269 "The window configuration before following a link.
7270 This is saved in case the need arises to restore it.")
7272 (defvar org-open-link-marker (make-marker)
7273 "Marker pointing to the location where `org-open-at-point; was called.")
7276 (defun org-open-at-point-global ()
7277 "Follow a link like Org-mode does.
7278 This command can be called in any mode to follow a link that has
7281 (org-run-like-in-org-mode 'org-open-at-point))
7283 (defun org-open-at-point (&optional in-emacs)
7284 "Open link at or after point.
7285 If there is no link at point, this function will search forward up to
7286 the end of the current subtree.
7287 Normally, files will be opened by an appropriate application. If the
7288 optional argument IN-EMACS is non-nil, Emacs will visit the file."
7290 (org-load-modules-maybe)
7291 (move-marker org-open-link-marker (point))
7292 (setq org-window-config-before-follow-link (current-window-configuration))
7293 (org-remove-occur-highlights nil nil t)
7294 (if (org-at-timestamp-p t)
7295 (org-follow-timestamp-link)
7296 (let (type path link line search (pos (point)))
7299 (skip-chars-forward "^]\n\r")
7300 (when (org-in-regexp org-bracket-link-regexp)
7301 (setq link (org-link-unescape (org-match-string-no-properties 1)))
7302 (while (string-match " *\n *" link)
7303 (setq link (replace-match " " t t link)))
7304 (setq link (org-link-expand-abbrev link))
7305 (if (string-match org-link-re-with-space2 link)
7306 (setq type (match-string 1 link) path (match-string 2 link))
7307 (setq type "thisfile" path link))
7310 (when (get-text-property (point) 'org-linked-text)
7311 (setq type "thisfile"
7312 pos (if (get-text-property (1+ (point)) 'org-linked-text)
7313 (1+ (point)) (point))
7314 path (buffer-substring
7315 (previous-single-property-change pos 'org-linked-text)
7316 (next-single-property-change pos 'org-linked-text)))
7320 (when (or (org-in-regexp org-angle-link-re)
7321 (org-in-regexp org-plain-link-re))
7322 (setq type (match-string 1) path (match-string 2))
7324 (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
7325 (setq type "tree-match"
7326 path (match-string 1))
7329 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
7331 path (match-string 1))
7332 (while (string-match ":" path)
7333 (setq path (replace-match "+" t t path)))
7336 (error "No link found"))
7337 ;; Remove any trailing spaces in path
7338 (if (string-match " +\\'" path)
7339 (setq path (replace-match "" t t path)))
7343 ((assoc type org-link-protocols)
7344 (funcall (nth 1 (assoc type org-link-protocols)) path))
7346 ((equal type "mailto")
7347 (let ((cmd (car org-link-mailto-program))
7348 (args (cdr org-link-mailto-program)) args1
7349 (address path) (subject "") a)
7350 (if (string-match "\\(.*\\)::\\(.*\\)" path)
7351 (setq address (match-string 1 path)
7352 subject (org-link-escape (match-string 2 path))))
7355 ((not (stringp (car args))) (push (pop args) args1))
7356 (t (setq a (pop args))
7357 (if (string-match "%a" a)
7358 (setq a (replace-match address t t a)))
7359 (if (string-match "%s" a)
7360 (setq a (replace-match subject t t a)))
7362 (apply cmd (nreverse args1))))
7364 ((member type '("http" "https" "ftp" "news"))
7365 (browse-url (concat type ":" (org-link-escape
7366 path org-link-escape-chars-browser))))
7368 ((member type '("message"))
7369 (browse-url (concat type ":" path)))
7371 ((string= type "tags")
7372 (org-tags-view in-emacs path))
7373 ((string= type "thisfile")
7375 (switch-to-buffer-other-window
7376 (org-get-buffer-for-internal-link (current-buffer)))
7377 (org-mark-ring-push))
7378 (let ((cmd `(org-link-search
7380 ,(cond ((equal in-emacs '(4)) 'occur)
7381 ((equal in-emacs '(16)) 'org-occur)
7384 (condition-case nil (eval cmd)
7385 (error (progn (widen) (eval cmd))))))
7387 ((string= type "tree-match")
7388 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
7390 ((string= type "file")
7391 (if (string-match "::\\([0-9]+\\)\\'" path)
7392 (setq line (string-to-number (match-string 1 path))
7393 path (substring path 0 (match-beginning 0)))
7394 (if (string-match "::\\(.+\\)\\'" path)
7395 (setq search (match-string 1 path)
7396 path (substring path 0 (match-beginning 0)))))
7397 (if (string-match "[*?{]" (file-name-nondirectory path))
7399 (org-open-file path in-emacs line search)))
7401 ((string= type "news")
7403 (org-gnus-follow-link path))
7405 ((string= type "shell")
7407 (if (or (not org-confirm-shell-link-function)
7408 (funcall org-confirm-shell-link-function
7409 (format "Execute \"%s\" in shell? "
7410 (org-add-props cmd nil
7411 'face 'org-warning))))
7413 (message "Executing %s" cmd)
7414 (shell-command cmd))
7417 ((string= type "elisp")
7419 (if (or (not org-confirm-elisp-link-function)
7420 (funcall org-confirm-elisp-link-function
7421 (format "Execute \"%s\" as elisp? "
7422 (org-add-props cmd nil
7423 'face 'org-warning))))
7424 (message "%s => %s" cmd (eval (read cmd)))
7428 (browse-url-at-point)))))
7429 (move-marker org-open-link-marker nil)
7430 (run-hook-with-args 'org-follow-link-hook))
7434 (defvar org-create-file-search-functions nil
7435 "List of functions to construct the right search string for a file link.
7436 These functions are called in turn with point at the location to
7437 which the link should point.
7439 A function in the hook should first test if it would like to
7440 handle this file type, for example by checking the major-mode or
7441 the file extension. If it decides not to handle this file, it
7442 should just return nil to give other functions a chance. If it
7443 does handle the file, it must return the search string to be used
7444 when following the link. The search string will be part of the
7445 file link, given after a double colon, and `org-open-at-point'
7446 will automatically search for it. If special measures must be
7447 taken to make the search successful, another function should be
7448 added to the companion hook `org-execute-file-search-functions',
7451 A function in this hook may also use `setq' to set the variable
7452 `description' to provide a suggestion for the descriptive text to
7453 be used for this link when it gets inserted into an Org-mode
7454 buffer with \\[org-insert-link].")
7456 (defvar org-execute-file-search-functions nil
7457 "List of functions to execute a file search triggered by a link.
7459 Functions added to this hook must accept a single argument, the
7460 search string that was part of the file link, the part after the
7461 double colon. The function must first check if it would like to
7462 handle this search, for example by checking the major-mode or the
7463 file extension. If it decides not to handle this search, it
7464 should just return nil to give other functions a chance. If it
7465 does handle the search, it must return a non-nil value to keep
7466 other functions from trying.
7468 Each function can access the current prefix argument through the
7469 variable `current-prefix-argument'. Note that a single prefix is
7470 used to force opening a link in Emacs, so it may be good to only
7471 use a numeric or double prefix to guide the search function.
7473 In case this is needed, a function in this hook can also restore
7474 the window configuration before `org-open-at-point' was called using:
7476 (set-window-configuration org-window-config-before-follow-link)")
7478 (defun org-link-search (s &optional type avoid-pos)
7479 "Search for a link search option.
7480 If S is surrounded by forward slashes, it is interpreted as a
7481 regular expression. In org-mode files, this will create an `org-occur'
7482 sparse tree. In ordinary files, `occur' will be used to list matches.
7483 If the current buffer is in `dired-mode', grep will be used to search
7484 in all files. If AVOID-POS is given, ignore matches near that position."
7485 (let ((case-fold-search t)
7486 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
7487 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
7488 (append '(("") (" ") ("\t") ("\n"))
7493 words re0 re1 re2 re3 re4 re5 re2a reall)
7495 ;; First check if there are any special
7496 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
7497 ;; Now try the builtin stuff
7499 (goto-char (point-min))
7502 (concat "<<" (regexp-quote s0) ">>") nil t)
7503 (setq pos (match-beginning 0))))
7504 ;; There is an exact target for this
7506 ((string-match "^/\\(.*\\)/$" s)
7507 ;; A regular expression
7510 (org-occur (match-string 1 s)))
7511 ;;((eq major-mode 'dired-mode)
7512 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
7513 (t (org-do-occur (match-string 1 s)))))
7515 ;; A normal search strings
7516 (when (equal (string-to-char s) ?*)
7517 ;; Anchor on headlines, post may include tags.
7518 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
7519 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
7521 (remove-text-properties
7523 '(face nil mouse-face nil keymap nil fontified nil) s)
7524 ;; Make a series of regular expressions to find a match
7525 (setq words (org-split-string s "[ \n\r\t]+")
7526 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
7527 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
7529 re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
7530 re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
7531 re1 (concat pre re2 post)
7532 re3 (concat pre re4 post)
7533 re5 (concat pre ".*" re4)
7534 re2 (concat pre re2)
7535 re2a (concat pre re2a)
7536 re4 (concat pre re4)
7537 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
7538 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
7542 ((eq type 'org-occur) (org-occur reall))
7543 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
7544 (t (goto-char (point-min))
7545 (if (or (org-search-not-self 1 re0 nil t)
7546 (org-search-not-self 1 re1 nil t)
7547 (org-search-not-self 1 re2 nil t)
7548 (org-search-not-self 1 re2a nil t)
7549 (org-search-not-self 1 re3 nil t)
7550 (org-search-not-self 1 re4 nil t)
7551 (org-search-not-self 1 re5 nil t)
7553 (goto-char (match-beginning 1))
7555 (error "No match")))))
7557 ;; Normal string-search
7558 (goto-char (point-min))
7559 (if (search-forward s nil t)
7560 (goto-char (match-beginning 0))
7561 (error "No match"))))
7562 (and (org-mode-p) (org-show-context 'link-search))))
7564 (defun org-search-not-self (group &rest args)
7565 "Execute `re-search-forward', but only accept matches that do not
7566 enclose the position of `org-open-link-marker'."
7567 (let ((m org-open-link-marker))
7569 (while (apply 're-search-forward args)
7570 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
7571 (goto-char (match-end group))
7572 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
7573 (> (match-beginning 0) (marker-position m))
7574 (< (match-end 0) (marker-position m)))
7576 (or (not (org-in-regexp
7577 org-bracket-link-analytic-regexp 1))
7578 (not (match-end 4)) ; no description
7579 (and (<= (match-beginning 4) (point))
7580 (>= (match-end 4) (point))))))
7581 (throw 'exit (point))))))))
7583 (defun org-get-buffer-for-internal-link (buffer)
7584 "Return a buffer to be used for displaying the link target of internal links."
7586 ((not org-display-internal-link-with-indirect-buffer)
7588 ((string-match "(Clone)$" (buffer-name buffer))
7589 (message "Buffer is already a clone, not making another one")
7590 ;; we also do not modify visibility in this case
7592 (t ; make a new indirect buffer for displaying the link
7593 (let* ((bn (buffer-name buffer))
7594 (ibn (concat bn "(Clone)"))
7595 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
7596 (with-current-buffer ib (org-overview))
7599 (defun org-do-occur (regexp &optional cleanup)
7600 "Call the Emacs command `occur'.
7601 If CLEANUP is non-nil, remove the printout of the regular expression
7602 in the *Occur* buffer. This is useful if the regex is long and not useful
7606 (let ((cwin (selected-window)) win beg end)
7607 (when (setq win (get-buffer-window "*Occur*"))
7608 (select-window win))
7609 (goto-char (point-min))
7610 (when (re-search-forward "match[a-z]+" nil t)
7611 (setq beg (match-end 0))
7612 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
7613 (setq end (1- (match-beginning 0)))))
7614 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
7615 (goto-char (point-min))
7616 (select-window cwin))))
7618 ;;; The mark ring for links jumps
7620 (defvar org-mark-ring nil
7621 "Mark ring for positions before jumps in Org-mode.")
7622 (defvar org-mark-ring-last-goto nil
7623 "Last position in the mark ring used to go back.")
7624 ;; Fill and close the ring
7625 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
7626 (loop for i from 1 to org-mark-ring-length do
7627 (push (make-marker) org-mark-ring))
7628 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
7631 (defun org-mark-ring-push (&optional pos buffer)
7632 "Put the current position or POS into the mark ring and rotate it."
7634 (setq pos (or pos (point)))
7635 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
7636 (move-marker (car org-mark-ring)
7638 (or buffer (current-buffer)))
7640 (substitute-command-keys
7641 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
7643 (defun org-mark-ring-goto (&optional n)
7644 "Jump to the previous position in the mark ring.
7645 With prefix arg N, jump back that many stored positions. When
7646 called several times in succession, walk through the entire ring.
7647 Org-mode commands jumping to a different position in the current file,
7648 or to another Org-mode file, automatically push the old position
7652 (if (eq last-command this-command)
7653 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
7654 (setq p org-mark-ring))
7655 (setq org-mark-ring-last-goto p)
7657 (switch-to-buffer (marker-buffer m))
7659 (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
7661 (defun org-remove-angle-brackets (s)
7662 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
7663 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
7665 (defun org-add-angle-brackets (s)
7666 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
7667 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
7670 ;;; Following specific links
7672 (defun org-follow-timestamp-link ()
7674 ((org-at-date-range-p t)
7675 (let ((org-agenda-start-on-weekday)
7676 (t1 (match-string 1))
7677 (t2 (match-string 2)))
7678 (setq t1 (time-to-days (org-time-string-to-time t1))
7679 t2 (time-to-days (org-time-string-to-time t2)))
7680 (org-agenda-list nil t1 (1+ (- t2 t1)))))
7681 ((org-at-timestamp-p t)
7682 (org-agenda-list nil (time-to-days (org-time-string-to-time
7683 (substring (match-string 1) 0 10)))
7685 (t (error "This should not happen"))))
7688 ;;; Following file links
7690 (defun org-open-file (path &optional in-emacs line search)
7691 "Open the file at PATH.
7692 First, this expands any special file name abbreviations. Then the
7693 configuration variable `org-file-apps' is checked if it contains an
7694 entry for this file type, and if yes, the corresponding command is launched.
7695 If no application is found, Emacs simply visits the file.
7696 With optional argument IN-EMACS, Emacs will visit the file.
7697 Optional LINE specifies a line to go to, optional SEARCH a string to
7698 search for. If LINE or SEARCH is given, the file will always be
7700 If the file does not exist, an error is thrown."
7701 (setq in-emacs (or in-emacs line search))
7702 (let* ((file (if (equal path "")
7704 (substitute-in-file-name (expand-file-name path))))
7705 (apps (append org-file-apps (org-default-apps)))
7706 (remp (and (assq 'remote apps) (org-file-remote-p file)))
7707 (dirp (if remp nil (file-directory-p file)))
7708 (dfile (downcase file))
7709 (old-buffer (current-buffer))
7711 (old-mode major-mode)
7713 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
7714 (setq ext (match-string 1 dfile))
7715 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
7716 (setq ext (match-string 1 dfile))))
7719 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
7720 (and dirp (cdr (assoc 'directory apps)))
7721 (cdr (assoc ext apps))
7722 (cdr (assoc t apps)))))
7723 (when (eq cmd 'mailcap)
7725 (mailcap-parse-mailcaps)
7726 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
7727 (command (mailcap-mime-info mime-type)))
7728 (if (stringp command)
7730 (setq cmd 'emacs))))
7731 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
7732 (not (file-exists-p file))
7733 (not org-open-non-existing-files))
7734 (error "No such file: %s" file))
7736 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
7737 ;; Remove quotes around the file name - we'll use shell-quote-argument.
7738 (while (string-match "['\"]%s['\"]" cmd)
7739 (setq cmd (replace-match "%s" t t cmd)))
7740 (while (string-match "%s" cmd)
7741 (setq cmd (replace-match
7742 (save-match-data (shell-quote-argument file))
7744 (save-window-excursion
7745 (start-process-shell-command cmd nil cmd)))
7748 (funcall (cdr (assq 'file org-link-frame-setup)) file)
7750 (if line (goto-line line)
7751 (if search (org-link-search search))))
7754 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
7755 (and (org-mode-p) (eq old-mode 'org-mode)
7756 (or (not (equal old-buffer (current-buffer)))
7757 (not (equal old-pos (point))))
7758 (org-mark-ring-push old-pos old-buffer))))
7760 (defun org-default-apps ()
7761 "Return the default applications for this operating system."
7763 ((eq system-type 'darwin)
7764 org-file-apps-defaults-macosx)
7765 ((eq system-type 'windows-nt)
7766 org-file-apps-defaults-windowsnt)
7767 (t org-file-apps-defaults-gnu)))
7769 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
7770 (defun org-file-remote-p (file)
7771 "Test whether FILE specifies a location on a remote system.
7772 Return non-nil if the location is indeed remote.
7774 For example, the filename \"/user@host:/foo\" specifies a location
7775 on the system \"/user@host:\"."
7776 (cond ((fboundp 'file-remote-p)
7777 (file-remote-p file))
7778 ((fboundp 'tramp-handle-file-remote-p)
7779 (tramp-handle-file-remote-p file))
7780 ((and (boundp 'ange-ftp-name-format)
7781 (string-match (car ange-ftp-name-format) file))
7788 (defun org-get-org-file ()
7789 "Read a filename, with default directory `org-directory'."
7790 (let ((default (or org-default-notes-file remember-data-file)))
7791 (read-file-name (format "File name [%s]: " default)
7792 (file-name-as-directory org-directory)
7795 (defun org-notes-order-reversed-p ()
7796 "Check if the current file should receive notes in reversed order."
7798 ((not org-reverse-note-order) nil)
7799 ((eq t org-reverse-note-order) t)
7800 ((not (listp org-reverse-note-order)) nil)
7802 (let ((all org-reverse-note-order)
7804 (while (setq entry (pop all))
7805 (if (string-match (car entry) buffer-file-name)
7806 (throw 'exit (cdr entry))))
7809 (defvar org-refile-target-table nil
7810 "The list of refile targets, created by `org-refile'.")
7812 (defvar org-agenda-new-buffers nil
7813 "Buffers created to visit agenda files.")
7815 (defun org-get-refile-targets (&optional default-buffer)
7816 "Produce a table with refile targets."
7817 (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
7818 targets txt re files f desc descre)
7819 (with-current-buffer (or default-buffer (current-buffer))
7820 (while (setq entry (pop entries))
7821 (setq files (car entry) desc (cdr entry))
7823 ((null files) (setq files (list (current-buffer))))
7824 ((eq files 'org-agenda-files)
7825 (setq files (org-agenda-files 'unrestricted)))
7826 ((and (symbolp files) (fboundp files))
7827 (setq files (funcall files)))
7828 ((and (symbolp files) (boundp files))
7829 (setq files (symbol-value files))))
7830 (if (stringp files) (setq files (list files)))
7832 ((eq (car desc) :tag)
7833 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
7834 ((eq (car desc) :todo)
7835 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
7836 ((eq (car desc) :regexp)
7837 (setq descre (cdr desc)))
7838 ((eq (car desc) :level)
7839 (setq descre (concat "^\\*\\{" (number-to-string
7840 (if org-odd-levels-only
7841 (1- (* 2 (cdr desc)))
7844 ((eq (car desc) :maxlevel)
7845 (setq descre (concat "^\\*\\{1," (number-to-string
7846 (if org-odd-levels-only
7847 (1- (* 2 (cdr desc)))
7850 (t (error "Bad refiling target description %s" desc)))
7851 (while (setq f (pop files))
7853 (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
7854 (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
7858 (goto-char (point-min))
7859 (while (re-search-forward descre nil t)
7860 (goto-char (point-at-bol))
7861 (when (looking-at org-complex-heading-regexp)
7862 (setq txt (match-string 4)
7863 re (concat "^" (regexp-quote
7864 (buffer-substring (match-beginning 1)
7866 (if (match-end 5) (setq re (concat re "[ \t]+"
7868 (match-string 5)))))
7869 (setq re (concat re "[ \t]*$"))
7870 (when org-refile-use-outline-path
7871 (setq txt (mapconcat 'identity
7873 (if (eq org-refile-use-outline-path 'file)
7874 (list (file-name-nondirectory
7875 (buffer-file-name (buffer-base-buffer))))
7876 (if (eq org-refile-use-outline-path 'full-file-path)
7877 (list (buffer-file-name (buffer-base-buffer)))))
7878 (org-get-outline-path)
7881 (push (list txt f re (point)) targets))
7882 (goto-char (point-at-eol))))))))
7883 (nreverse targets))))
7885 (defun org-get-outline-path ()
7886 "Return the outline path to the current entry, as a list."
7889 (while (org-up-heading-safe)
7890 (when (looking-at org-complex-heading-regexp)
7891 (push (org-match-string-no-properties 4) rtn)))
7894 (defvar org-refile-history nil
7895 "History for refiling operations.")
7897 (defun org-refile (&optional goto default-buffer)
7898 "Move the entry at point to another heading.
7899 The list of target headings is compiled using the information in
7900 `org-refile-targets', which see. This list is created before each use
7901 and will therefore always be up-to-date.
7903 At the target location, the entry is filed as a subitem of the target heading.
7904 Depending on `org-reverse-note-order', the new subitem will either be the
7905 first of the last subitem.
7907 With prefix arg GOTO, the command will only visit the target location,
7908 not actually move anything.
7909 With a double prefix `C-c C-c', go to the location where the last refiling
7910 operation has put the subtree."
7912 (let* ((cbuf (current-buffer))
7913 (filename (buffer-file-name (buffer-base-buffer cbuf)))
7914 pos it nbuf file re level reversed)
7915 (if (equal goto '(16))
7916 (org-refile-goto-last-stored)
7917 (when (setq it (org-refile-get-location
7918 (if goto "Goto: " "Refile to: ") default-buffer))
7919 (setq file (nth 1 it)
7922 (setq nbuf (or (find-buffer-visiting file)
7923 (find-file-noselect file)))
7926 (switch-to-buffer nbuf)
7928 (org-show-context 'org-goto))
7931 (set-buffer (setq nbuf (or (find-buffer-visiting file)
7932 (find-file-noselect file))))
7933 (setq reversed (org-notes-order-reversed-p))
7938 (looking-at outline-regexp)
7939 (setq level (org-get-valid-level (funcall outline-level) 1))
7942 (outline-next-heading)
7943 (or (save-excursion (outline-get-next-sibling))
7944 (org-end-of-subtree t t)
7946 (bookmark-set "org-refile-last-stored")
7947 (org-paste-subtree level))))
7949 (message "Entry refiled to \"%s\"" (car it)))))))
7951 (defun org-refile-goto-last-stored ()
7952 "Go to the location where the last refile was stored."
7954 (bookmark-jump "org-refile-last-stored")
7955 (message "This is the location of the last refile"))
7957 (defun org-refile-get-location (&optional prompt default-buffer)
7958 "Prompt the user for a refile location, using PROMPT."
7959 (let ((org-refile-targets org-refile-targets)
7960 (org-refile-use-outline-path org-refile-use-outline-path))
7961 (setq org-refile-target-table (org-get-refile-targets default-buffer)))
7962 (unless org-refile-target-table
7963 (error "No refile targets"))
7964 (let* ((cbuf (current-buffer))
7965 (filename (buffer-file-name (buffer-base-buffer cbuf)))
7966 (fname (and filename (file-truename filename)))
7969 (if (not (equal fname (file-truename (nth 1 x))))
7970 (cons (concat (car x) " (" (file-name-nondirectory
7974 org-refile-target-table))
7975 (completion-ignore-case t))
7976 (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
7981 (defun org-find-dblock (name)
7982 "Find the first dynamic block with name NAME in the buffer.
7983 If not found, stay at current position and return nil."
7986 (goto-char (point-min))
7987 (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
7989 (match-beginning 0))))
7990 (if pos (goto-char pos))
7993 (defconst org-dblock-start-re
7994 "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
7995 "Matches the startline of a dynamic block, with parameters.")
7997 (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
7998 "Matches the end of a dyhamic block.")
8000 (defun org-create-dblock (plist)
8001 "Create a dynamic block section, with parameters taken from PLIST.
8002 PLIST must containe a :name entry which is used as name of the block."
8003 (unless (bolp) (newline))
8004 (let ((name (plist-get plist :name)))
8005 (insert "#+BEGIN: " name)
8007 (if (eq (car plist) :name)
8008 (setq plist (cddr plist))
8009 (insert " " (prin1-to-string (pop plist)))))
8010 (insert "\n\n#+END:\n")
8011 (beginning-of-line -2)))
8013 (defun org-prepare-dblock ()
8014 "Prepare dynamic block for refresh.
8015 This empties the block, puts the cursor at the insert position and returns
8016 the property list including an extra property :name with the block name."
8017 (unless (looking-at org-dblock-start-re)
8018 (error "Not at a dynamic block"))
8019 (let* ((begdel (1+ (match-end 0)))
8020 (name (org-no-properties (match-string 1)))
8021 (params (append (list :name name)
8022 (read (concat "(" (match-string 3) ")")))))
8023 (unless (re-search-forward org-dblock-end-re nil t)
8024 (error "Dynamic block not terminated"))
8027 (list :content (buffer-substring
8028 begdel (match-beginning 0)))))
8029 (delete-region begdel (match-beginning 0))
8034 (defun org-map-dblocks (&optional command)
8035 "Apply COMMAND to all dynamic blocks in the current buffer.
8036 If COMMAND is not given, use `org-update-dblock'."
8037 (let ((cmd (or command 'org-update-dblock))
8040 (goto-char (point-min))
8041 (while (re-search-forward org-dblock-start-re nil t)
8042 (goto-char (setq pos (match-beginning 0)))
8045 (error (message "Error during update of dynamic block")))
8047 (unless (re-search-forward org-dblock-end-re nil t)
8048 (error "Dynamic block not terminated"))))))
8050 (defun org-dblock-update (&optional arg)
8051 "User command for updating dynamic blocks.
8052 Update the dynamic block at point. With prefix ARG, update all dynamic
8053 blocks in the buffer."
8056 (org-update-all-dblocks)
8057 (or (looking-at org-dblock-start-re)
8058 (org-beginning-of-dblock))
8059 (org-update-dblock)))
8061 (defun org-update-dblock ()
8062 "Update the dynamic block at point
8063 This means to empty the block, parse for parameters and then call
8064 the correct writing function."
8065 (save-window-excursion
8066 (let* ((pos (point))
8067 (line (org-current-line))
8068 (params (org-prepare-dblock))
8069 (name (plist-get params :name))
8070 (cmd (intern (concat "org-dblock-write:" name))))
8071 (message "Updating dynamic block `%s' at line %d..." name line)
8072 (funcall cmd params)
8073 (message "Updating dynamic block `%s' at line %d...done" name line)
8076 (defun org-beginning-of-dblock ()
8077 "Find the beginning of the dynamic block at point.
8078 Error if there is no scuh block at point."
8082 (if (and (re-search-backward org-dblock-start-re nil t)
8083 (setq beg (match-beginning 0))
8084 (re-search-forward org-dblock-end-re nil t)
8085 (> (match-end 0) pos))
8088 (error "Not in a dynamic block"))))
8090 (defun org-update-all-dblocks ()
8091 "Update all dynamic blocks in the buffer.
8092 This function can be used in a hook."
8094 (org-map-dblocks 'org-update-dblock)))
8099 (defconst org-additional-option-like-keywords
8100 '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
8101 "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
8102 "BEGIN_EXAMPLE" "END_EXAMPLE"))
8104 (defun org-complete (&optional arg)
8105 "Perform completion on word at point.
8106 At the beginning of a headline, this completes TODO keywords as given in
8107 `org-todo-keywords'.
8108 If the current word is preceded by a backslash, completes the TeX symbols
8109 that are supported for HTML support.
8110 If the current word is preceded by \"#+\", completes special words for
8111 setting file options.
8112 In the line after \"#+STARTUP:, complete valid keywords.\"
8113 At all other locations, this simply calls the value of
8114 `org-completion-fallback-command'."
8116 (org-without-partial-completion
8118 (let* ((end (point))
8119 (beg1 (save-excursion
8120 (skip-chars-backward (org-re "[:alnum:]_@"))
8122 (beg (save-excursion
8123 (skip-chars-backward "a-zA-Z0-9_:$")
8125 (confirm (lambda (x) (stringp (car x))))
8126 (searchhead (equal (char-before beg) ?*))
8127 (tag (and (equal (char-before beg1) ?:)
8128 (equal (char-after (point-at-bol)) ?*)))
8129 (prop (and (equal (char-before beg1) ?:)
8130 (not (equal (char-after (point-at-bol)) ?*))))
8131 (texp (equal (char-before beg) ?\\))
8132 (link (equal (char-before beg) ?\[))
8133 (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
8136 (startup (string-match "^#\\+STARTUP:.*"
8137 (buffer-substring (point-at-bol) (point))))
8138 (completion-ignore-case opt)
8148 (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
8149 (cons (match-string 2 x) (match-string 1 x)))
8150 (org-split-string (org-get-current-options) "\n"))
8151 (mapcar 'list org-additional-option-like-keywords)))
8153 (setq type :startup)
8154 org-startup-options)
8155 (link (append org-link-abbrev-alist-local
8156 org-link-abbrev-alist))
8160 ((string-match "\\`\\*+[ \t]+\\'"
8161 (buffer-substring (point-at-bol) beg))
8163 (mapcar 'list org-todo-keywords-1))
8165 (setq type :searchhead)
8167 (goto-char (point-min))
8168 (while (re-search-forward org-todo-line-regexp nil t)
8170 (org-make-org-heading-search-string
8171 (match-string 3) t))
8174 (tag (setq type :tag beg beg1)
8175 (or org-tag-alist (org-get-buffer-tags)))
8176 (prop (setq type :prop beg beg1)
8177 (mapcar 'list (org-buffer-property-keys nil t t)))
8179 (call-interactively org-completion-fallback-command)
8180 (throw 'exit nil)))))
8181 (pattern (buffer-substring-no-properties beg end))
8182 (completion (try-completion pattern table confirm)))
8183 (cond ((eq completion t)
8184 (if (not (assoc (upcase pattern) table))
8185 (message "Already complete")
8186 (if (and (equal type :opt)
8187 (not (member (car (assoc (upcase pattern) table))
8188 org-additional-option-like-keywords)))
8189 (insert (substring (cdr (assoc (upcase pattern) table))
8191 (if (memq type '(:tag :prop)) (insert ":")))))
8193 (message "Can't find completion for \"%s\"" pattern)
8195 ((not (string= pattern completion))
8196 (delete-region beg end)
8197 (if (string-match " +$" completion)
8198 (setq completion (replace-match "" t t completion)))
8200 (if (get-buffer-window "*Completions*")
8201 (delete-window (get-buffer-window "*Completions*")))
8202 (if (assoc completion table)
8203 (if (eq type :todo) (insert " ")
8204 (if (memq type '(:tag :prop)) (insert ":"))))
8205 (if (and (equal type :opt) (assoc completion table))
8206 (message "%s" (substitute-command-keys
8207 "Press \\[org-complete] again to insert example settings"))))
8209 (message "Making completion list...")
8210 (let ((list (sort (all-completions pattern table confirm)
8212 (with-output-to-temp-buffer "*Completions*"
8214 ;; Protection needed for XEmacs and emacs 21
8215 (display-completion-list list pattern)
8216 (error (display-completion-list list)))))
8217 (message "Making completion list...%s" "done")))))))
8219 ;;;; TODO, DEADLINE, Comments
8221 (defun org-toggle-comment ()
8222 "Change the COMMENT state of an entry."
8225 (org-back-to-heading)
8226 (let (case-fold-search)
8227 (if (looking-at (concat outline-regexp
8228 "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
8229 (replace-match "" t t nil 1)
8230 (if (looking-at outline-regexp)
8232 (goto-char (match-end 0))
8233 (insert org-comment-string " ")))))))
8235 (defvar org-last-todo-state-is-todo nil
8236 "This is non-nil when the last TODO state change led to a TODO state.
8237 If the last change removed the TODO tag or switched to DONE, then
8240 (defvar org-setting-tags nil) ; dynamically skiped
8242 (defun org-parse-local-options (string var)
8243 "Parse STRING for startup setting relevant for variable VAR."
8244 (let ((rtn (symbol-value var))
8247 (if (or (not string) (not (string-match "\\S-" string)))
8249 (setq opts (delq nil (mapcar (lambda (x)
8250 (setq e (assoc x org-startup-options))
8251 (if (eq (nth 1 e) var) e nil))
8252 (org-split-string string "[ \t]+"))))
8256 (while (setq e (pop opts))
8258 (setq rtn (nth 2 e))
8259 (if (not (listp rtn)) (setq rtn nil))
8260 (push (nth 2 e) rtn)))
8263 (defvar org-blocker-hook nil
8264 "Hook for functions that are allowed to block a state change.
8266 Each function gets as its single argument a property list, see
8267 `org-trigger-hook' for more information about this list.
8269 If any of the functions in this hook returns nil, the state change
8272 (defvar org-trigger-hook nil
8273 "Hook for functions that are triggered by a state change.
8275 Each function gets as its single argument a property list with at least
8276 the following elements:
8278 (:type type-of-change :position pos-at-entry-start
8279 :from old-state :to new-state)
8281 Depending on the type, more properties may be present.
8283 This mechanism is currently implemented for:
8287 :type todo-state-change
8288 :from previous state (keyword as a string), or nil
8289 :to new state (keyword as a string), or nil")
8292 (defun org-todo (&optional arg)
8293 "Change the TODO state of an item.
8294 The state of an item is given by a keyword at the start of the heading,
8296 *** TODO Write paper
8299 The different keywords are specified in the variable `org-todo-keywords'.
8300 By default the available states are \"TODO\" and \"DONE\".
8301 So for this example: when the item starts with TODO, it is changed to DONE.
8302 When it starts with DONE, the DONE is removed. And when neither TODO nor
8303 DONE are present, add TODO at the beginning of the heading.
8305 With C-u prefix arg, use completion to determine the new state.
8306 With numeric prefix arg, switch to that state.
8308 For calling through lisp, arg is also interpreted in the following way:
8309 'none -> empty state
8310 \"\"(empty string) -> switch to empty state
8311 'done -> switch to DONE
8312 'nextset -> switch to the next set of keywords
8313 'previousset -> switch to the previous set of keywords
8314 \"WAITING\" -> switch to the specified keyword, but only if it
8315 really is a member of `org-todo-keywords'."
8319 (org-back-to-heading)
8320 (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
8321 (or (looking-at (concat " +" org-todo-regexp " *"))
8323 (let* ((match-data (match-data))
8324 (startpos (point-at-bol))
8325 (logging (save-match-data (org-entry-get nil "LOGGING" t)))
8326 (org-log-done org-log-done)
8327 (org-log-repeat org-log-repeat)
8328 (org-todo-log-states org-todo-log-states)
8329 (this (match-string 1))
8330 (hl-pos (match-beginning 0))
8331 (head (org-get-todo-sequence-head this))
8332 (ass (assoc head org-todo-kwd-alist))
8333 (interpret (nth 1 ass))
8334 (done-word (nth 3 ass))
8335 (final-done-word (nth 4 ass))
8336 (last-state (or this ""))
8337 (completion-ignore-case t)
8338 (member (member this org-todo-keywords-1))
8341 ((and org-todo-key-trigger
8342 (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
8343 (and (not arg) org-use-fast-todo-selection
8344 (not (eq org-use-fast-todo-selection 'prefix)))))
8345 ;; Use fast selection
8346 (org-fast-todo-selection))
8347 ((and (equal arg '(4))
8348 (or (not org-use-fast-todo-selection)
8349 (not org-todo-key-trigger)))
8350 ;; Read a state with completion
8351 (completing-read "State: " (mapcar (lambda(x) (list x))
8352 org-todo-keywords-1)
8356 (if tail (car tail) nil)
8357 (car org-todo-keywords-1)))
8359 (if (equal member org-todo-keywords-1)
8362 (nth (- (length org-todo-keywords-1) (length tail) 2)
8363 org-todo-keywords-1)
8364 (org-last org-todo-keywords-1))))
8365 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
8366 (setq arg nil))) ; hack to fall back to cycling
8368 ;; user or caller requests a specific state
8370 ((equal arg "") nil)
8371 ((eq arg 'none) nil)
8372 ((eq arg 'done) (or done-word (car org-done-keywords)))
8374 (or (car (cdr (member head org-todo-heads)))
8375 (car org-todo-heads)))
8376 ((eq arg 'previousset)
8377 (let ((org-todo-heads (reverse org-todo-heads)))
8378 (or (car (cdr (member head org-todo-heads)))
8379 (car org-todo-heads))))
8380 ((car (member arg org-todo-keywords-1)))
8381 ((nth (1- (prefix-numeric-value arg))
8382 org-todo-keywords-1))))
8383 ((null member) (or head (car org-todo-keywords-1)))
8384 ((equal this final-done-word) nil) ;; -> make empty
8385 ((null tail) nil) ;; -> first entry
8386 ((eq interpret 'sequence)
8388 ((memq interpret '(type priority))
8389 (if (eq this-command last-command)
8391 (if (> (length tail) 0)
8392 (or done-word (car org-done-keywords))
8395 (next (if state (concat " " state " ") " "))
8396 (change-plist (list :type 'todo-state-change :from this :to state
8397 :position startpos))
8399 (when org-blocker-hook
8400 (unless (save-excursion
8402 (run-hook-with-args-until-failure
8403 'org-blocker-hook change-plist)))
8405 (error "TODO state change from %s to %s blocked" this state)
8407 (message "TODO state change from %s to %s blocked" this state)
8408 (throw 'exit nil))))
8409 (store-match-data match-data)
8410 (replace-match next t t)
8411 (unless (pos-visible-in-window-p hl-pos)
8412 (message "TODO state changed to %s" (org-trim next)))
8414 (setq head (org-get-todo-sequence-head state)
8415 ass (assoc head org-todo-kwd-alist)
8416 interpret (nth 1 ass)
8417 done-word (nth 3 ass)
8418 final-done-word (nth 4 ass)))
8419 (when (memq arg '(nextset previousset))
8420 (message "Keyword-Set %d/%d: %s"
8421 (- (length org-todo-sets) -1
8422 (length (memq (assoc state org-todo-sets) org-todo-sets)))
8423 (length org-todo-sets)
8424 (mapconcat 'identity (assoc state org-todo-sets) " ")))
8425 (setq org-last-todo-state-is-todo
8426 (not (member state org-done-keywords)))
8427 (setq now-done-p (and (member state org-done-keywords)
8428 (not (member this org-done-keywords))))
8429 (and logging (org-local-logging logging))
8430 (when (and (or org-todo-log-states org-log-done)
8431 (not (memq arg '(nextset previousset))))
8432 ;; we need to look at recording a time and note
8433 (setq dolog (or (nth 1 (assoc state org-todo-log-states))
8434 (nth 2 (assoc this org-todo-log-states))))
8436 (member state org-not-done-keywords)
8437 (not (member this org-not-done-keywords)))
8438 ;; This is now a todo state and was not one before
8439 ;; If there was a CLOSED time stamp, get rid of it.
8440 (org-add-planning-info nil nil 'closed))
8441 (when (and now-done-p org-log-done)
8442 ;; It is now done, and it was not done before
8443 (org-add-planning-info 'closed (org-current-time))
8444 (if (and (not dolog) (eq 'note org-log-done))
8445 (org-add-log-setup 'done state 'findpos 'note)))
8446 (when (and state dolog)
8447 ;; This is a non-nil state, and we need to log it
8448 (org-add-log-setup 'state state 'findpos dolog)))
8449 ;; Fixup tag positioning
8450 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
8451 (run-hooks 'org-after-todo-state-change-hook)
8452 (if (and arg (not (member state org-done-keywords)))
8453 (setq head (org-get-todo-sequence-head state)))
8454 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
8455 ;; Do we need to trigger a repeat?
8456 (when now-done-p (org-auto-repeat-maybe state))
8457 ;; Fixup cursor location if close to the keyword
8458 (if (and (outline-on-heading-p)
8460 (save-excursion (beginning-of-line 1)
8461 (looking-at org-todo-line-regexp))
8462 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
8464 (goto-char (or (match-end 2) (match-end 1)))
8466 (when org-trigger-hook
8468 (run-hook-with-args 'org-trigger-hook change-plist)))))))
8470 (defun org-local-logging (value)
8471 "Get logging settings from a property VALUE."
8473 ;; directly set the variables, they are already local.
8474 (setq org-log-done nil
8476 org-todo-log-states nil)
8477 (setq words (org-split-string value))
8478 (while (setq w (pop words))
8480 ((setq a (assoc w org-startup-options))
8481 (and (member (nth 1 a) '(org-log-done org-log-repeat))
8482 (set (nth 1 a) (nth 2 a))))
8483 ((setq a (org-extract-log-state-settings w))
8484 (and (member (car a) org-todo-keywords-1)
8485 (push a org-todo-log-states)))))))
8487 (defun org-get-todo-sequence-head (kwd)
8488 "Return the head of the TODO sequence to which KWD belongs.
8489 If KWD is not set, check if there is a text property remembering the
8494 (or (get-text-property (point-at-bol) 'org-todo-head)
8496 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
8497 nil (point-at-eol)))
8498 (get-text-property p 'org-todo-head))))
8499 ((not (member kwd org-todo-keywords-1))
8500 (car org-todo-keywords-1))
8501 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
8503 (defun org-fast-todo-selection ()
8504 "Fast TODO keyword selection with single keys.
8505 Returns the new TODO keyword, or nil if no state change should occur."
8506 (let* ((fulltable org-todo-key-alist)
8507 (done-keywords org-done-keywords) ;; needed for the faces.
8508 (maxlen (apply 'max (mapcar
8510 (if (stringp (car x)) (string-width (car x)) 0))
8513 (fwidth (+ maxlen 3 1 3))
8514 (ncol (/ (- (window-width) 4) fwidth))
8517 (save-window-excursion
8519 (set-buffer (get-buffer-create " *Org todo*"))
8520 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
8522 (org-set-local 'org-done-keywords done-keywords)
8523 (setq tbl fulltable cnt 0)
8524 (while (setq e (pop tbl))
8526 ((equal e '(:startgroup))
8527 (push '() groups) (setq ingroup t)
8528 (when (not (= cnt 0))
8532 ((equal e '(:endgroup))
8533 (setq ingroup nil cnt 0)
8536 (setq tg (car e) c (cdr e))
8537 (if ingroup (push tg (car groups)))
8538 (setq tg (org-add-props tg nil 'face
8539 (org-get-todo-face tg)))
8540 (if (and (= cnt 0) (not ingroup)) (insert " "))
8541 (insert "[" c "] " tg (make-string
8542 (- fwidth 4 (length tg)) ?\ ))
8543 (when (= (setq cnt (1+ cnt)) ncol)
8545 (if ingroup (insert " "))
8548 (goto-char (point-min))
8549 (if (and (not expert) (fboundp 'fit-window-to-buffer))
8550 (fit-window-to-buffer))
8551 (message "[a-z..]:Set [SPC]:clear")
8552 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
8555 (and (= c ?q) (not (rassoc c fulltable))))
8558 ((setq e (rassoc c fulltable) tg (car e))
8560 (t (setq quit-flag t))))))
8562 (defun org-entry-is-todo-p ()
8563 (member (org-get-todo-state) org-not-done-keywords))
8565 (defun org-entry-is-done-p ()
8566 (member (org-get-todo-state) org-done-keywords))
8568 (defun org-get-todo-state ()
8570 (org-back-to-heading t)
8571 (and (looking-at org-todo-line-regexp)
8575 (defun org-at-date-range-p (&optional inactive-ok)
8576 "Is the cursor inside a date range?"
8580 (let ((pos (point)))
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)
8586 (skip-chars-backward "^<[\r\n")
8587 (skip-chars-backward "<[")
8588 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
8589 (>= (match-end 0) pos)
8593 (defun org-get-repeat ()
8594 "Check if tere is a deadline/schedule with repeater in this entry."
8597 (org-back-to-heading t)
8598 (if (re-search-forward
8599 org-repeat-re (save-excursion (outline-next-heading) (point)) t)
8600 (match-string 1)))))
8602 (defvar org-last-changed-timestamp)
8603 (defvar org-log-post-message)
8604 (defvar org-log-note-purpose)
8605 (defvar org-log-note-how)
8606 (defun org-auto-repeat-maybe (done-word)
8607 "Check if the current headline contains a repeated deadline/schedule.
8608 If yes, set TODO state back to what it was and change the base date
8609 of repeating deadline/scheduled time stamps to new date.
8610 This function is run automatically after each state change to a DONE state."
8611 ;; last-state is dynamically scoped into this function
8612 (let* ((repeat (org-get-repeat))
8613 (aa (assoc last-state org-todo-kwd-alist))
8614 (interpret (nth 1 aa))
8616 (whata '(("d" . day) ("m" . month) ("y" . year)))
8617 (msg "Entry repeats: ")
8619 (org-todo-log-states nil)
8620 (nshiftmax 10) (nshift 0)
8621 re type n what ts mb0 time)
8623 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
8624 (org-todo (if (eq interpret 'type) last-state head))
8625 (when org-log-repeat
8626 (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
8627 (memq 'org-add-log-note post-command-hook))
8628 ;; OK, we are already setup for some record
8629 (if (eq org-log-repeat 'note)
8630 ;; make sure we take a note, not only a time stamp
8631 (setq org-log-note-how 'note))
8632 ;; Set up for taking a record
8633 (org-add-log-setup 'state (or done-word (car org-done-keywords))
8634 'findpos org-log-repeat)))
8635 (org-back-to-heading t)
8636 (org-add-planning-info nil nil 'closed)
8637 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
8638 org-deadline-time-regexp "\\)\\|\\("
8639 org-ts-regexp "\\)"))
8640 (while (re-search-forward
8641 re (save-excursion (outline-next-heading) (point)) t)
8642 (setq type (if (match-end 1) org-scheduled-string
8643 (if (match-end 3) org-deadline-string "Plain:"))
8644 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
8645 mb0 (match-beginning 0))
8646 (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
8647 (setq n (string-to-number (match-string 2 ts))
8648 what (match-string 3 ts))
8649 (if (equal what "w") (setq n (* n 7) what "d"))
8650 ;; Preparation, see if we need to modify the start date for the change
8652 (setq time (save-match-data (org-time-string-to-time ts)))
8654 ((equal (match-string 1 ts) ".")
8655 ;; Shift starting date to today
8656 (org-timestamp-change
8657 (- (time-to-days (current-time)) (time-to-days time))
8659 ((equal (match-string 1 ts) "+")
8660 (while (or (= nshift 0)
8661 (<= (time-to-days time) (time-to-days (current-time))))
8662 (when (= (incf nshift) nshiftmax)
8663 (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
8665 (org-timestamp-change n (cdr (assoc what whata)))
8666 (org-at-timestamp-p t)
8667 (setq ts (match-string 1))
8668 (setq time (save-match-data (org-time-string-to-time ts))))
8669 (org-timestamp-change (- n) (cdr (assoc what whata)))
8670 ;; rematch, so that we have everything in place for the real shift
8671 (org-at-timestamp-p t)
8672 (setq ts (match-string 1))
8673 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
8674 (org-timestamp-change n (cdr (assoc what whata)))
8675 (setq msg (concat msg type org-last-changed-timestamp " "))))
8676 (setq org-log-post-message msg)
8677 (message "%s" msg))))
8679 (defun org-show-todo-tree (arg)
8680 "Make a compact tree which shows all headlines marked with TODO.
8681 The tree will show the lines where the regexp matches, and all higher
8682 headlines above the match.
8683 With a \\[universal-argument] prefix, also show the DONE entries.
8684 With a numeric prefix N, construct a sparse tree for the Nth element
8685 of `org-todo-keywords-1'."
8687 (let ((case-fold-search nil)
8689 (cond ((null arg) org-not-done-regexp)
8691 (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
8692 (mapcar 'list org-todo-keywords-1))))
8694 (mapconcat 'identity (org-split-string kwd "|") "\\|")
8696 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
8697 (regexp-quote (nth (1- (prefix-numeric-value arg))
8698 org-todo-keywords-1)))
8699 (t (error "Invalid prefix argument: %s" arg)))))
8700 (message "%d TODO entries found"
8701 (org-occur (concat "^" outline-regexp " *" kwd-re )))))
8703 (defun org-deadline (&optional remove)
8704 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
8705 With argument REMOVE, remove any deadline from the item."
8709 (org-remove-timestamp-with-keyword org-deadline-string)
8710 (message "Item no longer has a deadline."))
8711 (org-add-planning-info 'deadline nil 'closed)))
8713 (defun org-schedule (&optional remove)
8714 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
8715 With argument REMOVE, remove any scheduling date from the item."
8719 (org-remove-timestamp-with-keyword org-scheduled-string)
8720 (message "Item is no longer scheduled."))
8721 (org-add-planning-info 'scheduled nil 'closed)))
8723 (defun org-remove-timestamp-with-keyword (keyword)
8724 "Remove all time stamps with KEYWORD in the current entry."
8725 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
8728 (org-back-to-heading t)
8730 (org-end-of-subtree t t)
8731 (while (re-search-backward re beg t)
8733 (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
8734 (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
8736 (defun org-add-planning-info (what &optional time &rest remove)
8737 "Insert new timestamp with keyword in the line directly after the headline.
8738 WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
8739 If non is given, the user is prompted for a date.
8740 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
8743 (let (org-time-was-given org-end-time-was-given ts
8744 end default-time default-input)
8746 (when (and (not time) (memq what '(scheduled deadline)))
8747 ;; Try to get a default date/time from existing timestamp
8749 (org-back-to-heading t)
8750 (setq end (save-excursion (outline-next-heading) (point)))
8751 (when (re-search-forward (if (eq what 'scheduled)
8752 org-scheduled-time-regexp
8753 org-deadline-time-regexp)
8755 (setq ts (match-string 1)
8757 (apply 'encode-time (org-parse-time-string ts))
8758 default-input (and ts (org-get-compact-tod ts))))))
8760 ;; If necessary, get the time from the user
8761 (setq time (or time (org-read-date nil 'to-time nil nil
8762 default-time default-input))))
8764 (when (and org-insert-labeled-timestamps-at-point
8765 (member what '(scheduled deadline)))
8767 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
8768 (org-insert-time-stamp time org-time-was-given
8769 nil nil nil (list org-end-time-was-given))
8773 (let (col list elt ts buffer-invisibility-spec)
8774 (org-back-to-heading t)
8775 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
8776 (goto-char (match-end 1))
8777 (setq col (current-column))
8778 (goto-char (match-end 0))
8779 (if (eobp) (insert "\n") (forward-char 1))
8780 (if (and (not (looking-at outline-regexp))
8781 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
8783 (not (equal (match-string 1) org-clock-string)))
8784 (narrow-to-region (match-beginning 0) (match-end 0))
8785 (insert-before-markers "\n")
8787 (narrow-to-region (point) (point))
8788 (indent-to-column col))
8789 ;; Check if we have to remove something.
8790 (setq list (cons what remove))
8792 (setq elt (pop list))
8793 (goto-char (point-min))
8794 (when (or (and (eq elt 'scheduled)
8795 (re-search-forward org-scheduled-time-regexp nil t))
8796 (and (eq elt 'deadline)
8797 (re-search-forward org-deadline-time-regexp nil t))
8798 (and (eq elt 'closed)
8799 (re-search-forward org-closed-time-regexp nil t)))
8801 (if (looking-at "--+<[^>]+>") (replace-match ""))
8802 (if (looking-at " +") (replace-match ""))))
8803 (goto-char (point-max))
8806 (if (not (equal (char-before) ?\ )) " " "")
8807 (cond ((eq what 'scheduled) org-scheduled-string)
8808 ((eq what 'deadline) org-deadline-string)
8809 ((eq what 'closed) org-closed-string))
8811 (setq ts (org-insert-time-stamp
8813 (or org-time-was-given
8814 (and (eq what 'closed) org-log-done-with-time))
8816 nil nil (list org-end-time-was-given)))
8818 (goto-char (point-min))
8820 (if (looking-at "[ \t]+\r?\n")
8824 (defvar org-log-note-marker (make-marker))
8825 (defvar org-log-note-purpose nil)
8826 (defvar org-log-note-state nil)
8827 (defvar org-log-note-how nil)
8828 (defvar org-log-note-window-configuration nil)
8829 (defvar org-log-note-return-to (make-marker))
8830 (defvar org-log-post-message nil
8831 "Message to be displayed after a log note has been stored.
8832 The auto-repeater uses this.")
8834 (defun org-add-note ()
8835 "Add a note to the current entry.
8836 This is done in the same way as adding a state change note."
8838 (org-add-log-setup 'note nil t nil))
8840 (defun org-add-log-setup (&optional purpose state findpos how)
8841 "Set up the post command hook to take a note.
8842 If this is about to TODO state change, the new state is expected in STATE.
8843 When FINDPOS is non-nil, find the correct position for the note in
8844 the current entry. If not, assume that it can be inserted at point."
8847 (org-back-to-heading t)
8848 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
8849 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
8851 (goto-char (match-end 0))
8852 (unless org-log-states-order-reversed
8853 (and (= (char-after) ?\n) (forward-char 1))
8854 (org-skip-over-state-notes)
8855 (skip-chars-backward " \t\n\r")))
8856 (move-marker org-log-note-marker (point))
8857 (setq org-log-note-purpose purpose
8858 org-log-note-state state
8859 org-log-note-how how)
8860 (add-hook 'post-command-hook 'org-add-log-note 'append)))
8862 (defun org-skip-over-state-notes ()
8863 "Skip past the list of State notes in an entry."
8864 (if (looking-at "\n[ \t]*- State") (forward-char 1))
8865 (while (looking-at "[ \t]*- State")
8868 (error (org-end-of-item)))))
8870 (defun org-add-log-note (&optional purpose)
8871 "Pop up a window for taking a note, and add this note later at point."
8872 (remove-hook 'post-command-hook 'org-add-log-note)
8873 (setq org-log-note-window-configuration (current-window-configuration))
8874 (delete-other-windows)
8875 (move-marker org-log-note-return-to (point))
8876 (switch-to-buffer (marker-buffer org-log-note-marker))
8877 (goto-char org-log-note-marker)
8878 (org-switch-to-buffer-other-window "*Org Note*")
8880 (if (memq org-log-note-how '(time state))
8881 (org-store-log-note)
8882 (let ((org-inhibit-startup t)) (org-mode))
8883 (insert (format "# Insert note for %s.
8884 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
8886 ((eq org-log-note-purpose 'clock-out) "stopped clock")
8887 ((eq org-log-note-purpose 'done) "closed todo item")
8888 ((eq org-log-note-purpose 'state)
8889 (format "state change to \"%s\"" org-log-note-state))
8890 ((eq org-log-note-purpose 'note)
8892 (t (error "This should not happen")))))
8893 (org-set-local 'org-finish-function 'org-store-log-note)))
8895 (defvar org-note-abort nil) ; dynamically scoped
8896 (defun org-store-log-note ()
8897 "Finish taking a log note, and insert it to where it belongs."
8898 (let ((txt (buffer-string))
8899 (note (cdr (assq org-log-note-purpose org-log-note-headings)))
8901 (kill-buffer (current-buffer))
8902 (while (string-match "\\`#.*\n[ \t\n]*" txt)
8903 (setq txt (replace-match "" t t txt)))
8904 (if (string-match "\\s-+\\'" txt)
8905 (setq txt (replace-match "" t t txt)))
8906 (setq lines (org-split-string txt "\n"))
8907 (when (and note (string-match "\\S-" note))
8909 (org-replace-escapes
8911 (list (cons "%u" (user-login-name))
8912 (cons "%U" user-full-name)
8913 (cons "%t" (format-time-string
8914 (org-time-stamp-format 'long 'inactive)
8916 (cons "%s" (if org-log-note-state
8917 (concat "\"" org-log-note-state "\"")
8919 (if lines (setq note (concat note " \\\\")))
8921 (when (or current-prefix-arg org-note-abort) (setq lines nil))
8924 (set-buffer (marker-buffer org-log-note-marker))
8926 (goto-char org-log-note-marker)
8927 (move-marker org-log-note-marker nil)
8929 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
8930 (indent-relative nil)
8931 (insert "- " (pop lines))
8932 (org-indent-line-function)
8933 (beginning-of-line 1)
8934 (looking-at "[ \t]*")
8935 (setq ind (concat (match-string 0) " "))
8937 (while lines (insert "\n" ind (pop lines)))))))
8938 (set-window-configuration org-log-note-window-configuration)
8939 (with-current-buffer (marker-buffer org-log-note-return-to)
8940 (goto-char org-log-note-return-to))
8941 (move-marker org-log-note-return-to nil)
8942 (and org-log-post-message (message "%s" org-log-post-message)))
8944 (defun org-sparse-tree (&optional arg)
8945 "Create a sparse tree, prompt for the details.
8946 This command can create sparse trees. You first need to select the type
8947 of match used to create the tree:
8949 t Show entries with a specific TODO keyword.
8950 T Show entries selected by a tags match.
8951 p Enter a property name and its value (both with completion on existing
8952 names/values) and show entries with that property.
8953 r Show entries matching a regular expression
8954 d Show deadlines due within `org-deadline-warning-days'."
8956 (let (ans kwd value)
8957 (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
8958 (setq ans (read-char-exclusive))
8961 (call-interactively 'org-check-deadlines))
8963 (call-interactively 'org-check-before-date))
8965 (org-show-todo-tree '(4)))
8967 (call-interactively 'org-tags-sparse-tree))
8968 ((member ans '(?p ?P))
8969 (setq kwd (completing-read "Property: "
8970 (mapcar 'list (org-buffer-property-keys))))
8971 (setq value (completing-read "Value: "
8972 (mapcar 'list (org-property-values kwd))))
8973 (unless (string-match "\\`{.*}\\'" value)
8974 (setq value (concat "\"" value "\"")))
8975 (org-tags-sparse-tree arg (concat kwd "=" value)))
8976 ((member ans '(?r ?R ?/))
8977 (call-interactively 'org-occur))
8978 (t (error "No such sparse tree command \"%c\"" ans)))))
8980 (defvar org-occur-highlights nil
8981 "List of overlays used for occur matches.")
8982 (make-variable-buffer-local 'org-occur-highlights)
8983 (defvar org-occur-parameters nil
8984 "Parameters of the active org-occur calls.
8985 This is a list, each call to org-occur pushes as cons cell,
8986 containing the regular expression and the callback, onto the list.
8987 The list can contain several entries if `org-occur' has been called
8988 several time with the KEEP-PREVIOUS argument. Otherwise, this list
8989 will only contain one set of parameters. When the highlights are
8990 removed (for example with `C-c C-c', or with the next edit (depending
8991 on `org-remove-highlights-with-change'), this variable is emptied
8993 (make-variable-buffer-local 'org-occur-parameters)
8995 (defun org-occur (regexp &optional keep-previous callback)
8996 "Make a compact tree which shows all matches of REGEXP.
8997 The tree will show the lines where the regexp matches, and all higher
8998 headlines above the match. It will also show the heading after the match,
8999 to make sure editing the matching entry is easy.
9000 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
9001 call to `org-occur' will be kept, to allow stacking of calls to this
9003 If CALLBACK is non-nil, it is a function which is called to confirm
9004 that the match should indeed be shown."
9005 (interactive "sRegexp: \nP")
9006 (unless keep-previous
9007 (org-remove-occur-highlights nil nil t))
9008 (push (cons regexp callback) org-occur-parameters)
9011 (goto-char (point-min))
9012 (if (or (not keep-previous) ; do not want to keep
9013 (not org-occur-highlights)) ; no previous matches
9016 (while (re-search-forward regexp nil t)
9017 (when (or (not callback)
9018 (save-match-data (funcall callback)))
9020 (when org-highlight-sparse-tree-matches
9021 (org-highlight-new-match (match-beginning 0) (match-end 0)))
9022 (org-show-context 'occur-tree))))
9023 (when org-remove-highlights-with-change
9024 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
9026 (unless org-sparse-tree-open-archived-trees
9027 (org-hide-archived-subtrees (point-min) (point-max)))
9028 (run-hooks 'org-occur-hook)
9030 (message "%d match(es) for regexp %s" cnt regexp))
9033 (defun org-show-context (&optional key)
9034 "Make sure point and context and visible.
9035 How much context is shown depends upon the variables
9036 `org-show-hierarchy-above', `org-show-following-heading'. and
9037 `org-show-siblings'."
9038 (let ((heading-p (org-on-heading-p t))
9039 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
9040 (following-p (org-get-alist-option org-show-following-heading key))
9041 (entry-p (org-get-alist-option org-show-entry-below key))
9042 (siblings-p (org-get-alist-option org-show-siblings key)))
9044 ;; Show heading or entry text
9045 (if (and heading-p (not entry-p))
9046 (org-flag-heading nil) ; only show the heading
9047 (and (or entry-p (org-invisible-p) (org-invisible-p2))
9048 (org-show-hidden-entry))) ; show entire entry
9050 ;; Show next sibling, or heading below text
9052 (and (if heading-p (org-goto-sibling) (outline-next-heading))
9053 (org-flag-heading nil))))
9054 (when siblings-p (org-show-siblings))
9056 ;; show all higher headings, possibly with siblings
9058 (while (and (condition-case nil
9059 (progn (org-up-heading-all 1) t)
9062 (org-flag-heading nil)
9063 (when siblings-p (org-show-siblings))))))))
9065 (defun org-reveal (&optional siblings)
9066 "Show current entry, hierarchy above it, and the following headline.
9067 This can be used to show a consistent set of context around locations
9068 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
9069 not t for the search context.
9071 With optional argument SIBLINGS, on each level of the hierarchy all
9072 siblings are shown. This repairs the tree structure to what it would
9073 look like when opened with hierarchical calls to `org-cycle'."
9075 (let ((org-show-hierarchy-above t)
9076 (org-show-following-heading t)
9077 (org-show-siblings (if siblings t org-show-siblings)))
9078 (org-show-context nil)))
9080 (defun org-highlight-new-match (beg end)
9081 "Highlight from BEG to END and mark the highlight is an occur headline."
9082 (let ((ov (org-make-overlay beg end)))
9083 (org-overlay-put ov 'face 'secondary-selection)
9084 (push ov org-occur-highlights)))
9086 (defun org-remove-occur-highlights (&optional beg end noremove)
9087 "Remove the occur highlights from the buffer.
9088 BEG and END are ignored. If NOREMOVE is nil, remove this function
9089 from the `before-change-functions' in the current buffer."
9091 (unless org-inhibit-highlight-removal
9092 (mapc 'org-delete-overlay org-occur-highlights)
9093 (setq org-occur-highlights nil)
9094 (setq org-occur-parameters nil)
9096 (remove-hook 'before-change-functions
9097 'org-remove-occur-highlights 'local))))
9101 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
9102 "Regular expression matching the priority indicator.")
9104 (defvar org-remove-priority-next-time nil)
9106 (defun org-priority-up ()
9107 "Increase the priority of the current item."
9111 (defun org-priority-down ()
9112 "Decrease the priority of the current item."
9114 (org-priority 'down))
9116 (defun org-priority (&optional action)
9117 "Change the priority of an item by ARG.
9118 ACTION can be `set', `up', `down', or a character."
9120 (setq action (or action 'set))
9121 (let (current new news have remove)
9123 (org-back-to-heading)
9124 (if (looking-at org-priority-regexp)
9125 (setq current (string-to-char (match-string 2))
9127 (setq current org-default-priority))
9129 ((or (eq action 'set) (integerp action))
9130 (if (integerp action)
9132 (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
9133 (setq new (read-char-exclusive)))
9134 (if (and (= (upcase org-highest-priority) org-highest-priority)
9135 (= (upcase org-lowest-priority) org-lowest-priority))
9136 (setq new (upcase new)))
9137 (cond ((equal new ?\ ) (setq remove t))
9138 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
9139 (error "Priority must be between `%c' and `%c'"
9140 org-highest-priority org-lowest-priority))))
9142 (if (and (not have) (eq last-command this-command))
9143 (setq new org-lowest-priority)
9144 (setq new (if (and org-priority-start-cycle-with-default (not have))
9145 org-default-priority (1- current)))))
9147 (if (and (not have) (eq last-command this-command))
9148 (setq new org-highest-priority)
9149 (setq new (if (and org-priority-start-cycle-with-default (not have))
9150 org-default-priority (1+ current)))))
9151 (t (error "Invalid action")))
9152 (if (or (< (upcase new) org-highest-priority)
9153 (> (upcase new) org-lowest-priority))
9155 (setq news (format "%c" new))
9158 (replace-match "" t t nil 1)
9159 (replace-match news t t nil 2))
9161 (error "No priority cookie found in line")
9162 (looking-at org-todo-line-regexp)
9165 (goto-char (match-end 2))
9166 (insert " [#" news "]"))
9167 (goto-char (match-beginning 3))
9168 (insert "[#" news "] ")))))
9169 (org-preserve-lc (org-set-tags nil 'align))
9171 (message "Priority removed")
9172 (message "Priority of current item set to %s" news))))
9175 (defun org-get-priority (s)
9176 "Find priority cookie and return priority."
9178 (if (not (string-match org-priority-regexp s))
9179 (* 1000 (- org-lowest-priority org-default-priority))
9180 (* 1000 (- org-lowest-priority
9181 (string-to-char (match-string 2 s)))))))
9185 (defun org-scan-tags (action matcher &optional todo-only)
9186 "Scan headline tags with inheritance and produce output ACTION.
9187 ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
9188 evaluated, testing if a given set of tags qualifies a headline for
9189 inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
9190 are included in the output."
9191 (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
9192 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
9194 "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
9195 (props (list 'face nil
9196 'done-face 'org-done
9198 'mouse-face 'highlight
9199 'org-not-done-regexp org-not-done-regexp
9200 'org-todo-regexp org-todo-regexp
9201 'keymap org-agenda-keymap
9203 (format "mouse-2 or RET jump to org file %s"
9204 (abbreviate-file-name
9205 (or (buffer-file-name (buffer-base-buffer))
9206 (buffer-name (buffer-base-buffer)))))))
9207 (case-fold-search nil)
9209 tags tags-list tags-alist (llast 0) rtn level category i txt
9210 todo marker entry priority)
9212 (goto-char (point-min))
9213 (when (eq action 'sparse-tree)
9215 (org-remove-occur-highlights))
9216 (while (re-search-forward re nil t)
9218 (setq todo (if (match-end 1) (match-string 2))
9219 tags (if (match-end 4) (match-string 4)))
9220 (goto-char (setq lspos (1+ (match-beginning 0))))
9221 (setq level (org-reduced-level (funcall outline-level))
9222 category (org-get-category))
9223 (setq i llast llast level)
9224 ;; remove tag lists from same and sublevels
9226 (when (setq entry (assoc i tags-alist))
9227 (setq tags-alist (delete entry tags-alist)))
9229 ;; add the next tags
9231 (setq tags (mapcar 'downcase (org-split-string tags ":"))
9233 (cons (cons level tags) tags-alist)))
9234 ;; compile tags for current headline
9236 (if org-use-tag-inheritance
9237 (apply 'append (mapcar 'cdr tags-alist))
9239 (when (and tags org-use-tag-inheritance
9240 (not (eq t org-use-tag-inheritance)))
9241 ;; selective inheritance, remove uninherited ones
9242 (setcdr (car tags-alist)
9243 (org-remove-uniherited-tags (cdar tags-alist))))
9244 (when (and (or (not todo-only) (member todo org-not-done-keywords))
9246 (or (not org-agenda-skip-archived-trees)
9247 (not (member org-archive-tag tags-list))))
9248 (and (eq action 'agenda) (org-agenda-skip))
9249 ;; list this headline
9251 (if (eq action 'sparse-tree)
9253 (and org-highlight-sparse-tree-matches
9254 (org-get-heading) (match-end 0)
9255 (org-highlight-new-match
9256 (match-beginning 0) (match-beginning 1)))
9257 (org-show-context 'tags-tree))
9258 (setq txt (org-format-agenda-item
9261 (if org-tags-match-list-sublevels
9262 (make-string (1- level) ?.) "")
9265 priority (org-get-priority txt))
9267 (setq marker (org-agenda-new-marker))
9268 (org-add-props txt props
9269 'org-marker marker 'org-hd-marker marker 'org-category category
9270 'priority priority 'type "tagsmatch")
9272 ;; if we are to skip sublevels, jump to end of subtree
9273 (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
9274 (when (and (eq action 'sparse-tree)
9275 (not org-sparse-tree-open-archived-trees))
9276 (org-hide-archived-subtrees (point-min) (point-max)))
9279 (defun org-remove-uniherited-tags (tags)
9280 "Remove all tags that are not inherited from the list TAGS."
9282 ((eq org-use-tag-inheritance t) tags)
9283 ((not org-use-tag-inheritance) nil)
9284 ((stringp org-use-tag-inheritance)
9286 (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
9288 ((listp org-use-tag-inheritance)
9289 (org-delete-all org-use-tag-inheritance tags))))
9291 (defvar todo-only) ;; dynamically scoped
9293 (defun org-tags-sparse-tree (&optional todo-only match)
9294 "Create a sparse tree according to tags string MATCH.
9295 MATCH can contain positive and negative selection of tags, like
9296 \"+WORK+URGENT-WITHBOSS\".
9297 If optional argument TODO_ONLY is non-nil, only select lines that are
9300 (org-prepare-agenda-buffers (list (current-buffer)))
9301 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
9303 (defvar org-cached-props nil)
9304 (defun org-cached-entry-get (pom property)
9305 (if (or (eq t org-use-property-inheritance)
9306 (and (stringp org-use-property-inheritance)
9307 (string-match org-use-property-inheritance property))
9308 (and (listp org-use-property-inheritance)
9309 (member property org-use-property-inheritance)))
9310 ;; Caching is not possible, check it directly
9311 (org-entry-get pom property 'inherit)
9312 ;; Get all properties, so that we can do complicated checks easily
9313 (cdr (assoc property (or org-cached-props
9314 (setq org-cached-props
9315 (org-entry-properties pom)))))))
9317 (defun org-global-tags-completion-table (&optional files)
9318 "Return the list of all tags in all agenda buffer/files."
9325 (set-buffer (find-file-noselect file))
9326 (append (org-get-buffer-tags)
9327 (mapcar (lambda (x) (if (stringp (car-safe x))
9328 (list (car-safe x)) nil))
9330 (if (and files (car files))
9332 (org-agenda-files))))))))
9334 (defun org-make-tags-matcher (match)
9335 "Create the TAGS//TODO matcher form for the selection string MATCH."
9336 ;; todo-only is scoped dynamically into this function, and the function
9337 ;; may change it it the matcher asksk for it.
9339 ;; Get a new match request, with completion
9340 (let ((org-last-tags-completion-table
9341 (org-global-tags-completion-table)))
9342 (setq match (completing-read
9343 "Match: " 'org-tags-completion-function nil nil nil
9344 'org-tags-history))))
9346 ;; Parse the string and create a lisp form
9347 (let ((match0 match)
9348 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]*\"\\)\\|[[:alnum:]_@]+\\)"))
9350 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
9351 orterms term orlist re-p level-p prop-p pn pv cat-p gv)
9352 (if (string-match "/+" match)
9353 ;; match contains also a todo-matching request
9355 (setq tagsmatch (substring match 0 (match-beginning 0))
9356 todomatch (substring match (match-end 0)))
9357 (if (string-match "^!" todomatch)
9358 (setq todo-only t todomatch (substring todomatch 1)))
9359 (if (string-match "^\\s-*$" todomatch)
9360 (setq todomatch nil)))
9361 ;; only matching tags
9362 (setq tagsmatch match todomatch nil))
9364 ;; Make the tags matcher
9365 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
9366 (setq tagsmatcher t)
9367 (setq orterms (org-split-string tagsmatch "|") orlist nil)
9368 (while (setq term (pop orterms))
9369 (while (and (equal (substring term -1) "\\") orterms)
9370 (setq term (concat term "|" (pop orterms)))) ; repair bad split
9371 (while (string-match re term)
9372 (setq minus (and (match-end 1)
9373 (equal (match-string 1 term) "-"))
9374 tag (match-string 2 term)
9375 re-p (equal (string-to-char tag) ?{)
9376 level-p (match-end 3)
9377 prop-p (match-end 4)
9379 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
9380 (level-p `(= level ,(string-to-number
9381 (match-string 3 term))))
9383 (setq pn (match-string 4 term)
9384 pv (match-string 5 term)
9385 cat-p (equal pn "CATEGORY")
9386 re-p (equal (string-to-char pv) ?{)
9387 pv (substring pv 1 -1))
9388 (if (equal pn "CATEGORY")
9389 (setq gv '(get-text-property (point) 'org-category))
9390 (setq gv `(org-cached-entry-get nil ,pn)))
9392 `(string-match ,pv (or ,gv ""))
9393 `(equal ,pv (or ,gv ""))))
9394 (t `(member ,(downcase tag) tags-list)))
9395 mm (if minus (list 'not mm) mm)
9396 term (substring term (match-end 0)))
9397 (push mm tagsmatcher))
9398 (push (if (> (length tagsmatcher) 1)
9399 (cons 'and tagsmatcher)
9402 (setq tagsmatcher nil))
9403 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
9405 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
9407 ;; Make the todo matcher
9408 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
9409 (setq todomatcher t)
9410 (setq orterms (org-split-string todomatch "|") orlist nil)
9411 (while (setq term (pop orterms))
9412 (while (string-match re term)
9413 (setq minus (and (match-end 1)
9414 (equal (match-string 1 term) "-"))
9415 kwd (match-string 2 term)
9416 re-p (equal (string-to-char kwd) ?{)
9417 term (substring term (match-end 0))
9419 `(string-match ,(substring kwd 1 -1) todo)
9420 (list 'equal 'todo kwd))
9421 mm (if minus (list 'not mm) mm))
9422 (push mm todomatcher))
9423 (push (if (> (length todomatcher) 1)
9424 (cons 'and todomatcher)
9427 (setq todomatcher nil))
9428 (setq todomatcher (if (> (length orlist) 1)
9429 (cons 'or orlist) (car orlist))))
9431 ;; Return the string and lisp forms of the matcher
9432 (setq matcher (if todomatcher
9433 (list 'and tagsmatcher todomatcher)
9435 (cons match0 matcher)))
9437 (defun org-match-any-p (re list)
9438 "Does re match any element of list?"
9439 (setq list (mapcar (lambda (x) (string-match re x)) list))
9442 (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
9443 (defvar org-tags-overlay (org-make-overlay 1 1))
9444 (org-detach-overlay org-tags-overlay)
9446 (defun org-get-tags-at (&optional pos)
9447 "Get a list of all headline tags applicable at POS.
9448 POS defaults to point. If tags are inherited, the list contains
9449 the targets in the same sequence as the headlines appear, i.e.
9450 sthe tags of the current headline come last."
9452 (let (tags ltags lastpos parent)
9456 (goto-char (or pos (point)))
9460 (org-back-to-heading t)
9461 (while (not (equal lastpos (point)))
9462 (setq lastpos (point))
9463 (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
9464 (setq ltags (org-split-string
9465 (org-match-string-no-properties 1) ":"))
9466 (setq tags (append (org-remove-uniherited-tags ltags)
9468 (or org-use-tag-inheritance (error ""))
9469 (org-up-heading-all 1)
9474 (defun org-align-tags-here (to-col)
9475 ;; Assumes that this is a headline
9476 (let ((pos (point)) (col (current-column)) tags)
9477 (beginning-of-line 1)
9478 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
9479 (< pos (match-beginning 2)))
9481 (setq tags (match-string 2))
9482 (goto-char (match-beginning 1))
9484 (delete-region (point) (1+ (match-end 0)))
9487 (max (1+ (current-column))
9491 (- (abs to-col) (length tags))))
9494 (move-to-column (min (current-column) col) t))
9497 (defun org-set-tags (&optional arg just-align)
9498 "Set the tags for the current headline.
9499 With prefix ARG, realign all tags in headings in the current buffer."
9501 (let* ((re (concat "^" outline-regexp))
9502 (current (org-get-tags-string))
9503 (col (current-column))
9504 (org-setting-tags t)
9505 table current-tags inherited-tags ; computed below when needed
9509 (goto-char (point-min))
9510 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
9511 (while (re-search-forward re nil t)
9512 (org-set-tags nil t)
9514 (message "All tags realigned to column %d" org-tags-column))
9517 ;; Get a new set of tags from the user
9519 (setq table (or org-tag-alist (org-get-buffer-tags))
9520 org-last-tags-completion-table table
9521 current-tags (org-split-string current ":")
9522 inherited-tags (nreverse
9523 (nthcdr (length current-tags)
9524 (nreverse (org-get-tags-at))))
9526 (if (or (eq t org-use-fast-tag-selection)
9527 (and org-use-fast-tag-selection
9528 (delq nil (mapcar 'cdr table))))
9529 (org-fast-tag-selection
9530 current-tags inherited-tags table
9531 (if org-fast-tag-selection-include-todo org-todo-key-alist))
9532 (let ((org-add-colon-after-tag-completion t))
9534 (org-without-partial-completion
9535 (completing-read "Tags: " 'org-tags-completion-function
9536 nil nil current 'org-tags-history)))))))
9537 (while (string-match "[-+&]+" tags)
9538 ;; No boolean logic, just a list
9539 (setq tags (replace-match ":" t t tags))))
9541 (if (string-match "\\`[\t ]*\\'" tags)
9543 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
9544 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
9546 ;; Insert new tags at the correct column
9547 (beginning-of-line 1)
9549 ((and (equal current "") (equal tags "")))
9551 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
9555 (goto-char (match-beginning 0))
9556 (setq c0 (current-column) p0 (point)
9557 c1 (max (1+ c0) (if (> org-tags-column 0)
9559 (- (- org-tags-column) (length tags))))
9560 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
9561 (replace-match rpl t t)
9562 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
9564 (t (error "Tags alignment failed")))
9565 (move-to-column col)
9567 (run-hooks 'org-after-tags-change-hook)))))
9569 (defun org-change-tag-in-region (beg end tag off)
9570 "Add or remove TAG for each entry in the region.
9571 This works in the agenda, and also in an org-mode buffer."
9573 (list (region-beginning) (region-end)
9574 (let ((org-last-tags-completion-table
9576 (org-get-buffer-tags)
9577 (org-global-tags-completion-table))))
9579 "Tag: " 'org-tags-completion-function nil nil nil
9582 (message "[s]et or [r]emove? ")
9583 (equal (read-char-exclusive) ?r))))
9584 (if (fboundp 'deactivate-mark) (deactivate-mark))
9585 (let ((agendap (equal major-mode 'org-agenda-mode))
9586 l1 l2 m buf pos newhead (cnt 0))
9588 (setq l2 (1- (org-current-line)))
9590 (setq l1 (org-current-line))
9591 (loop for l from l1 to l2 do
9593 (setq m (get-text-property (point) 'org-hd-marker))
9594 (when (or (and (org-mode-p) (org-on-heading-p))
9596 (setq buf (if agendap (marker-buffer m) (current-buffer))
9597 pos (if agendap m (point)))
9598 (with-current-buffer buf
9603 (org-toggle-tag tag (if off 'off 'on))
9604 (setq newhead (org-get-heading)))))
9605 (and agendap (org-agenda-change-all-lines newhead m))))
9606 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
9608 (defun org-tags-completion-function (string predicate &optional flag)
9609 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
9610 (confirm (lambda (x) (stringp (car x)))))
9611 (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
9612 (setq s1 (match-string 1 string)
9613 s2 (match-string 2 string))
9614 (setq s1 "" s2 string))
9618 (setq rtn (try-completion s2 ctable confirm))
9621 (concat s1 s2 (substring rtn (length s2))
9622 (if (and org-add-colon-after-tag-completion
9628 (all-completions s2 ctable confirm)
9635 (defun org-fast-tag-insert (kwd tags face &optional end)
9636 "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
9637 (insert (format "%-12s" (concat kwd ":"))
9638 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
9641 (defun org-fast-tag-show-exit (flag)
9644 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
9648 (move-to-column (- (window-width) 19) t)
9649 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
9651 (defun org-set-current-tags-overlay (current prefix)
9652 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
9653 (if (featurep 'xemacs)
9654 (org-overlay-display org-tags-overlay (concat prefix s)
9655 'secondary-selection)
9656 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
9657 (org-overlay-display org-tags-overlay (concat prefix s)))))
9659 (defun org-fast-tag-selection (current inherited table &optional todo-table)
9660 "Fast tag selection with single keys.
9661 CURRENT is the current list of tags in the headline, INHERITED is the
9662 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
9663 possibly with grouping information. TODO-TABLE is a similar table with
9664 TODO keywords, should these have keys assigned to them.
9665 If the keys are nil, a-z are automatically assigned.
9666 Returns the new tags string, or nil to not change the current settings."
9667 (let* ((fulltable (append table todo-table))
9668 (maxlen (apply 'max (mapcar
9670 (if (stringp (car x)) (string-width (car x)) 0))
9672 (buf (current-buffer))
9673 (expert (eq org-fast-tag-selection-single-key 'expert))
9675 (fwidth (+ maxlen 3 1 3))
9676 (ncol (/ (- (window-width) 4) fwidth))
9679 tg cnt e c char c1 c2 ntable tbl rtn
9680 ov-start ov-end ov-prefix
9681 (exit-after-next org-fast-tag-selection-single-key)
9682 (done-keywords org-done-keywords)
9685 (beginning-of-line 1)
9687 (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
9688 (setq ov-start (match-beginning 1)
9689 ov-end (match-end 1)
9691 (setq ov-start (1- (point-at-eol))
9692 ov-end (1+ ov-start))
9693 (skip-chars-forward "^\n\r")
9696 (buffer-substring (1- (point)) (point))
9697 (if (> (current-column) org-tags-column)
9699 (make-string (- org-tags-column (current-column)) ?\ ))))))
9700 (org-move-overlay org-tags-overlay ov-start ov-end)
9701 (save-window-excursion
9703 (set-buffer (get-buffer-create " *Org tags*"))
9704 (delete-other-windows)
9705 (split-window-vertically)
9706 (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
9708 (org-set-local 'org-done-keywords done-keywords)
9709 (org-fast-tag-insert "Inherited" inherited i-face "\n")
9710 (org-fast-tag-insert "Current" current c-face "\n\n")
9711 (org-fast-tag-show-exit exit-after-next)
9712 (org-set-current-tags-overlay current ov-prefix)
9713 (setq tbl fulltable char ?a cnt 0)
9714 (while (setq e (pop tbl))
9716 ((equal e '(:startgroup))
9717 (push '() groups) (setq ingroup t)
9718 (when (not (= cnt 0))
9722 ((equal e '(:endgroup))
9723 (setq ingroup nil cnt 0)
9726 (setq tg (car e) c2 nil)
9729 ;; automatically assign a character.
9730 (setq c1 (string-to-char
9731 (downcase (substring
9732 tg (if (= (string-to-char tg) ?@) 1 0)))))
9733 (if (or (rassoc c1 ntable) (rassoc c1 table))
9734 (while (or (rassoc char ntable) (rassoc char table))
9735 (setq char (1+ char)))
9737 (setq c (or c2 char)))
9738 (if ingroup (push tg (car groups)))
9739 (setq tg (org-add-props tg nil 'face
9741 ((not (assoc tg table))
9742 (org-get-todo-face tg))
9743 ((member tg current) c-face)
9744 ((member tg inherited) i-face)
9746 (if (and (= cnt 0) (not ingroup)) (insert " "))
9747 (insert "[" c "] " tg (make-string
9748 (- fwidth 4 (length tg)) ?\ ))
9749 (push (cons tg c) ntable)
9750 (when (= (setq cnt (1+ cnt)) ncol)
9752 (if ingroup (insert " "))
9754 (setq ntable (nreverse ntable))
9756 (goto-char (point-min))
9757 (if (and (not expert) (fboundp 'fit-window-to-buffer))
9758 (fit-window-to-buffer))
9762 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
9763 (if groups " [!] no groups" " [!]groups")
9764 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
9765 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
9767 ((= c ?\r) (throw 'exit t))
9769 (setq groups (not groups))
9770 (goto-char (point-min))
9771 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
9774 (org-fast-tag-show-exit
9775 (setq exit-after-next (not exit-after-next)))
9777 (delete-other-windows)
9778 (split-window-vertically)
9779 (org-switch-to-buffer-other-window " *Org tags*")
9780 (and (fboundp 'fit-window-to-buffer)
9781 (fit-window-to-buffer))))
9783 (and (= c ?q) (not (rassoc c ntable))))
9784 (org-detach-overlay org-tags-overlay)
9788 (if exit-after-next (setq exit-after-next 'now)))
9791 (setq tg (completing-read
9794 (with-current-buffer buf
9795 (org-get-buffer-tags)))))
9796 (quit (setq tg "")))
9797 (when (string-match "\\S-" tg)
9798 (add-to-list 'buffer-tags (list tg))
9799 (if (member tg current)
9800 (setq current (delete tg current))
9802 (if exit-after-next (setq exit-after-next 'now)))
9803 ((setq e (rassoc c todo-table) tg (car e))
9804 (with-current-buffer buf
9805 (save-excursion (org-todo tg)))
9806 (if exit-after-next (setq exit-after-next 'now)))
9807 ((setq e (rassoc c ntable) tg (car e))
9808 (if (member tg current)
9809 (setq current (delete tg current))
9810 (loop for g in groups do
9813 (setq current (delete x current)))
9816 (if exit-after-next (setq exit-after-next 'now))))
9818 ;; Create a sorted list
9822 (assoc b (cdr (memq (assoc a ntable) ntable))))))
9823 (if (eq exit-after-next 'now) (throw 'exit t))
9824 (goto-char (point-min))
9825 (beginning-of-line 2)
9826 (delete-region (point) (point-at-eol))
9827 (org-fast-tag-insert "Current" current c-face)
9828 (org-set-current-tags-overlay current ov-prefix)
9829 (while (re-search-forward
9830 (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
9831 (setq tg (match-string 1))
9832 (add-text-properties
9833 (match-beginning 1) (match-end 1)
9836 ((member tg current) c-face)
9837 ((member tg inherited) i-face)
9838 (t (get-text-property (match-beginning 1) 'face))))))
9839 (goto-char (point-min)))))
9840 (org-detach-overlay org-tags-overlay)
9842 (mapconcat 'identity current ":")
9845 (defun org-get-tags-string ()
9846 "Get the TAGS string in the current headline."
9847 (unless (org-on-heading-p t)
9848 (error "Not on a heading"))
9850 (beginning-of-line 1)
9851 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
9852 (org-match-string-no-properties 1)
9855 (defun org-get-tags ()
9856 "Get the list of tags specified in the current headline."
9857 (org-split-string (org-get-tags-string) ":"))
9859 (defun org-get-buffer-tags ()
9860 "Get a table of all tags used in the buffer, for completion."
9863 (goto-char (point-min))
9864 (while (re-search-forward
9865 (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
9866 (when (equal (char-after (point-at-bol 0)) ?*)
9867 (mapc (lambda (x) (add-to-list 'tags x))
9868 (org-split-string (org-match-string-no-properties 1) ":")))))
9869 (mapcar 'list tags)))
9874 ;;; Setting and retrieving properties
9876 (defconst org-special-properties
9877 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
9878 "TIMESTAMP" "TIMESTAMP_IA")
9879 "The special properties valid in Org-mode.
9881 These are properties that are not defined in the property drawer,
9882 but in some other way.")
9884 (defconst org-default-properties
9885 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
9886 "LOCATION" "LOGGING" "COLUMNS")
9887 "Some properties that are used by Org-mode for various purposes.
9888 Being in this list makes sure that they are offered for completion.")
9890 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
9891 "Regular expression matching the first line of a property drawer.")
9893 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
9894 "Regular expression matching the first line of a property drawer.")
9896 (defun org-property-action ()
9897 "Do an action on properties."
9901 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
9902 (setq c (read-char-exclusive))
9905 (call-interactively 'org-set-property))
9907 (call-interactively 'org-delete-property))
9909 (call-interactively 'org-delete-property-globally))
9911 (call-interactively 'org-compute-property-at-point))
9912 (t (error "No such property action %c" c)))))
9914 (defun org-at-property-p ()
9915 "Is the cursor in a property line?"
9916 ;; FIXME: Does not check if we are actually in the drawer.
9917 ;; FIXME: also returns true on any drawers.....
9918 ;; This is used by C-c C-c for property action.
9920 (beginning-of-line 1)
9921 (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
9923 (defun org-get-property-block (&optional beg end force)
9924 "Return the (beg . end) range of the body of the property drawer.
9925 BEG and END can be beginning and end of subtree, if not given
9927 If the drawer does not exist and FORCE is non-nil, create the drawer."
9930 (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
9931 (end (or end (progn (outline-next-heading) (point)))))
9933 (if (re-search-forward org-property-start-re end t)
9934 (setq beg (1+ (match-end 0)))
9937 (org-insert-property-drawer)
9938 (setq end (progn (outline-next-heading) (point))))
9941 (if (re-search-forward org-property-start-re end t)
9942 (setq beg (1+ (match-end 0)))))
9943 (if (re-search-forward org-property-end-re end t)
9944 (setq end (match-beginning 0))
9945 (or force (throw 'exit nil))
9948 (org-indent-line-function)
9952 (defun org-entry-properties (&optional pom which)
9953 "Get all properties of the entry at point-or-marker POM.
9954 This includes the TODO keyword, the tags, time strings for deadline,
9955 scheduled, and clocking, and any additional properties defined in the
9956 entry. The return value is an alist, keys may occur multiple times
9957 if the property key was used several times.
9958 POM may also be nil, in which case the current entry is used.
9959 If WHICH is nil or `all', get all properties. If WHICH is
9960 `special' or `standard', only get that subclass."
9961 (setq which (or which 'all))
9962 (org-with-point-at pom
9963 (let ((clockstr (substring org-clock-string 0 -1))
9964 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
9965 beg end range props sum-props key value string clocksum)
9967 (when (condition-case nil (org-back-to-heading t) (error nil))
9969 (setq sum-props (get-text-property (point) 'org-summaries))
9970 (setq clocksum (get-text-property (point) :org-clock-minutes))
9971 (outline-next-heading)
9973 (when (memq which '(all special))
9974 ;; Get the special properties, like TODO and tags
9976 (when (and (looking-at org-todo-line-regexp) (match-end 2))
9977 (push (cons "TODO" (org-match-string-no-properties 2)) props))
9978 (when (looking-at org-priority-regexp)
9979 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
9980 (when (and (setq value (org-get-tags-string))
9981 (string-match "\\S-" value))
9982 (push (cons "TAGS" value) props))
9983 (when (setq value (org-get-tags-at))
9984 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
9986 (while (re-search-forward org-maybe-keyword-time-regexp end t)
9987 (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
9988 string (if (equal key clockstr)
9992 (match-beginning 3) (goto-char (point-at-eol)))))
9993 (substring (org-match-string-no-properties 3) 1 -1)))
9995 (if (= (char-after (match-beginning 3)) ?\[)
9996 (setq key "TIMESTAMP_IA")
9997 (setq key "TIMESTAMP")))
9998 (when (or (equal key clockstr) (not (assoc key props)))
9999 (push (cons key string) props)))
10003 (when (memq which '(all standard))
10004 ;; Get the standard properties, like :PORP: ...
10005 (setq range (org-get-property-block beg end))
10007 (goto-char (car range))
10008 (while (re-search-forward
10009 (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
10011 (setq key (org-match-string-no-properties 1)
10012 value (org-trim (or (org-match-string-no-properties 2) "")))
10013 (unless (member key excluded)
10014 (push (cons key (or value "")) props)))))
10016 (push (cons "CLOCKSUM"
10017 (org-column-number-to-string (/ (float clocksum) 60.)
10020 (append sum-props (nreverse props)))))))
10022 (defun org-entry-get (pom property &optional inherit)
10023 "Get value of PROPERTY for entry at point-or-marker POM.
10024 If INHERIT is non-nil and the entry does not have the property,
10025 then also check higher levels of the hierarchy.
10026 If INHERIT is the symbol `selective', use inheritance only if the setting
10027 in `org-use-property-inheritance' selects PROPERTY for inheritance.
10028 If the property is present but empty, the return value is the empty string.
10029 If the property is not present at all, nil is returned."
10030 (org-with-point-at pom
10031 (if (and inherit (if (eq inherit 'selective)
10032 (org-property-inherit-p property)
10034 (org-entry-get-with-inheritance property)
10035 (if (member property org-special-properties)
10036 ;; We need a special property. Use brute force, get all properties.
10037 (cdr (assoc property (org-entry-properties nil 'special)))
10038 (let ((range (org-get-property-block)))
10040 (goto-char (car range))
10042 (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
10044 ;; Found the property, return it.
10046 (org-match-string-no-properties 1)
10049 (defun org-property-or-variable-value (var &optional inherit)
10050 "Check if there is a property fixing the value of VAR.
10051 If yes, return this value. If not, return the current value of the variable."
10052 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
10053 (if (and prop (stringp prop) (string-match "\\S-" prop))
10055 (symbol-value var))))
10057 (defun org-entry-delete (pom property)
10058 "Delete the property PROPERTY from entry at point-or-marker POM."
10059 (org-with-point-at pom
10060 (if (member property org-special-properties)
10061 nil ; cannot delete these properties.
10062 (let ((range (org-get-property-block)))
10064 (goto-char (car range))
10066 (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
10069 (delete-region (match-beginning 0) (1+ (point-at-eol)))
10073 ;; Multi-values properties are properties that contain multiple values
10074 ;; These values are assumed to be single words, separated by whitespace.
10075 (defun org-entry-add-to-multivalued-property (pom property value)
10076 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
10077 (let* ((old (org-entry-get pom property))
10078 (values (and old (org-split-string old "[ \t]"))))
10079 (unless (member value values)
10080 (setq values (cons value values))
10081 (org-entry-put pom property
10082 (mapconcat 'identity values " ")))))
10084 (defun org-entry-remove-from-multivalued-property (pom property value)
10085 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
10086 (let* ((old (org-entry-get pom property))
10087 (values (and old (org-split-string old "[ \t]"))))
10088 (when (member value values)
10089 (setq values (delete value values))
10090 (org-entry-put pom property
10091 (mapconcat 'identity values " ")))))
10093 (defun org-entry-member-in-multivalued-property (pom property value)
10094 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
10095 (let* ((old (org-entry-get pom property))
10096 (values (and old (org-split-string old "[ \t]"))))
10097 (member value values)))
10099 (defvar org-entry-property-inherited-from (make-marker))
10101 (defun org-entry-get-with-inheritance (property)
10102 "Get entry property, and search higher levels if not present."
10109 (when (setq tmp (org-entry-get nil property))
10110 (org-back-to-heading t)
10111 (move-marker org-entry-property-inherited-from (point))
10113 (or (org-up-heading-safe) (throw 'ex nil)))))
10114 (or tmp (cdr (assoc property org-local-properties))
10115 (cdr (assoc property org-global-properties))))))
10117 (defun org-entry-put (pom property value)
10118 "Set PROPERTY to VALUE for entry at point-or-marker POM."
10119 (org-with-point-at pom
10120 (org-back-to-heading t)
10121 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
10124 ((equal property "TODO")
10125 (when (and (stringp value) (string-match "\\S-" value)
10126 (not (member value org-todo-keywords-1)))
10127 (error "\"%s\" is not a valid TODO state" value))
10128 (if (or (not value)
10129 (not (string-match "\\S-" value)))
10130 (setq value 'none))
10132 (org-set-tags nil 'align))
10133 ((equal property "PRIORITY")
10134 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
10135 (string-to-char value) ?\ ))
10136 (org-set-tags nil 'align))
10137 ((equal property "SCHEDULED")
10138 (if (re-search-forward org-scheduled-time-regexp end t)
10140 ((eq value 'earlier) (org-timestamp-change -1 'day))
10141 ((eq value 'later) (org-timestamp-change 1 'day))
10142 (t (call-interactively 'org-schedule)))
10143 (call-interactively 'org-schedule)))
10144 ((equal property "DEADLINE")
10145 (if (re-search-forward org-deadline-time-regexp end t)
10147 ((eq value 'earlier) (org-timestamp-change -1 'day))
10148 ((eq value 'later) (org-timestamp-change 1 'day))
10149 (t (call-interactively 'org-deadline)))
10150 (call-interactively 'org-deadline)))
10151 ((member property org-special-properties)
10152 (error "The %s property can not yet be set with `org-entry-put'"
10154 (t ; a non-special property
10155 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
10156 (setq range (org-get-property-block beg end 'force))
10157 (goto-char (car range))
10158 (if (re-search-forward
10159 (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
10161 (delete-region (match-beginning 1) (match-end 1))
10162 (goto-char (match-beginning 1)))
10163 (goto-char (cdr range))
10166 (org-indent-line-function)
10167 (insert ":" property ":"))
10168 (and value (insert " " value))
10169 (org-indent-line-function)))))))
10171 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
10172 "Get all property keys in the current buffer.
10173 With INCLUDE-SPECIALS, also list the special properties that relect things
10174 like tags and TODO state.
10175 With INCLUDE-DEFAULTS, also include properties that has special meaning
10176 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
10177 With INCLUDE-COLUMNS, also include property names given in COLUMN
10178 formats in the current buffer."
10179 (let (rtn range cfmt cols s p)
10183 (goto-char (point-min))
10184 (while (re-search-forward org-property-start-re nil t)
10185 (setq range (org-get-property-block))
10186 (goto-char (car range))
10187 (while (re-search-forward
10188 (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
10190 (add-to-list 'rtn (org-match-string-no-properties 1)))
10191 (outline-next-heading))))
10193 (when include-specials
10194 (setq rtn (append org-special-properties rtn)))
10196 (when include-defaults
10197 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
10199 (when include-columns
10203 (goto-char (point-min))
10204 (while (re-search-forward
10205 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
10207 (setq cfmt (match-string 2) s 0)
10208 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
10210 (setq s (match-end 0)
10211 p (match-string 1 cfmt))
10212 (unless (or (equal p "ITEM")
10213 (member p org-special-properties))
10214 (add-to-list 'rtn (match-string 1 cfmt))))))))
10216 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
10218 (defun org-property-values (key)
10219 "Return a list of all values of property KEY."
10223 (goto-char (point-min))
10224 (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
10226 (while (re-search-forward re nil t)
10227 (add-to-list 'values (org-trim (match-string 1))))
10228 (delete "" values)))))
10230 (defun org-insert-property-drawer ()
10231 "Insert a property drawer into the current entry."
10233 (org-back-to-heading t)
10234 (looking-at outline-regexp)
10235 (let ((indent (- (match-end 0)(match-beginning 0)))
10237 (re (concat "^[ \t]*" org-keyword-time-regexp))
10239 (outline-next-heading)
10242 (while (re-search-forward re end t))
10243 (setq hiddenp (org-invisible-p))
10245 (and (equal (char-after) ?\n) (forward-char 1))
10246 (org-skip-over-state-notes)
10247 (skip-chars-backward " \t\n\r")
10248 (if (eq (char-before) ?*) (forward-char 1))
10249 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
10250 (beginning-of-line 0)
10251 (indent-to-column indent)
10252 (beginning-of-line 2)
10253 (indent-to-column indent)
10254 (beginning-of-line 0)
10257 (org-back-to-heading t)
10259 (org-flag-drawer t))))
10261 (defun org-set-property (property value)
10262 "In the current entry, set PROPERTY to VALUE.
10263 When called interactively, this will prompt for a property name, offering
10264 completion on existing and default properties. And then it will prompt
10265 for a value, offering competion either on allowed values (via an inherited
10266 xxx_ALL property) or on existing values in other instances of this property
10267 in the current file."
10269 (let* ((prop (completing-read
10270 "Property: " (mapcar 'list (org-buffer-property-keys nil t t))))
10271 (cur (org-entry-get nil prop))
10272 (allowed (org-property-get-allowed-values nil prop 'table))
10273 (existing (mapcar 'list (org-property-values prop)))
10275 (completing-read "Value: " allowed nil 'req-match)
10277 (concat "Value" (if (and cur (string-match "\\S-" cur))
10278 (concat "[" cur "]") "")
10280 existing nil nil "" nil cur))))
10281 (list prop (if (equal val "") cur val))))
10282 (unless (equal (org-entry-get nil property) value)
10283 (org-entry-put nil property value)))
10285 (defun org-delete-property (property)
10286 "In the current entry, delete PROPERTY."
10288 (let* ((prop (completing-read
10289 "Property: " (org-entry-properties nil 'standard))))
10291 (message "Property %s %s" property
10292 (if (org-entry-delete nil property)
10294 "was not present in the entry")))
10296 (defun org-delete-property-globally (property)
10297 "Remove PROPERTY globally, from all entries."
10299 (let* ((prop (completing-read
10300 "Globally remove property: "
10301 (mapcar 'list (org-buffer-property-keys)))))
10306 (goto-char (point-min))
10308 (while (re-search-forward
10309 (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
10311 (setq cnt (1+ cnt))
10312 (replace-match ""))
10313 (message "Property \"%s\" removed from %d entries" property cnt)))))
10315 (defvar org-columns-current-fmt-compiled) ; defined below
10317 (defun org-compute-property-at-point ()
10318 "Compute the property at point.
10319 This looks for an enclosing column format, extracts the operator and
10320 then applies it to the proerty in the column format's scope."
10322 (unless (org-at-property-p)
10323 (error "Not at a property"))
10324 (let ((prop (org-match-string-no-properties 2)))
10325 (org-columns-get-format-and-top-level)
10326 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
10327 (error "No operator defined for property %s" prop))
10328 (org-columns-compute prop)))
10330 (defun org-property-get-allowed-values (pom property &optional table)
10331 "Get allowed values for the property PROPERTY.
10332 When TABLE is non-nil, return an alist that can directly be used for
10336 ((equal property "TODO")
10337 (setq vals (org-with-point-at pom
10338 (append org-todo-keywords-1 '("")))))
10339 ((equal property "PRIORITY")
10340 (let ((n org-lowest-priority))
10341 (while (>= n org-highest-priority)
10342 (push (char-to-string n) vals)
10344 ((member property org-special-properties))
10346 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
10348 (when (and vals (string-match "\\S-" vals))
10349 (setq vals (car (read-from-string (concat "(" vals ")"))))
10350 (setq vals (mapcar (lambda (x)
10351 (cond ((stringp x) x)
10352 ((numberp x) (number-to-string x))
10353 ((symbolp x) (symbol-name x))
10356 (if table (mapcar 'list vals) vals)))
10358 (defun org-property-previous-allowed-value (&optional previous)
10359 "Switch to the next allowed value for this property."
10361 (org-property-next-allowed-value t))
10363 (defun org-property-next-allowed-value (&optional previous)
10364 "Switch to the next allowed value for this property."
10366 (unless (org-at-property-p)
10367 (error "Not at a property"))
10368 (let* ((key (match-string 2))
10369 (value (match-string 3))
10370 (allowed (or (org-property-get-allowed-values (point) key)
10371 (and (member value '("[ ]" "[-]" "[X]"))
10375 (error "Allowed values for this property have not been defined"))
10376 (if previous (setq allowed (reverse allowed)))
10377 (if (member value allowed)
10378 (setq nval (car (cdr (member value allowed)))))
10379 (setq nval (or nval (car allowed)))
10380 (if (equal nval value)
10381 (error "Only one allowed value for this property"))
10382 (org-at-property-p)
10383 (replace-match (concat " :" key ": " nval) t t)
10384 (org-indent-line-function)
10385 (beginning-of-line 1)
10386 (skip-chars-forward " \t")))
10388 (defun org-find-entry-with-id (ident)
10389 "Locate the entry that contains the ID property with exact value IDENT.
10390 IDENT can be a string, a symbol or a number, this function will search for
10391 the string representation of it.
10392 Return the position where this entry starts, or nil if there is no such entry."
10394 ((stringp ident) ident)
10395 ((symbol-name ident) (symbol-name ident))
10396 ((numberp ident) (number-to-string ident))
10397 (t (error "IDENT %s must be a string, symbol or number" ident))))
10398 (case-fold-search nil))
10402 (goto-char (point-min))
10403 (when (re-search-forward
10404 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
10406 (org-back-to-heading)
10411 (defvar org-columns-overlays nil
10412 "Holds the list of current column overlays.")
10414 (defvar org-columns-current-fmt nil
10415 "Local variable, holds the currently active column format.")
10416 (defvar org-columns-current-fmt-compiled nil
10417 "Local variable, holds the currently active column format.
10418 This is the compiled version of the format.")
10419 (defvar org-columns-current-widths nil
10420 "Loval variable, holds the currently widths of fields.")
10421 (defvar org-columns-current-maxwidths nil
10422 "Loval variable, holds the currently active maximum column widths.")
10423 (defvar org-columns-begin-marker (make-marker)
10424 "Points to the position where last a column creation command was called.")
10425 (defvar org-columns-top-level-marker (make-marker)
10426 "Points to the position where current columns region starts.")
10428 (defvar org-columns-map (make-sparse-keymap)
10429 "The keymap valid in column display.")
10431 (defun org-columns-content ()
10432 "Switch to contents view while in columns view."
10437 (org-defkey org-columns-map "c" 'org-columns-content)
10438 (org-defkey org-columns-map "o" 'org-overview)
10439 (org-defkey org-columns-map "e" 'org-columns-edit-value)
10440 (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
10441 (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
10442 (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
10443 (org-defkey org-columns-map "v" 'org-columns-show-value)
10444 (org-defkey org-columns-map "q" 'org-columns-quit)
10445 (org-defkey org-columns-map "r" 'org-columns-redo)
10446 (org-defkey org-columns-map "g" 'org-columns-redo)
10447 (org-defkey org-columns-map [left] 'backward-char)
10448 (org-defkey org-columns-map "\M-b" 'backward-char)
10449 (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
10450 (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
10451 (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
10452 (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
10453 (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
10454 (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
10455 (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
10456 (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
10457 (org-defkey org-columns-map "<" 'org-columns-narrow)
10458 (org-defkey org-columns-map ">" 'org-columns-widen)
10459 (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
10460 (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
10461 (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
10462 (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
10464 (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
10466 ["Edit property" org-columns-edit-value t]
10467 ["Next allowed value" org-columns-next-allowed-value t]
10468 ["Previous allowed value" org-columns-previous-allowed-value t]
10469 ["Show full value" org-columns-show-value t]
10470 ["Edit allowed values" org-columns-edit-allowed t]
10472 ["Edit column attributes" org-columns-edit-attributes t]
10473 ["Increase column width" org-columns-widen t]
10474 ["Decrease column width" org-columns-narrow t]
10476 ["Move column right" org-columns-move-right t]
10477 ["Move column left" org-columns-move-left t]
10478 ["Add column" org-columns-new t]
10479 ["Delete column" org-columns-delete t]
10481 ["CONTENTS" org-columns-content t]
10482 ["OVERVIEW" org-overview t]
10483 ["Refresh columns display" org-columns-redo t]
10485 ["Open link" org-columns-open-link t]
10487 ["Quit" org-columns-quit t]))
10489 (defun org-columns-new-overlay (beg end &optional string face)
10490 "Create a new column overlay and add it to the list."
10491 (let ((ov (org-make-overlay beg end)))
10492 (org-overlay-put ov 'face (or face 'secondary-selection))
10493 (org-overlay-display ov string face)
10494 (push ov org-columns-overlays)
10497 (defun org-columns-display-here (&optional props)
10498 "Overlay the current line with column display."
10500 (let* ((fmt org-columns-current-fmt-compiled)
10501 (beg (point-at-bol))
10502 (level-face (save-excursion
10503 (beginning-of-line 1)
10504 (and (looking-at "\\(\\**\\)\\(\\* \\)")
10505 (org-get-level-face 2))))
10506 (color (list :foreground
10507 (face-attribute (or level-face 'default) :foreground)))
10508 props pom property ass width f string ov column val modval)
10509 ;; Check if the entry is in another buffer.
10511 (if (eq major-mode 'org-agenda-mode)
10512 (setq pom (or (get-text-property (point) 'org-hd-marker)
10513 (get-text-property (point) 'org-marker))
10514 props (if pom (org-entry-properties pom) nil))
10515 (setq props (org-entry-properties nil))))
10517 (while (setq column (pop fmt))
10518 (setq property (car column)
10519 ass (if (equal property "ITEM")
10524 (buffer-substring-no-properties
10525 (point-at-bol) (point-at-eol))))))
10526 (assoc property props))
10527 width (or (cdr (assoc property org-columns-current-maxwidths))
10530 f (format "%%-%d.%ds | " width width)
10531 val (or (cdr ass) "")
10532 modval (if (equal property "ITEM")
10533 (org-columns-cleanup-item val org-columns-current-fmt-compiled))
10534 string (format f (or modval val)))
10535 ;; Create the overlay
10537 (setq ov (org-columns-new-overlay
10538 beg (setq beg (1+ beg)) string
10539 (list color 'org-column)))
10540 (org-overlay-put ov 'keymap org-columns-map)
10541 (org-overlay-put ov 'org-columns-key property)
10542 (org-overlay-put ov 'org-columns-value (cdr ass))
10543 (org-overlay-put ov 'org-columns-value-modified modval)
10544 (org-overlay-put ov 'org-columns-pom pom)
10545 (org-overlay-put ov 'org-columns-format f))
10546 (if (or (not (char-after beg))
10547 (equal (char-after beg) ?\n))
10548 (let ((inhibit-read-only t))
10551 (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
10552 ;; Make the rest of the line disappear.
10554 (setq ov (org-columns-new-overlay beg (point-at-eol)))
10555 (org-overlay-put ov 'invisible t)
10556 (org-overlay-put ov 'keymap org-columns-map)
10557 (org-overlay-put ov 'intangible t)
10558 (push ov org-columns-overlays)
10559 (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
10560 (org-overlay-put ov 'keymap org-columns-map)
10561 (push ov org-columns-overlays)
10562 (let ((inhibit-read-only t))
10563 (put-text-property (max (point-min) (1- (point-at-bol)))
10564 (min (point-max) (1+ (point-at-eol)))
10565 'read-only "Type `e' to edit property")))))
10567 (defvar org-columns-full-header-line-format nil
10568 "Fthe full header line format, will be shifted by horizontal scrolling." )
10569 (defvar org-previous-header-line-format nil
10570 "The header line format before column view was turned on.")
10571 (defvar org-columns-inhibit-recalculation nil
10572 "Inhibit recomputing of columns on column view startup.")
10575 (defvar header-line-format)
10576 (defvar org-columns-previous-hscroll 0)
10577 (defun org-columns-display-here-title ()
10578 "Overlay the newline before the current line with the table title."
10580 (let ((fmt org-columns-current-fmt-compiled)
10582 property width f column str widths)
10583 (while (setq column (pop fmt))
10584 (setq property (car column)
10585 str (or (nth 1 column) property)
10586 width (or (cdr (assoc property org-columns-current-maxwidths))
10589 widths (push width widths)
10590 f (format "%%-%d.%ds | " width width)
10591 string (format f str)
10592 title (concat title string)))
10593 (setq title (concat
10594 (org-add-props " " nil 'display '(space :align-to 0))
10595 (org-add-props title nil 'face '(:weight bold :underline t :inherit default))))
10596 (org-set-local 'org-previous-header-line-format header-line-format)
10597 (org-set-local 'org-columns-current-widths (nreverse widths))
10598 (setq org-columns-full-header-line-format title)
10599 (setq org-columns-previous-hscroll -1)
10600 ; (org-columns-hscoll-title)
10601 (org-add-hook 'post-command-hook 'org-columns-hscoll-title nil 'local)))
10603 (defun org-columns-hscoll-title ()
10604 "Set the header-line-format so that it scrolls along with the table."
10605 (sit-for .0001) ; need to force a redisplay to update window-hscroll
10606 (when (not (= (window-hscroll) org-columns-previous-hscroll))
10607 (setq header-line-format
10608 (concat (substring org-columns-full-header-line-format 0 1)
10609 (substring org-columns-full-header-line-format
10610 (1+ (window-hscroll))))
10611 org-columns-previous-hscroll (window-hscroll))
10612 (force-mode-line-update)))
10614 (defun org-columns-remove-overlays ()
10615 "Remove all currently active column overlays."
10617 (when (marker-buffer org-columns-begin-marker)
10618 (with-current-buffer (marker-buffer org-columns-begin-marker)
10619 (when (local-variable-p 'org-previous-header-line-format)
10620 (setq header-line-format org-previous-header-line-format)
10621 (kill-local-variable 'org-previous-header-line-format)
10622 (remove-hook 'post-command-hook 'org-columns-hscoll-title 'local))
10623 (move-marker org-columns-begin-marker nil)
10624 (move-marker org-columns-top-level-marker nil)
10626 (mapc 'org-delete-overlay org-columns-overlays)
10627 (setq org-columns-overlays nil)
10628 (let ((inhibit-read-only t))
10629 (remove-text-properties (point-min) (point-max) '(read-only t)))))))
10631 (defun org-columns-cleanup-item (item fmt)
10632 "Remove from ITEM what is a column in the format FMT."
10633 (if (not org-complex-heading-regexp)
10635 (when (string-match org-complex-heading-regexp item)
10637 (org-add-props (concat (match-string 1 item) " ") nil
10638 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
10639 (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
10640 (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
10641 " " (match-string 4 item)
10642 (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
10644 (defun org-columns-show-value ()
10645 "Show the full value of the property."
10647 (let ((value (get-char-property (point) 'org-columns-value)))
10648 (message "Value is: %s" (or value ""))))
10650 (defun org-columns-quit ()
10651 "Remove the column overlays and in this way exit column editing."
10654 (org-columns-remove-overlays)
10655 (let ((inhibit-read-only t))
10656 (remove-text-properties (point-min) (point-max) '(read-only t))))
10657 (when (eq major-mode 'org-agenda-mode)
10659 "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
10661 (defun org-columns-check-computed ()
10662 "Check if this column value is computed.
10663 If yes, throw an error indicating that changing it does not make sense."
10664 (let ((val (get-char-property (point) 'org-columns-value)))
10665 (when (and (stringp val)
10666 (get-char-property 0 'org-computed val))
10667 (error "This value is computed from the entry's children"))))
10669 (defun org-columns-todo (&optional arg)
10670 "Change the TODO state during column view."
10672 (org-columns-edit-value "TODO"))
10674 (defun org-columns-set-tags-or-toggle (&optional arg)
10675 "Toggle checkbox at point, or set tags for current headline."
10677 (if (string-match "\\`\\[[ xX-]\\]\\'"
10678 (get-char-property (point) 'org-columns-value))
10679 (org-columns-next-allowed-value)
10680 (org-columns-edit-value "TAGS")))
10682 (defun org-columns-edit-value (&optional key)
10683 "Edit the value of the property at point in column view.
10684 Where possible, use the standard interface for changing this line."
10686 (org-columns-check-computed)
10687 (let* ((external-key key)
10688 (col (current-column))
10689 (key (or key (get-char-property (point) 'org-columns-key)))
10690 (value (get-char-property (point) 'org-columns-value))
10691 (bol (point-at-bol)) (eol (point-at-eol))
10692 (pom (or (get-text-property bol 'org-hd-marker)
10693 (point))) ; keep despite of compiler waring
10695 (delq nil (mapcar (lambda (x)
10696 (and (eq (overlay-buffer x) (current-buffer))
10697 (>= (overlay-start x) bol)
10698 (<= (overlay-start x) eol)
10700 org-columns-overlays)))
10703 ((equal key "CLOCKSUM")
10704 (error "This special column cannot be edited"))
10705 ((equal key "ITEM")
10706 (setq eval '(org-with-point-at pom
10707 (org-edit-headline))))
10708 ((equal key "TODO")
10709 (setq eval '(org-with-point-at pom
10710 (let ((current-prefix-arg
10711 (if external-key current-prefix-arg '(4))))
10712 (call-interactively 'org-todo)))))
10713 ((equal key "PRIORITY")
10714 (setq eval '(org-with-point-at pom
10715 (call-interactively 'org-priority))))
10716 ((equal key "TAGS")
10717 (setq eval '(org-with-point-at pom
10718 (let ((org-fast-tag-selection-single-key
10719 (if (eq org-fast-tag-selection-single-key 'expert)
10720 t org-fast-tag-selection-single-key)))
10721 (call-interactively 'org-set-tags)))))
10722 ((equal key "DEADLINE")
10723 (setq eval '(org-with-point-at pom
10724 (call-interactively 'org-deadline))))
10725 ((equal key "SCHEDULED")
10726 (setq eval '(org-with-point-at pom
10727 (call-interactively 'org-schedule))))
10729 (setq allowed (org-property-get-allowed-values pom key 'table))
10731 (setq nval (completing-read "Value: " allowed nil t))
10732 (setq nval (read-string "Edit: " value)))
10733 (setq nval (org-trim nval))
10734 (when (not (equal nval value))
10735 (setq eval '(org-entry-put pom key nval)))))
10737 (let ((inhibit-read-only t))
10738 (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
10741 (setq org-columns-overlays
10742 (org-delete-all line-overlays org-columns-overlays))
10743 (mapc 'org-delete-overlay line-overlays)
10744 (org-columns-eval eval))
10745 (org-columns-display-here))))
10746 (move-to-column col)
10747 (if (and (org-mode-p)
10748 (nth 3 (assoc key org-columns-current-fmt-compiled)))
10749 (org-columns-update key))))
10751 (defun org-edit-headline () ; FIXME: this is not columns specific. Make interactive????? Use from agenda????
10752 "Edit the current headline, the part without TODO keyword, TAGS."
10753 (org-back-to-heading)
10754 (when (looking-at org-todo-line-regexp)
10755 (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
10756 (txt (match-string 3))
10759 (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
10760 (setq post (match-string 0 txt)
10761 txt (substring txt 0 (match-beginning 0))))
10762 (setq txt2 (read-string "Edit: " txt))
10763 (when (not (equal txt txt2))
10764 (beginning-of-line 1)
10765 (insert pre txt2 post)
10766 (delete-region (point) (point-at-eol))
10767 (org-set-tags nil t)))))
10769 (defun org-columns-edit-allowed ()
10770 "Edit the list of allowed values for the current property."
10772 (let* ((key (get-char-property (point) 'org-columns-key))
10773 (key1 (concat key "_ALL"))
10774 (allowed (org-entry-get (point) key1 t))
10776 ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
10777 (setq nval (read-string "Allowed: " allowed))
10779 (cond ((marker-position org-entry-property-inherited-from)
10780 org-entry-property-inherited-from)
10781 ((marker-position org-columns-top-level-marker)
10782 org-columns-top-level-marker))
10785 (defun org-columns-eval (form)
10788 (beginning-of-line 1)
10789 ;; `next-line' is needed here, because it skips invisible line.
10790 (condition-case nil (org-no-warnings (next-line 1)) (error nil))
10791 (setq hidep (org-on-heading-p 1)))
10793 (and hidep (hide-entry))))
10795 (defun org-columns-previous-allowed-value ()
10796 "Switch to the previous allowed value for this column."
10798 (org-columns-next-allowed-value t))
10800 (defun org-columns-next-allowed-value (&optional previous)
10801 "Switch to the next allowed value for this column."
10803 (org-columns-check-computed)
10804 (let* ((col (current-column))
10805 (key (get-char-property (point) 'org-columns-key))
10806 (value (get-char-property (point) 'org-columns-value))
10807 (bol (point-at-bol)) (eol (point-at-eol))
10808 (pom (or (get-text-property bol 'org-hd-marker)
10809 (point))) ; keep despite of compiler waring
10811 (delq nil (mapcar (lambda (x)
10812 (and (eq (overlay-buffer x) (current-buffer))
10813 (>= (overlay-start x) bol)
10814 (<= (overlay-start x) eol)
10816 org-columns-overlays)))
10817 (allowed (or (org-property-get-allowed-values pom key)
10819 (nth 4 (assoc key org-columns-current-fmt-compiled))
10820 '(checkbox checkbox-n-of-m checkbox-percent))
10823 (when (equal key "ITEM")
10824 (error "Cannot edit item headline from here"))
10825 (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
10826 (error "Allowed values for this property have not been defined"))
10827 (if (member key '("SCHEDULED" "DEADLINE"))
10828 (setq nval (if previous 'earlier 'later))
10829 (if previous (setq allowed (reverse allowed)))
10830 (if (member value allowed)
10831 (setq nval (car (cdr (member value allowed)))))
10832 (setq nval (or nval (car allowed)))
10833 (if (equal nval value)
10834 (error "Only one allowed value for this property")))
10835 (let ((inhibit-read-only t))
10836 (remove-text-properties (1- bol) eol '(read-only t))
10839 (setq org-columns-overlays
10840 (org-delete-all line-overlays org-columns-overlays))
10841 (mapc 'org-delete-overlay line-overlays)
10842 (org-columns-eval '(org-entry-put pom key nval)))
10843 (org-columns-display-here)))
10844 (move-to-column col)
10845 (if (and (org-mode-p)
10846 (nth 3 (assoc key org-columns-current-fmt-compiled)))
10847 (org-columns-update key))))
10849 (defun org-verify-version (task)
10851 ((eq task 'columns)
10852 (if (or (featurep 'xemacs)
10853 (< emacs-major-version 22))
10854 (error "Emacs 22 is required for the columns feature")))))
10856 (defun org-columns-open-link (&optional arg)
10858 (let ((value (get-char-property (point) 'org-columns-value)))
10859 (org-open-link-from-string value arg)))
10861 (defun org-open-link-from-string (s &optional arg)
10862 "Open a link in the string S, as if it was in Org-mode."
10865 (let ((org-inhibit-startup t))
10868 (goto-char (point-min))
10869 (org-open-at-point arg))))
10871 (defun org-columns-get-format-and-top-level ()
10873 (when (condition-case nil (org-back-to-heading) (error nil))
10874 (move-marker org-entry-property-inherited-from nil)
10875 (setq fmt (org-entry-get nil "COLUMNS" t)))
10876 (setq fmt (or fmt org-columns-default-format))
10877 (org-set-local 'org-columns-current-fmt fmt)
10878 (org-columns-compile-format fmt)
10879 (if (marker-position org-entry-property-inherited-from)
10880 (move-marker org-columns-top-level-marker
10881 org-entry-property-inherited-from)
10882 (move-marker org-columns-top-level-marker (point)))
10885 (defun org-columns ()
10886 "Turn on column view on an org-mode file."
10888 (org-verify-version 'columns)
10889 (org-columns-remove-overlays)
10890 (move-marker org-columns-begin-marker (point))
10891 (let (beg end fmt cache maxwidths)
10892 (setq fmt (org-columns-get-format-and-top-level))
10894 (goto-char org-columns-top-level-marker)
10896 (unless org-columns-inhibit-recalculation
10897 (org-columns-compute-all))
10898 (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
10900 ;; Get and cache the properties
10902 (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
10905 (narrow-to-region beg end)
10907 (while (re-search-forward (concat "^" outline-regexp) end t)
10908 (push (cons (org-current-line) (org-entry-properties)) cache))
10910 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
10911 (org-set-local 'org-columns-current-maxwidths maxwidths)
10912 (org-columns-display-here-title)
10914 (goto-line (car x))
10915 (org-columns-display-here (cdr x)))
10918 (defun org-columns-new (&optional prop title width op fmt &rest rest)
10919 "Insert a new column, to the left of the current column."
10921 (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
10923 (setq prop (completing-read
10924 "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
10926 (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
10927 (setq width (read-string "Column width: " (if width (number-to-string width))))
10928 (if (string-match "\\S-" width)
10929 (setq width (string-to-number width))
10931 (setq fmt (completing-read "Summary [none]: "
10932 '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
10934 (if (string-match "\\S-" fmt)
10935 (setq fmt (intern fmt))
10937 (if (eq fmt 'none) (setq fmt nil))
10940 (setcar editp prop)
10941 (setcdr editp (list title width nil fmt)))
10942 (setq cell (nthcdr (1- (current-column))
10943 org-columns-current-fmt-compiled))
10944 (setcdr cell (cons (list prop title width nil fmt)
10946 (org-columns-store-format)
10947 (org-columns-redo)))
10949 (defun org-columns-delete ()
10950 "Delete the column at point from columns view."
10952 (let* ((n (current-column))
10953 (title (nth 1 (nth n org-columns-current-fmt-compiled))))
10955 (format "Are you sure you want to remove column \"%s\"? " title))
10956 (setq org-columns-current-fmt-compiled
10957 (delq (nth n org-columns-current-fmt-compiled)
10958 org-columns-current-fmt-compiled))
10959 (org-columns-store-format)
10961 (if (>= (current-column) (length org-columns-current-fmt-compiled))
10962 (backward-char 1)))))
10964 (defun org-columns-edit-attributes ()
10965 "Edit the attributes of the current column."
10967 (let* ((n (current-column))
10968 (info (nth n org-columns-current-fmt-compiled)))
10969 (apply 'org-columns-new info)))
10971 (defun org-columns-widen (arg)
10972 "Make the column wider by ARG characters."
10974 (let* ((n (current-column))
10975 (entry (nth n org-columns-current-fmt-compiled))
10976 (width (or (nth 2 entry)
10977 (cdr (assoc (car entry) org-columns-current-maxwidths)))))
10978 (setq width (max 1 (+ width arg)))
10979 (setcar (nthcdr 2 entry) width)
10980 (org-columns-store-format)
10981 (org-columns-redo)))
10983 (defun org-columns-narrow (arg)
10984 "Make the column nrrower by ARG characters."
10986 (org-columns-widen (- arg)))
10988 (defun org-columns-move-right ()
10989 "Swap this column with the one to the right."
10991 (let* ((n (current-column))
10992 (cell (nthcdr n org-columns-current-fmt-compiled))
10994 (when (>= n (1- (length org-columns-current-fmt-compiled)))
10995 (error "Cannot shift this column further to the right"))
10996 (setq e (car cell))
10997 (setcar cell (car (cdr cell)))
10998 (setcdr cell (cons e (cdr (cdr cell))))
10999 (org-columns-store-format)
11003 (defun org-columns-move-left ()
11004 "Swap this column with the one to the left."
11006 (let* ((n (current-column)))
11008 (error "Cannot shift this column further to the left"))
11010 (org-columns-move-right)
11011 (backward-char 1)))
11013 (defun org-columns-store-format ()
11014 "Store the text version of the current columns format in appropriate place.
11015 This is either in the COLUMNS property of the node starting the current column
11016 display, or in the #+COLUMNS line of the current buffer."
11018 (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
11019 (org-set-local 'org-columns-current-fmt fmt)
11020 (if (marker-position org-columns-top-level-marker)
11022 (goto-char org-columns-top-level-marker)
11023 (if (and (org-at-heading-p)
11024 (org-entry-get nil "COLUMNS"))
11025 (org-entry-put nil "COLUMNS" fmt)
11026 (goto-char (point-min))
11027 ;; Overwrite all #+COLUMNS lines....
11028 (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
11029 (setq cnt (1+ cnt))
11030 (replace-match (concat "#+COLUMNS: " fmt) t t))
11032 (goto-char (point-min))
11033 (or (org-on-heading-p t) (outline-next-heading))
11034 (let ((inhibit-read-only t))
11035 (insert-before-markers "#+COLUMNS: " fmt "\n")))
11036 (org-set-local 'org-columns-default-format fmt))))))
11038 (defvar org-overriding-columns-format nil
11039 "When set, overrides any other definition.")
11040 (defvar org-agenda-view-columns-initially nil
11041 "When set, switch to columns view immediately after creating the agenda.")
11043 (defun org-agenda-columns ()
11044 "Turn on column view in the agenda."
11046 (org-verify-version 'columns)
11047 (org-columns-remove-overlays)
11048 (move-marker org-columns-begin-marker (point))
11049 (let (fmt cache maxwidths m)
11051 ((and (local-variable-p 'org-overriding-columns-format)
11052 org-overriding-columns-format)
11053 (setq fmt org-overriding-columns-format))
11054 ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
11055 (setq fmt (or (org-entry-get m "COLUMNS" t)
11056 (with-current-buffer (marker-buffer m)
11057 org-columns-default-format))))
11058 ((and (boundp 'org-columns-current-fmt)
11059 (local-variable-p 'org-columns-current-fmt)
11060 org-columns-current-fmt)
11061 (setq fmt org-columns-current-fmt))
11062 ((setq m (next-single-property-change (point-min) 'org-hd-marker))
11063 (setq m (get-text-property m 'org-hd-marker))
11064 (setq fmt (or (org-entry-get m "COLUMNS" t)
11065 (with-current-buffer (marker-buffer m)
11066 org-columns-default-format)))))
11067 (setq fmt (or fmt org-columns-default-format))
11068 (org-set-local 'org-columns-current-fmt fmt)
11069 (org-columns-compile-format fmt)
11071 ;; Get and cache the properties
11072 (goto-char (point-min))
11073 (while (not (eobp))
11074 (when (setq m (or (get-text-property (point) 'org-hd-marker)
11075 (get-text-property (point) 'org-marker)))
11076 (push (cons (org-current-line) (org-entry-properties m)) cache))
11077 (beginning-of-line 2))
11079 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
11080 (org-set-local 'org-columns-current-maxwidths maxwidths)
11081 (org-columns-display-here-title)
11083 (goto-line (car x))
11084 (org-columns-display-here (cdr x)))
11087 (defun org-columns-get-autowidth-alist (s cache)
11088 "Derive the maximum column widths from the format and the cache."
11089 (let ((start 0) rtn)
11090 (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
11091 (push (cons (match-string 1 s) 1) rtn)
11092 (setq start (match-end 0)))
11094 (setcdr x (apply 'max
11097 (length (or (cdr (assoc (car x) (cdr y))) " ")))
11102 (defun org-columns-compute-all ()
11103 "Compute all columns that have operators defined."
11105 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
11106 (let ((columns org-columns-current-fmt-compiled) col)
11107 (while (setq col (pop columns))
11110 (org-columns-compute (car col)))))))
11112 (defun org-columns-update (property)
11113 "Recompute PROPERTY, and update the columns display for it."
11114 (org-columns-compute property)
11118 (when (equal (org-overlay-get ov 'org-columns-key) property)
11119 (setq pos (org-overlay-start ov))
11121 (when (setq val (cdr (assoc property
11123 (point-at-bol) 'org-summaries))))
11124 (setq fmt (org-overlay-get ov 'org-columns-format))
11125 (org-overlay-put ov 'org-columns-value val)
11126 (org-overlay-put ov 'display (format fmt val)))))
11127 org-columns-overlays))))
11129 (defun org-columns-compute (property)
11130 "Sum the values of property PROPERTY hierarchically, for the entire buffer."
11132 (let* ((re (concat "^" outline-regexp))
11133 (lmax 30) ; Does anyone use deeper levels???
11134 (lsum (make-vector lmax 0))
11135 (lflag (make-vector lmax nil))
11137 (ass (assoc property org-columns-current-fmt-compiled))
11138 (format (nth 4 ass))
11139 (printf (nth 5 ass))
11140 (beg org-columns-top-level-marker)
11141 last-level val valflag flag end sumpos sum-alist sum str str1 useval)
11143 ;; Find the region to compute
11145 (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
11147 ;; Walk the tree from the back and do the computations
11148 (while (re-search-backward re beg t)
11149 (setq sumpos (match-beginning 0)
11151 level (org-outline-level)
11152 val (org-entry-get nil property)
11153 valflag (and val (string-match "\\S-" val)))
11155 ((< level last-level)
11156 ;; put the sum of lower levels here as a property
11157 (setq sum (aref lsum last-level) ; current sum
11158 flag (aref lflag last-level) ; any valid entries from children?
11159 str (org-column-number-to-string sum format printf)
11160 str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
11161 useval (if flag str1 (if valflag val ""))
11162 sum-alist (get-text-property sumpos 'org-summaries))
11163 (if (assoc property sum-alist)
11164 (setcdr (assoc property sum-alist) useval)
11165 (push (cons property useval) sum-alist)
11167 (add-text-properties sumpos (1+ sumpos)
11168 (list 'org-summaries sum-alist))))
11170 (org-entry-put nil property (if flag str val)))
11171 ;; add current to current level accumulator
11172 (when (or flag valflag)
11173 (aset lsum level (+ (aref lsum level)
11174 (if flag sum (org-column-string-to-number
11175 (if flag str val) format))))
11176 (aset lflag level t))
11177 ;; clear accumulators for deeper levels
11178 (loop for l from (1+ level) to (1- lmax) do
11180 (aset lflag l nil)))
11181 ((>= level last-level)
11182 ;; add what we have here to the accumulator for this level
11183 (aset lsum level (+ (aref lsum level)
11184 (org-column-string-to-number (or val "0") format)))
11185 (and valflag (aset lflag level t)))
11186 (t (error "This should not happen")))))))
11188 (defun org-columns-redo ()
11189 "Construct the column display again."
11191 (message "Recomputing columns...")
11193 (if (marker-position org-columns-begin-marker)
11194 (goto-char org-columns-begin-marker))
11195 (org-columns-remove-overlays)
11197 (call-interactively 'org-columns)
11198 (call-interactively 'org-agenda-columns)))
11199 (message "Recomputing columns...done"))
11201 (defun org-columns-not-in-agenda ()
11202 (if (eq major-mode 'org-agenda-mode)
11203 (error "This command is only allowed in Org-mode buffers")))
11206 (defun org-string-to-number (s)
11207 "Convert string to number, and interpret hh:mm:ss."
11208 (if (not (string-match ":" s))
11209 (string-to-number s)
11210 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
11212 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
11215 (defun org-column-number-to-string (n fmt &optional printf)
11216 "Convert a computed column number to a string value, according to FMT."
11218 ((eq fmt 'add_times)
11219 (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
11220 (format "%d:%02d" h m)))
11221 ((eq fmt 'checkbox)
11222 (cond ((= n (floor n)) "[X]")
11225 ((memq fmt '(checkbox-n-of-m checkbox-percent))
11226 (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
11227 (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
11228 (printf (format printf n))
11229 ((eq fmt 'currency)
11231 (t (number-to-string n))))
11233 (defun org-nofm-to-completion (n m &optional percent)
11235 (format "[%d/%d]" n m)
11236 (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
11238 (defun org-column-string-to-number (s fmt)
11239 "Convert a column value to a number that can be used for column computing."
11241 ((string-match ":" s)
11242 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
11244 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
11246 ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
11247 (if (equal s "[X]") 1. 0.000001))
11248 (t (string-to-number s))))
11250 (defun org-columns-uncompile-format (cfmt)
11251 "Turn the compiled columns format back into a string representation."
11252 (let ((rtn "") e s prop title op width fmt printf)
11253 (while (setq e (pop cfmt))
11261 ((eq fmt 'add_times) (setq op ":"))
11262 ((eq fmt 'checkbox) (setq op "X"))
11263 ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
11264 ((eq fmt 'checkbox-percent) (setq op "X%"))
11265 ((eq fmt 'add_numbers) (setq op "+"))
11266 ((eq fmt 'currency) (setq op "$")))
11267 (if (and op printf) (setq op (concat op ";" printf)))
11268 (if (equal title prop) (setq title nil))
11269 (setq s (concat "%" (if width (number-to-string width))
11271 (if title (concat "(" title ")"))
11272 (if op (concat "{" op "}"))))
11273 (setq rtn (concat rtn " " s)))
11276 (defun org-columns-compile-format (fmt)
11277 "Turn a column format string into an alist of specifications.
11278 The alist has one entry for each column in the format. The elements of
11280 property the property
11281 title the title field for the columns
11282 width the column width in characters, can be nil for automatic
11283 operator the operator if any
11284 format the output format for computed results, derived from operator
11285 printf a printf format for computed values"
11286 (let ((start 0) width prop title op f printf)
11287 (setq org-columns-current-fmt-compiled nil)
11288 (while (string-match
11289 (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
11291 (setq start (match-end 0)
11292 width (match-string 1 fmt)
11293 prop (match-string 2 fmt)
11294 title (or (match-string 3 fmt) prop)
11295 op (match-string 4 fmt)
11298 (if width (setq width (string-to-number width)))
11299 (when (and op (string-match ";" op))
11300 (setq printf (substring op (match-end 0))
11301 op (substring op 0 (match-beginning 0))))
11303 ((equal op "+") (setq f 'add_numbers))
11304 ((equal op "$") (setq f 'currency))
11305 ((equal op ":") (setq f 'add_times))
11306 ((equal op "X") (setq f 'checkbox))
11307 ((equal op "X/") (setq f 'checkbox-n-of-m))
11308 ((equal op "X%") (setq f 'checkbox-percent))
11310 (push (list prop title width op f printf) org-columns-current-fmt-compiled))
11311 (setq org-columns-current-fmt-compiled
11312 (nreverse org-columns-current-fmt-compiled))))
11315 ;;; Dynamic block for Column view
11317 (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
11318 "Get the column view of the current buffer or subtree.
11319 The first optional argument MAXLEVEL sets the level limit. A
11320 second optional argument SKIP-EMPTY-ROWS tells whether to skip
11321 empty rows, an empty row being one where all the column view
11322 specifiers except ITEM are empty. This function returns a list
11323 containing the title row and all other rows. Each row is a list
11326 (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
11327 (n (length title)) row tbl)
11328 (goto-char (point-min))
11329 (while (and (re-search-forward "^\\(\\*+\\) " nil t)
11330 (or (null maxlevel)
11332 (if org-odd-levels-only
11333 (/ (1+ (length (match-string 1))) 2)
11334 (length (match-string 1))))))
11335 (when (get-char-property (match-beginning 0) 'org-columns-key)
11337 (loop for i from 0 to (1- n) do
11338 (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
11339 (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
11342 (setq row (nreverse row))
11343 (unless (and skip-empty-rows
11344 (eq 1 (length (delete "" (delete-dups row)))))
11346 (append (list title 'hline) (nreverse tbl)))))
11348 (defun org-dblock-write:columnview (params)
11349 "Write the column view table.
11350 PARAMS is a property list of parameters:
11352 :width enforce same column widths with <N> specifiers.
11353 :id the :ID: property of the entry where the columns view
11354 should be built, as a string. When `local', call locally.
11355 When `global' call column view with the cursor at the beginning
11356 of the buffer (usually this means that the whole buffer switches
11358 :hlines When t, insert a hline before each item. When a number, insert
11359 a hline before each level <= that number.
11360 :vlines When t, make each column a colgroup to enforce vertical lines.
11361 :maxlevel When set to a number, don't capture headlines below this level.
11363 When t, skip rows where all specifiers other than ITEM are empty."
11364 (let ((pos (move-marker (make-marker) (point)))
11365 (hlines (plist-get params :hlines))
11366 (vlines (plist-get params :vlines))
11367 (maxlevel (plist-get params :maxlevel))
11368 (skip-empty-rows (plist-get params :skip-empty-rows))
11369 tbl id idpos nfields tmp)
11372 (when (setq id (plist-get params :id))
11373 (cond ((not id) nil)
11374 ((eq id 'global) (goto-char (point-min)))
11375 ((eq id 'local) nil)
11376 ((setq idpos (org-find-entry-with-id id))
11378 (t (error "Cannot find entry with :ID: %s" id))))
11380 (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
11381 (setq nfields (length (car tbl)))
11382 (org-columns-quit)))
11384 (move-marker pos nil)
11386 (when (plist-get params :hlines)
11389 (if (eq (car tbl) 'hline)
11390 (push (pop tbl) tmp)
11391 (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
11392 (if (and (not (eq (car tmp) 'hline))
11394 (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
11395 (push 'hline tmp)))
11396 (push (pop tbl) tmp)))
11397 (setq tbl (nreverse tmp)))
11399 (setq tbl (mapcar (lambda (x)
11400 (if (eq 'hline x) x (cons "" x)))
11402 (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
11404 (insert (org-listtable-to-string tbl))
11405 (when (plist-get params :width)
11406 (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
11407 org-columns-current-widths "|")))
11409 (org-table-align))))
11411 (defun org-listtable-to-string (tbl)
11412 "Convert a listtable TBL to a string that contains the Org-mode table.
11413 The table still need to be alligned. The resulting string has no leading
11414 and tailing newline characters."
11419 (concat "|" (mapconcat 'identity x "|") "|"))
11420 ((eq x 'hline) "|-|")
11421 (t (error "Garbage in listtable: %s" x))))
11424 (defun org-insert-columns-dblock ()
11425 "Create a dynamic block capturing a column view table."
11427 (let ((defaults '(:name "columnview" :hlines 1))
11428 (id (completing-read
11429 "Capture columns (local, global, entry with :ID: property) [local]: "
11430 (append '(("global") ("local"))
11431 (mapcar 'list (org-property-values "ID"))))))
11432 (if (equal id "") (setq id 'local))
11433 (if (equal id "global") (setq id 'global))
11434 (setq defaults (append defaults (list :id id)))
11435 (org-create-dblock defaults)
11436 (org-update-dblock)))
11440 (defvar org-last-changed-timestamp nil)
11441 (defvar org-time-was-given) ; dynamically scoped parameter
11442 (defvar org-end-time-was-given) ; dynamically scoped parameter
11443 (defvar org-ts-what) ; dynamically scoped parameter
11445 (defun org-time-stamp (arg)
11446 "Prompt for a date/time and insert a time stamp.
11447 If the user specifies a time like HH:MM, or if this command is called
11448 with a prefix argument, the time stamp will contain date and time.
11449 Otherwise, only the date will be included. All parts of a date not
11450 specified by the user will be filled in from the current date/time.
11451 So if you press just return without typing anything, the time stamp
11452 will represent the current date/time. If there is already a timestamp
11453 at the cursor, it will be modified."
11457 ;; Default time is either today, or, when entering a range,
11458 ;; the range start.
11459 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
11461 (re-search-backward
11462 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
11463 (- (point) 20) t)))
11464 (apply 'encode-time (org-parse-time-string (match-string 1)))
11466 (default-input (and ts (org-get-compact-tod ts)))
11467 org-time-was-given org-end-time-was-given time)
11469 ((and (org-at-timestamp-p)
11470 (eq last-command 'org-time-stamp)
11471 (eq this-command 'org-time-stamp))
11473 (setq time (let ((this-command this-command))
11474 (org-read-date arg 'totime nil nil default-time default-input)))
11475 (org-insert-time-stamp time (or org-time-was-given arg)))
11476 ((org-at-timestamp-p)
11477 (setq time (let ((this-command this-command))
11478 (org-read-date arg 'totime nil nil default-time default-input)))
11479 (when (org-at-timestamp-p) ; just to get the match data
11481 (setq org-last-changed-timestamp
11482 (org-insert-time-stamp
11483 time (or org-time-was-given arg)
11484 nil nil nil (list org-end-time-was-given))))
11485 (message "Timestamp updated"))
11487 (setq time (let ((this-command this-command))
11488 (org-read-date arg 'totime nil nil default-time default-input)))
11489 (org-insert-time-stamp time (or org-time-was-given arg)
11490 nil nil nil (list org-end-time-was-given))))))
11492 ;; FIXME: can we use this for something else, like computing time differences?
11493 (defun org-get-compact-tod (s)
11494 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
11495 (let* ((t1 (match-string 1 s))
11496 (h1 (string-to-number (match-string 2 s)))
11497 (m1 (string-to-number (match-string 3 s)))
11498 (t2 (and (match-end 4) (match-string 5 s)))
11499 (h2 (and t2 (string-to-number (match-string 6 s))))
11500 (m2 (and t2 (string-to-number (match-string 7 s))))
11504 (setq dh (- h2 h1) dm (- m2 m1))
11505 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
11506 (concat t1 "+" (number-to-string dh)
11507 (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
11509 (defun org-time-stamp-inactive (&optional arg)
11510 "Insert an inactive time stamp.
11511 An inactive time stamp is enclosed in square brackets instead of angle
11512 brackets. It is inactive in the sense that it does not trigger agenda entries,
11513 does not link to the calendar and cannot be changed with the S-cursor keys.
11514 So these are more for recording a certain time/date."
11516 (let (org-time-was-given org-end-time-was-given time)
11517 (setq time (org-read-date arg 'totime))
11518 (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
11519 nil nil (list org-end-time-was-given))))
11521 (defvar org-date-ovl (org-make-overlay 1 1))
11522 (org-overlay-put org-date-ovl 'face 'org-warning)
11523 (org-detach-overlay org-date-ovl)
11525 (defvar org-ans1) ; dynamically scoped parameter
11526 (defvar org-ans2) ; dynamically scoped parameter
11528 (defvar org-plain-time-of-day-regexp) ; defined below
11530 (defvar org-read-date-overlay nil)
11531 (defvar org-dcst nil) ; dynamically scoped
11533 (defun org-read-date (&optional with-time to-time from-string prompt
11534 default-time default-input)
11535 "Read a date, possibly a time, and make things smooth for the user.
11536 The prompt will suggest to enter an ISO date, but you can also enter anything
11537 which will at least partially be understood by `parse-time-string'.
11538 Unrecognized parts of the date will default to the current day, month, year,
11539 hour and minute. If this command is called to replace a timestamp at point,
11540 of to enter the second timestamp of a range, the default time is taken from the
11541 existing stamp. For example,
11542 3-2-5 --> 2003-02-05
11543 feb 15 --> currentyear-02-15
11544 sep 12 9 --> 2009-09-12
11545 12:45 --> today 12:45
11546 22 sept 0:34 --> currentyear-09-22 0:34
11547 12 --> currentyear-currentmonth-12
11548 Fri --> nearest Friday (today or later)
11551 Furthermore you can specify a relative date by giving, as the *first* thing
11552 in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
11553 change in days weeks, months, years.
11554 With a single plus or minus, the date is relative to today. With a double
11555 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
11556 +4d --> four days from today
11557 +4 --> same as above
11558 +2w --> two weeks from today
11559 ++5 --> five days from default date
11561 The function understands only English month and weekday abbreviations,
11562 but this can be configured with the variables `parse-time-months' and
11563 `parse-time-weekdays'.
11565 While prompting, a calendar is popped up - you can also select the
11566 date with the mouse (button 1). The calendar shows a period of three
11567 months. To scroll it to other months, use the keys `>' and `<'.
11568 If you don't like the calendar, turn it off with
11569 \(setq org-read-date-popup-calendar nil)
11571 With optional argument TO-TIME, the date will immediately be converted
11572 to an internal time.
11573 With an optional argument WITH-TIME, the prompt will suggest to also
11574 insert a time. Note that when WITH-TIME is not set, you can still
11575 enter a time, and this function will inform the calling routine about
11576 this change. The calling routine may then choose to change the format
11577 used to insert the time stamp into the buffer to include the time.
11578 With optional argument FROM-STRING, read from this string instead from
11579 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
11580 the time/date that is used for everything that is not specified by the
11582 (require 'parse-time)
11583 (let* ((org-time-stamp-rounding-minutes
11584 (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
11585 (org-dcst org-display-custom-times)
11586 (ct (org-current-time))
11587 (def (or default-time ct))
11588 (defdecode (decode-time def))
11590 (when (< (nth 2 defdecode) org-extend-today-until)
11591 (setcar (nthcdr 2 defdecode) -1)
11592 (setcar (nthcdr 1 defdecode) 59)
11593 (setq def (apply 'encode-time defdecode)
11594 defdecode (decode-time def)))))
11595 (calendar-move-hook nil)
11596 (view-diary-entries-initially nil)
11597 (view-calendar-holidays-initially nil)
11598 (timestr (format-time-string
11599 (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
11600 (prompt (concat (if prompt (concat prompt " ") "")
11601 (format "Date+time [%s]: " timestr)))
11602 ans (org-ans0 "") org-ans1 org-ans2 final)
11605 (from-string (setq ans from-string))
11606 (org-read-date-popup-calendar
11608 (save-window-excursion
11610 (calendar-forward-day (- (time-to-days def)
11611 (calendar-absolute-from-gregorian
11612 (calendar-current-date))))
11613 (org-eval-in-calendar nil t)
11614 (let* ((old-map (current-local-map))
11615 (map (copy-keymap calendar-mode-map))
11616 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
11617 (org-defkey map (kbd "RET") 'org-calendar-select)
11618 (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
11619 'org-calendar-select-mouse)
11620 (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2
])
11621 'org-calendar-select-mouse
)
11622 (org-defkey minibuffer-local-map
[(meta shift left
)]
11623 (lambda () (interactive)
11624 (org-eval-in-calendar '(calendar-backward-month 1))))
11625 (org-defkey minibuffer-local-map
[(meta shift right
)]
11626 (lambda () (interactive)
11627 (org-eval-in-calendar '(calendar-forward-month 1))))
11628 (org-defkey minibuffer-local-map
[(meta shift up
)]
11629 (lambda () (interactive)
11630 (org-eval-in-calendar '(calendar-backward-year 1))))
11631 (org-defkey minibuffer-local-map
[(meta shift down
)]
11632 (lambda () (interactive)
11633 (org-eval-in-calendar '(calendar-forward-year 1))))
11634 (org-defkey minibuffer-local-map
[(shift up
)]
11635 (lambda () (interactive)
11636 (org-eval-in-calendar '(calendar-backward-week 1))))
11637 (org-defkey minibuffer-local-map
[(shift down
)]
11638 (lambda () (interactive)
11639 (org-eval-in-calendar '(calendar-forward-week 1))))
11640 (org-defkey minibuffer-local-map
[(shift left
)]
11641 (lambda () (interactive)
11642 (org-eval-in-calendar '(calendar-backward-day 1))))
11643 (org-defkey minibuffer-local-map
[(shift right
)]
11644 (lambda () (interactive)
11645 (org-eval-in-calendar '(calendar-forward-day 1))))
11646 (org-defkey minibuffer-local-map
">"
11647 (lambda () (interactive)
11648 (org-eval-in-calendar '(scroll-calendar-left 1))))
11649 (org-defkey minibuffer-local-map
"<"
11650 (lambda () (interactive)
11651 (org-eval-in-calendar '(scroll-calendar-right 1))))
11654 (use-local-map map
)
11655 (add-hook 'post-command-hook
'org-read-date-display
)
11656 (setq org-ans0
(read-string prompt default-input nil nil
))
11657 ;; org-ans0: from prompt
11658 ;; org-ans1: from mouse click
11659 ;; org-ans2: from calendar motion
11660 (setq ans
(concat org-ans0
" " (or org-ans1 org-ans2
))))
11661 (remove-hook 'post-command-hook
'org-read-date-display
)
11662 (use-local-map old-map
)
11663 (when org-read-date-overlay
11664 (org-delete-overlay org-read-date-overlay
)
11665 (setq org-read-date-overlay nil
)))))))
11667 (t ; Naked prompt only
11669 (setq ans
(read-string prompt default-input nil timestr
))
11670 (when org-read-date-overlay
11671 (org-delete-overlay org-read-date-overlay
)
11672 (setq org-read-date-overlay nil
)))))
11674 (setq final
(org-read-date-analyze ans def defdecode
))
11677 (apply 'encode-time final
)
11678 (if (and (boundp 'org-time-was-given
) org-time-was-given
)
11679 (format "%04d-%02d-%02d %02d:%02d"
11680 (nth 5 final
) (nth 4 final
) (nth 3 final
)
11681 (nth 2 final
) (nth 1 final
))
11682 (format "%04d-%02d-%02d" (nth 5 final
) (nth 4 final
) (nth 3 final
))))))
11686 (defun org-read-date-display ()
11687 "Display the currrent date prompt interpretation in the minibuffer."
11688 (when org-read-date-display-live
11689 (when org-read-date-overlay
11690 (org-delete-overlay org-read-date-overlay
))
11693 (while (not (equal (buffer-substring
11694 (max (point-min) (- (point) 4)) (point))
11698 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
11699 " " (or org-ans1 org-ans2
)))
11700 (org-end-time-was-given nil
)
11701 (f (org-read-date-analyze ans def defdecode
))
11703 org-time-stamp-custom-formats
11704 org-time-stamp-formats
))
11705 (fmt (if (or with-time
11706 (and (boundp 'org-time-was-given
) org-time-was-given
))
11709 (txt (concat "=> " (format-time-string fmt
(apply 'encode-time f
)))))
11710 (when (and org-end-time-was-given
11711 (string-match org-plain-time-of-day-regexp txt
))
11712 (setq txt
(concat (substring txt
0 (match-end 0)) "-"
11713 org-end-time-was-given
11714 (substring txt
(match-end 0)))))
11715 (setq org-read-date-overlay
11716 (make-overlay (1- (point-at-eol)) (point-at-eol)))
11717 (org-overlay-display org-read-date-overlay txt
'secondary-selection
))))
11719 (defun org-read-date-analyze (ans def defdecode
)
11720 "Analyze the combined answer of the date prompt."
11721 ;; FIXME: cleanup and comment
11722 (let (delta deltan deltaw deltadef year month day
11723 hour minute second wday pm h2 m2 tl wday1
11724 iso-year iso-weekday iso-week iso-year iso-date
)
11726 (when (setq delta
(org-read-date-get-relative ans
(current-time) def
))
11727 (setq ans
(replace-match "" t t ans
)
11729 deltaw
(nth 1 delta
)
11730 deltadef
(nth 2 delta
)))
11732 ;; Check if there is an iso week date in there
11733 ;; If yes, sore the info and ostpone interpreting it until the rest
11734 ;; of the parsing is done
11735 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans
)
11736 (setq iso-year
(if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans
))))
11737 iso-weekday
(if (match-end 3) (string-to-number (match-string 3 ans
)))
11738 iso-week
(string-to-number (match-string 2 ans
)))
11739 (setq ans
(replace-match "" t t ans
)))
11741 ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
11742 (when (string-match
11743 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans
)
11744 (setq year
(if (match-end 2)
11745 (string-to-number (match-string 2 ans
))
11746 (string-to-number (format-time-string "%Y")))
11747 month
(string-to-number (match-string 3 ans
))
11748 day
(string-to-number (match-string 4 ans
)))
11749 (if (< year
100) (setq year
(+ 2000 year
)))
11750 (setq ans
(replace-match (format "%04d-%02d-%02d\\5" year month day
)
11752 ;; Help matching am/pm times, because `parse-time-string' does not do that.
11753 ;; If there is a time with am/pm, and *no* time without it, we convert
11754 ;; so that matching will be successful.
11755 (loop for i from
1 to
2 do
; twice, for end time as well
11756 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans
))
11757 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans
))
11758 (setq hour
(string-to-number (match-string 1 ans
))
11759 minute
(if (match-end 3)
11760 (string-to-number (match-string 3 ans
))
11763 (string-to-char (downcase (match-string 4 ans
)))))
11764 (if (and (= hour
12) (not pm
))
11766 (if (and pm
(< hour
12)) (setq hour
(+ 12 hour
))))
11767 (setq ans
(replace-match (format "%02d:%02d" hour minute
)
11770 ;; Check if a time range is given as a duration
11771 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans
)
11772 (setq hour
(string-to-number (match-string 1 ans
))
11773 h2
(+ hour
(string-to-number (match-string 3 ans
)))
11774 minute
(string-to-number (match-string 2 ans
))
11775 m2
(+ minute
(if (match-end 5) (string-to-number
11776 (match-string 5 ans
))0)))
11777 (if (>= m2
60) (setq h2
(1+ h2
) m2
(- m2
60)))
11778 (setq ans
(replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2
)
11781 ;; Check if there is a time range
11782 (when (boundp 'org-end-time-was-given
)
11783 (setq org-time-was-given nil
)
11784 (when (and (string-match org-plain-time-of-day-regexp ans
)
11786 (setq org-end-time-was-given
(match-string 8 ans
))
11787 (setq ans
(concat (substring ans
0 (match-beginning 7))
11788 (substring ans
(match-end 7))))))
11790 (setq tl
(parse-time-string ans
)
11791 day
(or (nth 3 tl
) (nth 3 defdecode
))
11792 month
(or (nth 4 tl
)
11793 (if (and org-read-date-prefer-future
11794 (nth 3 tl
) (< (nth 3 tl
) (nth 3 defdecode
)))
11795 (1+ (nth 4 defdecode
))
11796 (nth 4 defdecode
)))
11797 year
(or (nth 5 tl
)
11798 (if (and org-read-date-prefer-future
11799 (nth 4 tl
) (< (nth 4 tl
) (nth 4 defdecode
)))
11800 (1+ (nth 5 defdecode
))
11801 (nth 5 defdecode
)))
11802 hour
(or (nth 2 tl
) (nth 2 defdecode
))
11803 minute
(or (nth 1 tl
) (nth 1 defdecode
))
11804 second
(or (nth 0 tl
) 0)
11807 ;; Special date definitions below
11810 ;; There was an iso week
11811 (setq year
(or iso-year year
)
11812 day
(or iso-weekday wday
1)
11813 wday nil
; to make sure that the trigger below does not match
11814 iso-date
(calendar-gregorian-from-absolute
11815 (calendar-absolute-from-iso
11816 (list iso-week day year
))))
11817 ; FIXME: Should we also push ISO weeks into the future?
11818 ; (when (and org-read-date-prefer-future
11820 ; (< (calendar-absolute-from-gregorian iso-date)
11821 ; (time-to-days (current-time))))
11822 ; (setq year (1+ year)
11823 ; iso-date (calendar-gregorian-from-absolute
11824 ; (calendar-absolute-from-iso
11825 ; (list iso-week day year)))))
11826 (setq month
(car iso-date
)
11827 year
(nth 2 iso-date
)
11828 day
(nth 1 iso-date
)))
11831 (let ((now (decode-time (current-time))))
11832 (setq day
(nth 3 now
) month
(nth 4 now
) year
(nth 5 now
))))
11833 (cond ((member deltaw
'("d" "")) (setq day
(+ day deltan
)))
11834 ((equal deltaw
"w") (setq day
(+ day
(* 7 deltan
))))
11835 ((equal deltaw
"m") (setq month
(+ month deltan
)))
11836 ((equal deltaw
"y") (setq year
(+ year deltan
)))))
11837 ((and wday
(not (nth 3 tl
)))
11838 ;; Weekday was given, but no day, so pick that day in the week
11839 ;; on or after the derived date.
11840 (setq wday1
(nth 6 (decode-time (encode-time 0 0 0 day month year
))))
11841 (unless (equal wday wday1
)
11842 (setq day
(+ day
(%
(- wday wday1 -
7) 7))))))
11843 (if (and (boundp 'org-time-was-given
)
11845 (setq org-time-was-given t
))
11846 (if (< year
100) (setq year
(+ 2000 year
)))
11847 (if (< year
1970) (setq year
(nth 5 defdecode
))) ; not representable
11848 (list second minute hour day month year
)))
11850 (defvar parse-time-weekdays
)
11852 (defun org-read-date-get-relative (s today default
)
11853 "Check string S for special relative date string.
11854 TODAY and DEFAULT are internal times, for today and for a default.
11855 Return shift list (N what def-flag)
11856 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
11857 N is the number of WHATs to shift.
11858 DEF-FLAG is t when a double ++ or -- indicates shift relative to
11859 the DEFAULT date rather than TODAY."
11860 (when (string-match
11862 "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
11864 "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays
"\\|") "\\)\\)?"
11865 "\\([ \t]\\|$\\)") s
)
11866 (let* ((dir (if (match-end 1)
11867 (string-to-char (substring (match-string 1 s
) -
1))
11869 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
11870 (n (if (match-end 2) (string-to-number (match-string 2 s
)) 1))
11871 (what (if (match-end 3) (match-string 3 s
) "d"))
11872 (wday1 (cdr (assoc (downcase what
) parse-time-weekdays
)))
11873 (date (if rel default today
))
11874 (wday (nth 6 (decode-time date
)))
11878 (setq delta
(mod (+ 7 (- wday1 wday
)) 7))
11879 (if (= dir ?-
) (setq delta
(- delta
7)))
11880 (if (> n
1) (setq delta
(+ delta
(* (1- n
) (if (= dir ?-
) -
7 7)))))
11881 (list delta
"d" rel
))
11882 (list (* n
(if (= dir ?-
) -
1 1)) what rel
)))))
11884 (defun org-eval-in-calendar (form &optional keepdate
)
11885 "Eval FORM in the calendar window and return to current window.
11886 Also, store the cursor date in variable org-ans2."
11887 (let ((sw (selected-window)))
11888 (select-window (get-buffer-window "*Calendar*"))
11890 (when (and (not keepdate
) (calendar-cursor-to-date))
11891 (let* ((date (calendar-cursor-to-date))
11892 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
11893 (setq org-ans2
(format-time-string "%Y-%m-%d" time
))))
11894 (org-move-overlay org-date-ovl
(1- (point)) (1+ (point)) (current-buffer))
11895 (select-window sw
)))
11897 ; ;; Update the prompt to show new default date
11899 ; (goto-char (point-min))
11900 ; (when (and org-ans2
11901 ; (re-search-forward "\\[[-0-9]+\\]" nil t)
11902 ; (get-text-property (match-end 0) 'field))
11903 ; (let ((inhibit-read-only t))
11904 ; (replace-match (concat "[" org-ans2 "]") t t)
11905 ; (add-text-properties (point-min) (1+ (match-end 0))
11906 ; (text-properties-at (1+ (point-min)))))))))
11908 (defun org-calendar-select ()
11909 "Return to `org-read-date' with the date currently selected.
11910 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
11912 (when (calendar-cursor-to-date)
11913 (let* ((date (calendar-cursor-to-date))
11914 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
11915 (setq org-ans1
(format-time-string "%Y-%m-%d" time
)))
11916 (if (active-minibuffer-window) (exit-minibuffer))))
11918 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra
)
11919 "Insert a date stamp for the date given by the internal TIME.
11920 WITH-HM means, use the stamp format that includes the time of the day.
11921 INACTIVE means use square brackets instead of angular ones, so that the
11922 stamp will not contribute to the agenda.
11923 PRE and POST are optional strings to be inserted before and after the
11925 The command returns the inserted time stamp."
11926 (let ((fmt (funcall (if with-hm
'cdr
'car
) org-time-stamp-formats
))
11928 (if inactive
(setq fmt
(concat "[" (substring fmt
1 -
1) "]")))
11929 (insert-before-markers (or pre
""))
11930 (insert-before-markers (setq stamp
(format-time-string fmt time
)))
11931 (when (listp extra
)
11932 (setq extra
(car extra
))
11933 (if (and (stringp extra
)
11934 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra
))
11935 (setq extra
(format "-%02d:%02d"
11936 (string-to-number (match-string 1 extra
))
11937 (string-to-number (match-string 2 extra
))))
11941 (insert-before-markers extra
)
11943 (insert-before-markers (or post
""))
11946 (defun org-toggle-time-stamp-overlays ()
11947 "Toggle the use of custom time stamp formats."
11949 (setq org-display-custom-times
(not org-display-custom-times
))
11950 (unless org-display-custom-times
11951 (let ((p (point-min)) (bmp (buffer-modified-p)))
11952 (while (setq p
(next-single-property-change p
'display
))
11953 (if (and (get-text-property p
'display
)
11954 (eq (get-text-property p
'face
) 'org-date
))
11955 (remove-text-properties
11956 p
(setq p
(next-single-property-change p
'display
))
11958 (set-buffer-modified-p bmp
)))
11959 (if (featurep 'xemacs
)
11960 (remove-text-properties (point-min) (point-max) '(end-glyph t
)))
11961 (org-restart-font-lock)
11962 (setq org-table-may-need-update t
)
11963 (if org-display-custom-times
11964 (message "Time stamps are overlayed with custom format")
11965 (message "Time stamp overlays removed")))
11967 (defun org-display-custom-time (beg end
)
11968 "Overlay modified time stamp format over timestamp between BED and END."
11969 (let* ((ts (buffer-substring beg end
))
11970 t1 w1 with-hm tf time str w2
(off 0))
11972 (setq t1
(org-parse-time-string ts t
))
11973 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts
)
11974 (setq off
(- (match-end 0) (match-beginning 0)))))
11975 (setq end
(- end off
))
11976 (setq w1
(- end beg
)
11977 with-hm
(and (nth 1 t1
) (nth 2 t1
))
11978 tf
(funcall (if with-hm
'cdr
'car
) org-time-stamp-custom-formats
)
11979 time
(org-fix-decoded-time t1
)
11981 (format-time-string
11982 (substring tf
1 -
1) (apply 'encode-time time
))
11983 nil
'mouse-face
'highlight
)
11985 (if (not (= w2 w1
))
11986 (add-text-properties (1+ beg
) (+ 2 beg
)
11987 (list 'org-dwidth t
'org-dwidth-n
(- w1 w2
))))
11988 (if (featurep 'xemacs
)
11990 (put-text-property beg end
'invisible t
)
11991 (put-text-property beg end
'end-glyph
(make-glyph str
)))
11992 (put-text-property beg end
'display str
))))
11994 (defun org-translate-time (string)
11995 "Translate all timestamps in STRING to custom format.
11996 But do this only if the variable `org-display-custom-times' is set."
11997 (when org-display-custom-times
12000 (re org-ts-regexp-both
)
12001 t1 with-hm inactive tf time str beg end
)
12002 (while (setq start
(string-match re string start
))
12003 (setq beg
(match-beginning 0)
12005 t1
(save-match-data
12006 (org-parse-time-string (substring string beg end
) t
))
12007 with-hm
(and (nth 1 t1
) (nth 2 t1
))
12008 inactive
(equal (substring string beg
(1+ beg
)) "[")
12009 tf
(funcall (if with-hm
'cdr
'car
)
12010 org-time-stamp-custom-formats
)
12011 time
(org-fix-decoded-time t1
)
12012 str
(format-time-string
12014 (if inactive
"[" "<") (substring tf
1 -
1)
12015 (if inactive
"]" ">"))
12016 (apply 'encode-time time
))
12017 string
(replace-match str t t string
)
12018 start
(+ start
(length str
)))))))
12021 (defun org-fix-decoded-time (time)
12022 "Set 0 instead of nil for the first 6 elements of time.
12023 Don't touch the rest."
12025 (mapcar (lambda (x) (if (< (setq n
(1+ n
)) 7) (or x
0) x
)) time
)))
12027 (defun org-days-to-time (timestamp-string)
12028 "Difference between TIMESTAMP-STRING and now in days."
12029 (- (time-to-days (org-time-string-to-time timestamp-string
))
12030 (time-to-days (current-time))))
12032 (defun org-deadline-close (timestamp-string &optional ndays
)
12033 "Is the time in TIMESTAMP-STRING close to the current date?"
12034 (setq ndays
(or ndays
(org-get-wdays timestamp-string
)))
12035 (and (< (org-days-to-time timestamp-string
) ndays
)
12036 (not (org-entry-is-done-p))))
12038 (defun org-get-wdays (ts)
12039 "Get the deadline lead time appropriate for timestring TS."
12041 ((<= org-deadline-warning-days
0)
12042 ;; 0 or negative, enforce this value no matter what
12043 (- org-deadline-warning-days
))
12044 ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts
)
12045 ;; lead time is specified.
12046 (floor (* (string-to-number (match-string 1 ts
))
12047 (cdr (assoc (match-string 2 ts
)
12048 '(("d" .
1) ("w" .
7)
12049 ("m" .
30.4) ("y" .
365.25)))))))
12050 ;; go for the default.
12051 (t org-deadline-warning-days
)))
12053 (defun org-calendar-select-mouse (ev)
12054 "Return to `org-read-date' with the date currently selected.
12055 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
12057 (mouse-set-point ev
)
12058 (when (calendar-cursor-to-date)
12059 (let* ((date (calendar-cursor-to-date))
12060 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
12061 (setq org-ans1
(format-time-string "%Y-%m-%d" time
)))
12062 (if (active-minibuffer-window) (exit-minibuffer))))
12064 (defun org-check-deadlines (ndays)
12065 "Check if there are any deadlines due or past due.
12066 A deadline is considered due if it happens within `org-deadline-warning-days'
12067 days from today's date. If the deadline appears in an entry marked DONE,
12068 it is not shown. The prefix arg NDAYS can be used to test that many
12069 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
12071 (let* ((org-warn-days
12073 ((equal ndays
'(4)) 100000)
12074 (ndays (prefix-numeric-value ndays
))
12075 (t (abs org-deadline-warning-days
))))
12076 (case-fold-search nil
)
12077 (regexp (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>"))
12079 (lambda () (org-deadline-close (match-string 1) org-warn-days
))))
12081 (message "%d deadlines past-due or due within %d days"
12082 (org-occur regexp nil callback
)
12085 (defun org-check-before-date (date)
12086 "Check if there are deadlines or scheduled entries before DATE."
12087 (interactive (list (org-read-date)))
12088 (let ((case-fold-search nil
)
12089 (regexp (concat "\\<\\(" org-deadline-string
12090 "\\|" org-scheduled-string
12091 "\\) *<\\([^>]+\\)>"))
12093 (lambda () (time-less-p
12094 (org-time-string-to-time (match-string 2))
12095 (org-time-string-to-time date
)))))
12096 (message "%d entries before %s"
12097 (org-occur regexp nil callback
) date
)))
12099 (defun org-evaluate-time-range (&optional to-buffer
)
12100 "Evaluate a time range by computing the difference between start and end.
12101 Normally the result is just printed in the echo area, but with prefix arg
12102 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
12103 If the time range is actually in a table, the result is inserted into the
12105 For time difference computation, a year is assumed to be exactly 365
12106 days in order to avoid rounding problems."
12109 (org-clock-update-time-maybe)
12111 (unless (org-at-date-range-p t
)
12112 (goto-char (point-at-bol))
12113 (re-search-forward org-tr-regexp-both
(point-at-eol) t
))
12114 (if (not (org-at-date-range-p t
))
12115 (error "Not at a time-stamp range, and none found in current line")))
12116 (let* ((ts1 (match-string 1))
12117 (ts2 (match-string 2))
12118 (havetime (or (> (length ts1
) 15) (> (length ts2
) 15)))
12119 (match-end (match-end 0))
12120 (time1 (org-time-string-to-time ts1
))
12121 (time2 (org-time-string-to-time ts2
))
12122 (t1 (time-to-seconds time1
))
12123 (t2 (time-to-seconds time2
))
12124 (diff (abs (- t2 t1
)))
12125 (negative (< (- t2 t1
) 0))
12126 ;; (ys (floor (* 365 24 60 60)))
12129 (fy "%dy %dd %02d:%02d")
12131 (fd "%dd %02d:%02d")
12136 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
12138 d
(floor (/ diff ds
)) diff
(mod diff ds
)
12139 h
(floor (/ diff hs
)) diff
(mod diff hs
)
12140 m
(floor (/ diff
60)))
12141 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
12143 d
(floor (+ (/ diff ds
) 0.5))
12145 (if (not to-buffer
)
12146 (message "%s" (org-make-tdiff-string y d h m
))
12147 (if (org-at-table-p)
12149 (goto-char match-end
)
12151 (and (looking-at " *|") (goto-char (match-end 0))))
12152 (goto-char match-end
))
12154 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
12155 (replace-match ""))
12156 (if negative
(insert " -"))
12157 (if (> y
0) (insert " " (format (if havetime fy fy1
) y d h m
))
12158 (if (> d
0) (insert " " (format (if havetime fd fd1
) d h m
))
12159 (insert " " (format fh h m
))))
12160 (if align
(org-table-align))
12161 (message "Time difference inserted")))))
12163 (defun org-make-tdiff-string (y d h m
)
12166 (if (> y
0) (setq fmt
(concat fmt
"%d year" (if (> y
1) "s" "") " ")
12168 (if (> d
0) (setq fmt
(concat fmt
"%d day" (if (> d
1) "s" "") " ")
12170 (if (> h
0) (setq fmt
(concat fmt
"%d hour" (if (> h
1) "s" "") " ")
12172 (if (> m
0) (setq fmt
(concat fmt
"%d minute" (if (> m
1) "s" "") " ")
12174 (apply 'format fmt
(nreverse l
))))
12176 (defun org-time-string-to-time (s)
12177 (apply 'encode-time
(org-parse-time-string s
)))
12179 (defun org-time-string-to-absolute (s &optional daynr prefer show-all
)
12180 "Convert a time stamp to an absolute day number.
12181 If there is a specifyer for a cyclic time stamp, get the closest date to
12183 PREFER and SHOW_ALL are passed through to `org-closest-date'."
12185 ((and daynr
(string-match "\\`%%\\((.*)\\)" s
))
12186 (if (org-diary-sexp-entry (match-string 1 s
) "" date
)
12189 ((and daynr
(string-match "\\+[0-9]+[dwmy]" s
))
12190 (org-closest-date s
(if (and (boundp 'daynr
) (integerp daynr
)) daynr
12191 (time-to-days (current-time))) (match-string 0 s
)
12193 (t (time-to-days (apply 'encode-time
(org-parse-time-string s
))))))
12195 (defun org-days-to-iso-week (days)
12196 "Return the iso week number."
12198 (car (calendar-iso-from-absolute days
)))
12200 (defun org-small-year-to-year (year)
12201 "Convert 2-digit years into 4-digit years.
12202 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
12203 The year 2000 cannot be abbreviated. Any year lager than 99
12204 is retrned unchanged."
12206 (setq year
(+ 2000 year
))
12208 (setq year
(+ 1900 year
))))
12211 (defun org-time-from-absolute (d)
12212 "Return the time corresponding to date D.
12213 D may be an absolute day number, or a calendar-type list (month day year)."
12214 (if (numberp d
) (setq d
(calendar-gregorian-from-absolute d
)))
12215 (encode-time 0 0 0 (nth 1 d
) (car d
) (nth 2 d
)))
12217 (defun org-calendar-holiday ()
12218 "List of holidays, for Diary display in Org-mode."
12219 (require 'holidays
)
12221 (if (fboundp 'calendar-check-holidays
)
12222 'calendar-check-holidays
'check-calendar-holidays
) date
)))
12223 (if hl
(mapconcat 'identity hl
"; "))))
12225 (defun org-diary-sexp-entry (sexp entry date
)
12226 "Process a SEXP diary ENTRY for DATE."
12227 (require 'diary-lib
)
12228 (let ((result (if calendar-debug-sexp
12229 (let ((stack-trace-on-error t
))
12230 (eval (car (read-from-string sexp
))))
12231 (condition-case nil
12232 (eval (car (read-from-string sexp
)))
12235 (message "Bad sexp at line %d in %s: %s"
12237 (buffer-file-name) sexp
)
12239 (cond ((stringp result
) result
)
12240 ((and (consp result
)
12241 (stringp (cdr result
))) (cdr result
))
12245 (defun org-diary-to-ical-string (frombuf)
12246 "Get iCalendar entries from diary entries in buffer FROMBUF.
12247 This uses the icalendar.el library."
12248 (let* ((tmpdir (if (featurep 'xemacs
)
12250 temporary-file-directory
))
12251 (tmpfile (make-temp-name
12252 (expand-file-name "orgics" tmpdir
)))
12255 (set-buffer frombuf
)
12256 (icalendar-export-region (point-min) (point-max) tmpfile
)
12257 (setq buf
(find-buffer-visiting tmpfile
))
12259 (goto-char (point-min))
12260 (if (re-search-forward "^BEGIN:VEVENT" nil t
)
12261 (setq b
(match-beginning 0)))
12262 (goto-char (point-max))
12263 (if (re-search-backward "^END:VEVENT" nil t
)
12264 (setq e
(match-end 0)))
12265 (setq rtn
(if (and b e
) (concat (buffer-substring b e
) "\n") "")))
12267 (kill-buffer frombuf
)
12268 (delete-file tmpfile
)
12271 (defun org-closest-date (start current change prefer show-all
)
12272 "Find the date closest to CURRENT that is consistent with START and CHANGE.
12273 When PREFER is `past' return a date that is either CURRENT or past.
12274 When PREFER is `future', return a date that is either CURRENT or future.
12275 When SHOW-ALL is nil, only return the current occurence of a time stamp."
12276 ;; Make the proper lists from the dates
12278 (let ((a1 '(("d" . day
) ("w" . week
) ("m" . month
) ("y" . year
)))
12279 dn dw sday cday n1 n2
12280 d m y y1 y2 date1 date2 nmonths nm ny m2
)
12282 (setq start
(org-date-to-gregorian start
)
12283 current
(org-date-to-gregorian
12286 (time-to-days (current-time))))
12287 sday
(calendar-absolute-from-gregorian start
)
12288 cday
(calendar-absolute-from-gregorian current
))
12290 (if (<= cday sday
) (throw 'exit sday
))
12292 (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change
)
12293 (setq dn
(string-to-number (match-string 1 change
))
12294 dw
(cdr (assoc (match-string 2 change
) a1
)))
12295 (error "Invalid change specifyer: %s" change
))
12296 (if (eq dw
'week
) (setq dw
'day dn
(* 7 dn
)))
12299 (setq n1
(+ sday
(* dn
(floor (/ (- cday sday
) dn
))))
12302 (setq d
(nth 1 start
) m
(car start
) y1
(nth 2 start
) y2
(nth 2 current
))
12303 (setq y1
(+ (* (floor (/ (- y2 y1
) dn
)) dn
) y1
))
12304 (setq date1
(list m d y1
)
12305 n1
(calendar-absolute-from-gregorian date1
)
12306 date2
(list m d
(+ y1
(* (if (< n1 cday
) 1 -
1) dn
)))
12307 n2
(calendar-absolute-from-gregorian date2
)))
12309 ;; approx number of month between the tow dates
12310 (setq nmonths
(floor (/ (- cday sday
) 30.436875)))
12311 ;; How often does dn fit in there?
12312 (setq d
(nth 1 start
) m
(car start
) y
(nth 2 start
)
12313 nm
(* dn
(max 0 (1- (floor (/ nmonths dn
)))))
12315 ny
(floor (/ m
12))
12318 (while (> m
12) (setq m
(- m
12) y
(1+ y
)))
12319 (setq n1
(calendar-absolute-from-gregorian (list m d y
)))
12320 (setq m2
(+ m dn
) y2 y
)
12321 (if (> m2
12) (setq y2
(1+ y2
) m2
(- m2
12)))
12322 (setq n2
(calendar-absolute-from-gregorian (list m2 d y2
)))
12324 (setq n1 n2 m m2 y y2
)
12325 (setq m2
(+ m dn
) y2 y
)
12326 (if (> m2
12) (setq y2
(1+ y2
) m2
(- m2
12)))
12327 (setq n2
(calendar-absolute-from-gregorian (list m2 d y2
))))))
12331 ((eq prefer
'past
) n1
)
12332 ((eq prefer
'future
) (if (= cday n1
) n1 n2
))
12333 (t (if (> (abs (- cday n1
)) (abs (- cday n2
))) n2 n1
)))
12335 ((eq prefer
'past
) n1
)
12336 ((eq prefer
'future
) (if (= cday n1
) n1 n2
))
12337 (t (if (= cday n1
) n1 n2
)))))))
12339 (defun org-date-to-gregorian (date)
12340 "Turn any specification of DATE into a gregorian date for the calendar."
12341 (cond ((integerp date
) (calendar-gregorian-from-absolute date
))
12342 ((and (listp date
) (= (length date
) 3)) date
)
12344 (setq date
(org-parse-time-string date
))
12345 (list (nth 4 date
) (nth 3 date
) (nth 5 date
)))
12347 (list (nth 4 date
) (nth 3 date
) (nth 5 date
)))))
12349 (defun org-parse-time-string (s &optional nodefault
)
12350 "Parse the standard Org-mode time string.
12351 This should be a lot faster than the normal `parse-time-string'.
12352 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
12353 hour and minute fields will be nil if not given."
12354 (if (string-match org-ts-regexp0 s
)
12356 (if (or (match-beginning 8) (not nodefault
))
12357 (string-to-number (or (match-string 8 s
) "0")))
12358 (if (or (match-beginning 7) (not nodefault
))
12359 (string-to-number (or (match-string 7 s
) "0")))
12360 (string-to-number (match-string 4 s
))
12361 (string-to-number (match-string 3 s
))
12362 (string-to-number (match-string 2 s
))
12366 (defun org-timestamp-up (&optional arg
)
12367 "Increase the date item at the cursor by one.
12368 If the cursor is on the year, change the year. If it is on the month or
12369 the day, change that.
12370 With prefix ARG, change by that many units."
12372 (org-timestamp-change (prefix-numeric-value arg
)))
12374 (defun org-timestamp-down (&optional arg
)
12375 "Decrease the date item at the cursor by one.
12376 If the cursor is on the year, change the year. If it is on the month or
12377 the day, change that.
12378 With prefix ARG, change by that many units."
12380 (org-timestamp-change (- (prefix-numeric-value arg
))))
12382 (defun org-timestamp-up-day (&optional arg
)
12383 "Increase the date in the time stamp by one day.
12384 With prefix ARG, change that many days."
12386 (if (and (not (org-at-timestamp-p t
))
12387 (org-on-heading-p))
12389 (org-timestamp-change (prefix-numeric-value arg
) 'day
)))
12391 (defun org-timestamp-down-day (&optional arg
)
12392 "Decrease the date in the time stamp by one day.
12393 With prefix ARG, change that many days."
12395 (if (and (not (org-at-timestamp-p t
))
12396 (org-on-heading-p))
12398 (org-timestamp-change (- (prefix-numeric-value arg
)) 'day
)))
12400 (defun org-at-timestamp-p (&optional inactive-ok
)
12401 "Determine if the cursor is in or at a timestamp."
12403 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2
))
12405 (ans (or (looking-at tsr
)
12407 (skip-chars-backward "^[<\n\r\t")
12408 (if (> (point) (point-min)) (backward-char 1))
12409 (and (looking-at tsr
)
12410 (> (- (match-end 0) pos
) -
1))))))
12412 (boundp 'org-ts-what
)
12415 ((= pos
(match-beginning 0)) 'bracket
)
12416 ((= pos
(1- (match-end 0))) 'bracket
)
12417 ((org-pos-in-match-range pos
2) 'year
)
12418 ((org-pos-in-match-range pos
3) 'month
)
12419 ((org-pos-in-match-range pos
7) 'hour
)
12420 ((org-pos-in-match-range pos
8) 'minute
)
12421 ((or (org-pos-in-match-range pos
4)
12422 (org-pos-in-match-range pos
5)) 'day
)
12423 ((and (> pos
(or (match-end 8) (match-end 5)))
12424 (< pos
(match-end 0)))
12425 (- pos
(or (match-end 8) (match-end 5))))
12429 (defun org-toggle-timestamp-type ()
12430 "Toggle the type (<active> or [inactive]) of a time stamp."
12432 (when (org-at-timestamp-p t
)
12434 (goto-char (match-beginning 0))
12435 (insert (if (equal (char-after) ?
<) "[" "<")) (delete-char 1)
12436 (goto-char (1- (match-end 0)))
12437 (insert (if (equal (char-after) ?
>) "]" ">")) (delete-char 1))
12438 (message "Timestamp is now %sactive"
12439 (if (equal (char-before) ?
>) "in" ""))))
12441 (defun org-timestamp-change (n &optional what
)
12442 "Change the date in the time stamp at point.
12443 The date will be changed by N times WHAT. WHAT can be `day', `month',
12444 `year', `minute', `second'. If WHAT is not given, the cursor position
12445 in the timestamp determines what will be changed."
12446 (let ((pos (point))
12448 (dm (max (nth 1 org-time-stamp-rounding-minutes
) 1))
12452 (if (not (org-at-timestamp-p t
))
12453 (error "Not at a timestamp"))
12454 (if (and (not what
) (eq org-ts-what
'bracket
))
12455 (org-toggle-timestamp-type)
12456 (if (and (not what
) (not (eq org-ts-what
'day
))
12457 org-display-custom-times
12458 (get-text-property (point) 'display
)
12459 (not (get-text-property (1- (point)) 'display
)))
12460 (setq org-ts-what
'day
))
12461 (setq org-ts-what
(or what org-ts-what
)
12462 inactive
(= (char-after (match-beginning 0)) ?\
[)
12463 ts
(match-string 0))
12466 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
12468 (setq extra
(match-string 1 ts
)))
12469 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts
)
12471 (setq time0
(org-parse-time-string ts
))
12472 (when (and (eq org-ts-what
'minute
)
12473 (eq current-prefix-arg nil
))
12474 (setq n
(* dm
(cond ((> n
0) 1) ((< n
0) -
1) (t 0))))
12475 (when (not (= 0 (setq rem
(%
(nth 1 time0
) dm
))))
12476 (setcar (cdr time0
) (+ (nth 1 time0
)
12477 (if (> n
0) (- rem
) (- dm rem
))))))
12479 (encode-time (or (car time0
) 0)
12480 (+ (if (eq org-ts-what
'minute
) n
0) (nth 1 time0
))
12481 (+ (if (eq org-ts-what
'hour
) n
0) (nth 2 time0
))
12482 (+ (if (eq org-ts-what
'day
) n
0) (nth 3 time0
))
12483 (+ (if (eq org-ts-what
'month
) n
0) (nth 4 time0
))
12484 (+ (if (eq org-ts-what
'year
) n
0) (nth 5 time0
))
12486 (when (integerp org-ts-what
)
12487 (setq extra
(org-modify-ts-extra extra org-ts-what n dm
)))
12488 (if (eq what
'calendar
)
12489 (let ((cal-date (org-get-date-from-calendar)))
12490 (setcar (nthcdr 4 time0
) (nth 0 cal-date
)) ; month
12491 (setcar (nthcdr 3 time0
) (nth 1 cal-date
)) ; day
12492 (setcar (nthcdr 5 time0
) (nth 2 cal-date
)) ; year
12493 (setcar time0
(or (car time0
) 0))
12494 (setcar (nthcdr 1 time0
) (or (nth 1 time0
) 0))
12495 (setcar (nthcdr 2 time0
) (or (nth 2 time0
) 0))
12496 (setq time
(apply 'encode-time time0
))))
12497 (setq org-last-changed-timestamp
12498 (org-insert-time-stamp time with-hm inactive nil nil extra
))
12499 (org-clock-update-time-maybe)
12501 ;; Try to recenter the calendar window, if any
12502 (if (and org-calendar-follow-timestamp-change
12503 (get-buffer-window "*Calendar*" t
)
12504 (memq org-ts-what
'(day month year
)))
12505 (org-recenter-calendar (time-to-days time
))))))
12507 (defun org-modify-ts-extra (s pos n dm
)
12508 "Change the different parts of the lead-time and repeat fields in timestamp."
12509 (let ((idx '(("d" .
0) ("w" .
1) ("m" .
2) ("y" .
3) ("d" . -
1) ("y" .
4)))
12511 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s
)
12513 ((or (org-pos-in-match-range pos
2)
12514 (org-pos-in-match-range pos
3))
12515 (setq m
(string-to-number (match-string 3 s
))
12516 h
(string-to-number (match-string 2 s
)))
12517 (if (org-pos-in-match-range pos
2)
12519 (setq n
(* dm
(org-no-warnings (signum n
))))
12520 (when (not (= 0 (setq rem
(% m dm
))))
12521 (setq m
(+ m
(if (> n
0) (- rem
) (- dm rem
)))))
12523 (if (< m
0) (setq m
(+ m
60) h
(1- h
)))
12524 (if (> m
59) (setq m
(- m
60) h
(1+ h
)))
12525 (setq h
(min 24 (max 0 h
)))
12526 (setq ng
1 new
(format "-%02d:%02d" h m
)))
12527 ((org-pos-in-match-range pos
6)
12528 (setq ng
6 new
(car (rassoc (+ n
(cdr (assoc (match-string 6 s
) idx
))) idx
))))
12529 ((org-pos-in-match-range pos
5)
12530 (setq ng
5 new
(format "%d" (max 1 (+ n
(string-to-number (match-string 5 s
)))))))
12532 ((org-pos-in-match-range pos
9)
12533 (setq ng
9 new
(car (rassoc (+ n
(cdr (assoc (match-string 9 s
) idx
))) idx
))))
12534 ((org-pos-in-match-range pos
8)
12535 (setq ng
8 new
(format "%d" (max 0 (+ n
(string-to-number (match-string 8 s
))))))))
12539 (substring s
0 (match-beginning ng
))
12541 (substring s
(match-end ng
))))))
12544 (defun org-recenter-calendar (date)
12545 "If the calendar is visible, recenter it to DATE."
12546 (let* ((win (selected-window))
12547 (cwin (get-buffer-window "*Calendar*" t
))
12548 (calendar-move-hook nil
))
12550 (select-window cwin
)
12551 (calendar-goto-date (if (listp date
) date
12552 (calendar-gregorian-from-absolute date
)))
12553 (select-window win
))))
12555 (defun org-goto-calendar (&optional arg
)
12556 "Go to the Emacs calendar at the current date.
12557 If there is a time stamp in the current line, go to that date.
12558 A prefix ARG can be used to force the current date."
12560 (let ((tsr org-ts-regexp
) diff
12561 (calendar-move-hook nil
)
12562 (view-calendar-holidays-initially nil
)
12563 (view-diary-entries-initially nil
))
12564 (if (or (org-at-timestamp-p)
12566 (beginning-of-line 1)
12567 (looking-at (concat ".*" tsr
))))
12568 (let ((d1 (time-to-days (current-time)))
12570 (org-time-string-to-time (match-string 1)))))
12571 (setq diff
(- d2 d1
))))
12573 (calendar-goto-today)
12574 (if (and diff
(not arg
)) (calendar-forward-day diff
))))
12576 (defun org-get-date-from-calendar ()
12577 "Return a list (month day year) of date at point in calendar."
12578 (with-current-buffer "*Calendar*"
12580 (calendar-cursor-to-date))))
12582 (defun org-date-from-calendar ()
12583 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
12584 If there is already a time stamp at the cursor position, update it."
12586 (if (org-at-timestamp-p t
)
12587 (org-timestamp-change 0 'calendar
)
12588 (let ((cal-date (org-get-date-from-calendar)))
12589 (org-insert-time-stamp
12590 (encode-time 0 0 0 (nth 1 cal-date
) (car cal-date
) (nth 2 cal-date
))))))
12592 (defun org-minutes-to-hours (m)
12593 "Compute H:MM from a number of minutes."
12594 (let ((h (/ m
60)))
12595 (setq m
(- m
(* 60 h
)))
12596 (format "%d:%02d" h m
)))
12601 (defun org-agenda-files (&optional unrestricted
)
12602 "Get the list of agenda files.
12603 Optional UNRESTRICTED means return the full list even if a restriction
12604 is currently in place."
12607 ((and (not unrestricted
) (get 'org-agenda-files
'org-restrict
)))
12608 ((stringp org-agenda-files
) (org-read-agenda-file-list))
12609 ((listp org-agenda-files
) org-agenda-files
)
12610 (t (error "Invalid value of `org-agenda-files'")))))
12611 (setq files
(apply 'append
12612 (mapcar (lambda (f)
12613 (if (file-directory-p f
)
12614 (directory-files f t
12615 org-agenda-file-regexp
)
12618 (if org-agenda-skip-unavailable-files
12622 (and (file-readable-p file
) file
)))
12624 files
))) ; `org-check-agenda-file' will remove them from the list
12626 (defun org-edit-agenda-file-list ()
12627 "Edit the list of agenda files.
12628 Depending on setup, this either uses customize to edit the variable
12629 `org-agenda-files', or it visits the file that is holding the list. In the
12630 latter case, the buffer is set up in a way that saving it automatically kills
12631 the buffer and restores the previous window configuration."
12633 (if (stringp org-agenda-files
)
12634 (let ((cw (current-window-configuration)))
12635 (find-file org-agenda-files
)
12636 (org-set-local 'org-window-configuration cw
)
12637 (org-add-hook 'after-save-hook
12639 (set-window-configuration
12640 (prog1 org-window-configuration
12641 (kill-buffer (current-buffer))))
12642 (org-install-agenda-files-menu)
12643 (message "New agenda file list installed"))
12645 (message "%s" (substitute-command-keys
12646 "Edit list and finish with \\[save-buffer]")))
12647 (customize-variable 'org-agenda-files
)))
12649 (defun org-store-new-agenda-file-list (list)
12650 "Set new value for the agenda file list and save it correcly."
12651 (if (stringp org-agenda-files
)
12652 (let ((f org-agenda-files
) b
)
12653 (while (setq b
(find-buffer-visiting f
)) (kill-buffer b
))
12655 (insert (mapconcat 'identity list
"\n") "\n")))
12656 (let ((org-mode-hook nil
) (default-major-mode 'fundamental-mode
))
12657 (setq org-agenda-files list
)
12658 (customize-save-variable 'org-agenda-files org-agenda-files
))))
12660 (defun org-read-agenda-file-list ()
12661 "Read the list of agenda files from a file."
12662 (when (stringp org-agenda-files
)
12664 (insert-file-contents org-agenda-files
)
12665 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
12669 (defun org-cycle-agenda-files ()
12670 "Cycle through the files in `org-agenda-files'.
12671 If the current buffer visits an agenda file, find the next one in the list.
12672 If the current buffer does not, find the first agenda file."
12674 (let* ((fs (org-agenda-files t
))
12675 (files (append fs
(list (car fs
))))
12676 (tcf (if buffer-file-name
(file-truename buffer-file-name
)))
12678 (unless files
(error "No agenda files"))
12680 (while (setq file
(pop files
))
12681 (if (equal (file-truename file
) tcf
)
12683 (find-file (car files
))
12685 (find-file (car fs
)))
12686 (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
12688 (defun org-agenda-file-to-front (&optional to-end
)
12689 "Move/add the current file to the top of the agenda file list.
12690 If the file is not present in the list, it is added to the front. If it is
12691 present, it is moved there. With optional argument TO-END, add/move to the
12694 (let ((org-agenda-skip-unavailable-files nil
)
12695 (file-alist (mapcar (lambda (x)
12696 (cons (file-truename x
) x
))
12697 (org-agenda-files t
)))
12698 (ctf (file-truename buffer-file-name
))
12700 (setq x
(assoc ctf file-alist
) had x
)
12702 (if (not x
) (setq x
(cons ctf
(abbreviate-file-name buffer-file-name
))))
12704 (setq file-alist
(append (delq x file-alist
) (list x
)))
12705 (setq file-alist
(cons x
(delq x file-alist
))))
12706 (org-store-new-agenda-file-list (mapcar 'cdr file-alist
))
12707 (org-install-agenda-files-menu)
12708 (message "File %s to %s of agenda file list"
12709 (if had
"moved" "added") (if to-end
"end" "front"))))
12711 (defun org-remove-file (&optional file
)
12712 "Remove current file from the list of files in variable `org-agenda-files'.
12713 These are the files which are being checked for agenda entries.
12714 Optional argument FILE means, use this file instead of the current."
12716 (let* ((org-agenda-skip-unavailable-files nil
)
12717 (file (or file buffer-file-name
))
12718 (true-file (file-truename file
))
12719 (afile (abbreviate-file-name file
))
12720 (files (delq nil
(mapcar
12722 (if (equal true-file
12725 (org-agenda-files t
)))))
12726 (if (not (= (length files
) (length (org-agenda-files t
))))
12728 (org-store-new-agenda-file-list files
)
12729 (org-install-agenda-files-menu)
12730 (message "Removed file: %s" afile
))
12731 (message "File was not in list: %s (not removed)" afile
))))
12733 (defun org-file-menu-entry (file)
12734 (vector file
(list 'find-file file
) t
))
12736 (defun org-check-agenda-file (file)
12737 "Make sure FILE exists. If not, ask user what to do."
12738 (when (not (file-exists-p file
))
12739 (message "non-existent file %s. [R]emove from list or [A]bort?"
12740 (abbreviate-file-name file
))
12741 (let ((r (downcase (read-char-exclusive))))
12744 (org-remove-file file
)
12745 (throw 'nextfile t
))
12746 (t (error "Abort"))))))
12748 (defun org-get-agenda-file-buffer (file)
12749 "Get a buffer visiting FILE. If the buffer needs to be created, add
12750 it to the list of buffers which might be released later."
12751 (let ((buf (org-find-base-buffer-visiting file
)))
12753 buf
; just return it
12754 ;; Make a new buffer and remember it
12755 (setq buf
(find-file-noselect file
))
12756 (if buf
(push buf org-agenda-new-buffers
))
12759 (defun org-release-buffers (blist)
12760 "Release all buffers in list, asking the user for confirmation when needed.
12761 When a buffer is unmodified, it is just killed. When modified, it is saved
12762 \(if the user agrees) and then killed."
12764 (while (setq buf
(pop blist
))
12765 (setq file
(buffer-file-name buf
))
12766 (when (and (buffer-modified-p buf
)
12768 (y-or-n-p (format "Save file %s? " file
)))
12769 (with-current-buffer buf
(save-buffer)))
12770 (kill-buffer buf
))))
12772 (defun org-prepare-agenda-buffers (files)
12773 "Create buffers for all agenda files, protect archived trees and comments."
12775 (let ((pa '(:org-archived t
))
12776 (pc '(:org-comment t
))
12777 (pall '(:org-archived t
:org-comment t
))
12778 (inhibit-read-only t
)
12779 (rea (concat ":" org-archive-tag
":"))
12783 (while (setq file
(pop files
))
12786 (org-check-agenda-file file
)
12787 (set-buffer (org-get-agenda-file-buffer file
)))
12789 (setq bmp
(buffer-modified-p))
12790 (org-refresh-category-properties)
12791 (setq org-todo-keywords-for-agenda
12792 (append org-todo-keywords-for-agenda org-todo-keywords-1
))
12793 (setq org-done-keywords-for-agenda
12794 (append org-done-keywords-for-agenda org-done-keywords
))
12796 (remove-text-properties (point-min) (point-max) pall
)
12797 (when org-agenda-skip-archived-trees
12798 (goto-char (point-min))
12799 (while (re-search-forward rea nil t
)
12800 (if (org-on-heading-p t
)
12801 (add-text-properties (point-at-bol) (org-end-of-subtree t
) pa
))))
12802 (goto-char (point-min))
12803 (setq re
(concat "^\\*+ +" org-comment-string
"\\>"))
12804 (while (re-search-forward re nil t
)
12805 (add-text-properties
12806 (match-beginning 0) (org-end-of-subtree t
) pc
)))
12807 (set-buffer-modified-p bmp
))))))
12809 ;;;; Embedded LaTeX
12811 (defvar org-cdlatex-mode-map
(make-sparse-keymap)
12812 "Keymap for the minor `org-cdlatex-mode'.")
12814 (org-defkey org-cdlatex-mode-map
"_" 'org-cdlatex-underscore-caret
)
12815 (org-defkey org-cdlatex-mode-map
"^" 'org-cdlatex-underscore-caret
)
12816 (org-defkey org-cdlatex-mode-map
"`" 'cdlatex-math-symbol
)
12817 (org-defkey org-cdlatex-mode-map
"'" 'org-cdlatex-math-modify
)
12818 (org-defkey org-cdlatex-mode-map
"\C-c{" 'cdlatex-environment
)
12820 (defvar org-cdlatex-texmathp-advice-is-done nil
12821 "Flag remembering if we have applied the advice to texmathp already.")
12823 (define-minor-mode org-cdlatex-mode
12824 "Toggle the minor `org-cdlatex-mode'.
12825 This mode supports entering LaTeX environment and math in LaTeX fragments
12827 \\{org-cdlatex-mode-map}"
12829 (when org-cdlatex-mode
(require 'cdlatex
))
12830 (unless org-cdlatex-texmathp-advice-is-done
12831 (setq org-cdlatex-texmathp-advice-is-done t
)
12832 (defadvice texmathp
(around org-math-always-on activate
)
12833 "Always return t in org-mode buffers.
12834 This is because we want to insert math symbols without dollars even outside
12835 the LaTeX math segments. If Orgmode thinks that point is actually inside
12836 en embedded LaTeX fragement, let texmathp do its job.
12837 \\[org-cdlatex-mode-map]"
12841 ((not (org-mode-p)) ad-do-it
)
12842 ((eq this-command
'cdlatex-math-symbol
)
12843 (setq ad-return-value t
12844 texmathp-why
'("cdlatex-math-symbol in org-mode" .
0)))
12846 (let ((p (org-inside-LaTeX-fragment-p)))
12847 (if (and p
(member (car p
) (plist-get org-format-latex-options
:matchers
)))
12848 (setq ad-return-value t
12849 texmathp-why
'("Org-mode embedded math" .
0))
12850 (if p ad-do-it
)))))))))
12852 (defun turn-on-org-cdlatex ()
12853 "Unconditionally turn on `org-cdlatex-mode'."
12854 (org-cdlatex-mode 1))
12856 (defun org-inside-LaTeX-fragment-p ()
12857 "Test if point is inside a LaTeX fragment.
12858 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
12859 sequence appearing also before point.
12860 Even though the matchers for math are configurable, this function assumes
12861 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
12862 delimiters are skipped when they have been removed by customization.
12863 The return value is nil, or a cons cell with the delimiter and
12864 and the position of this delimiter.
12866 This function does a reasonably good job, but can locally be fooled by
12867 for example currency specifications. For example it will assume being in
12868 inline math after \"$22.34\". The LaTeX fragment formatter will only format
12869 fragments that are properly closed, but during editing, we have to live
12870 with the uncertainty caused by missing closing delimiters. This function
12871 looks only before point, not after."
12873 (let ((pos (point))
12874 (dodollar (member "$" (plist-get org-format-latex-options
:matchers
)))
12876 (re-search-backward (concat "^\\(" paragraph-start
"\\)") nil t
)
12878 dd-on str
(start 0) m re
)
12881 (setq str
(concat (buffer-substring lim
(point)) "\000 X$.")
12882 re
(nth 1 (assoc "$" org-latex-regexps
)))
12883 (while (string-match re str start
)
12885 ((= (match-end 0) (length str
))
12886 (throw 'exit
(cons "$" (+ lim
(match-beginning 0) 1))))
12887 ((= (match-end 0) (- (length str
) 5))
12889 (t (setq start
(match-end 0))))))
12890 (when (setq m
(re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t
))
12892 (and (match-beginning 1) (throw 'exit
(cons (match-string 1) m
)))
12893 (and (match-beginning 2) (throw 'exit nil
))
12895 (while (re-search-backward "\\$\\$" lim t
)
12896 (setq dd-on
(not dd-on
)))
12898 (if dd-on
(cons "$$" m
))))))
12901 (defun org-try-cdlatex-tab ()
12902 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
12903 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
12904 - inside a LaTeX fragment, or
12905 - after the first word in a line, where an abbreviation expansion could
12906 insert a LaTeX environment."
12907 (when org-cdlatex-mode
12910 (skip-chars-backward "a-zA-Z0-9*")
12911 (skip-chars-backward " \t")
12914 ((org-inside-LaTeX-fragment-p)
12918 (defun org-cdlatex-underscore-caret (&optional arg
)
12919 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
12920 Revert to the normal definition outside of these fragments."
12922 (if (org-inside-LaTeX-fragment-p)
12923 (call-interactively 'cdlatex-sub-superscript
)
12924 (let (org-cdlatex-mode)
12925 (call-interactively (key-binding (vector last-input-event
))))))
12927 (defun org-cdlatex-math-modify (&optional arg
)
12928 "Execute `cdlatex-math-modify' in LaTeX fragments.
12929 Revert to the normal definition outside of these fragments."
12931 (if (org-inside-LaTeX-fragment-p)
12932 (call-interactively 'cdlatex-math-modify
)
12933 (let (org-cdlatex-mode)
12934 (call-interactively (key-binding (vector last-input-event
))))))
12936 (defvar org-latex-fragment-image-overlays nil
12937 "List of overlays carrying the images of latex fragments.")
12938 (make-variable-buffer-local 'org-latex-fragment-image-overlays
)
12940 (defun org-remove-latex-fragment-image-overlays ()
12941 "Remove all overlays with LaTeX fragment images in current buffer."
12942 (mapc 'org-delete-overlay org-latex-fragment-image-overlays
)
12943 (setq org-latex-fragment-image-overlays nil
))
12945 (defun org-preview-latex-fragment (&optional subtree
)
12946 "Preview the LaTeX fragment at point, or all locally or globally.
12947 If the cursor is in a LaTeX fragment, create the image and overlay
12948 it over the source code. If there is no fragment at point, display
12949 all fragments in the current text, from one headline to the next. With
12950 prefix SUBTREE, display all fragments in the current subtree. With a
12951 double prefix `C-u C-u', or when the cursor is before the first headline,
12952 display all fragments in the buffer.
12953 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
12955 (org-remove-latex-fragment-image-overlays)
12958 (let (beg end at msg
)
12960 ((or (equal subtree
'(16))
12961 (not (save-excursion
12962 (re-search-backward (concat "^" outline-regexp
) nil t
))))
12963 (setq beg
(point-min) end
(point-max)
12964 msg
"Creating images for buffer...%s"))
12965 ((equal subtree
'(4))
12966 (org-back-to-heading)
12967 (setq beg
(point) end
(org-end-of-subtree t
)
12968 msg
"Creating images for subtree...%s"))
12970 (if (setq at
(org-inside-LaTeX-fragment-p))
12971 (goto-char (max (point-min) (- (cdr at
) 2)))
12972 (org-back-to-heading))
12973 (setq beg
(point) end
(progn (outline-next-heading) (point))
12974 msg
(if at
"Creating image...%s"
12975 "Creating images for entry...%s"))))
12977 (narrow-to-region beg end
)
12980 (concat "ltxpng/" (file-name-sans-extension
12981 (file-name-nondirectory
12982 buffer-file-name
)))
12983 default-directory
'overlays msg at
'forbuffer
)
12984 (message msg
"done. Use `C-c C-c' to remove images.")))))
12986 (defvar org-latex-regexps
12987 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t
)
12988 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
12989 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
12990 ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil
)
12991 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil
)
12992 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t
)
12993 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t
))
12994 "Regular expressions for matching embedded LaTeX.")
12996 (defun org-format-latex (prefix &optional dir overlays msg at forbuffer
)
12997 "Replace LaTeX fragments with links to an image, and produce images."
12998 (if (and overlays
(fboundp 'clear-image-cache
)) (clear-image-cache))
12999 (let* ((prefixnodir (file-name-nondirectory prefix
))
13000 (absprefix (expand-file-name prefix dir
))
13001 (todir (file-name-directory absprefix
))
13002 (opt org-format-latex-options
)
13003 (matchers (plist-get opt
:matchers
))
13004 (re-list org-latex-regexps
)
13005 (cnt 0) txt link beg end re e checkdir
13006 m n block linkfile movefile ov
)
13007 ;; Check if there are old images files with this prefix, and remove them
13008 (when (file-directory-p todir
)
13012 (concat (regexp-quote prefixnodir
) "_[0-9]+\\.png$"))))
13013 ;; Check the different regular expressions
13014 (while (setq e
(pop re-list
))
13015 (setq m
(car e
) re
(nth 1 e
) n
(nth 2 e
)
13016 block
(if (nth 3 e
) "\n\n" ""))
13017 (when (member m matchers
)
13018 (goto-char (point-min))
13019 (while (re-search-forward re nil t
)
13020 (when (or (not at
) (equal (cdr at
) (match-beginning n
)))
13021 (setq txt
(match-string n
)
13022 beg
(match-beginning n
) end
(match-end n
)
13024 linkfile
(format "%s_%04d.png" prefix cnt
)
13025 movefile
(format "%s_%04d.png" absprefix cnt
)
13026 link
(concat block
"[[file:" linkfile
"]]" block
))
13027 (if msg
(message msg cnt
))
13029 (unless checkdir
; make sure the directory exists
13031 (or (file-directory-p todir
) (make-directory todir
)))
13032 (org-create-formula-image
13033 txt movefile opt forbuffer
)
13036 (setq ov
(org-make-overlay beg end
))
13037 (if (featurep 'xemacs
)
13039 (org-overlay-put ov
'invisible t
)
13042 (make-glyph (vector 'png
:file movefile
))))
13045 (list 'image
:type
'png
:file movefile
:ascent
'center
)))
13046 (push ov org-latex-fragment-image-overlays
)
13048 (delete-region beg end
)
13049 (insert link
))))))))
13051 ;; This function borrows from Ganesh Swami's latex2png.el
13052 (defun org-create-formula-image (string tofile options buffer
)
13053 (let* ((tmpdir (if (featurep 'xemacs
)
13055 temporary-file-directory
))
13056 (texfilebase (make-temp-name
13057 (expand-file-name "orgtex" tmpdir
)))
13058 (texfile (concat texfilebase
".tex"))
13059 (dvifile (concat texfilebase
".dvi"))
13060 (pngfile (concat texfilebase
".png"))
13061 (fnh (face-attribute 'default
:height nil
))
13062 (scale (or (plist-get options
(if buffer
:scale
:html-scale
)) 1.0))
13063 (dpi (number-to-string (* scale
(floor (* 0.9 (if buffer fnh
140.
))))))
13064 (fg (or (plist-get options
(if buffer
:foreground
:html-foreground
))
13066 (bg (or (plist-get options
(if buffer
:background
:html-background
))
13068 (if (eq fg
'default
) (setq fg
(org-dvipng-color :foreground
)))
13069 (if (eq bg
'default
) (setq bg
(org-dvipng-color :background
)))
13070 (with-temp-file texfile
13071 (insert org-format-latex-header
13072 "\n\\begin{document}\n" string
"\n\\end{document}\n"))
13073 (let ((dir default-directory
))
13074 (condition-case nil
13077 (call-process "latex" nil nil nil texfile
))
13080 (if (not (file-exists-p dvifile
))
13081 (progn (message "Failed to create dvi file from %s" texfile
) nil
)
13082 (call-process "dvipng" nil nil nil
13083 "-E" "-fg" fg
"-bg" bg
13085 ;;"-x" scale "-y" scale
13089 (if (not (file-exists-p pngfile
))
13090 (progn (message "Failed to create png file from %s" texfile
) nil
)
13091 ;; Use the requested file name and clean up
13092 (copy-file pngfile tofile
'replace
)
13093 (loop for e in
'(".dvi" ".tex" ".aux" ".log" ".png") do
13094 (delete-file (concat texfilebase e
)))
13097 (defun org-dvipng-color (attr)
13098 "Return an rgb color specification for dvipng."
13099 (apply 'format
"rgb %s %s %s"
13100 (mapcar 'org-normalize-color
13101 (color-values (face-attribute 'default attr nil
)))))
13103 (defun org-normalize-color (value)
13104 "Return string to be used as color value for an RGB component."
13105 (format "%g" (/ value
65535.0)))
13110 ;; Make `C-c C-x' a prefix key
13111 (org-defkey org-mode-map
"\C-c\C-x" (make-sparse-keymap))
13113 ;; TAB key with modifiers
13114 (org-defkey org-mode-map
"\C-i" 'org-cycle
)
13115 (org-defkey org-mode-map
[(tab)] 'org-cycle
)
13116 (org-defkey org-mode-map
[(control tab
)] 'org-force-cycle-archived
)
13117 (org-defkey org-mode-map
[(meta tab
)] 'org-complete
)
13118 (org-defkey org-mode-map
"\M-\t" 'org-complete
)
13119 (org-defkey org-mode-map
"\M-\C-i" 'org-complete
)
13120 ;; The following line is necessary under Suse GNU/Linux
13121 (unless (featurep 'xemacs
)
13122 (org-defkey org-mode-map
[S-iso-lefttab
] 'org-shifttab
))
13123 (org-defkey org-mode-map
[(shift tab
)] 'org-shifttab
)
13124 (define-key org-mode-map
[backtab] 'org-shifttab)
13126 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
13127 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
13128 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
13130 ;; Cursor keys with modifiers
13131 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
13132 (org-defkey org-mode-map [(meta right)] 'org-metaright)
13133 (org-defkey org-mode-map [(meta up)] 'org-metaup)
13134 (org-defkey org-mode-map [(meta down)] 'org-metadown)
13136 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
13137 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
13138 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
13139 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
13141 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
13142 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
13143 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
13144 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
13146 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
13147 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
13149 ;;; Extra keys for tty access.
13150 ;; We only set them when really needed because otherwise the
13151 ;; menus don't show the simple keys
13153 (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
13154 (not window-system))
13155 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
13156 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
13157 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
13158 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
13159 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
13160 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
13161 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
13162 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
13163 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
13164 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
13165 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
13166 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
13167 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
13168 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
13169 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
13170 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
13171 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
13172 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
13173 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
13174 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
13175 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
13176 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
13178 ;; All the other keys
13180 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
13181 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
13182 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
13183 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
13184 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
13185 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
13186 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
13187 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
13188 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
13189 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
13190 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
13191 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
13192 (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
13193 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
13194 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
13195 (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
13196 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
13197 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
13198 (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
13199 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
13200 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
13201 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
13202 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
13203 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
13204 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
13205 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
13206 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
13207 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
13208 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
13209 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
13210 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
13211 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
13212 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
13213 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
13214 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
13215 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
13216 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
13217 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
13218 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
13219 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
13220 (org-defkey org-mode-map "\C-c^" 'org-sort)
13221 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
13222 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
13223 (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
13224 (org-defkey org-mode-map "\C-m" 'org-return)
13225 (org-defkey org-mode-map "\C-j" 'org-return-indent)
13226 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
13227 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
13228 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
13229 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
13230 (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
13231 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
13232 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
13233 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
13234 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
13235 (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
13236 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
13237 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
13238 (org-defkey org-mode-map "\C-c\C-e" 'org-export)
13239 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
13240 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
13242 (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
13243 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
13244 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
13245 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
13247 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
13248 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
13249 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
13250 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
13251 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
13252 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
13253 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
13254 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
13255 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
13256 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
13257 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
13258 (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
13260 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
13262 (when (featurep 'xemacs)
13263 (org-defkey org-mode-map 'button3 'popup-mode-menu))
13265 (defvar org-table-auto-blank-field) ; defined in org-table.el
13266 (defun org-self-insert-command (N)
13267 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
13268 If the cursor is in a table looking at whitespace, the whitespace is
13269 overwritten, and the table is not marked as requiring realignment."
13271 (if (and (org-table-p)
13273 ;; check if we blank the field, and if that triggers align
13274 (and (featurep 'org-table) org-table-auto-blank-field
13275 (member last-command
13276 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
13277 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
13278 ;; got extra space, this field does not determine column width
13279 (let (org-table-may-need-update) (org-table-blank-field))
13280 ;; no extra space, this field may determine column width
13281 (org-table-blank-field)))
13284 (looking-at "[^|\n]* |"))
13285 (let (org-table-may-need-update)
13286 (goto-char (1- (match-end 0)))
13287 (delete-backward-char 1)
13288 (goto-char (match-beginning 0))
13289 (self-insert-command N))
13290 (setq org-table-may-need-update t)
13291 (self-insert-command N)
13292 (org-fix-tags-on-the-fly)))
13294 (defun org-fix-tags-on-the-fly ()
13295 (when (and (equal (char-after (point-at-bol)) ?*)
13296 (org-on-heading-p))
13297 (org-align-tags-here org-tags-column)))
13299 (defun org-delete-backward-char (N)
13300 "Like `delete-backward-char', insert whitespace at field end in tables.
13301 When deleting backwards, in tables this function will insert whitespace in
13302 front of the next \"|\" separator, to keep the table aligned. The table will
13303 still be marked for re-alignment if the field did fill the entire column,
13304 because, in this case the deletion might narrow the column."
13306 (if (and (org-table-p)
13308 (string-match "|" (buffer-substring (point-at-bol) (point)))
13309 (looking-at ".*?|"))
13310 (let ((pos (point))
13311 (noalign (looking-at "[^|\n\r]* |"))
13312 (c org-table-may-need-update))
13313 (backward-delete-char N)
13314 (skip-chars-forward "^|")
13316 (goto-char (1- pos))
13317 ;; noalign: if there were two spaces at the end, this field
13318 ;; does not determine the width of the column.
13319 (if noalign (setq org-table-may-need-update c)))
13320 (backward-delete-char N)
13321 (org-fix-tags-on-the-fly)))
13323 (defun org-delete-char (N)
13324 "Like `delete-char', but insert whitespace at field end in tables.
13325 When deleting characters, in tables this function will insert whitespace in
13326 front of the next \"|\" separator, to keep the table aligned. The table will
13327 still be marked for re-alignment if the field did fill the entire column,
13328 because, in this case the deletion might narrow the column."
13330 (if (and (org-table-p)
13332 (not (= (char-after) ?|))
13334 (if (looking-at ".*?|")
13335 (let ((pos (point))
13336 (noalign (looking-at "[^|\n\r]* |"))
13337 (c org-table-may-need-update))
13338 (replace-match (concat
13339 (substring (match-string 0) 1 -1)
13342 ;; noalign: if there were two spaces at the end, this field
13343 ;; does not determine the width of the column.
13344 (if noalign (setq org-table-may-need-update c)))
13347 (org-fix-tags-on-the-fly)))
13349 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
13350 (put 'org-self-insert-command 'delete-selection t)
13351 (put 'orgtbl-self-insert-command 'delete-selection t)
13352 (put 'org-delete-char 'delete-selection 'supersede)
13353 (put 'org-delete-backward-char 'delete-selection 'supersede)
13355 ;; Make `flyspell-mode' delay after some commands
13356 (put 'org-self-insert-command 'flyspell-delayed t)
13357 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
13358 (put 'org-delete-char 'flyspell-delayed t)
13359 (put 'org-delete-backward-char 'flyspell-delayed t)
13361 ;; Make pabbrev-mode expand after org-mode commands
13362 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
13363 (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
13365 ;; How to do this: Measure non-white length of current string
13366 ;; If equal to column width, we should realign.
13368 (defun org-remap (map &rest commands)
13369 "In MAP, remap the functions given in COMMANDS.
13370 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
13373 (setq old (pop commands) new (pop commands))
13374 (if (fboundp 'command-remapping)
13375 (org-defkey map (vector 'remap old) new)
13376 (substitute-key-definition old new map global-map)))))
13378 (when (eq org-enable-table-editor 'optimized)
13379 ;; If the user wants maximum table support, we need to hijack
13380 ;; some standard editing functions
13381 (org-remap org-mode-map
13382 'self-insert-command 'org-self-insert-command
13383 'delete-char 'org-delete-char
13384 'delete-backward-char 'org-delete-backward-char)
13385 (org-defkey org-mode-map "|" 'org-force-self-insert))
13387 (defun org-shiftcursor-error ()
13388 "Throw an error because Shift-Cursor command was applied in wrong context."
13389 (error "This command is active in special context like tables, headlines or timestamps"))
13391 (defun org-shifttab (&optional arg)
13392 "Global visibility cycling or move to previous table field.
13393 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
13395 See the individual commands for more information."
13398 ((org-at-table-p) (call-interactively 'org-table-previous-field))
13399 (arg (message "Content view to level: ")
13400 (org-content (prefix-numeric-value arg))
13401 (setq org-cycle-global-status 'overview))
13402 (t (call-interactively 'org-global-cycle))))
13404 (defun org-shiftmetaleft ()
13405 "Promote subtree or delete table column.
13406 Calls `org-promote-subtree', `org-outdent-item',
13407 or `org-table-delete-column', depending on context.
13408 See the individual commands for more information."
13411 ((org-at-table-p) (call-interactively 'org-table-delete-column))
13412 ((org-on-heading-p) (call-interactively 'org-promote-subtree))
13413 ((org-at-item-p) (call-interactively 'org-outdent-item))
13414 (t (org-shiftcursor-error))))
13416 (defun org-shiftmetaright ()
13417 "Demote subtree or insert table column.
13418 Calls `org-demote-subtree', `org-indent-item',
13419 or `org-table-insert-column', depending on context.
13420 See the individual commands for more information."
13423 ((org-at-table-p) (call-interactively 'org-table-insert-column))
13424 ((org-on-heading-p) (call-interactively 'org-demote-subtree))
13425 ((org-at-item-p) (call-interactively 'org-indent-item))
13426 (t (org-shiftcursor-error))))
13428 (defun org-shiftmetaup (&optional arg)
13429 "Move subtree up or kill table row.
13430 Calls `org-move-subtree-up' or `org-table-kill-row' or
13431 `org-move-item-up' depending on context. See the individual commands
13432 for more information."
13435 ((org-at-table-p) (call-interactively 'org-table-kill-row))
13436 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
13437 ((org-at-item-p) (call-interactively 'org-move-item-up))
13438 (t (org-shiftcursor-error))))
13439 (defun org-shiftmetadown (&optional arg)
13440 "Move subtree down or insert table row.
13441 Calls `org-move-subtree-down' or `org-table-insert-row' or
13442 `org-move-item-down', depending on context. See the individual
13443 commands for more information."
13446 ((org-at-table-p) (call-interactively 'org-table-insert-row))
13447 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
13448 ((org-at-item-p) (call-interactively 'org-move-item-down))
13449 (t (org-shiftcursor-error))))
13451 (defun org-metaleft (&optional arg)
13452 "Promote heading or move table column to left.
13453 Calls `org-do-promote' or `org-table-move-column', depending on context.
13454 With no specific context, calls the Emacs default `backward-word'.
13455 See the individual commands for more information."
13458 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
13459 ((or (org-on-heading-p) (org-region-active-p))
13460 (call-interactively 'org-do-promote))
13461 ((org-at-item-p) (call-interactively 'org-outdent-item))
13462 (t (call-interactively 'backward-word))))
13464 (defun org-metaright (&optional arg)
13465 "Demote subtree or move table column to right.
13466 Calls `org-do-demote' or `org-table-move-column', depending on context.
13467 With no specific context, calls the Emacs default `forward-word'.
13468 See the individual commands for more information."
13471 ((org-at-table-p) (call-interactively 'org-table-move-column))
13472 ((or (org-on-heading-p) (org-region-active-p))
13473 (call-interactively 'org-do-demote))
13474 ((org-at-item-p) (call-interactively 'org-indent-item))
13475 (t (call-interactively 'forward-word))))
13477 (defun org-metaup (&optional arg)
13478 "Move subtree up or move table row up.
13479 Calls `org-move-subtree-up' or `org-table-move-row' or
13480 `org-move-item-up', depending on context. See the individual commands
13481 for more information."
13484 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
13485 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
13486 ((org-at-item-p) (call-interactively 'org-move-item-up))
13487 (t (transpose-lines 1) (beginning-of-line -1))))
13489 (defun org-metadown (&optional arg)
13490 "Move subtree down or move table row down.
13491 Calls `org-move-subtree-down' or `org-table-move-row' or
13492 `org-move-item-down', depending on context. See the individual
13493 commands for more information."
13496 ((org-at-table-p) (call-interactively 'org-table-move-row))
13497 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
13498 ((org-at-item-p) (call-interactively 'org-move-item-down))
13499 (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
13501 (defun org-shiftup (&optional arg)
13502 "Increase item in timestamp or increase priority of current headline.
13503 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
13504 depending on context. See the individual commands for more information."
13507 ((org-at-timestamp-p t)
13508 (call-interactively (if org-edit-timestamp-down-means-later
13509 'org-timestamp-down 'org-timestamp-up)))
13510 ((org-on-heading-p) (call-interactively 'org-priority-up))
13511 ((org-at-item-p) (call-interactively 'org-previous-item))
13512 ((org-clocktable-try-shift 'up arg))
13513 (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
13515 (defun org-shiftdown (&optional arg)
13516 "Decrease item in timestamp or decrease priority of current headline.
13517 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
13518 depending on context. See the individual commands for more information."
13521 ((org-at-timestamp-p t)
13522 (call-interactively (if org-edit-timestamp-down-means-later
13523 'org-timestamp-up 'org-timestamp-down)))
13524 ((org-on-heading-p) (call-interactively 'org-priority-down))
13525 ((org-clocktable-try-shift 'down arg))
13526 (t (call-interactively 'org-next-item))))
13528 (defun org-shiftright (&optional arg)
13529 "Next TODO keyword or timestamp one day later, depending on context."
13532 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
13533 ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
13534 ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
13535 ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
13536 ((org-clocktable-try-shift 'right arg))
13537 (t (org-shiftcursor-error))))
13539 (defun org-shiftleft (&optional arg)
13540 "Previous TODO keyword or timestamp one day earlier, depending on context."
13543 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
13544 ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
13545 ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
13546 ((org-at-property-p)
13547 (call-interactively 'org-property-previous-allowed-value))
13548 ((org-clocktable-try-shift 'left arg))
13549 (t (org-shiftcursor-error))))
13551 (defun org-shiftcontrolright ()
13552 "Switch to next TODO set."
13555 ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
13556 (t (org-shiftcursor-error))))
13558 (defun org-shiftcontrolleft ()
13559 "Switch to previous TODO set."
13562 ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
13563 (t (org-shiftcursor-error))))
13565 (defun org-ctrl-c-ret ()
13566 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
13569 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
13570 (t (call-interactively 'org-insert-heading))))
13572 (defun org-copy-special ()
13573 "Copy region in table or copy current subtree.
13574 Calls `org-table-copy' or `org-copy-subtree', depending on context.
13575 See the individual commands for more information."
13577 (call-interactively
13578 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
13580 (defun org-cut-special ()
13581 "Cut region in table or cut current subtree.
13582 Calls `org-table-copy' or `org-cut-subtree', depending on context.
13583 See the individual commands for more information."
13585 (call-interactively
13586 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
13588 (defun org-paste-special (arg)
13589 "Paste rectangular region into table, or past subtree relative to level.
13590 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
13591 See the individual commands for more information."
13593 (if (org-at-table-p)
13594 (org-table-paste-rectangle)
13595 (org-paste-subtree arg)))
13597 (defun org-ctrl-c-ctrl-c (&optional arg)
13598 "Set tags in headline, or update according to changed information at point.
13600 This command does many different things, depending on context:
13602 - If the cursor is in a headline, prompt for tags and insert them
13603 into the current line, aligned to `org-tags-column'. When called
13604 with prefix arg, realign all tags in the current buffer.
13606 - If the cursor is in one of the special #+KEYWORD lines, this
13607 triggers scanning the buffer for these lines and updating the
13610 - If the cursor is inside a table, realign the table. This command
13611 works even if the automatic table editor has been turned off.
13613 - If the cursor is on a #+TBLFM line, re-apply the formulas to
13616 - If the cursor is a the beginning of a dynamic block, update it.
13618 - If the cursor is inside a table created by the table.el package,
13619 activate that table.
13621 - If the current buffer is a remember buffer, close note and file it.
13622 with a prefix argument, file it without further interaction to the default
13625 - If the cursor is on a <<<target>>>, update radio targets and corresponding
13626 links in this buffer.
13628 - If the cursor is on a numbered item in a plain list, renumber the
13631 - If the cursor is on a checkbox, toggle it."
13633 (let ((org-enable-table-editor t))
13635 ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
13636 org-occur-highlights
13637 org-latex-fragment-image-overlays)
13638 (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
13639 (org-remove-occur-highlights)
13640 (org-remove-latex-fragment-image-overlays)
13641 (message "Temporary highlights/overlays removed from current buffer"))
13642 ((and (local-variable-p 'org-finish-function (current-buffer))
13643 (fboundp org-finish-function))
13644 (funcall org-finish-function))
13645 ((org-at-property-p)
13646 (call-interactively 'org-property-action))
13647 ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
13648 ((org-on-heading-p) (call-interactively 'org-set-tags))
13649 ((org-at-table.el-p)
13651 (beginning-of-line 1)
13652 (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
13653 (call-interactively 'table-recognize-table))
13655 (org-table-maybe-eval-formula)
13657 (call-interactively 'org-table-recalculate)
13658 (org-table-maybe-recalculate-line))
13659 (call-interactively 'org-table-align))
13660 ((org-at-item-checkbox-p)
13661 (call-interactively 'org-toggle-checkbox))
13663 (call-interactively 'org-maybe-renumber-ordered-list))
13664 ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
13666 (beginning-of-line 1)
13667 (org-update-dblock))
13668 ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
13670 ((equal (match-string 1) "TBLFM")
13671 ;; Recalculate the table before this line
13673 (beginning-of-line 1)
13674 (skip-chars-backward " \r\n\t")
13675 (if (org-at-table-p)
13676 (org-call-with-arg 'org-table-recalculate t))))
13678 (call-interactively 'org-mode-restart))))
13679 (t (error "C-c C-c can do nothing useful at this location.")))))
13681 (defun org-mode-restart ()
13682 "Restart Org-mode, to scan again for special lines.
13683 Also updates the keyword regular expressions."
13685 (let ((org-inhibit-startup t)) (org-mode))
13686 (message "Org-mode restarted to refresh keyword and special line setup"))
13688 (defun org-kill-note-or-show-branches ()
13689 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
13691 (if (not org-finish-function)
13692 (call-interactively 'show-branches)
13693 (let ((org-note-abort t))
13694 (funcall org-finish-function))))
13696 (defun org-return (&optional indent)
13697 "Goto next table row or insert a newline.
13698 Calls `org-table-next-row' or `newline', depending on context.
13699 See the individual commands for more information."
13702 ((bobp) (if indent (newline-and-indent) (newline)))
13703 ((and (org-at-heading-p)
13705 (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
13710 (org-table-justify-field-maybe)
13711 (call-interactively 'org-table-next-row))
13712 (t (if indent (newline-and-indent) (newline)))))
13714 (defun org-return-indent ()
13715 "Goto next table row or insert a newline and indent.
13716 Calls `org-table-next-row' or `newline-and-indent', depending on
13717 context. See the individual commands for more information."
13721 (defun org-ctrl-c-star ()
13722 "Compute table, or change heading status of lines.
13723 Calls `org-table-recalculate' or `org-toggle-region-headlines',
13724 depending on context. This will also turn a plain list item or a normal
13725 line into a subheading."
13729 (call-interactively 'org-table-recalculate))
13730 ((org-region-active-p)
13731 ;; Convert all lines in region to list items
13732 (call-interactively 'org-toggle-region-headings))
13733 ((org-on-heading-p)
13734 (org-toggle-region-headings (point-at-bol)
13735 (min (1+ (point-at-eol)) (point-max))))
13737 ;; Convert to heading
13738 (let ((level (save-match-data
13740 (condition-case nil
13742 (org-back-to-heading t)
13743 (funcall outline-level))
13746 (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
13747 (t (org-toggle-region-headings (point-at-bol)
13748 (min (1+ (point-at-eol)) (point-max))))))
13750 (defun org-ctrl-c-minus ()
13751 "Insert separator line in table or modify bullet status of line.
13752 Also turns a plain line or a region of lines into list items.
13753 Calls `org-table-insert-hline', `org-toggle-region-items', or
13754 `org-cycle-list-bullet', depending on context."
13758 (call-interactively 'org-table-insert-hline))
13759 ((org-on-heading-p)
13762 (beginning-of-line 1)
13763 (if (looking-at "\\*+ ")
13764 (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
13765 ((org-region-active-p)
13766 ;; Convert all lines in region to list items
13767 (call-interactively 'org-toggle-region-items))
13769 (call-interactively 'org-cycle-list-bullet))
13770 (t (org-toggle-region-items (point-at-bol)
13771 (min (1+ (point-at-eol)) (point-max))))))
13773 (defun org-toggle-region-items (beg end)
13774 "Convert all lines in region to list items.
13775 If the first line is already an item, convert all list items in the region
13781 (setq l2 (org-current-line))
13783 (beginning-of-line 1)
13784 (setq l (1- (org-current-line)))
13785 (if (org-at-item-p)
13786 ;; We already have items, de-itemize
13787 (while (< (setq l (1+ l)) l2)
13788 (when (org-at-item-p)
13789 (goto-char (match-beginning 2))
13790 (delete-region (match-beginning 2) (match-end 2))
13791 (and (looking-at "[ \t]+") (replace-match "")))
13792 (beginning-of-line 2))
13793 (while (< (setq l (1+ l)) l2)
13794 (unless (org-at-item-p)
13795 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
13796 (replace-match "\\1- \\2")))
13797 (beginning-of-line 2))))))
13799 (defun org-toggle-region-headings (beg end)
13800 "Convert all lines in region to list items.
13801 If the first line is already an item, convert all list items in the region
13807 (setq l2 (org-current-line))
13809 (beginning-of-line 1)
13810 (setq l (1- (org-current-line)))
13811 (if (org-on-heading-p)
13812 ;; We already have headlines, de-star them
13813 (while (< (setq l (1+ l)) l2)
13814 (when (org-on-heading-p t)
13815 (and (looking-at outline-regexp) (replace-match "")))
13816 (beginning-of-line 2))
13817 (let* ((stars (save-excursion
13818 (re-search-backward org-complex-heading-regexp nil t)
13819 (or (match-string 1) "*")))
13820 (add-stars (if org-odd-levels-only "**" "*"))
13821 (rpl (concat stars add-stars " \\2")))
13822 (while (< (setq l (1+ l)) l2)
13823 (unless (org-on-heading-p)
13824 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
13825 (replace-match rpl)))
13826 (beginning-of-line 2)))))))
13828 (defun org-meta-return (&optional arg)
13829 "Insert a new heading or wrap a region in a table.
13830 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
13831 See the individual commands for more information."
13835 (call-interactively 'org-table-wrap-region))
13836 (t (call-interactively 'org-insert-heading))))
13840 ;; Define the Org-mode menus
13841 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
13843 ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
13844 ["Next Field" org-cycle (org-at-table-p)]
13845 ["Previous Field" org-shifttab (org-at-table-p)]
13846 ["Next Row" org-return (org-at-table-p)]
13848 ["Blank Field" org-table-blank-field (org-at-table-p)]
13849 ["Edit Field" org-table-edit-field (org-at-table-p)]
13850 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
13853 ["Move Column Left" org-metaleft (org-at-table-p)]
13854 ["Move Column Right" org-metaright (org-at-table-p)]
13855 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
13856 ["Insert Column" org-shiftmetaright (org-at-table-p)])
13858 ["Move Row Up" org-metaup (org-at-table-p)]
13859 ["Move Row Down" org-metadown (org-at-table-p)]
13860 ["Delete Row" org-shiftmetaup (org-at-table-p)]
13861 ["Insert Row" org-shiftmetadown (org-at-table-p)]
13862 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
13864 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
13866 ["Copy Rectangle" org-copy-special (org-at-table-p)]
13867 ["Cut Rectangle" org-cut-special (org-at-table-p)]
13868 ["Paste Rectangle" org-paste-special (org-at-table-p)]
13869 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
13872 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
13873 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
13874 ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
13876 ["Recalculate line" org-table-recalculate (org-at-table-p)]
13877 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
13878 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
13880 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
13882 ["Sum Column/Rectangle" org-table-sum
13883 (or (org-at-table-p) (org-region-active-p))]
13884 ["Which Column?" org-table-current-column (org-at-table-p)])
13886 org-table-toggle-formula-debugger
13887 :style toggle :selected org-table-formula-debug]
13888 ["Show Col/Row Numbers"
13889 org-table-toggle-coordinate-overlays
13890 :style toggle :selected org-table-overlay-coordinates]
13892 ["Create" org-table-create (and (not (org-at-table-p))
13893 org-enable-table-editor)]
13894 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
13895 ["Import from File" org-table-import (not (org-at-table-p))]
13896 ["Export to File" org-table-export (org-at-table-p)]
13898 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
13900 (easy-menu-define org-org-menu org-mode-map "Org menu"
13903 ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
13904 ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
13905 ["Sparse Tree" org-occur t]
13906 ["Reveal Context" org-reveal t]
13907 ["Show All" show-all t]
13909 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
13911 ["New Heading" org-insert-heading t]
13912 ("Navigate Headings"
13913 ["Up" outline-up-heading t]
13914 ["Next" outline-next-visible-heading t]
13915 ["Previous" outline-previous-visible-heading t]
13916 ["Next Same Level" outline-forward-same-level t]
13917 ["Previous Same Level" outline-backward-same-level t]
13919 ["Jump" org-goto t])
13921 ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
13922 ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
13924 ["Copy Subtree" org-copy-special (not (org-at-table-p))]
13925 ["Cut Subtree" org-cut-special (not (org-at-table-p))]
13926 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
13928 ["Promote Heading" org-metaleft (not (org-at-table-p))]
13929 ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
13930 ["Demote Heading" org-metaright (not (org-at-table-p))]
13931 ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
13933 ["Sort Region/Children" org-sort (not (org-at-table-p))]
13935 ["Convert to odd levels" org-convert-to-odd-levels t]
13936 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
13938 ["Emphasis..." org-emphasize t])
13940 ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
13941 ; ["Check and Tag Children" (org-toggle-archive-tag (4))
13942 ; :active t :keys "C-u C-c C-x C-a"]
13943 ["Sparse trees open ARCHIVE trees"
13944 (setq org-sparse-tree-open-archived-trees
13945 (not org-sparse-tree-open-archived-trees))
13946 :style toggle :selected org-sparse-tree-open-archived-trees]
13947 ["Cycling opens ARCHIVE trees"
13948 (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
13949 :style toggle :selected org-cycle-open-archived-trees]
13950 ["Agenda includes ARCHIVE trees"
13951 (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
13952 :style toggle :selected (not org-agenda-skip-archived-trees)]
13954 ["Move Subtree to Archive" org-advertized-archive-subtree t]
13955 ; ["Check and Move Children" (org-archive-subtree '(4))
13956 ; :active t :keys "C-u C-c C-x C-s"]
13960 ["TODO/DONE/-" org-todo t]
13962 ["Next keyword" org-shiftright (org-on-heading-p)]
13963 ["Previous keyword" org-shiftleft (org-on-heading-p)]
13964 ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
13965 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
13966 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
13967 ["Show TODO Tree" org-show-todo-tree t]
13968 ["Global TODO list" org-todo-list t]
13970 ["Set Priority" org-priority t]
13971 ["Priority Up" org-shiftup t]
13972 ["Priority Down" org-shiftdown t])
13973 ("TAGS and Properties"
13974 ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
13975 ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
13977 ["Set property" 'org-set-property t]
13978 ["Column view of properties" org-columns t]
13979 ["Insert Column View DBlock" org-insert-columns-dblock t])
13980 ("Dates and Scheduling"
13981 ["Timestamp" org-time-stamp t]
13982 ["Timestamp (inactive)" org-time-stamp-inactive t]
13984 ["1 Day Later" org-shiftright t]
13985 ["1 Day Earlier" org-shiftleft t]
13986 ["1 ... Later" org-shiftup t]
13987 ["1 ... Earlier" org-shiftdown t])
13988 ["Compute Time Range" org-evaluate-time-range t]
13989 ["Schedule Item" org-schedule t]
13990 ["Deadline" org-deadline t]
13992 ["Custom time format" org-toggle-time-stamp-overlays
13993 :style radio :selected org-display-custom-times]
13995 ["Goto Calendar" org-goto-calendar t]
13996 ["Date from Calendar" org-date-from-calendar t])
13998 ["Clock in" org-clock-in t]
13999 ["Clock out" org-clock-out t]
14000 ["Clock cancel" org-clock-cancel t]
14001 ["Goto running clock" org-clock-goto t]
14002 ["Display times" org-clock-display t]
14003 ["Create clock table" org-clock-report t]
14005 ["Record DONE time"
14006 (progn (setq org-log-done (not org-log-done))
14007 (message "Switching to %s will %s record a timestamp"
14008 (car org-done-keywords)
14009 (if org-log-done "automatically" "not")))
14010 :style toggle :selected org-log-done])
14012 ["Agenda Command..." org-agenda t]
14013 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
14014 ("File List for Agenda")
14015 ("Special views current file"
14016 ["TODO Tree" org-show-todo-tree t]
14017 ["Check Deadlines" org-check-deadlines t]
14018 ["Timeline" org-timeline t]
14019 ["Tags Tree" org-tags-sparse-tree t])
14022 ["Store Link (Global)" org-store-link t]
14023 ["Insert Link" org-insert-link t]
14024 ["Follow Link" org-open-at-point t]
14026 ["Next link" org-next-link t]
14027 ["Previous link" org-previous-link t]
14029 ["Descriptive Links"
14030 (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
14031 :style radio :selected (member '(org-link) buffer-invisibility-spec)]
14034 (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
14035 :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
14037 ["Export/Publish..." org-export t]
14039 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
14040 :selected org-cdlatex-mode]
14041 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
14042 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
14043 ["Modify math symbol" org-cdlatex-math-modify
14044 (org-inside-LaTeX-fragment-p)]
14045 ["Export LaTeX fragments as images"
14046 (if (featurep 'org-exp)
14047 (setq org-export-with-LaTeX-fragments
14048 (not org-export-with-LaTeX-fragments))
14049 (require 'org-exp))
14050 :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
14051 org-export-with-LaTeX-fragments)])
14054 ["Show Version" org-version t]
14055 ["Info Documentation" org-info t])
14057 ["Browse Org Group" org-customize t]
14059 ["Expand This Menu" org-create-customize-menu
14060 (fboundp 'customize-menu-create)])
14062 ["Refresh setup" org-mode-restart t]
14065 (defun org-info (&optional node)
14066 "Read documentation for Org-mode in the info system.
14067 With optional NODE, go directly to that node."
14069 (info (format "(org)%s" (or node ""))))
14071 (defun org-install-agenda-files-menu ()
14072 (let ((bl (buffer-list)))
14075 (set-buffer (pop bl))
14076 (if (org-mode-p) (setq bl nil)))
14079 '("Org") "File List for Agenda"
14082 ["Edit File List" (org-edit-agenda-file-list) t]
14083 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
14084 ["Remove Current File from List" org-remove-file t]
14085 ["Cycle through agenda files" org-cycle-agenda-files t]
14086 ["Occur in all agenda files" org-occur-in-agenda-files t]
14088 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
14092 (defun org-customize ()
14093 "Call the customize function with org as argument."
14095 (customize-browse 'org))
14097 (defun org-create-customize-menu ()
14098 "Create a full customization menu for Org-mode, insert it into the menu."
14100 (if (fboundp 'customize-menu-create)
14103 '("Org") "Customize"
14104 `(["Browse Org group" org-customize t]
14106 ,(customize-menu-create 'org)
14107 ["Set" Custom-set t]
14108 ["Save" Custom-save t]
14109 ["Reset to Current" Custom-reset-current t]
14110 ["Reset to Saved" Custom-reset-saved t]
14111 ["Reset to Standard Settings" Custom-reset-standard t]))
14112 (message "\"Org\"-menu now contains full customization menu"))
14113 (error "Cannot expand menu (outdated version of cus-edit.el)")))
14115 ;;;; Miscellaneous stuff
14117 ;;; Generally useful functions
14119 (defun org-force-self-insert (N)
14120 "Needed to enforce self-insert under remapping."
14122 (self-insert-command N))
14124 (defun org-string-width (s)
14125 "Compute width of string, ignoring invisible characters.
14126 This ignores character with invisibility property `org-link', and also
14127 characters with property `org-cwidth', because these will become invisible
14128 upon the next fontification round."
14130 (when (or (eq t buffer-invisibility-spec)
14131 (assq 'org-link buffer-invisibility-spec))
14132 (while (setq b (text-property-any 0 (length s)
14133 'invisible 'org-link s))
14134 (setq s (concat (substring s 0 b)
14135 (substring s (or (next-single-property-change
14136 b 'invisible s) (length s)))))))
14137 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
14138 (setq s (concat (substring s 0 b)
14139 (substring s (or (next-single-property-change
14140 b 'org-cwidth s) (length s))))))
14141 (setq l (string-width s) b -1)
14142 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
14143 (setq l (- l (get-text-property b 'org-dwidth-n s))))
14147 (defun org-trim (s)
14148 "Remove whitespace at beginning and end of string."
14149 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
14150 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
14153 (defun org-wrap (string &optional width lines)
14154 "Wrap string to either a number of lines, or a width in characters.
14155 If WIDTH is non-nil, the string is wrapped to that width, however many lines
14156 that costs. If there is a word longer than WIDTH, the text is actually
14157 wrapped to the length of that word.
14158 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
14159 many lines, whatever width that takes.
14160 The return value is a list of lines, without newlines at the end."
14161 (let* ((words (org-split-string string "[ \t\n]+"))
14162 (maxword (apply 'max (mapcar 'org-string-width words)))
14165 (org-do-wrap words (max maxword width)))
14168 (setq ll (org-do-wrap words maxword))
14169 (if (<= (length ll) lines)
14172 (while (> (length ll) lines)
14174 (setq ll (org-do-wrap words w)))
14176 (t (error "Cannot wrap this")))))
14178 (defun org-do-wrap (words width)
14179 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
14182 (setq line (pop words))
14183 (while (and words (< (+ (length line) (length (car words))) width))
14184 (setq line (concat line " " (pop words))))
14185 (setq lines (push line lines)))
14188 (defun org-split-string (string &optional separators)
14189 "Splits STRING into substrings at SEPARATORS.
14190 No empty strings are returned if there are matches at the beginning
14191 and end of string."
14192 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
14196 (while (and (string-match rexp string
14198 (= start (match-beginning 0))
14199 (< start (length string)))
14201 (< (match-beginning 0) (length string)))
14203 (or (eq (match-beginning 0) 0)
14204 (and (eq (match-beginning 0) (match-end 0))
14205 (eq (match-beginning 0) start))
14207 (cons (substring string start (match-beginning 0))
14209 (setq start (match-end 0)))
14210 (or (eq start (length string))
14212 (cons (substring string start)
14216 (defun org-context ()
14217 "Return a list of contexts of the current cursor position.
14218 If several contexts apply, all are returned.
14219 Each context entry is a list with a symbol naming the context, and
14220 two positions indicating start and end of the context. Possible
14223 :headline anywhere in a headline
14224 :headline-stars on the leading stars in a headline
14225 :todo-keyword on a TODO keyword (including DONE) in a headline
14226 :tags on the TAGS in a headline
14227 :priority on the priority cookie in a headline
14228 :item on the first line of a plain list item
14229 :item-bullet on the bullet/number of a plain list item
14230 :checkbox on the checkbox in a plain list item
14231 :table in an org-mode table
14232 :table-special on a special filed in a table
14233 :table-table in a table.el table
14234 :link on a hyperlink
14235 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
14236 :target on a <<target>>
14237 :radio-target on a <<<radio-target>>>
14238 :latex-fragment on a LaTeX fragment
14239 :latex-preview on a LaTeX fragment with overlayed preview image
14241 This function expects the position to be visible because it uses font-lock
14242 faces as a help to recognize the following contexts: :table-special, :link,
14244 (let* ((f (get-text-property (point) 'face))
14245 (faces (if (listp f) f (list f)))
14246 (p (point)) clist o)
14247 ;; First the large context
14249 ((org-on-heading-p t)
14250 (push (list :headline (point-at-bol) (point-at-eol)) clist)
14252 (beginning-of-line 1)
14253 (looking-at org-todo-line-tags-regexp))
14254 (push (org-point-in-group p 1 :headline-stars) clist)
14255 (push (org-point-in-group p 2 :todo-keyword) clist)
14256 (push (org-point-in-group p 4 :tags) clist))
14258 (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
14259 (if (looking-at "\\[#[A-Z0-9]\\]")
14260 (push (org-point-in-group p 0 :priority) clist)))
14263 (push (org-point-in-group p 2 :item-bullet) clist)
14264 (push (list :item (point-at-bol)
14265 (save-excursion (org-end-of-item) (point)))
14267 (and (org-at-item-checkbox-p)
14268 (push (org-point-in-group p 0 :checkbox) clist)))
14271 (push (list :table (org-table-begin) (org-table-end)) clist)
14272 (if (memq 'org-formula faces)
14273 (push (list :table-special
14274 (previous-single-property-change p 'face)
14275 (next-single-property-change p 'face)) clist)))
14276 ((org-at-table-p 'any)
14277 (push (list :table-table) clist)))
14280 ;; Now the small context
14282 ((org-at-timestamp-p)
14283 (push (org-point-in-group p 0 :timestamp) clist))
14284 ((memq 'org-link faces)
14286 (previous-single-property-change p 'face)
14287 (next-single-property-change p 'face)) clist))
14288 ((memq 'org-special-keyword faces)
14289 (push (list :keyword
14290 (previous-single-property-change p 'face)
14291 (next-single-property-change p 'face)) clist))
14293 (push (org-point-in-group p 0 :target) clist)
14294 (goto-char (1- (match-beginning 0)))
14295 (if (looking-at org-radio-target-regexp)
14296 (push (org-point-in-group p 0 :radio-target) clist))
14298 ((setq o (car (delq nil
14301 (if (memq x org-latex-fragment-image-overlays) x))
14302 (org-overlays-at (point))))))
14303 (push (list :latex-fragment
14304 (org-overlay-start o) (org-overlay-end o)) clist)
14305 (push (list :latex-preview
14306 (org-overlay-start o) (org-overlay-end o)) clist))
14307 ((org-inside-LaTeX-fragment-p)
14308 ;; FIXME: positions wrong.
14309 (push (list :latex-fragment (point) (point)) clist)))
14311 (setq clist (nreverse (delq nil clist)))
14314 ;; FIXME: Compare with at-regexp-p Do we need both?
14315 (defun org-in-regexp (re &optional nlines visually)
14316 "Check if point is inside a match of regexp.
14317 Normally only the current line is checked, but you can include NLINES extra
14318 lines both before and after point into the search.
14319 If VISUALLY is set, require that the cursor is not after the match but
14320 really on, so that the block visually is on the match."
14322 (let ((pos (point))
14323 (eol (point-at-eol (+ 1 (or nlines 0))))
14324 (inc (if visually 1 0)))
14326 (beginning-of-line (- 1 (or nlines 0)))
14327 (while (re-search-forward re eol t)
14328 (if (and (<= (match-beginning 0) pos)
14329 (>= (+ inc (match-end 0)) pos))
14330 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
14332 (defun org-at-regexp-p (regexp)
14333 "Is point inside a match of REGEXP in the current line?"
14336 (let ((pos (point)) (end (point-at-eol)))
14337 (beginning-of-line 1)
14338 (while (re-search-forward regexp end t)
14339 (if (and (<= (match-beginning 0) pos)
14340 (>= (match-end 0) pos))
14344 (defun org-occur-in-agenda-files (regexp &optional nlines)
14345 "Call `multi-occur' with buffers for all agenda files."
14346 (interactive "sOrg-files matching: \np")
14347 (let* ((files (org-agenda-files))
14348 (tnames (mapcar 'file-truename files))
14349 (extra org-agenda-text-search-extra-files)
14351 (while (setq f (pop extra))
14352 (unless (member (file-truename f) tnames)
14353 (add-to-list 'files f 'append)
14354 (add-to-list 'tnames (file-truename f) 'append)))
14356 (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
14359 (if (boundp 'occur-mode-find-occurrence-hook)
14361 (add-hook 'occur-mode-find-occurrence-hook
14366 (defadvice occur-mode-goto-occurrence
14367 (after org-occur-reveal activate)
14368 (and (org-mode-p) (org-reveal)))
14369 (defadvice occur-mode-goto-occurrence-other-window
14370 (after org-occur-reveal activate)
14371 (and (org-mode-p) (org-reveal)))
14372 (defadvice occur-mode-display-occurrence
14373 (after org-occur-reveal activate)
14375 (let ((pos (occur-mode-find-occurrence)))
14376 (with-current-buffer (marker-buffer pos)
14381 (defun org-uniquify (list)
14382 "Remove duplicate elements from LIST."
14384 (mapc (lambda (x) (add-to-list 'res x 'append)) list)
14387 (defun org-delete-all (elts list)
14388 "Remove all elements in ELTS from LIST."
14390 (setq list (delete (pop elts) list)))
14393 (defun org-back-over-empty-lines ()
14394 "Move backwards over witespace, to the beginning of the first empty line.
14395 Returns the number of empty lines passed."
14396 (let ((pos (point)))
14397 (skip-chars-backward " \t\n\r")
14398 (beginning-of-line 2)
14399 (goto-char (min (point) pos))
14400 (count-lines (point) pos)))
14402 (defun org-skip-whitespace ()
14403 (skip-chars-forward " \t\n\r"))
14405 (defun org-point-in-group (point group &optional context)
14406 "Check if POINT is in match-group GROUP.
14407 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
14408 match. If the match group does ot exist or point is not inside it,
14410 (and (match-beginning group)
14411 (>= point (match-beginning group))
14412 (<= point (match-end group))
14414 (list context (match-beginning group) (match-end group))
14417 (defun org-switch-to-buffer-other-window (&rest args)
14418 "Switch to buffer in a second window on the current frame.
14419 In particular, do not allow pop-up frames."
14420 (let (pop-up-frames special-display-buffer-names special-display-regexps
14421 special-display-function)
14422 (apply 'switch-to-buffer-other-window args)))
14424 (defun org-combine-plists (&rest plists)
14425 "Create a single property list from all plists in PLISTS.
14426 The process starts by copying the first list, and then setting properties
14427 from the other lists. Settings in the last list are the most significant
14428 ones and overrule settings in the other lists."
14429 (let ((rtn (copy-sequence (pop plists)))
14432 (setq ls (pop plists))
14434 (setq p (pop ls) v (pop ls))
14435 (setq rtn (plist-put rtn p v))))
14438 (defun org-move-line-down (arg)
14439 "Move the current line down. With prefix argument, move it past ARG lines."
14441 (let ((col (current-column))
14443 (beginning-of-line 1) (setq beg (point))
14444 (beginning-of-line 2) (setq end (point))
14445 (beginning-of-line (+ 1 arg))
14446 (setq pos (move-marker (make-marker) (point)))
14447 (insert (delete-and-extract-region beg end))
14449 (move-to-column col)))
14451 (defun org-move-line-up (arg)
14452 "Move the current line up. With prefix argument, move it past ARG lines."
14454 (let ((col (current-column))
14456 (beginning-of-line 1) (setq beg (point))
14457 (beginning-of-line 2) (setq end (point))
14458 (beginning-of-line (- arg))
14459 (setq pos (move-marker (make-marker) (point)))
14460 (insert (delete-and-extract-region beg end))
14462 (move-to-column col)))
14464 (defun org-replace-escapes (string table)
14465 "Replace %-escapes in STRING with values in TABLE.
14466 TABLE is an association list with keys like \"%a\" and string values.
14467 The sequences in STRING may contain normal field width and padding information,
14468 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
14469 so values can contain further %-escapes if they are define later in TABLE."
14470 (let ((case-fold-search nil)
14472 (while (setq e (pop table))
14473 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
14474 (while (string-match re string)
14475 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
14477 (setq string (replace-match rpl t t string))))
14481 (defun org-sublist (list start end)
14482 "Return a section of LIST, from START to END.
14483 Counting starts at 1."
14484 (let (rtn (c start))
14485 (setq list (nthcdr (1- start) list))
14486 (while (and list (<= c end))
14487 (push (pop list) rtn)
14491 (defun org-find-base-buffer-visiting (file)
14492 "Like `find-buffer-visiting' but alway return the base buffer and
14493 not an indirect buffer."
14494 (let ((buf (find-buffer-visiting file)))
14496 (or (buffer-base-buffer buf) buf)
14499 (defun org-image-file-name-regexp ()
14500 "Return regexp matching the file names of images."
14501 (if (fboundp 'image-file-name-regexp)
14502 (image-file-name-regexp)
14503 (let ((image-file-name-extensions
14504 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
14505 "xbm" "xpm" "pbm" "pgm" "ppm")))
14507 (regexp-opt (nconc (mapcar 'upcase
14508 image-file-name-extensions)
14509 image-file-name-extensions)
14513 (defun org-file-image-p (file)
14514 "Return non-nil if FILE is an image."
14516 (string-match (org-image-file-name-regexp) file)))
14518 ;;; Paragraph filling stuff.
14519 ;; We want this to be just right, so use the full arsenal.
14521 (defun org-indent-line-function ()
14522 "Indent line like previous, but further if previous was headline or item."
14524 (let* ((pos (point))
14525 (itemp (org-at-item-p))
14526 column bpos bcol tpos tcol bullet btype bullet-type)
14527 ;; Find the previous relevant line
14528 (beginning-of-line 1)
14530 ((looking-at "#") (setq column 0))
14531 ((looking-at "\\*+ ") (setq column 0))
14533 (beginning-of-line 0)
14534 (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
14535 (beginning-of-line 0))
14537 ((looking-at "\\*+[ \t]+")
14538 (goto-char (match-end 0))
14539 (setq column (current-column)))
14541 (org-beginning-of-item)
14542 ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
14543 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
14544 (setq bpos (match-beginning 1) tpos (match-end 0)
14545 bcol (progn (goto-char bpos) (current-column))
14546 tcol (progn (goto-char tpos) (current-column))
14547 bullet (match-string 1)
14548 bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
14552 (beginning-of-line 1)
14553 (if (looking-at "\\S-")
14555 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
14556 (setq bullet (match-string 1)
14557 btype (if (string-match "[0-9]" bullet) "n" bullet))
14558 (setq column (if (equal btype bullet-type) bcol tcol)))
14559 (setq column (org-get-indentation)))))
14560 (t (setq column (org-get-indentation))))))
14562 (if (<= (current-column) (current-indentation))
14563 (indent-line-to column)
14564 (save-excursion (indent-line-to column)))
14565 (setq column (current-column))
14566 (beginning-of-line 1)
14568 "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
14569 (replace-match (concat "\\1" (format org-property-format
14570 (match-string 2) (match-string 3)))
14572 (move-to-column column)))
14574 (defun org-set-autofill-regexps ()
14576 ;; In the paragraph separator we include headlines, because filling
14577 ;; text in a line directly attached to a headline would otherwise
14578 ;; fill the headline as well.
14579 (org-set-local 'comment-start-skip "^#+[ \t]*")
14580 (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
14581 ;; The paragraph starter includes hand-formatted lists.
14582 (org-set-local 'paragraph-start
14583 "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
14584 ;; Inhibit auto-fill for headers, tables and fixed-width lines.
14585 ;; But only if the user has not turned off tables or fixed-width regions
14587 'auto-fill-inhibit-regexp
14588 (concat "\\*+ \\|#\\+"
14589 "\\|[ \t]*" org-keyword-time-regexp
14590 (if (or org-enable-table-editor org-enable-fixed-width-editor)
14593 (if org-enable-table-editor "|" "")
14594 (if org-enable-fixed-width-editor ":" "")
14596 ;; We use our own fill-paragraph function, to make sure that tables
14597 ;; and fixed-width regions are not wrapped. That function will pass
14598 ;; through to `fill-paragraph' when appropriate.
14599 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
14600 ; Adaptive filling: To get full control, first make sure that
14601 ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
14602 (org-set-local 'adaptive-fill-regexp "\000")
14603 (org-set-local 'adaptive-fill-function
14604 'org-adaptive-fill-function)
14606 'align-mode-rules-list
14607 '((org-in-buffer-settings
14608 (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
14609 (modes . '(org-mode))))))
14611 (defun org-fill-paragraph (&optional justify)
14612 "Re-align a table, pass through to fill-paragraph if no table."
14613 (let ((table-p (org-at-table-p))
14614 (table.el-p (org-at-table.el-p)))
14615 (cond ((and (equal (char-after (point-at-bol)) ?*)
14616 (save-excursion (goto-char (point-at-bol))
14617 (looking-at outline-regexp)))
14618 t) ; skip headlines
14619 (table.el-p t) ; skip table.el tables
14620 (table-p (org-table-align) t) ; align org-mode tables
14621 (t nil)))) ; call paragraph-fill
14623 ;; For reference, this is the default value of adaptive-fill-regexp
14624 ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
14626 (defun org-adaptive-fill-function ()
14627 "Return a fill prefix for org-mode files.
14628 In particular, this makes sure hanging paragraphs for hand-formatted lists
14630 (cond ((looking-at "#[ \t]+")
14632 ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
14634 (goto-char (match-end 0))
14635 (make-string (current-column) ?\ )))
14640 (defun org-toggle-fixed-width-section (arg)
14641 "Toggle the fixed-width export.
14642 If there is no active region, the QUOTE keyword at the current headline is
14643 inserted or removed. When present, it causes the text between this headline
14644 and the next to be exported as fixed-width text, and unmodified.
14645 If there is an active region, this command adds or removes a colon as the
14646 first character of this line. If the first character of a line is a colon,
14647 this line is also exported in fixed-width font."
14650 (regionp (org-region-active-p))
14651 (beg (if regionp (region-beginning) (point)))
14652 (end (if regionp (region-end)))
14653 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
14654 (case-fold-search nil)
14655 (re "[ \t]*\\(:\\)")
14660 (setq cc (current-column))
14661 (beginning-of-line 1)
14662 (setq off (looking-at re))
14663 (while (> nlines 0)
14664 (setq nlines (1- nlines))
14665 (beginning-of-line 1)
14668 (move-to-column cc t)
14671 ((and off (looking-at re))
14672 (replace-match "" t t nil 1))
14673 ((not off) (move-to-column cc t) (insert ":")))
14676 (org-back-to-heading)
14677 (if (looking-at (concat outline-regexp
14678 "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
14679 (replace-match "" t t nil 1)
14680 (if (looking-at outline-regexp)
14682 (goto-char (match-end 0))
14683 (insert org-quote-string " "))))))))
14685 ;;;; Functions extending outline functionality
14687 (defun org-beginning-of-line (&optional arg)
14688 "Go to the beginning of the current line. If that is invisible, continue
14689 to a visible line beginning. This makes the function of C-a more intuitive.
14690 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
14691 first attempt, and only move to after the tags when the cursor is already
14692 beyond the end of the headline."
14694 (let ((pos (point)))
14695 (beginning-of-line 1)
14699 (if (org-invisible-p)
14700 (while (and (not (bobp)) (org-invisible-p))
14702 (beginning-of-line 1))
14704 (when org-special-ctrl-a/e
14706 ((and (looking-at org-todo-line-regexp)
14707 (= (char-after (match-end 1)) ?\ ))
14709 (if (eq org-special-ctrl-a/e t)
14710 (cond ((> pos (match-beginning 3)) (match-beginning 3))
14711 ((= pos (point)) (match-beginning 3))
14713 (cond ((> pos (point)) (point))
14714 ((not (eq last-command this-command)) (point))
14715 (t (match-beginning 3))))))
14718 (if (eq org-special-ctrl-a/e t)
14719 (cond ((> pos (match-end 4)) (match-end 4))
14720 ((= pos (point)) (match-end 4))
14722 (cond ((> pos (point)) (point))
14723 ((not (eq last-command this-command)) (point))
14724 (t (match-end 4))))))))))
14726 (defun org-end-of-line (&optional arg)
14727 "Go to the end of the line.
14728 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
14729 first attempt, and only move to after the tags when the cursor is already
14730 beyond the end of the headline."
14732 (if (or (not org-special-ctrl-a/e)
14733 (not (org-on-heading-p)))
14735 (let ((pos (point)))
14736 (beginning-of-line 1)
14737 (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
14738 (if (eq org-special-ctrl-a/e t)
14739 (if (or (< pos (match-beginning 1))
14740 (= pos (match-end 0)))
14741 (goto-char (match-beginning 1))
14742 (goto-char (match-end 0)))
14743 (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
14744 (goto-char (match-end 0))
14745 (goto-char (match-beginning 1))))
14746 (end-of-line arg)))))
14748 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
14749 (define-key org-mode-map "\C-e" 'org-end-of-line)
14751 (defun org-kill-line (&optional arg)
14752 "Kill line, to tags or end of line."
14755 ((or (not org-special-ctrl-k)
14757 (not (org-on-heading-p)))
14758 (call-interactively 'kill-line))
14759 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
14760 (kill-region (point) (match-beginning 1))
14761 (org-set-tags nil t))
14762 (t (kill-region (point) (point-at-eol)))))
14764 (define-key org-mode-map "\C-k" 'org-kill-line)
14766 (defun org-invisible-p ()
14767 "Check if point is at a character currently not visible."
14768 ;; Early versions of noutline don't have `outline-invisible-p'.
14769 (if (fboundp 'outline-invisible-p)
14770 (outline-invisible-p)
14771 (get-char-property (point) 'invisible)))
14773 (defun org-invisible-p2 ()
14774 "Check if point is at a character currently not visible."
14776 (if (and (eolp) (not (bobp))) (backward-char 1))
14777 ;; Early versions of noutline don't have `outline-invisible-p'.
14778 (if (fboundp 'outline-invisible-p)
14779 (outline-invisible-p)
14780 (get-char-property (point) 'invisible))))
14782 (defalias 'org-back-to-heading 'outline-back-to-heading)
14783 (defalias 'org-on-heading-p 'outline-on-heading-p)
14784 (defalias 'org-at-heading-p 'outline-on-heading-p)
14785 (defun org-at-heading-or-item-p ()
14786 (or (org-on-heading-p) (org-at-item-p)))
14788 (defun org-on-target-p ()
14789 (or (org-in-regexp org-radio-target-regexp)
14790 (org-in-regexp org-target-regexp)))
14792 (defun org-up-heading-all (arg)
14793 "Move to the heading line of which the present line is a subheading.
14794 This function considers both visible and invisible heading lines.
14795 With argument, move up ARG levels."
14796 (if (fboundp 'outline-up-heading-all)
14797 (outline-up-heading-all arg) ; emacs 21 version of outline.el
14798 (outline-up-heading arg t))) ; emacs 22 version of outline.el
14800 (defun org-up-heading-safe ()
14801 "Move to the heading line of which the present line is a subheading.
14802 This version will not throw an error. It will return the level of the
14803 headline found, or nil if no higher level is found."
14804 (let ((pos (point)) start-level level
14805 (re (concat "^" outline-regexp)))
14807 (outline-back-to-heading t)
14808 (setq start-level (funcall outline-level))
14809 (if (equal start-level 1) (throw 'exit nil))
14810 (while (re-search-backward re nil t)
14811 (setq level (funcall outline-level))
14812 (if (< level start-level) (throw 'exit level)))
14815 (defun org-first-sibling-p ()
14816 "Is this heading the first child of its parents?"
14818 (let ((re (concat "^" outline-regexp))
14820 (unless (org-at-heading-p t)
14821 (error "Not at a heading"))
14822 (setq level (funcall outline-level))
14824 (if (not (re-search-backward re nil t))
14826 (setq l (funcall outline-level))
14829 (defun org-goto-sibling (&optional previous)
14830 "Goto the next sibling, even if it is invisible.
14831 When PREVIOUS is set, go to the previous sibling instead. Returns t
14832 when a sibling was found. When none is found, return nil and don't
14834 (let ((fun (if previous 're-search-backward 're-search-forward))
14836 (re (concat "^" outline-regexp))
14838 (when (condition-case nil (org-back-to-heading t) (error nil))
14839 (setq level (funcall outline-level))
14841 (or previous (forward-char 1))
14842 (while (funcall fun re nil t)
14843 (setq l (funcall outline-level))
14844 (when (< l level) (goto-char pos) (throw 'exit nil))
14845 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
14849 (defun org-show-siblings ()
14850 "Show all siblings of the current headline."
14852 (while (org-goto-sibling) (org-flag-heading nil)))
14854 (while (org-goto-sibling 'previous)
14855 (org-flag-heading nil))))
14857 (defun org-show-hidden-entry ()
14858 "Show an entry where even the heading is hidden."
14862 (defun org-flag-heading (flag &optional entry)
14863 "Flag the current heading. FLAG non-nil means make invisible.
14864 When ENTRY is non-nil, show the entire entry."
14866 (org-back-to-heading t)
14867 ;; Check if we should show the entire entry
14872 (and (outline-next-heading)
14873 (org-flag-heading nil))))
14874 (outline-flag-region (max (point-min) (1- (point)))
14875 (save-excursion (outline-end-of-heading) (point))
14878 (defun org-end-of-subtree (&optional invisible-OK to-heading)
14879 ;; This is an exact copy of the original function, but it uses
14880 ;; `org-back-to-heading', to make it work also in invisible
14881 ;; trees. And is uses an invisible-OK argument.
14882 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
14883 (org-back-to-heading invisible-OK)
14885 (level (funcall outline-level)))
14886 (while (and (not (eobp))
14887 (or first (> (funcall outline-level) level)))
14889 (outline-next-heading))
14891 (if (memq (preceding-char) '(?\n ?\^M))
14893 ;; Go to end of line before heading
14895 (if (memq (preceding-char) '(?\n ?\^M))
14896 ;; leave blank line before heading
14897 (forward-char -1))))))
14900 (defun org-show-subtree ()
14901 "Show everything after this heading at deeper levels."
14902 (outline-flag-region
14905 (outline-end-of-subtree) (outline-next-heading) (point))
14908 (defun org-show-entry ()
14909 "Show the body directly following this heading.
14910 Show the heading too, if it is currently invisible."
14913 (condition-case nil
14915 (org-back-to-heading t)
14916 (outline-flag-region
14917 (max (point-min) (1- (point)))
14920 (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
14921 (or (match-beginning 1) (point-max)))
14925 (defun org-make-options-regexp (kwds)
14926 "Make a regular expression for keyword lines."
14930 (mapconcat 'regexp-quote kwds "\\|")
14934 ;; Make isearch reveal the necessary context
14935 (defun org-isearch-end ()
14936 "Reveal context after isearch exits."
14937 (when isearch-success ; only if search was successful
14938 (if (featurep 'xemacs)
14939 ;; Under XEmacs, the hook is run in the correct place,
14940 ;; we directly show the context.
14941 (org-show-context 'isearch)
14942 ;; In Emacs the hook runs *before* restoring the overlays.
14943 ;; So we have to use a one-time post-command-hook to do this.
14944 ;; (Emacs 22 has a special variable, see function `org-mode')
14945 (unless (and (boundp 'isearch-mode-end-hook-quit)
14946 isearch-mode-end-hook-quit)
14947 ;; Only when the isearch was not quitted.
14948 (org-add-hook 'post-command-hook 'org-isearch-post-command
14949 'append 'local)))))
14951 (defun org-isearch-post-command ()
14952 "Remove self from hook, and show context."
14953 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
14954 (org-show-context 'isearch))
14957 ;;;; Integration with and fixes for other packages
14961 (defvar org-imenu-markers nil
14962 "All markers currently used by Imenu.")
14963 (make-variable-buffer-local 'org-imenu-markers)
14965 (defun org-imenu-new-marker (&optional pos)
14966 "Return a new marker for use by Imenu, and remember the marker."
14967 (let ((m (make-marker)))
14968 (move-marker m (or pos (point)))
14969 (push m org-imenu-markers)
14972 (defun org-imenu-get-tree ()
14973 "Produce the index for Imenu."
14974 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
14975 (setq org-imenu-markers nil)
14976 (let* ((n org-imenu-depth)
14977 (re (concat "^" outline-regexp))
14978 (subs (make-vector (1+ n) nil))
14984 (goto-char (point-max))
14985 (while (re-search-backward re nil t)
14986 (setq level (org-reduced-level (funcall outline-level)))
14988 (looking-at org-complex-heading-regexp)
14989 (setq head (org-match-string-no-properties 4)
14990 m (org-imenu-new-marker))
14991 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
14992 (if (>= level last-level)
14993 (push (cons head m) (aref subs level))
14994 (push (cons head (aref subs (1+ level))) (aref subs level))
14995 (loop for i from (1+ level) to n do (aset subs i nil)))
14996 (setq last-level level)))))
14999 (eval-after-load "imenu"
15001 (add-hook 'imenu-after-jump-hook
15002 (lambda () (org-show-context 'org-goto)))))
15004 ;; Speedbar support
15006 (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
15007 "Overlay marking the agenda restriction line in speedbar.")
15008 (org-overlay-put org-speedbar-restriction-lock-overlay
15009 'face 'org-agenda-restriction-lock)
15010 (org-overlay-put org-speedbar-restriction-lock-overlay
15011 'help-echo "Agendas are currently limited to this item.")
15012 (org-detach-overlay org-speedbar-restriction-lock-overlay)
15014 (defun org-speedbar-set-agenda-restriction ()
15015 "Restrict future agenda commands to the location at point in speedbar.
15016 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
15018 (require 'org-agenda)
15019 (let (p m tp np dir txt w)
15021 ((setq p (text-property-any (point-at-bol) (point-at-eol)
15023 (setq m (get-text-property p 'org-imenu-marker))
15026 (set-buffer (marker-buffer m))
15028 (org-agenda-set-restriction-lock 'subtree))))
15029 ((setq p (text-property-any (point-at-bol) (point-at-eol)
15030 'speedbar-function 'speedbar-find-file))
15031 (setq tp (previous-single-property-change
15032 (1+ p) 'speedbar-function)
15033 np (next-single-property-change
15034 tp 'speedbar-function)
15035 dir (speedbar-line-directory)
15036 txt (buffer-substring-no-properties (or tp (point-min))
15037 (or np (point-max))))
15040 (set-buffer (find-file-noselect
15041 (let ((default-directory dir))
15042 (expand-file-name txt))))
15043 (unless (org-mode-p)
15044 (error "Cannot restrict to non-Org-mode file"))
15045 (org-agenda-set-restriction-lock 'file))))
15046 (t (error "Don't know how to restrict Org-mode's agenda")))
15047 (org-move-overlay org-speedbar-restriction-lock-overlay
15048 (point-at-bol) (point-at-eol))
15049 (setq current-prefix-arg nil)
15050 (org-agenda-maybe-redo)))
15052 (eval-after-load "speedbar"
15054 (speedbar-add-supported-extension ".org")
15055 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
15056 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
15057 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
15058 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
15059 (add-hook 'speedbar-visiting-tag-hook
15060 (lambda () (org-show-context 'org-goto)))))
15063 ;;; Fixes and Hacks for problems with other packages
15065 ;; Make flyspell not check words in links, to not mess up our keymap
15066 (defun org-mode-flyspell-verify ()
15067 "Don't let flyspell put overlays at active buttons."
15068 (not (get-text-property (point) 'keymap)))
15070 ;; Make `bookmark-jump' show the jump location if it was hidden.
15071 (eval-after-load "bookmark"
15072 '(if (boundp 'bookmark-after-jump-hook)
15073 ;; We can use the hook
15074 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
15075 ;; Hook not available, use advice
15076 (defadvice bookmark-jump (after org-make-visible activate)
15077 "Make the position visible."
15078 (org-bookmark-jump-unhide))))
15080 (defun org-bookmark-jump-unhide ()
15081 "Unhide the current position, to show the bookmark location."
15083 (or (org-invisible-p)
15084 (save-excursion (goto-char (max (point-min) (1- (point))))
15085 (org-invisible-p)))
15086 (org-show-context 'bookmark-jump)))
15088 ;; Make session.el ignore our circular variable
15089 (eval-after-load "session"
15090 '(add-to-list 'session-globals-exclude 'org-mark-ring))
15092 ;;;; Experimental code
15094 (defun org-closed-in-range ()
15095 "Sparse tree of items closed in a certain time range.
15096 Still experimental, may disappear in the future."
15098 ;; Get the time interval from the user.
15099 (let* ((time1 (time-to-seconds
15100 (org-read-date nil 'to-time nil "Starting date: ")))
15101 (time2 (time-to-seconds
15102 (org-read-date nil 'to-time nil "End date:")))
15103 ;; callback function
15104 (callback (lambda ()
15107 (apply 'encode-time
15108 (org-parse-time-string
15109 (match-string 1))))))
15110 ;; check if time in interval
15111 (and (>= time time1) (<= time time2))))))
15112 ;; make tree, check each match with the callback
15113 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
15120 (run-hooks 'org-load-hook)
15122 ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
15123 ;;; org.el ends here
15125 (defun org-plist-delete (plist property)
15126 "Delete PROPERTY from PLIST.
15127 This is in contrast to merely setting it to 0."
15130 (if (not (eq property (car plist)))
15131 (setq p (plist-put p (car plist) (nth 1 plist))))
15132 (setq plist (cddr plist)))