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 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <dominik at science dot uva dot nl>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://www.astro.uva.nl/~dominik/Tools/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 2, 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://staff.science.uva.nl/~dominik/Tools/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://www.astro.uva.nl/~dominik/Tools/org/Changes
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.
82 ;;;; Customization variables
86 (defconst org-version
"5.06b"
87 "The version number of the file org.el.")
90 (message "Org-mode version %s" org-version
))
92 ;;; Compatibility constants
93 (defconst org-xemacs-p
(featurep 'xemacs
)) ; not used by org.el itself
94 (defconst org-format-transports-properties-p
96 (add-text-properties 0 1 '(test t
) x
)
97 (get-text-property 0 'test
(format "%s" x
)))
98 "Does format transport text properties?")
100 (defmacro org-unmodified
(&rest body
)
101 "Execute body without changing buffer-modified-p."
102 `(set-buffer-modified-p
103 (prog1 (buffer-modified-p) ,@body
)))
106 "Replace posix classes in regular expression."
107 (if (featurep 'xemacs
)
110 (while (string-match "\\[:alnum:\\]" ss
)
111 (setq ss
(replace-match "a-zA-Z0-9" t t ss
)))
115 (defmacro org-preserve-lc
(&rest body
)
116 `(let ((_line (org-current-line))
117 (_col (current-column)))
121 (move-to-column _col
))))
123 ;;; The custom variables
126 "Outline-based notes management and organizer."
132 (defgroup org-startup nil
133 "Options concerning startup of Org-mode."
137 (defcustom org-startup-folded t
138 "Non-nil means, entering Org-mode will switch to OVERVIEW.
139 This can also be configured on a per-file basis by adding one of
140 the following lines anywhere in the buffer:
147 (const :tag
"nofold: show all" nil
)
148 (const :tag
"fold: overview" t
)
149 (const :tag
"content: all headlines" content
)))
151 (defcustom org-startup-truncated t
152 "Non-nil means, entering Org-mode will set `truncate-lines'.
153 This is useful since some lines containing links can be very long and
154 uninteresting. Also tables look terrible when wrapped."
158 (defcustom org-startup-align-all-tables nil
159 "Non-nil means, align all tables when visiting a file.
160 This is useful when the column width in tables is forced with <N> cookies
161 in table fields. Such tables will look correct only after the first re-align.
162 This can also be configured on a per-file basis by adding one of
163 the following lines anywhere in the buffer:
169 (defcustom org-insert-mode-line-in-empty-file nil
170 "Non-nil means insert the first line setting Org-mode in empty files.
171 When the function `org-mode' is called interactively in an empty file, this
172 normally means that the file name does not automatically trigger Org-mode.
173 To ensure that the file will always be in Org-mode in the future, a
174 line enforcing Org-mode will be inserted into the buffer, if this option
179 (defcustom org-replace-disputed-keys nil
180 "Non-nil means use alternative key bindings for some keys.
181 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
182 These keys are also used by other packages like `CUA-mode' or `windmove.el'.
183 If you want to use Org-mode together with one of these other modes,
184 or more generally if you would like to move some Org-mode commands to
185 other keys, set this variable and configure the keys with the variable
188 This option is only relevant at load-time of Org-mode, and must be set
189 *before* org.el is loaded. Changing it requires a restart of Emacs to
194 (if (fboundp 'defvaralias
)
195 (defvaralias 'org-CUA-compatible
'org-replace-disputed-keys
))
197 (defcustom org-disputed-keys
198 '(([(shift up
)] .
[(meta p
)])
199 ([(shift down
)] .
[(meta n
)])
200 ([(shift left
)] .
[(meta -
)])
201 ([(shift right
)] .
[(meta +)])
202 ([(control shift right
)] .
[(meta shift
+)])
203 ([(control shift left
)] .
[(meta shift -
)]))
204 "Keys for which Org-mode and other modes compete.
205 This is an alist, cars are the default keys, second element specifies
206 the alternative to use when `org-replace-disputed-keys' is t.
208 Keys can be specified in any syntax supported by `define-key'.
209 The value of this option takes effect only at Org-mode's startup,
210 therefore you'll have to restart Emacs to apply it after changing."
215 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
216 Or return the original if not disputed."
217 (if org-replace-disputed-keys
218 (let* ((nkey (key-description key
))
219 (x (org-find-if (lambda (x)
220 (equal (key-description (car x
)) nkey
))
225 (defun org-find-if (predicate seq
)
228 (if (funcall predicate
(car seq
))
229 (throw 'exit
(car seq
))
232 (defun org-defkey (keymap key def
)
233 "Define a key, possibly translated, as returned by `org-key'."
234 (define-key keymap
(org-key key
) def
))
236 (defcustom org-ellipsis nil
237 "The ellipsis to use in the Org-mode outline.
238 When nil, just use the standard three dots. When a string, use that instead,
239 and just in Org-mode (which will then use its own display table).
240 Changing this requires executing `M-x org-mode' in a buffer to become
243 :type
'(choice (const :tag
"Default" nil
)
244 (string :tag
"String" :value
"...#")))
246 (defvar org-display-table nil
247 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
249 (defgroup org-keywords nil
250 "Keywords in Org-mode."
254 (defcustom org-deadline-string
"DEADLINE:"
255 "String to mark deadline entries.
256 A deadline is this string, followed by a time stamp. Should be a word,
257 terminated by a colon. You can insert a schedule keyword and
258 a timestamp with \\[org-deadline].
259 Changes become only effective after restarting Emacs."
263 (defcustom org-scheduled-string
"SCHEDULED:"
264 "String to mark scheduled TODO entries.
265 A schedule is this string, followed by a time stamp. Should be a word,
266 terminated by a colon. You can insert a schedule keyword and
267 a timestamp with \\[org-schedule].
268 Changes become only effective after restarting Emacs."
272 (defcustom org-closed-string
"CLOSED:"
273 "String used as the prefix for timestamps logging closing a TODO entry."
277 (defcustom org-archived-string
"ARCHIVED:"
278 "String used as the prefix for timestamps logging archiving a TODO entry."
282 (defcustom org-clock-string
"CLOCK:"
283 "String used as prefix for timestamps clocking work hours on an item."
287 (defcustom org-comment-string
"COMMENT"
288 "Entries starting with this keyword will never be exported.
289 An entry can be toggled between COMMENT and normal with
290 \\[org-toggle-comment].
291 Changes become only effective after restarting Emacs."
295 (defcustom org-quote-string
"QUOTE"
296 "Entries starting with this keyword will be exported in fixed-width font.
297 Quoting applies only to the text in the entry following the headline, and does
298 not extend beyond the next headline, even if that is lower level.
299 An entry can be toggled between QUOTE and normal with
300 \\[org-toggle-fixed-width-section]."
304 (defconst org-repeat-re
305 (concat "\\(?:\\<\\(?:" org-scheduled-string
"\\|" org-deadline-string
"\\)"
306 " +<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\)\\(\\+[0-9]+[dwmy]\\)")
307 "Regular expression for specifying repeated events.
308 After a match, group 1 contains the repeat expression.")
310 (defgroup org-structure nil
311 "Options concerning the general structure of Org-mode files."
315 (defgroup org-reveal-location nil
316 "Options about how to make context of a location visible."
317 :tag
"Org Reveal Location"
318 :group
'org-structure
)
320 (defcustom org-show-hierarchy-above
'((default . t
))
321 "Non-nil means, show full hierarchy when revealing a location.
322 Org-mode often shows locations in an org-mode file which might have
323 been invisible before. When this is set, the hierarchy of headings
324 above the exposed location is shown.
325 Turning this off for example for sparse trees makes them very compact.
326 Instead of t, this can also be an alist specifying this option for different
327 contexts. Valid contexts are
328 agenda when exposing an entry from the agenda
329 org-goto when using the command `org-goto' on key C-c C-j
330 occur-tree when using the command `org-occur' on key C-c /
331 tags-tree when constructing a sparse tree based on tags matches
332 link-search when exposing search matches associated with a link
333 mark-goto when exposing the jump goal of a mark
334 bookmark-jump when exposing a bookmark location
335 isearch when exiting from an incremental search
336 default default for all contexts not set explicitly"
337 :group
'org-reveal-location
339 (const :tag
"Always" t
)
340 (const :tag
"Never" nil
)
341 (repeat :greedy t
:tag
"Individual contexts"
343 (choice :tag
"Context"
350 (const bookmark-jump
)
355 (defcustom org-show-following-heading
'((default . nil
))
356 "Non-nil means, show following heading when revealing a location.
357 Org-mode often shows locations in an org-mode file which might have
358 been invisible before. When this is set, the heading following the
360 Turning this off for example for sparse trees makes them very compact,
361 but makes it harder to edit the location of the match. In such a case,
362 use the command \\[org-reveal] to show more context.
363 Instead of t, this can also be an alist specifying this option for different
364 contexts. See `org-show-hierarchy-above' for valid contexts."
365 :group
'org-reveal-location
367 (const :tag
"Always" t
)
368 (const :tag
"Never" nil
)
369 (repeat :greedy t
:tag
"Individual contexts"
371 (choice :tag
"Context"
378 (const bookmark-jump
)
383 (defcustom org-show-siblings
'((default . nil
) (isearch t
))
384 "Non-nil means, show all sibling heading when revealing a location.
385 Org-mode often shows locations in an org-mode file which might have
386 been invisible before. When this is set, the sibling of the current entry
387 heading are all made visible. If `org-show-hierarchy-above' is t,
388 the same happens on each level of the hierarchy above the current entry.
390 By default this is on for the isearch context, off for all other contexts.
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
398 (const :tag
"Always" t
)
399 (const :tag
"Never" nil
)
400 (repeat :greedy t
:tag
"Individual contexts"
402 (choice :tag
"Context"
409 (const bookmark-jump
)
414 (defgroup org-cycle nil
415 "Options concerning visibility cycling in Org-mode."
417 :group
'org-structure
)
419 (defcustom org-drawers
'("PROPERTIES")
420 "Names of drawers. Drawers are not opened by cycling on the headline above.
421 Drawers only open with a TAB on the drawer line itself. A drawer looks like
426 The drawer \"PROPERTIES\" is special for capturing properties through
428 :group
'org-structure
429 :type
'(repeat (string :tag
"Drawer Name")))
431 (defcustom org-cycle-global-at-bob nil
432 "Cycle globally if cursor is at beginning of buffer and not at a headline.
433 This makes it possible to do global cycling without having to use S-TAB or
434 C-u TAB. For this special case to work, the first line of the buffer
435 must not be a headline - it may be empty ot some other text. When used in
436 this way, `org-cycle-hook' is disables temporarily, to make sure the
437 cursor stays at the beginning of the buffer.
438 When this option is nil, don't do anything special at the beginning
443 (defcustom org-cycle-emulate-tab t
444 "Where should `org-cycle' emulate TAB.
446 white Only in completely white lines
447 whitestart Only at the beginning of lines, before the first non-white char.
448 t Everywhere except in headlines
449 exc-hl-bol Everywhere except at the start of a headline
450 If TAB is used in a place where it does not emulate TAB, the current subtree
451 visibility is cycled."
453 :type
'(choice (const :tag
"Never" nil
)
454 (const :tag
"Only in completely white lines" white
)
455 (const :tag
"Before first char in a line" whitestart
)
456 (const :tag
"Everywhere except in headlines" t
)
457 (const :tag
"Everywhere except at bol in headlines" exc-hl-bol
)
460 (defcustom org-cycle-separator-lines
2
461 "Number of empty lines needed to keep an empty line between collapsed trees.
462 If you leave an empty line between the end of a subtree and the following
463 headline, this empty line is hidden when the subtree is folded.
464 Org-mode will leave (exactly) one empty line visible if the number of
465 empty lines is equal or larger to the number given in this variable.
466 So the default 2 means, at least 2 empty lines after the end of a subtree
467 are needed to produce free space between a collapsed subtree and the
470 Special case: when 0, never leave empty lines in collapsed view."
474 (defcustom org-cycle-hook
'(org-cycle-hide-archived-subtrees
475 org-cycle-hide-drawers
476 org-cycle-show-empty-lines
477 org-optimize-window-after-visibility-change
)
478 "Hook that is run after `org-cycle' has changed the buffer visibility.
479 The function(s) in this hook must accept a single argument which indicates
480 the new state that was set by the most recent `org-cycle' command. The
481 argument is a symbol. After a global state change, it can have the values
482 `overview', `content', or `all'. After a local state change, it can have
483 the values `folded', `children', or `subtree'."
487 (defgroup org-edit-structure nil
488 "Options concerning structure editing in Org-mode."
489 :tag
"Org Edit Structure"
490 :group
'org-structure
)
492 (defcustom org-special-ctrl-a
/e nil
493 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
494 When t, `C-a' will bring back the cursor to the beginning of the
495 headline text, i.e. after the stars and after a possible TODO keyword.
496 In an item, this will be the position after the bullet.
497 When the cursor is already at that position, another `C-a' will bring
498 it to the beginning of the line.
499 `C-e' will jump to the end of the headline, ignoring the presence of tags
500 in the headline. A second `C-e' will then jump to the true end of the
501 line, after any tags.
502 When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
503 and only a directly following, identical keypress will bring the cursor
504 to the special positions."
505 :group
'org-edit-structure
507 (const :tag
"off" nil
)
508 (const :tag
"after bullet first" t
)
509 (const :tag
"border first" reversed
)))
511 (if (fboundp 'defvaralias
)
512 (defvaralias 'org-special-ctrl-a
'org-special-ctrl-a
/e
))
514 (defcustom org-odd-levels-only nil
515 "Non-nil means, skip even levels and only use odd levels for the outline.
516 This has the effect that two stars are being added/taken away in
517 promotion/demotion commands. It also influences how levels are
518 handled by the exporters.
519 Changing it requires restart of `font-lock-mode' to become effective
520 for fontification also in regions already fontified.
521 You may also set this on a per-file basis by adding one of the following
526 :group
'org-edit-structure
527 :group
'org-font-lock
530 (defcustom org-adapt-indentation t
531 "Non-nil means, adapt indentation when promoting and demoting.
532 When this is set and the *entire* text in an entry is indented, the
533 indentation is increased by one space in a demotion command, and
534 decreased by one in a promotion command. If any line in the entry
535 body starts at column 0, indentation is not changed at all."
536 :group
'org-edit-structure
539 (defcustom org-blank-before-new-entry
'((heading . nil
)
540 (plain-list-item . nil
))
541 "Should `org-insert-heading' leave a blank line before new heading/item?
542 The value is an alist, with `heading' and `plain-list-item' as car,
543 and a boolean flag as cdr."
544 :group
'org-edit-structure
546 (cons (const heading
) (boolean))
547 (cons (const plain-list-item
) (boolean))))
549 (defcustom org-insert-heading-hook nil
550 "Hook being run after inserting a new heading."
551 :group
'org-edit-structure
554 (defcustom org-enable-fixed-width-editor t
555 "Non-nil means, lines starting with \":\" are treated as fixed-width.
556 This currently only means, they are never auto-wrapped.
557 When nil, such lines will be treated like ordinary lines.
558 See also the QUOTE keyword."
559 :group
'org-edit-structure
562 (defgroup org-sparse-trees nil
563 "Options concerning sparse trees in Org-mode."
564 :tag
"Org Sparse Trees"
565 :group
'org-structure
)
567 (defcustom org-highlight-sparse-tree-matches t
568 "Non-nil means, highlight all matches that define a sparse tree.
569 The highlights will automatically disappear the next time the buffer is
570 changed by an edit command."
571 :group
'org-sparse-trees
574 (defcustom org-remove-highlights-with-change t
575 "Non-nil means, any change to the buffer will remove temporary highlights.
576 Such highlights are created by `org-occur' and `org-clock-display'.
577 When nil, `C-c C-c needs to be used to get rid of the highlights.
578 The highlights created by `org-preview-latex-fragment' always need
579 `C-c C-c' to be removed."
580 :group
'org-sparse-trees
585 (defcustom org-occur-hook
'(org-first-headline-recenter)
586 "Hook that is run after `org-occur' has constructed a sparse tree.
587 This can be used to recenter the window to show as much of the structure
589 :group
'org-sparse-trees
592 (defgroup org-plain-lists nil
593 "Options concerning plain lists in Org-mode."
594 :tag
"Org Plain lists"
595 :group
'org-structure
)
597 (defcustom org-cycle-include-plain-lists nil
598 "Non-nil means, include plain lists into visibility cycling.
599 This means that during cycling, plain list items will *temporarily* be
600 interpreted as outline headlines with a level given by 1000+i where i is the
601 indentation of the bullet. In all other operations, plain list items are
602 not seen as headlines. For example, you cannot assign a TODO keyword to
604 :group
'org-plain-lists
607 (defcustom org-plain-list-ordered-item-terminator t
608 "The character that makes a line with leading number an ordered list item.
609 Valid values are ?. and ?\). To get both terminators, use t. While
610 ?. may look nicer, it creates the danger that a line with leading
611 number may be incorrectly interpreted as an item. ?\) therefore is
613 :group
'org-plain-lists
614 :type
'(choice (const :tag
"dot like in \"2.\"" ?.
)
615 (const :tag
"paren like in \"2)\"" ?\
))
616 (const :tab
"both" t
)))
618 (defcustom org-auto-renumber-ordered-lists t
619 "Non-nil means, automatically renumber ordered plain lists.
620 Renumbering happens when the sequence have been changed with
621 \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
622 use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
623 :group
'org-plain-lists
626 (defcustom org-provide-checkbox-statistics t
627 "Non-nil means, update checkbox statistics after insert and toggle.
628 When this is set, checkbox statistics is updated each time you either insert
629 a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
630 with \\[org-ctrl-c-ctrl-c\\]."
631 :group
'org-plain-lists
634 (defgroup org-archive nil
635 "Options concerning archiving in Org-mode."
637 :group
'org-structure
)
639 (defcustom org-archive-tag
"ARCHIVE"
640 "The tag that marks a subtree as archived.
641 An archived subtree does not open during visibility cycling, and does
642 not contribute to the agenda listings."
647 (defcustom org-agenda-skip-archived-trees t
648 "Non-nil means, the agenda will skip any items located in archived trees.
649 An archived tree is a tree marked with the tag ARCHIVE."
651 :group
'org-agenda-skip
654 (defcustom org-cycle-open-archived-trees nil
655 "Non-nil means, `org-cycle' will open archived trees.
656 An archived tree is a tree marked with the tag ARCHIVE.
657 When nil, archived trees will stay folded. You can still open them with
658 normal outline commands like `show-all', but not with the cycling commands."
663 (defcustom org-sparse-tree-open-archived-trees nil
664 "Non-nil means sparse tree construction shows matches in archived trees.
665 When nil, matches in these trees are highlighted, but the trees are kept in
668 :group
'org-sparse-trees
671 (defcustom org-archive-location
"%s_archive::"
672 "The location where subtrees should be archived.
673 This string consists of two parts, separated by a double-colon.
675 The first part is a file name - when omitted, archiving happens in the same
676 file. %s will be replaced by the current file name (without directory part).
677 Archiving to a different file is useful to keep archived entries from
678 contributing to the Org-mode Agenda.
680 The part after the double colon is a headline. The archived entries will be
681 filed under that headline. When omitted, the subtrees are simply filed away
682 at the end of the file, as top-level entries.
684 Here are a few examples:
686 If the current file is Projects.org, archive in file
687 Projects.org_archive, as top-level trees. This is the default.
689 \"::* Archived Tasks\"
690 Archive in the current file, under the top-level headline
691 \"* Archived Tasks\".
693 \"~/org/archive.org::\"
694 Archive in file ~/org/archive.org (absolute path), as top-level trees.
696 \"basement::** Finished Tasks\"
697 Archive in file ./basement (relative path), as level 3 trees
698 below the level 2 heading \"** Finished Tasks\".
700 You may set this option on a per-file basis by adding to the buffer a
703 #+ARCHIVE: basement::** Finished Tasks"
707 (defcustom org-archive-mark-done t
708 "Non-nil means, mark entries as DONE when they are moved to the archive file.
709 This can be a string to set the keyword to use. When t, Org-mode will
710 use the first keyword in its list that means done."
713 (const :tag
"No" nil
)
715 (string :tag
"Use this keyword")))
717 (defcustom org-archive-stamp-time t
718 "Non-nil means, add a time stamp to entries moved to an archive file."
722 (defgroup org-table nil
723 "Options concerning tables in Org-mode."
727 (defcustom org-enable-table-editor
'optimized
728 "Non-nil means, lines starting with \"|\" are handled by the table editor.
729 When nil, such lines will be treated like ordinary lines.
731 When equal to the symbol `optimized', the table editor will be optimized to
733 - Automatic overwrite mode in front of whitespace in table fields.
734 This makes the structure of the table stay in tact as long as the edited
735 field does not exceed the column width.
736 - Minimize the number of realigns. Normally, the table is aligned each time
737 TAB or RET are pressed to move to another field. With optimization this
738 happens only if changes to a field might have changed the column width.
739 Optimization requires replacing the functions `self-insert-command',
740 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
741 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
742 very good at guessing when a re-align will be necessary, but you can always
743 force one with \\[org-ctrl-c-ctrl-c].
745 If you would like to use the optimized version in Org-mode, but the
746 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
748 This variable can be used to turn on and off the table editor during a session,
749 but in order to toggle optimization, a restart is required.
751 See also the variable `org-table-auto-blank-field'."
754 (const :tag
"off" nil
)
756 (const :tag
"on, optimized" optimized
)))
758 (defcustom orgtbl-optimized
(eq org-enable-table-editor
'optimized
)
759 "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
760 In the optimized version, the table editor takes over all simple keys that
761 normally just insert a character. In tables, the characters are inserted
762 in a way to minimize disturbing the table structure (i.e. in overwrite mode
763 for empty fields). Outside tables, the correct binding of the keys is
766 The default for this option is t if the optimized version is also used in
767 Org-mode. See the variable `org-enable-table-editor' for details. Changing
768 this variable requires a restart of Emacs to become effective."
772 (defcustom orgtbl-radio-table-templates
773 '((latex-mode "% BEGIN RECEIVE ORGTBL %n
774 % END RECEIVE ORGTBL %n
776 #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
779 (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
780 @c END RECEIVE ORGTBL %n
782 #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
785 (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
786 <!-- END RECEIVE ORGTBL %n -->
788 #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
791 "Templates for radio tables in different major modes.
792 All occurrences of %n in a template will be replaced with the name of the
793 table, obtained by prompting the user."
796 (list (symbol :tag
"Major mode")
797 (string :tag
"Format"))))
799 (defgroup org-table-settings nil
800 "Settings for tables in Org-mode."
801 :tag
"Org Table Settings"
804 (defcustom org-table-default-size
"5x2"
805 "The default size for newly created tables, Columns x Rows."
806 :group
'org-table-settings
809 (defcustom org-table-number-regexp
810 "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$"
811 "Regular expression for recognizing numbers in table columns.
812 If a table column contains mostly numbers, it will be aligned to the
813 right. If not, it will be aligned to the left.
815 The default value of this option is a regular expression which allows
816 anything which looks remotely like a number as used in scientific
817 context. For example, all of the following will be considered a
819 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
821 Other options offered by the customize interface are more restrictive."
822 :group
'org-table-settings
824 (const :tag
"Positive Integers"
826 (const :tag
"Integers"
828 (const :tag
"Floating Point Numbers"
829 "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
830 (const :tag
"Floating Point Number or Integer"
831 "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
832 (const :tag
"Exponential, Floating point, Integer"
833 "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
834 (const :tag
"Very General Number-Like, including hex"
835 "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$")
836 (string :tag
"Regexp:")))
838 (defcustom org-table-number-fraction
0.5
839 "Fraction of numbers in a column required to make the column align right.
840 In a column all non-white fields are considered. If at least this
841 fraction of fields is matched by `org-table-number-fraction',
842 alignment to the right border applies."
843 :group
'org-table-settings
846 (defgroup org-table-editing nil
847 "Bahavior of tables during editing in Org-mode."
848 :tag
"Org Table Editing"
851 (defcustom org-table-automatic-realign t
852 "Non-nil means, automatically re-align table when pressing TAB or RETURN.
853 When nil, aligning is only done with \\[org-table-align], or after column
855 :group
'org-table-editing
858 (defcustom org-table-auto-blank-field t
859 "Non-nil means, automatically blank table field when starting to type into it.
860 This only happens when typing immediately after a field motion
861 command (TAB, S-TAB or RET).
862 Only relevant when `org-enable-table-editor' is equal to `optimized'."
863 :group
'org-table-editing
866 (defcustom org-table-tab-jumps-over-hlines t
867 "Non-nil means, tab in the last column of a table with jump over a hline.
868 If a horizontal separator line is following the current line,
869 `org-table-next-field' can either create a new row before that line, or jump
870 over the line. When this option is nil, a new line will be created before
872 :group
'org-table-editing
875 (defcustom org-table-tab-recognizes-table.el t
876 "Non-nil means, TAB will automatically notice a table.el table.
877 When it sees such a table, it moves point into it and - if necessary -
878 calls `table-recognize-table'."
879 :group
'org-table-editing
882 (defgroup org-table-calculation nil
883 "Options concerning tables in Org-mode."
884 :tag
"Org Table Calculation"
887 (defcustom org-table-use-standard-references t
888 "Should org-mode work with table refrences like B3 instead of @3$2?
891 from accept as input, do not present for editing
892 t: accept as input and present for editing"
893 :group
'org-table-calculation
895 (const :tag
"Never, don't even check unser input for them" nil
)
896 (const :tag
"Always, both as user input, and when editing" t
)
897 (const :tag
"Convert user input, don't offer during editing" 'from
)))
899 (defcustom org-table-copy-increment t
900 "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
901 :group
'org-table-calculation
904 (defcustom org-calc-default-modes
905 '(calc-internal-prec 12
906 calc-float-format
(float 5)
909 calc-symbolic-mode nil
910 calc-date-format
(YYYY "-" MM
"-" DD
" " Www
(" " HH
":" mm
))
911 calc-display-working-message t
913 "List with Calc mode settings for use in calc-eval for table formulas.
914 The list must contain alternating symbols (Calc modes variables and values).
915 Don't remove any of the default settings, just change the values. Org-mode
916 relies on the variables to be present in the list."
917 :group
'org-table-calculation
920 (defcustom org-table-formula-evaluate-inline t
921 "Non-nil means, TAB and RET evaluate a formula in current table field.
922 If the current field starts with an equal sign, it is assumed to be a formula
923 which should be evaluated as described in the manual and in the documentation
924 string of the command `org-table-eval-formula'. This feature requires the
926 When this variable is nil, formula calculation is only available through
927 the command \\[org-table-eval-formula]."
928 :group
'org-table-calculation
931 (defcustom org-table-formula-use-constants t
932 "Non-nil means, interpret constants in formulas in tables.
933 A constant looks like `$c' or `$Grav' and will be replaced before evaluation
934 by the value given in `org-table-formula-constants', or by a value obtained
935 from the `constants.el' package."
936 :group
'org-table-calculation
939 (defcustom org-table-formula-constants nil
940 "Alist with constant names and values, for use in table formulas.
941 The car of each element is a name of a constant, without the `$' before it.
942 The cdr is the value as a string. For example, if you'd like to use the
943 speed of light in a formula, you would configure
945 (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
947 and then use it in an equation like `$1*$c'.
949 Constants can also be defined on a per-file basis using a line like
951 #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
952 :group
'org-table-calculation
954 (cons (string :tag
"name")
955 (string :tag
"value"))))
957 (defvar org-table-formula-constants-local nil
958 "Local version of `org-table-formula-constants'.")
959 (make-variable-buffer-local 'org-table-formula-constants-local
)
961 (defcustom org-table-allow-automatic-line-recalculation t
962 "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
963 Automatically means, when TAB or RET or C-c C-c are pressed in the line."
964 :group
'org-table-calculation
967 (defgroup org-link nil
968 "Options concerning links in Org-mode."
972 (defvar org-link-abbrev-alist-local nil
973 "Buffer-local version of `org-link-abbrev-alist', which see.
974 The value of this is taken from the #+LINK lines.")
975 (make-variable-buffer-local 'org-link-abbrev-alist-local
)
977 (defcustom org-link-abbrev-alist nil
978 "Alist of link abbreviations.
979 The car of each element is a string, to be replaced at the start of a link.
980 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
981 links in Org-mode buffers can have an optional tag after a double colon, e.g.
983 [[linkkey:tag][description]]
985 If REPLACE is a string, the tag will simply be appended to create the link.
986 If the string contains \"%s\", the tag will be inserted there. REPLACE may
987 also be a function that will be called with the tag as the only argument to
988 create the link. See the manual for examples."
992 (defcustom org-descriptive-links t
993 "Non-nil means, hide link part and only show description of bracket links.
994 Bracket links are like [[link][descritpion]]. This variable sets the initial
995 state in new org-mode buffers. The setting can then be toggled on a
996 per-buffer basis from the Org->Hyperlinks menu."
1000 (defcustom org-link-file-path-type
'adaptive
1001 "How the path name in file links should be stored.
1004 relative relative to the current directory, i.e. the directory of the file
1005 into which the link is being inserted.
1006 absolute absolute path, if possible with ~ for home directory.
1007 noabbrev absolute path, no abbreviation of home directory.
1008 adaptive Use relative path for files in the current directory and sub-
1009 directories of it. For other files, use an absolute path."
1017 (defcustom org-activate-links
'(bracket angle plain radio tag date
)
1018 "Types of links that should be activated in Org-mode files.
1019 This is a list of symbols, each leading to the activation of a certain link
1020 type. In principle, it does not hurt to turn on most link types - there may
1021 be a small gain when turning off unused link types. The types are:
1023 bracket The recommended [[link][description]] or [[link]] links with hiding.
1024 angular Links in angular brackes that may contain whitespace like
1025 <bbdb:Carsten Dominik>.
1026 plain Plain links in normal text, no whitespace, like http://google.com.
1027 radio Text that is matched by a radio target, see manual for details.
1028 tag Tag settings in a headline (link to tag search).
1029 date Time stamps (link to calendar).
1031 Changing this variable requires a restart of Emacs to become effective."
1033 :type
'(set (const :tag
"Double bracket links (new style)" bracket
)
1034 (const :tag
"Angular bracket links (old style)" angular
)
1035 (const :tag
"plain text links" plain
)
1036 (const :tag
"Radio target matches" radio
)
1037 (const :tag
"Tags" tag
)
1038 (const :tag
"Tags" target
)
1039 (const :tag
"Timestamps" date
)))
1041 (defgroup org-link-store nil
1042 "Options concerning storing links in Org-mode"
1043 :tag
"Org Store Link"
1046 (defcustom org-email-link-description-format
"Email %c: %.30s"
1047 "Format of the description part of a link to an email or usenet message.
1048 The following %-excapes will be replaced by corresponding information:
1050 %F full \"From\" field
1051 %f name, taken from \"From\" field, address if no name
1052 %T full \"To\" field
1053 %t first name in \"To\" field, address if no name
1054 %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
1055 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
1059 You may use normal field width specification between the % and the letter.
1060 This is for example useful to limit the length of the subject.
1062 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
1063 :group
'org-link-store
1066 (defcustom org-from-is-user-regexp
1068 (when (and user-mail-address
(not (string= user-mail-address
"")))
1069 (setq r1
(concat "\\<" (regexp-quote user-mail-address
) "\\>")))
1070 (when (and user-full-name
(not (string= user-full-name
"")))
1071 (setq r2
(concat "\\<" (regexp-quote user-full-name
) "\\>")))
1072 (if (and r1 r2
) (concat r1
"\\|" r2
) (or r1 r2
)))
1073 "Regexp mached against the \"From:\" header of an email or usenet message.
1074 It should match if the message is from the user him/herself."
1075 :group
'org-link-store
1078 (defcustom org-context-in-file-links t
1079 "Non-nil means, file links from `org-store-link' contain context.
1080 A search string will be added to the file name with :: as separator and
1081 used to find the context when the link is activated by the command
1082 `org-open-at-point'.
1083 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1084 negates this setting for the duration of the command."
1085 :group
'org-link-store
1088 (defcustom org-keep-stored-link-after-insertion nil
1089 "Non-nil means, keep link in list for entire session.
1091 The command `org-store-link' adds a link pointing to the current
1092 location to an internal list. These links accumulate during a session.
1093 The command `org-insert-link' can be used to insert links into any
1094 Org-mode file (offering completion for all stored links). When this
1095 option is nil, every link which has been inserted once using \\[org-insert-link]
1096 will be removed from the list, to make completing the unused links
1098 :group
'org-link-store
1101 (defcustom org-usenet-links-prefer-google nil
1102 "Non-nil means, `org-store-link' will create web links to Google groups.
1103 When nil, Gnus will be used for such links.
1104 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1105 negates this setting for the duration of the command."
1106 :group
'org-link-store
1109 (defgroup org-link-follow nil
1110 "Options concerning following links in Org-mode"
1111 :tag
"Org Follow Link"
1114 (defcustom org-tab-follows-link nil
1115 "Non-nil means, on links TAB will follow the link.
1116 Needs to be set before org.el is loaded."
1117 :group
'org-link-follow
1120 (defcustom org-return-follows-link nil
1121 "Non-nil means, on links RET will follow the link.
1122 Needs to be set before org.el is loaded."
1123 :group
'org-link-follow
1126 (defcustom org-mouse-1-follows-link t
1127 "Non-nil means, mouse-1 on a link will follow the link.
1128 A longer mouse click will still set point. Does not wortk on XEmacs.
1129 Needs to be set before org.el is loaded."
1130 :group
'org-link-follow
1133 (defcustom org-mark-ring-length
4
1134 "Number of different positions to be recorded in the ring
1135 Changing this requires a restart of Emacs to work correctly."
1136 :group
'org-link-follow
1139 (defcustom org-link-frame-setup
1140 '((vm . vm-visit-folder-other-frame
)
1141 (gnus . gnus-other-frame
)
1142 (file . find-file-other-window
))
1143 "Setup the frame configuration for following links.
1144 When following a link with Emacs, it may often be useful to display
1145 this link in another window or frame. This variable can be used to
1146 set this up for the different types of links.
1149 `vm-visit-folder-other-frame'
1150 For Gnus, use any of
1153 For FILE, use any of
1155 `find-file-other-window'
1156 `find-file-other-frame'
1157 For the calendar, use the variable `calendar-setup'.
1158 For BBDB, it is currently only possible to display the matches in
1160 :group
'org-link-follow
1164 (const vm-visit-folder
)
1165 (const vm-visit-folder-other-window
)
1166 (const vm-visit-folder-other-frame
)))
1170 (const gnus-other-frame
)))
1174 (const find-file-other-window
)
1175 (const find-file-other-frame
)))))
1177 (defcustom org-display-internal-link-with-indirect-buffer nil
1178 "Non-nil means, use indirect buffer to display infile links.
1179 Activating internal links (from one location in a file to another location
1180 in the same file) normally just jumps to the location. When the link is
1181 activated with a C-u prefix (or with mouse-3), the link is displayed in
1182 another window. When this option is set, the other window actually displays
1183 an indirect buffer clone of the current buffer, to avoid any visibility
1184 changes to the current buffer."
1185 :group
'org-link-follow
1188 (defcustom org-open-non-existing-files nil
1189 "Non-nil means, `org-open-file' will open non-existing files.
1190 When nil, an error will be generated."
1191 :group
'org-link-follow
1194 (defcustom org-link-mailto-program
'(browse-url "mailto:%a?subject=%s")
1195 "Function and arguments to call for following mailto links.
1196 This is a list with the first element being a lisp function, and the
1197 remaining elements being arguments to the function. In string arguments,
1198 %a will be replaced by the address, and %s will be replaced by the subject
1199 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1200 :group
'org-link-follow
1202 (const :tag
"browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1203 (const :tag
"compose-mail" (compose-mail "%a" "%s"))
1204 (const :tag
"message-mail" (message-mail "%a" "%s"))
1205 (cons :tag
"other" (function) (repeat :tag
"argument" sexp
))))
1207 (defcustom org-confirm-shell-link-function
'yes-or-no-p
1208 "Non-nil means, ask for confirmation before executing shell links.
1209 Shell links can be dangerous, just thing about a link
1211 [[shell:rm -rf ~/*][Google Search]]
1213 This link would show up in your Org-mode document as \"Google Search\"
1214 but really it would remove your entire home directory.
1215 Therefore I *definitely* advise against setting this variable to nil.
1216 Just change it to `y-or-n-p' of you want to confirm with a single key press
1217 rather than having to type \"yes\"."
1218 :group
'org-link-follow
1220 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1221 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1222 (const :tag
"no confirmation (dangerous)" nil
)))
1224 (defcustom org-confirm-elisp-link-function
'yes-or-no-p
1225 "Non-nil means, ask for confirmation before executing elisp links.
1226 Elisp links can be dangerous, just think about a link
1228 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1230 This link would show up in your Org-mode document as \"Google Search\"
1231 but really it would remove your entire home directory.
1232 Therefore I *definitely* advise against setting this variable to nil.
1233 Just change it to `y-or-n-p' of you want to confirm with a single key press
1234 rather than having to type \"yes\"."
1235 :group
'org-link-follow
1237 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1238 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1239 (const :tag
"no confirmation (dangerous)" nil
)))
1241 (defconst org-file-apps-defaults-gnu
1244 "Default file applications on a UNIX or GNU/Linux system.
1245 See `org-file-apps'.")
1247 (defconst org-file-apps-defaults-macosx
1253 ("eps.gz" .
"gv %s")
1255 ("fig" .
"xfig %s"))
1256 "Default file applications on a MacOS X system.
1257 The system \"open\" is known as a default, but we use X11 applications
1258 for some files for which the OS does not have a good default.
1259 See `org-file-apps'.")
1261 (defconst org-file-apps-defaults-windowsnt
1265 (list (if (featurep 'xemacs
)
1266 'mswindows-shell-execute
1269 "Default file applications on a Windows NT system.
1270 The system \"open\" is used for most files.
1271 See `org-file-apps'.")
1273 (defcustom org-file-apps
1282 "External applications for opening `file:path' items in a document.
1283 Org-mode uses system defaults for different file types, but
1284 you can use this variable to set the application for a given file
1285 extension. The entries in this list are cons cells where the car identifies
1286 files and the cdr the corresponding command. Possible values for the
1288 \"ext\" A string identifying an extension
1289 `directory' Matches a directory
1290 `remote' Matches a remote file, accessible through tramp or efs.
1291 Remote files most likely should be visited through Emacs
1292 because external applications cannot handle such paths.
1293 t Default for all remaining files
1295 Possible values for the command are:
1296 `emacs' The file will be visited by the current Emacs process.
1297 `default' Use the default application for this file type.
1298 string A command to be executed by a shell; %s will be replaced
1299 by the path to the file.
1300 sexp A Lisp form which will be evaluated. The file path will
1301 be available in the Lisp variable `file'.
1302 For more examples, see the system specific constants
1303 `org-file-apps-defaults-macosx'
1304 `org-file-apps-defaults-windowsnt'
1305 `org-file-apps-defaults-gnu'."
1306 :group
'org-link-follow
1308 (cons (choice :value
""
1309 (string :tag
"Extension")
1310 (const :tag
"Default for unrecognized files" t
)
1311 (const :tag
"Remote file" remote
)
1312 (const :tag
"Links to a directory" directory
))
1314 (const :tag
"Visit with Emacs" emacs
)
1315 (const :tag
"Use system default" default
)
1316 (string :tag
"Command")
1317 (sexp :tag
"Lisp form")))))
1319 (defcustom org-mhe-search-all-folders nil
1320 "Non-nil means, that the search for the mh-message will be extended to
1321 all folders if the message cannot be found in the folder given in the link.
1322 Searching all folders is very efficient with one of the search engines
1323 supported by MH-E, but will be slow with pick."
1324 :group
'org-link-follow
1327 (defgroup org-remember nil
1328 "Options concerning interaction with remember.el."
1332 (defcustom org-directory
"~/org"
1333 "Directory with org files.
1334 This directory will be used as default to prompt for org files.
1335 Used by the hooks for remember.el."
1336 :group
'org-remember
1339 (defcustom org-default-notes-file
"~/.notes"
1340 "Default target for storing notes.
1341 Used by the hooks for remember.el. This can be a string, or nil to mean
1342 the value of `remember-data-file'.
1343 You can set this on a per-template basis with the variable
1344 `org-remember-templates'."
1345 :group
'org-remember
1347 (const :tag
"Default from remember-data-file" nil
)
1350 (defcustom org-remember-store-without-prompt nil
1351 "Non-nil means, `C-c C-c' stores remember note without further promts.
1352 In this case, you need `C-u C-c C-c' to get the prompts for
1353 note file and headline.
1354 When this variable is nil, `C-c C-c' give you the prompts, and
1355 `C-u C-c C-c' trigger the fasttrack."
1356 :group
'org-remember
1359 (defcustom org-remember-default-headline
""
1360 "The headline that should be the default location in the notes file.
1361 When filing remember notes, the cursor will start at that position.
1362 You can set this on a per-template basis with the variable
1363 `org-remember-templates'."
1364 :group
'org-remember
1367 (defcustom org-remember-templates nil
1368 "Templates for the creation of remember buffers.
1369 When nil, just let remember make the buffer.
1370 When not nil, this is a list of 4-element lists. In each entry, the first
1371 element is a character, a unique key to select this template.
1372 The second element is the template. The third element is optional and can
1373 specify a destination file for remember items created with this template.
1374 The default file is given by `org-default-notes-file'. An optional forth
1375 element can specify the headline in that file that should be offered
1376 first when the user is asked to file the entry. The default headline is
1377 given in the variable `org-remember-default-headline'.
1379 The template specifies the structure of the remember buffer. It should have
1380 a first line starting with a star, to act as the org-mode headline.
1381 Furthermore, the following %-escapes will be replaced with content:
1383 %^{prompt} prompt the user for a string and replace this sequence with it.
1384 %t time stamp, date only
1385 %T time stamp with date and time
1386 %u, %U like the above, but inactive time stamps
1387 %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
1388 You may define a prompt like %^{Please specify birthday}t
1389 %n user name (taken from `user-full-name')
1390 %a annotation, normally the link created with org-store-link
1391 %i initial content, the region when remember is called with C-u.
1392 If %i is indented, the entire inserted text will be indented
1395 %? After completing the template, position cursor here.
1397 Apart from these general escapes, you can access information specific to the
1398 link type that is created. For example, calling `remember' in emails or gnus
1399 will record the author and the subject of the message, which you can access
1400 with %:author and %:subject, respectively. Here is a complete list of what
1401 is recorded for each link type.
1403 Link type | Available information
1404 -------------------+------------------------------------------------------
1405 bbdb | %:type %:name %:company
1406 vm, wl, mh, rmail | %:type %:subject %:message-id
1407 | %:from %:fromname %:fromaddress
1408 | %:to %:toname %:toaddress
1409 | %:fromto (either \"to NAME\" or \"from NAME\")
1410 gnus | %:group, for messages also all email fields
1411 w3, w3m | %:type %:url
1412 info | %:type %:file %:node
1413 calendar | %:type %:date"
1414 :group
'org-remember
1415 :get
(lambda (var) ; Make sure all entries have 4 elements
1417 (cond ((= (length x
) 3) (append x
'("")))
1418 ((= (length x
) 2) (append x
'("" "")))
1420 (default-value var
)))
1423 (list :value
(?a
"\n" nil nil
)
1424 (character :tag
"Selection Key")
1425 (string :tag
"Template")
1426 (file :tag
"Destination file (optional)")
1427 (string :tag
"Destination headline (optional)"))))
1429 (defcustom org-reverse-note-order nil
1430 "Non-nil means, store new notes at the beginning of a file or entry.
1431 When nil, new notes will be filed to the end of a file or entry."
1432 :group
'org-remember
1434 (const :tag
"Reverse always" t
)
1435 (const :tag
"Reverse never" nil
)
1436 (repeat :tag
"By file name regexp"
1437 (cons regexp boolean
))))
1439 (defgroup org-todo nil
1440 "Options concerning TODO items in Org-mode."
1444 (defgroup org-progress nil
1445 "Options concerning Progress logging in Org-mode."
1449 (defcustom org-todo-keywords
'((sequence "TODO" "DONE"))
1450 "List of TODO entry keyword sequences and their interpretation.
1451 \\<org-mode-map>This is a list of sequences.
1453 Each sequence starts with a symbol, either `sequence' or `type',
1454 indicating if the keywords should be interpreted as a sequence of
1455 action steps, or as different types of TODO items. The first
1456 keywords are states requiring action - these states will select a headline
1457 for inclusion into the global TODO list Org-mode produces. If one of
1458 the \"keywords\" is the vertical bat \"|\" the remaining keywords
1459 signify that no further action is necessary. If \"|\" is not found,
1460 the last keyword is treated as the only DONE state of the sequence.
1462 The command \\[org-todo] cycles an entry through these states, and one
1463 additional state where no keyword is present. For details about this
1464 cycling, see the manual.
1466 TODO keywords and interpretation can also be set on a per-file basis with
1467 the special #+SEQ_TODO and #+TYP_TODO lines.
1469 For backward compatibility, this variable may also be just a list
1470 of keywords - in this case the interptetation (sequence or type) will be
1471 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
1473 :group
'org-keywords
1475 (repeat :tag
"Old syntax, just keywords"
1476 (string :tag
"Keyword"))
1477 (repeat :tag
"New syntax"
1480 :tag
"Interpretation"
1481 (const :tag
"Sequence (cycling hits every state)" sequence
)
1482 (const :tag
"Type (cycling directly to DONE)" type
))
1484 (string :tag
"Keyword"))))))
1486 (defvar org-todo-keywords-1 nil
)
1487 (make-variable-buffer-local 'org-todo-keywords-1
)
1488 (defvar org-todo-keywords-for-agenda nil
)
1489 (defvar org-done-keywords-for-agenda nil
)
1490 (defvar org-not-done-keywords nil
)
1491 (make-variable-buffer-local 'org-not-done-keywords
)
1492 (defvar org-done-keywords nil
)
1493 (make-variable-buffer-local 'org-done-keywords
)
1494 (defvar org-todo-heads nil
)
1495 (make-variable-buffer-local 'org-todo-heads
)
1496 (defvar org-todo-sets nil
)
1497 (make-variable-buffer-local 'org-todo-sets
)
1498 (defvar org-todo-kwd-alist nil
)
1499 (make-variable-buffer-local 'org-todo-kwd-alist
)
1501 (defcustom org-todo-interpretation
'sequence
1502 "Controls how TODO keywords are interpreted.
1503 This variable is in principle obsolete and is only used for
1504 backward compatibility, if the interpretation of todo keywords is
1505 not given already in `org-todo-keywords'. See that variable for
1508 :group
'org-keywords
1509 :type
'(choice (const sequence
)
1512 (defcustom org-after-todo-state-change-hook nil
1513 "Hook which is run after the state of a TODO item was changed.
1514 The new state (a string with a TODO keyword, or nil) is available in the
1515 Lisp variable `state'."
1519 (defcustom org-log-done nil
1520 "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
1521 When the state of an entry is changed from nothing to TODO, remove a previous
1524 This can also be a list of symbols indicating under which conditions
1525 the time stamp recording the action should be annotated with a short note.
1526 Valid members of this list are
1528 done Offer to record a note when marking entries done
1529 state Offer to record a note whenever changing the TODO state
1530 of an item. This is only relevant if TODO keywords are
1531 interpreted as sequence, see variable `org-todo-interpretation'.
1532 When `state' is set, this includes tracking `done'.
1533 clock-out Offer to record a note when clocking out of an item.
1535 A separate window will then pop up and allow you to type a note.
1536 After finishing with C-c C-c, the note will be added directly after the
1537 timestamp, as a plain list item. See also the variable
1538 `org-log-note-headings'.
1540 Logging can also be configured on a per-file basis by adding one of
1541 the following lines anywhere in the buffer:
1544 #+STARTUP: nologging
1545 #+STARTUP: lognotedone
1546 #+STARTUP: lognotestate
1547 #+STARTUP: lognoteclock-out"
1549 :group
'org-progress
1551 (const :tag
"off" nil
)
1553 (set :tag
"on, with notes, detailed control" :greedy t
:value
(done)
1554 (const :tag
"when item is marked DONE" done
)
1555 (const :tag
"when TODO state changes" state
)
1556 (const :tag
"when clocking out" clock-out
))))
1558 (defcustom org-log-done-with-time t
1559 "Non-nil means, the CLOSED time stamp will contain date and time.
1560 When nil, only the date will be recorded."
1561 :group
'org-progress
1564 (defcustom org-log-note-headings
1565 '((done .
"CLOSING NOTE %t")
1566 (state .
"State %-12s %t")
1568 "Headings for notes added when clocking out or closing TODO items.
1569 The value is an alist, with the car being a symbol indicating the note
1570 context, and the cdr is the heading to be used. The heading may also be the
1572 %t in the heading will be replaced by a time stamp.
1573 %s will be replaced by the new TODO state, in double quotes.
1574 %u will be replaced by the user name.
1575 %U will be replaced by the full user name."
1577 :group
'org-progress
1578 :type
'(list :greedy t
1579 (cons (const :tag
"Heading when closing an item" done
) string
)
1581 "Heading when changing todo state (todo sequence only)"
1583 (cons (const :tag
"Heading when clocking out" clock-out
) string
)))
1585 (defcustom org-log-states-order-reversed t
1586 "Non-nil means, the latest state change note will be directly after heading.
1587 When nil, the notes will be orderer according to time."
1589 :group
'org-progress
1592 (defcustom org-log-repeat t
1593 "Non-nil means, prompt for a note when REPEAT is resetting a TODO entry.
1594 When nil, no note will be taken."
1596 :group
'org-progress
1599 (defcustom org-clock-out-when-done t
1600 "When t, the clock will be stopped when the relevant entry is marked DONE.
1601 Nil means, clock will keep running until stopped explicitly with
1602 `C-c C-x C-o', or until the clock is started in a different item."
1603 :group
'org-progress
1606 (defgroup org-priorities nil
1607 "Priorities in Org-mode."
1608 :tag
"Org Priorities"
1611 (defcustom org-highest-priority ?A
1612 "The highest priority of TODO items. A character like ?A, ?B etc.
1613 Must have a smaller ASCII number than `org-lowest-priority'."
1614 :group
'org-priorities
1617 (defcustom org-lowest-priority ?C
1618 "The lowest priority of TODO items. A character like ?A, ?B etc.
1619 Must have a larger ASCII number than `org-highest-priority'."
1620 :group
'org-priorities
1623 (defcustom org-default-priority ?B
1624 "The default priority of TODO items.
1625 This is the priority an item get if no explicit priority is given."
1626 :group
'org-priorities
1629 (defgroup org-time nil
1630 "Options concerning time stamps and deadlines in Org-mode."
1634 (defcustom org-insert-labeled-timestamps-at-point nil
1635 "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
1636 When nil, these labeled time stamps are forces into the second line of an
1637 entry, just after the headline. When scheduling from the global TODO list,
1638 the time stamp will always be forced into the second line."
1642 (defcustom org-insert-labeled-timestamps-before-properties-drawer t
1643 "Non-nil means, always insert planning info before property drawer.
1644 When this is nil and there is a property drawer *directly* after
1645 the headline, move the planning info into the drawer. If the property
1646 drawer separated from the headline by at least one line, this variable
1651 (defconst org-time-stamp-formats
'("<%Y-%m-%d %a>" .
"<%Y-%m-%d %a %H:%M>")
1652 "Formats for `format-time-string' which are used for time stamps.
1653 It is not recommended to change this constant.")
1655 (defcustom org-time-stamp-rounding-minutes
0
1656 "Number of minutes to round time stamps to upon insertion.
1657 When zero, insert the time unmodified. Useful rounding numbers
1658 should be factors of 60, so for example 5, 10, 15.
1659 When this is not zero, you can still force an exact time-stamp by using
1660 a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
1664 (defcustom org-display-custom-times nil
1665 "Non-nil means, overlay custom formats over all time stamps.
1666 The formats are defined through the variable `org-time-stamp-custom-formats'.
1667 To turn this on on a per-file basis, insert anywhere in the file:
1668 #+STARTUP: customtime"
1672 (make-variable-buffer-local 'org-display-custom-times
)
1674 (defcustom org-time-stamp-custom-formats
1675 '("<%m/%d/%y %a>" .
"<%m/%d/%y %a %H:%M>") ; american
1676 "Custom formats for time stamps. See `format-time-string' for the syntax.
1677 These are overlayed over the default ISO format if the variable
1678 `org-display-custom-times' is set. Time like %H:%M should be at the
1679 end of the second format."
1683 (defun org-time-stamp-format (&optional long inactive
)
1684 "Get the right format for a time string."
1685 (let ((f (if long
(cdr org-time-stamp-formats
)
1686 (car org-time-stamp-formats
))))
1688 (concat "[" (substring f
1 -
1) "]")
1691 (defcustom org-deadline-warning-days
30
1692 "No. of days before expiration during which a deadline becomes active.
1693 This variable governs the display in sparse trees and in the agenda."
1697 (defcustom org-popup-calendar-for-date-prompt t
1698 "Non-nil means, pop up a calendar when prompting for a date.
1699 In the calendar, the date can be selected with mouse-1. However, the
1700 minibuffer will also be active, and you can simply enter the date as well.
1701 When nil, only the minibuffer will be available."
1705 (defcustom org-calendar-follow-timestamp-change t
1706 "Non-nil means, make the calendar window follow timestamp changes.
1707 When a timestamp is modified and the calendar window is visible, it will be
1708 moved to the new date."
1712 (defgroup org-tags nil
1713 "Options concerning tags in Org-mode."
1717 (defcustom org-tag-alist nil
1718 "List of tags allowed in Org-mode files.
1719 When this list is nil, Org-mode will base TAG input on what is already in the
1721 The value of this variable is an alist, the car may be (and should) be a
1722 character that is used to select that tag through the fast-tag-selection
1723 interface. See the manual for details."
1727 (cons (string :tag
"Tag name")
1728 (character :tag
"Access char"))
1729 (const :tag
"Start radio group" (:startgroup
))
1730 (const :tag
"End radio group" (:endgroup
)))))
1732 (defcustom org-use-fast-tag-selection
'auto
1733 "Non-nil means, use fast tag selection scheme.
1734 This is a special interface to select and deselect tags with single keys.
1735 When nil, fast selection is never used.
1736 When the symbol `auto', fast selection is used if and only if selection
1737 characters for tags have been configured, either through the variable
1738 `org-tag-alist' or through a #+TAGS line in the buffer.
1739 When t, fast selection is always used and selection keys are assigned
1740 automatically if necessary."
1743 (const :tag
"Always" t
)
1744 (const :tag
"Never" nil
)
1745 (const :tag
"When selection characters are configured" 'auto
)))
1747 (defcustom org-fast-tag-selection-single-key nil
1748 "Non-nil means, fast tag selection exits after first change.
1749 When nil, you have to press RET to exit it.
1750 During fast tag selection, you can toggle this flag with `C-c'.
1751 This variable can also have the value `expert'. In this case, the window
1752 displaying the tags menu is not even shown, until you press C-c again."
1755 (const :tag
"No" nil
)
1756 (const :tag
"Yes" t
)
1757 (const :tag
"Expert" expert
)))
1759 (defcustom org-tags-column
48
1760 "The column to which tags should be indented in a headline.
1761 If this number is positive, it specifies the column. If it is negative,
1762 it means that the tags should be flushright to that column. For example,
1763 -79 works well for a normal 80 character screen."
1767 (defcustom org-auto-align-tags t
1768 "Non-nil means, realign tags after pro/demotion of TODO state change.
1769 These operations change the length of a headline and therefore shift
1770 the tags around. With this options turned on, after each such operation
1771 the tags are again aligned to `org-tags-column'."
1775 (defcustom org-use-tag-inheritance t
1776 "Non-nil means, tags in levels apply also for sublevels.
1777 When nil, only the tags directly given in a specific line apply there.
1778 If you turn off this option, you very likely want to turn on the
1779 companion option `org-tags-match-list-sublevels'."
1783 (defcustom org-tags-match-list-sublevels nil
1784 "Non-nil means list also sublevels of headlines matching tag search.
1785 Because of tag inheritance (see variable `org-use-tag-inheritance'),
1786 the sublevels of a headline matching a tag search often also match
1787 the same search. Listing all of them can create very long lists.
1788 Setting this variable to nil causes subtrees of a match to be skipped.
1789 This option is off by default, because inheritance in on. If you turn
1790 inheritance off, you very likely want to turn this option on.
1792 As a special case, if the tag search is restricted to TODO items, the
1793 value of this variable is ignored and sublevels are always checked, to
1794 make sure all corresponding TODO items find their way into the list."
1798 (defvar org-tags-history nil
1799 "History of minibuffer reads for tags.")
1800 (defvar org-last-tags-completion-table nil
1801 "The last used completion table for tags.")
1803 (defgroup org-properties nil
1804 "Options concerning properties in Org-mode."
1805 :tag
"Org Properties"
1808 (defcustom org-property-format
"%-10s %s"
1809 "How property key/value pairs should be formatted by `indent-line'.
1810 When `indent-line' hits a property definition, it will format the line
1811 according to this format, mainly to make sure that the values are
1812 lined-up with respect to each other."
1813 :group
'org-properties
1816 (defcustom org-columns-default-format
"%25ITEM %TODO %3PRIORITY %TAGS"
1817 "The default column format, if no other format has been defined.
1818 This variable can be set on the per-file basis by inserting a line
1820 #+COLUMNS: %25ITEM ....."
1821 :group
'org-properties
1824 (defcustom org-global-properties nil
1825 "List of property/value pairs that can be inherited by any entry.
1826 You can set buffer-local values for this by adding lines like
1828 #+PROPERTY: NAME VALUE"
1829 :group
'org-properties
1831 (cons (string :tag
"Property")
1832 (string :tag
"Value"))))
1834 (defvar org-local-properties nil
1835 "List of property/value pairs that can be inherited by any entry.
1836 Valid for the current buffer.
1837 This variable is populated from #+PROPERTY lines.")
1839 (defgroup org-agenda nil
1840 "Options concerning agenda views in Org-mode."
1844 (defvar org-category nil
1845 "Variable used by org files to set a category for agenda display.
1846 Such files should use a file variable to set it, for example
1848 # -*- mode: org; org-category: \"ELisp\"
1850 or contain a special line
1854 If the file does not specify a category, then file's base name
1856 (make-variable-buffer-local 'org-category
)
1858 (defcustom org-agenda-files nil
1859 "The files to be used for agenda display.
1860 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
1861 \\[org-remove-file]. You can also use customize to edit the list.
1863 If the value of the variable is not a list but a single file name, then
1864 the list of agenda files is actually stored and maintained in that file, one
1865 agenda file per line."
1868 (repeat :tag
"List of files" file
)
1869 (file :tag
"Store list in a file\n" :value
"~/.agenda_files")))
1872 (defcustom org-agenda-confirm-kill
1
1873 "When set, remote killing from the agenda buffer needs confirmation.
1874 When t, a confirmation is always needed. When a number N, confirmation is
1875 only needed when the text to be killed contains more than N non-white lines."
1878 (const :tag
"Never" nil
)
1879 (const :tag
"Always" t
)
1880 (number :tag
"When more than N lines")))
1882 (defcustom org-calendar-to-agenda-key
[?c
]
1883 "The key to be installed in `calendar-mode-map' for switching to the agenda.
1884 The command `org-calendar-goto-agenda' will be bound to this key. The
1885 default is the character `c' because then `c' can be used to switch back and
1886 forth between agenda and calendar."
1890 (defgroup org-agenda-export nil
1891 "Options concerning exporting agenda views in Org-mode."
1892 :tag
"Org Agenda Export"
1895 (defcustom org-agenda-with-colors t
1896 "Non-nil means, use colors in agenda views."
1897 :group
'org-agenda-export
1900 (defcustom org-agenda-exporter-settings nil
1901 "Alist of variable/value pairs that should be active during agenda export.
1902 This is a good place to set uptions for ps-print and for htmlize."
1903 :group
'org-agenda-export
1907 (sexp :tag
"Value"))))
1909 (defcustom org-agenda-export-html-style
""
1910 "The style specification for exported HTML Agenda files.
1911 If this variable contains a string, it will replace the default <style>
1912 section as produced by `htmlize'.
1913 Since there are different ways of setting style information, this variable
1914 needs to contain the full HTML structure to provide a style, including the
1915 surrounding HTML tags. The style specifications should include definitions
1916 the fonts used by the agenda, here is an example:
1918 <style type=\"text/css\">
1919 p { font-weight: normal; color: gray; }
1920 .org-agenda-structure {
1926 color: #cc6666;Week-agenda:
1932 .title { text-align: center; }
1933 .todo, .deadline { color: red; }
1934 .done { color: green; }
1937 or, if you want to keep the style in a file,
1939 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
1941 As the value of this option simply gets inserted into the HTML <head> header,
1942 you can \"misuse\" it to also add other text to the header. However,
1943 <style>...</style> is required, if not present the variable will be ignored."
1944 :group
'org-agenda-export
1945 :group
'org-export-html
1948 (defgroup org-agenda-custom-commands nil
1949 "Options concerning agenda views in Org-mode."
1950 :tag
"Org Agenda Custom Commands"
1953 (defcustom org-agenda-custom-commands nil
1954 "Custom commands for the agenda.
1955 These commands will be offered on the splash screen displayed by the
1956 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
1958 (key type match options files)
1960 key The key (a single char as a string) to be associated with the command.
1961 type The command type, any of the following symbols:
1962 todo Entries with a specific TODO keyword, in all agenda files.
1963 tags Tags match in all agenda files.
1964 tags-todo Tags match in all agenda files, TODO entries only.
1965 todo-tree Sparse tree of specific TODO keyword in *current* file.
1966 tags-tree Sparse tree with all tags matches in *current* file.
1967 occur-tree Occur sparse tree for *current* file.
1968 match What to search for:
1969 - a single keyword for TODO keyword searches
1970 - a tags match expression for tags searches
1971 - a regular expression for occur searches
1972 options A list of option settings, similar to that in a let form, so like
1973 this: ((opt1 val1) (opt2 val2) ...)
1974 files A list of files file to write the produced agenda buffer to
1975 with the command `org-store-agenda-views'.
1976 If a file name ends in \".html\", an HTML version of the buffer
1977 is written out. If it ends in \".ps\", a postscript version is
1978 produced. Otherwide, only the plain text is written to the file.
1980 You can also define a set of commands, to create a composite agenda buffer.
1981 In this case, an entry looks like this:
1983 (key desc (cmd1 cmd2 ...) general-options file)
1987 desc A description string to be displayed in the dispatcher menu.
1988 cmd An agenda command, similar to the above. However, tree commands
1989 are no allowed, but instead you can get agenda and global todo list.
1990 So valid commands for a set are:
1994 (todo \"match\" options files)
1995 (tags \"match\" options files)
1996 (tags-todo \"match\" options files)
1998 Each command can carry a list of options, and another set of options can be
1999 given for the whole set of commands. Individual command options take
2000 precedence over the general options."
2001 :group
'org-agenda-custom-commands
2003 (choice :value
("a" tags
"" nil
)
2004 (list :tag
"Single command"
2007 (const :tag
"Agenda" agenda
)
2008 (const :tag
"TODO list" alltodo
)
2009 (const :tag
"Stuck projects" stuck
)
2010 (const :tag
"Tags search (all agenda files)" tags
)
2011 (const :tag
"Tags search of TODO entries (all agenda files)" tags-todo
)
2012 (const :tag
"TODO keyword search (all agenda files)" todo
)
2013 (const :tag
"Tags sparse tree (current buffer)" tags-tree
)
2014 (const :tag
"TODO keyword tree (current buffer)" todo-tree
)
2015 (const :tag
"Occur tree (current buffer)" occur-tree
)
2016 (symbol :tag
"Other, user-defined function"))
2017 (string :tag
"Match")
2018 (repeat :tag
"Local options"
2019 (list (variable :tag
"Option") (sexp :tag
"Value")))
2020 (option (repeat :tag
"Export" (file :tag
"Export to"))))
2021 (list :tag
"Command series, all agenda files"
2023 (string :tag
"Description")
2026 (const :tag
"Agenda" (agenda))
2027 (const :tag
"TODO list" (alltodo))
2028 (const :tag
"Stuck projects" (stuck))
2029 (list :tag
"Tags search"
2030 (const :format
"" tags
)
2031 (string :tag
"Match")
2032 (repeat :tag
"Local options"
2033 (list (variable :tag
"Option")
2034 (sexp :tag
"Value"))))
2036 (list :tag
"Tags search, TODO entries only"
2037 (const :format
"" tags-todo
)
2038 (string :tag
"Match")
2039 (repeat :tag
"Local options"
2040 (list (variable :tag
"Option")
2041 (sexp :tag
"Value"))))
2043 (list :tag
"TODO keyword search"
2044 (const :format
"" todo
)
2045 (string :tag
"Match")
2046 (repeat :tag
"Local options"
2047 (list (variable :tag
"Option")
2048 (sexp :tag
"Value"))))
2050 (list :tag
"Other, user-defined function"
2051 (symbol :tag
"function")
2052 (string :tag
"Match")
2053 (repeat :tag
"Local options"
2054 (list (variable :tag
"Option")
2055 (sexp :tag
"Value"))))))
2057 (repeat :tag
"General options"
2058 (list (variable :tag
"Option")
2059 (sexp :tag
"Value")))
2060 (option (repeat :tag
"Export" (file :tag
"Export to")))))))
2062 (defcustom org-stuck-projects
2063 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil
"")
2064 "How to identify stuck projects.
2065 This is a list of four items:
2066 1. A tags/todo matcher string that is used to identify a project.
2067 The entire tree below a headline matched by this is considered one project.
2068 2. A list of TODO keywords identifying non-stuck projects.
2069 If the project subtree contains any headline with one of these todo
2070 keywords, the project is considered to be not stuck. If you specify
2071 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
2072 3. A list of tags identifying non-stuck projects.
2073 If the project subtree contains any headline with one of these tags,
2074 the project is considered to be not stuck. If you specify \"*\" as
2075 a tag, any tag will mark the project unstuck.
2076 4. An arbitrary regular expression matching non-stuck projects.
2078 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
2079 or `C-c a #' to produce the list."
2080 :group
'org-agenda-custom-commands
2082 (string :tag
"Tags/TODO match to identify a project")
2083 (repeat :tag
"Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
2084 (repeat :tag
"Projects are *not* stuck if they have an entry with TAG being any of" (string))
2085 (regexp :tag
"Projects are *not* stuck if this regexp matches\ninside the subtree")))
2088 (defgroup org-agenda-skip nil
2089 "Options concerning skipping parts of agenda files."
2090 :tag
"Org Agenda Skip"
2093 (defcustom org-agenda-todo-list-sublevels t
2094 "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
2095 When nil, the sublevels of a TODO entry are not checked, resulting in
2096 potentially much shorter TODO lists."
2097 :group
'org-agenda-skip
2101 (defcustom org-agenda-todo-ignore-scheduled nil
2102 "Non-nil means, don't show scheduled entries in the global todo list.
2103 The idea behind this is that by scheduling it, you have already taken care
2105 :group
'org-agenda-skip
2109 (defcustom org-agenda-todo-ignore-deadlines nil
2110 "Non-nil means, don't show near deadline entries in the global todo list.
2111 Near means closer than `org-deadline-warning-days' days.
2112 The idea behind this is that such items will appear in the agenda anyway."
2113 :group
'org-agenda-skip
2117 (defcustom org-agenda-skip-scheduled-if-done nil
2118 "Non-nil means don't show scheduled items in agenda when they are done.
2119 This is relevant for the daily/weekly agenda, not for the TODO list. And
2120 it applied only to the actualy date of the scheduling. Warnings about
2121 an item with a past scheduling dates are always turned off when the item
2123 :group
'org-agenda-skip
2126 (defcustom org-agenda-skip-deadline-if-done nil
2127 "Non-nil means don't show deadines when the corresponding item is done.
2128 When nil, the deadline is still shown and should give you a happy feeling.
2129 This is relevant for the daily/weekly agenda. And it applied only to the
2130 actualy date of the deadline. Warnings about approching and past-due
2131 deadlines are always turned off when the item is DONE."
2132 :group
'org-agenda-skip
2135 (defcustom org-timeline-show-empty-dates
3
2136 "Non-nil means, `org-timeline' also shows dates without an entry.
2137 When nil, only the days which actually have entries are shown.
2138 When t, all days between the first and the last date are shown.
2139 When an integer, show also empty dates, but if there is a gap of more than
2140 N days, just insert a special line indicating the size of the gap."
2141 :group
'org-agenda-skip
2143 (const :tag
"None" nil
)
2144 (const :tag
"All" t
)
2145 (number :tag
"at most")))
2148 (defgroup org-agenda-startup nil
2149 "Options concerning initial settings in the Agenda in Org Mode."
2150 :tag
"Org Agenda Startup"
2153 (defcustom org-finalize-agenda-hook nil
2154 "Hook run just before displaying an agenda buffer."
2155 :group
'org-agenda-startup
2158 (defcustom org-agenda-mouse-1-follows-link nil
2159 "Non-nil means, mouse-1 on a link will follow the link in the agenda.
2160 A longer mouse click will still set point. Does not wortk on XEmacs.
2161 Needs to be set before org.el is loaded."
2162 :group
'org-agenda-startup
2165 (defcustom org-agenda-start-with-follow-mode nil
2166 "The initial value of follow-mode in a newly created agenda window."
2167 :group
'org-agenda-startup
2170 (defgroup org-agenda-windows nil
2171 "Options concerning the windows used by the Agenda in Org Mode."
2172 :tag
"Org Agenda Windows"
2175 (defcustom org-agenda-window-setup
'reorganize-frame
2176 "How the agenda buffer should be displayed.
2177 Possible values for this option are:
2179 current-window Show agenda in the current window, keeping all other windows.
2180 other-frame Use `switch-to-buffer-other-frame' to display agenda.
2181 other-window Use `switch-to-buffer-other-window' to display agenda.
2182 reorganize-frame Show only two windows on the current frame, the current
2183 window and the agenda.
2184 See also the variable `org-agenda-restore-windows-after-quit'."
2185 :group
'org-agenda-windows
2187 (const current-window
)
2189 (const other-window
)
2190 (const reorganize-frame
)))
2192 (defcustom org-agenda-restore-windows-after-quit nil
2193 "Non-nil means, restore window configuration open exiting agenda.
2194 Before the window configuration is changed for displaying the agenda,
2195 the current status is recorded. When the agenda is exited with
2196 `q' or `x' and this option is set, the old state is restored. If
2197 `org-agenda-window-setup' is `other-frame', the value of this
2198 option will be ignored.."
2199 :group
'org-agenda-windows
2202 (defcustom org-indirect-buffer-display
'other-window
2203 "How should indirect tree buffers be displayed?
2204 This applies to indirect buffers created with the commands
2205 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
2207 current-window Display in the current window
2208 other-window Just display in another window.
2209 dedicated-frame Create one new frame, and re-use it each time.
2210 new-frame Make a new frame each time."
2211 :group
'org-structure
2212 :group
'org-agenda-windows
2214 (const :tag
"In current window" current-window
)
2215 (const :tag
"In current frame, other window" other-window
)
2216 (const :tag
"Each time a new frame" new-frame
)
2217 (const :tag
"One dedicated frame" dedicated-frame
)))
2219 (defgroup org-agenda-daily
/weekly nil
2220 "Options concerning the daily/weekly agenda."
2221 :tag
"Org Agenda Daily/Weekly"
2224 (defcustom org-agenda-ndays
7
2225 "Number of days to include in overview display.
2227 :group
'org-agenda-daily
/weekly
2230 (defcustom org-agenda-start-on-weekday
1
2231 "Non-nil means, start the overview always on the specified weekday.
2232 0 denotes Sunday, 1 denotes Monday etc.
2233 When nil, always start on the current day."
2234 :group
'org-agenda-daily
/weekly
2235 :type
'(choice (const :tag
"Today" nil
)
2236 (number :tag
"Weekday No.")))
2238 (defcustom org-agenda-show-all-dates t
2239 "Non-nil means, `org-agenda' shows every day in the selected range.
2240 When nil, only the days which actually have entries are shown."
2241 :group
'org-agenda-daily
/weekly
2244 (defcustom org-agenda-date-format
"%A %d %B %Y"
2245 "Format string for displaying dates in the agenda.
2246 Used by the daily/weekly agenda and by the timeline. This should be
2247 a format string understood by `format-time-string'.
2248 FIXME: Not used currently, because of timezone problem."
2249 :group
'org-agenda-daily
/weekly
2252 (defcustom org-agenda-include-diary nil
2253 "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
2254 :group
'org-agenda-daily
/weekly
2257 (defcustom org-agenda-include-all-todo nil
2258 "Set means weekly/daily agenda will always contain all TODO entries.
2259 The TODO entries will be listed at the top of the agenda, before
2260 the entries for specific days."
2261 :group
'org-agenda-daily
/weekly
2264 (defcustom org-agenda-repeating-timestamp-show-all t
2265 "Non-nil means, show all occurences of a repeating stamp in the agenda.
2266 When nil, only one occurence is shown, either today or the
2267 nearest into the future."
2268 :group
'org-agenda-daily
/weekly
2271 (defgroup org-agenda-time-grid nil
2272 "Options concerning the time grid in the Org-mode Agenda."
2273 :tag
"Org Agenda Time Grid"
2276 (defcustom org-agenda-use-time-grid t
2277 "Non-nil means, show a time grid in the agenda schedule.
2278 A time grid is a set of lines for specific times (like every two hours between
2279 8:00 and 20:00). The items scheduled for a day at specific times are
2280 sorted in between these lines.
2281 For details about when the grid will be shown, and what it will look like, see
2282 the variable `org-agenda-time-grid'."
2283 :group
'org-agenda-time-grid
2286 (defcustom org-agenda-time-grid
2287 '((daily today require-timed
)
2289 (800 1000 1200 1400 1600 1800 2000))
2291 "The settings for time grid for agenda display.
2292 This is a list of three items. The first item is again a list. It contains
2293 symbols specifying conditions when the grid should be displayed:
2295 daily if the agenda shows a single day
2296 weekly if the agenda shows an entire week
2297 today show grid on current date, independent of daily/weekly display
2298 require-timed show grid only if at least one item has a time specification
2300 The second item is a string which will be places behing the grid time.
2302 The third item is a list of integers, indicating the times that should have
2304 :group
'org-agenda-time-grid
2307 (set :greedy t
:tag
"Grid Display Options"
2308 (const :tag
"Show grid in single day agenda display" daily
)
2309 (const :tag
"Show grid in weekly agenda display" weekly
)
2310 (const :tag
"Always show grid for today" today
)
2311 (const :tag
"Show grid only if any timed entries are present"
2313 (const :tag
"Skip grid times already present in an entry"
2315 (string :tag
"Grid String")
2316 (repeat :tag
"Grid Times" (integer :tag
"Time"))))
2318 (defgroup org-agenda-sorting nil
2319 "Options concerning sorting in the Org-mode Agenda."
2320 :tag
"Org Agenda Sorting"
2323 (let ((sorting-choice
2325 (const time-up
) (const time-down
)
2326 (const category-keep
) (const category-up
) (const category-down
)
2327 (const tag-down
) (const tag-up
)
2328 (const priority-up
) (const priority-down
))))
2330 (defcustom org-agenda-sorting-strategy
2331 '((agenda time-up category-keep priority-down
)
2332 (todo category-keep priority-down
)
2333 (tags category-keep priority-down
))
2334 "Sorting structure for the agenda items of a single day.
2335 This is a list of symbols which will be used in sequence to determine
2336 if an entry should be listed before another entry. The following
2337 symbols are recognized:
2339 time-up Put entries with time-of-day indications first, early first
2340 time-down Put entries with time-of-day indications first, late first
2341 category-keep Keep the default order of categories, corresponding to the
2342 sequence in `org-agenda-files'.
2343 category-up Sort alphabetically by category, A-Z.
2344 category-down Sort alphabetically by category, Z-A.
2345 tag-up Sort alphabetically by last tag, A-Z.
2346 tag-down Sort alphabetically by last tag, Z-A.
2347 priority-up Sort numerically by priority, high priority last.
2348 priority-down Sort numerically by priority, high priority first.
2350 The different possibilities will be tried in sequence, and testing stops
2351 if one comparison returns a \"not-equal\". For example, the default
2352 '(time-up category-keep priority-down)
2353 means: Pull out all entries having a specified time of day and sort them,
2354 in order to make a time schedule for the current day the first thing in the
2355 agenda listing for the day. Of the entries without a time indication, keep
2356 the grouped in categories, don't sort the categories, but keep them in
2357 the sequence given in `org-agenda-files'. Within each category sort by
2360 Leaving out `category-keep' would mean that items will be sorted across
2361 categories by priority."
2362 :group
'org-agenda-sorting
2364 (repeat :tag
"General" ,sorting-choice
)
2365 (list :tag
"Individually"
2366 (cons (const :tag
"Strategy for Weekly/Daily agenda" agenda
)
2367 (repeat ,sorting-choice
))
2368 (cons (const :tag
"Strategy for TODO lists" todo
)
2369 (repeat ,sorting-choice
))
2370 (cons (const :tag
"Strategy for Tags matches" tags
)
2371 (repeat ,sorting-choice
))))))
2373 (defcustom org-sort-agenda-notime-is-late t
2374 "Non-nil means, items without time are considered late.
2375 This is only relevant for sorting. When t, items which have no explicit
2376 time like 15:30 will be considered as 99:01, i.e. later than any items which
2377 do have a time. When nil, the default time is before 0:00. You can use this
2378 option to decide if the schedule for today should come before or after timeless
2380 :group
'org-agenda-sorting
2383 (defgroup org-agenda-prefix nil
2384 "Options concerning the entry prefix in the Org-mode agenda display."
2385 :tag
"Org Agenda Prefix"
2388 (defcustom org-agenda-prefix-format
2389 '((agenda .
" %-12:c%?-12t% s")
2393 "Format specifications for the prefix of items in the agenda views.
2394 An alist with four entries, for the different agenda types. The keys to the
2395 sublists are `agenda', `timeline', `todo', and `tags'. The values
2397 This format works similar to a printf format, with the following meaning:
2399 %c the category of the item, \"Diary\" for entries from the diary, or
2400 as given by the CATEGORY keyword or derived from the file name.
2401 %T the *last* tag of the item. Last because inherited tags come
2403 %t the time-of-day specification if one applies to the entry, in the
2405 %s Scheduling/Deadline information, a short string
2407 All specifiers work basically like the standard `%s' of printf, but may
2408 contain two additional characters: A question mark just after the `%' and
2409 a whitespace/punctuation character just before the final letter.
2411 If the first character after `%' is a question mark, the entire field
2412 will only be included if the corresponding value applies to the
2413 current entry. This is useful for fields which should have fixed
2414 width when present, but zero width when absent. For example,
2415 \"%?-12t\" will result in a 12 character time field if a time of the
2416 day is specified, but will completely disappear in entries which do
2419 If there is punctuation or whitespace character just before the final
2420 format letter, this character will be appended to the field value if
2421 the value is not empty. For example, the format \"%-12:c\" leads to
2422 \"Diary: \" if the category is \"Diary\". If the category were be
2423 empty, no additional colon would be interted.
2425 The default value of this option is \" %-12:c%?-12t% s\", meaning:
2426 - Indent the line with two space characters
2427 - Give the category in a 12 chars wide field, padded with whitespace on
2428 the right (because of `-'). Append a colon if there is a category
2430 - If there is a time-of-day, put it into a 12 chars wide field. If no
2431 time, don't put in an empty field, just skip it (because of '?').
2432 - Finally, put the scheduling information and append a whitespace.
2434 As another example, if you don't want the time-of-day of entries in
2435 the prefix, you could use:
2437 (setq org-agenda-prefix-format \" %-11:c% s\")
2439 See also the variables `org-agenda-remove-times-when-in-prefix' and
2440 `org-agenda-remove-tags'."
2442 (string :tag
"General format")
2443 (list :greedy t
:tag
"View dependent"
2444 (cons (const agenda
) (string :tag
"Format"))
2445 (cons (const timeline
) (string :tag
"Format"))
2446 (cons (const todo
) (string :tag
"Format"))
2447 (cons (const tags
) (string :tag
"Format"))))
2448 :group
'org-agenda-prefix
)
2450 (defvar org-prefix-format-compiled nil
2451 "The compiled version of the most recently used prefix format.
2452 See the variable `org-agenda-prefix-format'.")
2454 (defcustom org-agenda-remove-times-when-in-prefix t
2455 "Non-nil means, remove duplicate time specifications in agenda items.
2456 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
2457 time-of-day specification in a headline or diary entry is extracted and
2458 placed into the prefix. If this option is non-nil, the original specification
2459 \(a timestamp or -range, or just a plain time(range) specification like
2460 11:30-4pm) will be removed for agenda display. This makes the agenda less
2462 The option can be t or nil. It may also be the symbol `beg', indicating
2463 that the time should only be removed what it is located at the beginning of
2464 the headline/diary entry."
2465 :group
'org-agenda-prefix
2467 (const :tag
"Always" t
)
2468 (const :tag
"Never" nil
)
2469 (const :tag
"When at beginning of entry" beg
)))
2472 (defcustom org-agenda-default-appointment-duration nil
2473 "Default duration for appointments that only have a starting time.
2474 When nil, no duration is specified in such cases.
2475 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
2476 :group
'org-agenda-prefix
2478 (integer :tag
"Minutes")
2479 (const :tag
"No default duration")))
2482 (defcustom org-agenda-remove-tags nil
2483 "Non-nil means, remove the tags from the headline copy in the agenda.
2484 When this is the symbol `prefix', only remove tags when
2485 `org-agenda-prefix-format' contains a `%T' specifier."
2486 :group
'org-agenda-prefix
2488 (const :tag
"Always" t
)
2489 (const :tag
"Never" nil
)
2490 (const :tag
"When prefix format contains %T" prefix
)))
2492 (if (fboundp 'defvaralias
)
2493 (defvaralias 'org-agenda-remove-tags-when-in-prefix
2494 'org-agenda-remove-tags
))
2496 (defcustom org-agenda-align-tags-to-column
65
2497 "Shift tags in agenda items to this column."
2498 :group
'org-agenda-prefix
2501 (defgroup org-latex nil
2502 "Options for embedding LaTeX code into Org-mode"
2506 (defcustom org-format-latex-options
2507 '(:foreground default
:background default
:scale
1.0
2508 :html-foreground
"Black" :html-background
"Transparent" :html-scale
1.0
2509 :matchers
("begin" "$" "$$" "\\(" "\\["))
2510 "Options for creating images from LaTeX fragments.
2511 This is a property list with the following properties:
2512 :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
2513 `default' means use the forground of the default face.
2514 :background the background color, or \"Transparent\".
2515 `default' means use the background of the default face.
2516 :scale a scaling factor for the size of the images
2517 :html-foreground, :html-background, :html-scale
2518 The same numbers for HTML export.
2519 :matchers a list indicating which matchers should be used to
2520 find LaTeX fragments. Valid members of this list are:
2521 \"begin\" find environments
2522 \"$\" find math expressions surrounded by $...$
2523 \"$$\" find math expressions surrounded by $$....$$
2524 \"\\(\" find math expressions surrounded by \\(...\\)
2525 \"\\ [\" find math expressions surrounded by \\ [...\\]"
2526 :group
'org-export-latex
2529 (defcustom org-format-latex-header
"\\documentclass{article}
2530 \\usepackage{fullpage} % do not remove
2531 \\usepackage{amssymb}
2532 \\usepackage[usenames]{color}
2533 \\usepackage{amsmath}
2534 \\usepackage{latexsym}
2535 \\usepackage[mathscr]{eucal}
2536 \\pagestyle{empty} % do not remove"
2537 "The document header used for processing LaTeX fragments."
2538 :group
'org-export-latex
2541 (defgroup org-export nil
2542 "Options for exporting org-listings."
2546 (defgroup org-export-general nil
2547 "General options for exporting Org-mode files."
2548 :tag
"Org Export General"
2551 (defcustom org-export-publishing-directory
"."
2552 "Path to the location where exported files should be located.
2553 This path may be relative to the directory where the Org-mode file lives.
2554 The default is to put them into the same directory as the Org-mode file.
2555 The variable may also be an alist with export types `:html', `:ascii',
2556 `:ical', or `:xoxo' and the corresponding directories. If a directory path
2557 is relative, it is interpreted relative to the directory where the exported
2558 Org-mode files lives."
2559 :group
'org-export-general
2565 (const :html
) (const :ascii
) (const :ical
) (const :xoxo
))
2568 (defcustom org-export-language-setup
2569 '(("en" "Author" "Date" "Table of Contents")
2570 ("cs" "Autor" "Datum" "Obsah")
2571 ("da" "Ophavsmand" "Dato" "Indhold")
2572 ("de" "Autor" "Datum" "Inhaltsverzeichnis")
2573 ("es" "Autor" "Fecha" "\xccndice")
2574 ("fr" "Auteur" "Date" "Table des Mati\xe8res")
2575 ("it" "Autore" "Data" "Indice")
2576 ("nl" "Auteur" "Datum" "Inhoudsopgave")
2577 ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
2578 ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
2579 "Terms used in export text, translated to different languages.
2580 Use the variable `org-export-default-language' to set the language,
2581 or use the +OPTION lines for a per-file setting."
2582 :group
'org-export-general
2585 (string :tag
"HTML language tag")
2586 (string :tag
"Author")
2587 (string :tag
"Date")
2588 (string :tag
"Table of Contents"))))
2590 (defcustom org-export-default-language
"en"
2591 "The default language of HTML export, as a string.
2592 This should have an association in `org-export-language-setup'."
2593 :group
'org-export-general
2596 (defcustom org-export-skip-text-before-1st-heading t
2597 "Non-nil means, skip all text before the first headline when exporting.
2598 When nil, that text is exported as well."
2599 :group
'org-export-general
2602 (defcustom org-export-headline-levels
3
2603 "The last level which is still exported as a headline.
2604 Inferior levels will produce itemize lists when exported.
2605 Note that a numeric prefix argument to an exporter function overrides
2608 This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
2609 :group
'org-export-general
2612 (defcustom org-export-with-section-numbers t
2613 "Non-nil means, add section numbers to headlines when exporting.
2615 This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
2616 :group
'org-export-general
2619 (defcustom org-export-with-toc t
2620 "Non-nil means, create a table of contents in exported files.
2621 The TOC contains headlines with levels up to`org-export-headline-levels'.
2622 When an integer, include levels up to N in the toc, this may then be
2623 different from `org-export-headline-levels', but it will not be allowed
2624 to be larger than the number of headline levels.
2625 When nil, no table of contents is made.
2627 Headlines which contain any TODO items will be marked with \"(*)\" in
2628 ASCII export, and with red color in HTML output, if the option
2629 `org-export-mark-todo-in-toc' is set.
2631 In HTML output, the TOC will be clickable.
2633 This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
2635 :group
'org-export-general
2637 (const :tag
"No Table of Contents" nil
)
2638 (const :tag
"Full Table of Contents" t
)
2639 (integer :tag
"TOC to level")))
2641 (defcustom org-export-mark-todo-in-toc nil
2642 "Non-nil means, mark TOC lines that contain any open TODO items."
2643 :group
'org-export-general
2646 (defcustom org-export-preserve-breaks nil
2647 "Non-nil means, preserve all line breaks when exporting.
2648 Normally, in HTML output paragraphs will be reformatted. In ASCII
2649 export, line breaks will always be preserved, regardless of this variable.
2651 This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
2652 :group
'org-export-general
2655 (defcustom org-export-with-archived-trees
'headline
2656 "Whether subtrees with the ARCHIVE tag should be exported.
2657 This can have three different values
2658 nil Do not export, pretend this tree is not present
2659 t Do export the entire tree
2660 headline Only export the headline, but skip the tree below it."
2661 :group
'org-export-general
2664 (const :tag
"not at all" nil
)
2665 (const :tag
"headline only" 'headline
)
2666 (const :tag
"entirely" t
)))
2668 (defcustom org-export-author-info t
2669 "Non-nil means, insert author name and email into the exported file.
2671 This option can also be set with the +OPTIONS line,
2672 e.g. \"author-info:nil\"."
2673 :group
'org-export-general
2676 (defcustom org-export-time-stamp-file t
2677 "Non-nil means, insert a time stamp into the exported file.
2678 The time stamp shows when the file was created.
2680 This option can also be set with the +OPTIONS line,
2681 e.g. \"timestamp:nil\"."
2682 :group
'org-export-general
2685 (defcustom org-export-with-timestamps t
2686 "If nil, do not export time stamps and associated keywords."
2687 :group
'org-export-general
2690 (defcustom org-export-remove-timestamps-from-toc t
2691 "If nil, remove timestamps from the table of contents entries."
2692 :group
'org-export-general
2695 (defcustom org-export-with-tags
'not-in-toc
2696 "If nil, do not export tags, just remove them from headlines.
2697 If this is the symbol `not-in-toc', tags will be removed from table of
2698 contents entries, but still be shown in the headlines of the document."
2699 :group
'org-export-general
2701 (const :tag
"Off" nil
)
2702 (const :tag
"Not in TOC" not-in-toc
)
2703 (const :tag
"On" t
)))
2705 (defcustom org-export-with-property-drawer nil
2706 "Non-nil means, export property drawers.
2707 When nil, these drawers are removed before export.
2709 This option can also be set with the +OPTIONS line, e.g. \"p:t\"."
2710 :group
'org-export-general
2713 (defgroup org-export-translation nil
2714 "Options for translating special ascii sequences for the export backends."
2715 :tag
"Org Export Translation"
2718 (defcustom org-export-with-emphasize t
2719 "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
2720 If the export target supports emphasizing text, the word will be
2721 typeset in bold, italic, or underlined, respectively. Works only for
2722 single words, but you can say: I *really* *mean* *this*.
2723 Not all export backends support this.
2725 This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
2726 :group
'org-export-translation
2729 (defcustom org-export-with-footnotes t
2730 "If nil, export [1] as a footnote marker.
2731 Lines starting with [1] will be formatted as footnotes.
2733 This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
2734 :group
'org-export-translation
2737 (defcustom org-export-with-sub-superscripts t
2738 "Non-nil means, interpret \"_\" and \"^\" for export.
2739 When this option is turned on, you can use TeX-like syntax for sub- and
2740 superscripts. Several characters after \"_\" or \"^\" will be
2741 considered as a single item - so grouping with {} is normally not
2742 needed. For example, the following things will be parsed as single
2743 sub- or superscripts.
2745 10^24 or 10^tau several digits will be considered 1 item.
2746 10^-12 or 10^-tau a leading sign with digits or a word
2747 x^2-y^3 will be read as x^2 - y^3, because items are
2748 terminated by almost any nonword/nondigit char.
2749 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
2751 Still, ambiguity is possible - so when in doubt use {} to enclose the
2752 sub/superscript. If you set this variable to the symbol `{}',
2753 the braces are *required* in order to trigger interpretations as
2754 sub/superscript. This can be helpful in documents that need \"_\"
2755 frequently in plain text.
2757 Not all export backends support this, but HTML does.
2759 This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
2760 :group
'org-export-translation
2762 (const :tag
"Always interpret" t
)
2763 (const :tag
"Only with braces" {})
2764 (const :tag
"Never interpret" nil
)))
2766 (defcustom org-export-with-TeX-macros t
2767 "Non-nil means, interpret simple TeX-like macros when exporting.
2768 For example, HTML export converts \\alpha to α and \\AA to Å.
2769 No only real TeX macros will work here, but the standard HTML entities
2770 for math can be used as macro names as well. For a list of supported
2771 names in HTML export, see the constant `org-html-entities'.
2772 Not all export backends support this.
2774 This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
2775 :group
'org-export-translation
2776 :group
'org-export-latex
2779 (defcustom org-export-with-LaTeX-fragments nil
2780 "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
2781 When set, the exporter will find LaTeX environments if the \\begin line is
2782 the first non-white thing on a line. It will also find the math delimiters
2783 like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
2786 This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
2787 :group
'org-export-translation
2788 :group
'org-export-latex
2791 (defcustom org-export-with-fixed-width t
2792 "Non-nil means, lines starting with \":\" will be in fixed width font.
2793 This can be used to have pre-formatted text, fragments of code etc. For
2795 : ;; Some Lisp examples
2798 will be looking just like this in also HTML. See also the QUOTE keyword.
2799 Not all export backends support this.
2801 This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
2802 :group
'org-export-translation
2805 (defcustom org-match-sexp-depth
3
2806 "Number of stacked braces for sub/superscript matching.
2807 This has to be set before loading org.el to be effective."
2808 :group
'org-export-translation
2811 (defgroup org-export-tables nil
2812 "Options for exporting tables in Org-mode."
2813 :tag
"Org Export Tables"
2816 (defcustom org-export-with-tables t
2817 "If non-nil, lines starting with \"|\" define a table.
2820 | Name | Address | Birthday |
2821 |-------------+----------+-----------|
2822 | Arthur Dent | England | 29.2.2100 |
2824 Not all export backends support this.
2826 This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
2827 :group
'org-export-tables
2830 (defcustom org-export-highlight-first-table-line t
2831 "Non-nil means, highlight the first table line.
2832 In HTML export, this means use <th> instead of <td>.
2833 In tables created with table.el, this applies to the first table line.
2834 In Org-mode tables, all lines before the first horizontal separator
2835 line will be formatted with <th> tags."
2836 :group
'org-export-tables
2839 (defcustom org-export-table-remove-special-lines t
2840 "Remove special lines and marking characters in calculating tables.
2841 This removes the special marking character column from tables that are set
2842 up for spreadsheet calculations. It also removes the entire lines
2843 marked with `!', `_', or `^'. The lines with `$' are kept, because
2844 the values of constants may be useful to have."
2845 :group
'org-export-tables
2848 (defcustom org-export-prefer-native-exporter-for-tables nil
2849 "Non-nil means, always export tables created with table.el natively.
2850 Natively means, use the HTML code generator in table.el.
2851 When nil, Org-mode's own HTML generator is used when possible (i.e. if
2852 the table does not use row- or column-spanning). This has the
2853 advantage, that the automatic HTML conversions for math symbols and
2854 sub/superscripts can be applied. Org-mode's HTML generator is also
2856 :group
'org-export-tables
2859 (defgroup org-export-ascii nil
2860 "Options specific for ASCII export of Org-mode files."
2861 :tag
"Org Export ASCII"
2864 (defcustom org-export-ascii-underline
'(?\$ ?\
# ?^ ?\~ ?\
= ?\-
)
2865 "Characters for underlining headings in ASCII export.
2866 In the given sequence, these characters will be used for level 1, 2, ..."
2867 :group
'org-export-ascii
2868 :type
'(repeat character
))
2870 (defcustom org-export-ascii-bullets
'(?
* ?
+ ?-
)
2871 "Bullet characters for headlines converted to lists in ASCII export.
2872 The first character is is used for the first lest level generated in this
2873 way, and so on. If there are more levels than characters given here,
2874 the list will be repeated.
2875 Note that plain lists will keep the same bullets as the have in the
2877 :group
'org-export-ascii
2878 :type
'(repeat character
))
2880 (defgroup org-export-xml nil
2881 "Options specific for XML export of Org-mode files."
2882 :tag
"Org Export XML"
2885 (defgroup org-export-html nil
2886 "Options specific for HTML export of Org-mode files."
2887 :tag
"Org Export HTML"
2890 (defcustom org-export-html-coding-system nil
2892 :group
'org-export-html
2893 :type
'coding-system
)
2895 (defcustom org-export-html-style
2896 "<style type=\"text/css\">
2898 font-family: Times, serif;
2901 .title { text-align: center; }
2902 .todo { color: red; }
2903 .done { color: green; }
2904 .timestamp { color: grey }
2905 .timestamp-kwd { color: CadetBlue }
2906 .tag { background-color:lightblue; font-weight:normal }
2907 .target { background-color: lavender; }
2909 border: 1pt solid #AEBDCC;
2910 background-color: #F3F5F7;
2912 font-family: courier, monospace;
2914 table { border-collapse: collapse; }
2916 vertical-align: top;
2917 <!--border: 1pt solid #ADB9CC;-->
2920 "The default style specification for exported HTML files.
2921 Since there are different ways of setting style information, this variable
2922 needs to contain the full HTML structure to provide a style, including the
2923 surrounding HTML tags. The style specifications should include definitions
2924 for new classes todo, done, title, and deadline. For example, legal values
2927 <style type=\"text/css\">
2928 p { font-weight: normal; color: gray; }
2929 h1 { color: black; }
2930 .title { text-align: center; }
2931 .todo, .deadline { color: red; }
2932 .done { color: green; }
2935 or, if you want to keep the style in a file,
2937 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
2939 As the value of this option simply gets inserted into the HTML <head> header,
2940 you can \"misuse\" it to add arbitrary text to the header."
2941 :group
'org-export-html
2945 (defcustom org-export-html-title-format
"<h1 class=\"title\">%s</h1>\n"
2946 "Format for typesetting the document title in HTML export."
2947 :group
'org-export-html
2950 (defcustom org-export-html-toplevel-hlevel
2
2951 "The <H> level for level 1 headings in HTML export."
2952 :group
'org-export-html
2955 (defcustom org-export-html-link-org-files-as-html t
2956 "Non-nil means, make file links to `file.org' point to `file.html'.
2957 When org-mode is exporting an org-mode file to HTML, links to
2958 non-html files are directly put into a href tag in HTML.
2959 However, links to other Org-mode files (recognized by the
2960 extension `.org.) should become links to the corresponding html
2961 file, assuming that the linked org-mode file will also be
2963 When nil, the links still point to the plain `.org' file."
2964 :group
'org-export-html
2967 (defcustom org-export-html-inline-images
'maybe
2968 "Non-nil means, inline images into exported HTML pages.
2969 This is done using an <img> tag. When nil, an anchor with href is used to
2970 link to the image. If this option is `maybe', then images in links with
2971 an empty description will be inlined, while images with a description will
2973 :group
'org-export-html
2974 :type
'(choice (const :tag
"Never" nil
)
2975 (const :tag
"Always" t
)
2976 (const :tag
"When there is no description" maybe
)))
2979 (defcustom org-export-html-expand t
2980 "Non-nil means, for HTML export, treat @<...> as HTML tag.
2981 When nil, these tags will be exported as plain text and therefore
2982 not be interpreted by a browser.
2984 This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
2985 :group
'org-export-html
2988 (defcustom org-export-html-table-tag
2989 "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
2990 "The HTML tag that is used to start a table.
2991 This must be a <table> tag, but you may change the options like
2992 borders and spacing."
2993 :group
'org-export-html
2996 (defcustom org-export-table-header-tags
'("<th>" .
"</th>")
2997 "The opening tag for table header fields.
2998 This is customizable so that alignment options can be specified."
2999 :group
'org-export-tables
3000 :type
'(cons (string :tag
"Opening tag") (string :tag
"Closing tag")))
3002 (defcustom org-export-table-data-tags
'("<td>" .
"</td>")
3003 "The opening tag for table data fields.
3004 This is customizable so that alignment options can be specified."
3005 :group
'org-export-tables
3006 :type
'(cons (string :tag
"Opening tag") (string :tag
"Closing tag")))
3008 (defcustom org-export-html-with-timestamp nil
3009 "If non-nil, write `org-export-html-html-helper-timestamp'
3010 into the exported HTML text. Otherwise, the buffer will just be saved
3012 :group
'org-export-html
3015 (defcustom org-export-html-html-helper-timestamp
3016 "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
3017 "The HTML tag used as timestamp delimiter for HTML-helper-mode."
3018 :group
'org-export-html
3021 (defgroup org-export-icalendar nil
3022 "Options specific for iCalendar export of Org-mode files."
3023 :tag
"Org Export iCalendar"
3026 (defcustom org-combined-agenda-icalendar-file
"~/org.ics"
3027 "The file name for the iCalendar file covering all agenda files.
3028 This file is created with the command \\[org-export-icalendar-all-agenda-files].
3029 The file name should be absolute."
3030 :group
'org-export-icalendar
3033 (defcustom org-icalendar-include-todo nil
3034 "Non-nil means, export to iCalendar files should also cover TODO items."
3035 :group
'org-export-icalendar
3037 (const :tag
"None" nil
)
3038 (const :tag
"Unfinished" t
)
3039 (const :tag
"All" all
)))
3041 (defcustom org-icalendar-include-sexps t
3042 "Non-nil means, export to iCalendar files should also cover sexp entries.
3043 These are entries like in the diary, but directly in an Org-mode file."
3044 :group
'org-export-icalendar
3047 (defcustom org-icalendar-combined-name
"OrgMode"
3048 "Calendar name for the combined iCalendar representing all agenda files."
3049 :group
'org-export-icalendar
3052 (defgroup org-font-lock nil
3053 "Font-lock settings for highlighting in Org-mode."
3054 :tag
"Org Font Lock"
3057 (defcustom org-level-color-stars-only nil
3058 "Non-nil means fontify only the stars in each headline.
3059 When nil, the entire headline is fontified.
3060 Changing it requires restart of `font-lock-mode' to become effective
3061 also in regions already fontified."
3062 :group
'org-font-lock
3065 (defcustom org-hide-leading-stars nil
3066 "Non-nil means, hide the first N-1 stars in a headline.
3067 This works by using the face `org-hide' for these stars. This
3068 face is white for a light background, and black for a dark
3069 background. You may have to customize the face `org-hide' to
3071 Changing it requires restart of `font-lock-mode' to become effective
3072 also in regions already fontified.
3073 You may also set this on a per-file basis by adding one of the following
3074 lines to the buffer:
3076 #+STARTUP: hidestars
3077 #+STARTUP: showstars"
3078 :group
'org-font-lock
3081 (defcustom org-fontify-done-headline nil
3082 "Non-nil means, change the face of a headline if it is marked DONE.
3083 Normally, only the TODO/DONE keyword indicates the state of a headline.
3084 When this is non-nil, the headline after the keyword is set to the
3085 `org-headline-done' as an additional indication."
3086 :group
'org-font-lock
3089 (defcustom org-fontify-emphasized-text t
3090 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
3091 Changing this variable requires a restart of Emacs to take effect."
3092 :group
'org-font-lock
3095 (defvar org-emph-re nil
3096 "Regular expression for matching emphasis.")
3097 (defvar org-emphasis-regexp-components
) ; defined just below
3098 (defvar org-emphasis-alist
) ; defined just below
3099 (defun org-set-emph-re (var val
)
3100 "Set variable and compute the emphasis regular expression."
3102 (when (and (boundp 'org-emphasis-alist
)
3103 (boundp 'org-emphasis-regexp-components
)
3104 org-emphasis-alist org-emphasis-regexp-components
)
3105 (let* ((e org-emphasis-regexp-components
)
3112 (body1 (concat body
"*?"))
3113 (markers (mapconcat 'car org-emphasis-alist
"")))
3114 ;; make sure special characters appear at the right position in the class
3115 (if (string-match "\\^" markers
)
3116 (setq markers
(concat (replace-match "" t t markers
) "^")))
3117 (if (string-match "-" markers
)
3118 (setq markers
(concat (replace-match "" t t markers
) "-")))
3120 (setq body1
(concat body1
"\\(?:\n" body
"*?\\)\\{0,"
3121 (int-to-string nl
) "\\}")))
3124 (concat "\\([" pre
(if stacked markers
) "]\\|^\\)"
3126 "\\([" markers
"]\\)"
3128 "[^" border
(if (and nil stacked
) markers
) "]"
3130 "[^" border
(if (and nil stacked
) markers
) "]"
3133 "\\([" post
(if stacked markers
) "]\\|$\\)")))))
3135 (defcustom org-emphasis-regexp-components
3136 '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1 nil
)
3137 "Components used to build the reqular expression for emphasis.
3138 This is a list with 6 entries. Terminology: In an emphasis string
3139 like \" *strong word* \", we call the initial space PREMATCH, the final
3140 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
3141 and \"trong wor\" is the body. The different components in this variable
3142 specify what is allowed/forbidden in each part:
3144 pre Chars allowed as prematch. Beginning of line will be allowed too.
3145 post Chars allowed as postmatch. End of line will be allowed too.
3146 border The chars *forbidden* as border characters.
3147 body-regexp A regexp like \".\" to match a body character. Don't use
3148 non-shy groups here, and don't allow newline here.
3149 newline The maximum number of newlines allowed in an emphasis exp.
3150 stacked Non-nil means, allow stacked styles. This works only in HTML
3151 export. When this is set, all marker characters (as given in
3152 `org-emphasis-alist') will be allowed as pre/post, aiding
3153 inside-out matching.
3154 Use customize to modify this, or restart Emacs after changing it."
3155 :group
'org-font-lock
3156 :set
'org-set-emph-re
3158 (sexp :tag
"Allowed chars in pre ")
3159 (sexp :tag
"Allowed chars in post ")
3160 (sexp :tag
"Forbidden chars in border ")
3161 (sexp :tag
"Regexp for body ")
3162 (integer :tag
"number of newlines allowed")
3163 (boolean :tag
"Stacking allowed ")))
3165 (defcustom org-emphasis-alist
3166 '(("*" bold
"<b>" "</b>")
3167 ("/" italic
"<i>" "</i>")
3168 ("_" underline
"<u>" "</u>")
3169 ("=" org-code
"<code>" "</code>")
3170 ("+" (:strike-through t
) "<del>" "</del>")
3172 "Special syntax for emphasized text.
3173 Text starting and ending with a special character will be emphasized, for
3174 example *bold*, _underlined_ and /italic/. This variable sets the marker
3175 characters, the face to be used by font-lock for highlighting in Org-mode
3176 Emacs buffers, and the HTML tags to be used for this.
3177 Use customize to modify this, or restart Emacs after changing it."
3178 :group
'org-font-lock
3179 :set
'org-set-emph-re
3182 (string :tag
"Marker character")
3184 (face :tag
"Font-lock-face")
3185 (plist :tag
"Face property list"))
3186 (string :tag
"HTML start tag")
3187 (string :tag
"HTML end tag"))))
3191 (defgroup org-faces nil
3192 "Faces in Org-mode."
3194 :group
'org-font-lock
)
3196 ;; FIXME: convert that into a macro? Not critical, because this
3197 ;; is only executed a few times at load time.
3198 (defun org-compatible-face (specs)
3199 "Make a compatible face specification.
3200 XEmacs and Emacs 21 do not know about the `min-colors' attribute.
3201 For them we convert a (min-colors 8) entry to a `tty' entry and move it
3202 to the top of the list. The `min-colors' attribute will be removed from
3203 any other entries, and any resulting duplicates will be removed entirely."
3204 (if (or (featurep 'xemacs
) (< emacs-major-version
22))
3206 (while (setq e
(pop specs
))
3208 ((memq (car e
) '(t default
)) (push e r
))
3209 ((setq a
(member '(min-colors 8) (car e
)))
3210 (nconc r
(list (cons (cons '(type tty
) (delq (car a
) (car e
)))
3212 ((setq a
(assq 'min-colors
(car e
)))
3213 (setq e
(cons (delq a
(car e
)) (cdr e
)))
3214 (or (assoc (car e
) r
) (push e r
)))
3215 (t (or (assoc (car e
) r
) (push e r
)))))
3220 '((((background light
)) (:foreground
"white"))
3221 (((background dark
)) (:foreground
"black")))
3222 "Face used to hide leading stars in headlines.
3223 The forground color of this face should be equal to the background
3224 color of the frame."
3227 (defface org-level-1
;; font-lock-function-name-face
3228 (org-compatible-face
3229 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Blue1"))
3230 (((class color
) (min-colors 88) (background dark
)) (:foreground
"LightSkyBlue"))
3231 (((class color
) (min-colors 16) (background light
)) (:foreground
"Blue"))
3232 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSkyBlue"))
3233 (((class color
) (min-colors 8)) (:foreground
"blue" :bold t
))
3235 "Face used for level 1 headlines."
3238 (defface org-level-2
;; font-lock-variable-name-face
3239 (org-compatible-face
3240 '((((class color
) (min-colors 16) (background light
)) (:foreground
"DarkGoldenrod"))
3241 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightGoldenrod"))
3242 (((class color
) (min-colors 8) (background light
)) (:foreground
"yellow"))
3243 (((class color
) (min-colors 8) (background dark
)) (:foreground
"yellow" :bold t
))
3245 "Face used for level 2 headlines."
3248 (defface org-level-3
;; font-lock-keyword-face
3249 (org-compatible-face
3250 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Purple"))
3251 (((class color
) (min-colors 88) (background dark
)) (:foreground
"Cyan1"))
3252 (((class color
) (min-colors 16) (background light
)) (:foreground
"Purple"))
3253 (((class color
) (min-colors 16) (background dark
)) (:foreground
"Cyan"))
3254 (((class color
) (min-colors 8) (background light
)) (:foreground
"purple" :bold t
))
3255 (((class color
) (min-colors 8) (background dark
)) (:foreground
"cyan" :bold t
))
3257 "Face used for level 3 headlines."
3260 (defface org-level-4
;; font-lock-comment-face
3261 (org-compatible-face
3262 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Firebrick"))
3263 (((class color
) (min-colors 88) (background dark
)) (:foreground
"chocolate1"))
3264 (((class color
) (min-colors 16) (background light
)) (:foreground
"red"))
3265 (((class color
) (min-colors 16) (background dark
)) (:foreground
"red1"))
3266 (((class color
) (min-colors 8) (background light
)) (:foreground
"red" :bold t
))
3267 (((class color
) (min-colors 8) (background dark
)) (:foreground
"red" :bold t
))
3269 "Face used for level 4 headlines."
3272 (defface org-level-5
;; font-lock-type-face
3273 (org-compatible-face
3274 '((((class color
) (min-colors 16) (background light
)) (:foreground
"ForestGreen"))
3275 (((class color
) (min-colors 16) (background dark
)) (:foreground
"PaleGreen"))
3276 (((class color
) (min-colors 8)) (:foreground
"green"))))
3277 "Face used for level 5 headlines."
3280 (defface org-level-6
;; font-lock-constant-face
3281 (org-compatible-face
3282 '((((class color
) (min-colors 16) (background light
)) (:foreground
"CadetBlue"))
3283 (((class color
) (min-colors 16) (background dark
)) (:foreground
"Aquamarine"))
3284 (((class color
) (min-colors 8)) (:foreground
"magenta"))))
3285 "Face used for level 6 headlines."
3288 (defface org-level-7
;; font-lock-builtin-face
3289 (org-compatible-face
3290 '((((class color
) (min-colors 16) (background light
)) (:foreground
"Orchid"))
3291 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSteelBlue"))
3292 (((class color
) (min-colors 8)) (:foreground
"blue"))))
3293 "Face used for level 7 headlines."
3296 (defface org-level-8
;; font-lock-string-face
3297 (org-compatible-face
3298 '((((class color
) (min-colors 16) (background light
)) (:foreground
"RosyBrown"))
3299 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSalmon"))
3300 (((class color
) (min-colors 8)) (:foreground
"green"))))
3301 "Face used for level 8 headlines."
3304 (defface org-special-keyword
;; font-lock-string-face
3305 (org-compatible-face
3306 '((((class color
) (min-colors 16) (background light
)) (:foreground
"RosyBrown"))
3307 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSalmon"))
3309 "Face used for special keywords."
3312 (defface org-drawer
;; font-lock-function-name-face
3313 (org-compatible-face
3314 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Blue1"))
3315 (((class color
) (min-colors 88) (background dark
)) (:foreground
"LightSkyBlue"))
3316 (((class color
) (min-colors 16) (background light
)) (:foreground
"Blue"))
3317 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSkyBlue"))
3318 (((class color
) (min-colors 8)) (:foreground
"blue" :bold t
))
3320 "Face used for drawers."
3323 (defface org-property-value nil
3324 "Face used for the value of a property."
3328 (org-compatible-face
3329 '((((class color
) (min-colors 16) (background light
))
3330 (:background
"grey90"))
3331 (((class color
) (min-colors 16) (background dark
))
3332 (:background
"grey30"))
3333 (((class color
) (min-colors 8))
3334 (:background
"cyan" :foreground
"black"))
3335 (t (:inverse-video t
))))
3336 "Face for column display of entry properties."
3339 (when (fboundp 'set-face-attribute
)
3340 ;; Make sure that a fixed-width face is used when we have a column table.
3341 (set-face-attribute 'org-column nil
3342 :height
(face-attribute 'default
:height
)
3343 :family
(face-attribute 'default
:family
)))
3345 (defface org-warning
;; font-lock-warning-face
3346 (org-compatible-face
3347 '((((class color
) (min-colors 16) (background light
)) (:foreground
"Red1" :bold t
))
3348 (((class color
) (min-colors 16) (background dark
)) (:foreground
"Pink" :bold t
))
3349 (((class color
) (min-colors 8) (background light
)) (:foreground
"red" :bold t
))
3350 (((class color
) (min-colors 8) (background dark
)) (:foreground
"red" :bold t
))
3352 "Face for deadlines and TODO keywords."
3355 (defface org-archived
; similar to shadow
3356 (org-compatible-face
3357 '((((class color grayscale
) (min-colors 88) (background light
))
3358 (:foreground
"grey50"))
3359 (((class color grayscale
) (min-colors 88) (background dark
))
3360 (:foreground
"grey70"))
3361 (((class color
) (min-colors 8) (background light
))
3362 (:foreground
"green"))
3363 (((class color
) (min-colors 8) (background dark
))
3364 (:foreground
"yellow"))))
3365 "Face for headline with the ARCHIVE tag."
3369 '((((class color
) (background light
)) (:foreground
"Purple" :underline t
))
3370 (((class color
) (background dark
)) (:foreground
"Cyan" :underline t
))
3376 '((((class color
) (background light
)) (:underline t
))
3377 (((class color
) (background dark
)) (:underline t
))
3383 '((((class color
) (background light
)) (:foreground
"Purple" :underline t
))
3384 (((class color
) (background dark
)) (:foreground
"Cyan" :underline t
))
3389 (defface org-sexp-date
3390 '((((class color
) (background light
)) (:foreground
"Purple"))
3391 (((class color
) (background dark
)) (:foreground
"Cyan"))
3401 (defface org-todo
;; font-lock-warning-face
3402 (org-compatible-face
3403 '((((class color
) (min-colors 16) (background light
)) (:foreground
"Red1" :bold t
))
3404 (((class color
) (min-colors 16) (background dark
)) (:foreground
"Pink" :bold t
))
3405 (((class color
) (min-colors 8) (background light
)) (:foreground
"red" :bold t
))
3406 (((class color
) (min-colors 8) (background dark
)) (:foreground
"red" :bold t
))
3407 (t (:inverse-video t
:bold t
))))
3408 "Face for TODO keywords."
3411 (defface org-done
;; font-lock-type-face
3412 (org-compatible-face
3413 '((((class color
) (min-colors 16) (background light
)) (:foreground
"ForestGreen" :bold t
))
3414 (((class color
) (min-colors 16) (background dark
)) (:foreground
"PaleGreen" :bold t
))
3415 (((class color
) (min-colors 8)) (:foreground
"green"))
3417 "Face used for todo keywords that indicate DONE items."
3420 (defface org-headline-done
;; font-lock-string-face
3421 (org-compatible-face
3422 '((((class color
) (min-colors 16) (background light
)) (:foreground
"RosyBrown"))
3423 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSalmon"))
3424 (((class color
) (min-colors 8) (background light
)) (:bold nil
))))
3425 "Face used to indicate that a headline is DONE.
3426 This face is only used if `org-fontify-done-headline' is set. If applies
3427 to the part of the headline after the DONE keyword."
3430 (defface org-table
;; font-lock-function-name-face
3431 (org-compatible-face
3432 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Blue1"))
3433 (((class color
) (min-colors 88) (background dark
)) (:foreground
"LightSkyBlue"))
3434 (((class color
) (min-colors 16) (background light
)) (:foreground
"Blue"))
3435 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSkyBlue"))
3436 (((class color
) (min-colors 8) (background light
)) (:foreground
"blue"))
3437 (((class color
) (min-colors 8) (background dark
)))))
3438 "Face used for tables."
3441 (defface org-formula
3442 (org-compatible-face
3443 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Firebrick"))
3444 (((class color
) (min-colors 88) (background dark
)) (:foreground
"chocolate1"))
3445 (((class color
) (min-colors 8) (background light
)) (:foreground
"red"))
3446 (((class color
) (min-colors 8) (background dark
)) (:foreground
"red"))
3447 (t (:bold t
:italic t
))))
3448 "Face for formulas."
3452 (org-compatible-face
3453 '((((class color grayscale
) (min-colors 88) (background light
))
3454 :foreground
"grey50")
3455 (((class color grayscale
) (min-colors 88) (background dark
))
3456 :foreground
"grey70")
3457 (((class color
) (min-colors 8) (background light
))
3458 :foreground
"green")
3459 (((class color
) (min-colors 8) (background dark
))
3460 :foreground
"yellow")))
3461 "Face for fixed-with text like code snippets."
3465 (defface org-agenda-structure
;; font-lock-function-name-face
3466 (org-compatible-face
3467 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Blue1"))
3468 (((class color
) (min-colors 88) (background dark
)) (:foreground
"LightSkyBlue"))
3469 (((class color
) (min-colors 16) (background light
)) (:foreground
"Blue"))
3470 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSkyBlue"))
3471 (((class color
) (min-colors 8)) (:foreground
"blue" :bold t
))
3473 "Face used in agenda for captions and dates."
3476 (defface org-scheduled-today
3477 (org-compatible-face
3478 '((((class color
) (min-colors 88) (background light
)) (:foreground
"DarkGreen"))
3479 (((class color
) (min-colors 88) (background dark
)) (:foreground
"PaleGreen"))
3480 (((class color
) (min-colors 8)) (:foreground
"green"))
3481 (t (:bold t
:italic t
))))
3482 "Face for items scheduled for a certain day."
3485 (defface org-scheduled-previously
3486 (org-compatible-face
3487 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Firebrick"))
3488 (((class color
) (min-colors 88) (background dark
)) (:foreground
"chocolate1"))
3489 (((class color
) (min-colors 8) (background light
)) (:foreground
"red"))
3490 (((class color
) (min-colors 8) (background dark
)) (:foreground
"red" :bold t
))
3492 "Face for items scheduled previously, and not yet done."
3495 (defface org-upcoming-deadline
3496 (org-compatible-face
3497 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Firebrick"))
3498 (((class color
) (min-colors 88) (background dark
)) (:foreground
"chocolate1"))
3499 (((class color
) (min-colors 8) (background light
)) (:foreground
"red"))
3500 (((class color
) (min-colors 8) (background dark
)) (:foreground
"red" :bold t
))
3502 "Face for items scheduled previously, and not yet done."
3505 (defcustom org-agenda-deadline-faces
3506 '((1.0 . org-warning
)
3507 (0.5 . org-upcoming-deadline
)
3509 "Faces for showing deadlines in the agenda.
3510 This is a list of cons cells. The cdr of each cess is a face to be used,
3511 and it can also just be a like like '(:foreground \"yellow\").
3512 Each car is a fraction of the head-warning time that must have passed for
3513 this the face in the cdr to be used for display. The numbers must be
3514 given in descending order. The head-warning time is normally taken
3515 from `org-deadline-warning-days', but can also be specified in the deadline
3516 timestamp itself, like this:
3518 DEADLINE: <2007-08-13 Mon -8d>
3520 You may use d for days, w for weeks, m for months and y for years. Months
3521 and years will only be treated in an approximate fashion (30.4 days for a
3522 month and 365.24 days for a year)."
3524 :group
'org-agenda-daily
/weekly
3527 (number :tag
"Fraction of head-warning time passed")
3528 (sexp :tag
"Face"))))
3530 (defface org-time-grid
;; font-lock-variable-name-face
3531 (org-compatible-face
3532 '((((class color
) (min-colors 16) (background light
)) (:foreground
"DarkGoldenrod"))
3533 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightGoldenrod"))
3534 (((class color
) (min-colors 8)) (:foreground
"yellow" :weight light
))))
3535 "Face used for time grids."
3538 (defconst org-level-faces
3539 '(org-level-1 org-level-2 org-level-3 org-level-4
3540 org-level-5 org-level-6 org-level-7 org-level-8
3543 (defcustom org-n-level-faces
(length org-level-faces
)
3544 "The number different faces to be used for headlines.
3545 Org-mode defines 8 different headline faces, so this can be at most 8.
3546 If it is less than 8, the level-1 face gets re-used for level N+1 etc."
3550 ;;; Variables for pre-computed regular expressions, all buffer local
3552 (defvar org-drawer-regexp nil
3553 "Matches first line of a hidden block.")
3554 (make-variable-buffer-local 'org-drawer-regexp
)
3555 (defvar org-todo-regexp nil
3556 "Matches any of the TODO state keywords.")
3557 (make-variable-buffer-local 'org-todo-regexp
)
3558 (defvar org-not-done-regexp nil
3559 "Matches any of the TODO state keywords except the last one.")
3560 (make-variable-buffer-local 'org-not-done-regexp
)
3561 (defvar org-todo-line-regexp nil
3562 "Matches a headline and puts TODO state into group 2 if present.")
3563 (make-variable-buffer-local 'org-todo-line-regexp
)
3564 (defvar org-todo-line-tags-regexp nil
3565 "Matches a headline and puts TODO state into group 2 if present.
3566 Also put tags into group 4 if tags are present.")
3567 (make-variable-buffer-local 'org-todo-line-tags-regexp
)
3568 (defvar org-nl-done-regexp nil
3569 "Matches newline followed by a headline with the DONE keyword.")
3570 (make-variable-buffer-local 'org-nl-done-regexp
)
3571 (defvar org-looking-at-done-regexp nil
3572 "Matches the DONE keyword a point.")
3573 (make-variable-buffer-local 'org-looking-at-done-regexp
)
3574 (defvar org-ds-keyword-length
12
3575 "Maximum length of the Deadline and SCHEDULED keywords.")
3576 (make-variable-buffer-local 'org-ds-keyword-length
)
3577 (defvar org-deadline-regexp nil
3578 "Matches the DEADLINE keyword.")
3579 (make-variable-buffer-local 'org-deadline-regexp
)
3580 (defvar org-deadline-time-regexp nil
3581 "Matches the DEADLINE keyword together with a time stamp.")
3582 (make-variable-buffer-local 'org-deadline-time-regexp
)
3583 (defvar org-deadline-line-regexp nil
3584 "Matches the DEADLINE keyword and the rest of the line.")
3585 (make-variable-buffer-local 'org-deadline-line-regexp
)
3586 (defvar org-scheduled-regexp nil
3587 "Matches the SCHEDULED keyword.")
3588 (make-variable-buffer-local 'org-scheduled-regexp
)
3589 (defvar org-scheduled-time-regexp nil
3590 "Matches the SCHEDULED keyword together with a time stamp.")
3591 (make-variable-buffer-local 'org-scheduled-time-regexp
)
3592 (defvar org-closed-time-regexp nil
3593 "Matches the CLOSED keyword together with a time stamp.")
3594 (make-variable-buffer-local 'org-closed-time-regexp
)
3596 (defvar org-keyword-time-regexp nil
3597 "Matches any of the 4 keywords, together with the time stamp.")
3598 (make-variable-buffer-local 'org-keyword-time-regexp
)
3599 (defvar org-keyword-time-not-clock-regexp nil
3600 "Matches any of the 3 keywords, together with the time stamp.")
3601 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp
)
3602 (defvar org-maybe-keyword-time-regexp nil
3603 "Matches a timestamp, possibly preceeded by a keyword.")
3604 (make-variable-buffer-local 'org-maybe-keyword-time-regexp
)
3605 (defvar org-planning-or-clock-line-re nil
3606 "Matches a line with planning or clock info.")
3607 (make-variable-buffer-local 'org-planning-or-clock-line-re
)
3609 (defconst org-rm-props
'(invisible t face t keymap t intangible t mouse-face t
3610 rear-nonsticky t mouse-map t fontified t
)
3611 "Properties to remove when a string without properties is wanted.")
3613 (defsubst org-match-string-no-properties
(num &optional string
)
3614 (if (featurep 'xemacs
)
3615 (let ((s (match-string num string
)))
3616 (remove-text-properties 0 (length s
) org-rm-props s
)
3618 (match-string-no-properties num string
)))
3620 (defsubst org-no-properties
(s)
3621 (if (fboundp 'set-text-properties
)
3622 (set-text-properties 0 (length s
) nil s
)
3623 (remove-text-properties 0 (length s
) org-rm-props s
))
3626 (defsubst org-get-alist-option
(option key
)
3627 (cond ((eq key t
) t
)
3629 ((assoc key option
) (cdr (assoc key option
)))
3630 (t (cdr (assq 'default option
)))))
3632 (defsubst org-inhibit-invisibility
()
3633 "Modified `buffer-invisibility-spec' for Emacs 21.
3634 Some ops with invisible text do not work correctly on Emacs 21. For these
3635 we turn off invisibility temporarily. Use this in a `let' form."
3636 (if (< emacs-major-version
22) nil buffer-invisibility-spec
))
3638 (defsubst org-set-local
(var value
)
3639 "Make VAR local in current buffer and set it to VALUE."
3640 (set (make-variable-buffer-local var
) value
))
3642 (defsubst org-mode-p
()
3643 "Check if the current buffer is in Org-mode."
3644 (eq major-mode
'org-mode
))
3646 (defsubst org-last
(list)
3647 "Return the last element of LIST."
3650 (defun org-let (list &rest body
)
3651 (eval (cons 'let
(cons list body
))))
3652 (put 'org-let
'lisp-indent-function
1)
3654 (defun org-let2 (list1 list2
&rest body
)
3655 (eval (cons 'let
(cons list1
(list (cons 'let
(cons list2 body
)))))))
3656 (put 'org-let2
'lisp-indent-function
2)
3657 (defconst org-startup-options
3658 '(("fold" org-startup-folded t
)
3659 ("overview" org-startup-folded t
)
3660 ("nofold" org-startup-folded nil
)
3661 ("showall" org-startup-folded nil
)
3662 ("content" org-startup-folded content
)
3663 ("hidestars" org-hide-leading-stars t
)
3664 ("showstars" org-hide-leading-stars nil
)
3665 ("odd" org-odd-levels-only t
)
3666 ("oddeven" org-odd-levels-only nil
)
3667 ("align" org-startup-align-all-tables t
)
3668 ("noalign" org-startup-align-all-tables nil
)
3669 ("customtime" org-display-custom-times t
)
3670 ("logging" org-log-done t
)
3671 ("logdone" org-log-done t
)
3672 ("nologging" org-log-done nil
)
3673 ("lognotedone" org-log-done done push
)
3674 ("lognotestate" org-log-done state push
)
3675 ("lognoteclock-out" org-log-done clock-out push
)
3676 ("logrepeat" org-log-repeat t
)
3677 ("nologrepeat" org-log-repeat nil
)
3678 ("constcgs" constants-unit-system cgs
)
3679 ("constSI" constants-unit-system SI
))
3680 "Variable associated with STARTUP options for org-mode.
3681 Each element is a list of three items: The startup options as written
3682 in the #+STARTUP line, the corresponding variable, and the value to
3683 set this variable to if the option is found. An optional forth element PUSH
3684 means to push this value onto the list in the variable.")
3686 (defun org-set-regexps-and-options ()
3687 "Precompute regular expressions for current buffer."
3689 (org-set-local 'org-todo-kwd-alist nil
)
3690 (org-set-local 'org-todo-keywords-1 nil
)
3691 (org-set-local 'org-done-keywords nil
)
3692 (org-set-local 'org-todo-heads nil
)
3693 (org-set-local 'org-todo-sets nil
)
3694 (let ((re (org-make-options-regexp
3695 '("CATEGORY" "SEQ_TODO" "PRI_TODO" "TYP_TODO" "COLUMNS"
3696 "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
3697 "CONSTANTS" "PROPERTY")))
3699 kwds key value cat arch tags const links hw dws tail sep kws1 prio
3704 (goto-char (point-min))
3705 (while (re-search-forward re nil t
)
3706 (setq key
(match-string 1) value
(org-match-string-no-properties 2))
3708 ((equal key
"CATEGORY")
3709 (if (string-match "[ \t]+$" value
)
3710 (setq value
(replace-match "" t t value
)))
3711 (setq cat
(intern value
)))
3712 ((equal key
"SEQ_TODO")
3713 (push (cons 'sequence
(org-split-string value splitre
)) kwds
))
3714 ((equal key
"TYP_TODO")
3715 (push (cons 'type
(org-split-string value splitre
)) kwds
))
3717 (setq tags
(append tags
(org-split-string value splitre
))))
3718 ((equal key
"COLUMNS")
3719 (org-set-local 'org-columns-default-format value
))
3721 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value
)
3722 (push (cons (match-string 1 value
)
3723 (org-trim (match-string 2 value
)))
3725 ((equal key
"PRIORITIES")
3726 (setq prio
(org-split-string value
" +")))
3727 ((equal key
"PROPERTY")
3728 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
3729 (push (cons (match-string 1 value
) (match-string 2 value
))
3731 ((equal key
"CONSTANTS")
3732 (setq const
(append const
(org-split-string value splitre
))))
3733 ((equal key
"STARTUP")
3734 (let ((opts (org-split-string value splitre
))
3736 (while (setq l
(pop opts
))
3737 (when (setq l
(assoc l org-startup-options
))
3738 (setq var
(nth 1 l
) val
(nth 2 l
))
3740 (set (make-local-variable var
) val
)
3741 (if (not (listp (symbol-value var
)))
3742 (set (make-local-variable var
) nil
))
3743 (set (make-local-variable var
) (symbol-value var
))
3744 (add-to-list var val
))))))
3745 ((equal key
"ARCHIVE")
3746 (string-match " *$" value
)
3747 (setq arch
(replace-match "" t t value
))
3748 (remove-text-properties 0 (length arch
)
3749 '(face t fontified t
) arch
)))
3751 (and cat
(org-set-local 'org-category cat
))
3753 (if (< (length prio
) 3) (setq prio
'("A" "C" "B")))
3754 (setq prio
(mapcar 'string-to-char prio
))
3755 (org-set-local 'org-highest-priority
(nth 0 prio
))
3756 (org-set-local 'org-lowest-priority
(nth 1 prio
))
3757 (org-set-local 'org-default-priority
(nth 2 prio
)))
3758 (and props
(org-set-local 'org-local-properties
(nreverse props
)))
3759 (and arch
(org-set-local 'org-archive-location arch
))
3760 (and links
(setq org-link-abbrev-alist-local
(nreverse links
)))
3761 ;; Process the TODO keywords
3763 ;; Use the global values as if they had been given locally.
3764 (setq kwds
(default-value 'org-todo-keywords
))
3765 (if (stringp (car kwds
))
3766 (setq kwds
(list (cons org-todo-interpretation
3767 (default-value 'org-todo-keywords
)))))
3768 (setq kwds
(reverse kwds
)))
3769 (setq kwds
(nreverse kwds
))
3771 (while (setq kws
(pop kwds
))
3772 (setq inter
(pop kws
) sep
(member "|" kws
)
3773 kws1
(delete "|" (copy-sequence kws
))
3775 dws
(if sep
(cdr sep
) (last kws1
))
3776 tail
(list inter hw
(car dws
) (org-last dws
)))
3777 (add-to-list 'org-todo-heads hw
'append
)
3778 (push kws1 org-todo-sets
)
3779 (setq org-done-keywords
(append org-done-keywords dws nil
))
3780 (mapc (lambda (x) (push (cons x tail
) org-todo-kwd-alist
)) kws1
)
3781 (setq org-todo-keywords-1
(append org-todo-keywords-1 kws1 nil
)))
3782 (setq org-todo-sets
(nreverse org-todo-sets
)
3783 org-todo-kwd-alist
(nreverse org-todo-kwd-alist
)))
3784 ;; Process the constants
3787 (while (setq e
(pop const
))
3788 (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e
)
3789 (push (cons (match-string 1 e
) (match-string 2 e
)) cst
)))
3790 (setq org-table-formula-constants-local cst
)))
3792 ;; Process the tags.
3795 (while (setq e
(pop tags
))
3797 ((equal e
"{") (push '(:startgroup
) tgs
))
3798 ((equal e
"}") (push '(:endgroup
) tgs
))
3799 ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e
)
3800 (push (cons (match-string 1 e
)
3801 (string-to-char (match-string 2 e
)))
3803 (t (push (list e
) tgs
))))
3804 (org-set-local 'org-tag-alist nil
)
3805 (while (setq e
(pop tgs
))
3806 (or (and (stringp (car e
))
3807 (assoc (car e
) org-tag-alist
))
3808 (push e org-tag-alist
))))))
3810 ;; Compute the regular expressions and other local variables
3811 (if (not org-done-keywords
)
3812 (setq org-done-keywords
(list (org-last org-todo-keywords-1
))))
3813 (setq org-ds-keyword-length
(+ 2 (max (length org-deadline-string
)
3814 (length org-scheduled-string
)))
3816 (concat "^[ \t]*:\\("
3817 (mapconcat 'regexp-quote org-drawers
"\\|")
3819 org-not-done-keywords
3820 (org-delete-all org-done-keywords
(copy-sequence org-todo-keywords-1
))
3822 (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
3826 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
3828 org-todo-line-regexp
3829 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3830 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
3831 "\\)\\>\\)?[ \t]*\\(.*\\)")
3833 (concat "\n\\*+[ \t]+"
3834 "\\(?:" (mapconcat 'regexp-quote org-done-keywords
"\\|")
3836 org-todo-line-tags-regexp
3837 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3838 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
3840 "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
3841 org-looking-at-done-regexp
3843 (mapconcat 'regexp-quote org-done-keywords
"\\|") "\\)"
3845 org-deadline-regexp
(concat "\\<" org-deadline-string
)
3846 org-deadline-time-regexp
3847 (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>")
3848 org-deadline-line-regexp
3849 (concat "\\<\\(" org-deadline-string
"\\).*")
3850 org-scheduled-regexp
3851 (concat "\\<" org-scheduled-string
)
3852 org-scheduled-time-regexp
3853 (concat "\\<" org-scheduled-string
" *<\\([^>]+\\)>")
3854 org-closed-time-regexp
3855 (concat "\\<" org-closed-string
" *\\[\\([^]]+\\)\\]")
3856 org-keyword-time-regexp
3857 (concat "\\<\\(" org-scheduled-string
3858 "\\|" org-deadline-string
3859 "\\|" org-closed-string
3860 "\\|" org-archived-string
3861 "\\|" org-clock-string
"\\)"
3862 " *[[<]\\([^]>]+\\)[]>]")
3863 org-keyword-time-not-clock-regexp
3864 (concat "\\<\\(" org-scheduled-string
3865 "\\|" org-deadline-string
3866 "\\|" org-closed-string
3867 "\\|" org-archived-string
3869 " *[[<]\\([^]>]+\\)[]>]")
3870 org-maybe-keyword-time-regexp
3871 (concat "\\(\\<\\(" org-scheduled-string
3872 "\\|" org-deadline-string
3873 "\\|" org-closed-string
3874 "\\|" org-archived-string
3875 "\\|" org-clock-string
"\\)\\)?"
3876 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
3877 org-planning-or-clock-line-re
3878 (concat "\\(?:^[ \t]*\\(" org-scheduled-string
3879 "\\|" org-deadline-string
3880 "\\|" org-closed-string
"\\|" org-clock-string
3881 "\\|" org-archived-string
"\\)\\>\\)")
3884 (org-set-font-lock-defaults)))
3887 ;;; Some variables ujsed in various places
3889 (defvar org-window-configuration nil
3890 "Used in various places to store a window configuration.")
3891 (defvar org-finish-function nil
3892 "Function to be called when `C-c C-c' is used.
3893 This is for getting out of special buffers like remember.")
3895 ;;; Foreign variables, to inform the compiler
3898 (defvar outline-mode-menu-heading
)
3899 (defvar outline-mode-menu-show
)
3900 (defvar outline-mode-menu-hide
)
3901 (defvar zmacs-regions
) ; XEmacs regions
3903 (defvar mark-active
)
3905 ;; Packages that org-mode interacts with
3906 (defvar calc-embedded-close-formula
)
3907 (defvar calc-embedded-open-formula
)
3908 (defvar font-lock-unfontify-region-function
)
3909 (defvar org-goto-start-pos
)
3910 (defvar vm-message-pointer
)
3911 (defvar vm-folder-directory
)
3912 (defvar wl-summary-buffer-elmo-folder
)
3913 (defvar wl-summary-buffer-folder-name
)
3914 (defvar gnus-other-frame-object
)
3915 (defvar gnus-group-name
)
3916 (defvar gnus-article-current
)
3917 (defvar w3m-current-url
)
3918 (defvar w3m-current-title
)
3920 (defvar mh-current-folder
)
3921 (defvar mh-show-folder-buffer
)
3922 (defvar mh-index-folder
)
3923 (defvar mh-searcher
)
3924 (defvar calendar-mode-map
)
3925 (defvar Info-current-file
)
3926 (defvar Info-current-node
)
3927 (defvar texmathp-why
)
3928 (defvar remember-save-after-remembering
)
3929 (defvar remember-data-file
)
3930 (defvar annotation
) ; from remember.el, dynamically scoped in `remember-mode'
3931 (defvar initial
) ; from remember.el, dynamically scoped in `remember-mode'
3932 (defvar org-latex-regexps
)
3933 (defvar constants-unit-system
)
3935 (defvar original-date
) ; dynamically scoped in calendar.el does scope this
3937 ;; FIXME: Occasionally check by commenting these, to make sure
3938 ;; no other functions uses these, forgetting to let-bind them.
3943 (defvar description
)
3946 ;; Defined somewhere in this file, but used before definition.
3947 (defvar orgtbl-mode-menu
) ; defined when orgtbl mode get initialized
3948 (defvar org-agenda-undo-list
)
3949 (defvar org-agenda-pending-undo-list
)
3950 (defvar org-agenda-overriding-header
)
3951 (defvar orgtbl-mode
)
3952 (defvar org-html-entities
)
3953 (defvar org-struct-menu
)
3954 (defvar org-org-menu
)
3955 (defvar org-tbl-menu
)
3956 (defvar org-agenda-keymap
)
3957 (defvar org-category-table
)
3959 ;;;; Emacs/XEmacs compatibility
3961 ;; Overlay compatibility functions
3962 (defun org-make-overlay (beg end
&optional buffer
)
3963 (if (featurep 'xemacs
)
3964 (make-extent beg end buffer
)
3965 (make-overlay beg end buffer
)))
3966 (defun org-delete-overlay (ovl)
3967 (if (featurep 'xemacs
) (delete-extent ovl
) (delete-overlay ovl
)))
3968 (defun org-detach-overlay (ovl)
3969 (if (featurep 'xemacs
) (detach-extent ovl
) (delete-overlay ovl
)))
3970 (defun org-move-overlay (ovl beg end
&optional buffer
)
3971 (if (featurep 'xemacs
)
3972 (set-extent-endpoints ovl beg end
(or buffer
(current-buffer)))
3973 (move-overlay ovl beg end buffer
)))
3974 (defun org-overlay-put (ovl prop value
)
3975 (if (featurep 'xemacs
)
3976 (set-extent-property ovl prop value
)
3977 (overlay-put ovl prop value
)))
3978 (defun org-overlay-display (ovl text
&optional face evap
)
3979 "Make overlay OVL display TEXT with face FACE."
3980 (if (featurep 'xemacs
)
3981 (let ((gl (make-glyph text
)))
3982 (and face
(set-glyph-face gl face
))
3983 (set-extent-property ovl
'invisible t
)
3984 (set-extent-property ovl
'end-glyph gl
))
3985 (overlay-put ovl
'display text
)
3986 (if face
(overlay-put ovl
'face face
))
3987 (if evap
(overlay-put ovl
'evaporate t
))))
3988 (defun org-overlay-before-string (ovl text
&optional face evap
)
3989 "Make overlay OVL display TEXT with face FACE."
3990 (if (featurep 'xemacs
)
3991 (let ((gl (make-glyph text
)))
3992 (and face
(set-glyph-face gl face
))
3993 (set-extent-property ovl
'begin-glyph gl
))
3994 (if face
(org-add-props text nil
'face face
))
3995 (overlay-put ovl
'before-string text
)
3996 (if evap
(overlay-put ovl
'evaporate t
))))
3997 (defun org-overlay-get (ovl prop
)
3998 (if (featurep 'xemacs
)
3999 (extent-property ovl prop
)
4000 (overlay-get ovl prop
)))
4001 (defun org-overlays-at (pos)
4002 (if (featurep 'xemacs
) (extents-at pos
) (overlays-at pos
)))
4003 ;; FIXME: this is currently not used
4004 (defun org-overlays-in (&optional start end
)
4005 (if (featurep 'xemacs
)
4006 (extent-list nil start end
)
4007 (overlays-in start end
)))
4008 (defun org-overlay-start (o)
4009 (if (featurep 'xemacs
) (extent-start-position o
) (overlay-start o
)))
4010 (defun org-overlay-end (o)
4011 (if (featurep 'xemacs
) (extent-end-position o
) (overlay-end o
)))
4012 ;; FIXME: this is currently not used
4013 (defun org-find-overlays (prop &optional pos delete
)
4014 "Find all overlays specifying PROP at POS or point.
4015 If DELETE is non-nil, delete all those overlays."
4016 (let ((overlays (org-overlays-at (or pos
(point))))
4018 (while (setq ov
(pop overlays
))
4019 (if (org-overlay-get ov prop
)
4020 (if delete
(org-delete-overlay ov
) (push ov found
))))
4023 ;; Region compatibility
4025 (defun org-add-hook (hook function
&optional append local
)
4026 "Add-hook, compatible with both Emacsen."
4027 (if (and local
(featurep 'xemacs
))
4028 (add-local-hook hook function append
)
4029 (add-hook hook function append local
)))
4031 (defvar org-ignore-region nil
4032 "To temporarily disable the active region.")
4034 (defun org-region-active-p ()
4035 "Is `transient-mark-mode' on and the region active?
4036 Works on both Emacs and XEmacs."
4037 (if org-ignore-region
4039 (if (featurep 'xemacs
)
4040 (and zmacs-regions
(region-active-p))
4041 (and transient-mark-mode mark-active
))))
4043 ;; Invisibility compatibility
4045 (defun org-add-to-invisibility-spec (arg)
4046 "Add elements to `buffer-invisibility-spec'.
4047 See documentation for `buffer-invisibility-spec' for the kind of elements
4050 ((fboundp 'add-to-invisibility-spec
)
4051 (add-to-invisibility-spec arg
))
4052 ((or (null buffer-invisibility-spec
) (eq buffer-invisibility-spec t
))
4053 (setq buffer-invisibility-spec
(list arg
)))
4055 (setq buffer-invisibility-spec
4056 (cons arg buffer-invisibility-spec
)))))
4058 (defun org-remove-from-invisibility-spec (arg)
4059 "Remove elements from `buffer-invisibility-spec'."
4060 (if (fboundp 'remove-from-invisibility-spec
)
4061 (remove-from-invisibility-spec arg
)
4062 (if (consp buffer-invisibility-spec
)
4063 (setq buffer-invisibility-spec
4064 (delete arg buffer-invisibility-spec
)))))
4066 ;; FIXME: this is currently not used
4067 (defun org-in-invisibility-spec-p (arg)
4068 "Is ARG a member of `buffer-invisibility-spec'?"
4069 (if (consp buffer-invisibility-spec
)
4070 (member arg buffer-invisibility-spec
)
4073 ;;;; Define the Org-mode
4075 (if (and (not (keymapp outline-mode-map
)) (featurep 'allout
))
4076 (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."))
4079 ;; We use a before-change function to check if a table might need
4081 (defvar org-table-may-need-update t
4082 "Indicates that a table might need an update.
4083 This variable is set by `org-before-change-function'.
4084 `org-table-align' sets it back to nil.")
4085 (defvar org-mode-map
)
4086 (defvar org-mode-hook nil
)
4087 (defvar org-inhibit-startup nil
) ; Dynamically-scoped param.
4088 (defvar org-agenda-keep-modes nil
) ; Dynamically-scoped param.
4089 (defvar org-table-buffer-is-an nil
)
4093 (define-derived-mode org-mode outline-mode
"Org"
4094 "Outline-based notes management and organizer, alias
4095 \"Carsten's outline-mode for keeping track of everything.\"
4097 Org-mode develops organizational tasks around a NOTES file which
4098 contains information about projects as plain text. Org-mode is
4099 implemented on top of outline-mode, which is ideal to keep the content
4100 of large files well structured. It supports ToDo items, deadlines and
4101 time stamps, which magically appear in the diary listing of the Emacs
4102 calendar. Tables are easily created with a built-in table editor.
4103 Plain text URL-like links connect to websites, emails (VM), Usenet
4104 messages (Gnus), BBDB entries, and any files related to the project.
4105 For printing and sharing of notes, an Org-mode file (or a part of it)
4106 can be exported as a structured ASCII or HTML file.
4108 The following commands are available:
4112 ;; Get rid of Outline menus, they are not needed
4113 ;; Need to do this here because define-derived-mode sets up
4114 ;; the keymap so late. Still, it is a waste to call this each time
4115 ;; we switch another buffer into org-mode.
4116 (if (featurep 'xemacs
)
4117 (when (boundp 'outline-mode-menu-heading
)
4118 ;; Assume this is Greg's port, it used easymenu
4119 (easy-menu-remove outline-mode-menu-heading
)
4120 (easy-menu-remove outline-mode-menu-show
)
4121 (easy-menu-remove outline-mode-menu-hide
))
4122 (define-key org-mode-map
[menu-bar headings
] 'undefined
)
4123 (define-key org-mode-map
[menu-bar hide
] 'undefined
)
4124 (define-key org-mode-map
[menu-bar show
] 'undefined
))
4126 (easy-menu-add org-org-menu
)
4127 (easy-menu-add org-tbl-menu
)
4128 (org-install-agenda-files-menu)
4129 (if org-descriptive-links
(org-add-to-invisibility-spec '(org-link)))
4130 (org-add-to-invisibility-spec '(org-cwidth))
4131 (when (featurep 'xemacs
)
4132 (org-set-local 'line-move-ignore-invisible t
))
4133 (org-set-local 'outline-regexp
"\\*+ ")
4134 (setq outline-level
'org-outline-level
)
4135 (when (and org-ellipsis
(stringp org-ellipsis
)
4136 (fboundp 'set-display-table-slot
) (boundp 'buffer-display-table
))
4137 (unless org-display-table
4138 (setq org-display-table
(make-display-table)))
4139 (set-display-table-slot org-display-table
4140 4 (string-to-vector org-ellipsis
))
4141 (setq buffer-display-table org-display-table
))
4142 (org-set-regexps-and-options)
4144 (org-set-local 'calc-embedded-open-mode
"# ")
4145 (modify-syntax-entry ?
# "<")
4146 (modify-syntax-entry ?
@ "w")
4147 (if org-startup-truncated
(setq truncate-lines t
))
4148 (org-set-local 'font-lock-unfontify-region-function
4149 'org-unfontify-region
)
4150 ;; Activate before-change-function
4151 (org-set-local 'org-table-may-need-update t
)
4152 (org-add-hook 'before-change-functions
'org-before-change-function nil
4154 ;; Check for running clock before killing a buffer
4155 (org-add-hook 'kill-buffer-hook
'org-check-running-clock nil
'local
)
4156 ;; Paragraphs and auto-filling
4157 (org-set-autofill-regexps)
4158 (setq indent-line-function
'org-indent-line-function
)
4159 (org-update-radio-target-regexp)
4161 ;; Comment characters
4162 ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
4163 (org-set-local 'comment-padding
" ")
4165 ;; Make isearch reveal context
4166 (if (or (featurep 'xemacs
)
4167 (not (boundp 'outline-isearch-open-invisible-function
)))
4168 ;; Emacs 21 and XEmacs make use of the hook
4169 (org-add-hook 'isearch-mode-end-hook
'org-isearch-end
'append
'local
)
4170 ;; Emacs 22 deals with this through a special variable
4171 (org-set-local 'outline-isearch-open-invisible-function
4172 (lambda (&rest ignore
) (org-show-context 'isearch
))))
4174 ;; If empty file that did not turn on org-mode automatically, make it to.
4175 (if (and org-insert-mode-line-in-empty-file
4177 (= (point-min) (point-max)))
4178 (insert "# -*- mode: org -*-\n\n"))
4180 (unless org-inhibit-startup
4181 (when org-startup-align-all-tables
4182 (let ((bmp (buffer-modified-p)))
4183 (org-table-map-tables 'org-table-align
)
4184 (set-buffer-modified-p bmp
)))
4185 (org-cycle-hide-drawers 'all
)
4187 ((eq org-startup-folded t
)
4189 ((eq org-startup-folded
'content
)
4190 (let ((this-command 'org-cycle
) (last-command 'org-cycle
))
4191 (org-cycle '(4)) (org-cycle '(4)))))))
4193 (put 'org-mode
'flyspell-mode-predicate
'org-mode-flyspell-verify
)
4195 (defsubst org-call-with-arg
(command arg
)
4196 "Call COMMAND interactively, but pretend prefix are was ARG."
4197 (let ((current-prefix-arg arg
)) (call-interactively command
)))
4199 (defsubst org-current-line
(&optional pos
)
4201 (and pos
(goto-char pos
))
4202 ;; works also in narrowed buffer, because we start at 1, not point-min
4203 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
4205 (defun org-current-time ()
4206 "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
4207 (if (> org-time-stamp-rounding-minutes
0)
4208 (let ((r org-time-stamp-rounding-minutes
)
4209 (time (decode-time)))
4211 (append (list 0 (* r
(floor (+ .5 (/ (float (nth 1 time
)) r
)))))
4215 (defun org-add-props (string plist
&rest props
)
4216 "Add text properties to entire string, from beginning to end.
4217 PLIST may be a list of properties, PROPS are individual properties and values
4218 that will be added to PLIST. Returns the string that was modified."
4219 (add-text-properties
4220 0 (length string
) (if props
(append plist props
) plist
) string
)
4222 (put 'org-add-props
'lisp-indent-function
2)
4225 ;;;; Font-Lock stuff, including the activators
4227 (defvar org-mouse-map
(make-sparse-keymap))
4228 (org-defkey org-mouse-map
4229 (if (featurep 'xemacs
) [button2] [mouse-2]) 'org-open-at-mouse)
4230 (org-defkey org-mouse-map
4231 (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
4232 (when org-mouse-1-follows-link
4233 (org-defkey org-mouse-map [follow-link] 'mouse-face))
4234 (when org-tab-follows-link
4235 (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
4236 (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
4237 (when org-return-follows-link
4238 (org-defkey org-mouse-map [(return)] 'org-open-at-point)
4239 (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
4241 (require 'font-lock)
4243 (defconst org-non-link-chars "]\t\n\r<>")
4244 (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news" "bbdb" "vm"
4245 "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
4246 (defvar org-link-re-with-space nil
4247 "Matches a link with spaces, optional angular brackets around it.")
4248 (defvar org-link-re-with-space2 nil
4249 "Matches a link with spaces, optional angular brackets around it.")
4250 (defvar org-angle-link-re nil
4251 "Matches link with angular brackets, spaces are allowed.")
4252 (defvar org-plain-link-re nil
4253 "Matches plain link, without spaces.")
4254 (defvar org-bracket-link-regexp nil
4255 "Matches a link in double brackets.")
4256 (defvar org-bracket-link-analytic-regexp nil
4257 "Regular expression used to analyze links.
4258 Here is what the match groups contain after a match:
4264 (defvar org-any-link-re nil
4265 "Regular expression matching any link.")
4267 (defun org-make-link-regexps ()
4268 "Update the link regular expressions.
4269 This should be called after the variable `org-link-types' has changed."
4270 (setq org-link-re-with-space
4272 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
4273 "\\([^" org-non-link-chars " ]"
4274 "[^" org-non-link-chars "]*"
4275 "[^" org-non-link-chars " ]\\)>?")
4276 org-link-re-with-space2
4278 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
4279 "\\([^" org-non-link-chars " ]"
4281 "[^" org-non-link-chars " ]\\)>?")
4284 "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
4285 "\\([^" org-non-link-chars " ]"
4286 "[^" org-non-link-chars "]*"
4290 "\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
4291 "\\([^]\t\n\r<>,;() ]+\\)")
4292 org-bracket-link-regexp
4293 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
4294 org-bracket-link-analytic-regexp
4297 "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
4300 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
4303 (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
4304 org-angle-link-re "\\)\\|\\("
4305 org-plain-link-re "\\)")))
4307 (org-make-link-regexps)
4309 (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
4310 "Regular expression for fast time stamp matching.")
4311 (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
4312 "Regular expression for fast time stamp matching.")
4313 (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
4314 "Regular expression matching time strings for analysis.
4315 This one does not require the space after the date.")
4316 (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) \\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
4317 "Regular expression matching time strings for analysis.")
4318 (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,11\\}>")
4319 "Regular expression matching time stamps, with groups.")
4320 (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,11\\}[]>]")
4321 "Regular expression matching time stamps (also [..]), with groups.")
4322 (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
4323 "Regular expression matching a time stamp range.")
4324 (defconst org-tr-regexp-both
4325 (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
4326 "Regular expression matching a time stamp range.")
4327 (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
4328 org-ts-regexp "\\)?")
4329 "Regular expression matching a time stamp or time stamp range.")
4330 (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
4331 org-ts-regexp-both "\\)?")
4332 "Regular expression matching a time stamp or time stamp range.
4333 The time stamps may be either active or inactive.")
4335 (defvar org-emph-face nil)
4337 (defun org-do-emphasis-faces (limit)
4338 "Run through the buffer and add overlays to links."
4340 (while (and (not rtn) (re-search-forward org-emph-re limit t))
4341 (if (not (= (char-after (match-beginning 3))
4342 (char-after (match-beginning 4))))
4345 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
4347 (nth 1 (assoc (match-string 3)
4348 org-emphasis-alist)))
4349 (add-text-properties (match-beginning 2) (match-end 2)
4350 '(font-lock-multiline t))
4351 (backward-char 1))))
4354 (defun org-emphasize (&optional char)
4355 "Insert or change an emphasis, i.e. a font like bold or italic.
4356 If there is an active region, change that region to a new emphasis.
4357 If there is no region, just insert the marker characters and position
4358 the cursor between them.
4359 CHAR should be either the marker character, or the first character of the
4360 HTML tag associated with that emphasis. If CHAR is a space, the means
4361 to remove the emphasis of the selected region.
4362 If char is not given (for example in an interactive call) it
4363 will be prompted for."
4365 (let ((eal org-emphasis-alist) e det
4366 (erc org-emphasis-regexp-components)
4368 (string "") beg end move tag c s)
4369 (if (org-region-active-p)
4370 (setq beg (region-beginning) end (region-end)
4371 string (buffer-substring beg end))
4374 (while (setq e (pop eal))
4375 (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
4377 (push (cons c (string-to-char (car e))) det)
4378 (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
4379 (substring tag 1)))))
4381 (message "%s" (concat "Emphasis marker or tag:" prompt))
4382 (setq char (read-char-exclusive)))
4383 (setq char (or (cdr (assoc char det)) char))
4384 (if (equal char ?\ )
4385 (setq s "" move nil)
4386 (unless (assoc (char-to-string char) org-emphasis-alist)
4387 (error "No such emphasis marker: \"%c\"" char))
4388 (setq s (char-to-string char)))
4389 (while (and (> (length string) 1)
4390 (equal (substring string 0 1) (substring string -1))
4391 (assoc (substring string 0 1) org-emphasis-alist))
4392 (setq string (substring string 1 -1)))
4393 (setq string (concat s string s))
4394 (if beg (delete-region beg end))
4396 (string-match (concat "[" (nth 0 erc) "\n]")
4397 (char-to-string (char-before (point)))))
4399 (unless (string-match (concat "[" (nth 1 erc) "\n]")
4400 (char-to-string (char-after (point))))
4401 (insert " ") (backward-char 1))
4403 (and move (backward-char 1))))
4405 (defun org-activate-plain-links (limit)
4406 "Run through the buffer and add overlays to links."
4409 (while (re-search-forward org-plain-link-re limit t)
4410 (setq f (get-text-property (match-beginning 0) 'face))
4411 (if (or (eq f 'org-tag)
4412 (and (listp f) (memq 'org-tag f)))
4414 (add-text-properties (match-beginning 0) (match-end 0)
4415 (list 'mouse-face 'highlight
4417 'keymap org-mouse-map
4419 (throw 'exit t))))))
4421 (defun org-activate-angle-links (limit)
4422 "Run through the buffer and add overlays to links."
4423 (if (re-search-forward org-angle-link-re limit t)
4425 (add-text-properties (match-beginning 0) (match-end 0)
4426 (list 'mouse-face 'highlight
4428 'keymap org-mouse-map
4432 (defmacro org-maybe-intangible (props)
4433 "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
4434 In emacs 21, invisible text is not avoided by the command loop, so the
4435 intangible property is needed to make sure point skips this text.
4436 In Emacs 22, this is not necessary. The intangible text property has
4437 led to problems with flyspell. These problems are fixed in flyspell.el,
4438 but we still avoid setting the property in Emacs 22 and later.
4439 We use a macro so that the test can happen at compilation time."
4440 (if (< emacs-major-version 22)
4441 `(append '(intangible t) ,props)
4444 (defun org-activate-bracket-links (limit)
4445 "Run through the buffer and add overlays to bracketed links."
4446 (if (re-search-forward org-bracket-link-regexp limit t)
4447 (let* ((help (concat "LINK: "
4448 (org-match-string-no-properties 1)))
4449 ;; FIXME: above we should remove the escapes.
4450 ;; but that requires another match, protecting match data,
4451 ;; a lot of overhead for font-lock.
4452 (ip (org-maybe-intangible
4453 (list 'invisible 'org-link 'rear-nonsticky t
4454 'keymap org-mouse-map 'mouse-face 'highlight
4456 (vp (list 'rear-nonsticky t
4457 'keymap org-mouse-map 'mouse-face 'highlight
4459 ;; We need to remove the invisible property here. Table narrowing
4460 ;; may have made some of this invisible.
4461 (remove-text-properties (match-beginning 0) (match-end 0)
4465 (add-text-properties (match-beginning 0) (match-beginning 3) ip)
4466 (add-text-properties (match-beginning 3) (match-end 3) vp)
4467 (add-text-properties (match-end 3) (match-end 0) ip))
4468 (add-text-properties (match-beginning 0) (match-beginning 1) ip)
4469 (add-text-properties (match-beginning 1) (match-end 1) vp)
4470 (add-text-properties (match-end 1) (match-end 0) ip))
4473 (defun org-activate-dates (limit)
4474 "Run through the buffer and add overlays to dates."
4475 (if (re-search-forward org-tsr-regexp-both limit t)
4477 (add-text-properties (match-beginning 0) (match-end 0)
4478 (list 'mouse-face 'highlight
4480 'keymap org-mouse-map))
4481 (when org-display-custom-times
4483 (org-display-custom-time (match-beginning 3) (match-end 3)))
4484 (org-display-custom-time (match-beginning 1) (match-end 1)))
4487 (defvar org-target-link-regexp nil
4488 "Regular expression matching radio targets in plain text.")
4489 (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
4490 "Regular expression matching a link target.")
4491 (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
4492 "Regular expression matching a radio target.")
4493 (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
4494 "Regular expression matching any target.")
4496 (defun org-activate-target-links (limit)
4497 "Run through the buffer and add overlays to target matches."
4498 (when org-target-link-regexp
4499 (let ((case-fold-search t))
4500 (if (re-search-forward org-target-link-regexp limit t)
4502 (add-text-properties (match-beginning 0) (match-end 0)
4503 (list 'mouse-face 'highlight
4505 'keymap org-mouse-map
4506 'help-echo "Radio target link"
4507 'org-linked-text t))
4510 (defun org-update-radio-target-regexp ()
4511 "Find all radio targets in this file and update the regular expression."
4513 (when (memq 'radio org-activate-links)
4514 (setq org-target-link-regexp
4515 (org-make-target-link-regexp (org-all-targets 'radio)))
4516 (org-restart-font-lock)))
4518 (defun org-hide-wide-columns (limit)
4520 (setq s (text-property-any (point) (or limit (point-max))
4523 (setq e (next-single-property-change s 'org-cwidth))
4524 (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
4528 (defun org-restart-font-lock ()
4529 "Restart font-lock-mode, to force refontification."
4530 (when (and (boundp 'font-lock-mode) font-lock-mode)
4531 ;; FIXME: Could font-lock-fontify-buffer be enough???
4533 (font-lock-mode 1)))
4535 (defun org-all-targets (&optional radio)
4536 "Return a list of all targets in this file.
4537 With optional argument RADIO, only find radio targets."
4538 (let ((re (if radio org-radio-target-regexp org-target-regexp))
4541 (goto-char (point-min))
4542 (while (re-search-forward re nil t)
4543 (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
4546 (defun org-make-target-link-regexp (targets)
4547 "Make regular expression matching all strings in TARGETS.
4548 The regular expression finds the targets also if there is a line break
4555 (while (string-match " +" x)
4556 (setq x (replace-match "\\s-+" t t x)))
4562 (defun org-activate-tags (limit)
4563 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
4565 (add-text-properties (match-beginning 1) (match-end 1)
4566 (list 'mouse-face 'highlight
4568 'keymap org-mouse-map))
4571 (defun org-outline-level ()
4573 (looking-at outline-regexp)
4574 (if (match-beginning 1)
4575 (+ (org-get-string-indentation (match-string 1)) 1000)
4576 (1- (- (match-end 0) (match-beginning 0))))))
4578 (defvar org-font-lock-keywords nil)
4580 (defconst org-property-re "^[ \t]*\\(:\\([a-zA-Z_0-9]+\\):\\)[ \t]*\\(\\S-.*\\)"
4581 "Regular expression matching a property line.")
4583 (defun org-set-font-lock-defaults ()
4584 (let* ((em org-fontify-emphasized-text)
4585 (lk org-activate-links)
4586 (org-font-lock-extra-keywords
4589 '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
4590 (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
4591 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
4594 (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
4595 (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
4596 (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
4597 (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
4598 (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
4599 (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
4600 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
4601 '(org-hide-wide-columns (0 nil append))
4603 (list (concat "^\\*+[ \t]+" org-not-done-regexp)
4606 (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
4608 (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
4609 (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
4610 (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
4611 (list (concat "\\<" org-archived-string) '(0 'org-special-keyword t))
4612 (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
4615 (if (featurep 'xemacs)
4616 '(org-do-emphasis-faces (0 nil append))
4617 '(org-do-emphasis-faces)))
4618 ;; Checkboxes, similar to Frank Ruell's org-checklet.el
4619 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
4621 (if org-provide-checkbox-statistics
4622 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
4623 (0 (org-get-checkbox-statistics-face) t)))
4625 (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
4626 "\\|" org-quote-string "\\)\\>")
4627 '(1 'org-special-keyword t))
4628 '("^#.*" (0 'font-lock-comment-face t))
4630 (if org-fontify-done-headline
4631 (list (concat "^[*]+ +\\<\\("
4632 (mapconcat 'regexp-quote org-done-keywords "\\|")
4634 '(1 'org-done t) '(2 'org-headline-done t))
4635 (list (concat "^[*]+ +\\<\\("
4636 (mapconcat 'regexp-quote org-done-keywords "\\|")
4640 '("^[ \t]*\\(:.*\\)" (1 'org-code t))
4642 '("| *\\(:?=[^|\n]*\\)" (1 'org-formula t))
4643 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
4644 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
4646 (list org-drawer-regexp '(0 'org-special-keyword t))
4647 (list "^[ \t]*:END:" '(0 'org-special-keyword t))
4649 (list org-property-re
4650 '(1 'org-special-keyword t)
4651 '(3 'org-property-value t))
4652 (if org-format-transports-properties-p
4653 '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
4654 '("^\\*+ \\(.*:ARCHIVE:.*\\)" (1 'org-archived prepend))
4656 (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
4657 ;; Now set the full font-lock-keywords
4658 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
4659 (org-set-local 'font-lock-defaults
4660 '(org-font-lock-keywords t nil nil backward-paragraph))
4661 (kill-local-variable 'font-lock-keywords) nil))
4666 (defun org-get-level-face (n)
4667 "Get the right face for match N in font-lock matching of healdines."
4668 (setq org-l (- (match-end 2) (match-beginning 1) 1))
4669 (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
4670 (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
4672 ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
4674 (t (if org-level-color-stars-only nil org-f))))
4676 (defun org-unfontify-region (beg end &optional maybe_loudly)
4677 "Remove fontification and activation overlays from links."
4678 (font-lock-default-unfontify-region beg end)
4679 (let* ((buffer-undo-list t)
4680 (inhibit-read-only t) (inhibit-point-motion-hooks t)
4681 (inhibit-modification-hooks t)
4682 deactivate-mark buffer-file-name buffer-file-truename)
4683 (remove-text-properties beg end
4684 '(mouse-face t keymap t org-linked-text t
4686 invisible t intangible t))))
4688 ;;;; Visibility cycling, including org-goto and indirect buffer
4692 (defvar org-cycle-global-status nil)
4693 (make-variable-buffer-local 'org-cycle-global-status)
4694 (defvar org-cycle-subtree-status nil)
4695 (make-variable-buffer-local 'org-cycle-subtree-status)
4698 (defun org-cycle (&optional arg)
4699 "Visibility cycling for Org-mode.
4701 - When this function is called with a prefix argument, rotate the entire
4702 buffer through 3 states (global cycling)
4703 1. OVERVIEW: Show only top-level headlines.
4704 2. CONTENTS: Show all headlines of all levels, but no body text.
4705 3. SHOW ALL: Show everything.
4707 - When point is at the beginning of a headline, rotate the subtree started
4708 by this line through 3 different states (local cycling)
4709 1. FOLDED: Only the main headline is shown.
4710 2. CHILDREN: The main headline and the direct children are shown.
4711 From this state, you can move to one of the children
4712 and zoom in further.
4713 3. SUBTREE: Show the entire subtree, including body text.
4715 - When there is a numeric prefix, go up to a heading with level ARG, do
4716 a `show-subtree' and return to the previous cursor position. If ARG
4717 is negative, go up that many levels.
4719 - When point is not at the beginning of a headline, execute
4720 `indent-relative', like TAB normally does. See the option
4721 `org-cycle-emulate-tab' for details.
4723 - Special case: if point is at the beginning of the buffer and there is
4724 no headline in line 1, this function will act as if called with prefix arg."
4726 (let* ((outline-regexp
4727 (if (and (org-mode-p) org-cycle-include-plain-lists)
4728 "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
4730 (bob-special (and org-cycle-global-at-bob (bobp)
4731 (not (looking-at outline-regexp))))
4734 (delq 'org-optimize-window-after-visibility-change
4735 (copy-sequence org-cycle-hook))
4739 (if (or bob-special (equal arg '(4)))
4740 ;; special case: use global cycling
4745 ((org-at-table-p 'any)
4746 ;; Enter the table or move to the next field in the table
4747 (or (org-table-recognize-table.el)
4749 (if arg (org-table-edit-field t)
4750 (org-table-justify-field-maybe)
4751 (call-interactively 'org-table-next-field)))))
4753 ((eq arg t) ;; Global cycling
4756 ((and (eq last-command this-command)
4757 (eq org-cycle-global-status 'overview))
4758 ;; We just created the overview - now do table of contents
4759 ;; This can be slow in very large buffers, so indicate action
4760 (message "CONTENTS...")
4762 (message "CONTENTS...done")
4763 (setq org-cycle-global-status 'contents)
4764 (run-hook-with-args 'org-cycle-hook 'contents))
4766 ((and (eq last-command this-command)
4767 (eq org-cycle-global-status 'contents))
4768 ;; We just showed the table of contents - now show everything
4770 (message "SHOW ALL")
4771 (setq org-cycle-global-status 'all)
4772 (run-hook-with-args 'org-cycle-hook 'all))
4775 ;; Default action: go to overview
4777 (message "OVERVIEW")
4778 (setq org-cycle-global-status 'overview)
4779 (run-hook-with-args 'org-cycle-hook 'overview))))
4781 ((and org-drawers org-drawer-regexp
4783 (beginning-of-line 1)
4784 (looking-at org-drawer-regexp)))
4785 ;; Toggle block visibility
4787 (not (get-char-property (match-end 0) 'invisible))))
4790 ;; Show-subtree, ARG levels up from here.
4792 (org-back-to-heading)
4793 (outline-up-heading (if (< arg 0) (- arg)
4794 (- (funcall outline-level) arg)))
4795 (org-show-subtree)))
4797 ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
4798 (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
4799 ;; At a heading: rotate between three different views
4800 (org-back-to-heading)
4801 (let ((goal-column 0) eoh eol eos)
4802 ;; First, some boundaries
4804 (org-back-to-heading)
4806 (beginning-of-line 2)
4807 (while (and (not (eobp)) ;; this is like `next-line'
4808 (get-char-property (1- (point)) 'invisible))
4809 (beginning-of-line 2)) (setq eol (point)))
4810 (outline-end-of-heading) (setq eoh (point))
4811 (org-end-of-subtree t)
4813 (skip-chars-forward " \t\n")
4814 (beginning-of-line 1) ; in case this is an item
4816 (setq eos (1- (point))))
4817 ;; Find out what to do next and set `this-command'
4820 ;; Nothing is hidden behind this heading
4821 (message "EMPTY ENTRY")
4822 (setq org-cycle-subtree-status nil)
4825 (outline-next-heading)
4826 (if (org-invisible-p) (org-flag-heading nil))))
4828 (not (string-match "\\S-" (buffer-substring eol eos))))
4829 ;; Entire subtree is hidden in one line: open it
4832 (message "CHILDREN")
4835 (outline-next-heading)
4836 (if (org-invisible-p) (org-flag-heading nil)))
4837 (setq org-cycle-subtree-status 'children)
4838 (run-hook-with-args 'org-cycle-hook 'children))
4839 ((and (eq last-command this-command)
4840 (eq org-cycle-subtree-status 'children))
4841 ;; We just showed the children, now show everything.
4844 (setq org-cycle-subtree-status 'subtree)
4845 (run-hook-with-args 'org-cycle-hook 'subtree))
4847 ;; Default action: hide the subtree.
4850 (setq org-cycle-subtree-status 'folded)
4851 (run-hook-with-args 'org-cycle-hook 'folded)))))
4854 (buffer-read-only (org-back-to-heading))
4856 ((org-try-cdlatex-tab))
4858 ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
4860 (not (looking-at outline-regexp))))
4861 (call-interactively (global-key-binding "\t")))
4863 ((if (and (memq org-cycle-emulate-tab '(white whitestart))
4864 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
4865 (or (and (eq org-cycle-emulate-tab 'white)
4866 (= (match-end 0) (point-at-eol)))
4867 (and (eq org-cycle-emulate-tab 'whitestart)
4868 (>= (match-end 0) pos))))
4870 (eq org-cycle-emulate-tab t))
4871 (if (and (looking-at "[ \n\r\t]")
4872 (string-match "^[ \t]*$" (buffer-substring
4873 (point-at-bol) (point))))
4875 (beginning-of-line 1)
4876 (and (looking-at "[ \t]+") (replace-match ""))))
4877 (call-interactively (global-key-binding "\t")))
4880 (org-back-to-heading)
4884 (defun org-global-cycle (&optional arg)
4885 "Cycle the global visibility. For details see `org-cycle'."
4887 (let ((org-cycle-include-plain-lists
4888 (if (org-mode-p) org-cycle-include-plain-lists nil)))
4892 (hide-sublevels arg)
4893 (setq org-cycle-global-status 'contents))
4896 (defun org-overview ()
4897 "Switch to overview mode, shoing only top-level headlines.
4898 Really, this shows all headlines with level equal or greater than the level
4899 of the first headline in the buffer. This is important, because if the
4900 first headline is not level one, then (hide-sublevels 1) gives confusing
4903 (let ((level (save-excursion
4904 (goto-char (point-min))
4905 (if (re-search-forward (concat "^" outline-regexp) nil t)
4907 (goto-char (match-beginning 0))
4908 (funcall outline-level))))))
4909 (and level (hide-sublevels level))))
4911 (defun org-content (&optional arg)
4912 "Show all headlines in the buffer, like a table of contents.
4913 With numerical argument N, show content up to level N."
4916 ;; Visit all headings and show their offspring
4917 (and (integerp arg) (org-overview))
4918 (goto-char (point-max))
4920 (while (and (progn (condition-case nil
4921 (outline-previous-visible-heading 1)
4922 (error (goto-char (point-min))))
4924 (looking-at outline-regexp))
4926 (show-children (1- arg))
4928 (if (bobp) (throw 'exit nil))))))
4931 (defun org-optimize-window-after-visibility-change (state)
4932 "Adjust the window after a change in outline visibility.
4933 This function is the default value of the hook `org-cycle-hook'."
4934 (when (get-buffer-window (current-buffer))
4936 ; ((eq state 'overview) (org-first-headline-recenter 1))
4937 ; ((eq state 'overview) (org-beginning-of-line))
4938 ((eq state 'content) nil)
4939 ((eq state 'all) nil)
4940 ((eq state 'folded) nil)
4941 ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
4942 ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
4945 (defun org-cycle-show-empty-lines (state)
4946 "Show empty lines above all visible headlines.
4947 The region to be covered depends on STATE when called through
4948 `org-cycle-hook'. Lisp program can use t for STATE to get the
4949 entire buffer covered. Note that an empty line is only shown if there
4950 are at least `org-cycle-separator-lines' empty lines before the headeline."
4951 (when (> org-cycle-separator-lines 0)
4953 (let* ((n org-cycle-separator-lines)
4955 ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
4956 ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
4957 (t (let ((ns (number-to-string (- n 2))))
4958 (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
4959 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
4962 ((memq state '(overview contents t))
4963 (setq beg (point-min) end (point-max)))
4964 ((memq state '(children folded))
4965 (setq beg (point) end (progn (org-end-of-subtree t t)
4966 (beginning-of-line 2)
4970 (while (re-search-forward re end t)
4971 (if (not (get-char-property (match-end 1) 'invisible))
4972 (outline-flag-region
4973 (match-beginning 1) (match-end 1) nil)))))))
4974 ;; Never hide empty lines at the end of the file.
4976 (goto-char (point-max))
4977 (outline-previous-heading)
4978 (outline-end-of-heading)
4979 (if (and (looking-at "[ \t\n]+")
4980 (= (match-end 0) (point-max)))
4981 (outline-flag-region (point) (match-end 0) nil))))
4983 (defun org-subtree-end-visible-p ()
4984 "Is the end of the current subtree visible?"
4985 (pos-visible-in-window-p
4986 (save-excursion (org-end-of-subtree t) (point))))
4988 (defun org-first-headline-recenter (&optional N)
4989 "Move cursor to the first headline and recenter the headline.
4990 Optional argument N means, put the headline into the Nth line of the window."
4991 (goto-char (point-min))
4992 (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
4994 (recenter (prefix-numeric-value N))))
4998 (defvar org-goto-window-configuration nil)
4999 (defvar org-goto-marker nil)
5000 (defvar org-goto-map
5001 (let ((map (make-sparse-keymap)))
5002 (let ((cmds '(isearch-forward isearch-backward)) cmd)
5003 (while (setq cmd (pop cmds))
5004 (substitute-key-definition cmd cmd map global-map)))
5005 (org-defkey map "\C-m" 'org-goto-ret)
5006 (org-defkey map [(left)] 'org-goto-left)
5007 (org-defkey map [(right)] 'org-goto-right)
5008 (org-defkey map [(?q)] 'org-goto-quit)
5009 (org-defkey map [(control ?g)] 'org-goto-quit)
5010 (org-defkey map "\C-i" 'org-cycle)
5011 (org-defkey map [(tab)] 'org-cycle)
5012 (org-defkey map [(down)] 'outline-next-visible-heading)
5013 (org-defkey map [(up)] 'outline-previous-visible-heading)
5014 (org-defkey map "n" 'outline-next-visible-heading)
5015 (org-defkey map "p" 'outline-previous-visible-heading)
5016 (org-defkey map "f" 'outline-forward-same-level)
5017 (org-defkey map "b" 'outline-backward-same-level)
5018 (org-defkey map "u" 'outline-up-heading)
5019 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
5020 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
5021 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
5022 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
5023 (org-defkey map "\C-c\C-u" 'outline-up-heading)
5024 ;; FIXME: Could we use suppress-keymap?
5025 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
5026 (while l (org-defkey map (int-to-string (pop l)) 'digit-argument)))
5029 (defconst org-goto-help
5030 "Select a location to jump to, press RET
5031 \[Up]/[Down]=next/prev headline TAB=cycle visibility RET=select [Q]uit")
5034 "Go to a different location of the document, keeping current visibility.
5036 When you want to go to a different location in a document, the fastest way
5037 is often to fold the entire buffer and then dive into the tree. This
5038 method has the disadvantage, that the previous location will be folded,
5039 which may not be what you want.
5041 This command works around this by showing a copy of the current buffer in
5042 overview mode. You can dive into the tree in that copy, to find the
5043 location you want to reach. When pressing RET, the command returns to the
5044 original buffer in which the visibility is still unchanged. It then jumps
5045 to the new location, making it and the headline hierarchy above it visible."
5047 (let* ((org-goto-start-pos (point))
5049 (org-get-location (current-buffer) org-goto-help)))
5052 (org-mark-ring-push org-goto-start-pos)
5053 (goto-char selected-point)
5054 (if (or (org-invisible-p) (org-invisible-p2))
5055 (org-show-context 'org-goto)))
5058 (defvar org-selected-point nil) ; dynamically scoped parameter
5060 (defun org-get-location (buf help)
5061 "Let the user select a location in the Org-mode buffer BUF.
5062 This function uses a recursive edit. It returns the selected position
5064 (let (org-selected-point)
5066 (save-window-excursion
5067 (delete-other-windows)
5068 (switch-to-buffer (get-buffer-create "*org-goto*"))
5069 (with-output-to-temp-buffer "*Help*"
5071 (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
5072 (setq buffer-read-only nil)
5074 (insert-buffer-substring buf)
5075 (let ((org-startup-truncated t)
5076 (org-startup-folded t)
5077 (org-startup-align-all-tables nil))
5079 (setq buffer-read-only t)
5080 (if (and (boundp 'org-goto-start-pos)
5081 (integer-or-marker-p org-goto-start-pos))
5082 (let ((org-show-hierarchy-above t)
5083 (org-show-siblings t)
5084 (org-show-following-heading t))
5085 (goto-char org-goto-start-pos)
5086 (and (org-invisible-p) (org-show-context)))
5087 (goto-char (point-min)))
5088 (org-beginning-of-line)
5089 (message "Select location and press RET")
5090 ;; now we make sure that during selection, ony very few keys work
5091 ;; and that it is impossible to switch to another window.
5092 (let ((gm (current-global-map))
5093 (overriding-local-map org-goto-map))
5096 (use-global-map org-goto-map)
5098 (use-global-map gm)))))
5099 (kill-buffer "*org-goto*")
5100 org-selected-point))
5102 (defun org-goto-ret (&optional arg)
5103 "Finish `org-goto' by going to the new location."
5105 (setq org-selected-point (point)
5106 current-prefix-arg arg)
5109 (defun org-goto-left ()
5110 "Finish `org-goto' by going to the new location."
5112 (if (org-on-heading-p)
5114 (beginning-of-line 1)
5115 (setq org-selected-point (point)
5116 current-prefix-arg (- (match-end 0) (match-beginning 0)))
5118 (error "Not on a heading")))
5120 (defun org-goto-right ()
5121 "Finish `org-goto' by going to the new location."
5123 (if (org-on-heading-p)
5125 (outline-end-of-subtree)
5126 (or (eobp) (forward-char 1))
5127 (setq org-selected-point (point)
5128 current-prefix-arg (- (match-end 0) (match-beginning 0)))
5130 (error "Not on a heading")))
5132 (defun org-goto-quit ()
5133 "Finish `org-goto' without cursor motion."
5135 (setq org-selected-point nil)
5138 ;;; Indirect buffer display of subtrees
5140 (defvar org-indirect-dedicated-frame nil
5141 "This is the frame being used for indirect tree display.")
5142 (defvar org-last-indirect-buffer nil)
5144 (defun org-tree-to-indirect-buffer (&optional arg)
5145 "Create indirect buffer and narrow it to current subtree.
5146 With numerical prefix ARG, go up to this level and then take that tree.
5147 If ARG is negative, go up that many levels.
5148 Normally this command removes the indirect buffer previously made
5149 with this command. However, when called with a C-u prefix, the last buffer
5150 is kept so that you can work with several indirect buffers at the same time.
5151 If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
5152 requests that a new frame be made for the new buffer, so that the dedicated
5153 frame is not changed."
5155 (let ((cbuf (current-buffer))
5156 (cwin (selected-window))
5158 beg end level heading ibuf)
5160 (org-back-to-heading t)
5162 (setq level (org-outline-level))
5163 (if (< arg 0) (setq arg (+ level arg)))
5164 (while (> (setq level (org-outline-level)) arg)
5165 (outline-up-heading 1 t)))
5167 heading (org-get-heading))
5168 (org-end-of-subtree t) (setq end (point)))
5170 (buffer-live-p org-last-indirect-buffer))
5171 (kill-buffer org-last-indirect-buffer))
5172 (setq ibuf (org-get-indirect-buffer cbuf)
5173 org-last-indirect-buffer ibuf)
5175 ((or (eq org-indirect-buffer-display 'new-frame)
5176 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
5177 (select-frame (make-frame))
5178 (delete-other-windows)
5179 (switch-to-buffer ibuf)
5180 (org-set-frame-title heading))
5181 ((eq org-indirect-buffer-display 'dedicated-frame)
5183 (select-frame (or (and org-indirect-dedicated-frame
5184 (frame-live-p org-indirect-dedicated-frame)
5185 org-indirect-dedicated-frame)
5186 (setq org-indirect-dedicated-frame (make-frame)))))
5187 (delete-other-windows)
5188 (switch-to-buffer ibuf)
5189 (org-set-frame-title (concat "Indirect: " heading)))
5190 ((eq org-indirect-buffer-display 'current-window)
5191 (switch-to-buffer ibuf))
5192 ((eq org-indirect-buffer-display 'other-window)
5193 (pop-to-buffer ibuf))
5194 (t (error "Invalid value.")))
5195 (if (featurep 'xemacs)
5196 (save-excursion (org-mode) (turn-on-font-lock)))
5197 (narrow-to-region beg end)
5200 (and (window-live-p cwin) (select-window cwin))))
5202 (defun org-get-indirect-buffer (&optional buffer)
5203 (setq buffer (or buffer (current-buffer)))
5204 (let ((n 1) (base (buffer-name buffer)) bname)
5205 (while (buffer-live-p
5206 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
5209 (make-indirect-buffer buffer bname 'clone)
5210 (error (make-indirect-buffer buffer bname)))))
5212 (defun org-set-frame-title (title)
5213 "Set the title of the current frame to the string TITLE."
5214 ;; FIXME: how to name a single frame in XEmacs???
5215 (unless (featurep 'xemacs)
5216 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
5218 ;;;; Structure editing
5220 ;;; Inserting headlines
5222 (defun org-insert-heading (&optional force-heading)
5223 "Insert a new heading or item with same depth at point.
5224 If point is in a plain list and FORCE-HEADING is nil, create a new list item.
5225 If point is at the beginning of a headline, insert a sibling before the
5226 current headline. If point is in the middle of a headline, split the headline
5227 at that position and make the rest of the headline part of the sibling below
5228 the current headline."
5230 (if (= (buffer-size) 0)
5232 (when (or force-heading (not (org-insert-item)))
5233 (let* ((head (save-excursion
5236 (org-back-to-heading)
5239 (blank (cdr (assq 'heading org-blank-before-new-entry)))
5242 ((and (org-on-heading-p) (bolp)
5244 (save-excursion (backward-char 1) (not (org-invisible-p)))))
5245 (open-line (if blank 2 1)))
5249 (backward-char 1) (not (org-invisible-p)))))
5251 (t (newline (if blank 2 1))))
5252 (insert head) (just-one-space)
5255 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
5256 (run-hooks 'org-insert-heading-hook)))))
5258 (defun org-insert-todo-heading (arg)
5259 "Insert a new heading with the same level and TODO state as current heading.
5260 If the heading has no TODO state, or if the state is DONE, use the first
5261 state (TODO by default). Also with prefix arg, force first state."
5263 (when (not (org-insert-item 'checkbox))
5264 (org-insert-heading)
5266 (org-back-to-heading)
5267 (outline-previous-heading)
5268 (looking-at org-todo-line-regexp))
5270 (not (match-beginning 2))
5271 (member (match-string 2) org-done-keywords))
5272 (insert (car org-todo-keywords-1) " ")
5273 (insert (match-string 2) " "))))
5275 (defun org-insert-subheading (arg)
5276 "Insert a new subheading and demote it.
5277 Works for outline headings and for plain lists alike."
5279 (org-insert-heading arg)
5281 ((org-on-heading-p) (org-do-demote))
5282 ((org-at-item-p) (org-indent-item 1))))
5284 (defun org-insert-todo-subheading (arg)
5285 "Insert a new subheading with TODO keyword or checkbox and demote it.
5286 Works for outline headings and for plain lists alike."
5288 (org-insert-todo-heading arg)
5290 ((org-on-heading-p) (org-do-demote))
5291 ((org-at-item-p) (org-indent-item 1))))
5293 ;;; Promotion and Demotion
5295 (defun org-promote-subtree ()
5296 "Promote the entire subtree.
5297 See also `org-promote'."
5300 (org-map-tree 'org-promote))
5301 (org-fix-position-after-promote))
5303 (defun org-demote-subtree ()
5304 "Demote the entire subtree. See `org-demote'.
5305 See also `org-promote'."
5308 (org-map-tree 'org-demote))
5309 (org-fix-position-after-promote))
5312 (defun org-do-promote ()
5313 "Promote the current heading higher up the tree.
5314 If the region is active in `transient-mark-mode', promote all headings
5318 (if (org-region-active-p)
5319 (org-map-region 'org-promote (region-beginning) (region-end))
5321 (org-fix-position-after-promote))
5323 (defun org-do-demote ()
5324 "Demote the current heading lower down the tree.
5325 If the region is active in `transient-mark-mode', demote all headings
5329 (if (org-region-active-p)
5330 (org-map-region 'org-demote (region-beginning) (region-end))
5332 (org-fix-position-after-promote))
5334 (defun org-fix-position-after-promote ()
5335 "Make sure that after pro/demotion cursor position is right."
5336 (let ((pos (point)))
5337 (when (save-excursion
5338 (beginning-of-line 1)
5339 (looking-at org-todo-line-regexp)
5340 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
5341 (cond ((eobp) (insert " "))
5342 ((eolp) (insert " "))
5343 ((equal (char-after) ?\ ) (forward-char 1))))))
5345 (defun org-reduced-level (l)
5346 (if org-odd-levels-only (1+ (floor (/ l 2))) l))
5348 (defun org-get-legal-level (level &optional change)
5349 "Rectify a level change under the influence of `org-odd-levels-only'
5350 LEVEL is a current level, CHANGE is by how much the level should be
5351 modified. Even if CHANGE is nil, LEVEL may be returned modified because
5352 even level numbers will become the next higher odd number."
5353 (if org-odd-levels-only
5354 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
5355 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
5356 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
5357 (max 1 (+ level change))))
5359 (defun org-promote ()
5360 "Promote the current heading higher up the tree.
5361 If the region is active in `transient-mark-mode', promote all headings
5363 (org-back-to-heading t)
5364 (let* ((level (save-match-data (funcall outline-level)))
5365 (up-head (concat (make-string (org-get-legal-level level -1) ?*) " "))
5366 (diff (abs (- level (length up-head) -1))))
5367 (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
5368 (replace-match up-head nil t)
5369 ;; Fixup tag positioning
5370 (and org-auto-align-tags (org-set-tags nil t))
5371 (if org-adapt-indentation (org-fixup-indentation (- diff)))))
5373 (defun org-demote ()
5374 "Demote the current heading lower down the tree.
5375 If the region is active in `transient-mark-mode', demote all headings
5377 (org-back-to-heading t)
5378 (let* ((level (save-match-data (funcall outline-level)))
5379 (down-head (concat (make-string (org-get-legal-level level 1) ?*) " "))
5380 (diff (abs (- level (length down-head) -1))))
5381 (replace-match down-head nil t)
5382 ;; Fixup tag positioning
5383 (and org-auto-align-tags (org-set-tags nil t))
5384 (if org-adapt-indentation (org-fixup-indentation diff))))
5386 (defun org-map-tree (fun)
5387 "Call FUN for every heading underneath the current one."
5388 (org-back-to-heading)
5389 (let ((level (funcall outline-level)))
5393 (outline-next-heading)
5394 (> (funcall outline-level) level))
5398 (defun org-map-region (fun beg end)
5399 "Call FUN for every heading between BEG and END."
5400 (let ((org-ignore-region t))
5402 (setq end (copy-marker end))
5404 (if (and (re-search-forward (concat "^" outline-regexp) nil t)
5408 (outline-next-heading)
5413 (defun org-fixup-indentation (diff)
5414 "Change the indentation in the current entry by DIFF
5415 However, if any line in the current entry has no indentation, or if it
5416 would end up with no indentation after the change, nothing at all is done."
5418 (let ((end (save-excursion (outline-next-heading)
5420 (prohibit (if (> diff 0)
5422 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
5424 (unless (save-excursion (end-of-line 1)
5425 (re-search-forward prohibit end t))
5426 (while (re-search-forward "^[ \t]+" end t)
5427 (goto-char (match-end 0))
5428 (setq col (current-column))
5429 (if (< diff 0) (replace-match ""))
5430 (indent-to (+ diff col))))
5431 (move-marker end nil))))
5433 (defun org-convert-to-odd-levels ()
5434 "Convert an org-mode file with all levels allowed to one with odd levels.
5435 This will leave level 1 alone, convert level 2 to level 3, level 3 to
5438 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
5439 (let ((org-odd-levels-only nil) n)
5441 (goto-char (point-min))
5442 (while (re-search-forward "^\\*\\*+ " nil t)
5443 (setq n (- (length (match-string 0)) 2))
5444 (while (>= (setq n (1- n)) 0)
5446 (end-of-line 1))))))
5449 (defun org-convert-to-oddeven-levels ()
5450 "Convert an org-mode file with only odd levels to one with odd and even levels.
5451 This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
5452 section with an even level, conversion would destroy the structure of the file. An error
5453 is signaled in this case."
5455 (goto-char (point-min))
5456 ;; First check if there are no even levels
5457 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
5458 (org-show-context t)
5459 (error "Not all levels are odd in this file. Conversion not possible."))
5460 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
5461 (let ((org-odd-levels-only nil) n)
5463 (goto-char (point-min))
5464 (while (re-search-forward "^\\*\\*+ " nil t)
5465 (setq n (/ (length (1- (match-string 0))) 2))
5466 (while (>= (setq n (1- n)) 0)
5468 (end-of-line 1))))))
5470 (defun org-tr-level (n)
5471 "Make N odd if required."
5472 (if org-odd-levels-only (1+ (/ n 2)) n))
5474 ;;; Vertical tree motion, cutting and pasting of subtrees
5476 (defun org-move-subtree-up (&optional arg)
5477 "Move the current subtree up past ARG headlines of the same level."
5479 (org-move-subtree-down (- (prefix-numeric-value arg))))
5481 (defun org-move-subtree-down (&optional arg)
5482 "Move the current subtree down past ARG headlines of the same level."
5484 (setq arg (prefix-numeric-value arg))
5485 (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
5486 'outline-get-last-sibling))
5487 (ins-point (make-marker))
5491 (org-back-to-heading)
5494 (save-excursion (outline-end-of-heading)
5495 (setq folded (org-invisible-p)))
5496 (outline-end-of-subtree))
5497 (outline-next-heading)
5499 ;; Find insertion point, with error handling
5502 (or (and (funcall movfunc) (looking-at outline-regexp))
5503 (progn (goto-char beg)
5504 (error "Cannot move past superior level or buffer limit")))
5505 (setq cnt (1- cnt)))
5507 ;; Moving forward - still need to move over subtree
5508 (progn (outline-end-of-subtree)
5509 (outline-next-heading)
5510 (if (not (or (looking-at (concat "^" outline-regexp))
5513 (move-marker ins-point (point))
5514 (setq txt (buffer-substring beg end))
5515 (delete-region beg end)
5517 (or (bolp) (insert "\n"))
5518 (goto-char ins-point)
5519 (if folded (hide-subtree))
5520 (move-marker ins-point nil)))
5522 (defvar org-subtree-clip ""
5523 "Clipboard for cut and paste of subtrees.
5524 This is actually only a copy of the kill, because we use the normal kill
5525 ring. We need it to check if the kill was created by `org-copy-subtree'.")
5527 (defvar org-subtree-clip-folded nil
5528 "Was the last copied subtree folded?
5529 This is used to fold the tree back after pasting.")
5531 (defun org-cut-subtree ()
5532 "Cut the current subtree into the clipboard.
5533 This is a short-hand for marking the subtree and then cutting it."
5535 (org-copy-subtree 'cut))
5537 (defun org-copy-subtree (&optional cut)
5538 "Cut the current subtree into the clipboard.
5539 This is a short-hand for marking the subtree and then copying it.
5540 If CUT is non-nil, actually cut the subtree."
5542 (let (beg end folded)
5544 (org-back-to-heading nil) ; take what looks like a subtree
5545 (org-back-to-heading t)) ; take what is really there
5548 (save-excursion (outline-end-of-heading)
5549 (setq folded (org-invisible-p)))
5550 (outline-end-of-subtree))
5551 (if (equal (char-after) ?\n) (forward-char 1))
5555 (setq org-subtree-clip-folded folded)
5556 (if cut (kill-region beg end) (copy-region-as-kill beg end))
5557 (setq org-subtree-clip (current-kill 0))
5558 (message "%s: Subtree with %d characters"
5559 (if cut "Cut" "Copied")
5560 (length org-subtree-clip)))))
5562 (defun org-paste-subtree (&optional level tree)
5563 "Paste the clipboard as a subtree, with modification of headline level.
5564 The entire subtree is promoted or demoted in order to match a new headline
5565 level. By default, the new level is derived from the visible headings
5566 before and after the insertion point, and taken to be the inferior headline
5567 level of the two. So if the previous visible heading is level 3 and the
5568 next is level 4 (or vice versa), level 4 will be used for insertion.
5569 This makes sure that the subtree remains an independent subtree and does
5570 not swallow low level entries.
5572 You can also force a different level, either by using a numeric prefix
5573 argument, or by inserting the heading marker by hand. For example, if the
5574 cursor is after \"*****\", then the tree will be shifted to level 5.
5576 If you want to insert the tree as is, just use \\[yank].
5578 If optional TREE is given, use this text instead of the kill ring."
5580 (unless (org-kill-is-subtree-p tree)
5582 (substitute-command-keys
5583 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
5584 (let* ((txt (or tree (and kill-ring (current-kill 0))))
5585 (^re (concat "^\\(" outline-regexp "\\)"))
5586 (re (concat "\\(" outline-regexp "\\)"))
5587 (^re_ (concat "\\(" outline-regexp "\\)[ \t]*"))
5589 (old-level (if (string-match ^re txt)
5590 (- (match-end 0) (match-beginning 0) 1)
5592 (force-level (cond (level (prefix-numeric-value level))
5594 ^re_ (buffer-substring (point-at-bol) (point)))
5595 (- (match-end 0) (match-beginning 0)))
5597 (previous-level (save-excursion
5600 (outline-previous-visible-heading 1)
5602 (- (match-end 0) (match-beginning 0))
5605 (next-level (save-excursion
5608 (outline-next-visible-heading 1)
5610 (- (match-end 0) (match-beginning 0))
5613 (new-level (or force-level (max previous-level next-level)))
5614 (shift (if (or (= old-level -1)
5616 (= old-level new-level))
5618 (- new-level old-level)))
5620 (delta (if (> shift 0) -1 1))
5621 (func (if (> shift 0) 'org-demote 'org-promote))
5622 (org-odd-levels-only nil)
5624 ;; Remove the forces level indicator
5626 (delete-region (point-at-bol) (point)))
5627 ;; Make sure we start at the beginning of an empty line
5628 (if (not (bolp)) (insert "\n"))
5629 (if (not (looking-at "[ \t]*$"))
5630 (progn (insert "\n") (backward-char 1)))
5633 (if (string-match "[ \t\r\n]+\\'" txt)
5634 (setq txt (replace-match "\n" t t txt)))
5637 (if (looking-at "[ \t\r\n]+")
5638 (replace-match "\n"))
5640 ;; Shift if necessary
5642 (message "Pasted at level %d, without shift" new-level)
5644 (narrow-to-region beg end)
5645 (while (not (= shift 0))
5646 (org-map-region func (point-min) (point-max))
5647 (setq shift (+ delta shift)))
5648 (goto-char (point-min))
5649 (message "Pasted at level %d, with shift by %d levels"
5652 (eq org-subtree-clip (current-kill 0))
5653 org-subtree-clip-folded)
5654 ;; The tree was folded before it was killed/copied
5657 (defun org-kill-is-subtree-p (&optional txt)
5658 "Check if the current kill is an outline subtree, or a set of trees.
5659 Returns nil if kill does not start with a headline, or if the first
5660 headline level is not the largest headline level in the tree.
5661 So this will actually accept several entries of equal levels as well,
5662 which is OK for `org-paste-subtree'.
5663 If optional TXT is given, check this string instead of the current kill."
5664 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
5665 (start-level (and kill
5666 (string-match (concat "\\`" outline-regexp) kill)
5667 (- (match-end 0) (match-beginning 0))))
5668 (re (concat "^" outline-regexp))
5670 (if (not start-level)
5671 nil ;; does not even start with a heading
5673 (while (setq start (string-match re kill (1+ start)))
5674 (if (< (- (match-end 0) (match-beginning 0)) start-level)
5678 (defun org-narrow-to-subtree ()
5679 "Narrow buffer to the current subtree."
5683 (progn (org-back-to-heading) (point))
5684 (progn (org-end-of-subtree t t) (point)))))
5689 (defun org-sort (with-case)
5690 "Call `org-sort-entries' or `org-table-sort-lines', depending on context."
5692 (if (org-at-table-p)
5693 (org-call-with-arg 'org-table-sort-lines with-case)
5694 (org-call-with-arg 'org-sort-entries with-case)))
5696 (defun org-sort-entries (&optional with-case sorting-type)
5697 "Sort entries on a certain level of an outline tree.
5698 If there is an active region, the entries in the region are sorted.
5699 Else, if the cursor is before the first entry, sort the top-level items.
5700 Else, the children of the entry at point are sorted.
5702 Sorting can be alphabetically, numerically, and by date/time as given by
5703 the first time stamp in the entry. The command prompts for the sorting
5704 type unless it has been given to the function through the SORTING-TYPE
5705 argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T).
5707 Comparing entries ignores case by default. However, with an optional argument
5708 WITH-CASE, the sorting considers case as well. With two prefix arguments
5709 `C-u C-u', sorting is case-sensitive and duplicate entries will be removed."
5711 (let ((unique (equal with-case '(16)))
5712 start beg end entries stars re re2 p nentries (nremoved 0)
5714 ;; Find beginning and end of region to sort
5716 ((org-region-active-p)
5717 ;; we will sort the region
5718 (setq end (region-end)
5720 (goto-char (region-beginning))
5721 (if (not (org-on-heading-p)) (outline-next-heading))
5722 (setq start (point)))
5723 ((or (org-on-heading-p)
5724 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
5725 ;; we will sort the children of the current headline
5726 (org-back-to-heading)
5727 (setq start (point) end (org-end-of-subtree) what "children")
5730 (outline-next-heading))
5732 ;; we will sort the top-level entries in this file
5733 (goto-char (point-min))
5734 (or (org-on-heading-p) (outline-next-heading))
5735 (setq start (point) end (point-max) what "top-level")
5739 (if (>= (point) end) (error "Nothing to sort"))
5740 (looking-at "\\(\\*+\\)")
5741 (setq stars (match-string 1)
5742 re (concat "^" (regexp-quote stars) " +")
5743 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
5744 txt (buffer-substring beg end))
5745 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
5746 (if (and (not (equal stars "*")) (string-match re2 txt))
5747 (error "Region to sort contains a level above the first entry"))
5748 ;; Make a list that can be sorted.
5749 ;; The car is the string for comparison, the cdr is the subtree
5750 (message "Sorting entries...")
5754 (string-match "^.*\\(\n.*\\)?" x) ; take two lines
5755 (cons (match-string 0 x) x))
5756 (org-split-string txt re)))
5761 (setq entries (org-do-sort entries what with-case sorting-type)))
5763 ;; Delete the old stuff
5765 (kill-region beg end)
5766 (setq nentries (length entries))
5767 ;; Insert the sorted entries, and remove duplicates if this is required
5768 (while (setq p (pop entries))
5769 (if (and unique (equal last (setq last (org-trim (cdr p)))))
5770 (setq nremoved (1+ nremoved)) ; same entry as before, skip it
5771 (insert stars " " (cdr p))))
5773 (message "Sorting entries...done (%d entries%s)"
5775 (if unique (format ", %d duplicates removed" nremoved) ""))))
5777 (defun org-do-sort (table what &optional with-case sorting-type)
5778 "Sort TABLE of WHAT according to SORTING-TYPE.
5779 The user will be prompted for the SORTING-TYPE if the call to this
5780 function does not specify it. WHAT is only for the prompt, to indicate
5781 what is being sorted. The sorting key will be extracted from
5782 the car of the elements of the table.
5783 If WITH-CASE is non-nil, the sorting will be case-sensitive."
5784 (unless sorting-type
5786 "Sort %s: [a]lphabetically [n]umerically [t]ime. A/N/T means reversed:"
5788 (setq sorting-type (read-char-exclusive)))
5789 (let ((dcst (downcase sorting-type))
5790 extractfun comparefun)
5791 ;; Define the appropriate functions
5794 (setq extractfun 'string-to-number
5795 comparefun (if (= dcst sorting-type) '< '>)))
5797 (setq extractfun (if with-case 'identity 'downcase)
5798 comparefun (if (= dcst sorting-type)
5800 (lambda (a b) (and (not (string< a b))
5801 (not (string= a b)))))))
5805 (if (string-match org-ts-regexp x)
5807 (org-time-string-to-time (match-string 0 x)))
5809 comparefun (if (= dcst sorting-type) '< '>)))
5810 (t (error "Invalid sorting type `%c'" sorting-type)))
5812 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
5814 (lambda (a b) (funcall comparefun (car a) (car b))))))
5816 ;;;; Plain list items, including checkboxes
5818 ;;; Plain list items
5820 (defun org-at-item-p ()
5821 "Is point in a line starting a hand-formatted item?"
5822 (let ((llt org-plain-list-ordered-item-terminator))
5824 (goto-char (point-at-bol))
5827 ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
5828 ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
5829 ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
5830 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
5832 (defun org-in-item-p ()
5833 "It the cursor inside a plain list item.
5834 Does not have to be the first line."
5838 (org-beginning-of-item)
5843 (defun org-insert-item (&optional checkbox)
5844 "Insert a new item at the current level.
5845 Return t when things worked, nil when we are not in an item."
5846 (when (save-excursion
5849 (org-beginning-of-item)
5851 (if (org-invisible-p) (error "Invisible item"))
5854 (let* ((bul (match-string 0))
5855 (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
5857 (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
5860 ((and (org-at-item-p) (<= (point) eow))
5861 ;; before the bullet
5862 (beginning-of-line 1)
5863 (open-line (if blank 2 1)))
5865 (beginning-of-line 1))
5866 (t (newline (if blank 2 1))))
5867 (insert bul (if checkbox "[ ]" ""))
5871 (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
5872 (org-maybe-renumber-ordered-list)
5873 (and checkbox (org-update-checkbox-count-maybe))
5878 (defun org-at-item-checkbox-p ()
5879 "Is point at a line starting a plain-list item with a checklet?"
5880 (and (org-at-item-p)
5882 (goto-char (match-end 0))
5883 (skip-chars-forward " \t")
5884 (looking-at "\\[[- X]\\]"))))
5886 (defun org-toggle-checkbox (&optional arg)
5887 "Toggle the checkbox in the current line."
5890 (let (beg end status (firstnew 'unknown))
5892 ((org-region-active-p)
5893 (setq beg (region-beginning) end (region-end)))
5895 (setq beg (point) end (save-excursion (outline-next-heading) (point))))
5896 ((org-at-item-checkbox-p)
5900 ((member (match-string 0) '("[ ]" "[-]")) "[X]")
5904 (t (error "Not at a checkbox or heading, and no active region")))
5907 (while (< (point) end)
5908 (when (org-at-item-checkbox-p)
5909 (setq status (equal (match-string 0) "[X]"))
5910 (when (eq firstnew 'unknown)
5911 (setq firstnew (not status)))
5913 (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
5914 (beginning-of-line 2)))))
5915 (org-update-checkbox-count-maybe))
5917 (defun org-update-checkbox-count-maybe ()
5918 "Update checkbox statistics unless turned off by user."
5919 (when org-provide-checkbox-statistics
5920 (org-update-checkbox-count)))
5922 (defun org-update-checkbox-count (&optional all)
5923 "Update the checkbox statistics in the current section.
5924 This will find all statistic cookies like [57%] and [6/12] and update them
5925 with the current numbers. With optional prefix argument ALL, do this for
5929 (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
5930 (beg (condition-case nil
5931 (progn (outline-back-to-heading) (point))
5932 (error (point-min))))
5933 (end (move-marker (make-marker)
5934 (progn (outline-next-heading) (point))))
5935 (re "\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)")
5936 (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
5937 b1 e1 f1 c-on c-off lim (cstat 0))
5939 (goto-char (point-min))
5940 (outline-next-heading)
5941 (setq beg (point) end (point-max)))
5943 (while (re-search-forward re end t)
5944 (setq cstat (1+ cstat)
5945 b1 (match-beginning 0)
5947 f1 (match-beginning 1)
5949 ((org-on-heading-p) (outline-next-heading) (point))
5950 ((org-at-item-p) (org-end-of-item) (point))
5955 (while (re-search-forward re-box lim t)
5956 (if (member (match-string 2) '("[ ]" "[-]"))
5957 (setq c-off (1+ c-off))
5958 (setq c-on (1+ c-on))))
5959 ; (delete-region b1 e1)
5962 (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
5963 (format "[%d/%d]" c-on (+ c-on c-off))))
5964 (and (looking-at "\\[.*?\\]")
5965 (replace-match ""))))
5966 (when (interactive-p)
5967 (message "Checkbox satistics updated %s (%d places)"
5968 (if all "in entire file" "in current outline entry") cstat)))))
5970 (defun org-get-checkbox-statistics-face ()
5971 "Select the face for checkbox statistics.
5972 The face will be `org-done' when all relevant boxes are checked. Otherwise
5973 it will be `org-todo'."
5975 (if (equal (match-string 1) "100%") 'org-done 'org-todo)
5976 (if (and (> (match-end 2) (match-beginning 2))
5977 (equal (match-string 2) (match-string 3)))
5981 (defun org-get-indentation (&optional line)
5982 "Get the indentation of the current line, interpreting tabs.
5983 When LINE is given, assume it represents a line and compute its indentation."
5985 (if (string-match "^ *" (org-remove-tabs line))
5988 (beginning-of-line 1)
5989 (skip-chars-forward " \t")
5992 (defun org-remove-tabs (s &optional width)
5993 "Replace tabulators in S with spaces.
5994 Assumes that s is a single line, starting in column 0."
5995 (setq width (or width tab-width))
5996 (while (string-match "\t" s)
5997 (setq s (replace-match
5999 (- (* width (/ (+ (match-beginning 0) width) width))
6000 (match-beginning 0)) ?\ )
6004 (defun org-fix-indentation (line ind)
6005 "Fix indentation in LINE.
6006 IND is a cons cell with target and minimum indentation.
6007 If the current indenation in LINE is smaller than the minimum,
6008 leave it alone. If it is larger than ind, set it to the target."
6009 (let* ((l (org-remove-tabs line))
6010 (i (org-get-indentation l))
6011 (i1 (car ind)) (i2 (cdr ind)))
6012 (if (>= i i2) (setq l (substring line i2)))
6014 (concat (make-string i1 ?\ ) l)
6017 (defcustom org-empty-line-terminates-plain-lists nil
6018 "Non-nil means, an empty line ends all plain list levels.
6019 When nil, empty lines are part of the preceeding item."
6020 :group 'org-plain-lists
6023 (defun org-beginning-of-item ()
6024 "Go to the beginning of the current hand-formatted item.
6025 If the cursor is not in an item, throw an error."
6028 (limit (save-excursion
6031 (org-back-to-heading)
6032 (beginning-of-line 2) (point))
6033 (error (point-min)))))
6034 (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
6037 (beginning-of-line 1)
6038 (beginning-of-line 1)
6039 (skip-chars-forward " \t")
6040 (setq ind (current-column))
6043 (beginning-of-line 0)
6044 (if (or (bobp) (< (point) limit)) (throw 'exit nil))
6046 (if (looking-at "[ \t]*$")
6047 (setq ind1 ind-empty)
6048 (skip-chars-forward " \t")
6049 (setq ind1 (current-column)))
6051 (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
6054 (error "Not in an item")))))
6056 (defun org-end-of-item ()
6057 "Go to the end of the current hand-formatted item.
6058 If the cursor is not in an item, throw an error."
6060 (let* ((pos (point))
6062 (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
6063 (limit (save-excursion (outline-next-heading) (point)))
6064 (ind (save-excursion
6065 (org-beginning-of-item)
6066 (skip-chars-forward " \t")
6070 (beginning-of-line 2)
6071 (if (eobp) (throw 'exit (point)))
6072 (if (>= (point) limit) (throw 'exit (point-at-bol)))
6073 (if (looking-at "[ \t]*$")
6074 (setq ind1 ind-empty)
6075 (skip-chars-forward " \t")
6076 (setq ind1 (current-column)))
6078 (throw 'exit (point-at-bol)))))))
6082 (error "Not in an item"))))
6084 (defun org-next-item ()
6085 "Move to the beginning of the next item in the current plain list.
6086 Error if not at a plain list, or if this is the last item in the list."
6088 (let (ind ind1 (pos (point)))
6089 (org-beginning-of-item)
6090 (setq ind (org-get-indentation))
6092 (setq ind1 (org-get-indentation))
6093 (unless (and (org-at-item-p) (= ind ind1))
6095 (error "On last item"))))
6097 (defun org-previous-item ()
6098 "Move to the beginning of the previous item in the current plain list.
6099 Error if not at a plain list, or if this is the first item in the list."
6101 (let (beg ind ind1 (pos (point)))
6102 (org-beginning-of-item)
6104 (setq ind (org-get-indentation))
6108 (beginning-of-line 0)
6109 (if (looking-at "[ \t]*$")
6111 (if (<= (setq ind1 (org-get-indentation)) ind)
6114 (if (or (not (org-at-item-p))
6117 (org-beginning-of-item))
6118 (error (goto-char pos)
6119 (error "On first item")))))
6121 (defun org-move-item-down ()
6122 "Move the plain list item at point down, i.e. swap with following item.
6123 Subitems (items with larger indentation) are considered part of the item,
6124 so this really moves item trees."
6126 (let (beg end ind ind1 (pos (point)) txt)
6127 (org-beginning-of-item)
6129 (setq ind (org-get-indentation))
6132 (setq ind1 (org-get-indentation))
6133 (if (and (org-at-item-p) (= ind ind1))
6136 (setq txt (buffer-substring beg end))
6138 (delete-region beg end))
6142 (org-maybe-renumber-ordered-list))
6144 (error "Cannot move this item further down"))))
6146 (defun org-move-item-up (arg)
6147 "Move the plain list item at point up, i.e. swap with previous item.
6148 Subitems (items with larger indentation) are considered part of the item,
6149 so this really moves item trees."
6151 (let (beg end ind ind1 (pos (point)) txt)
6152 (org-beginning-of-item)
6154 (setq ind (org-get-indentation))
6160 (beginning-of-line 0)
6161 (if (looking-at "[ \t]*$")
6162 (if org-empty-line-terminates-plain-lists
6165 (error "Cannot move this item further up"))
6167 (if (<= (setq ind1 (org-get-indentation)) ind)
6170 (org-beginning-of-item)
6171 (error (goto-char beg)
6172 (error "Cannot move this item further up")))
6173 (setq ind1 (org-get-indentation))
6174 (if (and (org-at-item-p) (= ind ind1))
6176 (setq txt (buffer-substring beg end))
6178 (delete-region beg end))
6182 (org-maybe-renumber-ordered-list))
6184 (error "Cannot move this item further up"))))
6186 (defun org-maybe-renumber-ordered-list ()
6187 "Renumber the ordered list at point if setup allows it.
6188 This tests the user option `org-auto-renumber-ordered-lists' before
6189 doing the renumbering."
6191 (when (and org-auto-renumber-ordered-lists
6193 (if (match-beginning 3)
6194 (org-renumber-ordered-list 1)
6195 (org-fix-bullet-type 1))))
6197 (defun org-maybe-renumber-ordered-list-safe ()
6200 (org-maybe-renumber-ordered-list))
6203 (defun org-cycle-list-bullet (&optional which)
6204 "Cycle through the different itemize/enumerate bullets.
6205 This cycle the entire list level through the sequence:
6207 `-' -> `+' -> `*' -> `1.' -> `1)'
6209 If WHICH is a string, use that as the new bullet. If WHICH is an integer,
6210 0 meand `-', 1 means `+' etc."
6213 (org-beginning-of-item-list)
6215 (beginning-of-line 1)
6216 (let ((current (match-string 0)) new)
6218 ((and which (nth (1- which) '("-" "+" "*" "1." "1)"))))
6219 ((string-match "-" current) "+")
6220 ((string-match "\\+" current)
6221 (if (looking-at "\\S-") "1." "*"))
6222 ((string-match "\\*" current) "1.")
6223 ((string-match "\\." current) "1)")
6224 ((string-match ")" current) "-")
6225 (t (error "This should not happen"))))
6226 (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
6227 (org-fix-bullet-type 1)
6228 (org-maybe-renumber-ordered-list))))
6230 (defun org-get-string-indentation (s)
6231 "What indentation has S due to SPACE and TAB at the beginning of the string?"
6232 (let ((n -1) (i 0) (w tab-width) c)
6234 (while (< (setq n (1+ n)) (length s))
6236 (cond ((= c ?\ ) (setq i (1+ i)))
6237 ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
6238 (t (throw 'exit t)))))
6241 (defun org-renumber-ordered-list (arg)
6242 "Renumber an ordered plain list.
6243 Cursor needs to be in the first line of an item, the line that starts
6244 with something like \"1.\" or \"2)\"."
6246 (unless (and (org-at-item-p)
6247 (match-beginning 3))
6248 (error "This is not an ordered list"))
6249 (let ((line (org-current-line))
6250 (col (current-column))
6251 (ind (org-get-string-indentation
6252 (buffer-substring (point-at-bol) (match-beginning 3))))
6253 ;; (term (substring (match-string 3) -1))
6256 ;; find where this list begins
6257 (org-beginning-of-item-list)
6258 (looking-at "[ \t]*[0-9]+\\([.)]\\)")
6259 (setq fmt (concat "%d" (match-string 1)))
6260 (beginning-of-line 0)
6261 ;; walk forward and replace these numbers
6265 (beginning-of-line 2)
6266 (if (eobp) (throw 'exit nil))
6267 (if (looking-at "[ \t]*$") (throw 'next nil))
6268 (skip-chars-forward " \t") (setq ind1 (current-column))
6269 (if (> ind1 ind) (throw 'next t))
6270 (if (< ind1 ind) (throw 'exit t))
6271 (if (not (org-at-item-p)) (throw 'exit nil))
6272 (delete-region (match-beginning 2) (match-end 2))
6273 (goto-char (match-beginning 2))
6274 (insert (format fmt (setq n (1+ n)))))))
6276 (move-to-column col)))
6278 (defun org-fix-bullet-type (arg)
6279 "Make sure all items in this list have the same bullet."
6281 (unless (org-at-item-p) (error "This is not a list"))
6282 (let ((line (org-current-line))
6283 (col (current-column))
6284 (ind (current-indentation))
6286 ;; find where this list begins
6287 (org-beginning-of-item-list)
6288 (beginning-of-line 1)
6289 ;; find out what the bullet type is
6290 (looking-at "[ \t]*\\(\\S-+\\)")
6291 (setq bullet (match-string 1))
6292 ;; walk forward and replace these numbers
6293 (beginning-of-line 0)
6297 (beginning-of-line 2)
6298 (if (eobp) (throw 'exit nil))
6299 (if (looking-at "[ \t]*$") (throw 'next nil))
6300 (skip-chars-forward " \t") (setq ind1 (current-column))
6301 (if (> ind1 ind) (throw 'next t))
6302 (if (< ind1 ind) (throw 'exit t))
6303 (if (not (org-at-item-p)) (throw 'exit nil))
6304 (skip-chars-forward " \t")
6305 (looking-at "\\S-+")
6306 (replace-match bullet))))
6308 (move-to-column col)
6309 (if (string-match "[0-9]" bullet)
6310 (org-renumber-ordered-list 1))))
6312 (defun org-beginning-of-item-list ()
6313 "Go to the beginning of the current item list.
6314 I.e. to the first item in this list."
6316 (org-beginning-of-item)
6317 (let ((pos (point-at-bol))
6318 (ind (org-get-indentation))
6320 ;; find where this list begins
6324 (beginning-of-line 0)
6325 (if (looking-at "[ \t]*$")
6326 (throw (if (bobp) 'exit 'next) t))
6327 (skip-chars-forward " \t") (setq ind1 (current-column))
6328 (if (or (< ind1 ind)
6330 (not (org-at-item-p)))
6333 (when (org-at-item-p) (setq pos (point-at-bol)))))))
6336 (defvar org-last-indent-begin-marker (make-marker))
6337 (defvar org-last-indent-end-marker (make-marker))
6339 (defun org-outdent-item (arg)
6340 "Outdent a local list item."
6342 (org-indent-item (- arg)))
6344 (defun org-indent-item (arg)
6345 "Indent a local list item."
6347 (unless (org-at-item-p)
6348 (error "Not on an item"))
6350 (let (beg end ind ind1 tmp delta ind-down ind-up)
6351 (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
6352 (setq beg org-last-indent-begin-marker
6353 end org-last-indent-end-marker)
6354 (org-beginning-of-item)
6355 (setq beg (move-marker org-last-indent-begin-marker (point)))
6357 (setq end (move-marker org-last-indent-end-marker (point))))
6359 (setq tmp (org-item-indent-positions)
6361 ind-down (nth 2 tmp)
6364 (if ind-down (- ind-down ind) 2)
6365 (if ind-up (- ind-up ind) -2)))
6366 (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
6367 (while (< (point) end)
6368 (beginning-of-line 1)
6369 (skip-chars-forward " \t") (setq ind1 (current-column))
6370 (delete-region (point-at-bol) (point))
6371 (or (eolp) (indent-to-column (+ ind1 delta)))
6372 (beginning-of-line 2))))
6373 (org-maybe-renumber-ordered-list-safe)
6375 (beginning-of-line 0)
6376 (condition-case nil (org-beginning-of-item) (error nil))
6377 (org-maybe-renumber-ordered-list-safe)))
6380 (defun org-item-indent-positions ()
6381 "Assumes cursor in item line. FIXME"
6382 (let* ((bolpos (point-at-bol))
6383 (ind (org-get-indentation))
6384 ind-down ind-up pos)
6386 (org-beginning-of-item-list)
6387 (skip-chars-backward "\n\r \t")
6388 (when (org-in-item-p)
6389 (org-beginning-of-item)
6390 (setq ind-up (org-get-indentation))))
6394 ((and (condition-case nil (progn (org-previous-item) t)
6396 (or (forward-char 1) t)
6397 (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
6398 (setq ind-down (org-get-indentation)))
6399 ((and (goto-char pos)
6401 (goto-char (match-end 0))
6402 (skip-chars-forward " \t")
6403 (setq ind-down (current-column)))))
6404 (list ind ind-up ind-down)))
6406 ;;; The orgstruct minor mode
6408 ;; Define a minor mode which can be used in other modes in order to
6409 ;; integrate the org-mode structure editing commands.
6411 ;; This is really a hack, because the org-mode structure commands use
6412 ;; keys which normally belong to the major mode. Here is how it
6413 ;; works: The minor mode defines all the keys necessary to operate the
6414 ;; structure commands, but wraps the commands into a function which
6415 ;; tests if the cursor is currently at a headline or a plain list
6416 ;; item. If that is the case, the structure command is used,
6417 ;; temporarily setting many Org-mode variables like regular
6418 ;; expressions for filling etc. However, when any of those keys is
6419 ;; used at a different location, function uses `key-binding' to look
6420 ;; up if the key has an associated command in another currently active
6421 ;; keymap (minor modes, major mode, global), and executes that
6422 ;; command. There might be problems if any of the keys is otherwise
6423 ;; used as a prefix key.
6425 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
6426 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
6427 ;; addresses this by checking explicitly for both bindings.
6429 (defvar orgstruct-mode-map (make-sparse-keymap)
6430 "Keymap for the minor `orgstruct-mode'.")
6433 (define-minor-mode orgstruct-mode
6434 "Toggle the minor more `orgstruct-mode'.
6435 This mode is for using Org-mode structure commands in other modes.
6436 The following key behave as if Org-mode was active, if the cursor
6437 is on a headline, or on a plain list item (both in the definition
6440 M-up Move entry/item up
6441 M-down Move entry/item down
6444 M-S-up Move entry/item up
6445 M-S-down Move entry/item down
6446 M-S-left Promote subtree
6447 M-S-right Demote subtree
6448 M-q Fill paragraph and items like in Org-mode
6450 C-c - Cycle list bullet
6451 TAB Cycle item visibility
6452 M-RET Insert new heading/item
6453 S-M-RET Insert new TODO heading / Chekbox item
6454 C-c C-c Set tags / toggle checkbox"
6455 nil " OrgStruct" nil
6456 (and (orgstruct-setup) (defun orgstruct-setup () nil)))
6459 (defun turn-on-orgstruct ()
6460 "Unconditionally turn on `orgstruct-mode'."
6463 (defun orgstruct-error ()
6464 "Error when there is no default binding for a structure key."
6466 (error "This key is has no function outside structure elements"))
6468 (defvar org-local-vars nil
6469 "List of local variables, for use by `orgstruct-mode'")
6471 (defun orgstruct-setup ()
6472 "Setup orgstruct keymaps."
6476 '([(meta up)] org-metaup)
6477 '([(meta down)] org-metadown)
6478 '([(meta left)] org-metaleft)
6479 '([(meta right)] org-metaright)
6480 '([(meta shift up)] org-shiftmetaup)
6481 '([(meta shift down)] org-shiftmetadown)
6482 '([(meta shift left)] org-shiftmetaleft)
6483 '([(meta shift right)] org-shiftmetaright)
6484 '([(shift up)] org-shiftup)
6485 '([(shift down)] org-shiftdown)
6486 '("\C-c\C-c" org-ctrl-c-ctrl-c)
6487 '("\M-q" fill-paragraph)
6489 '("\C-c-" org-cycle-list-bullet)))
6491 (while (setq elt (pop bindings))
6492 (setq nfunc (1+ nfunc))
6493 (setq key (org-key (car elt))
6495 cmd (orgstruct-make-binding fun nfunc key))
6496 (org-defkey orgstruct-mode-map key cmd))
6498 ;; Special treatment needed for TAB and RET
6499 (org-defkey orgstruct-mode-map [(tab)]
6500 (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
6501 (org-defkey orgstruct-mode-map "\C-i"
6502 (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
6504 (org-defkey orgstruct-mode-map "\M-\C-m"
6505 (orgstruct-make-binding 'org-insert-heading 105
6506 "\M-\C-m" [(meta return)]))
6507 (org-defkey orgstruct-mode-map [(meta return)]
6508 (orgstruct-make-binding 'org-insert-heading 106
6509 [(meta return)] "\M-\C-m"))
6511 (org-defkey orgstruct-mode-map [(shift meta return)]
6512 (orgstruct-make-binding 'org-insert-todo-heading 107
6513 [(meta return)] "\M-\C-m"))
6515 (unless org-local-vars
6516 (setq org-local-vars (org-get-local-variables)))
6520 (defun orgstruct-make-binding (fun n &rest keys)
6521 "Create a function for binding in the structure minor mode.
6522 FUN is the command to call inside a table. N is used to create a unique
6523 command name. KEYS are keys that should be checked in for a command
6524 to execute outside of tables."
6527 (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
6529 (concat "In Structure, run `" (symbol-name fun) "'.\n"
6530 "Outside of structure, run the binding of `"
6531 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
6535 '(org-context-p 'headline 'item)
6536 (list 'org-run-like-in-org-mode (list 'quote fun))
6537 (list 'let '(orgstruct-mode)
6538 (list 'call-interactively
6541 (list 'key-binding k))
6543 '('orgstruct-error))))))))
6545 (defun org-context-p (&rest contexts)
6547 (let ((pos (point)))
6548 (goto-char (point-at-bol))
6549 (prog1 (or (and (memq 'table contexts)
6550 (looking-at "[ \t]*|"))
6551 (and (memq 'headline contexts)
6552 (looking-at "\\*+"))
6553 (and (memq 'item contexts)
6554 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
6557 (defun org-get-local-variables ()
6558 "Return a list of all local variables in an org-mode buffer."
6560 (with-current-buffer (get-buffer-create "*Org tmp*")
6563 (setq varlist (buffer-local-variables)))
6564 (kill-buffer "*Org tmp*")
6571 (list (car x) (list 'quote (cdr x)))))
6573 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
6574 (symbol-name (car x)))
6579 (defun org-run-like-in-org-mode (cmd)
6580 (unless org-local-vars
6581 (setq org-local-vars (org-get-local-variables)))
6582 (eval (list 'let org-local-vars
6583 (list 'call-interactively (list 'quote cmd)))))
6587 (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
6589 (defun org-archive-subtree (&optional find-done)
6590 "Move the current subtree to the archive.
6591 The archive can be a certain top-level heading in the current file, or in
6592 a different file. The tree will be moved to that location, the subtree
6593 heading be marked DONE, and the current time will be added.
6595 When called with prefix argument FIND-DONE, find whole trees without any
6596 open TODO items and archive them (after getting confirmation from the user).
6597 If the cursor is not at a headline when this comand is called, try all level
6598 1 trees. If the cursor is on a headline, only try the direct children of
6602 (org-archive-all-done)
6603 ;; Save all relevant TODO keyword-relatex variables
6605 (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
6606 (tr-org-todo-keywords-1 org-todo-keywords-1)
6607 (tr-org-todo-kwd-alist org-todo-kwd-alist)
6608 (tr-org-done-keywords org-done-keywords)
6609 (tr-org-todo-regexp org-todo-regexp)
6610 (tr-org-todo-line-regexp org-todo-line-regexp)
6611 (tr-org-odd-levels-only org-odd-levels-only)
6612 (this-buffer (current-buffer))
6613 (org-archive-location org-archive-location)
6614 (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
6615 file heading buffer level newfile-p)
6617 ;; Try to find a local archive location
6621 (if (or (re-search-backward re nil t) (re-search-forward re nil t))
6622 (setq org-archive-location (match-string 1)))))
6624 (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
6626 (setq file (format (match-string 1 org-archive-location)
6627 (file-name-nondirectory buffer-file-name))
6628 heading (match-string 2 org-archive-location)))
6629 (error "Invalid `org-archive-location'"))
6630 (if (> (length file) 0)
6631 (setq newfile-p (not (file-exists-p file))
6632 buffer (find-file-noselect file))
6633 (setq buffer (current-buffer)))
6635 (error "Cannot access file \"%s\"" file))
6636 (if (and (> (length heading) 0)
6637 (string-match "^\\*+" heading))
6638 (setq level (match-end 0))
6639 (setq heading nil level 0))
6641 ;; We first only copy, in case something goes wrong
6642 ;; we need to protect this-command, to avoid kill-region sets it,
6643 ;; which would lead to duplication of subtrees
6644 (let (this-command) (org-copy-subtree))
6646 ;; Enforce org-mode for the archive buffer
6647 (if (not (org-mode-p))
6648 ;; Force the mode for future visits.
6649 (let ((org-insert-mode-line-in-empty-file t)
6650 (org-inhibit-startup t))
6651 (call-interactively 'org-mode)))
6653 (goto-char (point-max))
6654 (insert (format "\nArchived entries from file %s\n\n"
6655 (buffer-file-name this-buffer))))
6656 ;; Force the TODO keywords of the original buffer
6657 (let ((org-todo-line-regexp tr-org-todo-line-regexp)
6658 (org-todo-keywords-1 tr-org-todo-keywords-1)
6659 (org-todo-kwd-alist tr-org-todo-kwd-alist)
6660 (org-done-keywords tr-org-done-keywords)
6661 (org-todo-regexp tr-org-todo-regexp)
6662 (org-todo-line-regexp tr-org-todo-line-regexp)
6663 (org-odd-levels-only
6664 (if (local-variable-p 'org-odd-levels-only (current-buffer))
6666 tr-org-odd-levels-only)))
6667 (goto-char (point-min))
6670 (if (re-search-forward
6671 (concat "^" (regexp-quote heading)
6672 (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
6674 (goto-char (match-end 0))
6675 ;; Heading not found, just insert it at the end
6676 (goto-char (point-max))
6677 (or (bolp) (insert "\n"))
6678 (insert "\n" heading "\n")
6680 ;; Make the subtree visible
6682 (org-end-of-subtree t)
6683 (skip-chars-backward " \t\r\n")
6684 (and (looking-at "[ \t\r\n]*")
6685 (replace-match "\n\n")))
6686 ;; No specific heading, just go to end of file.
6687 (goto-char (point-max)) (insert "\n"))
6689 (org-paste-subtree (org-get-legal-level level 1))
6691 ;; Mark the entry as done
6692 (when (and org-archive-mark-done
6693 (looking-at org-todo-line-regexp)
6694 (or (not (match-end 2))
6695 (not (member (match-string 2) org-done-keywords))))
6698 (car (or (member org-archive-mark-done org-done-keywords)
6699 org-done-keywords)))))
6701 ;; Move cursor to right after the TODO keyword
6702 (when org-archive-stamp-time
6703 (org-add-planning-info 'archived (org-current-time)))
6704 ;; Save the buffer, if it is not the same buffer.
6705 (if (not (eq this-buffer buffer)) (save-buffer))))
6706 ;; Here we are back in the original buffer. Everything seems to have
6707 ;; worked. So now cut the tree and finish up.
6708 (let (this-command) (org-cut-subtree))
6709 (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
6710 (message "Subtree archived %s"
6711 (if (eq this-buffer buffer)
6712 (concat "under heading: " heading)
6713 (concat "in file: " (abbreviate-file-name file)))))))
6715 (defun org-archive-all-done (&optional tag)
6716 "Archive sublevels of the current tree without open TODO items.
6717 If the cursor is not on a headline, try all level 1 trees. If
6718 it is on a headline, try all direct children.
6719 When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
6720 (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
6721 (rea (concat ".*:" org-archive-tag ":"))
6722 (begm (make-marker))
6723 (endm (make-marker))
6724 (question (if tag "Set ARCHIVE tag (no open TODO items)? "
6725 "Move subtree to archive (no open TODO items)? "))
6726 beg end (cntarch 0))
6727 (if (org-on-heading-p)
6729 (setq re1 (concat "^" (regexp-quote
6731 (1+ (- (match-end 0) (match-beginning 0)))
6734 (move-marker begm (point))
6735 (move-marker endm (org-end-of-subtree t)))
6737 (move-marker begm (point-min))
6738 (move-marker endm (point-max)))
6741 (while (re-search-forward re1 endm t)
6742 (setq beg (match-beginning 0)
6743 end (save-excursion (org-end-of-subtree t) (point)))
6745 (if (re-search-forward re end t)
6748 (if (and (or (not tag) (not (looking-at rea)))
6749 (y-or-n-p question))
6752 (org-toggle-tag org-archive-tag 'on)
6753 (org-archive-subtree))
6754 (setq cntarch (1+ cntarch)))
6756 (message "%d trees archived" cntarch)))
6758 (defun org-cycle-hide-drawers (state)
6759 "Re-hide all drawers after a visibility state change."
6760 (when (and (org-mode-p)
6761 (not (memq state '(overview folded))))
6763 (let* ((globalp (memq state '(contents all)))
6764 (beg (if globalp (point-min) (point)))
6765 (end (if globalp (point-max) (org-end-of-subtree t))))
6767 (while (re-search-forward org-drawer-regexp end t)
6768 (org-flag-drawer t))))))
6770 (defun org-flag-drawer (flag)
6772 (beginning-of-line 1)
6773 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
6774 (let ((b (match-end 0)))
6775 (if (re-search-forward
6777 (save-excursion (outline-next-heading) (point)) t)
6778 (outline-flag-region b (point-at-eol) flag)
6779 (error ":END: line missing"))))))
6781 (defun org-cycle-hide-archived-subtrees (state)
6782 "Re-hide all archived subtrees after a visibility state change."
6783 (when (and (not org-cycle-open-archived-trees)
6784 (not (memq state '(overview folded))))
6786 (let* ((globalp (memq state '(contents all)))
6787 (beg (if globalp (point-min) (point)))
6788 (end (if globalp (point-max) (org-end-of-subtree t))))
6789 (org-hide-archived-subtrees beg end)
6791 (if (looking-at (concat ".*:" org-archive-tag ":"))
6792 (message (substitute-command-keys
6793 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
6795 (defun org-force-cycle-archived ()
6796 "Cycle subtree even if it is archived."
6798 (setq this-command 'org-cycle)
6799 (let ((org-cycle-open-archived-trees t))
6800 (call-interactively 'org-cycle)))
6802 (defun org-hide-archived-subtrees (beg end)
6803 "Re-hide all archived subtrees after a visibility state change."
6805 (let* ((re (concat ":" org-archive-tag ":")))
6807 (while (re-search-forward re end t)
6808 (and (org-on-heading-p) (hide-subtree))
6809 (org-end-of-subtree t)))))
6811 (defun org-toggle-tag (tag &optional onoff)
6812 "Toggle the tag TAG for the current line.
6813 If ONOFF is `on' or `off', don't toggle but set to this state."
6814 (unless (org-on-heading-p t) (error "Not on headling"))
6818 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
6821 (setq current (match-string 1))
6824 (setq current (nreverse (org-split-string current ":")))
6828 (or (member tag current) (push tag current)))
6830 (or (not (member tag current)) (setq current (delete tag current))))
6831 (t (if (member tag current)
6832 (setq current (delete tag current))
6834 (push tag current))))
6837 (insert " :" (mapconcat 'identity (nreverse current) ":") ":"))
6838 (org-set-tags nil t))
6841 (defun org-toggle-archive-tag (&optional arg)
6842 "Toggle the archive tag for the current headline.
6843 With prefix ARG, check all children of current headline and offer tagging
6844 the children that do not contain any open TODO items."
6847 (org-archive-all-done 'tag)
6850 (org-back-to-heading t)
6851 (setq set (org-toggle-tag org-archive-tag))
6852 (when set (hide-subtree)))
6853 (and set (beginning-of-line 1))
6854 (message "Subtree %s" (if set "archived" "unarchived")))))
6859 ;;; The table editor
6861 ;; Watch out: Here we are talking about two different kind of tables.
6862 ;; Most of the code is for the tables created with the Org-mode table editor.
6863 ;; Sometimes, we talk about tables created and edited with the table.el
6864 ;; Emacs package. We call the former org-type tables, and the latter
6865 ;; table.el-type tables.
6867 (defun org-before-change-function (beg end)
6868 "Every change indicates that a table might need an update."
6869 (setq org-table-may-need-update t))
6871 (defconst org-table-line-regexp "^[ \t]*|"
6872 "Detects an org-type table line.")
6873 (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
6874 "Detects an org-type table line.")
6875 (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
6876 "Detects a table line marked for automatic recalculation.")
6877 (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
6878 "Detects a table line marked for automatic recalculation.")
6879 (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
6880 "Detects a table line marked for automatic recalculation.")
6881 (defconst org-table-hline-regexp "^[ \t]*|-"
6882 "Detects an org-type table hline.")
6883 (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
6884 "Detects a table-type table hline.")
6885 (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
6886 "Detects an org-type or table-type table.")
6887 (defconst org-table-border-regexp "^[ \t]*[^| \t]"
6888 "Searching from within a table (any type) this finds the first line
6889 outside the table.")
6890 (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
6891 "Searching from within a table (any type) this finds the first line
6892 outside the table.")
6894 (defvar org-table-last-highlighted-reference nil)
6895 (defvar org-table-formula-history nil)
6897 (defvar org-table-column-names nil
6898 "Alist with column names, derived from the `!' line.")
6899 (defvar org-table-column-name-regexp nil
6900 "Regular expression matching the current column names.")
6901 (defvar org-table-local-parameters nil
6902 "Alist with parameter names, derived from the `$' line.")
6903 (defvar org-table-named-field-locations nil
6904 "Alist with locations of named fields.")
6906 (defvar org-table-current-line-types nil
6907 "Table row types, non-nil only for the duration of a comand.")
6908 (defvar org-table-current-begin-line nil
6909 "Table begin line, non-nil only for the duration of a comand.")
6910 (defvar org-table-current-begin-pos nil
6911 "Table begin position, non-nil only for the duration of a comand.")
6912 (defvar org-table-dlines nil
6913 "Vector of data line line numbers in the current table.")
6914 (defvar org-table-hlines nil
6915 "Vector of hline line numbers in the current table.")
6917 (defconst org-table-range-regexp
6918 "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
6920 "Regular expression for matching ranges in formulas.")
6922 (defconst org-table-range-regexp2
6924 "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
6926 "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
6927 "Match a range for reference display.")
6929 (defconst org-table-translate-regexp
6930 (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
6931 "Match a reference that needs translation, for reference display.")
6933 (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
6935 (defun org-table-create-with-table.el ()
6936 "Use the table.el package to insert a new table.
6937 If there is already a table at point, convert between Org-mode tables
6938 and table.el tables."
6942 ((org-at-table.el-p)
6943 (if (y-or-n-p "Convert table to Org-mode table? ")
6944 (org-table-convert)))
6946 (if (y-or-n-p "Convert table to table.el table? ")
6947 (org-table-convert)))
6948 (t (call-interactively 'table-insert))))
6950 (defun org-table-create-or-convert-from-region (arg)
6951 "Convert region to table, or create an empty table.
6952 If there is an active region, convert it to a table, using the function
6953 `org-table-convert-region'.
6954 If there is no such region, create an empty table with `org-table-create'."
6956 (if (org-region-active-p)
6957 (org-table-convert-region (region-beginning) (region-end) arg)
6958 (org-table-create arg)))
6960 (defun org-table-create (&optional size)
6961 "Query for a size and insert a table skeleton.
6962 SIZE is a string Columns x Rows like for example \"3x2\"."
6965 (setq size (read-string
6966 (concat "Table size Columns x Rows [e.g. "
6967 org-table-default-size "]: ")
6968 "" nil org-table-default-size)))
6970 (let* ((pos (point))
6971 (indent (make-string (current-column) ?\ ))
6972 (split (org-split-string size " *x *"))
6973 (rows (string-to-number (nth 1 split)))
6974 (columns (string-to-number (car split)))
6975 (line (concat (apply 'concat indent "|" (make-list columns " |"))
6977 (if (string-match "^[ \t]*$" (buffer-substring-no-properties
6978 (point-at-bol) (point)))
6979 (beginning-of-line 1)
6981 ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
6982 (dotimes (i rows) (insert line))
6985 ;; Insert a hline after the first row.
6992 (defun org-table-convert-region (beg0 end0 &optional nspace)
6993 "Convert region to a table.
6994 The region goes from BEG0 to END0, but these borders will be moved
6995 slightly, to make sure a beginning of line in the first line is included.
6996 When NSPACE is non-nil, it indicates the minimum number of spaces that
6997 separate columns. By default, the function first checks if every line
6998 contains at lease one TAB. If yes, it assumes that the material is TAB
6999 separated. If not, it assumes a single space as separator."
7001 (let* ((beg (min beg0 end0))
7002 (end (max beg0 end0))
7006 (beginning-of-line 1)
7007 (setq beg (move-marker (make-marker) (point)))
7009 (if (bolp) (backward-char 1) (end-of-line 1))
7010 (setq end (move-marker (make-marker) (point)))
7011 ;; Lets see if this is tab-separated material. If every nonempty line
7012 ;; contains a tab, we will assume that it is tab-separated material
7016 (and (re-search-forward "^[^\n\t]+$" end t) (setq tabsep nil)))
7017 (if nspace (setq tabsep nil))
7020 (setq re (format "^ *\\| *\t *\\| \\{%d,\\}"
7021 (max 1 (prefix-numeric-value nspace)))))
7023 (while (re-search-forward re end t)
7024 (replace-match "| " t t))
7029 (defun org-table-import (file arg)
7030 "Import FILE as a table.
7031 The file is assumed to be tab-separated. Such files can be produced by most
7032 spreadsheet and database applications. If no tabs (at least one per line)
7033 are found, lines will be split on whitespace into fields."
7034 (interactive "f\nP")
7035 (or (bolp) (newline))
7038 (insert-file-contents file)
7039 (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
7041 (defun org-table-export ()
7042 "Export table as a tab-separated file.
7043 Such a file can be imported into a spreadsheet program like Excel."
7045 (let* ((beg (org-table-begin))
7046 (end (org-table-end))
7047 (table (buffer-substring beg end))
7048 (file (read-file-name "Export table to: "))
7050 (unless (or (not (file-exists-p file))
7051 (y-or-n-p (format "Overwrite file %s? " file)))
7053 (with-current-buffer (find-file-noselect file)
7054 (setq buf (current-buffer))
7058 (goto-char (point-min))
7059 (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
7060 (replace-match "" t t)
7062 (goto-char (point-min))
7063 (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
7064 (replace-match "" t t)
7065 (goto-char (min (1+ (point)) (point-max))))
7066 (goto-char (point-min))
7067 (while (re-search-forward "^-[-+]*$" nil t)
7069 (if (looking-at "\n")
7071 (goto-char (point-min))
7072 (while (re-search-forward "[ \t]*|[ \t]*" nil t)
7073 (replace-match "\t" t t))
7077 (defvar org-table-aligned-begin-marker (make-marker)
7078 "Marker at the beginning of the table last aligned.
7079 Used to check if cursor still is in that table, to minimize realignment.")
7080 (defvar org-table-aligned-end-marker (make-marker)
7081 "Marker at the end of the table last aligned.
7082 Used to check if cursor still is in that table, to minimize realignment.")
7083 (defvar org-table-last-alignment nil
7084 "List of flags for flushright alignment, from the last re-alignment.
7085 This is being used to correctly align a single field after TAB or RET.")
7086 (defvar org-table-last-column-widths nil
7087 "List of max width of fields in each column.
7088 This is being used to correctly align a single field after TAB or RET.")
7089 (defvar org-table-overlay-coordinates nil
7090 "Overlay coordinates after each align of a table.")
7091 (make-variable-buffer-local 'org-table-overlay-coordinates)
7093 (defvar org-last-recalc-line nil)
7094 (defconst org-narrow-column-arrow "=>"
7095 "Used as display property in narrowed table columns.")
7097 (defun org-table-align ()
7098 "Align the table at point by aligning all vertical bars."
7102 (beg (org-table-begin))
7103 (end (org-table-end))
7104 ;; Current cursor position
7105 (linepos (org-current-line))
7106 (colpos (org-table-current-column))
7107 (winstart (window-start))
7108 (winstartline (org-current-line (min winstart (1- (point-max)))))
7109 lines (new "") lengths l typenums ty fields maxfields i
7111 (indent "") cnt frac
7117 (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
7119 (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
7120 emptystrings links dates narrow fmax f1 len c e)
7122 (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
7123 ;; Check if we have links or dates
7125 (setq links (re-search-forward org-bracket-link-regexp end t))
7127 (setq dates (and org-display-custom-times
7128 (re-search-forward org-ts-regexp-both end t)))
7129 ;; Make sure the link properties are right
7130 (when links (goto-char beg) (while (org-activate-bracket-links end)))
7131 ;; Make sure the date properties are right
7132 (when dates (goto-char beg) (while (org-activate-dates end)))
7134 ;; Check if we are narrowing any columns
7136 (setq narrow (and org-format-transports-properties-p
7137 (re-search-forward "<[0-9]+>" end t)))
7139 (setq lines (org-split-string
7140 (buffer-substring beg end) "\n"))
7141 ;; Store the indentation of the first line
7142 (if (string-match "^ *" (car lines))
7143 (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
7144 ;; Mark the hlines by setting the corresponding element to nil
7145 ;; At the same time, we remove trailing space.
7146 (setq lines (mapcar (lambda (l)
7147 (if (string-match "^ *|-" l)
7149 (if (string-match "[ \t]+$" l)
7150 (substring l 0 (match-beginning 0))
7153 ;; Get the data fields by splitting the lines.
7154 (setq fields (mapcar
7156 (org-split-string l " *| *"))
7157 (delq nil (copy-sequence lines))))
7158 ;; How many fields in the longest line?
7160 (setq maxfields (apply 'max (mapcar 'length fields)))
7162 (kill-region beg end)
7163 (org-table-create org-table-default-size)
7164 (error "Empty table - created default table")))
7165 ;; A list of empty strings to fill any short rows on output
7166 (setq emptystrings (make-list maxfields ""))
7167 ;; Check for special formatting.
7169 (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
7170 (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
7171 ;; Check if there is an explicit width specified
7173 (setq c column fmax nil)
7176 (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
7177 (setq fmax (string-to-number (match-string 1 e)) c nil)))
7178 ;; Find fields that are wider than fmax, and shorten them
7180 (loop for xx in column do
7181 (when (and (stringp xx)
7182 (> (org-string-width xx) fmax))
7183 (org-add-props xx nil
7185 (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
7186 (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
7188 (error "Cannot narrow field starting with wide link \"%s\""
7189 (match-string 0 xx)))
7190 (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
7191 (add-text-properties (- f1 2) f1
7192 (list 'display org-narrow-column-arrow)
7194 ;; Get the maximum width for each column
7195 (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
7196 ;; Get the fraction of numbers, to decide about alignment of the column
7197 (setq cnt 0 frac 0.0)
7198 (loop for x in column do
7201 (setq frac ( / (+ (* frac cnt)
7202 (if (string-match org-table-number-regexp x) 1 0))
7203 (setq cnt (1+ cnt))))))
7204 (push (>= frac org-table-number-fraction) typenums))
7205 (setq lengths (nreverse lengths) typenums (nreverse typenums))
7207 ;; Store the alignment of this table, for later editing of single fields
7208 (setq org-table-last-alignment typenums
7209 org-table-last-column-widths lengths)
7211 ;; With invisible characters, `format' does not get the field width right
7212 ;; So we need to make these fields wide by hand.
7214 (loop for i from 0 upto (1- maxfields) do
7215 (setq len (nth i lengths))
7216 (loop for j from 0 upto (1- (length fields)) do
7217 (setq c (nthcdr i (car (nthcdr j fields))))
7218 (if (and (stringp (car c))
7219 (string-match org-bracket-link-regexp (car c))
7220 (< (org-string-width (car c)) len))
7221 (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
7223 ;; Compute the formats needed for output of the table
7224 (setq rfmt (concat indent "|") hfmt (concat indent "|"))
7225 (while (setq l (pop lengths))
7226 (setq ty (if (pop typenums) "" "-")) ; number types flushright
7227 (setq rfmt (concat rfmt (format rfmt1 ty l))
7228 hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
7229 (setq rfmt (concat rfmt "\n")
7230 hfmt (concat (substring hfmt 0 -1) "|\n"))
7232 (setq new (mapconcat
7234 (if l (apply 'format rfmt
7235 (append (pop fields) emptystrings))
7238 ;; Replace the old one
7239 (delete-region beg end)
7240 (move-marker end nil)
7241 (move-marker org-table-aligned-begin-marker (point))
7243 (move-marker org-table-aligned-end-marker (point))
7244 (when (and orgtbl-mode (not (org-mode-p)))
7245 (goto-char org-table-aligned-begin-marker)
7246 (while (org-hide-wide-columns org-table-aligned-end-marker)))
7247 ;; Try to move to the old location
7248 (goto-line winstartline)
7249 (setq winstart (point-at-bol))
7251 (set-window-start (selected-window) winstart 'noforce)
7252 (org-table-goto-column colpos)
7253 (and org-table-overlay-coordinates (org-table-overlay-coordinates))
7254 (setq org-table-may-need-update nil)
7257 (defun org-string-width (s)
7258 "Compute width of string, ignoring invisible characters.
7259 This ignores character with invisibility property `org-link', and also
7260 characters with property `org-cwidth', because these will become invisible
7261 upon the next fontification round."
7263 (when (or (eq t buffer-invisibility-spec)
7264 (assq 'org-link buffer-invisibility-spec))
7265 (while (setq b (text-property-any 0 (length s)
7266 'invisible 'org-link s))
7267 (setq s (concat (substring s 0 b)
7268 (substring s (or (next-single-property-change
7269 b 'invisible s) (length s)))))))
7270 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
7271 (setq s (concat (substring s 0 b)
7272 (substring s (or (next-single-property-change
7273 b 'org-cwidth s) (length s))))))
7274 (setq l (string-width s) b -1)
7275 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
7276 (setq l (- l (get-text-property b 'org-dwidth-n s))))
7279 (defun org-table-begin (&optional table-type)
7280 "Find the beginning of the table and return its position.
7281 With argument TABLE-TYPE, go to the beginning of a table.el-type table."
7283 (if (not (re-search-backward
7284 (if table-type org-table-any-border-regexp
7285 org-table-border-regexp)
7287 (progn (goto-char (point-min)) (point))
7288 (goto-char (match-beginning 0))
7289 (beginning-of-line 2)
7292 (defun org-table-end (&optional table-type)
7293 "Find the end of the table and return its position.
7294 With argument TABLE-TYPE, go to the end of a table.el-type table."
7296 (if (not (re-search-forward
7297 (if table-type org-table-any-border-regexp
7298 org-table-border-regexp)
7300 (goto-char (point-max))
7301 (goto-char (match-beginning 0)))
7304 (defun org-table-justify-field-maybe (&optional new)
7305 "Justify the current field, text to left, number to right.
7306 Optional argument NEW may specify text to replace the current field content."
7308 ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
7309 ((org-at-table-hline-p))
7311 (or (not (equal (marker-buffer org-table-aligned-begin-marker)
7313 (< (point) org-table-aligned-begin-marker)
7314 (>= (point) org-table-aligned-end-marker)))
7315 ;; This is not the same table, force a full re-align
7316 (setq org-table-may-need-update t))
7317 (t ;; realign the current field, based on previous full realign
7318 (let* ((pos (point)) s
7319 (col (org-table-current-column))
7320 (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
7323 (skip-chars-backward "^|\n")
7324 (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
7326 (setq s (match-string 1)
7328 l (max 1 (- (match-end 0) (match-beginning 0) 3))
7329 e (not (= (match-beginning 2) (match-end 2))))
7330 (setq f (format (if num " %%%ds %s" " %%-%ds %s")
7331 l (if e "|" (setq org-table-may-need-update t) ""))
7334 (if (<= (length new) l) ;; FIXME: length -> str-width?
7335 (setq n (format f new))
7336 (setq n (concat new "|") org-table-may-need-update t)))
7338 (let (org-table-may-need-update)
7339 (replace-match n t t))))
7340 (setq org-table-may-need-update t))
7341 (goto-char pos))))))
7343 (defun org-table-next-field ()
7344 "Go to the next field in the current table, creating new lines as needed.
7345 Before doing so, re-align the table if necessary."
7347 (org-table-maybe-eval-formula)
7348 (org-table-maybe-recalculate-line)
7349 (if (and org-table-automatic-realign
7350 org-table-may-need-update)
7352 (let ((end (org-table-end)))
7353 (if (org-at-table-hline-p)
7357 (re-search-forward "|" end)
7358 (if (looking-at "[ \t]*$")
7359 (re-search-forward "|" end))
7360 (if (and (looking-at "-")
7361 org-table-tab-jumps-over-hlines
7362 (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
7363 (goto-char (match-beginning 1)))
7364 (if (looking-at "-")
7366 (beginning-of-line 0)
7367 (org-table-insert-row 'below))
7368 (if (looking-at " ") (forward-char 1))))
7370 (org-table-insert-row 'below)))))
7372 (defun org-table-previous-field ()
7373 "Go to the previous field in the table.
7374 Before doing so, re-align the table if necessary."
7376 (org-table-justify-field-maybe)
7377 (org-table-maybe-recalculate-line)
7378 (if (and org-table-automatic-realign
7379 org-table-may-need-update)
7381 (if (org-at-table-hline-p)
7383 (re-search-backward "|" (org-table-begin))
7384 (re-search-backward "|" (org-table-begin))
7385 (while (looking-at "|\\(-\\|[ \t]*$\\)")
7386 (re-search-backward "|" (org-table-begin)))
7387 (if (looking-at "| ?")
7388 (goto-char (match-end 0))))
7390 (defun org-table-next-row ()
7391 "Go to the next row (same column) in the current table.
7392 Before doing so, re-align the table if necessary."
7394 (org-table-maybe-eval-formula)
7395 (org-table-maybe-recalculate-line)
7396 (if (or (looking-at "[ \t]*$")
7397 (save-excursion (skip-chars-backward " \t") (bolp)))
7399 (if (and org-table-automatic-realign
7400 org-table-may-need-update)
7402 (let ((col (org-table-current-column)))
7403 (beginning-of-line 2)
7404 (if (or (not (org-at-table-p))
7405 (org-at-table-hline-p))
7407 (beginning-of-line 0)
7408 (org-table-insert-row 'below)))
7409 (org-table-goto-column col)
7410 (skip-chars-backward "^|\n\r")
7411 (if (looking-at " ") (forward-char 1)))))
7413 (defun org-table-copy-down (n)
7414 "Copy a field down in the current column.
7415 If the field at the cursor is empty, copy into it the content of the nearest
7416 non-empty field above. With argument N, use the Nth non-empty field.
7417 If the current field is not empty, it is copied down to the next row, and
7418 the cursor is moved with it. Therefore, repeating this command causes the
7419 column to be filled row-by-row.
7420 If the variable `org-table-copy-increment' is non-nil and the field is an
7421 integer or a timestamp, it will be incremented while copying. In the case of
7422 a timestamp, if the cursor is on the year, change the year. If it is on the
7423 month or the day, change that. Point will stay on the current date field
7424 in order to easily repeat the interval."
7426 (let* ((colpos (org-table-current-column))
7427 (col (current-column))
7428 (field (org-table-get-field))
7429 (non-empty (string-match "[^ \t]" field))
7430 (beg (org-table-begin))
7432 (org-table-check-inside-data-field)
7435 (setq txt (org-trim field))
7436 (org-table-next-row)
7437 (org-table-blank-field))
7441 (while (progn (beginning-of-line 1)
7442 (re-search-backward org-table-dataline-regexp
7444 (org-table-goto-column colpos t)
7445 (if (and (looking-at
7446 "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
7447 (= (setq n (1- n)) 0))
7448 (throw 'exit (match-string 1))))))))
7451 (if (and org-table-copy-increment
7452 (string-match "^[0-9]+$" txt))
7453 (setq txt (format "%d" (+ (string-to-number txt) 1))))
7455 (move-to-column col)
7456 (if (and org-table-copy-increment (org-at-timestamp-p t))
7457 (org-timestamp-up 1)
7458 (org-table-maybe-recalculate-line))
7460 (move-to-column col))
7461 (error "No non-empty field found"))))
7463 (defun org-table-check-inside-data-field ()
7464 "Is point inside a table data field?
7465 I.e. not on a hline or before the first or after the last column?
7466 This actually throws an error, so it aborts the current command."
7467 (if (or (not (org-at-table-p))
7468 (= (org-table-current-column) 0)
7469 (org-at-table-hline-p)
7470 (looking-at "[ \t]*$"))
7471 (error "Not in table data field")))
7473 (defvar org-table-clip nil
7474 "Clipboard for table regions.")
7476 (defun org-table-blank-field ()
7477 "Blank the current table field or active region."
7479 (org-table-check-inside-data-field)
7480 (if (and (interactive-p) (org-region-active-p))
7481 (let (org-table-clip)
7482 (org-table-cut-region (region-beginning) (region-end)))
7483 (skip-chars-backward "^|")
7485 (if (looking-at "|[^|\n]+")
7486 (let* ((pos (match-beginning 0))
7487 (match (match-string 0))
7488 (len (org-string-width match)))
7489 (replace-match (concat "|" (make-string (1- len) ?\ )))
7490 (goto-char (+ 2 pos))
7491 (substring match 1)))))
7493 (defun org-table-get-field (&optional n replace)
7494 "Return the value of the field in column N of current row.
7495 N defaults to current field.
7496 If REPLACE is a string, replace field with this value. The return value
7497 is always the old value."
7498 (and n (org-table-goto-column n))
7499 (skip-chars-backward "^|\n")
7501 (if (looking-at "|[^|\r\n]*")
7502 (let* ((pos (match-beginning 0))
7503 (val (buffer-substring (1+ pos) (match-end 0))))
7505 (replace-match (concat "|" replace) t t))
7506 (goto-char (min (point-at-eol) (+ 2 pos)))
7508 (forward-char 1) ""))
7510 (defun org-table-field-info (arg)
7511 "Show info about the current field, and highlight any reference at point."
7513 (org-table-get-specials)
7515 (let* ((pos (point))
7516 (col (org-table-current-column))
7517 (cname (car (rassoc (int-to-string col) org-table-column-names)))
7518 (name (car (rassoc (list (org-current-line) col)
7519 org-table-named-field-locations)))
7520 (eql (org-table-get-stored-formulas))
7521 (dline (org-table-current-dline))
7522 (ref (format "@%d$%d" dline col))
7523 (ref1 (org-table-convert-refs-to-an ref))
7524 (fequation (or (assoc name eql) (assoc ref eql)))
7525 (cequation (assoc (int-to-string col) eql))
7526 (eqn (or fequation cequation)))
7529 (org-table-show-reference 'local)
7531 (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
7533 (if cname (concat " or $" cname) "")
7535 (if name (concat " or $" name) "")
7536 ;; FIXME: formula info not correct if special table line
7538 (concat ", formula: "
7539 (org-table-formula-to-user
7541 (if (string-match "^[$@]"(car eqn)) "" "$")
7542 (car eqn) "=" (cdr eqn))))
7545 (defun org-table-current-column ()
7546 "Find out which column we are in.
7547 When called interactively, column is also displayed in echo area."
7549 (if (interactive-p) (org-table-check-inside-data-field))
7551 (let ((cnt 0) (pos (point)))
7552 (beginning-of-line 1)
7553 (while (search-forward "|" pos t)
7554 (setq cnt (1+ cnt)))
7555 (if (interactive-p) (message "This is table column %d" cnt))
7558 (defun org-table-current-dline ()
7559 "Find out what table data line we are in.
7560 Only datalins count for this."
7562 (if (interactive-p) (org-table-check-inside-data-field))
7564 (let ((cnt 0) (pos (point)))
7565 (goto-char (org-table-begin))
7566 (while (<= (point) pos)
7567 (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
7568 (beginning-of-line 2))
7569 (if (interactive-p) (message "This is table line %d" cnt))
7572 (defun org-table-goto-column (n &optional on-delim force)
7573 "Move the cursor to the Nth column in the current table line.
7574 With optional argument ON-DELIM, stop with point before the left delimiter
7576 If there are less than N fields, just go to after the last delimiter.
7577 However, when FORCE is non-nil, create new columns if necessary."
7579 (let ((pos (point-at-eol)))
7580 (beginning-of-line 1)
7582 (while (and (> (setq n (1- n)) -1)
7583 (or (search-forward "|" pos t)
7585 (progn (end-of-line 1)
7586 (skip-chars-backward "^|")
7588 ; (backward-char 2) t)))))
7589 (when (and force (not (looking-at ".*|")))
7590 (save-excursion (end-of-line 1) (insert " | ")))
7593 (if (looking-at " ") (forward-char 1))))))
7595 (defun org-at-table-p (&optional table-type)
7596 "Return t if the cursor is inside an org-type table.
7597 If TABLE-TYPE is non-nil, also check for table.el-type tables."
7598 (if org-enable-table-editor
7600 (beginning-of-line 1)
7601 (looking-at (if table-type org-table-any-line-regexp
7602 org-table-line-regexp)))
7605 (defun org-at-table.el-p ()
7606 "Return t if and only if we are at a table.el table."
7607 (and (org-at-table-p 'any)
7609 (goto-char (org-table-begin 'any))
7610 (looking-at org-table1-hline-regexp))))
7612 (defun org-table-recognize-table.el ()
7613 "If there is a table.el table nearby, recognize it and move into it."
7614 (if org-table-tab-recognizes-table.el
7615 (if (org-at-table.el-p)
7617 (beginning-of-line 1)
7618 (if (looking-at org-table-dataline-regexp)
7620 (if (looking-at org-table1-hline-regexp)
7622 (beginning-of-line 2)
7623 (if (looking-at org-table-any-border-regexp)
7624 (beginning-of-line -1)))))
7625 (if (re-search-forward "|" (org-table-end t) t)
7628 (if (table--at-cell-p (point))
7630 (message "recognizing table.el table...")
7631 (table-recognize-table)
7632 (message "recognizing table.el table...done")))
7633 (error "This should not happen..."))
7638 (defun org-at-table-hline-p ()
7639 "Return t if the cursor is inside a hline in a table."
7640 (if org-enable-table-editor
7642 (beginning-of-line 1)
7643 (looking-at org-table-hline-regexp))
7646 (defun org-table-insert-column ()
7647 "Insert a new column into the table."
7649 (if (not (org-at-table-p))
7650 (error "Not at a table"))
7651 (org-table-find-dataline)
7652 (let* ((col (max 1 (org-table-current-column)))
7653 (beg (org-table-begin))
7654 (end (org-table-end))
7655 ;; Current cursor position
7656 (linepos (org-current-line))
7659 (while (< (point) end)
7660 (if (org-at-table-hline-p)
7662 (org-table-goto-column col t)
7664 (beginning-of-line 2))
7665 (move-marker end nil)
7667 (org-table-goto-column colpos)
7669 (org-table-fix-formulas "$" nil (1- col) 1)))
7671 (defun org-table-find-dataline ()
7672 "Find a dataline in the current table, which is needed for column commands."
7673 (if (and (org-at-table-p)
7674 (not (org-at-table-hline-p)))
7676 (let ((col (current-column))
7677 (end (org-table-end)))
7678 (move-to-column col)
7679 (while (and (< (point) end)
7680 (or (not (= (current-column) col))
7681 (org-at-table-hline-p)))
7682 (beginning-of-line 2)
7683 (move-to-column col))
7684 (if (and (org-at-table-p)
7685 (not (org-at-table-hline-p)))
7688 "Please position cursor in a data line for column operations")))))
7690 (defun org-table-delete-column ()
7691 "Delete a column from the table."
7693 (if (not (org-at-table-p))
7694 (error "Not at a table"))
7695 (org-table-find-dataline)
7696 (org-table-check-inside-data-field)
7697 (let* ((col (org-table-current-column))
7698 (beg (org-table-begin))
7699 (end (org-table-end))
7700 ;; Current cursor position
7701 (linepos (org-current-line))
7704 (while (< (point) end)
7705 (if (org-at-table-hline-p)
7707 (org-table-goto-column col t)
7708 (and (looking-at "|[^|\n]+|")
7709 (replace-match "|")))
7710 (beginning-of-line 2))
7711 (move-marker end nil)
7713 (org-table-goto-column colpos)
7715 (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
7718 (defun org-table-move-column-right ()
7719 "Move column to the right."
7721 (org-table-move-column nil))
7722 (defun org-table-move-column-left ()
7723 "Move column to the left."
7725 (org-table-move-column 'left))
7727 (defun org-table-move-column (&optional left)
7728 "Move the current column to the right. With arg LEFT, move to the left."
7730 (if (not (org-at-table-p))
7731 (error "Not at a table"))
7732 (org-table-find-dataline)
7733 (org-table-check-inside-data-field)
7734 (let* ((col (org-table-current-column))
7735 (col1 (if left (1- col) col))
7736 (beg (org-table-begin))
7737 (end (org-table-end))
7738 ;; Current cursor position
7739 (linepos (org-current-line))
7740 (colpos (if left (1- col) (1+ col))))
7741 (if (and left (= col 1))
7742 (error "Cannot move column further left"))
7743 (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
7744 (error "Cannot move column further right"))
7746 (while (< (point) end)
7747 (if (org-at-table-hline-p)
7749 (org-table-goto-column col1 t)
7750 (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
7751 (replace-match "|\\2|\\1|")))
7752 (beginning-of-line 2))
7753 (move-marker end nil)
7755 (org-table-goto-column colpos)
7757 (org-table-fix-formulas
7758 "$" (list (cons (number-to-string col) (number-to-string colpos))
7759 (cons (number-to-string colpos) (number-to-string col))))))
7761 (defun org-table-move-row-down ()
7762 "Move table row down."
7764 (org-table-move-row nil))
7765 (defun org-table-move-row-up ()
7766 "Move table row up."
7768 (org-table-move-row 'up))
7770 (defun org-table-move-row (&optional up)
7771 "Move the current table line down. With arg UP, move it up."
7773 (let* ((col (current-column))
7775 (hline1p (save-excursion (beginning-of-line 1)
7776 (looking-at org-table-hline-regexp)))
7777 (dline1 (org-table-current-dline))
7778 (dline2 (+ dline1 (if up -1 1)))
7781 (beginning-of-line tonew)
7782 (unless (org-at-table-p)
7784 (error "Cannot move row further"))
7785 (setq hline2p (looking-at org-table-hline-regexp))
7787 (beginning-of-line 1)
7789 (setq txt (buffer-substring (point) (1+ (point-at-eol))))
7790 (delete-region (point) (1+ (point-at-eol)))
7791 (beginning-of-line tonew)
7793 (beginning-of-line 0)
7794 (move-to-column col)
7795 (unless (or hline1p hline2p)
7796 (org-table-fix-formulas
7797 "@" (list (cons (number-to-string dline1) (number-to-string dline2))
7798 (cons (number-to-string dline2) (number-to-string dline1)))))))
7800 (defun org-table-insert-row (&optional arg)
7801 "Insert a new row above the current line into the table.
7802 With prefix ARG, insert below the current line."
7804 (if (not (org-at-table-p))
7805 (error "Not at a table"))
7806 (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
7807 (new (org-table-clean-line line)))
7808 ;; Fix the first field if necessary
7809 (if (string-match "^[ \t]*| *[#$] *|" line)
7810 (setq new (replace-match (match-string 0 line) t t new)))
7811 (beginning-of-line (if arg 2 1))
7812 (let (org-table-may-need-update) (insert-before-markers new "\n"))
7813 (beginning-of-line 0)
7814 (re-search-forward "| ?" (point-at-eol) t)
7815 (and (or org-table-may-need-update org-table-overlay-coordinates)
7817 (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
7819 (defun org-table-insert-hline (&optional above)
7820 "Insert a horizontal-line below the current line into the table.
7821 With prefix ABOVE, insert above the current line."
7823 (if (not (org-at-table-p))
7824 (error "Not at a table"))
7825 (let ((line (org-table-clean-line
7826 (buffer-substring (point-at-bol) (point-at-eol))))
7827 (col (current-column)))
7828 (while (string-match "|\\( +\\)|" line)
7829 (setq line (replace-match
7830 (concat "+" (make-string (- (match-end 1) (match-beginning 1))
7831 ?-) "|") t t line)))
7832 (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
7833 (beginning-of-line (if above 1 2))
7835 (beginning-of-line (if above 1 -1))
7836 (move-to-column col)
7837 (and org-table-overlay-coordinates (org-table-align))))
7839 (defun org-table-hline-and-move (&optional same-column)
7840 "Insert a hline and move to the row below that line."
7842 (let ((col (org-table-current-column)))
7843 (org-table-maybe-eval-formula)
7844 (org-table-maybe-recalculate-line)
7845 (org-table-insert-hline)
7847 (if (looking-at "\n[ \t]*|-")
7848 (progn (insert "\n|") (org-table-align))
7849 (org-table-next-field))
7850 (if same-column (org-table-goto-column col))))
7852 (defun org-table-clean-line (s)
7853 "Convert a table line S into a string with only \"|\" and space.
7854 In particular, this does handle wide and invisible characters."
7855 (if (string-match "^[ \t]*|-" s)
7856 ;; It's a hline, just map the characters
7857 (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
7858 (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
7859 (setq s (replace-match
7860 (concat "|" (make-string (org-string-width (match-string 1 s))
7865 (defun org-table-kill-row ()
7866 "Delete the current row or horizontal line from the table."
7868 (if (not (org-at-table-p))
7869 (error "Not at a table"))
7870 (let ((col (current-column))
7871 (dline (org-table-current-dline)))
7872 (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
7873 (if (not (org-at-table-p)) (beginning-of-line 0))
7874 (move-to-column col)
7875 (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
7879 (defun org-table-sort-lines (with-case &optional sorting-type)
7880 "Sort table lines according to the column at point.
7882 The position of point indicates the column to be used for
7883 sorting, and the range of lines is the range between the nearest
7884 horizontal separator lines, or the entire table of no such lines
7885 exist. If point is before the first column, you will be prompted
7886 for the sorting column. If there is an active region, the mark
7887 specifies the first line and the sorting column, while point
7888 should be in the last line to be included into the sorting.
7890 The command then prompts for the sorting type which can be
7891 alphabetically, numerically, or by time (as given in a time stamp
7892 in the field). Sorting in reverse order is also possible.
7894 With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
7896 If SORTING-TYPE is specified when this function is called from a Lisp
7897 program, no prompting will take place. SORTING-TYPE must be a character,
7898 any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
7899 should be done in reverse order."
7901 (let* ((thisline (org-current-line))
7902 (thiscol (org-table-current-column))
7903 beg end bcol ecol tend tbeg column lns pos)
7904 (when (equal thiscol 0)
7908 (read-string "Use column N for sorting: ")))
7910 (org-table-goto-column thiscol))
7911 (org-table-check-inside-data-field)
7912 (if (org-region-active-p)
7914 (setq beg (region-beginning) end (region-end))
7916 (setq column (org-table-current-column)
7919 (setq end (point-at-bol 2)))
7920 (setq column (org-table-current-column)
7922 tbeg (org-table-begin)
7923 tend (org-table-end))
7924 (if (re-search-backward org-table-hline-regexp tbeg t)
7925 (setq beg (point-at-bol 2))
7927 (setq beg (point-at-bol 1)))
7929 (if (re-search-forward org-table-hline-regexp tend t)
7930 (setq end (point-at-bol 1))
7932 (setq end (point-at-bol))))
7933 (setq beg (move-marker (make-marker) beg)
7934 end (move-marker (make-marker) end))
7937 (org-table-goto-column column)
7938 (skip-chars-backward "^|")
7939 (setq bcol (current-column))
7940 (org-table-goto-column (1+ column))
7941 (skip-chars-backward "^|")
7942 (setq ecol (1- (current-column)))
7943 (org-table-goto-column column)
7944 (setq lns (mapcar (lambda(x) (cons (org-trim (substring x bcol ecol)) x))
7945 (org-split-string (buffer-substring beg end) "\n")))
7946 (setq lns (org-do-sort lns "Table" with-case sorting-type))
7947 (delete-region beg end)
7948 (move-marker beg nil)
7949 (move-marker end nil)
7950 (insert (mapconcat 'cdr lns "\n") "\n")
7951 (goto-line thisline)
7952 (org-table-goto-column thiscol)
7953 (message "%d lines sorted, based on column %d" (length lns) column)))
7955 (defun org-table-cut-region (beg end)
7956 "Copy region in table to the clipboard and blank all relevant fields."
7958 (org-table-copy-region beg end 'cut))
7960 (defun org-table-copy-region (beg end &optional cut)
7961 "Copy rectangular region in table to clipboard.
7962 A special clipboard is used which can only be accessed
7963 with `org-table-paste-rectangle'."
7965 (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
7967 (rpl (if cut " " nil)))
7969 (org-table-check-inside-data-field)
7970 (setq l01 (org-current-line)
7971 c01 (org-table-current-column))
7973 (org-table-check-inside-data-field)
7974 (setq l02 (org-current-line)
7975 c02 (org-table-current-column))
7976 (setq l1 (min l01 l02) l2 (max l01 l02)
7977 c1 (min c01 c02) c2 (max c01 c02))
7981 (if (> l1 l2) (throw 'exit t))
7983 (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
7984 (setq cols nil ic1 c1 ic2 c2)
7985 (while (< ic1 (1+ ic2))
7986 (push (org-table-get-field ic1 rpl) cols)
7987 (setq ic1 (1+ ic1)))
7988 (push (nreverse cols) region)
7989 (setq l1 (1+ l1)))))
7990 (setq org-table-clip (nreverse region))
7991 (if cut (org-table-align))
7994 (defun org-table-paste-rectangle ()
7995 "Paste a rectangular region into a table.
7996 The upper right corner ends up in the current field. All involved fields
7997 will be overwritten. If the rectangle does not fit into the present table,
7998 the table is enlarged as needed. The process ignores horizontal separator
8001 (unless (and org-table-clip (listp org-table-clip))
8002 (error "First cut/copy a region to paste!"))
8003 (org-table-check-inside-data-field)
8004 (let* ((clip org-table-clip)
8005 (line (org-current-line))
8006 (col (org-table-current-column))
8007 (org-enable-table-editor t)
8008 (org-table-automatic-realign nil)
8010 (while (setq cols (pop clip))
8011 (while (org-at-table-hline-p) (beginning-of-line 2))
8012 (if (not (org-at-table-p))
8013 (progn (end-of-line 0) (org-table-next-field)))
8015 (while (setq field (pop cols))
8016 (org-table-goto-column c nil 'force)
8017 (org-table-get-field nil field)
8019 (beginning-of-line 2))
8021 (org-table-goto-column col)
8024 (defun org-table-convert ()
8025 "Convert from `org-mode' table to table.el and back.
8026 Obviously, this only works within limits. When an Org-mode table is
8027 converted to table.el, all horizontal separator lines get lost, because
8028 table.el uses these as cell boundaries and has no notion of horizontal lines.
8029 A table.el table can be converted to an Org-mode table only if it does not
8030 do row or column spanning. Multiline cells will become multiple cells.
8031 Beware, Org-mode does not test if the table can be successfully converted - it
8032 blindly applies a recipe that works for simple tables."
8035 (if (org-at-table.el-p)
8036 ;; convert to Org-mode table
8037 (let ((beg (move-marker (make-marker) (org-table-begin t)))
8038 (end (move-marker (make-marker) (org-table-end t))))
8039 (table-unrecognize-region beg end)
8041 (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
8044 (if (org-at-table-p)
8045 ;; convert to table.el table
8046 (let ((beg (move-marker (make-marker) (org-table-begin)))
8047 (end (move-marker (make-marker) (org-table-end))))
8048 ;; first, get rid of all horizontal lines
8050 (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
8052 ;; insert a hline before first
8054 (org-table-insert-hline 'above)
8055 (beginning-of-line -1)
8056 ;; insert a hline after each line
8057 (while (progn (beginning-of-line 3) (< (point) end))
8058 (org-table-insert-hline))
8060 (setq end (move-marker end (org-table-end)))
8061 ;; replace "+" at beginning and ending of hlines
8062 (while (re-search-forward "^\\([ \t]*\\)|-" end t)
8063 (replace-match "\\1+-"))
8065 (while (re-search-forward "-|[ \t]*$" end t)
8066 (replace-match "-+"))
8069 (defun org-table-wrap-region (arg)
8070 "Wrap several fields in a column like a paragraph.
8071 This is useful if you'd like to spread the contents of a field over several
8072 lines, in order to keep the table compact.
8074 If there is an active region, and both point and mark are in the same column,
8075 the text in the column is wrapped to minimum width for the given number of
8076 lines. Generally, this makes the table more compact. A prefix ARG may be
8077 used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
8078 formats the selected text to two lines. If the region was longer than two
8079 lines, the remaining lines remain empty. A negative prefix argument reduces
8080 the current number of lines by that amount. The wrapped text is pasted back
8081 into the table. If you formatted it to more lines than it was before, fields
8082 further down in the table get overwritten - so you might need to make space in
8085 If there is no region, the current field is split at the cursor position and
8086 the text fragment to the right of the cursor is prepended to the field one
8089 If there is no region, but you specify a prefix ARG, the current field gets
8090 blank, and the content is appended to the field above."
8092 (org-table-check-inside-data-field)
8093 (if (org-region-active-p)
8094 ;; There is a region: fill as a paragraph
8095 (let* ((beg (region-beginning))
8096 (cline (save-excursion (goto-char beg) (org-current-line)))
8097 (ccol (save-excursion (goto-char beg) (org-table-current-column)))
8099 (org-table-cut-region (region-beginning) (region-end))
8100 (if (> (length (car org-table-clip)) 1)
8101 (error "Region must be limited to single column"))
8102 (setq nlines (if arg
8104 (+ (length org-table-clip) arg)
8106 (length org-table-clip)))
8107 (setq org-table-clip
8108 (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
8111 (org-table-goto-column ccol)
8112 (org-table-paste-rectangle))
8113 ;; No region, split the current field at point
8115 ;; combine with field above
8116 (let ((s (org-table-blank-field))
8117 (col (org-table-current-column)))
8118 (beginning-of-line 0)
8119 (while (org-at-table-hline-p) (beginning-of-line 0))
8120 (org-table-goto-column col)
8121 (skip-chars-forward "^|")
8122 (skip-chars-backward " ")
8123 (insert " " (org-trim s))
8126 (when (looking-at "\\([^|]+\\)+|")
8127 (let ((s (match-string 1)))
8128 (replace-match " |")
8129 (goto-char (match-beginning 0))
8130 (org-table-next-row)
8131 (insert (org-trim s) " ")
8132 (org-table-align))))))
8134 (defvar org-field-marker nil)
8136 (defun org-table-edit-field (arg)
8137 "Edit table field in a different window.
8138 This is mainly useful for fields that contain hidden parts.
8139 When called with a \\[universal-argument] prefix, just make the full field visible so that
8140 it can be edited in place."
8143 (let ((b (save-excursion (skip-chars-backward "^|") (point)))
8144 (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
8145 (remove-text-properties b e '(org-cwidth t invisible t
8146 display t intangible t))
8147 (if (and (boundp 'font-lock-mode) font-lock-mode)
8148 (font-lock-fontify-block)))
8149 (let ((pos (move-marker (make-marker) (point)))
8150 (field (org-table-get-field))
8151 (cw (current-window-configuration))
8153 (switch-to-buffer-other-window "*Org tmp*")
8155 (insert "#\n# Edit field and finish with C-c C-c\n#\n")
8156 (let ((org-inhibit-startup t)) (org-mode))
8157 (goto-char (setq p (point-max)))
8158 (insert (org-trim field))
8159 (remove-text-properties p (point-max)
8160 '(invisible t org-cwidth t display t
8163 (org-set-local 'org-finish-function 'org-table-finish-edit-field)
8164 (org-set-local 'org-window-configuration cw)
8165 (org-set-local 'org-field-marker pos)
8166 (message "Edit and finish with C-c C-c"))))
8168 (defun org-table-finish-edit-field ()
8169 "Finish editing a table data field.
8170 Remove all newline characters, insert the result into the table, realign
8171 the table and kill the editing buffer."
8172 (let ((pos org-field-marker)
8173 (cw org-window-configuration)
8174 (cb (current-buffer))
8176 (goto-char (point-min))
8177 (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
8178 (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
8179 (replace-match " "))
8180 (setq text (org-trim (buffer-string)))
8181 (set-window-configuration cw)
8183 (select-window (get-buffer-window (marker-buffer pos)))
8185 (move-marker pos nil)
8186 (org-table-check-inside-data-field)
8187 (org-table-get-field nil text)
8189 (message "New field value inserted")))
8192 "Remove whitespace at beginning and end of string."
8193 (if (string-match "^[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
8194 (if (string-match "[ \t\n\r]+$" s) (setq s (replace-match "" t t s)))
8197 (defun org-wrap (string &optional width lines)
8198 "Wrap string to either a number of lines, or a width in characters.
8199 If WIDTH is non-nil, the string is wrapped to that width, however many lines
8200 that costs. If there is a word longer than WIDTH, the text is actually
8201 wrapped to the length of that word.
8202 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
8203 many lines, whatever width that takes.
8204 The return value is a list of lines, without newlines at the end."
8205 (let* ((words (org-split-string string "[ \t\n]+"))
8206 (maxword (apply 'max (mapcar 'org-string-width words)))
8209 (org-do-wrap words (max maxword width)))
8212 (setq ll (org-do-wrap words maxword))
8213 (if (<= (length ll) lines)
8216 (while (> (length ll) lines)
8218 (setq ll (org-do-wrap words w)))
8220 (t (error "Cannot wrap this")))))
8223 (defun org-do-wrap (words width)
8224 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
8227 (setq line (pop words))
8228 (while (and words (< (+ (length line) (length (car words))) width))
8229 (setq line (concat line " " (pop words))))
8230 (setq lines (push line lines)))
8233 (defun org-split-string (string &optional separators)
8234 "Splits STRING into substrings at SEPARATORS.
8235 No empty strings are returned if there are matches at the beginning
8237 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
8241 (while (and (string-match rexp string
8243 (= start (match-beginning 0))
8244 (< start (length string)))
8246 (< (match-beginning 0) (length string)))
8248 (or (eq (match-beginning 0) 0)
8249 (and (eq (match-beginning 0) (match-end 0))
8250 (eq (match-beginning 0) start))
8252 (cons (substring string start (match-beginning 0))
8254 (setq start (match-end 0)))
8255 (or (eq start (length string))
8257 (cons (substring string start)
8261 (defun org-table-map-tables (function)
8262 "Apply FUNCTION to the start of all tables in the buffer."
8266 (goto-char (point-min))
8267 (while (re-search-forward org-table-any-line-regexp nil t)
8268 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
8269 (beginning-of-line 1)
8270 (if (looking-at org-table-line-regexp)
8271 (save-excursion (funcall function)))
8272 (re-search-forward org-table-any-border-regexp nil 1))))
8273 (message "Mapping tables: done"))
8275 (defvar org-timecnt) ; dynamically scoped parameter
8277 (defun org-table-sum (&optional beg end nlast)
8278 "Sum numbers in region of current table column.
8279 The result will be displayed in the echo area, and will be available
8280 as kill to be inserted with \\[yank].
8282 If there is an active region, it is interpreted as a rectangle and all
8283 numbers in that rectangle will be summed. If there is no active
8284 region and point is located in a table column, sum all numbers in that
8287 If at least one number looks like a time HH:MM or HH:MM:SS, all other
8288 numbers are assumed to be times as well (in decimal hours) and the
8289 numbers are added as such.
8291 If NLAST is a number, only the NLAST fields will actually be summed."
8294 (let (col (org-timecnt 0) diff h m s org-table-clip)
8296 ((and beg end)) ; beg and end given explicitly
8297 ((org-region-active-p)
8298 (setq beg (region-beginning) end (region-end)))
8300 (setq col (org-table-current-column))
8301 (goto-char (org-table-begin))
8302 (unless (re-search-forward "^[ \t]*|[^-]" nil t)
8303 (error "No table data"))
8304 (org-table-goto-column col)
8306 (goto-char (org-table-end))
8307 (unless (re-search-backward "^[ \t]*|[^-]" nil t)
8308 (error "No table data"))
8309 (org-table-goto-column col)
8310 (setq end (point))))
8311 (let* ((items (apply 'append (org-table-copy-region beg end)))
8312 (items1 (cond ((not nlast) items)
8313 ((>= nlast (length items)) items)
8314 (t (setq items (reverse items))
8315 (setcdr (nthcdr (1- nlast) items) nil)
8317 (numbers (delq nil (mapcar 'org-table-get-number-for-summing
8319 (res (apply '+ numbers))
8320 (sres (if (= org-timecnt 0)
8322 (setq diff (* 3600 res)
8323 h (floor (/ diff 3600)) diff (mod diff 3600)
8324 m (floor (/ diff 60)) diff (mod diff 60)
8326 (format "%d:%02d:%02d" h m s))))
8330 (substitute-command-keys
8331 (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
8332 (length numbers) sres))))
8335 (defun org-table-get-number-for-summing (s)
8337 (if (string-match "^ *|? *" s)
8338 (setq s (replace-match "" nil nil s)))
8339 (if (string-match " *|? *$" s)
8340 (setq s (replace-match "" nil nil s)))
8341 (setq n (string-to-number s))
8343 ((and (string-match "0" s)
8344 (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
8345 ((string-match "\\`[ \t]+\\'" s) nil)
8346 ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
8347 (let ((h (string-to-number (or (match-string 1 s) "0")))
8348 (m (string-to-number (or (match-string 2 s) "0")))
8349 (s (string-to-number (or (match-string 4 s) "0"))))
8350 (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
8351 (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
8355 (defun org-table-current-field-formula (&optional key noerror)
8356 "Return the formula active for the current field.
8357 Assumes that specials are in place.
8358 If KEY is given, return the key to this formula.
8359 Otherwise return the formula preceeded with \"=\" or \":=\"."
8360 (let* ((name (car (rassoc (list (org-current-line)
8361 (org-table-current-column))
8362 org-table-named-field-locations)))
8363 (col (org-table-current-column))
8364 (scol (int-to-string col))
8365 (ref (format "@%d$%d" (org-table-current-dline) col))
8366 (stored-list (org-table-get-stored-formulas noerror))
8367 (ass (or (assoc name stored-list)
8368 (assoc ref stored-list)
8369 (assoc scol stored-list))))
8372 (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
8375 (defun org-table-get-formula (&optional equation named)
8376 "Read a formula from the minibuffer, offer stored formula as default.
8377 When NAMED is non-nil, look for a named equation."
8378 (let* ((stored-list (org-table-get-stored-formulas))
8379 (name (car (rassoc (list (org-current-line)
8380 (org-table-current-column))
8381 org-table-named-field-locations)))
8382 (ref (format "@%d$%d" (org-table-current-dline)
8383 (org-table-current-column)))
8384 (refass (assoc ref stored-list))
8387 (int-to-string (org-table-current-column))))
8388 (dummy (and (or name refass) (not named)
8389 (not (y-or-n-p "Replace field formula with column formula? " ))
8391 (name (or name ref))
8392 (org-table-may-need-update nil)
8393 (stored (cdr (assoc scol stored-list)))
8395 ((and stored equation (string-match "^ *=? *$" equation))
8399 (t (org-table-formula-from-user
8401 (org-table-formula-to-user
8402 (format "%s formula %s%s="
8403 (if named "Field" "Column")
8404 (if (member (string-to-char scol) '(?$ ?@)) "" "$")
8406 (if stored (org-table-formula-to-user stored) "")
8407 'org-table-formula-history
8410 (when (not (string-match "\\S-" eq))
8412 (setq stored-list (delq (assoc scol stored-list) stored-list))
8413 (org-table-store-formulas stored-list)
8414 (error "Formula removed"))
8415 (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
8416 (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
8417 (if (and name (not named))
8418 ;; We set the column equation, delete the named one.
8419 (setq stored-list (delq (assoc name stored-list) stored-list)
8422 (setcdr (assoc scol stored-list) eq)
8423 (setq stored-list (cons (cons scol eq) stored-list)))
8424 (if (or mustsave (not (equal stored eq)))
8425 (org-table-store-formulas stored-list))
8428 (defun org-table-store-formulas (alist)
8429 "Store the list of formulas below the current table."
8430 (setq alist (sort alist 'org-table-formula-less-p))
8432 (goto-char (org-table-end))
8433 (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
8435 ;; don't overwrite TBLFM, we might use text properties to store stuff
8436 (goto-char (match-beginning 2))
8437 (delete-region (match-beginning 2) (match-end 0)))
8438 (insert "#+TBLFM:"))
8440 (mapconcat (lambda (x)
8442 (if (equal (string-to-char (car x)) ?@) "" "$")
8443 (car x) "=" (cdr x)))
8447 (defsubst org-table-formula-make-cmp-string (a)
8448 (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
8450 (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
8451 (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
8452 (if (match-end 5) (concat "@@" (match-string 5 a))))))
8454 (defun org-table-formula-less-p (a b)
8455 "Compare two formulas for sorting."
8456 (let ((as (org-table-formula-make-cmp-string (car a)))
8457 (bs (org-table-formula-make-cmp-string (car b))))
8458 (and as bs (string< as bs))))
8460 (defun org-table-get-stored-formulas (&optional noerror)
8461 "Return an alist with the stored formulas directly after current table."
8463 (let (scol eq eq-alist strings string seen)
8465 (goto-char (org-table-end))
8466 (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
8467 (setq strings (org-split-string (match-string 2) " *:: *"))
8468 (while (setq string (pop strings))
8469 (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
8470 (setq scol (if (match-end 2)
8471 (match-string 2 string)
8472 (match-string 1 string))
8473 eq (match-string 3 string)
8474 eq-alist (cons (cons scol eq) eq-alist))
8475 (if (member scol seen)
8478 (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
8481 (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
8482 (push scol seen))))))
8483 (nreverse eq-alist)))
8485 (defun org-table-fix-formulas (key replace &optional limit delta remove)
8486 "Modify the equations after the table structure has been edited.
8487 KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
8488 For all numbers larger than LIMIT, shift them by DELTA."
8490 (goto-char (org-table-end))
8491 (when (looking-at "#\\+TBLFM:")
8492 (let ((re (concat key "\\([0-9]+\\)"))
8496 (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
8497 (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
8500 (while (re-search-forward re2 (point-at-eol) t)
8501 (replace-match "")))
8502 (while (re-search-forward re (point-at-eol) t)
8503 (setq s (match-string 1) n (string-to-number s))
8505 ((setq a (assoc s replace))
8506 (replace-match (concat key (cdr a)) t t))
8507 ((and limit (> n limit))
8508 (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
8510 (defun org-table-get-specials ()
8511 "Get the column names and local parameters for this table."
8513 (let ((beg (org-table-begin)) (end (org-table-end))
8514 names name fields fields1 field cnt
8515 c v l line col types dlines hlines)
8516 (setq org-table-column-names nil
8517 org-table-local-parameters nil
8518 org-table-named-field-locations nil
8519 org-table-current-begin-line nil
8520 org-table-current-begin-pos nil
8521 org-table-current-line-types nil)
8523 (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
8524 (setq names (org-split-string (match-string 1) " *| *")
8526 (while (setq name (pop names))
8528 (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
8529 (push (cons name (int-to-string cnt)) org-table-column-names))))
8530 (setq org-table-column-names (nreverse org-table-column-names))
8531 (setq org-table-column-name-regexp
8532 (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
8534 (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
8535 (setq fields (org-split-string (match-string 1) " *| *"))
8536 (while (setq field (pop fields))
8537 (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
8538 (push (cons (match-string 1 field) (match-string 2 field))
8539 org-table-local-parameters))))
8541 (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
8542 (setq c (match-string 1)
8543 fields (org-split-string (match-string 2) " *| *"))
8545 (beginning-of-line (if (equal c "_") 2 0))
8546 (setq line (org-current-line) col 1)
8547 (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
8548 (setq fields1 (org-split-string (match-string 1) " *| *"))))
8549 (while (and fields1 (setq field (pop fields)))
8550 (setq v (pop fields1) col (1+ col))
8551 (when (and (stringp field) (stringp v)
8552 (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
8553 (push (cons field v) org-table-local-parameters)
8554 (push (list field line col) org-table-named-field-locations))))
8555 ;; Analyse the line types
8557 (setq org-table-current-begin-line (org-current-line)
8558 org-table-current-begin-pos (point)
8559 l org-table-current-begin-line)
8560 (while (looking-at "[ \t]*|\\(-\\)?")
8561 (push (if (match-end 1) 'hline 'dline) types)
8562 (if (match-end 1) (push l hlines) (push l dlines))
8563 (beginning-of-line 2)
8565 (setq org-table-current-line-types (apply 'vector (nreverse types))
8566 org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
8567 org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
8569 (defun org-table-maybe-eval-formula ()
8570 "Check if the current field starts with \"=\" or \":=\".
8571 If yes, store the formula and apply it."
8572 ;; We already know we are in a table. Get field will only return a formula
8573 ;; when appropriate. It might return a separator line, but no problem.
8574 (when org-table-formula-evaluate-inline
8575 (let* ((field (org-trim (or (org-table-get-field) "")))
8577 (when (string-match "^:?=\\(.*\\)" field)
8578 (setq named (equal (string-to-char field) ?:)
8579 eq (match-string 1 field))
8580 (if (or (fboundp 'calc-eval)
8581 (equal (substring eq 0 (min 2 (length eq))) "'("))
8582 (org-table-eval-formula (if named '(4) nil)
8583 (org-table-formula-from-user eq))
8584 (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
8586 (defvar org-recalc-commands nil
8587 "List of commands triggering the recalculation of a line.
8588 Will be filled automatically during use.")
8590 (defvar org-recalc-marks
8591 '((" " . "Unmarked: no special line, no automatic recalculation")
8592 ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
8593 ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
8594 ("!" . "Column name definition line. Reference in formula as $name.")
8595 ("$" . "Parameter definition line name=value. Reference in formula as $name.")
8596 ("_" . "Names for values in row below this one.")
8597 ("^" . "Names for values in row above this one.")))
8599 (defun org-table-rotate-recalc-marks (&optional newchar)
8600 "Rotate the recalculation mark in the first column.
8601 If in any row, the first field is not consistent with a mark,
8602 insert a new column for the markers.
8603 When there is an active region, change all the lines in the region,
8604 after prompting for the marking character.
8605 After each change, a message will be displayed indicating the meaning
8608 (unless (org-at-table-p) (error "Not at a table"))
8609 (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
8610 (beg (org-table-begin))
8611 (end (org-table-end))
8612 (l (org-current-line))
8613 (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
8614 (l2 (if (org-region-active-p) (org-current-line (region-end))))
8618 (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
8619 (col (org-table-current-column))
8620 (forcenew (car (assoc newchar org-recalc-marks)))
8623 (message "Change region to what mark? Type # * ! $ or SPC: ")
8624 (setq newchar (char-to-string (read-char-exclusive))
8625 forcenew (car (assoc newchar org-recalc-marks))))
8626 (if (and newchar (not forcenew))
8627 (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
8629 (if l1 (goto-line l1))
8631 (beginning-of-line 1)
8632 (unless (looking-at org-table-dataline-regexp)
8633 (error "Not at a table data line")))
8635 (org-table-goto-column 1)
8636 (org-table-insert-column)
8637 (org-table-goto-column (1+ col)))
8638 (setq epos (point-at-eol))
8640 (beginning-of-line 1)
8641 (org-table-get-field
8642 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
8644 (setq new (or forcenew
8645 (cadr (member (match-string 1) marks))))
8651 (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
8652 (and (looking-at org-table-dataline-regexp)
8653 (org-table-get-field 1 (concat " " new " "))))
8655 (if (not (= epos (point-at-eol))) (org-table-align))
8657 (and (interactive-p) (message (cdr (assoc new org-recalc-marks))))))
8659 (defun org-table-maybe-recalculate-line ()
8660 "Recompute the current line if marked for it, and if we haven't just done it."
8662 (and org-table-allow-automatic-line-recalculation
8663 (not (and (memq last-command org-recalc-commands)
8664 (equal org-last-recalc-line (org-current-line))))
8665 (save-excursion (beginning-of-line 1)
8666 (looking-at org-table-auto-recalculate-regexp))
8667 (org-table-recalculate) t))
8669 (defvar org-table-formula-debug nil
8670 "Non-nil means, debug table formulas.
8671 When nil, simply write \"#ERROR\" in corrupted fields.")
8672 (make-variable-buffer-local 'org-table-formula-debug)
8675 (defsubst org-set-calc-mode (var &optional value)
8677 (setq var (assoc var '(("D" calc-angle-mode deg)
8678 ("R" calc-angle-mode rad)
8679 ("F" calc-prefer-frac t)
8680 ("S" calc-symbolic-mode t)))
8681 value (nth 2 var) var (nth 1 var)))
8682 (if (memq var modes)
8683 (setcar (cdr (memq var modes)) value)
8684 (cons var (cons value modes)))
8687 (defun org-table-eval-formula (&optional arg equation
8688 suppress-align suppress-const
8689 suppress-store suppress-analysis)
8690 "Replace the table field value at the cursor by the result of a calculation.
8692 This function makes use of Dave Gillespie's Calc package, in my view the
8693 most exciting program ever written for GNU Emacs. So you need to have Calc
8694 installed in order to use this function.
8696 In a table, this command replaces the value in the current field with the
8697 result of a formula. It also installs the formula as the \"current\" column
8698 formula, by storing it in a special line below the table. When called
8699 with a `C-u' prefix, the current field must ba a named field, and the
8700 formula is installed as valid in only this specific field.
8702 When called with two `C-u' prefixes, insert the active equation
8703 for the field back into the current field, so that it can be
8704 edited there. This is useful in order to use \\[org-table-show-reference]
8705 to check the referenced fields.
8707 When called, the command first prompts for a formula, which is read in
8708 the minibuffer. Previously entered formulas are available through the
8709 history list, and the last used formula is offered as a default.
8710 These stored formulas are adapted correctly when moving, inserting, or
8711 deleting columns with the corresponding commands.
8713 The formula can be any algebraic expression understood by the Calc package.
8714 For details, see the Org-mode manual.
8716 This function can also be called from Lisp programs and offers
8717 additional arguments: EQUATION can be the formula to apply. If this
8718 argument is given, the user will not be prompted. SUPPRESS-ALIGN is
8719 used to speed-up recursive calls by by-passing unnecessary aligns.
8720 SUPPRESS-CONST suppresses the interpretation of constants in the
8721 formula, assuming that this has been done already outside the function.
8722 SUPPRESS-STORE means the formula should not be stored, either because
8723 it is already stored, or because it is a modified equation that should
8724 not overwrite the stored one."
8726 (org-table-check-inside-data-field)
8727 (or suppress-analysis (org-table-get-specials))
8728 (if (equal arg '(16))
8729 (let ((eq (org-table-current-field-formula)))
8730 (or eq (error "No equation active for current field"))
8731 (org-table-get-field nil eq)
8733 (setq org-table-may-need-update t))
8735 (ndown (if (integerp arg) arg 1))
8736 (org-table-automatic-realign nil)
8737 (case-fold-search nil)
8739 (formula (if (and equation suppress-store)
8741 (org-table-get-formula equation (equal arg '(4)))))
8742 (n0 (org-table-current-column))
8743 (modes (copy-sequence org-calc-default-modes))
8744 (numbers nil) ; was a variable, now fixed default
8746 n form form0 bw fmt x ev orig c lispp literal)
8747 ;; Parse the format string. Since we have a lot of modes, this is
8748 ;; a lot of work. However, I think calc still uses most of the time.
8749 (if (string-match ";" formula)
8750 (let ((tmp (org-split-string formula ";")))
8751 (setq formula (car tmp)
8752 fmt (concat (cdr (assoc "%" org-table-local-parameters))
8754 (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
8755 (setq c (string-to-char (match-string 1 fmt))
8756 n (string-to-number (match-string 2 fmt)))
8758 (setq modes (org-set-calc-mode 'calc-internal-prec n))
8759 (setq modes (org-set-calc-mode
8761 (list (cdr (assoc c '((?n . float) (?f . fix)
8762 (?s . sci) (?e . eng))))
8764 (setq fmt (replace-match "" t t fmt)))
8765 (if (string-match "[NT]" fmt)
8766 (setq numbers (equal (match-string 0 fmt) "N")
8767 fmt (replace-match "" t t fmt)))
8768 (if (string-match "L" fmt)
8770 fmt (replace-match "" t t fmt)))
8771 (if (string-match "E" fmt)
8773 fmt (replace-match "" t t fmt)))
8774 (while (string-match "[DRFS]" fmt)
8775 (setq modes (org-set-calc-mode (match-string 0 fmt)))
8776 (setq fmt (replace-match "" t t fmt)))
8777 (unless (string-match "\\S-" fmt)
8779 (if (and (not suppress-const) org-table-formula-use-constants)
8780 (setq formula (org-table-formula-substitute-names formula)))
8781 (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
8783 (setq fields (org-split-string
8785 (buffer-substring (point-at-bol) (point-at-eol)))
8788 (setq fields (mapcar
8789 (lambda (x) (number-to-string (string-to-number x)))
8791 (setq ndown (1- ndown))
8792 (setq form (copy-sequence formula)
8793 lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
8794 (if (and lispp literal) (setq lispp 'literal))
8795 ;; Check for old vertical references
8796 (setq form (org-rewrite-old-row-references form))
8797 ;; Insert complex ranges
8798 (while (string-match org-table-range-regexp form)
8802 (org-table-make-reference
8803 (org-table-get-range (match-string 0 form) nil n0)
8804 keep-empty numbers lispp))
8806 ;; Insert simple ranges
8807 (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
8811 (org-table-make-reference
8813 fields (string-to-number (match-string 1 form))
8814 (string-to-number (match-string 2 form)))
8815 keep-empty numbers lispp))
8818 ;; Insert the references to fields in same row
8819 (while (string-match "\\$\\([0-9]+\\)" form)
8820 (setq n (string-to-number (match-string 1 form))
8821 x (nth (1- (if (= n 0) n0 n)) fields))
8822 (unless x (error "Invalid field specifier \"%s\""
8823 (match-string 0 form)))
8824 (setq form (replace-match
8826 (org-table-make-reference x nil numbers lispp))
8830 (setq ev (condition-case nil
8831 (eval (eval (read form)))
8833 ev (if (numberp ev) (number-to-string ev) ev))
8834 (or (fboundp 'calc-eval)
8835 (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
8836 (setq ev (calc-eval (cons form modes)
8837 (if numbers 'num))))
8839 (when org-table-formula-debug
8840 (with-output-to-temp-buffer "*Substitution History*"
8841 (princ (format "Substitution history of formula
8845 $1-> %s\n" orig formula form0 form))
8847 (princ (format " %s^\nError: %s"
8848 (make-string (car ev) ?\-) (nth 1 ev)))
8849 (princ (format "Result: %s\nFormat: %s\nFinal: %s"
8851 (if fmt (format fmt (string-to-number ev)) ev)))))
8852 (setq bw (get-buffer-window "*Substitution History*"))
8853 (shrink-window-if-larger-than-buffer bw)
8854 (unless (and (interactive-p) (not ndown))
8855 (unless (let (inhibit-redisplay)
8856 (y-or-n-p "Debugging Formula. Continue to next? "))
8861 (if (listp ev) (setq fmt nil ev "#ERROR"))
8862 (org-table-justify-field-maybe
8863 (if fmt (format fmt (string-to-number ev)) ev))
8864 (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
8865 (call-interactively 'org-return)
8867 (and down (org-table-maybe-recalculate-line))
8868 (or suppress-align (and org-table-may-need-update
8869 (org-table-align))))))
8871 (defun org-table-put-field-property (prop value)
8873 (put-text-property (progn (skip-chars-backward "^|") (point))
8874 (progn (skip-chars-forward "^|") (point))
8877 (defun org-table-get-range (desc &optional tbeg col highlight)
8878 "Get a calc vector from a column, accorting to descriptor DESC.
8879 Optional arguments TBEG and COL can give the beginning of the table and
8880 the current column, to avoid unnecessary parsing.
8881 HIGHLIGHT means, just highlight the range."
8882 (if (not (equal (string-to-char desc) ?@))
8883 (setq desc (concat "@" desc)))
8885 (or tbeg (setq tbeg (org-table-begin)))
8886 (or col (setq col (org-table-current-column)))
8887 (let ((thisline (org-current-line))
8888 beg end c1 c2 r1 r2 rangep tmp)
8889 (unless (string-match org-table-range-regexp desc)
8890 (error "Invalid table range specifier `%s'" desc))
8891 (setq rangep (match-end 3)
8892 r1 (and (match-end 1) (match-string 1 desc))
8893 r2 (and (match-end 4) (match-string 4 desc))
8894 c1 (and (match-end 2) (substring (match-string 2 desc) 1))
8895 c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
8897 (and c1 (setq c1 (+ (string-to-number c1)
8898 (if (memq (string-to-char c1) '(?- ?+)) col 0))))
8899 (and c2 (setq c2 (+ (string-to-number c2)
8900 (if (memq (string-to-char c2) '(?- ?+)) col 0))))
8901 (if (equal r1 "") (setq r1 nil))
8902 (if (equal r2 "") (setq r2 nil))
8903 (if r1 (setq r1 (org-table-get-descriptor-line r1)))
8904 (if r2 (setq r2 (org-table-get-descriptor-line r2)))
8905 ; (setq r2 (or r2 r1) c2 (or c2 c1))
8906 (if (not r1) (setq r1 thisline))
8907 (if (not r2) (setq r2 thisline))
8908 (if (not c1) (setq c1 col))
8909 (if (not c2) (setq c2 col))
8910 (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
8914 (while (not (looking-at org-table-dataline-regexp))
8915 (beginning-of-line 2))
8916 (prog1 (org-trim (org-table-get-field c1))
8917 (if highlight (org-table-highlight-rectangle (point) (point)))))
8918 ;; A range, return a vector
8919 ;; First sort the numbers to get a regular ractangle
8920 (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
8921 (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
8923 (while (not (looking-at org-table-dataline-regexp))
8924 (beginning-of-line 2))
8925 (org-table-goto-column c1)
8928 (while (not (looking-at org-table-dataline-regexp))
8929 (beginning-of-line 0))
8930 (org-table-goto-column c2)
8933 (org-table-highlight-rectangle
8934 beg (progn (skip-chars-forward "^|\n") (point))))
8935 ;; return string representation of calc vector
8937 (apply 'append (org-table-copy-region beg end)))))))
8939 (defun org-table-get-descriptor-line (desc &optional cline bline table)
8940 "Analyze descriptor DESC and retrieve the corresponding line number.
8941 The cursor is currently in line CLINE, the table begins in line BLINE,
8942 and TABLE is a vector with line types."
8943 (if (string-match "^[0-9]+$" desc)
8944 (aref org-table-dlines (string-to-number desc))
8945 (setq cline (or cline (org-current-line))
8946 bline (or bline org-table-current-begin-line)
8947 table (or table org-table-current-line-types))
8949 (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
8951 (and (not (match-end 3)) (not (match-end 6)))
8952 (and (match-end 3) (match-end 6) (not (match-end 5))))
8953 (error "invalid row descriptor `%s'" desc))
8954 (let* ((hdir (and (match-end 2) (match-string 2 desc)))
8955 (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
8956 (odir (and (match-end 5) (match-string 5 desc)))
8957 (on (if (match-end 6) (string-to-number (match-string 6 desc))))
8959 (rel (and (match-end 6)
8960 (or (and (match-end 1) (not (match-end 3)))
8962 (if (and hn (not hdir))
8965 (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
8966 (if (and (not hn) on (not odir))
8967 (error "should never happen");;(aref org-table-dlines on)
8968 (if (and hn (> hn 0))
8969 (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
8971 (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
8974 (defun org-find-row-type (table i type backwards relative n)
8975 (let ((l (length table)))
8977 (while (and (setq i (+ i (if backwards -1 1)))
8979 (not (eq (aref table i) type))
8980 (if (and relative (eq (aref table i) 'hline))
8981 (progn (setq i (- i (if backwards -1 1)) n 1) nil)
8984 (if (or (< i 0) (>= i l))
8985 (error "Row descriptior leads outside table")
8988 (defun org-rewrite-old-row-references (s)
8989 (if (string-match "&[-+0-9I]" s)
8990 (error "Formula contains old &row reference, please rewrite using @-syntax")
8993 (defun org-table-make-reference (elements keep-empty numbers lispp)
8994 "Convert list ELEMENTS to something appropriate to insert into formula.
8995 KEEP-EMPTY indicated to keep empty fields, default is to skip them.
8996 NUMBERS indicates that everything should be converted to numbers.
8997 LISPP means to return something appropriate for a Lisp list."
8998 (if (stringp elements) ; just a single val
9000 (if (eq lispp 'literal)
9002 (prin1-to-string (if numbers (string-to-number elements) elements)))
9003 (if (equal elements "") (setq elements "0"))
9004 (if numbers (number-to-string (string-to-number elements)) elements))
9008 (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
9010 (setq elements (or elements '("0")))
9014 (if (eq lispp 'literal)
9016 (prin1-to-string (if numbers (string-to-number x) x))))
9018 (concat "[" (mapconcat
9020 (if numbers (number-to-string (string-to-number x)) x))
9024 (defun org-table-recalculate (&optional all noalign)
9025 "Recalculate the current table line by applying all stored formulas.
9026 With prefix arg ALL, do this for all lines in the table."
9028 (or (memq this-command org-recalc-commands)
9029 (setq org-recalc-commands (cons this-command org-recalc-commands)))
9030 (unless (org-at-table-p) (error "Not at a table"))
9031 (if (equal all '(16))
9033 (org-table-get-specials)
9034 (let* ((eqlist (sort (org-table-get-stored-formulas)
9035 (lambda (a b) (string< (car a) (car b)))))
9036 (inhibit-redisplay (not debug-on-error))
9037 (line-re org-table-dataline-regexp)
9038 (thisline (org-current-line))
9039 (thiscol (org-table-current-column))
9040 beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
9041 ;; Insert constants in all formulas
9044 (setcdr x (org-table-formula-substitute-names (cdr x)))
9047 ;; Split the equation list
9048 (while (setq eq (pop eqlist))
9049 (if (<= (string-to-char (car eq)) ?9)
9052 (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
9055 (setq end (move-marker (make-marker) (1+ (org-table-end))))
9056 (goto-char (setq beg (org-table-begin)))
9057 (if (re-search-forward org-table-calculate-mark-regexp end t)
9058 ;; This is a table with marked lines, compute selected lines
9059 (setq line-re org-table-recalculate-regexp)
9060 ;; Move forward to the first non-header line
9061 (if (and (re-search-forward org-table-dataline-regexp end t)
9062 (re-search-forward org-table-hline-regexp end t)
9063 (re-search-forward org-table-dataline-regexp end t))
9064 (setq beg (match-beginning 0))
9065 nil))) ;; just leave beg where it is
9066 (setq beg (point-at-bol)
9067 end (move-marker (make-marker) (1+ (point-at-eol)))))
9069 (and all (message "Re-applying formulas to full table..."))
9071 ;; First find the named fields, and mark them untouchanble
9072 (remove-text-properties beg end '(org-untouchable t))
9073 (while (setq eq (pop eqlname))
9075 a (assoc name org-table-named-field-locations))
9077 (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
9079 (aref org-table-dlines
9080 (string-to-number (match-string 1 name)))
9081 (string-to-number (match-string 2 name)))))
9082 (when (and a (or all (equal (nth 1 a) thisline)))
9083 (message "Re-applying formula to field: %s" name)
9084 (goto-line (nth 1 a))
9085 (org-table-goto-column (nth 2 a))
9086 (push (append a (list (cdr eq))) eqlname1)
9087 ;; FIXME (org-table-eval-formula nil (cdr eq) 'noalign 'nocst
9088 ;; FIXME 'nostore 'noanalysis)
9089 (org-table-put-field-property :org-untouchable t)))
9091 ;; Now evauluate the column formulas, but skip fields covered by
9094 (while (re-search-forward line-re end t)
9095 (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
9096 ;; Unprotected line, recalculate
9097 (and all (message "Re-applying formulas to full table...(line %d)"
9098 (setq cnt (1+ cnt))))
9099 (setq org-last-recalc-line (org-current-line))
9101 (while (setq entry (pop eql))
9102 (goto-line org-last-recalc-line)
9103 (org-table-goto-column (string-to-number (car entry)) nil 'force)
9104 (unless (get-text-property (point) :org-untouchable)
9105 (org-table-eval-formula nil (cdr entry)
9106 'noalign 'nocst 'nostore 'noanalysis)))))
9108 ;; Now evaluate the field formulas
9109 (while (setq eq (pop eqlname1))
9110 (message "Re-applying formula to field: %s" (car eq))
9111 (goto-line (nth 1 eq))
9112 (org-table-goto-column (nth 2 eq))
9113 (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
9114 'nostore 'noanalysis))
9116 (goto-line thisline)
9117 (org-table-goto-column thiscol)
9118 (remove-text-properties (point-min) (point-max) '(org-untouchable t))
9119 (or noalign (and org-table-may-need-update (org-table-align))
9120 (and all (message "Re-applying formulas to %d lines...done" cnt)))
9122 ;; back to initial position
9123 (message "Re-applying formulas...done")
9124 (goto-line thisline)
9125 (org-table-goto-column thiscol)
9126 (or noalign (and org-table-may-need-update (org-table-align))
9127 (and all (message "Re-applying formulas...done"))))))
9129 (defun org-table-iterate (&optional arg)
9130 "Recalculate the table until it does not change anymore."
9132 (let ((imax (if arg (prefix-numeric-value arg) 10))
9134 (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
9139 (org-table-recalculate 'all)
9140 (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
9141 (if (not (string= lasttbl thistbl))
9142 (setq lasttbl thistbl)
9144 (message "Convergence after %d iterations" i)
9145 (message "Table was already stable"))
9147 (error "No convergence after %d iterations" i))))
9149 (defun org-table-formula-substitute-names (f)
9150 "Replace $const with values in string F."
9151 (let ((start 0) a (f1 f))
9152 ;; First, check for column names
9153 (while (setq start (string-match org-table-column-name-regexp f start))
9154 (setq start (1+ start))
9155 (setq a (assoc (match-string 1 f) org-table-column-names))
9156 (setq f (replace-match (concat "$" (cdr a)) t t f)))
9157 ;; Parameters and constants
9159 (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
9160 (setq start (1+ start))
9161 (if (setq a (save-match-data
9162 (org-table-get-constant (match-string 1 f))))
9163 (setq f (replace-match (concat "(" a ")") t t f))))
9164 (if org-table-formula-debug
9165 (put-text-property 0 (length f) :orig-formula f1 f))
9168 (defun org-table-get-constant (const)
9169 "Find the value for a parameter or constant in a formula.
9170 Parameters get priority."
9171 (or (cdr (assoc const org-table-local-parameters))
9172 (cdr (assoc const org-table-formula-constants-local))
9173 (cdr (assoc const org-table-formula-constants))
9174 (and (fboundp 'constants-get) (constants-get const))
9175 (and (string= (substring const 0 (min 5 (length const))) "PROP_")
9176 (org-entry-get nil (substring const 5) 'inherit))
9179 (defvar org-table-fedit-map
9180 (let ((map (make-sparse-keymap)))
9181 (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
9182 (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
9183 (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
9184 (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
9185 (org-defkey map "\C-c?" 'org-table-show-reference)
9186 (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
9187 (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
9188 (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
9189 (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
9190 (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
9191 (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
9192 (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
9193 (org-defkey map [(meta down)] 'org-table-fedit-scroll)
9194 (org-defkey map [(meta tab)] 'lisp-complete-symbol)
9195 (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
9196 (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
9197 (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
9198 (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
9199 (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
9202 (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
9204 ["Finish and Install" org-table-fedit-finish t]
9205 ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
9206 ["Abort" org-table-fedit-abort t]
9208 ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
9209 ["Complete Lisp Symbol" lisp-complete-symbol t]
9211 "Shift Reference at Point"
9212 ["Up" org-table-fedit-ref-up t]
9213 ["Down" org-table-fedit-ref-down t]
9214 ["Left" org-table-fedit-ref-left t]
9215 ["Right" org-table-fedit-ref-right t]
9217 "Change Test Row for Column Formulas"
9218 ["Up" org-table-fedit-line-up t]
9219 ["Down" org-table-fedit-line-down t]
9221 ["Scroll Table Window" org-table-fedit-scroll t]
9222 ["Scroll Table Window down" org-table-fedit-scroll-down t]
9223 ["Show Table Grid" org-table-fedit-toggle-coordinates
9224 :style toggle :selected (with-current-buffer (marker-buffer org-pos)
9225 org-table-overlay-coordinates)]
9227 ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
9228 :style toggle :selected org-table-buffer-is-an]))
9232 (defun org-table-edit-formulas ()
9233 "Edit the formulas of the current table in a separate buffer."
9235 (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
9236 (beginning-of-line 0))
9237 (unless (org-at-table-p) (error "Not at a table"))
9238 (org-table-get-specials)
9239 (let ((key (org-table-current-field-formula 'key 'noerror))
9240 (eql (sort (org-table-get-stored-formulas 'noerror)
9241 'org-table-formula-less-p))
9242 (pos (move-marker (make-marker) (point)))
9244 (wc (current-window-configuration))
9245 (titles '((column . "# Column Formulas\n")
9246 (field . "# Field Formulas\n")
9247 (named . "# Named Field Formulas\n")))
9249 (switch-to-buffer-other-window "*Edit Formulas*")
9251 ;; Keep global-font-lock-mode from turning on font-lock-mode
9252 (let ((font-lock-global-modes '(not fundamental-mode)))
9254 (org-set-local 'font-lock-global-modes (list 'not major-mode))
9255 (org-set-local 'org-pos pos)
9256 (org-set-local 'org-window-configuration wc)
9257 (use-local-map org-table-fedit-map)
9258 (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
9259 (easy-menu-add org-table-fedit-menu)
9260 (setq startline (org-current-line))
9261 (while (setq entry (pop eql))
9263 ((equal (string-to-char (car entry)) ?@) 'field)
9264 ((string-match "^[0-9]" (car entry)) 'column)
9266 (when (setq title (assq type titles))
9267 (or (bobp) (insert "\n"))
9268 (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
9269 (setq titles (delq title titles)))
9270 (if (equal key (car entry)) (setq startline (org-current-line)))
9271 (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
9272 (car entry) " = " (cdr entry) "\n"))
9273 (remove-text-properties 0 (length s) '(face nil) s)
9275 (if (eq org-table-use-standard-references t)
9276 (org-table-fedit-toggle-ref-type))
9277 (goto-line startline)
9278 (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
9280 (defun org-table-fedit-post-command ()
9281 (when (not (memq this-command '(lisp-complete-symbol)))
9282 (let ((win (selected-window)))
9285 (org-table-show-reference)
9287 (select-window win)))))
9289 (defun org-table-formula-to-user (s)
9290 "Convert a formula from internal to user representation."
9291 (if (eq org-table-use-standard-references t)
9292 (org-table-convert-refs-to-an s)
9295 (defun org-table-formula-from-user (s)
9296 "Convert a formula from user to internal representation."
9297 (if org-table-use-standard-references
9298 (org-table-convert-refs-to-rc s)
9301 (defun org-table-convert-refs-to-rc (s)
9302 "Convert spreadsheet references from AB7 to @7$28.
9303 Works for single references, but also for entire formulas and even the
9306 (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
9309 ;; format match, just advance
9310 (setq start (match-end 0)))
9311 ((and (> (match-beginning 0) 0)
9312 (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
9313 (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
9314 ;; 3.e5 or something like this. FIXME: is this ok????
9315 (setq start (match-end 0)))
9317 (setq start (match-beginning 0)
9319 (if (equal (match-string 2 s) "&")
9320 (format "$%d" (org-letters-to-number (match-string 1 s)))
9322 (string-to-number (match-string 2 s))
9323 (org-letters-to-number (match-string 1 s))))
9327 (defun org-table-convert-refs-to-an (s)
9328 "Convert spreadsheet references from to @7$28 to AB7.
9329 Works for single references, but also for entire formulas and even the
9331 (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
9332 (setq s (replace-match
9334 (org-number-to-letters
9335 (string-to-number (match-string 2 s)))
9336 (string-to-number (match-string 1 s)))
9338 (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
9339 (setq s (replace-match (concat "\\1"
9340 (org-number-to-letters
9341 (string-to-number (match-string 2 s))) "&")
9345 (defun org-letters-to-number (s)
9346 "Convert a base 26 number represented by letters into an integer.
9347 For example: AB -> 28."
9350 (while (> (length s) 0)
9351 (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
9355 (defun org-number-to-letters (n)
9356 "Convert an integer into a base 26 number represented by letters.
9357 For example: 28 -> AB."
9360 (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
9364 (defun org-table-fedit-convert-buffer (function)
9365 "Convert all references in this buffer, using FUNTION."
9366 (let ((line (org-current-line)))
9367 (goto-char (point-min))
9369 (insert (funcall function (buffer-substring (point) (point-at-eol))))
9370 (delete-region (point) (point-at-eol))
9371 (or (eobp) (forward-char 1)))
9374 (defun org-table-fedit-toggle-ref-type ()
9375 "Convert all references in the buffer from B3 to @3$2 and back."
9377 (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
9378 (org-table-fedit-convert-buffer
9379 (if org-table-buffer-is-an
9380 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
9381 (message "Reference type switched to %s"
9382 (if org-table-buffer-is-an "A1 etc" "@row$column")))
9384 (defun org-table-fedit-ref-up ()
9385 "Shift the reference at point one row/hline up."
9387 (org-table-fedit-shift-reference 'up))
9388 (defun org-table-fedit-ref-down ()
9389 "Shift the reference at point one row/hline down."
9391 (org-table-fedit-shift-reference 'down))
9392 (defun org-table-fedit-ref-left ()
9393 "Shift the reference at point one field to the left."
9395 (org-table-fedit-shift-reference 'left))
9396 (defun org-table-fedit-ref-right ()
9397 "Shift the reference at point one field to the right."
9399 (org-table-fedit-shift-reference 'right))
9401 (defun org-table-fedit-shift-reference (dir)
9403 ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
9404 (if (memq dir '(left right))
9405 (org-rematch-and-replace 1 (eq dir 'left))
9406 (error "Cannot shift reference in this direction")))
9407 ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
9408 ;; A B3-like reference
9409 (if (memq dir '(up down))
9410 (org-rematch-and-replace 2 (eq dir 'up))
9411 (org-rematch-and-replace 1 (eq dir 'left))))
9413 "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
9414 ;; An internal reference
9415 (if (memq dir '(up down))
9416 (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
9417 (org-rematch-and-replace 5 (eq dir 'left))))))
9419 (defun org-rematch-and-replace (n &optional decr hline)
9420 "Re-match the group N, and replace it with the shifted refrence."
9421 (or (match-end n) (error "Cannot shift reference in this direction"))
9422 (goto-char (match-beginning n))
9423 (and (looking-at (regexp-quote (match-string n)))
9424 (replace-match (org-shift-refpart (match-string 0) decr hline)
9427 (defun org-shift-refpart (ref &optional decr hline)
9428 "Shift a refrence part REF.
9429 If DECR is set, decrease the references row/column, else increase.
9430 If HLINE is set, this may be a hline reference, it certainly is not
9431 a translation reference."
9433 (let* ((sign (string-match "^[-+]" ref)) n)
9435 (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
9437 ((and hline (string-match "^I+" ref))
9438 (setq n (string-to-number (concat sign (number-to-string (length ref)))))
9439 (setq n (+ n (if decr -1 1)))
9440 (if (= n 0) (setq n (+ n (if decr -1 1))))
9442 (setq sign (if (< n 0) "-" "+") n (abs n))
9444 (concat sign (make-string n ?I)))
9446 ((string-match "^[0-9]+" ref)
9447 (setq n (string-to-number (concat sign ref)))
9448 (setq n (+ n (if decr -1 1)))
9450 (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
9451 (number-to-string (max 1 n))))
9453 ((string-match "^[a-zA-Z]+" ref)
9454 (org-number-to-letters
9455 (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
9457 (t (error "Cannot shift reference"))))))
9459 (defun org-table-fedit-toggle-coordinates ()
9460 "Toggle the display of coordinates in the refrenced table."
9462 (let ((pos (marker-position org-pos)))
9463 (with-current-buffer (marker-buffer org-pos)
9466 (org-table-toggle-coordinate-overlays)))))
9468 (defun org-table-fedit-finish (&optional arg)
9469 "Parse the buffer for formula definitions and install them.
9470 With prefix ARG, apply the new formulas to the table."
9472 (org-table-remove-rectangle-highlight)
9473 (if org-table-use-standard-references
9475 (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
9476 (setq org-table-buffer-is-an nil)))
9477 (let ((pos org-pos) eql var form)
9478 (goto-char (point-min))
9479 (while (re-search-forward
9480 "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
9482 (setq var (if (match-end 2) (match-string 2) (match-string 1))
9483 form (match-string 3))
9484 (setq form (org-trim form))
9485 (when (not (equal form ""))
9486 (while (string-match "[ \t]*\n[ \t]*" form)
9487 (setq form (replace-match " " t t form)))
9488 (when (assoc var eql)
9489 (error "Double formulas for %s" var))
9490 (push (cons var form) eql)))
9492 (set-window-configuration org-window-configuration)
9493 (select-window (get-buffer-window (marker-buffer pos)))
9495 (unless (org-at-table-p)
9496 (error "Lost table position - cannot install formulae"))
9497 (org-table-store-formulas eql)
9498 (move-marker pos nil)
9499 (kill-buffer "*Edit Formulas*")
9501 (org-table-recalculate 'all)
9502 (message "New formulas installed - press C-u C-c C-c to apply."))))
9504 (defun org-table-fedit-abort ()
9505 "Abort editing formulas, without installing the changes."
9507 (org-table-remove-rectangle-highlight)
9508 (let ((pos org-pos))
9509 (set-window-configuration org-window-configuration)
9510 (select-window (get-buffer-window (marker-buffer pos)))
9512 (move-marker pos nil)
9513 (message "Formula editing aborted without installing changes")))
9515 (defun org-table-fedit-lisp-indent ()
9516 "Pretty-print and re-indent Lisp expressions in the Formula Editor."
9518 (let ((pos (point)) beg end ind)
9519 (beginning-of-line 1)
9521 ((looking-at "[ \t]")
9523 (call-interactively 'lisp-indent-line))
9524 ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
9525 ((not (fboundp 'pp-buffer))
9526 (error "Cannot pretty-print. Command `pp-buffer' is not available."))
9527 ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
9528 (goto-char (- (match-end 0) 2))
9530 (setq ind (make-string (current-column) ?\ ))
9531 (condition-case nil (forward-sexp 1)
9533 (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
9536 (narrow-to-region beg end)
9537 (if (eq last-command this-command)
9539 (goto-char (point-min))
9540 (setq this-command nil)
9541 (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
9542 (replace-match " ")))
9544 (untabify (point-min) (point-max))
9545 (goto-char (1+ (point-min)))
9546 (while (re-search-forward "^." nil t)
9547 (beginning-of-line 1)
9549 (goto-char (point-max))
9550 (backward-delete-char 1)))
9554 (defvar org-show-positions nil)
9556 (defun org-table-show-reference (&optional local)
9557 "Show the location/value of the $ expression at point."
9559 (org-table-remove-rectangle-highlight)
9561 (let ((pos (if local (point) org-pos))
9563 (org-inhibit-highlight-removal t)
9564 (win (selected-window))
9565 (org-show-positions nil)
9566 var name e what match dest)
9567 (if local (org-table-get-specials))
9569 ((or (org-at-regexp-p org-table-range-regexp2)
9570 (org-at-regexp-p org-table-translate-regexp)
9571 (org-at-regexp-p org-table-range-regexp))
9574 (org-table-convert-refs-to-rc (match-string 0))))
9576 ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
9577 ((org-at-regexp-p "\\$[0-9]+") 'column)
9579 (t (error "No reference at point")))
9580 match (and what (or match (match-string 0))))
9581 (when (and match (not (equal (match-beginning 0) (point-at-bol))))
9582 (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
9583 'secondary-selection))
9584 (org-add-hook 'before-change-functions
9585 'org-table-remove-rectangle-highlight)
9586 (if (eq what 'name) (setq var (substring match 1)))
9587 (when (eq what 'range)
9588 (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
9589 (setq match (org-table-formula-substitute-names match)))
9593 (re-search-backward "^\\S-" nil t)
9594 (beginning-of-line 1)
9595 (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
9598 (org-table-convert-refs-to-rc (match-string 1))))
9599 (org-table-add-rectangle-overlay
9600 (match-beginning 1) (match-end 1) face2))))
9601 (if (and (markerp pos) (marker-buffer pos))
9602 (if (get-buffer-window (marker-buffer pos))
9603 (select-window (get-buffer-window (marker-buffer pos)))
9604 (switch-to-buffer-other-window (get-buffer-window
9605 (marker-buffer pos)))))
9607 (org-table-force-dataline)
9609 (setq name (substring dest 1))
9611 ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
9612 (setq e (assoc name org-table-named-field-locations))
9613 (goto-line (nth 1 e))
9614 (org-table-goto-column (nth 2 e)))
9615 ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
9616 (let ((l (string-to-number (match-string 1 dest)))
9617 (c (string-to-number (match-string 2 dest))))
9618 (goto-line (aref org-table-dlines l))
9619 (org-table-goto-column c)))
9620 (t (org-table-goto-column (string-to-number name))))
9621 (move-marker pos (point))
9622 (org-table-highlight-rectangle nil nil face2))
9624 ((equal dest match))
9629 (org-table-get-range match nil nil 'highlight))
9631 ((setq e (assoc var org-table-named-field-locations))
9632 (goto-line (nth 1 e))
9633 (org-table-goto-column (nth 2 e))
9634 (org-table-highlight-rectangle (point) (point))
9635 (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
9636 ((setq e (assoc var org-table-column-names))
9637 (org-table-goto-column (string-to-number (cdr e)))
9638 (org-table-highlight-rectangle (point) (point))
9639 (goto-char (org-table-begin))
9640 (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
9643 (goto-char (match-beginning 1))
9644 (org-table-highlight-rectangle)
9645 (message "Named column (column %s)" (cdr e)))
9646 (error "Column name not found")))
9649 (org-table-goto-column (string-to-number (substring match 1)))
9650 (org-table-highlight-rectangle (point) (point))
9651 (message "Column %s" (substring match 1)))
9652 ((setq e (assoc var org-table-local-parameters))
9653 (goto-char (org-table-begin))
9654 (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
9656 (goto-char (match-beginning 1))
9657 (org-table-highlight-rectangle)
9658 (message "Local parameter."))
9659 (error "Parameter not found")))
9662 ((not var) (error "No reference at point"))
9663 ((setq e (assoc var org-table-formula-constants-local))
9664 (message "Local Constant: $%s=%s in #+CONSTANTS line."
9666 ((setq e (assoc var org-table-formula-constants))
9667 (message "Constant: $%s=%s in `org-table-formula-constants'."
9669 ((setq e (and (fboundp 'constants-get) (constants-get var)))
9670 (message "Constant: $%s=%s, from `constants.el'%s."
9671 var e (format " (%s units)" constants-unit-system)))
9672 (t (error "Undefined name $%s" var)))))
9674 (when (and org-show-positions
9675 (not (memq this-command '(org-table-fedit-scroll
9676 org-table-fedit-scroll-down))))
9677 (push pos org-show-positions)
9678 (push org-table-current-begin-pos org-show-positions)
9679 (let ((min (apply 'min org-show-positions))
9680 (max (apply 'max org-show-positions)))
9681 (goto-char min) (recenter 0)
9683 (or (pos-visible-in-window-p max) (recenter -1))))
9684 (select-window win))))
9686 (defun org-table-force-dataline ()
9687 "Make sure the cursor is in a dataline in a table."
9688 (unless (save-excursion
9689 (beginning-of-line 1)
9690 (looking-at org-table-dataline-regexp))
9691 (let* ((re org-table-dataline-regexp)
9692 (p1 (save-excursion (re-search-forward re nil 'move)))
9693 (p2 (save-excursion (re-search-backward re nil 'move))))
9695 (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
9697 ((or p1 p2) (goto-char (or p1 p2)))
9698 (t (error "No table dataline around here"))))))
9700 (defun org-table-fedit-line-up ()
9701 "Move cursor one line up in the window showing the table."
9703 (org-table-fedit-move 'previous-line))
9705 (defun org-table-fedit-line-down ()
9706 "Move cursor one line down in the window showing the table."
9708 (org-table-fedit-move 'next-line))
9710 (defun org-table-fedit-move (command)
9711 "Move the cursor in the window shoinw the table.
9712 Use COMMAND to do the motion, repeat if necessary to end up in a data line."
9713 (let ((org-table-allow-automatic-line-recalculation nil)
9714 (pos org-pos) (win (selected-window)) p)
9715 (select-window (get-buffer-window (marker-buffer org-pos)))
9717 (call-interactively command)
9718 (while (and (org-at-table-p)
9719 (org-at-table-hline-p))
9720 (call-interactively command))
9721 (or (org-at-table-p) (goto-char p))
9722 (move-marker pos (point))
9723 (select-window win)))
9725 (defun org-table-fedit-scroll (N)
9727 (let ((other-window-scroll-buffer (marker-buffer org-pos)))
9728 (scroll-other-window N)))
9730 (defun org-table-fedit-scroll-down (N)
9732 (org-table-fedit-scroll (- N)))
9734 (defvar org-table-rectangle-overlays nil)
9736 (defun org-table-add-rectangle-overlay (beg end &optional face)
9737 "Add a new overlay."
9738 (let ((ov (org-make-overlay beg end)))
9739 (org-overlay-put ov 'face (or face 'secondary-selection))
9740 (push ov org-table-rectangle-overlays)))
9742 (defun org-table-highlight-rectangle (&optional beg end face)
9743 "Highlight rectangular region in a table."
9744 (setq beg (or beg (point)) end (or end (point)))
9745 (let ((b (min beg end))
9748 (and (boundp 'org-show-positions)
9749 (setq org-show-positions (cons b (cons e org-show-positions))))
9750 (goto-char (min beg end))
9751 (setq l1 (org-current-line)
9752 c1 (org-table-current-column))
9753 (goto-char (max beg end))
9754 (setq l2 (org-current-line)
9755 c2 (org-table-current-column))
9756 (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
9758 (beginning-of-line 1)
9759 (loop for line from l1 to l2 do
9760 (when (looking-at org-table-dataline-regexp)
9761 (org-table-goto-column c1)
9762 (skip-chars-backward "^|\n") (setq beg (point))
9763 (org-table-goto-column c2)
9764 (skip-chars-forward "^|\n") (setq end (point))
9765 (org-table-add-rectangle-overlay beg end face))
9766 (beginning-of-line 2))
9768 (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
9770 (defun org-table-remove-rectangle-highlight (&rest ignore)
9771 "Remove the rectangle overlays."
9772 (unless org-inhibit-highlight-removal
9773 (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
9774 (mapc 'org-delete-overlay org-table-rectangle-overlays)
9775 (setq org-table-rectangle-overlays nil)))
9777 (defvar org-table-coordinate-overlays nil
9778 "Collects the cooordinate grid overlays, so that they can be removed.")
9779 (make-variable-buffer-local 'org-table-coordinate-overlays)
9781 (defun org-table-overlay-coordinates ()
9782 "Add overlays to the table at point, to show row/column coordinates."
9784 (mapc 'org-delete-overlay org-table-coordinate-overlays)
9785 (setq org-table-coordinate-overlays nil)
9787 (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
9788 (goto-char (org-table-begin))
9789 (while (org-at-table-p)
9790 (setq eol (point-at-eol))
9791 (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
9792 (push ov org-table-coordinate-overlays)
9793 (setq hline (looking-at org-table-hline-regexp))
9794 (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
9795 (format "%4d" (setq id (1+ id)))))
9796 (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
9799 (while (re-search-forward "[+|]\\(-+\\)" eol t)
9800 (setq beg (1+ (match-beginning 0))
9802 s1 (concat "$" (int-to-string ic))
9803 s2 (org-number-to-letters ic)
9804 str (if (eq org-table-use-standard-references t) s2 s1))
9805 (setq ov (org-make-overlay beg (+ beg (length str))))
9806 (push ov org-table-coordinate-overlays)
9807 (org-overlay-display ov str 'org-special-keyword 'evaporate)))
9808 (beginning-of-line 2)))))
9810 (defun org-table-toggle-coordinate-overlays ()
9811 "Toggle the display of Row/Column numbers in tables."
9813 (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
9814 (message "Row/Column number display turned %s"
9815 (if org-table-overlay-coordinates "on" "off"))
9816 (if (and (org-at-table-p) org-table-overlay-coordinates)
9818 (unless org-table-overlay-coordinates
9819 (mapc 'org-delete-overlay org-table-coordinate-overlays)
9820 (setq org-table-coordinate-overlays nil)))
9822 (defun org-table-toggle-formula-debugger ()
9823 "Toggle the formula debugger in tables."
9825 (setq org-table-formula-debug (not org-table-formula-debug))
9826 (message "Formula debugging has been turned %s"
9827 (if org-table-formula-debug "on" "off")))
9829 ;;; The orgtbl minor mode
9831 ;; Define a minor mode which can be used in other modes in order to
9832 ;; integrate the org-mode table editor.
9834 ;; This is really a hack, because the org-mode table editor uses several
9835 ;; keys which normally belong to the major mode, for example the TAB and
9836 ;; RET keys. Here is how it works: The minor mode defines all the keys
9837 ;; necessary to operate the table editor, but wraps the commands into a
9838 ;; function which tests if the cursor is currently inside a table. If that
9839 ;; is the case, the table editor command is executed. However, when any of
9840 ;; those keys is used outside a table, the function uses `key-binding' to
9841 ;; look up if the key has an associated command in another currently active
9842 ;; keymap (minor modes, major mode, global), and executes that command.
9843 ;; There might be problems if any of the keys used by the table editor is
9844 ;; otherwise used as a prefix key.
9846 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
9847 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
9848 ;; addresses this by checking explicitly for both bindings.
9850 ;; The optimized version (see variable `orgtbl-optimized') takes over
9851 ;; all keys which are bound to `self-insert-command' in the *global map*.
9852 ;; Some modes bind other commands to simple characters, for example
9853 ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
9854 ;; active, this binding is ignored inside tables and replaced with a
9855 ;; modified self-insert.
9857 (defvar orgtbl-mode nil
9858 "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
9859 table editor in arbitrary modes.")
9860 (make-variable-buffer-local 'orgtbl-mode)
9862 (defvar orgtbl-mode-map (make-keymap)
9863 "Keymap for `orgtbl-mode'.")
9866 (defun turn-on-orgtbl ()
9867 "Unconditionally turn on `orgtbl-mode'."
9870 (defvar org-old-auto-fill-inhibit-regexp nil
9871 "Local variable used by `orgtbl-mode'")
9873 (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
9874 "Matches a line belonging to an orgtbl.")
9876 (defconst orgtbl-extra-font-lock-keywords
9877 (list (list (concat "^" orgtbl-line-start-regexp ".*")
9878 0 (quote 'org-table) 'prepend))
9879 "Extra font-lock-keywords to be added when orgtbl-mode is active.")
9882 (defun orgtbl-mode (&optional arg)
9883 "The `org-mode' table editor as a minor mode for use in other modes."
9886 ;; Exit without error, in case some hook functions calls this
9887 ;; by accident in org-mode.
9888 (message "Orgtbl-mode is not useful in org-mode, command ignored")
9890 (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
9893 (and (orgtbl-setup) (defun orgtbl-setup () nil))
9894 ;; Make sure we are first in minor-mode-map-alist
9895 (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
9896 (and c (setq minor-mode-map-alist
9897 (cons c (delq c minor-mode-map-alist)))))
9898 (org-set-local (quote org-table-may-need-update) t)
9899 (org-add-hook 'before-change-functions 'org-before-change-function
9901 (org-set-local 'org-old-auto-fill-inhibit-regexp
9902 auto-fill-inhibit-regexp)
9903 (org-set-local 'auto-fill-inhibit-regexp
9904 (if auto-fill-inhibit-regexp
9905 (concat orgtbl-line-start-regexp "\\|"
9906 auto-fill-inhibit-regexp)
9907 orgtbl-line-start-regexp))
9908 (org-add-to-invisibility-spec '(org-cwidth))
9909 (when (fboundp 'font-lock-add-keywords)
9910 (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
9911 (org-restart-font-lock))
9912 (easy-menu-add orgtbl-mode-menu)
9913 (run-hooks 'orgtbl-mode-hook))
9914 (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
9915 (org-cleanup-narrow-column-properties)
9916 (org-remove-from-invisibility-spec '(org-cwidth))
9917 (remove-hook 'before-change-functions 'org-before-change-function t)
9918 (when (fboundp 'font-lock-remove-keywords)
9919 (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
9920 (org-restart-font-lock))
9921 (easy-menu-remove orgtbl-mode-menu)
9922 (force-mode-line-update 'all))))
9924 (defun org-cleanup-narrow-column-properties ()
9925 "Remove all properties related to narrow-column invisibility."
9927 (while (setq s (text-property-any s (point-max)
9928 'display org-narrow-column-arrow))
9929 (remove-text-properties s (1+ s) '(display t)))
9931 (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
9932 (remove-text-properties s (1+ s) '(org-cwidth t)))
9934 (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
9935 (remove-text-properties s (1+ s) '(invisible t)))))
9937 ;; Install it as a minor mode.
9938 (put 'orgtbl-mode :included t)
9939 (put 'orgtbl-mode :menu-tag "Org Table Mode")
9940 (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
9942 (defun orgtbl-make-binding (fun n &rest keys)
9943 "Create a function for binding in the table minor mode.
9944 FUN is the command to call inside a table. N is used to create a unique
9945 command name. KEYS are keys that should be checked in for a command
9946 to execute outside of tables."
9949 (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
9951 (concat "In tables, run `" (symbol-name fun) "'.\n"
9952 "Outside of tables, run the binding of `"
9953 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
9958 (list 'call-interactively (list 'quote fun))
9959 (list 'let '(orgtbl-mode)
9960 (list 'call-interactively
9963 (list 'key-binding k))
9965 '('orgtbl-error))))))))
9967 (defun orgtbl-error ()
9968 "Error when there is no default binding for a table key."
9970 (error "This key is has no function outside tables"))
9972 (defun orgtbl-setup ()
9973 "Setup orgtbl keymaps."
9977 '([(meta shift left)] org-table-delete-column)
9978 '([(meta left)] org-table-move-column-left)
9979 '([(meta right)] org-table-move-column-right)
9980 '([(meta shift right)] org-table-insert-column)
9981 '([(meta shift up)] org-table-kill-row)
9982 '([(meta shift down)] org-table-insert-row)
9983 '([(meta up)] org-table-move-row-up)
9984 '([(meta down)] org-table-move-row-down)
9985 '("\C-c\C-w" org-table-cut-region)
9986 '("\C-c\M-w" org-table-copy-region)
9987 '("\C-c\C-y" org-table-paste-rectangle)
9988 '("\C-c-" org-table-insert-hline)
9989 '("\C-c}" org-table-toggle-coordinate-overlays)
9990 '("\C-c{" org-table-toggle-formula-debugger)
9991 '("\C-m" org-table-next-row)
9992 '([(shift return)] org-table-copy-down)
9993 '("\C-c\C-q" org-table-wrap-region)
9994 '("\C-c?" org-table-field-info)
9995 '("\C-c " org-table-blank-field)
9996 '("\C-c+" org-table-sum)
9997 '("\C-c=" org-table-eval-formula)
9998 '("\C-c'" org-table-edit-formulas)
9999 '("\C-c`" org-table-edit-field)
10000 '("\C-c*" org-table-recalculate)
10001 '("\C-c|" org-table-create-or-convert-from-region)
10002 '("\C-c^" org-table-sort-lines)
10003 '([(control ?#)] org-table-rotate-recalc-marks)))
10005 (while (setq elt (pop bindings))
10006 (setq nfunc (1+ nfunc))
10007 (setq key (org-key (car elt))
10009 cmd (orgtbl-make-binding fun nfunc key))
10010 (org-defkey orgtbl-mode-map key cmd))
10012 ;; Special treatment needed for TAB and RET
10013 (org-defkey orgtbl-mode-map [(return)]
10014 (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
10015 (org-defkey orgtbl-mode-map "\C-m"
10016 (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
10018 (org-defkey orgtbl-mode-map [(tab)]
10019 (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
10020 (org-defkey orgtbl-mode-map "\C-i"
10021 (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
10023 (org-defkey orgtbl-mode-map [(shift tab)]
10024 (orgtbl-make-binding 'org-table-previous-field 104
10025 [(shift tab)] [(tab)] "\C-i"))
10027 (org-defkey orgtbl-mode-map "\M-\C-m"
10028 (orgtbl-make-binding 'org-table-wrap-region 105
10029 "\M-\C-m" [(meta return)]))
10030 (org-defkey orgtbl-mode-map [(meta return)]
10031 (orgtbl-make-binding 'org-table-wrap-region 106
10032 [(meta return)] "\M-\C-m"))
10034 (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
10035 (when orgtbl-optimized
10036 ;; If the user wants maximum table support, we need to hijack
10037 ;; some standard editing functions
10038 (org-remap orgtbl-mode-map
10039 'self-insert-command 'orgtbl-self-insert-command
10040 'delete-char 'org-delete-char
10041 'delete-backward-char 'org-delete-backward-char)
10042 (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
10043 (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
10045 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
10046 ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
10047 ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
10048 ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
10050 ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
10051 ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
10052 ["Copy Field from Above"
10053 org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
10056 ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
10057 ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
10058 ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
10059 ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
10061 ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
10062 ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
10063 ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
10064 ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
10065 ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
10067 ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
10069 ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
10070 ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
10071 ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
10072 ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
10075 ["Insert table template" orgtbl-insert-radio-table
10076 (assq major-mode orgtbl-radio-table-templates)]
10077 ["Comment/uncomment table" orgtbl-toggle-comment t])
10079 ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
10080 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
10081 ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
10082 ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
10083 ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
10084 ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
10085 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
10086 ["Sum Column/Rectangle" org-table-sum
10087 :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
10088 ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
10090 org-table-toggle-formula-debugger :active (org-at-table-p)
10092 :style toggle :selected org-table-formula-debug]
10093 ["Show Col/Row Numbers"
10094 org-table-toggle-coordinate-overlays :active (org-at-table-p)
10096 :style toggle :selected org-table-overlay-coordinates]
10100 (defun orgtbl-ctrl-c-ctrl-c (arg)
10101 "If the cursor is inside a table, realign the table.
10102 It it is a table to be sent away to a receiver, do it.
10103 With prefix arg, also recompute table."
10105 (let ((pos (point)) action)
10107 (beginning-of-line 1)
10108 (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
10109 ((looking-at "[ \t]*|") pos)
10110 ((looking-at "#\\+TBLFM:") 'recalc))))
10114 (org-table-maybe-eval-formula)
10116 (call-interactively 'org-table-recalculate)
10117 (org-table-maybe-recalculate-line))
10118 (call-interactively 'org-table-align)
10119 (orgtbl-send-table 'maybe))
10120 ((eq action 'recalc)
10122 (beginning-of-line 1)
10123 (skip-chars-backward " \r\n\t")
10124 (if (org-at-table-p)
10125 (org-call-with-arg 'org-table-recalculate t))))
10126 (t (let (orgtbl-mode)
10127 (call-interactively (key-binding "\C-c\C-c")))))))
10129 (defun orgtbl-tab (arg)
10130 "Justification and field motion for `orgtbl-mode'."
10132 (if arg (org-table-edit-field t)
10133 (org-table-justify-field-maybe)
10134 (org-table-next-field)))
10136 (defun orgtbl-ret ()
10137 "Justification and field motion for `orgtbl-mode'."
10139 (org-table-justify-field-maybe)
10140 (org-table-next-row))
10142 (defun orgtbl-self-insert-command (N)
10143 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
10144 If the cursor is in a table looking at whitespace, the whitespace is
10145 overwritten, and the table is not marked as requiring realignment."
10147 (if (and (org-at-table-p)
10149 (and org-table-auto-blank-field
10150 (member last-command
10151 '(orgtbl-hijacker-command-100
10152 orgtbl-hijacker-command-101
10153 orgtbl-hijacker-command-102
10154 orgtbl-hijacker-command-103
10155 orgtbl-hijacker-command-104
10156 orgtbl-hijacker-command-105))
10157 (org-table-blank-field))
10160 (looking-at "[^|\n]* +|"))
10161 (let (org-table-may-need-update)
10162 (goto-char (1- (match-end 0)))
10163 (delete-backward-char 1)
10164 (goto-char (match-beginning 0))
10165 (self-insert-command N))
10166 (setq org-table-may-need-update t)
10168 (call-interactively (key-binding (vector last-input-event))))))
10170 (defun org-force-self-insert (N)
10171 "Needed to enforce self-insert under remapping."
10173 (self-insert-command N))
10175 (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
10176 "Regula expression matching exponentials as produced by calc.")
10178 (defvar org-table-clean-did-remove-column nil)
10180 (defun orgtbl-export (table target)
10181 (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
10182 (lines (org-split-string table "[ \t]*\n[ \t]*"))
10183 org-table-last-alignment org-table-last-column-widths
10185 (if (not (fboundp func))
10186 (error "Cannot export orgtbl table to %s" target))
10187 (setq lines (org-table-clean-before-export lines))
10191 (if (string-match org-table-hline-regexp x)
10193 (org-split-string (org-trim x) "\\s-*|\\s-*")))
10195 (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
10197 (loop for i from (1- maxcol) downto 0 do
10198 (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
10199 (setq column (delq nil column))
10200 (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
10201 (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))
10202 (funcall func table nil)))
10204 (defun orgtbl-send-table (&optional maybe)
10205 "Send a tranformed version of this table to the receiver position.
10206 With argument MAYBE, fail quietly if no transformation is defined for
10210 (unless (org-at-table-p) (error "Not at a table"))
10211 ;; when non-interactive, we assume align has just happened.
10212 (when (interactive-p) (org-table-align))
10214 (goto-char (org-table-begin))
10215 (beginning-of-line 0)
10216 (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
10219 (error "Don't know how to transform this table."))))
10220 (let* ((name (match-string 1))
10222 (transform (intern (match-string 2)))
10223 (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
10224 (skip (plist-get params :skip))
10225 (skipcols (plist-get params :skipcols))
10226 (txt (buffer-substring-no-properties
10227 (org-table-begin) (org-table-end)))
10228 (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
10229 (lines (org-table-clean-before-export lines))
10230 (i0 (if org-table-clean-did-remove-column 2 1))
10233 (if (string-match org-table-hline-regexp x)
10235 (org-remove-by-index
10236 (org-split-string (org-trim x) "\\s-*|\\s-*")
10239 (fun (if (= i0 2) 'cdr 'identity))
10240 (org-table-last-alignment
10241 (org-remove-by-index (funcall fun org-table-last-alignment)
10243 (org-table-last-column-widths
10244 (org-remove-by-index (funcall fun org-table-last-column-widths)
10247 (unless (fboundp transform)
10248 (error "No such transformation function %s" transform))
10249 (setq txt (funcall transform table params))
10250 ;; Find the insertion place
10252 (goto-char (point-min))
10253 (unless (re-search-forward
10254 (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
10255 (error "Don't know where to insert translated table"))
10256 (goto-char (match-beginning 0))
10257 (beginning-of-line 2)
10259 (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
10260 (error "Cannot find end of insertion region"))
10261 (beginning-of-line 1)
10262 (delete-region beg (point))
10265 (message "Table converted and installed at receiver location"))))
10267 (defun org-remove-by-index (list indices &optional i0)
10268 "Remove the elements in LIST with indices in INDICES.
10269 First element has index 0, or I0 if given."
10272 (if (integerp indices) (setq indices (list indices)))
10273 (setq i0 (1- (or i0 0)))
10274 (delq :rm (mapcar (lambda (x)
10276 (if (memq i0 indices) :rm x))
10279 (defun orgtbl-toggle-comment ()
10280 "Comment or uncomment the orgtbl at point."
10282 (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
10283 (re2 (concat "^" orgtbl-line-start-regexp))
10284 (commented (save-excursion (beginning-of-line 1)
10285 (cond ((looking-at re1) t)
10286 ((looking-at re2) nil)
10287 (t (error "Not at an org table")))))
10288 (re (if commented re1 re2))
10291 (beginning-of-line 1)
10292 (while (looking-at re) (beginning-of-line 0))
10293 (beginning-of-line 2)
10295 (while (looking-at re) (beginning-of-line 2))
10296 (setq end (point)))
10297 (comment-region beg end (if commented '(4) nil))))
10299 (defun orgtbl-insert-radio-table ()
10300 "Insert a radio table template appropriate for this major mode."
10302 (let* ((e (assq major-mode orgtbl-radio-table-templates))
10305 (unless e (error "No radio table setup defined for %s" major-mode))
10306 (setq name (read-string "Table name: "))
10307 (while (string-match "%n" txt)
10308 (setq txt (replace-match name t t txt)))
10309 (or (bolp) (insert "\n"))
10314 (defun org-get-param (params header i sym &optional hsym)
10315 "Get parameter value for symbol SYM.
10316 If this is a header line, actually get the value for the symbol with an
10317 additional \"h\" inserted after the colon.
10318 If the value is a protperty list, get the element for the current column.
10319 Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
10320 (let ((val (plist-get params sym)))
10321 (and hsym header (setq val (or (plist-get params hsym) val)))
10322 (if (consp val) (plist-get val i) val)))
10324 (defun orgtbl-to-generic (table params)
10325 "Convert the orgtbl-mode TABLE to some other format.
10326 This generic routine can be used for many standard cases.
10327 TABLE is a list, each entry either the symbol `hline' for a horizontal
10328 separator line, or a list of fields for that line.
10329 PARAMS is a property list of parameters that can influence the conversion.
10330 For the generic converter, some parameters are obligatory: You need to
10331 specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
10332 :splice, you must have :tstart and :tend.
10334 Valid parameters are
10336 :tstart String to start the table. Ignored when :splice is t.
10337 :tend String to end the table. Ignored when :splice is t.
10339 :splice When set to t, return only table body lines, don't wrap
10340 them into :tstart and :tend. Default is nil.
10342 :hline String to be inserted on horizontal separation lines.
10343 May be nil to ignore hlines.
10345 :lstart String to start a new table line.
10346 :lend String to end a table line
10347 :sep Separator between two fields
10348 :lfmt Format for entire line, with enough %s to capture all fields.
10349 If this is present, :lstart, :lend, and :sep are ignored.
10350 :fmt A format to be used to wrap the field, should contain
10351 %s for the original field value. For example, to wrap
10352 everything in dollars, you could use :fmt \"$%s$\".
10353 This may also be a property list with column numbers and
10354 formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
10356 :hlstart :hlend :hlsep :hlfmt :hfmt
10357 Same as above, specific for the header lines in the table.
10358 All lines before the first hline are treated as header.
10359 If any of these is not present, the data line value is used.
10361 :efmt Use this format to print numbers with exponentials.
10362 The format should have %s twice for inserting mantissa
10363 and exponent, for example \"%s\\\\times10^{%s}\". This
10364 may also be a property list with column numbers and
10365 formats. :fmt will still be applied after :efmt.
10367 In addition to this, the parameters :skip and :skipcols are always handled
10368 directly by `orgtbl-send-table'. See manual."
10371 (splicep (plist-get p :splice))
10372 (hline (plist-get p :hline))
10373 rtn line i fm efm lfmt h)
10375 ;; Do we have a header?
10376 (if (and (not splicep) (listp (car table)) (memq 'hline table))
10381 (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
10383 ;; Now loop over all lines
10384 (while (setq line (pop table))
10385 (if (eq line 'hline)
10386 ;; A horizontal separator line
10387 (progn (if hline (push hline rtn))
10388 (setq h nil)) ; no longer in header
10389 ;; A normal line. Convert the fields, push line onto the result list
10395 fm (org-get-param p h i :fmt :hfmt)
10396 efm (org-get-param p h i :efmt))
10397 (if (and efm (string-match orgtbl-exp-regexp f))
10399 efm (match-string 1 f) (match-string 2 f))))
10400 (if fm (setq f (format fm f)))
10403 (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
10404 (push (apply 'format lfmt line) rtn)
10406 (org-get-param p h i :lstart :hlstart)
10407 (mapconcat 'identity line (org-get-param p h i :sep :hsep))
10408 (org-get-param p h i :lend :hlend))
10412 (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
10414 (mapconcat 'identity (nreverse rtn) "\n")))
10416 (defun orgtbl-to-latex (table params)
10417 "Convert the orgtbl-mode TABLE to LaTeX.
10418 TABLE is a list, each entry either the symbol `hline' for a horizontal
10419 separator line, or a list of fields for that line.
10420 PARAMS is a property list of parameters that can influence the conversion.
10421 Supports all parameters from `orgtbl-to-generic'. Most important for
10424 :splice When set to t, return only table body lines, don't wrap
10425 them into a tabular environment. Default is nil.
10427 :fmt A format to be used to wrap the field, should contain %s for the
10428 original field value. For example, to wrap everything in dollars,
10429 use :fmt \"$%s$\". This may also be a property list with column
10430 numbers and formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
10432 :efmt Format for transforming numbers with exponentials. The format
10433 should have %s twice for inserting mantissa and exponent, for
10434 example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
10435 This may also be a property list with column numbers and formats.
10437 The general parameters :skip and :skipcols have already been applied when
10438 this function is called."
10439 (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
10440 org-table-last-alignment ""))
10443 :tstart (concat "\\begin{tabular}{" alignment "}")
10444 :tend "\\end{tabular}"
10445 :lstart "" :lend " \\\\" :sep " & "
10446 :efmt "%s\\,(%s)" :hline "\\hline")))
10447 (orgtbl-to-generic table (org-combine-plists params2 params))))
10449 (defun orgtbl-to-html (table params)
10450 "Convert the orgtbl-mode TABLE to LaTeX.
10451 TABLE is a list, each entry either the symbol `hline' for a horizontal
10452 separator line, or a list of fields for that line.
10453 PARAMS is a property list of parameters that can influence the conversion.
10454 Currently this function recognizes the following parameters:
10456 :splice When set to t, return only table body lines, don't wrap
10457 them into a <table> environment. Default is nil.
10459 The general parameters :skip and :skipcols have already been applied when
10460 this function is called. The function does *not* use `orgtbl-to-generic',
10461 so you cannot specify parameters for it."
10462 (let* ((splicep (plist-get params :splice))
10464 ;; Just call the formatter we already have
10465 ;; We need to make text lines for it, so put the fields back together.
10466 (setq html (org-format-org-table-html
10471 (concat "| " (mapconcat 'identity x " | ") " |")))
10474 (if (string-match "\n+\\'" html)
10475 (setq html (replace-match "" t t html)))
10478 (defun orgtbl-to-texinfo (table params)
10479 "Convert the orgtbl-mode TABLE to TeXInfo.
10480 TABLE is a list, each entry either the symbol `hline' for a horizontal
10481 separator line, or a list of fields for that line.
10482 PARAMS is a property list of parameters that can influence the conversion.
10483 Supports all parameters from `orgtbl-to-generic'. Most important for
10486 :splice nil/t When set to t, return only table body lines, don't wrap
10487 them into a multitable environment. Default is nil.
10489 :fmt fmt A format to be used to wrap the field, should contain
10490 %s for the original field value. For example, to wrap
10491 everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
10492 This may also be a property list with column numbers and
10493 formats. for example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
10495 :cf \"f1 f2..\" The column fractions for the table. Bye default these
10496 are computed automatically from the width of the columns
10499 The general parameters :skip and :skipcols have already been applied when
10500 this function is called."
10501 (let* ((total (float (apply '+ org-table-last-column-widths)))
10502 (colfrac (or (plist-get params :cf)
10504 (lambda (x) (format "%.3f" (/ (float x) total)))
10505 org-table-last-column-widths " ")))
10508 :tstart (concat "@multitable @columnfractions " colfrac)
10509 :tend "@end multitable"
10510 :lstart "@item " :lend "" :sep " @tab "
10511 :hlstart "@headitem ")))
10512 (orgtbl-to-generic table (org-combine-plists params2 params))))
10516 ;;; Link abbreviations
10518 (defun org-link-expand-abbrev (link)
10519 "Apply replacements as defined in `org-link-abbrev-alist."
10520 (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
10521 (let* ((key (match-string 1 link))
10522 (as (or (assoc key org-link-abbrev-alist-local)
10523 (assoc key org-link-abbrev-alist)))
10524 (tag (and (match-end 2) (match-string 3 link)))
10528 (setq rpl (cdr as))
10530 ((symbolp rpl) (funcall rpl tag))
10531 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
10532 (t (concat rpl tag)))))
10535 ;;; Storing and inserting links
10537 (defvar org-insert-link-history nil
10538 "Minibuffer history for links inserted with `org-insert-link'.")
10540 (defvar org-stored-links nil
10541 "Contains the links stored with `org-store-link'.")
10543 (defvar org-store-link-plist nil
10544 "Plist with info about the most recently link created with `org-store-link'.")
10546 (defvar org-link-protocols nil
10547 "Link protocols added to Org-mode using `org-add-link-type'.")
10549 (defvar org-store-link-functions nil
10550 "List of functions that are called to create and store a link.
10551 Each function will be called in turn until one returns a non-nil
10552 value. Each function should check if it is responsible for creating
10553 this link (for example by looking at the major mode).
10554 If not, it must exit and return nil.
10555 If yes, it should return a non-nil value after a calling
10556 `org-store-link-properties' with a list of properties and values.
10557 Special properties are:
10559 :type The link prefix. like \"http\". This must be given.
10560 :link The link, like \"http://www.astro.uva.nl/~dominik\".
10561 This is obligatory as well.
10562 :description Optional default description for the second pair
10563 of brackets in an Org-mode link. The user can still change
10564 this when inserting this link into an Org-mode buffer.
10566 In addition to these, any additional properties can be specified
10567 and then used in remember templates.")
10569 (defun org-add-link-type (type &optional follow publish)
10570 "Add TYPE to the list of `org-link-types'.
10571 Re-compute all regular expressions depending on `org-link-types'
10572 FOLLOW and PUBLISH are two functions. Both take the link path as
10574 FOLLOW should do whatever is necessary to follow the link, for example
10575 to find a file or display a mail message.
10576 PUBLISH takes the path and retuns the string that should be used when
10577 this document is published."
10578 (add-to-list 'org-link-types type t)
10579 (org-make-link-regexps)
10580 (add-to-list 'org-link-protocols
10581 (list type follow publish)))
10583 (defun org-add-agenda-custom-command (entry)
10584 "Replace or add a command in `org-agenda-custom-commands'.
10585 This is mostly for hacking and trying a new command - once the command
10586 works you probably want to add it to `org-agenda-custom-commands' for good."
10587 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
10589 (setcdr ass (cdr entry))
10590 (push entry org-agenda-custom-commands))))
10593 (defun org-store-link (arg)
10594 "\\<org-mode-map>Store an org-link to the current location.
10595 This link can later be inserted into an org-buffer with
10596 \\[org-insert-link].
10597 For some link types, a prefix arg is interpreted:
10598 For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
10599 For file links, arg negates `org-context-in-file-links'."
10601 (setq org-store-link-plist nil) ; reset
10602 (let (link cpltxt desc description search txt)
10605 ((run-hook-with-args-until-success 'org-store-link-functions)
10606 (setq link (plist-get org-store-link-plist :link)
10607 desc (or (plist-get org-store-link-plist :description) link)))
10609 ((eq major-mode 'bbdb-mode)
10610 (let ((name (bbdb-record-name (bbdb-current-record)))
10611 (company (bbdb-record-getprop (bbdb-current-record) 'company)))
10612 (setq cpltxt (concat "bbdb:" (or name company))
10613 link (org-make-link cpltxt))
10614 (org-store-link-props :type "bbdb" :name name :company company)))
10616 ((eq major-mode 'Info-mode)
10617 (setq link (org-make-link "info:"
10618 (file-name-nondirectory Info-current-file)
10619 ":" Info-current-node))
10620 (setq cpltxt (concat (file-name-nondirectory Info-current-file)
10621 ":" Info-current-node))
10622 (org-store-link-props :type "info" :file Info-current-file
10623 :node Info-current-node))
10625 ((eq major-mode 'calendar-mode)
10626 (let ((cd (calendar-cursor-to-date)))
10628 (format-time-string
10629 (car org-time-stamp-formats)
10630 (apply 'encode-time
10631 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
10633 (org-store-link-props :type "calendar" :date cd)))
10635 ((or (eq major-mode 'vm-summary-mode)
10636 (eq major-mode 'vm-presentation-mode))
10637 (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
10638 (vm-follow-summary-cursor)
10640 (vm-select-folder-buffer)
10641 (let* ((message (car vm-message-pointer))
10642 (folder buffer-file-name)
10643 (subject (vm-su-subject message))
10644 (to (vm-get-header-contents message "To"))
10645 (from (vm-get-header-contents message "From"))
10646 (message-id (vm-su-message-id message)))
10647 (org-store-link-props :type "vm" :from from :to to :subject subject
10648 :message-id message-id)
10649 (setq message-id (org-remove-angle-brackets message-id))
10650 (setq folder (abbreviate-file-name folder))
10651 (if (string-match (concat "^" (regexp-quote vm-folder-directory))
10653 (setq folder (replace-match "" t t folder)))
10654 (setq cpltxt (org-email-link-description))
10655 (setq link (org-make-link "vm:" folder "#" message-id)))))
10657 ((eq major-mode 'wl-summary-mode)
10658 (let* ((msgnum (wl-summary-message-number))
10659 (message-id (elmo-message-field wl-summary-buffer-elmo-folder
10660 msgnum 'message-id))
10662 (if (fboundp 'elmo-message-entity)
10663 (elmo-message-entity
10664 wl-summary-buffer-elmo-folder msgnum)
10665 (elmo-msgdb-overview-get-entity
10666 msgnum (wl-summary-buffer-msgdb))))
10667 (from (wl-summary-line-from))
10668 (to (car (elmo-message-entity-field wl-message-entity 'to)))
10669 (subject (let (wl-thr-indent-string wl-parent-message-entity)
10670 (wl-summary-line-subject))))
10671 (org-store-link-props :type "wl" :from from :to to
10672 :subject subject :message-id message-id)
10673 (setq message-id (org-remove-angle-brackets message-id))
10674 (setq cpltxt (org-email-link-description))
10675 (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
10678 ((or (equal major-mode 'mh-folder-mode)
10679 (equal major-mode 'mh-show-mode))
10680 (let ((from (org-mhe-get-header "From:"))
10681 (to (org-mhe-get-header "To:"))
10682 (message-id (org-mhe-get-header "Message-Id:"))
10683 (subject (org-mhe-get-header "Subject:")))
10684 (org-store-link-props :type "mh" :from from :to to
10685 :subject subject :message-id message-id)
10686 (setq cpltxt (org-email-link-description))
10687 (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
10688 (org-remove-angle-brackets message-id)))))
10690 ((eq major-mode 'rmail-mode)
10693 (rmail-narrow-to-non-pruned-header)
10694 (let ((folder buffer-file-name)
10695 (message-id (mail-fetch-field "message-id"))
10696 (from (mail-fetch-field "from"))
10697 (to (mail-fetch-field "to"))
10698 (subject (mail-fetch-field "subject")))
10699 (org-store-link-props
10700 :type "rmail" :from from :to to
10701 :subject subject :message-id message-id)
10702 (setq message-id (org-remove-angle-brackets message-id))
10703 (setq cpltxt (org-email-link-description))
10704 (setq link (org-make-link "rmail:" folder "#" message-id))))))
10706 ((eq major-mode 'gnus-group-mode)
10707 (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
10708 (gnus-group-group-name)) ; version
10709 ((fboundp 'gnus-group-name)
10712 (unless group (error "Not on a group"))
10713 (org-store-link-props :type "gnus" :group group)
10714 (setq cpltxt (concat
10715 (if (org-xor arg org-usenet-links-prefer-google)
10716 "http://groups.google.com/groups?group="
10719 link (org-make-link cpltxt))))
10721 ((memq major-mode '(gnus-summary-mode gnus-article-mode))
10722 (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
10723 (let* ((group gnus-newsgroup-name)
10724 (article (gnus-summary-article-number))
10725 (header (gnus-summary-article-header article))
10726 (from (mail-header-from header))
10727 (message-id (mail-header-id header))
10728 (date (mail-header-date header))
10729 (subject (gnus-summary-subject-string)))
10730 (org-store-link-props :type "gnus" :from from :subject subject
10731 :message-id message-id :group group)
10732 (setq cpltxt (org-email-link-description))
10733 (if (org-xor arg org-usenet-links-prefer-google)
10737 (format "http://groups.google.com/groups?as_umsgid=%s"
10738 (org-fixup-message-id-for-http message-id))))
10739 (setq link (org-make-link "gnus:" group
10740 "#" (number-to-string article))))))
10742 ((eq major-mode 'w3-mode)
10743 (setq cpltxt (url-view-url t)
10744 link (org-make-link cpltxt))
10745 (org-store-link-props :type "w3" :url (url-view-url t)))
10747 ((eq major-mode 'w3m-mode)
10748 (setq cpltxt (or w3m-current-title w3m-current-url)
10749 link (org-make-link w3m-current-url))
10750 (org-store-link-props :type "w3m" :url (url-view-url t)))
10752 ((setq search (run-hook-with-args-until-success
10753 'org-create-file-search-functions))
10754 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
10756 (setq cpltxt (or description link)))
10758 ((eq major-mode 'image-mode)
10759 (setq cpltxt (concat "file:"
10760 (abbreviate-file-name buffer-file-name))
10761 link (org-make-link cpltxt))
10762 (org-store-link-props :type "image" :file buffer-file-name))
10764 ((eq major-mode 'dired-mode)
10765 ;; link to the file in the current line
10766 (setq cpltxt (concat "file:"
10767 (abbreviate-file-name
10769 (dired-get-filename nil t))))
10770 link (org-make-link cpltxt)))
10772 ((and buffer-file-name (org-mode-p))
10773 ;; Just link to current headline
10774 (setq cpltxt (concat "file:"
10775 (abbreviate-file-name buffer-file-name)))
10776 ;; Add a context search string
10777 (when (org-xor org-context-in-file-links arg)
10778 ;; Check if we are on a target
10779 (if (org-in-regexp "<<\\(.*?\\)>>")
10780 (setq cpltxt (concat cpltxt "::" (match-string 1)))
10782 ((org-on-heading-p) nil)
10783 ((org-region-active-p)
10784 (buffer-substring (region-beginning) (region-end)))
10785 (t (buffer-substring (point-at-bol) (point-at-eol)))))
10786 (when (or (null txt) (string-match "\\S-" txt))
10788 (concat cpltxt "::" (org-make-org-heading-search-string txt))
10790 (if (string-match "::\\'" cpltxt)
10791 (setq cpltxt (substring cpltxt 0 -2)))
10792 (setq link (org-make-link cpltxt)))
10795 ;; Just link to this file here.
10796 (setq cpltxt (concat "file:"
10797 (abbreviate-file-name buffer-file-name)))
10798 ;; Add a context string
10799 (when (org-xor org-context-in-file-links arg)
10800 (setq txt (if (org-region-active-p)
10801 (buffer-substring (region-beginning) (region-end))
10802 (buffer-substring (point-at-bol) (point-at-eol))))
10803 ;; Only use search option if there is some text.
10804 (when (string-match "\\S-" txt)
10806 (concat cpltxt "::" (org-make-org-heading-search-string txt))
10808 (setq link (org-make-link cpltxt)))
10811 (error "Cannot link to a buffer which is not visiting a file"))
10813 (t (setq link nil)))
10815 (if (consp link) (setq cpltxt (car link) link (cdr link)))
10816 (setq link (or link cpltxt)
10817 desc (or desc cpltxt))
10818 (if (equal desc "NONE") (setq desc nil))
10820 (if (and (interactive-p) link)
10822 (setq org-stored-links
10823 (cons (list cpltxt link desc) org-stored-links))
10824 (message "Stored: %s" (or cpltxt link)))
10825 (org-make-link-string link desc))))
10827 (defun org-store-link-props (&rest plist)
10828 "Store link properties, extract names and addresses."
10830 (when (setq x (plist-get plist :from))
10831 (setq adr (mail-extract-address-components x))
10832 (plist-put plist :fromname (car adr))
10833 (plist-put plist :fromaddress (nth 1 adr)))
10834 (when (setq x (plist-get plist :to))
10835 (setq adr (mail-extract-address-components x))
10836 (plist-put plist :toname (car adr))
10837 (plist-put plist :toaddress (nth 1 adr))))
10838 (let ((from (plist-get plist :from))
10839 (to (plist-get plist :to)))
10840 (when (and from to org-from-is-user-regexp)
10841 (plist-put plist :fromto
10842 (if (string-match org-from-is-user-regexp from)
10844 (concat "from %f")))))
10845 (setq org-store-link-plist plist))
10847 (defun org-email-link-description (&optional fmt)
10848 "Return the description part of an email link.
10849 This takes information from `org-store-link-plist' and formats it
10850 according to FMT (default from `org-email-link-description-format')."
10851 (setq fmt (or fmt org-email-link-description-format))
10852 (let* ((p org-store-link-plist)
10853 (to (plist-get p :toaddress))
10854 (from (plist-get p :fromaddress))
10857 (cons "%c" (plist-get p :fromto))
10858 (cons "%F" (plist-get p :from))
10859 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
10860 (cons "%T" (plist-get p :to))
10861 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
10862 (cons "%s" (plist-get p :subject))
10863 (cons "%m" (plist-get p :message-id)))))
10864 (when (string-match "%c" fmt)
10865 ;; Check if the user wrote this message
10866 (if (and org-from-is-user-regexp from to
10867 (save-match-data (string-match org-from-is-user-regexp from)))
10868 (setq fmt (replace-match "to %t" t t fmt))
10869 (setq fmt (replace-match "from %f" t t fmt))))
10870 (org-replace-escapes fmt table)))
10872 (defun org-make-org-heading-search-string (&optional string heading)
10873 "Make search string for STRING or current headline."
10875 (let ((s (or string (org-get-heading))))
10876 (unless (and string (not heading))
10877 ;; We are using a headline, clean up garbage in there.
10878 (if (string-match org-todo-regexp s)
10879 (setq s (replace-match "" t t s)))
10880 (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
10881 (setq s (replace-match "" t t s)))
10882 (setq s (org-trim s))
10883 (if (string-match (concat "^\\(" org-quote-string "\\|"
10884 org-comment-string "\\)") s)
10885 (setq s (replace-match "" t t s)))
10886 (while (string-match org-ts-regexp s)
10887 (setq s (replace-match "" t t s))))
10888 (while (string-match "[^a-zA-Z_0-9 \t]+" s)
10889 (setq s (replace-match " " t t s)))
10890 (or string (setq s (concat "*" s))) ; Add * for headlines
10891 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
10893 (defun org-make-link (&rest strings)
10894 "Concatenate STRINGS."
10895 (apply 'concat strings))
10897 (defun org-make-link-string (link &optional description)
10898 "Make a link with brackets, consisting of LINK and DESCRIPTION."
10899 (unless (string-match "\\S-" link)
10900 (error "Empty link"))
10901 (when (stringp description)
10902 ;; Remove brackets from the description, they are fatal.
10903 (while (string-match "\\[\\|\\]" description)
10904 (setq description (replace-match "" t t description))))
10905 (when (equal (org-link-escape link) description)
10906 ;; No description needed, it is identical
10907 (setq description nil))
10908 (when (and (not description)
10909 (not (equal link (org-link-escape link))))
10910 (setq description link))
10911 (concat "[[" (org-link-escape link) "]"
10912 (if description (concat "[" description "]") "")
10915 (defconst org-link-escape-chars
10916 '((" " . "%20") ("\340" . "%E0")
10917 ("\342" . "%E2") ("\347" . "%E7")
10918 ("\350" . "%E8") ("\351" . "%E9")
10919 ("\352" . "%EA") ("\356" . "%EE")
10920 ("\364" . "%F4") ("\371" . "%F9")
10921 ("\373" . "%FB") (";" . "%3B")
10922 ("?" . "%3F") ("=" . "%3D")
10924 "Association list of escapes for some characters problematic in links.")
10926 (defun org-link-escape (text)
10927 "Escape charaters in TEXT that are problematic for links."
10929 (let ((re (mapconcat (lambda (x) (regexp-quote (car x)))
10930 org-link-escape-chars "\\|")))
10931 (while (string-match re text)
10934 (cdr (assoc (match-string 0 text) org-link-escape-chars))
10938 (defun org-link-unescape (text)
10939 "Reverse the action of `org-link-escape'."
10941 (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
10942 org-link-escape-chars "\\|")))
10943 (while (string-match re text)
10946 (car (rassoc (match-string 0 text) org-link-escape-chars))
10950 (defun org-xor (a b)
10954 (defun org-get-header (header)
10955 "Find a header field in the current buffer."
10957 (goto-char (point-min))
10958 (let ((case-fold-search t) s)
10961 (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
10962 (setq s (match-string 1)))
10963 (while (string-match "\"" s)
10964 (setq s (replace-match "" t t s)))
10965 (if (string-match "[<(].*" s)
10966 (setq s (replace-match "" t t s))))
10967 ((eq header 'message-id)
10968 (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
10969 (setq s (match-string 1))))
10970 ((eq header 'subject)
10971 (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
10972 (setq s (match-string 1)))))
10973 (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
10974 (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
10978 (defun org-fixup-message-id-for-http (s)
10979 "Replace special characters in a message id, so it can be used in an http query."
10980 (while (string-match "<" s)
10981 (setq s (replace-match "%3C" t t s)))
10982 (while (string-match ">" s)
10983 (setq s (replace-match "%3E" t t s)))
10984 (while (string-match "@" s)
10985 (setq s (replace-match "%40" t t s)))
10989 (defun org-insert-link-global ()
10990 "Insert a link like Org-mode does.
10991 This command can be called in any mode to insert a link in Org-mode syntax."
10993 (org-run-like-in-org-mode 'org-insert-link))
10995 (defun org-insert-link (&optional complete-file)
10996 "Insert a link. At the prompt, enter the link.
10998 Completion can be used to select a link previously stored with
10999 `org-store-link'. When the empty string is entered (i.e. if you just
11000 press RET at the prompt), the link defaults to the most recently
11001 stored link. As SPC triggers completion in the minibuffer, you need to
11002 use M-SPC or C-q SPC to force the insertion of a space character.
11004 You will also be prompted for a description, and if one is given, it will
11005 be displayed in the buffer instead of the link.
11007 If there is already a link at point, this command will allow you to edit link
11008 and description parts.
11010 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
11011 selected using completion. The path to the file will be relative to
11012 the current directory if the file is in the current directory or a
11013 subdirectory. Otherwise, the link will be the absolute path as
11014 completed in the minibuffer (i.e. normally ~/path/to/file).
11016 With two \\[universal-argument] prefixes, enforce an absolute path even if the file
11017 is in the current directory or below.
11018 With three \\[universal-argument] prefixes, negate the meaning of
11019 `org-keep-stored-link-after-insertion'."
11021 (let ((wcf (current-window-configuration))
11022 (region (if (org-region-active-p)
11023 (prog1 (buffer-substring (region-beginning) (region-end))
11024 (delete-region (region-beginning) (region-end)))))
11025 tmphist ; byte-compile incorrectly complains about this
11026 link desc entry remove file)
11028 ((org-in-regexp org-bracket-link-regexp 1)
11029 ;; We do have a link at point, and we are going to edit it.
11030 (setq remove (list (match-beginning 0) (match-end 0)))
11031 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
11032 (setq link (read-string "Link: "
11034 (org-match-string-no-properties 1)))))
11035 ((or (org-in-regexp org-angle-link-re)
11036 (org-in-regexp org-plain-link-re))
11037 ;; Convert to bracket link
11038 (setq remove (list (match-beginning 0) (match-end 0))
11039 link (read-string "Link: "
11040 (org-remove-angle-brackets (match-string 0)))))
11041 ((equal complete-file '(4))
11042 ;; Completing read for file names.
11043 (setq file (read-file-name "File: "))
11044 (let ((pwd (file-name-as-directory (expand-file-name ".")))
11045 (pwd1 (file-name-as-directory (abbreviate-file-name
11046 (expand-file-name ".")))))
11048 ((equal complete-file '(16))
11049 (setq link (org-make-link
11051 (abbreviate-file-name (expand-file-name file)))))
11052 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
11053 (setq link (org-make-link "file:" (match-string 1 file))))
11054 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
11055 (expand-file-name file))
11056 (setq link (org-make-link
11057 "file:" (match-string 1 (expand-file-name file)))))
11058 (t (setq link (org-make-link "file:" file))))))
11060 ;; Read link, with completion for stored links.
11061 (with-output-to-temp-buffer "*Org Links*"
11062 (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
11063 (when org-stored-links
11064 (princ "\nStored links are available with <up>/<down> (most recent with RET):\n\n")
11065 (princ (mapconcat 'car (reverse org-stored-links) "\n"))))
11066 (let ((cw (selected-window)))
11067 (select-window (get-buffer-window "*Org Links*"))
11068 (shrink-window-if-larger-than-buffer)
11069 (setq truncate-lines t)
11070 (select-window cw))
11071 ;; Fake a link history, containing the stored links.
11072 (setq tmphist (append (mapcar 'car org-stored-links)
11073 org-insert-link-history))
11075 (setq link (org-completing-read
11078 (mapcar (lambda (x) (list (concat (car x) ":")))
11079 (append org-link-abbrev-alist-local org-link-abbrev-alist))
11080 (mapcar (lambda (x) (list (concat x ":")))
11084 (or (car (car org-stored-links)))))
11085 (set-window-configuration wcf)
11086 (kill-buffer "*Org Links*"))
11087 (setq entry (assoc link org-stored-links))
11088 (or entry (push link org-insert-link-history))
11089 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
11090 (not org-keep-stored-link-after-insertion))
11091 (setq org-stored-links (delq (assoc link org-stored-links)
11092 org-stored-links)))
11093 (setq link (if entry (nth 1 entry) link)
11094 desc (or region desc (nth 2 entry)))))
11096 (if (string-match org-plain-link-re link)
11097 ;; URL-like link, normalize the use of angular brackets.
11098 (setq link (org-make-link (org-remove-angle-brackets link))))
11100 ;; Check if we are linking to the current file with a search option
11101 ;; If yes, simplify the link by using only the search option.
11102 (when (and buffer-file-name
11103 (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
11104 (let* ((path (match-string 1 link))
11105 (case-fold-search nil)
11106 (search (match-string 2 link)))
11108 (if (equal (file-truename buffer-file-name) (file-truename path))
11109 ;; We are linking to this same file, with a search option
11110 (setq link search)))))
11112 ;; Check if we can/should use a relative path. If yes, simplify the link
11113 (when (string-match "\\<file:\\(.*\\)" link)
11114 (let* ((path (match-string 1 link))
11115 (case-fold-search nil))
11117 ((eq org-link-file-path-type 'absolute)
11118 (setq path (abbreviate-file-name (expand-file-name path))))
11119 ((eq org-link-file-path-type 'noabbrev)
11120 (setq path (expand-file-name path)))
11121 ((eq org-link-file-path-type 'relative)
11122 (setq path (file-relative-name path)))
11125 (if (string-match (concat "^" (regexp-quote
11126 (file-name-as-directory
11127 (expand-file-name "."))))
11128 (expand-file-name path))
11129 ;; We are linking a file with relative path name.
11130 (setq path (substring (expand-file-name path)
11131 (match-end 0)))))))
11132 (setq link (concat "file:" path))))
11134 (setq desc (read-string "Description: " desc))
11135 (unless (string-match "\\S-" desc) (setq desc nil))
11136 (if remove (apply 'delete-region remove))
11137 (insert (org-make-link-string link desc))))
11139 (defun org-completing-read (&rest args)
11140 (let ((minibuffer-local-completion-map
11141 (copy-keymap minibuffer-local-completion-map)))
11142 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
11143 (apply 'completing-read args)))
11145 ;;; Opening/following a link
11146 (defvar org-link-search-failed nil)
11148 (defun org-next-link ()
11149 "Move forward to the next link.
11150 If the link is in hidden text, expose it."
11152 (when (and org-link-search-failed (eq this-command last-command))
11153 (goto-char (point-min))
11154 (message "Link search wrapped back to beginning of buffer"))
11155 (setq org-link-search-failed nil)
11156 (let* ((pos (point))
11158 (a (assoc :link ct)))
11159 (if a (goto-char (nth 2 a)))
11160 (if (re-search-forward org-any-link-re nil t)
11162 (goto-char (match-beginning 0))
11163 (if (org-invisible-p) (org-show-context)))
11165 (setq org-link-search-failed t)
11166 (error "No further link found"))))
11168 (defun org-previous-link ()
11169 "Move backward to the previous link.
11170 If the link is in hidden text, expose it."
11172 (when (and org-link-search-failed (eq this-command last-command))
11173 (goto-char (point-max))
11174 (message "Link search wrapped back to end of buffer"))
11175 (setq org-link-search-failed nil)
11176 (let* ((pos (point))
11178 (a (assoc :link ct)))
11179 (if a (goto-char (nth 1 a)))
11180 (if (re-search-backward org-any-link-re nil t)
11182 (goto-char (match-beginning 0))
11183 (if (org-invisible-p) (org-show-context)))
11185 (setq org-link-search-failed t)
11186 (error "No further link found"))))
11188 (defun org-find-file-at-mouse (ev)
11189 "Open file link or URL at mouse."
11191 (mouse-set-point ev)
11192 (org-open-at-point 'in-emacs))
11194 (defun org-open-at-mouse (ev)
11195 "Open file link or URL at mouse."
11197 (mouse-set-point ev)
11198 (org-open-at-point))
11200 (defvar org-window-config-before-follow-link nil
11201 "The window configuration before following a link.
11202 This is saved in case the need arises to restore it.")
11204 (defvar org-open-link-marker (make-marker)
11205 "Marker pointing to the location where `org-open-at-point; was called.")
11208 (defun org-open-at-point-global ()
11209 "Follow a link like Org-mode does.
11210 This command can be called in any mode to follow a link that has
11213 (org-run-like-in-org-mode 'org-open-at-point))
11215 (defun org-open-at-point (&optional in-emacs)
11216 "Open link at or after point.
11217 If there is no link at point, this function will search forward up to
11218 the end of the current subtree.
11219 Normally, files will be opened by an appropriate application. If the
11220 optional argument IN-EMACS is non-nil, Emacs will visit the file."
11222 (move-marker org-open-link-marker (point))
11223 (setq org-window-config-before-follow-link (current-window-configuration))
11224 (org-remove-occur-highlights nil nil t)
11225 (if (org-at-timestamp-p t)
11226 (org-follow-timestamp-link)
11227 (let (type path link line search (pos (point)))
11230 (skip-chars-forward "^]\n\r")
11231 (when (org-in-regexp org-bracket-link-regexp)
11232 (setq link (org-link-unescape (org-match-string-no-properties 1)))
11233 (while (string-match " *\n *" link)
11234 (setq link (replace-match " " t t link)))
11235 (setq link (org-link-expand-abbrev link))
11236 (if (string-match org-link-re-with-space2 link)
11237 (setq type (match-string 1 link) path (match-string 2 link))
11238 (setq type "thisfile" path link))
11241 (when (get-text-property (point) 'org-linked-text)
11242 (setq type "thisfile"
11243 pos (if (get-text-property (1+ (point)) 'org-linked-text)
11244 (1+ (point)) (point))
11245 path (buffer-substring
11246 (previous-single-property-change pos 'org-linked-text)
11247 (next-single-property-change pos 'org-linked-text)))
11251 (when (or (org-in-regexp org-angle-link-re)
11252 (org-in-regexp org-plain-link-re))
11253 (setq type (match-string 1) path (match-string 2))
11255 (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
11256 (setq type "tree-match"
11257 path (match-string 1))
11260 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
11262 path (match-string 1))
11263 (while (string-match ":" path)
11264 (setq path (replace-match "+" t t path)))
11265 (throw 'match t))))
11267 (error "No link found"))
11268 ;; Remove any trailing spaces in path
11269 (if (string-match " +\\'" path)
11270 (setq path (replace-match "" t t path)))
11274 ((assoc type org-link-protocols)
11275 (funcall (nth 1 (assoc type org-link-protocols)) path))
11277 ((equal type "mailto")
11278 (let ((cmd (car org-link-mailto-program))
11279 (args (cdr org-link-mailto-program)) args1
11280 (address path) (subject "") a)
11281 (if (string-match "\\(.*\\)::\\(.*\\)" path)
11282 (setq address (match-string 1 path)
11283 subject (org-link-escape (match-string 2 path))))
11286 ((not (stringp (car args))) (push (pop args) args1))
11287 (t (setq a (pop args))
11288 (if (string-match "%a" a)
11289 (setq a (replace-match address t t a)))
11290 (if (string-match "%s" a)
11291 (setq a (replace-match subject t t a)))
11293 (apply cmd (nreverse args1))))
11295 ((member type '("http" "https" "ftp" "news"))
11296 (browse-url (concat type ":" path)))
11298 ((string= type "tags")
11299 (org-tags-view in-emacs path))
11300 ((string= type "thisfile")
11302 (switch-to-buffer-other-window
11303 (org-get-buffer-for-internal-link (current-buffer)))
11304 (org-mark-ring-push))
11305 (let ((cmd `(org-link-search
11307 ,(cond ((equal in-emacs '(4)) 'occur)
11308 ((equal in-emacs '(16)) 'org-occur)
11311 (condition-case nil (eval cmd)
11312 (error (progn (widen) (eval cmd))))))
11314 ((string= type "tree-match")
11315 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
11317 ((string= type "file")
11318 (if (string-match "::\\([0-9]+\\)\\'" path)
11319 (setq line (string-to-number (match-string 1 path))
11320 path (substring path 0 (match-beginning 0)))
11321 (if (string-match "::\\(.+\\)\\'" path)
11322 (setq search (match-string 1 path)
11323 path (substring path 0 (match-beginning 0)))))
11324 (org-open-file path in-emacs line search))
11326 ((string= type "news")
11327 (org-follow-gnus-link path))
11329 ((string= type "bbdb")
11330 (org-follow-bbdb-link path))
11332 ((string= type "info")
11333 (org-follow-info-link path))
11335 ((string= type "gnus")
11336 (let (group article)
11337 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
11338 (error "Error in Gnus link"))
11339 (setq group (match-string 1 path)
11340 article (match-string 3 path))
11341 (org-follow-gnus-link group article)))
11343 ((string= type "vm")
11344 (let (folder article)
11345 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
11346 (error "Error in VM link"))
11347 (setq folder (match-string 1 path)
11348 article (match-string 3 path))
11349 ;; in-emacs is the prefix arg, will be interpreted as read-only
11350 (org-follow-vm-link folder article in-emacs)))
11352 ((string= type "wl")
11353 (let (folder article)
11354 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
11355 (error "Error in Wanderlust link"))
11356 (setq folder (match-string 1 path)
11357 article (match-string 3 path))
11358 (org-follow-wl-link folder article)))
11360 ((string= type "mhe")
11361 (let (folder article)
11362 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
11363 (error "Error in MHE link"))
11364 (setq folder (match-string 1 path)
11365 article (match-string 3 path))
11366 (org-follow-mhe-link folder article)))
11368 ((string= type "rmail")
11369 (let (folder article)
11370 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
11371 (error "Error in RMAIL link"))
11372 (setq folder (match-string 1 path)
11373 article (match-string 3 path))
11374 (org-follow-rmail-link folder article)))
11376 ((string= type "shell")
11378 ;; FIXME: the following is only for backward compatibility
11379 (while (string-match "@{" cmd) (setq cmd (replace-match "<" t t cmd)))
11380 (while (string-match "@}" cmd) (setq cmd (replace-match ">" t t cmd)))
11381 (if (or (not org-confirm-shell-link-function)
11382 (funcall org-confirm-shell-link-function
11383 (format "Execute \"%s\" in shell? "
11384 (org-add-props cmd nil
11385 'face 'org-warning))))
11387 (message "Executing %s" cmd)
11388 (shell-command cmd))
11391 ((string= type "elisp")
11393 (if (or (not org-confirm-elisp-link-function)
11394 (funcall org-confirm-elisp-link-function
11395 (format "Execute \"%s\" as elisp? "
11396 (org-add-props cmd nil
11397 'face 'org-warning))))
11398 (message "%s => %s" cmd (eval (read cmd)))
11402 (browse-url-at-point)))))
11403 (move-marker org-open-link-marker nil))
11408 (defvar org-create-file-search-functions nil
11409 "List of functions to construct the right search string for a file link.
11410 These functions are called in turn with point at the location to
11411 which the link should point.
11413 A function in the hook should first test if it would like to
11414 handle this file type, for example by checking the major-mode or
11415 the file extension. If it decides not to handle this file, it
11416 should just return nil to give other functions a chance. If it
11417 does handle the file, it must return the search string to be used
11418 when following the link. The search string will be part of the
11419 file link, given after a double colon, and `org-open-at-point'
11420 will automatically search for it. If special measures must be
11421 taken to make the search successful, another function should be
11422 added to the companion hook `org-execute-file-search-functions',
11425 A function in this hook may also use `setq' to set the variable
11426 `description' to provide a suggestion for the descriptive text to
11427 be used for this link when it gets inserted into an Org-mode
11428 buffer with \\[org-insert-link].")
11430 (defvar org-execute-file-search-functions nil
11431 "List of functions to execute a file search triggered by a link.
11433 Functions added to this hook must accept a single argument, the
11434 search string that was part of the file link, the part after the
11435 double colon. The function must first check if it would like to
11436 handle this search, for example by checking the major-mode or the
11437 file extension. If it decides not to handle this search, it
11438 should just return nil to give other functions a chance. If it
11439 does handle the search, it must return a non-nil value to keep
11440 other functions from trying.
11442 Each function can access the current prefix argument through the
11443 variable `current-prefix-argument'. Note that a single prefix is
11444 used to force opening a link in Emacs, so it may be good to only
11445 use a numeric or double prefix to guide the search function.
11447 In case this is needed, a function in this hook can also restore
11448 the window configuration before `org-open-at-point' was called using:
11450 (set-window-configuration org-window-config-before-follow-link)")
11452 (defun org-link-search (s &optional type avoid-pos)
11453 "Search for a link search option.
11454 If S is surrounded by forward slashes, it is interpreted as a
11455 regular expression. In org-mode files, this will create an `org-occur'
11456 sparse tree. In ordinary files, `occur' will be used to list matches.
11457 If the current buffer is in `dired-mode', grep will be used to search
11458 in all files. If AVOID-POS is given, ignore matches near that position."
11459 (let ((case-fold-search t)
11460 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
11461 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
11462 (append '(("") (" ") ("\t") ("\n"))
11463 org-emphasis-alist)
11467 words re0 re1 re2 re3 re4 re5 re2a reall)
11469 ;; First check if there are any special
11470 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
11471 ;; Now try the builtin stuff
11473 (goto-char (point-min))
11476 (concat "<<" (regexp-quote s0) ">>") nil t)
11477 (setq pos (match-beginning 0))))
11478 ;; There is an exact target for this
11480 ((string-match "^/\\(.*\\)/$" s)
11481 ;; A regular expression
11484 (org-occur (match-string 1 s)))
11485 ;;((eq major-mode 'dired-mode)
11486 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
11487 (t (org-do-occur (match-string 1 s)))))
11489 ;; A normal search strings
11490 (when (equal (string-to-char s) ?*)
11491 ;; Anchor on headlines, post may include tags.
11492 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
11493 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
11494 s (substring s 1)))
11495 (remove-text-properties
11497 '(face nil mouse-face nil keymap nil fontified nil) s)
11498 ;; Make a series of regular expressions to find a match
11499 (setq words (org-split-string s "[ \n\r\t]+")
11500 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
11501 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
11503 re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
11504 re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
11505 re1 (concat pre re2 post)
11506 re3 (concat pre re4 post)
11507 re5 (concat pre ".*" re4)
11508 re2 (concat pre re2)
11509 re2a (concat pre re2a)
11510 re4 (concat pre re4)
11511 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
11512 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
11516 ((eq type 'org-occur) (org-occur reall))
11517 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
11518 (t (goto-char (point-min))
11519 (if (or (org-search-not-self 1 re0 nil t)
11520 (org-search-not-self 1 re1 nil t)
11521 (org-search-not-self 1 re2 nil t)
11522 (org-search-not-self 1 re2a nil t)
11523 (org-search-not-self 1 re3 nil t)
11524 (org-search-not-self 1 re4 nil t)
11525 (org-search-not-self 1 re5 nil t)
11527 (goto-char (match-beginning 1))
11529 (error "No match")))))
11531 ;; Normal string-search
11532 (goto-char (point-min))
11533 (if (search-forward s nil t)
11534 (goto-char (match-beginning 0))
11535 (error "No match"))))
11536 (and (org-mode-p) (org-show-context 'link-search))))
11538 (defun org-search-not-self (group &rest args)
11539 "Execute `re-search-forward', but only accept matches that do not
11540 enclose the position of `org-open-link-marker'."
11541 (let ((m org-open-link-marker))
11543 (while (apply 're-search-forward args)
11544 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
11545 (goto-char (match-end group))
11546 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
11547 (> (match-beginning 0) (marker-position m))
11548 (< (match-end 0) (marker-position m)))
11550 (or (not (org-in-regexp
11551 org-bracket-link-analytic-regexp 1))
11552 (not (match-end 4)) ; no description
11553 (and (<= (match-beginning 4) (point))
11554 (>= (match-end 4) (point))))))
11555 (throw 'exit (point))))))))
11557 (defun org-get-buffer-for-internal-link (buffer)
11558 "Return a buffer to be used for displaying the link target of internal links."
11560 ((not org-display-internal-link-with-indirect-buffer)
11562 ((string-match "(Clone)$" (buffer-name buffer))
11563 (message "Buffer is already a clone, not making another one")
11564 ;; we also do not modify visibility in this case
11566 (t ; make a new indirect buffer for displaying the link
11567 (let* ((bn (buffer-name buffer))
11568 (ibn (concat bn "(Clone)"))
11569 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
11570 (with-current-buffer ib (org-overview))
11573 (defun org-do-occur (regexp &optional cleanup)
11574 "Call the Emacs command `occur'.
11575 If CLEANUP is non-nil, remove the printout of the regular expression
11576 in the *Occur* buffer. This is useful if the regex is long and not useful
11580 (let ((cwin (selected-window)) win beg end)
11581 (when (setq win (get-buffer-window "*Occur*"))
11582 (select-window win))
11583 (goto-char (point-min))
11584 (when (re-search-forward "match[a-z]+" nil t)
11585 (setq beg (match-end 0))
11586 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
11587 (setq end (1- (match-beginning 0)))))
11588 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
11589 (goto-char (point-min))
11590 (select-window cwin))))
11592 ;;; The mark ring for links jumps
11594 (defvar org-mark-ring nil
11595 "Mark ring for positions before jumps in Org-mode.")
11596 (defvar org-mark-ring-last-goto nil
11597 "Last position in the mark ring used to go back.")
11598 ;; Fill and close the ring
11599 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
11600 (loop for i from 1 to org-mark-ring-length do
11601 (push (make-marker) org-mark-ring))
11602 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
11605 (defun org-mark-ring-push (&optional pos buffer)
11606 "Put the current position or POS into the mark ring and rotate it."
11608 (setq pos (or pos (point)))
11609 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
11610 (move-marker (car org-mark-ring)
11612 (or buffer (current-buffer)))
11614 (substitute-command-keys
11615 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
11617 (defun org-mark-ring-goto (&optional n)
11618 "Jump to the previous position in the mark ring.
11619 With prefix arg N, jump back that many stored positions. When
11620 called several times in succession, walk through the entire ring.
11621 Org-mode commands jumping to a different position in the current file,
11622 or to another Org-mode file, automatically push the old position
11626 (if (eq last-command this-command)
11627 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
11628 (setq p org-mark-ring))
11629 (setq org-mark-ring-last-goto p)
11631 (switch-to-buffer (marker-buffer m))
11633 (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
11635 (defun org-remove-angle-brackets (s)
11636 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
11637 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
11639 (defun org-add-angle-brackets (s)
11640 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
11641 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
11644 ;;; Following specific links
11646 (defun org-follow-timestamp-link ()
11648 ((org-at-date-range-p t)
11649 (let ((org-agenda-start-on-weekday)
11650 (t1 (match-string 1))
11651 (t2 (match-string 2)))
11652 (setq t1 (time-to-days (org-time-string-to-time t1))
11653 t2 (time-to-days (org-time-string-to-time t2)))
11654 (org-agenda-list nil t1 (1+ (- t2 t1)))))
11655 ((org-at-timestamp-p t)
11656 (org-agenda-list nil (time-to-days (org-time-string-to-time
11657 (substring (match-string 1) 0 10)))
11659 (t (error "This should not happen"))))
11662 (defun org-follow-bbdb-link (name)
11663 "Follow a BBDB link to NAME."
11665 (let ((inhibit-redisplay (not debug-on-error))
11666 (bbdb-electric-p nil))
11668 ;; Exact match on name
11669 (bbdb-name (concat "\\`" name "\\'") nil)
11670 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
11671 ;; Exact match on name
11672 (bbdb-company (concat "\\`" name "\\'") nil)
11673 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
11674 ;; Partial match on name
11675 (bbdb-name name nil)
11676 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
11677 ;; Partial match on company
11678 (bbdb-company name nil)
11679 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
11680 ;; General match including network address and notes
11682 (when (= 0 (buffer-size (get-buffer "*BBDB*")))
11683 (delete-window (get-buffer-window "*BBDB*"))
11684 (error "No matching BBDB record")))))
11686 (defun org-follow-info-link (name)
11687 "Follow an info file & node link to NAME."
11688 (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
11689 (string-match "\\(.*\\)" name))
11692 (if (match-string 2 name) ; If there isn't a node, choose "Top"
11693 (Info-find-node (match-string 1 name) (match-string 2 name))
11694 (Info-find-node (match-string 1 name) "Top")))
11695 (message (concat "Could not open: " name))))
11697 (defun org-follow-gnus-link (&optional group article)
11698 "Follow a Gnus link to GROUP and ARTICLE."
11700 (funcall (cdr (assq 'gnus org-link-frame-setup)))
11701 (if gnus-other-frame-object (select-frame gnus-other-frame-object))
11702 (cond ((and group article)
11703 (gnus-group-read-group 1 nil group)
11704 (gnus-summary-goto-article (string-to-number article) nil t))
11705 (group (gnus-group-jump-to-group group))))
11707 (defun org-follow-vm-link (&optional folder article readonly)
11708 "Follow a VM link to FOLDER and ARTICLE."
11710 (setq article (org-add-angle-brackets article))
11711 (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
11712 ;; ange-ftp or efs or tramp access
11713 (let ((user (or (match-string 1 folder) (user-login-name)))
11714 (host (match-string 2 folder))
11715 (file (match-string 3 folder)))
11718 ;; use tramp to access the file
11719 (if (featurep 'xemacs)
11720 (setq folder (format "[%s@%s]%s" user host file))
11721 (setq folder (format "/%s@%s:%s" user host file))))
11723 ;; use ange-ftp or efs
11724 (require (if (featurep 'xemacs) 'efs 'ange-ftp))
11725 (setq folder (format "/%s@%s:%s" user host file))))))
11727 (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
11730 (vm-select-folder-buffer)
11732 (let ((case-fold-search t))
11733 (goto-char (point-min))
11734 (if (not (re-search-forward
11735 (concat "^" "message-id: *" (regexp-quote article))))
11736 (error "Could not find the specified message in this folder"))
11737 (vm-isearch-update)
11738 (vm-isearch-narrow)
11739 (vm-beginning-of-message)
11742 (defun org-follow-wl-link (folder article)
11743 "Follow a Wanderlust link to FOLDER and ARTICLE."
11744 (if (and (string= folder "%")
11746 (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
11747 ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
11748 ;; Thus, we recompose folder and article ids.
11749 (setq folder (format "%s#%s" folder (match-string 1 article))
11750 article (match-string 3 article)))
11751 (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
11752 (error "No such folder: %s" folder))
11753 (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
11755 (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
11756 (wl-summary-redisplay)))
11758 (defun org-follow-rmail-link (folder article)
11759 "Follow an RMAIL link to FOLDER and ARTICLE."
11760 (setq article (org-add-angle-brackets article))
11761 (let (message-number)
11763 (save-window-excursion
11764 (rmail (if (string= folder "RMAIL") rmail-file-name folder))
11765 (setq message-number
11768 (goto-char (point-max))
11769 (if (re-search-backward
11770 (concat "^Message-ID:\\s-+" (regexp-quote
11773 (rmail-what-message))))))
11776 (rmail (if (string= folder "RMAIL") rmail-file-name folder))
11777 (rmail-show-message message-number)
11779 (error "Message not found"))))
11781 ;;; mh-e integration based on planner-mode
11782 (defun org-mhe-get-message-real-folder ()
11783 "Return the name of the current message real folder, so if you use
11784 sequences, it will now work."
11787 (if (equal major-mode 'mh-folder-mode)
11789 ;; Refer to the show buffer
11790 mh-show-folder-buffer))
11792 (if (boundp 'mh-index-folder)
11793 (min (length mh-index-folder) (length folder))))
11795 ;; a simple test on mh-index-data does not work, because
11796 ;; mh-index-data is always nil in a show buffer.
11797 (if (and (boundp 'mh-index-folder)
11798 (string= mh-index-folder (substring folder 0 end-index)))
11799 (if (equal major-mode 'mh-show-mode)
11800 (save-window-excursion
11801 (when (buffer-live-p (get-buffer folder))
11803 (pop-to-buffer folder)
11804 (org-mhe-get-message-folder-from-index)
11807 (org-mhe-get-message-folder-from-index)
11813 (defun org-mhe-get-message-folder-from-index ()
11814 "Returns the name of the message folder in a index folder buffer."
11816 (mh-index-previous-folder)
11817 (re-search-forward "^\\(+.*\\)$" nil t)
11818 (message (match-string 1))))
11820 (defun org-mhe-get-message-folder ()
11821 "Return the name of the current message folder. Be careful if you
11824 (if (equal major-mode 'mh-folder-mode)
11826 ;; Refer to the show buffer
11827 mh-show-folder-buffer)))
11829 (defun org-mhe-get-message-num ()
11830 "Return the number of the current message. Be careful if you
11833 (if (equal major-mode 'mh-folder-mode)
11834 (mh-get-msg-num nil)
11835 ;; Refer to the show buffer
11836 (mh-show-buffer-message-number))))
11838 (defun org-mhe-get-header (header)
11839 "Return a header of the message in folder mode. This will create a
11840 show buffer for the corresponding message. If you have a more clever
11842 (let* ((folder (org-mhe-get-message-folder))
11843 (num (org-mhe-get-message-num))
11844 (buffer (get-buffer-create (concat "show-" folder)))
11846 (with-current-buffer buffer
11847 (mh-display-msg num folder)
11848 (if (equal major-mode 'mh-folder-mode)
11849 (mh-header-display)
11850 (mh-show-header-display))
11851 (set-buffer buffer)
11852 (setq header-field (mh-get-header-field header))
11853 (if (equal major-mode 'mh-folder-mode)
11858 (defun org-follow-mhe-link (folder article)
11859 "Follow an MHE link to FOLDER and ARTICLE.
11860 If ARTICLE is nil FOLDER is shown. If the configuration variable
11861 `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
11862 ARTICLE is searched in all folders. Indexed searches (swish++,
11863 namazu, and others supported by MH-E) will always search in all
11866 (require 'mh-search)
11867 (require 'mh-utils)
11870 (mh-visit-folder (mh-normalize-folder-name folder))
11871 (setq article (org-add-angle-brackets article))
11873 (if (equal mh-searcher 'pick)
11875 (mh-search folder (list "--message-id" article))
11876 (when (and org-mhe-search-all-folders
11877 (not (org-mhe-get-message-real-folder)))
11879 (mh-search "+" (list "--message-id" article))))
11880 (mh-search "+" article))
11881 (if (org-mhe-get-message-real-folder)
11884 (error "Message not found"))))
11888 ;; Use the custom search meachnism to construct and use search strings for
11889 ;; file links to BibTeX database entries.
11891 (defun org-create-file-search-in-bibtex ()
11892 "Create the search string and description for a BibTeX database entry."
11893 (when (eq major-mode 'bibtex-mode)
11894 ;; yes, we want to construct this search string.
11895 ;; Make a good description for this entry, using names, year and the title
11896 ;; Put it into the `description' variable which is dynamically scoped.
11897 (let ((bibtex-autokey-names 1)
11898 (bibtex-autokey-names-stretch 1)
11899 (bibtex-autokey-name-case-convert-function 'identity)
11900 (bibtex-autokey-name-separator " & ")
11901 (bibtex-autokey-additional-names " et al.")
11902 (bibtex-autokey-year-length 4)
11903 (bibtex-autokey-name-year-separator " ")
11904 (bibtex-autokey-titlewords 3)
11905 (bibtex-autokey-titleword-separator " ")
11906 (bibtex-autokey-titleword-case-convert-function 'identity)
11907 (bibtex-autokey-titleword-length 'infty)
11908 (bibtex-autokey-year-title-separator ": "))
11909 (setq description (bibtex-generate-autokey)))
11910 ;; Now parse the entry, get the key and return it.
11912 (bibtex-beginning-of-entry)
11913 (cdr (assoc "=key=" (bibtex-parse-entry))))))
11915 (defun org-execute-file-search-in-bibtex (s)
11916 "Find the link search string S as a key for a database entry."
11917 (when (eq major-mode 'bibtex-mode)
11918 ;; Yes, we want to do the search in this file.
11919 ;; We construct a regexp that searches for "@entrytype{" followed by the key
11920 (goto-char (point-min))
11921 (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
11922 (regexp-quote s) "[ \t\n]*,") nil t)
11923 (goto-char (match-beginning 0)))
11924 (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
11925 ;; Use double prefix to indicate that any web link should be browsed
11926 (let ((b (current-buffer)) (p (point)))
11927 ;; Restore the window configuration because we just use the web link
11928 (set-window-configuration org-window-config-before-follow-link)
11929 (save-excursion (set-buffer b) (goto-char p)
11931 (recenter 0)) ; Move entry start to beginning of window
11932 ;; return t to indicate that the search is done.
11935 ;; Finally add the functions to the right hooks.
11936 (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
11937 (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
11939 ;; end of Bibtex link setup
11941 ;;; Following file links
11943 (defun org-open-file (path &optional in-emacs line search)
11944 "Open the file at PATH.
11945 First, this expands any special file name abbreviations. Then the
11946 configuration variable `org-file-apps' is checked if it contains an
11947 entry for this file type, and if yes, the corresponding command is launched.
11948 If no application is found, Emacs simply visits the file.
11949 With optional argument IN-EMACS, Emacs will visit the file.
11950 Optional LINE specifies a line to go to, optional SEARCH a string to
11951 search for. If LINE or SEARCH is given, the file will always be
11953 If the file does not exist, an error is thrown."
11954 (setq in-emacs (or in-emacs line search))
11955 (let* ((file (if (equal path "")
11957 (substitute-in-file-name (expand-file-name path))))
11958 (apps (append org-file-apps (org-default-apps)))
11959 (remp (and (assq 'remote apps) (org-file-remote-p file)))
11960 (dirp (if remp nil (file-directory-p file)))
11961 (dfile (downcase file))
11962 (old-buffer (current-buffer))
11964 (old-mode major-mode)
11966 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
11967 (setq ext (match-string 1 dfile))
11968 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
11969 (setq ext (match-string 1 dfile))))
11972 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
11973 (and dirp (cdr (assoc 'directory apps)))
11974 (cdr (assoc ext apps))
11975 (cdr (assoc t apps)))))
11976 (when (eq cmd 'mailcap)
11978 (mailcap-parse-mailcaps)
11979 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
11980 (command (mailcap-mime-info mime-type)))
11981 (if (stringp command)
11983 (setq cmd 'emacs))))
11984 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
11985 (not (file-exists-p file))
11986 (not org-open-non-existing-files))
11987 (error "No such file: %s" file))
11989 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
11990 ;; Remove quotes around the file name - we'll use shell-quote-argument.
11991 (if (string-match "['\"]%s['\"]" cmd)
11992 (setq cmd (replace-match "%s" t t cmd)))
11993 (setq cmd (format cmd (shell-quote-argument file)))
11994 (save-window-excursion
11995 (shell-command (concat cmd " &"))))
11998 (funcall (cdr (assq 'file org-link-frame-setup)) file)
12000 (if line (goto-line line)
12001 (if search (org-link-search search))))
12004 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
12005 (and (org-mode-p) (eq old-mode 'org-mode)
12006 (or (not (equal old-buffer (current-buffer)))
12007 (not (equal old-pos (point))))
12008 (org-mark-ring-push old-pos old-buffer))))
12010 (defun org-default-apps ()
12011 "Return the default applications for this operating system."
12013 ((eq system-type 'darwin)
12014 org-file-apps-defaults-macosx)
12015 ((eq system-type 'windows-nt)
12016 org-file-apps-defaults-windowsnt)
12017 (t org-file-apps-defaults-gnu)))
12019 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
12020 (defun org-file-remote-p (file)
12021 "Test whether FILE specifies a location on a remote system.
12022 Return non-nil if the location is indeed remote.
12024 For example, the filename \"/user@host:/foo\" specifies a location
12025 on the system \"/user@host:\"."
12026 (cond ((fboundp 'file-remote-p)
12027 (file-remote-p file))
12028 ((fboundp 'tramp-handle-file-remote-p)
12029 (tramp-handle-file-remote-p file))
12030 ((and (boundp 'ange-ftp-name-format)
12031 (string-match (car ange-ftp-name-format) file))
12036 ;;;; Hooks for remember.el
12039 (defun org-remember-annotation ()
12040 "Return a link to the current location as an annotation for remember.el.
12041 If you are using Org-mode files as target for data storage with
12042 remember.el, then the annotations should include a link compatible with the
12043 conventions in Org-mode. This function returns such a link."
12044 (org-store-link nil))
12046 (defconst org-remember-help
12047 "Select a destination location for the note.
12048 UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
12049 RET at beg-of-buf -> Append to file as level 2 headline
12050 RET on headline -> Store as sublevel entry to current headline
12051 <left>/<right> -> before/after current headline, same headings level")
12054 (defun org-remember-apply-template (&optional use-char skip-interactive)
12055 "Initialize *remember* buffer with template, invoke `org-mode'.
12056 This function should be placed into `remember-mode-hook' and in fact requires
12057 to be run from that hook to fucntion properly."
12058 (if org-remember-templates
12060 (let* ((char (or use-char
12061 (if (= (length org-remember-templates) 1)
12062 (caar org-remember-templates)
12063 (message "Select template: %s"
12065 (lambda (x) (char-to-string (car x)))
12066 org-remember-templates " "))
12067 (read-char-exclusive))))
12068 (entry (cdr (assoc char org-remember-templates)))
12070 (plist-p (if org-store-link-plist t nil))
12071 (file (if (and (nth 1 entry) (stringp (nth 1 entry))
12072 (string-match "\\S-" (nth 1 entry)))
12074 org-default-notes-file))
12075 (headline (nth 2 entry))
12076 (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
12077 (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
12078 (v-u (concat "[" (substring v-t 1 -1) "]"))
12079 (v-U (concat "[" (substring v-T 1 -1) "]"))
12080 (v-i initial) ; defined in `remember-mode'
12081 (v-a (if (equal annotation "[[]]") "" annotation)) ; likewise
12082 (v-n user-full-name)
12083 (org-startup-folded nil)
12084 org-time-was-given org-end-time-was-given x prompt char time)
12085 (setq org-store-link-plist
12086 (append (list :annotation v-a :initial v-i)
12087 org-store-link-plist))
12088 (unless tpl (setq tpl "") (message "No template") (ding))
12090 (insert (substitute-command-keys
12092 "## `C-c C-c' to file interactively, `C-u C-c C-c' to file directly.
12093 ## Target file \"%s\", headline \"%s\"
12094 ## To switch templates, use `\\[org-remember]'.\n\n"
12095 (abbreviate-file-name (or file org-default-notes-file))
12096 (or headline ""))))
12097 (insert tpl) (goto-char (point-min))
12098 ;; Simple %-escapes
12099 (while (re-search-forward "%\\([tTuUai]\\)" nil t)
12100 (when (and initial (equal (match-string 0) "%i"))
12102 (let* ((lead (buffer-substring
12103 (point-at-bol) (match-beginning 0))))
12104 (setq v-i (mapconcat 'identity
12105 (org-split-string initial "\n")
12106 (concat "\n" lead))))))
12108 (or (eval (intern (concat "v-" (match-string 1)))) "")
12110 ;; From the property list
12112 (goto-char (point-min))
12113 (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
12114 (and (setq x (plist-get org-store-link-plist
12115 (intern (match-string 1))))
12116 (replace-match x t t))))
12117 ;; Turn on org-mode in the remember buffer, set local variables
12119 (org-set-local 'org-finish-function 'remember-buffer)
12120 (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
12121 (org-set-local 'org-default-notes-file file))
12122 (if (and headline (stringp headline) (string-match "\\S-" headline))
12123 (org-set-local 'org-remember-default-headline headline))
12124 ;; Interactive template entries
12125 (goto-char (point-min))
12126 (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([guUtT]\\)?" nil t)
12127 (setq char (if (match-end 3) (match-string 3))
12128 prompt (if (match-end 2) (match-string 2)))
12129 (goto-char (match-beginning 0))
12132 ((member char '("G" "g"))
12133 (let* ((org-last-tags-completion-table
12134 (org-global-tags-completion-table
12135 (if (equal char "G") (org-agenda-files) (and file (list file)))))
12136 (org-add-colon-after-tag-completion t)
12137 (ins (completing-read
12138 (if prompt (concat prompt ": ") "Tags: ")
12139 'org-tags-completion-function nil nil nil
12140 'org-tags-history)))
12141 (setq ins (mapconcat 'identity
12142 (org-split-string ins (org-re "[^[:alnum:]]+"))
12144 (when (string-match "\\S-" ins)
12145 (or (equal (char-before) ?:) (insert ":"))
12147 (or (equal (char-after) ?:) (insert ":")))))
12149 (setq org-time-was-given (equal (upcase char) char))
12150 (setq time (org-read-date (equal (upcase char) "U") t nil
12152 (org-insert-time-stamp time org-time-was-given
12153 (member char '("u" "U"))
12154 nil nil (list org-end-time-was-given)))
12156 (insert (read-string
12157 (if prompt (concat prompt ": ") "Enter string"))))))
12158 (goto-char (point-min))
12159 (if (re-search-forward "%\\?" nil t)
12161 (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
12163 (org-set-local 'org-finish-function 'remember-buffer)))
12166 (defun org-remember ()
12167 "Call `remember'. If this is already a remember buffer, re-apply template.
12168 If there is an active region, make sure remember uses it as initial content
12169 of the remember buffer."
12171 (if (eq org-finish-function 'remember-buffer)
12173 (when (< (length org-remember-templates) 2)
12174 (error "No other template available"))
12176 (let ((annotation (plist-get org-store-link-plist :annotation))
12177 (initial (plist-get org-store-link-plist :initial)))
12178 (org-remember-apply-template))
12179 (message "Press C-c C-c to remember data"))
12180 (if (org-region-active-p)
12181 (remember (buffer-substring (point) (mark)))
12182 (call-interactively 'remember))))
12185 (defun org-remember-handler ()
12186 "Store stuff from remember.el into an org file.
12187 First prompts for an org file. If the user just presses return, the value
12188 of `org-default-notes-file' is used.
12189 Then the command offers the headings tree of the selected file in order to
12190 file the text at a specific location.
12191 You can either immediately press RET to get the note appended to the
12192 file, or you can use vertical cursor motion and visibility cycling (TAB) to
12193 find a better place. Then press RET or <left> or <right> in insert the note.
12195 Key Cursor position Note gets inserted
12196 -----------------------------------------------------------------------------
12197 RET buffer-start as level 2 heading at end of file
12198 RET on headline as sublevel of the heading at cursor
12199 RET no heading at cursor position, level taken from context.
12200 Or use prefix arg to specify level manually.
12201 <left> on headline as same level, before current heading
12202 <right> on headline as same level, after current heading
12204 So the fastest way to store the note is to press RET RET to append it to
12205 the default file. This way your current train of thought is not
12206 interrupted, in accordance with the principles of remember.el.
12207 You can also get the fast execution without prompting by using
12208 C-u C-c C-c to exit the remember buffer. See also the variable
12209 `org-remember-store-without-prompt'.
12211 Before being stored away, the function ensures that the text has a
12212 headline, i.e. a first line that starts with a \"*\". If not, a headline
12213 is constructed from the current date and some additional data.
12215 If the variable `org-adapt-indentation' is non-nil, the entire text is
12216 also indented so that it starts in the same column as the headline
12217 \(i.e. after the stars).
12219 See also the variable `org-reverse-note-order'."
12220 (goto-char (point-min))
12221 (while (looking-at "^[ \t]*\n\\|^##.*\n")
12222 (replace-match ""))
12224 (let* ((txt (buffer-substring (point-min) (point-max)))
12225 (fastp (org-xor (equal current-prefix-arg '(4))
12226 org-remember-store-without-prompt))
12227 (file (if fastp org-default-notes-file (org-get-org-file)))
12228 (heading org-remember-default-headline)
12229 (visiting (org-find-base-buffer-visiting file))
12230 (org-startup-folded nil)
12231 (org-startup-align-all-tables nil)
12232 (org-goto-start-pos 1)
12233 spos level indent reversed)
12234 (setq current-prefix-arg nil)
12235 ;; Modify text so that it becomes a nice subtree which can be inserted
12236 ;; into an org tree.
12237 (let* ((lines (split-string txt "\n"))
12239 (setq first (car lines) lines (cdr lines))
12240 (if (string-match "^\\*+ " first)
12241 ;; Is already a headline
12243 ;; We need to add a headline: Use time and first buffer line
12244 (setq lines (cons first lines)
12245 first (concat "* " (current-time-string)
12246 " (" (remember-buffer-desc) ")")
12248 (if (and org-adapt-indentation indent)
12249 (setq lines (mapcar (lambda (x) (concat indent x)) lines)))
12250 (setq txt (concat first "\n"
12251 (mapconcat 'identity lines "\n"))))
12253 (if (not visiting) (find-file-noselect file))
12254 (with-current-buffer (or visiting (get-file-buffer file))
12258 (and (goto-char (point-min))
12259 (not (re-search-forward "^\\* " nil t))
12260 (insert "\n* Notes\n"))
12261 (setq reversed (org-notes-order-reversed-p))
12263 ;; Find the default location
12264 (when (and heading (stringp heading) (string-match "\\S-" heading))
12265 (goto-char (point-min))
12266 (if (re-search-forward
12267 (concat "^\\*+[ \t]+" (regexp-quote heading)
12268 (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
12270 (setq org-goto-start-pos (match-beginning 0))))
12272 ;; Ask the User for a location
12273 (setq spos (if fastp
12275 (org-get-location (current-buffer) org-remember-help)))
12276 (if (not spos) (throw 'quit nil)) ; return nil to show we did
12277 ; not handle this note
12279 (cond ((and (bobp) (not reversed))
12280 ;; Put it at the end, one level below level 1
12283 (goto-char (point-max))
12284 (if (not (bolp)) (newline))
12285 (org-paste-subtree (org-get-legal-level 1 1) txt)))
12286 ((and (bobp) reversed)
12287 ;; Put it at the start, as level 1
12290 (goto-char (point-min))
12291 (re-search-forward "^\\*+ " nil t)
12292 (beginning-of-line 1)
12293 (org-paste-subtree 1 txt)))
12294 ((and (org-on-heading-p t) (not current-prefix-arg))
12295 ;; Put it below this entry, at the beg/end of the subtree
12296 (org-back-to-heading t)
12297 (setq level (funcall outline-level))
12299 (outline-next-heading)
12300 (org-end-of-subtree t))
12301 (if (not (bolp)) (newline))
12302 (beginning-of-line 1)
12303 (org-paste-subtree (org-get-legal-level level 1) txt))
12305 ;; Put it right there, with automatic level determined by
12306 ;; org-paste-subtree or from prefix arg
12308 (if (numberp current-prefix-arg) current-prefix-arg)
12310 (when remember-save-after-remembering
12312 (if (not visiting) (kill-buffer (current-buffer)))))))))
12313 t) ;; return t to indicate that we took care of this note.
12315 (defun org-get-org-file ()
12316 "Read a filename, with default directory `org-directory'."
12317 (let ((default (or org-default-notes-file remember-data-file)))
12318 (read-file-name (format "File name [%s]: " default)
12319 (file-name-as-directory org-directory)
12322 (defun org-notes-order-reversed-p ()
12323 "Check if the current file should receive notes in reversed order."
12325 ((not org-reverse-note-order) nil)
12326 ((eq t org-reverse-note-order) t)
12327 ((not (listp org-reverse-note-order)) nil)
12329 (let ((all org-reverse-note-order)
12331 (while (setq entry (pop all))
12332 (if (string-match (car entry) buffer-file-name)
12333 (throw 'exit (cdr entry))))
12336 ;;;; Dynamic blocks
12338 (defun org-find-dblock (name)
12339 "Find the first dynamic block with name NAME in the buffer.
12340 If not found, stay at current position and return nil."
12343 (goto-char (point-min))
12344 (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
12346 (match-beginning 0))))
12347 (if pos (goto-char pos))
12350 (defconst org-dblock-start-re
12351 "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
12352 "Matches the startline of a dynamic block, with parameters.")
12354 (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
12355 "Matches the end of a dyhamic block.")
12357 (defun org-create-dblock (plist)
12358 "Create a dynamic block section, with parameters taken from PLIST.
12359 PLIST must containe a :name entry which is used as name of the block."
12360 (unless (bolp) (newline))
12361 (let ((name (plist-get plist :name)))
12362 (insert "#+BEGIN: " name)
12364 (if (eq (car plist) :name)
12365 (setq plist (cddr plist))
12366 (insert " " (prin1-to-string (pop plist)))))
12367 (insert "\n\n#+END:\n")
12368 (beginning-of-line -2)))
12370 (defun org-prepare-dblock ()
12371 "Prepare dynamic block for refresh.
12372 This empties the block, puts the cursor at the insert position and returns
12373 the property list including an extra property :name with the block name."
12374 (unless (looking-at org-dblock-start-re)
12375 (error "Not at a dynamic block"))
12376 (let* ((begdel (1+ (match-end 0)))
12377 (name (match-string 1))
12378 (params (append (list :name name)
12379 (read (concat "(" (match-string 3) ")")))))
12380 (unless (re-search-forward org-dblock-end-re nil t)
12381 (error "Dynamic block not terminated"))
12382 (delete-region begdel (match-beginning 0))
12387 (defun org-map-dblocks (&optional command)
12388 "Apply COMMAND to all dynamic blocks in the current buffer.
12389 If COMMAND is not given, use `org-update-dblock'."
12390 (let ((cmd (or command 'org-update-dblock))
12393 (goto-char (point-min))
12394 (while (re-search-forward org-dblock-start-re nil t)
12395 (goto-char (setq pos (match-beginning 0)))
12396 (condition-case nil
12398 (error (message "Error during update of dynamic block")))
12400 (unless (re-search-forward org-dblock-end-re nil t)
12401 (error "Dynamic block not terminated"))))))
12403 (defun org-dblock-update (&optional arg)
12404 "User command for updating dynamic blocks.
12405 Update the dynamic block at point. With prefix ARG, update all dynamic
12406 blocks in the buffer."
12409 (org-update-all-dblocks)
12410 (or (looking-at org-dblock-start-re)
12411 (org-beginning-of-dblock))
12412 (org-update-dblock)))
12414 (defun org-update-dblock ()
12415 "Update the dynamic block at point
12416 This means to empty the block, parse for parameters and then call
12417 the correct writing function."
12418 (let* ((pos (point))
12419 (params (org-prepare-dblock))
12420 (name (plist-get params :name))
12421 (cmd (intern (concat "org-dblock-write:" name))))
12422 (funcall cmd params)
12425 (defun org-beginning-of-dblock ()
12426 "Find the beginning of the dynamic block at point.
12427 Error if there is no scuh block at point."
12428 (let ((pos (point))
12431 (if (and (re-search-backward org-dblock-start-re nil t)
12432 (setq beg (match-beginning 0))
12433 (re-search-forward org-dblock-end-re nil t)
12434 (> (match-end 0) pos))
12437 (error "Not in a dynamic block"))))
12439 (defun org-update-all-dblocks ()
12440 "Update all dynamic blocks in the buffer.
12441 This function can be used in a hook."
12443 (org-map-dblocks 'org-update-dblock)))
12448 (defun org-complete (&optional arg)
12449 "Perform completion on word at point.
12450 At the beginning of a headline, this completes TODO keywords as given in
12451 `org-todo-keywords'.
12452 If the current word is preceded by a backslash, completes the TeX symbols
12453 that are supported for HTML support.
12454 If the current word is preceded by \"#+\", completes special words for
12455 setting file options.
12456 In the line after \"#+STARTUP:, complete valid keywords.\"
12457 At all other locations, this simply calls `ispell-complete-word'."
12460 (let* ((end (point))
12461 (beg1 (save-excursion
12462 (skip-chars-backward (org-re "[:alnum:]_@"))
12464 (beg (save-excursion
12465 (skip-chars-backward "a-zA-Z0-9_:$")
12467 (confirm (lambda (x) (stringp (car x))))
12468 (searchhead (equal (char-before beg) ?*))
12469 (tag (and (equal (char-before beg1) ?:)
12470 (equal (char-after (point-at-bol)) ?*)))
12471 (prop (and (equal (char-before beg1) ?:)
12472 (not (equal (char-after (point-at-bol)) ?*))))
12473 (texp (equal (char-before beg) ?\\))
12474 (link (equal (char-before beg) ?\[))
12475 (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
12478 (startup (string-match "^#\\+STARTUP:.*"
12479 (buffer-substring (point-at-bol) (point))))
12480 (completion-ignore-case opt)
12486 (mapcar (lambda (x)
12487 (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
12488 (cons (match-string 2 x) (match-string 1 x)))
12489 (org-split-string (org-get-current-options) "\n")))
12491 (setq type :startup)
12492 org-startup-options)
12493 (link (append org-link-abbrev-alist-local
12494 org-link-abbrev-alist))
12498 ((string-match "\\`\\*+[ \t]+\\'"
12499 (buffer-substring (point-at-bol) beg))
12501 (mapcar 'list org-todo-keywords-1))
12503 (setq type :searchhead)
12505 (goto-char (point-min))
12506 (while (re-search-forward org-todo-line-regexp nil t)
12508 (org-make-org-heading-search-string
12509 (match-string 3) t))
12512 (tag (setq type :tag beg beg1)
12513 (or org-tag-alist (org-get-buffer-tags)))
12514 (prop (setq type :prop beg beg1)
12515 (mapcar 'list (org-buffer-property-keys)))
12516 (t (progn (ispell-complete-word arg) (throw 'exit nil)))))
12517 (pattern (buffer-substring-no-properties beg end))
12518 (completion (try-completion pattern table confirm)))
12519 (cond ((eq completion t)
12520 (if (equal type :opt)
12521 (insert (substring (cdr (assoc (upcase pattern) table))
12523 (if (memq type '(:tag :prop)) (insert ":"))))
12525 (message "Can't find completion for \"%s\"" pattern)
12527 ((not (string= pattern completion))
12528 (delete-region beg end)
12529 (if (string-match " +$" completion)
12530 (setq completion (replace-match "" t t completion)))
12531 (insert completion)
12532 (if (get-buffer-window "*Completions*")
12533 (delete-window (get-buffer-window "*Completions*")))
12534 (if (assoc completion table)
12535 (if (eq type :todo) (insert " ")
12536 (if (memq type '(:tag :prop)) (insert ":"))))
12537 (if (and (equal type :opt) (assoc completion table))
12538 (message "%s" (substitute-command-keys
12539 "Press \\[org-complete] again to insert example settings"))))
12541 (message "Making completion list...")
12542 (let ((list (sort (all-completions pattern table confirm)
12544 (with-output-to-temp-buffer "*Completions*"
12545 (condition-case nil
12546 ;; Protection needed for XEmacs and emacs 21
12547 (display-completion-list list pattern)
12548 (error (display-completion-list list)))))
12549 (message "Making completion list...%s" "done"))))))
12551 ;;;; TODO, DEADLINE, Comments
12553 (defun org-toggle-comment ()
12554 "Change the COMMENT state of an entry."
12557 (org-back-to-heading)
12558 (if (looking-at (concat outline-regexp
12559 "\\( *\\<" org-comment-string "\\>\\)"))
12560 (replace-match "" t t nil 1)
12561 (if (looking-at outline-regexp)
12563 (goto-char (match-end 0))
12564 (insert org-comment-string " "))))))
12566 (defvar org-last-todo-state-is-todo nil
12567 "This is non-nil when the last TODO state change led to a TODO state.
12568 If the last change removed the TODO tag or switched to DONE, then
12571 (defun org-todo (&optional arg)
12572 "Change the TODO state of an item.
12573 The state of an item is given by a keyword at the start of the heading,
12575 *** TODO Write paper
12578 The different keywords are specified in the variable `org-todo-keywords'.
12579 By default the available states are \"TODO\" and \"DONE\".
12580 So for this example: when the item starts with TODO, it is changed to DONE.
12581 When it starts with DONE, the DONE is removed. And when neither TODO nor
12582 DONE are present, add TODO at the beginning of the heading.
12584 With C-u prefix arg, use completion to determine the new state.
12585 With numeric prefix arg, switch to that state.
12587 For calling through lisp, arg is also interpreted in the following way:
12588 'none -> empty state
12589 \"\"(empty string) -> switch to empty state
12590 'done -> switch to DONE
12591 'nextset -> switch to the next set of keywords
12592 'previousset -> switch to the previous set of keywords
12593 \"WAITING\" -> switch to the specified keyword, but only if it
12594 really is a member of `org-todo-keywords'."
12597 (org-back-to-heading)
12598 (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
12599 (or (looking-at (concat " +" org-todo-regexp " *"))
12601 (let* ((this (match-string 1))
12602 (head (org-get-todo-sequence-head this))
12603 (ass (assoc head org-todo-kwd-alist))
12604 (interpret (nth 1 ass))
12605 (done-word (nth 3 ass))
12606 (final-done-word (nth 4 ass))
12607 (last-state (or this ""))
12608 (completion-ignore-case t)
12609 (member (member this org-todo-keywords-1))
12610 (tail (cdr member))
12613 ;; Read a state with completion
12614 (completing-read "State: " (mapcar (lambda(x) (list x))
12615 org-todo-keywords-1)
12619 (if tail (car tail) nil)
12620 (car org-todo-keywords-1)))
12622 (if (equal member org-todo-keywords-1)
12625 (nth (- (length org-todo-keywords-1) (length tail) 2)
12626 org-todo-keywords-1)
12627 (org-last org-todo-keywords-1))))
12629 ;; user or caller requests a specific state
12631 ((equal arg "") nil)
12632 ((eq arg 'none) nil)
12633 ((eq arg 'done) (or done-word (car org-done-keywords)))
12635 (or (car (cdr (member head org-todo-heads)))
12636 (car org-todo-heads)))
12637 ((eq arg 'previousset)
12638 (let ((org-todo-heads (reverse org-todo-heads)))
12639 (or (car (cdr (member head org-todo-heads)))
12640 (car org-todo-heads))))
12641 ((car (member arg org-todo-keywords-1)))
12642 ((nth (1- (prefix-numeric-value arg))
12643 org-todo-keywords-1))))
12644 ((null member) (or head (car org-todo-keywords-1)))
12645 ((equal this final-done-word) nil) ;; -> make empty
12646 ((null tail) nil) ;; -> first entry
12647 ((eq interpret 'sequence)
12649 ((memq interpret '(type priority))
12650 (if (eq this-command last-command)
12652 (if (> (length tail) 0)
12653 (or done-word (car org-done-keywords))
12656 (next (if state (concat " " state " ") " "))
12658 (replace-match next t t)
12660 (setq head (org-get-todo-sequence-head state)
12661 ass (assoc head org-todo-kwd-alist)
12662 interpret (nth 1 ass)
12663 done-word (nth 3 ass)
12664 final-done-word (nth 4 ass)))
12665 (when (memq arg '(nextset previousset))
12666 (message "Keyword-Set %d/%d: %s"
12667 (- (length org-todo-sets) -1
12668 (length (memq (assoc state org-todo-sets) org-todo-sets)))
12669 (length org-todo-sets)
12670 (mapconcat 'identity (assoc state org-todo-sets) " ")))
12671 (setq org-last-todo-state-is-todo
12672 (not (member state org-done-keywords)))
12673 (when (and org-log-done (not (memq arg '(nextset previousset))))
12674 (setq dostates (and (eq interpret 'sequence)
12675 (listp org-log-done) (memq 'state org-log-done)))
12677 ((and state (not this))
12678 ;; FIXME: should we remove CLOSED already then state is nil?
12679 (org-add-planning-info nil nil 'closed)
12680 (and dostates (org-add-log-maybe 'state state 'findpos)))
12681 ((and state dostates)
12682 (org-add-log-maybe 'state state 'findpos))
12683 ((member state org-done-keywords)
12684 ;; Planning info calls the note-setting command.
12685 (org-add-planning-info 'closed (org-current-time)
12686 (if (org-get-repeat) nil 'scheduled))
12687 (org-add-log-maybe 'done state 'findpos))))
12688 ;; Fixup tag positioning
12689 (and org-auto-align-tags (org-set-tags nil t))
12690 (run-hooks 'org-after-todo-state-change-hook)
12691 (and (member state org-done-keywords) (org-auto-repeat-maybe))
12692 (if (and arg (not (member state org-done-keywords)))
12693 (setq head (org-get-todo-sequence-head state)))
12694 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)))
12695 ;; Fixup cursor location if close to the keyword
12696 (if (and (outline-on-heading-p)
12698 (save-excursion (beginning-of-line 1)
12699 (looking-at org-todo-line-regexp))
12700 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
12702 (goto-char (or (match-end 2) (match-end 1)))
12703 (just-one-space))))
12705 (defun org-get-todo-sequence-head (kwd)
12706 "Return the head of the TODO sequence to which KWD belongs.
12707 If KWD is not set, check if there is a text property remembering the
12712 (or (get-text-property (point-at-bol) 'org-todo-head)
12714 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
12715 nil (point-at-eol)))
12716 (get-text-property p 'org-todo-head))))
12717 ((not (member kwd org-todo-keywords-1))
12718 (car org-todo-keywords-1))
12719 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
12721 (defun org-get-repeat ()
12722 "Check if tere is a deadline/schedule with repeater in this entry."
12725 (org-back-to-heading t)
12726 (if (re-search-forward
12727 org-repeat-re (save-excursion (outline-next-heading) (point)) t)
12728 (match-string 1)))))
12730 (defvar org-last-changed-timestamp)
12731 (defvar org-log-post-message)
12732 (defun org-auto-repeat-maybe ()
12733 "Check if the current headline contains a repeated deadline/schedule.
12734 If yes, set TODO state back to what it was and change the base date
12735 of repeating deadline/scheduled time stamps to new date.
12736 This function should be run in the `org-after-todo-state-change-hook'."
12737 ;; last-state is dynamically scoped into this function
12738 (let* ((repeat (org-get-repeat))
12739 (aa (assoc last-state org-todo-kwd-alist))
12740 (interpret (nth 1 aa))
12742 (done-word (nth 3 aa))
12743 (whata '(("d" . day) ("m" . month) ("y" . year)))
12744 (msg "Entry repeats: ")
12748 (org-todo (if (eq interpret 'type) last-state head))
12749 (when (and org-log-repeat
12750 (not (memq 'org-add-log-note
12751 (default-value 'post-command-hook))))
12752 ;; Make sure a note is taken
12753 (let ((org-log-done '(done)))
12754 (org-add-log-maybe 'done (or done-word (car org-done-keywords))
12756 (org-back-to-heading t)
12757 (org-add-planning-info nil nil 'closed)
12758 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
12759 org-deadline-time-regexp "\\)"))
12760 (while (re-search-forward
12761 re (save-excursion (outline-next-heading) (point)) t)
12762 (setq type (if (match-end 1) org-scheduled-string org-deadline-string)
12763 ts (match-string (if (match-end 2) 2 4)))
12764 (when (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" ts)
12765 (setq n (string-to-number (match-string 1 ts))
12766 what (match-string 2 ts))
12767 (if (equal what "w") (setq n (* n 7) what "d"))
12768 (org-timestamp-change n (cdr (assoc what whata))))
12769 (setq msg (concat msg type org-last-changed-timestamp " ")))
12770 (setq org-log-post-message msg)
12773 (defun org-show-todo-tree (arg)
12774 "Make a compact tree which shows all headlines marked with TODO.
12775 The tree will show the lines where the regexp matches, and all higher
12776 headlines above the match.
12777 With \\[universal-argument] prefix, also show the DONE entries.
12778 With a numeric prefix N, construct a sparse tree for the Nth element
12779 of `org-todo-keywords-1'."
12781 (let ((case-fold-search nil)
12783 (cond ((null arg) org-not-done-regexp)
12785 (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
12786 (mapcar 'list org-todo-keywords-1))))
12788 (mapconcat 'identity (org-split-string kwd "|") "\\|")
12790 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
12791 (regexp-quote (nth (1- (prefix-numeric-value arg))
12792 org-todo-keywords-1)))
12793 (t (error "Invalid prefix argument: %s" arg)))))
12794 (message "%d TODO entries found"
12795 (org-occur (concat "^" outline-regexp " *" kwd-re )))))
12797 (defun org-deadline ()
12798 "Insert the DEADLINE: string to make a deadline.
12799 A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
12800 to modify it to the correct date."
12802 (org-add-planning-info 'deadline nil 'closed))
12804 (defun org-schedule ()
12805 "Insert the SCHEDULED: string to schedule a TODO item.
12806 A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
12807 to modify it to the correct date."
12809 (org-add-planning-info 'scheduled nil 'closed))
12811 (defun org-add-planning-info (what &optional time &rest remove)
12812 "Insert new timestamp with keyword in the line directly after the headline.
12813 WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
12814 If non is given, the user is prompted for a date.
12815 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
12818 (let (org-time-was-given org-end-time-was-given)
12819 (when what (setq time (or time (org-read-date nil 'to-time))))
12820 (when (and org-insert-labeled-timestamps-at-point
12821 (member what '(scheduled deadline)))
12823 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
12824 (org-insert-time-stamp time org-time-was-given
12825 nil nil nil (list org-end-time-was-given))
12829 (let (col list elt ts buffer-invisibility-spec)
12830 (org-back-to-heading t)
12831 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
12832 (goto-char (match-end 1))
12833 (setq col (current-column))
12834 (goto-char (match-end 0))
12835 (if (eobp) (insert "\n"))
12837 (when (and (not org-insert-labeled-timestamps-before-properties-drawer)
12838 (looking-at "[ \t]*:PROPERTIES:[ \t]*$"))
12839 (goto-char (match-end 0))
12840 (if (eobp) (insert "\n"))
12842 (if (and (not (looking-at outline-regexp))
12843 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
12845 (not (equal (match-string 1) org-clock-string)))
12846 (narrow-to-region (match-beginning 0) (match-end 0))
12847 (insert-before-markers "\n")
12849 (narrow-to-region (point) (point))
12850 (indent-to-column col))
12851 ;; Check if we have to remove something.
12852 (setq list (cons what remove))
12854 (setq elt (pop list))
12855 (goto-char (point-min))
12856 (when (or (and (eq elt 'scheduled)
12857 (re-search-forward org-scheduled-time-regexp nil t))
12858 (and (eq elt 'deadline)
12859 (re-search-forward org-deadline-time-regexp nil t))
12860 (and (eq elt 'closed)
12861 (re-search-forward org-closed-time-regexp nil t)))
12863 (if (looking-at "--+<[^>]+>") (replace-match ""))
12864 (if (looking-at " +") (replace-match ""))))
12865 (goto-char (point-max))
12868 (if (not (equal (char-before) ?\ )) " " "")
12869 (cond ((eq what 'scheduled) org-scheduled-string)
12870 ((eq what 'deadline) org-deadline-string)
12871 ((eq what 'closed) org-closed-string)
12872 ((eq what 'archived) org-archived-string))
12874 (org-insert-time-stamp
12876 (or org-time-was-given
12877 (and (eq what 'closed) org-log-done-with-time))
12879 nil nil (list org-end-time-was-given))
12881 (goto-char (point-min))
12883 (if (looking-at "[ \t]+\r?\n")
12884 (replace-match ""))
12887 (defvar org-log-note-marker (make-marker))
12888 (defvar org-log-note-purpose nil)
12889 (defvar org-log-note-state nil)
12890 (defvar org-log-note-window-configuration nil)
12891 (defvar org-log-note-return-to (make-marker))
12892 (defvar org-log-post-message nil
12893 "Message to be displayed after a log note has been stored.
12894 The auto-repeater uses this.")
12896 (defun org-add-log-maybe (&optional purpose state findpos)
12897 "Set up the post command hook to take a note."
12899 (when (and (listp org-log-done)
12900 (memq purpose org-log-done))
12902 (org-back-to-heading t)
12903 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
12904 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
12906 (goto-char (match-end 0))
12907 (unless org-log-states-order-reversed
12908 (if (looking-at "\n[ \t]*- State") (forward-char 1))
12909 (while (looking-at "[ \t]*- State")
12910 (condition-case nil
12912 (error (org-end-of-item))))
12913 (skip-chars-backward " \t\n\r")))
12914 (move-marker org-log-note-marker (point))
12915 (setq org-log-note-purpose purpose)
12916 (setq org-log-note-state state)
12917 (add-hook 'post-command-hook 'org-add-log-note 'append))))
12919 (defun org-add-log-note (&optional purpose)
12920 "Pop up a window for taking a note, and add this note later at point."
12921 (remove-hook 'post-command-hook 'org-add-log-note)
12922 (setq org-log-note-window-configuration (current-window-configuration))
12923 (delete-other-windows)
12924 (move-marker org-log-note-return-to (point))
12925 (switch-to-buffer (marker-buffer org-log-note-marker))
12926 (goto-char org-log-note-marker)
12927 (switch-to-buffer-other-window "*Org Note*")
12929 (let ((org-inhibit-startup t)) (org-mode))
12930 (insert (format "# Insert note for %s, finish with C-c C-c.\n\n"
12932 ((eq org-log-note-purpose 'clock-out) "stopped clock")
12933 ((eq org-log-note-purpose 'done) "closed todo item")
12934 ((eq org-log-note-purpose 'state) "state change")
12935 (t (error "This should not happen")))))
12936 (org-set-local 'org-finish-function 'org-store-log-note))
12938 (defun org-store-log-note ()
12939 "Finish taking a log note, and insert it to where it belongs."
12940 (let ((txt (buffer-string))
12941 (note (cdr (assq org-log-note-purpose org-log-note-headings)))
12943 (kill-buffer (current-buffer))
12944 (if (string-match "^#.*\n[ \t\n]*" txt)
12945 (setq txt (replace-match "" t t txt)))
12946 (if (string-match "\\s-+\\'" txt)
12947 (setq txt (replace-match "" t t txt)))
12948 (setq lines (org-split-string txt "\n"))
12949 (when (and note (string-match "\\S-" note))
12951 (org-replace-escapes
12953 (list (cons "%u" (user-login-name))
12954 (cons "%U" user-full-name)
12955 (cons "%t" (format-time-string
12956 (org-time-stamp-format 'long 'inactive)
12958 (cons "%s" (if org-log-note-state
12959 (concat "\"" org-log-note-state "\"")
12961 (if lines (setq note (concat note " \\\\")))
12965 (set-buffer (marker-buffer org-log-note-marker))
12967 (goto-char org-log-note-marker)
12968 (move-marker org-log-note-marker nil)
12970 (if (not (bolp)) (insert "\n")) (indent-relative nil)
12971 (insert " - " (pop lines))
12972 (org-indent-line-function)
12973 (beginning-of-line 1)
12974 (looking-at "[ \t]*")
12975 (setq ind (concat (match-string 0) " "))
12977 (while lines (insert "\n" ind (pop lines)))))))
12978 (set-window-configuration org-log-note-window-configuration)
12979 (with-current-buffer (marker-buffer org-log-note-return-to)
12980 (goto-char org-log-note-return-to))
12981 (move-marker org-log-note-return-to nil)
12982 (and org-log-post-message (message org-log-post-message)))
12984 (defvar org-occur-highlights nil)
12985 (make-variable-buffer-local 'org-occur-highlights)
12987 (defun org-occur (regexp &optional keep-previous callback)
12988 "Make a compact tree which shows all matches of REGEXP.
12989 The tree will show the lines where the regexp matches, and all higher
12990 headlines above the match. It will also show the heading after the match,
12991 to make sure editing the matching entry is easy.
12992 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
12993 call to `org-occur' will be kept, to allow stacking of calls to this
12995 If CALLBACK is non-nil, it is a function which is called to confirm
12996 that the match should indeed be shown."
12997 (interactive "sRegexp: \nP")
12998 (or keep-previous (org-remove-occur-highlights nil nil t))
13001 (goto-char (point-min))
13002 (if (or (not keep-previous) ; do not want to keep
13003 (not org-occur-highlights)) ; no previous matches
13006 (while (re-search-forward regexp nil t)
13007 (when (or (not callback)
13008 (save-match-data (funcall callback)))
13009 (setq cnt (1+ cnt))
13010 (when org-highlight-sparse-tree-matches
13011 (org-highlight-new-match (match-beginning 0) (match-end 0)))
13012 (org-show-context 'occur-tree))))
13013 (when org-remove-highlights-with-change
13014 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
13016 (unless org-sparse-tree-open-archived-trees
13017 (org-hide-archived-subtrees (point-min) (point-max)))
13018 (run-hooks 'org-occur-hook)
13019 (if (interactive-p)
13020 (message "%d match(es) for regexp %s" cnt regexp))
13023 (defun org-show-context (&optional key)
13024 "Make sure point and context and visible.
13025 How much context is shown depends upon the variables
13026 `org-show-hierarchy-above', `org-show-following-heading'. and
13027 `org-show-siblings'."
13028 (let ((heading-p (org-on-heading-p t))
13029 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
13030 (following-p (org-get-alist-option org-show-following-heading key))
13031 (siblings-p (org-get-alist-option org-show-siblings key)))
13033 ;; Show heading or entry text
13035 (org-flag-heading nil) ; only show the heading
13036 (and (or (org-invisible-p) (org-invisible-p2))
13037 (org-show-hidden-entry))) ; show entire entry
13039 ;; Show next sibling, or heading below text
13041 (and (if heading-p (org-goto-sibling) (outline-next-heading))
13042 (org-flag-heading nil))))
13043 (when siblings-p (org-show-siblings))
13045 ;; show all higher headings, possibly with siblings
13047 (while (and (condition-case nil
13048 (progn (org-up-heading-all 1) t)
13051 (org-flag-heading nil)
13052 (when siblings-p (org-show-siblings))))))))
13054 (defun org-reveal (&optional siblings)
13055 "Show current entry, hierarchy above it, and the following headline.
13056 This can be used to show a consistent set of context around locations
13057 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
13058 not t for the search context.
13060 With optional argument SIBLINGS, on each level of the hierarchy all
13061 siblings are shown. This repairs the tree structure to what it would
13062 look like when opened with hierarchical calls to `org-cycle'."
13064 (let ((org-show-hierarchy-above t)
13065 (org-show-following-heading t)
13066 (org-show-siblings (if siblings t org-show-siblings)))
13067 (org-show-context nil)))
13069 (defun org-highlight-new-match (beg end)
13070 "Highlight from BEG to END and mark the highlight is an occur headline."
13071 (let ((ov (org-make-overlay beg end)))
13072 (org-overlay-put ov 'face 'secondary-selection)
13073 (push ov org-occur-highlights)))
13075 (defun org-remove-occur-highlights (&optional beg end noremove)
13076 "Remove the occur highlights from the buffer.
13077 BEG and END are ignored. If NOREMOVE is nil, remove this function
13078 from the `before-change-functions' in the current buffer."
13080 (unless org-inhibit-highlight-removal
13081 (mapc 'org-delete-overlay org-occur-highlights)
13082 (setq org-occur-highlights nil)
13084 (remove-hook 'before-change-functions
13085 'org-remove-occur-highlights 'local))))
13089 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
13090 "Regular expression matching the priority indicator.")
13092 (defvar org-remove-priority-next-time nil)
13094 (defun org-priority-up ()
13095 "Increase the priority of the current item."
13097 (org-priority 'up))
13099 (defun org-priority-down ()
13100 "Decrease the priority of the current item."
13102 (org-priority 'down))
13104 (defun org-priority (&optional action)
13105 "Change the priority of an item by ARG.
13106 ACTION can be `set', `up', `down', or a character."
13108 (setq action (or action 'set))
13109 (let (current new news have remove)
13111 (org-back-to-heading)
13112 (if (looking-at org-priority-regexp)
13113 (setq current (string-to-char (match-string 2))
13115 (setq current org-default-priority))
13117 ((or (eq action 'set) (integerp action))
13118 (if (integerp action)
13120 (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
13121 (setq new (read-char-exclusive)))
13122 (cond ((equal new ?\ ) (setq remove t))
13123 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
13124 (error "Priority must be between `%c' and `%c'"
13125 org-highest-priority org-lowest-priority))))
13127 (setq new (1- current)))
13129 (setq new (1+ current)))
13130 (t (error "Invalid action")))
13131 (setq new (min (max org-highest-priority (upcase new)) org-lowest-priority))
13132 (setq news (format "%c" new))
13135 (replace-match "" t t nil 1)
13136 (replace-match news t t nil 2))
13138 (error "No priority cookie found in line")
13139 (looking-at org-todo-line-regexp)
13142 (goto-char (match-end 2))
13143 (insert " [#" news "]"))
13144 (goto-char (match-beginning 3))
13145 (insert "[#" news "] ")))))
13146 (org-preserve-lc (org-set-tags nil 'align))
13148 (message "Priority removed")
13149 (message "Priority of current item set to %s" news))))
13152 (defun org-get-priority (s)
13153 "Find priority cookie and return priority."
13155 (if (not (string-match org-priority-regexp s))
13156 (* 1000 (- org-lowest-priority org-default-priority))
13157 (* 1000 (- org-lowest-priority
13158 (string-to-char (match-string 2 s)))))))
13162 (defun org-scan-tags (action matcher &optional todo-only)
13163 "Scan headline tags with inheritance and produce output ACTION.
13164 ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
13165 evaluated, testing if a given set of tags qualifies a headline for
13166 inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
13167 are included in the output."
13168 (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
13169 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
13171 "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
13172 (props (list 'face nil
13173 'done-face 'org-done
13175 'mouse-face 'highlight
13176 'org-not-done-regexp org-not-done-regexp
13177 'org-todo-regexp org-todo-regexp
13178 'keymap org-agenda-keymap
13180 (format "mouse-2 or RET jump to org file %s"
13181 (abbreviate-file-name buffer-file-name))))
13182 (case-fold-search nil)
13184 tags tags-list tags-alist (llast 0) rtn level category i txt
13185 todo marker entry priority)
13187 (goto-char (point-min))
13188 (when (eq action 'sparse-tree) (org-overview))
13189 (while (re-search-forward re nil t)
13191 (setq todo (if (match-end 1) (match-string 2))
13192 tags (if (match-end 4) (match-string 4)))
13193 (goto-char (setq lspos (1+ (match-beginning 0))))
13194 (setq level (org-reduced-level (funcall outline-level))
13195 category (org-get-category))
13196 (setq i llast llast level)
13197 ;; remove tag lists from same and sublevels
13198 (while (>= i level)
13199 (when (setq entry (assoc i tags-alist))
13200 (setq tags-alist (delete entry tags-alist)))
13202 ;; add the nex tags
13204 (setq tags (mapcar 'downcase (org-split-string tags ":"))
13206 (cons (cons level tags) tags-alist)))
13207 ;; compile tags for current headline
13209 (if org-use-tag-inheritance
13210 (apply 'append (mapcar 'cdr tags-alist))
13212 (when (and (or (not todo-only) (member todo org-not-done-keywords))
13214 (or (not org-agenda-skip-archived-trees)
13215 (not (member org-archive-tag tags-list))))
13216 (and (eq action 'agenda) (org-agenda-skip))
13217 ;; list this headline
13218 (if (eq action 'sparse-tree)
13220 (org-show-context 'tags-tree))
13221 (setq txt (org-format-agenda-item
13224 (if org-tags-match-list-sublevels
13225 (make-string (1- level) ?.) "")
13227 category tags-list)
13228 priority (org-get-priority txt))
13230 (setq marker (org-agenda-new-marker))
13231 (org-add-props txt props
13232 'org-marker marker 'org-hd-marker marker 'org-category category
13233 'priority priority 'type "tagsmatch")
13235 ;; if we are to skip sublevels, jump to end of subtree
13236 (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
13237 (when (and (eq action 'sparse-tree)
13238 (not org-sparse-tree-open-archived-trees))
13239 (org-hide-archived-subtrees (point-min) (point-max)))
13242 (defvar todo-only) ;; dynamically scoped
13244 (defun org-tags-sparse-tree (&optional todo-only match)
13245 "Create a sparse tree according to tags string MATCH.
13246 MATCH can contain positive and negative selection of tags, like
13247 \"+WORK+URGENT-WITHBOSS\".
13248 If optional argument TODO_ONLY is non-nil, only select lines that are
13251 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
13253 (defvar org-cached-props nil)
13254 (defun org-cached-entry-get (pom property)
13255 (cdr (assoc property (or org-cached-props
13256 (setq org-cached-props
13257 (org-entry-properties pom))))))
13259 (defun org-global-tags-completion-table (&optional files)
13260 "Return the list of all tags in all agenda buffer/files."
13266 (set-buffer (find-file-noselect file))
13267 (org-get-buffer-tags))
13268 (if (and files (car files))
13270 (org-agenda-files)))))))
13272 (defun org-make-tags-matcher (match)
13273 "Create the TAGS//TODO matcher form for the selection string MATCH."
13274 ;; todo-only is scoped dynamically into this function, and the function
13275 ;; may change it it the matcher asksk for it.
13277 ;; Get a new match request, with completion
13278 (let ((org-last-tags-completion-table
13279 (org-global-tags-completion-table)))
13280 (setq match (completing-read
13281 "Match: " 'org-tags-completion-function nil nil nil
13282 'org-tags-history))))
13284 ;; Parse the string and create a lisp form
13285 (let ((match0 match)
13286 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]]+\\)=\\({[^}]+}\\|\"[^\"]+\"\\)\\|[[:alnum:]_@]+\\)"))
13288 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
13289 orterms term orlist re-p level-p prop-p pn pv)
13290 (if (string-match "/+" match)
13291 ;; match contains also a todo-matching request
13293 (setq tagsmatch (substring match 0 (match-beginning 0))
13294 todomatch (substring match (match-end 0)))
13295 (if (string-match "^!" todomatch)
13296 (setq todo-only t todomatch (substring todomatch 1)))
13297 (if (string-match "^\\s-*$" todomatch)
13298 (setq todomatch nil)))
13299 ;; only matching tags
13300 (setq tagsmatch match todomatch nil))
13302 ;; Make the tags matcher
13303 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
13304 (setq tagsmatcher t)
13305 (setq orterms (org-split-string tagsmatch "|") orlist nil)
13306 (while (setq term (pop orterms))
13307 (while (and (equal (substring term -1) "\\") orterms)
13308 (setq term (concat term "|" (pop orterms)))) ; repair bad split
13309 (while (string-match re term)
13310 (setq minus (and (match-end 1)
13311 (equal (match-string 1 term) "-"))
13312 tag (match-string 2 term)
13313 re-p (equal (string-to-char tag) ?{)
13314 level-p (match-end 3)
13315 prop-p (match-end 4)
13317 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
13318 (level-p `(= level ,(string-to-number
13319 (match-string 3 term))))
13321 (setq pn (match-string 4 term)
13322 pv (match-string 5 term)
13323 re-p (equal (string-to-char pv) ?{)
13324 pv (substring pv 1 -1))
13326 `(string-match ,pv (org-cached-entry-get nil ,pn))
13327 `(equal ,pv (org-cached-entry-get nil ,pn))))
13328 (t `(member ,(downcase tag) tags-list)))
13329 mm (if minus (list 'not mm) mm)
13330 term (substring term (match-end 0)))
13331 (push mm tagsmatcher))
13332 (push (if (> (length tagsmatcher) 1)
13333 (cons 'and tagsmatcher)
13336 (setq tagsmatcher nil))
13337 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
13339 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
13341 ;; Make the todo matcher
13342 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
13343 (setq todomatcher t)
13344 (setq orterms (org-split-string todomatch "|") orlist nil)
13345 (while (setq term (pop orterms))
13346 (while (string-match re term)
13347 (setq minus (and (match-end 1)
13348 (equal (match-string 1 term) "-"))
13349 kwd (match-string 2 term)
13350 re-p (equal (string-to-char kwd) ?{)
13351 term (substring term (match-end 0))
13353 `(string-match ,(substring kwd 1 -1) todo)
13354 (list 'equal 'todo kwd))
13355 mm (if minus (list 'not mm) mm))
13356 (push mm todomatcher))
13357 (push (if (> (length todomatcher) 1)
13358 (cons 'and todomatcher)
13361 (setq todomatcher nil))
13362 (setq todomatcher (if (> (length orlist) 1)
13363 (cons 'or orlist) (car orlist))))
13365 ;; Return the string and lisp forms of the matcher
13366 (setq matcher (if todomatcher
13367 (list 'and tagsmatcher todomatcher)
13369 (cons match0 matcher)))
13371 (defun org-match-any-p (re list)
13372 "Does re match any element of list?"
13373 (setq list (mapcar (lambda (x) (string-match re x)) list))
13376 (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
13377 (defvar org-tags-overlay (org-make-overlay 1 1))
13378 (org-detach-overlay org-tags-overlay)
13380 (defun org-align-tags-here (to-col)
13381 ;; Assumes that this is a headline
13382 (let ((pos (point)) (col (current-column)) tags)
13383 (beginning-of-line 1)
13384 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
13385 (< pos (match-beginning 2)))
13387 (setq tags (match-string 2))
13388 (goto-char (match-beginning 1))
13390 (delete-region (point) (1+ (match-end 0)))
13393 (max (1+ (current-column))
13397 (- (abs to-col) (length tags))))
13400 (move-to-column (min (current-column) col) t))
13403 (defun org-set-tags (&optional arg just-align)
13404 "Set the tags for the current headline.
13405 With prefix ARG, realign all tags in headings in the current buffer."
13407 (let* ((re (concat "^" outline-regexp))
13408 (current (org-get-tags))
13409 table current-tags inherited-tags ; computed below when needed
13413 (goto-char (point-min))
13414 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
13415 (while (re-search-forward re nil t)
13416 (org-set-tags nil t)
13418 (message "All tags realigned to column %d" org-tags-column))
13420 (setq tags current)
13421 ;; Get a new set of tags from the user
13423 (setq table (or org-tag-alist (org-get-buffer-tags))
13424 org-last-tags-completion-table table
13425 current-tags (org-split-string current ":")
13426 inherited-tags (nreverse
13427 (nthcdr (length current-tags)
13428 (nreverse (org-get-tags-at))))
13430 (if (or (eq t org-use-fast-tag-selection)
13431 (and org-use-fast-tag-selection
13432 (delq nil (mapcar 'cdr table))))
13433 (org-fast-tag-selection current-tags inherited-tags table)
13434 (let ((org-add-colon-after-tag-completion t))
13436 (completing-read "Tags: " 'org-tags-completion-function
13437 nil nil current 'org-tags-history))))))
13438 (while (string-match "[-+&]+" tags)
13439 ;; No boolean logic, just a list
13440 (setq tags (replace-match ":" t t tags))))
13442 (if (string-match "\\`[\t ]*\\'" tags)
13444 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
13445 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
13447 ;; Insert new tags at the correct column
13448 (beginning-of-line 1)
13450 ((and (equal current "") (equal tags "")))
13451 ((re-search-forward
13452 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
13454 (if (equal tags "")
13456 (goto-char (match-beginning 0))
13457 (setq c0 (current-column) p0 (point)
13458 c1 (max (1+ c0) (if (> org-tags-column 0)
13460 (- (- org-tags-column) (length tags))))
13461 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
13462 (replace-match rpl t t)
13463 (and (not (featurep 'xemacs)) c0 (tabify p0 (point)))
13465 (t (error "Tags alignment failed"))))))
13467 (defun org-change-tag-in-region (beg end tag off)
13468 "Add or remove TAG for each entry in the region.
13469 This works in the agenda, and also in an org-mode buffer."
13471 (list (region-beginning) (region-end)
13472 (let ((org-last-tags-completion-table
13474 (org-get-buffer-tags)
13475 (org-global-tags-completion-table))))
13477 "Tag: " 'org-tags-completion-function nil nil nil
13478 'org-tags-history))
13480 (message "[s]et or [r]emove? ")
13481 (equal (read-char-exclusive) ?r))))
13482 (if (fboundp 'deactivate-mark) (deactivate-mark))
13483 (let ((agendap (equal major-mode 'org-agenda-mode))
13484 l1 l2 m buf pos newhead (cnt 0))
13486 (setq l2 (1- (org-current-line)))
13488 (setq l1 (org-current-line))
13489 (loop for l from l1 to l2 do
13491 (setq m (get-text-property (point) 'org-hd-marker))
13492 (when (or (and (org-mode-p) (org-on-heading-p))
13494 (setq buf (if agendap (marker-buffer m) (current-buffer))
13495 pos (if agendap m (point)))
13496 (with-current-buffer buf
13500 (setq cnt (1+ cnt))
13501 (org-toggle-tag tag (if off 'off 'on))
13502 (setq newhead (org-get-heading)))))
13503 (and agendap (org-agenda-change-all-lines newhead m))))
13504 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
13506 (defun org-tags-completion-function (string predicate &optional flag)
13507 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
13508 (confirm (lambda (x) (stringp (car x)))))
13509 (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
13510 (setq s1 (match-string 1 string)
13511 s2 (match-string 2 string))
13512 (setq s1 "" s2 string))
13516 (setq rtn (try-completion s2 ctable confirm))
13519 (concat s1 s2 (substring rtn (length s2))
13520 (if (and org-add-colon-after-tag-completion
13521 (assoc rtn ctable))
13526 (all-completions s2 ctable confirm)
13530 (assoc s2 ctable)))
13533 (defun org-fast-tag-insert (kwd tags face &optional end)
13534 "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
13535 (insert (format "%-12s" (concat kwd ":"))
13536 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
13539 (defun org-fast-tag-show-exit (flag)
13542 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
13543 (replace-match ""))
13546 (move-to-column (- (window-width) 19) t)
13547 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
13549 (defun org-set-current-tags-overlay (current prefix)
13550 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
13551 (if (featurep 'xemacs)
13552 (org-overlay-display org-tags-overlay (concat prefix s)
13553 'secondary-selection)
13554 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
13555 (org-overlay-display org-tags-overlay (concat prefix s)))))
13557 (defun org-fast-tag-selection (current inherited table)
13558 "Fast tag selection with single keys.
13559 CURRENT is the current list of tags in the headline, INHERITED is the
13560 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
13561 possibly with grouping information.
13562 If the keys are nil, a-z are automatically assigned.
13563 Returns the new tags string, or nil to not change the current settings."
13564 (let* ((maxlen (apply 'max (mapcar
13566 (if (stringp (car x)) (string-width (car x)) 0))
13568 (buf (current-buffer))
13569 (expert (eq org-fast-tag-selection-single-key 'expert))
13571 (fwidth (+ maxlen 3 1 3))
13572 (ncol (/ (- (window-width) 4) fwidth))
13575 tg cnt e c char c1 c2 ntable tbl rtn
13576 ov-start ov-end ov-prefix
13577 (exit-after-next org-fast-tag-selection-single-key)
13580 (beginning-of-line 1)
13582 (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
13583 (setq ov-start (match-beginning 1)
13584 ov-end (match-end 1)
13586 (setq ov-start (1- (point-at-eol))
13587 ov-end (1+ ov-start))
13588 (skip-chars-forward "^\n\r")
13591 (buffer-substring (1- (point)) (point))
13592 (if (> (current-column) org-tags-column)
13594 (make-string (- org-tags-column (current-column)) ?\ ))))))
13595 (org-move-overlay org-tags-overlay ov-start ov-end)
13596 (save-window-excursion
13598 (set-buffer (get-buffer-create " *Org tags*"))
13599 (delete-other-windows)
13600 (split-window-vertically)
13601 (switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
13603 (org-fast-tag-insert "Inherited" inherited i-face "\n")
13604 (org-fast-tag-insert "Current" current c-face "\n\n")
13605 (org-fast-tag-show-exit exit-after-next)
13606 (org-set-current-tags-overlay current ov-prefix)
13607 (setq tbl table char ?a cnt 0)
13608 (while (setq e (pop tbl))
13610 ((equal e '(:startgroup))
13611 (push '() groups) (setq ingroup t)
13612 (when (not (= cnt 0))
13616 ((equal e '(:endgroup))
13617 (setq ingroup nil cnt 0)
13620 (setq tg (car e) c2 nil)
13623 ;; automatically assign a character.
13624 (setq c1 (string-to-char
13625 (downcase (substring
13626 tg (if (= (string-to-char tg) ?@) 1 0)))))
13627 (if (or (rassoc c1 ntable) (rassoc c1 table))
13628 (while (or (rassoc char ntable) (rassoc char table))
13629 (setq char (1+ char)))
13631 (setq c (or c2 char)))
13632 (if ingroup (push tg (car groups)))
13633 (setq tg (org-add-props tg nil 'face
13635 ((member tg current) c-face)
13636 ((member tg inherited) i-face)
13638 (if (and (= cnt 0) (not ingroup)) (insert " "))
13639 (insert "[" c "] " tg (make-string
13640 (- fwidth 4 (length tg)) ?\ ))
13641 (push (cons tg c) ntable)
13642 (when (= (setq cnt (1+ cnt)) ncol)
13644 (if ingroup (insert " "))
13646 (setq ntable (nreverse ntable))
13648 (goto-char (point-min))
13649 (if (and (not expert) (fboundp 'fit-window-to-buffer))
13650 (fit-window-to-buffer))
13654 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
13655 (if groups " [!] no groups" " [!]groups")
13656 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
13657 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
13659 ((= c ?\r) (throw 'exit t))
13661 (setq groups (not groups))
13662 (goto-char (point-min))
13663 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
13666 (org-fast-tag-show-exit
13667 (setq exit-after-next (not exit-after-next)))
13669 (delete-other-windows)
13670 (split-window-vertically)
13671 (switch-to-buffer-other-window " *Org tags*")
13672 (and (fboundp 'fit-window-to-buffer)
13673 (fit-window-to-buffer))))
13675 (and (= c ?q) (not (rassoc c ntable))))
13676 (org-detach-overlay org-tags-overlay)
13677 (setq quit-flag t))
13680 (if exit-after-next (setq exit-after-next 'now)))
13682 (condition-case nil
13683 (setq tg (completing-read
13686 (with-current-buffer buf
13687 (org-get-buffer-tags)))))
13688 (quit (setq tg "")))
13689 (when (string-match "\\S-" tg)
13690 (add-to-list 'buffer-tags (list tg))
13691 (if (member tg current)
13692 (setq current (delete tg current))
13693 (push tg current)))
13694 (if exit-after-next (setq exit-after-next 'now)))
13695 ((setq e (rassoc c ntable) tg (car e))
13696 (if (member tg current)
13697 (setq current (delete tg current))
13698 (loop for g in groups do
13700 (mapcar (lambda (x)
13701 (setq current (delete x current)))
13704 (if exit-after-next (setq exit-after-next 'now))))
13706 ;; Create a sorted list
13710 (assoc b (cdr (memq (assoc a ntable) ntable))))))
13711 (if (eq exit-after-next 'now) (throw 'exit t))
13712 (goto-char (point-min))
13713 (beginning-of-line 2)
13714 (delete-region (point) (point-at-eol))
13715 (org-fast-tag-insert "Current" current c-face)
13716 (org-set-current-tags-overlay current ov-prefix)
13717 (while (re-search-forward
13718 (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
13719 (setq tg (match-string 1))
13720 (add-text-properties (match-beginning 1) (match-end 1)
13723 ((member tg current) c-face)
13724 ((member tg inherited) i-face)
13726 (goto-char (point-min)))))
13727 (org-detach-overlay org-tags-overlay)
13729 (mapconcat 'identity current ":")
13732 (defun org-get-tags ()
13733 "Get the TAGS string in the current headline."
13734 (unless (org-on-heading-p t)
13735 (error "Not on a heading"))
13737 (beginning-of-line 1)
13738 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
13739 (org-match-string-no-properties 1)
13742 (defun org-get-buffer-tags ()
13743 "Get a table of all tags used in the buffer, for completion."
13746 (goto-char (point-min))
13747 (while (re-search-forward
13748 (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
13749 (when (equal (char-after (point-at-bol 0)) ?*)
13750 (mapc (lambda (x) (add-to-list 'tags x))
13751 (org-split-string (org-match-string-no-properties 1) ":")))))
13752 (mapcar 'list tags)))
13757 ;;; Setting and retrieving properties
13759 (defconst org-special-properties
13760 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED"
13761 "CLOCK" "PRIORITY")
13762 "The special properties valid in Org-mode.
13764 These are properties that are not defined in the property drawer,
13765 but in some other way.")
13767 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
13768 "Regular expression matching the first line of a property drawer.")
13770 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
13771 "Regular expression matching the first line of a property drawer.")
13773 (defun org-property-action ()
13774 "Do an action on properties."
13777 (org-at-property-p)
13778 (setq prop (match-string 2))
13779 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
13780 (setq c (read-char-exclusive))
13783 (call-interactively 'org-set-property))
13785 (call-interactively 'org-delete-property))
13787 (call-interactively 'org-delete-property-globally))
13789 (call-interactively 'org-compute-property-at-point))
13790 (t (error "No such property action %c" c)))))
13792 (defun org-at-property-p ()
13793 "Is the cursor in a property line?"
13794 ;; FIXME: Does not check if we are actually in the drawer.
13795 ;; FIXME: also returns true on any drawers.....
13796 ;; This is used by C-c C-c for property action.
13798 (beginning-of-line 1)
13799 (looking-at "^[ \t]*\\(:\\([a-zA-Z_0-9]+\\):\\)[ \t]*\\(.*\\)")))
13801 (defmacro org-with-point-at (pom &rest body)
13802 "Move to buffer and point of point-or-marker POM for the duration of BODY."
13803 (declare (indent 1) (debug t))
13805 (if (markerp pom) (set-buffer (marker-buffer pom)))
13807 (goto-char (or pom (point)))
13810 (defun org-get-property-block (&optional beg end force)
13811 "Return the (beg . end) range of the body of the property drawer.
13812 BEG and END can be beginning and end of subtree, if not given
13813 they will be found.
13814 If the drawer does not exist and FORCE is non-nil, create the drawer."
13817 (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
13818 (end (or end (progn (outline-next-heading) (point)))))
13820 (if (re-search-forward org-property-start-re end t)
13821 (setq beg (1+ (match-end 0)))
13824 (org-insert-property-drawer)
13825 (setq end (progn (outline-next-heading) (point))))
13828 (if (re-search-forward org-property-start-re end t)
13829 (setq beg (1+ (match-end 0)))))
13830 (if (re-search-forward org-property-end-re end t)
13831 (setq end (match-beginning 0))
13832 (or force (throw 'exit nil))
13835 (org-indent-line-function)
13836 (insert ":END:\n"))
13839 (defun org-entry-properties (&optional pom which)
13840 "Get all properties of the entry at point-or-marker POM.
13841 This includes the TODO keyword, the tags, time strings for deadline,
13842 scheduled, and clocking, and any additional properties defined in the
13843 entry. The return value is an alist, keys may occur multiple times
13844 if the property key was used several times.
13845 POM may also be nil, in which case the current entry is used.
13846 If WHICH is nil or `all', get all properties. If WHICH is
13847 `special' or `standard', only get that subclass."
13848 (setq which (or which 'all))
13849 (org-with-point-at pom
13850 (let ((clockstr (substring org-clock-string 0 -1))
13851 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
13852 beg end range props sum-props key value)
13854 (when (condition-case nil (org-back-to-heading t) (error nil))
13856 (setq sum-props (get-text-property (point) 'org-summaries))
13857 (outline-next-heading)
13859 (when (memq which '(all special))
13860 ;; Get the special properties, like TODO and tags
13862 (when (and (looking-at org-todo-line-regexp) (match-end 2))
13863 (push (cons "TODO" (org-match-string-no-properties 2)) props))
13864 (when (looking-at org-priority-regexp)
13865 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
13866 (when (and (setq value (org-get-tags)) (string-match "\\S-" value))
13867 (push (cons "TAGS" value) props))
13868 (when (setq value (org-get-tags-at))
13869 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
13871 (while (re-search-forward org-keyword-time-regexp end t)
13872 (setq key (substring (org-match-string-no-properties 1) 0 -1))
13873 (unless (member key excluded) (push key excluded))
13875 (if (equal key clockstr)
13879 (match-beginning 2) (point-at-eol))))
13880 (org-match-string-no-properties 2)))
13882 (when (memq which '(all standard))
13883 ;; Get the standard properties, like :PORP: ...
13884 (setq range (org-get-property-block beg end))
13886 (goto-char (car range))
13887 (while (re-search-forward
13888 "^[ \t]*:\\([a-zA-Z][a-zA-Z_0-9]*\\):[ \t]*\\(\\S-.*\\)?"
13890 (setq key (org-match-string-no-properties 1)
13891 value (org-trim (or (org-match-string-no-properties 2) "")))
13892 (unless (member key excluded)
13893 (push (cons key (or value "")) props)))))
13894 (append sum-props (nreverse props)))))))
13896 (defun org-entry-get (pom property &optional inherit)
13897 "Get value of PROPERTY for entry at point-or-marker POM.
13898 If INHERIT is non-nil and the entry does not have the property,
13899 then also check higher levels of the hierarchy.
13900 If the property is present but empty, the return value is the empty string.
13901 If the property is not present at all, nil is returned."
13902 (org-with-point-at pom
13904 (org-entry-get-with-inheritance property)
13905 (if (member property org-special-properties)
13906 ;; We need a special property. Use brute force, get all properties.
13907 (cdr (assoc property (org-entry-properties nil 'special)))
13908 (let ((range (org-get-property-block)))
13910 (goto-char (car range))
13912 (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
13914 ;; Found the property, return it.
13916 (org-match-string-no-properties 1)
13919 (defun org-entry-delete (pom property)
13920 "Delete the property PROPERTY from entry at point-or-marker POM."
13921 (org-with-point-at pom
13922 (if (member property org-special-properties)
13923 nil ; cannot delete these properties.
13924 (let ((range (org-get-property-block)))
13926 (goto-char (car range))
13928 (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
13931 (delete-region (match-beginning 0) (1+ (point-at-eol)))
13935 (defvar org-entry-property-inherited-from (make-marker))
13937 (defun org-entry-get-with-inheritance (property)
13938 "Get entry property, and search higher levels if not present."
13943 (when (setq tmp (org-entry-get nil property))
13944 (org-back-to-heading t)
13945 (move-marker org-entry-property-inherited-from (point))
13947 (condition-case nil
13948 (org-up-heading-all 1)
13949 (error (throw 'ex nil))))))
13950 (or tmp (cdr (assoc property org-local-properties))
13951 (cdr (assoc property org-global-properties)))))
13953 (defun org-entry-put (pom property value)
13954 "Set PROPERTY to VALUE for entry at point-or-marker POM."
13955 (org-with-point-at pom
13956 (org-back-to-heading t)
13957 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
13960 ((equal property "TODO")
13961 (when (and (stringp value) (string-match "\\S-" value)
13962 (not (member value org-todo-keywords-1)))
13963 (error "\"%s\" is not a valid TODO state" value))
13964 (if (or (not value)
13965 (not (string-match "\\S-" value)))
13966 (setq value 'none))
13968 (org-set-tags nil 'align))
13969 ((equal property "PRIORITY")
13970 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
13971 (string-to-char value) ?\ ))
13972 (org-set-tags nil 'align))
13973 ((equal property "SCHEDULED")
13974 (if (re-search-forward org-scheduled-time-regexp end t)
13976 ((eq value 'earlier) (org-timestamp-change -1 'day))
13977 ((eq value 'later) (org-timestamp-change 1 'day))
13978 (t (call-interactively 'org-schedule)))
13979 (call-interactively 'org-schedule)))
13980 ((equal property "DEADLINE")
13981 (if (re-search-forward org-deadline-time-regexp end t)
13983 ((eq value 'earlier) (org-timestamp-change -1 'day))
13984 ((eq value 'later) (org-timestamp-change 1 'day))
13985 (t (call-interactively 'org-deadline)))
13986 (call-interactively 'org-deadline)))
13987 ((member property org-special-properties)
13988 (error "The %s property can not yet be set with `org-entry-put'"
13990 (t ; a non-special property
13991 (setq range (org-get-property-block beg end 'force))
13992 (goto-char (car range))
13993 (if (re-search-forward
13994 (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
13996 (delete-region (match-beginning 1) (match-end 1))
13997 (goto-char (match-beginning 1)))
13998 (goto-char (cdr range))
14001 (org-indent-line-function)
14002 (insert ":" property ":"))
14003 (and value (insert " " value))
14004 (org-indent-line-function))))))
14006 (defun org-buffer-property-keys (&optional include-specials)
14007 "Get all property keys in the current buffer."
14012 (goto-char (point-min))
14013 (while (re-search-forward org-property-start-re nil t)
14014 (setq range (org-get-property-block))
14015 (goto-char (car range))
14016 (while (re-search-forward "^[ \t]*:\\([a-zA-Z0-9]+\\):" (cdr range) t)
14017 (add-to-list 'rtn (org-match-string-no-properties 1)))
14018 (outline-next-heading))))
14019 (when include-specials
14020 (setq rtn (append org-special-properties rtn)))
14021 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
14023 (defun org-insert-property-drawer ()
14024 "Insert a property drawer into the current entry."
14026 (org-back-to-heading t)
14027 (let ((beg (point))
14028 (re (concat "^[ \t]*" org-keyword-time-regexp))
14030 (outline-next-heading)
14033 (while (re-search-forward re end t))
14034 (setq hiddenp (org-invisible-p))
14036 (insert "\n:PROPERTIES:\n:END:")
14037 (beginning-of-line 0)
14038 (org-indent-line-function)
14039 (beginning-of-line 2)
14040 (org-indent-line-function)
14041 (beginning-of-line 0)
14044 (org-back-to-heading t)
14046 (org-flag-drawer t))))
14048 (defun org-set-property (property value)
14049 "In the current entry, set PROPERTY to VALUE."
14051 (let* ((prop (completing-read "Property: "
14052 (mapcar 'list (org-buffer-property-keys))))
14053 (cur (org-entry-get nil prop))
14054 (allowed (org-property-get-allowed-values nil prop 'table))
14056 (completing-read "Value: " allowed nil 'req-match)
14058 (concat "Value" (if (and cur (string-match "\\S-" cur))
14059 (concat "[" cur "]") "")
14062 (list prop (if (equal val "") cur val))))
14063 (unless (equal (org-entry-get nil property) value)
14064 (org-entry-put nil property value)))
14066 (defun org-delete-property (property)
14067 "In the current entry, delete PROPERTY."
14069 (let* ((prop (completing-read
14070 "Property: " (org-entry-properties nil 'standard))))
14072 (message (concat "Property " property
14073 (if (org-entry-delete nil property)
14075 " was not present in the entry"))))
14077 (defun org-delete-property-globally (property)
14078 "Remove PROPERTY globally, from all entries."
14080 (let* ((prop (completing-read
14081 "Globally remove property: "
14082 (mapcar 'list (org-buffer-property-keys)))))
14087 (goto-char (point-min))
14089 (while (re-search-forward
14090 (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
14092 (setq cnt (1+ cnt))
14093 (replace-match ""))
14094 (message "Property \"%s\" removed from %d entries" property cnt)))))
14096 (defvar org-columns-current-fmt-compiled) ; defined below
14098 (defun org-compute-property-at-point ()
14101 (unless (org-at-property-p)
14102 (error "Not at a property"))
14103 (let ((prop (org-match-string-no-properties 2)))
14104 (org-columns-get-format-and-top-level)
14105 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
14106 (error "No operator defined for property %s" prop))
14107 (org-columns-compute prop)))
14109 (defun org-property-get-allowed-values (pom property &optional table)
14110 "Get allowed values for the property PROPERTY.
14111 When TABLE is non-nil, return an alist that can directly be used for
14115 ((equal property "TODO")
14116 (setq vals (org-with-point-at pom
14117 (append org-todo-keywords-1 '("")))))
14118 ((equal property "PRIORITY")
14119 (let ((n org-lowest-priority))
14120 (while (>= n org-highest-priority)
14121 (push (char-to-string n) vals)
14123 ((member property org-special-properties))
14125 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
14127 (when (and vals (string-match "\\S-" vals))
14128 (setq vals (car (read-from-string (concat "(" vals ")"))))
14129 (setq vals (mapcar (lambda (x)
14130 (cond ((stringp x) x)
14131 ((numberp x) (number-to-string x))
14132 ((symbolp x) (symbol-name x))
14135 (if table (mapcar 'list vals) vals)))
14137 (defun org-property-previous-allowed-value (&optional previous)
14138 "Switch to the next allowed value for this property."
14140 (org-property-next-allowed-value t))
14142 (defun org-property-next-allowed-value (&optional previous)
14143 "Switch to the next allowed value for this property."
14145 (unless (org-at-property-p)
14146 (error "Not at a property"))
14147 (let* ((key (match-string 2))
14148 (value (match-string 3))
14149 (allowed (or (org-property-get-allowed-values (point) key)
14150 (and (member value '("[ ]" "[-]" "[X]"))
14154 (error "Allowed values for this property have not been defined"))
14155 (if previous (setq allowed (reverse allowed)))
14156 (if (member value allowed)
14157 (setq nval (car (cdr (member value allowed)))))
14158 (setq nval (or nval (car allowed)))
14159 (if (equal nval value)
14160 (error "Only one allowed value for this property"))
14161 (org-at-property-p)
14162 (replace-match (concat " :" key ": " nval) t t)
14163 (org-indent-line-function)
14164 (beginning-of-line 1)
14165 (skip-chars-forward " \t")))
14169 (defvar org-columns-overlays nil
14170 "Holds the list of current column overlays.")
14172 (defvar org-columns-current-fmt nil
14173 "Local variable, holds the currently active column format.")
14174 (defvar org-columns-current-fmt-compiled nil
14175 "Local variable, holds the currently active column format.
14176 This is the compiled version of the format.")
14177 (defvar org-columns-current-maxwidths nil
14178 "Loval variable, holds the currently active maximum column widths.")
14179 (defvar org-columns-begin-marker (make-marker)
14180 "Points to the position where last a column creation command was called.")
14181 (defvar org-columns-top-level-marker (make-marker)
14182 "Points to the position where current columns region starts.")
14184 (defvar org-columns-map (make-sparse-keymap)
14185 "The keymap valid in column display.")
14187 (defun org-columns-content ()
14188 "Switch to contents view while in columns view."
14193 (org-defkey org-columns-map "c" 'org-columns-content)
14194 (org-defkey org-columns-map "o" 'org-overview)
14195 (org-defkey org-columns-map "e" 'org-columns-edit-value)
14196 (org-defkey org-columns-map "v" 'org-columns-show-value)
14197 (org-defkey org-columns-map "q" 'org-columns-quit)
14198 (org-defkey org-columns-map "r" 'org-columns-redo)
14199 (org-defkey org-columns-map [left] 'backward-char)
14200 (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
14201 (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
14202 (org-defkey org-columns-map [right] 'forward-char)
14203 (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
14204 (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
14205 (org-defkey org-columns-map "\C-c\C-c" 'org-columns-next-allowed-value)
14206 (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
14207 (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
14208 (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
14209 (org-defkey org-columns-map "<" 'org-columns-narrow)
14210 (org-defkey org-columns-map ">" 'org-columns-widen)
14211 (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
14212 (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
14213 (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
14214 (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
14216 (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
14218 ["Edit property" org-columns-edit-value t]
14219 ["Next allowed value" org-columns-next-allowed-value t]
14220 ["Previous allowed value" org-columns-previous-allowed-value t]
14221 ["Show full value" org-columns-show-value t]
14222 ["Edit allowed" org-columns-edit-allowed t]
14224 ["Edit column attributes" org-columns-edit-attributes t]
14225 ["Increase column width" org-columns-widen t]
14226 ["Decrease column width" org-columns-narrow t]
14228 ["Move column right" org-columns-move-right t]
14229 ["Move column left" org-columns-move-left t]
14230 ["Add column" org-columns-new t]
14231 ["Delete column" org-columns-delete t]
14233 ["CONTENTS" org-columns-content t]
14234 ["OVERVIEW" org-overview t]
14235 ["Refresh columns display" org-columns-redo t]
14237 ["Quit" org-columns-quit t]))
14239 (defun org-columns-new-overlay (beg end &optional string face)
14240 "Create a new column overlay and add it to the list."
14241 (let ((ov (org-make-overlay beg end)))
14242 (org-overlay-put ov 'face (or face 'secondary-selection))
14243 (org-overlay-display ov string face)
14244 (push ov org-columns-overlays)
14247 (defun org-columns-display-here (&optional props)
14248 "Overlay the current line with column display."
14250 (let* ((fmt org-columns-current-fmt-compiled)
14251 (beg (point-at-bol))
14252 (level-face (save-excursion
14253 (beginning-of-line 1)
14254 (looking-at "\\(\\**\\)\\(\\* \\)")
14255 (org-get-level-face 2)))
14256 (color (list :foreground
14257 (face-attribute (or level-face 'default) :foreground)))
14258 props pom property ass width f string ov column)
14259 ;; Check if the entry is in another buffer.
14261 (if (eq major-mode 'org-agenda-mode)
14262 (setq pom (or (get-text-property (point) 'org-hd-marker)
14263 (get-text-property (point) 'org-marker))
14264 props (if pom (org-entry-properties pom) nil))
14265 (setq props (org-entry-properties nil))))
14267 (while (setq column (pop fmt))
14268 (setq property (car column)
14269 ass (if (equal property "ITEM")
14274 (buffer-substring-no-properties
14275 (point-at-bol) (point-at-eol))))))
14276 (assoc property props))
14277 width (or (cdr (assoc property org-columns-current-maxwidths))
14279 f (format "%%-%d.%ds | " width width)
14280 string (format f (or (cdr ass) "")))
14281 ;; Create the overlay
14283 (setq ov (org-columns-new-overlay
14284 beg (setq beg (1+ beg)) string
14285 (list color 'org-column)))
14286 ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
14287 (org-overlay-put ov 'keymap org-columns-map)
14288 (org-overlay-put ov 'org-columns-key property)
14289 (org-overlay-put ov 'org-columns-value (cdr ass))
14290 (org-overlay-put ov 'org-columns-pom pom)
14291 (org-overlay-put ov 'org-columns-format f))
14292 (if (or (not (char-after beg))
14293 (equal (char-after beg) ?\n))
14294 (let ((inhibit-read-only t))
14298 ;; Make the rest of the line disappear.
14300 (setq ov (org-columns-new-overlay beg (point-at-eol)))
14301 (org-overlay-put ov 'invisible t)
14302 (org-overlay-put ov 'keymap org-columns-map)
14303 (org-overlay-put ov 'intangible t)
14304 (push ov org-columns-overlays)
14305 (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
14306 (org-overlay-put ov 'keymap org-columns-map)
14307 (push ov org-columns-overlays)
14308 (let ((inhibit-read-only t))
14309 (put-text-property (max (point-min) (1- (point-at-bol)))
14310 (min (point-max) (1+ (point-at-eol)))
14311 'read-only "Type `e' to edit property")))))
14313 (defvar org-previous-header-line-format nil
14314 "The header line format before column view was turned on.")
14315 (defvar org-columns-inhibit-recalculation nil
14316 "Inhibit recomputing of columns on column view startup.")
14318 (defvar header-line-format)
14319 (defun org-columns-display-here-title ()
14320 "Overlay the newline before the current line with the table title."
14322 (let ((fmt org-columns-current-fmt-compiled)
14324 property width f column str)
14325 (while (setq column (pop fmt))
14326 (setq property (car column)
14327 str (or (nth 1 column) property)
14328 width (or (cdr (assoc property org-columns-current-maxwidths))
14330 f (format "%%-%d.%ds | " width width)
14331 string (format f str)
14332 title (concat title string)))
14333 (setq title (concat
14334 (org-add-props " " nil 'display '(space :align-to 0))
14335 (org-add-props title nil 'face '(:weight bold :underline t))))
14336 (org-set-local 'org-previous-header-line-format header-line-format)
14337 (setq header-line-format title)))
14339 (defun org-columns-remove-overlays ()
14340 "Remove all currently active column overlays."
14342 (when (marker-buffer org-columns-begin-marker)
14343 (with-current-buffer (marker-buffer org-columns-begin-marker)
14344 (when (local-variable-p 'org-previous-header-line-format)
14345 (setq header-line-format org-previous-header-line-format)
14346 (kill-local-variable 'org-previous-header-line-format))
14347 (move-marker org-columns-begin-marker nil)
14348 (move-marker org-columns-top-level-marker nil)
14350 (mapc 'org-delete-overlay org-columns-overlays)
14351 (setq org-columns-overlays nil)
14352 (let ((inhibit-read-only t))
14353 (remove-text-properties (point-min) (point-max) '(read-only t)))))))
14355 (defun org-columns-show-value ()
14356 "Show the full value of the property."
14358 (let ((value (get-char-property (point) 'org-columns-value)))
14359 (message "Value is: %s" (or value ""))))
14361 (defun org-columns-quit ()
14362 "Remove the column overlays and in this way exit column editing."
14365 (org-columns-remove-overlays)
14366 (let ((inhibit-read-only t))
14367 ;; FIXME: is this safe???
14368 ;; or are there other reasons why there may be a read-only property????
14369 (remove-text-properties (point-min) (point-max) '(read-only t))))
14370 (when (eq major-mode 'org-agenda-mode)
14371 (message "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
14373 (defun org-columns-edit-value ()
14374 "Edit the value of the property at point in column view.
14375 Where possible, use the standard interface for changing this line."
14377 (let* ((col (current-column))
14378 (key (get-char-property (point) 'org-columns-key))
14379 (value (get-char-property (point) 'org-columns-value))
14380 (bol (point-at-bol)) (eol (point-at-eol))
14381 (pom (or (get-text-property bol 'org-hd-marker)
14382 (point))) ; keep despite of compiler waring
14384 (delq nil (mapcar (lambda (x)
14385 (and (eq (overlay-buffer x) (current-buffer))
14386 (>= (overlay-start x) bol)
14387 (<= (overlay-start x) eol)
14389 org-columns-overlays)))
14391 (when (equal key "ITEM")
14392 (error "Cannot edit item headline from here"))
14395 ((equal key "TODO")
14396 (setq eval '(org-with-point-at pom
14397 (let ((current-prefix-arg '(4))) (org-todo '(4))))))
14398 ((equal key "PRIORITY")
14399 (setq eval '(org-with-point-at pom
14400 (call-interactively 'org-priority))))
14401 ((equal key "TAGS")
14402 (setq eval '(org-with-point-at pom
14403 (let ((org-fast-tag-selection-single-key
14404 (if (eq org-fast-tag-selection-single-key 'expert)
14405 t org-fast-tag-selection-single-key)))
14406 (call-interactively 'org-set-tags)))))
14407 ((equal key "DEADLINE")
14408 (setq eval '(org-with-point-at pom
14409 (call-interactively 'org-deadline))))
14410 ((equal key "SCHEDULED")
14411 (setq eval '(org-with-point-at pom
14412 (call-interactively 'org-schedule))))
14414 (setq allowed (org-property-get-allowed-values pom key 'table))
14416 (setq nval (completing-read "Value: " allowed nil t))
14417 (setq nval (read-string "Edit: " value)))
14418 (setq nval (org-trim nval))
14419 (when (not (equal nval value))
14420 (setq eval '(org-entry-put pom key nval)))))
14422 (let ((inhibit-read-only t))
14423 (remove-text-properties (1- bol) eol '(read-only t))
14426 (setq org-columns-overlays
14427 (org-delete-all line-overlays org-columns-overlays))
14428 (mapc 'org-delete-overlay line-overlays)
14429 (org-columns-eval eval))
14430 (org-columns-display-here))))
14431 (move-to-column col)
14432 (if (nth 3 (assoc key org-columns-current-fmt-compiled))
14433 (org-columns-update key))))
14435 (defun org-columns-edit-allowed ()
14436 "Edit the list of allowed values for the current property."
14438 (let* ((col (current-column))
14439 (key (get-char-property (point) 'org-columns-key))
14440 (key1 (concat key "_ALL"))
14441 (value (get-char-property (point) 'org-columns-value))
14442 (allowed (org-entry-get (point) key1 t))
14444 (setq nval (read-string "Allowed: " allowed))
14446 (cond ((marker-position org-entry-property-inherited-from)
14447 org-entry-property-inherited-from)
14448 ((marker-position org-columns-top-level-marker)
14449 org-columns-top-level-marker))
14452 (defun org-columns-eval (form)
14455 (beginning-of-line 1)
14457 (setq hidep (org-on-heading-p 1)))
14459 (and hidep (hide-entry))))
14461 (defun org-columns-previous-allowed-value ()
14462 "Switch to the previous allowed value for this column."
14464 (org-columns-next-allowed-value t))
14466 (defun org-columns-next-allowed-value (&optional previous)
14467 "Switch to the next allowed value for this column."
14469 (let* ((col (current-column))
14470 (key (get-char-property (point) 'org-columns-key))
14471 (value (get-char-property (point) 'org-columns-value))
14472 (bol (point-at-bol)) (eol (point-at-eol))
14473 (pom (or (get-text-property bol 'org-hd-marker)
14474 (point))) ; keep despite of compiler waring
14476 (delq nil (mapcar (lambda (x)
14477 (and (eq (overlay-buffer x) (current-buffer))
14478 (>= (overlay-start x) bol)
14479 (<= (overlay-start x) eol)
14481 org-columns-overlays)))
14482 (allowed (or (org-property-get-allowed-values pom key)
14484 (nth 4 (assoc key org-columns-current-fmt-compiled))
14485 'checkbox) '("[ ]" "[X]"))))
14487 (when (equal key "ITEM")
14488 (error "Cannot edit item headline from here"))
14489 (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
14490 (error "Allowed values for this property have not been defined"))
14491 (if (member key '("SCHEDULED" "DEADLINE"))
14492 (setq nval (if previous 'earlier 'later))
14493 (if previous (setq allowed (reverse allowed)))
14494 (if (member value allowed)
14495 (setq nval (car (cdr (member value allowed)))))
14496 (setq nval (or nval (car allowed)))
14497 (if (equal nval value)
14498 (error "Only one allowed value for this property")))
14499 (let ((inhibit-read-only t))
14500 (remove-text-properties (1- bol) eol '(read-only t))
14503 (setq org-columns-overlays
14504 (org-delete-all line-overlays org-columns-overlays))
14505 (mapc 'org-delete-overlay line-overlays)
14506 (org-columns-eval '(org-entry-put pom key nval)))
14507 (org-columns-display-here)))
14508 (move-to-column col)
14509 (if (nth 3 (assoc key org-columns-current-fmt-compiled))
14510 (org-columns-update key))))
14512 (defun org-verify-version (task)
14514 ((eq task 'columns)
14515 (if (or (featurep 'xemacs)
14516 (< emacs-major-version 22))
14517 (error "Emacs 22 is required for the columns feature")))))
14519 (defun org-columns-get-format-and-top-level ()
14521 (when (condition-case nil (org-back-to-heading) (error nil))
14522 (move-marker org-entry-property-inherited-from nil)
14523 (setq fmt (org-entry-get nil "COLUMNS" t)))
14524 (setq fmt (or fmt org-columns-default-format))
14525 (org-set-local 'org-columns-current-fmt fmt)
14526 (org-columns-compile-format fmt)
14527 (if (marker-position org-entry-property-inherited-from)
14528 (move-marker org-columns-top-level-marker
14529 org-entry-property-inherited-from)
14530 (move-marker org-columns-top-level-marker (point)))
14533 (defun org-columns ()
14534 "Turn on column view on an org-mode file."
14536 (org-verify-version 'columns)
14537 (org-columns-remove-overlays)
14538 (move-marker org-columns-begin-marker (point))
14539 (let (beg end fmt cache maxwidths)
14540 (setq fmt (org-columns-get-format-and-top-level))
14542 (goto-char org-columns-top-level-marker)
14544 (unless org-columns-inhibit-recalculation
14545 (org-columns-compute-all))
14546 (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
14549 ;; Get and cache the properties
14550 (while (re-search-forward (concat "^" outline-regexp) end t)
14551 (push (cons (org-current-line) (org-entry-properties)) cache))
14553 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
14554 (org-set-local 'org-columns-current-maxwidths maxwidths)
14555 (org-columns-display-here-title)
14557 (goto-line (car x))
14558 (org-columns-display-here (cdr x)))
14561 (defun org-columns-new (&optional prop title width op fmt)
14562 "Insert a new column, to the leeft o the current column."
14564 (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
14566 (setq prop (completing-read
14567 "Property: " (mapcar 'list (org-buffer-property-keys t))
14569 (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
14570 (setq width (read-string "Column width: " (if width (number-to-string width))))
14571 (if (string-match "\\S-" width)
14572 (setq width (string-to-number width))
14574 (setq fmt (completing-read "Summary [none]: "
14575 '(("none") ("add_numbers") ("add_times") ("checkbox"))
14577 (if (string-match "\\S-" fmt)
14578 (setq fmt (intern fmt))
14580 (if (eq fmt 'none) (setq fmt nil))
14583 (setcar editp prop)
14584 (setcdr editp (list title width nil fmt)))
14585 (setq cell (nthcdr (1- (current-column))
14586 org-columns-current-fmt-compiled))
14587 (setcdr cell (cons (list prop title width nil fmt)
14589 (org-columns-store-format)
14590 (org-columns-redo)))
14592 (defun org-columns-delete ()
14593 "Delete the column at point from columns view."
14595 (let* ((n (current-column))
14596 (title (nth 1 (nth n org-columns-current-fmt-compiled))))
14598 (format "Are you sure you want to remove column \"%s\"? " title))
14599 (setq org-columns-current-fmt-compiled
14600 (delq (nth n org-columns-current-fmt-compiled)
14601 org-columns-current-fmt-compiled))
14602 (org-columns-store-format)
14604 (if (>= (current-column) (length org-columns-current-fmt-compiled))
14605 (backward-char 1)))))
14607 (defun org-columns-edit-attributes ()
14608 "Edit the attributes of the current column."
14610 (let* ((n (current-column))
14611 (info (nth n org-columns-current-fmt-compiled)))
14612 (apply 'org-columns-new info)))
14614 (defun org-columns-widen (arg)
14615 "Make the column wider by ARG characters."
14617 (let* ((n (current-column))
14618 (entry (nth n org-columns-current-fmt-compiled))
14619 (width (or (nth 2 entry)
14620 (cdr (assoc (car entry) org-columns-current-maxwidths)))))
14621 (setq width (max 1 (+ width arg)))
14622 (setcar (nthcdr 2 entry) width)
14623 (org-columns-store-format)
14624 (org-columns-redo)))
14626 (defun org-columns-narrow (arg)
14627 "Make the column nrrower by ARG characters."
14629 (org-columns-widen (- arg)))
14631 (defun org-columns-move-right ()
14632 "Swap this column with the one to the right."
14634 (let* ((n (current-column))
14635 (cell (nthcdr n org-columns-current-fmt-compiled))
14637 (when (>= n (1- (length org-columns-current-fmt-compiled)))
14638 (error "Cannot shift this column further to the right"))
14639 (setq e (car cell))
14640 (setcar cell (car (cdr cell)))
14641 (setcdr cell (cons e (cdr (cdr cell))))
14642 (org-columns-store-format)
14646 (defun org-columns-move-left ()
14647 "Swap this column with the one to the left."
14649 (let* ((n (current-column)))
14651 (error "Cannot shift this column further to the left"))
14653 (org-columns-move-right)
14654 (backward-char 1)))
14656 (defun org-columns-store-format ()
14657 "Store the text version of the current columns format in appropriate place.
14658 This is either in the COLUMNS property of the node starting the current column
14659 display, or in the #+COLUMNS line of the current buffer."
14661 (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
14662 (if (marker-position org-columns-top-level-marker)
14664 (goto-char org-columns-top-level-marker)
14665 (if (org-entry-get nil "COLUMNS")
14666 (org-entry-put nil "COLUMNS" fmt)
14667 (goto-char (point-min))
14668 (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
14669 (replace-match (concat "#+COLUMNS: " fmt t t)))))
14670 (setq org-columns-current-fmt fmt))))
14672 (defvar org-overriding-columns-format nil
14673 "When set, overrides any other definition.")
14674 (defvar org-agenda-view-columns-initially nil
14675 "When set, switch to columns view immediately after creating the agenda.")
14677 (defun org-agenda-columns ()
14678 "Turn on column view in the agenda."
14680 (org-verify-version 'columns)
14681 (org-columns-remove-overlays)
14682 (move-marker org-columns-begin-marker (point))
14683 (let (fmt cache maxwidths m)
14685 ((and (local-variable-p 'org-overriding-columns-format)
14686 org-overriding-columns-format)
14687 (setq fmt org-overriding-columns-format))
14688 ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
14689 (setq fmt (org-entry-get m "COLUMNS" t)))
14690 ((and (boundp 'org-columns-current-fmt)
14691 (local-variable-p 'org-columns-current-fmt)
14692 org-columns-current-fmt)
14693 (setq fmt org-columns-current-fmt))
14694 ((setq m (next-single-property-change (point-min) 'org-hd-marker))
14695 (setq m (get-text-property m 'org-hd-marker))
14696 (setq fmt (org-entry-get m "COLUMNS" t))))
14697 (setq fmt (or fmt org-columns-default-format))
14698 (org-set-local 'org-columns-current-fmt fmt)
14699 (org-columns-compile-format fmt)
14701 ;; Get and cache the properties
14702 (goto-char (point-min))
14703 (while (not (eobp))
14704 (when (setq m (or (get-text-property (point) 'org-hd-marker)
14705 (get-text-property (point) 'org-marker)))
14706 (push (cons (org-current-line) (org-entry-properties m)) cache))
14707 (beginning-of-line 2))
14709 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
14710 (org-set-local 'org-columns-current-maxwidths maxwidths)
14711 (org-columns-display-here-title)
14713 (goto-line (car x))
14714 (org-columns-display-here (cdr x)))
14717 (defun org-columns-get-autowidth-alist (s cache)
14718 "Derive the maximum column widths from the format and the cache."
14719 (let ((start 0) rtn)
14720 (while (string-match "%\\([a-zA-Z]\\S-*\\)" s start)
14721 (push (cons (match-string 1 s) 1) rtn)
14722 (setq start (match-end 0)))
14724 (setcdr x (apply 'max
14727 (length (or (cdr (assoc (car x) (cdr y))) " ")))
14732 (defun org-columns-compute-all ()
14733 "Compute all columns that have operators defined."
14735 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
14736 (let ((columns org-columns-current-fmt-compiled) col)
14737 (while (setq col (pop columns))
14740 (org-columns-compute (car col)))))))
14742 (defun org-columns-update (property)
14743 "Recompute PROPERTY, and update the columns display for it."
14744 (org-columns-compute property)
14748 (when (equal (org-overlay-get ov 'org-columns-key) property)
14749 (setq pos (org-overlay-start ov))
14751 (when (setq val (cdr (assoc property
14752 (get-text-property (point-at-bol) 'org-summaries))))
14753 (setq fmt (org-overlay-get ov 'org-columns-format))
14754 (org-overlay-put ov 'display (format fmt val)))))
14755 org-columns-overlays))))
14757 (defun org-columns-compute (property)
14758 "Sum the values of property PROPERTY hierarchically, for the entire buffer."
14760 (let* ((re (concat "^" outline-regexp))
14761 (lmax 30) ; Does anyone use deeper levels???
14762 (lsum (make-vector lmax 0))
14764 (ass (assoc property org-columns-current-fmt-compiled))
14765 (format (nth 4 ass))
14766 (beg org-columns-top-level-marker)
14767 last-level val end sumpos sum-alist sum str)
14769 ;; Find the region to compute
14771 (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
14773 ;; Walk the tree from the back and do the computations
14774 (while (re-search-backward re beg t)
14775 (setq sumpos (match-beginning 0)
14777 level (org-outline-level)
14778 val (org-entry-get nil property))
14780 ((< level last-level)
14781 ;; put the sum of lower levels here as a property
14782 (setq sum (aref lsum last-level)
14783 str (org-column-number-to-string sum format)
14784 sum-alist (get-text-property sumpos 'org-summaries))
14785 (if (assoc property sum-alist)
14786 (setcdr (assoc property sum-alist) str)
14787 (push (cons property str) sum-alist)
14789 (add-text-properties sumpos (1+ sumpos)
14790 (list 'org-summaries sum-alist))))
14791 (when val ;?????????????????????????????????? and force?????
14792 (org-entry-put nil property str))
14793 ;; add current to current level accumulator
14794 (aset lsum level (+ (aref lsum level) sum))
14795 ;; clear accumulators for deeper levels
14796 (loop for l from (1+ level) to (1- lmax) do (aset lsum l 0)))
14797 ((>= level last-level)
14798 ;; add what we have here to the accumulator for this level
14799 (aset lsum level (+ (aref lsum level)
14800 (org-column-string-to-number (or val "0") format))))
14801 (t (error "This should not happen")))))))
14803 (defun org-columns-redo ()
14804 "Construct the column display again."
14806 (message "Recomputing columns...")
14808 (if (marker-position org-columns-begin-marker)
14809 (goto-char org-columns-begin-marker))
14810 (org-columns-remove-overlays)
14812 (call-interactively 'org-columns)
14813 (call-interactively 'org-agenda-columns)))
14814 (message "Recomputing columns...done"))
14816 (defun org-columns-not-in-agenda ()
14817 (if (eq major-mode 'org-agenda-mode)
14818 (error "This command is only allowed in Org-mode buffers")))
14821 (defun org-string-to-number (s)
14822 "Convert string to number, and interpret hh:mm:ss."
14823 (if (not (string-match ":" s))
14824 (string-to-number s)
14825 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
14827 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
14830 (defun org-column-number-to-string (n fmt)
14831 "Convert a computed column number to a string value, according to FMT."
14833 ((eq fmt 'add_times)
14834 (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
14835 (format "%d:%02d" h m)))
14836 ((eq fmt 'checkbox)
14837 (cond ((= n (floor n)) "[X]")
14840 (t (number-to-string n))))
14842 (defun org-column-string-to-number (s fmt)
14843 "Convert a column value to a number that can be used for column computing."
14845 ((string-match ":" s)
14846 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
14848 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
14850 ((eq fmt 'checkbox)
14851 (if (equal s "[X]") 1. 0.000001))
14852 (t (string-to-number s))))
14854 (defun org-columns-uncompile-format (cfmt)
14855 "Turn the compiled columns format back into a string representation."
14856 (let ((rtn "") e s prop title op width fmt)
14857 (while (setq e (pop cfmt))
14864 ((eq fmt 'add_times) (setq op ":"))
14865 ((eq fmt 'checkbox) (setq op "X"))
14866 ((eq fmt 'add_numbers) (setq op "+")))
14867 (if (equal title prop) (setq title nil))
14868 (setq s (concat "%" (if width (number-to-string width))
14870 (if title (concat "(" title ")"))
14871 (if op (concat "{" op "}"))))
14872 (setq rtn (concat rtn " " s)))
14875 (defun org-columns-compile-format (fmt)
14877 (let ((start 0) width prop title op f)
14878 (setq org-columns-current-fmt-compiled nil)
14879 (while (string-match "%\\([0-9]+\\)?\\([a-zA-Z_0-9]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*"
14881 (setq start (match-end 0)
14882 width (match-string 1 fmt)
14883 prop (match-string 2 fmt)
14884 title (or (match-string 3 fmt) prop)
14885 op (match-string 4 fmt)
14887 (if width (setq width (string-to-number width)))
14889 ((equal op "+") (setq f 'add_numbers))
14890 ((equal op ":") (setq f 'add_times))
14891 ((equal op "X") (setq f 'checkbox)))
14892 (push (list prop title width op f) org-columns-current-fmt-compiled))
14893 (setq org-columns-current-fmt-compiled
14894 (nreverse org-columns-current-fmt-compiled))))
14898 (defvar org-last-changed-timestamp nil)
14899 (defvar org-time-was-given) ; dynamically scoped parameter
14900 (defvar org-end-time-was-given) ; dynamically scoped parameter
14901 (defvar org-ts-what) ; dynamically scoped parameter
14903 (defun org-time-stamp (arg)
14904 "Prompt for a date/time and insert a time stamp.
14905 If the user specifies a time like HH:MM, or if this command is called
14906 with a prefix argument, the time stamp will contain date and time.
14907 Otherwise, only the date will be included. All parts of a date not
14908 specified by the user will be filled in from the current date/time.
14909 So if you press just return without typing anything, the time stamp
14910 will represent the current date/time. If there is already a timestamp
14911 at the cursor, it will be modified."
14913 (let (org-time-was-given org-end-time-was-given time)
14915 ((and (org-at-timestamp-p)
14916 (eq last-command 'org-time-stamp)
14917 (eq this-command 'org-time-stamp))
14919 (setq time (let ((this-command this-command))
14920 (org-read-date arg 'totime)))
14921 (org-insert-time-stamp time (or org-time-was-given arg)))
14922 ((org-at-timestamp-p)
14923 (setq time (let ((this-command this-command))
14924 (org-read-date arg 'totime)))
14925 (when (org-at-timestamp-p) ; just to get the match data
14927 (setq org-last-changed-timestamp
14928 (org-insert-time-stamp
14929 time (or org-time-was-given arg)
14930 nil nil nil (list org-end-time-was-given))))
14931 (message "Timestamp updated"))
14933 (setq time (let ((this-command this-command))
14934 (org-read-date arg 'totime)))
14935 (org-insert-time-stamp time (or org-time-was-given arg)
14936 nil nil nil (list org-end-time-was-given))))))
14938 (defun org-time-stamp-inactive (&optional arg)
14939 "Insert an inactive time stamp.
14940 An inactive time stamp is enclosed in square brackets instead of angle
14941 brackets. It is inactive in the sense that it does not trigger agenda entries,
14942 does not link to the calendar and cannot be changed with the S-cursor keys.
14943 So these are more for recording a certain time/date."
14945 (let (org-time-was-given org-end-time-was-given time)
14946 (setq time (org-read-date arg 'totime))
14947 (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
14948 nil nil (list org-end-time-was-given))))
14950 (defvar org-date-ovl (org-make-overlay 1 1))
14951 (org-overlay-put org-date-ovl 'face 'org-warning)
14952 (org-detach-overlay org-date-ovl)
14954 (defvar org-ans1) ; dynamically scoped parameter
14955 (defvar org-ans2) ; dynamically scoped parameter
14957 (defvar org-plain-time-of-day-regexp) ; defined below
14958 (defun org-read-date (&optional with-time to-time from-string prompt)
14959 "Read a date and make things smooth for the user.
14960 The prompt will suggest to enter an ISO date, but you can also enter anything
14961 which will at least partially be understood by `parse-time-string'.
14962 Unrecognized parts of the date will default to the current day, month, year,
14963 hour and minute. For example,
14964 3-2-5 --> 2003-02-05
14965 feb 15 --> currentyear-02-15
14966 sep 12 9 --> 2009-09-12
14967 12:45 --> today 12:45
14968 22 sept 0:34 --> currentyear-09-22 0:34
14969 12 --> currentyear-currentmonth-12
14970 Fri --> nearest Friday (today or later)
14971 +4 --> four days from today (only if +N is the only thing given)
14973 The function understands only English month and weekday abbreviations,
14974 but this can be configured with the variables `parse-time-months' and
14975 `parse-time-weekdays'.
14977 While prompting, a calendar is popped up - you can also select the
14978 date with the mouse (button 1). The calendar shows a period of three
14979 months. To scroll it to other months, use the keys `>' and `<'.
14980 If you don't like the calendar, turn it off with
14981 \(setq org-popup-calendar-for-date-prompt nil)
14983 With optional argument TO-TIME, the date will immediately be converted
14984 to an internal time.
14985 With an optional argument WITH-TIME, the prompt will suggest to also
14986 insert a time. Note that when WITH-TIME is not set, you can still
14987 enter a time, and this function will inform the calling routine about
14988 this change. The calling routine may then choose to change the format
14989 used to insert the time stamp into the buffer to include the time."
14990 (require 'parse-time)
14991 (let* ((org-time-stamp-rounding-minutes
14992 (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
14993 (ct (org-current-time))
14995 ;; Default time is either today, or, when entering a range,
14996 ;; the range start.
14997 (if (save-excursion
14998 (re-search-backward
14999 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
15003 (mapcar (lambda(x) (or x 0))
15004 (parse-time-string (match-string 1))))
15006 (calendar-move-hook nil)
15007 (view-diary-entries-initially nil)
15008 (view-calendar-holidays-initially nil)
15009 (timestr (format-time-string
15010 (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") default-time))
15011 (prompt (concat (if prompt (concat prompt " ") "")
15012 (format "Date and/or time (default [%s]): " timestr)))
15013 ans (org-ans0 "") org-ans1 org-ans2 (deltadays 0)
15014 second minute hour day month year tl wday wday1 pm)
15017 (from-string (setq ans from-string))
15018 (org-popup-calendar-for-date-prompt
15020 (save-window-excursion
15022 (calendar-forward-day (- (time-to-days default-time)
15023 (calendar-absolute-from-gregorian
15024 (calendar-current-date))))
15025 (org-eval-in-calendar nil t)
15026 (let* ((old-map (current-local-map))
15027 (map (copy-keymap calendar-mode-map))
15028 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
15029 (org-defkey map (kbd "RET") 'org-calendar-select)
15030 (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
15031 'org-calendar-select-mouse)
15032 (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2
])
15033 'org-calendar-select-mouse
)
15034 (org-defkey minibuffer-local-map
[(meta shift left
)]
15035 (lambda () (interactive)
15036 (org-eval-in-calendar '(calendar-backward-month 1))))
15037 (org-defkey minibuffer-local-map
[(meta shift right
)]
15038 (lambda () (interactive)
15039 (org-eval-in-calendar '(calendar-forward-month 1))))
15040 (org-defkey minibuffer-local-map
[(shift up
)]
15041 (lambda () (interactive)
15042 (org-eval-in-calendar '(calendar-backward-week 1))))
15043 (org-defkey minibuffer-local-map
[(shift down
)]
15044 (lambda () (interactive)
15045 (org-eval-in-calendar '(calendar-forward-week 1))))
15046 (org-defkey minibuffer-local-map
[(shift left
)]
15047 (lambda () (interactive)
15048 (org-eval-in-calendar '(calendar-backward-day 1))))
15049 (org-defkey minibuffer-local-map
[(shift right
)]
15050 (lambda () (interactive)
15051 (org-eval-in-calendar '(calendar-forward-day 1))))
15052 (org-defkey minibuffer-local-map
">"
15053 (lambda () (interactive)
15054 (org-eval-in-calendar '(scroll-calendar-left 1))))
15055 (org-defkey minibuffer-local-map
"<"
15056 (lambda () (interactive)
15057 (org-eval-in-calendar '(scroll-calendar-right 1))))
15060 (use-local-map map
)
15061 (setq org-ans0
(read-string prompt
"" nil nil
))
15062 ;; org-ans0: from prompt
15063 ;; org-ans1: from mouse click
15064 ;; org-ans2: from calendar motion
15065 (setq ans
(concat org-ans0
" " (or org-ans1 org-ans2
))))
15066 (use-local-map old-map
))))))
15067 (t ; Naked prompt only
15068 (setq ans
(read-string prompt
"" nil timestr
))))
15069 (org-detach-overlay org-date-ovl
)
15071 (if (string-match "^[ \t]*[-+][0-9]+[ \t]*$" org-ans0
)
15072 (setq deltadays
(string-to-number ans
) ans
""))
15074 ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
15075 (when (string-match
15076 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans
)
15077 (setq year
(if (match-end 2)
15078 (string-to-number (match-string 2 ans
))
15079 (string-to-number (format-time-string "%Y")))
15080 month
(string-to-number (match-string 3 ans
))
15081 day
(string-to-number (match-string 4 ans
)))
15082 (if (< year
100) (setq year
(+ 2000 year
)))
15083 (setq ans
(replace-match (format "%04d-%02d-%02d\\5" year month day
)
15085 ;; Help matching am/pm times, because `parse-time-string' does not do that.
15086 ;; If there is a time with am/pm, and *no* time without it, we convert
15087 ;; so that matching will be successful.
15088 ;; FIXME: make this replace twice, so that we catch the end time.
15089 (when (and (not (string-match "[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans
))
15090 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans
))
15091 (setq hour
(string-to-number (match-string 1 ans
))
15092 minute
(if (match-end 3) (string-to-number (match-string 3 ans
)) 0)
15093 pm
(equal ?p
(string-to-char (downcase (match-string 4 ans
)))))
15094 (if (and (= hour
12) (not pm
))
15096 (if (and pm
(< hour
12)) (setq hour
(+ 12 hour
))))
15097 (setq ans
(replace-match (format "%02d:%02d" hour minute
) t t ans
)))
15099 ;; Check if there is a time range
15100 (when (and (boundp 'org-end-time-was-given
)
15101 (string-match org-plain-time-of-day-regexp ans
)
15103 (setq org-end-time-was-given
(match-string 8 ans
))
15104 (setq ans
(concat (substring ans
0 (match-beginning 7))
15105 (substring ans
(match-end 7)))))
15107 (setq tl
(parse-time-string ans
)
15108 year
(or (nth 5 tl
) (string-to-number (format-time-string "%Y" ct
)))
15109 month
(or (nth 4 tl
) (string-to-number (format-time-string "%m" ct
)))
15110 day
(or (nth 3 tl
) (string-to-number (format-time-string "%d" ct
)))
15111 hour
(or (nth 2 tl
) (string-to-number (format-time-string "%H" ct
)))
15112 minute
(or (nth 1 tl
) (string-to-number (format-time-string "%M" ct
)))
15113 second
(or (nth 0 tl
) 0)
15115 (setq day
(+ day deltadays
))
15116 (when (and wday
(not (nth 3 tl
)))
15117 ;; Weekday was given, but no day, so pick that day in the week
15118 ;; on or after the derived date.
15119 (setq wday1
(nth 6 (decode-time (encode-time 0 0 0 day month year
))))
15120 (unless (equal wday wday1
)
15121 (setq day
(+ day
(%
(- wday wday1 -
7) 7)))))
15122 (if (and (boundp 'org-time-was-given
)
15124 (setq org-time-was-given t
))
15125 (if (< year
100) (setq year
(+ 2000 year
)))
15127 (encode-time second minute hour day month year
)
15128 (if (or (nth 1 tl
) (nth 2 tl
))
15129 (format "%04d-%02d-%02d %02d:%02d" year month day hour minute
)
15130 (format "%04d-%02d-%02d" year month day
)))))
15132 (defun org-eval-in-calendar (form &optional keepdate
)
15133 "Eval FORM in the calendar window and return to current window.
15134 Also, store the cursor date in variable org-ans2."
15135 (let ((sw (selected-window)))
15136 (select-window (get-buffer-window "*Calendar*"))
15138 (when (and (not keepdate
) (calendar-cursor-to-date))
15139 (let* ((date (calendar-cursor-to-date))
15140 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
15141 (setq org-ans2
(format-time-string "%Y-%m-%d" time
))))
15142 (org-move-overlay org-date-ovl
(1- (point)) (1+ (point)) (current-buffer))
15144 ;; Update the prompt to show new default date
15146 (goto-char (point-min))
15147 (when (and org-ans2
15148 (re-search-forward "\\[[-0-9]+\\]" nil t
)
15149 (get-text-property (match-end 0) 'field
))
15150 (let ((inhibit-read-only t
))
15151 (replace-match (concat "[" org-ans2
"]") t t
)
15152 (add-text-properties (point-min) (1+ (match-end 0))
15153 (text-properties-at (1+ (point-min)))))))))
15155 (defun org-calendar-select ()
15156 "Return to `org-read-date' with the date currently selected.
15157 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
15159 (when (calendar-cursor-to-date)
15160 (let* ((date (calendar-cursor-to-date))
15161 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
15162 (setq org-ans1
(format-time-string "%Y-%m-%d" time
)))
15163 (if (active-minibuffer-window) (exit-minibuffer))))
15165 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra
)
15166 "Insert a date stamp for the date given by the internal TIME.
15167 WITH-HM means, use the stamp format that includes the time of the day.
15168 INACTIVE means use square brackets instead of angular ones, so that the
15169 stamp will not contribute to the agenda.
15170 PRE and POST are optional strings to be inserted before and after the
15172 The command returns the inserted time stamp."
15173 (let ((fmt (funcall (if with-hm
'cdr
'car
) org-time-stamp-formats
))
15175 (if inactive
(setq fmt
(concat "[" (substring fmt
1 -
1) "]")))
15176 (insert (or pre
""))
15177 (insert (setq stamp
(format-time-string fmt time
)))
15178 (when (listp extra
)
15179 (setq extra
(car extra
))
15180 (if (and (stringp extra
)
15181 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra
))
15182 (setq extra
(format "-%02d:%02d"
15183 (string-to-number (match-string 1 extra
))
15184 (string-to-number (match-string 2 extra
))))
15190 (insert (or post
""))
15193 (defun org-toggle-time-stamp-overlays ()
15194 "Toggle the use of custom time stamp formats."
15196 (setq org-display-custom-times
(not org-display-custom-times
))
15197 (unless org-display-custom-times
15198 (let ((p (point-min)) (bmp (buffer-modified-p)))
15199 (while (setq p
(next-single-property-change p
'display
))
15200 (if (and (get-text-property p
'display
)
15201 (eq (get-text-property p
'face
) 'org-date
))
15202 (remove-text-properties
15203 p
(setq p
(next-single-property-change p
'display
))
15205 (set-buffer-modified-p bmp
)))
15206 (if (featurep 'xemacs
)
15207 (remove-text-properties (point-min) (point-max) '(end-glyph t
)))
15208 (org-restart-font-lock)
15209 (setq org-table-may-need-update t
)
15210 (if org-display-custom-times
15211 (message "Time stamps are overlayed with custom format")
15212 (message "Time stamp overlays removed")))
15214 (defun org-display-custom-time (beg end
)
15215 "Overlay modified time stamp format over timestamp between BED and END."
15216 (let* ((ts (buffer-substring beg end
))
15217 t1 w1 with-hm tf time str w2
(off 0))
15219 (setq t1
(org-parse-time-string ts t
))
15220 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( \\+[0-9]+[dwmy]\\)?\\'" ts
)
15221 (setq off
(- (match-end 0) (match-beginning 0)))))
15222 (setq end
(- end off
))
15223 (setq w1
(- end beg
)
15224 with-hm
(and (nth 1 t1
) (nth 2 t1
))
15225 tf
(funcall (if with-hm
'cdr
'car
) org-time-stamp-custom-formats
)
15226 time
(org-fix-decoded-time t1
)
15228 (format-time-string
15229 (substring tf
1 -
1) (apply 'encode-time time
))
15230 nil
'mouse-face
'highlight
)
15232 (if (not (= w2 w1
))
15233 (add-text-properties (1+ beg
) (+ 2 beg
)
15234 (list 'org-dwidth t
'org-dwidth-n
(- w1 w2
))))
15235 (if (featurep 'xemacs
)
15237 (put-text-property beg end
'invisible t
)
15238 (put-text-property beg end
'end-glyph
(make-glyph str
)))
15239 (put-text-property beg end
'display str
))))
15241 (defun org-translate-time (string)
15242 "Translate all timestamps in STRING to custom format.
15243 But do this only if the variable `org-display-custom-times' is set."
15244 (when org-display-custom-times
15247 (re org-ts-regexp-both
)
15248 t1 with-hm inactive tf time str beg end
)
15249 (while (setq start
(string-match re string start
))
15250 (setq beg
(match-beginning 0)
15252 t1
(save-match-data
15253 (org-parse-time-string (substring string beg end
) t
))
15254 with-hm
(and (nth 1 t1
) (nth 2 t1
))
15255 inactive
(equal (substring string beg
(1+ beg
)) "[")
15256 tf
(funcall (if with-hm
'cdr
'car
)
15257 org-time-stamp-custom-formats
)
15258 time
(org-fix-decoded-time t1
)
15259 str
(format-time-string
15261 (if inactive
"[" "<") (substring tf
1 -
1)
15262 (if inactive
"]" ">"))
15263 (apply 'encode-time time
))
15264 string
(replace-match str t t string
)
15265 start
(+ start
(length str
)))))))
15268 (defun org-fix-decoded-time (time)
15269 "Set 0 instead of nil for the first 6 elements of time.
15270 Don't touch the rest."
15272 (mapcar (lambda (x) (if (< (setq n
(1+ n
)) 7) (or x
0) x
)) time
)))
15274 (defun org-days-to-time (timestamp-string)
15275 "Difference between TIMESTAMP-STRING and now in days."
15276 (- (time-to-days (org-time-string-to-time timestamp-string
))
15277 (time-to-days (current-time))))
15279 (defun org-deadline-close (timestamp-string &optional ndays
)
15280 "Is the time in TIMESTAMP-STRING close to the current date?"
15281 (and (< (org-days-to-time timestamp-string
)
15282 (or ndays org-deadline-warning-days
))
15283 (not (org-entry-is-done-p))))
15285 (defun org-calendar-select-mouse (ev)
15286 "Return to `org-read-date' with the date currently selected.
15287 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
15289 (mouse-set-point ev
)
15290 (when (calendar-cursor-to-date)
15291 (let* ((date (calendar-cursor-to-date))
15292 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
15293 (setq org-ans1
(format-time-string "%Y-%m-%d" time
)))
15294 (if (active-minibuffer-window) (exit-minibuffer))))
15296 (defun org-check-deadlines (ndays)
15297 "Check if there are any deadlines due or past due.
15298 A deadline is considered due if it happens within `org-deadline-warning-days'
15299 days from today's date. If the deadline appears in an entry marked DONE,
15300 it is not shown. The prefix arg NDAYS can be used to test that many
15301 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
15303 (let* ((org-warn-days
15305 ((equal ndays
'(4)) 100000)
15306 (ndays (prefix-numeric-value ndays
))
15307 (t org-deadline-warning-days
)))
15308 (case-fold-search nil
)
15309 (regexp (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>"))
15311 (lambda () (org-deadline-close (match-string 1) org-warn-days
))))
15313 (message "%d deadlines past-due or due within %d days"
15314 (org-occur regexp nil callback
)
15317 (defun org-evaluate-time-range (&optional to-buffer
)
15318 "Evaluate a time range by computing the difference between start and end.
15319 Normally the result is just printed in the echo area, but with prefix arg
15320 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
15321 If the time range is actually in a table, the result is inserted into the
15323 For time difference computation, a year is assumed to be exactly 365
15324 days in order to avoid rounding problems."
15327 (org-clock-update-time-maybe)
15329 (unless (org-at-date-range-p)
15330 (goto-char (point-at-bol))
15331 (re-search-forward org-tr-regexp
(point-at-eol) t
))
15332 (if (not (org-at-date-range-p))
15333 (error "Not at a time-stamp range, and none found in current line")))
15334 (let* ((ts1 (match-string 1))
15335 (ts2 (match-string 2))
15336 (havetime (or (> (length ts1
) 15) (> (length ts2
) 15)))
15337 (match-end (match-end 0))
15338 (time1 (org-time-string-to-time ts1
))
15339 (time2 (org-time-string-to-time ts2
))
15340 (t1 (time-to-seconds time1
))
15341 (t2 (time-to-seconds time2
))
15342 (diff (abs (- t2 t1
)))
15343 (negative (< (- t2 t1
) 0))
15344 ;; (ys (floor (* 365 24 60 60)))
15347 (fy "%dy %dd %02d:%02d")
15349 (fd "%dd %02d:%02d")
15354 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
15356 d
(floor (/ diff ds
)) diff
(mod diff ds
)
15357 h
(floor (/ diff hs
)) diff
(mod diff hs
)
15358 m
(floor (/ diff
60)))
15359 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
15361 d
(floor (+ (/ diff ds
) 0.5))
15363 (if (not to-buffer
)
15364 (message (org-make-tdiff-string y d h m
))
15365 (when (org-at-table-p)
15366 (goto-char match-end
)
15368 (and (looking-at " *|") (goto-char (match-end 0))))
15370 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
15371 (replace-match ""))
15372 (if negative
(insert " -"))
15373 (if (> y
0) (insert " " (format (if havetime fy fy1
) y d h m
))
15374 (if (> d
0) (insert " " (format (if havetime fd fd1
) d h m
))
15375 (insert " " (format fh h m
))))
15376 (if align
(org-table-align))
15377 (message "Time difference inserted")))))
15379 (defun org-make-tdiff-string (y d h m
)
15382 (if (> y
0) (setq fmt
(concat fmt
"%d year" (if (> y
1) "s" "") " ")
15384 (if (> d
0) (setq fmt
(concat fmt
"%d day" (if (> d
1) "s" "") " ")
15386 (if (> h
0) (setq fmt
(concat fmt
"%d hour" (if (> h
1) "s" "") " ")
15388 (if (> m
0) (setq fmt
(concat fmt
"%d minute" (if (> m
1) "s" "") " ")
15390 (apply 'format fmt
(nreverse l
))))
15392 (defun org-time-string-to-time (s)
15393 (apply 'encode-time
(org-parse-time-string s
)))
15395 (defun org-time-string-to-absolute (s &optional daynr
)
15396 "Convert a time stamp to an absolute day number.
15397 If there is a specifyer for a cyclic time stamp, get the closest date to
15400 ((and daynr
(string-match "\\`%%\\((.*)\\)" s
))
15401 (if (org-diary-sexp-entry (match-string 1 s
) "" date
)
15404 ((and daynr
(string-match "\\+[0-9]+[dwmy]" s
))
15405 (org-closest-date s
(if (and (boundp 'daynr
) (integerp daynr
)) daynr
15406 (time-to-days (current-time))) (match-string 0 s
)))
15407 (t (time-to-days (apply 'encode-time
(org-parse-time-string s
))))))
15409 (defun org-calendar-holiday ()
15410 "List of holidays, for Diary display in Org-mode."
15411 (let ((hl (check-calendar-holidays date
)))
15412 (if hl
(mapconcat 'identity hl
"; "))))
15414 (defun org-diary-sexp-entry (sexp entry date
)
15415 "Process a SEXP diary ENTRY for DATE."
15416 (require 'diary-lib
)
15417 (let ((result (if calendar-debug-sexp
15418 (let ((stack-trace-on-error t
))
15419 (eval (car (read-from-string sexp
))))
15420 (condition-case nil
15421 (eval (car (read-from-string sexp
)))
15424 (message "Bad sexp at line %d in %s: %s"
15426 (buffer-file-name) sexp
)
15428 (cond ((stringp result
) result
)
15429 ((and (consp result
)
15430 (stringp (cdr result
))) (cdr result
))
15434 (defun org-diary-to-ical-string (frombuf)
15436 (let* ((tmpdir (if (featurep 'xemacs
)
15438 temporary-file-directory
))
15439 (tmpfile (make-temp-name
15440 (expand-file-name "orgics" tmpdir
)))
15443 (set-buffer frombuf
)
15444 (icalendar-export-region (point-min) (point-max) tmpfile
)
15445 (setq buf
(find-buffer-visiting tmpfile
))
15447 (goto-char (point-min))
15448 (if (re-search-forward "^BEGIN:VEVENT" nil t
)
15449 (setq b
(match-beginning 0)))
15450 (goto-char (point-max))
15451 (if (re-search-backward "^END:VEVENT" nil t
)
15452 (setq e
(match-end 0)))
15453 (setq rtn
(if (and b e
) (concat (buffer-substring b e
) "\n") "")))
15455 (kill-buffer frombuf
)
15456 (delete-file tmpfile
)
15459 (defun org-closest-date (start current change
)
15460 "Find the date closest to CURRENT that is consistent with START and CHANGE."
15461 ;; Make the proper lists from the dates
15463 (let ((a1 '(("d" . day
) ("w" . week
) ("m" . month
) ("y" . year
)))
15464 dn dw sday cday n1 n2
15465 d m y y1 y2 date1 date2 nmonths nm ny m2
)
15467 (setq start
(org-date-to-gregorian start
)
15468 current
(org-date-to-gregorian
15469 (if org-agenda-repeating-timestamp-show-all
15471 (time-to-days (current-time))))
15472 sday
(calendar-absolute-from-gregorian start
)
15473 cday
(calendar-absolute-from-gregorian current
))
15475 (if (<= cday sday
) (throw 'exit sday
))
15477 (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change
)
15478 (setq dn
(string-to-number (match-string 1 change
))
15479 dw
(cdr (assoc (match-string 2 change
) a1
)))
15480 (error "Invalid change specifyer: %s" change
))
15481 (if (eq dw
'week
) (setq dw
'day dn
(* 7 dn
)))
15484 (setq n1
(+ sday
(* dn
(floor (/ (- cday sday
) dn
))))
15487 (setq d
(nth 1 start
) m
(car start
) y1
(nth 2 start
) y2
(nth 2 current
))
15488 (setq y1
(+ (* (floor (/ (- y2 y1
) dn
)) dn
) y1
))
15489 (setq date1
(list m d y1
)
15490 n1
(calendar-absolute-from-gregorian date1
)
15491 date2
(list m d
(+ y1
(* (if (< n1 cday
) 1 -
1) dn
)))
15492 n2
(calendar-absolute-from-gregorian date2
)))
15494 ;; approx number of month between the tow dates
15495 (setq nmonths
(floor (/ (- cday sday
) 30.436875)))
15496 ;; How often does dn fit in there?
15497 (setq d
(nth 1 start
) m
(car start
) y
(nth 2 start
)
15498 nm
(* dn
(max 0 (1- (floor (/ nmonths dn
)))))
15500 ny
(floor (/ m
12))
15503 (while (> m
12) (setq m
(- m
12) y
(1+ y
)))
15504 (setq n1
(calendar-absolute-from-gregorian (list m d y
)))
15505 (setq m2
(+ m dn
) y2 y
)
15506 (if (> m2
12) (setq y2
(1+ y2
) m2
(- m2
12)))
15507 (setq n2
(calendar-absolute-from-gregorian (list m2 d y2
)))
15509 (setq n1 n2 m m2 y y2
)
15510 (setq m2
(+ m dn
) y2 y
)
15511 (if (> m2
12) (setq y2
(1+ y2
) m2
(- m2
12)))
15512 (setq n2
(calendar-absolute-from-gregorian (list m2 d y2
))))))
15514 (if org-agenda-repeating-timestamp-show-all
15515 (if (> (abs (- cday n1
)) (abs (- cday n2
))) n2 n1
)
15516 (if (= cday n1
) n1 n2
)))))
15518 (defun org-date-to-gregorian (date)
15519 "Turn any specification of DATE into a gregorian date for the calendar."
15520 (cond ((integerp date
) (calendar-gregorian-from-absolute date
))
15521 ((and (listp date
) (= (length date
) 3)) date
)
15523 (setq date
(org-parse-time-string date
))
15524 (list (nth 4 date
) (nth 3 date
) (nth 5 date
)))
15526 (list (nth 4 date
) (nth 3 date
) (nth 5 date
)))))
15528 (defun org-parse-time-string (s &optional nodefault
)
15529 "Parse the standard Org-mode time string.
15530 This should be a lot faster than the normal `parse-time-string'.
15531 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
15532 hour and minute fields will be nil if not given."
15533 (if (string-match org-ts-regexp0 s
)
15535 (if (or (match-beginning 8) (not nodefault
))
15536 (string-to-number (or (match-string 8 s
) "0")))
15537 (if (or (match-beginning 7) (not nodefault
))
15538 (string-to-number (or (match-string 7 s
) "0")))
15539 (string-to-number (match-string 4 s
))
15540 (string-to-number (match-string 3 s
))
15541 (string-to-number (match-string 2 s
))
15545 (defun org-timestamp-up (&optional arg
)
15546 "Increase the date item at the cursor by one.
15547 If the cursor is on the year, change the year. If it is on the month or
15548 the day, change that.
15549 With prefix ARG, change by that many units."
15551 (org-timestamp-change (prefix-numeric-value arg
)))
15553 (defun org-timestamp-down (&optional arg
)
15554 "Decrease the date item at the cursor by one.
15555 If the cursor is on the year, change the year. If it is on the month or
15556 the day, change that.
15557 With prefix ARG, change by that many units."
15559 (org-timestamp-change (- (prefix-numeric-value arg
))))
15561 (defun org-timestamp-up-day (&optional arg
)
15562 "Increase the date in the time stamp by one day.
15563 With prefix ARG, change that many days."
15565 (if (and (not (org-at-timestamp-p t
))
15566 (org-on-heading-p))
15568 (org-timestamp-change (prefix-numeric-value arg
) 'day
)))
15570 (defun org-timestamp-down-day (&optional arg
)
15571 "Decrease the date in the time stamp by one day.
15572 With prefix ARG, change that many days."
15574 (if (and (not (org-at-timestamp-p t
))
15575 (org-on-heading-p))
15577 (org-timestamp-change (- (prefix-numeric-value arg
)) 'day
)))
15579 (defsubst org-pos-in-match-range
(pos n
)
15580 (and (match-beginning n
)
15581 (<= (match-beginning n
) pos
)
15582 (>= (match-end n
) pos
)))
15584 (defun org-at-timestamp-p (&optional inactive-ok
)
15585 "Determine if the cursor is in or at a timestamp."
15587 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2
))
15589 (ans (or (looking-at tsr
)
15591 (skip-chars-backward "^[<\n\r\t")
15592 (if (> (point) 1) (backward-char 1))
15593 (and (looking-at tsr
)
15594 (> (- (match-end 0) pos
) -
1))))))
15595 (and (boundp 'org-ts-what
)
15598 ((org-pos-in-match-range pos
2) 'year
)
15599 ((org-pos-in-match-range pos
3) 'month
)
15600 ((org-pos-in-match-range pos
7) 'hour
)
15601 ((org-pos-in-match-range pos
8) 'minute
)
15602 ((or (org-pos-in-match-range pos
4)
15603 (org-pos-in-match-range pos
5)) 'day
)
15604 ((and (> pos
(or (match-end 8) (match-end 5)))
15605 (< pos
(match-end 0)))
15606 (- pos
(or (match-end 8) (match-end 5))))
15610 (defun org-timestamp-change (n &optional what
)
15611 "Change the date in the time stamp at point.
15612 The date will be changed by N times WHAT. WHAT can be `day', `month',
15613 `year', `minute', `second'. If WHAT is not given, the cursor position
15614 in the timestamp determines what will be changed."
15615 (let ((pos (point))
15620 (if (not (org-at-timestamp-p t
))
15621 (error "Not at a timestamp"))
15622 (if (and (not what
) (not (eq org-ts-what
'day
))
15623 org-display-custom-times
15624 (get-text-property (point) 'display
)
15625 (not (get-text-property (1- (point)) 'display
)))
15626 (setq org-ts-what
'day
))
15627 (setq org-ts-what
(or what org-ts-what
)
15628 inactive
(= (char-after (match-beginning 0)) ?\
[)
15629 ts
(match-string 0))
15632 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( [-+][0-9]+[dwmy]\\)*\\)[]>]"
15634 (setq extra
(match-string 1 ts
)))
15635 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts
)
15637 (setq time0
(org-parse-time-string ts
))
15639 (apply 'encode-time
15641 (list (or (car time0
) 0))
15642 (list (+ (if (eq org-ts-what
'minute
) n
0) (nth 1 time0
)))
15643 (list (+ (if (eq org-ts-what
'hour
) n
0) (nth 2 time0
)))
15644 (list (+ (if (eq org-ts-what
'day
) n
0) (nth 3 time0
)))
15645 (list (+ (if (eq org-ts-what
'month
) n
0) (nth 4 time0
)))
15646 (list (+ (if (eq org-ts-what
'year
) n
0) (nth 5 time0
)))
15647 (nthcdr 6 time0
))))
15648 (when (integerp org-ts-what
)
15649 (setq extra
(org-modify-ts-extra extra org-ts-what n
)))
15650 (if (eq what
'calendar
)
15654 (set-buffer "*Calendar*")
15655 (calendar-cursor-to-date)))))
15656 (setcar (nthcdr 4 time0
) (nth 0 cal-date
)) ; month
15657 (setcar (nthcdr 3 time0
) (nth 1 cal-date
)) ; day
15658 (setcar (nthcdr 5 time0
) (nth 2 cal-date
)) ; year
15659 (setcar time0
(or (car time0
) 0))
15660 (setcar (nthcdr 1 time0
) (or (nth 1 time0
) 0))
15661 (setcar (nthcdr 2 time0
) (or (nth 1 time0
) 0))
15662 (setq time
(apply 'encode-time time0
))))
15663 (setq org-last-changed-timestamp
15664 (org-insert-time-stamp time with-hm inactive nil nil extra
))
15665 (org-clock-update-time-maybe)
15667 ;; Try to recenter the calendar window, if any
15668 (if (and org-calendar-follow-timestamp-change
15669 (get-buffer-window "*Calendar*" t
)
15670 (memq org-ts-what
'(day month year
)))
15671 (org-recenter-calendar (time-to-days time
)))))
15673 (defun org-modify-ts-extra (s pos n
)
15675 (let ((idx '(("d" .
0) ("w" .
1) ("m" .
2) ("y" .
3) ("d" . -
1) ("y" .
4)))
15677 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( \\+\\([0-9]+\\)\\([dmwy]\\)\\)?" s
)
15679 ((or (org-pos-in-match-range pos
2)
15680 (org-pos-in-match-range pos
3))
15681 (setq m
(string-to-number (match-string 3 s
))
15682 h
(string-to-number (match-string 2 s
)))
15683 (if (org-pos-in-match-range pos
2)
15686 (if (< m
0) (setq m
(+ m
60) h
(1- h
)))
15687 (if (> m
59) (setq m
(- m
60) h
(1+ h
)))
15688 (setq h
(min 24 (max 0 h
)))
15689 (setq ng
1 new
(format "-%02d:%02d" h m
)))
15690 ((org-pos-in-match-range pos
6)
15691 (setq ng
6 new
(car (rassoc (+ n
(cdr (assoc (match-string 6 s
) idx
))) idx
))))
15692 ((org-pos-in-match-range pos
5)
15693 (setq ng
5 new
(format "%d" (max 1 (+ n
(string-to-number (match-string 5 s
))))))))
15697 (substring s
0 (match-beginning ng
))
15699 (substring s
(match-end ng
))))))
15702 (defun org-recenter-calendar (date)
15703 "If the calendar is visible, recenter it to DATE."
15704 (let* ((win (selected-window))
15705 (cwin (get-buffer-window "*Calendar*" t
))
15706 (calendar-move-hook nil
))
15708 (select-window cwin
)
15709 (calendar-goto-date (if (listp date
) date
15710 (calendar-gregorian-from-absolute date
)))
15711 (select-window win
))))
15713 (defun org-goto-calendar (&optional arg
)
15714 "Go to the Emacs calendar at the current date.
15715 If there is a time stamp in the current line, go to that date.
15716 A prefix ARG can be used to force the current date."
15718 (let ((tsr org-ts-regexp
) diff
15719 (calendar-move-hook nil
)
15720 (view-calendar-holidays-initially nil
)
15721 (view-diary-entries-initially nil
))
15722 (if (or (org-at-timestamp-p)
15724 (beginning-of-line 1)
15725 (looking-at (concat ".*" tsr
))))
15726 (let ((d1 (time-to-days (current-time)))
15728 (org-time-string-to-time (match-string 1)))))
15729 (setq diff
(- d2 d1
))))
15731 (calendar-goto-today)
15732 (if (and diff
(not arg
)) (calendar-forward-day diff
))))
15734 (defun org-date-from-calendar ()
15735 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
15736 If there is already a time stamp at the cursor position, update it."
15738 (org-timestamp-change 0 'calendar
))
15740 ;;; The clock for measuring work time.
15742 (defvar org-mode-line-string
"")
15743 (put 'org-mode-line-string
'risky-local-variable t
)
15745 (defvar org-mode-line-timer nil
)
15746 (defvar org-clock-heading
"")
15747 (defvar org-clock-start-time
"")
15749 (defun org-update-mode-line ()
15750 (let* ((delta (- (time-to-seconds (current-time))
15751 (time-to-seconds org-clock-start-time
)))
15752 (h (floor delta
3600))
15753 (m (floor (- delta
(* 3600 h
)) 60)))
15754 (setq org-mode-line-string
15755 (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading
)
15756 'help-echo
"Org-mode clock is running"))
15757 (force-mode-line-update)))
15759 (defvar org-clock-marker
(make-marker)
15760 "Marker recording the last clock-in.")
15761 (defvar org-clock-mode-line-entry nil
15762 "Information for the modeline about the running clock.")
15764 (defun org-clock-in ()
15765 "Start the clock on the current item.
15766 If necessary, clock-out of the currently active clock."
15771 (org-back-to-heading t
)
15772 (if (looking-at org-todo-line-regexp
)
15773 (setq org-clock-heading
(match-string 3))
15774 (setq org-clock-heading
"???"))
15775 (setq org-clock-heading
(propertize org-clock-heading
'face nil
))
15776 (beginning-of-line 2)
15778 (or (and (looking-at (concat "[ \t]*" org-keyword-time-regexp
))
15779 (not (equal (match-string 1) org-clock-string
)))
15780 (and (looking-at "[ \t]*:PROPERTIES:")
15781 (not org-insert-labeled-timestamps-before-properties-drawer
)))
15782 ;; Scheduling info, or properties drawer, move one line further
15783 (beginning-of-line 2)
15784 (or (bolp) (newline)))
15785 (insert "\n") (backward-char 1)
15787 (insert org-clock-string
" ")
15788 (setq org-clock-start-time
(current-time))
15789 (setq ts
(org-insert-time-stamp (current-time) 'with-hm
'inactive
))
15790 (move-marker org-clock-marker
(point) (buffer-base-buffer))
15791 (or global-mode-string
(setq global-mode-string
'("")))
15792 (or (memq 'org-mode-line-string global-mode-string
)
15793 (setq global-mode-string
15794 (append global-mode-string
'(org-mode-line-string))))
15795 (org-update-mode-line)
15796 (setq org-mode-line-timer
(run-with-timer 60 60 'org-update-mode-line
))
15797 (message "Clock started at %s" ts
))))
15799 (defun org-clock-out (&optional fail-quietly
)
15800 "Stop the currently running clock.
15801 If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
15804 (if (not (marker-buffer org-clock-marker
))
15805 (if fail-quietly
(throw 'exit t
) (error "No active clock")))
15808 (set-buffer (marker-buffer org-clock-marker
))
15809 (goto-char org-clock-marker
)
15810 (beginning-of-line 1)
15811 (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp
))
15812 (equal (match-string 1) org-clock-string
))
15813 (setq ts
(match-string 2))
15814 (if fail-quietly
(throw 'exit nil
) (error "Clock start time is gone")))
15815 (goto-char (match-end 0))
15816 (delete-region (point) (point-at-eol))
15818 (setq te
(org-insert-time-stamp (current-time) 'with-hm
'inactive
))
15819 (setq s
(- (time-to-seconds (apply 'encode-time
(org-parse-time-string te
)))
15820 (time-to-seconds (apply 'encode-time
(org-parse-time-string ts
))))
15821 h
(floor (/ s
3600))
15825 (insert " => " (format "%2d:%02d" h m
))
15826 (move-marker org-clock-marker nil
)
15827 (org-add-log-maybe 'clock-out
)
15828 (when org-mode-line-timer
15829 (cancel-timer org-mode-line-timer
)
15830 (setq org-mode-line-timer nil
))
15831 (setq global-mode-string
15832 (delq 'org-mode-line-string global-mode-string
))
15833 (force-mode-line-update)
15834 (message "Clock stopped at %s after HH:MM = %d:%02d" te h m
)))))
15836 (defun org-clock-cancel ()
15837 "Cancel the running clock be removing the start timestamp."
15839 (if (not (marker-buffer org-clock-marker
))
15840 (error "No active clock"))
15842 (set-buffer (marker-buffer org-clock-marker
))
15843 (goto-char org-clock-marker
)
15844 (delete-region (1- (point-at-bol)) (point-at-eol)))
15845 (message "Clock canceled"))
15847 (defvar org-clock-file-total-minutes nil
15848 "Holds the file total time in minutes, after a call to `org-clock-sum'.")
15849 (make-variable-buffer-local 'org-clock-file-total-minutes
)
15851 (defun org-clock-sum (&optional tstart tend
)
15852 "Sum the times for each subtree.
15853 Puts the resulting times in minutes as a text property on each headline."
15855 (let* ((bmp (buffer-modified-p))
15856 (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
15858 "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
15860 (ltimes (make-vector lmax
0))
15865 (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t
))
15867 (goto-char (point-max))
15868 (while (re-search-backward re nil t
)
15872 (setq ts
(match-string 2)
15873 te
(match-string 3)
15874 ts
(time-to-seconds
15875 (apply 'encode-time
(org-parse-time-string ts
)))
15876 te
(time-to-seconds
15877 (apply 'encode-time
(org-parse-time-string te
)))
15878 ts
(if tstart
(max ts tstart
) ts
)
15879 te
(if tend
(min te tend
) te
)
15881 t1
(if (> dt
0) (+ t1
(floor (/ dt
60))) t1
)))
15884 (setq t1
(+ t1
(string-to-number (match-string 5))
15885 (* 60 (string-to-number (match-string 4))))))
15887 (setq level
(- (match-end 1) (match-beginning 1)))
15888 (when (or (> t1
0) (> (aref ltimes level
) 0))
15889 (loop for l from
0 to level do
15890 (aset ltimes l
(+ (aref ltimes l
) t1
)))
15891 (setq t1
0 time
(aref ltimes level
))
15892 (loop for l from level to
(1- lmax
) do
15894 (goto-char (match-beginning 0))
15895 (put-text-property (point) (point-at-eol) :org-clock-minutes time
)))))
15896 (setq org-clock-file-total-minutes
(aref ltimes
0)))
15897 (set-buffer-modified-p bmp
)))
15899 (defun org-clock-display (&optional total-only
)
15900 "Show subtree times in the entire buffer.
15901 If TOTAL-ONLY is non-nil, only show the total time for the entire file
15904 (org-remove-clock-overlays)
15909 (goto-char (point-min))
15910 (while (setq p
(next-single-property-change (point) :org-clock-minutes
))
15912 (when (setq time
(get-text-property p
:org-clock-minutes
))
15913 (org-put-clock-overlay time
(funcall outline-level
))))
15914 (setq h
(/ org-clock-file-total-minutes
60)
15915 m
(- org-clock-file-total-minutes
(* 60 h
)))
15916 ;; Arrange to remove the overlays upon next change.
15917 (when org-remove-highlights-with-change
15918 (org-add-hook 'before-change-functions
'org-remove-clock-overlays
15920 (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m
)))
15922 (defvar org-clock-overlays nil
)
15923 (make-variable-buffer-local 'org-clock-overlays
)
15925 (defun org-put-clock-overlay (time &optional level
)
15926 "Put an overlays on the current line, displaying TIME.
15927 If LEVEL is given, prefix time with a corresponding number of stars.
15928 This creates a new overlay and stores it in `org-clock-overlays', so that it
15929 will be easy to remove."
15930 (let* ((c 60) (h (floor (/ time
60))) (m (- time
(* 60 h
)))
15931 (l (if level
(org-get-legal-level level
0) 0))
15935 (unless (eolp) (skip-chars-backward "^ \t"))
15936 (skip-chars-backward " \t")
15937 (setq ov
(org-make-overlay (1- (point)) (point-at-eol))
15938 tx
(concat (buffer-substring (1- (point)) (point))
15939 (make-string (+ off
(max 0 (- c
(current-column)))) ?.
)
15940 (org-add-props (format "%s %2d:%02d%s"
15941 (make-string l ?
*) h m
15942 (make-string (- 10 l
) ?\
))
15943 '(face secondary-selection
))
15945 (if (not (featurep 'xemacs
))
15946 (org-overlay-put ov
'display tx
)
15947 (org-overlay-put ov
'invisible t
)
15948 (org-overlay-put ov
'end-glyph
(make-glyph tx
)))
15949 (push ov org-clock-overlays
)))
15951 (defun org-remove-clock-overlays (&optional beg end noremove
)
15952 "Remove the occur highlights from the buffer.
15953 BEG and END are ignored. If NOREMOVE is nil, remove this function
15954 from the `before-change-functions' in the current buffer."
15956 (unless org-inhibit-highlight-removal
15957 (mapc 'org-delete-overlay org-clock-overlays
)
15958 (setq org-clock-overlays nil
)
15960 (remove-hook 'before-change-functions
15961 'org-remove-clock-overlays
'local
))))
15963 (defun org-clock-out-if-current ()
15964 "Clock out if the current entry contains the running clock.
15965 This is used to stop the clock after a TODO entry is marked DONE,
15966 and is only done if the variable `org-clock-out-when-done' is not nil."
15967 (when (and org-clock-out-when-done
15968 (member state org-done-keywords
)
15969 (equal (marker-buffer org-clock-marker
) (current-buffer))
15970 (< (point) org-clock-marker
)
15971 (> (save-excursion (outline-next-heading) (point))
15973 ;; Clock out, but don't accept a logging message for this.
15974 (let ((org-log-done (if (and (listp org-log-done
)
15975 (member 'clock-out org-log-done
))
15980 (add-hook 'org-after-todo-state-change-hook
15981 'org-clock-out-if-current
)
15983 (defun org-check-running-clock ()
15984 "Check if the current buffer contains the running clock.
15985 If yes, offer to stop it and to save the buffer with the changes."
15986 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
15987 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
15990 (when (y-or-n-p "Save changed buffer?")
15993 (defun org-clock-report ()
15994 "Create a table containing a report about clocked time.
15995 If the buffer contains lines
15996 #+BEGIN: clocktable :maxlevel 3 :emphasize nil
15999 then the table will be inserted between these lines, replacing whatever
16000 is was there before. If these lines are not in the buffer, the table
16001 is inserted at point, surrounded by the special lines.
16002 The BEGIN line can contain parameters. Allowed are:
16003 :maxlevel The maximum level to be included in the table. Default is 3.
16004 :emphasize t/nil, if levell 1 and level 2 should be bold/italic in the table."
16006 (org-remove-clock-overlays)
16007 (unless (org-find-dblock "clocktable")
16008 (org-create-dblock (list :name
"clocktable"
16009 :maxlevel
2 :emphasize nil
)))
16010 (org-update-dblock))
16012 (defun org-clock-update-time-maybe ()
16013 "If this is a CLOCK line, update it and return t.
16014 Otherwise, return nil."
16017 (beginning-of-line 1)
16018 (skip-chars-forward " \t")
16019 (when (looking-at org-clock-string
)
16020 (let ((re (concat "[ \t]*" org-clock-string
16021 " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
16022 "\\([ \t]*=>.*\\)?"))
16024 (if (not (looking-at re
))
16026 (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
16028 (setq ts
(match-string 1)
16029 te
(match-string 2))
16030 (setq s
(- (time-to-seconds
16031 (apply 'encode-time
(org-parse-time-string te
)))
16033 (apply 'encode-time
(org-parse-time-string ts
))))
16034 h
(floor (/ s
3600))
16038 (insert " => " (format "%2d:%02d" h m
))
16041 (defun org-clock-special-range (key &optional time as-strings
)
16042 "Return two times bordering a special time range.
16043 Key is a symbol specifying the range and can be one of `today', `yesterday',
16044 `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
16045 A week starts Monday 0:00 and ends Sunday 24:00.
16046 The range is determined relative to TIME. TIME defaults to the current time.
16047 The return value is a cons cell with two internal times like the ones
16048 returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
16049 the returned times will be formatted strings."
16050 (let* ((tm (decode-time (or time
(current-time))))
16051 (s 0) (m (nth 1 tm
)) (h (nth 2 tm
))
16052 (d (nth 3 tm
)) (month (nth 4 tm
)) (y (nth 5 tm
))
16054 s1 m1 h1 d1 month1 y1 diff ts te fm
)
16057 (setq h
0 m
0 h1
24 m1
0))
16058 ((eq key
'yesterday
)
16059 (setq d
(1- d
) h
0 m
0 h1
24 m1
0))
16060 ((eq key
'thisweek
)
16061 (setq diff
(if (= dow
0) 6 (1- dow
))
16062 m
0 h
0 d
(- d diff
) d1
(+ 7 d
)))
16063 ((eq key
'lastweek
)
16064 (setq diff
(+ 7 (if (= dow
0) 6 (1- dow
)))
16065 m
0 h
0 d
(- d diff
) d1
(+ 7 d
)))
16066 ((eq key
'thismonth
)
16067 (setq d
1 h
0 m
0 d1
1 month1
(1+ month
) h1
0 m1
0))
16068 ((eq key
'lastmonth
)
16069 (setq d
1 h
0 m
0 d1
1 month
(1- month
) month1
(1+ month
) h1
0 m1
0))
16070 ((eq key
'thisyear
)
16071 (setq m
0 h
0 d
1 month
1 y1
(1+ y
)))
16072 ((eq key
'lastyear
)
16073 (setq m
0 h
0 d
1 month
1 y
(1- y
) y1
(1+ y
)))
16074 (t (error "No such time block %s" key
)))
16075 (setq ts
(encode-time s m h d month y
)
16076 te
(encode-time (or s1 s
) (or m1 m
) (or h1 h
)
16077 (or d1 d
) (or month1 month
) (or y1 y
)))
16078 (setq fm
(cdr org-time-stamp-formats
))
16080 (cons (format-time-string fm ts
) (format-time-string fm te
))
16083 (defun org-dblock-write:clocktable
(params)
16084 "Write the standard clocktable."
16085 (let ((hlchars '((1 .
"*") (2 . ?
/)))
16087 (ins (make-marker))
16088 ipos time h m p level hlc hdl maxlevel
16090 (setq maxlevel
(or (plist-get params
:maxlevel
) 3)
16091 emph
(plist-get params
:emphasize
)
16092 ts
(plist-get params
:tstart
)
16093 te
(plist-get params
:tend
)
16094 block
(plist-get params
:block
))
16096 (setq cc
(org-clock-special-range block nil t
)
16097 ts
(car cc
) te
(cdr cc
)))
16098 (if ts
(setq ts
(time-to-seconds
16099 (apply 'encode-time
(org-parse-time-string ts
)))))
16100 (if te
(setq te
(time-to-seconds
16101 (apply 'encode-time
(org-parse-time-string te
)))))
16102 (move-marker ins
(point))
16103 (setq ipos
(point))
16104 (insert-before-markers "Clock summary at ["
16106 (format-time-string (cdr org-time-stamp-formats
))
16110 (format " Considered range is /%s/." block
)
16112 "\n\n|L|Headline|Time|\n")
16113 (org-clock-sum ts te
)
16114 (setq h
(/ org-clock-file-total-minutes
60)
16115 m
(- org-clock-file-total-minutes
(* 60 h
)))
16116 (insert-before-markers "|-\n|0|" "*Total file time*| "
16117 (format "*%d:%02d*" h m
)
16119 (goto-char (point-min))
16120 (while (setq p
(next-single-property-change (point) :org-clock-minutes
))
16122 (when (setq time
(get-text-property p
:org-clock-minutes
))
16124 (beginning-of-line 1)
16125 (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
16126 (setq level
(- (match-end 1) (match-beginning 1)))
16127 (<= level maxlevel
))
16128 (setq hlc
(if emph
(or (cdr (assoc level hlchars
)) "") "")
16129 hdl
(match-string 2)
16131 m
(- time
(* 60 h
)))
16133 (if (= level
1) (insert-before-markers "|-\n"))
16134 (insert-before-markers
16135 "| " (int-to-string level
) "|" hlc hdl hlc
" |"
16136 (make-string (1- level
) ?|
)
16138 (format "%d:%02d" h m
)
16142 (backward-delete-char 1)
16144 (skip-chars-forward "^|")
16145 (org-table-align)))
16147 ;; FIXME: I don't think anybody uses this, ask David
16148 (defun org-collect-clock-time-entries ()
16149 "Return an internal list with clocking information.
16150 This list has one entry for each CLOCK interval.
16151 FIXME: describe the elements."
16153 (let ((re (concat "^[ \t]*" org-clock-string
16154 " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
16155 rtn beg end next cont level title total closedp leafp
16156 clockpos titlepos h m donep
)
16159 (goto-char (point-min))
16160 (while (re-search-forward re nil t
)
16161 (setq clockpos
(match-beginning 0)
16162 beg
(match-string 1) end
(match-string 2)
16163 cont
(match-end 0))
16164 (setq beg
(apply 'encode-time
(org-parse-time-string beg
))
16165 end
(apply 'encode-time
(org-parse-time-string end
)))
16166 (org-back-to-heading t
)
16167 (setq donep
(org-entry-is-done-p))
16168 (setq titlepos
(point)
16169 total
(or (get-text-property (1+ (point)) :org-clock-minutes
) 0)
16170 h
(/ total
60) m
(- total
(* 60 h
))
16172 (looking-at "\\(\\*+\\) +\\(.*\\)")
16173 (setq level
(- (match-end 1) (match-beginning 1))
16174 title
(org-match-string-no-properties 2))
16175 (save-excursion (outline-next-heading) (setq next
(point)))
16176 (setq closedp
(re-search-forward org-closed-time-regexp next t
))
16178 (setq leafp
(and (looking-at "^\\*+ ")
16179 (<= (- (match-end 0) (point)) level
)))
16180 (push (list beg end clockpos closedp donep
16181 total title titlepos level leafp
)
16186 ;;;; Agenda, and Diary Integration
16188 ;;; Define the Org-agenda-mode
16190 (defvar org-agenda-mode-map
(make-sparse-keymap)
16191 "Keymap for `org-agenda-mode'.")
16193 (defvar org-agenda-menu
) ; defined later in this file.
16194 (defvar org-agenda-follow-mode nil
)
16195 (defvar org-agenda-show-log nil
)
16196 (defvar org-agenda-redo-command nil
)
16197 (defvar org-agenda-mode-hook nil
)
16198 (defvar org-agenda-type nil
)
16199 (defvar org-agenda-force-single-file nil
)
16201 (defun org-agenda-mode ()
16202 "Mode for time-sorted view on action items in Org-mode files.
16204 The following commands are available:
16206 \\{org-agenda-mode-map}"
16208 (kill-all-local-variables)
16209 (setq org-agenda-undo-list nil
16210 org-agenda-pending-undo-list nil
)
16211 (setq major-mode
'org-agenda-mode
)
16212 ;; Keep global-font-lock-mode from turning on font-lock-mode
16213 (org-set-local 'font-lock-global-modes
(list 'not major-mode
))
16214 (setq mode-name
"Org-Agenda")
16215 (use-local-map org-agenda-mode-map
)
16216 (easy-menu-add org-agenda-menu
)
16217 (if org-startup-truncated
(setq truncate-lines t
))
16218 (org-add-hook 'post-command-hook
'org-agenda-post-command-hook nil
'local
)
16219 (org-add-hook 'pre-command-hook
'org-unhighlight nil
'local
)
16220 ;; Make sure properties are removed when copying text
16221 (when (boundp 'buffer-substring-filters
)
16222 (org-set-local 'buffer-substring-filters
16224 (set-text-properties 0 (length x
) nil x
) x
)
16225 buffer-substring-filters
)))
16226 (unless org-agenda-keep-modes
16227 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
16228 org-agenda-show-log nil
))
16230 '("Agenda") "Agenda Files"
16234 (if (get 'org-agenda-files
'org-restrict
)
16235 "Restricted to single file"
16237 '(org-edit-agenda-file-list)
16238 (not (get 'org-agenda-files
'org-restrict
)))
16240 (mapcar 'org-file-menu-entry
(org-agenda-files))))
16241 (org-agenda-set-mode-name)
16243 (if (fboundp 'run-mode-hooks
) 'run-mode-hooks
'run-hooks
)
16244 (list 'org-agenda-mode-hook
)))
16246 (substitute-key-definition 'undo
'org-agenda-undo
16247 org-agenda-mode-map global-map
)
16248 (org-defkey org-agenda-mode-map
"\C-i" 'org-agenda-goto
)
16249 (org-defkey org-agenda-mode-map
[(tab)] 'org-agenda-goto
)
16250 (org-defkey org-agenda-mode-map
"\C-m" 'org-agenda-switch-to
)
16251 (org-defkey org-agenda-mode-map
"\C-k" 'org-agenda-kill
)
16252 (org-defkey org-agenda-mode-map
"\C-c$" 'org-agenda-archive
)
16253 (org-defkey org-agenda-mode-map
"\C-c\C-x\C-s" 'org-agenda-archive
)
16254 (org-defkey org-agenda-mode-map
"$" 'org-agenda-archive
)
16255 (org-defkey org-agenda-mode-map
"\C-c\C-o" 'org-agenda-open-link
)
16256 (org-defkey org-agenda-mode-map
" " 'org-agenda-show
)
16257 (org-defkey org-agenda-mode-map
"\C-c\C-t" 'org-agenda-todo
)
16258 (org-defkey org-agenda-mode-map
[(control shift right
)] 'org-agenda-todo-nextset
)
16259 (org-defkey org-agenda-mode-map
[(control shift left
)] 'org-agenda-todo-previousset
)
16260 (org-defkey org-agenda-mode-map
"\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer
)
16261 (org-defkey org-agenda-mode-map
"b" 'org-agenda-tree-to-indirect-buffer
)
16262 (org-defkey org-agenda-mode-map
"o" 'delete-other-windows
)
16263 (org-defkey org-agenda-mode-map
"L" 'org-agenda-recenter
)
16264 (org-defkey org-agenda-mode-map
"t" 'org-agenda-todo
)
16265 (org-defkey org-agenda-mode-map
"a" 'org-agenda-toggle-archive-tag
)
16266 (org-defkey org-agenda-mode-map
":" 'org-agenda-set-tags
)
16267 (org-defkey org-agenda-mode-map
"." 'org-agenda-goto-today
)
16268 (org-defkey org-agenda-mode-map
"j" 'org-agenda-goto-date
)
16269 (org-defkey org-agenda-mode-map
"d" 'org-agenda-day-view
)
16270 (org-defkey org-agenda-mode-map
"w" 'org-agenda-week-view
)
16271 (org-defkey org-agenda-mode-map
"m" 'org-agenda-month-view
)
16272 (org-defkey org-agenda-mode-map
"y" 'org-agenda-year-view
)
16273 (org-defkey org-agenda-mode-map
[(shift right
)] 'org-agenda-date-later
)
16274 (org-defkey org-agenda-mode-map
[(shift left
)] 'org-agenda-date-earlier
)
16275 (org-defkey org-agenda-mode-map
[?\C-c ?\C-x
(right)] 'org-agenda-date-later
)
16276 (org-defkey org-agenda-mode-map
[?\C-c ?\C-x
(left)] 'org-agenda-date-earlier
)
16278 (org-defkey org-agenda-mode-map
">" 'org-agenda-date-prompt
)
16279 (org-defkey org-agenda-mode-map
"\C-c\C-s" 'org-agenda-schedule
)
16280 (org-defkey org-agenda-mode-map
"\C-c\C-d" 'org-agenda-deadline
)
16281 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
16282 (while l
(org-defkey org-agenda-mode-map
16283 (int-to-string (pop l
)) 'digit-argument
)))
16285 (org-defkey org-agenda-mode-map
"f" 'org-agenda-follow-mode
)
16286 (org-defkey org-agenda-mode-map
"l" 'org-agenda-log-mode
)
16287 (org-defkey org-agenda-mode-map
"D" 'org-agenda-toggle-diary
)
16288 (org-defkey org-agenda-mode-map
"g" 'org-agenda-toggle-time-grid
)
16289 (org-defkey org-agenda-mode-map
"r" 'org-agenda-redo
)
16290 (org-defkey org-agenda-mode-map
"q" 'org-agenda-quit
)
16291 (org-defkey org-agenda-mode-map
"x" 'org-agenda-exit
)
16292 (org-defkey org-agenda-mode-map
"\C-x\C-w" 'org-write-agenda
)
16293 (org-defkey org-agenda-mode-map
"s" 'org-save-all-org-buffers
)
16294 (org-defkey org-agenda-mode-map
"P" 'org-agenda-show-priority
)
16295 (org-defkey org-agenda-mode-map
"T" 'org-agenda-show-tags
)
16296 (org-defkey org-agenda-mode-map
"n" 'next-line
)
16297 (org-defkey org-agenda-mode-map
"p" 'previous-line
)
16298 (org-defkey org-agenda-mode-map
"\C-n" 'org-agenda-next-date-line
)
16299 (org-defkey org-agenda-mode-map
"\C-p" 'org-agenda-previous-date-line
)
16300 (org-defkey org-agenda-mode-map
"," 'org-agenda-priority
)
16301 (org-defkey org-agenda-mode-map
"\C-c," 'org-agenda-priority
)
16302 (org-defkey org-agenda-mode-map
"i" 'org-agenda-diary-entry
)
16303 (org-defkey org-agenda-mode-map
"c" 'org-agenda-goto-calendar
)
16304 (eval-after-load "calendar"
16305 '(org-defkey calendar-mode-map org-calendar-to-agenda-key
16306 'org-calendar-goto-agenda
))
16307 (org-defkey org-agenda-mode-map
"C" 'org-agenda-convert-date
)
16308 (org-defkey org-agenda-mode-map
"M" 'org-agenda-phases-of-moon
)
16309 (org-defkey org-agenda-mode-map
"S" 'org-agenda-sunrise-sunset
)
16310 (org-defkey org-agenda-mode-map
"h" 'org-agenda-holidays
)
16311 (org-defkey org-agenda-mode-map
"H" 'org-agenda-holidays
)
16312 (org-defkey org-agenda-mode-map
"I" 'org-agenda-clock-in
)
16313 (org-defkey org-agenda-mode-map
"O" 'org-agenda-clock-out
)
16314 (org-defkey org-agenda-mode-map
"X" 'org-agenda-clock-cancel
)
16315 (org-defkey org-agenda-mode-map
"+" 'org-agenda-priority-up
)
16316 (org-defkey org-agenda-mode-map
"-" 'org-agenda-priority-down
)
16317 (org-defkey org-agenda-mode-map
[(shift up
)] 'org-agenda-priority-up
)
16318 (org-defkey org-agenda-mode-map
[(shift down
)] 'org-agenda-priority-down
)
16319 (org-defkey org-agenda-mode-map
[?\C-c ?\C-x
(up)] 'org-agenda-priority-up
)
16320 (org-defkey org-agenda-mode-map
[?\C-c ?\C-x
(down)] 'org-agenda-priority-down
)
16321 (org-defkey org-agenda-mode-map
[(right)] 'org-agenda-later
)
16322 (org-defkey org-agenda-mode-map
[(left)] 'org-agenda-earlier
)
16323 (org-defkey org-agenda-mode-map
"\C-c\C-x\C-c" 'org-agenda-columns
)
16325 (defvar org-agenda-keymap
(copy-keymap org-agenda-mode-map
)
16326 "Local keymap for agenda entries from Org-mode.")
16328 (org-defkey org-agenda-keymap
16329 (if (featurep 'xemacs
) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse
)
16330 (org-defkey org-agenda-keymap
16331 (if (featurep 'xemacs
) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse
)
16332 (when org-agenda-mouse-1-follows-link
16333 (org-defkey org-agenda-keymap
[follow-link
] 'mouse-face
))
16334 (easy-menu-define org-agenda-menu org-agenda-mode-map
"Agenda menu"
16338 ["Show" org-agenda-show t
]
16339 ["Go To (other window)" org-agenda-goto t
]
16340 ["Go To (this window)" org-agenda-switch-to t
]
16341 ["Follow Mode" org-agenda-follow-mode
16342 :style toggle
:selected org-agenda-follow-mode
:active t
]
16343 ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t
]
16345 ["Cycle TODO" org-agenda-todo t
]
16346 ["Archive subtree" org-agenda-archive t
]
16347 ["Delete subtree" org-agenda-kill t
]
16349 ["Goto Today" org-agenda-goto-today
(org-agenda-check-type nil
'agenda
'timeline
)]
16350 ["Next Dates" org-agenda-later
(org-agenda-check-type nil
'agenda
)]
16351 ["Previous Dates" org-agenda-earlier
(org-agenda-check-type nil
'agenda
)]
16352 ["Jump to date" org-agenda-goto-date
(org-agenda-check-type nil
'agenda
)]
16354 ("Tags and Properties"
16355 ["Show all Tags" org-agenda-show-tags t
]
16356 ["Set Tags current line" org-agenda-set-tags
(not (org-region-active-p))]
16357 ["Change tag in region" org-agenda-set-tags
(org-region-active-p)]
16359 ["Column View" org-columns t
])
16361 ["Schedule" org-agenda-schedule t
]
16362 ["Set Deadline" org-agenda-deadline t
]
16364 ["Change Date +1 day" org-agenda-date-later
(org-agenda-check-type nil
'agenda
'timeline
)]
16365 ["Change Date -1 day" org-agenda-date-earlier
(org-agenda-check-type nil
'agenda
'timeline
)]
16366 ["Change Date to ..." org-agenda-date-prompt
(org-agenda-check-type nil
'agenda
'timeline
)])
16368 ["Set Priority" org-agenda-priority t
]
16369 ["Increase Priority" org-agenda-priority-up t
]
16370 ["Decrease Priority" org-agenda-priority-down t
]
16371 ["Show Priority" org-agenda-show-priority t
])
16373 ["New Diary Entry" org-agenda-diary-entry
(org-agenda-check-type nil
'agenda
'timeline
)]
16374 ["Goto Calendar" org-agenda-goto-calendar
(org-agenda-check-type nil
'agenda
'timeline
)]
16375 ["Phases of the Moon" org-agenda-phases-of-moon
(org-agenda-check-type nil
'agenda
'timeline
)]
16376 ["Sunrise/Sunset" org-agenda-sunrise-sunset
(org-agenda-check-type nil
'agenda
'timeline
)]
16377 ["Holidays" org-agenda-holidays
(org-agenda-check-type nil
'agenda
'timeline
)]
16378 ["Convert" org-agenda-convert-date
(org-agenda-check-type nil
'agenda
'timeline
)]
16380 ["Create iCalendar file" org-export-icalendar-combine-agenda-files t
])
16383 ["Day View" org-agenda-day-view
:active
(org-agenda-check-type nil
'agenda
)
16384 :style radio
:selected
(equal org-agenda-ndays
1)]
16385 ["Week View" org-agenda-week-view
:active
(org-agenda-check-type nil
'agenda
)
16386 :style radio
:selected
(equal org-agenda-ndays
7)]
16387 ["Month View" org-agenda-month-view
:active
(org-agenda-check-type nil
'agenda
)
16388 :style radio
:selected
(member org-agenda-ndays
'(28 29 30 31))]
16389 ["Year View" org-agenda-year-view
:active
(org-agenda-check-type nil
'agenda
)
16390 :style radio
:selected
(member org-agenda-ndays
'(365 366))]
16392 ["Show Logbook entries" org-agenda-log-mode
16393 :style toggle
:selected org-agenda-show-log
:active
(org-agenda-check-type nil
'agenda
'timeline
)]
16394 ["Include Diary" org-agenda-toggle-diary
16395 :style toggle
:selected org-agenda-include-diary
:active
(org-agenda-check-type nil
'agenda
)]
16396 ["Use Time Grid" org-agenda-toggle-time-grid
16397 :style toggle
:selected org-agenda-use-time-grid
:active
(org-agenda-check-type nil
'agenda
)])
16398 ["Write view to file" org-write-agenda t
]
16399 ["Rebuild buffer" org-agenda-redo t
]
16400 ["Save all Org-mode Buffers" org-save-all-org-buffers t
]
16402 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list
]
16404 ["Quit" org-agenda-quit t
]
16405 ["Exit and Release Buffers" org-agenda-exit t
]
16410 (defvar org-agenda-allow-remote-undo t
16411 "Non-nil means, allow remote undo from the agenda buffer.")
16412 (defvar org-agenda-undo-list nil
16413 "List of undoable operations in the agenda since last refresh.")
16414 (defvar org-agenda-undo-has-started-in nil
16415 "Buffers that have already seen `undo-start' in the current undo sequence.")
16416 (defvar org-agenda-pending-undo-list nil
16417 "In a series of undo commands, this is the list of remaning undo items.")
16419 (defmacro org-if-unprotected
(&rest body
)
16420 "Execute BODY if there is no `org-protected' text property at point."
16421 (declare (debug t
))
16422 `(unless (get-text-property (point) 'org-protected
)
16425 (defmacro org-with-remote-undo
(_buffer &rest _body
)
16426 "Execute BODY while recording undo information in two buffers."
16427 (declare (indent 1) (debug t
))
16428 `(let ((_cline (org-current-line))
16429 (_cmd this-command
)
16430 (_buf1 (current-buffer))
16432 (_undo1 buffer-undo-list
)
16433 (_undo2 (with-current-buffer ,_buffer buffer-undo-list
))
16436 (when org-agenda-allow-remote-undo
16437 (setq _c1
(org-verify-change-for-undo
16438 _undo1
(with-current-buffer _buf1 buffer-undo-list
))
16439 _c2
(org-verify-change-for-undo
16440 _undo2
(with-current-buffer _buf2 buffer-undo-list
)))
16442 ;; make sure there are undo boundaries
16443 (and _c1
(with-current-buffer _buf1
(undo-boundary)))
16444 (and _c2
(with-current-buffer _buf2
(undo-boundary)))
16445 ;; remember which buffer to undo
16446 (push (list _cmd _cline _buf1 _c1 _buf2 _c2
)
16447 org-agenda-undo-list
)))))
16449 (defun org-agenda-undo ()
16450 "Undo a remote editing step in the agenda.
16451 This undoes changes both in the agenda buffer and in the remote buffer
16452 that have been changed along."
16454 (or org-agenda-allow-remote-undo
16455 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
16456 (if (not (eq this-command last-command
))
16457 (setq org-agenda-undo-has-started-in nil
16458 org-agenda-pending-undo-list org-agenda-undo-list
))
16459 (if (not org-agenda-pending-undo-list
)
16460 (error "No further undo information"))
16461 (let* ((entry (pop org-agenda-pending-undo-list
))
16462 buf line cmd rembuf
)
16463 (setq cmd
(pop entry
) line
(pop entry
))
16464 (setq rembuf
(nth 2 entry
))
16465 (org-with-remote-undo rembuf
16466 (while (bufferp (setq buf
(pop entry
)))
16468 (with-current-buffer buf
16469 (let ((last-undo-buffer buf
)
16470 (inhibit-read-only t
))
16471 (unless (memq buf org-agenda-undo-has-started-in
)
16472 (push buf org-agenda-undo-has-started-in
)
16473 (make-local-variable 'pending-undo-list
)
16475 (while (and pending-undo-list
16476 (listp pending-undo-list
)
16477 (not (car pending-undo-list
)))
16478 (pop pending-undo-list
))
16481 (message "`%s' undone (buffer %s)" cmd
(buffer-name rembuf
))))
16483 (defun org-verify-change-for-undo (l1 l2
)
16484 "Verify that a real change occurred between the undo lists L1 and L2."
16485 (while (and l1
(listp l1
) (null (car l1
))) (pop l1
))
16486 (while (and l2
(listp l2
) (null (car l2
))) (pop l2
))
16489 ;;; Agenda dispatch
16491 (defvar org-agenda-restrict nil
)
16492 (defvar org-agenda-restrict-begin
(make-marker))
16493 (defvar org-agenda-restrict-end
(make-marker))
16494 (defvar org-agenda-last-dispatch-buffer nil
)
16497 (defun org-agenda (arg)
16498 "Dispatch agenda commands to collect entries to the agenda buffer.
16499 Prompts for a character to select a command. Any prefix arg will be passed
16500 on to the selected command. The default selections are:
16502 a Call `org-agenda-list' to display the agenda for current day or week.
16503 t Call `org-todo-list' to display the global todo list.
16504 T Call `org-todo-list' to display the global todo list, select only
16505 entries with a specific TODO keyword (the user gets a prompt).
16506 m Call `org-tags-view' to display headlines with tags matching
16507 a condition (the user is prompted for the condition).
16508 M Like `m', but select only TODO entries, no ordinary headlines.
16509 L Create a timeline for the current buffer.
16510 e Export views to associated files.
16512 More commands can be added by configuring the variable
16513 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
16514 searches can be pre-defined in this way.
16516 If the current buffer is in Org-mode and visiting a file, you can also
16517 first press `1' to indicate that the agenda should be temporarily (until the
16518 next use of \\[org-agenda]) restricted to the current file."
16521 (let* ((buf (current-buffer))
16522 (bfn (buffer-file-name (buffer-base-buffer)))
16523 (restrict-ok (and bfn
(org-mode-p)))
16524 (custom org-agenda-custom-commands
)
16525 c entry key type match lprops
)
16526 ;; Turn off restriction
16527 (put 'org-agenda-files
'org-restrict nil
)
16528 (setq org-agenda-restrict nil
)
16529 (move-marker org-agenda-restrict-begin nil
)
16530 (move-marker org-agenda-restrict-end nil
)
16531 ;; Delete old local properties
16532 (put 'org-agenda-redo-command
'org-lprops nil
)
16533 ;; Remember where this call originated
16534 (setq org-agenda-last-dispatch-buffer
(current-buffer))
16535 (save-window-excursion
16536 (delete-other-windows)
16537 (switch-to-buffer-other-window " *Agenda Commands*")
16539 (insert (eval-when-compile
16541 "Press key for an agenda command:
16542 -------------------------------- C Configure custom agenda commands
16543 a Agenda for current week or day e Export agenda views
16544 t List of all TODO entries T Entries with special TODO kwd
16545 m Match a TAGS query M Like m, but only TODO entries
16546 L Timeline for current buffer # List stuck projects (!=configure)
16549 (while (string-match "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)" header start
)
16550 (setq start
(match-end 0))
16551 (add-text-properties (match-beginning 2) (match-end 2)
16552 '(face bold
) header
))
16554 (while (setq entry
(pop custom
))
16555 (setq key
(car entry
) type
(nth 1 entry
) match
(nth 2 entry
))
16556 (insert (format "\n%-4s%-14s: %s"
16557 (org-add-props (copy-sequence key
)
16560 ((stringp type
) type
)
16561 ((eq type
'agenda
) "Agenda for current week or day")
16562 ((eq type
'alltodo
) "List of all TODO entries")
16563 ((eq type
'stuck
) "List of stuck projects")
16564 ((eq type
'todo
) "TODO keyword")
16565 ((eq type
'tags
) "Tags query")
16566 ((eq type
'tags-todo
) "Tags (TODO)")
16567 ((eq type
'tags-tree
) "Tags tree")
16568 ((eq type
'todo-tree
) "TODO kwd tree")
16569 ((eq type
'occur-tree
) "Occur tree")
16570 ((functionp type
) (symbol-name type
))
16572 (if (stringp match
)
16573 (org-add-props match nil
'face
'org-warning
)
16574 (format "set of %d commands" (length match
))))))
16577 (org-add-props "1 Restrict call to current buffer 0 Restrict call to region or subtree" nil
'face
'org-table
)))
16578 (goto-char (point-min))
16579 (if (fboundp 'fit-window-to-buffer
) (fit-window-to-buffer))
16580 (message "Press key for agenda command%s"
16581 (if restrict-ok
", or [1] or [0] to restrict" ""))
16582 (setq c
(read-char-exclusive))
16584 (when (memq c
'(?L ?
1 ?
0))
16586 (put 'org-agenda-files
'org-restrict
(list bfn
))
16587 (error "Cannot restrict agenda to current buffer"))
16588 (with-current-buffer " *Agenda Commands*"
16589 (goto-char (point-max))
16590 (delete-region (point-at-bol) (point))
16591 (goto-char (point-min)))
16593 (setq org-agenda-restrict t
)
16594 (with-current-buffer buf
16595 (if (org-region-active-p)
16597 (move-marker org-agenda-restrict-begin
(region-beginning))
16598 (move-marker org-agenda-restrict-end
(region-end)))
16600 (org-back-to-heading t
)
16601 (move-marker org-agenda-restrict-begin
(point))
16602 (move-marker org-agenda-restrict-end
16603 (progn (org-end-of-subtree t
)))))))
16605 (message "Press key for agenda command%s"
16606 (if restrict-ok
" (restricted to current file)" ""))
16607 (setq c
(read-char-exclusive)))
16609 (require 'calendar
) ; FIXME: can we avoid this for some commands?
16610 ;; For example the todo list should not need it (but does...)
16612 ((setq entry
(assoc (char-to-string c
) org-agenda-custom-commands
))
16613 (if (symbolp (nth 1 entry
))
16615 (setq type
(nth 1 entry
) match
(nth 2 entry
) lprops
(nth 3 entry
)
16616 lprops
(nth 3 entry
))
16617 (put 'org-agenda-redo-command
'org-lprops lprops
)
16620 (org-let lprops
'(org-agenda-list current-prefix-arg
)))
16621 ((eq type
'alltodo
)
16622 (org-let lprops
'(org-todo-list current-prefix-arg
)))
16624 (org-let lprops
'(org-agenda-list-stuck-projects
16625 current-prefix-arg
)))
16627 (org-let lprops
'(org-tags-view current-prefix-arg match
)))
16628 ((eq type
'tags-todo
)
16629 (org-let lprops
'(org-tags-view '(4) match
)))
16631 (org-let lprops
'(org-todo-list match
)))
16632 ((eq type
'tags-tree
)
16633 (org-check-for-org-mode)
16634 (org-let lprops
'(org-tags-sparse-tree current-prefix-arg match
)))
16635 ((eq type
'todo-tree
)
16636 (org-check-for-org-mode)
16638 '(org-occur (concat "^" outline-regexp
"[ \t]*"
16639 (regexp-quote match
) "\\>"))))
16640 ((eq type
'occur-tree
)
16641 (org-check-for-org-mode)
16642 (org-let lprops
'(org-occur match
)))
16644 (org-let lprops
'(funcall type match
)))
16645 (t (error "Invalid custom agenda command type %s" type
))))
16646 (org-run-agenda-series (nth 1 entry
) (cddr entry
))))
16647 ((equal c ?C
) (customize-variable 'org-agenda-custom-commands
))
16648 ((equal c ?a
) (call-interactively 'org-agenda-list
))
16649 ((equal c ?t
) (call-interactively 'org-todo-list
))
16650 ((equal c ?T
) (org-call-with-arg 'org-todo-list
(or arg
'(4))))
16651 ((equal c ?m
) (call-interactively 'org-tags-view
))
16652 ((equal c ?M
) (org-call-with-arg 'org-tags-view
(or arg
'(4))))
16653 ((equal c ?e
) (call-interactively 'org-store-agenda-views
))
16655 (unless restrict-ok
16656 (error "This is not an Org-mode file"))
16657 (org-call-with-arg 'org-timeline arg
))
16658 ((equal c ?
#) (call-interactively 'org-agenda-list-stuck-projects
))
16659 ((equal c ?
!) (customize-variable 'org-stuck-projects
))
16660 (t (error "Invalid key"))))))
16662 (defun org-run-agenda-series (name series
)
16663 (org-prepare-agenda name
)
16664 (let* ((org-agenda-multi t
)
16665 (redo (list 'org-run-agenda-series name
(list 'quote series
)))
16666 (cmds (car series
))
16667 (gprops (nth 1 series
))
16668 match
;; The byte compiler incorrectly complains about this. Keep it!
16670 (while (setq cmd
(pop cmds
))
16671 (setq type
(car cmd
) match
(nth 1 cmd
) lprops
(nth 2 cmd
))
16674 (org-let2 gprops lprops
16675 '(call-interactively 'org-agenda-list
)))
16676 ((eq type
'alltodo
)
16677 (org-let2 gprops lprops
16678 '(call-interactively 'org-todo-list
)))
16680 (org-let2 gprops lprops
16681 '(call-interactively 'org-agenda-list-stuck-projects
)))
16683 (org-let2 gprops lprops
16684 '(org-tags-view current-prefix-arg match
)))
16685 ((eq type
'tags-todo
)
16686 (org-let2 gprops lprops
16687 '(org-tags-view '(4) match
)))
16689 (org-let2 gprops lprops
16690 '(org-todo-list match
)))
16692 (org-let2 gprops lprops
16693 '(funcall type match
)))
16694 (t (error "Invalid type in command series"))))
16696 (setq org-agenda-redo-command redo
)
16697 (goto-char (point-min)))
16698 (org-finalize-agenda))
16701 (defmacro org-batch-agenda
(cmd-key &rest parameters
)
16702 "Run an agenda command in batch mode and send the result to STDOUT.
16703 If CMD-KEY is a string of length 1, it is used as a key in
16704 `org-agenda-custom-commands' and triggers this command. If it is a
16705 longer string is is used as a tags/todo match string.
16706 Paramters are alternating variable names and values that will be bound
16707 before running the agenda command."
16710 (push (list (pop parameters
) (if parameters
(pop parameters
))) pars
))
16711 (if (> (length cmd-key
) 1)
16712 (eval (list 'let
(nreverse pars
)
16713 (list 'org-tags-view nil cmd-key
)))
16714 (flet ((read-char-exclusive () (string-to-char cmd-key
)))
16715 (eval (list 'let
(nreverse pars
) '(org-agenda nil
)))))
16716 (set-buffer "*Org Agenda*")
16717 (princ (org-encode-for-stdout (buffer-string)))))
16719 (defun org-encode-for-stdout (string)
16720 (if (fboundp 'encode-coding-string
)
16721 (encode-coding-string string buffer-file-coding-system
)
16724 (defvar org-agenda-info nil
)
16727 (defmacro org-batch-agenda-csv
(cmd-key &rest parameters
)
16728 "Run an agenda command in batch mode and send the result to STDOUT.
16729 If CMD-KEY is a string of length 1, it is used as a key in
16730 `org-agenda-custom-commands' and triggers this command. If it is a
16731 longer string is is used as a tags/todo match string.
16732 Paramters are alternating variable names and values that will be bound
16733 before running the agenda command.
16735 The output gives a line for each selected agenda item. Each
16736 item is a list of comma-separated values, like this:
16738 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
16740 category The category of the item
16741 head The headline, without TODO kwd, TAGS and PRIORITY
16742 type The type of the agenda entry, can be
16743 todo selected in TODO match
16744 tagsmatch selected in tags match
16745 diary imported from diary
16746 deadline a deadline on given date
16747 scheduled scheduled on given date
16748 timestamp entry has timestamp on given date
16749 closed entry was closed on given date
16750 upcoming-deadline warning about deadline
16751 past-scheduled forwarded scheduled item
16752 block entry has date block including g. date
16753 todo The todo keyword, if any
16754 tags All tags including inherited ones, separated by colons
16755 date The relevant date, like 2007-2-14
16756 time The time, like 15:00-16:50
16757 extra Sting with extra planning info
16758 priority-l The priority letter if any was given
16759 priority-n The computed numerical priority
16760 agenda-day The day in the agenda where this is listed"
16764 (push (list (pop parameters
) (if parameters
(pop parameters
))) pars
))
16765 (push (list 'org-agenda-remove-tags t
) pars
)
16766 (if (> (length cmd-key
) 1)
16767 (eval (list 'let
(nreverse pars
)
16768 (list 'org-tags-view nil cmd-key
)))
16769 (flet ((read-char-exclusive () (string-to-char cmd-key
)))
16770 (eval (list 'let
(nreverse pars
) '(org-agenda nil
)))))
16771 (set-buffer "*Org Agenda*")
16772 (let* ((lines (org-split-string (buffer-string) "\n"))
16774 (while (setq line
(pop lines
))
16776 (if (not (get-text-property 0 'org-category line
)) (throw 'next nil
))
16777 (setq org-agenda-info
16778 (org-fix-agenda-info (text-properties-at 0 line
)))
16780 (org-encode-for-stdout
16781 (mapconcat 'org-agenda-export-csv-mapper
16782 '(org-category txt type todo tags date time-of-day extra
16783 priority-letter priority agenda-day
)
16787 (defun org-fix-agenda-info (props)
16790 (when (setq tmp
(plist-get props
'tags
))
16791 (setq props
(plist-put props
'tags
(mapconcat 'identity tmp
":"))))
16792 (when (setq tmp
(plist-get props
'date
))
16793 (if (integerp tmp
) (setq tmp
(calendar-gregorian-from-absolute tmp
)))
16794 (let ((calendar-date-display-form '(year "-" month
"-" day
)))
16795 '((format "%4d, %9s %2s, %4s" dayname monthname day year
))
16797 (setq tmp
(calendar-date-string tmp
)))
16798 (setq props
(plist-put props
'date tmp
)))
16799 (when (setq tmp
(plist-get props
'day
))
16800 (if (integerp tmp
) (setq tmp
(calendar-gregorian-from-absolute tmp
)))
16801 (let ((calendar-date-display-form '(year "-" month
"-" day
)))
16802 (setq tmp
(calendar-date-string tmp
)))
16803 (setq props
(plist-put props
'day tmp
))
16804 (setq props
(plist-put props
'agenda-day tmp
)))
16805 (when (setq tmp
(plist-get props
'txt
))
16806 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp
)
16807 (plist-put props
'priority-letter
(match-string 1 tmp
))
16808 (setq tmp
(replace-match "" t t tmp
)))
16809 (when (and (setq re
(plist-get props
'org-todo-regexp
))
16810 (setq re
(concat "\\`\\.*" re
" ?"))
16811 (string-match re tmp
))
16812 (plist-put props
'todo
(match-string 1 tmp
))
16813 (setq tmp
(replace-match "" t t tmp
)))
16814 (plist-put props
'txt tmp
)))
16817 (defun org-agenda-export-csv-mapper (prop)
16818 (let ((res (plist-get org-agenda-info prop
)))
16822 ((stringp res
) res
)
16823 (t (prin1-to-string res
))))
16824 (while (string-match "," res
)
16825 (setq res
(replace-match ";" t t res
)))
16830 (defun org-store-agenda-views (&rest parameters
)
16832 (eval (list 'org-batch-store-agenda-views
)))
16834 (defvar org-agenda-buffer-name
)
16836 ;; FIXME, why is this a macro?????
16838 (defmacro org-batch-store-agenda-views
(&rest parameters
)
16839 "Run all custom agenda commands that have a file argument."
16840 (let ((cmds org-agenda-custom-commands
)
16841 (dir default-directory
)
16842 pars cmd thiscmdkey files opts
)
16844 (push (list (pop parameters
) (if parameters
(pop parameters
))) pars
))
16845 (setq pars
(reverse pars
))
16846 (save-window-excursion
16848 (setq cmd
(pop cmds
)
16849 thiscmdkey
(car cmd
)
16851 files
(org-last cmd
))
16852 (if (stringp files
) (setq files
(list files
)))
16854 (flet ((read-char-exclusive () (string-to-char thiscmdkey
)))
16855 (eval (list 'let
(append org-agenda-exporter-settings opts pars
)
16856 '(org-agenda nil
))))
16857 (set-buffer "*Org Agenda*")
16859 (eval (list 'let
(append org-agenda-exporter-settings opts pars
)
16860 (list 'org-write-agenda
16861 (expand-file-name (pop files
) dir
) t
)))))
16862 (kill-buffer org-agenda-buffer-name
)))))
16864 (defun org-write-agenda (file &optional nosettings
)
16865 "Write the current buffer (an agenda view) as a file.
16866 Depending on the extension of the file name, plain text (.txt),
16867 HTML (.html or .htm) or Postscript (.ps) is produced.
16868 If NOSETTINGS is given, do not scope the settings of
16869 `org-agenda-exporter-settings' into the export commands. This is used when
16870 the settings have already been scoped and we do not wish to overrule other,
16871 higher priority settings."
16872 (interactive "FWrite agenda to file: ")
16873 (if (not (file-writable-p file
))
16874 (error "Cannot write agenda to file %s" file
))
16876 ((string-match "\\.html?\\'" file
) (require 'htmlize
))
16877 ((string-match "\\.ps\\'" file
) (require 'ps-print
)))
16878 (org-let (if nosettings nil org-agenda-exporter-settings
)
16880 (save-window-excursion
16882 ((string-match "\\.html?\\'" file
)
16883 (set-buffer (htmlize-buffer (current-buffer)))
16885 (when (and org-agenda-export-html-style
16886 (string-match "<style>" org-agenda-export-html-style
))
16887 ;; replace <style> section with org-agenda-export-html-style
16888 (goto-char (point-min))
16889 (kill-region (- (search-forward "<style") 6)
16890 (search-forward "</style>"))
16891 (insert org-agenda-export-html-style
))
16893 (kill-buffer (current-buffer))
16894 (message "HTML written to %s" file
))
16895 ((string-match "\\.ps\\'" file
)
16896 (ps-print-buffer-with-faces file
)
16897 (message "Postscript written to %s" file
))
16899 (let ((bs (buffer-string)))
16903 (kill-buffer (current-buffer))
16904 (message "Plain text written to %s" file
))))))
16905 (set-buffer org-agenda-buffer-name
)))
16907 (defmacro org-no-read-only
(&rest body
)
16908 "Inhibit read-only for BODY."
16909 `(let ((inhibit-read-only t
)) ,@body
))
16911 (defun org-check-for-org-mode ()
16912 "Make sure current buffer is in org-mode. Error if not."
16914 (error "Cannot execute org-mode agenda command on buffer in %s."
16917 (defun org-fit-agenda-window ()
16918 "Fit the window to the buffer size."
16919 (and (memq org-agenda-window-setup
'(reorganize-frame))
16920 (fboundp 'fit-window-to-buffer
)
16921 (fit-window-to-buffer nil
(/ (* (frame-height) 3) 4)
16922 (/ (frame-height) 2))))
16924 ;;; Agenda file list
16926 (defun org-agenda-files (&optional unrestricted
)
16927 "Get the list of agenda files.
16928 Optional UNRESTRICTED means return the full list even if a restriction
16929 is currently in place."
16931 ((and (not unrestricted
) (get 'org-agenda-files
'org-restrict
)))
16932 ((stringp org-agenda-files
) (org-read-agenda-file-list))
16933 ((listp org-agenda-files
) org-agenda-files
)
16934 (t (error "Invalid value of `org-agenda-files'"))))
16936 (defun org-edit-agenda-file-list ()
16937 "Edit the list of agenda files.
16938 Depending on setup, this either uses customize to edit the variable
16939 `org-agenda-files', or it visits the file that is holding the list. In the
16940 latter case, the buffer is set up in a way that saving it automatically kills
16941 the buffer and restores the previous window configuration."
16943 (if (stringp org-agenda-files
)
16944 (let ((cw (current-window-configuration)))
16945 (find-file org-agenda-files
)
16946 (org-set-local 'org-window-configuration cw
)
16947 (org-add-hook 'after-save-hook
16949 (set-window-configuration
16950 (prog1 org-window-configuration
16951 (kill-buffer (current-buffer))))
16952 (org-install-agenda-files-menu)
16953 (message "New agenda file list installed"))
16955 (message (substitute-command-keys
16956 "Edit list and finish with \\[save-buffer]")))
16957 (customize-variable 'org-agenda-files
)))
16959 (defun org-store-new-agenda-file-list (list)
16960 "Set new value for the agenda file list and save it correcly."
16961 (if (stringp org-agenda-files
)
16962 (let ((f org-agenda-files
) b
)
16963 (while (setq b
(find-buffer-visiting f
)) (kill-buffer b
))
16965 (insert (mapconcat 'identity list
"\n") "\n")))
16966 (let ((org-mode-hook nil
) (default-major-mode 'fundamental-mode
))
16967 (setq org-agenda-files list
)
16968 (customize-save-variable 'org-agenda-files org-agenda-files
))))
16970 (defun org-read-agenda-file-list ()
16971 "Read the list of agenda files from a file."
16972 (when (stringp org-agenda-files
)
16974 (insert-file-contents org-agenda-files
)
16975 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
16979 (defun org-cycle-agenda-files ()
16980 "Cycle through the files in `org-agenda-files'.
16981 If the current buffer visits an agenda file, find the next one in the list.
16982 If the current buffer does not, find the first agenda file."
16984 (let* ((fs (org-agenda-files t
))
16985 (files (append fs
(list (car fs
))))
16986 (tcf (if buffer-file-name
(file-truename buffer-file-name
)))
16988 (unless files
(error "No agenda files"))
16990 (while (setq file
(pop files
))
16991 (if (equal (file-truename file
) tcf
)
16993 (find-file (car files
))
16995 (find-file (car fs
)))
16996 (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
16998 (defun org-agenda-file-to-front (&optional to-end
)
16999 "Move/add the current file to the top of the agenda file list.
17000 If the file is not present in the list, it is added to the front. If it is
17001 present, it is moved there. With optional argument TO-END, add/move to the
17004 (let ((file-alist (mapcar (lambda (x)
17005 (cons (file-truename x
) x
))
17006 (org-agenda-files t
)))
17007 (ctf (file-truename buffer-file-name
))
17009 (setq x
(assoc ctf file-alist
) had x
)
17011 (if (not x
) (setq x
(cons ctf
(abbreviate-file-name buffer-file-name
))))
17013 (setq file-alist
(append (delq x file-alist
) (list x
)))
17014 (setq file-alist
(cons x
(delq x file-alist
))))
17015 (org-store-new-agenda-file-list (mapcar 'cdr file-alist
))
17016 (org-install-agenda-files-menu)
17017 (message "File %s to %s of agenda file list"
17018 (if had
"moved" "added") (if to-end
"end" "front"))))
17020 (defun org-remove-file (&optional file
)
17021 "Remove current file from the list of files in variable `org-agenda-files'.
17022 These are the files which are being checked for agenda entries.
17023 Optional argument FILE means, use this file instead of the current."
17025 (let* ((file (or file buffer-file-name
))
17026 (true-file (file-truename file
))
17027 (afile (abbreviate-file-name file
))
17028 (files (delq nil
(mapcar
17030 (if (equal true-file
17033 (org-agenda-files t
)))))
17034 (if (not (= (length files
) (length (org-agenda-files t
))))
17036 (org-store-new-agenda-file-list files
)
17037 (org-install-agenda-files-menu)
17038 (message "Removed file: %s" afile
))
17039 (message "File was not in list: %s" afile
))))
17041 (defun org-file-menu-entry (file)
17042 (vector file
(list 'find-file file
) t
))
17044 (defun org-check-agenda-file (file)
17045 "Make sure FILE exists. If not, ask user what to do."
17046 (when (not (file-exists-p file
))
17047 (message "non-existent file %s. [R]emove from list or [A]bort?"
17048 (abbreviate-file-name file
))
17049 (let ((r (downcase (read-char-exclusive))))
17052 (org-remove-file file
)
17053 (throw 'nextfile t
))
17054 (t (error "Abort"))))))
17056 ;;; Agenda prepare and finalize
17058 (defvar org-agenda-multi nil
) ; dynammically scoped
17059 (defvar org-agenda-buffer-name
"*Org Agenda*")
17060 (defvar org-pre-agenda-window-conf nil
)
17061 (defvar org-agenda-name nil
)
17062 (defun org-prepare-agenda (&optional name
)
17063 (setq org-todo-keywords-for-agenda nil
)
17064 (setq org-done-keywords-for-agenda nil
)
17065 (if org-agenda-multi
17067 (setq buffer-read-only nil
)
17068 (goto-char (point-max))
17070 (insert "\n" (make-string (window-width) ?
=) "\n"))
17071 (narrow-to-region (point) (point-max)))
17072 (org-agenda-maybe-reset-markers 'force
)
17073 (org-prepare-agenda-buffers (org-agenda-files))
17074 (setq org-todo-keywords-for-agenda
17075 (org-uniquify org-todo-keywords-for-agenda
))
17076 (setq org-done-keywords-for-agenda
17077 (org-uniquify org-done-keywords-for-agenda
))
17078 (let* ((abuf (get-buffer-create org-agenda-buffer-name
))
17079 (awin (get-buffer-window abuf
)))
17081 ((equal (current-buffer) abuf
) nil
)
17082 (awin (select-window awin
))
17083 ((not (setq org-pre-agenda-window-conf
(current-window-configuration))))
17084 ((equal org-agenda-window-setup
'current-window
)
17085 (switch-to-buffer abuf
))
17086 ((equal org-agenda-window-setup
'other-window
)
17087 (switch-to-buffer-other-window abuf
))
17088 ((equal org-agenda-window-setup
'other-frame
)
17089 (switch-to-buffer-other-frame abuf
))
17090 ((equal org-agenda-window-setup
'reorganize-frame
)
17091 (delete-other-windows)
17092 (switch-to-buffer-other-window abuf
))))
17093 (setq buffer-read-only nil
)
17096 (and name
(not org-agenda-name
)
17097 (org-set-local 'org-agenda-name name
)))
17098 (setq buffer-read-only nil
))
17100 (defun org-finalize-agenda ()
17101 "Finishing touch for the agenda buffer, called just before displaying it."
17102 (unless org-agenda-multi
17104 (let ((inhibit-read-only t
))
17105 (goto-char (point-min))
17106 (while (org-activate-bracket-links (point-max))
17107 (add-text-properties (match-beginning 0) (match-end 0)
17109 (org-agenda-align-tags)
17110 (unless org-agenda-with-colors
17111 (remove-text-properties (point-min) (point-max) '(face nil
))))
17112 (if (and (boundp 'org-overriding-columns-format
)
17113 org-overriding-columns-format
)
17114 (org-set-local 'org-overriding-columns-format
17115 org-overriding-columns-format
))
17116 (if (and (boundp 'org-agenda-view-columns-initially
)
17117 org-agenda-view-columns-initially
)
17118 (org-agenda-columns))
17119 (run-hooks 'org-finalize-agenda-hook
))))
17121 (defun org-prepare-agenda-buffers (files)
17122 "Create buffers for all agenda files, protect archived trees and comments."
17124 (let ((pa '(:org-archived t
))
17125 (pc '(:org-comment t
))
17126 (pall '(:org-archived t
:org-comment t
))
17127 (inhibit-read-only t
)
17128 (rea (concat ":" org-archive-tag
":"))
17132 (while (setq file
(pop files
))
17133 (org-check-agenda-file file
)
17134 (set-buffer (org-get-agenda-file-buffer file
))
17136 (setq bmp
(buffer-modified-p))
17137 (setq org-todo-keywords-for-agenda
17138 (append org-todo-keywords-for-agenda org-todo-keywords-1
))
17139 (setq org-done-keywords-for-agenda
17140 (append org-done-keywords-for-agenda org-done-keywords
))
17142 (remove-text-properties (point-min) (point-max) pall
)
17143 (when org-agenda-skip-archived-trees
17144 (goto-char (point-min))
17145 (while (re-search-forward rea nil t
)
17146 (if (org-on-heading-p t
)
17147 (add-text-properties (point-at-bol) (org-end-of-subtree t
) pa
))))
17148 (goto-char (point-min))
17149 (setq re
(concat "^\\*+ +" org-comment-string
"\\>"))
17150 (while (re-search-forward re nil t
)
17151 (add-text-properties
17152 (match-beginning 0) (org-end-of-subtree t
) pc
)))
17153 (set-buffer-modified-p bmp
))))))
17155 (defvar org-agenda-skip-function nil
17156 "Function to be called at each match during agenda construction.
17157 If this function returns nil, the current match should not be skipped.
17158 Otherwise, the function must return a position from where the search
17159 should be continued.
17160 This may also be a Lisp form, it will be evaluated.
17161 Never set this variable using `setq' or so, because then it will apply
17162 to all future agenda commands. Instead, bind it with `let' to scope
17163 it dynamically into the agenda-constructing command. A good way to set
17164 it is through options in org-agenda-custom-commands.")
17166 (defun org-agenda-skip ()
17167 "Throw to `:skip' in places that should be skipped.
17168 Also moves point to the end of the skipped region, so that search can
17169 continue from there."
17170 (let ((p (point-at-bol)) to fp
)
17171 (and org-agenda-skip-archived-trees
17172 (get-text-property p
:org-archived
)
17173 (org-end-of-subtree t
)
17175 (and (get-text-property p
:org-comment
)
17176 (org-end-of-subtree t
)
17178 (if (equal (char-after p
) ?
#) (throw :skip t
))
17179 (when (and (or (setq fp
(functionp org-agenda-skip-function
))
17180 (consp org-agenda-skip-function
))
17181 (setq to
(save-excursion
17184 (funcall org-agenda-skip-function
)
17185 (eval org-agenda-skip-function
))))))
17189 (defvar org-agenda-markers nil
17190 "List of all currently active markers created by `org-agenda'.")
17191 (defvar org-agenda-last-marker-time
(time-to-seconds (current-time))
17192 "Creation time of the last agenda marker.")
17194 (defun org-agenda-new-marker (&optional pos
)
17195 "Return a new agenda marker.
17196 Org-mode keeps a list of these markers and resets them when they are
17198 (let ((m (copy-marker (or pos
(point)))))
17199 (setq org-agenda-last-marker-time
(time-to-seconds (current-time)))
17200 (push m org-agenda-markers
)
17203 (defun org-agenda-maybe-reset-markers (&optional force
)
17204 "Reset markers created by `org-agenda'. But only if they are old enough."
17205 (if (or (and force
(not org-agenda-multi
))
17206 (> (- (time-to-seconds (current-time))
17207 org-agenda-last-marker-time
)
17209 (while org-agenda-markers
17210 (move-marker (pop org-agenda-markers
) nil
))))
17212 (defvar org-agenda-new-buffers nil
17213 "Buffers created to visit agenda files.")
17215 (defun org-get-agenda-file-buffer (file)
17216 "Get a buffer visiting FILE. If the buffer needs to be created, add
17217 it to the list of buffers which might be released later."
17218 (let ((buf (org-find-base-buffer-visiting file
)))
17220 buf
; just return it
17221 ;; Make a new buffer and remember it
17222 (setq buf
(find-file-noselect file
))
17223 (if buf
(push buf org-agenda-new-buffers
))
17226 (defun org-release-buffers (blist)
17227 "Release all buffers in list, asking the user for confirmation when needed.
17228 When a buffer is unmodified, it is just killed. When modified, it is saved
17229 \(if the user agrees) and then killed."
17231 (while (setq buf
(pop blist
))
17232 (setq file
(buffer-file-name buf
))
17233 (when (and (buffer-modified-p buf
)
17235 (y-or-n-p (format "Save file %s? " file
)))
17236 (with-current-buffer buf
(save-buffer)))
17237 (kill-buffer buf
))))
17239 (defvar org-category-table nil
)
17240 (defun org-get-category-table ()
17241 "Get the table of categories and positions in current buffer."
17246 (goto-char (point-min))
17247 (while (re-search-forward "^#\\+CATEGORY:[ \t]*\\(.*\\)"
17249 (push (cons (match-beginning 1)
17250 (org-trim (match-string 1))) tbl
))))
17253 (defun org-get-category (&optional pos
)
17254 "Get the category applying to position POS."
17255 (if (not org-category-table
)
17257 ((null org-category
)
17259 (if buffer-file-name
17260 (file-name-sans-extension
17261 (file-name-nondirectory buffer-file-name
))
17263 ((symbolp org-category
) (symbol-name org-category
))
17265 (let ((tbl org-category-table
)
17266 (pos (or pos
(point))))
17267 (while (and tbl
(> (caar tbl
) pos
))
17269 (or (cdar tbl
) (cdr (nth (1- (length org-category-table
))
17270 org-category-table
))))))
17271 ;;; Agenda timeline
17273 (defun org-timeline (&optional include-all
)
17274 "Show a time-sorted view of the entries in the current org file.
17275 Only entries with a time stamp of today or later will be listed. With
17276 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
17277 under the current date.
17278 If the buffer contains an active region, only check the region for
17281 (require 'calendar
)
17282 (org-compile-prefix-format 'timeline
)
17283 (org-set-sorting-strategy 'timeline
)
17285 (dotodo include-all
)
17286 (doclosed org-agenda-show-log
)
17287 (entry buffer-file-name
)
17288 (date (calendar-current-date))
17289 (beg (if (org-region-active-p) (region-beginning) (point-min)))
17290 (end (if (org-region-active-p) (region-end) (point-max)))
17291 (day-numbers (org-get-all-dates beg end
'no-ranges
17292 t doclosed
; always include today
17293 org-timeline-show-empty-dates
))
17294 (today (time-to-days (current-time)))
17298 (setq org-agenda-redo-command
17300 (list 'switch-to-buffer-other-window
(current-buffer))
17301 (list 'org-timeline
(list 'quote include-all
))))
17303 ;; Remove past dates from the list of dates.
17304 (setq day-numbers
(delq nil
(mapcar (lambda(x)
17305 (if (>= x today
) x nil
))
17307 (org-prepare-agenda (concat "Timeline "
17308 (file-name-nondirectory buffer-file-name
)))
17309 (if doclosed
(push :closed args
))
17310 (push :timestamp args
)
17312 (if dotodo
(push :todo args
))
17313 (while (setq d
(pop day-numbers
))
17314 (if (and (listp d
) (eq (car d
) :omitted
))
17317 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d
)))
17318 (put-text-property s
(1- (point)) 'face
'org-agenda-structure
))
17319 (if (listp d
) (setq d
(car d
) emptyp t
) (setq emptyp nil
))
17320 (if (and (>= d today
)
17325 (insert (make-string 79 ?-
) "\n")))
17326 (setq date
(calendar-gregorian-from-absolute d
))
17328 (setq rtn
(and (not emptyp
)
17329 (apply 'org-agenda-get-day-entries
17331 (if (or rtn
(equal d today
) org-timeline-show-empty-dates
)
17333 (insert (calendar-day-name date
) " "
17334 (number-to-string (extract-calendar-day date
)) " "
17335 (calendar-month-name (extract-calendar-month date
)) " "
17336 (number-to-string (extract-calendar-year date
)) "\n")
17337 ; FIXME: this gives a timezone problem
17338 ; (insert (format-time-string org-agenda-date-format
17339 ; (calendar-time-from-absolute d 0))
17341 (put-text-property s
(1- (point)) 'face
'org-agenda-structure
)
17342 (put-text-property s
(1- (point)) 'org-date-line t
)
17343 (if (equal d today
)
17344 (put-text-property s
(1- (point)) 'org-today t
))
17345 (and rtn
(insert (org-finalize-agenda-entries rtn
) "\n"))
17346 (put-text-property s
(1- (point)) 'day d
)))))
17347 (goto-char (point-min))
17348 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t
)
17350 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline
))
17351 (org-finalize-agenda)
17352 (setq buffer-read-only t
)))
17354 (defun org-get-all-dates (beg end
&optional no-ranges force-today inactive empty
)
17355 "Return a list of all relevant day numbers from BEG to END buffer positions.
17356 If NO-RANGES is non-nil, include only the start and end dates of a range,
17357 not every single day in the range. If FORCE-TODAY is non-nil, make
17358 sure that TODAY is included in the list. If INACTIVE is non-nil, also
17359 inactive time stamps (those in square brackets) are included.
17360 When EMPTY is non-nil, also include days without any entries."
17361 (let ((re (if inactive org-ts-regexp-both org-ts-regexp
))
17362 dates dates1 date day day1 day2 ts1 ts2
)
17364 (setq dates
(list (time-to-days (current-time)))))
17367 (while (re-search-forward re end t
)
17368 (setq day
(time-to-days (org-time-string-to-time
17369 (substring (match-string 1) 0 10))))
17370 (or (memq day dates
) (push day dates
)))
17373 (while (re-search-forward org-tr-regexp end t
)
17374 (setq ts1
(substring (match-string 1) 0 10)
17375 ts2
(substring (match-string 2) 0 10)
17376 day1
(time-to-days (org-time-string-to-time ts1
))
17377 day2
(time-to-days (org-time-string-to-time ts2
)))
17378 (while (< (setq day1
(1+ day1
)) day2
)
17379 (or (memq day1 dates
) (push day1 dates
)))))
17380 (setq dates
(sort dates
'<))
17382 (while (setq day
(pop dates
))
17383 (setq day2
(car dates
))
17385 (when (and day2 empty
)
17386 (if (or (eq empty t
)
17387 (and (numberp empty
) (<= (- day2 day
) empty
)))
17388 (while (< (setq day
(1+ day
)) day2
)
17389 (push (list day
) dates1
))
17390 (push (cons :omitted
(- day2 day
)) dates1
))))
17391 (setq dates
(nreverse dates1
)))
17394 ;;; Agenda Daily/Weekly
17396 (defvar org-agenda-overriding-arguments nil
) ; dynamically scoped parameter
17397 (defvar org-agenda-start-day nil
) ; dynamically scoped parameter
17398 (defvar org-agenda-last-arguments nil
17399 "The arguments of the previous call to org-agenda")
17400 (defvar org-starting-day nil
) ; local variable in the agenda buffer
17401 (defvar org-agenda-span nil
) ; local variable in the agenda buffer
17402 (defvar org-include-all-loc nil
) ; local variable
17406 (defun org-agenda-list (&optional include-all start-day ndays
)
17407 "Produce a weekly view from all files in variable `org-agenda-files'.
17408 The view will be for the current week, but from the overview buffer you
17409 will be able to go to other weeks.
17410 With one \\[universal-argument] prefix argument INCLUDE-ALL, all unfinished TODO items will
17411 also be shown, under the current date.
17412 With two \\[universal-argument] prefix argument INCLUDE-ALL, all TODO entries marked DONE
17413 on the days are also shown. See the variable `org-log-done' for how
17414 to turn on logging.
17415 START-DAY defaults to TODAY, or to the most recent match for the weekday
17416 given in `org-agenda-start-on-weekday'.
17417 NDAYS defaults to `org-agenda-ndays'."
17419 (setq ndays
(or ndays org-agenda-ndays
)
17420 start-day
(or start-day org-agenda-start-day
))
17421 (if org-agenda-overriding-arguments
17422 (setq include-all
(car org-agenda-overriding-arguments
)
17423 start-day
(nth 1 org-agenda-overriding-arguments
)
17424 ndays
(nth 2 org-agenda-overriding-arguments
)))
17425 (if (stringp start-day
)
17426 ;; Convert to an absolute day number
17427 (setq start-day
(time-to-days (org-read-date nil t start-day
))))
17428 (setq org-agenda-last-arguments
(list include-all start-day ndays
))
17429 (org-compile-prefix-format 'agenda
)
17430 (org-set-sorting-strategy 'agenda
)
17431 (require 'calendar
)
17432 (let* ((org-agenda-start-on-weekday
17433 (if (or (equal ndays
7) (and (null ndays
) (equal 7 org-agenda-ndays
)))
17434 org-agenda-start-on-weekday nil
))
17435 (thefiles (org-agenda-files))
17437 (today (time-to-days (current-time)))
17438 (sd (or start-day today
))
17439 (start (if (or (null org-agenda-start-on-weekday
)
17440 (< org-agenda-ndays
7))
17442 (let* ((nt (calendar-day-of-week
17443 (calendar-gregorian-from-absolute sd
)))
17444 (n1 org-agenda-start-on-weekday
)
17446 (- sd
(+ (if (< d
0) 7 0) d
)))))
17447 (day-numbers (list start
))
17448 (inhibit-redisplay (not debug-on-error
))
17449 s e rtn rtnall file date d start-pos end-pos todayp nd
)
17450 (setq org-agenda-redo-command
17451 (list 'org-agenda-list
(list 'quote include-all
) start-day ndays
))
17452 ;; Make the list of days
17453 (setq ndays
(or ndays org-agenda-ndays
)
17456 (push (1+ (car day-numbers
)) day-numbers
)
17457 (setq ndays
(1- ndays
)))
17458 (setq day-numbers
(nreverse day-numbers
))
17459 (org-prepare-agenda "Day/Week")
17460 (org-set-local 'org-starting-day
(car day-numbers
))
17461 (org-set-local 'org-include-all-loc include-all
)
17462 (org-set-local 'org-agenda-span
17463 (org-agenda-ndays-to-span nd
))
17464 (when (and (or include-all org-agenda-include-all-todo
)
17465 (member today day-numbers
))
17466 (setq files thefiles
17468 (while (setq file
(pop files
))
17470 (org-check-agenda-file file
)
17471 (setq date
(calendar-gregorian-from-absolute today
)
17472 rtn
(org-agenda-get-day-entries
17474 (setq rtnall
(append rtnall rtn
))))
17476 (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
17477 (add-text-properties (point-min) (1- (point))
17478 (list 'face
'org-agenda-structure
))
17479 (insert (org-finalize-agenda-entries rtnall
) "\n")))
17481 (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd
)))
17483 (add-text-properties s
(1- (point)) (list 'face
'org-agenda-structure
17485 (while (setq d
(pop day-numbers
))
17486 (setq date
(calendar-gregorian-from-absolute d
)
17488 (if (or (setq todayp
(= d today
))
17489 (and (not start-pos
) (= d sd
)))
17490 (setq start-pos
(point))
17491 (if (and start-pos
(not end-pos
))
17492 (setq end-pos
(point))))
17493 (setq files thefiles
17495 (while (setq file
(pop files
))
17497 (org-check-agenda-file file
)
17498 (if org-agenda-show-log
17499 (setq rtn
(org-agenda-get-day-entries
17501 :deadline
:scheduled
:timestamp
:sexp
:closed
))
17502 (setq rtn
(org-agenda-get-day-entries
17504 :deadline
:scheduled
:sexp
:timestamp
)))
17505 (setq rtnall
(append rtnall rtn
))))
17506 (if org-agenda-include-diary
17508 (require 'diary-lib
)
17509 (setq rtn
(org-get-entries-from-diary date
))
17510 (setq rtnall
(append rtnall rtn
))))
17511 (if (or rtnall org-agenda-show-all-dates
)
17513 (insert (format "%-9s %2d %s %4d\n"
17514 (calendar-day-name date
)
17515 (extract-calendar-day date
)
17516 (calendar-month-name (extract-calendar-month date
))
17517 (extract-calendar-year date
)))
17518 ; FIXME: this gives a timezone problem
17519 ; (insert (format-time-string org-agenda-date-format
17520 ; (calendar-time-from-absolute d 0)) "\n")
17521 (put-text-property s
(1- (point)) 'face
'org-agenda-structure
)
17522 (put-text-property s
(1- (point)) 'org-date-line t
)
17523 (if todayp
(put-text-property s
(1- (point)) 'org-today t
))
17525 (org-finalize-agenda-entries
17526 (org-agenda-add-time-grid-maybe
17529 (put-text-property s
(1- (point)) 'day d
))))
17530 (goto-char (point-min))
17531 (org-fit-agenda-window)
17532 (unless (and (pos-visible-in-window-p (point-min))
17533 (pos-visible-in-window-p (point-max)))
17534 (goto-char (1- (point-max)))
17536 (if (not (pos-visible-in-window-p (or start-pos
1)))
17538 (goto-char (or start-pos
1))
17540 (goto-char (or start-pos
1))
17541 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda
))
17542 (org-finalize-agenda)
17543 (setq buffer-read-only t
)
17546 (defun org-agenda-ndays-to-span (n)
17547 (cond ((< n
7) 'day
) ((= n
7) 'week
) ((< n
32) 'month
) (t 'year
)))
17549 ;;; Agenda TODO list
17551 (defvar org-select-this-todo-keyword nil
)
17552 (defvar org-last-arg nil
)
17555 (defun org-todo-list (arg)
17556 "Show all TODO entries from all agenda file in a single list.
17557 The prefix arg can be used to select a specific TODO keyword and limit
17558 the list to these. When using \\[universal-argument], you will be prompted
17559 for a keyword. A numeric prefix directly selects the Nth keyword in
17560 `org-todo-keywords-1'."
17562 (require 'calendar
)
17563 (org-compile-prefix-format 'todo
)
17564 (org-set-sorting-strategy 'todo
)
17565 (org-prepare-agenda "TODO")
17566 (let* ((today (time-to-days (current-time)))
17567 (date (calendar-gregorian-from-absolute today
))
17568 (kwds org-todo-keywords-for-agenda
)
17569 (completion-ignore-case t
)
17570 (org-select-this-todo-keyword
17571 (if (stringp arg
) arg
17572 (and arg
(integerp arg
) (> arg
0)
17573 (nth (1- arg
) kwds
))))
17574 rtn rtnall files file pos
)
17575 (when (equal arg
'(4))
17576 (setq org-select-this-todo-keyword
17577 (completing-read "Keyword (or KWD1|K2D2|...): "
17578 (mapcar 'list kwds
) nil nil
)))
17579 (and (equal 0 arg
) (setq org-select-this-todo-keyword nil
))
17580 (org-set-local 'org-last-arg arg
)
17581 (setq org-agenda-redo-command
17582 '(org-todo-list (or current-prefix-arg org-last-arg
)))
17583 (setq files
(org-agenda-files)
17585 (while (setq file
(pop files
))
17587 (org-check-agenda-file file
)
17588 (setq rtn
(org-agenda-get-day-entries file date
:todo
))
17589 (setq rtnall
(append rtnall rtn
))))
17590 (if org-agenda-overriding-header
17591 (insert (org-add-props (copy-sequence org-agenda-overriding-header
)
17592 nil
'face
'org-agenda-structure
) "\n")
17593 (insert "Global list of TODO items of type: ")
17594 (add-text-properties (point-min) (1- (point))
17595 (list 'face
'org-agenda-structure
))
17597 (insert (or org-select-this-todo-keyword
"ALL") "\n")
17598 (add-text-properties pos
(1- (point)) (list 'face
'org-warning
))
17600 (unless org-agenda-multi
17601 (insert "Available with `N r': (0)ALL")
17604 (setq s
(format "(%d)%s" (setq n
(1+ n
)) x
))
17605 (if (> (+ (current-column) (string-width s
) 1) (frame-width))
17610 (add-text-properties pos
(1- (point)) (list 'face
'org-agenda-structure
)))
17612 (insert (org-finalize-agenda-entries rtnall
) "\n"))
17613 (goto-char (point-min))
17614 (org-fit-agenda-window)
17615 (add-text-properties (point-min) (point-max) '(org-agenda-type todo
))
17616 (org-finalize-agenda)
17617 (setq buffer-read-only t
)))
17619 ;;; Agenda tags match
17622 (defun org-tags-view (&optional todo-only match
)
17623 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
17624 The prefix arg TODO-ONLY limits the search to TODO entries."
17626 (org-compile-prefix-format 'tags
)
17627 (org-set-sorting-strategy 'tags
)
17628 (let* ((org-tags-match-list-sublevels
17629 (if todo-only t org-tags-match-list-sublevels
))
17630 (completion-ignore-case t
)
17631 rtn rtnall files file pos matcher
17633 (setq matcher
(org-make-tags-matcher match
)
17634 match
(car matcher
) matcher
(cdr matcher
))
17635 (org-prepare-agenda (concat "TAGS " match
))
17636 (setq org-agenda-redo-command
17637 (list 'org-tags-view
(list 'quote todo-only
)
17638 (list 'if
'current-prefix-arg nil match
)))
17639 (setq files
(org-agenda-files)
17641 (while (setq file
(pop files
))
17643 (org-check-agenda-file file
)
17644 (setq buffer
(if (file-exists-p file
)
17645 (org-get-agenda-file-buffer file
)
17646 (error "No such file %s" file
)))
17648 ;; If file does not exist, merror message to agenda
17650 (format "ORG-AGENDA-ERROR: No such org-file %s" file
))
17651 rtnall
(append rtnall rtn
))
17652 (with-current-buffer buffer
17653 (unless (org-mode-p)
17654 (error "Agenda file %s is not in `org-mode'" file
))
17655 (setq org-category-table
(org-get-category-table))
17658 (if org-agenda-restrict
17659 (narrow-to-region org-agenda-restrict-begin
17660 org-agenda-restrict-end
)
17662 (setq rtn
(org-scan-tags 'agenda matcher todo-only
))
17663 (setq rtnall
(append rtnall rtn
))))))))
17664 (if org-agenda-overriding-header
17665 (insert (org-add-props (copy-sequence org-agenda-overriding-header
)
17666 nil
'face
'org-agenda-structure
) "\n")
17667 (insert "Headlines with TAGS match: ")
17668 (add-text-properties (point-min) (1- (point))
17669 (list 'face
'org-agenda-structure
))
17671 (insert match
"\n")
17672 (add-text-properties pos
(1- (point)) (list 'face
'org-warning
))
17674 (unless org-agenda-multi
17675 (insert "Press `C-u r' to search again with new search string\n"))
17676 (add-text-properties pos
(1- (point)) (list 'face
'org-agenda-structure
)))
17678 (insert (org-finalize-agenda-entries rtnall
) "\n"))
17679 (goto-char (point-min))
17680 (org-fit-agenda-window)
17681 (add-text-properties (point-min) (point-max) '(org-agenda-type tags
))
17682 (org-finalize-agenda)
17683 (setq buffer-read-only t
)))
17685 ;;; Agenda Finding stuck projects
17687 (defvar org-agenda-skip-regexp nil
17688 "Regular expression used in skipping subtrees for the agenda.
17689 This is basically a temporary global variable that can be set and then
17690 used by user-defined selections using `org-agenda-skip-function'.")
17692 (defvar org-agenda-overriding-header nil
17693 "When this is set during todo and tags searches, will replace header.")
17695 (defun org-agenda-skip-subtree-when-regexp-matches ()
17696 "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
17697 If yes, it returns the end position of this tree, causing agenda commands
17698 to skip this subtree. This is a function that can be put into
17699 `org-agenda-skip-function' for the duration of a command."
17700 (let ((end (save-excursion (org-end-of-subtree t
)))
17703 (setq skip
(re-search-forward org-agenda-skip-regexp end t
)))
17706 (defun org-agenda-skip-entry-if (&rest conditions
)
17707 "Skip entry is any of CONDITIONS is true.
17708 See `org-agenda-skip-if for details."
17709 (org-agenda-skip-if nil conditions
))
17710 (defun org-agenda-skip-subtree-if (&rest conditions
)
17711 "Skip entry is any of CONDITIONS is true.
17712 See `org-agenda-skip-if for details."
17713 (org-agenda-skip-if t conditions
))
17715 (defun org-agenda-skip-if (subtree conditions
)
17716 "Checks current entity for CONDITIONS.
17717 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
17718 the entry, i.e. the text before the next heading is checked.
17720 CONDITIONS is a list of symbols, boolean OR is used to combine the results
17721 from different tests. Valid conditions are:
17723 scheduled Check if there is a scheduled cookie
17724 notscheduled Check if there is no scheduled cookie
17725 deadline Check if there is a deadline
17726 notdeadline Check if there is no deadline
17727 regexp Check if regexp matches
17728 notregexp Check if regexp does not match.
17730 The regexp is taken from the conditions list, it must com right after the
17731 `regexp' of `notregexp' element.
17733 If any of these conditions is met, this function returns the end point of
17734 the entity, causing the search to continue from there. This is a function
17735 that can be put into `org-agenda-skip-function' for the duration of a command."
17737 (org-back-to-heading t
)
17740 (progn (org-end-of-subtree t
) (point))
17741 (progn (outline-next-heading) (1- (point)))))
17745 (and (memq 'scheduled conditions
)
17746 (re-search-forward org-scheduled-time-regexp end t
))
17747 (and (memq 'notscheduled conditions
)
17748 (not (re-search-forward org-scheduled-time-regexp end t
)))
17749 (and (memq 'deadline conditions
)
17750 (re-search-forward org-deadline-time-regexp end t
))
17751 (and (memq 'notdeadline conditions
)
17752 (not (re-search-forward org-deadline-time-regexp end t
)))
17753 (and (setq m
(memq 'regexp conditions
))
17754 (stringp (setq r
(nth 1 m
)))
17755 (re-search-forward (nth 1 m
) end t
))
17756 (and (setq m
(memq 'notregexp conditions
))
17757 (stringp (setq r
(nth 1 m
)))
17758 (not (re-search-forward (nth 1 m
) end t
))))
17761 (defun org-agenda-list-stuck-projects (&rest ignore
)
17762 "Create agenda view for projects that are stuck.
17763 Stuck projects are project that have no next actions. For the definitions
17764 of what a project is and how to check if it stuck, customize the variable
17765 `org-stuck-projects'.
17766 MATCH is being ignored."
17768 (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches
)
17769 ;; FIXME: we could have used org-agenda-skip-if here.
17770 (org-agenda-overriding-header "List of stuck projects: ")
17771 (matcher (nth 0 org-stuck-projects
))
17772 (todo (nth 1 org-stuck-projects
))
17773 (todo-wds (if (member "*" todo
)
17775 (org-prepare-agenda-buffers (org-agenda-files))
17777 org-done-keywords-for-agenda
17778 (copy-sequence org-todo-keywords-for-agenda
)))
17780 (todo-re (concat "^\\*+[ \t]+\\("
17781 (mapconcat 'identity todo-wds
"\\|")
17783 (tags (nth 2 org-stuck-projects
))
17784 (tags-re (if (member "*" tags
)
17785 (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
17786 (concat "^\\*+ .*:\\("
17787 (mapconcat 'identity tags
"\\|")
17788 (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
17789 (gen-re (nth 3 org-stuck-projects
))
17795 (and gen-re
(stringp gen-re
) (string-match "\\S-" gen-re
)
17797 (setq org-agenda-skip-regexp
17799 (mapconcat 'identity re-list
"\\|")
17800 (error "No information how to identify unstuck projects")))
17801 (org-tags-view nil matcher
)
17802 (with-current-buffer org-agenda-buffer-name
17803 (setq org-agenda-redo-command
17804 '(org-agenda-list-stuck-projects
17805 (or current-prefix-arg org-last-arg
))))))
17807 ;;; Diary integration
17809 (defvar org-disable-agenda-to-diary nil
) ;Dynamically-scoped param.
17811 (defun org-get-entries-from-diary (date)
17812 "Get the (Emacs Calendar) diary entries for DATE."
17813 (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
17814 (diary-display-hook '(fancy-diary-display))
17815 (list-diary-entries-hook
17816 (cons 'org-diary-default-entry list-diary-entries-hook
))
17817 (diary-file-name-prefix-function nil
) ; turn this feature off
17818 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string
)
17820 (org-disable-agenda-to-diary t
))
17822 (save-window-excursion
17823 (list-diary-entries date
1))) ;; Keep this name for now, compatibility
17824 (if (not (get-buffer fancy-diary-buffer
))
17826 (with-current-buffer fancy-diary-buffer
17827 (setq buffer-read-only nil
)
17828 (if (zerop (buffer-size))
17831 ;; Omit the date and other unnecessary stuff
17832 (org-agenda-cleanup-fancy-diary)
17833 ;; Add prefix to each line and extend the text properties
17834 (if (zerop (buffer-size))
17836 (setq entries
(buffer-substring (point-min) (- (point-max) 1)))))
17837 (set-buffer-modified-p nil
)
17838 (kill-buffer fancy-diary-buffer
)))
17840 (setq entries
(org-split-string entries
"\n"))
17844 (setq x
(org-format-agenda-item "" x
"Diary" nil
'time
))
17845 ;; Extend the text properties to the beginning of the line
17846 (org-add-props x
(text-properties-at (1- (length x
)) x
)
17847 'type
"diary" 'date date
))
17850 (defun org-agenda-cleanup-fancy-diary ()
17851 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
17852 This gets rid of the date, the underline under the date, and
17853 the dummy entry installed by `org-mode' to ensure non-empty diary for each
17854 date. It also removes lines that contain only whitespace."
17855 (goto-char (point-min))
17856 (if (looking-at ".*?:[ \t]*")
17859 (re-search-forward "\n=+$" nil t
)
17861 (while (re-search-backward "^ +\n?" nil t
) (replace-match "")))
17862 (re-search-forward "\n=+$" nil t
)
17863 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
17864 (goto-char (point-min))
17865 (while (re-search-forward "^ +\n" nil t
)
17866 (replace-match ""))
17867 (goto-char (point-min))
17868 (if (re-search-forward "^Org-mode dummy\n?" nil t
)
17869 (replace-match "")))
17871 ;; Make sure entries from the diary have the right text properties.
17872 (eval-after-load "diary-lib"
17873 '(if (boundp 'diary-modify-entry-list-string-function
)
17874 ;; We can rely on the hook, nothing to do
17876 ;; Hook not avaiable, must use advice to make this work
17877 (defadvice add-to-diary-list
(before org-mark-diary-entry activate
)
17878 "Make the position visible."
17879 (if (and org-disable-agenda-to-diary
;; called from org-agenda
17882 (setq string
(org-modify-diary-entry-string string
))))))
17884 (defun org-modify-diary-entry-string (string)
17885 "Add text properties to string, allowing org-mode to act on it."
17886 (org-add-props string nil
17887 'mouse-face
'highlight
17888 'keymap org-agenda-keymap
17889 'help-echo
(if buffer-file-name
17890 (format "mouse-2 or RET jump to diary file %s"
17891 (abbreviate-file-name buffer-file-name
))
17893 'org-agenda-diary-link t
17894 'org-marker
(org-agenda-new-marker (point-at-bol))))
17896 (defun org-diary-default-entry ()
17897 "Add a dummy entry to the diary.
17898 Needed to avoid empty dates which mess up holiday display."
17899 ;; Catch the error if dealing with the new add-to-diary-alist
17900 (when org-disable-agenda-to-diary
17901 (condition-case nil
17902 (add-to-diary-list original-date
"Org-mode dummy" "")
17904 (add-to-diary-list original-date
"Org-mode dummy" "" nil
)))))
17907 (defun org-diary (&rest args
)
17908 "Return diary information from org-files.
17909 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
17910 It accesses org files and extracts information from those files to be
17911 listed in the diary. The function accepts arguments specifying what
17912 items should be listed. The following arguments are allowed:
17914 :timestamp List the headlines of items containing a date stamp or
17915 date range matching the selected date. Deadlines will
17916 also be listed, on the expiration day.
17920 :deadline List any deadlines past due, or due within
17921 `org-deadline-warning-days'. The listing occurs only
17922 in the diary for *today*, not at any other date. If
17923 an entry is marked DONE, it is no longer listed.
17925 :scheduled List all items which are scheduled for the given date.
17926 The diary for *today* also contains items which were
17927 scheduled earlier and are not yet marked DONE.
17929 :todo List all TODO items from the org-file. This may be a
17930 long list - so this is not turned on by default.
17931 Like deadlines, these entries only show up in the
17932 diary for *today*, not at any other date.
17934 The call in the diary file should look like this:
17936 &%%(org-diary) ~/path/to/some/orgfile.org
17938 Use a separate line for each org file to check. Or, if you omit the file name,
17939 all files listed in `org-agenda-files' will be checked automatically:
17943 If you don't give any arguments (as in the example above), the default
17944 arguments (:deadline :scheduled :timestamp :sexp) are used.
17945 So the example above may also be written as
17947 &%%(org-diary :deadline :timestamp :sexp :scheduled)
17949 The function expects the lisp variables `entry' and `date' to be provided
17950 by the caller, because this is how the calendar works. Don't use this
17951 function from a program - use `org-agenda-get-day-entries' instead."
17952 (org-agenda-maybe-reset-markers)
17953 (org-compile-prefix-format 'agenda
)
17954 (org-set-sorting-strategy 'agenda
)
17955 (setq args
(or args
'(:deadline
:scheduled
:timestamp
:sexp
)))
17956 (let* ((files (if (and entry
(stringp entry
) (string-match "\\S-" entry
))
17958 (org-agenda-files t
)))
17960 (org-prepare-agenda-buffers files
)
17961 ;; If this is called during org-agenda, don't return any entries to
17962 ;; the calendar. Org Agenda will list these entries itself.
17963 (if org-disable-agenda-to-diary
(setq files nil
))
17964 (while (setq file
(pop files
))
17965 (setq rtn
(apply 'org-agenda-get-day-entries file date args
))
17966 (setq results
(append results rtn
)))
17968 (concat (org-finalize-agenda-entries results
) "\n"))))
17970 ;;; Agenda entry finders
17972 (defun org-agenda-get-day-entries (file date
&rest args
)
17973 "Does the work for `org-diary' and `org-agenda'.
17974 FILE is the path to a file to be checked for entries. DATE is date like
17975 the one returned by `calendar-current-date'. ARGS are symbols indicating
17976 which kind of entries should be extracted. For details about these, see
17977 the documentation of `org-diary'."
17978 (setq args
(or args
'(:deadline
:scheduled
:timestamp
:sexp
)))
17979 (let* ((org-startup-folded nil
)
17980 (org-startup-align-all-tables nil
)
17981 (buffer (if (file-exists-p file
)
17982 (org-get-agenda-file-buffer file
)
17983 (error "No such file %s" file
)))
17986 ;; If file does not exist, make sure an error message ends up in diary
17987 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file
))
17988 (with-current-buffer buffer
17989 (unless (org-mode-p)
17990 (error "Agenda file %s is not in `org-mode'" file
))
17991 (setq org-category-table
(org-get-category-table))
17992 (let ((case-fold-search nil
))
17995 (if org-agenda-restrict
17996 (narrow-to-region org-agenda-restrict-begin
17997 org-agenda-restrict-end
)
17999 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
18000 (while (setq arg
(pop args
))
18002 ((and (eq arg
:todo
)
18003 (equal date
(calendar-current-date)))
18004 (setq rtn
(org-agenda-get-todos))
18005 (setq results
(append results rtn
)))
18006 ((eq arg
:timestamp
)
18007 (setq rtn
(org-agenda-get-blocks))
18008 (setq results
(append results rtn
))
18009 (setq rtn
(org-agenda-get-timestamps))
18010 (setq results
(append results rtn
)))
18012 (setq rtn
(org-agenda-get-sexps))
18013 (setq results
(append results rtn
)))
18014 ((eq arg
:scheduled
)
18015 (setq rtn
(org-agenda-get-scheduled))
18016 (setq results
(append results rtn
)))
18018 (setq rtn
(org-agenda-get-closed))
18019 (setq results
(append results rtn
)))
18020 ((eq arg
:deadline
)
18021 (setq rtn
(org-agenda-get-deadlines))
18022 (setq results
(append results rtn
))))))))
18025 ;; FIXME: this works only if the cursor is not at the
18026 ;; beginning of the entry
18027 (defun org-entry-is-done-p ()
18028 "Is the current entry marked DONE?"
18030 (and (re-search-backward "[\r\n]\\*+ " nil t
)
18031 (looking-at org-nl-done-regexp
))))
18033 (defun org-at-date-range-p (&optional inactive-ok
)
18034 "Is the cursor inside a date range?"
18038 (let ((pos (point)))
18039 (skip-chars-backward "^[<\r\n")
18040 (skip-chars-backward "<[")
18041 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp
))
18042 (>= (match-end 0) pos
)
18044 (skip-chars-backward "^<[\r\n")
18045 (skip-chars-backward "<[")
18046 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp
))
18047 (>= (match-end 0) pos
)
18051 (defun org-agenda-get-todos ()
18052 "Return the TODO information for agenda display."
18053 (let* ((props (list 'face nil
18054 'done-face
'org-done
18055 'org-not-done-regexp org-not-done-regexp
18056 'org-todo-regexp org-todo-regexp
18057 'mouse-face
'highlight
18058 'keymap org-agenda-keymap
18060 (format "mouse-2 or RET jump to org file %s"
18061 (abbreviate-file-name buffer-file-name
))))
18062 ;; FIXME: get rid of the \n at some point but watch out
18063 (regexp (concat "^\\*+[ \t]+\\("
18064 (if org-select-this-todo-keyword
18065 (if (equal org-select-this-todo-keyword
"*")
18068 (mapconcat 'identity
(org-split-string org-select-this-todo-keyword
"|") "\\|")
18070 org-not-done-regexp
)
18072 marker priority category tags
18074 (goto-char (point-min))
18075 (while (re-search-forward regexp nil t
)
18078 (beginning-of-line)
18079 (setq beg
(point) end
(progn (outline-next-heading) (point)))
18080 (when (or (and org-agenda-todo-ignore-scheduled
(goto-char beg
)
18081 (re-search-forward org-scheduled-time-regexp end t
))
18082 (and org-agenda-todo-ignore-deadlines
(goto-char beg
)
18083 (re-search-forward org-deadline-time-regexp end t
)
18084 (org-deadline-close (match-string 1))))
18086 (or org-agenda-todo-list-sublevels
(org-end-of-subtree 'invisible
))
18087 (throw :skip nil
)))
18090 (goto-char (match-beginning 1))
18091 (setq marker
(org-agenda-new-marker (match-beginning 0))
18092 category
(org-get-category)
18093 tags
(org-get-tags-at (point))
18094 txt
(org-format-agenda-item "" (match-string 1) category tags
)
18095 priority
(1+ (org-get-priority txt
)))
18096 (org-add-props txt props
18097 'org-marker marker
'org-hd-marker marker
18098 'priority priority
'org-category category
18101 (if org-agenda-todo-list-sublevels
18102 (goto-char (match-end 1))
18103 (org-end-of-subtree 'invisible
))))
18106 (defconst org-agenda-no-heading-message
18107 "No heading for this item in buffer or region.")
18109 (defun org-agenda-get-timestamps ()
18110 "Return the date stamp information for agenda display."
18111 (let* ((props (list 'face nil
18112 'org-not-done-regexp org-not-done-regexp
18113 'org-todo-regexp org-todo-regexp
18114 'mouse-face
'highlight
18115 'keymap org-agenda-keymap
18117 (format "mouse-2 or RET jump to org file %s"
18118 (abbreviate-file-name buffer-file-name
))))
18119 (d1 (calendar-absolute-from-gregorian date
))
18124 (format-time-string
18125 (car org-time-stamp-formats
)
18126 (apply 'encode-time
; DATE bound by calendar
18127 (list 0 0 0 (nth 1 date
) (car date
) (nth 2 date
))))
18129 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
18130 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
18131 marker hdmarker deadlinep scheduledp donep tmp priority category
18132 ee txt timestr tags b0 b3 e3
)
18133 (goto-char (point-min))
18134 (while (re-search-forward regexp nil t
)
18135 (setq b0
(match-beginning 0)
18136 b3
(match-beginning 3) e3
(match-end 3))
18138 (and (org-at-date-range-p) (throw :skip nil
))
18140 (if (and (match-end 1)
18141 (not (= d1
(org-time-string-to-absolute (match-string 1) d1
))))
18144 (not (org-diary-sexp-entry (buffer-substring b3 e3
) "" date
)))
18146 (setq marker
(org-agenda-new-marker b0
)
18147 category
(org-get-category b0
)
18148 tmp
(buffer-substring (max (point-min)
18149 (- b0 org-ds-keyword-length
))
18151 timestr
(if b3
"" (buffer-substring b0
(point-at-eol)))
18152 deadlinep
(string-match org-deadline-regexp tmp
)
18153 scheduledp
(string-match org-scheduled-regexp tmp
)
18154 donep
(org-entry-is-done-p))
18155 (if (or scheduledp deadlinep
) (throw :skip t
))
18156 (if (string-match ">" timestr
)
18157 ;; substring should only run to end of time stamp
18158 (setq timestr
(substring timestr
0 (match-end 0))))
18160 (if (re-search-backward "^\\*+ " nil t
)
18162 (goto-char (match-beginning 0))
18163 (setq hdmarker
(org-agenda-new-marker)
18164 tags
(org-get-tags-at))
18165 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
18166 (setq txt
(org-format-agenda-item
18167 nil
(match-string 1) category tags timestr
)))
18168 (setq txt org-agenda-no-heading-message
))
18169 (setq priority
(org-get-priority txt
))
18170 (org-add-props txt props
18171 'org-marker marker
'org-hd-marker hdmarker
)
18172 (org-add-props txt nil
'priority priority
18173 'org-category category
'date date
18176 (outline-next-heading)))
18179 (defun org-agenda-get-sexps ()
18180 "Return the sexp information for agenda display."
18181 (require 'diary-lib
)
18182 (let* ((props (list 'face nil
18183 'mouse-face
'highlight
18184 'keymap org-agenda-keymap
18186 (format "mouse-2 or RET jump to org file %s"
18187 (abbreviate-file-name buffer-file-name
))))
18189 marker category ee txt tags entry result beg b sexp sexp-entry
)
18190 (goto-char (point-min))
18191 (while (re-search-forward regexp nil t
)
18194 (setq beg
(match-beginning 0))
18195 (goto-char (1- (match-end 0)))
18198 (setq sexp
(buffer-substring b
(point)))
18199 (setq sexp-entry
(if (looking-at "[ \t]*\\(\\S-.*\\)")
18200 (org-trim (match-string 1))
18202 (setq result
(org-diary-sexp-entry sexp sexp-entry date
))
18204 (setq marker
(org-agenda-new-marker beg
)
18205 category
(org-get-category beg
))
18207 (if (string-match "\\S-" result
)
18209 (setq txt
"SEXP entry returned empty string"))
18211 (setq txt
(org-format-agenda-item
18212 "" txt category tags
'time
))
18213 (org-add-props txt props
'org-marker marker
)
18214 (org-add-props txt nil
18215 'org-category category
'date date
18220 (defun org-agenda-get-closed ()
18221 "Return the logged TODO entries for agenda display."
18222 (let* ((props (list 'mouse-face
'highlight
18223 'org-not-done-regexp org-not-done-regexp
18224 'org-todo-regexp org-todo-regexp
18225 'keymap org-agenda-keymap
18227 (format "mouse-2 or RET jump to org file %s"
18228 (abbreviate-file-name buffer-file-name
))))
18230 "\\<\\(" org-closed-string
"\\|" org-clock-string
"\\) *\\["
18233 (format-time-string
18234 (car org-time-stamp-formats
)
18235 (apply 'encode-time
; DATE bound by calendar
18236 (list 0 0 0 (nth 1 date
) (car date
) (nth 2 date
))))
18238 marker hdmarker priority category tags closedp
18240 (goto-char (point-min))
18241 (while (re-search-forward regexp nil t
)
18244 (setq marker
(org-agenda-new-marker (match-beginning 0))
18245 closedp
(equal (match-string 1) org-closed-string
)
18246 category
(org-get-category (match-beginning 0))
18247 timestr
(buffer-substring (match-beginning 0) (point-at-eol))
18248 ;; donep (org-entry-is-done-p)
18250 (if (string-match "\\]" timestr
)
18251 ;; substring should only run to end of time stamp
18252 (setq timestr
(substring timestr
0 (match-end 0))))
18254 (if (re-search-backward "^\\*+ " nil t
)
18256 (goto-char (match-beginning 0))
18257 (setq hdmarker
(org-agenda-new-marker)
18258 tags
(org-get-tags-at))
18259 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
18260 (setq txt
(org-format-agenda-item
18261 (if closedp
"Closed: " "Clocked: ")
18262 (match-string 1) category tags timestr
)))
18263 (setq txt org-agenda-no-heading-message
))
18264 (setq priority
100000)
18265 (org-add-props txt props
18266 'org-marker marker
'org-hd-marker hdmarker
'face
'org-done
18267 'priority priority
'org-category category
18268 'type
"closed" 'date date
18269 'undone-face
'org-warning
'done-face
'org-done
)
18271 (outline-next-heading)))
18274 (defun org-agenda-get-deadlines ()
18275 "Return the deadline information for agenda display."
18276 (let* ((props (list 'mouse-face
'highlight
18277 'org-not-done-regexp org-not-done-regexp
18278 'org-todo-regexp org-todo-regexp
18279 'keymap org-agenda-keymap
18281 (format "mouse-2 or RET jump to org file %s"
18282 (abbreviate-file-name buffer-file-name
))))
18283 (regexp org-deadline-time-regexp
)
18284 (todayp (equal date
(calendar-current-date))) ; DATE bound by calendar
18285 (d1 (calendar-absolute-from-gregorian date
)) ; DATE bound by calendar
18286 d2 diff dfrac wdays pos pos1 category tags
18287 ee txt head face s upcomingp donep timestr
)
18288 (goto-char (point-min))
18289 (while (re-search-forward regexp nil t
)
18292 (setq s
(match-string 1)
18293 pos
(1- (match-beginning 1))
18294 d2
(org-time-string-to-absolute (match-string 1) d1
)
18296 (if (string-match "-\\([0-9]+\\)\\([dwmy]\\)\\'" s
)
18299 (* (string-to-number (match-string 1 s
))
18300 (cdr (assoc (match-string 2 s
)
18301 '(("d" .
1) ("w" .
7)
18302 ("m" .
30.4) ("y" .
365.25)))))))
18303 (setq wdays org-deadline-warning-days
))
18304 (setq dfrac
(/ (* 1.0 (- wdays diff
)) wdays
))
18305 (setq upcomingp
(and todayp
(> diff
0)))
18306 ;; When to show a deadline in the calendar:
18307 ;; If the expiration is within wdays warning time.
18308 ;; Past-due deadlines are only shown on the current date
18309 (if (or (and (<= diff wdays
) todayp
)
18312 (setq category
(org-get-category))
18313 (if (re-search-backward "^\\*+[ \t]+" nil t
)
18315 (goto-char (match-end 0))
18316 (setq pos1
(match-beginning 0))
18317 (setq tags
(org-get-tags-at pos1
))
18318 (setq head
(buffer-substring-no-properties
18320 (progn (skip-chars-forward "^\r\n")
18322 (setq donep
(string-match org-looking-at-done-regexp head
))
18323 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s
)
18324 (setq timestr
(concat (match-string 1 s
) " "))
18325 (setq timestr nil
))
18327 (or org-agenda-skip-deadline-if-done
18330 (setq txt
(org-format-agenda-item
18333 (format "In %3d d.: " diff
))
18334 head category tags timestr
))))
18335 (setq txt org-agenda-no-heading-message
))
18337 (setq face
(org-agenda-deadline-face dfrac
))
18338 (org-add-props txt props
18339 'org-marker
(org-agenda-new-marker pos
)
18340 'org-hd-marker
(org-agenda-new-marker pos1
)
18341 'priority
(+ (if upcomingp
(floor (* dfrac
10.
)) 100)
18342 (org-get-priority txt
))
18343 'org-category category
18344 'type
(if upcomingp
"upcoming-deadline" "deadline")
18345 'date
(if upcomingp date d2
)
18346 'face
(if donep
'org-done face
)
18347 'undone-face face
'done-face
'org-done
)
18351 (defun org-agenda-deadline-face (fraction)
18352 "Return the face to displaying a deadline item.
18353 FRACTION is what fraction of the head-warning time has passed."
18354 (let ((faces org-agenda-deadline-faces
) f
)
18356 (while (setq f
(pop faces
))
18357 (if (>= fraction
(car f
)) (throw 'exit
(cdr f
)))))))
18359 (defun org-agenda-get-scheduled ()
18360 "Return the scheduled information for agenda display."
18361 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
18362 'org-todo-regexp org-todo-regexp
18363 'done-face
'org-done
18364 'mouse-face
'highlight
18365 'keymap org-agenda-keymap
18367 (format "mouse-2 or RET jump to org file %s"
18368 (abbreviate-file-name buffer-file-name
))))
18369 (regexp org-scheduled-time-regexp
)
18370 (todayp (equal date
(calendar-current-date))) ; DATE bound by calendar
18371 (d1 (calendar-absolute-from-gregorian date
)) ; DATE bound by calendar
18372 d2 diff pos pos1 category tags
18373 ee txt head pastschedp donep face timestr s
)
18374 (goto-char (point-min))
18375 (while (re-search-forward regexp nil t
)
18378 (setq s
(match-string 1)
18379 pos
(1- (match-beginning 1))
18380 d2
(org-time-string-to-absolute (match-string 1) d1
)
18382 (setq pastschedp
(and todayp
(< diff
0)))
18383 ;; When to show a scheduled item in the calendar:
18384 ;; If it is on or past the date.
18385 (if (or (and (< diff
0) todayp
)
18388 (setq category
(org-get-category))
18389 (if (re-search-backward "^\\*+[ \t]+" nil t
)
18391 (goto-char (match-end 0))
18392 (setq pos1
(match-beginning 0))
18393 (setq tags
(org-get-tags-at))
18394 (setq head
(buffer-substring-no-properties
18396 (progn (skip-chars-forward "^\r\n") (point))))
18397 (setq donep
(string-match org-looking-at-done-regexp head
))
18398 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s
)
18399 (setq timestr
(concat (match-string 1 s
) " "))
18400 (setq timestr nil
))
18402 (or org-agenda-skip-scheduled-if-done
18405 (setq txt
(org-format-agenda-item
18408 (format "Sched.%2dx: " (- 1 diff
)))
18409 head category tags timestr
))))
18410 (setq txt org-agenda-no-heading-message
))
18412 (setq face
(if pastschedp
18413 'org-scheduled-previously
18414 'org-scheduled-today
))
18415 (org-add-props txt props
18417 'face
(if donep
'org-done face
)
18418 'org-marker
(org-agenda-new-marker pos
)
18419 'org-hd-marker
(org-agenda-new-marker pos1
)
18420 'type
(if pastschedp
"past-scheduled" "scheduled")
18421 'date
(if pastschedp d2 date
)
18422 'priority
(+ (- 5 diff
) (org-get-priority txt
))
18423 'org-category category
)
18427 (defun org-agenda-get-blocks ()
18428 "Return the date-range information for agenda display."
18429 (let* ((props (list 'face nil
18430 'org-not-done-regexp org-not-done-regexp
18431 'org-todo-regexp org-todo-regexp
18432 'mouse-face
'highlight
18433 'keymap org-agenda-keymap
18435 (format "mouse-2 or RET jump to org file %s"
18436 (abbreviate-file-name buffer-file-name
))))
18437 (regexp org-tr-regexp
)
18438 (d0 (calendar-absolute-from-gregorian date
))
18439 marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos
)
18440 (goto-char (point-min))
18441 (while (re-search-forward regexp nil t
)
18445 (setq timestr
(match-string 0)
18446 s1
(match-string 1)
18447 s2
(match-string 2)
18448 d1
(time-to-days (org-time-string-to-time s1
))
18449 d2
(time-to-days (org-time-string-to-time s2
)))
18450 (if (and (> (- d0 d1
) -
1) (> (- d2 d0
) -
1))
18451 ;; Only allow days between the limits, because the normal
18452 ;; date stamps will catch the limits.
18454 (setq marker
(org-agenda-new-marker (point)))
18455 (setq category
(org-get-category))
18456 (if (re-search-backward "^\\*+ " nil t
)
18458 (goto-char (match-beginning 0))
18459 (setq hdmarker
(org-agenda-new-marker (point)))
18460 (setq tags
(org-get-tags-at))
18461 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
18462 (setq txt
(org-format-agenda-item
18463 (format (if (= d1 d2
) "" "(%d/%d): ")
18464 (1+ (- d0 d1
)) (1+ (- d2 d1
)))
18465 (match-string 1) category tags
18466 (if (= d0 d1
) timestr
))))
18467 (setq txt org-agenda-no-heading-message
))
18468 (org-add-props txt props
18469 'org-marker marker
'org-hd-marker hdmarker
18470 'type
"block" 'date date
18471 'priority
(org-get-priority txt
) 'org-category category
)
18474 ;; Sort the entries by expiration date.
18477 ;;; Agenda presentation and sorting
18479 (defconst org-plain-time-of-day-regexp
18481 "\\(\\<[012]?[0-9]"
18482 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
18484 "\\(\\<[012]?[0-9]"
18485 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
18487 "Regular expression to match a plain time or time range.
18488 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
18489 groups carry important information:
18491 1 the first time, range or not
18492 8 the second time, if it is a range.")
18494 (defconst org-stamp-time-of-day-regexp
18496 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
18497 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
18499 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
18500 "Regular expression to match a timestamp time or time range.
18501 After a match, the following groups carry important information:
18503 1 date plus weekday, for backreferencing to make sure both times on same day
18504 2 the first time, range or not
18505 4 the second time, if it is a range.")
18507 (defvar org-prefix-has-time nil
18508 "A flag, set by `org-compile-prefix-format'.
18509 The flag is set if the currently compiled format contains a `%t'.")
18510 (defvar org-prefix-has-tag nil
18511 "A flag, set by `org-compile-prefix-format'.
18512 The flag is set if the currently compiled format contains a `%T'.")
18514 (defun org-format-agenda-item (extra txt
&optional category tags dotime
18516 "Format TXT to be inserted into the agenda buffer.
18517 In particular, it adds the prefix and corresponding text properties. EXTRA
18518 must be a string and replaces the `%s' specifier in the prefix format.
18519 CATEGORY (string, symbol or nil) may be used to overrule the default
18520 category taken from local variable or file name. It will replace the `%c'
18521 specifier in the format. DOTIME, when non-nil, indicates that a
18522 time-of-day should be extracted from TXT for sorting of this entry, and for
18523 the `%t' specifier in the format. When DOTIME is a string, this string is
18524 searched for a time before TXT is. NOPREFIX is a flag and indicates that
18525 only the correctly processes TXT should be returned - this is used by
18526 `org-agenda-change-all-lines'. TAGS can be the tags of the headline."
18528 ;; Diary entries sometimes have extra whitespace at the beginning
18529 (if (string-match "^ +" txt
) (setq txt
(replace-match "" nil nil txt
)))
18530 (let* ((category (or category
18532 (if buffer-file-name
18533 (file-name-sans-extension
18534 (file-name-nondirectory buffer-file-name
))
18536 (tag (if tags
(nth (1- (length tags
)) tags
) ""))
18537 time
; time and tag are needed for the eval of the prefix format
18538 (ts (if dotime
(concat (if (stringp dotime
) dotime
"") txt
)))
18539 (time-of-day (and dotime
(org-get-time-of-day ts
)))
18540 stamp plain s0 s1 s2 rtn srp
)
18541 (when (and dotime time-of-day org-prefix-has-time
)
18542 ;; Extract starting and ending time and move them to prefix
18543 (when (or (setq stamp
(string-match org-stamp-time-of-day-regexp ts
))
18544 (setq plain
(string-match org-plain-time-of-day-regexp ts
)))
18545 (setq s0
(match-string 0 ts
)
18546 srp
(and stamp
(match-end 3))
18547 s1
(match-string (if plain
1 2) ts
)
18548 s2
(match-string (if plain
8 (if srp
4 6)) ts
))
18550 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
18551 ;; them, we might want to remove them there to avoid duplication.
18552 ;; The user can turn this off with a variable.
18553 (if (and org-agenda-remove-times-when-in-prefix
(or stamp plain
)
18554 (string-match (concat (regexp-quote s0
) " *") txt
)
18555 (if (eq org-agenda-remove-times-when-in-prefix
'beg
)
18556 (= (match-beginning 0) 0)
18558 (setq txt
(replace-match "" nil nil txt
))))
18559 ;; Normalize the time(s) to 24 hour
18560 (if s1
(setq s1
(org-get-time-of-day s1
'string t
)))
18561 (if s2
(setq s2
(org-get-time-of-day s2
'string t
))))
18563 (when (and s1
(not s2
) org-agenda-default-appointment-duration
18564 (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1
))
18565 (let ((m (+ (string-to-number (match-string 2 s1
))
18566 (* 60 (string-to-number (match-string 1 s1
)))
18567 org-agenda-default-appointment-duration
))
18569 (setq h
(/ m
60) m
(- m
(* h
60)))
18570 (setq s2
(format "%02d:%02d" h m
))))
18572 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
18574 ;; Tags are in the string
18575 (if (or (eq org-agenda-remove-tags t
)
18576 (and org-agenda-remove-tags
18577 org-prefix-has-tag
))
18578 (setq txt
(replace-match "" t t txt
))
18579 (setq txt
(replace-match
18580 (concat (make-string (max (- 50 (length txt
)) 1) ?\
)
18581 (match-string 2 txt
))
18584 ;; Create the final string
18587 ;; Prepare the variables needed in the eval of the compiled format
18588 (setq time
(cond (s2 (concat s1
"-" s2
))
18589 (s1 (concat s1
"......"))
18591 extra
(or extra
"")
18592 category
(if (symbolp category
) (symbol-name category
) category
))
18593 ;; Evaluate the compiled format
18594 (setq rtn
(concat (eval org-prefix-format-compiled
) txt
)))
18596 ;; And finally add the text properties
18597 (org-add-props rtn nil
18598 'org-category
(downcase category
) 'tags tags
18599 'prefix-length
(- (length rtn
) (length txt
))
18600 'time-of-day time-of-day
18606 (defvar org-agenda-sorting-strategy
) ;; FIXME: can be removed?
18607 (defvar org-agenda-sorting-strategy-selected nil
)
18609 (defun org-agenda-add-time-grid-maybe (list ndays todayp
)
18611 (cond ((not org-agenda-use-time-grid
) (throw 'exit list
))
18612 ((and todayp
(member 'today
(car org-agenda-time-grid
))))
18613 ((and (= ndays
1) (member 'daily
(car org-agenda-time-grid
))))
18614 ((member 'weekly
(car org-agenda-time-grid
)))
18615 (t (throw 'exit list
)))
18616 (let* ((have (delq nil
(mapcar
18617 (lambda (x) (get-text-property 1 'time-of-day x
))
18619 (string (nth 1 org-agenda-time-grid
))
18620 (gridtimes (nth 2 org-agenda-time-grid
))
18621 (req (car org-agenda-time-grid
))
18622 (remove (member 'remove-match req
))
18624 (if (and (member 'require-timed req
) (not have
))
18625 ;; don't show empty grid
18626 (throw 'exit list
))
18627 (while (setq time
(pop gridtimes
))
18628 (unless (and remove
(member time have
))
18629 (setq time
(int-to-string time
))
18630 (push (org-format-agenda-item
18632 (concat (substring time
0 -
2) ":" (substring time -
2)))
18635 1 (length (car new
)) 'face
'org-time-grid
(car new
))))
18636 (if (member 'time-up org-agenda-sorting-strategy-selected
)
18638 (append list new
)))))
18640 (defun org-compile-prefix-format (key)
18641 "Compile the prefix format into a Lisp form that can be evaluated.
18642 The resulting form is returned and stored in the variable
18643 `org-prefix-format-compiled'."
18644 (setq org-prefix-has-time nil org-prefix-has-tag nil
)
18646 ((stringp org-agenda-prefix-format
)
18647 org-agenda-prefix-format
)
18648 ((assq key org-agenda-prefix-format
)
18649 (cdr (assq key org-agenda-prefix-format
)))
18650 (t " %-12:c%?-12t% s")))
18652 varform vars var e c f opt
)
18653 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
18655 (setq var
(cdr (assoc (match-string 4 s
)
18656 '(("c" . category
) ("t" . time
) ("s" . extra
)
18658 c
(or (match-string 3 s
) "")
18659 opt
(match-beginning 1)
18660 start
(1+ (match-beginning 0)))
18661 (if (equal var
'time
) (setq org-prefix-has-time t
))
18662 (if (equal var
'tag
) (setq org-prefix-has-tag t
))
18663 (setq f
(concat "%" (match-string 2 s
) "s"))
18666 `(if (equal "" ,var
)
18668 (format ,f
(if (equal "" ,var
) "" (concat ,var
,c
)))))
18669 (setq varform
`(format ,f
(if (equal ,var
"") "" (concat ,var
,c
)))))
18670 (setq s
(replace-match "%s" t nil s
))
18671 (push varform vars
))
18672 (setq vars
(nreverse vars
))
18673 (setq org-prefix-format-compiled
`(format ,s
,@vars
))))
18675 (defun org-set-sorting-strategy (key)
18676 (if (symbolp (car org-agenda-sorting-strategy
))
18678 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy
)
18679 (setq org-agenda-sorting-strategy-selected
18680 (or (cdr (assq key org-agenda-sorting-strategy
))
18681 (cdr (assq 'agenda org-agenda-sorting-strategy
))
18682 '(time-up category-keep priority-down
)))))
18684 (defun org-get-time-of-day (s &optional string mod24
)
18685 "Check string S for a time of day.
18686 If found, return it as a military time number between 0 and 2400.
18687 If not found, return nil.
18688 The optional STRING argument forces conversion into a 5 character wide string
18694 "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s
)
18696 "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s
))
18697 (let* ((h (string-to-number (match-string 1 s
)))
18698 (m (if (match-end 3) (string-to-number (match-string 3 s
)) 0))
18699 (ampm (if (match-end 4) (downcase (match-string 4 s
))))
18700 (am-p (equal ampm
"am"))
18701 (h1 (cond ((not ampm
) h
)
18702 ((= h
12) (if am-p
0 12))
18703 (t (+ h
(if am-p
0 12)))))
18704 (h2 (if (and string mod24
(not (and (= m
0) (= h1
24))))
18706 (t0 (+ (* 100 h2
) m
))
18707 (t1 (concat (if (>= h1
24) "+" " ")
18708 (if (< t0
100) "0" "")
18709 (if (< t0
10) "0" "")
18710 (int-to-string t0
))))
18711 (if string
(concat (substring t1 -
4 -
2) ":" (substring t1 -
2)) t0
)))))
18713 (defun org-finalize-agenda-entries (list &optional nosort
)
18714 "Sort and concatenate the agenda items."
18715 (setq list
(mapcar 'org-agenda-highlight-todo list
))
18718 (mapconcat 'identity
(sort list
'org-entries-lessp
) "\n")))
18720 (defun org-agenda-highlight-todo (x)
18724 (beginning-of-line 1)
18725 (setq re
(get-text-property (point) 'org-not-done-regexp
))
18726 (goto-char (+ (point) (or (get-text-property (point) 'prefix-length
) 0)))
18727 (and (looking-at (concat "[ \t]*\\.*" re
))
18728 (add-text-properties (match-beginning 0) (match-end 0)
18729 '(face org-todo
))))
18730 (setq re
(concat (get-text-property 0 'org-not-done-regexp x
))
18731 pl
(get-text-property 0 'prefix-length x
))
18732 (and re
(equal (string-match (concat "\\(\\.*\\)" re
) x
(or pl
0)) pl
)
18733 (add-text-properties (or (match-end 1) (match-end 0)) (match-end 0)
18734 '(face org-todo
) x
))
18737 (defsubst org-cmp-priority
(a b
)
18738 "Compare the priorities of string A and B."
18739 (let ((pa (or (get-text-property 1 'priority a
) 0))
18740 (pb (or (get-text-property 1 'priority b
) 0)))
18741 (cond ((> pa pb
) +1)
18745 (defsubst org-cmp-category
(a b
)
18746 "Compare the string values of categories of strings A and B."
18747 (let ((ca (or (get-text-property 1 'org-category a
) ""))
18748 (cb (or (get-text-property 1 'org-category b
) "")))
18749 (cond ((string-lessp ca cb
) -
1)
18750 ((string-lessp cb ca
) +1)
18753 (defsubst org-cmp-tag
(a b
)
18754 "Compare the string values of categories of strings A and B."
18755 (let ((ta (car (last (get-text-property 1 'tags a
))))
18756 (tb (car (last (get-text-property 1 'tags b
)))))
18757 (cond ((not ta
) +1)
18759 ((string-lessp ta tb
) -
1)
18760 ((string-lessp tb ta
) +1)
18763 (defsubst org-cmp-time
(a b
)
18764 "Compare the time-of-day values of strings A and B."
18765 (let* ((def (if org-sort-agenda-notime-is-late
9901 -
1))
18766 (ta (or (get-text-property 1 'time-of-day a
) def
))
18767 (tb (or (get-text-property 1 'time-of-day b
) def
)))
18768 (cond ((< ta tb
) -
1)
18772 (defun org-entries-lessp (a b
)
18773 "Predicate for sorting agenda entries."
18774 ;; The following variables will be used when the form is evaluated.
18775 ;; So even though the compiler complains, keep them.
18776 (let* ((time-up (org-cmp-time a b
))
18777 (time-down (if time-up
(- time-up
) nil
))
18778 (priority-up (org-cmp-priority a b
))
18779 (priority-down (if priority-up
(- priority-up
) nil
))
18780 (category-up (org-cmp-category a b
))
18781 (category-down (if category-up
(- category-up
) nil
))
18782 (category-keep (if category-up
+1 nil
))
18783 (tag-up (org-cmp-tag a b
))
18784 (tag-down (if tag-up
(- tag-up
) nil
)))
18786 (eval (cons 'or org-agenda-sorting-strategy-selected
))
18787 '((-1 . t
) (1 . nil
) (nil . nil
))))))
18789 ;;; Agenda commands
18791 (defun org-agenda-check-type (error &rest types
)
18792 "Check if agenda buffer is of allowed type.
18793 If ERROR is non-nil, throw an error, otherwise just return nil."
18794 (if (memq org-agenda-type types
)
18797 (error "Not allowed in %s-type agenda buffers" org-agenda-type
)
18800 (defun org-agenda-quit ()
18801 "Exit agenda by removing the window or the buffer."
18803 (let ((buf (current-buffer)))
18804 (if (not (one-window-p)) (delete-window))
18806 (org-agenda-maybe-reset-markers 'force
)
18807 (org-columns-remove-overlays))
18808 ;; Maybe restore the pre-agenda window configuration.
18809 (and org-agenda-restore-windows-after-quit
18810 (not (eq org-agenda-window-setup
'other-frame
))
18811 org-pre-agenda-window-conf
18812 (set-window-configuration org-pre-agenda-window-conf
)))
18814 (defun org-agenda-exit ()
18815 "Exit agenda by removing the window or the buffer.
18816 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
18817 Org-mode buffers visited directly by the user will not be touched."
18819 (org-release-buffers org-agenda-new-buffers
)
18820 (setq org-agenda-new-buffers nil
)
18823 (defun org-save-all-org-buffers ()
18824 "Save all Org-mode buffers without user confirmation."
18826 (message "Saving all Org-mode buffers...")
18827 (save-some-buffers t
'org-mode-p
)
18828 (message "Saving all Org-mode buffers... done"))
18830 (defun org-agenda-redo ()
18832 When this is the global TODO list, a prefix argument will be interpreted."
18834 (let* ((org-agenda-keep-modes t
)
18835 (line (org-current-line))
18836 (window-line (- line
(org-current-line (window-start))))
18837 (lprops (get 'org-agenda-redo-command
'org-lprops
)))
18838 (message "Rebuilding agenda buffer...")
18839 (org-let lprops
'(eval org-agenda-redo-command
))
18840 (setq org-agenda-undo-list nil
18841 org-agenda-pending-undo-list nil
)
18842 (message "Rebuilding agenda buffer...done")
18844 (recenter window-line
)))
18846 (defun org-agenda-goto-date (date)
18847 "Jump to DATE in agenda."
18848 (interactive (list (org-read-date)))
18849 (org-agenda-list nil date
))
18851 (defun org-agenda-goto-today ()
18854 (org-agenda-check-type t
'timeline
'agenda
)
18855 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t
)))
18857 (tdpos (goto-char tdpos
))
18858 ((eq org-agenda-type
'agenda
)
18859 (let* ((sd (time-to-days (current-time)))
18860 (comp (org-agenda-compute-time-span sd org-agenda-span
))
18861 (org-agenda-overriding-arguments org-agenda-last-arguments
))
18862 (setf (nth 1 org-agenda-overriding-arguments
) (car comp
))
18863 (setf (nth 2 org-agenda-overriding-arguments
) (cdr comp
))
18865 (org-agenda-find-today-or-agenda)))
18866 (t (error "Cannot find today")))))
18868 (defun org-agenda-find-today-or-agenda ()
18870 (or (text-property-any (point-min) (point-max) 'org-today t
)
18871 (text-property-any (point-min) (point-max) 'org-agenda-type
'agenda
)
18874 (defun org-agenda-later (arg)
18875 "Go forward in time by thee current span.
18876 With prefix ARG, go forward that many times the current span."
18878 (org-agenda-check-type t
'agenda
)
18879 (let* ((span org-agenda-span
)
18880 (sd org-starting-day
)
18881 (greg (calendar-gregorian-from-absolute sd
))
18885 (setq sd
(+ arg sd
) nd
1))
18887 (setq sd
(+ (* 7 arg
) sd
) nd
7))
18889 (setq greg2
(list (+ (car greg
) arg
) (nth 1 greg
) (nth 2 greg
))
18890 sd
(calendar-absolute-from-gregorian greg2
))
18891 (setcar greg2
(1+ (car greg2
)))
18892 (setq nd
(- (calendar-absolute-from-gregorian greg2
) sd
)))
18894 (setq greg2
(list (car greg
) (nth 1 greg
) (+ arg
(nth 2 greg
)))
18895 sd
(calendar-absolute-from-gregorian greg2
))
18896 (setcar (nthcdr 2 greg2
) (1+ (nth 2 greg2
)))
18897 (setq nd
(- (calendar-absolute-from-gregorian greg2
) sd
))))
18898 (let ((org-agenda-overriding-arguments
18899 (list (car org-agenda-last-arguments
) sd nd t
)))
18901 (org-agenda-find-today-or-agenda))))
18903 (defun org-agenda-earlier (arg)
18904 "Go backward in time by the current span.
18905 With prefix ARG, go backward that many times the current span."
18907 (org-agenda-later (- arg
)))
18909 (defun org-agenda-day-view ()
18910 "Switch to daily view for agenda."
18912 (setq org-agenda-ndays
1)
18913 (org-agenda-change-time-span 'day
))
18914 (defun org-agenda-week-view ()
18915 "Switch to daily view for agenda."
18917 (setq org-agenda-ndays
7)
18918 (org-agenda-change-time-span 'week
))
18919 (defun org-agenda-month-view ()
18920 "Switch to daily view for agenda."
18922 (org-agenda-change-time-span 'month
))
18923 (defun org-agenda-year-view ()
18924 "Switch to daily view for agenda."
18926 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
18927 (org-agenda-change-time-span 'year
)
18930 (defun org-agenda-change-time-span (span)
18931 "Change the agenda view to SPAN.
18932 SPAN may be `day', `week', `month', `year'."
18933 (org-agenda-check-type t
'agenda
)
18934 (if (equal org-agenda-span span
)
18935 (error "Viewing span is already \"%s\"" span
))
18936 (let* ((sd (or (get-text-property (point) 'day
)
18938 (computed (org-agenda-compute-time-span sd span
))
18939 (org-agenda-overriding-arguments
18940 (list (car org-agenda-last-arguments
)
18941 (car computed
) (cdr computed
) t
)))
18943 (org-agenda-find-today-or-agenda))
18944 (org-agenda-set-mode-name)
18945 (message "Switched to %s view" span
))
18947 (defun org-agenda-compute-time-span (sd span
)
18948 "Compute starting date and number of days for agenda.
18949 SPAN may be `day', `week', `month', `year'. The return value
18950 is a cons cell with the starting date and the number of days,
18951 so that the date SD will be in that range."
18952 (let* ((greg (calendar-gregorian-from-absolute sd
))
18958 (let* ((nt (calendar-day-of-week
18959 (calendar-gregorian-from-absolute sd
)))
18960 (d (if org-agenda-start-on-weekday
18961 (- nt org-agenda-start-on-weekday
)
18963 (setq sd
(- sd
(+ (if (< d
0) 7 0) d
)))
18966 (setq sd
(calendar-absolute-from-gregorian
18967 (list (car greg
) 1 (nth 2 greg
)))
18968 nd
(- (calendar-absolute-from-gregorian
18969 (list (1+ (car greg
)) 1 (nth 2 greg
)))
18972 (setq sd
(calendar-absolute-from-gregorian
18973 (list 1 1 (nth 2 greg
)))
18974 nd
(- (calendar-absolute-from-gregorian
18975 (list 1 1 (1+ (nth 2 greg
))))
18979 ;; FIXME: this no longer works if user make date format that starts with a blank
18980 (defun org-agenda-next-date-line (&optional arg
)
18981 "Jump to the next line indicating a date in agenda buffer."
18983 (org-agenda-check-type t
'agenda
'timeline
)
18984 (beginning-of-line 1)
18985 (if (looking-at "^\\S-") (forward-char 1))
18986 (if (not (re-search-forward "^\\S-" nil t arg
))
18989 (error "No next date after this line in this buffer")))
18990 (goto-char (match-beginning 0)))
18992 (defun org-agenda-previous-date-line (&optional arg
)
18993 "Jump to the previous line indicating a date in agenda buffer."
18995 (org-agenda-check-type t
'agenda
'timeline
)
18996 (beginning-of-line 1)
18997 (if (not (re-search-backward "^\\S-" nil t arg
))
18998 (error "No previous date before this line in this buffer")))
19000 ;; Initialize the highlight
19001 (defvar org-hl
(org-make-overlay 1 1))
19002 (org-overlay-put org-hl
'face
'highlight
)
19004 (defun org-highlight (begin end
&optional buffer
)
19005 "Highlight a region with overlay."
19006 (funcall (if (featurep 'xemacs
) 'set-extent-endpoints
'move-overlay
)
19007 org-hl begin end
(or buffer
(current-buffer))))
19009 (defun org-unhighlight ()
19010 "Detach overlay INDEX."
19011 (funcall (if (featurep 'xemacs
) 'detach-extent
'delete-overlay
) org-hl
))
19013 ;; FIXME this is currently not used.
19014 (defun org-highlight-until-next-command (beg end
&optional buffer
)
19015 (org-highlight beg end buffer
)
19016 (add-hook 'pre-command-hook
'org-unhighlight-once
))
19018 (defun org-unhighlight-once ()
19019 (remove-hook 'pre-command-hook
'org-unhighlight-once
)
19022 (defun org-agenda-follow-mode ()
19023 "Toggle follow mode in an agenda buffer."
19025 (setq org-agenda-follow-mode
(not org-agenda-follow-mode
))
19026 (org-agenda-set-mode-name)
19027 (message "Follow mode is %s"
19028 (if org-agenda-follow-mode
"on" "off")))
19030 (defun org-agenda-log-mode ()
19031 "Toggle log mode in an agenda buffer."
19033 (org-agenda-check-type t
'agenda
'timeline
)
19034 (setq org-agenda-show-log
(not org-agenda-show-log
))
19035 (org-agenda-set-mode-name)
19037 (message "Log mode is %s"
19038 (if org-agenda-show-log
"on" "off")))
19040 (defun org-agenda-toggle-diary ()
19041 "Toggle diary inclusion in an agenda buffer."
19043 (org-agenda-check-type t
'agenda
)
19044 (setq org-agenda-include-diary
(not org-agenda-include-diary
))
19046 (org-agenda-set-mode-name)
19047 (message "Diary inclusion turned %s"
19048 (if org-agenda-include-diary
"on" "off")))
19050 (defun org-agenda-toggle-time-grid ()
19051 "Toggle time grid in an agenda buffer."
19053 (org-agenda-check-type t
'agenda
)
19054 (setq org-agenda-use-time-grid
(not org-agenda-use-time-grid
))
19056 (org-agenda-set-mode-name)
19057 (message "Time-grid turned %s"
19058 (if org-agenda-use-time-grid
"on" "off")))
19060 (defun org-agenda-set-mode-name ()
19061 "Set the mode name to indicate all the small mode settings."
19063 (concat "Org-Agenda"
19064 (if (equal org-agenda-ndays
1) " Day" "")
19065 (if (equal org-agenda-ndays
7) " Week" "")
19066 (if org-agenda-follow-mode
" Follow" "")
19067 (if org-agenda-include-diary
" Diary" "")
19068 (if org-agenda-use-time-grid
" Grid" "")
19069 (if org-agenda-show-log
" Log" "")))
19070 (force-mode-line-update))
19072 (defun org-agenda-post-command-hook ()
19073 (and (eolp) (not (bolp)) (backward-char 1))
19074 (setq org-agenda-type
(get-text-property (point) 'org-agenda-type
))
19075 (if (and org-agenda-follow-mode
19076 (get-text-property (point) 'org-marker
))
19077 (org-agenda-show)))
19079 (defun org-agenda-show-priority ()
19080 "Show the priority of the current item.
19081 This priority is composed of the main priority given with the [#A] cookies,
19082 and by additional input from the age of a schedules or deadline entry."
19084 (let* ((pri (get-text-property (point-at-bol) 'priority
)))
19085 (message "Priority is %d" (if pri pri -
1000))))
19087 (defun org-agenda-show-tags ()
19088 "Show the tags applicable to the current item."
19090 (let* ((tags (get-text-property (point-at-bol) 'tags
)))
19092 (message "Tags are :%s:"
19093 (org-no-properties (mapconcat 'identity tags
":")))
19094 (message "No tags associated with this line"))))
19096 (defun org-agenda-goto (&optional highlight
)
19097 "Go to the Org-mode file which contains the item at point."
19099 (let* ((marker (or (get-text-property (point) 'org-marker
)
19100 (org-agenda-error)))
19101 (buffer (marker-buffer marker
))
19102 (pos (marker-position marker
)))
19103 (switch-to-buffer-other-window buffer
)
19107 (org-show-context 'agenda
)
19109 (and (outline-next-heading)
19110 (org-flag-heading nil
)))) ; show the next heading
19111 (and highlight
(org-highlight (point-at-bol) (point-at-eol)))))
19113 (defun org-agenda-kill ()
19114 "Kill the entry or subtree belonging to the current agenda entry."
19116 (or (eq major-mode
'org-agenda-mode
) (error "Not in agenda"))
19117 (let* ((marker (or (get-text-property (point) 'org-marker
)
19118 (org-agenda-error)))
19119 (buffer (marker-buffer marker
))
19120 (pos (marker-position marker
))
19121 (type (get-text-property (point) 'type
))
19122 dbeg dend
(n 0) conf
)
19123 (org-with-remote-undo buffer
19124 (with-current-buffer buffer
19127 (if (and (org-mode-p) (not (member type
'("sexp"))))
19128 (setq dbeg
(progn (org-back-to-heading t
) (point))
19129 dend
(org-end-of-subtree t t
))
19130 (setq dbeg
(point-at-bol)
19131 dend
(min (point-max) (1+ (point-at-eol)))))
19133 (while (re-search-forward "^[ \t]*\\S-" dend t
) (setq n
(1+ n
)))))
19134 (setq conf
(or (eq t org-agenda-confirm-kill
)
19135 (and (numberp org-agenda-confirm-kill
)
19136 (> n org-agenda-confirm-kill
))))
19139 (format "Delete entry with %d lines in buffer \"%s\"? "
19140 n
(buffer-name buffer
))))
19142 (org-remove-subtree-entries-from-agenda buffer dbeg dend
)
19143 (with-current-buffer buffer
(delete-region dbeg dend
))
19144 (message "Agenda item and source killed"))))
19146 (defun org-agenda-archive ()
19147 "Kill the entry or subtree belonging to the current agenda entry."
19149 (or (eq major-mode
'org-agenda-mode
) (error "Not in agenda"))
19150 (let* ((marker (or (get-text-property (point) 'org-marker
)
19151 (org-agenda-error)))
19152 (buffer (marker-buffer marker
))
19153 (pos (marker-position marker
)))
19154 (org-with-remote-undo buffer
19155 (with-current-buffer buffer
19159 (org-remove-subtree-entries-from-agenda)
19160 (org-back-to-heading t
)
19161 (org-archive-subtree))
19162 (error "Archiving works only in Org-mode files"))))))
19164 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end
)
19165 "Remove all lines in the agenda that correspond to a given subtree.
19166 The subtree is the one in buffer BUF, starting at BEG and ending at END.
19167 If this information is not given, the function uses the tree at point."
19168 (let ((buf (or buf
(current-buffer))) m p
)
19170 (unless (and beg end
)
19171 (org-back-to-heading t
)
19173 (org-end-of-subtree t
)
19174 (setq end
(point)))
19175 (set-buffer (get-buffer org-agenda-buffer-name
))
19177 (goto-char (point-max))
19178 (beginning-of-line 1)
19179 (while (not (bobp))
19180 (when (and (setq m
(get-text-property (point) 'org-marker
))
19181 (equal buf
(marker-buffer m
))
19182 (setq p
(marker-position m
))
19185 (let ((inhibit-read-only t
))
19186 (delete-region (point-at-bol) (1+ (point-at-eol)))))
19187 (beginning-of-line 0))))))
19189 (defun org-agenda-open-link ()
19190 "Follow the link in the current line, if any."
19192 (let ((eol (point-at-eol)))
19194 (if (or (re-search-forward org-bracket-link-regexp eol t
)
19195 (re-search-forward org-angle-link-re eol t
)
19196 (re-search-forward org-plain-link-re eol t
))
19197 (call-interactively 'org-open-at-point
)
19198 (error "No link in current line")))))
19200 (defun org-agenda-switch-to (&optional delete-other-windows
)
19201 "Go to the Org-mode file which contains the item at point."
19203 (let* ((marker (or (get-text-property (point) 'org-marker
)
19204 (org-agenda-error)))
19205 (buffer (marker-buffer marker
))
19206 (pos (marker-position marker
)))
19207 (switch-to-buffer buffer
)
19208 (and delete-other-windows
(delete-other-windows))
19212 (org-show-context 'agenda
)
19214 (and (outline-next-heading)
19215 (org-flag-heading nil
)))))) ; show the next heading
19217 (defun org-agenda-goto-mouse (ev)
19218 "Go to the Org-mode file which contains the item at the mouse click."
19220 (mouse-set-point ev
)
19223 (defun org-agenda-show ()
19224 "Display the Org-mode file which contains the item at point."
19226 (let ((win (selected-window)))
19227 (org-agenda-goto t
)
19228 (select-window win
)))
19230 (defun org-agenda-recenter (arg)
19231 "Display the Org-mode file which contains the item at point and recenter."
19233 (let ((win (selected-window)))
19234 (org-agenda-goto t
)
19236 (select-window win
)))
19238 (defun org-agenda-show-mouse (ev)
19239 "Display the Org-mode file which contains the item at the mouse click."
19241 (mouse-set-point ev
)
19244 (defun org-agenda-check-no-diary ()
19245 "Check if the entry is a diary link and abort if yes."
19246 (if (get-text-property (point) 'org-agenda-diary-link
)
19247 (org-agenda-error)))
19249 (defun org-agenda-error ()
19250 (error "Command not allowed in this line"))
19252 (defun org-agenda-tree-to-indirect-buffer ()
19253 "Show the subtree corresponding to the current entry in an indirect buffer.
19254 This calls the command `org-tree-to-indirect-buffer' from the original
19256 With numerical prefix arg ARG, go up to this level and then take that tree.
19257 With a C-u prefix, make a separate frame for this tree (i.e. don't use the
19260 (org-agenda-check-no-diary)
19261 (let* ((marker (or (get-text-property (point) 'org-marker
)
19262 (org-agenda-error)))
19263 (buffer (marker-buffer marker
))
19264 (pos (marker-position marker
)))
19265 (with-current-buffer buffer
19268 (call-interactively 'org-tree-to-indirect-buffer
)))))
19270 (defvar org-last-heading-marker
(make-marker)
19271 "Marker pointing to the headline that last changed its TODO state
19272 by a remote command from the agenda.")
19274 (defun org-agenda-todo-nextset ()
19275 "Switch TODO entry to next sequence."
19277 (org-agenda-todo 'nextset
))
19279 (defun org-agenda-todo-previousset ()
19280 "Switch TODO entry to previous sequence."
19282 (org-agenda-todo 'previousset
))
19284 (defun org-agenda-todo (&optional arg
)
19285 "Cycle TODO state of line at point, also in Org-mode file.
19286 This changes the line at point, all other lines in the agenda referring to
19287 the same tree node, and the headline of the tree node in the Org-mode file."
19289 (org-agenda-check-no-diary)
19290 (let* ((col (current-column))
19291 (marker (or (get-text-property (point) 'org-marker
)
19292 (org-agenda-error)))
19293 (buffer (marker-buffer marker
))
19294 (pos (marker-position marker
))
19295 (hdmarker (get-text-property (point) 'org-hd-marker
))
19296 (inhibit-read-only t
)
19298 (org-with-remote-undo buffer
19299 (with-current-buffer buffer
19302 (org-show-context 'agenda
)
19304 (and (outline-next-heading)
19305 (org-flag-heading nil
))) ; show the next heading
19307 (and (bolp) (forward-char 1))
19308 (setq newhead
(org-get-heading))
19310 (org-back-to-heading)
19311 (move-marker org-last-heading-marker
(point))))
19312 (beginning-of-line 1)
19314 (org-agenda-change-all-lines newhead hdmarker
'fixface
))
19315 (move-to-column col
))))
19317 (defun org-agenda-change-all-lines (newhead hdmarker
&optional fixface
)
19318 "Change all lines in the agenda buffer which match HDMARKER.
19319 The new content of the line will be NEWHEAD (as modified by
19320 `org-format-agenda-item'). HDMARKER is checked with
19321 `equal' against all `org-hd-marker' text properties in the file.
19322 If FIXFACE is non-nil, the face of each item is modified acording to
19323 the new TODO state."
19324 (let* ((inhibit-read-only t
)
19325 props m pl undone-face done-face finish new dotime cat tags
)
19327 (goto-char (point-max))
19328 (beginning-of-line 1)
19329 (while (not finish
)
19330 (setq finish
(bobp))
19331 (when (and (setq m
(get-text-property (point) 'org-hd-marker
))
19332 (equal m hdmarker
))
19333 (setq props
(text-properties-at (point))
19334 dotime
(get-text-property (point) 'dotime
)
19335 cat
(get-text-property (point) 'org-category
)
19336 tags
(get-text-property (point) 'tags
)
19337 new
(org-format-agenda-item "x" newhead cat tags dotime
'noprefix
)
19338 pl
(get-text-property (point) 'prefix-length
)
19339 undone-face
(get-text-property (point) 'undone-face
)
19340 done-face
(get-text-property (point) 'done-face
))
19341 (move-to-column pl
)
19344 (beginning-of-line 1)
19345 (and (looking-at ".*\n?") (replace-match "")))
19347 (replace-match new t t
)
19348 (beginning-of-line 1)
19349 (add-text-properties (point-at-bol) (point-at-eol) props
)
19351 (add-text-properties
19352 (point-at-bol) (point-at-eol)
19354 (if org-last-todo-state-is-todo
19355 undone-face done-face
))))
19356 (org-agenda-highlight-todo 'line
)
19357 (beginning-of-line 1))
19358 (t (error "Line update did not work"))))
19359 (beginning-of-line 0)))
19360 (org-finalize-agenda)))
19362 ;; FIXME: allow negative value for org-agenda-align-tags-to-column
19363 ;; See the code in set-tags for the way to do this.
19364 (defun org-agenda-align-tags (&optional line
)
19365 "Align all tags in agenda items to `org-agenda-align-tags-to-column'."
19366 (let ((inhibit-read-only t
))
19368 (goto-char (if line
(point-at-bol) (point-min)))
19369 (while (re-search-forward (org-re "\\([ \t]+\\):[[:alnum:]_@:]+:[ \t]*$")
19370 (if line
(point-at-eol) nil
) t
)
19371 (delete-region (match-beginning 1) (match-end 1))
19372 (goto-char (match-beginning 1))
19373 (insert (org-add-props
19374 (make-string (max 1 (- org-agenda-align-tags-to-column
19375 (current-column))) ?\
)
19376 (text-properties-at (point))))))))
19378 (defun org-agenda-priority-up ()
19379 "Increase the priority of line at point, also in Org-mode file."
19381 (org-agenda-priority 'up
))
19383 (defun org-agenda-priority-down ()
19384 "Decrease the priority of line at point, also in Org-mode file."
19386 (org-agenda-priority 'down
))
19388 (defun org-agenda-priority (&optional force-direction
)
19389 "Set the priority of line at point, also in Org-mode file.
19390 This changes the line at point, all other lines in the agenda referring to
19391 the same tree node, and the headline of the tree node in the Org-mode file."
19393 (org-agenda-check-no-diary)
19394 (let* ((marker (or (get-text-property (point) 'org-marker
)
19395 (org-agenda-error)))
19396 (hdmarker (get-text-property (point) 'org-hd-marker
))
19397 (buffer (marker-buffer hdmarker
))
19398 (pos (marker-position hdmarker
))
19399 (inhibit-read-only t
)
19401 (org-with-remote-undo buffer
19402 (with-current-buffer buffer
19405 (org-show-context 'agenda
)
19407 (and (outline-next-heading)
19408 (org-flag-heading nil
))) ; show the next heading
19409 (funcall 'org-priority force-direction
)
19411 (setq newhead
(org-get-heading)))
19412 (org-agenda-change-all-lines newhead hdmarker
)
19413 (beginning-of-line 1))))
19415 (defun org-get-tags-at (&optional pos
)
19416 "Get a list of all headline tags applicable at POS.
19417 POS defaults to point. If tags are inherited, the list contains
19418 the targets in the same sequence as the headlines appear, i.e.
19419 the tags of the current headline come last."
19425 (goto-char (or pos
(point)))
19427 (org-back-to-heading t
)
19428 (condition-case nil
19430 (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
19431 (setq tags
(append (org-split-string
19432 (org-match-string-no-properties 1) ":")
19434 (or org-use-tag-inheritance
(error ""))
19435 (org-up-heading-all 1))
19439 ;; FIXME: should fix the tags property of the agenda line.
19440 (defun org-agenda-set-tags ()
19441 "Set tags for the current headline."
19443 (org-agenda-check-no-diary)
19444 (if (and (org-region-active-p) (interactive-p))
19445 (call-interactively 'org-change-tag-in-region
)
19446 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
19447 (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker
)
19448 (org-agenda-error)))
19449 (buffer (marker-buffer hdmarker
))
19450 (pos (marker-position hdmarker
))
19451 (inhibit-read-only t
)
19453 (org-with-remote-undo buffer
19454 (with-current-buffer buffer
19458 (org-show-context 'agenda
))
19460 (and (outline-next-heading)
19461 (org-flag-heading nil
))) ; show the next heading
19463 (call-interactively 'org-set-tags
)
19465 (setq newhead
(org-get-heading)))
19466 (org-agenda-change-all-lines newhead hdmarker
)
19467 (beginning-of-line 1)))))
19469 (defun org-agenda-toggle-archive-tag ()
19470 "Toggle the archive tag for the current entry."
19472 (org-agenda-check-no-diary)
19473 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
19474 (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker
)
19475 (org-agenda-error)))
19476 (buffer (marker-buffer hdmarker
))
19477 (pos (marker-position hdmarker
))
19478 (inhibit-read-only t
)
19480 (org-with-remote-undo buffer
19481 (with-current-buffer buffer
19484 (org-show-context 'agenda
)
19486 (and (outline-next-heading)
19487 (org-flag-heading nil
))) ; show the next heading
19488 (call-interactively 'org-toggle-archive-tag
)
19490 (setq newhead
(org-get-heading)))
19491 (org-agenda-change-all-lines newhead hdmarker
)
19492 (beginning-of-line 1))))
19494 (defun org-agenda-date-later (arg &optional what
)
19495 "Change the date of this item to one day later."
19497 (org-agenda-check-type t
'agenda
'timeline
)
19498 (org-agenda-check-no-diary)
19499 (let* ((marker (or (get-text-property (point) 'org-marker
)
19500 (org-agenda-error)))
19501 (buffer (marker-buffer marker
))
19502 (pos (marker-position marker
)))
19503 (org-with-remote-undo buffer
19504 (with-current-buffer buffer
19507 (if (not (org-at-timestamp-p))
19508 (error "Cannot find time stamp"))
19509 (org-timestamp-change arg
(or what
'day
)))
19510 (org-agenda-show-new-time marker org-last-changed-timestamp
))
19511 (message "Time stamp changed to %s" org-last-changed-timestamp
)))
19513 (defun org-agenda-date-earlier (arg &optional what
)
19514 "Change the date of this item to one day earlier."
19516 (org-agenda-date-later (- arg
) what
))
19518 (defun org-agenda-show-new-time (marker stamp
)
19519 "Show new date stamp via text properties."
19520 ;; We use text properties to make this undoable
19521 (let ((inhibit-read-only t
))
19522 (setq stamp
(concat " => " stamp
))
19524 (goto-char (point-max))
19525 (while (not (bobp))
19526 (when (equal marker
(get-text-property (point) 'org-marker
))
19527 (move-to-column (- (window-width) (length stamp
)) t
)
19528 (if (featurep 'xemacs
)
19529 ;; Use `duplicable' property to trigger undo recording
19530 (let ((ex (make-extent nil nil
))
19531 (gl (make-glyph stamp
)))
19532 (set-glyph-face gl
'secondary-selection
)
19533 (set-extent-properties
19534 ex
(list 'invisible t
'end-glyph gl
'duplicable t
))
19535 (insert-extent ex
(1- (point)) (point-at-eol)))
19536 (add-text-properties
19537 (1- (point)) (point-at-eol)
19538 (list 'display
(org-add-props stamp nil
19539 'face
'secondary-selection
))))
19540 (beginning-of-line 1))
19541 (beginning-of-line 0)))))
19543 (defun org-agenda-date-prompt (arg)
19544 "Change the date of this item. Date is prompted for, with default today.
19545 The prefix ARG is passed to the `org-time-stamp' command and can therefore
19546 be used to request time specification in the time stamp."
19548 (org-agenda-check-type t
'agenda
'timeline
)
19549 (org-agenda-check-no-diary)
19550 (let* ((marker (or (get-text-property (point) 'org-marker
)
19551 (org-agenda-error)))
19552 (buffer (marker-buffer marker
))
19553 (pos (marker-position marker
)))
19554 (org-with-remote-undo buffer
19555 (with-current-buffer buffer
19558 (if (not (org-at-timestamp-p))
19559 (error "Cannot find time stamp"))
19560 (org-time-stamp arg
)
19561 (message "Time stamp changed to %s" org-last-changed-timestamp
)))))
19563 (defun org-agenda-schedule (arg)
19564 "Schedule the item at point."
19566 (org-agenda-check-type t
'agenda
'timeline
'todo
'tags
)
19567 (org-agenda-check-no-diary)
19568 (let* ((marker (or (get-text-property (point) 'org-marker
)
19569 (org-agenda-error)))
19570 (buffer (marker-buffer marker
))
19571 (pos (marker-position marker
))
19572 (org-insert-labeled-timestamps-at-point nil
)
19574 (org-with-remote-undo buffer
19575 (with-current-buffer buffer
19578 (setq ts
(org-schedule))
19579 (message "Item scheduled for %s" ts
)))))
19581 (defun org-agenda-deadline (arg)
19582 "Schedule the item at point."
19584 (org-agenda-check-type t
'agenda
'timeline
'todo
'tags
)
19585 (org-agenda-check-no-diary)
19586 (let* ((marker (or (get-text-property (point) 'org-marker
)
19587 (org-agenda-error)))
19588 (buffer (marker-buffer marker
))
19589 (pos (marker-position marker
))
19590 (org-insert-labeled-timestamps-at-point nil
)
19592 (org-with-remote-undo buffer
19593 (with-current-buffer buffer
19596 (setq ts
(org-deadline))
19597 (message "Deadline for this item set to %s" ts
)))))
19599 (defun org-get-heading ()
19600 "Return the heading of the current entry, without the stars."
19602 (org-back-to-heading t
)
19603 (if (looking-at "\\*+[ \t]+\\([^\r\n]*\\)") (match-string 1) "")))
19605 (defun org-agenda-clock-in (&optional arg
)
19606 "Start the clock on the currently selected item."
19608 (org-agenda-check-no-diary)
19609 (let* ((marker (or (get-text-property (point) 'org-marker
)
19610 (org-agenda-error)))
19611 (pos (marker-position marker
)))
19612 (org-with-remote-undo (marker-buffer marker
)
19613 (with-current-buffer (marker-buffer marker
)
19618 (defun org-agenda-clock-out (&optional arg
)
19619 "Stop the currently running clock."
19621 (unless (marker-buffer org-clock-marker
)
19622 (error "No running clock"))
19623 (org-with-remote-undo (marker-buffer org-clock-marker
)
19626 (defun org-agenda-clock-cancel (&optional arg
)
19627 "Cancel the currently running clock."
19629 (unless (marker-buffer org-clock-marker
)
19630 (error "No running clock"))
19631 (org-with-remote-undo (marker-buffer org-clock-marker
)
19632 (org-clock-cancel)))
19634 (defun org-agenda-diary-entry ()
19635 "Make a diary entry, like the `i' command from the calendar.
19636 All the standard commands work: block, weekly etc."
19638 (org-agenda-check-type t
'agenda
'timeline
)
19639 (require 'diary-lib
)
19640 (let* ((char (progn
19641 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
19642 (read-char-exclusive)))
19643 (cmd (cdr (assoc char
19644 '((?d . insert-diary-entry
)
19645 (?w . insert-weekly-diary-entry
)
19646 (?m . insert-monthly-diary-entry
)
19647 (?y . insert-yearly-diary-entry
)
19648 (?a . insert-anniversary-diary-entry
)
19649 (?b . insert-block-diary-entry
)
19650 (?c . insert-cyclic-diary-entry
)))))
19651 (oldf (symbol-function 'calendar-cursor-to-date
))
19652 ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
19654 (mark (or (mark t
) (point))))
19656 (error "No command associated with <%c>" char
))
19657 (unless (and (get-text-property point
'day
)
19658 (or (not (equal ?b char
))
19659 (get-text-property mark
'day
)))
19660 (error "Don't know which date to use for diary entry"))
19661 ;; We implement this by hacking the `calendar-cursor-to-date' function
19662 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
19663 (let ((calendar-mark-ring
19664 (list (calendar-gregorian-from-absolute
19665 (or (get-text-property mark
'day
)
19666 (get-text-property point
'day
))))))
19669 (fset 'calendar-cursor-to-date
19670 (lambda (&optional error
)
19671 (calendar-gregorian-from-absolute
19672 (get-text-property point
'day
))))
19673 (call-interactively cmd
))
19674 (fset 'calendar-cursor-to-date oldf
)))))
19677 (defun org-agenda-execute-calendar-command (cmd)
19678 "Execute a calendar command from the agenda, with the date associated to
19679 the cursor position."
19680 (org-agenda-check-type t
'agenda
'timeline
)
19681 (require 'diary-lib
)
19682 (unless (get-text-property (point) 'day
)
19683 (error "Don't know which date to use for calendar command"))
19684 (let* ((oldf (symbol-function 'calendar-cursor-to-date
))
19686 (date (calendar-gregorian-from-absolute
19687 (get-text-property point
'day
)))
19688 ;; the following 3 vars are needed in the calendar
19689 (displayed-day (extract-calendar-day date
))
19690 (displayed-month (extract-calendar-month date
))
19691 (displayed-year (extract-calendar-year date
)))
19694 (fset 'calendar-cursor-to-date
19695 (lambda (&optional error
)
19696 (calendar-gregorian-from-absolute
19697 (get-text-property point
'day
))))
19698 (call-interactively cmd
))
19699 (fset 'calendar-cursor-to-date oldf
))))
19701 (defun org-agenda-phases-of-moon ()
19702 "Display the phases of the moon for the 3 months around the cursor date."
19704 (org-agenda-execute-calendar-command 'calendar-phases-of-moon
))
19706 (defun org-agenda-holidays ()
19707 "Display the holidays for the 3 months around the cursor date."
19709 (org-agenda-execute-calendar-command 'list-calendar-holidays
))
19711 (defun org-agenda-sunrise-sunset (arg)
19712 "Display sunrise and sunset for the cursor date.
19713 Latitude and longitude can be specified with the variables
19714 `calendar-latitude' and `calendar-longitude'. When called with prefix
19715 argument, latitude and longitude will be prompted for."
19717 (let ((calendar-longitude (if arg nil calendar-longitude
))
19718 (calendar-latitude (if arg nil calendar-latitude
))
19719 (calendar-location-name
19720 (if arg
"the given coordinates" calendar-location-name
)))
19721 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset
)))
19723 (defun org-agenda-goto-calendar ()
19724 "Open the Emacs calendar with the date at the cursor."
19726 (org-agenda-check-type t
'agenda
'timeline
)
19727 (let* ((day (or (get-text-property (point) 'day
)
19728 (error "Don't know which date to open in calendar")))
19729 (date (calendar-gregorian-from-absolute day
))
19730 (calendar-move-hook nil
)
19731 (view-calendar-holidays-initially nil
)
19732 (view-diary-entries-initially nil
))
19734 (calendar-goto-date date
)))
19736 (defun org-calendar-goto-agenda ()
19737 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
19738 This is a command that has to be installed in `calendar-mode-map'."
19740 (org-agenda-list nil
(calendar-absolute-from-gregorian
19741 (calendar-cursor-to-date))
19744 (defun org-agenda-convert-date ()
19746 (org-agenda-check-type t
'agenda
'timeline
)
19747 (let ((day (get-text-property (point) 'day
))
19750 (error "Don't know which date to convert"))
19751 (setq date
(calendar-gregorian-from-absolute day
))
19753 "Gregorian: " (calendar-date-string date
) "\n"
19754 "ISO: " (calendar-iso-date-string date
) "\n"
19755 "Day of Yr: " (calendar-day-of-year-string date
) "\n"
19756 "Julian: " (calendar-julian-date-string date
) "\n"
19757 "Astron. JD: " (calendar-astro-date-string date
)
19758 " (Julian date number at noon UTC)\n"
19759 "Hebrew: " (calendar-hebrew-date-string date
) " (until sunset)\n"
19760 "Islamic: " (calendar-islamic-date-string date
) " (until sunset)\n"
19761 "French: " (calendar-french-date-string date
) "\n"
19762 "Mayan: " (calendar-mayan-date-string date
) "\n"
19763 "Coptic: " (calendar-coptic-date-string date
) "\n"
19764 "Ethiopic: " (calendar-ethiopic-date-string date
) "\n"
19765 "Persian: " (calendar-persian-date-string date
) "\n"
19766 "Chinese: " (calendar-chinese-date-string date
) "\n"))
19767 (with-output-to-temp-buffer "*Dates*"
19769 (if (fboundp 'fit-window-to-buffer
)
19770 (fit-window-to-buffer (get-buffer-window "*Dates*")))))
19773 ;;;; Embedded LaTeX
19775 (defvar org-cdlatex-mode-map
(make-sparse-keymap)
19776 "Keymap for the minor `org-cdlatex-mode'.")
19778 (org-defkey org-cdlatex-mode-map
"_" 'org-cdlatex-underscore-caret
)
19779 (org-defkey org-cdlatex-mode-map
"^" 'org-cdlatex-underscore-caret
)
19780 (org-defkey org-cdlatex-mode-map
"`" 'cdlatex-math-symbol
)
19781 (org-defkey org-cdlatex-mode-map
"'" 'org-cdlatex-math-modify
)
19782 (org-defkey org-cdlatex-mode-map
"\C-c{" 'cdlatex-environment
)
19784 (defvar org-cdlatex-texmathp-advice-is-done nil
19785 "Flag remembering if we have applied the advice to texmathp already.")
19787 (define-minor-mode org-cdlatex-mode
19788 "Toggle the minor `org-cdlatex-mode'.
19789 This mode supports entering LaTeX environment and math in LaTeX fragments
19791 \\{org-cdlatex-mode-map}"
19793 (when org-cdlatex-mode
(require 'cdlatex
))
19794 (unless org-cdlatex-texmathp-advice-is-done
19795 (setq org-cdlatex-texmathp-advice-is-done t
)
19796 (defadvice texmathp
(around org-math-always-on activate
)
19797 "Always return t in org-mode buffers.
19798 This is because we want to insert math symbols without dollars even outside
19799 the LaTeX math segments. If Orgmode thinks that point is actually inside
19800 en embedded LaTeX fragement, let texmathp do its job.
19801 \\[org-cdlatex-mode-map]"
19805 ((not (org-mode-p)) ad-do-it
)
19806 ((eq this-command
'cdlatex-math-symbol
)
19807 (setq ad-return-value t
19808 texmathp-why
'("cdlatex-math-symbol in org-mode" .
0)))
19810 (let ((p (org-inside-LaTeX-fragment-p)))
19811 (if (and p
(member (car p
) (plist-get org-format-latex-options
:matchers
)))
19812 (setq ad-return-value t
19813 texmathp-why
'("Org-mode embedded math" .
0))
19814 (if p ad-do-it
)))))))))
19816 (defun turn-on-org-cdlatex ()
19817 "Unconditionally turn on `org-cdlatex-mode'."
19818 (org-cdlatex-mode 1))
19820 (defun org-inside-LaTeX-fragment-p ()
19821 "Test if point is inside a LaTeX fragment.
19822 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
19823 sequence appearing also before point.
19824 Even though the matchers for math are configurable, this function assumes
19825 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
19826 delimiters are skipped when they have been removed by customization.
19827 The return value is nil, or a cons cell with the delimiter and
19828 and the position of this delimiter.
19830 This function does a reasonably good job, but can locally be fooled by
19831 for example currency specifications. For example it will assume being in
19832 inline math after \"$22.34\". The LaTeX fragment formatter will only format
19833 fragments that are properly closed, but during editing, we have to live
19834 with the uncertainty caused by missing closing delimiters. This function
19835 looks only before point, not after."
19837 (let ((pos (point))
19838 (dodollar (member "$" (plist-get org-format-latex-options
:matchers
)))
19840 (re-search-backward (concat "^\\(" paragraph-start
"\\)") nil t
)
19842 dd-on str
(start 0) m re
)
19845 (setq str
(concat (buffer-substring lim
(point)) "\000 X$.")
19846 re
(nth 1 (assoc "$" org-latex-regexps
)))
19847 (while (string-match re str start
)
19849 ((= (match-end 0) (length str
))
19850 (throw 'exit
(cons "$" (+ lim
(match-beginning 0) 1))))
19851 ((= (match-end 0) (- (length str
) 5))
19853 (t (setq start
(match-end 0))))))
19854 (when (setq m
(re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t
))
19856 (and (match-beginning 1) (throw 'exit
(cons (match-string 1) m
)))
19857 (and (match-beginning 2) (throw 'exit nil
))
19859 (while (re-search-backward "\\$\\$" lim t
)
19860 (setq dd-on
(not dd-on
)))
19862 (if dd-on
(cons "$$" m
))))))
19865 (defun org-try-cdlatex-tab ()
19866 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
19867 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
19868 - inside a LaTeX fragment, or
19869 - after the first word in a line, where an abbreviation expansion could
19870 insert a LaTeX environment."
19871 (when org-cdlatex-mode
19874 (skip-chars-backward "a-zA-Z0-9*")
19875 (skip-chars-backward " \t")
19878 ((org-inside-LaTeX-fragment-p)
19882 (defun org-cdlatex-underscore-caret (&optional arg
)
19883 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
19884 Revert to the normal definition outside of these fragments."
19886 (if (org-inside-LaTeX-fragment-p)
19887 (call-interactively 'cdlatex-sub-superscript
)
19888 (let (org-cdlatex-mode)
19889 (call-interactively (key-binding (vector last-input-event
))))))
19891 (defun org-cdlatex-math-modify (&optional arg
)
19892 "Execute `cdlatex-math-modify' in LaTeX fragments.
19893 Revert to the normal definition outside of these fragments."
19895 (if (org-inside-LaTeX-fragment-p)
19896 (call-interactively 'cdlatex-math-modify
)
19897 (let (org-cdlatex-mode)
19898 (call-interactively (key-binding (vector last-input-event
))))))
19900 (defvar org-latex-fragment-image-overlays nil
19901 "List of overlays carrying the images of latex fragments.")
19902 (make-variable-buffer-local 'org-latex-fragment-image-overlays
)
19904 (defun org-remove-latex-fragment-image-overlays ()
19905 "Remove all overlays with LaTeX fragment images in current buffer."
19906 (mapc 'org-delete-overlay org-latex-fragment-image-overlays
)
19907 (setq org-latex-fragment-image-overlays nil
))
19909 (defun org-preview-latex-fragment (&optional subtree
)
19910 "Preview the LaTeX fragment at point, or all locally or globally.
19911 If the cursor is in a LaTeX fragment, create the image and overlay
19912 it over the source code. If there is no fragment at point, display
19913 all fragments in the current text, from one headline to the next. With
19914 prefix SUBTREE, display all fragments in the current subtree. With a
19915 double prefix `C-u C-u', or when the cursor is before the first headline,
19916 display all fragments in the buffer.
19917 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
19919 (org-remove-latex-fragment-image-overlays)
19922 (let (beg end at msg
)
19924 ((or (equal subtree
'(16))
19925 (not (save-excursion
19926 (re-search-backward (concat "^" outline-regexp
) nil t
))))
19927 (setq beg
(point-min) end
(point-max)
19928 msg
"Creating images for buffer...%s"))
19929 ((equal subtree
'(4))
19930 (org-back-to-heading)
19931 (setq beg
(point) end
(org-end-of-subtree t
)
19932 msg
"Creating images for subtree...%s"))
19934 (if (setq at
(org-inside-LaTeX-fragment-p))
19935 (goto-char (max (point-min) (- (cdr at
) 2)))
19936 (org-back-to-heading))
19937 (setq beg
(point) end
(progn (outline-next-heading) (point))
19938 msg
(if at
"Creating image...%s"
19939 "Creating images for entry...%s"))))
19941 (narrow-to-region beg end
)
19944 (concat "ltxpng/" (file-name-sans-extension
19945 (file-name-nondirectory
19946 buffer-file-name
)))
19947 default-directory
'overlays msg at
'forbuffer
)
19948 (message msg
"done. Use `C-c C-c' to remove images.")))))
19950 (defvar org-latex-regexps
19951 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t
)
19952 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
19953 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
19954 ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil
)
19955 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil
)
19956 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t
)
19957 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t
))
19958 "Regular expressions for matching embedded LaTeX.")
19960 (defun org-format-latex (prefix &optional dir overlays msg at forbuffer
)
19961 "Replace LaTeX fragments with links to an image, and produce images."
19962 (if (and overlays
(fboundp 'clear-image-cache
)) (clear-image-cache))
19963 (let* ((prefixnodir (file-name-nondirectory prefix
))
19964 (absprefix (expand-file-name prefix dir
))
19965 (todir (file-name-directory absprefix
))
19966 (opt org-format-latex-options
)
19967 (matchers (plist-get opt
:matchers
))
19968 (re-list org-latex-regexps
)
19969 (cnt 0) txt link beg end re e checkdir
19970 m n block linkfile movefile ov
)
19971 ;; Check if there are old images files with this prefix, and remove them
19972 (when (file-directory-p todir
)
19976 (concat (regexp-quote prefixnodir
) "_[0-9]+\\.png$"))))
19977 ;; Check the different regular expressions
19978 (while (setq e
(pop re-list
))
19979 (setq m
(car e
) re
(nth 1 e
) n
(nth 2 e
)
19980 block
(if (nth 3 e
) "\n\n" ""))
19981 (when (member m matchers
)
19982 (goto-char (point-min))
19983 (while (re-search-forward re nil t
)
19984 (when (or (not at
) (equal (cdr at
) (match-beginning n
)))
19985 (setq txt
(match-string n
)
19986 beg
(match-beginning n
) end
(match-end n
)
19988 linkfile
(format "%s_%04d.png" prefix cnt
)
19989 movefile
(format "%s_%04d.png" absprefix cnt
)
19990 link
(concat block
"[[file:" linkfile
"]]" block
))
19991 (if msg
(message msg cnt
))
19993 (unless checkdir
; make sure the directory exists
19995 (or (file-directory-p todir
) (make-directory todir
)))
19996 (org-create-formula-image
19997 txt movefile opt forbuffer
)
20000 (setq ov
(org-make-overlay beg end
))
20001 (if (featurep 'xemacs
)
20003 (org-overlay-put ov
'invisible t
)
20006 (make-glyph (vector 'png
:file movefile
))))
20009 (list 'image
:type
'png
:file movefile
:ascent
'center
)))
20010 (push ov org-latex-fragment-image-overlays
)
20012 (delete-region beg end
)
20013 (insert link
))))))))
20015 ;; This function borrows from Ganesh Swami's latex2png.el
20016 (defun org-create-formula-image (string tofile options buffer
)
20017 (let* ((tmpdir (if (featurep 'xemacs
)
20019 temporary-file-directory
))
20020 (texfilebase (make-temp-name
20021 (expand-file-name "orgtex" tmpdir
)))
20022 (texfile (concat texfilebase
".tex"))
20023 (dvifile (concat texfilebase
".dvi"))
20024 (pngfile (concat texfilebase
".png"))
20025 (fnh (face-attribute 'default
:height nil
))
20026 (scale (or (plist-get options
(if buffer
:scale
:html-scale
)) 1.0))
20027 (dpi (number-to-string (* scale
(floor (* 0.9 (if buffer fnh
140.
))))))
20028 (fg (or (plist-get options
(if buffer
:foreground
:html-foreground
))
20030 (bg (or (plist-get options
(if buffer
:background
:html-background
))
20032 (if (eq fg
'default
) (setq fg
(org-dvipng-color :foreground
)))
20033 (if (eq bg
'default
) (setq bg
(org-dvipng-color :background
)))
20034 (with-temp-file texfile
20035 (insert org-format-latex-header
20036 "\n\\begin{document}\n" string
"\n\\end{document}\n"))
20037 (let ((dir default-directory
))
20038 (condition-case nil
20041 (call-process "latex" nil nil nil texfile
))
20044 (if (not (file-exists-p dvifile
))
20045 (progn (message "Failed to create dvi file from %s" texfile
) nil
)
20046 (call-process "dvipng" nil nil nil
20047 "-E" "-fg" fg
"-bg" bg
20049 ;;"-x" scale "-y" scale
20053 (if (not (file-exists-p pngfile
))
20054 (progn (message "Failed to create png file from %s" texfile
) nil
)
20055 ;; Use the requested file name and clean up
20056 (copy-file pngfile tofile
'replace
)
20057 (loop for e in
'(".dvi" ".tex" ".aux" ".log" ".png") do
20058 (delete-file (concat texfilebase e
)))
20061 (defun org-dvipng-color (attr)
20062 "Return an rgb color specification for dvipng."
20063 (apply 'format
"rgb %s %s %s"
20064 (mapcar 'org-normalize-color
20065 (color-values (face-attribute 'default attr nil
)))))
20067 (defun org-normalize-color (value)
20068 "Return string to be used as color value for an RGB component."
20069 (format "%g" (/ value
65535.0)))
20073 ;;; Variables, constants, and parameter plists
20075 (defconst org-level-max
20)
20077 (defvar org-export-html-preamble nil
20078 "Preamble, to be inserted just after <body>. Set by publishing functions.")
20079 (defvar org-export-html-postamble nil
20080 "Preamble, to be inserted just before </body>. Set by publishing functions.")
20081 (defvar org-export-html-auto-preamble t
20082 "Should default preamble be inserted? Set by publishing functions.")
20083 (defvar org-export-html-auto-postamble t
20084 "Should default postamble be inserted? Set by publishing functions.")
20085 (defvar org-current-export-file nil
) ; dynamically scoped parameter
20086 (defvar org-current-export-dir nil
) ; dynamically scoped parameter
20089 (defconst org-export-plist-vars
20090 '((:language . org-export-default-language
)
20091 (:customtime . org-display-custom-times
)
20092 (:headline-levels . org-export-headline-levels
)
20093 (:section-numbers . org-export-with-section-numbers
)
20094 (:table-of-contents . org-export-with-toc
)
20095 (:preserve-breaks . org-export-preserve-breaks
)
20096 (:archived-trees . org-export-with-archived-trees
)
20097 (:emphasize . org-export-with-emphasize
)
20098 (:sub-superscript . org-export-with-sub-superscripts
)
20099 (:footnotes . org-export-with-footnotes
)
20100 (:property-drawer . org-export-with-property-drawer
)
20101 (:TeX-macros . org-export-with-TeX-macros
)
20102 (:LaTeX-fragments . org-export-with-LaTeX-fragments
)
20103 (:skip-before-1st-heading . org-export-skip-text-before-1st-heading
)
20104 (:fixed-width . org-export-with-fixed-width
)
20105 (:timestamps . org-export-with-timestamps
)
20106 (:author-info . org-export-author-info
)
20107 (:time-stamp-file . org-export-time-stamp-file
)
20108 (:tables . org-export-with-tables
)
20109 (:table-auto-headline . org-export-highlight-first-table-line
)
20110 (:style . org-export-html-style
)
20111 (:agenda-style . org-agenda-export-html-style
) ;; FIXME: Does this work????
20112 (:convert-org-links . org-export-html-link-org-files-as-html
)
20113 (:inline-images . org-export-html-inline-images
)
20114 (:expand-quoted-html . org-export-html-expand
)
20115 (:timestamp . org-export-html-with-timestamp
)
20116 (:publishing-directory . org-export-publishing-directory
)
20117 (:preamble . org-export-html-preamble
)
20118 (:postamble . org-export-html-postamble
)
20119 (:auto-preamble . org-export-html-auto-preamble
)
20120 (:auto-postamble . org-export-html-auto-postamble
)
20121 (:author . user-full-name
)
20122 (:email . user-mail-address
)))
20124 (defun org-default-export-plist ()
20125 "Return the property list with default settings for the export variables."
20126 (let ((l org-export-plist-vars
) rtn e
)
20127 (while (setq e
(pop l
))
20128 (setq rtn
(cons (car e
) (cons (symbol-value (cdr e
)) rtn
))))
20131 (defun org-infile-export-plist ()
20132 "Return the property list with file-local settings for export."
20135 (let ((re (org-make-options-regexp
20136 '("TITLE" "AUTHOR" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
20137 p key val text options
)
20138 (while (re-search-forward re nil t
)
20139 (setq key
(org-match-string-no-properties 1)
20140 val
(org-match-string-no-properties 2))
20142 ((string-equal key
"TITLE") (setq p
(plist-put p
:title val
)))
20143 ((string-equal key
"AUTHOR")(setq p
(plist-put p
:author val
)))
20144 ((string-equal key
"EMAIL") (setq p
(plist-put p
:email val
)))
20145 ((string-equal key
"LANGUAGE") (setq p
(plist-put p
:language val
)))
20146 ((string-equal key
"TEXT")
20147 (setq text
(if text
(concat text
"\n" val
) val
)))
20148 ((string-equal key
"OPTIONS") (setq options val
))))
20149 (setq p
(plist-put p
:text text
))
20151 (let ((op '(("H" .
:headline-levels
)
20152 ("num" .
:section-numbers
)
20153 ("toc" .
:table-of-contents
)
20154 ("\\n" .
:preserve-breaks
)
20155 ("@" .
:expand-quoted-html
)
20156 (":" .
:fixed-width
)
20158 ("^" .
:sub-superscript
)
20160 ("p" .
:property-drawer
)
20162 ("TeX" .
:TeX-macros
)
20163 ("LaTeX" .
:LaTeX-fragments
)
20164 ("skip" .
:skip-before-1st-heading
)
20165 ("author" .
:author-info
)
20166 ("timestamp" .
:time-stamp-file
)))
20168 (while (setq o
(pop op
))
20169 (if (string-match (concat (regexp-quote (car o
))
20170 ":\\([^ \t\n\r;,.]*\\)")
20172 (setq p
(plist-put p
(cdr o
)
20173 (car (read-from-string
20174 (match-string 1 options
)))))))))
20177 (defun org-export-directory (type plist
)
20178 (let* ((val (plist-get plist
:publishing-directory
))
20179 (dir (if (listp val
)
20180 (or (cdr (assoc type val
)) ".")
20184 (defun org-skip-comments (lines)
20185 "Skip lines starting with \"#\" and subtrees starting with COMMENT."
20186 (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string
))
20187 (re2 "^\\(\\*+\\)[ \t\n\r]")
20188 (case-fold-search nil
)
20190 (while (setq line
(pop lines
))
20192 ((and (string-match re1 line
)
20193 (setq level
(- (match-end 1) (match-beginning 1))))
20194 ;; Beginning of a COMMENT subtree. Skip it.
20195 (while (and (setq line
(pop lines
))
20196 (or (not (string-match re2 line
))
20197 (> (- (match-end 1) (match-beginning 1)) level
))))
20198 (setq lines
(cons line lines
)))
20199 ((string-match "^#" line
)
20200 ;; an ordinary comment line
20202 ((and org-export-table-remove-special-lines
20203 (string-match "^[ \t]*|" line
)
20204 (or (string-match "^[ \t]*| *[!_^] *|" line
)
20205 (and (string-match "| *<[0-9]+> *|" line
)
20206 (not (string-match "| *[^ <|]" line
)))))
20207 ;; a special table line that should be removed
20209 (t (setq rtn
(cons line rtn
)))))
20212 (defun org-export (&optional arg
)
20214 (let ((help "[t] insert the export option template
20215 \[v] limit export to visible part of outline tree
20217 \[a] export as ASCII
20219 \[h] export as HTML
20220 \[H] export as HTML to temporary buffer
20221 \[R] export region as HTML
20222 \[b] export as HTML and browse immediately
20223 \[x] export as XOXO
20225 \[l] export as LaTeX
20226 \[L] export as LaTeX to temporary buffer
20228 \[i] export current file as iCalendar file
20229 \[I] export all agenda files as iCalendar files
20230 \[c] export agenda files into combined iCalendar file
20232 \[F] publish current file
20233 \[P] publish current project
20234 \[X] publish... (project will be prompted for)
20235 \[A] publish all projects")
20237 '((?t . org-insert-export-options-template
)
20238 (?v . org-export-visible
)
20239 (?a . org-export-as-ascii
)
20240 (?h . org-export-as-html
)
20241 (?b . org-export-as-html-and-open
)
20242 (?H . org-export-as-html-to-buffer
)
20243 (?R . org-export-region-as-html
)
20244 (?x . org-export-as-xoxo
)
20245 (?l . org-export-as-latex
)
20246 (?L . org-export-as-latex-to-buffer
)
20247 (?i . org-export-icalendar-this-file
)
20248 (?I . org-export-icalendar-all-agenda-files
)
20249 (?c . org-export-icalendar-combine-agenda-files
)
20250 (?F . org-publish-current-file
)
20251 (?P . org-publish-current-project
)
20253 (?A . org-publish-all
)))
20255 (save-window-excursion
20256 (delete-other-windows)
20257 (with-output-to-temp-buffer "*Org Export/Publishing Help*"
20259 (message "Select command: ")
20260 (setq r1
(read-char-exclusive)))
20261 (setq r2
(if (< r1
27) (+ r1
96) r1
))
20262 (if (setq ass
(assq r2 cmds
))
20263 (call-interactively (cdr ass
))
20264 (error "No command associated with key %c" r1
))))
20266 (defconst org-html-entities
20274 ("vert" .
"|")
20307 ("Aring") ("AA".
"Å")
20396 ("varepsilon".
"ε")
20409 ("sigmaf") ("varsigma".
"ς")
20417 ("thetasym") ("vartheta".
"ϑ")
20420 ("bull") ("bullet".
"•")
20421 ("hellip") ("dots".
"…")
20431 ("larr") ("leftarrow".
"←") ("gets".
"←")
20432 ("uarr") ("uparrow".
"↑")
20433 ("rarr") ("to".
"→") ("rightarrow".
"→")
20434 ("darr")("downarrow".
"↓")
20435 ("harr") ("leftrightarrow".
"↔")
20436 ("crarr") ("hookleftarrow".
"↵") ; has round hook, not quite CR
20437 ("lArr") ("Leftarrow".
"⇐")
20438 ("uArr") ("Uparrow".
"⇑")
20439 ("rArr") ("Rightarrow".
"⇒")
20440 ("dArr") ("Downarrow".
"⇓")
20441 ("hArr") ("Leftrightarrow".
"⇔")
20443 ("part") ("partial".
"∂")
20444 ("exist") ("exists".
"∃")
20445 ("empty") ("emptyset".
"∅")
20447 ("isin") ("in".
"∈")
20453 ("lowast") ("ast".
"∗")
20455 ("prop") ("proptp".
"∝")
20456 ("infin") ("infty".
"∞")
20457 ("ang") ("angle".
"∠")
20458 ("and") ("vee".
"∧")
20459 ("or") ("wedge".
"∨")
20465 ("cong") ("simeq".
"≅")
20466 ("asymp")("approx".
"≈")
20467 ("ne") ("neq".
"≠")
20471 ("sub") ("subset".
"⊂")
20472 ("sup") ("supset".
"⊃")
20479 ("sdot") ("cdot".
"⋅")
20486 ("loz") ("Diamond".
"◊")
20487 ("spades") ("spadesuit".
"♠")
20488 ("clubs") ("clubsuit".
"♣")
20489 ("hearts") ("diamondsuit".
"♥")
20490 ("diams") ("diamondsuit".
"♦")
20491 ("smile".
"☺") ("blacksmile".
"☻") ("sad".
"☹")
20525 ("arccos".
"arccos")
20526 ("arcsin".
"arcsin")
20527 ("arctan".
"arctan")
20544 ("liminf".
"liminf")
20545 ("limsup".
"limsup")
20558 "Entities for TeX->HTML translation.
20559 Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
20560 \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
20561 In that case, \"\\ent\" will be translated to \"&other;\".
20562 The list contains HTML entities for Latin-1, Greek and other symbols.
20563 It is supplemented by a number of commonly used TeX macros with appropriate
20564 translations. There is currently no way for users to extend this.")
20566 ;;; General functions for all backends
20568 (defun org-cleaned-string-for-export (string &rest parameters
)
20569 "Cleanup a buffer STRING so that links can be created safely."
20571 (let* ((re-radio (and org-target-link-regexp
20572 (concat "\\([^<]\\)\\(" org-target-link-regexp
"\\)")))
20573 (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re
))
20574 (re-angle-link (concat "\\([^[]\\)" org-angle-link-re
))
20575 (re-archive (concat ":" org-archive-tag
":"))
20576 (re-quote (concat "^\\*+[ \t]+" org-quote-string
"\\>"))
20577 (re-commented (concat "^\\*+[ \t]+" org-comment-string
"\\>"))
20578 (htmlp (plist-get parameters
:for-html
))
20579 (asciip (plist-get parameters
:for-ascii
))
20580 (latexp (plist-get parameters
:for-LaTeX
))
20581 (commentsp (plist-get parameters
:comments
))
20582 (archived-trees (plist-get parameters
:archived-trees
))
20583 (inhibit-read-only t
)
20584 (outline-regexp "\\*+ ")
20587 (with-current-buffer (get-buffer-create " org-mode-tmp")
20590 ;; Remove license-to-kill stuff
20591 (while (setq p
(text-property-any (point-min) (point-max)
20592 :org-license-to-kill t
))
20593 (delete-region p
(next-single-property-change p
:org-license-to-kill
)))
20595 (let ((org-inhibit-startup t
)) (org-mode))
20596 (untabify (point-min) (point-max))
20598 ;; Get the correct stuff before the first headline
20599 (when (plist-get parameters
:skip-before-1st-heading
)
20600 (goto-char (point-min))
20601 (when (re-search-forward "^\\*+[ \t]" nil t
)
20602 (delete-region (point-min) (match-beginning 0))
20603 (goto-char (point-min))
20605 (when (plist-get parameters
:add-text
)
20606 (goto-char (point-min))
20607 (insert (plist-get parameters
:add-text
) "\n"))
20609 ;; Get rid of archived trees
20610 (when (not (eq archived-trees t
))
20611 (goto-char (point-min))
20612 (while (re-search-forward re-archive nil t
)
20613 (if (not (org-on-heading-p t
))
20614 (org-end-of-subtree t
)
20615 (beginning-of-line 1)
20616 (setq a
(if archived-trees
20617 (1+ (point-at-eol)) (point))
20618 b
(org-end-of-subtree t
))
20619 (if (> b a
) (delete-region a b
)))))
20621 ;; Get rid of property drawers
20622 (unless org-export-with-property-drawer
20623 (goto-char (point-min))
20624 (while (re-search-forward "^[ \t]*:PROPERTIES:[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n" nil t
)
20625 (replace-match "")))
20627 ;; Find targets in comments and move them out of comments,
20628 ;; but mark them as targets that should be invisible
20629 (goto-char (point-min))
20630 (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t
)
20631 (replace-match "\\1(INVISIBLE)"))
20633 ;; Protect backend specific stuff, throw away the others.
20634 (goto-char (point-min))
20636 `((,htmlp
"HTML" "BEGIN_HTML" "END_HTML")
20637 (,asciip
"ASCII" "BEGIN_ASCII" "END_ASCII")
20638 (,latexp
"LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
20640 (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t
)
20641 (add-text-properties (match-beginning 0) (match-end 0)
20642 '(org-protected t
)))
20644 (setq fmt
(pop formatters
))
20646 (goto-char (point-min))
20647 (while (re-search-forward (concat "^#\\+" (cadr fmt
)
20648 ":[ \t]*\\(.*\\)") nil t
)
20649 (replace-match "\\1" t
)
20650 (add-text-properties
20651 (point-at-bol) (min (1+ (point-at-eol)) (point-max))
20652 '(org-protected t
))))
20653 (goto-char (point-min))
20654 (while (re-search-forward
20656 (caddr fmt
) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
20657 (cadddr fmt
) "\\>.*\n?") nil t
)
20659 (add-text-properties (match-beginning 1) (1+ (match-end 1))
20660 '(org-protected t
))
20661 (delete-region (match-beginning 0) (match-end 0))))))
20663 ;; Protect quoted subtrees
20664 (goto-char (point-min))
20665 (while (re-search-forward re-quote nil t
)
20666 (goto-char (match-beginning 0))
20668 (add-text-properties (point) (org-end-of-subtree t
)
20669 '(org-protected t
)))
20671 ;; Remove subtrees that are commented
20672 (goto-char (point-min))
20673 (while (re-search-forward re-commented nil t
)
20674 (goto-char (match-beginning 0))
20675 (delete-region (point) (org-end-of-subtree t
)))
20677 ;; Remove special table lines
20678 (when org-export-table-remove-special-lines
20679 (goto-char (point-min))
20680 (while (re-search-forward "^[ \t]*|" nil t
)
20681 (beginning-of-line 1)
20682 (if (or (looking-at "[ \t]*| *[!_^] *|")
20683 (and (looking-at ".*?| *<[0-9]+> *|")
20684 (not (looking-at ".*?| *[^ <|]"))))
20685 (delete-region (max (point-min) (1- (point-at-bol)))
20689 ;; Specific LaTeX stuff
20691 (require 'org-export-latex nil t
)
20692 (org-export-latex-cleaned-string))
20694 ;; Specific HTML stuff
20696 ;; Convert LaTeX fragments to images
20697 (when (plist-get parameters
:LaTeX-fragments
)
20699 (concat "ltxpng/" (file-name-sans-extension
20700 (file-name-nondirectory
20701 org-current-export-file
)))
20702 org-current-export-dir nil
"Creating LaTeX image %s"))
20703 (message "Exporting..."))
20705 ;; Remove or replace comments
20706 ;; FIXME: Does LaTeX export take care of its own comments?
20707 ;; If :comments is set, use this char for commenting out comments and
20708 ;; protect them. otherwise delete them
20709 (goto-char (point-min))
20710 (while (re-search-forward "^#\\(.*\n?\\)" nil t
)
20712 (progn (add-text-properties
20713 (match-beginning 0) (match-end 0) '(org-protected t
))
20714 (replace-match (format commentsp
(match-string 1)) t t
))
20715 (replace-match "")))
20717 ;; Find matches for radio targets and turn them into internal links
20718 (goto-char (point-min))
20720 (while (re-search-forward re-radio nil t
)
20721 (org-if-unprotected
20722 (replace-match "\\1[[\\2]]"))))
20724 ;; Find all links that contain a newline and put them into a single line
20725 (goto-char (point-min))
20726 (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t
)
20727 (org-if-unprotected
20728 (replace-match "\\1 \\3")
20729 (goto-char (match-beginning 0))))
20732 ;; Normalize links: Convert angle and plain links into bracket links
20733 ;; Expand link abbreviations
20734 (goto-char (point-min))
20735 (while (re-search-forward re-plain-link nil t
)
20736 (goto-char (1- (match-end 0)))
20737 (org-if-unprotected
20738 (let* ((s (concat (match-string 1) "[[" (match-string 2)
20739 ":" (match-string 3) "]]")))
20740 ;; added 'org-link face to links
20741 (put-text-property 0 (length s
) 'face
'org-link s
)
20742 (replace-match s t t
))))
20743 (goto-char (point-min))
20744 (while (re-search-forward re-angle-link nil t
)
20745 (goto-char (1- (match-end 0)))
20746 (org-if-unprotected
20747 (let* ((s (concat (match-string 1) "[[" (match-string 2)
20748 ":" (match-string 3) "]]")))
20749 (put-text-property 0 (length s
) 'face
'org-link s
)
20750 (replace-match s t t
))))
20751 (goto-char (point-min))
20752 (while (re-search-forward org-bracket-link-regexp nil t
)
20753 (org-if-unprotected
20754 (let* ((s (concat "[[" (setq xx
(save-match-data
20755 (org-link-expand-abbrev (match-string 1))))
20759 (concat "[" xx
"]"))
20761 (put-text-property 0 (length s
) 'face
'org-link s
)
20762 (replace-match s t t
))))
20764 ;; Find multiline emphasis and put them into single line
20765 (when (plist-get parameters
:emph-multiline
)
20766 (goto-char (point-min))
20767 (while (re-search-forward org-emph-re nil t
)
20768 (if (not (= (char-after (match-beginning 3))
20769 (char-after (match-beginning 4))))
20770 (org-if-unprotected
20771 (subst-char-in-region (match-beginning 0) (match-end 0)
20773 (goto-char (1- (match-end 0))))
20774 (goto-char (1+ (match-beginning 0))))))
20776 (setq rtn
(buffer-string)))
20777 (kill-buffer " org-mode-tmp")
20780 (defun org-export-grab-title-from-buffer ()
20781 "Get a title for the current document, from looking at the buffer."
20782 (let ((inhibit-read-only t
))
20784 (goto-char (point-min))
20785 (let ((end (save-excursion (outline-next-heading) (point))))
20786 (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t
)
20787 ;; Mark the line so that it will not be exported as normal text.
20789 (add-text-properties (match-beginning 0) (match-end 0)
20790 (list :org-license-to-kill t
)))
20791 ;; Return the title string
20792 (org-trim (match-string 0)))))))
20794 (defun org-export-get-title-from-subtree ()
20795 "Return subtree title and exclude it from export."
20796 (let (title (m (mark)))
20798 (goto-char (region-beginning))
20799 (when (and (org-at-heading-p)
20800 (>= (org-end-of-subtree t t
) (region-end)))
20801 ;; This is a subtree, we take the title from the first heading
20802 (goto-char (region-beginning))
20803 (looking-at org-todo-line-regexp
)
20804 (setq title
(match-string 3))
20806 (add-text-properties (point) (1+ (point-at-eol))
20807 (list :org-license-to-kill t
)))))
20810 (defun org-solidify-link-text (s &optional alist
)
20811 "Take link text and make a safe target out of it."
20816 (org-split-string s
"[ \t\r\n]+") "--"))
20817 (a (assoc rtn alist
)))
20818 (or (cdr a
) rtn
))))
20820 ;; Variable holding the vector with section numbers
20821 (defvar org-section-numbers
(make-vector org-level-max
0))
20823 (defun org-init-section-numbers ()
20824 "Initialize the vector for the section numbers."
20826 (numbers (nreverse (org-split-string "" "\\.")))
20827 (depth (1- (length org-section-numbers
)))
20828 (i depth
) number-string
)
20831 (aset org-section-numbers i
0)
20832 (setq number-string
(or (car numbers
) "0"))
20833 (if (string-match "\\`[A-Z]\\'" number-string
)
20834 (aset org-section-numbers i
20835 (- (string-to-char number-string
) ?A -
1))
20836 (aset org-section-numbers i
(string-to-number number-string
)))
20840 (defun org-section-number (&optional level
)
20841 "Return a string with the current section number.
20842 When LEVEL is non-nil, increase section numbers on that level."
20843 (let* ((depth (1- (length org-section-numbers
))) idx n
(string ""))
20846 (aset org-section-numbers
20847 level
(1+ (aref org-section-numbers level
))))
20848 (setq idx
(1+ level
))
20849 (while (<= idx depth
)
20850 (if (not (= idx
1))
20851 (aset org-section-numbers idx
0))
20852 (setq idx
(1+ idx
))))
20854 (while (<= idx depth
)
20855 (setq n
(aref org-section-numbers idx
))
20856 (setq string
(concat string
(if (not (string= string
"")) "." "")
20857 (int-to-string n
)))
20858 (setq idx
(1+ idx
)))
20860 (if (string-match "\\`\\([@0]\\.\\)+" string
)
20861 (setq string
(replace-match "" t nil string
)))
20862 (if (string-match "\\(\\.0\\)+\\'" string
)
20863 (setq string
(replace-match "" t nil string
))))
20868 (defvar org-last-level nil
) ; dynamically scoped variable
20869 (defvar org-min-level nil
) ; dynamically scoped variable
20870 (defvar org-levels-open nil
) ; dynamically scoped parameter
20871 (defvar org-ascii-current-indentation nil
) ; For communication
20873 (defun org-export-as-ascii (arg)
20874 "Export the outline as a pretty ASCII file.
20875 If there is an active region, export only the region.
20876 The prefix ARG specifies how many levels of the outline should become
20877 underlined headlines. The default is 3."
20879 (setq-default org-todo-line-regexp org-todo-line-regexp
)
20880 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
20881 (org-infile-export-plist)))
20882 (region-p (org-region-active-p))
20886 (goto-char (region-beginning))
20887 (and (org-at-heading-p)
20888 (>= (org-end-of-subtree t t
) (region-end))))))
20889 (custom-times org-display-custom-times
)
20890 (org-ascii-current-indentation '(0 .
0))
20894 (case-fold-search nil
)
20895 (filename (concat (file-name-as-directory
20896 (org-export-directory :ascii opt-plist
))
20897 (file-name-sans-extension
20899 (org-entry-get (region-beginning)
20900 "EXPORT_FILE_NAME" t
))
20901 (file-name-nondirectory buffer-file-name
)))
20903 (filename (if (equal (file-truename filename
)
20904 (file-truename buffer-file-name
))
20905 (concat filename
".txt")
20907 (buffer (find-file-noselect filename
))
20908 (org-levels-open (make-vector org-level-max nil
))
20909 (odd org-odd-levels-only
)
20910 (date (format-time-string "%Y/%m/%d" (current-time)))
20911 (time (format-time-string "%X" (org-current-time)))
20912 (author (plist-get opt-plist
:author
))
20913 (title (or (and subtree-p
(org-export-get-title-from-subtree))
20914 (plist-get opt-plist
:title
)
20916 (plist-get opt-plist
:skip-before-1st-heading
))
20917 (org-export-grab-title-from-buffer))
20918 (file-name-sans-extension
20919 (file-name-nondirectory buffer-file-name
))))
20920 (email (plist-get opt-plist
:email
))
20921 (language (plist-get opt-plist
:language
))
20922 (quote-re0 (concat "^[ \t]*" org-quote-string
"\\>"))
20923 ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
20928 (if (org-region-active-p) (region-beginning) (point-min))
20929 (if (org-region-active-p) (region-end) (point-max))))
20930 (lines (org-split-string
20931 (org-cleaned-string-for-export
20934 :skip-before-1st-heading
20935 (plist-get opt-plist
:skip-before-1st-heading
)
20937 (plist-get opt-plist
:archived-trees
)
20938 :add-text
(plist-get opt-plist
:text
))
20939 "[\r\n]")) ;; FIXME: why \r here???/
20940 thetoc have-headings first-heading-pos
20941 table-open table-buffer
)
20943 (let ((inhibit-read-only t
))
20945 (remove-text-properties (point-min) (point-max)
20946 '(:org-license-to-kill t
))))
20948 (setq org-min-level
(org-get-min-level lines
))
20949 (setq org-last-level org-min-level
)
20950 (org-init-section-numbers)
20952 (find-file-noselect filename
)
20954 (setq lang-words
(or (assoc language org-export-language-setup
)
20955 (assoc "en" org-export-language-setup
)))
20956 (switch-to-buffer-other-window buffer
)
20959 ;; create local variables for all options, to make sure all called
20960 ;; functions get the correct information
20961 (mapcar (lambda (x)
20962 (set (make-local-variable (cdr x
))
20963 (plist-get opt-plist
(car x
))))
20964 org-export-plist-vars
)
20965 (org-set-local 'org-odd-levels-only odd
)
20966 (setq umax
(if arg
(prefix-numeric-value arg
)
20967 org-export-headline-levels
))
20968 (setq umax-toc
(if (integerp org-export-with-toc
)
20969 (min org-export-with-toc umax
)
20973 (if title
(org-insert-centered title ?
=))
20975 (if (and (or author email
)
20976 org-export-author-info
)
20977 (insert (concat (nth 1 lang-words
) ": " (or author
"")
20978 (if email
(concat " <" email
">") "")
20980 (if (and date time org-export-time-stamp-file
)
20981 (insert (concat (nth 2 lang-words
) ": " date
" " time
"\n")))
20985 (if org-export-with-toc
20987 (push (concat (nth 3 lang-words
) "\n") thetoc
)
20988 (push (concat (make-string (length (nth 3 lang-words
)) ?
=) "\n") thetoc
)
20989 (mapcar '(lambda (line)
20990 (if (string-match org-todo-line-regexp
20992 ;; This is a headline
20994 (setq have-headings t
)
20995 (setq level
(- (match-end 1) (match-beginning 1))
20996 level
(org-tr-level level
)
20997 txt
(match-string 3 line
)
20999 (or (and org-export-mark-todo-in-toc
21000 (match-beginning 2)
21001 (not (member (match-string 2 line
)
21002 org-done-keywords
)))
21004 (and org-export-mark-todo-in-toc
21006 (org-search-todo-below
21007 line lines level
))))
21008 (setq txt
(org-html-expand-for-ascii txt
))
21010 (if (and (memq org-export-with-tags
'(not-in-toc nil
))
21012 (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
21014 (setq txt
(replace-match "" t t txt
)))
21015 (if (string-match quote-re0 txt
)
21016 (setq txt
(replace-match "" t t txt
)))
21018 (if org-export-with-section-numbers
21019 (setq txt
(concat (org-section-number level
)
21021 (if (<= level umax-toc
)
21026 (* (max 0 (- level org-min-level
)) 4) ?\
)
21027 (format (if todo
"%s (*)\n" "%s\n") txt
))
21029 (setq org-last-level level
))
21032 (setq thetoc
(if have-headings
(nreverse thetoc
) nil
))))
21034 (org-init-section-numbers)
21035 (while (setq line
(pop lines
))
21036 ;; Remove the quoted HTML tags.
21037 (setq line
(org-html-expand-for-ascii line
))
21039 (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line
)
21040 (setq line
(replace-match "" t t line
)))
21041 ;; Replace internal links
21042 (while (string-match org-bracket-link-regexp line
)
21043 (setq line
(replace-match
21044 (if (match-end 3) "[\\3]" "[\\1]")
21047 (setq line
(org-translate-time line
)))
21049 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line
)
21051 (setq first-heading-pos
(or first-heading-pos
(point)))
21052 (setq level
(org-tr-level (- (match-end 1) (match-beginning 1)))
21053 txt
(match-string 2 line
))
21054 (org-ascii-level-start level txt umax lines
))
21056 ((and org-export-with-tables
21057 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line
))
21058 (if (not table-open
)
21059 ;; New table starts
21060 (setq table-open t table-buffer nil
))
21061 ;; Accumulate lines
21062 (setq table-buffer
(cons line table-buffer
))
21063 (when (or (not lines
)
21064 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
21066 (setq table-open nil
21067 table-buffer
(nreverse table-buffer
))
21070 (org-fix-indentation x org-ascii-current-indentation
))
21071 (org-format-table-ascii table-buffer
)
21074 (setq line
(org-fix-indentation line org-ascii-current-indentation
))
21075 (if (and org-export-with-fixed-width
21076 (string-match "^\\([ \t]*\\)\\(:\\)" line
))
21077 (setq line
(replace-match "\\1" nil nil line
)))
21078 (insert line
"\n"))))
21082 ;; insert the table of contents
21084 (goto-char (point-min))
21085 (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t
)
21087 (goto-char (match-beginning 0))
21088 (replace-match ""))
21089 (goto-char first-heading-pos
))
21090 (mapc 'insert thetoc
)
21091 (or (looking-at "[ \t]*\n[ \t]*\n")
21095 ;; remove display and invisible chars
21097 (goto-char (point-min))
21098 (while (setq beg
(next-single-property-change (point) 'display
))
21099 (setq end
(next-single-property-change beg
'display
))
21100 (delete-region beg end
)
21103 (goto-char (point-min))
21104 (while (setq beg
(next-single-property-change (point) 'org-cwidth
))
21105 (setq end
(next-single-property-change beg
'org-cwidth
))
21106 (delete-region beg end
)
21108 (goto-char (point-min))))
21110 (defun org-search-todo-below (line lines level
)
21111 "Search the subtree below LINE for any TODO entries."
21112 (let ((rest (cdr (memq line lines
)))
21113 (re org-todo-line-regexp
)
21116 (while (setq line
(pop rest
))
21117 (if (string-match re line
)
21119 (setq lv
(- (match-end 1) (match-beginning 1))
21120 todo
(and (match-beginning 2)
21121 (not (member (match-string 2 line
)
21122 org-done-keywords
))))
21124 (if (<= lv level
) (throw 'exit nil
))
21125 (if todo
(throw 'exit t
))))))))
21127 (defun org-html-expand-for-ascii (line)
21128 "Handle quoted HTML for ASCII export."
21129 (if org-export-html-expand
21130 (while (string-match "@<[^<>\n]*>" line
)
21131 ;; We just remove the tags for now.
21132 (setq line
(replace-match "" nil nil line
))))
21135 (defun org-insert-centered (s &optional underline
)
21136 "Insert the string S centered and underline it with character UNDERLINE."
21137 (let ((ind (max (/ (- 80 (string-width s
)) 2) 0)))
21138 (insert (make-string ind ?\
) s
"\n")
21140 (insert (make-string ind ?\
)
21141 (make-string (string-width s
) underline
)
21144 (defun org-ascii-level-start (level title umax
&optional lines
)
21145 "Insert a new level in ASCII export."
21146 (let (char (n (- level umax
1)) (ind 0))
21149 (insert (make-string (* 2 n
) ?\
)
21150 (char-to-string (nth (% n
(length org-export-ascii-bullets
))
21151 org-export-ascii-bullets
))
21153 ;; find the indentation of the next non-empty line
21156 (if (string-match "^\\* " (car lines
)) (throw 'stop nil
))
21157 (if (string-match "^\\([ \t]*\\)\\S-" (car lines
))
21158 (throw 'stop
(setq ind
(org-get-indentation (car lines
)))))
21160 (setq org-ascii-current-indentation
(cons (* 2 (1+ n
)) ind
)))
21161 (if (or (not (equal (char-before) ?
\n))
21162 (not (equal (char-before (1- (point))) ?
\n)))
21164 (setq char
(nth (- umax level
) (reverse org-export-ascii-underline
)))
21165 (unless org-export-with-tags
21166 (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title
)
21167 (setq title
(replace-match "" t t title
))))
21168 (if org-export-with-section-numbers
21169 (setq title
(concat (org-section-number level
) " " title
)))
21170 (insert title
"\n" (make-string (string-width title
) char
) "\n")
21171 (setq org-ascii-current-indentation
'(0 .
0)))))
21173 (defun org-export-visible (type arg
)
21174 "Create a copy of the visible part of the current buffer, and export it.
21175 The copy is created in a temporary buffer and removed after use.
21176 TYPE is the final key (as a string) that also select the export command in
21177 the `C-c C-e' export dispatcher.
21178 As a special case, if the you type SPC at the prompt, the temporary
21179 org-mode file will not be removed but presented to you so that you can
21180 continue to use it. The prefix arg ARG is passed through to the exporting
21184 (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
21185 (read-char-exclusive))
21186 current-prefix-arg
))
21187 (if (not (member type
'(?a ?\C-a ?b ?\C-b ?h ?x ?\
)))
21188 (error "Invalid export key"))
21189 (let* ((binding (cdr (assoc type
21190 '((?a . org-export-as-ascii
)
21191 (?\C-a . org-export-as-ascii
)
21192 (?b . org-export-as-html-and-open
)
21193 (?\C-b . org-export-as-html-and-open
)
21194 (?h . org-export-as-html
)
21195 (?H . org-export-as-html-to-buffer
)
21196 (?R . org-export-region-as-html
)
21197 (?x . org-export-as-xoxo
)))))
21198 (keepp (equal type ?\
))
21199 (file buffer-file-name
)
21200 (buffer (get-buffer-create "*Org Export Visible*"))
21202 (with-current-buffer buffer
(erase-buffer))
21204 (setq s
(goto-char (point-min)))
21205 (while (not (= (point) (point-max)))
21206 (goto-char (org-find-invisible))
21207 (append-to-buffer buffer s
(point))
21208 (setq s
(goto-char (org-find-visible))))
21209 (goto-char (point-min))
21211 ;; Copy all comment lines to the end, to make sure #+ settings are
21212 ;; still available for the second export step. Kind of a hack, but
21213 ;; does do the trick.
21214 (if (looking-at "#[^\r\n]*")
21215 (append-to-buffer buffer
(match-beginning 0) (1+ (match-end 0))))
21216 (while (re-search-forward "[\n\r]#[^\n\r]*" nil t
)
21217 (append-to-buffer buffer
(1+ (match-beginning 0))
21218 (min (point-max) (1+ (match-end 0))))))
21219 (set-buffer buffer
)
21220 (let ((buffer-file-name file
)
21221 (org-inhibit-startup t
))
21224 (unless keepp
(funcall binding arg
))))
21226 (kill-buffer buffer
)
21227 (switch-to-buffer-other-window buffer
)
21228 (goto-char (point-min)))))
21230 (defun org-find-visible ()
21232 (while (and (not (= (point-max) (setq s
(next-overlay-change s
))))
21233 (get-char-property s
'invisible
)))
21235 (defun org-find-invisible ()
21237 (while (and (not (= (point-max) (setq s
(next-overlay-change s
))))
21238 (not (get-char-property s
'invisible
))))
21243 (defun org-get-current-options ()
21244 "Return a string with current options as keyword options.
21245 Does include HTML export options as well as TODO and CATEGORY stuff."
21251 #+TEXT: Some descriptive text to be emitted. Several lines OK.
21252 #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s f:%s *:%s TeX:%s LaTeX:%s skip:%s p:%s
21256 #+PRIORITIES: %c %c %c
21257 #+STARTUP: %s %s %s %s %s
21262 (buffer-name) (user-full-name) user-mail-address org-export-default-language
21263 org-export-headline-levels
21264 org-export-with-section-numbers
21265 org-export-with-toc
21266 org-export-preserve-breaks
21267 org-export-html-expand
21268 org-export-with-fixed-width
21269 org-export-with-tables
21270 org-export-with-sub-superscripts
21271 org-export-with-footnotes
21272 org-export-with-emphasize
21273 org-export-with-TeX-macros
21274 org-export-with-LaTeX-fragments
21275 org-export-skip-text-before-1st-heading
21276 org-export-with-property-drawer
21277 (file-name-nondirectory buffer-file-name
)
21278 "TODO FEEDBACK VERIFY DONE"
21279 "Me Jason Marie DONE"
21280 org-highest-priority org-lowest-priority org-default-priority
21281 (cdr (assoc org-startup-folded
21282 '((nil .
"showall") (t .
"overview") (content .
"content"))))
21283 (if org-odd-levels-only
"odd" "oddeven")
21284 (if org-hide-leading-stars
"hidestars" "showstars")
21285 (if org-startup-align-all-tables
"align" "noalign")
21286 (cond ((eq t org-log-done
) "logdone")
21287 ((not org-log-done
) "nologging")
21288 ((listp org-log-done
)
21289 (mapconcat (lambda (x) (concat "lognote" (symbol-name x
)))
21290 org-log-done
" ")))
21291 (or (mapconcat (lambda (x)
21293 ((equal '(:startgroup
) x
) "{")
21294 ((equal '(:endgroup
) x
) "}")
21295 ((cdr x
) (format "%s(%c)" (car x
) (cdr x
)))
21297 (or org-tag-alist
(org-get-buffer-tags)) " ") "")
21298 org-archive-location
21299 "org file:~/org/%s.org"
21302 (defun org-insert-export-options-template ()
21303 "Insert into the buffer a template with information for exporting."
21305 (if (not (bolp)) (newline))
21306 (let ((s (org-get-current-options)))
21307 (and (string-match "#\\+CATEGORY" s
)
21308 (setq s
(substring s
0 (match-beginning 0))))
21311 (defun org-toggle-fixed-width-section (arg)
21312 "Toggle the fixed-width export.
21313 If there is no active region, the QUOTE keyword at the current headline is
21314 inserted or removed. When present, it causes the text between this headline
21315 and the next to be exported as fixed-width text, and unmodified.
21316 If there is an active region, this command adds or removes a colon as the
21317 first character of this line. If the first character of a line is a colon,
21318 this line is also exported in fixed-width font."
21321 (regionp (org-region-active-p))
21322 (beg (if regionp
(region-beginning) (point)))
21323 (end (if regionp
(region-end)))
21324 (nlines (or arg
(if (and beg end
) (count-lines beg end
) 1)))
21325 (re "[ \t]*\\(:\\)")
21330 (setq cc
(current-column))
21331 (beginning-of-line 1)
21332 (setq off
(looking-at re
))
21333 (while (> nlines
0)
21334 (setq nlines
(1- nlines
))
21335 (beginning-of-line 1)
21338 (move-to-column cc t
)
21341 ((and off
(looking-at re
))
21342 (replace-match "" t t nil
1))
21343 ((not off
) (move-to-column cc t
) (insert ":")))
21346 (org-back-to-heading)
21347 (if (looking-at (concat outline-regexp
21348 "\\( *\\<" org-quote-string
"\\>\\)"))
21349 (replace-match "" t t nil
1)
21350 (if (looking-at outline-regexp
)
21352 (goto-char (match-end 0))
21353 (insert org-quote-string
" "))))))))
21355 (defun org-export-as-html-and-open (arg)
21356 "Export the outline as HTML and immediately open it with a browser.
21357 If there is an active region, export only the region.
21358 The prefix ARG specifies how many levels of the outline should become
21359 headlines. The default is 3. Lower levels will become bulleted lists."
21361 (org-export-as-html arg
'hidden
)
21362 (org-open-file buffer-file-name
))
21364 (defun org-export-as-html-batch ()
21365 "Call `org-export-as-html', may be used in batch processing as
21367 --load=$HOME/lib/emacs/org.el
21368 --eval \"(setq org-export-headline-levels 2)\"
21369 --visit=MyFile --funcall org-export-as-html-batch"
21370 (org-export-as-html org-export-headline-levels
'hidden
))
21372 (defun org-export-as-html-to-buffer (arg)
21373 "Call `org-exort-as-html` with output to a temporary buffer.
21374 No file is created. The prefix ARG is passed through to `org-export-as-html'."
21376 (org-export-as-html arg nil nil
"*Org HTML Export*")
21377 (switch-to-buffer-other-window "*Org HTML Export*"))
21379 (defun org-replace-region-by-html (beg end
)
21380 "Assume the current region has org-mode syntax, and convert it to HTML.
21381 This can be used in any buffer. For example, you could write an
21382 itemized list in org-mode syntax in an HTML buffer and then use this
21383 command to convert it."
21385 (let (reg html buf
)
21386 (save-window-excursion
21388 (setq html
(org-export-region-as-html
21389 beg end t
'string
))
21390 (setq reg
(buffer-substring beg end
)
21391 buf
(get-buffer-create "*Org tmp*"))
21392 (with-current-buffer buf
21396 (setq html
(org-export-region-as-html
21397 (point-min) (point-max) t
'string
)))
21398 (kill-buffer buf
)))
21399 (delete-region beg end
)
21402 (defun org-export-region-as-html (beg end
&optional body-only buffer
)
21403 "Convert region from BEG to END in org-mode buffer to HTML.
21404 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
21405 contents, and only produce the region of converted text, useful for
21406 cut-and-paste operations.
21407 If BUFFER is a buffer or a string, use/create that buffer as a target
21408 of the converted HTML. If BUFFER is the symbol `string', return the
21409 produced HTML as a string and leave not buffer behind. For example,
21410 a Lisp program could call this function in the following way:
21412 (setq html (org-export-region-as-html beg end t 'string))
21414 When called interactively, the output buffer is selected, and shown
21415 in a window. A non-interactive call will only retunr the buffer."
21416 (interactive "r\nP")
21417 (when (interactive-p)
21418 (setq buffer
"*Org HTML Export*"))
21419 (let ((transient-mark-mode t
) (zmacs-regions t
)
21422 (set-mark (point)) ;; to activate the region
21424 (setq rtn
(org-export-as-html
21427 (if (fboundp 'deactivate-mark
) (deactivate-mark))
21428 (if (and (interactive-p) (bufferp rtn
))
21429 (switch-to-buffer-other-window rtn
)
21432 (defun org-export-as-html (arg &optional hidden ext-plist
21433 to-buffer body-only
)
21434 "Export the outline as a pretty HTML file.
21435 If there is an active region, export only the region. The prefix
21436 ARG specifies how many levels of the outline should become
21437 headlines. The default is 3. Lower levels will become bulleted
21438 lists. When HIDDEN is non-nil, don't display the HTML buffer.
21439 EXT-PLIST is a property list with external parameters overriding
21440 org-mode's default settings, but still inferior to file-local
21441 settings. When TO-BUFFER is non-nil, create a buffer with that
21442 name and export to that buffer. If TO-BUFFER is the symbol `string',
21443 don't leave any buffer behind but just return the resulting HTML as
21444 a string. When BODY-ONLY is set, don't produce the file header and footer,
21445 simply return the content of <body>...</body>, without even
21446 the body tags themselves."
21449 ;; Make sure we have a file name when we need it.
21450 (when (and (not (or to-buffer body-only
))
21451 (not buffer-file-name
))
21452 (if (buffer-base-buffer)
21453 (org-set-local 'buffer-file-name
21454 (with-current-buffer (buffer-base-buffer)
21456 (error "Need a file name to be able to export.")))
21458 (message "Exporting...")
21459 (setq-default org-todo-line-regexp org-todo-line-regexp
)
21460 (setq-default org-deadline-line-regexp org-deadline-line-regexp
)
21461 (setq-default org-done-keywords org-done-keywords
)
21462 (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp
)
21463 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
21465 (org-infile-export-plist)))
21467 (style (plist-get opt-plist
:style
))
21468 (link-validate (plist-get opt-plist
:link-validation-function
))
21469 valid thetoc have-headings first-heading-pos
21470 (odd org-odd-levels-only
)
21471 (region-p (org-region-active-p))
21475 (goto-char (region-beginning))
21476 (and (org-at-heading-p)
21477 (>= (org-end-of-subtree t t
) (region-end))))))
21478 ;; The following two are dynamically scoped into other
21480 (org-current-export-dir (org-export-directory :html opt-plist
))
21481 (org-current-export-file buffer-file-name
)
21482 (level 0) (line "") (origline "") txt todo
21485 (filename (if to-buffer nil
21486 (concat (file-name-as-directory
21487 (org-export-directory :html opt-plist
))
21488 (file-name-sans-extension
21490 (org-entry-get (region-beginning)
21491 "EXPORT_FILE_NAME" t
))
21492 (file-name-nondirectory buffer-file-name
)))
21494 (current-dir (if buffer-file-name
21495 (file-name-directory buffer-file-name
)
21496 default-directory
))
21497 (buffer (if to-buffer
21499 ((eq to-buffer
'string
) (get-buffer-create "*Org HTML Export*"))
21500 (t (get-buffer-create to-buffer
)))
21501 (find-file-noselect filename
)))
21502 (org-levels-open (make-vector org-level-max nil
))
21503 (date (format-time-string "%Y/%m/%d" (current-time)))
21504 (time (format-time-string "%X" (org-current-time)))
21505 (author (plist-get opt-plist
:author
))
21506 (title (or (and subtree-p
(org-export-get-title-from-subtree))
21507 (plist-get opt-plist
:title
)
21509 (plist-get opt-plist
:skip-before-1st-heading
))
21510 (org-export-grab-title-from-buffer))
21511 (and buffer-file-name
21512 (file-name-sans-extension
21513 (file-name-nondirectory buffer-file-name
)))
21515 (quote-re0 (concat "^[ \t]*" org-quote-string
"\\>"))
21516 (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string
"\\>\\)"))
21519 (in-local-list nil
)
21520 (local-list-num nil
)
21521 (local-list-indent nil
)
21522 (llt org-plain-list-ordered-item-terminator
)
21523 (email (plist-get opt-plist
:email
))
21524 (language (plist-get opt-plist
:language
))
21526 (target-alist nil
) tg
21529 (coding-system (and (boundp 'buffer-file-coding-system
)
21530 buffer-file-coding-system
))
21531 (coding-system-for-write (or org-export-html-coding-system
21533 (save-buffer-coding-system (or org-export-html-coding-system
21535 (charset (and coding-system-for-write
21536 (fboundp 'coding-system-get
)
21537 (coding-system-get coding-system-for-write
21541 (if region-p
(region-beginning) (point-min))
21542 (if region-p
(region-end) (point-max))))
21545 (org-cleaned-string-for-export
21549 :skip-before-1st-heading
21550 (plist-get opt-plist
:skip-before-1st-heading
)
21552 (plist-get opt-plist
:archived-trees
)
21554 (plist-get opt-plist
:text
)
21556 (plist-get opt-plist
:LaTeX-fragments
))
21559 table-buffer table-orig-buffer
21560 ind start-is-num starter didclose
21561 rpl path desc descp desc1 desc2 link
21564 (let ((inhibit-read-only t
))
21566 (remove-text-properties (point-min) (point-max)
21567 '(:org-license-to-kill t
))))
21569 (message "Exporting...")
21571 (setq org-min-level
(org-get-min-level lines
))
21572 (setq org-last-level org-min-level
)
21573 (org-init-section-numbers)
21575 ;; Get the language-dependent settings
21576 (setq lang-words
(or (assoc language org-export-language-setup
)
21577 (assoc "en" org-export-language-setup
)))
21579 ;; Switch to the output buffer
21580 (set-buffer buffer
)
21584 (and (fboundp 'set-buffer-file-coding-system
)
21585 (set-buffer-file-coding-system coding-system-for-write
))
21587 (let ((case-fold-search nil
)
21588 (org-odd-levels-only odd
))
21589 ;; create local variables for all options, to make sure all called
21590 ;; functions get the correct information
21591 (mapcar (lambda (x)
21592 (set (make-local-variable (cdr x
))
21593 (plist-get opt-plist
(car x
))))
21594 org-export-plist-vars
)
21595 (setq umax
(if arg
(prefix-numeric-value arg
)
21596 org-export-headline-levels
))
21597 (setq umax-toc
(if (integerp org-export-with-toc
)
21598 (min org-export-with-toc umax
)
21603 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
21604 \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
21605 <html xmlns=\"http://www.w3.org/1999/xhtml\"
21606 lang=\"%s\" xml:lang=\"%s\">
21609 <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
21610 <meta name=\"generator\" content=\"Org-mode\"/>
21611 <meta name=\"generated\" content=\"%s %s\"/>
21612 <meta name=\"author\" content=\"%s\"/>
21616 language language
(org-html-expand title
)
21617 (or charset
"iso-8859-1") date time author style
))
21619 (insert (or (plist-get opt-plist
:preamble
) ""))
21621 (when (plist-get opt-plist
:auto-preamble
)
21622 (if title
(insert (format org-export-html-title-format
21623 (org-html-expand title
))))))
21625 (if (and org-export-with-toc
(not body-only
))
21627 (push (format "<h%d>%s</h%d>\n"
21628 org-export-html-toplevel-hlevel
21630 org-export-html-toplevel-hlevel
)
21632 (push "<ul>\n<li>" thetoc
)
21634 (mapcar '(lambda (line)
21635 (if (string-match org-todo-line-regexp line
)
21636 ;; This is a headline
21638 (setq have-headings t
)
21639 (setq level
(- (match-end 1) (match-beginning 1))
21640 level
(org-tr-level level
)
21641 txt
(save-match-data
21643 (org-export-cleanup-toc-line
21644 (match-string 3 line
))))
21646 (or (and org-export-mark-todo-in-toc
21647 (match-beginning 2)
21648 (not (member (match-string 2 line
)
21649 org-done-keywords
)))
21651 (and org-export-mark-todo-in-toc
21653 (org-search-todo-below
21654 line lines level
))))
21655 (if (and (memq org-export-with-tags
'(not-in-toc nil
))
21657 (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
21659 (setq txt
(replace-match "" t t txt
)))
21660 (if (string-match quote-re0 txt
)
21661 (setq txt
(replace-match "" t t txt
)))
21662 (if org-export-with-section-numbers
21663 (setq txt
(concat (org-section-number level
)
21665 (if (<= level
(max umax umax-toc
))
21666 (setq head-count
(+ head-count
1)))
21667 (if (<= level umax-toc
)
21669 (if (> level org-last-level
)
21671 (setq cnt
(- level org-last-level
))
21672 (while (>= (setq cnt
(1- cnt
)) 0)
21673 (push "\n<ul>\n<li>" thetoc
))
21674 (push "\n" thetoc
)))
21675 (if (< level org-last-level
)
21677 (setq cnt
(- org-last-level level
))
21678 (while (>= (setq cnt
(1- cnt
)) 0)
21679 (push "</li>\n</ul>" thetoc
))
21680 (push "\n" thetoc
)))
21681 ;; Check for targets
21682 (while (string-match org-target-regexp line
)
21683 (setq tg
(match-string 1 line
)
21684 line
(replace-match
21685 (concat "@<span class=\"target\">" tg
"@</span> ")
21687 (push (cons (org-solidify-link-text tg
)
21688 (format "sec-%d" head-count
))
21690 (while (string-match "<\\(<\\)+\\|>\\(>\\)+" txt
)
21691 (setq txt
(replace-match "" t t txt
)))
21695 "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
21696 "</li>\n<li><a href=\"#sec-%d\">%s</a>")
21697 head-count txt
) thetoc
)
21699 (setq org-last-level level
))
21703 (while (> org-last-level
(1- org-min-level
))
21704 (setq org-last-level
(1- org-last-level
))
21705 (push "</li>\n</ul>\n" thetoc
))
21706 (setq thetoc
(if have-headings
(nreverse thetoc
) nil
))))
21708 (setq head-count
0)
21709 (org-init-section-numbers)
21711 (while (setq line
(pop lines
) origline line
)
21714 ;; end of quote section?
21715 (when (and inquote
(string-match "^\\*+ " line
))
21716 (insert "</pre>\n")
21717 (setq inquote nil
))
21718 ;; inside a quote section?
21720 (insert (org-html-protect line
) "\n")
21721 (throw 'nextline nil
))
21724 (when (and org-export-with-fixed-width
21725 (string-match "^[ \t]*:\\(.*\\)" line
))
21726 (when (not infixed
)
21728 (insert "<pre>\n"))
21729 (insert (org-html-protect (match-string 1 line
)) "\n")
21731 (not (string-match "^[ \t]*\\(:.*\\)"
21734 (insert "</pre>\n"))
21735 (throw 'nextline nil
))
21738 (when (get-text-property 0 'org-protected line
)
21740 (when (re-search-backward
21741 "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t
)
21742 (setq par
(match-string 1))
21743 (replace-match "\\2\n"))
21746 (get-text-property 0 'org-protected
(car lines
)))
21747 (insert (pop lines
) "\n"))
21748 (and par
(insert "<p>\n")))
21749 (throw 'nextline nil
))
21752 (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line
)
21753 (insert "\n<hr/>\n")
21754 (throw 'nextline nil
))
21756 ;; make targets to anchors
21757 (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line
)
21760 (setq line
(replace-match
21761 (concat "@<a name=\""
21762 (org-solidify-link-text (match-string 1 line
))
21765 ((and org-export-with-toc
(equal (string-to-char line
) ?
*))
21766 (setq line
(replace-match
21767 (concat "@<span class=\"target\">" (match-string 1 line
) "@</span> ")
21768 ; (concat "@<i>" (match-string 1 line) "@</i> ")
21771 (setq line
(replace-match
21772 (concat "@<a name=\""
21773 (org-solidify-link-text (match-string 1 line
))
21774 "\" class=\"target\">" (match-string 1 line
) "@</a> ")
21777 (setq line
(org-html-handle-time-stamps line
))
21779 ;; replace "&" by "&", "<" and ">" by "<" and ">"
21780 ;; handle @<..> HTML tags (replace "@>..<" by "<..>")
21781 ;; Also handle sub_superscripts and checkboxes
21782 (setq line
(org-html-expand line
))
21784 ;; Format the links
21786 (while (string-match org-bracket-link-analytic-regexp line start
)
21787 (setq start
(match-beginning 0))
21788 (setq type
(if (match-end 2) (match-string 2 line
) "internal"))
21789 (setq path
(match-string 3 line
))
21790 (setq desc1
(if (match-end 5) (match-string 5 line
))
21791 desc2
(if (match-end 2) (concat type
":" path
) path
)
21792 descp
(and desc1
(not (equal desc1 desc2
)))
21793 desc
(or desc1 desc2
))
21794 ;; Make an image out of the description if that is so wanted
21795 (when (and descp
(org-file-image-p desc
))
21797 (if (string-match "^file:" desc
)
21798 (setq desc
(substring desc
(match-end 0)))))
21799 (setq desc
(concat "<img src=\"" desc
"\"/>")))
21800 ;; FIXME: do we need to unescape here somewhere?
21802 ((equal type
"internal")
21806 (org-solidify-link-text
21807 (save-match-data (org-link-unescape path
)) target-alist
)
21808 "\">" desc
"</a>")))
21809 ((member type
'("http" "https")) ; FIXME: need to test this.
21810 ;; standard URL, just check if we need to inline an image
21811 (if (and (or (eq t org-export-html-inline-images
)
21812 (and org-export-html-inline-images
(not descp
)))
21813 (org-file-image-p path
))
21814 (setq rpl
(concat "<img src=\"" type
":" path
"\"/>"))
21815 (setq link
(concat type
":" path
))
21816 (setq rpl
(concat "<a href=\"" link
"\">" desc
"</a>"))))
21817 ((member type
'("ftp" "mailto" "news"))
21819 (setq link
(concat type
":" path
))
21820 (setq rpl
(concat "<a href=\"" link
"\">" desc
"</a>")))
21821 ((string= type
"file")
21823 (let* ((filename path
)
21824 (abs-p (file-name-absolute-p filename
))
21825 thefile file-is-image-p search
)
21827 (if (string-match "::\\(.*\\)" filename
)
21828 (setq search
(match-string 1 filename
)
21829 filename
(replace-match "" t nil filename
)))
21831 (if (functionp link-validate
)
21832 (funcall link-validate filename current-dir
)
21834 (setq file-is-image-p
(org-file-image-p filename
))
21835 (setq thefile
(if abs-p
(expand-file-name filename
) filename
))
21836 (when (and org-export-html-link-org-files-as-html
21837 (string-match "\\.org$" thefile
))
21838 (setq thefile
(concat (substring thefile
0
21839 (match-beginning 0))
21842 ;; make sure this is can be used as target search
21843 (not (string-match "^[0-9]*$" search
))
21844 (not (string-match "^\\*" search
))
21845 (not (string-match "^/.*/$" search
)))
21846 (setq thefile
(concat thefile
"#"
21847 (org-solidify-link-text
21848 (org-link-unescape search
)))))
21849 (when (string-match "^file:" desc
)
21850 (setq desc
(replace-match "" t t desc
))
21851 (if (string-match "\\.org$" desc
)
21852 (setq desc
(replace-match "" t t desc
))))))
21853 (setq rpl
(if (and file-is-image-p
21854 (or (eq t org-export-html-inline-images
)
21855 (and org-export-html-inline-images
21857 (concat "<img src=\"" thefile
"\"/>")
21858 (concat "<a href=\"" thefile
"\">" desc
"</a>")))
21859 (if (not valid
) (setq rpl desc
))))
21860 ((member type
'("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
21861 (setq rpl
(concat "<i><" type
":"
21862 (save-match-data (org-link-unescape path
))
21864 (setq line
(replace-match rpl t t line
)
21865 start
(+ start
(length rpl
))))
21868 (if (and (string-match org-todo-line-regexp line
)
21869 (match-beginning 2))
21872 (concat (substring line
0 (match-beginning 2))
21874 (if (member (match-string 2 line
)
21877 "\">" (match-string 2 line
)
21878 "</span>" (substring line
(match-end 2)))))
21880 ;; Does this contain a reference to a footnote?
21881 (when org-export-with-footnotes
21882 (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line
)
21883 (let ((n (match-string 2 line
)))
21887 "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
21888 (match-string 1 line
) n n n
)
21892 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line
)
21893 ;; This is a headline
21894 (setq level
(org-tr-level (- (match-end 1) (match-beginning 1)))
21895 txt
(match-string 2 line
))
21896 (if (string-match quote-re0 txt
)
21897 (setq txt
(replace-match "" t t txt
)))
21898 (if (<= level
(max umax umax-toc
))
21899 (setq head-count
(+ head-count
1)))
21900 (when in-local-list
21901 ;; Close any local lists before inserting a new header line
21902 (while local-list-num
21904 (insert (if (car local-list-num
) "</ol>\n" "</ul>"))
21905 (pop local-list-num
))
21906 (setq local-list-indent nil
21907 in-local-list nil
))
21908 (setq first-heading-pos
(or first-heading-pos
(point)))
21909 (org-html-level-start level txt umax
21910 (and org-export-with-toc
(<= level umax
))
21913 (when (string-match quote-re line
)
21917 ((and org-export-with-tables
21918 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line
))
21919 (if (not table-open
)
21920 ;; New table starts
21921 (setq table-open t table-buffer nil table-orig-buffer nil
))
21922 ;; Accumulate lines
21923 (setq table-buffer
(cons line table-buffer
)
21924 table-orig-buffer
(cons origline table-orig-buffer
))
21925 (when (or (not lines
)
21926 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
21928 (setq table-open nil
21929 table-buffer
(nreverse table-buffer
)
21930 table-orig-buffer
(nreverse table-orig-buffer
))
21931 (org-close-par-maybe)
21932 (insert (org-format-table-html table-buffer table-orig-buffer
))))
21935 (when (string-match
21937 ((eq llt t
) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
21938 ((= llt ?.
) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
21939 ((= llt ?\
)) "^\\( \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
21940 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
21942 (setq ind
(org-get-string-indentation line
)
21943 start-is-num
(match-beginning 4)
21944 starter
(if (match-beginning 2)
21945 (substring (match-string 2 line
) 0 -
1))
21946 line
(substring line
(match-beginning 5)))
21947 (unless (string-match "[^ \t]" line
)
21948 ;; empty line. Pretend indentation is large.
21949 (setq ind
(if org-empty-line-terminates-plain-lists
21951 (1+ (or (car local-list-indent
) 1)))))
21952 (setq didclose nil
)
21953 (while (and in-local-list
21954 (or (and (= ind
(car local-list-indent
))
21956 (< ind
(car local-list-indent
))))
21959 (insert (if (car local-list-num
) "</ol>\n" "</ul>"))
21960 (pop local-list-num
) (pop local-list-indent
)
21961 (setq in-local-list local-list-indent
))
21964 (or (not in-local-list
)
21965 (> ind
(car local-list-indent
))))
21966 ;; Start new (level of) list
21967 (org-close-par-maybe)
21968 (insert (if start-is-num
"<ol>\n<li>\n" "<ul>\n<li>\n"))
21969 (push start-is-num local-list-num
)
21970 (push ind local-list-indent
)
21971 (setq in-local-list t
))
21973 ;; continue current list
21977 ;; we did close a list, normal text follows: need <p>
21979 (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line
)
21982 (if (equal (match-string 1 line
) "X")
21984 "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
21987 ;; Empty lines start a new paragraph. If hand-formatted lists
21988 ;; are not fully interpreted, lines starting with "-", "+", "*"
21989 ;; also start a new paragraph.
21990 (if (string-match "^ [-+*]-\\|^[ \t]*$" line
) (org-open-par))
21992 ;; Is this the start of a footnote?
21993 (when org-export-with-footnotes
21994 (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line
)
21995 (org-close-par-maybe)
21996 (let ((n (match-string 1 line
)))
21997 (setq line
(replace-match
21998 (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n
) t t line
)))))
22000 ;; Check if the line break needs to be conserved
22002 ((string-match "\\\\\\\\[ \t]*$" line
)
22003 (setq line
(replace-match "<br/>" t t line
)))
22004 (org-export-preserve-breaks
22005 (setq line
(concat line
"<br/>"))))
22007 (insert line
"\n")))))
22009 ;; Properly close all local lists and other lists
22010 (when inquote
(insert "</pre>\n"))
22011 (when in-local-list
22012 ;; Close any local lists before inserting a new header line
22013 (while local-list-num
22015 (insert (if (car local-list-num
) "</ol>\n" "</ul>\n"))
22016 (pop local-list-num
))
22017 (setq local-list-indent nil
22018 in-local-list nil
))
22019 (org-html-level-start 1 nil umax
22020 (and org-export-with-toc
(<= level umax
))
22024 (when (plist-get opt-plist
:auto-postamble
)
22025 (when (and org-export-author-info author
)
22026 (insert "<p class=\"author\"> "
22027 (nth 1 lang-words
) ": " author
"\n")
22029 (insert "<a href=\"mailto:" email
"\"><"
22030 email
"></a>\n"))
22032 (when (and date time org-export-time-stamp-file
)
22033 (insert "<p class=\"date\"> "
22034 (nth 2 lang-words
) ": "
22035 date
" " time
"</p>\n")))
22037 (if org-export-html-with-timestamp
22038 (insert org-export-html-html-helper-timestamp
))
22039 (insert (or (plist-get opt-plist
:postamble
) ""))
22040 (insert "</body>\n</html>\n"))
22043 (if (eq major-mode default-major-mode
) (html-mode))
22045 ;; insert the table of contents
22046 (goto-char (point-min))
22048 (if (or (re-search-forward
22049 "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t
)
22051 "\\[TABLE-OF-CONTENTS\\]" nil t
))
22053 (goto-char (match-beginning 0))
22054 (replace-match ""))
22055 (goto-char first-heading-pos
)
22056 (when (looking-at "\\s-*</p>")
22057 (goto-char (match-end 0))
22059 (mapc 'insert thetoc
))
22060 ;; remove empty paragraphs and lists
22061 (goto-char (point-min))
22062 (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t
)
22063 (replace-match ""))
22064 (goto-char (point-min))
22065 (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t
)
22066 (replace-match ""))
22067 (or to-buffer
(save-buffer))
22068 (goto-char (point-min))
22069 (message "Exporting... done")
22070 (if (eq to-buffer
'string
)
22071 (prog1 (buffer-substring (point-min) (point-max))
22072 (kill-buffer (current-buffer)))
22073 (current-buffer)))))
22075 (defvar org-table-colgroup-info nil
) ;; FIXME: mode to a better place
22076 (defun org-format-table-ascii (lines)
22077 "Format a table for ascii export."
22078 (if (stringp lines
)
22079 (setq lines
(org-split-string lines
"\n")))
22080 (if (not (string-match "^[ \t]*|" (car lines
)))
22081 ;; Table made by table.el - test for spanning
22084 ;; A normal org table
22085 ;; Get rid of hlines at beginning and end
22086 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
22087 (setq lines
(nreverse lines
))
22088 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
22089 (setq lines
(nreverse lines
))
22090 (when org-export-table-remove-special-lines
22091 ;; Check if the table has a marking column. If yes remove the
22092 ;; column and the special lines
22093 (setq lines
(org-table-clean-before-export lines
)))
22094 ;; Get rid of the vertical lines except for grouping
22095 (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info
))
22096 rtn line vl1 start
)
22097 (while (setq line
(pop lines
))
22098 (if (string-match org-table-hline-regexp line
)
22099 (and (string-match "|\\(.*\\)|" line
)
22100 (setq line
(replace-match " \\1" t nil line
)))
22101 (setq start
0 vl1 vl
)
22102 (while (string-match "|" line start
)
22103 (setq start
(match-end 0))
22104 (or (pop vl1
) (setq line
(replace-match " " t t line
)))))
22108 (defun org-colgroup-info-to-vline-list (info)
22111 (setq last new new
(pop info
))
22112 (if (or (memq last
'(:end
:startend
))
22113 (memq new
'(:start
:startend
)))
22116 (setq vl
(cons nil
(nreverse vl
)))))
22119 (defun org-format-table-html (lines olines
)
22120 "Find out which HTML converter to use and return the HTML code."
22121 (if (stringp lines
)
22122 (setq lines
(org-split-string lines
"\n")))
22123 (if (string-match "^[ \t]*|" (car lines
))
22124 ;; A normal org table
22125 (org-format-org-table-html lines
)
22126 ;; Table made by table.el - test for spanning
22127 (let* ((hlines (delq nil
(mapcar
22129 (if (string-match "^[ \t]*\\+-" x
) x
22132 (first (car hlines
))
22133 (ll (and (string-match "\\S-+" first
)
22134 (match-string 0 first
)))
22135 (re (concat "^[ \t]*" (regexp-quote ll
)))
22136 (spanning (delq nil
(mapcar (lambda (x) (not (string-match re x
)))
22138 (if (and (not spanning
)
22139 (not org-export-prefer-native-exporter-for-tables
))
22140 ;; We can use my own converter with HTML conversions
22141 (org-format-table-table-html lines
)
22142 ;; Need to use the code generator in table.el, with the original text.
22143 (org-format-table-table-html-using-table-generate-source olines
)))))
22145 (defun org-format-org-table-html (lines &optional splice
)
22146 "Format a table into HTML."
22147 ;; Get rid of hlines at beginning and end
22148 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
22149 (setq lines
(nreverse lines
))
22150 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
22151 (setq lines
(nreverse lines
))
22152 (when org-export-table-remove-special-lines
22153 ;; Check if the table has a marking column. If yes remove the
22154 ;; column and the special lines
22155 (setq lines
(org-table-clean-before-export lines
)))
22157 (let ((head (and org-export-highlight-first-table-line
22159 (lambda (x) (string-match "^[ \t]*|-" x
))
22162 tbopen line fields html gr colgropen
)
22163 (if splice
(setq head nil
))
22164 (unless splice
(push (if head
"<thead>" "<tbody>") html
))
22166 (while (setq line
(pop lines
))
22168 (if (string-match "^[ \t]*|-" line
)
22171 (push (if head
"</thead>" "</tbody>") html
)
22172 (if lines
(push "<tbody>" html
) (setq tbopen nil
)))
22173 (setq head nil
) ;; head ends here, first time around
22174 ;; ignore this line
22175 (throw 'next-line t
)))
22176 ;; Break the line into fields
22177 (setq fields
(org-split-string line
"[ \t]*|[ \t]*"))
22178 (unless fnum
(setq fnum
(make-vector (length fields
) 0)))
22179 (setq nlines
(1+ nlines
) i -
1)
22180 (push (concat "<tr>"
22184 (if (and (< i nlines
)
22185 (string-match org-table-number-regexp x
))
22186 (incf (aref fnum i
)))
22188 (concat (car org-export-table-header-tags
) x
22189 (cdr org-export-table-header-tags
))
22190 (concat (car org-export-table-data-tags
) x
22191 (cdr org-export-table-data-tags
))))
22195 (unless splice
(if tbopen
(push "</tbody>" html
)))
22196 (unless splice
(push "</table>\n" html
))
22197 (setq html
(nreverse html
))
22199 ;; Put in COL tags with the alignment (unfortuntely often ignored...)
22202 (setq gr
(pop org-table-colgroup-info
))
22203 (format "%s<COL align=\"%s\"></COL>%s"
22204 (if (memq gr
'(:start
:startend
))
22206 (if colgropen
"</colgroup>\n<colgroup>" "<colgroup>")
22207 (setq colgropen t
))
22209 (if (> (/ (float x
) nlines
) org-table-number-fraction
)
22211 (if (memq gr
'(:end
:startend
))
22212 (progn (setq colgropen nil
) "</colgroup>")
22216 (if colgropen
(setq html
(cons (car html
) (cons "</colgroup>" (cdr html
)))))
22217 (push org-export-html-table-tag html
))
22218 (concat (mapconcat 'identity html
"\n") "\n")))
22220 (defun org-table-clean-before-export (lines)
22221 "Check if the table has a marking column.
22222 If yes remove the column and the special lines."
22223 (setq org-table-colgroup-info nil
)
22226 (lambda (x) (or (string-match "^[ \t]*|-" x
)
22227 (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x
)))
22230 (setq org-table-clean-did-remove-column nil
)
22235 ((string-match "^[ \t]*| */ *|" x
)
22236 (setq org-table-colgroup-info
22237 (mapcar (lambda (x)
22238 (cond ((member x
'("<" "<")) :start
)
22239 ((member x
'(">" ">")) :end
)
22240 ((member x
'("<>" "<>")) :startend
)
22242 (org-split-string x
"[ \t]*|[ \t]*")))
22246 (setq org-table-clean-did-remove-column t
)
22251 ((string-match "^[ \t]*| */ *|" x
)
22252 (setq org-table-colgroup-info
22253 (mapcar (lambda (x)
22254 (cond ((member x
'("<" "<")) :start
)
22255 ((member x
'(">" ">")) :end
)
22256 ((member x
'("<>" "<>")) :startend
)
22258 (cdr (org-split-string x
"[ \t]*|[ \t]*"))))
22260 ((string-match "^[ \t]*| *[!_^/] *|" x
)
22261 nil
) ; ignore this line
22262 ((or (string-match "^\\([ \t]*\\)|-+\\+" x
)
22263 (string-match "^\\([ \t]*\\)|[^|]*|" x
))
22264 ;; remove the first column
22265 (replace-match "\\1|" t nil x
))
22266 (t (error "This should not happen"))))
22269 (defun org-format-table-table-html (lines)
22270 "Format a table generated by table.el into HTML.
22271 This conversion does *not* use `table-generate-source' from table.el.
22272 This has the advantage that Org-mode's HTML conversions can be used.
22273 But it has the disadvantage, that no cell- or row-spanning is allowed."
22274 (let (line field-buffer
22275 (head org-export-highlight-first-table-line
)
22277 (setq html
(concat org-export-html-table-tag
"\n"))
22278 (while (setq line
(pop lines
))
22279 (setq empty
" ")
22281 (if (string-match "^[ \t]*\\+-" line
)
22292 (if (equal x
"") (setq x empty
))
22294 (concat (car org-export-table-header-tags
) x
22295 (cdr org-export-table-header-tags
))
22296 (concat (car org-export-table-data-tags
) x
22297 (cdr org-export-table-data-tags
))))
22301 (setq field-buffer nil
)))
22302 ;; Ignore this line
22303 (throw 'next-line t
)))
22304 ;; Break the line into fields and store the fields
22305 (setq fields
(org-split-string line
"[ \t]*|[ \t]*"))
22307 (setq field-buffer
(mapcar
22309 (concat x
"<br/>" (pop fields
)))
22311 (setq field-buffer fields
))))
22312 (setq html
(concat html
"</table>\n"))
22315 (defun org-format-table-table-html-using-table-generate-source (lines)
22316 "Format a table into html, using `table-generate-source' from table.el.
22317 This has the advantage that cell- or row-spanning is allowed.
22318 But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
22320 (with-current-buffer (get-buffer-create " org-tmp1 ")
22322 (insert (mapconcat 'identity lines
"\n"))
22323 (goto-char (point-min))
22324 (if (not (re-search-forward "|[^+]" nil t
))
22325 (error "Error processing table"))
22326 (table-recognize-table)
22327 (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
22328 (table-generate-source 'html
" org-tmp2 ")
22329 (set-buffer " org-tmp2 ")
22330 (buffer-substring (point-min) (point-max))))
22332 (defun org-html-handle-time-stamps (s)
22333 "Format time stamps in string S, or remove them."
22336 (while (string-match org-maybe-keyword-time-regexp s
)
22337 (if (and (match-end 1) (equal (match-string 1 s
) org-clock-string
))
22338 ;; never export CLOCK
22340 (or b
(setq b
(substring s
0 (match-beginning 0))))
22341 (if (not org-export-with-timestamps
)
22342 (setq r
(concat r
(substring s
0 (match-beginning 0)))
22343 s
(substring s
(match-end 0)))
22345 r
(substring s
0 (match-beginning 0))
22347 (format "@<span class=\"timestamp-kwd\">%s @</span>"
22348 (match-string 1 s
)))
22349 (format " @<span class=\"timestamp\">%s@</span>"
22351 (org-translate-time (match-string 3 s
)) 1 -
1)))
22352 s
(substring s
(match-end 0)))))
22353 ;; Line break if line started and ended with time stamp stuff
22356 (setq r
(concat r s
))
22357 (unless (string-match "\\S-" (concat b s
))
22358 (setq r
(concat r
"@<br/>")))
22361 (defun org-html-protect (s)
22362 ;; convert & to &, < to < and > to >
22364 (while (string-match "&" s start
)
22365 (setq s
(replace-match "&" t t s
)
22366 start
(1+ (match-beginning 0))))
22367 (while (string-match "<" s
)
22368 (setq s
(replace-match "<" t t s
)))
22369 (while (string-match ">" s
)
22370 (setq s
(replace-match ">" t t s
))))
22373 (defun org-export-cleanup-toc-line (s)
22374 "Remove tags and time staps from lines going into the toc."
22375 (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s
)
22376 (setq s
(replace-match "" t t s
)))
22377 (when org-export-remove-timestamps-from-toc
22378 (while (string-match org-maybe-keyword-time-regexp s
)
22379 (setq s
(replace-match "" t t s
))))
22380 (while (string-match org-bracket-link-regexp s
)
22381 (setq s
(replace-match (match-string (if (match-end 3) 3 1) s
)
22385 (defun org-html-expand (string)
22386 "Prepare STRING for HTML export. Applies all active conversions.
22387 If there are links in the string, don't modify these."
22389 (while (setq m
(string-match org-bracket-link-regexp string
))
22390 (setq s
(substring string
0 m
)
22391 l
(match-string 0 string
)
22392 string
(substring string
(match-end 0)))
22393 (push (org-html-do-expand s
) res
)
22395 (push (org-html-do-expand string
) res
)
22396 (apply 'concat
(nreverse res
))))
22398 (defun org-html-do-expand (s)
22399 "Apply all active conversions to translate special ASCII to HTML."
22400 (setq s
(org-html-protect s
))
22401 (if org-export-html-expand
22402 (while (string-match "@<\\([^&]*\\)>" s
)
22403 (setq s
(replace-match "<\\1>" t nil s
))))
22404 (if org-export-with-emphasize
22405 (setq s
(org-export-html-convert-emphasize s
)))
22406 (if org-export-with-sub-superscripts
22407 (setq s
(org-export-html-convert-sub-super s
)))
22408 (if org-export-with-TeX-macros
22409 (let ((start 0) wd ass
)
22410 (while (setq start
(string-match "\\\\\\([a-zA-Z]+\\)" s start
))
22411 (setq wd
(match-string 1 s
))
22412 (if (setq ass
(assoc wd org-html-entities
))
22413 (setq s
(replace-match (or (cdr ass
)
22414 (concat "&" (car ass
) ";"))
22416 (setq start
(+ start
(length wd
)))))))
22419 (defun org-create-multibrace-regexp (left right n
)
22420 "Create a regular expression which will match a balanced sexp.
22421 Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
22422 as single character strings.
22423 The regexp returned will match the entire expression including the
22424 delimiters. It will also define a single group which contains the
22425 match except for the outermost delimiters. The maximum depth of
22426 stacked delimiters is N. Escaping delimiters is not possible."
22427 (let* ((nothing (concat "[^" "\\" left
"\\" right
"]*?"))
22430 (next (concat "\\(?:" nothing left nothing right
"\\)+" nothing
)))
22433 re
(concat re or next
)
22434 next
(concat "\\(?:" nothing left next right
"\\)+" nothing
)))
22435 (concat left
"\\(" re
"\\)" right
)))
22437 (defvar org-match-substring-regexp
22439 "\\([^\\]\\)\\([_^]\\)\\("
22440 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
22442 "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth
) "\\)"
22444 "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
22445 "The regular expression matching a sub- or superscript.")
22447 ;(let ((s "a\\_b"))
22448 ; (and (string-match org-match-substring-regexp s)
22449 ; (conca t (match-string 1 s) ":::" (match-string 2 s))))
22451 (defun org-export-html-convert-sub-super (string)
22452 "Convert sub- and superscripts in STRING to HTML."
22453 (let (key c
(s 0) (requireb (eq org-export-with-sub-superscripts
'{})))
22454 (while (string-match org-match-substring-regexp string s
)
22455 (if (and requireb
(match-end 8))
22456 (setq s
(match-end 2))
22457 (setq s
(match-end 1)
22458 key
(if (string= (match-string 2 string
) "_") "sub" "sup")
22459 c
(or (match-string 8 string
)
22460 (match-string 6 string
)
22461 (match-string 5 string
))
22462 string
(replace-match
22463 (concat (match-string 1 string
)
22464 "<" key
">" c
"</" key
">")
22466 (while (string-match "\\\\\\([_^]\\)" string
)
22467 (setq string
(replace-match (match-string 1 string
) t t string
)))
22470 (defun org-export-html-convert-emphasize (string)
22473 (while (string-match org-emph-re string s
)
22475 (substring string
(match-beginning 3) (1+ (match-beginning 3)))
22476 (substring string
(match-beginning 4) (1+ (match-beginning 4)))))
22477 (setq string
(replace-match
22478 (concat "\\1" (nth 2 (assoc (match-string 3 string
) org-emphasis-alist
))
22479 "\\4" (nth 3 (assoc (match-string 3 string
) org-emphasis-alist
))
22480 "\\5") t nil string
))
22484 (defvar org-par-open nil
)
22485 (defun org-open-par ()
22486 "Insert <p>, but first close previous paragraph if any."
22487 (org-close-par-maybe)
22489 (setq org-par-open t
))
22490 (defun org-close-par-maybe ()
22491 "Close paragraph if there is one open."
22494 (setq org-par-open nil
)))
22495 (defun org-close-li ()
22496 "Close <li> if necessary."
22497 (org-close-par-maybe)
22498 (insert "</li>\n"))
22500 (defvar body-only
) ; dynamically scoped into this.
22501 (defun org-html-level-start (level title umax with-toc head-count
)
22502 "Insert a new level in HTML export.
22503 When TITLE is nil, just close all open levels."
22504 (org-close-par-maybe)
22505 (let ((l (1+ (max level umax
))))
22506 (while (<= l org-level-max
)
22507 (if (aref org-levels-open
(1- l
))
22509 (org-html-level-close l
)
22510 (aset org-levels-open
(1- l
) nil
)))
22513 ;; If title is nil, this means this function is called to close
22514 ;; all levels, so the rest is done only if title is given
22515 (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title
)
22516 (setq title
(replace-match
22517 (if org-export-with-tags
22520 " <span class=\"tag\">"
22521 (mapconcat 'identity
(org-split-string
22522 (match-string 1 title
) ":")
22529 (if (aref org-levels-open
(1- level
))
22532 (insert "<li>" title
"<br/>\n"))
22533 (aset org-levels-open
(1- level
) t
)
22534 (org-close-par-maybe)
22535 (insert "<ul>\n<li>" title
"<br/>\n")))
22536 (if (and org-export-with-section-numbers
(not body-only
))
22537 (setq title
(concat (org-section-number level
) " " title
)))
22538 (setq level
(+ level org-export-html-toplevel-hlevel -
1))
22540 (insert (format "\n<h%d id=\"sec-%d\">%s</h%d>\n"
22541 level head-count title level
))
22542 (insert (format "\n<h%d>%s</h%d>\n" level title level
)))
22545 (defun org-html-level-close (&rest args
)
22546 "Terminate one level in HTML export."
22548 (insert "</ul>\n"))
22550 ;;; iCalendar export
22553 (defun org-export-icalendar-this-file ()
22554 "Export current file as an iCalendar file.
22555 The iCalendar file will be located in the same directory as the Org-mode
22556 file, but with extension `.ics'."
22558 (org-export-icalendar nil buffer-file-name
))
22561 (defun org-export-icalendar-all-agenda-files ()
22562 "Export all files in `org-agenda-files' to iCalendar .ics files.
22563 Each iCalendar file will be located in the same directory as the Org-mode
22564 file, but with extension `.ics'."
22566 (apply 'org-export-icalendar nil
(org-agenda-files t
)))
22569 (defun org-export-icalendar-combine-agenda-files ()
22570 "Export all files in `org-agenda-files' to a single combined iCalendar file.
22571 The file is stored under the name `org-combined-agenda-icalendar-file'."
22573 (apply 'org-export-icalendar t
(org-agenda-files t
)))
22575 (defun org-export-icalendar (combine &rest files
)
22576 "Create iCalendar files for all elements of FILES.
22577 If COMBINE is non-nil, combine all calendar entries into a single large
22578 file and store it under the name `org-combined-agenda-icalendar-file'."
22580 (org-prepare-agenda-buffers files
)
22581 (let* ((dir (org-export-directory
22582 :ical
(list :publishing-directory
22583 org-export-publishing-directory
)))
22584 file ical-file ical-buffer category started org-agenda-new-buffers
)
22586 (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
22589 (if (file-name-absolute-p org-combined-agenda-icalendar-file
)
22590 org-combined-agenda-icalendar-file
22591 (expand-file-name org-combined-agenda-icalendar-file dir
))
22592 ical-buffer
(org-get-agenda-file-buffer ical-file
))
22593 (set-buffer ical-buffer
) (erase-buffer))
22594 (while (setq file
(pop files
))
22596 (org-check-agenda-file file
)
22597 (set-buffer (org-get-agenda-file-buffer file
))
22599 (setq ical-file
(concat (file-name-as-directory dir
)
22600 (file-name-sans-extension
22601 (file-name-nondirectory buffer-file-name
))
22603 (setq ical-buffer
(org-get-agenda-file-buffer ical-file
))
22604 (with-current-buffer ical-buffer
(erase-buffer)))
22605 (setq category
(or org-category
22606 (file-name-sans-extension
22607 (file-name-nondirectory buffer-file-name
))))
22608 (if (symbolp category
) (setq category
(symbol-name category
)))
22609 (let ((standard-output ical-buffer
))
22611 (and (not started
) (setq started t
)
22612 (org-start-icalendar-file org-icalendar-combined-name
))
22613 (org-start-icalendar-file category
))
22614 (org-print-icalendar-entries combine
)
22615 (when (or (and combine
(not files
)) (not combine
))
22616 (org-finish-icalendar-file)
22617 (set-buffer ical-buffer
)
22619 (run-hooks 'org-after-save-iCalendar-file-hook
)))))
22620 (org-release-buffers org-agenda-new-buffers
))))
22622 (defvar org-after-save-iCalendar-file-hook nil
22623 "Hook run after an iCalendar file has been saved.
22624 The iCalendar buffer is still current when this hook is run.
22625 A good way to use this is to tell a desktop calenndar application to re-read
22626 the iCalendar file.")
22628 (defun org-print-icalendar-entries (&optional combine
)
22629 "Print iCalendar entries for the current Org-mode file to `standard-output'.
22630 When COMBINE is non nil, add the category to each line."
22631 (let ((re1 (concat org-ts-regexp
"\\|<%%([^>\n]+>"))
22632 (re2 (concat "--?-?\\(" org-ts-regexp
"\\)"))
22633 (org-category-table (org-get-category-table))
22634 (dts (org-ical-ts-to-string
22635 (format-time-string (cdr org-time-stamp-formats
) (current-time))
22637 hd ts ts2 state status
(inc t
) pos b sexp rrule
22638 scheduledp deadlinep tmp pri category
22639 (sexp-buffer (get-buffer-create "*ical-tmp*")))
22641 (goto-char (point-min))
22642 (while (re-search-forward re1 nil t
)
22645 (setq pos
(match-beginning 0)
22646 ts
(match-string 0)
22648 hd
(org-get-heading)
22649 category
(org-get-category))
22650 (if (looking-at re2
)
22652 (goto-char (match-end 0))
22653 (setq ts2
(match-string 1) inc nil
))
22654 (setq tmp
(buffer-substring (max (point-min)
22655 (- pos org-ds-keyword-length
))
22657 ts2
(if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts
)
22660 (replace-match "\\1" t nil ts
))
22662 deadlinep
(string-match org-deadline-regexp tmp
)
22663 scheduledp
(string-match org-scheduled-regexp tmp
)
22664 ;; donep (org-entry-is-done-p)
22666 (if (or (string-match org-tr-regexp hd
)
22667 (string-match org-ts-regexp hd
))
22668 (setq hd
(replace-match "" t t hd
)))
22669 (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts
)
22671 (concat "\nRRULE:FREQ="
22673 (match-string 2 ts
)
22674 '(("d" .
"DAILY")("w" .
"WEEKLY")
22675 ("m" .
"MONTHLY")("y" .
"YEARLY"))))
22676 ";INTERVAL=" (match-string 1 ts
)))
22678 (if (string-match org-bracket-link-regexp hd
)
22679 (setq hd
(replace-match (if (match-end 3) (match-string 3 hd
)
22680 (match-string 1 hd
))
22682 (if deadlinep
(setq hd
(concat "DL: " hd
)))
22683 (if scheduledp
(setq hd
(concat "S: " hd
)))
22684 (if (string-match "\\`<%%" ts
)
22685 (with-current-buffer sexp-buffer
22686 (insert (substring ts
1 -
1) " " hd
"\n"))
22687 (princ (format "BEGIN:VEVENT
22693 (org-ical-ts-to-string ts
"DTSTART")
22694 (org-ical-ts-to-string ts2
"DTEND" inc
)
22695 rrule hd category
)))))
22697 (when (and org-icalendar-include-sexps
22698 (condition-case nil
(require 'icalendar
) (error nil
))
22699 (fboundp 'icalendar-export-region
))
22700 ;; Get all the literal sexps
22701 (goto-char (point-min))
22702 (while (re-search-forward "^&?%%(" nil t
)
22705 (setq b
(match-beginning 0))
22706 (goto-char (1- (match-end 0)))
22709 (setq sexp
(buffer-substring b
(point)))
22710 (with-current-buffer sexp-buffer
22711 (insert sexp
"\n"))
22712 (princ (org-diary-to-ical-string sexp-buffer
)))))
22714 (when org-icalendar-include-todo
22715 (goto-char (point-min))
22716 (while (re-search-forward org-todo-line-regexp nil t
)
22719 (setq state
(match-string 2))
22720 (setq status
(if (member state org-done-keywords
)
22721 "COMPLETED" "NEEDS-ACTION"))
22723 (or (not (member state org-done-keywords
))
22724 (eq org-icalendar-include-todo
'all
))
22725 (not (member org-archive-tag
(org-get-tags-at)))
22727 (setq hd
(match-string 3))
22728 (if (string-match org-bracket-link-regexp hd
)
22729 (setq hd
(replace-match (if (match-end 3) (match-string 3 hd
)
22730 (match-string 1 hd
))
22732 (if (string-match org-priority-regexp hd
)
22733 (setq pri
(string-to-char (match-string 2 hd
))
22734 hd
(concat (substring hd
0 (match-beginning 1))
22735 (substring hd
(match-end 1))))
22736 (setq pri org-default-priority
))
22737 (setq pri
(floor (1+ (* 8.
(/ (float (- org-lowest-priority pri
))
22738 (- org-lowest-priority org-highest-priority
))))))
22740 (princ (format "BEGIN:VTODO
22748 dts hd category pri status
)))))))))
22750 (defun org-start-icalendar-file (name)
22751 "Start an iCalendar file by inserting the header."
22752 (let ((user user-full-name
)
22753 (name (or name
"unknown"))
22754 (timezone (cadr (current-time-zone))))
22756 (format "BEGIN:VCALENDAR
22759 PRODID:-//%s//Emacs with Org-mode//EN
22761 CALSCALE:GREGORIAN\n" name user timezone
))))
22763 (defun org-finish-icalendar-file ()
22764 "Finish an iCalendar file by inserting the END statement."
22765 (princ "END:VCALENDAR\n"))
22767 (defun org-ical-ts-to-string (s keyword
&optional inc
)
22768 "Take a time string S and convert it to iCalendar format.
22769 KEYWORD is added in front, to make a complete line like DTSTART....
22770 When INC is non-nil, increase the hour by two (if time string contains
22771 a time), or the day by one (if it does not contain a time)."
22772 (let ((t1 (org-parse-time-string s
'nodefault
))
22773 t2 fmt have-time time
)
22774 (if (and (car t1
) (nth 1 t1
) (nth 2 t1
))
22775 (setq t2 t1 have-time t
)
22776 (setq t2
(org-parse-time-string s
)))
22777 (let ((s (car t2
)) (mi (nth 1 t2
)) (h (nth 2 t2
))
22778 (d (nth 3 t2
)) (m (nth 4 t2
)) (y (nth 5 t2
)))
22781 (if org-agenda-default-appointment-duration
22782 (setq mi
(+ org-agenda-default-appointment-duration mi
))
22785 (setq time
(encode-time s mi h d m y
)))
22786 (setq fmt
(if have-time
":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
22787 (concat keyword
(format-time-string fmt time
))))
22791 (defun org-export-as-xoxo-insert-into (buffer &rest output
)
22792 (with-current-buffer buffer
22793 (apply 'insert output
)))
22794 (put 'org-export-as-xoxo-insert-into
'lisp-indent-function
1)
22796 (defun org-export-as-xoxo (&optional buffer
)
22797 "Export the org buffer as XOXO.
22798 The XOXO buffer is named *xoxo-<source buffer name>*"
22799 (interactive (list (current-buffer)))
22800 ;; A quickie abstraction
22802 ;; Output everything as XOXO
22803 (with-current-buffer (get-buffer buffer
)
22804 (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
22805 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
22806 (org-infile-export-plist)))
22807 (filename (concat (file-name-as-directory
22808 (org-export-directory :xoxo opt-plist
))
22809 (file-name-sans-extension
22810 (file-name-nondirectory buffer-file-name
))
22812 (out (find-file-noselect filename
))
22815 ;; Check the output buffer is empty.
22816 (with-current-buffer out
(erase-buffer))
22817 ;; Kick off the output
22818 (org-export-as-xoxo-insert-into out
"<ol class='xoxo'>\n")
22819 (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't
)
22820 (let* ((hd (match-string-no-properties 1))
22821 (level (length hd
))
22823 (match-string-no-properties 2)
22825 (goto-char (match-end 0))
22830 (if (looking-at "^[ \t]\\(.*\\)")
22831 (setq str
(concat str
(match-string-no-properties 1)))
22832 (throw 'loop str
)))))))))
22834 ;; Handle level rendering
22836 ((> level last-level
)
22837 (org-export-as-xoxo-insert-into out
"\n<ol>\n"))
22839 ((< level last-level
)
22840 (dotimes (- (- last-level level
) 1)
22842 (org-export-as-xoxo-insert-into out
"</li>\n"))
22843 (org-export-as-xoxo-insert-into out
"</ol>\n"))
22845 (org-export-as-xoxo-insert-into out
"</li>\n")
22846 (setq hanging-li nil
)))
22848 ((equal level last-level
)
22850 (org-export-as-xoxo-insert-into out
"</li>\n")))
22853 (setq last-level level
)
22855 ;; And output the new li
22856 (setq hanging-li
't
)
22857 (if (equal ?
+ (elt text
0))
22858 (org-export-as-xoxo-insert-into out
"<li class='" (substring text
1) "'>")
22859 (org-export-as-xoxo-insert-into out
"<li>" text
))))
22861 ;; Finally finish off the ol
22862 (dotimes (- last-level
1)
22864 (org-export-as-xoxo-insert-into out
"</li>\n"))
22865 (org-export-as-xoxo-insert-into out
"</ol>\n"))
22867 ;; Finish the buffer off and clean it up.
22868 (switch-to-buffer-other-window out
)
22869 (indent-region (point-min) (point-max) nil
)
22871 (goto-char (point-min))
22877 ;; Make `C-c C-x' a prefix key
22878 (org-defkey org-mode-map
"\C-c\C-x" (make-sparse-keymap))
22880 ;; TAB key with modifiers
22881 (org-defkey org-mode-map
"\C-i" 'org-cycle
)
22882 (org-defkey org-mode-map
[(tab)] 'org-cycle
)
22883 (org-defkey org-mode-map
[(control tab
)] 'org-force-cycle-archived
)
22884 (org-defkey org-mode-map
[(meta tab
)] 'org-complete
)
22885 (org-defkey org-mode-map
"\M-\t" 'org-complete
)
22886 (org-defkey org-mode-map
"\M-\C-i" 'org-complete
)
22887 ;; The following line is necessary under Suse GNU/Linux
22888 (unless (featurep 'xemacs
)
22889 (org-defkey org-mode-map
[S-iso-lefttab
] 'org-shifttab
))
22890 (org-defkey org-mode-map
[(shift tab
)] 'org-shifttab
)
22891 (define-key org-mode-map
(kbd "<backtab>") 'org-shifttab
)
22893 (org-defkey org-mode-map
[(shift return
)] 'org-table-copy-down
)
22894 (org-defkey org-mode-map
[(meta shift return
)] 'org-insert-todo-heading
)
22895 (org-defkey org-mode-map
[(meta return
)] 'org-meta-return
)
22897 ;; Cursor keys with modifiers
22898 (org-defkey org-mode-map
[(meta left
)] 'org-metaleft
)
22899 (org-defkey org-mode-map
[(meta right
)] 'org-metaright
)
22900 (org-defkey org-mode-map
[(meta up
)] 'org-metaup
)
22901 (org-defkey org-mode-map
[(meta down
)] 'org-metadown
)
22903 (org-defkey org-mode-map
[(meta shift left
)] 'org-shiftmetaleft
)
22904 (org-defkey org-mode-map
[(meta shift right
)] 'org-shiftmetaright
)
22905 (org-defkey org-mode-map
[(meta shift up
)] 'org-shiftmetaup
)
22906 (org-defkey org-mode-map
[(meta shift down
)] 'org-shiftmetadown
)
22908 (org-defkey org-mode-map
[(shift up
)] 'org-shiftup
)
22909 (org-defkey org-mode-map
[(shift down
)] 'org-shiftdown
)
22910 (org-defkey org-mode-map
[(shift left
)] 'org-shiftleft
)
22911 (org-defkey org-mode-map
[(shift right
)] 'org-shiftright
)
22913 (org-defkey org-mode-map
[(control shift right
)] 'org-shiftcontrolright
)
22914 (org-defkey org-mode-map
[(control shift left
)] 'org-shiftcontrolleft
)
22916 ;;; Extra keys for tty access.
22917 ;; We only set them when really needed because otherwise the
22918 ;; menus don't show the simple keys
22920 (when (or (featurep 'xemacs
) ;; because XEmacs supports multi-device stuff
22921 (not window-system
))
22922 (org-defkey org-mode-map
"\C-c\C-xc" 'org-table-copy-down
)
22923 (org-defkey org-mode-map
"\C-c\C-xM" 'org-insert-todo-heading
)
22924 (org-defkey org-mode-map
"\C-c\C-xm" 'org-meta-return
)
22925 (org-defkey org-mode-map
[?\e
(return)] 'org-meta-return
)
22926 (org-defkey org-mode-map
[?\e
(left)] 'org-metaleft
)
22927 (org-defkey org-mode-map
"\C-c\C-xl" 'org-metaleft
)
22928 (org-defkey org-mode-map
[?\e
(right)] 'org-metaright
)
22929 (org-defkey org-mode-map
"\C-c\C-xr" 'org-metaright
)
22930 (org-defkey org-mode-map
[?\e
(up)] 'org-metaup
)
22931 (org-defkey org-mode-map
"\C-c\C-xu" 'org-metaup
)
22932 (org-defkey org-mode-map
[?\e
(down)] 'org-metadown
)
22933 (org-defkey org-mode-map
"\C-c\C-xd" 'org-metadown
)
22934 (org-defkey org-mode-map
"\C-c\C-xL" 'org-shiftmetaleft
)
22935 (org-defkey org-mode-map
"\C-c\C-xR" 'org-shiftmetaright
)
22936 (org-defkey org-mode-map
"\C-c\C-xU" 'org-shiftmetaup
)
22937 (org-defkey org-mode-map
"\C-c\C-xD" 'org-shiftmetadown
)
22938 (org-defkey org-mode-map
[?\C-c
(up)] 'org-shiftup
)
22939 (org-defkey org-mode-map
[?\C-c
(down)] 'org-shiftdown
)
22940 (org-defkey org-mode-map
[?\C-c
(left)] 'org-shiftleft
)
22941 (org-defkey org-mode-map
[?\C-c
(right)] 'org-shiftright
)
22942 (org-defkey org-mode-map
[?\C-c ?\C-x
(right)] 'org-shiftcontrolright
)
22943 (org-defkey org-mode-map
[?\C-c ?\C-x
(left)] 'org-shiftcontrolleft
))
22945 ;; All the other keys
22947 (org-defkey org-mode-map
"\C-c\C-a" 'show-all
) ; in case allout messed up.
22948 (org-defkey org-mode-map
"\C-c\C-r" 'org-reveal
)
22949 (org-defkey org-mode-map
"\C-xns" 'org-narrow-to-subtree
)
22950 (org-defkey org-mode-map
"\C-c$" 'org-archive-subtree
)
22951 (org-defkey org-mode-map
"\C-c\C-x\C-s" 'org-advertized-archive-subtree
)
22952 (org-defkey org-mode-map
"\C-c\C-x\C-a" 'org-toggle-archive-tag
)
22953 (org-defkey org-mode-map
"\C-c\C-xb" 'org-tree-to-indirect-buffer
)
22954 (org-defkey org-mode-map
"\C-c\C-j" 'org-goto
)
22955 (org-defkey org-mode-map
"\C-c\C-t" 'org-todo
)
22956 (org-defkey org-mode-map
"\C-c\C-s" 'org-schedule
)
22957 (org-defkey org-mode-map
"\C-c\C-d" 'org-deadline
)
22958 (org-defkey org-mode-map
"\C-c;" 'org-toggle-comment
)
22959 (org-defkey org-mode-map
"\C-c\C-v" 'org-show-todo-tree
)
22960 (org-defkey org-mode-map
"\C-c\C-w" 'org-check-deadlines
)
22961 (org-defkey org-mode-map
"\C-c/" 'org-occur
) ; Minor-mode reserved
22962 (org-defkey org-mode-map
"\C-c\\" 'org-tags-sparse-tree
) ; Minor-mode res.
22963 (org-defkey org-mode-map
"\C-c\C-m" 'org-ctrl-c-ret
)
22964 (org-defkey org-mode-map
"\M-\C-m" 'org-insert-heading
)
22965 (org-defkey org-mode-map
"\C-c\C-x\C-n" 'org-next-link
)
22966 (org-defkey org-mode-map
"\C-c\C-x\C-p" 'org-previous-link
)
22967 (org-defkey org-mode-map
"\C-c\C-l" 'org-insert-link
)
22968 (org-defkey org-mode-map
"\C-c\C-o" 'org-open-at-point
)
22969 (org-defkey org-mode-map
"\C-c%" 'org-mark-ring-push
)
22970 (org-defkey org-mode-map
"\C-c&" 'org-mark-ring-goto
)
22971 (org-defkey org-mode-map
"\C-c\C-z" 'org-time-stamp
) ; Alternative binding
22972 (org-defkey org-mode-map
"\C-c." 'org-time-stamp
) ; Minor-mode reserved
22973 (org-defkey org-mode-map
"\C-c!" 'org-time-stamp-inactive
) ; Minor-mode r.
22974 (org-defkey org-mode-map
"\C-c," 'org-priority
) ; Minor-mode reserved
22975 (org-defkey org-mode-map
"\C-c\C-y" 'org-evaluate-time-range
)
22976 (org-defkey org-mode-map
"\C-c>" 'org-goto-calendar
)
22977 (org-defkey org-mode-map
"\C-c<" 'org-date-from-calendar
)
22978 (org-defkey org-mode-map
[(control ?
,)] 'org-cycle-agenda-files
)
22979 (org-defkey org-mode-map
[(control ?
\')] 'org-cycle-agenda-files
)
22980 (org-defkey org-mode-map
"\C-c[" 'org-agenda-file-to-front
)
22981 (org-defkey org-mode-map
"\C-c]" 'org-remove-file
)
22982 (org-defkey org-mode-map
"\C-c-" 'org-ctrl-c-minus
)
22983 (org-defkey org-mode-map
"\C-c^" 'org-sort
)
22984 (org-defkey org-mode-map
"\C-c\C-c" 'org-ctrl-c-ctrl-c
)
22985 (org-defkey org-mode-map
"\C-c#" 'org-update-checkbox-count
)
22986 (org-defkey org-mode-map
"\C-m" 'org-return
)
22987 (org-defkey org-mode-map
"\C-c?" 'org-table-field-info
)
22988 (org-defkey org-mode-map
"\C-c " 'org-table-blank-field
)
22989 (org-defkey org-mode-map
"\C-c+" 'org-table-sum
)
22990 (org-defkey org-mode-map
"\C-c=" 'org-table-eval-formula
)
22991 (org-defkey org-mode-map
"\C-c'" 'org-table-edit-formulas
)
22992 (org-defkey org-mode-map
"\C-c`" 'org-table-edit-field
)
22993 (org-defkey org-mode-map
"\C-c|" 'org-table-create-or-convert-from-region
)
22994 (org-defkey org-mode-map
"\C-c*" 'org-table-recalculate
)
22995 (org-defkey org-mode-map
[(control ?
#)] 'org-table-rotate-recalc-marks
)
22996 (org-defkey org-mode-map
"\C-c~" 'org-table-create-with-table.el
)
22997 (org-defkey org-mode-map
"\C-c\C-q" 'org-table-wrap-region
)
22998 (org-defkey org-mode-map
"\C-c}" 'org-table-toggle-coordinate-overlays
)
22999 (org-defkey org-mode-map
"\C-c{" 'org-table-toggle-formula-debugger
)
23000 (org-defkey org-mode-map
"\C-c\C-e" 'org-export
)
23001 (org-defkey org-mode-map
"\C-c:" 'org-toggle-fixed-width-section
)
23002 (org-defkey org-mode-map
"\C-c\C-x\C-f" 'org-emphasize
)
23004 (org-defkey org-mode-map
"\C-c\C-x\C-k" 'org-cut-special
)
23005 (org-defkey org-mode-map
"\C-c\C-x\C-w" 'org-cut-special
)
23006 (org-defkey org-mode-map
"\C-c\C-x\M-w" 'org-copy-special
)
23007 (org-defkey org-mode-map
"\C-c\C-x\C-y" 'org-paste-special
)
23009 (org-defkey org-mode-map
"\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays
)
23010 (org-defkey org-mode-map
"\C-c\C-x\C-i" 'org-clock-in
)
23011 (org-defkey org-mode-map
"\C-c\C-x\C-o" 'org-clock-out
)
23012 (org-defkey org-mode-map
"\C-c\C-x\C-x" 'org-clock-cancel
)
23013 (org-defkey org-mode-map
"\C-c\C-x\C-d" 'org-clock-display
)
23014 (org-defkey org-mode-map
"\C-c\C-x\C-r" 'org-clock-report
)
23015 (org-defkey org-mode-map
"\C-c\C-x\C-u" 'org-dblock-update
)
23016 (org-defkey org-mode-map
"\C-c\C-x\C-l" 'org-preview-latex-fragment
)
23017 (org-defkey org-mode-map
"\C-c\C-x\C-b" 'org-toggle-checkbox
)
23019 (define-key org-mode-map
"\C-c\C-x\C-c" 'org-columns
)
23021 (when (featurep 'xemacs
)
23022 (org-defkey org-mode-map
'button3
'popup-mode-menu
))
23024 (defsubst org-table-p
() (org-at-table-p))
23026 (defun org-self-insert-command (N)
23027 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
23028 If the cursor is in a table looking at whitespace, the whitespace is
23029 overwritten, and the table is not marked as requiring realignment."
23031 (if (and (org-table-p)
23033 ;; check if we blank the field, and if that triggers align
23034 (and org-table-auto-blank-field
23035 (member last-command
23036 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c
))
23037 (if (or (equal (char-after) ?\
) (looking-at "[^|\n]* |"))
23038 ;; got extra space, this field does not determine column width
23039 (let (org-table-may-need-update) (org-table-blank-field))
23040 ;; no extra space, this field may determine column width
23041 (org-table-blank-field)))
23044 (looking-at "[^|\n]* |"))
23045 (let (org-table-may-need-update)
23046 (goto-char (1- (match-end 0)))
23047 (delete-backward-char 1)
23048 (goto-char (match-beginning 0))
23049 (self-insert-command N
))
23050 (setq org-table-may-need-update t
)
23051 (self-insert-command N
)
23052 (org-fix-tags-on-the-fly)))
23054 (defun org-fix-tags-on-the-fly ()
23055 (when (and (equal (char-after (point-at-bol)) ?
*)
23056 (org-on-heading-p))
23057 (org-align-tags-here org-tags-column
)))
23059 (defun org-delete-backward-char (N)
23060 "Like `delete-backward-char', insert whitespace at field end in tables.
23061 When deleting backwards, in tables this function will insert whitespace in
23062 front of the next \"|\" separator, to keep the table aligned. The table will
23063 still be marked for re-alignment if the field did fill the entire column,
23064 because, in this case the deletion might narrow the column."
23066 (if (and (org-table-p)
23068 (string-match "|" (buffer-substring (point-at-bol) (point)))
23069 (looking-at ".*?|"))
23070 (let ((pos (point))
23071 (noalign (looking-at "[^|\n\r]* |"))
23072 (c org-table-may-need-update
))
23073 (backward-delete-char N
)
23074 (skip-chars-forward "^|")
23076 (goto-char (1- pos
))
23077 ;; noalign: if there were two spaces at the end, this field
23078 ;; does not determine the width of the column.
23079 (if noalign
(setq org-table-may-need-update c
)))
23080 (backward-delete-char N
)
23081 (org-fix-tags-on-the-fly)))
23083 (defun org-delete-char (N)
23084 "Like `delete-char', but insert whitespace at field end in tables.
23085 When deleting characters, in tables this function will insert whitespace in
23086 front of the next \"|\" separator, to keep the table aligned. The table will
23087 still be marked for re-alignment if the field did fill the entire column,
23088 because, in this case the deletion might narrow the column."
23090 (if (and (org-table-p)
23092 (not (= (char-after) ?|
))
23094 (if (looking-at ".*?|")
23095 (let ((pos (point))
23096 (noalign (looking-at "[^|\n\r]* |"))
23097 (c org-table-may-need-update
))
23098 (replace-match (concat
23099 (substring (match-string 0) 1 -
1)
23102 ;; noalign: if there were two spaces at the end, this field
23103 ;; does not determine the width of the column.
23104 (if noalign
(setq org-table-may-need-update c
)))
23107 (org-fix-tags-on-the-fly)))
23109 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
23110 (put 'org-self-insert-command
'delete-selection t
)
23111 (put 'orgtbl-self-insert-command
'delete-selection t
)
23112 (put 'org-delete-char
'delete-selection
'supersede
)
23113 (put 'org-delete-backward-char
'delete-selection
'supersede
)
23115 ;; Make `flyspell-mode' delay after some commands
23116 (put 'org-self-insert-command
'flyspell-delayed t
)
23117 (put 'orgtbl-self-insert-command
'flyspell-delayed t
)
23118 (put 'org-delete-char
'flyspell-delayed t
)
23119 (put 'org-delete-backward-char
'flyspell-delayed t
)
23121 ;; How to do this: Measure non-white length of current string
23122 ;; If equal to column width, we should realign.
23124 (defun org-remap (map &rest commands
)
23125 "In MAP, remap the functions given in COMMANDS.
23126 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
23129 (setq old
(pop commands
) new
(pop commands
))
23130 (if (fboundp 'command-remapping
)
23131 (org-defkey map
(vector 'remap old
) new
)
23132 (substitute-key-definition old new map global-map
)))))
23134 (when (eq org-enable-table-editor
'optimized
)
23135 ;; If the user wants maximum table support, we need to hijack
23136 ;; some standard editing functions
23137 (org-remap org-mode-map
23138 'self-insert-command
'org-self-insert-command
23139 'delete-char
'org-delete-char
23140 'delete-backward-char
'org-delete-backward-char
)
23141 (org-defkey org-mode-map
"|" 'org-force-self-insert
))
23143 (defun org-shiftcursor-error ()
23144 "Throw an error because Shift-Cursor command was applied in wrong context."
23145 (error "This command is active in special context like tables, headlines or timestamps"))
23147 (defun org-shifttab (&optional arg
)
23148 "Global visibility cycling or move to previous table field.
23149 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
23151 See the individual commands for more information."
23154 ((org-at-table-p) (call-interactively 'org-table-previous-field
))
23155 (arg (message "Content view to level: ")
23156 (org-content (prefix-numeric-value arg
))
23157 (setq org-cycle-global-status
'overview
))
23158 (t (call-interactively 'org-global-cycle
))))
23160 (defun org-shiftmetaleft ()
23161 "Promote subtree or delete table column.
23162 Calls `org-promote-subtree', `org-outdent-item',
23163 or `org-table-delete-column', depending on context.
23164 See the individual commands for more information."
23167 ((org-at-table-p) (call-interactively 'org-table-delete-column
))
23168 ((org-on-heading-p) (call-interactively 'org-promote-subtree
))
23169 ((org-at-item-p) (call-interactively 'org-outdent-item
))
23170 (t (org-shiftcursor-error))))
23172 (defun org-shiftmetaright ()
23173 "Demote subtree or insert table column.
23174 Calls `org-demote-subtree', `org-indent-item',
23175 or `org-table-insert-column', depending on context.
23176 See the individual commands for more information."
23179 ((org-at-table-p) (call-interactively 'org-table-insert-column
))
23180 ((org-on-heading-p) (call-interactively 'org-demote-subtree
))
23181 ((org-at-item-p) (call-interactively 'org-indent-item
))
23182 (t (org-shiftcursor-error))))
23184 (defun org-shiftmetaup (&optional arg
)
23185 "Move subtree up or kill table row.
23186 Calls `org-move-subtree-up' or `org-table-kill-row' or
23187 `org-move-item-up' depending on context. See the individual commands
23188 for more information."
23191 ((org-at-table-p) (call-interactively 'org-table-kill-row
))
23192 ((org-on-heading-p) (call-interactively 'org-move-subtree-up
))
23193 ((org-at-item-p) (call-interactively 'org-move-item-up
))
23194 (t (org-shiftcursor-error))))
23195 (defun org-shiftmetadown (&optional arg
)
23196 "Move subtree down or insert table row.
23197 Calls `org-move-subtree-down' or `org-table-insert-row' or
23198 `org-move-item-down', depending on context. See the individual
23199 commands for more information."
23202 ((org-at-table-p) (call-interactively 'org-table-insert-row
))
23203 ((org-on-heading-p) (call-interactively 'org-move-subtree-down
))
23204 ((org-at-item-p) (call-interactively 'org-move-item-down
))
23205 (t (org-shiftcursor-error))))
23207 (defun org-metaleft (&optional arg
)
23208 "Promote heading or move table column to left.
23209 Calls `org-do-promote' or `org-table-move-column', depending on context.
23210 With no specific context, calls the Emacs default `backward-word'.
23211 See the individual commands for more information."
23214 ((org-at-table-p) (org-call-with-arg 'org-table-move-column
'left
))
23215 ((or (org-on-heading-p) (org-region-active-p))
23216 (call-interactively 'org-do-promote
))
23217 ((org-at-item-p) (call-interactively 'org-outdent-item
))
23218 (t (call-interactively 'backward-word
))))
23220 (defun org-metaright (&optional arg
)
23221 "Demote subtree or move table column to right.
23222 Calls `org-do-demote' or `org-table-move-column', depending on context.
23223 With no specific context, calls the Emacs default `forward-word'.
23224 See the individual commands for more information."
23227 ((org-at-table-p) (call-interactively 'org-table-move-column
))
23228 ((or (org-on-heading-p) (org-region-active-p))
23229 (call-interactively 'org-do-demote
))
23230 ((org-at-item-p) (call-interactively 'org-indent-item
))
23231 (t (call-interactively 'forward-word
))))
23233 (defun org-metaup (&optional arg
)
23234 "Move subtree up or move table row up.
23235 Calls `org-move-subtree-up' or `org-table-move-row' or
23236 `org-move-item-up', depending on context. See the individual commands
23237 for more information."
23240 ((org-at-table-p) (org-call-with-arg 'org-table-move-row
'up
))
23241 ((org-on-heading-p) (call-interactively 'org-move-subtree-up
))
23242 ((org-at-item-p) (call-interactively 'org-move-item-up
))
23243 (t (org-shiftcursor-error))))
23245 (defun org-metadown (&optional arg
)
23246 "Move subtree down or move table row down.
23247 Calls `org-move-subtree-down' or `org-table-move-row' or
23248 `org-move-item-down', depending on context. See the individual
23249 commands for more information."
23252 ((org-at-table-p) (call-interactively 'org-table-move-row
))
23253 ((org-on-heading-p) (call-interactively 'org-move-subtree-down
))
23254 ((org-at-item-p) (call-interactively 'org-move-item-down
))
23255 (t (org-shiftcursor-error))))
23257 (defun org-shiftup (&optional arg
)
23258 "Increase item in timestamp or increase priority of current headline.
23259 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
23260 depending on context. See the individual commands for more information."
23263 ((org-at-timestamp-p t
) (call-interactively 'org-timestamp-up
))
23264 ((org-on-heading-p) (call-interactively 'org-priority-up
))
23265 ((org-at-item-p) (call-interactively 'org-previous-item
))
23266 (t (call-interactively 'org-beginning-of-item
) (beginning-of-line 1))))
23268 (defun org-shiftdown (&optional arg
)
23269 "Decrease item in timestamp or decrease priority of current headline.
23270 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
23271 depending on context. See the individual commands for more information."
23274 ((org-at-timestamp-p t
) (call-interactively 'org-timestamp-down
))
23275 ((org-on-heading-p) (call-interactively 'org-priority-down
))
23276 (t (call-interactively 'org-next-item
))))
23278 (defun org-shiftright ()
23279 "Next TODO keyword or timestamp one day later, depending on context."
23282 ((org-at-timestamp-p t
) (call-interactively 'org-timestamp-up-day
))
23283 ((org-on-heading-p) (org-call-with-arg 'org-todo
'right
))
23284 ((org-at-property-p) (call-interactively 'org-property-next-allowed-value
))
23285 (t (org-shiftcursor-error))))
23287 (defun org-shiftleft ()
23288 "Previous TODO keyword or timestamp one day earlier, depending on context."
23291 ((org-at-timestamp-p t
) (call-interactively 'org-timestamp-down-day
))
23292 ((org-on-heading-p) (org-call-with-arg 'org-todo
'left
))
23293 ((org-at-property-p)
23294 (call-interactively 'org-property-previous-allowed-value
))
23295 (t (org-shiftcursor-error))))
23297 (defun org-shiftcontrolright ()
23298 "Switch to next TODO set."
23301 ((org-on-heading-p) (org-call-with-arg 'org-todo
'nextset
))
23302 (t (org-shiftcursor-error))))
23304 (defun org-shiftcontrolleft ()
23305 "Switch to previous TODO set."
23308 ((org-on-heading-p) (org-call-with-arg 'org-todo
'previousset
))
23309 (t (org-shiftcursor-error))))
23311 (defun org-ctrl-c-ret ()
23312 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
23315 ((org-at-table-p) (call-interactively 'org-table-hline-and-move
))
23316 (t (call-interactively 'org-insert-heading
))))
23318 (defun org-copy-special ()
23319 "Copy region in table or copy current subtree.
23320 Calls `org-table-copy' or `org-copy-subtree', depending on context.
23321 See the individual commands for more information."
23323 (call-interactively
23324 (if (org-at-table-p) 'org-table-copy-region
'org-copy-subtree
)))
23326 (defun org-cut-special ()
23327 "Cut region in table or cut current subtree.
23328 Calls `org-table-copy' or `org-cut-subtree', depending on context.
23329 See the individual commands for more information."
23331 (call-interactively
23332 (if (org-at-table-p) 'org-table-cut-region
'org-cut-subtree
)))
23334 (defun org-paste-special (arg)
23335 "Paste rectangular region into table, or past subtree relative to level.
23336 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
23337 See the individual commands for more information."
23339 (if (org-at-table-p)
23340 (org-table-paste-rectangle)
23341 (org-paste-subtree arg
)))
23343 (defun org-ctrl-c-ctrl-c (&optional arg
)
23344 "Set tags in headline, or update according to changed information at point.
23346 This command does many different things, depending on context:
23348 - If the cursor is in a headline, prompt for tags and insert them
23349 into the current line, aligned to `org-tags-column'. When called
23350 with prefix arg, realign all tags in the current buffer.
23352 - If the cursor is in one of the special #+KEYWORD lines, this
23353 triggers scanning the buffer for these lines and updating the
23356 - If the cursor is inside a table, realign the table. This command
23357 works even if the automatic table editor has been turned off.
23359 - If the cursor is on a #+TBLFM line, re-apply the formulas to
23362 - If the cursor is inside a table created by the table.el package,
23363 activate that table.
23365 - If the current buffer is a remember buffer, close note and file it.
23366 with a prefix argument, file it without further interaction to the default
23369 - If the cursor is on a <<<target>>>, update radio targets and corresponding
23370 links in this buffer.
23372 - If the cursor is on a numbered item in a plain list, renumber the
23375 (let ((org-enable-table-editor t
))
23377 ((or org-clock-overlays
23378 org-occur-highlights
23379 org-latex-fragment-image-overlays
)
23380 (org-remove-clock-overlays)
23381 (org-remove-occur-highlights)
23382 (org-remove-latex-fragment-image-overlays)
23383 (message "Temporary highlights/overlays removed from current buffer"))
23384 ((and (local-variable-p 'org-finish-function
(current-buffer))
23385 (fboundp org-finish-function
))
23386 (funcall org-finish-function
))
23387 ((org-at-property-p)
23388 (call-interactively 'org-property-action
))
23389 ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp
))
23390 ((org-on-heading-p) (call-interactively 'org-set-tags
))
23391 ((org-at-table.el-p
)
23393 (beginning-of-line 1)
23394 (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
23395 (call-interactively 'table-recognize-table
))
23397 (org-table-maybe-eval-formula)
23399 (call-interactively 'org-table-recalculate
)
23400 (org-table-maybe-recalculate-line))
23401 (call-interactively 'org-table-align
))
23402 ((org-at-item-checkbox-p)
23403 (call-interactively 'org-toggle-checkbox
))
23405 (call-interactively 'org-maybe-renumber-ordered-list
))
23406 ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
23408 ((equal (match-string 1) "TBLFM")
23409 ;; Recalculate the table before this line
23411 (beginning-of-line 1)
23412 (skip-chars-backward " \r\n\t")
23413 (if (org-at-table-p)
23414 (org-call-with-arg 'org-table-recalculate t
))))
23416 (call-interactively 'org-mode-restart
))))
23417 (t (error "C-c C-c can do nothing useful at this location.")))))
23419 (defun org-mode-restart ()
23420 "Restart Org-mode, to scan again for special lines.
23421 Also updates the keyword regular expressions."
23423 (let ((org-inhibit-startup t
)) (org-mode))
23424 (message "Org-mode restarted to refresh keyword and special line setup"))
23426 (defun org-return ()
23427 "Goto next table row or insert a newline.
23428 Calls `org-table-next-row' or `newline', depending on context.
23429 See the individual commands for more information."
23434 (org-table-justify-field-maybe)
23435 (call-interactively 'org-table-next-row
))
23438 (defun org-ctrl-c-minus ()
23439 "Insert separator line in table or modify bullet type in list.
23440 Calls `org-table-insert-hline' or `org-cycle-list-bullet',
23441 depending on context."
23445 (call-interactively 'org-table-insert-hline
))
23447 (call-interactively 'org-cycle-list-bullet
))
23448 (t (error "`C-c -' does have no function here."))))
23450 (defun org-meta-return (&optional arg
)
23451 "Insert a new heading or wrap a region in a table.
23452 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
23453 See the individual commands for more information."
23457 (call-interactively 'org-table-wrap-region
))
23458 (t (call-interactively 'org-insert-heading
))))
23462 ;; Define the Org-mode menus
23463 (easy-menu-define org-tbl-menu org-mode-map
"Tbl menu"
23465 ["Align" org-ctrl-c-ctrl-c
(org-at-table-p)]
23466 ["Next Field" org-cycle
(org-at-table-p)]
23467 ["Previous Field" org-shifttab
(org-at-table-p)]
23468 ["Next Row" org-return
(org-at-table-p)]
23470 ["Blank Field" org-table-blank-field
(org-at-table-p)]
23471 ["Edit Field" org-table-edit-field
(org-at-table-p)]
23472 ["Copy Field from Above" org-table-copy-down
(org-at-table-p)]
23475 ["Move Column Left" org-metaleft
(org-at-table-p)]
23476 ["Move Column Right" org-metaright
(org-at-table-p)]
23477 ["Delete Column" org-shiftmetaleft
(org-at-table-p)]
23478 ["Insert Column" org-shiftmetaright
(org-at-table-p)])
23480 ["Move Row Up" org-metaup
(org-at-table-p)]
23481 ["Move Row Down" org-metadown
(org-at-table-p)]
23482 ["Delete Row" org-shiftmetaup
(org-at-table-p)]
23483 ["Insert Row" org-shiftmetadown
(org-at-table-p)]
23484 ["Sort lines in region" org-table-sort-lines
(org-at-table-p)]
23486 ["Insert Hline" org-ctrl-c-minus
(org-at-table-p)])
23488 ["Copy Rectangle" org-copy-special
(org-at-table-p)]
23489 ["Cut Rectangle" org-cut-special
(org-at-table-p)]
23490 ["Paste Rectangle" org-paste-special
(org-at-table-p)]
23491 ["Fill Rectangle" org-table-wrap-region
(org-at-table-p)])
23494 ["Set Column Formula" org-table-eval-formula
(org-at-table-p)]
23495 ["Set Field Formula" (org-table-eval-formula '(4)) :active
(org-at-table-p) :keys
"C-u C-c ="]
23496 ["Edit Formulas" org-table-edit-formulas
(org-at-table-p)]
23498 ["Recalculate line" org-table-recalculate
(org-at-table-p)]
23499 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active
(org-at-table-p) :keys
"C-u C-c *"]
23500 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active
(org-at-table-p) :keys
"C-u C-u C-c *"]
23502 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks
(org-at-table-p)]
23504 ["Sum Column/Rectangle" org-table-sum
23505 (or (org-at-table-p) (org-region-active-p))]
23506 ["Which Column?" org-table-current-column
(org-at-table-p)])
23508 org-table-toggle-formula-debugger
23509 :style toggle
:selected org-table-formula-debug
]
23510 ["Show Col/Row Numbers"
23511 org-table-toggle-coordinate-overlays
23512 :style toggle
:selected org-table-overlay-coordinates
]
23514 ["Create" org-table-create
(and (not (org-at-table-p))
23515 org-enable-table-editor
)]
23516 ["Convert Region" org-table-convert-region
(not (org-at-table-p 'any
))]
23517 ["Import from File" org-table-import
(not (org-at-table-p))]
23518 ["Export to File" org-table-export
(org-at-table-p)]
23520 ["Create/Convert from/to table.el" org-table-create-with-table.el t
]))
23522 (easy-menu-define org-org-menu org-mode-map
"Org menu"
23525 ["Cycle Visibility" org-cycle
(or (bobp) (outline-on-heading-p))]
23526 ["Cycle Global Visibility" org-shifttab
(not (org-at-table-p))]
23527 ["Sparse Tree" org-occur t
]
23528 ["Reveal Context" org-reveal t
]
23529 ["Show All" show-all t
]
23531 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t
])
23533 ["New Heading" org-insert-heading t
]
23534 ("Navigate Headings"
23535 ["Up" outline-up-heading t
]
23536 ["Next" outline-next-visible-heading t
]
23537 ["Previous" outline-previous-visible-heading t
]
23538 ["Next Same Level" outline-forward-same-level t
]
23539 ["Previous Same Level" outline-backward-same-level t
]
23541 ["Jump" org-goto t
])
23543 ["Move Subtree Up" org-shiftmetaup
(not (org-at-table-p))]
23544 ["Move Subtree Down" org-shiftmetadown
(not (org-at-table-p))]
23546 ["Copy Subtree" org-copy-special
(not (org-at-table-p))]
23547 ["Cut Subtree" org-cut-special
(not (org-at-table-p))]
23548 ["Paste Subtree" org-paste-special
(not (org-at-table-p))]
23550 ["Promote Heading" org-metaleft
(not (org-at-table-p))]
23551 ["Promote Subtree" org-shiftmetaleft
(not (org-at-table-p))]
23552 ["Demote Heading" org-metaright
(not (org-at-table-p))]
23553 ["Demote Subtree" org-shiftmetaright
(not (org-at-table-p))]
23555 ["Sort Region/Children" org-sort
(not (org-at-table-p))]
23557 ["Convert to odd levels" org-convert-to-odd-levels t
]
23558 ["Convert to odd/even levels" org-convert-to-oddeven-levels t
])
23560 ["Emphasis..." org-emphasize t
])
23562 ["Toggle ARCHIVE tag" org-toggle-archive-tag t
]
23563 ; ["Check and Tag Children" (org-toggle-archive-tag (4))
23564 ; :active t :keys "C-u C-c C-x C-a"]
23565 ["Sparse trees open ARCHIVE trees"
23566 (setq org-sparse-tree-open-archived-trees
23567 (not org-sparse-tree-open-archived-trees
))
23568 :style toggle
:selected org-sparse-tree-open-archived-trees
]
23569 ["Cycling opens ARCHIVE trees"
23570 (setq org-cycle-open-archived-trees
(not org-cycle-open-archived-trees
))
23571 :style toggle
:selected org-cycle-open-archived-trees
]
23572 ["Agenda includes ARCHIVE trees"
23573 (setq org-agenda-skip-archived-trees
(not org-agenda-skip-archived-trees
))
23574 :style toggle
:selected
(not org-agenda-skip-archived-trees
)]
23576 ["Move Subtree to Archive" org-advertized-archive-subtree t
]
23577 ; ["Check and Move Children" (org-archive-subtree '(4))
23578 ; :active t :keys "C-u C-c C-x C-s"]
23582 ["TODO/DONE/-" org-todo t
]
23584 ["Next keyword" org-shiftright
(org-on-heading-p)]
23585 ["Previous keyword" org-shiftleft
(org-on-heading-p)]
23586 ["Complete Keyword" org-complete
(assq :todo-keyword
(org-context))]
23587 ["Next keyword set" org-shiftcontrolright
(and (> (length org-todo-sets
) 1) (org-on-heading-p))]
23588 ["Previous keyword set" org-shiftcontrolright
(and (> (length org-todo-sets
) 1) (org-on-heading-p))])
23589 ["Show TODO Tree" org-show-todo-tree t
]
23590 ["Global TODO list" org-todo-list t
]
23592 ["Set Priority" org-priority t
]
23593 ["Priority Up" org-shiftup t
]
23594 ["Priority Down" org-shiftdown t
])
23595 ("TAGS and Properties"
23596 ["Set Tags" 'org-ctrl-c-ctrl-c
(org-at-heading-p)]
23597 ["Change tag in region" 'org-change-tag-in-region
(org-region-active-p)] ;FIXME
23598 ["Column view of properties" org-columns t
])
23599 ("Dates and Scheduling"
23600 ["Timestamp" org-time-stamp t
]
23601 ["Timestamp (inactive)" org-time-stamp-inactive t
]
23603 ["1 Day Later" org-shiftright t
]
23604 ["1 Day Earlier" org-shiftleft t
]
23605 ["1 ... Later" org-shiftup t
]
23606 ["1 ... Earlier" org-shiftdown t
])
23607 ["Compute Time Range" org-evaluate-time-range t
]
23608 ["Schedule Item" org-schedule t
]
23609 ["Deadline" org-deadline t
]
23611 ["Custom time format" org-toggle-time-stamp-overlays
23612 :style radio
:selected org-display-custom-times
]
23614 ["Goto Calendar" org-goto-calendar t
]
23615 ["Date from Calendar" org-date-from-calendar t
])
23617 ["Clock in" org-clock-in t
]
23618 ["Clock out" org-clock-out t
]
23619 ["Clock cancel" org-clock-cancel t
]
23620 ["Display times" org-clock-display t
]
23621 ["Create clock table" org-clock-report t
]
23623 ["Record DONE time"
23624 (progn (setq org-log-done
(not org-log-done
))
23625 (message "Switching to %s will %s record a timestamp"
23626 (car org-done-keywords
)
23627 (if org-log-done
"automatically" "not")))
23628 :style toggle
:selected org-log-done
])
23630 ["Agenda Command..." org-agenda t
]
23631 ("File List for Agenda")
23632 ("Special views current file"
23633 ["TODO Tree" org-show-todo-tree t
]
23634 ["Check Deadlines" org-check-deadlines t
]
23635 ["Timeline" org-timeline t
]
23636 ["Tags Tree" org-tags-sparse-tree t
])
23639 ["Store Link (Global)" org-store-link t
]
23640 ["Insert Link" org-insert-link t
]
23641 ["Follow Link" org-open-at-point t
]
23643 ["Next link" org-next-link t
]
23644 ["Previous link" org-previous-link t
]
23646 ["Descriptive Links"
23647 (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
23648 :style radio
:selected
(member '(org-link) buffer-invisibility-spec
)]
23651 (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
23652 :style radio
:selected
(not (member '(org-link) buffer-invisibility-spec
))])
23654 ["Export/Publish..." org-export t
]
23656 ["Org CDLaTeX mode" org-cdlatex-mode
:style toggle
23657 :selected org-cdlatex-mode
]
23658 ["Insert Environment" cdlatex-environment
(fboundp 'cdlatex-environment
)]
23659 ["Insert math symbol" cdlatex-math-symbol
(fboundp 'cdlatex-math-symbol
)]
23660 ["Modify math symbol" org-cdlatex-math-modify
23661 (org-inside-LaTeX-fragment-p)]
23662 ["Export LaTeX fragments as images"
23663 (setq org-export-with-LaTeX-fragments
(not org-export-with-LaTeX-fragments
))
23664 :style toggle
:selected org-export-with-LaTeX-fragments
])
23667 ["Show Version" org-version t
]
23668 ["Info Documentation" org-info t
])
23670 ["Browse Org Group" org-customize t
]
23672 ["Expand This Menu" org-create-customize-menu
23673 (fboundp 'customize-menu-create
)])
23675 ["Refresh setup" org-mode-restart t
]
23678 (defun org-info (&optional node
)
23679 "Read documentation for Org-mode in the info system.
23680 With optional NODE, go directly to that node."
23683 (Info-goto-node (format "(org)%s" (or node
""))))
23685 (defun org-install-agenda-files-menu ()
23686 (let ((bl (buffer-list)))
23689 (set-buffer (pop bl
))
23690 (if (org-mode-p) (setq bl nil
)))
23693 '("Org") "File List for Agenda"
23696 ["Edit File List" (org-edit-agenda-file-list) t
]
23697 ["Add/Move Current File to Front of List" org-agenda-file-to-front t
]
23698 ["Remove Current File from List" org-remove-file t
]
23699 ["Cycle through agenda files" org-cycle-agenda-files t
]
23701 (mapcar 'org-file-menu-entry
(org-agenda-files t
))))))))
23705 (defun org-customize ()
23706 "Call the customize function with org as argument."
23708 (customize-browse 'org
))
23710 (defun org-create-customize-menu ()
23711 "Create a full customization menu for Org-mode, insert it into the menu."
23713 (if (fboundp 'customize-menu-create
)
23716 '("Org") "Customize"
23717 `(["Browse Org group" org-customize t
]
23719 ,(customize-menu-create 'org
)
23720 ["Set" Custom-set t
]
23721 ["Save" Custom-save t
]
23722 ["Reset to Current" Custom-reset-current t
]
23723 ["Reset to Saved" Custom-reset-saved t
]
23724 ["Reset to Standard Settings" Custom-reset-standard t
]))
23725 (message "\"Org\"-menu now contains full customization menu"))
23726 (error "Cannot expand menu (outdated version of cus-edit.el)")))
23728 ;;;; Miscellaneous stuff
23731 ;;; Generally useful functions
23733 (defun org-context ()
23734 "Return a list of contexts of the current cursor position.
23735 If several contexts apply, all are returned.
23736 Each context entry is a list with a symbol naming the context, and
23737 two positions indicating start and end of the context. Possible
23740 :headline anywhere in a headline
23741 :headline-stars on the leading stars in a headline
23742 :todo-keyword on a TODO keyword (including DONE) in a headline
23743 :tags on the TAGS in a headline
23744 :priority on the priority cookie in a headline
23745 :item on the first line of a plain list item
23746 :item-bullet on the bullet/number of a plain list item
23747 :checkbox on the checkbox in a plain list item
23748 :table in an org-mode table
23749 :table-special on a special filed in a table
23750 :table-table in a table.el table
23751 :link on a hyperlink
23752 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
23753 :target on a <<target>>
23754 :radio-target on a <<<radio-target>>>
23755 :latex-fragment on a LaTeX fragment
23756 :latex-preview on a LaTeX fragment with overlayed preview image
23758 This function expects the position to be visible because it uses font-lock
23759 faces as a help to recognize the following contexts: :table-special, :link,
23761 (let* ((f (get-text-property (point) 'face
))
23762 (faces (if (listp f
) f
(list f
)))
23763 (p (point)) clist o
)
23764 ;; First the large context
23766 ((org-on-heading-p t
)
23767 (push (list :headline
(point-at-bol) (point-at-eol)) clist
)
23769 (beginning-of-line 1)
23770 (looking-at org-todo-line-tags-regexp
))
23771 (push (org-point-in-group p
1 :headline-stars
) clist
)
23772 (push (org-point-in-group p
2 :todo-keyword
) clist
)
23773 (push (org-point-in-group p
4 :tags
) clist
))
23775 (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
23776 (if (looking-at "\\[#[A-Z0-9]\\]")
23777 (push (org-point-in-group p
0 :priority
) clist
)))
23780 (push (org-point-in-group p
2 :item-bullet
) clist
)
23781 (push (list :item
(point-at-bol)
23782 (save-excursion (org-end-of-item) (point)))
23784 (and (org-at-item-checkbox-p)
23785 (push (org-point-in-group p
0 :checkbox
) clist
)))
23788 (push (list :table
(org-table-begin) (org-table-end)) clist
)
23789 (if (memq 'org-formula faces
)
23790 (push (list :table-special
23791 (previous-single-property-change p
'face
)
23792 (next-single-property-change p
'face
)) clist
)))
23793 ((org-at-table-p 'any
)
23794 (push (list :table-table
) clist
)))
23797 ;; Now the small context
23799 ((org-at-timestamp-p)
23800 (push (org-point-in-group p
0 :timestamp
) clist
))
23801 ((memq 'org-link faces
)
23803 (previous-single-property-change p
'face
)
23804 (next-single-property-change p
'face
)) clist
))
23805 ((memq 'org-special-keyword faces
)
23806 (push (list :keyword
23807 (previous-single-property-change p
'face
)
23808 (next-single-property-change p
'face
)) clist
))
23810 (push (org-point-in-group p
0 :target
) clist
)
23811 (goto-char (1- (match-beginning 0)))
23812 (if (looking-at org-radio-target-regexp
)
23813 (push (org-point-in-group p
0 :radio-target
) clist
))
23815 ((setq o
(car (delq nil
23818 (if (memq x org-latex-fragment-image-overlays
) x
))
23819 (org-overlays-at (point))))))
23820 (push (list :latex-fragment
23821 (org-overlay-start o
) (org-overlay-end o
)) clist
)
23822 (push (list :latex-preview
23823 (org-overlay-start o
) (org-overlay-end o
)) clist
))
23824 ((org-inside-LaTeX-fragment-p)
23825 ;; FIXME: positions wrong.
23826 (push (list :latex-fragment
(point) (point)) clist
)))
23828 (setq clist
(nreverse (delq nil clist
)))
23831 ;; FIXME Compare with at-regexp-p
23832 (defun org-in-regexp (re &optional nlines visually
)
23833 "Check if point is inside a match of regexp.
23834 Normally only the current line is checked, but you can include NLINES extra
23835 lines both before and after point into the search.
23836 If VISUALLY is set, require that the cursor is not after the match but
23837 really on, so that the block visually is on the match."
23839 (let ((pos (point))
23840 (eol (point-at-eol (+ 1 (or nlines
0))))
23841 (inc (if visually
1 0)))
23843 (beginning-of-line (- 1 (or nlines
0)))
23844 (while (re-search-forward re eol t
)
23845 (if (and (<= (match-beginning 0) pos
)
23846 (>= (+ inc
(match-end 0)) pos
))
23847 (throw 'exit
(cons (match-beginning 0) (match-end 0)))))))))
23849 (defun org-at-regexp-p (regexp)
23850 "Is point inside a match of REGEXP in the current line?"
23853 (let ((pos (point)) (end (point-at-eol)))
23854 (beginning-of-line 1)
23855 (while (re-search-forward regexp end t
)
23856 (if (and (<= (match-beginning 0) pos
)
23857 (>= (match-end 0) pos
))
23861 (defun org-uniquify (list)
23862 "Remove duplicate elements from LIST."
23864 (mapc (lambda (x) (add-to-list 'res x
'append
)) list
)
23867 (defun org-delete-all (elts list
)
23868 "Remove all elements in ELTS from LIST."
23870 (setq list
(delete (pop elts
) list
)))
23873 (defun org-point-in-group (point group
&optional context
)
23874 "Check if POINT is in match-group GROUP.
23875 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
23876 match. If the match group does ot exist or point is not inside it,
23878 (and (match-beginning group
)
23879 (>= point
(match-beginning group
))
23880 (<= point
(match-end group
))
23882 (list context
(match-beginning group
) (match-end group
))
23885 (defun org-combine-plists (&rest plists
)
23886 "Create a single property list from all plists in PLISTS.
23887 The process starts by copying the first list, and then setting properties
23888 from the other lists. Settings in the last list are the most significant
23889 ones and overrule settings in the other lists."
23890 (let ((rtn (copy-sequence (pop plists
)))
23893 (setq ls
(pop plists
))
23895 (setq p
(pop ls
) v
(pop ls
))
23896 (setq rtn
(plist-put rtn p v
))))
23899 (defun org-move-line-down (arg)
23900 "Move the current line down. With prefix argument, move it past ARG lines."
23902 (let ((col (current-column))
23904 (beginning-of-line 1) (setq beg
(point))
23905 (beginning-of-line 2) (setq end
(point))
23906 (beginning-of-line (+ 1 arg
))
23907 (setq pos
(move-marker (make-marker) (point)))
23908 (insert (delete-and-extract-region beg end
))
23910 (move-to-column col
)))
23912 (defun org-move-line-up (arg)
23913 "Move the current line up. With prefix argument, move it past ARG lines."
23915 (let ((col (current-column))
23917 (beginning-of-line 1) (setq beg
(point))
23918 (beginning-of-line 2) (setq end
(point))
23919 (beginning-of-line (- arg
))
23920 (setq pos
(move-marker (make-marker) (point)))
23921 (insert (delete-and-extract-region beg end
))
23923 (move-to-column col
)))
23925 (defun org-replace-escapes (string table
)
23926 "Replace %-escapes in STRING with values in TABLE.
23927 TABLE is an association list with keys line \"%a\" and string values.
23928 The sequences in STRING may contain normal field width and padding information,
23929 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
23930 so values can contain further %-escapes if they are define later in TABLE."
23931 (let ((case-fold-search nil
)
23933 (while (setq e
(pop table
))
23934 (setq re
(concat "%-?[0-9.]*" (substring (car e
) 1)))
23935 (while (string-match re string
)
23936 (setq rpl
(format (concat (substring (match-string 0 string
) 0 -
1) "s")
23938 (setq string
(replace-match rpl t t string
))))
23942 (defun org-sublist (list start end
)
23943 "Return a section of LIST, from START to END.
23944 Counting starts at 1."
23945 (let (rtn (c start
))
23946 (setq list
(nthcdr (1- start
) list
))
23947 (while (and list
(<= c end
))
23948 (push (pop list
) rtn
)
23952 (defun org-find-base-buffer-visiting (file)
23953 "Like `find-buffer-visiting' but alway return the base buffer and
23954 not an indirect buffer"
23955 (let ((buf (find-buffer-visiting file
)))
23956 (or (buffer-base-buffer buf
) buf
)))
23958 (defun org-image-file-name-regexp ()
23959 "Return regexp matching the file names of images."
23960 (if (fboundp 'image-file-name-regexp
)
23961 (image-file-name-regexp)
23962 (let ((image-file-name-extensions
23963 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
23964 "xbm" "xpm" "pbm" "pgm" "ppm")))
23966 (regexp-opt (nconc (mapcar 'upcase
23967 image-file-name-extensions
)
23968 image-file-name-extensions
)
23972 (defun org-file-image-p (file)
23973 "Return non-nil if FILE is an image."
23975 (string-match (org-image-file-name-regexp) file
)))
23977 ;;; Paragraph filling stuff.
23978 ;; We want this to be just right, so use the full arsenal.
23980 (defun org-indent-line-function ()
23981 "Indent line like previous, but further if previous was headline or item."
23983 (let* ((pos (point))
23984 (itemp (org-at-item-p))
23985 column bpos bcol tpos tcol bullet btype bullet-type
)
23986 ;; Find the previous relevant line
23987 (beginning-of-line 1)
23989 ((looking-at "#") (setq column
0))
23990 ((looking-at "\\*+ ") (setq column
0))
23992 (beginning-of-line 0)
23993 (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
23994 (beginning-of-line 0))
23996 ((looking-at "\\*+[ \t]+")
23997 (goto-char (match-end 0))
23998 (setq column
(current-column)))
24000 (org-beginning-of-item)
24001 ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
24002 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
24003 (setq bpos
(match-beginning 1) tpos
(match-end 0)
24004 bcol
(progn (goto-char bpos
) (current-column))
24005 tcol
(progn (goto-char tpos
) (current-column))
24006 bullet
(match-string 1)
24007 bullet-type
(if (string-match "[0-9]" bullet
) "n" bullet
))
24011 (beginning-of-line 1)
24012 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
24013 (setq bullet
(match-string 1)
24014 btype
(if (string-match "[0-9]" bullet
) "n" bullet
))
24015 (setq column
(if (equal btype bullet-type
) bcol tcol
))))
24016 (t (setq column
(org-get-indentation))))))
24018 (if (<= (current-column) (current-indentation))
24019 (indent-line-to column
)
24020 (save-excursion (indent-line-to column
)))
24021 (setq column
(current-column))
24022 (beginning-of-line 1)
24024 "\\([ \t]+\\)\\(:[0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
24025 (replace-match (concat "\\1" (format org-property-format
24026 (match-string 2) (match-string 3)))
24028 (move-to-column column
)))
24030 (defun org-set-autofill-regexps ()
24032 ;; In the paragraph separator we include headlines, because filling
24033 ;; text in a line directly attached to a headline would otherwise
24034 ;; fill the headline as well.
24035 (org-set-local 'comment-start-skip
"^#+[ \t]*")
24036 (org-set-local 'paragraph-separate
"\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
24037 ;; FIXME!!!!!!! (org-set-local 'paragraph-separate "\f\\|[ ]*$")
24038 ;; The paragraph starter includes hand-formatted lists.
24039 (org-set-local 'paragraph-start
24040 "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
24041 ;; Inhibit auto-fill for headers, tables and fixed-width lines.
24042 ;; But only if the user has not turned off tables or fixed-width regions
24044 'auto-fill-inhibit-regexp
24045 (concat "\\*+ \\|#\\+"
24046 "\\|[ \t]*" org-keyword-time-regexp
24047 (if (or org-enable-table-editor org-enable-fixed-width-editor
)
24050 (if org-enable-table-editor
"|" "")
24051 (if org-enable-fixed-width-editor
":" "")
24053 ;; We use our own fill-paragraph function, to make sure that tables
24054 ;; and fixed-width regions are not wrapped. That function will pass
24055 ;; through to `fill-paragraph' when appropriate.
24056 (org-set-local 'fill-paragraph-function
'org-fill-paragraph
)
24057 ; Adaptive filling: To get full control, first make sure that
24058 ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
24059 (org-set-local 'adaptive-fill-regexp
"\000")
24060 (org-set-local 'adaptive-fill-function
24061 'org-adaptive-fill-function
))
24063 (defun org-fill-paragraph (&optional justify
)
24064 "Re-align a table, pass through to fill-paragraph if no table."
24065 (let ((table-p (org-at-table-p))
24066 (table.el-p
(org-at-table.el-p
)))
24067 (cond ((equal (char-after (point-at-bol)) ?
*) t
) ; skip headlines
24068 (table.el-p t
) ; skip table.el tables
24069 (table-p (org-table-align) t
) ; align org-mode tables
24070 (t nil
)))) ; call paragraph-fill
24072 ;; For reference, this is the default value of adaptive-fill-regexp
24073 ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
24075 (defun org-adaptive-fill-function ()
24076 "Return a fill prefix for org-mode files.
24077 In particular, this makes sure hanging paragraphs for hand-formatted lists
24079 (cond ((looking-at "#[ \t]+")
24081 ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
24083 (goto-char (match-end 0))
24084 (make-string (current-column) ?\
)))
24087 ;;;; Functions extending outline functionality
24089 (defun org-beginning-of-line (&optional arg
)
24090 "Go to the beginning of the current line. If that is invisible, continue
24091 to a visible line beginning. This makes the function of C-a more intuitive.
24092 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
24093 first attempt, and only move to after the tags when the cursor is already
24094 beyond the end of the headline."
24096 (let ((pos (point)))
24097 (beginning-of-line 1)
24101 (if (org-invisible-p)
24102 (while (and (not (bobp)) (org-invisible-p))
24104 (beginning-of-line 1))
24106 (when org-special-ctrl-a
/e
24108 ((and (looking-at org-todo-line-regexp
)
24109 (= (char-after (match-end 1)) ?\
))
24111 (if (eq org-special-ctrl-a
/e t
)
24112 (cond ((> pos
(match-beginning 3)) (match-beginning 3))
24113 ((= pos
(point)) (match-beginning 3))
24115 (cond ((> pos
(point)) (point))
24116 ((not (eq last-command this-command
)) (point))
24117 (t (match-beginning 3))))))
24120 (if (eq org-special-ctrl-a
/e t
)
24121 (cond ((> pos
(match-end 4)) (match-end 4))
24122 ((= pos
(point)) (match-end 4))
24124 (cond ((> pos
(point)) (point))
24125 ((not (eq last-command this-command
)) (point))
24126 (t (match-end 4))))))))))
24128 (defun org-end-of-line (&optional arg
)
24129 "Go to the end of the line.
24130 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
24131 first attempt, and only move to after the tags when the cursor is already
24132 beyond the end of the headline."
24134 (if (or (not org-special-ctrl-a
/e
)
24135 (not (org-on-heading-p)))
24137 (let ((pos (point)))
24138 (beginning-of-line 1)
24139 (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
24140 (if (eq org-special-ctrl-a
/e t
)
24141 (if (or (< pos
(match-beginning 1))
24142 (= pos
(match-end 0)))
24143 (goto-char (match-beginning 1))
24144 (goto-char (match-end 0)))
24145 (if (or (< pos
(match-end 0)) (not (eq this-command last-command
)))
24146 (goto-char (match-end 0))
24147 (goto-char (match-beginning 1))))
24148 (end-of-line arg
)))))
24150 (define-key org-mode-map
"\C-a" 'org-beginning-of-line
)
24151 (define-key org-mode-map
"\C-e" 'org-end-of-line
)
24153 (defun org-invisible-p ()
24154 "Check if point is at a character currently not visible."
24155 ;; Early versions of noutline don't have `outline-invisible-p'.
24156 (if (fboundp 'outline-invisible-p
)
24157 (outline-invisible-p)
24158 (get-char-property (point) 'invisible
)))
24160 (defun org-invisible-p2 ()
24161 "Check if point is at a character currently not visible."
24163 (if (and (eolp) (not (bobp))) (backward-char 1))
24164 ;; Early versions of noutline don't have `outline-invisible-p'.
24165 (if (fboundp 'outline-invisible-p
)
24166 (outline-invisible-p)
24167 (get-char-property (point) 'invisible
))))
24169 (defalias 'org-back-to-heading
'outline-back-to-heading
)
24170 (defalias 'org-on-heading-p
'outline-on-heading-p
)
24171 (defalias 'org-at-heading-p
'outline-on-heading-p
)
24172 (defun org-at-heading-or-item-p ()
24173 (or (org-on-heading-p) (org-at-item-p)))
24175 (defun org-on-target-p ()
24176 (or (org-in-regexp org-radio-target-regexp
)
24177 (org-in-regexp org-target-regexp
)))
24179 (defun org-up-heading-all (arg)
24180 "Move to the heading line of which the present line is a subheading.
24181 This function considers both visible and invisible heading lines.
24182 With argument, move up ARG levels."
24183 (if (fboundp 'outline-up-heading-all
)
24184 (outline-up-heading-all arg
) ; emacs 21 version of outline.el
24185 (outline-up-heading arg t
))) ; emacs 22 version of outline.el
24187 (defun org-goto-sibling (&optional previous
)
24188 "Goto the next sibling, even if it is invisible.
24189 When PREVIOUS is set, go to the previous sibling instead. Returns t
24190 when a sibling was found. When none is found, return nil and don't
24192 (let ((fun (if previous
're-search-backward
're-search-forward
))
24194 (re (concat "^" outline-regexp
))
24196 (when (condition-case nil
(org-back-to-heading t
) (error nil
))
24197 (setq level
(funcall outline-level
))
24199 (or previous
(forward-char 1))
24200 (while (funcall fun re nil t
)
24201 (setq l
(funcall outline-level
))
24202 (when (< l level
) (goto-char pos
) (throw 'exit nil
))
24203 (when (= l level
) (goto-char (match-beginning 0)) (throw 'exit t
)))
24207 (defun org-show-siblings ()
24208 "Show all siblings of the current headline."
24210 (while (org-goto-sibling) (org-flag-heading nil
)))
24212 (while (org-goto-sibling 'previous
)
24213 (org-flag-heading nil
))))
24215 (defun org-show-hidden-entry ()
24216 "Show an entry where even the heading is hidden."
24220 (defun org-flag-heading (flag &optional entry
)
24221 "Flag the current heading. FLAG non-nil means make invisible.
24222 When ENTRY is non-nil, show the entire entry."
24224 (org-back-to-heading t
)
24225 ;; Check if we should show the entire entry
24230 (and (outline-next-heading)
24231 (org-flag-heading nil
))))
24232 (outline-flag-region (max (point-min) (1- (point)))
24233 (save-excursion (outline-end-of-heading) (point))
24236 (defun org-end-of-subtree (&optional invisible-OK to-heading
)
24237 ;; This is an exact copy of the original function, but it uses
24238 ;; `org-back-to-heading', to make it work also in invisible
24239 ;; trees. And is uses an invisible-OK argument.
24240 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
24241 (org-back-to-heading invisible-OK
)
24243 (level (funcall outline-level
)))
24244 (while (and (not (eobp))
24245 (or first
(> (funcall outline-level
) level
)))
24247 (outline-next-heading))
24249 (if (memq (preceding-char) '(?
\n ?\^M
))
24251 ;; Go to end of line before heading
24253 (if (memq (preceding-char) '(?
\n ?\^M
))
24254 ;; leave blank line before heading
24255 (forward-char -
1))))))
24258 (defun org-show-subtree ()
24259 "Show everything after this heading at deeper levels."
24260 (outline-flag-region
24263 (outline-end-of-subtree) (outline-next-heading) (point))
24266 (defun org-show-entry ()
24267 "Show the body directly following this heading.
24268 Show the heading too, if it is currently invisible."
24271 (org-back-to-heading t
)
24272 (outline-flag-region
24273 (max (point-min) (1- (point)))
24275 (re-search-forward (concat "[\r\n]\\(" outline-regexp
"\\)") nil
'move
)
24276 (or (match-beginning 1) (point-max)))
24279 (defun org-make-options-regexp (kwds)
24280 "Make a regular expression for keyword lines."
24284 (mapconcat 'regexp-quote kwds
"\\|")
24288 ;; Make isearch reveal the necessary context
24289 (defun org-isearch-end ()
24290 "Reveal context after isearch exits."
24291 (when isearch-success
; only if search was successful
24292 (if (featurep 'xemacs
)
24293 ;; Under XEmacs, the hook is run in the correct place,
24294 ;; we directly show the context.
24295 (org-show-context 'isearch
)
24296 ;; In Emacs the hook runs *before* restoring the overlays.
24297 ;; So we have to use a one-time post-command-hook to do this.
24298 ;; (Emacs 22 has a special variable, see function `org-mode')
24299 (unless (and (boundp 'isearch-mode-end-hook-quit
)
24300 isearch-mode-end-hook-quit
)
24301 ;; Only when the isearch was not quitted.
24302 (org-add-hook 'post-command-hook
'org-isearch-post-command
24303 'append
'local
)))))
24305 (defun org-isearch-post-command ()
24306 "Remove self from hook, and show context."
24307 (remove-hook 'post-command-hook
'org-isearch-post-command
'local
)
24308 (org-show-context 'isearch
))
24311 ;;;; Address problems with some other packages
24313 ;; Make flyspell not check words in links, to not mess up our keymap
24314 (defun org-mode-flyspell-verify ()
24315 "Don't let flyspell put overlays at active buttons."
24316 (not (get-text-property (point) 'keymap
)))
24318 ;; Make `bookmark-jump' show the jump location if it was hidden.
24319 (eval-after-load "bookmark"
24320 '(if (boundp 'bookmark-after-jump-hook
)
24321 ;; We can use the hook
24322 (add-hook 'bookmark-after-jump-hook
'org-bookmark-jump-unhide
)
24323 ;; Hook not available, use advice
24324 (defadvice bookmark-jump
(after org-make-visible activate
)
24325 "Make the position visible."
24326 (org-bookmark-jump-unhide))))
24328 (defun org-bookmark-jump-unhide ()
24329 "Unhide the current position, to show the bookmark location."
24331 (or (org-invisible-p)
24332 (save-excursion (goto-char (max (point-min) (1- (point))))
24333 (org-invisible-p)))
24334 (org-show-context 'bookmark-jump
)))
24336 ;; Make session.el ignore our circular variable
24337 (eval-after-load "session"
24338 '(add-to-list 'session-globals-exclude
'org-mark-ring
))
24340 ;;;; Experimental code
24342 ;; Make appt aware of appointments from the agenda
24343 (defun org-agenda-to-appt ()
24344 "Activate appointments found in `org-agenda-files'."
24347 (let* ((today (org-date-to-gregorian
24348 (time-to-days (current-time))))
24349 (files org-agenda-files
) entries file
)
24350 (while (setq file
(pop files
))
24351 (setq entries
(append entries
(org-agenda-get-day-entries
24352 file today
:timestamp
))))
24353 (setq entries
(delq nil entries
))
24355 (let* ((event (org-trim (get-text-property 1 'txt x
)))
24356 (time-of-day (get-text-property 1 'time-of-day x
)) tod
)
24358 (setq tod
(number-to-string time-of-day
)
24359 tod
(when (string-match
24360 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)" tod
)
24361 (concat (match-string 1 tod
) ":"
24362 (match-string 2 tod
))))
24363 (if tod
(appt-add tod event
))))) entries
)))
24365 (defun org-closed-in-range ()
24366 "Sparse tree of items closed in a certain time range.
24367 Still experimental, may disappear in the furture."
24369 ;; Get the time interval from the user.
24370 (let* ((time1 (time-to-seconds
24371 (org-read-date nil
'to-time nil
"Starting date: ")))
24372 (time2 (time-to-seconds
24373 (org-read-date nil
'to-time nil
"End date:")))
24374 ;; callback function
24375 (callback (lambda ()
24378 (apply 'encode-time
24379 (org-parse-time-string
24380 (match-string 1))))))
24381 ;; check if time in interval
24382 (and (>= time time1
) (<= time time2
))))))
24383 ;; make tree, check each match with the callback
24384 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback
)))
24386 (defun org-fill-paragraph-experimental (&optional justify
)
24387 "Re-align a table, pass through to fill-paragraph if no table."
24388 (let ((table-p (org-at-table-p))
24389 (table.el-p
(org-at-table.el-p
)))
24390 (cond ((equal (char-after (point-at-bol)) ?
*) t
) ; skip headlines
24391 (table.el-p t
) ; skip table.el tables
24392 (table-p (org-table-align) t
) ; align org-mode tables
24394 (let ((pos (1+ (point-at-eol))))
24395 (backward-paragraph 1)
24396 (re-search-forward "\\\\\\\\[ \t]*$" pos t
)))
24399 (narrow-to-region (1+ (match-end 0)) (point-max))
24400 (fill-paragraph nil
)
24402 (t nil
)))) ; call paragraph-fill
24404 (defun org-get-min-level (lines)
24405 (let ((re "^\\(\\*+\\) ") l min
)
24407 (while (setq l
(pop lines
))
24408 (if (string-match re l
)
24409 (throw 'exit
(org-tr-level (length (match-string 1 l
))))))
24416 (run-hooks 'org-load-hook
)
24418 ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
24419 ;;; org.el ends here