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 ;;;; Require other packages
73 ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
74 ;; the file noutline.el being loaded.
75 (if (featurep 'xemacs
) (condition-case nil
(require 'noutline
)))
76 ;; We require noutline, which might be provided in outline.el
77 (require 'outline
) (require 'noutline
)
78 ;; Other stuff we need.
80 (unless (fboundp 'time-subtract
) (defalias 'time-subtract
'subtract-time
))
83 ;;;; Customization variables
87 (defconst org-version
"5.21+"
88 "The version number of the file org.el.")
90 (defun org-version (&optional here
)
91 "Show the org-mode version in the echo area.
92 With prefix arg HERE, insert it at point."
94 (let ((version (format "Org-mode version %s" org-version
)))
99 ;;; Compatibility constants
100 (defconst org-xemacs-p
(featurep 'xemacs
)) ; not used by org.el itself
101 (defconst org-format-transports-properties-p
103 (add-text-properties 0 1 '(test t
) x
)
104 (get-text-property 0 'test
(format "%s" x
)))
105 "Does format transport text properties?")
107 (defmacro org-bound-and-true-p
(var)
108 "Return the value of symbol VAR if it is bound, else nil."
109 `(and (boundp (quote ,var
)) ,var
))
111 (defmacro org-unmodified
(&rest body
)
112 "Execute body without changing `buffer-modified-p'."
113 `(set-buffer-modified-p
114 (prog1 (buffer-modified-p) ,@body
)))
117 "Replace posix classes in regular expression."
118 (if (featurep 'xemacs
)
121 (while (string-match "\\[:alnum:\\]" ss
)
122 (setq ss
(replace-match "a-zA-Z0-9" t t ss
)))
123 (while (string-match "\\[:alpha:\\]" ss
)
124 (setq ss
(replace-match "a-zA-Z" t t ss
)))
128 (defmacro org-preserve-lc
(&rest body
)
129 `(let ((_line (org-current-line))
130 (_col (current-column)))
134 (move-to-column _col
))))
136 (defmacro org-without-partial-completion
(&rest body
)
137 `(let ((pc-mode (and (boundp 'partial-completion-mode
)
138 partial-completion-mode
)))
141 (if pc-mode
(partial-completion-mode -
1))
143 (if pc-mode
(partial-completion-mode 1)))))
145 ;;; The custom variables
148 "Outline-based notes management and organizer."
154 ;; FIXME: Needs a separate group...
155 (defcustom org-completion-fallback-command
'hippie-expand
156 "The expansion command called by \\[org-complete] in normal context.
157 Normal means, no org-mode-specific context."
161 (defgroup org-startup nil
162 "Options concerning startup of Org-mode."
166 (defcustom org-startup-folded t
167 "Non-nil means, entering Org-mode will switch to OVERVIEW.
168 This can also be configured on a per-file basis by adding one of
169 the following lines anywhere in the buffer:
176 (const :tag
"nofold: show all" nil
)
177 (const :tag
"fold: overview" t
)
178 (const :tag
"content: all headlines" content
)))
180 (defcustom org-startup-truncated t
181 "Non-nil means, entering Org-mode will set `truncate-lines'.
182 This is useful since some lines containing links can be very long and
183 uninteresting. Also tables look terrible when wrapped."
187 (defcustom org-startup-align-all-tables nil
188 "Non-nil means, align all tables when visiting a file.
189 This is useful when the column width in tables is forced with <N> cookies
190 in table fields. Such tables will look correct only after the first re-align.
191 This can also be configured on a per-file basis by adding one of
192 the following lines anywhere in the buffer:
198 (defcustom org-insert-mode-line-in-empty-file nil
199 "Non-nil means insert the first line setting Org-mode in empty files.
200 When the function `org-mode' is called interactively in an empty file, this
201 normally means that the file name does not automatically trigger Org-mode.
202 To ensure that the file will always be in Org-mode in the future, a
203 line enforcing Org-mode will be inserted into the buffer, if this option
208 (defcustom org-replace-disputed-keys nil
209 "Non-nil means use alternative key bindings for some keys.
210 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
211 These keys are also used by other packages like `CUA-mode' or `windmove.el'.
212 If you want to use Org-mode together with one of these other modes,
213 or more generally if you would like to move some Org-mode commands to
214 other keys, set this variable and configure the keys with the variable
217 This option is only relevant at load-time of Org-mode, and must be set
218 *before* org.el is loaded. Changing it requires a restart of Emacs to
223 (if (fboundp 'defvaralias
)
224 (defvaralias 'org-CUA-compatible
'org-replace-disputed-keys
))
226 (defcustom org-disputed-keys
227 '(([(shift up
)] .
[(meta p
)])
228 ([(shift down
)] .
[(meta n
)])
229 ([(shift left
)] .
[(meta -
)])
230 ([(shift right
)] .
[(meta +)])
231 ([(control shift right
)] .
[(meta shift
+)])
232 ([(control shift left
)] .
[(meta shift -
)]))
233 "Keys for which Org-mode and other modes compete.
234 This is an alist, cars are the default keys, second element specifies
235 the alternative to use when `org-replace-disputed-keys' is t.
237 Keys can be specified in any syntax supported by `define-key'.
238 The value of this option takes effect only at Org-mode's startup,
239 therefore you'll have to restart Emacs to apply it after changing."
244 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
245 Or return the original if not disputed."
246 (if org-replace-disputed-keys
247 (let* ((nkey (key-description key
))
248 (x (org-find-if (lambda (x)
249 (equal (key-description (car x
)) nkey
))
254 (defun org-find-if (predicate seq
)
257 (if (funcall predicate
(car seq
))
258 (throw 'exit
(car seq
))
261 (defun org-defkey (keymap key def
)
262 "Define a key, possibly translated, as returned by `org-key'."
263 (define-key keymap
(org-key key
) def
))
265 (defcustom org-ellipsis nil
266 "The ellipsis to use in the Org-mode outline.
267 When nil, just use the standard three dots. When a string, use that instead,
268 When a face, use the standart 3 dots, but with the specified face.
269 The change affects only Org-mode (which will then use its own display table).
270 Changing this requires executing `M-x org-mode' in a buffer to become
273 :type
'(choice (const :tag
"Default" nil
)
274 (face :tag
"Face" :value org-warning
)
275 (string :tag
"String" :value
"...#")))
277 (defvar org-display-table nil
278 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
280 (defgroup org-keywords nil
281 "Keywords in Org-mode."
285 (defcustom org-deadline-string
"DEADLINE:"
286 "String to mark deadline entries.
287 A deadline is this string, followed by a time stamp. Should be a word,
288 terminated by a colon. You can insert a schedule keyword and
289 a timestamp with \\[org-deadline].
290 Changes become only effective after restarting Emacs."
294 (defcustom org-scheduled-string
"SCHEDULED:"
295 "String to mark scheduled TODO entries.
296 A schedule is this string, followed by a time stamp. Should be a word,
297 terminated by a colon. You can insert a schedule keyword and
298 a timestamp with \\[org-schedule].
299 Changes become only effective after restarting Emacs."
303 (defcustom org-closed-string
"CLOSED:"
304 "String used as the prefix for timestamps logging closing a TODO entry."
308 (defcustom org-clock-string
"CLOCK:"
309 "String used as prefix for timestamps clocking work hours on an item."
313 (defcustom org-comment-string
"COMMENT"
314 "Entries starting with this keyword will never be exported.
315 An entry can be toggled between COMMENT and normal with
316 \\[org-toggle-comment].
317 Changes become only effective after restarting Emacs."
321 (defcustom org-quote-string
"QUOTE"
322 "Entries starting with this keyword will be exported in fixed-width font.
323 Quoting applies only to the text in the entry following the headline, and does
324 not extend beyond the next headline, even if that is lower level.
325 An entry can be toggled between QUOTE and normal with
326 \\[org-toggle-fixed-width-section]."
330 (defconst org-repeat-re
331 ; (concat "\\(?:\\<\\(?:" org-scheduled-string "\\|" org-deadline-string "\\)"
332 ; " +<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\)\\(\\+[0-9]+[dwmy]\\)")
333 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\(\\+[0-9]+[dwmy]\\)"
334 "Regular expression for specifying repeated events.
335 After a match, group 1 contains the repeat expression.")
337 (defgroup org-structure nil
338 "Options concerning the general structure of Org-mode files."
342 (defgroup org-reveal-location nil
343 "Options about how to make context of a location visible."
344 :tag
"Org Reveal Location"
345 :group
'org-structure
)
347 (defconst org-context-choice
349 (const :tag
"Always" t
)
350 (const :tag
"Never" nil
)
351 (repeat :greedy t
:tag
"Individual contexts"
353 (choice :tag
"Context"
360 (const bookmark-jump
)
364 "Contexts for the reveal options.")
366 (defcustom org-show-hierarchy-above
'((default . t
))
367 "Non-nil means, show full hierarchy when revealing a location.
368 Org-mode often shows locations in an org-mode file which might have
369 been invisible before. When this is set, the hierarchy of headings
370 above the exposed location is shown.
371 Turning this off for example for sparse trees makes them very compact.
372 Instead of t, this can also be an alist specifying this option for different
373 contexts. Valid contexts are
374 agenda when exposing an entry from the agenda
375 org-goto when using the command `org-goto' on key C-c C-j
376 occur-tree when using the command `org-occur' on key C-c /
377 tags-tree when constructing a sparse tree based on tags matches
378 link-search when exposing search matches associated with a link
379 mark-goto when exposing the jump goal of a mark
380 bookmark-jump when exposing a bookmark location
381 isearch when exiting from an incremental search
382 default default for all contexts not set explicitly"
383 :group
'org-reveal-location
384 :type org-context-choice
)
386 (defcustom org-show-following-heading
'((default . nil
))
387 "Non-nil means, show following heading when revealing a location.
388 Org-mode often shows locations in an org-mode file which might have
389 been invisible before. When this is set, the heading following the
391 Turning this off for example for sparse trees makes them very compact,
392 but makes it harder to edit the location of the match. In such a case,
393 use the command \\[org-reveal] to show more context.
394 Instead of t, this can also be an alist specifying this option for different
395 contexts. See `org-show-hierarchy-above' for valid contexts."
396 :group
'org-reveal-location
397 :type org-context-choice
)
399 (defcustom org-show-siblings
'((default . nil
) (isearch t
))
400 "Non-nil means, show all sibling heading when revealing a location.
401 Org-mode often shows locations in an org-mode file which might have
402 been invisible before. When this is set, the sibling of the current entry
403 heading are all made visible. If `org-show-hierarchy-above' is t,
404 the same happens on each level of the hierarchy above the current entry.
406 By default this is on for the isearch context, off for all other contexts.
407 Turning this off for example for sparse trees makes them very compact,
408 but makes it harder to edit the location of the match. In such a case,
409 use the command \\[org-reveal] to show more context.
410 Instead of t, this can also be an alist specifying this option for different
411 contexts. See `org-show-hierarchy-above' for valid contexts."
412 :group
'org-reveal-location
413 :type org-context-choice
)
415 (defcustom org-show-entry-below
'((default . nil
))
416 "Non-nil means, show the entry below a headline when revealing a location.
417 Org-mode often shows locations in an org-mode file which might have
418 been invisible before. When this is set, the text below the headline that is
419 exposed is also shown.
421 By default this is off for all contexts.
422 Instead of t, this can also be an alist specifying this option for different
423 contexts. See `org-show-hierarchy-above' for valid contexts."
424 :group
'org-reveal-location
425 :type org-context-choice
)
427 (defgroup org-cycle nil
428 "Options concerning visibility cycling in Org-mode."
430 :group
'org-structure
)
432 (defcustom org-drawers
'("PROPERTIES" "CLOCK")
433 "Names of drawers. Drawers are not opened by cycling on the headline above.
434 Drawers only open with a TAB on the drawer line itself. A drawer looks like
439 The drawer \"PROPERTIES\" is special for capturing properties through
442 Drawers can be defined on the per-file basis with a line like:
444 #+DRAWERS: HIDDEN STATE PROPERTIES"
445 :group
'org-structure
446 :type
'(repeat (string :tag
"Drawer Name")))
448 (defcustom org-cycle-global-at-bob nil
449 "Cycle globally if cursor is at beginning of buffer and not at a headline.
450 This makes it possible to do global cycling without having to use S-TAB or
451 C-u TAB. For this special case to work, the first line of the buffer
452 must not be a headline - it may be empty ot some other text. When used in
453 this way, `org-cycle-hook' is disables temporarily, to make sure the
454 cursor stays at the beginning of the buffer.
455 When this option is nil, don't do anything special at the beginning
460 (defcustom org-cycle-emulate-tab t
461 "Where should `org-cycle' emulate TAB.
463 white Only in completely white lines
464 whitestart Only at the beginning of lines, before the first non-white char
465 t Everywhere except in headlines
466 exc-hl-bol Everywhere except at the start of a headline
467 If TAB is used in a place where it does not emulate TAB, the current subtree
468 visibility is cycled."
470 :type
'(choice (const :tag
"Never" nil
)
471 (const :tag
"Only in completely white lines" white
)
472 (const :tag
"Before first char in a line" whitestart
)
473 (const :tag
"Everywhere except in headlines" t
)
474 (const :tag
"Everywhere except at bol in headlines" exc-hl-bol
)
477 (defcustom org-cycle-separator-lines
2
478 "Number of empty lines needed to keep an empty line between collapsed trees.
479 If you leave an empty line between the end of a subtree and the following
480 headline, this empty line is hidden when the subtree is folded.
481 Org-mode will leave (exactly) one empty line visible if the number of
482 empty lines is equal or larger to the number given in this variable.
483 So the default 2 means, at least 2 empty lines after the end of a subtree
484 are needed to produce free space between a collapsed subtree and the
487 Special case: when 0, never leave empty lines in collapsed view."
491 (defcustom org-cycle-hook
'(org-cycle-hide-archived-subtrees
492 org-cycle-hide-drawers
493 org-cycle-show-empty-lines
494 org-optimize-window-after-visibility-change
)
495 "Hook that is run after `org-cycle' has changed the buffer visibility.
496 The function(s) in this hook must accept a single argument which indicates
497 the new state that was set by the most recent `org-cycle' command. The
498 argument is a symbol. After a global state change, it can have the values
499 `overview', `content', or `all'. After a local state change, it can have
500 the values `folded', `children', or `subtree'."
504 (defgroup org-edit-structure nil
505 "Options concerning structure editing in Org-mode."
506 :tag
"Org Edit Structure"
507 :group
'org-structure
)
509 (defcustom org-special-ctrl-a
/e nil
510 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
511 When t, `C-a' will bring back the cursor to the beginning of the
512 headline text, i.e. after the stars and after a possible TODO keyword.
513 In an item, this will be the position after the bullet.
514 When the cursor is already at that position, another `C-a' will bring
515 it to the beginning of the line.
516 `C-e' will jump to the end of the headline, ignoring the presence of tags
517 in the headline. A second `C-e' will then jump to the true end of the
518 line, after any tags.
519 When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
520 and only a directly following, identical keypress will bring the cursor
521 to the special positions."
522 :group
'org-edit-structure
524 (const :tag
"off" nil
)
525 (const :tag
"after bullet first" t
)
526 (const :tag
"border first" reversed
)))
528 (if (fboundp 'defvaralias
)
529 (defvaralias 'org-special-ctrl-a
'org-special-ctrl-a
/e
))
531 (defcustom org-special-ctrl-k nil
532 "Non-nil means `C-k' will behave specially in headlines.
533 When nil, `C-k' will call the default `kill-line' command.
534 When t, the following will happen while the cursor is in the headline:
536 - When the cursor is at the beginning of a headline, kill the entire
537 line and possible the folded subtree below the line.
538 - When in the middle of the headline text, kill the headline up to the tags.
539 - When after the headline text, kill the tags."
540 :group
'org-edit-structure
543 (defcustom org-odd-levels-only nil
544 "Non-nil means, skip even levels and only use odd levels for the outline.
545 This has the effect that two stars are being added/taken away in
546 promotion/demotion commands. It also influences how levels are
547 handled by the exporters.
548 Changing it requires restart of `font-lock-mode' to become effective
549 for fontification also in regions already fontified.
550 You may also set this on a per-file basis by adding one of the following
555 :group
'org-edit-structure
556 :group
'org-font-lock
559 (defcustom org-adapt-indentation t
560 "Non-nil means, adapt indentation when promoting and demoting.
561 When this is set and the *entire* text in an entry is indented, the
562 indentation is increased by one space in a demotion command, and
563 decreased by one in a promotion command. If any line in the entry
564 body starts at column 0, indentation is not changed at all."
565 :group
'org-edit-structure
568 (defcustom org-blank-before-new-entry
'((heading . nil
)
569 (plain-list-item . nil
))
570 "Should `org-insert-heading' leave a blank line before new heading/item?
571 The value is an alist, with `heading' and `plain-list-item' as car,
572 and a boolean flag as cdr."
573 :group
'org-edit-structure
575 (cons (const heading
) (boolean))
576 (cons (const plain-list-item
) (boolean))))
578 (defcustom org-insert-heading-hook nil
579 "Hook being run after inserting a new heading."
580 :group
'org-edit-structure
583 (defcustom org-enable-fixed-width-editor t
584 "Non-nil means, lines starting with \":\" are treated as fixed-width.
585 This currently only means, they are never auto-wrapped.
586 When nil, such lines will be treated like ordinary lines.
587 See also the QUOTE keyword."
588 :group
'org-edit-structure
591 (defcustom org-goto-auto-isearch t
592 "Non-nil means, typing characters in org-goto starts incremental search."
593 :group
'org-edit-structure
596 (defgroup org-sparse-trees nil
597 "Options concerning sparse trees in Org-mode."
598 :tag
"Org Sparse Trees"
599 :group
'org-structure
)
601 (defcustom org-highlight-sparse-tree-matches t
602 "Non-nil means, highlight all matches that define a sparse tree.
603 The highlights will automatically disappear the next time the buffer is
604 changed by an edit command."
605 :group
'org-sparse-trees
608 (defcustom org-remove-highlights-with-change t
609 "Non-nil means, any change to the buffer will remove temporary highlights.
610 Such highlights are created by `org-occur' and `org-clock-display'.
611 When nil, `C-c C-c needs to be used to get rid of the highlights.
612 The highlights created by `org-preview-latex-fragment' always need
613 `C-c C-c' to be removed."
614 :group
'org-sparse-trees
619 (defcustom org-occur-hook
'(org-first-headline-recenter)
620 "Hook that is run after `org-occur' has constructed a sparse tree.
621 This can be used to recenter the window to show as much of the structure
623 :group
'org-sparse-trees
626 (defgroup org-plain-lists nil
627 "Options concerning plain lists in Org-mode."
628 :tag
"Org Plain lists"
629 :group
'org-structure
)
631 (defcustom org-cycle-include-plain-lists nil
632 "Non-nil means, include plain lists into visibility cycling.
633 This means that during cycling, plain list items will *temporarily* be
634 interpreted as outline headlines with a level given by 1000+i where i is the
635 indentation of the bullet. In all other operations, plain list items are
636 not seen as headlines. For example, you cannot assign a TODO keyword to
638 :group
'org-plain-lists
641 (defcustom org-plain-list-ordered-item-terminator t
642 "The character that makes a line with leading number an ordered list item.
643 Valid values are ?. and ?\). To get both terminators, use t. While
644 ?. may look nicer, it creates the danger that a line with leading
645 number may be incorrectly interpreted as an item. ?\) therefore is
647 :group
'org-plain-lists
648 :type
'(choice (const :tag
"dot like in \"2.\"" ?.
)
649 (const :tag
"paren like in \"2)\"" ?\
))
650 (const :tab
"both" t
)))
652 (defcustom org-auto-renumber-ordered-lists t
653 "Non-nil means, automatically renumber ordered plain lists.
654 Renumbering happens when the sequence have been changed with
655 \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
656 use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
657 :group
'org-plain-lists
660 (defcustom org-provide-checkbox-statistics t
661 "Non-nil means, update checkbox statistics after insert and toggle.
662 When this is set, checkbox statistics is updated each time you either insert
663 a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
664 with \\[org-ctrl-c-ctrl-c\\]."
665 :group
'org-plain-lists
668 (defgroup org-archive nil
669 "Options concerning archiving in Org-mode."
671 :group
'org-structure
)
673 (defcustom org-archive-tag
"ARCHIVE"
674 "The tag that marks a subtree as archived.
675 An archived subtree does not open during visibility cycling, and does
676 not contribute to the agenda listings.
677 After changing this, font-lock must be restarted in the relevant buffers to
678 get the proper fontification."
683 (defcustom org-agenda-skip-archived-trees t
684 "Non-nil means, the agenda will skip any items located in archived trees.
685 An archived tree is a tree marked with the tag ARCHIVE."
687 :group
'org-agenda-skip
690 (defcustom org-cycle-open-archived-trees nil
691 "Non-nil means, `org-cycle' will open archived trees.
692 An archived tree is a tree marked with the tag ARCHIVE.
693 When nil, archived trees will stay folded. You can still open them with
694 normal outline commands like `show-all', but not with the cycling commands."
699 (defcustom org-sparse-tree-open-archived-trees nil
700 "Non-nil means sparse tree construction shows matches in archived trees.
701 When nil, matches in these trees are highlighted, but the trees are kept in
704 :group
'org-sparse-trees
707 (defcustom org-archive-location
"%s_archive::"
708 "The location where subtrees should be archived.
709 This string consists of two parts, separated by a double-colon.
711 The first part is a file name - when omitted, archiving happens in the same
712 file. %s will be replaced by the current file name (without directory part).
713 Archiving to a different file is useful to keep archived entries from
714 contributing to the Org-mode Agenda.
716 The part after the double colon is a headline. The archived entries will be
717 filed under that headline. When omitted, the subtrees are simply filed away
718 at the end of the file, as top-level entries.
720 Here are a few examples:
722 If the current file is Projects.org, archive in file
723 Projects.org_archive, as top-level trees. This is the default.
725 \"::* Archived Tasks\"
726 Archive in the current file, under the top-level headline
727 \"* Archived Tasks\".
729 \"~/org/archive.org::\"
730 Archive in file ~/org/archive.org (absolute path), as top-level trees.
732 \"basement::** Finished Tasks\"
733 Archive in file ./basement (relative path), as level 3 trees
734 below the level 2 heading \"** Finished Tasks\".
736 You may set this option on a per-file basis by adding to the buffer a
739 #+ARCHIVE: basement::** Finished Tasks"
743 (defcustom org-archive-mark-done t
744 "Non-nil means, mark entries as DONE when they are moved to the archive file.
745 This can be a string to set the keyword to use. When t, Org-mode will
746 use the first keyword in its list that means done."
749 (const :tag
"No" nil
)
751 (string :tag
"Use this keyword")))
753 (defcustom org-archive-stamp-time t
754 "Non-nil means, add a time stamp to entries moved to an archive file.
755 This variable is obsolete and has no effect anymore, instead add ot remove
756 `time' from the variablle `org-archive-save-context-info'."
760 (defcustom org-archive-save-context-info
'(time file olpath category todo itags
)
761 "Parts of context info that should be stored as properties when archiving.
762 When a subtree is moved to an archive file, it looses information given by
763 context, like inherited tags, the category, and possibly also the TODO
764 state (depending on the variable `org-archive-mark-done').
765 This variable can be a list of any of the following symbols:
767 time The time of archiving.
768 file The file where the entry originates.
769 itags The local tags, in the headline of the subtree.
770 ltags The tags the subtree inherits from further up the hierarchy.
771 todo The pre-archive TODO state.
772 category The category, taken from file name or #+CATEGORY lines.
773 olpath The outline path to the item. These are all headlines above
774 the current item, separated by /, like a file path.
776 For each symbol present in the list, a property will be created in
777 the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
780 :type
'(set :greedy t
781 (const :tag
"Time" time
)
782 (const :tag
"File" file
)
783 (const :tag
"Category" category
)
784 (const :tag
"TODO state" todo
)
785 (const :tag
"TODO state" priority
)
786 (const :tag
"Inherited tags" itags
)
787 (const :tag
"Outline path" olpath
)
788 (const :tag
"Local tags" ltags
)))
790 (defgroup org-imenu-and-speedbar nil
791 "Options concerning imenu and speedbar in Org-mode."
792 :tag
"Org Imenu and Speedbar"
793 :group
'org-structure
)
795 (defcustom org-imenu-depth
2
796 "The maximum level for Imenu access to Org-mode headlines.
797 This also applied for speedbar access."
798 :group
'org-imenu-and-speedbar
801 (defgroup org-table nil
802 "Options concerning tables in Org-mode."
806 (defcustom org-enable-table-editor
'optimized
807 "Non-nil means, lines starting with \"|\" are handled by the table editor.
808 When nil, such lines will be treated like ordinary lines.
810 When equal to the symbol `optimized', the table editor will be optimized to
812 - Automatic overwrite mode in front of whitespace in table fields.
813 This makes the structure of the table stay in tact as long as the edited
814 field does not exceed the column width.
815 - Minimize the number of realigns. Normally, the table is aligned each time
816 TAB or RET are pressed to move to another field. With optimization this
817 happens only if changes to a field might have changed the column width.
818 Optimization requires replacing the functions `self-insert-command',
819 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
820 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
821 very good at guessing when a re-align will be necessary, but you can always
822 force one with \\[org-ctrl-c-ctrl-c].
824 If you would like to use the optimized version in Org-mode, but the
825 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
827 This variable can be used to turn on and off the table editor during a session,
828 but in order to toggle optimization, a restart is required.
830 See also the variable `org-table-auto-blank-field'."
833 (const :tag
"off" nil
)
835 (const :tag
"on, optimized" optimized
)))
837 (defcustom orgtbl-optimized
(eq org-enable-table-editor
'optimized
)
838 "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
839 In the optimized version, the table editor takes over all simple keys that
840 normally just insert a character. In tables, the characters are inserted
841 in a way to minimize disturbing the table structure (i.e. in overwrite mode
842 for empty fields). Outside tables, the correct binding of the keys is
845 The default for this option is t if the optimized version is also used in
846 Org-mode. See the variable `org-enable-table-editor' for details. Changing
847 this variable requires a restart of Emacs to become effective."
851 (defcustom orgtbl-radio-table-templates
852 '((latex-mode "% BEGIN RECEIVE ORGTBL %n
853 % END RECEIVE ORGTBL %n
855 #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
858 (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
859 @c END RECEIVE ORGTBL %n
861 #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
864 (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
865 <!-- END RECEIVE ORGTBL %n -->
867 #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
870 "Templates for radio tables in different major modes.
871 All occurrences of %n in a template will be replaced with the name of the
872 table, obtained by prompting the user."
875 (list (symbol :tag
"Major mode")
876 (string :tag
"Format"))))
878 (defgroup org-table-settings nil
879 "Settings for tables in Org-mode."
880 :tag
"Org Table Settings"
883 (defcustom org-table-default-size
"5x2"
884 "The default size for newly created tables, Columns x Rows."
885 :group
'org-table-settings
888 (defcustom org-table-number-regexp
889 "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$"
890 "Regular expression for recognizing numbers in table columns.
891 If a table column contains mostly numbers, it will be aligned to the
892 right. If not, it will be aligned to the left.
894 The default value of this option is a regular expression which allows
895 anything which looks remotely like a number as used in scientific
896 context. For example, all of the following will be considered a
898 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
900 Other options offered by the customize interface are more restrictive."
901 :group
'org-table-settings
903 (const :tag
"Positive Integers"
905 (const :tag
"Integers"
907 (const :tag
"Floating Point Numbers"
908 "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
909 (const :tag
"Floating Point Number or Integer"
910 "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
911 (const :tag
"Exponential, Floating point, Integer"
912 "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
913 (const :tag
"Very General Number-Like, including hex"
914 "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$")
915 (string :tag
"Regexp:")))
917 (defcustom org-table-number-fraction
0.5
918 "Fraction of numbers in a column required to make the column align right.
919 In a column all non-white fields are considered. If at least this
920 fraction of fields is matched by `org-table-number-fraction',
921 alignment to the right border applies."
922 :group
'org-table-settings
925 (defgroup org-table-editing nil
926 "Behavior of tables during editing in Org-mode."
927 :tag
"Org Table Editing"
930 (defcustom org-table-automatic-realign t
931 "Non-nil means, automatically re-align table when pressing TAB or RETURN.
932 When nil, aligning is only done with \\[org-table-align], or after column
934 :group
'org-table-editing
937 (defcustom org-table-auto-blank-field t
938 "Non-nil means, automatically blank table field when starting to type into it.
939 This only happens when typing immediately after a field motion
940 command (TAB, S-TAB or RET).
941 Only relevant when `org-enable-table-editor' is equal to `optimized'."
942 :group
'org-table-editing
945 (defcustom org-table-tab-jumps-over-hlines t
946 "Non-nil means, tab in the last column of a table with jump over a hline.
947 If a horizontal separator line is following the current line,
948 `org-table-next-field' can either create a new row before that line, or jump
949 over the line. When this option is nil, a new line will be created before
951 :group
'org-table-editing
954 (defcustom org-table-tab-recognizes-table.el t
955 "Non-nil means, TAB will automatically notice a table.el table.
956 When it sees such a table, it moves point into it and - if necessary -
957 calls `table-recognize-table'."
958 :group
'org-table-editing
961 (defgroup org-table-calculation nil
962 "Options concerning tables in Org-mode."
963 :tag
"Org Table Calculation"
966 (defcustom org-table-use-standard-references t
967 "Should org-mode work with table refrences like B3 instead of @3$2?
970 from accept as input, do not present for editing
971 t: accept as input and present for editing"
972 :group
'org-table-calculation
974 (const :tag
"Never, don't even check unser input for them" nil
)
975 (const :tag
"Always, both as user input, and when editing" t
)
976 (const :tag
"Convert user input, don't offer during editing" 'from
)))
978 (defcustom org-table-copy-increment t
979 "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
980 :group
'org-table-calculation
983 (defcustom org-calc-default-modes
984 '(calc-internal-prec 12
985 calc-float-format
(float 5)
988 calc-symbolic-mode nil
989 calc-date-format
(YYYY "-" MM
"-" DD
" " Www
(" " HH
":" mm
))
990 calc-display-working-message t
992 "List with Calc mode settings for use in calc-eval for table formulas.
993 The list must contain alternating symbols (Calc modes variables and values).
994 Don't remove any of the default settings, just change the values. Org-mode
995 relies on the variables to be present in the list."
996 :group
'org-table-calculation
999 (defcustom org-table-formula-evaluate-inline t
1000 "Non-nil means, TAB and RET evaluate a formula in current table field.
1001 If the current field starts with an equal sign, it is assumed to be a formula
1002 which should be evaluated as described in the manual and in the documentation
1003 string of the command `org-table-eval-formula'. This feature requires the
1005 When this variable is nil, formula calculation is only available through
1006 the command \\[org-table-eval-formula]."
1007 :group
'org-table-calculation
1010 (defcustom org-table-formula-use-constants t
1011 "Non-nil means, interpret constants in formulas in tables.
1012 A constant looks like `$c' or `$Grav' and will be replaced before evaluation
1013 by the value given in `org-table-formula-constants', or by a value obtained
1014 from the `constants.el' package."
1015 :group
'org-table-calculation
1018 (defcustom org-table-formula-constants nil
1019 "Alist with constant names and values, for use in table formulas.
1020 The car of each element is a name of a constant, without the `$' before it.
1021 The cdr is the value as a string. For example, if you'd like to use the
1022 speed of light in a formula, you would configure
1024 (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
1026 and then use it in an equation like `$1*$c'.
1028 Constants can also be defined on a per-file basis using a line like
1030 #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
1031 :group
'org-table-calculation
1033 (cons (string :tag
"name")
1034 (string :tag
"value"))))
1036 (defvar org-table-formula-constants-local nil
1037 "Local version of `org-table-formula-constants'.")
1038 (make-variable-buffer-local 'org-table-formula-constants-local
)
1040 (defcustom org-table-allow-automatic-line-recalculation t
1041 "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
1042 Automatically means, when TAB or RET or C-c C-c are pressed in the line."
1043 :group
'org-table-calculation
1046 (defgroup org-link nil
1047 "Options concerning links in Org-mode."
1051 (defvar org-link-abbrev-alist-local nil
1052 "Buffer-local version of `org-link-abbrev-alist', which see.
1053 The value of this is taken from the #+LINK lines.")
1054 (make-variable-buffer-local 'org-link-abbrev-alist-local
)
1056 (defcustom org-link-abbrev-alist nil
1057 "Alist of link abbreviations.
1058 The car of each element is a string, to be replaced at the start of a link.
1059 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
1060 links in Org-mode buffers can have an optional tag after a double colon, e.g.
1062 [[linkkey:tag][description]]
1064 If REPLACE is a string, the tag will simply be appended to create the link.
1065 If the string contains \"%s\", the tag will be inserted there.
1067 REPLACE may also be a function that will be called with the tag as the
1068 only argument to create the link, which should be returned as a string.
1070 See the manual for examples."
1074 (defcustom org-descriptive-links t
1075 "Non-nil means, hide link part and only show description of bracket links.
1076 Bracket links are like [[link][descritpion]]. This variable sets the initial
1077 state in new org-mode buffers. The setting can then be toggled on a
1078 per-buffer basis from the Org->Hyperlinks menu."
1082 (defcustom org-link-file-path-type
'adaptive
1083 "How the path name in file links should be stored.
1086 relative Relative to the current directory, i.e. the directory of the file
1087 into which the link is being inserted.
1088 absolute Absolute path, if possible with ~ for home directory.
1089 noabbrev Absolute path, no abbreviation of home directory.
1090 adaptive Use relative path for files in the current directory and sub-
1091 directories of it. For other files, use an absolute path."
1099 (defcustom org-activate-links
'(bracket angle plain radio tag date
)
1100 "Types of links that should be activated in Org-mode files.
1101 This is a list of symbols, each leading to the activation of a certain link
1102 type. In principle, it does not hurt to turn on most link types - there may
1103 be a small gain when turning off unused link types. The types are:
1105 bracket The recommended [[link][description]] or [[link]] links with hiding.
1106 angular Links in angular brackes that may contain whitespace like
1107 <bbdb:Carsten Dominik>.
1108 plain Plain links in normal text, no whitespace, like http://google.com.
1109 radio Text that is matched by a radio target, see manual for details.
1110 tag Tag settings in a headline (link to tag search).
1111 date Time stamps (link to calendar).
1113 Changing this variable requires a restart of Emacs to become effective."
1115 :type
'(set (const :tag
"Double bracket links (new style)" bracket
)
1116 (const :tag
"Angular bracket links (old style)" angular
)
1117 (const :tag
"plain text links" plain
)
1118 (const :tag
"Radio target matches" radio
)
1119 (const :tag
"Tags" tag
)
1120 (const :tag
"Tags" target
)
1121 (const :tag
"Timestamps" date
)))
1123 (defgroup org-link-store nil
1124 "Options concerning storing links in Org-mode"
1125 :tag
"Org Store Link"
1128 (defcustom org-email-link-description-format
"Email %c: %.30s"
1129 "Format of the description part of a link to an email or usenet message.
1130 The following %-excapes will be replaced by corresponding information:
1132 %F full \"From\" field
1133 %f name, taken from \"From\" field, address if no name
1134 %T full \"To\" field
1135 %t first name in \"To\" field, address if no name
1136 %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
1137 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
1141 You may use normal field width specification between the % and the letter.
1142 This is for example useful to limit the length of the subject.
1144 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
1145 :group
'org-link-store
1148 (defcustom org-from-is-user-regexp
1150 (when (and user-mail-address
(not (string= user-mail-address
"")))
1151 (setq r1
(concat "\\<" (regexp-quote user-mail-address
) "\\>")))
1152 (when (and user-full-name
(not (string= user-full-name
"")))
1153 (setq r2
(concat "\\<" (regexp-quote user-full-name
) "\\>")))
1154 (if (and r1 r2
) (concat r1
"\\|" r2
) (or r1 r2
)))
1155 "Regexp mached against the \"From:\" header of an email or usenet message.
1156 It should match if the message is from the user him/herself."
1157 :group
'org-link-store
1160 (defcustom org-context-in-file-links t
1161 "Non-nil means, file links from `org-store-link' contain context.
1162 A search string will be added to the file name with :: as separator and
1163 used to find the context when the link is activated by the command
1164 `org-open-at-point'.
1165 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1166 negates this setting for the duration of the command."
1167 :group
'org-link-store
1170 (defcustom org-keep-stored-link-after-insertion nil
1171 "Non-nil means, keep link in list for entire session.
1173 The command `org-store-link' adds a link pointing to the current
1174 location to an internal list. These links accumulate during a session.
1175 The command `org-insert-link' can be used to insert links into any
1176 Org-mode file (offering completion for all stored links). When this
1177 option is nil, every link which has been inserted once using \\[org-insert-link]
1178 will be removed from the list, to make completing the unused links
1180 :group
'org-link-store
1183 (defcustom org-usenet-links-prefer-google nil
1184 "Non-nil means, `org-store-link' will create web links to Google groups.
1185 When nil, Gnus will be used for such links.
1186 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1187 negates this setting for the duration of the command."
1188 :group
'org-link-store
1191 (defgroup org-link-follow nil
1192 "Options concerning following links in Org-mode"
1193 :tag
"Org Follow Link"
1196 (defcustom org-tab-follows-link nil
1197 "Non-nil means, on links TAB will follow the link.
1198 Needs to be set before org.el is loaded."
1199 :group
'org-link-follow
1202 (defcustom org-return-follows-link nil
1203 "Non-nil means, on links RET will follow the link.
1204 Needs to be set before org.el is loaded."
1205 :group
'org-link-follow
1208 (defcustom org-mouse-1-follows-link t
1209 "Non-nil means, mouse-1 on a link will follow the link.
1210 A longer mouse click will still set point. Does not wortk on XEmacs.
1211 Needs to be set before org.el is loaded."
1212 :group
'org-link-follow
1215 (defcustom org-mark-ring-length
4
1216 "Number of different positions to be recorded in the ring
1217 Changing this requires a restart of Emacs to work correctly."
1218 :group
'org-link-follow
1221 (defcustom org-link-frame-setup
1222 '((vm . vm-visit-folder-other-frame
)
1223 (gnus . gnus-other-frame
)
1224 (file . find-file-other-window
))
1225 "Setup the frame configuration for following links.
1226 When following a link with Emacs, it may often be useful to display
1227 this link in another window or frame. This variable can be used to
1228 set this up for the different types of links.
1231 `vm-visit-folder-other-frame'
1232 For Gnus, use any of
1235 For FILE, use any of
1237 `find-file-other-window'
1238 `find-file-other-frame'
1239 For the calendar, use the variable `calendar-setup'.
1240 For BBDB, it is currently only possible to display the matches in
1242 :group
'org-link-follow
1246 (const vm-visit-folder
)
1247 (const vm-visit-folder-other-window
)
1248 (const vm-visit-folder-other-frame
)))
1252 (const gnus-other-frame
)))
1256 (const find-file-other-window
)
1257 (const find-file-other-frame
)))))
1259 (defcustom org-display-internal-link-with-indirect-buffer nil
1260 "Non-nil means, use indirect buffer to display infile links.
1261 Activating internal links (from one location in a file to another location
1262 in the same file) normally just jumps to the location. When the link is
1263 activated with a C-u prefix (or with mouse-3), the link is displayed in
1264 another window. When this option is set, the other window actually displays
1265 an indirect buffer clone of the current buffer, to avoid any visibility
1266 changes to the current buffer."
1267 :group
'org-link-follow
1270 (defcustom org-open-non-existing-files nil
1271 "Non-nil means, `org-open-file' will open non-existing files.
1272 When nil, an error will be generated."
1273 :group
'org-link-follow
1276 (defcustom org-link-mailto-program
'(browse-url "mailto:%a?subject=%s")
1277 "Function and arguments to call for following mailto links.
1278 This is a list with the first element being a lisp function, and the
1279 remaining elements being arguments to the function. In string arguments,
1280 %a will be replaced by the address, and %s will be replaced by the subject
1281 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1282 :group
'org-link-follow
1284 (const :tag
"browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1285 (const :tag
"compose-mail" (compose-mail "%a" "%s"))
1286 (const :tag
"message-mail" (message-mail "%a" "%s"))
1287 (cons :tag
"other" (function) (repeat :tag
"argument" sexp
))))
1289 (defcustom org-confirm-shell-link-function
'yes-or-no-p
1290 "Non-nil means, ask for confirmation before executing shell links.
1291 Shell links can be dangerous: just think about a link
1293 [[shell:rm -rf ~/*][Google Search]]
1295 This link would show up in your Org-mode document as \"Google Search\",
1296 but really it would remove your entire home directory.
1297 Therefore we advise against setting this variable to nil.
1298 Just change it to `y-or-n-p' of you want to confirm with a
1299 single keystroke rather than having to type \"yes\"."
1300 :group
'org-link-follow
1302 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1303 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1304 (const :tag
"no confirmation (dangerous)" nil
)))
1306 (defcustom org-confirm-elisp-link-function
'yes-or-no-p
1307 "Non-nil means, ask for confirmation before executing Emacs Lisp links.
1308 Elisp links can be dangerous: just think about a link
1310 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1312 This link would show up in your Org-mode document as \"Google Search\",
1313 but really it would remove your entire home directory.
1314 Therefore we advise against setting this variable to nil.
1315 Just change it to `y-or-n-p' of you want to confirm with a
1316 single keystroke rather than having to type \"yes\"."
1317 :group
'org-link-follow
1319 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1320 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1321 (const :tag
"no confirmation (dangerous)" nil
)))
1323 (defconst org-file-apps-defaults-gnu
1326 "Default file applications on a UNIX or GNU/Linux system.
1327 See `org-file-apps'.")
1329 (defconst org-file-apps-defaults-macosx
1335 ("eps.gz" .
"gv %s")
1337 ("fig" .
"xfig %s"))
1338 "Default file applications on a MacOS X system.
1339 The system \"open\" is known as a default, but we use X11 applications
1340 for some files for which the OS does not have a good default.
1341 See `org-file-apps'.")
1343 (defconst org-file-apps-defaults-windowsnt
1347 (list (if (featurep 'xemacs
)
1348 'mswindows-shell-execute
1351 "Default file applications on a Windows NT system.
1352 The system \"open\" is used for most files.
1353 See `org-file-apps'.")
1355 (defcustom org-file-apps
1364 "External applications for opening `file:path' items in a document.
1365 Org-mode uses system defaults for different file types, but
1366 you can use this variable to set the application for a given file
1367 extension. The entries in this list are cons cells where the car identifies
1368 files and the cdr the corresponding command. Possible values for the
1370 \"ext\" A string identifying an extension
1371 `directory' Matches a directory
1372 `remote' Matches a remote file, accessible through tramp or efs.
1373 Remote files most likely should be visited through Emacs
1374 because external applications cannot handle such paths.
1375 t Default for all remaining files
1377 Possible values for the command are:
1378 `emacs' The file will be visited by the current Emacs process.
1379 `default' Use the default application for this file type.
1380 string A command to be executed by a shell; %s will be replaced
1381 by the path to the file.
1382 sexp A Lisp form which will be evaluated. The file path will
1383 be available in the Lisp variable `file'.
1384 For more examples, see the system specific constants
1385 `org-file-apps-defaults-macosx'
1386 `org-file-apps-defaults-windowsnt'
1387 `org-file-apps-defaults-gnu'."
1388 :group
'org-link-follow
1390 (cons (choice :value
""
1391 (string :tag
"Extension")
1392 (const :tag
"Default for unrecognized files" t
)
1393 (const :tag
"Remote file" remote
)
1394 (const :tag
"Links to a directory" directory
))
1396 (const :tag
"Visit with Emacs" emacs
)
1397 (const :tag
"Use system default" default
)
1398 (string :tag
"Command")
1399 (sexp :tag
"Lisp form")))))
1401 (defcustom org-mhe-search-all-folders nil
1402 "Non-nil means, that the search for the mh-message will be extended to
1403 all folders if the message cannot be found in the folder given in the link.
1404 Searching all folders is very efficient with one of the search engines
1405 supported by MH-E, but will be slow with pick."
1406 :group
'org-link-follow
1409 (defgroup org-remember nil
1410 "Options concerning interaction with remember.el."
1414 (defcustom org-directory
"~/org"
1415 "Directory with org files.
1416 This directory will be used as default to prompt for org files.
1417 Used by the hooks for remember.el."
1418 :group
'org-remember
1421 (defcustom org-default-notes-file
"~/.notes"
1422 "Default target for storing notes.
1423 Used by the hooks for remember.el. This can be a string, or nil to mean
1424 the value of `remember-data-file'.
1425 You can set this on a per-template basis with the variable
1426 `org-remember-templates'."
1427 :group
'org-remember
1429 (const :tag
"Default from remember-data-file" nil
)
1432 (defcustom org-remember-store-without-prompt t
1433 "Non-nil means, `C-c C-c' stores remember note without further promts.
1434 In this case, you need `C-u C-c C-c' to get the prompts for
1435 note file and headline.
1436 When this variable is nil, `C-c C-c' give you the prompts, and
1437 `C-u C-c C-c' trigger the fasttrack."
1438 :group
'org-remember
1441 (defcustom org-remember-interactive-interface
'refile
1442 "The interface to be used for interactive filing of remember notes.
1443 This is only used when the interactive mode for selecting a filing
1444 location is used (see the variable `org-remember-store-without-prompt').
1446 outline The interface shows an outline of the relevant file
1447 and the correct heading is found by moving through
1448 the outline or by searching with incremental search.
1449 outline-path-completion Headlines in the current buffer are offered via
1451 refile Use the refile interface, and offer headlines,
1452 possibly from different buffers."
1453 :group
'org-remember
1455 (const :tag
"Refile" refile
)
1456 (const :tag
"Outline" outline
)
1457 (const :tag
"Outline-path-completion" outline-path-completion
)))
1459 (defcustom org-goto-interface
'outline
1460 "The default interface to be used for `org-goto'.
1462 outline The interface shows an outline of the relevant file
1463 and the correct heading is found by moving through
1464 the outline or by searching with incremental search.
1465 outline-path-completion Headlines in the current buffer are offered via
1467 :group
'org-remember
; FIXME: different group for org-goto and org-refile
1469 (const :tag
"Outline" outline
)
1470 (const :tag
"Outline-path-completion" outline-path-completion
)))
1472 (defcustom org-remember-default-headline
""
1473 "The headline that should be the default location in the notes file.
1474 When filing remember notes, the cursor will start at that position.
1475 You can set this on a per-template basis with the variable
1476 `org-remember-templates'."
1477 :group
'org-remember
1480 (defcustom org-remember-templates nil
1481 "Templates for the creation of remember buffers.
1482 When nil, just let remember make the buffer.
1483 When not nil, this is a list of 5-element lists. In each entry, the first
1484 element is the name of the template, which should be a single short word.
1485 The second element is a character, a unique key to select this template.
1486 The third element is the template. The fourth element is optional and can
1487 specify a destination file for remember items created with this template.
1488 The default file is given by `org-default-notes-file'. An optional fifth
1489 element can specify the headline in that file that should be offered
1490 first when the user is asked to file the entry. The default headline is
1491 given in the variable `org-remember-default-headline'.
1493 The template specifies the structure of the remember buffer. It should have
1494 a first line starting with a star, to act as the org-mode headline.
1495 Furthermore, the following %-escapes will be replaced with content:
1497 %^{prompt} Prompt the user for a string and replace this sequence with it.
1498 A default value and a completion table ca be specified like this:
1499 %^{prompt|default|completion2|completion3|...}
1500 %t time stamp, date only
1501 %T time stamp with date and time
1502 %u, %U like the above, but inactive time stamps
1503 %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
1504 You may define a prompt like %^{Please specify birthday}t
1505 %n user name (taken from `user-full-name')
1506 %a annotation, normally the link created with org-store-link
1507 %i initial content, the region active. If %i is indented,
1508 the entire inserted text will be indented as well.
1509 %c content of the clipboard, or current kill ring head
1510 %^g prompt for tags, with completion on tags in target file
1511 %^G prompt for tags, with completion all tags in all agenda files
1512 %:keyword specific information for certain link types, see below
1513 %[pathname] insert the contents of the file given by `pathname'
1514 %(sexp) evaluate elisp `(sexp)' and replace with the result
1515 %! Store this note immediately after filling the template
1517 %? After completing the template, position cursor here.
1519 Apart from these general escapes, you can access information specific to the
1520 link type that is created. For example, calling `remember' in emails or gnus
1521 will record the author and the subject of the message, which you can access
1522 with %:author and %:subject, respectively. Here is a complete list of what
1523 is recorded for each link type.
1525 Link type | Available information
1526 -------------------+------------------------------------------------------
1527 bbdb | %:type %:name %:company
1528 vm, wl, mh, rmail | %:type %:subject %:message-id
1529 | %:from %:fromname %:fromaddress
1530 | %:to %:toname %:toaddress
1531 | %:fromto (either \"to NAME\" or \"from NAME\")
1532 gnus | %:group, for messages also all email fields
1533 w3, w3m | %:type %:url
1534 info | %:type %:file %:node
1535 calendar | %:type %:date"
1536 :group
'org-remember
1537 :get
(lambda (var) ; Make sure all entries have 5 elements
1539 (if (not (stringp (car x
))) (setq x
(cons "" x
)))
1540 (cond ((= (length x
) 4) (append x
'("")))
1541 ((= (length x
) 3) (append x
'("" "")))
1543 (default-value var
)))
1546 (list :value
("" ?a
"\n" nil nil
)
1547 (string :tag
"Name")
1548 (character :tag
"Selection Key")
1549 (string :tag
"Template")
1551 (file :tag
"Destination file")
1552 (const :tag
"Prompt for file" nil
))
1554 (string :tag
"Destination headline")
1555 (const :tag
"Selection interface for heading")))))
1557 (defcustom org-reverse-note-order nil
1558 "Non-nil means, store new notes at the beginning of a file or entry.
1559 When nil, new notes will be filed to the end of a file or entry.
1560 This can also be a list with cons cells of regular expressions that
1561 are matched against file names, and values."
1562 :group
'org-remember
1564 (const :tag
"Reverse always" t
)
1565 (const :tag
"Reverse never" nil
)
1566 (repeat :tag
"By file name regexp"
1567 (cons regexp boolean
))))
1569 (defcustom org-refile-targets nil
1570 "Targets for refiling entries with \\[org-refile].
1571 This is list of cons cells. Each cell contains:
1572 - a specification of the files to be considered, either a list of files,
1573 or a symbol whose function or value fields will be used to retrieve
1574 a file name or a list of file names. Nil means, refile to a different
1575 heading in the current buffer.
1576 - A specification of how to find candidate refile targets. This may be
1578 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
1579 This tag has to be present in all target headlines, inheritance will
1581 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
1583 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
1584 headlines that are refiling targets.
1585 - a cons cell (:level . N). Any headline of level N is considered a target.
1586 - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
1587 ;; FIXME: what if there are a var and func with same name???
1588 :group
'org-remember
1591 (choice :value org-agenda-files
1592 (const :tag
"All agenda files" org-agenda-files
)
1593 (const :tag
"Current buffer" nil
)
1594 (function) (variable) (file))
1595 (choice :tag
"Identify target headline by"
1596 (cons :tag
"Specific tag" (const :tag
) (string))
1597 (cons :tag
"TODO keyword" (const :todo
) (string))
1598 (cons :tag
"Regular expression" (const :regexp
) (regexp))
1599 (cons :tag
"Level number" (const :level
) (integer))
1600 (cons :tag
"Max Level number" (const :maxlevel
) (integer))))))
1602 (defcustom org-refile-use-outline-path nil
1603 "Non-nil means, provide refile targets as paths.
1604 So a level 3 headline will be available as level1/level2/level3.
1605 When the value is `file', also include the file name (without directory)
1606 into the path. When `full-file-path', include the full file path."
1607 :group
'org-remember
1609 (const :tag
"Not" nil
)
1610 (const :tag
"Yes" t
)
1611 (const :tag
"Start with file name" file
)
1612 (const :tag
"Start with full file path" full-file-path
)))
1614 (defgroup org-todo nil
1615 "Options concerning TODO items in Org-mode."
1619 (defgroup org-progress nil
1620 "Options concerning Progress logging in Org-mode."
1624 (defcustom org-todo-keywords
'((sequence "TODO" "DONE"))
1625 "List of TODO entry keyword sequences and their interpretation.
1626 \\<org-mode-map>This is a list of sequences.
1628 Each sequence starts with a symbol, either `sequence' or `type',
1629 indicating if the keywords should be interpreted as a sequence of
1630 action steps, or as different types of TODO items. The first
1631 keywords are states requiring action - these states will select a headline
1632 for inclusion into the global TODO list Org-mode produces. If one of
1633 the \"keywords\" is the vertical bat \"|\" the remaining keywords
1634 signify that no further action is necessary. If \"|\" is not found,
1635 the last keyword is treated as the only DONE state of the sequence.
1637 The command \\[org-todo] cycles an entry through these states, and one
1638 additional state where no keyword is present. For details about this
1639 cycling, see the manual.
1641 TODO keywords and interpretation can also be set on a per-file basis with
1642 the special #+SEQ_TODO and #+TYP_TODO lines.
1644 For backward compatibility, this variable may also be just a list
1645 of keywords - in this case the interptetation (sequence or type) will be
1646 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
1648 :group
'org-keywords
1650 (repeat :tag
"Old syntax, just keywords"
1651 (string :tag
"Keyword"))
1652 (repeat :tag
"New syntax"
1655 :tag
"Interpretation"
1656 (const :tag
"Sequence (cycling hits every state)" sequence
)
1657 (const :tag
"Type (cycling directly to DONE)" type
))
1659 (string :tag
"Keyword"))))))
1661 (defvar org-todo-keywords-1 nil
)
1662 (make-variable-buffer-local 'org-todo-keywords-1
)
1663 (defvar org-todo-keywords-for-agenda nil
)
1664 (defvar org-done-keywords-for-agenda nil
)
1665 (defvar org-not-done-keywords nil
)
1666 (make-variable-buffer-local 'org-not-done-keywords
)
1667 (defvar org-done-keywords nil
)
1668 (make-variable-buffer-local 'org-done-keywords
)
1669 (defvar org-todo-heads nil
)
1670 (make-variable-buffer-local 'org-todo-heads
)
1671 (defvar org-todo-sets nil
)
1672 (make-variable-buffer-local 'org-todo-sets
)
1673 (defvar org-todo-log-states nil
)
1674 (make-variable-buffer-local 'org-todo-log-states
)
1675 (defvar org-todo-kwd-alist nil
)
1676 (make-variable-buffer-local 'org-todo-kwd-alist
)
1677 (defvar org-todo-key-alist nil
)
1678 (make-variable-buffer-local 'org-todo-key-alist
)
1679 (defvar org-todo-key-trigger nil
)
1680 (make-variable-buffer-local 'org-todo-key-trigger
)
1682 (defcustom org-todo-interpretation
'sequence
1683 "Controls how TODO keywords are interpreted.
1684 This variable is in principle obsolete and is only used for
1685 backward compatibility, if the interpretation of todo keywords is
1686 not given already in `org-todo-keywords'. See that variable for
1689 :group
'org-keywords
1690 :type
'(choice (const sequence
)
1693 (defcustom org-use-fast-todo-selection
'prefix
1694 "Non-nil means, use the fast todo selection scheme with C-c C-t.
1695 This variable describes if and under what circumstances the cycling
1696 mechanism for TODO keywords will be replaced by a single-key, direct
1699 When nil, fast selection is never used.
1701 When the symbol `prefix', it will be used when `org-todo' is called with
1702 a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
1703 in an agenda buffer.
1705 When t, fast selection is used by default. In this case, the prefix
1706 argument forces cycling instead.
1708 In all cases, the special interface is only used if access keys have actually
1709 been assigned by the user, i.e. if keywords in the configuration are followed
1710 by a letter in parenthesis, like TODO(t)."
1713 (const :tag
"Never" nil
)
1714 (const :tag
"By default" t
)
1715 (const :tag
"Only with C-u C-c C-t" prefix
)))
1717 (defcustom org-after-todo-state-change-hook nil
1718 "Hook which is run after the state of a TODO item was changed.
1719 The new state (a string with a TODO keyword, or nil) is available in the
1720 Lisp variable `state'."
1724 (defcustom org-log-done nil
1725 "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
1726 When the state of an entry is changed from nothing or a DONE state to
1727 a not-done TODO state, remove a previous closing date.
1729 This can also be a list of symbols indicating under which conditions
1730 the time stamp recording the action should be annotated with a short note.
1731 Valid members of this list are
1733 done Offer to record a note when marking entries done
1734 state Offer to record a note whenever changing the TODO state
1735 of an item. This is only relevant if TODO keywords are
1736 interpreted as sequence, see variable `org-todo-interpretation'.
1737 When `state' is set, this includes tracking `done'.
1738 clock-out Offer to record a note when clocking out of an item.
1740 A separate window will then pop up and allow you to type a note.
1741 After finishing with C-c C-c, the note will be added directly after the
1742 timestamp, as a plain list item. See also the variable
1743 `org-log-note-headings'.
1745 Logging can also be configured on a per-file basis by adding one of
1746 the following lines anywhere in the buffer:
1749 #+STARTUP: nologging
1750 #+STARTUP: lognotedone
1751 #+STARTUP: lognotestate
1752 #+STARTUP: lognoteclock-out
1754 You can have local logging settings for a subtree by setting the LOGGING
1755 property to one or more of these keywords."
1757 :group
'org-progress
1759 (const :tag
"off" nil
)
1761 (set :tag
"on, with notes, detailed control" :greedy t
:value
(done)
1762 (const :tag
"when item is marked DONE" done
)
1763 (const :tag
"when TODO state changes" state
)
1764 (const :tag
"when clocking out" clock-out
))))
1766 (defcustom org-log-done-with-time t
1767 "Non-nil means, the CLOSED time stamp will contain date and time.
1768 When nil, only the date will be recorded."
1769 :group
'org-progress
1772 (defcustom org-log-note-headings
1773 '((done .
"CLOSING NOTE %t")
1774 (state .
"State %-12s %t")
1776 "Headings for notes added when clocking out or closing TODO items.
1777 The value is an alist, with the car being a symbol indicating the note
1778 context, and the cdr is the heading to be used. The heading may also be the
1780 %t in the heading will be replaced by a time stamp.
1781 %s will be replaced by the new TODO state, in double quotes.
1782 %u will be replaced by the user name.
1783 %U will be replaced by the full user name."
1785 :group
'org-progress
1786 :type
'(list :greedy t
1787 (cons (const :tag
"Heading when closing an item" done
) string
)
1789 "Heading when changing todo state (todo sequence only)"
1791 (cons (const :tag
"Heading when clocking out" clock-out
) string
)))
1793 (defcustom org-log-states-order-reversed t
1794 "Non-nil means, the latest state change note will be directly after heading.
1795 When nil, the notes will be orderer according to time."
1797 :group
'org-progress
1800 (defcustom org-log-repeat t
1801 "Non-nil means, prompt for a note when REPEAT is resetting a TODO entry.
1802 When nil, no note will be taken.
1803 This option can also be set with on a per-file-basis with
1805 #+STARTUP: logrepeat
1806 #+STARTUP: nologrepeat
1808 You can have local logging settings for a subtree by setting the LOGGING
1809 property to one or more of these keywords."
1811 :group
'org-progress
1814 (defcustom org-clock-into-drawer
2
1815 "Should clocking info be wrapped into a drawer?
1816 When t, clocking info will always be inserted into a :CLOCK: drawer.
1817 If necessary, the drawer will be created.
1818 When nil, the drawer will not be created, but used when present.
1819 When an integer and the number of clocking entries in an item
1820 reaches or exceeds this number, a drawer will be created."
1822 :group
'org-progress
1824 (const :tag
"Always" t
)
1825 (const :tag
"Only when drawer exists" nil
)
1826 (integer :tag
"When at least N clock entries")))
1828 (defcustom org-clock-out-when-done t
1829 "When t, the clock will be stopped when the relevant entry is marked DONE.
1830 Nil means, clock will keep running until stopped explicitly with
1831 `C-c C-x C-o', or until the clock is started in a different item."
1832 :group
'org-progress
1835 (defcustom org-clock-in-switch-to-state nil
1836 "Set task to a special todo state while clocking it.
1837 The value should be the state to which the entry should be switched."
1838 :group
'org-progress
1841 (const :tag
"Don't force a state" nil
)
1842 (string :tag
"State")))
1844 (defgroup org-priorities nil
1845 "Priorities in Org-mode."
1846 :tag
"Org Priorities"
1849 (defcustom org-highest-priority ?A
1850 "The highest priority of TODO items. A character like ?A, ?B etc.
1851 Must have a smaller ASCII number than `org-lowest-priority'."
1852 :group
'org-priorities
1855 (defcustom org-lowest-priority ?C
1856 "The lowest priority of TODO items. A character like ?A, ?B etc.
1857 Must have a larger ASCII number than `org-highest-priority'."
1858 :group
'org-priorities
1861 (defcustom org-default-priority ?B
1862 "The default priority of TODO items.
1863 This is the priority an item get if no explicit priority is given."
1864 :group
'org-priorities
1867 (defcustom org-priority-start-cycle-with-default t
1868 "Non-nil means, start with default priority when starting to cycle.
1869 When this is nil, the first step in the cycle will be (depending on the
1870 command used) one higher or lower that the default priority."
1871 :group
'org-priorities
1874 (defgroup org-time nil
1875 "Options concerning time stamps and deadlines in Org-mode."
1879 (defcustom org-insert-labeled-timestamps-at-point nil
1880 "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
1881 When nil, these labeled time stamps are forces into the second line of an
1882 entry, just after the headline. When scheduling from the global TODO list,
1883 the time stamp will always be forced into the second line."
1887 (defconst org-time-stamp-formats
'("<%Y-%m-%d %a>" .
"<%Y-%m-%d %a %H:%M>")
1888 "Formats for `format-time-string' which are used for time stamps.
1889 It is not recommended to change this constant.")
1891 (defcustom org-time-stamp-rounding-minutes
0
1892 "Number of minutes to round time stamps to upon insertion.
1893 When zero, insert the time unmodified. Useful rounding numbers
1894 should be factors of 60, so for example 5, 10, 15.
1895 When this is not zero, you can still force an exact time-stamp by using
1896 a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
1900 (defcustom org-display-custom-times nil
1901 "Non-nil means, overlay custom formats over all time stamps.
1902 The formats are defined through the variable `org-time-stamp-custom-formats'.
1903 To turn this on on a per-file basis, insert anywhere in the file:
1904 #+STARTUP: customtime"
1908 (make-variable-buffer-local 'org-display-custom-times
)
1910 (defcustom org-time-stamp-custom-formats
1911 '("<%m/%d/%y %a>" .
"<%m/%d/%y %a %H:%M>") ; american
1912 "Custom formats for time stamps. See `format-time-string' for the syntax.
1913 These are overlayed over the default ISO format if the variable
1914 `org-display-custom-times' is set. Time like %H:%M should be at the
1915 end of the second format."
1919 (defun org-time-stamp-format (&optional long inactive
)
1920 "Get the right format for a time string."
1921 (let ((f (if long
(cdr org-time-stamp-formats
)
1922 (car org-time-stamp-formats
))))
1924 (concat "[" (substring f
1 -
1) "]")
1927 (defcustom org-read-date-prefer-future t
1928 "Non-nil means, assume future for incomplete date input from user.
1929 This affects the following situations:
1930 1. The user gives a day, but no month.
1931 For example, if today is the 15th, and you enter \"3\", Org-mode will
1932 read this as the third of *next* month. However, if you enter \"17\",
1933 it will be considered as *this* month.
1934 2. The user gives a month but not a year.
1935 For example, if it is april and you enter \"feb 2\", this will be read
1936 as feb 2, *next* year. \"May 5\", however, will be this year.
1938 When this option is nil, the current month and year will always be used
1943 (defcustom org-read-date-display-live t
1944 "Non-nil means, display current interpretation of date prompt live.
1945 This display will be in an overlay, in the minibuffer."
1949 (defcustom org-read-date-popup-calendar t
1950 "Non-nil means, pop up a calendar when prompting for a date.
1951 In the calendar, the date can be selected with mouse-1. However, the
1952 minibuffer will also be active, and you can simply enter the date as well.
1953 When nil, only the minibuffer will be available."
1956 (if (fboundp 'defvaralias
)
1957 (defvaralias 'org-popup-calendar-for-date-prompt
1958 'org-read-date-popup-calendar
))
1960 (defcustom org-extend-today-until
0
1961 "The hour when your day really ends.
1962 This has influence for the following applications:
1963 - When switching the agenda to \"today\". It it is still earlier than
1964 the time given here, the day recognized as TODAY is actually yesterday.
1965 - When a date is read from the user and it is still before the time given
1966 here, the current date and time will be assumed to be yesterday, 23:59.
1969 IMPORTANT: This is still a very experimental feature, it may disappear
1970 again or it may be extended to mean more things."
1974 (defcustom org-edit-timestamp-down-means-later nil
1975 "Non-nil means, S-down will increase the time in a time stamp.
1976 When nil, S-up will increase."
1980 (defcustom org-calendar-follow-timestamp-change t
1981 "Non-nil means, make the calendar window follow timestamp changes.
1982 When a timestamp is modified and the calendar window is visible, it will be
1983 moved to the new date."
1987 (defcustom org-clock-heading-function nil
1988 "When non-nil, should be a function to create `org-clock-heading'.
1989 This is the string shown in the mode line when a clock is running.
1990 The function is called with point at the beginning of the headline."
1991 :group
'org-time
; FIXME: Should we have a separate group????
1994 (defgroup org-tags nil
1995 "Options concerning tags in Org-mode."
1999 (defcustom org-tag-alist nil
2000 "List of tags allowed in Org-mode files.
2001 When this list is nil, Org-mode will base TAG input on what is already in the
2003 The value of this variable is an alist, the car of each entry must be a
2004 keyword as a string, the cdr may be a character that is used to select
2005 that tag through the fast-tag-selection interface.
2006 See the manual for details."
2010 (cons (string :tag
"Tag name")
2011 (character :tag
"Access char"))
2012 (const :tag
"Start radio group" (:startgroup
))
2013 (const :tag
"End radio group" (:endgroup
)))))
2015 (defcustom org-use-fast-tag-selection
'auto
2016 "Non-nil means, use fast tag selection scheme.
2017 This is a special interface to select and deselect tags with single keys.
2018 When nil, fast selection is never used.
2019 When the symbol `auto', fast selection is used if and only if selection
2020 characters for tags have been configured, either through the variable
2021 `org-tag-alist' or through a #+TAGS line in the buffer.
2022 When t, fast selection is always used and selection keys are assigned
2023 automatically if necessary."
2026 (const :tag
"Always" t
)
2027 (const :tag
"Never" nil
)
2028 (const :tag
"When selection characters are configured" 'auto
)))
2030 (defcustom org-fast-tag-selection-single-key nil
2031 "Non-nil means, fast tag selection exits after first change.
2032 When nil, you have to press RET to exit it.
2033 During fast tag selection, you can toggle this flag with `C-c'.
2034 This variable can also have the value `expert'. In this case, the window
2035 displaying the tags menu is not even shown, until you press C-c again."
2038 (const :tag
"No" nil
)
2039 (const :tag
"Yes" t
)
2040 (const :tag
"Expert" expert
)))
2042 (defvar org-fast-tag-selection-include-todo nil
2043 "Non-nil means, fast tags selection interface will also offer TODO states.
2044 This is an undocumented feature, you should not rely on it.")
2046 (defcustom org-tags-column -
80
2047 "The column to which tags should be indented in a headline.
2048 If this number is positive, it specifies the column. If it is negative,
2049 it means that the tags should be flushright to that column. For example,
2050 -80 works well for a normal 80 character screen."
2054 (defcustom org-auto-align-tags t
2055 "Non-nil means, realign tags after pro/demotion of TODO state change.
2056 These operations change the length of a headline and therefore shift
2057 the tags around. With this options turned on, after each such operation
2058 the tags are again aligned to `org-tags-column'."
2062 (defcustom org-use-tag-inheritance t
2063 "Non-nil means, tags in levels apply also for sublevels.
2064 When nil, only the tags directly given in a specific line apply there.
2065 If you turn off this option, you very likely want to turn on the
2066 companion option `org-tags-match-list-sublevels'."
2070 (defcustom org-tags-match-list-sublevels nil
2071 "Non-nil means list also sublevels of headlines matching tag search.
2072 Because of tag inheritance (see variable `org-use-tag-inheritance'),
2073 the sublevels of a headline matching a tag search often also match
2074 the same search. Listing all of them can create very long lists.
2075 Setting this variable to nil causes subtrees of a match to be skipped.
2076 This option is off by default, because inheritance in on. If you turn
2077 inheritance off, you very likely want to turn this option on.
2079 As a special case, if the tag search is restricted to TODO items, the
2080 value of this variable is ignored and sublevels are always checked, to
2081 make sure all corresponding TODO items find their way into the list."
2085 (defvar org-tags-history nil
2086 "History of minibuffer reads for tags.")
2087 (defvar org-last-tags-completion-table nil
2088 "The last used completion table for tags.")
2089 (defvar org-after-tags-change-hook nil
2090 "Hook that is run after the tags in a line have changed.")
2092 (defgroup org-properties nil
2093 "Options concerning properties in Org-mode."
2094 :tag
"Org Properties"
2097 (defcustom org-property-format
"%-10s %s"
2098 "How property key/value pairs should be formatted by `indent-line'.
2099 When `indent-line' hits a property definition, it will format the line
2100 according to this format, mainly to make sure that the values are
2101 lined-up with respect to each other."
2102 :group
'org-properties
2105 (defcustom org-use-property-inheritance nil
2106 "Non-nil means, properties apply also for sublevels.
2107 This setting is only relevant during property searches, not when querying
2108 an entry with `org-entry-get'. To retrieve a property with inheritance,
2109 you need to call `org-entry-get' with the inheritance flag.
2110 Turning this on can cause significant overhead when doing a search, so
2111 this is turned off by default.
2112 When nil, only the properties directly given in the current entry count.
2113 The value may also be a list of properties that shouldhave inheritance.
2115 However, note that some special properties use inheritance under special
2116 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
2117 and the properties ending in \"_ALL\" when they are used as descriptor
2118 for valid values of a property."
2119 :group
'org-properties
2121 (const :tag
"Not" nil
)
2122 (const :tag
"Always" nil
)
2123 (repeat :tag
"Specific properties" (string :tag
"Property"))))
2125 (defcustom org-columns-default-format
"%25ITEM %TODO %3PRIORITY %TAGS"
2126 "The default column format, if no other format has been defined.
2127 This variable can be set on the per-file basis by inserting a line
2129 #+COLUMNS: %25ITEM ....."
2130 :group
'org-properties
2133 (defcustom org-global-properties nil
2134 "List of property/value pairs that can be inherited by any entry.
2135 You can set buffer-local values for this by adding lines like
2137 #+PROPERTY: NAME VALUE"
2138 :group
'org-properties
2140 (cons (string :tag
"Property")
2141 (string :tag
"Value"))))
2143 (defvar org-local-properties nil
2144 "List of property/value pairs that can be inherited by any entry.
2145 Valid for the current buffer.
2146 This variable is populated from #+PROPERTY lines.")
2148 (defgroup org-agenda nil
2149 "Options concerning agenda views in Org-mode."
2153 (defvar org-category nil
2154 "Variable used by org files to set a category for agenda display.
2155 Such files should use a file variable to set it, for example
2157 # -*- mode: org; org-category: \"ELisp\"
2159 or contain a special line
2163 If the file does not specify a category, then file's base name
2165 (make-variable-buffer-local 'org-category
)
2167 (defcustom org-agenda-files nil
2168 "The files to be used for agenda display.
2169 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
2170 \\[org-remove-file]. You can also use customize to edit the list.
2172 If an entry is a directory, all files in that directory that are matched by
2173 `org-agenda-file-regexp' will be part of the file list.
2175 If the value of the variable is not a list but a single file name, then
2176 the list of agenda files is actually stored and maintained in that file, one
2177 agenda file per line."
2180 (repeat :tag
"List of files and directories" file
)
2181 (file :tag
"Store list in a file\n" :value
"~/.agenda_files")))
2183 (defcustom org-agenda-file-regexp
"\\`[^.].*\\.org\\'"
2184 "Regular expression to match files for `org-agenda-files'.
2185 If any element in the list in that variable contains a directory instead
2186 of a normal file, all files in that directory that are matched by this
2187 regular expression will be included."
2191 (defcustom org-agenda-skip-unavailable-files nil
2192 "t means to just skip non-reachable files in `org-agenda-files'.
2193 Nil means to remove them, after a query, from the list."
2197 (defcustom org-agenda-multi-occur-extra-files nil
2198 "List of extra files to be searched by `org-occur-in-agenda-files'.
2199 The files in `org-agenda-files' are always searched."
2201 :type
'(repeat file
))
2203 (defcustom org-agenda-confirm-kill
1
2204 "When set, remote killing from the agenda buffer needs confirmation.
2205 When t, a confirmation is always needed. When a number N, confirmation is
2206 only needed when the text to be killed contains more than N non-white lines."
2209 (const :tag
"Never" nil
)
2210 (const :tag
"Always" t
)
2211 (number :tag
"When more than N lines")))
2213 (defcustom org-calendar-to-agenda-key
[?c
]
2214 "The key to be installed in `calendar-mode-map' for switching to the agenda.
2215 The command `org-calendar-goto-agenda' will be bound to this key. The
2216 default is the character `c' because then `c' can be used to switch back and
2217 forth between agenda and calendar."
2221 (defcustom org-agenda-compact-blocks nil
2222 "Non-nil means, make the block agenda more compact.
2223 This is done by leaving out unnecessary lines."
2227 (defgroup org-agenda-export nil
2228 "Options concerning exporting agenda views in Org-mode."
2229 :tag
"Org Agenda Export"
2232 (defcustom org-agenda-with-colors t
2233 "Non-nil means, use colors in agenda views."
2234 :group
'org-agenda-export
2237 (defcustom org-agenda-exporter-settings nil
2238 "Alist of variable/value pairs that should be active during agenda export.
2239 This is a good place to set uptions for ps-print and for htmlize."
2240 :group
'org-agenda-export
2244 (sexp :tag
"Value"))))
2246 (defcustom org-agenda-export-html-style
""
2247 "The style specification for exported HTML Agenda files.
2248 If this variable contains a string, it will replace the default <style>
2249 section as produced by `htmlize'.
2250 Since there are different ways of setting style information, this variable
2251 needs to contain the full HTML structure to provide a style, including the
2252 surrounding HTML tags. The style specifications should include definitions
2253 the fonts used by the agenda, here is an example:
2255 <style type=\"text/css\">
2256 p { font-weight: normal; color: gray; }
2257 .org-agenda-structure {
2263 color: #cc6666;Week-agenda:
2269 .title { text-align: center; }
2270 .todo, .deadline { color: red; }
2271 .done { color: green; }
2274 or, if you want to keep the style in a file,
2276 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
2278 As the value of this option simply gets inserted into the HTML <head> header,
2279 you can \"misuse\" it to also add other text to the header. However,
2280 <style>...</style> is required, if not present the variable will be ignored."
2281 :group
'org-agenda-export
2282 :group
'org-export-html
2285 (defgroup org-agenda-custom-commands nil
2286 "Options concerning agenda views in Org-mode."
2287 :tag
"Org Agenda Custom Commands"
2290 (defcustom org-agenda-custom-commands nil
2291 "Custom commands for the agenda.
2292 These commands will be offered on the splash screen displayed by the
2293 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
2295 (key desc type match options files)
2297 key The key (one or more characters as a string) to be associated
2299 desc A description of the commend, when omitted or nil, a default
2300 description is built using MATCH.
2301 type The command type, any of the following symbols:
2302 todo Entries with a specific TODO keyword, in all agenda files.
2303 tags Tags match in all agenda files.
2304 tags-todo Tags match in all agenda files, TODO entries only.
2305 todo-tree Sparse tree of specific TODO keyword in *current* file.
2306 tags-tree Sparse tree with all tags matches in *current* file.
2307 occur-tree Occur sparse tree for *current* file.
2308 ... A user-defined function.
2309 match What to search for:
2310 - a single keyword for TODO keyword searches
2311 - a tags match expression for tags searches
2312 - a regular expression for occur searches
2313 options A list of option settings, similar to that in a let form, so like
2314 this: ((opt1 val1) (opt2 val2) ...)
2315 files A list of files file to write the produced agenda buffer to
2316 with the command `org-store-agenda-views'.
2317 If a file name ends in \".html\", an HTML version of the buffer
2318 is written out. If it ends in \".ps\", a postscript version is
2319 produced. Otherwide, only the plain text is written to the file.
2321 You can also define a set of commands, to create a composite agenda buffer.
2322 In this case, an entry looks like this:
2324 (key desc (cmd1 cmd2 ...) general-options file)
2328 desc A description string to be displayed in the dispatcher menu.
2329 cmd An agenda command, similar to the above. However, tree commands
2330 are no allowed, but instead you can get agenda and global todo list.
2331 So valid commands for a set are:
2335 (todo \"match\" options files)
2336 (tags \"match\" options files)
2337 (tags-todo \"match\" options files)
2339 Each command can carry a list of options, and another set of options can be
2340 given for the whole set of commands. Individual command options take
2341 precedence over the general options.
2343 When using several characters as key to a command, the first characters
2344 are prefix commands. For the dispatcher to display useful information, you
2345 should provide a description for the prefix, like
2347 (setq org-agenda-custom-commands
2348 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
2349 (\"hl\" tags \"+HOME+Lisa\")
2350 (\"hp\" tags \"+HOME+Peter\")
2351 (\"hk\" tags \"+HOME+Kim\")))"
2352 :group
'org-agenda-custom-commands
2354 (choice :value
("a" "" tags
"" nil
)
2355 (list :tag
"Single command"
2356 (string :tag
"Access Key(s) ")
2357 (option (string :tag
"Description"))
2359 (const :tag
"Agenda" agenda
)
2360 (const :tag
"TODO list" alltodo
)
2361 (const :tag
"Stuck projects" stuck
)
2362 (const :tag
"Tags search (all agenda files)" tags
)
2363 (const :tag
"Tags search of TODO entries (all agenda files)" tags-todo
)
2364 (const :tag
"TODO keyword search (all agenda files)" todo
)
2365 (const :tag
"Tags sparse tree (current buffer)" tags-tree
)
2366 (const :tag
"TODO keyword tree (current buffer)" todo-tree
)
2367 (const :tag
"Occur tree (current buffer)" occur-tree
)
2368 (sexp :tag
"Other, user-defined function"))
2369 (string :tag
"Match")
2370 (repeat :tag
"Local options"
2371 (list (variable :tag
"Option") (sexp :tag
"Value")))
2372 (option (repeat :tag
"Export" (file :tag
"Export to"))))
2373 (list :tag
"Command series, all agenda files"
2374 (string :tag
"Access Key(s)")
2375 (string :tag
"Description ")
2378 (const :tag
"Agenda" (agenda))
2379 (const :tag
"TODO list" (alltodo))
2380 (const :tag
"Stuck projects" (stuck))
2381 (list :tag
"Tags search"
2382 (const :format
"" tags
)
2383 (string :tag
"Match")
2384 (repeat :tag
"Local options"
2385 (list (variable :tag
"Option")
2386 (sexp :tag
"Value"))))
2388 (list :tag
"Tags search, TODO entries only"
2389 (const :format
"" tags-todo
)
2390 (string :tag
"Match")
2391 (repeat :tag
"Local options"
2392 (list (variable :tag
"Option")
2393 (sexp :tag
"Value"))))
2395 (list :tag
"TODO keyword search"
2396 (const :format
"" todo
)
2397 (string :tag
"Match")
2398 (repeat :tag
"Local options"
2399 (list (variable :tag
"Option")
2400 (sexp :tag
"Value"))))
2402 (list :tag
"Other, user-defined function"
2403 (symbol :tag
"function")
2404 (string :tag
"Match")
2405 (repeat :tag
"Local options"
2406 (list (variable :tag
"Option")
2407 (sexp :tag
"Value"))))))
2409 (repeat :tag
"General options"
2410 (list (variable :tag
"Option")
2411 (sexp :tag
"Value")))
2412 (option (repeat :tag
"Export" (file :tag
"Export to"))))
2413 (cons :tag
"Prefix key documentation"
2414 (string :tag
"Access Key(s)")
2415 (string :tag
"Description ")))))
2417 (defcustom org-stuck-projects
2418 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil
"")
2419 "How to identify stuck projects.
2420 This is a list of four items:
2421 1. A tags/todo matcher string that is used to identify a project.
2422 The entire tree below a headline matched by this is considered one project.
2423 2. A list of TODO keywords identifying non-stuck projects.
2424 If the project subtree contains any headline with one of these todo
2425 keywords, the project is considered to be not stuck. If you specify
2426 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
2427 3. A list of tags identifying non-stuck projects.
2428 If the project subtree contains any headline with one of these tags,
2429 the project is considered to be not stuck. If you specify \"*\" as
2430 a tag, any tag will mark the project unstuck.
2431 4. An arbitrary regular expression matching non-stuck projects.
2433 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
2434 or `C-c a #' to produce the list."
2435 :group
'org-agenda-custom-commands
2437 (string :tag
"Tags/TODO match to identify a project")
2438 (repeat :tag
"Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
2439 (repeat :tag
"Projects are *not* stuck if they have an entry with TAG being any of" (string))
2440 (regexp :tag
"Projects are *not* stuck if this regexp matches\ninside the subtree")))
2443 (defgroup org-agenda-skip nil
2444 "Options concerning skipping parts of agenda files."
2445 :tag
"Org Agenda Skip"
2448 (defcustom org-agenda-todo-list-sublevels t
2449 "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
2450 When nil, the sublevels of a TODO entry are not checked, resulting in
2451 potentially much shorter TODO lists."
2452 :group
'org-agenda-skip
2456 (defcustom org-agenda-todo-ignore-with-date nil
2457 "Non-nil means, don't show entries with a date in the global todo list.
2458 You can use this if you prefer to mark mere appointments with a TODO keyword,
2459 but don't want them to show up in the TODO list.
2460 When this is set, it also covers deadlines and scheduled items, the settings
2461 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
2463 :group
'org-agenda-skip
2467 (defcustom org-agenda-todo-ignore-scheduled nil
2468 "Non-nil means, don't show scheduled entries in the global todo list.
2469 The idea behind this is that by scheduling it, you have already taken care
2471 See also `org-agenda-todo-ignore-with-date'."
2472 :group
'org-agenda-skip
2476 (defcustom org-agenda-todo-ignore-deadlines nil
2477 "Non-nil means, don't show near deadline entries in the global todo list.
2478 Near means closer than `org-deadline-warning-days' days.
2479 The idea behind this is that such items will appear in the agenda anyway.
2480 See also `org-agenda-todo-ignore-with-date'."
2481 :group
'org-agenda-skip
2485 (defcustom org-agenda-skip-scheduled-if-done nil
2486 "Non-nil means don't show scheduled items in agenda when they are done.
2487 This is relevant for the daily/weekly agenda, not for the TODO list. And
2488 it applies only to the actual date of the scheduling. Warnings about
2489 an item with a past scheduling dates are always turned off when the item
2491 :group
'org-agenda-skip
2494 (defcustom org-agenda-skip-deadline-if-done nil
2495 "Non-nil means don't show deadines when the corresponding item is done.
2496 When nil, the deadline is still shown and should give you a happy feeling.
2497 This is relevant for the daily/weekly agenda. And it applied only to the
2498 actualy date of the deadline. Warnings about approching and past-due
2499 deadlines are always turned off when the item is DONE."
2500 :group
'org-agenda-skip
2503 (defcustom org-agenda-skip-timestamp-if-done nil
2504 "Non-nil means don't select item by timestamp or -range if it is DONE."
2505 :group
'org-agenda-skip
2508 (defcustom org-timeline-show-empty-dates
3
2509 "Non-nil means, `org-timeline' also shows dates without an entry.
2510 When nil, only the days which actually have entries are shown.
2511 When t, all days between the first and the last date are shown.
2512 When an integer, show also empty dates, but if there is a gap of more than
2513 N days, just insert a special line indicating the size of the gap."
2514 :group
'org-agenda-skip
2516 (const :tag
"None" nil
)
2517 (const :tag
"All" t
)
2518 (number :tag
"at most")))
2521 (defgroup org-agenda-startup nil
2522 "Options concerning initial settings in the Agenda in Org Mode."
2523 :tag
"Org Agenda Startup"
2526 (defcustom org-finalize-agenda-hook nil
2527 "Hook run just before displaying an agenda buffer."
2528 :group
'org-agenda-startup
2531 (defcustom org-agenda-mouse-1-follows-link nil
2532 "Non-nil means, mouse-1 on a link will follow the link in the agenda.
2533 A longer mouse click will still set point. Does not wortk on XEmacs.
2534 Needs to be set before org.el is loaded."
2535 :group
'org-agenda-startup
2538 (defcustom org-agenda-start-with-follow-mode nil
2539 "The initial value of follow-mode in a newly created agenda window."
2540 :group
'org-agenda-startup
2543 (defgroup org-agenda-windows nil
2544 "Options concerning the windows used by the Agenda in Org Mode."
2545 :tag
"Org Agenda Windows"
2548 (defcustom org-agenda-window-setup
'reorganize-frame
2549 "How the agenda buffer should be displayed.
2550 Possible values for this option are:
2552 current-window Show agenda in the current window, keeping all other windows.
2553 other-frame Use `switch-to-buffer-other-frame' to display agenda.
2554 other-window Use `switch-to-buffer-other-window' to display agenda.
2555 reorganize-frame Show only two windows on the current frame, the current
2556 window and the agenda.
2557 See also the variable `org-agenda-restore-windows-after-quit'."
2558 :group
'org-agenda-windows
2560 (const current-window
)
2562 (const other-window
)
2563 (const reorganize-frame
)))
2565 (defcustom org-agenda-window-frame-fractions
'(0.5 .
0.75)
2566 "The min and max height of the agenda window as a fraction of frame height.
2567 The value of the variable is a cons cell with two numbers between 0 and 1.
2568 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
2569 :group
'org-agenda-windows
2570 :type
'(cons (number :tag
"Minimum") (number :tag
"Maximum")))
2572 (defcustom org-agenda-restore-windows-after-quit nil
2573 "Non-nil means, restore window configuration open exiting agenda.
2574 Before the window configuration is changed for displaying the agenda,
2575 the current status is recorded. When the agenda is exited with
2576 `q' or `x' and this option is set, the old state is restored. If
2577 `org-agenda-window-setup' is `other-frame', the value of this
2578 option will be ignored.."
2579 :group
'org-agenda-windows
2582 (defcustom org-indirect-buffer-display
'other-window
2583 "How should indirect tree buffers be displayed?
2584 This applies to indirect buffers created with the commands
2585 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
2587 current-window Display in the current window
2588 other-window Just display in another window.
2589 dedicated-frame Create one new frame, and re-use it each time.
2590 new-frame Make a new frame each time. Note that in this case
2591 previously-made indirect buffers are kept, and you need to
2592 kill these buffers yourself."
2593 :group
'org-structure
2594 :group
'org-agenda-windows
2596 (const :tag
"In current window" current-window
)
2597 (const :tag
"In current frame, other window" other-window
)
2598 (const :tag
"Each time a new frame" new-frame
)
2599 (const :tag
"One dedicated frame" dedicated-frame
)))
2601 (defgroup org-agenda-daily
/weekly nil
2602 "Options concerning the daily/weekly agenda."
2603 :tag
"Org Agenda Daily/Weekly"
2606 (defcustom org-agenda-ndays
7
2607 "Number of days to include in overview display.
2609 :group
'org-agenda-daily
/weekly
2612 (defcustom org-agenda-start-on-weekday
1
2613 "Non-nil means, start the overview always on the specified weekday.
2614 0 denotes Sunday, 1 denotes Monday etc.
2615 When nil, always start on the current day."
2616 :group
'org-agenda-daily
/weekly
2617 :type
'(choice (const :tag
"Today" nil
)
2618 (number :tag
"Weekday No.")))
2620 (defcustom org-agenda-show-all-dates t
2621 "Non-nil means, `org-agenda' shows every day in the selected range.
2622 When nil, only the days which actually have entries are shown."
2623 :group
'org-agenda-daily
/weekly
2626 (defcustom org-agenda-format-date
'org-agenda-format-date-aligned
2627 "Format string for displaying dates in the agenda.
2628 Used by the daily/weekly agenda and by the timeline. This should be
2629 a format string understood by `format-time-string', or a function returning
2630 the formatted date as a string. The function must take a single argument,
2631 a calendar-style date list like (month day year)."
2632 :group
'org-agenda-daily
/weekly
2634 (string :tag
"Format string")
2635 (function :tag
"Function")))
2637 (defun org-agenda-format-date-aligned (date)
2638 "Format a date string for display in the daily/weekly agenda, or timeline.
2639 This function makes sure that dates are aligned for easy reading."
2640 (format "%-9s %2d %s %4d"
2641 (calendar-day-name date
)
2642 (extract-calendar-day date
)
2643 (calendar-month-name (extract-calendar-month date
))
2644 (extract-calendar-year date
)))
2646 (defcustom org-agenda-include-diary nil
2647 "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
2648 :group
'org-agenda-daily
/weekly
2651 (defcustom org-agenda-include-all-todo nil
2652 "Set means weekly/daily agenda will always contain all TODO entries.
2653 The TODO entries will be listed at the top of the agenda, before
2654 the entries for specific days."
2655 :group
'org-agenda-daily
/weekly
2658 (defcustom org-agenda-repeating-timestamp-show-all t
2659 "Non-nil means, show all occurences of a repeating stamp in the agenda.
2660 When nil, only one occurence is shown, either today or the
2661 nearest into the future."
2662 :group
'org-agenda-daily
/weekly
2665 (defcustom org-deadline-warning-days
14
2666 "No. of days before expiration during which a deadline becomes active.
2667 This variable governs the display in sparse trees and in the agenda.
2668 When negative, it means use this number (the absolute value of it)
2669 even if a deadline has a different individual lead time specified."
2671 :group
'org-agenda-daily
/weekly
2674 (defcustom org-scheduled-past-days
10000
2675 "No. of days to continue listing scheduled items that are not marked DONE.
2676 When an item is scheduled on a date, it shows up in the agenda on this
2677 day and will be listed until it is marked done for the number of days
2679 :group
'org-agenda-daily
/weekly
2682 (defgroup org-agenda-time-grid nil
2683 "Options concerning the time grid in the Org-mode Agenda."
2684 :tag
"Org Agenda Time Grid"
2687 (defcustom org-agenda-use-time-grid t
2688 "Non-nil means, show a time grid in the agenda schedule.
2689 A time grid is a set of lines for specific times (like every two hours between
2690 8:00 and 20:00). The items scheduled for a day at specific times are
2691 sorted in between these lines.
2692 For details about when the grid will be shown, and what it will look like, see
2693 the variable `org-agenda-time-grid'."
2694 :group
'org-agenda-time-grid
2697 (defcustom org-agenda-time-grid
2698 '((daily today require-timed
)
2700 (800 1000 1200 1400 1600 1800 2000))
2702 "The settings for time grid for agenda display.
2703 This is a list of three items. The first item is again a list. It contains
2704 symbols specifying conditions when the grid should be displayed:
2706 daily if the agenda shows a single day
2707 weekly if the agenda shows an entire week
2708 today show grid on current date, independent of daily/weekly display
2709 require-timed show grid only if at least one item has a time specification
2711 The second item is a string which will be places behing the grid time.
2713 The third item is a list of integers, indicating the times that should have
2715 :group
'org-agenda-time-grid
2718 (set :greedy t
:tag
"Grid Display Options"
2719 (const :tag
"Show grid in single day agenda display" daily
)
2720 (const :tag
"Show grid in weekly agenda display" weekly
)
2721 (const :tag
"Always show grid for today" today
)
2722 (const :tag
"Show grid only if any timed entries are present"
2724 (const :tag
"Skip grid times already present in an entry"
2726 (string :tag
"Grid String")
2727 (repeat :tag
"Grid Times" (integer :tag
"Time"))))
2729 (defgroup org-agenda-sorting nil
2730 "Options concerning sorting in the Org-mode Agenda."
2731 :tag
"Org Agenda Sorting"
2734 (defconst org-sorting-choice
2736 (const time-up
) (const time-down
)
2737 (const category-keep
) (const category-up
) (const category-down
)
2738 (const tag-down
) (const tag-up
)
2739 (const priority-up
) (const priority-down
))
2742 (defcustom org-agenda-sorting-strategy
2743 '((agenda time-up category-keep priority-down
)
2744 (todo category-keep priority-down
)
2745 (tags category-keep priority-down
))
2746 "Sorting structure for the agenda items of a single day.
2747 This is a list of symbols which will be used in sequence to determine
2748 if an entry should be listed before another entry. The following
2749 symbols are recognized:
2751 time-up Put entries with time-of-day indications first, early first
2752 time-down Put entries with time-of-day indications first, late first
2753 category-keep Keep the default order of categories, corresponding to the
2754 sequence in `org-agenda-files'.
2755 category-up Sort alphabetically by category, A-Z.
2756 category-down Sort alphabetically by category, Z-A.
2757 tag-up Sort alphabetically by last tag, A-Z.
2758 tag-down Sort alphabetically by last tag, Z-A.
2759 priority-up Sort numerically by priority, high priority last.
2760 priority-down Sort numerically by priority, high priority first.
2762 The different possibilities will be tried in sequence, and testing stops
2763 if one comparison returns a \"not-equal\". For example, the default
2764 '(time-up category-keep priority-down)
2765 means: Pull out all entries having a specified time of day and sort them,
2766 in order to make a time schedule for the current day the first thing in the
2767 agenda listing for the day. Of the entries without a time indication, keep
2768 the grouped in categories, don't sort the categories, but keep them in
2769 the sequence given in `org-agenda-files'. Within each category sort by
2772 Leaving out `category-keep' would mean that items will be sorted across
2773 categories by priority.
2775 Instead of a single list, this can also be a set of list for specific
2776 contents, with a context symbol in the car of the list, any of
2777 `agenda', `todo', `tags' for the corresponding agenda views."
2778 :group
'org-agenda-sorting
2780 (repeat :tag
"General" ,org-sorting-choice
)
2781 (list :tag
"Individually"
2782 (cons (const :tag
"Strategy for Weekly/Daily agenda" agenda
)
2783 (repeat ,org-sorting-choice
))
2784 (cons (const :tag
"Strategy for TODO lists" todo
)
2785 (repeat ,org-sorting-choice
))
2786 (cons (const :tag
"Strategy for Tags matches" tags
)
2787 (repeat ,org-sorting-choice
)))))
2789 (defcustom org-sort-agenda-notime-is-late t
2790 "Non-nil means, items without time are considered late.
2791 This is only relevant for sorting. When t, items which have no explicit
2792 time like 15:30 will be considered as 99:01, i.e. later than any items which
2793 do have a time. When nil, the default time is before 0:00. You can use this
2794 option to decide if the schedule for today should come before or after timeless
2796 :group
'org-agenda-sorting
2799 (defgroup org-agenda-line-format nil
2800 "Options concerning the entry prefix in the Org-mode agenda display."
2801 :tag
"Org Agenda Line Format"
2804 (defcustom org-agenda-prefix-format
2805 '((agenda .
" %-12:c%?-12t% s")
2809 "Format specifications for the prefix of items in the agenda views.
2810 An alist with four entries, for the different agenda types. The keys to the
2811 sublists are `agenda', `timeline', `todo', and `tags'. The values
2813 This format works similar to a printf format, with the following meaning:
2815 %c the category of the item, \"Diary\" for entries from the diary, or
2816 as given by the CATEGORY keyword or derived from the file name.
2817 %T the *last* tag of the item. Last because inherited tags come
2819 %t the time-of-day specification if one applies to the entry, in the
2821 %s Scheduling/Deadline information, a short string
2823 All specifiers work basically like the standard `%s' of printf, but may
2824 contain two additional characters: A question mark just after the `%' and
2825 a whitespace/punctuation character just before the final letter.
2827 If the first character after `%' is a question mark, the entire field
2828 will only be included if the corresponding value applies to the
2829 current entry. This is useful for fields which should have fixed
2830 width when present, but zero width when absent. For example,
2831 \"%?-12t\" will result in a 12 character time field if a time of the
2832 day is specified, but will completely disappear in entries which do
2835 If there is punctuation or whitespace character just before the final
2836 format letter, this character will be appended to the field value if
2837 the value is not empty. For example, the format \"%-12:c\" leads to
2838 \"Diary: \" if the category is \"Diary\". If the category were be
2839 empty, no additional colon would be interted.
2841 The default value of this option is \" %-12:c%?-12t% s\", meaning:
2842 - Indent the line with two space characters
2843 - Give the category in a 12 chars wide field, padded with whitespace on
2844 the right (because of `-'). Append a colon if there is a category
2846 - If there is a time-of-day, put it into a 12 chars wide field. If no
2847 time, don't put in an empty field, just skip it (because of '?').
2848 - Finally, put the scheduling information and append a whitespace.
2850 As another example, if you don't want the time-of-day of entries in
2851 the prefix, you could use:
2853 (setq org-agenda-prefix-format \" %-11:c% s\")
2855 See also the variables `org-agenda-remove-times-when-in-prefix' and
2856 `org-agenda-remove-tags'."
2858 (string :tag
"General format")
2859 (list :greedy t
:tag
"View dependent"
2860 (cons (const agenda
) (string :tag
"Format"))
2861 (cons (const timeline
) (string :tag
"Format"))
2862 (cons (const todo
) (string :tag
"Format"))
2863 (cons (const tags
) (string :tag
"Format"))))
2864 :group
'org-agenda-line-format
)
2866 (defvar org-prefix-format-compiled nil
2867 "The compiled version of the most recently used prefix format.
2868 See the variable `org-agenda-prefix-format'.")
2870 (defcustom org-agenda-todo-keyword-format
"%-1s"
2871 "Format for the TODO keyword in agenda lines.
2872 Set this to something like \"%-12s\" if you want all TODO keywords
2873 to occupy a fixed space in the agenda display."
2874 :group
'org-agenda-line-format
2877 (defcustom org-agenda-scheduled-leaders
'("Scheduled: " "Sched.%2dx: ")
2878 "Text preceeding scheduled items in the agenda view.
2879 This is a list with two strings. The first applies when the item is
2880 scheduled on the current day. The second applies when it has been scheduled
2881 previously, it may contain a %d to capture how many days ago the item was
2883 :group
'org-agenda-line-format
2885 (string :tag
"Scheduled today ")
2886 (string :tag
"Scheduled previously")))
2888 (defcustom org-agenda-deadline-leaders
'("Deadline: " "In %3d d.: ")
2889 "Text preceeding deadline items in the agenda view.
2890 This is a list with two strings. The first applies when the item has its
2891 deadline on the current day. The second applies when it is in the past or
2892 in the future, it may contain %d to capture how many days away the deadline
2894 :group
'org-agenda-line-format
2896 (string :tag
"Deadline today ")
2897 (string :tag
"Deadline relative")))
2899 (defcustom org-agenda-remove-times-when-in-prefix t
2900 "Non-nil means, remove duplicate time specifications in agenda items.
2901 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
2902 time-of-day specification in a headline or diary entry is extracted and
2903 placed into the prefix. If this option is non-nil, the original specification
2904 \(a timestamp or -range, or just a plain time(range) specification like
2905 11:30-4pm) will be removed for agenda display. This makes the agenda less
2907 The option can be t or nil. It may also be the symbol `beg', indicating
2908 that the time should only be removed what it is located at the beginning of
2909 the headline/diary entry."
2910 :group
'org-agenda-line-format
2912 (const :tag
"Always" t
)
2913 (const :tag
"Never" nil
)
2914 (const :tag
"When at beginning of entry" beg
)))
2917 (defcustom org-agenda-default-appointment-duration nil
2918 "Default duration for appointments that only have a starting time.
2919 When nil, no duration is specified in such cases.
2920 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
2921 :group
'org-agenda-line-format
2923 (integer :tag
"Minutes")
2924 (const :tag
"No default duration")))
2927 (defcustom org-agenda-remove-tags nil
2928 "Non-nil means, remove the tags from the headline copy in the agenda.
2929 When this is the symbol `prefix', only remove tags when
2930 `org-agenda-prefix-format' contains a `%T' specifier."
2931 :group
'org-agenda-line-format
2933 (const :tag
"Always" t
)
2934 (const :tag
"Never" nil
)
2935 (const :tag
"When prefix format contains %T" prefix
)))
2937 (if (fboundp 'defvaralias
)
2938 (defvaralias 'org-agenda-remove-tags-when-in-prefix
2939 'org-agenda-remove-tags
))
2941 (defcustom org-agenda-tags-column -
80
2942 "Shift tags in agenda items to this column.
2943 If this number is positive, it specifies the column. If it is negative,
2944 it means that the tags should be flushright to that column. For example,
2945 -80 works well for a normal 80 character screen."
2946 :group
'org-agenda-line-format
2949 (if (fboundp 'defvaralias
)
2950 (defvaralias 'org-agenda-align-tags-to-column
'org-agenda-tags-column
))
2952 (defcustom org-agenda-fontify-priorities t
2953 "Non-nil means, highlight low and high priorities in agenda.
2954 When t, the highest priority entries are bold, lowest priority italic.
2955 This may also be an association list of priority faces. The face may be
2956 a names face, or a list like `(:background \"Red\")'."
2957 :group
'org-agenda-line-format
2959 (const :tag
"Never" nil
)
2960 (const :tag
"Defaults" t
)
2961 (repeat :tag
"Specify"
2962 (list (character :tag
"Priority" :value ?A
)
2963 (sexp :tag
"face")))))
2965 (defgroup org-latex nil
2966 "Options for embedding LaTeX code into Org-mode"
2970 (defcustom org-format-latex-options
2971 '(:foreground default
:background default
:scale
1.0
2972 :html-foreground
"Black" :html-background
"Transparent" :html-scale
1.0
2973 :matchers
("begin" "$" "$$" "\\(" "\\["))
2974 "Options for creating images from LaTeX fragments.
2975 This is a property list with the following properties:
2976 :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
2977 `default' means use the forground of the default face.
2978 :background the background color, or \"Transparent\".
2979 `default' means use the background of the default face.
2980 :scale a scaling factor for the size of the images
2981 :html-foreground, :html-background, :html-scale
2982 The same numbers for HTML export.
2983 :matchers a list indicating which matchers should be used to
2984 find LaTeX fragments. Valid members of this list are:
2985 \"begin\" find environments
2986 \"$\" find math expressions surrounded by $...$
2987 \"$$\" find math expressions surrounded by $$....$$
2988 \"\\(\" find math expressions surrounded by \\(...\\)
2989 \"\\ [\" find math expressions surrounded by \\ [...\\]"
2993 (defcustom org-format-latex-header
"\\documentclass{article}
2994 \\usepackage{fullpage} % do not remove
2995 \\usepackage{amssymb}
2996 \\usepackage[usenames]{color}
2997 \\usepackage{amsmath}
2998 \\usepackage{latexsym}
2999 \\usepackage[mathscr]{eucal}
3000 \\pagestyle{empty} % do not remove"
3001 "The document header used for processing LaTeX fragments."
3005 (defgroup org-export nil
3006 "Options for exporting org-listings."
3010 (defgroup org-export-general nil
3011 "General options for exporting Org-mode files."
3012 :tag
"Org Export General"
3016 (defvar org-export-publishing-directory nil
)
3018 (defcustom org-export-with-special-strings t
3019 "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
3020 When this option is turned on, these strings will be exported as:
3023 -----+----------+--------
3029 This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
3030 :group
'org-export-translation
3033 (defcustom org-export-language-setup
3034 '(("en" "Author" "Date" "Table of Contents")
3035 ("cs" "Autor" "Datum" "Obsah")
3036 ("da" "Ophavsmand" "Dato" "Indhold")
3037 ("de" "Autor" "Datum" "Inhaltsverzeichnis")
3038 ("es" "Autor" "Fecha" "\xcdndice")
3039 ("fr" "Auteur" "Date" "Table des mati\xe8res")
3040 ("it" "Autore" "Data" "Indice")
3041 ("nl" "Auteur" "Datum" "Inhoudsopgave")
3042 ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
3043 ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
3044 "Terms used in export text, translated to different languages.
3045 Use the variable `org-export-default-language' to set the language,
3046 or use the +OPTION lines for a per-file setting."
3047 :group
'org-export-general
3050 (string :tag
"HTML language tag")
3051 (string :tag
"Author")
3052 (string :tag
"Date")
3053 (string :tag
"Table of Contents"))))
3055 (defcustom org-export-default-language
"en"
3056 "The default language of HTML export, as a string.
3057 This should have an association in `org-export-language-setup'."
3058 :group
'org-export-general
3061 (defcustom org-export-skip-text-before-1st-heading t
3062 "Non-nil means, skip all text before the first headline when exporting.
3063 When nil, that text is exported as well."
3064 :group
'org-export-general
3067 (defcustom org-export-headline-levels
3
3068 "The last level which is still exported as a headline.
3069 Inferior levels will produce itemize lists when exported.
3070 Note that a numeric prefix argument to an exporter function overrides
3073 This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
3074 :group
'org-export-general
3077 (defcustom org-export-with-section-numbers t
3078 "Non-nil means, add section numbers to headlines when exporting.
3080 This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
3081 :group
'org-export-general
3084 (defcustom org-export-with-toc t
3085 "Non-nil means, create a table of contents in exported files.
3086 The TOC contains headlines with levels up to`org-export-headline-levels'.
3087 When an integer, include levels up to N in the toc, this may then be
3088 different from `org-export-headline-levels', but it will not be allowed
3089 to be larger than the number of headline levels.
3090 When nil, no table of contents is made.
3092 Headlines which contain any TODO items will be marked with \"(*)\" in
3093 ASCII export, and with red color in HTML output, if the option
3094 `org-export-mark-todo-in-toc' is set.
3096 In HTML output, the TOC will be clickable.
3098 This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
3100 :group
'org-export-general
3102 (const :tag
"No Table of Contents" nil
)
3103 (const :tag
"Full Table of Contents" t
)
3104 (integer :tag
"TOC to level")))
3106 (defcustom org-export-mark-todo-in-toc nil
3107 "Non-nil means, mark TOC lines that contain any open TODO items."
3108 :group
'org-export-general
3111 (defcustom org-export-preserve-breaks nil
3112 "Non-nil means, preserve all line breaks when exporting.
3113 Normally, in HTML output paragraphs will be reformatted. In ASCII
3114 export, line breaks will always be preserved, regardless of this variable.
3116 This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
3117 :group
'org-export-general
3120 (defcustom org-export-with-archived-trees
'headline
3121 "Whether subtrees with the ARCHIVE tag should be exported.
3122 This can have three different values
3123 nil Do not export, pretend this tree is not present
3124 t Do export the entire tree
3125 headline Only export the headline, but skip the tree below it."
3126 :group
'org-export-general
3129 (const :tag
"not at all" nil
)
3130 (const :tag
"headline only" 'headline
)
3131 (const :tag
"entirely" t
)))
3133 (defcustom org-export-author-info t
3134 "Non-nil means, insert author name and email into the exported file.
3136 This option can also be set with the +OPTIONS line,
3137 e.g. \"author-info:nil\"."
3138 :group
'org-export-general
3141 (defcustom org-export-time-stamp-file t
3142 "Non-nil means, insert a time stamp into the exported file.
3143 The time stamp shows when the file was created.
3145 This option can also be set with the +OPTIONS line,
3146 e.g. \"timestamp:nil\"."
3147 :group
'org-export-general
3150 (defcustom org-export-with-timestamps t
3151 "If nil, do not export time stamps and associated keywords."
3152 :group
'org-export-general
3155 (defcustom org-export-remove-timestamps-from-toc t
3156 "If nil, remove timestamps from the table of contents entries."
3157 :group
'org-export-general
3160 (defcustom org-export-with-tags
'not-in-toc
3161 "If nil, do not export tags, just remove them from headlines.
3162 If this is the symbol `not-in-toc', tags will be removed from table of
3163 contents entries, but still be shown in the headlines of the document.
3165 This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
3166 :group
'org-export-general
3168 (const :tag
"Off" nil
)
3169 (const :tag
"Not in TOC" not-in-toc
)
3170 (const :tag
"On" t
)))
3172 (defcustom org-export-with-drawers nil
3173 "Non-nil means, export with drawers like the property drawer.
3174 When t, all drawers are exported. This may also be a list of
3175 drawer names to export."
3176 :group
'org-export-general
3178 (const :tag
"All drawers" t
)
3179 (const :tag
"None" nil
)
3180 (repeat :tag
"Selected drawers"
3181 (string :tag
"Drawer name"))))
3183 (defgroup org-export-translation nil
3184 "Options for translating special ascii sequences for the export backends."
3185 :tag
"Org Export Translation"
3188 (defcustom org-export-with-emphasize t
3189 "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
3190 If the export target supports emphasizing text, the word will be
3191 typeset in bold, italic, or underlined, respectively. Works only for
3192 single words, but you can say: I *really* *mean* *this*.
3193 Not all export backends support this.
3195 This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
3196 :group
'org-export-translation
3199 (defcustom org-export-with-footnotes t
3200 "If nil, export [1] as a footnote marker.
3201 Lines starting with [1] will be formatted as footnotes.
3203 This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
3204 :group
'org-export-translation
3207 (defcustom org-export-with-sub-superscripts t
3208 "Non-nil means, interpret \"_\" and \"^\" for export.
3209 When this option is turned on, you can use TeX-like syntax for sub- and
3210 superscripts. Several characters after \"_\" or \"^\" will be
3211 considered as a single item - so grouping with {} is normally not
3212 needed. For example, the following things will be parsed as single
3213 sub- or superscripts.
3215 10^24 or 10^tau several digits will be considered 1 item.
3216 10^-12 or 10^-tau a leading sign with digits or a word
3217 x^2-y^3 will be read as x^2 - y^3, because items are
3218 terminated by almost any nonword/nondigit char.
3219 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
3221 Still, ambiguity is possible - so when in doubt use {} to enclose the
3222 sub/superscript. If you set this variable to the symbol `{}',
3223 the braces are *required* in order to trigger interpretations as
3224 sub/superscript. This can be helpful in documents that need \"_\"
3225 frequently in plain text.
3227 Not all export backends support this, but HTML does.
3229 This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
3230 :group
'org-export-translation
3232 (const :tag
"Always interpret" t
)
3233 (const :tag
"Only with braces" {})
3234 (const :tag
"Never interpret" nil
)))
3236 (defcustom org-export-with-special-strings t
3237 "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
3238 When this option is turned on, these strings will be exported as:
3244 Not all export backends support this, but HTML does.
3246 This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
3247 :group
'org-export-translation
3250 (defcustom org-export-with-TeX-macros t
3251 "Non-nil means, interpret simple TeX-like macros when exporting.
3252 For example, HTML export converts \\alpha to α and \\AA to Å.
3253 No only real TeX macros will work here, but the standard HTML entities
3254 for math can be used as macro names as well. For a list of supported
3255 names in HTML export, see the constant `org-html-entities'.
3256 Not all export backends support this.
3258 This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
3259 :group
'org-export-translation
3260 :group
'org-export-latex
3263 (defcustom org-export-with-LaTeX-fragments nil
3264 "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
3265 When set, the exporter will find LaTeX environments if the \\begin line is
3266 the first non-white thing on a line. It will also find the math delimiters
3267 like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
3270 This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
3271 :group
'org-export-translation
3272 :group
'org-export-latex
3275 (defcustom org-export-with-fixed-width t
3276 "Non-nil means, lines starting with \":\" will be in fixed width font.
3277 This can be used to have pre-formatted text, fragments of code etc. For
3279 : ;; Some Lisp examples
3282 will be looking just like this in also HTML. See also the QUOTE keyword.
3283 Not all export backends support this.
3285 This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
3286 :group
'org-export-translation
3289 (defcustom org-match-sexp-depth
3
3290 "Number of stacked braces for sub/superscript matching.
3291 This has to be set before loading org.el to be effective."
3292 :group
'org-export-translation
3295 (defgroup org-export-tables nil
3296 "Options for exporting tables in Org-mode."
3297 :tag
"Org Export Tables"
3300 (defcustom org-export-with-tables t
3301 "If non-nil, lines starting with \"|\" define a table.
3304 | Name | Address | Birthday |
3305 |-------------+----------+-----------|
3306 | Arthur Dent | England | 29.2.2100 |
3308 Not all export backends support this.
3310 This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
3311 :group
'org-export-tables
3314 (defcustom org-export-highlight-first-table-line t
3315 "Non-nil means, highlight the first table line.
3316 In HTML export, this means use <th> instead of <td>.
3317 In tables created with table.el, this applies to the first table line.
3318 In Org-mode tables, all lines before the first horizontal separator
3319 line will be formatted with <th> tags."
3320 :group
'org-export-tables
3323 (defcustom org-export-table-remove-special-lines t
3324 "Remove special lines and marking characters in calculating tables.
3325 This removes the special marking character column from tables that are set
3326 up for spreadsheet calculations. It also removes the entire lines
3327 marked with `!', `_', or `^'. The lines with `$' are kept, because
3328 the values of constants may be useful to have."
3329 :group
'org-export-tables
3332 (defcustom org-export-prefer-native-exporter-for-tables nil
3333 "Non-nil means, always export tables created with table.el natively.
3334 Natively means, use the HTML code generator in table.el.
3335 When nil, Org-mode's own HTML generator is used when possible (i.e. if
3336 the table does not use row- or column-spanning). This has the
3337 advantage, that the automatic HTML conversions for math symbols and
3338 sub/superscripts can be applied. Org-mode's HTML generator is also
3340 :group
'org-export-tables
3343 (defgroup org-export-ascii nil
3344 "Options specific for ASCII export of Org-mode files."
3345 :tag
"Org Export ASCII"
3348 (defcustom org-export-ascii-underline
'(?\$ ?\
# ?^ ?\~ ?\
= ?\-
)
3349 "Characters for underlining headings in ASCII export.
3350 In the given sequence, these characters will be used for level 1, 2, ..."
3351 :group
'org-export-ascii
3352 :type
'(repeat character
))
3354 (defcustom org-export-ascii-bullets
'(?
* ?
+ ?-
)
3355 "Bullet characters for headlines converted to lists in ASCII export.
3356 The first character is used for the first lest level generated in this
3357 way, and so on. If there are more levels than characters given here,
3358 the list will be repeated.
3359 Note that plain lists will keep the same bullets as the have in the
3361 :group
'org-export-ascii
3362 :type
'(repeat character
))
3364 (defgroup org-export-xml nil
3365 "Options specific for XML export of Org-mode files."
3366 :tag
"Org Export XML"
3369 (defgroup org-export-html nil
3370 "Options specific for HTML export of Org-mode files."
3371 :tag
"Org Export HTML"
3374 (defcustom org-export-html-coding-system nil
3376 :group
'org-export-html
3377 :type
'coding-system
)
3379 (defcustom org-export-html-extension
"html"
3380 "The extension for exported HTML files."
3381 :group
'org-export-html
3384 (defcustom org-export-html-style
3385 "<style type=\"text/css\">
3387 font-family: Times, serif;
3390 .title { text-align: center; }
3391 .todo { color: red; }
3392 .done { color: green; }
3393 .timestamp { color: grey }
3394 .timestamp-kwd { color: CadetBlue }
3395 .tag { background-color:lightblue; font-weight:normal }
3396 .target { background-color: lavender; }
3398 border: 1pt solid #AEBDCC;
3399 background-color: #F3F5F7;
3401 font-family: courier, monospace;
3403 table { border-collapse: collapse; }
3405 vertical-align: top;
3406 <!--border: 1pt solid #ADB9CC;-->
3409 "The default style specification for exported HTML files.
3410 Since there are different ways of setting style information, this variable
3411 needs to contain the full HTML structure to provide a style, including the
3412 surrounding HTML tags. The style specifications should include definitions
3413 for new classes todo, done, title, and deadline. For example, legal values
3416 <style type=\"text/css\">
3417 p { font-weight: normal; color: gray; }
3418 h1 { color: black; }
3419 .title { text-align: center; }
3420 .todo, .deadline { color: red; }
3421 .done { color: green; }
3424 or, if you want to keep the style in a file,
3426 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
3428 As the value of this option simply gets inserted into the HTML <head> header,
3429 you can \"misuse\" it to add arbitrary text to the header."
3430 :group
'org-export-html
3434 (defcustom org-export-html-title-format
"<h1 class=\"title\">%s</h1>\n"
3435 "Format for typesetting the document title in HTML export."
3436 :group
'org-export-html
3439 (defcustom org-export-html-toplevel-hlevel
2
3440 "The <H> level for level 1 headings in HTML export."
3441 :group
'org-export-html
3444 (defcustom org-export-html-link-org-files-as-html t
3445 "Non-nil means, make file links to `file.org' point to `file.html'.
3446 When org-mode is exporting an org-mode file to HTML, links to
3447 non-html files are directly put into a href tag in HTML.
3448 However, links to other Org-mode files (recognized by the
3449 extension `.org.) should become links to the corresponding html
3450 file, assuming that the linked org-mode file will also be
3452 When nil, the links still point to the plain `.org' file."
3453 :group
'org-export-html
3456 (defcustom org-export-html-inline-images
'maybe
3457 "Non-nil means, inline images into exported HTML pages.
3458 This is done using an <img> tag. When nil, an anchor with href is used to
3459 link to the image. If this option is `maybe', then images in links with
3460 an empty description will be inlined, while images with a description will
3462 :group
'org-export-html
3463 :type
'(choice (const :tag
"Never" nil
)
3464 (const :tag
"Always" t
)
3465 (const :tag
"When there is no description" maybe
)))
3468 (defcustom org-export-html-expand t
3469 "Non-nil means, for HTML export, treat @<...> as HTML tag.
3470 When nil, these tags will be exported as plain text and therefore
3471 not be interpreted by a browser.
3473 This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
3474 :group
'org-export-html
3477 (defcustom org-export-html-table-tag
3478 "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
3479 "The HTML tag that is used to start a table.
3480 This must be a <table> tag, but you may change the options like
3481 borders and spacing."
3482 :group
'org-export-html
3485 (defcustom org-export-table-header-tags
'("<th>" .
"</th>")
3486 "The opening tag for table header fields.
3487 This is customizable so that alignment options can be specified."
3488 :group
'org-export-tables
3489 :type
'(cons (string :tag
"Opening tag") (string :tag
"Closing tag")))
3491 (defcustom org-export-table-data-tags
'("<td>" .
"</td>")
3492 "The opening tag for table data fields.
3493 This is customizable so that alignment options can be specified."
3494 :group
'org-export-tables
3495 :type
'(cons (string :tag
"Opening tag") (string :tag
"Closing tag")))
3497 (defcustom org-export-html-with-timestamp nil
3498 "If non-nil, write `org-export-html-html-helper-timestamp'
3499 into the exported HTML text. Otherwise, the buffer will just be saved
3501 :group
'org-export-html
3504 (defcustom org-export-html-html-helper-timestamp
3505 "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
3506 "The HTML tag used as timestamp delimiter for HTML-helper-mode."
3507 :group
'org-export-html
3510 (defgroup org-export-icalendar nil
3511 "Options specific for iCalendar export of Org-mode files."
3512 :tag
"Org Export iCalendar"
3515 (defcustom org-combined-agenda-icalendar-file
"~/org.ics"
3516 "The file name for the iCalendar file covering all agenda files.
3517 This file is created with the command \\[org-export-icalendar-all-agenda-files].
3518 The file name should be absolute, the file will be overwritten without warning."
3519 :group
'org-export-icalendar
3522 (defcustom org-icalendar-include-todo nil
3523 "Non-nil means, export to iCalendar files should also cover TODO items."
3524 :group
'org-export-icalendar
3526 (const :tag
"None" nil
)
3527 (const :tag
"Unfinished" t
)
3528 (const :tag
"All" all
)))
3530 (defcustom org-icalendar-include-sexps t
3531 "Non-nil means, export to iCalendar files should also cover sexp entries.
3532 These are entries like in the diary, but directly in an Org-mode file."
3533 :group
'org-export-icalendar
3536 (defcustom org-icalendar-include-body
100
3537 "Amount of text below headline to be included in iCalendar export.
3538 This is a number of characters that should maximally be included.
3539 Properties, scheduling and clocking lines will always be removed.
3540 The text will be inserted into the DESCRIPTION field."
3541 :group
'org-export-icalendar
3543 (const :tag
"Nothing" nil
)
3544 (const :tag
"Everything" t
)
3545 (integer :tag
"Max characters")))
3547 (defcustom org-icalendar-combined-name
"OrgMode"
3548 "Calendar name for the combined iCalendar representing all agenda files."
3549 :group
'org-export-icalendar
3552 (defgroup org-font-lock nil
3553 "Font-lock settings for highlighting in Org-mode."
3554 :tag
"Org Font Lock"
3557 (defcustom org-level-color-stars-only nil
3558 "Non-nil means fontify only the stars in each headline.
3559 When nil, the entire headline is fontified.
3560 Changing it requires restart of `font-lock-mode' to become effective
3561 also in regions already fontified."
3562 :group
'org-font-lock
3565 (defcustom org-hide-leading-stars nil
3566 "Non-nil means, hide the first N-1 stars in a headline.
3567 This works by using the face `org-hide' for these stars. This
3568 face is white for a light background, and black for a dark
3569 background. You may have to customize the face `org-hide' to
3571 Changing it requires restart of `font-lock-mode' to become effective
3572 also in regions already fontified.
3573 You may also set this on a per-file basis by adding one of the following
3574 lines to the buffer:
3576 #+STARTUP: hidestars
3577 #+STARTUP: showstars"
3578 :group
'org-font-lock
3581 (defcustom org-fontify-done-headline nil
3582 "Non-nil means, change the face of a headline if it is marked DONE.
3583 Normally, only the TODO/DONE keyword indicates the state of a headline.
3584 When this is non-nil, the headline after the keyword is set to the
3585 `org-headline-done' as an additional indication."
3586 :group
'org-font-lock
3589 (defcustom org-fontify-emphasized-text t
3590 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
3591 Changing this variable requires a restart of Emacs to take effect."
3592 :group
'org-font-lock
3595 (defcustom org-highlight-latex-fragments-and-specials nil
3596 "Non-nil means, fontify what is treated specially by the exporters."
3597 :group
'org-font-lock
3600 (defcustom org-hide-emphasis-markers nil
3601 "Non-nil mean font-lock should hide the emphasis marker characters."
3602 :group
'org-font-lock
3605 (defvar org-emph-re nil
3606 "Regular expression for matching emphasis.")
3607 (defvar org-verbatim-re nil
3608 "Regular expression for matching verbatim text.")
3609 (defvar org-emphasis-regexp-components
) ; defined just below
3610 (defvar org-emphasis-alist
) ; defined just below
3611 (defun org-set-emph-re (var val
)
3612 "Set variable and compute the emphasis regular expression."
3614 (when (and (boundp 'org-emphasis-alist
)
3615 (boundp 'org-emphasis-regexp-components
)
3616 org-emphasis-alist org-emphasis-regexp-components
)
3617 (let* ((e org-emphasis-regexp-components
)
3623 (stacked (and nil
(nth 5 e
))) ; stacked is no longer allowed, forced to nil
3624 (body1 (concat body
"*?"))
3625 (markers (mapconcat 'car org-emphasis-alist
""))
3626 (vmarkers (mapconcat
3627 (lambda (x) (if (eq (nth 4 x
) 'verbatim
) (car x
) ""))
3628 org-emphasis-alist
"")))
3629 ;; make sure special characters appear at the right position in the class
3630 (if (string-match "\\^" markers
)
3631 (setq markers
(concat (replace-match "" t t markers
) "^")))
3632 (if (string-match "-" markers
)
3633 (setq markers
(concat (replace-match "" t t markers
) "-")))
3634 (if (string-match "\\^" vmarkers
)
3635 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "^")))
3636 (if (string-match "-" vmarkers
)
3637 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "-")))
3639 (setq body1
(concat body1
"\\(?:\n" body
"*?\\)\\{0,"
3640 (int-to-string nl
) "\\}")))
3643 (concat "\\([" pre
(if (and nil stacked
) markers
) "]\\|^\\)"
3645 "\\([" markers
"]\\)"
3648 "[^" border
(if (and nil stacked
) markers
) "]"
3650 "[^" border
(if (and nil stacked
) markers
) "]"
3653 "\\([" post
(if (and nil stacked
) markers
) "]\\|$\\)"))
3654 (setq org-verbatim-re
3655 (concat "\\([" pre
"]\\|^\\)"
3657 "\\([" vmarkers
"]\\)"
3665 "\\([" post
"]\\|$\\)")))))
3667 (defcustom org-emphasis-regexp-components
3668 '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
3669 "Components used to build the regular expression for emphasis.
3670 This is a list with 6 entries. Terminology: In an emphasis string
3671 like \" *strong word* \", we call the initial space PREMATCH, the final
3672 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
3673 and \"trong wor\" is the body. The different components in this variable
3674 specify what is allowed/forbidden in each part:
3676 pre Chars allowed as prematch. Beginning of line will be allowed too.
3677 post Chars allowed as postmatch. End of line will be allowed too.
3678 border The chars *forbidden* as border characters.
3679 body-regexp A regexp like \".\" to match a body character. Don't use
3680 non-shy groups here, and don't allow newline here.
3681 newline The maximum number of newlines allowed in an emphasis exp.
3683 Use customize to modify this, or restart Emacs after changing it."
3684 :group
'org-font-lock
3685 :set
'org-set-emph-re
3687 (sexp :tag
"Allowed chars in pre ")
3688 (sexp :tag
"Allowed chars in post ")
3689 (sexp :tag
"Forbidden chars in border ")
3690 (sexp :tag
"Regexp for body ")
3691 (integer :tag
"number of newlines allowed")
3692 (option (boolean :tag
"Stacking (DISABLED) "))))
3694 (defcustom org-emphasis-alist
3695 '(("*" bold
"<b>" "</b>")
3696 ("/" italic
"<i>" "</i>")
3697 ("_" underline
"<u>" "</u>")
3698 ("=" org-code
"<code>" "</code>" verbatim
)
3699 ("~" org-verbatim
"" "" verbatim
)
3700 ("+" (:strike-through t
) "<del>" "</del>")
3702 "Special syntax for emphasized text.
3703 Text starting and ending with a special character will be emphasized, for
3704 example *bold*, _underlined_ and /italic/. This variable sets the marker
3705 characters, the face to be used by font-lock for highlighting in Org-mode
3706 Emacs buffers, and the HTML tags to be used for this.
3707 Use customize to modify this, or restart Emacs after changing it."
3708 :group
'org-font-lock
3709 :set
'org-set-emph-re
3712 (string :tag
"Marker character")
3714 (face :tag
"Font-lock-face")
3715 (plist :tag
"Face property list"))
3716 (string :tag
"HTML start tag")
3717 (string :tag
"HTML end tag")
3718 (option (const verbatim
)))))
3722 (defgroup org-faces nil
3723 "Faces in Org-mode."
3725 :group
'org-font-lock
)
3727 (defun org-compatible-face (inherits specs
)
3728 "Make a compatible face specification.
3729 If INHERITS is an existing face and if the Emacs version supports it,
3730 just inherit the face. If not, use SPECS to define the face.
3731 XEmacs and Emacs 21 do not know about the `min-colors' attribute.
3732 For them we convert a (min-colors 8) entry to a `tty' entry and move it
3733 to the top of the list. The `min-colors' attribute will be removed from
3734 any other entries, and any resulting duplicates will be removed entirely."
3736 ((and inherits
(facep inherits
)
3737 (not (featurep 'xemacs
)) (> emacs-major-version
22))
3738 ;; In Emacs 23, we use inheritance where possible.
3739 ;; We only do this in Emacs 23, because only there the outline
3740 ;; faces have been changed to the original org-mode-level-faces.
3741 (list (list t
:inherit inherits
)))
3742 ((or (featurep 'xemacs
) (< emacs-major-version
22))
3743 ;; These do not understand the `min-colors' attribute.
3745 (while (setq e
(pop specs
))
3747 ((memq (car e
) '(t default
)) (push e r
))
3748 ((setq a
(member '(min-colors 8) (car e
)))
3749 (nconc r
(list (cons (cons '(type tty
) (delq (car a
) (car e
)))
3751 ((setq a
(assq 'min-colors
(car e
)))
3752 (setq e
(cons (delq a
(car e
)) (cdr e
)))
3753 (or (assoc (car e
) r
) (push e r
)))
3754 (t (or (assoc (car e
) r
) (push e r
)))))
3757 (put 'org-compatible-face
'lisp-indent-function
1)
3760 '((((background light
)) (:foreground
"white"))
3761 (((background dark
)) (:foreground
"black")))
3762 "Face used to hide leading stars in headlines.
3763 The forground color of this face should be equal to the background
3764 color of the frame."
3767 (defface org-level-1
;; font-lock-function-name-face
3768 (org-compatible-face 'outline-1
3769 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Blue1"))
3770 (((class color
) (min-colors 88) (background dark
)) (:foreground
"LightSkyBlue"))
3771 (((class color
) (min-colors 16) (background light
)) (:foreground
"Blue"))
3772 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSkyBlue"))
3773 (((class color
) (min-colors 8)) (:foreground
"blue" :bold t
))
3775 "Face used for level 1 headlines."
3778 (defface org-level-2
;; font-lock-variable-name-face
3779 (org-compatible-face 'outline-2
3780 '((((class color
) (min-colors 16) (background light
)) (:foreground
"DarkGoldenrod"))
3781 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightGoldenrod"))
3782 (((class color
) (min-colors 8) (background light
)) (:foreground
"yellow"))
3783 (((class color
) (min-colors 8) (background dark
)) (:foreground
"yellow" :bold t
))
3785 "Face used for level 2 headlines."
3788 (defface org-level-3
;; font-lock-keyword-face
3789 (org-compatible-face 'outline-3
3790 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Purple"))
3791 (((class color
) (min-colors 88) (background dark
)) (:foreground
"Cyan1"))
3792 (((class color
) (min-colors 16) (background light
)) (:foreground
"Purple"))
3793 (((class color
) (min-colors 16) (background dark
)) (:foreground
"Cyan"))
3794 (((class color
) (min-colors 8) (background light
)) (:foreground
"purple" :bold t
))
3795 (((class color
) (min-colors 8) (background dark
)) (:foreground
"cyan" :bold t
))
3797 "Face used for level 3 headlines."
3800 (defface org-level-4
;; font-lock-comment-face
3801 (org-compatible-face 'outline-4
3802 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Firebrick"))
3803 (((class color
) (min-colors 88) (background dark
)) (:foreground
"chocolate1"))
3804 (((class color
) (min-colors 16) (background light
)) (:foreground
"red"))
3805 (((class color
) (min-colors 16) (background dark
)) (:foreground
"red1"))
3806 (((class color
) (min-colors 8) (background light
)) (:foreground
"red" :bold t
))
3807 (((class color
) (min-colors 8) (background dark
)) (:foreground
"red" :bold t
))
3809 "Face used for level 4 headlines."
3812 (defface org-level-5
;; font-lock-type-face
3813 (org-compatible-face 'outline-5
3814 '((((class color
) (min-colors 16) (background light
)) (:foreground
"ForestGreen"))
3815 (((class color
) (min-colors 16) (background dark
)) (:foreground
"PaleGreen"))
3816 (((class color
) (min-colors 8)) (:foreground
"green"))))
3817 "Face used for level 5 headlines."
3820 (defface org-level-6
;; font-lock-constant-face
3821 (org-compatible-face 'outline-6
3822 '((((class color
) (min-colors 16) (background light
)) (:foreground
"CadetBlue"))
3823 (((class color
) (min-colors 16) (background dark
)) (:foreground
"Aquamarine"))
3824 (((class color
) (min-colors 8)) (:foreground
"magenta"))))
3825 "Face used for level 6 headlines."
3828 (defface org-level-7
;; font-lock-builtin-face
3829 (org-compatible-face 'outline-7
3830 '((((class color
) (min-colors 16) (background light
)) (:foreground
"Orchid"))
3831 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSteelBlue"))
3832 (((class color
) (min-colors 8)) (:foreground
"blue"))))
3833 "Face used for level 7 headlines."
3836 (defface org-level-8
;; font-lock-string-face
3837 (org-compatible-face 'outline-8
3838 '((((class color
) (min-colors 16) (background light
)) (:foreground
"RosyBrown"))
3839 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSalmon"))
3840 (((class color
) (min-colors 8)) (:foreground
"green"))))
3841 "Face used for level 8 headlines."
3844 (defface org-special-keyword
;; font-lock-string-face
3845 (org-compatible-face nil
3846 '((((class color
) (min-colors 16) (background light
)) (:foreground
"RosyBrown"))
3847 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSalmon"))
3849 "Face used for special keywords."
3852 (defface org-drawer
;; font-lock-function-name-face
3853 (org-compatible-face nil
3854 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Blue1"))
3855 (((class color
) (min-colors 88) (background dark
)) (:foreground
"LightSkyBlue"))
3856 (((class color
) (min-colors 16) (background light
)) (:foreground
"Blue"))
3857 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSkyBlue"))
3858 (((class color
) (min-colors 8)) (:foreground
"blue" :bold t
))
3860 "Face used for drawers."
3863 (defface org-property-value nil
3864 "Face used for the value of a property."
3868 (org-compatible-face nil
3869 '((((class color
) (min-colors 16) (background light
))
3870 (:background
"grey90"))
3871 (((class color
) (min-colors 16) (background dark
))
3872 (:background
"grey30"))
3873 (((class color
) (min-colors 8))
3874 (:background
"cyan" :foreground
"black"))
3875 (t (:inverse-video t
))))
3876 "Face for column display of entry properties."
3879 (when (fboundp 'set-face-attribute
)
3880 ;; Make sure that a fixed-width face is used when we have a column table.
3881 (set-face-attribute 'org-column nil
3882 :height
(face-attribute 'default
:height
)
3883 :family
(face-attribute 'default
:family
)))
3885 (defface org-warning
3886 (org-compatible-face 'font-lock-warning-face
3887 '((((class color
) (min-colors 16) (background light
)) (:foreground
"Red1" :bold t
))
3888 (((class color
) (min-colors 16) (background dark
)) (:foreground
"Pink" :bold t
))
3889 (((class color
) (min-colors 8) (background light
)) (:foreground
"red" :bold t
))
3890 (((class color
) (min-colors 8) (background dark
)) (:foreground
"red" :bold t
))
3892 "Face for deadlines and TODO keywords."
3895 (defface org-archived
; similar to shadow
3896 (org-compatible-face 'shadow
3897 '((((class color grayscale
) (min-colors 88) (background light
))
3898 (:foreground
"grey50"))
3899 (((class color grayscale
) (min-colors 88) (background dark
))
3900 (:foreground
"grey70"))
3901 (((class color
) (min-colors 8) (background light
))
3902 (:foreground
"green"))
3903 (((class color
) (min-colors 8) (background dark
))
3904 (:foreground
"yellow"))))
3905 "Face for headline with the ARCHIVE tag."
3909 '((((class color
) (background light
)) (:foreground
"Purple" :underline t
))
3910 (((class color
) (background dark
)) (:foreground
"Cyan" :underline t
))
3915 (defface org-ellipsis
3916 '((((class color
) (background light
)) (:foreground
"DarkGoldenrod" :underline t
))
3917 (((class color
) (background dark
)) (:foreground
"LightGoldenrod" :underline t
))
3918 (t (:strike-through t
)))
3919 "Face for the ellipsis in folded text."
3923 '((((class color
) (background light
)) (:underline t
))
3924 (((class color
) (background dark
)) (:underline t
))
3930 '((((class color
) (background light
)) (:foreground
"Purple" :underline t
))
3931 (((class color
) (background dark
)) (:foreground
"Cyan" :underline t
))
3936 (defface org-sexp-date
3937 '((((class color
) (background light
)) (:foreground
"Purple"))
3938 (((class color
) (background dark
)) (:foreground
"Cyan"))
3948 (defface org-todo
; font-lock-warning-face
3949 (org-compatible-face nil
3950 '((((class color
) (min-colors 16) (background light
)) (:foreground
"Red1" :bold t
))
3951 (((class color
) (min-colors 16) (background dark
)) (:foreground
"Pink" :bold t
))
3952 (((class color
) (min-colors 8) (background light
)) (:foreground
"red" :bold t
))
3953 (((class color
) (min-colors 8) (background dark
)) (:foreground
"red" :bold t
))
3954 (t (:inverse-video t
:bold t
))))
3955 "Face for TODO keywords."
3958 (defface org-done
;; font-lock-type-face
3959 (org-compatible-face nil
3960 '((((class color
) (min-colors 16) (background light
)) (:foreground
"ForestGreen" :bold t
))
3961 (((class color
) (min-colors 16) (background dark
)) (:foreground
"PaleGreen" :bold t
))
3962 (((class color
) (min-colors 8)) (:foreground
"green"))
3964 "Face used for todo keywords that indicate DONE items."
3967 (defface org-headline-done
;; font-lock-string-face
3968 (org-compatible-face nil
3969 '((((class color
) (min-colors 16) (background light
)) (:foreground
"RosyBrown"))
3970 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSalmon"))
3971 (((class color
) (min-colors 8) (background light
)) (:bold nil
))))
3972 "Face used to indicate that a headline is DONE.
3973 This face is only used if `org-fontify-done-headline' is set. If applies
3974 to the part of the headline after the DONE keyword."
3977 (defcustom org-todo-keyword-faces nil
3978 "Faces for specific TODO keywords.
3979 This is a list of cons cells, with TODO keywords in the car
3980 and faces in the cdr. The face can be a symbol, or a property
3981 list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
3986 (string :tag
"keyword")
3987 (sexp :tag
"face"))))
3989 (defface org-table
;; font-lock-function-name-face
3990 (org-compatible-face nil
3991 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Blue1"))
3992 (((class color
) (min-colors 88) (background dark
)) (:foreground
"LightSkyBlue"))
3993 (((class color
) (min-colors 16) (background light
)) (:foreground
"Blue"))
3994 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSkyBlue"))
3995 (((class color
) (min-colors 8) (background light
)) (:foreground
"blue"))
3996 (((class color
) (min-colors 8) (background dark
)))))
3997 "Face used for tables."
4000 (defface org-formula
4001 (org-compatible-face nil
4002 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Firebrick"))
4003 (((class color
) (min-colors 88) (background dark
)) (:foreground
"chocolate1"))
4004 (((class color
) (min-colors 8) (background light
)) (:foreground
"red"))
4005 (((class color
) (min-colors 8) (background dark
)) (:foreground
"red"))
4006 (t (:bold t
:italic t
))))
4007 "Face for formulas."
4011 (org-compatible-face nil
4012 '((((class color grayscale
) (min-colors 88) (background light
))
4013 (:foreground
"grey50"))
4014 (((class color grayscale
) (min-colors 88) (background dark
))
4015 (:foreground
"grey70"))
4016 (((class color
) (min-colors 8) (background light
))
4017 (:foreground
"green"))
4018 (((class color
) (min-colors 8) (background dark
))
4019 (:foreground
"yellow"))))
4020 "Face for fixed-with text like code snippets."
4024 (defface org-verbatim
4025 (org-compatible-face nil
4026 '((((class color grayscale
) (min-colors 88) (background light
))
4027 (:foreground
"grey50" :underline t
))
4028 (((class color grayscale
) (min-colors 88) (background dark
))
4029 (:foreground
"grey70" :underline t
))
4030 (((class color
) (min-colors 8) (background light
))
4031 (:foreground
"green" :underline t
))
4032 (((class color
) (min-colors 8) (background dark
))
4033 (:foreground
"yellow" :underline t
))))
4034 "Face for fixed-with text like code snippets."
4038 (defface org-agenda-structure
;; font-lock-function-name-face
4039 (org-compatible-face nil
4040 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Blue1"))
4041 (((class color
) (min-colors 88) (background dark
)) (:foreground
"LightSkyBlue"))
4042 (((class color
) (min-colors 16) (background light
)) (:foreground
"Blue"))
4043 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSkyBlue"))
4044 (((class color
) (min-colors 8)) (:foreground
"blue" :bold t
))
4046 "Face used in agenda for captions and dates."
4049 (defface org-scheduled-today
4050 (org-compatible-face nil
4051 '((((class color
) (min-colors 88) (background light
)) (:foreground
"DarkGreen"))
4052 (((class color
) (min-colors 88) (background dark
)) (:foreground
"PaleGreen"))
4053 (((class color
) (min-colors 8)) (:foreground
"green"))
4054 (t (:bold t
:italic t
))))
4055 "Face for items scheduled for a certain day."
4058 (defface org-scheduled-previously
4059 (org-compatible-face nil
4060 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Firebrick"))
4061 (((class color
) (min-colors 88) (background dark
)) (:foreground
"chocolate1"))
4062 (((class color
) (min-colors 8) (background light
)) (:foreground
"red"))
4063 (((class color
) (min-colors 8) (background dark
)) (:foreground
"red" :bold t
))
4065 "Face for items scheduled previously, and not yet done."
4068 (defface org-upcoming-deadline
4069 (org-compatible-face nil
4070 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Firebrick"))
4071 (((class color
) (min-colors 88) (background dark
)) (:foreground
"chocolate1"))
4072 (((class color
) (min-colors 8) (background light
)) (:foreground
"red"))
4073 (((class color
) (min-colors 8) (background dark
)) (:foreground
"red" :bold t
))
4075 "Face for items scheduled previously, and not yet done."
4078 (defcustom org-agenda-deadline-faces
4079 '((1.0 . org-warning
)
4080 (0.5 . org-upcoming-deadline
)
4082 "Faces for showing deadlines in the agenda.
4083 This is a list of cons cells. The cdr of each cell is a face to be used,
4084 and it can also just be like '(:foreground \"yellow\").
4085 Each car is a fraction of the head-warning time that must have passed for
4086 this the face in the cdr to be used for display. The numbers must be
4087 given in descending order. The head-warning time is normally taken
4088 from `org-deadline-warning-days', but can also be specified in the deadline
4089 timestamp itself, like this:
4091 DEADLINE: <2007-08-13 Mon -8d>
4093 You may use d for days, w for weeks, m for months and y for years. Months
4094 and years will only be treated in an approximate fashion (30.4 days for a
4095 month and 365.24 days for a year)."
4097 :group
'org-agenda-daily
/weekly
4100 (number :tag
"Fraction of head-warning time passed")
4101 (sexp :tag
"Face"))))
4103 ;; FIXME: this is not a good face yet.
4104 (defface org-agenda-restriction-lock
4105 (org-compatible-face nil
4106 '((((class color
) (min-colors 88) (background light
)) (:background
"yellow1"))
4107 (((class color
) (min-colors 88) (background dark
)) (:background
"skyblue4"))
4108 (((class color
) (min-colors 16) (background light
)) (:background
"yellow1"))
4109 (((class color
) (min-colors 16) (background dark
)) (:background
"skyblue4"))
4110 (((class color
) (min-colors 8)) (:background
"cyan" :foreground
"black"))
4111 (t (:inverse-video t
))))
4112 "Face for showing the agenda restriction lock."
4115 (defface org-time-grid
;; font-lock-variable-name-face
4116 (org-compatible-face nil
4117 '((((class color
) (min-colors 16) (background light
)) (:foreground
"DarkGoldenrod"))
4118 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightGoldenrod"))
4119 (((class color
) (min-colors 8)) (:foreground
"yellow" :weight light
))))
4120 "Face used for time grids."
4123 (defconst org-level-faces
4124 '(org-level-1 org-level-2 org-level-3 org-level-4
4125 org-level-5 org-level-6 org-level-7 org-level-8
4128 (defcustom org-n-level-faces
(length org-level-faces
)
4129 "The number different faces to be used for headlines.
4130 Org-mode defines 8 different headline faces, so this can be at most 8.
4131 If it is less than 8, the level-1 face gets re-used for level N+1 etc."
4135 ;;; Functions and variables from ther packages
4136 ;; Declared here to avoid compiler warnings
4139 (unless (fboundp 'declare-function
)
4140 (defmacro declare-function
(fn file
&optional arglist fileonly
))))
4143 (defvar outline-mode-menu-heading
)
4144 (defvar outline-mode-menu-show
)
4145 (defvar outline-mode-menu-hide
)
4146 (defvar zmacs-regions
) ; XEmacs regions
4149 (defvar mark-active
)
4152 ;; FIXME: get the argument lists for the UNKNOWN stuff
4153 (declare-function add-to-diary-list
"diary-lib"
4154 (date string specifier
&optional marker globcolor literal
))
4155 (declare-function table--at-cell-p
"table" (position &optional object at-column
))
4156 (declare-function Info-find-node
"info" (filename nodename
&optional no-going-back
))
4157 (declare-function Info-goto-node
"info" (nodename &optional fork
))
4158 (declare-function bbdb
"ext:bbdb-com" (string elidep
))
4159 (declare-function bbdb-company
"ext:bbdb-com" (string elidep
))
4160 (declare-function bbdb-current-record
"ext:bbdb-com" (&optional planning-on-modifying
))
4161 (declare-function bbdb-name
"ext:bbdb-com" (string elidep
))
4162 (declare-function bbdb-record-getprop
"ext:bbdb" (record property
))
4163 (declare-function bbdb-record-name
"ext:bbdb" (record))
4164 (declare-function bibtex-beginning-of-entry
"bibtex" ())
4165 (declare-function bibtex-generate-autokey
"bibtex" ())
4166 (declare-function bibtex-parse-entry
"bibtex" (&optional content
))
4167 (declare-function bibtex-url
"bibtex" (&optional pos no-browse
))
4168 (defvar calc-embedded-close-formula
)
4169 (defvar calc-embedded-open-formula
)
4170 (declare-function calendar-astro-date-string
"cal-julian" (&optional date
))
4171 (declare-function calendar-bahai-date-string
"cal-bahai" (&optional date
))
4172 (declare-function calendar-check-holidays
"holidays" (date))
4173 (declare-function calendar-chinese-date-string
"cal-china" (&optional date
))
4174 (declare-function calendar-coptic-date-string
"cal-coptic" (&optional date
))
4175 (declare-function calendar-ethiopic-date-string
"cal-coptic" (&optional date
))
4176 (declare-function calendar-forward-day
"cal-move" (arg))
4177 (declare-function calendar-french-date-string
"cal-french" (&optional date
))
4178 (declare-function calendar-goto-date
"cal-move" (date))
4179 (declare-function calendar-goto-today
"cal-move" ())
4180 (declare-function calendar-hebrew-date-string
"cal-hebrew" (&optional date
))
4181 (declare-function calendar-islamic-date-string
"cal-islam" (&optional date
))
4182 (declare-function calendar-iso-date-string
"cal-iso" (&optional date
))
4183 (declare-function calendar-julian-date-string
"cal-julian" (&optional date
))
4184 (declare-function calendar-mayan-date-string
"cal-mayan" (&optional date
))
4185 (declare-function calendar-persian-date-string
"cal-persia" (&optional date
))
4186 (defvar calendar-mode-map
)
4187 (defvar original-date
) ; dynamically scoped in calendar.el does scope this
4188 (declare-function cdlatex-tab
"ext:cdlatex" ())
4189 (declare-function dired-get-filename
"dired" (&optional localp no-error-if-not-filep
))
4190 (declare-function elmo-folder-exists-p
"ext:elmo" (folder) t
)
4191 (declare-function elmo-message-entity-field
"ext:elmo-msgdb" (entity field
&optional type
))
4192 (declare-function elmo-message-field
"ext:elmo" (folder number field
&optional type
) t
)
4193 (declare-function elmo-msgdb-overview-get-entity
"ext:elmo" (&rest unknown
) t
)
4194 (defvar font-lock-unfontify-region-function
)
4195 (declare-function gnus-article-show-summary
"gnus-art" ())
4196 (declare-function gnus-summary-last-subject
"gnus-sum" ())
4197 (defvar gnus-other-frame-object
)
4198 (defvar gnus-group-name
)
4199 (defvar gnus-article-current
)
4200 (defvar Info-current-file
)
4201 (defvar Info-current-node
)
4202 (declare-function mh-display-msg
"mh-show" (msg-num folder-name
))
4203 (declare-function mh-find-path
"mh-utils" ())
4204 (declare-function mh-get-header-field
"mh-utils" (field))
4205 (declare-function mh-get-msg-num
"mh-utils" (error-if-no-message))
4206 (declare-function mh-header-display
"mh-show" ())
4207 (declare-function mh-index-previous-folder
"mh-search" ())
4208 (declare-function mh-normalize-folder-name
"mh-utils" (folder &optional empty-string-okay dont-remove-trailing-slash return-nil-if-folder-empty
))
4209 (declare-function mh-search
"mh-search" (folder search-regexp
&optional redo-search-flag window-config
))
4210 (declare-function mh-search-choose
"mh-search" (&optional searcher
))
4211 (declare-function mh-show
"mh-show" (&optional message redisplay-flag
))
4212 (declare-function mh-show-buffer-message-number
"mh-comp" (&optional buffer
))
4213 (declare-function mh-show-header-display
"mh-show" t t
)
4214 (declare-function mh-show-msg
"mh-show" (msg))
4215 (declare-function mh-show-show
"mh-show" t t
)
4216 (declare-function mh-visit-folder
"mh-folder" (folder &optional range index-data
))
4218 (defvar mh-current-folder
)
4219 (defvar mh-show-folder-buffer
)
4220 (defvar mh-index-folder
)
4221 (defvar mh-searcher
)
4222 (declare-function org-export-latex-cleaned-string
"org-export-latex" ())
4223 (declare-function parse-time-string
"parse-time" (string))
4224 (declare-function remember
"remember" (&optional initial
))
4225 (declare-function remember-buffer-desc
"remember" ())
4226 (declare-function remember-finalize
"remember" ())
4227 (defvar remember-save-after-remembering
)
4228 (defvar remember-data-file
)
4229 (defvar remember-register
)
4230 (defvar remember-buffer
)
4231 (defvar remember-handler-functions
)
4232 (defvar remember-annotation-functions
)
4233 (declare-function rmail-narrow-to-non-pruned-header
"rmail" ())
4234 (declare-function rmail-show-message
"rmail" (&optional n no-summary
))
4235 (declare-function rmail-what-message
"rmail" ())
4236 (defvar texmathp-why
)
4237 (declare-function vm-beginning-of-message
"ext:vm-page" ())
4238 (declare-function vm-follow-summary-cursor
"ext:vm-motion" ())
4239 (declare-function vm-get-header-contents
"ext:vm-summary" (message header-name-regexp
&optional clump-sep
))
4240 (declare-function vm-isearch-narrow
"ext:vm-search" ())
4241 (declare-function vm-isearch-update
"ext:vm-search" ())
4242 (declare-function vm-select-folder-buffer
"ext:vm-macro" ())
4243 (declare-function vm-su-message-id
"ext:vm-summary" (m))
4244 (declare-function vm-su-subject
"ext:vm-summary" (m))
4245 (declare-function vm-summarize
"ext:vm-summary" (&optional display raise
))
4246 (defvar vm-message-pointer
)
4247 (defvar vm-folder-directory
)
4248 (defvar w3m-current-url
)
4249 (defvar w3m-current-title
)
4250 ;; backward compatibility to old version of wl
4251 (declare-function wl-summary-buffer-msgdb
"ext:wl-folder" (&rest unknown
) t
)
4252 (declare-function wl-folder-get-elmo-folder
"ext:wl-folder" (entity &optional no-cache
))
4253 (declare-function wl-summary-goto-folder-subr
"ext:wl-summary" (&optional name scan-type other-window sticky interactive scoring force-exit
))
4254 (declare-function wl-summary-jump-to-msg-by-message-id
"ext:wl-summary" (&optional id
))
4255 (declare-function wl-summary-line-from
"ext:wl-summary" ())
4256 (declare-function wl-summary-line-subject
"ext:wl-summary" ())
4257 (declare-function wl-summary-message-number
"ext:wl-summary" ())
4258 (declare-function wl-summary-redisplay
"ext:wl-summary" (&optional arg
))
4259 (defvar wl-summary-buffer-elmo-folder
)
4260 (defvar wl-summary-buffer-folder-name
)
4261 (declare-function speedbar-line-directory
"speedbar" (&optional depth
))
4263 (defvar org-latex-regexps
)
4264 (defvar constants-unit-system
)
4266 ;;; Variables for pre-computed regular expressions, all buffer local
4268 (defvar org-drawer-regexp nil
4269 "Matches first line of a hidden block.")
4270 (make-variable-buffer-local 'org-drawer-regexp
)
4271 (defvar org-todo-regexp nil
4272 "Matches any of the TODO state keywords.")
4273 (make-variable-buffer-local 'org-todo-regexp
)
4274 (defvar org-not-done-regexp nil
4275 "Matches any of the TODO state keywords except the last one.")
4276 (make-variable-buffer-local 'org-not-done-regexp
)
4277 (defvar org-todo-line-regexp nil
4278 "Matches a headline and puts TODO state into group 2 if present.")
4279 (make-variable-buffer-local 'org-todo-line-regexp
)
4280 (defvar org-complex-heading-regexp nil
4281 "Matches a headline and puts everything into groups:
4283 group 2: The todo keyword, maybe
4284 group 3: Priority cookie
4285 group 4: True headline
4287 (make-variable-buffer-local 'org-complex-heading-regexp
)
4288 (defvar org-todo-line-tags-regexp nil
4289 "Matches a headline and puts TODO state into group 2 if present.
4290 Also put tags into group 4 if tags are present.")
4291 (make-variable-buffer-local 'org-todo-line-tags-regexp
)
4292 (defvar org-nl-done-regexp nil
4293 "Matches newline followed by a headline with the DONE keyword.")
4294 (make-variable-buffer-local 'org-nl-done-regexp
)
4295 (defvar org-looking-at-done-regexp nil
4296 "Matches the DONE keyword a point.")
4297 (make-variable-buffer-local 'org-looking-at-done-regexp
)
4298 (defvar org-ds-keyword-length
12
4299 "Maximum length of the Deadline and SCHEDULED keywords.")
4300 (make-variable-buffer-local 'org-ds-keyword-length
)
4301 (defvar org-deadline-regexp nil
4302 "Matches the DEADLINE keyword.")
4303 (make-variable-buffer-local 'org-deadline-regexp
)
4304 (defvar org-deadline-time-regexp nil
4305 "Matches the DEADLINE keyword together with a time stamp.")
4306 (make-variable-buffer-local 'org-deadline-time-regexp
)
4307 (defvar org-deadline-line-regexp nil
4308 "Matches the DEADLINE keyword and the rest of the line.")
4309 (make-variable-buffer-local 'org-deadline-line-regexp
)
4310 (defvar org-scheduled-regexp nil
4311 "Matches the SCHEDULED keyword.")
4312 (make-variable-buffer-local 'org-scheduled-regexp
)
4313 (defvar org-scheduled-time-regexp nil
4314 "Matches the SCHEDULED keyword together with a time stamp.")
4315 (make-variable-buffer-local 'org-scheduled-time-regexp
)
4316 (defvar org-closed-time-regexp nil
4317 "Matches the CLOSED keyword together with a time stamp.")
4318 (make-variable-buffer-local 'org-closed-time-regexp
)
4320 (defvar org-keyword-time-regexp nil
4321 "Matches any of the 4 keywords, together with the time stamp.")
4322 (make-variable-buffer-local 'org-keyword-time-regexp
)
4323 (defvar org-keyword-time-not-clock-regexp nil
4324 "Matches any of the 3 keywords, together with the time stamp.")
4325 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp
)
4326 (defvar org-maybe-keyword-time-regexp nil
4327 "Matches a timestamp, possibly preceeded by a keyword.")
4328 (make-variable-buffer-local 'org-maybe-keyword-time-regexp
)
4329 (defvar org-planning-or-clock-line-re nil
4330 "Matches a line with planning or clock info.")
4331 (make-variable-buffer-local 'org-planning-or-clock-line-re
)
4333 (defconst org-rm-props
'(invisible t face t keymap t intangible t mouse-face t
4334 rear-nonsticky t mouse-map t fontified t
)
4335 "Properties to remove when a string without properties is wanted.")
4337 (defsubst org-match-string-no-properties
(num &optional string
)
4338 (if (featurep 'xemacs
)
4339 (let ((s (match-string num string
)))
4340 (remove-text-properties 0 (length s
) org-rm-props s
)
4342 (match-string-no-properties num string
)))
4344 (defsubst org-no-properties
(s)
4345 (if (fboundp 'set-text-properties
)
4346 (set-text-properties 0 (length s
) nil s
)
4347 (remove-text-properties 0 (length s
) org-rm-props s
))
4350 (defsubst org-get-alist-option
(option key
)
4351 (cond ((eq key t
) t
)
4353 ((assoc key option
) (cdr (assoc key option
)))
4354 (t (cdr (assq 'default option
)))))
4356 (defsubst org-inhibit-invisibility
()
4357 "Modified `buffer-invisibility-spec' for Emacs 21.
4358 Some ops with invisible text do not work correctly on Emacs 21. For these
4359 we turn off invisibility temporarily. Use this in a `let' form."
4360 (if (< emacs-major-version
22) nil buffer-invisibility-spec
))
4362 (defsubst org-set-local
(var value
)
4363 "Make VAR local in current buffer and set it to VALUE."
4364 (set (make-variable-buffer-local var
) value
))
4366 (defsubst org-mode-p
()
4367 "Check if the current buffer is in Org-mode."
4368 (eq major-mode
'org-mode
))
4370 (defsubst org-last
(list)
4371 "Return the last element of LIST."
4374 (defun org-let (list &rest body
)
4375 (eval (cons 'let
(cons list body
))))
4376 (put 'org-let
'lisp-indent-function
1)
4378 (defun org-let2 (list1 list2
&rest body
)
4379 (eval (cons 'let
(cons list1
(list (cons 'let
(cons list2 body
)))))))
4380 (put 'org-let2
'lisp-indent-function
2)
4381 (defconst org-startup-options
4382 '(("fold" org-startup-folded t
)
4383 ("overview" org-startup-folded t
)
4384 ("nofold" org-startup-folded nil
)
4385 ("showall" org-startup-folded nil
)
4386 ("content" org-startup-folded content
)
4387 ("hidestars" org-hide-leading-stars t
)
4388 ("showstars" org-hide-leading-stars nil
)
4389 ("odd" org-odd-levels-only t
)
4390 ("oddeven" org-odd-levels-only nil
)
4391 ("align" org-startup-align-all-tables t
)
4392 ("noalign" org-startup-align-all-tables nil
)
4393 ("customtime" org-display-custom-times t
)
4394 ("logging" org-log-done t
)
4395 ("logdone" org-log-done t
)
4396 ("nologging" org-log-done nil
)
4397 ("lognotedone" org-log-done done push
)
4398 ("lognotestate" org-log-done state push
)
4399 ("lognoteclock-out" org-log-done clock-out push
)
4400 ("logrepeat" org-log-repeat t
)
4401 ("nologrepeat" org-log-repeat nil
)
4402 ("constcgs" constants-unit-system cgs
)
4403 ("constSI" constants-unit-system SI
))
4404 "Variable associated with STARTUP options for org-mode.
4405 Each element is a list of three items: The startup options as written
4406 in the #+STARTUP line, the corresponding variable, and the value to
4407 set this variable to if the option is found. An optional forth element PUSH
4408 means to push this value onto the list in the variable.")
4410 (defun org-set-regexps-and-options ()
4411 "Precompute regular expressions for current buffer."
4413 (org-set-local 'org-todo-kwd-alist nil
)
4414 (org-set-local 'org-todo-key-alist nil
)
4415 (org-set-local 'org-todo-key-trigger nil
)
4416 (org-set-local 'org-todo-keywords-1 nil
)
4417 (org-set-local 'org-done-keywords nil
)
4418 (org-set-local 'org-todo-heads nil
)
4419 (org-set-local 'org-todo-sets nil
)
4420 (org-set-local 'org-todo-log-states nil
)
4421 (let ((re (org-make-options-regexp
4422 '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
4423 "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
4424 "CONSTANTS" "PROPERTY" "DRAWERS")))
4426 kwds kws0 kwsa key value cat arch tags const links hw dws
4427 tail sep kws1 prio props drawers
4432 (goto-char (point-min))
4433 (while (re-search-forward re nil t
)
4434 (setq key
(match-string 1) value
(org-match-string-no-properties 2))
4436 ((equal key
"CATEGORY")
4437 (if (string-match "[ \t]+$" value
)
4438 (setq value
(replace-match "" t t value
)))
4440 ((member key
'("SEQ_TODO" "TODO"))
4441 (push (cons 'sequence
(org-split-string value splitre
)) kwds
))
4442 ((equal key
"TYP_TODO")
4443 (push (cons 'type
(org-split-string value splitre
)) kwds
))
4445 (setq tags
(append tags
(org-split-string value splitre
))))
4446 ((equal key
"COLUMNS")
4447 (org-set-local 'org-columns-default-format value
))
4449 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value
)
4450 (push (cons (match-string 1 value
)
4451 (org-trim (match-string 2 value
)))
4453 ((equal key
"PRIORITIES")
4454 (setq prio
(org-split-string value
" +")))
4455 ((equal key
"PROPERTY")
4456 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
4457 (push (cons (match-string 1 value
) (match-string 2 value
))
4459 ((equal key
"DRAWERS")
4460 (setq drawers
(org-split-string value splitre
)))
4461 ((equal key
"CONSTANTS")
4462 (setq const
(append const
(org-split-string value splitre
))))
4463 ((equal key
"STARTUP")
4464 (let ((opts (org-split-string value splitre
))
4466 (while (setq l
(pop opts
))
4467 (when (setq l
(assoc l org-startup-options
))
4468 (setq var
(nth 1 l
) val
(nth 2 l
))
4470 (set (make-local-variable var
) val
)
4471 (if (not (listp (symbol-value var
)))
4472 (set (make-local-variable var
) nil
))
4473 (set (make-local-variable var
) (symbol-value var
))
4474 (add-to-list var val
))))))
4475 ((equal key
"ARCHIVE")
4476 (string-match " *$" value
)
4477 (setq arch
(replace-match "" t t value
))
4478 (remove-text-properties 0 (length arch
)
4479 '(face t fontified t
) arch
)))
4482 (org-set-local 'org-category
(intern cat
))
4483 (push (cons "CATEGORY" cat
) props
))
4485 (if (< (length prio
) 3) (setq prio
'("A" "C" "B")))
4486 (setq prio
(mapcar 'string-to-char prio
))
4487 (org-set-local 'org-highest-priority
(nth 0 prio
))
4488 (org-set-local 'org-lowest-priority
(nth 1 prio
))
4489 (org-set-local 'org-default-priority
(nth 2 prio
)))
4490 (and props
(org-set-local 'org-local-properties
(nreverse props
)))
4491 (and drawers
(org-set-local 'org-drawers drawers
))
4492 (and arch
(org-set-local 'org-archive-location arch
))
4493 (and links
(setq org-link-abbrev-alist-local
(nreverse links
)))
4494 ;; Process the TODO keywords
4496 ;; Use the global values as if they had been given locally.
4497 (setq kwds
(default-value 'org-todo-keywords
))
4498 (if (stringp (car kwds
))
4499 (setq kwds
(list (cons org-todo-interpretation
4500 (default-value 'org-todo-keywords
)))))
4501 (setq kwds
(reverse kwds
)))
4502 (setq kwds
(nreverse kwds
))
4504 (while (setq kws
(pop kwds
))
4505 (setq inter
(pop kws
) sep
(member "|" kws
)
4506 kws0
(delete "|" (copy-sequence kws
))
4510 (if (string-match "^\\(.*?\\)\\(?:(\\(..?\\))\\)?$" x
)
4512 (setq kw
(match-string 1 x
)
4513 ex
(and (match-end 2) (match-string 2 x
))
4514 log
(and ex
(string-match "@" ex
))
4515 key
(and ex
(substring ex
0 1)))
4516 (if (equal key
"@") (setq key nil
))
4517 (push (cons kw
(and key
(string-to-char key
))) kwsa
)
4518 (and log
(push kw org-todo-log-states
))
4520 (error "Invalid TODO keyword %s" x
)))
4522 kwsa
(if kwsa
(append '((:startgroup
))
4526 dws
(if sep
(org-remove-keyword-keys (cdr sep
)) (last kws1
))
4527 tail
(list inter hw
(car dws
) (org-last dws
)))
4528 (add-to-list 'org-todo-heads hw
'append
)
4529 (push kws1 org-todo-sets
)
4530 (setq org-done-keywords
(append org-done-keywords dws nil
))
4531 (setq org-todo-key-alist
(append org-todo-key-alist kwsa
))
4532 (mapc (lambda (x) (push (cons x tail
) org-todo-kwd-alist
)) kws1
)
4533 (setq org-todo-keywords-1
(append org-todo-keywords-1 kws1 nil
)))
4534 (setq org-todo-sets
(nreverse org-todo-sets
)
4535 org-todo-kwd-alist
(nreverse org-todo-kwd-alist
)
4536 org-todo-key-trigger
(delq nil
(mapcar 'cdr org-todo-key-alist
))
4537 org-todo-key-alist
(org-assign-fast-keys org-todo-key-alist
)))
4538 ;; Process the constants
4541 (while (setq e
(pop const
))
4542 (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e
)
4543 (push (cons (match-string 1 e
) (match-string 2 e
)) cst
)))
4544 (setq org-table-formula-constants-local cst
)))
4546 ;; Process the tags.
4549 (while (setq e
(pop tags
))
4551 ((equal e
"{") (push '(:startgroup
) tgs
))
4552 ((equal e
"}") (push '(:endgroup
) tgs
))
4553 ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e
)
4554 (push (cons (match-string 1 e
)
4555 (string-to-char (match-string 2 e
)))
4557 (t (push (list e
) tgs
))))
4558 (org-set-local 'org-tag-alist nil
)
4559 (while (setq e
(pop tgs
))
4560 (or (and (stringp (car e
))
4561 (assoc (car e
) org-tag-alist
))
4562 (push e org-tag-alist
))))))
4564 ;; Compute the regular expressions and other local variables
4565 (if (not org-done-keywords
)
4566 (setq org-done-keywords
(list (org-last org-todo-keywords-1
))))
4567 (setq org-ds-keyword-length
(+ 2 (max (length org-deadline-string
)
4568 (length org-scheduled-string
)))
4570 (concat "^[ \t]*:\\("
4571 (mapconcat 'regexp-quote org-drawers
"\\|")
4573 org-not-done-keywords
4574 (org-delete-all org-done-keywords
(copy-sequence org-todo-keywords-1
))
4576 (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
4580 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
4582 org-todo-line-regexp
4583 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
4584 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
4585 "\\)\\>\\)?[ \t]*\\(.*\\)")
4586 org-complex-heading-regexp
4587 (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
4588 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
4589 "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
4590 "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
4592 (concat "\n\\*+[ \t]+"
4593 "\\(?:" (mapconcat 'regexp-quote org-done-keywords
"\\|")
4595 org-todo-line-tags-regexp
4596 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
4597 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
4599 "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
4600 org-looking-at-done-regexp
4602 (mapconcat 'regexp-quote org-done-keywords
"\\|") "\\)"
4604 org-deadline-regexp
(concat "\\<" org-deadline-string
)
4605 org-deadline-time-regexp
4606 (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>")
4607 org-deadline-line-regexp
4608 (concat "\\<\\(" org-deadline-string
"\\).*")
4609 org-scheduled-regexp
4610 (concat "\\<" org-scheduled-string
)
4611 org-scheduled-time-regexp
4612 (concat "\\<" org-scheduled-string
" *<\\([^>]+\\)>")
4613 org-closed-time-regexp
4614 (concat "\\<" org-closed-string
" *\\[\\([^]]+\\)\\]")
4615 org-keyword-time-regexp
4616 (concat "\\<\\(" org-scheduled-string
4617 "\\|" org-deadline-string
4618 "\\|" org-closed-string
4619 "\\|" org-clock-string
"\\)"
4620 " *[[<]\\([^]>]+\\)[]>]")
4621 org-keyword-time-not-clock-regexp
4622 (concat "\\<\\(" org-scheduled-string
4623 "\\|" org-deadline-string
4624 "\\|" org-closed-string
4626 " *[[<]\\([^]>]+\\)[]>]")
4627 org-maybe-keyword-time-regexp
4628 (concat "\\(\\<\\(" org-scheduled-string
4629 "\\|" org-deadline-string
4630 "\\|" org-closed-string
4631 "\\|" org-clock-string
"\\)\\)?"
4632 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
4633 org-planning-or-clock-line-re
4634 (concat "\\(?:^[ \t]*\\(" org-scheduled-string
4635 "\\|" org-deadline-string
4636 "\\|" org-closed-string
"\\|" org-clock-string
4639 (org-compute-latex-and-specials-regexp)
4640 (org-set-font-lock-defaults)))
4642 (defun org-remove-keyword-keys (list)
4644 (if (string-match "(..?)$" x
)
4645 (substring x
0 (match-beginning 0))
4649 ;; FIXME: this could be done much better, using second characters etc.
4650 (defun org-assign-fast-keys (alist)
4651 "Assign fast keys to a keyword-key alist.
4652 Respect keys that are already there."
4653 (let (new e k c c1 c2
(char ?a
))
4654 (while (setq e
(pop alist
))
4656 ((equal e
'(:startgroup
)) (push e new
))
4657 ((equal e
'(:endgroup
)) (push e new
))
4659 (setq k
(car e
) c2 nil
)
4662 ;; automatically assign a character.
4663 (setq c1
(string-to-char
4664 (downcase (substring
4665 k
(if (= (string-to-char k
) ?
@) 1 0)))))
4666 (if (or (rassoc c1 new
) (rassoc c1 alist
))
4667 (while (or (rassoc char new
) (rassoc char alist
))
4668 (setq char
(1+ char
)))
4670 (setq c
(or c2 char
)))
4671 (push (cons k c
) new
))))
4674 ;;; Some variables ujsed in various places
4676 (defvar org-window-configuration nil
4677 "Used in various places to store a window configuration.")
4678 (defvar org-finish-function nil
4679 "Function to be called when `C-c C-c' is used.
4680 This is for getting out of special buffers like remember.")
4683 ;; FIXME: Occasionally check by commenting these, to make sure
4684 ;; no other functions uses these, forgetting to let-bind them.
4689 (defvar description
)
4691 ;; Defined somewhere in this file, but used before definition.
4692 (defvar orgtbl-mode-menu
) ; defined when orgtbl mode get initialized
4693 (defvar org-agenda-buffer-name
)
4694 (defvar org-agenda-undo-list
)
4695 (defvar org-agenda-pending-undo-list
)
4696 (defvar org-agenda-overriding-header
)
4697 (defvar orgtbl-mode
)
4698 (defvar org-html-entities
)
4699 (defvar org-struct-menu
)
4700 (defvar org-org-menu
)
4701 (defvar org-tbl-menu
)
4702 (defvar org-agenda-keymap
)
4704 ;;;; Emacs/XEmacs compatibility
4706 ;; Overlay compatibility functions
4707 (defun org-make-overlay (beg end
&optional buffer
)
4708 (if (featurep 'xemacs
)
4709 (make-extent beg end buffer
)
4710 (make-overlay beg end buffer
)))
4711 (defun org-delete-overlay (ovl)
4712 (if (featurep 'xemacs
) (delete-extent ovl
) (delete-overlay ovl
)))
4713 (defun org-detach-overlay (ovl)
4714 (if (featurep 'xemacs
) (detach-extent ovl
) (delete-overlay ovl
)))
4715 (defun org-move-overlay (ovl beg end
&optional buffer
)
4716 (if (featurep 'xemacs
)
4717 (set-extent-endpoints ovl beg end
(or buffer
(current-buffer)))
4718 (move-overlay ovl beg end buffer
)))
4719 (defun org-overlay-put (ovl prop value
)
4720 (if (featurep 'xemacs
)
4721 (set-extent-property ovl prop value
)
4722 (overlay-put ovl prop value
)))
4723 (defun org-overlay-display (ovl text
&optional face evap
)
4724 "Make overlay OVL display TEXT with face FACE."
4725 (if (featurep 'xemacs
)
4726 (let ((gl (make-glyph text
)))
4727 (and face
(set-glyph-face gl face
))
4728 (set-extent-property ovl
'invisible t
)
4729 (set-extent-property ovl
'end-glyph gl
))
4730 (overlay-put ovl
'display text
)
4731 (if face
(overlay-put ovl
'face face
))
4732 (if evap
(overlay-put ovl
'evaporate t
))))
4733 (defun org-overlay-before-string (ovl text
&optional face evap
)
4734 "Make overlay OVL display TEXT with face FACE."
4735 (if (featurep 'xemacs
)
4736 (let ((gl (make-glyph text
)))
4737 (and face
(set-glyph-face gl face
))
4738 (set-extent-property ovl
'begin-glyph gl
))
4739 (if face
(org-add-props text nil
'face face
))
4740 (overlay-put ovl
'before-string text
)
4741 (if evap
(overlay-put ovl
'evaporate t
))))
4742 (defun org-overlay-get (ovl prop
)
4743 (if (featurep 'xemacs
)
4744 (extent-property ovl prop
)
4745 (overlay-get ovl prop
)))
4746 (defun org-overlays-at (pos)
4747 (if (featurep 'xemacs
) (extents-at pos
) (overlays-at pos
)))
4748 (defun org-overlays-in (&optional start end
)
4749 (if (featurep 'xemacs
)
4750 (extent-list nil start end
)
4751 (overlays-in start end
)))
4752 (defun org-overlay-start (o)
4753 (if (featurep 'xemacs
) (extent-start-position o
) (overlay-start o
)))
4754 (defun org-overlay-end (o)
4755 (if (featurep 'xemacs
) (extent-end-position o
) (overlay-end o
)))
4756 (defun org-find-overlays (prop &optional pos delete
)
4757 "Find all overlays specifying PROP at POS or point.
4758 If DELETE is non-nil, delete all those overlays."
4759 (let ((overlays (org-overlays-at (or pos
(point))))
4761 (while (setq ov
(pop overlays
))
4762 (if (org-overlay-get ov prop
)
4763 (if delete
(org-delete-overlay ov
) (push ov found
))))
4766 ;; Region compatibility
4768 (defun org-add-hook (hook function
&optional append local
)
4769 "Add-hook, compatible with both Emacsen."
4770 (if (and local
(featurep 'xemacs
))
4771 (add-local-hook hook function append
)
4772 (add-hook hook function append local
)))
4774 (defvar org-ignore-region nil
4775 "To temporarily disable the active region.")
4777 (defun org-region-active-p ()
4778 "Is `transient-mark-mode' on and the region active?
4779 Works on both Emacs and XEmacs."
4780 (if org-ignore-region
4782 (if (featurep 'xemacs
)
4783 (and zmacs-regions
(region-active-p))
4784 (if (fboundp 'use-region-p
)
4786 (and transient-mark-mode mark-active
))))) ; Emacs 22 and before
4788 ;; Invisibility compatibility
4790 (defun org-add-to-invisibility-spec (arg)
4791 "Add elements to `buffer-invisibility-spec'.
4792 See documentation for `buffer-invisibility-spec' for the kind of elements
4795 ((fboundp 'add-to-invisibility-spec
)
4796 (add-to-invisibility-spec arg
))
4797 ((or (null buffer-invisibility-spec
) (eq buffer-invisibility-spec t
))
4798 (setq buffer-invisibility-spec
(list arg
)))
4800 (setq buffer-invisibility-spec
4801 (cons arg buffer-invisibility-spec
)))))
4803 (defun org-remove-from-invisibility-spec (arg)
4804 "Remove elements from `buffer-invisibility-spec'."
4805 (if (fboundp 'remove-from-invisibility-spec
)
4806 (remove-from-invisibility-spec arg
)
4807 (if (consp buffer-invisibility-spec
)
4808 (setq buffer-invisibility-spec
4809 (delete arg buffer-invisibility-spec
)))))
4811 (defun org-in-invisibility-spec-p (arg)
4812 "Is ARG a member of `buffer-invisibility-spec'?"
4813 (if (consp buffer-invisibility-spec
)
4814 (member arg buffer-invisibility-spec
)
4817 ;;;; Define the Org-mode
4819 (if (and (not (keymapp outline-mode-map
)) (featurep 'allout
))
4820 (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."))
4823 ;; We use a before-change function to check if a table might need
4825 (defvar org-table-may-need-update t
4826 "Indicates that a table might need an update.
4827 This variable is set by `org-before-change-function'.
4828 `org-table-align' sets it back to nil.")
4829 (defvar org-mode-map
)
4830 (defvar org-mode-hook nil
)
4831 (defvar org-inhibit-startup nil
) ; Dynamically-scoped param.
4832 (defvar org-agenda-keep-modes nil
) ; Dynamically-scoped param.
4833 (defvar org-table-buffer-is-an nil
)
4834 (defconst org-outline-regexp
"\\*+ ")
4837 (define-derived-mode org-mode outline-mode
"Org"
4838 "Outline-based notes management and organizer, alias
4839 \"Carsten's outline-mode for keeping track of everything.\"
4841 Org-mode develops organizational tasks around a NOTES file which
4842 contains information about projects as plain text. Org-mode is
4843 implemented on top of outline-mode, which is ideal to keep the content
4844 of large files well structured. It supports ToDo items, deadlines and
4845 time stamps, which magically appear in the diary listing of the Emacs
4846 calendar. Tables are easily created with a built-in table editor.
4847 Plain text URL-like links connect to websites, emails (VM), Usenet
4848 messages (Gnus), BBDB entries, and any files related to the project.
4849 For printing and sharing of notes, an Org-mode file (or a part of it)
4850 can be exported as a structured ASCII or HTML file.
4852 The following commands are available:
4856 ;; Get rid of Outline menus, they are not needed
4857 ;; Need to do this here because define-derived-mode sets up
4858 ;; the keymap so late. Still, it is a waste to call this each time
4859 ;; we switch another buffer into org-mode.
4860 (if (featurep 'xemacs
)
4861 (when (boundp 'outline-mode-menu-heading
)
4862 ;; Assume this is Greg's port, it used easymenu
4863 (easy-menu-remove outline-mode-menu-heading
)
4864 (easy-menu-remove outline-mode-menu-show
)
4865 (easy-menu-remove outline-mode-menu-hide
))
4866 (define-key org-mode-map
[menu-bar headings
] 'undefined
)
4867 (define-key org-mode-map
[menu-bar hide
] 'undefined
)
4868 (define-key org-mode-map
[menu-bar show
] 'undefined
))
4870 (easy-menu-add org-org-menu
)
4871 (easy-menu-add org-tbl-menu
)
4872 (org-install-agenda-files-menu)
4873 (if org-descriptive-links
(org-add-to-invisibility-spec '(org-link)))
4874 (org-add-to-invisibility-spec '(org-cwidth))
4875 (when (featurep 'xemacs
)
4876 (org-set-local 'line-move-ignore-invisible t
))
4877 (org-set-local 'outline-regexp org-outline-regexp
)
4878 (org-set-local 'outline-level
'org-outline-level
)
4879 (when (and org-ellipsis
4880 (fboundp 'set-display-table-slot
) (boundp 'buffer-display-table
)
4881 (fboundp 'make-glyph-code
))
4882 (unless org-display-table
4883 (setq org-display-table
(make-display-table)))
4884 (set-display-table-slot
4887 (lambda (c) (make-glyph-code c
(and (not (stringp org-ellipsis
))
4889 (if (stringp org-ellipsis
) org-ellipsis
"..."))))
4890 (setq buffer-display-table org-display-table
))
4891 (org-set-regexps-and-options)
4893 (org-set-local 'calc-embedded-open-mode
"# ")
4894 (modify-syntax-entry ?
# "<")
4895 (modify-syntax-entry ?
@ "w")
4896 (if org-startup-truncated
(setq truncate-lines t
))
4897 (org-set-local 'font-lock-unfontify-region-function
4898 'org-unfontify-region
)
4899 ;; Activate before-change-function
4900 (org-set-local 'org-table-may-need-update t
)
4901 (org-add-hook 'before-change-functions
'org-before-change-function nil
4903 ;; Check for running clock before killing a buffer
4904 (org-add-hook 'kill-buffer-hook
'org-check-running-clock nil
'local
)
4905 ;; Paragraphs and auto-filling
4906 (org-set-autofill-regexps)
4907 (setq indent-line-function
'org-indent-line-function
)
4908 (org-update-radio-target-regexp)
4910 ;; Comment characters
4911 ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
4912 (org-set-local 'comment-padding
" ")
4914 ;; Align options lines
4916 'align-mode-rules-list
4917 '((org-in-buffer-settings
4918 (regexp .
"^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
4919 (modes .
'(org-mode)))))
4922 (org-set-local 'imenu-create-index-function
4923 'org-imenu-get-tree
)
4925 ;; Make isearch reveal context
4926 (if (or (featurep 'xemacs
)
4927 (not (boundp 'outline-isearch-open-invisible-function
)))
4928 ;; Emacs 21 and XEmacs make use of the hook
4929 (org-add-hook 'isearch-mode-end-hook
'org-isearch-end
'append
'local
)
4930 ;; Emacs 22 deals with this through a special variable
4931 (org-set-local 'outline-isearch-open-invisible-function
4932 (lambda (&rest ignore
) (org-show-context 'isearch
))))
4934 ;; If empty file that did not turn on org-mode automatically, make it to.
4935 (if (and org-insert-mode-line-in-empty-file
4937 (= (point-min) (point-max)))
4938 (insert "# -*- mode: org -*-\n\n"))
4940 (unless org-inhibit-startup
4941 (when org-startup-align-all-tables
4942 (let ((bmp (buffer-modified-p)))
4943 (org-table-map-tables 'org-table-align
)
4944 (set-buffer-modified-p bmp
)))
4945 (org-cycle-hide-drawers 'all
)
4947 ((eq org-startup-folded t
)
4949 ((eq org-startup-folded
'content
)
4950 (let ((this-command 'org-cycle
) (last-command 'org-cycle
))
4951 (org-cycle '(4)) (org-cycle '(4)))))))
4953 (put 'org-mode
'flyspell-mode-predicate
'org-mode-flyspell-verify
)
4955 (defsubst org-call-with-arg
(command arg
)
4956 "Call COMMAND interactively, but pretend prefix are was ARG."
4957 (let ((current-prefix-arg arg
)) (call-interactively command
)))
4959 (defsubst org-current-line
(&optional pos
)
4961 (and pos
(goto-char pos
))
4962 ;; works also in narrowed buffer, because we start at 1, not point-min
4963 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
4965 (defun org-current-time ()
4966 "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
4967 (if (> org-time-stamp-rounding-minutes
0)
4968 (let ((r org-time-stamp-rounding-minutes
)
4969 (time (decode-time)))
4971 (append (list 0 (* r
(floor (+ .5 (/ (float (nth 1 time
)) r
)))))
4975 (defun org-add-props (string plist
&rest props
)
4976 "Add text properties to entire string, from beginning to end.
4977 PLIST may be a list of properties, PROPS are individual properties and values
4978 that will be added to PLIST. Returns the string that was modified."
4979 (add-text-properties
4980 0 (length string
) (if props
(append plist props
) plist
) string
)
4982 (put 'org-add-props
'lisp-indent-function
2)
4985 ;;;; Font-Lock stuff, including the activators
4987 (defvar org-mouse-map
(make-sparse-keymap))
4988 (org-defkey org-mouse-map
4989 (if (featurep 'xemacs
) [button2] [mouse-2]) 'org-open-at-mouse)
4990 (org-defkey org-mouse-map
4991 (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
4992 (when org-mouse-1-follows-link
4993 (org-defkey org-mouse-map [follow-link] 'mouse-face))
4994 (when org-tab-follows-link
4995 (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
4996 (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
4997 (when org-return-follows-link
4998 (org-defkey org-mouse-map [(return)] 'org-open-at-point)
4999 (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
5001 (require 'font-lock)
5003 (defconst org-non-link-chars "]\t\n\r<>")
5004 (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news" "bbdb" "vm"
5005 "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp" "message"))
5006 (defvar org-link-re-with-space nil
5007 "Matches a link with spaces, optional angular brackets around it.")
5008 (defvar org-link-re-with-space2 nil
5009 "Matches a link with spaces, optional angular brackets around it.")
5010 (defvar org-angle-link-re nil
5011 "Matches link with angular brackets, spaces are allowed.")
5012 (defvar org-plain-link-re nil
5013 "Matches plain link, without spaces.")
5014 (defvar org-bracket-link-regexp nil
5015 "Matches a link in double brackets.")
5016 (defvar org-bracket-link-analytic-regexp nil
5017 "Regular expression used to analyze links.
5018 Here is what the match groups contain after a match:
5024 (defvar org-any-link-re nil
5025 "Regular expression matching any link.")
5027 (defun org-make-link-regexps ()
5028 "Update the link regular expressions.
5029 This should be called after the variable `org-link-types' has changed."
5030 (setq org-link-re-with-space
5032 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
5033 "\\([^" org-non-link-chars " ]"
5034 "[^" org-non-link-chars "]*"
5035 "[^" org-non-link-chars " ]\\)>?")
5036 org-link-re-with-space2
5038 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
5039 "\\([^" org-non-link-chars " ]"
5041 "[^" org-non-link-chars " ]\\)>?")
5044 "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
5045 "\\([^" org-non-link-chars " ]"
5046 "[^" org-non-link-chars "]*"
5050 "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
5051 "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
5052 org-bracket-link-regexp
5053 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
5054 org-bracket-link-analytic-regexp
5057 "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
5060 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5063 (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
5064 org-angle-link-re "\\)\\|\\("
5065 org-plain-link-re "\\)")))
5067 (org-make-link-regexps)
5069 (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
5070 "Regular expression for fast time stamp matching.")
5071 (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
5072 "Regular expression for fast time stamp matching.")
5073 (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5074 "Regular expression matching time strings for analysis.
5075 This one does not require the space after the date.")
5076 (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) \\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5077 "Regular expression matching time strings for analysis.")
5078 (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
5079 "Regular expression matching time stamps, with groups.")
5080 (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
5081 "Regular expression matching time stamps (also [..]), with groups.")
5082 (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
5083 "Regular expression matching a time stamp range.")
5084 (defconst org-tr-regexp-both
5085 (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
5086 "Regular expression matching a time stamp range.")
5087 (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
5088 org-ts-regexp "\\)?")
5089 "Regular expression matching a time stamp or time stamp range.")
5090 (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
5091 org-ts-regexp-both "\\)?")
5092 "Regular expression matching a time stamp or time stamp range.
5093 The time stamps may be either active or inactive.")
5095 (defvar org-emph-face nil)
5097 (defun org-do-emphasis-faces (limit)
5098 "Run through the buffer and add overlays to links."
5100 (while (and (not rtn) (re-search-forward org-emph-re limit t))
5101 (if (not (= (char-after (match-beginning 3))
5102 (char-after (match-beginning 4))))
5105 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
5107 (nth 1 (assoc (match-string 3)
5108 org-emphasis-alist)))
5109 (add-text-properties (match-beginning 2) (match-end 2)
5110 '(font-lock-multiline t))
5111 (when org-hide-emphasis-markers
5112 (add-text-properties (match-end 4) (match-beginning 5)
5113 '(invisible org-link))
5114 (add-text-properties (match-beginning 3) (match-end 3)
5115 '(invisible org-link)))))
5119 (defun org-emphasize (&optional char)
5120 "Insert or change an emphasis, i.e. a font like bold or italic.
5121 If there is an active region, change that region to a new emphasis.
5122 If there is no region, just insert the marker characters and position
5123 the cursor between them.
5124 CHAR should be either the marker character, or the first character of the
5125 HTML tag associated with that emphasis. If CHAR is a space, the means
5126 to remove the emphasis of the selected region.
5127 If char is not given (for example in an interactive call) it
5128 will be prompted for."
5130 (let ((eal org-emphasis-alist) e det
5131 (erc org-emphasis-regexp-components)
5133 (string "") beg end move tag c s)
5134 (if (org-region-active-p)
5135 (setq beg (region-beginning) end (region-end)
5136 string (buffer-substring beg end))
5139 (while (setq e (pop eal))
5140 (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
5142 (push (cons c (string-to-char (car e))) det)
5143 (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
5144 (substring tag 1)))))
5146 (message "%s" (concat "Emphasis marker or tag:" prompt))
5147 (setq char (read-char-exclusive)))
5148 (setq char (or (cdr (assoc char det)) char))
5149 (if (equal char ?\ )
5150 (setq s "" move nil)
5151 (unless (assoc (char-to-string char) org-emphasis-alist)
5152 (error "No such emphasis marker: \"%c\"" char))
5153 (setq s (char-to-string char)))
5154 (while (and (> (length string) 1)
5155 (equal (substring string 0 1) (substring string -1))
5156 (assoc (substring string 0 1) org-emphasis-alist))
5157 (setq string (substring string 1 -1)))
5158 (setq string (concat s string s))
5159 (if beg (delete-region beg end))
5161 (string-match (concat "[" (nth 0 erc) "\n]")
5162 (char-to-string (char-before (point)))))
5164 (unless (string-match (concat "[" (nth 1 erc) "\n]")
5165 (char-to-string (char-after (point))))
5166 (insert " ") (backward-char 1))
5168 (and move (backward-char 1))))
5170 (defconst org-nonsticky-props
5171 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
5174 (defun org-activate-plain-links (limit)
5175 "Run through the buffer and add overlays to links."
5178 (while (re-search-forward org-plain-link-re limit t)
5179 (setq f (get-text-property (match-beginning 0) 'face))
5180 (if (or (eq f 'org-tag)
5181 (and (listp f) (memq 'org-tag f)))
5183 (add-text-properties (match-beginning 0) (match-end 0)
5184 (list 'mouse-face 'highlight
5185 'rear-nonsticky org-nonsticky-props
5186 'keymap org-mouse-map
5188 (throw 'exit t))))))
5190 (defun org-activate-code (limit)
5191 (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
5192 (unless (get-text-property (match-beginning 1) 'face)
5193 (remove-text-properties (match-beginning 0) (match-end 0)
5194 '(display t invisible t intangible t))
5197 (defun org-activate-angle-links (limit)
5198 "Run through the buffer and add overlays to links."
5199 (if (re-search-forward org-angle-link-re limit t)
5201 (add-text-properties (match-beginning 0) (match-end 0)
5202 (list 'mouse-face 'highlight
5203 'rear-nonsticky org-nonsticky-props
5204 'keymap org-mouse-map
5208 (defmacro org-maybe-intangible (props)
5209 "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
5210 In emacs 21, invisible text is not avoided by the command loop, so the
5211 intangible property is needed to make sure point skips this text.
5212 In Emacs 22, this is not necessary. The intangible text property has
5213 led to problems with flyspell. These problems are fixed in flyspell.el,
5214 but we still avoid setting the property in Emacs 22 and later.
5215 We use a macro so that the test can happen at compilation time."
5216 (if (< emacs-major-version 22)
5217 `(append '(intangible t) ,props)
5220 (defun org-activate-bracket-links (limit)
5221 "Run through the buffer and add overlays to bracketed links."
5222 (if (re-search-forward org-bracket-link-regexp limit t)
5223 (let* ((help (concat "LINK: "
5224 (org-match-string-no-properties 1)))
5225 ;; FIXME: above we should remove the escapes.
5226 ;; but that requires another match, protecting match data,
5227 ;; a lot of overhead for font-lock.
5228 (ip (org-maybe-intangible
5229 (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
5230 'keymap org-mouse-map 'mouse-face 'highlight
5231 'font-lock-multiline t 'help-echo help)))
5232 (vp (list 'rear-nonsticky org-nonsticky-props
5233 'keymap org-mouse-map 'mouse-face 'highlight
5234 ' font-lock-multiline t 'help-echo help)))
5235 ;; We need to remove the invisible property here. Table narrowing
5236 ;; may have made some of this invisible.
5237 (remove-text-properties (match-beginning 0) (match-end 0)
5241 (add-text-properties (match-beginning 0) (match-beginning 3) ip)
5242 (add-text-properties (match-beginning 3) (match-end 3) vp)
5243 (add-text-properties (match-end 3) (match-end 0) ip))
5244 (add-text-properties (match-beginning 0) (match-beginning 1) ip)
5245 (add-text-properties (match-beginning 1) (match-end 1) vp)
5246 (add-text-properties (match-end 1) (match-end 0) ip))
5249 (defun org-activate-dates (limit)
5250 "Run through the buffer and add overlays to dates."
5251 (if (re-search-forward org-tsr-regexp-both limit t)
5253 (add-text-properties (match-beginning 0) (match-end 0)
5254 (list 'mouse-face 'highlight
5255 'rear-nonsticky org-nonsticky-props
5256 'keymap org-mouse-map))
5257 (when org-display-custom-times
5259 (org-display-custom-time (match-beginning 3) (match-end 3)))
5260 (org-display-custom-time (match-beginning 1) (match-end 1)))
5263 (defvar org-target-link-regexp nil
5264 "Regular expression matching radio targets in plain text.")
5265 (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
5266 "Regular expression matching a link target.")
5267 (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
5268 "Regular expression matching a radio target.")
5269 (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
5270 "Regular expression matching any target.")
5272 (defun org-activate-target-links (limit)
5273 "Run through the buffer and add overlays to target matches."
5274 (when org-target-link-regexp
5275 (let ((case-fold-search t))
5276 (if (re-search-forward org-target-link-regexp limit t)
5278 (add-text-properties (match-beginning 0) (match-end 0)
5279 (list 'mouse-face 'highlight
5280 'rear-nonsticky org-nonsticky-props
5281 'keymap org-mouse-map
5282 'help-echo "Radio target link"
5283 'org-linked-text t))
5286 (defun org-update-radio-target-regexp ()
5287 "Find all radio targets in this file and update the regular expression."
5289 (when (memq 'radio org-activate-links)
5290 (setq org-target-link-regexp
5291 (org-make-target-link-regexp (org-all-targets 'radio)))
5292 (org-restart-font-lock)))
5294 (defun org-hide-wide-columns (limit)
5296 (setq s (text-property-any (point) (or limit (point-max))
5299 (setq e (next-single-property-change s 'org-cwidth))
5300 (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
5304 (defvar org-latex-and-specials-regexp nil
5305 "Regular expression for highlighting export special stuff.")
5306 (defvar org-match-substring-regexp)
5307 (defvar org-match-substring-with-braces-regexp)
5308 (defvar org-export-html-special-string-regexps)
5310 (defun org-compute-latex-and-specials-regexp ()
5311 "Compute regular expression for stuff treated specially by exporters."
5312 (if (not org-highlight-latex-fragments-and-specials)
5313 (org-set-local 'org-latex-and-specials-regexp nil)
5315 ((matchers (plist-get org-format-latex-options :matchers))
5316 (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
5317 org-latex-regexps)))
5318 (options (org-combine-plists (org-default-export-plist)
5319 (org-infile-export-plist)))
5320 (org-export-with-sub-superscripts (plist-get options :sub-superscript))
5321 (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
5322 (org-export-with-TeX-macros (plist-get options :TeX-macros))
5323 (org-export-html-expand (plist-get options :expand-quoted-html))
5324 (org-export-with-special-strings (plist-get options :special-strings))
5327 ((equal org-export-with-sub-superscripts '{})
5328 (list org-match-substring-with-braces-regexp))
5329 (org-export-with-sub-superscripts
5330 (list org-match-substring-regexp))
5333 (if org-export-with-LaTeX-fragments
5334 (mapcar (lambda (x) (nth 1 x)) latexs)))
5336 (if org-export-with-TeX-macros
5337 (list (concat "\\\\"
5339 (append (mapcar 'car org-html-entities)
5340 (if (boundp 'org-latex-entities)
5341 org-latex-entities nil))
5344 ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
5345 (re-special (if org-export-with-special-strings
5346 (mapcar (lambda (x) (car x))
5347 org-export-html-special-string-regexps)))
5351 (if org-export-html-expand "@<[^>\n]+>")
5354 'org-latex-and-specials-regexp
5355 (mapconcat 'identity (append re-latex re-sub re-macros re-special
5358 (defface org-latex-and-export-specials
5359 (let ((font (cond ((assq :inherit custom-face-attributes)
5360 '(:inherit underline))
5361 (t '(:underline t)))))
5362 `((((class grayscale) (background light))
5363 (:foreground "DimGray" ,@font))
5364 (((class grayscale) (background dark))
5365 (:foreground "LightGray" ,@font))
5366 (((class color) (background light))
5367 (:foreground "SaddleBrown"))
5368 (((class color) (background dark))
5369 (:foreground "burlywood"))
5371 "Face used to highlight math latex and other special exporter stuff."
5374 (defun org-do-latex-and-special-faces (limit)
5375 "Run through the buffer and add overlays to links."
5376 (when org-latex-and-specials-regexp
5378 (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
5380 (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
5382 '(org-code org-verbatim underline)))
5385 d (cond ((member (char-after (1+ (match-beginning 0)))
5388 (font-lock-prepend-text-property
5389 (+ d (match-beginning 0)) (match-end 0)
5390 'face 'org-latex-and-export-specials)
5391 (add-text-properties (+ d (match-beginning 0)) (match-end 0)
5392 '(font-lock-multiline t)))))
5395 (defun org-restart-font-lock ()
5396 "Restart font-lock-mode, to force refontification."
5397 (when (and (boundp 'font-lock-mode) font-lock-mode)
5399 (font-lock-mode 1)))
5401 (defun org-all-targets (&optional radio)
5402 "Return a list of all targets in this file.
5403 With optional argument RADIO, only find radio targets."
5404 (let ((re (if radio org-radio-target-regexp org-target-regexp))
5407 (goto-char (point-min))
5408 (while (re-search-forward re nil t)
5409 (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
5412 (defun org-make-target-link-regexp (targets)
5413 "Make regular expression matching all strings in TARGETS.
5414 The regular expression finds the targets also if there is a line break
5421 (while (string-match " +" x)
5422 (setq x (replace-match "\\s-+" t t x)))
5428 (defun org-activate-tags (limit)
5429 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
5431 (add-text-properties (match-beginning 1) (match-end 1)
5432 (list 'mouse-face 'highlight
5433 'rear-nonsticky org-nonsticky-props
5434 'keymap org-mouse-map))
5437 (defun org-outline-level ()
5439 (looking-at outline-regexp)
5440 (if (match-beginning 1)
5441 (+ (org-get-string-indentation (match-string 1)) 1000)
5442 (1- (- (match-end 0) (match-beginning 0))))))
5444 (defvar org-font-lock-keywords nil)
5446 (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
5447 "Regular expression matching a property line.")
5449 (defun org-set-font-lock-defaults ()
5450 (let* ((em org-fontify-emphasized-text)
5451 (lk org-activate-links)
5452 (org-font-lock-extra-keywords
5455 '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
5456 (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
5458 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
5461 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
5462 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
5463 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
5465 (list org-drawer-regexp '(0 'org-special-keyword t))
5466 (list "^[ \t]*:END:" '(0 'org-special-keyword t))
5468 (list org-property-re
5469 '(1 'org-special-keyword t)
5470 '(3 'org-property-value t))
5471 (if org-format-transports-properties-p
5472 '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
5474 (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
5475 (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
5476 (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
5477 (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
5478 (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
5479 (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
5480 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
5481 '(org-hide-wide-columns (0 nil append))
5483 (list (concat "^\\*+[ \t]+" org-todo-regexp)
5484 '(1 (org-get-todo-face 1) t))
5486 (if org-fontify-done-headline
5487 (list (concat "^[*]+ +\\<\\("
5488 (mapconcat 'regexp-quote org-done-keywords "\\|")
5490 '(2 'org-headline-done t))
5493 (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
5495 (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
5496 (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
5497 (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
5498 (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
5501 (if (featurep 'xemacs)
5502 '(org-do-emphasis-faces (0 nil append))
5503 '(org-do-emphasis-faces)))
5505 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
5507 (if org-provide-checkbox-statistics
5508 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
5509 (0 (org-get-checkbox-statistics-face) t)))
5510 (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
5511 '(1 'org-archived prepend))
5513 '(org-do-latex-and-special-faces)
5515 '(org-activate-code (1 'org-code t))
5517 (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
5518 "\\|" org-quote-string "\\)\\>")
5519 '(1 'org-special-keyword t))
5520 '("^#.*" (0 'font-lock-comment-face t))
5522 (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
5523 ;; Now set the full font-lock-keywords
5524 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
5525 (org-set-local 'font-lock-defaults
5526 '(org-font-lock-keywords t nil nil backward-paragraph))
5527 (kill-local-variable 'font-lock-keywords) nil))
5532 (defun org-get-level-face (n)
5533 "Get the right face for match N in font-lock matching of healdines."
5534 (setq org-l (- (match-end 2) (match-beginning 1) 1))
5535 (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
5536 (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
5538 ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
5540 (t (if org-level-color-stars-only nil org-f))))
5542 (defun org-get-todo-face (kwd)
5543 "Get the right face for a TODO keyword KWD.
5544 If KWD is a number, get the corresponding match group."
5545 (if (numberp kwd) (setq kwd (match-string kwd)))
5546 (or (cdr (assoc kwd org-todo-keyword-faces))
5547 (and (member kwd org-done-keywords) 'org-done)
5550 (defun org-unfontify-region (beg end &optional maybe_loudly)
5551 "Remove fontification and activation overlays from links."
5552 (font-lock-default-unfontify-region beg end)
5553 (let* ((buffer-undo-list t)
5554 (inhibit-read-only t) (inhibit-point-motion-hooks t)
5555 (inhibit-modification-hooks t)
5556 deactivate-mark buffer-file-name buffer-file-truename)
5557 (remove-text-properties beg end
5558 '(mouse-face t keymap t org-linked-text t
5559 invisible t intangible t))))
5561 ;;;; Visibility cycling, including org-goto and indirect buffer
5565 (defvar org-cycle-global-status nil)
5566 (make-variable-buffer-local 'org-cycle-global-status)
5567 (defvar org-cycle-subtree-status nil)
5568 (make-variable-buffer-local 'org-cycle-subtree-status)
5571 (defun org-cycle (&optional arg)
5572 "Visibility cycling for Org-mode.
5574 - When this function is called with a prefix argument, rotate the entire
5575 buffer through 3 states (global cycling)
5576 1. OVERVIEW: Show only top-level headlines.
5577 2. CONTENTS: Show all headlines of all levels, but no body text.
5578 3. SHOW ALL: Show everything.
5580 - When point is at the beginning of a headline, rotate the subtree started
5581 by this line through 3 different states (local cycling)
5582 1. FOLDED: Only the main headline is shown.
5583 2. CHILDREN: The main headline and the direct children are shown.
5584 From this state, you can move to one of the children
5585 and zoom in further.
5586 3. SUBTREE: Show the entire subtree, including body text.
5588 - When there is a numeric prefix, go up to a heading with level ARG, do
5589 a `show-subtree' and return to the previous cursor position. If ARG
5590 is negative, go up that many levels.
5592 - When point is not at the beginning of a headline, execute
5593 `indent-relative', like TAB normally does. See the option
5594 `org-cycle-emulate-tab' for details.
5596 - Special case: if point is at the beginning of the buffer and there is
5597 no headline in line 1, this function will act as if called with prefix arg.
5598 But only if also the variable `org-cycle-global-at-bob' is t."
5600 (let* ((outline-regexp
5601 (if (and (org-mode-p) org-cycle-include-plain-lists)
5602 "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
5604 (bob-special (and org-cycle-global-at-bob (bobp)
5605 (not (looking-at outline-regexp))))
5608 (delq 'org-optimize-window-after-visibility-change
5609 (copy-sequence org-cycle-hook))
5613 (if (or bob-special (equal arg '(4)))
5614 ;; special case: use global cycling
5619 ((org-at-table-p 'any)
5620 ;; Enter the table or move to the next field in the table
5621 (or (org-table-recognize-table.el)
5623 (if arg (org-table-edit-field t)
5624 (org-table-justify-field-maybe)
5625 (call-interactively 'org-table-next-field)))))
5627 ((eq arg t) ;; Global cycling
5630 ((and (eq last-command this-command)
5631 (eq org-cycle-global-status 'overview))
5632 ;; We just created the overview - now do table of contents
5633 ;; This can be slow in very large buffers, so indicate action
5634 (message "CONTENTS...")
5636 (message "CONTENTS...done")
5637 (setq org-cycle-global-status 'contents)
5638 (run-hook-with-args 'org-cycle-hook 'contents))
5640 ((and (eq last-command this-command)
5641 (eq org-cycle-global-status 'contents))
5642 ;; We just showed the table of contents - now show everything
5644 (message "SHOW ALL")
5645 (setq org-cycle-global-status 'all)
5646 (run-hook-with-args 'org-cycle-hook 'all))
5649 ;; Default action: go to overview
5651 (message "OVERVIEW")
5652 (setq org-cycle-global-status 'overview)
5653 (run-hook-with-args 'org-cycle-hook 'overview))))
5655 ((and org-drawers org-drawer-regexp
5657 (beginning-of-line 1)
5658 (looking-at org-drawer-regexp)))
5659 ;; Toggle block visibility
5661 (not (get-char-property (match-end 0) 'invisible))))
5664 ;; Show-subtree, ARG levels up from here.
5666 (org-back-to-heading)
5667 (outline-up-heading (if (< arg 0) (- arg)
5668 (- (funcall outline-level) arg)))
5669 (org-show-subtree)))
5671 ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
5672 (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
5673 ;; At a heading: rotate between three different views
5674 (org-back-to-heading)
5675 (let ((goal-column 0) eoh eol eos)
5676 ;; First, some boundaries
5678 (org-back-to-heading)
5680 (beginning-of-line 2)
5681 (while (and (not (eobp)) ;; this is like `next-line'
5682 (get-char-property (1- (point)) 'invisible))
5683 (beginning-of-line 2)) (setq eol (point)))
5684 (outline-end-of-heading) (setq eoh (point))
5685 (org-end-of-subtree t)
5687 (skip-chars-forward " \t\n")
5688 (beginning-of-line 1) ; in case this is an item
5690 (setq eos (1- (point))))
5691 ;; Find out what to do next and set `this-command'
5694 ;; Nothing is hidden behind this heading
5695 (message "EMPTY ENTRY")
5696 (setq org-cycle-subtree-status nil)
5699 (outline-next-heading)
5700 (if (org-invisible-p) (org-flag-heading nil))))
5702 (not (string-match "\\S-" (buffer-substring eol eos))))
5703 ;; Entire subtree is hidden in one line: open it
5706 (message "CHILDREN")
5709 (outline-next-heading)
5710 (if (org-invisible-p) (org-flag-heading nil)))
5711 (setq org-cycle-subtree-status 'children)
5712 (run-hook-with-args 'org-cycle-hook 'children))
5713 ((and (eq last-command this-command)
5714 (eq org-cycle-subtree-status 'children))
5715 ;; We just showed the children, now show everything.
5718 (setq org-cycle-subtree-status 'subtree)
5719 (run-hook-with-args 'org-cycle-hook 'subtree))
5721 ;; Default action: hide the subtree.
5724 (setq org-cycle-subtree-status 'folded)
5725 (run-hook-with-args 'org-cycle-hook 'folded)))))
5728 (buffer-read-only (org-back-to-heading))
5730 ((org-try-cdlatex-tab))
5732 ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
5734 (not (looking-at outline-regexp))))
5735 (call-interactively (global-key-binding "\t")))
5737 ((if (and (memq org-cycle-emulate-tab '(white whitestart))
5738 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
5739 (or (and (eq org-cycle-emulate-tab 'white)
5740 (= (match-end 0) (point-at-eol)))
5741 (and (eq org-cycle-emulate-tab 'whitestart)
5742 (>= (match-end 0) pos))))
5744 (eq org-cycle-emulate-tab t))
5745 ; (if (and (looking-at "[ \n\r\t]")
5746 ; (string-match "^[ \t]*$" (buffer-substring
5747 ; (point-at-bol) (point))))
5749 ; (beginning-of-line 1)
5750 ; (and (looking-at "[ \t]+") (replace-match ""))))
5751 (call-interactively (global-key-binding "\t")))
5754 (org-back-to-heading)
5758 (defun org-global-cycle (&optional arg)
5759 "Cycle the global visibility. For details see `org-cycle'."
5761 (let ((org-cycle-include-plain-lists
5762 (if (org-mode-p) org-cycle-include-plain-lists nil)))
5766 (hide-sublevels arg)
5767 (setq org-cycle-global-status 'contents))
5770 (defun org-overview ()
5771 "Switch to overview mode, shoing only top-level headlines.
5772 Really, this shows all headlines with level equal or greater than the level
5773 of the first headline in the buffer. This is important, because if the
5774 first headline is not level one, then (hide-sublevels 1) gives confusing
5777 (let ((level (save-excursion
5778 (goto-char (point-min))
5779 (if (re-search-forward (concat "^" outline-regexp) nil t)
5781 (goto-char (match-beginning 0))
5782 (funcall outline-level))))))
5783 (and level (hide-sublevels level))))
5785 (defun org-content (&optional arg)
5786 "Show all headlines in the buffer, like a table of contents.
5787 With numerical argument N, show content up to level N."
5790 ;; Visit all headings and show their offspring
5791 (and (integerp arg) (org-overview))
5792 (goto-char (point-max))
5794 (while (and (progn (condition-case nil
5795 (outline-previous-visible-heading 1)
5796 (error (goto-char (point-min))))
5798 (looking-at outline-regexp))
5800 (show-children (1- arg))
5802 (if (bobp) (throw 'exit nil))))))
5805 (defun org-optimize-window-after-visibility-change (state)
5806 "Adjust the window after a change in outline visibility.
5807 This function is the default value of the hook `org-cycle-hook'."
5808 (when (get-buffer-window (current-buffer))
5810 ; ((eq state 'overview) (org-first-headline-recenter 1))
5811 ; ((eq state 'overview) (org-beginning-of-line))
5812 ((eq state 'content) nil)
5813 ((eq state 'all) nil)
5814 ((eq state 'folded) nil)
5815 ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
5816 ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
5818 (defun org-compact-display-after-subtree-move ()
5821 (if (org-up-heading-safe)
5826 (org-cycle-show-empty-lines 'children)
5827 (org-cycle-hide-drawers 'children))
5830 (defun org-cycle-show-empty-lines (state)
5831 "Show empty lines above all visible headlines.
5832 The region to be covered depends on STATE when called through
5833 `org-cycle-hook'. Lisp program can use t for STATE to get the
5834 entire buffer covered. Note that an empty line is only shown if there
5835 are at least `org-cycle-separator-lines' empty lines before the headeline."
5836 (when (> org-cycle-separator-lines 0)
5838 (let* ((n org-cycle-separator-lines)
5840 ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
5841 ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
5842 (t (let ((ns (number-to-string (- n 2))))
5843 (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
5844 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
5847 ((memq state '(overview contents t))
5848 (setq beg (point-min) end (point-max)))
5849 ((memq state '(children folded))
5850 (setq beg (point) end (progn (org-end-of-subtree t t)
5851 (beginning-of-line 2)
5855 (while (re-search-forward re end t)
5856 (if (not (get-char-property (match-end 1) 'invisible))
5857 (outline-flag-region
5858 (match-beginning 1) (match-end 1) nil)))))))
5859 ;; Never hide empty lines at the end of the file.
5861 (goto-char (point-max))
5862 (outline-previous-heading)
5863 (outline-end-of-heading)
5864 (if (and (looking-at "[ \t\n]+")
5865 (= (match-end 0) (point-max)))
5866 (outline-flag-region (point) (match-end 0) nil))))
5868 (defun org-subtree-end-visible-p ()
5869 "Is the end of the current subtree visible?"
5870 (pos-visible-in-window-p
5871 (save-excursion (org-end-of-subtree t) (point))))
5873 (defun org-first-headline-recenter (&optional N)
5874 "Move cursor to the first headline and recenter the headline.
5875 Optional argument N means, put the headline into the Nth line of the window."
5876 (goto-char (point-min))
5877 (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
5879 (recenter (prefix-numeric-value N))))
5883 (defvar org-goto-window-configuration nil)
5884 (defvar org-goto-marker nil)
5885 (defvar org-goto-map
5886 (let ((map (make-sparse-keymap)))
5887 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
5888 (while (setq cmd (pop cmds))
5889 (substitute-key-definition cmd cmd map global-map)))
5890 (suppress-keymap map)
5891 (org-defkey map "\C-m" 'org-goto-ret)
5892 (org-defkey map [(return)] 'org-goto-ret)
5893 (org-defkey map [(left)] 'org-goto-left)
5894 (org-defkey map [(right)] 'org-goto-right)
5895 (org-defkey map [(control ?g)] 'org-goto-quit)
5896 (org-defkey map "\C-i" 'org-cycle)
5897 (org-defkey map [(tab)] 'org-cycle)
5898 (org-defkey map [(down)] 'outline-next-visible-heading)
5899 (org-defkey map [(up)] 'outline-previous-visible-heading)
5900 (if org-goto-auto-isearch
5901 (if (fboundp 'define-key-after)
5902 (define-key-after map [t] 'org-goto-local-auto-isearch)
5904 (org-defkey map "q" 'org-goto-quit)
5905 (org-defkey map "n" 'outline-next-visible-heading)
5906 (org-defkey map "p" 'outline-previous-visible-heading)
5907 (org-defkey map "f" 'outline-forward-same-level)
5908 (org-defkey map "b" 'outline-backward-same-level)
5909 (org-defkey map "u" 'outline-up-heading))
5910 (org-defkey map "/" 'org-occur)
5911 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
5912 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
5913 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
5914 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
5915 (org-defkey map "\C-c\C-u" 'outline-up-heading)
5918 (defconst org-goto-help
5919 "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
5920 RET=jump to location [Q]uit and return to previous location
5921 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
5923 (defvar org-goto-start-pos) ; dynamically scoped parameter
5925 (defun org-goto (&optional alternative-interface)
5926 "Look up a different location in the current file, keeping current visibility.
5928 When you want look-up or go to a different location in a document, the
5929 fastest way is often to fold the entire buffer and then dive into the tree.
5930 This method has the disadvantage, that the previous location will be folded,
5931 which may not be what you want.
5933 This command works around this by showing a copy of the current buffer
5934 in an indirect buffer, in overview mode. You can dive into the tree in
5935 that copy, use org-occur and incremental search to find a location.
5936 When pressing RET or `Q', the command returns to the original buffer in
5937 which the visibility is still unchanged. After RET is will also jump to
5938 the location selected in the indirect buffer and expose the
5939 the headline hierarchy above."
5941 (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
5942 (org-refile-use-outline-path t)
5944 (if (not alternative-interface)
5946 (if (eq org-goto-interface 'outline)
5947 'outline-path-completion
5949 (org-goto-start-pos (point))
5951 (if (eq interface 'outline)
5952 (car (org-get-location (current-buffer) org-goto-help))
5953 (nth 3 (org-refile-get-location "Goto: ")))))
5956 (org-mark-ring-push org-goto-start-pos)
5957 (goto-char selected-point)
5958 (if (or (org-invisible-p) (org-invisible-p2))
5959 (org-show-context 'org-goto)))
5962 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
5963 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
5964 (defvar org-goto-local-auto-isearch-map) ; defined below
5966 (defun org-get-location (buf help)
5967 "Let the user select a location in the Org-mode buffer BUF.
5968 This function uses a recursive edit. It returns the selected position
5970 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
5971 (isearch-hide-immediately nil)
5972 (isearch-search-fun-function
5973 (lambda () 'org-goto-local-search-forward-headings))
5974 (org-goto-selected-point org-goto-exit-command))
5976 (save-window-excursion
5977 (delete-other-windows)
5978 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
5981 (make-indirect-buffer (current-buffer) "*org-goto*")
5982 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
5983 (with-output-to-temp-buffer "*Help*"
5985 (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
5986 (setq buffer-read-only nil)
5987 (let ((org-startup-truncated t)
5988 (org-startup-folded nil)
5989 (org-startup-align-all-tables nil))
5992 (setq buffer-read-only t)
5993 (if (and (boundp 'org-goto-start-pos)
5994 (integer-or-marker-p org-goto-start-pos))
5995 (let ((org-show-hierarchy-above t)
5996 (org-show-siblings t)
5997 (org-show-following-heading t))
5998 (goto-char org-goto-start-pos)
5999 (and (org-invisible-p) (org-show-context)))
6000 (goto-char (point-min)))
6001 (org-beginning-of-line)
6002 (message "Select location and press RET")
6003 (use-local-map org-goto-map)
6006 (kill-buffer "*org-goto*")
6007 (cons org-goto-selected-point org-goto-exit-command)))
6009 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
6010 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
6011 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
6012 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
6014 (defun org-goto-local-search-forward-headings (string bound noerror)
6015 "Search and make sure that anu matches are in headlines."
6017 (while (search-forward string bound noerror)
6018 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
6019 (and (member :headline context)
6020 (not (member :tags context))))
6021 (throw 'return (point))))))
6023 (defun org-goto-local-auto-isearch ()
6026 (goto-char (point-min))
6027 (let ((keys (this-command-keys)))
6028 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
6030 (isearch-process-search-char (string-to-char keys)))))
6032 (defun org-goto-ret (&optional arg)
6033 "Finish `org-goto' by going to the new location."
6035 (setq org-goto-selected-point (point)
6036 org-goto-exit-command 'return)
6039 (defun org-goto-left ()
6040 "Finish `org-goto' by going to the new location."
6042 (if (org-on-heading-p)
6044 (beginning-of-line 1)
6045 (setq org-goto-selected-point (point)
6046 org-goto-exit-command 'left)
6048 (error "Not on a heading")))
6050 (defun org-goto-right ()
6051 "Finish `org-goto' by going to the new location."
6053 (if (org-on-heading-p)
6055 (setq org-goto-selected-point (point)
6056 org-goto-exit-command 'right)
6058 (error "Not on a heading")))
6060 (defun org-goto-quit ()
6061 "Finish `org-goto' without cursor motion."
6063 (setq org-goto-selected-point nil)
6064 (setq org-goto-exit-command 'quit)
6067 ;;; Indirect buffer display of subtrees
6069 (defvar org-indirect-dedicated-frame nil
6070 "This is the frame being used for indirect tree display.")
6071 (defvar org-last-indirect-buffer nil)
6073 (defun org-tree-to-indirect-buffer (&optional arg)
6074 "Create indirect buffer and narrow it to current subtree.
6075 With numerical prefix ARG, go up to this level and then take that tree.
6076 If ARG is negative, go up that many levels.
6077 If `org-indirect-buffer-display' is not `new-frame', the command removes the
6078 indirect buffer previously made with this command, to avoid proliferation of
6079 indirect buffers. However, when you call the command with a `C-u' prefix, or
6080 when `org-indirect-buffer-display' is `new-frame', the last buffer
6081 is kept so that you can work with several indirect buffers at the same time.
6082 If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
6083 requests that a new frame be made for the new buffer, so that the dedicated
6084 frame is not changed."
6086 (let ((cbuf (current-buffer))
6087 (cwin (selected-window))
6089 beg end level heading ibuf)
6091 (org-back-to-heading t)
6093 (setq level (org-outline-level))
6094 (if (< arg 0) (setq arg (+ level arg)))
6095 (while (> (setq level (org-outline-level)) arg)
6096 (outline-up-heading 1 t)))
6098 heading (org-get-heading))
6099 (org-end-of-subtree t) (setq end (point)))
6100 (if (and (buffer-live-p org-last-indirect-buffer)
6101 (not (eq org-indirect-buffer-display 'new-frame))
6103 (kill-buffer org-last-indirect-buffer))
6104 (setq ibuf (org-get-indirect-buffer cbuf)
6105 org-last-indirect-buffer ibuf)
6107 ((or (eq org-indirect-buffer-display 'new-frame)
6108 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
6109 (select-frame (make-frame))
6110 (delete-other-windows)
6111 (switch-to-buffer ibuf)
6112 (org-set-frame-title heading))
6113 ((eq org-indirect-buffer-display 'dedicated-frame)
6115 (select-frame (or (and org-indirect-dedicated-frame
6116 (frame-live-p org-indirect-dedicated-frame)
6117 org-indirect-dedicated-frame)
6118 (setq org-indirect-dedicated-frame (make-frame)))))
6119 (delete-other-windows)
6120 (switch-to-buffer ibuf)
6121 (org-set-frame-title (concat "Indirect: " heading)))
6122 ((eq org-indirect-buffer-display 'current-window)
6123 (switch-to-buffer ibuf))
6124 ((eq org-indirect-buffer-display 'other-window)
6125 (pop-to-buffer ibuf))
6126 (t (error "Invalid value.")))
6127 (if (featurep 'xemacs)
6128 (save-excursion (org-mode) (turn-on-font-lock)))
6129 (narrow-to-region beg end)
6132 (and (window-live-p cwin) (select-window cwin))))
6134 (defun org-get-indirect-buffer (&optional buffer)
6135 (setq buffer (or buffer (current-buffer)))
6136 (let ((n 1) (base (buffer-name buffer)) bname)
6137 (while (buffer-live-p
6138 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
6141 (make-indirect-buffer buffer bname 'clone)
6142 (error (make-indirect-buffer buffer bname)))))
6144 (defun org-set-frame-title (title)
6145 "Set the title of the current frame to the string TITLE."
6146 ;; FIXME: how to name a single frame in XEmacs???
6147 (unless (featurep 'xemacs)
6148 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
6150 ;;;; Structure editing
6152 ;;; Inserting headlines
6154 (defun org-insert-heading (&optional force-heading)
6155 "Insert a new heading or item with same depth at point.
6156 If point is in a plain list and FORCE-HEADING is nil, create a new list item.
6157 If point is at the beginning of a headline, insert a sibling before the
6158 current headline. If point is not at the beginning, do not split the line,
6159 but create the new hedline after the current line."
6161 (if (= (buffer-size) 0)
6163 (when (or force-heading (not (org-insert-item)))
6164 (let* ((head (save-excursion
6167 (org-back-to-heading)
6170 (blank (cdr (assq 'heading org-blank-before-new-entry)))
6173 ((and (org-on-heading-p) (bolp)
6175 (save-excursion (backward-char 1) (not (org-invisible-p)))))
6176 ;; insert before the current line
6177 (open-line (if blank 2 1)))
6181 (backward-char 1) (not (org-invisible-p)))))
6182 ;; insert right here
6185 ;; in the middle of the line
6188 (newline (if blank 2 1))))
6189 (insert head) (just-one-space)
6192 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
6193 (run-hooks 'org-insert-heading-hook)))))
6195 (defun org-insert-heading-after-current ()
6196 "Insert a new heading with same level as current, after current subtree."
6198 (org-back-to-heading)
6199 (org-insert-heading)
6200 (org-move-subtree-down)
6203 (defun org-insert-todo-heading (arg)
6204 "Insert a new heading with the same level and TODO state as current heading.
6205 If the heading has no TODO state, or if the state is DONE, use the first
6206 state (TODO by default). Also with prefix arg, force first state."
6208 (when (not (org-insert-item 'checkbox))
6209 (org-insert-heading)
6211 (org-back-to-heading)
6212 (outline-previous-heading)
6213 (looking-at org-todo-line-regexp))
6215 (not (match-beginning 2))
6216 (member (match-string 2) org-done-keywords))
6217 (insert (car org-todo-keywords-1) " ")
6218 (insert (match-string 2) " "))))
6220 (defun org-insert-subheading (arg)
6221 "Insert a new subheading and demote it.
6222 Works for outline headings and for plain lists alike."
6224 (org-insert-heading arg)
6226 ((org-on-heading-p) (org-do-demote))
6227 ((org-at-item-p) (org-indent-item 1))))
6229 (defun org-insert-todo-subheading (arg)
6230 "Insert a new subheading with TODO keyword or checkbox and demote it.
6231 Works for outline headings and for plain lists alike."
6233 (org-insert-todo-heading arg)
6235 ((org-on-heading-p) (org-do-demote))
6236 ((org-at-item-p) (org-indent-item 1))))
6238 ;;; Promotion and Demotion
6240 (defun org-promote-subtree ()
6241 "Promote the entire subtree.
6242 See also `org-promote'."
6245 (org-map-tree 'org-promote))
6246 (org-fix-position-after-promote))
6248 (defun org-demote-subtree ()
6249 "Demote the entire subtree. See `org-demote'.
6250 See also `org-promote'."
6253 (org-map-tree 'org-demote))
6254 (org-fix-position-after-promote))
6257 (defun org-do-promote ()
6258 "Promote the current heading higher up the tree.
6259 If the region is active in `transient-mark-mode', promote all headings
6263 (if (org-region-active-p)
6264 (org-map-region 'org-promote (region-beginning) (region-end))
6266 (org-fix-position-after-promote))
6268 (defun org-do-demote ()
6269 "Demote the current heading lower down the tree.
6270 If the region is active in `transient-mark-mode', demote all headings
6274 (if (org-region-active-p)
6275 (org-map-region 'org-demote (region-beginning) (region-end))
6277 (org-fix-position-after-promote))
6279 (defun org-fix-position-after-promote ()
6280 "Make sure that after pro/demotion cursor position is right."
6281 (let ((pos (point)))
6282 (when (save-excursion
6283 (beginning-of-line 1)
6284 (looking-at org-todo-line-regexp)
6285 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
6286 (cond ((eobp) (insert " "))
6287 ((eolp) (insert " "))
6288 ((equal (char-after) ?\ ) (forward-char 1))))))
6290 (defun org-reduced-level (l)
6291 (if org-odd-levels-only (1+ (floor (/ l 2))) l))
6293 (defun org-get-legal-level (level &optional change)
6294 "Rectify a level change under the influence of `org-odd-levels-only'
6295 LEVEL is a current level, CHANGE is by how much the level should be
6296 modified. Even if CHANGE is nil, LEVEL may be returned modified because
6297 even level numbers will become the next higher odd number."
6298 (if org-odd-levels-only
6299 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
6300 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
6301 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
6302 (max 1 (+ level change))))
6304 (defun org-promote ()
6305 "Promote the current heading higher up the tree.
6306 If the region is active in `transient-mark-mode', promote all headings
6308 (org-back-to-heading t)
6309 (let* ((level (save-match-data (funcall outline-level)))
6310 (up-head (concat (make-string (org-get-legal-level level -1) ?*) " "))
6311 (diff (abs (- level (length up-head) -1))))
6312 (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
6313 (replace-match up-head nil t)
6314 ;; Fixup tag positioning
6315 (and org-auto-align-tags (org-set-tags nil t))
6316 (if org-adapt-indentation (org-fixup-indentation (- diff)))))
6318 (defun org-demote ()
6319 "Demote the current heading lower down the tree.
6320 If the region is active in `transient-mark-mode', demote all headings
6322 (org-back-to-heading t)
6323 (let* ((level (save-match-data (funcall outline-level)))
6324 (down-head (concat (make-string (org-get-legal-level level 1) ?*) " "))
6325 (diff (abs (- level (length down-head) -1))))
6326 (replace-match down-head nil t)
6327 ;; Fixup tag positioning
6328 (and org-auto-align-tags (org-set-tags nil t))
6329 (if org-adapt-indentation (org-fixup-indentation diff))))
6331 (defun org-map-tree (fun)
6332 "Call FUN for every heading underneath the current one."
6333 (org-back-to-heading)
6334 (let ((level (funcall outline-level)))
6338 (outline-next-heading)
6339 (> (funcall outline-level) level))
6343 (defun org-map-region (fun beg end)
6344 "Call FUN for every heading between BEG and END."
6345 (let ((org-ignore-region t))
6347 (setq end (copy-marker end))
6349 (if (and (re-search-forward (concat "^" outline-regexp) nil t)
6353 (outline-next-heading)
6358 (defun org-fixup-indentation (diff)
6359 "Change the indentation in the current entry by DIFF
6360 However, if any line in the current entry has no indentation, or if it
6361 would end up with no indentation after the change, nothing at all is done."
6363 (let ((end (save-excursion (outline-next-heading)
6365 (prohibit (if (> diff 0)
6367 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
6369 (unless (save-excursion (end-of-line 1)
6370 (re-search-forward prohibit end t))
6371 (while (and (< (point) end)
6372 (re-search-forward "^[ \t]+" end t))
6373 (goto-char (match-end 0))
6374 (setq col (current-column))
6375 (if (< diff 0) (replace-match ""))
6376 (indent-to (+ diff col))))
6377 (move-marker end nil))))
6379 (defun org-convert-to-odd-levels ()
6380 "Convert an org-mode file with all levels allowed to one with odd levels.
6381 This will leave level 1 alone, convert level 2 to level 3, level 3 to
6384 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
6385 (let ((org-odd-levels-only nil) n)
6387 (goto-char (point-min))
6388 (while (re-search-forward "^\\*\\*+ " nil t)
6389 (setq n (- (length (match-string 0)) 2))
6390 (while (>= (setq n (1- n)) 0)
6392 (end-of-line 1))))))
6395 (defun org-convert-to-oddeven-levels ()
6396 "Convert an org-mode file with only odd levels to one with odd and even levels.
6397 This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
6398 section with an even level, conversion would destroy the structure of the file. An error
6399 is signaled in this case."
6401 (goto-char (point-min))
6402 ;; First check if there are no even levels
6403 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
6404 (org-show-context t)
6405 (error "Not all levels are odd in this file. Conversion not possible."))
6406 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
6407 (let ((org-odd-levels-only nil) n)
6409 (goto-char (point-min))
6410 (while (re-search-forward "^\\*\\*+ " nil t)
6411 (setq n (/ (1- (length (match-string 0))) 2))
6412 (while (>= (setq n (1- n)) 0)
6414 (end-of-line 1))))))
6416 (defun org-tr-level (n)
6417 "Make N odd if required."
6418 (if org-odd-levels-only (1+ (/ n 2)) n))
6420 ;;; Vertical tree motion, cutting and pasting of subtrees
6422 (defun org-move-subtree-up (&optional arg)
6423 "Move the current subtree up past ARG headlines of the same level."
6425 (org-move-subtree-down (- (prefix-numeric-value arg))))
6427 (defun org-move-subtree-down (&optional arg)
6428 "Move the current subtree down past ARG headlines of the same level."
6430 (setq arg (prefix-numeric-value arg))
6431 (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
6432 'outline-get-last-sibling))
6433 (ins-point (make-marker))
6435 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
6437 (org-back-to-heading)
6440 (setq ne-beg (org-back-over-empty-lines))
6443 (save-excursion (outline-end-of-heading)
6444 (setq folded (org-invisible-p)))
6445 (outline-end-of-subtree))
6446 (outline-next-heading)
6447 (setq ne-end (org-back-over-empty-lines))
6450 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
6451 ;; include less whitespace
6454 (forward-line (- ne-beg ne-end))
6455 (setq beg (point))))
6456 ;; Find insertion point, with error handling
6458 (or (and (funcall movfunc) (looking-at outline-regexp))
6459 (progn (goto-char beg0)
6460 (error "Cannot move past superior level or buffer limit")))
6461 (setq cnt (1- cnt)))
6463 ;; Moving forward - still need to move over subtree
6464 (progn (org-end-of-subtree t t)
6466 (org-back-over-empty-lines)
6467 (or (bolp) (newline)))))
6468 (setq ne-ins (org-back-over-empty-lines))
6469 (move-marker ins-point (point))
6470 (setq txt (buffer-substring beg end))
6471 (delete-region beg end)
6472 (outline-flag-region (1- beg) beg nil)
6473 (outline-flag-region (1- (point)) (point) nil)
6475 (or (bolp) (insert "\n"))
6476 (setq ins-end (point))
6477 (goto-char ins-point)
6478 (org-skip-whitespace)
6479 (when (and (< arg 0)
6480 (org-first-sibling-p)
6482 ;; Move whitespace back to beginning
6485 (let ((kill-whole-line t))
6486 (kill-line (- ne-ins ne-beg)) (point)))
6487 (insert (make-string (- ne-ins ne-beg) ?\n)))
6488 (move-marker ins-point nil)
6489 (org-compact-display-after-subtree-move)
6493 (org-cycle-hide-drawers 'children))))
6495 (defvar org-subtree-clip ""
6496 "Clipboard for cut and paste of subtrees.
6497 This is actually only a copy of the kill, because we use the normal kill
6498 ring. We need it to check if the kill was created by `org-copy-subtree'.")
6500 (defvar org-subtree-clip-folded nil
6501 "Was the last copied subtree folded?
6502 This is used to fold the tree back after pasting.")
6504 (defun org-cut-subtree (&optional n)
6505 "Cut the current subtree into the clipboard.
6506 With prefix arg N, cut this many sequential subtrees.
6507 This is a short-hand for marking the subtree and then cutting it."
6509 (org-copy-subtree n 'cut))
6511 (defun org-copy-subtree (&optional n cut)
6512 "Cut the current subtree into the clipboard.
6513 With prefix arg N, cut this many sequential subtrees.
6514 This is a short-hand for marking the subtree and then copying it.
6515 If CUT is non-nil, actually cut the subtree."
6517 (let (beg end folded (beg0 (point)))
6519 (org-back-to-heading nil) ; take what looks like a subtree
6520 (org-back-to-heading t)) ; take what is really there
6521 (org-back-over-empty-lines)
6523 (skip-chars-forward " \t\r\n")
6525 (save-excursion (outline-end-of-heading)
6526 (setq folded (org-invisible-p)))
6528 (outline-forward-same-level (1- n))
6530 (org-end-of-subtree t t))
6531 (org-back-over-empty-lines)
6535 (setq org-subtree-clip-folded folded)
6536 (if cut (kill-region beg end) (copy-region-as-kill beg end))
6537 (setq org-subtree-clip (current-kill 0))
6538 (message "%s: Subtree(s) with %d characters"
6539 (if cut "Cut" "Copied")
6540 (length org-subtree-clip)))))
6542 (defun org-paste-subtree (&optional level tree)
6543 "Paste the clipboard as a subtree, with modification of headline level.
6544 The entire subtree is promoted or demoted in order to match a new headline
6545 level. By default, the new level is derived from the visible headings
6546 before and after the insertion point, and taken to be the inferior headline
6547 level of the two. So if the previous visible heading is level 3 and the
6548 next is level 4 (or vice versa), level 4 will be used for insertion.
6549 This makes sure that the subtree remains an independent subtree and does
6550 not swallow low level entries.
6552 You can also force a different level, either by using a numeric prefix
6553 argument, or by inserting the heading marker by hand. For example, if the
6554 cursor is after \"*****\", then the tree will be shifted to level 5.
6556 If you want to insert the tree as is, just use \\[yank].
6558 If optional TREE is given, use this text instead of the kill ring."
6560 (unless (org-kill-is-subtree-p tree)
6562 (substitute-command-keys
6563 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
6564 (let* ((txt (or tree (and kill-ring (current-kill 0))))
6565 (^re (concat "^\\(" outline-regexp "\\)"))
6566 (re (concat "\\(" outline-regexp "\\)"))
6567 (^re_ (concat "\\(\\*+\\)[ \t]*"))
6569 (old-level (if (string-match ^re txt)
6570 (- (match-end 0) (match-beginning 0) 1)
6572 (force-level (cond (level (prefix-numeric-value level))
6574 ^re_ (buffer-substring (point-at-bol) (point)))
6575 (- (match-end 1) (match-beginning 1)))
6577 (previous-level (save-excursion
6580 (outline-previous-visible-heading 1)
6582 (- (match-end 0) (match-beginning 0) 1)
6585 (next-level (save-excursion
6588 (or (looking-at outline-regexp)
6589 (outline-next-visible-heading 1))
6591 (- (match-end 0) (match-beginning 0) 1)
6594 (new-level (or force-level (max previous-level next-level)))
6595 (shift (if (or (= old-level -1)
6597 (= old-level new-level))
6599 (- new-level old-level)))
6600 (delta (if (> shift 0) -1 1))
6601 (func (if (> shift 0) 'org-demote 'org-promote))
6602 (org-odd-levels-only nil)
6604 ;; Remove the forced level indicator
6606 (delete-region (point-at-bol) (point)))
6608 (beginning-of-line 1)
6609 (org-back-over-empty-lines) ;; FIXME: correct fix????
6611 (insert-before-markers txt) ;; FIXME: correct fix????
6612 (unless (string-match "\n\\'" txt) (insert "\n"))
6615 (skip-chars-forward " \t\n\r")
6617 ;; Shift if necessary
6620 (narrow-to-region beg end)
6621 (while (not (= shift 0))
6622 (org-map-region func (point-min) (point-max))
6623 (setq shift (+ delta shift)))
6624 (goto-char (point-min))))
6625 (when (interactive-p)
6626 (message "Clipboard pasted as level %d subtree" new-level))
6628 (eq org-subtree-clip (current-kill 0))
6629 org-subtree-clip-folded)
6630 ;; The tree was folded before it was killed/copied
6633 (defun org-kill-is-subtree-p (&optional txt)
6634 "Check if the current kill is an outline subtree, or a set of trees.
6635 Returns nil if kill does not start with a headline, or if the first
6636 headline level is not the largest headline level in the tree.
6637 So this will actually accept several entries of equal levels as well,
6638 which is OK for `org-paste-subtree'.
6639 If optional TXT is given, check this string instead of the current kill."
6640 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
6641 (start-level (and kill
6642 (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
6643 org-outline-regexp "\\)")
6645 (- (match-end 2) (match-beginning 2) 1)))
6646 (re (concat "^" org-outline-regexp))
6647 (start (1+ (match-beginning 2))))
6648 (if (not start-level)
6650 nil) ;; does not even start with a heading
6652 (while (setq start (string-match re kill (1+ start)))
6653 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
6657 (defun org-narrow-to-subtree ()
6658 "Narrow buffer to the current subtree."
6662 (progn (org-back-to-heading) (point))
6663 (progn (org-end-of-subtree t t) (point)))))
6668 (defun org-sort (with-case)
6669 "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
6670 Optional argument WITH-CASE means sort case-sensitively."
6672 (if (org-at-table-p)
6673 (org-call-with-arg 'org-table-sort-lines with-case)
6674 (org-call-with-arg 'org-sort-entries-or-items with-case)))
6676 (defvar org-priority-regexp) ; defined later in the file
6678 (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
6679 "Sort entries on a certain level of an outline tree.
6680 If there is an active region, the entries in the region are sorted.
6681 Else, if the cursor is before the first entry, sort the top-level items.
6682 Else, the children of the entry at point are sorted.
6684 Sorting can be alphabetically, numerically, and by date/time as given by
6685 the first time stamp in the entry. The command prompts for the sorting
6686 type unless it has been given to the function through the SORTING-TYPE
6687 argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
6688 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
6689 called with point at the beginning of the record. It must return either
6690 a string or a number that should serve as the sorting key for that record.
6692 Comparing entries ignores case by default. However, with an optional argument
6693 WITH-CASE, the sorting considers case as well."
6695 (let ((case-func (if with-case 'identity 'downcase))
6696 start beg end stars re re2
6697 txt what tmp plain-list-p)
6698 ;; Find beginning and end of region to sort
6700 ((org-region-active-p)
6701 ;; we will sort the region
6702 (setq end (region-end)
6704 (goto-char (region-beginning))
6705 (if (not (org-on-heading-p)) (outline-next-heading))
6706 (setq start (point)))
6708 ;; we will sort this plain list
6709 (org-beginning-of-item-list) (setq start (point))
6710 (org-end-of-item-list) (setq end (point))
6712 (setq plain-list-p t
6714 ((or (org-on-heading-p)
6715 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
6716 ;; we will sort the children of the current headline
6717 (org-back-to-heading)
6719 end (progn (org-end-of-subtree t t)
6720 (org-back-over-empty-lines)
6725 (outline-next-heading))
6727 ;; we will sort the top-level entries in this file
6728 (goto-char (point-min))
6729 (or (org-on-heading-p) (outline-next-heading))
6730 (setq start (point) end (point-max) what "top-level")
6735 (if (>= beg end) (error "Nothing to sort"))
6737 (unless plain-list-p
6738 (looking-at "\\(\\*+\\)")
6739 (setq stars (match-string 1)
6740 re (concat "^" (regexp-quote stars) " +")
6741 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
6742 txt (buffer-substring beg end))
6743 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
6744 (if (and (not (equal stars "*")) (string-match re2 txt))
6745 (error "Region to sort contains a level above the first entry")))
6747 (unless sorting-type
6750 "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
6751 "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
6753 (setq sorting-type (read-char-exclusive))
6755 (and (= (downcase sorting-type) ?f)
6757 (completing-read "Sort using function: "
6758 obarray 'fboundp t nil nil))
6759 (setq getkey-func (intern getkey-func)))
6761 (and (= (downcase sorting-type) ?r)
6763 (completing-read "Property: "
6764 (mapcar 'list (org-buffer-property-keys t))
6767 (message "Sorting entries...")
6770 (narrow-to-region start end)
6772 (let ((dcst (downcase sorting-type))
6773 (now (current-time)))
6775 (/= dcst sorting-type)
6776 ;; This function moves to the beginning character of the "record" to
6780 (if (org-at-item-p) t (goto-char (point-max))))
6782 (if (re-search-forward re nil t)
6783 (goto-char (match-beginning 0))
6784 (goto-char (point-max)))))
6785 ;; This function moves to the last character of the "record" being
6792 (outline-forward-same-level 1)
6794 (goto-char (point-max)))))))
6796 ;; This function returns the value that gets sorted against.
6799 (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
6802 (string-to-number (buffer-substring (match-end 0)
6805 (buffer-substring (match-end 0) (point-at-eol)))
6807 (if (re-search-forward org-ts-regexp
6809 (org-time-string-to-time (match-string 0))
6814 (setq tmp (funcall getkey-func))
6815 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
6817 (error "Invalid key function `%s'" getkey-func)))
6818 (t (error "Invalid sorting type `%c'" sorting-type)))))
6822 (if (looking-at outline-regexp)
6823 (string-to-number (buffer-substring (match-end 0)
6827 (funcall case-func (buffer-substring (point-at-bol)
6830 (if (re-search-forward org-ts-regexp
6834 (org-time-string-to-time (match-string 0))
6837 (if (re-search-forward org-priority-regexp (point-at-eol) t)
6838 (string-to-char (match-string 2))
6839 org-default-priority))
6841 (or (org-entry-get nil property) ""))
6845 (setq tmp (funcall getkey-func))
6846 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
6848 (error "Invalid key function `%s'" getkey-func)))
6849 (t (error "Invalid sorting type `%c'" sorting-type)))))
6852 ((= dcst ?a) 'string<)
6853 ((= dcst ?t) 'time-less-p)
6855 (message "Sorting entries...done")))
6857 (defun org-do-sort (table what &optional with-case sorting-type)
6858 "Sort TABLE of WHAT according to SORTING-TYPE.
6859 The user will be prompted for the SORTING-TYPE if the call to this
6860 function does not specify it. WHAT is only for the prompt, to indicate
6861 what is being sorted. The sorting key will be extracted from
6862 the car of the elements of the table.
6863 If WITH-CASE is non-nil, the sorting will be case-sensitive."
6864 (unless sorting-type
6866 "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
6868 (setq sorting-type (read-char-exclusive)))
6869 (let ((dcst (downcase sorting-type))
6870 extractfun comparefun)
6871 ;; Define the appropriate functions
6874 (setq extractfun 'string-to-number
6875 comparefun (if (= dcst sorting-type) '< '>)))
6877 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
6878 (lambda(x) (downcase (org-sort-remove-invisible x))))
6879 comparefun (if (= dcst sorting-type)
6881 (lambda (a b) (and (not (string< a b))
6882 (not (string= a b)))))))
6886 (if (string-match org-ts-regexp x)
6888 (org-time-string-to-time (match-string 0 x)))
6890 comparefun (if (= dcst sorting-type) '< '>)))
6891 (t (error "Invalid sorting type `%c'" sorting-type)))
6893 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
6895 (lambda (a b) (funcall comparefun (car a) (car b))))))
6897 ;;;; Plain list items, including checkboxes
6899 ;;; Plain list items
6901 (defun org-at-item-p ()
6902 "Is point in a line starting a hand-formatted item?"
6903 (let ((llt org-plain-list-ordered-item-terminator))
6905 (goto-char (point-at-bol))
6908 ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
6909 ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
6910 ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
6911 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
6913 (defun org-in-item-p ()
6914 "It the cursor inside a plain list item.
6915 Does not have to be the first line."
6919 (org-beginning-of-item)
6924 (defun org-insert-item (&optional checkbox)
6925 "Insert a new item at the current level.
6926 Return t when things worked, nil when we are not in an item."
6927 (when (save-excursion
6930 (org-beginning-of-item)
6932 (if (org-invisible-p) (error "Invisible item"))
6935 (let* ((bul (match-string 0))
6936 (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
6938 (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
6941 ((and (org-at-item-p) (<= (point) eow))
6942 ;; before the bullet
6943 (beginning-of-line 1)
6944 (open-line (if blank 2 1)))
6946 (beginning-of-line 1))
6947 (t (newline (if blank 2 1))))
6948 (insert bul (if checkbox "[ ]" ""))
6952 (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
6953 (org-maybe-renumber-ordered-list)
6954 (and checkbox (org-update-checkbox-count-maybe))
6959 (defun org-at-item-checkbox-p ()
6960 "Is point at a line starting a plain-list item with a checklet?"
6961 (and (org-at-item-p)
6963 (goto-char (match-end 0))
6964 (skip-chars-forward " \t")
6965 (looking-at "\\[[- X]\\]"))))
6967 (defun org-toggle-checkbox (&optional arg)
6968 "Toggle the checkbox in the current line."
6971 (let (beg end status (firstnew 'unknown))
6973 ((org-region-active-p)
6974 (setq beg (region-beginning) end (region-end)))
6976 (setq beg (point) end (save-excursion (outline-next-heading) (point))))
6977 ((org-at-item-checkbox-p)
6978 (let ((pos (point)))
6981 ((member (match-string 0) '("[ ]" "[-]")) "[X]")
6986 (t (error "Not at a checkbox or heading, and no active region")))
6989 (while (< (point) end)
6990 (when (org-at-item-checkbox-p)
6991 (setq status (equal (match-string 0) "[X]"))
6992 (when (eq firstnew 'unknown)
6993 (setq firstnew (not status)))
6995 (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
6996 (beginning-of-line 2)))))
6997 (org-update-checkbox-count-maybe))
6999 (defun org-update-checkbox-count-maybe ()
7000 "Update checkbox statistics unless turned off by user."
7001 (when org-provide-checkbox-statistics
7002 (org-update-checkbox-count)))
7004 (defun org-update-checkbox-count (&optional all)
7005 "Update the checkbox statistics in the current section.
7006 This will find all statistic cookies like [57%] and [6/12] and update them
7007 with the current numbers. With optional prefix argument ALL, do this for
7011 (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
7012 (beg (condition-case nil
7013 (progn (outline-back-to-heading) (point))
7014 (error (point-min))))
7015 (end (move-marker (make-marker)
7016 (progn (outline-next-heading) (point))))
7017 (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
7018 (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
7019 (re-find (concat re "\\|" re-box))
7020 beg-cookie end-cookie is-percent c-on c-off lim
7021 eline curr-ind next-ind continue-from startsearch
7025 (goto-char (point-min))
7026 (outline-next-heading)
7027 (setq beg (point) end (point-max)))
7029 ;; find each statistic cookie
7030 (while (re-search-backward re-find beg t)
7031 (setq beg-cookie (match-beginning 1)
7032 end-cookie (match-end 1)
7033 cstat (+ cstat (if end-cookie 1 0))
7034 startsearch (point-at-eol)
7035 continue-from (point-at-bol)
7036 is-percent (match-beginning 2)
7038 ((org-on-heading-p) (outline-next-heading) (point))
7039 ((org-at-item-p) (org-end-of-item) (point))
7044 ;; find first checkbox for this cookie and gather
7045 ;; statistics from all that are at this indentation level
7046 (goto-char startsearch)
7047 (if (re-search-forward re-box lim t)
7049 (org-beginning-of-item)
7050 (setq curr-ind (org-get-indentation))
7051 (setq next-ind curr-ind)
7052 (while (= curr-ind next-ind)
7053 (save-excursion (end-of-line) (setq eline (point)))
7054 (if (re-search-forward re-box eline t)
7055 (if (member (match-string 2) '("[ ]" "[-]"))
7056 (setq c-off (1+ c-off))
7057 (setq c-on (1+ c-on))
7061 (setq next-ind (org-get-indentation))
7063 (goto-char continue-from)
7066 (delete-region beg-cookie end-cookie)
7067 (goto-char beg-cookie)
7070 (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
7071 (format "[%d/%d]" c-on (+ c-on c-off)))))
7072 ;; update items checkbox if it has one
7073 (when (org-at-item-p)
7074 (org-beginning-of-item)
7075 (when (and (> (+ c-on c-off) 0)
7076 (re-search-forward re-box (point-at-eol) t))
7077 (setq beg-cookie (match-beginning 2)
7078 end-cookie (match-end 2))
7079 (delete-region beg-cookie end-cookie)
7080 (goto-char beg-cookie)
7081 (cond ((= c-off 0) (insert "[X]"))
7082 ((= c-on 0) (insert "[ ]"))
7085 (goto-char continue-from))
7086 (when (interactive-p)
7087 (message "Checkbox satistics updated %s (%d places)"
7088 (if all "in entire file" "in current outline entry") cstat)))))
7090 (defun org-get-checkbox-statistics-face ()
7091 "Select the face for checkbox statistics.
7092 The face will be `org-done' when all relevant boxes are checked. Otherwise
7093 it will be `org-todo'."
7095 (if (equal (match-string 1) "100%") 'org-done 'org-todo)
7096 (if (and (> (match-end 2) (match-beginning 2))
7097 (equal (match-string 2) (match-string 3)))
7101 (defun org-get-indentation (&optional line)
7102 "Get the indentation of the current line, interpreting tabs.
7103 When LINE is given, assume it represents a line and compute its indentation."
7105 (if (string-match "^ *" (org-remove-tabs line))
7108 (beginning-of-line 1)
7109 (skip-chars-forward " \t")
7112 (defun org-remove-tabs (s &optional width)
7113 "Replace tabulators in S with spaces.
7114 Assumes that s is a single line, starting in column 0."
7115 (setq width (or width tab-width))
7116 (while (string-match "\t" s)
7117 (setq s (replace-match
7119 (- (* width (/ (+ (match-beginning 0) width) width))
7120 (match-beginning 0)) ?\ )
7124 (defun org-fix-indentation (line ind)
7125 "Fix indentation in LINE.
7126 IND is a cons cell with target and minimum indentation.
7127 If the current indenation in LINE is smaller than the minimum,
7128 leave it alone. If it is larger than ind, set it to the target."
7129 (let* ((l (org-remove-tabs line))
7130 (i (org-get-indentation l))
7131 (i1 (car ind)) (i2 (cdr ind)))
7132 (if (>= i i2) (setq l (substring line i2)))
7134 (concat (make-string i1 ?\ ) l)
7137 (defcustom org-empty-line-terminates-plain-lists nil
7138 "Non-nil means, an empty line ends all plain list levels.
7139 When nil, empty lines are part of the preceeding item."
7140 :group 'org-plain-lists
7143 (defun org-beginning-of-item ()
7144 "Go to the beginning of the current hand-formatted item.
7145 If the cursor is not in an item, throw an error."
7148 (limit (save-excursion
7151 (org-back-to-heading)
7152 (beginning-of-line 2) (point))
7153 (error (point-min)))))
7154 (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
7157 (beginning-of-line 1)
7158 (beginning-of-line 1)
7159 (skip-chars-forward " \t")
7160 (setq ind (current-column))
7163 (beginning-of-line 0)
7164 (if (or (bobp) (< (point) limit)) (throw 'exit nil))
7166 (if (looking-at "[ \t]*$")
7167 (setq ind1 ind-empty)
7168 (skip-chars-forward " \t")
7169 (setq ind1 (current-column)))
7171 (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
7174 (error "Not in an item")))))
7176 (defun org-end-of-item ()
7177 "Go to the end of the current hand-formatted item.
7178 If the cursor is not in an item, throw an error."
7180 (let* ((pos (point))
7182 (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
7183 (limit (save-excursion (outline-next-heading) (point)))
7184 (ind (save-excursion
7185 (org-beginning-of-item)
7186 (skip-chars-forward " \t")
7190 (beginning-of-line 2)
7191 (if (eobp) (throw 'exit (point)))
7192 (if (>= (point) limit) (throw 'exit (point-at-bol)))
7193 (if (looking-at "[ \t]*$")
7194 (setq ind1 ind-empty)
7195 (skip-chars-forward " \t")
7196 (setq ind1 (current-column)))
7198 (throw 'exit (point-at-bol)))))))
7202 (error "Not in an item"))))
7204 (defun org-next-item ()
7205 "Move to the beginning of the next item in the current plain list.
7206 Error if not at a plain list, or if this is the last item in the list."
7208 (let (ind ind1 (pos (point)))
7209 (org-beginning-of-item)
7210 (setq ind (org-get-indentation))
7212 (setq ind1 (org-get-indentation))
7213 (unless (and (org-at-item-p) (= ind ind1))
7215 (error "On last item"))))
7217 (defun org-previous-item ()
7218 "Move to the beginning of the previous item in the current plain list.
7219 Error if not at a plain list, or if this is the first item in the list."
7221 (let (beg ind ind1 (pos (point)))
7222 (org-beginning-of-item)
7224 (setq ind (org-get-indentation))
7228 (beginning-of-line 0)
7229 (if (looking-at "[ \t]*$")
7231 (if (<= (setq ind1 (org-get-indentation)) ind)
7234 (if (or (not (org-at-item-p))
7237 (org-beginning-of-item))
7238 (error (goto-char pos)
7239 (error "On first item")))))
7241 (defun org-first-list-item-p ()
7242 "Is this heading the item in a plain list?"
7243 (unless (org-at-item-p)
7244 (error "Not at a plain list item"))
7245 (org-beginning-of-item)
7246 (= (point) (save-excursion (org-beginning-of-item-list))))
7248 (defun org-move-item-down ()
7249 "Move the plain list item at point down, i.e. swap with following item.
7250 Subitems (items with larger indentation) are considered part of the item,
7251 so this really moves item trees."
7253 (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
7254 (org-beginning-of-item)
7257 (setq ne-beg (org-back-over-empty-lines))
7260 (setq ind (org-get-indentation))
7263 (setq ind1 (org-get-indentation))
7264 (setq ne-end (org-back-over-empty-lines))
7267 (when (and (org-first-list-item-p) (< ne-end ne-beg))
7268 ;; include less whitespace
7271 (forward-line (- ne-beg ne-end))
7272 (setq beg (point))))
7274 (if (and (org-at-item-p) (= ind ind1))
7277 (org-back-over-empty-lines)
7278 (setq txt (buffer-substring beg end))
7280 (delete-region beg end))
7283 (goto-char pos) (org-skip-whitespace)
7284 (org-maybe-renumber-ordered-list))
7286 (error "Cannot move this item further down"))))
7288 (defun org-move-item-up (arg)
7289 "Move the plain list item at point up, i.e. swap with previous item.
7290 Subitems (items with larger indentation) are considered part of the item,
7291 so this really moves item trees."
7293 (let (beg beg0 end end0 ind ind1 (pos (point)) txt
7294 ne-beg ne-end ne-ins ins-end)
7295 (org-beginning-of-item)
7297 (setq ind (org-get-indentation))
7299 (setq ne-beg (org-back-over-empty-lines))
7303 (setq ne-end (org-back-over-empty-lines))
7308 (beginning-of-line 0)
7309 (if (looking-at "[ \t]*$")
7310 (if org-empty-line-terminates-plain-lists
7313 (error "Cannot move this item further up"))
7315 (if (<= (setq ind1 (org-get-indentation)) ind)
7318 (org-beginning-of-item)
7319 (error (goto-char beg)
7320 (error "Cannot move this item further up")))
7321 (setq ind1 (org-get-indentation))
7322 (if (and (org-at-item-p) (= ind ind1))
7324 (setq ne-ins (org-back-over-empty-lines))
7325 (setq txt (buffer-substring beg end))
7327 (delete-region beg end))
7330 (setq ins-end (point))
7331 (goto-char pos) (org-skip-whitespace)
7333 (when (and (org-first-list-item-p) (> ne-ins ne-beg))
7334 ;; Move whitespace back to beginning
7337 (let ((kill-whole-line t))
7338 (kill-line (- ne-ins ne-beg)) (point)))
7339 (insert (make-string (- ne-ins ne-beg) ?\n)))
7341 (org-maybe-renumber-ordered-list))
7343 (error "Cannot move this item further up"))))
7345 (defun org-maybe-renumber-ordered-list ()
7346 "Renumber the ordered list at point if setup allows it.
7347 This tests the user option `org-auto-renumber-ordered-lists' before
7348 doing the renumbering."
7350 (when (and org-auto-renumber-ordered-lists
7352 (if (match-beginning 3)
7353 (org-renumber-ordered-list 1)
7354 (org-fix-bullet-type))))
7356 (defun org-maybe-renumber-ordered-list-safe ()
7359 (org-maybe-renumber-ordered-list))
7362 (defun org-cycle-list-bullet (&optional which)
7363 "Cycle through the different itemize/enumerate bullets.
7364 This cycle the entire list level through the sequence:
7366 `-' -> `+' -> `*' -> `1.' -> `1)'
7368 If WHICH is a string, use that as the new bullet. If WHICH is an integer,
7369 0 meand `-', 1 means `+' etc."
7372 (org-beginning-of-item-list)
7374 (beginning-of-line 1)
7375 (let ((current (match-string 0))
7376 (prevp (eq which 'previous))
7379 ((and (numberp which)
7380 (nth (1- which) '("-" "+" "*" "1." "1)"))))
7381 ((string-match "-" current) (if prevp "1)" "+"))
7382 ((string-match "\\+" current)
7383 (if prevp "-" (if (looking-at "\\S-") "1." "*")))
7384 ((string-match "\\*" current) (if prevp "+" "1."))
7385 ((string-match "\\." current) (if prevp "*" "1)"))
7386 ((string-match ")" current) (if prevp "1." "-"))
7387 (t (error "This should not happen"))))
7388 (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
7389 (org-fix-bullet-type)
7390 (org-maybe-renumber-ordered-list))))
7392 (defun org-get-string-indentation (s)
7393 "What indentation has S due to SPACE and TAB at the beginning of the string?"
7394 (let ((n -1) (i 0) (w tab-width) c)
7396 (while (< (setq n (1+ n)) (length s))
7398 (cond ((= c ?\ ) (setq i (1+ i)))
7399 ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
7400 (t (throw 'exit t)))))
7403 (defun org-renumber-ordered-list (arg)
7404 "Renumber an ordered plain list.
7405 Cursor needs to be in the first line of an item, the line that starts
7406 with something like \"1.\" or \"2)\"."
7408 (unless (and (org-at-item-p)
7409 (match-beginning 3))
7410 (error "This is not an ordered list"))
7411 (let ((line (org-current-line))
7412 (col (current-column))
7413 (ind (org-get-string-indentation
7414 (buffer-substring (point-at-bol) (match-beginning 3))))
7415 ;; (term (substring (match-string 3) -1))
7418 ;; find where this list begins
7419 (org-beginning-of-item-list)
7420 (looking-at "[ \t]*[0-9]+\\([.)]\\)")
7421 (setq fmt (concat "%d" (match-string 1)))
7422 (beginning-of-line 0)
7423 ;; walk forward and replace these numbers
7427 (beginning-of-line 2)
7428 (if (eobp) (throw 'exit nil))
7429 (if (looking-at "[ \t]*$") (throw 'next nil))
7430 (skip-chars-forward " \t") (setq ind1 (current-column))
7431 (if (> ind1 ind) (throw 'next t))
7432 (if (< ind1 ind) (throw 'exit t))
7433 (if (not (org-at-item-p)) (throw 'exit nil))
7434 (delete-region (match-beginning 2) (match-end 2))
7435 (goto-char (match-beginning 2))
7436 (insert (format fmt (setq n (1+ n)))))))
7438 (move-to-column col)))
7440 (defun org-fix-bullet-type ()
7441 "Make sure all items in this list have the same bullet as the firsst item."
7443 (unless (org-at-item-p) (error "This is not a list"))
7444 (let ((line (org-current-line))
7445 (col (current-column))
7446 (ind (current-indentation))
7448 ;; find where this list begins
7449 (org-beginning-of-item-list)
7450 (beginning-of-line 1)
7451 ;; find out what the bullet type is
7452 (looking-at "[ \t]*\\(\\S-+\\)")
7453 (setq bullet (match-string 1))
7454 ;; walk forward and replace these numbers
7455 (beginning-of-line 0)
7459 (beginning-of-line 2)
7460 (if (eobp) (throw 'exit nil))
7461 (if (looking-at "[ \t]*$") (throw 'next nil))
7462 (skip-chars-forward " \t") (setq ind1 (current-column))
7463 (if (> ind1 ind) (throw 'next t))
7464 (if (< ind1 ind) (throw 'exit t))
7465 (if (not (org-at-item-p)) (throw 'exit nil))
7466 (skip-chars-forward " \t")
7467 (looking-at "\\S-+")
7468 (replace-match bullet))))
7470 (move-to-column col)
7471 (if (string-match "[0-9]" bullet)
7472 (org-renumber-ordered-list 1))))
7474 (defun org-beginning-of-item-list ()
7475 "Go to the beginning of the current item list.
7476 I.e. to the first item in this list."
7478 (org-beginning-of-item)
7479 (let ((pos (point-at-bol))
7480 (ind (org-get-indentation))
7482 ;; find where this list begins
7486 (beginning-of-line 0)
7487 (if (looking-at "[ \t]*$")
7488 (throw (if (bobp) 'exit 'next) t))
7489 (skip-chars-forward " \t") (setq ind1 (current-column))
7490 (if (or (< ind1 ind)
7492 (not (org-at-item-p)))
7495 (when (org-at-item-p) (setq pos (point-at-bol)))))))
7499 (defun org-end-of-item-list ()
7500 "Go to the end of the current item list.
7501 I.e. to the text after the last item."
7503 (org-beginning-of-item)
7504 (let ((pos (point-at-bol))
7505 (ind (org-get-indentation))
7507 ;; find where this list begins
7511 (beginning-of-line 2)
7512 (if (looking-at "[ \t]*$")
7513 (throw (if (eobp) 'exit 'next) t))
7514 (skip-chars-forward " \t") (setq ind1 (current-column))
7515 (if (or (< ind1 ind)
7517 (not (org-at-item-p)))
7520 (setq pos (point-at-bol))
7521 (throw 'exit t))))))
7525 (defvar org-last-indent-begin-marker (make-marker))
7526 (defvar org-last-indent-end-marker (make-marker))
7528 (defun org-outdent-item (arg)
7529 "Outdent a local list item."
7531 (org-indent-item (- arg)))
7533 (defun org-indent-item (arg)
7534 "Indent a local list item."
7536 (unless (org-at-item-p)
7537 (error "Not on an item"))
7539 (let (beg end ind ind1 tmp delta ind-down ind-up)
7540 (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
7541 (setq beg org-last-indent-begin-marker
7542 end org-last-indent-end-marker)
7543 (org-beginning-of-item)
7544 (setq beg (move-marker org-last-indent-begin-marker (point)))
7546 (setq end (move-marker org-last-indent-end-marker (point))))
7548 (setq tmp (org-item-indent-positions)
7550 ind-down (nth 2 tmp)
7553 (if ind-down (- ind-down ind) 2)
7554 (if ind-up (- ind-up ind) -2)))
7555 (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
7556 (while (< (point) end)
7557 (beginning-of-line 1)
7558 (skip-chars-forward " \t") (setq ind1 (current-column))
7559 (delete-region (point-at-bol) (point))
7560 (or (eolp) (indent-to-column (+ ind1 delta)))
7561 (beginning-of-line 2))))
7562 (org-fix-bullet-type)
7563 (org-maybe-renumber-ordered-list-safe)
7565 (beginning-of-line 0)
7566 (condition-case nil (org-beginning-of-item) (error nil))
7567 (org-maybe-renumber-ordered-list-safe)))
7569 (defun org-item-indent-positions ()
7570 "Return indentation for plain list items.
7571 This returns a list with three values: The current indentation, the
7572 parent indentation and the indentation a child should habe.
7573 Assumes cursor in item line."
7574 (let* ((bolpos (point-at-bol))
7575 (ind (org-get-indentation))
7576 ind-down ind-up pos)
7578 (org-beginning-of-item-list)
7579 (skip-chars-backward "\n\r \t")
7580 (when (org-in-item-p)
7581 (org-beginning-of-item)
7582 (setq ind-up (org-get-indentation))))
7586 ((and (condition-case nil (progn (org-previous-item) t)
7588 (or (forward-char 1) t)
7589 (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
7590 (setq ind-down (org-get-indentation)))
7591 ((and (goto-char pos)
7593 (goto-char (match-end 0))
7594 (skip-chars-forward " \t")
7595 (setq ind-down (current-column)))))
7596 (list ind ind-up ind-down)))
7598 ;;; The orgstruct minor mode
7600 ;; Define a minor mode which can be used in other modes in order to
7601 ;; integrate the org-mode structure editing commands.
7603 ;; This is really a hack, because the org-mode structure commands use
7604 ;; keys which normally belong to the major mode. Here is how it
7605 ;; works: The minor mode defines all the keys necessary to operate the
7606 ;; structure commands, but wraps the commands into a function which
7607 ;; tests if the cursor is currently at a headline or a plain list
7608 ;; item. If that is the case, the structure command is used,
7609 ;; temporarily setting many Org-mode variables like regular
7610 ;; expressions for filling etc. However, when any of those keys is
7611 ;; used at a different location, function uses `key-binding' to look
7612 ;; up if the key has an associated command in another currently active
7613 ;; keymap (minor modes, major mode, global), and executes that
7614 ;; command. There might be problems if any of the keys is otherwise
7615 ;; used as a prefix key.
7617 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
7618 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
7619 ;; addresses this by checking explicitly for both bindings.
7621 (defvar orgstruct-mode-map (make-sparse-keymap)
7622 "Keymap for the minor `orgstruct-mode'.")
7624 (defvar org-local-vars nil
7625 "List of local variables, for use by `orgstruct-mode'")
7628 (define-minor-mode orgstruct-mode
7629 "Toggle the minor more `orgstruct-mode'.
7630 This mode is for using Org-mode structure commands in other modes.
7631 The following key behave as if Org-mode was active, if the cursor
7632 is on a headline, or on a plain list item (both in the definition
7635 M-up Move entry/item up
7636 M-down Move entry/item down
7639 M-S-up Move entry/item up
7640 M-S-down Move entry/item down
7641 M-S-left Promote subtree
7642 M-S-right Demote subtree
7643 M-q Fill paragraph and items like in Org-mode
7645 C-c - Cycle list bullet
7646 TAB Cycle item visibility
7647 M-RET Insert new heading/item
7648 S-M-RET Insert new TODO heading / Chekbox item
7649 C-c C-c Set tags / toggle checkbox"
7650 nil " OrgStruct" nil
7651 (and (orgstruct-setup) (defun orgstruct-setup () nil)))
7654 (defun turn-on-orgstruct ()
7655 "Unconditionally turn on `orgstruct-mode'."
7659 (defun turn-on-orgstruct++ ()
7660 "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
7661 In addition to setting orgstruct-mode, this also exports all indentation and
7662 autofilling variables from org-mode into the buffer. Note that turning
7663 off orgstruct-mode will *not* remove these additional settings."
7669 "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
7670 (symbol-name (car x)))
7671 (setq var (car x) val (nth 1 x))
7672 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
7675 (defun orgstruct-error ()
7676 "Error when there is no default binding for a structure key."
7678 (error "This key has no function outside structure elements"))
7680 (defun orgstruct-setup ()
7681 "Setup orgstruct keymaps."
7685 '([(meta up)] org-metaup)
7686 '([(meta down)] org-metadown)
7687 '([(meta left)] org-metaleft)
7688 '([(meta right)] org-metaright)
7689 '([(meta shift up)] org-shiftmetaup)
7690 '([(meta shift down)] org-shiftmetadown)
7691 '([(meta shift left)] org-shiftmetaleft)
7692 '([(meta shift right)] org-shiftmetaright)
7693 '([(shift up)] org-shiftup)
7694 '([(shift down)] org-shiftdown)
7695 '("\C-c\C-c" org-ctrl-c-ctrl-c)
7696 '("\M-q" fill-paragraph)
7698 '("\C-c-" org-cycle-list-bullet)))
7700 (while (setq elt (pop bindings))
7701 (setq nfunc (1+ nfunc))
7702 (setq key (org-key (car elt))
7704 cmd (orgstruct-make-binding fun nfunc key))
7705 (org-defkey orgstruct-mode-map key cmd))
7707 ;; Special treatment needed for TAB and RET
7708 (org-defkey orgstruct-mode-map [(tab)]
7709 (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
7710 (org-defkey orgstruct-mode-map "\C-i"
7711 (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
7713 (org-defkey orgstruct-mode-map "\M-\C-m"
7714 (orgstruct-make-binding 'org-insert-heading 105
7715 "\M-\C-m" [(meta return)]))
7716 (org-defkey orgstruct-mode-map [(meta return)]
7717 (orgstruct-make-binding 'org-insert-heading 106
7718 [(meta return)] "\M-\C-m"))
7720 (org-defkey orgstruct-mode-map [(shift meta return)]
7721 (orgstruct-make-binding 'org-insert-todo-heading 107
7722 [(meta return)] "\M-\C-m"))
7724 (unless org-local-vars
7725 (setq org-local-vars (org-get-local-variables)))
7729 (defun orgstruct-make-binding (fun n &rest keys)
7730 "Create a function for binding in the structure minor mode.
7731 FUN is the command to call inside a table. N is used to create a unique
7732 command name. KEYS are keys that should be checked in for a command
7733 to execute outside of tables."
7736 (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
7738 (concat "In Structure, run `" (symbol-name fun) "'.\n"
7739 "Outside of structure, run the binding of `"
7740 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
7744 '(org-context-p 'headline 'item)
7745 (list 'org-run-like-in-org-mode (list 'quote fun))
7746 (list 'let '(orgstruct-mode)
7747 (list 'call-interactively
7750 (list 'key-binding k))
7752 '('orgstruct-error))))))))
7754 (defun org-context-p (&rest contexts)
7755 "Check if local context is and of CONTEXTS.
7756 Possible values in the list of contexts are `table', `headline', and `item'."
7757 (let ((pos (point)))
7758 (goto-char (point-at-bol))
7759 (prog1 (or (and (memq 'table contexts)
7760 (looking-at "[ \t]*|"))
7761 (and (memq 'headline contexts)
7762 (looking-at "\\*+"))
7763 (and (memq 'item contexts)
7764 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
7767 (defun org-get-local-variables ()
7768 "Return a list of all local variables in an org-mode buffer."
7770 (with-current-buffer (get-buffer-create "*Org tmp*")
7773 (setq varlist (buffer-local-variables)))
7774 (kill-buffer "*Org tmp*")
7781 (list (car x) (list 'quote (cdr x)))))
7783 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
7784 (symbol-name (car x)))
7789 (defun org-run-like-in-org-mode (cmd)
7790 (unless org-local-vars
7791 (setq org-local-vars (org-get-local-variables)))
7792 (eval (list 'let org-local-vars
7793 (list 'call-interactively (list 'quote cmd)))))
7797 (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
7799 (defun org-archive-subtree (&optional find-done)
7800 "Move the current subtree to the archive.
7801 The archive can be a certain top-level heading in the current file, or in
7802 a different file. The tree will be moved to that location, the subtree
7803 heading be marked DONE, and the current time will be added.
7805 When called with prefix argument FIND-DONE, find whole trees without any
7806 open TODO items and archive them (after getting confirmation from the user).
7807 If the cursor is not at a headline when this comand is called, try all level
7808 1 trees. If the cursor is on a headline, only try the direct children of
7812 (org-archive-all-done)
7813 ;; Save all relevant TODO keyword-relatex variables
7815 (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
7816 (tr-org-todo-keywords-1 org-todo-keywords-1)
7817 (tr-org-todo-kwd-alist org-todo-kwd-alist)
7818 (tr-org-done-keywords org-done-keywords)
7819 (tr-org-todo-regexp org-todo-regexp)
7820 (tr-org-todo-line-regexp org-todo-line-regexp)
7821 (tr-org-odd-levels-only org-odd-levels-only)
7822 (this-buffer (current-buffer))
7823 (org-archive-location org-archive-location)
7824 (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
7825 ;; start of variables that will be used for saving context
7826 ;; The compiler complains about them - keep them anyway!
7827 (file (abbreviate-file-name (buffer-file-name)))
7828 (olpath (mapconcat 'identity (org-get-outline-path) "/"))
7829 (time (format-time-string
7830 (substring (cdr org-time-stamp-formats) 1 -1)
7832 afile heading buffer level newfile-p
7833 category todo priority
7834 ;; start of variables that will be used for savind context
7837 ;; Try to find a local archive location
7841 (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
7842 (if (and prop (string-match "\\S-" prop))
7843 (setq org-archive-location prop)
7844 (if (or (re-search-backward re nil t)
7845 (re-search-forward re nil t))
7846 (setq org-archive-location (match-string 1))))))
7848 (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
7850 (setq afile (format (match-string 1 org-archive-location)
7851 (file-name-nondirectory buffer-file-name))
7852 heading (match-string 2 org-archive-location)))
7853 (error "Invalid `org-archive-location'"))
7854 (if (> (length afile) 0)
7855 (setq newfile-p (not (file-exists-p afile))
7856 buffer (find-file-noselect afile))
7857 (setq buffer (current-buffer)))
7859 (error "Cannot access file \"%s\"" afile))
7860 (if (and (> (length heading) 0)
7861 (string-match "^\\*+" heading))
7862 (setq level (match-end 0))
7863 (setq heading nil level 0))
7865 (org-back-to-heading t)
7866 ;; Get context information that will be lost by moving the tree
7867 (org-refresh-category-properties)
7868 (setq category (org-get-category)
7869 todo (and (looking-at org-todo-line-regexp)
7871 priority (org-get-priority (if (match-end 3) (match-string 3) ""))
7872 ltags (org-get-tags)
7873 itags (org-delete-all ltags (org-get-tags-at)))
7874 (setq ltags (mapconcat 'identity ltags " ")
7875 itags (mapconcat 'identity itags " "))
7876 ;; We first only copy, in case something goes wrong
7877 ;; we need to protect this-command, to avoid kill-region sets it,
7878 ;; which would lead to duplication of subtrees
7879 (let (this-command) (org-copy-subtree))
7881 ;; Enforce org-mode for the archive buffer
7882 (if (not (org-mode-p))
7883 ;; Force the mode for future visits.
7884 (let ((org-insert-mode-line-in-empty-file t)
7885 (org-inhibit-startup t))
7886 (call-interactively 'org-mode)))
7888 (goto-char (point-max))
7889 (insert (format "\nArchived entries from file %s\n\n"
7890 (buffer-file-name this-buffer))))
7891 ;; Force the TODO keywords of the original buffer
7892 (let ((org-todo-line-regexp tr-org-todo-line-regexp)
7893 (org-todo-keywords-1 tr-org-todo-keywords-1)
7894 (org-todo-kwd-alist tr-org-todo-kwd-alist)
7895 (org-done-keywords tr-org-done-keywords)
7896 (org-todo-regexp tr-org-todo-regexp)
7897 (org-todo-line-regexp tr-org-todo-line-regexp)
7898 (org-odd-levels-only
7899 (if (local-variable-p 'org-odd-levels-only (current-buffer))
7901 tr-org-odd-levels-only)))
7902 (goto-char (point-min))
7905 (if (re-search-forward
7906 (concat "^" (regexp-quote heading)
7907 (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
7909 (goto-char (match-end 0))
7910 ;; Heading not found, just insert it at the end
7911 (goto-char (point-max))
7912 (or (bolp) (insert "\n"))
7913 (insert "\n" heading "\n")
7915 ;; Make the subtree visible
7917 (org-end-of-subtree t)
7918 (skip-chars-backward " \t\r\n")
7919 (and (looking-at "[ \t\r\n]*")
7920 (replace-match "\n\n")))
7921 ;; No specific heading, just go to end of file.
7922 (goto-char (point-max)) (insert "\n"))
7924 (org-paste-subtree (org-get-legal-level level 1))
7926 ;; Mark the entry as done
7927 (when (and org-archive-mark-done
7928 (looking-at org-todo-line-regexp)
7929 (or (not (match-end 2))
7930 (not (member (match-string 2) org-done-keywords))))
7933 (car (or (member org-archive-mark-done org-done-keywords)
7934 org-done-keywords)))))
7936 ;; Add the context info
7937 (when org-archive-save-context-info
7938 (let ((l org-archive-save-context-info) e n v)
7939 (while (setq e (pop l))
7940 (when (and (setq v (symbol-value e))
7941 (stringp v) (string-match "\\S-" v))
7942 (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
7943 (org-entry-put (point) n v)))))
7945 ;; Save the buffer, if it is not the same buffer.
7946 (if (not (eq this-buffer buffer)) (save-buffer))))
7947 ;; Here we are back in the original buffer. Everything seems to have
7948 ;; worked. So now cut the tree and finish up.
7949 (let (this-command) (org-cut-subtree))
7950 (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
7951 (message "Subtree archived %s"
7952 (if (eq this-buffer buffer)
7953 (concat "under heading: " heading)
7954 (concat "in file: " (abbreviate-file-name afile)))))))
7956 (defun org-refresh-category-properties ()
7957 "Refresh category text properties in teh buffer."
7958 (let ((def-cat (cond
7959 ((null org-category)
7960 (if buffer-file-name
7961 (file-name-sans-extension
7962 (file-name-nondirectory buffer-file-name))
7964 ((symbolp org-category) (symbol-name org-category))
7966 beg end cat pos optionp)
7971 (goto-char (point-min))
7972 (put-text-property (point) (point-max) 'org-category def-cat)
7973 (while (re-search-forward
7974 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
7975 (setq pos (match-end 0)
7976 optionp (equal (char-after (match-beginning 0)) ?#)
7977 cat (org-trim (match-string 2)))
7979 (setq beg (point-at-bol) end (point-max))
7980 (org-back-to-heading t)
7981 (setq beg (point) end (org-end-of-subtree t t)))
7982 (put-text-property beg end 'org-category cat)
7983 (goto-char pos)))))))
7985 (defun org-archive-all-done (&optional tag)
7986 "Archive sublevels of the current tree without open TODO items.
7987 If the cursor is not on a headline, try all level 1 trees. If
7988 it is on a headline, try all direct children.
7989 When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
7990 (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
7991 (rea (concat ".*:" org-archive-tag ":"))
7992 (begm (make-marker))
7993 (endm (make-marker))
7994 (question (if tag "Set ARCHIVE tag (no open TODO items)? "
7995 "Move subtree to archive (no open TODO items)? "))
7996 beg end (cntarch 0))
7997 (if (org-on-heading-p)
7999 (setq re1 (concat "^" (regexp-quote
8001 (1+ (- (match-end 0) (match-beginning 0) 1))
8004 (move-marker begm (point))
8005 (move-marker endm (org-end-of-subtree t)))
8007 (move-marker begm (point-min))
8008 (move-marker endm (point-max)))
8011 (while (re-search-forward re1 endm t)
8012 (setq beg (match-beginning 0)
8013 end (save-excursion (org-end-of-subtree t) (point)))
8015 (if (re-search-forward re end t)
8018 (if (and (or (not tag) (not (looking-at rea)))
8019 (y-or-n-p question))
8022 (org-toggle-tag org-archive-tag 'on)
8023 (org-archive-subtree))
8024 (setq cntarch (1+ cntarch)))
8026 (message "%d trees archived" cntarch)))
8028 (defun org-cycle-hide-drawers (state)
8029 "Re-hide all drawers after a visibility state change."
8030 (when (and (org-mode-p)
8031 (not (memq state '(overview folded))))
8033 (let* ((globalp (memq state '(contents all)))
8034 (beg (if globalp (point-min) (point)))
8035 (end (if globalp (point-max) (org-end-of-subtree t))))
8037 (while (re-search-forward org-drawer-regexp end t)
8038 (org-flag-drawer t))))))
8040 (defun org-flag-drawer (flag)
8042 (beginning-of-line 1)
8043 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
8044 (let ((b (match-end 0))
8045 (outline-regexp org-outline-regexp))
8046 (if (re-search-forward
8048 (save-excursion (outline-next-heading) (point)) t)
8049 (outline-flag-region b (point-at-eol) flag)
8050 (error ":END: line missing"))))))
8052 (defun org-cycle-hide-archived-subtrees (state)
8053 "Re-hide all archived subtrees after a visibility state change."
8054 (when (and (not org-cycle-open-archived-trees)
8055 (not (memq state '(overview folded))))
8057 (let* ((globalp (memq state '(contents all)))
8058 (beg (if globalp (point-min) (point)))
8059 (end (if globalp (point-max) (org-end-of-subtree t))))
8060 (org-hide-archived-subtrees beg end)
8062 (if (looking-at (concat ".*:" org-archive-tag ":"))
8063 (message "%s" (substitute-command-keys
8064 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
8066 (defun org-force-cycle-archived ()
8067 "Cycle subtree even if it is archived."
8069 (setq this-command 'org-cycle)
8070 (let ((org-cycle-open-archived-trees t))
8071 (call-interactively 'org-cycle)))
8073 (defun org-hide-archived-subtrees (beg end)
8074 "Re-hide all archived subtrees after a visibility state change."
8076 (let* ((re (concat ":" org-archive-tag ":")))
8078 (while (re-search-forward re end t)
8079 (and (org-on-heading-p) (hide-subtree))
8080 (org-end-of-subtree t)))))
8082 (defun org-toggle-tag (tag &optional onoff)
8083 "Toggle the tag TAG for the current line.
8084 If ONOFF is `on' or `off', don't toggle but set to this state."
8085 (unless (org-on-heading-p t) (error "Not on headling"))
8089 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
8092 (setq current (match-string 1))
8095 (setq current (nreverse (org-split-string current ":")))
8099 (or (member tag current) (push tag current)))
8101 (or (not (member tag current)) (setq current (delete tag current))))
8102 (t (if (member tag current)
8103 (setq current (delete tag current))
8105 (push tag current))))
8109 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
8110 (org-set-tags nil t))
8111 (delete-horizontal-space))
8112 (run-hooks 'org-after-tags-change-hook))
8115 (defun org-toggle-archive-tag (&optional arg)
8116 "Toggle the archive tag for the current headline.
8117 With prefix ARG, check all children of current headline and offer tagging
8118 the children that do not contain any open TODO items."
8121 (org-archive-all-done 'tag)
8124 (org-back-to-heading t)
8125 (setq set (org-toggle-tag org-archive-tag))
8126 (when set (hide-subtree)))
8127 (and set (beginning-of-line 1))
8128 (message "Subtree %s" (if set "archived" "unarchived")))))
8133 ;;; The table editor
8135 ;; Watch out: Here we are talking about two different kind of tables.
8136 ;; Most of the code is for the tables created with the Org-mode table editor.
8137 ;; Sometimes, we talk about tables created and edited with the table.el
8138 ;; Emacs package. We call the former org-type tables, and the latter
8139 ;; table.el-type tables.
8141 (defun org-before-change-function (beg end)
8142 "Every change indicates that a table might need an update."
8143 (setq org-table-may-need-update t))
8145 (defconst org-table-line-regexp "^[ \t]*|"
8146 "Detects an org-type table line.")
8147 (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
8148 "Detects an org-type table line.")
8149 (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
8150 "Detects a table line marked for automatic recalculation.")
8151 (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
8152 "Detects a table line marked for automatic recalculation.")
8153 (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
8154 "Detects a table line marked for automatic recalculation.")
8155 (defconst org-table-hline-regexp "^[ \t]*|-"
8156 "Detects an org-type table hline.")
8157 (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
8158 "Detects a table-type table hline.")
8159 (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
8160 "Detects an org-type or table-type table.")
8161 (defconst org-table-border-regexp "^[ \t]*[^| \t]"
8162 "Searching from within a table (any type) this finds the first line
8163 outside the table.")
8164 (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
8165 "Searching from within a table (any type) this finds the first line
8166 outside the table.")
8168 (defvar org-table-last-highlighted-reference nil)
8169 (defvar org-table-formula-history nil)
8171 (defvar org-table-column-names nil
8172 "Alist with column names, derived from the `!' line.")
8173 (defvar org-table-column-name-regexp nil
8174 "Regular expression matching the current column names.")
8175 (defvar org-table-local-parameters nil
8176 "Alist with parameter names, derived from the `$' line.")
8177 (defvar org-table-named-field-locations nil
8178 "Alist with locations of named fields.")
8180 (defvar org-table-current-line-types nil
8181 "Table row types, non-nil only for the duration of a comand.")
8182 (defvar org-table-current-begin-line nil
8183 "Table begin line, non-nil only for the duration of a comand.")
8184 (defvar org-table-current-begin-pos nil
8185 "Table begin position, non-nil only for the duration of a comand.")
8186 (defvar org-table-dlines nil
8187 "Vector of data line line numbers in the current table.")
8188 (defvar org-table-hlines nil
8189 "Vector of hline line numbers in the current table.")
8191 (defconst org-table-range-regexp
8192 "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
8194 "Regular expression for matching ranges in formulas.")
8196 (defconst org-table-range-regexp2
8198 "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
8200 "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
8201 "Match a range for reference display.")
8203 (defconst org-table-translate-regexp
8204 (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
8205 "Match a reference that needs translation, for reference display.")
8207 (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
8209 (defun org-table-create-with-table.el ()
8210 "Use the table.el package to insert a new table.
8211 If there is already a table at point, convert between Org-mode tables
8212 and table.el tables."
8216 ((org-at-table.el-p)
8217 (if (y-or-n-p "Convert table to Org-mode table? ")
8218 (org-table-convert)))
8220 (if (y-or-n-p "Convert table to table.el table? ")
8221 (org-table-convert)))
8222 (t (call-interactively 'table-insert))))
8224 (defun org-table-create-or-convert-from-region (arg)
8225 "Convert region to table, or create an empty table.
8226 If there is an active region, convert it to a table, using the function
8227 `org-table-convert-region'. See the documentation of that function
8228 to learn how the prefix argument is interpreted to determine the field
8230 If there is no such region, create an empty table with `org-table-create'."
8232 (if (org-region-active-p)
8233 (org-table-convert-region (region-beginning) (region-end) arg)
8234 (org-table-create arg)))
8236 (defun org-table-create (&optional size)
8237 "Query for a size and insert a table skeleton.
8238 SIZE is a string Columns x Rows like for example \"3x2\"."
8241 (setq size (read-string
8242 (concat "Table size Columns x Rows [e.g. "
8243 org-table-default-size "]: ")
8244 "" nil org-table-default-size)))
8246 (let* ((pos (point))
8247 (indent (make-string (current-column) ?\ ))
8248 (split (org-split-string size " *x *"))
8249 (rows (string-to-number (nth 1 split)))
8250 (columns (string-to-number (car split)))
8251 (line (concat (apply 'concat indent "|" (make-list columns " |"))
8253 (if (string-match "^[ \t]*$" (buffer-substring-no-properties
8254 (point-at-bol) (point)))
8255 (beginning-of-line 1)
8257 ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
8258 (dotimes (i rows) (insert line))
8261 ;; Insert a hline after the first row.
8268 (defun org-table-convert-region (beg0 end0 &optional separator)
8269 "Convert region to a table.
8270 The region goes from BEG0 to END0, but these borders will be moved
8271 slightly, to make sure a beginning of line in the first line is included.
8273 SEPARATOR specifies the field separator in the lines. It can have the
8276 '(4) Use the comma as a field separator
8277 '(16) Use a TAB as field separator
8278 integer When a number, use that many spaces as field separator
8279 nil When nil, the command tries to be smart and figure out the
8280 separator in the following way:
8281 - when each line contains a TAB, assume TAB-separated material
8282 - when each line contains a comme, assume CSV material
8283 - else, assume one or more SPACE charcters as separator."
8285 (let* ((beg (min beg0 end0))
8286 (end (max beg0 end0))
8289 (beginning-of-line 1)
8290 (setq beg (move-marker (make-marker) (point)))
8292 (if (bolp) (backward-char 1) (end-of-line 1))
8293 (setq end (move-marker (make-marker) (point)))
8294 ;; Get the right field separator
8299 ((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
8300 ((not (re-search-forward "^[^\n,]+$" end t)) '(4))
8303 ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
8304 ((equal separator '(16)) "^\\|\t")
8305 ((integerp separator)
8306 (format "^ *\\| *\t *\\| \\{%d,\\}" separator))
8307 (t (error "This should not happen"))))
8309 (while (re-search-forward re end t)
8310 (replace-match "| " t t))
8315 (defun org-table-import (file arg)
8316 "Import FILE as a table.
8317 The file is assumed to be tab-separated. Such files can be produced by most
8318 spreadsheet and database applications. If no tabs (at least one per line)
8319 are found, lines will be split on whitespace into fields."
8320 (interactive "f\nP")
8321 (or (bolp) (newline))
8324 (insert-file-contents file)
8325 (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
8327 (defun org-table-export ()
8328 "Export table as a tab-separated file.
8329 Such a file can be imported into a spreadsheet program like Excel."
8331 (let* ((beg (org-table-begin))
8332 (end (org-table-end))
8333 (table (buffer-substring beg end))
8334 (file (read-file-name "Export table to: "))
8336 (unless (or (not (file-exists-p file))
8337 (y-or-n-p (format "Overwrite file %s? " file)))
8339 (with-current-buffer (find-file-noselect file)
8340 (setq buf (current-buffer))
8344 (goto-char (point-min))
8345 (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
8346 (replace-match "" t t)
8348 (goto-char (point-min))
8349 (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
8350 (replace-match "" t t)
8351 (goto-char (min (1+ (point)) (point-max))))
8352 (goto-char (point-min))
8353 (while (re-search-forward "^-[-+]*$" nil t)
8355 (if (looking-at "\n")
8357 (goto-char (point-min))
8358 (while (re-search-forward "[ \t]*|[ \t]*" nil t)
8359 (replace-match "\t" t t))
8363 (defvar org-table-aligned-begin-marker (make-marker)
8364 "Marker at the beginning of the table last aligned.
8365 Used to check if cursor still is in that table, to minimize realignment.")
8366 (defvar org-table-aligned-end-marker (make-marker)
8367 "Marker at the end of the table last aligned.
8368 Used to check if cursor still is in that table, to minimize realignment.")
8369 (defvar org-table-last-alignment nil
8370 "List of flags for flushright alignment, from the last re-alignment.
8371 This is being used to correctly align a single field after TAB or RET.")
8372 (defvar org-table-last-column-widths nil
8373 "List of max width of fields in each column.
8374 This is being used to correctly align a single field after TAB or RET.")
8375 (defvar org-table-overlay-coordinates nil
8376 "Overlay coordinates after each align of a table.")
8377 (make-variable-buffer-local 'org-table-overlay-coordinates)
8379 (defvar org-last-recalc-line nil)
8380 (defconst org-narrow-column-arrow "=>"
8381 "Used as display property in narrowed table columns.")
8383 (defun org-table-align ()
8384 "Align the table at point by aligning all vertical bars."
8388 (beg (org-table-begin))
8389 (end (org-table-end))
8390 ;; Current cursor position
8391 (linepos (org-current-line))
8392 (colpos (org-table-current-column))
8393 (winstart (window-start))
8394 (winstartline (org-current-line (min winstart (1- (point-max)))))
8395 lines (new "") lengths l typenums ty fields maxfields i
8397 (indent "") cnt frac
8403 (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
8405 (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
8406 emptystrings links dates emph narrow fmax f1 len c e)
8408 (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
8409 ;; Check if we have links or dates
8411 (setq links (re-search-forward org-bracket-link-regexp end t))
8413 (setq emph (and org-hide-emphasis-markers
8414 (re-search-forward org-emph-re end t)))
8416 (setq dates (and org-display-custom-times
8417 (re-search-forward org-ts-regexp-both end t)))
8418 ;; Make sure the link properties are right
8419 (when links (goto-char beg) (while (org-activate-bracket-links end)))
8420 ;; Make sure the date properties are right
8421 (when dates (goto-char beg) (while (org-activate-dates end)))
8422 (when emph (goto-char beg) (while (org-do-emphasis-faces end)))
8424 ;; Check if we are narrowing any columns
8426 (setq narrow (and org-format-transports-properties-p
8427 (re-search-forward "<[0-9]+>" end t)))
8429 (setq lines (org-split-string
8430 (buffer-substring beg end) "\n"))
8431 ;; Store the indentation of the first line
8432 (if (string-match "^ *" (car lines))
8433 (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
8434 ;; Mark the hlines by setting the corresponding element to nil
8435 ;; At the same time, we remove trailing space.
8436 (setq lines (mapcar (lambda (l)
8437 (if (string-match "^ *|-" l)
8439 (if (string-match "[ \t]+$" l)
8440 (substring l 0 (match-beginning 0))
8443 ;; Get the data fields by splitting the lines.
8444 (setq fields (mapcar
8446 (org-split-string l " *| *"))
8447 (delq nil (copy-sequence lines))))
8448 ;; How many fields in the longest line?
8450 (setq maxfields (apply 'max (mapcar 'length fields)))
8452 (kill-region beg end)
8453 (org-table-create org-table-default-size)
8454 (error "Empty table - created default table")))
8455 ;; A list of empty strings to fill any short rows on output
8456 (setq emptystrings (make-list maxfields ""))
8457 ;; Check for special formatting.
8459 (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
8460 (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
8461 ;; Check if there is an explicit width specified
8463 (setq c column fmax nil)
8466 (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
8467 (setq fmax (string-to-number (match-string 1 e)) c nil)))
8468 ;; Find fields that are wider than fmax, and shorten them
8470 (loop for xx in column do
8471 (when (and (stringp xx)
8472 (> (org-string-width xx) fmax))
8473 (org-add-props xx nil
8475 (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
8476 (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
8478 (error "Cannot narrow field starting with wide link \"%s\""
8479 (match-string 0 xx)))
8480 (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
8481 (add-text-properties (- f1 2) f1
8482 (list 'display org-narrow-column-arrow)
8484 ;; Get the maximum width for each column
8485 (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
8486 ;; Get the fraction of numbers, to decide about alignment of the column
8487 (setq cnt 0 frac 0.0)
8488 (loop for x in column do
8491 (setq frac ( / (+ (* frac cnt)
8492 (if (string-match org-table-number-regexp x) 1 0))
8493 (setq cnt (1+ cnt))))))
8494 (push (>= frac org-table-number-fraction) typenums))
8495 (setq lengths (nreverse lengths) typenums (nreverse typenums))
8497 ;; Store the alignment of this table, for later editing of single fields
8498 (setq org-table-last-alignment typenums
8499 org-table-last-column-widths lengths)
8501 ;; With invisible characters, `format' does not get the field width right
8502 ;; So we need to make these fields wide by hand.
8503 (when (or links emph)
8504 (loop for i from 0 upto (1- maxfields) do
8505 (setq len (nth i lengths))
8506 (loop for j from 0 upto (1- (length fields)) do
8507 (setq c (nthcdr i (car (nthcdr j fields))))
8508 (if (and (stringp (car c))
8509 (text-property-any 0 (length (car c)) 'invisible 'org-link (car c))
8510 ; (string-match org-bracket-link-regexp (car c))
8511 (< (org-string-width (car c)) len))
8512 (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
8514 ;; Compute the formats needed for output of the table
8515 (setq rfmt (concat indent "|") hfmt (concat indent "|"))
8516 (while (setq l (pop lengths))
8517 (setq ty (if (pop typenums) "" "-")) ; number types flushright
8518 (setq rfmt (concat rfmt (format rfmt1 ty l))
8519 hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
8520 (setq rfmt (concat rfmt "\n")
8521 hfmt (concat (substring hfmt 0 -1) "|\n"))
8523 (setq new (mapconcat
8525 (if l (apply 'format rfmt
8526 (append (pop fields) emptystrings))
8529 ;; Replace the old one
8530 (delete-region beg end)
8531 (move-marker end nil)
8532 (move-marker org-table-aligned-begin-marker (point))
8534 (move-marker org-table-aligned-end-marker (point))
8535 (when (and orgtbl-mode (not (org-mode-p)))
8536 (goto-char org-table-aligned-begin-marker)
8537 (while (org-hide-wide-columns org-table-aligned-end-marker)))
8538 ;; Try to move to the old location
8539 (goto-line winstartline)
8540 (setq winstart (point-at-bol))
8542 (set-window-start (selected-window) winstart 'noforce)
8543 (org-table-goto-column colpos)
8544 (and org-table-overlay-coordinates (org-table-overlay-coordinates))
8545 (setq org-table-may-need-update nil)
8548 (defun org-string-width (s)
8549 "Compute width of string, ignoring invisible characters.
8550 This ignores character with invisibility property `org-link', and also
8551 characters with property `org-cwidth', because these will become invisible
8552 upon the next fontification round."
8554 (when (or (eq t buffer-invisibility-spec)
8555 (assq 'org-link buffer-invisibility-spec))
8556 (while (setq b (text-property-any 0 (length s)
8557 'invisible 'org-link s))
8558 (setq s (concat (substring s 0 b)
8559 (substring s (or (next-single-property-change
8560 b 'invisible s) (length s)))))))
8561 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
8562 (setq s (concat (substring s 0 b)
8563 (substring s (or (next-single-property-change
8564 b 'org-cwidth s) (length s))))))
8565 (setq l (string-width s) b -1)
8566 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
8567 (setq l (- l (get-text-property b 'org-dwidth-n s))))
8570 (defun org-table-begin (&optional table-type)
8571 "Find the beginning of the table and return its position.
8572 With argument TABLE-TYPE, go to the beginning of a table.el-type table."
8574 (if (not (re-search-backward
8575 (if table-type org-table-any-border-regexp
8576 org-table-border-regexp)
8578 (progn (goto-char (point-min)) (point))
8579 (goto-char (match-beginning 0))
8580 (beginning-of-line 2)
8583 (defun org-table-end (&optional table-type)
8584 "Find the end of the table and return its position.
8585 With argument TABLE-TYPE, go to the end of a table.el-type table."
8587 (if (not (re-search-forward
8588 (if table-type org-table-any-border-regexp
8589 org-table-border-regexp)
8591 (goto-char (point-max))
8592 (goto-char (match-beginning 0)))
8595 (defun org-table-justify-field-maybe (&optional new)
8596 "Justify the current field, text to left, number to right.
8597 Optional argument NEW may specify text to replace the current field content."
8599 ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
8600 ((org-at-table-hline-p))
8602 (or (not (equal (marker-buffer org-table-aligned-begin-marker)
8604 (< (point) org-table-aligned-begin-marker)
8605 (>= (point) org-table-aligned-end-marker)))
8606 ;; This is not the same table, force a full re-align
8607 (setq org-table-may-need-update t))
8608 (t ;; realign the current field, based on previous full realign
8609 (let* ((pos (point)) s
8610 (col (org-table-current-column))
8611 (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
8614 (skip-chars-backward "^|\n")
8615 (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
8617 (setq s (match-string 1)
8619 l (max 1 (- (match-end 0) (match-beginning 0) 3))
8620 e (not (= (match-beginning 2) (match-end 2))))
8621 (setq f (format (if num " %%%ds %s" " %%-%ds %s")
8622 l (if e "|" (setq org-table-may-need-update t) ""))
8625 (if (<= (length new) l) ;; FIXME: length -> str-width?
8626 (setq n (format f new))
8627 (setq n (concat new "|") org-table-may-need-update t)))
8629 (let (org-table-may-need-update)
8630 (replace-match n t t))))
8631 (setq org-table-may-need-update t))
8632 (goto-char pos))))))
8634 (defun org-table-next-field ()
8635 "Go to the next field in the current table, creating new lines as needed.
8636 Before doing so, re-align the table if necessary."
8638 (org-table-maybe-eval-formula)
8639 (org-table-maybe-recalculate-line)
8640 (if (and org-table-automatic-realign
8641 org-table-may-need-update)
8643 (let ((end (org-table-end)))
8644 (if (org-at-table-hline-p)
8648 (re-search-forward "|" end)
8649 (if (looking-at "[ \t]*$")
8650 (re-search-forward "|" end))
8651 (if (and (looking-at "-")
8652 org-table-tab-jumps-over-hlines
8653 (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
8654 (goto-char (match-beginning 1)))
8655 (if (looking-at "-")
8657 (beginning-of-line 0)
8658 (org-table-insert-row 'below))
8659 (if (looking-at " ") (forward-char 1))))
8661 (org-table-insert-row 'below)))))
8663 (defun org-table-previous-field ()
8664 "Go to the previous field in the table.
8665 Before doing so, re-align the table if necessary."
8667 (org-table-justify-field-maybe)
8668 (org-table-maybe-recalculate-line)
8669 (if (and org-table-automatic-realign
8670 org-table-may-need-update)
8672 (if (org-at-table-hline-p)
8674 (re-search-backward "|" (org-table-begin))
8675 (re-search-backward "|" (org-table-begin))
8676 (while (looking-at "|\\(-\\|[ \t]*$\\)")
8677 (re-search-backward "|" (org-table-begin)))
8678 (if (looking-at "| ?")
8679 (goto-char (match-end 0))))
8681 (defun org-table-next-row ()
8682 "Go to the next row (same column) in the current table.
8683 Before doing so, re-align the table if necessary."
8685 (org-table-maybe-eval-formula)
8686 (org-table-maybe-recalculate-line)
8687 (if (or (looking-at "[ \t]*$")
8688 (save-excursion (skip-chars-backward " \t") (bolp)))
8690 (if (and org-table-automatic-realign
8691 org-table-may-need-update)
8693 (let ((col (org-table-current-column)))
8694 (beginning-of-line 2)
8695 (if (or (not (org-at-table-p))
8696 (org-at-table-hline-p))
8698 (beginning-of-line 0)
8699 (org-table-insert-row 'below)))
8700 (org-table-goto-column col)
8701 (skip-chars-backward "^|\n\r")
8702 (if (looking-at " ") (forward-char 1)))))
8704 (defun org-table-copy-down (n)
8705 "Copy a field down in the current column.
8706 If the field at the cursor is empty, copy into it the content of the nearest
8707 non-empty field above. With argument N, use the Nth non-empty field.
8708 If the current field is not empty, it is copied down to the next row, and
8709 the cursor is moved with it. Therefore, repeating this command causes the
8710 column to be filled row-by-row.
8711 If the variable `org-table-copy-increment' is non-nil and the field is an
8712 integer or a timestamp, it will be incremented while copying. In the case of
8713 a timestamp, if the cursor is on the year, change the year. If it is on the
8714 month or the day, change that. Point will stay on the current date field
8715 in order to easily repeat the interval."
8717 (let* ((colpos (org-table-current-column))
8718 (col (current-column))
8719 (field (org-table-get-field))
8720 (non-empty (string-match "[^ \t]" field))
8721 (beg (org-table-begin))
8723 (org-table-check-inside-data-field)
8726 (setq txt (org-trim field))
8727 (org-table-next-row)
8728 (org-table-blank-field))
8732 (while (progn (beginning-of-line 1)
8733 (re-search-backward org-table-dataline-regexp
8735 (org-table-goto-column colpos t)
8736 (if (and (looking-at
8737 "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
8738 (= (setq n (1- n)) 0))
8739 (throw 'exit (match-string 1))))))))
8742 (if (and org-table-copy-increment
8743 (string-match "^[0-9]+$" txt))
8744 (setq txt (format "%d" (+ (string-to-number txt) 1))))
8746 (move-to-column col)
8747 (if (and org-table-copy-increment (org-at-timestamp-p t))
8748 (org-timestamp-up 1)
8749 (org-table-maybe-recalculate-line))
8751 (move-to-column col))
8752 (error "No non-empty field found"))))
8754 (defun org-table-check-inside-data-field ()
8755 "Is point inside a table data field?
8756 I.e. not on a hline or before the first or after the last column?
8757 This actually throws an error, so it aborts the current command."
8758 (if (or (not (org-at-table-p))
8759 (= (org-table-current-column) 0)
8760 (org-at-table-hline-p)
8761 (looking-at "[ \t]*$"))
8762 (error "Not in table data field")))
8764 (defvar org-table-clip nil
8765 "Clipboard for table regions.")
8767 (defun org-table-blank-field ()
8768 "Blank the current table field or active region."
8770 (org-table-check-inside-data-field)
8771 (if (and (interactive-p) (org-region-active-p))
8772 (let (org-table-clip)
8773 (org-table-cut-region (region-beginning) (region-end)))
8774 (skip-chars-backward "^|")
8776 (if (looking-at "|[^|\n]+")
8777 (let* ((pos (match-beginning 0))
8778 (match (match-string 0))
8779 (len (org-string-width match)))
8780 (replace-match (concat "|" (make-string (1- len) ?\ )))
8781 (goto-char (+ 2 pos))
8782 (substring match 1)))))
8784 (defun org-table-get-field (&optional n replace)
8785 "Return the value of the field in column N of current row.
8786 N defaults to current field.
8787 If REPLACE is a string, replace field with this value. The return value
8788 is always the old value."
8789 (and n (org-table-goto-column n))
8790 (skip-chars-backward "^|\n")
8792 (if (looking-at "|[^|\r\n]*")
8793 (let* ((pos (match-beginning 0))
8794 (val (buffer-substring (1+ pos) (match-end 0))))
8796 (replace-match (concat "|" replace) t t))
8797 (goto-char (min (point-at-eol) (+ 2 pos)))
8799 (forward-char 1) ""))
8801 (defun org-table-field-info (arg)
8802 "Show info about the current field, and highlight any reference at point."
8804 (org-table-get-specials)
8806 (let* ((pos (point))
8807 (col (org-table-current-column))
8808 (cname (car (rassoc (int-to-string col) org-table-column-names)))
8809 (name (car (rassoc (list (org-current-line) col)
8810 org-table-named-field-locations)))
8811 (eql (org-table-get-stored-formulas))
8812 (dline (org-table-current-dline))
8813 (ref (format "@%d$%d" dline col))
8814 (ref1 (org-table-convert-refs-to-an ref))
8815 (fequation (or (assoc name eql) (assoc ref eql)))
8816 (cequation (assoc (int-to-string col) eql))
8817 (eqn (or fequation cequation)))
8820 (org-table-show-reference 'local)
8822 (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
8824 (if cname (concat " or $" cname) "")
8826 (if name (concat " or $" name) "")
8827 ;; FIXME: formula info not correct if special table line
8829 (concat ", formula: "
8830 (org-table-formula-to-user
8832 (if (string-match "^[$@]"(car eqn)) "" "$")
8833 (car eqn) "=" (cdr eqn))))
8836 (defun org-table-current-column ()
8837 "Find out which column we are in.
8838 When called interactively, column is also displayed in echo area."
8840 (if (interactive-p) (org-table-check-inside-data-field))
8842 (let ((cnt 0) (pos (point)))
8843 (beginning-of-line 1)
8844 (while (search-forward "|" pos t)
8845 (setq cnt (1+ cnt)))
8846 (if (interactive-p) (message "This is table column %d" cnt))
8849 (defun org-table-current-dline ()
8850 "Find out what table data line we are in.
8851 Only datalins count for this."
8853 (if (interactive-p) (org-table-check-inside-data-field))
8855 (let ((cnt 0) (pos (point)))
8856 (goto-char (org-table-begin))
8857 (while (<= (point) pos)
8858 (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
8859 (beginning-of-line 2))
8860 (if (interactive-p) (message "This is table line %d" cnt))
8863 (defun org-table-goto-column (n &optional on-delim force)
8864 "Move the cursor to the Nth column in the current table line.
8865 With optional argument ON-DELIM, stop with point before the left delimiter
8867 If there are less than N fields, just go to after the last delimiter.
8868 However, when FORCE is non-nil, create new columns if necessary."
8870 (let ((pos (point-at-eol)))
8871 (beginning-of-line 1)
8873 (while (and (> (setq n (1- n)) -1)
8874 (or (search-forward "|" pos t)
8876 (progn (end-of-line 1)
8877 (skip-chars-backward "^|")
8879 ; (backward-char 2) t)))))
8880 (when (and force (not (looking-at ".*|")))
8881 (save-excursion (end-of-line 1) (insert " | ")))
8884 (if (looking-at " ") (forward-char 1))))))
8886 (defun org-at-table-p (&optional table-type)
8887 "Return t if the cursor is inside an org-type table.
8888 If TABLE-TYPE is non-nil, also check for table.el-type tables."
8889 (if org-enable-table-editor
8891 (beginning-of-line 1)
8892 (looking-at (if table-type org-table-any-line-regexp
8893 org-table-line-regexp)))
8896 (defun org-at-table.el-p ()
8897 "Return t if and only if we are at a table.el table."
8898 (and (org-at-table-p 'any)
8900 (goto-char (org-table-begin 'any))
8901 (looking-at org-table1-hline-regexp))))
8903 (defun org-table-recognize-table.el ()
8904 "If there is a table.el table nearby, recognize it and move into it."
8905 (if org-table-tab-recognizes-table.el
8906 (if (org-at-table.el-p)
8908 (beginning-of-line 1)
8909 (if (looking-at org-table-dataline-regexp)
8911 (if (looking-at org-table1-hline-regexp)
8913 (beginning-of-line 2)
8914 (if (looking-at org-table-any-border-regexp)
8915 (beginning-of-line -1)))))
8916 (if (re-search-forward "|" (org-table-end t) t)
8919 (if (table--at-cell-p (point))
8921 (message "recognizing table.el table...")
8922 (table-recognize-table)
8923 (message "recognizing table.el table...done")))
8924 (error "This should not happen..."))
8929 (defun org-at-table-hline-p ()
8930 "Return t if the cursor is inside a hline in a table."
8931 (if org-enable-table-editor
8933 (beginning-of-line 1)
8934 (looking-at org-table-hline-regexp))
8937 (defun org-table-insert-column ()
8938 "Insert a new column into the table."
8940 (if (not (org-at-table-p))
8941 (error "Not at a table"))
8942 (org-table-find-dataline)
8943 (let* ((col (max 1 (org-table-current-column)))
8944 (beg (org-table-begin))
8945 (end (org-table-end))
8946 ;; Current cursor position
8947 (linepos (org-current-line))
8950 (while (< (point) end)
8951 (if (org-at-table-hline-p)
8953 (org-table-goto-column col t)
8955 (beginning-of-line 2))
8956 (move-marker end nil)
8958 (org-table-goto-column colpos)
8960 (org-table-fix-formulas "$" nil (1- col) 1)))
8962 (defun org-table-find-dataline ()
8963 "Find a dataline in the current table, which is needed for column commands."
8964 (if (and (org-at-table-p)
8965 (not (org-at-table-hline-p)))
8967 (let ((col (current-column))
8968 (end (org-table-end)))
8969 (move-to-column col)
8970 (while (and (< (point) end)
8971 (or (not (= (current-column) col))
8972 (org-at-table-hline-p)))
8973 (beginning-of-line 2)
8974 (move-to-column col))
8975 (if (and (org-at-table-p)
8976 (not (org-at-table-hline-p)))
8979 "Please position cursor in a data line for column operations")))))
8981 (defun org-table-delete-column ()
8982 "Delete a column from the table."
8984 (if (not (org-at-table-p))
8985 (error "Not at a table"))
8986 (org-table-find-dataline)
8987 (org-table-check-inside-data-field)
8988 (let* ((col (org-table-current-column))
8989 (beg (org-table-begin))
8990 (end (org-table-end))
8991 ;; Current cursor position
8992 (linepos (org-current-line))
8995 (while (< (point) end)
8996 (if (org-at-table-hline-p)
8998 (org-table-goto-column col t)
8999 (and (looking-at "|[^|\n]+|")
9000 (replace-match "|")))
9001 (beginning-of-line 2))
9002 (move-marker end nil)
9004 (org-table-goto-column colpos)
9006 (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
9009 (defun org-table-move-column-right ()
9010 "Move column to the right."
9012 (org-table-move-column nil))
9013 (defun org-table-move-column-left ()
9014 "Move column to the left."
9016 (org-table-move-column 'left))
9018 (defun org-table-move-column (&optional left)
9019 "Move the current column to the right. With arg LEFT, move to the left."
9021 (if (not (org-at-table-p))
9022 (error "Not at a table"))
9023 (org-table-find-dataline)
9024 (org-table-check-inside-data-field)
9025 (let* ((col (org-table-current-column))
9026 (col1 (if left (1- col) col))
9027 (beg (org-table-begin))
9028 (end (org-table-end))
9029 ;; Current cursor position
9030 (linepos (org-current-line))
9031 (colpos (if left (1- col) (1+ col))))
9032 (if (and left (= col 1))
9033 (error "Cannot move column further left"))
9034 (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
9035 (error "Cannot move column further right"))
9037 (while (< (point) end)
9038 (if (org-at-table-hline-p)
9040 (org-table-goto-column col1 t)
9041 (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
9042 (replace-match "|\\2|\\1|")))
9043 (beginning-of-line 2))
9044 (move-marker end nil)
9046 (org-table-goto-column colpos)
9048 (org-table-fix-formulas
9049 "$" (list (cons (number-to-string col) (number-to-string colpos))
9050 (cons (number-to-string colpos) (number-to-string col))))))
9052 (defun org-table-move-row-down ()
9053 "Move table row down."
9055 (org-table-move-row nil))
9056 (defun org-table-move-row-up ()
9057 "Move table row up."
9059 (org-table-move-row 'up))
9061 (defun org-table-move-row (&optional up)
9062 "Move the current table line down. With arg UP, move it up."
9064 (let* ((col (current-column))
9066 (hline1p (save-excursion (beginning-of-line 1)
9067 (looking-at org-table-hline-regexp)))
9068 (dline1 (org-table-current-dline))
9069 (dline2 (+ dline1 (if up -1 1)))
9072 (beginning-of-line tonew)
9073 (unless (org-at-table-p)
9075 (error "Cannot move row further"))
9076 (setq hline2p (looking-at org-table-hline-regexp))
9078 (beginning-of-line 1)
9080 (setq txt (buffer-substring (point) (1+ (point-at-eol))))
9081 (delete-region (point) (1+ (point-at-eol)))
9082 (beginning-of-line tonew)
9084 (beginning-of-line 0)
9085 (move-to-column col)
9086 (unless (or hline1p hline2p)
9087 (org-table-fix-formulas
9088 "@" (list (cons (number-to-string dline1) (number-to-string dline2))
9089 (cons (number-to-string dline2) (number-to-string dline1)))))))
9091 (defun org-table-insert-row (&optional arg)
9092 "Insert a new row above the current line into the table.
9093 With prefix ARG, insert below the current line."
9095 (if (not (org-at-table-p))
9096 (error "Not at a table"))
9097 (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
9098 (new (org-table-clean-line line)))
9099 ;; Fix the first field if necessary
9100 (if (string-match "^[ \t]*| *[#$] *|" line)
9101 (setq new (replace-match (match-string 0 line) t t new)))
9102 (beginning-of-line (if arg 2 1))
9103 (let (org-table-may-need-update) (insert-before-markers new "\n"))
9104 (beginning-of-line 0)
9105 (re-search-forward "| ?" (point-at-eol) t)
9106 (and (or org-table-may-need-update org-table-overlay-coordinates)
9108 (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
9110 (defun org-table-insert-hline (&optional above)
9111 "Insert a horizontal-line below the current line into the table.
9112 With prefix ABOVE, insert above the current line."
9114 (if (not (org-at-table-p))
9115 (error "Not at a table"))
9116 (let ((line (org-table-clean-line
9117 (buffer-substring (point-at-bol) (point-at-eol))))
9118 (col (current-column)))
9119 (while (string-match "|\\( +\\)|" line)
9120 (setq line (replace-match
9121 (concat "+" (make-string (- (match-end 1) (match-beginning 1))
9122 ?-) "|") t t line)))
9123 (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
9124 (beginning-of-line (if above 1 2))
9126 (beginning-of-line (if above 1 -1))
9127 (move-to-column col)
9128 (and org-table-overlay-coordinates (org-table-align))))
9130 (defun org-table-hline-and-move (&optional same-column)
9131 "Insert a hline and move to the row below that line."
9133 (let ((col (org-table-current-column)))
9134 (org-table-maybe-eval-formula)
9135 (org-table-maybe-recalculate-line)
9136 (org-table-insert-hline)
9138 (if (looking-at "\n[ \t]*|-")
9139 (progn (insert "\n|") (org-table-align))
9140 (org-table-next-field))
9141 (if same-column (org-table-goto-column col))))
9143 (defun org-table-clean-line (s)
9144 "Convert a table line S into a string with only \"|\" and space.
9145 In particular, this does handle wide and invisible characters."
9146 (if (string-match "^[ \t]*|-" s)
9147 ;; It's a hline, just map the characters
9148 (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
9149 (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
9150 (setq s (replace-match
9151 (concat "|" (make-string (org-string-width (match-string 1 s))
9156 (defun org-table-kill-row ()
9157 "Delete the current row or horizontal line from the table."
9159 (if (not (org-at-table-p))
9160 (error "Not at a table"))
9161 (let ((col (current-column))
9162 (dline (org-table-current-dline)))
9163 (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
9164 (if (not (org-at-table-p)) (beginning-of-line 0))
9165 (move-to-column col)
9166 (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
9169 (defun org-table-sort-lines (with-case &optional sorting-type)
9170 "Sort table lines according to the column at point.
9172 The position of point indicates the column to be used for
9173 sorting, and the range of lines is the range between the nearest
9174 horizontal separator lines, or the entire table of no such lines
9175 exist. If point is before the first column, you will be prompted
9176 for the sorting column. If there is an active region, the mark
9177 specifies the first line and the sorting column, while point
9178 should be in the last line to be included into the sorting.
9180 The command then prompts for the sorting type which can be
9181 alphabetically, numerically, or by time (as given in a time stamp
9182 in the field). Sorting in reverse order is also possible.
9184 With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
9186 If SORTING-TYPE is specified when this function is called from a Lisp
9187 program, no prompting will take place. SORTING-TYPE must be a character,
9188 any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
9189 should be done in reverse order."
9191 (let* ((thisline (org-current-line))
9192 (thiscol (org-table-current-column))
9193 beg end bcol ecol tend tbeg column lns pos)
9194 (when (equal thiscol 0)
9198 (read-string "Use column N for sorting: ")))
9200 (org-table-goto-column thiscol))
9201 (org-table-check-inside-data-field)
9202 (if (org-region-active-p)
9204 (setq beg (region-beginning) end (region-end))
9206 (setq column (org-table-current-column)
9209 (setq end (point-at-bol 2)))
9210 (setq column (org-table-current-column)
9212 tbeg (org-table-begin)
9213 tend (org-table-end))
9214 (if (re-search-backward org-table-hline-regexp tbeg t)
9215 (setq beg (point-at-bol 2))
9217 (setq beg (point-at-bol 1)))
9219 (if (re-search-forward org-table-hline-regexp tend t)
9220 (setq end (point-at-bol 1))
9222 (setq end (point-at-bol))))
9223 (setq beg (move-marker (make-marker) beg)
9224 end (move-marker (make-marker) end))
9227 (org-table-goto-column column)
9228 (skip-chars-backward "^|")
9229 (setq bcol (current-column))
9230 (org-table-goto-column (1+ column))
9231 (skip-chars-backward "^|")
9232 (setq ecol (1- (current-column)))
9233 (org-table-goto-column column)
9234 (setq lns (mapcar (lambda(x) (cons
9235 (org-sort-remove-invisible
9237 (org-split-string x "[ \t]*|[ \t]*")))
9239 (org-split-string (buffer-substring beg end) "\n")))
9240 (setq lns (org-do-sort lns "Table" with-case sorting-type))
9241 (delete-region beg end)
9242 (move-marker beg nil)
9243 (move-marker end nil)
9244 (insert (mapconcat 'cdr lns "\n") "\n")
9245 (goto-line thisline)
9246 (org-table-goto-column thiscol)
9247 (message "%d lines sorted, based on column %d" (length lns) column)))
9249 ;; FIXME: maybe we will not need this? Table sorting is broken....
9250 (defun org-sort-remove-invisible (s)
9251 (remove-text-properties 0 (length s) org-rm-props s)
9252 (while (string-match org-bracket-link-regexp s)
9253 (setq s (replace-match (if (match-end 2)
9255 (match-string 1 s)) t t s)))
9258 (defun org-table-cut-region (beg end)
9259 "Copy region in table to the clipboard and blank all relevant fields."
9261 (org-table-copy-region beg end 'cut))
9263 (defun org-table-copy-region (beg end &optional cut)
9264 "Copy rectangular region in table to clipboard.
9265 A special clipboard is used which can only be accessed
9266 with `org-table-paste-rectangle'."
9268 (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
9270 (rpl (if cut " " nil)))
9272 (org-table-check-inside-data-field)
9273 (setq l01 (org-current-line)
9274 c01 (org-table-current-column))
9276 (org-table-check-inside-data-field)
9277 (setq l02 (org-current-line)
9278 c02 (org-table-current-column))
9279 (setq l1 (min l01 l02) l2 (max l01 l02)
9280 c1 (min c01 c02) c2 (max c01 c02))
9284 (if (> l1 l2) (throw 'exit t))
9286 (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
9287 (setq cols nil ic1 c1 ic2 c2)
9288 (while (< ic1 (1+ ic2))
9289 (push (org-table-get-field ic1 rpl) cols)
9290 (setq ic1 (1+ ic1)))
9291 (push (nreverse cols) region)
9292 (setq l1 (1+ l1)))))
9293 (setq org-table-clip (nreverse region))
9294 (if cut (org-table-align))
9297 (defun org-table-paste-rectangle ()
9298 "Paste a rectangular region into a table.
9299 The upper right corner ends up in the current field. All involved fields
9300 will be overwritten. If the rectangle does not fit into the present table,
9301 the table is enlarged as needed. The process ignores horizontal separator
9304 (unless (and org-table-clip (listp org-table-clip))
9305 (error "First cut/copy a region to paste!"))
9306 (org-table-check-inside-data-field)
9307 (let* ((clip org-table-clip)
9308 (line (org-current-line))
9309 (col (org-table-current-column))
9310 (org-enable-table-editor t)
9311 (org-table-automatic-realign nil)
9313 (while (setq cols (pop clip))
9314 (while (org-at-table-hline-p) (beginning-of-line 2))
9315 (if (not (org-at-table-p))
9316 (progn (end-of-line 0) (org-table-next-field)))
9318 (while (setq field (pop cols))
9319 (org-table-goto-column c nil 'force)
9320 (org-table-get-field nil field)
9322 (beginning-of-line 2))
9324 (org-table-goto-column col)
9327 (defun org-table-convert ()
9328 "Convert from `org-mode' table to table.el and back.
9329 Obviously, this only works within limits. When an Org-mode table is
9330 converted to table.el, all horizontal separator lines get lost, because
9331 table.el uses these as cell boundaries and has no notion of horizontal lines.
9332 A table.el table can be converted to an Org-mode table only if it does not
9333 do row or column spanning. Multiline cells will become multiple cells.
9334 Beware, Org-mode does not test if the table can be successfully converted - it
9335 blindly applies a recipe that works for simple tables."
9338 (if (org-at-table.el-p)
9339 ;; convert to Org-mode table
9340 (let ((beg (move-marker (make-marker) (org-table-begin t)))
9341 (end (move-marker (make-marker) (org-table-end t))))
9342 (table-unrecognize-region beg end)
9344 (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
9347 (if (org-at-table-p)
9348 ;; convert to table.el table
9349 (let ((beg (move-marker (make-marker) (org-table-begin)))
9350 (end (move-marker (make-marker) (org-table-end))))
9351 ;; first, get rid of all horizontal lines
9353 (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
9355 ;; insert a hline before first
9357 (org-table-insert-hline 'above)
9358 (beginning-of-line -1)
9359 ;; insert a hline after each line
9360 (while (progn (beginning-of-line 3) (< (point) end))
9361 (org-table-insert-hline))
9363 (setq end (move-marker end (org-table-end)))
9364 ;; replace "+" at beginning and ending of hlines
9365 (while (re-search-forward "^\\([ \t]*\\)|-" end t)
9366 (replace-match "\\1+-"))
9368 (while (re-search-forward "-|[ \t]*$" end t)
9369 (replace-match "-+"))
9372 (defun org-table-wrap-region (arg)
9373 "Wrap several fields in a column like a paragraph.
9374 This is useful if you'd like to spread the contents of a field over several
9375 lines, in order to keep the table compact.
9377 If there is an active region, and both point and mark are in the same column,
9378 the text in the column is wrapped to minimum width for the given number of
9379 lines. Generally, this makes the table more compact. A prefix ARG may be
9380 used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
9381 formats the selected text to two lines. If the region was longer than two
9382 lines, the remaining lines remain empty. A negative prefix argument reduces
9383 the current number of lines by that amount. The wrapped text is pasted back
9384 into the table. If you formatted it to more lines than it was before, fields
9385 further down in the table get overwritten - so you might need to make space in
9388 If there is no region, the current field is split at the cursor position and
9389 the text fragment to the right of the cursor is prepended to the field one
9392 If there is no region, but you specify a prefix ARG, the current field gets
9393 blank, and the content is appended to the field above."
9395 (org-table-check-inside-data-field)
9396 (if (org-region-active-p)
9397 ;; There is a region: fill as a paragraph
9398 (let* ((beg (region-beginning))
9399 (cline (save-excursion (goto-char beg) (org-current-line)))
9400 (ccol (save-excursion (goto-char beg) (org-table-current-column)))
9402 (org-table-cut-region (region-beginning) (region-end))
9403 (if (> (length (car org-table-clip)) 1)
9404 (error "Region must be limited to single column"))
9405 (setq nlines (if arg
9407 (+ (length org-table-clip) arg)
9409 (length org-table-clip)))
9410 (setq org-table-clip
9411 (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
9414 (org-table-goto-column ccol)
9415 (org-table-paste-rectangle))
9416 ;; No region, split the current field at point
9418 ;; combine with field above
9419 (let ((s (org-table-blank-field))
9420 (col (org-table-current-column)))
9421 (beginning-of-line 0)
9422 (while (org-at-table-hline-p) (beginning-of-line 0))
9423 (org-table-goto-column col)
9424 (skip-chars-forward "^|")
9425 (skip-chars-backward " ")
9426 (insert " " (org-trim s))
9429 (when (looking-at "\\([^|]+\\)+|")
9430 (let ((s (match-string 1)))
9431 (replace-match " |")
9432 (goto-char (match-beginning 0))
9433 (org-table-next-row)
9434 (insert (org-trim s) " ")
9435 (org-table-align))))))
9437 (defvar org-field-marker nil)
9439 (defun org-table-edit-field (arg)
9440 "Edit table field in a different window.
9441 This is mainly useful for fields that contain hidden parts.
9442 When called with a \\[universal-argument] prefix, just make the full field visible so that
9443 it can be edited in place."
9446 (let ((b (save-excursion (skip-chars-backward "^|") (point)))
9447 (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
9448 (remove-text-properties b e '(org-cwidth t invisible t
9449 display t intangible t))
9450 (if (and (boundp 'font-lock-mode) font-lock-mode)
9451 (font-lock-fontify-block)))
9452 (let ((pos (move-marker (make-marker) (point)))
9453 (field (org-table-get-field))
9454 (cw (current-window-configuration))
9456 (org-switch-to-buffer-other-window "*Org tmp*")
9458 (insert "#\n# Edit field and finish with C-c C-c\n#\n")
9459 (let ((org-inhibit-startup t)) (org-mode))
9460 (goto-char (setq p (point-max)))
9461 (insert (org-trim field))
9462 (remove-text-properties p (point-max)
9463 '(invisible t org-cwidth t display t
9466 (org-set-local 'org-finish-function 'org-table-finish-edit-field)
9467 (org-set-local 'org-window-configuration cw)
9468 (org-set-local 'org-field-marker pos)
9469 (message "Edit and finish with C-c C-c"))))
9471 (defun org-table-finish-edit-field ()
9472 "Finish editing a table data field.
9473 Remove all newline characters, insert the result into the table, realign
9474 the table and kill the editing buffer."
9475 (let ((pos org-field-marker)
9476 (cw org-window-configuration)
9477 (cb (current-buffer))
9479 (goto-char (point-min))
9480 (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
9481 (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
9482 (replace-match " "))
9483 (setq text (org-trim (buffer-string)))
9484 (set-window-configuration cw)
9486 (select-window (get-buffer-window (marker-buffer pos)))
9488 (move-marker pos nil)
9489 (org-table-check-inside-data-field)
9490 (org-table-get-field nil text)
9492 (message "New field value inserted")))
9495 "Remove whitespace at beginning and end of string."
9496 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
9497 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
9500 (defun org-wrap (string &optional width lines)
9501 "Wrap string to either a number of lines, or a width in characters.
9502 If WIDTH is non-nil, the string is wrapped to that width, however many lines
9503 that costs. If there is a word longer than WIDTH, the text is actually
9504 wrapped to the length of that word.
9505 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
9506 many lines, whatever width that takes.
9507 The return value is a list of lines, without newlines at the end."
9508 (let* ((words (org-split-string string "[ \t\n]+"))
9509 (maxword (apply 'max (mapcar 'org-string-width words)))
9512 (org-do-wrap words (max maxword width)))
9515 (setq ll (org-do-wrap words maxword))
9516 (if (<= (length ll) lines)
9519 (while (> (length ll) lines)
9521 (setq ll (org-do-wrap words w)))
9523 (t (error "Cannot wrap this")))))
9526 (defun org-do-wrap (words width)
9527 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
9530 (setq line (pop words))
9531 (while (and words (< (+ (length line) (length (car words))) width))
9532 (setq line (concat line " " (pop words))))
9533 (setq lines (push line lines)))
9536 (defun org-split-string (string &optional separators)
9537 "Splits STRING into substrings at SEPARATORS.
9538 No empty strings are returned if there are matches at the beginning
9540 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
9544 (while (and (string-match rexp string
9546 (= start (match-beginning 0))
9547 (< start (length string)))
9549 (< (match-beginning 0) (length string)))
9551 (or (eq (match-beginning 0) 0)
9552 (and (eq (match-beginning 0) (match-end 0))
9553 (eq (match-beginning 0) start))
9555 (cons (substring string start (match-beginning 0))
9557 (setq start (match-end 0)))
9558 (or (eq start (length string))
9560 (cons (substring string start)
9564 (defun org-table-map-tables (function)
9565 "Apply FUNCTION to the start of all tables in the buffer."
9569 (goto-char (point-min))
9570 (while (re-search-forward org-table-any-line-regexp nil t)
9571 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
9572 (beginning-of-line 1)
9573 (if (looking-at org-table-line-regexp)
9574 (save-excursion (funcall function)))
9575 (re-search-forward org-table-any-border-regexp nil 1))))
9576 (message "Mapping tables: done"))
9578 (defvar org-timecnt) ; dynamically scoped parameter
9580 (defun org-table-sum (&optional beg end nlast)
9581 "Sum numbers in region of current table column.
9582 The result will be displayed in the echo area, and will be available
9583 as kill to be inserted with \\[yank].
9585 If there is an active region, it is interpreted as a rectangle and all
9586 numbers in that rectangle will be summed. If there is no active
9587 region and point is located in a table column, sum all numbers in that
9590 If at least one number looks like a time HH:MM or HH:MM:SS, all other
9591 numbers are assumed to be times as well (in decimal hours) and the
9592 numbers are added as such.
9594 If NLAST is a number, only the NLAST fields will actually be summed."
9597 (let (col (org-timecnt 0) diff h m s org-table-clip)
9599 ((and beg end)) ; beg and end given explicitly
9600 ((org-region-active-p)
9601 (setq beg (region-beginning) end (region-end)))
9603 (setq col (org-table-current-column))
9604 (goto-char (org-table-begin))
9605 (unless (re-search-forward "^[ \t]*|[^-]" nil t)
9606 (error "No table data"))
9607 (org-table-goto-column col)
9609 (goto-char (org-table-end))
9610 (unless (re-search-backward "^[ \t]*|[^-]" nil t)
9611 (error "No table data"))
9612 (org-table-goto-column col)
9613 (setq end (point))))
9614 (let* ((items (apply 'append (org-table-copy-region beg end)))
9615 (items1 (cond ((not nlast) items)
9616 ((>= nlast (length items)) items)
9617 (t (setq items (reverse items))
9618 (setcdr (nthcdr (1- nlast) items) nil)
9620 (numbers (delq nil (mapcar 'org-table-get-number-for-summing
9622 (res (apply '+ numbers))
9623 (sres (if (= org-timecnt 0)
9625 (setq diff (* 3600 res)
9626 h (floor (/ diff 3600)) diff (mod diff 3600)
9627 m (floor (/ diff 60)) diff (mod diff 60)
9629 (format "%d:%02d:%02d" h m s))))
9633 (substitute-command-keys
9634 (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
9635 (length numbers) sres))))
9638 (defun org-table-get-number-for-summing (s)
9640 (if (string-match "^ *|? *" s)
9641 (setq s (replace-match "" nil nil s)))
9642 (if (string-match " *|? *$" s)
9643 (setq s (replace-match "" nil nil s)))
9644 (setq n (string-to-number s))
9646 ((and (string-match "0" s)
9647 (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
9648 ((string-match "\\`[ \t]+\\'" s) nil)
9649 ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
9650 (let ((h (string-to-number (or (match-string 1 s) "0")))
9651 (m (string-to-number (or (match-string 2 s) "0")))
9652 (s (string-to-number (or (match-string 4 s) "0"))))
9653 (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
9654 (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
9658 (defun org-table-current-field-formula (&optional key noerror)
9659 "Return the formula active for the current field.
9660 Assumes that specials are in place.
9661 If KEY is given, return the key to this formula.
9662 Otherwise return the formula preceeded with \"=\" or \":=\"."
9663 (let* ((name (car (rassoc (list (org-current-line)
9664 (org-table-current-column))
9665 org-table-named-field-locations)))
9666 (col (org-table-current-column))
9667 (scol (int-to-string col))
9668 (ref (format "@%d$%d" (org-table-current-dline) col))
9669 (stored-list (org-table-get-stored-formulas noerror))
9670 (ass (or (assoc name stored-list)
9671 (assoc ref stored-list)
9672 (assoc scol stored-list))))
9675 (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
9678 (defun org-table-get-formula (&optional equation named)
9679 "Read a formula from the minibuffer, offer stored formula as default.
9680 When NAMED is non-nil, look for a named equation."
9681 (let* ((stored-list (org-table-get-stored-formulas))
9682 (name (car (rassoc (list (org-current-line)
9683 (org-table-current-column))
9684 org-table-named-field-locations)))
9685 (ref (format "@%d$%d" (org-table-current-dline)
9686 (org-table-current-column)))
9687 (refass (assoc ref stored-list))
9690 (int-to-string (org-table-current-column))))
9691 (dummy (and (or name refass) (not named)
9692 (not (y-or-n-p "Replace field formula with column formula? " ))
9694 (name (or name ref))
9695 (org-table-may-need-update nil)
9696 (stored (cdr (assoc scol stored-list)))
9698 ((and stored equation (string-match "^ *=? *$" equation))
9702 (t (org-table-formula-from-user
9704 (org-table-formula-to-user
9705 (format "%s formula %s%s="
9706 (if named "Field" "Column")
9707 (if (member (string-to-char scol) '(?$ ?@)) "" "$")
9709 (if stored (org-table-formula-to-user stored) "")
9710 'org-table-formula-history
9713 (when (not (string-match "\\S-" eq))
9715 (setq stored-list (delq (assoc scol stored-list) stored-list))
9716 (org-table-store-formulas stored-list)
9717 (error "Formula removed"))
9718 (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
9719 (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
9720 (if (and name (not named))
9721 ;; We set the column equation, delete the named one.
9722 (setq stored-list (delq (assoc name stored-list) stored-list)
9725 (setcdr (assoc scol stored-list) eq)
9726 (setq stored-list (cons (cons scol eq) stored-list)))
9727 (if (or mustsave (not (equal stored eq)))
9728 (org-table-store-formulas stored-list))
9731 (defun org-table-store-formulas (alist)
9732 "Store the list of formulas below the current table."
9733 (setq alist (sort alist 'org-table-formula-less-p))
9735 (goto-char (org-table-end))
9736 (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
9738 ;; don't overwrite TBLFM, we might use text properties to store stuff
9739 (goto-char (match-beginning 2))
9740 (delete-region (match-beginning 2) (match-end 0)))
9741 (insert "#+TBLFM:"))
9743 (mapconcat (lambda (x)
9745 (if (equal (string-to-char (car x)) ?@) "" "$")
9746 (car x) "=" (cdr x)))
9750 (defsubst org-table-formula-make-cmp-string (a)
9751 (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
9753 (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
9754 (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
9755 (if (match-end 5) (concat "@@" (match-string 5 a))))))
9757 (defun org-table-formula-less-p (a b)
9758 "Compare two formulas for sorting."
9759 (let ((as (org-table-formula-make-cmp-string (car a)))
9760 (bs (org-table-formula-make-cmp-string (car b))))
9761 (and as bs (string< as bs))))
9763 (defun org-table-get-stored-formulas (&optional noerror)
9764 "Return an alist with the stored formulas directly after current table."
9766 (let (scol eq eq-alist strings string seen)
9768 (goto-char (org-table-end))
9769 (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
9770 (setq strings (org-split-string (match-string 2) " *:: *"))
9771 (while (setq string (pop strings))
9772 (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
9773 (setq scol (if (match-end 2)
9774 (match-string 2 string)
9775 (match-string 1 string))
9776 eq (match-string 3 string)
9777 eq-alist (cons (cons scol eq) eq-alist))
9778 (if (member scol seen)
9781 (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
9784 (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
9785 (push scol seen))))))
9786 (nreverse eq-alist)))
9788 (defun org-table-fix-formulas (key replace &optional limit delta remove)
9789 "Modify the equations after the table structure has been edited.
9790 KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
9791 For all numbers larger than LIMIT, shift them by DELTA."
9793 (goto-char (org-table-end))
9794 (when (looking-at "#\\+TBLFM:")
9795 (let ((re (concat key "\\([0-9]+\\)"))
9799 (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
9800 (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
9803 (while (re-search-forward re2 (point-at-eol) t)
9804 (replace-match "")))
9805 (while (re-search-forward re (point-at-eol) t)
9806 (setq s (match-string 1) n (string-to-number s))
9808 ((setq a (assoc s replace))
9809 (replace-match (concat key (cdr a)) t t))
9810 ((and limit (> n limit))
9811 (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
9813 (defun org-table-get-specials ()
9814 "Get the column names and local parameters for this table."
9816 (let ((beg (org-table-begin)) (end (org-table-end))
9817 names name fields fields1 field cnt
9818 c v l line col types dlines hlines)
9819 (setq org-table-column-names nil
9820 org-table-local-parameters nil
9821 org-table-named-field-locations nil
9822 org-table-current-begin-line nil
9823 org-table-current-begin-pos nil
9824 org-table-current-line-types nil)
9826 (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
9827 (setq names (org-split-string (match-string 1) " *| *")
9829 (while (setq name (pop names))
9831 (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
9832 (push (cons name (int-to-string cnt)) org-table-column-names))))
9833 (setq org-table-column-names (nreverse org-table-column-names))
9834 (setq org-table-column-name-regexp
9835 (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
9837 (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
9838 (setq fields (org-split-string (match-string 1) " *| *"))
9839 (while (setq field (pop fields))
9840 (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
9841 (push (cons (match-string 1 field) (match-string 2 field))
9842 org-table-local-parameters))))
9844 (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
9845 (setq c (match-string 1)
9846 fields (org-split-string (match-string 2) " *| *"))
9848 (beginning-of-line (if (equal c "_") 2 0))
9849 (setq line (org-current-line) col 1)
9850 (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
9851 (setq fields1 (org-split-string (match-string 1) " *| *"))))
9852 (while (and fields1 (setq field (pop fields)))
9853 (setq v (pop fields1) col (1+ col))
9854 (when (and (stringp field) (stringp v)
9855 (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
9856 (push (cons field v) org-table-local-parameters)
9857 (push (list field line col) org-table-named-field-locations))))
9858 ;; Analyse the line types
9860 (setq org-table-current-begin-line (org-current-line)
9861 org-table-current-begin-pos (point)
9862 l org-table-current-begin-line)
9863 (while (looking-at "[ \t]*|\\(-\\)?")
9864 (push (if (match-end 1) 'hline 'dline) types)
9865 (if (match-end 1) (push l hlines) (push l dlines))
9866 (beginning-of-line 2)
9868 (setq org-table-current-line-types (apply 'vector (nreverse types))
9869 org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
9870 org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
9872 (defun org-table-maybe-eval-formula ()
9873 "Check if the current field starts with \"=\" or \":=\".
9874 If yes, store the formula and apply it."
9875 ;; We already know we are in a table. Get field will only return a formula
9876 ;; when appropriate. It might return a separator line, but no problem.
9877 (when org-table-formula-evaluate-inline
9878 (let* ((field (org-trim (or (org-table-get-field) "")))
9880 (when (string-match "^:?=\\(.*\\)" field)
9881 (setq named (equal (string-to-char field) ?:)
9882 eq (match-string 1 field))
9883 (if (or (fboundp 'calc-eval)
9884 (equal (substring eq 0 (min 2 (length eq))) "'("))
9885 (org-table-eval-formula (if named '(4) nil)
9886 (org-table-formula-from-user eq))
9887 (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
9889 (defvar org-recalc-commands nil
9890 "List of commands triggering the recalculation of a line.
9891 Will be filled automatically during use.")
9893 (defvar org-recalc-marks
9894 '((" " . "Unmarked: no special line, no automatic recalculation")
9895 ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
9896 ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
9897 ("!" . "Column name definition line. Reference in formula as $name.")
9898 ("$" . "Parameter definition line name=value. Reference in formula as $name.")
9899 ("_" . "Names for values in row below this one.")
9900 ("^" . "Names for values in row above this one.")))
9902 (defun org-table-rotate-recalc-marks (&optional newchar)
9903 "Rotate the recalculation mark in the first column.
9904 If in any row, the first field is not consistent with a mark,
9905 insert a new column for the markers.
9906 When there is an active region, change all the lines in the region,
9907 after prompting for the marking character.
9908 After each change, a message will be displayed indicating the meaning
9911 (unless (org-at-table-p) (error "Not at a table"))
9912 (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
9913 (beg (org-table-begin))
9914 (end (org-table-end))
9915 (l (org-current-line))
9916 (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
9917 (l2 (if (org-region-active-p) (org-current-line (region-end))))
9921 (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
9922 (col (org-table-current-column))
9923 (forcenew (car (assoc newchar org-recalc-marks)))
9926 (message "Change region to what mark? Type # * ! $ or SPC: ")
9927 (setq newchar (char-to-string (read-char-exclusive))
9928 forcenew (car (assoc newchar org-recalc-marks))))
9929 (if (and newchar (not forcenew))
9930 (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
9932 (if l1 (goto-line l1))
9934 (beginning-of-line 1)
9935 (unless (looking-at org-table-dataline-regexp)
9936 (error "Not at a table data line")))
9938 (org-table-goto-column 1)
9939 (org-table-insert-column)
9940 (org-table-goto-column (1+ col)))
9941 (setq epos (point-at-eol))
9943 (beginning-of-line 1)
9944 (org-table-get-field
9945 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
9947 (setq new (or forcenew
9948 (cadr (member (match-string 1) marks))))
9954 (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
9955 (and (looking-at org-table-dataline-regexp)
9956 (org-table-get-field 1 (concat " " new " "))))
9958 (if (not (= epos (point-at-eol))) (org-table-align))
9960 (and (interactive-p) (message "%s" (cdr (assoc new org-recalc-marks))))))
9962 (defun org-table-maybe-recalculate-line ()
9963 "Recompute the current line if marked for it, and if we haven't just done it."
9965 (and org-table-allow-automatic-line-recalculation
9966 (not (and (memq last-command org-recalc-commands)
9967 (equal org-last-recalc-line (org-current-line))))
9968 (save-excursion (beginning-of-line 1)
9969 (looking-at org-table-auto-recalculate-regexp))
9970 (org-table-recalculate) t))
9972 (defvar org-table-formula-debug nil
9973 "Non-nil means, debug table formulas.
9974 When nil, simply write \"#ERROR\" in corrupted fields.")
9975 (make-variable-buffer-local 'org-table-formula-debug)
9978 (defsubst org-set-calc-mode (var &optional value)
9980 (setq var (assoc var '(("D" calc-angle-mode deg)
9981 ("R" calc-angle-mode rad)
9982 ("F" calc-prefer-frac t)
9983 ("S" calc-symbolic-mode t)))
9984 value (nth 2 var) var (nth 1 var)))
9985 (if (memq var modes)
9986 (setcar (cdr (memq var modes)) value)
9987 (cons var (cons value modes)))
9990 (defun org-table-eval-formula (&optional arg equation
9991 suppress-align suppress-const
9992 suppress-store suppress-analysis)
9993 "Replace the table field value at the cursor by the result of a calculation.
9995 This function makes use of Dave Gillespie's Calc package, in my view the
9996 most exciting program ever written for GNU Emacs. So you need to have Calc
9997 installed in order to use this function.
9999 In a table, this command replaces the value in the current field with the
10000 result of a formula. It also installs the formula as the \"current\" column
10001 formula, by storing it in a special line below the table. When called
10002 with a `C-u' prefix, the current field must ba a named field, and the
10003 formula is installed as valid in only this specific field.
10005 When called with two `C-u' prefixes, insert the active equation
10006 for the field back into the current field, so that it can be
10007 edited there. This is useful in order to use \\[org-table-show-reference]
10008 to check the referenced fields.
10010 When called, the command first prompts for a formula, which is read in
10011 the minibuffer. Previously entered formulas are available through the
10012 history list, and the last used formula is offered as a default.
10013 These stored formulas are adapted correctly when moving, inserting, or
10014 deleting columns with the corresponding commands.
10016 The formula can be any algebraic expression understood by the Calc package.
10017 For details, see the Org-mode manual.
10019 This function can also be called from Lisp programs and offers
10020 additional arguments: EQUATION can be the formula to apply. If this
10021 argument is given, the user will not be prompted. SUPPRESS-ALIGN is
10022 used to speed-up recursive calls by by-passing unnecessary aligns.
10023 SUPPRESS-CONST suppresses the interpretation of constants in the
10024 formula, assuming that this has been done already outside the function.
10025 SUPPRESS-STORE means the formula should not be stored, either because
10026 it is already stored, or because it is a modified equation that should
10027 not overwrite the stored one."
10029 (org-table-check-inside-data-field)
10030 (or suppress-analysis (org-table-get-specials))
10031 (if (equal arg '(16))
10032 (let ((eq (org-table-current-field-formula)))
10033 (or eq (error "No equation active for current field"))
10034 (org-table-get-field nil eq)
10036 (setq org-table-may-need-update t))
10038 (ndown (if (integerp arg) arg 1))
10039 (org-table-automatic-realign nil)
10040 (case-fold-search nil)
10042 (formula (if (and equation suppress-store)
10044 (org-table-get-formula equation (equal arg '(4)))))
10045 (n0 (org-table-current-column))
10046 (modes (copy-sequence org-calc-default-modes))
10047 (numbers nil) ; was a variable, now fixed default
10049 n form form0 bw fmt x ev orig c lispp literal)
10050 ;; Parse the format string. Since we have a lot of modes, this is
10051 ;; a lot of work. However, I think calc still uses most of the time.
10052 (if (string-match ";" formula)
10053 (let ((tmp (org-split-string formula ";")))
10054 (setq formula (car tmp)
10055 fmt (concat (cdr (assoc "%" org-table-local-parameters))
10057 (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
10058 (setq c (string-to-char (match-string 1 fmt))
10059 n (string-to-number (match-string 2 fmt)))
10061 (setq modes (org-set-calc-mode 'calc-internal-prec n))
10062 (setq modes (org-set-calc-mode
10064 (list (cdr (assoc c '((?n . float) (?f . fix)
10065 (?s . sci) (?e . eng))))
10067 (setq fmt (replace-match "" t t fmt)))
10068 (if (string-match "[NT]" fmt)
10069 (setq numbers (equal (match-string 0 fmt) "N")
10070 fmt (replace-match "" t t fmt)))
10071 (if (string-match "L" fmt)
10073 fmt (replace-match "" t t fmt)))
10074 (if (string-match "E" fmt)
10076 fmt (replace-match "" t t fmt)))
10077 (while (string-match "[DRFS]" fmt)
10078 (setq modes (org-set-calc-mode (match-string 0 fmt)))
10079 (setq fmt (replace-match "" t t fmt)))
10080 (unless (string-match "\\S-" fmt)
10082 (if (and (not suppress-const) org-table-formula-use-constants)
10083 (setq formula (org-table-formula-substitute-names formula)))
10084 (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
10086 (setq fields (org-split-string
10088 (buffer-substring (point-at-bol) (point-at-eol)))
10091 (setq fields (mapcar
10092 (lambda (x) (number-to-string (string-to-number x)))
10094 (setq ndown (1- ndown))
10095 (setq form (copy-sequence formula)
10096 lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
10097 (if (and lispp literal) (setq lispp 'literal))
10098 ;; Check for old vertical references
10099 (setq form (org-rewrite-old-row-references form))
10100 ;; Insert complex ranges
10101 (while (string-match org-table-range-regexp form)
10105 (org-table-make-reference
10106 (org-table-get-range (match-string 0 form) nil n0)
10107 keep-empty numbers lispp))
10109 ;; Insert simple ranges
10110 (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
10114 (org-table-make-reference
10116 fields (string-to-number (match-string 1 form))
10117 (string-to-number (match-string 2 form)))
10118 keep-empty numbers lispp))
10121 ;; Insert the references to fields in same row
10122 (while (string-match "\\$\\([0-9]+\\)" form)
10123 (setq n (string-to-number (match-string 1 form))
10124 x (nth (1- (if (= n 0) n0 n)) fields))
10125 (unless x (error "Invalid field specifier \"%s\""
10126 (match-string 0 form)))
10127 (setq form (replace-match
10129 (org-table-make-reference x nil numbers lispp))
10133 (setq ev (condition-case nil
10134 (eval (eval (read form)))
10136 ev (if (numberp ev) (number-to-string ev) ev))
10137 (or (fboundp 'calc-eval)
10138 (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
10139 (setq ev (calc-eval (cons form modes)
10140 (if numbers 'num))))
10142 (when org-table-formula-debug
10143 (with-output-to-temp-buffer "*Substitution History*"
10144 (princ (format "Substitution history of formula
10148 $1-> %s\n" orig formula form0 form))
10150 (princ (format " %s^\nError: %s"
10151 (make-string (car ev) ?\-) (nth 1 ev)))
10152 (princ (format "Result: %s\nFormat: %s\nFinal: %s"
10154 (if fmt (format fmt (string-to-number ev)) ev)))))
10155 (setq bw (get-buffer-window "*Substitution History*"))
10156 (shrink-window-if-larger-than-buffer bw)
10157 (unless (and (interactive-p) (not ndown))
10158 (unless (let (inhibit-redisplay)
10159 (y-or-n-p "Debugging Formula. Continue to next? "))
10164 (if (listp ev) (setq fmt nil ev "#ERROR"))
10165 (org-table-justify-field-maybe
10166 (if fmt (format fmt (string-to-number ev)) ev))
10167 (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
10168 (call-interactively 'org-return)
10170 (and down (org-table-maybe-recalculate-line))
10171 (or suppress-align (and org-table-may-need-update
10172 (org-table-align))))))
10174 (defun org-table-put-field-property (prop value)
10176 (put-text-property (progn (skip-chars-backward "^|") (point))
10177 (progn (skip-chars-forward "^|") (point))
10180 (defun org-table-get-range (desc &optional tbeg col highlight)
10181 "Get a calc vector from a column, accorting to descriptor DESC.
10182 Optional arguments TBEG and COL can give the beginning of the table and
10183 the current column, to avoid unnecessary parsing.
10184 HIGHLIGHT means, just highlight the range."
10185 (if (not (equal (string-to-char desc) ?@))
10186 (setq desc (concat "@" desc)))
10188 (or tbeg (setq tbeg (org-table-begin)))
10189 (or col (setq col (org-table-current-column)))
10190 (let ((thisline (org-current-line))
10191 beg end c1 c2 r1 r2 rangep tmp)
10192 (unless (string-match org-table-range-regexp desc)
10193 (error "Invalid table range specifier `%s'" desc))
10194 (setq rangep (match-end 3)
10195 r1 (and (match-end 1) (match-string 1 desc))
10196 r2 (and (match-end 4) (match-string 4 desc))
10197 c1 (and (match-end 2) (substring (match-string 2 desc) 1))
10198 c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
10200 (and c1 (setq c1 (+ (string-to-number c1)
10201 (if (memq (string-to-char c1) '(?- ?+)) col 0))))
10202 (and c2 (setq c2 (+ (string-to-number c2)
10203 (if (memq (string-to-char c2) '(?- ?+)) col 0))))
10204 (if (equal r1 "") (setq r1 nil))
10205 (if (equal r2 "") (setq r2 nil))
10206 (if r1 (setq r1 (org-table-get-descriptor-line r1)))
10207 (if r2 (setq r2 (org-table-get-descriptor-line r2)))
10208 ; (setq r2 (or r2 r1) c2 (or c2 c1))
10209 (if (not r1) (setq r1 thisline))
10210 (if (not r2) (setq r2 thisline))
10211 (if (not c1) (setq c1 col))
10212 (if (not c2) (setq c2 col))
10213 (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
10217 (while (not (looking-at org-table-dataline-regexp))
10218 (beginning-of-line 2))
10219 (prog1 (org-trim (org-table-get-field c1))
10220 (if highlight (org-table-highlight-rectangle (point) (point)))))
10221 ;; A range, return a vector
10222 ;; First sort the numbers to get a regular ractangle
10223 (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
10224 (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
10226 (while (not (looking-at org-table-dataline-regexp))
10227 (beginning-of-line 2))
10228 (org-table-goto-column c1)
10231 (while (not (looking-at org-table-dataline-regexp))
10232 (beginning-of-line 0))
10233 (org-table-goto-column c2)
10236 (org-table-highlight-rectangle
10237 beg (progn (skip-chars-forward "^|\n") (point))))
10238 ;; return string representation of calc vector
10240 (apply 'append (org-table-copy-region beg end)))))))
10242 (defun org-table-get-descriptor-line (desc &optional cline bline table)
10243 "Analyze descriptor DESC and retrieve the corresponding line number.
10244 The cursor is currently in line CLINE, the table begins in line BLINE,
10245 and TABLE is a vector with line types."
10246 (if (string-match "^[0-9]+$" desc)
10247 (aref org-table-dlines (string-to-number desc))
10248 (setq cline (or cline (org-current-line))
10249 bline (or bline org-table-current-begin-line)
10250 table (or table org-table-current-line-types))
10252 (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
10254 (and (not (match-end 3)) (not (match-end 6)))
10255 (and (match-end 3) (match-end 6) (not (match-end 5))))
10256 (error "invalid row descriptor `%s'" desc))
10257 (let* ((hdir (and (match-end 2) (match-string 2 desc)))
10258 (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
10259 (odir (and (match-end 5) (match-string 5 desc)))
10260 (on (if (match-end 6) (string-to-number (match-string 6 desc))))
10261 (i (- cline bline))
10262 (rel (and (match-end 6)
10263 (or (and (match-end 1) (not (match-end 3)))
10265 (if (and hn (not hdir))
10267 (setq i 0 hdir "+")
10268 (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
10269 (if (and (not hn) on (not odir))
10270 (error "should never happen");;(aref org-table-dlines on)
10271 (if (and hn (> hn 0))
10272 (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
10274 (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
10277 (defun org-find-row-type (table i type backwards relative n)
10278 (let ((l (length table)))
10280 (while (and (setq i (+ i (if backwards -1 1)))
10282 (not (eq (aref table i) type))
10283 (if (and relative (eq (aref table i) 'hline))
10284 (progn (setq i (- i (if backwards -1 1)) n 1) nil)
10287 (if (or (< i 0) (>= i l))
10288 (error "Row descriptior leads outside table")
10291 (defun org-rewrite-old-row-references (s)
10292 (if (string-match "&[-+0-9I]" s)
10293 (error "Formula contains old &row reference, please rewrite using @-syntax")
10296 (defun org-table-make-reference (elements keep-empty numbers lispp)
10297 "Convert list ELEMENTS to something appropriate to insert into formula.
10298 KEEP-EMPTY indicated to keep empty fields, default is to skip them.
10299 NUMBERS indicates that everything should be converted to numbers.
10300 LISPP means to return something appropriate for a Lisp list."
10301 (if (stringp elements) ; just a single val
10303 (if (eq lispp 'literal)
10305 (prin1-to-string (if numbers (string-to-number elements) elements)))
10306 (if (equal elements "") (setq elements "0"))
10307 (if numbers (number-to-string (string-to-number elements)) elements))
10311 (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
10313 (setq elements (or elements '("0")))
10317 (if (eq lispp 'literal)
10319 (prin1-to-string (if numbers (string-to-number x) x))))
10321 (concat "[" (mapconcat
10323 (if numbers (number-to-string (string-to-number x)) x))
10327 (defun org-table-recalculate (&optional all noalign)
10328 "Recalculate the current table line by applying all stored formulas.
10329 With prefix arg ALL, do this for all lines in the table."
10331 (or (memq this-command org-recalc-commands)
10332 (setq org-recalc-commands (cons this-command org-recalc-commands)))
10333 (unless (org-at-table-p) (error "Not at a table"))
10334 (if (equal all '(16))
10335 (org-table-iterate)
10336 (org-table-get-specials)
10337 (let* ((eqlist (sort (org-table-get-stored-formulas)
10338 (lambda (a b) (string< (car a) (car b)))))
10339 (inhibit-redisplay (not debug-on-error))
10340 (line-re org-table-dataline-regexp)
10341 (thisline (org-current-line))
10342 (thiscol (org-table-current-column))
10343 beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
10344 ;; Insert constants in all formulas
10346 (mapcar (lambda (x)
10347 (setcdr x (org-table-formula-substitute-names (cdr x)))
10350 ;; Split the equation list
10351 (while (setq eq (pop eqlist))
10352 (if (<= (string-to-char (car eq)) ?9)
10354 (push eq eqlname)))
10355 (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
10358 (setq end (move-marker (make-marker) (1+ (org-table-end))))
10359 (goto-char (setq beg (org-table-begin)))
10360 (if (re-search-forward org-table-calculate-mark-regexp end t)
10361 ;; This is a table with marked lines, compute selected lines
10362 (setq line-re org-table-recalculate-regexp)
10363 ;; Move forward to the first non-header line
10364 (if (and (re-search-forward org-table-dataline-regexp end t)
10365 (re-search-forward org-table-hline-regexp end t)
10366 (re-search-forward org-table-dataline-regexp end t))
10367 (setq beg (match-beginning 0))
10368 nil))) ;; just leave beg where it is
10369 (setq beg (point-at-bol)
10370 end (move-marker (make-marker) (1+ (point-at-eol)))))
10372 (and all (message "Re-applying formulas to full table..."))
10374 ;; First find the named fields, and mark them untouchanble
10375 (remove-text-properties beg end '(org-untouchable t))
10376 (while (setq eq (pop eqlname))
10377 (setq name (car eq)
10378 a (assoc name org-table-named-field-locations))
10380 (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
10382 (aref org-table-dlines
10383 (string-to-number (match-string 1 name)))
10384 (string-to-number (match-string 2 name)))))
10385 (when (and a (or all (equal (nth 1 a) thisline)))
10386 (message "Re-applying formula to field: %s" name)
10387 (goto-line (nth 1 a))
10388 (org-table-goto-column (nth 2 a))
10389 (push (append a (list (cdr eq))) eqlname1)
10390 (org-table-put-field-property :org-untouchable t)))
10392 ;; Now evauluate the column formulas, but skip fields covered by
10395 (while (re-search-forward line-re end t)
10396 (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
10397 ;; Unprotected line, recalculate
10398 (and all (message "Re-applying formulas to full table...(line %d)"
10399 (setq cnt (1+ cnt))))
10400 (setq org-last-recalc-line (org-current-line))
10402 (while (setq entry (pop eql))
10403 (goto-line org-last-recalc-line)
10404 (org-table-goto-column (string-to-number (car entry)) nil 'force)
10405 (unless (get-text-property (point) :org-untouchable)
10406 (org-table-eval-formula nil (cdr entry)
10407 'noalign 'nocst 'nostore 'noanalysis)))))
10409 ;; Now evaluate the field formulas
10410 (while (setq eq (pop eqlname1))
10411 (message "Re-applying formula to field: %s" (car eq))
10412 (goto-line (nth 1 eq))
10413 (org-table-goto-column (nth 2 eq))
10414 (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
10415 'nostore 'noanalysis))
10417 (goto-line thisline)
10418 (org-table-goto-column thiscol)
10419 (remove-text-properties (point-min) (point-max) '(org-untouchable t))
10420 (or noalign (and org-table-may-need-update (org-table-align))
10421 (and all (message "Re-applying formulas to %d lines...done" cnt)))
10423 ;; back to initial position
10424 (message "Re-applying formulas...done")
10425 (goto-line thisline)
10426 (org-table-goto-column thiscol)
10427 (or noalign (and org-table-may-need-update (org-table-align))
10428 (and all (message "Re-applying formulas...done"))))))
10430 (defun org-table-iterate (&optional arg)
10431 "Recalculate the table until it does not change anymore."
10433 (let ((imax (if arg (prefix-numeric-value arg) 10))
10435 (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
10440 (org-table-recalculate 'all)
10441 (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
10442 (if (not (string= lasttbl thistbl))
10443 (setq lasttbl thistbl)
10445 (message "Convergence after %d iterations" i)
10446 (message "Table was already stable"))
10448 (error "No convergence after %d iterations" i))))
10450 (defun org-table-formula-substitute-names (f)
10451 "Replace $const with values in string F."
10452 (let ((start 0) a (f1 f) (pp (/= (string-to-char f) ?')))
10453 ;; First, check for column names
10454 (while (setq start (string-match org-table-column-name-regexp f start))
10455 (setq start (1+ start))
10456 (setq a (assoc (match-string 1 f) org-table-column-names))
10457 (setq f (replace-match (concat "$" (cdr a)) t t f)))
10458 ;; Parameters and constants
10460 (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
10461 (setq start (1+ start))
10462 (if (setq a (save-match-data
10463 (org-table-get-constant (match-string 1 f))))
10464 (setq f (replace-match
10465 (concat (if pp "(") a (if pp ")")) t t f))))
10466 (if org-table-formula-debug
10467 (put-text-property 0 (length f) :orig-formula f1 f))
10470 (defun org-table-get-constant (const)
10471 "Find the value for a parameter or constant in a formula.
10472 Parameters get priority."
10473 (or (cdr (assoc const org-table-local-parameters))
10474 (cdr (assoc const org-table-formula-constants-local))
10475 (cdr (assoc const org-table-formula-constants))
10476 (and (fboundp 'constants-get) (constants-get const))
10477 (and (string= (substring const 0 (min 5 (length const))) "PROP_")
10478 (org-entry-get nil (substring const 5) 'inherit))
10479 "#UNDEFINED_NAME"))
10481 (defvar org-table-fedit-map
10482 (let ((map (make-sparse-keymap)))
10483 (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
10484 (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
10485 (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
10486 (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
10487 (org-defkey map "\C-c?" 'org-table-show-reference)
10488 (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
10489 (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
10490 (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
10491 (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
10492 (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
10493 (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
10494 (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
10495 (org-defkey map [(meta down)] 'org-table-fedit-scroll)
10496 (org-defkey map [(meta tab)] 'lisp-complete-symbol)
10497 (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
10498 (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
10499 (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
10500 (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
10501 (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
10504 (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
10506 ["Finish and Install" org-table-fedit-finish t]
10507 ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
10508 ["Abort" org-table-fedit-abort t]
10510 ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
10511 ["Complete Lisp Symbol" lisp-complete-symbol t]
10513 "Shift Reference at Point"
10514 ["Up" org-table-fedit-ref-up t]
10515 ["Down" org-table-fedit-ref-down t]
10516 ["Left" org-table-fedit-ref-left t]
10517 ["Right" org-table-fedit-ref-right t]
10519 "Change Test Row for Column Formulas"
10520 ["Up" org-table-fedit-line-up t]
10521 ["Down" org-table-fedit-line-down t]
10523 ["Scroll Table Window" org-table-fedit-scroll t]
10524 ["Scroll Table Window down" org-table-fedit-scroll-down t]
10525 ["Show Table Grid" org-table-fedit-toggle-coordinates
10526 :style toggle :selected (with-current-buffer (marker-buffer org-pos)
10527 org-table-overlay-coordinates)]
10529 ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
10530 :style toggle :selected org-table-buffer-is-an]))
10534 (defun org-table-edit-formulas ()
10535 "Edit the formulas of the current table in a separate buffer."
10537 (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
10538 (beginning-of-line 0))
10539 (unless (org-at-table-p) (error "Not at a table"))
10540 (org-table-get-specials)
10541 (let ((key (org-table-current-field-formula 'key 'noerror))
10542 (eql (sort (org-table-get-stored-formulas 'noerror)
10543 'org-table-formula-less-p))
10544 (pos (move-marker (make-marker) (point)))
10546 (wc (current-window-configuration))
10547 (titles '((column . "# Column Formulas\n")
10548 (field . "# Field Formulas\n")
10549 (named . "# Named Field Formulas\n")))
10550 entry s type title)
10551 (org-switch-to-buffer-other-window "*Edit Formulas*")
10553 ;; Keep global-font-lock-mode from turning on font-lock-mode
10554 (let ((font-lock-global-modes '(not fundamental-mode)))
10555 (fundamental-mode))
10556 (org-set-local 'font-lock-global-modes (list 'not major-mode))
10557 (org-set-local 'org-pos pos)
10558 (org-set-local 'org-window-configuration wc)
10559 (use-local-map org-table-fedit-map)
10560 (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
10561 (easy-menu-add org-table-fedit-menu)
10562 (setq startline (org-current-line))
10563 (while (setq entry (pop eql))
10565 ((equal (string-to-char (car entry)) ?@) 'field)
10566 ((string-match "^[0-9]" (car entry)) 'column)
10568 (when (setq title (assq type titles))
10569 (or (bobp) (insert "\n"))
10570 (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
10571 (setq titles (delq title titles)))
10572 (if (equal key (car entry)) (setq startline (org-current-line)))
10573 (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
10574 (car entry) " = " (cdr entry) "\n"))
10575 (remove-text-properties 0 (length s) '(face nil) s)
10577 (if (eq org-table-use-standard-references t)
10578 (org-table-fedit-toggle-ref-type))
10579 (goto-line startline)
10580 (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
10582 (defun org-table-fedit-post-command ()
10583 (when (not (memq this-command '(lisp-complete-symbol)))
10584 (let ((win (selected-window)))
10586 (condition-case nil
10587 (org-table-show-reference)
10589 (select-window win)))))
10591 (defun org-table-formula-to-user (s)
10592 "Convert a formula from internal to user representation."
10593 (if (eq org-table-use-standard-references t)
10594 (org-table-convert-refs-to-an s)
10597 (defun org-table-formula-from-user (s)
10598 "Convert a formula from user to internal representation."
10599 (if org-table-use-standard-references
10600 (org-table-convert-refs-to-rc s)
10603 (defun org-table-convert-refs-to-rc (s)
10604 "Convert spreadsheet references from AB7 to @7$28.
10605 Works for single references, but also for entire formulas and even the
10608 (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
10611 ;; format match, just advance
10612 (setq start (match-end 0)))
10613 ((and (> (match-beginning 0) 0)
10614 (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
10615 (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
10616 ;; 3.e5 or something like this.
10617 (setq start (match-end 0)))
10619 (setq start (match-beginning 0)
10621 (if (equal (match-string 2 s) "&")
10622 (format "$%d" (org-letters-to-number (match-string 1 s)))
10624 (string-to-number (match-string 2 s))
10625 (org-letters-to-number (match-string 1 s))))
10629 (defun org-table-convert-refs-to-an (s)
10630 "Convert spreadsheet references from to @7$28 to AB7.
10631 Works for single references, but also for entire formulas and even the
10633 (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
10634 (setq s (replace-match
10636 (org-number-to-letters
10637 (string-to-number (match-string 2 s)))
10638 (string-to-number (match-string 1 s)))
10640 (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
10641 (setq s (replace-match (concat "\\1"
10642 (org-number-to-letters
10643 (string-to-number (match-string 2 s))) "&")
10647 (defun org-letters-to-number (s)
10648 "Convert a base 26 number represented by letters into an integer.
10649 For example: AB -> 28."
10651 (setq s (upcase s))
10652 (while (> (length s) 0)
10653 (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
10654 s (substring s 1)))
10657 (defun org-number-to-letters (n)
10658 "Convert an integer into a base 26 number represented by letters.
10659 For example: 28 -> AB."
10662 (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
10666 (defun org-table-fedit-convert-buffer (function)
10667 "Convert all references in this buffer, using FUNTION."
10668 (let ((line (org-current-line)))
10669 (goto-char (point-min))
10670 (while (not (eobp))
10671 (insert (funcall function (buffer-substring (point) (point-at-eol))))
10672 (delete-region (point) (point-at-eol))
10673 (or (eobp) (forward-char 1)))
10676 (defun org-table-fedit-toggle-ref-type ()
10677 "Convert all references in the buffer from B3 to @3$2 and back."
10679 (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
10680 (org-table-fedit-convert-buffer
10681 (if org-table-buffer-is-an
10682 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
10683 (message "Reference type switched to %s"
10684 (if org-table-buffer-is-an "A1 etc" "@row$column")))
10686 (defun org-table-fedit-ref-up ()
10687 "Shift the reference at point one row/hline up."
10689 (org-table-fedit-shift-reference 'up))
10690 (defun org-table-fedit-ref-down ()
10691 "Shift the reference at point one row/hline down."
10693 (org-table-fedit-shift-reference 'down))
10694 (defun org-table-fedit-ref-left ()
10695 "Shift the reference at point one field to the left."
10697 (org-table-fedit-shift-reference 'left))
10698 (defun org-table-fedit-ref-right ()
10699 "Shift the reference at point one field to the right."
10701 (org-table-fedit-shift-reference 'right))
10703 (defun org-table-fedit-shift-reference (dir)
10705 ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
10706 (if (memq dir '(left right))
10707 (org-rematch-and-replace 1 (eq dir 'left))
10708 (error "Cannot shift reference in this direction")))
10709 ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
10710 ;; A B3-like reference
10711 (if (memq dir '(up down))
10712 (org-rematch-and-replace 2 (eq dir 'up))
10713 (org-rematch-and-replace 1 (eq dir 'left))))
10715 "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
10716 ;; An internal reference
10717 (if (memq dir '(up down))
10718 (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
10719 (org-rematch-and-replace 5 (eq dir 'left))))))
10721 (defun org-rematch-and-replace (n &optional decr hline)
10722 "Re-match the group N, and replace it with the shifted refrence."
10723 (or (match-end n) (error "Cannot shift reference in this direction"))
10724 (goto-char (match-beginning n))
10725 (and (looking-at (regexp-quote (match-string n)))
10726 (replace-match (org-shift-refpart (match-string 0) decr hline)
10729 (defun org-shift-refpart (ref &optional decr hline)
10730 "Shift a refrence part REF.
10731 If DECR is set, decrease the references row/column, else increase.
10732 If HLINE is set, this may be a hline reference, it certainly is not
10733 a translation reference."
10735 (let* ((sign (string-match "^[-+]" ref)) n)
10737 (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
10739 ((and hline (string-match "^I+" ref))
10740 (setq n (string-to-number (concat sign (number-to-string (length ref)))))
10741 (setq n (+ n (if decr -1 1)))
10742 (if (= n 0) (setq n (+ n (if decr -1 1))))
10744 (setq sign (if (< n 0) "-" "+") n (abs n))
10745 (setq n (max 1 n)))
10746 (concat sign (make-string n ?I)))
10748 ((string-match "^[0-9]+" ref)
10749 (setq n (string-to-number (concat sign ref)))
10750 (setq n (+ n (if decr -1 1)))
10752 (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
10753 (number-to-string (max 1 n))))
10755 ((string-match "^[a-zA-Z]+" ref)
10756 (org-number-to-letters
10757 (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
10759 (t (error "Cannot shift reference"))))))
10761 (defun org-table-fedit-toggle-coordinates ()
10762 "Toggle the display of coordinates in the refrenced table."
10764 (let ((pos (marker-position org-pos)))
10765 (with-current-buffer (marker-buffer org-pos)
10768 (org-table-toggle-coordinate-overlays)))))
10770 (defun org-table-fedit-finish (&optional arg)
10771 "Parse the buffer for formula definitions and install them.
10772 With prefix ARG, apply the new formulas to the table."
10774 (org-table-remove-rectangle-highlight)
10775 (if org-table-use-standard-references
10777 (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
10778 (setq org-table-buffer-is-an nil)))
10779 (let ((pos org-pos) eql var form)
10780 (goto-char (point-min))
10781 (while (re-search-forward
10782 "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
10784 (setq var (if (match-end 2) (match-string 2) (match-string 1))
10785 form (match-string 3))
10786 (setq form (org-trim form))
10787 (when (not (equal form ""))
10788 (while (string-match "[ \t]*\n[ \t]*" form)
10789 (setq form (replace-match " " t t form)))
10790 (when (assoc var eql)
10791 (error "Double formulas for %s" var))
10792 (push (cons var form) eql)))
10794 (set-window-configuration org-window-configuration)
10795 (select-window (get-buffer-window (marker-buffer pos)))
10797 (unless (org-at-table-p)
10798 (error "Lost table position - cannot install formulae"))
10799 (org-table-store-formulas eql)
10800 (move-marker pos nil)
10801 (kill-buffer "*Edit Formulas*")
10803 (org-table-recalculate 'all)
10804 (message "New formulas installed - press C-u C-c C-c to apply."))))
10806 (defun org-table-fedit-abort ()
10807 "Abort editing formulas, without installing the changes."
10809 (org-table-remove-rectangle-highlight)
10810 (let ((pos org-pos))
10811 (set-window-configuration org-window-configuration)
10812 (select-window (get-buffer-window (marker-buffer pos)))
10814 (move-marker pos nil)
10815 (message "Formula editing aborted without installing changes")))
10817 (defun org-table-fedit-lisp-indent ()
10818 "Pretty-print and re-indent Lisp expressions in the Formula Editor."
10820 (let ((pos (point)) beg end ind)
10821 (beginning-of-line 1)
10823 ((looking-at "[ \t]")
10825 (call-interactively 'lisp-indent-line))
10826 ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
10827 ((not (fboundp 'pp-buffer))
10828 (error "Cannot pretty-print. Command `pp-buffer' is not available."))
10829 ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
10830 (goto-char (- (match-end 0) 2))
10832 (setq ind (make-string (current-column) ?\ ))
10833 (condition-case nil (forward-sexp 1)
10835 (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
10838 (narrow-to-region beg end)
10839 (if (eq last-command this-command)
10841 (goto-char (point-min))
10842 (setq this-command nil)
10843 (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
10844 (replace-match " ")))
10846 (untabify (point-min) (point-max))
10847 (goto-char (1+ (point-min)))
10848 (while (re-search-forward "^." nil t)
10849 (beginning-of-line 1)
10851 (goto-char (point-max))
10852 (backward-delete-char 1)))
10856 (defvar org-show-positions nil)
10858 (defun org-table-show-reference (&optional local)
10859 "Show the location/value of the $ expression at point."
10861 (org-table-remove-rectangle-highlight)
10863 (let ((pos (if local (point) org-pos))
10865 (org-inhibit-highlight-removal t)
10866 (win (selected-window))
10867 (org-show-positions nil)
10868 var name e what match dest)
10869 (if local (org-table-get-specials))
10871 ((or (org-at-regexp-p org-table-range-regexp2)
10872 (org-at-regexp-p org-table-translate-regexp)
10873 (org-at-regexp-p org-table-range-regexp))
10876 (org-table-convert-refs-to-rc (match-string 0))))
10878 ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
10879 ((org-at-regexp-p "\\$[0-9]+") 'column)
10881 (t (error "No reference at point")))
10882 match (and what (or match (match-string 0))))
10883 (when (and match (not (equal (match-beginning 0) (point-at-bol))))
10884 (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
10885 'secondary-selection))
10886 (org-add-hook 'before-change-functions
10887 'org-table-remove-rectangle-highlight)
10888 (if (eq what 'name) (setq var (substring match 1)))
10889 (when (eq what 'range)
10890 (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
10891 (setq match (org-table-formula-substitute-names match)))
10895 (re-search-backward "^\\S-" nil t)
10896 (beginning-of-line 1)
10897 (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
10900 (org-table-convert-refs-to-rc (match-string 1))))
10901 (org-table-add-rectangle-overlay
10902 (match-beginning 1) (match-end 1) face2))))
10903 (if (and (markerp pos) (marker-buffer pos))
10904 (if (get-buffer-window (marker-buffer pos))
10905 (select-window (get-buffer-window (marker-buffer pos)))
10906 (org-switch-to-buffer-other-window (get-buffer-window
10907 (marker-buffer pos)))))
10909 (org-table-force-dataline)
10911 (setq name (substring dest 1))
10913 ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
10914 (setq e (assoc name org-table-named-field-locations))
10915 (goto-line (nth 1 e))
10916 (org-table-goto-column (nth 2 e)))
10917 ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
10918 (let ((l (string-to-number (match-string 1 dest)))
10919 (c (string-to-number (match-string 2 dest))))
10920 (goto-line (aref org-table-dlines l))
10921 (org-table-goto-column c)))
10922 (t (org-table-goto-column (string-to-number name))))
10923 (move-marker pos (point))
10924 (org-table-highlight-rectangle nil nil face2))
10926 ((equal dest match))
10929 (condition-case nil
10931 (org-table-get-range match nil nil 'highlight))
10933 ((setq e (assoc var org-table-named-field-locations))
10934 (goto-line (nth 1 e))
10935 (org-table-goto-column (nth 2 e))
10936 (org-table-highlight-rectangle (point) (point))
10937 (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
10938 ((setq e (assoc var org-table-column-names))
10939 (org-table-goto-column (string-to-number (cdr e)))
10940 (org-table-highlight-rectangle (point) (point))
10941 (goto-char (org-table-begin))
10942 (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
10945 (goto-char (match-beginning 1))
10946 (org-table-highlight-rectangle)
10947 (message "Named column (column %s)" (cdr e)))
10948 (error "Column name not found")))
10951 (org-table-goto-column (string-to-number (substring match 1)))
10952 (org-table-highlight-rectangle (point) (point))
10953 (message "Column %s" (substring match 1)))
10954 ((setq e (assoc var org-table-local-parameters))
10955 (goto-char (org-table-begin))
10956 (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
10958 (goto-char (match-beginning 1))
10959 (org-table-highlight-rectangle)
10960 (message "Local parameter."))
10961 (error "Parameter not found")))
10964 ((not var) (error "No reference at point"))
10965 ((setq e (assoc var org-table-formula-constants-local))
10966 (message "Local Constant: $%s=%s in #+CONSTANTS line."
10968 ((setq e (assoc var org-table-formula-constants))
10969 (message "Constant: $%s=%s in `org-table-formula-constants'."
10971 ((setq e (and (fboundp 'constants-get) (constants-get var)))
10972 (message "Constant: $%s=%s, from `constants.el'%s."
10973 var e (format " (%s units)" constants-unit-system)))
10974 (t (error "Undefined name $%s" var)))))
10976 (when (and org-show-positions
10977 (not (memq this-command '(org-table-fedit-scroll
10978 org-table-fedit-scroll-down))))
10979 (push pos org-show-positions)
10980 (push org-table-current-begin-pos org-show-positions)
10981 (let ((min (apply 'min org-show-positions))
10982 (max (apply 'max org-show-positions)))
10983 (goto-char min) (recenter 0)
10985 (or (pos-visible-in-window-p max) (recenter -1))))
10986 (select-window win))))
10988 (defun org-table-force-dataline ()
10989 "Make sure the cursor is in a dataline in a table."
10990 (unless (save-excursion
10991 (beginning-of-line 1)
10992 (looking-at org-table-dataline-regexp))
10993 (let* ((re org-table-dataline-regexp)
10994 (p1 (save-excursion (re-search-forward re nil 'move)))
10995 (p2 (save-excursion (re-search-backward re nil 'move))))
10997 (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
10999 ((or p1 p2) (goto-char (or p1 p2)))
11000 (t (error "No table dataline around here"))))))
11002 (defun org-table-fedit-line-up ()
11003 "Move cursor one line up in the window showing the table."
11005 (org-table-fedit-move 'previous-line))
11007 (defun org-table-fedit-line-down ()
11008 "Move cursor one line down in the window showing the table."
11010 (org-table-fedit-move 'next-line))
11012 (defun org-table-fedit-move (command)
11013 "Move the cursor in the window shoinw the table.
11014 Use COMMAND to do the motion, repeat if necessary to end up in a data line."
11015 (let ((org-table-allow-automatic-line-recalculation nil)
11016 (pos org-pos) (win (selected-window)) p)
11017 (select-window (get-buffer-window (marker-buffer org-pos)))
11019 (call-interactively command)
11020 (while (and (org-at-table-p)
11021 (org-at-table-hline-p))
11022 (call-interactively command))
11023 (or (org-at-table-p) (goto-char p))
11024 (move-marker pos (point))
11025 (select-window win)))
11027 (defun org-table-fedit-scroll (N)
11029 (let ((other-window-scroll-buffer (marker-buffer org-pos)))
11030 (scroll-other-window N)))
11032 (defun org-table-fedit-scroll-down (N)
11034 (org-table-fedit-scroll (- N)))
11036 (defvar org-table-rectangle-overlays nil)
11038 (defun org-table-add-rectangle-overlay (beg end &optional face)
11039 "Add a new overlay."
11040 (let ((ov (org-make-overlay beg end)))
11041 (org-overlay-put ov 'face (or face 'secondary-selection))
11042 (push ov org-table-rectangle-overlays)))
11044 (defun org-table-highlight-rectangle (&optional beg end face)
11045 "Highlight rectangular region in a table."
11046 (setq beg (or beg (point)) end (or end (point)))
11047 (let ((b (min beg end))
11050 (and (boundp 'org-show-positions)
11051 (setq org-show-positions (cons b (cons e org-show-positions))))
11052 (goto-char (min beg end))
11053 (setq l1 (org-current-line)
11054 c1 (org-table-current-column))
11055 (goto-char (max beg end))
11056 (setq l2 (org-current-line)
11057 c2 (org-table-current-column))
11058 (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
11060 (beginning-of-line 1)
11061 (loop for line from l1 to l2 do
11062 (when (looking-at org-table-dataline-regexp)
11063 (org-table-goto-column c1)
11064 (skip-chars-backward "^|\n") (setq beg (point))
11065 (org-table-goto-column c2)
11066 (skip-chars-forward "^|\n") (setq end (point))
11067 (org-table-add-rectangle-overlay beg end face))
11068 (beginning-of-line 2))
11070 (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
11072 (defun org-table-remove-rectangle-highlight (&rest ignore)
11073 "Remove the rectangle overlays."
11074 (unless org-inhibit-highlight-removal
11075 (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
11076 (mapc 'org-delete-overlay org-table-rectangle-overlays)
11077 (setq org-table-rectangle-overlays nil)))
11079 (defvar org-table-coordinate-overlays nil
11080 "Collects the cooordinate grid overlays, so that they can be removed.")
11081 (make-variable-buffer-local 'org-table-coordinate-overlays)
11083 (defun org-table-overlay-coordinates ()
11084 "Add overlays to the table at point, to show row/column coordinates."
11086 (mapc 'org-delete-overlay org-table-coordinate-overlays)
11087 (setq org-table-coordinate-overlays nil)
11089 (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
11090 (goto-char (org-table-begin))
11091 (while (org-at-table-p)
11092 (setq eol (point-at-eol))
11093 (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
11094 (push ov org-table-coordinate-overlays)
11095 (setq hline (looking-at org-table-hline-regexp))
11096 (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
11097 (format "%4d" (setq id (1+ id)))))
11098 (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
11101 (while (re-search-forward "[+|]\\(-+\\)" eol t)
11102 (setq beg (1+ (match-beginning 0))
11104 s1 (concat "$" (int-to-string ic))
11105 s2 (org-number-to-letters ic)
11106 str (if (eq org-table-use-standard-references t) s2 s1))
11107 (setq ov (org-make-overlay beg (+ beg (length str))))
11108 (push ov org-table-coordinate-overlays)
11109 (org-overlay-display ov str 'org-special-keyword 'evaporate)))
11110 (beginning-of-line 2)))))
11112 (defun org-table-toggle-coordinate-overlays ()
11113 "Toggle the display of Row/Column numbers in tables."
11115 (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
11116 (message "Row/Column number display turned %s"
11117 (if org-table-overlay-coordinates "on" "off"))
11118 (if (and (org-at-table-p) org-table-overlay-coordinates)
11120 (unless org-table-overlay-coordinates
11121 (mapc 'org-delete-overlay org-table-coordinate-overlays)
11122 (setq org-table-coordinate-overlays nil)))
11124 (defun org-table-toggle-formula-debugger ()
11125 "Toggle the formula debugger in tables."
11127 (setq org-table-formula-debug (not org-table-formula-debug))
11128 (message "Formula debugging has been turned %s"
11129 (if org-table-formula-debug "on" "off")))
11131 ;;; The orgtbl minor mode
11133 ;; Define a minor mode which can be used in other modes in order to
11134 ;; integrate the org-mode table editor.
11136 ;; This is really a hack, because the org-mode table editor uses several
11137 ;; keys which normally belong to the major mode, for example the TAB and
11138 ;; RET keys. Here is how it works: The minor mode defines all the keys
11139 ;; necessary to operate the table editor, but wraps the commands into a
11140 ;; function which tests if the cursor is currently inside a table. If that
11141 ;; is the case, the table editor command is executed. However, when any of
11142 ;; those keys is used outside a table, the function uses `key-binding' to
11143 ;; look up if the key has an associated command in another currently active
11144 ;; keymap (minor modes, major mode, global), and executes that command.
11145 ;; There might be problems if any of the keys used by the table editor is
11146 ;; otherwise used as a prefix key.
11148 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
11149 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
11150 ;; addresses this by checking explicitly for both bindings.
11152 ;; The optimized version (see variable `orgtbl-optimized') takes over
11153 ;; all keys which are bound to `self-insert-command' in the *global map*.
11154 ;; Some modes bind other commands to simple characters, for example
11155 ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
11156 ;; active, this binding is ignored inside tables and replaced with a
11157 ;; modified self-insert.
11159 (defvar orgtbl-mode nil
11160 "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
11161 table editor in arbitrary modes.")
11162 (make-variable-buffer-local 'orgtbl-mode)
11164 (defvar orgtbl-mode-map (make-keymap)
11165 "Keymap for `orgtbl-mode'.")
11168 (defun turn-on-orgtbl ()
11169 "Unconditionally turn on `orgtbl-mode'."
11172 (defvar org-old-auto-fill-inhibit-regexp nil
11173 "Local variable used by `orgtbl-mode'")
11175 (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
11176 "Matches a line belonging to an orgtbl.")
11178 (defconst orgtbl-extra-font-lock-keywords
11179 (list (list (concat "^" orgtbl-line-start-regexp ".*")
11180 0 (quote 'org-table) 'prepend))
11181 "Extra font-lock-keywords to be added when orgtbl-mode is active.")
11184 (defun orgtbl-mode (&optional arg)
11185 "The `org-mode' table editor as a minor mode for use in other modes."
11188 ;; Exit without error, in case some hook functions calls this
11189 ;; by accident in org-mode.
11190 (message "Orgtbl-mode is not useful in org-mode, command ignored")
11192 (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
11195 (and (orgtbl-setup) (defun orgtbl-setup () nil))
11196 ;; Make sure we are first in minor-mode-map-alist
11197 (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
11198 (and c (setq minor-mode-map-alist
11199 (cons c (delq c minor-mode-map-alist)))))
11200 (org-set-local (quote org-table-may-need-update) t)
11201 (org-add-hook 'before-change-functions 'org-before-change-function
11203 (org-set-local 'org-old-auto-fill-inhibit-regexp
11204 auto-fill-inhibit-regexp)
11205 (org-set-local 'auto-fill-inhibit-regexp
11206 (if auto-fill-inhibit-regexp
11207 (concat orgtbl-line-start-regexp "\\|"
11208 auto-fill-inhibit-regexp)
11209 orgtbl-line-start-regexp))
11210 (org-add-to-invisibility-spec '(org-cwidth))
11211 (when (fboundp 'font-lock-add-keywords)
11212 (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
11213 (org-restart-font-lock))
11214 (easy-menu-add orgtbl-mode-menu)
11215 (run-hooks 'orgtbl-mode-hook))
11216 (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
11217 (org-cleanup-narrow-column-properties)
11218 (org-remove-from-invisibility-spec '(org-cwidth))
11219 (remove-hook 'before-change-functions 'org-before-change-function t)
11220 (when (fboundp 'font-lock-remove-keywords)
11221 (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
11222 (org-restart-font-lock))
11223 (easy-menu-remove orgtbl-mode-menu)
11224 (force-mode-line-update 'all))))
11226 (defun org-cleanup-narrow-column-properties ()
11227 "Remove all properties related to narrow-column invisibility."
11229 (while (setq s (text-property-any s (point-max)
11230 'display org-narrow-column-arrow))
11231 (remove-text-properties s (1+ s) '(display t)))
11233 (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
11234 (remove-text-properties s (1+ s) '(org-cwidth t)))
11236 (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
11237 (remove-text-properties s (1+ s) '(invisible t)))))
11239 ;; Install it as a minor mode.
11240 (put 'orgtbl-mode :included t)
11241 (put 'orgtbl-mode :menu-tag "Org Table Mode")
11242 (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
11244 (defun orgtbl-make-binding (fun n &rest keys)
11245 "Create a function for binding in the table minor mode.
11246 FUN is the command to call inside a table. N is used to create a unique
11247 command name. KEYS are keys that should be checked in for a command
11248 to execute outside of tables."
11251 (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
11253 (concat "In tables, run `" (symbol-name fun) "'.\n"
11254 "Outside of tables, run the binding of `"
11255 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
11260 (list 'call-interactively (list 'quote fun))
11261 (list 'let '(orgtbl-mode)
11262 (list 'call-interactively
11264 (mapcar (lambda (k)
11265 (list 'key-binding k))
11267 '('orgtbl-error))))))))
11269 (defun orgtbl-error ()
11270 "Error when there is no default binding for a table key."
11272 (error "This key has no function outside tables"))
11274 (defun orgtbl-setup ()
11275 "Setup orgtbl keymaps."
11279 '([(meta shift left)] org-table-delete-column)
11280 '([(meta left)] org-table-move-column-left)
11281 '([(meta right)] org-table-move-column-right)
11282 '([(meta shift right)] org-table-insert-column)
11283 '([(meta shift up)] org-table-kill-row)
11284 '([(meta shift down)] org-table-insert-row)
11285 '([(meta up)] org-table-move-row-up)
11286 '([(meta down)] org-table-move-row-down)
11287 '("\C-c\C-w" org-table-cut-region)
11288 '("\C-c\M-w" org-table-copy-region)
11289 '("\C-c\C-y" org-table-paste-rectangle)
11290 '("\C-c-" org-table-insert-hline)
11291 '("\C-c}" org-table-toggle-coordinate-overlays)
11292 '("\C-c{" org-table-toggle-formula-debugger)
11293 '("\C-m" org-table-next-row)
11294 '([(shift return)] org-table-copy-down)
11295 '("\C-c\C-q" org-table-wrap-region)
11296 '("\C-c?" org-table-field-info)
11297 '("\C-c " org-table-blank-field)
11298 '("\C-c+" org-table-sum)
11299 '("\C-c=" org-table-eval-formula)
11300 '("\C-c'" org-table-edit-formulas)
11301 '("\C-c`" org-table-edit-field)
11302 '("\C-c*" org-table-recalculate)
11303 '("\C-c|" org-table-create-or-convert-from-region)
11304 '("\C-c^" org-table-sort-lines)
11305 '([(control ?#)] org-table-rotate-recalc-marks)))
11307 (while (setq elt (pop bindings))
11308 (setq nfunc (1+ nfunc))
11309 (setq key (org-key (car elt))
11311 cmd (orgtbl-make-binding fun nfunc key))
11312 (org-defkey orgtbl-mode-map key cmd))
11314 ;; Special treatment needed for TAB and RET
11315 (org-defkey orgtbl-mode-map [(return)]
11316 (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
11317 (org-defkey orgtbl-mode-map "\C-m"
11318 (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
11320 (org-defkey orgtbl-mode-map [(tab)]
11321 (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
11322 (org-defkey orgtbl-mode-map "\C-i"
11323 (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
11325 (org-defkey orgtbl-mode-map [(shift tab)]
11326 (orgtbl-make-binding 'org-table-previous-field 104
11327 [(shift tab)] [(tab)] "\C-i"))
11329 (org-defkey orgtbl-mode-map "\M-\C-m"
11330 (orgtbl-make-binding 'org-table-wrap-region 105
11331 "\M-\C-m" [(meta return)]))
11332 (org-defkey orgtbl-mode-map [(meta return)]
11333 (orgtbl-make-binding 'org-table-wrap-region 106
11334 [(meta return)] "\M-\C-m"))
11336 (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
11337 (when orgtbl-optimized
11338 ;; If the user wants maximum table support, we need to hijack
11339 ;; some standard editing functions
11340 (org-remap orgtbl-mode-map
11341 'self-insert-command 'orgtbl-self-insert-command
11342 'delete-char 'org-delete-char
11343 'delete-backward-char 'org-delete-backward-char)
11344 (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
11345 (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
11347 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
11348 ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
11349 ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
11350 ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
11352 ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
11353 ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
11354 ["Copy Field from Above"
11355 org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
11358 ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
11359 ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
11360 ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
11361 ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
11363 ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
11364 ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
11365 ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
11366 ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
11367 ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
11369 ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
11371 ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
11372 ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
11373 ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
11374 ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
11377 ["Insert table template" orgtbl-insert-radio-table
11378 (assq major-mode orgtbl-radio-table-templates)]
11379 ["Comment/uncomment table" orgtbl-toggle-comment t])
11381 ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
11382 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
11383 ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
11384 ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
11385 ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
11386 ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
11387 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
11388 ["Sum Column/Rectangle" org-table-sum
11389 :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
11390 ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
11392 org-table-toggle-formula-debugger :active (org-at-table-p)
11394 :style toggle :selected org-table-formula-debug]
11395 ["Show Col/Row Numbers"
11396 org-table-toggle-coordinate-overlays :active (org-at-table-p)
11398 :style toggle :selected org-table-overlay-coordinates]
11402 (defun orgtbl-ctrl-c-ctrl-c (arg)
11403 "If the cursor is inside a table, realign the table.
11404 It it is a table to be sent away to a receiver, do it.
11405 With prefix arg, also recompute table."
11407 (let ((pos (point)) action)
11409 (beginning-of-line 1)
11410 (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
11411 ((looking-at "[ \t]*|") pos)
11412 ((looking-at "#\\+TBLFM:") 'recalc))))
11416 (org-table-maybe-eval-formula)
11418 (call-interactively 'org-table-recalculate)
11419 (org-table-maybe-recalculate-line))
11420 (call-interactively 'org-table-align)
11421 (orgtbl-send-table 'maybe))
11422 ((eq action 'recalc)
11424 (beginning-of-line 1)
11425 (skip-chars-backward " \r\n\t")
11426 (if (org-at-table-p)
11427 (org-call-with-arg 'org-table-recalculate t))))
11428 (t (let (orgtbl-mode)
11429 (call-interactively (key-binding "\C-c\C-c")))))))
11431 (defun orgtbl-tab (arg)
11432 "Justification and field motion for `orgtbl-mode'."
11434 (if arg (org-table-edit-field t)
11435 (org-table-justify-field-maybe)
11436 (org-table-next-field)))
11438 (defun orgtbl-ret ()
11439 "Justification and field motion for `orgtbl-mode'."
11441 (org-table-justify-field-maybe)
11442 (org-table-next-row))
11444 (defun orgtbl-self-insert-command (N)
11445 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
11446 If the cursor is in a table looking at whitespace, the whitespace is
11447 overwritten, and the table is not marked as requiring realignment."
11449 (if (and (org-at-table-p)
11451 (and org-table-auto-blank-field
11452 (member last-command
11453 '(orgtbl-hijacker-command-100
11454 orgtbl-hijacker-command-101
11455 orgtbl-hijacker-command-102
11456 orgtbl-hijacker-command-103
11457 orgtbl-hijacker-command-104
11458 orgtbl-hijacker-command-105))
11459 (org-table-blank-field))
11462 (looking-at "[^|\n]* +|"))
11463 (let (org-table-may-need-update)
11464 (goto-char (1- (match-end 0)))
11465 (delete-backward-char 1)
11466 (goto-char (match-beginning 0))
11467 (self-insert-command N))
11468 (setq org-table-may-need-update t)
11470 (call-interactively (key-binding (vector last-input-event))))))
11472 (defun org-force-self-insert (N)
11473 "Needed to enforce self-insert under remapping."
11475 (self-insert-command N))
11477 (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
11478 "Regula expression matching exponentials as produced by calc.")
11480 (defvar org-table-clean-did-remove-column nil)
11482 (defun orgtbl-export (table target)
11483 (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
11484 (lines (org-split-string table "[ \t]*\n[ \t]*"))
11485 org-table-last-alignment org-table-last-column-widths
11487 (if (not (fboundp func))
11488 (error "Cannot export orgtbl table to %s" target))
11489 (setq lines (org-table-clean-before-export lines))
11493 (if (string-match org-table-hline-regexp x)
11495 (org-split-string (org-trim x) "\\s-*|\\s-*")))
11497 (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
11499 (loop for i from (1- maxcol) downto 0 do
11500 (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
11501 (setq column (delq nil column))
11502 (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
11503 (push (> (/ (apply '+ (mapcar (lambda (x) (if (string-match org-table-number-regexp x) 1 0)) column)) maxcol) org-table-number-fraction) org-table-last-alignment))
11504 (funcall func table nil)))
11506 (defun orgtbl-send-table (&optional maybe)
11507 "Send a tranformed version of this table to the receiver position.
11508 With argument MAYBE, fail quietly if no transformation is defined for
11512 (unless (org-at-table-p) (error "Not at a table"))
11513 ;; when non-interactive, we assume align has just happened.
11514 (when (interactive-p) (org-table-align))
11516 (goto-char (org-table-begin))
11517 (beginning-of-line 0)
11518 (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
11521 (error "Don't know how to transform this table."))))
11522 (let* ((name (match-string 1))
11524 (transform (intern (match-string 2)))
11525 (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
11526 (skip (plist-get params :skip))
11527 (skipcols (plist-get params :skipcols))
11528 (txt (buffer-substring-no-properties
11529 (org-table-begin) (org-table-end)))
11530 (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
11531 (lines (org-table-clean-before-export lines))
11532 (i0 (if org-table-clean-did-remove-column 2 1))
11535 (if (string-match org-table-hline-regexp x)
11537 (org-remove-by-index
11538 (org-split-string (org-trim x) "\\s-*|\\s-*")
11541 (fun (if (= i0 2) 'cdr 'identity))
11542 (org-table-last-alignment
11543 (org-remove-by-index (funcall fun org-table-last-alignment)
11545 (org-table-last-column-widths
11546 (org-remove-by-index (funcall fun org-table-last-column-widths)
11549 (unless (fboundp transform)
11550 (error "No such transformation function %s" transform))
11551 (setq txt (funcall transform table params))
11552 ;; Find the insertion place
11554 (goto-char (point-min))
11555 (unless (re-search-forward
11556 (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
11557 (error "Don't know where to insert translated table"))
11558 (goto-char (match-beginning 0))
11559 (beginning-of-line 2)
11561 (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
11562 (error "Cannot find end of insertion region"))
11563 (beginning-of-line 1)
11564 (delete-region beg (point))
11567 (message "Table converted and installed at receiver location"))))
11569 (defun org-remove-by-index (list indices &optional i0)
11570 "Remove the elements in LIST with indices in INDICES.
11571 First element has index 0, or I0 if given."
11574 (if (integerp indices) (setq indices (list indices)))
11575 (setq i0 (1- (or i0 0)))
11576 (delq :rm (mapcar (lambda (x)
11578 (if (memq i0 indices) :rm x))
11581 (defun orgtbl-toggle-comment ()
11582 "Comment or uncomment the orgtbl at point."
11584 (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
11585 (re2 (concat "^" orgtbl-line-start-regexp))
11586 (commented (save-excursion (beginning-of-line 1)
11587 (cond ((looking-at re1) t)
11588 ((looking-at re2) nil)
11589 (t (error "Not at an org table")))))
11590 (re (if commented re1 re2))
11593 (beginning-of-line 1)
11594 (while (looking-at re) (beginning-of-line 0))
11595 (beginning-of-line 2)
11597 (while (looking-at re) (beginning-of-line 2))
11598 (setq end (point)))
11599 (comment-region beg end (if commented '(4) nil))))
11601 (defun orgtbl-insert-radio-table ()
11602 "Insert a radio table template appropriate for this major mode."
11604 (let* ((e (assq major-mode orgtbl-radio-table-templates))
11607 (unless e (error "No radio table setup defined for %s" major-mode))
11608 (setq name (read-string "Table name: "))
11609 (while (string-match "%n" txt)
11610 (setq txt (replace-match name t t txt)))
11611 (or (bolp) (insert "\n"))
11616 (defun org-get-param (params header i sym &optional hsym)
11617 "Get parameter value for symbol SYM.
11618 If this is a header line, actually get the value for the symbol with an
11619 additional \"h\" inserted after the colon.
11620 If the value is a protperty list, get the element for the current column.
11621 Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
11622 (let ((val (plist-get params sym)))
11623 (and hsym header (setq val (or (plist-get params hsym) val)))
11624 (if (consp val) (plist-get val i) val)))
11626 (defun orgtbl-to-generic (table params)
11627 "Convert the orgtbl-mode TABLE to some other format.
11628 This generic routine can be used for many standard cases.
11629 TABLE is a list, each entry either the symbol `hline' for a horizontal
11630 separator line, or a list of fields for that line.
11631 PARAMS is a property list of parameters that can influence the conversion.
11632 For the generic converter, some parameters are obligatory: You need to
11633 specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
11634 :splice, you must have :tstart and :tend.
11636 Valid parameters are
11638 :tstart String to start the table. Ignored when :splice is t.
11639 :tend String to end the table. Ignored when :splice is t.
11641 :splice When set to t, return only table body lines, don't wrap
11642 them into :tstart and :tend. Default is nil.
11644 :hline String to be inserted on horizontal separation lines.
11645 May be nil to ignore hlines.
11647 :lstart String to start a new table line.
11648 :lend String to end a table line
11649 :sep Separator between two fields
11650 :lfmt Format for entire line, with enough %s to capture all fields.
11651 If this is present, :lstart, :lend, and :sep are ignored.
11652 :fmt A format to be used to wrap the field, should contain
11653 %s for the original field value. For example, to wrap
11654 everything in dollars, you could use :fmt \"$%s$\".
11655 This may also be a property list with column numbers and
11656 formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
11658 :hlstart :hlend :hlsep :hlfmt :hfmt
11659 Same as above, specific for the header lines in the table.
11660 All lines before the first hline are treated as header.
11661 If any of these is not present, the data line value is used.
11663 :efmt Use this format to print numbers with exponentials.
11664 The format should have %s twice for inserting mantissa
11665 and exponent, for example \"%s\\\\times10^{%s}\". This
11666 may also be a property list with column numbers and
11667 formats. :fmt will still be applied after :efmt.
11669 In addition to this, the parameters :skip and :skipcols are always handled
11670 directly by `orgtbl-send-table'. See manual."
11673 (splicep (plist-get p :splice))
11674 (hline (plist-get p :hline))
11675 rtn line i fm efm lfmt h)
11677 ;; Do we have a header?
11678 (if (and (not splicep) (listp (car table)) (memq 'hline table))
11683 (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
11685 ;; Now loop over all lines
11686 (while (setq line (pop table))
11687 (if (eq line 'hline)
11688 ;; A horizontal separator line
11689 (progn (if hline (push hline rtn))
11690 (setq h nil)) ; no longer in header
11691 ;; A normal line. Convert the fields, push line onto the result list
11697 fm (org-get-param p h i :fmt :hfmt)
11698 efm (org-get-param p h i :efmt))
11699 (if (and efm (string-match orgtbl-exp-regexp f))
11701 efm (match-string 1 f) (match-string 2 f))))
11702 (if fm (setq f (format fm f)))
11705 (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
11706 (push (apply 'format lfmt line) rtn)
11708 (org-get-param p h i :lstart :hlstart)
11709 (mapconcat 'identity line (org-get-param p h i :sep :hsep))
11710 (org-get-param p h i :lend :hlend))
11714 (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
11716 (mapconcat 'identity (nreverse rtn) "\n")))
11718 (defun orgtbl-to-latex (table params)
11719 "Convert the orgtbl-mode TABLE to LaTeX.
11720 TABLE is a list, each entry either the symbol `hline' for a horizontal
11721 separator line, or a list of fields for that line.
11722 PARAMS is a property list of parameters that can influence the conversion.
11723 Supports all parameters from `orgtbl-to-generic'. Most important for
11726 :splice When set to t, return only table body lines, don't wrap
11727 them into a tabular environment. Default is nil.
11729 :fmt A format to be used to wrap the field, should contain %s for the
11730 original field value. For example, to wrap everything in dollars,
11731 use :fmt \"$%s$\". This may also be a property list with column
11732 numbers and formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
11734 :efmt Format for transforming numbers with exponentials. The format
11735 should have %s twice for inserting mantissa and exponent, for
11736 example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
11737 This may also be a property list with column numbers and formats.
11739 The general parameters :skip and :skipcols have already been applied when
11740 this function is called."
11741 (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
11742 org-table-last-alignment ""))
11745 :tstart (concat "\\begin{tabular}{" alignment "}")
11746 :tend "\\end{tabular}"
11747 :lstart "" :lend " \\\\" :sep " & "
11748 :efmt "%s\\,(%s)" :hline "\\hline")))
11749 (orgtbl-to-generic table (org-combine-plists params2 params))))
11751 (defun orgtbl-to-html (table params)
11752 "Convert the orgtbl-mode TABLE to LaTeX.
11753 TABLE is a list, each entry either the symbol `hline' for a horizontal
11754 separator line, or a list of fields for that line.
11755 PARAMS is a property list of parameters that can influence the conversion.
11756 Currently this function recognizes the following parameters:
11758 :splice When set to t, return only table body lines, don't wrap
11759 them into a <table> environment. Default is nil.
11761 The general parameters :skip and :skipcols have already been applied when
11762 this function is called. The function does *not* use `orgtbl-to-generic',
11763 so you cannot specify parameters for it."
11764 (let* ((splicep (plist-get params :splice))
11766 ;; Just call the formatter we already have
11767 ;; We need to make text lines for it, so put the fields back together.
11768 (setq html (org-format-org-table-html
11773 (concat "| " (mapconcat 'identity x " | ") " |")))
11776 (if (string-match "\n+\\'" html)
11777 (setq html (replace-match "" t t html)))
11780 (defun orgtbl-to-texinfo (table params)
11781 "Convert the orgtbl-mode TABLE to TeXInfo.
11782 TABLE is a list, each entry either the symbol `hline' for a horizontal
11783 separator line, or a list of fields for that line.
11784 PARAMS is a property list of parameters that can influence the conversion.
11785 Supports all parameters from `orgtbl-to-generic'. Most important for
11788 :splice nil/t When set to t, return only table body lines, don't wrap
11789 them into a multitable environment. Default is nil.
11791 :fmt fmt A format to be used to wrap the field, should contain
11792 %s for the original field value. For example, to wrap
11793 everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
11794 This may also be a property list with column numbers and
11795 formats. For example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
11797 :cf \"f1 f2..\" The column fractions for the table. By default these
11798 are computed automatically from the width of the columns
11801 The general parameters :skip and :skipcols have already been applied when
11802 this function is called."
11803 (let* ((total (float (apply '+ org-table-last-column-widths)))
11804 (colfrac (or (plist-get params :cf)
11806 (lambda (x) (format "%.3f" (/ (float x) total)))
11807 org-table-last-column-widths " ")))
11810 :tstart (concat "@multitable @columnfractions " colfrac)
11811 :tend "@end multitable"
11812 :lstart "@item " :lend "" :sep " @tab "
11813 :hlstart "@headitem ")))
11814 (orgtbl-to-generic table (org-combine-plists params2 params))))
11818 ;;; Link abbreviations
11820 (defun org-link-expand-abbrev (link)
11821 "Apply replacements as defined in `org-link-abbrev-alist."
11822 (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
11823 (let* ((key (match-string 1 link))
11824 (as (or (assoc key org-link-abbrev-alist-local)
11825 (assoc key org-link-abbrev-alist)))
11826 (tag (and (match-end 2) (match-string 3 link)))
11830 (setq rpl (cdr as))
11832 ((symbolp rpl) (funcall rpl tag))
11833 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
11834 (t (concat rpl tag)))))
11837 ;;; Storing and inserting links
11839 (defvar org-insert-link-history nil
11840 "Minibuffer history for links inserted with `org-insert-link'.")
11842 (defvar org-stored-links nil
11843 "Contains the links stored with `org-store-link'.")
11845 (defvar org-store-link-plist nil
11846 "Plist with info about the most recently link created with `org-store-link'.")
11848 (defvar org-link-protocols nil
11849 "Link protocols added to Org-mode using `org-add-link-type'.")
11851 (defvar org-store-link-functions nil
11852 "List of functions that are called to create and store a link.
11853 Each function will be called in turn until one returns a non-nil
11854 value. Each function should check if it is responsible for creating
11855 this link (for example by looking at the major mode).
11856 If not, it must exit and return nil.
11857 If yes, it should return a non-nil value after a calling
11858 `org-store-link-props' with a list of properties and values.
11859 Special properties are:
11861 :type The link prefix. like \"http\". This must be given.
11862 :link The link, like \"http://www.astro.uva.nl/~dominik\".
11863 This is obligatory as well.
11864 :description Optional default description for the second pair
11865 of brackets in an Org-mode link. The user can still change
11866 this when inserting this link into an Org-mode buffer.
11868 In addition to these, any additional properties can be specified
11869 and then used in remember templates.")
11871 (defun org-add-link-type (type &optional follow publish)
11872 "Add TYPE to the list of `org-link-types'.
11873 Re-compute all regular expressions depending on `org-link-types'
11874 FOLLOW and PUBLISH are two functions. Both take the link path as
11876 FOLLOW should do whatever is necessary to follow the link, for example
11877 to find a file or display a mail message.
11879 PUBLISH takes the path and retuns the string that should be used when
11880 this document is published. FIMXE: This is actually not yet implemented."
11881 (add-to-list 'org-link-types type t)
11882 (org-make-link-regexps)
11883 (add-to-list 'org-link-protocols
11884 (list type follow publish)))
11886 (defun org-add-agenda-custom-command (entry)
11887 "Replace or add a command in `org-agenda-custom-commands'.
11888 This is mostly for hacking and trying a new command - once the command
11889 works you probably want to add it to `org-agenda-custom-commands' for good."
11890 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
11892 (setcdr ass (cdr entry))
11893 (push entry org-agenda-custom-commands))))
11896 (defun org-store-link (arg)
11897 "\\<org-mode-map>Store an org-link to the current location.
11898 This link is added to `org-stored-links' and can later be inserted
11899 into an org-buffer with \\[org-insert-link].
11901 For some link types, a prefix arg is interpreted:
11902 For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
11903 For file links, arg negates `org-context-in-file-links'."
11905 (setq org-store-link-plist nil) ; reset
11906 (let (link cpltxt desc description search txt)
11909 ((run-hook-with-args-until-success 'org-store-link-functions)
11910 (setq link (plist-get org-store-link-plist :link)
11911 desc (or (plist-get org-store-link-plist :description) link)))
11913 ((eq major-mode 'bbdb-mode)
11914 (let ((name (bbdb-record-name (bbdb-current-record)))
11915 (company (bbdb-record-getprop (bbdb-current-record) 'company)))
11916 (setq cpltxt (concat "bbdb:" (or name company))
11917 link (org-make-link cpltxt))
11918 (org-store-link-props :type "bbdb" :name name :company company)))
11920 ((eq major-mode 'Info-mode)
11921 (setq link (org-make-link "info:"
11922 (file-name-nondirectory Info-current-file)
11923 ":" Info-current-node))
11924 (setq cpltxt (concat (file-name-nondirectory Info-current-file)
11925 ":" Info-current-node))
11926 (org-store-link-props :type "info" :file Info-current-file
11927 :node Info-current-node))
11929 ((eq major-mode 'calendar-mode)
11930 (let ((cd (calendar-cursor-to-date)))
11932 (format-time-string
11933 (car org-time-stamp-formats)
11934 (apply 'encode-time
11935 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
11937 (org-store-link-props :type "calendar" :date cd)))
11939 ((or (eq major-mode 'vm-summary-mode)
11940 (eq major-mode 'vm-presentation-mode))
11941 (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
11942 (vm-follow-summary-cursor)
11944 (vm-select-folder-buffer)
11945 (let* ((message (car vm-message-pointer))
11946 (folder buffer-file-name)
11947 (subject (vm-su-subject message))
11948 (to (vm-get-header-contents message "To"))
11949 (from (vm-get-header-contents message "From"))
11950 (message-id (vm-su-message-id message)))
11951 (org-store-link-props :type "vm" :from from :to to :subject subject
11952 :message-id message-id)
11953 (setq message-id (org-remove-angle-brackets message-id))
11954 (setq folder (abbreviate-file-name folder))
11955 (if (string-match (concat "^" (regexp-quote vm-folder-directory))
11957 (setq folder (replace-match "" t t folder)))
11958 (setq cpltxt (org-email-link-description))
11959 (setq link (org-make-link "vm:" folder "#" message-id)))))
11961 ((eq major-mode 'wl-summary-mode)
11962 (let* ((msgnum (wl-summary-message-number))
11963 (message-id (elmo-message-field wl-summary-buffer-elmo-folder
11964 msgnum 'message-id))
11966 (if (fboundp 'elmo-message-entity)
11967 (elmo-message-entity
11968 wl-summary-buffer-elmo-folder msgnum)
11969 (elmo-msgdb-overview-get-entity
11970 msgnum (wl-summary-buffer-msgdb))))
11971 (from (wl-summary-line-from))
11972 (to (car (elmo-message-entity-field wl-message-entity 'to)))
11973 (subject (let (wl-thr-indent-string wl-parent-message-entity)
11974 (wl-summary-line-subject))))
11975 (org-store-link-props :type "wl" :from from :to to
11976 :subject subject :message-id message-id)
11977 (setq message-id (org-remove-angle-brackets message-id))
11978 (setq cpltxt (org-email-link-description))
11979 (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
11982 ((or (equal major-mode 'mh-folder-mode)
11983 (equal major-mode 'mh-show-mode))
11984 (let ((from (org-mhe-get-header "From:"))
11985 (to (org-mhe-get-header "To:"))
11986 (message-id (org-mhe-get-header "Message-Id:"))
11987 (subject (org-mhe-get-header "Subject:")))
11988 (org-store-link-props :type "mh" :from from :to to
11989 :subject subject :message-id message-id)
11990 (setq cpltxt (org-email-link-description))
11991 (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
11992 (org-remove-angle-brackets message-id)))))
11994 ((or (eq major-mode 'rmail-mode)
11995 (eq major-mode 'rmail-summary-mode))
11996 (save-window-excursion
11998 (when (eq major-mode 'rmail-summary-mode)
11999 (rmail-show-message rmail-current-message))
12000 (rmail-narrow-to-non-pruned-header)
12001 (let ((folder buffer-file-name)
12002 (message-id (mail-fetch-field "message-id"))
12003 (from (mail-fetch-field "from"))
12004 (to (mail-fetch-field "to"))
12005 (subject (mail-fetch-field "subject")))
12006 (org-store-link-props
12007 :type "rmail" :from from :to to
12008 :subject subject :message-id message-id)
12009 (setq message-id (org-remove-angle-brackets message-id))
12010 (setq cpltxt (org-email-link-description))
12011 (setq link (org-make-link "rmail:" folder "#" message-id)))
12012 (rmail-show-message rmail-current-message))))
12014 ((eq major-mode 'gnus-group-mode)
12015 (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
12016 (gnus-group-group-name)) ; version
12017 ((fboundp 'gnus-group-name)
12020 (unless group (error "Not on a group"))
12021 (org-store-link-props :type "gnus" :group group)
12022 (setq cpltxt (concat
12023 (if (org-xor arg org-usenet-links-prefer-google)
12024 "http://groups.google.com/groups?group="
12027 link (org-make-link cpltxt))))
12029 ((memq major-mode '(gnus-summary-mode gnus-article-mode))
12030 (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
12031 (let* ((group gnus-newsgroup-name)
12032 (article (gnus-summary-article-number))
12033 (header (gnus-summary-article-header article))
12034 (from (mail-header-from header))
12035 (message-id (mail-header-id header))
12036 (date (mail-header-date header))
12037 (subject (gnus-summary-subject-string)))
12038 (org-store-link-props :type "gnus" :from from :subject subject
12039 :message-id message-id :group group)
12040 (setq cpltxt (org-email-link-description))
12041 (if (org-xor arg org-usenet-links-prefer-google)
12045 (format "http://groups.google.com/groups?as_umsgid=%s"
12046 (org-fixup-message-id-for-http message-id))))
12047 (setq link (org-make-link "gnus:" group
12048 "#" (number-to-string article))))))
12050 ((eq major-mode 'w3-mode)
12051 (setq cpltxt (url-view-url t)
12052 link (org-make-link cpltxt))
12053 (org-store-link-props :type "w3" :url (url-view-url t)))
12055 ((eq major-mode 'w3m-mode)
12056 (setq cpltxt (or w3m-current-title w3m-current-url)
12057 link (org-make-link w3m-current-url))
12058 (org-store-link-props :type "w3m" :url (url-view-url t)))
12060 ((setq search (run-hook-with-args-until-success
12061 'org-create-file-search-functions))
12062 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
12064 (setq cpltxt (or description link)))
12066 ((eq major-mode 'image-mode)
12067 (setq cpltxt (concat "file:"
12068 (abbreviate-file-name buffer-file-name))
12069 link (org-make-link cpltxt))
12070 (org-store-link-props :type "image" :file buffer-file-name))
12072 ((eq major-mode 'dired-mode)
12073 ;; link to the file in the current line
12074 (setq cpltxt (concat "file:"
12075 (abbreviate-file-name
12077 (dired-get-filename nil t))))
12078 link (org-make-link cpltxt)))
12080 ((and buffer-file-name (org-mode-p))
12081 ;; Just link to current headline
12082 (setq cpltxt (concat "file:"
12083 (abbreviate-file-name buffer-file-name)))
12084 ;; Add a context search string
12085 (when (org-xor org-context-in-file-links arg)
12086 ;; Check if we are on a target
12087 (if (org-in-regexp "<<\\(.*?\\)>>")
12088 (setq cpltxt (concat cpltxt "::" (match-string 1)))
12090 ((org-on-heading-p) nil)
12091 ((org-region-active-p)
12092 (buffer-substring (region-beginning) (region-end)))
12093 (t (buffer-substring (point-at-bol) (point-at-eol)))))
12094 (when (or (null txt) (string-match "\\S-" txt))
12096 (concat cpltxt "::" (org-make-org-heading-search-string txt))
12098 (if (string-match "::\\'" cpltxt)
12099 (setq cpltxt (substring cpltxt 0 -2)))
12100 (setq link (org-make-link cpltxt)))
12102 ((buffer-file-name (buffer-base-buffer))
12103 ;; Just link to this file here.
12104 (setq cpltxt (concat "file:"
12105 (abbreviate-file-name
12106 (buffer-file-name (buffer-base-buffer)))))
12107 ;; Add a context string
12108 (when (org-xor org-context-in-file-links arg)
12109 (setq txt (if (org-region-active-p)
12110 (buffer-substring (region-beginning) (region-end))
12111 (buffer-substring (point-at-bol) (point-at-eol))))
12112 ;; Only use search option if there is some text.
12113 (when (string-match "\\S-" txt)
12115 (concat cpltxt "::" (org-make-org-heading-search-string txt))
12117 (setq link (org-make-link cpltxt)))
12120 (error "Cannot link to a buffer which is not visiting a file"))
12122 (t (setq link nil)))
12124 (if (consp link) (setq cpltxt (car link) link (cdr link)))
12125 (setq link (or link cpltxt)
12126 desc (or desc cpltxt))
12127 (if (equal desc "NONE") (setq desc nil))
12129 (if (and (interactive-p) link)
12131 (setq org-stored-links
12132 (cons (list link desc) org-stored-links))
12133 (message "Stored: %s" (or desc link)))
12134 (and link (org-make-link-string link desc)))))
12136 (defun org-store-link-props (&rest plist)
12137 "Store link properties, extract names and addresses."
12139 (when (setq x (plist-get plist :from))
12140 (setq adr (mail-extract-address-components x))
12141 (plist-put plist :fromname (car adr))
12142 (plist-put plist :fromaddress (nth 1 adr)))
12143 (when (setq x (plist-get plist :to))
12144 (setq adr (mail-extract-address-components x))
12145 (plist-put plist :toname (car adr))
12146 (plist-put plist :toaddress (nth 1 adr))))
12147 (let ((from (plist-get plist :from))
12148 (to (plist-get plist :to)))
12149 (when (and from to org-from-is-user-regexp)
12150 (plist-put plist :fromto
12151 (if (string-match org-from-is-user-regexp from)
12153 (concat "from %f")))))
12154 (setq org-store-link-plist plist))
12156 (defun org-email-link-description (&optional fmt)
12157 "Return the description part of an email link.
12158 This takes information from `org-store-link-plist' and formats it
12159 according to FMT (default from `org-email-link-description-format')."
12160 (setq fmt (or fmt org-email-link-description-format))
12161 (let* ((p org-store-link-plist)
12162 (to (plist-get p :toaddress))
12163 (from (plist-get p :fromaddress))
12166 (cons "%c" (plist-get p :fromto))
12167 (cons "%F" (plist-get p :from))
12168 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
12169 (cons "%T" (plist-get p :to))
12170 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
12171 (cons "%s" (plist-get p :subject))
12172 (cons "%m" (plist-get p :message-id)))))
12173 (when (string-match "%c" fmt)
12174 ;; Check if the user wrote this message
12175 (if (and org-from-is-user-regexp from to
12176 (save-match-data (string-match org-from-is-user-regexp from)))
12177 (setq fmt (replace-match "to %t" t t fmt))
12178 (setq fmt (replace-match "from %f" t t fmt))))
12179 (org-replace-escapes fmt table)))
12181 (defun org-make-org-heading-search-string (&optional string heading)
12182 "Make search string for STRING or current headline."
12184 (let ((s (or string (org-get-heading))))
12185 (unless (and string (not heading))
12186 ;; We are using a headline, clean up garbage in there.
12187 (if (string-match org-todo-regexp s)
12188 (setq s (replace-match "" t t s)))
12189 (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
12190 (setq s (replace-match "" t t s)))
12191 (setq s (org-trim s))
12192 (if (string-match (concat "^\\(" org-quote-string "\\|"
12193 org-comment-string "\\)") s)
12194 (setq s (replace-match "" t t s)))
12195 (while (string-match org-ts-regexp s)
12196 (setq s (replace-match "" t t s))))
12197 (while (string-match "[^a-zA-Z_0-9 \t]+" s)
12198 (setq s (replace-match " " t t s)))
12199 (or string (setq s (concat "*" s))) ; Add * for headlines
12200 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
12202 (defun org-make-link (&rest strings)
12203 "Concatenate STRINGS."
12204 (apply 'concat strings))
12206 (defun org-make-link-string (link &optional description)
12207 "Make a link with brackets, consisting of LINK and DESCRIPTION."
12208 (unless (string-match "\\S-" link)
12209 (error "Empty link"))
12210 (when (stringp description)
12211 ;; Remove brackets from the description, they are fatal.
12212 (while (string-match "\\[" description)
12213 (setq description (replace-match "{" t t description)))
12214 (while (string-match "\\]" description)
12215 (setq description (replace-match "}" t t description))))
12216 (when (equal (org-link-escape link) description)
12217 ;; No description needed, it is identical
12218 (setq description nil))
12219 (when (and (not description)
12220 (not (equal link (org-link-escape link))))
12221 (setq description link))
12222 (concat "[[" (org-link-escape link) "]"
12223 (if description (concat "[" description "]") "")
12226 (defconst org-link-escape-chars
12230 (?\340 . "%E0") ; `a
12231 (?\342 . "%E2") ; ^a
12232 (?\347 . "%E7") ; ,c
12233 (?\350 . "%E8") ; `e
12234 (?\351 . "%E9") ; 'e
12235 (?\352 . "%EA") ; ^e
12236 (?\356 . "%EE") ; ^i
12237 (?\364 . "%F4") ; ^o
12238 (?\371 . "%F9") ; `u
12239 (?\373 . "%FB") ; ^u
12245 "Association list of escapes for some characters problematic in links.
12246 This is the list that is used for internal purposes.")
12248 (defconst org-link-escape-chars-browser
12249 '((?\ . "%20")) ; 32 for the SPC char
12250 "Association list of escapes for some characters problematic in links.
12251 This is the list that is used before handing over to the browser.")
12253 (defun org-link-escape (text &optional table)
12254 "Escape charaters in TEXT that are problematic for links."
12255 (setq table (or table org-link-escape-chars))
12257 (let ((re (mapconcat (lambda (x) (regexp-quote
12258 (char-to-string (car x))))
12260 (while (string-match re text)
12263 (cdr (assoc (string-to-char (match-string 0 text))
12268 (defun org-link-unescape (text &optional table)
12269 "Reverse the action of `org-link-escape'."
12270 (setq table (or table org-link-escape-chars))
12272 (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
12274 (while (string-match re text)
12277 (char-to-string (car (rassoc (match-string 0 text) table)))
12281 (defun org-xor (a b)
12285 (defun org-get-header (header)
12286 "Find a header field in the current buffer."
12288 (goto-char (point-min))
12289 (let ((case-fold-search t) s)
12292 (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
12293 (setq s (match-string 1)))
12294 (while (string-match "\"" s)
12295 (setq s (replace-match "" t t s)))
12296 (if (string-match "[<(].*" s)
12297 (setq s (replace-match "" t t s))))
12298 ((eq header 'message-id)
12299 (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
12300 (setq s (match-string 1))))
12301 ((eq header 'subject)
12302 (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
12303 (setq s (match-string 1)))))
12304 (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
12305 (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
12309 (defun org-fixup-message-id-for-http (s)
12310 "Replace special characters in a message id, so it can be used in an http query."
12311 (while (string-match "<" s)
12312 (setq s (replace-match "%3C" t t s)))
12313 (while (string-match ">" s)
12314 (setq s (replace-match "%3E" t t s)))
12315 (while (string-match "@" s)
12316 (setq s (replace-match "%40" t t s)))
12320 (defun org-insert-link-global ()
12321 "Insert a link like Org-mode does.
12322 This command can be called in any mode to insert a link in Org-mode syntax."
12324 (org-run-like-in-org-mode 'org-insert-link))
12326 (defun org-insert-link (&optional complete-file)
12327 "Insert a link. At the prompt, enter the link.
12329 Completion can be used to select a link previously stored with
12330 `org-store-link'. When the empty string is entered (i.e. if you just
12331 press RET at the prompt), the link defaults to the most recently
12332 stored link. As SPC triggers completion in the minibuffer, you need to
12333 use M-SPC or C-q SPC to force the insertion of a space character.
12335 You will also be prompted for a description, and if one is given, it will
12336 be displayed in the buffer instead of the link.
12338 If there is already a link at point, this command will allow you to edit link
12339 and description parts.
12341 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
12342 selected using completion. The path to the file will be relative to
12343 the current directory if the file is in the current directory or a
12344 subdirectory. Otherwise, the link will be the absolute path as
12345 completed in the minibuffer (i.e. normally ~/path/to/file).
12347 With two \\[universal-argument] prefixes, enforce an absolute path even if the file
12348 is in the current directory or below.
12349 With three \\[universal-argument] prefixes, negate the meaning of
12350 `org-keep-stored-link-after-insertion'."
12352 (let* ((wcf (current-window-configuration))
12353 (region (if (org-region-active-p)
12354 (buffer-substring (region-beginning) (region-end))))
12355 (remove (and region (list (region-beginning) (region-end))))
12357 tmphist ; byte-compile incorrectly complains about this
12360 ((org-in-regexp org-bracket-link-regexp 1)
12361 ;; We do have a link at point, and we are going to edit it.
12362 (setq remove (list (match-beginning 0) (match-end 0)))
12363 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
12364 (setq link (read-string "Link: "
12366 (org-match-string-no-properties 1)))))
12367 ((or (org-in-regexp org-angle-link-re)
12368 (org-in-regexp org-plain-link-re))
12369 ;; Convert to bracket link
12370 (setq remove (list (match-beginning 0) (match-end 0))
12371 link (read-string "Link: "
12372 (org-remove-angle-brackets (match-string 0)))))
12373 ((equal complete-file '(4))
12374 ;; Completing read for file names.
12375 (setq file (read-file-name "File: "))
12376 (let ((pwd (file-name-as-directory (expand-file-name ".")))
12377 (pwd1 (file-name-as-directory (abbreviate-file-name
12378 (expand-file-name ".")))))
12380 ((equal complete-file '(16))
12381 (setq link (org-make-link
12383 (abbreviate-file-name (expand-file-name file)))))
12384 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
12385 (setq link (org-make-link "file:" (match-string 1 file))))
12386 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
12387 (expand-file-name file))
12388 (setq link (org-make-link
12389 "file:" (match-string 1 (expand-file-name file)))))
12390 (t (setq link (org-make-link "file:" file))))))
12392 ;; Read link, with completion for stored links.
12393 (with-output-to-temp-buffer "*Org Links*"
12394 (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
12395 (when org-stored-links
12396 (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
12399 (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
12400 (reverse org-stored-links) "\n"))))
12401 (let ((cw (selected-window)))
12402 (select-window (get-buffer-window "*Org Links*"))
12403 (shrink-window-if-larger-than-buffer)
12404 (setq truncate-lines t)
12405 (select-window cw))
12406 ;; Fake a link history, containing the stored links.
12407 (setq tmphist (append (mapcar 'car org-stored-links)
12408 org-insert-link-history))
12410 (setq link (org-completing-read
12413 (mapcar (lambda (x) (list (concat (car x) ":")))
12414 (append org-link-abbrev-alist-local org-link-abbrev-alist))
12415 (mapcar (lambda (x) (list (concat x ":")))
12419 (or (car (car org-stored-links)))))
12420 (set-window-configuration wcf)
12421 (kill-buffer "*Org Links*"))
12422 (setq entry (assoc link org-stored-links))
12423 (or entry (push link org-insert-link-history))
12424 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
12425 (not org-keep-stored-link-after-insertion))
12426 (setq org-stored-links (delq (assoc link org-stored-links)
12427 org-stored-links)))
12428 (setq desc (or desc (nth 1 entry)))))
12430 (if (string-match org-plain-link-re link)
12431 ;; URL-like link, normalize the use of angular brackets.
12432 (setq link (org-make-link (org-remove-angle-brackets link))))
12434 ;; Check if we are linking to the current file with a search option
12435 ;; If yes, simplify the link by using only the search option.
12436 (when (and buffer-file-name
12437 (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
12438 (let* ((path (match-string 1 link))
12439 (case-fold-search nil)
12440 (search (match-string 2 link)))
12442 (if (equal (file-truename buffer-file-name) (file-truename path))
12443 ;; We are linking to this same file, with a search option
12444 (setq link search)))))
12446 ;; Check if we can/should use a relative path. If yes, simplify the link
12447 (when (string-match "\\<file:\\(.*\\)" link)
12448 (let* ((path (match-string 1 link))
12450 (desc-is-link (equal link desc))
12451 (case-fold-search nil))
12453 ((eq org-link-file-path-type 'absolute)
12454 (setq path (abbreviate-file-name (expand-file-name path))))
12455 ((eq org-link-file-path-type 'noabbrev)
12456 (setq path (expand-file-name path)))
12457 ((eq org-link-file-path-type 'relative)
12458 (setq path (file-relative-name path)))
12461 (if (string-match (concat "^" (regexp-quote
12462 (file-name-as-directory
12463 (expand-file-name "."))))
12464 (expand-file-name path))
12465 ;; We are linking a file with relative path name.
12466 (setq path (substring (expand-file-name path)
12467 (match-end 0)))))))
12468 (setq link (concat "file:" path))
12469 (if (equal desc origpath)
12470 (setq desc path))))
12472 (setq desc (read-string "Description: " desc))
12473 (unless (string-match "\\S-" desc) (setq desc nil))
12474 (if remove (apply 'delete-region remove))
12475 (insert (org-make-link-string link desc))))
12477 (defun org-completing-read (&rest args)
12478 (let ((minibuffer-local-completion-map
12479 (copy-keymap minibuffer-local-completion-map)))
12480 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
12481 (apply 'completing-read args)))
12483 ;;; Opening/following a link
12484 (defvar org-link-search-failed nil)
12486 (defun org-next-link ()
12487 "Move forward to the next link.
12488 If the link is in hidden text, expose it."
12490 (when (and org-link-search-failed (eq this-command last-command))
12491 (goto-char (point-min))
12492 (message "Link search wrapped back to beginning of buffer"))
12493 (setq org-link-search-failed nil)
12494 (let* ((pos (point))
12496 (a (assoc :link ct)))
12497 (if a (goto-char (nth 2 a)))
12498 (if (re-search-forward org-any-link-re nil t)
12500 (goto-char (match-beginning 0))
12501 (if (org-invisible-p) (org-show-context)))
12503 (setq org-link-search-failed t)
12504 (error "No further link found"))))
12506 (defun org-previous-link ()
12507 "Move backward to the previous link.
12508 If the link is in hidden text, expose it."
12510 (when (and org-link-search-failed (eq this-command last-command))
12511 (goto-char (point-max))
12512 (message "Link search wrapped back to end of buffer"))
12513 (setq org-link-search-failed nil)
12514 (let* ((pos (point))
12516 (a (assoc :link ct)))
12517 (if a (goto-char (nth 1 a)))
12518 (if (re-search-backward org-any-link-re nil t)
12520 (goto-char (match-beginning 0))
12521 (if (org-invisible-p) (org-show-context)))
12523 (setq org-link-search-failed t)
12524 (error "No further link found"))))
12526 (defun org-find-file-at-mouse (ev)
12527 "Open file link or URL at mouse."
12529 (mouse-set-point ev)
12530 (org-open-at-point 'in-emacs))
12532 (defun org-open-at-mouse (ev)
12533 "Open file link or URL at mouse."
12535 (mouse-set-point ev)
12536 (org-open-at-point))
12538 (defvar org-window-config-before-follow-link nil
12539 "The window configuration before following a link.
12540 This is saved in case the need arises to restore it.")
12542 (defvar org-open-link-marker (make-marker)
12543 "Marker pointing to the location where `org-open-at-point; was called.")
12546 (defun org-open-at-point-global ()
12547 "Follow a link like Org-mode does.
12548 This command can be called in any mode to follow a link that has
12551 (org-run-like-in-org-mode 'org-open-at-point))
12553 (defun org-open-at-point (&optional in-emacs)
12554 "Open link at or after point.
12555 If there is no link at point, this function will search forward up to
12556 the end of the current subtree.
12557 Normally, files will be opened by an appropriate application. If the
12558 optional argument IN-EMACS is non-nil, Emacs will visit the file."
12561 (move-marker org-open-link-marker (point))
12562 (setq org-window-config-before-follow-link (current-window-configuration))
12563 (org-remove-occur-highlights nil nil t)
12564 (if (org-at-timestamp-p t)
12565 (org-follow-timestamp-link)
12566 (let (type path link line search (pos (point)))
12569 (skip-chars-forward "^]\n\r")
12570 (when (org-in-regexp org-bracket-link-regexp)
12571 (setq link (org-link-unescape (org-match-string-no-properties 1)))
12572 (while (string-match " *\n *" link)
12573 (setq link (replace-match " " t t link)))
12574 (setq link (org-link-expand-abbrev link))
12575 (if (string-match org-link-re-with-space2 link)
12576 (setq type (match-string 1 link) path (match-string 2 link))
12577 (setq type "thisfile" path link))
12580 (when (get-text-property (point) 'org-linked-text)
12581 (setq type "thisfile"
12582 pos (if (get-text-property (1+ (point)) 'org-linked-text)
12583 (1+ (point)) (point))
12584 path (buffer-substring
12585 (previous-single-property-change pos 'org-linked-text)
12586 (next-single-property-change pos 'org-linked-text)))
12590 (when (or (org-in-regexp org-angle-link-re)
12591 (org-in-regexp org-plain-link-re))
12592 (setq type (match-string 1) path (match-string 2))
12594 (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
12595 (setq type "tree-match"
12596 path (match-string 1))
12599 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
12601 path (match-string 1))
12602 (while (string-match ":" path)
12603 (setq path (replace-match "+" t t path)))
12604 (throw 'match t))))
12606 (error "No link found"))
12607 ;; Remove any trailing spaces in path
12608 (if (string-match " +\\'" path)
12609 (setq path (replace-match "" t t path)))
12613 ((assoc type org-link-protocols)
12614 (funcall (nth 1 (assoc type org-link-protocols)) path))
12616 ((equal type "mailto")
12617 (let ((cmd (car org-link-mailto-program))
12618 (args (cdr org-link-mailto-program)) args1
12619 (address path) (subject "") a)
12620 (if (string-match "\\(.*\\)::\\(.*\\)" path)
12621 (setq address (match-string 1 path)
12622 subject (org-link-escape (match-string 2 path))))
12625 ((not (stringp (car args))) (push (pop args) args1))
12626 (t (setq a (pop args))
12627 (if (string-match "%a" a)
12628 (setq a (replace-match address t t a)))
12629 (if (string-match "%s" a)
12630 (setq a (replace-match subject t t a)))
12632 (apply cmd (nreverse args1))))
12634 ((member type '("http" "https" "ftp" "news"))
12635 (browse-url (concat type ":" (org-link-escape
12636 path org-link-escape-chars-browser))))
12638 ((member type '("message"))
12639 (browse-url (concat type ":" path)))
12641 ((string= type "tags")
12642 (org-tags-view in-emacs path))
12643 ((string= type "thisfile")
12645 (switch-to-buffer-other-window
12646 (org-get-buffer-for-internal-link (current-buffer)))
12647 (org-mark-ring-push))
12648 (let ((cmd `(org-link-search
12650 ,(cond ((equal in-emacs '(4)) 'occur)
12651 ((equal in-emacs '(16)) 'org-occur)
12654 (condition-case nil (eval cmd)
12655 (error (progn (widen) (eval cmd))))))
12657 ((string= type "tree-match")
12658 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
12660 ((string= type "file")
12661 (if (string-match "::\\([0-9]+\\)\\'" path)
12662 (setq line (string-to-number (match-string 1 path))
12663 path (substring path 0 (match-beginning 0)))
12664 (if (string-match "::\\(.+\\)\\'" path)
12665 (setq search (match-string 1 path)
12666 path (substring path 0 (match-beginning 0)))))
12667 (if (string-match "[*?{]" (file-name-nondirectory path))
12669 (org-open-file path in-emacs line search)))
12671 ((string= type "news")
12672 (org-follow-gnus-link path))
12674 ((string= type "bbdb")
12675 (org-follow-bbdb-link path))
12677 ((string= type "info")
12678 (org-follow-info-link path))
12680 ((string= type "gnus")
12681 (let (group article)
12682 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
12683 (error "Error in Gnus link"))
12684 (setq group (match-string 1 path)
12685 article (match-string 3 path))
12686 (org-follow-gnus-link group article)))
12688 ((string= type "vm")
12689 (let (folder article)
12690 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
12691 (error "Error in VM link"))
12692 (setq folder (match-string 1 path)
12693 article (match-string 3 path))
12694 ;; in-emacs is the prefix arg, will be interpreted as read-only
12695 (org-follow-vm-link folder article in-emacs)))
12697 ((string= type "wl")
12698 (let (folder article)
12699 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
12700 (error "Error in Wanderlust link"))
12701 (setq folder (match-string 1 path)
12702 article (match-string 3 path))
12703 (org-follow-wl-link folder article)))
12705 ((string= type "mhe")
12706 (let (folder article)
12707 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
12708 (error "Error in MHE link"))
12709 (setq folder (match-string 1 path)
12710 article (match-string 3 path))
12711 (org-follow-mhe-link folder article)))
12713 ((string= type "rmail")
12714 (let (folder article)
12715 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
12716 (error "Error in RMAIL link"))
12717 (setq folder (match-string 1 path)
12718 article (match-string 3 path))
12719 (org-follow-rmail-link folder article)))
12721 ((string= type "shell")
12723 (if (or (not org-confirm-shell-link-function)
12724 (funcall org-confirm-shell-link-function
12725 (format "Execute \"%s\" in shell? "
12726 (org-add-props cmd nil
12727 'face 'org-warning))))
12729 (message "Executing %s" cmd)
12730 (shell-command cmd))
12733 ((string= type "elisp")
12735 (if (or (not org-confirm-elisp-link-function)
12736 (funcall org-confirm-elisp-link-function
12737 (format "Execute \"%s\" as elisp? "
12738 (org-add-props cmd nil
12739 'face 'org-warning))))
12740 (message "%s => %s" cmd (eval (read cmd)))
12744 (browse-url-at-point)))))
12745 (move-marker org-open-link-marker nil)))
12749 (defvar org-create-file-search-functions nil
12750 "List of functions to construct the right search string for a file link.
12751 These functions are called in turn with point at the location to
12752 which the link should point.
12754 A function in the hook should first test if it would like to
12755 handle this file type, for example by checking the major-mode or
12756 the file extension. If it decides not to handle this file, it
12757 should just return nil to give other functions a chance. If it
12758 does handle the file, it must return the search string to be used
12759 when following the link. The search string will be part of the
12760 file link, given after a double colon, and `org-open-at-point'
12761 will automatically search for it. If special measures must be
12762 taken to make the search successful, another function should be
12763 added to the companion hook `org-execute-file-search-functions',
12766 A function in this hook may also use `setq' to set the variable
12767 `description' to provide a suggestion for the descriptive text to
12768 be used for this link when it gets inserted into an Org-mode
12769 buffer with \\[org-insert-link].")
12771 (defvar org-execute-file-search-functions nil
12772 "List of functions to execute a file search triggered by a link.
12774 Functions added to this hook must accept a single argument, the
12775 search string that was part of the file link, the part after the
12776 double colon. The function must first check if it would like to
12777 handle this search, for example by checking the major-mode or the
12778 file extension. If it decides not to handle this search, it
12779 should just return nil to give other functions a chance. If it
12780 does handle the search, it must return a non-nil value to keep
12781 other functions from trying.
12783 Each function can access the current prefix argument through the
12784 variable `current-prefix-argument'. Note that a single prefix is
12785 used to force opening a link in Emacs, so it may be good to only
12786 use a numeric or double prefix to guide the search function.
12788 In case this is needed, a function in this hook can also restore
12789 the window configuration before `org-open-at-point' was called using:
12791 (set-window-configuration org-window-config-before-follow-link)")
12793 (defun org-link-search (s &optional type avoid-pos)
12794 "Search for a link search option.
12795 If S is surrounded by forward slashes, it is interpreted as a
12796 regular expression. In org-mode files, this will create an `org-occur'
12797 sparse tree. In ordinary files, `occur' will be used to list matches.
12798 If the current buffer is in `dired-mode', grep will be used to search
12799 in all files. If AVOID-POS is given, ignore matches near that position."
12800 (let ((case-fold-search t)
12801 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
12802 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
12803 (append '(("") (" ") ("\t") ("\n"))
12804 org-emphasis-alist)
12808 words re0 re1 re2 re3 re4 re5 re2a reall)
12810 ;; First check if there are any special
12811 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
12812 ;; Now try the builtin stuff
12814 (goto-char (point-min))
12817 (concat "<<" (regexp-quote s0) ">>") nil t)
12818 (setq pos (match-beginning 0))))
12819 ;; There is an exact target for this
12821 ((string-match "^/\\(.*\\)/$" s)
12822 ;; A regular expression
12825 (org-occur (match-string 1 s)))
12826 ;;((eq major-mode 'dired-mode)
12827 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
12828 (t (org-do-occur (match-string 1 s)))))
12830 ;; A normal search strings
12831 (when (equal (string-to-char s) ?*)
12832 ;; Anchor on headlines, post may include tags.
12833 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
12834 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
12835 s (substring s 1)))
12836 (remove-text-properties
12838 '(face nil mouse-face nil keymap nil fontified nil) s)
12839 ;; Make a series of regular expressions to find a match
12840 (setq words (org-split-string s "[ \n\r\t]+")
12841 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
12842 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
12844 re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
12845 re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
12846 re1 (concat pre re2 post)
12847 re3 (concat pre re4 post)
12848 re5 (concat pre ".*" re4)
12849 re2 (concat pre re2)
12850 re2a (concat pre re2a)
12851 re4 (concat pre re4)
12852 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
12853 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
12857 ((eq type 'org-occur) (org-occur reall))
12858 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
12859 (t (goto-char (point-min))
12860 (if (or (org-search-not-self 1 re0 nil t)
12861 (org-search-not-self 1 re1 nil t)
12862 (org-search-not-self 1 re2 nil t)
12863 (org-search-not-self 1 re2a nil t)
12864 (org-search-not-self 1 re3 nil t)
12865 (org-search-not-self 1 re4 nil t)
12866 (org-search-not-self 1 re5 nil t)
12868 (goto-char (match-beginning 1))
12870 (error "No match")))))
12872 ;; Normal string-search
12873 (goto-char (point-min))
12874 (if (search-forward s nil t)
12875 (goto-char (match-beginning 0))
12876 (error "No match"))))
12877 (and (org-mode-p) (org-show-context 'link-search))))
12879 (defun org-search-not-self (group &rest args)
12880 "Execute `re-search-forward', but only accept matches that do not
12881 enclose the position of `org-open-link-marker'."
12882 (let ((m org-open-link-marker))
12884 (while (apply 're-search-forward args)
12885 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
12886 (goto-char (match-end group))
12887 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
12888 (> (match-beginning 0) (marker-position m))
12889 (< (match-end 0) (marker-position m)))
12891 (or (not (org-in-regexp
12892 org-bracket-link-analytic-regexp 1))
12893 (not (match-end 4)) ; no description
12894 (and (<= (match-beginning 4) (point))
12895 (>= (match-end 4) (point))))))
12896 (throw 'exit (point))))))))
12898 (defun org-get-buffer-for-internal-link (buffer)
12899 "Return a buffer to be used for displaying the link target of internal links."
12901 ((not org-display-internal-link-with-indirect-buffer)
12903 ((string-match "(Clone)$" (buffer-name buffer))
12904 (message "Buffer is already a clone, not making another one")
12905 ;; we also do not modify visibility in this case
12907 (t ; make a new indirect buffer for displaying the link
12908 (let* ((bn (buffer-name buffer))
12909 (ibn (concat bn "(Clone)"))
12910 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
12911 (with-current-buffer ib (org-overview))
12914 (defun org-do-occur (regexp &optional cleanup)
12915 "Call the Emacs command `occur'.
12916 If CLEANUP is non-nil, remove the printout of the regular expression
12917 in the *Occur* buffer. This is useful if the regex is long and not useful
12921 (let ((cwin (selected-window)) win beg end)
12922 (when (setq win (get-buffer-window "*Occur*"))
12923 (select-window win))
12924 (goto-char (point-min))
12925 (when (re-search-forward "match[a-z]+" nil t)
12926 (setq beg (match-end 0))
12927 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
12928 (setq end (1- (match-beginning 0)))))
12929 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
12930 (goto-char (point-min))
12931 (select-window cwin))))
12933 ;;; The mark ring for links jumps
12935 (defvar org-mark-ring nil
12936 "Mark ring for positions before jumps in Org-mode.")
12937 (defvar org-mark-ring-last-goto nil
12938 "Last position in the mark ring used to go back.")
12939 ;; Fill and close the ring
12940 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
12941 (loop for i from 1 to org-mark-ring-length do
12942 (push (make-marker) org-mark-ring))
12943 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
12946 (defun org-mark-ring-push (&optional pos buffer)
12947 "Put the current position or POS into the mark ring and rotate it."
12949 (setq pos (or pos (point)))
12950 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
12951 (move-marker (car org-mark-ring)
12953 (or buffer (current-buffer)))
12955 (substitute-command-keys
12956 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
12958 (defun org-mark-ring-goto (&optional n)
12959 "Jump to the previous position in the mark ring.
12960 With prefix arg N, jump back that many stored positions. When
12961 called several times in succession, walk through the entire ring.
12962 Org-mode commands jumping to a different position in the current file,
12963 or to another Org-mode file, automatically push the old position
12967 (if (eq last-command this-command)
12968 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
12969 (setq p org-mark-ring))
12970 (setq org-mark-ring-last-goto p)
12972 (switch-to-buffer (marker-buffer m))
12974 (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
12976 (defun org-remove-angle-brackets (s)
12977 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
12978 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
12980 (defun org-add-angle-brackets (s)
12981 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
12982 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
12985 ;;; Following specific links
12987 (defun org-follow-timestamp-link ()
12989 ((org-at-date-range-p t)
12990 (let ((org-agenda-start-on-weekday)
12991 (t1 (match-string 1))
12992 (t2 (match-string 2)))
12993 (setq t1 (time-to-days (org-time-string-to-time t1))
12994 t2 (time-to-days (org-time-string-to-time t2)))
12995 (org-agenda-list nil t1 (1+ (- t2 t1)))))
12996 ((org-at-timestamp-p t)
12997 (org-agenda-list nil (time-to-days (org-time-string-to-time
12998 (substring (match-string 1) 0 10)))
13000 (t (error "This should not happen"))))
13003 (defun org-follow-bbdb-link (name)
13004 "Follow a BBDB link to NAME."
13006 (let ((inhibit-redisplay (not debug-on-error))
13007 (bbdb-electric-p nil))
13009 ;; Exact match on name
13010 (bbdb-name (concat "\\`" name "\\'") nil)
13011 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
13012 ;; Exact match on name
13013 (bbdb-company (concat "\\`" name "\\'") nil)
13014 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
13015 ;; Partial match on name
13016 (bbdb-name name nil)
13017 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
13018 ;; Partial match on company
13019 (bbdb-company name nil)
13020 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
13021 ;; General match including network address and notes
13023 (when (= 0 (buffer-size (get-buffer "*BBDB*")))
13024 (delete-window (get-buffer-window "*BBDB*"))
13025 (error "No matching BBDB record")))))
13027 (defun org-follow-info-link (name)
13028 "Follow an info file & node link to NAME."
13029 (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
13030 (string-match "\\(.*\\)" name))
13033 (if (match-string 2 name) ; If there isn't a node, choose "Top"
13034 (Info-find-node (match-string 1 name) (match-string 2 name))
13035 (Info-find-node (match-string 1 name) "Top")))
13036 (message "Could not open: %s" name)))
13038 (defun org-follow-gnus-link (&optional group article)
13039 "Follow a Gnus link to GROUP and ARTICLE."
13041 (funcall (cdr (assq 'gnus org-link-frame-setup)))
13042 (if gnus-other-frame-object (select-frame gnus-other-frame-object))
13043 (cond ((and group article)
13044 (gnus-group-read-group 1 nil group)
13045 (gnus-summary-goto-article (string-to-number article) nil t))
13046 (group (gnus-group-jump-to-group group))))
13048 (defun org-follow-vm-link (&optional folder article readonly)
13049 "Follow a VM link to FOLDER and ARTICLE."
13051 (setq article (org-add-angle-brackets article))
13052 (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
13053 ;; ange-ftp or efs or tramp access
13054 (let ((user (or (match-string 1 folder) (user-login-name)))
13055 (host (match-string 2 folder))
13056 (file (match-string 3 folder)))
13059 ;; use tramp to access the file
13060 (if (featurep 'xemacs)
13061 (setq folder (format "[%s@%s]%s" user host file))
13062 (setq folder (format "/%s@%s:%s" user host file))))
13064 ;; use ange-ftp or efs
13065 (require (if (featurep 'xemacs) 'efs 'ange-ftp))
13066 (setq folder (format "/%s@%s:%s" user host file))))))
13068 (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
13071 (vm-select-folder-buffer)
13073 (let ((case-fold-search t))
13074 (goto-char (point-min))
13075 (if (not (re-search-forward
13076 (concat "^" "message-id: *" (regexp-quote article))))
13077 (error "Could not find the specified message in this folder"))
13078 (vm-isearch-update)
13079 (vm-isearch-narrow)
13080 (vm-beginning-of-message)
13083 (defun org-follow-wl-link (folder article)
13084 "Follow a Wanderlust link to FOLDER and ARTICLE."
13085 (if (and (string= folder "%")
13087 (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
13088 ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
13089 ;; Thus, we recompose folder and article ids.
13090 (setq folder (format "%s#%s" folder (match-string 1 article))
13091 article (match-string 3 article)))
13092 (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
13093 (error "No such folder: %s" folder))
13094 (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
13096 (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
13097 (wl-summary-redisplay)))
13099 (defun org-follow-rmail-link (folder article)
13100 "Follow an RMAIL link to FOLDER and ARTICLE."
13101 (setq article (org-add-angle-brackets article))
13102 (let (message-number)
13104 (save-window-excursion
13105 (rmail (if (string= folder "RMAIL") rmail-file-name folder))
13106 (setq message-number
13109 (goto-char (point-max))
13110 (if (re-search-backward
13111 (concat "^Message-ID:\\s-+" (regexp-quote
13114 (rmail-what-message))))))
13117 (rmail (if (string= folder "RMAIL") rmail-file-name folder))
13118 (rmail-show-message message-number)
13120 (error "Message not found"))))
13122 ;;; mh-e integration based on planner-mode
13123 (defun org-mhe-get-message-real-folder ()
13124 "Return the name of the current message real folder, so if you use
13125 sequences, it will now work."
13128 (if (equal major-mode 'mh-folder-mode)
13130 ;; Refer to the show buffer
13131 mh-show-folder-buffer))
13133 (if (boundp 'mh-index-folder)
13134 (min (length mh-index-folder) (length folder))))
13136 ;; a simple test on mh-index-data does not work, because
13137 ;; mh-index-data is always nil in a show buffer.
13138 (if (and (boundp 'mh-index-folder)
13139 (string= mh-index-folder (substring folder 0 end-index)))
13140 (if (equal major-mode 'mh-show-mode)
13141 (save-window-excursion
13142 (let (pop-up-frames)
13143 (when (buffer-live-p (get-buffer folder))
13145 (pop-to-buffer folder)
13146 (org-mhe-get-message-folder-from-index)
13149 (org-mhe-get-message-folder-from-index)
13155 (defun org-mhe-get-message-folder-from-index ()
13156 "Returns the name of the message folder in a index folder buffer."
13158 (mh-index-previous-folder)
13159 (re-search-forward "^\\(+.*\\)$" nil t)
13160 (message "%s" (match-string 1))))
13162 (defun org-mhe-get-message-folder ()
13163 "Return the name of the current message folder. Be careful if you
13166 (if (equal major-mode 'mh-folder-mode)
13168 ;; Refer to the show buffer
13169 mh-show-folder-buffer)))
13171 (defun org-mhe-get-message-num ()
13172 "Return the number of the current message. Be careful if you
13175 (if (equal major-mode 'mh-folder-mode)
13176 (mh-get-msg-num nil)
13177 ;; Refer to the show buffer
13178 (mh-show-buffer-message-number))))
13180 (defun org-mhe-get-header (header)
13181 "Return a header of the message in folder mode. This will create a
13182 show buffer for the corresponding message. If you have a more clever
13184 (let* ((folder (org-mhe-get-message-folder))
13185 (num (org-mhe-get-message-num))
13186 (buffer (get-buffer-create (concat "show-" folder)))
13188 (with-current-buffer buffer
13189 (mh-display-msg num folder)
13190 (if (equal major-mode 'mh-folder-mode)
13191 (mh-header-display)
13192 (mh-show-header-display))
13193 (set-buffer buffer)
13194 (setq header-field (mh-get-header-field header))
13195 (if (equal major-mode 'mh-folder-mode)
13200 (defun org-follow-mhe-link (folder article)
13201 "Follow an MHE link to FOLDER and ARTICLE.
13202 If ARTICLE is nil FOLDER is shown. If the configuration variable
13203 `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
13204 ARTICLE is searched in all folders. Indexed searches (swish++,
13205 namazu, and others supported by MH-E) will always search in all
13208 (require 'mh-search)
13209 (require 'mh-utils)
13212 (mh-visit-folder (mh-normalize-folder-name folder))
13213 (setq article (org-add-angle-brackets article))
13215 (if (equal mh-searcher 'pick)
13217 (mh-search folder (list "--message-id" article))
13218 (when (and org-mhe-search-all-folders
13219 (not (org-mhe-get-message-real-folder)))
13221 (mh-search "+" (list "--message-id" article))))
13222 (mh-search "+" article))
13223 (if (org-mhe-get-message-real-folder)
13226 (error "Message not found"))))
13230 ;; Use the custom search meachnism to construct and use search strings for
13231 ;; file links to BibTeX database entries.
13233 (defun org-create-file-search-in-bibtex ()
13234 "Create the search string and description for a BibTeX database entry."
13235 (when (eq major-mode 'bibtex-mode)
13236 ;; yes, we want to construct this search string.
13237 ;; Make a good description for this entry, using names, year and the title
13238 ;; Put it into the `description' variable which is dynamically scoped.
13239 (let ((bibtex-autokey-names 1)
13240 (bibtex-autokey-names-stretch 1)
13241 (bibtex-autokey-name-case-convert-function 'identity)
13242 (bibtex-autokey-name-separator " & ")
13243 (bibtex-autokey-additional-names " et al.")
13244 (bibtex-autokey-year-length 4)
13245 (bibtex-autokey-name-year-separator " ")
13246 (bibtex-autokey-titlewords 3)
13247 (bibtex-autokey-titleword-separator " ")
13248 (bibtex-autokey-titleword-case-convert-function 'identity)
13249 (bibtex-autokey-titleword-length 'infty)
13250 (bibtex-autokey-year-title-separator ": "))
13251 (setq description (bibtex-generate-autokey)))
13252 ;; Now parse the entry, get the key and return it.
13254 (bibtex-beginning-of-entry)
13255 (cdr (assoc "=key=" (bibtex-parse-entry))))))
13257 (defun org-execute-file-search-in-bibtex (s)
13258 "Find the link search string S as a key for a database entry."
13259 (when (eq major-mode 'bibtex-mode)
13260 ;; Yes, we want to do the search in this file.
13261 ;; We construct a regexp that searches for "@entrytype{" followed by the key
13262 (goto-char (point-min))
13263 (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
13264 (regexp-quote s) "[ \t\n]*,") nil t)
13265 (goto-char (match-beginning 0)))
13266 (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
13267 ;; Use double prefix to indicate that any web link should be browsed
13268 (let ((b (current-buffer)) (p (point)))
13269 ;; Restore the window configuration because we just use the web link
13270 (set-window-configuration org-window-config-before-follow-link)
13271 (save-excursion (set-buffer b) (goto-char p)
13273 (recenter 0)) ; Move entry start to beginning of window
13274 ;; return t to indicate that the search is done.
13277 ;; Finally add the functions to the right hooks.
13278 (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
13279 (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
13281 ;; end of Bibtex link setup
13283 ;;; Following file links
13285 (defun org-open-file (path &optional in-emacs line search)
13286 "Open the file at PATH.
13287 First, this expands any special file name abbreviations. Then the
13288 configuration variable `org-file-apps' is checked if it contains an
13289 entry for this file type, and if yes, the corresponding command is launched.
13290 If no application is found, Emacs simply visits the file.
13291 With optional argument IN-EMACS, Emacs will visit the file.
13292 Optional LINE specifies a line to go to, optional SEARCH a string to
13293 search for. If LINE or SEARCH is given, the file will always be
13295 If the file does not exist, an error is thrown."
13296 (setq in-emacs (or in-emacs line search))
13297 (let* ((file (if (equal path "")
13299 (substitute-in-file-name (expand-file-name path))))
13300 (apps (append org-file-apps (org-default-apps)))
13301 (remp (and (assq 'remote apps) (org-file-remote-p file)))
13302 (dirp (if remp nil (file-directory-p file)))
13303 (dfile (downcase file))
13304 (old-buffer (current-buffer))
13306 (old-mode major-mode)
13308 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
13309 (setq ext (match-string 1 dfile))
13310 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
13311 (setq ext (match-string 1 dfile))))
13314 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
13315 (and dirp (cdr (assoc 'directory apps)))
13316 (cdr (assoc ext apps))
13317 (cdr (assoc t apps)))))
13318 (when (eq cmd 'mailcap)
13320 (mailcap-parse-mailcaps)
13321 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
13322 (command (mailcap-mime-info mime-type)))
13323 (if (stringp command)
13325 (setq cmd 'emacs))))
13326 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
13327 (not (file-exists-p file))
13328 (not org-open-non-existing-files))
13329 (error "No such file: %s" file))
13331 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
13332 ;; Remove quotes around the file name - we'll use shell-quote-argument.
13333 (while (string-match "['\"]%s['\"]" cmd)
13334 (setq cmd (replace-match "%s" t t cmd)))
13335 (while (string-match "%s" cmd)
13336 (setq cmd (replace-match
13337 (save-match-data (shell-quote-argument file))
13339 (save-window-excursion
13340 (start-process-shell-command cmd nil cmd)))
13343 (funcall (cdr (assq 'file org-link-frame-setup)) file)
13345 (if line (goto-line line)
13346 (if search (org-link-search search))))
13349 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
13350 (and (org-mode-p) (eq old-mode 'org-mode)
13351 (or (not (equal old-buffer (current-buffer)))
13352 (not (equal old-pos (point))))
13353 (org-mark-ring-push old-pos old-buffer))))
13355 (defun org-default-apps ()
13356 "Return the default applications for this operating system."
13358 ((eq system-type 'darwin)
13359 org-file-apps-defaults-macosx)
13360 ((eq system-type 'windows-nt)
13361 org-file-apps-defaults-windowsnt)
13362 (t org-file-apps-defaults-gnu)))
13364 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
13365 (defun org-file-remote-p (file)
13366 "Test whether FILE specifies a location on a remote system.
13367 Return non-nil if the location is indeed remote.
13369 For example, the filename \"/user@host:/foo\" specifies a location
13370 on the system \"/user@host:\"."
13371 (cond ((fboundp 'file-remote-p)
13372 (file-remote-p file))
13373 ((fboundp 'tramp-handle-file-remote-p)
13374 (tramp-handle-file-remote-p file))
13375 ((and (boundp 'ange-ftp-name-format)
13376 (string-match (car ange-ftp-name-format) file))
13381 ;;;; Hooks for remember.el, and refiling
13383 (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
13384 (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
13387 (defun org-remember-insinuate ()
13388 "Setup remember.el for use wiht Org-mode."
13389 (require 'remember)
13390 (setq remember-annotation-functions '(org-remember-annotation))
13391 (setq remember-handler-functions '(org-remember-handler))
13392 (add-hook 'remember-mode-hook 'org-remember-apply-template))
13395 (defun org-remember-annotation ()
13396 "Return a link to the current location as an annotation for remember.el.
13397 If you are using Org-mode files as target for data storage with
13398 remember.el, then the annotations should include a link compatible with the
13399 conventions in Org-mode. This function returns such a link."
13400 (org-store-link nil))
13402 (defconst org-remember-help
13403 "Select a destination location for the note.
13404 UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
13405 RET on headline -> Store as sublevel entry to current headline
13406 RET at beg-of-buf -> Append to file as level 2 headline
13407 <left>/<right> -> before/after current headline, same headings level")
13409 (defvar org-remember-previous-location nil)
13410 (defvar org-force-remember-template-char) ;; dynamically scoped
13412 (defun org-select-remember-template (&optional use-char)
13413 (when org-remember-templates
13414 (let* ((templates (mapcar (lambda (x)
13415 (if (stringp (car x))
13416 (append (list (nth 1 x) (car x)) (cddr x))
13417 (append (list (car x) "") (cdr x))))
13418 org-remember-templates))
13421 ((= (length templates) 1)
13423 ((and (boundp 'org-force-remember-template-char)
13424 org-force-remember-template-char)
13425 (if (stringp org-force-remember-template-char)
13426 (string-to-char org-force-remember-template-char)
13427 org-force-remember-template-char))
13429 (message "Select template: %s"
13433 ((not (string-match "\\S-" (nth 1 x)))
13434 (format "[%c]" (car x)))
13435 ((equal (downcase (car x))
13436 (downcase (aref (nth 1 x) 0)))
13437 (format "[%c]%s" (car x)
13438 (substring (nth 1 x) 1)))
13439 (t (format "[%c]%s" (car x) (nth 1 x)))))
13441 (let ((inhibit-quit t) (char0 (read-char-exclusive)))
13442 (when (equal char0 ?\C-g)
13443 (jump-to-register remember-register)
13444 (kill-buffer remember-buffer))
13446 (cddr (assoc char templates)))))
13448 (defvar x-last-selected-text)
13449 (defvar x-last-selected-text-primary)
13452 (defun org-remember-apply-template (&optional use-char skip-interactive)
13453 "Initialize *remember* buffer with template, invoke `org-mode'.
13454 This function should be placed into `remember-mode-hook' and in fact requires
13455 to be run from that hook to function properly."
13456 (if org-remember-templates
13457 (let* ((entry (org-select-remember-template use-char))
13459 (plist-p (if org-store-link-plist t nil))
13460 (file (if (and (nth 1 entry) (stringp (nth 1 entry))
13461 (string-match "\\S-" (nth 1 entry)))
13463 org-default-notes-file))
13464 (headline (nth 2 entry))
13465 (v-c (or (and (eq window-system 'x)
13466 (fboundp 'x-cut-buffer-or-selection-value)
13467 (x-cut-buffer-or-selection-value))
13468 (org-bound-and-true-p x-last-selected-text)
13469 (org-bound-and-true-p x-last-selected-text-primary)
13470 (and (> (length kill-ring) 0) (current-kill 0))))
13471 (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
13472 (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
13473 (v-u (concat "[" (substring v-t 1 -1) "]"))
13474 (v-U (concat "[" (substring v-T 1 -1) "]"))
13475 ;; `initial' and `annotation' are bound in `remember'
13476 (v-i (if (boundp 'initial) initial))
13477 (v-a (if (and (boundp 'annotation) annotation)
13478 (if (equal annotation "[[]]") "" annotation)
13481 (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
13482 (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
13484 (v-n user-full-name)
13485 (org-startup-folded nil)
13486 org-time-was-given org-end-time-was-given x
13487 prompt completions char time pos default histvar)
13488 (setq org-store-link-plist
13489 (append (list :annotation v-a :initial v-i)
13490 org-store-link-plist))
13491 (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
13493 (insert (substitute-command-keys
13495 "## Filing location: Select interactively, default, or last used:
13496 ## %s to select file and header location interactively.
13497 ## %s \"%s\" -> \"* %s\"
13498 ## C-u C-u C-c C-c \"%s\" -> \"* %s\"
13499 ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
13500 (if org-remember-store-without-prompt " C-u C-c C-c" " C-c C-c")
13501 (if org-remember-store-without-prompt " C-c C-c" " C-u C-c C-c")
13502 (abbreviate-file-name (or file org-default-notes-file))
13504 (or (car org-remember-previous-location) "???")
13505 (or (cdr org-remember-previous-location) "???"))))
13506 (insert tpl) (goto-char (point-min))
13507 ;; Simple %-escapes
13508 (while (re-search-forward "%\\([tTuUaiAc]\\)" nil t)
13509 (when (and initial (equal (match-string 0) "%i"))
13511 (let* ((lead (buffer-substring
13512 (point-at-bol) (match-beginning 0))))
13513 (setq v-i (mapconcat 'identity
13514 (org-split-string initial "\n")
13515 (concat "\n" lead))))))
13517 (or (eval (intern (concat "v-" (match-string 1)))) "")
13520 ;; %[] Insert contents of a file.
13521 (goto-char (point-min))
13522 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
13523 (let ((start (match-beginning 0))
13524 (end (match-end 0))
13525 (filename (expand-file-name (match-string 1))))
13527 (delete-region start end)
13528 (condition-case error
13529 (insert-file-contents filename)
13530 (error (insert (format "%%![Couldn't insert %s: %s]"
13531 filename error))))))
13532 ;; %() embedded elisp
13533 (goto-char (point-min))
13534 (while (re-search-forward "%\\((.+)\\)" nil t)
13535 (goto-char (match-beginning 0))
13536 (let ((template-start (point)))
13539 (condition-case error
13540 (eval (read (current-buffer)))
13541 (error (format "%%![Error: %s]" error)))))
13542 (delete-region template-start (point))
13545 ;; From the property list
13547 (goto-char (point-min))
13548 (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
13549 (and (setq x (or (plist-get org-store-link-plist
13550 (intern (match-string 1))) ""))
13551 (replace-match x t t))))
13553 ;; Turn on org-mode in the remember buffer, set local variables
13555 (org-set-local 'org-finish-function 'org-remember-finalize)
13556 (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
13557 (org-set-local 'org-default-notes-file file))
13558 (if (and headline (stringp headline) (string-match "\\S-" headline))
13559 (org-set-local 'org-remember-default-headline headline))
13560 ;; Interactive template entries
13561 (goto-char (point-min))
13562 (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGuUtT]\\)?" nil t)
13563 (setq char (if (match-end 3) (match-string 3))
13564 prompt (if (match-end 2) (match-string 2)))
13565 (goto-char (match-beginning 0))
13567 (setq completions nil default nil)
13569 (setq completions (org-split-string prompt "|")
13570 prompt (pop completions)
13571 default (car completions)
13572 histvar (intern (concat
13573 "org-remember-template-prompt-history::"
13575 completions (mapcar 'list completions)))
13577 ((member char '("G" "g"))
13578 (let* ((org-last-tags-completion-table
13579 (org-global-tags-completion-table
13580 (if (equal char "G") (org-agenda-files) (and file (list file)))))
13581 (org-add-colon-after-tag-completion t)
13582 (ins (completing-read
13583 (if prompt (concat prompt ": ") "Tags: ")
13584 'org-tags-completion-function nil nil nil
13585 'org-tags-history)))
13586 (setq ins (mapconcat 'identity
13587 (org-split-string ins (org-re "[^[:alnum:]_@]+"))
13589 (when (string-match "\\S-" ins)
13590 (or (equal (char-before) ?:) (insert ":"))
13592 (or (equal (char-after) ?:) (insert ":")))))
13594 (setq org-time-was-given (equal (upcase char) char))
13595 (setq time (org-read-date (equal (upcase char) "U") t nil
13597 (org-insert-time-stamp time org-time-was-given
13598 (member char '("u" "U"))
13599 nil nil (list org-end-time-was-given)))
13601 (insert (org-completing-read
13602 (concat (if prompt prompt "Enter string")
13603 (if default (concat " [" default "]"))
13605 completions nil nil nil histvar default)))))
13606 (goto-char (point-min))
13607 (if (re-search-forward "%\\?" nil t)
13609 (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
13611 (org-set-local 'org-finish-function 'org-remember-finalize))
13612 (when (save-excursion
13613 (goto-char (point-min))
13614 (re-search-forward "%!" nil t))
13616 (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
13618 (defun org-remember-finish-immediately ()
13619 "File remember note immediately.
13620 This should be run in `post-command-hook' and will remove itself
13622 (remove-hook 'post-command-hook 'org-remember-finish-immediately)
13623 (when org-finish-function
13624 (funcall org-finish-function)))
13626 (defvar org-clock-marker) ; Defined below
13627 (defun org-remember-finalize ()
13628 "Finalize the remember process."
13629 (unless (fboundp 'remember-finalize)
13630 (defalias 'remember-finalize 'remember-buffer))
13631 (when (and org-clock-marker
13632 (equal (marker-buffer org-clock-marker) (current-buffer)))
13633 ;; FIXME: test this, this is w/o notetaking!
13634 (let (org-log-done) (org-clock-out)))
13635 (when buffer-file-name
13637 (setq buffer-file-name nil))
13638 (remember-finalize))
13641 (defun org-remember (&optional goto org-force-remember-template-char)
13642 "Call `remember'. If this is already a remember buffer, re-apply template.
13643 If there is an active region, make sure remember uses it as initial content
13644 of the remember buffer.
13646 When called interactively with a `C-u' prefix argument GOTO, don't remember
13647 anything, just go to the file/headline where the selected template usually
13648 stores its notes. With a double prefix arg `C-u C-u', go to the last
13649 note stored by remember.
13651 Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
13652 associated with a template in `org-remember-templates'."
13655 ((equal goto '(4)) (org-go-to-remember-target))
13656 ((equal goto '(16)) (org-remember-goto-last-stored))
13658 (if (memq org-finish-function '(remember-buffer remember-finalize))
13660 (when (< (length org-remember-templates) 2)
13661 (error "No other template available"))
13663 (let ((annotation (plist-get org-store-link-plist :annotation))
13664 (initial (plist-get org-store-link-plist :initial)))
13665 (org-remember-apply-template))
13666 (message "Press C-c C-c to remember data"))
13667 (if (org-region-active-p)
13668 (remember (buffer-substring (point) (mark)))
13669 (call-interactively 'remember))))))
13671 (defun org-remember-goto-last-stored ()
13672 "Go to the location where the last remember note was stored."
13674 (bookmark-jump "org-remember-last-stored")
13675 (message "This is the last note stored by remember"))
13677 (defun org-go-to-remember-target (&optional template-key)
13678 "Go to the target location of a remember template.
13679 The user is queried for the template."
13681 (let* ((entry (org-select-remember-template template-key))
13682 (file (nth 1 entry))
13683 (heading (nth 2 entry))
13685 (unless (and file (stringp file) (string-match "\\S-" file))
13686 (setq file org-default-notes-file))
13687 (unless (and heading (stringp heading) (string-match "\\S-" heading))
13688 (setq heading org-remember-default-headline))
13689 (setq visiting (org-find-base-buffer-visiting file))
13690 (if (not visiting) (find-file-noselect file))
13691 (switch-to-buffer (or visiting (get-file-buffer file)))
13693 (goto-char (point-min))
13694 (if (re-search-forward
13695 (concat "^\\*+[ \t]+" (regexp-quote heading)
13696 (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
13698 (goto-char (match-beginning 0))
13699 (error "Target headline not found: %s" heading))))
13701 (defvar org-note-abort nil) ; dynamically scoped
13704 (defun org-remember-handler ()
13705 "Store stuff from remember.el into an org file.
13706 First prompts for an org file. If the user just presses return, the value
13707 of `org-default-notes-file' is used.
13708 Then the command offers the headings tree of the selected file in order to
13709 file the text at a specific location.
13710 You can either immediately press RET to get the note appended to the
13711 file, or you can use vertical cursor motion and visibility cycling (TAB) to
13712 find a better place. Then press RET or <left> or <right> in insert the note.
13714 Key Cursor position Note gets inserted
13715 -----------------------------------------------------------------------------
13716 RET buffer-start as level 1 heading at end of file
13717 RET on headline as sublevel of the heading at cursor
13718 RET no heading at cursor position, level taken from context.
13719 Or use prefix arg to specify level manually.
13720 <left> on headline as same level, before current heading
13721 <right> on headline as same level, after current heading
13723 So the fastest way to store the note is to press RET RET to append it to
13724 the default file. This way your current train of thought is not
13725 interrupted, in accordance with the principles of remember.el.
13726 You can also get the fast execution without prompting by using
13727 C-u C-c C-c to exit the remember buffer. See also the variable
13728 `org-remember-store-without-prompt'.
13730 Before being stored away, the function ensures that the text has a
13731 headline, i.e. a first line that starts with a \"*\". If not, a headline
13732 is constructed from the current date and some additional data.
13734 If the variable `org-adapt-indentation' is non-nil, the entire text is
13735 also indented so that it starts in the same column as the headline
13736 \(i.e. after the stars).
13738 See also the variable `org-reverse-note-order'."
13739 (goto-char (point-min))
13740 (while (looking-at "^[ \t]*\n\\|^##.*\n")
13741 (replace-match ""))
13742 (goto-char (point-max))
13743 (beginning-of-line 1)
13744 (while (looking-at "[ \t]*$\\|##.*")
13745 (delete-region (1- (point)) (point-max))
13746 (beginning-of-line 1))
13748 (if org-note-abort (throw 'quit nil))
13749 (let* ((txt (buffer-substring (point-min) (point-max)))
13750 (fastp (org-xor (equal current-prefix-arg '(4))
13751 org-remember-store-without-prompt))
13753 (fastp org-default-notes-file)
13754 ((and (eq org-remember-interactive-interface 'refile)
13755 org-refile-targets)
13756 org-default-notes-file)
13757 ((not (and (equal current-prefix-arg '(16))
13758 org-remember-previous-location))
13759 (org-get-org-file))))
13760 (heading org-remember-default-headline)
13761 (visiting (and file (org-find-base-buffer-visiting file)))
13762 (org-startup-folded nil)
13763 (org-startup-align-all-tables nil)
13764 (org-goto-start-pos 1)
13765 spos exitcmd level indent reversed)
13766 (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
13767 (setq file (car org-remember-previous-location)
13768 heading (cdr org-remember-previous-location)
13770 (setq current-prefix-arg nil)
13771 (if (string-match "[ \t\n]+\\'" txt)
13772 (setq txt (replace-match "" t t txt)))
13773 ;; Modify text so that it becomes a nice subtree which can be inserted
13774 ;; into an org tree.
13775 (let* ((lines (split-string txt "\n"))
13777 (setq first (car lines) lines (cdr lines))
13778 (if (string-match "^\\*+ " first)
13779 ;; Is already a headline
13781 ;; We need to add a headline: Use time and first buffer line
13782 (setq lines (cons first lines)
13783 first (concat "* " (current-time-string)
13784 " (" (remember-buffer-desc) ")")
13786 (if (and org-adapt-indentation indent)
13787 (setq lines (mapcar
13789 (if (string-match "\\S-" x)
13790 (concat indent x) x))
13792 (setq txt (concat first "\n"
13793 (mapconcat 'identity lines "\n"))))
13794 (if (string-match "\n[ \t]*\n[ \t\n]*\\'" txt)
13795 (setq txt (replace-match "\n\n" t t txt))
13796 (if (string-match "[ \t\n]*\\'" txt)
13797 (setq txt (replace-match "\n" t t txt))))
13798 ;; Put the modified text back into the remember buffer, for refile.
13801 (goto-char (point-min))
13802 (when (and (eq org-remember-interactive-interface 'refile)
13804 (org-refile nil (or visiting (find-file-noselect file)))
13807 (if (not visiting) (find-file-noselect file))
13808 (with-current-buffer (or visiting (get-file-buffer file))
13809 (unless (org-mode-p)
13810 (error "Target files for remember notes must be in Org-mode"))
13814 (and (goto-char (point-min))
13815 (not (re-search-forward "^\\* " nil t))
13816 (insert "\n* " (or heading "Notes") "\n"))
13817 (setq reversed (org-notes-order-reversed-p))
13819 ;; Find the default location
13820 (when (and heading (stringp heading) (string-match "\\S-" heading))
13821 (goto-char (point-min))
13822 (if (re-search-forward
13823 (concat "^\\*+[ \t]+" (regexp-quote heading)
13824 (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
13826 (setq org-goto-start-pos (match-beginning 0))
13828 (goto-char (point-max))
13829 (unless (bolp) (newline))
13830 (insert "* " heading "\n")
13831 (setq org-goto-start-pos (point-at-bol 0)))))
13833 ;; Ask the User for a location, using the appropriate interface
13835 (fastp (setq spos org-goto-start-pos
13837 ((eq org-remember-interactive-interface 'outline)
13838 (setq spos (org-get-location (current-buffer)
13842 ((eq org-remember-interactive-interface 'outline-path-completion)
13843 (let ((org-refile-targets '((nil . (:maxlevel . 10))))
13844 (org-refile-use-outline-path t))
13845 (setq spos (org-refile-get-location "Heading: ")
13847 spos (nth 3 spos))))
13848 (t (error "this should not hapen")))
13849 (if (not spos) (throw 'quit nil)) ; return nil to show we did
13850 ; not handle this note
13852 (cond ((org-on-heading-p t)
13853 (org-back-to-heading t)
13854 (setq level (funcall outline-level))
13856 ((eq exitcmd 'return)
13857 ;; sublevel of current
13858 (setq org-remember-previous-location
13859 (cons (abbreviate-file-name file)
13860 (org-get-heading 'notags)))
13862 (outline-next-heading)
13863 (org-end-of-subtree t)
13865 (if (looking-at "[ \t]*\n")
13866 (beginning-of-line 2)
13869 (bookmark-set "org-remember-last-stored")
13870 (org-paste-subtree (org-get-legal-level level 1) txt))
13871 ((eq exitcmd 'left)
13873 (bookmark-set "org-remember-last-stored")
13874 (org-paste-subtree level txt))
13875 ((eq exitcmd 'right)
13877 (org-end-of-subtree t)
13878 (bookmark-set "org-remember-last-stored")
13879 (org-paste-subtree level txt))
13880 (t (error "This should not happen"))))
13882 ((and (bobp) (not reversed))
13883 ;; Put it at the end, one level below level 1
13886 (goto-char (point-max))
13887 (if (not (bolp)) (newline))
13888 (bookmark-set "org-remember-last-stored")
13889 (org-paste-subtree (org-get-legal-level 1 1) txt)))
13891 ((and (bobp) reversed)
13892 ;; Put it at the start, as level 1
13895 (goto-char (point-min))
13896 (re-search-forward "^\\*+ " nil t)
13897 (beginning-of-line 1)
13898 (bookmark-set "org-remember-last-stored")
13899 (org-paste-subtree 1 txt)))
13901 ;; Put it right there, with automatic level determined by
13902 ;; org-paste-subtree or from prefix arg
13903 (bookmark-set "org-remember-last-stored")
13905 (if (numberp current-prefix-arg) current-prefix-arg)
13907 (when remember-save-after-remembering
13909 (if (not visiting) (kill-buffer (current-buffer)))))))))
13911 t) ;; return t to indicate that we took care of this note.
13913 (defun org-get-org-file ()
13914 "Read a filename, with default directory `org-directory'."
13915 (let ((default (or org-default-notes-file remember-data-file)))
13916 (read-file-name (format "File name [%s]: " default)
13917 (file-name-as-directory org-directory)
13920 (defun org-notes-order-reversed-p ()
13921 "Check if the current file should receive notes in reversed order."
13923 ((not org-reverse-note-order) nil)
13924 ((eq t org-reverse-note-order) t)
13925 ((not (listp org-reverse-note-order)) nil)
13927 (let ((all org-reverse-note-order)
13929 (while (setq entry (pop all))
13930 (if (string-match (car entry) buffer-file-name)
13931 (throw 'exit (cdr entry))))
13936 (defvar org-refile-target-table nil
13937 "The list of refile targets, created by `org-refile'.")
13939 (defvar org-agenda-new-buffers nil
13940 "Buffers created to visit agenda files.")
13942 (defun org-get-refile-targets (&optional default-buffer)
13943 "Produce a table with refile targets."
13944 (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
13945 targets txt re files f desc descre)
13946 (with-current-buffer (or default-buffer (current-buffer))
13947 (while (setq entry (pop entries))
13948 (setq files (car entry) desc (cdr entry))
13950 ((null files) (setq files (list (current-buffer))))
13951 ((eq files 'org-agenda-files)
13952 (setq files (org-agenda-files 'unrestricted)))
13953 ((and (symbolp files) (fboundp files))
13954 (setq files (funcall files)))
13955 ((and (symbolp files) (boundp files))
13956 (setq files (symbol-value files))))
13957 (if (stringp files) (setq files (list files)))
13959 ((eq (car desc) :tag)
13960 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
13961 ((eq (car desc) :todo)
13962 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
13963 ((eq (car desc) :regexp)
13964 (setq descre (cdr desc)))
13965 ((eq (car desc) :level)
13966 (setq descre (concat "^\\*\\{" (number-to-string
13967 (if org-odd-levels-only
13968 (1- (* 2 (cdr desc)))
13971 ((eq (car desc) :maxlevel)
13972 (setq descre (concat "^\\*\\{1," (number-to-string
13973 (if org-odd-levels-only
13974 (1- (* 2 (cdr desc)))
13977 (t (error "Bad refiling target description %s" desc)))
13978 (while (setq f (pop files))
13980 (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
13981 (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
13985 (goto-char (point-min))
13986 (while (re-search-forward descre nil t)
13987 (goto-char (point-at-bol))
13988 (when (looking-at org-complex-heading-regexp)
13989 (setq txt (match-string 4)
13990 re (concat "^" (regexp-quote
13991 (buffer-substring (match-beginning 1)
13993 (if (match-end 5) (setq re (concat re "[ \t]+"
13995 (match-string 5)))))
13996 (setq re (concat re "[ \t]*$"))
13997 (when org-refile-use-outline-path
13998 (setq txt (mapconcat 'identity
14000 (if (eq org-refile-use-outline-path 'file)
14001 (list (file-name-nondirectory
14002 (buffer-file-name (buffer-base-buffer))))
14003 (if (eq org-refile-use-outline-path 'full-file-path)
14004 (list (buffer-file-name (buffer-base-buffer)))))
14005 (org-get-outline-path)
14008 (push (list txt f re (point)) targets))
14009 (goto-char (point-at-eol))))))))
14010 (nreverse targets))))
14012 (defun org-get-outline-path ()
14013 "Return the outline path to the current entry, as a list."
14016 (while (org-up-heading-safe)
14017 (when (looking-at org-complex-heading-regexp)
14018 (push (org-match-string-no-properties 4) rtn)))
14021 (defvar org-refile-history nil
14022 "History for refiling operations.")
14024 (defun org-refile (&optional goto default-buffer)
14025 "Move the entry at point to another heading.
14026 The list of target headings is compiled using the information in
14027 `org-refile-targets', which see. This list is created upon first use, and
14028 you can update it by calling this command with a double prefix (`C-u C-u').
14029 FIXME: Can we find a better way of updating?
14031 At the target location, the entry is filed as a subitem of the target heading.
14032 Depending on `org-reverse-note-order', the new subitem will either be the
14033 first of the last subitem.
14035 With prefix arg GOTO, the command will only visit the target location,
14036 not actually move anything.
14037 With a double prefix `C-c C-c', go to the location where the last refiling
14038 operation has put the subtree.
14040 With a double prefix argument, the command can be used to jump to any
14041 heading in the current buffer."
14043 (let* ((cbuf (current-buffer))
14044 (filename (buffer-file-name (buffer-base-buffer cbuf)))
14045 (fname (and filename (file-truename filename)))
14046 pos it nbuf file re level reversed)
14047 (if (equal goto '(16))
14048 (org-refile-goto-last-stored)
14049 (when (setq it (org-refile-get-location
14050 (if goto "Goto: " "Refile to: ") default-buffer))
14051 (setq file (nth 1 it)
14054 (setq nbuf (or (find-buffer-visiting file)
14055 (find-file-noselect file)))
14058 (switch-to-buffer nbuf)
14060 (org-show-context 'org-goto))
14063 (set-buffer (setq nbuf (or (find-buffer-visiting file)
14064 (find-file-noselect file))))
14065 (setq reversed (org-notes-order-reversed-p))
14070 (looking-at outline-regexp)
14071 (setq level (org-get-legal-level (funcall outline-level) 1))
14074 (outline-next-heading)
14075 (or (save-excursion (outline-get-next-sibling))
14076 (org-end-of-subtree t t)
14078 (bookmark-set "org-refile-last-stored")
14079 (org-paste-subtree level))))
14081 (message "Entry refiled to \"%s\"" (car it)))))))
14083 (defun org-refile-goto-last-stored ()
14084 "Go to the location where the last refile was stored."
14086 (bookmark-jump "org-refile-last-stored")
14087 (message "This is the location of the last refile"))
14089 (defun org-refile-get-location (&optional prompt default-buffer)
14090 "Prompt the user for a refile location, using PROMPT."
14091 (let ((org-refile-targets org-refile-targets)
14092 (org-refile-use-outline-path org-refile-use-outline-path))
14093 (setq org-refile-target-table (org-get-refile-targets default-buffer)))
14094 (unless org-refile-target-table
14095 (error "No refile targets"))
14096 (let* ((cbuf (current-buffer))
14097 (filename (buffer-file-name (buffer-base-buffer cbuf)))
14098 (fname (and filename (file-truename filename)))
14101 (if (not (equal fname (file-truename (nth 1 x))))
14102 (cons (concat (car x) " (" (file-name-nondirectory
14106 org-refile-target-table))
14107 (completion-ignore-case t)
14108 pos it nbuf file re level reversed)
14109 (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
14112 ;;;; Dynamic blocks
14114 (defun org-find-dblock (name)
14115 "Find the first dynamic block with name NAME in the buffer.
14116 If not found, stay at current position and return nil."
14119 (goto-char (point-min))
14120 (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
14122 (match-beginning 0))))
14123 (if pos (goto-char pos))
14126 (defconst org-dblock-start-re
14127 "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
14128 "Matches the startline of a dynamic block, with parameters.")
14130 (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
14131 "Matches the end of a dyhamic block.")
14133 (defun org-create-dblock (plist)
14134 "Create a dynamic block section, with parameters taken from PLIST.
14135 PLIST must containe a :name entry which is used as name of the block."
14136 (unless (bolp) (newline))
14137 (let ((name (plist-get plist :name)))
14138 (insert "#+BEGIN: " name)
14140 (if (eq (car plist) :name)
14141 (setq plist (cddr plist))
14142 (insert " " (prin1-to-string (pop plist)))))
14143 (insert "\n\n#+END:\n")
14144 (beginning-of-line -2)))
14146 (defun org-prepare-dblock ()
14147 "Prepare dynamic block for refresh.
14148 This empties the block, puts the cursor at the insert position and returns
14149 the property list including an extra property :name with the block name."
14150 (unless (looking-at org-dblock-start-re)
14151 (error "Not at a dynamic block"))
14152 (let* ((begdel (1+ (match-end 0)))
14153 (name (org-no-properties (match-string 1)))
14154 (params (append (list :name name)
14155 (read (concat "(" (match-string 3) ")")))))
14156 (unless (re-search-forward org-dblock-end-re nil t)
14157 (error "Dynamic block not terminated"))
14158 (delete-region begdel (match-beginning 0))
14163 (defun org-map-dblocks (&optional command)
14164 "Apply COMMAND to all dynamic blocks in the current buffer.
14165 If COMMAND is not given, use `org-update-dblock'."
14166 (let ((cmd (or command 'org-update-dblock))
14169 (goto-char (point-min))
14170 (while (re-search-forward org-dblock-start-re nil t)
14171 (goto-char (setq pos (match-beginning 0)))
14172 (condition-case nil
14174 (error (message "Error during update of dynamic block")))
14176 (unless (re-search-forward org-dblock-end-re nil t)
14177 (error "Dynamic block not terminated"))))))
14179 (defun org-dblock-update (&optional arg)
14180 "User command for updating dynamic blocks.
14181 Update the dynamic block at point. With prefix ARG, update all dynamic
14182 blocks in the buffer."
14185 (org-update-all-dblocks)
14186 (or (looking-at org-dblock-start-re)
14187 (org-beginning-of-dblock))
14188 (org-update-dblock)))
14190 (defun org-update-dblock ()
14191 "Update the dynamic block at point
14192 This means to empty the block, parse for parameters and then call
14193 the correct writing function."
14194 (save-window-excursion
14195 (let* ((pos (point))
14196 (line (org-current-line))
14197 (params (org-prepare-dblock))
14198 (name (plist-get params :name))
14199 (cmd (intern (concat "org-dblock-write:" name))))
14200 (message "Updating dynamic block `%s' at line %d..." name line)
14201 (funcall cmd params)
14202 (message "Updating dynamic block `%s' at line %d...done" name line)
14205 (defun org-beginning-of-dblock ()
14206 "Find the beginning of the dynamic block at point.
14207 Error if there is no scuh block at point."
14208 (let ((pos (point))
14211 (if (and (re-search-backward org-dblock-start-re nil t)
14212 (setq beg (match-beginning 0))
14213 (re-search-forward org-dblock-end-re nil t)
14214 (> (match-end 0) pos))
14217 (error "Not in a dynamic block"))))
14219 (defun org-update-all-dblocks ()
14220 "Update all dynamic blocks in the buffer.
14221 This function can be used in a hook."
14223 (org-map-dblocks 'org-update-dblock)))
14228 (defconst org-additional-option-like-keywords
14229 '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
14230 "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "DATE:" "TBLFM"
14231 "BEGIN_EXAMPLE" "END_EXAMPLE"))
14233 (defun org-complete (&optional arg)
14234 "Perform completion on word at point.
14235 At the beginning of a headline, this completes TODO keywords as given in
14236 `org-todo-keywords'.
14237 If the current word is preceded by a backslash, completes the TeX symbols
14238 that are supported for HTML support.
14239 If the current word is preceded by \"#+\", completes special words for
14240 setting file options.
14241 In the line after \"#+STARTUP:, complete valid keywords.\"
14242 At all other locations, this simply calls the value of
14243 `org-completion-fallback-command'."
14245 (org-without-partial-completion
14247 (let* ((end (point))
14248 (beg1 (save-excursion
14249 (skip-chars-backward (org-re "[:alnum:]_@"))
14251 (beg (save-excursion
14252 (skip-chars-backward "a-zA-Z0-9_:$")
14254 (confirm (lambda (x) (stringp (car x))))
14255 (searchhead (equal (char-before beg) ?*))
14256 (tag (and (equal (char-before beg1) ?:)
14257 (equal (char-after (point-at-bol)) ?*)))
14258 (prop (and (equal (char-before beg1) ?:)
14259 (not (equal (char-after (point-at-bol)) ?*))))
14260 (texp (equal (char-before beg) ?\\))
14261 (link (equal (char-before beg) ?\[))
14262 (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
14265 (startup (string-match "^#\\+STARTUP:.*"
14266 (buffer-substring (point-at-bol) (point))))
14267 (completion-ignore-case opt)
14276 (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
14277 (cons (match-string 2 x) (match-string 1 x)))
14278 (org-split-string (org-get-current-options) "\n"))
14279 (mapcar 'list org-additional-option-like-keywords)))
14281 (setq type :startup)
14282 org-startup-options)
14283 (link (append org-link-abbrev-alist-local
14284 org-link-abbrev-alist))
14288 ((string-match "\\`\\*+[ \t]+\\'"
14289 (buffer-substring (point-at-bol) beg))
14291 (mapcar 'list org-todo-keywords-1))
14293 (setq type :searchhead)
14295 (goto-char (point-min))
14296 (while (re-search-forward org-todo-line-regexp nil t)
14298 (org-make-org-heading-search-string
14299 (match-string 3) t))
14302 (tag (setq type :tag beg beg1)
14303 (or org-tag-alist (org-get-buffer-tags)))
14304 (prop (setq type :prop beg beg1)
14305 (mapcar 'list (org-buffer-property-keys nil t t)))
14307 (call-interactively org-completion-fallback-command)
14308 (throw 'exit nil)))))
14309 (pattern (buffer-substring-no-properties beg end))
14310 (completion (try-completion pattern table confirm)))
14311 (cond ((eq completion t)
14312 (if (not (assoc (upcase pattern) table))
14313 (message "Already complete")
14314 (if (equal type :opt)
14315 (insert (substring (cdr (assoc (upcase pattern) table))
14317 (if (memq type '(:tag :prop)) (insert ":")))))
14319 (message "Can't find completion for \"%s\"" pattern)
14321 ((not (string= pattern completion))
14322 (delete-region beg end)
14323 (if (string-match " +$" completion)
14324 (setq completion (replace-match "" t t completion)))
14325 (insert completion)
14326 (if (get-buffer-window "*Completions*")
14327 (delete-window (get-buffer-window "*Completions*")))
14328 (if (assoc completion table)
14329 (if (eq type :todo) (insert " ")
14330 (if (memq type '(:tag :prop)) (insert ":"))))
14331 (if (and (equal type :opt) (assoc completion table))
14332 (message "%s" (substitute-command-keys
14333 "Press \\[org-complete] again to insert example settings"))))
14335 (message "Making completion list...")
14336 (let ((list (sort (all-completions pattern table confirm)
14338 (with-output-to-temp-buffer "*Completions*"
14339 (condition-case nil
14340 ;; Protection needed for XEmacs and emacs 21
14341 (display-completion-list list pattern)
14342 (error (display-completion-list list)))))
14343 (message "Making completion list...%s" "done")))))))
14345 ;;;; TODO, DEADLINE, Comments
14347 (defun org-toggle-comment ()
14348 "Change the COMMENT state of an entry."
14351 (org-back-to-heading)
14352 (let (case-fold-search)
14353 (if (looking-at (concat outline-regexp
14354 "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
14355 (replace-match "" t t nil 1)
14356 (if (looking-at outline-regexp)
14358 (goto-char (match-end 0))
14359 (insert org-comment-string " ")))))))
14361 (defvar org-last-todo-state-is-todo nil
14362 "This is non-nil when the last TODO state change led to a TODO state.
14363 If the last change removed the TODO tag or switched to DONE, then
14366 (defvar org-setting-tags nil) ; dynamically skiped
14368 ;; FIXME: better place
14369 (defun org-property-or-variable-value (var &optional inherit)
14370 "Check if there is a property fixing the value of VAR.
14371 If yes, return this value. If not, return the current value of the variable."
14372 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
14373 (if (and prop (stringp prop) (string-match "\\S-" prop))
14375 (symbol-value var))))
14377 (defun org-parse-local-options (string var)
14378 "Parse STRING for startup setting relevant for variable VAR."
14379 (let ((rtn (symbol-value var))
14382 (if (or (not string) (not (string-match "\\S-" string)))
14384 (setq opts (delq nil (mapcar (lambda (x)
14385 (setq e (assoc x org-startup-options))
14386 (if (eq (nth 1 e) var) e nil))
14387 (org-split-string string "[ \t]+"))))
14391 (while (setq e (pop opts))
14392 (if (not (nth 3 e))
14393 (setq rtn (nth 2 e))
14394 (if (not (listp rtn)) (setq rtn nil))
14395 (push (nth 2 e) rtn)))
14398 (defvar org-blocker-hook nil
14399 "Hook for functions that are allowed to block a state change.
14401 Each function gets as its single argument a property list, see
14402 `org-trigger-hook' for more information about this list.
14404 If any of the functions in this hook returns nil, the state change
14407 (defvar org-trigger-hook nil
14408 "Hook for functions that are triggered by a state change.
14410 Each function gets as its single argument a property list with at least
14411 the following elements:
14413 (:type type-of-change :position pos-at-entry-start
14414 :from old-state :to new-state)
14416 Depending on the type, more properties may be present.
14418 This mechanism is currently implemented for:
14422 :type todo-state-change
14423 :from previous state (keyword as a string), or nil
14424 :to new state (keyword as a string), or nil")
14427 (defun org-todo (&optional arg)
14428 "Change the TODO state of an item.
14429 The state of an item is given by a keyword at the start of the heading,
14431 *** TODO Write paper
14434 The different keywords are specified in the variable `org-todo-keywords'.
14435 By default the available states are \"TODO\" and \"DONE\".
14436 So for this example: when the item starts with TODO, it is changed to DONE.
14437 When it starts with DONE, the DONE is removed. And when neither TODO nor
14438 DONE are present, add TODO at the beginning of the heading.
14440 With C-u prefix arg, use completion to determine the new state.
14441 With numeric prefix arg, switch to that state.
14443 For calling through lisp, arg is also interpreted in the following way:
14444 'none -> empty state
14445 \"\"(empty string) -> switch to empty state
14446 'done -> switch to DONE
14447 'nextset -> switch to the next set of keywords
14448 'previousset -> switch to the previous set of keywords
14449 \"WAITING\" -> switch to the specified keyword, but only if it
14450 really is a member of `org-todo-keywords'."
14454 (org-back-to-heading)
14455 (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
14456 (or (looking-at (concat " +" org-todo-regexp " *"))
14458 (let* ((match-data (match-data))
14459 (startpos (point-at-bol))
14460 (logging (save-match-data (org-entry-get nil "LOGGING" t)))
14461 (org-log-done (org-parse-local-options logging 'org-log-done))
14462 (org-log-repeat (org-parse-local-options logging 'org-log-repeat))
14463 (this (match-string 1))
14464 (hl-pos (match-beginning 0))
14465 (head (org-get-todo-sequence-head this))
14466 (ass (assoc head org-todo-kwd-alist))
14467 (interpret (nth 1 ass))
14468 (done-word (nth 3 ass))
14469 (final-done-word (nth 4 ass))
14470 (last-state (or this ""))
14471 (completion-ignore-case t)
14472 (member (member this org-todo-keywords-1))
14473 (tail (cdr member))
14475 ((and org-todo-key-trigger
14476 (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
14477 (and (not arg) org-use-fast-todo-selection
14478 (not (eq org-use-fast-todo-selection 'prefix)))))
14479 ;; Use fast selection
14480 (org-fast-todo-selection))
14481 ((and (equal arg '(4))
14482 (or (not org-use-fast-todo-selection)
14483 (not org-todo-key-trigger)))
14484 ;; Read a state with completion
14485 (completing-read "State: " (mapcar (lambda(x) (list x))
14486 org-todo-keywords-1)
14490 (if tail (car tail) nil)
14491 (car org-todo-keywords-1)))
14493 (if (equal member org-todo-keywords-1)
14496 (nth (- (length org-todo-keywords-1) (length tail) 2)
14497 org-todo-keywords-1)
14498 (org-last org-todo-keywords-1))))
14499 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
14500 (setq arg nil))) ; hack to fall back to cycling
14502 ;; user or caller requests a specific state
14504 ((equal arg "") nil)
14505 ((eq arg 'none) nil)
14506 ((eq arg 'done) (or done-word (car org-done-keywords)))
14508 (or (car (cdr (member head org-todo-heads)))
14509 (car org-todo-heads)))
14510 ((eq arg 'previousset)
14511 (let ((org-todo-heads (reverse org-todo-heads)))
14512 (or (car (cdr (member head org-todo-heads)))
14513 (car org-todo-heads))))
14514 ((car (member arg org-todo-keywords-1)))
14515 ((nth (1- (prefix-numeric-value arg))
14516 org-todo-keywords-1))))
14517 ((null member) (or head (car org-todo-keywords-1)))
14518 ((equal this final-done-word) nil) ;; -> make empty
14519 ((null tail) nil) ;; -> first entry
14520 ((eq interpret 'sequence)
14522 ((memq interpret '(type priority))
14523 (if (eq this-command last-command)
14525 (if (> (length tail) 0)
14526 (or done-word (car org-done-keywords))
14529 (next (if state (concat " " state " ") " "))
14530 (change-plist (list :type 'todo-state-change :from this :to state
14531 :position startpos))
14533 (when org-blocker-hook
14534 (unless (save-excursion
14536 (run-hook-with-args-until-failure
14537 'org-blocker-hook change-plist)))
14538 (if (interactive-p)
14539 (error "TODO state change from %s to %s blocked" this state)
14541 (message "TODO state change from %s to %s blocked" this state)
14542 (throw 'exit nil))))
14543 (store-match-data match-data)
14544 (replace-match next t t)
14545 (unless (pos-visible-in-window-p hl-pos)
14546 (message "TODO state changed to %s" (org-trim next)))
14548 (setq head (org-get-todo-sequence-head state)
14549 ass (assoc head org-todo-kwd-alist)
14550 interpret (nth 1 ass)
14551 done-word (nth 3 ass)
14552 final-done-word (nth 4 ass)))
14553 (when (memq arg '(nextset previousset))
14554 (message "Keyword-Set %d/%d: %s"
14555 (- (length org-todo-sets) -1
14556 (length (memq (assoc state org-todo-sets) org-todo-sets)))
14557 (length org-todo-sets)
14558 (mapconcat 'identity (assoc state org-todo-sets) " ")))
14559 (setq org-last-todo-state-is-todo
14560 (not (member state org-done-keywords)))
14561 (when (and org-log-done (not (memq arg '(nextset previousset))))
14562 (setq dostates (and (listp org-log-done) (memq 'state org-log-done)
14563 (or (not org-todo-log-states)
14564 (member state org-todo-log-states))))
14567 ((and state (member state org-not-done-keywords)
14568 (not (member this org-not-done-keywords)))
14569 ;; This is now a todo state and was not one before
14570 ;; Remove any CLOSED timestamp, and possibly log the state change
14571 (org-add-planning-info nil nil 'closed)
14572 (and dostates (org-add-log-maybe 'state state 'findpos)))
14573 ((and state dostates)
14574 ;; This is a non-nil state, and we need to log it
14575 (org-add-log-maybe 'state state 'findpos))
14576 ((and (member state org-done-keywords)
14577 (not (member this org-done-keywords)))
14578 ;; It is now done, and it was not done before
14579 (org-add-planning-info 'closed (org-current-time))
14580 (org-add-log-maybe 'done state 'findpos))))
14581 ;; Fixup tag positioning
14582 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
14583 (run-hooks 'org-after-todo-state-change-hook)
14584 (and (member state org-done-keywords) (org-auto-repeat-maybe))
14585 (if (and arg (not (member state org-done-keywords)))
14586 (setq head (org-get-todo-sequence-head state)))
14587 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
14588 ;; Fixup cursor location if close to the keyword
14589 (if (and (outline-on-heading-p)
14591 (save-excursion (beginning-of-line 1)
14592 (looking-at org-todo-line-regexp))
14593 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
14595 (goto-char (or (match-end 2) (match-end 1)))
14597 (when org-trigger-hook
14599 (run-hook-with-args 'org-trigger-hook change-plist)))))))
14601 (defun org-get-todo-sequence-head (kwd)
14602 "Return the head of the TODO sequence to which KWD belongs.
14603 If KWD is not set, check if there is a text property remembering the
14608 (or (get-text-property (point-at-bol) 'org-todo-head)
14610 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
14611 nil (point-at-eol)))
14612 (get-text-property p 'org-todo-head))))
14613 ((not (member kwd org-todo-keywords-1))
14614 (car org-todo-keywords-1))
14615 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
14617 (defun org-fast-todo-selection ()
14618 "Fast TODO keyword selection with single keys.
14619 Returns the new TODO keyword, or nil if no state change should occur."
14620 (let* ((fulltable org-todo-key-alist)
14621 (done-keywords org-done-keywords) ;; needed for the faces.
14622 (maxlen (apply 'max (mapcar
14624 (if (stringp (car x)) (string-width (car x)) 0))
14627 (fwidth (+ maxlen 3 1 3))
14628 (ncol (/ (- (window-width) 4) fwidth))
14631 (save-window-excursion
14633 (set-buffer (get-buffer-create " *Org todo*"))
14634 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
14636 (org-set-local 'org-done-keywords done-keywords)
14637 (setq tbl fulltable cnt 0)
14638 (while (setq e (pop tbl))
14640 ((equal e '(:startgroup))
14641 (push '() groups) (setq ingroup t)
14642 (when (not (= cnt 0))
14646 ((equal e '(:endgroup))
14647 (setq ingroup nil cnt 0)
14650 (setq tg (car e) c (cdr e))
14651 (if ingroup (push tg (car groups)))
14652 (setq tg (org-add-props tg nil 'face
14653 (org-get-todo-face tg)))
14654 (if (and (= cnt 0) (not ingroup)) (insert " "))
14655 (insert "[" c "] " tg (make-string
14656 (- fwidth 4 (length tg)) ?\ ))
14657 (when (= (setq cnt (1+ cnt)) ncol)
14659 (if ingroup (insert " "))
14662 (goto-char (point-min))
14663 (if (and (not expert) (fboundp 'fit-window-to-buffer))
14664 (fit-window-to-buffer))
14665 (message "[a-z..]:Set [SPC]:clear")
14666 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
14669 (and (= c ?q) (not (rassoc c fulltable))))
14670 (setq quit-flag t))
14672 ((setq e (rassoc c fulltable) tg (car e))
14674 (t (setq quit-flag t))))))
14676 (defun org-get-repeat ()
14677 "Check if tere is a deadline/schedule with repeater in this entry."
14680 (org-back-to-heading t)
14681 (if (re-search-forward
14682 org-repeat-re (save-excursion (outline-next-heading) (point)) t)
14683 (match-string 1)))))
14685 (defvar org-last-changed-timestamp)
14686 (defvar org-log-post-message)
14687 (defun org-auto-repeat-maybe ()
14688 "Check if the current headline contains a repeated deadline/schedule.
14689 If yes, set TODO state back to what it was and change the base date
14690 of repeating deadline/scheduled time stamps to new date.
14691 This function should be run in the `org-after-todo-state-change-hook'."
14692 ;; last-state is dynamically scoped into this function
14693 (let* ((repeat (org-get-repeat))
14694 (aa (assoc last-state org-todo-kwd-alist))
14695 (interpret (nth 1 aa))
14697 (done-word (nth 3 aa))
14698 (whata '(("d" . day) ("m" . month) ("y" . year)))
14699 (msg "Entry repeats: ")
14703 (org-todo (if (eq interpret 'type) last-state head))
14704 (when (and org-log-repeat
14705 (not (memq 'org-add-log-note
14706 (default-value 'post-command-hook))))
14707 ;; Make sure a note is taken
14708 (let ((org-log-done '(done)))
14709 (org-add-log-maybe 'done (or done-word (car org-done-keywords))
14711 (org-back-to-heading t)
14712 (org-add-planning-info nil nil 'closed)
14713 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
14714 org-deadline-time-regexp "\\)\\|\\("
14715 org-ts-regexp "\\)"))
14716 (while (re-search-forward
14717 re (save-excursion (outline-next-heading) (point)) t)
14718 (setq type (if (match-end 1) org-scheduled-string
14719 (if (match-end 3) org-deadline-string "Plain:"))
14720 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
14721 (when (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" ts)
14722 (setq n (string-to-number (match-string 1 ts))
14723 what (match-string 2 ts))
14724 (if (equal what "w") (setq n (* n 7) what "d"))
14725 (org-timestamp-change n (cdr (assoc what whata)))
14726 (setq msg (concat msg type org-last-changed-timestamp " "))))
14727 (setq org-log-post-message msg)
14728 (message "%s" msg))))
14730 (defun org-show-todo-tree (arg)
14731 "Make a compact tree which shows all headlines marked with TODO.
14732 The tree will show the lines where the regexp matches, and all higher
14733 headlines above the match.
14734 With \\[universal-argument] prefix, also show the DONE entries.
14735 With a numeric prefix N, construct a sparse tree for the Nth element
14736 of `org-todo-keywords-1'."
14738 (let ((case-fold-search nil)
14740 (cond ((null arg) org-not-done-regexp)
14742 (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
14743 (mapcar 'list org-todo-keywords-1))))
14745 (mapconcat 'identity (org-split-string kwd "|") "\\|")
14747 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
14748 (regexp-quote (nth (1- (prefix-numeric-value arg))
14749 org-todo-keywords-1)))
14750 (t (error "Invalid prefix argument: %s" arg)))))
14751 (message "%d TODO entries found"
14752 (org-occur (concat "^" outline-regexp " *" kwd-re )))))
14754 (defun org-deadline (&optional remove)
14755 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
14756 With argument REMOVE, remove any deadline from the item."
14760 (org-remove-timestamp-with-keyword org-deadline-string)
14761 (message "Item no longer has a deadline."))
14762 (org-add-planning-info 'deadline nil 'closed)))
14764 (defun org-schedule (&optional remove)
14765 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
14766 With argument REMOVE, remove any scheduling date from the item."
14770 (org-remove-timestamp-with-keyword org-scheduled-string)
14771 (message "Item is no longer scheduled."))
14772 (org-add-planning-info 'scheduled nil 'closed)))
14774 (defun org-remove-timestamp-with-keyword (keyword)
14775 "Remove all time stamps with KEYWORD in the current entry."
14776 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
14779 (org-back-to-heading t)
14781 (org-end-of-subtree t t)
14782 (while (re-search-backward re beg t)
14784 (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
14785 (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
14787 (defun org-add-planning-info (what &optional time &rest remove)
14788 "Insert new timestamp with keyword in the line directly after the headline.
14789 WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
14790 If non is given, the user is prompted for a date.
14791 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
14794 (let (org-time-was-given org-end-time-was-given)
14795 (when what (setq time (or time (org-read-date nil 'to-time))))
14796 (when (and org-insert-labeled-timestamps-at-point
14797 (member what '(scheduled deadline)))
14799 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
14800 (org-insert-time-stamp time org-time-was-given
14801 nil nil nil (list org-end-time-was-given))
14805 (let (col list elt ts buffer-invisibility-spec)
14806 (org-back-to-heading t)
14807 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
14808 (goto-char (match-end 1))
14809 (setq col (current-column))
14810 (goto-char (match-end 0))
14811 (if (eobp) (insert "\n") (forward-char 1))
14812 (if (and (not (looking-at outline-regexp))
14813 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
14815 (not (equal (match-string 1) org-clock-string)))
14816 (narrow-to-region (match-beginning 0) (match-end 0))
14817 (insert-before-markers "\n")
14819 (narrow-to-region (point) (point))
14820 (indent-to-column col))
14821 ;; Check if we have to remove something.
14822 (setq list (cons what remove))
14824 (setq elt (pop list))
14825 (goto-char (point-min))
14826 (when (or (and (eq elt 'scheduled)
14827 (re-search-forward org-scheduled-time-regexp nil t))
14828 (and (eq elt 'deadline)
14829 (re-search-forward org-deadline-time-regexp nil t))
14830 (and (eq elt 'closed)
14831 (re-search-forward org-closed-time-regexp nil t)))
14833 (if (looking-at "--+<[^>]+>") (replace-match ""))
14834 (if (looking-at " +") (replace-match ""))))
14835 (goto-char (point-max))
14838 (if (not (equal (char-before) ?\ )) " " "")
14839 (cond ((eq what 'scheduled) org-scheduled-string)
14840 ((eq what 'deadline) org-deadline-string)
14841 ((eq what 'closed) org-closed-string))
14843 (setq ts (org-insert-time-stamp
14845 (or org-time-was-given
14846 (and (eq what 'closed) org-log-done-with-time))
14848 nil nil (list org-end-time-was-given)))
14850 (goto-char (point-min))
14852 (if (looking-at "[ \t]+\r?\n")
14853 (replace-match ""))
14856 (defvar org-log-note-marker (make-marker))
14857 (defvar org-log-note-purpose nil)
14858 (defvar org-log-note-state nil)
14859 (defvar org-log-note-window-configuration nil)
14860 (defvar org-log-note-return-to (make-marker))
14861 (defvar org-log-post-message nil
14862 "Message to be displayed after a log note has been stored.
14863 The auto-repeater uses this.")
14865 (defun org-add-log-maybe (&optional purpose state findpos)
14866 "Set up the post command hook to take a note."
14868 (when (and (listp org-log-done)
14869 (memq purpose org-log-done))
14871 (org-back-to-heading t)
14872 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
14873 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
14875 (goto-char (match-end 0))
14876 (unless org-log-states-order-reversed
14877 (and (= (char-after) ?\n) (forward-char 1))
14878 (org-skip-over-state-notes)
14879 (skip-chars-backward " \t\n\r")))
14880 (move-marker org-log-note-marker (point))
14881 (setq org-log-note-purpose purpose)
14882 (setq org-log-note-state state)
14883 (add-hook 'post-command-hook 'org-add-log-note 'append))))
14885 (defun org-skip-over-state-notes ()
14886 "Skip past the list of State notes in an entry."
14887 (if (looking-at "\n[ \t]*- State") (forward-char 1))
14888 (while (looking-at "[ \t]*- State")
14889 (condition-case nil
14891 (error (org-end-of-item)))))
14893 (defun org-add-log-note (&optional purpose)
14894 "Pop up a window for taking a note, and add this note later at point."
14895 (remove-hook 'post-command-hook 'org-add-log-note)
14896 (setq org-log-note-window-configuration (current-window-configuration))
14897 (delete-other-windows)
14898 (move-marker org-log-note-return-to (point))
14899 (switch-to-buffer (marker-buffer org-log-note-marker))
14900 (goto-char org-log-note-marker)
14901 (org-switch-to-buffer-other-window "*Org Note*")
14903 (let ((org-inhibit-startup t)) (org-mode))
14904 (insert (format "# Insert note for %s.
14905 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
14907 ((eq org-log-note-purpose 'clock-out) "stopped clock")
14908 ((eq org-log-note-purpose 'done) "closed todo item")
14909 ((eq org-log-note-purpose 'state)
14910 (format "state change to \"%s\"" org-log-note-state))
14911 (t (error "This should not happen")))))
14912 (org-set-local 'org-finish-function 'org-store-log-note))
14914 (defun org-store-log-note ()
14915 "Finish taking a log note, and insert it to where it belongs."
14916 (let ((txt (buffer-string))
14917 (note (cdr (assq org-log-note-purpose org-log-note-headings)))
14919 (kill-buffer (current-buffer))
14920 (while (string-match "\\`#.*\n[ \t\n]*" txt)
14921 (setq txt (replace-match "" t t txt)))
14922 (if (string-match "\\s-+\\'" txt)
14923 (setq txt (replace-match "" t t txt)))
14924 (setq lines (org-split-string txt "\n"))
14925 (when (and note (string-match "\\S-" note))
14927 (org-replace-escapes
14929 (list (cons "%u" (user-login-name))
14930 (cons "%U" user-full-name)
14931 (cons "%t" (format-time-string
14932 (org-time-stamp-format 'long 'inactive)
14934 (cons "%s" (if org-log-note-state
14935 (concat "\"" org-log-note-state "\"")
14937 (if lines (setq note (concat note " \\\\")))
14939 (when (or current-prefix-arg org-note-abort) (setq lines nil))
14942 (set-buffer (marker-buffer org-log-note-marker))
14944 (goto-char org-log-note-marker)
14945 (move-marker org-log-note-marker nil)
14947 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
14948 (indent-relative nil)
14949 (insert "- " (pop lines))
14950 (org-indent-line-function)
14951 (beginning-of-line 1)
14952 (looking-at "[ \t]*")
14953 (setq ind (concat (match-string 0) " "))
14955 (while lines (insert "\n" ind (pop lines)))))))
14956 (set-window-configuration org-log-note-window-configuration)
14957 (with-current-buffer (marker-buffer org-log-note-return-to)
14958 (goto-char org-log-note-return-to))
14959 (move-marker org-log-note-return-to nil)
14960 (and org-log-post-message (message "%s" org-log-post-message)))
14962 ;; FIXME: what else would be useful?
14966 (defun org-sparse-tree (&optional arg)
14967 "Create a sparse tree, prompt for the details.
14968 This command can create sparse trees. You first need to select the type
14969 of match used to create the tree:
14971 t Show entries with a specific TODO keyword.
14972 T Show entries selected by a tags match.
14973 p Enter a property name and its value (both with completion on existing
14974 names/values) and show entries with that property.
14975 r Show entries matching a regular expression
14976 d Show deadlines due within `org-deadline-warning-days'."
14978 (let (ans kwd value)
14979 (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
14980 (setq ans (read-char-exclusive))
14983 (call-interactively 'org-check-deadlines))
14985 (call-interactively 'org-check-before-date))
14987 (org-show-todo-tree '(4)))
14989 (call-interactively 'org-tags-sparse-tree))
14990 ((member ans '(?p ?P))
14991 (setq kwd (completing-read "Property: "
14992 (mapcar 'list (org-buffer-property-keys))))
14993 (setq value (completing-read "Value: "
14994 (mapcar 'list (org-property-values kwd))))
14995 (unless (string-match "\\`{.*}\\'" value)
14996 (setq value (concat "\"" value "\"")))
14997 (org-tags-sparse-tree arg (concat kwd "=" value)))
14998 ((member ans '(?r ?R ?/))
14999 (call-interactively 'org-occur))
15000 (t (error "No such sparse tree command \"%c\"" ans)))))
15002 (defvar org-occur-highlights nil)
15003 (make-variable-buffer-local 'org-occur-highlights)
15005 (defun org-occur (regexp &optional keep-previous callback)
15006 "Make a compact tree which shows all matches of REGEXP.
15007 The tree will show the lines where the regexp matches, and all higher
15008 headlines above the match. It will also show the heading after the match,
15009 to make sure editing the matching entry is easy.
15010 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
15011 call to `org-occur' will be kept, to allow stacking of calls to this
15013 If CALLBACK is non-nil, it is a function which is called to confirm
15014 that the match should indeed be shown."
15015 (interactive "sRegexp: \nP")
15016 (or keep-previous (org-remove-occur-highlights nil nil t))
15019 (goto-char (point-min))
15020 (if (or (not keep-previous) ; do not want to keep
15021 (not org-occur-highlights)) ; no previous matches
15024 (while (re-search-forward regexp nil t)
15025 (when (or (not callback)
15026 (save-match-data (funcall callback)))
15027 (setq cnt (1+ cnt))
15028 (when org-highlight-sparse-tree-matches
15029 (org-highlight-new-match (match-beginning 0) (match-end 0)))
15030 (org-show-context 'occur-tree))))
15031 (when org-remove-highlights-with-change
15032 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
15034 (unless org-sparse-tree-open-archived-trees
15035 (org-hide-archived-subtrees (point-min) (point-max)))
15036 (run-hooks 'org-occur-hook)
15037 (if (interactive-p)
15038 (message "%d match(es) for regexp %s" cnt regexp))
15041 (defun org-show-context (&optional key)
15042 "Make sure point and context and visible.
15043 How much context is shown depends upon the variables
15044 `org-show-hierarchy-above', `org-show-following-heading'. and
15045 `org-show-siblings'."
15046 (let ((heading-p (org-on-heading-p t))
15047 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
15048 (following-p (org-get-alist-option org-show-following-heading key))
15049 (entry-p (org-get-alist-option org-show-entry-below key))
15050 (siblings-p (org-get-alist-option org-show-siblings key)))
15052 ;; Show heading or entry text
15053 (if (and heading-p (not entry-p))
15054 (org-flag-heading nil) ; only show the heading
15055 (and (or entry-p (org-invisible-p) (org-invisible-p2))
15056 (org-show-hidden-entry))) ; show entire entry
15058 ;; Show next sibling, or heading below text
15060 (and (if heading-p (org-goto-sibling) (outline-next-heading))
15061 (org-flag-heading nil))))
15062 (when siblings-p (org-show-siblings))
15064 ;; show all higher headings, possibly with siblings
15066 (while (and (condition-case nil
15067 (progn (org-up-heading-all 1) t)
15070 (org-flag-heading nil)
15071 (when siblings-p (org-show-siblings))))))))
15073 (defun org-reveal (&optional siblings)
15074 "Show current entry, hierarchy above it, and the following headline.
15075 This can be used to show a consistent set of context around locations
15076 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
15077 not t for the search context.
15079 With optional argument SIBLINGS, on each level of the hierarchy all
15080 siblings are shown. This repairs the tree structure to what it would
15081 look like when opened with hierarchical calls to `org-cycle'."
15083 (let ((org-show-hierarchy-above t)
15084 (org-show-following-heading t)
15085 (org-show-siblings (if siblings t org-show-siblings)))
15086 (org-show-context nil)))
15088 (defun org-highlight-new-match (beg end)
15089 "Highlight from BEG to END and mark the highlight is an occur headline."
15090 (let ((ov (org-make-overlay beg end)))
15091 (org-overlay-put ov 'face 'secondary-selection)
15092 (push ov org-occur-highlights)))
15094 (defun org-remove-occur-highlights (&optional beg end noremove)
15095 "Remove the occur highlights from the buffer.
15096 BEG and END are ignored. If NOREMOVE is nil, remove this function
15097 from the `before-change-functions' in the current buffer."
15099 (unless org-inhibit-highlight-removal
15100 (mapc 'org-delete-overlay org-occur-highlights)
15101 (setq org-occur-highlights nil)
15103 (remove-hook 'before-change-functions
15104 'org-remove-occur-highlights 'local))))
15108 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
15109 "Regular expression matching the priority indicator.")
15111 (defvar org-remove-priority-next-time nil)
15113 (defun org-priority-up ()
15114 "Increase the priority of the current item."
15116 (org-priority 'up))
15118 (defun org-priority-down ()
15119 "Decrease the priority of the current item."
15121 (org-priority 'down))
15123 (defun org-priority (&optional action)
15124 "Change the priority of an item by ARG.
15125 ACTION can be `set', `up', `down', or a character."
15127 (setq action (or action 'set))
15128 (let (current new news have remove)
15130 (org-back-to-heading)
15131 (if (looking-at org-priority-regexp)
15132 (setq current (string-to-char (match-string 2))
15134 (setq current org-default-priority))
15136 ((or (eq action 'set) (integerp action))
15137 (if (integerp action)
15139 (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
15140 (setq new (read-char-exclusive)))
15141 (if (and (= (upcase org-highest-priority) org-highest-priority)
15142 (= (upcase org-lowest-priority) org-lowest-priority))
15143 (setq new (upcase new)))
15144 (cond ((equal new ?\ ) (setq remove t))
15145 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
15146 (error "Priority must be between `%c' and `%c'"
15147 org-highest-priority org-lowest-priority))))
15149 (if (and (not have) (eq last-command this-command))
15150 (setq new org-lowest-priority)
15151 (setq new (if (and org-priority-start-cycle-with-default (not have))
15152 org-default-priority (1- current)))))
15154 (if (and (not have) (eq last-command this-command))
15155 (setq new org-highest-priority)
15156 (setq new (if (and org-priority-start-cycle-with-default (not have))
15157 org-default-priority (1+ current)))))
15158 (t (error "Invalid action")))
15159 (if (or (< (upcase new) org-highest-priority)
15160 (> (upcase new) org-lowest-priority))
15162 (setq news (format "%c" new))
15165 (replace-match "" t t nil 1)
15166 (replace-match news t t nil 2))
15168 (error "No priority cookie found in line")
15169 (looking-at org-todo-line-regexp)
15172 (goto-char (match-end 2))
15173 (insert " [#" news "]"))
15174 (goto-char (match-beginning 3))
15175 (insert "[#" news "] ")))))
15176 (org-preserve-lc (org-set-tags nil 'align))
15178 (message "Priority removed")
15179 (message "Priority of current item set to %s" news))))
15182 (defun org-get-priority (s)
15183 "Find priority cookie and return priority."
15185 (if (not (string-match org-priority-regexp s))
15186 (* 1000 (- org-lowest-priority org-default-priority))
15187 (* 1000 (- org-lowest-priority
15188 (string-to-char (match-string 2 s)))))))
15192 (defun org-scan-tags (action matcher &optional todo-only)
15193 "Scan headline tags with inheritance and produce output ACTION.
15194 ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
15195 evaluated, testing if a given set of tags qualifies a headline for
15196 inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
15197 are included in the output."
15198 (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
15199 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
15201 "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
15202 (props (list 'face nil
15203 'done-face 'org-done
15205 'mouse-face 'highlight
15206 'org-not-done-regexp org-not-done-regexp
15207 'org-todo-regexp org-todo-regexp
15208 'keymap org-agenda-keymap
15210 (format "mouse-2 or RET jump to org file %s"
15211 (abbreviate-file-name
15212 (or (buffer-file-name (buffer-base-buffer))
15213 (buffer-name (buffer-base-buffer)))))))
15214 (case-fold-search nil)
15216 tags tags-list tags-alist (llast 0) rtn level category i txt
15217 todo marker entry priority)
15219 (goto-char (point-min))
15220 (when (eq action 'sparse-tree)
15222 (org-remove-occur-highlights))
15223 (while (re-search-forward re nil t)
15225 (setq todo (if (match-end 1) (match-string 2))
15226 tags (if (match-end 4) (match-string 4)))
15227 (goto-char (setq lspos (1+ (match-beginning 0))))
15228 (setq level (org-reduced-level (funcall outline-level))
15229 category (org-get-category))
15230 (setq i llast llast level)
15231 ;; remove tag lists from same and sublevels
15232 (while (>= i level)
15233 (when (setq entry (assoc i tags-alist))
15234 (setq tags-alist (delete entry tags-alist)))
15236 ;; add the nex tags
15238 (setq tags (mapcar 'downcase (org-split-string tags ":"))
15240 (cons (cons level tags) tags-alist)))
15241 ;; compile tags for current headline
15243 (if org-use-tag-inheritance
15244 (apply 'append (mapcar 'cdr tags-alist))
15246 (when (and (or (not todo-only) (member todo org-not-done-keywords))
15248 (or (not org-agenda-skip-archived-trees)
15249 (not (member org-archive-tag tags-list))))
15250 (and (eq action 'agenda) (org-agenda-skip))
15251 ;; list this headline
15253 (if (eq action 'sparse-tree)
15255 (and org-highlight-sparse-tree-matches
15256 (org-get-heading) (match-end 0)
15257 (org-highlight-new-match
15258 (match-beginning 0) (match-beginning 1)))
15259 (org-show-context 'tags-tree))
15260 (setq txt (org-format-agenda-item
15263 (if org-tags-match-list-sublevels
15264 (make-string (1- level) ?.) "")
15266 category tags-list)
15267 priority (org-get-priority txt))
15269 (setq marker (org-agenda-new-marker))
15270 (org-add-props txt props
15271 'org-marker marker 'org-hd-marker marker 'org-category category
15272 'priority priority 'type "tagsmatch")
15274 ;; if we are to skip sublevels, jump to end of subtree
15275 (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
15276 (when (and (eq action 'sparse-tree)
15277 (not org-sparse-tree-open-archived-trees))
15278 (org-hide-archived-subtrees (point-min) (point-max)))
15281 (defvar todo-only) ;; dynamically scoped
15283 (defun org-tags-sparse-tree (&optional todo-only match)
15284 "Create a sparse tree according to tags string MATCH.
15285 MATCH can contain positive and negative selection of tags, like
15286 \"+WORK+URGENT-WITHBOSS\".
15287 If optional argument TODO_ONLY is non-nil, only select lines that are
15290 (org-prepare-agenda-buffers (list (current-buffer)))
15291 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
15293 (defvar org-cached-props nil)
15294 (defun org-cached-entry-get (pom property)
15295 (if (or (eq t org-use-property-inheritance)
15296 (member property org-use-property-inheritance))
15297 ;; Caching is not possible, check it directly
15298 (org-entry-get pom property 'inherit)
15299 ;; Get all properties, so that we can do complicated checks easily
15300 (cdr (assoc property (or org-cached-props
15301 (setq org-cached-props
15302 (org-entry-properties pom)))))))
15304 (defun org-global-tags-completion-table (&optional files)
15305 "Return the list of all tags in all agenda buffer/files."
15312 (set-buffer (find-file-noselect file))
15313 (append (org-get-buffer-tags)
15314 (mapcar (lambda (x) (if (stringp (car-safe x))
15315 (list (car-safe x)) nil))
15317 (if (and files (car files))
15319 (org-agenda-files))))))))
15321 (defun org-make-tags-matcher (match)
15322 "Create the TAGS//TODO matcher form for the selection string MATCH."
15323 ;; todo-only is scoped dynamically into this function, and the function
15324 ;; may change it it the matcher asksk for it.
15326 ;; Get a new match request, with completion
15327 (let ((org-last-tags-completion-table
15328 (org-global-tags-completion-table)))
15329 (setq match (completing-read
15330 "Match: " 'org-tags-completion-function nil nil nil
15331 'org-tags-history))))
15333 ;; Parse the string and create a lisp form
15334 (let ((match0 match)
15335 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]*\"\\)\\|[[:alnum:]_@]+\\)"))
15337 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
15338 orterms term orlist re-p level-p prop-p pn pv cat-p gv)
15339 (if (string-match "/+" match)
15340 ;; match contains also a todo-matching request
15342 (setq tagsmatch (substring match 0 (match-beginning 0))
15343 todomatch (substring match (match-end 0)))
15344 (if (string-match "^!" todomatch)
15345 (setq todo-only t todomatch (substring todomatch 1)))
15346 (if (string-match "^\\s-*$" todomatch)
15347 (setq todomatch nil)))
15348 ;; only matching tags
15349 (setq tagsmatch match todomatch nil))
15351 ;; Make the tags matcher
15352 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
15353 (setq tagsmatcher t)
15354 (setq orterms (org-split-string tagsmatch "|") orlist nil)
15355 (while (setq term (pop orterms))
15356 (while (and (equal (substring term -1) "\\") orterms)
15357 (setq term (concat term "|" (pop orterms)))) ; repair bad split
15358 (while (string-match re term)
15359 (setq minus (and (match-end 1)
15360 (equal (match-string 1 term) "-"))
15361 tag (match-string 2 term)
15362 re-p (equal (string-to-char tag) ?{)
15363 level-p (match-end 3)
15364 prop-p (match-end 4)
15366 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
15367 (level-p `(= level ,(string-to-number
15368 (match-string 3 term))))
15370 (setq pn (match-string 4 term)
15371 pv (match-string 5 term)
15372 cat-p (equal pn "CATEGORY")
15373 re-p (equal (string-to-char pv) ?{)
15374 pv (substring pv 1 -1))
15375 (if (equal pn "CATEGORY")
15376 (setq gv '(get-text-property (point) 'org-category))
15377 (setq gv `(org-cached-entry-get nil ,pn)))
15379 `(string-match ,pv (or ,gv ""))
15380 `(equal ,pv (or ,gv ""))))
15381 (t `(member ,(downcase tag) tags-list)))
15382 mm (if minus (list 'not mm) mm)
15383 term (substring term (match-end 0)))
15384 (push mm tagsmatcher))
15385 (push (if (> (length tagsmatcher) 1)
15386 (cons 'and tagsmatcher)
15389 (setq tagsmatcher nil))
15390 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
15392 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
15394 ;; Make the todo matcher
15395 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
15396 (setq todomatcher t)
15397 (setq orterms (org-split-string todomatch "|") orlist nil)
15398 (while (setq term (pop orterms))
15399 (while (string-match re term)
15400 (setq minus (and (match-end 1)
15401 (equal (match-string 1 term) "-"))
15402 kwd (match-string 2 term)
15403 re-p (equal (string-to-char kwd) ?{)
15404 term (substring term (match-end 0))
15406 `(string-match ,(substring kwd 1 -1) todo)
15407 (list 'equal 'todo kwd))
15408 mm (if minus (list 'not mm) mm))
15409 (push mm todomatcher))
15410 (push (if (> (length todomatcher) 1)
15411 (cons 'and todomatcher)
15414 (setq todomatcher nil))
15415 (setq todomatcher (if (> (length orlist) 1)
15416 (cons 'or orlist) (car orlist))))
15418 ;; Return the string and lisp forms of the matcher
15419 (setq matcher (if todomatcher
15420 (list 'and tagsmatcher todomatcher)
15422 (cons match0 matcher)))
15424 (defun org-match-any-p (re list)
15425 "Does re match any element of list?"
15426 (setq list (mapcar (lambda (x) (string-match re x)) list))
15429 (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
15430 (defvar org-tags-overlay (org-make-overlay 1 1))
15431 (org-detach-overlay org-tags-overlay)
15433 (defun org-align-tags-here (to-col)
15434 ;; Assumes that this is a headline
15435 (let ((pos (point)) (col (current-column)) tags)
15436 (beginning-of-line 1)
15437 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
15438 (< pos (match-beginning 2)))
15440 (setq tags (match-string 2))
15441 (goto-char (match-beginning 1))
15443 (delete-region (point) (1+ (match-end 0)))
15446 (max (1+ (current-column))
15450 (- (abs to-col) (length tags))))
15453 (move-to-column (min (current-column) col) t))
15456 (defun org-set-tags (&optional arg just-align)
15457 "Set the tags for the current headline.
15458 With prefix ARG, realign all tags in headings in the current buffer."
15460 (let* ((re (concat "^" outline-regexp))
15461 (current (org-get-tags-string))
15462 (col (current-column))
15463 (org-setting-tags t)
15464 table current-tags inherited-tags ; computed below when needed
15468 (goto-char (point-min))
15469 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
15470 (while (re-search-forward re nil t)
15471 (org-set-tags nil t)
15473 (message "All tags realigned to column %d" org-tags-column))
15475 (setq tags current)
15476 ;; Get a new set of tags from the user
15478 (setq table (or org-tag-alist (org-get-buffer-tags))
15479 org-last-tags-completion-table table
15480 current-tags (org-split-string current ":")
15481 inherited-tags (nreverse
15482 (nthcdr (length current-tags)
15483 (nreverse (org-get-tags-at))))
15485 (if (or (eq t org-use-fast-tag-selection)
15486 (and org-use-fast-tag-selection
15487 (delq nil (mapcar 'cdr table))))
15488 (org-fast-tag-selection
15489 current-tags inherited-tags table
15490 (if org-fast-tag-selection-include-todo org-todo-key-alist))
15491 (let ((org-add-colon-after-tag-completion t))
15493 (org-without-partial-completion
15494 (completing-read "Tags: " 'org-tags-completion-function
15495 nil nil current 'org-tags-history)))))))
15496 (while (string-match "[-+&]+" tags)
15497 ;; No boolean logic, just a list
15498 (setq tags (replace-match ":" t t tags))))
15500 (if (string-match "\\`[\t ]*\\'" tags)
15502 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
15503 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
15505 ;; Insert new tags at the correct column
15506 (beginning-of-line 1)
15508 ((and (equal current "") (equal tags "")))
15509 ((re-search-forward
15510 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
15512 (if (equal tags "")
15514 (goto-char (match-beginning 0))
15515 (setq c0 (current-column) p0 (point)
15516 c1 (max (1+ c0) (if (> org-tags-column 0)
15518 (- (- org-tags-column) (length tags))))
15519 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
15520 (replace-match rpl t t)
15521 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
15523 (t (error "Tags alignment failed")))
15524 (move-to-column col)
15526 (run-hooks 'org-after-tags-change-hook)))))
15528 (defun org-change-tag-in-region (beg end tag off)
15529 "Add or remove TAG for each entry in the region.
15530 This works in the agenda, and also in an org-mode buffer."
15532 (list (region-beginning) (region-end)
15533 (let ((org-last-tags-completion-table
15535 (org-get-buffer-tags)
15536 (org-global-tags-completion-table))))
15538 "Tag: " 'org-tags-completion-function nil nil nil
15539 'org-tags-history))
15541 (message "[s]et or [r]emove? ")
15542 (equal (read-char-exclusive) ?r))))
15543 (if (fboundp 'deactivate-mark) (deactivate-mark))
15544 (let ((agendap (equal major-mode 'org-agenda-mode))
15545 l1 l2 m buf pos newhead (cnt 0))
15547 (setq l2 (1- (org-current-line)))
15549 (setq l1 (org-current-line))
15550 (loop for l from l1 to l2 do
15552 (setq m (get-text-property (point) 'org-hd-marker))
15553 (when (or (and (org-mode-p) (org-on-heading-p))
15555 (setq buf (if agendap (marker-buffer m) (current-buffer))
15556 pos (if agendap m (point)))
15557 (with-current-buffer buf
15561 (setq cnt (1+ cnt))
15562 (org-toggle-tag tag (if off 'off 'on))
15563 (setq newhead (org-get-heading)))))
15564 (and agendap (org-agenda-change-all-lines newhead m))))
15565 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
15567 (defun org-tags-completion-function (string predicate &optional flag)
15568 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
15569 (confirm (lambda (x) (stringp (car x)))))
15570 (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
15571 (setq s1 (match-string 1 string)
15572 s2 (match-string 2 string))
15573 (setq s1 "" s2 string))
15577 (setq rtn (try-completion s2 ctable confirm))
15580 (concat s1 s2 (substring rtn (length s2))
15581 (if (and org-add-colon-after-tag-completion
15582 (assoc rtn ctable))
15587 (all-completions s2 ctable confirm)
15591 (assoc s2 ctable)))
15594 (defun org-fast-tag-insert (kwd tags face &optional end)
15595 "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
15596 (insert (format "%-12s" (concat kwd ":"))
15597 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
15600 (defun org-fast-tag-show-exit (flag)
15603 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
15604 (replace-match ""))
15607 (move-to-column (- (window-width) 19) t)
15608 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
15610 (defun org-set-current-tags-overlay (current prefix)
15611 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
15612 (if (featurep 'xemacs)
15613 (org-overlay-display org-tags-overlay (concat prefix s)
15614 'secondary-selection)
15615 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
15616 (org-overlay-display org-tags-overlay (concat prefix s)))))
15618 (defun org-fast-tag-selection (current inherited table &optional todo-table)
15619 "Fast tag selection with single keys.
15620 CURRENT is the current list of tags in the headline, INHERITED is the
15621 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
15622 possibly with grouping information. TODO-TABLE is a similar table with
15623 TODO keywords, should these have keys assigned to them.
15624 If the keys are nil, a-z are automatically assigned.
15625 Returns the new tags string, or nil to not change the current settings."
15626 (let* ((fulltable (append table todo-table))
15627 (maxlen (apply 'max (mapcar
15629 (if (stringp (car x)) (string-width (car x)) 0))
15631 (buf (current-buffer))
15632 (expert (eq org-fast-tag-selection-single-key 'expert))
15634 (fwidth (+ maxlen 3 1 3))
15635 (ncol (/ (- (window-width) 4) fwidth))
15638 tg cnt e c char c1 c2 ntable tbl rtn
15639 ov-start ov-end ov-prefix
15640 (exit-after-next org-fast-tag-selection-single-key)
15641 (done-keywords org-done-keywords)
15644 (beginning-of-line 1)
15646 (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
15647 (setq ov-start (match-beginning 1)
15648 ov-end (match-end 1)
15650 (setq ov-start (1- (point-at-eol))
15651 ov-end (1+ ov-start))
15652 (skip-chars-forward "^\n\r")
15655 (buffer-substring (1- (point)) (point))
15656 (if (> (current-column) org-tags-column)
15658 (make-string (- org-tags-column (current-column)) ?\ ))))))
15659 (org-move-overlay org-tags-overlay ov-start ov-end)
15660 (save-window-excursion
15662 (set-buffer (get-buffer-create " *Org tags*"))
15663 (delete-other-windows)
15664 (split-window-vertically)
15665 (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
15667 (org-set-local 'org-done-keywords done-keywords)
15668 (org-fast-tag-insert "Inherited" inherited i-face "\n")
15669 (org-fast-tag-insert "Current" current c-face "\n\n")
15670 (org-fast-tag-show-exit exit-after-next)
15671 (org-set-current-tags-overlay current ov-prefix)
15672 (setq tbl fulltable char ?a cnt 0)
15673 (while (setq e (pop tbl))
15675 ((equal e '(:startgroup))
15676 (push '() groups) (setq ingroup t)
15677 (when (not (= cnt 0))
15681 ((equal e '(:endgroup))
15682 (setq ingroup nil cnt 0)
15685 (setq tg (car e) c2 nil)
15688 ;; automatically assign a character.
15689 (setq c1 (string-to-char
15690 (downcase (substring
15691 tg (if (= (string-to-char tg) ?@) 1 0)))))
15692 (if (or (rassoc c1 ntable) (rassoc c1 table))
15693 (while (or (rassoc char ntable) (rassoc char table))
15694 (setq char (1+ char)))
15696 (setq c (or c2 char)))
15697 (if ingroup (push tg (car groups)))
15698 (setq tg (org-add-props tg nil 'face
15700 ((not (assoc tg table))
15701 (org-get-todo-face tg))
15702 ((member tg current) c-face)
15703 ((member tg inherited) i-face)
15705 (if (and (= cnt 0) (not ingroup)) (insert " "))
15706 (insert "[" c "] " tg (make-string
15707 (- fwidth 4 (length tg)) ?\ ))
15708 (push (cons tg c) ntable)
15709 (when (= (setq cnt (1+ cnt)) ncol)
15711 (if ingroup (insert " "))
15713 (setq ntable (nreverse ntable))
15715 (goto-char (point-min))
15716 (if (and (not expert) (fboundp 'fit-window-to-buffer))
15717 (fit-window-to-buffer))
15721 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
15722 (if groups " [!] no groups" " [!]groups")
15723 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
15724 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
15726 ((= c ?\r) (throw 'exit t))
15728 (setq groups (not groups))
15729 (goto-char (point-min))
15730 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
15733 (org-fast-tag-show-exit
15734 (setq exit-after-next (not exit-after-next)))
15736 (delete-other-windows)
15737 (split-window-vertically)
15738 (org-switch-to-buffer-other-window " *Org tags*")
15739 (and (fboundp 'fit-window-to-buffer)
15740 (fit-window-to-buffer))))
15742 (and (= c ?q) (not (rassoc c ntable))))
15743 (org-detach-overlay org-tags-overlay)
15744 (setq quit-flag t))
15747 (if exit-after-next (setq exit-after-next 'now)))
15749 (condition-case nil
15750 (setq tg (completing-read
15753 (with-current-buffer buf
15754 (org-get-buffer-tags)))))
15755 (quit (setq tg "")))
15756 (when (string-match "\\S-" tg)
15757 (add-to-list 'buffer-tags (list tg))
15758 (if (member tg current)
15759 (setq current (delete tg current))
15760 (push tg current)))
15761 (if exit-after-next (setq exit-after-next 'now)))
15762 ((setq e (rassoc c todo-table) tg (car e))
15763 (with-current-buffer buf
15764 (save-excursion (org-todo tg)))
15765 (if exit-after-next (setq exit-after-next 'now)))
15766 ((setq e (rassoc c ntable) tg (car e))
15767 (if (member tg current)
15768 (setq current (delete tg current))
15769 (loop for g in groups do
15772 (setq current (delete x current)))
15775 (if exit-after-next (setq exit-after-next 'now))))
15777 ;; Create a sorted list
15781 (assoc b (cdr (memq (assoc a ntable) ntable))))))
15782 (if (eq exit-after-next 'now) (throw 'exit t))
15783 (goto-char (point-min))
15784 (beginning-of-line 2)
15785 (delete-region (point) (point-at-eol))
15786 (org-fast-tag-insert "Current" current c-face)
15787 (org-set-current-tags-overlay current ov-prefix)
15788 (while (re-search-forward
15789 (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
15790 (setq tg (match-string 1))
15791 (add-text-properties
15792 (match-beginning 1) (match-end 1)
15795 ((member tg current) c-face)
15796 ((member tg inherited) i-face)
15797 (t (get-text-property (match-beginning 1) 'face))))))
15798 (goto-char (point-min)))))
15799 (org-detach-overlay org-tags-overlay)
15801 (mapconcat 'identity current ":")
15804 (defun org-get-tags-string ()
15805 "Get the TAGS string in the current headline."
15806 (unless (org-on-heading-p t)
15807 (error "Not on a heading"))
15809 (beginning-of-line 1)
15810 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
15811 (org-match-string-no-properties 1)
15814 (defun org-get-tags ()
15815 "Get the list of tags specified in the current headline."
15816 (org-split-string (org-get-tags-string) ":"))
15818 (defun org-get-buffer-tags ()
15819 "Get a table of all tags used in the buffer, for completion."
15822 (goto-char (point-min))
15823 (while (re-search-forward
15824 (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
15825 (when (equal (char-after (point-at-bol 0)) ?*)
15826 (mapc (lambda (x) (add-to-list 'tags x))
15827 (org-split-string (org-match-string-no-properties 1) ":")))))
15828 (mapcar 'list tags)))
15833 ;;; Setting and retrieving properties
15835 (defconst org-special-properties
15836 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
15837 "TIMESTAMP" "TIMESTAMP_IA")
15838 "The special properties valid in Org-mode.
15840 These are properties that are not defined in the property drawer,
15841 but in some other way.")
15843 (defconst org-default-properties
15844 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
15845 "LOCATION" "LOGGING" "COLUMNS")
15846 "Some properties that are used by Org-mode for various purposes.
15847 Being in this list makes sure that they are offered for completion.")
15849 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
15850 "Regular expression matching the first line of a property drawer.")
15852 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
15853 "Regular expression matching the first line of a property drawer.")
15855 (defun org-property-action ()
15856 "Do an action on properties."
15859 (org-at-property-p)
15860 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
15861 (setq c (read-char-exclusive))
15864 (call-interactively 'org-set-property))
15866 (call-interactively 'org-delete-property))
15868 (call-interactively 'org-delete-property-globally))
15870 (call-interactively 'org-compute-property-at-point))
15871 (t (error "No such property action %c" c)))))
15873 (defun org-at-property-p ()
15874 "Is the cursor in a property line?"
15875 ;; FIXME: Does not check if we are actually in the drawer.
15876 ;; FIXME: also returns true on any drawers.....
15877 ;; This is used by C-c C-c for property action.
15879 (beginning-of-line 1)
15880 (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
15882 (defmacro org-with-point-at (pom &rest body)
15883 "Move to buffer and point of point-or-marker POM for the duration of BODY."
15884 (declare (indent 1) (debug t))
15886 (if (markerp pom) (set-buffer (marker-buffer pom)))
15888 (goto-char (or pom (point)))
15891 (defun org-get-property-block (&optional beg end force)
15892 "Return the (beg . end) range of the body of the property drawer.
15893 BEG and END can be beginning and end of subtree, if not given
15894 they will be found.
15895 If the drawer does not exist and FORCE is non-nil, create the drawer."
15898 (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
15899 (end (or end (progn (outline-next-heading) (point)))))
15901 (if (re-search-forward org-property-start-re end t)
15902 (setq beg (1+ (match-end 0)))
15905 (org-insert-property-drawer)
15906 (setq end (progn (outline-next-heading) (point))))
15909 (if (re-search-forward org-property-start-re end t)
15910 (setq beg (1+ (match-end 0)))))
15911 (if (re-search-forward org-property-end-re end t)
15912 (setq end (match-beginning 0))
15913 (or force (throw 'exit nil))
15916 (org-indent-line-function)
15917 (insert ":END:\n"))
15920 (defun org-entry-properties (&optional pom which)
15921 "Get all properties of the entry at point-or-marker POM.
15922 This includes the TODO keyword, the tags, time strings for deadline,
15923 scheduled, and clocking, and any additional properties defined in the
15924 entry. The return value is an alist, keys may occur multiple times
15925 if the property key was used several times.
15926 POM may also be nil, in which case the current entry is used.
15927 If WHICH is nil or `all', get all properties. If WHICH is
15928 `special' or `standard', only get that subclass."
15929 (setq which (or which 'all))
15930 (org-with-point-at pom
15931 (let ((clockstr (substring org-clock-string 0 -1))
15932 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
15933 beg end range props sum-props key value string clocksum)
15935 (when (condition-case nil (org-back-to-heading t) (error nil))
15937 (setq sum-props (get-text-property (point) 'org-summaries))
15938 (setq clocksum (get-text-property (point) :org-clock-minutes))
15939 (outline-next-heading)
15941 (when (memq which '(all special))
15942 ;; Get the special properties, like TODO and tags
15944 (when (and (looking-at org-todo-line-regexp) (match-end 2))
15945 (push (cons "TODO" (org-match-string-no-properties 2)) props))
15946 (when (looking-at org-priority-regexp)
15947 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
15948 (when (and (setq value (org-get-tags-string))
15949 (string-match "\\S-" value))
15950 (push (cons "TAGS" value) props))
15951 (when (setq value (org-get-tags-at))
15952 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
15954 (while (re-search-forward org-maybe-keyword-time-regexp end t)
15955 (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
15956 string (if (equal key clockstr)
15960 (match-beginning 3) (goto-char (point-at-eol)))))
15961 (substring (org-match-string-no-properties 3) 1 -1)))
15963 (if (= (char-after (match-beginning 3)) ?\[)
15964 (setq key "TIMESTAMP_IA")
15965 (setq key "TIMESTAMP")))
15966 (when (or (equal key clockstr) (not (assoc key props)))
15967 (push (cons key string) props)))
15971 (when (memq which '(all standard))
15972 ;; Get the standard properties, like :PORP: ...
15973 (setq range (org-get-property-block beg end))
15975 (goto-char (car range))
15976 (while (re-search-forward
15977 (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
15979 (setq key (org-match-string-no-properties 1)
15980 value (org-trim (or (org-match-string-no-properties 2) "")))
15981 (unless (member key excluded)
15982 (push (cons key (or value "")) props)))))
15984 (push (cons "CLOCKSUM"
15985 (org-column-number-to-string (/ (float clocksum) 60.)
15988 (append sum-props (nreverse props)))))))
15990 (defun org-entry-get (pom property &optional inherit)
15991 "Get value of PROPERTY for entry at point-or-marker POM.
15992 If INHERIT is non-nil and the entry does not have the property,
15993 then also check higher levels of the hierarchy.
15994 If the property is present but empty, the return value is the empty string.
15995 If the property is not present at all, nil is returned."
15996 (org-with-point-at pom
15998 (org-entry-get-with-inheritance property)
15999 (if (member property org-special-properties)
16000 ;; We need a special property. Use brute force, get all properties.
16001 (cdr (assoc property (org-entry-properties nil 'special)))
16002 (let ((range (org-get-property-block)))
16004 (goto-char (car range))
16006 (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
16008 ;; Found the property, return it.
16010 (org-match-string-no-properties 1)
16013 (defun org-entry-delete (pom property)
16014 "Delete the property PROPERTY from entry at point-or-marker POM."
16015 (org-with-point-at pom
16016 (if (member property org-special-properties)
16017 nil ; cannot delete these properties.
16018 (let ((range (org-get-property-block)))
16020 (goto-char (car range))
16022 (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
16025 (delete-region (match-beginning 0) (1+ (point-at-eol)))
16029 ;; Multi-values properties are properties that contain multiple values
16030 ;; These values are assumed to be single words, separated by whitespace.
16031 (defun org-entry-add-to-multivalued-property (pom property value)
16032 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
16033 (let* ((old (org-entry-get pom property))
16034 (values (and old (org-split-string old "[ \t]"))))
16035 (unless (member value values)
16036 (setq values (cons value values))
16037 (org-entry-put pom property
16038 (mapconcat 'identity values " ")))))
16040 (defun org-entry-remove-from-multivalued-property (pom property value)
16041 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
16042 (let* ((old (org-entry-get pom property))
16043 (values (and old (org-split-string old "[ \t]"))))
16044 (when (member value values)
16045 (setq values (delete value values))
16046 (org-entry-put pom property
16047 (mapconcat 'identity values " ")))))
16049 (defun org-entry-member-in-multivalued-property (pom property value)
16050 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
16051 (let* ((old (org-entry-get pom property))
16052 (values (and old (org-split-string old "[ \t]"))))
16053 (member value values)))
16055 (defvar org-entry-property-inherited-from (make-marker))
16057 (defun org-entry-get-with-inheritance (property)
16058 "Get entry property, and search higher levels if not present."
16065 (when (setq tmp (org-entry-get nil property))
16066 (org-back-to-heading t)
16067 (move-marker org-entry-property-inherited-from (point))
16069 (or (org-up-heading-safe) (throw 'ex nil)))))
16070 (or tmp (cdr (assoc property org-local-properties))
16071 (cdr (assoc property org-global-properties))))))
16073 (defun org-entry-put (pom property value)
16074 "Set PROPERTY to VALUE for entry at point-or-marker POM."
16075 (org-with-point-at pom
16076 (org-back-to-heading t)
16077 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
16080 ((equal property "TODO")
16081 (when (and (stringp value) (string-match "\\S-" value)
16082 (not (member value org-todo-keywords-1)))
16083 (error "\"%s\" is not a valid TODO state" value))
16084 (if (or (not value)
16085 (not (string-match "\\S-" value)))
16086 (setq value 'none))
16088 (org-set-tags nil 'align))
16089 ((equal property "PRIORITY")
16090 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
16091 (string-to-char value) ?\ ))
16092 (org-set-tags nil 'align))
16093 ((equal property "SCHEDULED")
16094 (if (re-search-forward org-scheduled-time-regexp end t)
16096 ((eq value 'earlier) (org-timestamp-change -1 'day))
16097 ((eq value 'later) (org-timestamp-change 1 'day))
16098 (t (call-interactively 'org-schedule)))
16099 (call-interactively 'org-schedule)))
16100 ((equal property "DEADLINE")
16101 (if (re-search-forward org-deadline-time-regexp end t)
16103 ((eq value 'earlier) (org-timestamp-change -1 'day))
16104 ((eq value 'later) (org-timestamp-change 1 'day))
16105 (t (call-interactively 'org-deadline)))
16106 (call-interactively 'org-deadline)))
16107 ((member property org-special-properties)
16108 (error "The %s property can not yet be set with `org-entry-put'"
16110 (t ; a non-special property
16111 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
16112 (setq range (org-get-property-block beg end 'force))
16113 (goto-char (car range))
16114 (if (re-search-forward
16115 (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
16117 (delete-region (match-beginning 1) (match-end 1))
16118 (goto-char (match-beginning 1)))
16119 (goto-char (cdr range))
16122 (org-indent-line-function)
16123 (insert ":" property ":"))
16124 (and value (insert " " value))
16125 (org-indent-line-function)))))))
16127 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
16128 "Get all property keys in the current buffer.
16129 With INCLUDE-SPECIALS, also list the special properties that relect things
16130 like tags and TODO state.
16131 With INCLUDE-DEFAULTS, also include properties that has special meaning
16132 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
16133 With INCLUDE-COLUMNS, also include property names given in COLUMN
16134 formats in the current buffer."
16135 (let (rtn range cfmt cols s p)
16139 (goto-char (point-min))
16140 (while (re-search-forward org-property-start-re nil t)
16141 (setq range (org-get-property-block))
16142 (goto-char (car range))
16143 (while (re-search-forward
16144 (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
16146 (add-to-list 'rtn (org-match-string-no-properties 1)))
16147 (outline-next-heading))))
16149 (when include-specials
16150 (setq rtn (append org-special-properties rtn)))
16152 (when include-defaults
16153 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
16155 (when include-columns
16159 (goto-char (point-min))
16160 (while (re-search-forward
16161 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
16163 (setq cfmt (match-string 2) s 0)
16164 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
16166 (setq s (match-end 0)
16167 p (match-string 1 cfmt))
16168 (unless (or (equal p "ITEM")
16169 (member p org-special-properties))
16170 (add-to-list 'rtn (match-string 1 cfmt))))))))
16172 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
16174 (defun org-property-values (key)
16175 "Return a list of all values of property KEY."
16179 (goto-char (point-min))
16180 (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
16182 (while (re-search-forward re nil t)
16183 (add-to-list 'values (org-trim (match-string 1))))
16184 (delete "" values)))))
16186 (defun org-insert-property-drawer ()
16187 "Insert a property drawer into the current entry."
16189 (org-back-to-heading t)
16190 (looking-at outline-regexp)
16191 (let ((indent (- (match-end 0)(match-beginning 0)))
16193 (re (concat "^[ \t]*" org-keyword-time-regexp))
16195 (outline-next-heading)
16198 (while (re-search-forward re end t))
16199 (setq hiddenp (org-invisible-p))
16201 (and (equal (char-after) ?\n) (forward-char 1))
16202 (org-skip-over-state-notes)
16203 (skip-chars-backward " \t\n\r")
16204 (if (eq (char-before) ?*) (forward-char 1))
16205 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
16206 (beginning-of-line 0)
16207 (indent-to-column indent)
16208 (beginning-of-line 2)
16209 (indent-to-column indent)
16210 (beginning-of-line 0)
16213 (org-back-to-heading t)
16215 (org-flag-drawer t))))
16217 (defun org-set-property (property value)
16218 "In the current entry, set PROPERTY to VALUE.
16219 When called interactively, this will prompt for a property name, offering
16220 completion on existing and default properties. And then it will prompt
16221 for a value, offering competion either on allowed values (via an inherited
16222 xxx_ALL property) or on existing values in other instances of this property
16223 in the current file."
16225 (let* ((prop (completing-read
16226 "Property: " (mapcar 'list (org-buffer-property-keys nil t t))))
16227 (cur (org-entry-get nil prop))
16228 (allowed (org-property-get-allowed-values nil prop 'table))
16229 (existing (mapcar 'list (org-property-values prop)))
16231 (completing-read "Value: " allowed nil 'req-match)
16233 (concat "Value" (if (and cur (string-match "\\S-" cur))
16234 (concat "[" cur "]") "")
16236 existing nil nil "" nil cur))))
16237 (list prop (if (equal val "") cur val))))
16238 (unless (equal (org-entry-get nil property) value)
16239 (org-entry-put nil property value)))
16241 (defun org-delete-property (property)
16242 "In the current entry, delete PROPERTY."
16244 (let* ((prop (completing-read
16245 "Property: " (org-entry-properties nil 'standard))))
16247 (message "Property %s %s" property
16248 (if (org-entry-delete nil property)
16250 "was not present in the entry")))
16252 (defun org-delete-property-globally (property)
16253 "Remove PROPERTY globally, from all entries."
16255 (let* ((prop (completing-read
16256 "Globally remove property: "
16257 (mapcar 'list (org-buffer-property-keys)))))
16262 (goto-char (point-min))
16264 (while (re-search-forward
16265 (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
16267 (setq cnt (1+ cnt))
16268 (replace-match ""))
16269 (message "Property \"%s\" removed from %d entries" property cnt)))))
16271 (defvar org-columns-current-fmt-compiled) ; defined below
16273 (defun org-compute-property-at-point ()
16274 "Compute the property at point.
16275 This looks for an enclosing column format, extracts the operator and
16276 then applies it to the proerty in the column format's scope."
16278 (unless (org-at-property-p)
16279 (error "Not at a property"))
16280 (let ((prop (org-match-string-no-properties 2)))
16281 (org-columns-get-format-and-top-level)
16282 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
16283 (error "No operator defined for property %s" prop))
16284 (org-columns-compute prop)))
16286 (defun org-property-get-allowed-values (pom property &optional table)
16287 "Get allowed values for the property PROPERTY.
16288 When TABLE is non-nil, return an alist that can directly be used for
16292 ((equal property "TODO")
16293 (setq vals (org-with-point-at pom
16294 (append org-todo-keywords-1 '("")))))
16295 ((equal property "PRIORITY")
16296 (let ((n org-lowest-priority))
16297 (while (>= n org-highest-priority)
16298 (push (char-to-string n) vals)
16300 ((member property org-special-properties))
16302 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
16304 (when (and vals (string-match "\\S-" vals))
16305 (setq vals (car (read-from-string (concat "(" vals ")"))))
16306 (setq vals (mapcar (lambda (x)
16307 (cond ((stringp x) x)
16308 ((numberp x) (number-to-string x))
16309 ((symbolp x) (symbol-name x))
16312 (if table (mapcar 'list vals) vals)))
16314 (defun org-property-previous-allowed-value (&optional previous)
16315 "Switch to the next allowed value for this property."
16317 (org-property-next-allowed-value t))
16319 (defun org-property-next-allowed-value (&optional previous)
16320 "Switch to the next allowed value for this property."
16322 (unless (org-at-property-p)
16323 (error "Not at a property"))
16324 (let* ((key (match-string 2))
16325 (value (match-string 3))
16326 (allowed (or (org-property-get-allowed-values (point) key)
16327 (and (member value '("[ ]" "[-]" "[X]"))
16331 (error "Allowed values for this property have not been defined"))
16332 (if previous (setq allowed (reverse allowed)))
16333 (if (member value allowed)
16334 (setq nval (car (cdr (member value allowed)))))
16335 (setq nval (or nval (car allowed)))
16336 (if (equal nval value)
16337 (error "Only one allowed value for this property"))
16338 (org-at-property-p)
16339 (replace-match (concat " :" key ": " nval) t t)
16340 (org-indent-line-function)
16341 (beginning-of-line 1)
16342 (skip-chars-forward " \t")))
16344 (defun org-find-entry-with-id (ident)
16345 "Locate the entry that contains the ID property with exact value IDENT.
16346 IDENT can be a string, a symbol or a number, this function will search for
16347 the string representation of it.
16348 Return the position where this entry starts, or nil if there is no such entry."
16350 ((stringp ident) ident)
16351 ((symbol-name ident) (symbol-name ident))
16352 ((numberp ident) (number-to-string ident))
16353 (t (error "IDENT %s must be a string, symbol or number" ident))))
16354 (case-fold-search nil))
16357 (goto-char (point-min))
16358 (when (re-search-forward
16359 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
16361 (org-back-to-heading)
16366 (defvar org-columns-overlays nil
16367 "Holds the list of current column overlays.")
16369 (defvar org-columns-current-fmt nil
16370 "Local variable, holds the currently active column format.")
16371 (defvar org-columns-current-fmt-compiled nil
16372 "Local variable, holds the currently active column format.
16373 This is the compiled version of the format.")
16374 (defvar org-columns-current-widths nil
16375 "Loval variable, holds the currently widths of fields.")
16376 (defvar org-columns-current-maxwidths nil
16377 "Loval variable, holds the currently active maximum column widths.")
16378 (defvar org-columns-begin-marker (make-marker)
16379 "Points to the position where last a column creation command was called.")
16380 (defvar org-columns-top-level-marker (make-marker)
16381 "Points to the position where current columns region starts.")
16383 (defvar org-columns-map (make-sparse-keymap)
16384 "The keymap valid in column display.")
16386 (defun org-columns-content ()
16387 "Switch to contents view while in columns view."
16392 (org-defkey org-columns-map "c" 'org-columns-content)
16393 (org-defkey org-columns-map "o" 'org-overview)
16394 (org-defkey org-columns-map "e" 'org-columns-edit-value)
16395 (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
16396 (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
16397 (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
16398 (org-defkey org-columns-map "v" 'org-columns-show-value)
16399 (org-defkey org-columns-map "q" 'org-columns-quit)
16400 (org-defkey org-columns-map "r" 'org-columns-redo)
16401 (org-defkey org-columns-map "g" 'org-columns-redo)
16402 (org-defkey org-columns-map [left] 'backward-char)
16403 (org-defkey org-columns-map "\M-b" 'backward-char)
16404 (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
16405 (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
16406 (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
16407 (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
16408 (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
16409 (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
16410 (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
16411 (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
16412 (org-defkey org-columns-map "<" 'org-columns-narrow)
16413 (org-defkey org-columns-map ">" 'org-columns-widen)
16414 (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
16415 (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
16416 (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
16417 (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
16419 (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
16421 ["Edit property" org-columns-edit-value t]
16422 ["Next allowed value" org-columns-next-allowed-value t]
16423 ["Previous allowed value" org-columns-previous-allowed-value t]
16424 ["Show full value" org-columns-show-value t]
16425 ["Edit allowed values" org-columns-edit-allowed t]
16427 ["Edit column attributes" org-columns-edit-attributes t]
16428 ["Increase column width" org-columns-widen t]
16429 ["Decrease column width" org-columns-narrow t]
16431 ["Move column right" org-columns-move-right t]
16432 ["Move column left" org-columns-move-left t]
16433 ["Add column" org-columns-new t]
16434 ["Delete column" org-columns-delete t]
16436 ["CONTENTS" org-columns-content t]
16437 ["OVERVIEW" org-overview t]
16438 ["Refresh columns display" org-columns-redo t]
16440 ["Open link" org-columns-open-link t]
16442 ["Quit" org-columns-quit t]))
16444 (defun org-columns-new-overlay (beg end &optional string face)
16445 "Create a new column overlay and add it to the list."
16446 (let ((ov (org-make-overlay beg end)))
16447 (org-overlay-put ov 'face (or face 'secondary-selection))
16448 (org-overlay-display ov string face)
16449 (push ov org-columns-overlays)
16452 (defun org-columns-display-here (&optional props)
16453 "Overlay the current line with column display."
16455 (let* ((fmt org-columns-current-fmt-compiled)
16456 (beg (point-at-bol))
16457 (level-face (save-excursion
16458 (beginning-of-line 1)
16459 (and (looking-at "\\(\\**\\)\\(\\* \\)")
16460 (org-get-level-face 2))))
16461 (color (list :foreground
16462 (face-attribute (or level-face 'default) :foreground)))
16463 props pom property ass width f string ov column val modval)
16464 ;; Check if the entry is in another buffer.
16466 (if (eq major-mode 'org-agenda-mode)
16467 (setq pom (or (get-text-property (point) 'org-hd-marker)
16468 (get-text-property (point) 'org-marker))
16469 props (if pom (org-entry-properties pom) nil))
16470 (setq props (org-entry-properties nil))))
16472 (while (setq column (pop fmt))
16473 (setq property (car column)
16474 ass (if (equal property "ITEM")
16479 (buffer-substring-no-properties
16480 (point-at-bol) (point-at-eol))))))
16481 (assoc property props))
16482 width (or (cdr (assoc property org-columns-current-maxwidths))
16485 f (format "%%-%d.%ds | " width width)
16486 val (or (cdr ass) "")
16487 modval (if (equal property "ITEM")
16488 (org-columns-cleanup-item val org-columns-current-fmt-compiled))
16489 string (format f (or modval val)))
16490 ;; Create the overlay
16492 (setq ov (org-columns-new-overlay
16493 beg (setq beg (1+ beg)) string
16494 (list color 'org-column)))
16495 ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
16496 (org-overlay-put ov 'keymap org-columns-map)
16497 (org-overlay-put ov 'org-columns-key property)
16498 (org-overlay-put ov 'org-columns-value (cdr ass))
16499 (org-overlay-put ov 'org-columns-value-modified modval)
16500 (org-overlay-put ov 'org-columns-pom pom)
16501 (org-overlay-put ov 'org-columns-format f))
16502 (if (or (not (char-after beg))
16503 (equal (char-after beg) ?\n))
16504 (let ((inhibit-read-only t))
16507 (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
16508 ;; Make the rest of the line disappear.
16510 (setq ov (org-columns-new-overlay beg (point-at-eol)))
16511 (org-overlay-put ov 'invisible t)
16512 (org-overlay-put ov 'keymap org-columns-map)
16513 (org-overlay-put ov 'intangible t)
16514 (push ov org-columns-overlays)
16515 (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
16516 (org-overlay-put ov 'keymap org-columns-map)
16517 (push ov org-columns-overlays)
16518 (let ((inhibit-read-only t))
16519 (put-text-property (max (point-min) (1- (point-at-bol)))
16520 (min (point-max) (1+ (point-at-eol)))
16521 'read-only "Type `e' to edit property")))))
16523 (defvar org-previous-header-line-format nil
16524 "The header line format before column view was turned on.")
16525 (defvar org-columns-inhibit-recalculation nil
16526 "Inhibit recomputing of columns on column view startup.")
16529 (defvar header-line-format)
16530 (defun org-columns-display-here-title ()
16531 "Overlay the newline before the current line with the table title."
16533 (let ((fmt org-columns-current-fmt-compiled)
16535 property width f column str widths)
16536 (while (setq column (pop fmt))
16537 (setq property (car column)
16538 str (or (nth 1 column) property)
16539 width (or (cdr (assoc property org-columns-current-maxwidths))
16542 widths (push width widths)
16543 f (format "%%-%d.%ds | " width width)
16544 string (format f str)
16545 title (concat title string)))
16546 (setq title (concat
16547 (org-add-props " " nil 'display '(space :align-to 0))
16548 (org-add-props title nil 'face '(:weight bold :underline t))))
16549 (org-set-local 'org-previous-header-line-format header-line-format)
16550 (org-set-local 'org-columns-current-widths (nreverse widths))
16551 (setq header-line-format title)))
16553 (defun org-columns-remove-overlays ()
16554 "Remove all currently active column overlays."
16556 (when (marker-buffer org-columns-begin-marker)
16557 (with-current-buffer (marker-buffer org-columns-begin-marker)
16558 (when (local-variable-p 'org-previous-header-line-format)
16559 (setq header-line-format org-previous-header-line-format)
16560 (kill-local-variable 'org-previous-header-line-format))
16561 (move-marker org-columns-begin-marker nil)
16562 (move-marker org-columns-top-level-marker nil)
16564 (mapc 'org-delete-overlay org-columns-overlays)
16565 (setq org-columns-overlays nil)
16566 (let ((inhibit-read-only t))
16567 (remove-text-properties (point-min) (point-max) '(read-only t)))))))
16569 (defun org-columns-cleanup-item (item fmt)
16570 "Remove from ITEM what is a column in the format FMT."
16571 (if (not org-complex-heading-regexp)
16573 (when (string-match org-complex-heading-regexp item)
16575 (org-add-props (concat (match-string 1 item) " ") nil
16576 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
16577 (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
16578 (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
16579 " " (match-string 4 item)
16580 (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
16582 (defun org-columns-show-value ()
16583 "Show the full value of the property."
16585 (let ((value (get-char-property (point) 'org-columns-value)))
16586 (message "Value is: %s" (or value ""))))
16588 (defun org-columns-quit ()
16589 "Remove the column overlays and in this way exit column editing."
16592 (org-columns-remove-overlays)
16593 (let ((inhibit-read-only t))
16594 (remove-text-properties (point-min) (point-max) '(read-only t))))
16595 (when (eq major-mode 'org-agenda-mode)
16597 "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
16599 (defun org-columns-check-computed ()
16600 "Check if this column value is computed.
16601 If yes, throw an error indicating that changing it does not make sense."
16602 (let ((val (get-char-property (point) 'org-columns-value)))
16603 (when (and (stringp val)
16604 (get-char-property 0 'org-computed val))
16605 (error "This value is computed from the entry's children"))))
16607 (defun org-columns-todo (&optional arg)
16608 "Change the TODO state during column view."
16610 (org-columns-edit-value "TODO"))
16612 (defun org-columns-set-tags-or-toggle (&optional arg)
16613 "Toggle checkbox at point, or set tags for current headline."
16615 (if (string-match "\\`\\[[ xX-]\\]\\'"
16616 (get-char-property (point) 'org-columns-value))
16617 (org-columns-next-allowed-value)
16618 (org-columns-edit-value "TAGS")))
16620 (defun org-columns-edit-value (&optional key)
16621 "Edit the value of the property at point in column view.
16622 Where possible, use the standard interface for changing this line."
16624 (org-columns-check-computed)
16625 (let* ((external-key key)
16626 (col (current-column))
16627 (key (or key (get-char-property (point) 'org-columns-key)))
16628 (value (get-char-property (point) 'org-columns-value))
16629 (bol (point-at-bol)) (eol (point-at-eol))
16630 (pom (or (get-text-property bol 'org-hd-marker)
16631 (point))) ; keep despite of compiler waring
16633 (delq nil (mapcar (lambda (x)
16634 (and (eq (overlay-buffer x) (current-buffer))
16635 (>= (overlay-start x) bol)
16636 (<= (overlay-start x) eol)
16638 org-columns-overlays)))
16641 ((equal key "CLOCKSUM")
16642 (error "This special column cannot be edited"))
16643 ((equal key "ITEM")
16644 (setq eval '(org-with-point-at pom
16645 (org-edit-headline))))
16646 ((equal key "TODO")
16647 (setq eval '(org-with-point-at pom
16648 (let ((current-prefix-arg
16649 (if external-key current-prefix-arg '(4))))
16650 (call-interactively 'org-todo)))))
16651 ((equal key "PRIORITY")
16652 (setq eval '(org-with-point-at pom
16653 (call-interactively 'org-priority))))
16654 ((equal key "TAGS")
16655 (setq eval '(org-with-point-at pom
16656 (let ((org-fast-tag-selection-single-key
16657 (if (eq org-fast-tag-selection-single-key 'expert)
16658 t org-fast-tag-selection-single-key)))
16659 (call-interactively 'org-set-tags)))))
16660 ((equal key "DEADLINE")
16661 (setq eval '(org-with-point-at pom
16662 (call-interactively 'org-deadline))))
16663 ((equal key "SCHEDULED")
16664 (setq eval '(org-with-point-at pom
16665 (call-interactively 'org-schedule))))
16667 (setq allowed (org-property-get-allowed-values pom key 'table))
16669 (setq nval (completing-read "Value: " allowed nil t))
16670 (setq nval (read-string "Edit: " value)))
16671 (setq nval (org-trim nval))
16672 (when (not (equal nval value))
16673 (setq eval '(org-entry-put pom key nval)))))
16675 (let ((inhibit-read-only t))
16676 (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
16679 (setq org-columns-overlays
16680 (org-delete-all line-overlays org-columns-overlays))
16681 (mapc 'org-delete-overlay line-overlays)
16682 (org-columns-eval eval))
16683 (org-columns-display-here))))
16684 (move-to-column col)
16685 (if (and (org-mode-p)
16686 (nth 3 (assoc key org-columns-current-fmt-compiled)))
16687 (org-columns-update key))))
16689 (defun org-edit-headline () ; FIXME: this is not columns specific
16690 "Edit the current headline, the part without TODO keyword, TAGS."
16691 (org-back-to-heading)
16692 (when (looking-at org-todo-line-regexp)
16693 (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
16694 (txt (match-string 3))
16697 (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
16698 (setq post (match-string 0 txt)
16699 txt (substring txt 0 (match-beginning 0))))
16700 (setq txt2 (read-string "Edit: " txt))
16701 (when (not (equal txt txt2))
16702 (beginning-of-line 1)
16703 (insert pre txt2 post)
16704 (delete-region (point) (point-at-eol))
16705 (org-set-tags nil t)))))
16707 (defun org-columns-edit-allowed ()
16708 "Edit the list of allowed values for the current property."
16710 (let* ((key (get-char-property (point) 'org-columns-key))
16711 (key1 (concat key "_ALL"))
16712 (allowed (org-entry-get (point) key1 t))
16714 ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
16715 (setq nval (read-string "Allowed: " allowed))
16717 (cond ((marker-position org-entry-property-inherited-from)
16718 org-entry-property-inherited-from)
16719 ((marker-position org-columns-top-level-marker)
16720 org-columns-top-level-marker))
16723 (defmacro org-no-warnings (&rest body)
16724 (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
16726 (defun org-columns-eval (form)
16729 (beginning-of-line 1)
16730 ;; `next-line' is needed here, because it skips invisible line.
16731 (condition-case nil (org-no-warnings (next-line 1)) (error nil))
16732 (setq hidep (org-on-heading-p 1)))
16734 (and hidep (hide-entry))))
16736 (defun org-columns-previous-allowed-value ()
16737 "Switch to the previous allowed value for this column."
16739 (org-columns-next-allowed-value t))
16741 (defun org-columns-next-allowed-value (&optional previous)
16742 "Switch to the next allowed value for this column."
16744 (org-columns-check-computed)
16745 (let* ((col (current-column))
16746 (key (get-char-property (point) 'org-columns-key))
16747 (value (get-char-property (point) 'org-columns-value))
16748 (bol (point-at-bol)) (eol (point-at-eol))
16749 (pom (or (get-text-property bol 'org-hd-marker)
16750 (point))) ; keep despite of compiler waring
16752 (delq nil (mapcar (lambda (x)
16753 (and (eq (overlay-buffer x) (current-buffer))
16754 (>= (overlay-start x) bol)
16755 (<= (overlay-start x) eol)
16757 org-columns-overlays)))
16758 (allowed (or (org-property-get-allowed-values pom key)
16760 (nth 4 (assoc key org-columns-current-fmt-compiled))
16761 '(checkbox checkbox-n-of-m checkbox-percent))
16764 (when (equal key "ITEM")
16765 (error "Cannot edit item headline from here"))
16766 (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
16767 (error "Allowed values for this property have not been defined"))
16768 (if (member key '("SCHEDULED" "DEADLINE"))
16769 (setq nval (if previous 'earlier 'later))
16770 (if previous (setq allowed (reverse allowed)))
16771 (if (member value allowed)
16772 (setq nval (car (cdr (member value allowed)))))
16773 (setq nval (or nval (car allowed)))
16774 (if (equal nval value)
16775 (error "Only one allowed value for this property")))
16776 (let ((inhibit-read-only t))
16777 (remove-text-properties (1- bol) eol '(read-only t))
16780 (setq org-columns-overlays
16781 (org-delete-all line-overlays org-columns-overlays))
16782 (mapc 'org-delete-overlay line-overlays)
16783 (org-columns-eval '(org-entry-put pom key nval)))
16784 (org-columns-display-here)))
16785 (move-to-column col)
16786 (if (and (org-mode-p)
16787 (nth 3 (assoc key org-columns-current-fmt-compiled)))
16788 (org-columns-update key))))
16790 (defun org-verify-version (task)
16792 ((eq task 'columns)
16793 (if (or (featurep 'xemacs)
16794 (< emacs-major-version 22))
16795 (error "Emacs 22 is required for the columns feature")))))
16797 (defun org-columns-open-link (&optional arg)
16799 (let ((key (get-char-property (point) 'org-columns-key))
16800 (value (get-char-property (point) 'org-columns-value)))
16801 (org-open-link-from-string arg)))
16803 (defun org-open-link-from-string (s &optional arg)
16804 "Open a link in the string S, as if it was in Org-mode."
16807 (let ((org-inhibit-startup t))
16810 (goto-char (point-min))
16811 (org-open-at-point arg))))
16813 (defun org-columns-get-format-and-top-level ()
16815 (when (condition-case nil (org-back-to-heading) (error nil))
16816 (move-marker org-entry-property-inherited-from nil)
16817 (setq fmt (org-entry-get nil "COLUMNS" t)))
16818 (setq fmt (or fmt org-columns-default-format))
16819 (org-set-local 'org-columns-current-fmt fmt)
16820 (org-columns-compile-format fmt)
16821 (if (marker-position org-entry-property-inherited-from)
16822 (move-marker org-columns-top-level-marker
16823 org-entry-property-inherited-from)
16824 (move-marker org-columns-top-level-marker (point)))
16827 (defun org-columns ()
16828 "Turn on column view on an org-mode file."
16830 (org-verify-version 'columns)
16831 (org-columns-remove-overlays)
16832 (move-marker org-columns-begin-marker (point))
16833 (let (beg end fmt cache maxwidths clocksump)
16834 (setq fmt (org-columns-get-format-and-top-level))
16836 (goto-char org-columns-top-level-marker)
16838 (unless org-columns-inhibit-recalculation
16839 (org-columns-compute-all))
16840 (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
16842 ;; Get and cache the properties
16844 (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
16848 (narrow-to-region beg end)
16850 (while (re-search-forward (concat "^" outline-regexp) end t)
16851 (push (cons (org-current-line) (org-entry-properties)) cache))
16853 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
16854 (org-set-local 'org-columns-current-maxwidths maxwidths)
16855 (org-columns-display-here-title)
16857 (goto-line (car x))
16858 (org-columns-display-here (cdr x)))
16861 (defun org-columns-new (&optional prop title width op fmt &rest rest)
16862 "Insert a new column, to the leeft o the current column."
16864 (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
16866 (setq prop (completing-read
16867 "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
16869 (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
16870 (setq width (read-string "Column width: " (if width (number-to-string width))))
16871 (if (string-match "\\S-" width)
16872 (setq width (string-to-number width))
16874 (setq fmt (completing-read "Summary [none]: "
16875 '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
16877 (if (string-match "\\S-" fmt)
16878 (setq fmt (intern fmt))
16880 (if (eq fmt 'none) (setq fmt nil))
16883 (setcar editp prop)
16884 (setcdr editp (list title width nil fmt)))
16885 (setq cell (nthcdr (1- (current-column))
16886 org-columns-current-fmt-compiled))
16887 (setcdr cell (cons (list prop title width nil fmt)
16889 (org-columns-store-format)
16890 (org-columns-redo)))
16892 (defun org-columns-delete ()
16893 "Delete the column at point from columns view."
16895 (let* ((n (current-column))
16896 (title (nth 1 (nth n org-columns-current-fmt-compiled))))
16898 (format "Are you sure you want to remove column \"%s\"? " title))
16899 (setq org-columns-current-fmt-compiled
16900 (delq (nth n org-columns-current-fmt-compiled)
16901 org-columns-current-fmt-compiled))
16902 (org-columns-store-format)
16904 (if (>= (current-column) (length org-columns-current-fmt-compiled))
16905 (backward-char 1)))))
16907 (defun org-columns-edit-attributes ()
16908 "Edit the attributes of the current column."
16910 (let* ((n (current-column))
16911 (info (nth n org-columns-current-fmt-compiled)))
16912 (apply 'org-columns-new info)))
16914 (defun org-columns-widen (arg)
16915 "Make the column wider by ARG characters."
16918 (let* ((n (current-column))
16919 (entry (nth n org-columns-current-fmt-compiled))
16920 (width (or (nth 2 entry)
16921 (cdr (assoc (car entry) org-columns-current-maxwidths)))))
16922 (setq width (max 1 (+ width arg)))
16923 (setcar (nthcdr 2 entry) width)
16924 (org-columns-store-format)
16925 (org-columns-redo)))
16927 (defun org-columns-narrow (arg)
16928 "Make the column nrrower by ARG characters."
16930 (org-columns-widen (- arg)))
16932 (defun org-columns-move-right ()
16933 "Swap this column with the one to the right."
16935 (let* ((n (current-column))
16936 (cell (nthcdr n org-columns-current-fmt-compiled))
16938 (when (>= n (1- (length org-columns-current-fmt-compiled)))
16939 (error "Cannot shift this column further to the right"))
16940 (setq e (car cell))
16941 (setcar cell (car (cdr cell)))
16942 (setcdr cell (cons e (cdr (cdr cell))))
16943 (org-columns-store-format)
16947 (defun org-columns-move-left ()
16948 "Swap this column with the one to the left."
16950 (let* ((n (current-column)))
16952 (error "Cannot shift this column further to the left"))
16954 (org-columns-move-right)
16955 (backward-char 1)))
16957 (defun org-columns-store-format ()
16958 "Store the text version of the current columns format in appropriate place.
16959 This is either in the COLUMNS property of the node starting the current column
16960 display, or in the #+COLUMNS line of the current buffer."
16962 (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
16963 (org-set-local 'org-columns-current-fmt fmt)
16964 (if (marker-position org-columns-top-level-marker)
16966 (goto-char org-columns-top-level-marker)
16967 (if (and (org-at-heading-p)
16968 (org-entry-get nil "COLUMNS"))
16969 (org-entry-put nil "COLUMNS" fmt)
16970 (goto-char (point-min))
16971 ;; Overwrite all #+COLUMNS lines....
16972 (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
16973 (setq cnt (1+ cnt))
16974 (replace-match (concat "#+COLUMNS: " fmt) t t))
16976 (goto-char (point-min))
16977 (or (org-on-heading-p t) (outline-next-heading))
16978 (let ((inhibit-read-only t))
16979 (insert-before-markers "#+COLUMNS: " fmt "\n")))
16980 (org-set-local 'org-columns-default-format fmt))))))
16982 (defvar org-overriding-columns-format nil
16983 "When set, overrides any other definition.")
16984 (defvar org-agenda-view-columns-initially nil
16985 "When set, switch to columns view immediately after creating the agenda.")
16987 (defun org-agenda-columns ()
16988 "Turn on column view in the agenda."
16990 (org-verify-version 'columns)
16991 (org-columns-remove-overlays)
16992 (move-marker org-columns-begin-marker (point))
16993 (let (fmt cache maxwidths m)
16995 ((and (local-variable-p 'org-overriding-columns-format)
16996 org-overriding-columns-format)
16997 (setq fmt org-overriding-columns-format))
16998 ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
16999 (setq fmt (org-entry-get m "COLUMNS" t)))
17000 ((and (boundp 'org-columns-current-fmt)
17001 (local-variable-p 'org-columns-current-fmt)
17002 org-columns-current-fmt)
17003 (setq fmt org-columns-current-fmt))
17004 ((setq m (next-single-property-change (point-min) 'org-hd-marker))
17005 (setq m (get-text-property m 'org-hd-marker))
17006 (setq fmt (org-entry-get m "COLUMNS" t))))
17007 (setq fmt (or fmt org-columns-default-format))
17008 (org-set-local 'org-columns-current-fmt fmt)
17009 (org-columns-compile-format fmt)
17011 ;; Get and cache the properties
17012 (goto-char (point-min))
17013 (while (not (eobp))
17014 (when (setq m (or (get-text-property (point) 'org-hd-marker)
17015 (get-text-property (point) 'org-marker)))
17016 (push (cons (org-current-line) (org-entry-properties m)) cache))
17017 (beginning-of-line 2))
17019 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
17020 (org-set-local 'org-columns-current-maxwidths maxwidths)
17021 (org-columns-display-here-title)
17023 (goto-line (car x))
17024 (org-columns-display-here (cdr x)))
17027 (defun org-columns-get-autowidth-alist (s cache)
17028 "Derive the maximum column widths from the format and the cache."
17029 (let ((start 0) rtn)
17030 (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
17031 (push (cons (match-string 1 s) 1) rtn)
17032 (setq start (match-end 0)))
17034 (setcdr x (apply 'max
17037 (length (or (cdr (assoc (car x) (cdr y))) " ")))
17042 (defun org-columns-compute-all ()
17043 "Compute all columns that have operators defined."
17045 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
17046 (let ((columns org-columns-current-fmt-compiled) col)
17047 (while (setq col (pop columns))
17050 (org-columns-compute (car col)))))))
17052 (defun org-columns-update (property)
17053 "Recompute PROPERTY, and update the columns display for it."
17054 (org-columns-compute property)
17058 (when (equal (org-overlay-get ov 'org-columns-key) property)
17059 (setq pos (org-overlay-start ov))
17061 (when (setq val (cdr (assoc property
17063 (point-at-bol) 'org-summaries))))
17064 (setq fmt (org-overlay-get ov 'org-columns-format))
17065 (org-overlay-put ov 'org-columns-value val)
17066 (org-overlay-put ov 'display (format fmt val)))))
17067 org-columns-overlays))))
17069 (defun org-columns-compute (property)
17070 "Sum the values of property PROPERTY hierarchically, for the entire buffer."
17072 (let* ((re (concat "^" outline-regexp))
17073 (lmax 30) ; Does anyone use deeper levels???
17074 (lsum (make-vector lmax 0))
17075 (lflag (make-vector lmax nil))
17077 (ass (assoc property org-columns-current-fmt-compiled))
17078 (format (nth 4 ass))
17079 (printf (nth 5 ass))
17080 (beg org-columns-top-level-marker)
17081 last-level val valflag flag end sumpos sum-alist sum str str1 useval)
17083 ;; Find the region to compute
17085 (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
17087 ;; Walk the tree from the back and do the computations
17088 (while (re-search-backward re beg t)
17089 (setq sumpos (match-beginning 0)
17091 level (org-outline-level)
17092 val (org-entry-get nil property)
17093 valflag (and val (string-match "\\S-" val)))
17095 ((< level last-level)
17096 ;; put the sum of lower levels here as a property
17097 (setq sum (aref lsum last-level) ; current sum
17098 flag (aref lflag last-level) ; any valid entries from children?
17099 str (org-column-number-to-string sum format printf)
17100 str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
17101 useval (if flag str1 (if valflag val ""))
17102 sum-alist (get-text-property sumpos 'org-summaries))
17103 (if (assoc property sum-alist)
17104 (setcdr (assoc property sum-alist) useval)
17105 (push (cons property useval) sum-alist)
17107 (add-text-properties sumpos (1+ sumpos)
17108 (list 'org-summaries sum-alist))))
17110 (org-entry-put nil property (if flag str val)))
17111 ;; add current to current level accumulator
17112 (when (or flag valflag)
17113 (aset lsum level (+ (aref lsum level)
17114 (if flag sum (org-column-string-to-number
17115 (if flag str val) format))))
17116 (aset lflag level t))
17117 ;; clear accumulators for deeper levels
17118 (loop for l from (1+ level) to (1- lmax) do
17120 (aset lflag l nil)))
17121 ((>= level last-level)
17122 ;; add what we have here to the accumulator for this level
17123 (aset lsum level (+ (aref lsum level)
17124 (org-column-string-to-number (or val "0") format)))
17125 (and valflag (aset lflag level t)))
17126 (t (error "This should not happen")))))))
17128 (defun org-columns-redo ()
17129 "Construct the column display again."
17131 (message "Recomputing columns...")
17133 (if (marker-position org-columns-begin-marker)
17134 (goto-char org-columns-begin-marker))
17135 (org-columns-remove-overlays)
17137 (call-interactively 'org-columns)
17138 (call-interactively 'org-agenda-columns)))
17139 (message "Recomputing columns...done"))
17141 (defun org-columns-not-in-agenda ()
17142 (if (eq major-mode 'org-agenda-mode)
17143 (error "This command is only allowed in Org-mode buffers")))
17146 (defun org-string-to-number (s)
17147 "Convert string to number, and interpret hh:mm:ss."
17148 (if (not (string-match ":" s))
17149 (string-to-number s)
17150 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
17152 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
17155 (defun org-column-number-to-string (n fmt &optional printf)
17156 "Convert a computed column number to a string value, according to FMT."
17158 ((eq fmt 'add_times)
17159 (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
17160 (format "%d:%02d" h m)))
17161 ((eq fmt 'checkbox)
17162 (cond ((= n (floor n)) "[X]")
17165 ((memq fmt '(checkbox-n-of-m checkbox-percent))
17166 (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
17167 (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
17168 (printf (format printf n))
17169 ((eq fmt 'currency)
17171 (t (number-to-string n))))
17173 (defun org-nofm-to-completion (n m &optional percent)
17175 (format "[%d/%d]" n m)
17176 (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
17178 (defun org-column-string-to-number (s fmt)
17179 "Convert a column value to a number that can be used for column computing."
17181 ((string-match ":" s)
17182 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
17184 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
17186 ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
17187 (if (equal s "[X]") 1. 0.000001))
17188 (t (string-to-number s))))
17190 (defun org-columns-uncompile-format (cfmt)
17191 "Turn the compiled columns format back into a string representation."
17192 (let ((rtn "") e s prop title op width fmt printf)
17193 (while (setq e (pop cfmt))
17201 ((eq fmt 'add_times) (setq op ":"))
17202 ((eq fmt 'checkbox) (setq op "X"))
17203 ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
17204 ((eq fmt 'checkbox-percent) (setq op "X%"))
17205 ((eq fmt 'add_numbers) (setq op "+"))
17206 ((eq fmt 'currency) (setq op "$")))
17207 (if (and op printf) (setq op (concat op ";" printf)))
17208 (if (equal title prop) (setq title nil))
17209 (setq s (concat "%" (if width (number-to-string width))
17211 (if title (concat "(" title ")"))
17212 (if op (concat "{" op "}"))))
17213 (setq rtn (concat rtn " " s)))
17216 (defun org-columns-compile-format (fmt)
17217 "Turn a column format string into an alist of specifications.
17218 The alist has one entry for each column in the format. The elements of
17220 property the property
17221 title the title field for the columns
17222 width the column width in characters, can be nil for automatic
17223 operator the operator if any
17224 format the output format for computed results, derived from operator
17225 printf a printf format for computed values"
17226 (let ((start 0) width prop title op f printf)
17227 (setq org-columns-current-fmt-compiled nil)
17228 (while (string-match
17229 (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
17231 (setq start (match-end 0)
17232 width (match-string 1 fmt)
17233 prop (match-string 2 fmt)
17234 title (or (match-string 3 fmt) prop)
17235 op (match-string 4 fmt)
17238 (if width (setq width (string-to-number width)))
17239 (when (and op (string-match ";" op))
17240 (setq printf (substring op (match-end 0))
17241 op (substring op 0 (match-beginning 0))))
17243 ((equal op "+") (setq f 'add_numbers))
17244 ((equal op "$") (setq f 'currency))
17245 ((equal op ":") (setq f 'add_times))
17246 ((equal op "X") (setq f 'checkbox))
17247 ((equal op "X/") (setq f 'checkbox-n-of-m))
17248 ((equal op "X%") (setq f 'checkbox-percent))
17250 (push (list prop title width op f printf) org-columns-current-fmt-compiled))
17251 (setq org-columns-current-fmt-compiled
17252 (nreverse org-columns-current-fmt-compiled))))
17255 ;;; Dynamic block for Column view
17257 (defun org-columns-capture-view ()
17258 "Get the column view of the current buffer and return it as a list.
17259 The list will contains the title row and all other rows. Each row is
17262 (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
17263 (n (length title)) row tbl)
17264 (goto-char (point-min))
17265 (while (re-search-forward "^\\*+ " nil t)
17266 (when (get-char-property (match-beginning 0) 'org-columns-key)
17268 (loop for i from 0 to (1- n) do
17269 (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
17270 (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
17273 (setq row (nreverse row))
17275 (append (list title 'hline) (nreverse tbl)))))
17277 (defun org-dblock-write:columnview (params)
17278 "Write the column view table.
17279 PARAMS is a property list of parameters:
17281 :width enforce same column widths with <N> specifiers.
17282 :id the :ID: property of the entry where the columns view
17283 should be built, as a string. When `local', call locally.
17284 When `global' call column view with the cursor at the beginning
17285 of the buffer (usually this means that the whole buffer switches
17287 :hlines When t, insert a hline before each item. When a number, insert
17288 a hline before each level <= that number.
17289 :vlines When t, make each column a colgroup to enforce vertical lines."
17290 (let ((pos (move-marker (make-marker) (point)))
17291 (hlines (plist-get params :hlines))
17292 (vlines (plist-get params :vlines))
17293 tbl id idpos nfields tmp)
17296 (when (setq id (plist-get params :id))
17297 (cond ((not id) nil)
17298 ((eq id 'global) (goto-char (point-min)))
17299 ((eq id 'local) nil)
17300 ((setq idpos (org-find-entry-with-id id))
17302 (t (error "Cannot find entry with :ID: %s" id))))
17304 (setq tbl (org-columns-capture-view))
17305 (setq nfields (length (car tbl)))
17306 (org-columns-quit)))
17308 (move-marker pos nil)
17310 (when (plist-get params :hlines)
17313 (if (eq (car tbl) 'hline)
17314 (push (pop tbl) tmp)
17315 (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
17316 (if (and (not (eq (car tmp) 'hline))
17318 (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
17319 (push 'hline tmp)))
17320 (push (pop tbl) tmp)))
17321 (setq tbl (nreverse tmp)))
17323 (setq tbl (mapcar (lambda (x)
17324 (if (eq 'hline x) x (cons "" x)))
17326 (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
17328 (insert (org-listtable-to-string tbl))
17329 (when (plist-get params :width)
17330 (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
17331 org-columns-current-widths "|")))
17333 (org-table-align))))
17335 (defun org-listtable-to-string (tbl)
17336 "Convert a listtable TBL to a string that contains the Org-mode table.
17337 The table still need to be alligned. The resulting string has no leading
17338 and tailing newline characters."
17343 (concat "|" (mapconcat 'identity x "|") "|"))
17344 ((eq x 'hline) "|-|")
17345 (t (error "Garbage in listtable: %s" x))))
17348 (defun org-insert-columns-dblock ()
17349 "Create a dynamic block capturing a column view table."
17351 (let ((defaults '(:name "columnview" :hlines 1))
17352 (id (completing-read
17353 "Capture columns (local, global, entry with :ID: property) [local]: "
17354 (append '(("global") ("local"))
17355 (mapcar 'list (org-property-values "ID"))))))
17356 (if (equal id "") (setq id 'local))
17357 (if (equal id "global") (setq id 'global))
17358 (setq defaults (append defaults (list :id id)))
17359 (org-create-dblock defaults)
17360 (org-update-dblock)))
17364 (defvar org-last-changed-timestamp nil)
17365 (defvar org-time-was-given) ; dynamically scoped parameter
17366 (defvar org-end-time-was-given) ; dynamically scoped parameter
17367 (defvar org-ts-what) ; dynamically scoped parameter
17369 (defun org-time-stamp (arg)
17370 "Prompt for a date/time and insert a time stamp.
17371 If the user specifies a time like HH:MM, or if this command is called
17372 with a prefix argument, the time stamp will contain date and time.
17373 Otherwise, only the date will be included. All parts of a date not
17374 specified by the user will be filled in from the current date/time.
17375 So if you press just return without typing anything, the time stamp
17376 will represent the current date/time. If there is already a timestamp
17377 at the cursor, it will be modified."
17381 ;; Default time is either today, or, when entering a range,
17382 ;; the range start.
17383 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
17385 (re-search-backward
17386 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
17387 (- (point) 20) t)))
17388 (apply 'encode-time (org-parse-time-string (match-string 1)))
17390 (default-input (and ts (org-get-compact-tod ts)))
17391 org-time-was-given org-end-time-was-given time)
17393 ((and (org-at-timestamp-p)
17394 (eq last-command 'org-time-stamp)
17395 (eq this-command 'org-time-stamp))
17397 (setq time (let ((this-command this-command))
17398 (org-read-date arg 'totime nil nil default-time default-input)))
17399 (org-insert-time-stamp time (or org-time-was-given arg)))
17400 ((org-at-timestamp-p)
17401 (setq time (let ((this-command this-command))
17402 (org-read-date arg 'totime nil nil default-time default-input)))
17403 (when (org-at-timestamp-p) ; just to get the match data
17405 (setq org-last-changed-timestamp
17406 (org-insert-time-stamp
17407 time (or org-time-was-given arg)
17408 nil nil nil (list org-end-time-was-given))))
17409 (message "Timestamp updated"))
17411 (setq time (let ((this-command this-command))
17412 (org-read-date arg 'totime nil nil default-time default-input)))
17413 (org-insert-time-stamp time (or org-time-was-given arg)
17414 nil nil nil (list org-end-time-was-given))))))
17416 ;; FIXME: can we use this for something else????
17417 ;; like computing time differences?????
17418 (defun org-get-compact-tod (s)
17419 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
17420 (let* ((t1 (match-string 1 s))
17421 (h1 (string-to-number (match-string 2 s)))
17422 (m1 (string-to-number (match-string 3 s)))
17423 (t2 (and (match-end 4) (match-string 5 s)))
17424 (h2 (and t2 (string-to-number (match-string 6 s))))
17425 (m2 (and t2 (string-to-number (match-string 7 s))))
17429 (setq dh (- h2 h1) dm (- m2 m1))
17430 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
17431 (concat t1 "+" (number-to-string dh)
17432 (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
17434 (defun org-time-stamp-inactive (&optional arg)
17435 "Insert an inactive time stamp.
17436 An inactive time stamp is enclosed in square brackets instead of angle
17437 brackets. It is inactive in the sense that it does not trigger agenda entries,
17438 does not link to the calendar and cannot be changed with the S-cursor keys.
17439 So these are more for recording a certain time/date."
17441 (let (org-time-was-given org-end-time-was-given time)
17442 (setq time (org-read-date arg 'totime))
17443 (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
17444 nil nil (list org-end-time-was-given))))
17446 (defvar org-date-ovl (org-make-overlay 1 1))
17447 (org-overlay-put org-date-ovl 'face 'org-warning)
17448 (org-detach-overlay org-date-ovl)
17450 (defvar org-ans1) ; dynamically scoped parameter
17451 (defvar org-ans2) ; dynamically scoped parameter
17453 (defvar org-plain-time-of-day-regexp) ; defined below
17455 (defvar org-read-date-overlay nil)
17456 (defvar org-dcst nil) ; dynamically scoped
17458 (defun org-read-date (&optional with-time to-time from-string prompt
17459 default-time default-input)
17460 "Read a date, possibly a time, and make things smooth for the user.
17461 The prompt will suggest to enter an ISO date, but you can also enter anything
17462 which will at least partially be understood by `parse-time-string'.
17463 Unrecognized parts of the date will default to the current day, month, year,
17464 hour and minute. If this command is called to replace a timestamp at point,
17465 of to enter the second timestamp of a range, the default time is taken from the
17466 existing stamp. For example,
17467 3-2-5 --> 2003-02-05
17468 feb 15 --> currentyear-02-15
17469 sep 12 9 --> 2009-09-12
17470 12:45 --> today 12:45
17471 22 sept 0:34 --> currentyear-09-22 0:34
17472 12 --> currentyear-currentmonth-12
17473 Fri --> nearest Friday (today or later)
17476 Furthermore you can specify a relative date by giving, as the *first* thing
17477 in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
17478 change in days weeks, months, years.
17479 With a single plus or minus, the date is relative to today. With a double
17480 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
17481 +4d --> four days from today
17482 +4 --> same as above
17483 +2w --> two weeks from today
17484 ++5 --> five days from default date
17486 The function understands only English month and weekday abbreviations,
17487 but this can be configured with the variables `parse-time-months' and
17488 `parse-time-weekdays'.
17490 While prompting, a calendar is popped up - you can also select the
17491 date with the mouse (button 1). The calendar shows a period of three
17492 months. To scroll it to other months, use the keys `>' and `<'.
17493 If you don't like the calendar, turn it off with
17494 \(setq org-read-date-popup-calendar nil)
17496 With optional argument TO-TIME, the date will immediately be converted
17497 to an internal time.
17498 With an optional argument WITH-TIME, the prompt will suggest to also
17499 insert a time. Note that when WITH-TIME is not set, you can still
17500 enter a time, and this function will inform the calling routine about
17501 this change. The calling routine may then choose to change the format
17502 used to insert the time stamp into the buffer to include the time.
17503 With optional argument FROM-STRING, read from this string instead from
17504 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
17505 the time/date that is used for everything that is not specified by the
17507 (require 'parse-time)
17508 (let* ((org-time-stamp-rounding-minutes
17509 (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
17510 (org-dcst org-display-custom-times)
17511 (ct (org-current-time))
17512 (def (or default-time ct))
17513 (defdecode (decode-time def))
17515 (when (< (nth 2 defdecode) org-extend-today-until)
17516 (setcar (nthcdr 2 defdecode) -1)
17517 (setcar (nthcdr 1 defdecode) 59)
17518 (setq def (apply 'encode-time defdecode)
17519 defdecode (decode-time def)))))
17520 (calendar-move-hook nil)
17521 (view-diary-entries-initially nil)
17522 (view-calendar-holidays-initially nil)
17523 (timestr (format-time-string
17524 (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
17525 (prompt (concat (if prompt (concat prompt " ") "")
17526 (format "Date+time [%s]: " timestr)))
17527 ans (org-ans0 "") org-ans1 org-ans2 final)
17530 (from-string (setq ans from-string))
17531 (org-read-date-popup-calendar
17533 (save-window-excursion
17535 (calendar-forward-day (- (time-to-days def)
17536 (calendar-absolute-from-gregorian
17537 (calendar-current-date))))
17538 (org-eval-in-calendar nil t)
17539 (let* ((old-map (current-local-map))
17540 (map (copy-keymap calendar-mode-map))
17541 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
17542 (org-defkey map (kbd "RET") 'org-calendar-select)
17543 (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
17544 'org-calendar-select-mouse)
17545 (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2
])
17546 'org-calendar-select-mouse
)
17547 (org-defkey minibuffer-local-map
[(meta shift left
)]
17548 (lambda () (interactive)
17549 (org-eval-in-calendar '(calendar-backward-month 1))))
17550 (org-defkey minibuffer-local-map
[(meta shift right
)]
17551 (lambda () (interactive)
17552 (org-eval-in-calendar '(calendar-forward-month 1))))
17553 (org-defkey minibuffer-local-map
[(meta shift up
)]
17554 (lambda () (interactive)
17555 (org-eval-in-calendar '(calendar-backward-year 1))))
17556 (org-defkey minibuffer-local-map
[(meta shift down
)]
17557 (lambda () (interactive)
17558 (org-eval-in-calendar '(calendar-forward-year 1))))
17559 (org-defkey minibuffer-local-map
[(shift up
)]
17560 (lambda () (interactive)
17561 (org-eval-in-calendar '(calendar-backward-week 1))))
17562 (org-defkey minibuffer-local-map
[(shift down
)]
17563 (lambda () (interactive)
17564 (org-eval-in-calendar '(calendar-forward-week 1))))
17565 (org-defkey minibuffer-local-map
[(shift left
)]
17566 (lambda () (interactive)
17567 (org-eval-in-calendar '(calendar-backward-day 1))))
17568 (org-defkey minibuffer-local-map
[(shift right
)]
17569 (lambda () (interactive)
17570 (org-eval-in-calendar '(calendar-forward-day 1))))
17571 (org-defkey minibuffer-local-map
">"
17572 (lambda () (interactive)
17573 (org-eval-in-calendar '(scroll-calendar-left 1))))
17574 (org-defkey minibuffer-local-map
"<"
17575 (lambda () (interactive)
17576 (org-eval-in-calendar '(scroll-calendar-right 1))))
17579 (use-local-map map
)
17580 (add-hook 'post-command-hook
'org-read-date-display
)
17581 (setq org-ans0
(read-string prompt default-input nil nil
))
17582 ;; org-ans0: from prompt
17583 ;; org-ans1: from mouse click
17584 ;; org-ans2: from calendar motion
17585 (setq ans
(concat org-ans0
" " (or org-ans1 org-ans2
))))
17586 (remove-hook 'post-command-hook
'org-read-date-display
)
17587 (use-local-map old-map
)
17588 (when org-read-date-overlay
17589 (org-delete-overlay org-read-date-overlay
)
17590 (setq org-read-date-overlay nil
)))))))
17592 (t ; Naked prompt only
17594 (setq ans
(read-string prompt default-input nil timestr
))
17595 (when org-read-date-overlay
17596 (org-delete-overlay org-read-date-overlay
)
17597 (setq org-read-date-overlay nil
)))))
17599 (setq final
(org-read-date-analyze ans def defdecode
))
17602 (apply 'encode-time final
)
17603 (if (and (boundp 'org-time-was-given
) org-time-was-given
)
17604 (format "%04d-%02d-%02d %02d:%02d"
17605 (nth 5 final
) (nth 4 final
) (nth 3 final
)
17606 (nth 2 final
) (nth 1 final
))
17607 (format "%04d-%02d-%02d" (nth 5 final
) (nth 4 final
) (nth 3 final
))))))
17611 (defun org-read-date-display ()
17612 "Display the currrent date prompt interpretation in the minibuffer."
17613 (when org-read-date-display-live
17614 (when org-read-date-overlay
17615 (org-delete-overlay org-read-date-overlay
))
17618 (while (not (equal (buffer-substring
17619 (max (point-min) (- (point) 4)) (point))
17623 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
17624 " " (or org-ans1 org-ans2
)))
17625 (org-end-time-was-given nil
)
17626 (f (org-read-date-analyze ans def defdecode
))
17628 org-time-stamp-custom-formats
17629 org-time-stamp-formats
))
17630 (fmt (if (or with-time
17631 (and (boundp 'org-time-was-given
) org-time-was-given
))
17634 (txt (concat "=> " (format-time-string fmt
(apply 'encode-time f
)))))
17635 (when (and org-end-time-was-given
17636 (string-match org-plain-time-of-day-regexp txt
))
17637 (setq txt
(concat (substring txt
0 (match-end 0)) "-"
17638 org-end-time-was-given
17639 (substring txt
(match-end 0)))))
17640 (setq org-read-date-overlay
17641 (make-overlay (1- (point-at-eol)) (point-at-eol)))
17642 (org-overlay-display org-read-date-overlay txt
'secondary-selection
))))
17644 (defun org-read-date-analyze (ans def defdecode
)
17645 "Analyze the combined answer of the date prompt."
17646 ;; FIXME: cleanup and comment
17647 (let (delta deltan deltaw deltadef year month day
17648 hour minute second wday pm h2 m2 tl wday1
)
17650 (when (setq delta
(org-read-date-get-relative ans
(current-time) def
))
17651 (setq ans
(replace-match "" t t ans
)
17653 deltaw
(nth 1 delta
)
17654 deltadef
(nth 2 delta
)))
17656 ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
17657 (when (string-match
17658 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans
)
17659 (setq year
(if (match-end 2)
17660 (string-to-number (match-string 2 ans
))
17661 (string-to-number (format-time-string "%Y")))
17662 month
(string-to-number (match-string 3 ans
))
17663 day
(string-to-number (match-string 4 ans
)))
17664 (if (< year
100) (setq year
(+ 2000 year
)))
17665 (setq ans
(replace-match (format "%04d-%02d-%02d\\5" year month day
)
17667 ;; Help matching am/pm times, because `parse-time-string' does not do that.
17668 ;; If there is a time with am/pm, and *no* time without it, we convert
17669 ;; so that matching will be successful.
17670 (loop for i from
1 to
2 do
; twice, for end time as well
17671 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans
))
17672 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans
))
17673 (setq hour
(string-to-number (match-string 1 ans
))
17674 minute
(if (match-end 3)
17675 (string-to-number (match-string 3 ans
))
17678 (string-to-char (downcase (match-string 4 ans
)))))
17679 (if (and (= hour
12) (not pm
))
17681 (if (and pm
(< hour
12)) (setq hour
(+ 12 hour
))))
17682 (setq ans
(replace-match (format "%02d:%02d" hour minute
)
17685 ;; Check if a time range is given as a duration
17686 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans
)
17687 (setq hour
(string-to-number (match-string 1 ans
))
17688 h2
(+ hour
(string-to-number (match-string 3 ans
)))
17689 minute
(string-to-number (match-string 2 ans
))
17690 m2
(+ minute
(if (match-end 5) (string-to-number (match-string 5 ans
))0)))
17691 (if (>= m2
60) (setq h2
(1+ h2
) m2
(- m2
60)))
17692 (setq ans
(replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2
) t t ans
)))
17694 ;; Check if there is a time range
17695 (when (boundp 'org-end-time-was-given
)
17696 (setq org-time-was-given nil
)
17697 (when (and (string-match org-plain-time-of-day-regexp ans
)
17699 (setq org-end-time-was-given
(match-string 8 ans
))
17700 (setq ans
(concat (substring ans
0 (match-beginning 7))
17701 (substring ans
(match-end 7))))))
17703 (setq tl
(parse-time-string ans
)
17704 day
(or (nth 3 tl
) (nth 3 defdecode
))
17705 month
(or (nth 4 tl
)
17706 (if (and org-read-date-prefer-future
17707 (nth 3 tl
) (< (nth 3 tl
) (nth 3 defdecode
)))
17708 (1+ (nth 4 defdecode
))
17709 (nth 4 defdecode
)))
17710 year
(or (nth 5 tl
)
17711 (if (and org-read-date-prefer-future
17712 (nth 4 tl
) (< (nth 4 tl
) (nth 4 defdecode
)))
17713 (1+ (nth 5 defdecode
))
17714 (nth 5 defdecode
)))
17715 hour
(or (nth 2 tl
) (nth 2 defdecode
))
17716 minute
(or (nth 1 tl
) (nth 1 defdecode
))
17717 second
(or (nth 0 tl
) 0)
17721 (let ((now (decode-time (current-time))))
17722 (setq day
(nth 3 now
) month
(nth 4 now
) year
(nth 5 now
))))
17723 (cond ((member deltaw
'("d" "")) (setq day
(+ day deltan
)))
17724 ((equal deltaw
"w") (setq day
(+ day
(* 7 deltan
))))
17725 ((equal deltaw
"m") (setq month
(+ month deltan
)))
17726 ((equal deltaw
"y") (setq year
(+ year deltan
)))))
17727 (when (and wday
(not (nth 3 tl
)))
17728 ;; Weekday was given, but no day, so pick that day in the week
17729 ;; on or after the derived date.
17730 (setq wday1
(nth 6 (decode-time (encode-time 0 0 0 day month year
))))
17731 (unless (equal wday wday1
)
17732 (setq day
(+ day
(%
(- wday wday1 -
7) 7)))))
17733 (if (and (boundp 'org-time-was-given
)
17735 (setq org-time-was-given t
))
17736 (if (< year
100) (setq year
(+ 2000 year
)))
17737 (if (< year
1970) (setq year
(nth 5 defdecode
))) ; not representable
17738 (list second minute hour day month year
)))
17740 (defvar parse-time-weekdays
)
17742 (defun org-read-date-get-relative (s today default
)
17743 "Check string S for special relative date string.
17744 TODAY and DEFAULT are internal times, for today and for a default.
17745 Return shift list (N what def-flag)
17746 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
17747 N is the number of WHATs to shift.
17748 DEF-FLAG is t when a double ++ or -- indicates shift relative to
17749 the DEFAULT date rather than TODAY."
17750 (when (string-match
17752 "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
17754 "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays
"\\|") "\\)\\)?"
17755 "\\([ \t]\\|$\\)") s
)
17756 (let* ((dir (if (match-end 1)
17757 (string-to-char (substring (match-string 1 s
) -
1))
17759 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
17760 (n (if (match-end 2) (string-to-number (match-string 2 s
)) 1))
17761 (what (if (match-end 3) (match-string 3 s
) "d"))
17762 (wday1 (cdr (assoc (downcase what
) parse-time-weekdays
)))
17763 (date (if rel default today
))
17764 (wday (nth 6 (decode-time date
)))
17768 (setq delta
(mod (+ 7 (- wday1 wday
)) 7))
17769 (if (= dir ?-
) (setq delta
(- delta
7)))
17770 (if (> n
1) (setq delta
(+ delta
(* (1- n
) (if (= dir ?-
) -
7 7)))))
17771 (list delta
"d" rel
))
17772 (list (* n
(if (= dir ?-
) -
1 1)) what rel
)))))
17774 (defun org-eval-in-calendar (form &optional keepdate
)
17775 "Eval FORM in the calendar window and return to current window.
17776 Also, store the cursor date in variable org-ans2."
17777 (let ((sw (selected-window)))
17778 (select-window (get-buffer-window "*Calendar*"))
17780 (when (and (not keepdate
) (calendar-cursor-to-date))
17781 (let* ((date (calendar-cursor-to-date))
17782 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
17783 (setq org-ans2
(format-time-string "%Y-%m-%d" time
))))
17784 (org-move-overlay org-date-ovl
(1- (point)) (1+ (point)) (current-buffer))
17785 (select-window sw
)))
17787 ; ;; Update the prompt to show new default date
17789 ; (goto-char (point-min))
17790 ; (when (and org-ans2
17791 ; (re-search-forward "\\[[-0-9]+\\]" nil t)
17792 ; (get-text-property (match-end 0) 'field))
17793 ; (let ((inhibit-read-only t))
17794 ; (replace-match (concat "[" org-ans2 "]") t t)
17795 ; (add-text-properties (point-min) (1+ (match-end 0))
17796 ; (text-properties-at (1+ (point-min)))))))))
17798 (defun org-calendar-select ()
17799 "Return to `org-read-date' with the date currently selected.
17800 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
17802 (when (calendar-cursor-to-date)
17803 (let* ((date (calendar-cursor-to-date))
17804 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
17805 (setq org-ans1
(format-time-string "%Y-%m-%d" time
)))
17806 (if (active-minibuffer-window) (exit-minibuffer))))
17808 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra
)
17809 "Insert a date stamp for the date given by the internal TIME.
17810 WITH-HM means, use the stamp format that includes the time of the day.
17811 INACTIVE means use square brackets instead of angular ones, so that the
17812 stamp will not contribute to the agenda.
17813 PRE and POST are optional strings to be inserted before and after the
17815 The command returns the inserted time stamp."
17816 (let ((fmt (funcall (if with-hm
'cdr
'car
) org-time-stamp-formats
))
17818 (if inactive
(setq fmt
(concat "[" (substring fmt
1 -
1) "]")))
17819 (insert-before-markers (or pre
""))
17820 (insert-before-markers (setq stamp
(format-time-string fmt time
)))
17821 (when (listp extra
)
17822 (setq extra
(car extra
))
17823 (if (and (stringp extra
)
17824 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra
))
17825 (setq extra
(format "-%02d:%02d"
17826 (string-to-number (match-string 1 extra
))
17827 (string-to-number (match-string 2 extra
))))
17831 (insert-before-markers extra
)
17833 (insert-before-markers (or post
""))
17836 (defun org-toggle-time-stamp-overlays ()
17837 "Toggle the use of custom time stamp formats."
17839 (setq org-display-custom-times
(not org-display-custom-times
))
17840 (unless org-display-custom-times
17841 (let ((p (point-min)) (bmp (buffer-modified-p)))
17842 (while (setq p
(next-single-property-change p
'display
))
17843 (if (and (get-text-property p
'display
)
17844 (eq (get-text-property p
'face
) 'org-date
))
17845 (remove-text-properties
17846 p
(setq p
(next-single-property-change p
'display
))
17848 (set-buffer-modified-p bmp
)))
17849 (if (featurep 'xemacs
)
17850 (remove-text-properties (point-min) (point-max) '(end-glyph t
)))
17851 (org-restart-font-lock)
17852 (setq org-table-may-need-update t
)
17853 (if org-display-custom-times
17854 (message "Time stamps are overlayed with custom format")
17855 (message "Time stamp overlays removed")))
17857 (defun org-display-custom-time (beg end
)
17858 "Overlay modified time stamp format over timestamp between BED and END."
17859 (let* ((ts (buffer-substring beg end
))
17860 t1 w1 with-hm tf time str w2
(off 0))
17862 (setq t1
(org-parse-time-string ts t
))
17863 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( \\+[0-9]+[dwmy]\\)?\\'" ts
)
17864 (setq off
(- (match-end 0) (match-beginning 0)))))
17865 (setq end
(- end off
))
17866 (setq w1
(- end beg
)
17867 with-hm
(and (nth 1 t1
) (nth 2 t1
))
17868 tf
(funcall (if with-hm
'cdr
'car
) org-time-stamp-custom-formats
)
17869 time
(org-fix-decoded-time t1
)
17871 (format-time-string
17872 (substring tf
1 -
1) (apply 'encode-time time
))
17873 nil
'mouse-face
'highlight
)
17875 (if (not (= w2 w1
))
17876 (add-text-properties (1+ beg
) (+ 2 beg
)
17877 (list 'org-dwidth t
'org-dwidth-n
(- w1 w2
))))
17878 (if (featurep 'xemacs
)
17880 (put-text-property beg end
'invisible t
)
17881 (put-text-property beg end
'end-glyph
(make-glyph str
)))
17882 (put-text-property beg end
'display str
))))
17884 (defun org-translate-time (string)
17885 "Translate all timestamps in STRING to custom format.
17886 But do this only if the variable `org-display-custom-times' is set."
17887 (when org-display-custom-times
17890 (re org-ts-regexp-both
)
17891 t1 with-hm inactive tf time str beg end
)
17892 (while (setq start
(string-match re string start
))
17893 (setq beg
(match-beginning 0)
17895 t1
(save-match-data
17896 (org-parse-time-string (substring string beg end
) t
))
17897 with-hm
(and (nth 1 t1
) (nth 2 t1
))
17898 inactive
(equal (substring string beg
(1+ beg
)) "[")
17899 tf
(funcall (if with-hm
'cdr
'car
)
17900 org-time-stamp-custom-formats
)
17901 time
(org-fix-decoded-time t1
)
17902 str
(format-time-string
17904 (if inactive
"[" "<") (substring tf
1 -
1)
17905 (if inactive
"]" ">"))
17906 (apply 'encode-time time
))
17907 string
(replace-match str t t string
)
17908 start
(+ start
(length str
)))))))
17911 (defun org-fix-decoded-time (time)
17912 "Set 0 instead of nil for the first 6 elements of time.
17913 Don't touch the rest."
17915 (mapcar (lambda (x) (if (< (setq n
(1+ n
)) 7) (or x
0) x
)) time
)))
17917 (defun org-days-to-time (timestamp-string)
17918 "Difference between TIMESTAMP-STRING and now in days."
17919 (- (time-to-days (org-time-string-to-time timestamp-string
))
17920 (time-to-days (current-time))))
17922 (defun org-deadline-close (timestamp-string &optional ndays
)
17923 "Is the time in TIMESTAMP-STRING close to the current date?"
17924 (setq ndays
(or ndays
(org-get-wdays timestamp-string
)))
17925 (and (< (org-days-to-time timestamp-string
) ndays
)
17926 (not (org-entry-is-done-p))))
17928 (defun org-get-wdays (ts)
17929 "Get the deadline lead time appropriate for timestring TS."
17931 ((<= org-deadline-warning-days
0)
17932 ;; 0 or negative, enforce this value no matter what
17933 (- org-deadline-warning-days
))
17934 ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts
)
17935 ;; lead time is specified.
17936 (floor (* (string-to-number (match-string 1 ts
))
17937 (cdr (assoc (match-string 2 ts
)
17938 '(("d" .
1) ("w" .
7)
17939 ("m" .
30.4) ("y" .
365.25)))))))
17940 ;; go for the default.
17941 (t org-deadline-warning-days
)))
17943 (defun org-calendar-select-mouse (ev)
17944 "Return to `org-read-date' with the date currently selected.
17945 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
17947 (mouse-set-point ev
)
17948 (when (calendar-cursor-to-date)
17949 (let* ((date (calendar-cursor-to-date))
17950 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
17951 (setq org-ans1
(format-time-string "%Y-%m-%d" time
)))
17952 (if (active-minibuffer-window) (exit-minibuffer))))
17954 (defun org-check-deadlines (ndays)
17955 "Check if there are any deadlines due or past due.
17956 A deadline is considered due if it happens within `org-deadline-warning-days'
17957 days from today's date. If the deadline appears in an entry marked DONE,
17958 it is not shown. The prefix arg NDAYS can be used to test that many
17959 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
17961 (let* ((org-warn-days
17963 ((equal ndays
'(4)) 100000)
17964 (ndays (prefix-numeric-value ndays
))
17965 (t (abs org-deadline-warning-days
))))
17966 (case-fold-search nil
)
17967 (regexp (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>"))
17969 (lambda () (org-deadline-close (match-string 1) org-warn-days
))))
17971 (message "%d deadlines past-due or due within %d days"
17972 (org-occur regexp nil callback
)
17975 (defun org-check-before-date (date)
17976 "Check if there are deadlines or scheduled entries before DATE."
17977 (interactive (list (org-read-date)))
17978 (let ((case-fold-search nil
)
17979 (regexp (concat "\\<\\(" org-deadline-string
17980 "\\|" org-scheduled-string
17981 "\\) *<\\([^>]+\\)>"))
17983 (lambda () (time-less-p
17984 (org-time-string-to-time (match-string 2))
17985 (org-time-string-to-time date
)))))
17986 (message "%d entries before %s"
17987 (org-occur regexp nil callback
) date
)))
17989 (defun org-evaluate-time-range (&optional to-buffer
)
17990 "Evaluate a time range by computing the difference between start and end.
17991 Normally the result is just printed in the echo area, but with prefix arg
17992 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
17993 If the time range is actually in a table, the result is inserted into the
17995 For time difference computation, a year is assumed to be exactly 365
17996 days in order to avoid rounding problems."
17999 (org-clock-update-time-maybe)
18001 (unless (org-at-date-range-p t
)
18002 (goto-char (point-at-bol))
18003 (re-search-forward org-tr-regexp-both
(point-at-eol) t
))
18004 (if (not (org-at-date-range-p t
))
18005 (error "Not at a time-stamp range, and none found in current line")))
18006 (let* ((ts1 (match-string 1))
18007 (ts2 (match-string 2))
18008 (havetime (or (> (length ts1
) 15) (> (length ts2
) 15)))
18009 (match-end (match-end 0))
18010 (time1 (org-time-string-to-time ts1
))
18011 (time2 (org-time-string-to-time ts2
))
18012 (t1 (time-to-seconds time1
))
18013 (t2 (time-to-seconds time2
))
18014 (diff (abs (- t2 t1
)))
18015 (negative (< (- t2 t1
) 0))
18016 ;; (ys (floor (* 365 24 60 60)))
18019 (fy "%dy %dd %02d:%02d")
18021 (fd "%dd %02d:%02d")
18026 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
18028 d
(floor (/ diff ds
)) diff
(mod diff ds
)
18029 h
(floor (/ diff hs
)) diff
(mod diff hs
)
18030 m
(floor (/ diff
60)))
18031 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
18033 d
(floor (+ (/ diff ds
) 0.5))
18035 (if (not to-buffer
)
18036 (message "%s" (org-make-tdiff-string y d h m
))
18037 (if (org-at-table-p)
18039 (goto-char match-end
)
18041 (and (looking-at " *|") (goto-char (match-end 0))))
18042 (goto-char match-end
))
18044 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
18045 (replace-match ""))
18046 (if negative
(insert " -"))
18047 (if (> y
0) (insert " " (format (if havetime fy fy1
) y d h m
))
18048 (if (> d
0) (insert " " (format (if havetime fd fd1
) d h m
))
18049 (insert " " (format fh h m
))))
18050 (if align
(org-table-align))
18051 (message "Time difference inserted")))))
18053 (defun org-make-tdiff-string (y d h m
)
18056 (if (> y
0) (setq fmt
(concat fmt
"%d year" (if (> y
1) "s" "") " ")
18058 (if (> d
0) (setq fmt
(concat fmt
"%d day" (if (> d
1) "s" "") " ")
18060 (if (> h
0) (setq fmt
(concat fmt
"%d hour" (if (> h
1) "s" "") " ")
18062 (if (> m
0) (setq fmt
(concat fmt
"%d minute" (if (> m
1) "s" "") " ")
18064 (apply 'format fmt
(nreverse l
))))
18066 (defun org-time-string-to-time (s)
18067 (apply 'encode-time
(org-parse-time-string s
)))
18069 (defun org-time-string-to-absolute (s &optional daynr prefer
)
18070 "Convert a time stamp to an absolute day number.
18071 If there is a specifyer for a cyclic time stamp, get the closest date to
18074 ((and daynr
(string-match "\\`%%\\((.*)\\)" s
))
18075 (if (org-diary-sexp-entry (match-string 1 s
) "" date
)
18078 ((and daynr
(string-match "\\+[0-9]+[dwmy]" s
))
18079 (org-closest-date s
(if (and (boundp 'daynr
) (integerp daynr
)) daynr
18080 (time-to-days (current-time))) (match-string 0 s
)
18082 (t (time-to-days (apply 'encode-time
(org-parse-time-string s
))))))
18084 (defun org-time-from-absolute (d)
18085 "Return the time corresponding to date D.
18086 D may be an absolute day number, or a calendar-type list (month day year)."
18087 (if (numberp d
) (setq d
(calendar-gregorian-from-absolute d
)))
18088 (encode-time 0 0 0 (nth 1 d
) (car d
) (nth 2 d
)))
18090 (defun org-calendar-holiday ()
18091 "List of holidays, for Diary display in Org-mode."
18092 (require 'holidays
)
18094 (if (fboundp 'calendar-check-holidays
)
18095 'calendar-check-holidays
'check-calendar-holidays
) date
)))
18096 (if hl
(mapconcat 'identity hl
"; "))))
18098 (defun org-diary-sexp-entry (sexp entry date
)
18099 "Process a SEXP diary ENTRY for DATE."
18100 (require 'diary-lib
)
18101 (let ((result (if calendar-debug-sexp
18102 (let ((stack-trace-on-error t
))
18103 (eval (car (read-from-string sexp
))))
18104 (condition-case nil
18105 (eval (car (read-from-string sexp
)))
18108 (message "Bad sexp at line %d in %s: %s"
18110 (buffer-file-name) sexp
)
18112 (cond ((stringp result
) result
)
18113 ((and (consp result
)
18114 (stringp (cdr result
))) (cdr result
))
18118 (defun org-diary-to-ical-string (frombuf)
18119 "Get iCalendar entries from diary entries in buffer FROMBUF.
18120 This uses the icalendar.el library."
18121 (let* ((tmpdir (if (featurep 'xemacs
)
18123 temporary-file-directory
))
18124 (tmpfile (make-temp-name
18125 (expand-file-name "orgics" tmpdir
)))
18128 (set-buffer frombuf
)
18129 (icalendar-export-region (point-min) (point-max) tmpfile
)
18130 (setq buf
(find-buffer-visiting tmpfile
))
18132 (goto-char (point-min))
18133 (if (re-search-forward "^BEGIN:VEVENT" nil t
)
18134 (setq b
(match-beginning 0)))
18135 (goto-char (point-max))
18136 (if (re-search-backward "^END:VEVENT" nil t
)
18137 (setq e
(match-end 0)))
18138 (setq rtn
(if (and b e
) (concat (buffer-substring b e
) "\n") "")))
18140 (kill-buffer frombuf
)
18141 (delete-file tmpfile
)
18144 (defun org-closest-date (start current change prefer
)
18145 "Find the date closest to CURRENT that is consistent with START and CHANGE.
18146 When PREFER is `past' return a date that is either CURRENT or past.
18147 When PREFER is `future', return a date that is either CURRENT or future."
18148 ;; Make the proper lists from the dates
18150 (let ((a1 '(("d" . day
) ("w" . week
) ("m" . month
) ("y" . year
)))
18151 dn dw sday cday n1 n2
18152 d m y y1 y2 date1 date2 nmonths nm ny m2
)
18154 (setq start
(org-date-to-gregorian start
)
18155 current
(org-date-to-gregorian
18156 (if org-agenda-repeating-timestamp-show-all
18158 (time-to-days (current-time))))
18159 sday
(calendar-absolute-from-gregorian start
)
18160 cday
(calendar-absolute-from-gregorian current
))
18162 (if (<= cday sday
) (throw 'exit sday
))
18164 (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change
)
18165 (setq dn
(string-to-number (match-string 1 change
))
18166 dw
(cdr (assoc (match-string 2 change
) a1
)))
18167 (error "Invalid change specifyer: %s" change
))
18168 (if (eq dw
'week
) (setq dw
'day dn
(* 7 dn
)))
18171 (setq n1
(+ sday
(* dn
(floor (/ (- cday sday
) dn
))))
18174 (setq d
(nth 1 start
) m
(car start
) y1
(nth 2 start
) y2
(nth 2 current
))
18175 (setq y1
(+ (* (floor (/ (- y2 y1
) dn
)) dn
) y1
))
18176 (setq date1
(list m d y1
)
18177 n1
(calendar-absolute-from-gregorian date1
)
18178 date2
(list m d
(+ y1
(* (if (< n1 cday
) 1 -
1) dn
)))
18179 n2
(calendar-absolute-from-gregorian date2
)))
18181 ;; approx number of month between the tow dates
18182 (setq nmonths
(floor (/ (- cday sday
) 30.436875)))
18183 ;; How often does dn fit in there?
18184 (setq d
(nth 1 start
) m
(car start
) y
(nth 2 start
)
18185 nm
(* dn
(max 0 (1- (floor (/ nmonths dn
)))))
18187 ny
(floor (/ m
12))
18190 (while (> m
12) (setq m
(- m
12) y
(1+ y
)))
18191 (setq n1
(calendar-absolute-from-gregorian (list m d y
)))
18192 (setq m2
(+ m dn
) y2 y
)
18193 (if (> m2
12) (setq y2
(1+ y2
) m2
(- m2
12)))
18194 (setq n2
(calendar-absolute-from-gregorian (list m2 d y2
)))
18196 (setq n1 n2 m m2 y y2
)
18197 (setq m2
(+ m dn
) y2 y
)
18198 (if (> m2
12) (setq y2
(1+ y2
) m2
(- m2
12)))
18199 (setq n2
(calendar-absolute-from-gregorian (list m2 d y2
))))))
18201 (if org-agenda-repeating-timestamp-show-all
18203 ((eq prefer
'past
) n1
)
18204 ((eq prefer
'future
) (if (= cday n1
) n1 n2
))
18205 (t (if (> (abs (- cday n1
)) (abs (- cday n2
))) n2 n1
)))
18207 ((eq prefer
'past
) n1
)
18208 ((eq prefer
'future
) (if (= cday n1
) n1 n2
))
18209 (t (if (= cday n1
) n1 n2
)))))))
18211 (defun org-date-to-gregorian (date)
18212 "Turn any specification of DATE into a gregorian date for the calendar."
18213 (cond ((integerp date
) (calendar-gregorian-from-absolute date
))
18214 ((and (listp date
) (= (length date
) 3)) date
)
18216 (setq date
(org-parse-time-string date
))
18217 (list (nth 4 date
) (nth 3 date
) (nth 5 date
)))
18219 (list (nth 4 date
) (nth 3 date
) (nth 5 date
)))))
18221 (defun org-parse-time-string (s &optional nodefault
)
18222 "Parse the standard Org-mode time string.
18223 This should be a lot faster than the normal `parse-time-string'.
18224 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
18225 hour and minute fields will be nil if not given."
18226 (if (string-match org-ts-regexp0 s
)
18228 (if (or (match-beginning 8) (not nodefault
))
18229 (string-to-number (or (match-string 8 s
) "0")))
18230 (if (or (match-beginning 7) (not nodefault
))
18231 (string-to-number (or (match-string 7 s
) "0")))
18232 (string-to-number (match-string 4 s
))
18233 (string-to-number (match-string 3 s
))
18234 (string-to-number (match-string 2 s
))
18238 (defun org-timestamp-up (&optional arg
)
18239 "Increase the date item at the cursor by one.
18240 If the cursor is on the year, change the year. If it is on the month or
18241 the day, change that.
18242 With prefix ARG, change by that many units."
18244 (org-timestamp-change (prefix-numeric-value arg
)))
18246 (defun org-timestamp-down (&optional arg
)
18247 "Decrease the date item at the cursor by one.
18248 If the cursor is on the year, change the year. If it is on the month or
18249 the day, change that.
18250 With prefix ARG, change by that many units."
18252 (org-timestamp-change (- (prefix-numeric-value arg
))))
18254 (defun org-timestamp-up-day (&optional arg
)
18255 "Increase the date in the time stamp by one day.
18256 With prefix ARG, change that many days."
18258 (if (and (not (org-at-timestamp-p t
))
18259 (org-on-heading-p))
18261 (org-timestamp-change (prefix-numeric-value arg
) 'day
)))
18263 (defun org-timestamp-down-day (&optional arg
)
18264 "Decrease the date in the time stamp by one day.
18265 With prefix ARG, change that many days."
18267 (if (and (not (org-at-timestamp-p t
))
18268 (org-on-heading-p))
18270 (org-timestamp-change (- (prefix-numeric-value arg
)) 'day
)))
18272 (defsubst org-pos-in-match-range
(pos n
)
18273 (and (match-beginning n
)
18274 (<= (match-beginning n
) pos
)
18275 (>= (match-end n
) pos
)))
18277 (defun org-at-timestamp-p (&optional inactive-ok
)
18278 "Determine if the cursor is in or at a timestamp."
18280 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2
))
18282 (ans (or (looking-at tsr
)
18284 (skip-chars-backward "^[<\n\r\t")
18285 (if (> (point) (point-min)) (backward-char 1))
18286 (and (looking-at tsr
)
18287 (> (- (match-end 0) pos
) -
1))))))
18289 (boundp 'org-ts-what
)
18292 ((= pos
(match-beginning 0)) 'bracket
)
18293 ((= pos
(1- (match-end 0))) 'bracket
)
18294 ((org-pos-in-match-range pos
2) 'year
)
18295 ((org-pos-in-match-range pos
3) 'month
)
18296 ((org-pos-in-match-range pos
7) 'hour
)
18297 ((org-pos-in-match-range pos
8) 'minute
)
18298 ((or (org-pos-in-match-range pos
4)
18299 (org-pos-in-match-range pos
5)) 'day
)
18300 ((and (> pos
(or (match-end 8) (match-end 5)))
18301 (< pos
(match-end 0)))
18302 (- pos
(or (match-end 8) (match-end 5))))
18306 (defun org-toggle-timestamp-type ()
18309 (when (org-at-timestamp-p t
)
18311 (goto-char (match-beginning 0))
18312 (insert (if (equal (char-after) ?
<) "[" "<")) (delete-char 1)
18313 (goto-char (1- (match-end 0)))
18314 (insert (if (equal (char-after) ?
>) "]" ">")) (delete-char 1))
18315 (message "Timestamp is now %sactive"
18316 (if (equal (char-before) ?
>) "in" ""))))
18318 (defun org-timestamp-change (n &optional what
)
18319 "Change the date in the time stamp at point.
18320 The date will be changed by N times WHAT. WHAT can be `day', `month',
18321 `year', `minute', `second'. If WHAT is not given, the cursor position
18322 in the timestamp determines what will be changed."
18323 (let ((pos (point))
18328 (if (not (org-at-timestamp-p t
))
18329 (error "Not at a timestamp"))
18330 (if (and (not what
) (eq org-ts-what
'bracket
))
18331 (org-toggle-timestamp-type)
18332 (if (and (not what
) (not (eq org-ts-what
'day
))
18333 org-display-custom-times
18334 (get-text-property (point) 'display
)
18335 (not (get-text-property (1- (point)) 'display
)))
18336 (setq org-ts-what
'day
))
18337 (setq org-ts-what
(or what org-ts-what
)
18338 inactive
(= (char-after (match-beginning 0)) ?\
[)
18339 ts
(match-string 0))
18342 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( [-+][0-9]+[dwmy]\\)*\\)[]>]"
18344 (setq extra
(match-string 1 ts
)))
18345 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts
)
18347 (setq time0
(org-parse-time-string ts
))
18349 (encode-time (or (car time0
) 0)
18350 (+ (if (eq org-ts-what
'minute
) n
0) (nth 1 time0
))
18351 (+ (if (eq org-ts-what
'hour
) n
0) (nth 2 time0
))
18352 (+ (if (eq org-ts-what
'day
) n
0) (nth 3 time0
))
18353 (+ (if (eq org-ts-what
'month
) n
0) (nth 4 time0
))
18354 (+ (if (eq org-ts-what
'year
) n
0) (nth 5 time0
))
18356 (when (integerp org-ts-what
)
18357 (setq extra
(org-modify-ts-extra extra org-ts-what n
)))
18358 (if (eq what
'calendar
)
18359 (let ((cal-date (org-get-date-from-calendar)))
18360 (setcar (nthcdr 4 time0
) (nth 0 cal-date
)) ; month
18361 (setcar (nthcdr 3 time0
) (nth 1 cal-date
)) ; day
18362 (setcar (nthcdr 5 time0
) (nth 2 cal-date
)) ; year
18363 (setcar time0
(or (car time0
) 0))
18364 (setcar (nthcdr 1 time0
) (or (nth 1 time0
) 0))
18365 (setcar (nthcdr 2 time0
) (or (nth 2 time0
) 0))
18366 (setq time
(apply 'encode-time time0
))))
18367 (setq org-last-changed-timestamp
18368 (org-insert-time-stamp time with-hm inactive nil nil extra
))
18369 (org-clock-update-time-maybe)
18371 ;; Try to recenter the calendar window, if any
18372 (if (and org-calendar-follow-timestamp-change
18373 (get-buffer-window "*Calendar*" t
)
18374 (memq org-ts-what
'(day month year
)))
18375 (org-recenter-calendar (time-to-days time
))))))
18377 ;; FIXME: does not yet work for lead times
18378 (defun org-modify-ts-extra (s pos n
)
18379 "Change the different parts of the lead-time and repeat fields in timestamp."
18380 (let ((idx '(("d" .
0) ("w" .
1) ("m" .
2) ("y" .
3) ("d" . -
1) ("y" .
4)))
18382 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( \\+\\([0-9]+\\)\\([dmwy]\\)\\)?" s
)
18384 ((or (org-pos-in-match-range pos
2)
18385 (org-pos-in-match-range pos
3))
18386 (setq m
(string-to-number (match-string 3 s
))
18387 h
(string-to-number (match-string 2 s
)))
18388 (if (org-pos-in-match-range pos
2)
18391 (if (< m
0) (setq m
(+ m
60) h
(1- h
)))
18392 (if (> m
59) (setq m
(- m
60) h
(1+ h
)))
18393 (setq h
(min 24 (max 0 h
)))
18394 (setq ng
1 new
(format "-%02d:%02d" h m
)))
18395 ((org-pos-in-match-range pos
6)
18396 (setq ng
6 new
(car (rassoc (+ n
(cdr (assoc (match-string 6 s
) idx
))) idx
))))
18397 ((org-pos-in-match-range pos
5)
18398 (setq ng
5 new
(format "%d" (max 1 (+ n
(string-to-number (match-string 5 s
))))))))
18402 (substring s
0 (match-beginning ng
))
18404 (substring s
(match-end ng
))))))
18407 (defun org-recenter-calendar (date)
18408 "If the calendar is visible, recenter it to DATE."
18409 (let* ((win (selected-window))
18410 (cwin (get-buffer-window "*Calendar*" t
))
18411 (calendar-move-hook nil
))
18413 (select-window cwin
)
18414 (calendar-goto-date (if (listp date
) date
18415 (calendar-gregorian-from-absolute date
)))
18416 (select-window win
))))
18418 (defun org-goto-calendar (&optional arg
)
18419 "Go to the Emacs calendar at the current date.
18420 If there is a time stamp in the current line, go to that date.
18421 A prefix ARG can be used to force the current date."
18423 (let ((tsr org-ts-regexp
) diff
18424 (calendar-move-hook nil
)
18425 (view-calendar-holidays-initially nil
)
18426 (view-diary-entries-initially nil
))
18427 (if (or (org-at-timestamp-p)
18429 (beginning-of-line 1)
18430 (looking-at (concat ".*" tsr
))))
18431 (let ((d1 (time-to-days (current-time)))
18433 (org-time-string-to-time (match-string 1)))))
18434 (setq diff
(- d2 d1
))))
18436 (calendar-goto-today)
18437 (if (and diff
(not arg
)) (calendar-forward-day diff
))))
18439 (defun org-get-date-from-calendar ()
18440 "Return a list (month day year) of date at point in calendar."
18441 (with-current-buffer "*Calendar*"
18443 (calendar-cursor-to-date))))
18445 (defun org-date-from-calendar ()
18446 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
18447 If there is already a time stamp at the cursor position, update it."
18449 (if (org-at-timestamp-p t
)
18450 (org-timestamp-change 0 'calendar
)
18451 (let ((cal-date (org-get-date-from-calendar)))
18452 (org-insert-time-stamp
18453 (encode-time 0 0 0 (nth 1 cal-date
) (car cal-date
) (nth 2 cal-date
))))))
18455 ;; Make appt aware of appointments from the agenda
18457 (defun org-agenda-to-appt (&optional filter
)
18458 "Activate appointments found in `org-agenda-files'.
18459 When prefixed, prompt for a regular expression and use it as a
18460 filter: only add entries if they match this regular expression.
18462 FILTER can be a string. In this case, use this string as a
18463 regular expression to filter results.
18465 FILTER can also be an alist, with the car of each cell being
18466 either 'headline or 'category. For example:
18468 '((headline \"IMPORTANT\")
18469 (category \"Work\"))
18471 will only add headlines containing IMPORTANT or headlines
18472 belonging to the category \"Work\"."
18474 (require 'calendar
)
18475 (if (equal filter
'(4))
18476 (setq filter
(read-from-minibuffer "Regexp filter: ")))
18477 (let* ((cnt 0) ; count added events
18478 (org-agenda-new-buffers nil
)
18479 (today (org-date-to-gregorian
18480 (time-to-days (current-time))))
18481 (files (org-agenda-files)) entries file
)
18482 ;; Get all entries which may contain an appt
18483 (while (setq file
(pop files
))
18486 (org-agenda-get-day-entries
18488 :timestamp
:scheduled
:deadline
))))
18489 (setq entries
(delq nil entries
))
18490 ;; Map thru entries and find if they pass thru the filter
18493 (let* ((evt (org-trim (get-text-property 1 'txt x
)))
18494 (cat (get-text-property 1 'org-category x
))
18495 (tod (get-text-property 1 'time-of-day x
))
18496 (ok (or (null filter
)
18497 (and (stringp filter
) (string-match filter evt
))
18498 (and (listp filter
)
18500 (cadr (assoc 'category filter
)) cat
)
18502 (cadr (assoc 'headline filter
)) evt
))))))
18503 ;; FIXME: Shall we remove text-properties for the appt text?
18504 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
18506 (setq tod
(number-to-string tod
)
18507 tod
(when (string-match
18508 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)" tod
)
18509 (concat (match-string 1 tod
) ":"
18510 (match-string 2 tod
))))
18512 (setq cnt
(1+ cnt
))))) entries
)
18513 (org-release-buffers org-agenda-new-buffers
)
18514 (message "Added %d event%s for today" cnt
(if (> cnt
1) "s" ""))))
18516 ;;; The clock for measuring work time.
18518 (defvar org-mode-line-string
"")
18519 (put 'org-mode-line-string
'risky-local-variable t
)
18521 (defvar org-mode-line-timer nil
)
18522 (defvar org-clock-heading
"")
18523 (defvar org-clock-start-time
"")
18525 (defun org-update-mode-line ()
18526 (let* ((delta (- (time-to-seconds (current-time))
18527 (time-to-seconds org-clock-start-time
)))
18528 (h (floor delta
3600))
18529 (m (floor (- delta
(* 3600 h
)) 60)))
18530 (setq org-mode-line-string
18531 (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading
)
18532 'help-echo
"Org-mode clock is running"))
18533 (force-mode-line-update)))
18535 (defvar org-clock-marker
(make-marker)
18536 "Marker recording the last clock-in.")
18537 (defvar org-clock-mode-line-entry nil
18538 "Information for the modeline about the running clock.")
18540 (defun org-clock-in ()
18541 "Start the clock on the current item.
18542 If necessary, clock-out of the currently active clock."
18547 (org-back-to-heading t
)
18548 (when (and org-clock-in-switch-to-state
18549 (not (looking-at (concat outline-regexp
"[ \t]*"
18550 org-clock-in-switch-to-state
18552 (org-todo org-clock-in-switch-to-state
))
18553 (if (and org-clock-heading-function
18554 (functionp org-clock-heading-function
))
18555 (setq org-clock-heading
(funcall org-clock-heading-function
))
18556 (if (looking-at org-complex-heading-regexp
)
18557 (setq org-clock-heading
(match-string 4))
18558 (setq org-clock-heading
"???")))
18559 (setq org-clock-heading
(propertize org-clock-heading
'face nil
))
18560 (org-clock-find-position)
18562 (insert "\n") (backward-char 1)
18564 (insert org-clock-string
" ")
18565 (setq org-clock-start-time
(current-time))
18566 (setq ts
(org-insert-time-stamp (current-time) 'with-hm
'inactive
))
18567 (move-marker org-clock-marker
(point) (buffer-base-buffer))
18568 (or global-mode-string
(setq global-mode-string
'("")))
18569 (or (memq 'org-mode-line-string global-mode-string
)
18570 (setq global-mode-string
18571 (append global-mode-string
'(org-mode-line-string))))
18572 (org-update-mode-line)
18573 (setq org-mode-line-timer
(run-with-timer 60 60 'org-update-mode-line
))
18574 (message "Clock started at %s" ts
))))
18576 (defun org-clock-find-position ()
18577 "Find the location where the next clock line should be inserted."
18578 (org-back-to-heading t
)
18580 (let ((beg (point-at-bol 2)) (end (progn (outline-next-heading) (point)))
18581 (re (concat "^[ \t]*" org-clock-string
))
18585 (when (eobp) (newline) (setq end
(max (point) end
)))
18586 (when (re-search-forward "^[ \t]*:CLOCK:" end t
)
18587 ;; we seem to have a CLOCK drawer, so go there.
18588 (beginning-of-line 2)
18590 ;; Lets count the CLOCK lines
18592 (while (re-search-forward re end t
)
18593 (setq first
(or first
(match-beginning 0))
18594 last
(match-beginning 0)
18596 (when (and (integerp org-clock-into-drawer
)
18597 (>= (1+ cnt
) org-clock-into-drawer
))
18598 ;; Wrap current entries into a new drawer
18600 (beginning-of-line 2)
18601 (if (org-at-item-p) (org-end-of-item))
18603 (beginning-of-line 0)
18604 (org-indent-line-function)
18606 (insert ":CLOCK:\n")
18607 (beginning-of-line 0)
18608 (org-indent-line-function)
18609 (org-flag-drawer t
)
18610 (beginning-of-line 2)
18614 (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp
))
18615 (not (equal (match-string 1) org-clock-string
)))
18616 ;; Planning info, skip to after it
18617 (beginning-of-line 2)
18618 (or (bolp) (newline)))
18619 (when (eq t org-clock-into-drawer
)
18620 (insert ":CLOCK:\n:END:\n")
18621 (beginning-of-line -
1)
18622 (org-indent-line-function)
18623 (org-flag-drawer t
)
18624 (beginning-of-line 2)
18625 (org-indent-line-function)))))
18627 (defun org-clock-out (&optional fail-quietly
)
18628 "Stop the currently running clock.
18629 If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
18632 (if (not (marker-buffer org-clock-marker
))
18633 (if fail-quietly
(throw 'exit t
) (error "No active clock")))
18636 (set-buffer (marker-buffer org-clock-marker
))
18637 (goto-char org-clock-marker
)
18638 (beginning-of-line 1)
18639 (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp
))
18640 (equal (match-string 1) org-clock-string
))
18641 (setq ts
(match-string 2))
18642 (if fail-quietly
(throw 'exit nil
) (error "Clock start time is gone")))
18643 (goto-char (match-end 0))
18644 (delete-region (point) (point-at-eol))
18646 (setq te
(org-insert-time-stamp (current-time) 'with-hm
'inactive
))
18647 (setq s
(- (time-to-seconds (apply 'encode-time
(org-parse-time-string te
)))
18648 (time-to-seconds (apply 'encode-time
(org-parse-time-string ts
))))
18649 h
(floor (/ s
3600))
18653 (insert " => " (format "%2d:%02d" h m
))
18654 (move-marker org-clock-marker nil
)
18655 (let* ((logging (save-match-data (org-entry-get nil
"LOGGING" t
)))
18656 (org-log-done (org-parse-local-options logging
'org-log-done
))
18657 (org-log-repeat (org-parse-local-options logging
'org-log-repeat
)))
18658 (org-add-log-maybe 'clock-out
))
18659 (when org-mode-line-timer
18660 (cancel-timer org-mode-line-timer
)
18661 (setq org-mode-line-timer nil
))
18662 (setq global-mode-string
18663 (delq 'org-mode-line-string global-mode-string
))
18664 (force-mode-line-update)
18665 (message "Clock stopped at %s after HH:MM = %d:%02d" te h m
)))))
18667 (defun org-clock-cancel ()
18668 "Cancel the running clock be removing the start timestamp."
18670 (if (not (marker-buffer org-clock-marker
))
18671 (error "No active clock"))
18673 (set-buffer (marker-buffer org-clock-marker
))
18674 (goto-char org-clock-marker
)
18675 (delete-region (1- (point-at-bol)) (point-at-eol)))
18676 (setq global-mode-string
18677 (delq 'org-mode-line-string global-mode-string
))
18678 (force-mode-line-update)
18679 (message "Clock canceled"))
18681 (defun org-clock-goto (&optional delete-windows
)
18682 "Go to the currently clocked-in entry."
18684 (if (not (marker-buffer org-clock-marker
))
18685 (error "No active clock"))
18686 (switch-to-buffer-other-window
18687 (marker-buffer org-clock-marker
))
18688 (if delete-windows
(delete-other-windows))
18689 (goto-char org-clock-marker
)
18691 (org-back-to-heading)
18694 (defvar org-clock-file-total-minutes nil
18695 "Holds the file total time in minutes, after a call to `org-clock-sum'.")
18696 (make-variable-buffer-local 'org-clock-file-total-minutes
)
18698 (defun org-clock-sum (&optional tstart tend
)
18699 "Sum the times for each subtree.
18700 Puts the resulting times in minutes as a text property on each headline."
18702 (let* ((bmp (buffer-modified-p))
18703 (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
18705 "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
18707 (ltimes (make-vector lmax
0))
18712 (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t
))
18714 (goto-char (point-max))
18715 (while (re-search-backward re nil t
)
18719 (setq ts
(match-string 2)
18720 te
(match-string 3)
18721 ts
(time-to-seconds
18722 (apply 'encode-time
(org-parse-time-string ts
)))
18723 te
(time-to-seconds
18724 (apply 'encode-time
(org-parse-time-string te
)))
18725 ts
(if tstart
(max ts tstart
) ts
)
18726 te
(if tend
(min te tend
) te
)
18728 t1
(if (> dt
0) (+ t1
(floor (/ dt
60))) t1
)))
18731 (setq t1
(+ t1
(string-to-number (match-string 5))
18732 (* 60 (string-to-number (match-string 4))))))
18734 (setq level
(- (match-end 1) (match-beginning 1)))
18735 (when (or (> t1
0) (> (aref ltimes level
) 0))
18736 (loop for l from
0 to level do
18737 (aset ltimes l
(+ (aref ltimes l
) t1
)))
18738 (setq t1
0 time
(aref ltimes level
))
18739 (loop for l from level to
(1- lmax
) do
18741 (goto-char (match-beginning 0))
18742 (put-text-property (point) (point-at-eol) :org-clock-minutes time
)))))
18743 (setq org-clock-file-total-minutes
(aref ltimes
0)))
18744 (set-buffer-modified-p bmp
)))
18746 (defun org-clock-display (&optional total-only
)
18747 "Show subtree times in the entire buffer.
18748 If TOTAL-ONLY is non-nil, only show the total time for the entire file
18751 (org-remove-clock-overlays)
18756 (goto-char (point-min))
18757 (while (or (and (equal (setq p
(point)) (point-min))
18758 (get-text-property p
:org-clock-minutes
))
18759 (setq p
(next-single-property-change
18760 (point) :org-clock-minutes
)))
18762 (when (setq time
(get-text-property p
:org-clock-minutes
))
18763 (org-put-clock-overlay time
(funcall outline-level
))))
18764 (setq h
(/ org-clock-file-total-minutes
60)
18765 m
(- org-clock-file-total-minutes
(* 60 h
)))
18766 ;; Arrange to remove the overlays upon next change.
18767 (when org-remove-highlights-with-change
18768 (org-add-hook 'before-change-functions
'org-remove-clock-overlays
18770 (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m
)))
18772 (defvar org-clock-overlays nil
)
18773 (make-variable-buffer-local 'org-clock-overlays
)
18775 (defun org-put-clock-overlay (time &optional level
)
18776 "Put an overlays on the current line, displaying TIME.
18777 If LEVEL is given, prefix time with a corresponding number of stars.
18778 This creates a new overlay and stores it in `org-clock-overlays', so that it
18779 will be easy to remove."
18780 (let* ((c 60) (h (floor (/ time
60))) (m (- time
(* 60 h
)))
18781 (l (if level
(org-get-legal-level level
0) 0))
18785 (unless (eolp) (skip-chars-backward "^ \t"))
18786 (skip-chars-backward " \t")
18787 (setq ov
(org-make-overlay (1- (point)) (point-at-eol))
18788 tx
(concat (buffer-substring (1- (point)) (point))
18789 (make-string (+ off
(max 0 (- c
(current-column)))) ?.
)
18790 (org-add-props (format "%s %2d:%02d%s"
18791 (make-string l ?
*) h m
18792 (make-string (- 10 l
) ?\
))
18793 '(face secondary-selection
))
18795 (if (not (featurep 'xemacs
))
18796 (org-overlay-put ov
'display tx
)
18797 (org-overlay-put ov
'invisible t
)
18798 (org-overlay-put ov
'end-glyph
(make-glyph tx
)))
18799 (push ov org-clock-overlays
)))
18801 (defun org-remove-clock-overlays (&optional beg end noremove
)
18802 "Remove the occur highlights from the buffer.
18803 BEG and END are ignored. If NOREMOVE is nil, remove this function
18804 from the `before-change-functions' in the current buffer."
18806 (unless org-inhibit-highlight-removal
18807 (mapc 'org-delete-overlay org-clock-overlays
)
18808 (setq org-clock-overlays nil
)
18810 (remove-hook 'before-change-functions
18811 'org-remove-clock-overlays
'local
))))
18813 (defun org-clock-out-if-current ()
18814 "Clock out if the current entry contains the running clock.
18815 This is used to stop the clock after a TODO entry is marked DONE,
18816 and is only done if the variable `org-clock-out-when-done' is not nil."
18817 (when (and org-clock-out-when-done
18818 (member state org-done-keywords
)
18819 (equal (marker-buffer org-clock-marker
) (current-buffer))
18820 (< (point) org-clock-marker
)
18821 (> (save-excursion (outline-next-heading) (point))
18823 ;; Clock out, but don't accept a logging message for this.
18824 (let ((org-log-done (if (and (listp org-log-done
)
18825 (member 'clock-out org-log-done
))
18830 (add-hook 'org-after-todo-state-change-hook
18831 'org-clock-out-if-current
)
18833 (defun org-check-running-clock ()
18834 "Check if the current buffer contains the running clock.
18835 If yes, offer to stop it and to save the buffer with the changes."
18836 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
18837 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
18840 (when (y-or-n-p "Save changed buffer?")
18843 (defun org-clock-report (&optional arg
)
18844 "Create a table containing a report about clocked time.
18845 If the cursor is inside an existing clocktable block, then the table
18846 will be updated. If not, a new clocktable will be inserted.
18847 When called with a prefix argument, move to the first clock table in the
18848 buffer and update it."
18850 (org-remove-clock-overlays)
18852 (org-find-dblock "clocktable")
18854 (if (org-in-clocktable-p)
18855 (goto-char (org-in-clocktable-p))
18856 (org-create-dblock (list :name
"clocktable"
18857 :maxlevel
2 :scope
'file
)))
18858 (org-update-dblock))
18860 (defun org-in-clocktable-p ()
18861 "Check if the cursor is in a clocktable."
18862 (let ((pos (point)) start
)
18865 (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t
)
18866 (setq start
(match-beginning 0))
18867 (re-search-forward "^#\\+END:.*" nil t
)
18868 (>= (match-end 0) pos
)
18871 (defun org-clock-update-time-maybe ()
18872 "If this is a CLOCK line, update it and return t.
18873 Otherwise, return nil."
18876 (beginning-of-line 1)
18877 (skip-chars-forward " \t")
18878 (when (looking-at org-clock-string
)
18879 (let ((re (concat "[ \t]*" org-clock-string
18880 " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
18881 "\\([ \t]*=>.*\\)?"))
18883 (if (not (looking-at re
))
18885 (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
18887 (setq ts
(match-string 1)
18888 te
(match-string 2))
18889 (setq s
(- (time-to-seconds
18890 (apply 'encode-time
(org-parse-time-string te
)))
18892 (apply 'encode-time
(org-parse-time-string ts
))))
18893 h
(floor (/ s
3600))
18897 (insert " => " (format "%2d:%02d" h m
))
18900 (defun org-clock-special-range (key &optional time as-strings
)
18901 "Return two times bordering a special time range.
18902 Key is a symbol specifying the range and can be one of `today', `yesterday',
18903 `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
18904 A week starts Monday 0:00 and ends Sunday 24:00.
18905 The range is determined relative to TIME. TIME defaults to the current time.
18906 The return value is a cons cell with two internal times like the ones
18907 returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
18908 the returned times will be formatted strings."
18909 (let* ((tm (decode-time (or time
(current-time))))
18910 (s 0) (m (nth 1 tm
)) (h (nth 2 tm
))
18911 (d (nth 3 tm
)) (month (nth 4 tm
)) (y (nth 5 tm
))
18913 s1 m1 h1 d1 month1 y1 diff ts te fm
)
18916 (setq h
0 m
0 h1
24 m1
0))
18917 ((eq key
'yesterday
)
18918 (setq d
(1- d
) h
0 m
0 h1
24 m1
0))
18919 ((eq key
'thisweek
)
18920 (setq diff
(if (= dow
0) 6 (1- dow
))
18921 m
0 h
0 d
(- d diff
) d1
(+ 7 d
)))
18922 ((eq key
'lastweek
)
18923 (setq diff
(+ 7 (if (= dow
0) 6 (1- dow
)))
18924 m
0 h
0 d
(- d diff
) d1
(+ 7 d
)))
18925 ((eq key
'thismonth
)
18926 (setq d
1 h
0 m
0 d1
1 month1
(1+ month
) h1
0 m1
0))
18927 ((eq key
'lastmonth
)
18928 (setq d
1 h
0 m
0 d1
1 month
(1- month
) month1
(1+ month
) h1
0 m1
0))
18929 ((eq key
'thisyear
)
18930 (setq m
0 h
0 d
1 month
1 y1
(1+ y
)))
18931 ((eq key
'lastyear
)
18932 (setq m
0 h
0 d
1 month
1 y
(1- y
) y1
(1+ y
)))
18933 (t (error "No such time block %s" key
)))
18934 (setq ts
(encode-time s m h d month y
)
18935 te
(encode-time (or s1 s
) (or m1 m
) (or h1 h
)
18936 (or d1 d
) (or month1 month
) (or y1 y
)))
18937 (setq fm
(cdr org-time-stamp-formats
))
18939 (cons (format-time-string fm ts
) (format-time-string fm te
))
18942 (defun org-dblock-write:clocktable
(params)
18943 "Write the standard clocktable."
18945 (let* ((hlchars '((1 .
"*") (2 .
"/")))
18946 (ins (make-marker))
18948 (scope (plist-get params
:scope
))
18949 (tostring (plist-get params
:tostring
))
18950 (multifile (plist-get params
:multifile
))
18951 (header (plist-get params
:header
))
18952 (maxlevel (or (plist-get params
:maxlevel
) 3))
18953 (step (plist-get params
:step
))
18954 (emph (plist-get params
:emphasize
))
18955 (ts (plist-get params
:tstart
))
18956 (te (plist-get params
:tend
))
18957 (block (plist-get params
:block
))
18958 ipos time h m p level hlc hdl
18959 cc beg end pos tbl
)
18961 (org-clocktable-steps params
)
18964 (setq cc
(org-clock-special-range block nil t
)
18965 ts
(car cc
) te
(cdr cc
)))
18966 (if ts
(setq ts
(time-to-seconds
18967 (apply 'encode-time
(org-parse-time-string ts
)))))
18968 (if te
(setq te
(time-to-seconds
18969 (apply 'encode-time
(org-parse-time-string te
)))))
18970 (move-marker ins
(point))
18971 (setq ipos
(point))
18973 ;; Get the right scope
18978 ((eq scope
'file
) (widen))
18979 ((eq scope
'subtree
) (org-narrow-to-subtree))
18981 (while (org-up-heading-safe))
18982 (org-narrow-to-subtree))
18983 ((and (symbolp scope
) (string-match "^tree\\([0-9]+\\)$"
18984 (symbol-name scope
)))
18985 (setq level
(string-to-number (match-string 1 (symbol-name scope
))))
18987 (while (org-up-heading-safe)
18988 (looking-at outline-regexp
)
18989 (if (<= (org-reduced-level (funcall outline-level
)) level
)
18990 (throw 'exit nil
))))
18991 (org-narrow-to-subtree))
18992 ((or (listp scope
) (eq scope
'agenda
))
18993 (let* ((files (if (listp scope
) scope
(org-agenda-files)))
18995 (p1 (copy-sequence params
))
18997 (plist-put p1
:tostring t
)
18998 (plist-put p1
:multifile t
)
18999 (plist-put p1
:scope
'file
)
19000 (org-prepare-agenda-buffers files
)
19001 (while (setq file
(pop files
))
19002 (with-current-buffer (find-buffer-visiting file
)
19003 (push (org-clocktable-add-file
19004 file
(org-dblock-write:clocktable p1
)) tbl
)
19005 (setq total-time
(+ (or total-time
0)
19006 org-clock-file-total-minutes
)))))))
19009 (unless (eq scope
'agenda
)
19010 (org-clock-sum ts te
)
19011 (goto-char (point-min))
19012 (while (setq p
(next-single-property-change (point) :org-clock-minutes
))
19014 (when (setq time
(get-text-property p
:org-clock-minutes
))
19016 (beginning-of-line 1)
19017 (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
19018 (setq level
(org-reduced-level
19019 (- (match-end 1) (match-beginning 1))))
19020 (<= level maxlevel
))
19021 (setq hlc
(if emph
(or (cdr (assoc level hlchars
)) "") "")
19022 hdl
(match-string 2)
19024 m
(- time
(* 60 h
)))
19025 (if (and (not multifile
) (= level
1)) (push "|-" tbl
))
19027 "| " (int-to-string level
) "|" hlc hdl hlc
" |"
19028 (make-string (1- level
) ?|
)
19029 hlc
(format "%d:%02d" h m
) hlc
19031 (setq tbl
(nreverse tbl
))
19033 (if tbl
(mapconcat 'identity tbl
"\n") nil
)
19035 (insert-before-markers
19038 "Clock summary at ["
19040 (format-time-string (cdr org-time-stamp-formats
))
19044 (format " Considered range is /%s/." block
)
19047 (if (eq scope
'agenda
) "|File" "")
19048 "|L|Headline|Time|\n")
19049 (setq total-time
(or total-time org-clock-file-total-minutes
)
19050 h
(/ total-time
60)
19051 m
(- total-time
(* 60 h
)))
19052 (insert-before-markers
19054 (if (eq scope
'agenda
) "|" "")
19057 (format "*%d:%02d*" h m
)
19059 (setq tbl
(delq nil tbl
))
19060 (if (and (stringp (car tbl
)) (> (length (car tbl
)) 1)
19061 (equal (substring (car tbl
) 0 2) "|-"))
19063 (insert-before-markers (mapconcat
19064 'identity
(delq nil tbl
)
19065 (if (eq scope
'agenda
) "\n|-\n" "\n")))
19066 (backward-delete-char 1)
19068 (skip-chars-forward "^|")
19069 (org-table-align))))))
19071 (defun org-clocktable-steps (params)
19072 (let* ((p1 (copy-sequence params
))
19073 (ts (plist-get p1
:tstart
))
19074 (te (plist-get p1
:tend
))
19075 (step0 (plist-get p1
:step
))
19076 (step (cdr (assoc step0
'((day .
86400) (week .
604800)))))
19077 (block (plist-get p1
:block
))
19080 (setq cc
(org-clock-special-range block nil t
)
19081 ts
(car cc
) te
(cdr cc
)))
19082 (if ts
(setq ts
(time-to-seconds
19083 (apply 'encode-time
(org-parse-time-string ts
)))))
19084 (if te
(setq te
(time-to-seconds
19085 (apply 'encode-time
(org-parse-time-string te
)))))
19086 (plist-put p1
:header
"")
19087 (plist-put p1
:step nil
)
19088 (plist-put p1
:block nil
)
19090 (or (bolp) (insert "\n"))
19091 (plist-put p1
:tstart
(format-time-string
19092 (car org-time-stamp-formats
)
19093 (seconds-to-time ts
)))
19094 (plist-put p1
:tend
(format-time-string
19095 (car org-time-stamp-formats
)
19096 (seconds-to-time (setq ts
(+ ts step
)))))
19097 (insert "\n" (if (eq step0
'day
) "Daily report: " "Weekly report starting on: ")
19098 (plist-get p1
:tstart
) "\n")
19099 (org-dblock-write:clocktable p1
)
19100 (re-search-forward "#\\+END:")
19104 (defun org-clocktable-add-file (file table
)
19106 (let ((lines (org-split-string table
"\n"))
19107 (ff (file-name-nondirectory file
)))
19108 (mapconcat 'identity
19109 (mapcar (lambda (x)
19110 (if (string-match org-table-dataline-regexp x
)
19116 ;; FIXME: I don't think anybody uses this, ask David
19117 (defun org-collect-clock-time-entries ()
19118 "Return an internal list with clocking information.
19119 This list has one entry for each CLOCK interval.
19120 FIXME: describe the elements."
19122 (let ((re (concat "^[ \t]*" org-clock-string
19123 " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
19124 rtn beg end next cont level title total closedp leafp
19125 clockpos titlepos h m donep
)
19128 (goto-char (point-min))
19129 (while (re-search-forward re nil t
)
19130 (setq clockpos
(match-beginning 0)
19131 beg
(match-string 1) end
(match-string 2)
19132 cont
(match-end 0))
19133 (setq beg
(apply 'encode-time
(org-parse-time-string beg
))
19134 end
(apply 'encode-time
(org-parse-time-string end
)))
19135 (org-back-to-heading t
)
19136 (setq donep
(org-entry-is-done-p))
19137 (setq titlepos
(point)
19138 total
(or (get-text-property (1+ (point)) :org-clock-minutes
) 0)
19139 h
(/ total
60) m
(- total
(* 60 h
))
19141 (looking-at "\\(\\*+\\) +\\(.*\\)")
19142 (setq level
(- (match-end 1) (match-beginning 1))
19143 title
(org-match-string-no-properties 2))
19144 (save-excursion (outline-next-heading) (setq next
(point)))
19145 (setq closedp
(re-search-forward org-closed-time-regexp next t
))
19147 (setq leafp
(and (looking-at "^\\*+ ")
19148 (<= (- (match-end 0) (point)) level
)))
19149 (push (list beg end clockpos closedp donep
19150 total title titlepos level leafp
)
19155 ;;;; Agenda, and Diary Integration
19157 ;;; Define the Org-agenda-mode
19159 (defvar org-agenda-mode-map
(make-sparse-keymap)
19160 "Keymap for `org-agenda-mode'.")
19162 (defvar org-agenda-menu
) ; defined later in this file.
19163 (defvar org-agenda-follow-mode nil
)
19164 (defvar org-agenda-show-log nil
)
19165 (defvar org-agenda-redo-command nil
)
19166 (defvar org-agenda-mode-hook nil
)
19167 (defvar org-agenda-type nil
)
19168 (defvar org-agenda-force-single-file nil
)
19170 (defun org-agenda-mode ()
19171 "Mode for time-sorted view on action items in Org-mode files.
19173 The following commands are available:
19175 \\{org-agenda-mode-map}"
19177 (kill-all-local-variables)
19178 (setq org-agenda-undo-list nil
19179 org-agenda-pending-undo-list nil
)
19180 (setq major-mode
'org-agenda-mode
)
19181 ;; Keep global-font-lock-mode from turning on font-lock-mode
19182 (org-set-local 'font-lock-global-modes
(list 'not major-mode
))
19183 (setq mode-name
"Org-Agenda")
19184 (use-local-map org-agenda-mode-map
)
19185 (easy-menu-add org-agenda-menu
)
19186 (if org-startup-truncated
(setq truncate-lines t
))
19187 (org-add-hook 'post-command-hook
'org-agenda-post-command-hook nil
'local
)
19188 (org-add-hook 'pre-command-hook
'org-unhighlight nil
'local
)
19189 ;; Make sure properties are removed when copying text
19190 (when (boundp 'buffer-substring-filters
)
19191 (org-set-local 'buffer-substring-filters
19193 (set-text-properties 0 (length x
) nil x
) x
)
19194 buffer-substring-filters
)))
19195 (unless org-agenda-keep-modes
19196 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
19197 org-agenda-show-log nil
))
19199 '("Agenda") "Agenda Files"
19203 (if (get 'org-agenda-files
'org-restrict
)
19204 "Restricted to single file"
19206 '(org-edit-agenda-file-list)
19207 (not (get 'org-agenda-files
'org-restrict
)))
19209 (mapcar 'org-file-menu-entry
(org-agenda-files))))
19210 (org-agenda-set-mode-name)
19212 (if (fboundp 'run-mode-hooks
) 'run-mode-hooks
'run-hooks
)
19213 (list 'org-agenda-mode-hook
)))
19215 (substitute-key-definition 'undo
'org-agenda-undo
19216 org-agenda-mode-map global-map
)
19217 (org-defkey org-agenda-mode-map
"\C-i" 'org-agenda-goto
)
19218 (org-defkey org-agenda-mode-map
[(tab)] 'org-agenda-goto
)
19219 (org-defkey org-agenda-mode-map
"\C-m" 'org-agenda-switch-to
)
19220 (org-defkey org-agenda-mode-map
"\C-k" 'org-agenda-kill
)
19221 (org-defkey org-agenda-mode-map
"\C-c$" 'org-agenda-archive
)
19222 (org-defkey org-agenda-mode-map
"\C-c\C-x\C-s" 'org-agenda-archive
)
19223 (org-defkey org-agenda-mode-map
"$" 'org-agenda-archive
)
19224 (org-defkey org-agenda-mode-map
"\C-c\C-o" 'org-agenda-open-link
)
19225 (org-defkey org-agenda-mode-map
" " 'org-agenda-show
)
19226 (org-defkey org-agenda-mode-map
"\C-c\C-t" 'org-agenda-todo
)
19227 (org-defkey org-agenda-mode-map
[(control shift right
)] 'org-agenda-todo-nextset
)
19228 (org-defkey org-agenda-mode-map
[(control shift left
)] 'org-agenda-todo-previousset
)
19229 (org-defkey org-agenda-mode-map
"\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer
)
19230 (org-defkey org-agenda-mode-map
"b" 'org-agenda-tree-to-indirect-buffer
)
19231 (org-defkey org-agenda-mode-map
"o" 'delete-other-windows
)
19232 (org-defkey org-agenda-mode-map
"L" 'org-agenda-recenter
)
19233 (org-defkey org-agenda-mode-map
"t" 'org-agenda-todo
)
19234 (org-defkey org-agenda-mode-map
"a" 'org-agenda-toggle-archive-tag
)
19235 (org-defkey org-agenda-mode-map
":" 'org-agenda-set-tags
)
19236 (org-defkey org-agenda-mode-map
"." 'org-agenda-goto-today
)
19237 (org-defkey org-agenda-mode-map
"j" 'org-agenda-goto-date
)
19238 (org-defkey org-agenda-mode-map
"d" 'org-agenda-day-view
)
19239 (org-defkey org-agenda-mode-map
"w" 'org-agenda-week-view
)
19240 (org-defkey org-agenda-mode-map
"m" 'org-agenda-month-view
)
19241 (org-defkey org-agenda-mode-map
"y" 'org-agenda-year-view
)
19242 (org-defkey org-agenda-mode-map
[(shift right
)] 'org-agenda-date-later
)
19243 (org-defkey org-agenda-mode-map
[(shift left
)] 'org-agenda-date-earlier
)
19244 (org-defkey org-agenda-mode-map
[?\C-c ?\C-x
(right)] 'org-agenda-date-later
)
19245 (org-defkey org-agenda-mode-map
[?\C-c ?\C-x
(left)] 'org-agenda-date-earlier
)
19247 (org-defkey org-agenda-mode-map
">" 'org-agenda-date-prompt
)
19248 (org-defkey org-agenda-mode-map
"\C-c\C-s" 'org-agenda-schedule
)
19249 (org-defkey org-agenda-mode-map
"\C-c\C-d" 'org-agenda-deadline
)
19250 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
19251 (while l
(org-defkey org-agenda-mode-map
19252 (int-to-string (pop l
)) 'digit-argument
)))
19254 (org-defkey org-agenda-mode-map
"f" 'org-agenda-follow-mode
)
19255 (org-defkey org-agenda-mode-map
"l" 'org-agenda-log-mode
)
19256 (org-defkey org-agenda-mode-map
"D" 'org-agenda-toggle-diary
)
19257 (org-defkey org-agenda-mode-map
"G" 'org-agenda-toggle-time-grid
)
19258 (org-defkey org-agenda-mode-map
"r" 'org-agenda-redo
)
19259 (org-defkey org-agenda-mode-map
"g" 'org-agenda-redo
)
19260 (org-defkey org-agenda-mode-map
"e" 'org-agenda-execute
)
19261 (org-defkey org-agenda-mode-map
"q" 'org-agenda-quit
)
19262 (org-defkey org-agenda-mode-map
"x" 'org-agenda-exit
)
19263 (org-defkey org-agenda-mode-map
"\C-x\C-w" 'org-write-agenda
)
19264 (org-defkey org-agenda-mode-map
"s" 'org-save-all-org-buffers
)
19265 (org-defkey org-agenda-mode-map
"\C-x\C-s" 'org-save-all-org-buffers
)
19266 (org-defkey org-agenda-mode-map
"P" 'org-agenda-show-priority
)
19267 (org-defkey org-agenda-mode-map
"T" 'org-agenda-show-tags
)
19268 (org-defkey org-agenda-mode-map
"n" 'next-line
)
19269 (org-defkey org-agenda-mode-map
"p" 'previous-line
)
19270 (org-defkey org-agenda-mode-map
"\C-c\C-n" 'org-agenda-next-date-line
)
19271 (org-defkey org-agenda-mode-map
"\C-c\C-p" 'org-agenda-previous-date-line
)
19272 (org-defkey org-agenda-mode-map
"," 'org-agenda-priority
)
19273 (org-defkey org-agenda-mode-map
"\C-c," 'org-agenda-priority
)
19274 (org-defkey org-agenda-mode-map
"i" 'org-agenda-diary-entry
)
19275 (org-defkey org-agenda-mode-map
"c" 'org-agenda-goto-calendar
)
19276 (eval-after-load "calendar"
19277 '(org-defkey calendar-mode-map org-calendar-to-agenda-key
19278 'org-calendar-goto-agenda
))
19279 (org-defkey org-agenda-mode-map
"C" 'org-agenda-convert-date
)
19280 (org-defkey org-agenda-mode-map
"M" 'org-agenda-phases-of-moon
)
19281 (org-defkey org-agenda-mode-map
"S" 'org-agenda-sunrise-sunset
)
19282 (org-defkey org-agenda-mode-map
"h" 'org-agenda-holidays
)
19283 (org-defkey org-agenda-mode-map
"H" 'org-agenda-holidays
)
19284 (org-defkey org-agenda-mode-map
"\C-c\C-x\C-i" 'org-agenda-clock-in
)
19285 (org-defkey org-agenda-mode-map
"I" 'org-agenda-clock-in
)
19286 (org-defkey org-agenda-mode-map
"\C-c\C-x\C-o" 'org-agenda-clock-out
)
19287 (org-defkey org-agenda-mode-map
"O" 'org-agenda-clock-out
)
19288 (org-defkey org-agenda-mode-map
"\C-c\C-x\C-x" 'org-agenda-clock-cancel
)
19289 (org-defkey org-agenda-mode-map
"X" 'org-agenda-clock-cancel
)
19290 (org-defkey org-agenda-mode-map
"\C-c\C-x\C-j" 'org-clock-goto
)
19291 (org-defkey org-agenda-mode-map
"J" 'org-clock-goto
)
19292 (org-defkey org-agenda-mode-map
"+" 'org-agenda-priority-up
)
19293 (org-defkey org-agenda-mode-map
"-" 'org-agenda-priority-down
)
19294 (org-defkey org-agenda-mode-map
[(shift up
)] 'org-agenda-priority-up
)
19295 (org-defkey org-agenda-mode-map
[(shift down
)] 'org-agenda-priority-down
)
19296 (org-defkey org-agenda-mode-map
[?\C-c ?\C-x
(up)] 'org-agenda-priority-up
)
19297 (org-defkey org-agenda-mode-map
[?\C-c ?\C-x
(down)] 'org-agenda-priority-down
)
19298 (org-defkey org-agenda-mode-map
[(right)] 'org-agenda-later
)
19299 (org-defkey org-agenda-mode-map
[(left)] 'org-agenda-earlier
)
19300 (org-defkey org-agenda-mode-map
"\C-c\C-x\C-c" 'org-agenda-columns
)
19302 (defvar org-agenda-keymap
(copy-keymap org-agenda-mode-map
)
19303 "Local keymap for agenda entries from Org-mode.")
19305 (org-defkey org-agenda-keymap
19306 (if (featurep 'xemacs
) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse
)
19307 (org-defkey org-agenda-keymap
19308 (if (featurep 'xemacs
) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse
)
19309 (when org-agenda-mouse-1-follows-link
19310 (org-defkey org-agenda-keymap
[follow-link
] 'mouse-face
))
19311 (easy-menu-define org-agenda-menu org-agenda-mode-map
"Agenda menu"
19315 ["Show" org-agenda-show t
]
19316 ["Go To (other window)" org-agenda-goto t
]
19317 ["Go To (this window)" org-agenda-switch-to t
]
19318 ["Follow Mode" org-agenda-follow-mode
19319 :style toggle
:selected org-agenda-follow-mode
:active t
]
19320 ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t
]
19322 ["Cycle TODO" org-agenda-todo t
]
19323 ["Archive subtree" org-agenda-archive t
]
19324 ["Delete subtree" org-agenda-kill t
]
19326 ["Goto Today" org-agenda-goto-today
(org-agenda-check-type nil
'agenda
'timeline
)]
19327 ["Next Dates" org-agenda-later
(org-agenda-check-type nil
'agenda
)]
19328 ["Previous Dates" org-agenda-earlier
(org-agenda-check-type nil
'agenda
)]
19329 ["Jump to date" org-agenda-goto-date
(org-agenda-check-type nil
'agenda
)]
19331 ("Tags and Properties"
19332 ["Show all Tags" org-agenda-show-tags t
]
19333 ["Set Tags current line" org-agenda-set-tags
(not (org-region-active-p))]
19334 ["Change tag in region" org-agenda-set-tags
(org-region-active-p)]
19336 ["Column View" org-columns t
])
19338 ["Schedule" org-agenda-schedule t
]
19339 ["Set Deadline" org-agenda-deadline t
]
19341 ["Change Date +1 day" org-agenda-date-later
(org-agenda-check-type nil
'agenda
'timeline
)]
19342 ["Change Date -1 day" org-agenda-date-earlier
(org-agenda-check-type nil
'agenda
'timeline
)]
19343 ["Change Date to ..." org-agenda-date-prompt
(org-agenda-check-type nil
'agenda
'timeline
)])
19345 ["Clock in" org-agenda-clock-in t
]
19346 ["Clock out" org-agenda-clock-out t
]
19347 ["Clock cancel" org-agenda-clock-cancel t
]
19348 ["Goto running clock" org-clock-goto t
])
19350 ["Set Priority" org-agenda-priority t
]
19351 ["Increase Priority" org-agenda-priority-up t
]
19352 ["Decrease Priority" org-agenda-priority-down t
]
19353 ["Show Priority" org-agenda-show-priority t
])
19355 ["New Diary Entry" org-agenda-diary-entry
(org-agenda-check-type nil
'agenda
'timeline
)]
19356 ["Goto Calendar" org-agenda-goto-calendar
(org-agenda-check-type nil
'agenda
'timeline
)]
19357 ["Phases of the Moon" org-agenda-phases-of-moon
(org-agenda-check-type nil
'agenda
'timeline
)]
19358 ["Sunrise/Sunset" org-agenda-sunrise-sunset
(org-agenda-check-type nil
'agenda
'timeline
)]
19359 ["Holidays" org-agenda-holidays
(org-agenda-check-type nil
'agenda
'timeline
)]
19360 ["Convert" org-agenda-convert-date
(org-agenda-check-type nil
'agenda
'timeline
)]
19362 ["Create iCalendar file" org-export-icalendar-combine-agenda-files t
])
19365 ["Day View" org-agenda-day-view
:active
(org-agenda-check-type nil
'agenda
)
19366 :style radio
:selected
(equal org-agenda-ndays
1)]
19367 ["Week View" org-agenda-week-view
:active
(org-agenda-check-type nil
'agenda
)
19368 :style radio
:selected
(equal org-agenda-ndays
7)]
19369 ["Month View" org-agenda-month-view
:active
(org-agenda-check-type nil
'agenda
)
19370 :style radio
:selected
(member org-agenda-ndays
'(28 29 30 31))]
19371 ["Year View" org-agenda-year-view
:active
(org-agenda-check-type nil
'agenda
)
19372 :style radio
:selected
(member org-agenda-ndays
'(365 366))]
19374 ["Show Logbook entries" org-agenda-log-mode
19375 :style toggle
:selected org-agenda-show-log
:active
(org-agenda-check-type nil
'agenda
'timeline
)]
19376 ["Include Diary" org-agenda-toggle-diary
19377 :style toggle
:selected org-agenda-include-diary
:active
(org-agenda-check-type nil
'agenda
)]
19378 ["Use Time Grid" org-agenda-toggle-time-grid
19379 :style toggle
:selected org-agenda-use-time-grid
:active
(org-agenda-check-type nil
'agenda
)])
19380 ["Write view to file" org-write-agenda t
]
19381 ["Rebuild buffer" org-agenda-redo t
]
19382 ["Save all Org-mode Buffers" org-save-all-org-buffers t
]
19384 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list
]
19386 ["Quit" org-agenda-quit t
]
19387 ["Exit and Release Buffers" org-agenda-exit t
]
19392 (defvar org-agenda-allow-remote-undo t
19393 "Non-nil means, allow remote undo from the agenda buffer.")
19394 (defvar org-agenda-undo-list nil
19395 "List of undoable operations in the agenda since last refresh.")
19396 (defvar org-agenda-undo-has-started-in nil
19397 "Buffers that have already seen `undo-start' in the current undo sequence.")
19398 (defvar org-agenda-pending-undo-list nil
19399 "In a series of undo commands, this is the list of remaning undo items.")
19401 (defmacro org-if-unprotected
(&rest body
)
19402 "Execute BODY if there is no `org-protected' text property at point."
19403 (declare (debug t
))
19404 `(unless (get-text-property (point) 'org-protected
)
19407 (defmacro org-with-remote-undo
(_buffer &rest _body
)
19408 "Execute BODY while recording undo information in two buffers."
19409 (declare (indent 1) (debug t
))
19410 `(let ((_cline (org-current-line))
19411 (_cmd this-command
)
19412 (_buf1 (current-buffer))
19414 (_undo1 buffer-undo-list
)
19415 (_undo2 (with-current-buffer ,_buffer buffer-undo-list
))
19418 (when org-agenda-allow-remote-undo
19419 (setq _c1
(org-verify-change-for-undo
19420 _undo1
(with-current-buffer _buf1 buffer-undo-list
))
19421 _c2
(org-verify-change-for-undo
19422 _undo2
(with-current-buffer _buf2 buffer-undo-list
)))
19424 ;; make sure there are undo boundaries
19425 (and _c1
(with-current-buffer _buf1
(undo-boundary)))
19426 (and _c2
(with-current-buffer _buf2
(undo-boundary)))
19427 ;; remember which buffer to undo
19428 (push (list _cmd _cline _buf1 _c1 _buf2 _c2
)
19429 org-agenda-undo-list
)))))
19431 (defun org-agenda-undo ()
19432 "Undo a remote editing step in the agenda.
19433 This undoes changes both in the agenda buffer and in the remote buffer
19434 that have been changed along."
19436 (or org-agenda-allow-remote-undo
19437 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
19438 (if (not (eq this-command last-command
))
19439 (setq org-agenda-undo-has-started-in nil
19440 org-agenda-pending-undo-list org-agenda-undo-list
))
19441 (if (not org-agenda-pending-undo-list
)
19442 (error "No further undo information"))
19443 (let* ((entry (pop org-agenda-pending-undo-list
))
19444 buf line cmd rembuf
)
19445 (setq cmd
(pop entry
) line
(pop entry
))
19446 (setq rembuf
(nth 2 entry
))
19447 (org-with-remote-undo rembuf
19448 (while (bufferp (setq buf
(pop entry
)))
19450 (with-current-buffer buf
19451 (let ((last-undo-buffer buf
)
19452 (inhibit-read-only t
))
19453 (unless (memq buf org-agenda-undo-has-started-in
)
19454 (push buf org-agenda-undo-has-started-in
)
19455 (make-local-variable 'pending-undo-list
)
19457 (while (and pending-undo-list
19458 (listp pending-undo-list
)
19459 (not (car pending-undo-list
)))
19460 (pop pending-undo-list
))
19463 (message "`%s' undone (buffer %s)" cmd
(buffer-name rembuf
))))
19465 (defun org-verify-change-for-undo (l1 l2
)
19466 "Verify that a real change occurred between the undo lists L1 and L2."
19467 (while (and l1
(listp l1
) (null (car l1
))) (pop l1
))
19468 (while (and l2
(listp l2
) (null (car l2
))) (pop l2
))
19471 ;;; Agenda dispatch
19473 (defvar org-agenda-restrict nil
)
19474 (defvar org-agenda-restrict-begin
(make-marker))
19475 (defvar org-agenda-restrict-end
(make-marker))
19476 (defvar org-agenda-last-dispatch-buffer nil
)
19477 (defvar org-agenda-overriding-restriction nil
)
19480 (defun org-agenda (arg &optional keys restriction
)
19481 "Dispatch agenda commands to collect entries to the agenda buffer.
19482 Prompts for a command to execute. Any prefix arg will be passed
19483 on to the selected command. The default selections are:
19485 a Call `org-agenda-list' to display the agenda for current day or week.
19486 t Call `org-todo-list' to display the global todo list.
19487 T Call `org-todo-list' to display the global todo list, select only
19488 entries with a specific TODO keyword (the user gets a prompt).
19489 m Call `org-tags-view' to display headlines with tags matching
19490 a condition (the user is prompted for the condition).
19491 M Like `m', but select only TODO entries, no ordinary headlines.
19492 L Create a timeline for the current buffer.
19493 e Export views to associated files.
19495 More commands can be added by configuring the variable
19496 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
19497 searches can be pre-defined in this way.
19499 If the current buffer is in Org-mode and visiting a file, you can also
19500 first press `<' once to indicate that the agenda should be temporarily
19501 \(until the next use of \\[org-agenda]) restricted to the current file.
19502 Pressing `<' twice means to restrict to the current subtree or region
19506 (let* ((prefix-descriptions nil
)
19507 (org-agenda-custom-commands-orig org-agenda-custom-commands
)
19508 (org-agenda-custom-commands
19509 ;; normalize different versions
19513 (cond ((stringp (cdr x
))
19514 (push x prefix-descriptions
)
19516 ((stringp (nth 1 x
)) x
)
19517 ((not (nth 1 x
)) (cons (car x
) (cons "" (cddr x
))))
19518 (t (cons (car x
) (cons "" (cdr x
))))))
19519 org-agenda-custom-commands
)))
19520 (buf (current-buffer))
19521 (bfn (buffer-file-name (buffer-base-buffer)))
19522 entry key type match lprops ans
)
19523 ;; Turn off restriction unless there is an overriding one
19524 (unless org-agenda-overriding-restriction
19525 (put 'org-agenda-files
'org-restrict nil
)
19526 (setq org-agenda-restrict nil
)
19527 (move-marker org-agenda-restrict-begin nil
)
19528 (move-marker org-agenda-restrict-end nil
))
19529 ;; Delete old local properties
19530 (put 'org-agenda-redo-command
'org-lprops nil
)
19531 ;; Remember where this call originated
19532 (setq org-agenda-last-dispatch-buffer
(current-buffer))
19534 (setq ans
(org-agenda-get-restriction-and-command prefix-descriptions
)
19536 restriction
(cdr ans
)))
19537 ;; Estabish the restriction, if any
19538 (when (and (not org-agenda-overriding-restriction
) restriction
)
19539 (put 'org-agenda-files
'org-restrict
(list bfn
))
19541 ((eq restriction
'region
)
19542 (setq org-agenda-restrict t
)
19543 (move-marker org-agenda-restrict-begin
(region-beginning))
19544 (move-marker org-agenda-restrict-end
(region-end)))
19545 ((eq restriction
'subtree
)
19547 (setq org-agenda-restrict t
)
19548 (org-back-to-heading t
)
19549 (move-marker org-agenda-restrict-begin
(point))
19550 (move-marker org-agenda-restrict-end
19551 (progn (org-end-of-subtree t
)))))))
19553 (require 'calendar
) ; FIXME: can we avoid this for some commands?
19554 ;; For example the todo list should not need it (but does...)
19556 ((setq entry
(assoc keys org-agenda-custom-commands
))
19557 (if (or (symbolp (nth 2 entry
)) (functionp (nth 2 entry
)))
19559 (setq type
(nth 2 entry
) match
(nth 3 entry
) lprops
(nth 4 entry
))
19560 (put 'org-agenda-redo-command
'org-lprops lprops
)
19563 (org-let lprops
'(org-agenda-list current-prefix-arg
)))
19564 ((eq type
'alltodo
)
19565 (org-let lprops
'(org-todo-list current-prefix-arg
)))
19567 (org-let lprops
'(org-agenda-list-stuck-projects
19568 current-prefix-arg
)))
19570 (org-let lprops
'(org-tags-view current-prefix-arg match
)))
19571 ((eq type
'tags-todo
)
19572 (org-let lprops
'(org-tags-view '(4) match
)))
19574 (org-let lprops
'(org-todo-list match
)))
19575 ((eq type
'tags-tree
)
19576 (org-check-for-org-mode)
19577 (org-let lprops
'(org-tags-sparse-tree current-prefix-arg match
)))
19578 ((eq type
'todo-tree
)
19579 (org-check-for-org-mode)
19581 '(org-occur (concat "^" outline-regexp
"[ \t]*"
19582 (regexp-quote match
) "\\>"))))
19583 ((eq type
'occur-tree
)
19584 (org-check-for-org-mode)
19585 (org-let lprops
'(org-occur match
)))
19587 (org-let lprops
'(funcall type match
)))
19589 (org-let lprops
'(funcall type match
)))
19590 (t (error "Invalid custom agenda command type %s" type
))))
19591 (org-run-agenda-series (nth 1 entry
) (cddr entry
))))
19593 (setq org-agenda-custom-commands org-agenda-custom-commands-orig
)
19594 (customize-variable 'org-agenda-custom-commands
))
19595 ((equal keys
"a") (call-interactively 'org-agenda-list
))
19596 ((equal keys
"t") (call-interactively 'org-todo-list
))
19597 ((equal keys
"T") (org-call-with-arg 'org-todo-list
(or arg
'(4))))
19598 ((equal keys
"m") (call-interactively 'org-tags-view
))
19599 ((equal keys
"M") (org-call-with-arg 'org-tags-view
(or arg
'(4))))
19600 ((equal keys
"e") (call-interactively 'org-store-agenda-views
))
19602 (unless (org-mode-p)
19603 (error "This is not an Org-mode file"))
19604 (unless restriction
19605 (put 'org-agenda-files
'org-restrict
(list bfn
))
19606 (org-call-with-arg 'org-timeline arg
)))
19607 ((equal keys
"#") (call-interactively 'org-agenda-list-stuck-projects
))
19608 ((equal keys
"/") (call-interactively 'org-occur-in-agenda-files
))
19609 ((equal keys
"!") (customize-variable 'org-stuck-projects
))
19610 (t (error "Invalid agenda key"))))))
19612 (defun org-agenda-normalize-custom-commands (cmds)
19616 (cond ((stringp (cdr x
)) nil
)
19617 ((stringp (nth 1 x
)) x
)
19618 ((not (nth 1 x
)) (cons (car x
) (cons "" (cddr x
))))
19619 (t (cons (car x
) (cons "" (cdr x
))))))
19622 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
19623 "The user interface for selecting an agenda command."
19625 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
19626 (restrict-ok (and bfn
(org-mode-p)))
19627 (region-p (org-region-active-p))
19628 (custom org-agenda-custom-commands
)
19630 restriction second-time
19631 c entry key type match prefixes rmheader header-end custom1 desc
)
19632 (save-window-excursion
19633 (delete-other-windows)
19634 (org-switch-to-buffer-other-window " *Agenda Commands*")
19636 (insert (eval-when-compile
19639 Press key for an agenda command: < Buffer,subtree/region restriction
19640 -------------------------------- > Remove restriction
19641 a Agenda for current week or day e Export agenda views
19642 t List of all TODO entries T Entries with special TODO kwd
19643 m Match a TAGS query M Like m, but only TODO entries
19644 L Timeline for current buffer # List stuck projects (!=configure)
19645 / Multi-occur C Configure custom agenda commands
19648 (while (string-match
19649 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
19651 (setq start
(match-end 0))
19652 (add-text-properties (match-beginning 2) (match-end 2)
19653 '(face bold
) header
))
19655 (setq header-end
(move-marker (make-marker) (point)))
19657 (setq custom1 custom
)
19658 (when (eq rmheader t
)
19660 (re-search-forward ":" nil t
)
19661 (delete-region (match-end 0) (point-at-eol))
19664 (delete-region (match-end 0) (point-at-eol))
19665 (move-marker header-end
(match-end 0)))
19666 (goto-char header-end
)
19667 (delete-region (point) (point-max))
19668 (while (setq entry
(pop custom1
))
19669 (setq key
(car entry
) desc
(nth 1 entry
)
19670 type
(nth 2 entry
) match
(nth 3 entry
))
19671 (if (> (length key
) 1)
19672 (add-to-list 'prefixes
(string-to-char key
))
19676 (org-add-props (copy-sequence key
)
19679 ((string-match "\\S-" desc
) desc
)
19680 ((eq type
'agenda
) "Agenda for current week or day")
19681 ((eq type
'alltodo
) "List of all TODO entries")
19682 ((eq type
'stuck
) "List of stuck projects")
19683 ((eq type
'todo
) "TODO keyword")
19684 ((eq type
'tags
) "Tags query")
19685 ((eq type
'tags-todo
) "Tags (TODO)")
19686 ((eq type
'tags-tree
) "Tags tree")
19687 ((eq type
'todo-tree
) "TODO kwd tree")
19688 ((eq type
'occur-tree
) "Occur tree")
19689 ((functionp type
) (if (symbolp type
)
19691 "Lambda expression"))
19695 (org-add-props match nil
'face
'org-warning
))
19697 (format "set of %d commands" (length match
)))
19703 (org-add-props (char-to-string x
)
19705 (or (cdr (assoc (concat selstring
(char-to-string x
))
19706 prefix-descriptions
))
19709 (goto-char (point-min))
19710 (when (fboundp 'fit-window-to-buffer
)
19712 (if (not (pos-visible-in-window-p (point-max)))
19713 (fit-window-to-buffer))
19714 (setq second-time t
)
19715 (fit-window-to-buffer)))
19716 (message "Press key for agenda command%s:"
19717 (if (or restrict-ok org-agenda-overriding-restriction
)
19718 (if org-agenda-overriding-restriction
19719 " (restriction lock active)"
19721 (format " (restricted to %s)" restriction
)
19722 " (unrestricted)"))
19724 (setq c
(read-char-exclusive))
19727 ((assoc (char-to-string c
) custom
)
19728 (setq selstring
(concat selstring
(char-to-string c
)))
19729 (throw 'exit
(cons selstring restriction
)))
19731 (setq selstring
(concat selstring
(char-to-string c
))
19733 rmheader
(or rmheader t
)
19734 custom
(delq nil
(mapcar
19736 (if (or (= (length (car x
)) 1)
19737 (/= (string-to-char (car x
)) c
))
19739 (cons (substring (car x
) 1) (cdr x
))))
19741 ((and (not restrict-ok
) (memq c
'(?
1 ?
0 ?
<)))
19742 (message "Restriction is only possible in Org-mode buffers")
19743 (ding) (sit-for 1))
19745 (org-agenda-remove-restriction-lock 'noupdate
)
19746 (setq restriction
'buffer
))
19748 (org-agenda-remove-restriction-lock 'noupdate
)
19749 (setq restriction
(if region-p
'region
'subtree
)))
19751 (org-agenda-remove-restriction-lock 'noupdate
)
19754 ((eq restriction
'buffer
)
19755 (if region-p
'region
'subtree
))
19756 ((memq restriction
'(subtree region
))
19760 (org-agenda-remove-restriction-lock 'noupdate
)
19761 (setq restriction nil
))
19762 ((and (equal selstring
"") (memq c
'(?a ?t ?m ?L ?C ?e ?T ?M ?
# ?
! ?
/)))
19763 (throw 'exit
(cons (setq selstring
(char-to-string c
)) restriction
)))
19764 ((and (> (length selstring
) 0) (eq c ?\d
))
19766 (org-agenda-get-restriction-and-command prefix-descriptions
))
19768 ((equal c ?q
) (error "Abort"))
19769 (t (error "Invalid key %c" c
))))))))
19771 (defun org-run-agenda-series (name series
)
19772 (org-prepare-agenda name
)
19773 (let* ((org-agenda-multi t
)
19774 (redo (list 'org-run-agenda-series name
(list 'quote series
)))
19775 (cmds (car series
))
19776 (gprops (nth 1 series
))
19777 match
;; The byte compiler incorrectly complains about this. Keep it!
19779 (while (setq cmd
(pop cmds
))
19780 (setq type
(car cmd
) match
(nth 1 cmd
) lprops
(nth 2 cmd
))
19783 (org-let2 gprops lprops
19784 '(call-interactively 'org-agenda-list
)))
19785 ((eq type
'alltodo
)
19786 (org-let2 gprops lprops
19787 '(call-interactively 'org-todo-list
)))
19789 (org-let2 gprops lprops
19790 '(call-interactively 'org-agenda-list-stuck-projects
)))
19792 (org-let2 gprops lprops
19793 '(org-tags-view current-prefix-arg match
)))
19794 ((eq type
'tags-todo
)
19795 (org-let2 gprops lprops
19796 '(org-tags-view '(4) match
)))
19798 (org-let2 gprops lprops
19799 '(org-todo-list match
)))
19801 (org-let2 gprops lprops
19802 '(funcall type match
)))
19803 (t (error "Invalid type in command series"))))
19805 (setq org-agenda-redo-command redo
)
19806 (goto-char (point-min)))
19807 (org-finalize-agenda))
19810 (defmacro org-batch-agenda
(cmd-key &rest parameters
)
19811 "Run an agenda command in batch mode and send the result to STDOUT.
19812 If CMD-KEY is a string of length 1, it is used as a key in
19813 `org-agenda-custom-commands' and triggers this command. If it is a
19814 longer string it is used as a tags/todo match string.
19815 Paramters are alternating variable names and values that will be bound
19816 before running the agenda command."
19819 (push (list (pop parameters
) (if parameters
(pop parameters
))) pars
))
19820 (if (> (length cmd-key
) 2)
19821 (eval (list 'let
(nreverse pars
)
19822 (list 'org-tags-view nil cmd-key
)))
19823 (eval (list 'let
(nreverse pars
) (list 'org-agenda nil cmd-key
))))
19824 (set-buffer org-agenda-buffer-name
)
19825 (princ (org-encode-for-stdout (buffer-string)))))
19827 (defun org-encode-for-stdout (string)
19828 (if (fboundp 'encode-coding-string
)
19829 (encode-coding-string string buffer-file-coding-system
)
19832 (defvar org-agenda-info nil
)
19835 (defmacro org-batch-agenda-csv
(cmd-key &rest parameters
)
19836 "Run an agenda command in batch mode and send the result to STDOUT.
19837 If CMD-KEY is a string of length 1, it is used as a key in
19838 `org-agenda-custom-commands' and triggers this command. If it is a
19839 longer string it is used as a tags/todo match string.
19840 Paramters are alternating variable names and values that will be bound
19841 before running the agenda command.
19843 The output gives a line for each selected agenda item. Each
19844 item is a list of comma-separated values, like this:
19846 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
19848 category The category of the item
19849 head The headline, without TODO kwd, TAGS and PRIORITY
19850 type The type of the agenda entry, can be
19851 todo selected in TODO match
19852 tagsmatch selected in tags match
19853 diary imported from diary
19854 deadline a deadline on given date
19855 scheduled scheduled on given date
19856 timestamp entry has timestamp on given date
19857 closed entry was closed on given date
19858 upcoming-deadline warning about deadline
19859 past-scheduled forwarded scheduled item
19860 block entry has date block including g. date
19861 todo The todo keyword, if any
19862 tags All tags including inherited ones, separated by colons
19863 date The relevant date, like 2007-2-14
19864 time The time, like 15:00-16:50
19865 extra Sting with extra planning info
19866 priority-l The priority letter if any was given
19867 priority-n The computed numerical priority
19868 agenda-day The day in the agenda where this is listed"
19872 (push (list (pop parameters
) (if parameters
(pop parameters
))) pars
))
19873 (push (list 'org-agenda-remove-tags t
) pars
)
19874 (if (> (length cmd-key
) 2)
19875 (eval (list 'let
(nreverse pars
)
19876 (list 'org-tags-view nil cmd-key
)))
19877 (eval (list 'let
(nreverse pars
) (list 'org-agenda nil cmd-key
))))
19878 (set-buffer org-agenda-buffer-name
)
19879 (let* ((lines (org-split-string (buffer-string) "\n"))
19881 (while (setq line
(pop lines
))
19883 (if (not (get-text-property 0 'org-category line
)) (throw 'next nil
))
19884 (setq org-agenda-info
19885 (org-fix-agenda-info (text-properties-at 0 line
)))
19887 (org-encode-for-stdout
19888 (mapconcat 'org-agenda-export-csv-mapper
19889 '(org-category txt type todo tags date time-of-day extra
19890 priority-letter priority agenda-day
)
19894 (defun org-fix-agenda-info (props)
19895 "Make sure all properties on an agenda item have a canonical form,
19896 so the export commands can easily use it."
19898 (when (setq tmp
(plist-get props
'tags
))
19899 (setq props
(plist-put props
'tags
(mapconcat 'identity tmp
":"))))
19900 (when (setq tmp
(plist-get props
'date
))
19901 (if (integerp tmp
) (setq tmp
(calendar-gregorian-from-absolute tmp
)))
19902 (let ((calendar-date-display-form '(year "-" month
"-" day
)))
19903 '((format "%4d, %9s %2s, %4s" dayname monthname day year
))
19905 (setq tmp
(calendar-date-string tmp
)))
19906 (setq props
(plist-put props
'date tmp
)))
19907 (when (setq tmp
(plist-get props
'day
))
19908 (if (integerp tmp
) (setq tmp
(calendar-gregorian-from-absolute tmp
)))
19909 (let ((calendar-date-display-form '(year "-" month
"-" day
)))
19910 (setq tmp
(calendar-date-string tmp
)))
19911 (setq props
(plist-put props
'day tmp
))
19912 (setq props
(plist-put props
'agenda-day tmp
)))
19913 (when (setq tmp
(plist-get props
'txt
))
19914 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp
)
19915 (plist-put props
'priority-letter
(match-string 1 tmp
))
19916 (setq tmp
(replace-match "" t t tmp
)))
19917 (when (and (setq re
(plist-get props
'org-todo-regexp
))
19918 (setq re
(concat "\\`\\.*" re
" ?"))
19919 (string-match re tmp
))
19920 (plist-put props
'todo
(match-string 1 tmp
))
19921 (setq tmp
(replace-match "" t t tmp
)))
19922 (plist-put props
'txt tmp
)))
19925 (defun org-agenda-export-csv-mapper (prop)
19926 (let ((res (plist-get org-agenda-info prop
)))
19930 ((stringp res
) res
)
19931 (t (prin1-to-string res
))))
19932 (while (string-match "," res
)
19933 (setq res
(replace-match ";" t t res
)))
19938 (defun org-store-agenda-views (&rest parameters
)
19940 (eval (list 'org-batch-store-agenda-views
)))
19942 ;; FIXME, why is this a macro?????
19944 (defmacro org-batch-store-agenda-views
(&rest parameters
)
19945 "Run all custom agenda commands that have a file argument."
19946 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands
))
19947 (pop-up-frames nil
)
19948 (dir default-directory
)
19949 pars cmd thiscmdkey files opts
)
19951 (push (list (pop parameters
) (if parameters
(pop parameters
))) pars
))
19952 (setq pars
(reverse pars
))
19953 (save-window-excursion
19955 (setq cmd
(pop cmds
)
19956 thiscmdkey
(car cmd
)
19959 (if (stringp files
) (setq files
(list files
)))
19961 (eval (list 'let
(append org-agenda-exporter-settings opts pars
)
19962 (list 'org-agenda nil thiscmdkey
)))
19963 (set-buffer org-agenda-buffer-name
)
19965 (eval (list 'let
(append org-agenda-exporter-settings opts pars
)
19966 (list 'org-write-agenda
19967 (expand-file-name (pop files
) dir
) t
))))
19968 (and (get-buffer org-agenda-buffer-name
)
19969 (kill-buffer org-agenda-buffer-name
)))))))
19971 (defun org-write-agenda (file &optional nosettings
)
19972 "Write the current buffer (an agenda view) as a file.
19973 Depending on the extension of the file name, plain text (.txt),
19974 HTML (.html or .htm) or Postscript (.ps) is produced.
19975 If NOSETTINGS is given, do not scope the settings of
19976 `org-agenda-exporter-settings' into the export commands. This is used when
19977 the settings have already been scoped and we do not wish to overrule other,
19978 higher priority settings."
19979 (interactive "FWrite agenda to file: ")
19980 (if (not (file-writable-p file
))
19981 (error "Cannot write agenda to file %s" file
))
19983 ((string-match "\\.html?\\'" file
) (require 'htmlize
))
19984 ((string-match "\\.ps\\'" file
) (require 'ps-print
)))
19985 (org-let (if nosettings nil org-agenda-exporter-settings
)
19987 (save-window-excursion
19989 ((string-match "\\.html?\\'" file
)
19990 (set-buffer (htmlize-buffer (current-buffer)))
19992 (when (and org-agenda-export-html-style
19993 (string-match "<style>" org-agenda-export-html-style
))
19994 ;; replace <style> section with org-agenda-export-html-style
19995 (goto-char (point-min))
19996 (kill-region (- (search-forward "<style") 6)
19997 (search-forward "</style>"))
19998 (insert org-agenda-export-html-style
))
20000 (kill-buffer (current-buffer))
20001 (message "HTML written to %s" file
))
20002 ((string-match "\\.ps\\'" file
)
20003 (ps-print-buffer-with-faces file
)
20004 (message "Postscript written to %s" file
))
20006 (let ((bs (buffer-string)))
20010 (kill-buffer (current-buffer))
20011 (message "Plain text written to %s" file
))))))
20012 (set-buffer org-agenda-buffer-name
)))
20014 (defmacro org-no-read-only
(&rest body
)
20015 "Inhibit read-only for BODY."
20016 `(let ((inhibit-read-only t
)) ,@body
))
20018 (defun org-check-for-org-mode ()
20019 "Make sure current buffer is in org-mode. Error if not."
20021 (error "Cannot execute org-mode agenda command on buffer in %s."
20024 (defun org-fit-agenda-window ()
20025 "Fit the window to the buffer size."
20026 (and (memq org-agenda-window-setup
'(reorganize-frame))
20027 (fboundp 'fit-window-to-buffer
)
20028 (fit-window-to-buffer
20030 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions
)))
20031 (floor (* (frame-height) (car org-agenda-window-frame-fractions
))))))
20033 ;;; Agenda file list
20035 (defun org-agenda-files (&optional unrestricted
)
20036 "Get the list of agenda files.
20037 Optional UNRESTRICTED means return the full list even if a restriction
20038 is currently in place."
20041 ((and (not unrestricted
) (get 'org-agenda-files
'org-restrict
)))
20042 ((stringp org-agenda-files
) (org-read-agenda-file-list))
20043 ((listp org-agenda-files
) org-agenda-files
)
20044 (t (error "Invalid value of `org-agenda-files'")))))
20045 (setq files
(apply 'append
20046 (mapcar (lambda (f)
20047 (if (file-directory-p f
)
20048 (directory-files f t
20049 org-agenda-file-regexp
)
20052 (if org-agenda-skip-unavailable-files
20056 (and (file-readable-p file
) file
)))
20058 files
))) ; `org-check-agenda-file' will remove them from the list
20060 (defun org-edit-agenda-file-list ()
20061 "Edit the list of agenda files.
20062 Depending on setup, this either uses customize to edit the variable
20063 `org-agenda-files', or it visits the file that is holding the list. In the
20064 latter case, the buffer is set up in a way that saving it automatically kills
20065 the buffer and restores the previous window configuration."
20067 (if (stringp org-agenda-files
)
20068 (let ((cw (current-window-configuration)))
20069 (find-file org-agenda-files
)
20070 (org-set-local 'org-window-configuration cw
)
20071 (org-add-hook 'after-save-hook
20073 (set-window-configuration
20074 (prog1 org-window-configuration
20075 (kill-buffer (current-buffer))))
20076 (org-install-agenda-files-menu)
20077 (message "New agenda file list installed"))
20079 (message "%s" (substitute-command-keys
20080 "Edit list and finish with \\[save-buffer]")))
20081 (customize-variable 'org-agenda-files
)))
20083 (defun org-store-new-agenda-file-list (list)
20084 "Set new value for the agenda file list and save it correcly."
20085 (if (stringp org-agenda-files
)
20086 (let ((f org-agenda-files
) b
)
20087 (while (setq b
(find-buffer-visiting f
)) (kill-buffer b
))
20089 (insert (mapconcat 'identity list
"\n") "\n")))
20090 (let ((org-mode-hook nil
) (default-major-mode 'fundamental-mode
))
20091 (setq org-agenda-files list
)
20092 (customize-save-variable 'org-agenda-files org-agenda-files
))))
20094 (defun org-read-agenda-file-list ()
20095 "Read the list of agenda files from a file."
20096 (when (stringp org-agenda-files
)
20098 (insert-file-contents org-agenda-files
)
20099 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
20103 (defun org-cycle-agenda-files ()
20104 "Cycle through the files in `org-agenda-files'.
20105 If the current buffer visits an agenda file, find the next one in the list.
20106 If the current buffer does not, find the first agenda file."
20108 (let* ((fs (org-agenda-files t
))
20109 (files (append fs
(list (car fs
))))
20110 (tcf (if buffer-file-name
(file-truename buffer-file-name
)))
20112 (unless files
(error "No agenda files"))
20114 (while (setq file
(pop files
))
20115 (if (equal (file-truename file
) tcf
)
20117 (find-file (car files
))
20119 (find-file (car fs
)))
20120 (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
20122 (defun org-agenda-file-to-front (&optional to-end
)
20123 "Move/add the current file to the top of the agenda file list.
20124 If the file is not present in the list, it is added to the front. If it is
20125 present, it is moved there. With optional argument TO-END, add/move to the
20128 (let ((org-agenda-skip-unavailable-files nil
)
20129 (file-alist (mapcar (lambda (x)
20130 (cons (file-truename x
) x
))
20131 (org-agenda-files t
)))
20132 (ctf (file-truename buffer-file-name
))
20134 (setq x
(assoc ctf file-alist
) had x
)
20136 (if (not x
) (setq x
(cons ctf
(abbreviate-file-name buffer-file-name
))))
20138 (setq file-alist
(append (delq x file-alist
) (list x
)))
20139 (setq file-alist
(cons x
(delq x file-alist
))))
20140 (org-store-new-agenda-file-list (mapcar 'cdr file-alist
))
20141 (org-install-agenda-files-menu)
20142 (message "File %s to %s of agenda file list"
20143 (if had
"moved" "added") (if to-end
"end" "front"))))
20145 (defun org-remove-file (&optional file
)
20146 "Remove current file from the list of files in variable `org-agenda-files'.
20147 These are the files which are being checked for agenda entries.
20148 Optional argument FILE means, use this file instead of the current."
20150 (let* ((org-agenda-skip-unavailable-files nil
)
20151 (file (or file buffer-file-name
))
20152 (true-file (file-truename file
))
20153 (afile (abbreviate-file-name file
))
20154 (files (delq nil
(mapcar
20156 (if (equal true-file
20159 (org-agenda-files t
)))))
20160 (if (not (= (length files
) (length (org-agenda-files t
))))
20162 (org-store-new-agenda-file-list files
)
20163 (org-install-agenda-files-menu)
20164 (message "Removed file: %s" afile
))
20165 (message "File was not in list: %s (not removed)" afile
))))
20167 (defun org-file-menu-entry (file)
20168 (vector file
(list 'find-file file
) t
))
20170 (defun org-check-agenda-file (file)
20171 "Make sure FILE exists. If not, ask user what to do."
20172 (when (not (file-exists-p file
))
20173 (message "non-existent file %s. [R]emove from list or [A]bort?"
20174 (abbreviate-file-name file
))
20175 (let ((r (downcase (read-char-exclusive))))
20178 (org-remove-file file
)
20179 (throw 'nextfile t
))
20180 (t (error "Abort"))))))
20182 ;;; Agenda prepare and finalize
20184 (defvar org-agenda-multi nil
) ; dynammically scoped
20185 (defvar org-agenda-buffer-name
"*Org Agenda*")
20186 (defvar org-pre-agenda-window-conf nil
)
20187 (defvar org-agenda-name nil
)
20188 (defun org-prepare-agenda (&optional name
)
20189 (setq org-todo-keywords-for-agenda nil
)
20190 (setq org-done-keywords-for-agenda nil
)
20191 (if org-agenda-multi
20193 (setq buffer-read-only nil
)
20194 (goto-char (point-max))
20195 (unless (or (bobp) org-agenda-compact-blocks
)
20196 (insert "\n" (make-string (window-width) ?
=) "\n"))
20197 (narrow-to-region (point) (point-max)))
20198 (org-agenda-reset-markers)
20199 (org-prepare-agenda-buffers (org-agenda-files))
20200 (setq org-todo-keywords-for-agenda
20201 (org-uniquify org-todo-keywords-for-agenda
))
20202 (setq org-done-keywords-for-agenda
20203 (org-uniquify org-done-keywords-for-agenda
))
20204 (let* ((abuf (get-buffer-create org-agenda-buffer-name
))
20205 (awin (get-buffer-window abuf
)))
20207 ((equal (current-buffer) abuf
) nil
)
20208 (awin (select-window awin
))
20209 ((not (setq org-pre-agenda-window-conf
(current-window-configuration))))
20210 ((equal org-agenda-window-setup
'current-window
)
20211 (switch-to-buffer abuf
))
20212 ((equal org-agenda-window-setup
'other-window
)
20213 (org-switch-to-buffer-other-window abuf
))
20214 ((equal org-agenda-window-setup
'other-frame
)
20215 (switch-to-buffer-other-frame abuf
))
20216 ((equal org-agenda-window-setup
'reorganize-frame
)
20217 (delete-other-windows)
20218 (org-switch-to-buffer-other-window abuf
))))
20219 (setq buffer-read-only nil
)
20222 (and name
(not org-agenda-name
)
20223 (org-set-local 'org-agenda-name name
)))
20224 (setq buffer-read-only nil
))
20226 (defun org-finalize-agenda ()
20227 "Finishing touch for the agenda buffer, called just before displaying it."
20228 (unless org-agenda-multi
20230 (let ((inhibit-read-only t
))
20231 (goto-char (point-min))
20232 (while (org-activate-bracket-links (point-max))
20233 (add-text-properties (match-beginning 0) (match-end 0)
20235 (org-agenda-align-tags)
20236 (unless org-agenda-with-colors
20237 (remove-text-properties (point-min) (point-max) '(face nil
))))
20238 (if (and (boundp 'org-overriding-columns-format
)
20239 org-overriding-columns-format
)
20240 (org-set-local 'org-overriding-columns-format
20241 org-overriding-columns-format
))
20242 (if (and (boundp 'org-agenda-view-columns-initially
)
20243 org-agenda-view-columns-initially
)
20244 (org-agenda-columns))
20245 (when org-agenda-fontify-priorities
20246 (org-fontify-priorities))
20247 (run-hooks 'org-finalize-agenda-hook
)
20248 (setq org-agenda-type
(get-text-property (point) 'org-agenda-type
))
20251 (defun org-fontify-priorities ()
20252 "Make highest priority lines bold, and lowest italic."
20254 (mapc (lambda (o) (if (eq (org-overlay-get o
'org-type
) 'org-priority
)
20255 (org-delete-overlay o
)))
20256 (org-overlays-in (point-min) (point-max)))
20258 (let ((inhibit-read-only t
)
20260 (goto-char (point-min))
20261 (while (re-search-forward "\\[#\\(.\\)\\]" nil t
)
20262 (setq h
(or (get-char-property (point) 'org-highest-priority
)
20263 org-highest-priority
)
20264 l
(or (get-char-property (point) 'org-lowest-priority
)
20265 org-lowest-priority
)
20266 p
(string-to-char (match-string 1))
20267 b
(match-beginning 0) e
(point-at-eol)
20268 ov
(org-make-overlay b e
))
20271 (cond ((listp org-agenda-fontify-priorities
)
20272 (cdr (assoc p org-agenda-fontify-priorities
)))
20273 ((equal p l
) 'italic
)
20274 ((equal p h
) 'bold
)))
20275 (org-overlay-put ov
'org-type
'org-priority
)))))
20277 (defun org-prepare-agenda-buffers (files)
20278 "Create buffers for all agenda files, protect archived trees and comments."
20280 (let ((pa '(:org-archived t
))
20281 (pc '(:org-comment t
))
20282 (pall '(:org-archived t
:org-comment t
))
20283 (inhibit-read-only t
)
20284 (rea (concat ":" org-archive-tag
":"))
20288 (while (setq file
(pop files
))
20291 (org-check-agenda-file file
)
20292 (set-buffer (org-get-agenda-file-buffer file
)))
20294 (setq bmp
(buffer-modified-p))
20295 (org-refresh-category-properties)
20296 (setq org-todo-keywords-for-agenda
20297 (append org-todo-keywords-for-agenda org-todo-keywords-1
))
20298 (setq org-done-keywords-for-agenda
20299 (append org-done-keywords-for-agenda org-done-keywords
))
20301 (remove-text-properties (point-min) (point-max) pall
)
20302 (when org-agenda-skip-archived-trees
20303 (goto-char (point-min))
20304 (while (re-search-forward rea nil t
)
20305 (if (org-on-heading-p t
)
20306 (add-text-properties (point-at-bol) (org-end-of-subtree t
) pa
))))
20307 (goto-char (point-min))
20308 (setq re
(concat "^\\*+ +" org-comment-string
"\\>"))
20309 (while (re-search-forward re nil t
)
20310 (add-text-properties
20311 (match-beginning 0) (org-end-of-subtree t
) pc
)))
20312 (set-buffer-modified-p bmp
))))))
20314 (defvar org-agenda-skip-function nil
20315 "Function to be called at each match during agenda construction.
20316 If this function returns nil, the current match should not be skipped.
20317 Otherwise, the function must return a position from where the search
20318 should be continued.
20319 This may also be a Lisp form, it will be evaluated.
20320 Never set this variable using `setq' or so, because then it will apply
20321 to all future agenda commands. Instead, bind it with `let' to scope
20322 it dynamically into the agenda-constructing command. A good way to set
20323 it is through options in org-agenda-custom-commands.")
20325 (defun org-agenda-skip ()
20326 "Throw to `:skip' in places that should be skipped.
20327 Also moves point to the end of the skipped region, so that search can
20328 continue from there."
20329 (let ((p (point-at-bol)) to fp
)
20330 (and org-agenda-skip-archived-trees
20331 (get-text-property p
:org-archived
)
20332 (org-end-of-subtree t
)
20334 (and (get-text-property p
:org-comment
)
20335 (org-end-of-subtree t
)
20337 (if (equal (char-after p
) ?
#) (throw :skip t
))
20338 (when (and (or (setq fp
(functionp org-agenda-skip-function
))
20339 (consp org-agenda-skip-function
))
20340 (setq to
(save-excursion
20343 (funcall org-agenda-skip-function
)
20344 (eval org-agenda-skip-function
))))))
20348 (defvar org-agenda-markers nil
20349 "List of all currently active markers created by `org-agenda'.")
20350 (defvar org-agenda-last-marker-time
(time-to-seconds (current-time))
20351 "Creation time of the last agenda marker.")
20353 (defun org-agenda-new-marker (&optional pos
)
20354 "Return a new agenda marker.
20355 Org-mode keeps a list of these markers and resets them when they are
20357 (let ((m (copy-marker (or pos
(point)))))
20358 (setq org-agenda-last-marker-time
(time-to-seconds (current-time)))
20359 (push m org-agenda-markers
)
20362 (defun org-agenda-reset-markers ()
20363 "Reset markers created by `org-agenda'."
20364 (while org-agenda-markers
20365 (move-marker (pop org-agenda-markers
) nil
)))
20367 (defun org-get-agenda-file-buffer (file)
20368 "Get a buffer visiting FILE. If the buffer needs to be created, add
20369 it to the list of buffers which might be released later."
20370 (let ((buf (org-find-base-buffer-visiting file
)))
20372 buf
; just return it
20373 ;; Make a new buffer and remember it
20374 (setq buf
(find-file-noselect file
))
20375 (if buf
(push buf org-agenda-new-buffers
))
20378 (defun org-release-buffers (blist)
20379 "Release all buffers in list, asking the user for confirmation when needed.
20380 When a buffer is unmodified, it is just killed. When modified, it is saved
20381 \(if the user agrees) and then killed."
20383 (while (setq buf
(pop blist
))
20384 (setq file
(buffer-file-name buf
))
20385 (when (and (buffer-modified-p buf
)
20387 (y-or-n-p (format "Save file %s? " file
)))
20388 (with-current-buffer buf
(save-buffer)))
20389 (kill-buffer buf
))))
20391 (defun org-get-category (&optional pos
)
20392 "Get the category applying to position POS."
20393 (get-text-property (or pos
(point)) 'org-category
))
20395 ;;; Agenda timeline
20397 (defvar org-agenda-only-exact-dates nil
) ; dynamically scoped
20399 (defun org-timeline (&optional include-all
)
20400 "Show a time-sorted view of the entries in the current org file.
20401 Only entries with a time stamp of today or later will be listed. With
20402 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
20403 under the current date.
20404 If the buffer contains an active region, only check the region for
20407 (require 'calendar
)
20408 (org-compile-prefix-format 'timeline
)
20409 (org-set-sorting-strategy 'timeline
)
20411 (dotodo include-all
)
20412 (doclosed org-agenda-show-log
)
20413 (entry buffer-file-name
)
20414 (date (calendar-current-date))
20415 (beg (if (org-region-active-p) (region-beginning) (point-min)))
20416 (end (if (org-region-active-p) (region-end) (point-max)))
20417 (day-numbers (org-get-all-dates beg end
'no-ranges
20418 t doclosed
; always include today
20419 org-timeline-show-empty-dates
))
20420 (org-deadline-warning-days 0)
20421 (org-agenda-only-exact-dates t
)
20422 (today (time-to-days (current-time)))
20426 (setq org-agenda-redo-command
20428 (list 'org-switch-to-buffer-other-window
(current-buffer))
20429 (list 'org-timeline
(list 'quote include-all
))))
20431 ;; Remove past dates from the list of dates.
20432 (setq day-numbers
(delq nil
(mapcar (lambda(x)
20433 (if (>= x today
) x nil
))
20435 (org-prepare-agenda (concat "Timeline "
20436 (file-name-nondirectory buffer-file-name
)))
20437 (if doclosed
(push :closed args
))
20438 (push :timestamp args
)
20439 (push :deadline args
)
20440 (push :scheduled args
)
20442 (if dotodo
(push :todo args
))
20443 (while (setq d
(pop day-numbers
))
20444 (if (and (listp d
) (eq (car d
) :omitted
))
20447 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d
)))
20448 (put-text-property s
(1- (point)) 'face
'org-agenda-structure
))
20449 (if (listp d
) (setq d
(car d
) emptyp t
) (setq emptyp nil
))
20450 (if (and (>= d today
)
20455 (insert (make-string 79 ?-
) "\n")))
20456 (setq date
(calendar-gregorian-from-absolute d
))
20458 (setq rtn
(and (not emptyp
)
20459 (apply 'org-agenda-get-day-entries entry
20461 (if (or rtn
(equal d today
) org-timeline-show-empty-dates
)
20464 (if (stringp org-agenda-format-date
)
20465 (format-time-string org-agenda-format-date
20466 (org-time-from-absolute date
))
20467 (funcall org-agenda-format-date date
))
20469 (put-text-property s
(1- (point)) 'face
'org-agenda-structure
)
20470 (put-text-property s
(1- (point)) 'org-date-line t
)
20471 (if (equal d today
)
20472 (put-text-property s
(1- (point)) 'org-today t
))
20473 (and rtn
(insert (org-finalize-agenda-entries rtn
) "\n"))
20474 (put-text-property s
(1- (point)) 'day d
)))))
20475 (goto-char (point-min))
20476 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t
)
20478 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline
))
20479 (org-finalize-agenda)
20480 (setq buffer-read-only t
)))
20482 (defun org-get-all-dates (beg end
&optional no-ranges force-today inactive empty pre-re
)
20483 "Return a list of all relevant day numbers from BEG to END buffer positions.
20484 If NO-RANGES is non-nil, include only the start and end dates of a range,
20485 not every single day in the range. If FORCE-TODAY is non-nil, make
20486 sure that TODAY is included in the list. If INACTIVE is non-nil, also
20487 inactive time stamps (those in square brackets) are included.
20488 When EMPTY is non-nil, also include days without any entries."
20490 (if pre-re pre-re
"")
20491 (if inactive org-ts-regexp-both org-ts-regexp
)))
20492 dates dates1 date day day1 day2 ts1 ts2
)
20494 (setq dates
(list (time-to-days (current-time)))))
20497 (while (re-search-forward re end t
)
20498 (setq day
(time-to-days (org-time-string-to-time
20499 (substring (match-string 1) 0 10))))
20500 (or (memq day dates
) (push day dates
)))
20503 (while (re-search-forward org-tr-regexp end t
)
20504 (setq ts1
(substring (match-string 1) 0 10)
20505 ts2
(substring (match-string 2) 0 10)
20506 day1
(time-to-days (org-time-string-to-time ts1
))
20507 day2
(time-to-days (org-time-string-to-time ts2
)))
20508 (while (< (setq day1
(1+ day1
)) day2
)
20509 (or (memq day1 dates
) (push day1 dates
)))))
20510 (setq dates
(sort dates
'<))
20512 (while (setq day
(pop dates
))
20513 (setq day2
(car dates
))
20515 (when (and day2 empty
)
20516 (if (or (eq empty t
)
20517 (and (numberp empty
) (<= (- day2 day
) empty
)))
20518 (while (< (setq day
(1+ day
)) day2
)
20519 (push (list day
) dates1
))
20520 (push (cons :omitted
(- day2 day
)) dates1
))))
20521 (setq dates
(nreverse dates1
)))
20524 ;;; Agenda Daily/Weekly
20526 (defvar org-agenda-overriding-arguments nil
) ; dynamically scoped parameter
20527 (defvar org-agenda-start-day nil
) ; dynamically scoped parameter
20528 (defvar org-agenda-last-arguments nil
20529 "The arguments of the previous call to org-agenda")
20530 (defvar org-starting-day nil
) ; local variable in the agenda buffer
20531 (defvar org-agenda-span nil
) ; local variable in the agenda buffer
20532 (defvar org-include-all-loc nil
) ; local variable
20533 (defvar org-agenda-remove-date nil
) ; dynamically scoped
20536 (defun org-agenda-list (&optional include-all start-day ndays
)
20537 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
20538 The view will be for the current day or week, but from the overview buffer
20539 you will be able to go to other days/weeks.
20541 With one \\[universal-argument] prefix argument INCLUDE-ALL,
20542 all unfinished TODO items will also be shown, before the agenda.
20543 This feature is considered obsolete, please use the TODO list or a block
20546 With a numeric prefix argument in an interactive call, the agenda will
20547 span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
20548 the number of days. NDAYS defaults to `org-agenda-ndays'.
20550 START-DAY defaults to TODAY, or to the most recent match for the weekday
20551 given in `org-agenda-start-on-weekday'."
20553 (if (and (integerp include-all
) (> include-all
0))
20554 (setq ndays include-all include-all nil
))
20555 (setq ndays
(or ndays org-agenda-ndays
)
20556 start-day
(or start-day org-agenda-start-day
))
20557 (if org-agenda-overriding-arguments
20558 (setq include-all
(car org-agenda-overriding-arguments
)
20559 start-day
(nth 1 org-agenda-overriding-arguments
)
20560 ndays
(nth 2 org-agenda-overriding-arguments
)))
20561 (if (stringp start-day
)
20562 ;; Convert to an absolute day number
20563 (setq start-day
(time-to-days (org-read-date nil t start-day
))))
20564 (setq org-agenda-last-arguments
(list include-all start-day ndays
))
20565 (org-compile-prefix-format 'agenda
)
20566 (org-set-sorting-strategy 'agenda
)
20567 (require 'calendar
)
20568 (let* ((org-agenda-start-on-weekday
20569 (if (or (equal ndays
7) (and (null ndays
) (equal 7 org-agenda-ndays
)))
20570 org-agenda-start-on-weekday nil
))
20571 (thefiles (org-agenda-files))
20573 (today (time-to-days
20574 (time-subtract (current-time)
20575 (list 0 (* 3600 org-extend-today-until
) 0))))
20576 (sd (or start-day today
))
20577 (start (if (or (null org-agenda-start-on-weekday
)
20578 (< org-agenda-ndays
7))
20580 (let* ((nt (calendar-day-of-week
20581 (calendar-gregorian-from-absolute sd
)))
20582 (n1 org-agenda-start-on-weekday
)
20584 (- sd
(+ (if (< d
0) 7 0) d
)))))
20585 (day-numbers (list start
))
20587 (inhibit-redisplay (not debug-on-error
))
20588 s e rtn rtnall file date d start-pos end-pos todayp nd
)
20589 (setq org-agenda-redo-command
20590 (list 'org-agenda-list
(list 'quote include-all
) start-day ndays
))
20591 ;; Make the list of days
20592 (setq ndays
(or ndays org-agenda-ndays
)
20595 (push (1+ (car day-numbers
)) day-numbers
)
20596 (setq ndays
(1- ndays
)))
20597 (setq day-numbers
(nreverse day-numbers
))
20598 (org-prepare-agenda "Day/Week")
20599 (org-set-local 'org-starting-day
(car day-numbers
))
20600 (org-set-local 'org-include-all-loc include-all
)
20601 (org-set-local 'org-agenda-span
20602 (org-agenda-ndays-to-span nd
))
20603 (when (and (or include-all org-agenda-include-all-todo
)
20604 (member today day-numbers
))
20605 (setq files thefiles
20607 (while (setq file
(pop files
))
20609 (org-check-agenda-file file
)
20610 (setq date
(calendar-gregorian-from-absolute today
)
20611 rtn
(org-agenda-get-day-entries
20613 (setq rtnall
(append rtnall rtn
))))
20615 (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
20616 (add-text-properties (point-min) (1- (point))
20617 (list 'face
'org-agenda-structure
))
20618 (insert (org-finalize-agenda-entries rtnall
) "\n")))
20619 (unless org-agenda-compact-blocks
20621 (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd
)))
20623 (add-text-properties s
(1- (point)) (list 'face
'org-agenda-structure
20624 'org-date-line t
)))
20625 (while (setq d
(pop day-numbers
))
20626 (setq date
(calendar-gregorian-from-absolute d
)
20628 (if (or (setq todayp
(= d today
))
20629 (and (not start-pos
) (= d sd
)))
20630 (setq start-pos
(point))
20631 (if (and start-pos
(not end-pos
))
20632 (setq end-pos
(point))))
20633 (setq files thefiles
20635 (while (setq file
(pop files
))
20637 (org-check-agenda-file file
)
20638 (if org-agenda-show-log
20639 (setq rtn
(org-agenda-get-day-entries
20641 :deadline
:scheduled
:timestamp
:sexp
:closed
))
20642 (setq rtn
(org-agenda-get-day-entries
20644 :deadline
:scheduled
:sexp
:timestamp
)))
20645 (setq rtnall
(append rtnall rtn
))))
20646 (if org-agenda-include-diary
20648 (require 'diary-lib
)
20649 (setq rtn
(org-get-entries-from-diary date
))
20650 (setq rtnall
(append rtnall rtn
))))
20651 (if (or rtnall org-agenda-show-all-dates
)
20653 (setq day-cnt
(1+ day-cnt
))
20655 (if (stringp org-agenda-format-date
)
20656 (format-time-string org-agenda-format-date
20657 (org-time-from-absolute date
))
20658 (funcall org-agenda-format-date date
))
20660 (put-text-property s
(1- (point)) 'face
'org-agenda-structure
)
20661 (put-text-property s
(1- (point)) 'org-date-line t
)
20662 (put-text-property s
(1- (point)) 'org-day-cnt day-cnt
)
20663 (if todayp
(put-text-property s
(1- (point)) 'org-today t
))
20665 (org-finalize-agenda-entries
20666 (org-agenda-add-time-grid-maybe
20669 (put-text-property s
(1- (point)) 'day d
)
20670 (put-text-property s
(1- (point)) 'org-day-cnt day-cnt
))))
20671 (goto-char (point-min))
20672 (org-fit-agenda-window)
20673 (unless (and (pos-visible-in-window-p (point-min))
20674 (pos-visible-in-window-p (point-max)))
20675 (goto-char (1- (point-max)))
20677 (if (not (pos-visible-in-window-p (or start-pos
1)))
20679 (goto-char (or start-pos
1))
20681 (goto-char (or start-pos
1))
20682 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda
))
20683 (org-finalize-agenda)
20684 (setq buffer-read-only t
)
20687 (defun org-agenda-ndays-to-span (n)
20688 (cond ((< n
7) 'day
) ((= n
7) 'week
) ((< n
32) 'month
) (t 'year
)))
20690 ;;; Agenda TODO list
20692 (defvar org-select-this-todo-keyword nil
)
20693 (defvar org-last-arg nil
)
20696 (defun org-todo-list (arg)
20697 "Show all TODO entries from all agenda file in a single list.
20698 The prefix arg can be used to select a specific TODO keyword and limit
20699 the list to these. When using \\[universal-argument], you will be prompted
20700 for a keyword. A numeric prefix directly selects the Nth keyword in
20701 `org-todo-keywords-1'."
20703 (require 'calendar
)
20704 (org-compile-prefix-format 'todo
)
20705 (org-set-sorting-strategy 'todo
)
20706 (org-prepare-agenda "TODO")
20707 (let* ((today (time-to-days (current-time)))
20708 (date (calendar-gregorian-from-absolute today
))
20709 (kwds org-todo-keywords-for-agenda
)
20710 (completion-ignore-case t
)
20711 (org-select-this-todo-keyword
20712 (if (stringp arg
) arg
20713 (and arg
(integerp arg
) (> arg
0)
20714 (nth (1- arg
) kwds
))))
20715 rtn rtnall files file pos
)
20716 (when (equal arg
'(4))
20717 (setq org-select-this-todo-keyword
20718 (completing-read "Keyword (or KWD1|K2D2|...): "
20719 (mapcar 'list kwds
) nil nil
)))
20720 (and (equal 0 arg
) (setq org-select-this-todo-keyword nil
))
20721 (org-set-local 'org-last-arg arg
)
20722 (setq org-agenda-redo-command
20723 '(org-todo-list (or current-prefix-arg org-last-arg
)))
20724 (setq files
(org-agenda-files)
20726 (while (setq file
(pop files
))
20728 (org-check-agenda-file file
)
20729 (setq rtn
(org-agenda-get-day-entries file date
:todo
))
20730 (setq rtnall
(append rtnall rtn
))))
20731 (if org-agenda-overriding-header
20732 (insert (org-add-props (copy-sequence org-agenda-overriding-header
)
20733 nil
'face
'org-agenda-structure
) "\n")
20734 (insert "Global list of TODO items of type: ")
20735 (add-text-properties (point-min) (1- (point))
20736 (list 'face
'org-agenda-structure
))
20738 (insert (or org-select-this-todo-keyword
"ALL") "\n")
20739 (add-text-properties pos
(1- (point)) (list 'face
'org-warning
))
20741 (unless org-agenda-multi
20742 (insert "Available with `N r': (0)ALL")
20745 (setq s
(format "(%d)%s" (setq n
(1+ n
)) x
))
20746 (if (> (+ (current-column) (string-width s
) 1) (frame-width))
20751 (add-text-properties pos
(1- (point)) (list 'face
'org-agenda-structure
)))
20753 (insert (org-finalize-agenda-entries rtnall
) "\n"))
20754 (goto-char (point-min))
20755 (org-fit-agenda-window)
20756 (add-text-properties (point-min) (point-max) '(org-agenda-type todo
))
20757 (org-finalize-agenda)
20758 (setq buffer-read-only t
)))
20760 ;;; Agenda tags match
20763 (defun org-tags-view (&optional todo-only match
)
20764 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
20765 The prefix arg TODO-ONLY limits the search to TODO entries."
20767 (org-compile-prefix-format 'tags
)
20768 (org-set-sorting-strategy 'tags
)
20769 (let* ((org-tags-match-list-sublevels
20770 (if todo-only t org-tags-match-list-sublevels
))
20771 (completion-ignore-case t
)
20772 rtn rtnall files file pos matcher
20774 (setq matcher
(org-make-tags-matcher match
)
20775 match
(car matcher
) matcher
(cdr matcher
))
20776 (org-prepare-agenda (concat "TAGS " match
))
20777 (setq org-agenda-redo-command
20778 (list 'org-tags-view
(list 'quote todo-only
)
20779 (list 'if
'current-prefix-arg nil match
)))
20780 (setq files
(org-agenda-files)
20782 (while (setq file
(pop files
))
20784 (org-check-agenda-file file
)
20785 (setq buffer
(if (file-exists-p file
)
20786 (org-get-agenda-file-buffer file
)
20787 (error "No such file %s" file
)))
20789 ;; If file does not exist, merror message to agenda
20791 (format "ORG-AGENDA-ERROR: No such org-file %s" file
))
20792 rtnall
(append rtnall rtn
))
20793 (with-current-buffer buffer
20794 (unless (org-mode-p)
20795 (error "Agenda file %s is not in `org-mode'" file
))
20798 (if org-agenda-restrict
20799 (narrow-to-region org-agenda-restrict-begin
20800 org-agenda-restrict-end
)
20802 (setq rtn
(org-scan-tags 'agenda matcher todo-only
))
20803 (setq rtnall
(append rtnall rtn
))))))))
20804 (if org-agenda-overriding-header
20805 (insert (org-add-props (copy-sequence org-agenda-overriding-header
)
20806 nil
'face
'org-agenda-structure
) "\n")
20807 (insert "Headlines with TAGS match: ")
20808 (add-text-properties (point-min) (1- (point))
20809 (list 'face
'org-agenda-structure
))
20811 (insert match
"\n")
20812 (add-text-properties pos
(1- (point)) (list 'face
'org-warning
))
20814 (unless org-agenda-multi
20815 (insert "Press `C-u r' to search again with new search string\n"))
20816 (add-text-properties pos
(1- (point)) (list 'face
'org-agenda-structure
)))
20818 (insert (org-finalize-agenda-entries rtnall
) "\n"))
20819 (goto-char (point-min))
20820 (org-fit-agenda-window)
20821 (add-text-properties (point-min) (point-max) '(org-agenda-type tags
))
20822 (org-finalize-agenda)
20823 (setq buffer-read-only t
)))
20825 ;;; Agenda Finding stuck projects
20827 (defvar org-agenda-skip-regexp nil
20828 "Regular expression used in skipping subtrees for the agenda.
20829 This is basically a temporary global variable that can be set and then
20830 used by user-defined selections using `org-agenda-skip-function'.")
20832 (defvar org-agenda-overriding-header nil
20833 "When this is set during todo and tags searches, will replace header.")
20835 (defun org-agenda-skip-subtree-when-regexp-matches ()
20836 "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
20837 If yes, it returns the end position of this tree, causing agenda commands
20838 to skip this subtree. This is a function that can be put into
20839 `org-agenda-skip-function' for the duration of a command."
20840 (let ((end (save-excursion (org-end-of-subtree t
)))
20843 (setq skip
(re-search-forward org-agenda-skip-regexp end t
)))
20846 (defun org-agenda-skip-entry-if (&rest conditions
)
20847 "Skip entry if any of CONDITIONS is true.
20848 See `org-agenda-skip-if' for details."
20849 (org-agenda-skip-if nil conditions
))
20851 (defun org-agenda-skip-subtree-if (&rest conditions
)
20852 "Skip entry if any of CONDITIONS is true.
20853 See `org-agenda-skip-if' for details."
20854 (org-agenda-skip-if t conditions
))
20856 (defun org-agenda-skip-if (subtree conditions
)
20857 "Checks current entity for CONDITIONS.
20858 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
20859 the entry, i.e. the text before the next heading is checked.
20861 CONDITIONS is a list of symbols, boolean OR is used to combine the results
20862 from different tests. Valid conditions are:
20864 scheduled Check if there is a scheduled cookie
20865 notscheduled Check if there is no scheduled cookie
20866 deadline Check if there is a deadline
20867 notdeadline Check if there is no deadline
20868 regexp Check if regexp matches
20869 notregexp Check if regexp does not match.
20871 The regexp is taken from the conditions list, it must come right after
20872 the `regexp' or `notregexp' element.
20874 If any of these conditions is met, this function returns the end point of
20875 the entity, causing the search to continue from there. This is a function
20876 that can be put into `org-agenda-skip-function' for the duration of a command."
20878 (org-back-to-heading t
)
20881 (progn (org-end-of-subtree t
) (point))
20882 (progn (outline-next-heading) (1- (point)))))
20886 (and (memq 'scheduled conditions
)
20887 (re-search-forward org-scheduled-time-regexp end t
))
20888 (and (memq 'notscheduled conditions
)
20889 (not (re-search-forward org-scheduled-time-regexp end t
)))
20890 (and (memq 'deadline conditions
)
20891 (re-search-forward org-deadline-time-regexp end t
))
20892 (and (memq 'notdeadline conditions
)
20893 (not (re-search-forward org-deadline-time-regexp end t
)))
20894 (and (setq m
(memq 'regexp conditions
))
20895 (stringp (nth 1 m
))
20896 (re-search-forward (nth 1 m
) end t
))
20897 (and (setq m
(memq 'notregexp conditions
))
20898 (stringp (nth 1 m
))
20899 (not (re-search-forward (nth 1 m
) end t
))))
20903 (defun org-agenda-list-stuck-projects (&rest ignore
)
20904 "Create agenda view for projects that are stuck.
20905 Stuck projects are project that have no next actions. For the definitions
20906 of what a project is and how to check if it stuck, customize the variable
20907 `org-stuck-projects'.
20908 MATCH is being ignored."
20910 (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches
)
20911 ;; FIXME: we could have used org-agenda-skip-if here.
20912 (org-agenda-overriding-header "List of stuck projects: ")
20913 (matcher (nth 0 org-stuck-projects
))
20914 (todo (nth 1 org-stuck-projects
))
20915 (todo-wds (if (member "*" todo
)
20917 (org-prepare-agenda-buffers (org-agenda-files))
20919 org-done-keywords-for-agenda
20920 (copy-sequence org-todo-keywords-for-agenda
)))
20922 (todo-re (concat "^\\*+[ \t]+\\("
20923 (mapconcat 'identity todo-wds
"\\|")
20925 (tags (nth 2 org-stuck-projects
))
20926 (tags-re (if (member "*" tags
)
20927 (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
20928 (concat "^\\*+ .*:\\("
20929 (mapconcat 'identity tags
"\\|")
20930 (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
20931 (gen-re (nth 3 org-stuck-projects
))
20937 (and gen-re
(stringp gen-re
) (string-match "\\S-" gen-re
)
20939 (setq org-agenda-skip-regexp
20941 (mapconcat 'identity re-list
"\\|")
20942 (error "No information how to identify unstuck projects")))
20943 (org-tags-view nil matcher
)
20944 (with-current-buffer org-agenda-buffer-name
20945 (setq org-agenda-redo-command
20946 '(org-agenda-list-stuck-projects
20947 (or current-prefix-arg org-last-arg
))))))
20949 ;;; Diary integration
20951 (defvar org-disable-agenda-to-diary nil
) ;Dynamically-scoped param.
20953 (defun org-get-entries-from-diary (date)
20954 "Get the (Emacs Calendar) diary entries for DATE."
20955 (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
20956 (diary-display-hook '(fancy-diary-display))
20957 (pop-up-frames nil
)
20958 (list-diary-entries-hook
20959 (cons 'org-diary-default-entry list-diary-entries-hook
))
20960 (diary-file-name-prefix-function nil
) ; turn this feature off
20961 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string
)
20963 (org-disable-agenda-to-diary t
))
20965 (save-window-excursion
20966 (funcall (if (fboundp 'diary-list-entries
)
20967 'diary-list-entries
'list-diary-entries
)
20969 (if (not (get-buffer fancy-diary-buffer
))
20971 (with-current-buffer fancy-diary-buffer
20972 (setq buffer-read-only nil
)
20973 (if (zerop (buffer-size))
20976 ;; Omit the date and other unnecessary stuff
20977 (org-agenda-cleanup-fancy-diary)
20978 ;; Add prefix to each line and extend the text properties
20979 (if (zerop (buffer-size))
20981 (setq entries
(buffer-substring (point-min) (- (point-max) 1)))))
20982 (set-buffer-modified-p nil
)
20983 (kill-buffer fancy-diary-buffer
)))
20985 (setq entries
(org-split-string entries
"\n"))
20989 (setq x
(org-format-agenda-item "" x
"Diary" nil
'time
))
20990 ;; Extend the text properties to the beginning of the line
20991 (org-add-props x
(text-properties-at (1- (length x
)) x
)
20992 'type
"diary" 'date date
))
20995 (defun org-agenda-cleanup-fancy-diary ()
20996 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
20997 This gets rid of the date, the underline under the date, and
20998 the dummy entry installed by `org-mode' to ensure non-empty diary for each
20999 date. It also removes lines that contain only whitespace."
21000 (goto-char (point-min))
21001 (if (looking-at ".*?:[ \t]*")
21004 (re-search-forward "\n=+$" nil t
)
21006 (while (re-search-backward "^ +\n?" nil t
) (replace-match "")))
21007 (re-search-forward "\n=+$" nil t
)
21008 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
21009 (goto-char (point-min))
21010 (while (re-search-forward "^ +\n" nil t
)
21011 (replace-match ""))
21012 (goto-char (point-min))
21013 (if (re-search-forward "^Org-mode dummy\n?" nil t
)
21014 (replace-match "")))
21016 ;; Make sure entries from the diary have the right text properties.
21017 (eval-after-load "diary-lib"
21018 '(if (boundp 'diary-modify-entry-list-string-function
)
21019 ;; We can rely on the hook, nothing to do
21021 ;; Hook not avaiable, must use advice to make this work
21022 (defadvice add-to-diary-list
(before org-mark-diary-entry activate
)
21023 "Make the position visible."
21024 (if (and org-disable-agenda-to-diary
;; called from org-agenda
21027 (setq string
(org-modify-diary-entry-string string
))))))
21029 (defun org-modify-diary-entry-string (string)
21030 "Add text properties to string, allowing org-mode to act on it."
21031 (org-add-props string nil
21032 'mouse-face
'highlight
21033 'keymap org-agenda-keymap
21034 'help-echo
(if buffer-file-name
21035 (format "mouse-2 or RET jump to diary file %s"
21036 (abbreviate-file-name buffer-file-name
))
21038 'org-agenda-diary-link t
21039 'org-marker
(org-agenda-new-marker (point-at-bol))))
21041 (defun org-diary-default-entry ()
21042 "Add a dummy entry to the diary.
21043 Needed to avoid empty dates which mess up holiday display."
21044 ;; Catch the error if dealing with the new add-to-diary-alist
21045 (when org-disable-agenda-to-diary
21046 (condition-case nil
21047 (add-to-diary-list original-date
"Org-mode dummy" "")
21049 (add-to-diary-list original-date
"Org-mode dummy" "" nil
)))))
21052 (defun org-diary (&rest args
)
21053 "Return diary information from org-files.
21054 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
21055 It accesses org files and extracts information from those files to be
21056 listed in the diary. The function accepts arguments specifying what
21057 items should be listed. The following arguments are allowed:
21059 :timestamp List the headlines of items containing a date stamp or
21060 date range matching the selected date. Deadlines will
21061 also be listed, on the expiration day.
21063 :sexp List entries resulting from diary-like sexps.
21065 :deadline List any deadlines past due, or due within
21066 `org-deadline-warning-days'. The listing occurs only
21067 in the diary for *today*, not at any other date. If
21068 an entry is marked DONE, it is no longer listed.
21070 :scheduled List all items which are scheduled for the given date.
21071 The diary for *today* also contains items which were
21072 scheduled earlier and are not yet marked DONE.
21074 :todo List all TODO items from the org-file. This may be a
21075 long list - so this is not turned on by default.
21076 Like deadlines, these entries only show up in the
21077 diary for *today*, not at any other date.
21079 The call in the diary file should look like this:
21081 &%%(org-diary) ~/path/to/some/orgfile.org
21083 Use a separate line for each org file to check. Or, if you omit the file name,
21084 all files listed in `org-agenda-files' will be checked automatically:
21088 If you don't give any arguments (as in the example above), the default
21089 arguments (:deadline :scheduled :timestamp :sexp) are used.
21090 So the example above may also be written as
21092 &%%(org-diary :deadline :timestamp :sexp :scheduled)
21094 The function expects the lisp variables `entry' and `date' to be provided
21095 by the caller, because this is how the calendar works. Don't use this
21096 function from a program - use `org-agenda-get-day-entries' instead."
21097 (when (> (- (time-to-seconds (current-time))
21098 org-agenda-last-marker-time
)
21100 (org-agenda-reset-markers))
21101 (org-compile-prefix-format 'agenda
)
21102 (org-set-sorting-strategy 'agenda
)
21103 (setq args
(or args
'(:deadline
:scheduled
:timestamp
:sexp
)))
21104 (let* ((files (if (and entry
(stringp entry
) (string-match "\\S-" entry
))
21106 (org-agenda-files t
)))
21108 (org-prepare-agenda-buffers files
)
21109 ;; If this is called during org-agenda, don't return any entries to
21110 ;; the calendar. Org Agenda will list these entries itself.
21111 (if org-disable-agenda-to-diary
(setq files nil
))
21112 (while (setq file
(pop files
))
21113 (setq rtn
(apply 'org-agenda-get-day-entries file date args
))
21114 (setq results
(append results rtn
)))
21116 (concat (org-finalize-agenda-entries results
) "\n"))))
21118 ;;; Agenda entry finders
21120 (defun org-agenda-get-day-entries (file date
&rest args
)
21121 "Does the work for `org-diary' and `org-agenda'.
21122 FILE is the path to a file to be checked for entries. DATE is date like
21123 the one returned by `calendar-current-date'. ARGS are symbols indicating
21124 which kind of entries should be extracted. For details about these, see
21125 the documentation of `org-diary'."
21126 (setq args
(or args
'(:deadline
:scheduled
:timestamp
:sexp
)))
21127 (let* ((org-startup-folded nil
)
21128 (org-startup-align-all-tables nil
)
21129 (buffer (if (file-exists-p file
)
21130 (org-get-agenda-file-buffer file
)
21131 (error "No such file %s" file
)))
21134 ;; If file does not exist, make sure an error message ends up in diary
21135 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file
))
21136 (with-current-buffer buffer
21137 (unless (org-mode-p)
21138 (error "Agenda file %s is not in `org-mode'" file
))
21139 (let ((case-fold-search nil
))
21142 (if org-agenda-restrict
21143 (narrow-to-region org-agenda-restrict-begin
21144 org-agenda-restrict-end
)
21146 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
21147 (while (setq arg
(pop args
))
21149 ((and (eq arg
:todo
)
21150 (equal date
(calendar-current-date)))
21151 (setq rtn
(org-agenda-get-todos))
21152 (setq results
(append results rtn
)))
21153 ((eq arg
:timestamp
)
21154 (setq rtn
(org-agenda-get-blocks))
21155 (setq results
(append results rtn
))
21156 (setq rtn
(org-agenda-get-timestamps))
21157 (setq results
(append results rtn
)))
21159 (setq rtn
(org-agenda-get-sexps))
21160 (setq results
(append results rtn
)))
21161 ((eq arg
:scheduled
)
21162 (setq rtn
(org-agenda-get-scheduled))
21163 (setq results
(append results rtn
)))
21165 (setq rtn
(org-agenda-get-closed))
21166 (setq results
(append results rtn
)))
21167 ((eq arg
:deadline
)
21168 (setq rtn
(org-agenda-get-deadlines))
21169 (setq results
(append results rtn
))))))))
21172 (defun org-entry-is-todo-p ()
21173 (member (org-get-todo-state) org-not-done-keywords
))
21175 (defun org-entry-is-done-p ()
21176 (member (org-get-todo-state) org-done-keywords
))
21178 (defun org-get-todo-state ()
21180 (org-back-to-heading t
)
21181 (and (looking-at org-todo-line-regexp
)
21183 (match-string 2))))
21185 (defun org-at-date-range-p (&optional inactive-ok
)
21186 "Is the cursor inside a date range?"
21190 (let ((pos (point)))
21191 (skip-chars-backward "^[<\r\n")
21192 (skip-chars-backward "<[")
21193 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp
))
21194 (>= (match-end 0) pos
)
21196 (skip-chars-backward "^<[\r\n")
21197 (skip-chars-backward "<[")
21198 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp
))
21199 (>= (match-end 0) pos
)
21203 (defun org-agenda-get-todos ()
21204 "Return the TODO information for agenda display."
21205 (let* ((props (list 'face nil
21206 'done-face
'org-done
21207 'org-not-done-regexp org-not-done-regexp
21208 'org-todo-regexp org-todo-regexp
21209 'mouse-face
'highlight
21210 'keymap org-agenda-keymap
21212 (format "mouse-2 or RET jump to org file %s"
21213 (abbreviate-file-name buffer-file-name
))))
21214 ;; FIXME: get rid of the \n at some point but watch out
21215 (regexp (concat "^\\*+[ \t]+\\("
21216 (if org-select-this-todo-keyword
21217 (if (equal org-select-this-todo-keyword
"*")
21220 (mapconcat 'identity
(org-split-string org-select-this-todo-keyword
"|") "\\|")
21222 org-not-done-regexp
)
21224 marker priority category tags
21226 (goto-char (point-min))
21227 (while (re-search-forward regexp nil t
)
21230 (beginning-of-line)
21231 (setq beg
(point) end
(progn (outline-next-heading) (point)))
21232 (when (or (and org-agenda-todo-ignore-with-date
(goto-char beg
)
21233 (re-search-forward org-ts-regexp end t
))
21234 (and org-agenda-todo-ignore-scheduled
(goto-char beg
)
21235 (re-search-forward org-scheduled-time-regexp end t
))
21236 (and org-agenda-todo-ignore-deadlines
(goto-char beg
)
21237 (re-search-forward org-deadline-time-regexp end t
)
21238 (org-deadline-close (match-string 1))))
21239 (goto-char (1+ beg
))
21240 (or org-agenda-todo-list-sublevels
(org-end-of-subtree 'invisible
))
21241 (throw :skip nil
)))
21244 (goto-char (match-beginning 1))
21245 (setq marker
(org-agenda-new-marker (match-beginning 0))
21246 category
(org-get-category)
21247 tags
(org-get-tags-at (point))
21248 txt
(org-format-agenda-item "" (match-string 1) category tags
)
21249 priority
(1+ (org-get-priority txt
)))
21250 (org-add-props txt props
21251 'org-marker marker
'org-hd-marker marker
21252 'priority priority
'org-category category
21255 (if org-agenda-todo-list-sublevels
21256 (goto-char (match-end 1))
21257 (org-end-of-subtree 'invisible
))))
21260 (defconst org-agenda-no-heading-message
21261 "No heading for this item in buffer or region.")
21263 (defun org-agenda-get-timestamps ()
21264 "Return the date stamp information for agenda display."
21265 (let* ((props (list 'face nil
21266 'org-not-done-regexp org-not-done-regexp
21267 'org-todo-regexp org-todo-regexp
21268 'mouse-face
'highlight
21269 'keymap org-agenda-keymap
21271 (format "mouse-2 or RET jump to org file %s"
21272 (abbreviate-file-name buffer-file-name
))))
21273 (d1 (calendar-absolute-from-gregorian date
))
21277 (format-time-string
21279 (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
21285 (format-time-string
21286 (car org-time-stamp-formats
)
21287 (apply 'encode-time
; DATE bound by calendar
21288 (list 0 0 0 (nth 1 date
) (car date
) (nth 2 date
))))
21290 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
21291 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
21292 marker hdmarker deadlinep scheduledp donep tmp priority category
21293 ee txt timestr tags b0 b3 e3 head
)
21294 (goto-char (point-min))
21295 (while (re-search-forward regexp nil t
)
21296 (setq b0
(match-beginning 0)
21297 b3
(match-beginning 3) e3
(match-end 3))
21299 (and (org-at-date-range-p) (throw :skip nil
))
21301 (if (and (match-end 1)
21302 (not (= d1
(org-time-string-to-absolute (match-string 1) d1
))))
21305 (not (org-diary-sexp-entry (buffer-substring b3 e3
) "" date
)))
21307 (setq marker
(org-agenda-new-marker b0
)
21308 category
(org-get-category b0
)
21309 tmp
(buffer-substring (max (point-min)
21310 (- b0 org-ds-keyword-length
))
21312 timestr
(if b3
"" (buffer-substring b0
(point-at-eol)))
21313 deadlinep
(string-match org-deadline-regexp tmp
)
21314 scheduledp
(string-match org-scheduled-regexp tmp
)
21315 donep
(org-entry-is-done-p))
21316 (if (or scheduledp deadlinep
) (throw :skip t
))
21317 (if (string-match ">" timestr
)
21318 ;; substring should only run to end of time stamp
21319 (setq timestr
(substring timestr
0 (match-end 0))))
21321 (if (re-search-backward "^\\*+ " nil t
)
21323 (goto-char (match-beginning 0))
21324 (setq hdmarker
(org-agenda-new-marker)
21325 tags
(org-get-tags-at))
21326 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
21327 (setq head
(match-string 1))
21328 (and org-agenda-skip-timestamp-if-done donep
(throw :skip t
))
21329 (setq txt
(org-format-agenda-item
21330 nil head category tags timestr nil
21332 (setq txt org-agenda-no-heading-message
))
21333 (setq priority
(org-get-priority txt
))
21334 (org-add-props txt props
21335 'org-marker marker
'org-hd-marker hdmarker
)
21336 (org-add-props txt nil
'priority priority
21337 'org-category category
'date date
21340 (outline-next-heading)))
21343 (defun org-agenda-get-sexps ()
21344 "Return the sexp information for agenda display."
21345 (require 'diary-lib
)
21346 (let* ((props (list 'face nil
21347 'mouse-face
'highlight
21348 'keymap org-agenda-keymap
21350 (format "mouse-2 or RET jump to org file %s"
21351 (abbreviate-file-name buffer-file-name
))))
21353 marker category ee txt tags entry result beg b sexp sexp-entry
)
21354 (goto-char (point-min))
21355 (while (re-search-forward regexp nil t
)
21358 (setq beg
(match-beginning 0))
21359 (goto-char (1- (match-end 0)))
21362 (setq sexp
(buffer-substring b
(point)))
21363 (setq sexp-entry
(if (looking-at "[ \t]*\\(\\S-.*\\)")
21364 (org-trim (match-string 1))
21366 (setq result
(org-diary-sexp-entry sexp sexp-entry date
))
21368 (setq marker
(org-agenda-new-marker beg
)
21369 category
(org-get-category beg
))
21371 (if (string-match "\\S-" result
)
21373 (setq txt
"SEXP entry returned empty string"))
21375 (setq txt
(org-format-agenda-item
21376 "" txt category tags
'time
))
21377 (org-add-props txt props
'org-marker marker
)
21378 (org-add-props txt nil
21379 'org-category category
'date date
21384 (defun org-agenda-get-closed ()
21385 "Return the logged TODO entries for agenda display."
21386 (let* ((props (list 'mouse-face
'highlight
21387 'org-not-done-regexp org-not-done-regexp
21388 'org-todo-regexp org-todo-regexp
21389 'keymap org-agenda-keymap
21391 (format "mouse-2 or RET jump to org file %s"
21392 (abbreviate-file-name buffer-file-name
))))
21394 "\\<\\(" org-closed-string
"\\|" org-clock-string
"\\) *\\["
21397 (format-time-string
21398 (car org-time-stamp-formats
)
21399 (apply 'encode-time
; DATE bound by calendar
21400 (list 0 0 0 (nth 1 date
) (car date
) (nth 2 date
))))
21402 marker hdmarker priority category tags closedp
21404 (goto-char (point-min))
21405 (while (re-search-forward regexp nil t
)
21408 (setq marker
(org-agenda-new-marker (match-beginning 0))
21409 closedp
(equal (match-string 1) org-closed-string
)
21410 category
(org-get-category (match-beginning 0))
21411 timestr
(buffer-substring (match-beginning 0) (point-at-eol))
21412 ;; donep (org-entry-is-done-p)
21414 (if (string-match "\\]" timestr
)
21415 ;; substring should only run to end of time stamp
21416 (setq timestr
(substring timestr
0 (match-end 0))))
21418 (if (re-search-backward "^\\*+ " nil t
)
21420 (goto-char (match-beginning 0))
21421 (setq hdmarker
(org-agenda-new-marker)
21422 tags
(org-get-tags-at))
21423 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
21424 (setq txt
(org-format-agenda-item
21425 (if closedp
"Closed: " "Clocked: ")
21426 (match-string 1) category tags timestr
)))
21427 (setq txt org-agenda-no-heading-message
))
21428 (setq priority
100000)
21429 (org-add-props txt props
21430 'org-marker marker
'org-hd-marker hdmarker
'face
'org-done
21431 'priority priority
'org-category category
21432 'type
"closed" 'date date
21433 'undone-face
'org-warning
'done-face
'org-done
)
21435 (goto-char (point-at-eol))))
21438 (defun org-agenda-get-deadlines ()
21439 "Return the deadline information for agenda display."
21440 (let* ((props (list 'mouse-face
'highlight
21441 'org-not-done-regexp org-not-done-regexp
21442 'org-todo-regexp org-todo-regexp
21443 'keymap org-agenda-keymap
21445 (format "mouse-2 or RET jump to org file %s"
21446 (abbreviate-file-name buffer-file-name
))))
21447 (regexp org-deadline-time-regexp
)
21448 (todayp (equal date
(calendar-current-date))) ; DATE bound by calendar
21449 (d1 (calendar-absolute-from-gregorian date
)) ; DATE bound by calendar
21450 d2 diff dfrac wdays pos pos1 category tags
21451 ee txt head face s upcomingp donep timestr
)
21452 (goto-char (point-min))
21453 (while (re-search-forward regexp nil t
)
21456 (setq s
(match-string 1)
21457 pos
(1- (match-beginning 1))
21458 d2
(org-time-string-to-absolute (match-string 1) d1
'past
)
21460 wdays
(org-get-wdays s
)
21461 dfrac
(/ (* 1.0 (- wdays diff
)) (max wdays
1))
21462 upcomingp
(and todayp
(> diff
0)))
21463 ;; When to show a deadline in the calendar:
21464 ;; If the expiration is within wdays warning time.
21465 ;; Past-due deadlines are only shown on the current date
21466 (if (or (and (<= diff wdays
)
21467 (and todayp
(not org-agenda-only-exact-dates
)))
21470 (setq category
(org-get-category))
21471 (if (re-search-backward "^\\*+[ \t]+" nil t
)
21473 (goto-char (match-end 0))
21474 (setq pos1
(match-beginning 0))
21475 (setq tags
(org-get-tags-at pos1
))
21476 (setq head
(buffer-substring-no-properties
21478 (progn (skip-chars-forward "^\r\n")
21480 (setq donep
(string-match org-looking-at-done-regexp head
))
21481 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s
)
21483 (concat (substring s
(match-beginning 1)) " "))
21484 (setq timestr
'time
))
21486 (or org-agenda-skip-deadline-if-done
21489 (setq txt
(org-format-agenda-item
21491 (car org-agenda-deadline-leaders
)
21492 (format (nth 1 org-agenda-deadline-leaders
)
21494 head category tags timestr
))))
21495 (setq txt org-agenda-no-heading-message
))
21497 (setq face
(org-agenda-deadline-face dfrac
))
21498 (org-add-props txt props
21499 'org-marker
(org-agenda-new-marker pos
)
21500 'org-hd-marker
(org-agenda-new-marker pos1
)
21501 'priority
(+ (- diff
)
21502 (org-get-priority txt
))
21503 'org-category category
21504 'type
(if upcomingp
"upcoming-deadline" "deadline")
21505 'date
(if upcomingp date d2
)
21506 'face
(if donep
'org-done face
)
21507 'undone-face face
'done-face
'org-done
)
21511 (defun org-agenda-deadline-face (fraction)
21512 "Return the face to displaying a deadline item.
21513 FRACTION is what fraction of the head-warning time has passed."
21514 (let ((faces org-agenda-deadline-faces
) f
)
21516 (while (setq f
(pop faces
))
21517 (if (>= fraction
(car f
)) (throw 'exit
(cdr f
)))))))
21519 (defun org-agenda-get-scheduled ()
21520 "Return the scheduled information for agenda display."
21521 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
21522 'org-todo-regexp org-todo-regexp
21523 'done-face
'org-done
21524 'mouse-face
'highlight
21525 'keymap org-agenda-keymap
21527 (format "mouse-2 or RET jump to org file %s"
21528 (abbreviate-file-name buffer-file-name
))))
21529 (regexp org-scheduled-time-regexp
)
21530 (todayp (equal date
(calendar-current-date))) ; DATE bound by calendar
21531 (d1 (calendar-absolute-from-gregorian date
)) ; DATE bound by calendar
21532 d2 diff pos pos1 category tags
21533 ee txt head pastschedp donep face timestr s
)
21534 (goto-char (point-min))
21535 (while (re-search-forward regexp nil t
)
21538 (setq s
(match-string 1)
21539 pos
(1- (match-beginning 1))
21540 d2
(org-time-string-to-absolute (match-string 1) d1
'past
)
21542 ;;; do we need to do this for deadleine too????
21543 ;;; d2 (org-time-string-to-absolute (match-string 1) (if todayp nil d1))
21545 (setq pastschedp
(and todayp
(< diff
0)))
21546 ;; When to show a scheduled item in the calendar:
21547 ;; If it is on or past the date.
21548 (if (or (and (< diff
0)
21549 (and todayp
(not org-agenda-only-exact-dates
)))
21552 (setq category
(org-get-category))
21553 (if (re-search-backward "^\\*+[ \t]+" nil t
)
21555 (goto-char (match-end 0))
21556 (setq pos1
(match-beginning 0))
21557 (setq tags
(org-get-tags-at))
21558 (setq head
(buffer-substring-no-properties
21560 (progn (skip-chars-forward "^\r\n") (point))))
21561 (setq donep
(string-match org-looking-at-done-regexp head
))
21562 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s
)
21564 (concat (substring s
(match-beginning 1)) " "))
21565 (setq timestr
'time
))
21567 (or org-agenda-skip-scheduled-if-done
21570 (setq txt
(org-format-agenda-item
21572 (car org-agenda-scheduled-leaders
)
21573 (format (nth 1 org-agenda-scheduled-leaders
)
21575 head category tags timestr
))))
21576 (setq txt org-agenda-no-heading-message
))
21578 (setq face
(if pastschedp
21579 'org-scheduled-previously
21580 'org-scheduled-today
))
21581 (org-add-props txt props
21583 'face
(if donep
'org-done face
)
21584 'org-marker
(org-agenda-new-marker pos
)
21585 'org-hd-marker
(org-agenda-new-marker pos1
)
21586 'type
(if pastschedp
"past-scheduled" "scheduled")
21587 'date
(if pastschedp d2 date
)
21588 'priority
(+ 94 (- 5 diff
) (org-get-priority txt
))
21589 'org-category category
)
21593 (defun org-agenda-get-blocks ()
21594 "Return the date-range information for agenda display."
21595 (let* ((props (list 'face nil
21596 'org-not-done-regexp org-not-done-regexp
21597 'org-todo-regexp org-todo-regexp
21598 'mouse-face
'highlight
21599 'keymap org-agenda-keymap
21601 (format "mouse-2 or RET jump to org file %s"
21602 (abbreviate-file-name buffer-file-name
))))
21603 (regexp org-tr-regexp
)
21604 (d0 (calendar-absolute-from-gregorian date
))
21605 marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos
21607 (goto-char (point-min))
21608 (while (re-search-forward regexp nil t
)
21612 (setq timestr
(match-string 0)
21613 s1
(match-string 1)
21614 s2
(match-string 2)
21615 d1
(time-to-days (org-time-string-to-time s1
))
21616 d2
(time-to-days (org-time-string-to-time s2
)))
21617 (if (and (> (- d0 d1
) -
1) (> (- d2 d0
) -
1))
21618 ;; Only allow days between the limits, because the normal
21619 ;; date stamps will catch the limits.
21621 (setq marker
(org-agenda-new-marker (point)))
21622 (setq category
(org-get-category))
21623 (if (re-search-backward "^\\*+ " nil t
)
21625 (goto-char (match-beginning 0))
21626 (setq hdmarker
(org-agenda-new-marker (point)))
21627 (setq tags
(org-get-tags-at))
21628 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
21629 (setq head
(match-string 1))
21630 (and org-agenda-skip-timestamp-if-done
21631 (org-entry-is-done-p)
21633 (setq txt
(org-format-agenda-item
21634 (format (if (= d1 d2
) "" "(%d/%d): ")
21635 (1+ (- d0 d1
)) (1+ (- d2 d1
)))
21637 (if (= d0 d1
) timestr
))))
21638 (setq txt org-agenda-no-heading-message
))
21639 (org-add-props txt props
21640 'org-marker marker
'org-hd-marker hdmarker
21641 'type
"block" 'date date
21642 'priority
(org-get-priority txt
) 'org-category category
)
21645 ;; Sort the entries by expiration date.
21648 ;;; Agenda presentation and sorting
21650 (defconst org-plain-time-of-day-regexp
21652 "\\(\\<[012]?[0-9]"
21653 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
21655 "\\(\\<[012]?[0-9]"
21656 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
21658 "Regular expression to match a plain time or time range.
21659 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
21660 groups carry important information:
21662 1 the first time, range or not
21663 8 the second time, if it is a range.")
21665 (defconst org-plain-time-extension-regexp
21667 "\\(\\<[012]?[0-9]"
21668 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
21669 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
21670 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
21671 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
21672 groups carry important information:
21674 7 hours of duration
21675 9 minutes of duration")
21677 (defconst org-stamp-time-of-day-regexp
21679 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
21680 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
21682 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
21683 "Regular expression to match a timestamp time or time range.
21684 After a match, the following groups carry important information:
21686 1 date plus weekday, for backreferencing to make sure both times on same day
21687 2 the first time, range or not
21688 4 the second time, if it is a range.")
21690 (defvar org-prefix-has-time nil
21691 "A flag, set by `org-compile-prefix-format'.
21692 The flag is set if the currently compiled format contains a `%t'.")
21693 (defvar org-prefix-has-tag nil
21694 "A flag, set by `org-compile-prefix-format'.
21695 The flag is set if the currently compiled format contains a `%T'.")
21697 (defun org-format-agenda-item (extra txt
&optional category tags dotime
21698 noprefix remove-re
)
21699 "Format TXT to be inserted into the agenda buffer.
21700 In particular, it adds the prefix and corresponding text properties. EXTRA
21701 must be a string and replaces the `%s' specifier in the prefix format.
21702 CATEGORY (string, symbol or nil) may be used to overrule the default
21703 category taken from local variable or file name. It will replace the `%c'
21704 specifier in the format. DOTIME, when non-nil, indicates that a
21705 time-of-day should be extracted from TXT for sorting of this entry, and for
21706 the `%t' specifier in the format. When DOTIME is a string, this string is
21707 searched for a time before TXT is. NOPREFIX is a flag and indicates that
21708 only the correctly processes TXT should be returned - this is used by
21709 `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
21710 Any match of REMOVE-RE will be removed from TXT."
21712 ;; Diary entries sometimes have extra whitespace at the beginning
21713 (if (string-match "^ +" txt
) (setq txt
(replace-match "" nil nil txt
)))
21714 (let* ((category (or category
21716 (if buffer-file-name
21717 (file-name-sans-extension
21718 (file-name-nondirectory buffer-file-name
))
21720 (tag (if tags
(nth (1- (length tags
)) tags
) ""))
21721 time
; time and tag are needed for the eval of the prefix format
21722 (ts (if dotime
(concat (if (stringp dotime
) dotime
"") txt
)))
21723 (time-of-day (and dotime
(org-get-time-of-day ts
)))
21724 stamp plain s0 s1 s2 rtn srp
)
21725 (when (and dotime time-of-day org-prefix-has-time
)
21726 ;; Extract starting and ending time and move them to prefix
21727 (when (or (setq stamp
(string-match org-stamp-time-of-day-regexp ts
))
21728 (setq plain
(string-match org-plain-time-of-day-regexp ts
)))
21729 (setq s0
(match-string 0 ts
)
21730 srp
(and stamp
(match-end 3))
21731 s1
(match-string (if plain
1 2) ts
)
21732 s2
(match-string (if plain
8 (if srp
4 6)) ts
))
21734 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
21735 ;; them, we might want to remove them there to avoid duplication.
21736 ;; The user can turn this off with a variable.
21737 (if (and org-agenda-remove-times-when-in-prefix
(or stamp plain
)
21738 (string-match (concat (regexp-quote s0
) " *") txt
)
21739 (not (equal ?\
] (string-to-char (substring txt
(match-end 0)))))
21740 (if (eq org-agenda-remove-times-when-in-prefix
'beg
)
21741 (= (match-beginning 0) 0)
21743 (setq txt
(replace-match "" nil nil txt
))))
21744 ;; Normalize the time(s) to 24 hour
21745 (if s1
(setq s1
(org-get-time-of-day s1
'string t
)))
21746 (if s2
(setq s2
(org-get-time-of-day s2
'string t
))))
21748 (when (and s1
(not s2
) org-agenda-default-appointment-duration
21749 (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1
))
21750 (let ((m (+ (string-to-number (match-string 2 s1
))
21751 (* 60 (string-to-number (match-string 1 s1
)))
21752 org-agenda-default-appointment-duration
))
21754 (setq h
(/ m
60) m
(- m
(* h
60)))
21755 (setq s2
(format "%02d:%02d" h m
))))
21757 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
21759 ;; Tags are in the string
21760 (if (or (eq org-agenda-remove-tags t
)
21761 (and org-agenda-remove-tags
21762 org-prefix-has-tag
))
21763 (setq txt
(replace-match "" t t txt
))
21764 (setq txt
(replace-match
21765 (concat (make-string (max (- 50 (length txt
)) 1) ?\
)
21766 (match-string 2 txt
))
21770 (while (string-match remove-re txt
)
21771 (setq txt
(replace-match "" t t txt
))))
21773 ;; Create the final string
21776 ;; Prepare the variables needed in the eval of the compiled format
21777 (setq time
(cond (s2 (concat s1
"-" s2
))
21778 (s1 (concat s1
"......"))
21780 extra
(or extra
"")
21781 category
(if (symbolp category
) (symbol-name category
) category
))
21782 ;; Evaluate the compiled format
21783 (setq rtn
(concat (eval org-prefix-format-compiled
) txt
)))
21785 ;; And finally add the text properties
21786 (org-add-props rtn nil
21787 'org-category
(downcase category
) 'tags tags
21788 'org-highest-priority org-highest-priority
21789 'org-lowest-priority org-lowest-priority
21790 'prefix-length
(- (length rtn
) (length txt
))
21791 'time-of-day time-of-day
21797 (defvar org-agenda-sorting-strategy
) ;; because the def is in a let form
21798 (defvar org-agenda-sorting-strategy-selected nil
)
21800 (defun org-agenda-add-time-grid-maybe (list ndays todayp
)
21802 (cond ((not org-agenda-use-time-grid
) (throw 'exit list
))
21803 ((and todayp
(member 'today
(car org-agenda-time-grid
))))
21804 ((and (= ndays
1) (member 'daily
(car org-agenda-time-grid
))))
21805 ((member 'weekly
(car org-agenda-time-grid
)))
21806 (t (throw 'exit list
)))
21807 (let* ((have (delq nil
(mapcar
21808 (lambda (x) (get-text-property 1 'time-of-day x
))
21810 (string (nth 1 org-agenda-time-grid
))
21811 (gridtimes (nth 2 org-agenda-time-grid
))
21812 (req (car org-agenda-time-grid
))
21813 (remove (member 'remove-match req
))
21815 (if (and (member 'require-timed req
) (not have
))
21816 ;; don't show empty grid
21817 (throw 'exit list
))
21818 (while (setq time
(pop gridtimes
))
21819 (unless (and remove
(member time have
))
21820 (setq time
(int-to-string time
))
21821 (push (org-format-agenda-item
21823 (concat (substring time
0 -
2) ":" (substring time -
2)))
21826 1 (length (car new
)) 'face
'org-time-grid
(car new
))))
21827 (if (member 'time-up org-agenda-sorting-strategy-selected
)
21829 (append list new
)))))
21831 (defun org-compile-prefix-format (key)
21832 "Compile the prefix format into a Lisp form that can be evaluated.
21833 The resulting form is returned and stored in the variable
21834 `org-prefix-format-compiled'."
21835 (setq org-prefix-has-time nil org-prefix-has-tag nil
)
21837 ((stringp org-agenda-prefix-format
)
21838 org-agenda-prefix-format
)
21839 ((assq key org-agenda-prefix-format
)
21840 (cdr (assq key org-agenda-prefix-format
)))
21841 (t " %-12:c%?-12t% s")))
21843 varform vars var e c f opt
)
21844 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
21846 (setq var
(cdr (assoc (match-string 4 s
)
21847 '(("c" . category
) ("t" . time
) ("s" . extra
)
21849 c
(or (match-string 3 s
) "")
21850 opt
(match-beginning 1)
21851 start
(1+ (match-beginning 0)))
21852 (if (equal var
'time
) (setq org-prefix-has-time t
))
21853 (if (equal var
'tag
) (setq org-prefix-has-tag t
))
21854 (setq f
(concat "%" (match-string 2 s
) "s"))
21857 `(if (equal "" ,var
)
21859 (format ,f
(if (equal "" ,var
) "" (concat ,var
,c
)))))
21860 (setq varform
`(format ,f
(if (equal ,var
"") "" (concat ,var
,c
)))))
21861 (setq s
(replace-match "%s" t nil s
))
21862 (push varform vars
))
21863 (setq vars
(nreverse vars
))
21864 (setq org-prefix-format-compiled
`(format ,s
,@vars
))))
21866 (defun org-set-sorting-strategy (key)
21867 (if (symbolp (car org-agenda-sorting-strategy
))
21869 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy
)
21870 (setq org-agenda-sorting-strategy-selected
21871 (or (cdr (assq key org-agenda-sorting-strategy
))
21872 (cdr (assq 'agenda org-agenda-sorting-strategy
))
21873 '(time-up category-keep priority-down
)))))
21875 (defun org-get-time-of-day (s &optional string mod24
)
21876 "Check string S for a time of day.
21877 If found, return it as a military time number between 0 and 2400.
21878 If not found, return nil.
21879 The optional STRING argument forces conversion into a 5 character wide string
21883 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s
)
21884 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s
))
21885 (let* ((h (string-to-number (match-string 1 s
)))
21886 (m (if (match-end 3) (string-to-number (match-string 3 s
)) 0))
21887 (ampm (if (match-end 4) (downcase (match-string 4 s
))))
21888 (am-p (equal ampm
"am"))
21889 (h1 (cond ((not ampm
) h
)
21890 ((= h
12) (if am-p
0 12))
21891 (t (+ h
(if am-p
0 12)))))
21892 (h2 (if (and string mod24
(not (and (= m
0) (= h1
24))))
21894 (t0 (+ (* 100 h2
) m
))
21895 (t1 (concat (if (>= h1
24) "+" " ")
21896 (if (< t0
100) "0" "")
21897 (if (< t0
10) "0" "")
21898 (int-to-string t0
))))
21899 (if string
(concat (substring t1 -
4 -
2) ":" (substring t1 -
2)) t0
)))))
21901 (defun org-finalize-agenda-entries (list &optional nosort
)
21902 "Sort and concatenate the agenda items."
21903 (setq list
(mapcar 'org-agenda-highlight-todo list
))
21906 (mapconcat 'identity
(sort list
'org-entries-lessp
) "\n")))
21908 (defun org-agenda-highlight-todo (x)
21912 (beginning-of-line 1)
21913 (setq re
(get-text-property (point) 'org-todo-regexp
))
21914 (goto-char (+ (point) (or (get-text-property (point) 'prefix-length
) 0)))
21915 (when (looking-at (concat "[ \t]*\\.*" re
" +"))
21916 (add-text-properties (match-beginning 0) (match-end 0)
21917 (list 'face
(org-get-todo-face 0)))
21918 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
21919 (delete-region (match-beginning 1) (1- (match-end 0)))
21920 (goto-char (match-beginning 1))
21921 (insert (format org-agenda-todo-keyword-format s
)))))
21922 (setq re
(concat (get-text-property 0 'org-todo-regexp x
))
21923 pl
(get-text-property 0 'prefix-length x
))
21924 ; (and re (equal (string-match (concat "\\(\\.*\\)" re) x (or pl 0)) pl)
21925 ; (add-text-properties
21926 ; (or (match-end 1) (match-end 0)) (match-end 0)
21927 ; (list 'face (org-get-todo-face (match-string 2 x)))
21930 (equal (string-match (concat "\\(\\.*\\)" re
"\\( +\\)")
21932 (add-text-properties
21933 (or (match-end 1) (match-end 0)) (match-end 0)
21934 (list 'face
(org-get-todo-face (match-string 2 x
)))
21936 (setq x
(concat (substring x
0 (match-end 1))
21937 (format org-agenda-todo-keyword-format
21938 (match-string 2 x
))
21940 (substring x
(match-end 3)))))
21943 (defsubst org-cmp-priority
(a b
)
21944 "Compare the priorities of string A and B."
21945 (let ((pa (or (get-text-property 1 'priority a
) 0))
21946 (pb (or (get-text-property 1 'priority b
) 0)))
21947 (cond ((> pa pb
) +1)
21951 (defsubst org-cmp-category
(a b
)
21952 "Compare the string values of categories of strings A and B."
21953 (let ((ca (or (get-text-property 1 'org-category a
) ""))
21954 (cb (or (get-text-property 1 'org-category b
) "")))
21955 (cond ((string-lessp ca cb
) -
1)
21956 ((string-lessp cb ca
) +1)
21959 (defsubst org-cmp-tag
(a b
)
21960 "Compare the string values of categories of strings A and B."
21961 (let ((ta (car (last (get-text-property 1 'tags a
))))
21962 (tb (car (last (get-text-property 1 'tags b
)))))
21963 (cond ((not ta
) +1)
21965 ((string-lessp ta tb
) -
1)
21966 ((string-lessp tb ta
) +1)
21969 (defsubst org-cmp-time
(a b
)
21970 "Compare the time-of-day values of strings A and B."
21971 (let* ((def (if org-sort-agenda-notime-is-late
9901 -
1))
21972 (ta (or (get-text-property 1 'time-of-day a
) def
))
21973 (tb (or (get-text-property 1 'time-of-day b
) def
)))
21974 (cond ((< ta tb
) -
1)
21978 (defun org-entries-lessp (a b
)
21979 "Predicate for sorting agenda entries."
21980 ;; The following variables will be used when the form is evaluated.
21981 ;; So even though the compiler complains, keep them.
21982 (let* ((time-up (org-cmp-time a b
))
21983 (time-down (if time-up
(- time-up
) nil
))
21984 (priority-up (org-cmp-priority a b
))
21985 (priority-down (if priority-up
(- priority-up
) nil
))
21986 (category-up (org-cmp-category a b
))
21987 (category-down (if category-up
(- category-up
) nil
))
21988 (category-keep (if category-up
+1 nil
))
21989 (tag-up (org-cmp-tag a b
))
21990 (tag-down (if tag-up
(- tag-up
) nil
)))
21992 (eval (cons 'or org-agenda-sorting-strategy-selected
))
21993 '((-1 . t
) (1 . nil
) (nil . nil
))))))
21995 ;;; Agenda restriction lock
21997 (defvar org-agenda-restriction-lock-overlay
(org-make-overlay 1 1)
21998 "Overlay to mark the headline to which arenda commands are restricted.")
21999 (org-overlay-put org-agenda-restriction-lock-overlay
22000 'face
'org-agenda-restriction-lock
)
22001 (org-overlay-put org-agenda-restriction-lock-overlay
22002 'help-echo
"Agendas are currently limited to this subtree.")
22003 (org-detach-overlay org-agenda-restriction-lock-overlay
)
22004 (defvar org-speedbar-restriction-lock-overlay
(org-make-overlay 1 1)
22005 "Overlay marking the agenda restriction line in speedbar.")
22006 (org-overlay-put org-speedbar-restriction-lock-overlay
22007 'face
'org-agenda-restriction-lock
)
22008 (org-overlay-put org-speedbar-restriction-lock-overlay
22009 'help-echo
"Agendas are currently limited to this item.")
22010 (org-detach-overlay org-speedbar-restriction-lock-overlay
)
22012 (defun org-agenda-set-restriction-lock (&optional type
)
22013 "Set restriction lock for agenda, to current subtree or file.
22014 Restriction will be the file if TYPE is `file', or if type is the
22015 universal prefix '(4), or if the cursor is before the first headline
22016 in the file. Otherwise, restriction will be to the current subtree."
22018 (and (equal type
'(4)) (setq type
'file
))
22021 ((org-at-heading-p) 'subtree
)
22022 ((condition-case nil
(org-back-to-heading t
) (error nil
))
22025 (if (eq type
'subtree
)
22027 (setq org-agenda-restrict t
)
22028 (setq org-agenda-overriding-restriction
'subtree
)
22029 (put 'org-agenda-files
'org-restrict
22030 (list (buffer-file-name (buffer-base-buffer))))
22031 (org-back-to-heading t
)
22032 (org-move-overlay org-agenda-restriction-lock-overlay
(point) (point-at-eol))
22033 (move-marker org-agenda-restrict-begin
(point))
22034 (move-marker org-agenda-restrict-end
22035 (save-excursion (org-end-of-subtree t
)))
22036 (message "Locking agenda restriction to subtree"))
22037 (put 'org-agenda-files
'org-restrict
22038 (list (buffer-file-name (buffer-base-buffer))))
22039 (setq org-agenda-restrict nil
)
22040 (setq org-agenda-overriding-restriction
'file
)
22041 (move-marker org-agenda-restrict-begin nil
)
22042 (move-marker org-agenda-restrict-end nil
)
22043 (message "Locking agenda restriction to file"))
22044 (setq current-prefix-arg nil
)
22045 (org-agenda-maybe-redo))
22047 (defun org-agenda-remove-restriction-lock (&optional noupdate
)
22048 "Remove the agenda restriction lock."
22050 (org-detach-overlay org-agenda-restriction-lock-overlay
)
22051 (org-detach-overlay org-speedbar-restriction-lock-overlay
)
22052 (setq org-agenda-overriding-restriction nil
)
22053 (setq org-agenda-restrict nil
)
22054 (put 'org-agenda-files
'org-restrict nil
)
22055 (move-marker org-agenda-restrict-begin nil
)
22056 (move-marker org-agenda-restrict-end nil
)
22057 (setq current-prefix-arg nil
)
22058 (message "Agenda restriction lock removed")
22059 (or noupdate
(org-agenda-maybe-redo)))
22061 (defun org-agenda-maybe-redo ()
22062 "If there is any window showing the agenda view, update it."
22063 (let ((w (get-buffer-window org-agenda-buffer-name t
))
22064 (w0 (selected-window)))
22069 (if org-agenda-overriding-restriction
22070 (message "Agenda view shifted to new %s restriction"
22071 org-agenda-overriding-restriction
)
22072 (message "Agenda restriction lock removed")))))
22074 ;;; Agenda commands
22076 (defun org-agenda-check-type (error &rest types
)
22077 "Check if agenda buffer is of allowed type.
22078 If ERROR is non-nil, throw an error, otherwise just return nil."
22079 (if (memq org-agenda-type types
)
22082 (error "Not allowed in %s-type agenda buffers" org-agenda-type
)
22085 (defun org-agenda-quit ()
22086 "Exit agenda by removing the window or the buffer."
22088 (let ((buf (current-buffer)))
22089 (if (not (one-window-p)) (delete-window))
22091 (org-agenda-reset-markers)
22092 (org-columns-remove-overlays))
22093 ;; Maybe restore the pre-agenda window configuration.
22094 (and org-agenda-restore-windows-after-quit
22095 (not (eq org-agenda-window-setup
'other-frame
))
22096 org-pre-agenda-window-conf
22097 (set-window-configuration org-pre-agenda-window-conf
)))
22099 (defun org-agenda-exit ()
22100 "Exit agenda by removing the window or the buffer.
22101 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
22102 Org-mode buffers visited directly by the user will not be touched."
22104 (org-release-buffers org-agenda-new-buffers
)
22105 (setq org-agenda-new-buffers nil
)
22108 (defun org-agenda-execute (arg)
22109 "Execute another agenda command, keeping same window.\\<global-map>
22110 So this is just a shortcut for `\\[org-agenda]', available in the agenda."
22112 (let ((org-agenda-window-setup 'current-window
))
22115 (defun org-save-all-org-buffers ()
22116 "Save all Org-mode buffers without user confirmation."
22118 (message "Saving all Org-mode buffers...")
22119 (save-some-buffers t
'org-mode-p
)
22120 (message "Saving all Org-mode buffers... done"))
22122 (defun org-agenda-redo ()
22124 When this is the global TODO list, a prefix argument will be interpreted."
22126 (let* ((org-agenda-keep-modes t
)
22127 (line (org-current-line))
22128 (window-line (- line
(org-current-line (window-start))))
22129 (lprops (get 'org-agenda-redo-command
'org-lprops
)))
22130 (message "Rebuilding agenda buffer...")
22131 (org-let lprops
'(eval org-agenda-redo-command
))
22132 (setq org-agenda-undo-list nil
22133 org-agenda-pending-undo-list nil
)
22134 (message "Rebuilding agenda buffer...done")
22136 (recenter window-line
)))
22138 (defun org-agenda-goto-date (date)
22139 "Jump to DATE in agenda."
22140 (interactive (list (org-read-date)))
22141 (org-agenda-list nil date
))
22143 (defun org-agenda-goto-today ()
22146 (org-agenda-check-type t
'timeline
'agenda
)
22147 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t
)))
22149 (tdpos (goto-char tdpos
))
22150 ((eq org-agenda-type
'agenda
)
22151 (let* ((sd (time-to-days
22152 (time-subtract (current-time)
22153 (list 0 (* 3600 org-extend-today-until
) 0))))
22154 (comp (org-agenda-compute-time-span sd org-agenda-span
))
22155 (org-agenda-overriding-arguments org-agenda-last-arguments
))
22156 (setf (nth 1 org-agenda-overriding-arguments
) (car comp
))
22157 (setf (nth 2 org-agenda-overriding-arguments
) (cdr comp
))
22159 (org-agenda-find-same-or-today-or-agenda)))
22160 (t (error "Cannot find today")))))
22162 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt
)
22164 (or (and cnt
(text-property-any (point-min) (point-max) 'org-day-cnt cnt
))
22165 (text-property-any (point-min) (point-max) 'org-today t
)
22166 (text-property-any (point-min) (point-max) 'org-agenda-type
'agenda
)
22169 (defun org-agenda-later (arg)
22170 "Go forward in time by thee current span.
22171 With prefix ARG, go forward that many times the current span."
22173 (org-agenda-check-type t
'agenda
)
22174 (let* ((span org-agenda-span
)
22175 (sd org-starting-day
)
22176 (greg (calendar-gregorian-from-absolute sd
))
22177 (cnt (get-text-property (point) 'org-day-cnt
))
22181 (setq sd
(+ arg sd
) nd
1))
22183 (setq sd
(+ (* 7 arg
) sd
) nd
7))
22185 (setq greg2
(list (+ (car greg
) arg
) (nth 1 greg
) (nth 2 greg
))
22186 sd
(calendar-absolute-from-gregorian greg2
))
22187 (setcar greg2
(1+ (car greg2
)))
22188 (setq nd
(- (calendar-absolute-from-gregorian greg2
) sd
)))
22190 (setq greg2
(list (car greg
) (nth 1 greg
) (+ arg
(nth 2 greg
)))
22191 sd
(calendar-absolute-from-gregorian greg2
))
22192 (setcar (nthcdr 2 greg2
) (1+ (nth 2 greg2
)))
22193 (setq nd
(- (calendar-absolute-from-gregorian greg2
) sd
))))
22194 (let ((org-agenda-overriding-arguments
22195 (list (car org-agenda-last-arguments
) sd nd t
)))
22197 (org-agenda-find-same-or-today-or-agenda cnt
))))
22199 (defun org-agenda-earlier (arg)
22200 "Go backward in time by the current span.
22201 With prefix ARG, go backward that many times the current span."
22203 (org-agenda-later (- arg
)))
22205 (defun org-agenda-day-view ()
22206 "Switch to daily view for agenda."
22208 (setq org-agenda-ndays
1)
22209 (org-agenda-change-time-span 'day
))
22210 (defun org-agenda-week-view ()
22211 "Switch to daily view for agenda."
22213 (setq org-agenda-ndays
7)
22214 (org-agenda-change-time-span 'week
))
22215 (defun org-agenda-month-view ()
22216 "Switch to daily view for agenda."
22218 (org-agenda-change-time-span 'month
))
22219 (defun org-agenda-year-view ()
22220 "Switch to daily view for agenda."
22222 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
22223 (org-agenda-change-time-span 'year
)
22226 (defun org-agenda-change-time-span (span)
22227 "Change the agenda view to SPAN.
22228 SPAN may be `day', `week', `month', `year'."
22229 (org-agenda-check-type t
'agenda
)
22230 (if (equal org-agenda-span span
)
22231 (error "Viewing span is already \"%s\"" span
))
22232 (let* ((sd (or (get-text-property (point) 'day
)
22234 (computed (org-agenda-compute-time-span sd span
))
22235 (org-agenda-overriding-arguments
22236 (list (car org-agenda-last-arguments
)
22237 (car computed
) (cdr computed
) t
)))
22239 (org-agenda-find-same-or-today-or-agenda))
22240 (org-agenda-set-mode-name)
22241 (message "Switched to %s view" span
))
22243 (defun org-agenda-compute-time-span (sd span
)
22244 "Compute starting date and number of days for agenda.
22245 SPAN may be `day', `week', `month', `year'. The return value
22246 is a cons cell with the starting date and the number of days,
22247 so that the date SD will be in that range."
22248 (let* ((greg (calendar-gregorian-from-absolute sd
))
22254 (let* ((nt (calendar-day-of-week
22255 (calendar-gregorian-from-absolute sd
)))
22256 (d (if org-agenda-start-on-weekday
22257 (- nt org-agenda-start-on-weekday
)
22259 (setq sd
(- sd
(+ (if (< d
0) 7 0) d
)))
22262 (setq sd
(calendar-absolute-from-gregorian
22263 (list (car greg
) 1 (nth 2 greg
)))
22264 nd
(- (calendar-absolute-from-gregorian
22265 (list (1+ (car greg
)) 1 (nth 2 greg
)))
22268 (setq sd
(calendar-absolute-from-gregorian
22269 (list 1 1 (nth 2 greg
)))
22270 nd
(- (calendar-absolute-from-gregorian
22271 (list 1 1 (1+ (nth 2 greg
))))
22275 ;; FIXME: does not work if user makes date format that starts with a blank
22276 (defun org-agenda-next-date-line (&optional arg
)
22277 "Jump to the next line indicating a date in agenda buffer."
22279 (org-agenda-check-type t
'agenda
'timeline
)
22280 (beginning-of-line 1)
22281 (if (looking-at "^\\S-") (forward-char 1))
22282 (if (not (re-search-forward "^\\S-" nil t arg
))
22285 (error "No next date after this line in this buffer")))
22286 (goto-char (match-beginning 0)))
22288 (defun org-agenda-previous-date-line (&optional arg
)
22289 "Jump to the previous line indicating a date in agenda buffer."
22291 (org-agenda-check-type t
'agenda
'timeline
)
22292 (beginning-of-line 1)
22293 (if (not (re-search-backward "^\\S-" nil t arg
))
22294 (error "No previous date before this line in this buffer")))
22296 ;; Initialize the highlight
22297 (defvar org-hl
(org-make-overlay 1 1))
22298 (org-overlay-put org-hl
'face
'highlight
)
22300 (defun org-highlight (begin end
&optional buffer
)
22301 "Highlight a region with overlay."
22302 (funcall (if (featurep 'xemacs
) 'set-extent-endpoints
'move-overlay
)
22303 org-hl begin end
(or buffer
(current-buffer))))
22305 (defun org-unhighlight ()
22306 "Detach overlay INDEX."
22307 (funcall (if (featurep 'xemacs
) 'detach-extent
'delete-overlay
) org-hl
))
22309 ;; FIXME this is currently not used.
22310 (defun org-highlight-until-next-command (beg end
&optional buffer
)
22311 (org-highlight beg end buffer
)
22312 (add-hook 'pre-command-hook
'org-unhighlight-once
))
22313 (defun org-unhighlight-once ()
22314 (remove-hook 'pre-command-hook
'org-unhighlight-once
)
22317 (defun org-agenda-follow-mode ()
22318 "Toggle follow mode in an agenda buffer."
22320 (setq org-agenda-follow-mode
(not org-agenda-follow-mode
))
22321 (org-agenda-set-mode-name)
22322 (message "Follow mode is %s"
22323 (if org-agenda-follow-mode
"on" "off")))
22325 (defun org-agenda-log-mode ()
22326 "Toggle log mode in an agenda buffer."
22328 (org-agenda-check-type t
'agenda
'timeline
)
22329 (setq org-agenda-show-log
(not org-agenda-show-log
))
22330 (org-agenda-set-mode-name)
22332 (message "Log mode is %s"
22333 (if org-agenda-show-log
"on" "off")))
22335 (defun org-agenda-toggle-diary ()
22336 "Toggle diary inclusion in an agenda buffer."
22338 (org-agenda-check-type t
'agenda
)
22339 (setq org-agenda-include-diary
(not org-agenda-include-diary
))
22341 (org-agenda-set-mode-name)
22342 (message "Diary inclusion turned %s"
22343 (if org-agenda-include-diary
"on" "off")))
22345 (defun org-agenda-toggle-time-grid ()
22346 "Toggle time grid in an agenda buffer."
22348 (org-agenda-check-type t
'agenda
)
22349 (setq org-agenda-use-time-grid
(not org-agenda-use-time-grid
))
22351 (org-agenda-set-mode-name)
22352 (message "Time-grid turned %s"
22353 (if org-agenda-use-time-grid
"on" "off")))
22355 (defun org-agenda-set-mode-name ()
22356 "Set the mode name to indicate all the small mode settings."
22358 (concat "Org-Agenda"
22359 (if (equal org-agenda-ndays
1) " Day" "")
22360 (if (equal org-agenda-ndays
7) " Week" "")
22361 (if org-agenda-follow-mode
" Follow" "")
22362 (if org-agenda-include-diary
" Diary" "")
22363 (if org-agenda-use-time-grid
" Grid" "")
22364 (if org-agenda-show-log
" Log" "")))
22365 (force-mode-line-update))
22367 (defun org-agenda-post-command-hook ()
22368 (and (eolp) (not (bolp)) (backward-char 1))
22369 (setq org-agenda-type
(get-text-property (point) 'org-agenda-type
))
22370 (if (and org-agenda-follow-mode
22371 (get-text-property (point) 'org-marker
))
22372 (org-agenda-show)))
22374 (defun org-agenda-show-priority ()
22375 "Show the priority of the current item.
22376 This priority is composed of the main priority given with the [#A] cookies,
22377 and by additional input from the age of a schedules or deadline entry."
22379 (let* ((pri (get-text-property (point-at-bol) 'priority
)))
22380 (message "Priority is %d" (if pri pri -
1000))))
22382 (defun org-agenda-show-tags ()
22383 "Show the tags applicable to the current item."
22385 (let* ((tags (get-text-property (point-at-bol) 'tags
)))
22387 (message "Tags are :%s:"
22388 (org-no-properties (mapconcat 'identity tags
":")))
22389 (message "No tags associated with this line"))))
22391 (defun org-agenda-goto (&optional highlight
)
22392 "Go to the Org-mode file which contains the item at point."
22394 (let* ((marker (or (get-text-property (point) 'org-marker
)
22395 (org-agenda-error)))
22396 (buffer (marker-buffer marker
))
22397 (pos (marker-position marker
)))
22398 (switch-to-buffer-other-window buffer
)
22402 (org-show-context 'agenda
)
22404 (and (outline-next-heading)
22405 (org-flag-heading nil
)))) ; show the next heading
22406 (recenter (/ (window-height) 2))
22407 (run-hooks 'org-agenda-after-show-hook
)
22408 (and highlight
(org-highlight (point-at-bol) (point-at-eol)))))
22410 (defvar org-agenda-after-show-hook nil
22411 "Normal hook run after an item has been shown from the agenda.
22412 Point is in the buffer where the item originated.")
22414 (defun org-agenda-kill ()
22415 "Kill the entry or subtree belonging to the current agenda entry."
22417 (or (eq major-mode
'org-agenda-mode
) (error "Not in agenda"))
22418 (let* ((marker (or (get-text-property (point) 'org-marker
)
22419 (org-agenda-error)))
22420 (buffer (marker-buffer marker
))
22421 (pos (marker-position marker
))
22422 (type (get-text-property (point) 'type
))
22423 dbeg dend
(n 0) conf
)
22424 (org-with-remote-undo buffer
22425 (with-current-buffer buffer
22428 (if (and (org-mode-p) (not (member type
'("sexp"))))
22429 (setq dbeg
(progn (org-back-to-heading t
) (point))
22430 dend
(org-end-of-subtree t t
))
22431 (setq dbeg
(point-at-bol)
22432 dend
(min (point-max) (1+ (point-at-eol)))))
22434 (while (re-search-forward "^[ \t]*\\S-" dend t
) (setq n
(1+ n
)))))
22435 (setq conf
(or (eq t org-agenda-confirm-kill
)
22436 (and (numberp org-agenda-confirm-kill
)
22437 (> n org-agenda-confirm-kill
))))
22440 (format "Delete entry with %d lines in buffer \"%s\"? "
22441 n
(buffer-name buffer
))))
22443 (org-remove-subtree-entries-from-agenda buffer dbeg dend
)
22444 (with-current-buffer buffer
(delete-region dbeg dend
))
22445 (message "Agenda item and source killed"))))
22447 (defun org-agenda-archive ()
22448 "Kill the entry or subtree belonging to the current agenda entry."
22450 (or (eq major-mode
'org-agenda-mode
) (error "Not in agenda"))
22451 (let* ((marker (or (get-text-property (point) 'org-marker
)
22452 (org-agenda-error)))
22453 (buffer (marker-buffer marker
))
22454 (pos (marker-position marker
)))
22455 (org-with-remote-undo buffer
22456 (with-current-buffer buffer
22460 (org-remove-subtree-entries-from-agenda)
22461 (org-back-to-heading t
)
22462 (org-archive-subtree))
22463 (error "Archiving works only in Org-mode files"))))))
22465 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end
)
22466 "Remove all lines in the agenda that correspond to a given subtree.
22467 The subtree is the one in buffer BUF, starting at BEG and ending at END.
22468 If this information is not given, the function uses the tree at point."
22469 (let ((buf (or buf
(current-buffer))) m p
)
22471 (unless (and beg end
)
22472 (org-back-to-heading t
)
22474 (org-end-of-subtree t
)
22475 (setq end
(point)))
22476 (set-buffer (get-buffer org-agenda-buffer-name
))
22478 (goto-char (point-max))
22479 (beginning-of-line 1)
22480 (while (not (bobp))
22481 (when (and (setq m
(get-text-property (point) 'org-marker
))
22482 (equal buf
(marker-buffer m
))
22483 (setq p
(marker-position m
))
22486 (let ((inhibit-read-only t
))
22487 (delete-region (point-at-bol) (1+ (point-at-eol)))))
22488 (beginning-of-line 0))))))
22490 (defun org-agenda-open-link ()
22491 "Follow the link in the current line, if any."
22493 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local
)
22496 (narrow-to-region (point-at-bol) (point-at-eol))
22497 (org-open-at-point))))
22499 (defun org-agenda-copy-local-variable (var)
22500 "Get a variable from a referenced buffer and install it here."
22501 (let ((m (get-text-property (point) 'org-marker
)))
22502 (when (and m
(buffer-live-p (marker-buffer m
)))
22503 (org-set-local var
(with-current-buffer (marker-buffer m
)
22504 (symbol-value var
))))))
22506 (defun org-agenda-switch-to (&optional delete-other-windows
)
22507 "Go to the Org-mode file which contains the item at point."
22509 (let* ((marker (or (get-text-property (point) 'org-marker
)
22510 (org-agenda-error)))
22511 (buffer (marker-buffer marker
))
22512 (pos (marker-position marker
)))
22513 (switch-to-buffer buffer
)
22514 (and delete-other-windows
(delete-other-windows))
22518 (org-show-context 'agenda
)
22520 (and (outline-next-heading)
22521 (org-flag-heading nil
)))))) ; show the next heading
22523 (defun org-agenda-goto-mouse (ev)
22524 "Go to the Org-mode file which contains the item at the mouse click."
22526 (mouse-set-point ev
)
22529 (defun org-agenda-show ()
22530 "Display the Org-mode file which contains the item at point."
22532 (let ((win (selected-window)))
22533 (org-agenda-goto t
)
22534 (select-window win
)))
22536 (defun org-agenda-recenter (arg)
22537 "Display the Org-mode file which contains the item at point and recenter."
22539 (let ((win (selected-window)))
22540 (org-agenda-goto t
)
22542 (select-window win
)))
22544 (defun org-agenda-show-mouse (ev)
22545 "Display the Org-mode file which contains the item at the mouse click."
22547 (mouse-set-point ev
)
22550 (defun org-agenda-check-no-diary ()
22551 "Check if the entry is a diary link and abort if yes."
22552 (if (get-text-property (point) 'org-agenda-diary-link
)
22553 (org-agenda-error)))
22555 (defun org-agenda-error ()
22556 (error "Command not allowed in this line"))
22558 (defun org-agenda-tree-to-indirect-buffer ()
22559 "Show the subtree corresponding to the current entry in an indirect buffer.
22560 This calls the command `org-tree-to-indirect-buffer' from the original
22562 With numerical prefix arg ARG, go up to this level and then take that tree.
22563 With a C-u prefix, make a separate frame for this tree (i.e. don't use the
22566 (org-agenda-check-no-diary)
22567 (let* ((marker (or (get-text-property (point) 'org-marker
)
22568 (org-agenda-error)))
22569 (buffer (marker-buffer marker
))
22570 (pos (marker-position marker
)))
22571 (with-current-buffer buffer
22574 (call-interactively 'org-tree-to-indirect-buffer
)))))
22576 (defvar org-last-heading-marker
(make-marker)
22577 "Marker pointing to the headline that last changed its TODO state
22578 by a remote command from the agenda.")
22580 (defun org-agenda-todo-nextset ()
22581 "Switch TODO entry to next sequence."
22583 (org-agenda-todo 'nextset
))
22585 (defun org-agenda-todo-previousset ()
22586 "Switch TODO entry to previous sequence."
22588 (org-agenda-todo 'previousset
))
22590 (defun org-agenda-todo (&optional arg
)
22591 "Cycle TODO state of line at point, also in Org-mode file.
22592 This changes the line at point, all other lines in the agenda referring to
22593 the same tree node, and the headline of the tree node in the Org-mode file."
22595 (org-agenda-check-no-diary)
22596 (let* ((col (current-column))
22597 (marker (or (get-text-property (point) 'org-marker
)
22598 (org-agenda-error)))
22599 (buffer (marker-buffer marker
))
22600 (pos (marker-position marker
))
22601 (hdmarker (get-text-property (point) 'org-hd-marker
))
22602 (inhibit-read-only t
)
22604 (org-with-remote-undo buffer
22605 (with-current-buffer buffer
22608 (org-show-context 'agenda
)
22610 (and (outline-next-heading)
22611 (org-flag-heading nil
))) ; show the next heading
22613 (and (bolp) (forward-char 1))
22614 (setq newhead
(org-get-heading))
22616 (org-back-to-heading)
22617 (move-marker org-last-heading-marker
(point))))
22618 (beginning-of-line 1)
22620 (org-agenda-change-all-lines newhead hdmarker
'fixface
))
22621 (move-to-column col
))))
22623 (defun org-agenda-change-all-lines (newhead hdmarker
&optional fixface
)
22624 "Change all lines in the agenda buffer which match HDMARKER.
22625 The new content of the line will be NEWHEAD (as modified by
22626 `org-format-agenda-item'). HDMARKER is checked with
22627 `equal' against all `org-hd-marker' text properties in the file.
22628 If FIXFACE is non-nil, the face of each item is modified acording to
22629 the new TODO state."
22630 (let* ((inhibit-read-only t
)
22631 props m pl undone-face done-face finish new dotime cat tags
)
22633 (goto-char (point-max))
22634 (beginning-of-line 1)
22635 (while (not finish
)
22636 (setq finish
(bobp))
22637 (when (and (setq m
(get-text-property (point) 'org-hd-marker
))
22638 (equal m hdmarker
))
22639 (setq props
(text-properties-at (point))
22640 dotime
(get-text-property (point) 'dotime
)
22641 cat
(get-text-property (point) 'org-category
)
22642 tags
(get-text-property (point) 'tags
)
22643 new
(org-format-agenda-item "x" newhead cat tags dotime
'noprefix
)
22644 pl
(get-text-property (point) 'prefix-length
)
22645 undone-face
(get-text-property (point) 'undone-face
)
22646 done-face
(get-text-property (point) 'done-face
))
22647 (move-to-column pl
)
22650 (beginning-of-line 1)
22651 (and (looking-at ".*\n?") (replace-match "")))
22653 (replace-match new t t
)
22654 (beginning-of-line 1)
22655 (add-text-properties (point-at-bol) (point-at-eol) props
)
22657 (add-text-properties
22658 (point-at-bol) (point-at-eol)
22660 (if org-last-todo-state-is-todo
22661 undone-face done-face
))))
22662 (org-agenda-highlight-todo 'line
)
22663 (beginning-of-line 1))
22664 (t (error "Line update did not work"))))
22665 (beginning-of-line 0)))
22666 (org-finalize-agenda)))
22668 (defun org-agenda-align-tags (&optional line
)
22669 "Align all tags in agenda items to `org-agenda-tags-column'."
22670 (let ((inhibit-read-only t
) l c
)
22672 (goto-char (if line
(point-at-bol) (point-min)))
22673 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
22674 (if line
(point-at-eol) nil
) t
)
22675 (add-text-properties
22676 (match-beginning 2) (match-end 2)
22677 (list 'face
(list 'org-tag
(get-text-property
22678 (match-beginning 2) 'face
))))
22679 (setq l
(- (match-end 2) (match-beginning 2))
22680 c
(if (< org-agenda-tags-column
0)
22681 (- (abs org-agenda-tags-column
) l
)
22682 org-agenda-tags-column
))
22683 (delete-region (match-beginning 1) (match-end 1))
22684 (goto-char (match-beginning 1))
22685 (insert (org-add-props
22686 (make-string (max 1 (- c
(current-column))) ?\
)
22687 (text-properties-at (point))))))))
22689 (defun org-agenda-priority-up ()
22690 "Increase the priority of line at point, also in Org-mode file."
22692 (org-agenda-priority 'up
))
22694 (defun org-agenda-priority-down ()
22695 "Decrease the priority of line at point, also in Org-mode file."
22697 (org-agenda-priority 'down
))
22699 (defun org-agenda-priority (&optional force-direction
)
22700 "Set the priority of line at point, also in Org-mode file.
22701 This changes the line at point, all other lines in the agenda referring to
22702 the same tree node, and the headline of the tree node in the Org-mode file."
22704 (org-agenda-check-no-diary)
22705 (let* ((marker (or (get-text-property (point) 'org-marker
)
22706 (org-agenda-error)))
22707 (hdmarker (get-text-property (point) 'org-hd-marker
))
22708 (buffer (marker-buffer hdmarker
))
22709 (pos (marker-position hdmarker
))
22710 (inhibit-read-only t
)
22712 (org-with-remote-undo buffer
22713 (with-current-buffer buffer
22716 (org-show-context 'agenda
)
22718 (and (outline-next-heading)
22719 (org-flag-heading nil
))) ; show the next heading
22720 (funcall 'org-priority force-direction
)
22722 (setq newhead
(org-get-heading)))
22723 (org-agenda-change-all-lines newhead hdmarker
)
22724 (beginning-of-line 1))))
22726 (defun org-get-tags-at (&optional pos
)
22727 "Get a list of all headline tags applicable at POS.
22728 POS defaults to point. If tags are inherited, the list contains
22729 the targets in the same sequence as the headlines appear, i.e.
22730 the tags of the current headline come last."
22732 (let (tags lastpos
)
22736 (goto-char (or pos
(point)))
22738 (org-back-to-heading t
)
22739 (condition-case nil
22740 (while (not (equal lastpos
(point)))
22741 (setq lastpos
(point))
22742 (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
22743 (setq tags
(append (org-split-string
22744 (org-match-string-no-properties 1) ":")
22746 (or org-use-tag-inheritance
(error ""))
22747 (org-up-heading-all 1))
22751 ;; FIXME: should fix the tags property of the agenda line.
22752 (defun org-agenda-set-tags ()
22753 "Set tags for the current headline."
22755 (org-agenda-check-no-diary)
22756 (if (and (org-region-active-p) (interactive-p))
22757 (call-interactively 'org-change-tag-in-region
)
22758 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
22759 (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker
)
22760 (org-agenda-error)))
22761 (buffer (marker-buffer hdmarker
))
22762 (pos (marker-position hdmarker
))
22763 (inhibit-read-only t
)
22765 (org-with-remote-undo buffer
22766 (with-current-buffer buffer
22770 (org-show-context 'agenda
))
22772 (and (outline-next-heading)
22773 (org-flag-heading nil
))) ; show the next heading
22775 (call-interactively 'org-set-tags
)
22777 (setq newhead
(org-get-heading)))
22778 (org-agenda-change-all-lines newhead hdmarker
)
22779 (beginning-of-line 1)))))
22781 (defun org-agenda-toggle-archive-tag ()
22782 "Toggle the archive tag for the current entry."
22784 (org-agenda-check-no-diary)
22785 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
22786 (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker
)
22787 (org-agenda-error)))
22788 (buffer (marker-buffer hdmarker
))
22789 (pos (marker-position hdmarker
))
22790 (inhibit-read-only t
)
22792 (org-with-remote-undo buffer
22793 (with-current-buffer buffer
22796 (org-show-context 'agenda
)
22798 (and (outline-next-heading)
22799 (org-flag-heading nil
))) ; show the next heading
22800 (call-interactively 'org-toggle-archive-tag
)
22802 (setq newhead
(org-get-heading)))
22803 (org-agenda-change-all-lines newhead hdmarker
)
22804 (beginning-of-line 1))))
22806 (defun org-agenda-date-later (arg &optional what
)
22807 "Change the date of this item to one day later."
22809 (org-agenda-check-type t
'agenda
'timeline
)
22810 (org-agenda-check-no-diary)
22811 (let* ((marker (or (get-text-property (point) 'org-marker
)
22812 (org-agenda-error)))
22813 (buffer (marker-buffer marker
))
22814 (pos (marker-position marker
)))
22815 (org-with-remote-undo buffer
22816 (with-current-buffer buffer
22819 (if (not (org-at-timestamp-p))
22820 (error "Cannot find time stamp"))
22821 (org-timestamp-change arg
(or what
'day
)))
22822 (org-agenda-show-new-time marker org-last-changed-timestamp
))
22823 (message "Time stamp changed to %s" org-last-changed-timestamp
)))
22825 (defun org-agenda-date-earlier (arg &optional what
)
22826 "Change the date of this item to one day earlier."
22828 (org-agenda-date-later (- arg
) what
))
22830 (defun org-agenda-show-new-time (marker stamp
&optional prefix
)
22831 "Show new date stamp via text properties."
22832 ;; We use text properties to make this undoable
22833 (let ((inhibit-read-only t
))
22834 (setq stamp
(concat " " prefix
" => " stamp
))
22836 (goto-char (point-max))
22837 (while (not (bobp))
22838 (when (equal marker
(get-text-property (point) 'org-marker
))
22839 (move-to-column (- (window-width) (length stamp
)) t
)
22840 (if (featurep 'xemacs
)
22841 ;; Use `duplicable' property to trigger undo recording
22842 (let ((ex (make-extent nil nil
))
22843 (gl (make-glyph stamp
)))
22844 (set-glyph-face gl
'secondary-selection
)
22845 (set-extent-properties
22846 ex
(list 'invisible t
'end-glyph gl
'duplicable t
))
22847 (insert-extent ex
(1- (point)) (point-at-eol)))
22848 (add-text-properties
22849 (1- (point)) (point-at-eol)
22850 (list 'display
(org-add-props stamp nil
22851 'face
'secondary-selection
))))
22852 (beginning-of-line 1))
22853 (beginning-of-line 0)))))
22855 (defun org-agenda-date-prompt (arg)
22856 "Change the date of this item. Date is prompted for, with default today.
22857 The prefix ARG is passed to the `org-time-stamp' command and can therefore
22858 be used to request time specification in the time stamp."
22860 (org-agenda-check-type t
'agenda
'timeline
)
22861 (org-agenda-check-no-diary)
22862 (let* ((marker (or (get-text-property (point) 'org-marker
)
22863 (org-agenda-error)))
22864 (buffer (marker-buffer marker
))
22865 (pos (marker-position marker
)))
22866 (org-with-remote-undo buffer
22867 (with-current-buffer buffer
22870 (if (not (org-at-timestamp-p))
22871 (error "Cannot find time stamp"))
22872 (org-time-stamp arg
)
22873 (message "Time stamp changed to %s" org-last-changed-timestamp
)))))
22875 (defun org-agenda-schedule (arg)
22876 "Schedule the item at point."
22878 (org-agenda-check-type t
'agenda
'timeline
'todo
'tags
)
22879 (org-agenda-check-no-diary)
22880 (let* ((marker (or (get-text-property (point) 'org-marker
)
22881 (org-agenda-error)))
22882 (buffer (marker-buffer marker
))
22883 (pos (marker-position marker
))
22884 (org-insert-labeled-timestamps-at-point nil
)
22886 (org-with-remote-undo buffer
22887 (with-current-buffer buffer
22890 (setq ts
(org-schedule arg
)))
22891 (org-agenda-show-new-time marker ts
"S"))
22892 (message "Item scheduled for %s" ts
)))
22894 (defun org-agenda-deadline (arg)
22895 "Schedule the item at point."
22897 (org-agenda-check-type t
'agenda
'timeline
'todo
'tags
)
22898 (org-agenda-check-no-diary)
22899 (let* ((marker (or (get-text-property (point) 'org-marker
)
22900 (org-agenda-error)))
22901 (buffer (marker-buffer marker
))
22902 (pos (marker-position marker
))
22903 (org-insert-labeled-timestamps-at-point nil
)
22905 (org-with-remote-undo buffer
22906 (with-current-buffer buffer
22909 (setq ts
(org-deadline arg
)))
22910 (org-agenda-show-new-time marker ts
"S"))
22911 (message "Deadline for this item set to %s" ts
)))
22913 (defun org-get-heading (&optional no-tags
)
22914 "Return the heading of the current entry, without the stars."
22916 (org-back-to-heading t
)
22919 (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
22920 "\\*+[ \t]+\\([^\r\n]*\\)"))
22921 (match-string 1) "")))
22923 (defun org-agenda-clock-in (&optional arg
)
22924 "Start the clock on the currently selected item."
22926 (org-agenda-check-no-diary)
22927 (let* ((marker (or (get-text-property (point) 'org-marker
)
22928 (org-agenda-error)))
22929 (pos (marker-position marker
)))
22930 (org-with-remote-undo (marker-buffer marker
)
22931 (with-current-buffer (marker-buffer marker
)
22936 (defun org-agenda-clock-out (&optional arg
)
22937 "Stop the currently running clock."
22939 (unless (marker-buffer org-clock-marker
)
22940 (error "No running clock"))
22941 (org-with-remote-undo (marker-buffer org-clock-marker
)
22944 (defun org-agenda-clock-cancel (&optional arg
)
22945 "Cancel the currently running clock."
22947 (unless (marker-buffer org-clock-marker
)
22948 (error "No running clock"))
22949 (org-with-remote-undo (marker-buffer org-clock-marker
)
22950 (org-clock-cancel)))
22952 (defun org-agenda-diary-entry ()
22953 "Make a diary entry, like the `i' command from the calendar.
22954 All the standard commands work: block, weekly etc."
22956 (org-agenda-check-type t
'agenda
'timeline
)
22957 (require 'diary-lib
)
22958 (let* ((char (progn
22959 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
22960 (read-char-exclusive)))
22961 (cmd (cdr (assoc char
22962 '((?d . insert-diary-entry
)
22963 (?w . insert-weekly-diary-entry
)
22964 (?m . insert-monthly-diary-entry
)
22965 (?y . insert-yearly-diary-entry
)
22966 (?a . insert-anniversary-diary-entry
)
22967 (?b . insert-block-diary-entry
)
22968 (?c . insert-cyclic-diary-entry
)))))
22969 (oldf (symbol-function 'calendar-cursor-to-date
))
22970 ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
22972 (mark (or (mark t
) (point))))
22974 (error "No command associated with <%c>" char
))
22975 (unless (and (get-text-property point
'day
)
22976 (or (not (equal ?b char
))
22977 (get-text-property mark
'day
)))
22978 (error "Don't know which date to use for diary entry"))
22979 ;; We implement this by hacking the `calendar-cursor-to-date' function
22980 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
22981 (let ((calendar-mark-ring
22982 (list (calendar-gregorian-from-absolute
22983 (or (get-text-property mark
'day
)
22984 (get-text-property point
'day
))))))
22987 (fset 'calendar-cursor-to-date
22988 (lambda (&optional error
)
22989 (calendar-gregorian-from-absolute
22990 (get-text-property point
'day
))))
22991 (call-interactively cmd
))
22992 (fset 'calendar-cursor-to-date oldf
)))))
22995 (defun org-agenda-execute-calendar-command (cmd)
22996 "Execute a calendar command from the agenda, with the date associated to
22997 the cursor position."
22998 (org-agenda-check-type t
'agenda
'timeline
)
22999 (require 'diary-lib
)
23000 (unless (get-text-property (point) 'day
)
23001 (error "Don't know which date to use for calendar command"))
23002 (let* ((oldf (symbol-function 'calendar-cursor-to-date
))
23004 (date (calendar-gregorian-from-absolute
23005 (get-text-property point
'day
)))
23006 ;; the following 3 vars are needed in the calendar
23007 (displayed-day (extract-calendar-day date
))
23008 (displayed-month (extract-calendar-month date
))
23009 (displayed-year (extract-calendar-year date
)))
23012 (fset 'calendar-cursor-to-date
23013 (lambda (&optional error
)
23014 (calendar-gregorian-from-absolute
23015 (get-text-property point
'day
))))
23016 (call-interactively cmd
))
23017 (fset 'calendar-cursor-to-date oldf
))))
23019 (defun org-agenda-phases-of-moon ()
23020 "Display the phases of the moon for the 3 months around the cursor date."
23022 (org-agenda-execute-calendar-command 'calendar-phases-of-moon
))
23024 (defun org-agenda-holidays ()
23025 "Display the holidays for the 3 months around the cursor date."
23027 (org-agenda-execute-calendar-command 'list-calendar-holidays
))
23029 (defun org-agenda-sunrise-sunset (arg)
23030 "Display sunrise and sunset for the cursor date.
23031 Latitude and longitude can be specified with the variables
23032 `calendar-latitude' and `calendar-longitude'. When called with prefix
23033 argument, latitude and longitude will be prompted for."
23035 (let ((calendar-longitude (if arg nil calendar-longitude
))
23036 (calendar-latitude (if arg nil calendar-latitude
))
23037 (calendar-location-name
23038 (if arg
"the given coordinates" calendar-location-name
)))
23039 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset
)))
23041 (defun org-agenda-goto-calendar ()
23042 "Open the Emacs calendar with the date at the cursor."
23044 (org-agenda-check-type t
'agenda
'timeline
)
23045 (let* ((day (or (get-text-property (point) 'day
)
23046 (error "Don't know which date to open in calendar")))
23047 (date (calendar-gregorian-from-absolute day
))
23048 (calendar-move-hook nil
)
23049 (view-calendar-holidays-initially nil
)
23050 (view-diary-entries-initially nil
))
23052 (calendar-goto-date date
)))
23054 (defun org-calendar-goto-agenda ()
23055 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
23056 This is a command that has to be installed in `calendar-mode-map'."
23058 (org-agenda-list nil
(calendar-absolute-from-gregorian
23059 (calendar-cursor-to-date))
23062 (defun org-agenda-convert-date ()
23064 (org-agenda-check-type t
'agenda
'timeline
)
23065 (let ((day (get-text-property (point) 'day
))
23068 (error "Don't know which date to convert"))
23069 (setq date
(calendar-gregorian-from-absolute day
))
23071 "Gregorian: " (calendar-date-string date
) "\n"
23072 "ISO: " (calendar-iso-date-string date
) "\n"
23073 "Day of Yr: " (calendar-day-of-year-string date
) "\n"
23074 "Julian: " (calendar-julian-date-string date
) "\n"
23075 "Astron. JD: " (calendar-astro-date-string date
)
23076 " (Julian date number at noon UTC)\n"
23077 "Hebrew: " (calendar-hebrew-date-string date
) " (until sunset)\n"
23078 "Islamic: " (calendar-islamic-date-string date
) " (until sunset)\n"
23079 "French: " (calendar-french-date-string date
) "\n"
23080 "Baha'i: " (calendar-bahai-date-string date
) " (until sunset)\n"
23081 "Mayan: " (calendar-mayan-date-string date
) "\n"
23082 "Coptic: " (calendar-coptic-date-string date
) "\n"
23083 "Ethiopic: " (calendar-ethiopic-date-string date
) "\n"
23084 "Persian: " (calendar-persian-date-string date
) "\n"
23085 "Chinese: " (calendar-chinese-date-string date
) "\n"))
23086 (with-output-to-temp-buffer "*Dates*"
23088 (if (fboundp 'fit-window-to-buffer
)
23089 (fit-window-to-buffer (get-buffer-window "*Dates*")))))
23092 ;;;; Embedded LaTeX
23094 (defvar org-cdlatex-mode-map
(make-sparse-keymap)
23095 "Keymap for the minor `org-cdlatex-mode'.")
23097 (org-defkey org-cdlatex-mode-map
"_" 'org-cdlatex-underscore-caret
)
23098 (org-defkey org-cdlatex-mode-map
"^" 'org-cdlatex-underscore-caret
)
23099 (org-defkey org-cdlatex-mode-map
"`" 'cdlatex-math-symbol
)
23100 (org-defkey org-cdlatex-mode-map
"'" 'org-cdlatex-math-modify
)
23101 (org-defkey org-cdlatex-mode-map
"\C-c{" 'cdlatex-environment
)
23103 (defvar org-cdlatex-texmathp-advice-is-done nil
23104 "Flag remembering if we have applied the advice to texmathp already.")
23106 (define-minor-mode org-cdlatex-mode
23107 "Toggle the minor `org-cdlatex-mode'.
23108 This mode supports entering LaTeX environment and math in LaTeX fragments
23110 \\{org-cdlatex-mode-map}"
23112 (when org-cdlatex-mode
(require 'cdlatex
))
23113 (unless org-cdlatex-texmathp-advice-is-done
23114 (setq org-cdlatex-texmathp-advice-is-done t
)
23115 (defadvice texmathp
(around org-math-always-on activate
)
23116 "Always return t in org-mode buffers.
23117 This is because we want to insert math symbols without dollars even outside
23118 the LaTeX math segments. If Orgmode thinks that point is actually inside
23119 en embedded LaTeX fragement, let texmathp do its job.
23120 \\[org-cdlatex-mode-map]"
23124 ((not (org-mode-p)) ad-do-it
)
23125 ((eq this-command
'cdlatex-math-symbol
)
23126 (setq ad-return-value t
23127 texmathp-why
'("cdlatex-math-symbol in org-mode" .
0)))
23129 (let ((p (org-inside-LaTeX-fragment-p)))
23130 (if (and p
(member (car p
) (plist-get org-format-latex-options
:matchers
)))
23131 (setq ad-return-value t
23132 texmathp-why
'("Org-mode embedded math" .
0))
23133 (if p ad-do-it
)))))))))
23135 (defun turn-on-org-cdlatex ()
23136 "Unconditionally turn on `org-cdlatex-mode'."
23137 (org-cdlatex-mode 1))
23139 (defun org-inside-LaTeX-fragment-p ()
23140 "Test if point is inside a LaTeX fragment.
23141 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
23142 sequence appearing also before point.
23143 Even though the matchers for math are configurable, this function assumes
23144 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
23145 delimiters are skipped when they have been removed by customization.
23146 The return value is nil, or a cons cell with the delimiter and
23147 and the position of this delimiter.
23149 This function does a reasonably good job, but can locally be fooled by
23150 for example currency specifications. For example it will assume being in
23151 inline math after \"$22.34\". The LaTeX fragment formatter will only format
23152 fragments that are properly closed, but during editing, we have to live
23153 with the uncertainty caused by missing closing delimiters. This function
23154 looks only before point, not after."
23156 (let ((pos (point))
23157 (dodollar (member "$" (plist-get org-format-latex-options
:matchers
)))
23159 (re-search-backward (concat "^\\(" paragraph-start
"\\)") nil t
)
23161 dd-on str
(start 0) m re
)
23164 (setq str
(concat (buffer-substring lim
(point)) "\000 X$.")
23165 re
(nth 1 (assoc "$" org-latex-regexps
)))
23166 (while (string-match re str start
)
23168 ((= (match-end 0) (length str
))
23169 (throw 'exit
(cons "$" (+ lim
(match-beginning 0) 1))))
23170 ((= (match-end 0) (- (length str
) 5))
23172 (t (setq start
(match-end 0))))))
23173 (when (setq m
(re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t
))
23175 (and (match-beginning 1) (throw 'exit
(cons (match-string 1) m
)))
23176 (and (match-beginning 2) (throw 'exit nil
))
23178 (while (re-search-backward "\\$\\$" lim t
)
23179 (setq dd-on
(not dd-on
)))
23181 (if dd-on
(cons "$$" m
))))))
23184 (defun org-try-cdlatex-tab ()
23185 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
23186 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
23187 - inside a LaTeX fragment, or
23188 - after the first word in a line, where an abbreviation expansion could
23189 insert a LaTeX environment."
23190 (when org-cdlatex-mode
23193 (skip-chars-backward "a-zA-Z0-9*")
23194 (skip-chars-backward " \t")
23197 ((org-inside-LaTeX-fragment-p)
23201 (defun org-cdlatex-underscore-caret (&optional arg
)
23202 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
23203 Revert to the normal definition outside of these fragments."
23205 (if (org-inside-LaTeX-fragment-p)
23206 (call-interactively 'cdlatex-sub-superscript
)
23207 (let (org-cdlatex-mode)
23208 (call-interactively (key-binding (vector last-input-event
))))))
23210 (defun org-cdlatex-math-modify (&optional arg
)
23211 "Execute `cdlatex-math-modify' in LaTeX fragments.
23212 Revert to the normal definition outside of these fragments."
23214 (if (org-inside-LaTeX-fragment-p)
23215 (call-interactively 'cdlatex-math-modify
)
23216 (let (org-cdlatex-mode)
23217 (call-interactively (key-binding (vector last-input-event
))))))
23219 (defvar org-latex-fragment-image-overlays nil
23220 "List of overlays carrying the images of latex fragments.")
23221 (make-variable-buffer-local 'org-latex-fragment-image-overlays
)
23223 (defun org-remove-latex-fragment-image-overlays ()
23224 "Remove all overlays with LaTeX fragment images in current buffer."
23225 (mapc 'org-delete-overlay org-latex-fragment-image-overlays
)
23226 (setq org-latex-fragment-image-overlays nil
))
23228 (defun org-preview-latex-fragment (&optional subtree
)
23229 "Preview the LaTeX fragment at point, or all locally or globally.
23230 If the cursor is in a LaTeX fragment, create the image and overlay
23231 it over the source code. If there is no fragment at point, display
23232 all fragments in the current text, from one headline to the next. With
23233 prefix SUBTREE, display all fragments in the current subtree. With a
23234 double prefix `C-u C-u', or when the cursor is before the first headline,
23235 display all fragments in the buffer.
23236 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
23238 (org-remove-latex-fragment-image-overlays)
23241 (let (beg end at msg
)
23243 ((or (equal subtree
'(16))
23244 (not (save-excursion
23245 (re-search-backward (concat "^" outline-regexp
) nil t
))))
23246 (setq beg
(point-min) end
(point-max)
23247 msg
"Creating images for buffer...%s"))
23248 ((equal subtree
'(4))
23249 (org-back-to-heading)
23250 (setq beg
(point) end
(org-end-of-subtree t
)
23251 msg
"Creating images for subtree...%s"))
23253 (if (setq at
(org-inside-LaTeX-fragment-p))
23254 (goto-char (max (point-min) (- (cdr at
) 2)))
23255 (org-back-to-heading))
23256 (setq beg
(point) end
(progn (outline-next-heading) (point))
23257 msg
(if at
"Creating image...%s"
23258 "Creating images for entry...%s"))))
23260 (narrow-to-region beg end
)
23263 (concat "ltxpng/" (file-name-sans-extension
23264 (file-name-nondirectory
23265 buffer-file-name
)))
23266 default-directory
'overlays msg at
'forbuffer
)
23267 (message msg
"done. Use `C-c C-c' to remove images.")))))
23269 (defvar org-latex-regexps
23270 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t
)
23271 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
23272 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
23273 ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil
)
23274 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil
)
23275 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t
)
23276 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t
))
23277 "Regular expressions for matching embedded LaTeX.")
23279 (defun org-format-latex (prefix &optional dir overlays msg at forbuffer
)
23280 "Replace LaTeX fragments with links to an image, and produce images."
23281 (if (and overlays
(fboundp 'clear-image-cache
)) (clear-image-cache))
23282 (let* ((prefixnodir (file-name-nondirectory prefix
))
23283 (absprefix (expand-file-name prefix dir
))
23284 (todir (file-name-directory absprefix
))
23285 (opt org-format-latex-options
)
23286 (matchers (plist-get opt
:matchers
))
23287 (re-list org-latex-regexps
)
23288 (cnt 0) txt link beg end re e checkdir
23289 m n block linkfile movefile ov
)
23290 ;; Check if there are old images files with this prefix, and remove them
23291 (when (file-directory-p todir
)
23295 (concat (regexp-quote prefixnodir
) "_[0-9]+\\.png$"))))
23296 ;; Check the different regular expressions
23297 (while (setq e
(pop re-list
))
23298 (setq m
(car e
) re
(nth 1 e
) n
(nth 2 e
)
23299 block
(if (nth 3 e
) "\n\n" ""))
23300 (when (member m matchers
)
23301 (goto-char (point-min))
23302 (while (re-search-forward re nil t
)
23303 (when (or (not at
) (equal (cdr at
) (match-beginning n
)))
23304 (setq txt
(match-string n
)
23305 beg
(match-beginning n
) end
(match-end n
)
23307 linkfile
(format "%s_%04d.png" prefix cnt
)
23308 movefile
(format "%s_%04d.png" absprefix cnt
)
23309 link
(concat block
"[[file:" linkfile
"]]" block
))
23310 (if msg
(message msg cnt
))
23312 (unless checkdir
; make sure the directory exists
23314 (or (file-directory-p todir
) (make-directory todir
)))
23315 (org-create-formula-image
23316 txt movefile opt forbuffer
)
23319 (setq ov
(org-make-overlay beg end
))
23320 (if (featurep 'xemacs
)
23322 (org-overlay-put ov
'invisible t
)
23325 (make-glyph (vector 'png
:file movefile
))))
23328 (list 'image
:type
'png
:file movefile
:ascent
'center
)))
23329 (push ov org-latex-fragment-image-overlays
)
23331 (delete-region beg end
)
23332 (insert link
))))))))
23334 ;; This function borrows from Ganesh Swami's latex2png.el
23335 (defun org-create-formula-image (string tofile options buffer
)
23336 (let* ((tmpdir (if (featurep 'xemacs
)
23338 temporary-file-directory
))
23339 (texfilebase (make-temp-name
23340 (expand-file-name "orgtex" tmpdir
)))
23341 (texfile (concat texfilebase
".tex"))
23342 (dvifile (concat texfilebase
".dvi"))
23343 (pngfile (concat texfilebase
".png"))
23344 (fnh (face-attribute 'default
:height nil
))
23345 (scale (or (plist-get options
(if buffer
:scale
:html-scale
)) 1.0))
23346 (dpi (number-to-string (* scale
(floor (* 0.9 (if buffer fnh
140.
))))))
23347 (fg (or (plist-get options
(if buffer
:foreground
:html-foreground
))
23349 (bg (or (plist-get options
(if buffer
:background
:html-background
))
23351 (if (eq fg
'default
) (setq fg
(org-dvipng-color :foreground
)))
23352 (if (eq bg
'default
) (setq bg
(org-dvipng-color :background
)))
23353 (with-temp-file texfile
23354 (insert org-format-latex-header
23355 "\n\\begin{document}\n" string
"\n\\end{document}\n"))
23356 (let ((dir default-directory
))
23357 (condition-case nil
23360 (call-process "latex" nil nil nil texfile
))
23363 (if (not (file-exists-p dvifile
))
23364 (progn (message "Failed to create dvi file from %s" texfile
) nil
)
23365 (call-process "dvipng" nil nil nil
23366 "-E" "-fg" fg
"-bg" bg
23368 ;;"-x" scale "-y" scale
23372 (if (not (file-exists-p pngfile
))
23373 (progn (message "Failed to create png file from %s" texfile
) nil
)
23374 ;; Use the requested file name and clean up
23375 (copy-file pngfile tofile
'replace
)
23376 (loop for e in
'(".dvi" ".tex" ".aux" ".log" ".png") do
23377 (delete-file (concat texfilebase e
)))
23380 (defun org-dvipng-color (attr)
23381 "Return an rgb color specification for dvipng."
23382 (apply 'format
"rgb %s %s %s"
23383 (mapcar 'org-normalize-color
23384 (color-values (face-attribute 'default attr nil
)))))
23386 (defun org-normalize-color (value)
23387 "Return string to be used as color value for an RGB component."
23388 (format "%g" (/ value
65535.0)))
23392 ;;; Variables, constants, and parameter plists
23394 (defconst org-level-max
20)
23396 (defvar org-export-html-preamble nil
23397 "Preamble, to be inserted just after <body>. Set by publishing functions.")
23398 (defvar org-export-html-postamble nil
23399 "Preamble, to be inserted just before </body>. Set by publishing functions.")
23400 (defvar org-export-html-auto-preamble t
23401 "Should default preamble be inserted? Set by publishing functions.")
23402 (defvar org-export-html-auto-postamble t
23403 "Should default postamble be inserted? Set by publishing functions.")
23404 (defvar org-current-export-file nil
) ; dynamically scoped parameter
23405 (defvar org-current-export-dir nil
) ; dynamically scoped parameter
23408 (defconst org-export-plist-vars
23409 '((:language . org-export-default-language
)
23410 (:customtime . org-display-custom-times
)
23411 (:headline-levels . org-export-headline-levels
)
23412 (:section-numbers . org-export-with-section-numbers
)
23413 (:table-of-contents . org-export-with-toc
)
23414 (:preserve-breaks . org-export-preserve-breaks
)
23415 (:archived-trees . org-export-with-archived-trees
)
23416 (:emphasize . org-export-with-emphasize
)
23417 (:sub-superscript . org-export-with-sub-superscripts
)
23418 (:special-strings . org-export-with-special-strings
)
23419 (:footnotes . org-export-with-footnotes
)
23420 (:drawers . org-export-with-drawers
)
23421 (:tags . org-export-with-tags
)
23422 (:TeX-macros . org-export-with-TeX-macros
)
23423 (:LaTeX-fragments . org-export-with-LaTeX-fragments
)
23424 (:skip-before-1st-heading . org-export-skip-text-before-1st-heading
)
23425 (:fixed-width . org-export-with-fixed-width
)
23426 (:timestamps . org-export-with-timestamps
)
23427 (:author-info . org-export-author-info
)
23428 (:time-stamp-file . org-export-time-stamp-file
)
23429 (:tables . org-export-with-tables
)
23430 (:table-auto-headline . org-export-highlight-first-table-line
)
23431 (:style . org-export-html-style
)
23432 (:agenda-style . org-agenda-export-html-style
)
23433 (:convert-org-links . org-export-html-link-org-files-as-html
)
23434 (:inline-images . org-export-html-inline-images
)
23435 (:html-extension . org-export-html-extension
)
23436 (:html-table-tag . org-export-html-table-tag
)
23437 (:expand-quoted-html . org-export-html-expand
)
23438 (:timestamp . org-export-html-with-timestamp
)
23439 (:publishing-directory . org-export-publishing-directory
)
23440 (:preamble . org-export-html-preamble
)
23441 (:postamble . org-export-html-postamble
)
23442 (:auto-preamble . org-export-html-auto-preamble
)
23443 (:auto-postamble . org-export-html-auto-postamble
)
23444 (:author . user-full-name
)
23445 (:email . user-mail-address
)))
23447 (defun org-default-export-plist ()
23448 "Return the property list with default settings for the export variables."
23449 (let ((l org-export-plist-vars
) rtn e
)
23450 (while (setq e
(pop l
))
23451 (setq rtn
(cons (car e
) (cons (symbol-value (cdr e
)) rtn
))))
23454 (defun org-infile-export-plist ()
23455 "Return the property list with file-local settings for export."
23460 (let ((re (org-make-options-regexp
23461 '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
23462 p key val text options
)
23463 (while (re-search-forward re nil t
)
23464 (setq key
(org-match-string-no-properties 1)
23465 val
(org-match-string-no-properties 2))
23467 ((string-equal key
"TITLE") (setq p
(plist-put p
:title val
)))
23468 ((string-equal key
"AUTHOR")(setq p
(plist-put p
:author val
)))
23469 ((string-equal key
"EMAIL") (setq p
(plist-put p
:email val
)))
23470 ((string-equal key
"DATE") (setq p
(plist-put p
:date val
)))
23471 ((string-equal key
"LANGUAGE") (setq p
(plist-put p
:language val
)))
23472 ((string-equal key
"TEXT")
23473 (setq text
(if text
(concat text
"\n" val
) val
)))
23474 ((string-equal key
"OPTIONS") (setq options val
))))
23475 (setq p
(plist-put p
:text text
))
23477 (let ((op '(("H" .
:headline-levels
)
23478 ("num" .
:section-numbers
)
23479 ("toc" .
:table-of-contents
)
23480 ("\\n" .
:preserve-breaks
)
23481 ("@" .
:expand-quoted-html
)
23482 (":" .
:fixed-width
)
23484 ("^" .
:sub-superscript
)
23485 ("-" .
:special-strings
)
23490 ("TeX" .
:TeX-macros
)
23491 ("LaTeX" .
:LaTeX-fragments
)
23492 ("skip" .
:skip-before-1st-heading
)
23493 ("author" .
:author-info
)
23494 ("timestamp" .
:time-stamp-file
)))
23496 (while (setq o
(pop op
))
23497 (if (string-match (concat (regexp-quote (car o
))
23498 ":\\([^ \t\n\r;,.]*\\)")
23500 (setq p
(plist-put p
(cdr o
)
23501 (car (read-from-string
23502 (match-string 1 options
)))))))))
23505 (defun org-export-directory (type plist
)
23506 (let* ((val (plist-get plist
:publishing-directory
))
23507 (dir (if (listp val
)
23508 (or (cdr (assoc type val
)) ".")
23512 (defun org-skip-comments (lines)
23513 "Skip lines starting with \"#\" and subtrees starting with COMMENT."
23514 (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string
))
23515 (re2 "^\\(\\*+\\)[ \t\n\r]")
23516 (case-fold-search nil
)
23518 (while (setq line
(pop lines
))
23520 ((and (string-match re1 line
)
23521 (setq level
(- (match-end 1) (match-beginning 1))))
23522 ;; Beginning of a COMMENT subtree. Skip it.
23523 (while (and (setq line
(pop lines
))
23524 (or (not (string-match re2 line
))
23525 (> (- (match-end 1) (match-beginning 1)) level
))))
23526 (setq lines
(cons line lines
)))
23527 ((string-match "^#" line
)
23528 ;; an ordinary comment line
23530 ((and org-export-table-remove-special-lines
23531 (string-match "^[ \t]*|" line
)
23532 (or (string-match "^[ \t]*| *[!_^] *|" line
)
23533 (and (string-match "| *<[0-9]+> *|" line
)
23534 (not (string-match "| *[^ <|]" line
)))))
23535 ;; a special table line that should be removed
23537 (t (setq rtn
(cons line rtn
)))))
23540 (defun org-export (&optional arg
)
23542 (let ((help "[t] insert the export option template
23543 \[v] limit export to visible part of outline tree
23545 \[a] export as ASCII
23547 \[h] export as HTML
23548 \[H] export as HTML to temporary buffer
23549 \[R] export region as HTML
23550 \[b] export as HTML and browse immediately
23551 \[x] export as XOXO
23553 \[l] export as LaTeX
23554 \[L] export as LaTeX to temporary buffer
23556 \[i] export current file as iCalendar file
23557 \[I] export all agenda files as iCalendar files
23558 \[c] export agenda files into combined iCalendar file
23560 \[F] publish current file
23561 \[P] publish current project
23562 \[X] publish... (project will be prompted for)
23563 \[A] publish all projects")
23565 '((?t . org-insert-export-options-template
)
23566 (?v . org-export-visible
)
23567 (?a . org-export-as-ascii
)
23568 (?h . org-export-as-html
)
23569 (?b . org-export-as-html-and-open
)
23570 (?H . org-export-as-html-to-buffer
)
23571 (?R . org-export-region-as-html
)
23572 (?x . org-export-as-xoxo
)
23573 (?l . org-export-as-latex
)
23574 (?L . org-export-as-latex-to-buffer
)
23575 (?i . org-export-icalendar-this-file
)
23576 (?I . org-export-icalendar-all-agenda-files
)
23577 (?c . org-export-icalendar-combine-agenda-files
)
23578 (?F . org-publish-current-file
)
23579 (?P . org-publish-current-project
)
23581 (?A . org-publish-all
)))
23583 (save-window-excursion
23584 (delete-other-windows)
23585 (with-output-to-temp-buffer "*Org Export/Publishing Help*"
23587 (message "Select command: ")
23588 (setq r1
(read-char-exclusive)))
23589 (setq r2
(if (< r1
27) (+ r1
96) r1
))
23590 (if (setq ass
(assq r2 cmds
))
23591 (call-interactively (cdr ass
))
23592 (error "No command associated with key %c" r1
))))
23594 (defconst org-html-entities
23602 ("vert" .
"|")
23635 ("Aring") ("AA".
"Å")
23724 ("varepsilon".
"ε")
23737 ("sigmaf") ("varsigma".
"ς")
23745 ("thetasym") ("vartheta".
"ϑ")
23748 ("bull") ("bullet".
"•")
23749 ("hellip") ("dots".
"…")
23759 ("larr") ("leftarrow".
"←") ("gets".
"←")
23760 ("uarr") ("uparrow".
"↑")
23761 ("rarr") ("to".
"→") ("rightarrow".
"→")
23762 ("darr")("downarrow".
"↓")
23763 ("harr") ("leftrightarrow".
"↔")
23764 ("crarr") ("hookleftarrow".
"↵") ; has round hook, not quite CR
23765 ("lArr") ("Leftarrow".
"⇐")
23766 ("uArr") ("Uparrow".
"⇑")
23767 ("rArr") ("Rightarrow".
"⇒")
23768 ("dArr") ("Downarrow".
"⇓")
23769 ("hArr") ("Leftrightarrow".
"⇔")
23771 ("part") ("partial".
"∂")
23772 ("exist") ("exists".
"∃")
23773 ("empty") ("emptyset".
"∅")
23775 ("isin") ("in".
"∈")
23781 ("lowast") ("ast".
"∗")
23783 ("prop") ("proptp".
"∝")
23784 ("infin") ("infty".
"∞")
23785 ("ang") ("angle".
"∠")
23786 ("and") ("wedge".
"∧")
23787 ("or") ("vee".
"∨")
23793 ("cong") ("simeq".
"≅")
23794 ("asymp")("approx".
"≈")
23795 ("ne") ("neq".
"≠")
23799 ("sub") ("subset".
"⊂")
23800 ("sup") ("supset".
"⊃")
23807 ("sdot") ("cdot".
"⋅")
23814 ("loz") ("Diamond".
"◊")
23815 ("spades") ("spadesuit".
"♠")
23816 ("clubs") ("clubsuit".
"♣")
23817 ("hearts") ("diamondsuit".
"♥")
23818 ("diams") ("diamondsuit".
"♦")
23819 ("smile".
"☺") ("blacksmile".
"☻") ("sad".
"☹")
23853 ("arccos".
"arccos")
23854 ("arcsin".
"arcsin")
23855 ("arctan".
"arctan")
23872 ("liminf".
"liminf")
23873 ("limsup".
"limsup")
23886 "Entities for TeX->HTML translation.
23887 Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
23888 \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
23889 In that case, \"\\ent\" will be translated to \"&other;\".
23890 The list contains HTML entities for Latin-1, Greek and other symbols.
23891 It is supplemented by a number of commonly used TeX macros with appropriate
23892 translations. There is currently no way for users to extend this.")
23894 ;;; General functions for all backends
23896 (defun org-cleaned-string-for-export (string &rest parameters
)
23897 "Cleanup a buffer STRING so that links can be created safely."
23899 (let* ((re-radio (and org-target-link-regexp
23900 (concat "\\([^<]\\)\\(" org-target-link-regexp
"\\)")))
23901 (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re
))
23902 (re-angle-link (concat "\\([^[]\\)" org-angle-link-re
))
23903 (re-archive (concat ":" org-archive-tag
":"))
23904 (re-quote (concat "^\\*+[ \t]+" org-quote-string
"\\>"))
23905 (re-commented (concat "^\\*+[ \t]+" org-comment-string
"\\>"))
23906 (htmlp (plist-get parameters
:for-html
))
23907 (asciip (plist-get parameters
:for-ascii
))
23908 (latexp (plist-get parameters
:for-LaTeX
))
23909 (commentsp (plist-get parameters
:comments
))
23910 (archived-trees (plist-get parameters
:archived-trees
))
23911 (inhibit-read-only t
)
23912 (drawers org-drawers
)
23913 (exp-drawers (plist-get parameters
:drawers
))
23914 (outline-regexp "\\*+ ")
23917 (with-current-buffer (get-buffer-create " org-mode-tmp")
23920 ;; Remove license-to-kill stuff
23921 (while (setq p
(text-property-any (point-min) (point-max)
23922 :org-license-to-kill t
))
23923 (delete-region p
(next-single-property-change p
:org-license-to-kill
)))
23925 (let ((org-inhibit-startup t
)) (org-mode))
23926 (untabify (point-min) (point-max))
23928 ;; Get rid of drawers
23929 (unless (eq t exp-drawers
)
23930 (goto-char (point-min))
23931 (let ((re (concat "^[ \t]*:\\("
23934 (org-delete-all exp-drawers
23935 (copy-sequence drawers
))
23937 "\\):[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n")))
23938 (while (re-search-forward re nil t
)
23939 (replace-match ""))))
23941 ;; Get the correct stuff before the first headline
23942 (when (plist-get parameters
:skip-before-1st-heading
)
23943 (goto-char (point-min))
23944 (when (re-search-forward "^\\*+[ \t]" nil t
)
23945 (delete-region (point-min) (match-beginning 0))
23946 (goto-char (point-min))
23948 (when (plist-get parameters
:add-text
)
23949 (goto-char (point-min))
23950 (insert (plist-get parameters
:add-text
) "\n"))
23952 ;; Get rid of archived trees
23953 (when (not (eq archived-trees t
))
23954 (goto-char (point-min))
23955 (while (re-search-forward re-archive nil t
)
23956 (if (not (org-on-heading-p t
))
23957 (org-end-of-subtree t
)
23958 (beginning-of-line 1)
23959 (setq a
(if archived-trees
23960 (1+ (point-at-eol)) (point))
23961 b
(org-end-of-subtree t
))
23962 (if (> b a
) (delete-region a b
)))))
23964 ;; Find targets in comments and move them out of comments,
23965 ;; but mark them as targets that should be invisible
23966 (goto-char (point-min))
23967 (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t
)
23968 (replace-match "\\1(INVISIBLE)"))
23970 ;; Protect backend specific stuff, throw away the others.
23972 `((,htmlp
"HTML" "BEGIN_HTML" "END_HTML")
23973 (,asciip
"ASCII" "BEGIN_ASCII" "END_ASCII")
23974 (,latexp
"LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
23976 (goto-char (point-min))
23977 (while (re-search-forward "^#\\+BEGIN_EXAMPLE[ \t]*\n" nil t
)
23978 (goto-char (match-end 0))
23979 (while (not (looking-at "#\\+END_EXAMPLE"))
23981 (beginning-of-line 2)))
23982 (goto-char (point-min))
23983 (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t
)
23984 (add-text-properties (match-beginning 0) (match-end 0)
23985 '(org-protected t
)))
23987 (setq fmt
(pop formatters
))
23989 (goto-char (point-min))
23990 (while (re-search-forward (concat "^#\\+" (cadr fmt
)
23991 ":[ \t]*\\(.*\\)") nil t
)
23992 (replace-match "\\1" t
)
23993 (add-text-properties
23994 (point-at-bol) (min (1+ (point-at-eol)) (point-max))
23995 '(org-protected t
))))
23996 (goto-char (point-min))
23997 (while (re-search-forward
23999 (caddr fmt
) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
24000 (cadddr fmt
) "\\>.*\n?") nil t
)
24002 (add-text-properties (match-beginning 1) (1+ (match-end 1))
24003 '(org-protected t
))
24004 (delete-region (match-beginning 0) (match-end 0))))))
24006 ;; Protect quoted subtrees
24007 (goto-char (point-min))
24008 (while (re-search-forward re-quote nil t
)
24009 (goto-char (match-beginning 0))
24011 (add-text-properties (point) (org-end-of-subtree t
)
24012 '(org-protected t
)))
24014 ;; Protect verbatim elements
24015 (goto-char (point-min))
24016 (while (re-search-forward org-verbatim-re nil t
)
24017 (add-text-properties (match-beginning 4) (match-end 4)
24018 '(org-protected t
))
24019 (goto-char (1+ (match-end 4))))
24021 ;; Remove subtrees that are commented
24022 (goto-char (point-min))
24023 (while (re-search-forward re-commented nil t
)
24024 (goto-char (match-beginning 0))
24025 (delete-region (point) (org-end-of-subtree t
)))
24027 ;; Remove special table lines
24028 (when org-export-table-remove-special-lines
24029 (goto-char (point-min))
24030 (while (re-search-forward "^[ \t]*|" nil t
)
24031 (beginning-of-line 1)
24032 (if (or (looking-at "[ \t]*| *[!_^] *|")
24033 (and (looking-at ".*?| *<[0-9]+> *|")
24034 (not (looking-at ".*?| *[^ <|]"))))
24035 (delete-region (max (point-min) (1- (point-at-bol)))
24039 ;; Specific LaTeX stuff
24041 (require 'org-export-latex nil
)
24042 (org-export-latex-cleaned-string))
24045 (org-export-ascii-clean-string))
24047 ;; Specific HTML stuff
24049 ;; Convert LaTeX fragments to images
24050 (when (plist-get parameters
:LaTeX-fragments
)
24052 (concat "ltxpng/" (file-name-sans-extension
24053 (file-name-nondirectory
24054 org-current-export-file
)))
24055 org-current-export-dir nil
"Creating LaTeX image %s"))
24056 (message "Exporting..."))
24058 ;; Remove or replace comments
24059 (goto-char (point-min))
24060 (while (re-search-forward "^#\\(.*\n?\\)" nil t
)
24062 (progn (add-text-properties
24063 (match-beginning 0) (match-end 0) '(org-protected t
))
24064 (replace-match (format commentsp
(match-string 1)) t t
))
24065 (replace-match "")))
24067 ;; Find matches for radio targets and turn them into internal links
24068 (goto-char (point-min))
24070 (while (re-search-forward re-radio nil t
)
24071 (org-if-unprotected
24072 (replace-match "\\1[[\\2]]"))))
24074 ;; Find all links that contain a newline and put them into a single line
24075 (goto-char (point-min))
24076 (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t
)
24077 (org-if-unprotected
24078 (replace-match "\\1 \\3")
24079 (goto-char (match-beginning 0))))
24082 ;; Normalize links: Convert angle and plain links into bracket links
24083 ;; Expand link abbreviations
24084 (goto-char (point-min))
24085 (while (re-search-forward re-plain-link nil t
)
24086 (goto-char (1- (match-end 0)))
24087 (org-if-unprotected
24088 (let* ((s (concat (match-string 1) "[[" (match-string 2)
24089 ":" (match-string 3) "]]")))
24090 ;; added 'org-link face to links
24091 (put-text-property 0 (length s
) 'face
'org-link s
)
24092 (replace-match s t t
))))
24093 (goto-char (point-min))
24094 (while (re-search-forward re-angle-link nil t
)
24095 (goto-char (1- (match-end 0)))
24096 (org-if-unprotected
24097 (let* ((s (concat (match-string 1) "[[" (match-string 2)
24098 ":" (match-string 3) "]]")))
24099 (put-text-property 0 (length s
) 'face
'org-link s
)
24100 (replace-match s t t
))))
24101 (goto-char (point-min))
24102 (while (re-search-forward org-bracket-link-regexp nil t
)
24103 (org-if-unprotected
24104 (let* ((s (concat "[[" (setq xx
(save-match-data
24105 (org-link-expand-abbrev (match-string 1))))
24109 (concat "[" xx
"]"))
24111 (put-text-property 0 (length s
) 'face
'org-link s
)
24112 (replace-match s t t
))))
24114 ;; Find multiline emphasis and put them into single line
24115 (when (plist-get parameters
:emph-multiline
)
24116 (goto-char (point-min))
24117 (while (re-search-forward org-emph-re nil t
)
24118 (if (not (= (char-after (match-beginning 3))
24119 (char-after (match-beginning 4))))
24120 (org-if-unprotected
24121 (subst-char-in-region (match-beginning 0) (match-end 0)
24123 (goto-char (1- (match-end 0))))
24124 (goto-char (1+ (match-beginning 0))))))
24126 (setq rtn
(buffer-string)))
24127 (kill-buffer " org-mode-tmp")
24130 (defun org-export-grab-title-from-buffer ()
24131 "Get a title for the current document, from looking at the buffer."
24132 (let ((inhibit-read-only t
))
24134 (goto-char (point-min))
24135 (let ((end (save-excursion (outline-next-heading) (point))))
24136 (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t
)
24137 ;; Mark the line so that it will not be exported as normal text.
24139 (add-text-properties (match-beginning 0) (match-end 0)
24140 (list :org-license-to-kill t
)))
24141 ;; Return the title string
24142 (org-trim (match-string 0)))))))
24144 (defun org-export-get-title-from-subtree ()
24145 "Return subtree title and exclude it from export."
24146 (let (title (m (mark)))
24148 (goto-char (region-beginning))
24149 (when (and (org-at-heading-p)
24150 (>= (org-end-of-subtree t t
) (region-end)))
24151 ;; This is a subtree, we take the title from the first heading
24152 (goto-char (region-beginning))
24153 (looking-at org-todo-line-regexp
)
24154 (setq title
(match-string 3))
24156 (add-text-properties (point) (1+ (point-at-eol))
24157 (list :org-license-to-kill t
)))))
24160 (defun org-solidify-link-text (s &optional alist
)
24161 "Take link text and make a safe target out of it."
24166 (org-split-string s
"[ \t\r\n]+") "--"))
24167 (a (assoc rtn alist
)))
24168 (or (cdr a
) rtn
))))
24170 (defun org-get-min-level (lines)
24171 "Get the minimum level in LINES."
24172 (let ((re "^\\(\\*+\\) ") l min
)
24174 (while (setq l
(pop lines
))
24175 (if (string-match re l
)
24176 (throw 'exit
(org-tr-level (length (match-string 1 l
))))))
24179 ;; Variable holding the vector with section numbers
24180 (defvar org-section-numbers
(make-vector org-level-max
0))
24182 (defun org-init-section-numbers ()
24183 "Initialize the vector for the section numbers."
24185 (numbers (nreverse (org-split-string "" "\\.")))
24186 (depth (1- (length org-section-numbers
)))
24187 (i depth
) number-string
)
24190 (aset org-section-numbers i
0)
24191 (setq number-string
(or (car numbers
) "0"))
24192 (if (string-match "\\`[A-Z]\\'" number-string
)
24193 (aset org-section-numbers i
24194 (- (string-to-char number-string
) ?A -
1))
24195 (aset org-section-numbers i
(string-to-number number-string
)))
24199 (defun org-section-number (&optional level
)
24200 "Return a string with the current section number.
24201 When LEVEL is non-nil, increase section numbers on that level."
24202 (let* ((depth (1- (length org-section-numbers
))) idx n
(string ""))
24205 (aset org-section-numbers
24206 level
(1+ (aref org-section-numbers level
))))
24207 (setq idx
(1+ level
))
24208 (while (<= idx depth
)
24209 (if (not (= idx
1))
24210 (aset org-section-numbers idx
0))
24211 (setq idx
(1+ idx
))))
24213 (while (<= idx depth
)
24214 (setq n
(aref org-section-numbers idx
))
24215 (setq string
(concat string
(if (not (string= string
"")) "." "")
24216 (int-to-string n
)))
24217 (setq idx
(1+ idx
)))
24219 (if (string-match "\\`\\([@0]\\.\\)+" string
)
24220 (setq string
(replace-match "" t nil string
)))
24221 (if (string-match "\\(\\.0\\)+\\'" string
)
24222 (setq string
(replace-match "" t nil string
))))
24227 (defvar org-last-level nil
) ; dynamically scoped variable
24228 (defvar org-min-level nil
) ; dynamically scoped variable
24229 (defvar org-levels-open nil
) ; dynamically scoped parameter
24230 (defvar org-ascii-current-indentation nil
) ; For communication
24232 (defun org-export-as-ascii (arg)
24233 "Export the outline as a pretty ASCII file.
24234 If there is an active region, export only the region.
24235 The prefix ARG specifies how many levels of the outline should become
24236 underlined headlines. The default is 3."
24238 (setq-default org-todo-line-regexp org-todo-line-regexp
)
24239 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
24240 (org-infile-export-plist)))
24241 (region-p (org-region-active-p))
24245 (goto-char (region-beginning))
24246 (and (org-at-heading-p)
24247 (>= (org-end-of-subtree t t
) (region-end))))))
24248 (custom-times org-display-custom-times
)
24249 (org-ascii-current-indentation '(0 .
0))
24253 (case-fold-search nil
)
24254 (filename (concat (file-name-as-directory
24255 (org-export-directory :ascii opt-plist
))
24256 (file-name-sans-extension
24258 (org-entry-get (region-beginning)
24259 "EXPORT_FILE_NAME" t
))
24260 (file-name-nondirectory buffer-file-name
)))
24262 (filename (if (equal (file-truename filename
)
24263 (file-truename buffer-file-name
))
24264 (concat filename
".txt")
24266 (buffer (find-file-noselect filename
))
24267 (org-levels-open (make-vector org-level-max nil
))
24268 (odd org-odd-levels-only
)
24269 (date (plist-get opt-plist
:date
))
24270 (author (plist-get opt-plist
:author
))
24271 (title (or (and subtree-p
(org-export-get-title-from-subtree))
24272 (plist-get opt-plist
:title
)
24274 (plist-get opt-plist
:skip-before-1st-heading
))
24275 (org-export-grab-title-from-buffer))
24276 (file-name-sans-extension
24277 (file-name-nondirectory buffer-file-name
))))
24278 (email (plist-get opt-plist
:email
))
24279 (language (plist-get opt-plist
:language
))
24280 (quote-re0 (concat "^[ \t]*" org-quote-string
"\\>"))
24281 ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
24286 (if (org-region-active-p) (region-beginning) (point-min))
24287 (if (org-region-active-p) (region-end) (point-max))))
24288 (lines (org-split-string
24289 (org-cleaned-string-for-export
24292 :skip-before-1st-heading
24293 (plist-get opt-plist
:skip-before-1st-heading
)
24294 :drawers
(plist-get opt-plist
:drawers
)
24295 :verbatim-multiline t
24297 (plist-get opt-plist
:archived-trees
)
24298 :add-text
(plist-get opt-plist
:text
))
24300 thetoc have-headings first-heading-pos
24301 table-open table-buffer
)
24303 (let ((inhibit-read-only t
))
24305 (remove-text-properties (point-min) (point-max)
24306 '(:org-license-to-kill t
))))
24308 (setq org-min-level
(org-get-min-level lines
))
24309 (setq org-last-level org-min-level
)
24310 (org-init-section-numbers)
24312 (find-file-noselect filename
)
24314 (setq lang-words
(or (assoc language org-export-language-setup
)
24315 (assoc "en" org-export-language-setup
)))
24316 (switch-to-buffer-other-window buffer
)
24319 ;; create local variables for all options, to make sure all called
24320 ;; functions get the correct information
24322 (set (make-local-variable (cdr x
))
24323 (plist-get opt-plist
(car x
))))
24324 org-export-plist-vars
)
24325 (org-set-local 'org-odd-levels-only odd
)
24326 (setq umax
(if arg
(prefix-numeric-value arg
)
24327 org-export-headline-levels
))
24328 (setq umax-toc
(if (integerp org-export-with-toc
)
24329 (min org-export-with-toc umax
)
24333 (if title
(org-insert-centered title ?
=))
24335 (if (and (or author email
)
24336 org-export-author-info
)
24337 (insert (concat (nth 1 lang-words
) ": " (or author
"")
24338 (if email
(concat " <" email
">") "")
24342 ((and date
(string-match "%" date
))
24343 (setq date
(format-time-string date
(current-time))))
24345 (t (setq date
(format-time-string "%Y/%m/%d %X" (current-time)))))
24347 (if (and date org-export-time-stamp-file
)
24348 (insert (concat (nth 2 lang-words
) ": " date
"\n")))
24352 (if org-export-with-toc
24354 (push (concat (nth 3 lang-words
) "\n") thetoc
)
24355 (push (concat (make-string (length (nth 3 lang-words
)) ?
=) "\n") thetoc
)
24356 (mapc '(lambda (line)
24357 (if (string-match org-todo-line-regexp
24359 ;; This is a headline
24361 (setq have-headings t
)
24362 (setq level
(- (match-end 1) (match-beginning 1))
24363 level
(org-tr-level level
)
24364 txt
(match-string 3 line
)
24366 (or (and org-export-mark-todo-in-toc
24367 (match-beginning 2)
24368 (not (member (match-string 2 line
)
24369 org-done-keywords
)))
24371 (and org-export-mark-todo-in-toc
24373 (org-search-todo-below
24374 line lines level
))))
24375 (setq txt
(org-html-expand-for-ascii txt
))
24377 (while (string-match org-bracket-link-regexp txt
)
24380 (match-string (if (match-end 2) 3 1) txt
)
24383 (if (and (memq org-export-with-tags
'(not-in-toc nil
))
24385 (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
24387 (setq txt
(replace-match "" t t txt
)))
24388 (if (string-match quote-re0 txt
)
24389 (setq txt
(replace-match "" t t txt
)))
24391 (if org-export-with-section-numbers
24392 (setq txt
(concat (org-section-number level
)
24394 (if (<= level umax-toc
)
24399 (* (max 0 (- level org-min-level
)) 4) ?\
)
24400 (format (if todo
"%s (*)\n" "%s\n") txt
))
24402 (setq org-last-level level
))
24405 (setq thetoc
(if have-headings
(nreverse thetoc
) nil
))))
24407 (org-init-section-numbers)
24408 (while (setq line
(pop lines
))
24409 ;; Remove the quoted HTML tags.
24410 (setq line
(org-html-expand-for-ascii line
))
24412 (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line
)
24413 (setq line
(replace-match "" t t line
)))
24414 ;; Replace internal links
24415 (while (string-match org-bracket-link-regexp line
)
24416 (setq line
(replace-match
24417 (if (match-end 3) "[\\3]" "[\\1]")
24420 (setq line
(org-translate-time line
)))
24422 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line
)
24424 (setq first-heading-pos
(or first-heading-pos
(point)))
24425 (setq level
(org-tr-level (- (match-end 1) (match-beginning 1)))
24426 txt
(match-string 2 line
))
24427 (org-ascii-level-start level txt umax lines
))
24429 ((and org-export-with-tables
24430 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line
))
24431 (if (not table-open
)
24432 ;; New table starts
24433 (setq table-open t table-buffer nil
))
24434 ;; Accumulate lines
24435 (setq table-buffer
(cons line table-buffer
))
24436 (when (or (not lines
)
24437 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
24439 (setq table-open nil
24440 table-buffer
(nreverse table-buffer
))
24443 (org-fix-indentation x org-ascii-current-indentation
))
24444 (org-format-table-ascii table-buffer
)
24447 (setq line
(org-fix-indentation line org-ascii-current-indentation
))
24448 (if (and org-export-with-fixed-width
24449 (string-match "^\\([ \t]*\\)\\(:\\)" line
))
24450 (setq line
(replace-match "\\1" nil nil line
)))
24451 (insert line
"\n"))))
24455 ;; insert the table of contents
24457 (goto-char (point-min))
24458 (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t
)
24460 (goto-char (match-beginning 0))
24461 (replace-match ""))
24462 (goto-char first-heading-pos
))
24463 (mapc 'insert thetoc
)
24464 (or (looking-at "[ \t]*\n[ \t]*\n")
24467 ;; Convert whitespace place holders
24468 (goto-char (point-min))
24470 (while (setq beg
(next-single-property-change (point) 'org-whitespace
))
24471 (setq end
(next-single-property-change beg
'org-whitespace
))
24473 (delete-region beg end
)
24474 (insert (make-string (- end beg
) ?\
))))
24477 ;; remove display and invisible chars
24479 (goto-char (point-min))
24480 (while (setq beg
(next-single-property-change (point) 'display
))
24481 (setq end
(next-single-property-change beg
'display
))
24482 (delete-region beg end
)
24485 (goto-char (point-min))
24486 (while (setq beg
(next-single-property-change (point) 'org-cwidth
))
24487 (setq end
(next-single-property-change beg
'org-cwidth
))
24488 (delete-region beg end
)
24490 (goto-char (point-min))))
24492 (defun org-export-ascii-clean-string ()
24493 "Do extra work for ASCII export"
24494 (goto-char (point-min))
24495 (while (re-search-forward org-verbatim-re nil t
)
24496 (goto-char (match-end 2))
24497 (backward-delete-char 1) (insert "'")
24498 (goto-char (match-beginning 2))
24499 (delete-char 1) (insert "`")
24500 (goto-char (match-end 2))))
24502 (defun org-search-todo-below (line lines level
)
24503 "Search the subtree below LINE for any TODO entries."
24504 (let ((rest (cdr (memq line lines
)))
24505 (re org-todo-line-regexp
)
24508 (while (setq line
(pop rest
))
24509 (if (string-match re line
)
24511 (setq lv
(- (match-end 1) (match-beginning 1))
24512 todo
(and (match-beginning 2)
24513 (not (member (match-string 2 line
)
24514 org-done-keywords
))))
24516 (if (<= lv level
) (throw 'exit nil
))
24517 (if todo
(throw 'exit t
))))))))
24519 (defun org-html-expand-for-ascii (line)
24520 "Handle quoted HTML for ASCII export."
24521 (if org-export-html-expand
24522 (while (string-match "@<[^<>\n]*>" line
)
24523 ;; We just remove the tags for now.
24524 (setq line
(replace-match "" nil nil line
))))
24527 (defun org-insert-centered (s &optional underline
)
24528 "Insert the string S centered and underline it with character UNDERLINE."
24529 (let ((ind (max (/ (- 80 (string-width s
)) 2) 0)))
24530 (insert (make-string ind ?\
) s
"\n")
24532 (insert (make-string ind ?\
)
24533 (make-string (string-width s
) underline
)
24536 (defun org-ascii-level-start (level title umax
&optional lines
)
24537 "Insert a new level in ASCII export."
24538 (let (char (n (- level umax
1)) (ind 0))
24541 (insert (make-string (* 2 n
) ?\
)
24542 (char-to-string (nth (% n
(length org-export-ascii-bullets
))
24543 org-export-ascii-bullets
))
24545 ;; find the indentation of the next non-empty line
24548 (if (string-match "^\\* " (car lines
)) (throw 'stop nil
))
24549 (if (string-match "^\\([ \t]*\\)\\S-" (car lines
))
24550 (throw 'stop
(setq ind
(org-get-indentation (car lines
)))))
24552 (setq org-ascii-current-indentation
(cons (* 2 (1+ n
)) ind
)))
24553 (if (or (not (equal (char-before) ?
\n))
24554 (not (equal (char-before (1- (point))) ?
\n)))
24556 (setq char
(nth (- umax level
) (reverse org-export-ascii-underline
)))
24557 (unless org-export-with-tags
24558 (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title
)
24559 (setq title
(replace-match "" t t title
))))
24560 (if org-export-with-section-numbers
24561 (setq title
(concat (org-section-number level
) " " title
)))
24562 (insert title
"\n" (make-string (string-width title
) char
) "\n")
24563 (setq org-ascii-current-indentation
'(0 .
0)))))
24565 (defun org-export-visible (type arg
)
24566 "Create a copy of the visible part of the current buffer, and export it.
24567 The copy is created in a temporary buffer and removed after use.
24568 TYPE is the final key (as a string) that also select the export command in
24569 the `C-c C-e' export dispatcher.
24570 As a special case, if the you type SPC at the prompt, the temporary
24571 org-mode file will not be removed but presented to you so that you can
24572 continue to use it. The prefix arg ARG is passed through to the exporting
24576 (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
24577 (read-char-exclusive))
24578 current-prefix-arg
))
24579 (if (not (member type
'(?a ?\C-a ?b ?\C-b ?h ?x ?\
)))
24580 (error "Invalid export key"))
24581 (let* ((binding (cdr (assoc type
24582 '((?a . org-export-as-ascii
)
24583 (?\C-a . org-export-as-ascii
)
24584 (?b . org-export-as-html-and-open
)
24585 (?\C-b . org-export-as-html-and-open
)
24586 (?h . org-export-as-html
)
24587 (?H . org-export-as-html-to-buffer
)
24588 (?R . org-export-region-as-html
)
24589 (?x . org-export-as-xoxo
)))))
24590 (keepp (equal type ?\
))
24591 (file buffer-file-name
)
24592 (buffer (get-buffer-create "*Org Export Visible*"))
24594 ;; Need to hack the drawers here.
24596 (goto-char (point-min))
24597 (while (re-search-forward org-drawer-regexp nil t
)
24598 (goto-char (match-beginning 1))
24599 (or (org-invisible-p) (org-flag-drawer nil
))))
24600 (with-current-buffer buffer
(erase-buffer))
24602 (setq s
(goto-char (point-min)))
24603 (while (not (= (point) (point-max)))
24604 (goto-char (org-find-invisible))
24605 (append-to-buffer buffer s
(point))
24606 (setq s
(goto-char (org-find-visible))))
24607 (org-cycle-hide-drawers 'all
)
24608 (goto-char (point-min))
24610 ;; Copy all comment lines to the end, to make sure #+ settings are
24611 ;; still available for the second export step. Kind of a hack, but
24612 ;; does do the trick.
24613 (if (looking-at "#[^\r\n]*")
24614 (append-to-buffer buffer
(match-beginning 0) (1+ (match-end 0))))
24615 (while (re-search-forward "[\n\r]#[^\n\r]*" nil t
)
24616 (append-to-buffer buffer
(1+ (match-beginning 0))
24617 (min (point-max) (1+ (match-end 0))))))
24618 (set-buffer buffer
)
24619 (let ((buffer-file-name file
)
24620 (org-inhibit-startup t
))
24623 (unless keepp
(funcall binding arg
))))
24625 (kill-buffer buffer
)
24626 (switch-to-buffer-other-window buffer
)
24627 (goto-char (point-min)))))
24629 (defun org-find-visible ()
24631 (while (and (not (= (point-max) (setq s
(next-overlay-change s
))))
24632 (get-char-property s
'invisible
)))
24634 (defun org-find-invisible ()
24636 (while (and (not (= (point-max) (setq s
(next-overlay-change s
))))
24637 (not (get-char-property s
'invisible
))))
24642 (defun org-get-current-options ()
24643 "Return a string with current options as keyword options.
24644 Does include HTML export options as well as TODO and CATEGORY stuff."
24650 #+TEXT: Some descriptive text to be emitted. Several lines OK.
24651 #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s -:%s f:%s *:%s TeX:%s LaTeX:%s skip:%s d:%s tags:%s
24655 #+PRIORITIES: %c %c %c
24657 #+STARTUP: %s %s %s %s %s
24662 (buffer-name) (user-full-name) user-mail-address org-export-default-language
24663 org-export-headline-levels
24664 org-export-with-section-numbers
24665 org-export-with-toc
24666 org-export-preserve-breaks
24667 org-export-html-expand
24668 org-export-with-fixed-width
24669 org-export-with-tables
24670 org-export-with-sub-superscripts
24671 org-export-with-special-strings
24672 org-export-with-footnotes
24673 org-export-with-emphasize
24674 org-export-with-TeX-macros
24675 org-export-with-LaTeX-fragments
24676 org-export-skip-text-before-1st-heading
24677 org-export-with-drawers
24678 org-export-with-tags
24679 (file-name-nondirectory buffer-file-name
)
24680 "TODO FEEDBACK VERIFY DONE"
24681 "Me Jason Marie DONE"
24682 org-highest-priority org-lowest-priority org-default-priority
24683 (mapconcat 'identity org-drawers
" ")
24684 (cdr (assoc org-startup-folded
24685 '((nil .
"showall") (t .
"overview") (content .
"content"))))
24686 (if org-odd-levels-only
"odd" "oddeven")
24687 (if org-hide-leading-stars
"hidestars" "showstars")
24688 (if org-startup-align-all-tables
"align" "noalign")
24689 (cond ((eq t org-log-done
) "logdone")
24690 ((not org-log-done
) "nologging")
24691 ((listp org-log-done
)
24692 (mapconcat (lambda (x) (concat "lognote" (symbol-name x
)))
24693 org-log-done
" ")))
24694 (or (mapconcat (lambda (x)
24696 ((equal '(:startgroup
) x
) "{")
24697 ((equal '(:endgroup
) x
) "}")
24698 ((cdr x
) (format "%s(%c)" (car x
) (cdr x
)))
24700 (or org-tag-alist
(org-get-buffer-tags)) " ") "")
24701 org-archive-location
24702 "org file:~/org/%s.org"
24705 (defun org-insert-export-options-template ()
24706 "Insert into the buffer a template with information for exporting."
24708 (if (not (bolp)) (newline))
24709 (let ((s (org-get-current-options)))
24710 (and (string-match "#\\+CATEGORY" s
)
24711 (setq s
(substring s
0 (match-beginning 0))))
24714 (defun org-toggle-fixed-width-section (arg)
24715 "Toggle the fixed-width export.
24716 If there is no active region, the QUOTE keyword at the current headline is
24717 inserted or removed. When present, it causes the text between this headline
24718 and the next to be exported as fixed-width text, and unmodified.
24719 If there is an active region, this command adds or removes a colon as the
24720 first character of this line. If the first character of a line is a colon,
24721 this line is also exported in fixed-width font."
24724 (regionp (org-region-active-p))
24725 (beg (if regionp
(region-beginning) (point)))
24726 (end (if regionp
(region-end)))
24727 (nlines (or arg
(if (and beg end
) (count-lines beg end
) 1)))
24728 (case-fold-search nil
)
24729 (re "[ \t]*\\(:\\)")
24734 (setq cc
(current-column))
24735 (beginning-of-line 1)
24736 (setq off
(looking-at re
))
24737 (while (> nlines
0)
24738 (setq nlines
(1- nlines
))
24739 (beginning-of-line 1)
24742 (move-to-column cc t
)
24745 ((and off
(looking-at re
))
24746 (replace-match "" t t nil
1))
24747 ((not off
) (move-to-column cc t
) (insert ":")))
24750 (org-back-to-heading)
24751 (if (looking-at (concat outline-regexp
24752 "\\( *\\<" org-quote-string
"\\>[ \t]*\\)"))
24753 (replace-match "" t t nil
1)
24754 (if (looking-at outline-regexp
)
24756 (goto-char (match-end 0))
24757 (insert org-quote-string
" "))))))))
24759 (defun org-export-as-html-and-open (arg)
24760 "Export the outline as HTML and immediately open it with a browser.
24761 If there is an active region, export only the region.
24762 The prefix ARG specifies how many levels of the outline should become
24763 headlines. The default is 3. Lower levels will become bulleted lists."
24765 (org-export-as-html arg
'hidden
)
24766 (org-open-file buffer-file-name
))
24768 (defun org-export-as-html-batch ()
24769 "Call `org-export-as-html', may be used in batch processing as
24771 --load=$HOME/lib/emacs/org.el
24772 --eval \"(setq org-export-headline-levels 2)\"
24773 --visit=MyFile --funcall org-export-as-html-batch"
24774 (org-export-as-html org-export-headline-levels
'hidden
))
24776 (defun org-export-as-html-to-buffer (arg)
24777 "Call `org-exort-as-html` with output to a temporary buffer.
24778 No file is created. The prefix ARG is passed through to `org-export-as-html'."
24780 (org-export-as-html arg nil nil
"*Org HTML Export*")
24781 (switch-to-buffer-other-window "*Org HTML Export*"))
24783 (defun org-replace-region-by-html (beg end
)
24784 "Assume the current region has org-mode syntax, and convert it to HTML.
24785 This can be used in any buffer. For example, you could write an
24786 itemized list in org-mode syntax in an HTML buffer and then use this
24787 command to convert it."
24789 (let (reg html buf pop-up-frames
)
24790 (save-window-excursion
24792 (setq html
(org-export-region-as-html
24793 beg end t
'string
))
24794 (setq reg
(buffer-substring beg end
)
24795 buf
(get-buffer-create "*Org tmp*"))
24796 (with-current-buffer buf
24800 (setq html
(org-export-region-as-html
24801 (point-min) (point-max) t
'string
)))
24802 (kill-buffer buf
)))
24803 (delete-region beg end
)
24806 (defun org-export-region-as-html (beg end
&optional body-only buffer
)
24807 "Convert region from BEG to END in org-mode buffer to HTML.
24808 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
24809 contents, and only produce the region of converted text, useful for
24810 cut-and-paste operations.
24811 If BUFFER is a buffer or a string, use/create that buffer as a target
24812 of the converted HTML. If BUFFER is the symbol `string', return the
24813 produced HTML as a string and leave not buffer behind. For example,
24814 a Lisp program could call this function in the following way:
24816 (setq html (org-export-region-as-html beg end t 'string))
24818 When called interactively, the output buffer is selected, and shown
24819 in a window. A non-interactive call will only retunr the buffer."
24820 (interactive "r\nP")
24821 (when (interactive-p)
24822 (setq buffer
"*Org HTML Export*"))
24823 (let ((transient-mark-mode t
) (zmacs-regions t
)
24826 (set-mark (point)) ;; to activate the region
24828 (setq rtn
(org-export-as-html
24831 (if (fboundp 'deactivate-mark
) (deactivate-mark))
24832 (if (and (interactive-p) (bufferp rtn
))
24833 (switch-to-buffer-other-window rtn
)
24836 (defvar html-table-tag nil
) ; dynamically scoped into this.
24837 (defun org-export-as-html (arg &optional hidden ext-plist
24838 to-buffer body-only
)
24839 "Export the outline as a pretty HTML file.
24840 If there is an active region, export only the region. The prefix
24841 ARG specifies how many levels of the outline should become
24842 headlines. The default is 3. Lower levels will become bulleted
24843 lists. When HIDDEN is non-nil, don't display the HTML buffer.
24844 EXT-PLIST is a property list with external parameters overriding
24845 org-mode's default settings, but still inferior to file-local
24846 settings. When TO-BUFFER is non-nil, create a buffer with that
24847 name and export to that buffer. If TO-BUFFER is the symbol `string',
24848 don't leave any buffer behind but just return the resulting HTML as
24849 a string. When BODY-ONLY is set, don't produce the file header and footer,
24850 simply return the content of <body>...</body>, without even
24851 the body tags themselves."
24854 ;; Make sure we have a file name when we need it.
24855 (when (and (not (or to-buffer body-only
))
24856 (not buffer-file-name
))
24857 (if (buffer-base-buffer)
24858 (org-set-local 'buffer-file-name
24859 (with-current-buffer (buffer-base-buffer)
24861 (error "Need a file name to be able to export.")))
24863 (message "Exporting...")
24864 (setq-default org-todo-line-regexp org-todo-line-regexp
)
24865 (setq-default org-deadline-line-regexp org-deadline-line-regexp
)
24866 (setq-default org-done-keywords org-done-keywords
)
24867 (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp
)
24868 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
24870 (org-infile-export-plist)))
24872 (style (plist-get opt-plist
:style
))
24873 (html-extension (plist-get opt-plist
:html-extension
))
24874 (link-validate (plist-get opt-plist
:link-validation-function
))
24875 valid thetoc have-headings first-heading-pos
24876 (odd org-odd-levels-only
)
24877 (region-p (org-region-active-p))
24881 (goto-char (region-beginning))
24882 (and (org-at-heading-p)
24883 (>= (org-end-of-subtree t t
) (region-end))))))
24884 ;; The following two are dynamically scoped into other
24886 (org-current-export-dir (org-export-directory :html opt-plist
))
24887 (org-current-export-file buffer-file-name
)
24888 (level 0) (line "") (origline "") txt todo
24891 (filename (if to-buffer nil
24894 (file-name-sans-extension
24896 (org-entry-get (region-beginning)
24897 "EXPORT_FILE_NAME" t
))
24898 (file-name-nondirectory buffer-file-name
)))
24899 "." html-extension
)
24900 (file-name-as-directory
24901 (org-export-directory :html opt-plist
)))))
24902 (current-dir (if buffer-file-name
24903 (file-name-directory buffer-file-name
)
24904 default-directory
))
24905 (buffer (if to-buffer
24907 ((eq to-buffer
'string
) (get-buffer-create "*Org HTML Export*"))
24908 (t (get-buffer-create to-buffer
)))
24909 (find-file-noselect filename
)))
24910 (org-levels-open (make-vector org-level-max nil
))
24911 (date (plist-get opt-plist
:date
))
24912 (author (plist-get opt-plist
:author
))
24913 (title (or (and subtree-p
(org-export-get-title-from-subtree))
24914 (plist-get opt-plist
:title
)
24916 (plist-get opt-plist
:skip-before-1st-heading
))
24917 (org-export-grab-title-from-buffer))
24918 (and buffer-file-name
24919 (file-name-sans-extension
24920 (file-name-nondirectory buffer-file-name
)))
24922 (html-table-tag (plist-get opt-plist
:html-table-tag
))
24923 (quote-re0 (concat "^[ \t]*" org-quote-string
"\\>"))
24924 (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string
"\\>\\)"))
24927 (in-local-list nil
)
24928 (local-list-num nil
)
24929 (local-list-indent nil
)
24930 (llt org-plain-list-ordered-item-terminator
)
24931 (email (plist-get opt-plist
:email
))
24932 (language (plist-get opt-plist
:language
))
24934 (target-alist nil
) tg
24937 (coding-system (and (boundp 'buffer-file-coding-system
)
24938 buffer-file-coding-system
))
24939 (coding-system-for-write (or org-export-html-coding-system
24941 (save-buffer-coding-system (or org-export-html-coding-system
24943 (charset (and coding-system-for-write
24944 (fboundp 'coding-system-get
)
24945 (coding-system-get coding-system-for-write
24949 (if region-p
(region-beginning) (point-min))
24950 (if region-p
(region-end) (point-max))))
24953 (org-cleaned-string-for-export
24957 :skip-before-1st-heading
24958 (plist-get opt-plist
:skip-before-1st-heading
)
24959 :drawers
(plist-get opt-plist
:drawers
)
24961 (plist-get opt-plist
:archived-trees
)
24963 (plist-get opt-plist
:text
)
24965 (plist-get opt-plist
:LaTeX-fragments
))
24968 table-buffer table-orig-buffer
24969 ind start-is-num starter didclose
24970 rpl path desc descp desc1 desc2 link
24973 (let ((inhibit-read-only t
))
24975 (remove-text-properties (point-min) (point-max)
24976 '(:org-license-to-kill t
))))
24978 (message "Exporting...")
24980 (setq org-min-level
(org-get-min-level lines
))
24981 (setq org-last-level org-min-level
)
24982 (org-init-section-numbers)
24985 ((and date
(string-match "%" date
))
24986 (setq date
(format-time-string date
(current-time))))
24988 (t (setq date
(format-time-string "%Y/%m/%d %X" (current-time)))))
24990 ;; Get the language-dependent settings
24991 (setq lang-words
(or (assoc language org-export-language-setup
)
24992 (assoc "en" org-export-language-setup
)))
24994 ;; Switch to the output buffer
24995 (set-buffer buffer
)
24996 (let ((inhibit-read-only t
)) (erase-buffer))
24999 (and (fboundp 'set-buffer-file-coding-system
)
25000 (set-buffer-file-coding-system coding-system-for-write
))
25002 (let ((case-fold-search nil
)
25003 (org-odd-levels-only odd
))
25004 ;; create local variables for all options, to make sure all called
25005 ;; functions get the correct information
25007 (set (make-local-variable (cdr x
))
25008 (plist-get opt-plist
(car x
))))
25009 org-export-plist-vars
)
25010 (setq umax
(if arg
(prefix-numeric-value arg
)
25011 org-export-headline-levels
))
25012 (setq umax-toc
(if (integerp org-export-with-toc
)
25013 (min org-export-with-toc umax
)
25018 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
25019 \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
25020 <html xmlns=\"http://www.w3.org/1999/xhtml\"
25021 lang=\"%s\" xml:lang=\"%s\">
25024 <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
25025 <meta name=\"generator\" content=\"Org-mode\"/>
25026 <meta name=\"generated\" content=\"%s\"/>
25027 <meta name=\"author\" content=\"%s\"/>
25031 language language
(org-html-expand title
)
25032 (or charset
"iso-8859-1") date author style
))
25034 (insert (or (plist-get opt-plist
:preamble
) ""))
25036 (when (plist-get opt-plist
:auto-preamble
)
25037 (if title
(insert (format org-export-html-title-format
25038 (org-html-expand title
))))))
25040 (if (and org-export-with-toc
(not body-only
))
25042 (push (format "<h%d>%s</h%d>\n"
25043 org-export-html-toplevel-hlevel
25045 org-export-html-toplevel-hlevel
)
25047 (push "<ul>\n<li>" thetoc
)
25049 (mapcar '(lambda (line)
25050 (if (string-match org-todo-line-regexp line
)
25051 ;; This is a headline
25053 (setq have-headings t
)
25054 (setq level
(- (match-end 1) (match-beginning 1))
25055 level
(org-tr-level level
)
25056 txt
(save-match-data
25058 (org-export-cleanup-toc-line
25059 (match-string 3 line
))))
25061 (or (and org-export-mark-todo-in-toc
25062 (match-beginning 2)
25063 (not (member (match-string 2 line
)
25064 org-done-keywords
)))
25066 (and org-export-mark-todo-in-toc
25068 (org-search-todo-below
25069 line lines level
))))
25071 (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt
)
25072 (setq txt
(replace-match " <span class=\"tag\"> \\1</span>" t nil txt
)))
25073 (if (string-match quote-re0 txt
)
25074 (setq txt
(replace-match "" t t txt
)))
25075 (if org-export-with-section-numbers
25076 (setq txt
(concat (org-section-number level
)
25078 (if (<= level
(max umax umax-toc
))
25079 (setq head-count
(+ head-count
1)))
25080 (if (<= level umax-toc
)
25082 (if (> level org-last-level
)
25084 (setq cnt
(- level org-last-level
))
25085 (while (>= (setq cnt
(1- cnt
)) 0)
25086 (push "\n<ul>\n<li>" thetoc
))
25087 (push "\n" thetoc
)))
25088 (if (< level org-last-level
)
25090 (setq cnt
(- org-last-level level
))
25091 (while (>= (setq cnt
(1- cnt
)) 0)
25092 (push "</li>\n</ul>" thetoc
))
25093 (push "\n" thetoc
)))
25094 ;; Check for targets
25095 (while (string-match org-target-regexp line
)
25096 (setq tg
(match-string 1 line
)
25097 line
(replace-match
25098 (concat "@<span class=\"target\">" tg
"@</span> ")
25100 (push (cons (org-solidify-link-text tg
)
25101 (format "sec-%d" head-count
))
25103 (while (string-match "<\\(<\\)+\\|>\\(>\\)+" txt
)
25104 (setq txt
(replace-match "" t t txt
)))
25108 "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
25109 "</li>\n<li><a href=\"#sec-%d\">%s</a>")
25110 head-count txt
) thetoc
)
25112 (setq org-last-level level
))
25116 (while (> org-last-level
(1- org-min-level
))
25117 (setq org-last-level
(1- org-last-level
))
25118 (push "</li>\n</ul>\n" thetoc
))
25119 (setq thetoc
(if have-headings
(nreverse thetoc
) nil
))))
25121 (setq head-count
0)
25122 (org-init-section-numbers)
25124 (while (setq line
(pop lines
) origline line
)
25127 ;; end of quote section?
25128 (when (and inquote
(string-match "^\\*+ " line
))
25129 (insert "</pre>\n")
25130 (setq inquote nil
))
25131 ;; inside a quote section?
25133 (insert (org-html-protect line
) "\n")
25134 (throw 'nextline nil
))
25137 (when (and org-export-with-fixed-width
25138 (string-match "^[ \t]*:\\(.*\\)" line
))
25139 (when (not infixed
)
25141 (insert "<pre>\n"))
25142 (insert (org-html-protect (match-string 1 line
)) "\n")
25144 (not (string-match "^[ \t]*\\(:.*\\)"
25147 (insert "</pre>\n"))
25148 (throw 'nextline nil
))
25151 (when (get-text-property 0 'org-protected line
)
25153 (when (re-search-backward
25154 "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t
)
25155 (setq par
(match-string 1))
25156 (replace-match "\\2\n"))
25159 (or (= (length (car lines
)) 0)
25160 (get-text-property 0 'org-protected
(car lines
))))
25161 (insert (pop lines
) "\n"))
25162 (and par
(insert "<p>\n")))
25163 (throw 'nextline nil
))
25166 (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line
)
25167 (insert "\n<hr/>\n")
25168 (throw 'nextline nil
))
25170 ;; make targets to anchors
25171 (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line
)
25174 (setq line
(replace-match
25175 (concat "@<a name=\""
25176 (org-solidify-link-text (match-string 1 line
))
25179 ((and org-export-with-toc
(equal (string-to-char line
) ?
*))
25180 (setq line
(replace-match
25181 (concat "@<span class=\"target\">" (match-string 1 line
) "@</span> ")
25182 ; (concat "@<i>" (match-string 1 line) "@</i> ")
25185 (setq line
(replace-match
25186 (concat "@<a name=\""
25187 (org-solidify-link-text (match-string 1 line
))
25188 "\" class=\"target\">" (match-string 1 line
) "@</a> ")
25191 (setq line
(org-html-handle-time-stamps line
))
25193 ;; replace "&" by "&", "<" and ">" by "<" and ">"
25194 ;; handle @<..> HTML tags (replace "@>..<" by "<..>")
25195 ;; Also handle sub_superscripts and checkboxes
25196 (or (string-match org-table-hline-regexp line
)
25197 (setq line
(org-html-expand line
)))
25199 ;; Format the links
25201 (while (string-match org-bracket-link-analytic-regexp line start
)
25202 (setq start
(match-beginning 0))
25203 (setq type
(if (match-end 2) (match-string 2 line
) "internal"))
25204 (setq path
(match-string 3 line
))
25205 (setq desc1
(if (match-end 5) (match-string 5 line
))
25206 desc2
(if (match-end 2) (concat type
":" path
) path
)
25207 descp
(and desc1
(not (equal desc1 desc2
)))
25208 desc
(or desc1 desc2
))
25209 ;; Make an image out of the description if that is so wanted
25210 (when (and descp
(org-file-image-p desc
))
25212 (if (string-match "^file:" desc
)
25213 (setq desc
(substring desc
(match-end 0)))))
25214 (setq desc
(concat "<img src=\"" desc
"\"/>")))
25215 ;; FIXME: do we need to unescape here somewhere?
25217 ((equal type
"internal")
25221 (org-solidify-link-text
25222 (save-match-data (org-link-unescape path
)) target-alist
)
25223 "\">" desc
"</a>")))
25224 ((member type
'("http" "https"))
25225 ;; standard URL, just check if we need to inline an image
25226 (if (and (or (eq t org-export-html-inline-images
)
25227 (and org-export-html-inline-images
(not descp
)))
25228 (org-file-image-p path
))
25229 (setq rpl
(concat "<img src=\"" type
":" path
"\"/>"))
25230 (setq link
(concat type
":" path
))
25231 (setq rpl
(concat "<a href=\"" link
"\">" desc
"</a>"))))
25232 ((member type
'("ftp" "mailto" "news"))
25234 (setq link
(concat type
":" path
))
25235 (setq rpl
(concat "<a href=\"" link
"\">" desc
"</a>")))
25236 ((string= type
"file")
25238 (let* ((filename path
)
25239 (abs-p (file-name-absolute-p filename
))
25240 thefile file-is-image-p search
)
25242 (if (string-match "::\\(.*\\)" filename
)
25243 (setq search
(match-string 1 filename
)
25244 filename
(replace-match "" t nil filename
)))
25246 (if (functionp link-validate
)
25247 (funcall link-validate filename current-dir
)
25249 (setq file-is-image-p
(org-file-image-p filename
))
25250 (setq thefile
(if abs-p
(expand-file-name filename
) filename
))
25251 (when (and org-export-html-link-org-files-as-html
25252 (string-match "\\.org$" thefile
))
25253 (setq thefile
(concat (substring thefile
0
25254 (match-beginning 0))
25255 "." html-extension
))
25257 ;; make sure this is can be used as target search
25258 (not (string-match "^[0-9]*$" search
))
25259 (not (string-match "^\\*" search
))
25260 (not (string-match "^/.*/$" search
)))
25261 (setq thefile
(concat thefile
"#"
25262 (org-solidify-link-text
25263 (org-link-unescape search
)))))
25264 (when (string-match "^file:" desc
)
25265 (setq desc
(replace-match "" t t desc
))
25266 (if (string-match "\\.org$" desc
)
25267 (setq desc
(replace-match "" t t desc
))))))
25268 (setq rpl
(if (and file-is-image-p
25269 (or (eq t org-export-html-inline-images
)
25270 (and org-export-html-inline-images
25272 (concat "<img src=\"" thefile
"\"/>")
25273 (concat "<a href=\"" thefile
"\">" desc
"</a>")))
25274 (if (not valid
) (setq rpl desc
))))
25275 ((member type
'("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
25276 (setq rpl
(concat "<i><" type
":"
25277 (save-match-data (org-link-unescape path
))
25279 (setq line
(replace-match rpl t t line
)
25280 start
(+ start
(length rpl
))))
25283 (if (and (string-match org-todo-line-regexp line
)
25284 (match-beginning 2))
25287 (concat (substring line
0 (match-beginning 2))
25289 (if (member (match-string 2 line
)
25292 "\">" (match-string 2 line
)
25293 "</span>" (substring line
(match-end 2)))))
25295 ;; Does this contain a reference to a footnote?
25296 (when org-export-with-footnotes
25298 (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start
)
25299 (if (get-text-property (match-beginning 2) 'org-protected line
)
25300 (setq start
(match-end 2))
25301 (let ((n (match-string 2 line
)))
25305 "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
25306 (match-string 1 line
) n n n
)
25310 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line
)
25311 ;; This is a headline
25312 (setq level
(org-tr-level (- (match-end 1) (match-beginning 1)))
25313 txt
(match-string 2 line
))
25314 (if (string-match quote-re0 txt
)
25315 (setq txt
(replace-match "" t t txt
)))
25316 (if (<= level
(max umax umax-toc
))
25317 (setq head-count
(+ head-count
1)))
25318 (when in-local-list
25319 ;; Close any local lists before inserting a new header line
25320 (while local-list-num
25322 (insert (if (car local-list-num
) "</ol>\n" "</ul>"))
25323 (pop local-list-num
))
25324 (setq local-list-indent nil
25325 in-local-list nil
))
25326 (setq first-heading-pos
(or first-heading-pos
(point)))
25327 (org-html-level-start level txt umax
25328 (and org-export-with-toc
(<= level umax
))
25331 (when (string-match quote-re line
)
25335 ((and org-export-with-tables
25336 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line
))
25337 (if (not table-open
)
25338 ;; New table starts
25339 (setq table-open t table-buffer nil table-orig-buffer nil
))
25340 ;; Accumulate lines
25341 (setq table-buffer
(cons line table-buffer
)
25342 table-orig-buffer
(cons origline table-orig-buffer
))
25343 (when (or (not lines
)
25344 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
25346 (setq table-open nil
25347 table-buffer
(nreverse table-buffer
)
25348 table-orig-buffer
(nreverse table-orig-buffer
))
25349 (org-close-par-maybe)
25350 (insert (org-format-table-html table-buffer table-orig-buffer
))))
25353 (when (string-match
25355 ((eq llt t
) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
25356 ((= llt ?.
) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
25357 ((= llt ?\
)) "^\\( \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
25358 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
25360 (setq ind
(org-get-string-indentation line
)
25361 start-is-num
(match-beginning 4)
25362 starter
(if (match-beginning 2)
25363 (substring (match-string 2 line
) 0 -
1))
25364 line
(substring line
(match-beginning 5)))
25365 (unless (string-match "[^ \t]" line
)
25366 ;; empty line. Pretend indentation is large.
25367 (setq ind
(if org-empty-line-terminates-plain-lists
25369 (1+ (or (car local-list-indent
) 1)))))
25370 (setq didclose nil
)
25371 (while (and in-local-list
25372 (or (and (= ind
(car local-list-indent
))
25374 (< ind
(car local-list-indent
))))
25377 (insert (if (car local-list-num
) "</ol>\n" "</ul>"))
25378 (pop local-list-num
) (pop local-list-indent
)
25379 (setq in-local-list local-list-indent
))
25382 (or (not in-local-list
)
25383 (> ind
(car local-list-indent
))))
25384 ;; Start new (level of) list
25385 (org-close-par-maybe)
25386 (insert (if start-is-num
"<ol>\n<li>\n" "<ul>\n<li>\n"))
25387 (push start-is-num local-list-num
)
25388 (push ind local-list-indent
)
25389 (setq in-local-list t
))
25391 ;; continue current list
25395 ;; we did close a list, normal text follows: need <p>
25397 (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line
)
25400 (if (equal (match-string 1 line
) "X")
25402 "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
25405 ;; Empty lines start a new paragraph. If hand-formatted lists
25406 ;; are not fully interpreted, lines starting with "-", "+", "*"
25407 ;; also start a new paragraph.
25408 (if (string-match "^ [-+*]-\\|^[ \t]*$" line
) (org-open-par))
25410 ;; Is this the start of a footnote?
25411 (when org-export-with-footnotes
25412 (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line
)
25413 (org-close-par-maybe)
25414 (let ((n (match-string 1 line
)))
25415 (setq line
(replace-match
25416 (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n
) t t line
)))))
25418 ;; Check if the line break needs to be conserved
25420 ((string-match "\\\\\\\\[ \t]*$" line
)
25421 (setq line
(replace-match "<br/>" t t line
)))
25422 (org-export-preserve-breaks
25423 (setq line
(concat line
"<br/>"))))
25425 (insert line
"\n")))))
25427 ;; Properly close all local lists and other lists
25428 (when inquote
(insert "</pre>\n"))
25429 (when in-local-list
25430 ;; Close any local lists before inserting a new header line
25431 (while local-list-num
25433 (insert (if (car local-list-num
) "</ol>\n" "</ul>\n"))
25434 (pop local-list-num
))
25435 (setq local-list-indent nil
25436 in-local-list nil
))
25437 (org-html-level-start 1 nil umax
25438 (and org-export-with-toc
(<= level umax
))
25442 (when (plist-get opt-plist
:auto-postamble
)
25443 (insert "<div id=\"postamble\">")
25444 (when (and org-export-author-info author
)
25445 (insert "<p class=\"author\"> "
25446 (nth 1 lang-words
) ": " author
"\n")
25448 (if (listp (split-string email
",+ *"))
25450 (insert "<a href=\"mailto:" e
"\"><"
25452 (split-string email
",+ *"))
25453 (insert "<a href=\"mailto:" email
"\"><"
25454 email
"></a>\n")))
25456 (when (and date org-export-time-stamp-file
)
25457 (insert "<p class=\"date\"> "
25458 (nth 2 lang-words
) ": "
25462 (if org-export-html-with-timestamp
25463 (insert org-export-html-html-helper-timestamp
))
25464 (insert (or (plist-get opt-plist
:postamble
) ""))
25465 (insert "</body>\n</html>\n"))
25468 (if (eq major-mode default-major-mode
) (html-mode))
25470 ;; insert the table of contents
25471 (goto-char (point-min))
25473 (if (or (re-search-forward
25474 "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t
)
25476 "\\[TABLE-OF-CONTENTS\\]" nil t
))
25478 (goto-char (match-beginning 0))
25479 (replace-match ""))
25480 (goto-char first-heading-pos
)
25481 (when (looking-at "\\s-*</p>")
25482 (goto-char (match-end 0))
25484 (insert "<div id=\"table-of-contents\">\n")
25485 (mapc 'insert thetoc
)
25486 (insert "</div>\n"))
25487 ;; remove empty paragraphs and lists
25488 (goto-char (point-min))
25489 (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t
)
25490 (replace-match ""))
25491 (goto-char (point-min))
25492 (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t
)
25493 (replace-match ""))
25494 (goto-char (point-min))
25495 (while (re-search-forward "</ul>\\s-*<ul>\n?" nil t
)
25496 (replace-match ""))
25497 ;; Convert whitespace place holders
25498 (goto-char (point-min))
25500 (while (setq beg
(next-single-property-change (point) 'org-whitespace
))
25501 (setq n
(get-text-property beg
'org-whitespace
)
25502 end
(next-single-property-change beg
'org-whitespace
))
25504 (delete-region beg end
)
25505 (insert (format "<span style=\"visibility:hidden;\">%s</span>"
25506 (make-string n ?x
)))))
25508 (or to-buffer
(save-buffer))
25509 (goto-char (point-min))
25510 (message "Exporting... done")
25511 (if (eq to-buffer
'string
)
25512 (prog1 (buffer-substring (point-min) (point-max))
25513 (kill-buffer (current-buffer)))
25514 (current-buffer)))))
25516 (defvar org-table-colgroup-info nil
)
25517 (defun org-format-table-ascii (lines)
25518 "Format a table for ascii export."
25519 (if (stringp lines
)
25520 (setq lines
(org-split-string lines
"\n")))
25521 (if (not (string-match "^[ \t]*|" (car lines
)))
25522 ;; Table made by table.el - test for spanning
25525 ;; A normal org table
25526 ;; Get rid of hlines at beginning and end
25527 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
25528 (setq lines
(nreverse lines
))
25529 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
25530 (setq lines
(nreverse lines
))
25531 (when org-export-table-remove-special-lines
25532 ;; Check if the table has a marking column. If yes remove the
25533 ;; column and the special lines
25534 (setq lines
(org-table-clean-before-export lines
)))
25535 ;; Get rid of the vertical lines except for grouping
25536 (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info
))
25537 rtn line vl1 start
)
25538 (while (setq line
(pop lines
))
25539 (if (string-match org-table-hline-regexp line
)
25540 (and (string-match "|\\(.*\\)|" line
)
25541 (setq line
(replace-match " \\1" t nil line
)))
25542 (setq start
0 vl1 vl
)
25543 (while (string-match "|" line start
)
25544 (setq start
(match-end 0))
25545 (or (pop vl1
) (setq line
(replace-match " " t t line
)))))
25549 (defun org-colgroup-info-to-vline-list (info)
25552 (setq last new new
(pop info
))
25553 (if (or (memq last
'(:end
:startend
))
25554 (memq new
'(:start
:startend
)))
25557 (setq vl
(nreverse vl
))
25558 (and vl
(setcar vl nil
))
25561 (defun org-format-table-html (lines olines
)
25562 "Find out which HTML converter to use and return the HTML code."
25563 (if (stringp lines
)
25564 (setq lines
(org-split-string lines
"\n")))
25565 (if (string-match "^[ \t]*|" (car lines
))
25566 ;; A normal org table
25567 (org-format-org-table-html lines
)
25568 ;; Table made by table.el - test for spanning
25569 (let* ((hlines (delq nil
(mapcar
25571 (if (string-match "^[ \t]*\\+-" x
) x
25574 (first (car hlines
))
25575 (ll (and (string-match "\\S-+" first
)
25576 (match-string 0 first
)))
25577 (re (concat "^[ \t]*" (regexp-quote ll
)))
25578 (spanning (delq nil
(mapcar (lambda (x) (not (string-match re x
)))
25580 (if (and (not spanning
)
25581 (not org-export-prefer-native-exporter-for-tables
))
25582 ;; We can use my own converter with HTML conversions
25583 (org-format-table-table-html lines
)
25584 ;; Need to use the code generator in table.el, with the original text.
25585 (org-format-table-table-html-using-table-generate-source olines
)))))
25587 (defun org-format-org-table-html (lines &optional splice
)
25588 "Format a table into HTML."
25589 ;; Get rid of hlines at beginning and end
25590 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
25591 (setq lines
(nreverse lines
))
25592 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
25593 (setq lines
(nreverse lines
))
25594 (when org-export-table-remove-special-lines
25595 ;; Check if the table has a marking column. If yes remove the
25596 ;; column and the special lines
25597 (setq lines
(org-table-clean-before-export lines
)))
25599 (let ((head (and org-export-highlight-first-table-line
25601 (lambda (x) (string-match "^[ \t]*|-" x
))
25604 tbopen line fields html gr colgropen
)
25605 (if splice
(setq head nil
))
25606 (unless splice
(push (if head
"<thead>" "<tbody>") html
))
25608 (while (setq line
(pop lines
))
25610 (if (string-match "^[ \t]*|-" line
)
25613 (push (if head
"</thead>" "</tbody>") html
)
25614 (if lines
(push "<tbody>" html
) (setq tbopen nil
)))
25615 (setq head nil
) ;; head ends here, first time around
25616 ;; ignore this line
25617 (throw 'next-line t
)))
25618 ;; Break the line into fields
25619 (setq fields
(org-split-string line
"[ \t]*|[ \t]*"))
25620 (unless fnum
(setq fnum
(make-vector (length fields
) 0)))
25621 (setq nlines
(1+ nlines
) i -
1)
25622 (push (concat "<tr>"
25626 (if (and (< i nlines
)
25627 (string-match org-table-number-regexp x
))
25628 (incf (aref fnum i
)))
25630 (concat (car org-export-table-header-tags
) x
25631 (cdr org-export-table-header-tags
))
25632 (concat (car org-export-table-data-tags
) x
25633 (cdr org-export-table-data-tags
))))
25637 (unless splice
(if tbopen
(push "</tbody>" html
)))
25638 (unless splice
(push "</table>\n" html
))
25639 (setq html
(nreverse html
))
25641 ;; Put in col tags with the alignment (unfortuntely often ignored...)
25644 (setq gr
(pop org-table-colgroup-info
))
25645 (format "%s<col align=\"%s\"></col>%s"
25646 (if (memq gr
'(:start
:startend
))
25648 (if colgropen
"</colgroup>\n<colgroup>" "<colgroup>")
25649 (setq colgropen t
))
25651 (if (> (/ (float x
) nlines
) org-table-number-fraction
)
25653 (if (memq gr
'(:end
:startend
))
25654 (progn (setq colgropen nil
) "</colgroup>")
25658 (if colgropen
(setq html
(cons (car html
) (cons "</colgroup>" (cdr html
)))))
25659 (push html-table-tag html
))
25660 (concat (mapconcat 'identity html
"\n") "\n")))
25662 (defun org-table-clean-before-export (lines)
25663 "Check if the table has a marking column.
25664 If yes remove the column and the special lines."
25665 (setq org-table-colgroup-info nil
)
25668 (lambda (x) (or (string-match "^[ \t]*|-" x
)
25669 (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x
)))
25672 (setq org-table-clean-did-remove-column nil
)
25677 ((string-match "^[ \t]*| */ *|" x
)
25678 (setq org-table-colgroup-info
25679 (mapcar (lambda (x)
25680 (cond ((member x
'("<" "<")) :start
)
25681 ((member x
'(">" ">")) :end
)
25682 ((member x
'("<>" "<>")) :startend
)
25684 (org-split-string x
"[ \t]*|[ \t]*")))
25688 (setq org-table-clean-did-remove-column t
)
25693 ((string-match "^[ \t]*| */ *|" x
)
25694 (setq org-table-colgroup-info
25695 (mapcar (lambda (x)
25696 (cond ((member x
'("<" "<")) :start
)
25697 ((member x
'(">" ">")) :end
)
25698 ((member x
'("<>" "<>")) :startend
)
25700 (cdr (org-split-string x
"[ \t]*|[ \t]*"))))
25702 ((string-match "^[ \t]*| *[!_^/] *|" x
)
25703 nil
) ; ignore this line
25704 ((or (string-match "^\\([ \t]*\\)|-+\\+" x
)
25705 (string-match "^\\([ \t]*\\)|[^|]*|" x
))
25706 ;; remove the first column
25707 (replace-match "\\1|" t nil x
))))
25710 (defun org-format-table-table-html (lines)
25711 "Format a table generated by table.el into HTML.
25712 This conversion does *not* use `table-generate-source' from table.el.
25713 This has the advantage that Org-mode's HTML conversions can be used.
25714 But it has the disadvantage, that no cell- or row-spanning is allowed."
25715 (let (line field-buffer
25716 (head org-export-highlight-first-table-line
)
25718 (setq html
(concat html-table-tag
"\n"))
25719 (while (setq line
(pop lines
))
25720 (setq empty
" ")
25722 (if (string-match "^[ \t]*\\+-" line
)
25733 (if (equal x
"") (setq x empty
))
25735 (concat (car org-export-table-header-tags
) x
25736 (cdr org-export-table-header-tags
))
25737 (concat (car org-export-table-data-tags
) x
25738 (cdr org-export-table-data-tags
))))
25742 (setq field-buffer nil
)))
25743 ;; Ignore this line
25744 (throw 'next-line t
)))
25745 ;; Break the line into fields and store the fields
25746 (setq fields
(org-split-string line
"[ \t]*|[ \t]*"))
25748 (setq field-buffer
(mapcar
25750 (concat x
"<br/>" (pop fields
)))
25752 (setq field-buffer fields
))))
25753 (setq html
(concat html
"</table>\n"))
25756 (defun org-format-table-table-html-using-table-generate-source (lines)
25757 "Format a table into html, using `table-generate-source' from table.el.
25758 This has the advantage that cell- or row-spanning is allowed.
25759 But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
25761 (with-current-buffer (get-buffer-create " org-tmp1 ")
25763 (insert (mapconcat 'identity lines
"\n"))
25764 (goto-char (point-min))
25765 (if (not (re-search-forward "|[^+]" nil t
))
25766 (error "Error processing table"))
25767 (table-recognize-table)
25768 (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
25769 (table-generate-source 'html
" org-tmp2 ")
25770 (set-buffer " org-tmp2 ")
25771 (buffer-substring (point-min) (point-max))))
25773 (defun org-html-handle-time-stamps (s)
25774 "Format time stamps in string S, or remove them."
25777 (while (string-match org-maybe-keyword-time-regexp s
)
25778 (if (and (match-end 1) (equal (match-string 1 s
) org-clock-string
))
25779 ;; never export CLOCK
25781 (or b
(setq b
(substring s
0 (match-beginning 0))))
25782 (if (not org-export-with-timestamps
)
25783 (setq r
(concat r
(substring s
0 (match-beginning 0)))
25784 s
(substring s
(match-end 0)))
25786 r
(substring s
0 (match-beginning 0))
25788 (format "@<span class=\"timestamp-kwd\">%s @</span>"
25789 (match-string 1 s
)))
25790 (format " @<span class=\"timestamp\">%s@</span>"
25792 (org-translate-time (match-string 3 s
)) 1 -
1)))
25793 s
(substring s
(match-end 0)))))
25794 ;; Line break if line started and ended with time stamp stuff
25797 (setq r
(concat r s
))
25798 (unless (string-match "\\S-" (concat b s
))
25799 (setq r
(concat r
"@<br/>")))
25802 (defun org-html-protect (s)
25803 ;; convert & to &, < to < and > to >
25805 (while (string-match "&" s start
)
25806 (setq s
(replace-match "&" t t s
)
25807 start
(1+ (match-beginning 0))))
25808 (while (string-match "<" s
)
25809 (setq s
(replace-match "<" t t s
)))
25810 (while (string-match ">" s
)
25811 (setq s
(replace-match ">" t t s
))))
25814 (defun org-export-cleanup-toc-line (s)
25815 "Remove tags and time staps from lines going into the toc."
25816 (when (memq org-export-with-tags
'(not-in-toc nil
))
25817 (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s
)
25818 (setq s
(replace-match "" t t s
))))
25819 (when org-export-remove-timestamps-from-toc
25820 (while (string-match org-maybe-keyword-time-regexp s
)
25821 (setq s
(replace-match "" t t s
))))
25822 (while (string-match org-bracket-link-regexp s
)
25823 (setq s
(replace-match (match-string (if (match-end 3) 3 1) s
)
25827 (defun org-html-expand (string)
25828 "Prepare STRING for HTML export. Applies all active conversions.
25829 If there are links in the string, don't modify these."
25830 (let* ((re (concat org-bracket-link-regexp
"\\|"
25831 (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
25833 (while (setq m
(string-match re string
))
25834 (setq s
(substring string
0 m
)
25835 l
(match-string 0 string
)
25836 string
(substring string
(match-end 0)))
25837 (push (org-html-do-expand s
) res
)
25839 (push (org-html-do-expand string
) res
)
25840 (apply 'concat
(nreverse res
))))
25842 (defun org-html-do-expand (s)
25843 "Apply all active conversions to translate special ASCII to HTML."
25844 (setq s
(org-html-protect s
))
25845 (if org-export-html-expand
25847 (while (string-match "@<\\([^&]*\\)>" s
)
25848 (setq s
(replace-match "<\\1>" t nil s
)))))
25849 (if org-export-with-emphasize
25850 (setq s
(org-export-html-convert-emphasize s
)))
25851 (if org-export-with-special-strings
25852 (setq s
(org-export-html-convert-special-strings s
)))
25853 (if org-export-with-sub-superscripts
25854 (setq s
(org-export-html-convert-sub-super s
)))
25855 (if org-export-with-TeX-macros
25856 (let ((start 0) wd ass
)
25857 (while (setq start
(string-match "\\\\\\([a-zA-Z]+\\)" s start
))
25858 (if (get-text-property (match-beginning 0) 'org-protected s
)
25859 (setq start
(match-end 0))
25860 (setq wd
(match-string 1 s
))
25861 (if (setq ass
(assoc wd org-html-entities
))
25862 (setq s
(replace-match (or (cdr ass
)
25863 (concat "&" (car ass
) ";"))
25865 (setq start
(+ start
(length wd
))))))))
25868 (defun org-create-multibrace-regexp (left right n
)
25869 "Create a regular expression which will match a balanced sexp.
25870 Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
25871 as single character strings.
25872 The regexp returned will match the entire expression including the
25873 delimiters. It will also define a single group which contains the
25874 match except for the outermost delimiters. The maximum depth of
25875 stacked delimiters is N. Escaping delimiters is not possible."
25876 (let* ((nothing (concat "[^" "\\" left
"\\" right
"]*?"))
25879 (next (concat "\\(?:" nothing left nothing right
"\\)+" nothing
)))
25882 re
(concat re or next
)
25883 next
(concat "\\(?:" nothing left next right
"\\)+" nothing
)))
25884 (concat left
"\\(" re
"\\)" right
)))
25886 (defvar org-match-substring-regexp
25888 "\\([^\\]\\)\\([_^]\\)\\("
25889 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
25891 "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth
) "\\)"
25893 "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
25894 "The regular expression matching a sub- or superscript.")
25896 (defvar org-match-substring-with-braces-regexp
25898 "\\([^\\]\\)\\([_^]\\)\\("
25899 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
25901 "The regular expression matching a sub- or superscript, forcing braces.")
25903 (defconst org-export-html-special-string-regexps
25904 '(("\\\\-" .
"­")
25905 ("---\\([^-]\\)" .
"—\\1")
25906 ("--\\([^-]\\)" .
"–\\1")
25907 ("\\.\\.\\." .
"…"))
25908 "Regular expressions for special string conversion.")
25910 (defun org-export-html-convert-special-strings (string)
25911 "Convert special characters in STRING to HTML."
25912 (let ((all org-export-html-special-string-regexps
)
25914 (while (setq a
(pop all
))
25915 (setq re
(car a
) rpl
(cdr a
) start
0)
25916 (while (string-match re string start
)
25917 (if (get-text-property (match-beginning 0) 'org-protected string
)
25918 (setq start
(match-end 0))
25919 (setq string
(replace-match rpl t nil string
)))))
25922 (defun org-export-html-convert-sub-super (string)
25923 "Convert sub- and superscripts in STRING to HTML."
25924 (let (key c
(s 0) (requireb (eq org-export-with-sub-superscripts
'{})))
25925 (while (string-match org-match-substring-regexp string s
)
25927 ((and requireb
(match-end 8)) (setq s
(match-end 2)))
25928 ((get-text-property (match-beginning 2) 'org-protected string
)
25929 (setq s
(match-end 2)))
25931 (setq s
(match-end 1)
25932 key
(if (string= (match-string 2 string
) "_") "sub" "sup")
25933 c
(or (match-string 8 string
)
25934 (match-string 6 string
)
25935 (match-string 5 string
))
25936 string
(replace-match
25937 (concat (match-string 1 string
)
25938 "<" key
">" c
"</" key
">")
25940 (while (string-match "\\\\\\([_^]\\)" string
)
25941 (setq string
(replace-match (match-string 1 string
) t t string
)))
25944 (defun org-export-html-convert-emphasize (string)
25947 (while (string-match org-emph-re string s
)
25949 (substring string
(match-beginning 3) (1+ (match-beginning 3)))
25950 (substring string
(match-beginning 4) (1+ (match-beginning 4)))))
25951 (setq s
(match-beginning 0)
25954 (match-string 1 string
)
25955 (nth 2 (assoc (match-string 3 string
) org-emphasis-alist
))
25956 (match-string 4 string
)
25957 (nth 3 (assoc (match-string 3 string
)
25958 org-emphasis-alist
))
25959 (match-string 5 string
))
25960 string
(replace-match rpl t t string
)
25961 s
(+ s
(- (length rpl
) 2)))
25965 (defvar org-par-open nil
)
25966 (defun org-open-par ()
25967 "Insert <p>, but first close previous paragraph if any."
25968 (org-close-par-maybe)
25970 (setq org-par-open t
))
25971 (defun org-close-par-maybe ()
25972 "Close paragraph if there is one open."
25975 (setq org-par-open nil
)))
25976 (defun org-close-li ()
25977 "Close <li> if necessary."
25978 (org-close-par-maybe)
25979 (insert "</li>\n"))
25981 (defvar body-only
) ; dynamically scoped into this.
25982 (defun org-html-level-start (level title umax with-toc head-count
)
25983 "Insert a new level in HTML export.
25984 When TITLE is nil, just close all open levels."
25985 (org-close-par-maybe)
25986 (let ((l org-level-max
))
25987 (while (>= l level
)
25988 (if (aref org-levels-open
(1- l
))
25990 (org-html-level-close l umax
)
25991 (aset org-levels-open
(1- l
) nil
)))
25994 ;; If title is nil, this means this function is called to close
25995 ;; all levels, so the rest is done only if title is given
25996 (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title
)
25997 (setq title
(replace-match
25998 (if org-export-with-tags
26001 " <span class=\"tag\">"
26002 (mapconcat 'identity
(org-split-string
26003 (match-string 1 title
) ":")
26010 (if (aref org-levels-open
(1- level
))
26013 (insert "<li>" title
"<br/>\n"))
26014 (aset org-levels-open
(1- level
) t
)
26015 (org-close-par-maybe)
26016 (insert "<ul>\n<li>" title
"<br/>\n")))
26017 (aset org-levels-open
(1- level
) t
)
26018 (if (and org-export-with-section-numbers
(not body-only
))
26019 (setq title
(concat (org-section-number level
) " " title
)))
26020 (setq level
(+ level org-export-html-toplevel-hlevel -
1))
26022 (insert (format "\n<div class=\"outline-%d\">\n<h%d id=\"sec-%d\">%s</h%d>\n"
26023 level level head-count title level
))
26024 (insert (format "\n<div class=\"outline-%d\">\n<h%d>%s</h%d>\n" level level title level
)))
26027 (defun org-html-level-close (level max-outline-level
)
26028 "Terminate one level in HTML export."
26029 (if (<= level max-outline-level
)
26030 (insert "</div>\n")
26032 (insert "</ul>\n")))
26034 ;;; iCalendar export
26037 (defun org-export-icalendar-this-file ()
26038 "Export current file as an iCalendar file.
26039 The iCalendar file will be located in the same directory as the Org-mode
26040 file, but with extension `.ics'."
26042 (org-export-icalendar nil buffer-file-name
))
26045 (defun org-export-icalendar-all-agenda-files ()
26046 "Export all files in `org-agenda-files' to iCalendar .ics files.
26047 Each iCalendar file will be located in the same directory as the Org-mode
26048 file, but with extension `.ics'."
26050 (apply 'org-export-icalendar nil
(org-agenda-files t
)))
26053 (defun org-export-icalendar-combine-agenda-files ()
26054 "Export all files in `org-agenda-files' to a single combined iCalendar file.
26055 The file is stored under the name `org-combined-agenda-icalendar-file'."
26057 (apply 'org-export-icalendar t
(org-agenda-files t
)))
26059 (defun org-export-icalendar (combine &rest files
)
26060 "Create iCalendar files for all elements of FILES.
26061 If COMBINE is non-nil, combine all calendar entries into a single large
26062 file and store it under the name `org-combined-agenda-icalendar-file'."
26064 (org-prepare-agenda-buffers files
)
26065 (let* ((dir (org-export-directory
26066 :ical
(list :publishing-directory
26067 org-export-publishing-directory
)))
26068 file ical-file ical-buffer category started org-agenda-new-buffers
)
26070 (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
26073 (if (file-name-absolute-p org-combined-agenda-icalendar-file
)
26074 org-combined-agenda-icalendar-file
26075 (expand-file-name org-combined-agenda-icalendar-file dir
))
26076 ical-buffer
(org-get-agenda-file-buffer ical-file
))
26077 (set-buffer ical-buffer
) (erase-buffer))
26078 (while (setq file
(pop files
))
26080 (org-check-agenda-file file
)
26081 (set-buffer (org-get-agenda-file-buffer file
))
26083 (setq ical-file
(concat (file-name-as-directory dir
)
26084 (file-name-sans-extension
26085 (file-name-nondirectory buffer-file-name
))
26087 (setq ical-buffer
(org-get-agenda-file-buffer ical-file
))
26088 (with-current-buffer ical-buffer
(erase-buffer)))
26089 (setq category
(or org-category
26090 (file-name-sans-extension
26091 (file-name-nondirectory buffer-file-name
))))
26092 (if (symbolp category
) (setq category
(symbol-name category
)))
26093 (let ((standard-output ical-buffer
))
26095 (and (not started
) (setq started t
)
26096 (org-start-icalendar-file org-icalendar-combined-name
))
26097 (org-start-icalendar-file category
))
26098 (org-print-icalendar-entries combine
)
26099 (when (or (and combine
(not files
)) (not combine
))
26100 (org-finish-icalendar-file)
26101 (set-buffer ical-buffer
)
26103 (run-hooks 'org-after-save-iCalendar-file-hook
)))))
26104 (org-release-buffers org-agenda-new-buffers
))))
26106 (defvar org-after-save-iCalendar-file-hook nil
26107 "Hook run after an iCalendar file has been saved.
26108 The iCalendar buffer is still current when this hook is run.
26109 A good way to use this is to tell a desktop calenndar application to re-read
26110 the iCalendar file.")
26112 (defun org-print-icalendar-entries (&optional combine
)
26113 "Print iCalendar entries for the current Org-mode file to `standard-output'.
26114 When COMBINE is non nil, add the category to each line."
26115 (let ((re1 (concat org-ts-regexp
"\\|<%%([^>\n]+>"))
26116 (re2 (concat "--?-?\\(" org-ts-regexp
"\\)"))
26117 (dts (org-ical-ts-to-string
26118 (format-time-string (cdr org-time-stamp-formats
) (current-time))
26120 hd ts ts2 state status
(inc t
) pos b sexp rrule
26121 scheduledp deadlinep tmp pri category entry location summary desc
26122 (sexp-buffer (get-buffer-create "*ical-tmp*")))
26123 (org-refresh-category-properties)
26125 (goto-char (point-min))
26126 (while (re-search-forward re1 nil t
)
26129 (setq pos
(match-beginning 0)
26130 ts
(match-string 0)
26132 hd
(org-get-heading)
26133 summary
(org-icalendar-cleanup-string
26134 (org-entry-get nil
"SUMMARY"))
26135 desc
(org-icalendar-cleanup-string
26136 (or (org-entry-get nil
"DESCRIPTION")
26137 (and org-icalendar-include-body
(org-get-entry)))
26138 t org-icalendar-include-body
)
26139 location
(org-icalendar-cleanup-string
26140 (org-entry-get nil
"LOCATION"))
26141 category
(org-get-category))
26142 (if (looking-at re2
)
26144 (goto-char (match-end 0))
26145 (setq ts2
(match-string 1) inc nil
))
26146 (setq tmp
(buffer-substring (max (point-min)
26147 (- pos org-ds-keyword-length
))
26149 ts2
(if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts
)
26152 (replace-match "\\1" t nil ts
))
26154 deadlinep
(string-match org-deadline-regexp tmp
)
26155 scheduledp
(string-match org-scheduled-regexp tmp
)
26156 ;; donep (org-entry-is-done-p)
26158 (if (or (string-match org-tr-regexp hd
)
26159 (string-match org-ts-regexp hd
))
26160 (setq hd
(replace-match "" t t hd
)))
26161 (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts
)
26163 (concat "\nRRULE:FREQ="
26165 (match-string 2 ts
)
26166 '(("d" .
"DAILY")("w" .
"WEEKLY")
26167 ("m" .
"MONTHLY")("y" .
"YEARLY"))))
26168 ";INTERVAL=" (match-string 1 ts
)))
26170 (setq summary
(or summary hd
))
26171 (if (string-match org-bracket-link-regexp summary
)
26173 (replace-match (if (match-end 3)
26174 (match-string 3 summary
)
26175 (match-string 1 summary
))
26177 (if deadlinep
(setq summary
(concat "DL: " summary
)))
26178 (if scheduledp
(setq summary
(concat "S: " summary
)))
26179 (if (string-match "\\`<%%" ts
)
26180 (with-current-buffer sexp-buffer
26181 (insert (substring ts
1 -
1) " " summary
"\n"))
26182 (princ (format "BEGIN:VEVENT
26188 (org-ical-ts-to-string ts
"DTSTART")
26189 (org-ical-ts-to-string ts2
"DTEND" inc
)
26191 (if (and desc
(string-match "\\S-" desc
))
26192 (concat "\nDESCRIPTION: " desc
) "")
26193 (if (and location
(string-match "\\S-" location
))
26194 (concat "\nLOCATION: " location
) "")
26197 (when (and org-icalendar-include-sexps
26198 (condition-case nil
(require 'icalendar
) (error nil
))
26199 (fboundp 'icalendar-export-region
))
26200 ;; Get all the literal sexps
26201 (goto-char (point-min))
26202 (while (re-search-forward "^&?%%(" nil t
)
26205 (setq b
(match-beginning 0))
26206 (goto-char (1- (match-end 0)))
26209 (setq sexp
(buffer-substring b
(point)))
26210 (with-current-buffer sexp-buffer
26211 (insert sexp
"\n"))
26212 (princ (org-diary-to-ical-string sexp-buffer
)))))
26214 (when org-icalendar-include-todo
26215 (goto-char (point-min))
26216 (while (re-search-forward org-todo-line-regexp nil t
)
26219 (setq state
(match-string 2))
26220 (setq status
(if (member state org-done-keywords
)
26221 "COMPLETED" "NEEDS-ACTION"))
26223 (or (not (member state org-done-keywords
))
26224 (eq org-icalendar-include-todo
'all
))
26225 (not (member org-archive-tag
(org-get-tags-at)))
26227 (setq hd
(match-string 3)
26228 summary
(org-icalendar-cleanup-string
26229 (org-entry-get nil
"SUMMARY"))
26230 desc
(org-icalendar-cleanup-string
26231 (or (org-entry-get nil
"DESCRIPTION")
26232 (and org-icalendar-include-body
(org-get-entry)))
26233 t org-icalendar-include-body
)
26234 location
(org-icalendar-cleanup-string
26235 (org-entry-get nil
"LOCATION")))
26236 (if (string-match org-bracket-link-regexp hd
)
26237 (setq hd
(replace-match (if (match-end 3) (match-string 3 hd
)
26238 (match-string 1 hd
))
26240 (if (string-match org-priority-regexp hd
)
26241 (setq pri
(string-to-char (match-string 2 hd
))
26242 hd
(concat (substring hd
0 (match-beginning 1))
26243 (substring hd
(match-end 1))))
26244 (setq pri org-default-priority
))
26245 (setq pri
(floor (1+ (* 8.
(/ (float (- org-lowest-priority pri
))
26246 (- org-lowest-priority org-highest-priority
))))))
26248 (princ (format "BEGIN:VTODO
26258 (if (and location
(string-match "\\S-" location
))
26259 (concat "\nLOCATION: " location
) "")
26260 (if (and desc
(string-match "\\S-" desc
))
26261 (concat "\nDESCRIPTION: " desc
) "")
26262 category pri status
)))))))))
26264 (defun org-icalendar-cleanup-string (s &optional is-body maxlength
)
26265 "Take out stuff and quote what needs to be quoted.
26266 When IS-BODY is non-nil, assume that this is the body of an item, clean up
26267 whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
26272 (let ((re (concat "\\(" org-drawer-regexp
"\\)[^\000]*?:END:.*\n?"))
26273 (re2 (concat "^[ \t]*" org-keyword-time-regexp
".*\n?")))
26274 (while (string-match re s
) (setq s
(replace-match "" t t s
)))
26275 (while (string-match re2 s
) (setq s
(replace-match "" t t s
)))))
26277 (while (string-match "\\([,;\\]\\)" s start
)
26278 (setq start
(+ (match-beginning 0) 2)
26279 s
(replace-match "\\\\\\1" nil nil s
))))
26281 (while (string-match "[ \t]*\n[ \t]*" s
)
26282 (setq s
(replace-match "\\n" t t s
))))
26283 (setq s
(org-trim s
))
26286 (if (and (numberp maxlength
)
26287 (> (length s
) maxlength
))
26288 (setq s
(substring s
0 maxlength
)))))
26291 (defun org-get-entry ()
26292 "Clean-up description string."
26294 (org-back-to-heading t
)
26295 (buffer-substring (point-at-bol 2) (org-end-of-subtree t
))))
26297 (defun org-start-icalendar-file (name)
26298 "Start an iCalendar file by inserting the header."
26299 (let ((user user-full-name
)
26300 (name (or name
"unknown"))
26301 (timezone (cadr (current-time-zone))))
26303 (format "BEGIN:VCALENDAR
26306 PRODID:-//%s//Emacs with Org-mode//EN
26308 CALSCALE:GREGORIAN\n" name user timezone
))))
26310 (defun org-finish-icalendar-file ()
26311 "Finish an iCalendar file by inserting the END statement."
26312 (princ "END:VCALENDAR\n"))
26314 (defun org-ical-ts-to-string (s keyword
&optional inc
)
26315 "Take a time string S and convert it to iCalendar format.
26316 KEYWORD is added in front, to make a complete line like DTSTART....
26317 When INC is non-nil, increase the hour by two (if time string contains
26318 a time), or the day by one (if it does not contain a time)."
26319 (let ((t1 (org-parse-time-string s
'nodefault
))
26320 t2 fmt have-time time
)
26321 (if (and (car t1
) (nth 1 t1
) (nth 2 t1
))
26322 (setq t2 t1 have-time t
)
26323 (setq t2
(org-parse-time-string s
)))
26324 (let ((s (car t2
)) (mi (nth 1 t2
)) (h (nth 2 t2
))
26325 (d (nth 3 t2
)) (m (nth 4 t2
)) (y (nth 5 t2
)))
26328 (if org-agenda-default-appointment-duration
26329 (setq mi
(+ org-agenda-default-appointment-duration mi
))
26332 (setq time
(encode-time s mi h d m y
)))
26333 (setq fmt
(if have-time
":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
26334 (concat keyword
(format-time-string fmt time
))))
26338 (defun org-export-as-xoxo-insert-into (buffer &rest output
)
26339 (with-current-buffer buffer
26340 (apply 'insert output
)))
26341 (put 'org-export-as-xoxo-insert-into
'lisp-indent-function
1)
26343 (defun org-export-as-xoxo (&optional buffer
)
26344 "Export the org buffer as XOXO.
26345 The XOXO buffer is named *xoxo-<source buffer name>*"
26346 (interactive (list (current-buffer)))
26347 ;; A quickie abstraction
26349 ;; Output everything as XOXO
26350 (with-current-buffer (get-buffer buffer
)
26351 (let* ((pos (point))
26352 (opt-plist (org-combine-plists (org-default-export-plist)
26353 (org-infile-export-plist)))
26354 (filename (concat (file-name-as-directory
26355 (org-export-directory :xoxo opt-plist
))
26356 (file-name-sans-extension
26357 (file-name-nondirectory buffer-file-name
))
26359 (out (find-file-noselect filename
))
26362 (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
26363 ;; Check the output buffer is empty.
26364 (with-current-buffer out
(erase-buffer))
26365 ;; Kick off the output
26366 (org-export-as-xoxo-insert-into out
"<ol class='xoxo'>\n")
26367 (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't
)
26368 (let* ((hd (match-string-no-properties 1))
26369 (level (length hd
))
26371 (match-string-no-properties 2)
26373 (goto-char (match-end 0))
26378 (if (looking-at "^[ \t]\\(.*\\)")
26379 (setq str
(concat str
(match-string-no-properties 1)))
26380 (throw 'loop str
)))))))))
26382 ;; Handle level rendering
26384 ((> level last-level
)
26385 (org-export-as-xoxo-insert-into out
"\n<ol>\n"))
26387 ((< level last-level
)
26388 (dotimes (- (- last-level level
) 1)
26390 (org-export-as-xoxo-insert-into out
"</li>\n"))
26391 (org-export-as-xoxo-insert-into out
"</ol>\n"))
26393 (org-export-as-xoxo-insert-into out
"</li>\n")
26394 (setq hanging-li nil
)))
26396 ((equal level last-level
)
26398 (org-export-as-xoxo-insert-into out
"</li>\n")))
26401 (setq last-level level
)
26403 ;; And output the new li
26404 (setq hanging-li
't
)
26405 (if (equal ?
+ (elt text
0))
26406 (org-export-as-xoxo-insert-into out
"<li class='" (substring text
1) "'>")
26407 (org-export-as-xoxo-insert-into out
"<li>" text
))))
26409 ;; Finally finish off the ol
26410 (dotimes (- last-level
1)
26412 (org-export-as-xoxo-insert-into out
"</li>\n"))
26413 (org-export-as-xoxo-insert-into out
"</ol>\n"))
26416 ;; Finish the buffer off and clean it up.
26417 (switch-to-buffer-other-window out
)
26418 (indent-region (point-min) (point-max) nil
)
26420 (goto-char (point-min))
26426 ;; Make `C-c C-x' a prefix key
26427 (org-defkey org-mode-map
"\C-c\C-x" (make-sparse-keymap))
26429 ;; TAB key with modifiers
26430 (org-defkey org-mode-map
"\C-i" 'org-cycle
)
26431 (org-defkey org-mode-map
[(tab)] 'org-cycle
)
26432 (org-defkey org-mode-map
[(control tab
)] 'org-force-cycle-archived
)
26433 (org-defkey org-mode-map
[(meta tab
)] 'org-complete
)
26434 (org-defkey org-mode-map
"\M-\t" 'org-complete
)
26435 (org-defkey org-mode-map
"\M-\C-i" 'org-complete
)
26436 ;; The following line is necessary under Suse GNU/Linux
26437 (unless (featurep 'xemacs
)
26438 (org-defkey org-mode-map
[S-iso-lefttab
] 'org-shifttab
))
26439 (org-defkey org-mode-map
[(shift tab
)] 'org-shifttab
)
26440 (define-key org-mode-map
[backtab] 'org-shifttab)
26442 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
26443 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
26444 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
26446 ;; Cursor keys with modifiers
26447 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
26448 (org-defkey org-mode-map [(meta right)] 'org-metaright)
26449 (org-defkey org-mode-map [(meta up)] 'org-metaup)
26450 (org-defkey org-mode-map [(meta down)] 'org-metadown)
26452 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
26453 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
26454 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
26455 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
26457 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
26458 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
26459 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
26460 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
26462 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
26463 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
26465 ;;; Extra keys for tty access.
26466 ;; We only set them when really needed because otherwise the
26467 ;; menus don't show the simple keys
26469 (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
26470 (not window-system))
26471 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
26472 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
26473 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
26474 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
26475 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
26476 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
26477 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
26478 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
26479 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
26480 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
26481 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
26482 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
26483 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
26484 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
26485 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
26486 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
26487 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
26488 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
26489 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
26490 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
26491 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
26492 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
26494 ;; All the other keys
26496 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
26497 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
26498 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
26499 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
26500 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
26501 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
26502 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
26503 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
26504 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
26505 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
26506 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
26507 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
26508 (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
26509 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
26510 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
26511 (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
26512 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
26513 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
26514 (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
26515 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
26516 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
26517 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
26518 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
26519 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
26520 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
26521 (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
26522 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
26523 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
26524 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
26525 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
26526 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
26527 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
26528 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
26529 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
26530 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
26531 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
26532 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
26533 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
26534 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
26535 (org-defkey org-mode-map "\C-c^" 'org-sort)
26536 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
26537 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
26538 (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
26539 (org-defkey org-mode-map "\C-m" 'org-return)
26540 (org-defkey org-mode-map "\C-j" 'org-return-indent)
26541 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
26542 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
26543 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
26544 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
26545 (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
26546 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
26547 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
26548 (org-defkey org-mode-map "\C-c*" 'org-table-recalculate)
26549 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
26550 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
26551 (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
26552 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
26553 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
26554 (org-defkey org-mode-map "\C-c\C-e" 'org-export)
26555 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
26556 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
26558 (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
26559 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
26560 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
26561 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
26563 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
26564 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
26565 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
26566 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
26567 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
26568 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
26569 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
26570 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
26571 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
26572 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
26573 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
26574 (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
26576 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
26578 (when (featurep 'xemacs)
26579 (org-defkey org-mode-map 'button3 'popup-mode-menu))
26581 (defsubst org-table-p () (org-at-table-p))
26583 (defun org-self-insert-command (N)
26584 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
26585 If the cursor is in a table looking at whitespace, the whitespace is
26586 overwritten, and the table is not marked as requiring realignment."
26588 (if (and (org-table-p)
26590 ;; check if we blank the field, and if that triggers align
26591 (and org-table-auto-blank-field
26592 (member last-command
26593 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
26594 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
26595 ;; got extra space, this field does not determine column width
26596 (let (org-table-may-need-update) (org-table-blank-field))
26597 ;; no extra space, this field may determine column width
26598 (org-table-blank-field)))
26601 (looking-at "[^|\n]* |"))
26602 (let (org-table-may-need-update)
26603 (goto-char (1- (match-end 0)))
26604 (delete-backward-char 1)
26605 (goto-char (match-beginning 0))
26606 (self-insert-command N))
26607 (setq org-table-may-need-update t)
26608 (self-insert-command N)
26609 (org-fix-tags-on-the-fly)))
26611 (defun org-fix-tags-on-the-fly ()
26612 (when (and (equal (char-after (point-at-bol)) ?*)
26613 (org-on-heading-p))
26614 (org-align-tags-here org-tags-column)))
26616 (defun org-delete-backward-char (N)
26617 "Like `delete-backward-char', insert whitespace at field end in tables.
26618 When deleting backwards, in tables this function will insert whitespace in
26619 front of the next \"|\" separator, to keep the table aligned. The table will
26620 still be marked for re-alignment if the field did fill the entire column,
26621 because, in this case the deletion might narrow the column."
26623 (if (and (org-table-p)
26625 (string-match "|" (buffer-substring (point-at-bol) (point)))
26626 (looking-at ".*?|"))
26627 (let ((pos (point))
26628 (noalign (looking-at "[^|\n\r]* |"))
26629 (c org-table-may-need-update))
26630 (backward-delete-char N)
26631 (skip-chars-forward "^|")
26633 (goto-char (1- pos))
26634 ;; noalign: if there were two spaces at the end, this field
26635 ;; does not determine the width of the column.
26636 (if noalign (setq org-table-may-need-update c)))
26637 (backward-delete-char N)
26638 (org-fix-tags-on-the-fly)))
26640 (defun org-delete-char (N)
26641 "Like `delete-char', but insert whitespace at field end in tables.
26642 When deleting characters, in tables this function will insert whitespace in
26643 front of the next \"|\" separator, to keep the table aligned. The table will
26644 still be marked for re-alignment if the field did fill the entire column,
26645 because, in this case the deletion might narrow the column."
26647 (if (and (org-table-p)
26649 (not (= (char-after) ?|))
26651 (if (looking-at ".*?|")
26652 (let ((pos (point))
26653 (noalign (looking-at "[^|\n\r]* |"))
26654 (c org-table-may-need-update))
26655 (replace-match (concat
26656 (substring (match-string 0) 1 -1)
26659 ;; noalign: if there were two spaces at the end, this field
26660 ;; does not determine the width of the column.
26661 (if noalign (setq org-table-may-need-update c)))
26664 (org-fix-tags-on-the-fly)))
26666 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
26667 (put 'org-self-insert-command 'delete-selection t)
26668 (put 'orgtbl-self-insert-command 'delete-selection t)
26669 (put 'org-delete-char 'delete-selection 'supersede)
26670 (put 'org-delete-backward-char 'delete-selection 'supersede)
26672 ;; Make `flyspell-mode' delay after some commands
26673 (put 'org-self-insert-command 'flyspell-delayed t)
26674 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
26675 (put 'org-delete-char 'flyspell-delayed t)
26676 (put 'org-delete-backward-char 'flyspell-delayed t)
26678 ;; Make pabbrev-mode expand after org-mode commands
26679 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
26680 (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
26682 ;; How to do this: Measure non-white length of current string
26683 ;; If equal to column width, we should realign.
26685 (defun org-remap (map &rest commands)
26686 "In MAP, remap the functions given in COMMANDS.
26687 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
26690 (setq old (pop commands) new (pop commands))
26691 (if (fboundp 'command-remapping)
26692 (org-defkey map (vector 'remap old) new)
26693 (substitute-key-definition old new map global-map)))))
26695 (when (eq org-enable-table-editor 'optimized)
26696 ;; If the user wants maximum table support, we need to hijack
26697 ;; some standard editing functions
26698 (org-remap org-mode-map
26699 'self-insert-command 'org-self-insert-command
26700 'delete-char 'org-delete-char
26701 'delete-backward-char 'org-delete-backward-char)
26702 (org-defkey org-mode-map "|" 'org-force-self-insert))
26704 (defun org-shiftcursor-error ()
26705 "Throw an error because Shift-Cursor command was applied in wrong context."
26706 (error "This command is active in special context like tables, headlines or timestamps"))
26708 (defun org-shifttab (&optional arg)
26709 "Global visibility cycling or move to previous table field.
26710 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
26712 See the individual commands for more information."
26715 ((org-at-table-p) (call-interactively 'org-table-previous-field))
26716 (arg (message "Content view to level: ")
26717 (org-content (prefix-numeric-value arg))
26718 (setq org-cycle-global-status 'overview))
26719 (t (call-interactively 'org-global-cycle))))
26721 (defun org-shiftmetaleft ()
26722 "Promote subtree or delete table column.
26723 Calls `org-promote-subtree', `org-outdent-item',
26724 or `org-table-delete-column', depending on context.
26725 See the individual commands for more information."
26728 ((org-at-table-p) (call-interactively 'org-table-delete-column))
26729 ((org-on-heading-p) (call-interactively 'org-promote-subtree))
26730 ((org-at-item-p) (call-interactively 'org-outdent-item))
26731 (t (org-shiftcursor-error))))
26733 (defun org-shiftmetaright ()
26734 "Demote subtree or insert table column.
26735 Calls `org-demote-subtree', `org-indent-item',
26736 or `org-table-insert-column', depending on context.
26737 See the individual commands for more information."
26740 ((org-at-table-p) (call-interactively 'org-table-insert-column))
26741 ((org-on-heading-p) (call-interactively 'org-demote-subtree))
26742 ((org-at-item-p) (call-interactively 'org-indent-item))
26743 (t (org-shiftcursor-error))))
26745 (defun org-shiftmetaup (&optional arg)
26746 "Move subtree up or kill table row.
26747 Calls `org-move-subtree-up' or `org-table-kill-row' or
26748 `org-move-item-up' depending on context. See the individual commands
26749 for more information."
26752 ((org-at-table-p) (call-interactively 'org-table-kill-row))
26753 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
26754 ((org-at-item-p) (call-interactively 'org-move-item-up))
26755 (t (org-shiftcursor-error))))
26756 (defun org-shiftmetadown (&optional arg)
26757 "Move subtree down or insert table row.
26758 Calls `org-move-subtree-down' or `org-table-insert-row' or
26759 `org-move-item-down', depending on context. See the individual
26760 commands for more information."
26763 ((org-at-table-p) (call-interactively 'org-table-insert-row))
26764 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
26765 ((org-at-item-p) (call-interactively 'org-move-item-down))
26766 (t (org-shiftcursor-error))))
26768 (defun org-metaleft (&optional arg)
26769 "Promote heading or move table column to left.
26770 Calls `org-do-promote' or `org-table-move-column', depending on context.
26771 With no specific context, calls the Emacs default `backward-word'.
26772 See the individual commands for more information."
26775 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
26776 ((or (org-on-heading-p) (org-region-active-p))
26777 (call-interactively 'org-do-promote))
26778 ((org-at-item-p) (call-interactively 'org-outdent-item))
26779 (t (call-interactively 'backward-word))))
26781 (defun org-metaright (&optional arg)
26782 "Demote subtree or move table column to right.
26783 Calls `org-do-demote' or `org-table-move-column', depending on context.
26784 With no specific context, calls the Emacs default `forward-word'.
26785 See the individual commands for more information."
26788 ((org-at-table-p) (call-interactively 'org-table-move-column))
26789 ((or (org-on-heading-p) (org-region-active-p))
26790 (call-interactively 'org-do-demote))
26791 ((org-at-item-p) (call-interactively 'org-indent-item))
26792 (t (call-interactively 'forward-word))))
26794 (defun org-metaup (&optional arg)
26795 "Move subtree up or move table row up.
26796 Calls `org-move-subtree-up' or `org-table-move-row' or
26797 `org-move-item-up', depending on context. See the individual commands
26798 for more information."
26801 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
26802 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
26803 ((org-at-item-p) (call-interactively 'org-move-item-up))
26804 (t (transpose-lines 1) (beginning-of-line -1))))
26806 (defun org-metadown (&optional arg)
26807 "Move subtree down or move table row down.
26808 Calls `org-move-subtree-down' or `org-table-move-row' or
26809 `org-move-item-down', depending on context. See the individual
26810 commands for more information."
26813 ((org-at-table-p) (call-interactively 'org-table-move-row))
26814 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
26815 ((org-at-item-p) (call-interactively 'org-move-item-down))
26816 (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
26818 (defun org-shiftup (&optional arg)
26819 "Increase item in timestamp or increase priority of current headline.
26820 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
26821 depending on context. See the individual commands for more information."
26824 ((org-at-timestamp-p t)
26825 (call-interactively (if org-edit-timestamp-down-means-later
26826 'org-timestamp-down 'org-timestamp-up)))
26827 ((org-on-heading-p) (call-interactively 'org-priority-up))
26828 ((org-at-item-p) (call-interactively 'org-previous-item))
26829 (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
26831 (defun org-shiftdown (&optional arg)
26832 "Decrease item in timestamp or decrease priority of current headline.
26833 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
26834 depending on context. See the individual commands for more information."
26837 ((org-at-timestamp-p t)
26838 (call-interactively (if org-edit-timestamp-down-means-later
26839 'org-timestamp-up 'org-timestamp-down)))
26840 ((org-on-heading-p) (call-interactively 'org-priority-down))
26841 (t (call-interactively 'org-next-item))))
26843 (defun org-shiftright ()
26844 "Next TODO keyword or timestamp one day later, depending on context."
26847 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
26848 ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
26849 ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
26850 ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
26851 (t (org-shiftcursor-error))))
26853 (defun org-shiftleft ()
26854 "Previous TODO keyword or timestamp one day earlier, depending on context."
26857 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
26858 ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
26859 ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
26860 ((org-at-property-p)
26861 (call-interactively 'org-property-previous-allowed-value))
26862 (t (org-shiftcursor-error))))
26864 (defun org-shiftcontrolright ()
26865 "Switch to next TODO set."
26868 ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
26869 (t (org-shiftcursor-error))))
26871 (defun org-shiftcontrolleft ()
26872 "Switch to previous TODO set."
26875 ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
26876 (t (org-shiftcursor-error))))
26878 (defun org-ctrl-c-ret ()
26879 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
26882 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
26883 (t (call-interactively 'org-insert-heading))))
26885 (defun org-copy-special ()
26886 "Copy region in table or copy current subtree.
26887 Calls `org-table-copy' or `org-copy-subtree', depending on context.
26888 See the individual commands for more information."
26890 (call-interactively
26891 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
26893 (defun org-cut-special ()
26894 "Cut region in table or cut current subtree.
26895 Calls `org-table-copy' or `org-cut-subtree', depending on context.
26896 See the individual commands for more information."
26898 (call-interactively
26899 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
26901 (defun org-paste-special (arg)
26902 "Paste rectangular region into table, or past subtree relative to level.
26903 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
26904 See the individual commands for more information."
26906 (if (org-at-table-p)
26907 (org-table-paste-rectangle)
26908 (org-paste-subtree arg)))
26910 (defun org-ctrl-c-ctrl-c (&optional arg)
26911 "Set tags in headline, or update according to changed information at point.
26913 This command does many different things, depending on context:
26915 - If the cursor is in a headline, prompt for tags and insert them
26916 into the current line, aligned to `org-tags-column'. When called
26917 with prefix arg, realign all tags in the current buffer.
26919 - If the cursor is in one of the special #+KEYWORD lines, this
26920 triggers scanning the buffer for these lines and updating the
26923 - If the cursor is inside a table, realign the table. This command
26924 works even if the automatic table editor has been turned off.
26926 - If the cursor is on a #+TBLFM line, re-apply the formulas to
26929 - If the cursor is a the beginning of a dynamic block, update it.
26931 - If the cursor is inside a table created by the table.el package,
26932 activate that table.
26934 - If the current buffer is a remember buffer, close note and file it.
26935 with a prefix argument, file it without further interaction to the default
26938 - If the cursor is on a <<<target>>>, update radio targets and corresponding
26939 links in this buffer.
26941 - If the cursor is on a numbered item in a plain list, renumber the
26944 - If the cursor is on a checkbox, toggle it."
26946 (let ((org-enable-table-editor t))
26948 ((or org-clock-overlays
26949 org-occur-highlights
26950 org-latex-fragment-image-overlays)
26951 (org-remove-clock-overlays)
26952 (org-remove-occur-highlights)
26953 (org-remove-latex-fragment-image-overlays)
26954 (message "Temporary highlights/overlays removed from current buffer"))
26955 ((and (local-variable-p 'org-finish-function (current-buffer))
26956 (fboundp org-finish-function))
26957 (funcall org-finish-function))
26958 ((org-at-property-p)
26959 (call-interactively 'org-property-action))
26960 ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
26961 ((org-on-heading-p) (call-interactively 'org-set-tags))
26962 ((org-at-table.el-p)
26964 (beginning-of-line 1)
26965 (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
26966 (call-interactively 'table-recognize-table))
26968 (org-table-maybe-eval-formula)
26970 (call-interactively 'org-table-recalculate)
26971 (org-table-maybe-recalculate-line))
26972 (call-interactively 'org-table-align))
26973 ((org-at-item-checkbox-p)
26974 (call-interactively 'org-toggle-checkbox))
26976 (call-interactively 'org-maybe-renumber-ordered-list))
26977 ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
26979 (beginning-of-line 1)
26980 (org-update-dblock))
26981 ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
26983 ((equal (match-string 1) "TBLFM")
26984 ;; Recalculate the table before this line
26986 (beginning-of-line 1)
26987 (skip-chars-backward " \r\n\t")
26988 (if (org-at-table-p)
26989 (org-call-with-arg 'org-table-recalculate t))))
26991 (call-interactively 'org-mode-restart))))
26992 (t (error "C-c C-c can do nothing useful at this location.")))))
26994 (defun org-mode-restart ()
26995 "Restart Org-mode, to scan again for special lines.
26996 Also updates the keyword regular expressions."
26998 (let ((org-inhibit-startup t)) (org-mode))
26999 (message "Org-mode restarted to refresh keyword and special line setup"))
27001 (defun org-kill-note-or-show-branches ()
27002 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
27004 (if (not org-finish-function)
27005 (call-interactively 'show-branches)
27006 (let ((org-note-abort t))
27007 (funcall org-finish-function))))
27009 (defun org-return (&optional indent)
27010 "Goto next table row or insert a newline.
27011 Calls `org-table-next-row' or `newline', depending on context.
27012 See the individual commands for more information."
27015 ((bobp) (if indent (newline-and-indent) (newline)))
27016 ((and (org-at-heading-p)
27018 (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
27023 (org-table-justify-field-maybe)
27024 (call-interactively 'org-table-next-row))
27025 (t (if indent (newline-and-indent) (newline)))))
27027 (defun org-return-indent ()
27029 "Goto next table row or insert a newline and indent.
27030 Calls `org-table-next-row' or `newline-and-indent', depending on
27031 context. See the individual commands for more information."
27034 (defun org-ctrl-c-minus ()
27035 "Insert separator line in table or modify bullet type in list.
27036 Calls `org-table-insert-hline' or `org-cycle-list-bullet',
27037 depending on context."
27041 (call-interactively 'org-table-insert-hline))
27042 ((org-on-heading-p)
27045 (beginning-of-line 1)
27046 (if (looking-at "\\*+ ")
27047 (replace-match (concat (make-string (- (match-end 0) (point)) ?\ ) "- ")))))
27049 (call-interactively 'org-cycle-list-bullet))
27050 (t (error "`C-c -' does have no function here."))))
27052 (defun org-meta-return (&optional arg)
27053 "Insert a new heading or wrap a region in a table.
27054 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
27055 See the individual commands for more information."
27059 (call-interactively 'org-table-wrap-region))
27060 (t (call-interactively 'org-insert-heading))))
27064 ;; Define the Org-mode menus
27065 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
27067 ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
27068 ["Next Field" org-cycle (org-at-table-p)]
27069 ["Previous Field" org-shifttab (org-at-table-p)]
27070 ["Next Row" org-return (org-at-table-p)]
27072 ["Blank Field" org-table-blank-field (org-at-table-p)]
27073 ["Edit Field" org-table-edit-field (org-at-table-p)]
27074 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
27077 ["Move Column Left" org-metaleft (org-at-table-p)]
27078 ["Move Column Right" org-metaright (org-at-table-p)]
27079 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
27080 ["Insert Column" org-shiftmetaright (org-at-table-p)])
27082 ["Move Row Up" org-metaup (org-at-table-p)]
27083 ["Move Row Down" org-metadown (org-at-table-p)]
27084 ["Delete Row" org-shiftmetaup (org-at-table-p)]
27085 ["Insert Row" org-shiftmetadown (org-at-table-p)]
27086 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
27088 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
27090 ["Copy Rectangle" org-copy-special (org-at-table-p)]
27091 ["Cut Rectangle" org-cut-special (org-at-table-p)]
27092 ["Paste Rectangle" org-paste-special (org-at-table-p)]
27093 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
27096 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
27097 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
27098 ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
27100 ["Recalculate line" org-table-recalculate (org-at-table-p)]
27101 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
27102 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
27104 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
27106 ["Sum Column/Rectangle" org-table-sum
27107 (or (org-at-table-p) (org-region-active-p))]
27108 ["Which Column?" org-table-current-column (org-at-table-p)])
27110 org-table-toggle-formula-debugger
27111 :style toggle :selected org-table-formula-debug]
27112 ["Show Col/Row Numbers"
27113 org-table-toggle-coordinate-overlays
27114 :style toggle :selected org-table-overlay-coordinates]
27116 ["Create" org-table-create (and (not (org-at-table-p))
27117 org-enable-table-editor)]
27118 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
27119 ["Import from File" org-table-import (not (org-at-table-p))]
27120 ["Export to File" org-table-export (org-at-table-p)]
27122 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
27124 (easy-menu-define org-org-menu org-mode-map "Org menu"
27127 ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
27128 ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
27129 ["Sparse Tree" org-occur t]
27130 ["Reveal Context" org-reveal t]
27131 ["Show All" show-all t]
27133 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
27135 ["New Heading" org-insert-heading t]
27136 ("Navigate Headings"
27137 ["Up" outline-up-heading t]
27138 ["Next" outline-next-visible-heading t]
27139 ["Previous" outline-previous-visible-heading t]
27140 ["Next Same Level" outline-forward-same-level t]
27141 ["Previous Same Level" outline-backward-same-level t]
27143 ["Jump" org-goto t])
27145 ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
27146 ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
27148 ["Copy Subtree" org-copy-special (not (org-at-table-p))]
27149 ["Cut Subtree" org-cut-special (not (org-at-table-p))]
27150 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
27152 ["Promote Heading" org-metaleft (not (org-at-table-p))]
27153 ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
27154 ["Demote Heading" org-metaright (not (org-at-table-p))]
27155 ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
27157 ["Sort Region/Children" org-sort (not (org-at-table-p))]
27159 ["Convert to odd levels" org-convert-to-odd-levels t]
27160 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
27162 ["Emphasis..." org-emphasize t])
27164 ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
27165 ; ["Check and Tag Children" (org-toggle-archive-tag (4))
27166 ; :active t :keys "C-u C-c C-x C-a"]
27167 ["Sparse trees open ARCHIVE trees"
27168 (setq org-sparse-tree-open-archived-trees
27169 (not org-sparse-tree-open-archived-trees))
27170 :style toggle :selected org-sparse-tree-open-archived-trees]
27171 ["Cycling opens ARCHIVE trees"
27172 (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
27173 :style toggle :selected org-cycle-open-archived-trees]
27174 ["Agenda includes ARCHIVE trees"
27175 (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
27176 :style toggle :selected (not org-agenda-skip-archived-trees)]
27178 ["Move Subtree to Archive" org-advertized-archive-subtree t]
27179 ; ["Check and Move Children" (org-archive-subtree '(4))
27180 ; :active t :keys "C-u C-c C-x C-s"]
27184 ["TODO/DONE/-" org-todo t]
27186 ["Next keyword" org-shiftright (org-on-heading-p)]
27187 ["Previous keyword" org-shiftleft (org-on-heading-p)]
27188 ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
27189 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
27190 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
27191 ["Show TODO Tree" org-show-todo-tree t]
27192 ["Global TODO list" org-todo-list t]
27194 ["Set Priority" org-priority t]
27195 ["Priority Up" org-shiftup t]
27196 ["Priority Down" org-shiftdown t])
27197 ("TAGS and Properties"
27198 ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
27199 ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
27201 ["Set property" 'org-set-property t]
27202 ["Column view of properties" org-columns t]
27203 ["Insert Column View DBlock" org-insert-columns-dblock t])
27204 ("Dates and Scheduling"
27205 ["Timestamp" org-time-stamp t]
27206 ["Timestamp (inactive)" org-time-stamp-inactive t]
27208 ["1 Day Later" org-shiftright t]
27209 ["1 Day Earlier" org-shiftleft t]
27210 ["1 ... Later" org-shiftup t]
27211 ["1 ... Earlier" org-shiftdown t])
27212 ["Compute Time Range" org-evaluate-time-range t]
27213 ["Schedule Item" org-schedule t]
27214 ["Deadline" org-deadline t]
27216 ["Custom time format" org-toggle-time-stamp-overlays
27217 :style radio :selected org-display-custom-times]
27219 ["Goto Calendar" org-goto-calendar t]
27220 ["Date from Calendar" org-date-from-calendar t])
27222 ["Clock in" org-clock-in t]
27223 ["Clock out" org-clock-out t]
27224 ["Clock cancel" org-clock-cancel t]
27225 ["Goto running clock" org-clock-goto t]
27226 ["Display times" org-clock-display t]
27227 ["Create clock table" org-clock-report t]
27229 ["Record DONE time"
27230 (progn (setq org-log-done (not org-log-done))
27231 (message "Switching to %s will %s record a timestamp"
27232 (car org-done-keywords)
27233 (if org-log-done "automatically" "not")))
27234 :style toggle :selected org-log-done])
27236 ["Agenda Command..." org-agenda t]
27237 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
27238 ("File List for Agenda")
27239 ("Special views current file"
27240 ["TODO Tree" org-show-todo-tree t]
27241 ["Check Deadlines" org-check-deadlines t]
27242 ["Timeline" org-timeline t]
27243 ["Tags Tree" org-tags-sparse-tree t])
27246 ["Store Link (Global)" org-store-link t]
27247 ["Insert Link" org-insert-link t]
27248 ["Follow Link" org-open-at-point t]
27250 ["Next link" org-next-link t]
27251 ["Previous link" org-previous-link t]
27253 ["Descriptive Links"
27254 (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
27255 :style radio :selected (member '(org-link) buffer-invisibility-spec)]
27258 (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
27259 :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
27261 ["Export/Publish..." org-export t]
27263 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
27264 :selected org-cdlatex-mode]
27265 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
27266 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
27267 ["Modify math symbol" org-cdlatex-math-modify
27268 (org-inside-LaTeX-fragment-p)]
27269 ["Export LaTeX fragments as images"
27270 (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
27271 :style toggle :selected org-export-with-LaTeX-fragments])
27274 ["Show Version" org-version t]
27275 ["Info Documentation" org-info t])
27277 ["Browse Org Group" org-customize t]
27279 ["Expand This Menu" org-create-customize-menu
27280 (fboundp 'customize-menu-create)])
27282 ["Refresh setup" org-mode-restart t]
27285 (defun org-info (&optional node)
27286 "Read documentation for Org-mode in the info system.
27287 With optional NODE, go directly to that node."
27290 (Info-goto-node (format "(org)%s" (or node ""))))
27292 (defun org-install-agenda-files-menu ()
27293 (let ((bl (buffer-list)))
27296 (set-buffer (pop bl))
27297 (if (org-mode-p) (setq bl nil)))
27300 '("Org") "File List for Agenda"
27303 ["Edit File List" (org-edit-agenda-file-list) t]
27304 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
27305 ["Remove Current File from List" org-remove-file t]
27306 ["Cycle through agenda files" org-cycle-agenda-files t]
27307 ["Occur in all agenda files" org-occur-in-agenda-files t]
27309 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
27313 (defun org-customize ()
27314 "Call the customize function with org as argument."
27316 (customize-browse 'org))
27318 (defun org-create-customize-menu ()
27319 "Create a full customization menu for Org-mode, insert it into the menu."
27321 (if (fboundp 'customize-menu-create)
27324 '("Org") "Customize"
27325 `(["Browse Org group" org-customize t]
27327 ,(customize-menu-create 'org)
27328 ["Set" Custom-set t]
27329 ["Save" Custom-save t]
27330 ["Reset to Current" Custom-reset-current t]
27331 ["Reset to Saved" Custom-reset-saved t]
27332 ["Reset to Standard Settings" Custom-reset-standard t]))
27333 (message "\"Org\"-menu now contains full customization menu"))
27334 (error "Cannot expand menu (outdated version of cus-edit.el)")))
27336 ;;;; Miscellaneous stuff
27339 ;;; Generally useful functions
27341 (defun org-context ()
27342 "Return a list of contexts of the current cursor position.
27343 If several contexts apply, all are returned.
27344 Each context entry is a list with a symbol naming the context, and
27345 two positions indicating start and end of the context. Possible
27348 :headline anywhere in a headline
27349 :headline-stars on the leading stars in a headline
27350 :todo-keyword on a TODO keyword (including DONE) in a headline
27351 :tags on the TAGS in a headline
27352 :priority on the priority cookie in a headline
27353 :item on the first line of a plain list item
27354 :item-bullet on the bullet/number of a plain list item
27355 :checkbox on the checkbox in a plain list item
27356 :table in an org-mode table
27357 :table-special on a special filed in a table
27358 :table-table in a table.el table
27359 :link on a hyperlink
27360 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
27361 :target on a <<target>>
27362 :radio-target on a <<<radio-target>>>
27363 :latex-fragment on a LaTeX fragment
27364 :latex-preview on a LaTeX fragment with overlayed preview image
27366 This function expects the position to be visible because it uses font-lock
27367 faces as a help to recognize the following contexts: :table-special, :link,
27369 (let* ((f (get-text-property (point) 'face))
27370 (faces (if (listp f) f (list f)))
27371 (p (point)) clist o)
27372 ;; First the large context
27374 ((org-on-heading-p t)
27375 (push (list :headline (point-at-bol) (point-at-eol)) clist)
27377 (beginning-of-line 1)
27378 (looking-at org-todo-line-tags-regexp))
27379 (push (org-point-in-group p 1 :headline-stars) clist)
27380 (push (org-point-in-group p 2 :todo-keyword) clist)
27381 (push (org-point-in-group p 4 :tags) clist))
27383 (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
27384 (if (looking-at "\\[#[A-Z0-9]\\]")
27385 (push (org-point-in-group p 0 :priority) clist)))
27388 (push (org-point-in-group p 2 :item-bullet) clist)
27389 (push (list :item (point-at-bol)
27390 (save-excursion (org-end-of-item) (point)))
27392 (and (org-at-item-checkbox-p)
27393 (push (org-point-in-group p 0 :checkbox) clist)))
27396 (push (list :table (org-table-begin) (org-table-end)) clist)
27397 (if (memq 'org-formula faces)
27398 (push (list :table-special
27399 (previous-single-property-change p 'face)
27400 (next-single-property-change p 'face)) clist)))
27401 ((org-at-table-p 'any)
27402 (push (list :table-table) clist)))
27405 ;; Now the small context
27407 ((org-at-timestamp-p)
27408 (push (org-point-in-group p 0 :timestamp) clist))
27409 ((memq 'org-link faces)
27411 (previous-single-property-change p 'face)
27412 (next-single-property-change p 'face)) clist))
27413 ((memq 'org-special-keyword faces)
27414 (push (list :keyword
27415 (previous-single-property-change p 'face)
27416 (next-single-property-change p 'face)) clist))
27418 (push (org-point-in-group p 0 :target) clist)
27419 (goto-char (1- (match-beginning 0)))
27420 (if (looking-at org-radio-target-regexp)
27421 (push (org-point-in-group p 0 :radio-target) clist))
27423 ((setq o (car (delq nil
27426 (if (memq x org-latex-fragment-image-overlays) x))
27427 (org-overlays-at (point))))))
27428 (push (list :latex-fragment
27429 (org-overlay-start o) (org-overlay-end o)) clist)
27430 (push (list :latex-preview
27431 (org-overlay-start o) (org-overlay-end o)) clist))
27432 ((org-inside-LaTeX-fragment-p)
27433 ;; FIXME: positions wrong.
27434 (push (list :latex-fragment (point) (point)) clist)))
27436 (setq clist (nreverse (delq nil clist)))
27439 ;; FIXME: Compare with at-regexp-p Do we need both?
27440 (defun org-in-regexp (re &optional nlines visually)
27441 "Check if point is inside a match of regexp.
27442 Normally only the current line is checked, but you can include NLINES extra
27443 lines both before and after point into the search.
27444 If VISUALLY is set, require that the cursor is not after the match but
27445 really on, so that the block visually is on the match."
27447 (let ((pos (point))
27448 (eol (point-at-eol (+ 1 (or nlines 0))))
27449 (inc (if visually 1 0)))
27451 (beginning-of-line (- 1 (or nlines 0)))
27452 (while (re-search-forward re eol t)
27453 (if (and (<= (match-beginning 0) pos)
27454 (>= (+ inc (match-end 0)) pos))
27455 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
27457 (defun org-at-regexp-p (regexp)
27458 "Is point inside a match of REGEXP in the current line?"
27461 (let ((pos (point)) (end (point-at-eol)))
27462 (beginning-of-line 1)
27463 (while (re-search-forward regexp end t)
27464 (if (and (<= (match-beginning 0) pos)
27465 (>= (match-end 0) pos))
27469 (defun org-occur-in-agenda-files (regexp &optional nlines)
27470 "Call `multi-occur' with buffers for all agenda files."
27471 (interactive "sOrg-files matching: \np")
27472 (let* ((files (org-agenda-files))
27473 (tnames (mapcar 'file-truename files))
27474 (extra org-agenda-multi-occur-extra-files)
27476 (while (setq f (pop extra))
27477 (unless (member (file-truename f) tnames)
27478 (add-to-list 'files f 'append)
27479 (add-to-list 'tnames (file-truename f) 'append)))
27481 (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
27484 (if (boundp 'occur-mode-find-occurrence-hook)
27486 (add-hook 'occur-mode-find-occurrence-hook
27491 (defadvice occur-mode-goto-occurrence
27492 (after org-occur-reveal activate)
27493 (and (org-mode-p) (org-reveal)))
27494 (defadvice occur-mode-goto-occurrence-other-window
27495 (after org-occur-reveal activate)
27496 (and (org-mode-p) (org-reveal)))
27497 (defadvice occur-mode-display-occurrence
27498 (after org-occur-reveal activate)
27500 (let ((pos (occur-mode-find-occurrence)))
27501 (with-current-buffer (marker-buffer pos)
27506 (defun org-uniquify (list)
27507 "Remove duplicate elements from LIST."
27509 (mapc (lambda (x) (add-to-list 'res x 'append)) list)
27512 (defun org-delete-all (elts list)
27513 "Remove all elements in ELTS from LIST."
27515 (setq list (delete (pop elts) list)))
27518 (defun org-back-over-empty-lines ()
27519 "Move backwards over witespace, to the beginning of the first empty line.
27520 Returns the number o empty lines passed."
27521 (let ((pos (point)))
27522 (skip-chars-backward " \t\n\r")
27523 (beginning-of-line 2)
27524 (goto-char (min (point) pos))
27525 (count-lines (point) pos)))
27527 (defun org-skip-whitespace ()
27528 (skip-chars-forward " \t\n\r"))
27530 (defun org-point-in-group (point group &optional context)
27531 "Check if POINT is in match-group GROUP.
27532 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
27533 match. If the match group does ot exist or point is not inside it,
27535 (and (match-beginning group)
27536 (>= point (match-beginning group))
27537 (<= point (match-end group))
27539 (list context (match-beginning group) (match-end group))
27542 (defun org-switch-to-buffer-other-window (&rest args)
27543 "Switch to buffer in a second window on the current frame.
27544 In particular, do not allow pop-up frames."
27545 (let (pop-up-frames special-display-buffer-names special-display-regexps
27546 special-display-function)
27547 (apply 'switch-to-buffer-other-window args)))
27549 (defun org-combine-plists (&rest plists)
27550 "Create a single property list from all plists in PLISTS.
27551 The process starts by copying the first list, and then setting properties
27552 from the other lists. Settings in the last list are the most significant
27553 ones and overrule settings in the other lists."
27554 (let ((rtn (copy-sequence (pop plists)))
27557 (setq ls (pop plists))
27559 (setq p (pop ls) v (pop ls))
27560 (setq rtn (plist-put rtn p v))))
27563 (defun org-move-line-down (arg)
27564 "Move the current line down. With prefix argument, move it past ARG lines."
27566 (let ((col (current-column))
27568 (beginning-of-line 1) (setq beg (point))
27569 (beginning-of-line 2) (setq end (point))
27570 (beginning-of-line (+ 1 arg))
27571 (setq pos (move-marker (make-marker) (point)))
27572 (insert (delete-and-extract-region beg end))
27574 (move-to-column col)))
27576 (defun org-move-line-up (arg)
27577 "Move the current line up. With prefix argument, move it past ARG lines."
27579 (let ((col (current-column))
27581 (beginning-of-line 1) (setq beg (point))
27582 (beginning-of-line 2) (setq end (point))
27583 (beginning-of-line (- arg))
27584 (setq pos (move-marker (make-marker) (point)))
27585 (insert (delete-and-extract-region beg end))
27587 (move-to-column col)))
27589 (defun org-replace-escapes (string table)
27590 "Replace %-escapes in STRING with values in TABLE.
27591 TABLE is an association list with keys like \"%a\" and string values.
27592 The sequences in STRING may contain normal field width and padding information,
27593 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
27594 so values can contain further %-escapes if they are define later in TABLE."
27595 (let ((case-fold-search nil)
27597 (while (setq e (pop table))
27598 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
27599 (while (string-match re string)
27600 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
27602 (setq string (replace-match rpl t t string))))
27606 (defun org-sublist (list start end)
27607 "Return a section of LIST, from START to END.
27608 Counting starts at 1."
27609 (let (rtn (c start))
27610 (setq list (nthcdr (1- start) list))
27611 (while (and list (<= c end))
27612 (push (pop list) rtn)
27616 (defun org-find-base-buffer-visiting (file)
27617 "Like `find-buffer-visiting' but alway return the base buffer and
27618 not an indirect buffer"
27619 (let ((buf (find-buffer-visiting file)))
27621 (or (buffer-base-buffer buf) buf)
27624 (defun org-image-file-name-regexp ()
27625 "Return regexp matching the file names of images."
27626 (if (fboundp 'image-file-name-regexp)
27627 (image-file-name-regexp)
27628 (let ((image-file-name-extensions
27629 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
27630 "xbm" "xpm" "pbm" "pgm" "ppm")))
27632 (regexp-opt (nconc (mapcar 'upcase
27633 image-file-name-extensions)
27634 image-file-name-extensions)
27638 (defun org-file-image-p (file)
27639 "Return non-nil if FILE is an image."
27641 (string-match (org-image-file-name-regexp) file)))
27643 ;;; Paragraph filling stuff.
27644 ;; We want this to be just right, so use the full arsenal.
27646 (defun org-indent-line-function ()
27647 "Indent line like previous, but further if previous was headline or item."
27649 (let* ((pos (point))
27650 (itemp (org-at-item-p))
27651 column bpos bcol tpos tcol bullet btype bullet-type)
27652 ;; Find the previous relevant line
27653 (beginning-of-line 1)
27655 ((looking-at "#") (setq column 0))
27656 ((looking-at "\\*+ ") (setq column 0))
27658 (beginning-of-line 0)
27659 (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
27660 (beginning-of-line 0))
27662 ((looking-at "\\*+[ \t]+")
27663 (goto-char (match-end 0))
27664 (setq column (current-column)))
27666 (org-beginning-of-item)
27667 ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
27668 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
27669 (setq bpos (match-beginning 1) tpos (match-end 0)
27670 bcol (progn (goto-char bpos) (current-column))
27671 tcol (progn (goto-char tpos) (current-column))
27672 bullet (match-string 1)
27673 bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
27677 (beginning-of-line 1)
27678 (if (looking-at "\\S-")
27680 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
27681 (setq bullet (match-string 1)
27682 btype (if (string-match "[0-9]" bullet) "n" bullet))
27683 (setq column (if (equal btype bullet-type) bcol tcol)))
27684 (setq column (org-get-indentation)))))
27685 (t (setq column (org-get-indentation))))))
27687 (if (<= (current-column) (current-indentation))
27688 (indent-line-to column)
27689 (save-excursion (indent-line-to column)))
27690 (setq column (current-column))
27691 (beginning-of-line 1)
27693 "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
27694 (replace-match (concat "\\1" (format org-property-format
27695 (match-string 2) (match-string 3)))
27697 (move-to-column column)))
27699 (defun org-set-autofill-regexps ()
27701 ;; In the paragraph separator we include headlines, because filling
27702 ;; text in a line directly attached to a headline would otherwise
27703 ;; fill the headline as well.
27704 (org-set-local 'comment-start-skip "^#+[ \t]*")
27705 (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
27706 ;; The paragraph starter includes hand-formatted lists.
27707 (org-set-local 'paragraph-start
27708 "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
27709 ;; Inhibit auto-fill for headers, tables and fixed-width lines.
27710 ;; But only if the user has not turned off tables or fixed-width regions
27712 'auto-fill-inhibit-regexp
27713 (concat "\\*+ \\|#\\+"
27714 "\\|[ \t]*" org-keyword-time-regexp
27715 (if (or org-enable-table-editor org-enable-fixed-width-editor)
27718 (if org-enable-table-editor "|" "")
27719 (if org-enable-fixed-width-editor ":" "")
27721 ;; We use our own fill-paragraph function, to make sure that tables
27722 ;; and fixed-width regions are not wrapped. That function will pass
27723 ;; through to `fill-paragraph' when appropriate.
27724 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
27725 ; Adaptive filling: To get full control, first make sure that
27726 ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
27727 (org-set-local 'adaptive-fill-regexp "\000")
27728 (org-set-local 'adaptive-fill-function
27729 'org-adaptive-fill-function)
27731 'align-mode-rules-list
27732 '((org-in-buffer-settings
27733 (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
27734 (modes . '(org-mode))))))
27736 (defun org-fill-paragraph (&optional justify)
27737 "Re-align a table, pass through to fill-paragraph if no table."
27738 (let ((table-p (org-at-table-p))
27739 (table.el-p (org-at-table.el-p)))
27740 (cond ((and (equal (char-after (point-at-bol)) ?*)
27741 (save-excursion (goto-char (point-at-bol))
27742 (looking-at outline-regexp)))
27743 t) ; skip headlines
27744 (table.el-p t) ; skip table.el tables
27745 (table-p (org-table-align) t) ; align org-mode tables
27746 (t nil)))) ; call paragraph-fill
27748 ;; For reference, this is the default value of adaptive-fill-regexp
27749 ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
27751 (defun org-adaptive-fill-function ()
27752 "Return a fill prefix for org-mode files.
27753 In particular, this makes sure hanging paragraphs for hand-formatted lists
27755 (cond ((looking-at "#[ \t]+")
27757 ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
27759 (goto-char (match-end 0))
27760 (make-string (current-column) ?\ )))
27763 ;;;; Functions extending outline functionality
27766 (defun org-beginning-of-line (&optional arg)
27767 "Go to the beginning of the current line. If that is invisible, continue
27768 to a visible line beginning. This makes the function of C-a more intuitive.
27769 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
27770 first attempt, and only move to after the tags when the cursor is already
27771 beyond the end of the headline."
27773 (let ((pos (point)))
27774 (beginning-of-line 1)
27778 (if (org-invisible-p)
27779 (while (and (not (bobp)) (org-invisible-p))
27781 (beginning-of-line 1))
27783 (when org-special-ctrl-a/e
27785 ((and (looking-at org-todo-line-regexp)
27786 (= (char-after (match-end 1)) ?\ ))
27788 (if (eq org-special-ctrl-a/e t)
27789 (cond ((> pos (match-beginning 3)) (match-beginning 3))
27790 ((= pos (point)) (match-beginning 3))
27792 (cond ((> pos (point)) (point))
27793 ((not (eq last-command this-command)) (point))
27794 (t (match-beginning 3))))))
27797 (if (eq org-special-ctrl-a/e t)
27798 (cond ((> pos (match-end 4)) (match-end 4))
27799 ((= pos (point)) (match-end 4))
27801 (cond ((> pos (point)) (point))
27802 ((not (eq last-command this-command)) (point))
27803 (t (match-end 4))))))))))
27805 (defun org-end-of-line (&optional arg)
27806 "Go to the end of the line.
27807 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
27808 first attempt, and only move to after the tags when the cursor is already
27809 beyond the end of the headline."
27811 (if (or (not org-special-ctrl-a/e)
27812 (not (org-on-heading-p)))
27814 (let ((pos (point)))
27815 (beginning-of-line 1)
27816 (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
27817 (if (eq org-special-ctrl-a/e t)
27818 (if (or (< pos (match-beginning 1))
27819 (= pos (match-end 0)))
27820 (goto-char (match-beginning 1))
27821 (goto-char (match-end 0)))
27822 (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
27823 (goto-char (match-end 0))
27824 (goto-char (match-beginning 1))))
27825 (end-of-line arg)))))
27827 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
27828 (define-key org-mode-map "\C-e" 'org-end-of-line)
27830 (defun org-kill-line (&optional arg)
27831 "Kill line, to tags or end of line."
27834 ((or (not org-special-ctrl-k)
27836 (not (org-on-heading-p)))
27837 (call-interactively 'kill-line))
27838 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
27839 (kill-region (point) (match-beginning 1))
27840 (org-set-tags nil t))
27841 (t (kill-region (point) (point-at-eol)))))
27843 (define-key org-mode-map "\C-k" 'org-kill-line)
27845 (defun org-invisible-p ()
27846 "Check if point is at a character currently not visible."
27847 ;; Early versions of noutline don't have `outline-invisible-p'.
27848 (if (fboundp 'outline-invisible-p)
27849 (outline-invisible-p)
27850 (get-char-property (point) 'invisible)))
27852 (defun org-invisible-p2 ()
27853 "Check if point is at a character currently not visible."
27855 (if (and (eolp) (not (bobp))) (backward-char 1))
27856 ;; Early versions of noutline don't have `outline-invisible-p'.
27857 (if (fboundp 'outline-invisible-p)
27858 (outline-invisible-p)
27859 (get-char-property (point) 'invisible))))
27861 (defalias 'org-back-to-heading 'outline-back-to-heading)
27862 (defalias 'org-on-heading-p 'outline-on-heading-p)
27863 (defalias 'org-at-heading-p 'outline-on-heading-p)
27864 (defun org-at-heading-or-item-p ()
27865 (or (org-on-heading-p) (org-at-item-p)))
27867 (defun org-on-target-p ()
27868 (or (org-in-regexp org-radio-target-regexp)
27869 (org-in-regexp org-target-regexp)))
27871 (defun org-up-heading-all (arg)
27872 "Move to the heading line of which the present line is a subheading.
27873 This function considers both visible and invisible heading lines.
27874 With argument, move up ARG levels."
27875 (if (fboundp 'outline-up-heading-all)
27876 (outline-up-heading-all arg) ; emacs 21 version of outline.el
27877 (outline-up-heading arg t))) ; emacs 22 version of outline.el
27879 (defun org-up-heading-safe ()
27880 "Move to the heading line of which the present line is a subheading.
27881 This version will not throw an error. It will return the level of the
27882 headline found, or nil if no higher level is found."
27883 (let ((pos (point)) start-level level
27884 (re (concat "^" outline-regexp)))
27886 (outline-back-to-heading t)
27887 (setq start-level (funcall outline-level))
27888 (if (equal start-level 1) (throw 'exit nil))
27889 (while (re-search-backward re nil t)
27890 (setq level (funcall outline-level))
27891 (if (< level start-level) (throw 'exit level)))
27894 (defun org-first-sibling-p ()
27895 "Is this heading the first child of its parents?"
27897 (let ((re (concat "^" outline-regexp))
27899 (unless (org-at-heading-p t)
27900 (error "Not at a heading"))
27901 (setq level (funcall outline-level))
27903 (if (not (re-search-backward re nil t))
27905 (setq l (funcall outline-level))
27908 (defun org-goto-sibling (&optional previous)
27909 "Goto the next sibling, even if it is invisible.
27910 When PREVIOUS is set, go to the previous sibling instead. Returns t
27911 when a sibling was found. When none is found, return nil and don't
27913 (let ((fun (if previous 're-search-backward 're-search-forward))
27915 (re (concat "^" outline-regexp))
27917 (when (condition-case nil (org-back-to-heading t) (error nil))
27918 (setq level (funcall outline-level))
27920 (or previous (forward-char 1))
27921 (while (funcall fun re nil t)
27922 (setq l (funcall outline-level))
27923 (when (< l level) (goto-char pos) (throw 'exit nil))
27924 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
27928 (defun org-show-siblings ()
27929 "Show all siblings of the current headline."
27931 (while (org-goto-sibling) (org-flag-heading nil)))
27933 (while (org-goto-sibling 'previous)
27934 (org-flag-heading nil))))
27936 (defun org-show-hidden-entry ()
27937 "Show an entry where even the heading is hidden."
27941 (defun org-flag-heading (flag &optional entry)
27942 "Flag the current heading. FLAG non-nil means make invisible.
27943 When ENTRY is non-nil, show the entire entry."
27945 (org-back-to-heading t)
27946 ;; Check if we should show the entire entry
27951 (and (outline-next-heading)
27952 (org-flag-heading nil))))
27953 (outline-flag-region (max (point-min) (1- (point)))
27954 (save-excursion (outline-end-of-heading) (point))
27957 (defun org-end-of-subtree (&optional invisible-OK to-heading)
27958 ;; This is an exact copy of the original function, but it uses
27959 ;; `org-back-to-heading', to make it work also in invisible
27960 ;; trees. And is uses an invisible-OK argument.
27961 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
27962 (org-back-to-heading invisible-OK)
27964 (level (funcall outline-level)))
27965 (while (and (not (eobp))
27966 (or first (> (funcall outline-level) level)))
27968 (outline-next-heading))
27970 (if (memq (preceding-char) '(?\n ?\^M))
27972 ;; Go to end of line before heading
27974 (if (memq (preceding-char) '(?\n ?\^M))
27975 ;; leave blank line before heading
27976 (forward-char -1))))))
27979 (defun org-show-subtree ()
27980 "Show everything after this heading at deeper levels."
27981 (outline-flag-region
27984 (outline-end-of-subtree) (outline-next-heading) (point))
27987 (defun org-show-entry ()
27988 "Show the body directly following this heading.
27989 Show the heading too, if it is currently invisible."
27992 (condition-case nil
27994 (org-back-to-heading t)
27995 (outline-flag-region
27996 (max (point-min) (1- (point)))
27999 (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
28000 (or (match-beginning 1) (point-max)))
28004 (defun org-make-options-regexp (kwds)
28005 "Make a regular expression for keyword lines."
28009 (mapconcat 'regexp-quote kwds "\\|")
28013 ;; Make isearch reveal the necessary context
28014 (defun org-isearch-end ()
28015 "Reveal context after isearch exits."
28016 (when isearch-success ; only if search was successful
28017 (if (featurep 'xemacs)
28018 ;; Under XEmacs, the hook is run in the correct place,
28019 ;; we directly show the context.
28020 (org-show-context 'isearch)
28021 ;; In Emacs the hook runs *before* restoring the overlays.
28022 ;; So we have to use a one-time post-command-hook to do this.
28023 ;; (Emacs 22 has a special variable, see function `org-mode')
28024 (unless (and (boundp 'isearch-mode-end-hook-quit)
28025 isearch-mode-end-hook-quit)
28026 ;; Only when the isearch was not quitted.
28027 (org-add-hook 'post-command-hook 'org-isearch-post-command
28028 'append 'local)))))
28030 (defun org-isearch-post-command ()
28031 "Remove self from hook, and show context."
28032 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
28033 (org-show-context 'isearch))
28036 ;;;; Integration with and fixes for other packages
28040 (defvar org-imenu-markers nil
28041 "All markers currently used by Imenu.")
28042 (make-variable-buffer-local 'org-imenu-markers)
28044 (defun org-imenu-new-marker (&optional pos)
28045 "Return a new marker for use by Imenu, and remember the marker."
28046 (let ((m (make-marker)))
28047 (move-marker m (or pos (point)))
28048 (push m org-imenu-markers)
28051 (defun org-imenu-get-tree ()
28052 "Produce the index for Imenu."
28053 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
28054 (setq org-imenu-markers nil)
28055 (let* ((n org-imenu-depth)
28056 (re (concat "^" outline-regexp))
28057 (subs (make-vector (1+ n) nil))
28063 (goto-char (point-max))
28064 (while (re-search-backward re nil t)
28065 (setq level (org-reduced-level (funcall outline-level)))
28067 (looking-at org-complex-heading-regexp)
28068 (setq head (org-match-string-no-properties 4)
28069 m (org-imenu-new-marker))
28070 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
28071 (if (>= level last-level)
28072 (push (cons head m) (aref subs level))
28073 (push (cons head (aref subs (1+ level))) (aref subs level))
28074 (loop for i from (1+ level) to n do (aset subs i nil)))
28075 (setq last-level level)))))
28078 (eval-after-load "imenu"
28080 (add-hook 'imenu-after-jump-hook
28081 (lambda () (org-show-context 'org-goto)))))
28083 ;; Speedbar support
28085 (defun org-speedbar-set-agenda-restriction ()
28086 "Restrict future agenda commands to the location at point in speedbar.
28087 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
28089 (let (p m tp np dir txt w)
28091 ((setq p (text-property-any (point-at-bol) (point-at-eol)
28093 (setq m (get-text-property p 'org-imenu-marker))
28096 (set-buffer (marker-buffer m))
28098 (org-agenda-set-restriction-lock 'subtree))))
28099 ((setq p (text-property-any (point-at-bol) (point-at-eol)
28100 'speedbar-function 'speedbar-find-file))
28101 (setq tp (previous-single-property-change
28102 (1+ p) 'speedbar-function)
28103 np (next-single-property-change
28104 tp 'speedbar-function)
28105 dir (speedbar-line-directory)
28106 txt (buffer-substring-no-properties (or tp (point-min))
28107 (or np (point-max))))
28110 (set-buffer (find-file-noselect
28111 (let ((default-directory dir))
28112 (expand-file-name txt))))
28113 (unless (org-mode-p)
28114 (error "Cannot restrict to non-Org-mode file"))
28115 (org-agenda-set-restriction-lock 'file))))
28116 (t (error "Don't know how to restrict Org-mode's agenda")))
28117 (org-move-overlay org-speedbar-restriction-lock-overlay
28118 (point-at-bol) (point-at-eol))
28119 (setq current-prefix-arg nil)
28120 (org-agenda-maybe-redo)))
28122 (eval-after-load "speedbar"
28124 (speedbar-add-supported-extension ".org")
28125 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
28126 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
28127 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
28128 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
28129 (add-hook 'speedbar-visiting-tag-hook
28130 (lambda () (org-show-context 'org-goto)))))
28133 ;;; Fixes and Hacks
28135 ;; Make flyspell not check words in links, to not mess up our keymap
28136 (defun org-mode-flyspell-verify ()
28137 "Don't let flyspell put overlays at active buttons."
28138 (not (get-text-property (point) 'keymap)))
28140 ;; Make `bookmark-jump' show the jump location if it was hidden.
28141 (eval-after-load "bookmark"
28142 '(if (boundp 'bookmark-after-jump-hook)
28143 ;; We can use the hook
28144 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
28145 ;; Hook not available, use advice
28146 (defadvice bookmark-jump (after org-make-visible activate)
28147 "Make the position visible."
28148 (org-bookmark-jump-unhide))))
28150 (defun org-bookmark-jump-unhide ()
28151 "Unhide the current position, to show the bookmark location."
28153 (or (org-invisible-p)
28154 (save-excursion (goto-char (max (point-min) (1- (point))))
28155 (org-invisible-p)))
28156 (org-show-context 'bookmark-jump)))
28158 ;; Fix a bug in htmlize where there are text properties (face nil)
28159 (eval-after-load "htmlize"
28161 (defadvice htmlize-faces-in-buffer (after org-no-nil-faces activate)
28162 "Make sure there are no nil faces"
28163 (setq ad-return-value (delq nil ad-return-value)))))
28165 ;; Make session.el ignore our circular variable
28166 (eval-after-load "session"
28167 '(add-to-list 'session-globals-exclude 'org-mark-ring))
28169 ;;;; Experimental code
28171 (defun org-closed-in-range ()
28172 "Sparse tree of items closed in a certain time range.
28173 Still experimental, may disappear in the future."
28175 ;; Get the time interval from the user.
28176 (let* ((time1 (time-to-seconds
28177 (org-read-date nil 'to-time nil "Starting date: ")))
28178 (time2 (time-to-seconds
28179 (org-read-date nil 'to-time nil "End date:")))
28180 ;; callback function
28181 (callback (lambda ()
28184 (apply 'encode-time
28185 (org-parse-time-string
28186 (match-string 1))))))
28187 ;; check if time in interval
28188 (and (>= time time1) (<= time time2))))))
28189 ;; make tree, check each match with the callback
28190 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
28196 (run-hooks 'org-load-hook)
28198 ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
28199 ;;; org.el ends here